Example #1
0
        private void loadModel()
        {
            var ftype        = (FileExtensionEnum)this.type;
            var tempfullName = Path.Combine(HttpContext.Current.Server.MapPath("~/"), this.path);

            switch (ftype)
            {
            case FileExtensionEnum.Allmethods:
                if (this._mBind == null)
                {
                    this._mBind      = BindModel.ReadModel <BindModel>(tempfullName);
                    this._components = this._mBind.GetComponents();
                }
                break;

            case FileExtensionEnum.PLSBind:
                if (this._mPLS == null)
                {
                    this._mPLS       = BindModel.ReadModel <PLSModel>(tempfullName);
                    this._components = this._mPLS.GetComponents();
                }
                break;

            case FileExtensionEnum.IdLib:
                if (this._mId == null)
                {
                    this._mId        = BindModel.ReadModel <IdentifyModel>(tempfullName);
                    this._components = this._mId.GetComponents();
                }
                break;

            case FileExtensionEnum.FitLib:
                if (this._mFitting == null)
                {
                    this._mFitting   = BindModel.ReadModel <FittingModel>(tempfullName);
                    this._components = this._mFitting.GetComponents();
                }
                break;

            case FileExtensionEnum.PLS1:
            case FileExtensionEnum.PLSANN:
                if (this._mPLS1 == null)
                {
                    this._mPLS1      = BindModel.ReadModel <PLSSubModel>(tempfullName);
                    this._components = this._mPLS1.GetComponents();
                }
                break;

            case FileExtensionEnum.ItgBind:
                if (this._itgSub == null)
                {
                    this._itgSub     = BindModel.ReadModel <IntegrateModel>(tempfullName);
                    this._components = this._itgSub.GetComponents();
                }
                break;

            default:
                break;
            }
        }
Example #2
0
 public SavePanel(PLSSubModel model)
 {
     InitializeComponent();
     if (model == null)
     {
         throw new ArgumentNullException("");
     }
     this._model = model;
     this.Load  += new EventHandler(SavePanel_Load);
 }
Example #3
0
        public override void Predict(List <string> files, object model, int numofId)
        {
            //throw new NotImplementedException();
            PLSSubModel m = model as PLSSubModel;

            if (m == null || files == null)
            {
                throw new ArgumentNullException("");
            }
            if (!this._inited)
            {
                if (this.dataGridView1.InvokeRequired)
                {
                    ThreadStart s = () => { this.init(); };
                    this.dataGridView1.Invoke(s);
                }
                else
                {
                    this.init();
                }
            }
            var error_filelst = new List <string>();
            int rowNum        = 1;

            foreach (var f in files)
            {
                try
                {
                    var spec = new Spectrum(f);
                    var robj = m.Predict(spec);
                    if (this.dataGridView1.InvokeRequired)
                    {
                        ThreadStart s = () => { this.addRow(robj, spec, rowNum, numofId); };
                        this.dataGridView1.Invoke(s);
                    }
                    else
                    {
                        this.addRow(robj, spec, rowNum, numofId);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    error_filelst.Add(new FileInfo(f).Name);
                }
                rowNum++;
            }
            if (error_filelst.Count > 0)
            {
                MessageBox.Show(string.Format("以下{1}条光谱未正确预测:\n{0}",
                                              string.Join(";", error_filelst),
                                              error_filelst.Count
                                              ));
            }
        }
Example #4
0
        private void clear()
        {
            //  this.fittingGridViewCV.Clear();
            //  this.fittingGridViewV.Clear();
            this._model             = null;
            this._cvLst             = null;
            this._vLst              = null;
            this.btnRebuild.Enabled = this._lib != null && this._baseModel != null;

            this.lblInfo.Text = string.Format("光谱库{0},PLS1模型{1}",
                                              this._lib == null ? "未加载" : this._lib.ToString(),
                                              this._baseModel == null ? "未加载" : this._baseModel.ToString());
        }
Example #5
0
 public void ShowGrid(List <RIPP.App.Chem.Forms.Model.MainForm.MyTreeNode> nodes, PLSSubModel model)
 {
     this._Nodes = nodes;
     this._model = model;
     this.init();
     this.dataGridView1.Rows.Clear();
     foreach (var n in this._Nodes)
     {
         var pls = n.PLS.Model;
         this.dataGridView1.Rows.Add(pls.Comp.Name,
                                     pls.Trained ? "已建模" : "未建",
                                     "", pls.Comp != model.Comp, true);
         //this.dataGridView1[ 3,this.dataGridView1.Rows.Count-1].e
     }
     this.ShowDialog();
 }
Example #6
0
        private void btnLoadModel_Click(object sender, EventArgs e)
        {
            OpenFileDialog myOpenFileDialog = new OpenFileDialog();

            myOpenFileDialog.Filter = string.Format("所有文件|*.{0};*.{2}|{1} (*.{0})|*.{0}|{3} (*.{2})|*.{2}",
                                                    FileExtensionEnum.PLS1, FileExtensionEnum.PLS1.GetDescription(),
                                                    FileExtensionEnum.PLSANN, FileExtensionEnum.PLSANN.GetDescription()
                                                    );


            myOpenFileDialog.InitialDirectory = Busi.Common.Configuration.FolderMModel;
            if (myOpenFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            this._baseModel = BindModel.ReadModel <PLSSubModel>(myOpenFileDialog.FileName);
            ToolTip tip = new ToolTip();

            tip.SetToolTip(this.btnLoadModel, myOpenFileDialog.FileName);
            this.clear();
        }
Example #7
0
        private void btnRebuild_Click(object sender, EventArgs e)
        {
            if (this._lib == null || this._baseModel == null)
            {
                return;
            }
            bool needcv = false;

            if (MessageBox.Show("是否进行交互验证和外部验证?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                needcv = true;
            }
            this._model                        = Serialize.DeepClone <PLSSubModel>(this._baseModel);
            this.btnRebuild.Text               = "正在计算";
            this.btnRebuild.Enabled            = false;
            this.btnLoadModel.Enabled          = false;
            this.btnLoadSpec.Enabled           = false;
            this.toolStripProgressBar1.Visible = true;
            Action a = () =>
            {
                if (needcv)
                {
                    this._cvLst = this._model.CrossValidation(this._lib);
                    this._vLst  = this._model.Validation(this._lib);
                }
                else
                {
                    this._cvLst = null;
                    this._vLst  = null;
                }
                this._model.Train(this._lib);

                //显示
                this.showdata();
            };

            a.BeginInvoke(null, null);
        }
Example #8
0
        public void ModelNew()
        {
            OpenFileDialog myOpenFileDialog = new OpenFileDialog();

            myOpenFileDialog.Filter           = string.Format("{1} (*.{0})|*.{0}", FileExtensionEnum.Lib, FileExtensionEnum.Lib.GetDescription());
            myOpenFileDialog.Title            = "选择光谱库";
            myOpenFileDialog.InitialDirectory = Busi.Common.Configuration.FolderSpecLib;
            if (myOpenFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            this.statuTxt.Text            = "正在加载光谱库:";
            this.statuProgressBar.Value   = 0;
            this.statuProgressBar.Visible = true;
            this._fileName = myOpenFileDialog.FileName;

            Action a = () =>
            {
                this._model.LibBase = new SpecBase(this._fileName);
                if (this._model.LibBase == null)
                {
                    return;
                }
                this._model.SubModels = new List <PLSSubModel>();

                // 检查性质个数并给出提示
                if (this._model.LibBase.Components.Count < 1)
                {
                    MessageBox.Show("您导入的光谱库没有性质,请先添加性质", "信息提示");
                }


                this._nodes.Clear();



                foreach (var c in this._model.LibBase.Components)
                {
                    var m = new PLSSubModel();
                    m.ParentModel = this._model;
                    m.Comp        = c;
                    this._model.SubModels.Add(m);

                    this._nodes.Add(new MyTreeNode()
                    {
                        IsFinished  = false,
                        ToolTipText = "双击可编辑子模型",
                        PLS         = new PLSFormContent()
                        {
                            ActiveStep = 1,
                            CVResult   = null,
                            Model      = m
                        }
                    });
                }

                this.treeInit();


                this.statuProgressBar.Value   = 100;
                this.statuProgressBar.Visible = false;
                this.statuTxt.Text            = "光谱库加载完成";
            };

            this.BeginInvoke(a);
        }
Example #9
0
        public void DrawChart(IList <PLS1Result> lst, int factor, bool iscv, PLSSubModel model = null)
        {
            this.guetPanel1.Title = iscv ? "交互验证结果" : "外部验证结果";
            if (lst == null || factor < 1)
            {
                //throw new ArgumentNullException("");
                this.Clear();
                return;
            }
            if (!this._inited)
            {
                this.init(model != null);
            }
            else
            {
                this.gPRESS.Rows.Clear();
            }
            factor--;
            foreach (var r in lst)
            {
                var cmp = r.Comp;
                if (model != null)
                {
                    if (r.MahDist[factor] > model.MDMin)
                    {
                        cmp.State = NIR.ComponentStatu.Red;
                    }
                    else if (r.SR[factor] > model.SRMin)
                    {
                        cmp.State = NIR.ComponentStatu.Blue;
                    }
                    else if (r.ND[factor] > model.NNDMin)
                    {
                        cmp.State = NIR.ComponentStatu.Green;
                    }

                    this.gPRESS.Rows.Add(
                        r.Spec.Name,
                        r.MahDist[factor].ToString("F4"),
                        r.MahDist[factor] < model.MDMin,
                        r.SR[factor].ToString("F4"),
                        r.SR[factor] < model.SRMin,
                        r.ND[factor].ToString("F4"),
                        r.ND[factor] < model.NNDMin,
                        cmp.ActualValue.ToString(cmp.EpsFormatString),
                        r.YLast[factor].ToString(cmp.EpsFormatString),
                        (r.YLast[factor] - cmp.ActualValue).ToString(cmp.EpsFormatString)
                        );
                    this.gPRESS.Rows[this.gPRESS.Rows.Count - 1].Cells[2].Style = cmp.Style;
                    this.gPRESS.Rows[this.gPRESS.Rows.Count - 1].Cells[4].Style = cmp.Style; this.gPRESS.Rows[this.gPRESS.Rows.Count - 1].Cells[6].Style = cmp.Style;
                    this.gPRESS.Rows[this.gPRESS.Rows.Count - 1].Cells[8].Style = cmp.Style;
                    //.Style = FontStyle.Italic;
                }
                else
                {
                    this.gPRESS.Rows.Add(
                        r.Spec.Name,
                        r.MahDist[factor].ToString("F4"),
                        r.SR[factor].ToString("F4"),
                        cmp.ActualValue.ToString(cmp.EpsFormatString),
                        r.YLast[factor].ToString(cmp.EpsFormatString),
                        (r.YLast[factor] - cmp.ActualValue).ToString(cmp.EpsFormatString)
                        );
                }
            }
            for (int i = this.gPRESS.Rows.Count; i < this._rowCount; i++)
            {
                this.gPRESS.Rows.Add(new DataGridViewRow());
            }
        }
Example #10
0
        private void btnModelLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = string.Format("所有方法文件 (*.{0};*.{2};*.{4};*.{6};*.{8};*.{10};*.{12})|*.{0};*.{2};*.{4};*.{6};*.{8};*.{10};*.{12}|{1} (*.{0})|*.{0}|{3} (*.{2})|*.{2}|{5} (*.{4})|*.{4}|{7} (*.{6})|*.{6}|{9} (*.{8})|*.{8}|{11} (*.{10})|*.{10}|{13} (*.{12})|*.{12}",
                                       FileExtensionEnum.Allmethods,
                                       FileExtensionEnum.Allmethods.GetDescription(),
                                       FileExtensionEnum.IdLib,
                                       FileExtensionEnum.IdLib.GetDescription(),
                                       FileExtensionEnum.FitLib,
                                       FileExtensionEnum.FitLib.GetDescription(),
                                       FileExtensionEnum.PLSBind,
                                       FileExtensionEnum.PLSBind.GetDescription(),
                                       FileExtensionEnum.PLS1,
                                       FileExtensionEnum.PLS1.GetDescription(),
                                       FileExtensionEnum.PLSANN,
                                       FileExtensionEnum.PLSANN.GetDescription(),
                                       FileExtensionEnum.ItgBind,
                                       FileExtensionEnum.ItgBind.GetDescription()

                                       );
            dlg.InitialDirectory = Busi.Common.Configuration.DefaultDirectory;
            dlg.Title            = "选择方法";
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            var ftype = BindModel.CheckType(dlg.FileName);

            switch (ftype)
            {
            case FileExtensionEnum.Allmethods:
                this._mBind                    = BindModel.ReadModel <BindModel>(dlg.FileName);
                this._mBind.FullPath           = dlg.FileName;
                this.tabControl1.SelectedIndex = 0;
                break;

            case FileExtensionEnum.PLSBind:
                this._mPLS                     = BindModel.ReadModel <PLSModel>(dlg.FileName);
                this._mPLS.FullPath            = dlg.FileName;
                this.tabControl1.SelectedIndex = 1;
                break;

            case FileExtensionEnum.IdLib:
                this._mId                      = BindModel.ReadModel <IdentifyModel>(dlg.FileName);
                this._mId.FullPath             = dlg.FileName;
                this.tabControl1.SelectedIndex = 2;
                break;

            case FileExtensionEnum.FitLib:
                this._mFitting                 = BindModel.ReadModel <FittingModel>(dlg.FileName);
                this._mFitting.FullPath        = dlg.FileName;
                this.tabControl1.SelectedIndex = 3;
                break;

            case FileExtensionEnum.PLS1:
            case FileExtensionEnum.PLSANN:
                this._mPLS1                    = BindModel.ReadModel <PLSSubModel>(dlg.FileName);
                this._mPLS1.FullPath           = dlg.FileName;
                this.tabControl1.SelectedIndex = 4;
                break;

            case FileExtensionEnum.ItgBind:
                this._itgSub                   = BindModel.ReadModel <IntegrateModel>(dlg.FileName);
                this._itgSub.FullPath          = dlg.FileName;
                this.tabControl1.SelectedIndex = 5;
                break;

            default:
                break;
            }
            this.tabChange();
            var p = this.tabControl1.SelectedTab as Controls.IPanel;

            if (p != null)
            {
                p.Clear();
            }
        }
Example #11
0
        public void DrawChart(IList <PLS1Result> lst, PLSSubModel model, int factor, bool iscv, bool isANN = false)
        {
            this.Clear();
            this.guetPanel2.Title = iscv ? "交互验证结果" : "外部验证结果";
            if (lst == null || factor < 1)
            {
                this.Clear();
                return;
            }
            if (lst.Count == 0)
            {
                return;
            }
            var tl = lst.First();

            //if (tl.ModelResult.SR.Count < factor)
            //{
            //    throw new ArgumentException(string.Format("factor({0})>Maxfactor({1})", factor, tl.ModelResult.SR.Count));
            //}
            factor--;


            var smpIdx = new double[lst.Count];

            for (int i = 0; i < smpIdx.Length; i++)
            {
                smpIdx[i] = i + 1;
            }

            int maxrank = tl.YLast.Length;
            // 1、Error
            var actual  = lst.Select(l => l.Comp.ActualValue).ToArray();
            var predict = lst.Select(l => l.YLast[factor]).ToArray();
            var error   = lst.Select(l => l.YLast[factor] - l.Comp.ActualValue).ToArray();
            var eline   = this.gPR.GraphPane.AddCurve("性质残差图", smpIdx, error, Color.Blue, SymbolType.Plus);

            eline.Line.IsVisible = false;
            this.gPR.AxisChange();
            this.gPR.Refresh();

            // 2、Mash距离
            var mash  = lst.Select(l => l.MahDist[factor]).ToArray();
            var mline = this.gMahDist.GraphPane.AddCurve("马氏距离图", smpIdx, mash, Color.Blue, SymbolType.TriangleDown);

            mline.Line.IsVisible = false;
            this.gMahDist.AxisChange();
            this.gMahDist.Refresh();

            // 3、PRESS

            var PRESS = new double[maxrank];

            foreach (var r in lst)
            {
                var act = r.Comp.ActualValue;
                for (int i = 0; i < PRESS.Length; i++)
                {
                    if (!double.IsNaN(r.YLast[i] - act))
                    {
                        PRESS[i] += Math.Pow((r.YLast[i] - act), 2);
                    }
                }
            }
            if (!isANN)
            {
                var idxFacotr = new double[PRESS.Length];
                for (int i = 0; i < PRESS.Length; i++)
                {
                    idxFacotr[i] = i + 1;
                }
                this.gPRESS.GraphPane.AddCurve("PRESS图", idxFacotr, PRESS, Color.Blue);
                this.gPRESS.AxisChange();
                this.gPRESS.Refresh();
            }

            //预测实际图
            PointPairList pp = new PointPairList(actual, predict);

            var line = this.gPreReal.GraphPane.AddCurve("预测实际图", actual, predict, Color.Blue, SymbolType.Star);

            line.Line.IsVisible = false;

            double a, b;
            var    fity  = RIPP.NIR.Data.Tools.corecurve(actual, predict);
            var    olst  = actual.Select((l, i) => new { i = i, d = l }).OrderBy(d => d.d).ToList();
            var    txaix = new double[actual.Length];
            var    tyaix = new double[actual.Length];

            for (int i = 0; i < actual.Length; i++)
            {
                txaix[i] = olst[i].d;
                tyaix[i] = fity[olst[i].i];
            }
            this.gPreReal.GraphPane.AddCurve("", txaix, tyaix, Color.Red, SymbolType.None);

            this.gPreReal.AxisChange();
            this.gPreReal.Refresh();


            // 计算决定系数R2

            var averageActual = RIPP.NIR.Data.Tools.Mean(actual);

            var cvR = new double[maxrank];
            var vR  = new double[maxrank];

            // var sep = new DenseVector(maxrank);
            for (int f = 0; f < maxrank; f++)
            {
                double ssr     = 0;
                double ssfact  = 0;
                double yperror = 0;
                for (int i = 0; i < lst.Count; i++)
                {
                    if (!double.IsNaN(error[i]))
                    {
                        ssr += error[i] * error[i];
                    }
                    if (!double.IsNaN(actual[i]))
                    {
                        ssfact += Math.Pow(actual[i] - averageActual, 2);
                    }
                    if (!double.IsNaN(predict[i]))
                    {
                        yperror += Math.Pow(predict[i] - averageActual, 2);
                    }
                }
                cvR[f] = 1 - ssr / ssfact;
                vR[f]  = yperror / (yperror + ssr);
            }



            //  var errorVector = new DenseVector(error);
            if (iscv)
            {
                if (isANN)
                {
                    string tmpl = "{0}——{1}={2} , {3}={4}";
                    this.guetPanel2.Title = string.Format(tmpl, "校正-实际图",
                                                          "SEC", model.SEC[factor].ToString("F4"),
                                                          "CR", vR[factor].ToString("F4")
                                                          );
                }
                else
                {
                    string tmpl = "{0}——{1}={2} , {3}={4} , {5}={6} , {7}={8}";
                    this.guetPanel2.Title = string.Format(tmpl, "交互验证结果",
                                                          "SECV", (Math.Sqrt(PRESS[factor] / lst.Count)).ToString("F4"),
                                                          "CVR", cvR[factor].ToString("F4"),
                                                          "SEC", model.SEC[factor].ToString("F4"),
                                                          "CR", model.CR[factor].ToString("F4")
                                                          );
                }
            }
            else
            {
                if (isANN)
                {
                    string tmpl = "{0}——{1}={2} , {3}={4} , {5}={6} , {7}={8}";
                    this.guetPanel2.Title = string.Format(tmpl, "交互验证结果",
                                                          "SEP", Math.Sqrt((RIPP.NIR.Data.Tools.DotProduct(error) / (lst.Count - 1))).ToString("F4"),
                                                          "PR", vR[factor].ToString("F4"),
                                                          "SEM", model.SEM.ToString("F4"),
                                                          "MR", model.MR.ToString("F4")
                                                          );
                }
                else
                {
                    //计算SEC
                    string tmpl = "{0}——{1}={2} , {3}={4}";
                    this.guetPanel2.Title = string.Format(tmpl, "外部验证结果",
                                                          "SEP", Math.Sqrt((RIPP.NIR.Data.Tools.DotProduct(error) / (lst.Count - 1))).ToString("F4"),
                                                          "PR", vR[factor].ToString("F4")
                                                          );
                }
            }
        }