read

Contents

read#

pypulseqpp.io.read()[source]#

Read a Pulseq file into a sequence whose system describes what is in it.

Unlike read(), which keeps the system the sequence was constructed with, this builds the system from the file, so that the checks of pypulseqpp.safety compare the sequence with its own limits. Text and binary files are detected from their contents.

The rasters come from the file. The gradient limit, slew-rate limit and B0 come from the MaxGrad, MaxSlew and B0 definitions when the file states them, and from max_grad and max_slew when given, which take precedence. A limit that neither supplies is the larger of the Opts default and the largest value the file reaches. Dead times are not recorded in a .seq file and keep their defaults.

Parameters:
  • file_path (str | os.PathLike[str] | BinaryIO) – The file to read, or a binary file object whose read() returns its contents.

  • max_grad (float, default=None) – Gradient amplitude limit (Hz/m) for the system built. None takes the file’s own, or the larger of the default and what the file reaches.

  • max_slew (float, default=None) – Slew-rate limit (Hz/m/s), resolved the same way.

  • detect_rf_use (bool, default=False) – Assign a use to each RF event without one, as detect_rf_use() does. Files before revision 1.5.0 record no use.

  • remove_duplicates (bool, default=True) – Collapse identical library rows after reading.

  • verify (bool, default=False) – Check the file against the signature it carries; a file without one is refused.

Returns:

The sequence, with system built from the file.

Return type:

pypulseqpp.Sequence

Raises:
  • FileNotFoundError – If file_path does not exist.

  • RuntimeError – If the file cannot be parsed, or verify is set and the signature does not match.

  • TypeError – If a file object returns text, as one opened in text mode does.

See also

write

The other direction.

pypulseqpp.Sequence.read

Read into an existing sequence, keeping its system.

Examples

>>> import pathlib, tempfile
>>> import pypulseqpp as pp
>>> path = pathlib.Path(tempfile.mkdtemp()) / "gre.seq"
>>> system = pp.Opts(max_grad=80.0, grad_unit="mT/m")
>>> seq = pp.Sequence(system)
>>> seq.add_block(pp.make_trapezoid("x", area=4000, duration=2e-3, system=system))
1
>>> _ = pp.io.write(seq, path)

The file states no limit, so the one built admits what the file reaches, which is more than the default system allows:

>>> loaded = pp.io.read(path)
>>> loaded.num_blocks, loaded.system.max_grad > pp.Opts().max_grad
(1, True)