General Utilities
The general utilities module provides a collection of helper functions that are used across various parts of the SpectraNorm package. These functions include tools for logging, data validation, directory management, and other general-purpose tasks that support the core functionality of the package.
spectranorm.utils.general
utils/general.py
General utility functions for spectranorm.
ReportTimeFormatter
Bases: Formatter
Custom log formatter that injects general.report_time().
Source code in src/spectranorm/utils/general.py
ensure_dir(file_name: Path) -> Path
Ensure that the directory for the given file name exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
Path
|
Path The file name for which to ensure the directory exists. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path The original file name. |
Source code in src/spectranorm/utils/general.py
get_logger(name: str, level: int = logging.INFO) -> logging.Logger
Get a logger with standardized formatting and time reporting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name (usually name). |
required |
level
|
int
|
Logging level, default INFO. |
INFO
|
Returns:
| Type | Description |
|---|---|
Logger
|
logging.Logger with a custom ReportTimeFormatter. |
Source code in src/spectranorm/utils/general.py
prepare_save_directory(directory: Path, subdirectory: str = 'saved_model') -> Path
Prepare a directory to save a model.
A subdirectory named 'saved_model' will be created if it does not exist. If this directory exists, but is not empty, an error is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
Path to a directory to save the model. |
required |
subdirectory
|
str
|
Name of the subdirectory to create (default: "saved_model"). |
'saved_model'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path The path to the created subdirectory. |
Source code in src/spectranorm/utils/general.py
report_time(*, relative_to: float | None = None, absolute: bool = False) -> str | float
Report the current time or the time elapsed since a given reference point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relative_to
|
float | None
|
float | None The reference time in seconds since the epoch. If None, report the current time. |
None
|
absolute
|
bool
|
bool If True, report the absolute time format (float). If False, report the time in a human-readable format (str). |
False
|
Returns:
| Type | Description |
|---|---|
str | float
|
str | float The current time or the elapsed time since the reference point. |
Source code in src/spectranorm/utils/general.py
suppress_output() -> Iterator[None]
Context manager to suppress stdout and stderr output.
Source code in src/spectranorm/utils/general.py
validate_dataframe(dataframe: pd.DataFrame, column_names: list[str]) -> None
Validate the input DataFrame to ensure all required columns are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataframe
|
DataFrame
|
pd.DataFrame The DataFrame to validate. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the DataFrame is not valid. |
Source code in src/spectranorm/utils/general.py
validate_load_directory(directory: Path, subdirectory: str = 'saved_model') -> Path
Validate the directory structure for loading a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
Path to the main directory. |
required |
subdirectory
|
str
|
Name of the subdirectory to check (default: "saved_model"). |
'saved_model'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path The path to the validated subdirectory. |