mednet.scripts.utils

Utilities for command-line scripts.

Module Attributes

JSONable

Defines types that can be encoded in a JSON string.

CheckpointMetricType

Defines a type for the metric used to track and save the best checkpoint of a model.

Functions

device_properties(device_type)

Generate information concerning hardware properties.

execution_metadata()

Produce metadata concerning the running script, in the form of a dictionary.

get_ckpt_metric_mode(train_metadata_file[, ...])

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.

model_summary(model)

Save a little summary of the model in a txt file.

parse_checkpoint_metric(value)

Validate and then parse the string as a 'CheckpointMetricType'.

save_json_metadata(output_file, **kwargs)

Save prediction hyperparameters into a .json file.

save_json_with_backup(path, data)

Save a dictionary into a JSON file with path checking and backup.

Classes

NumpyJSONEncoder(*[, skipkeys, ...])

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’.

Parameters:

value (str) – The string to be validated and then parsed.

Return type:

tuple[str, Literal['min', 'max']]

Returns:

  • The name of the metric used for saving the best checkpoint and the modality

  • {‘min’, ‘max’} in this exact order.

mednet.scripts.utils.model_summary(model)[source]

Save a little summary of the model in a txt file.

Parameters:

model (Module) – Instance of the model for which to save the summary.

Returns:

A tuple with the model summary in a text format and number of parameters of the model.

Return type:

dict[str, int | list[tuple[str, str, int]]]

mednet.scripts.utils.device_properties(device_type)[source]

Generate information concerning hardware properties.

Parameters:

device_type (Literal['cpu', 'cuda', 'mps']) – The type of compute device we are using.

Return type:

dict[str, int | float | str]

Returns:

Static properties of the current machine.

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:

dict[str, int | float | str | dict[str, str] | list[str]]

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 environment

  • path: current path when executing the command

  • command-line: the command-line that is being run

  • hostname: 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: JSONEncoder

Extends the standard JSON encoder to support Numpy arrays.

default(o)[source]

If input object is a ndarray it will be converted into a list.

Parameters:

o (Any) – Input object to be JSON serialized.

Return type:

Any

Returns:

A serializable representation of object o.

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:
Return type:

None

mednet.scripts.utils.save_json_metadata(output_file, **kwargs)[source]

Save prediction hyperparameters into a .json file.

Return type:

None

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:

tuple[str, Literal['min', 'max']]

Returns:

The name of the metric used for saving the best checkpoint and the modality {‘min’, ‘max’} in this exact order.