From 47a24a7fbdd25579d7033f8f6c245cb1f0aeaba3 Mon Sep 17 00:00:00 2001 From: Michael Sevestre Date: Wed, 9 Feb 2022 19:13:04 +0100 Subject: [PATCH] Fixes #1755 aucr cmax r not calculated properly --- src/PKSim.Core/Model/Simulation.cs | 4 +- .../Services/GlobalPKAnalysisTask.cs | 21 ++++++---- .../Core/GlobalPKAnalysisTaskSpecs.cs | 40 ++++++++++++------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/PKSim.Core/Model/Simulation.cs b/src/PKSim.Core/Model/Simulation.cs index 8f9a2b790..e78d97406 100644 --- a/src/PKSim.Core/Model/Simulation.cs +++ b/src/PKSim.Core/Model/Simulation.cs @@ -444,11 +444,11 @@ public virtual void UpdateFromOriginalSimulation(Simulation originalSimulation) } /// - /// Returns the endtime of the simulation in kernel unit + /// Returns the end time of the simulation in kernel unit /// public virtual double? EndTime { - get { return OutputSchema.Intervals.Select(x => x.EndTime.Value).Max(); } + get { return OutputSchema?.Intervals.Select(x => x.EndTime.Value).Max(); } } /// diff --git a/src/PKSim.Core/Services/GlobalPKAnalysisTask.cs b/src/PKSim.Core/Services/GlobalPKAnalysisTask.cs index 04b598218..fb92dffb6 100644 --- a/src/PKSim.Core/Services/GlobalPKAnalysisTask.cs +++ b/src/PKSim.Core/Services/GlobalPKAnalysisTask.cs @@ -1,14 +1,14 @@ using System.Collections.Generic; using System.Linq; -using PKSim.Assets; +using OSPSuite.Core.Domain; +using OSPSuite.Core.Domain.Data; +using OSPSuite.Core.Domain.Services; using OSPSuite.Utility.Extensions; using OSPSuite.Utility.Validation; +using PKSim.Assets; using PKSim.Core.Mappers; using PKSim.Core.Model; using PKSim.Core.Model.Extensions; -using OSPSuite.Core.Domain; -using OSPSuite.Core.Domain.Data; -using OSPSuite.Core.Domain.Services; using IParameterFactory = PKSim.Core.Model.IParameterFactory; namespace PKSim.Core.Services @@ -123,10 +123,10 @@ public GlobalPKAnalysis CalculateGlobalPKAnalysisFor(IEnumerable sim continue; } - var schemaItem = singleDosingItem(simulation, compound); var pkValues = new List(); var bioAvailabilityCalculated = !double.IsNaN(bioAvailabilityValue); + var schemaItem = singleDosingItem(simulation, compound); if (isIntravenous(schemaItem)) { container.AddChildren(vssPlasma, vdPlasma, vssPhysChem, totalPlasmaCL); @@ -319,17 +319,23 @@ private bool isMultipleOral(Simulation simulation, Compound compound) private static bool isOral(ISchemaItem schemaItem) { + if (schemaItem == null) + return false; + return schemaItem.ApplicationType == ApplicationTypes.Oral; } private static bool isIntravenous(ISchemaItem schemaItem) { + if (schemaItem == null) + return false; + return schemaItem.ApplicationType == ApplicationTypes.Intravenous || schemaItem.ApplicationType == ApplicationTypes.IntravenousBolus; } private ApplicationType applicationTypeFor(Simulation simulation, Compound compound) { - var numberOfApplications = schemaItemsFrom(simulation, compound).Count; + var numberOfApplications = simulation.AllApplicationParametersOrderedByStartTimeFor(compound.Name).Count; if (numberOfApplications == 1) return ApplicationType.Single; @@ -338,7 +344,8 @@ private ApplicationType applicationTypeFor(Simulation simulation, Compound compo private ISchemaItem singleDosingItem(Simulation simulation, Compound compound) { - return schemaItemsFrom(simulation, compound)[0]; + //this may be null for compound created by metabolization process only + return schemaItemsFrom(simulation, compound).FirstOrDefault(); } private IReadOnlyList schemaItemsFrom(Simulation simulation, Compound compound) diff --git a/tests/PKSim.Tests/Core/GlobalPKAnalysisTaskSpecs.cs b/tests/PKSim.Tests/Core/GlobalPKAnalysisTaskSpecs.cs index a3dfc2835..1514c2cf3 100644 --- a/tests/PKSim.Tests/Core/GlobalPKAnalysisTaskSpecs.cs +++ b/tests/PKSim.Tests/Core/GlobalPKAnalysisTaskSpecs.cs @@ -1,16 +1,16 @@ using System; -using System.Collections.Generic; +using FakeItEasy; using OSPSuite.BDDHelper; using OSPSuite.BDDHelper.Extensions; -using FakeItEasy; -using PKSim.Core.Mappers; -using PKSim.Core.Model; -using PKSim.Core.Services; using OSPSuite.Core.Domain; +using OSPSuite.Core.Domain.Builder; using OSPSuite.Core.Domain.Data; using OSPSuite.Core.Domain.Formulas; using OSPSuite.Core.Domain.Services; using OSPSuite.Core.Domain.UnitSystem; +using PKSim.Core.Mappers; +using PKSim.Core.Model; +using PKSim.Core.Services; using IParameterFactory = PKSim.Core.Model.IParameterFactory; using IPKAnalysesTask = PKSim.Core.Services.IPKAnalysesTask; using IPKCalculationOptionsFactory = PKSim.Core.Services.IPKCalculationOptionsFactory; @@ -36,10 +36,12 @@ public abstract class concern_for_GlobalPKAnalysisTask : ContextSpecification _simulationSchemaItems; private IInteractionTask _interactionTask; protected Compound _compound; private ICloner _cloner; + protected EventGroup _eventGroup; + private Container _application1; + protected Container _application2; protected override void Context() { @@ -65,10 +67,8 @@ protected override void Context() _compound = new Compound().WithName(_compoundName); _compoundProperties = new CompoundProperties {Compound = _compound}; - _simulationSchemaItems = new List(); _protocol = new SimpleProtocol(); _compoundProperties.ProtocolProperties.Protocol = _protocol; - A.CallTo(() => _protocolMapper.MapFrom(_protocol)).Returns(_simulationSchemaItems); _simulation = new IndividualSimulation {Properties = new SimulationProperties()}; @@ -76,7 +76,19 @@ protected override void Context() _simulation.AddUsedBuildingBlock(new UsedBuildingBlock("CompId", PKSimBuildingBlockType.Compound) {BuildingBlock = _compound}); _simulation.AddUsedBuildingBlock(new UsedBuildingBlock("IndividualId", PKSimBuildingBlockType.Individual) {BuildingBlock = _individual}); _simulation.DataRepository = new DataRepository {_venousBloodPlasma, _peripheralVenousBloodPlasma}; - + _simulation.SimulationSettings = new SimulationSettings(); + _simulation.OutputSchema = new OutputSchema(); + _simulation.OutputSchema.AddInterval(new OutputInterval{DomainHelperForSpecs.ConstantParameterWithValue(100).WithName(Constants.Parameters.END_TIME) }); + _simulation.Model = new OSPSuite.Core.Domain.Model {Root = new Container()}; + _eventGroup = new EventGroup(); + _application1 = new Container().WithName("App1").WithContainerType(ContainerType.Application); + _application1.Add(new MoleculeAmount().WithName(_compoundName)); + _application1.Add(DomainHelperForSpecs.ConstantParameterWithValue(10).WithName(Constants.Parameters.START_TIME)); + _application2 = new Container().WithName("App2").WithContainerType(ContainerType.Application); + _application2.Add(DomainHelperForSpecs.ConstantParameterWithValue(10).WithName(Constants.Parameters.START_TIME)); + _application2.Add(new MoleculeAmount().WithName(_compoundName)); + _simulation.Model.Root.Add(_eventGroup); + _eventGroup.Add(_application1); _venousBloodPK = new PKValues(); _venousBloodPK.AddValue(Constants.PKParameters.Vss, 10); _venousBloodPK.AddValue(Constants.PKParameters.Vd, 11); @@ -113,7 +125,7 @@ protected DataColumn CalculationColumnFor(BaseGrid baseGrid, string organ, strin } } - public class When_calculating_the_global_pk_analyes_parameter_such_as_VSS_VD_and_Plasma_CL_for_the_human_species : concern_for_GlobalPKAnalysisTask + public class When_calculating_the_global_pk_analyses_parameter_such_as_VSS_VD_and_Plasma_CL_for_the_human_species : concern_for_GlobalPKAnalysisTask { private GlobalPKAnalysis _results; @@ -309,7 +321,9 @@ protected override void Context() var startTime = DomainHelperForSpecs.ConstantParameterWithValue(3); A.CallTo(() => schemaItem.Dose).Returns(inputDose); A.CallTo(() => schemaItem.StartTime).Returns(startTime); - _simulationSchemaItems.Add(schemaItem); + + A.CallTo(() => _protocolMapper.MapFrom(_protocol)).Returns(new []{schemaItem, }); + } protected override void Because() @@ -361,8 +375,6 @@ protected override void Context() A.CallTo(() => _globalPKAnalysisRunner.RunForDDIRatio(_simulation)).Returns(_ddiRatioSimulation); A.CallTo(() => _pkAnalysisTask.CalculateFor(_ddiRatioSimulation, _peripheralVenousBloodPlasma)).Returns(new IndividualPKAnalysis(_peripheralVenousBloodPlasma, _pkAnalysis)); - //single dosing - _simulationSchemaItems.Add(new SchemaItem()); } protected override void Because() @@ -444,7 +456,7 @@ protected override void Context() A.CallTo(() => _globalPKAnalysisRunner.RunForDDIRatio(_simulation)).Returns(_ddiRatioSimulation); A.CallTo(() => _pkAnalysisTask.CalculateFor(_ddiRatioSimulation, _peripheralVenousBloodPlasma)).Returns(new IndividualPKAnalysis(_peripheralVenousBloodPlasma, _pkAnalysis)); //multiple dosing - _simulationSchemaItems.AddRange(new[] {new SchemaItem(), new SchemaItem()}); + _eventGroup.Add(_application2); } protected override void Because()