diff --git a/CaseStudy.py b/CaseStudy.py index 8f1bb5f..614a128 100644 --- a/CaseStudy.py +++ b/CaseStudy.py @@ -12,7 +12,7 @@ import ExcelReader from InOutModule import Utilities -from printer import Printer +from InOutModule.printer import Printer printer = Printer.getInstance() @@ -196,6 +196,8 @@ def __init__(self, printer.error(f"Error reading for '{attr_name}': {exc}") raise exc + self.check_duplicate_lines_dPower_Network() + # === SEQUENTIAL DEPENDENTS === if dPower_WeightsRP is not None: self.dPower_WeightsRP = dPower_WeightsRP @@ -256,6 +258,45 @@ def copy(self): new_self = copy.deepcopy(self) return new_self + def check_duplicate_lines_dPower_Network(self): + """ + Check dPower_Network for duplicate and parallel lines. + i -> j and j -> i are considered as the same line. + Lines with same endpoints and circuit -> error. + Lines with same endpoints and different circuits-> warning. + :return: None + """ + dPower_Network = self.dPower_Network.reset_index() + line_keys_without_circuit = dPower_Network.apply(lambda row: frozenset((row["i"], row["j"])), axis=1) + dPower_Network["_line_key_without_circuit"] = line_keys_without_circuit + line_keys = dPower_Network.apply(lambda row: (row["scenario"], line_keys_without_circuit[row.name], row["c"]), axis=1) + duplicate_lines = dPower_Network[line_keys.duplicated(keep=False)] + + if not duplicate_lines.empty: + duplicate_line = duplicate_lines.iloc[0] + raise ValueError( + f"Duplicate network line found in (at least) scenario '{duplicate_line['scenario']}' " + f"for line {duplicate_line['i']} <-> {duplicate_line['j']} " + f"with circuit '{duplicate_line['c']}'. " + "If the lines should be parallel, assign different 'c' for each parallel line." + ) + + lines_with_multiple_circuits = dPower_Network[dPower_Network.groupby(["scenario", "_line_key_without_circuit"])["c"].transform("nunique") > 1] + + if not lines_with_multiple_circuits.empty: + parallel_line = lines_with_multiple_circuits.iloc[0] # for printing the example: use first row that belongs to a parallel line group + parallel_group = lines_with_multiple_circuits[ + (lines_with_multiple_circuits["scenario"] == parallel_line["scenario"]) # same scenario + & (lines_with_multiple_circuits["_line_key_without_circuit"] == parallel_line["_line_key_without_circuit"]) + ] + circuits = parallel_group["c"].head(2).tolist() # show only first two circuit IDs + + printer.warning( + f"Parallel network lines found in (at least) scenario '{parallel_line['scenario']}' " + f"for line {parallel_line['i']} <-> {parallel_line['j']} " + f"with circuits {circuits}." + ) + def equal_to(self, cs: typing.Self) -> bool: """ Check if this CaseStudy is equal to another CaseStudy, checking all dataframes for equality. diff --git a/ExcelReader.py b/ExcelReader.py index 6ae4ab9..d940c42 100644 --- a/ExcelReader.py +++ b/ExcelReader.py @@ -5,7 +5,7 @@ from openpyxl import load_workbook from openpyxl.utils.cell import get_column_letter -from printer import Printer +from InOutModule.printer import Printer printer = Printer.getInstance() diff --git a/ExcelWriter.py b/ExcelWriter.py index f1ee83e..9dfaccb 100644 --- a/ExcelWriter.py +++ b/ExcelWriter.py @@ -19,7 +19,7 @@ if TYPE_CHECKING: from CaseStudy import CaseStudy from TableDefinition import CellStyle, Alignment, Font, Color, Text, Column, NumberFormat, TableDefinition -from printer import Printer +from InOutModule.printer import Printer package_directory_ExcelWriter = os.path.dirname(os.path.abspath(__file__)) diff --git a/nrel118-reader.py b/nrel118-reader.py index 3ef7ec4..784f31e 100644 --- a/nrel118-reader.py +++ b/nrel118-reader.py @@ -2,7 +2,7 @@ import pandas as pd -from printer import Printer +from InOutModule.printer import Printer printer = Printer.getInstance() diff --git a/printer.py b/printer.py index 1517c9c..19baf42 100644 --- a/printer.py +++ b/printer.py @@ -5,6 +5,11 @@ from rich.console import Console from rich.markup import escape +if __name__ == "printer": + raise ImportError( + "Import Printer as 'from InOutModule.printer import Printer', not 'from printer import Printer'" + ) + class Printer: """ diff --git a/pyproject.toml b/pyproject.toml index a72a436..25294be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,4 +8,7 @@ version = "0.0.0-dev" readme = "README.md" [tool.setuptools] -py-modules = ['CaseStudy', 'ExcelReader', 'ExcelWriter', 'printer', 'PypsaReader', 'pypsa_helper', 'SQLiteWriter', 'TableDefinition'] \ No newline at end of file +py-modules = ['CaseStudy', 'ExcelReader', 'ExcelWriter', 'printer', 'PypsaReader', 'pypsa_helper', 'SQLiteWriter', 'TableDefinition'] + +[tool.pytest.ini_options] +pythonpath = [".."] diff --git a/tests/test_ExcelReaderWriter.py b/tests/test_ExcelReaderWriter.py index e33f829..baa578f 100644 --- a/tests/test_ExcelReaderWriter.py +++ b/tests/test_ExcelReaderWriter.py @@ -2,7 +2,7 @@ import ExcelReader as ExcelReader from ExcelWriter import ExcelWriter -from printer import Printer +from InOutModule.printer import Printer printer = Printer.getInstance()