Skip to content

Commit

Permalink
attempt fix pylint
Browse files Browse the repository at this point in the history
Signed-off-by: Martijn Govers <[email protected]>
  • Loading branch information
mgovers committed Sep 30, 2024
1 parent 770f21a commit 76fb98c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 37 deletions.
1 change: 0 additions & 1 deletion .github/workflows/check-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ jobs:
- name: If needed raise error
run: |
if [[ `git status --porcelain --untracked-files=no` ]]; then
echo "Formatting not correct! See below the files which need to be reformatted!"
git status --porcelain --untracked-files=no
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

repos:
- repo: https://github.com/fsfe/reuse-tool
rev: v3.0.1
rev: v4.0.3
hooks:
- id: reuse
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.10.0
rev: 24.8.0
hooks:
- id: black-jupyter
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.2.0
rev: v1.11.2
hooks:
- id: mypy
additional_dependencies: [numpy, pandas]
Expand Down
3 changes: 2 additions & 1 deletion set_pypi_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
from pathlib import Path
from typing import cast

import requests

Expand Down Expand Up @@ -43,7 +44,7 @@ def set_version(pkg_dir: Path):
def get_pypi_latest():
request = requests.get("https://pypi.org/pypi/power-grid-model-io/json")
data = request.json()
version: str = data["info"]["version"]
version = str(data["info"]["version"])
return (int(x) for x in version.split("."))


Expand Down
132 changes: 104 additions & 28 deletions src/power_grid_model_io/converters/tabular_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Tabular Data Converter: Load data from multiple tables and use a mapping file to convert the data to PGM
"""

import inspect
import logging
from pathlib import Path
Expand Down Expand Up @@ -141,8 +142,7 @@ def _parse_data(self, data: TabularData, data_type: str, extra_info: Optional[Ex
)
return input_data

# pylint: disable = too-many-arguments
def _convert_table_to_component(
def _convert_table_to_component( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
data_type: str,
Expand Down Expand Up @@ -197,7 +197,10 @@ def _convert_table_to_component(

# Make sure that the "id" column is always parsed first (at least before "extra" is parsed)
attributes_without_filter = {k: v for k, v in attributes.items() if k != "filters"}
sorted_attributes = sorted(attributes_without_filter.items(), key=lambda x: "" if x[0] == "id" else x[0])
sorted_attributes = sorted(
attributes_without_filter.items(),
key=lambda x: "" if x[0] == "id" else x[0],
)

for attr, col_def in sorted_attributes:
self._convert_col_def_to_attribute(
Expand All @@ -224,8 +227,7 @@ def _parse_table_filters(self, data: TabularData, table: str, filtering_function
table_mask &= cast(pd.DataFrame, data[table]).apply(fn_ptr, axis=1, **kwargs).values
return table_mask

# pylint: disable = too-many-arguments
def _convert_col_def_to_attribute(
def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
pgm_data: np.ndarray,
Expand Down Expand Up @@ -282,15 +284,19 @@ def _convert_col_def_to_attribute(
return

attr_data = self._parse_col_def(
data=data, table=table, table_mask=table_mask, col_def=col_def, extra_info=extra_info
data=data,
table=table,
table_mask=table_mask,
col_def=col_def,
extra_info=extra_info,
)

if len(attr_data.columns) != 1:
raise ValueError(f"DataFrame for {component}.{attr} should contain a single column ({attr_data.columns})")

pgm_data[attr] = attr_data.iloc[:, 0]

def _handle_extra_info(
def _handle_extra_info( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
Expand Down Expand Up @@ -322,7 +328,11 @@ def _handle_extra_info(
return

extra = self._parse_col_def(
data=data, table=table, table_mask=table_mask, col_def=col_def, extra_info=None
data=data,
table=table,
table_mask=table_mask,
col_def=col_def,
extra_info=None,
).to_dict(orient="records")
for i, xtr in zip(uuids, extra):
xtr = {
Expand Down Expand Up @@ -369,7 +379,7 @@ def _serialize_data(self, data: Dataset, extra_info: Optional[ExtraInfo]) -> Tab
raise NotImplementedError("Batch data can not (yet) be stored for tabular data")
return TabularData(logger=self._log, **data)

def _parse_col_def(
def _parse_col_def( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
Expand All @@ -394,15 +404,22 @@ def _parse_col_def(
return self._parse_col_def_column_name(data=data, table=table, col_def=col_def, table_mask=table_mask)
if isinstance(col_def, dict):
return self._parse_col_def_filter(
data=data, table=table, table_mask=table_mask, col_def=col_def, extra_info=extra_info
data=data,
table=table,
table_mask=table_mask,
col_def=col_def,
extra_info=extra_info,
)
if isinstance(col_def, list):
return self._parse_col_def_composite(data=data, table=table, col_def=col_def, table_mask=table_mask)
raise TypeError(f"Invalid column definition: {col_def}")

@staticmethod
def _parse_col_def_const(
data: TabularData, table: str, col_def: Union[int, float], table_mask: Optional[np.ndarray] = None
data: TabularData,
table: str,
col_def: Union[int, float],
table_mask: Optional[np.ndarray] = None,
) -> pd.DataFrame:
"""Create a single column pandas DataFrame containing the const value.
Expand All @@ -423,7 +440,11 @@ def _parse_col_def_const(
return const_df

def _parse_col_def_column_name(
self, data: TabularData, table: str, col_def: str, table_mask: Optional[np.ndarray] = None
self,
data: TabularData,
table: str,
col_def: str,
table_mask: Optional[np.ndarray] = None,
) -> pd.DataFrame:
"""Extract a column from the data. If the column doesn't exist, check if the col_def is a special float value,
like 'inf'. If that's the case, create a single column pandas DataFrame containing the const value.
Expand Down Expand Up @@ -471,7 +492,7 @@ def _apply_multiplier(self, table: str, column: str, data: pd.Series) -> pd.Seri
except KeyError:
return data

def _parse_reference(
def _parse_reference( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
Expand Down Expand Up @@ -502,7 +523,7 @@ def _parse_reference(
result = queries.merge(other, how="left", left_on=query_column, right_on=key_column)
return result[[value_column]]

def _parse_col_def_filter(
def _parse_col_def_filter( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
Expand Down Expand Up @@ -553,18 +574,26 @@ def _parse_col_def_filter(
)
elif isinstance(sub_def, list):
col_data = self._parse_pandas_function(
data=data, table=table, table_mask=table_mask, fn_name=name, col_def=sub_def
data=data,
table=table,
table_mask=table_mask,
fn_name=name,
col_def=sub_def,
)
elif isinstance(sub_def, dict):
col_data = self._parse_function(
data=data, table=table, table_mask=table_mask, function=name, col_def=sub_def
data=data,
table=table,
table_mask=table_mask,
function=name,
col_def=sub_def,
)
else:
raise TypeError(f"Invalid {name} definition: {sub_def}")
data_frames.append(col_data)
return pd.concat(data_frames, axis=1)

def _parse_auto_id(
def _parse_auto_id( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
Expand Down Expand Up @@ -606,7 +635,11 @@ def _parse_auto_id(
raise TypeError(f"Invalid key definition type '{type(key_col_def).__name__}': {key_col_def}")

col_data = self._parse_col_def(
data=data, table=table, table_mask=table_mask, col_def=key_col_def, extra_info=None
data=data,
table=table,
table_mask=table_mask,
col_def=key_col_def,
extra_info=None,
)

def auto_id(row: np.ndarray):
Expand All @@ -622,7 +655,13 @@ def auto_id(row: np.ndarray):
# the first argument to be parsed.
if extra_info is not None and ref_table_str == table and pgm_id not in extra_info:
if ref_name is not None:
extra_info[pgm_id] = {"id_reference": {"table": ref_table_str, "name": ref_name, "key": key}}
extra_info[pgm_id] = {
"id_reference": {
"table": ref_table_str,
"name": ref_name,
"key": key,
}
}
else:
extra_info[pgm_id] = {"id_reference": {"table": ref_table_str, "key": key}}

Expand All @@ -632,8 +671,13 @@ def auto_id(row: np.ndarray):
return col_data
return col_data.apply(auto_id, axis=1, raw=True)

def _parse_pandas_function(
self, data: TabularData, table: str, fn_name: str, col_def: List[Any], table_mask: Optional[np.ndarray]
def _parse_pandas_function( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
fn_name: str,
col_def: List[Any],
table_mask: Optional[np.ndarray],
) -> pd.DataFrame:
"""Special vectorized functions.
Expand All @@ -652,7 +696,13 @@ def _parse_pandas_function(
if fn_name == "multiply":
fn_name = "prod"

col_data = self._parse_col_def(data=data, table=table, col_def=col_def, table_mask=table_mask, extra_info=None)
col_data = self._parse_col_def(
data=data,
table=table,
col_def=col_def,
table_mask=table_mask,
extra_info=None,
)

try:
fn_ptr = getattr(col_data, fn_name)
Expand All @@ -675,8 +725,13 @@ def _parse_pandas_function(

return pd.DataFrame(fn_ptr(axis=1))

def _parse_function(
self, data: TabularData, table: str, function: str, col_def: Dict[str, Any], table_mask: Optional[np.ndarray]
def _parse_function( # pylint: disable = too-many-arguments,too-many-positional-arguments
self,
data: TabularData,
table: str,
function: str,
col_def: Dict[str, Any],
table_mask: Optional[np.ndarray],
) -> pd.DataFrame:
"""Import the function by name and apply it to each row.
Expand All @@ -694,7 +749,13 @@ def _parse_function(
fn_ptr = get_function(function)
key_words = list(col_def.keys())
sub_def = list(col_def.values())
col_data = self._parse_col_def(data=data, table=table, col_def=sub_def, table_mask=table_mask, extra_info=None)
col_data = self._parse_col_def(
data=data,
table=table,
col_def=sub_def,
table_mask=table_mask,
extra_info=None,
)

if col_data.empty:
raise ValueError(f"Cannot apply function {function} to an empty DataFrame")
Expand All @@ -703,7 +764,11 @@ def _parse_function(
return pd.DataFrame(col_data)

def _parse_col_def_composite(
self, data: TabularData, table: str, col_def: list, table_mask: Optional[np.ndarray]
self,
data: TabularData,
table: str,
col_def: list,
table_mask: Optional[np.ndarray],
) -> pd.DataFrame:
"""Select multiple columns (each is created from a column definition) and return them as a new DataFrame.
Expand All @@ -717,7 +782,13 @@ def _parse_col_def_composite(
"""
assert isinstance(col_def, list)
columns = [
self._parse_col_def(data=data, table=table, col_def=sub_def, table_mask=table_mask, extra_info=None)
self._parse_col_def(
data=data,
table=table,
col_def=sub_def,
table_mask=table_mask,
extra_info=None,
)
for sub_def in col_def
]
return pd.concat(columns, axis=1)
Expand Down Expand Up @@ -748,7 +819,12 @@ def get_id(self, table: str, key: Mapping[str, int], name: Optional[str] = None)
raise KeyError((table, key, name))
return self._auto_id(item=(table, key, name), key=auto_id_key)

def get_ids(self, keys: pd.DataFrame, table: Optional[str] = None, name: Optional[str] = None) -> List[int]:
def get_ids(
self,
keys: pd.DataFrame,
table: Optional[str] = None,
name: Optional[str] = None,
) -> List[int]:
"""
Get a the numerical ID previously associated with the supplied name / key combination
Args:
Expand Down
4 changes: 2 additions & 2 deletions src/power_grid_model_io/converters/vision_excel_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class VisionExcelConverter(TabularConverter):
Vision Excel Converter: Load data from a Vision Excel export file and use a mapping file to convert the data to PGM
"""

def __init__(
def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
self,
source_file: Optional[Union[Path, str]] = None,
language: str = LANGUAGE_EN,
terms_changed: Optional[dict] = None,
mapping_file: Optional[Union[Path, str]] = None,
log_level: int = logging.INFO,
): # pylint: disable=too-many-arguments
):
_mapping_file = Path(
mapping_file if mapping_file is not None else str(DEFAULT_MAPPING_FILE).format(language=language)
)
Expand Down
2 changes: 1 addition & 1 deletion src/power_grid_model_io/functions/phase_to_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def reactive_power(p: float, cos_phi: float) -> float:
return p * math.sqrt(1 - cos_phi**2) / cos_phi


def power_wind_speed( # pylint: disable=too-many-arguments
def power_wind_speed( # pylint: disable=too-many-arguments,too-many-positional-arguments
p_nom: float,
wind_speed: float,
cut_in_wind_speed: float = 3.0,
Expand Down

0 comments on commit 76fb98c

Please sign in to comment.