From 754ecbae6cc52927a0301b27a8005a3c4a208e01 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Nov 2023 13:11:07 +0100 Subject: [PATCH] feat(model) --- .../bindingconstraints/bindingcontraints.py | 71 +++++++++++++------ .../storage/study_upgrader/upgrader_870.py | 37 +++------- .../binding_constraint/__init__.py | 2 +- .../binding_constraint/series_after_v87.py | 10 +++ .../{series.py => series_before_v87.py} | 0 .../business/matrix_constants_generator.py | 2 +- .../test_manage_binding_constraints.py | 2 +- 7 files changed, 74 insertions(+), 50 deletions(-) create mode 100644 antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_after_v87.py rename antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/{series.py => series_before_v87.py} (100%) diff --git a/antarest/study/storage/rawstudy/model/filesystem/root/input/bindingconstraints/bindingcontraints.py b/antarest/study/storage/rawstudy/model/filesystem/root/input/bindingconstraints/bindingcontraints.py index e86dedfe18..8811927c91 100644 --- a/antarest/study/storage/rawstudy/model/filesystem/root/input/bindingconstraints/bindingcontraints.py +++ b/antarest/study/storage/rawstudy/model/filesystem/root/input/bindingconstraints/bindingcontraints.py @@ -6,31 +6,62 @@ from antarest.study.storage.rawstudy.model.filesystem.root.input.bindingconstraints.bindingconstraints_ini import ( BindingConstraintsIni, ) -from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series import ( - default_binding_constraint_daily, - default_binding_constraint_hourly, - default_binding_constraint_weekly, +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import ( + default_binding_constraint_daily as default_bd_87_daily, +) +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import ( + default_binding_constraint_hourly as default_bd_87_hourly, +) +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import ( + default_binding_constraint_weekly as default_bd_87_weekly, +) +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import ( + default_binding_constraint_daily as default_bd_86_daily, +) +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import ( + default_binding_constraint_hourly as default_bd_86_hourly, +) +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import ( + default_binding_constraint_weekly as default_bd_86_weekly, ) class BindingConstraints(FolderNode): def build(self) -> TREE: - default_matrices = { - BindingConstraintFrequency.HOURLY: default_binding_constraint_hourly, - BindingConstraintFrequency.DAILY: default_binding_constraint_daily, - BindingConstraintFrequency.WEEKLY: default_binding_constraint_weekly, - } - children: TREE = { - binding.id: InputSeriesMatrix( - self.context, - self.config.next_file(f"{binding.id}.txt"), - freq=MatrixFrequency(binding.time_step), - nb_columns=3, - default_empty=default_matrices[binding.time_step], - ) - for binding in self.config.bindings - } - + cfg = self.config + if cfg.version < 870: + default_matrices = { + BindingConstraintFrequency.HOURLY: default_bd_86_hourly, + BindingConstraintFrequency.DAILY: default_bd_86_daily, + BindingConstraintFrequency.WEEKLY: default_bd_86_weekly, + } + children: TREE = { + binding.id: InputSeriesMatrix( + self.context, + self.config.next_file(f"{binding.id}.txt"), + freq=MatrixFrequency(binding.time_step), + nb_columns=3, + default_empty=default_matrices[binding.time_step], + ) + for binding in self.config.bindings + } + else: + default_matrices = { + BindingConstraintFrequency.HOURLY: default_bd_87_hourly, + BindingConstraintFrequency.DAILY: default_bd_87_daily, + BindingConstraintFrequency.WEEKLY: default_bd_87_weekly, + } + children = {} + for binding in self.config.bindings: + for term in ["lt", "gt", "eq"]: + matrix_id = f"{binding.id}_{term}" + children[matrix_id] = InputSeriesMatrix( + self.context, + self.config.next_file(f"{matrix_id}.txt"), + freq=MatrixFrequency(binding.time_step), + nb_columns=1 if term in ["lt", "gt"] else None, + default_empty=default_matrices[binding.time_step], + ) children["bindingconstraints"] = BindingConstraintsIni( self.context, self.config.next_file("bindingconstraints.ini") ) diff --git a/antarest/study/storage/study_upgrader/upgrader_870.py b/antarest/study/storage/study_upgrader/upgrader_870.py index 0b97debea9..5e0fd452f4 100644 --- a/antarest/study/storage/study_upgrader/upgrader_870.py +++ b/antarest/study/storage/study_upgrader/upgrader_870.py @@ -29,35 +29,18 @@ def upgrade_870(study_path: Path) -> None: for file in binding_constraints_files: name = Path(file).stem if os.path.getsize(file) == 0: - low_term = pandas.Series() - great_term = pandas.Series() - equality_term = pandas.Series() + lt, gt, eq = pandas.Series(), pandas.Series(), pandas.Series() else: df = pandas.read_csv(file, sep="\t", header=None) - low_term = df.iloc[:, 0] - great_term = df.iloc[:, 1] - equality_term = df.iloc[:, 2] - # noinspection PyTypeChecker - np.savetxt( - binding_constraints_path / f"{name}_lt.txt", - cast(npt.NDArray[np.float64], low_term.values), - delimiter="\t", - fmt="%.6f", - ) - # noinspection PyTypeChecker - np.savetxt( - binding_constraints_path / f"{name}_gt.txt", - cast(npt.NDArray[np.float64], great_term.values), - delimiter="\t", - fmt="%.6f", - ) - # noinspection PyTypeChecker - np.savetxt( - binding_constraints_path / f"{name}_eq.txt", - cast(npt.NDArray[np.float64], equality_term.values), - delimiter="\t", - fmt="%.6f", - ) + lt, gt, eq = df.iloc[:, 0], df.iloc[:, 1], df.iloc[:, 2] + for term in [lt, gt, eq]: + # noinspection PyTypeChecker + np.savetxt( + binding_constraints_path / f"{name}_{str(term)}.txt", + cast(npt.NDArray[np.float64], term.values), + delimiter="\t", + fmt="%.6f", + ) Path(file).unlink() # Add property group for every section in .ini file diff --git a/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/__init__.py b/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/__init__.py index 0a1b9046e5..02d9909415 100644 --- a/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/__init__.py +++ b/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/__init__.py @@ -1 +1 @@ -from . import series +from . import series_after_v87, series_before_v87 diff --git a/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_after_v87.py b/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_after_v87.py new file mode 100644 index 0000000000..0740d08951 --- /dev/null +++ b/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_after_v87.py @@ -0,0 +1,10 @@ +import numpy as np + +default_binding_constraint_hourly = np.zeros((8760, 1), dtype=np.float64) +default_binding_constraint_hourly.flags.writeable = False + +default_binding_constraint_daily = np.zeros((365, 1), dtype=np.float64) +default_binding_constraint_daily.flags.writeable = False + +default_binding_constraint_weekly = np.zeros((52, 1), dtype=np.float64) +default_binding_constraint_weekly.flags.writeable = False diff --git a/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series.py b/antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_before_v87.py similarity index 100% rename from antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series.py rename to antarest/study/storage/variantstudy/business/matrix_constants/binding_constraint/series_before_v87.py diff --git a/antarest/study/storage/variantstudy/business/matrix_constants_generator.py b/antarest/study/storage/variantstudy/business/matrix_constants_generator.py index 8cb973785e..d5c929e5bd 100644 --- a/antarest/study/storage/variantstudy/business/matrix_constants_generator.py +++ b/antarest/study/storage/variantstudy/business/matrix_constants_generator.py @@ -89,7 +89,7 @@ def _init(self) -> None: self.hashes[MISCGEN_TS] = self.matrix_service.create(FIXED_8_COLUMNS) # Binding constraint matrices - series = matrix_constants.binding_constraint.series + series = matrix_constants.binding_constraint.series_before_v87 self.hashes[BINDING_CONSTRAINT_HOURLY] = self.matrix_service.create(series.default_binding_constraint_hourly) self.hashes[BINDING_CONSTRAINT_DAILY] = self.matrix_service.create(series.default_binding_constraint_daily) self.hashes[BINDING_CONSTRAINT_WEEKLY] = self.matrix_service.create(series.default_binding_constraint_weekly) diff --git a/tests/variantstudy/model/command/test_manage_binding_constraints.py b/tests/variantstudy/model/command/test_manage_binding_constraints.py index a1309c2e47..a910a40db4 100644 --- a/tests/variantstudy/model/command/test_manage_binding_constraints.py +++ b/tests/variantstudy/model/command/test_manage_binding_constraints.py @@ -7,7 +7,7 @@ from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy from antarest.study.storage.variantstudy.business.command_extractor import CommandExtractor from antarest.study.storage.variantstudy.business.command_reverter import CommandReverter -from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series import ( +from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import ( default_binding_constraint_daily, default_binding_constraint_hourly, default_binding_constraint_weekly,