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

Python 3.10+ & use uv for docs & fix RTD & support numpy 2 #830

Merged
merged 7 commits into from
Nov 28, 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
5 changes: 4 additions & 1 deletion .github/workflows/run_notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
"docs/tutorials/notebooks/ehrapy_introduction.ipynb",
"docs/tutorials/notebooks/mimic_2_introduction.ipynb",
"docs/tutorials/notebooks/mimic_2_survival_analysis.ipynb",
"docs/tutorials/notebooks/mimic_2_fate.ipynb",
# "docs/tutorials/notebooks/mimic_2_fate.ipynb", # https://github.com/theislab/cellrank/issues/1235
"docs/tutorials/notebooks/mimic_2_causal_inference.ipynb",
# "docs/tutorials/notebooks/mimic_3_demo.ipynb",
# "docs/tutorials/notebooks/medcat.ipynb",
Expand All @@ -34,5 +34,8 @@ jobs:
- name: Install ehrapy and additional dependencies
run: uv pip install --system . cellrank nbconvert ipykernel

- name: Install scvelo from Github
run: uv pip install --system git+https://github.com/theislab/scvelo.git

- name: Run ${{ matrix.notebook }} Notebook
run: jupyter nbconvert --to notebook --execute ${{ matrix.notebook }}
17 changes: 7 additions & 10 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ build:
os: ubuntu-22.04
tools:
python: "3.11"
jobs:
pre_build:
- python -c "import ehrapy"
- pip freeze
post_create_environment:
- pip install uv
post_install:
# VIRTUAL_ENV needs to be set manually for now.
# See https://github.com/readthedocs/readthedocs.org/pull/11152/
- VIRTUAL_ENV=$READTHEDOCS_VIRTUALENV_PATH pip install .[docs]
commands:
- asdf plugin add uv
- asdf install uv latest
- asdf global uv latest
- uv venv
- uv pip install .[docs]
- .venv/bin/python -m sphinx -T -b html -d docs/_build/doctrees -D language=en docs $READTHEDOCS_OUTPUT/html
sphinx:
configuration: docs/conf.py
fail_on_warning: false
Expand Down
2 changes: 1 addition & 1 deletion docs/_ext/edit_on_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_github_repo(app: Sphinx, path: str) -> str:


def _html_page_context(
app: Sphinx, _pagename: str, templatename: str, context: dict[str, Any], doctree: Optional[Any]
app: Sphinx, _pagename: str, templatename: str, context: dict[str, Any], doctree: Any | None
) -> None:
# doctree is None - otherwise viewcode fails
if templatename != "page.html" or doctree is None:
Expand Down
4 changes: 2 additions & 2 deletions ehrapy/_utils_doc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import inspect
from collections.abc import Callable
from textwrap import dedent
from typing import Callable, Optional, Union


def getdoc(c_or_f: Union[Callable, type]) -> Optional[str]: # pragma: no cover
def getdoc(c_or_f: Callable | type) -> str | None: # pragma: no cover
if getattr(c_or_f, "__doc__", None) is None:
return None
doc = inspect.getdoc(c_or_f)
Expand Down
4 changes: 2 additions & 2 deletions ehrapy/anndata/anndata_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _detect_binary_columns(df: pd.DataFrame, numerical_columns: list[str]) -> li
for column in numerical_columns:
# checking for float and int as well as NaNs (this is safe since checked columns are numericals only)
# only columns that contain at least one 0 and one 1 are counted as binary (or 0.0/1.0)
if df[column].isin([0.0, 1.0, np.NaN, 0, 1]).all() and df[column].nunique() == 2:
if df[column].isin([0.0, 1.0, np.nan, 0, 1]).all() and df[column].nunique() == 2:
binary_columns.append(column)

return binary_columns
Expand All @@ -423,7 +423,7 @@ def _cast_obs_columns(obs: pd.DataFrame) -> pd.DataFrame:
# type cast each non-numerical column to either bool (if possible) or category else
obs[object_columns] = obs[object_columns].apply(
lambda obs_name: obs_name.astype("category")
if not set(pd.unique(obs_name)).issubset({False, True, np.NaN})
if not set(pd.unique(obs_name)).issubset({False, True, np.nan})
else obs_name.astype("bool"),
axis=0,
)
Expand Down
2 changes: 1 addition & 1 deletion ehrapy/data/_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ def synthea_1k_sample(

df = anndata_to_df(adata)
df.drop(
columns=[col for col in df.columns if any(isinstance(x, (list, dict)) for x in df[col].dropna())], inplace=True
columns=[col for col in df.columns if any(isinstance(x, list | dict) for x in df[col].dropna())], inplace=True
)
df.drop(columns=df.columns[df.isna().all()], inplace=True)
adata = df_to_anndata(df, index_column="id")
Expand Down
10 changes: 5 additions & 5 deletions ehrapy/plot/_scanpy_pl_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from collections.abc import Collection, Iterable, Mapping, Sequence
from collections.abc import Callable, Collection, Iterable, Mapping, Sequence
from enum import Enum
from functools import partial
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Callable, Literal, Union
from typing import TYPE_CHECKING, Any, Literal

import scanpy as sc
from scanpy.plotting import DotPlot, MatrixPlot, StackedViolin
Expand Down Expand Up @@ -36,12 +36,12 @@
from scanpy.plotting._utils import _AxesSubplot

_Basis = Literal["pca", "tsne", "umap", "diffmap", "draw_graph_fr"]
_VarNames = Union[str, Sequence[str]]
ColorLike = Union[str, tuple[float, ...]]
_VarNames = str | Sequence[str]
ColorLike = str | tuple[float, ...]
_IGraphLayout = Literal["fa", "fr", "rt", "rt_circular", "drl", "eq_tree", ...] # type: ignore
_FontWeight = Literal["light", "normal", "medium", "semibold", "bold", "heavy", "black"]
_FontSize = Literal["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"]
VBound = Union[str, float, Callable[[Sequence[float]], float]]
VBound = str | float | Callable[[Sequence[float]], float]


@_doc_params(scatter_temp=doc_scatter_basic, show_save_ax=doc_show_save_ax)
Expand Down
4 changes: 2 additions & 2 deletions ehrapy/preprocessing/_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def encode(

if isinstance(encodings, str) and not autodetect:
raise ValueError("Passing a string for parameter encodings is only possible when using autodetect=True!")
elif autodetect and not isinstance(encodings, (str, type(None))):
elif autodetect and not isinstance(encodings, str | type(None)):
raise ValueError(
f"Setting encode mode with autodetect=True only works by passing a string (encode mode name) or None not {type(encodings)}!"
)
Expand Down Expand Up @@ -630,7 +630,7 @@ def _update_obs(adata: AnnData, categorical_names: list[str]) -> pd.DataFrame:
updated_obs[var_name] = adata.X[::, idx : idx + 1].flatten()
# note: this will count binary columns (0 and 1 only) as well
# needed for writing to .h5ad files
if set(pd.unique(updated_obs[var_name])).issubset({False, True, np.NaN}):
if set(pd.unique(updated_obs[var_name])).issubset({False, True, np.nan}):
updated_obs[var_name] = updated_obs[var_name].astype("bool")
# get all non bool object columns and cast them to category dtype
object_columns = list(updated_obs.select_dtypes(include="object").columns)
Expand Down
2 changes: 1 addition & 1 deletion ehrapy/preprocessing/_imputation.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def explicit_impute(
_warn_imputation_threshold(adata, var_names=replacement.keys(), threshold=warning_threshold) # type: ignore

# 1: Replace all missing values with the specified value
if isinstance(replacement, (int, str)):
if isinstance(replacement, int | str):
_replace_explicit(adata.X, replacement, impute_empty_strings)

# 2: Replace all missing values in a subset of columns with a specified value per column or a default value, when the column is not explicitly named
Expand Down
4 changes: 2 additions & 2 deletions ehrapy/preprocessing/_normalization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING

import numpy as np
import sklearn.preprocessing as sklearn_pp
Expand All @@ -20,7 +20,7 @@
)

if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Callable, Sequence

import pandas as pd
from anndata import AnnData
Expand Down
7 changes: 4 additions & 3 deletions ehrapy/preprocessing/_scanpy_pp_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from collections.abc import Callable
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Callable, Literal, Union
from typing import TYPE_CHECKING, Any, Literal, TypeAlias

import numpy as np
import scanpy as sc
Expand All @@ -15,7 +16,7 @@

from ehrapy.preprocessing._types import KnownTransformer

AnyRandom = Union[int, np.random.RandomState, None]
AnyRandom: TypeAlias = int | np.random.RandomState | None


def pca(
Expand Down Expand Up @@ -193,7 +194,7 @@ def combat(
"sqeuclidean",
"yule",
]
_Metric = Union[_MetricSparseCapable, _MetricScipySpatial]
_Metric = _MetricSparseCapable | _MetricScipySpatial


def neighbors(
Expand Down
4 changes: 2 additions & 2 deletions ehrapy/tools/_method_options.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Literal, Optional
from typing import Literal

_InitPos = Literal["paga", "spectral", "random"]

_LAYOUTS = ("fr", "drl", "kk", "grid_fr", "lgl", "rt", "rt_circular", "fa")
_Layout = Literal[_LAYOUTS] # type: ignore

_rank_features_groups_method = Optional[Literal["logreg", "t-test", "wilcoxon", "t-test_overestim_var"]]
_rank_features_groups_method = Literal["logreg", "t-test", "wilcoxon", "t-test_overestim_var"] | None
_correction_method = Literal["benjamini-hochberg", "bonferroni"]
_rank_features_groups_cat_method = Literal[
"chi-square", "g-test", "freeman-tukey", "mod-log-likelihood", "neyman", "cressie-read"
Expand Down
Loading
Loading