void PerformAnalysis() { var filterStartTime = _filterStartTime; var filterEndTime = _filterEndTime; var selectedTimeRange = SelectedTimeRange; Task<bool> processTask = null; var selectedModeIds = (from mode in AvailableModes where mode.IsSelected select ((ModeNameGuid)mode.NameGuidRecord).ActorID).ToList(); var selectedSpeciesGuids = (from species in AvailableSpecies where species.IsSelected select species.NameGuidRecord.Guid).ToList(); Func<ActorExposureRecord, bool> modeFilter = record => { var speciesRecord = SimulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid; return speciesRecord != null && selectedModeIds.Contains(record.ModeID); }; Func<ActorExposureRecord, bool> speciesFilter = record => { var speciesRecord = SimulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid; return speciesRecord != null && selectedSpeciesGuids.Contains(speciesRecord.Guid); }; var histogramBinsViewModels = new ObservableCollection<HistogramBinsViewModel>(); var viewModel = new SimulationExposuresViewModel(histogramBinsViewModels) { WindowTitle = "Test" }; _dispatcher.InvokeIfRequired(() => OpenWindows.Add(_visualizer.ShowWindow("SimulationExposuresView", viewModel))); var modeThresholdHistogram = new ModeThresholdHistogram(this, SimulationLog, StartBinValue, BinWidth, BinCount, modeFilter, speciesFilter); _modeBinsCollectionObserver = new CollectionObserver(modeThresholdHistogram.GroupedExposures.Groups) .RegisterHandler((s, e) => { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (GroupedExposuresHistogram histogram in e.NewItems) histogramBinsViewModels.Add(new HistogramBinsViewModel(histogram)); break; } }); var timeStepIndex = 0; foreach (var timeStepRecord in SimulationLog.Where(timeStepRecord => !selectedTimeRange || (timeStepRecord.StartTime >= filterStartTime && filterEndTime >= timeStepRecord.StartTime))) { timeStepRecord.ReadAll(); timeStepIndex++; var record = timeStepRecord; if (processTask != null) processTask.Wait(); processTask = modeThresholdHistogram.Process(record, _dispatcher); //if (timeStepIndex % 10 == 0) UpdateHistogramDisplay(); } if (processTask != null) processTask.Wait(); UpdateHistogramDisplay(); Debug.WriteLine("Finished processing simulation exposure file"); }
public async Task Analyze(Dispatcher dispatcher, IUIVisualizerService visualizer) { if (SelectedModeIds == null) throw new NullReferenceException("SelectedModeIds may not be null"); if (SelectedSpeciesGuids == null) throw new NullReferenceException("SelectedSpeciesGuids may not be null"); if (SelectedModeIds.Count == 0) throw new InvalidOperationException("SelectedModeIds may not be an empty list"); if (SelectedSpeciesGuids.Count == 0) throw new InvalidOperationException("SelectedSpeciesGuids may not be an empty list"); if (double.IsNaN(StartBinValue) || (StartBinValue < 0)) throw new InvalidOperationException("StartBinValue must be a positive value"); if (double.IsNaN(BinWidth) || (BinWidth < 0)) throw new InvalidOperationException("BinWidth must be a positive value"); if (BinCount <= 0) throw new InvalidOperationException("BinCount must be a positive value"); if (FilterStartTime.Ticks < 0) throw new InvalidOperationException("FilterStartTime must be a positive value"); if (FilterEndTime.Ticks < 0) throw new InvalidOperationException("FilterEndTime must be a positive value"); if (FilterEndTime.Ticks <= FilterStartTime.Ticks) throw new InvalidOperationException("FilterEndTime must be greater than FilterStartTime"); SimulationLog simulationLog = null; try { simulationLog = SimulationLog.Open(LogFilename); if (FilterEndTime.Ticks > (simulationLog.TimeStepSize.Ticks * simulationLog.TimeStepCount)) throw new InvalidOperationException("FilterEndTime cannot be greater than the last time step in the simulation"); for (var speciesIndex = 0; speciesIndex < simulationLog.SpeciesRecords.Count; speciesIndex++) GuidToColorMap.Add(simulationLog.SpeciesRecords[speciesIndex].Guid, BarColors[speciesIndex % BarColors.Count]); Func<ActorExposureRecord, bool> modeFilter = record => { var speciesRecord = simulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid; return speciesRecord != null && SelectedModeIds.Contains(record.ModeID); }; Func<ActorExposureRecord, bool> speciesFilter = record => { var speciesRecord = simulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid; return speciesRecord != null && SelectedSpeciesGuids.Contains(speciesRecord.Guid); }; var histogramBinsViewModels = new ObservableCollection<HistogramBinsViewModel>(); var scenarioName = simulationLog.ScenarioRecord.Name; var simulationStartTime = simulationLog.StartTime; SimulationExposuresViewModel viewModel = null; dispatcher.InvokeIfRequired(() => { viewModel = new SimulationExposuresViewModel(histogramBinsViewModels) { WindowTitle = string.Format("Scenario: {0} simulated on {1} (Processing)", scenarioName, simulationStartTime) }; Window = visualizer.ShowWindow("SimulationExposuresView", viewModel); }); var modeThresholdHistogram = new ModeThresholdHistogram(this, simulationLog, StartBinValue, BinWidth, BinCount, modeFilter, speciesFilter); _modeBinsCollectionObserver = new CollectionObserver(modeThresholdHistogram.GroupedExposures.Groups) .RegisterHandler((s, e) => { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (GroupedExposuresHistogram histogram in e.NewItems) histogramBinsViewModels.Add(new HistogramBinsViewModel(histogram)); break; } }); var timeStepIndex = 0; Task<bool> processTask = null; foreach (var timeStepRecord in simulationLog.Where(timeStepRecord => timeStepRecord.StartTime >= FilterStartTime && FilterEndTime >= timeStepRecord.StartTime)) { timeStepRecord.ReadAll(); timeStepIndex++; var record = timeStepRecord; if (processTask != null) await processTask; processTask = modeThresholdHistogram.Process(record, dispatcher); if (timeStepIndex % 10 == 0) UpdateHistogramDisplay(); } if (processTask != null) await processTask; UpdateHistogramDisplay(); viewModel.WindowTitle = string.Format("Scenario: {0} simulated on {1}", scenarioName, simulationStartTime); Debug.WriteLine("Finished processing simulation exposure file"); } finally { if (simulationLog != null) simulationLog.Close(); } }
public async Task Start(TimeSpan timeStepSize) { TimeStepSize = timeStepSize; _cancellationTokenSource = new CancellationTokenSource(); SimulationLog = SimulationLog.Create(Path.Combine(_simulationDirectory, "simulation.exposures"), TimeStepSize, Scenario); ModeThresholdHistogram = new ModeThresholdHistogram(this, SimulationLog, 100.0, 10.0, 10); //SpeciesThresholdHistogram = new SpeciesThresholdHistogram(this); try { var result = Task.Run(() => Run(TimeStepSize, _cancellationTokenSource.Token)); await result; } catch (Exception e) { throw new SimulationException("Simulation failed", e); } //return Run(TimeStepSize, _cancellationTokenSource.Token); }