public PKCalculationOptions CreateFor(ISimulation simulation, string moleculeName) { var options = new PKCalculationOptions(); var endTime = simulation.EndTime ?? 0; var allApplicationParameters = AllApplicationParametersOrderedByStartTimeFor(simulation, moleculeName); // all application start times starting before the end of the simulation var applicationStartTimes = allApplicationParameters.Select(x => x.StartTime.Value).ToFloatArray(); options.FirstDosingStartValue = applicationStartTimes.FirstOrDefault(); // single dosing if (applicationStartTimes.Length <= 1) { options.FirstDosingEndValue = endTime.ToFloat(); options.InfusionTime = allApplicationParameters.FirstOrDefault()?.InfusionTime?.Value; } else { // 1 because we want the start time of the second application options.FirstDosingEndValue = applicationStartTimes[1]; options.LastMinusOneDosingStartValue = applicationStartTimes[applicationStartTimes.Length - 2]; options.LastDosingStartValue = applicationStartTimes[applicationStartTimes.Length - 1]; options.LastDosingEndValue = endTime.ToFloat(); } // Once all dosing are defined, update applied dose UpdateAppliedDose(simulation, moleculeName, options, allApplicationParameters); return(options); }
public DosingInterval(List <float> time, List <float> conc, PKCalculationOptions options) { _options = options; _time = time; _concentration = conc; _timeSteps = ArrayHelper.TimeStepsFrom(time); _polyFit = new PolyFit(); }
public PKInterval(List <float> time, List <float> concentration, PKCalculationOptions options, double?drugMassPerBodyWeight = null, double?concentrationThreshold = null) { _options = options; DrugMassPerBodyWeight = drugMassPerBodyWeight; _time = time; _concentration = concentration; _concentrationThreshold = concentrationThreshold; _timeSteps = ArrayHelper.TimeStepsFrom(time); _polyFit = new PolyFit(); }
private void addPKParametersForOutput( IModelCoreSimulation simulation, SimulationResults simulationResults, Action <int> performIndividualScalingAction, QuantitySelection selectedQuantity, PopulationSimulationPKAnalyses popAnalyses, string moleculeName, PKCalculationOptions pkCalculationOptions, IReadOnlyList <ApplicationParameters> allApplicationParameters) { var allPKParameters = _pkParameterRepository.All().Where(p => PKParameterCanBeUsed(p, pkCalculationOptions)).ToList(); var allUserDefinedPKParameters = allPKParameters.OfType <UserDefinedPKParameter>().ToList(); //create pk parameter for each pk parameters (predefined and dynamic) foreach (var pkParameter in allPKParameters) { var quantityPKParameter = new QuantityPKParameter { Name = pkParameter.Name, QuantityPath = selectedQuantity.Path, Dimension = pkParameter.Dimension }; popAnalyses.AddPKAnalysis(quantityPKParameter); } //add the values for each individual foreach (var individualResult in simulationResults.AllIndividualResults) { performIndividualScalingAction(individualResult.IndividualId); _pkCalculationOptionsFactory.UpdateTotalDrugMassPerBodyWeight(simulation, moleculeName, pkCalculationOptions, allApplicationParameters); var values = individualResult.QuantityValuesFor(selectedQuantity.Path); //This can happen is the results do not match the simulation if (values == null) { continue; } var pkValues = _pkValuesCalculator.CalculatePK(individualResult.Time.Values, values.Values, pkCalculationOptions, allUserDefinedPKParameters); foreach (var quantityPKParameter in popAnalyses.AllPKParametersFor(selectedQuantity.Path)) { quantityPKParameter.SetValue(individualResult.IndividualId, pkValues.ValueOrDefaultFor(quantityPKParameter.Name)); } } }
public PKCalculationOptions CreateFor(IModelCoreSimulation simulation, string moleculeName) { var options = new PKCalculationOptions(); var endTime = (simulation.EndTime ?? 0).ToFloat(); var allApplicationParameters = simulation.AllApplicationParametersOrderedByStartTimeFor(moleculeName); // all application start times starting before the end of the simulation var applicationStartTimes = allApplicationParameters.Select(x => x.StartTime.Value).ToFloatArray(); var applicationEndTimes = new List <float>(applicationStartTimes.Skip(1)) { endTime }; for (int i = 0; i < applicationStartTimes.Length; i++) { var dosingInterval = new DosingInterval { StartValue = applicationStartTimes[i], EndValue = applicationEndTimes[i] }; options.AddInterval(dosingInterval); } // single dosing if (applicationStartTimes.Length <= 1) { options.InfusionTime = allApplicationParameters.FirstOrDefault()?.InfusionTime?.Value; } // Once all dosing are defined, update total drug mass UpdateTotalDrugMassPerBodyWeight(simulation, moleculeName, options, allApplicationParameters); return(options); }
private void setMultipleDosingPKValues(ICache <string, double> pk, List <DosingInterval> allIntervals, PKCalculationOptions options) { var firstInterval = allIntervals[FIRST_INTERVAL]; var lastMinusOneInterval = allIntervals[LAST_MINUS_ONE_INTERVAL]; var lastInterval = allIntervals[LAST_INTERVAL]; var fullInterval = allIntervals[FULL_RANGE_INTERVAL]; setCmaxAndTmax(pk, firstInterval, options.FirstDose, Constants.PKParameters.C_max_t1_t2, Constants.PKParameters.Tmax_t1_t2); setValue(pk, Constants.PKParameters.Ctrough_t2, firstInterval.CTrough); setValueAndNormalize(pk, Constants.PKParameters.AUC_t1_t2, firstInterval.Auc, options.FirstDose); setValueAndNormalize(pk, Constants.PKParameters.AUC_inf_t1, firstInterval.AucInf, options.FirstDose); setFirstIntervalPKValues(pk, firstInterval, options.FirstDose); setValueAndNormalize(pk, Constants.PKParameters.AUC_tLast_minus_1_tLast, lastMinusOneInterval.Auc, options.LastMinusOneDose); setCmaxAndTmax(pk, lastInterval, options.LastDose, Constants.PKParameters.C_max_tLast_tEnd, Constants.PKParameters.Tmax_tLast_tEnd); setValue(pk, Constants.PKParameters.Ctrough_tLast, lastInterval.CTrough); setValue(pk, Constants.PKParameters.Thalf_tLast_tEnd, lastInterval.Thalf); setValueAndNormalize(pk, Constants.PKParameters.AUC_inf_tLast, lastInterval.AucInf, options.LastDose); setCmaxAndTmax(pk, fullInterval, options.Dose, Constants.PKParameters.C_max, Constants.PKParameters.Tmax); }
public PKValues CalculatePK(IReadOnlyList <float> time, IReadOnlyList <float> concentration, PKCalculationOptions options = null) { //we use a cache to avoid conversion problem between double and float. var pk = new Cache <string, double>(); options = checkOptions(options); var allIntervals = allIntervalsFor(time, concentration, options).ToList(); var isSingleDosing = allIntervals.Count == 1; allIntervals.Each(x => x.Calculate()); if (isSingleDosing) { setSingleDosingPKValues(pk, allIntervals[FIRST_INTERVAL], options.Dose); } else if (allIntervals.Any()) { setMultipleDosingPKValues(pk, allIntervals, options); } return(pkValuesFrom(pk)); }
private DosingInterval dosingInterval(List <float> time, List <float> concentration, int startIndex, int endIndex, PKCalculationOptions options) { return(new DosingInterval(ArrayHelper.TruncateArray(time, startIndex, endIndex), ArrayHelper.TruncateArray(concentration, startIndex, endIndex), options)); }
private void addDynamicPKValues(ICache <string, double> pk, IReadOnlyList <UserDefinedPKParameter> dynamicPKParameters, List <float> time, List <float> concentration, PKCalculationOptions options) { foreach (var dynamicPKParameter in dynamicPKParameters) { //No start time specified? Then we assume we want to calculate from the beginning of the time array var startTime = dynamicPKParameter.EstimateStartTimeFrom(options) ?? time.First(); //No end time specified? Then we assume we want to calculate until the end of the time array var endTime = dynamicPKParameter.EstimateEndTimeFrom(options) ?? time.Last(); var startIndex = ArrayHelper.ClosestIndexOf(time, startTime); var endIndex = ArrayHelper.ClosestIndexOf(time, endTime); if (oneTimeIndexInvalid(startIndex, endIndex) || startIndex >= endIndex) { continue; } var drugMassPerBodyWeight = dynamicPKParameter.EstimateDrugMassPerBodyWeight(options) ?? options.TotalDrugMassPerBodyWeight; var pkInterval = createPKInterval(time, concentration, startIndex, endIndex, options, drugMassPerBodyWeight: drugMassPerBodyWeight, concentrationThreshold: dynamicPKParameter.ConcentrationThreshold); var value = pkInterval.ValueFor(dynamicPKParameter.StandardPKParameter, dynamicPKParameter.NormalizationFactor); setValue(pk, dynamicPKParameter.Name, value); } }
public virtual void UpdateTotalDrugMassPerBodyWeight(IModelCoreSimulation simulation, string moleculeName, PKCalculationOptions options, IReadOnlyList <ApplicationParameters> allApplicationParametersOrderedByStartTime) { var bodyWeight = simulation.BodyWeight?.Value; var totalDrugMass = simulation.TotalDrugMassFor(moleculeName); options.TotalDrugMassPerBodyWeight = drugMassPerBodyWeightFor(totalDrugMass, bodyWeight); options.DosingIntervals.Each((x, i) => { x.DrugMassPerBodyWeight = drugMassPerBodyWeightFor(allApplicationParametersOrderedByStartTime[i].DrugMass, bodyWeight); }); }
private void setMultipleDosingPKValues(ICache <string, double> pk, IReadOnlyList <PKInterval> allIntervals, PKCalculationOptions options) { var firstInterval = allIntervals[FIRST_INTERVAL]; var lastMinusOneInterval = allIntervals[LAST_MINUS_ONE_INTERVAL]; var lastInterval = allIntervals[LAST_INTERVAL]; var fullInterval = allIntervals[FULL_RANGE_INTERVAL]; setCmaxAndTmax(pk, firstInterval, Constants.PKParameters.C_max_tD1_tD2, Constants.PKParameters.Tmax_tD1_tD2); setValue(pk, Constants.PKParameters.Ctrough_tD2, firstInterval, x => x.CTrough); setValueAndNormalize(pk, Constants.PKParameters.AUC_tD1_tD2, firstInterval, x => x.AucTend); setValueAndNormalize(pk, Constants.PKParameters.AUC_inf_tD1, firstInterval, x => x.AucInf); setValue(pk, Constants.PKParameters.Thalf, firstInterval, x => x.Thalf); setValue(pk, Constants.PKParameters.MRT, firstInterval, x => x.Mrt); setValueAndNormalize(pk, Constants.PKParameters.AUC_tDLast_minus_1_tDLast, lastMinusOneInterval, x => x.AucTend); setCmaxAndTmax(pk, lastInterval, Constants.PKParameters.C_max_tDLast_tDEnd, Constants.PKParameters.Tmax_tDLast_tDEnd); setValue(pk, Constants.PKParameters.Ctrough_tDLast, lastInterval, x => x.CTrough); setValue(pk, Constants.PKParameters.Thalf_tDLast_tEnd, lastInterval, x => x.Thalf); setValueAndNormalize(pk, Constants.PKParameters.AUC_inf_tDLast, lastInterval, x => x.AucInf); setCmaxAndTmax(pk, fullInterval, Constants.PKParameters.C_max, Constants.PKParameters.Tmax); }
public PKValues CalculatePK(IReadOnlyList <float> time, IReadOnlyList <float> concentration, PKCalculationOptions options = null, IReadOnlyList <UserDefinedPKParameter> userDefinedPKParameters = null) { //we use a cache to avoid conversion problem between double and float. var pk = new Cache <string, double>(); var timeValues = time.ToList(); var concentrationValues = concentration.ToList(); if (!timeValues.Any() || !concentrationValues.Any()) { return(new PKValues()); } options = options ?? new PKCalculationOptions(); var allStandardIntervals = allStandardIntervalsFor(timeValues, concentrationValues, options); var isSingleDosing = allStandardIntervals.Count == 1; allStandardIntervals.Each(x => x.Calculate()); if (isSingleDosing) { setSingleDosingPKValues(pk, allStandardIntervals[FIRST_INTERVAL]); } else if (allStandardIntervals.Any()) { setMultipleDosingPKValues(pk, allStandardIntervals, options); } if (userDefinedPKParameters != null) { addDynamicPKValues(pk, userDefinedPKParameters, timeValues, concentrationValues, options); } return(pkValuesFrom(pk)); }
private PKInterval createPKInterval(List <float> time, List <float> concentration, int startIndex, int endIndex, PKCalculationOptions options, double?drugMassPerBodyWeight = null, double?concentrationThreshold = null) { return(new PKInterval(ArrayHelper.TruncateArray(time, startIndex, endIndex), ArrayHelper.TruncateArray(concentration, startIndex, endIndex), options, drugMassPerBodyWeight, concentrationThreshold)); }
private PKInterval pkIntervalFromDosingInterval(DosingInterval dosingInterval, List <float> time, List <float> concentration, PKCalculationOptions options) { if (dosingInterval == null) { return(null); } var dosingStartIndex = ArrayHelper.ClosestIndexOf(time, dosingInterval.StartValue); var dosingEndIndex = ArrayHelper.ClosestIndexOf(time, dosingInterval.EndValue); if (oneTimeIndexInvalid(dosingStartIndex, dosingEndIndex)) { return(null); } return(createPKInterval(time, concentration, dosingStartIndex, dosingEndIndex, options, dosingInterval.DrugMassPerBodyWeight)); }
private IReadOnlyList <PKInterval> allStandardIntervalsFor(List <float> time, List <float> concentration, PKCalculationOptions options) { var fullRange = new PKInterval(time, concentration, options, drugMassPerBodyWeight: options.TotalDrugMassPerBodyWeight); //only one interval, return the full range if (options.SingleDosing) { return new[] { fullRange } } ; //Two or more intervals var intervals = new[] { pkIntervalFromDosingInterval(options.FirstInterval, time, concentration, options), pkIntervalFromDosingInterval(options.LastMinusOneInterval, time, concentration, options), pkIntervalFromDosingInterval(options.LastInterval, time, concentration, options), fullRange }; return(intervals.Where(x => x != null).ToList()); }
public virtual void UpdateAppliedDose(ISimulation simulation, string moleculeName, PKCalculationOptions options, IReadOnlyList <ApplicationParameters> allApplicationParametersOrderedByStartTime) { options.Dose = simulation.TotalDrugMassPerBodyWeightFor(moleculeName); }
private static PKCalculationOptions checkOptions(PKCalculationOptions options) { return(options ?? new PKCalculationOptions()); }
public PKValues CalculatePK(DataColumn dataColumn, PKCalculationOptions options = null) { return(CalculatePK(dataColumn.BaseGrid.Values, dataColumn.Values, options)); }
private IEnumerable <DosingInterval> allIntervalsFor(IReadOnlyList <float> time, IReadOnlyList <float> concentration, PKCalculationOptions options) { var timeValues = time.ToList(); var concentrationValues = concentration.ToList(); if (!timeValues.Any() || !concentrationValues.Any()) { yield break; } var fullRange = new DosingInterval(timeValues, concentrationValues, options); //only one interval if (options.SingleDosing) { yield return(fullRange); yield break; } var firstDosingStartIndex = ArrayHelper.ClosestIndexOf(timeValues, options.FirstDosingStartValue); var firstDosingEndIndex = ArrayHelper.ClosestIndexOf(timeValues, options.FirstDosingEndValue); var lastMinusOneDosingStartIndex = ArrayHelper.ClosestIndexOf(timeValues, options.LastMinusOneDosingStartValue); var lastDosingStartIndex = ArrayHelper.ClosestIndexOf(timeValues, options.LastDosingStartValue); var lastDosingEndIndex = ArrayHelper.ClosestIndexOf(timeValues, options.LastDosingEndValue); if (oneTimeIndexInvalid(firstDosingEndIndex, firstDosingStartIndex, lastDosingStartIndex, lastDosingEndIndex, lastMinusOneDosingStartIndex)) { yield break; } yield return(dosingInterval(timeValues, concentrationValues, firstDosingStartIndex, firstDosingEndIndex, options)); //The end time of the last minus one dosing interval is the start time of the last interval yield return(dosingInterval(timeValues, concentrationValues, lastMinusOneDosingStartIndex, lastDosingStartIndex, options)); yield return(dosingInterval(timeValues, concentrationValues, lastDosingStartIndex, lastDosingEndIndex, options)); yield return(fullRange); }
public virtual bool PKParameterCanBeUsed(PKParameter p, PKCalculationOptions pkCalculationOptions) { return(p.Mode.Is(pkCalculationOptions.PKParameterMode)); }
public PKValues CalculatePK(DataColumn dataColumn, PKCalculationOptions options = null, IReadOnlyList <UserDefinedPKParameter> userDefinedPKParameters = null) { return(CalculatePK(dataColumn.BaseGrid.Values, dataColumn.Values, options, userDefinedPKParameters)); }