示例#1
0
        internal protected void SetSharedVariableToParent(string key, VariableDesc variable)
        {
            if (ParentTask == null)
            {
                throw new InvalidOperationException("No parent");
            }

            ParentTask.SetSharedVariable(key, variable);
        }
示例#2
0
        internal protected void SetSharedVariable(string key, VariableDesc variable)
        {
            if (sharedVariables == null)
            {
                sharedVariables = new VariableSet();
            }

            sharedVariables.Set(key, variable);
        }
        private static bool UpdateItemViewer(TreeView itemViewer, VariableDesc v, IntPtr sf, IntPtr pc)
        {
            foreach (TreeViewItem item in itemViewer.Items)
            {
                if (UpdateSubitems(itemViewer, item, v.Name, v, sf, pc))
                {
                    return(true);
                }
            }

            return(false);
        }
        private static SFItem FindItem(VariableDesc v, ListView view)
        {
            foreach (ListViewItem item in view.Items)
            {
                var sfItem = item.Content as SFItem;
                if (sfItem != null && sfItem.Kind != VariableKind.Pseudo)
                {
                    if (v.Name == sfItem.Name)
                    {
                        return(sfItem);
                    }
                }
            }

            return(null);
        }
 public Tuple<IEnumerable<VariableDesc>, Exception> GetDataSetVariables(StatDataSet oSet)
 {
     List<VariableDesc> oRet = null;
     Exception err = null;
     if (oSet == null)
     {
         return new Tuple<IEnumerable<VariableDesc>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = findDataSet(ctx, oSet);
             if (pSet != null)
             {
                 var q = from x in pSet.Variables orderby x.Name ascending select x;
                 oRet = new List<VariableDesc>();
                 foreach (var p in q)
                 {
                     VariableDesc pp = new VariableDesc();
                     convertVariable(p, pp);
                     oRet.Add(pp);
                 }// p
             }// pSet
         }// ctx
         if ((oRet != null) && (oRet.Count > 1))
         {
             oRet.Sort();
         }
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err);
 }
 protected Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception> readCategValues(statdataEntities ctx,
     VariableDesc oCategVar, IEnumerable<VariableDesc> oDataVars)
 {
     if ((oCategVar == null) || (oDataVars == null))
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentNullException());
     }
     List<VariableDesc> oList = new List<VariableDesc>();
     oList.Add(oCategVar);
     oList.AddRange(oDataVars);
     var xx = getIndexes(ctx, oList);
     if (xx == null)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentException());
     }
     var oIndexes = xx.Item1;
     if ((oIndexes == null) || (xx.Item2 != null))
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, xx.Item2);
     }
     Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>> oRet = new Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>();
     DbVariable pCategVar = findVariable(ctx, oCategVar);
     if (pCategVar == null)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentException());
     }
     var qcateg = pCategVar.Values;
     foreach (var ind in oIndexes)
     {
         var q = from x in qcateg where x.Index == ind select x;
         if (q.Count() > 0)
         {
             DbValue vcateg = q.First();
             String scateg = StatHelpers.ConvertValue(vcateg.Value);
             if (!String.IsNullOrEmpty(scateg))
             {
                 if (!oRet.ContainsKey(scateg))
                 {
                     oRet[scateg] = new Dictionary<VariableDesc, List<ValueDesc>>();
                 }
                 var dict = oRet[scateg];
                 foreach (var vdata in oDataVars)
                 {
                     if (!dict.ContainsKey(vdata))
                     {
                         dict[vdata] = new List<ValueDesc>();
                     }
                     List<ValueDesc> xlist = dict[vdata];
                     var qy = from x in ctx.DbValues where (x.VariableId == vdata.Id) && (x.Index == ind) select x;
                     if (qy.Count() > 0)
                     {
                         var v = qy.First();
                         String s = StatHelpers.ConvertValue(v.Value);
                         if (!String.IsNullOrEmpty(s))
                         {
                             ValueDesc vz = new ValueDesc();
                             convertValue(v, vz);
                             xlist.Add(vz);
                         }
                     }// qy
                 }// vdata
             }// scateg
         }// qCount
     }// ind
     if (oRet.Count < 1)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, null);
     }
     return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(oRet, null);
 }
 protected DbVariable maintainsVariable(statdataEntities ctx, VariableDesc oVar)
 {
     DbVariable pRet = null;
     if (oVar != null)
     {
         String sname = oVar.Name.Trim();
         int nId = oVar.DataSetId;
         String stype = oVar.DataType.Trim();
         if (String.IsNullOrEmpty(sname) || (nId == 0) || String.IsNullOrEmpty(stype))
         {
             return null;
         }
         pRet = findVariable(ctx, oVar);
         if (pRet != null)
         {
             pRet.Name = sname;
             pRet.VarType = stype;
             pRet.IsCateg = oVar.IsCategVar;
             pRet.IsId = oVar.IsIdVar;
             pRet.IsName = oVar.IsNameVar;
             pRet.IsImageVar = oVar.IsImageVar;
             pRet.IsInfoVar = oVar.IsInfoVar;
             pRet.Description = oVar.Description;
         }
         else
         {
             DbDataSet pSet = null;
             {
                 var qq = from x in ctx.DbDataSets where x.Id == nId select x;
                 if (qq.Count() < 1)
                 {
                     return null;
                 }
                 pSet = qq.First();
             }
             pRet = new DbVariable();
             pRet.Id = nextId(ctx, TAB_VARIABLE);
             pRet.DataSet = pSet;
             pRet.Name = sname;
             pRet.VarType = stype;
             pRet.IsCateg = oVar.IsCategVar;
             pRet.IsId = oVar.IsIdVar;
             pRet.IsName = oVar.IsNameVar;
             pRet.IsImageVar = oVar.IsImageVar;
             pRet.IsInfoVar = oVar.IsInfoVar;
             pRet.Description = oVar.Description;
             ctx.DbVariables.Add(pRet);
         }
     }
     return pRet;
 }
 protected DbVariable findVariable(statdataEntities ctx, VariableDesc oVar)
 {
     DbVariable pRet = null;
     if (oVar != null)
     {
         if (oVar.Id != 0)
         {
             var q = from x in ctx.DbVariables where x.Id == oVar.Id select x;
             if (q.Count() > 0)
             {
                 return q.First();
             }
         }
         String sname = oVar.Name.Trim().ToLower();
         int nId = oVar.DataSetId;
         if ((nId != 0) && (!String.IsNullOrEmpty(sname)))
         {
             var q = from x in ctx.DbVariables where (x.DataSetId == nId) && x.Name.Trim().ToLower() == sname select x;
             if (q.Count() > 0)
             {
                 return q.First();
             }
         }
     }// oSet
     return pRet;
 }
 public Tuple<bool, Exception> RemoveVariable(VariableDesc oVar)
 {
     bool bRet = false;
     Exception err = null;
     if (oVar == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbVariable pVar = findVariable(ctx, oVar);
             if (pVar != null)
             {
                 ctx.DbVariables.Remove(pVar);
                 ctx.SaveChanges();
                 bRet = true;
             }
         }
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<bool, Exception>(bRet, err);
 }
 public Tuple<Dictionary<string, double[]>, Exception> ReadNumCategValues(VariableDesc oCategVar, VariableDesc oDataVar)
 {
     Dictionary<string, double[]> oRet = null;
     var xx = ReadCategValues(oCategVar, oDataVar);
     var srcDict = xx.Item1;
     var err = xx.Item2;
     if ((err != null) || (srcDict == null))
     {
         return new Tuple<Dictionary<string, double[]>, Exception>(oRet, err);
     }
     oRet = new Dictionary<string, double[]>();
     var keys = srcDict.Keys;
     foreach (var key in keys)
     {
         List<double> dd = new List<double>();
         var vals = srcDict[key];
         foreach (var v in vals)
         {
             if (v != null)
             {
                 String s = StatHelpers.ConvertValue(v.DataStringValue);
                 if (!String.IsNullOrEmpty(s))
                 {
                     double d = 0.0;
                     if (double.TryParse(s, out d))
                     {
                         dd.Add(d);
                     }
                 }
             }// v
         }// v
         if (dd.Count > 0)
         {
             oRet[key] = dd.ToArray();
         }
     }// key
     return new Tuple<Dictionary<string, double[]>, Exception>(oRet, err);
 }
 private Task<Tuple<StatDataSet, Exception>> readDataSet(String filename,
     sds.DataSet input, CancellationToken cancellationToken, IProgress<int> progress)
 {
     return Task.Run<Tuple<StatDataSet, Exception>>(() =>
     {
         StatDataSet oSet = null;
         Exception err = null;
         try
         {
             FileInfo info = new FileInfo(filename);
             oSet = new StatDataSet();
             oSet.Name = info.Name;
             var col = input.Variables;
             int nTotal = col.Count;
             int nCur = 0;
             foreach (var v in col)
             {
                 if (progress != null)
                 {
                     progress.Report(nCur++);
                 }
                 if (v.Dimensions.Count != 1)
                 {
                     continue;
                 }
                 VariableDesc vv = new VariableDesc();
                 ValueDescs vals = new ValueDescs();
                 vv.Name = v.Name;
                 Type t = v.TypeOfData;
                 if (t == typeof(String))
                 {
                     vv.DataType = "string";
                     vv.IsCategVar = true;
                     String[] data = input.GetData<String[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             vx.DataStringValue = data[i];
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(double))
                 {
                     vv.DataType = "double";
                     vv.IsCategVar = false;
                     double[] data = input.GetData<double[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(myconvert(data[i]));
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(float))
                 {
                     vv.DataType = "float";
                     vv.IsCategVar = false;
                     float[] data = input.GetData<float[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(myconvert(data[i]));
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(int))
                 {
                     vv.DataType = "int";
                     vv.IsCategVar = false;
                     int[] data = input.GetData<int[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(short))
                 {
                     vv.DataType = "short";
                     vv.IsCategVar = false;
                     short[] data = input.GetData<short[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(bool))
                 {
                     vv.DataType = "bool";
                     vv.IsCategVar = true;
                     bool[] data = input.GetData<bool[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 vv.Values = vals;
                 oSet.Variables.Add(vv);
             }// v
         }
         catch (Exception ex)
         {
             err = ex;
         }
         return new Tuple<StatDataSet, Exception>(oSet, err);
     }, cancellationToken);
 }// readDataSet
 void m_main_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
 {
     if (m_busy)
     {
         return;
     }
     m_busy = true;
     String name = e.PropertyName;
     if (name == "IsBusy")
     {
         NotifyPropertyChanged("IsBusy");
         return;
     }
     if (name == "Variables")
     {
         NotifyPropertyChanged("AllVariables");
         this.InitialData = null;
     }
     if (name == "CurrentStatDataSet")
     {
         m_main.RefreshVariables();
     }
     else if (name == "AllNumVariables")
     {
         NotifyPropertyChanged("AllNumVariables");
         this.m_currentvariables = null;
         this.LeftVariables = m_main.AllNumVariables;
         NotifyPropertyChanged("CurrentVariables");
         this.RefreshVariablesValues();
     }
     else if (name == "AllCategVariables")
     {
         this.m_currentcategvariable = null;
         NotifyPropertyChanged("AllCategVariables");
         NotifyPropertyChanged("CurrentcategVariable");
         if (this.CurrentType == TYPE_INDIVS)
         {
             this.UpdatePlot();
         }
     }
     m_busy = false;
 }
 }// RemoveVariables
 public async void AddToDataSet()
 {
     var pAna = this.Anacompo;
     if (pAna == null)
     {
         return;
     }
     if (!pAna.IsValid)
     {
         return;
     }
     var oSet = this.CurrentStatDataSet;
     if (oSet == null)
     {
         return;
     }
     if (!oSet.IsValid)
     {
         return;
     }
     var inds = pAna.RecodedInds;
     if (inds == null)
     {
         return;
     }
     var oIndexes = pAna.Indexes;
     if (oIndexes == null)
     {
         return;
     }
     int nMax = this.FactorCount;
     int nv = this.FactorCount;
     if (nv < 1)
     {
         nv = 3;
     }
     if (nv > nMax)
     {
         nv = nMax;
     }
     int nr = inds.GetLength(0);
     var pMan = this.DataService;
     if (pMan == null)
     {
         return;
     }
     this.IsBusy = true;
     try
     {
         List<VariableDesc> oVars = new List<VariableDesc>();
         String prefix = this.FactorPrefix;
         for (int i = 0; i < nv; ++i)
         {
             VariableDesc oVar = new VariableDesc();
             oVar.DataSetId = oSet.Id;
             oVar.Name = String.Format("{0}{1}", prefix, i + 1);
             oVar.IsCategVar = false;
             oVar.DataType = "float";
             oVar.Description = String.Format("Composante principale {0}", i + 1);
             oVar.IsModified = true;
             List<ValueDesc> vals = new List<ValueDesc>();
             for (int j = 0; j < nr; ++j)
             {
                 ValueDesc v = new ValueDesc();
                 v.Index = oIndexes[j];
                 v.DataStringValue = Convert.ToString(inds[j, i]);
                 v.IsModified = true;
                 vals.Add(v);
             }// j
             oVar.Values = new ValueDescs(vals);
             oVars.Add(oVar);
         }// i
         var xx = await Task.Run<Tuple<bool, Exception>>(() =>
         {
             return pMan.MaintainsVariableAndValues(oVars);
         });
         if ((xx != null) && (xx.Item2 != null))
         {
             this.ShowError(xx.Item2);
         }
     }
     catch (Exception ex)
     {
         this.ShowError(ex);
     }
     this.IsBusy = false;
 }// addDataSet
        private static bool UpdateSubitems(TreeView view, TreeViewItem item, string name, VariableDesc v, IntPtr sf, IntPtr pc)
        {
            string category = item.Tag as string;

            string head, tail;

            if (Strings.SplitHead(name, out head, out tail))
            {
                if (!string.Equals(category, head))
                {
                    return(false);
                }
                else
                {
                    foreach (TreeViewItem subItem in item.Items)
                    {
                        if (UpdateSubitems(view, subItem, tail, v, sf, pc))
                        {
                            return(true);
                        }
                    }

                    Add(view, item, tail, v.Type, v.Value, v.Address.ToString("X"), sf, pc);

                    return(true);
                }
            }
            else
            {
                if (string.Equals(category, name))
                {
                    UpdateItem(item, name, v.Type, v.Value, v.Address.ToString("X"), sf, pc);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
 private static bool IsForVariable(VariableDesc v, TreeViewItem i)
 {
     return(string.Equals(v.Name, i.Tag as string));
 }
 public Tuple<VariableDesc, Exception> GetVariable(VariableDesc oVar)
 {
     VariableDesc oRet = null;
     Exception err = null;
     if (oVar == null)
     {
         return new Tuple<VariableDesc, Exception>(null, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbVariable p = findVariable(ctx, oVar);
             if (p != null)
             {
                 convertVariable(p, oRet);
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<VariableDesc, Exception>(oRet, err);
 }
        public Tuple<IEnumerable<ValueDesc>, Exception> GetVariableValues(VariableDesc oVar, int skip, int taken)
        {
            List<ValueDesc> oRet = new List<ValueDesc>();
            Exception err = null;
            if (oVar == null)
            {
                return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentNullException());
            }
            try
            {
                using (var ctx = getContext())
                {
                    DbVariable pVar = findVariable(ctx, oVar);
                    if (pVar == null)
                    {
                        return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentException());
                    }
                    int nMax = pVar.Values.Count();
                    if (skip < 0)
                    {
                        skip = 0;
                    }
                    if (taken < 0)
                    {
                        taken = 0;
                    }
                    if ((skip + taken) > nMax)
                    {
                        taken = nMax - skip;
                        if (taken < 0)
                        {
                            taken = 0;
                        }
                    }
                    if (taken == 0)
                    {
                        if (skip > 0)
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Skip(skip);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = from x in pVar.Values orderby x.Index ascending select x;
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }

                    }
                    else
                    {
                        if (skip > 0)
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Skip(skip).Take(taken);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Take(taken);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                    }
                }// ctx
            }
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IEnumerable<ValueDesc>, Exception>(oRet, err);
        }
 public PlotModel createNormalPlotModel(VariableDesc oCategVar, VariableDesc oVar)
 {
     if (oVar == null)
     {
         return null;
     }
     var srcData = ComputeVariableCategValues(oCategVar, oVar);
     if (srcData == null)
     {
         return null;
     }
     List<ValueDesc> sortedData = new List<ValueDesc>();
     List<double> oList = new List<double>();
     Dictionary<String, ScatterSeries> oDict = new Dictionary<string, ScatterSeries>();
     foreach (var s in srcData.Keys)
     {
         oDict[s] = new ScatterSeries(s) { MarkerType = MarkerType.Circle };
         var vals = srcData[s];
         foreach (var v in vals)
         {
             v.StringTag = s;
             sortedData.Add(v);
             oList.Add(v.DoubleValue);
         }
     }// s
     sortedData.Sort((v1, v2) =>
     {
         double d1 = v1.DoubleValue;
         double d2 = v2.DoubleValue;
         int nRet = -1;
         if (d1 > d2)
         {
             nRet = 1;
         }
         else if (d1 == d2)
         {
             nRet = 0;
         }
         return nRet;
     });
     int n = sortedData.Count;
     if (n < 2)
     {
         return null;
     }
     DescriptiveStatistics st = new DescriptiveStatistics(oList);
     double mean = st.Mean;
     double dev = st.StandardDeviation;
     if (dev <= 0.0)
     {
         return null;
     }
     var dist = new Normal();
     var s2 = new LineSeries { Title = "Distribution normale" };
     int nTotal = 1;
     int i = 0;
     double dn = (double)n;
     var oAr = sortedData.ToArray();
     ValueDesc vCur = null;
     while (i < n)
     {
         vCur = oAr[i];
         double xMax = vCur.DoubleValue;
         int c = 0;
         while ((vCur.DoubleValue <= xMax) && (i < n))
         {
             double x = vCur.DoubleValue;
             double xr = (x - mean) / dev;
             double yr = dist.CumulativeDistribution(xr);
             s2.Points.Add(new DataPoint(x, yr));
             double yc = (double)nTotal / dn;
             vCur.DoubleTag = yc;
             String scateg = vCur.StringTag;
             if ((!String.IsNullOrEmpty(scateg)) && oDict.ContainsKey(scateg))
             {
                 oDict[scateg].Points.Add(new ScatterPoint(x, yc) { Tag = vCur.Index });
             }
             ++i;
             ++c;
             if (i >= n)
             {
                 break;
             }
             vCur = oAr[i];
         }
         nTotal += c;
     }// i
     PlotModel model = new PlotModel(oVar.Name);
     foreach (var ss in oDict.Values)
     {
         model.Series.Add(ss);
     }
     s2.Smooth = true;
     model.Series.Add(s2);
     return model;
 }
 public Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception> ReadCategValues(VariableDesc oCategVar, IEnumerable<VariableDesc> oDataVars)
 {
     Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>> oRet = null;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             var xx = readCategValues(ctx, oCategVar, oDataVars);
             oRet = xx.Item1;
             err = xx.Item2;
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(oRet, err);
 }
 public IEnumerable<int> GetCommonIndexes(VariableDesc oVar1, VariableDesc oVar2)
 {
     List<VariableDesc> oList = new List<VariableDesc>();
     oList.Add(oVar1);
     oList.Add(oVar2);
     return GetCommonIndexes(oList);
 }
 public Tuple<Dictionary<string, Dictionary<VariableDesc, double[]>>, Exception> ReadNumCategValues(VariableDesc oCategVar, IEnumerable<VariableDesc> oDataVars)
 {
     Dictionary<string, Dictionary<VariableDesc, double[]>> oRet = null;
     Exception err = null;
     try
     {
         Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>> srcDict = null;
         using (var ctx = getContext())
         {
             var xx = readCategValues(ctx, oCategVar, oDataVars);
             srcDict = xx.Item1;
             if ((srcDict == null) || (xx.Item2 != null))
             {
                 return new Tuple<Dictionary<string, Dictionary<VariableDesc, double[]>>, Exception>(null, xx.Item2);
             }
         }// ctx
         oRet = new Dictionary<string, Dictionary<VariableDesc, double[]>>();
         var keys = srcDict.Keys;
         foreach (var key in keys)
         {
             oRet[key] = new Dictionary<VariableDesc, double[]>();
             var curDict = oRet[key];
             var src = srcDict[key];
             foreach (var vdata in src.Keys)
             {
                 List<double> dd = new List<double>();
                 var ll = src[vdata];
                 foreach (var v in ll)
                 {
                     double d = 0.0;
                     if (double.TryParse(StatHelpers.ConvertValue(v.DataStringValue), out d))
                     {
                         dd.Add(d);
                     }
                 }// v
                 curDict[vdata] = dd.ToArray();
             }// vsata
         }// key
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<Dictionary<string, Dictionary<VariableDesc, double[]>>, Exception>(oRet, null);
 }
 public static List<Tuple<double, double, Cluster>> GetClustersCenters(VariableDesc oVarX, VariableDesc oVarY, CategClusterSet oSet)
 {
     List<Tuple<double,double, Cluster>> oList = new List<Tuple<double, double,Cluster>>();
     if ((oVarX == null) || (oVarY == null) || (oSet == null))
     {
         return oList;
     }
     var col = new VariableDesc[] { oVarX, oVarY};
     var tt = GetClustersCenters(col, oSet);
     if (tt != null)
     {
         foreach (var t in tt)
         {
             double[] dd = t.Item1;
             if ((dd != null) && (dd.Length > 1))
             {
                 double x = dd[0];
                 double y = dd[1];
                 Cluster c = t.Item2;
                 oList.Add(new Tuple<double, double, Cluster>(x, y, c));
             }
         }// t
     }// tt
     return oList;
 }
 protected void convertVariable(DbVariable p, VariableDesc pp)
 {
     pp.Id = p.Id;
     pp.DataSetId = p.DataSetId;
     pp.DataType = p.VarType;
     pp.Name = p.Name;
     pp.IsCategVar = p.IsCateg;
     pp.IsIdVar = p.IsId;
     pp.IsNameVar = p.IsName;
     pp.IsImageVar = p.IsImageVar;
     pp.IsInfoVar = p.IsInfoVar;
     pp.Description = p.Description;
     pp.IsModified = false;
 }
 public Dictionary<int, String> ComputeCategDict(VariableDesc oCateg)
 {
     Dictionary<int, String> oRet = new Dictionary<int, string>();
     ValueDescs categVals = null;
     if ((oCateg != null) && oCateg.HasValues)
     {
         categVals = oCateg.Values;
     }
     if (categVals == null)
     {
         return oRet;
     }
     foreach (var v in categVals)
     {
         int key = v.Index;
         String s = StatHelpers.ConvertValue(v.DataStringValue);
         if ((key >= 0) && (!String.IsNullOrEmpty(s)))
         {
             oRet[key] = s;
         }
     }// v
     return oRet;
 }
        protected Tuple<VariableInfo, Exception> getVariableInfo(statdataEntities ctx, VariableDesc oVar)
        {
            Exception err = null;
            VariableInfo info = null;
            if (oVar == null)
            {
                return new Tuple<VariableInfo, Exception>(info, new ArgumentNullException());
            }

            DbVariable pVar = findVariable(ctx, oVar);
            if (pVar == null)
            {
                return new Tuple<VariableInfo, Exception>(info, err);
            }
            info = new VariableInfo();
            String stype = pVar.VarType.Trim().ToLower();
            info.VariableId = pVar.Id;
            info.VariableName = pVar.Name;
            info.DataSetId = pVar.DataSetId;
            info.DataSetName = pVar.DataSet.Name;
            info.DataType = stype;
            info.IsCategVar = pVar.IsCateg;
            if ((stype == "bool") || (stype == "string"))
            {
                info.IsCategVar = true;
            }
            {
                var q1 = pVar.Values;
                int nMissing = 0;
                int nCount = 0;
                foreach (var p in q1)
                {
                    String sval = p.Value;
                    if (!String.IsNullOrEmpty(sval))
                    {
                        sval = StatHelpers.ConvertValue(sval);
                    }
                    if (String.IsNullOrEmpty(sval))
                    {
                        ++nMissing;
                    }
                    ++nCount;
                }// p
                info.MissingValuesCount = nMissing;
                info.TotalValuesCount = nCount;
            }
            if (info.IsCategVar)
            {
                Dictionary<String, int> dict = new Dictionary<string, int>();
                var q2 = from x in pVar.Values select x.Value;
                foreach (var p in q2)
                {
                    String sval = StatHelpers.ConvertValue(p);
                    if (!String.IsNullOrEmpty(sval))
                    {
                        if (!dict.ContainsKey(sval))
                        {
                            dict[sval] = 1;
                        }
                        else
                        {
                            dict[sval] = dict[sval] + 1;
                        }
                    }
                }// p
                CategValueDescs vv = new CategValueDescs();
                var keys = dict.Keys;
                foreach (var s in keys)
                {
                    CategValueDesc c = new CategValueDesc();
                    c.StringValue = s;
                    c.Count = dict[s];
                    c.VariableId = info.VariableId;
                    vv.Add(c);
                }
                info.CategValues = vv;
                info.DistinctValues = keys.ToList();
            }
            else
            {
                var q3 = pVar.Values;
                int nCount = 0;
                List<double> dList = new List<double>();
                foreach (var p in q3)
                {
                    String sval = StatHelpers.ConvertValue(p.Value);
                    if (!String.IsNullOrEmpty(sval))
                    {
                        double xcur = 0;
                        if (double.TryParse(sval, out xcur))
                        {
                            dList.Add(xcur);
                        }// ok
                    }
                }// p
                nCount = dList.Count();
                if (nCount > 0)
                {
                    DescriptiveStatistics st = new DescriptiveStatistics(dList);
                    info.MinValue = st.Minimum;
                    info.MaxValue = st.Maximum;
                    info.MeanValue = myconvert(st.Mean);
                    info.Deviation = myconvert(st.StandardDeviation);
                    info.Skewness = myconvert(st.Skewness);
                    info.Flatness = myconvert(st.Kurtosis);
                    dList.Sort();
                    var oAr = dList.ToArray();
                    info.Quantile05 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.05));
                    info.Quantile10 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.10));
                    info.Quantile25 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.25));
                    info.Median = myconvert(SortedArrayStatistics.Quantile(oAr, 0.5));
                    info.Quantile75 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.75));
                    info.Quantile90 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.90));
                    info.Quantile95 = myconvert(SortedArrayStatistics.Quantile(oAr, 0.10));
                }
            }
            info.IsModified = false;
            //
            return new Tuple<VariableInfo, Exception>(info, err);
        }
 public CorrelData ComputeCorrelation(VariableDesc oVar1, VariableDesc oVar2)
 {
     CorrelData oRet = null;
     if ((oVar1 == null) || (oVar2 == null))
     {
         return null;
     }
     if (!oVar1.IsNumVar)
     {
         return null;
     }
     if (!oVar2.IsNumVar)
     {
         return null;
     }
     try
     {
         var dict = GetCommonValues(new List<VariableDesc>() { oVar1, oVar2 });
         if (dict == null)
         {
             return null;
         }
         var vars = dict.Keys.ToArray();
         if (vars.Length < 2)
         {
             return null;
         }
         List<double> oList1 = new List<double>();
         List<double> oList2 = new List<double>();
         var v1 = vars[0];
         var vals1 = dict[v1];
         var v2 = vars[1];
         var vals2 = dict[v2];
         foreach (var vx in vals1)
         {
             int index = vx.Index;
             var q = from x in vals2 where x.Index == index select x;
             if (q.Count() > 0)
             {
                 var vy = q.First();
                 oList1.Add(vx.DoubleValue);
                 oList2.Add(vy.DoubleValue);
             }
         }// vx
         if (oList1.Count < 4)
         {
             return null;
         }
         oRet = ComputeCorrelation(oList1, oList2);
         if (oRet != null)
         {
             oRet.FirstName = v1.Name;
             oRet.SecondName = v2.Name;
         }
     }
     catch (Exception /*ex */)
     {
         oRet = null;
     }
     return oRet;
 }
 protected Tuple<Dictionary<string, List<ValueDesc>>, Exception> readCategValues(statdataEntities ctx,
     VariableDesc oCategVar, VariableDesc oDataVar)
 {
     Exception err = null;
     Dictionary<String, List<ValueDesc>> xRet = null;
     if ((oCategVar == null) || (oDataVar == null))
     {
         return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         DbVariable pCategVar = findVariable(ctx, oCategVar);
         DbVariable pDataVar = findVariable(ctx, oDataVar);
         if ((pCategVar == null) || (pDataVar == null))
         {
             return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(null, new ArgumentException());
         }
         xRet = new Dictionary<string, List<ValueDesc>>();
         var qcateg = from x in ctx.DbValues where x.VariableId == pCategVar.Id select x;
         foreach (var vcateg in qcateg)
         {
             int index = vcateg.Index;
             String scateg = StatHelpers.ConvertValue(vcateg.Value);
             if ((index >= 0) && (!String.IsNullOrEmpty(scateg)))
             {
                 var qdata = from x in ctx.DbValues where (x.VariableId == pDataVar.Id) && (x.Index == index) select x;
                 foreach (var vdata in qdata)
                 {
                     String sdata = StatHelpers.ConvertValue(vdata.Value);
                     if (!String.IsNullOrEmpty(sdata))
                     {
                         ValueDesc vv = new ValueDesc();
                         convertValue(vdata, vv);
                         if (!xRet.ContainsKey(scateg))
                         {
                             List<ValueDesc> ll = new List<ValueDesc>();
                             ll.Add(vv);
                             xRet[scateg] = ll;
                         }
                         else
                         {
                             (xRet[scateg]).Add(vv);
                         }
                     }// sdata
                 }// vdata
             }// vcateg
         }// vcateg
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(xRet, err);
 }
 public Dictionary<String, ValueDescs> ComputeVariableCategValues(VariableDesc oCateg, VariableDesc oVar)
 {
     Dictionary<String, ValueDescs> oRet = new Dictionary<string, ValueDescs>();
     ValueDescs categVals = oCateg.Values;
     ValueDescs dataVals = oVar.Values;
     if (categVals.Count < 1)
     {
         oRet[DEFAULT_SERIE_NAME] = dataVals;
     }
     else
     {
         foreach (var vcateg in categVals)
         {
             String skey = StatHelpers.ConvertValue(vcateg.DataStringValue);
             if (!String.IsNullOrEmpty(skey))
             {
                 var q = from x in dataVals where x.Index == vcateg.Index select x;
                 if (q.Count() > 0)
                 {
                     var vdata = q.First();
                     String sval = StatHelpers.ConvertValue(vdata.DataStringValue);
                     if (!String.IsNullOrEmpty(sval))
                     {
                         if (!oRet.ContainsKey(skey))
                         {
                             oRet[skey] = new ValueDescs();
                         }
                         oRet[skey].Add(vdata);
                     }// ok
                 }
             }// sKey
         }// vcateg
     }
     return oRet;
 }
        public Tuple<IndivDescs, VariableDescs, Exception> FetchAllDataSetData(StatDataSet oSet)
        {
            IndivDescs indivs = null;
            VariableDescs vars = null;
            Exception err = null;
            try
            {
                using (var ctx = getContext())
                {
                    DbDataSet pSet = findDataSet(ctx, oSet);
                    if (pSet == null)
                    {
                        return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
                    }
                    DbVariable pIdVar = null;
                    DbVariable pNameVar = null;
                    DbVariable pImageVar = null;
                    var q = from x in pSet.Variables orderby x.Name select x;
                    foreach (var v in q)
                    {
                        if (v.IsId)
                        {
                            pIdVar = v;
                        }
                        if (v.IsName)
                        {
                            pNameVar = v;
                        }
                        if (v.IsImageVar)
                        {
                            pImageVar = v;
                        }
                    }// v
                    bool bImageInt = false;
                    if (pImageVar != null)
                    {
                        String s = pImageVar.VarType.Trim().ToLower();
                        if ((s != "string") && (s != "bool"))
                        {
                            bImageInt = true;
                        }
                    }
                    List<VariableDesc> oRet = new List<VariableDesc>();
                    HashSet<int> oSetIndex = new HashSet<int>();
                    List<IndivDesc> listIndivs = new List<IndivDesc>();
                    Dictionary<int, ValueDescs> vals = new Dictionary<int, ValueDescs>();
                    foreach (var p in q)
                    {
                        VariableDesc pp = new VariableDesc();
                        convertVariable(p, pp);
                        var col = p.Values;
                        List<ValueDesc> pList = new List<ValueDesc>();
                        foreach (var v in col)
                        {
                            int index = v.Index;
                            if (index >= 0)
                            {
                                ValueDesc vv = new ValueDesc();
                                convertValue(v, vv);
                                pList.Add(vv);
                                if (!vals.ContainsKey(index))
                                {
                                    vals[index] = new ValueDescs();
                                }
                                (vals[index]).Add(vv);
                                if (!oSetIndex.Contains(index))
                                {
                                    oSetIndex.Add(index);
                                    var ind = new IndivDesc();
                                    listIndivs.Add(ind);
                                    ind.IndivIndex = index;
                                    if (pIdVar != null)
                                    {
                                        var qx = from x in pIdVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            ind.IdString = qx.First().Value;
                                        }
                                    }// Id
                                    if (pNameVar != null)
                                    {
                                        var qx = from x in pNameVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            ind.Name = qx.First().Value;
                                        }
                                    }// name
                                    if (pImageVar != null)
                                    {
                                        var qx = from x in pImageVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            var px = qx.First();
                                            String sx = StatHelpers.ConvertValue(px.Value);
                                            if (!String.IsNullOrEmpty(sx))
                                            {
                                                if (bImageInt)
                                                {
                                                    double dval = 0.0;
                                                    if (double.TryParse(sx, out dval))
                                                    {
                                                        int ival = (int)dval;
                                                        if (ival != 0)
                                                        {
                                                            var qz = from x in ctx.DbPhotoes where x.Id == ival select x;
                                                            if (qz.Count() > 0)
                                                            {
                                                                var pz = qz.First();
                                                                ind.PhotoId = pz.Id;
                                                                ind.PhotoData = pz.DataBytes;
                                                            }
                                                        }// ival
                                                    }
                                                }
                                                else
                                                {
                                                    var qz = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == sx.ToLower() select x;
                                                    if (qz.Count() > 0)
                                                    {
                                                        var pz = qz.First();
                                                        ind.PhotoId = pz.Id;
                                                        ind.PhotoData = pz.DataBytes;
                                                    }
                                                }// ival
                                            }// sx
                                        }
                                    }// photo

                                }// add value
                            }// v
                        }// v
                        var xx = getVariableInfo(ctx, pp);
                        if (xx.Item2 != null)
                        {
                            return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
                        }
                        pp.Info = xx.Item1;
                        pp.Values = new ValueDescs(pList);
                        oRet.Add(pp);
                    }// p
                    foreach (var ind in listIndivs)
                    {
                        int index = ind.IndivIndex;
                        if (vals.ContainsKey(index))
                        {
                            ind.Values = vals[index];
                        }
                    }// ind
                    if (listIndivs.Count > 1)
                    {
                        listIndivs.Sort();
                    }
                    if (oRet.Count > 1)
                    {
                        oRet.Sort();
                    }
                    indivs = new IndivDescs(listIndivs);
                    vars = new VariableDescs(oRet);
                }// ctx
            }// try
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
        }
 public PlotModel CreateCartesianPlot(String sTitle, IEnumerable<IndivData> inds,
     VariableDesc oVarX, VariableDesc oVarY,
     Dictionary<int, OxyImage> imagesDict = null,
     Dictionary<int, String> categDict = null,
     bool bPoints = true,
     bool bLabels = false,
     bool bImages = false,
     bool bZeroCrossing = false,
     bool bLeastSquares = false,
     List<Tuple<double,double,Cluster>> oCenters = null)
 {
     if ((inds == null) || (oVarX == null) || (oVarY == null))
     {
         return null;
     }
     PlotModel model = null;
     try
     {
         ValueDescs xvars = oVarX.Values;
         ValueDescs yvars = oVarY.Values;
         if ((xvars == null) || (yvars == null))
         {
             return null;
         }
         Dictionary<int, Tuple<double, double, IndivData>> vals = new Dictionary<int, Tuple<double, double, IndivData>>();
         List<double> xlist = new List<double>();
         List<double> ylist = new List<double>();
         List<DataPoint> pointsList = null;
         if (bLeastSquares)
         {
             pointsList = new List<DataPoint>();
         }
         foreach (var ind in inds)
         {
             if ((ind != null) && ind.IsValid)
             {
                 int index = ind.IndivIndex;
                 var qx = from x in xvars where x.Index == index select x;
                 var qy = from y in yvars where y.Index == index select y;
                 if ((qx.Count() > 0) && (qy.Count() > 0))
                 {
                     var vx = qx.First();
                     var vy = qy.First();
                     double xx = vx.DoubleValue;
                     double yy = vy.DoubleValue;
                     xlist.Add(xx);
                     ylist.Add(yy);
                     vals[index] = new Tuple<double, double, IndivData>(xx, yy, ind);
                     if (bLeastSquares)
                     {
                         pointsList.Add(new DataPoint(xx, yy));
                     }
                 }
             }// ind
         }// ind
         int nr = xlist.Count;
         if (nr < 1)
         {
             return null;
         }
         double xmin = xlist.Min();
         double xmax = xlist.Max();
         double ymin = ylist.Min();
         double ymax = ylist.Max();
         if ((xmin >= xmax) || (ymin >= ymax))
         {
             return null;
         }
         double xwidth = IMAGE_SCALE_FACTOR * (xmax - xmin) / nr;
         model = new PlotModel(sTitle);
         model.Subtitle = String.Format("{0} / {1}", oVarY.Name, oVarX.Name);
         model.LegendPlacement = LegendPlacement.Outside;
         if (bZeroCrossing)
         {
             model.Axes.Add(new LinearAxis(AxisPosition.Bottom, xmin, xmax, oVarX.Name) { PositionAtZeroCrossing = true });
             model.Axes.Add(new LinearAxis(AxisPosition.Left, ymin, ymax, oVarY.Name) { PositionAtZeroCrossing = true });
         }
         else
         {
             model.Axes.Add(new LinearAxis(AxisPosition.Bottom, xmin, xmax, oVarX.Name));
             model.Axes.Add(new LinearAxis(AxisPosition.Left, ymin, ymax, oVarY.Name));
         }
         Dictionary<String, ScatterSeries> oSeries = new Dictionary<string, ScatterSeries>();
         var indexes = vals.Keys;
         int nColor = 0;
         foreach (var index in indexes)
         {
             var t = vals[index];
             double x = t.Item1;
             double y = t.Item2;
             IndivData ind = t.Item3;
             if (bPoints)
             {
                 if ((categDict != null) && categDict.ContainsKey(index))
                 {
                     String scateg = categDict[index];
                     if (!String.IsNullOrEmpty(scateg))
                     {
                         if (!oSeries.ContainsKey(scateg))
                         {
                             OxyColor color = StatModelViewBase.GetColor(nColor++);
                             oSeries[scateg] = new ScatterSeries(scateg) { MarkerType = MarkerType.Circle,MarkerFill=color };
                         }
                         oSeries[scateg].Points.Add(new ScatterPoint(x, y) { Tag = ind });
                     }
                     else
                     {
                         if (!oSeries.ContainsKey(DEFAULT_SERIE_NAME))
                         {
                             OxyColor color = StatModelViewBase.GetColor(nColor++);
                             oSeries[DEFAULT_SERIE_NAME] = new ScatterSeries(DEFAULT_SERIE_NAME) { MarkerType = MarkerType.Circle,MarkerFill=color };
                         }
                         oSeries[DEFAULT_SERIE_NAME].Points.Add(new ScatterPoint(x, y) { Tag = ind });
                     }
                 }
                 else
                 {
                     if (!oSeries.ContainsKey(DEFAULT_SERIE_NAME))
                     {
                         OxyColor color = StatModelViewBase.GetColor(nColor++);
                         oSeries[DEFAULT_SERIE_NAME] = new ScatterSeries(DEFAULT_SERIE_NAME) { MarkerType = MarkerType.Circle,MarkerFill=color };
                     }
                     oSeries[DEFAULT_SERIE_NAME].Points.Add(new ScatterPoint(x, y) { Tag = ind });
                 }
             }// points
             if (bLabels)
             {
                 String name = ind.IdString;
                 if (String.IsNullOrEmpty(name))
                 {
                     name = ind.Name;
                 }
                 if (!String.IsNullOrEmpty(name))
                 {
                     model.Annotations.Add(new TextAnnotation
                     {
                         Position = new DataPoint(x, y),
                         Text = name,
                         FontWeight = OxyPlot.FontWeights.Bold,
                         HorizontalAlignment = OxyPlot.HorizontalAlignment.Center,
                         VerticalAlignment = OxyPlot.VerticalAlignment.Middle,
                         Tag = ind
                     });
                 }// write
             }// labels
             if (bImages && (imagesDict != null) && imagesDict.ContainsKey(index))
             {
                 OxyImage image = imagesDict[index];
                 var data = ind.PhotoData;
                 if (image != null)
                 {
                     model.Annotations.Add(new ImageAnnotation
                     {
                         ImageSource = image,
                         X = new PlotLength(x, PlotLengthUnit.Data),
                         Y = new PlotLength(y, PlotLengthUnit.Data),
                         HorizontalAlignment = OxyPlot.HorizontalAlignment.Center,
                         VerticalAlignment = OxyPlot.VerticalAlignment.Middle,
                         Width = new PlotLength(xwidth, PlotLengthUnit.Data)
                     });
                 }// image
             }// images
         }// index
         if (bPoints)
         {
             foreach (var s1 in oSeries.Values)
             {
                 model.Series.Add(s1);
             }// s1
         }// bPoints
         if ((oCenters != null) && (oCenters.Count > 0))
         {
             foreach (var t in oCenters)
             {
                 Cluster c = t.Item3;
                 if (c != null)
                 {
                     String name = c.Name;
                     if (String.IsNullOrEmpty(name))
                     {
                         name = String.Format("CL{0}", c.Index + 1);
                     }
                     double x = t.Item1;
                     double y = t.Item2;
                     model.Annotations.Add(new TextAnnotation
                     {
                         Position = new DataPoint(x, y),
                         Text = name,
                         FontWeight = OxyPlot.FontWeights.Bold,
                         HorizontalAlignment = OxyPlot.HorizontalAlignment.Center,
                         VerticalAlignment = OxyPlot.VerticalAlignment.Middle
                     });
                 }// c
             }// t
         }
         if (bLeastSquares)
         {
             double a, b;
             LeastSquaresFit(pointsList, out a, out b);
             model.Annotations.Add(new LineAnnotation { Slope = a, Intercept = b, Text = "Moindres Carrés" });
         }// leastsquares
     }// try
     catch (Exception /* ex */)
     {
         model = null;
     }
     return model;
 }
 public Tuple<IEnumerable<VariableDesc>, Exception> GetDataSetVariablesAndData(StatDataSet oSet)
 {
     List<VariableDesc> oRet = null;
     Exception err = null;
     if (oSet == null)
     {
         return new Tuple<IEnumerable<VariableDesc>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = findDataSet(ctx, oSet);
             if (pSet != null)
             {
                 var q = from x in pSet.Variables orderby x.Name ascending select x;
                 oRet = new List<VariableDesc>();
                 foreach (var p in q)
                 {
                     VariableDesc pp = new VariableDesc();
                     convertVariable(p, pp);
                     List<ValueDesc> oList = new List<ValueDesc>();
                     var col = p.Values;
                     foreach (var v in col)
                     {
                         if (v.Index >= 0)
                         {
                             ValueDesc vv = new ValueDesc();
                             convertValue(v, vv);
                             oList.Add(vv);
                         }// v
                     }// v
                     pp.Values = new ValueDescs(oList);
                     var xx = getVariableInfo(ctx, pp);
                     if (xx.Item2 != null)
                     {
                         return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err);
                     }
                     pp.Info = xx.Item1;
                     oRet.Add(pp);
                 }// p
             }// pSet
         }// ctx
         if ((oRet != null) && (oRet.Count > 1))
         {
             oRet.Sort();
         }
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err);
 }
 private async void buttonAdd_Click(object sender, RoutedEventArgs e)
 {
     var model = getModel();
     if (model == null)
     {
         return;
     }
     var oSet = model.CurrentStatDataSet;
     if (oSet == null)
     {
         return;
     }
     var pMan = model.DataService;
     if ((!oSet.IsValid) || (pMan == null))
     {
         return;
     }
     VariableDesc vv = new VariableDesc();
     vv.DataSetId = oSet.Id;
     vv.Name = "ArrangeIndex";
     vv.DataType = "short";
     vv.IsCategVar = true;
     vv.Description = "Matrix Arrange";
     this.buttonAdd.IsEnabled = false;
     this.buttonCompute.IsEnabled = false;
     model.IsBusy = true;
     var yy = await Task.Run<Tuple<bool, Exception>>(() =>
     {
         return pMan.RemoveVariable(vv);
     });
     if ((yy != null) && (yy.Item2 != null))
     {
         this.buttonAdd.IsEnabled = true;
         this.buttonCompute.IsEnabled = true;
         model.ShowError(yy.Item2);
         model.IsBusy = false;
         return;
     }
     var xx = await Task.Run<Tuple<VariableDesc, Exception>>(() =>
     {
         return pMan.MaintainsVariable(vv);
     });
     if ((xx != null) && (xx.Item1 != null) && (xx.Item2 == null))
     {
         VariableDesc oVar = xx.Item1;
         int nVarId = oVar.Id;
         List<ValueDesc> vals = new List<ValueDesc>();
         foreach (var ind in model.Individus)
         {
             int ival = ind.Position;
             int index = ind.IndivIndex;
             if ((ival >= 0) && (index >= 0))
             {
                 ValueDesc v = new ValueDesc();
                 v.VariableId = nVarId;
                 v.Index = index;
                 v.DataStringValue = Convert.ToString(ival);
                 v.IsModified = true;
                 vals.Add(v);
             }
         }// ind
         if (vals.Count > 0)
         {
             var zz = await Task.Run<Tuple<bool, Exception>>(() =>
             {
                 return pMan.WriteValues(vals);
             });
             if ((zz != null) && (zz.Item2 != null))
             {
                 model.ShowError(zz.Item2);
             }
         }// vals
     }
     else if ((xx != null) && (xx.Item2 != null))
     {
         model.ShowError(xx.Item2);
     }
     model.IsBusy = false;
     this.buttonAdd.IsEnabled = true;
     this.buttonCompute.IsEnabled = true;
     myUpdateUI();
 }
 public Tuple<VariableInfo, Exception> GetVariableInfo(VariableDesc oVar)
 {
     VariableInfo oRet = null;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             var xx = getVariableInfo(ctx, oVar);
             if (xx != null)
             {
                 oRet = xx.Item1;
                 err = xx.Item2;
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<VariableInfo, Exception>(oRet, err);
 }
 public Tuple<Dictionary<string, List<ValueDesc>>, Exception> ReadCategValues(VariableDesc oCategVar, VariableDesc oDataVar)
 {
     using (var ctx = getContext())
     {
         return readCategValues(ctx, oCategVar, oDataVar);
     }
 }
 public Tuple<int, Exception> GetVariableValuesCount(VariableDesc oVar)
 {
     int nRet = 0;
     Exception err = null;
     if (oVar == null)
     {
         return new Tuple<int, Exception>(0, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbVariable pVar = findVariable(ctx, oVar);
             if (pVar != null)
             {
                 nRet = pVar.Values.Count();
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<int, Exception>(nRet, err);
 }
 }// refreshIndivs
 private void refreshVariables()
 {
     m_idvar = null;
     m_namevar = null;
     m_photovar = null;
     var col = m_main.Variables;
     foreach (var v in col)
     {
         if (v.IsIdVar)
         {
             m_idvar = v;
             this.IdsVarName = v.Name;
         }
         if (v.IsNameVar)
         {
             m_namevar = v;
             this.NamesVarName = v.Name;
         }
         if (v.IsImageVar)
         {
             m_photovar = v;
             this.PhotosVarName = v.Name;
         }
     }// v
     if (m_idsvarname == m_namesvarname)
     {
         m_idsvarname = null;
         this.IdsVarName = null;
     }
 }