|
1 |
| -import os |
2 |
| -from os.path import join as pjoin |
3 |
| -import gzip |
4 | 1 | import numpy as np
|
5 |
| -import nibabel as nib |
6 |
| -from nibabel.spatialimages import ImageFileError |
7 | 2 |
|
8 | 3 |
|
9 | 4 | def _fread3(fobj):
|
@@ -127,59 +122,6 @@ def read_morph_data(filepath):
|
127 | 122 | return curv
|
128 | 123 |
|
129 | 124 |
|
130 |
| -def read_scalar_data(filepath): |
131 |
| - """Load in scalar data from an image. |
132 |
| -
|
133 |
| - Parameters |
134 |
| - ---------- |
135 |
| - filepath : str |
136 |
| - path to scalar data file |
137 |
| -
|
138 |
| - Returns |
139 |
| - ------- |
140 |
| - scalar_data : numpy array |
141 |
| - flat numpy array of scalar data |
142 |
| - """ |
143 |
| - ext = os.path.splitext(filepath)[1] |
144 |
| - if ext == ".mgz": |
145 |
| - openfile = gzip.open |
146 |
| - elif ext == ".mgh": |
147 |
| - openfile = open |
148 |
| - else: |
149 |
| - raise ValueError("Scalar file format must be in .mg{hz} format") |
150 |
| - |
151 |
| - fobj = openfile(filepath, "rb") |
152 |
| - # We have to use np.fromstring here as gzip fileobjects don't work |
153 |
| - # with np.fromfile; same goes for try/finally instead of with statement |
154 |
| - try: |
155 |
| - v = np.fromstring(fobj.read(4), ">i4")[0] |
156 |
| - if v != 1: |
157 |
| - # I don't actually know what versions this code will read, so to be |
158 |
| - # on the safe side, let's only let version 1 in for now. |
159 |
| - # Scalar data might also be in curv format (e.g. lh.thickness) |
160 |
| - # in which case the first item in the file is a magic number. |
161 |
| - raise NotImplementedError("Scalar data file version not supported") |
162 |
| - ndim1 = np.fromstring(fobj.read(4), ">i4")[0] |
163 |
| - ndim2 = np.fromstring(fobj.read(4), ">i4")[0] |
164 |
| - ndim3 = np.fromstring(fobj.read(4), ">i4")[0] |
165 |
| - nframes = np.fromstring(fobj.read(4), ">i4")[0] |
166 |
| - datatype = np.fromstring(fobj.read(4), ">i4")[0] |
167 |
| - # Set the number of bytes per voxel and numpy data type according to |
168 |
| - # FS codes |
169 |
| - databytes, typecode = {0: (1, ">i1"), 1: (4, ">i4"), 3: (4, ">f4"), |
170 |
| - 4: (2, ">h")}[datatype] |
171 |
| - # Ignore the rest of the header here, just seek to the data |
172 |
| - fobj.seek(284) |
173 |
| - nbytes = ndim1 * ndim2 * ndim3 * nframes * databytes |
174 |
| - # Read in all the data, keep it in flat representation |
175 |
| - # (is this ever a problem?) |
176 |
| - scalar_data = np.fromstring(fobj.read(nbytes), typecode) |
177 |
| - finally: |
178 |
| - fobj.close() |
179 |
| - |
180 |
| - return scalar_data |
181 |
| - |
182 |
| - |
183 | 125 | def read_annot(filepath, orig_ids=False):
|
184 | 126 | """Read in a Freesurfer annotation from a .annot file.
|
185 | 127 |
|
@@ -264,4 +206,3 @@ def read_label(filepath):
|
264 | 206 | """
|
265 | 207 | label_array = np.loadtxt(filepath, dtype=np.int, skiprows=2, usecols=[0])
|
266 | 208 | return label_array
|
267 |
| - |
0 commit comments