Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 36 additions & 1 deletion dhl_sdk/_input_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def validate(self) -> bool:
- Garantees that all the mandatory model variables are present in the inputs
- Garantees that the inputs that need to be complete for the recipe
have the same length as the timestamps
- Garantees that the initial condition variables only have one value
- Garantees that the initial condition variables only have one value,
or for X variables, their values up to the starting index

Returns
-------
Expand Down Expand Up @@ -314,9 +315,11 @@ def format(self) -> list[dict]:
if variable.matches_key(key):
formatted_inputs[variable.id] = {}
# insert initial value only for prop. pred. vars, if no filtering present
# and only the value at the starting index is given
if (
groupcode_is_propagation_prediction(variable.group.code)
and self.prediction_config.filtering_config is None
and len(value) == 1
):
formatted_inputs[variable.id]["values"] = [value[0]]
formatted_inputs[variable.id]["timestamps"] = [
Expand Down Expand Up @@ -685,6 +688,21 @@ def _validate_propagation_with_variables(
)
)

elif groupcode_is_propagation_prediction(variable.group.code):
# without filtering, X inputs are either the initial value at the starting index,
# or the values from the first timestamp up to and including the starting index.
# Models that propagate from the start of the run (e.g. the continuous hybrid
# model) need the latter to predict from a starting index other than 0
history_length = prediction_config.starting_index + 1
if len(inputs[variable.code]) not in (1, history_length):
raise InvalidInputsException(
(
f"Input {variable.code} only requires initial "
f"values, so it must have a length of 1, or a length of "
f"{history_length} to provide its values up to the starting index"
)
)

else:
# validate if non time dependent inputs have length of 1 (only initial values)
if len(inputs[variable.code]) != 1:
Expand All @@ -706,6 +724,23 @@ def _validate_propagation_with_variables(
raise InvalidInputsException(
f"Input {variable.code} must be a valid numeric value"
)
elif (
groupcode_is_propagation_prediction(variable.group.code)
and len(inputs[variable.code]) > 1
):
# values up to the starting index may have gaps, but the first value
# and the value at the starting index must not be missing
values = inputs[variable.code]
if not (is_numeric(values[0]) and is_numeric(values[-1])):
raise InvalidInputsException(
f"Input {variable.code} must have a valid numeric value "
f"at the first timestamp and at the starting index"
)
if not all(value is None or is_numeric(value) for value in values):
raise InvalidInputsException(
f"All values of input {variable.code} must be "
f"valid numeric values or None"
)
else:
if not all(is_numeric(value) for value in inputs[variable.code]):
raise InvalidInputsException(
Expand Down
1 change: 1 addition & 0 deletions examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@
"* All model variables must be present (To check what variables are needed for the prediction, use the `model_prop.model_variables` property.)\n",
"* Some variable groups need to be complete for the prediction - `Flows/Feeds` and `W`. For this variable groups, the list of values must be of the same dimension as the `timestamps`. \n",
"* For the rest of the variables (`X`, `Z`, `Feed Concentration`), only the initial value must be provided. \n",
"* When predicting from a `starting_index` other than 0, `X` variables can instead be given their values from the first timestamp up to and including the `starting_index` (missing values in between can be `None`). Models that propagate from the start of the run, such as the continuous hybrid model, need these values. \n",
"* Only `Z` variables can be of type different than `numeric`\n",
"\n",
"The prediction works from the initial given point onwards. "
Expand Down
48 changes: 48 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,54 @@ def test_input_validation(self):
{"timestamps": [1, 2, 3], "steps": [0, 1, 2], "values": [1.0, 2.0, 3.0]},
)

# valid case with startingIndex != 0 and X values up to the starting index
prediction_config = PredictionRequestConfig(startingIndex=2)
inputs = {
"var1": [10, None, 12],
"var2": [20, 21, 22],
"var3": [1, 2, 3],
"var4": [40],
}
processor = CultivationPropagationPreprocessor(
timestamps, "s", inputs, prediction_config, model
)
processor.validate()
outputs = processor.format()
instance_x = outputs[0]["instances"][0][0]
self.assertEqual(
instance_x,
{"timestamps": [1, 2, 3], "steps": [0, 1, 2], "values": [10.0, None, 12.0]},
)

# invalid case: X values neither the initial value nor the values up to the starting index
prediction_config = PredictionRequestConfig(startingIndex=2)
inputs = {"var1": [10, 11], "var2": [20], "var3": [1, 2, 3], "var4": [40]}
processor = CultivationPropagationPreprocessor(
timestamps, "s", inputs, prediction_config, model
)
with self.assertRaises(InvalidInputsException) as ex:
processor.validate()
self.assertTrue(
ex.exception.message.startswith("Input var1 only requires initial values")
)

# invalid case: X value missing at the starting index
inputs = {
"var1": [10, 11, None],
"var2": [20],
"var3": [1, 2, 3],
"var4": [40],
}
processor = CultivationPropagationPreprocessor(
timestamps, "s", inputs, prediction_config, model
)
with self.assertRaises(InvalidInputsException) as ex:
processor.validate()
self.assertEqual(
ex.exception.message,
"Input var1 must have a valid numeric value at the first timestamp and at the starting index",
)

# valid case where filtering and startingIndex collide
prediction_config = PredictionRequestConfig(
startingIndex=1,
Expand Down
Loading