trx.io#

Unified I/O interface for tractogram file formats.

Attributes#

Functions#

get_trx_tmp_dir()

Return a temporary directory honoring the TRX_TMPDIR setting.

load_sft_with_reference(filepath[, reference, bbox_check])

Load a tractogram as a StatefulTractogram with an explicit reference.

load(tractogram_filename, reference)

Load a tractogram from disk and return a TRX or StatefulTractogram.

save(tractogram_obj, tractogram_filename[, ...])

Save a tractogram object to disk.

Module Contents#

trx.io.dipy_available = True[source]#
trx.io.get_trx_tmp_dir()[source]#

Return a temporary directory honoring the TRX_TMPDIR setting.

When the TRX_TMPDIR environment variable is set to "use_working_dir" the current working directory is used. Otherwise, the value of TRX_TMPDIR is used directly. If the variable is not set, the system temporary directory is used.

Returns:
tempfile.TemporaryDirectory

Context-managed temporary directory placed according to the environment configuration.

trx.io.load_sft_with_reference(filepath, reference=None, bbox_check=True)[source]#

Load a tractogram as a StatefulTractogram with an explicit reference.

Parameters:
filepathstr

Path to the tractogram file (.trk, .tck, .fib, .vtk, .dpy).

referencestr or nibabel.Nifti1Image, optional

Reference image used for formats without embedded affine information. Pass "same" to reuse the header embedded in .trk files.

bbox_checkbool, optional

If True, validate that streamlines lie within the reference bounding box. Defaults to True.

Returns:
StatefulTractogram or None

Loaded tractogram. Returns None when dipy is unavailable.

Raises:
IOError

If the file format is unsupported or a required reference is missing.

trx.io.load(tractogram_filename, reference)[source]#

Load a tractogram from disk and return a TRX or StatefulTractogram.

Parameters:
tractogram_filenamestr

Path to the input tractogram. TRX directories are supported.

referencestr or nibabel.Nifti1Image

Reference image used for formats without embedded affine information.

Returns:
TrxFile or StatefulTractogram

TRX file handle for .trx inputs, otherwise a StatefulTractogram.

trx.io.save(tractogram_obj, tractogram_filename, bbox_valid_check=False)[source]#

Save a tractogram object to disk.

Parameters:
tractogram_objTrxFile or StatefulTractogram

Tractogram to persist. Non-TRX inputs are converted to StatefulTractogram before saving to non-TRX formats.

tractogram_filenamestr

Destination file name. .trx will be saved using the TRX writer; all other extensions are handled by dipy.save_tractogram.

bbox_valid_checkbool, optional

If True, validate that streamlines lie within the reference bounding box when saving non-TRX formats. Defaults to False.

Returns:
None

The function writes to disk and returns None. Returns None immediately when dipy is unavailable.