Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 8 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,22 @@ repos:
rev: v5.0.2
hooks:
- id: reuse
- repo: https://github.com/pycqa/isort
rev: 6.0.1
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.11.12
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 25.1.0
hooks:
- id: black-jupyter
# Run the linter.
- id: ruff-check
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.16.0
hooks:
- id: mypy
additional_dependencies: [numpy, pandas]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
files: ^src/.+\.py$
language: system
types: [ python ]
args: [ "--rcfile=pyproject.toml" ]
require_serial: true
- id: pytest
name: pytest
entry: pytest
Expand Down
18 changes: 8 additions & 10 deletions docs/examples/arrow_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@
"outputs": [],
"source": [
"%%capture cap --no-stderr\n",
"from IPython.display import display\n",
"from typing import Iterable\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import pyarrow as pa\n",
"from IPython.display import display\n",
"from power_grid_model import (\n",
" PowerGridModel,\n",
" initialize_array,\n",
" CalculationMethod,\n",
" power_grid_meta_data,\n",
" ComponentAttributeFilterOptions,\n",
" ComponentType,\n",
" DatasetType,\n",
" ComponentAttributeFilterOptions,\n",
" PowerGridModel,\n",
" power_grid_meta_data,\n",
")\n",
"from power_grid_model.data_types import SingleColumnarData\n",
"import pyarrow as pa\n",
"import pandas as pd\n",
"import numpy as np"
"from power_grid_model.data_types import SingleColumnarData"
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/pandapower_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"outputs": [],
"source": [
"import warnings\n",
"\n",
"import pandapower as pp\n",
"\n",
"warnings.filterwarnings(\"ignore\", module=\"pandapower\", category=FutureWarning) # Hide warnings related to pandas\n",
Expand Down Expand Up @@ -1046,8 +1047,9 @@
"source": [
"%%capture cap --no-stderr\n",
"\n",
"from power_grid_model import PowerGridModel, CalculationType\n",
"from power_grid_model import CalculationType, PowerGridModel\n",
"from power_grid_model.validation import assert_valid_input_data\n",
"\n",
"from power_grid_model_io.converters import PandaPowerConverter\n",
"\n",
"output_file = \"data/pandapower/example_simple_output.json\"\n",
Expand Down
7 changes: 5 additions & 2 deletions docs/examples/pgm_json_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
],
"source": [
"from pathlib import Path\n",
"from IPython.display import display, Markdown\n",
"\n",
"from IPython.display import Markdown, display\n",
"\n",
"with Path(source_file).open() as json_file:\n",
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
Expand Down Expand Up @@ -578,7 +579,8 @@
],
"source": [
"from pathlib import Path\n",
"from IPython.display import display, Markdown\n",
"\n",
"from IPython.display import Markdown, display\n",
"\n",
"with Path(destination_file).open() as json_file:\n",
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
Expand All @@ -599,6 +601,7 @@
"source": [
"%%capture cap --no-stderr\n",
"from power_grid_model import PowerGridModel\n",
"\n",
"from power_grid_model_io.converters import PgmJsonConverter\n",
"\n",
"source_file = \"data/tiny-net/input.json\"\n",
Expand Down
8 changes: 5 additions & 3 deletions docs/examples/vision_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@
],
"source": [
"from pathlib import Path\n",
"from IPython.display import display, Markdown\n",
"\n",
"from IPython.display import Markdown, display\n",
"\n",
"with Path(destination_file).open() as json_file:\n",
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
Expand Down Expand Up @@ -651,9 +652,10 @@
"source": [
"%%capture cap --no-stderr\n",
"\n",
"from power_grid_model import PowerGridModel, CalculationType\n",
"from power_grid_model import CalculationType, PowerGridModel\n",
"from power_grid_model.validation import assert_valid_input_data\n",
"from power_grid_model_io.converters import VisionExcelConverter, PgmJsonConverter\n",
"\n",
"from power_grid_model_io.converters import PgmJsonConverter, VisionExcelConverter\n",
"\n",
"source_file = \"data/vision/example.xlsx\"\n",
"destination_file = \"data/vision/sym_output.json\"\n",
Expand Down
57 changes: 34 additions & 23 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dev = [
"pylint",
"pytest",
"pytest-cov",
"ruff",
"pydantic>2", # Used in unit tests
"pandapower>2.11.1",
]
Expand Down Expand Up @@ -89,33 +90,43 @@ addopts = [
]
xfail_strict = true

[tool.black]
[tool.ruff]
# Same as Black.
line-length = 120
target-version = ["py311", "py312", "py313"]
indent-width = 4
show-fixes = true

[tool.isort]
profile = "black"
line_length = 120
# Assume Python 3.11
target-version = "py311"

[tool.pylint]
max-line-length = 120
ignore-paths = [
"docs/",
"examples/",
"tests/",
"setup.py",
]
disable = [
"fixme", # allow todos
]
good-names=[
"ex", # exception
"i", # iterator or current
"p", # active power
"q", # reactive power
"s", # power (p + q)
"v", # voltage
[tool.ruff.lint]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# isort
"I",
"SIM",
"YTT",
"BLE",
"PERF",
"Q",
"ICN",
"ISC",
"G",
"LOG",
"EXE",
"FA",
"FURB",
"FLY",
"SLOT",
]
ignore = ["SIM108", "SIM118", "SIM110", "SIM211"]

[tool.ruff.lint.isort]
# Imports that are imported using keyword "as" and are from the same source - are combined.
combine-as-imports = true

[tool.mypy]
follow_imports = "silent"
Expand Down
3 changes: 1 addition & 2 deletions set_pypi_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import os
from pathlib import Path
from typing import cast

import requests

Expand All @@ -26,7 +25,7 @@ def set_version(pkg_dir: Path):
ref = os.environ["GITHUB_REF"]
build_number = os.environ["GITHUB_RUN_NUMBER"]
# short hash number in numeric
short_hash = f'{int(f"0x{sha[0:6]}", base=16):08}'
short_hash = f"{int(f'0x{sha[0:6]}', base=16):08}"

if "main" in ref:
# main branch
Expand Down
2 changes: 2 additions & 0 deletions src/power_grid_model_io/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
from power_grid_model_io.converters.pandapower_converter import PandaPowerConverter
from power_grid_model_io.converters.pgm_json_converter import PgmJsonConverter
from power_grid_model_io.converters.vision_excel_converter import VisionExcelConverter

__all__ = ["PandaPowerConverter", "PgmJsonConverter", "VisionExcelConverter"]
1 change: 1 addition & 0 deletions src/power_grid_model_io/converters/base_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Abstract converter class
"""

import logging
from abc import ABC, abstractmethod
from typing import Generic, Optional, Tuple, TypeVar
Expand Down
6 changes: 3 additions & 3 deletions src/power_grid_model_io/converters/pandapower_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _create_pgm_input_sources(self):
}
if not all(checks.values()):
failed_checks = ", ".join([key for key, value in checks.items() if not value])
logger.warning(f"Zero sequence parameters given in external grid shall be ignored:{failed_checks}")
logger.warning("Zero sequence parameters given in external grid shall be ignored: %s", failed_checks)

pgm_sources = initialize_array(data_type="input", component_type="source", shape=len(pp_ext_grid))
pgm_sources["id"] = self._generate_ids("ext_grid", pp_ext_grid.index)
Expand Down Expand Up @@ -753,7 +753,7 @@ def _create_pgm_input_transformers(self): # pylint: disable=too-many-statements
}
if not all(checks.values()):
failed_checks = ", ".join([key for key, value in checks.items() if not value])
logger.warning(f"Zero sequence parameters given in trafo shall be ignored:{failed_checks}")
logger.warning("Zero sequence parameters given in trafo shall be ignored: %s", failed_checks)

# Do not use taps when mandatory tap data is not available
no_taps = np.equal(tap_side, None) | np.isnan(tap_pos) | np.isnan(tap_nom) | np.isnan(tap_size)
Expand Down Expand Up @@ -862,7 +862,7 @@ def _create_pgm_input_three_winding_transformers(self):
}
if not all(checks.values()):
failed_checks = ", ".join([key for key, value in checks.items() if not value])
logger.warning(f"Zero sequence parameters given in trafo3w are ignored: {failed_checks}")
logger.warning("Zero sequence parameters given in trafo3w are ignored: %s", failed_checks)

# Do not use taps when mandatory tap data is not available
no_taps = np.equal(tap_side, None) | np.isnan(tap_pos) | np.isnan(tap_nom) | np.isnan(tap_size)
Expand Down
2 changes: 1 addition & 1 deletion src/power_grid_model_io/converters/pgm_json_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _serialize_dataset(self, data: SingleDataset, extra_info: Optional[ExtraInfo
"""

# This should be a single data set
for component, array in data.items():
for array in data.values():
if not isinstance(array, np.ndarray) or array.ndim != 1:
raise ValueError("Invalid data format")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Vision Excel file store
"""

from pathlib import Path
from typing import Optional

Expand Down
2 changes: 2 additions & 0 deletions src/power_grid_model_io/data_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

from power_grid_model_io.data_types._data_types import ExtraInfo, ExtraInfoLookup, StructuredData
from power_grid_model_io.data_types.tabular_data import LazyDataFrame, TabularData

__all__ = ["ExtraInfo", "ExtraInfoLookup", "StructuredData", "LazyDataFrame", "TabularData"]
12 changes: 12 additions & 0 deletions src/power_grid_model_io/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@
value_or_default,
value_or_zero,
)

__all__ = [
"both_zeros_to_nan",
"complex_inverse_imaginary_part",
"complex_inverse_real_part",
"degrees_to_clock",
"get_winding",
"has_value",
"is_greater_than",
"value_or_default",
"value_or_zero",
]
1 change: 1 addition & 0 deletions src/power_grid_model_io/mappings/tabular_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Tabular data mapping helper class
"""

from typing import Dict, Generator, List, Tuple, Union

import structlog
Expand Down
1 change: 1 addition & 0 deletions src/power_grid_model_io/mappings/value_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Value substitution helper class
"""

from typing import Dict, Optional, Union

import structlog
Expand Down
1 change: 1 addition & 0 deletions src/power_grid_model_io/utils/auto_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Automatic ID generator class
"""

import collections
from collections.abc import Hashable
from typing import Any, Dict, List, Optional, Union
Expand Down
1 change: 1 addition & 0 deletions src/power_grid_model_io/utils/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
General dictionary utilities
"""

from copy import deepcopy
from typing import Dict

Expand Down
6 changes: 3 additions & 3 deletions src/power_grid_model_io/utils/excel_ambiguity_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- xml.etree.ElementTree for parsing XML structures within the Excel file.
- zipfile to handle the Excel file as a ZIP archive for parsing.
"""

import os
import xml.etree.ElementTree as ET
import zipfile
Expand Down Expand Up @@ -80,11 +81,10 @@ def _parse_zip(self, zip_file) -> List[Optional[str]]:
list: A list of shared strings used in the Excel file.
"""
shared_strings_path = SHARED_STR_PATH
shared_strings = []
shared_strings: List[Optional[str]] = []
with zip_file.open(shared_strings_path) as f:
tree = ET.parse(f)
for si in tree.findall(FIND_T, namespaces=XML_NAME_SPACE):
shared_strings.append(si.text)
shared_strings.extend(si.text for si in tree.findall(FIND_T, namespaces=XML_NAME_SPACE))
return shared_strings

def _get_column_names_from_row(self, row, shared_strings) -> List[Optional[str]]:
Expand Down
1 change: 1 addition & 0 deletions src/power_grid_model_io/utils/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Module utilities, expecially useful for loading optional dependencies
"""

from importlib import import_module
from typing import Callable

Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#
# SPDX-License-Identifier: MPL-2.0

from contextlib import suppress

import pandas as pd

try:
with suppress(pd.errors.OptionError):
pd.set_option("future.no_silent_downcasting", True)
except pd.errors.OptionError:
pass
6 changes: 3 additions & 3 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#
# SPDX-License-Identifier: MPL-2.0

from contextlib import suppress

import pandas as pd

try:
with suppress(pd.errors.OptionError):
# TODO(mgovers) We're ready for Pandas 3.x, but pandapower is not. Move to parent conftest when it is.
pd.set_option("mode.copy_on_write", True)
except pd.errors.OptionError:
pass
Loading
Loading