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

Several schema updates #260

Merged
merged 3 commits into from
Oct 8, 2024
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
8 changes: 4 additions & 4 deletions pywr-schema/src/data_tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ use tracing::{debug, info};
#[cfg(feature = "core")]
use vec::{load_csv_row2_vec_table_one, load_csv_row_vec_table_one, LoadedVecTable};

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, strum_macros::Display)]
#[serde(rename_all = "lowercase")]
pub enum DataTableType {
Scalar,
Array,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, strum_macros::Display)]
pub enum DataTableFormat {
CSV,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, strum_macros::Display)]
#[serde(tag = "format", rename_all = "lowercase")]
pub enum DataTable {
CSV(CsvDataTable),
Expand All @@ -54,7 +54,7 @@ impl DataTable {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, strum_macros::Display)]
#[serde(rename_all = "lowercase")]
pub enum CsvDataTableLookup {
Row(usize),
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl TryFromV1Parameter<ParameterValueV1> for Metric {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, strum_macros::Display)]
#[serde(tag = "type", content = "name")]
pub enum TimeseriesColumns {
Scenario(String),
Expand Down
6 changes: 4 additions & 2 deletions pywr-schema/src/metric_sets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use serde::{Deserialize, Serialize};
use std::num::NonZeroUsize;

/// Aggregation function to apply over metric values.
#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitPaths)]
#[derive(
serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitPaths, strum_macros::Display,
)]
#[serde(tag = "type")]
pub enum MetricAggFunc {
Sum,
Expand All @@ -34,7 +36,7 @@ impl From<MetricAggFunc> for pywr_core::recorders::AggregationFunction {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, strum_macros::Display)]
#[serde(tag = "type")]
pub enum MetricAggFrequency {
Monthly,
Expand Down
6 changes: 3 additions & 3 deletions pywr-schema/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl TryFrom<pywr_v1_schema::model::Metadata> for Metadata {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, strum_macros::Display)]
#[serde(untagged)]
pub enum Timestep {
Days(i64),
Expand All @@ -69,7 +69,7 @@ impl From<pywr_v1_schema::model::Timestep> for Timestep {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Copy, Debug, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Copy, Debug, JsonSchema, strum_macros::Display)]
#[serde(untagged)]
pub enum DateType {
Date(NaiveDate),
Expand Down Expand Up @@ -526,7 +526,7 @@ impl PywrNetwork {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone)]
#[derive(serde::Deserialize, serde::Serialize, Clone, strum_macros::Display)]
#[serde(untagged)]
pub enum PywrNetworkRef {
Path(PathBuf),
Expand Down
6 changes: 4 additions & 2 deletions pywr-schema/src/nodes/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,9 @@ impl TryFrom<OutputNodeV1> for OutputNode {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, PartialEq, Copy, Debug, JsonSchema, PywrVisitAll)]
#[derive(
serde::Deserialize, serde::Serialize, Clone, PartialEq, Copy, Debug, JsonSchema, PywrVisitAll, strum_macros::Display,
)]
pub enum StorageInitialVolume {
Absolute(f64),
Proportional(f64),
Expand Down Expand Up @@ -1041,7 +1043,7 @@ impl TryFrom<CatchmentNodeV1> for CatchmentNode {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll, strum_macros::Display)]
#[serde(tag = "type", deny_unknown_fields)]
pub enum Relationship {
Proportion {
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/nodes/loss_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use schemars::JsonSchema;
/// Gross losses are typically applied as a proportion of the total flow into a node, whereas
/// net losses are applied as a proportion of the net flow. Please see the documentation for
/// specific nodes (e.g. [`LossLinkNode`]) to understand how the loss factor is applied.
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll, strum_macros::Display)]
#[serde(tag = "type", deny_unknown_fields)]
pub enum LossFactor {
Gross { factor: Metric },
Expand Down
4 changes: 2 additions & 2 deletions pywr-schema/src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use core::{
};
pub use delay::DelayNode;
pub use loss_link::{LossFactor, LossLinkNode};
pub use monthly_virtual_storage::MonthlyVirtualStorageNode;
pub use monthly_virtual_storage::{MonthlyVirtualStorageNode, NumberOfMonthsReset};
pub use piecewise_link::{PiecewiseLinkNode, PiecewiseLinkStep};
pub use piecewise_storage::{PiecewiseStorageNode, PiecewiseStore};
#[cfg(feature = "core")]
Expand Down Expand Up @@ -237,7 +237,7 @@ impl NodeBuilder {
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, EnumDiscriminants, Debug, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Clone, EnumDiscriminants, Debug, JsonSchema, strum_macros::Display)]
#[serde(tag = "type")]
#[strum_discriminants(derive(Display, IntoStaticStr, EnumString, VariantNames))]
// This creates a separate enum called `NodeType` that is available in this module.
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/nodes/rolling_virtual_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::num::NonZeroUsize;
/// The length of the rolling window.
///
/// This can be specified in either days or time-steps.
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema, PywrVisitAll, strum_macros::Display)]
pub enum RollingWindow {
Days(NonZeroUsize),
Timesteps(NonZeroUsize),
Expand Down
6 changes: 4 additions & 2 deletions pywr-schema/src/outputs/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use std::num::NonZeroU32;
use std::path::Path;
use std::path::PathBuf;

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Default, JsonSchema, PywrVisitPaths)]
#[derive(
serde::Deserialize, serde::Serialize, Debug, Clone, Default, JsonSchema, PywrVisitPaths, strum_macros::Display,
)]
#[serde(rename_all = "lowercase")]
pub enum CsvFormat {
Wide,
#[default]
Long,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitPaths)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitPaths, strum_macros::Display)]
#[serde(untagged)]
pub enum CsvMetricSet {
Single(String),
Expand Down
4 changes: 3 additions & 1 deletion pywr-schema/src/outputs/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ impl From<MemoryAggregation> for pywr_core::recorders::Aggregation {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitPaths)]
#[derive(
serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitPaths, strum_macros::Display,
)]
pub enum MemoryAggregationOrder {
MetricTimeScenario,
TimeMetricScenario,
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use schemars::JsonSchema;
#[cfg(feature = "core")]
use std::path::Path;

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitPaths)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitPaths, strum_macros::Display)]
#[serde(tag = "type")]
pub enum Output {
CSV(CsvOutput),
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/parameters/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use schemars::JsonSchema;
/// algorithms to represent a, for example, binary-like variable in a continuous domain. Each
/// activation function requires different data to parameterize the function's behaviour.
///
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy, JsonSchema, PywrVisitAll, strum_macros::Display)]
#[serde(tag = "type", deny_unknown_fields)]
pub enum ActivationFunction {
/// A unit or null transformation.
Expand Down
14 changes: 7 additions & 7 deletions pywr-schema/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl FromV1Parameter<Option<ParameterMetaV1>> for ParameterMeta {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, EnumDiscriminants, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, EnumDiscriminants, Clone, JsonSchema, Display)]
#[serde(tag = "type")]
#[strum_discriminants(derive(Display, IntoStaticStr, EnumString, VariantNames))]
// This creates a separate enum called `NodeType` that is available in this module.
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Parameter {
Self::Max(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
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)?),
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::TablesArray(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Python(p) => p.add_to_model(network, args)?,
Expand Down Expand Up @@ -660,7 +660,7 @@ impl TryFromV1Parameter<ParameterV1> for ParameterOrTimeseries {
/// An non-variable constant floating-point (f64) value
///
/// This value can be a literal float or an external reference to an input table.
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, Display)]
#[serde(untagged)]
pub enum ConstantValue<T> {
Literal(T),
Expand Down Expand Up @@ -755,7 +755,7 @@ impl TryFrom<ParameterValueV1> for ConstantValue<f64> {
}

/// An integer (i64) value from another parameter
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll, Display)]
#[serde(untagged)]
pub enum ParameterIndexValue {
Reference(String),
Expand Down Expand Up @@ -798,7 +798,7 @@ impl ParameterIndexValue {
///
/// This value can be a constant (literal or otherwise) or a dynamic value provided
/// by another parameter.
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll, Display)]
#[serde(untagged)]
pub enum DynamicIndexValue {
Constant(ConstantValue<usize>),
Expand Down Expand Up @@ -854,7 +854,7 @@ impl TryFromV1Parameter<ParameterValueV1> for DynamicIndexValue {
/// An non-variable vector of constant floating-point (f64) values
///
/// This value can be a literal vector of floats or an external reference to an input table.
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll, Display)]
#[serde(untagged)]
pub enum ConstantFloatVec {
Literal(Vec<f64>),
Expand All @@ -872,7 +872,7 @@ impl ConstantFloatVec {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll, Display)]
#[serde(untagged)]
pub enum TableIndex {
Single(String),
Expand Down
29 changes: 22 additions & 7 deletions pywr-schema/src/parameters/polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::error::ConversionError;
#[cfg(feature = "core")]
use crate::error::SchemaError;
use crate::metric::{Metric, NodeReference};
#[cfg(feature = "core")]
use crate::model::LoadArgs;
use crate::nodes::NodeAttribute;
use crate::parameters::{IntoV2Parameter, ParameterMeta, TryFromV1Parameter};
#[cfg(feature = "core")]
use pywr_core::parameters::ParameterIndex;
Expand All @@ -12,18 +16,20 @@ use schemars::JsonSchema;
#[serde(deny_unknown_fields)]
pub struct Polynomial1DParameter {
pub meta: ParameterMeta,
pub storage_node: String,
pub metric: Metric,
pub coefficients: Vec<f64>,
pub use_proportional_volume: Option<bool>,
pub scale: Option<f64>,
pub offset: Option<f64>,
}

#[cfg(feature = "core")]
impl Polynomial1DParameter {
pub fn add_to_model(&self, network: &mut pywr_core::network::Network) -> Result<ParameterIndex<f64>, SchemaError> {
let metric =
network.get_storage_node_metric(&self.storage_node, None, self.use_proportional_volume.unwrap_or(true))?;
pub fn add_to_model(
&self,
network: &mut pywr_core::network::Network,
args: &LoadArgs,
) -> Result<ParameterIndex<f64>, SchemaError> {
let metric = self.metric.load(network, args)?;

let p = pywr_core::parameters::Polynomial1DParameter::new(
self.meta.name.as_str().into(),
Expand All @@ -44,11 +50,20 @@ impl TryFromV1Parameter<Polynomial1DParameterV1> for Polynomial1DParameter {
parent_node: Option<&str>,
unnamed_count: &mut usize,
) -> Result<Self, Self::Error> {
let attribute = match v1.use_proportional_volume.unwrap_or(true) {
true => Some(NodeAttribute::ProportionalVolume),
false => Some(NodeAttribute::Volume),
};

let metric = Metric::Node(NodeReference {
name: v1.storage_node,
attribute,
});

let p = Self {
meta: v1.meta.into_v2_parameter(parent_node, unnamed_count),
storage_node: v1.storage_node,
metric,
coefficients: v1.coefficients,
use_proportional_volume: v1.use_proportional_volume,
scale: v1.scale,
offset: v1.offset,
};
Expand Down
4 changes: 2 additions & 2 deletions pywr-schema/src/parameters/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl TryFromV1Parameter<UniformDrawdownProfileParameterV1> for UniformDrawdownPr
}

/// Distance functions for radial basis function interpolation.
#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitAll, strum_macros::Display)]
#[serde(deny_unknown_fields)]
pub enum RadialBasisFunction {
Linear,
Expand Down Expand Up @@ -457,7 +457,7 @@ impl TryFromV1Parameter<RbfProfileParameterV1> for RbfProfileParameter {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Copy, Clone, JsonSchema, PywrVisitAll, strum_macros::Display)]
pub enum WeeklyInterpDay {
First,
Last,
Expand Down
4 changes: 2 additions & 2 deletions pywr-schema/src/parameters/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, strum_macros::Display)]
#[serde(rename_all = "lowercase")]
pub enum PythonSource {
Module(String),
Path(PathBuf),
}

/// The expected return type of the Python parameter.
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Default, JsonSchema)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Default, JsonSchema, strum_macros::Display)]
#[serde(rename_all = "lowercase")]
pub enum PythonReturnType {
#[default]
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/parameters/thresholds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use pywr_v1_schema::parameters::{
};
use schemars::JsonSchema;

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy, JsonSchema, PywrVisitAll)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy, JsonSchema, PywrVisitAll, strum_macros::Display)]
pub enum Predicate {
#[serde(alias = "<")]
LT,
Expand Down