Пример #1
0
        //[Fact]
        public void ScatterBasicTest()
        {
            //Inputs
            uint wavelength          = 660;
            var  distanceToDetector1 = 0.1;
            var  distanceToDetector2 = 0.3;
            var  width              = 0.05;
            var  dSkin              = 0.05;
            var  dMuscle            = 1;
            var  dBone              = 3;
            var  concentrationBlood = 0.150;
            var  ratioOxygen        = 0.9;

            //Expected values (ballpark figures, actually)
            var detectedPhotons1 = 22777800.0;
            var detectedPhotons2 = 90400.0;

            var averageLength1 = 0.08154087172417027;
            var averageLength2 = 0.22765482292591624;

            var data = Scatter.Scatterlight(wavelength, distanceToDetector1, distanceToDetector2, width, dSkin, dMuscle,
                                            dBone, concentrationBlood, ratioOxygen, Directory.GetCurrentDirectory());

            var photonTolerance   = 50000;
            var distanceTolerance = 0.01;

            Assert.True(Math.Abs(detectedPhotons1 - data.DetectedPhotons1) < photonTolerance, "Tolerance was " + Math.Abs(detectedPhotons1 - data.DetectedPhotons1));
            Assert.True(Math.Abs(detectedPhotons2 - data.DetectedPhotons2) < photonTolerance, "Tolerance was " + Math.Abs(detectedPhotons2 - data.DetectedPhotons2));
            Assert.True(Math.Abs(averageLength1 - data.LengthToD1) < distanceTolerance, "Tolerance was " + Math.Abs(averageLength1 - data.LengthToD1));
            Assert.True(Math.Abs(averageLength2 - data.LengthToD2) < distanceTolerance, "Tolerance was " + Math.Abs(averageLength2 - data.LengthToD2));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("ScatterParameter called");

            string name = req.Query["name"];

            uint   wavelength          = Convert.ToUInt32(req.Query["wavelength"]);
            double distanceToDetector1 = Convert.ToDouble(req.Query["distanceTo1"]);
            double distanceToDetector2 = Convert.ToDouble(req.Query["distanceTo2"]);
            double width              = Convert.ToDouble(req.Query["halfWidth"]);
            double thicknessSkin      = Convert.ToDouble(req.Query["thicknessSkin"]);
            double thicknessMuscle    = Convert.ToDouble(req.Query["thicknessMuscle"]);
            double thicknessBone      = Convert.ToDouble(req.Query["thicknessBone"]);
            double concentrationBlood = Convert.ToDouble(req.Query["concentrationBlood"]);
            double ratio              = Convert.ToDouble(req.Query["ratio"]);

            if (req.Method == "POST")
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                var    body        = JsonConvert.DeserializeObject <Core.ScatterParameters>(requestBody);

                try
                {
                    var data = Scatter.Scatterlight(body, context.FunctionAppDirectory);
                    return(new OkObjectResult(data));
                }
                catch (Exception e)
                {
                    log.LogError(e, "Excpetion occured in ScatterLight function");
                    return(new ExceptionResult(e, false));
                }
            }
            else
            {
                try
                {
                    var data = Scatter.Scatterlight(wavelength, distanceToDetector1, distanceToDetector2, width,
                                                    thicknessSkin, thicknessMuscle, thicknessBone, concentrationBlood, ratio, context.FunctionAppDirectory);
                    return(new OkObjectResult(data));
                }
                catch (Exception e)
                {
                    log.LogError(e, "Excpetion occured in ScatterLight function");
                    return(new ExceptionResult(e, false));
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            var absorption             = Coefficients.ObtainAbsorptionCoefficients(660);
            var scatteringCoefficients = Coefficients.ObtainScatteringCoefficients(660);

            var data400 = Scatter.Scatterlight(450, 0.1, 0.3, 0.05, 0.05, 1, 3, 0.150, 0.9);
            var data660 = Scatter.Scatterlight(660, 0.1, 0.3, 0.05, 0.05, 1, 3, 0.150, 0.9);
            var data900 = Scatter.Scatterlight(900, 0.1, 0.3, 0.05, 0.05, 1, 3, 0.150, 0.9);

            var datas = new List <ScatterData>
            {
                data400,
                data660,
                data900
            };
            var wavelengths = new List <int> {
                450, 660, 900
            };

            CalculateRatio(datas, wavelengths);
        }
Пример #4
0
        static void Main(string[] args)
        {
            /* TODO
             * ratio 0-1 15 steps
             * 75 - 200 g/L 20 steps or so
             * wavelength:
             * 450-550
             * 550-590
             * 750-850
             */
            double ratioStart    = 0.7;
            double ratioEnd      = 1;
            double ratioIncrease = 0.1;

            double concentrationStart     = 75;
            double concentrationStop      = 200;
            double concentrationIncrement = 12.5;

            // TODO Add variable that stores the used parameters, or possibly create a dictionary with the parameters on one side and the data on the other side

            var         parameters  = ScatterParameters.GetExampleParameters();
            List <uint> wavelengths = new List <uint>();

            if (args.Length == 0 || args[0] == "-sweep-parameters-better")
            {
                //Set ratio and concentration variables
                //Then call regular sweep-parameters
                //ratioStart = 0.9;
                //concentrationStart = 150;
                if (args.Length == 0)
                {
                    args = new string[1];
                }
                args[0] = "-sweep-parameters";
            }
            if (args[0] == "-sweep-parameters")
            {
                for (uint i = 450; i < 550; i += 10)
                {
                    wavelengths.Add(i);
                }
                for (uint i = 550; i < 590; i += 10)
                {
                    wavelengths.Add(i);
                }

                for (uint i = 750; i < 890; i += 10)
                {
                    wavelengths.Add(i);
                }
            }
            else if (args[0] == "-manual")
            {
                Console.WriteLine("Enter the wavelength");
                wavelengths.Add(Convert.ToUInt32(Console.ReadLine()));
                parameters.Wavelength = wavelengths[0];
                Console.WriteLine("Enter the ratio");
                var ratioInput = Convert.ToDouble(Console.ReadLine());
                ratioStart    = ratioInput;
                ratioEnd      = ratioInput;
                ratioIncrease = 100;
                Console.WriteLine("Enter the concentration");
                var concentrationInput = Convert.ToDouble(Console.ReadLine());
                concentrationStart     = concentrationInput;
                concentrationStop      = concentrationInput;
                concentrationIncrement = 100;
            }
            else
            {
                wavelengths.Add(uint.Parse(args[0]));
            }

            double ratio = 0.0;

            const int numOfSamples = 3;

            DateTime start = DateTime.Now;

            Console.WriteLine("Started at {0:G}", start);

            var infoPath = $"output_data/{start:yyyy-MM-dd--HH-mm-ss}/info.txt";

            Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(infoPath)));
            using (StreamWriter writer = new StreamWriter(infoPath))
            {
                if (args.Length > 0)
                {
                    writer.WriteLine($"Run using parameters: {args[0]}");
                    writer.WriteLine($"Ratio start: {ratioStart}");
                    writer.WriteLine($"Ratio end: {ratioEnd}");
                    writer.WriteLine($"Ratio increment: {ratioIncrease}");

                    writer.WriteLine($"Concentration start: {concentrationStart}");
                    writer.WriteLine($"Concentration end: {concentrationStop}");
                    writer.WriteLine($"Concentration increment: {concentrationIncrement}");

                    writer.WriteLine($"Wavelengths:");
                    foreach (var wavelength in wavelengths)
                    {
                        writer.WriteLine(wavelength);
                    }
                }
                if (args[0] == "-manual")
                {
                    writer.WriteLine("Run using parameters: ");
                    writer.WriteLine($"Wavelength: {wavelengths[0]}");
                    writer.WriteLine($"Ratio: {ratioStart}");
                    writer.WriteLine($"Concentration: {concentrationStart}");
                }
            }

            for (int i = wavelengths.Count - 1; i >= 0; i--)
            {
                Console.WriteLine();
                uint wavelength = wavelengths[i];
                Console.WriteLine($"Wavelength: {wavelength}");
                Console.WriteLine($"Run {wavelengths.Count - i} of {wavelengths.Count}");
                //for (ratio = ratioStart; ratio <= ratioEnd; ratio += ratioIncrease)
                for (ratio = ratioEnd; ratio >= ratioStart; ratio -= ratioIncrease)
                {
                    for (double concentration = concentrationStop; concentration >= concentrationStart; concentration -= concentrationIncrement)
                    {
                        var builder         = new DataBuilder();
                        var detectedPhotons = builder.NewArray <double>(numOfSamples, 2);
                        parameters.ConcentrationBlood = concentration / 1000;
                        parameters.RatioOxygen        = ratio;
                        parameters.Wavelength         = wavelength;

                        for (int n = 0; n < numOfSamples; n++)
                        {
                            var data = Scatter.Scatterlight(parameters);
                            detectedPhotons[n, 0] = data.DetectedPhotons1;
                            detectedPhotons[n, 1] = data.DetectedPhotons2;
                            Console.Write(".");
                        }

                        var variable = builder.NewVariable("detectedPhotons1", detectedPhotons);
                        var matFile  = builder.NewFile(new[] { variable });

                        var path = $"output_data/{start:yyyy-MM-dd--HH-mm-ss}/{wavelength}/{ratio}/{concentration}.mat";
                        Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
                        using (var fileStream = new FileStream(path, FileMode.Create))
                        {
                            var writer = new MatFileWriter(fileStream);
                            writer.Write(matFile);
                        }
                    }
                }
                DateTime tmpEnd       = DateTime.Now;
                TimeSpan tmpSpendTime = new TimeSpan(tmpEnd.Ticks - start.Ticks);

                using (StreamWriter writer = new StreamWriter($"output_data/{start:yyyy-MM-dd--HH-mm-ss}/{wavelength}/time.txt"))
                {
                    writer.WriteLine($"Time spend: {tmpSpendTime}");
                }
            }

            DateTime end       = DateTime.Now;
            TimeSpan spendTime = new TimeSpan(end.Ticks - start.Ticks);

            Console.WriteLine("Stopped at {0:G}", end);
            Console.WriteLine($"Time spend: {spendTime}");
            Console.WriteLine("Done");
            Console.WriteLine("Press any key to close...");
            Console.ReadKey();
        }