public string GetPlotData(SolutionDomainPlotParameters plotParameters)
 {
     try
     {
         var inverseSolver      = plotParameters.InverseSolverType;
         var initialGuessParams = _parameterTools.GetParametersInOrder(
             _parameterTools.GetOpticalPropertiesObject(plotParameters.OpticalProperties),
             plotParameters.XAxis.AsEnumerable().ToArray(),
             plotParameters.SolutionDomain,
             plotParameters.IndependentAxes.Label,
             plotParameters.IndependentAxes.Value);
         var initialGuessParamsConvert = initialGuessParams.Values.ToArray();
         // get measured data from inverse solver analysis component
         var measuredPoints  = plotParameters.MeasuredData;
         var dependentValues = measuredPoints.Select(p => p.Last()).ToArray(); // get y value
         var lowerBounds     = new double[] { 0, 0, 0, 0 };
         var upperBounds     = new []
         {
             double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity
         };
         var fit = ComputationFactory.SolveInverse(
             plotParameters.InverseSolverType,
             plotParameters.OptimizerType,
             plotParameters.SolutionDomain,
             dependentValues,
             dependentValues, // set standard deviation to measured to match WPF
             plotParameters.OptimizationParameters,
             initialGuessParamsConvert,
             lowerBounds,
             upperBounds);
         var fitops = ComputationFactory.UnFlattenOpticalProperties(fit);
         //var fitparms =
         //    GetParametersInOrder(fitops, independentValues, sd, independentAxis, independentAxisValue);
         plotParameters.ForwardSolverType = inverseSolver;
         plotParameters.OpticalProperties = fitops[0]; // not sure [0] is always going to work here
         plotParameters.NoiseValue        = 0;
         var msg = _plotFactory.GetPlot(PlotType.SolutionDomain, plotParameters);
         return(msg);
     }
     catch (Exception e)
     {
         _logger.LogError("An error occurred: {Message}", e.Message);
         throw;
     }
 }
        public void Test_get_parameters_in_order_rofrho()
        {
            var postData = "{\"inverseSolverType\":\"PointSourceSDA\",\"optimizerType\":\"MPFitLevenbergMarquardt\",\"optimizationParameters\":\"MuaMusp\",\"solutionDomain\":\"ROfRho\",\"measuredData\":[],\"independentAxes\":{\"label\":\"t\",\"value\":0.05},\"xAxis\":{\"start\":0.5,\"stop\":9.5,\"count\":\"36\"},\"opticalProperties\":{\"mua\":0.01,\"musp\":1,\"g\":0.8,\"n\":1.4}}";
            var solutionDomainPlotParameters = JsonConvert.DeserializeObject <SolutionDomainPlotParameters>(postData);
            var initialGuessParams           = _parameterTools.GetParametersInOrder(
                _parameterTools.GetOpticalPropertiesObject(solutionDomainPlotParameters.OpticalProperties),
                solutionDomainPlotParameters.XAxis.AsEnumerable().ToArray(),
                solutionDomainPlotParameters.SolutionDomain,
                solutionDomainPlotParameters.IndependentAxes.Label,
                solutionDomainPlotParameters.IndependentAxes.Value);
            var test = (OpticalProperties[])initialGuessParams[IndependentVariableAxis.Wavelength];

            Assert.IsNotNull(test);
            Assert.AreEqual(0.01, test[0].Mua);
        }
Example #3
0
        public string Plot(IPlotParameters plotParameters)
        {
            var parameters        = (SolutionDomainPlotParameters)plotParameters;
            var fs                = parameters.ForwardSolverType;
            var op                = parameters.OpticalProperties;
            var independentValue  = parameters.IndependentAxes.Value;
            var independentValues = parameters.XAxis.AsEnumerable().ToArray();

            try
            {
                Plots plot;
                var   parametersInOrder = _parameterTools.GetParametersInOrder(
                    _parameterTools.GetOpticalPropertiesObject(parameters.OpticalProperties),
                    plotParameters.XAxis.AsEnumerable().ToArray(),
                    parameters.SolutionDomain,
                    parameters.IndependentAxes.Label,
                    parameters.IndependentAxes.Value);
                var parametersInOrderObject = parametersInOrder.Values.ToArray();
                var reflectance             = parameters.NoiseValue > 0 ? ComputationFactory.ComputeReflectance(fs, parameters.SolutionDomain, parameters.ModelAnalysis, parametersInOrderObject).AddNoise(parameters.NoiseValue) : ComputationFactory.ComputeReflectance(fs, parameters.SolutionDomain, parameters.ModelAnalysis, parametersInOrderObject);
                var isComplex          = ComputationFactory.IsComplexSolver(parameters.SolutionDomain);
                var hasIndependentAxis = parameters.SolutionDomain != SolutionDomainType.ROfFx && parameters.SolutionDomain != SolutionDomainType.ROfRho;
                if (!isComplex)
                {
                    var xyPoints = independentValues.Zip(reflectance, (x, y) => new Point(x, y));
                    var plotData = new PlotData {
                        Data = xyPoints, Label = parameters.SolutionDomain.ToString()
                    };
                    plot = new Plots {
                        Id       = hasIndependentAxis ? $"{parameters.SolutionDomain.ToString()}Fixed{parameters.IndependentAxes.Label}" : $"{parameters.SolutionDomain.ToString()}",
                        PlotList = new List <PlotDataJson>()
                    };
                    plot.PlotList.Add(new PlotDataJson {
                        Data = plotData.Data.Select(item => new List <double> {
                            item.X, item.Y
                        }).ToList(),
                        Label = hasIndependentAxis ? $"{fs} μa={op.Mua} μs'={op.Musp} {parameters.IndependentAxes.Label}={parameters.IndependentAxes.Value}" : $"{fs} μa={op.Mua} μs'={op.Musp}"
                    });
                }
                else
                {
                    var offset = reflectance.Length / 2;
                    IEnumerable <ComplexPoint> xyPointsComplex = independentValues.Zip(reflectance, (x, y) => new ComplexPoint(x, new Complex(y, reflectance[Array.IndexOf(reflectance, y) + offset]))).ToArray();
                    var xyPointsReal      = xyPointsComplex.Select(item => new Point(item.X, item.Y.Real));
                    var xyPointsImaginary = xyPointsComplex.Select(item => new Point(item.X, item.Y.Imaginary));
                    var plotDataReal      = new PlotData {
                        Data = xyPointsReal, Label = parameters.SolutionDomain.ToString()
                    };
                    var plotDataImaginary = new PlotData {
                        Data = xyPointsImaginary, Label = parameters.SolutionDomain.ToString()
                    };
                    plot = new Plots {
                        Id       = $"{parameters.SolutionDomain.ToString()}Fixed{parameters.IndependentAxes.Label}",
                        PlotList = new List <PlotDataJson>()
                    };
                    plot.PlotList.Add(new PlotDataJson {
                        Data = plotDataReal.Data.Select(item => new List <double> {
                            item.X, item.Y
                        }).ToList(),
                        Label = $"{fs} μa={op.Mua} μs'={op.Musp} {parameters.IndependentAxes.Label}={independentValue}(real)"
                    });
                    plot.PlotList.Add(new PlotDataJson {
                        Data = plotDataImaginary.Data.Select(item => new List <double> {
                            item.X, item.Y
                        }).ToList(),
                        Label = $"{fs} μa={op.Mua} μs'={op.Musp} {parameters.IndependentAxes.Label}={independentValue}(imag)"
                    });
                }
                var msg = JsonConvert.SerializeObject(plot);
                return(msg);
            }
            catch (Exception e)
            {
                _logger.LogError("An error occurred: {Message}", e.Message);
                throw;
            }
        }