Commit 2198c8e9 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

moving code for alignment to INAF.Libraries.NetStandard.Math for being used in web app

parent 9bebb8d3
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ using INAF.Apps.Uwp.SLabDataManager.ViewModels;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.ContentDialogsViewModel;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.Other;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels;
using INAF.Libraries.NetStandard.Math.Alignment;
using INAF.Libraries.NetStandard.Math.Fit.Linear;
using INAF.Libraries.NetStandard.Math.Fit.Spline;
using INAF.Libraries.NetStandard.Math.Smoothing;
@@ -143,6 +144,7 @@ namespace INAF.Apps.Uwp.SLabDataManager
                .AddSingleton<WorkingItemsModel>()
                /* scoped */
                .AddScoped<ActionQuestionViewModel>()
                .AddScoped<AlignmentHelper>()
                .AddScoped<AppConfig>()
                .AddScoped<LegendItemsHelper>()
                .AddScoped<MultipleSelectionViewModel>()
+3 −3
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8" ?>
<logbook>
	<updates>
		<fixes>
		<!--<fixes>
			<fix>Fixed missing warning when saving info</fix>
			<fix>Removed wrong animation when resizing app and Chart page is active with right-panels opened</fix>
			<fix>Fixed smoothing by spectrum</fix>
		</fixes>
		</fixes>-->
		<addings>
			<adding>Added reload button for sample/measurement info panel</adding>
			<adding>Added reading of stdDev for Y values, when available</adding>
		</addings>
	</updates>
</logbook>
 No newline at end of file
+2 −1
Original line number Diff line number Diff line
using CommunityToolkit.Mvvm.DependencyInjection;
using INAF.Apps.Uwp.SLabDataManager.Extensions;
using INAF.Apps.Uwp.SLabDataManager.Helpers;
using INAF.Apps.Uwp.SLabDataManager.Helpers.UI;
using INAF.Apps.Uwp.SLabDataManager.Models;
@@ -15,7 +16,7 @@ using Telerik.Charting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Values.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Converters
{
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Values.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Converters
{
+1 −228
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ using INAF.Libraries.NetStandard.SLabCommonModels.Models.Spectrum.ProcessedSpect
using INAF.Libraries.NetStandard.SLabCommonModels.Models.Spectrum.ProcessedSpectraInfo.Smoothing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
@@ -22,192 +21,13 @@ using Windows.Data.Xml.Dom;
using Windows.UI;
using Windows.UI.Xaml.Media;
using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Values.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Extensions
{
    public static class Extensions
    {
        #region spectrum model
        public static IEnumerable<ElementModel> GetRefSegmentElements(this IChartSpectrumModel spectrum,
                                                                      double xMin,
                                                                      bool isXMinLowestBoundary,
                                                                      double xMax,
                                                                      bool isXMaxHighestBoundary)
        {
            /* if the selected left-separator is NOT the leftmost one, then check which segment the XMIN boundary point belongs to */
            xMin = getXMin(spectrum,
                           xMin,
                           isXMinLowestBoundary);

            /* if the selected right-separator is NOT the rightmost one, then check which segment the XMAX boundary point belongs to */
            xMax = getXMax(spectrum,
                           xMax,
                           isXMaxHighestBoundary);

            /* returns the ref segment */
            return spectrum.Elements.Where(x => x.X >= xMin && x.X <= xMax);
        }

        private static double getXMax(IChartSpectrumModel spectrum,
                                      double xMax,
                                      bool isXMaxHighestBoundary)
        {
            if (!isXMaxHighestBoundary)
            {
                StatsManager statsManager = new StatsManager();

                var xMaxBoundaryElem = spectrum.Elements.FirstOrDefault(x => x.X == xMax);

                /* take points at the left of the boundary point and calculate mean+stddev */
                var leftSegment = spectrum.Elements
                                        .Where(x => x.X < xMaxBoundaryElem.X)
                                        .OrderByDescending(x => x.X)
                                        .Take(10);
                var leftMeanStdDev = statsManager.getMeanAndStdDev(leftSegment.Select(x => x.Y));

                /* take points at the right of the boundary point and calculate mean+stddev */
                var rightSegment = spectrum.Elements
                                        .Where(x => x.X > xMaxBoundaryElem.X)
                                        .Take(10);
                var rightMeanStdDev = statsManager.getMeanAndStdDev(rightSegment.Select(x => x.Y));

                bool testLeft = xMaxBoundaryElem.Y >= leftMeanStdDev.mean && xMaxBoundaryElem.Y <= (leftMeanStdDev.mean + (3d * leftMeanStdDev.stdDev));
                bool testRight = xMaxBoundaryElem.Y <= rightMeanStdDev.mean && xMaxBoundaryElem.Y >= (rightMeanStdDev.mean - (3d * rightMeanStdDev.stdDev));

                /* if testleft is true, then the point belongs to the left segment and it's ok, no action required */
                if (!testLeft)
                {
                    if (testRight)
                    {
                        /* the boundary point belongs to the right segment, so the xMax for the current segment is the 1st before the current xMax */
                        var element = spectrum.Elements.FirstOrDefault(x => x.X < xMax);
                        xMax = element.X;
                    }
                    else
                    {
                        /* the boundary point doesn't belong to any segment...let's detect in a different way */
                        double distLeft = Math.Abs(xMaxBoundaryElem.Y - leftMeanStdDev.mean);
                        double distRight = Math.Abs(xMaxBoundaryElem.Y - rightMeanStdDev.mean);

                        if (distRight < distLeft)
                        {
                            /* the boundary point belongs to the right segment, so the xMax for the current segment is the 1st before the current xMax */
                            var element = spectrum.Elements.FirstOrDefault(x => x.X < xMax);
                            xMax = element.X;
                        }
                    }
                }
            }

            return xMax;
        }

        private static double getXMin(IChartSpectrumModel spectrum,
                                      double xMin,
                                      bool isXMinLowestBoundary)
        {
            if (!isXMinLowestBoundary)
            {
                StatsManager statsManager = new StatsManager();

                var xMinBoundaryElem = spectrum.Elements.FirstOrDefault(x => x.X == xMin);

                /* take points at the left of the boundary point and calculate mean+stddev */
                var leftSegment = spectrum.Elements
                                        .Where(x => x.X < xMinBoundaryElem.X)
                                        .OrderByDescending(x => x.X)
                                        .Take(10);
                var leftMeanStdDev = statsManager.getMeanAndStdDev(leftSegment.Select(x => x.Y));

                /* take points at the right of the boundary point and calculate mean+stddev */
                var rightSegment = spectrum.Elements
                                        .Where(x => x.X > xMinBoundaryElem.X)
                                        .Take(10);
                var rightMeanStdDev = statsManager.getMeanAndStdDev(rightSegment.Select(x => x.Y));


                bool testLeft = xMinBoundaryElem.Y >= leftMeanStdDev.mean && xMinBoundaryElem.Y <= (leftMeanStdDev.mean + (3d * leftMeanStdDev.stdDev));
                bool testRight = xMinBoundaryElem.Y <= rightMeanStdDev.mean && xMinBoundaryElem.Y >= (rightMeanStdDev.mean - (3d * rightMeanStdDev.stdDev));

                /* if testright is true, then the point belongs to the right segment and it's ok, no action required */
                if (!testRight)
                {
                    if (testLeft)
                    {
                        /* the boundary point belongs to the left segment, so the xMin for the current segment is the 1st after the current xMin */
                        var element = spectrum.Elements.FirstOrDefault(x => x.X > xMin);
                        xMin = element.X;
                    }
                    else
                    {
                        /* the boundary point doesn't belong to any segment...let's detect in a different way */
                        double distLeft = Math.Abs(xMinBoundaryElem.Y - leftMeanStdDev.mean);
                        double distRight = Math.Abs(xMinBoundaryElem.Y - rightMeanStdDev.mean);

                        if (distLeft < distRight)
                        {
                            /* the boundary point belongs to the left segment, so the xMin for the current segment is the 1st after the current xMin */
                            var element = spectrum.Elements.FirstOrDefault(x => x.X > xMin);
                            xMin = element.X;
                        }
                    }
                }
            }

            return xMin;
        }

        public static IEnumerable<ElementModel> GetLeftSegmentElements(this IChartSpectrumModel spectrum,
                                                                       double xMin,
                                                                       bool isXMinLowestBoundary,
                                                                       double xMax,
                                                                       bool isXMaxHighestBoundary)
        {
            System.Diagnostics.Debug.WriteLine($"GetLeftSegmentElements - PRIMA xMin:{xMin}, xMax:{xMax}");
            /* if the selected left-separator is NOT the leftmost one, then check which segment the XMIN boundary point belongs to */
            xMin = getXMin(spectrum,
                           xMin,
                           isXMinLowestBoundary);

            /* if the selected right-separator is NOT the rightmost one, then check which segment the XMAX boundary point belongs to */
            xMax = getXMax(spectrum,
                           xMax,
                           isXMaxHighestBoundary);
            System.Diagnostics.Debug.WriteLine($"GetLeftSegmentElements - DOPO xMin:{xMin}, xMax:{xMax}");
            return spectrum.Elements.Where(x => x.X >= xMin && x.X <= xMax);
        }

        public static IEnumerable<ElementModel> GetRightSegmentElements(this IChartSpectrumModel spectrum,
                                                                       double xMin,
                                                                       bool isXMinLowestBoundary,
                                                                       double xMax,
                                                                       bool isXMaxHighestBoundary)
        {
            System.Diagnostics.Debug.WriteLine($"GetRightSegmentElements - PRIMA xMin:{xMin}, xMax:{xMax}");
            /* if the selected left-separator is NOT the leftmost one, then check which segment the XMIN boundary point belongs to */
            xMin = getXMin(spectrum,
                           xMin,
                           isXMinLowestBoundary);

            /* if the selected right-separator is NOT the rightmost one, then check which segment the XMAX boundary point belongs to */
            xMax = getXMax(spectrum,
                           xMax,
                           isXMaxHighestBoundary);
            System.Diagnostics.Debug.WriteLine($"GetRightSegmentElements - DOPO xMin:{xMin}, xMax:{xMax}");
            return spectrum.Elements.Where(x => x.X >= xMin && x.X <= xMax);
        }

        public static void UpdateElements(this IChartSpectrumModel spectrum,
                                          IEnumerable<ElementModel> elements)
        {
            foreach (var element in elements)
            {
                int index = spectrum.Elements.IndexOf(spectrum.Elements.FirstOrDefault(x => x.X == element.X));
                spectrum.Elements.ElementAt(index).updateY(element.Y);
            }
        }

        public static FileSpectrumModel ToSpectrumFile(this IChartSpectrumModel model)
        {
            return new FileSpectrumModel()
@@ -434,53 +254,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Extensions

            return xmlelement;
        }

        public static double GetSegmentLeftMeanValue(this IEnumerable<ElementModel> elements,
                                                     int numOfPoints)
        {
            return elements
                    .Take(numOfPoints)
                    .Average(x => x.Y);
        }

        public static double GetSegmentRightMeanValue(this IEnumerable<ElementModel> elements,
                                                      int numOfPoints)
        {
            return elements
                    .OrderByDescending(x => x.X)
                    .Take(numOfPoints)
                    .Average(x => x.Y);
        }

        public static IEnumerable<ElementModel> FixLeftSegment(this IEnumerable<ElementModel> elements,
                                                               double refLeftValue,
                                                               int numOfPoints)
        {
            double rightValue = elements.OrderByDescending(x => x.X)
                                        .Take(numOfPoints)
                                        .Average(x => x.Y);

            double diff = refLeftValue - rightValue;

            elements.All(x => { x.addToY(diff); return true; });

            return elements;
        }

        public static IEnumerable<ElementModel> FixRightSegment(this IEnumerable<ElementModel> elements,
                                                                double refRightValue,
                                                                int numOfPoints)
        {
            double leftValue = elements.OrderBy(x => x.X)
                                       .Take(numOfPoints)
                                       .Average(x => x.Y);

            double diff = refRightValue - leftValue;

            elements.All(x => { x.addToY(diff); return true; });

            return elements;
        }
        #endregion

        #region settings
Loading