Tools and helper modules
Get values
Tiny modules to reduce repetition in the main code when extracting information from DICOM headers using pydicom.
- openrem.remapp.tools.get_values.get_keys_by_value(dict_of_elements, value_to_find)
Get a list of keys from a dictionary which have the given value :param dict_of_elements: a dictionary of elements :param value_to_find: the value to look for in the dictionary :return: list of key names matching the given value
- openrem.remapp.tools.get_values.get_or_create_cid(codevalue, codemeaning)
Create a code_value code_meaning pair entry in the ContextID table if it doesn’t already exist.
- Parameters:
codevalue (int.) – Code value as defined in the DICOM standard part 16
codemeaning – Code meaning as defined in the DICOM standard part 16
- Returns:
ContextID entry for code value passed
- openrem.remapp.tools.get_values.get_seq_code_meaning(sequence, dataset)
From a DICOM sequence, get the code meaning.
- Parameters:
sequence (DICOM keyword, no spaces or plural as per dictionary.) – DICOM sequence name.
dataset (DICOM dataset) – The DICOM dataset containing the sequence.
- Returns:
str. – code meaning
- openrem.remapp.tools.get_values.get_seq_code_value(sequence, dataset)
From a DICOM sequence, get the code value.
- Parameters:
sequence (DICOM keyword, no spaces or plural as per dictionary.) – DICOM sequence name.
dataset (DICOM dataset) – The DICOM dataset containing the sequence.
- Returns:
int. – code value
- openrem.remapp.tools.get_values.get_value_kw(tag, dataset)
Get DICOM value by keyword reference.
- Parameters:
tag (str.) – DICOM keyword, no spaces or plural as per dictionary.
dataset (dataset) – The DICOM dataset containing the tag.
- Returns:
str. – value
- openrem.remapp.tools.get_values.get_value_num(tag, dataset)
Get DICOM value by tag group and element number.
Always use get_value_kw by preference for readability. This module can be required when reading private elements.
- Parameters:
tag (hex) – DICOM group and element number as a single hexadecimal number (prefix 0x).
dataset (dataset) – The DICOM dataset containing the tag.
- Returns:
str. – value
- openrem.remapp.tools.get_values.list_to_string(dicom_value)
Turn multivalue names into a single string for correct encoding and pretty reproduction :param dicom_value: returned DICOM value, usually a name field. Might be single (string) or multivalue (list) :return: string of name(s)
- openrem.remapp.tools.get_values.return_for_export(model, field)
Prevent errors due to missing data in models :param model: database table :param field: database field :return: value or None
- openrem.remapp.tools.get_values.test_numeric_value(string_number)
Tests if string can be converted to a float. If it can, return it :param string_number: string to test if is a number :return: string if number, nothing otherwise
- openrem.remapp.tools.get_values.to_decimal_value(string_number)
Tests if string can be converted to a float. If yes returns it as decimal. :param string_number: string to test if a number :return: Decimal if convertable, None otherwise
Check if UID exists
Small module to check if UID already exists in the database.
- openrem.remapp.tools.check_uid.check_uid(uid, level='Study')
Check if UID already exists in database.
- Parameters:
uid (str.) – Study UID.
- Returns:
1 if it does exist, 0 otherwise
- openrem.remapp.tools.check_uid.record_sop_instance_uid(study, sop_instance_uid)
Record the object’s SOP Instance UID so we can ignore it next time. If an object does need to be imported again, the original one needs to be deleted first.
- Parameters:
study – GeneralStudyModuleAttr database object
sop_instance_uid – SOP Instance UID of object being imported
- Returns:
DICOM time and date values
Module to convert betweeen DICOM and Python dates and times.
- openrem.remapp.tools.dcmdatetime.get_date(tag, dataset)
Get DICOM date string and return Python date.
- Parameters:
tag (str.) – DICOM keyword, no spaces or plural as per dictionary.
dataset (dataset) – The DICOM dataset containing the tag.
- Returns:
Python date value
- openrem.remapp.tools.dcmdatetime.get_date_time(tag, dataset)
Get DICOM date time string and return Python date time.
- Parameters:
tag (str.) – DICOM keyword, no spaces or plural as per dictionary.
dataset (dataset) – The DICOM dataset containing the tag.
- Returns:
Python date time value
- openrem.remapp.tools.dcmdatetime.get_time(tag, dataset)
Get DICOM time string and return Python time.
- Parameters:
tag (str.) – DICOM keyword, no spaces or plural as per dictionary.
dataset (dataset) – The DICOM dataset containing the tag.
- Returns:
python time value
- openrem.remapp.tools.dcmdatetime.make_date(dicomdate)
Given a DICOM date, return a Python date.
- Parameters:
dicomdate (str.) – DICOM style date.
- Returns:
Python date value
- openrem.remapp.tools.dcmdatetime.make_date_time(dicomdatetime)
Given a DICOM date time, return a Python date time.
- Parameters:
dicomdate (str.) – DICOM style date time.
- Returns:
Python date time value
- openrem.remapp.tools.dcmdatetime.make_dcm_date(pythondate)
Given a Python date, return a DICOM date :param pythondate: Date :type pythondate: Python date object :returns: DICOM date as string
- openrem.remapp.tools.dcmdatetime.make_dcm_date_range(date1=None, date2=None, single_date=False)
Given one or two dates of the form yyyy-mm-dd, return a DICOM date range.
- Parameters:
date1 – Date from, string, yyyy-mm-dd, 1900-01-01 if None or badly formatted
date2 – Date until, string, yyyy-mm-dd, today if None or badly formatted
single_date – Single date range, bool, default False
- Returns:
DICOM formatted date range or single date
- openrem.remapp.tools.dcmdatetime.make_dcm_time(python_time)
Return DICOM formatted time without seconds from python time
- Parameters:
python_time – Python datetime.time object
- Returns:
string, %H%M
- openrem.remapp.tools.dcmdatetime.make_dcm_time_range(time1=None, time2=None)
Given one or two times of the format 0123, return DICOM formatted time range (without seconds)
- Parameters:
time1 – time, format 0123, 0000 if None
time2 – time, format 0123, 2359 if None
- Returns:
time range, string, format 0123-1234
- openrem.remapp.tools.dcmdatetime.make_time(dicomtime)
Given a DICOM time, return a Python time.
- Parameters:
dicomdate (str.) – DICOM style time.
- Returns:
Python time value