예제 #1
0
 /// <summary>
 /// Create a featureizer for the given sketch
 /// </summary>
 /// <param name="filename">The filename of the sketc</param>
 public SketchFeatureizer(string filename)
 {
     _filename = filename;
     ConverterXML.ReadXML reader = new ConverterXML.ReadXML(_filename);
     _s           = reader.Sketch;
     _outFilename = _filename.Replace(Files.FUtil.Extension(Files.Filetype.XML), Files.FUtil.Extension(Files.Filetype.FEATURESKETCH));
 }
예제 #2
0
 public Preprocessor(List <string> files, Set <string> ingates, Dictionary <string, string> canonical_examples, bool verbosity)
 {
     _verbose = verbosity;
     _td      = new TrainingData(WIDTH, HEIGHT);
     _gates   = ingates;
     if (_verbose)
     {
         Console.WriteLine("Beginning search for {0} gate types", _gates.Count);
     }
     foreach (string file in files)
     {
         if (_verbose)
         {
             Console.Write(".");
         }
         Sketch.Sketch input = new ConverterXML.ReadXML(file).Sketch;
         addGatesFrom(input);
     }
     foreach (KeyValuePair <string, string> ce in canonical_examples)
     {
         Sketch.Sketch input = new ConverterXML.ReadXML(ce.Value).Sketch;
         addCanonicalExample(ce.Key, input);
     }
     if (_verbose)
     {
         Console.Write(Environment.NewLine);
         Console.WriteLine("SUMMARY:");
         foreach (string gate in _gates)
         {
             Console.WriteLine("Found {0} {1}s", _td.Images(gate).Count, gate);
         }
     }
 }
예제 #3
0
        public void testSegment(string filename, int method, int colorMode)
        {
            Console.WriteLine();
            Console.WriteLine("GROUPING: " + filename + ".xml");

            //Console.Write("Loading XML file...");
            ConverterXML.ReadXML reader;
            try
            {
                reader = new ConverterXML.ReadXML(filename + ".xml");
                //Console.WriteLine("Done");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("ERROR: Could not find file: " + filename + ".xml");
                return;
            }

            //Console.WriteLine("Grouping...");
            Grouper.Grouper grouper = new Grouper.Grouper(reader.Sketch, method);
            grouper.groupAllSubstrokes();
            //Console.WriteLine("Done");

            //Console.Write("Writing output to " + filename + "...");
            grouper.writeToXML(filename, colorMode);
            //grouper.
        }
예제 #4
0
        public IndividualPreprocessor(List <string> files, Set <string> ingates, string dir)
        {
            _files = files;
            _gates = ingates;
            _dir   = dir;

            _sps = new Dictionary <string, List <Shape> >();
            foreach (string gate in _gates)
            {
                if (gate == "" || gate == " ")
                {
                    Console.WriteLine("Empty?");
                }
                _sps.Add(gate, new List <Shape>());
            }

            foreach (string file in files)
            {
                Sketch.Sketch input = new ConverterXML.ReadXML(file).Sketch;
                foreach (Shape sp in input.ShapesL)
                {
                    if (_sps.ContainsKey(sp.LowercasedType))
                    {
                        _sps[sp.LowercasedType].Add(sp);
                    }
                }
            }
        }
        public void TestXMLConversion()
        {
            const string filename = "tmp.xml";

            Domain.ShapeType testType = Domain.LogicDomain.AND;

            Sketch.Sketch sketch1 = Sketches.newValidSketch();

            foreach (Sketch.Shape shape in sketch1.Shapes)
            {
                shape.Type = testType;
            }

            ConverterXML.SaveToXML writer = new ConverterXML.SaveToXML(sketch1);
            writer.WriteXML(filename);

            ConverterXML.ReadXML reader  = new ConverterXML.ReadXML(filename);
            Sketch.Sketch        sketch2 = reader.Sketch;

            sketch2.CheckConsistency();

            Assert.IsTrue(sketch1.Equals(sketch2), "Original sketch is not equal to the loaded sketch");
            Assert.IsTrue(sketch2.Equals(sketch1), "Loaded sketch is not equal to the original");

            foreach (Sketch.Shape shape in sketch2.Shapes)
            {
                Assert.AreEqual(testType, shape.Type, "Shape types are not preserved across save/load");
            }
        }
예제 #6
0
        public void TestXMLConversion()
        {
            const string      filename = "tmp.xml";
            RecognitionResult type     = new RecognitionResult(LogicDomain.AND, 0.9, 0.33);

            Sketch.Project project1 = new Sketch.Project(Sketches.newValidSketch());;

            foreach (Sketch.Shape shape in project1.sketch.Shapes)
            {
                type.ApplyToShape(shape);
            }

            ConverterXML.SaveToXML writer = new ConverterXML.SaveToXML(project1);
            writer.WriteXML(filename);

            ConverterXML.ReadXML reader  = new ConverterXML.ReadXML(filename);
            Sketch.Sketch        sketch2 = reader.Sketch;

            sketch2.CheckConsistency();

            Assert.IsTrue(project1.sketch.Equals(sketch2), "Original sketch is not equal to the loaded sketch");
            Assert.IsTrue(sketch2.Equals(project1.sketch), "Loaded sketch is not equal to the original");

            foreach (Sketch.Shape shape in sketch2.Shapes)
            {
                Assert.AreEqual(type.Type, shape.Type, "Shape types are not preserved across save/load");
                Assert.IsTrue(Math.Abs(type.Confidence - shape.Probability) < 0.0001, "Shape confidences are not preserved across save/load");
                Assert.IsTrue(Math.Abs(type.Orientation - shape.Orientation) < 0.0001, "Shape orientations are not preserved across save/load");
            }
        }
예제 #7
0
        public void testMakeHHRECO()
        {
            string filename = "c:\\trainingexamples.labeled.xml";

            ConverterXML.ReadXML testRead = new ConverterXML.ReadXML(filename);

            ConverterXML.MakeHHRECO hhreco = new ConverterXML.MakeHHRECO(testRead.Sketch);
            hhreco.writeXML("c:\\test.sml");
        }
예제 #8
0
        public void testWriteSVM()
        {
            string filename = "C:\\Documents and Settings\\Devin Smith\\My Documents\\Visual Studio Projects\\branches\\redesign\\JntToXml\\bin\\Release\\convertedJnt\\boxcir.1.fragged.labeled.xml";

            //string filename = "C:\\Documents and Settings\\Devin Smith\\My Documents\\Visual Studio Projects\\branches\\redesign\\JntToXml\\bin\\Release\\convertedJnt\\circ_sq_test.1.fragged.labeled.xml";
            ConverterXML.ReadXML testRead = new ConverterXML.ReadXML(filename);

            Sketch.Substroke[] substrokes = testRead.Sketch.Substrokes;
            Featurefy.Normal   normal     = new Featurefy.Normal(substrokes);
            double[][]         features   = normal.Features;

            FileStream file = new FileStream(filename + ".train", FileMode.Create, FileAccess.Write);

            StreamWriter sw = new StreamWriter(file);


            int length = substrokes.Length;

            for (int i = 0; i < length; ++i)
            {
                /*
                 * if(i == length - 1)
                 *      Console.WriteLine();
                 */
                string[] labels = substrokes[i].GetLabels();

                if (labels.Length > 0 && substrokes[i].GetLabels()[0].ToLower().Equals("circle"))
                {
                    sw.Write("1 ");
                }
                else
                {
                    sw.Write("-1 ");
                }

                int dlength = features[i].Length;

                for (int j = 0; j < dlength; ++j)
                {
                    sw.Write((j + 1) + ":" + features[i][j] + " ");
                }
                sw.WriteLine();
            }

            sw.Close();
            file.Close();
        }
예제 #9
0
        /// <summary>
        /// A quick hacky main function to test this.
        /// Uses hard-coded paths for input and output files.
        /// </summary>
        /// <param name="args">unused</param>
        public static void Main(String[] args)
        {
            String filename = @"c:\documents and settings\christine\my documents\sketch\data\e85\new labeled documents\labeled summer 2007\0268_1.6.1.labeled.xml";
            string outfile  = @"c:\documents and settings\christine\my documents\out.xml";

            Sketch.Sketch sketch = new ConverterXML.ReadXML(filename).Sketch;
            sketch.RemoveShapes();
            Console.WriteLine("beginning to find neighborhood graph");
            NeighborhoodMap graph = new NeighborhoodMap(sketch, 100, false, 5);

            Console.WriteLine("done");
            AddDebuggingLines(ref sketch, graph);
            Console.WriteLine("writing xml");
            ConverterXML.MakeXML mx = new ConverterXML.MakeXML(sketch);
            mx.WriteXML(outfile);
            Console.Read();
        }
예제 #10
0
        /// <summary>
        /// If the module is first in the execution queue, this function will be called on a series of
        /// filenames.
        /// Default behavior: Standard loading behavior for .jnt and .xml files
        /// </summary>
        /// <param name="filename">A filename to load</param>
        /// <returns>The sketch that is loaded.</returns>
        private static Sketch.Sketch loadFile(string filename)
        {
            Sketch.Sketch sketch;

            // Load
            if (Files.FUtil.FileType(filename) == Files.Filetype.XML)
            {
                try
                {
                    ConverterXML.ReadXML xmlReader = new ConverterXML.ReadXML(filename);
                    sketch = xmlReader.Sketch;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("oops, caught exception {0}", e);
                    return(null);
                }
            }
            else if (Files.FUtil.FileType(filename) == Files.Filetype.JOURNAL)
            {
                int numPages = ConverterJnt.ReadJnt.NumberOfPages(filename);

                if (numPages > 1)
                {
                    Console.WriteLine("WARNING: Detected more than one page in journal file '" + filename + "'. Just using the first one...");
                }
                ConverterJnt.ReadJnt ink = new ConverterJnt.ReadJnt(filename, 1);
                sketch = ink.Sketch;
            }
            else
            {
                Console.WriteLine("WARNING: {0} is an unsupported filetype", filename);
                return(null);
            }

            // Fill in stroke classifications based on shape types, since this info
            // isn't saved in the XML or JNT files.
            foreach (Sketch.Shape shape in sketch.Shapes)
            {
                // In making this assignment we update the classification for the
                // substrokes and the shape itself.
                shape.Type = shape.Type;
            }

            return(sketch);
        }
        private static string recognizeFiles(string dir, string pattern, GateRecognizer gr)
        {
            // "-r" = recursively search subdirectories for files that match pattern
            List <string> filenames = getFiles(dir, pattern);

            string output = "";

            if (filenames == null)
            {
                Console.WriteLine("Error, no files found in directory {0} with pattern {1}", dir, pattern);
                return("");
            }
            foreach (string fileName in filenames)
            {
                ConverterXML.ReadXML reader  = new ConverterXML.ReadXML(fileName);
                Substroke[]          strokes = reader.Sketch.Substrokes;
                Recognizer.Results   results = gr.Recognize(strokes);
                output += fileName + "\n\t";
                Console.WriteLine(fileName);
                output += results.ToString() + "\n";
            }
            return(output);
        }
예제 #12
0
        // It should be noted that the functions to create the ARFF files are somewhat of a hack. They work, on my computer.
        // They may also work on yours. Hopefully once the ARFF files are created nobody will ever have to use these functions
        // again, but we're leaving them here anyways. Use at your own risk and feel free to email me if you have questions.
        //      Good luck!  -- Jessi Peck, Sketcher 2011, [email protected]
        /// <summary>
        /// Create ARFF files for the classifier and grouper from a set of sketches
        /// </summary>
        /// <param name="fileList">a list of sketch file names</param>
        public static void createARFFs(List <string> fileList)
        {
            // These dictionaries will store the feature values for each of the ARFF files we're creating
            Dictionary <double[], string> strokeValues = new Dictionary <double[], string>();
            Dictionary <string, Dictionary <double[], string> > groupValues = new Dictionary <string, Dictionary <double[], string> >();

            groupValues.Add(TEXT, new Dictionary <double[], string>());
            groupValues.Add(WIRE, new Dictionary <double[], string>());
            groupValues.Add(GATE, new Dictionary <double[], string>());

            // These lists will store the names of the features for the ARFF files we're creating
            List <string> strokeNames = new List <string>();
            List <string> groupNames  = new List <string>();

            // Stores each stroke's classification.
            Dictionary <Substroke, string> strokeClassifications = new Dictionary <Substroke, string>();

            Dictionary <string, string> settingsFiles = Files.SettingsReader.readSettings();

            bool firstFile = true;

            foreach (string file in fileList)
            {
                if (!file.EndsWith(".xml"))
                {
                    continue;
                }                                         // only read .xml files

                //Console.WriteLine("Reading file " + file);

                // Load the sketch from the file and make it into a feature sketch
                Sketch.Sketch           sketch        = new ConverterXML.ReadXML(file).Sketch;
                Featurefy.FeatureSketch featureSketch = Featurefy.FeatureSketch.MakeFeatureSketch(new Sketch.Project(sketch), settingsFiles);

                // Get featureSketch to tell you the feature values for each individual stroke in the sketch
                Dictionary <Substroke, double[]> classificationDict = new Dictionary <Substroke, double[]>();
                foreach (Substroke substroke in sketch.Substrokes)
                {
                    classificationDict.Add(substroke, featureSketch.GetValuesSingle(substroke));
                }

                // Classify each substroke based on what shape it's a part of
                foreach (Sketch.Shape shape in sketch.Shapes)
                {
                    string classif = shape.Type.Classification;
                    if (classif == UNKNOWN)
                    {
                        continue;
                    }

                    foreach (Substroke stroke in shape.Substrokes)
                    {
                        strokeValues.Add(classificationDict[stroke], classif);
                        strokeClassifications.Add(stroke, classif);
                    }
                }

                #region Bad XML File Catcher
                // This shouldn't be necessary if your XML files are properly formatted,
                // but it's here just in case.
                List <Substroke> badStrokes = new List <Substroke>();
                foreach (Substroke strokeToCheck in classificationDict.Keys)
                {
                    if (classificationDict[strokeToCheck].Length != 27)
                    {
                        Console.WriteLine("OH MAN. GET VALUES SINGLE FAILS.");
                        Console.WriteLine("Your length of features is " + classificationDict[strokeToCheck].Length + " elements long.");
                        badStrokes.Add(strokeToCheck);
                    }
                }

                foreach (Substroke naughtyStroke in badStrokes)
                {
                    strokeValues.Remove(classificationDict[naughtyStroke]);
                    strokeClassifications.Remove(naughtyStroke);
                    classificationDict.Remove(naughtyStroke);
                    Console.WriteLine("You will never see that stroke again.");
                }
                #endregion

                // Get featureSketch to tell you the feature values for each pair of strokes in the sketch
                Dictionary <string, Dictionary <Featurefy.FeatureStrokePair, double[]> > grouperDict = featureSketch.GetValuesPairwise(strokeClassifications);

                // You only need to call this once, might as well be on the first file you do.
                if (firstFile)
                {
                    strokeNames = featureNames(featureSketch.FeatureListSingle); // set up the list of feature names for stroke classification
                    groupNames  = featureNames(featureSketch.FeatureListPair);   // set up the list of feature names for stroke grouping
                    firstFile   = false;
                }

                // Adds feature values for grouping to groupValues
                getGroupValues(ref groupValues, grouperDict);
            }
            // Populate the lists of class names
            List <string> strokeClasses, groupClasses;
            classNames(out strokeClasses, out groupClasses);

            string directory = AppDomain.CurrentDomain.BaseDirectory;

            // write ARFF for stroke classifier
            writeARFFfile(directory + STROKE + ARFF, strokeNames, strokeValues, strokeClasses);

            // write ARFF for stroke grouper x3
            writeARFFfile(directory + TEXT_GROUP + ARFF, groupNames, groupValues[TEXT], groupClasses);
            //writeARFFfile(directory + WIRE_GROUP + ARFF, groupNames, groupValues[WIRE], groupClasses);
            writeARFFfile(directory + GATE_GROUP + ARFF, groupNames, groupValues[GATE], groupClasses);

            //Console.WriteLine("ARFF files created!");
        }
예제 #13
0
        public static void Main(string[] args)
        {
            bool fragment = false;

            #region Handle Input

            List <string> arguments = new List <string>(args);

            // First, look for flags
            List <string> flags = arguments.FindAll(delegate(string s) { return(s[0] == '-'); });
            foreach (string f in flags)
            {
                arguments.Remove(f);
            }
            foreach (string flag in flags)
            {
                switch (flag[1])
                {
                case 'F':
                    fragment = true;
                    break;
                }
            }

            if (arguments.Count != 2)
            {
                printUsageAndExit();
            }
            if (!Directory.Exists(arguments[1]))
            {
                Console.WriteLine("Directory {0} not found!", arguments[1]);
                Console.WriteLine();
                printUsageAndExit();
            }
            if (!File.Exists(arguments[0]))
            {
                Console.WriteLine("LabelMapper Description File {0} not found!{1}", arguments[0], Environment.NewLine);
                printUsageAndExit();
            }
            string lmFile = arguments[0];

            List <string> files = new List <string>(Directory.GetFiles(arguments[1], "*.xml", SearchOption.AllDirectories));
            if (files.Count == 0)
            {
                Console.WriteLine("No XML files found in {0}!{1}", arguments[1], Environment.NewLine);
                printUsageAndExit();
            }
            string outputDirectory;
            if (fragment)
            {
                outputDirectory = arguments[1] + @"\DTF_Fragmented";
                Console.WriteLine("Will auto-fragment all sketches");
            }
            else
            {
                outputDirectory = arguments[1] + @"\DTF_Unfragmented";
                Console.WriteLine("Will not auto-fragment any sketches");
            }
            if (!Directory.Exists(outputDirectory))
            {
                Console.WriteLine("Creating output directory {0}", outputDirectory);
                Directory.CreateDirectory(outputDirectory);
            }
            else
            {
                Console.WriteLine("Using existing output directory {0}", outputDirectory);
            }

            #endregion

            string names_file = outputDirectory + "\\features.names";

            LabelMapper.LabelMapper lm = new LabelMapper.LabelMapper(lmFile);
            DTF all = new DTF(lm.translatedClasses);
            addFeaturesToDTF(ref all);
            all.WriteNamesFile(names_file);

            Console.WriteLine("Beginning DTF calculations at {0}", DateTime.Now.ToShortTimeString());

            foreach (string filename in files)
            {
                Console.Write(".");
                if (!File.Exists(filename))
                {
                    throw new FileNotFoundException("File {0} not found", filename);
                }
                Sketch.Sketch sk;
                try
                {
                    ConverterXML.ReadXML reader = new ConverterXML.ReadXML(filename);
                    sk = reader.Sketch;
                }
                catch (System.Xml.XmlException e)
                {
                    Console.WriteLine("Error reading file {0} with ConverterXML. Exception Text: {1}", filename, e.Message);
                    continue;
                }
                if (fragment)
                {
                    Fragmenter.Fragment.fragmentSketch(sk);
                }
                FeatureSketch sketch = new FeatureSketch(ref sk);

                Node[] nodes = new Node[sk.Substrokes.Length];
                for (int i = 0; i < sk.Substrokes.Length; ++i)
                {
                    nodes[i] = new Node(sk.Substrokes[i], lm.translatedClasses.Count, i);
                }

                #region Calculate Features

                double   totalMinDistBetweenFrag     = sketch.TotalMinDistBetweenSubstrokes;
                double   totalAverageDistBetweenFrag = sketch.TotalAvgDistBetweenSubstrokes;
                double   totalMaxDistBetweenFrag     = sketch.TotalMaxDistBetweenSubstrokes;
                double   totalTimeBetweenFrag        = CreateGraph.totTimeBetweenFrag(nodes);
                double   totalArcLength          = sketch.TotalArcLength;
                double   totalLengthOfFrag       = sketch.TotalDistance;
                double   averageSpeedOfFrag      = sketch.AverageAverageSpeed;
                double   totalMinDistBetweenEnds = sketch.TotalMinDistBetweenSubstrokes;
                double[] bbox = sketch.BBox.ToArray();


                SiteFeatures.setStageNumber(1);
                InteractionFeatures.setStageNumber(1);
                SiteFeatures site = new SiteFeatures(totalMinDistBetweenFrag,
                                                     totalAverageDistBetweenFrag,
                                                     totalMaxDistBetweenFrag,
                                                     totalTimeBetweenFrag,
                                                     totalArcLength,
                                                     totalLengthOfFrag,
                                                     averageSpeedOfFrag,
                                                     totalMinDistBetweenEnds,
                                                     bbox, ref sketch);
                InteractionFeatures inter = new InteractionFeatures(totalMinDistBetweenFrag,
                                                                    totalAverageDistBetweenFrag,
                                                                    totalMaxDistBetweenFrag,
                                                                    totalTimeBetweenFrag,
                                                                    totalArcLength,
                                                                    totalLengthOfFrag,
                                                                    averageSpeedOfFrag,
                                                                    totalMinDistBetweenEnds,
                                                                    ref sketch);

                #endregion

                #region Write out

                for (int i = 0; i < nodes.Length; ++i)
                {
                    if (!lm.labelMap.ContainsKey(sk.Substrokes[i].FirstLabel))
                    {
                        Console.WriteLine("Label {0} not found in label map, continuing", sk.Substrokes[i].FirstLabel);
                        continue;
                    }
                    double[] ret    = site.evalSiteFeatures(nodes[i], sk.Substrokes);
                    int      allIdx = addSSStrokeToDTF(ref all, ref ret, ref sk.Substrokes[i], ref lm);
                    ret = aggregateInteractionFeatures(inter, nodes[i], nodes, sk.Substrokes);
                    addMSStrokeToDTF(ref all, ref ret, allIdx);
                }

                #endregion
            }
            Console.Write(Environment.NewLine);
            all.WriteDataFile(outputDirectory + "\\all.data");
            Console.WriteLine("DTF Calculations finished at {0}", DateTime.Now.ToShortTimeString());
        }
예제 #14
0
        /// <summary>
        /// If the module is first in the execution queue, this function will be called on a series of
        /// filenames.
        /// Default behavior: Standard loading behavior for .jnt and .xml files
        /// </summary>
        /// <param name="filename">A filename to load</param>
        /// <returns>The sketch that is loaded.</returns>
        private static Sketch.Sketch loadFile(string filename)
        {
            Sketch.Sketch sketch;

            // Load
            if (Files.FUtil.FileType(filename) == Files.Filetype.XML)
            {
                try
                {
                    ConverterXML.ReadXML xmlReader = new ConverterXML.ReadXML(filename);
                    sketch = xmlReader.Sketch;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("oops, caught exception {0}", e);
                    return(null);
                }
            }
            else if (Files.FUtil.FileType(filename) == Files.Filetype.JOURNAL)
            {
                int numPages = ConverterJnt.ReadJnt.NumberOfPages(filename);

                if (numPages > 1)
                {
                    Console.WriteLine("WARNING: Detected more than one page in journal file '" + filename + "'. Just using the first one...");
                }
                ConverterJnt.ReadJnt ink = new ConverterJnt.ReadJnt(filename, 1);
                sketch = ink.Sketch;
            }
            else
            {
                Console.WriteLine("WARNING: {0} is an unsupported filetype", filename);
                return(null);
            }

            List <Sketch.Substroke> substrokesToRemove = new List <Sketch.Substroke>();

            // Fill in stroke classifications based on shape types, since this info
            // isn't saved in the XML or JNT files.
            foreach (Sketch.Shape shape in sketch.Shapes)
            {
                if (shape.Type.Equals(new ShapeType()))
                {
                    Console.WriteLine("Ignoring shape with unknown type: " + shape);
                    substrokesToRemove.AddRange(shape.Substrokes);
                }
                else
                {
                    // In making this assignment we update the classification for the
                    // substrokes and the shape itself.
                    shape.Type = shape.Type;

                    // Make absolutely sure we are free to alter the shape
                    shape.AlreadyLabeled  = false;
                    shape.AlreadyGrouped  = false;
                    shape.AlreadyOriented = false;
                }
            }

            // Remove shapes with unknown types
            foreach (Substroke substroke in substrokesToRemove)
            {
                sketch.RemoveSubstroke(substroke);
            }

            sketch.CheckConsistency();

            return(sketch);
        }