Utils Reference
This page documents the utils sub-package.
medpipe.utils.logger
Logger functions.
This module provides helper functions for logging.
Functions: - setup_logger: sets up a logger for a script. - exception_handler: Function that logs exceptions. - print_message: Function that prints and logs an info message.
exception_handler(logger, log_path, log_config, script_name)
Handles exceptions and logs them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger object that logs the exception. |
required |
log_path
|
str
|
Path to the log file being used. |
required |
log_config
|
dict
|
Configuration parameters for the log messages. |
required |
script_name
|
str
|
Name of the script in which the error occurred. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/utils/logger.py
print_message(message, logger=None, script_name='')
Wrapper function to print message or log them.
If the logger.level is less than 0, only log message. If the logger.level is greater than 0, log and print. If logger is None, only print.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message to print or log. |
required |
logger
|
logging.Logger or None, default:None
|
Logger to log message. If None message is printed to terminal. |
None
|
script_name
|
str
|
Script name to know where the message is coming from. |
""
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/utils/logger.py
setup_logger(script_name, log_path)
Setups a logger for logging exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_name
|
str
|
Name of the script to log exceptions from. |
required |
log_path
|
str
|
Path to the folder to store the log file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
logger |
Logger
|
Logger object. |
Raises
|
|
|
______
|
|
|
TypeError
|
If script_name or log_path are not a str. |
|
FileNotFoundError
|
If log_path do not exist. |
|
NotADirectoryError
|
If log_path is not a directory. |
Source code in src/medpipe/utils/logger.py
medpipe.utils.io
I/O utilities module.
This module provides helper functions for reading from and writing to files, handling various common I/O tasks.
Functions: - load_data_from_csv: Loads the data from a .csv file. - read_toml_configuration: Parses the contents of a .TOML file.
load_data_from_csv(data_file)
Reads a .csv file and returns its contents.
Parameters
data_file Path to the .csv file to load.
Returns
data : pd.DataFrame Loaded data.
Raises
TypeError If data_file is not a str. FileNotFoundError If data_file does not exist. IsADirectoryError If data_file is not a file. ValueError If data_file extension is not .csv file.
Source code in src/medpipe/utils/io.py
read_toml_configuration(config_file)
Reads a .TOML configuration file and returns contents.
The function adds the base_dir variable to any dir variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file
|
str
|
Path to the configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
config |
dict
|
Configuration contents as a dictionary. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If config_file is not a str. |
FileNotFoundError
|
If config_file does not exist. |
IsADirectoryError
|
If config_file is not a file. |
ValueError
|
If data_file extension is not .csv file. |
TOMLDecodeError
|
If the file was not read properly. |
Source code in src/medpipe/utils/io.py
medpipe.utils.exceptions
Execption functions module.
This module provides functions for execption handling and raising.
Functions: - file_checks: Checks if the file is correct. - path_checks: Checks if the path is correct. - array_check: Checks for an array-like. - array_dim_check: Checks that the dimension of two arrays agree.
array_check(arr)
Checks that the input is an array-like.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
array - like
|
Array to check. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arr is not an array-like. |
Source code in src/medpipe/utils/exceptions.py
array_dim_check(arr1, arr2, dim=None)
Checks that the dimensions of the arrays match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr1
|
array - like
|
First array. |
required |
arr2
|
array - like
|
Second array. |
required |
dim
|
int or None
|
Dimension to compare. If None shape is used. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If dim is not an integer. |
ValueError
|
If the arrays do not have the same dimensions. |
Source code in src/medpipe/utils/exceptions.py
file_checks(file, extension, exists=True)
Performs checks to ensure that a file and extension are correct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
File to check. |
required |
extension
|
str
|
Extension of the file to check. |
required |
exists
|
default: True
|
Flag to indicate if the file should exists. |
True
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If file is not a str. |
FileNotFoundError
|
If file does not exist. |
IsADirectoryError
|
If file is a directory. |
ValueError
|
If file extension is not .extension file. |
Source code in src/medpipe/utils/exceptions.py
path_checks(path)
Performs checks to ensure that a path is correct and creates it if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If path is not a str. |
FileNotFoundError
|
If path does not exist. |
NotADirectoryError
|
If path is not a directory. |
Source code in src/medpipe/utils/exceptions.py
medpipe.utils.config
Configuration utilities module.
This module provides helper functions for reading configuration files.
Functions: - get_file_path: Gets a file path from a configuration dictionary. - get_configuration: Gets the configuration by chaining .toml configurations. - parse_version_number: Function that parses a version number. - split_version_number: Splits a version number into the data and model version numbers.
get_configuration(parameters, v_number)
Gets the configuration by chaining .toml configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
dict
|
Parameters for the configuration chaining. |
required |
v_number
|
str
|
Version number of the data to recuperate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
config_dict |
dict
|
Configuration parameters dictionary. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If parameters is not a dict. |
Source code in src/medpipe/utils/config.py
get_file_path(config_dict, v_number='', path_type='io', exists=True)
Gets the path to a file from a configuration dictionary.
If the path_type is "fig", the extension is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dict
|
dict
|
Dictionary from a loaded .TOML file. |
required |
v_number
|
default: ""
|
Version number. |
''
|
path_type
|
(io, db, data, fig)
|
Path type in the configuration file. |
"io"
|
exists
|
default: True
|
Flag to indicate if the file should exists. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
file_path |
str
|
Path to the file. |
Source code in src/medpipe/utils/config.py
parse_version_number(v_number)
Parses a version number.
Expecting a version number in the format vX.Y.Z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v_number
|
str
|
Version number to parse. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
v_list |
list[str]
|
List containing [source, extraction, preprocessing] numbers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If v_number is not a string. |
Source code in src/medpipe/utils/config.py
split_version_number(v_number)
Splits a version number into the data and model version numbers.
Expecting a version number in the format vX.Y.Z-nN, where X.Y.Z is the data version number and nN is the model version number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v_number
|
str
|
Data version number to split. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
data_v_number |
str
|
Data version number in the format vX.Y.Z. |
model_v_number |
str
|
Model version number in the format vnN. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If v_number is not a string. |
ValueError
|
If v_number format is incorrect. |