Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow for multiple Myokit simulations #366

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend-v2/src/app/backendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ export type CombinedModelSetVariablesFromInferenceUpdateApiArg = {
combinedModel: CombinedModel;
};
export type CombinedModelSimulateCreateApiResponse =
/** status 200 */ SimulateResponse;
/** status 200 */ SimulateResponse[];
export type CombinedModelSimulateCreateApiArg = {
id: number;
simulate: Simulate;
Expand Down Expand Up @@ -1350,7 +1350,7 @@ export type PharmacodynamicSetVariablesFromInferenceUpdateApiArg = {
pharmacodynamic: Pharmacodynamic;
};
export type PharmacodynamicSimulateCreateApiResponse =
/** status 200 */ SimulateResponse;
/** status 200 */ SimulateResponse[];
export type PharmacodynamicSimulateCreateApiArg = {
id: number;
simulate: Simulate;
Expand Down
51 changes: 27 additions & 24 deletions frontend-v2/src/features/simulation/Simulations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const Simulations: FC = () => {
? (simulateErrorBase.data as ErrorObject)
: { error: "Unknown error" }
: undefined;
const [data, setData] = useState<SimulateResponse | null>(null);
const [data, setData] = useState<SimulateResponse[] | null>(null);
const { data: compound, isLoading: isLoadingCompound } =
useCompoundRetrieveQuery(
{ id: project?.compound || 0 },
Expand Down Expand Up @@ -303,7 +303,8 @@ const Simulations: FC = () => {
}).then((response) => {
setLoadingSimulate(false);
if ("data" in response) {
const responseData = response.data as SimulateResponse;
const responseData = response.data as SimulateResponse[];
console.log({ responseData })
setData(responseData);
}
});
Expand Down Expand Up @@ -342,10 +343,10 @@ const Simulations: FC = () => {
),
}).then((response) => {
if ("data" in response) {
const responseData = response.data as SimulateResponse;
const [ projectData ] = response.data;
const nrows =
responseData.outputs[Object.keys(responseData.outputs)[0]].length;
const cols = Object.keys(responseData.outputs);
projectData.outputs[Object.keys(projectData.outputs)[0]].length;
const cols = Object.keys(projectData.outputs);
const vars = cols.map((vid) =>
variables.find((v) => v.id === parseInt(vid)),
);
Expand Down Expand Up @@ -377,7 +378,7 @@ const Simulations: FC = () => {
for (let i = 0; i < nrows; i++) {
rows[rowi] = new Array(ncols);
for (let j = 0; j < ncols; j++) {
rows[rowi][j] = responseData.outputs[cols[j]][i];
rows[rowi][j] = projectData.outputs[cols[j]][i];
}
rowi++;
}
Expand Down Expand Up @@ -592,24 +593,26 @@ const Simulations: FC = () => {
)}
<Grid container spacing={1}>
{plots.map((plot, index) => (
<Grid item xl={layout === "vertical" ? 12 : 6} md={layout === "vertical" ? 12 : 6} xs={layout === "vertical" ? 12 : 12} key={index}>
{data && model ? (
<SimulationPlotView
index={index}
plot={plot}
data={data}
variables={variables || []}
control={control}
setValue={setValue}
remove={removePlot}
units={units}
compound={compound}
model={model}
/>
) : (
<div>Loading...</div>
)}
</Grid>
data?.map(d => (
<Grid item xl={layout === "vertical" ? 12 : 6} md={layout === "vertical" ? 12 : 6} xs={layout === "vertical" ? 12 : 12} key={index}>
{d && model ? (
<SimulationPlotView
index={index}
plot={plot}
data={d}
variables={variables || []}
control={control}
setValue={setValue}
remove={removePlot}
units={units}
compound={compound}
model={model}
/>
) : (
<div>Loading...</div>
)}
</Grid>
))
))}
</Grid>
<Snackbar open={Boolean(simulateError)} autoHideDuration={6000}>
Expand Down
8 changes: 6 additions & 2 deletions pkpdapp/pkpdapp/api/views/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ def to_representation(self, instance):
}


class SimulateResponseListSerializer(serializers.ListSerializer):
child = SimulateResponseSerializer()


class ErrorResponseSerializer(serializers.Serializer):
error = serializers.CharField()


@extend_schema(
request=SimulateSerializer,
responses={
200: SimulateResponseSerializer,
200: SimulateResponseListSerializer,
400: ErrorResponseSerializer,
404: None,
},
Expand All @@ -67,7 +71,7 @@ def post(self, request, pk, format=None):
return Response(
serialized_result.data, status=status.HTTP_400_BAD_REQUEST
)
serialized_result = SimulateResponseSerializer(result)
serialized_result = SimulateResponseSerializer(result, many=True)
return Response(serialized_result.data)


Expand Down
77 changes: 64 additions & 13 deletions pkpdapp/pkpdapp/models/myokit_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#

import pkpdapp
from pkpdapp.models import Protocol
import numpy as np
from myokit.formats.mathml import MathMLExpressionWriter
from myokit.formats.sbml import SBMLParser
Expand Down Expand Up @@ -440,6 +441,27 @@ def serialize_datalog(self, datalog, myokit_model):
def get_time_max(self):
return self.time_max

def simulate_model(
self, outputs=None, variables=None, time_max=None, dosing_protocols=None
):
model = self.get_myokit_model()
# Convert units
variables = self._initialise_variables(model, variables)
time_max = self._convert_bound_unit("time", time_max, model)
# get tlag vars
override_tlag = self._get_override_tlag(variables)
# create simulator
sim = self.create_myokit_simulator(
override_tlag=override_tlag,
model=model,
time_max=time_max,
dosing_protocols=dosing_protocols
)
# TODO: take these from simulation model
sim.set_tolerance(abs_tol=1e-06, rel_tol=1e-08)
# Simulate, logging only state variables given by `outputs`
return self.serialize_datalog(sim.run(time_max, log=outputs), model)

def simulate(self, outputs=None, variables=None, time_max=None):
"""
Arguments
Expand Down Expand Up @@ -475,21 +497,32 @@ def simulate(self, outputs=None, variables=None, time_max=None):
**variables,
}

model = self.get_myokit_model()
# Convert units
variables = self._initialise_variables(model, variables)
time_max = self._convert_bound_unit("time", time_max, model)
# get tlag vars
override_tlag = self._get_override_tlag(variables)
# create simulator
sim = self.create_myokit_simulator(
override_tlag=override_tlag, model=model, time_max=time_max
project_sim = self.simulate_model(
variables=variables, time_max=time_max, outputs=outputs
)
# TODO: take these from simulation model
sim.set_tolerance(abs_tol=1e-06, rel_tol=1e-08)

# Simulate, logging only state variables given by `outputs`
return self.serialize_datalog(sim.run(time_max, log=outputs), model)
project = self.get_project()
sims = [project_sim]
if project is not None:
for subjects in get_project_cohorts(project):
# find unique protocols for this subject cohort
dosing_protocols = {}
subject_protocols = [
Protocol.objects.get(pk=p['protocol'])
for p in subjects.values('protocol').distinct()
if p['protocol'] is not None
]
for protocol in subject_protocols:
dosing_protocols[protocol.mapped_qname] = protocol
sim = self.simulate_model(
outputs=outputs,
variables=variables,
time_max=time_max,
dosing_protocols=dosing_protocols
)
sims.append(sim)

return sims


def set_administration(model, drug_amount, direct=True):
Expand Down Expand Up @@ -689,3 +722,21 @@ def _get_dosing_events(
elif abs(start + duration - time_max) < 1e-6:
dosing_events[i] = (level, start, time_max - start)
return dosing_events


def get_project_cohorts(project):
dataset = project.datasets.first()
cohorts = []
if dataset is not None:
# TODO: create backend subject cohorts based on the
# frontend upload stepper
dataset_protocols = [
Protocol.objects.get(pk=p['protocol'])
for p in dataset.subjects.values('protocol').distinct()
if p['protocol'] is not None
]
cohorts = [
dataset.subjects.filter(protocol=protocol)
for protocol in dataset_protocols
]
return cohorts
9 changes: 5 additions & 4 deletions pkpdapp/pkpdapp/tests/test_views/test_combined_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ def simulate_combined_model(self, id):
format="json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
keys = [key for key in response.data["outputs"].keys()]
data = response.data[0]
keys = [key for key in data["outputs"].keys()]
return (
response.data["outputs"][keys[0]],
response.data["outputs"][keys[1]],
response.data["outputs"][keys[2]],
data["outputs"][keys[0]],
data["outputs"][keys[1]],
data["outputs"][keys[2]],
)

def test_swap_mapped_pd_model(self):
Expand Down
17 changes: 9 additions & 8 deletions pkpdapp/pkpdapp/tests/test_views/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ def test_simulate(self):
response = self.client.post(url, data, format='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
outputs = response.data.get('outputs')
self.assertCountEqual(
list(outputs.keys()),
[
Variable.objects.get(qname=qname, dosed_pk_model=m).id
for qname in data['outputs']
]
)
for sim in response.data:
outputs = sim.get('outputs')
self.assertCountEqual(
list(outputs.keys()),
[
Variable.objects.get(qname=qname, dosed_pk_model=m).id
for qname in data['outputs']
]
)

url = reverse('simulate-combined-model', args=(123,))
response = self.client.post(url, data, format='json')
Expand Down
2 changes: 1 addition & 1 deletion pkpdapp/pkpdapp/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def create_pd_inference(sampling=False):
# generate some fake data
output = model.variables.get(qname='PDCompartment.TS')
time = model.variables.get(qname='environment.t')
data = model.simulate(outputs=[output.qname, time.qname])
data = model.simulate(outputs=[output.qname, time.qname])[0]
print(data)
TS = data[output.id]
times = data[time.id]
Expand Down
8 changes: 6 additions & 2 deletions pkpdapp/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/SimulateResponse'
type: array
items:
$ref: '#/components/schemas/SimulateResponse'
description: ''
'400':
content:
Expand Down Expand Up @@ -1674,7 +1676,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/SimulateResponse'
type: array
items:
$ref: '#/components/schemas/SimulateResponse'
description: ''
'400':
content:
Expand Down
Loading