/// <summary> /// Application entry point. /// Opens USDA Nutrient database (revision 26), denormalizes it using UsdaDenormalizer library /// and writes result to a CSV file with a specified name. /// </summary> /// <param name="args"> /// [0] path to the directory containing ASCII version of database /// [1] path to the output CSV file /// </param> static void Main(string[] args) { // check if input is valid if (args.Length > 1) { string inputDirectoryPath = args[0]; string outputCsvPath = args[1]; if (System.IO.Directory.Exists(inputDirectoryPath)) { // read nutrient data from files in filesystem to object model in memory DirectoryDataProvider dataProvider = new DirectoryDataProvider(inputDirectoryPath, 26, new WindowsTextReader()); DatabaseRepresentation dbModel = new DatabaseRepresentation(dataProvider); // create denormalized (flat) model based using database data DenormalizedModel denormalizedModel = new DenormalizedModel(dbModel); // write flat model into CSV file as one big table using (var writer = System.IO.File.CreateText(outputCsvPath)) { CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(writer); csvWriter.WriteRecords(denormalizedModel.FoodItems); } } else { throw new ArgumentException("Specified directory does not exist or you don't have an access to it: " + inputDirectoryPath); } } else { throw new ArgumentException("No arguments were provided."); } }
/// <summary> /// Application entry point. /// Opens USDA Nutrient database (revision 26), denormalizes it using UsdaDenormalizer library /// and writes result to a CSV file with a specified name. /// </summary> /// <param name="args"> /// [0] path to the directory containing ASCII version of database /// [1] path to the output CSV file /// </param> private static void Main(string[] args) { // check if input is valid if (args.Length > 1) { var inputDirectoryPath = args[0]; var outputCsvPath = args[1]; if (Directory.Exists(inputDirectoryPath)) { // read nutrient data from files in filesystem to object model in memory var dataProvider = new DirectoryDataProvider(inputDirectoryPath, 26, new WindowsTextReader()); var dbModel = new DatabaseRepresentation(dataProvider); // create denormalized (flat) model based using database data var denormalizedModel = new DenormalizedModel(dbModel); // write flat model into CSV file as one big table using (var writer = File.CreateText(outputCsvPath)) { var csvWriter = new CsvWriter(writer); csvWriter.WriteRecords(denormalizedModel.FoodItems); } } else { throw new ArgumentException( "Specified directory does not exist or you don't have an access to it: " + inputDirectoryPath); } } else { throw new ArgumentException("No arguments were provided."); } }
private PointsOfInterestClassifier Learn(string sourcePath, string truthPath) { this.WriteLine(null, "Learning..."); PointsOfInterestClassifier classifier = new PointsOfInterestClassifier(); using (DirectoryDataProvider dataProvider = new DirectoryDataProvider()) { dataProvider.Add(sourcePath, false, truthPath, "#Class"); ClassifierProgress <TestImage> progress = new ClassifierProgress <TestImage>( (source, index) => { this.StopwatchRestart(); }, (source, index, answer, exception) => { long duration = this.StopwatchStop(); Interlocked.Increment(ref this.totalImages); this.Write( null, "({0})\tFile: {1} ... ", index, source.SourceId.ToFileName(false)); if (exception != null) { this.WriteLine(null, "ERROR."); this.WriteException(null, exception); } else { this.WriteLine(null, "OK ({0} ms)", duration); } }); classifier.Train <TestImage>( dataProvider.Generate(null), (x, cancellationToken) => new ImageSource(x.SourceId, x.Image), x => string.Concat(x.Labels), progress, CancellationToken.None); } return(classifier); }
private void Test(PointsOfInterestClassifier classifier, string sourcePath, string truthPath) { this.WriteLine(null, "Testing..."); List <ClassificationResult <string> > results = new List <ClassificationResult <string> >(); using (DirectoryDataProvider dataProvider = new DirectoryDataProvider()) { dataProvider.Add(sourcePath, false, truthPath, "#Class"); ClassifierProgress <TestImage> progress = new ClassifierProgress <TestImage>( (source, index) => { this.StopwatchRestart(); }, (source, index, answer, exception) => { long duration = this.StopwatchStop(); Interlocked.Increment(ref this.totalImages); this.Write( null, "({0})\tFile: {1} ... ", index, source.SourceId.ToFileName(false)); if (exception != null) { this.WriteLine(null, "ERROR."); this.WriteException(null, exception); } else { this.WriteLine( null, "OK ({0} ms) {1} {2:F4}", duration, answer?.ClassName ?? string.Empty, answer?.Confidence ?? 0.0); } }); foreach ((TestImage image, Answer answer) in classifier.Classify <TestImage>( dataProvider.Generate(null), (x, cancellationToken) => new ImageSource(x.SourceId, x.Image), progress, CancellationToken.None)) { results.Add(new ClassificationResult <string>( answer.Id, answer.ClassName, string.Concat(image.Labels), answer.Confidence, answer.Confidence >= 0.2f)); } } // write report ClassificationReport <string> testReport = new ClassificationReport <string>(results); using (StreamWriter outputFile = new StreamWriter(Console.OpenStandardOutput())) ////using (StreamWriter outputFile = File.CreateText(this.options.OutputFileName)) { ClassificationReportWriter <string> .WriteReport(outputFile, testReport, ClassificationReportMode.All); } }