Ejemplo n.º 1
0
        public async Task RunAsync(IndividualSimulation individualSimulation, SimulationRunOptions simulationRunOptions)
        {
            _shouldRaiseEvents = simulationRunOptions.RaiseEvents;
            initializeProgress();

            _simModelManager.SimulationProgress += simulationProgress;
            //make sure that thread methods always catch and handle any exception,
            //otherwise we risk unplanned application termination
            var begin = SystemTime.UtcNow();

            try
            {
                raiseEvent(new SimulationRunStartedEvent());
                await runSimulation(individualSimulation, simulationRunOptions);
            }
            catch (Exception)
            {
                terminated();
                throw;
            }
            finally
            {
                var end       = SystemTime.UtcNow();
                var timeSpent = end - begin;
                raiseEvent(new SimulationRunFinishedEvent(individualSimulation, timeSpent));
            }
        }
Ejemplo n.º 2
0
        private async Task exportSimulation(Simulation simulation, ExportRunOptions exportRunOptions)
        {
            var outputFolder = Path.Combine(exportRunOptions.OutputFolder, simulation.Name);

            DirectoryHelper.CreateDirectory(outputFolder);

            var simulationRunOptions = new SimulationRunOptions();

            _lazyLoadTask.Load(simulation);
            _lazyLoadTask.LoadResults(simulation);

            if (!simulation.OutputSelections.HasSelection)
            {
                _logger.AddWarning($"Simulation '{simulation.Name}' does not have any selected output and will not be exported");
                return;
            }

            if (exportRunOptions.RunSimulation)
            {
                await _simulationExporter.RunAndExport(simulation, outputFolder, simulationRunOptions, exportRunOptions.ExportMode);
            }

            else if (simulation.HasResults)
            {
                await _simulationExporter.Export(simulation, outputFolder, exportRunOptions.ExportMode);
            }

            else
            {
                _logger.AddWarning($"Simulation '{simulation.Name}' does not have any results and will not be exported");
            }
        }
Ejemplo n.º 3
0
 public override void GlobalContext()
 {
     base.GlobalContext();
     _compoundProcessRepository = IoC.Resolve <ICompoundProcessRepository>();
     _cloneManager                 = IoC.Resolve <ICloneManager>();
     _enzymeFactory                = IoC.Resolve <IIndividualEnzymeFactory>();
     _transporterFactory           = IoC.Resolve <IIndividualTransporterFactory>();
     _modelPropertiesTask          = IoC.Resolve <IModelPropertiesTask>();
     _modelConfigurationRepository = IoC.Resolve <IModelConfigurationRepository>();
     _compound   = DomainFactoryForSpecs.CreateStandardCompound();
     _individual = DomainFactoryForSpecs.CreateStandardIndividual();
     _protocol   = DomainFactoryForSpecs.CreateStandardIVBolusProtocol();
     _enzyme     = _enzymeFactory.CreateFor(_individual).DowncastTo <IndividualEnzyme>().WithName("CYP");
     _enzyme.GetRelativeExpressionNormParameterFor(CoreConstants.Compartment.Plasma).Value              = _relExpNormPls;
     _enzyme.GetRelativeExpressionNormParameterFor(CoreConstants.Compartment.BloodCells).Value          = _relExpNormBloodCells;
     _enzyme.GetRelativeExpressionNormParameterFor(CoreConstants.Compartment.VascularEndothelium).Value = _relExpVascEndo;
     _individual.AddMolecule(_enzyme);
     _hct = _individual.Organism.Parameter(CoreConstants.Parameters.HCT).Value;
     _metabolizationProcess      = _cloneManager.Clone(_compoundProcessRepository.ProcessByName(CoreConstantsForSpecs.Process.METABOLIZATION_SPECIFIC_FIRST_ORDER).DowncastTo <PartialProcess>());
     _metabolizationProcess.Name = "My Partial Process";
     _metabolizationProcess.Parameter(ConverterConstants.Parameter.CLspec).Value = 15;
     _compound.AddProcess(_metabolizationProcess);
     _simulationRunOptions = new SimulationRunOptions {
         RaiseEvents = false
     };
 }
Ejemplo n.º 4
0
        public async Task RunAndExport(Simulation simulation, string outputFolder, SimulationRunOptions simulationRunOptions, SimulationExportMode simulationExportMode, string projectName = null)
        {
            _logger.AddDebug($"Running simulation '{simulation.Name}'");
            await _simulationRunner.RunSimulation(simulation, simulationRunOptions);

            await Export(simulation, outputFolder, simulationExportMode, projectName);
        }
Ejemplo n.º 5
0
        private Task runSimulation(IndividualSimulation simulation, bool exportAll, bool raiseEvents, bool checkForNegativeValues)
        {
            //Should be done outside of the Task.Run to ensure that any event that might be raised by this action won't cause threading issue
            updatePersistableFor(simulation, exportAll);

            return(Task.Run(() =>
            {
                var modelCoreSimulation = _modelCoreSimulationMapper.MapFrom(simulation, shouldCloneModel: false);
                var simRunOptions = new SimulationRunOptions
                {
                    SimModelExportMode = SimModelExportMode.Optimized,
                    CheckForNegativeValues = checkForNegativeValues
                };

                var simResults = _simModelManager.RunSimulation(modelCoreSimulation, simRunOptions);

                if (!simResults.Success)
                {
                    return;
                }

                _simulationResultsSynchronizer.Synchronize(simulation, simResults.Results);
                simulation.ClearPKCache();

                if (raiseEvents)
                {
                    _eventPublisher.PublishEvent(new SimulationResultsUpdatedEvent(simulation));
                }
            }));
        }
Ejemplo n.º 6
0
        protected override async Task Context()
        {
            await base.Context();

            _simulationRunOptions = new SimulationRunOptions();
            _simulation           = A.Fake <IndividualSimulation>();
            A.CallTo(() => _entityTask.Validate(_simulation)).Returns(true);
        }
Ejemplo n.º 7
0
 public override void GlobalContext()
 {
     base.GlobalContext();
     _compound             = DomainFactoryForSpecs.CreateStandardCompound();
     _individual           = DomainFactoryForSpecs.CreateStandardIndividual();
     _protocol             = DomainFactoryForSpecs.CreateStandardIVBolusProtocol();
     _globalPKAnalysisTask = IoC.Resolve <IGlobalPKAnalysisTask>();
     _simulationRunOptions = new SimulationRunOptions();
 }
Ejemplo n.º 8
0
        public async Task <SimulationExport> ExportSimulation(Simulation simulation, ExportRunOptions exportRunOptions, PKSimProject project)
        {
            var projectName      = project.Name;
            var simulationName   = simulation.Name;
            var simulationFolder = Path.Combine(exportRunOptions.OutputFolder, FileHelper.RemoveIllegalCharactersFrom(simulationName));

            DirectoryHelper.CreateDirectory(simulationFolder);

            var simulationExport = new SimulationExport
            {
                Project          = projectName,
                SimulationFolder = simulationFolder,
                Simulation       = simulationName
            };

            var simulationRunOptions = new SimulationRunOptions();

            _lazyLoadTask.Load(simulation);
            _lazyLoadTask.LoadResults(simulation);

            var simulationExportOptions = new SimulationExportOptions
            {
                OutputFolder = simulationFolder,
                ExportMode   = exportRunOptions.ExportMode,
                ProjectName  = projectName,
            };

            if (!simulation.OutputSelections.HasSelection)
            {
                _logger.AddWarning($"Simulation '{simulationName}' does not have any selected output and will not be exported", projectName);
                return(simulationExport);
            }

            if (exportRunOptions.RunSimulation)
            {
                await _simulationExporter.RunAndExport(simulation, simulationRunOptions, simulationExportOptions);
            }

            else if (simulation.HasResults)
            {
                await _simulationExporter.Export(simulation, simulationExportOptions);
            }

            else
            {
                _logger.AddWarning($"Simulation '{simulationName}' does not have any results and will not be exported", projectName);
                return(simulationExport);
            }

            _logger.AddDebug($"Simulation '{simulationName}' exported to '{simulationFolder}'", projectName);
            return(simulationExport);
        }
        protected override async Task Context()
        {
            await base.Context();

            _populationSimulation = A.Fake <PopulationSimulation>();
            _modelSimulation      = A.Fake <IModelCoreSimulation>();
            _populationData       = A.Fake <DataTable>();
            _runResults           = new PopulationRunResults();
            _simulationRunOptions = new SimulationRunOptions();
            A.CallTo(() => _popExportTask.CreatePopulationDataFor(_populationSimulation, A <bool> ._)).Returns(_populationData);
            A.CallTo(() => _simMapper.MapFrom(_populationSimulation, false)).Returns(_modelSimulation);
            A.CallTo(() => _populationRunner.RunPopulationAsync(_modelSimulation, _populationData, A <DataTable> ._, A <DataTable> ._)).Returns(_runResults);
        }
Ejemplo n.º 10
0
 public JsonSimulationRunner(
     ISimulationExporter simulationExporter,
     IOSPSuiteLogger logger,
     ISnapshotTask snapshotTask
     )
 {
     _simulationExporter   = simulationExporter;
     _logger               = logger;
     _snapshotTask         = snapshotTask;
     _simulationRunOptions = new SimulationRunOptions
     {
         Validate = false,
         CheckForNegativeValues = false,
     };
 }
Ejemplo n.º 11
0
        public InteractiveSimulationRunner(ISimulationSettingsRetriever simulationSettingsRetriever, ISimulationRunner simulationRunner, ICloner cloner, ISimulationAnalysisCreator simulationAnalysisCreator)
        {
            _simulationSettingsRetriever = simulationSettingsRetriever;
            _simulationRunner            = simulationRunner;
            _cloner = cloner;
            _simulationAnalysisCreator = simulationAnalysisCreator;

            _simulationRunOptions = new SimulationRunOptions
            {
                CheckForNegativeValues = true,
                RaiseEvents            = true,
                RunForAllOutputs       = false,
                SimModelExportMode     = SimModelExportMode.Optimized
            };
        }
Ejemplo n.º 12
0
        protected override Task Context()
        {
            _simulationEngine             = A.Fake <IIndividualSimulationEngine>();
            _popSimulationEngine          = A.Fake <IPopulationSimulationEngine>();
            _simulationEngineFactory      = A.Fake <ISimulationEngineFactory>();
            _simulationPersistableUpdater = A.Fake <ISimulationPersistableUpdater>();
            _lazyLoadTask = A.Fake <ILazyLoadTask>();
            _entityTask   = A.Fake <IEntityValidationTask>();
            A.CallTo(() => _simulationEngineFactory.Create <PopulationSimulation, PopulationRunResults>()).Returns(_popSimulationEngine);
            A.CallTo(() => _simulationEngineFactory.Create <IndividualSimulation, SimulationRunResults>()).Returns(_simulationEngine);

            sut = new SimulationRunner(_simulationEngineFactory, _lazyLoadTask, _entityTask, _simulationPersistableUpdater);

            _simulationRunOptions = new SimulationRunOptions();
            return(_completed);
        }
        protected override Task Context()
        {
            _simModelManager = A.Fake <ISimModelManager>();
            _progressUpdater = A.Fake <IProgressUpdater>();
            _progressManager = A.Fake <IProgressManager>();
            _eventPublisher  = A.Fake <IEventPublisher>();
            _simulationResultsSynchronizer = A.Fake <ISimulationResultsSynchronizer>();
            _modelCoreSimulationMapper     = A.Fake <ISimulationToModelCoreSimulationMapper>();

            sut = new IndividualSimulationEngine(_simModelManager, _progressManager, _simulationResultsSynchronizer,
                                                 _eventPublisher, _modelCoreSimulationMapper);

            A.CallTo(() => _progressManager.Create()).Returns(_progressUpdater);
            _simulationRunOption = new SimulationRunOptions {
                RaiseEvents = true
            };
            return(_completed);
        }
Ejemplo n.º 14
0
        public override void GlobalContext()
        {
            base.GlobalContext();
            _compound   = DomainFactoryForSpecs.CreateStandardCompound().WithName("Parent");
            _metabolite = DomainFactoryForSpecs.CreateStandardCompound().WithName("Metabolite");
            _individual = DomainFactoryForSpecs.CreateStandardIndividual();
            _protocol   = DomainFactoryForSpecs.CreateStandardIVBolusProtocol();

            _cyp3A4 = AddEnzymeTo(_individual, "CYP3A4");
            _parentMetabolizationCYP3A4 = AddEnzymaticProcess(_compound, _cyp3A4);

            _cyp2D6 = AddEnzymeTo(_individual, "CYP2D6");
            _parentMetabolizationCYP2D6 = AddEnzymaticProcess(_compound, _cyp2D6);

            _simulationRunOptions = new SimulationRunOptions {
                RaiseEvents = false
            };
        }
        public async Task <SimulationMapping> ExportSimulation(Simulation simulation, ExportRunOptions exportRunOptions, PKSimProject project)
        {
            var projectName      = project.Name;
            var simulationName   = simulation.Name;
            var simulationFile   = FileHelper.RemoveIllegalCharactersFrom(simulationName);
            var simulationFolder = Path.Combine(exportRunOptions.OutputFolder, simulationFile);

            DirectoryHelper.CreateDirectory(simulationFolder);

            var simulationExport = new SimulationMapping
            {
                Project        = projectName,
                Path           = simulationFolder,
                Simulation     = simulationName,
                SimulationFile = simulationFile
            };

            var simulationRunOptions = new SimulationRunOptions();

            _lazyLoadTask.Load(simulation);
            _lazyLoadTask.LoadResults(simulation);

            var simulationExportOptions = new SimulationExportOptions
            {
                OutputFolder = simulationFolder,
                ExportMode   = exportRunOptions.ExportMode,
                ProjectName  = projectName,
            };

            if (exportRunOptions.RunSimulation)
            {
                await _simulationExporter.RunAndExport(simulation, simulationRunOptions, simulationExportOptions);
            }

            else
            {
                await _simulationExporter.Export(simulation, simulationExportOptions);
            }

            _logger.AddDebug($"Simulation '{simulationName}' exported to '{simulationFolder}'", projectName);
            return(simulationExport);
        }
Ejemplo n.º 16
0
        public override void GlobalContext()
        {
            base.GlobalContext();

            _compound   = DomainFactoryForSpecs.CreateStandardCompound();
            _individual = DomainFactoryForSpecs.CreateStandardIndividual();
            _protocol   = DomainFactoryForSpecs.CreateStandardIVBolusProtocol();

            var cmRepo = IoC.Resolve <ICalculationMethodRepository>();
            var intestinalPermGroup     = _compound.ParameterAlternativeGroup(CoreConstants.Groups.COMPOUND_INTESTINAL_PERMEABILITY);
            var paramAlternativeFactory = IoC.Resolve <IParameterAlternativeFactory>();
            var alternative             = paramAlternativeFactory.CreateAlternativeFor(intestinalPermGroup).WithName(_intestinalPermAlternativeName);

            intestinalPermGroup.AddAlternative(alternative);

            _intestinalPermeabilityCalcMethods = cmRepo.Where(cm => cm.Category.Equals(CoreConstants.Category.IntestinalPermeability)).ToList();
            _alternativeIntestinalPermParam    = alternative.Parameter(CoreConstants.Parameters.SPECIFIC_INTESTINAL_PERMEABILITY);
            _simulationEngine     = IoC.Resolve <IIndividualSimulationEngine>();
            _simulationRunOptions = new SimulationRunOptions();
        }
Ejemplo n.º 17
0
        public Task RunSimulation(Simulation simulation, SimulationRunOptions simulationRunOptions = null)
        {
            _simulationRunOptions = simulationRunOptions ?? new SimulationRunOptions();
            _lazyLoadTask.Load(simulation);

            if (_simulationRunOptions.Validate && !_entityValidationTask.Validate(simulation))
            {
                return(_simulationDidNotRun);
            }

            switch (simulation)
            {
            case IndividualSimulation individualSimulation:
                return(runSimulation(individualSimulation, _simulationPersistableUpdater.UpdatePersistableFromSettings));

            case PopulationSimulation populationSimulation:
                return(runSimulation(populationSimulation, _simulationPersistableUpdater.UpdatePersistableFromSettings));
            }
            return(_simulationDidNotRun);
        }
Ejemplo n.º 18
0
        private Task runSimulation(IndividualSimulation simulation, SimulationRunOptions simulationRunOptions)
        {
            return(Task.Run(() =>
            {
                var modelCoreSimulation = _modelCoreSimulationMapper.MapFrom(simulation, shouldCloneModel: false);
                var simResults = _simModelManager.RunSimulation(modelCoreSimulation, simulationRunOptions);

                if (!simResults.Success)
                {
                    return;
                }

                _simulationResultsSynchronizer.Synchronize(simulation, simResults.Results);
                updateResultsName(simulation);

                simulation.ClearPKCache();

                raiseEvent(new SimulationResultsUpdatedEvent(simulation));
            }));
        }
Ejemplo n.º 19
0
        public async Task RunAsync(PopulationSimulation populationSimulation, SimulationRunOptions simulationRunOptions)
        {
            _progressUpdater = _progressManager.Create();
            _progressUpdater.Initialize(populationSimulation.NumberOfItems, PKSimConstants.UI.Calculating);
            _populationRunner.NumberOfCoresToUse = _userSettings.MaximumNumberOfCoresToUse;

            var begin = SystemTime.UtcNow();

            try
            {
                var populationData      = _populationExporter.CreatePopulationDataFor(populationSimulation);
                var modelCoreSimulation = _modelCoreSimulationMapper.MapFrom(populationSimulation, shouldCloneModel: false);

                _eventPublisher.PublishEvent(new SimulationRunStartedEvent());
                var populationRunResults = await _populationRunner.RunPopulationAsync(modelCoreSimulation, populationData, populationSimulation.AgingData.ToDataTable());

                _simulationResultsSynchronizer.Synchronize(populationSimulation, populationRunResults.Results);
                _populationSimulationAnalysisSynchronizer.UpdateAnalysesDefinedIn(populationSimulation);
                _eventPublisher.PublishEvent(new SimulationResultsUpdatedEvent(populationSimulation));
            }
            catch (OperationCanceledException)
            {
                simulationTerminated();
            }
            catch (Exception)
            {
                simulationTerminated();
                throw;
            }
            finally
            {
                var end       = SystemTime.UtcNow();
                var timeSpent = end - begin;
                _eventPublisher.PublishEvent(new SimulationRunFinishedEvent(populationSimulation, timeSpent));
            }
        }