skrt.application module

Classes and functions defining a framework for analysing patient datasets.

The following classes are defined:

  • Algorithm: Base class for analysis algorithms.

  • Application: Class that represents an application as a sequence of algorithms, and manages algorithm execution.

  • Status : Class that represents the exit status of an algorithm or application.

This module provides for construction of an analysis application as a sequence of algorithms, each of which inherits from the Algorithm base class. This defines three methods relating to execution:

  • initialise() - run before any patient data are read;

  • execute() - run once for each patient;

  • finalise() - run after all patient data have been read.

Each method returns an instance of the Status class, providing information on whether execution problems were encountered. Running of algorithm methods is handled by the Application class.

This module also provides the following function that may be useful within an application:

  • get_paths() - get list of paths to patient datasets.

class skrt.application.Algorithm(opts=None, name=None, log_level=None)

Bases: object

Base class for analysis algorithms.

Each analysis algorithm should inherit from Algorithm, overwriting as needed the methods initialise, execute, finalise, to implement analysis-specific functionality.

Methods:

  • __init__(): Create instance of Algorithm class.

  • initialise(): Perform tasks required before considering any patients.

  • execute(): Perform tasks required for each patient.

  • finalise(): Perform tasks required after considering all patients.

  • set_attributes(): Set values for algorithm attributes.

__init__(opts=None, name=None, log_level=None)

Create instance of Algorithm class.

Parameters:

opts: dict, default=None

Dictionary for setting algorithm attributes. If null, set to empty dictionary.

name: str, default=None

Name for identifying algorithm instance. If null, set to class name.

log_level: str/int/None, default=None

Severity level for event logging. If the value is None, log_level is set to the value of skrt.core.Defaults().log_level.

execute(patient=None)

Perform tasks required for each patient.

Parameter:

patient: skrt.patient.Patient/None, default=None

Object providing access to patient information.

finalise()

Perform tasks required after considering all patients.

initialise()

Perform tasks required before considering any patients.

set_attributes(opts)

Set values for algorithm attributes.

Parameter:

opts: dict, default={}

Dictionary for setting algorithm attributes.

class skrt.application.Application(algs=None, log_level=None)

Bases: object

Class that represents an application as a sequence of algorithms, and manages algorithm execution.

Methods:

  • __init__(): Create instance of Application class.

  • initialise(): Call each algorithm’s initialise method.

  • execute(): For each patient, call each algorithm’s execute method.

  • finalise(): Call each algorithm’s finalise method.

  • run(): Initialise analysis, process patient data, finalise analysis.

Class method:

  • from_alg_specs() Create instance of Application class from specifications of the constituent algorithms.

__init__(algs=None, log_level=None)

Create instance of Application class.

Parameters:

algs: list, default=None

List of algorithms to be managed. Algorithms are processed in the order in which they are specified in the list. If null, set to empty list.

log_level: str/int/None, default=None

Severity level for event logging. If the value is None, log_level is set to the value of skrt.core.Defaults().log_level.

execute(patient=None)

For each patient, call each algorithm’s execute method.

Parameter:

patient: skrt.patient.Patient/None, default=None

Object providing access to patient information.

finalise()

Call each algorithm’s finalise method.

classmethod from_alg_specs(alg_specs=(), log_level=None)

Create instance of Application class from specifications of the constituent algorithms.

Parameters:

alg_specs: tuple, default=()

Tuple of five-element tuples. The elements of each inner tuple provide information from which to create an instance of an Algorithm subclass:

  • path to the module where the Algorithm subclass is defined;

  • name of Algorithm subclass;

  • value to pass as <opts>, <name>, <log_level> to the constructor of the Algorithm subclass.

log_level: str/int/None, default=None

Severity level for event logging. If the value is None, log_level is set to the value of skrt.core.Defaults().log_level.

initialise()

Call each algorithm’s initialise method.

run(paths=None, patient_cls=None, **kwargs)

Initialise analysis, process patient data, finalise analysis.

Parameters:

paths: list, default=None

List of paths to folders containing patient data. If null, set to empty list.

patient_cls: class, default=None

Class to be used to create objects representing patient datasets. The class constructor must have a parameter <path>, which will be passed, one by one, the elements of <paths>. The class constructor may have any number of additional parameters, values for which can be passed via <**kwargs>. If a null value is given, the class used is skrt.patient.Patient.

**kwargs:

Keyword arguments that will be passed to the <PatientClass> constructor.

class skrt.application.Status(code=0, name=None, reason=None)

Bases: object

Class that represents the exit status of an algorithm or application.

Methods:

  • __init__(): Create instance of Status class.

  • copy_attributes(): Copy attributes from another Status object.

  • is_ok(): Return boolean, indicating whether status is okay.

__init__(code=0, name=None, reason=None)

Create instance of Status class.

Instances of the Status class are intended to be returned by algorithm methods, with attributes set to indicate exit status.

Parameters:

code: int, default=0

Value for exit status.

name: str, default=None

Name for exit status. If null, set to empty string.

reason: str, default=None

Reason for exit status. If null, set to empty string.

__repr__()

Print status information.

copy_attributes(status)

Copy attributes from another Status object.

Parameter:

statusskrt.application.Status

Status object from which to copy attributes.

is_ok()

Return boolean indicating whether status is okay (exit code non-zero).

skrt.application.get_paths(data_locations=None, max_path=None, to_keep=None, to_exclude=None, pathlib=False)

Get list of paths to patient datasets.

Parameter:

data_locationspathlib.Path/str/list/dict, default=None

Specification of how to identify paths to patient datasets:

  • string or pathlib.Path, optionally including wildcards, giving path to a single patient dataset, or matching paths with multiple multiple datasets;

  • list of strings or pathlib.Path objects, optionally including wildcards, giving or matching paths to patient datasets;

  • dictionary where keys are paths to directories containing patient datasets, and values are strings, or lists of strings, indicating patterns to be matched.

max_pathint, default=None

Maximum number of paths to return. If None, return paths to all directories in data directory (currently hardcoded in this function).

to_keepstr/list, default=None

Patient identifier(s), specified directly or via paths where basenames are taken identifiers. If non-null, <to_keep> is interpreted using skrt.core.get_basenames(). Dataset paths are then returned only for the resulting list of identifiers.

to_excludestr/list, default=None

Patient identifier(s), specified directly or via paths where basenames are taken as identifiers. If non-null, <to_exclude> is interpreted using skrt.core.get_basenames(). Dataset paths are then returned excluding the resulting list of identifiers. If an identifier is specified as both <to_keep> and <to_exclude>, the latter takes precedence.

pathlib: bool, default=False

If False, return paths as strings. If True, return paths as pathlib.Path objects.