generated from frehburg/TemplateForPythonProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from BIH-CEI/31-auto-parse-ordinals-in-data-mo…
…del-input added auto ordinal parsing we did it after doctest hell
- Loading branch information
Showing
12 changed files
with
212 additions
and
82 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/rarelink_phenopacket_mapper/data_standards/data_models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""Selection of rare disease specific data models""" | ||
from .rarelink_datamodel import RARELINK_DATA_MODEL | ||
from .erdri_cds import ERDRI_CDS | ||
from .data_type_string_representation import parse_type_string_representation | ||
from rarelink_phenopacket_mapper.utils.parsing.parse_data_type import parse_data_type | ||
|
||
__all__ = ["RARELINK_DATA_MODEL", "ERDRI_CDS", "parse_type_string_representation"] | ||
__all__ = ["RARELINK_DATA_MODEL", "ERDRI_CDS", "parse_data_type"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""This submodule contains utility functions that are used throughout the package.""" | ||
from .create_ipynb_in_code import NotebookBuilder | ||
from .pandas_utils import loc_default | ||
|
||
__all__ = ["NotebookBuilder"] | ||
__all__ = ["NotebookBuilder", "loc_default"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rarelink_phenopacket_mapper.data_standards import DataField | ||
|
||
|
||
def get_field_name(field): | ||
return field.__name__ | ||
|
||
|
||
if __name__ == "__main__": | ||
print(get_field_name(DataField.data_type)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Any | ||
|
||
import pandas as pd | ||
|
||
|
||
def loc_default(df: pd.DataFrame, row_index: int, column_name: str, default: Any = '') -> Any: | ||
"""Safely performs loc on a `pd.DataFrame`, returns default value if something goes wrong | ||
:param df: the dataframe | ||
:param row_index: index of the row | ||
:param column_name: name of the column | ||
:param default: default value to return if some error occurs | ||
:return: Value at the row and column specified | ||
""" | ||
try: | ||
return df.loc[row_index, column_name] | ||
except Exception as e: | ||
print(e) | ||
return default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""This module contains utility functions concerning the parsing of strings to python values""" | ||
|
||
from .parse_data_type import parse_data_type | ||
from .parse_ordinal import parse_ordinal | ||
|
||
__all__ = [ | ||
"parse_data_type", | ||
"parse_ordinal" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/rarelink_phenopacket_mapper/utils/parsing/parse_ordinal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Tuple | ||
import re | ||
|
||
|
||
def parse_ordinal(field_name_str: str) -> Tuple[str, str]: | ||
""" | ||
Parsing `DataField.name` string to separate strings containing the ordinal and the name respectively | ||
This method is meant as part of reading in a `DataModel` from a file, where data model fields might have an ordinal | ||
attached to them (e.g., "1.1. Pseudonym"), which this method can then neatly separate into ordinal="1.1." and | ||
name="Pseudonym". | ||
>>> parse_ordinal("1.1. Pseudonym") | ||
('1.1', 'Pseudonym') | ||
>>> parse_ordinal("1. Pseudonym") | ||
('1', 'Pseudonym') | ||
>>> parse_ordinal("I.a. Pseudonym") | ||
('I.a', 'Pseudonym') | ||
>>> parse_ordinal("ii. Pseudonym") | ||
('ii', 'Pseudonym') | ||
:param field_name_str: name of the field, containing an ordinal, to parse | ||
:returns: a tuple containing the ordinal and the name | ||
""" | ||
# Regex to extract the section number and field name | ||
match = re.match(r"([0-9]+(?:\.[0-9]+)*|[Iivxlc]+\.[a-z]*|[a-z]*)\.?\s*(.+)", field_name_str, re.IGNORECASE) | ||
|
||
if match: | ||
# Extract the field number and description | ||
ordinal = match.group(1).strip() | ||
field_name = match.group(2).strip() | ||
|
||
if ordinal[-1] == '.': | ||
ordinal = ordinal[0:-1] | ||
|
||
return ordinal, field_name | ||
else: | ||
return '', field_name_str # since this is more for optics, do not raise error and just do "nothing" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
|
||
from rarelink_phenopacket_mapper.utils.parsing import parse_ordinal | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"field_name, result", [ | ||
("1.1. Pseudonym", ("1.1", "Pseudonym")), | ||
("2.1. Date of Birth", ("2.1", "Date of Birth")), | ||
("2.2. Sex", ("2.2", "Sex")), | ||
("3.1. Patient's status", ("3.1", "Patient's status")), | ||
("3.2. Date of death", ("3.2", "Date of death")), | ||
("4.1. First contact with specialised centre", ("4.1", "First contact with specialised centre")), | ||
("5.1. Age at onset", ("5.1", "Age at onset")), | ||
("5.2. Age at diagnosis", ("5.2", "Age at diagnosis")), | ||
("6.1. Diagnosis of the rare disease", ("6.1", "Diagnosis of the rare disease")), | ||
("6.2. Genetic diagnosis", ("6.2", "Genetic diagnosis")), | ||
("6.3. Undiagnosed case", ("6.3", "Undiagnosed case")), | ||
("1.1 Pseudonym", ("1.1", "Pseudonym")), | ||
("1. Pseudonym", ("1", "Pseudonym")), | ||
("1 Pseudonym", ("1", "Pseudonym")), | ||
("I.a. Pseudonym", ("I.a", "Pseudonym")), | ||
("I.a Pseudonym", ("I.a", "Pseudonym")), | ||
("ii. Pseudonym", ("ii", "Pseudonym")), | ||
("ii Pseudonym", ("ii", "Pseudonym")), | ||
] | ||
) | ||
def test_parse_ordinal(field_name, result): | ||
assert parse_ordinal(field_name) == result |