Skip to content

Commit

Permalink
fix: Fix resampling of kinetic conversion factors in uncertainty run
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkruisselbrink committed Jun 13, 2024
1 parent 75990b0 commit 71c15d5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 54 deletions.
2 changes: 1 addition & 1 deletion MCRA.Data.Compiled/Objects/ExposureBiomarkerConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class ExposureBiomarkerConversion {
public ExpressionType ExpressionTypeFrom { get; set; } = ExpressionType.None;
public ExposureUnitTriple UnitTo { get; set; }
public ExpressionType ExpressionTypeTo { get; set; } = ExpressionType.None;
public BiomarkerConversionDistribution Distribution { get; set; }
public BiomarkerConversionDistribution Distribution { get; set; } = BiomarkerConversionDistribution.Unspecified;
public double ConversionFactor { get; set; }
public double? VariabilityUpper { get; set; }
public ICollection<ExposureBiomarkerConversionSG> EBCSubgroups { get; set; } = new HashSet<ExposureBiomarkerConversionSG>();
Expand Down
2 changes: 1 addition & 1 deletion MCRA.Data.Compiled/Objects/KineticConversionFactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class KineticConversionFactor {
public ExposureUnitTriple DoseUnitTo { get; set; }
public ExpressionType ExpressionTypeTo { get; set; } = ExpressionType.None;
public double ConversionFactor { get; set; }
public BiomarkerConversionDistribution Distribution { get; set; }
public BiomarkerConversionDistribution Distribution { get; set; } = BiomarkerConversionDistribution.Unspecified;
public double? UncertaintyUpper { get; set; }

public ICollection<KineticConversionFactorSG> KCFSubgroups { get; set; } = new HashSet<KineticConversionFactorSG>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ public static KineticConversionFactorModel FakeKineticConversionFactorModel(
kineticConversionFactor.DoseUnitTo = ExposureUnitTriple.FromDoseUnit(doseUnitTo);
return KineticConversionFactorCalculatorFactory.Create(
conversion: kineticConversionFactor,
useSubgroups: false,
isUncertainty: false
useSubgroups: false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ StandardiseUrineMethod standardiseUrineMethod
}
var kineticConversionFactorModel = KineticConversionFactorCalculatorFactory.Create(
conversion: kineticConversionFactor,
useSubgroups: false,
isUncertainty: false
useSubgroups: false
);

substances.ForEach(c => c.IsLipidSoluble = true);
Expand Down Expand Up @@ -661,8 +660,7 @@ StandardiseBloodMethod standardiseBloodMethod
}
var kineticConversionFactorModel = KineticConversionFactorCalculatorFactory.Create(
conversion: kineticConversionFactor,
useSubgroups: false,
isUncertainty: false
useSubgroups: false
);
substances.ForEach(c => c.IsLipidSoluble = true);
var data = new ActionData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,6 @@ namespace MCRA.Simulation.Test.UnitTests.Calculators.HumanMonitoringCalculation.
[TestClass]
public class HbmKineticConversionFactorCalculatorTests {

[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void HbmKineticConversionFactorCalculator_TestConstNoUnc(bool isNominal) {
var conversion = new KineticConversionFactor() {
IdKineticConversionFactor = "id1",
ConversionFactor = .4,
Distribution = BiomarkerConversionDistribution.Unspecified,
};
var model = KineticConversionFactorCalculatorFactory.Create(conversion, false, !isNominal);
Assert.IsNotNull(model);

var seed = 1;
var random = new McraRandomGenerator(seed);
model.ResampleModelParameters(random);
var draw = model.GetConversionFactor(75, GenderType.Male);
Assert.AreEqual(0.4, draw);
}

[TestMethod]
[DataRow(BiomarkerConversionDistribution.Unspecified, true)]
[DataRow(BiomarkerConversionDistribution.LogNormal, true)]
Expand All @@ -44,16 +25,21 @@ bool isNominal
UncertaintyUpper = 0.6,
Distribution = distributionType,
};
var model = KineticConversionFactorCalculatorFactory.Create(conversion, false, !isNominal);
var model = KineticConversionFactorCalculatorFactory.Create(conversion, false);
Assert.IsNotNull(model);

var seed = 1;
var random = new McraRandomGenerator(seed);
model.ResampleModelParameters(random);
if (!isNominal) {
var random = new McraRandomGenerator(1);
model.ResampleModelParameters(random);
}

var draw = model.GetConversionFactor(75, GenderType.Male);
if (isNominal || distributionType == BiomarkerConversionDistribution.Unspecified) {
Assert.AreEqual(0.4, draw);
} else {
// This is an uncertainty draw; it is be very suspicious when it is
// exactly the same as the nominal value
Assert.AreNotEqual(0.4, draw);
Assert.IsTrue(draw > 0);
}
}
Expand All @@ -79,11 +65,14 @@ bool isNominal
(null, GenderType.Female, 2.5, 2.9)
};
var conversion = createKineticConversionFactor(distributionType, subgroups, nominalFactor);
var model = KineticConversionFactorCalculatorFactory.Create(conversion, true, !isNominal);
var model = KineticConversionFactorCalculatorFactory.Create(conversion, true);
Assert.IsNotNull(model);

var seed = 1;
var random = new McraRandomGenerator(seed);
if (!isNominal) {
var random = new McraRandomGenerator(1);
model.ResampleModelParameters(random);
}

var scenarios = new List<(double? age, GenderType gender, double expectedConst)>() {
(5, GenderType.Male, 1.0),
(10, GenderType.Male, 1.1),
Expand All @@ -96,11 +85,13 @@ bool isNominal
(null, GenderType.Undefined, 0.5)
};
foreach (var scenario in scenarios) {
model.ResampleModelParameters(random);
var draw = model.GetConversionFactor(scenario.age, scenario.gender);
if (isNominal || distributionType == BiomarkerConversionDistribution.Unspecified) {
Assert.AreEqual(scenario.expectedConst, draw);
} else {
// This is an uncertainty draw; it is be very suspicious when it is
// exactly the same as the nominal value
Assert.AreNotEqual(scenario.expectedConst, draw);
Assert.IsTrue(draw > 0);
}
}
Expand Down Expand Up @@ -137,7 +128,7 @@ public void HbmKineticConversionFactorCalculatorLognormal_TestUncertain_FailNoUp
ConversionFactor = .4,
Distribution = BiomarkerConversionDistribution.LogNormal,
};
_ = KineticConversionFactorCalculatorFactory.Create(conversion, false, true);
_ = KineticConversionFactorCalculatorFactory.Create(conversion, false);
}

/// <summary>
Expand All @@ -158,7 +149,7 @@ public void HbmKineticConversionFactorCalculatorLognormal_TestUncertain_FailNoSg
}
}
};
_ = KineticConversionFactorCalculatorFactory.Create(conversion, true, true);
_ = KineticConversionFactorCalculatorFactory.Create(conversion, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ double expected
ConversionFactor = factor
};

var conversion = KineticConversionFactorCalculatorFactory.Create(fakeConversionFactor, false, false);
var conversion = KineticConversionFactorCalculatorFactory.Create(fakeConversionFactor, false);
var converter = new TargetMatrixKineticConversionCalculator(
new List<KineticConversionFactorModel>() { conversion },
targetUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ out var selectedKineticModel
data.KineticConversionFactors = subsetManager.AllKineticConversionFactors;

data.KineticConversionFactorModels = data.KineticConversionFactors?
.Select(c => KineticConversionFactorCalculatorFactory.Create(
c,
_project.KineticModelSettings.KCFSubgroupDependent,
isUncertainty: false
)
.Select(c => KineticConversionFactorCalculatorFactory
.Create(c, _project.KineticModelSettings.KCFSubgroupDependent)
)
.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ public class KineticConversionFactorCalculatorFactory {

public static KineticConversionFactorModel Create(
KineticConversionFactor conversion,
bool useSubgroups,
bool isUncertainty
bool useSubgroups
) {
KineticConversionFactorModel model = null;
KineticConversionFactorModel model;

//In the nominal run, always return Constant Model (e.g. the conversion factor is returned).
if (!isUncertainty) {
model = new KineticConversionFactorConstantModel(conversion, useSubgroups);
model.CalculateParameters();
return model;
}

// In the uncertainty runs, create model for specified uncertainty distribution type
switch (conversion.Distribution) {
case BiomarkerConversionDistribution.Unspecified:
model = new KineticConversionFactorConstantModel(conversion, useSubgroups);
Expand All @@ -29,6 +20,8 @@ bool isUncertainty
case BiomarkerConversionDistribution.Uniform:
model = new KineticConversionFactorUniformModel(conversion, useSubgroups);
break;
default:
throw new NotImplementedException($"No kinetic conversion model for distribution type ${conversion.Distribution}.");
}
model.CalculateParameters();
return model;
Expand Down

0 comments on commit 71c15d5

Please sign in to comment.