Skip to content

Using ruff for linting #02 #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ select = [
# "I", # isort (Ensures imports are sorted properly)
# "B", # flake8-bugbear (Detects likely bugs and bad practices)
# "TID", # flake8-tidy-imports (Checks for banned or misplaced imports)
# "UP", # pyupgrade (Automatically updates old Python syntax)
"UP", # pyupgrade (Automatically updates old Python syntax)
# "YTT", # flake8-2020 (Detects outdated Python 2/3 compatibility issues)
# "FLY", # flynt (Converts old-style string formatting to f-strings)
# "PIE", # flake8-pie
Expand Down
14 changes: 6 additions & 8 deletions src/lasso/diffcrash/diffcrash_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time
import typing
from concurrent import futures
from typing import List, Union
from typing import Union
from pathlib import Path
import psutil

Expand Down Expand Up @@ -688,9 +688,7 @@ def run_import(self, pool: futures.ThreadPoolExecutor):

if n_imports_finished != n_new_imports_finished:
# pylint: disable = consider-using-f-string
msg = "Running Imports ... [{0}/{1}] - {2:3.2f}%\r".format(
n_new_imports_finished, len(return_code_futures), percentage
)
msg = f"Running Imports ... [{n_new_imports_finished}/{len(return_code_futures)}] - {percentage:3.2f}%\r"
print(str_running(msg), end="", flush=True)
self.logger.info(msg)

Expand Down Expand Up @@ -1091,7 +1089,7 @@ def is_logfile_successful(self, logfile: Path) -> bool:
success : `bool`
"""

with open(logfile, "r", encoding="utf-8") as fp:
with open(logfile, encoding="utf-8") as fp:
for line in fp:
if "successfully" in line:
return True
Expand Down Expand Up @@ -1181,7 +1179,7 @@ def clear_project_dir(self):
# reinit logger
self.logger = self._setup_logger()

def read_config_file(self, config_file: str) -> List[str]:
def read_config_file(self, config_file: str) -> list[str]:
"""Read a diffcrash config file

Parameters
Expand All @@ -1203,7 +1201,7 @@ def read_config_file(self, config_file: str) -> List[str]:
# pylint: disable = too-many-branches
# pylint: disable = too-many-statements

with open(config_file, "r", encoding="utf-8") as conf:
with open(config_file, encoding="utf-8") as conf:
conf_lines = conf.readlines()
line = 0

Expand Down Expand Up @@ -1303,7 +1301,7 @@ def read_config_file(self, config_file: str) -> List[str]:

return export_item_list

def check_if_logfiles_show_success(self, pattern: str) -> List[str]:
def check_if_logfiles_show_success(self, pattern: str) -> list[str]:
"""Check if a logfiles with given pattern show success

Parameters
Expand Down
5 changes: 3 additions & 2 deletions src/lasso/dimred/dimred_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import sys
import time
from concurrent.futures.process import ProcessPoolExecutor
from typing import Sequence, Tuple, Union
from typing import Union
from collections.abc import Sequence

import h5py
import numpy as np
Expand Down Expand Up @@ -529,7 +530,7 @@ def _parse_simulation_and_reference_runs(
reference_run_pattern: Union[None, str],
exclude_runs: Sequence[str],
table: Table,
) -> Tuple[Sequence[str], str, Sequence[str]]:
) -> tuple[Sequence[str], str, Sequence[str]]:
# pylint: disable = too-many-locals

# search all denoted runs
Expand Down
11 changes: 6 additions & 5 deletions src/lasso/dimred/hashing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import multiprocessing
import os
import time
from typing import List, Tuple, Union, Sequence
from typing import Union
from collections.abc import Sequence

import h5py
import numpy as np
Expand Down Expand Up @@ -104,8 +105,8 @@ def _compute_mode_similarities(
hashes2: np.ndarray,
eigenvectors_sub1: np.ndarray,
eigenvectors_sub2: np.ndarray,
matches: List[Tuple[int, int]],
) -> List[float]:
matches: list[tuple[int, int]],
) -> list[float]:
"""Compute the mode similarity between different meshes

Parameters
Expand Down Expand Up @@ -217,7 +218,7 @@ def _join_hash_comparison_thread_files(

def run_hash_comparison(
comparison_filepath: str,
hashes_filepaths: List[str],
hashes_filepaths: list[str],
n_threads: int = 1,
print_progress: bool = False,
):
Expand Down Expand Up @@ -597,7 +598,7 @@ def curve_normalizer(x: np.ndarray, y: np.ndarray):

def compute_hashes(
eig_vecs: np.ndarray, result_field: np.ndarray, n_points: int = 100, bandwidth: float = 0.05
) -> List[Tuple[np.ndarray, np.ndarray]]:
) -> list[tuple[np.ndarray, np.ndarray]]:
"""Compute hashes for a result field

Parameters
Expand Down
5 changes: 2 additions & 3 deletions src/lasso/dimred/hashing_sphere.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import typing
import warnings

import h5py
Expand All @@ -15,7 +14,7 @@
warnings.simplefilter(action="ignore", category=FutureWarning)


def _create_sphere_mesh(diameter: np.ndarray) -> typing.Tuple[np.ndarray, np.ndarray]:
def _create_sphere_mesh(diameter: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""Compute the alpha and beta increments for a
meshed sphere for binning the projected values

Expand Down Expand Up @@ -70,7 +69,7 @@ def _create_sphere_mesh(diameter: np.ndarray) -> typing.Tuple[np.ndarray, np.nda

def _project_to_sphere(
points: np.ndarray, centroid: np.ndarray, axis: str = "Z"
) -> typing.Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""compute the projection vectors of centroid to each point in terms of spherical coordinates

Parameters
Expand Down
9 changes: 5 additions & 4 deletions src/lasso/dimred/svd/clustering_betas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Sequence, Tuple, Union
from typing import Union
from collections.abc import Sequence

import numpy as np
from sklearn.cluster import DBSCAN, OPTICS, KMeans, SpectralClustering
Expand Down Expand Up @@ -418,7 +419,7 @@ def document_algorithm(keyword):
}


def create_cluster_arg_dict(args: Sequence[str]) -> Union[Tuple[str, dict], str]:
def create_cluster_arg_dict(args: Sequence[str]) -> Union[tuple[str, dict], str]:
"""Determines which cluster to use and creates a python dictionary to use as cluster_params

Parameters
Expand Down Expand Up @@ -493,7 +494,7 @@ def create_cluster_arg_dict(args: Sequence[str]) -> Union[Tuple[str, dict], str]
return cluster_type, cluster_arg_dict


def create_detector_arg_dict(args: Sequence[str]) -> Union[Tuple[str, dict], str]:
def create_detector_arg_dict(args: Sequence[str]) -> Union[tuple[str, dict], str]:
"""Determines which detector to use and creates a python dictionary to use as detector_params

Parameters
Expand Down Expand Up @@ -579,7 +580,7 @@ def group_betas(
detector=None,
cluster_params=None,
detector_params=None,
) -> Union[Tuple[list, list], str]:
) -> Union[tuple[list, list], str]:
"""
Base function to to group betas into groups, detect outliers. Provides that all different
clustering and outlier detection algorithms are implemented in an easy to access environment.
Expand Down
7 changes: 2 additions & 5 deletions src/lasso/dimred/svd/keyword_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import typing


class ClusterType:
"""Specifies names of specific clustering algorithms

Expand All @@ -22,7 +19,7 @@ class ClusterType:
SpectralClustering = "SpectralClustering"

@staticmethod
def get_cluster_type_name() -> typing.List[str]:
def get_cluster_type_name() -> list[str]:
"""Get the name of the clustering algorithms"""
return [
ClusterType.OPTICS,
Expand Down Expand Up @@ -51,7 +48,7 @@ class DetectorType:
# Experimental = "Experimental"

@staticmethod
def get_detector_type_name() -> typing.List[str]:
def get_detector_type_name() -> list[str]:
"""Get the name of the detector algorithms"""
return [
DetectorType.IsolationForest,
Expand Down
19 changes: 11 additions & 8 deletions src/lasso/dimred/svd/plot_beta_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import re
import time
import webbrowser
from typing import Sequence, Union
from typing import Union
from collections.abc import Sequence

import numpy as np

Expand Down Expand Up @@ -118,11 +119,13 @@ def plot_clusters_js(
id_nr.append(id_group)

# pylint: disable = consider-using-f-string
_three_min_ = '<script type="text/javascript">%s</script>' % _read_file(
os.path.join(
# move path to "~/lasso/"
os.path.split(os.path.split(os.path.dirname(__file__))[0])[0],
"plotting/resources/three_latest.min.js",
_three_min_ = '<script type="text/javascript">{}</script>'.format(
_read_file(
os.path.join(
# move path to "~/lasso/"
os.path.split(os.path.split(os.path.dirname(__file__))[0])[0],
"plotting/resources/three_latest.min.js",
)
)
)

Expand All @@ -136,10 +139,10 @@ def plot_clusters_js(
name = "outliers"
color = "black"
else:
name = "cluster {i}".format(i=index)
name = f"cluster {index}"
color = colorlist[(index - 1) % 10]
formatted_trace = TRACE_STRING.format(
_traceNr_="trace{i}".format(i=index),
_traceNr_=f"trace{index}",
_name_=name,
_color_=color,
_runIDs_=id_cluster[index].tolist(),
Expand Down
4 changes: 2 additions & 2 deletions src/lasso/dimred/svd/pod_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Union
from typing import Union

import numpy as np
from scipy.sparse import csc_matrix
Expand Down Expand Up @@ -37,7 +37,7 @@ def calculate_v_and_betas(
stacked_sub_displ: np.ndarray,
progress_bar: Union[None, Progress, PlaceHolderBar] = None,
task_id: Union[None, TaskID] = None,
) -> Union[str, Tuple[np.ndarray, np.ndarray]]:
) -> Union[str, tuple[np.ndarray, np.ndarray]]:
"""
Calculates the right reduced order Basis V and up to 10 eigenvalues of the subsamples

Expand Down
11 changes: 6 additions & 5 deletions src/lasso/dimred/svd/subsampling_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import random
import time
from typing import List, Sequence, Tuple, Union
from typing import Union
from collections.abc import Sequence

import numpy as np
from sklearn.neighbors import NearestNeighbors
Expand Down Expand Up @@ -45,7 +46,7 @@ def _mark_dead_eles(node_indexes: np.ndarray, alive_shells: np.ndarray) -> np.nd

def _extract_shell_parts(
part_list: Sequence[int], d3plot: D3plot
) -> Union[Tuple[np.ndarray, np.ndarray], str]:
) -> Union[tuple[np.ndarray, np.ndarray], str]:
"""
Extracts a shell part defined by its part ID out of the given d3plot.
Returns a new node index, relevant coordinates and displacement
Expand Down Expand Up @@ -123,7 +124,7 @@ def _extract_shell_parts(
return err_msg.format(part)

def mask_parts(
part_list2: List[int], element_part_index: np.ndarray, element_node_index: np.ndarray
part_list2: list[int], element_part_index: np.ndarray, element_node_index: np.ndarray
) -> np.ndarray:
element_part_filter = np.full(element_part_index.shape, False)
proc_parts = []
Expand Down Expand Up @@ -180,7 +181,7 @@ def mask_parts(

def create_reference_subsample(
load_path: str, parts: Sequence[int], nr_samples=2000
) -> Union[Tuple[np.ndarray, float, float], str]:
) -> Union[tuple[np.ndarray, float, float], str]:
"""
Loads the D3plot at load_path, extracts the node coordinates of part 13, returns
a random subsample of these nodes
Expand Down Expand Up @@ -238,7 +239,7 @@ def create_reference_subsample(

def remap_random_subsample(
load_path: str, parts: list, reference_subsample: np.ndarray
) -> Union[Tuple[np.ndarray, float, float], str]:
) -> Union[tuple[np.ndarray, float, float], str]:
"""
Remaps the specified sample onto a new mesh provided by reference subsampl, using knn matching

Expand Down
5 changes: 1 addition & 4 deletions src/lasso/dyna/array_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import typing


class ArrayType:
"""Specifies the names for specific arrays

Expand Down Expand Up @@ -494,7 +491,7 @@ class ArrayType:
rigid_wall_position = "rigid_wall_position"

@staticmethod
def get_state_array_names() -> typing.List[str]:
def get_state_array_names() -> list[str]:
"""Get the names of all state arrays

Returns:
Expand Down
6 changes: 3 additions & 3 deletions src/lasso/dyna/binout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import glob
from typing import List, Union
from typing import Union

import h5py
import numpy as np
Expand Down Expand Up @@ -67,13 +67,13 @@ def __init__(self, filepath: str):

# check file existence
if not self.filelist:
raise IOError("No file was found.")
raise OSError("No file was found.")

# open lsda buffer
self.lsda = Lsda(self.filelist, "r")
self.lsda_root = self.lsda.root

def read(self, *path) -> Union[List[str], str, np.ndarray]:
def read(self, *path) -> Union[list[str], str, np.ndarray]:
"""Read all data from Binout (top to low level)

Parameters
Expand Down
Loading