Example #1
0
 public Cluster(IndivData p, ClassificationType t)
 {
     switch (t)
     {
         case ClassificationType.Hierar:
             AddHierar(p);
             break;
         case ClassificationType.KMeans:
             AddKMeans(p);
             break;
         case ClassificationType.Utility:
             AddUtility(p);
             break;
         default:
             break;
     }
 }
Example #2
0
 public bool RemoveUtility(IndivData p)
 {
     if (p == null)
     {
         return false;
     }
     if (!p.IsValid)
     {
         return false;
     }
     var q = from x in this.Elements where x.IndivIndex == p.IndivIndex select x;
     if (q.Count() < 1)
     {
         return false;
     }
     var xx = q.First();
     xx.UtilityClusterIndex = -1;
     this.Elements.Remove(xx);
     this.NotifyPropertyChanged("Elements");
     NotifyPropertyChanged("Count");
     NotifyPropertyChanged("IsEmpty");
     return true;
 }
Example #3
0
 public double GetDistance(IndivData pIndivData)
 {
     double dRet = 0.0;
     if (pIndivData == null)
     {
         return dRet;
     }
     double[] dd1 = this.Center;
     double[] dd2 = pIndivData.DoubleData;
     if ((dd1 == null) || (dd2 == null))
     {
         return dRet;
     }
     int n = (dd1.Length < dd2.Length) ? dd1.Length : dd2.Length;
     for (int i = 0; i < n; ++i)
     {
         double x = dd1[i] - dd2[i];
         dRet += x * x;
     }// i
     return dRet;
 }
 public IEnumerable<IndivData> GetIndivsData(IEnumerable<IndivDesc> inds, IEnumerable<VariableDesc> oVars, MatriceComputeMode mode,
     int nClasses)
 {
     List<IndivData> oRet = null;
     try
     {
         var vars = oVars.ToArray();
         int nv = vars.Length;
         if (nv < 1)
         {
             return null;
         }
         var vals = GetCommonValues(oVars);
         if (vals == null)
         {
             return null;
         }
         var dictDouble = GetDoubleData(vals, mode);
         var dictInt = GetIntData(vals, mode, nClasses);
         if ((dictDouble == null) || (dictInt == null))
         {
             return null;
         }
         oRet = new List<IndivData>();
         foreach (var ind in inds)
         {
             int index = ind.IndivIndex;
             if (index < 0)
             {
                 continue;
             }
             double[] dd = new double[nv];
             int[] di = new int[nv];
             for (int j = 0; j < nv; ++j)
             {
                 var vv = vars[j];
                 foreach (var x in dictDouble.Keys)
                 {
                     if (x.Id == vv.Id)
                     {
                         var dict = dictDouble[x];
                         if (dict.ContainsKey(index))
                         {
                             dd[j] = dict[index];
                         }
                         break;
                     }//
                 }// double
                 foreach (var x in dictInt.Keys)
                 {
                     if (x.Id == vv.Id)
                     {
                         var dict = dictInt[x];
                         if (dict.ContainsKey(index))
                         {
                             di[j] = dict[index];
                         }
                         break;
                     }//
                 }// double
             }// j
             var vz = new IndivData(ind, dd, di);
             oRet.Add(vz);
         }// inds
     }// try
     catch (Exception /*ex */)
     {
         oRet = null;
     }
     return oRet;
 }
Example #5
0
 public bool AddUtility(IndivData p)
 {
     if (p == null)
     {
         return false;
     }
     if (!p.IsValid)
     {
         return false;
     }
     var q = from x in this.Elements where x.IndivIndex == p.IndivIndex select x;
     if (q.Count() > 0)
     {
         return true;
     }
     p.UtilityClusterIndex = this.Index;
     this.Elements.Add(p);
     if (this.Elements.Count > 1)
     {
         this.Elements.Sort();
     }
     this.NotifyPropertyChanged("Elements");
     NotifyPropertyChanged("Count");
     NotifyPropertyChanged("IsEmpty");
     return true;
 }
Example #6
0
 private double getDistance(IndivData ind1, IndivData ind2)
 {
     double dRet = 0.0;
     if ((m_dist != null) && (ind1 != null) && (ind2 != null))
     {
         int iIndex = ind1.IndivIndex;
         int jIndex = ind2.IndivIndex;
         if (iIndex != jIndex)
         {
             dRet = (m_dist[iIndex])[jIndex];
         }
     }// ok
     return dRet;
 }
Example #7
0
 public bool Add(IndivData ind)
 {
     if (ind == null)
     {
         return false;
     }
     if (!ind.IsValid)
     {
         return false;
     }
     var q = from x in this.Indivs where x.IndivIndex == ind.IndivIndex select x;
     if (q.Count() > 0)
     {
         return false;
     }
     this.Indivs.Add(ind);
     NotifyPropertyChanged("Indivs");
     NotifyPropertyChanged("IsValid");
     NotifyPropertyChanged("Criteria");
     return true;
 }
Example #8
0
 public ArrangeSet(IEnumerable<IndivData> inds, IndivData indStart)
 {
     m_list = new List<IndivData>();
     m_left = new List<IndivData>();
     m_crit = -1.0;
     if (inds != null)
     {
         m_left.AddRange(inds);
         if (indStart != null)
         {
             var q = from x in m_left where x.IndivIndex == indStart.IndivIndex select x;
             if (q.Count() > 0)
             {
                 var p = q.First();
                 m_left.Remove(p);
                 m_list.Add(p);
             }
         }// indStart
         computeDistances(inds);
     }// inds
 }
Example #9
0
 private static List<IndivData> transpose(IEnumerable<IndivData> oInds)
 {
     List<IndivData> oRet = null;
     try
     {
         var src = oInds.ToArray();
         int nr = src.Length;
         if (nr > 0)
         {
             int nv = (src[0]).DoubleData.Length;
             if (nv > 0)
             {
                 double[,] data = new double[nv, nr];
                 for (int i = 0; i < nr; ++i)
                 {
                     double[] dd = (src[i]).DoubleData;
                     for (int j = 0; j < nv; ++j)
                     {
                         data[j, i] = dd[j];
                     }// j
                 }// i
                 oRet = new List<IndivData>();
                 for (int i = 0; i < nv; ++i)
                 {
                     IndivData ind = new IndivData();
                     ind.Individu.IndivIndex = i;
                     double[] dd = new double[nr];
                     for (int j = 0; j < nr; ++j)
                     {
                         dd[j] = data[i, j];
                     }// j
                     ind.DoubleData = dd;
                     oRet.Add(ind);
                 }// i
             }// nv
         }// nre
     }
     catch (Exception /* ex */)
     {
         oRet = null;
     }
     return oRet;
 }
 }// RemoveNumgVariables
 #endregion
 #region Plot Helpers
 public Tuple<PlotModel, CorrelData> createCorrelationPlotModel()
 {
     PlotModel model = null;
     CorrelData odata = null;
     VariableDesc oVarY = this.CurrentYVariable;
     VariableDesc oVarX = this.CurrentXVariable;
     var allIndivs = this.AllIndividus;
     Dictionary<int, String> categDict = new Dictionary<int, string>();
     List<Tuple<double, double, Cluster>> oList = null;
     var oCateg = this.CurrentCategVariable;
     if ((oCateg != null) && oCateg.IsValid)
     {
         var vals = oCateg.Values;
         foreach (var ind in allIndivs)
         {
             int index = ind.IndivIndex;
             var q = from x in vals where x.Index == index select x;
             if (q.Count() > 0)
             {
                 String key = StatHelpers.ConvertValue(q.First().DataStringValue);
                 if (!String.IsNullOrEmpty(key))
                 {
                     categDict[index] = key;
                 }
             }// q
         }// ind
     }
     else
     {
         String sval = DEFAULT_SERIE_NAME;
         foreach (var ind in allIndivs)
         {
             int index = ind.IndivIndex;
             categDict[index] = sval;
         }// ind
     }
     List<IndivData> xList = new List<IndivData>();
     foreach (var ind in allIndivs)
     {
         IndivData vv = new IndivData(ind);
         xList.Add(vv);
     }// ind
     bool bPoints = this.HasPoints;
     bool bLabels = this.HasLabels;
     bool bImages = this.HasImages;
     bool bZeroCrossing = false;
     bool bLeastSquares = true;
     var imagesDict = this.ImagesDictionary;
     String title = String.Format("{0} / {1}", oVarY.Name, oVarX.Name);
     model = CreateCartesianPlot(title, xList, oVarX, oVarY, imagesDict, categDict, bPoints, bLabels, bImages, bZeroCrossing,
         bLeastSquares, oList);
     List<double> xx = new List<double>();
     List<double> yy = new List<double>();
     var valsx = oVarX.Values;
     var valsy = oVarY.Values;
     foreach (var vx in valsx)
     {
         int index = vx.Index;
         var q = from x in valsy where x.Index == index select x;
         if (q.Count() > 0)
         {
             double ty = q.First().DoubleValue;
             double tx = vx.DoubleValue;
             xx.Add(tx);
             yy.Add(ty);
         }
     }// varx
     odata = ComputeCorrelation(xx.ToArray(), yy.ToArray());
     if (odata != null)
     {
         odata.FirstName = oVarY.Name;
         odata.SecondName = oVarX.Name;
     }// odata
     return new Tuple<PlotModel, CorrelData>(model, odata);
 }//createCorrelationPlotModel
 private bool processOne(IndivData oIndiv, CancellationToken cancellationToken)
 {
     int nBestIndex = -1;
     double bestCrit = 0.0;
     foreach (var cluster in this.Clusters)
     {
         if (cancellationToken.IsCancellationRequested)
         {
             return false;
         }
         int index = cluster.Index;
         cluster.AddUtility(oIndiv);
         double c = compute_current_crit(cancellationToken);
         cluster.RemoveUtility(oIndiv);
         if (nBestIndex < 0)
         {
             if (c != 0.0)
             {
                 nBestIndex = index;
                 bestCrit = c;
             }
         }
         else if (c > bestCrit)
         {
             nBestIndex = index;
             bestCrit = c;
         }
     }// cluster
     if ((nBestIndex < 0) || (bestCrit == 0.0))
     {
         return false;
     }
     var qq = from x in this.Clusters where x.Index == nBestIndex select x;
     if (qq.Count() > 0)
     {
         var p = qq.First();
         p.AddUtility(oIndiv);
         this.CurrentCrit = bestCrit;
         return true;
     }
     return false;
 }
 private int findNearestClusterIndex(IndivData ind, out double dMin)
 {
     int nRet = -1;
     dMin = 0.0;
     if (ind == null)
     {
         return nRet;
     }
     foreach (var cluster in this.Clusters)
     {
         double d = cluster.GetDistance(ind);
         if (nRet < 0)
         {
             nRet = cluster.Index;
             dMin = d;
         }
         else if (d < dMin)
         {
             nRet = cluster.Index;
             dMin = d;
         }
     }// cluster
     return nRet;
 }
 private void modifyIndiv(IndivData ind,bool bImage)
 {
     int index = ind.IndivIndex;
     foreach (var dd in this.DisplayIndivs)
     {
         var obj = dd.Tag;
         if ((obj != null) && (obj is IndivData))
         {
             IndivData dx = obj as IndivData;
             if (dx.IndivIndex == index)
             {
                 var col = dd.ToArray();
                 col[1].StringValue = ind.IdString;
                 col[2].StringValue = ind.Name;
                 if (bImage)
                 {
                     if ((ind.PhotoData != null) && (ind.PhotoData.Length > 1))
                     {
                         col[3].DisplayType = DisplayItemType.eDisplayImage;
                         col[3].DataBytes = ind.PhotoData;
                     }
                     else
                     {
                         col[3].DisplayType = DisplayItemType.eDisplayDefault;
                     }
                 }// bImage
                 break;
             }// found
         }
     }// dd
 }// ModifyIndex
 }// RefreshPhotos
 public void refreshIndivs()
 {
     if (m_busy)
     {
         return;
     }
     m_busy = true;
     this.refreshVariables();
     var oRet = new List<IndivData>();
     var col = m_main.AllIndividus;
     DisplayItemsArray oDisp = new DisplayItemsArray();
     foreach (var ind in col)
     {
         int index = ind.IndivIndex;
         var vv = new IndivData(ind);
         oRet.Add(vv);
         DisplayItems dd = new DisplayItems();
         dd.Tag = vv;
         dd.Add(new DisplayItem(vv.IndivIndex));
         String sz = vv.IdString;
         dd.Add(new DisplayItem(sz));
         dd.Add(new DisplayItem(vv.Name));
         if ((vv.PhotoData != null) && (vv.PhotoData.Length > 1))
         {
             dd.Add(new DisplayItem(vv.PhotoData));
         }
         else
         {
             dd.Add(new DisplayItem());
         }
         oDisp.Add(dd);
     }// ind
     if (oRet.Count > 1)
     {
         oRet.Sort();
     }
     this.Individus = new IndivDatas(oRet);
     this.DisplayIndivs = oDisp;
     this.IsModified = false;
     m_busy = false;
     NotifyPropertyChanged("WorkDone");
 }// refreshIndivs