|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from surfa.io import protocol |
| 5 | +from surfa.io.utils import check_file_readability |
| 6 | +from surfa.io.framed import framed_array_from_4d |
| 7 | + |
| 8 | +from surfa import TimeSeries |
| 9 | + |
| 10 | +def load_timeseries(filename, fmt=None): |
| 11 | + """ |
| 12 | + Generic loader for `FramedArray` objects. |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + filename : str or Path |
| 17 | + File path to read. |
| 18 | + fmt : str, optional |
| 19 | + Explicit file format. If None, we extrapolate from the file extension. |
| 20 | +
|
| 21 | + Returns |
| 22 | + ------- |
| 23 | + TimeSeries |
| 24 | + A TimeSeries object loaded file. |
| 25 | + """ |
| 26 | + check_file_readability(filename) |
| 27 | + |
| 28 | + if fmt is None: |
| 29 | + iop = find_timeseries_protocol_by_extension(filename) |
| 30 | + else: |
| 31 | + iop = protocol.find_protocol_by_name(timeseries_io_protocols, fmt) |
| 32 | + if iop is None: |
| 33 | + raise ValueError(f'unknown file format {fmt}') |
| 34 | + |
| 35 | + return iop().load(filename) |
| 36 | + |
| 37 | + |
| 38 | +def find_timeseries_protocol_by_extension(filename): |
| 39 | + """ |
| 40 | + Find timeseries IO protocol from file extension. |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + filename : str |
| 45 | + File path to read. |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + protocol : IOProtocol |
| 50 | + Matched timeseries IO protocol class. |
| 51 | + """ |
| 52 | + |
| 53 | + # find matching protocol |
| 54 | + iop = protocol.find_protocol_by_extension(timeseries_io_protocols, filename) |
| 55 | + if iop is None: |
| 56 | + basename = os.path.basename(filename).lower() |
| 57 | + raise ValueError(f'timeseries file type {basename} is not supported.') |
| 58 | + |
| 59 | + return iop |
| 60 | + |
| 61 | + |
| 62 | +class GiftiIO(protocol.IOProtocol): |
| 63 | + """ |
| 64 | + GIFTI IO protocol for time-series files. |
| 65 | + """ |
| 66 | + name = 'time-series' |
| 67 | + extensions = ('.gii') |
| 68 | + |
| 69 | + def __init__(self): |
| 70 | + try: |
| 71 | + import nibabel as nib |
| 72 | + except ImportError: |
| 73 | + raise ImportError('the `nibabel` python package must be installed for gifti IO') |
| 74 | + self.nib = nib |
| 75 | + |
| 76 | + def load(self, filename): |
| 77 | + """ |
| 78 | + Read time-series from a gifti file. |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + filename : str or Path |
| 83 | + File path read. |
| 84 | +
|
| 85 | + Returns |
| 86 | + ------- |
| 87 | + TimeSeries |
| 88 | + TimeSeries object loaded from file. |
| 89 | + """ |
| 90 | + |
| 91 | + giftiImage = self.nib.load(filename) |
| 92 | + data = np.expand_dims(giftiImage.agg_data(), axis=(1,2)) |
| 93 | + |
| 94 | + arr = framed_array_from_4d(TimeSeries, data) |
| 95 | + |
| 96 | + return arr |
| 97 | + |
| 98 | + |
| 99 | + def save(self, arr, filename): |
| 100 | + """ |
| 101 | + Write time-series to a gifti file. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + arr : array |
| 106 | + Array to save as gifti time-series |
| 107 | + filename : str or Path |
| 108 | + Target file path. |
| 109 | + """ |
| 110 | + |
| 111 | + raise ValueError(f'ERROR: surfa.io.timeseries.GiftiIO.save() not implemented!') |
| 112 | + |
| 113 | + |
| 114 | +# enabled TimeSeries IO protocol classes |
| 115 | +timeseries_io_protocols = [ |
| 116 | + GiftiIO, # '.gii' |
| 117 | +] |
0 commit comments