Skip to content

Commit

Permalink
Merge branch 'develop' into ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Nov 12, 2024
2 parents 1575ddb + 118fab5 commit a0f2c3d
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 200 deletions.
3 changes: 1 addition & 2 deletions doc/examples/example_cli_famos_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import List, Tuple

import pandas as pd
from more_itertools import one
Expand Down Expand Up @@ -43,7 +42,7 @@ def calibrate(

def parse_summary_to_progress_list(
summary_tsv: str,
) -> List[Tuple[Method, set]]:
) -> list[tuple[Method, set]]:
"""Get progress information from the summary file."""
df_raw = pd.read_csv(summary_tsv, sep="\t")
df = df_raw.loc[~pd.isnull(df_raw["predecessor change"])]
Expand Down
63 changes: 31 additions & 32 deletions petab_select/candidate_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import csv
import logging
import warnings
from collections.abc import Callable
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Type, Union
from typing import Any

import numpy as np
from more_itertools import one
Expand Down Expand Up @@ -96,11 +97,11 @@ def __init__(
method: Method,
criterion: Criterion,
# TODO add MODEL_TYPE = Union[str, Model], str for VIRTUAL_INITIAL_MODEL
predecessor_model: Optional[Model] = None,
excluded_hashes: Optional[list[ModelHash]] = None,
predecessor_model: Model | None = None,
excluded_hashes: list[ModelHash] | None = None,
limit: TYPE_LIMIT = np.inf,
summary_tsv: TYPE_PATH = None,
previous_predecessor_model: Optional[Model] = None,
previous_predecessor_model: Model | None = None,
calibrated_models: dict[ModelHash, Model] = None,
):
"""See class attributes for arguments."""
Expand Down Expand Up @@ -132,7 +133,7 @@ def __init__(
self.latest_iteration_calibrated_models = {}

def set_iteration_user_calibrated_models(
self, user_calibrated_models: Optional[dict[str, Model]]
self, user_calibrated_models: dict[str, Model] | None
) -> None:
"""Hide previously-calibrated models from the calibration tool.
Expand Down Expand Up @@ -216,7 +217,7 @@ def get_iteration_calibrated_models(
)
return combined_calibrated_models

def write_summary_tsv(self, row: List[Any]):
def write_summary_tsv(self, row: list[Any]):
"""Write the summary of the last iteration to a TSV file.
The destination is defined in ``self.summary_tsv``.
Expand Down Expand Up @@ -258,8 +259,8 @@ def _setup_summary_tsv(self):
@classmethod
def read_arguments_from_yaml_dict(
cls,
yaml_dict: Dict[str, str],
) -> Dict[str, Union[str, Model]]:
yaml_dict: dict[str, str],
) -> dict[str, str | Model]:
"""Parse settings that were stored in YAML.
Args:
Expand Down Expand Up @@ -304,7 +305,7 @@ def is_plausible(self, model: Model) -> bool:
"""
return True

def distance(self, model: Model) -> Union[None, float, int]:
def distance(self, model: Model) -> None | float | int:
"""Compute the distance between two models that are neighbors.
Args:
Expand All @@ -320,7 +321,7 @@ def distance(self, model: Model) -> Union[None, float, int]:
def accept(
self,
model: Model,
distance: Union[None, float, int],
distance: None | float | int,
) -> None:
"""Add a candidate model to the candidate space.
Expand Down Expand Up @@ -349,7 +350,7 @@ def n_accepted(self) -> int:

def excluded(
self,
model_hash: Union[Model, ModelHash],
model_hash: Model | ModelHash,
) -> bool:
"""Check whether a model is excluded.
Expand Down Expand Up @@ -377,7 +378,7 @@ def _consider_method(self, model: Model) -> bool:
"""
return True

def consider(self, model: Union[Model, None]) -> bool:
def consider(self, model: Model | None) -> bool:
"""Add a candidate model, if it should be added.
Args:
Expand Down Expand Up @@ -420,9 +421,7 @@ def reset_accepted(self) -> None:
self.models = []
self.distances = []

def set_predecessor_model(
self, predecessor_model: Union[Model, str, None]
):
def set_predecessor_model(self, predecessor_model: Model | str | None):
"""Set the predecessor model.
See class attributes for arguments.
Expand All @@ -436,13 +435,13 @@ def set_predecessor_model(
f"A virtual initial model was requested for a method ({self.method}) that does not support them."
)

def get_predecessor_model(self) -> Union[str, Model]:
def get_predecessor_model(self) -> str | Model:
"""Get the predecessor model."""
return self.predecessor_model

def set_excluded_hashes(
self,
hashes: Union[Model, ModelHash, list[Union[Model, ModelHash]]],
hashes: Model | ModelHash | list[Model | ModelHash],
extend: bool = False,
) -> None:
"""Set the excluded hashes.
Expand All @@ -453,7 +452,7 @@ def set_excluded_hashes(
extend:
Whether to replace or extend the current excluded hashes.
"""
if isinstance(hashes, (Model, ModelHash)):
if isinstance(hashes, Model | ModelHash):
hashes = [hashes]
excluded_hashes = set()
for potential_hash in hashes:
Expand Down Expand Up @@ -517,9 +516,9 @@ def wrapper():

def reset(
self,
predecessor_model: Optional[Union[Model, str, None]] = None,
predecessor_model: Model | str | None | None = None,
# FIXME change `Any` to some `TYPE_MODEL_HASH` (e.g. union of str/int/float)
excluded_hashes: Optional[list[ModelHash]] = None,
excluded_hashes: list[ModelHash] | None = None,
limit: TYPE_LIMIT = None,
) -> None:
"""Reset the candidate models, optionally reinitialize with a model.
Expand Down Expand Up @@ -549,8 +548,8 @@ def reset(
def distances_in_estimated_parameters(
self,
model: Model,
predecessor_model: Optional[Model] = None,
) -> Dict[str, Union[float, int]]:
predecessor_model: Model | None = None,
) -> dict[str, float | int]:
"""Distance between two models in model space, using different metrics.
All metrics are in terms of estimated parameters.
Expand Down Expand Up @@ -679,7 +678,7 @@ class ForwardCandidateSpace(CandidateSpace):
def __init__(
self,
*args,
predecessor_model: Optional[Union[Model, str]] = None,
predecessor_model: Model | str | None = None,
max_steps: int = None,
**kwargs,
):
Expand Down Expand Up @@ -868,10 +867,10 @@ class FamosCandidateSpace(CandidateSpace):
def __init__(
self,
*args,
predecessor_model: Optional[Union[Model, str, None]] = None,
critical_parameter_sets: List = [],
swap_parameter_sets: List = [],
method_scheme: Dict[tuple, str] = None,
predecessor_model: Model | str | None | None = None,
critical_parameter_sets: list = [],
swap_parameter_sets: list = [],
method_scheme: dict[tuple, str] = None,
n_reattempts: int = 0,
consecutive_laterals: bool = False,
**kwargs,
Expand Down Expand Up @@ -1031,7 +1030,7 @@ def read_arguments_from_yaml_dict(cls, yaml_dict) -> dict:
def update_after_calibration(
self,
*args,
iteration_calibrated_models: Dict[str, Model],
iteration_calibrated_models: dict[str, Model],
**kwargs,
) -> None:
"""See `CandidateSpace.update_after_calibration`."""
Expand Down Expand Up @@ -1068,7 +1067,7 @@ def update_after_calibration(

def update_from_iteration_calibrated_models(
self,
iteration_calibrated_models: Dict[str, Model],
iteration_calibrated_models: dict[str, Model],
) -> bool:
"""Update ``self.best_models`` with the latest ``iteration_calibrated_models``
and determine if there was a new best model. If so, return
Expand Down Expand Up @@ -1122,7 +1121,7 @@ def insert_model_into_best_models(self, model_to_insert: Model) -> None:
)
self.best_models.insert(insert_index, model_to_insert)

def consider(self, model: Union[Model, None]) -> bool:
def consider(self, model: Model | None) -> bool:
"""Re-define ``consider`` of FAMoS to be the ``consider`` method
of the ``inner_candidate_space``. Update all the attributes
changed in the ``consider`` method.
Expand Down Expand Up @@ -1392,7 +1391,7 @@ class LateralCandidateSpace(CandidateSpace):
def __init__(
self,
*args,
predecessor_model: Union[Model, None],
predecessor_model: Model | None,
max_steps: int = None,
**kwargs,
):
Expand Down Expand Up @@ -1472,7 +1471,7 @@ def _consider_method(self, model):
}


def method_to_candidate_space_class(method: Method) -> Type[CandidateSpace]:
def method_to_candidate_space_class(method: Method) -> type[CandidateSpace]:
"""Get a candidate space class, given its method name.
Args:
Expand Down
24 changes: 12 additions & 12 deletions petab_select/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The PEtab Select command-line interface."""

from pathlib import Path
from typing import Any, Dict, List
from typing import Any

import click
import dill
Expand All @@ -17,7 +17,7 @@
from .problem import Problem


def read_state(filename: str) -> Dict[str, Any]:
def read_state(filename: str) -> dict[str, Any]:
with open(filename, "rb") as f:
state = dill.load(f)

Expand All @@ -28,17 +28,17 @@ def read_state(filename: str) -> Dict[str, Any]:


def write_state(
state: Dict[str, Any],
state: dict[str, Any],
filename: str,
) -> Dict[str, Any]:
) -> dict[str, Any]:
with open(filename, "wb") as f:
dill.dump(state, f)


def get_state(
problem: Problem,
candidate_space: CandidateSpace,
) -> Dict[str, Any]:
) -> dict[str, Any]:
state = {
"problem": dill.dumps(problem),
"candidate_space": dill.dumps(candidate_space),
Expand Down Expand Up @@ -164,8 +164,8 @@ def start_iteration(
limit: float = np.inf,
limit_sent: float = np.inf,
relative_paths: bool = False,
excluded_model_files: List[str] = None,
excluded_model_hash_files: List[str] = None,
excluded_model_files: list[str] = None,
excluded_model_hash_files: list[str] = None,
) -> None:
"""Search for candidate models in the model space.
Expand Down Expand Up @@ -319,7 +319,7 @@ def end_iteration(
state_dill: str,
models_yaml: str,
metadata_yaml: str,
calibrated_models_yamls: List[str] = None,
calibrated_models_yamls: list[str] = None,
relative_paths: bool = False,
) -> None:
"""Finalize a model selection iteration.
Expand Down Expand Up @@ -398,7 +398,7 @@ def end_iteration(
),
)
def model_to_petab(
models_yamls: List[str],
models_yamls: list[str],
output_path: str,
model_id: str = None,
) -> None:
Expand Down Expand Up @@ -454,7 +454,7 @@ def model_to_petab(
help="The directory where the PEtab files will be output. The PEtab files will be stored in a model-specific subdirectory.",
)
def models_to_petab(
models_yamls: List[str],
models_yamls: list[str],
output_path_prefix: str,
) -> None:
"""Create a PEtab problem for each model in a PEtab Select model YAML file.
Expand Down Expand Up @@ -488,7 +488,7 @@ def models_to_petab(
result_string = "\n".join(
[
"\t".join([model.model_id, result[PETAB_YAML]])
for model, result in zip(models, results)
for model, result in zip(models, results, strict=False)
]
)
print(result_string)
Expand Down Expand Up @@ -542,7 +542,7 @@ def models_to_petab(
)
def get_best(
problem_yaml: str,
models_yamls: List[str],
models_yamls: list[str],
output: str,
state_filename: str = None,
criterion: str = None,
Expand Down
12 changes: 6 additions & 6 deletions petab_select/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from enum import Enum
from pathlib import Path
from typing import Dict, List, Literal, Union
from typing import Literal

# Zero-indexed column/row indices
MODEL_ID_COLUMN = 0
Expand All @@ -21,7 +21,7 @@
PETAB_ESTIMATE_TRUE = 1

# TYPING_PATH = Union[str, Path]
TYPE_PATH = Union[str, Path]
TYPE_PATH = str | Path

# Model space file columns
# TODO ensure none of these occur twice in the column header (this would
Expand Down Expand Up @@ -100,12 +100,12 @@

# Parameters can be fixed to a value, or estimated if indicated with the string
# `ESTIMATE`.
TYPE_PARAMETER = Union[float, int, Literal[ESTIMATE]]
TYPE_PARAMETER_OPTIONS = List[TYPE_PARAMETER]
TYPE_PARAMETER = float | int | Literal[ESTIMATE]
TYPE_PARAMETER_OPTIONS = list[TYPE_PARAMETER]
# Parameter ID -> parameter value mapping.
TYPE_PARAMETER_DICT = Dict[str, TYPE_PARAMETER]
TYPE_PARAMETER_DICT = dict[str, TYPE_PARAMETER]
# Parameter ID -> multiple possible parameter values.
TYPE_PARAMETER_OPTIONS_DICT = Dict[str, TYPE_PARAMETER_OPTIONS]
TYPE_PARAMETER_OPTIONS_DICT = dict[str, TYPE_PARAMETER_OPTIONS]

TYPE_CRITERION = float

Expand Down
4 changes: 2 additions & 2 deletions petab_select/handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Callable, Union
from collections.abc import Callable

# `float` for `np.inf`
TYPE_LIMIT = Union[float, int]
TYPE_LIMIT = float | int


# TODO exclusions handler
Expand Down
Loading

0 comments on commit a0f2c3d

Please sign in to comment.