Commit 15aa8ee1 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixing alignment by finding whether the boundary points belongs to left-segment or right-segment

parent e4829050
Loading
Loading
Loading
Loading
+26 −9
Original line number Diff line number Diff line
@@ -24,13 +24,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers

        public SpectrumAlignmentConfigModel SpectrumAlignmentConfig { get; protected set; }

        public async Task removeSpectrumOfTypeAsync(SpectrumType type)
        {
            var requestedSpectrum = tryGetSpectrumOfType(type);
            Spectra.Remove(requestedSpectrum);
            await updateBoundariesAsync();
        }

        #region spectra
        public override async Task addOrUpdateSpectrumAsync(SpectrumModel spectrum)
        {
@@ -81,10 +74,10 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
            return Spectra.Any(x => x.Type == spectrumType);
        }

        /* wrapper of 'tryRemoveSpectrumOfTypeAsync' method for aligned spectrum */
        public void removeAlignedSpectrum()
        {
            removeSpectrumOfTypeAsync(SpectrumType.Aligned);
            IsSpectrumAligned = false;
            tryRemoveSpectrumOfTypeAsync(SpectrumType.Aligned);
            IsAlignedSpectrumSaved = false;
        }

@@ -97,6 +90,30 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
        {
            return Spectra.FirstOrDefault(x => x.Type == spectrumType);
        }

        public async Task tryRemoveSpectrumOfTypeAsync(SpectrumType type)
        {
            var requestedSpectrum = tryGetSpectrumOfType(type);
            if (requestedSpectrum != null)
            {
                int index = -1;
                int spectraNum = spectra.Count;
                for (int i = 0; i < spectraNum; i++)
                {
                    if (spectra[i].Type == requestedSpectrum.Type)
                    {
                        index = i;
                        break;
                    }
                }

                if (index != -1)
                {
                    Spectra.RemoveAt(index);
                    await updateBoundariesAsync();
                }
            }
        }
        #endregion

        public void clear()
+0 −8
Original line number Diff line number Diff line
@@ -75,13 +75,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
            }
        }

        protected bool isSpectrumAligned;
        public bool IsSpectrumAligned
        {
            get { return isSpectrumAligned; }
            set { SetProperty(ref isSpectrumAligned, value); }
        }

        protected bool isSpectrumContinuumRemoved;
        public bool IsSpectrumContinuumRemoved
        {
@@ -193,7 +186,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
        {
            IsAlignedSpectrumSaved = false;
            IsAnySpectrumLoaded = false;
            IsSpectrumAligned = false;
            IsChartUpdateLayoutRequired = false;

            XAxisMajorStepValues = new List<double>(2);
+7 −6
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ using SpectrumModel = INAF.Apps.Uwp.SLabDataManager.Models.Spectrum.SpectrumMode

namespace INAF.Apps.Uwp.SLabDataManager.Converters
{
    public sealed class IsSpectrumLoadedConverter : IValueConverter
    public sealed class IsAnySpectrumOfTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
@@ -19,11 +19,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters
                SpectrumType spectrumType = _spectrumType.ToSpectrumType();

                SpectraContainer spectraContainer = Ioc.Default.GetService<WorkingItemsModel>().SpectraContainer;
                SpectrumModel spectrum = spectraContainer.tryGetSpectrumOfType(spectrumType);
                if (spectrum != null)
                    return true;
                else
                    return false;
                return spectraContainer.isAnySpectrumOfType(spectrumType);
                //SpectrumModel spectrum = spectraContainer.tryGetSpectrumOfType(spectrumType);
                //if (spectrum != null)
                //    return true;
                //else
                //    return false;
            }
            catch (Exception)
            {
+58 −4
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
using INAF.Libraries.NetStandard.Math.Models;
using INAF.Libraries.NetStandard.ScienceModels.Spectra;
using INAF.Libraries.NetStandard.SLabCommonModels.Models.Files;
using MathNet.Numerics.Statistics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -17,10 +18,63 @@ namespace INAF.Apps.Uwp.SLabDataManager.Extensions
    public static class Extensions
    {
        #region spectrum model
        public static IEnumerable<ElementModel> GetCenterSegmentElements(this SpectrumModel spectrum,
        public static IEnumerable<ElementModel> GetRefSegmentElements(this SpectrumModel spectrum,
                                                                      double xMin,
                                                                       double xMax)
                                                                      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 */
            if (!isXMinLowestBoundary)
            {
                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 = 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 > xMinBoundaryElem.X)
                                        .Take(10);
                var rightMeanStdDev = rightSegment.Select(x => x.Y).MeanStandardDeviation();


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

                /* 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 spectrum.Elements.Where(x => x.X >= xMin && x.X <= xMax);
        }

+56 −68
Original line number Diff line number Diff line
@@ -58,27 +58,27 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
        }
        #endregion

        private void raiseIsDialogRequired()
        {
            IsDialogRequired = true;
            isDialogRequired = false;
        }
        //private void raiseIsDialogRequired()
        //{
        //    IsDialogRequired = true;
        //    isDialogRequired = false;
        //}

        #region alignment
        public async Task alignSpectrumProcedureAsync()
        {
            string exMsg = await alignRawSpectrumAsync();
            if (!string.IsNullOrEmpty(exMsg))
            {
                UpdateUIHelper.UpdateUIAsync(() =>
                {
                    DialogMessageType = DialogMessageType.Warning;
                    DialogMessage = exMsg;
                    raiseIsDialogRequired();
                });
                return;
            }
        }
        //public async Task alignSpectrumProcedureAsync()
        //{
        //    string exMsg = await alignRawSpectrumAsync();
        //    if (!string.IsNullOrEmpty(exMsg))
        //    {
        //        UpdateUIHelper.UpdateUIAsync(() =>
        //        {
        //            DialogMessageType = DialogMessageType.Warning;
        //            DialogMessage = exMsg;
        //            raiseIsDialogRequired();
        //        });
        //        return;
        //    }
        //}

        public void removeAlignedSpectrum()
        {
@@ -94,7 +94,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            return aligningSpectrum;
        }

        private async Task<string> alignRawSpectrumAsync()
        public async Task<ProcessingResult> alignRawSpectrumAsync()
        {
            IsDialogRequired = false;
            DialogMessage = string.Empty;
@@ -102,52 +102,42 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            SpectrumModel alignedSpectrum = null;

            string exMsg = string.Empty;
            await Task.Run(() =>
            {
                try
                {
                    alignedSpectrum = cloneSpectrumForAlignment();

            /* try to retrieve the ref white max value for normalizing raw spectrum during alignment process */
            double refWhiteMaxY = 1d;
            var refWhiteSpectrum = workingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.RefWhite);
            if (refWhiteSpectrum == null)
                    {
                        UpdateUIHelper.UpdateUIAsync(() =>
                        {
                            DialogMessageType = DialogMessageType.Warning;
                            DialogMessage = "RefWhiteSpectrumNotAvailable".GetText();
                            raiseIsDialogRequired();
                        });
                    }
                return new ProcessingResult("RefWhiteSpectrumNotAvailable".GetText(), DialogMessageType.Warning);
            else
                refWhiteMaxY = refWhiteSpectrum.Elements.Max(x => x.Y);

            /* try to retrieve ref spectrum for normalizing raw spectrum during alignment process */
            var refSpectrum = workingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.Ref);
            if (refSpectrum == null)
                return new ProcessingResult("NoNormalizationAvailableMessage".GetText(), DialogMessageType.Warning);

            await Task.Run(() =>
            {
                        UpdateUIHelper.UpdateUIAsync(() =>
                try
                {
                            DialogMessageType = DialogMessageType.Warning;
                            DialogMessage = "NoNormalizationAvailableMessage".GetText();
                            raiseIsDialogRequired();
                        });
                    }
                    alignedSpectrum = cloneSpectrumForAlignment();

                    /* normalize spectrum, if possible */
                    if (refWhiteSpectrum != null && refSpectrum != null)
                    {
                        multiplyByRef(ref alignedSpectrum,
                                      refWhiteMaxY,
                                      refSpectrum);
                    }
                    //if (refWhiteSpectrum != null && refSpectrum != null)
                    //{
                    //    multiplyByRef(ref alignedSpectrum,
                    //                  refWhiteMaxY,
                    //                  refSpectrum);
                    //}

                    /* retrieve the reference segment and calculate the mean values at the borders of the segment for aligning other segments */
                    var refLowSeparator = spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue >= selectedRefBand.XMin && x.CurrentValue <= selectedRefBand.XMax);
                    var refHighSeparator = spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue > refLowSeparator.CurrentValue);

                    var refSegment = alignedSpectrum.GetCenterSegmentElements(refLowSeparator.CurrentValue, refHighSeparator.CurrentValue);
                    var refSegment = alignedSpectrum.GetRefSegmentElements(refLowSeparator.CurrentValue,
                                                                           refLowSeparator.CurrentValue == spectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault().CurrentValue,
                                                                           refHighSeparator.CurrentValue,
                                                                           refHighSeparator.CurrentValue == spectrumAlignmentConfig.WavelengthSeparators.LastOrDefault().CurrentValue);

                    double refSegmentLeftMean = refSegment.GetSegmentLeftMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
                    double refSegmentRightMean = refSegment.GetSegmentRightMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
@@ -170,18 +160,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                }
            });

            if (string.IsNullOrEmpty(exMsg))
            {
                workingItems.SpectraContainer.IsSpectrumAligned = true;
                await workingItems.SpectraContainer.addOrUpdateSpectrumAsync(alignedSpectrum);
            }
            if (!string.IsNullOrEmpty(exMsg))
                return new ProcessingResult(exMsg);
            else
            {
                DialogMessage = exMsg;
                raiseIsDialogRequired();
            }
                await workingItems.SpectraContainer.addOrUpdateSpectrumAsync(alignedSpectrum);

            return exMsg;
            return new ProcessingResult();
        }

        private void alignLeftSide(ref SpectrumModel aligningSpectrum,
@@ -191,16 +175,17 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            var leftSeparators = spectrumAlignmentConfig.WavelengthSeparators
                                                               .Where(x => x.MinValue < refLowSeparator.MinValue)
                                                               .OrderByDescending(x => x.MinValue);

            double rightSeparatorCurrentValue = refLowSeparator.CurrentValue;
            double refLeftValue = refSegmentLeftMean;

            foreach (var leftSeparator in leftSeparators)
            {
                var elements = aligningSpectrum
                                .GetLeftSegmentElements(leftSeparator.CurrentValue, rightSeparatorCurrentValue)
                                .FixLeftSegment(refLeftValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

                aligningSpectrum.UpdateElements(elements);

                //aligningSpectrum.UpdateElements(elements); //inutile

                rightSeparatorCurrentValue = leftSeparator.CurrentValue;
                refLeftValue = elements.GetSegmentLeftMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
@@ -211,16 +196,19 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                                    WavelengthModel refHighSeparator,
                                    double refSegmentRightMean)
        {
            var rightSeparators = spectrumAlignmentConfig.WavelengthSeparators.Where(x => x.MinValue > refHighSeparator.MinValue);
            var rightSeparators = spectrumAlignmentConfig.WavelengthSeparators
                                                            .Where(x => x.MinValue > refHighSeparator.MinValue);

            double leftSeparatorCurrentValue = refHighSeparator.CurrentValue;
            double refRightValue = refSegmentRightMean;

            foreach (var rightSeparator in rightSeparators)
            {
                var elements = aligningSpectrum
                                .GetRightSegmentElements(leftSeparatorCurrentValue, rightSeparator.CurrentValue)
                                .FixRightSegment(refRightValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

                aligningSpectrum.UpdateElements(elements);
                //aligningSpectrum.UpdateElements(elements); //inutile

                leftSeparatorCurrentValue = rightSeparator.CurrentValue;
                refRightValue = elements.GetSegmentRightMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
Loading