Skip to content

Commit

Permalink
feat(upgrader): simplify upgrade_870 implementation and improve TU …
Browse files Browse the repository at this point in the history
…coverage
  • Loading branch information
laurent-laporte-pro committed Mar 11, 2024
1 parent 0b27f16 commit 1417928
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
49 changes: 22 additions & 27 deletions antarest/study/storage/study_upgrader/upgrader_870.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import glob
import os.path
import typing as t
from pathlib import Path
from typing import cast

import numpy as np
import numpy.typing as npt
import pandas
import pandas as pd

from antarest.study.storage.rawstudy.ini_reader import IniReader
from antarest.study.storage.rawstudy.ini_writer import IniWriter
from antarest.study.storage.rawstudy.model.filesystem.root.settings.generaldata import DUPLICATE_KEYS


# noinspection SpellCheckingInspection
def upgrade_870(study_path: Path) -> None:
"""
Upgrade the study configuration to version 870.
Expand All @@ -25,40 +23,37 @@ def upgrade_870(study_path: Path) -> None:

# Split existing binding constraints in 3 different files
binding_constraints_path = study_path / "input" / "bindingconstraints"
binding_constraints_files = glob.glob(str(binding_constraints_path / "*.txt"))
binding_constraints_files = binding_constraints_path.glob("*.txt")
for file in binding_constraints_files:
name = Path(file).stem
if os.path.getsize(file) == 0:
lt, gt, eq = pandas.Series(), pandas.Series(), pandas.Series()
name = file.stem
if file.stat().st_size == 0:
lt, gt, eq = pd.Series(), pd.Series(), pd.Series()
else:
df = pandas.read_csv(file, sep="\t", header=None)
df = pd.read_csv(file, sep="\t", header=None)
lt, gt, eq = df.iloc[:, 0], df.iloc[:, 1], df.iloc[:, 2]
for term, suffix in zip([lt, gt, eq], ["lt", "gt", "eq"]):
# noinspection PyTypeChecker
np.savetxt(
binding_constraints_path / f"{name}_{suffix}.txt",
cast(npt.NDArray[np.float64], term.values),
t.cast(npt.NDArray[np.float64], term.values),
delimiter="\t",
fmt="%.6f",
)
Path(file).unlink()
file.unlink()

# Add property group for every section in .ini file
ini_file_path = binding_constraints_path / "bindingconstraints.ini"
reader = IniReader(DUPLICATE_KEYS)
data = reader.read(ini_file_path)
for key in list(data.keys()):
data[key]["group"] = "default"
writer = IniWriter(special_keys=DUPLICATE_KEYS)
writer.write(data, ini_file_path)
data = IniReader().read(ini_file_path)
for section in data:
data[section]["group"] = "default"
IniWriter().write(data, ini_file_path)

# Add properties for thermal clusters in .ini file
areas = glob.glob(str(study_path.joinpath("input/thermal/clusters/*")))
for area in areas:
ini_file_path = Path(area) / "list.ini"
data = reader.read(ini_file_path)
for key in list(data.keys()):
data[key]["costgeneration"] = "SetManually"
data[key]["efficiency"] = 100
data[key]["variableomcost"] = 0
writer.write(data, ini_file_path)
ini_files = study_path.glob("input/thermal/clusters/*/list.ini")
for ini_file_path in ini_files:
data = IniReader().read(ini_file_path)
for section in data:
data[section]["costgeneration"] = "SetManually"
data[section]["efficiency"] = 100
data[section]["variableomcost"] = 0
IniWriter().write(data, ini_file_path)
14 changes: 14 additions & 0 deletions tests/storage/study_upgrader/test_upgrade_870.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ def test_nominal_case(study_assets: StudyAssets):
actual_input_path = study_assets.study_dir.joinpath("input")
expected_input_path = study_assets.expected_dir.joinpath("input")
assert are_same_dir(actual_input_path, expected_input_path)


def test_empty_binding_constraints(study_assets: StudyAssets):
"""
Check that binding constraints and thermal folders are correctly modified
"""

# upgrade the study
upgrade_870(study_assets.study_dir)

# compare input folders (bindings + thermals)
actual_input_path = study_assets.study_dir.joinpath("input")
expected_input_path = study_assets.expected_dir.joinpath("input")
assert are_same_dir(actual_input_path, expected_input_path)
Binary file not shown.
Binary file not shown.

0 comments on commit 1417928

Please sign in to comment.