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 #118 from BIH-CEI/115-add-preprocessing-2
115 add preprocessing 2
- Loading branch information
Showing
6 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
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
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,7 @@ | ||
"""Methods for preprocessing data before mapping to Phenopackets.""" | ||
|
||
from .preprocess_dict import preprocess_dict | ||
from .preprocess_method import preprocess_method | ||
from .preprocess import preprocess | ||
|
||
__all__ = ["preprocess_dict", "preprocess_method", "preprocess"] |
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,23 @@ | ||
import warnings | ||
from typing import Any, Union, Dict, Callable | ||
|
||
from phenopacket_mapper.preprocessing import preprocess_dict, preprocess_method | ||
|
||
|
||
def preprocess( | ||
value: Any, | ||
mapping: Union[Dict, Callable], | ||
**kwargs | ||
) -> Any: | ||
"""Preprocess a value before mapping to a Phenopacket. | ||
Relies on `preprocess_dict` and `preprocess_method` to preprocess using a dictionary or method, respectively. Please | ||
consult the documentation for these functions for more information. | ||
""" | ||
if isinstance(mapping, dict): | ||
return preprocess_dict(value, mapping) | ||
elif isinstance(mapping, Callable): | ||
return preprocess_method(value, mapping, **kwargs) | ||
|
||
warnings.warn(f"Mapping type {type(mapping)} in preprocessing not supported. Returning original value.") | ||
return value |
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,21 @@ | ||
import warnings | ||
from typing import Any, Dict | ||
|
||
|
||
def preprocess_dict(value: Any, mapping_dict: Dict) -> Any: | ||
"""Takes a value and uses a mapping dictionary to preprocess it. | ||
If the value is in the mapping dictionary, the corresponding value is returned. | ||
If the value is not in the mapping dictionary, the original value is returned. | ||
:param value: The value to preprocess. | ||
:param mapping_dict: A dictionary containing the mapping rules. | ||
:return: The preprocessed value. | ||
""" | ||
try: | ||
ret_value = mapping_dict[value] | ||
except KeyError: | ||
ret_value = value | ||
warnings.warn(f"Value {value} not found in mapping dictionary.") | ||
|
||
return ret_value |
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,25 @@ | ||
from typing import Any, Callable | ||
|
||
|
||
def preprocess_method(value: Any, method: Callable, **kwargs) -> Any: | ||
"""Takes a value and uses a method to preprocess it. | ||
The method is called with the value as an argument. | ||
If the method raises an exception, the original value is returned. | ||
If the method requires additional arguments, they can be passed as keyword arguments in `kwargs`. | ||
Please write the method such that it is callable as `method(value, **kwargs)`. | ||
:param value: The value to preprocess. | ||
:param method: The method to use for preprocessing. | ||
:param kwargs: Additional arguments for the method. | ||
:return: The preprocessed value. | ||
""" | ||
try: | ||
ret_value = method(value, **kwargs) | ||
except Exception as e: | ||
ret_value = value | ||
print(f"Error while preprocessing value {value} with method {method}. Error message: {e}") | ||
|
||
return ret_value |