|
| 1 | +from typing import List, Self, Union |
| 2 | +from pydantic import Field |
| 3 | + |
| 4 | +from pyenzyme.versions import v2 |
| 5 | +from .baserow import BaseRow |
| 6 | + |
| 7 | + |
| 8 | +class MeasurementRow(BaseRow): |
| 9 | + """ |
| 10 | + Represents a row in a PEtab measurements table. |
| 11 | +
|
| 12 | + This class models experimental measurements with species initial concentrations |
| 13 | + and other measurement-specific parameters. |
| 14 | +
|
| 15 | + Attributes: |
| 16 | + observable_id (str): The identifier of the observable being measured. |
| 17 | + Maps to 'observableId' in the PEtab specification. |
| 18 | + preequilibration_condition_id (str | None): The identifier of the preequilibration condition. |
| 19 | + Maps to 'preequilibrationConditionId' in the PEtab specification. |
| 20 | + Defaults to None if no preequilibration was performed. |
| 21 | + condition_id (str): The identifier of the simulation condition. |
| 22 | + Maps to 'simulationConditionId' in the PEtab specification. |
| 23 | + measurement (float): The measured value of the observable. |
| 24 | + Maps to 'measurement' in the PEtab specification. |
| 25 | + time (float): The time point at which the measurement was taken. |
| 26 | + Maps to 'time' in the PEtab specification. |
| 27 | + observable_parameters (Union[str, float, None]): Parameters for the observable transformation. |
| 28 | + Maps to 'observableParameters' in the PEtab specification. |
| 29 | + Can be a parameter ID, a numeric value, or None if not applicable. |
| 30 | + noise_parameters (Union[str, float, None]): Parameters for the noise model. |
| 31 | + Maps to 'noiseParameters' in the PEtab specification. |
| 32 | + Can be a parameter ID, a numeric value, or None if not applicable. |
| 33 | + dataset_id (str | None): An identifier for the dataset this measurement belongs to. |
| 34 | + Maps to 'datasetId' in the PEtab specification. |
| 35 | + Useful for grouping measurements from the same experiment. |
| 36 | + replicate_id (str | None): An identifier for the replicate this measurement belongs to. |
| 37 | + Maps to 'replicateId' in the PEtab specification. |
| 38 | + Useful for identifying repeated measurements under identical conditions. |
| 39 | + """ |
| 40 | + |
| 41 | + observable_id: str = Field(alias="observableId") |
| 42 | + preequilibration_condition_id: str | None = Field( |
| 43 | + default=None, |
| 44 | + alias="preequilibrationConditionId", |
| 45 | + ) |
| 46 | + condition_id: str = Field(alias="simulationConditionId") |
| 47 | + measurement: float = Field(alias="measurement") |
| 48 | + time: float = Field(alias="time") |
| 49 | + observable_parameters: Union[str, float, None] = Field( |
| 50 | + default=None, |
| 51 | + alias="observableParameters", |
| 52 | + ) |
| 53 | + noise_parameters: Union[str, float, None] = Field( |
| 54 | + default=None, |
| 55 | + alias="noiseParameters", |
| 56 | + ) |
| 57 | + dataset_id: str | None = Field(alias="datasetId", default=None) |
| 58 | + replicate_id: str | None = Field(alias="replicateId", default=None) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def from_measurements(cls, measurements: list[v2.Measurement]) -> List[Self]: |
| 62 | + """ |
| 63 | + Convert a list of EnzymeML Measurement objects to a list of PEtab MeasurementRow objects. |
| 64 | + """ |
| 65 | + return [ |
| 66 | + row |
| 67 | + for measurement in measurements |
| 68 | + for row in cls.from_measurement(measurement) |
| 69 | + ] |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_measurement(cls, measurement: v2.Measurement) -> List[Self]: |
| 73 | + """ |
| 74 | + Convert an EnzymeML Measurement object to a list of PEtab MeasurementRow objects. |
| 75 | +
|
| 76 | + This method extracts the time series data from a Measurement object and creates |
| 77 | + individual MeasurementRow entries for each time point and corresponding data value. |
| 78 | +
|
| 79 | + Args: |
| 80 | + measurement (v2.Measurement): An EnzymeML Measurement object containing |
| 81 | + species concentration time series data. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + List[Self]: A list of MeasurementRow objects, each representing a single |
| 85 | + data point from the original measurement. Each row contains the species ID, |
| 86 | + measurement condition ID, the measured value, and the time point. |
| 87 | +
|
| 88 | + Example: |
| 89 | + If a Measurement contains data for species 'S1' with time points [0, 10, 20] |
| 90 | + and corresponding values [1.0, 0.8, 0.6], this method will return three |
| 91 | + MeasurementRow objects, one for each time-value pair. |
| 92 | + """ |
| 93 | + meas_rows = [] |
| 94 | + for meas_data in measurement.species_data: |
| 95 | + for t, x in zip(meas_data.time, meas_data.data): |
| 96 | + meas_rows.append( |
| 97 | + cls( |
| 98 | + observableId=meas_data.species_id, |
| 99 | + simulationConditionId=measurement.id, |
| 100 | + measurement=x, |
| 101 | + time=t, |
| 102 | + ) |
| 103 | + ) |
| 104 | + return meas_rows |
0 commit comments