Skip to content
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

Bump python min version to 3.9 #13

Merged
merged 3 commits into from
Apr 24, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: [3.7, 3.8, 3.9]
python-version: ['3.9', '3.10', '3.11']

steps:
- name: Clone repo
Expand Down
22 changes: 13 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py39-plus]

- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 24.4.0
hooks:
- id: black

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.9.0
hooks:
- id: mypy
args: [--strict, --ignore-missing-imports, --show-error-codes]
additional_dependencies: [types-termcolor]
exclude: (docs|tests)/

- repo: https://github.com/python-poetry/poetry
rev: 1.2.2
rev: 1.8.0
hooks:
- id: poetry-check
# Commented out due to lock files being generated by different versions of poetry
# Refer python-poetry/poetry#4978. Uncomment once poetry 1.2 is released.
# - id: poetry-lock
# args: [--no-update]
- id: poetry-lock
args: [--no-update]
12 changes: 6 additions & 6 deletions changedet/algos/catalog.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from collections import UserDict
from typing import TYPE_CHECKING, Callable, List, Type
from typing import TYPE_CHECKING, Callable

from changedet.algos.base import MetaAlgo

# Refer https://github.com/python/typing/issues/60
if TYPE_CHECKING:
_Base = UserDict[str, Type[MetaAlgo]]
_Base = UserDict[str, type[MetaAlgo]] # pragma: no cover
else:
_Base = UserDict

Expand All @@ -30,16 +30,16 @@ class AlgoCatalog_(_Base):

"""

def register(self, name: str) -> Callable[[Type[MetaAlgo]], Type[MetaAlgo]]:
def inner_wrapper(wrapped_class: Type[MetaAlgo]) -> Type[MetaAlgo]:
def register(self, name: str) -> Callable[[type[MetaAlgo]], type[MetaAlgo]]:
def inner_wrapper(wrapped_class: type[MetaAlgo]) -> type[MetaAlgo]:
if name in self.keys():
raise AssertionError(f"Algorithm {name} already exists.")
self[name] = wrapped_class
return wrapped_class

return inner_wrapper

def get(self, name: str) -> Type[MetaAlgo]: # type: ignore[override]
def get(self, name: str) -> type[MetaAlgo]: # type: ignore[override]
try:
f = self[name]
except KeyError as e:
Expand All @@ -52,7 +52,7 @@ def get(self, name: str) -> Type[MetaAlgo]: # type: ignore[override]
f = None
return f

def list(self) -> List[str]:
def list(self) -> list[str]:
"""List all registered algorithms.

Returns:
Expand Down
4 changes: 2 additions & 2 deletions changedet/algos/cva.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Tuple
from typing import Any

import numpy as np

Expand All @@ -8,7 +8,7 @@

def calc_cvs(
im1: np.ndarray, im2: np.ndarray, distance: str = "euclidean"
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
diffmap = im2 - im1
if distance == "manhattan":
# Manhattan distance/L1 norm
Expand Down
6 changes: 3 additions & 3 deletions changedet/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Any, Tuple
from typing import Any

import numpy as np
import rasterio as rio
Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self, algo: str):
self.logger = init_logger("changedet")

# Image loading and sanity checks should be done here
def read(self, im1: str, im2: str, band: int) -> Tuple[np.ndarray, np.ndarray]:
def read(self, im1: str, im2: str, band: int) -> tuple[np.ndarray, np.ndarray]:
"""Read and prepare images

Args:
Expand Down Expand Up @@ -75,7 +75,7 @@ def read(self, im1: str, im2: str, band: int) -> Tuple[np.ndarray, np.ndarray]:

return arr1, arr2

def _read(self, im: str, band: int) -> Tuple[np.ndarray, CRS, Profile]:
def _read(self, im: str, band: int) -> tuple[np.ndarray, CRS, Profile]:
with rio.open(im) as raster:
profile = raster.profile
crs = raster.crs
Expand Down
20 changes: 10 additions & 10 deletions changedet/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import sys
from pathlib import Path
from typing import List, Optional, Tuple
from typing import Optional

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -136,7 +136,7 @@ def prepare(self, im1: np.ndarray, im2: np.ndarray, plot: bool = True) -> np.nda
return icm

@staticmethod
def roots(mean: np.ndarray, var: np.ndarray, pi: np.ndarray) -> Tuple[float, float]:
def roots(mean: np.ndarray, var: np.ndarray, pi: np.ndarray) -> tuple[float, float]:
"""Compute the threshold between the no-change and change distributions
from the mean, variance and mixture weight (pi) of no change, change and
ambigous distributions.
Expand Down Expand Up @@ -183,7 +183,7 @@ def __init__(

def init_cluster_params(
self, X: np.ndarray
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Initialse cluster parameters

Shape notation:
Expand Down Expand Up @@ -308,7 +308,7 @@ def e_step(
cov: np.ndarray,
pi: np.ndarray,
sample_inds: ArrayLike,
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""Expectation step

Shape notation:
Expand Down Expand Up @@ -344,7 +344,7 @@ def e_step(

def m_step(
self, X: np.ndarray, resp: np.ndarray
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Maximisation step

Shape notation:
Expand Down Expand Up @@ -455,7 +455,7 @@ def update(self, X: np.ndarray, weights: Optional[np.ndarray] = None) -> None:

def np_weight_stats(
x: np.ndarray, ws: Optional[np.ndarray] = None
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""Calculate weighted mean and sample covariance.

Args:
Expand All @@ -481,7 +481,7 @@ def contrast_stretch(
*,
target_type: str = "uint8",
stretch_type: str = "minmax",
percentile: Tuple[int, int] = (2, 98),
percentile: tuple[int, int] = (2, 98),
) -> np.ndarray:
"""Change image distribution to cover full range of target_type.

Expand Down Expand Up @@ -518,7 +518,7 @@ def contrast_stretch(

def histogram_equalisation(
im: np.ndarray, nbr_bins: int = 256
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
# Refer http://www.janeriksolem.net/histogram-equalization-with-python-and.html
# get image histogram
imhist, bins = np.histogram(im.flatten(), nbr_bins)
Expand Down Expand Up @@ -560,7 +560,7 @@ def format(self, record: logging.LogRecord) -> str:
# Python3 compatibility
self._style._fmt = fmt
self._fmt = fmt
return super(_ColorFormatter, self).format(record)
return super().format(record)


def init_logger(name: str = "logger", output: Optional[str] = None) -> logging.Logger:
Expand Down Expand Up @@ -597,7 +597,7 @@ def init_logger(name: str = "logger", output: Optional[str] = None) -> logging.L
return logger


def histplot(xlist: ArrayLike, xlabel: List[str], bins: Optional[int] = 50) -> Figure:
def histplot(xlist: ArrayLike, xlabel: list[str], bins: Optional[int] = 50) -> Figure:
"""Plot multiple histograms in the same figure

Args:
Expand Down
Loading
Loading