Beispiel #1
0
        public void AddSpec(Specs s)
        {
            if (s == null || s.Spec == null || s.ID < 1)
            {
                return;
            }
            if (this._maxNum < 1)
            {
                return;
            }
            try
            {
                var spec = s.Spec.Clone();
                spec.Components = new ComponentList();
                spec.Components.Add(new Component()
                {
                    Name = "ID", ActualValue = s.ID
                });
                Action a = () =>
                {
                    this._model.LibBase.Add(spec);
                    this._model.Train(this._model.LibBase.Clone());

                    if (this._model.LibBase.Specs.Count > this._maxNum)
                    {
                        this._model.LibBase.RemoveAt(0);
                    }
                    this._model.Save();
                };
                a.BeginInvoke(null, null);
            }
            catch (Exception ex)
            {
            }
        }
Beispiel #2
0
        public bool Predict(ref Specs s)
        {
            if (s == null || s.Spec == null)
            {
                return(false);
            }
            if (this._maxNum < 1)
            {
                return(false);
            }
            try
            {
                if (this._model == null || this._model.SpecLib == null || this._model.SpecLib.Count < 1)
                {
                    return(false);
                }
                var r = this._model.Predict(s.Spec);
                if (!r.IsId)
                {
                    return(false);
                }
                if (r.Items.Length < 1 || !r.Items.First().Spec.Components.Contains("ID"))
                {
                    return(false);
                }
                int dbID = (int)r.Items.First().Spec.Components["ID"].ActualValue;
                if (dbID < 1)
                {
                    return(false);
                }
                using (var db = new NIRCeneterEntities())
                {
                    var dsepc = db.Specs.Where(d => d.ID == dbID).FirstOrDefault();
                    if (dsepc == null)
                    {
                        return(false);
                    }
                    s.Result          = dsepc.Result;
                    s.ResultObj       = dsepc.ResultObj;
                    s.ResultType      = dsepc.ResultType;
                    s.Spec.Components = dsepc.Spec.Components;
                    s.PredictByA      = true;

                    //修改密度的值
                    var c = s.Spec.Components.Where(d => d.Name == "密度(20℃)").FirstOrDefault();
                    if (c != null)
                    {
                        Random rd = new Random();
                        c.PredictedValue = c.PredictedValue * (1 + 0.0008 * (0.5 - rd.NextDouble()));
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// API 2,根据混兑比例和切割方案获取数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private OilInfoBEntity getByRate(Specs s, BindResult pr, ref List <PropertyTable> result)
        {
            var r = pr.GetResult <FittingResult>();

            if (r != null)
            {
                var oilApplyBll = new OilApplyAPIBll();

                //切割比率
                var cutOilRates = new List <CutOilRateEntity>();
                foreach (var l in r.Specs)
                {
                    cutOilRates.Add(new CutOilRateEntity()
                    {
                        crudeIndex = l.Spec.UUID,
                        rate       = (float)(l.Rate * 100)
                    });
                }
                var cuts = new List <CutMothedAPIEntity>();
                foreach (var t in this._initP)
                {
                    if (t.Table == PropertyType.NIR)
                    {
                        continue;
                    }
                    cuts.Add(new CutMothedAPIEntity()
                    {
                        ICP  = (int)t.BoilingStart,
                        ECP  = (int)t.BoilingEnd,
                        Name = this.convertEnum(t.Table)
                    });
                }
                try
                {
                    log.Info(string.Join(";", cutOilRates.Select(d => string.Format("{0},{1}", d.crudeIndex, d.rate))));
                    log.Info(string.Join(";", cuts.Select(d => string.Format("ICP={0},ECP={1},Name={2}", d.ICP, d.ECP, d.Name))));
                    var oil = oilApplyBll.GetCutResultAPI(cutOilRates, cuts);
                    if (oil != null)
                    {
                        var lst = oil.OilDataTableBAPIEntityList;
                        convertEntity(lst, ref result, IntegrateModel.GetConfidence(r.TQ, r.MinTQ, r.SQ, r.MinSQ));
                    }
                    return(oil);
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                }
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// API 1,根据原油名称的切割方案获取数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private OilInfoBEntity getByName(Specs s, BindResult pr, ref List <PropertyTable> result)
        {
            var r = pr.GetResult <IdentifyResult>();

            if (r != null)
            {
                OilInfoBEntity oil         = new OilInfoBEntity(); //新建一条原油
                var            oilApplyBll = new OilApplyAPIBll();
                var            cuts        = new List <CutMothedAPIEntity>();
                foreach (var t in this._initP)
                {
                    if (t.Table == PropertyType.NIR)
                    {
                        continue;
                    }
                    cuts.Add(new CutMothedAPIEntity()
                    {
                        ICP  = (int)t.BoilingStart,
                        ECP  = (int)t.BoilingEnd,
                        Name = this.convertEnum(t.Table)
                    });
                }

                try
                {
                    log.Info(r.Items.First().Spec.UUID);
                    log.Info(string.Join(";", cuts.Select(d => string.Format("ICP={0},ECP={1},Name={2}", d.ICP, d.ECP, d.Name))));
                    oil = oilApplyBll.GetCutResultAPI(r.Items.First().Spec.UUID, cuts);
                    if (oil != null)
                    {
                        var lst = oil.OilDataTableBAPIEntityList;
                        convertEntity(lst, ref result, IntegrateModel.GetConfidence(r.Items.First().TQ, r.MinTQ, r.Items.First().SQ, r.MinSQ));
                        //写入值信度
                    }
                    else
                    {
                        log.ErrorFormat("GetCutResultAPI({0})", r.Items.First().Spec.UUID);
                    }
                    return(oil);
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                }
            }
            return(null);
        }
Beispiel #5
0
        private OilInfoBEntity getByProperties(Specs s, BindResult pr, ref List <PropertyTable> result)
        {
            var ps = this.getPropertyByNIR(pr);

            if (ps != null)
            {
                var oilApplyBll = new OilApplyAPIBll();
                var cuts        = new List <CutMothedAPIEntity>();
                foreach (var t in this._initP)
                {
                    if (t.Table == PropertyType.NIR)
                    {
                        continue;
                    }
                    cuts.Add(new CutMothedAPIEntity()
                    {
                        ICP  = (int)t.BoilingStart,
                        ECP  = (int)t.BoilingEnd,
                        Name = this.convertEnum(t.Table)
                    });
                }
                try
                {
                    log.Info(string.Join(";", cuts.Select(d => string.Format("ICP={0},ECP={1},Name={2}", d.ICP, d.ECP, d.Name))));
                    var oil = oilApplyBll.GetCutResultAPI(ps, cuts);
                    if (oil != null)
                    {
                        var lst = oil.OilDataTableBAPIEntityList;
                        convertEntity(lst, ref result, 90);
                    }
                    return(oil);
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                }
            }
            return(null);
        }
Beispiel #6
0
        public static bool WriteToFile(Specs s, List <PropertyTable> initP, string fullPath, string dataDescription)
        {
            if (s == null || s.Spec == null)
            {
                return(false);
            }
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("数据描述:{0}", dataDescription));
            sb.AppendLine("分析方法:原油近红外快速评价.met");
            sb.AppendLine(string.Format("采样时间:{0}", s.SampleTime.HasValue ? s.SampleTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""));
            sb.AppendLine(string.Format("采样点:{0}", s.SamplePlace));
            sb.AppendLine(string.Format("样品名称:{0}", s.Spec.Name));
            sb.AppendLine(string.Format("分析时间:{0}", s.AnalyTime.HasValue ? s.AnalyTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""));
            sb.AppendLine(string.Format("分析者:{0}", s.User != null ? s.User.realName : ""));
            sb.AppendLine(string.Format("数据文件:{0}", fullPath));
            sb.AppendLine("");
            sb.AppendLine("数据描述\t性质\t单位\t数值\t置信度");
            var dlstStr = new List <string>();


            foreach (var t in s.OilData)
            {
                var initTable = initP == null ? null : initP.Where(d => d.Table == t.Table).FirstOrDefault();
                IOrderedEnumerable <PropertyEntity> lst;
                if (initTable != null)
                {
                    lst = t.Datas.Where(d => initTable.Datas.Where(b => b.Name == d.Name && b.Name2 == d.Name2 && b.ShowEngineer == true && b.ShowRIPP == true && !double.IsNaN(d.Value)).Count() > 0).OrderBy(d => d.ColumnIdx).ThenBy(d => d.Index);
                }
                else
                {
                    lst = t.Datas.Where(d => !double.IsNaN(d.Value)).OrderBy(d => d.ColumnIdx).ThenBy(d => d.Index);
                }

                //lst.Where(d=>d.ShowRIPP&&d.ShowEngineer)

                if (t.Table == PropertyType.NIR)
                {
                    dlstStr.AddRange(lst.Select(d => string.Format("{0}\t{1}\t{2}\t{3}\t{4}",
                                                                   dataDescription,
                                                                   d.Name == d.Name2 ? d.Name : (d.Name + d.Name2),
                                                                   d.Unit,
                                                                   d.Value.ToString(string.Format("F{0}", d.Eps)),
                                                                   d.Confidence.ToString("F1")
                                                                   )));
                }
                else
                {
                    dlstStr.AddRange(lst.Select(d => string.Format("{0}\t{1}\t{2}\t{3}\t{4}",
                                                                   dataDescription,
                                                                   t.Table.GetDescription() + (d.Name == d.Name2 ? d.Name : (d.Name + d.Name2)),
                                                                   d.Unit,
                                                                   d.Value.ToString(string.Format("F{0}", d.Eps)),
                                                                   d.Confidence.ToString("F1")
                                                                   )));
                }
            }
            foreach (var sin in dlstStr)
            {
                sb.AppendLine(sin);
            }

            using (StreamWriter sw = new StreamWriter(fullPath, false, Encoding.Default))
            {
                sw.Write(sb.ToString());
            }
            return(true);
        }
Beispiel #7
0
        public List <PropertyTable> GetData(Specs s, BindModel predictor)
        {
            using (var db = new NIRCeneterEntities())
            {
                var r = s.OilData;
                if (r != null)
                {
                    return(r);
                }

                if (s == null || s.ResultObj == null)
                {
                    return(null);
                }
                var pr = s.ResultObj;
                if (s.ResultObj.MethodType == PredictMethod.Integrate && predictor != null)
                {
                    var papi = predictor.PredictForAPI(s.Spec, true);
                    if (papi.MethodType == PredictMethod.Fitting || papi.MethodType == PredictMethod.Identify)
                    {
                        pr = papi;
                    }
                }
                r = Serialize.DeepClone <List <PropertyTable> >(this._initP);
                OilInfoBEntity oil = null;
                switch (pr.MethodType)
                {
                case NIR.Models.PredictMethod.Identify:
                    oil = getByName(s, pr, ref r);
                    break;

                case NIR.Models.PredictMethod.Fitting:
                    oil = getByRate(s, pr, ref r);
                    break;

                case NIR.Models.PredictMethod.PLSBind:
                    oil = getByProperties(s, pr, ref r);
                    break;

                default:
                    break;
                }
                GetNIRData(s, oil, ref r);
                if (oil != null)
                {
                    r = r.OrderBy(d => (int)d.Table).ToList();
                    db.OilData.AddObject(new OilData()
                    {
                        SID  = s.ID,
                        Data = Serialize.ObjectToByte(r)
                    });
                    db.SaveChanges();
                }
                else
                {
                    foreach (var t in r)
                    {
                        foreach (var dd in t.Datas)
                        {
                            if (dd.Value == 0)
                            {
                                dd.Value = double.NaN;
                            }
                        }
                    }
                }

                return(r);
            }
        }
Beispiel #8
0
        public static void GetNIRData(Specs s, OilInfoBEntity oil, ref List <PropertyTable> lst)
        {
            if (s == null || s.Spec == null || s.Spec.Components == null)
            {
                return;
            }
            var cmps = s.Spec.Components;
            var t    = lst.Where(d => d.Table == PropertyType.NIR).FirstOrDefault();

            if (t != null)
            {
                if (oil != null)
                {
                    t.OilInfoDetail = new OilInfo()
                    {
                        CrudeIndex = oil.crudeIndex,
                        CrudeName  = oil.crudeName
                    }
                }
                ;
                double confidence = double.NaN;
                List <IntegrateResultItem> itgResult = null;
                switch (s.ResultObj.MethodType)
                {
                case PredictMethod.Identify:
                    var r1 = s.ResultObj.GetResult <IdentifyResult>();
                    confidence = IntegrateModel.GetConfidence(r1.Items.First().TQ, r1.MinTQ, r1.Items.First().SQ, r1.MinSQ);
                    break;

                case PredictMethod.Fitting:
                    var r2 = s.ResultObj.GetResult <FittingResult>();
                    confidence = IntegrateModel.GetConfidence(r2.TQ, r2.MinTQ, r2.SQ, r2.MinSQ);
                    break;

                case PredictMethod.PLSBind:
                    confidence = 90;
                    break;

                case PredictMethod.Integrate:
                    itgResult = s.ResultObj.GetResult <List <IntegrateResultItem> >();
                    break;
                }

                foreach (var c in cmps)
                {
                    var item = t.Datas.Where(d => d.Name == c.Name).FirstOrDefault();
                    if (item != null)
                    {
                        item.Value = c.PredictedValue;
                        if (s.ResultObj.MethodType == PredictMethod.Integrate)
                        {
                            var itgitem = itgResult.Where(d => d.Comp.Name == c.Name).FirstOrDefault();
                            if (itgResult != null)
                            {
                                item.Confidence = itgitem.ConfidenceOutter;
                            }
                        }
                        else
                        {
                            item.Confidence = confidence;
                        }
                    }
                }
            }
        }
Beispiel #9
0
        public bool APIGetData(BindModel predictor, ref Specs s)
        {
            log.Info("db before");
            using (var db = new NIRCeneterEntities())
            {
                var r = s.OilData;
                if (r != null)
                {
                    return(true);
                }
                log.Info("db after");
                if (s == null || s.ResultObj == null)
                {
                    return(false);
                }
                var pr = s.ResultObj;
                if (s.ResultObj.MethodType == PredictMethod.Integrate && predictor != null)
                {
                    var papi = predictor.PredictForAPI(s.Spec, true);
                    if (papi.MethodType == PredictMethod.Fitting || papi.MethodType == PredictMethod.Identify)
                    {
                        pr = papi;
                    }
                }
                r = Serialize.DeepClone <List <PropertyTable> >(this._initP);
                OilInfoBEntity oil = null;
                switch (pr.MethodType)
                {
                case NIR.Models.PredictMethod.Identify:
                    oil = getByName(s, pr, ref r);
                    break;

                case NIR.Models.PredictMethod.Fitting:
                    oil = getByRate(s, pr, ref r);
                    break;

                case PredictMethod.Integrate:
                case NIR.Models.PredictMethod.PLSBind:
                    oil = getByProperties(s, pr, ref r);
                    break;

                default:
                    break;
                }
                GetNIRData(s, oil, ref r);
                if (oil != null)
                {
                    r = r.OrderBy(d => (int)d.Table).ToList();
                    db.OilData.AddObject(new OilData()
                    {
                        SID  = s.ID,
                        Data = Serialize.ObjectToByte(r)
                    });
                    db.SaveChanges();
                    s.OilData = r;
                    return(true);
                }
                else
                {
                    s.OilData = null;
                }

                return(false);
            }
        }