rtCommon.imageHandling

This script includes all of the functions that are needed (1) to transfer dicom files back and forth from the cloud and (2) to convert the dicom files to nifti files, which is a file format that is better for data analyses.

Module Contents

Functions

getDicomFileName(cfg, scanNum, fileNum)

This function takes in different variables (which are both specific to the specific

anonymizeDicom(dicomImg)

This function takes in the dicom image that you read in and deletes

readDicomFromFile(filename)

This function takes the path/name of the dicom file of interest and reads it.

writeDicomFile(dicomImg, filename)

This function takes a dicomImg and the path/name of the file to write to.

writeDicomToBuffer(dicomImg)

This function write dicom data to binary mode so that it can be transferred

readDicomFromBuffer(data) → pydicom.dataset.FileDataset

This function reads data that is in binary mode and then converts it into a

readRetryDicomFromDataInterface(dataInterface, filename, timeout=5)

This function is waiting and watching for a dicom file to be sent to the cloud

parseDicomVolume(dicomImg, sliceDim)

The raw dicom file coming from the scanner will be a 2-dimensional picture

getDicomAcquisitionTime(dicomImg) → datetime.datetime.time

Returns the acquisition time as a datetime.time

getDicomRepetitionTime(dicomImg) → float

Returns the TR repetition time in seconds

dicomTimeToNextTr(dicomImg, clockSkew, now=None)

Based on Dicom header. Returns seconds to next TR start

bidsIncrementalTimeToNextTr(bidsIncremental, clockSkew, now=None)

Based on BidsIncremental header. Returns seconds to next TR start

getAxesForTransform(startingDicomFile, cfg)

This function takes a single dicom file (which can be the first file) and

getTransform(target_orientation, dicom_orientation)

This function calculates the right transformation needed to go from the original

saveAsNiftiImage(dicomDataObject, fullNiftiFilename, cfg, reference)

This function takes in a dicom data object written in bytes, what you expect

convertDicomFileToNifti(dicomFilename, niftiFilename)

niftiToMem(niftiImg)

Fully load Nifti image into memory and remove any file-backing.

readNifti(niftiFilename, memCached=True)

convertDicomImgToNifti(dicomImg, dicomFilename=None)

Given an in-memory dicomImg, convert it to an in-memory niftiImg.

Attributes

binPath

attributesToAnonymize

rtCommon.imageHandling.binPath
rtCommon.imageHandling.getDicomFileName(cfg, scanNum, fileNum)

This function takes in different variables (which are both specific to the specific scan and the general setup for the entire experiment) to produce the full filename for the dicom file of interest.

Used externally.

rtCommon.imageHandling.attributesToAnonymize = ['PatientID', 'PatientAge', 'PatientBirthDate', 'PatientName', 'PatientSex', 'PatientSize',...
rtCommon.imageHandling.anonymizeDicom(dicomImg)

This function takes in the dicom image that you read in and deletes lots of different attributes. The purpose of this is to anonymize the dicom data before transferring it to the cloud.

Used externally.

rtCommon.imageHandling.readDicomFromFile(filename)

This function takes the path/name of the dicom file of interest and reads it.

Used internally.

rtCommon.imageHandling.writeDicomFile(dicomImg, filename)

This function takes a dicomImg and the path/name of the file to write to.

Used internally.

rtCommon.imageHandling.writeDicomToBuffer(dicomImg)

This function write dicom data to binary mode so that it can be transferred to the cloud, where it again becomes a dicom. This is needed because files are transferred to the cloud in the following manner: dicom from scanner –> binary file –> transfer to cloud –> dicom file

Used internally.

rtCommon.imageHandling.readDicomFromBuffer(data) pydicom.dataset.FileDataset

This function reads data that is in binary mode and then converts it into a structure that can be read as a dicom file. This is necessary because files are transferred to the cloud in the following manner: dicom from scanner –> binary file –> transfer to cloud –> dicom file

Use internally.

rtCommon.imageHandling.readRetryDicomFromDataInterface(dataInterface, filename, timeout=5)

This function is waiting and watching for a dicom file to be sent to the cloud from the scanner. It dodes this by calling the ‘watchFile()’ function in the ‘dataInterface.py’

Used externally (and internally). :param dataInterface: A dataInterface to make calls on :param filename: Dicom filename to watch for and read when available :param timeout: Max number of seconds to wait for file to be available

Returns

The dicom image

rtCommon.imageHandling.parseDicomVolume(dicomImg, sliceDim)

The raw dicom file coming from the scanner will be a 2-dimensional picture made of up multiple image slices that are tiled together. This function separates the image slices to form a single volume.

Used externally.

rtCommon.imageHandling.getDicomAcquisitionTime(dicomImg) datetime.datetime.time

Returns the acquisition time as a datetime.time Note: day, month and year are not specified

rtCommon.imageHandling.getDicomRepetitionTime(dicomImg) float

Returns the TR repetition time in seconds

rtCommon.imageHandling.dicomTimeToNextTr(dicomImg, clockSkew, now=None)

Based on Dicom header. Returns seconds to next TR start

rtCommon.imageHandling.bidsIncrementalTimeToNextTr(bidsIncremental, clockSkew, now=None)

Based on BidsIncremental header. Returns seconds to next TR start

rtCommon.imageHandling.getAxesForTransform(startingDicomFile, cfg)

This function takes a single dicom file (which can be the first file) and the config file to obtain the target_orientation (in nifti space) and the dicom_orientation (in the original space).

NOTE: You only need to run this function once to obtain the target and dicom orientations. You can save and load these variables so that ‘getTransform()’ is hard coded.

Used externally.

rtCommon.imageHandling.getTransform(target_orientation, dicom_orientation)

This function calculates the right transformation needed to go from the original axis space (dicom_orientation) to the target axis space in nifti space (target_orientation).

Used externally.

rtCommon.imageHandling.saveAsNiftiImage(dicomDataObject, fullNiftiFilename, cfg, reference)

This function takes in a dicom data object written in bytes, what you expect the dicom file to be called (we will use the same name format for the nifti file), and the config file while will have (1) the axes transformation for the dicom file and (2) the header information from a reference scan.

Used externally.

rtCommon.imageHandling.convertDicomFileToNifti(dicomFilename, niftiFilename)
rtCommon.imageHandling.niftiToMem(niftiImg)

Fully load Nifti image into memory and remove any file-backing. NiftiImage by default contains a pointer to the image file for the data.

rtCommon.imageHandling.readNifti(niftiFilename, memCached=True)
rtCommon.imageHandling.convertDicomImgToNifti(dicomImg, dicomFilename=None)

Given an in-memory dicomImg, convert it to an in-memory niftiImg. Note: due to how nibabel niftiImage works, it is just a pointer to a file on disk, so we can’t delete the niftiFile while niftiImage is in use.