Skip to content

Commit

Permalink
refactor: apply bandit checks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kenibrewer authored Mar 30, 2024
1 parent e639116 commit 1aa12e2
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 27 deletions.
7 changes: 4 additions & 3 deletions pycytominer/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ def annotate(

if not isinstance(external_metadata, pd.DataFrame):
if external_metadata is not None:
assert os.path.exists(
external_metadata
), f"external metadata at {external_metadata} does not exist"
if not os.path.exists(external_metadata):
raise FileNotFoundError(
f"external metadata at {external_metadata} does not exist"
)

external_metadata = pd.read_csv(external_metadata)
else:
Expand Down
4 changes: 2 additions & 2 deletions pycytominer/cyto_utils/collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def run_check_errors(cmd):
"""Run a system command, and exit if an error occurred, otherwise continue"""
if isinstance(cmd, str):
cmd = cmd.split()
output = subprocess.run(cmd, capture_output=True, text=True)
output = subprocess.run(cmd, capture_output=True, text=True) # noqa: S603
if output.stderr != "":
print_cmd = " ".join(map(str, cmd))
sys.exit(
Expand All @@ -28,7 +28,7 @@ def collate(
csv_dir="analysis",
aws_remote=None,
aggregate_only=False,
tmp_dir="/tmp",
tmp_dir="/tmp", # noqa: S108
overwrite=False,
add_image_features=True,
image_feature_categories=["Granularity", "Texture", "ImageQuality", "Threshold"],
Expand Down
2 changes: 1 addition & 1 deletion pycytominer/cyto_utils/collate_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
parser.add_argument(
"--tmp-dir",
dest="tmp_dir",
default="/tmp",
default="/tmp", # noqa: S108
help="The temporary directory to be used by cytominer-databases for output",
)
parser.add_argument(
Expand Down
10 changes: 6 additions & 4 deletions pycytominer/feature_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ def feature_select(

# Make sure the user provides a supported operation
if isinstance(operation, list):
assert all(
x in all_ops for x in operation
), f"Some operation(s) {operation} not supported. Choose {all_ops}"
if not all(x in all_ops for x in operation):
raise ValueError(
f"Some operation(s) {operation} not supported. Choose {all_ops}"
)
elif isinstance(operation, str):
assert operation in all_ops, f"{operation} not supported. Choose {all_ops}"
if operation not in all_ops:
raise ValueError(f"{operation} not supported. Choose {all_ops}")
operation = operation.split()
else:
return ValueError("Operation must be a list or string")
Expand Down
3 changes: 2 additions & 1 deletion pycytominer/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def normalize(
method = method.lower()

avail_methods = ["standardize", "robustize", "mad_robustize", "spherize"]
assert method in avail_methods, f"operation must be one {avail_methods}"
if method not in avail_methods:
raise ValueError(f"operation must be one {avail_methods}")

if method == "standardize":
scaler = StandardScaler()
Expand Down
3 changes: 2 additions & 1 deletion pycytominer/operations/correlation_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def correlation_threshold(
# Check that the input method is supported
method = check_correlation_method(method)

assert 0 <= threshold <= 1, "threshold variable must be between (0 and 1)"
if not 0 <= threshold <= 1:
raise ValueError("threshold variable must be between (0 and 1)")

# Subset dataframe and calculate correlation matrix across subset features
if samples != "all":
Expand Down
16 changes: 9 additions & 7 deletions pycytominer/operations/noise_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ def noise_removal(

# If a metadata column name is specified, use that as the perturb groups
if isinstance(noise_removal_perturb_groups, str):
assert noise_removal_perturb_groups in population_df.columns, (
'f"{perturb} not found. Are you sure it is a ' "metadata column?"
)
if noise_removal_perturb_groups not in population_df.columns:
raise ValueError(
'f"{perturb} not found. Are you sure it is a ' "metadata column?"
)
group_info = population_df[noise_removal_perturb_groups]

# Otherwise, the user specifies a list of perturbs
elif isinstance(noise_removal_perturb_groups, list):
assert len(noise_removal_perturb_groups) == len(population_df), (
f"The length of input list: {len(noise_removal_perturb_groups)} is not equivalent to your "
f"data: {population_df.shape[0]}"
)
if not len(noise_removal_perturb_groups) == len(population_df):
raise ValueError(
f"The length of input list: {len(noise_removal_perturb_groups)} is not equivalent to your "
f"data: {population_df.shape[0]}"
)
group_info = noise_removal_perturb_groups
else:
raise TypeError(
Expand Down
7 changes: 4 additions & 3 deletions pycytominer/operations/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ def fit(self, X, y=None):
self.W = self.W @ Vt

# number of columns of self.W should be equal to that of X
assert (
self.W.shape[1] == X.shape[1]
), f"Error: W has {self.W.shape[1]} columns, X has {X.shape[1]} columns"
if not (self.W.shape[1] == X.shape[1]):
raise ValueError(
f"Error: W has {self.W.shape[1]} columns, X has {X.shape[1]} columns"
)

if self.W.shape[1] != X.shape[1]:
error_detail = (
Expand Down
6 changes: 4 additions & 2 deletions pycytominer/operations/variance_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def variance_threshold(
"""

assert 0 <= freq_cut <= 1, "freq_cut variable must be between (0 and 1)"
assert 0 <= unique_cut <= 1, "unique_cut variable must be between (0 and 1)"
if not 0 <= freq_cut <= 1:
raise ValueError("freq_cut variable must be between (0 and 1)")
if not 0 <= unique_cut <= 1:
raise ValueError("unique_cut variable must be between (0 and 1)")

# Subset dataframe
if samples != "all":
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ select = [
"UP",
# ruff
"RUF",
# The following checks are disabled, but we are working towards enabling them in the future.
# flake8-bandit
# "S",
"S",
# The following checks are disabled, but we are working towards enabling them in the future.
# flake8-bugbear
# "B",
# isort
Expand All @@ -163,6 +163,7 @@ ignore = [
# Ignore `E402` and `F401` (unusued imports) in all `__init__.py` files
"__init__.py" = ["E402", "F401"]
"tests/*" = ["S101"] # Ignore assert statements in tests
"pycytominer/cyto_utils/*" = ["S101", "S608"] # Ignore assert statements in cyto_utils

[tool.ruff.format]
preview = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
DELETE FROM {table}
WHERE TableNumber NOT IN (SELECT TableNumber FROM Image)
OR ObjectNumber > 1
"""
""" # noqa: S608
)

conn.commit()
Expand Down

0 comments on commit 1aa12e2

Please sign in to comment.