public static Arr <SimObservations> GetRelevantObservations(this ModuleState moduleState) { var selectedOutputStates = moduleState.OutputStates.Filter(os => os.IsSelected); return(moduleState.SelectedObservations.Filter( o => selectedOutputStates.Exists(os => os.Name == o.Subject) )); }
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 ) ) ); }
internal LikelihoodViewModel(IAppState appState, IAppService appService, IAppSettings appSettings, ModuleState moduleState) { _appSettings = appSettings; _simulation = appState.Target.AssertSome("No simulation"); _evidence = appState.SimEvidence; _moduleState = moduleState; _toggleSelectOutput = ReactiveCommand.Create <IOutputViewModel>(HandleToggleSelectOutput); AllOutputViewModels = _simulation.SimConfig.SimOutput.DependentVariables .Map(e => new OutputViewModel(e.Name, _toggleSelectOutput)) .OrderBy(vm => vm.SortKey) .ToArr <IOutputViewModel>(); _moduleState.OutputStates.Iter(os => { var outputViewModel = AllOutputViewModels .Find(vm => vm.Name == os.Name) .AssertSome($"Unknown output in module state: {os.Name}"); outputViewModel.IsSelected = os.IsSelected; outputViewModel.ErrorModel = os.GetErrorModel().ToString(os.Name); }); SelectedOutputViewModels = new ObservableCollection <IOutputViewModel>( AllOutputViewModels.Where(vm => vm.IsSelected) ); SelectedOutputViewModel = SelectedOutputViewModels.FindIndex( vm => vm.Name == moduleState.LikelihoodState.SelectedOutput ); var errorModelTypes = ErrorModelType.All; _outputErrorViewModel = new OutputErrorViewModel( appState, appService, errorModelTypes ); var ivElement = _simulation.SimConfig.SimOutput.IndependentVariable; _observationsScatterPlot = new PlotModel(); _observationsScatterPlot.AssignDefaultColors(appSettings.IsBaseDark, PLOT_COLOR_COUNT); _observationsScatterPlot.AddAxes(ivElement.GetFQName(), default, default, default, default, default);
internal PriorsViewModel(IAppState appState, IAppService appService, IAppSettings appSettings, ModuleState moduleState) { _simulation = appState.Target.AssertSome("No simulation"); _moduleState = moduleState; _toggleSelectPrior = ReactiveCommand.Create <IPriorViewModel>(HandleToggleSelectPrior); var parameters = _simulation.SimConfig.SimInput.SimParameters; AllPriorViewModels = parameters .Map(p => new PriorViewModel(p.Name, _toggleSelectPrior)) .OrderBy(vm => vm.SortKey) .ToArr <IPriorViewModel>(); moduleState.PriorStates.Iter(ps => { var priorViewModel = AllPriorViewModels .Find(vm => vm.Name == ps.Name) .AssertSome($"Unknown prior in module state: {ps.Name}"); priorViewModel.IsSelected = ps.IsSelected; priorViewModel.Distribution = ps.GetDistribution().ToString(ps.Name); }); SelectedPriorViewModels = new ObservableCollection <IPriorViewModel>( AllPriorViewModels.Where(vm => vm.IsSelected) ); SelectedPriorViewModel = SelectedPriorViewModels.FindIndex( vm => vm.Name == moduleState.PriorsState.SelectedPrior ); var distributionTypes = DistributionType.Beta | DistributionType.Gamma | DistributionType.Invariant | DistributionType.LogNormal | DistributionType.Normal | DistributionType.Uniform ; _parameterDistributionViewModel = new ParameterDistributionViewModel( appState, appService, appSettings, distributionTypes, allowTruncation: true ); _reactiveSafeInvoke = appService.GetReactiveSafeInvoke(); _subscriptions = new CompositeDisposable( this .WhenAny(vm => vm.SelectedPriorViewModel, _ => default(object)) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object?>( ObserveSelectedPriorViewModel ) ), _parameterDistributionViewModel .ObservableForProperty(vm => vm.ParameterState) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object>( ObserveParameterDistributionViewModelParameterState ) ), moduleState.PriorStateChanges.Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <(Arr <ParameterState>, ObservableQualifier)>( ObserveModuleStatePriorStateChange ) ) ); }
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; } }
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 ) ) ); } }
internal static void Save(ModuleState instance, Simulation simulation, ISimEvidence evidence) { simulation.SavePrivateData( new _ModuleStateDTO { PriorsState = new _PriorsStateDTO { SelectedPrior = instance.PriorsState.SelectedPrior }, LikelihoodState = new _LikelihoodStateDTO { SelectedOutput = instance.LikelihoodState.SelectedOutput }, DesignState = new _DesignStateDTO { Iterations = instance.DesignState.Iterations, BurnIn = instance.DesignState.BurnIn, Chains = instance.DesignState.Chains }, SimulationState = new _SimulationStateDTO { SelectedParameter = instance.SimulationState.SelectedParameter }, PriorStates = instance.PriorStates .Map(ps => new _PriorStateDTO { Name = ps.Name, DistributionType = ps.DistributionType.ToString(), DistributionStates = Distribution.SerializeDistributions(ps.Distributions), IsSelected = ps.IsSelected }) .ToArray(), OutputStates = instance.OutputStates .Map(os => new _OutputStateDTO { Name = os.Name, ErrorModelType = os.ErrorModelType.ToString(), ErrorModelStates = ErrorModel.SerializeErrorModels(os.ErrorModels), IsSelected = os.IsSelected }) .ToArray(), SelectedObservationsReferences = instance.SelectedObservations .Map(o => evidence.GetReference(o)) .ToArray(), EstimationDesign = instance.EstimationDesign?.CreatedOn.ToDirectoryName(), RootExportDirectory = instance.RootExportDirectory, OpenAfterExport = instance.OpenAfterExport, AutoApplyParameterSharedState = instance.AutoApplyParameterSharedState, AutoShareParameterSharedState = instance.AutoShareParameterSharedState, AutoApplyElementSharedState = instance.AutoApplyElementSharedState, AutoShareElementSharedState = instance.AutoShareElementSharedState, AutoApplyObservationsSharedState = instance.AutoApplyObservationsSharedState, AutoShareObservationsSharedState = instance.AutoShareObservationsSharedState }, nameof(Estimation), nameof(ViewModel), nameof(ModuleState) ); }
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 ) ) ); }
internal PosteriorViewModel(IAppState appState, IAppService appService, IAppSettings appSettings, ModuleState moduleState) { _appState = appState; _moduleState = moduleState; _reactiveSafeInvoke = appService.GetReactiveSafeInvoke(); PlotModel = new PlotModel() { LegendPlacement = LegendPlacement.Inside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical }; _horizontalAxis = new LinearAxis { Position = AxisPosition.Bottom }; PlotModel.Axes.Add(_horizontalAxis); _leftVerticalAxis = new LinearAxis { Title = "Frequency", Position = AxisPosition.Left, Key = nameof(_leftVerticalAxis) }; PlotModel.Axes.Add(_leftVerticalAxis); _rightVerticalAxis = new LinearAxis { Title = "Probability", Position = AxisPosition.Right, Key = nameof(_rightVerticalAxis) }; PlotModel.Axes.Add(_rightVerticalAxis); PlotModel.ApplyThemeToPlotModelAndAxes(); using (_reactiveSafeInvoke.SuspendedReactivity) { _subscriptions = new CompositeDisposable( appSettings .GetWhenPropertyChanged() .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <string?>( ObserveAppSettingsPropertyChange ) ), moduleState .ObservableForProperty(ms => ms.EstimationDesign) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object>( ObserveModuleStateEstimationDesign ) ), moduleState .WhenAnyValue( ms => ms.ChainStates, ms => ms.PosteriorState ) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <(Arr <ChainState>, PosteriorState?)>( ObserveModuleStateEstimationDataChange ) ), this .ObservableForProperty(vm => vm.SelectedParameterName) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object>( ObserveSelectedParameterName ) ) ); if (CanConfigureControls) { PopulateControls(); } if (CanViewPosterior) { PopulatePosterior(); } } }
internal FitViewModel(IAppState appState, IAppService appService, IAppSettings appSettings, ModuleState moduleState) { _appState = appState; _moduleState = moduleState; _reactiveSafeInvoke = appService.GetReactiveSafeInvoke(); PlotModel = new PlotModel() { LegendPlacement = LegendPlacement.Inside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical }; var simulation = _appState.Target.AssertSome(); var output = simulation.SimConfig.SimOutput; var independentVariable = output.IndependentVariable; _horizontalAxis = new LinearAxis { Title = independentVariable.Name, Unit = independentVariable.Unit, Position = AxisPosition.Bottom }; PlotModel.Axes.Add(_horizontalAxis); _verticalAxis = new LinearAxis { Position = AxisPosition.Left }; PlotModel.Axes.Add(_verticalAxis); PlotModel.ApplyThemeToPlotModelAndAxes(); using (_reactiveSafeInvoke.SuspendedReactivity) { _subscriptions = new CompositeDisposable( appSettings .GetWhenPropertyChanged() .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <string?>( ObserveAppSettingsPropertyChange ) ), moduleState .ObservableForProperty(ms => ms.EstimationDesign) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object>( ObserveModuleStateEstimationDesign ) ), moduleState .WhenAnyValue( ms => ms.ChainStates, ms => ms.PosteriorState ) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <(Arr <ChainState>, PosteriorState?)>( ObserveModuleStateEstimationDataChange ) ), this .ObservableForProperty(vm => vm.SelectedOutputName) .Subscribe( _reactiveSafeInvoke.SuspendAndInvoke <object>( ObserveSelectedOutputName ) ) ); if (CanConfigureControls) { PopulateControls(); } if (CanViewFit) { PopulateChart(); } } }