Skip to content

Commit

Permalink
refactor: apply flake8-simplify checks
Browse files Browse the repository at this point in the history
This commit applies flake8-simplify checks to the codebase. The non-style refactoring included in this commit is primarily condensing nested ifs and using or/and instead.
  • Loading branch information
kenibrewer authored Mar 2, 2024
1 parent db5c4b3 commit 013f239
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 22 deletions.
15 changes: 7 additions & 8 deletions pycytominer/cyto_utils/annotate_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ def annotate_cmap(
"Metadata_mg_per_ml",
] = 0

if perturbation_mode == "genetic":
if "Metadata_pert_name" in annotated.columns:
annotated = annotated.assign(
Metadata_broad_sample_type=[
"control" if x == "EMPTY" else "trt"
for x in annotated.Metadata_pert_name
]
)
if perturbation_mode == "genetic" and "Metadata_pert_name" in annotated.columns:
annotated = annotated.assign(
Metadata_broad_sample_type=[
"control" if x == "EMPTY" else "trt"
for x in annotated.Metadata_pert_name
]
)

if "Metadata_broad_sample_type" in annotated.columns:
annotated = annotated.assign(
Expand Down
12 changes: 5 additions & 7 deletions pycytominer/cyto_utils/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def __init__(

# Check that the subsample_frac is between 0 and 1
assert (
0 < subsample_frac and 1 >= subsample_frac
subsample_frac > 0 and subsample_frac <= 1
), "subsample_frac must be between 0 and 1"

self.sql_file = sql_file
Expand Down Expand Up @@ -795,9 +795,9 @@ def merge_single_cells(
if normalize_args is None:
normalize_args = {}
features = infer_cp_features(sc_df, compartments=self.compartments)
elif "features" not in normalize_args:
features = infer_cp_features(sc_df, compartments=self.compartments)
elif normalize_args["features"] == "infer":
elif ("features" not in normalize_args) or (
normalize_args["features"] == "infer"
):
features = infer_cp_features(sc_df, compartments=self.compartments)
else:
features = normalize_args["features"]
Expand Down Expand Up @@ -860,8 +860,7 @@ def aggregate_profiles(
if output_file is not None:
self.set_output_file(output_file)

compartment_idx = 0
for compartment in self.compartments:
for compartment_idx, compartment in enumerate(self.compartments):
if compartment_idx == 0:
aggregated = self.aggregate_compartment(
compartment=compartment,
Expand All @@ -879,7 +878,6 @@ def aggregate_profiles(
on=self.strata,
how="inner",
)
compartment_idx += 1

self.is_aggregated = True

Expand Down
2 changes: 1 addition & 1 deletion pycytominer/cyto_utils/cp_image_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def aggregate_image_features(
)

# Aggregate other image features
if not len(np.setdiff1d(image_feature_categories, [count_prefix])) == 0:
if len(np.setdiff1d(image_feature_categories, [count_prefix])) != 0:
image_features_df = image_features_df.drop(
remove_cols, axis="columns", errors="ignore"
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ select = [
# flake8-debugger
# "T10",
# flake8-simplify
# "SIM",
"SIM",
# isort
# "I",
# mccabe
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cyto_utils/test_cell_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def test_output_shape_and_required_columns(
)

# verify that the Nuclear_Location_Center_X and Nuclear_Location_Center_Y columns are present
assert "Nuclei_Location_Center_X" in cell_loc["CellCenters"][0][0].keys()
assert "Nuclei_Location_Center_Y" in cell_loc["CellCenters"][0][0].keys()
assert "Nuclei_Location_Center_X" in cell_loc["CellCenters"][0][0]
assert "Nuclei_Location_Center_Y" in cell_loc["CellCenters"][0][0]


@pytest.mark.parametrize("cell_loc", ["cell_loc1", "cell_loc2", "cell_loc3"])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cyto_utils/test_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_load_compartment():
# note: pd.api.types.is_integer sometimes is unable to detect int64
or CELLS_DF[colname].dtype == "int64"
# avoid recasting the metadata_types
and colname not in metadata_types.keys()
and colname not in metadata_types
}

# create deep copy of CELLS_DF with manually re-typed float columns as float32
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cyto_utils/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@
platemap_df.to_csv(output_platemap_file_gzip, sep="\t", index=False, compression="gzip")

# Write npz temp files
key_values = {k: npz_metadata_dict[k] for k in npz_metadata_dict.keys()}
key_values = {k: npz_metadata_dict[k] for k in npz_metadata_dict}
npz_metadata_dict.update(npz_model_key)
key_with_model_values = {k: npz_metadata_dict[k] for k in npz_metadata_dict.keys()}
key_with_model_values = {k: npz_metadata_dict[k] for k in npz_metadata_dict}

np.savez_compressed(output_npz_file, features=npz_feats, metadata=key_values)
np.savez_compressed(
Expand Down

0 comments on commit 013f239

Please sign in to comment.