Esempio n. 1
0
        /// <summary>
        /// Writes the Chromophore dictionary to separate text files
        /// </summary>
        /// <param name="chromophoreDictionary">The dictionary to write</param>
        public static void WriteDatabaseToFiles(ChromophoreSpectrumDictionary chromophoreDictionary)
        {
            //loop through each of the ChromophoreSpectrum objects
            foreach (var item in chromophoreDictionary)
            {
                var cs = item.Value;
                var cd = new StringBuilder();
                //Get the spectral units
                var units           = SpectralConverter.getSpectralUnit(cs.MolarUnit, cs.AbsorptionCoefficientUnit);
                var wavelengthUnits = SpectralConverter.getWavelengthUnit(cs.WavelengthUnit);

                //write the first line with the header - LAMBDA<space>units<tab>Name<space>units
                cd.AppendLine("%LAMBDA " + wavelengthUnits + "\t" + cs.Name + " " + units);
                var counter = 0;
                foreach (var wavelength in cs.Wavelengths)
                {
                    //need to divide MolarAbsorptionCoefficients by ln(10)
                    var k = 1.0;
                    if (cs.ChromophoreCoefficientType == ChromophoreCoefficientType.MolarAbsorptionCoefficient)
                    {
                        k = Math.Log(10);
                    }
                    var spectrum = cs.Spectrum[counter] / k;
                    cd.AppendLine(wavelength + "\t" + spectrum);
                    counter++;
                }
                //write to text file
                FileIO.WriteToTextFile(cd.ToString(), "absorber-" + cs.Name + ".txt");
            }
        }
Esempio n. 2
0
        public static ChromophoreSpectrumDictionary ToDictionary(this IEnumerable <ChromophoreSpectrum> spectra)
        {
            var dictionary = new ChromophoreSpectrumDictionary();

            foreach (var chromophoreSpectrum in spectra)
            {
                dictionary.Add(chromophoreSpectrum.Name, chromophoreSpectrum);
            }

            return(dictionary);
        }
Esempio n. 3
0
        /// <summary>
        /// Appends a new chromophore spectral dictionary created from a tab-delimited stream onto an existing dictionary of chromophore spectra
        /// </summary>
        /// <param name="existingDictionary">The existing dictionary to which to append</param>
        /// <param name="fileStream">The file stream</param>
        /// <returns>The new dictionary of chromophore spectra</returns>
        public static ChromophoreSpectrumDictionary AppendDatabaseFromFile(ChromophoreSpectrumDictionary existingDictionary, Stream fileStream)
        {
            //create a new dictionary
            var chromophoreSpectra = GetSpectraFromFile(fileStream, true);

            foreach (var item in chromophoreSpectra)
            {
                ChromophoreSpectrum spectrum;
                if (existingDictionary.TryGetValue(item.Name, out spectrum))
                {
                    existingDictionary.Remove(item.Name);
                }
                existingDictionary.Add(item.Name, item);
            }

            return(existingDictionary);
        }
        /// <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);
        }
Esempio n. 5
0
        private ChromophoreSpectrumDictionary CreateDictionary()
        {
            string name = "Melanin";
            AbsorptionCoefficientUnit muaUnit = AbsorptionCoefficientUnit.InverseMillimeters;
            MolarUnit molarUnit = MolarUnit.MicroMolar;
            ChromophoreCoefficientType coeffType = ChromophoreCoefficientType.FractionalAbsorptionCoefficient;

            // populate list of wavelengths
            List <double> wavelengths = new List <double>();

            wavelengths.Add(0.0);
            wavelengths.Add(1.0);
            wavelengths.Add(2.0);

            // populate list of values
            List <double> values = new List <double>();

            values.Add(0.1);
            values.Add(1.1);
            values.Add(2.1);

            ChromophoreSpectrum chromophoreSpectrum = new ChromophoreSpectrum(wavelengths, values, name, coeffType, muaUnit, molarUnit, WavelengthUnit.Nanometers);

            var testDictionary = new ChromophoreSpectrumDictionary();

            testDictionary.Add(chromophoreSpectrum.Name, chromophoreSpectrum);

            name      = "HbO2";
            muaUnit   = AbsorptionCoefficientUnit.InverseMillimeters;
            molarUnit = MolarUnit.MicroMolar;
            coeffType = ChromophoreCoefficientType.MolarAbsorptionCoefficient;

            chromophoreSpectrum = new ChromophoreSpectrum(wavelengths, values, name, coeffType, muaUnit, molarUnit, WavelengthUnit.Nanometers);
            testDictionary.Add(chromophoreSpectrum.Name, chromophoreSpectrum);

            return(testDictionary);
        }
Esempio n. 6
0
 /// <summary>
 /// Saves a given dictionary of Chromophore spectra to the specified file
 /// </summary>
 /// <param name="dictionary">Name of the chromophore spectrum dictionary</param>
 /// <param name="filename">Name of the file</param>
 public static void SaveDatabaseToFile(ChromophoreSpectrumDictionary dictionary, string filename)
 {
     dictionary.WriteToJson(filename);
 }