Skip to content

Commit

Permalink
bug(compartments): avoid lowercase comprtmnt str's (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
d33bs authored Jun 20, 2024
1 parent 9829206 commit 5aa2c28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
12 changes: 5 additions & 7 deletions pycytominer/cyto_utils/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import pandas as pd
from typing import Union, List

blocklist_file = os.path.join(
os.path.dirname(__file__), "..", "data", "blocklist_features.txt"
Expand Down Expand Up @@ -185,7 +186,9 @@ def drop_outlier_features(
return outlier_features


def convert_compartment_format_to_list(compartments):
def convert_compartment_format_to_list(
compartments: Union[List[str], str],
) -> List[str]:
"""Converts compartment to a list.
Parameters
Expand All @@ -199,9 +202,4 @@ def convert_compartment_format_to_list(compartments):
List of Cell Painting compartments.
"""

if isinstance(compartments, list):
compartments = [x.lower() for x in compartments]
elif isinstance(compartments, str):
compartments = [compartments.lower()]

return compartments
return compartments if isinstance(compartments, list) else [compartments]
11 changes: 8 additions & 3 deletions tests/test_cyto_utils/test_features_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@


def test_convert_compartment_format_to_list():
compartments = convert_compartment_format_to_list(["cells", "CYTOplasm", "nuclei"])
assert compartments == ["cells", "cytoplasm", "nuclei"]
compartments = convert_compartment_format_to_list([
"cells",
"CYTOplasm",
"nuclei",
"MyoD",
])
assert compartments == ["cells", "CYTOplasm", "nuclei", "MyoD"]

compartments = convert_compartment_format_to_list("FoO")
assert compartments == ["foo"]
assert compartments == ["FoO"]
14 changes: 9 additions & 5 deletions tests/test_cyto_utils/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,35 @@ def test_check_compartments():


def test_check_compartments_not_valid():
warn_expected_string = "Non-canonical compartment detected: something"
warnings.simplefilter("always")
with warnings.catch_warnings(record=True) as w:
not_valid = ["SOMETHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert warn_expected_string in str(w[-1].message)
assert "Non-canonical compartment detected: SOMETHING" in str(w[-1].message)

with warnings.catch_warnings(record=True) as w:
not_valid = "SOMETHING" # Also works with strings
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert warn_expected_string in str(w[-1].message)
assert "Non-canonical compartment detected: SOMETHING" in str(w[-1].message)

with warnings.catch_warnings(record=True) as w:
not_valid = ["CelLs", "CytopLasM", "SOMETHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert warn_expected_string in str(w[-1].message)
assert "Non-canonical compartment detected: CelLs, CytopLasM, SOMETHING" in str(
w[-1].message
)

with warnings.catch_warnings(record=True) as w:
not_valid = ["CelLs", "CytopLasM", "SOMETHING", "NOTHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert f"{warn_expected_string}, nothing" in str(w[-1].message)
assert (
"Non-canonical compartment detected: CelLs, CytopLasM, SOMETHING, NOTHING"
in str(w[-1].message)
)


def test_get_default_compartments():
Expand Down

0 comments on commit 5aa2c28

Please sign in to comment.