diff --git a/dhl_sdk/_input_processing.py b/dhl_sdk/_input_processing.py index 8f44e2e..7f0ee6d 100644 --- a/dhl_sdk/_input_processing.py +++ b/dhl_sdk/_input_processing.py @@ -281,6 +281,11 @@ def validate(self) -> bool: # validate inputs with model variables _validate_propagation_with_variables(self.timestamps, self.inputs, self.model) + + # validate inputs and timestamps with prediction config + _validate_propagation_prediction_config( + self.timestamps, self.inputs, self.prediction_config + ) return True def format(self) -> list[dict]: @@ -305,10 +310,21 @@ def format(self) -> list[dict]: for variable in model_variables: if variable.matches_key(key): formatted_inputs[variable.id] = {} - formatted_inputs[variable.id]["values"] = value - formatted_inputs[variable.id]["timestamps"] = self.timestamps[ - : len(value) - ] + if groupcode_is_propagation_prediction(variable.group.code): + formatted_inputs[variable.id]["values"] = [value[0]] + formatted_inputs[variable.id]["timestamps"] = [ + self.timestamps[self.prediction_config.starting_index] + ] + formatted_inputs[variable.id]["steps"] = [ + self.prediction_config.starting_index + ] + else: + formatted_inputs[variable.id]["values"] = value + formatted_inputs[variable.id]["timestamps"] = self.timestamps[ + : len(value) + ] + formatted_inputs[variable.id]["steps"] = list(range(len(value))) + break for variable in input_variables: @@ -327,12 +343,13 @@ def format(self) -> list[dict]: by_alias=True, exclude_none=True, include={ - "instances": {"__all__": {"__all__": {"timestamps", "values"}}}, + "instances": { + "__all__": {"__all__": {"timestamps", "values", "steps"}} + }, "metadata": True, "stages": True, }, ) - return [json_data] @@ -720,6 +737,37 @@ def _validate_historical_with_variables( ) +# pylint: disable=unused-argument +def _validate_propagation_prediction_config( + timestamps: list[int], + inputs: dict[str, list], + prediction_config: PredictionRequestConfig, +) -> None: + """ + Validate the prediction config for propagation cultivation model prediction. + + Parameters + ---------- + timestamps : list[int] + List of timestamps for the prediction + inputs : dict[str, list] + A dictionary where keys are variable codes, and values are lists of inputs. + prediction_config: PredictionRequestConfig + Prediction configuration for the propagation model + + Raises + ------ + InvalidInputsException + """ + + # the starting index cannot be larger than the len(timestamps) + if prediction_config.starting_index >= len(timestamps): + raise InvalidInputsException( + "The starting index of the prediction cannot be larger than the length " + + "of the provided list of timestamps." + ) + + def format_predictions( predictions: list[PredictionResponse], model: Model ) -> Predictions: @@ -783,6 +831,11 @@ def groupcode_is_numeric(code: str): ] +def groupcode_is_propagation_prediction(code: str): + """Check if the group is propagation prediction""" + return code in ["X"] + + def groupcode_is_output(code: str): """Check if the group is an output / CQA""" return code == "Y" diff --git a/dhl_sdk/_utils.py b/dhl_sdk/_utils.py index 5250016..73ba2e2 100644 --- a/dhl_sdk/_utils.py +++ b/dhl_sdk/_utils.py @@ -49,14 +49,14 @@ class Instance(BaseModel): timestamps: Optional[list[int]] = Field(default=None, alias="timestamps") sample_id: Optional[list[str]] = Field(default=None, alias="sampleId") steps: Optional[list[int]] = Field(default=None, alias="steps") - values: Union[list[float], list[list[float]]] - - high_values: Optional[Union[list[float], list[list[float]]]] = Field( - default=None, alias="highValues" - ) - low_values: Optional[Union[list[float], list[list[float]]]] = Field( - default=None, alias="lowValues" - ) + values: Union[list[Optional[float]], list[list[Optional[float]]]] + + high_values: Optional[ + Union[list[Optional[float]], list[list[Optional[float]]]] + ] = Field(default=None, alias="highValues") + low_values: Optional[ + Union[list[Optional[float]], list[list[Optional[float]]]] + ] = Field(default=None, alias="lowValues") @model_validator(mode="after") def generate_sample_ids(self): diff --git a/dhl_sdk/entities.py b/dhl_sdk/entities.py index 8b5a684..4a7271f 100644 --- a/dhl_sdk/entities.py +++ b/dhl_sdk/entities.py @@ -233,11 +233,14 @@ class PredictionConfig(BaseModel): expected to fall, with a specified level of certainty, i.e, setting it to 80% corresponds to capturing the range between the 10th and 90th percentiles of the model's output. Must be a value between 1 and 99, by default 80 + starting_index: int, optional + The index of the timestamp after which the prediction will commence """ model_config = ConfigDict(protected_namespaces=()) model_confidence: float = Field(default=80.0, ge=1.0, le=99.0) + starting_index: int = Field(default=0, ge=0) class SpectraModel(Model): @@ -417,7 +420,8 @@ def predict( ) prediction_config = PredictionRequestConfig.new( - model_confidence=config.model_confidence + model_confidence=config.model_confidence, + starting_index=config.starting_index, ) data_processing_strategy = CultivationPropagationPreprocessor(