Import / Export (IO)#

Classes:

PulseSeqFile([file_path, sequence])

API for reading and writing Pulseq definition files

Functions:

sequence_from_json(file)

Loads json file from specified locations and reconstructs a Sequence object from it.

sequence_to_json(sequence[, file_name])

Converts a cmrseq.Sequence object to an ordered dictionary containing a JSON compatible representation of the sequence.

class PulseSeqFile(file_path=None, sequence=None)[source]#

API for reading and writing Pulseq definition files

Methods:

check_add_def(def_tuple, table[, last_id])

Checks if definition tuple is already in self.rf_table (if not adds it to the table) and returns the corresponding rf_id.

check_add_shape(arr)

Checks if specified array is already in self.shape_table (if not adds it to the table) and returns the corresponding shape_id.

from_pulseq_file(filepath)

Loads a *.seq file and parses all sections into the

from_sequence(sequence)

Creates a pulseq-style sequence definition from a cmrseq.sequence object.

to_cmrseq(system_specs[, block_indices])

Converts the parsed file into a list of cmrseq.Sequence objects.

write(filepath)

raises:

ValueError if file at specified location already exists

Attributes:

REQUIRED_DEFINITIONS

Required definitions in [DEFINITIONS] - section

adc_table

Dictionary containing the ADC definition per shape_id as dictionary with following keys: dict_keys=(num_samples, dwell, delay, frequency_offset, phase_offset)

additional_defs

Dictionary containing values specified in the [DEFINITIONS] section

block_array

Integer array (n_blocks, 8) containing the Block-definitions

ext_table

Dictionary containing the extensions definiton

grads_table

Dictionary containing the shape-gradient definitions per shape_id as dictionary dict_keys=(delay, shape_ids=[amp, time], amplitude)

raster_times

Dictionary containing Quantities[Time] for 'grad', 'rf', 'adc' and 'block' raster time

shape_table

Dictionary (id: shape) containing the uncompressed shape definitions

version

Assembled python like version number

Parameters:
  • file_path (str) –

  • sequence (Sequence) –

static check_add_def(def_tuple, table, last_id=None)[source]#

Checks if definition tuple is already in self.rf_table (if not adds it to the table) and returns the corresponding rf_id.

Lookup is performed by computing the hash value of the stringified definition tuple which serves as key of the rf_table dictionary.

Parameters:
  • def_tuple (tuple) – RF definition as specified in the Pulseq package (amp, magnitude_id, phase_id, time_id, delay, frequency_offset, phase_offset)

  • table (dict[slice(<class ‘int’>, <class ‘tuple’>, None)]) – One of the following lookup dictionaries [file.rf_table, file.adc_table, file.trap_table, file.arb_table]

  • last_id (Optional[int]) – if specified, the new entry is inserted at last_id + 1 otherwise the last id is computed as the length of table.

Return type:

int

Returns:

definition id is corresponding table

check_add_shape(arr)[source]#

Checks if specified array is already in self.shape_table (if not adds it to the table) and returns the corresponding shape_id.

Lookup is performed by computing the hash value of the array which serves as key of the shape_table dictionary.

Parameters:

arr (ndarray) –

Return type:

int

Returns:

shape_id int

from_pulseq_file(filepath)[source]#

Loads a *.seq file and parses all sections into the

Raise:

ValueError if file does not exist

Parameters:

filepath (str) – path to a file of type *.seq

from_sequence(sequence)[source]#

Creates a pulseq-style sequence definition from a cmrseq.sequence object.

Parameters:

sequence (Sequence) –

to_cmrseq(system_specs, block_indices=None)[source]#

Converts the parsed file into a list of cmrseq.Sequence objects.

Parameters:
  • gyromagentic_ratio – in MHz/T

  • max_slew – in mT/m

  • max_grad – in mT/m/ms

  • block_indices (Optional[Iterable[int]]) – Iterable[int] specifiying which blocks to convert if None all blocks are converted

  • system_specs (SystemSpec) –

Return type:

List[Sequence]

Returns:

List of cmrseq.Sequence each representing one block of the pulseseq definition

write(filepath)[source]#
Raises:

ValueError if file at specified location already exists

Parameters:

filepath (str) –

Returns:

None

REQUIRED_DEFINITIONS: Tuple[str, ...] = ('AdcRasterTime', 'BlockDurationRaster', 'GradientRasterTime', 'RadiofrequencyRasterTime')#

Required definitions in [DEFINITIONS] - section

adc_table: OrderedDict#

Dictionary containing the ADC definition per shape_id as dictionary with following keys: dict_keys=(num_samples, dwell, delay, frequency_offset, phase_offset)

additional_defs: dict#

Dictionary containing values specified in the [DEFINITIONS] section

block_array: ndarray#

Integer array (n_blocks, 8) containing the Block-definitions

ext_table: OrderedDict#

Dictionary containing the extensions definiton

grads_table: OrderedDict#

Dictionary containing the shape-gradient definitions per shape_id as dictionary dict_keys=(delay, shape_ids=[amp, time], amplitude)

raster_times: dict#

Dictionary containing Quantities[Time] for ‘grad’, ‘rf’, ‘adc’ and ‘block’ raster time

shape_table: OrderedDict#

Dictionary (id: shape) containing the uncompressed shape definitions

version: str#

Assembled python like version number

sequence_from_json(file)[source]#

Loads json file from specified locations and reconstructs a Sequence object from it.

Parameters:

file (str) – str - file location containing the sequence definition

Return type:

Sequence

Returns:

cmrseq.Sequence object

sequence_to_json(sequence, file_name=None)[source]#

Converts a cmrseq.Sequence object to an ordered dictionary containing a JSON compatible representation of the sequence. The first node contains the system specifications and subsequent nodes contain the block definitions. After converting the sequence to a dict, this function serializes it to json format and saves it to the specified location (if not None).

Parameters:
  • sequence (Sequence) –

  • file_name (Optional[str]) – str - defaults to None. Specifies the saving location. If not specified, the sequence representation is not saved but only returned as json compatible dictionary.

Return type:

OrderedDict

Returns: