Commit 417f1125 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixed smoothing for aligned, to be tested for continuum-removed

parent cb3d909d
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
using INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels;
using INAF.Apps.Uwp.SLabDataManager.Charts.Smoothing;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels;
using INAF.Libraries.NetStandard.Math.Smoothing;
using INAF.Libraries.NetStandard.ScienceModels.Spectra;
using INAF.Libraries.Uwp.Logging;
@@ -6,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
using Telerik.UI.Xaml.Controls.Input;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
{
@@ -59,9 +61,34 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart.ProcessingHelpers
            });
        }

        //public async Task<(SpectrumModel filteredSpectrum, bool isOk)> applySmoothingToSpectrum(SpectrumModel clonedSpectrum)
        //{
        public async Task<(SpectrumModel filteredSpectrum, bool isOk)> applySmoothingToSpectrum(SpectrumModel clonedSpectrum)
        {
            return await Task.Run(() =>
            {
                bool isOk = true;

        //}
                try
                {
                    SmoothingViewModel smoothingViewModel = serviceProvider.GetRequiredService<SmoothingViewModel>();

                    var filteredValues = savitzkyGolayHelper.applySavitzkyGolayFilter(clonedSpectrum.Elements.Select(x => x.Y).ToArray(),
                                                                                      smoothingViewModel.SpectrumSmoothingBoundariesItem.FirstOrDefault().WindowSize,
                                                                                      smoothingViewModel.SpectrumSmoothingBoundariesItem.FirstOrDefault().PolynomialOrder);

                    int elementsNum = clonedSpectrum.Elements.Count();
                    for (int i = 0; i < elementsNum; i++)
                    {
                        clonedSpectrum.Elements.ElementAt(i).updateY(filteredValues[i]);
                    }
                }
                catch (Exception ex)
                {
                    isOk = false;
                    logger.Write<SmoothingProcessingHelper>($"{nameof(applySmoothingToSpectrum)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
                }

                return (clonedSpectrum, isOk);
            });
        }
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -694,7 +694,7 @@
    <value>Add segment below</value>
  </data>
  <data name="SmoothingBoundariesItemLabel.Text" xml:space="preserve">
    <value>Segment boundaries</value>
    <value>Boundaries</value>
  </data>
  <data name="SmoothingBoundariesItemPolynomialOrderLabel.Text" xml:space="preserve">
    <value>Polynomial order</value>
@@ -702,7 +702,7 @@
  <data name="SmoothingBoundariesItemWindowSizeLabel.Text" xml:space="preserve">
    <value>Window size</value>
  </data>
  <data name="SmoothingBoundariesListView.Header" xml:space="preserve">
  <data name="SmoothingBoundariesForSegmentsListView.Header" xml:space="preserve">
    <value>Boundaries for segments</value>
  </data>
  <data name="SmoothingButtonLabel.Text" xml:space="preserve">
@@ -753,4 +753,7 @@
  <data name="SmoothingPivotItemSmoothingToSpectrum.Header" xml:space="preserve">
    <value>Spectrum</value>
  </data>
  <data name="SmoothingBoundariesForSpectrumListView.Header" xml:space="preserve">
    <value>Boundaries for spectrum</value>
  </data>
</root>
 No newline at end of file
+5 −5
Original line number Diff line number Diff line
@@ -57,13 +57,13 @@
    <SolidColorBrush x:Key="colorraw" Color="#FF319CDB" />
    <SolidColorBrush x:Key="colorref" Color="#FF91C139" />
    <SolidColorBrush x:Key="colorrefwhite" Color="#FFB3D950" />
    <SolidColorBrush x:Key="coloraligned" Color="#FF1872C2" />
    <SolidColorBrush x:Key="colorcontinuum" Color="#FF5E0813" />
    <SolidColorBrush x:Key="coloraligned" Color="#FF1872C2" />
    <SolidColorBrush x:Key="coloralignedsmoothed" Color="#FF50B517" />
    <SolidColorBrush x:Key="coloralignedsmoothedbysegments" Color="#FF3C8E0D" />
    <SolidColorBrush x:Key="colorcontinuumremoved" Color="#FF08365E" />
    <SolidColorBrush x:Key="colorsmoothedaligned" Color="#FF50B517" />
    <SolidColorBrush x:Key="colorsmoothedbysegmentsaligned" Color="#FF3C8E0D" />
    <SolidColorBrush x:Key="colorsmoothedcontinuumremoved" Color="#FF265909" />
    <SolidColorBrush x:Key="colorsmoothedbysegmentscontinuumremoved" Color="#FF173505" />
    <SolidColorBrush x:Key="colorcontinuumremovedsmoothed" Color="#FF265909" />
    <SolidColorBrush x:Key="colorcontinuumremovedsmoothedbysegments" Color="#FF173505" />

    <!-- CHROMELESS BUTTON STYLE -->
    <Style x:Key="ChromelessButtonStyle" TargetType="Button">
+104 −38
Original line number Diff line number Diff line
@@ -1106,6 +1106,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        #endregion

        #region smoothing
        #region by segments
        private async Task applySmoothingBySegmentsPart1Async()
        {
            if (SmoothingViewModel.SelectedSpectrumType == SpectrumType.None)
@@ -1127,6 +1128,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(destSpectrumType))
            {
                /* if dest spectrumType is already present, then ask whether ovewrite it... */
                SelectedActionCommand = CommandOverwriteExistingSmoothedSpectrumBySegments;
                /* bind the right command to the user-control */
                raiseIsActionCommandBindingRequired();
                /* set message and show dialog */
@@ -1135,16 +1137,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            }
            else
                /* ...otherwise apply smoothing */
                await applySmoothingBySegmentsPart2Async();
                await applySmoothingBySegmentsPart2Async(destSpectrumType);
        }

        private async Task applySmoothingBySegmentsPart2Async()
        private async Task applySmoothingBySegmentsPart2Async(SpectrumType destSpectrumType)
        {
            IsLoading = true;

            /* retrieve spectrumType for smoothed spectrum */
            SpectrumType destSpectrumType = getDestSpectrumType(SmoothingViewModel.SelectedSpectrumType);

            /* clone spectrum for smoothing */
            var clonedSpectrum = WorkingItems.SpectraContainer.getClonedSpectrum(SmoothingViewModel.SelectedSpectrumType, destSpectrumType);

@@ -1174,20 +1173,46 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            }
        }

        private void applySmoothingToSpectrumPart1()
        {
            if (SmoothingViewModel.SelectedSpectrumType == SpectrumType.None)
        private async Task deleteSmoothingBySegmentsAsync()
        {
                showDialogMessage("SmoothingNoSrcSpectrumTypeSelectedErrorMessage".GetText());
            if (!WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.AlignedSmoothedBySegments))
                return;

            if (await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.AlignedSmoothedBySegments))
            {
                /* refresh delete button for smoothing by segments */
                SmoothingViewModel.setIsDeleteSmoothingBySegmentsAllowed();

                /* by deleting smoothed spectrum by segments, also smoothed spectrum must be removed, being generated starting from the 'by segments' one */
                deleteSmoothingToSpectrumAsync();
            }
        }

            string smoothedBySegmentsSpectrumTypeString = string.Join(string.Empty, SmoothingViewModel.SelectedSpectrumType.ToString(), "SmoothedBySegments");
            SpectrumType smoothedBySegmentsSpectrumType = smoothedBySegmentsSpectrumTypeString.ToSpectrumType();
        private async Task overwriteExistingSmoothedSpectrumBySegmentsAsync()
        {
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.AlignedSmoothedBySegments))
                await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.AlignedSmoothedBySegments);

            /* retrieve spectrumType for smoothed spectrum */
            SpectrumType destSpectrumType = getDestSpectrumType(SmoothingViewModel.SelectedSpectrumType);

            /* hide dialog question */
            IsActionQuestionVisible = false;

            /* apply smoothing */
            await applySmoothingBySegmentsPart2Async(destSpectrumType);
        }
        #endregion

        #region spectrum
        private async Task applySmoothingToSpectrumPart1Async()
        {
            if (SmoothingViewModel.SelectedSpectrumType == SpectrumType.None)
            {
                showDialogMessage("SmoothingNoSrcSpectrumTypeSelectedErrorMessage".GetText());
                return;
            }

            /* if no starting spectrum is present, then cancel action */
            if (!WorkingItems.SpectraContainer.isAnySpectrumOfType(SmoothingViewModel.SelectedSpectrumType))
            {
@@ -1195,9 +1220,15 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                return;
            }

            SpectrumType smoothedBySegmentsSpectrumType = getSrcSpectrumTypeForSmoothingToSpectrum();

            /* retrieve spectrumType for smoothed spectrum */
            SpectrumType destSpectrumType = getDestSpectrumType(smoothedBySegmentsSpectrumType);

            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(destSpectrumType))
            {
                /* if dest spectrumType is already present, then ask whether ovewrite it... */
                SelectedActionCommand = CommandOverwriteExistingSmoothedSpectrum;
                /* bind the right command to the user-control */
                raiseIsActionCommandBindingRequired();
                /* set message and show dialog */
@@ -1206,47 +1237,79 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            }
            else
                /* ...otherwise apply smoothing */
                applySmoothingToSpectrumPart2();
                await applySmoothingToSpectrumPart2Async(smoothedBySegmentsSpectrumType, destSpectrumType);
        }

        private void applySmoothingToSpectrumPart2()
        private async Task applySmoothingToSpectrumPart2Async(SpectrumType srcSpectrumType,
                                                              SpectrumType destSpectrumType)
        {
            IsLoading = true;

        }
            /* clone spectrum for smoothing */
            var clonedSpectrum = WorkingItems.SpectraContainer.getClonedSpectrum(srcSpectrumType, destSpectrumType);

        private async Task overwriteExistingSmoothedSpectrumAsync()
        {
            /* retrieve spectrumType for smoothed spectrum */
            SpectrumType destSpectrumType = getDestSpectrumType(SmoothingViewModel.SelectedSpectrumType);
            /* apply smoothing */
            SmoothingProcessingHelper smoothingProcessingHelper = serviceProvider.GetRequiredService<SmoothingProcessingHelper>();
            (SpectrumModel filteredSpectrum, bool isOk) smoothingResult = await smoothingProcessingHelper.applySmoothingToSpectrum(clonedSpectrum);

            /* remove spectrum to be overwritten */
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(destSpectrumType))
                await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(destSpectrumType);
            IsLoading = false;

            /* hide actionUserControl (if visible) */
            IsActionQuestionVisible = false;
            if (smoothingResult.isOk)
            {
                /* add or update smoothed spectrum */
                await WorkingItems.SpectraContainer.addOrUpdateSpectrumAsync(clonedSpectrum);

            /* apply smoothing */
            await applySmoothingBySegmentsPart2Async();
                /* refresh delete button for smoothing to spectrum */
                SmoothingViewModel.setIsDeleteSmoothingToSpectrumAllowed();

                /* show successmessage */
                showInfoBar(Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational,
                            "SmoothedSpectrumTitle".GetText(),
                            "SmoothingSuccessfullyExecutedMessage".GetText(),
                            autoHide: true);
            }
            else
            {
                showDialogMessage("SmoothingErrorMessage".GetText());
            }
        }

        private async Task deleteSmoothingBySegmentsAsync()
        private async Task deleteSmoothingToSpectrumAsync()
        {
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.AlignedSmoothedBySegments))
                if (await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.AlignedSmoothedBySegments))
            if (!WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.AlignedSmoothed))
                return;

            if (await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.AlignedSmoothed))
            {
                /* refresh delete button for smoothing by segments */
                    SmoothingViewModel.setIsDeleteSmoothingBySegmentsAllowed();

                    /* by deleting smoothed spectrum by segments, also smoothed spectrum must be removed, being generated starting from the 'by segments' one */
                    deleteSmoothingToSpectrum();
                SmoothingViewModel.setIsDeleteSmoothingToSpectrumAllowed();
            }
        }

        private SpectrumType getSrcSpectrumTypeForSmoothingToSpectrum()
        {
            string smoothedBySegmentsSpectrumTypeString = string.Join(string.Empty, SmoothingViewModel.SelectedSpectrumType.ToString(), "SmoothedBySegments");
            return smoothedBySegmentsSpectrumTypeString.ToSpectrumType();
        }

        private void deleteSmoothingToSpectrum()
        private async Task overwriteExistingSmoothedSpectrumAsync()
        {
            SpectrumType smoothedBySegmentsSpectrumType = getSrcSpectrumTypeForSmoothingToSpectrum();

            /* retrieve spectrumType for smoothed spectrum */
            SpectrumType destSpectrumType = getDestSpectrumType(smoothedBySegmentsSpectrumType);

            /* remove spectrum to be overwritten */
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(destSpectrumType))
                await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(destSpectrumType);

            /* hide actionUserControl (if visible) */
            IsActionQuestionVisible = false;

            /* apply smoothing */
            await applySmoothingToSpectrumPart2Async(smoothedBySegmentsSpectrumType, destSpectrumType);
        }
        #endregion

        private SpectrumType getDestSpectrumType(SpectrumType srcSpectrumType)
        {
@@ -1394,16 +1457,19 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        public RelayCommand CommandApplySmoothingBySegments => commandApplySmoothingBySegments ?? (commandApplySmoothingBySegments = new RelayCommand(() => applySmoothingBySegmentsPart1Async()));

        private RelayCommand commandApplySmoothingToSpectrum;
        public RelayCommand CommandApplySmoothingToSpectrum => commandApplySmoothingToSpectrum ?? (commandApplySmoothingToSpectrum = new RelayCommand(() => applySmoothingToSpectrumPart1()));

        private RelayCommand commandOverwriteExistingSmoothedSpectrum;
        public RelayCommand CommandOverwriteExistingSmoothedSpectrum => commandOverwriteExistingSmoothedSpectrum ?? (commandOverwriteExistingSmoothedSpectrum = new RelayCommand(() => overwriteExistingSmoothedSpectrumAsync()));
        public RelayCommand CommandApplySmoothingToSpectrum => commandApplySmoothingToSpectrum ?? (commandApplySmoothingToSpectrum = new RelayCommand(() => applySmoothingToSpectrumPart1Async()));

        private RelayCommand commandDeleteSmoothingBySegments;
        public RelayCommand CommandDeleteSmoothingBySegments => commandDeleteSmoothingBySegments ?? (commandDeleteSmoothingBySegments = new RelayCommand(() => deleteSmoothingBySegmentsAsync()));

        private RelayCommand commandDeleteSmoothingToSpectrum;
        public RelayCommand CommandDeleteSmoothingToSpectrum => commandDeleteSmoothingToSpectrum ?? (commandDeleteSmoothingToSpectrum = new RelayCommand(() => deleteSmoothingToSpectrum()));
        public RelayCommand CommandDeleteSmoothingToSpectrum => commandDeleteSmoothingToSpectrum ?? (commandDeleteSmoothingToSpectrum = new RelayCommand(() => deleteSmoothingToSpectrumAsync()));

        private RelayCommand commandOverwriteExistingSmoothedSpectrumBySegments;
        public RelayCommand CommandOverwriteExistingSmoothedSpectrumBySegments => commandOverwriteExistingSmoothedSpectrumBySegments ?? (commandOverwriteExistingSmoothedSpectrumBySegments = new RelayCommand(() => overwriteExistingSmoothedSpectrumBySegmentsAsync()));

        private RelayCommand commandOverwriteExistingSmoothedSpectrum;
        public RelayCommand CommandOverwriteExistingSmoothedSpectrum => commandOverwriteExistingSmoothedSpectrum ?? (commandOverwriteExistingSmoothedSpectrum = new RelayCommand(() => overwriteExistingSmoothedSpectrumAsync()));
        #endregion

        #region generic for DeleteDialog
+16 −19
Original line number Diff line number Diff line
@@ -73,8 +73,8 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            set { SetProperty(ref smoothingBoundariesContainer, value); }
        }

        private BoundariesItem spectrumSmoothingBoundariesItem;
        public BoundariesItem SpectrumSmoothingBoundariesItem
        private ObservableCollection<BoundariesItem> spectrumSmoothingBoundariesItem;
        public ObservableCollection<BoundariesItem> SpectrumSmoothingBoundariesItem
        {
            get { return spectrumSmoothingBoundariesItem; }
            private set { SetProperty(ref spectrumSmoothingBoundariesItem, value); }
@@ -92,14 +92,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels

            IsDeleteSmoothingBySegmentsAllowed = false;
            IsDeleteSmoothingToSpectrumAllowed = false;

            if (!tryReadBoundariesItemSettings())
            {
                SmoothingBoundariesContainer = serviceProvider.GetRequiredService<SmoothingBoundariesContainer>();
                SpectrumSmoothingBoundariesItem = new BoundariesItem(SmoothingBoundariesContainer.Boundaries.FirstOrDefault().LowerBoundary,
                                                                     SmoothingBoundariesContainer.Boundaries.LastOrDefault().HigherBoundary,
                                                                     isAddButtonAllowed: false);
            }
        }

        public async Task initAsync()
@@ -113,6 +105,16 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            /* load default boundary values and build-up boundary items */
            SmoothingBoundariesContainer = serviceProvider.GetRequiredService<SmoothingBoundariesContainer>();
            SmoothingBoundariesContainer.buildBoundaries();

            /* build spectrum smoothing parameters from settings or from default values */
            SpectrumSmoothingBoundariesItem = new ObservableCollection<BoundariesItem>();
            if (!tryReadBoundariesItemSettings())
            {
                SpectrumSmoothingBoundariesItem.Add(new BoundariesItem(SmoothingBoundariesContainer.Boundaries.FirstOrDefault().LowerBoundary,
                                                                       SmoothingBoundariesContainer.Boundaries.LastOrDefault().HigherBoundary,
                                                                       isAddButtonAllowed: false));
            }
            SpectrumSmoothingBoundariesItem.All(x => { x.PropertyChanged += X_PropertyChanged; return true; });
        }

        private void initAvailableSpectraTypes()
@@ -164,15 +166,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            settingsHelper.save<BoundariesItem>(nameof(SpectrumSmoothingBoundariesItem), SpectrumSmoothingBoundariesItem);
        }

        public void setSpectrumSmoothingBoundariesItem(double lowerBoundary, double higherBoundary)
        {
            SpectrumSmoothingBoundariesItem = new BoundariesItem(lowerBoundary,
                                                                 higherBoundary,
                                                                 isAddButtonAllowed: false);
            SpectrumSmoothingBoundariesItem.PropertyChanged += (s, e) =>
        private void X_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            saveBoundariesItemSettings();
            };
        }

        private bool tryReadBoundariesItemSettings()
@@ -180,7 +176,8 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            if (!settingsHelper.contains(nameof(SpectrumSmoothingBoundariesItem)))
                return false;

            SpectrumSmoothingBoundariesItem = settingsHelper.get<BoundariesItem>(nameof(SpectrumSmoothingBoundariesItem));
            var item = settingsHelper.get<BoundariesItem>(nameof(SpectrumSmoothingBoundariesItem));
            SpectrumSmoothingBoundariesItem.Add(item);

            return true;
        }
Loading