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: add returned_metric attribute to threshold parameter #331

Merged
merged 3 commits into from
Jan 20, 2025

Conversation

Batch21
Copy link
Contributor

@Batch21 Batch21 commented Jan 19, 2025

This optional attribute contains metrics to be returned by the parameter. It is equivalent to the values attribute of the v1 threshold parameters and enables conversions of v1 parameters that specify this attribute.

This optional attribute contains metrics to be returned by the parameter.
It is equivalent to the values attribute of the v1 threshold parameters
and enables conversions of v1 parameters that specify this attribute.
@@ -191,7 +191,18 @@ impl Parameter {
Self::Min(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Negative(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Polynomial1D(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Threshold(p) => pywr_core::parameters::ParameterType::Index(p.add_to_model(network, args)?),
Self::Threshold(p) => match p.add_to_model(network, args)? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.add_to_model already returns a ParameterType?

@@ -54,13 +55,37 @@ impl From<Predicate> for pywr_core::parameters::Predicate {
}
}

/// A parameter that compares a metric against a threshold metric
///
/// The metrics are compared using the given predicate and the result is returned as an index. If the comparison
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to say that the returned metrics are used instead if given.

/// is set as the index parameter of a [`pywr_core::parameters::IndexedArrayParameter`] containing the `returned_metrics`
/// values.
///
/// An equivalent representation could be achieved by defining the two parameters in the schema directly:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand this to compare the approach with one parameter and two.

Copy link
Member

@jetuk jetuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment and a request to make the IndexMetric change in a different PR. Otherwise LGTM.

@@ -472,7 +472,7 @@ impl EdgeReference {
/// This struct is the integer equivalent of [`Metric`] and is used in places where an integer
/// value is required. See [`Metric`] for more information.
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, Display, PartialEq)]
#[serde(untagged)]
#[serde(tag = "type")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in a separate PR?

let _ = serde_json::from_value::<Vec<Parameter>>(value)
.unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p));
}
_ => panic!("Expected JSON object: {:?}", p),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"... JSON object or array."

@jetuk jetuk merged commit 99c9bd9 into main Jan 20, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants