Esempio n. 1
0
        internal DesignDigestsViewModel(IAppService appService, ModuleState moduleState, EstimationDesigns estimationDesigns)
        {
            _moduleState       = moduleState;
            _estimationDesigns = estimationDesigns;

            LoadEstimationDesign = ReactiveCommand.Create(
                HandleLoadEstimationDesign,
                this.WhenAny(vm => vm.SelectedDesignDigestViewModel, _ => SelectedDesignDigestViewModel != default)
                );

            DeleteEstimationDesign = ReactiveCommand.Create(
                HandleDeleteEstimationDesign,
                this.WhenAny(vm => vm.SelectedDesignDigestViewModel, _ => SelectedDesignDigestViewModel != default)
                );

            FollowKeyboardInDesignDigests = ReactiveCommand.Create <(Key Key, bool Control, bool Shift)>(
                HandleFollowKeyboardInDesignDigests
                );

            DesignDigestViewModels = new ObservableCollection <IDesignDigestViewModel>(
                estimationDesigns.DesignDigests.Map(dd => new DesignDigestViewModel(dd.CreatedOn, dd.Description)
                                                    ));

            if (_moduleState.EstimationDesign != default)
            {
                TargetEstimationDesign = Some((_moduleState.EstimationDesign.CreatedOn, DateTime.Now));

                SelectedDesignDigestViewModel = DesignDigestViewModels
                                                .Find(vm => vm.CreatedOn == _moduleState.EstimationDesign.CreatedOn)
                                                .Match <IDesignDigestViewModel?>(vm => vm, () => default);
            }

            _reactiveSafeInvoke = appService.GetReactiveSafeInvoke();

            _subscriptions = new CompositeDisposable(

                moduleState
                .ObservableForProperty(ms => ms.EstimationDesign)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveModuleStateEstimationDesign
                        )
                    ),

                estimationDesigns.EstimationDesignChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(DesignDigest, ObservableQualifier)>(
                        ObserveEstimationDesignChange
                        )
                    )

                );
        }
Esempio n. 2
0
        internal static ModuleState LoadOrCreate(
            Simulation simulation,
            ISimEvidence evidence,
            IAppService appService,
            EstimationDesigns estimationDesigns
            )
        {
            var maybeDTO = simulation.LoadPrivateData <_ModuleStateDTO>(
                nameof(Estimation),
                nameof(ViewModel),
                nameof(ModuleState)
                );

            return(maybeDTO.Match(
                       dto => new ModuleState(dto, evidence, appService, estimationDesigns),
                       () => new ModuleState(evidence, appService, estimationDesigns)
                       ));
        }
Esempio n. 3
0
        internal ViewModel(IAppState appState, IAppService appService, IAppSettings appSettings)
        {
            _appState    = appState;
            _appService  = appService;
            _sharedState = appState.SimSharedState;
            _simulation  = appState.Target.AssertSome();
            _evidence    = appState.SimEvidence;

            var pathToEstimationDesignsDirectory = _simulation.GetPrivateDirectory(nameof(Estimation), nameof(EstimationDesigns));

            _estimationDesigns = new EstimationDesigns(pathToEstimationDesignsDirectory, _evidence);

            _moduleState = ModuleState.LoadOrCreate(_simulation, _evidence, appService, _estimationDesigns);

            _priorsViewModel        = new PriorsViewModel(appState, appService, appSettings, _moduleState);
            _likelihoodViewModel    = new LikelihoodViewModel(appState, appService, appSettings, _moduleState);
            _designViewModel        = new DesignViewModel(appState, appService, appSettings, _moduleState, _estimationDesigns);
            _simulationViewModel    = new SimulationViewModel(appState, appService, appSettings, _moduleState, _estimationDesigns);
            _posteriorViewModel     = new PosteriorViewModel(appState, appService, appSettings, _moduleState);
            _fitViewModel           = new FitViewModel(appState, appService, appSettings, _moduleState);
            _designDigestsViewModel = new DesignDigestsViewModel(appService, _moduleState, _estimationDesigns);

            _reactiveSafeInvoke = appService.GetReactiveSafeInvoke();

            _subscriptions = new CompositeDisposable(

                _moduleState.PriorStateChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <ParameterState>, ObservableQualifier)>(
                        ObservePriorStateChange
                        )
                    ),

                _sharedState.ParameterSharedStateChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimParameterSharedState>, ObservableQualifier)>(
                        ObserveParameterSharedStateChange
                        )
                    ),

                _moduleState.OutputStateChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <OutputState>, ObservableQualifier)>(
                        ObserveOutputStateChange
                        )
                    ),

                _sharedState.ElementSharedStateChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimElementSharedState>, ObservableQualifier)>(
                        ObserveElementSharedStateChange
                        )
                    ),

                _moduleState.ObservationsChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimObservations>, ObservableQualifier)>(
                        ObserveModuleStateObservationsChange
                        )
                    ),

                _sharedState.ObservationsSharedStateChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimObservationsSharedState>, ObservableQualifier)>(
                        ObserveSharedStateObservationsChange
                        )
                    ),

                _evidence.ObservationsChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimObservations>, ObservableQualifier)>(
                        ObserveEvidenceObservationsChange
                        )
                    ),

                _designDigestsViewModel
                .ObservableForProperty(vm => vm.TargetEstimationDesign).Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveTargetEstimationDesignCreatedOn
                        )
                    ),

                _moduleState
                .ObservableForProperty(ms => ms.EstimationDesign)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveModuleStateEstimationDesign
                        )
                    ),

                _estimationDesigns.EstimationDesignChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(DesignDigest, ObservableQualifier)>(
                        ObserveEstimationDesignChange
                        )
                    )

                );

            SetActivities();

            if (_moduleState.EstimationDesign != default)
            {
                _designViewModel.IsSelected = true;
            }
        }
Esempio n. 4
0
        internal DesignViewModel(
            IAppState appState,
            IAppService appService,
            IAppSettings appSettings,
            ModuleState moduleState,
            EstimationDesigns estimationDesigns
            )
        {
            _appState          = appState;
            _appService        = appService;
            _appSettings       = appSettings;
            _moduleState       = moduleState;
            _estimationDesigns = estimationDesigns;

            _reactiveSafeInvoke = appService.GetReactiveSafeInvoke();

            using (_reactiveSafeInvoke.SuspendedReactivity)
            {
                if (_moduleState.EstimationDesign == default)
                {
                    Populate();
                }
                else
                {
                    Populate(_moduleState.EstimationDesign);
                }

                UpdateDesignEnable();
            }

            CreateDesign = ReactiveCommand.Create(
                HandleCreateDesign,
                this.WhenAny(
                    vm => vm.CanCreateDesign,
                    vm => vm.Iterations,
                    vm => vm.BurnIn,
                    (_, __, ___) =>
                    CanCreateDesign &&
                    BurnIn >= 0 &&
                    BurnIn < Iterations
                    )
                );

            UnloadDesign = ReactiveCommand.Create(
                HandleUnloadDesign,
                this.WhenAny(vm => vm.CanUnloadDesign, _ => CanUnloadDesign)
                );

            using (_reactiveSafeInvoke.SuspendedReactivity)
            {
                _subscriptions = new CompositeDisposable(

                    moduleState
                    .WhenAnyValue(
                        ms => ms.PriorStates,
                        ms => ms.OutputStates,
                        ms => ms.SelectedObservations
                        )
                    .Subscribe(
                        _reactiveSafeInvoke.SuspendAndInvoke <(Arr <ParameterState>, Arr <OutputState>, Arr <SimObservations>)>(
                            ObserveEstimationStateChange
                            )
                        ),

                    moduleState
                    .ObservableForProperty(vm => vm.EstimationDesign)
                    .Subscribe(
                        _reactiveSafeInvoke.SuspendAndInvoke <object>(
                            ObserveModuleStateEstimationDesign
                            )
                        ),

                    _appSettings
                    .ObservableForProperty(@as => @as.RThrottlingUseCores)
                    .Subscribe(
                        _reactiveSafeInvoke.SuspendAndInvoke <object>(
                            ObserveAppSettingsRThrottlingUseCores
                            )
                        ),

                    this
                    .WhenAnyValue(vm => vm.Iterations, vm => vm.BurnIn, vm => vm.ChainsIndex)
                    .Subscribe(
                        _reactiveSafeInvoke.SuspendAndInvoke <(int?, int?, int)>(
                            ObserveInputs
                            )
                        )

                    );
            }
        }
Esempio n. 5
0
 private ModuleState(ISimEvidence evidence, IAppService appService, EstimationDesigns estimationDesigns)
     : this(new _ModuleStateDTO(), evidence, appService, estimationDesigns)
 {
 }
Esempio n. 6
0
        private ModuleState(
            _ModuleStateDTO dto,
            ISimEvidence evidence,
            IAppService appService,
            EstimationDesigns estimationDesigns
            )
        {
            _estimationDesigns = estimationDesigns;

            PriorsState.SelectedPrior = dto.PriorsState?.SelectedPrior;

            LikelihoodState.SelectedOutput = dto.LikelihoodState?.SelectedOutput;

            DesignState.Iterations = dto.DesignState?.Iterations;
            DesignState.BurnIn     = dto.DesignState?.BurnIn;
            DesignState.Chains     = dto.DesignState?.Chains;

            SimulationState.SelectedParameter = dto.SimulationState?.SelectedParameter;

            if (!dto.PriorStates.IsNullOrEmpty())
            {
                _priorStates = dto.PriorStates
                               .Select(ps =>
                {
                    var name               = ps.Name.AssertNotNull();
                    var distributionType   = Enum.TryParse(ps.DistributionType, out DistributionType dt) ? dt : DistributionType.None;
                    var distributionStates = Distribution.DeserializeDistributions(ps.DistributionStates.AssertNotNull());
                    var isSelected         = ps.IsSelected;
                    return(new ParameterState(name, distributionType, distributionStates, isSelected));
                })
                               .ToArr();
            }

            if (!dto.OutputStates.IsNullOrEmpty())
            {
                _outputStates = dto.OutputStates
                                .Select(os =>
                {
                    var name             = os.Name.AssertNotNull();
                    var errorModelType   = Enum.TryParse(os.ErrorModelType, out ErrorModelType emt) ? emt : ErrorModelType.None;
                    var errorModelStates = ErrorModel.DeserializeErrorModels(os.ErrorModelStates.AssertNotNull());
                    var isSelected       = os.IsSelected;
                    return(new OutputState(name, errorModelType, errorModelStates, isSelected));
                })
                                .ToArr();
            }

            _selectedObservations = dto.SelectedObservationsReferences.IsNullOrEmpty()
        ? default
        : dto.SelectedObservationsReferences
                                    .Select(r => evidence.GetObservations(r))
                                    .Somes()
                                    .ToArr();

            if (dto.EstimationDesign.IsAString())
            {
                try
                {
                    var createdOn = dto.EstimationDesign.FromDirectoryName();
                    EstimationDesign = estimationDesigns.Load(createdOn);
                    var pathToEstimationDesign = estimationDesigns.GetPathToEstimationDesign(EstimationDesign);
                    ChainStates    = ChainState.Load(pathToEstimationDesign);
                    PosteriorState = PosteriorState.Load(pathToEstimationDesign);
                }
                catch (Exception) { /* logged elsewhere */ }
            }

            RootExportDirectory = dto.RootExportDirectory;
            OpenAfterExport     = dto.OpenAfterExport;

            _autoApplyParameterSharedState    = dto.AutoApplyParameterSharedState;
            _autoShareParameterSharedState    = dto.AutoShareParameterSharedState;
            _autoApplyElementSharedState      = dto.AutoApplyElementSharedState;
            _autoShareElementSharedState      = dto.AutoShareElementSharedState;
            _autoApplyObservationsSharedState = dto.AutoApplyObservationsSharedState;
            _autoShareObservationsSharedState = dto.AutoShareObservationsSharedState;

            _reactiveSafeInvoke = appService.GetReactiveSafeInvoke();

            _subscriptions = new CompositeDisposable(

                evidence.ObservationsChanges.Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <(Arr <SimObservations> Observations, ObservableQualifier ObservableQualifier)>(
                        ObserveEvidenceObservationsChanges
                        )
                    )

                );
        }
Esempio n. 7
0
        internal SimulationViewModel(IAppState appState, IAppService appService, IAppSettings appSettings, ModuleState moduleState, EstimationDesigns estimationDesigns)
        {
            _appService         = appService;
            _moduleState        = moduleState;
            _estimationDesigns  = estimationDesigns;
            _simulation         = appState.Target.AssertSome("No simulation");
            _simData            = appState.SimData;
            _estimationDesign   = moduleState.EstimationDesign;
            _chainStates        = _moduleState.ChainStates;
            _reactiveSafeInvoke = appService.GetReactiveSafeInvoke();

            StartIterating = ReactiveCommand.Create(
                HandleStartIterating,
                this.WhenAny(vm => vm.CanStartIterating, _ => CanStartIterating)
                );

            StopIterating = ReactiveCommand.Create(
                HandleStopIterating,
                this.WhenAny(vm => vm.CanStopIterating, _ => CanStopIterating)
                );

            ShowSettings = ReactiveCommand.Create(
                HandleShowSettings,
                this.WhenAny(vm => vm.CanShowSettings, _ => CanShowSettings)
                );

            SetConvergenceRange = ReactiveCommand.Create(
                HandleSetConvergenceRange,
                this.WhenAny(vm => vm.CanSetConvergenceRange, _ => CanSetConvergenceRange)
                );

            PlotModel = new PlotModel();

            _horizontalAxis = new LinearAxis
            {
                Position        = AxisPosition.Bottom,
                AbsoluteMinimum = 1d,
                Minimum         = 1d,
                Title           = "Iteration"
            };
            PlotModel.Axes.Add(_horizontalAxis);

            _verticalAxis = new LinearAxis
            {
                Position = AxisPosition.Left
            };
            PlotModel.Axes.Add(_verticalAxis);

            _posteriorAnnotation = new RectangleAnnotation
            {
                Fill     = OxyColor.FromAColor(120, OxyColors.SkyBlue),
                MinimumX = 0,
                MaximumX = 0
            };
            PlotModel.Annotations.Add(_posteriorAnnotation);

            PlotModel.MouseDown += HandlePlotModelMouseDown;
            PlotModel.MouseMove += HandlePlotModelMouseMove;
            PlotModel.MouseUp   += HandlePlotModelMouseUp;

            PlotModel.ApplyThemeToPlotModelAndAxes();

            using (_reactiveSafeInvoke.SuspendedReactivity)
            {
                _posteriorBegin = _moduleState.PosteriorState?.BeginIteration;
                _posteriorEnd   = _moduleState.PosteriorState?.EndIteration;

                PopulateControls();
                PopulateChartData();
                PopulateChart();
                PopulatePosteriorAnnotation();
                UpdateEnable();
            }

            _subscriptions = new CompositeDisposable(

                appSettings
                .GetWhenPropertyChanged()
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <string?>(ObserveAppSettingsPropertyChange)
                    ),

                _moduleState
                .ObservableForProperty(ms => ms.EstimationDesign)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveModuleStateEstimationDesign
                        )
                    ),

                _moduleState
                .ObservableForProperty(ms => ms.ChainStates)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveModuleStateChainStates
                        )
                    ),

                _moduleState
                .ObservableForProperty(ms => ms.PosteriorState)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveModuleStatePosteriorState
                        )
                    ),

                this
                .ObservableForProperty(vm => vm.SelectedParameter)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObserveSelectedParameter
                        )
                    ),

                this
                .ObservableForProperty(vm => vm.PosteriorBegin)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObservePosteriorBegin
                        )
                    ),

                this
                .ObservableForProperty(vm => vm.PosteriorEnd)
                .Subscribe(
                    _reactiveSafeInvoke.SuspendAndInvoke <object>(
                        ObservePosteriorEnd
                        )
                    )

                );
        }
Esempio n. 8
0
 internal static Option <DesignDigest> GetDesignDigest(this EstimationDesigns estimationDesigns, DateTime createdOn) =>
 estimationDesigns.DesignDigests.Find(dd => dd.CreatedOn == createdOn);