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

moved smoothing to whole spectrum to SmoothingHelper to use it in web app

parent db190065
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
  <Identity
    Name="INAF.Apps.Uwp.SLabDataManager"
    Publisher="CN=INAF-IAPS, O=INAF-IAPS, C=IT"
    Version="1.0.38.0" />
    Version="1.0.39.0" />

  <mp:PhoneIdentity PhoneProductId="07F38165-05DE-4ED7-8514-60D1E8CDCBFE" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

+18 −20
Original line number Diff line number Diff line
@@ -113,10 +113,10 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            }
            else
                /* ...otherwise apply smoothing */
                await executeSmoothingBySegmentsAsync(destSpectrumType);
                await applySmoothingBySegmentsAsync(destSpectrumType);
        }

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

@@ -129,6 +129,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                /* add processing step to the list */
                addProcessingStep(destSpectrumType);

                /* set parent file */
                ((ChartSpectrumOfTypeSmoothedModel)result.Spectrum).ParentFiles.ParentFile = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SmoothingViewModel.SelectedSpectrumType).ToSpectrumFileBaseInfo();

                /* save info into spectrum for being used when saving it on cloud */
@@ -161,12 +162,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.AlignedSmoothedBySegments);

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

        #region spectrum
        private async Task applySmoothingToSpectrumPart1Async()
        private async Task applySmoothingToWholeSpectrumProcedureAsync()
        {
            if (SmoothingViewModel.SelectedSpectrumType == SpectrumType.None)
            {
@@ -200,31 +201,28 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            }
            else
                /* ...otherwise apply smoothing */
                await applySmoothingToSpectrumPart2Async(SmoothingViewModel.SelectedSpectrumType, destSpectrumType);
                await applySmoothingToWholeSpectrumAsync(destSpectrumType);
        }

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

            /* clone spectrum for smoothing */
            var spectraFactory = serviceProvider.GetRequiredService<SpectraFactory>();
            var clonedSpectrum = spectraFactory.getClonedSpectrum<ChartSpectrumOfTypeSmoothedModel>(srcType: srcSpectrumType,
                                                                                                    destType: destSpectrumType,
                                                                                                    spectrumLevel: SpectrumLevel.Averaged);

            /* apply smoothing */
            (IChartSpectrumModel smoothedSpectrum, bool isOk) smoothingResult = await SmoothingViewModel.applySmoothingToSpectrum(clonedSpectrum);
            AppProcessingResult result = await SmoothingViewModel.applySmoothingToWholepectrumAsync(destSpectrumType);

            IsLoading = false;

            if (smoothingResult.isOk)
            if (string.IsNullOrEmpty(result.DialogMessage))
            {
                /* add or update smoothed spectrum */
                await WorkingItems.SpectraContainer.addOrUpdateSpectrumAsync(smoothingResult.smoothedSpectrum);
                /* add processing step to the list */
                addProcessingStep(smoothingResult.smoothedSpectrum.Type);
                addProcessingStep(destSpectrumType);

                /* set parent file */
                ((ChartSpectrumOfTypeSmoothedModel)result.Spectrum).ParentFiles.ParentFile = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SmoothingViewModel.SelectedSpectrumType).ToSpectrumFileBaseInfo();

                /* add or update smoothed spectrum */
                await WorkingItems.SpectraContainer.addOrUpdateSpectrumAsync(result.Spectrum);

                /* show successmessage */
                showInfoBar(Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational,
@@ -251,7 +249,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(destSpectrumType);

            /* apply smoothing */
            await applySmoothingToSpectrumPart2Async(SmoothingViewModel.SelectedSpectrumType, destSpectrumType);
            await applySmoothingToWholeSpectrumAsync(destSpectrumType);
        }
        #endregion

@@ -333,7 +331,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        public RelayCommand CommandApplySmoothingBySegments => commandApplySmoothingBySegments ?? (commandApplySmoothingBySegments = new RelayCommand(() => applySmoothingBySegmentsProcedureAsync()));

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

        private RelayCommand commandAskDeleteSmoothingBySegments;
        public RelayCommand CommandAskDeleteSmoothingBySegments => commandAskDeleteSmoothingBySegments ?? (commandAskDeleteSmoothingBySegments = new RelayCommand(() => askDeleteSmoothedSpectrumBySegments()));
+63 −43
Original line number Diff line number Diff line
@@ -217,9 +217,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
        public void setSmoothingSettings(SpectrumType spectrumType,
                                         List<SegmentSmoothingSettingsModel> smoothingSettings)
        {
            //SmoothingSettings = smoothingSettings;
            //raiseAreSmoothingSettingsReady();

            switch (spectrumType)
            {
                case SpectrumType.AlignedSmoothedBySegments:
@@ -249,7 +246,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            /* apply smoothing */
            var smoothingHelper = serviceProvider.GetRequiredService<SmoothingHelper>();
            clonedSpectrum.setPointsFromElements();
            (List<PointModel> smoothedPoints, bool isOk, string exMsg) smoothingResult = await smoothingHelper.applySmoothingBySegments(clonedSpectrum.Points,
            (List<PointModel> smoothedPoints, bool isOk, string exMsg) smoothingResult = await smoothingHelper.applySmoothingBySegmentsAsync(clonedSpectrum.Points,
                                                                                                                                        SmoothingBySegmentsContainer.Segments.ToList());

            if (smoothingResult.isOk)
@@ -260,6 +257,29 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
            else
                return new AppProcessingResult(smoothingResult.exMsg);
        }

        public async Task<AppProcessingResult> applySmoothingToWholepectrumAsync(SpectrumType destSpectrumType)
        {
            /* clone spectrum for smoothing */
            var spectraFactory = serviceProvider.GetRequiredService<SpectraFactory>();
            var clonedSpectrum = spectraFactory.getClonedSpectrum<ChartSpectrumOfTypeSmoothedModel>(srcType: SelectedSpectrumType,
                                                                                                    destType: destSpectrumType,
                                                                                                    spectrumLevel: SpectrumLevel.Averaged);

            /* apply smoothing */
            var smoothingHelper = serviceProvider.GetRequiredService<SmoothingHelper>();
            clonedSpectrum.setPointsFromElements();
            (List<PointModel> smoothedPoints, bool isOk, string exMsg) smoothingResult = await smoothingHelper.applySmoothingToWholeSpectrumAsync(clonedSpectrum.Points,
                                                                                                                                                  SmoothingBySegmentsContainer.Segments.First());

            if (smoothingResult.isOk)
            {
                smoothingResult.smoothedPoints.ForEach(p => clonedSpectrum.addOrUpdateY(p.X, p.Y));
                return new AppProcessingResult(clonedSpectrum);
            }
            else
                return new AppProcessingResult(smoothingResult.exMsg);
        }
        #endregion

        #region GET/SET VALUES ON CLOUD
@@ -414,46 +434,46 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels
        //    return result;
        //}

        public async Task<(IChartSpectrumModel filteredSpectrum, bool isOk)> applySmoothingToSpectrum(IChartSpectrumModel inputSpectrum)
        {
            (IChartSpectrumModel smoothedSpectrum, bool isOk) result = await Task.Run(() =>
            {
                bool isOk = true;
        //public async Task<(IChartSpectrumModel filteredSpectrum, bool isOk)> applySmoothingToSpectrum(IChartSpectrumModel inputSpectrum)
        //{
        //    (IChartSpectrumModel smoothedSpectrum, bool isOk) result = await Task.Run(() =>
        //    {
        //        bool isOk = true;

                try
                {
                    var savitzkyGolayHelper = serviceProvider.GetRequiredService<SavitzkyGolayHelper>();
                    var filteredValues = savitzkyGolayHelper.applySavitzkyGolayFilter(inputSpectrum.Elements.Select(x => x.Y).ToArray(),
                                                                                      SmoothingByWholeSpectrumContainer.Segments.FirstOrDefault().WindowSize,
                                                                                      SmoothingByWholeSpectrumContainer.Segments.FirstOrDefault().PolynomialOrder);
        //        try
        //        {
        //            var savitzkyGolayHelper = serviceProvider.GetRequiredService<SavitzkyGolayHelper>();
        //            var filteredValues = savitzkyGolayHelper.applySavitzkyGolayFilter(inputSpectrum.Elements.Select(x => x.Y).ToArray(),
        //                                                                              SmoothingByWholeSpectrumContainer.Segments.FirstOrDefault().WindowSize,
        //                                                                              SmoothingByWholeSpectrumContainer.Segments.FirstOrDefault().PolynomialOrder);

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

                return (inputSpectrum, isOk);
            });
        //        return (inputSpectrum, isOk);
        //    });

            if (!result.isOk)
                return result;
        //    if (!result.isOk)
        //        return result;

            /* fill spectrum-model with values for smoothing */
            SmoothingSettingsModel smoothingSettings = new SmoothingSettingsModel()
            {
                Segments = getSegments(inputSpectrum.Type)
            };
            ((ChartSpectrumOfTypeSmoothedModel)inputSpectrum).SmoothingSettings = smoothingSettings;
        //    /* fill spectrum-model with values for smoothing */
        //    SmoothingSettingsModel smoothingSettings = new SmoothingSettingsModel()
        //    {
        //        Segments = getSegments(inputSpectrum.Type)
        //    };
        //    ((ChartSpectrumOfTypeSmoothedModel)inputSpectrum).SmoothingSettings = smoothingSettings;

            return result;
        }
        //    return result;
        //}
        #endregion

        #region save