Commit 836446cb authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixed alignment algorithm

parent 15aa8ee1
Loading
Loading
Loading
Loading
+102 −11
Original line number Diff line number Diff line
@@ -25,6 +25,75 @@ namespace INAF.Apps.Uwp.SLabDataManager.Extensions
                                                                      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(SpectrumModel spectrum,
                                      double xMax,
                                      bool isXMaxHighestBoundary)
        {
            if (!isXMaxHighestBoundary)
            {
                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 = leftSegment.Select(x => x.Y).MeanStandardDeviation();

                /* 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 = rightSegment.Select(x => x.Y).MeanStandardDeviation();

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

                /* 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(SpectrumModel spectrum,
                                      double xMin,
                                      bool isXMinLowestBoundary)
        {
            if (!isXMinLowestBoundary)
            {
                var xMinBoundaryElem = spectrum.Elements.FirstOrDefault(x => x.X == xMin);
@@ -71,25 +140,47 @@ namespace INAF.Apps.Uwp.SLabDataManager.Extensions
                }
            }

            



            return spectrum.Elements.Where(x => x.X >= xMin && x.X <= xMax);
            return xMin;
        }

        public static IEnumerable<ElementModel> GetLeftSegmentElements(this SpectrumModel spectrum,
                                                                       double xMin,
                                                                       double xMax)
                                                                       bool isXMinLowestBoundary,
                                                                       double xMax,
                                                                       bool isXMaxHighestBoundary)
        {
            return spectrum.Elements.Where(x => x.X >= xMin && x.X < xMax);
            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 SpectrumModel spectrum,
                                                                       double xMin,
                                                                       double xMax)
                                                                       bool isXMinLowestBoundary,
                                                                       double xMax,
                                                                       bool isXMaxHighestBoundary)
        {
            return spectrum.Elements.Where(x => x.X > xMin && x.X <= xMax);
            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 SpectrumModel spectrum,
+16 −6
Original line number Diff line number Diff line
@@ -134,15 +134,15 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                    var refLowSeparator = spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue >= selectedRefBand.XMin && x.CurrentValue <= selectedRefBand.XMax);
                    var refHighSeparator = spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue > refLowSeparator.CurrentValue);

                    /* create the ref segment, depending on area selected by user */
                    var refSegment = alignedSpectrum.GetRefSegmentElements(refLowSeparator.CurrentValue,
                                                                           refLowSeparator.CurrentValue == spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault().CurrentValue,
                                                                           getIsXMinLowestBoundary(refLowSeparator.CurrentValue),
                                                                           refHighSeparator.CurrentValue,
                                                                           refHighSeparator.CurrentValue == spectrumAlignmentConfig.WavelengthSeparators.LastOrDefault().CurrentValue);

                                                                           getIsXMaxHighestBoundary(refHighSeparator.CurrentValue));
                    System.Diagnostics.Debug.WriteLine($"refsegment - xmin:{refSegment.FirstOrDefault().X}, xmax:{refSegment.LastOrDefault().X}");
                    double refSegmentLeftMean = refSegment.GetSegmentLeftMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
                    double refSegmentRightMean = refSegment.GetSegmentRightMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);


                    /* retrieve segments on the left of selected one for later alignment */
                    alignLeftSide(ref alignedSpectrum,
                                  refLowSeparator,
@@ -182,7 +182,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            foreach (var leftSeparator in leftSeparators)
            {
                var elements = aligningSpectrum
                                .GetLeftSegmentElements(leftSeparator.CurrentValue, rightSeparatorCurrentValue)
                                .GetLeftSegmentElements(leftSeparator.CurrentValue, getIsXMinLowestBoundary(leftSeparator.CurrentValue), rightSeparatorCurrentValue, getIsXMaxHighestBoundary(rightSeparatorCurrentValue))
                                .FixLeftSegment(refLeftValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

                //aligningSpectrum.UpdateElements(elements); //inutile
@@ -205,7 +205,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            foreach (var rightSeparator in rightSeparators)
            {
                var elements = aligningSpectrum
                                .GetRightSegmentElements(leftSeparatorCurrentValue, rightSeparator.CurrentValue)
                                .GetRightSegmentElements(leftSeparatorCurrentValue, getIsXMinLowestBoundary(leftSeparatorCurrentValue), rightSeparator.CurrentValue, getIsXMaxHighestBoundary(rightSeparator.CurrentValue))
                                .FixRightSegment(refRightValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

                //aligningSpectrum.UpdateElements(elements); //inutile
@@ -215,6 +215,16 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            }
        }

        private bool getIsXMinLowestBoundary(double currentValue)
        {
            return currentValue == spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault().CurrentValue;
        }

        private bool getIsXMaxHighestBoundary(double currentValue)
        {
            return currentValue == spectrumAlignmentConfig.WavelengthSeparators.LastOrDefault().CurrentValue;
        }

        private void multiplyByRef(ref SpectrumModel aligningSpectrum,
                                   double refWhiteMaxY,
                                   SpectrumModel refSpectrum)