Skip to content

Commit

Permalink
feat: #55 drop py39
Browse files Browse the repository at this point in the history
  • Loading branch information
cmp0xff committed Jun 16, 2024
1 parent 4f4378e commit 4760632
Show file tree
Hide file tree
Showing 8 changed files with 890 additions and 927 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
tests:
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
build:
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
12 changes: 6 additions & 6 deletions hamilflow/models/brownian_motion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import cached_property
from typing import Dict, List, Optional, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -54,11 +53,11 @@ class BrownianMotionIC(BaseModel):
the dimension of the model too.
"""

x0: Union[float, int, List[Union[float, int]]] = 1.0
x0: float | int | list[float | int] = Field(default=1.0)

@field_validator("x0")
@classmethod
def check_x0_types(cls, v: Union[float, int, list]) -> np.ndarray:
def check_x0_types(cls, v: float | int | list[float]) -> np.ndarray:
if not isinstance(v, (float, int, list)):
raise ValueError(f"Value of x0 should be int/float/list of int/float: {v=}")

Expand Down Expand Up @@ -142,9 +141,10 @@ class BrownianMotion:

def __init__(
self,
system: Dict[str, float],
initial_condition: Optional[Dict[str, float]] = {},
system: dict[str, float],
initial_condition: dict[str, float] | None = None,
):
initial_condition = initial_condition or {}
self.system = BrownianMotionSystem.model_validate(system)
self.initial_condition = BrownianMotionIC.model_validate(initial_condition)

Expand All @@ -154,7 +154,7 @@ def dim(self) -> int:
return self.initial_condition.x0.size

@property
def _axis_names(self) -> List[str]:
def _axis_names(self) -> list[str]:
return [f"y_{i}" for i in range(self.dim)]

def _trajectory(self, n_new_steps: int, seed: int) -> np.ndarray:
Expand Down
33 changes: 16 additions & 17 deletions hamilflow/models/harmonic_oscillator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from functools import cached_property
from typing import Dict, Literal, Optional, Union
from typing import Literal

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -77,14 +77,15 @@ class HarmonicOscillatorBase(ABC):

def __init__(
self,
system: Dict[str, float],
initial_condition: Optional[Dict[str, float]] = {},
):
system: dict[str, float],
initial_condition: dict[str, float] | None = None,
) -> None:
initial_condition = initial_condition or {}
self.system = HarmonicOscillatorSystem.model_validate(system)
self.initial_condition = HarmonicOscillatorIC.model_validate(initial_condition)

@cached_property
def definition(self) -> Dict[str, float]:
def definition(self) -> dict[str, float]:
"""model params and initial conditions defined as a dictionary."""
return {
"system": self.system.model_dump(),
Expand Down Expand Up @@ -152,9 +153,9 @@ class SimpleHarmonicOscillator(HarmonicOscillatorBase):

def __init__(
self,
system: Dict[str, float],
initial_condition: Optional[Dict[str, float]] = {},
):
system: dict[str, float],
initial_condition: dict[str, float] | None = None,
) -> None:
super().__init__(system, initial_condition)
if self.system.type != "simple":
raise ValueError(
Expand Down Expand Up @@ -224,17 +225,17 @@ class DampedHarmonicOscillator(HarmonicOscillatorBase):

def __init__(
self,
system: Dict[str, float],
initial_condition: Optional[Dict[str, float]] = {},
):
system: dict[str, float],
initial_condition: dict[str, float] | None = None,
) -> None:
super().__init__(system, initial_condition)
if self.system.type == "simple":
raise ValueError(
f"System is not a Damped Harmonic Oscillator: {self.system}\n"
f"This is a simple harmonic oscillator, use `SimpleHarmonicOscillator`."
)

def _x_under_damped(self, t: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
def _x_under_damped(self, t: float | np.ndarray) -> float | np.ndarray:
r"""Solution to under damped harmonic oscillators:
$$
Expand All @@ -259,9 +260,7 @@ def _x_under_damped(self, t: Union[float, np.ndarray]) -> Union[float, np.ndarra
* np.sin(omega_damp * t)
) * np.exp(-self.system.zeta * self.system.omega * t)

def _x_critical_damped(
self, t: Union[float, np.ndarray]
) -> Union[float, np.ndarray]:
def _x_critical_damped(self, t: float | np.ndarray) -> float | np.ndarray:
r"""Solution to critical damped harmonic oscillators:
$$
Expand All @@ -279,7 +278,7 @@ def _x_critical_damped(
-self.system.zeta * self.system.omega * t
)

def _x_over_damped(self, t: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
def _x_over_damped(self, t: float | np.ndarray) -> float | np.ndarray:
r"""Solution to over harmonic oscillators:
$$
Expand All @@ -305,7 +304,7 @@ def _x_over_damped(self, t: Union[float, np.ndarray]) -> Union[float, np.ndarray
* np.sinh(gamma_damp * t)
) * np.exp(-self.system.zeta * self.system.omega * t)

def _x(self, t: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
def _x(self, t: float | np.ndarray) -> float | np.ndarray:
r"""Solution to damped harmonic oscillators."""
if self.system.type == "under_damped":
x = self._x_under_damped(t)
Expand Down
7 changes: 3 additions & 4 deletions hamilflow/models/pendulum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import math
from functools import cached_property
from typing import Dict, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -55,8 +54,8 @@ class Pendulum:

def __init__(
self,
system: Union[int, float, Dict[str, Union[int, float]]],
initial_condition: Union[int, float, Dict[str, Union[int, float]]],
system: int | float | dict[str, int | float],
initial_condition: int | float | dict[str, int | float],
) -> None:
if isinstance(system, (float, int)):
system = {"omega0": system}
Expand All @@ -66,7 +65,7 @@ def __init__(
self.initial_condition = PendulumIC.model_validate(initial_condition)

@cached_property
def definition(self) -> Dict[str, float]:
def definition(self) -> dict[str, float]:
"""Model params and initial conditions defined as a dictionary."""
return dict(
system=self.system.model_dump(),
Expand Down
Loading

0 comments on commit 4760632

Please sign in to comment.