Utility

Utility objects and functions

class heudiconv.utils.File(name, executable=False)

Helper for a file entry in the create_tree/@with_tree

It allows to define additional settings for entries

class heudiconv.utils.SeqInfo(total_files_till_now, example_dcm_file, series_id, dcm_dir_name, series_files, unspecified, dim1, dim2, dim3, dim4, TR, TE, protocol_name, is_motion_corrected, is_derived, patient_id, study_description, referring_physician_name, series_description, sequence_name, image_type, accession_number, patient_age, patient_sex, date, series_uid, time)
TE

Alias for field number 11

TR

Alias for field number 10

accession_number

Alias for field number 21

date

Alias for field number 24

dcm_dir_name

Alias for field number 3

dim1

Alias for field number 6

dim2

Alias for field number 7

dim3

Alias for field number 8

dim4

Alias for field number 9

example_dcm_file

Alias for field number 1

image_type

Alias for field number 20

is_derived

Alias for field number 14

is_motion_corrected

Alias for field number 13

patient_age

Alias for field number 22

patient_id

Alias for field number 15

patient_sex

Alias for field number 23

protocol_name

Alias for field number 12

referring_physician_name

Alias for field number 17

sequence_name

Alias for field number 19

series_description

Alias for field number 18

series_files

Alias for field number 4

series_id

Alias for field number 2

series_uid

Alias for field number 25

study_description

Alias for field number 16

time

Alias for field number 26

total_files_till_now

Alias for field number 0

unspecified

Alias for field number 5

class heudiconv.utils.StudySessionInfo(locator, session, subject)
locator

Alias for field number 0

session

Alias for field number 1

subject

Alias for field number 2

class heudiconv.utils.TempDirs

A helper to centralize handling and cleanup of dirs

heudiconv.utils.anonymize_sid(sid, anon_sid_cmd)
Raises:ValueError – if script returned an empty string (after whitespace stripping), or output with multiple words/lines.
heudiconv.utils.assure_no_file_exists(path)

Check if file or symlink (git-annex?) exists, and if so – remove

heudiconv.utils.clear_temp_dicoms(item_dicoms)

Ensures DICOM temporary directories are safely cleared

heudiconv.utils.create_file_if_missing(filename, content)

Create file if missing, so we do not override any possibly introduced changes

heudiconv.utils.create_tree(path, tree, archives_leading_dir=True)

Given a list of tuples (name, load) or a dict create such a tree

if load is a tuple or a dict itself – that would create either a subtree or an archive with that content and place it into the tree if name ends with .tar.gz

heudiconv.utils.docstring_parameter(*sub)

Borrowed from https://stackoverflow.com/a/10308363/6145776

heudiconv.utils.get_datetime(date, time, *, microseconds=True)

Combine date and time from dicom to isoformat.

Parameters:
  • date (str) – Date in YYYYMMDD format.
  • time (str) – Time in either HHMMSS.ffffff format or HHMMSS format.
  • microseconds (bool, optional) – Either to include microseconds in the output
Returns:

datetime_str – Combined date and time in ISO format, with microseconds as if fraction was provided in ‘time’, and ‘microseconds’ was True.

Return type:

str

heudiconv.utils.get_known_heuristic_names()

Return a list of heuristic names present under heudiconv/heuristics

heudiconv.utils.get_typed_attr(obj, attr, _type, default=None)

Typecasts an object’s named attribute. If the attribute cannot be converted, the default value is returned instead.

Parameters:
  • obj (Object) –
  • attr (Attribute) –
  • _type (Type) –
  • default (value, optional) –
heudiconv.utils.is_readonly(path)

Return True if it is a fully read-only file (dereferences the symlink)

heudiconv.utils.json_dumps(json_obj, indent=2, sort_keys=True)

Unified (default indent and sort_keys) invocation of json.dumps

heudiconv.utils.json_dumps_pretty(j, indent=2, sort_keys=True)

Given a json structure, pretty print it by colliding numeric arrays into a line.

If resultant structure differs from original – throws exception

heudiconv.utils.load_heuristic(heuristic)

Load heuristic from the file, return the module

heudiconv.utils.load_json(filename, retry=0)

Load data from a json file

Parameters:
  • filename (str) – Filename to load data from.
  • retry (int, optional) – Number of times to retry opening/loading the file in case of failure. Code will sleep for 0.1 seconds between retries. Could be used in code which is not sensitive to order effects (e.g. like populating bids templates where the last one to do it, would make sure it would be the correct/final state).
Returns:

data

Return type:

dict

heudiconv.utils.remove_prefix(s, pre)

Remove prefix from the beginning of the string

Parameters:
  • s (str) –
  • pre (str) –
Returns:

s – string with “pre” removed from the beginning (if present)

Return type:

str

heudiconv.utils.remove_suffix(s, suf)

Remove suffix from the end of the string

Parameters:
  • s (str) –
  • suf (str) –
Returns:

s – string with “suf” removed from the end (if present)

Return type:

str

heudiconv.utils.safe_copyfile(src, dest, overwrite=False)

Copy file but blow if destination name already exists

heudiconv.utils.safe_movefile(src, dest, overwrite=False)

Move file but blow if destination name already exists

heudiconv.utils.save_json(filename, data, indent=2, sort_keys=True, pretty=False)

Save data to a json file

Parameters:
  • filename (str) – Filename to save data in.
  • data (dict) – Dictionary to save in json file.
  • indent (int, optional) –
  • sort_keys (bool, optional) –
  • pretty (bool, optional) –
heudiconv.utils.set_readonly(path, read_only=True)

Make file read only or writeable while preserving “access levels”

So if file was not readable by others, it should remain not readable by others.

Parameters:
  • path (str) –
  • read_only (bool, optional) – If True (default) - would make it read-only. If False, would make it writeable for levels where it is readable
heudiconv.utils.slim_down_info(j)

Given an aggregated info structure, removes excessive details

Such as CSA fields, and SourceImageSequence which on Siemens files could be huge and not providing any additional immediately usable information. If needed, could be recovered from stored DICOMs

heudiconv.utils.treat_infofile(filename)

Tune up generated .json file (slim down, pretty-print for humans).

heudiconv.utils.update_json(json_file, new_data, pretty=False)

Adds a given field (and its value) to a json file

Parameters:
  • json_file (str or Path) – path for the corresponding json file
  • new_data (dict) – pair of “key”: “value” to add to the json file
  • pretty (bool) – argument to be passed to save_json