Skip to content

Commit bde05d4

Browse files
committed
Merge pull request #67 from agramfort/fs_refactor_load_scalar
Removing freesurfer.io.read_scalar_data now that we support MGH format
2 parents 0573f20 + 8e1f0e8 commit bde05d4

File tree

3 files changed

+3
-71
lines changed

3 files changed

+3
-71
lines changed

nibabel/freesurfer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Reading functions for freesurfer files
22
"""
33

4-
from io import read_geometry, read_morph_data, read_scalar_data, \
4+
from io import read_geometry, read_morph_data, \
55
read_annot, read_label
66
from mghformat import load, save, MGHImage

nibabel/freesurfer/io.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import os
2-
from os.path import join as pjoin
3-
import gzip
41
import numpy as np
5-
import nibabel as nib
6-
from nibabel.spatialimages import ImageFileError
72

83

94
def _fread3(fobj):
@@ -127,59 +122,6 @@ def read_morph_data(filepath):
127122
return curv
128123

129124

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-
183125
def read_annot(filepath, orig_ids=False):
184126
"""Read in a Freesurfer annotation from a .annot file.
185127
@@ -264,4 +206,3 @@ def read_label(filepath):
264206
"""
265207
label_array = np.loadtxt(filepath, dtype=np.int, skiprows=2, usecols=[0])
266208
return label_array
267-

nibabel/freesurfer/tests/test_io.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import numpy as np
66
from numpy.testing import assert_equal
77

8-
from .. import read_geometry, read_morph_data, read_annot, read_label, \
9-
read_scalar_data
8+
from .. import read_geometry, read_morph_data, read_annot, read_label
9+
1010

1111
have_freesurfer = True
1212
if 'SUBJECTS_DIR' not in os.environ:
@@ -64,12 +64,3 @@ def test_label():
6464
label = read_label(label_path)
6565
# XXX : test more
6666
assert_true(np.all(label > 0))
67-
68-
69-
@freesurfer_test
70-
def test_scalar_data():
71-
"""Test IO of .mgz and .mgh files"""
72-
scalar_path = pjoin(data_path, "mri", "T1.mgz")
73-
scalar = read_scalar_data(scalar_path)
74-
scalar = scalar.astype(np.int)
75-
assert_true((scalar.max() - scalar.min()) == 255)

0 commit comments

Comments
 (0)