mednet.scripts.utils¶
Utilities for command-line scripts.
Module Attributes
Defines types that can be encoded in a JSON string. |
|
Defines a type for the metric used to track and save the best checkpoint of a model. |
Functions
|
Generate information concerning hardware properties. |
Produce metadata concerning the running script, in the form of a dictionary. |
|
|
Retrieve information regarding the metric and modality used to save the best checkpoint of the model by looking at the train metadata in the json file. |
|
Save a little summary of the model in a txt file. |
|
Validate and then parse the string as a 'CheckpointMetricType'. |
|
Save prediction hyperparameters into a .json file. |
|
Save a dictionary into a JSON file with path checking and backup. |
Classes
|
Extends the standard JSON encoder to support Numpy arrays. |
- mednet.scripts.utils.JSONable¶
Defines types that can be encoded in a JSON string.
alias of
Mapping[str,JSONable] |Sequence[JSONable] |str|int|float|bool|None
- mednet.scripts.utils.CheckpointMetricType¶
Defines a type for the metric used to track and save the best checkpoint of a model. This type represents a constrained string in the format ‘mode/metric’, where: - ‘mode’ is either ‘min’ or ‘max’, indicating the optimization direction; - ‘metric’ is a non-empty string specifying the name of the evaluation metric (e.g., ‘loss’, ‘auc’).
alias of
Annotated[str,StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=None, max_length=None, pattern=^(min|max)/.+$, ascii_only=None)]
- mednet.scripts.utils.parse_checkpoint_metric(value)[source]¶
Validate and then parse the string as a ‘CheckpointMetricType’.
- mednet.scripts.utils.model_summary(model)[source]¶
Save a little summary of the model in a txt file.
- mednet.scripts.utils.device_properties(device_type)[source]¶
Generate information concerning hardware properties.
- mednet.scripts.utils.execution_metadata()[source]¶
Produce metadata concerning the running script, in the form of a dictionary.
This function returns potentially useful metadata concerning program execution. It contains a certain number of preset variables.
- Return type:
- Returns:
A dictionary that contains the following fields:
package-name: current package name (e.g.mednet)package-version: current package version (e.g.1.0.0b0)datetime: date and time in ISO8601 format (e.g.2024-02-23T18:38:09+01:00)user: username (e.g.johndoe)conda-env: if set, the name of the current conda environmentpath: current path when executing the commandcommand-line: the command-line that is being runhostname: machine hostname (e.g.localhost)platform: machine platform (e.g.darwin)accelerator: acceleration devices available (e.g.cuda)
- class mednet.scripts.utils.NumpyJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
Bases:
JSONEncoderExtends the standard JSON encoder to support Numpy arrays.
- mednet.scripts.utils.save_json_with_backup(path, data)[source]¶
Save a dictionary into a JSON file with path checking and backup.
This function will save a dictionary into a JSON file. It will check to the existence of the directory leading to the file and create it if necessary. If the file already exists on the destination folder, it is backed-up before a new file is created with the new contents.
- Parameters:
path (
Path) – The full path where to save the JSON data.data (
Union[Mapping[str,Union[Mapping[str, JSONable],Sequence[JSONable],str,int,float,bool,None]],Sequence[Union[Mapping[str, JSONable],Sequence[JSONable],str,int,float,bool,None]],str,int,float,bool,None]) – The data to save on the JSON file.
- Return type:
- mednet.scripts.utils.save_json_metadata(output_file, **kwargs)[source]¶
Save prediction hyperparameters into a .json file.
- Return type:
- mednet.scripts.utils.get_ckpt_metric_mode(train_metadata_file, default_metric='loss', default_mode='min')[source]¶
Retrieve information regarding the metric and modality used to save the best checkpoint of the model by looking at the train metadata in the json file.
- Parameters:
train_metadata_file (
Path) – Path of the train.meta.json file.default_metric (
str) – The metric name to return when no metric information is found in train.meta JSON file. The default value is set to “loss”.default_mode (
Literal['min','max']) – The modality of evaluation to return when no mode information is found in train.meta JSON file. The default value is set to “min”.
- Return type:
- Returns:
The name of the metric used for saving the best checkpoint and the modality {‘min’, ‘max’} in this exact order.