autoqild.utilities.utils¶
Implements several utility functions for array normalization, logging exceptions, managing HDF5 files, and creating directories safely.
Functions
|
Check if an HDF5 file is corrupt and delete it if necessary. |
|
Create a directory if it does not exist, handling potential errors safely. |
|
Log an exception with traceback details. |
|
Compute the log of the sum of exponentials of input elements. |
|
Normalize the input array along the specified axis. |
|
Format the dictionary to print it in logs. |
|
Display a progress bar in the console. |
|
Compute the sigmoid of the input array. |
|
Compute the softmax of input elements. |
|
Standardize the features in the training and test sets using the specified scaler. |
- autoqild.utilities.utils.check_and_delete_corrupt_h5_file(file_path, logger)[source]¶
Check if an HDF5 file is corrupt and delete it if necessary.
- Parameters:
file_path (str) – Path to the HDF5 file.
logger (logging.Logger) – Logger instance to log actions.
- autoqild.utilities.utils.create_directory_safely(path, is_file_path=False)[source]¶
Create a directory if it does not exist, handling potential errors safely.
- Parameters:
path (str) – Path to the directory or file.
is_file_path (bool, optional) – If True, considers “path” as a file path and creates the directory containing the file.
- autoqild.utilities.utils.log_exception_error(logger, e)[source]¶
Log an exception with traceback details.
- Parameters:
logger (logging.Logger) – Logger instance to log the error.
e (Exception) – Exception instance to log.
- autoqild.utilities.utils.logsumexp(x, axis=1)[source]¶
Compute the log of the sum of exponentials of input elements.
- Parameters:
x (array-like) – Input array.
axis (int, optional) – Axis along which the sum is computed. Default is 1.
- Returns:
lsum_exp – An array with the log of the sum of exponentials of elements along the specified axis.
- Return type:
array-like
- autoqild.utilities.utils.normalize(x, axis=1)[source]¶
Normalize the input array along the specified axis.
- Parameters:
x (array-like) – Input array to normalize.
axis (int, optional) – Axis along which the normalization is applied. Default is 1.
- Returns:
normed – The normalized array.
- Return type:
array-like
- autoqild.utilities.utils.print_dictionary(dictionary, sep='\n', n_keys=None)[source]¶
Format the dictionary to print it in logs.
- dictionarydict
The dictionary to print.
- sepstr, optional
The separator between key-value pairs. The default is ‘
- ‘.
- n_keysint, optional
The number of key-value pairs to print. If None, all pairs are printed.
- outputstr
Formatted string representation of the dictionary.
- autoqild.utilities.utils.progress_bar(count, total, status='')[source]¶
Display a progress bar in the console.
- Parameters:
count (int) – Current progress count.
total (int) – Total count for completion.
status (str, optional) – A status message to display along with the progress bar.
- autoqild.utilities.utils.sigmoid(x)[source]¶
Compute the sigmoid of the input array.
- Parameters:
x (array-like) – Input array.
- Returns:
x – The sigmoid of the input array.
- Return type:
array-like
- autoqild.utilities.utils.softmax(x, axis=1)[source]¶
Compute the softmax of input elements.
- Parameters:
x (array-like) – Input array.
axis (int, optional) – Axis along which the softmax is computed. Default is 1.
- Returns:
s_values – An array with the softmax applied along the specified axis.
- Return type:
array-like
- autoqild.utilities.utils.standardize_features(x_train, x_test, scaler=<class 'sklearn.preprocessing._data.RobustScaler'>, scaler_params={})[source]¶
Standardize the features in the training and test sets using the specified scaler.
The function offers flexibility to choose between StandardScaler, RobustScaler, and MinMaxScaler. It allows customization of the chosen scaler’s parameters using a dictionary and raises a ValueError if an unsupported scaler is passed.
- Parameters:
x_train (array-like of shape (n_samples, n_features)) – Training set features.
x_test (array-like of shape (n_samples, n_features)) – Test set features.
scaler ({StandardScaler, RobustScaler, MinMaxScaler}, optional, default=RobustScaler) – The scaling class to be used for standardization. Choose from: - StandardScaler: Standardize features by removing the mean and scaling to unit variance. - RobustScaler: Scale features using statistics that are robust to outliers. - MinMaxScaler: Scale features to a given range (usually between 0 and 1).
scaler_params (dict, optional, default={}) – Parameters to be passed to the selected scaler. Example: {‘with_mean’: False} for StandardScaler.
- Returns:
x_train (array-like of shape (n_samples, n_features)) – Standardized training set features.
x_test (array-like of shape (n_samples, n_features)) – Standardized test set features.
- Raises:
ValueError – If the specified scaler is not one of StandardScaler, RobustScaler, or MinMaxScaler.
Example
>>> from sklearn.preprocessing import StandardScaler, RobustScaler, MinMaxScaler >>> import numpy as np >>> x_train = np.array([[1, 2], [2, 3], [3, 4]]) >>> x_test = np.array([[4, 5], [5, 6]])
# Example with StandardScaler and a custom parameter >>> scaler_params = {‘with_mean’: False} >>> x_train_scaled, x_test_scaled = standardize_features( … x_train, x_test, scaler=StandardScaler, scaler_params=scaler_params … )
# Example with RobustScaler (default) >>> x_train_scaled, x_test_scaled = standardize_features(x_train, x_test, scaler=RobustScaler)
# Example with MinMaxScaler >>> x_train_scaled, x_test_scaled = standardize_features(x_train, x_test, scaler=MinMaxScaler)
# Example with an invalid scaler (this will raise a ValueError) >>> try: … x_train_scaled, x_test_scaled = standardize_features(x_train, x_test, scaler=”InvalidScaler”) … except ValueError as e: … print(e) ‘Invalid scaler specified. Choose from StandardScaler, RobustScaler, or MinMaxScaler.’