public void Compute(IMoBiSimulation simulation) { var buildConfiguration = _buildConfigurationFactory.CreateFromReferencesUsedIn(simulation.MoBiBuildConfiguration); buildConfiguration.ShowProgress = false; _logger.AddDebug("Creating new simulation from loaded building blocks"); var results = _modelConstructor.CreateModelFrom(buildConfiguration, "BatchRun"); if (results.IsInvalid) { _logger.AddWarning(results.ValidationResult.Messages.SelectMany(x => x.Details).ToString()); } var newSimulation = new MoBiSimulation { BuildConfiguration = buildConfiguration, Model = results.Model, Id = "Sim" }; _context.Register(newSimulation); _logger.AddDebug("Running simulation"); _simModelManager.RunSimulation(newSimulation); }
public Model.Individual MapFrom(Individual batchIndividual) { var batchOriginData = new OriginData { Species = batchIndividual.Species, Population = batchIndividual.Population, Gender = batchIndividual.Gender, Age = batchIndividual.Age.GetValueOrDefault(double.NaN), Height = batchIndividual.Height.GetValueOrDefault(double.NaN), Weight = batchIndividual.Weight.GetValueOrDefault(double.NaN), GestationalAge = batchIndividual.GestationalAge.GetValueOrDefault(double.NaN), }; var originData = _originDataMapper.MapFrom(batchOriginData); var individual = _individualFactory.CreateAndOptimizeFor(originData, batchIndividual.Seed).WithName("Individual"); batchIndividual.Enzymes.Each(enzyme => addMoleculeTo <IndividualEnzyme>(individual, enzyme)); batchIndividual.OtherProteins.Each(otherProtein => addMoleculeTo <IndividualOtherProtein>(individual, otherProtein)); batchIndividual.Transporters.Each(transporter => { var individualTransporter = addMoleculeTo <IndividualTransporter>(individual, transporter); individualTransporter.TransportType = EnumHelper.ParseValue <TransportType>(transporter.TransportType); _batchLogger.AddDebug($"Transport type for transporter '{individualTransporter.Name}' is {individualTransporter.TransportType}"); }); return(individual); }
public async Task RunAndExport(IndividualSimulation simulation, string outputFolder, string exportFileName, SimulationConfiguration simulationConfiguration, BatchExportMode batchExportMode) { _logger.AddDebug($"------> Running simulation '{exportFileName}'"); var engine = _simulationEngineFactory.Create <IndividualSimulation>(); await engine.RunForBatch(simulation, simulationConfiguration.CheckForNegativeValues); await exportResults(simulation, outputFolder, exportFileName, batchExportMode); }
public async Task RunBatch(dynamic parameters) { string inputFolder = parameters.inputFolder; string outputFolder = parameters.outputFolder; string logFileFullPath = parameters.logFileFullPath; BatchExportMode exportMode = parameters.exportMode; NotificationType notificationType = parameters.notificationType; clear(); using (new BatchLoggerDisposer(_logger, logFileFullPath, notificationType)) { _logger.AddInSeparator($"Starting batch run: {DateTime.Now.ToIsoFormat()}", NotificationType.Info); var inputDirectory = new DirectoryInfo(inputFolder); if (!inputDirectory.Exists) { throw new ArgumentException($"Input folder '{inputFolder}' does not exist"); } var allSimulationFiles = inputDirectory.GetFiles(CoreConstants.Filter.JSON_FILTER); if (allSimulationFiles.Length == 0) { throw new ArgumentException($"No simulation json file found in '{inputFolder}'"); } var outputDirectory = new DirectoryInfo(outputFolder); if (!outputDirectory.Exists) { _logger.AddDebug($"Creating folder '{outputFolder}'"); outputDirectory.Create(); } var allExistingFiles = outputDirectory.GetFiles("*.csv"); allExistingFiles.Each(outputFile => { FileHelper.DeleteFile(outputFile.FullName); _logger.AddDebug($"Deleting file '{outputFile.FullName}'"); }); var begin = DateTime.UtcNow; _logger.AddInfo($"Found {allSimulationFiles.Length} files in '{inputFolder}'"); foreach (var simulationFile in allSimulationFiles) { await exportSimulationTo(simulationFile, outputFolder, exportMode); } var end = DateTime.UtcNow; var timeSpent = end - begin; _logger.AddInSeparator($"{_allSimulationNames.Count} simulations calculated and exported in '{timeSpent.ToDisplay()}'", NotificationType.Info); createSummaryFilesIn(outputDirectory); _logger.AddInSeparator($"Batch run finished: {DateTime.Now.ToIsoFormat()}", NotificationType.Info); } }
public ModelProperties MapFrom(SimulationConfiguration configuration, Model.Individual individual) { var modelProperties = _modelPropertiesTask.DefaultFor(individual.OriginData, configuration.Model); _batchLogger.AddDebug($"Using model method '{configuration.Model}'"); return(modelProperties); }
private async Task <bool> exportSimulation(DirectoryInfo originalDirectory, DirectoryInfo afterConversionDirectory, DirectoryInfo newDirectory, DirectoryInfo pkmlOldDirectory, DirectoryInfo pkmlNewDirectory, IndividualSimulation simulation, IPKSimProject project) { _lazyLoadTask.Load(simulation); if (!simulation.HasResults) { return(warn($"No results found for simulation '{simulation.Name}'")); } if (!simulation.HasUpToDateResults) { return(warn($"Results not up to date in simulation '{simulation.Name}'")); } if (simulation.IsImported) { return(warn($"Imported simulation '{simulation.Name}' cannot be exported")); } await exportSimulationToPkml(pkmlOldDirectory, project.Name, simulation); await exportSimulationResultsToCSV(originalDirectory, project.Name, simulation, "old"); await runSimulation(simulation); await exportSimulationResultsToCSV(afterConversionDirectory, project.Name, simulation, "new"); IndividualSimulation otherSimulation = null; try { _logger.AddDebug($"Creating simulation based on old building blocks for '{simulation.Name}'"); otherSimulation = await createNewSimulationBasedOn(simulation, project); await runSimulation(simulation); await exportSimulationResultsToCSV(newDirectory, project.Name, otherSimulation, "new from old bb"); await exportSimulationToPkml(pkmlNewDirectory, project.Name, simulation); } finally { _executionContext.Unregister(otherSimulation); } return(true); }
public Protocol MapFrom(ApplicationProtocol batchProtocol) { var applicationType = ApplicationTypes.ByName(batchProtocol.ApplicationType); var dosingIntervalId = EnumHelper.ParseValue <DosingIntervalId>(batchProtocol.DosingInterval); var simpleProtocol = _protocolFactory.Create(ProtocolMode.Simple, applicationType).DowncastTo <SimpleProtocol>(); simpleProtocol.Name = batchProtocol.Name ?? "Protocol"; simpleProtocol.EndTimeParameter.Value = batchProtocol.EndTime; simpleProtocol.Dose.DisplayUnit = simpleProtocol.Dose.Dimension.Unit(batchProtocol.DoseUnit); simpleProtocol.Dose.Value = simpleProtocol.Dose.Dimension.UnitValueToBaseUnitValue(simpleProtocol.Dose.DisplayUnit, batchProtocol.Dose); simpleProtocol.DosingInterval = DosingIntervals.ById(dosingIntervalId); _logger.AddDebug($"Application Type = {applicationType.Name}"); _logger.AddDebug($"Dosing Interval = {simpleProtocol.DosingInterval.DisplayName}"); _logger.AddDebug($"Application Dose = {simpleProtocol.Dose.Value} [{simpleProtocol.Dose.DisplayUnit}]"); _logger.AddDebug($"Application End Time = {simpleProtocol.EndTime}"); return(simpleProtocol); }
private void addPartialProcesses <TPartialProcess, TIndividualMolecule, TProcessSelection>(Model.Compound compound, Model.Individual individual, ProcessSelectionGroup processSelectionGroup) where TPartialProcess : Model.PartialProcess where TIndividualMolecule : IndividualMolecule where TProcessSelection : ProcessSelection, new() { //default mapping with processes: Mapping done only by name foreach (var process in compound.AllProcesses <TPartialProcess>()) { var molecule = individual.MoleculeByName <TIndividualMolecule>(process.MoleculeName); //enzyme not found in individual if (molecule == null) { _batchLogger.AddDebug($"Molecule '{process.MoleculeName}' not found in individual but is defined in compound for process '{process.Name}'"); continue; } _batchLogger.AddDebug($"Adding process {process.Name} for molecule {molecule.Name}"); processSelectionGroup.AddPartialProcessSelection(new TProcessSelection { ProcessName = process.Name, MoleculeName = molecule.Name, CompoundName = compound.Name }); } }
public ModelCompound MapFrom(BatchCompound batchCompound) { var compound = _compoundFactory.Create(); compound.Name = batchCompound.Name; setValue(compound, CoreConstants.Groups.COMPOUND_LIPOPHILICITY, CoreConstants.Parameter.LIPOPHILICITY, batchCompound.Lipophilicity); setValue(compound, CoreConstants.Groups.COMPOUND_FRACTION_UNBOUND, CoreConstants.Parameter.FractionUnbound, batchCompound.FractionUnbound); setValue(compound, Constants.Parameters.MOL_WEIGHT, batchCompound.MolWeight); setValue(compound, CoreConstants.Parameter.CL, batchCompound.Cl); setValue(compound, CoreConstants.Parameter.F, batchCompound.F); setValue(compound, CoreConstants.Parameter.BR, batchCompound.Br); setValue(compound, CoreConstants.Parameter.I, batchCompound.I); setValue(compound, CoreConstants.Parameter.IS_SMALL_MOLECULE, batchCompound.IsSmallMolecule ? 1 : 0); setValue(compound, CoreConstants.Groups.COMPOUND_SOLUBILITY, CoreConstants.Parameter.SolubilityAtRefpH, batchCompound.SolubilityAtRefpH); setValue(compound, CoreConstants.Groups.COMPOUND_SOLUBILITY, CoreConstants.Parameter.RefpH, batchCompound.RefpH); for (int i = 0; i < batchCompound.PkaTypes.Count; i++) { setPka(compound, batchCompound.PkaTypes[i], i); } foreach (var batchSystemicProcess in batchCompound.SystemicProcesses) { var systemicProcess = retrieveProcessFrom <Model.SystemicProcess>(batchSystemicProcess); if (systemicProcess == null) { continue; } systemicProcess.SystemicProcessType = SystemicProcessTypes.ById(batchSystemicProcess.SystemicProcessType); systemicProcess.RefreshName(); compound.AddProcess(systemicProcess); } foreach (var batchPartialProcess in batchCompound.PartialProcesses) { var partialProcess = retrieveProcessFrom <Model.PartialProcess>(batchPartialProcess); if (partialProcess == null) { continue; } partialProcess.MoleculeName = batchPartialProcess.MoleculeName; partialProcess.RefreshName(); compound.AddProcess(partialProcess); } //Update default with available CM in configuration foreach (var calculationMethodName in batchCompound.CalculationMethods) { var calculationMethod = _calculationMethodRepository.FindByName(calculationMethodName); if (calculationMethod == null) { _batchLogger.AddWarning($"Calculation method '{calculationMethodName}' not found"); continue; } var category = _calculationMethodCategoryRepository.FindByName(calculationMethod.Category); if (category == null) { _batchLogger.AddWarning($"Could not find compound category '{calculationMethod.Category}' for calculation method '{calculationMethodName}."); continue; } //this is a compound calculationmethod. Swap them out var existingCalculationMethod = compound.CalculationMethodFor(category); compound.RemoveCalculationMethod(existingCalculationMethod); compound.AddCalculationMethod(calculationMethod); _batchLogger.AddDebug($"Using calculation method '{calculationMethod.Name}' instead of '{existingCalculationMethod.Name}' for category '{calculationMethod.Category}'"); } return(compound); }
public SimulationForBatch MapFrom(Simulation batchSimulation) { var individual = _individualMapper.MapFrom(batchSimulation.Individual); var compounds = batchSimulation.Compounds.Select(_compoundMapper.MapFrom).ToList(); var protocols = batchSimulation.ApplicationProtocols.Select(_protocolMapper.MapFrom).ToList(); var protocolForCompound = new Cache <Model.Compound, Protocol>(); foreach (var applicationProtocol in batchSimulation.ApplicationProtocols) { var protocol = _protocolMapper.MapFrom(applicationProtocol); var compound = compounds.FindByName(applicationProtocol.CompoundName); if (compound != null) { protocolForCompound.Add(compound, protocol); } } //if protocol for compound is not empty, that means that name were specified explictely in json file and we should use that var protocolToUse = protocolForCompound.Any() ? protocolForCompound.ToList() : protocols; //a requirement is that compounds and protocols have the same length. Fill missing entries with null while (protocolToUse.Count < compounds.Count) { protocolToUse.Add(null); } var modelProperties = _modelPropertiesMapper.MapFrom(batchSimulation.Configuration, individual); var formulation = _formulationMapper.MapFrom(batchSimulation.Formulation); var simulationConstruction = new SimulationConstruction { SimulationSubject = individual, TemplateCompounds = compounds, TemplateProtocols = protocols, TemplateFormulation = formulation, ModelProperties = modelProperties, SimulationName = Simulation.Name, AllowAging = batchSimulation.Configuration.AllowAging, Interactions = batchSimulation.Interactions }; var simulation = _simulationConstructor .CreateSimulation(simulationConstruction) .DowncastTo <IndividualSimulation>(); var config = batchSimulation.Configuration; var interval = simulation.OutputSchema.Intervals.First(); //remove old ones foreach (var otherInterval in simulation.OutputSchema.Intervals.Skip(1).ToList()) { simulation.OutputSchema.RemoveInterval(otherInterval); } interval.StartTime.Value = config.StartTime; interval.EndTime.Value = config.EndTime; interval.Resolution.Value = config.Resolution; simulation.Solver.AbsTol = config.AbsTol; simulation.Solver.RelTol = config.RelTol; _logger.AddDebug($"Start Time = {config.StartTime}"); _logger.AddDebug($"End Time = {config.EndTime}"); _logger.AddDebug($"Resolution = {config.Resolution}"); _logger.AddDebug($"AbsTol = {config.AbsTol}"); _logger.AddDebug($"RelTol = {config.RelTol}"); _logger.AddDebug($"UseJacobian = {config.UseJacobian}"); var simForBatch = new SimulationForBatch { Simulation = simulation, Configuration = config }; simForBatch.ParameterVariationSets.AddRange(batchSimulation.ParameterVariationSets); return(simForBatch); }