Commit bc3eacff authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixed continuum removal. still need to fix a few errors

parent 836446cb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -139,6 +139,7 @@ namespace INAF.Apps.Uwp.SLabDataManager
                /* transient */
                .AddTransient<AuthenticationManager>()
                .AddTransient<ConfigReader>()
                .AddTransient<ContinuumMissingPointsHelper>()
                .AddTransient<FitFunctionsReader>()
                .AddTransient<LinearFitHelper>()
                .AddTransient<LinearProcessingHelper>()
+7 −2
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers

        public SpectrumAlignmentConfigModel SpectrumAlignmentConfig { get; protected set; }

        public SpectrumType SelectedSpectrumType { get; protected set; }

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

        public async Task tryRemoveSpectrumOfTypeAsync(SpectrumType type)
        public async Task tryRemoveSpectrumOfTypeAsync(SpectrumType spectrumType)
        {
            var requestedSpectrum = tryGetSpectrumOfType(type);
            var requestedSpectrum = tryGetSpectrumOfType(spectrumType);
            if (requestedSpectrum != null)
            {
                int index = -1;
@@ -113,6 +115,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
                    await updateBoundariesAsync();
                }
            }

            SelectedSpectrumType = spectrumType;
            raiseIsAnySpectrumRemoved();
        }
        #endregion

+30 −21
Original line number Diff line number Diff line
@@ -37,21 +37,15 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
        }

        public async Task addOrRemoveTappedPointForContinuumAsync(RadCartesianChart chart,
                                                                  Point mousePosition,
                                                                  Tuple<object, object> point,
                                                                  DataTemplate tappedPointTemplate)
        {
            System.Diagnostics.Debug.WriteLine($"mousePosition: {mousePosition.X}, {mousePosition.Y}");

            try
            {
                var segmentsFitModelContainer = serviceProvider.GetRequiredService<SegmentsFitModelContainer>();

                /* retrieve coord on chart from mouse coords on screen */
                var point = chart.ConvertPointToData(mousePosition);
                System.Diagnostics.Debug.WriteLine($"point: {point.Item1}, {point.Item2}");

                /*  find closest points on spectrum to mouse position */
                var spectrum = workingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.Raw);
                var spectrum = workingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.Aligned);
                (PointModel lowBoundary, PointModel highBoundary) boundaries = getSegmentBoundaries(spectrum,
                                                                                                    (double)point.Item1,
                                                                                                    (double)point.Item2);
@@ -76,8 +70,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                else
                {
                    /* calculate y on spectrum */
                    double yOnSpectrum = (double)point.Item2;
                    if (lowerBoundaryPoint.X != higherBoundaryPoint.X)
                    {
                        var line = new StraightLineModel(lowerBoundaryPoint, higherBoundaryPoint);
                    var yOnSpectrum = line.calculateY((double)point.Item1);
                        yOnSpectrum = line.calculateY((double)point.Item1);
                    }

                    /* create new point to be used for new segment(s) */
                    var pointModel = new PointModel((double)point.Item1, yOnSpectrum);
@@ -98,7 +96,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                    return;

                /* try to create lines connecting selected points */
                addNewSegments(chart);
                await addNewSegmentsAsync(chart);
            }
            catch (Exception ex)
            {
@@ -106,35 +104,46 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
            }
        }

        private void addNewSegments(RadCartesianChart chart)
        private async Task addNewSegmentsAsync(RadCartesianChart chart)
        {
            /* recover points on chart and order them for creating segments */
            var orderedPoints = chart.Annotations
                                        .Where(x => x is CartesianCustomAnnotation)
                                        .Cast<CartesianCustomAnnotation>()
                                        .OrderBy(x => x.HorizontalValue);
            foreach (var point in orderedPoints)
                System.Diagnostics.Debug.WriteLine($"orderedPoint - x,y: {point.HorizontalValue},{point.VerticalValue}");

            /* clear existing segments before creating new ones */
            var segmentsFitModelContainer = serviceProvider.GetRequiredService<SegmentsFitModelContainer>();

            SegmentFitModel newSegment = null;

            /* create segments starting from ordered points */
            var pointsNum = chart.Annotations.Count(x => x is CartesianCustomAnnotation);
            for (int i = 0; i < pointsNum - 1; i++)
            {
                /* try adding new segment, only the new segment is returned, if segment is already existant, then method returns null */
                var testNewSegment = segmentsFitModelContainer.tryAddOrInsertSegment(orderedPoints.ElementAt(i).ToPointModel(), orderedPoints.ElementAt(i + 1).ToPointModel());
                if (testNewSegment != null)
                    newSegment = testNewSegment;
                segmentsFitModelContainer.tryAddOrInsertSegment(orderedPoints.ElementAt(i).ToPointModel(), orderedPoints.ElementAt(i + 1).ToPointModel());
            }

            /* fit new segment(s) */
            var viewModel = serviceProvider.GetRequiredService<ChartViewModel>();
            segmentsFitModelContainer.SegmentsFitModels
                                        .Where(x => !x.IsShown)
                                        .ToList()
                                        .ForEach(async x => await viewModel.fitSelectedSegmentAsync(x));        }

            var newSegments = segmentsFitModelContainer.SegmentsFitModels
                                        .Where(x => !x.IsShown);
            if (newSegments?.Count() > 0)
            {
                int newSegmentsNum = newSegments.Count();
                for (int i = 0; i < newSegmentsNum; i++)
                {
                    System.Diagnostics.Debug.WriteLine($"fit segment {newSegments.ElementAt(i).P1.X}-{newSegments.ElementAt(i).P2.X}");
                    await viewModel.fitSelectedSegmentAsync(newSegments.ElementAt(i));
                }
            }
            //segmentsFitModelContainer.SegmentsFitModels
            //                            .Where(x => !x.IsShown)
            //                            .ToList()
            //                            .ForEach(async x => await viewModel.fitSelectedSegmentAsync(x));
        }

        public void removePointsForContinuum(RadCartesianChart chart)
        {
+90 −0
Original line number Diff line number Diff line
using INAF.Libraries.NetStandard.Math.Models;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
{
    public sealed class ContinuumMissingPointsHelper : ObservableObject
    {
        public ContinuumMissingPointsHelper()
        {
            MissingPoints = new Queue<PointModel>();
            AddedMissingPointsNum = 0;
            MissingPointsNum = 0;
            IsChecking = false;
            isCompleted = false;
        }

        #region properties
        private int addedMissingPointsNum;
        public int AddedMissingPointsNum
        {
            get { return addedMissingPointsNum; }
            set { SetProperty(ref addedMissingPointsNum, value); }
        }

        private bool isCompleted;
        public bool IsCompleted
        {
            get { return isCompleted; }
            set { SetProperty(ref isCompleted, value); }
        }

        public bool IsChecking { get; private set; }

        public Queue<PointModel> MissingPoints { get; private set; }

        public int MissingPointsNum { get; private set; }
        #endregion

        #region methods
        public void addMissingPoint(PointModel missingPoint)
        {
            MissingPoints.Enqueue(missingPoint);
            MissingPointsNum++;
        }

        public PointModel getMissingPoint()
        {
            MissingPoints.TryDequeue(out PointModel missingPoint);
            return missingPoint;
        }

        public void tryIncreaseAddedMissingPointsNum()
        {
            if (IsChecking)
                AddedMissingPointsNum++;
        }

        public void raiseIsCompleted()
        {
            IsCompleted = true;
            isCompleted = false;
        }

        public async void startCheckingAreAllMissingPointsAdded()
        {
            bool continueLoop = true;

            IsChecking = true;

            await Task.Run(async () =>
            {
                while (continueLoop)
                {
                    if (MissingPointsNum > 0 && AddedMissingPointsNum == MissingPointsNum)
                    {
                        IsChecking = false;
                        continueLoop = false;
                    }
                    else
                        await Task.Delay(200);
                }
            });

            raiseIsCompleted();
        }
        #endregion
    }
}
+9 −45
Original line number Diff line number Diff line
@@ -5,16 +5,12 @@ using INAF.Apps.Uwp.SLabDataManager.Models.Fit;
using INAF.Apps.Uwp.SLabDataManager.Models.Processing;
using INAF.Libraries.NetStandard.Math.Fit.Linear;
using INAF.Libraries.NetStandard.Math.Fit.Spline;
using INAF.Libraries.NetStandard.Math.Fit.Spline.Models;
using INAF.Libraries.NetStandard.Math.Models;
using INAF.Libraries.NetStandard.ScienceModels.Spectra;
using INAF.Libraries.Uwp.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;
using SpectrumModel = INAF.Apps.Uwp.SLabDataManager.Models.Spectrum.SpectrumModel;
@@ -58,33 +54,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
        }
        #endregion

        //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 void removeAlignedSpectrum()
        {
            workingItems.SpectraContainer.removeAlignedSpectrum();
        }

        #region alignment methods
        private SpectrumModel cloneSpectrumForAlignment()
        {
@@ -123,12 +93,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                    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);
@@ -163,9 +133,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            if (!string.IsNullOrEmpty(exMsg))
                return new ProcessingResult(exMsg);
            else
                await workingItems.SpectraContainer.addOrUpdateSpectrumAsync(alignedSpectrum);

            return new ProcessingResult();
                return new ProcessingResult(alignedSpectrum);
        }

        private void alignLeftSide(ref SpectrumModel aligningSpectrum,
@@ -185,8 +153,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                                .GetLeftSegmentElements(leftSeparator.CurrentValue, getIsXMinLowestBoundary(leftSeparator.CurrentValue), rightSeparatorCurrentValue, getIsXMaxHighestBoundary(rightSeparatorCurrentValue))
                                .FixLeftSegment(refLeftValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

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

                rightSeparatorCurrentValue = leftSeparator.CurrentValue;
                refLeftValue = elements.GetSegmentLeftMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
            }
@@ -208,8 +174,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
                                .GetRightSegmentElements(leftSeparatorCurrentValue, getIsXMinLowestBoundary(leftSeparatorCurrentValue), rightSeparator.CurrentValue, getIsXMaxHighestBoundary(rightSeparator.CurrentValue))
                                .FixRightSegment(refRightValue, spectrumAlignmentConfig.NumOfPointsForAlignment);

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

                leftSeparatorCurrentValue = rightSeparator.CurrentValue;
                refRightValue = elements.GetSegmentRightMeanValue(spectrumAlignmentConfig.NumOfPointsForAlignment);
            }
@@ -268,7 +232,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            return new ProcessingResult(spectrumForContinuumRemoval, exMsg);
        }

        public async Task<ProcessingResult> removeContinuumAsync(SpectrumModel spectrumForContinuumRemoval,
        public async Task<ProcessingResult> executeContinuumRemovalAsync(SpectrumModel spectrumForContinuumRemoval,
                                                                         SpectrumModel continuumFromFit)
        {
            string exMsg = string.Empty;
Loading