Exemplo n.º 1
0
        static float[][] Normalize(float[][] matrix)
        {
            int rows    = matrix.Length;
            int columns = matrix[0].Length;

            MT.ForEach(matrix, R => {
                float av   = R.Average();
                double sum = 0.0;
                for (int col = 0; col < columns; col++)
                {
                    R[col] -= av;
                    sum    += R[col] * R[col];
                }
                if (sum <= 0)
                {
                    float c = (float)(1.0 / Math.Sqrt(columns));
                    for (int col = 0; col < columns; col++)
                    {
                        R[col] = c;
                    }
                }
                else
                {
                    double norm = Math.Sqrt(sum);
                    for (int col = 0; col < columns; col++)
                    {
                        R[col] = (float)(R[col] / norm);
                    }
                }
            });
            return(matrix);
        }
Exemplo n.º 2
0
        private void App_ItemsSelected(object sender, Lib.ItemListEventArgs e)
        {
            List <string> idList = e.Items as List <string>;

            if ((featureMap == null) || (idList == null) || (idList.Count == 0))
            {
                return;
            }

            // Calculate the squared mean of columns into m[].
            int columns = NumTable.Columns;
            var rowList = NumTable.IndexOfRows(idList);

            double[] m = new double[columns];
            MT.ForEach(rowList, row => {
                var R = NumTable.Matrix[row];
                for (int col = 0; col < columns; col++)
                {
                    double v = R[col];
                    m[col]  += v * v;
                }
            });
            for (int col = 0; col < columns; col++)
            {
                m[col] = Math.Sqrt(m[col] / rowList.Count);
            }

            for (int i = 0; i < m.Length; i++)
            {
                short t = (short)((m[i] - minExpression) / stepExpression);
                OrgBodies[i].Type = Math.Max((short)0, Math.Min((short)15, t));
            }
            featureMap.RedrawBodiesType();
        }
Exemplo n.º 3
0
        void PropagateByNeighborsSimple(Neighbor[][] nbList, float[][] a)
        {
            float avAff = 0.0f;

            MT.ForEach(nbList, nbs => {
                float s = nbs.Sum(nb => nb.distance * nb.distance);
                lock (this)
                    avAff += s;
            });
            avAff = (float)Math.Sqrt(avAff / (nbList.Length * nbList[0].Length));

            int rows    = a.Length;
            int columns = a[0].Length;

            MT.Loop(0, rows, row => {
                float[] newAff = new float[columns];
                float[] aR     = a[row];
                for (int col = 0; col < columns; col++)
                {
                    float aff = aR[col];
                    foreach (var nb in nbList[col])
                    {
                        if (nb.distance >= avAff)
                        {
                            break;
                        }
                        aff += aR[nb.index];
                    }
                    newAff[col] = aff;
                }
                a[row] = newAff;
            });
        }
Exemplo n.º 4
0
        public static Tuple <float, float, float> MinMaxMean(float[][] matrix)
        {
            float minValue  = float.MaxValue;
            float maxValue  = float.MinValue;
            float meanValue = 0;
            int   cnt       = 0;

            MT.ForEach(matrix, R => {
                float minV  = float.MaxValue;
                float maxV  = float.MinValue;
                float meanV = 0;
                for (int j = 0; j < R.Length; j++)
                {
                    float v = R[j];
                    minV    = Math.Min(minV, v);
                    maxV    = Math.Max(maxV, v);
                    meanV  += v;
                }
                lock (matrix) {
                    minValue   = Math.Min(minValue, minV);
                    maxValue   = Math.Max(maxValue, maxV);
                    meanValue += meanV;
                    cnt       += R.Length;
                }
            });
            meanValue /= cnt;
            return(Tuple.Create(minValue, maxValue, meanValue));
        }
Exemplo n.º 5
0
 public static void ShiftMatrix(float[][] matrix, float delta)
 {
     if (delta == 0.0f)
     {
         return;
     }
     MT.ForEach(matrix, R => {
         for (int j = 0; j < R.Length; j++)
         {
             R[j] = Math.Max(0, R[j] + delta);
         }
     });
 }
Exemplo n.º 6
0
 public static void ScaleMatrix(float[][] matrix, double fct)
 {
     if (fct == 1.0)
     {
         return;
     }
     MT.ForEach(matrix, R => {
         for (int j = 0; j < R.Length; j++)
         {
             R[j] = (float)(R[j] * fct);
         }
     });
 }
Exemplo n.º 7
0
        double ExpThreshold()
        {
            double maxV = double.MinValue;

            MT.ForEach(NumTable.Matrix, R => {
                double mxV = double.MinValue;
                for (int col = 0; col < NumTable.Columns; col++)
                {
                    mxV = Math.Max(R[col], mxV);
                }
                lock (this)
                    maxV = Math.Max(maxV, mxV);
            });
            return((maxV < 100) ? maxV / 2.0 : Math.Sqrt(maxV));
        }
Exemplo n.º 8
0
        public static float Average2(float[][] matrix)
        {
            float sum = 0.0f;

            MT.ForEach(matrix, R => {
                float s = 0;
                for (int i = 0; i < R.Length; i++)
                {
                    s += R[i] * R[i];
                }
                lock (SingleCellPlugin.App)
                    sum += s;
            });
            return((float)Math.Sqrt(sum / matrix.Sum(R => (double)R.Length)));
        }
Exemplo n.º 9
0
        public static float AverageSqrt(float[][] matrix)
        {
            double sum = 0.0f;

            MT.ForEach(matrix, R => {
                double s = 0;
                for (int i = 0; i < R.Length; i++)
                {
                    s += Math.Sqrt(Math.Abs(R[i]));
                }
                lock (SingleCellPlugin.App)
                    sum += s;
            });
            double mean = sum / matrix.Sum(R => (double)R.Length);

            return((float)(mean * mean));
        }
Exemplo n.º 10
0
        void PreCalculate() {
            if (dataset == null)
                return;

            // Extract the relevant data table.
            var bs = dataset.BodyList;
            INumberTable nt = dataset.GetNumberTableView();
            toIdx = Enumerable.Range(0, bs.Count).Where(i => !bs[i].Disabled).ToArray();    // Indexes of enabled bodies.
            int N = nt.Rows - pcaSamples;
            int[] enabledRows = toIdx.Where(i => i < N).ToArray();

            if (enabledRows.Length == 0) 
                throw new TException("No data available!");
            P = new float[enabledRows.Length][];
            MT.Loop(0, P.Length, row => {
                float[] R = P[row] = new float[nt.Columns];
                double[] dsR = nt.Matrix[enabledRows[row]] as double[];
                for (int col = 0; col < nt.Columns; col++)
                    R[col] = (float)dsR[col];
            });

            // Reverse toIdx;
            int[] rIdx = Enumerable.Repeat(-1, bs.Count).ToArray();
            for (int i = 0; i < toIdx.Length; i++) rIdx[toIdx[i]] = i;
            toIdx = rIdx;

            using (var gpu = new VisuMap.DxShader.GpuDevice())
                dtP = DualAffinity.DotProduct(gpu, P, false);

            float[] singValues = new float[pcaMax];
            float[][] PC = FastPca.DoFastReduction(P, pcaMax, singValues, true); 
            P = VisuMap.MathUtil.NewMatrix<float>(PC.Length, pcaSamples);  // P now links data points with the injected points on the main PCA axis.
            float span = 4.0f * singValues[0];
            stepSize = span / (pcaSamples - 1);
            float x0 = - 0.5f * span;
            MT.ForEach(PC, (R, row) => {
                double yy = R.Skip(1).Sum(v => v * v);
                for (int col = 0; col < pcaSamples; col++) {
                    double x = R[0] - (x0 + col * stepSize);
                    P[row][col] = (float)Math.Sqrt(x * x + yy);
                }
            });
        }
Exemplo n.º 11
0
        void PropagateByNeighbors(Neighbor[][] nbList, float[][] a)
        {
            int kNN     = nbList[0].Length;
            int rows    = a.Length;
            int columns = a[0].Length;

            // Calculate the average distance to kNN neighbors.
            float avAff = 0.0f;

            MT.ForEach(nbList, nbs => {
                float s = nbs.Sum(nb => nb.distance);
                lock (this)
                    avAff += s;
            });
            avAff /= nbList.Length * kNN;

            // Prepare the exponentially descreasing coefficent to propgate neighboring affinity.
            MT.ForEach(nbList, nbs => {
                for (int k = 0; k < kNN; k++)
                {
                    double e        = nbs[k].distance / avAff;
                    nbs[k].distance = (float)Math.Exp(-0.5 * e * e);
                }
            });

            // Propagate affinity from neibhoring columns.
            MT.Loop(0, rows, row => {
                float[] newAff = new float[columns];
                float[] aR     = a[row];
                for (int col = 0; col < columns; col++)
                {
                    var nbs   = nbList[col];
                    float aff = aR[col];
                    foreach (var nb in nbs)
                    {
                        aff += aR[nb.index] * nb.distance;
                    }
                    newAff[col] = aff;
                }
                a[row] = newAff;
            });
        }
Exemplo n.º 12
0
        public void SetExpressionTable(INumberTable numTable, IForm featureView)
        {
            if ((featureView is IMapSnapshot) || (featureView is IMdsCluster))
            {
                featureMap = featureView;
            }
            else
            {
                MsgBox.Alert("Only parnet window is not a map-snapshot or mds-cluster view.");
                return;
            }

            NumTable            = numTable;
            OrgBodies           = featureMap.BodyList;
            featureMap.GlyphSet = "Ordered Glyphs";
            featureMap.Redraw();

            minExpression = double.MaxValue;
            double maxExpression = double.MinValue;

            MT.ForEach(NumTable.Matrix, R => {
                double minV = double.MaxValue;
                double maxV = double.MinValue;
                foreach (var v in R)
                {
                    minV = Math.Min(minV, v);
                    maxV = Math.Max(maxV, v);
                }
                lock (this) {
                    minExpression = Math.Min(minExpression, minV);
                    maxExpression = Math.Max(maxExpression, maxV);
                }
            });
            stepExpression = (maxExpression - minExpression) / 16;

            SingleCellPlugin.App.ItemsSelected += App_ItemsSelected;
            featureView.TheForm.FormClosing    += (s, e) => {
                SingleCellPlugin.App.ItemsSelected -= App_ItemsSelected;
            };
        }
Exemplo n.º 13
0
 public static void PowerMatrix(float[][] matrix, double exp)
 {
     if (exp == 1.0)
     {
         return;
     }
     if (exp == 0.0)
     {
         foreach (var R in matrix)
         {
             for (int j = 0; j < R.Length; j++)
             {
                 R[j] = 1.0f;
             }
         }
         return;
     }
     MT.ForEach(matrix, R => {
         for (int j = 0; j < R.Length; j++)
         {
             R[j] = (float)Math.Pow(R[j], exp);
         }
     });
 }
Exemplo n.º 14
0
        static float[][] CallShadere(DxShader.GpuDevice gpu, CBuf cc, ComputeShader sd, float[][] M, bool isCorrelation)
        {
            cc.c.N = M.Length;
            int       columns   = M[0].Length;
            const int groupSize = 256;
            int       distSize  = groupSize * cc.c.N;

            float[][] dMatrix = new float[M.Length][];
            for (int row = 0; row < M.Length; row++)
            {
                dMatrix[row] = new float[row];
            }
            int maxColumns = MaxGpuFloats / cc.c.N;
            int secSize    = Math.Min(columns, (maxColumns > 4096) ? 4096 : (maxColumns - 32));

            float[] uploadBuf = new float[cc.c.N * secSize];

            using (var dataBuf = gpu.CreateBufferRO(cc.c.N * secSize, 4, 0))
                using (var distBuf = gpu.CreateBufferRW(distSize, 4, 0))
                    using (var distStaging = gpu.CreateStagingBuffer(distBuf)) {
                        gpu.SetShader(sd);
                        for (int s0 = 0; s0 < columns; s0 += secSize)
                        {
                            int s1 = Math.Min(s0 + secSize, columns);
                            WriteMarix(gpu, dataBuf, M, s0, s1, uploadBuf);
                            float[] blockDist = new float[distSize];
                            for (cc.c.iBlock = 1; cc.c.iBlock < cc.c.N; cc.c.iBlock += groupSize)
                            {
                                cc.c.columns = s1 - s0;
                                cc.Upload();
                                gpu.Run(groupSize);
                                int iBlock2 = Math.Min(cc.c.iBlock + groupSize, cc.c.N);
                                int bSize   = (iBlock2 - cc.c.iBlock) * (iBlock2 + cc.c.iBlock - 1) / 2;
                                gpu.ReadRange <float>(blockDist, 0, distStaging, distBuf, bSize);

                                int offset = 0;
                                for (int row = cc.c.iBlock; row < iBlock2; row++)
                                {
                                    float[] R = dMatrix[row];
                                    for (int k = 0; k < row; k++)
                                    {
                                        R[k] += blockDist[offset + k];
                                    }
                                    offset += row;
                                }
                                Application.DoEvents();
                            }
                        }
                    }

            if (isCorrelation)
            {
                MT.ForEach(dMatrix, R => {
                    for (int col = 0; col < R.Length; col++)
                    {
                        R[col] = 1.0f - R[col];
                    }
                });
            }
            else     // Euclidean distance is wanted.
            {
                float[] norm2 = new float[M.Length];
                Array.Clear(norm2, 0, M.Length);
                int L = M[0].Length;
                MT.ForEach(M, (R, row) => {
                    float sumSquared = 0.0f;
                    for (int col = 0; col < L; col++)
                    {
                        sumSquared += R[col] * R[col];
                    }
                    norm2[row] = sumSquared;
                });
                MT.Loop(1, M.Length, row => {
                    float[] R = dMatrix[row];
                    for (int col = 0; col < row; col++)
                    {
                        R[col] = (float)Math.Sqrt(Math.Abs(norm2[row] + norm2[col] - 2 * R[col]));
                    }
                });
            }
            return(dMatrix);
        }