Skip to content

Commit af07d49

Browse files
authored
ci(mypy): add mypy check and adjust code for types (#439)
* ci(mypy): add mypy check and adjust code for types * remove redefinitions config * update stubs and ignores * update output for compression typing * update expanduser types for cell_locations * add ignore for change in types * adjust for typing * update celllocations typing * remove ignores * add missing library ignore settings; fix aggregate * add test condition for aggregate
1 parent 2fd5ca3 commit af07d49

File tree

11 files changed

+257
-25
lines changed

11 files changed

+257
-25
lines changed

.github/workflows/integration-test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ jobs:
5656
uses: pre-commit/[email protected]
5757
with:
5858
extra_args: --all-files
59+
python-type-checks:
60+
# This job is used to check Python types
61+
name: Python type checks
62+
# Avoid fail-fast to retain output
63+
strategy:
64+
fail-fast: false
65+
runs-on: ubuntu-22.04
66+
if: github.event_name != 'schedule'
67+
steps:
68+
- name: Checkout repo
69+
uses: actions/checkout@v4
70+
- name: Setup python, and check pre-commit cache
71+
uses: ./.github/actions/setup-env
72+
with:
73+
python-version: ${{ env.TARGET_PYTHON_VERSION }}
74+
cache-pre-commit: false
75+
cache-venv: true
76+
setup-poetry: true
77+
install-deps: true
78+
- name: Run mypy
79+
run: |
80+
poetry run mypy .
5981
integration-test:
6082
name: Pytest (Python ${{ matrix.python-version }} on ${{ matrix.os }})
6183
# Runs pytest on all tested versions of python and OSes

poetry.lock

Lines changed: 143 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pycytominer/aggregate.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def aggregate(
8383
# Only extract single object column in preparation for count
8484
if compute_object_count:
8585
count_object_df = (
86-
population_df.loc[:, np.union1d(strata, [object_feature])]
86+
population_df.loc[:, list(np.union1d(strata, [object_feature]))]
8787
.groupby(strata)[object_feature]
8888
.count()
8989
.reset_index()
@@ -92,7 +92,9 @@ def aggregate(
9292

9393
if features == "infer":
9494
features = infer_cp_features(population_df)
95-
population_df = population_df[features]
95+
96+
# recast as dataframe to protect against scenarios where a series may be returned
97+
population_df = pd.DataFrame(population_df[features])
9698

9799
# Fix dtype of input features (they should all be floats!)
98100
population_df = population_df.astype(float)
@@ -101,7 +103,9 @@ def aggregate(
101103
population_df = pd.concat([strata_df, population_df], axis="columns")
102104

103105
# Perform aggregating function
104-
population_df = population_df.groupby(strata, dropna=False)
106+
# Note: type ignore added below to address the change in variable types for
107+
# label `population_df`.
108+
population_df = population_df.groupby(strata, dropna=False) # type: ignore[assignment]
105109

106110
if operation == "median":
107111
population_df = population_df.median().reset_index()
@@ -118,10 +122,10 @@ def aggregate(
118122
for column in population_df.columns
119123
if column in ["ImageNumber", "ObjectNumber"]
120124
]:
121-
population_df = population_df.drop([columns_to_drop], axis="columns")
125+
population_df = population_df.drop(columns=columns_to_drop, axis="columns")
122126

123127
if output_file is not None:
124-
output(
128+
return output(
125129
df=population_df,
126130
output_filename=output_file,
127131
output_type=output_type,

pycytominer/cyto_utils/DeepProfiler_processing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import pandas as pd
88
import warnings
99

10-
from pycytominer import aggregate, normalize
11-
from pycytominer.cyto_utils import (
10+
# use mypy ignores below to avoid duplicate import warnings
11+
from pycytominer import aggregate, normalize # type: ignore[no-redef]
12+
from pycytominer.cyto_utils import ( # type: ignore[no-redef]
1213
load_npz_features,
1314
load_npz_locations,
1415
infer_cp_features,

pycytominer/cyto_utils/cell_locations.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def __init__(
106106
"s3", config=botocore.config.Config(signature_version=botocore.UNSIGNED)
107107
)
108108

109-
def _expanduser(self, obj: Union[str, None]):
109+
def _expanduser(
110+
self, obj: Union[str, pd.DataFrame, sqlalchemy.engine.Engine, None]
111+
):
110112
"""Expand the user home directory in a path"""
111113
if obj is not None and isinstance(obj, str) and not obj.startswith("s3://"):
112114
return pathlib.Path(obj).expanduser().as_posix()

pycytominer/cyto_utils/cells.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def merge_single_cells(
714714
"""
715715

716716
# Load the single cell dataframe by merging on the specific linking columns
717-
sc_df = ""
717+
left_compartment_loaded = False
718718
linking_check_cols = []
719719
merge_suffix_rename = []
720720
for left_compartment in self.compartment_linking_cols:
@@ -737,7 +737,7 @@ def merge_single_cells(
737737
left_compartment
738738
]
739739

740-
if isinstance(sc_df, str):
740+
if not left_compartment_loaded:
741741
sc_df = self.load_compartment(compartment=left_compartment)
742742

743743
if compute_subsample:
@@ -752,6 +752,8 @@ def merge_single_cells(
752752
sc_df, how="left", on=subset_logic_df.columns.tolist()
753753
).reindex(sc_df.columns, axis="columns")
754754

755+
left_compartment_loaded = True
756+
755757
sc_df = sc_df.merge(
756758
self.load_compartment(compartment=right_compartment),
757759
left_on=[*self.merge_cols, left_link_col],
@@ -804,11 +806,13 @@ def merge_single_cells(
804806

805807
normalize_args["features"] = features
806808

807-
sc_df = normalize(profiles=sc_df, **normalize_args)
809+
# ignore mypy warnings below as these reference root package imports
810+
sc_df = normalize(profiles=sc_df, **normalize_args) # type: ignore[operator]
808811

809812
# In case platemap metadata is provided, use pycytominer.annotate for metadata
810813
if platemap is not None:
811-
sc_df = annotate(
814+
# ignore mypy warnings below as these reference root package imports
815+
sc_df = annotate( # type: ignore[operator]
812816
profiles=sc_df, platemap=platemap, output_file=None, **kwargs
813817
)
814818

pycytominer/cyto_utils/collate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def collate(
131131
with sqlite3.connect(cache_backend_file, isolation_level=None) as connection:
132132
cursor = connection.cursor()
133133
if column:
134-
if print:
134+
if printtoscreen:
135135
print(f"Adding a Metadata_Plate column based on column {column}")
136136
cursor.execute("ALTER TABLE Image ADD COLUMN Metadata_Plate TEXT;")
137137
cursor.execute(f"UPDATE image SET Metadata_Plate ={column};")

0 commit comments

Comments
 (0)