Ejemplo n.º 1
0
        /// <summary>
        /// Run DSS
        /// </summary>
        /// <param name="patientId">Patient Id</param>
        /// <param name="configJson">DSS Config as Json String file</param>
        /// <returns></returns>
        public async Task <IEnumerable <DSSValue> > Run(string patientId, string configJson)
        {
            Dictionary <string, int> valueMapping = new Dictionary <string, int>();
            var config = DSSConfig.FromString(configJson);

            //TODO: Handle Exceptions
            var model = LoadModel(config.DexiFile);

            // Set initial values
            foreach (var c in config.Input)
            {
                model.SetInputValue(c.Name, c.DefaultValue);
            }

            //Get DSS input values
            var values = await GetInputValues(patientId, configJson);

            Dictionary <string, string> dict = new Dictionary <string, string>();

            foreach (var c in values)
            {
                if (!dict.ContainsKey(c.Name))
                {
                    dict.Add(c.Name, c.Value);
                }
            }
            return(Evaluate(model, config, dict));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load From File
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static DSSConfig FromString(string config)
        {
            DSSConfig ret = null;

            try
            {
                ret = JsonConvert.DeserializeObject <DSSConfig>(config);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(ret);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Evaluate
        /// </summary>
        /// <param name="model"></param>
        /// <param name="config"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        private IEnumerable <DSSValue> Evaluate(Model model, DSSConfig config, Dictionary <string, string> values)
        {
            foreach (var parameterInfo in config.Input)
            {
                var key = parameterInfo.Name;

                if (values.ContainsKey(key) && !string.IsNullOrEmpty(values[key]))
                {
                    if (parameterInfo.Numeric)
                    {
                        double v = double.Parse(values[key]);
                        model.SetInputValue(parameterInfo.Name, parameterInfo.GetNumericMapping(v));
                    }
                    else
                    {
                        int?v = parameterInfo.GetCategoryMapping(values[key]);
                        if (v.HasValue)
                        {
                            model.SetInputValue(parameterInfo.Name, v.Value);
                        }
                        else
                        {
                            model.SetInputValue(parameterInfo.Name, parameterInfo.DefaultValue);
                        }
                    }
                }
                else
                {
                    model.SetInputValue(parameterInfo.Name, parameterInfo.DefaultValue);
                }
            }

            model.Evaluate(Model.Evaluation.SET, true);
            var ret = model.Aggregate.Select(e => new DSSValue()
            {
                Name = e.Name, Code = e.Name, Value = e.ValuesString
            });

            return(ret);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="patientId"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private async Task <IEnumerable <DSSValue> > GetInputValues(string patientId, DSSConfig config)
        {
            List <DSSValue> values = new List <DSSValue>();
            //Codes from observations
            List <string> observationCodes = new List <string>();

            //Codes from Meta o
            List <string> metaObservationCodes = new List <string>();

            //Codes from Patient Clinical Info
            List <string> clinicalInfoCodes = new List <string>();

            foreach (var c in config.Input)
            {
                if (string.IsNullOrEmpty(c.Source))
                {
                    //TODO: Add some error to response
                    continue;
                }


                if (c.Source.ToLower() == ObservationType)
                {
                    if (!observationCodes.Contains(c.Code))
                    {
                        observationCodes.Add(c.Code);
                    }
                }
                else if (c.Source.ToLower() == MetaObservationType)
                {
                    if (!metaObservationCodes.Contains(c.Code))
                    {
                        metaObservationCodes.Add(c.Code);
                    }
                }
                else if (c.Source.ToLower() == ClinicalInfoType)
                {
                    if (!clinicalInfoCodes.Contains(c.Code))
                    {
                        clinicalInfoCodes.Add(c.Code);
                    }
                }
                else
                {
                    throw new NotSupportedException($"Source type {c.Source} not supported from PDManager DSS");
                }
            }

            #region Observation Codes


            //TODO: Join observations codes in a single request in case

            foreach (var code in observationCodes)
            {
                try
                {
                    var observations = await _dataProxy.Get <PDObservation>(10, 0, String.Format("{{patientid:\"{0}\",deviceid:\"\",datefrom:{2},dateto:{3},codeid:\"{1}\",aggr:\"total\"}}", patientId, code, (DateTime.Now.AddDays(-config.AggregationPeriodDays).ToUnixTimestampMilli()), (DateTime.Now.ToUnixTimestampMilli())), null);


                    foreach (var observation in config.Input.Where(e => e.Code == code))
                    {
                        values.Add(new DSSValue()
                        {
                            Name = observation.Name, Code = observation.Code, Value = observations.Select(e => e.Value).DefaultIfEmpty(0).Average().ToString()
                        });
                    }

                    // }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            #endregion Meta Observation Codes

            #region Meta Observation Codes


            foreach (var code in metaObservationCodes)
            {
                try
                {
                    //Get Aggregated observation
                    var aggrObservation = await _aggregator.Run(patientId, code, DateTime.Now.AddDays(-config.AggregationPeriodDays));

                    //Find Corresponding observation in config


                    foreach (var metaObservation in config.Input.Where(e => e.Code == code))
                    {
                        //Meta Observations are only numeric
                        values.Add(new DSSValue()
                        {
                            Name = metaObservation.Name, Code = metaObservation.Code, Value = aggrObservation.Select(e => e.Value).DefaultIfEmpty(0).Average().ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            #endregion Observation Codes


            #region Clinical Info

            try
            {
                var patient = await _dataProxy.Get <PDPatient>(patientId);

                var clinicalInfoList = GetClinicalInfoList(patient.ClinicalInfo);
                foreach (var c in clinicalInfoList)
                {
                    var clinicalInfo = config.Input.FirstOrDefault(e => e.Code.ToLower() == c.Code.ToLower());

                    if (clinicalInfo != null)
                    {
                        values.Add(new DSSValue()
                        {
                            Name = clinicalInfo.Name, Code = clinicalInfo.Code, Value = c.Value
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            #endregion Clinical Info


            return(values);
        }