From fee6821ecbbea0c447c0d07e499084bb51469f62 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Thu, 13 Oct 2022 21:42:05 +0200 Subject: [PATCH 1/8] Added parameter type hints --- pytometry/plotting/_histogram.py | 11 ++++---- pytometry/plotting/_scatter_density.py | 27 +++++++++---------- pytometry/preprocessing/_process_data.py | 12 ++++----- pytometry/tools/_normalization.py | 33 ++++++++++++------------ 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/pytometry/plotting/_histogram.py b/pytometry/plotting/_histogram.py index 700eccc..3423c81 100644 --- a/pytometry/plotting/_histogram.py +++ b/pytometry/plotting/_histogram.py @@ -3,6 +3,7 @@ from anndata import AnnData from matplotlib import pyplot as plt from matplotlib import rcParams +from typing import Tuple from ..preprocessing._process_data import find_indexes from ..tools._normalization import normalize_arcsinh, normalize_biExp, normalize_logicle @@ -11,12 +12,12 @@ # Plot data. Choose between Area, Height both(default) def plotdata( adata: AnnData, - key="signal_type", + key: str = "signal_type", normalize: str = None, - cofactor=10, - figsize=(15, 6), - option="area", - save="", + cofactor: float = 10, + figsize: Tuple[float, float] = (15, 6), + option: str = "area", + save: str = "", **kwargs, ): """Creating histogram plot from Anndata object. diff --git a/pytometry/plotting/_scatter_density.py b/pytometry/plotting/_scatter_density.py index 2214109..0912d6e 100644 --- a/pytometry/plotting/_scatter_density.py +++ b/pytometry/plotting/_scatter_density.py @@ -1,26 +1,27 @@ import datashader as ds +import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pd import scanpy as sc from anndata import AnnData from datashader.mpl_ext import dsshow - +from typing import List, Optional, Tuple, Union def scatter_density( adata: AnnData, - x="FSC-A", - y="SSC-A", - x_label="FSC-A", - y_label="SSC-A", - x_scale="linear", - y_scale="linear", - x_lim=[-2 * 1e4, 3 * 1e5], - y_lim=[-2 * 1e4, 3 * 1e5], - ax=None, - cmap="jet", - vmin=None, - vmax=None, + x: str = "FSC-A", + y: str = "SSC-A", + x_label: str = "FSC-A", + y_label: str = "SSC-A", + x_scale: str = "linear", + y_scale: str = "linear", + x_lim: List[float] = [-2 * 1e4, 3 * 1e5], + y_lim: List[float] = [-2 * 1e4, 3 * 1e5], + ax: Optional[mpl.Axes] = None, + cmap: Union[str, List, mpl.cm.Colormap] = "jet", + vmin: Optional[float] = None, + vmax: Optional[float] = None, ): """Plots the cell density across two adata.obs. diff --git a/pytometry/preprocessing/_process_data.py b/pytometry/preprocessing/_process_data.py index 0ffce56..756a85c 100644 --- a/pytometry/preprocessing/_process_data.py +++ b/pytometry/preprocessing/_process_data.py @@ -33,8 +33,8 @@ def create_comp_mat(spillmat: pd.DataFrame, relevant_data: str = "") -> pd.DataF def find_indexes( adata: AnnData, var_key: str = None, - key_added="signal_type", - data_type="facs", + key_added: str ="signal_type", + data_type: str ="facs", inplace: bool = True, ) -> Optional[AnnData]: """Find channels of interest for computing compensation. @@ -186,10 +186,10 @@ def compensate( def split_signal( adata: AnnData, - var_key=None, - key="signal_type", - option="area", - data_type="facs", + var_key: Optional[str] = None, + key: str = "signal_type", + option: str = "area", + data_type: str = "facs", inplace: bool = True, ) -> Optional[AnnData]: """Method to filter out height or area data. diff --git a/pytometry/tools/_normalization.py b/pytometry/tools/_normalization.py index 799fd4c..d2bea5c 100644 --- a/pytometry/tools/_normalization.py +++ b/pytometry/tools/_normalization.py @@ -1,9 +1,8 @@ -# from typing import Optional - import numpy as np +import pandas as pd from anndata import AnnData from scipy import interpolate - +from typing import Union def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): """Inverse hyperbolic sine transformation. @@ -27,11 +26,11 @@ def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): def normalize_logicle( - adata, - t=262144, - m=4.5, - w=0.5, - a=0, + adata: AnnData, + t: float = 262144, + m: float = 4.5, + w: float = 0.5, + a: float = 0, inplace: bool = True, ): """Logicle transformation. @@ -126,7 +125,7 @@ def normalize_logicle( return None if inplace else adata -def _scale(value, p) -> float: +def _scale(value: float, p: dict) -> float: """Scale helper function. Args: @@ -194,7 +193,7 @@ def _scale(value, p) -> float: return -1 -def _solve(b, w) -> float: +def _solve(b: float, w: float) -> float: """Helper function for biexponential transformation. Args: @@ -269,7 +268,7 @@ def _solve(b, w) -> float: return -1 -def _seriesBiexponential(p, value) -> float: +def _seriesBiexponential(p: dict, value: float) -> float: """Helper function to compute biex trafo. Args: @@ -290,11 +289,11 @@ def _seriesBiexponential(p, value) -> float: def normalize_biExp( - adata, - negative=0.0, - width=-10.0, - positive=4.418540, - max_value=262144.000029, + adata: AnnData, + negative: Union[float, pd.Series] = 0.0, + width: Union[float, pd.Series] = -10.0, + positive: Union[float, pd.Series] = 4.418540, + max_value: Union[float, pd.Series] = 262144.000029, inplace: bool = True, ): """Biexponential transformation. @@ -491,7 +490,7 @@ def _generate_biex_lut( return positive, values -def _log_root(b, w) -> float: +def _log_root(b: float, w: float) -> float: """Helper function. Args: From 55ce8f46e6dbad47b9098da6731c3f321c9c27b8 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 10:16:11 +0200 Subject: [PATCH 2/8] Removed unused import --- pytometry/plotting/_scatter_density.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytometry/plotting/_scatter_density.py b/pytometry/plotting/_scatter_density.py index 0912d6e..b310c9f 100644 --- a/pytometry/plotting/_scatter_density.py +++ b/pytometry/plotting/_scatter_density.py @@ -6,7 +6,7 @@ import scanpy as sc from anndata import AnnData from datashader.mpl_ext import dsshow -from typing import List, Optional, Tuple, Union +from typing import List, Optional, Union def scatter_density( adata: AnnData, From cfcf67d5d85172da20324de09b0f805fb7453a2e Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:45:31 +0200 Subject: [PATCH 3/8] Fixed annotations Reverted (for now) annotations in two helper functions which made mypy trip up. Additionally, changed two assignments (ll. 216 and 504) from "=0" to "=0.0", so that mypy understands we are dealing with floats here. Finally, explicitly annotated variable dx (l. 507) as float, so that mypy doesn't complain when we assign floats to it later (e.g. l. 525). --- pytometry/tools/_normalization.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pytometry/tools/_normalization.py b/pytometry/tools/_normalization.py index d2bea5c..ed6c623 100644 --- a/pytometry/tools/_normalization.py +++ b/pytometry/tools/_normalization.py @@ -26,12 +26,12 @@ def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): def normalize_logicle( - adata: AnnData, - t: float = 262144, - m: float = 4.5, - w: float = 0.5, - a: float = 0, - inplace: bool = True, + adata, + t = 262144, + m = 4.5, + w = 0.5, + a = 0, + inplace = True ): """Logicle transformation. @@ -213,7 +213,7 @@ def _solve(b: float, w: float) -> float: # based on RTSAFE from Numerical Recipes 1st Edition # bracket the root - d_lo = 0 + d_lo = 0.0 d_hi = b # bisection first step @@ -289,12 +289,12 @@ def _seriesBiexponential(p: dict, value: float) -> float: def normalize_biExp( - adata: AnnData, - negative: Union[float, pd.Series] = 0.0, - width: Union[float, pd.Series] = -10.0, - positive: Union[float, pd.Series] = 4.418540, - max_value: Union[float, pd.Series] = 262144.000029, - inplace: bool = True, + adata, + negative = 0.0, + width = -10.0, + positive = 4.418540, + max_value = 262144.000029, + inplace = True ): """Biexponential transformation. @@ -501,10 +501,10 @@ def _log_root(b: float, w: float) -> float: float: Solution to interpolation """ # Code adopted from FlowKit Python package - x_lo = 0 + x_lo = 0.0 x_hi = b d = (x_lo + x_hi) / 2 - dx = abs(int(x_lo - x_hi)) + dx = abs(int(x_lo - x_hi)) # type: float dx_last = dx fb = -2 * np.log(b) + w * b f = 2.0 * np.log(d) + w * b + fb From 27eb0e2e281bcbebd21bbc367615b7fc223fc629 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:49:22 +0200 Subject: [PATCH 4/8] Removed unused imports after reverting type annotations in two functions --- pytometry/tools/_normalization.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pytometry/tools/_normalization.py b/pytometry/tools/_normalization.py index ed6c623..c8e0992 100644 --- a/pytometry/tools/_normalization.py +++ b/pytometry/tools/_normalization.py @@ -1,8 +1,6 @@ import numpy as np -import pandas as pd from anndata import AnnData from scipy import interpolate -from typing import Union def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): """Inverse hyperbolic sine transformation. From b55da0faf18165fb3a9b149bd218a93db452a308 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:58:12 +0200 Subject: [PATCH 5/8] Import order --- pytometry/plotting/_histogram.py | 6 ++++-- pytometry/plotting/_scatter_density.py | 4 +++- pytometry/preprocessing/__init__.py | 9 ++------- pytometry/tools/__init__.py | 3 ++- pytometry/tools/_normalization.py | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pytometry/plotting/_histogram.py b/pytometry/plotting/_histogram.py index 3423c81..e53ca7b 100644 --- a/pytometry/plotting/_histogram.py +++ b/pytometry/plotting/_histogram.py @@ -1,12 +1,14 @@ +from typing import Tuple + import numpy as np import seaborn as sns from anndata import AnnData from matplotlib import pyplot as plt from matplotlib import rcParams -from typing import Tuple from ..preprocessing._process_data import find_indexes -from ..tools._normalization import normalize_arcsinh, normalize_biExp, normalize_logicle +from ..tools._normalization import (normalize_arcsinh, normalize_biExp, + normalize_logicle) # Plot data. Choose between Area, Height both(default) diff --git a/pytometry/plotting/_scatter_density.py b/pytometry/plotting/_scatter_density.py index b310c9f..7b7aa6e 100644 --- a/pytometry/plotting/_scatter_density.py +++ b/pytometry/plotting/_scatter_density.py @@ -1,3 +1,5 @@ +from typing import List, Optional, Union + import datashader as ds import matplotlib as mpl import matplotlib.pyplot as plt @@ -6,7 +8,7 @@ import scanpy as sc from anndata import AnnData from datashader.mpl_ext import dsshow -from typing import List, Optional, Union + def scatter_density( adata: AnnData, diff --git a/pytometry/preprocessing/__init__.py b/pytometry/preprocessing/__init__.py index c1e8167..4f40f20 100644 --- a/pytometry/preprocessing/__init__.py +++ b/pytometry/preprocessing/__init__.py @@ -1,7 +1,2 @@ -from ._process_data import ( - _dummy_spillover, - compensate, - create_comp_mat, - find_indexes, - split_signal, -) +from ._process_data import (_dummy_spillover, compensate, create_comp_mat, + find_indexes, split_signal) diff --git a/pytometry/tools/__init__.py b/pytometry/tools/__init__.py index 6777556..8a551ec 100644 --- a/pytometry/tools/__init__.py +++ b/pytometry/tools/__init__.py @@ -1 +1,2 @@ -from ._normalization import normalize_arcsinh, normalize_biExp, normalize_logicle +from ._normalization import (normalize_arcsinh, normalize_biExp, + normalize_logicle) diff --git a/pytometry/tools/_normalization.py b/pytometry/tools/_normalization.py index c8e0992..04ab619 100644 --- a/pytometry/tools/_normalization.py +++ b/pytometry/tools/_normalization.py @@ -2,6 +2,7 @@ from anndata import AnnData from scipy import interpolate + def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): """Inverse hyperbolic sine transformation. From 4e4685d19e74eaad9cd86abfbfe2e11512d650e1 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 13:23:37 +0200 Subject: [PATCH 6/8] Used pre-commit locally to ensure proper coding style --- pytometry/plotting/_histogram.py | 3 +-- pytometry/preprocessing/__init__.py | 9 +++++++-- pytometry/preprocessing/_process_data.py | 4 ++-- pytometry/tools/__init__.py | 3 +-- pytometry/tools/_normalization.py | 19 ++++++------------- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pytometry/plotting/_histogram.py b/pytometry/plotting/_histogram.py index e53ca7b..08ba643 100644 --- a/pytometry/plotting/_histogram.py +++ b/pytometry/plotting/_histogram.py @@ -7,8 +7,7 @@ from matplotlib import rcParams from ..preprocessing._process_data import find_indexes -from ..tools._normalization import (normalize_arcsinh, normalize_biExp, - normalize_logicle) +from ..tools._normalization import normalize_arcsinh, normalize_biExp, normalize_logicle # Plot data. Choose between Area, Height both(default) diff --git a/pytometry/preprocessing/__init__.py b/pytometry/preprocessing/__init__.py index 4f40f20..c1e8167 100644 --- a/pytometry/preprocessing/__init__.py +++ b/pytometry/preprocessing/__init__.py @@ -1,2 +1,7 @@ -from ._process_data import (_dummy_spillover, compensate, create_comp_mat, - find_indexes, split_signal) +from ._process_data import ( + _dummy_spillover, + compensate, + create_comp_mat, + find_indexes, + split_signal, +) diff --git a/pytometry/preprocessing/_process_data.py b/pytometry/preprocessing/_process_data.py index 756a85c..7db590c 100644 --- a/pytometry/preprocessing/_process_data.py +++ b/pytometry/preprocessing/_process_data.py @@ -33,8 +33,8 @@ def create_comp_mat(spillmat: pd.DataFrame, relevant_data: str = "") -> pd.DataF def find_indexes( adata: AnnData, var_key: str = None, - key_added: str ="signal_type", - data_type: str ="facs", + key_added: str = "signal_type", + data_type: str = "facs", inplace: bool = True, ) -> Optional[AnnData]: """Find channels of interest for computing compensation. diff --git a/pytometry/tools/__init__.py b/pytometry/tools/__init__.py index 8a551ec..6777556 100644 --- a/pytometry/tools/__init__.py +++ b/pytometry/tools/__init__.py @@ -1,2 +1 @@ -from ._normalization import (normalize_arcsinh, normalize_biExp, - normalize_logicle) +from ._normalization import normalize_arcsinh, normalize_biExp, normalize_logicle diff --git a/pytometry/tools/_normalization.py b/pytometry/tools/_normalization.py index 04ab619..77a005e 100644 --- a/pytometry/tools/_normalization.py +++ b/pytometry/tools/_normalization.py @@ -24,14 +24,7 @@ def normalize_arcsinh(adata: AnnData, cofactor: float, inplace: bool = True): return None if inplace else adata -def normalize_logicle( - adata, - t = 262144, - m = 4.5, - w = 0.5, - a = 0, - inplace = True -): +def normalize_logicle(adata, t=262144, m=4.5, w=0.5, a=0, inplace=True): """Logicle transformation. Args: @@ -289,11 +282,11 @@ def _seriesBiexponential(p: dict, value: float) -> float: def normalize_biExp( adata, - negative = 0.0, - width = -10.0, - positive = 4.418540, - max_value = 262144.000029, - inplace = True + negative=0.0, + width=-10.0, + positive=4.418540, + max_value=262144.000029, + inplace=True, ): """Biexponential transformation. From 3f8970304dec38eb9281fcb7b5839c45188a8746 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 13:45:36 +0200 Subject: [PATCH 7/8] Added import for annotation of ax, as suggested --- pytometry/plotting/_scatter_density.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pytometry/plotting/_scatter_density.py b/pytometry/plotting/_scatter_density.py index 7b7aa6e..4c1a6f6 100644 --- a/pytometry/plotting/_scatter_density.py +++ b/pytometry/plotting/_scatter_density.py @@ -8,6 +8,7 @@ import scanpy as sc from anndata import AnnData from datashader.mpl_ext import dsshow +from matplotlib.axes import Axes def scatter_density( @@ -20,7 +21,7 @@ def scatter_density( y_scale: str = "linear", x_lim: List[float] = [-2 * 1e4, 3 * 1e5], y_lim: List[float] = [-2 * 1e4, 3 * 1e5], - ax: Optional[mpl.Axes] = None, + ax: Optional[Axes] = None, cmap: Union[str, List, mpl.cm.Colormap] = "jet", vmin: Optional[float] = None, vmax: Optional[float] = None, From 9c1ec6af9954572e674b5d4b1150f617f69f6ca1 Mon Sep 17 00:00:00 2001 From: Ma-Fi-94 <33027321+Ma-Fi-94@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:00:56 +0200 Subject: [PATCH 8/8] Add files via upload --- pytometry/plotting/_scatter_density.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytometry/plotting/_scatter_density.py b/pytometry/plotting/_scatter_density.py index 4c1a6f6..299959b 100644 --- a/pytometry/plotting/_scatter_density.py +++ b/pytometry/plotting/_scatter_density.py @@ -1,7 +1,6 @@ from typing import List, Optional, Union import datashader as ds -import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pd @@ -9,6 +8,7 @@ from anndata import AnnData from datashader.mpl_ext import dsshow from matplotlib.axes import Axes +from matplotlib.colors import Colormap def scatter_density( @@ -22,7 +22,7 @@ def scatter_density( x_lim: List[float] = [-2 * 1e4, 3 * 1e5], y_lim: List[float] = [-2 * 1e4, 3 * 1e5], ax: Optional[Axes] = None, - cmap: Union[str, List, mpl.cm.Colormap] = "jet", + cmap: Union[str, List, Colormap] = "jet", vmin: Optional[float] = None, vmax: Optional[float] = None, ):