Exemplo n.º 1
0
        public FittingResult Predict(Spectrum specInput, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            var spec = specInput.Clone();
            var y    = new MWNumericArray(specInput.Data.Lenght, 1, specInput.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                y           = Preprocesser.ProcessForPredict(this._filters, y);
                spec.Data.Y = (double[])y.ToVector(MWArrayComponent.Real);
            }

            var handler = Tools.ModelHandler;


            var rlst    = handler.FitPredictor(5, this._lib.X, y, this._wind, (MWNumericArray)this._region.VarIndex, topK);
            var tq      = (MWNumericArray)rlst[0];
            var sq      = (MWNumericArray)rlst[1];
            var ratio   = (MWNumericArray)rlst[2];
            var idx     = (MWNumericArray)rlst[3];
            var fit     = (MWNumericArray)rlst[4];
            var fitspec = spec.Clone();

            fitspec.Components = this._lib.Components.Clone();
            fitspec.Data.Y     = (double[])fit.ToVector(MWArrayComponent.Real);
            var r = new FittingResult()
            {
                SpecOriginal = spec,
                SQ           = sq.ToScalarDouble(),
                TQ           = tq.ToScalarDouble(),
                FitSpec      = fitspec,
                MinSQ        = this._minSQ,
                MinTQ        = this._TQ,
                Wind         = this._wind,
                VarIndex     = this._region.VarIndex
            };
            var flst = new List <FittingSpec>();

            for (int i = 1; i <= ratio.NumberOfElements; i++)
            {
                if ((double)ratio[i] <= double.Epsilon)
                {
                    break;
                }
                flst.Add(new FittingSpec()
                {
                    Spec = this._lib[(int)idx[i] - 1],
                    Rate = (double)ratio[i]
                });
            }

            r.Specs = flst.ToArray();
            this.getResult(ref r);
            r.Result = r.TQ >= this._TQ && r.SQ >= this._minSQ;
            return(r);
        }
Exemplo n.º 2
0
        private static SpecBase mix(Spectrum s, double[] v1, double[] v2, double[] v3, string[] names)
        {
            var lib = new SpecBase();

            lib.Comp_Add(new Component()
            {
                Name = names[0], Eps = 2
            });
            lib.Comp_Add(new Component()
            {
                Name = names[1], Eps = 2
            });
            lib.Comp_Add(new Component()
            {
                Name = names[2], Eps = 2
            });
            var rlst = Tools.ToolHandler.Mix3(4, (MWNumericArray)v1, (MWNumericArray)v2, (MWNumericArray)v3);
            var x    = (MWNumericArray)rlst[0];
            var c1   = (double[])((MWNumericArray)rlst[1]).ToVector(MWArrayComponent.Real);
            var c2   = (double[])((MWNumericArray)rlst[2]).ToVector(MWArrayComponent.Real);
            var c3   = (double[])((MWNumericArray)rlst[3]).ToVector(MWArrayComponent.Real);

            for (int i = 0; i < x.Dimensions[1]; i++)
            {
                var st = s.Clone();
                st.Usage = UsageTypeEnum.Calibrate;
                st.Components.Clear();
                st.Components.Add(new Component()
                {
                    Name        = names[0],
                    ActualValue = c1[i]
                });
                st.Components.Add(new Component()
                {
                    Name        = names[1],
                    ActualValue = c2[i]
                });
                st.Components.Add(new Component()
                {
                    Name        = names[2],
                    ActualValue = c3[i]
                });
                st.Name   = (i + 1).ToString();
                st.Color  = Spectrum.RandomColor();
                st.Data.Y = (double[])Tools.SelectColumn(x, i + 1).ToVector(MWArrayComponent.Real);
                lib.Add(st);
            }
            return(lib);
        }
Exemplo n.º 3
0
        public IdentifyResult Predict(Spectrum speciii, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            if (this._lib == null || speciii == null)
            {
                throw new ArgumentNullException("");
            }
            var spec = speciii.Clone();
            var y    = new MWNumericArray(speciii.Data.Lenght, 1, speciii.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                y           = Preprocesser.ProcessForPredict(this._filters, y);
                spec.Data.Y = (double[])y.ToVector(MWArrayComponent.Real);
            }

            var handler = Tools.ModelHandler;

            //Tools.ToolHandler.Save(this._lib.X, "x", "x");
            //Tools.ToolHandler.Save(y, "y", "y");
            //Tools.ToolHandler.Save(this._baseLib.X, "xa", "xa");
            //this.LibBase.FullPath = "aaaa.lib";
            //this.LibBase.Save();


            this._maxNum = Math.Min(this._maxNum, this._lib.X.Dimensions[1]);
            var rlst  = handler.IdentifyPredictor(3, this._lib.X, y, this._wind, this._maxNum, this._p, this._w, this._t, topK);
            var minSQ = (MWNumericArray)rlst[0];
            var minTQ = (MWNumericArray)rlst[1];
            var ad    = (MWNumericArray)rlst[2];

            var r = new IdentifyResult()
            {
                Spec  = spec,
                MinSQ = this.MinSQ,
                MinTQ = this.TQ
            };
            //设置结果
            var itemlst = new List <IdentifyItem>();

            // r.Items = new IdentifyItem[ad.NumberOfElements];
            for (int i = 0; i < this._maxNum; i++)
            {
                var adidx = (int)ad[i + 1] - 1;
                if (adidx >= this._lib.Count || adidx < 0)
                {
                    break;
                }
                itemlst.Add(new IdentifyItem()
                {
                    Parent       = r,
                    TQ           = (double)minTQ[i + 1],
                    SQ           = (double)minSQ[i + 1],
                    Spec         = this._lib[adidx],
                    Wind         = this._wind,
                    SpecOriginal = r.Spec
                });
            }
            r.Items = itemlst.ToArray();
            r       = GetPredictValue(r, r.Items.Length, numOfId);
            r.IsId  = r.Items.Where(d => d.Result).Count() > 0;
            return(r);
        }
Exemplo n.º 4
0
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (spec1 == null || spec2 == null)
            {
                MessageBox.Show(string.Format("第{0}条光谱为空,请先选择。",
                                              spec1 == null ? 1 : 2), "信息提示");
                return;
            }
            int mwin = 0;

            if (!int.TryParse(txbmwin.Text, out mwin))
            {
                MessageBox.Show("移动窗口大小必须为整数", "信息提示");
                return;
            }
            IList <IFilter> filters = new List <IFilter>();

            if (this._model != null)
            {
                if (this._model is IdentifyModel)
                {
                    filters = ((IdentifyModel)this._model).Filters;
                }
                else if (this._model is FittingModel)
                {
                    filters = ((FittingModel)this._model).Filters;
                }
            }

            var s1 = spec1.Clone();
            var s2 = spec2.Clone();

            //对光谱预处理
            if (filters != null)
            {
                var  ya       = new MWNumericArray(s1.Data.Lenght, 1, s1.Data.Y);
                var  yb       = new MWNumericArray(s2.Data.Lenght, 1, s2.Data.Y);
                bool splitadd = false;
                var  y1       = (MWNumericArray)ya.Clone();
                var  y2       = (MWNumericArray)yb.Clone();
                var  x1       = Serialize.DeepClone <double[]>(s1.Data.X);
                var  x2       = Serialize.DeepClone <double[]>(s2.Data.X);
                var  y1lst    = new List <MWNumericArray>();
                var  x1lst    = new List <double[]>();
                var  y2lst    = new List <MWNumericArray>();
                var  x2lst    = new List <double[]>();

                foreach (var tf in filters)
                {
                    var f = RIPP.Lib.Serialize.DeepClone <RIPP.NIR.Data.Filter.IFilter>(tf);
                    if (f is Spliter)
                    {
                        if (splitadd)
                        {
                            x1lst.Add(x1);
                            y1lst.Add(y1);
                            x2lst.Add(x2);
                            y2lst.Add(y2);
                        }
                        splitadd = false;
                        y1       = (MWNumericArray)ya.Clone();
                        y2       = (MWNumericArray)yb.Clone();
                        x1       = Serialize.DeepClone <double[]>(s1.Data.X);
                        x2       = Serialize.DeepClone <double[]>(s2.Data.X);
                    }
                    else
                    {
                        splitadd = true;
                    }
                    y1 = f.Process(y1);
                    y2 = f.Process(y2);
                    if (f.FType == FilterType.VarFilter)
                    {
                        x1 = f.VarProcess(x1);
                        x2 = f.VarProcess(x2);
                    }
                }
                if (splitadd)
                {
                    x1lst.Add(x1);
                    y1lst.Add(y1);
                    x2lst.Add(x2);
                    y2lst.Add(y2);
                }
                //合并
                if (x1lst.Count > 0)
                {
                    s1.Data.X = x1lst[0];
                    s1.Data.Y = (double[])y1lst[0].ToVector(MWArrayComponent.Real);
                    s2.Data.X = x2lst[0];
                    s2.Data.Y = (double[])y2lst[0].ToVector(MWArrayComponent.Real);
                    for (int i = 1; i < x1lst.Count; i++)
                    {
                        s1.Data.X = RIPP.NIR.Data.Tools.InsertColumn(s1.Data.X, x1lst[i], s1.Data.X.Length + 1);
                        s1.Data.Y = RIPP.NIR.Data.Tools.InsertColumn(s1.Data.Y, (double[])y1lst[i].ToVector(MWArrayComponent.Real), s1.Data.Y.Length + 1);

                        s2.Data.X = RIPP.NIR.Data.Tools.InsertColumn(s2.Data.X, x2lst[i], s2.Data.X.Length + 1);
                        s2.Data.Y = RIPP.NIR.Data.Tools.InsertColumn(s2.Data.Y, (double[])y2lst[i].ToVector(MWArrayComponent.Real), s2.Data.Y.Length + 1);
                    }
                }
                else
                {
                    s1.Data.X = x1;
                    s1.Data.Y = (double[])y1.ToVector(MWArrayComponent.Real);
                    s2.Data.X = x2;
                    s2.Data.Y = (double[])y2.ToVector(MWArrayComponent.Real);
                }

                //s1.Data.Y = (double[])y1.ToVector(MWArrayComponent.Real);
                //s2.Data.Y = (double[])y2.ToVector(MWArrayComponent.Real);
            }
            if (this._model is FittingModel)
            {
                var f = ((FittingModel)this._model).IdRegion;
                s1.Data.X = f.VarProcess(s1.Data.X);
                s2.Data.X = f.VarProcess(s2.Data.X);
                s1.Data.Y = f.VarProcess(s1.Data.Y);
                s2.Data.Y = f.VarProcess(s2.Data.Y);
            }

            //绘制
            var lst = new List <Spectrum>();

            lst.Add(s1);
            lst.Add(s2);
            this.specGraph3.DrawSpec(lst);
            this.specGraph3.SetTitle("预处理后光谱");

            double[] tSQ;
            double   tTQ;

            RIPP.NIR.Data.Tools.MWCorr(s1.Data.Y, s2.Data.Y, mwin, out tTQ, out tSQ);


            var spec = RIPP.Lib.Serialize.DeepClone <Spectrum>(s1);

            spec.Data.Y = tSQ;
            spec.Name   = "SQ";
            spec.Color  = Color.Blue;
            this.specGraph2.DrawSpec(new List <Spectrum>()
            {
                spec
            });

            this.txbTQ.Text = tTQ.ToString("f3");
            this.txbSQ.Text = tSQ.Min().ToString("f3");
        }
Exemplo n.º 5
0
        /// <summary>
        /// 预测
        /// </summary>
        /// <param name="spec"></param>
        /// <param name="needFilter">是否需要前处理</param>
        /// <returns></returns>
        public PLS1Result Predict(Spectrum spec, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            if (spec == null)
            {
                throw new ArgumentNullException("");
            }
            var s = spec.Clone();

            if (s.Data == null || s.Data.Y == null || s.Data.Y.Length == 0)
            {
                return(null);
            }
            var x = new MWNumericArray(s.Data.Lenght, 1, s.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                x        = Preprocesser.ProcessForPredict(this._filters, x);
                s.Data.Y = (double[])x.ToVector(MWArrayComponent.Real);
            }
            var c = this._comp.Clone();

            //定义变量
            double[] SR, MD, ND, Ylast;


            var handler = Tools.ModelHandler;

            if (this._annType != PLSAnnEnum.None)
            {
                var r           = handler.PLS1Predictor(5, x, this._Scores, this._Loads, this._Weights, this._Bias, this._Score_Length, this._centerSpecData, this._centerCompValue, (int)this._method);
                var toolHandler = Tools.ToolHandler;
                // SR,MD,nd,XScores
                Ylast = (double[])((MWNumericArray)r[0]).ToVector(MWArrayComponent.Real);
                SR    = (double[])((MWNumericArray)r[1]).ToVector(MWArrayComponent.Real);
                MD    = (double[])((MWNumericArray)r[2]).ToVector(MWArrayComponent.Real);
                ND    = (double[])((MWNumericArray)r[3]).ToVector(MWArrayComponent.Real);
                var mscores = (MWNumericArray)toolHandler.Centring(1, r[4], this._ScoresMean)[0];


                var annp = (MWNumericArray)handler.annp(1,
                                                        mscores,
                                                        this._annModel.w1,
                                                        this._annModel.b1,
                                                        this._annModel.w2,
                                                        this._annModel.b2,
                                                        this._annArgus.F1.GetDescription(),
                                                        this._annArgus.F2.GetDescription())[0];
                Ylast[this._factor - 1] = annp.ToScalarDouble() + this._centerCompValue;
            }
            else
            {
                var r = handler.PLS1Predictor(5, x, this._Scores, this._Loads, this._Weights, this._Bias, this._Score_Length, this._centerSpecData, this._centerCompValue, (int)this._method);
                Ylast = (double[])((MWNumericArray)r[0]).ToVector(MWArrayComponent.Real);
                SR    = (double[])((MWNumericArray)r[1]).ToVector(MWArrayComponent.Real);
                MD    = (double[])((MWNumericArray)r[2]).ToVector(MWArrayComponent.Real);
                ND    = (double[])((MWNumericArray)r[3]).ToVector(MWArrayComponent.Real);
            }
            c.PredictedValue = Ylast[this.Factor - 1];

            // c.PredictedValue = this._Nonnegative && c.PredictedValue < 0 ? 0 : c.PredictedValue;

            c.PredictedValue = this._Nonnegative && c.PredictedValue < 0 ? Math.Pow(10, -c.Eps)
                : c.PredictedValue;

            if (s.Components != null && s.Components.Contains(c.Name))
            {
                c.ActualValue = s.Components[c.Name].ActualValue;
                c.Error       = c.PredictedValue - c.ActualValue;
            }
            //加入判定条件,设置样式
            int errorcount = 0;

            if (MD[this._factor - 1] > this._MDMin)
            {
                errorcount++;
            }
            else if (SR[this._factor - 1] > this._SRMin)
            {
                errorcount++;
            }
            else if (ND[this._factor - 1] > this._NNDMin)
            {
                errorcount++;
            }
            switch (errorcount)
            {
            case 0:
                c.State = ComponentStatu.Pass;
                break;

            case 1:
                c.State = ComponentStatu.Green;
                break;

            case 2:
                c.State = ComponentStatu.Blue;
                break;

            default:
                c.State = ComponentStatu.Red;
                break;
            }
            PLS1Result item = new PLS1Result()
            {
                Spec    = s,
                MDMin   = this._MDMin,
                NDMin   = this._NNDMin,
                SRMin   = this._SRMin,
                Comp    = c,
                MahDist = MD,
                ND      = ND,
                SR      = SR,
                YLast   = Ylast,
                Factor  = this.Factor
            };

            return(item);
        }