Skip to content

Commit

Permalink
Merge pull request #83 from yutanagano/move-to-logging
Browse files Browse the repository at this point in the history
Migrate from warnings to logging
  • Loading branch information
yutanagano authored Jan 11, 2025
2 parents 2970e50 + cda5cbe commit a37479f
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 127 deletions.
8 changes: 5 additions & 3 deletions src/tidytcells/_query_engine/hla_query_engine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import logging
from typing import FrozenSet
import warnings

from tidytcells._query_engine import QueryEngine
from tidytcells._resources import VALID_HOMOSAPIENS_MH


logger = logging.getLogger(__name__)


class HlaQueryEngine(QueryEngine):
@classmethod
def query(cls, precision: str, functionality: str) -> FrozenSet[str]:
if precision == "allele":
warnings.warn(
logger.warning(
"tidytcells is not fully aware of all HLA alleles, and the "
"highest resolution it can provide is up to the level of the "
"protein (two allele designations)."
Expand Down
8 changes: 5 additions & 3 deletions src/tidytcells/_query_engine/mus_musculus_mh_query_engine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import logging
from typing import FrozenSet
import warnings

from tidytcells._query_engine import QueryEngine
from tidytcells._resources import VALID_MUSMUSCULUS_MH


logger = logging.getLogger(__name__)


class MusMusculusMhQueryEngine(QueryEngine):
@classmethod
def query(cls, precision: str, functionality: str) -> FrozenSet[str]:
if precision == "allele":
warnings.warn(
logger.warning(
"tidytcells is not aware of Mus musculus MH alleles at all, "
"and can only provide up to the level of the gene."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_reason_why_invalid(self, enforce_functional: bool = False) -> Optional[s
return None

if not self._gene_name in VALID_HOMOSAPIENS_MH:
return "unrecognised gene name"
return "unrecognized gene name"

# Verify allele designators up to the level of the protein (or G/P)
allele_designation = self._allele_designation.copy()
Expand All @@ -149,7 +149,7 @@ def get_reason_why_invalid(self, enforce_functional: bool = False) -> Optional[s
try:
current_root = current_root[allele_designation.pop(0)]
except KeyError:
return "nonexistent allele for recognised gene"
return "nonexistent allele for recognized gene"

# If there are designator fields past the protein level, just make sure
# they look like legitimate designator field values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _is_synonym(self) -> bool:

def get_reason_why_invalid(self, enforce_functional: bool = False) -> Optional[str]:
if not self._gene_name in VALID_MUSMUSCULUS_MH:
return "unrecognised gene name"
return "unrecognized gene name"

return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ def _try_adding_or_removing_dash1_to_gene_name(self) -> None:

def get_reason_why_invalid(self, enforce_functional: bool = False) -> Optional[str]:
if not self._gene_name in self._valid_tr_dictionary:
return "unrecognised gene name"
return "unrecognized gene name"

if self._allele_designation:
allele_valid = (
self._allele_designation in self._valid_tr_dictionary[self._gene_name]
)

if not allele_valid:
return "nonexistent allele for recognised gene"
return "nonexistent allele for recognized gene"

if (
enforce_functional
Expand Down
16 changes: 11 additions & 5 deletions src/tidytcells/_utils/warnings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from warnings import warn
from logging import Logger


def warn_failure(
reason_for_failure: str, original_input: str, attempted_fix: str, species: str
reason_for_failure: str,
original_input: str,
attempted_fix: str,
species: str,
logger: Logger,
):
warning_message = f'Failed to standardize "{original_input}" for species {species}: {reason_for_failure}.'

if original_input != attempted_fix:
warning_message += f' (best attempted fix: "{attempted_fix}").'

warn(warning_message)
logger.warning(warning_message)


def warn_unsupported_species(species: str, gene_type: str):
warn(f'Unsupported species: "{species}". ' f"Skipping {gene_type} standardisation.")
def warn_unsupported_species(species: str, gene_type: str, logger: Logger):
logger.warning(
f'Unsupported species: "{species}". ' f"Skipping {gene_type} standardisation."
)
8 changes: 5 additions & 3 deletions src/tidytcells/aa/_standardize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import warnings

import logging
from tidytcells._resources import AMINO_ACIDS
from tidytcells._utils import Parameter


logger = logging.getLogger(__name__)


def standardize(seq: str, on_fail: str = "reject", suppress_warnings: bool = False):
"""
Ensures that a string value looks like a valid amino acid sequence.
Expand Down Expand Up @@ -77,7 +79,7 @@ def standardize(seq: str, on_fail: str = "reject", suppress_warnings: bool = Fal
for char in seq:
if not char in AMINO_ACIDS:
if not suppress_warnings:
warnings.warn(
logger.warning(
f"Failed to standardize {original_input}: not a valid amino acid sequence."
)
if on_fail == "reject":
Expand Down
8 changes: 5 additions & 3 deletions src/tidytcells/junction/_standardize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import re
import warnings

from tidytcells import aa


logger = logging.getLogger(__name__)


JUNCTION_MATCHING_REGEX = re.compile(f"^C[A-Z]*[FW]$")


Expand Down Expand Up @@ -112,7 +114,7 @@ def standardize(
if not JUNCTION_MATCHING_REGEX.match(seq):
if strict:
if not suppress_warnings:
warnings.warn(
logger.warning(
f"Failed to standardize {original_input}: not a valid junction sequence."
)
if on_fail == "reject":
Expand Down
10 changes: 6 additions & 4 deletions src/tidytcells/mh/_get_chain.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import re
from typing import Optional
import warnings

from tidytcells._resources import VALID_HOMOSAPIENS_MH
from tidytcells._utils import Parameter


logger = logging.getLogger(__name__)


ALPHA_MATCHING_REGEX = re.compile(r"HLA-([ABCEFG]|D[PQR]A)")
BETA_MATCHING_REGEX = re.compile(r"HLA-D[PQR]B|B2M")

Expand Down Expand Up @@ -51,7 +53,7 @@ def get_chain(

if not gene in (*VALID_HOMOSAPIENS_MH, "B2M"):
if not suppress_warnings:
warnings.warn(f"Unrecognised gene {gene}. Is this standardized?")
logger.warning(f"Unrecognized gene {gene}. Is this standardized?")
return None

if ALPHA_MATCHING_REGEX.match(gene):
Expand All @@ -61,5 +63,5 @@ def get_chain(
return "beta"

if not suppress_warnings:
warnings.warn(f"Chain for {gene} unknown.")
logger.warning(f"Chain for {gene} unknown.")
return None
10 changes: 6 additions & 4 deletions src/tidytcells/mh/_get_class.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import re
from typing import Optional
import warnings

from tidytcells._resources import VALID_HOMOSAPIENS_MH
from tidytcells._utils import Parameter


logger = logging.getLogger(__name__)


CLASS_1_MATCHING_REGEX = re.compile(r"HLA-[ABCEFG]|B2M")
CLASS_2_MATCHING_REGEX = re.compile(r"HLA-D[PQR][AB]")

Expand Down Expand Up @@ -51,7 +53,7 @@ def get_class(

if not gene in (*VALID_HOMOSAPIENS_MH, "B2M"):
if not suppress_warnings:
warnings.warn(f"Unrecognised gene {gene}. Is this standardized?")
logger.warning(f"Unrecognized gene {gene}. Is this standardized?")
return None

if CLASS_1_MATCHING_REGEX.match(gene):
Expand All @@ -61,5 +63,5 @@ def get_class(
return 2

if not suppress_warnings:
warnings.warn(f"Class for {gene} unknown.")
logger.warning(f"Class for {gene} unknown.")
return None
8 changes: 6 additions & 2 deletions src/tidytcells/mh/_standardize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Optional, Type

from tidytcells import _utils
from tidytcells._utils import Parameter
from tidytcells._standardized_gene_symbol import (
Expand All @@ -9,6 +9,9 @@
)


logger = logging.getLogger(__name__)


SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS: Dict[str, Type[StandardizedGeneSymbol]] = {
"homosapiens": StandardizedHlaSymbol,
"musmusculus": StandardizedMusMusculusMhSymbol,
Expand Down Expand Up @@ -150,7 +153,7 @@ def standardize(
species_is_supported = species in SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS
if not species_is_supported:
if not suppress_warnings:
_utils.warn_unsupported_species(species, "MH")
_utils.warn_unsupported_species(species, "MH", logger)
return gene

StandardizedMhSymbolClass = SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS[species]
Expand All @@ -164,6 +167,7 @@ def standardize(
original_input=gene,
attempted_fix=standardized_mh_symbol.compile("allele"),
species=species,
logger=logger,
)
if on_fail == "reject":
return None
Expand Down
8 changes: 6 additions & 2 deletions src/tidytcells/tr/_standardize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Optional, Type

from tidytcells import _utils
from tidytcells._utils import Parameter
from tidytcells._standardized_gene_symbol import (
Expand All @@ -9,6 +9,9 @@
)


logger = logging.getLogger(__name__)


SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS: Dict[str, Type[StandardizedGeneSymbol]] = {
"homosapiens": StandardizedHomoSapiensTrSymbol,
"musmusculus": StandardizedMusMusculusTrSymbol,
Expand Down Expand Up @@ -167,7 +170,7 @@ def standardize(
species_is_supported = species in SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS
if not species_is_supported:
if not suppress_warnings:
_utils.warn_unsupported_species(species, "TR")
_utils.warn_unsupported_species(species, "TR", logger)
return gene

StandardizedTrSymbolClass = SUPPORTED_SPECIES_AND_THEIR_STANDARDIZERS[species]
Expand All @@ -181,6 +184,7 @@ def standardize(
original_input=gene,
attempted_fix=standardized_tr_symbol.compile("allele"),
species=species,
logger=logger,
)
if on_fail == "reject":
return None
Expand Down
22 changes: 9 additions & 13 deletions tests/test_aa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from tidytcells import aa
import warnings


class TestStandardise:
Expand All @@ -11,10 +10,9 @@ def test_alreadY_correct(self, seq):
assert result == seq

@pytest.mark.parametrize("seq", ("123456", "ASDFGHJKL", "A?AAAA", "AAAXAA"))
def test_various_rejections(self, seq):
with pytest.warns(UserWarning, match="not a valid amino acid"):
result = aa.standardize(seq=seq)

def test_various_rejections(self, seq, caplog):
result = aa.standardize(seq=seq)
assert "not a valid amino acid" in caplog.text
assert result == None

@pytest.mark.parametrize(("seq", "expected"), (("klgak", "KLGAK"),))
Expand All @@ -33,13 +31,11 @@ def test_standardise(self):

assert result == "KLGAK"

def test_suppress_warnings(self):
with warnings.catch_warnings():
warnings.simplefilter("error")
aa.standardize(seq="123456", suppress_warnings=True)

def test_on_fail(self):
with pytest.warns(UserWarning):
result = aa.standardize("foobarbaz", on_fail="keep")
def test_suppress_warnings(self, caplog):
aa.standardize(seq="123456", suppress_warnings=True)
assert len(caplog.records) == 0

def test_on_fail(self, caplog):
result = aa.standardize("foobarbaz", on_fail="keep")
assert "Failed to standardize" in caplog.text
assert result == "foobarbaz"
29 changes: 12 additions & 17 deletions tests/test_junction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from tidytcells import junction
import warnings


class TestStandardise:
Expand All @@ -21,10 +20,9 @@ def test_already_correct(self, seq):
assert result == seq

@pytest.mark.parametrize("seq", ("123456", "ASDFGHJKL", "A?AAAA", "AAAXAA"))
def test_various_rejections(self, seq):
with pytest.warns(UserWarning, match="not a valid amino acid sequence"):
result = junction.standardise(seq=seq)

def test_various_rejections(self, seq, caplog):
result = junction.standardise(seq=seq)
assert "not a valid amino acid sequence" in caplog.text
assert result is None

@pytest.mark.parametrize(
Expand All @@ -47,24 +45,21 @@ def test_bad_input_type(self, seq):
junction.standardise(seq=seq)

@pytest.mark.parametrize("seq", ("ASQY", "CASQY", "ASQYF", "ASQYW"))
def test_strict(self, seq):
with pytest.warns(UserWarning, match="not a valid junction"):
result = junction.standardise(seq=seq, strict=True)

def test_strict(self, seq, caplog):
result = junction.standardise(seq=seq, strict=True)
assert "not a valid junction" in caplog.text
assert result is None

def test_standardize(self):
result = junction.standardize(seq="CASSPGGADRRIDGYTF")

assert result == "CASSPGGADRRIDGYTF"

def test_suppress_warnings(self):
with warnings.catch_warnings():
warnings.simplefilter("error")
junction.standardise(seq="123456", suppress_warnings=True)

def test_on_fail(self):
with pytest.warns(UserWarning):
result = junction.standardize("foobarbaz", on_fail="keep")
def test_suppress_warnings(self, caplog):
junction.standardise(seq="123456", suppress_warnings=True)
assert len(caplog.records) == 0

def test_on_fail(self, caplog):
result = junction.standardize("foobarbaz", on_fail="keep")
assert "Failed to standardize foobarbaz" in caplog.text
assert result == "foobarbaz"
Loading

0 comments on commit a37479f

Please sign in to comment.