Skip to content
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
43 changes: 42 additions & 1 deletion CaseStudy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import ExcelReader
from InOutModule import Utilities
from printer import Printer
from InOutModule.printer import Printer

printer = Printer.getInstance()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ExcelReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion ExcelWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))

Expand Down
2 changes: 1 addition & 1 deletion nrel118-reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from printer import Printer
from InOutModule.printer import Printer

printer = Printer.getInstance()

Expand Down
5 changes: 5 additions & 0 deletions printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
py-modules = ['CaseStudy', 'ExcelReader', 'ExcelWriter', 'printer', 'PypsaReader', 'pypsa_helper', 'SQLiteWriter', 'TableDefinition']

[tool.pytest.ini_options]
pythonpath = [".."]
2 changes: 1 addition & 1 deletion tests/test_ExcelReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ExcelReader as ExcelReader
from ExcelWriter import ExcelWriter
from printer import Printer
from InOutModule.printer import Printer

printer = Printer.getInstance()

Expand Down
Loading