mednet.scripts.utils

Utilities for command-line scripts.

Module Attributes

JSONable

Defines types that can be encoded in a JSON string.

Functions

device_properties(device_type)

Generate information concerning hardware properties.

execution_metadata()

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

model_summary(model)

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

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

tuple[lightning.pytorch.callbacks.ModelSummary, 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