Пример #1
0
        static List <LabeledTomogram> LabeledTomogramsFromPaintedFiles()
        {
            List <LabeledTomogram> ret = new List <LabeledTomogram>();

            LabeledTomogram tom = DataReader.ReadMRCFile("/home/brush/tomography2_fullsirtcliptrim.mrc", 145);

            tom.Labels = new float[tom.Width * tom.Height];

            int positiveLabels = 0;

            using (Bitmap bmp = (Bitmap)Image.FromFile("145_painted.png"))
            {
                for (int y = 0, i = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++, i++)
                    {
                        Color c = bmp.GetPixel(x, y);

                        if (c.R != c.G)
                        {
                            tom.Labels[i] = 1;
                            positiveLabels++;
                        }
                    }
                }
            }
            ret.Add(tom);

            Console.WriteLine($"Found {positiveLabels} positive pixels.");

            return(ret);
        }
Пример #2
0
        public static Bitmap LabeledTomogram2Bitmap(LabeledTomogram tomogram)
        {
            float minFloat = tomogram.Data.Min();
            float maxfloat = tomogram.Data.Max();

            float delta  = maxfloat - minFloat;
            float scaler = 255 / delta;

            float[] labels = tomogram.Labels;

            Bitmap bmp = new Bitmap(tomogram.Width, tomogram.Height);

            for (int y = 0, i = 0; y < tomogram.Height; y++)
            {
                for (int x = 0; x < tomogram.Width; x++, i++)
                {
                    if (labels != null && labels[i] > 0)
                    {
                        bmp.SetPixel(x, y, System.Drawing.Color.Red);
                    }
                    else
                    {
                        byte b = (byte)((tomogram.Data[i] + Math.Abs(minFloat)) * scaler);

                        bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(b, b, b));
                    }
                }
            }

            return(bmp);
        }
Пример #3
0
        public static LabeledTomogram ReadTomogramPair(string dataFile, string labelFile, int width, int height)
        {
            byte[] dataFileBytes  = File.ReadAllBytes(dataFile);
            byte[] labelFileBytes = null;

            if (!string.IsNullOrEmpty(labelFile))
            {
                labelFileBytes = File.ReadAllBytes(labelFile);
            }

            if (width * height != dataFileBytes.Length / sizeof(float) &&
                width * height != labelFileBytes.Length / sizeof(float))
            {
                throw new ArgumentOutOfRangeException("Width or height specify invalid dimensions for this data file.");
            }

            LabeledTomogram ret = new LabeledTomogram();

            ret.Data = new float[dataFileBytes.Length / sizeof(float)];
            Buffer.BlockCopy(dataFileBytes, 0, ret.Data, 0, dataFileBytes.Length);

            if (labelFileBytes != null)
            {
                ret.Labels = new float[labelFileBytes.Length / sizeof(float)];
                Buffer.BlockCopy(labelFileBytes, 0, ret.Labels, 0, labelFileBytes.Length);
            }

            ret.Width  = width;
            ret.Height = height;

            return(ret);
        }
Пример #4
0
        public static LabeledTomogram ReadDatFile(string datFile)
        {
            using (FileStream fin = File.OpenRead(datFile))
            {
                BinaryFormatter bf  = new BinaryFormatter();
                Tomogram        tom = (Tomogram)bf.Deserialize(fin);

                LabeledTomogram l = new LabeledTomogram();
                l.Data   = tom.Data;
                l.Width  = tom.Width;
                l.Height = tom.Height;
                l.Labels = new float[l.Data.Length];

                int m = 0;
                for (int c = 0; c < l.Labels.Length; c++)
                {
                    if (tom.DataClasses[c] == -1)
                    {
                        l.Labels[c] = 1;
                        m++;
                    }
                    else
                    {
                        l.Labels[c] = 0;
                    }
                }

                Console.WriteLine(m.ToString());

                return(l);
            }
        }
Пример #5
0
 private static LabeledTomogram ShallowCopy(LabeledTomogram tom)
 {
     return(new LabeledTomogram
     {
         Width = tom.Width,
         Height = tom.Height,
         Data = new float[tom.Data.Length],
         Labels = new float[tom.Labels.Length],
     });
 }
Пример #6
0
        public static LabeledTomogram ReadMRCFile(string mrcFilePath, int frameNumber)
        {
            MRCFile  file  = MRCParser.Parse(mrcFilePath);
            MRCFrame frame = file.Frames[frameNumber];

            LabeledTomogram ret = new LabeledTomogram();

            ret.Labels = null;
            ret.Data   = frame.Data;
            ret.Width  = frame.Width;
            ret.Height = frame.Height;

            return(ret);
        }
Пример #7
0
        public static float[] Predict(LabeledTomogram image, DecisionTreeNode node, DecisionTreeOptions options)
        {
            List <LabeledPoint> points = TomogramsToPoints(
                new List <LabeledTomogram>(new LabeledTomogram[] { image }), null, null);

            List <float> labels = new List <float>();

            foreach (LabeledPoint point in points)
            {
                labels.Add(RecurseAndPredict(point, node, options));
            }

            return(labels.ToArray());
        }
Пример #8
0
        public static LabeledTomogram FlipTomogram(LabeledTomogram input)
        {
            LabeledTomogram ret = ShallowCopy(input);

            for (int y = 0; y < input.Height; y++)
            {
                for (int x = 0; x < input.Width; x++)
                {
                    int sourceIndex      = y * input.Width + x;
                    int destinationIndex = y * input.Width + (input.Width - 1 - x);

                    ret.Data[destinationIndex]   = input.Data[sourceIndex];
                    ret.Labels[destinationIndex] = input.Labels[sourceIndex];
                }
            }

            return(ret);
        }
Пример #9
0
        public static List <LabeledTomogram> FlipAndRotateTomogram(LabeledTomogram tomogram)
        {
            List <LabeledTomogram> ret = new List <LabeledTomogram>();

            ret.Add(tomogram);

            ret.Add(RotateTomogram(tomogram, 90));
            ret.Add(RotateTomogram(tomogram, 180));
            ret.Add(RotateTomogram(tomogram, 270));

            LabeledTomogram flippedTomogram = FlipTomogram(tomogram);

            ret.Add(flippedTomogram);

            ret.Add(RotateTomogram(flippedTomogram, 90));
            ret.Add(RotateTomogram(flippedTomogram, 180));
            ret.Add(RotateTomogram(flippedTomogram, 270));

            return(ret);
        }
Пример #10
0
        public static LabeledTomogram Erode(LabeledTomogram tomogram, int elementRadius)
        {
            float[] labelsCopy = new float[tomogram.Labels.Length];
            for (int y = 0, i = 0; y < tomogram.Height; y++)
            {
                for (int x = 0; x < tomogram.Width; x++, i++)
                {
                    float label = tomogram.Labels[i];

                    if (label > 0)
                    {
                        if (y > elementRadius - 1 && y < tomogram.Height - elementRadius &&
                            x > elementRadius - 1 && x < tomogram.Width - elementRadius)
                        {
                            bool keep = true;
                            for (int y1 = y - elementRadius; y1 < y + elementRadius && keep; y1++)
                            {
                                for (int x1 = x - elementRadius; x1 < x + elementRadius && keep; x1++)
                                {
                                    int index = y1 * tomogram.Width + x1;

                                    if (tomogram.Labels[index] != label)
                                    {
                                        keep = false;
                                    }
                                }
                            }

                            if (keep)
                            {
                                labelsCopy[i] = label;
                            }
                        }
                    }
                }
            }

            tomogram.Labels = labelsCopy;

            return(tomogram);
        }
Пример #11
0
        public static Bitmap PaintClassifiedPixelsOnTomogram(LabeledTomogram tom, float[] labels)
        {
            tom.Labels = new float[tom.Width * tom.Height];
            Bitmap bmp = LabeledTomogram2Bitmap(tom);

            //using (Bitmap bmp = Tomogram2Bitmap(tom))
            {
                for (int y = 0, i = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++, i++)
                    {
                        if (labels[i] > 0)
                        {
                            bmp.SetPixel(x, y, Color.Red);
                        }
                    }
                }
            }

            return(bmp);
        }
Пример #12
0
        static List <LabeledTomogram> TomogramsFromGeneratedFiles()
        {
            Console.WriteLine("Loading shit...");
            List <LabeledTomogram> tomograms = new List <LabeledTomogram>();

            string[] files = Directory.GetFiles(@"/home/brush/tom4", "*.dat").ToArray();
            int      i     = 0;

            foreach (string file in files)
            {
                Console.WriteLine($"{i + 1}/{files.Length}");
                i++;
                //string dataFile = $"C:/Users/brush/Desktop/DataSets/data/{c}.bin";
                //string labelFile = $"C:/Users/brush/Desktop/DataSets/labels/{c}.bin";

                LabeledTomogram tom = DataReader.ReadDatFile(file);
                tomograms.AddRange(DataManipulator.FlipAndRotateTomogram(tom));
            }
            Console.WriteLine("...shit loaded.");

            return(tomograms);
        }
Пример #13
0
        public static LabeledTomogram RotateTomogram(LabeledTomogram input, float degrees)
        {
            LabeledTomogram ret = ShallowCopy(input);

            double aa = Math.Cos(degrees * Math.PI / 180);
            double ab = -Math.Sin(degrees * Math.PI / 180);
            double ba = Math.Sin(degrees * Math.PI / 180);
            double bb = Math.Cos(degrees * Math.PI / 180);

            int centerX = input.Width / 2;
            int centerY = input.Height / 2;

            int halfWidth  = input.Width / 2;
            int halfHeight = input.Height / 2;

            for (int y = -halfHeight; y < halfHeight; y++)
            {
                for (int x = -halfWidth; x < halfWidth; x++)
                {
                    int newX = (int)Math.Round(aa * x + ab * y);
                    int newY = (int)Math.Round(ba * x + bb * y);

                    int sourceIndex      = (y + halfHeight) * input.Width + (x + halfWidth);
                    int destinationIndex = (newY + halfHeight) * input.Width + (newX + halfWidth);

                    if (sourceIndex >= 0 && sourceIndex < input.Data.Length &&
                        destinationIndex >= 0 && destinationIndex < input.Data.Length)
                    {
                        ret.Data[destinationIndex]   = input.Data[sourceIndex];
                        ret.Labels[destinationIndex] = input.Labels[sourceIndex];
                    }
                }
            }

            return(ret);
        }
Пример #14
0
        static void Test()
        {
            BinaryFormatter bf = new BinaryFormatter();

            using (FileStream fs = File.OpenRead("serialized.dat"))
            {
                DecisionTreeNode node = bf.Deserialize(fs) as DecisionTreeNode;


                DecisionTreeOptions options = new DecisionTreeOptions
                {
                    // TODO: Fill in
                    MaximumNumberOfRecursionLevels = 25,
                    NumberOfFeatures        = 300,
                    NumberOfThresholds      = 35,
                    OffsetXMax              = 40,
                    OffsetXMin              = -40,
                    OffsetYMax              = 40,
                    OffsetYMin              = -40,
                    OutOfRangeValue         = 1000000,
                    SplittingThresholdMax   = .2f,
                    SufficientGainLevel     = 0,
                    PercentageOfPixelsToUse = .9f,
                    //DistanceThreshold = .1f,
                };


                MRCFile file = MRCParser.Parse(Path.Combine("/home/brush/tomography2_fullsirtcliptrim.mrc"));

                MRCFrame frame = file.Frames[145];

                LabeledTomogram tom = new LabeledTomogram();
                tom.Width  = frame.Width;
                tom.Height = frame.Height;
                tom.Data   = new float[frame.Width * frame.Height];

                for (int i = 0; i < frame.Data.Length; i++)
                {
                    tom.Data[i] = frame.Data[i];
                }

                //for (int y = 264, i = 0; y < 364; y++)
                //{
                //    for (int x = 501; x < 601; x++, i++)
                //    {
                //        tom.Data[i] = frame.Data[y * frame.Width + x];
                //    }
                //}


                float[] labels = DecisionTreeBuilder.Predict(tom, node, options);
                //Bitmap bmp = DataManipulator.Tomogram2Bitmap(tom);
                Bitmap bmp = Drawing.TomogramDrawing.PaintClassifiedPixelsOnTomogram(tom, labels);
                bmp.Save("/var/www/html/static/labeled_real.png", System.Drawing.Imaging.ImageFormat.Png);

                LabeledTomogram tom2 = DataReader.ReadDatFile("/home/brush/tom4/0.dat");

                labels = DecisionTreeBuilder.Predict(tom2, node, options);
                //Bitmap bmp = DataManipulator.Tomogram2Bitmap(tom);
                bmp = Drawing.TomogramDrawing.PaintClassifiedPixelsOnTomogram(tom2, labels);
                bmp.Save("/var/www/html/static/labeled_simulated.png", System.Drawing.Imaging.ImageFormat.Png);
            }
        }
Пример #15
0
 public static LabeledTomogram Open(LabeledTomogram tomogram, int erodeElementRadius, int dilateElementRadius)
 {
     return(Dilate(Erode(tomogram, erodeElementRadius), dilateElementRadius));
 }
Пример #16
0
 public static void SaveTomogramAsLabeledBitmap(LabeledTomogram tomogram)
 {
 }
Пример #17
0
        public static Cluster[] Cluster(LabeledTomogram tomogram, float epsilon, int minimumClusterSize)
        {
            List <Cluster> clusterCenters = new List <Cluster>();

            int currentClusterLabel = 1;

            int[] clusterLabels = new int[tomogram.Labels.Length];

            List <Point2D> points = new List <Point2D>();

            for (int y = 0, i = 0; y < tomogram.Height; y++)
            {
                for (int x = 0; x < tomogram.Width; x++, i++)
                {
                    if (tomogram.Labels[i] > 0)
                    {
                        points.Add(new Point2D {
                            X = x, Y = y
                        });
                    }
                }
            }

            foreach (Point2D point in points)
            {
                int index = (int)(point.Y * tomogram.Width + point.X);

                if (clusterLabels[index] == 0)
                {
                    List <Point2D> cluster = new List <Point2D>();
                    foreach (Point2D otherPoint in points)
                    {
                        if (Distance(point, otherPoint) <= epsilon)
                        {
                            cluster.Add(otherPoint);
                        }
                    }

                    // is the seed cluster size big enough?
                    if (cluster.Count >= minimumClusterSize)
                    {
                        // yes, expand outwards.
                        for (int n = 0; n < cluster.Count; n++)
                        {
                            Point2D clusterPoint  = cluster[n];
                            int     neighborIndex = (int)(clusterPoint.Y * tomogram.Width + clusterPoint.X);

                            // was this seen as noise originally?
                            if (clusterLabels[neighborIndex] == -1)
                            {
                                // yes, it's not noise afterall.
                                clusterLabels[neighborIndex] = currentClusterLabel;
                            }
                            // does this already belong to another group?
                            else if (clusterLabels[neighborIndex] > 0)
                            {
                                // yes, so drop out. look at next point.
                                continue;
                            }
                            // must be unlabeled.
                            else
                            {
                                // label it.
                                clusterLabels[neighborIndex] = currentClusterLabel;
                            }

                            List <Point2D> toAdd = new List <Point2D>();
                            // find all this guy's neighbors.
                            foreach (Point2D otherPoint in points)
                            {
                                int otherPointIndex = (int)otherPoint.Y * tomogram.Width + (int)otherPoint.X;
                                if (Distance(clusterPoint, otherPoint) <= epsilon)
                                {
                                    toAdd.Add(otherPoint);
                                }
                            }

                            // was this new cluster large enough?
                            if (toAdd.Count >= minimumClusterSize)
                            {
                                // yes, add it to the bunch.
                                cluster.AddRange(toAdd);
                            }
                        }

                        // we're done. create new label.
                        currentClusterLabel++;
                    }
                    else
                    {
                        // no, it's noise
                        clusterLabels[index] = -1;
                    }
                }
            }

            for (int c = 1; c < currentClusterLabel; c++)
            {
                Cluster cluster       = new Cluster();
                Point2D averagedPoint = new Point2D();
                int     number        = 0;

                for (int y = 0, i = 0; y < tomogram.Height; y++)
                {
                    for (int x = 0; x < tomogram.Width; x++, i++)
                    {
                        if (clusterLabels[i] == c)
                        {
                            averagedPoint.X += x;
                            averagedPoint.Y += y;

                            number++;

                            cluster.Members.Add(new Point2D {
                                X = x, Y = y
                            });
                        }
                    }
                }
                cluster.Center = new Point2D
                {
                    X = averagedPoint.X / (number * 1.0f),
                    Y = averagedPoint.Y / (number * 1.0f),
                };

                clusterCenters.Add(cluster);
            }

            return(clusterCenters.ToArray());
        }