public void validate_data_from_tsv()
        {
            int    linenumber = 5;
            string line;

            string[] row;

            Stream stream         = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", "Vts");
            var    testSpectra    = SpectralDatabase.GetSpectraFromFile(stream, true);
            var    testDictionary = testSpectra.ToDictionary();

            stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", "Vts");
            using (StreamReader readFile = new StreamReader(stream))
            {
                // read n lines (there is one line of header so
                for (int i = 0; i <= linenumber; i++)
                {
                    readFile.ReadLine();
                }
                // get a line from the stream and split the data
                line = readFile.ReadLine();
                row  = line.Split('\t');
            }
            Assert.AreEqual(testDictionary["Hb"].Wavelengths[linenumber], Convert.ToDouble(row[0]));
            // dc: this would be only for MolarExtinctionCoefficient or FractionalExtinctionCoefficient, not MolarAbsorptionCoefficient or FractionalAbsorptionCoefficient
            // multiply the value by ln(10)
            // double k =  Math.Log(10);
            double k       = 1D;
            double spectra = Convert.ToDouble(row[1]) * k;

            // test that the values in the text stream match the ones in the object
            Assert.AreEqual(testDictionary["HbO2"].Spectrum[linenumber], spectra);
            spectra = Convert.ToDouble(row[2]) * k;
            Assert.AreEqual(testDictionary["Hb"].Spectrum[linenumber], spectra);
        }
        public void validate_loading_spectral_database_from_tsv_in_resources()
        {
            Stream stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", "Vts");

            List <ChromophoreSpectrum> myChromophoreList = new List <ChromophoreSpectrum>();
            //create 2 sets of values for the tab delimeted file
            ChromophoreSpectrum c = new ChromophoreSpectrum();

            c.Name = "HbO2";
            c.AbsorptionCoefficientUnit = AbsorptionCoefficientUnit.InverseMillimeters;
            c.MolarUnit = MolarUnit.None;
            c.ChromophoreCoefficientType = ChromophoreCoefficientType.MolarAbsorptionCoefficient;
            myChromophoreList.Add(c);
            ChromophoreSpectrum c2 = new ChromophoreSpectrum();

            c2.Name = "Hb";
            c2.AbsorptionCoefficientUnit = AbsorptionCoefficientUnit.InverseMillimeters;
            c2.MolarUnit = MolarUnit.None;
            c2.ChromophoreCoefficientType = ChromophoreCoefficientType.MolarAbsorptionCoefficient;
            myChromophoreList.Add(c2);
            var testDictionary = myChromophoreList.ToDictionary();

            SpectralDatabase.AppendDatabaseFromFile(testDictionary, stream);
            testDictionary.WriteToJson("dictionary3.txt");
            Assert.IsTrue(FileIO.FileExists("dictionary3.txt"));
        }
        public void validate_loading_spectral_database_and_header_from_tsv_no_conversion()
        {
            Stream stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", "Vts");

            var testSpectra    = SpectralDatabase.GetSpectraFromFile(stream, false);
            var testDictionary = testSpectra.ToDictionary();

            testDictionary.WriteToJson("dictionary5.txt");
            Assert.IsTrue(FileIO.FileExists("dictionary5.txt"));
        }
Exemple #4
0
        public void validate_Loading_Spectral_Database_and_header_from_tsv_in_resources()
        {
            Stream stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", _assemblyName);

            var testSpectra    = SpectralDatabase.GetSpectraFromFile(stream, true);
            var testDictionary = testSpectra.ToDictionary();

            testDictionary.WriteToJson("dictionary4.txt");
            Assert.IsTrue(FileIO.FileExists("dictionary4.txt"));
        }
        public void validate_write_text_files_from_file_in_resources()
        {
            var testDictionary = SpectralDatabase.GetDefaultDatabaseFromFileInResources();

            SpectralDatabase.WriteDatabaseToFiles(testDictionary);
            Assert.IsTrue(FileIO.FileExists("absorber-Fat.txt"));
            Assert.IsTrue(FileIO.FileExists("absorber-H2O.txt"));
            Assert.IsTrue(FileIO.FileExists("absorber-Hb.txt"));
            Assert.IsTrue(FileIO.FileExists("absorber-HbO2.txt"));
            Assert.IsTrue(FileIO.FileExists("absorber-Melanin.txt"));
            Assert.IsTrue(FileIO.FileExists("absorber-Nigrosin.txt"));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="importFiles">the name(s) of the file(s) to import</param>
        /// <param name="importPath">the path of the files to import (relative or absolute)</param>
        /// <param name="outname">the name of the resulting output xml spectral dictionary</param>
        /// <param name="outpath">the output directory of the generated xml dictionary (relative or absolute)</param>
        public static ChromophoreSpectrumDictionary ImportSpectraFromFile(
            string[] importFiles = null,
            string importPath    = "",
            string outname       = "SpectralDictionary",
            string outpath       = "")
        {
            if (importFiles == null || importFiles.Length == 0 || string.IsNullOrEmpty(importFiles[0]))
            {
                importFiles = new string[] { "absorber-*.txt" };
            }

            var allFiles = importFiles.SelectMany(file => Directory.GetFiles(
                                                      importPath ?? Directory.GetCurrentDirectory(), file));

            var chromophoreDictionary = new ChromophoreSpectrumDictionary();

            logger.Info(() => "Importing spectral data files");
            foreach (var file in allFiles)
            {
                if (File.Exists(file))
                {
                    try
                    {
                        logger.Info("Importing file: " + file);
                        var stream = StreamFinder.GetFileStream(file, FileMode.Open);

                        SpectralDatabase.AppendDatabaseFromFile(chromophoreDictionary, stream);
                    }
                    catch (Exception e)
                    {
                        logger.Info("****  An error occurred while importing file: " + file);
                        logger.Info("Detailed error: " + e.Message);
                    }
                }
            }

            return(chromophoreDictionary);
        }
        public void validate_loading_spectral_database_from_file_in_resources()
        {
            var _testDictionary = SpectralDatabase.GetDefaultDatabaseFromFileInResources();

            Assert.IsNotNull(_testDictionary);
        }
        static void Main(string[] args)
        {
            var skipImport = false;

            string[] filenames = null;
            string   path      = null;
            string   outname   = null;
            string   outpath   = null;

            args.Process(() =>
            {
                logger.Info("\nSpectral Data Import\n");
                logger.Info("For more information type import.exe help");
                logger.Info("For help on a specific topic type import.exe help=<topicname>\n");
            },
                         new CommandLine.Switch("help", val =>
            {
                var helpTopic = val.FirstOrDefault();
                ShowHelp(helpTopic);
                skipImport = true;
            }),
                         new CommandLine.Switch("@/help", val =>
            {
                var helpTopic = val.FirstOrDefault();
                ShowHelp(helpTopic);
                skipImport = true;
            }),
                         new CommandLine.Switch("generatefiles", val =>
            {
                logger.Info(() => "Generating spectral data files...");
                try
                {
                    var testDictionary = Vts.SpectralMapping.SpectralDatabase.GetDefaultDatabaseFromFileInResources();
                    SpectralDatabase.WriteDatabaseToFiles(testDictionary);
                }
                catch (Exception e)
                {
                    logger.Info("****  An error occurred while generating the import files  ****");
                    logger.Info("Detailed error: " + e.Message);
                }
                skipImport = true;
            }),
                         new CommandLine.Switch("filename", val =>
            {
                filenames = new[] { val.FirstOrDefault() };
                logger.Info(() => "import files specified as: " + filenames[0]);
            }),
                         new CommandLine.Switch("filenames", val =>
            {
                filenames   = val.ToArray();
                var message = "import files specified as: ";
                if (filenames.Length > 0)
                {
                    message = message + filenames.First();
                    for (int i = 1; i < filenames.Count(); i++)
                    {
                        message = message + ", " + filenames[i];
                    }
                }
                logger.Info(() => message);
            }),
                         new CommandLine.Switch("path", val =>
            {
                path = val.FirstOrDefault();
                logger.Info(() => "import path specified as " + path);
            }),
                         new CommandLine.Switch("outname", val =>
            {
                outname = val.FirstOrDefault();
                logger.Info(() => "outname specified as " + outname);
            }),
                         new CommandLine.Switch("outpath", val =>
            {
                outpath = val.FirstOrDefault();
                logger.Info(() => "outpath specified as " + outpath);
            }));

            if (skipImport)
            {
                return;
            }

            var chromophoreDictionary = SpectralImporter.ImportSpectraFromFile(filenames, path);

            chromophoreDictionary.WriteToJson(Path.Combine(outpath ?? "", outname ?? "SpectralDictionary.txt"));
        }