Code Documentation

qmenta.sdk.context

class qmenta.sdk.context.AnalysisContext(analysis_id, comm)

Bases: object

get_files(input_id, modality=None, tags=None, reg_expression=None, file_filter_condition_name=None)

Returns the files in the input container specified by input_id that match the modality, tags and regular expression, if any. (X)OR that are selected by the file filter condition defined in the tool settings.

Parameters
  • input_id (str) – Identifier “id” of the input container in advanced settings.

  • modality (str, optional) – Optional modality, will allow any modalities if None given. Default is None.

  • tags (set, optional) – Optional set of tags, will allow any tags if None given, and will match any set of tags (including the empty set) otherwise. Default is None.

  • reg_expression (str, optional) – Regular expression to match with the file names in the specified container, will allow any file names if None given. Default is None.

  • file_filter_condition_name (str, optional) – File filter specified in the settings of the tool. Default is None.

Examples

Get all files that passed c_TEST (including any selection the user may have done using the GUI)

>>> context.get_files('input', file_filter_condition_name='c_TEST')

Get all files that passed c_TEST and have no tags

>>> context.get_files('input', file_fitler_condition_name='c_TEST', tags=set()

Get all files that passed c_TEST and have the DTI tag in their list of tags

>>> context.get_files('input', file_fitler_condition_name='c_TEST', tags={'DTI'})

Get all files that passed c_TEST whose name match the given regexp

>>> context.get_files('input', file_filter_condition_name='c_TEST', reg_expression='^some_expression[0-9]')

Alternatively, you can use no file_filter_condition_name to browse through the whole input container

>>> context.get_files('input')
Returns

List of selected file handlers from the container.

Return type

list of qmenta.sdk.context.File

upload_file(self, source_file_path, destination_path, modality=None, tags=None, file_info=None, file_format=None)

Upload a file to the platform, to be part of the result files.

Parameters
  • source_file_path (str) – Path to the file to be uploaded.

  • destination_path (str) – Path in the output container. Will be shown in the platform.

  • modality (str, optional) – Optional modality of the uploaded file. None by default.

  • tags (set, optional) – Set of tags for the uploaded file. None by default.

  • file_info (dict, optional) – File information metadata.

  • file_format (str, optional) – Use “dicom” when .zip file with .dcm slices (uplading a “nifti” file is automatically recognized as such).

class QCEntity

Bases: enum.Enum

Enum with the following options: ANALYSIS, SESSION

ANALYSIS = 'analysis'
SESSION = 'patients'
class QCStatus

Bases: enum.Enum

Enum with the following options: FAIL, PASS

PASS = 'pass'
FAIL = 'fail'
fetch_analysis_data()

Fetch information about current analysis.

Returns

Dictionary that contains important information about the current analysis, such as:
  • state (str) : State of the analysis

  • name (str) : Name of the analysis

  • script_name (str) : Name of the script

  • analysis_id (str) : A unique identifier for the current analysis

  • user_id (str) : Username of the user that started the analysis

  • patient_secret_name (str) : Name of the subject

  • ssid (str) : Timepoint identifier

  • settings (dict) : Settings dictionary. See documentation in method get_settings

  • tags (list) : List of tags

  • version (str) : Tool version

Return type

dict

fetch_parent_analysis_data()

Fetch information about the parent analysis of the current analysis.

Returns

Dictionary that contains important information about the current analysis, such as:
  • state (str) : State of the analysis

  • name (str) : Name of the analysis

  • script_name (str) : Name of the script

  • user_id (str) : Username of the user that started the analysis

  • patient_secret_name (str) : Name of the subject

  • ssid (str) : Timepoint identifier

  • settings (dict) : Settings dictionary. See documentation in method get_settings

  • tags (list) : List of tags

  • version (str) : Tool version

Return type

dict

get_communication_object()

Return the communication object used for communication with the platform

Returns

The communication object used to communicate the platform

Return type

CommunicationObject

set_qc_status(status=<QCStatus.PASS: 'pass'>, comments='', entity=<QCEntity.ANALYSIS: 'analysis'>, input_id=None)

Changes the analysis or session QC status.

Parameters
  • status (QCStatus) – QCStatus.PASS or QCStatus.FAIL

  • comments (str, optional) – Additional comments explaining why the QC status has been set to pass or fail.

  • entity (QCEntity, optional) – QCEntity.ANALYSIS or QCEntity.SESSION

  • input_id (str, optional) – The id of the session in the settings definition when entity is QCEntity.SESSION

set_progress(message=None, value=None)

Sets analysis progress.

Parameters
  • value (int, optional) – Number between 0 and 100 indicating the current status of the execution

  • message (str, optional) – Progress message.

get_settings()

Analysis settings.

Returns

Settings for the current analysis. This includes all the parameters defined in the tool specification (checkboxes, input fields, etc.) and can be accessed using their identifier.

Return type

dict

download_resource(resource_path, destination_path)

Downloads a file from the user/group tool resources. The resource path can include subdirectories and must be relative:

>>> context.download_resource('dir/subdir/file.nii.gz', '/root/file.nii.gz')
Parameters
  • path (str) – Path to the file in the resources bucket.

  • destination_file_path (str) – Path where the file will be downloaded.

set_metadata_value(key, value, title=None, readonly=False)

Sets the value of a metadata parameter.

Parameters
  • key (str) – The ID of the metadata parameter.

  • value (int, ,str, float or list) – The new content of the parameter.

  • title (str, optional) – How the metadata field should be presented

  • readonly (bool, optional) – Whether the user should be able to edit the value on the platform or not (only by analyses)

get_metadata_value(key)

Gets the value of a metadata parameter for the current session

Parameters

key (str) – The ID of the metadata parameter.

Returns

Return type

The value of the parameter if it exists, None otherwise

class qmenta.sdk.context.File(container_id, name, metadata, tags, comm)

Bases: object

get_file_modality()

Get the modality of the file.

Returns

Modality of the file or None if not known.

Return type

str or None

get_file_format()

Get the format of the file (e.g. ‘nifti’, ‘txt’, etc.).

Returns

File format or None if not known.

Return type

str

get_file_info()

Get the file information. The type of information depends on the type of file (e.g. nifti files include information such as ‘Data strides’, ‘Data type’, ‘Dimensions’ or ‘Voxel size’).

Returns

Dictionary of key-value pairs that depend on the format of the file.

Return type

dict

get_file_tags()

Get the file tags.

Returns

Set of tags associated to this file

Return type

set

get_file_path()

Get the file download path.

Returns

The path of the file where it has been downloaded

Return type

str

Raises

RuntimeError – If get_file_path is called before the file has been downloaded

download(dest_path, unpack=True)

Downloads a file or the contents of a packed file to to the specified path.

Parameters
  • dest_path – Path where the file should be downloaded to.

  • unpack – Tells the function whether the file should be unpacked to the given folder.

Returns

The full path of the file or the folder with the unpacked files.

Return type

str