예제 #1
0
 internal void Save(string filePath)
 {
     using (FileStream fs = new FileStream(filePath, FileMode.Create))
     {
         XmlSerializer xs = new XmlSerializerFactory().CreateSerializer(this.GetType());
         xs.Serialize(fs, this);
     }
 }
예제 #2
0
        public static void Serialization(string path, object target)
        {
            var serializer = new XmlSerializerFactory().CreateSerializer(target.GetType());

            using (Stream stream = new StreamWriter(path).BaseStream)
            {
                serializer.Serialize(stream, target);
            }
        }
예제 #3
0
        public static void Serialization(string path, object serializationObject)
        {
            if (serializationObject == null)
            {
                return;
            }

            var serializer = new XmlSerializerFactory().CreateSerializer(serializationObject.GetType());

            using (Stream stream = new StreamWriter(path).BaseStream)
            {
                serializer.Serialize(stream, serializationObject);
            }
        }
예제 #4
0
 public void Save()
 {
     if (!Directory.GetParent(SettingsFilePath).Exists)
     {
         Directory.GetParent(SettingsFilePath).Create();
     }
     using (FileStream fs = new FileStream(SettingsFilePath, FileMode.Create))
     {
         XmlSerializer xs = new XmlSerializerFactory().CreateSerializer(this.GetType());
         if (xs != null)
         {
             xs.Serialize(fs, this);
         }
     }
     IsDirty = false;
 }
예제 #5
0
 private void SaveFile(string filePath)
 {
     if (!Directory.GetParent(filePath).Exists)
     {
         Directory.GetParent(filePath).Create();
     }
     else if (File.Exists(filePath) && File.GetAttributes(filePath).HasFlag(FileAttributes.ReadOnly))
     {
         File.SetAttributes(filePath, FileAttributes.Normal);
     }
     using (FileStream fs = new FileStream(filePath, FileMode.Create))
     {
         XmlSerializer xs = new XmlSerializerFactory().CreateSerializer(this.GetType());
         xs.Serialize(fs, this);
     }
 }
예제 #6
0
        public static void Save <T>(string path, T data)
        {
            if (!File.Exists(path))
            {
                throw new CommandLineException("Failed to find file at {0}", path);
            }

            using (var file = File.CreateText(path))
            {
                var parser = new XmlSerializerFactory().CreateSerializer(typeof(T));
                if (parser == null)
                {
                    throw new CommandLineException("Failed to create serialized for " + typeof(T).FullName);
                }

                parser.Serialize(file, data);
            }
        }
예제 #7
0
        /// <summary>
        /// Save rss feed information to file
        /// </summary>
        public static void SaveFeedDetails()
        {
            Stream output = File.Create(Resources.DataPath + "data.xml");

            try
            {
                XmlSerializer s = new XmlSerializerFactory().CreateSerializer(typeof(FeedList));
                s.Serialize(output, Feeds);
            }
            //catch (Exception e)
            //{
            //    throw e;
            //}
            finally
            {
                output.Close();
            }
            //foreach (PodcastFeed p in Feeds)
            //{
            //    p.SaveEpisodes();
            //}
        }
예제 #8
0
        private bool WriteObjectGraphToStorage(GenesisContext genesis)
        {
            var s = new XmlSerializerFactory().CreateSerializer(typeof(List <ObjectGraph>));

            var outDir = Path.Combine(Environment.CurrentDirectory, @"Output");

            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }

            var d              = DateTime.UtcNow;
            var fileName       = $"ObjectGraphDump_{d.Day}{ d.Hour}{d.Minute}{d.Second}{d.Millisecond}.xml";
            var outputFilePath = Path.Combine(outDir, fileName);

            using var stream = File.OpenWrite(outputFilePath);

            s?.Serialize(stream, genesis.Objects);

            Text.YellowLine($"ObjectGraph written to [{outputFilePath}]");
            return(true);
        }
예제 #9
0
        public static void Main(string[] args)
        {
            Hyperparameters parameters = new Hyperparameters {
                NumTrees    = 10,
                MaxFeatures = -1,
                MinFeatures = 1,
                MaxDepth    = 10,
                Seed        = (int)(DateTime.Now.Ticks % int.MaxValue),
                OutOfBag    = 0.3
            };
            string        trainingFile   = null;
            string        serializedFile = null;
            List <double> testData       = new List <double>();

            for (int i = 0; i < args.Length; ++i)
            {
                switch (args[i])
                {
                case "--num-trees":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!int.TryParse(args[++i], out parameters.NumTrees))
                    {
                        InvalidArgs();
                    }
                    break;

                case "--max-features":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!int.TryParse(args[++i], out parameters.MaxFeatures))
                    {
                        InvalidArgs();
                    }
                    break;

                case "--min-features":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!int.TryParse(args[++i], out parameters.MinFeatures))
                    {
                        InvalidArgs();
                    }
                    break;

                case "--max-depth":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!int.TryParse(args[++i], out parameters.MaxDepth))
                    {
                        InvalidArgs();
                    }
                    break;

                case "--seed":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!int.TryParse(args[++i], out parameters.Seed))
                    {
                        InvalidArgs();
                    }
                    break;

                case "--oob":
                    if (i + 1 >= args.Length)
                    {
                        InvalidArgs();
                    }
                    if (!double.TryParse(args[++i], out parameters.OutOfBag))
                    {
                        InvalidArgs();
                    }
                    break;

                default:
                    double val;
                    if (args[i].EndsWith(".csv"))
                    {
                        if (trainingFile == null)
                        {
                            trainingFile = args[i];
                        }
                        else
                        {
                            InvalidArgs();
                        }
                    }
                    else if (args[i].EndsWith(".bin") || args[i].EndsWith(".xml"))
                    {
                        if (serializedFile == null)
                        {
                            serializedFile = args[i];
                        }
                        else
                        {
                            InvalidArgs();
                        }
                    }
                    else if (double.TryParse(args[i], out val))
                    {
                        testData.Add(val);
                    }
                    else
                    {
                        InvalidArgs();
                    }
                    break;
                }
            }
            RandomForest forest = null;

            if (trainingFile == null)
            {
                if (serializedFile == null || !File.Exists(serializedFile))
                {
                    Console.WriteLine("No model source");
                    InvalidArgs();
                }
                else
                {
                    if (serializedFile.EndsWith(".xml"))
                    {
                        XmlSerializer serializer = new XmlSerializerFactory().CreateSerializer(typeof(RandomForest));
                        using (Stream stream = new FileStream(serializedFile, FileMode.Open, FileAccess.Read)) {
                            forest = (RandomForest)serializer.Deserialize(stream);
                        }
                    }
                    else
                    {
                        BinaryFormatter serializer = new BinaryFormatter();
                        using (Stream stream = new FileStream(serializedFile, FileMode.Open, FileAccess.Read)) {
                            forest = (RandomForest)serializer.Deserialize(stream);
                        }
                    }
                }
            }
            else
            {
                LearningSet learningSet = new LearningSet(trainingFile);
                if (parameters.MaxFeatures == -1)
                {
                    parameters.MaxFeatures = (int)Math.Sqrt(learningSet.First().Features.Length);
                }
                forest = new RandomForest(learningSet, parameters);
                if (serializedFile != null)
                {
                    if (serializedFile.EndsWith(".xml"))
                    {
                        XmlSerializer serializer = new XmlSerializerFactory().CreateSerializer(typeof(RandomForest));
                        using (Stream stream = new FileStream(serializedFile, FileMode.Create, FileAccess.Write)) {
                            serializer.Serialize(stream, forest);
                        }
                    }
                    else
                    {
                        BinaryFormatter serializer = new BinaryFormatter();
                        using (Stream stream = new FileStream(serializedFile, FileMode.Create, FileAccess.Write)) {
                            serializer.Serialize(stream, forest);
                        }
                    }
                }
            }
            if (testData.Count > 0)
            {
                if (testData.Count == forest.NumFeatures)
                {
                    Console.WriteLine(forest.Classify(new DataPoint {
                        Features = testData.ToArray()
                    }));
                }
                else
                {
                    Console.WriteLine("Invalid number of features");
                    InvalidArgs();
                }
            }
            else
            {
                Console.WriteLine("Accuracy: {0}%", forest.Accuracy * 100.0);
            }
        }