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

refactor: apply bugbears linting checks #388

Draft
wants to merge 1 commit into
base: ci/update-ruff-to-0.3.4
Choose a base branch
from
Draft
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 pycytominer/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def aggregate(
population_df,
strata=["Metadata_Plate", "Metadata_Well"],
strata=("Metadata_Plate", "Metadata_Well"),
features="infer",
operation="median",
output_file=None,
Expand Down
6 changes: 4 additions & 2 deletions pycytominer/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def annotate(
profiles,
platemap,
join_on=["Metadata_well_position", "Metadata_Well"],
join_on=("Metadata_well_position", "Metadata_Well"),
output_file=None,
output_type="csv",
add_metadata_id_to_platemap=True,
Expand All @@ -28,7 +28,7 @@ def annotate(
external_join_right=None,
compression_options=None,
float_format=None,
cmap_args={},
cmap_args=None,
**kwargs,
):
"""Add metadata to aggregated profiles.
Expand Down Expand Up @@ -76,6 +76,8 @@ def annotate(
data.
"""

cmap_args = cmap_args if cmap_args is not None else {}

# Load Data
profiles = load_profiles(profiles)
platemap = load_platemap(platemap, add_metadata_id_to_platemap)
Expand Down
8 changes: 6 additions & 2 deletions pycytominer/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

def consensus(
profiles,
replicate_columns=["Metadata_Plate", "Metadata_Well"],
replicate_columns=("Metadata_Plate", "Metadata_Well"),
operation="median",
features="infer",
output_file=None,
output_type="csv",
compression_options=None,
float_format=None,
modz_args={"method": "spearman"},
modz_args=None,
):
"""Form level 5 consensus profile data.

Expand Down Expand Up @@ -95,6 +95,10 @@ def consensus(
output_file=None,
)
"""
# Set default modz_args
if modz_args is None:
modz_args = {"method": "spearman"}

# Confirm that the operation is supported
check_consensus_operation(operation)

Expand Down
6 changes: 4 additions & 2 deletions pycytominer/cyto_utils/DeepProfiler_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ def aggregate_deep(self):
# if no files were found there is a miss-match between the index and the output files
if not len(arr):
warnings.warn(
f"No files for the key {metadata_level} could be found.\nThis program will continue, but be aware that this might induce errors!"
f"No files for the key {metadata_level} could be found.\nThis program will continue, but be aware that this might induce errors!",
stacklevel=1,
)
continue
df = pd.concat(arr)
Expand Down Expand Up @@ -384,7 +385,8 @@ def get_single_cells(
# skip a file if there are no features
if len(features.index) == 0:
warnings.warn(
f"No features could be found at {features_path}.\nThis program will continue, but be aware that this might induce errors!"
f"No features could be found at {features_path}.\nThis program will continue, but be aware that this might induce errors!",
stacklevel=1,
)
continue
locations = load_npz_locations(
Expand Down
2 changes: 1 addition & 1 deletion pycytominer/cyto_utils/cell_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
image_column: str = "ImageNumber",
object_column: str = "ObjectNumber",
table_column: str = "TableNumber",
image_key: list = ["Metadata_Plate", "Metadata_Well", "Metadata_Site"],
image_key: list = ("Metadata_Plate", "Metadata_Well", "Metadata_Site"),
cell_x_loc: str = "Nuclei_Location_Center_X",
cell_y_loc: str = "Nuclei_Location_Center_Y",
):
Expand Down
10 changes: 5 additions & 5 deletions pycytominer/cyto_utils/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ class SingleCells:
def __init__(
self,
sql_file,
strata=["Metadata_Plate", "Metadata_Well"],
strata=("Metadata_Plate", "Metadata_Well"),
aggregation_operation="median",
output_file=None,
compartments=default_compartments,
compartment_linking_cols=default_linking_cols,
merge_cols=["TableNumber", "ImageNumber"],
image_cols=["TableNumber", "ImageNumber", "Metadata_Site"],
merge_cols=("TableNumber", "ImageNumber"),
image_cols=("TableNumber", "ImageNumber", "Metadata_Site"),
add_image_features=False,
image_feature_categories=None,
features="infer",
Expand Down Expand Up @@ -241,8 +241,8 @@ def set_subsample_n(self, subsample_n):

try:
self.subsample_n = int(subsample_n)
except ValueError:
raise ValueError("subsample n must be an integer or coercable")
except ValueError as e:
raise ValueError("subsample n must be an integer or coercable") from e
self._check_subsampling()

def set_subsample_random_state(self, random_state):
Expand Down
6 changes: 3 additions & 3 deletions pycytominer/cyto_utils/collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def collate(
tmp_dir="/tmp",
overwrite=False,
add_image_features=True,
image_feature_categories=["Granularity", "Texture", "ImageQuality", "Threshold"],
image_feature_categories=("Granularity", "Texture", "ImageQuality", "Threshold"),
printtoscreen=True,
):
"""Collate the CellProfiler-created CSVs into a single SQLite file by calling cytominer-database
Expand Down Expand Up @@ -74,12 +74,12 @@ def collate(
try:
import cytominer_database.ingest
import cytominer_database.munge
except ImportError:
except ImportError as e:
raise ImportError(
"""Optional dependency cytominer-database is not installed.
Please install the `collate` optional dependency group: e.g. `pip install pycytominer[collate]`
"""
)
) from e

# Set up directories (these need to be abspaths to keep from confusing makedirs later)
input_dir = pathlib.Path(f"{base_directory}/analysis/{batch}/{plate}/{csv_dir}")
Expand Down
2 changes: 1 addition & 1 deletion pycytominer/cyto_utils/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def label_compartment(cp_features, compartment, metadata_cols):

def infer_cp_features(
population_df,
compartments=["Cells", "Nuclei", "Cytoplasm"],
compartments=("Cells", "Nuclei", "Cytoplasm"),
metadata=False,
image_features=False,
):
Expand Down
4 changes: 2 additions & 2 deletions pycytominer/cyto_utils/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def load_platemap(platemap, add_metadata_id=True):
try:
delim = infer_delim(platemap)
platemap = pd.read_csv(platemap, sep=delim)
except FileNotFoundError:
raise FileNotFoundError(f"{platemap} platemap file not found")
except FileNotFoundError as e:
raise FileNotFoundError(f"{platemap} platemap file not found") from e
else:
# Setting platemap to a copy to prevent column name changes from back-propagating
platemap = platemap.copy()
Expand Down
7 changes: 6 additions & 1 deletion pycytominer/cyto_utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def output(
output_type: str = "csv",
sep: str = ",",
float_format: Optional[str] = None,
compression_options: Union[str, Dict] = {"method": "gzip", "mtime": 1},
compression_options: Union[str, Dict, None] = None,
**kwargs,
):
"""Given an output file and compression options, write file to disk
Expand Down Expand Up @@ -79,6 +79,11 @@ def output(
)
"""

# Set default compression options (done outside of function signature to avoid mutable default arguments)
compression_options = (
compression_options if compression_options else {"method": "gzip", "mtime": 1}
)

if output_type == "csv":
compression_options = set_compression_method(compression=compression_options)

Expand Down
24 changes: 10 additions & 14 deletions pycytominer/cyto_utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def check_compartments(compartments):
warn_str = "Non-canonical compartment detected: {x}".format(
x=", ".join(non_canonical_compartments)
)
warnings.warn(warn_str)
warnings.warn(warn_str, stacklevel=1)


def load_known_metadata_dictionary(metadata_file=default_metadata_file):
Expand Down Expand Up @@ -186,10 +186,10 @@ def check_fields_of_view_format(fields_of_view):
else:
try:
return list(map(int, fields_of_view))
except ValueError:
except ValueError as e:
raise TypeError(
"Variables of type int expected, however some of the input fields of view are not integers."
)
) from e
else:
raise TypeError(
f"Variable of type list expected, however type {type(fields_of_view)} was passed."
Expand All @@ -215,11 +215,9 @@ def check_fields_of_view(data_fields_of_view, input_fields_of_view):

"""

try:
assert len(
list(np.intersect1d(data_fields_of_view, input_fields_of_view))
) == len(input_fields_of_view)
except AssertionError:
if not len(list(np.intersect1d(data_fields_of_view, input_fields_of_view))) == len(
input_fields_of_view
):
raise ValueError(
"Some of the input fields of view are not present in the image table."
)
Expand Down Expand Up @@ -248,12 +246,10 @@ def check_image_features(image_features, image_columns):
else:
level = 0

try:
assert all(
feature in list({img_col.split("_")[level] for img_col in image_columns})
for feature in image_features
)
except AssertionError:
if not all(
feature in list({img_col.split("_")[level] for img_col in image_columns})
for feature in image_features
):
raise ValueError(
"Some of the input image features are not present in the image table."
)
Expand Down
2 changes: 1 addition & 1 deletion pycytominer/cyto_utils/write_gct.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ def write_gct(
gctwriter = csv.writer(gctfile, delimiter="\t")
gctwriter.writerow([version])
gctwriter.writerow(data_dimensions)
for feature, row in full_df.iterrows():
for _, row in full_df.iterrows():
gctwriter.writerow(row)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ select = [
# flake8-bandit
# "S",
# flake8-bugbear
# "B",
"B",
# isort
# "I",
# mccabe
Expand Down
7 changes: 5 additions & 2 deletions tests/test_cyto_utils/test_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

def build_random_data(
compartment="cells",
ImageNumber=sorted(["x", "y"] * 50),
TableNumber=sorted(["x_hash", "y_hash"] * 50),
ImageNumber=None,
TableNumber=None,
):
# Set mutable default values outside of function signature
ImageNumber = ImageNumber if ImageNumber else sorted(["x", "y"] * 50)
TableNumber = TableNumber if TableNumber else sorted(["x_hash", "y_hash"] * 50)
a_feature = random.sample(range(1, 1000), 100)
b_feature = random.sample(range(1, 1000), 100)
c_feature = random.sample(range(1, 1000), 100)
Expand Down
Loading