|
| 1 | +import os |
| 2 | +import warnings |
| 3 | +from ..externals.six import string_types |
| 4 | + |
| 5 | +from .header import Field |
| 6 | +from .array_sequence import ArraySequence |
| 7 | +from .tractogram import Tractogram, LazyTractogram |
| 8 | +from .tractogram_file import ExtensionWarning |
| 9 | + |
| 10 | +from .trk import TrkFile |
| 11 | + |
| 12 | +# List of all supported formats |
| 13 | +FORMATS = {".trk": TrkFile} |
| 14 | + |
| 15 | + |
| 16 | +def is_supported(fileobj): |
| 17 | + """ Checks if the file-like object if supported by NiBabel. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + fileobj : string or file-like object |
| 22 | + If string, a filename; otherwise an open file-like object pointing |
| 23 | + to a streamlines file (and ready to read from the beginning of the |
| 24 | + header) |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + is_supported : boolean |
| 29 | + """ |
| 30 | + return detect_format(fileobj) is not None |
| 31 | + |
| 32 | + |
| 33 | +def detect_format(fileobj): |
| 34 | + """ Returns the StreamlinesFile object guessed from the file-like object. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + fileobj : string or file-like object |
| 39 | + If string, a filename; otherwise an open file-like object pointing |
| 40 | + to a tractogram file (and ready to read from the beginning of the |
| 41 | + header) |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + tractogram_file : :class:`TractogramFile` class |
| 46 | + The class type guessed from the content of `fileobj`. |
| 47 | + """ |
| 48 | + for format in FORMATS.values(): |
| 49 | + try: |
| 50 | + if format.is_correct_format(fileobj): |
| 51 | + return format |
| 52 | + except IOError: |
| 53 | + pass |
| 54 | + |
| 55 | + if isinstance(fileobj, string_types): |
| 56 | + _, ext = os.path.splitext(fileobj) |
| 57 | + return FORMATS.get(ext.lower()) |
| 58 | + |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +def load(fileobj, lazy_load=False): |
| 63 | + """ Loads streamlines in *RAS+* and *mm* space from a file-like object. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + fileobj : string or file-like object |
| 68 | + If string, a filename; otherwise an open file-like object |
| 69 | + pointing to a streamlines file (and ready to read from the beginning |
| 70 | + of the streamlines file's header). |
| 71 | + lazy_load : {False, True}, optional |
| 72 | + If True, load streamlines in a lazy manner i.e. they will not be kept |
| 73 | + in memory and only be loaded when needed. |
| 74 | + Otherwise, load all streamlines in memory. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + tractogram_file : :class:`TractogramFile` object |
| 79 | + Returns an instance of a :class:`TractogramFile` containing data and |
| 80 | + metadata of the tractogram loaded from `fileobj`. |
| 81 | +
|
| 82 | + Notes |
| 83 | + ----- |
| 84 | + The streamline coordinate (0,0,0) refers to the center of the voxel. |
| 85 | + """ |
| 86 | + tractogram_file = detect_format(fileobj) |
| 87 | + |
| 88 | + if tractogram_file is None: |
| 89 | + raise ValueError("Unknown format for 'fileobj': {}".format(fileobj)) |
| 90 | + |
| 91 | + return tractogram_file.load(fileobj, lazy_load=lazy_load) |
| 92 | + |
| 93 | + |
| 94 | +def save(tractogram, filename, **kwargs): |
| 95 | + """ Saves a tractogram to a file. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + tractogram : :class:`Tractogram` object or :class:`TractogramFile` object |
| 100 | + If :class:`Tractogram` object, the file format will be guessed from |
| 101 | + `filename` and a :class:`TractogramFile` object will be created using |
| 102 | + provided keyword arguments. |
| 103 | + If :class:`TractogramFile` object, the file format is known and will |
| 104 | + be used to save its content to `filename`. |
| 105 | + filename : str |
| 106 | + Name of the file where the tractogram will be saved. |
| 107 | + \*\*kwargs : keyword arguments |
| 108 | + Keyword arguments passed to :class:`TractogramFile` constructor. |
| 109 | + Should not be specified if `tractogram` is already an instance of |
| 110 | + :class:`TractogramFile`. |
| 111 | + """ |
| 112 | + tractogram_file_class = detect_format(filename) |
| 113 | + if isinstance(tractogram, Tractogram): |
| 114 | + if tractogram_file_class is None: |
| 115 | + msg = "Unknown tractogram file format: '{}'".format(filename) |
| 116 | + raise ValueError(msg) |
| 117 | + |
| 118 | + tractogram_file = tractogram_file_class(tractogram, **kwargs) |
| 119 | + |
| 120 | + else: # Assume it's a TractogramFile object. |
| 121 | + tractogram_file = tractogram |
| 122 | + if (tractogram_file_class is None or |
| 123 | + not isinstance(tractogram_file, tractogram_file_class)): |
| 124 | + msg = ("The extension you specified is unusual for the provided" |
| 125 | + " 'TractogramFile' object.") |
| 126 | + warnings.warn(msg, ExtensionWarning) |
| 127 | + |
| 128 | + if len(kwargs) > 0: |
| 129 | + msg = ("A 'TractogramFile' object was provided, no need for" |
| 130 | + " keyword arguments.") |
| 131 | + raise ValueError(msg) |
| 132 | + |
| 133 | + tractogram_file.save(filename) |
0 commit comments