Utilities
Submodules
kale.utils.download module
kale.utils.initialize_nn module
Provide methods for initializing neural network parameters (i.e., weights and biases).
- kale.utils.initialize_nn.xavier_init(module) None
Fills the weight of the input Tensor with values using a normal distribution.
- Parameters:
module (torch.Tensor) – The input module.
- kale.utils.initialize_nn.bias_init(module) None
Fills the bias of the input Tensor with zeros.
- Parameters:
module (torch.Tensor) – The input module.
kale.utils.logger module
Logging functions, based on https://github.com/HaozhiQi/ISONet/blob/master/isonet/utils/logger.py
- kale.utils.logger.out_file_core()
Creates an output file name concatenating a formatted date and uuid, but without an extension.
- Returns:
A string to be used in a file name.
- Return type:
string
- kale.utils.logger.construct_logger(name, save_dir, log_to_terminal=False)
Constructs a logger. Saves the output as a text file at a specified path. Also saves the output of git diff HEAD to the same folder. Takes option to log to terminal, which will print logging statements. Default is False.
The logger is configured to output messages at the DEBUG level, and it saves the output as a text file with a name based on the current timestamp and the specified name. It also saves the output of git diff HEAD to a file with the same name and the extension .gitdiff.patch.
- Parameters:
name (str) – The name of the logger, typically the name of the method being logged.
save_dir (str) – The directory where the log file and git diff file will be saved.
log_to_terminal (bool, optional) – Whether to also log messages to the terminal. Defaults to False.
- Returns:
The constructed logger.
- Return type:
logging.Logger
- Raises:
None. –
kale.utils.print module
Screen printing functions, from https://github.com/HaozhiQi/ISONet/blob/master/isonet/utils/misc.py
- kale.utils.print.tprint(*args)
Temporarily prints things on the screen so that it won’t be flooded
- kale.utils.print.pprint(*args)
Permanently prints things on the screen to have all info displayed
- kale.utils.print.pprint_without_newline(*args)
Permanently prints things on the screen, separated by space rather than newline
kale.utils.save_xlsx module
Authors: Lawrence Schobs, lawrenceschobs@gmail.com
Functions to save results to Excel files.
- kale.utils.save_xlsx.generate_summary_df(results_dictionary: dict, cols_to_save: list, sheet_name: str, save_location: str) DataFrame
Generates pandas dataframe with summary statistics. Designed for use in Quantile Binning (/pykale/examples/landmark_uncertainty/main.py).
- Parameters:
results_dictionary (dict) – A dictionary containing results for each quantile bin and uncertainty method. The keys are strings indicating the name of each uncertainty method. The values are dictionaries containing results for each quantile bin.
cols_to_save (list) – A list of 2-element lists, each containing a string indicating the key in the results_dictionary and a string indicating the name to use for that column in the output dataframe.
sheet_name (str) – The name of the sheet to create in the output Excel file.
save_location (str) – The file path and name to use for the output Excel file.
- Returns:
A dataframe with statistics including mean error, std error of All and individual targets. Also includes the Sucess detection rates (SDR). The dataframe should have the following structure:
- df = {
“All um col_save_name Mean”: value, “All um col_save_name Std”: value, “B1 um col_save_name Mean”: value, “B1 um col_save_name Std”: value, …
}
- Return type:
pd.DataFrame
- kale.utils.save_xlsx.save_dict_xlsx(data_dict: Dict[Any, Any], save_location: str, sheet_name: str) None
Save a dictionary to an Excel file using the XlsxWriter engine.
- Parameters:
data_dict (Dict[Any, Any]) – The dictionary that needs to be saved to an Excel file. The keys represent the row index and the values represent the data in the row. If a dictionary value is a list or a series, each element in the list/series will be a column in the row.
save_location (str) – The location where the Excel file will be saved. This should include the full path and the filename, for example, “/path/to/save/data.xlsx”. Overwrites the file if it already exists.
sheet_name (str) – The name of the sheet where the dictionary will be saved in the Excel file.
- Returns:
This function does not return anything. It saves the dictionary as an Excel file at the specified location.
- Return type:
None
kale.utils.seed module
Setting seed for reproducibility
- kale.utils.seed.set_seed(seed=1000)
Sets the seed for generating random numbers to get (as) reproducible (as possible) results.
The CuDNN options are set according to the official PyTorch guidance on reproducibility: https://pytorch.org/docs/stable/notes/randomness.html. Another references are https://discuss.pytorch.org/t/difference-between-torch-manual-seed-and-torch-cuda-manual-seed/13848/6 https://pytorch.org/docs/stable/cuda.html#torch.cuda.manual_seed https://github.com/open-mmlab/mmcv/blob/master/mmcv/runner/utils.py#L58
- Parameters:
seed (int, optional) – The desired seed. Defaults to 1000.
kale.utils.remap_model_parameters module
- kale.utils.remap_model_parameters.remap_state_dict_keys(state_dict)
Remap parameter names in a state_dict to match updated module names or variable conventions.
This utility function updates parameter keys from a loaded checkpoint to match a model’s new architecture, such as when module or variable names have changed (e.g., from “ecg_encoder” to “signal_encoder”). This allows loading pretrained weights into a model with different internal naming conventions.
- Parameters:
state_dict (dict) – State dictionary from a pretrained PyTorch model, typically loaded using
torch.load.- Returns:
A new state dictionary with parameter names remapped according to the defined mapping.
- Return type:
dict