change_header

Modify DICOM header information to simulate failing QA.

Functions

change_tags(dcm_dir, new_data[, out_dir])

Change specified tags of all dicoms in a directory.

gen_acqdates()

Generate DataElements for random acquisition day and time.

gen_anon()

Make random date of birth, age, and sex.

gen_ids(new_id)

Generate ID DataElements.

main_make_mods()

Exercise header modification code to make example data we can use.

change_header.change_tags(dcm_dir, new_data, out_dir=None)[source]

Change specified tags of all dicoms in a directory. Optionally make copies in out_dir.

sideffect: writes copies of dcm_dir dicoms into out_dir unless out_dir is None.

Parameters:
  • dcm_dir (Path) – input directory with dicom files (MR*, *IMA , or *dcm)

  • new_data (List[DataElement]) – list of data elements to replace like [pydicom.DataElement(value="newpname", VR="LO", tag=(0x0018, 0x1030))]

  • out_dir (Path | None) – Optional. Where to save modified dicoms

Returns:

example modified dicom. last if out_dir, first and only if no out_dir.

Return type:

FileDataset | None

>>> new_data = [pydicom.DataElement(value="newpname", VR="LO", tag=(0x0018, 0x1030))]
>>> ex_path = Path('example_dicoms/')
>>> ex = change_tags(ex_path, new_data)
>>> ex.ProtocolName
'newpname'
change_header.gen_acqdates()[source]

Generate DataElements for random acquisition day and time.

Returns:

ID DataElements List

Return type:

List[DataElement]

See gen_anon() for tag and VR info.

change_header.gen_anon()[source]

Make random date of birth, age, and sex.

Returns:

list of DataElements with randomized values

Return type:

List[DataElement]

Tip

Field tag type and location can be extracted like:

x = pydicom.dcmread(example_fname)
fields = ['AcquisitionDate', 'AcquisitionTime',
          'PatientBirthDate', 'PatientAge', 'PatientSex']
[x[k] for k in fields]

# yields
[(0008,0022) Acquisition Date                    DA: '20221222',
 (0008,0032) Acquisition Time                    TM: '092132.722500',
 (0010,0030) Patient's Birth Date                DA: '20070404',
 (0010,1010) Patient's Age                       AS: '015Y',
 (0010,0040) Patient's Sex                       CS: 'F']

Where the tag is the tuple and “XX:” is the type

change_header.gen_ids(new_id)[source]

Generate ID DataElements.

Parameters:

new_id (str) – id string to put into pat name and pat id dicom headers.

Returns:

ID DataElements List

Return type:

List[DataElement]

See gen_anon() for tag and VR info.

>>> data_els = gen_ids('example_name')
>>> data_els[0].value
'example_name'
>>> data_els[0].VR
'PN'
>>> data_els[0].tag
(0010, 0010)
change_header.main_make_mods()[source]

Exercise header modification code to make example data we can use.

We can confirm changes are made from shell using AFNI’s dicom_hinfo

find example/dicom/mod* -iname 'MR*' -exec dicom_hinfo -tag 0010,0010 -sepstr $'\t' -last {} \+

#   mod1 example/dicom/mod1/HabitTask/MR.1.3.12.2.1107.5.2.43.167046.2022122209214150118864465
#   mod1 example/dicom/mod1/HabitTask/MR.1.3.12.2.1107.5.2.43.167046.2022122209214176799264617
#   mod2 example/dicom/mod2/HabitTask/MR.1.3.12.2.1107.5.2.43.167046.2022122209214150118864465
#   mod2 example/dicom/mod2/HabitTask/MR.1.3.12.2.1107.5.2.43.167046.2022122209214176799264617