rtCommon.dataInterface

DataInterface is a client interface (i.e. for the experiment script running in the cloud) that provides data access, such as reading and writing files.

To support RPC calls from the client, there will be two instances of dataInterface, one at the cloud projectServer which is a stub to forward requests (started with dataRemote=True), and another at the control room computer, run as a service and with dataRemote=False.

When not using RPC, i.e. when the projectServer is run without –dataRemote, there will be only one instance of dataInterface, as part of the projectServer with dataRemote=False.

Module Contents

Classes

DataInterface

Provides functions for accessing remote or local files depending on if dateRemote flag is

Functions

uploadFilesFromList(→ None)

Copies files in fileList from the remote onto the system where this call is being made.

downloadFilesFromList(→ None)

Copies files in fileList from this computer to the remote.

uploadFolderToCloud(→ None)

Copies a folder (directory) from the remote to the system where this call is run

uploadFilesToCloud(dataInterface, srcFilePattern, ...)

Copies files matching (regex) srcFilePattern from the remote onto the system

downloadFolderFromCloud(→ None)

Copies a directory from the system where this call is made to the remote system.

downloadFilesFromCloud(→ None)

Copies files matching srcFilePattern from the system where this call is made

class rtCommon.dataInterface.DataInterface(dataRemote: bool = False, allowedDirs: List[str] = None, allowedFileTypes: List[str] = None, scannerClockSkew: float = 0)

Bases: rtCommon.remoteable.RemoteableExtensible

Provides functions for accessing remote or local files depending on if dateRemote flag is set true or false.

If dataRemote=True, then the RemoteExtensible parent class takes over and forwards all requests to a remote server via a callback function registered with the RemoteExtensible object. In that case none of the methods below will be locally invoked.

If dataRemote=False, then the methods below will be invoked locally and the RemoteExtensible parent class is inoperable (i.e. does nothing).

__del__()
initScannerStream(imgDir: str, filePattern: str, minFileSize: int, anonymize: bool = True, demoStep: int = 0) int

Initialize a data stream context with image directory and filepattern. Once the stream is initialized call getImageData() to retrieve image data. NOTE: currently only one stream at a time is supported.

Parameters
  • imgDir – the directory where the images are or will be written from the MRI scanner.

  • filePattern – a pattern of the image file names that has a TR tag which will be used to index the images, for example ‘scan01_{TR:03d}.dcm’. In this example a call to getImageData(imgIndex=6) would look for dicom file ‘scan01_006.dcm’.

  • minFileSize – Minimum size of the file to return (continue waiting if below this size)

  • anonymize – Whether to remove participant specific fields from the Dicom header

Returns

An identifier used when calling getImageData()

Return type

streamId

getImageData(streamId: int, imageIndex: int = None, timeout: int = 5) pydicom.dataset.FileDataset

Get data from a stream initialized with initScannerStream

Parameters
  • streamId – Id of a previously opened stream.

  • imageIndex – Which image from the stream to retrieve. If left blank it will retrieve the next image in the stream (next after either the last request or starting from 0 if no previous requests)

  • timeout – Max number of seconds to wait for image data to be available

Returns

The bytes array representing the image data returns pydicom.dataset.FileDataset

getFile(filename: str) bytes

Returns a file’s data immediately or fails if the file doesn’t exist.

getNewestFile(filepattern: str) bytes

Searches for files matching filePattern and returns the data from the newest one.

initWatch(dir: str, filePattern: str, minFileSize: int, demoStep: int = 0) None

Initialize a watch directory for files matching filePattern.

No data is returned by this function, but a filesystem watch is established. After calling initWatch, use watchFile() to watch for a specific file’s arrival.

Parameters
  • dir – Directory to watch for arrival (creation) of new files

  • filePattern – Regex style filename pattern of files to watch for (i.e. *.dcm)

  • minFileSize – Minimum size of the file to return (continue waiting if below this size)

  • demoStep – Minimum interval (in seconds) to wait before returning files. Useful for demos replaying existing files while mimicking original timing.

watchFile(filename: str, timeout: int = 5) bytes

Watches for a specific file to be created and returns the file data.

InitWatch() must be called first, before watching for specific files. If filename includes the full path, the path must match that used in initWatch().

Parameters
  • filename – Filename to watch for

  • timeout – Max number of seconds to wait for file to be available

Returns

The file data

putFile(filename: str, data: Union[str, bytes], compress: bool = False) None

Create a file (filename) and write the bytes or text to it. In remote mode the file is written at the remote.

Parameters
  • filename – Name of file to create

  • data – data to write to the file

  • compress – Whether to compress the data in transit (not within the file), only has affect in remote mode.

listFiles(filepattern: str) List[str]

Lists files matching the regex filePattern

listDirs(dirpattern: str) List[str]

Lists directories matching the regex filePattern

getAllowedFileTypes() List[str]

Returns the list of file extensions which are allowed for read and write

getClockSkew(callerClockTime: float, roundTripTime: float) float

Returns the clock skew between the caller’s computer and the scanner clock. This function is assumed to be running in the scanner room and have adjustments to translate this server’s clock to the scanner clock. Value returned is in seconds. A positive number means the scanner clock is ahead of the caller’s clock. The caller should add the skew to their localtime to get the time in the scanner’s clock. :param callerClockTime - current time: :type callerClockTime - current time: secs since epoch :param roundTripTime - measured RTT in seconds to remote caller:

Returns

Clockskew - seconds the scanner’s clock is ahead of the caller’s clock

ping() float

Returns seconds since the epoch

_checkAllowedDirs(dir: str) bool
_checkAllowedFileTypes(filename: str) bool

Class-private function for checking if a file is allowed.

_filterFileList(fileList: List[str]) List[str]

Class-private funtion to filter a list of files to include only allowed ones. Args: fileList - list of files to filter Returns: filtered fileList - containing only the allowed files

rtCommon.dataInterface.uploadFilesFromList(dataInterface, fileList: List[str], outputDir: str, srcDirPrefix=None) None

Copies files in fileList from the remote onto the system where this call is being made.

rtCommon.dataInterface.downloadFilesFromList(dataInterface, fileList: List[str], outputDir: str, srcDirPrefix=None) None

Copies files in fileList from this computer to the remote.

rtCommon.dataInterface.uploadFolderToCloud(dataInterface, srcDir: str, outputDir: str) None

Copies a folder (directory) from the remote to the system where this call is run

rtCommon.dataInterface.uploadFilesToCloud(dataInterface, srcFilePattern: str, outputDir: str)
Copies files matching (regex) srcFilePattern from the remote onto the system

where this call is being made.

rtCommon.dataInterface.downloadFolderFromCloud(dataInterface, srcDir: str, outputDir: str, deleteAfter=False) None

Copies a directory from the system where this call is made to the remote system.

rtCommon.dataInterface.downloadFilesFromCloud(dataInterface, srcFilePattern: str, outputDir: str, deleteAfter=False) None
Copies files matching srcFilePattern from the system where this call is made

to the remote system.