Exemplo n.º 1
0
        private static void ReportInverseSolverROfRhoAndTime(double dt,
                                                             double riseMarker,
                                                             double tailMarker,
                                                             string stDevMode,
                                                             InverseFitType IFT,
                                                             string projectName,
                                                             string inputPath,
                                                             ForwardSolverType[] forwardSolverTypes,
                                                             OptimizerType[] optimizerTypes,
                                                             IEnumerable <OpticalProperties> guessOps,
                                                             IEnumerable <OpticalProperties> realOps,
                                                             double[] rhos,
                                                             double noisePercentage,
                                                             bool stepByStep)
        {
            Console.WriteLine("#############################################");
            Console.WriteLine("##### REPORT INVERSE SOLVER: ROfRhoAndT #####");
            Console.WriteLine("#############################################");
            //path definition
            string spaceDomainFolder = "Real";
            string timeDomainFolder  = "TimeDomain";
            string noiseFolder       = "noise" + noisePercentage.ToString();
            string problemFolder     = "dt" + (dt * 1000).ToString() + "markers" + riseMarker.ToString() +
                                       tailMarker.ToString();

            problemFolder = problemFolder.Replace(".", "p");

            foreach (var fST in forwardSolverTypes)
            {
                //initialize forward solver
                Console.WriteLine("Forward Solver Type: {0}", fST.ToString());
                foreach (var oT in optimizerTypes)
                {
                    Console.WriteLine("Optimizer Type: {0}", oT.ToString());
                    foreach (var rho in rhos)
                    {
                        string rhoFolder = rho.ToString();
                        Console.WriteLine("=================================================");
                        Console.WriteLine("SOURCE DETECTOR SEPARETION: R = {0} mm", rhoFolder);
                        if (stepByStep)
                        {
                            Console.WriteLine("Press enter to continue");
                        }
                        Console.WriteLine("=================================================");
                        if (stepByStep)
                        {
                            Console.ReadLine();
                        }
                        rhoFolder = rhoFolder.Replace(".", "p");
                        rhoFolder = "rho" + rhoFolder;
                        double[] constantVals = { rho };

                        foreach (var rOp in realOps)
                        {
                            //output
                            double   bestMua        = 0.0;
                            double   meanMua        = 0.0;
                            double   guessBestMua   = 0.0;
                            double   bestMusp       = 0.0;
                            double   meanMusp       = 0.0;
                            double   guessBestMusp  = 0.0;
                            double   bestChiSquared = 10000000000000.0; //initialize very large to avoid if first
                            double   meanChiSquared = 0.0;
                            DateTime start          = new DateTime();   //processing start time
                            DateTime end            = new DateTime();   //processing finish time
                            double   elapsedSeconds;                    //processing time

                            //set filename based on real optical properties
                            var filename = "musp" + rOp.Musp.ToString() + "mua" + rOp.Mua.ToString();
                            filename = filename.Replace(".", "p");
                            Console.WriteLine("Looking for file {0}", filename);

                            if (File.Exists(inputPath + spaceDomainFolder + "/" + timeDomainFolder + "/" + problemFolder + "/" + rhoFolder + "/" + filename + "Range"))
                            {
                                Console.WriteLine("The file has been found for rho = {0} mm.", rho);
                                //read binary files
                                var timeRange = (double[])FileIO.ReadArrayFromBinaryInResources <double>
                                                    ("Resources/" + spaceDomainFolder + "/" + timeDomainFolder + "/" + problemFolder + "/" + rhoFolder + "/" + filename + "Range", projectName, 2);
                                int numberOfPoints = Convert.ToInt32((timeRange[1] - timeRange[0]) / dt) + 1;
                                var T = new DoubleRange(timeRange[0], timeRange[1], numberOfPoints).AsEnumerable().ToArray();
                                var R = (double[])FileIO.ReadArrayFromBinaryInResources <double>
                                            ("Resources/" + spaceDomainFolder + "/" + timeDomainFolder + "/" + problemFolder + "/" + rhoFolder + "/" + filename + "R", projectName, numberOfPoints);
                                var S = GetStandardDeviationValues("Resources/" + spaceDomainFolder + "/" + timeDomainFolder + "/" + problemFolder + "/" + rhoFolder + "/" + filename + "S",
                                                                   projectName, stDevMode, numberOfPoints, R.ToArray());
                                // add noise
                                if (noisePercentage != 0.0)
                                {
                                    R = R.AddNoise(noisePercentage);
                                }
                                start = DateTime.Now;
                                int convergedCounter = 0;
                                foreach (var gOp in guessOps)
                                {
                                    bool converged;
                                    if (IFT == InverseFitType.Mua)
                                    {
                                        gOp.Musp = rOp.Musp;
                                    }
                                    if (IFT == InverseFitType.Musp)
                                    {
                                        gOp.Mua = rOp.Mua;
                                    }
                                    //solve inverse problem
                                    double[] fit = ComputationFactory.SolveInverse(fST, oT, SolutionDomainType.ROfRhoAndTime, R, S, IFT, new object[] { new[] { gOp }, constantVals, T });
                                    if (fit[0] != 0 && fit[1] != 0)
                                    {
                                        converged = true;
                                    }
                                    else
                                    {
                                        converged = false;
                                    }
                                    if (converged)
                                    {
                                        OpticalProperties fOp = new OpticalProperties(fit[0], fit[1], gOp.G, gOp.N);
                                        //calculate chi squared and change best values if it improved
                                        double chiSquared = EvaluateChiSquared(R.ToArray(), SolverFactory.GetForwardSolver(fST).ROfRhoAndTime(fOp.AsEnumerable(), rho.AsEnumerable(), T).ToArray(), S.ToArray());
                                        if (chiSquared < bestChiSquared)
                                        {
                                            guessBestMua   = gOp.Mua;
                                            bestMua        = fit[0];
                                            guessBestMusp  = gOp.Musp;
                                            bestMusp       = fit[1];
                                            bestChiSquared = chiSquared;
                                        }
                                        meanMua          += fit[0];
                                        meanMusp         += fit[1];
                                        meanChiSquared   += chiSquared;
                                        convergedCounter += 1;
                                    }
                                }
                                end             = DateTime.Now;
                                meanMua        /= convergedCounter;
                                meanMusp       /= convergedCounter;
                                meanChiSquared /= convergedCounter;
                                elapsedSeconds  = (end - start).TotalSeconds;

                                MakeDirectoryIfNonExistent(new string[] { spaceDomainFolder, timeDomainFolder, noiseFolder, problemFolder, fST.ToString(), oT.ToString(), IFT.ToString(), rhoFolder });
                                //write results to array
                                double[] inverseProblemValues = FillInverseSolverValuesArray(bestMua, meanMua, guessBestMua,
                                                                                             bestMusp, meanMusp, guessBestMusp,
                                                                                             bestChiSquared, meanChiSquared,
                                                                                             elapsedSeconds, numberOfPoints);
                                // write array to binary
                                LocalWriteArrayToBinary(inverseProblemValues, @"Output/" + spaceDomainFolder + "/" +
                                                        timeDomainFolder + "/" + noiseFolder + "/" + problemFolder + "/" + fST.ToString() + "/" +
                                                        oT.ToString() + "/" + IFT.ToString() + "/" + rhoFolder + "/" + filename, FileMode.Create);

                                Console.WriteLine("Real MUA = {0} - best MUA = {1} - mean MUA = {2}", rOp.Mua, bestMua, meanMua);
                                Console.WriteLine("Real MUSp = {0} - best MUSp = {1} - mean MUSp = {2}", rOp.Musp, bestMusp, meanMusp);
                                if (stepByStep)
                                {
                                    Console.ReadLine();
                                }
                            }
                            else
                            {
                                Console.WriteLine("The file has not been found.");
                            }

                            Console.Clear();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void ReportInverseSolverROfRho(double drho,
                                                      double[] rhoRange,
                                                      InverseFitType IFT,
                                                      string projectName,
                                                      string inputPath,
                                                      ForwardSolverType[] forwardSolverTypes,
                                                      OptimizerType[] optimizerTypes,
                                                      IEnumerable <OpticalProperties> guessOps,
                                                      IEnumerable <OpticalProperties> realOps,
                                                      int ratioDetectors,
                                                      double noisePercentage,
                                                      bool stepByStep)
        {
            Console.WriteLine("#############################################");
            Console.WriteLine("####### REPORT INVERSE SOLVER: ROfRho #######");
            Console.WriteLine("#############################################");
            //path definition
            string spaceDomainFolder = "Real";
            string timeDomainFolder  = "SteadyState";
            string problemFolder     = "drho" + drho.ToString() + "/" + "ratioD" + ratioDetectors.ToString() + "/" +
                                       "noise" + noisePercentage.ToString() + "/" + rhoRange[0].ToString() + "_" + rhoRange[1].ToString();

            problemFolder = problemFolder.Replace(".", "p");
            //rhos based on range
            int numberOfPoints = Convert.ToInt32((rhoRange[1] - rhoRange[0]) / drho) + 1;
            var rhos           = new DoubleRange(rhoRange[0], rhoRange[1], numberOfPoints).AsEnumerable().ToArray();

            double[] R = new double[numberOfPoints];
            double[] S = new double[numberOfPoints];
            //based on range evaluate the index of first and last points to use
            int firstInd = Convert.ToInt32((rhoRange[0] + drho / 2.0) / drho) - 1;
            int lastInd  = Convert.ToInt32((rhoRange[1] + drho / 2) / drho) - 1;

            //execute
            foreach (var fST in forwardSolverTypes)
            {
                Console.WriteLine("Forward Solver Type: {0}", fST.ToString());
                foreach (var oT in optimizerTypes)
                {
                    Console.WriteLine("Optimizer Type: {0}", oT.ToString());
                    if (stepByStep)
                    {
                        Console.WriteLine("Press enter to continue");
                    }
                    Console.WriteLine("=================================================");
                    if (stepByStep)
                    {
                        Console.ReadLine();
                    }

                    foreach (var rOp in realOps)
                    {
                        //output
                        double   bestMua        = 0.0;
                        double   meanMua        = 0.0;
                        double   guessBestMua   = 0.0;
                        double   bestMusp       = 0.0;
                        double   meanMusp       = 0.0;
                        double   guessBestMusp  = 0.0;
                        double   bestChiSquared = 10000000000000.0; //initialize very large to avoid if first
                        double   meanChiSquared = 0.0;
                        DateTime start          = new DateTime();   //processing start time
                        DateTime end            = new DateTime();   //processing finish time
                        double   elapsedSeconds;                    //processing time

                        //set filename based on real optical properties
                        var filename = "musp" + rOp.Musp.ToString() + "mua" + rOp.Mua.ToString();
                        filename = filename.Replace(".", "p");
                        Console.WriteLine("Looking for file {0}", filename);

                        if (File.Exists(inputPath + spaceDomainFolder + "/" + timeDomainFolder + "/" + filename + "R"))
                        {
                            Console.WriteLine("The file has been found");
                            //read binary files
                            var Rtot = (IEnumerable <double>)FileIO.ReadArrayFromBinaryInResources <double>
                                           ("Resources/" + spaceDomainFolder + "/" + timeDomainFolder + "/" + filename + "R", projectName, 88);
                            var Stot = (IEnumerable <double>)FileIO.ReadArrayFromBinaryInResources <double>
                                           ("Resources/" + spaceDomainFolder + "/" + timeDomainFolder + "/" + filename + "S", projectName, 88);
                            // extract points within range
                            for (int i = firstInd; i <= lastInd; i++)
                            {
                                R[i - firstInd] = Rtot.ToArray()[i];
                                S[i - firstInd] = Stot.ToArray()[i];
                            }
                            // reduce number of measurements
                            var mrhos = FilterArray(rhos, ratioDetectors);
                            var mR    = FilterArray(R, ratioDetectors);
                            var mS    = FilterArray(S, ratioDetectors);
                            // add noise
                            if (noisePercentage != 0.0)
                            {
                                mR.AddNoise(noisePercentage);
                            }
                            start = DateTime.Now;
                            int covergedCounter = 0;
                            foreach (var gOp in guessOps)
                            {
                                bool converged;
                                //if fitting only one parameter change the guess to the true value
                                if (IFT == InverseFitType.Mua)
                                {
                                    gOp.Musp = rOp.Musp;
                                }
                                if (IFT == InverseFitType.Musp)
                                {
                                    gOp.Mua = rOp.Mua;
                                }
                                //solve inverse problem
                                double[] fit = ComputationFactory.SolveInverse(fST, oT, SolutionDomainType.ROfRho, mR, mS, IFT, new object[] { new[] { gOp }, mrhos });
                                if (fit[0] != 0 && fit[1] != 0)
                                {
                                    converged = true;
                                }
                                else
                                {
                                    converged = false;
                                }
                                // fitted op
                                if (converged)
                                {
                                    OpticalProperties fOp = new OpticalProperties(fit[0], fit[1], gOp.G, gOp.N);
                                    //calculate chi squared and change values if it improved
                                    double chiSquared = EvaluateChiSquared(mR, SolverFactory.GetForwardSolver(fST).ROfRho(fOp.AsEnumerable(), mrhos).ToArray(), mS);
                                    if (chiSquared < bestChiSquared)
                                    {
                                        guessBestMua   = gOp.Mua;
                                        bestMua        = fit[0];
                                        guessBestMusp  = gOp.Musp;
                                        bestMusp       = fit[1];
                                        bestChiSquared = chiSquared;
                                    }
                                    meanMua         += fit[0];
                                    meanMusp        += fit[1];
                                    meanChiSquared  += chiSquared;
                                    covergedCounter += 1;
                                }
                            }
                            end             = DateTime.Now;
                            meanMua        /= covergedCounter;
                            meanMusp       /= covergedCounter;
                            meanChiSquared /= covergedCounter;
                            elapsedSeconds  = (end - start).TotalSeconds;

                            MakeDirectoryIfNonExistent(new string[] { spaceDomainFolder, timeDomainFolder, problemFolder, fST.ToString(), oT.ToString(), IFT.ToString() });
                            //write results to array
                            double[] inverseProblemValues = FillInverseSolverValuesArray(bestMua, meanMua, guessBestMua,
                                                                                         bestMusp, meanMusp, guessBestMusp,
                                                                                         bestChiSquared, meanChiSquared,
                                                                                         elapsedSeconds, mR.Count());
                            // write array to binary
                            LocalWriteArrayToBinary(inverseProblemValues, @"Output/" + spaceDomainFolder + "/" +
                                                    timeDomainFolder + "/" + problemFolder + "/" + fST.ToString() + "/" +
                                                    oT.ToString() + "/" + IFT.ToString() + "/" + filename, FileMode.Create);

                            Console.WriteLine("Real MUA = {0} - best MUA = {1} - mean MUA = {2}", rOp.Mua, bestMua, meanMua);
                            Console.WriteLine("Real MUSp = {0} - best MUSp = {1} - mean MUSp = {2}", rOp.Musp, bestMusp, meanMusp);
                            if (stepByStep)
                            {
                                Console.ReadLine();
                            }
                        }
                        else
                        {
                            Console.WriteLine("The file has not been found.");
                        }

                        Console.Clear();
                    }
                }
            }
        }