From 092ae35512b3ef236622c4ab9dfe2bafb221ded7 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 12 Feb 2024 13:03:35 +0300 Subject: Refactor: Use new decimal places checker. --- quality_control/checks.py | 18 ++++++++++++++++++ r_qtl/r_qtl2_qc.py | 26 ++++---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/quality_control/checks.py b/quality_control/checks.py index 28b9ab5..475eb9e 100644 --- a/quality_control/checks.py +++ b/quality_control/checks.py @@ -2,6 +2,8 @@ import re from typing import Optional +from .errors import InvalidValue + def decimal_places_pattern(mini: int, maxi: Optional[int] = None) -> re.Pattern: """ Generate a regular expression for checking numbers @@ -49,3 +51,19 @@ def decimal_places_pattern(mini: int, maxi: Optional[int] = None) -> re.Pattern: + r"}" + r")$" ) + +def decimal_points_error(lineno: int, + field: str, + value: str, + mini: int, + maxi: Optional[int] = None) -> Optional[InvalidValue]: + """ + Check that 'value' in a decimal number with the appropriate decimal places. + """ + if not bool(decimal_places_pattern(mini, maxi).match(value)): + return InvalidValue(lineno, field, value, ( + f"Invalid value '{value}'. Expected numerical value " + + f"with at least {mini} decimal places" + + (f" and at most {maxi} decimal places" if maxi is not None else "") + + ".")) + return None diff --git a/r_qtl/r_qtl2_qc.py b/r_qtl/r_qtl2_qc.py index f62f142..aa5c81e 100644 --- a/r_qtl/r_qtl2_qc.py +++ b/r_qtl/r_qtl2_qc.py @@ -1,7 +1,6 @@ """Quality control checks for R/qtl2 data bundles.""" -import re from zipfile import ZipFile -from functools import reduce +from functools import reduce, partial from typing import Union, Iterator, Optional, Callable from r_qtl import errors as rqe @@ -10,6 +9,7 @@ from r_qtl.r_qtl2 import __FILE_TYPES__ from r_qtl.fileerrors import MissingFile from quality_control.errors import InvalidValue +from quality_control.checks import decimal_points_error def bundle_files_list(cdata: dict) -> tuple[tuple[str, str], ...]: """Retrieve files listed in control file.""" @@ -83,34 +83,16 @@ def geno_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: def pheno_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: """Check for and retrieve pheno errors.""" - def __min_3_decimal_places__( - lineno: int, field: str, value: str) -> Optional[InvalidValue]: - if not (re.search(r"^([0-9]+\.[0-9]{3,}|[0-9]+\.?0*)$", value) - or re.search(r"^0\.0+$", value) - or re.search("^0+$", value)): - return InvalidValue(lineno, field, value, ( - f"Invalid value '{value}'. Expected numerical value " - "with at least 3 decimal places.")) - return None return ( error for error in retrieve_errors( - zfile, "pheno", (__min_3_decimal_places__,)) + zfile, "pheno", (partial(decimal_points_error, mini=3),)) if error is not None) def phenose_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: """Check for and retrieve phenose errors.""" - def __min_6_decimal_places__( - lineno: int, field: str, value: str) -> Optional[InvalidValue]: - if not (re.search(r"^([0-9]+\.[0-9]{6,}|[0-9]+\.?0*)$", value) - or re.search(r"^0\.0+$", value) - or re.search("^0+$", value)): - return InvalidValue(lineno, field, value, ( - f"Invalid value '{value}'. Expected numerical value " - "with at least 6 decimal places.")) - return None return ( error for error in retrieve_errors( - zfile, "phenose", (__min_6_decimal_places__,)) + zfile, "phenose", (partial(decimal_points_error, mini=6),)) if error is not None) def retrieve_errors(zfile: ZipFile, filetype: str, checkers: tuple[Callable]) -> Iterator[ -- cgit v1.2.3