Skip to content

Commit

Permalink
resolved issues raised by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaasRostock committed Oct 24, 2024
1 parent 5fc2f67 commit 6b54983
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 111 deletions.
21 changes: 9 additions & 12 deletions src/farn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# from farn.core import Case as Case
# from farn.core import Parameter as Parameter

from .farn import (
run_farn as run_farn,
create_samples as create_samples,
create_cases as create_cases,
create_case_folders as create_case_folders,
create_param_dict_files as create_param_dict_files,
create_case_list_files as create_case_list_files,
execute_command_set as execute_command_set,
)
from .farn import (
run_farn,
create_samples,
create_cases,
create_case_folders,
create_param_dict_files,
create_case_list_files,
execute_command_set,
)
64 changes: 37 additions & 27 deletions src/farn/cli/farn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
# If we did NOT remove the current directory from the Python search path,
# Python would start searching for the imported names within the current file (farn.py)
# instead of the package 'farn' (and the import statements fail).
# sys.path = sys.path[1:]
sys.path = [path for path in sys.path if Path(path) != Path(__file__).parent]
from farn import run_farn # noqa E402
from farn.utils.logging import configure_logging # noqa E402
from farn import run_farn # noqa: E402
from farn.utils.logging import configure_logging # noqa: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,7 +45,10 @@ def _argparser() -> argparse.ArgumentParser:
"-s",
"--sample",
action="store_true",
help="read farn dict file, run the sampling defined for each layer and save the sampled farnDict file with prefix sampled.",
help=(
"read farn dict file, run the sampling defined for each layer "
"and save the sampled farnDict file with prefix sampled."
),
default=False,
required=False,
)
Expand Down Expand Up @@ -139,9 +141,9 @@ def main() -> None:
parser = _argparser()
try:
args = parser.parse_args()
except Exception:
except Exception: # noqa: BLE001
parser.print_help()
exit(0)
sys.exit(0)

# Configure Logging
# ..to console
Expand Down Expand Up @@ -174,7 +176,7 @@ def main() -> None:
if not farn_dict_file.is_file():
logger.error(f"farn: File {farn_dict_file} not found.")
# easter egg: Generate Barnsley fern
# _generate_barnsley_fern()
# _generate_barnsley_fern() # noqa: ERA001
return

logger.info(
Expand Down Expand Up @@ -203,17 +205,16 @@ def _generate_barnsley_fern() -> None:
easter egg: Barnsley fern.
Barnsley Fern:
┌ ┐ ┌ ┐ ┌ ┐
| a b | | x | | e |
┌ ┐ ┌ ┐ ┌ ┐
| a b | | x | | e |
ƒ(x,y) = | | | | + | |
| c d | | y | | f |
└ ┘ └ ┘ └ ┘
w a b c d e f p Portion generated
ƒ1 0 0 0 0.16 0 0 0.01 Stem
ƒ2 0.85 0.04 −0.04 0.85 0 1.60 0.85 Successively smaller leaflets
ƒ3 0.20 −0.26 0.23 0.22 0 1.60 0.07 Largest left-hand leaflet
ƒ4 −0.15 0.28 0.26 0.24 0 0.44 0.07 Largest right-hand leaflet
| c d | | y | | f |
└ ┘ └ ┘ └ ┘
w a b c d e f p Portion generated
ƒ1 0 0 0 0.16 0 0 0.01 Stem
ƒ2 0.85 0.04 -0.04 0.85 0 1.60 0.85 Successively smaller leaflets
ƒ3 0.20 -0.26 0.23 0.22 0 1.60 0.07 Largest left-hand leaflet
ƒ4 -0.15 0.28 0.26 0.24 0 0.44 0.07 Largest right-hand leaflet
"""
import tempfile
import tkinter as tk
Expand Down Expand Up @@ -254,12 +255,20 @@ def t4(p: tuple[float, float]) -> tuple[float, float]:
rnd2 = rng.normal(1, 0)
e = 1
s = 0
rnd3 = (rng.normal(e, s), rng.normal(e, s), rng.normal(e, s))
rnd3 = (
rng.normal(e, s),
rng.normal(e, s),
rng.normal(e, s),
)
while ii < end:
rnd = rng.random()
rnd2 = rng.normal(1, 0)
if ii % 1 == 0:
rnd3 = (rng.normal(e, s), rng.normal(e, s), rng.normal(e, s))
rnd3 = (
rng.normal(e, s),
rng.normal(e, s),
rng.normal(e, s),
)
rgb = [148, 204, 48]
if rnd <= (0.01 * rnd2):
p = t1(p)
Expand All @@ -269,23 +278,25 @@ def t4(p: tuple[float, float]) -> tuple[float, float]:
p = t3(p)
else:
p = t4(p)
# ImageDraw.Draw(im,)
draw.point( # pyright: ignore [reportUnknownMemberType]
draw.point(
(p[0] * scale + x_offset, p[1] * scale),
fill=(int(rgb[0] * rnd3[0]), int(rgb[1] * rnd3[1]), int(rgb[2] * rnd3[2])),
fill=(
int(rgb[0] * rnd3[0]),
int(rgb[1] * rnd3[1]),
int(rgb[2] * rnd3[2]),
),
)

ii += 1

del draw

with tempfile.TemporaryDirectory() as temp_dir:
# im.save(Path(os.getenv('HOME')) / 'splash.png')
temp_file = Path(temp_dir) / "splash.png"
im.save(temp_file) # pyright: ignore [reportUnknownMemberType]
im.save(temp_file)

root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(boolean=True)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(
Expand All @@ -297,10 +308,9 @@ def t4(p: tuple[float, float]) -> tuple[float, float]:
screen_height / 2 - y_size / 2,
)
)
# image = tk.PhotoImage(file=Path(os.getenv('HOME')) / 'splash.png')
image = tk.PhotoImage(file=temp_file)
canvas = tk.Canvas(root, height=y_size, width=x_size, bg="dark slate gray")
_ = canvas.create_image(x_size / 2, y_size / 2, image=image) # type: ignore
_ = canvas.create_image(x_size / 2, y_size / 2, image=image) # pyright: ignore[reportUnknownMemberType]
canvas.pack()
_ = root.after(3000, root.destroy)
root.mainloop()
Expand Down
12 changes: 6 additions & 6 deletions src/farn/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .parameter import Parameter as Parameter
from .case import (
CaseStatus as CaseStatus,
Case as Case,
Cases as Cases,
)
from farn.core.parameter import Parameter
from farn.core.case import (
CaseStatus,
Case,
Cases,
)
81 changes: 50 additions & 31 deletions src/farn/core/case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# pyright: reportUnknownMemberType=false
# ruff: noqa: N806
from __future__ import annotations

import logging
import re
from collections.abc import MutableMapping, MutableSequence, Sequence
Expand Down Expand Up @@ -47,19 +50,20 @@ class Case:
- ..
"""

def __init__(
def __init__( # noqa: PLR0913
self,
case: str = "",
layer: str = "",
level: int = 0,
no_of_samples: int = 0,
index: int = 0,
path: Path | None = None,
*,
is_leaf: bool = False,
condition: MutableMapping[str, str] | None = None,
parameters: MutableSequence[Parameter] | None = None,
command_sets: MutableMapping[str, list[str]] | None = None,
):
) -> None:
self.case: str | None = case
self.layer: str | None = layer
self.level: int = level
Expand Down Expand Up @@ -88,17 +92,19 @@ def is_valid(self) -> bool:
return True

# Check whether filter expression is defined.
# If filter expression is missing, condition cannot be evaluated but case is, by default, still considered valid.
filter_expression = self.condition["_filter"] if "_filter" in self.condition else None
# If filter expression is missing, condition cannot be evaluated but case is, by default,
# still considered valid.
filter_expression = self.condition.get("_filter", None)
if not filter_expression:
logger.warning(
f"Layer {self.layer}: _condition element found but no _filter element defined therein. "
f"As the filter expression is missing, the condition cannot be evalued. Case {self.case} is hence considered valid. "
f"As the filter expression is missing, the condition cannot be evalued. "
f"Case {self.case} is hence considered valid."
)
return True

# Check whether optional argument '_action' is defined. Use default action, if not.
action = self.condition["_action"] if "_action" in self.condition else None
action = self.condition.get("_action", None)
if not action:
logger.warning(
f"Layer {self.layer}: No _action defined in _condition element. Default action 'exclude' is used. "
Expand Down Expand Up @@ -144,20 +150,21 @@ def is_valid(self) -> bool:
"condition",
"command_sets",
]:
locals()[attribute] = eval(f"self.{attribute}")
locals()[attribute] = eval(f"self.{attribute}") # noqa: S307
available_vars.add(attribute)
except Exception:
except Exception: # noqa: PERF203
logger.exception(
f"Layer {self.layer}, case {self.case} validity check: case {self.case} is invalid: "
f"Reading case property '{attribute}' failed."
)
return False

# Read all parameter names and their associated values defined in current case, and assign them to local in-memory variables
# Read all parameter names and their associated values defined in current case
# and assign them to local in-memory variables.
for parameter in self.parameters:
if parameter.name and not re.match("^_", parameter.name):
try:
exec(f"{parameter.name} = {parameter.value}")
exec(f"{parameter.name} = {parameter.value}") # noqa: S102
available_vars.add(parameter.name)
except Exception:
logger.exception(
Expand All @@ -173,13 +180,14 @@ def is_valid(self) -> bool:
# Evaluate filter expression
filter_expression_evaluates_to_true = False
try:
filter_expression_evaluates_to_true = eval(filter_expression)
except Exception:
filter_expression_evaluates_to_true = eval(filter_expression) # noqa: S307
except Exception: # noqa: BLE001
# In case evaluation of the filter expression fails, processing will not stop.
# However, a warning will be logged and the respective case will be considered valid.
logger.warning(
f"Layer {self.layer}, case {self.case} evaluation of the filter expression failed:\n"
f"\tOne or more of the variables used in the filter expression are not defined or not accessible in the current layer.\n"
f"\tOne or more of the variables used in the filter expression are not defined "
f"or not accessible in the current layer.\n"
f"\t\tLayer: {self.layer}\n"
f"\t\tLevel: {self.level}\n"
f"\t\tCase: {self.case}\n"
Expand Down Expand Up @@ -213,7 +221,7 @@ def is_valid(self) -> bool:
def add_parameters(
self,
parameters: MutableSequence[Parameter] | MutableMapping[str, str] | None = None,
):
) -> None:
"""Manually add extra parameters."""
if isinstance(parameters, MutableSequence):
self.parameters.extend(parameters)
Expand All @@ -228,9 +236,8 @@ def add_parameters(
f"Layer {self.layer}, case {self.case} add_parameters failed:\n"
f"\tWrong input data format for additional parameters.\n"
)
exit(1)

return True
return

def to_dict(self) -> dict[str, Any]:
"""Return a dict with all case attributes.
Expand Down Expand Up @@ -273,16 +280,17 @@ class Cases(list[Case]):
def add_parameters(
self,
parameters: MutableSequence[Parameter] | MutableMapping[str, str] | None = None,
):
) -> None:
"""Manually add extra parameters."""
_cases: list[Case] = deepcopy(self)
for case in _cases:
_ = case.add_parameters(parameters)

return False
return

def to_pandas(
self,
*,
use_path_as_index: bool = True,
parameters_only: bool = False,
) -> DataFrame:
Expand Down Expand Up @@ -313,35 +321,45 @@ def to_pandas(
if not parameter.name:
parameter.name = "NA"

series: dict[str, Series] = { # pyright: ignore
series: dict[str, Series] = { # pyright: ignore[reportMissingTypeArgument]
"case": Series(data=None, dtype=np.dtype(str), name="case"),
"path": Series(data=None, dtype=np.dtype(str), name="path"),
}

for _index, case in enumerate(_cases):
# TODO @CLAROS: Check whether we can replace .loc[_index] with .iloc[_index]
# and .loc[_index] with .at[_index]
# CLAROS, 2024-10-24
if case.case:
series["case"].loc[_index] = case.case # pyright: ignore
series["path"].loc[_index] = str(case.path) # pyright: ignore
series["case"].loc[_index] = case.case # type: ignore[call-overload, reportCallIssue]
series["path"].loc[_index] = str(case.path) # type: ignore[call-overload, reportCallIssue]
if case.parameters:
for parameter in case.parameters:
if parameter.name not in series:
series[parameter.name] = Series(
data=None,
dtype=parameter.dtype, # pyright: ignore
name=parameter.name,
)
if parameter.dtype is not None:
series[parameter.name] = Series(
data=None,
dtype=parameter.dtype,
name=parameter.name,
)
else:
series[parameter.name] = Series(
data=None,
name=parameter.name,
)

if parameter.value is not None:
series[parameter.name].loc[_index] = parameter.value # pyright: ignore
series[parameter.name].loc[_index] = parameter.value # type: ignore[call-overload, reportCallIssue]

if parameters_only:
_ = series.pop("case")
if not use_path_as_index:
_ = series.pop("path")

df_X = DataFrame(data=series) # noqa: N806
df_X = DataFrame(data=series)

if use_path_as_index:
df_X.set_index("path", inplace=True)
df_X = df_X.set_index("path")

return df_X

Expand All @@ -353,15 +371,16 @@ def to_numpy(self) -> ndarray[Any, Any]:
ndarray[Any, Any]
2-dimensional numpy array with case specific parameter values of all cases.
"""
df_X: DataFrame = self.to_pandas(parameters_only=True) # noqa: N806
df_X: DataFrame = self.to_pandas(parameters_only=True)
array: ndarray[Any, Any] = df_X.to_numpy()
return array

def filter(
self,
levels: int | Sequence[int] = -1,
*,
valid_only: bool = True,
) -> "Cases":
) -> Cases:
"""Return a sub-set of cases according to the passed in selection criteria.
Parameters
Expand Down
Loading

0 comments on commit 6b54983

Please sign in to comment.