Skip to content

Commit

Permalink
Merge pull request #1593 from lisphilar/issue1592
Browse files Browse the repository at this point in the history
Drop Python 3.8 and add Python 3.12 support
  • Loading branch information
lisphilar authored Feb 4, 2024
2 parents a4adc60 + 4b48a12 commit a6d6a9a
Show file tree
Hide file tree
Showing 17 changed files with 1,736 additions and 1,664 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
python-version: "3.12"

- name: install Poetry
uses: snok/install-poetry@v1
Expand Down
52 changes: 46 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ jobs:
all_but_latest: true
access_token: ${{ github.token }}

poetry-test:
name: quality check and pytest with Poetry
poetry--quality-check:
name: quality check
runs-on: ubuntu-latest
strategy:
matrix:
# Use 3.11 because pyproject-flake8 does not support Python 3.12
# https://github.com/csachs/pyproject-flake8/issues/30
python-version: ["3.11"]

steps:
Expand Down Expand Up @@ -56,8 +58,46 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root --with test

- name: run quality check with "make test" command
run: make test
- name: run quality check with "make check" command
run: make check

poetry-test:
name: Pytest with Poetry
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12"]

steps:
- name: check out repository
uses: actions/checkout@v3

- name: set up Python
id: setup-python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

- name: load cached venv if available
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: install dependencies and test tools if cache does not exist
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root --with test

- name: run tests
run: poetry run pytest tests

- name: upload test coverage to Codecov
uses: codecov/codecov-action@v3
Expand All @@ -67,7 +107,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- name: Checkout
Expand All @@ -91,7 +131,7 @@ jobs:
strategy:
matrix:
os: ["windows-latest"]
python-version: ["3.11"]
python-version: ["3.12"]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion covsirphy/downloading/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DataDownloader(Term):
"""
LAYERS: list[str] = [Term.ISO3, Term.PROVINCE, Term.CITY]

def __init__(self, directory: str or Path = "input", update_interval: int = 12, **kwargs) -> None:
def __init__(self, directory: str | Path = "input", update_interval: int = 12, **kwargs) -> None:
self._directory = directory
self._update_interval = Validator(update_interval, "update_interval").int(value_range=(0, None))
self._gis = GIS(layers=self.LAYERS, country=self.ISO3, date=self.DATE)
Expand Down
16 changes: 13 additions & 3 deletions covsirphy/dynamics/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ def register(self, data: pd.DataFrame | None = None) -> pd.DataFrame:
if data is not None:
new_df = Validator(data, "data").dataframe(time_index=True)
new_df.index = pd.to_datetime(new_df.index).round("D")
all_df = pd.DataFrame(np.nan, index=self._df.index, columns=self._df.columns)
all_df = pd.DataFrame(
np.nan,
index=self._df.index,
columns=self._df.columns,
)
all_df[self._PH] = 0
for col in new_df:
new_df[col] = new_df[col].astype(pd.Float64Dtype())
all_df[col] = all_df[col].astype(pd.Float64Dtype())
all_df.update(new_df, overwrite=True)
if all_df.loc[self._first, self._SIRF].isna().any():
raise EmptyError(
Expand Down Expand Up @@ -333,7 +340,7 @@ def track(self) -> pd.DataFrame:
"""
df = self.summary()
df[self.DATE] = df[[self.START, self.END]].apply(
lambda x: pd.date_range(start=x[0], end=x[1], freq="D"), axis=1)
lambda x: pd.date_range(start=x[self.START], end=x[self.END], freq="D"), axis=1)
return df.explode(self.DATE).set_index(self.DATE).drop([self.START, self.END], axis=1)

def simulate(self, model_specific: bool = False) -> pd.DataFrame:
Expand Down Expand Up @@ -473,13 +480,16 @@ def estimate_params(self, metric: str = "RMSLE", digits: int | None = None, n_jo
config.info(f"\n<{self._model._NAME}: parameter estimation>")
config.info(f"Running optimization with {n_jobs_validated} CPUs...")
stopwatch = StopWatch()
# p-tqdm with Python 3.12: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version.
warnings.filterwarnings("ignore", category=DeprecationWarning)
results = p_umap(est_f, phase_dataframes, num_cpus=n_jobs_validated)
config.info(f"Completed optimization. Total: {stopwatch.stop_show()}\n")
est_df = pd.concat(results, sort=True, axis=0)
est_df = est_df.loc[:, [*self._parameters, metric, self.TRIALS, self.RUNTIME]].ffill().convert_dtypes()
# Update registered parameter values
r_df = self.register()
warnings.filterwarnings("ignore", category=FutureWarning)
for col in self._parameters:
r_df[col] = r_df[col].astype(pd.Float64Dtype())
r_df.update(est_df, overwrite=True)
self.register(data=r_df)
return est_df
Expand Down
2 changes: 1 addition & 1 deletion covsirphy/engineering/_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def resample(self, date_range=None):
end_date, name="the second date of @date_range").date(default=df[self._date].max(), value_range=(start, None))
df = df[df[self._date].between(start, end, inclusive="both")]
grouped = df.set_index(self._date).groupby(self._layers, as_index=False, observed=True)
df = grouped.resample("D").ffill()
df = grouped[list(set(df.columns) - set([self._date]))].resample("D").ffill()
self._df = df.reset_index().drop("level_0", errors="ignore", axis=1)

def fillna(self):
Expand Down
1 change: 1 addition & 0 deletions covsirphy/science/_autots.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class _AutoTSHandler(Term):
"""

def __init__(self, Y, days, seed, **kwargs):
warnings.simplefilter("ignore", SyntaxWarning)
from autots import AutoTS # https://github.com/lisphilar/covid19-sir/issues/1265
self._Y = Validator(Y, "Y").dataframe(time_index=True, empty_ok=False)
self._days = Validator(days, name="days").int(value_range=(1, None))
Expand Down
3 changes: 2 additions & 1 deletion covsirphy/visualization/vbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from inspect import signature
import sys
import matplotlib
from matplotlib.axes import Axes
if not hasattr(sys, "ps1"):
matplotlib.use("Agg")
from matplotlib import pyplot as plt
Expand Down Expand Up @@ -91,7 +92,7 @@ def ax(self):

@ax.setter
def ax(self, ax):
self._ax = Validator(ax, "ax").instance(matplotlib.axes.Axes)
self._ax = Validator(ax, "ax").instance(Axes)

def plot(self):
"""Method for plotting. This will be defined in child classes.
Expand Down
2 changes: 1 addition & 1 deletion example/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def main(geo="Japan", file_prefix="jpn"):
geo (str): location identifier
file_prefix (str): prefix of the filenames
"""
print("This script works with version >= 2.26.0-eta")
print("This script works with version >= 3.0.0")
print(cs.get_version())
code_path = Path(__file__)
output_dir = code_path.with_name("output").joinpath(f"{code_path.stem}_{file_prefix}")
Expand Down
Binary file modified example/output/demo_jpn/01_actual.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/output/demo_jpn/02_segmentation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/output/demo_jpn/03_simulate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/output/demo_jpn/04_predicted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/output/demo_jpn/05_Rt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/output/demo_jpn/06_confirmed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a6d6a9a

Please sign in to comment.