Skip to content

Commit 2a3a0b2

Browse files
committed
DOC: Update docstring
Check bounds on values to be written Add fnum parameter to match write_curv.m
1 parent df0b9bb commit 2a3a0b2

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

nibabel/freesurfer/io.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,46 @@ def read_morph_data(filepath):
160160
return curv
161161

162162

163-
def write_morph_data(filepath, values):
164-
"""Write out a Freesurfer morphometry data file.
163+
def write_morph_data(filepath, values, fnum=0):
164+
"""Write Freesurfer morphometry data `values` to file `filepath`
165165
166-
See:
166+
Equivalent to FreeSurfer's `write_curv.m`_
167+
168+
See also:
167169
http://www.grahamwideman.com/gw/brain/fs/surfacefileformats.htm#CurvNew
168170
171+
.. _write_curv.m: \
172+
https://github.com/neurodebian/freesurfer/blob/debian-sloppy/matlab/write_curv.m
173+
169174
Parameters
170175
----------
171176
filepath : str
172177
Path to annotation file to be written
173-
values : ndarray, shape (n_vertices,)
178+
values : array-like
174179
Surface morphometry values
180+
fnum : int, optional
181+
Number of faces in the associated surface
175182
"""
176183
magic_bytes = np.array([255, 255, 255], dtype=np.uint8)
184+
185+
i4info = np.iinfo('i4')
186+
if len(values) > i4info.max:
187+
raise ValueError("Too many values for morphometry file")
188+
if not i4info.min <= fnum <= i4info.max:
189+
raise ValueError("Argument fnum must be between {0} and {1}".format(
190+
i4info.min, i4info.max))
191+
192+
array = np.asarray(values).astype('>f4')
193+
if len(array.shape) > 1:
194+
raise ValueError("Multi-dimensional values not supported")
195+
177196
with open(filepath, 'wb') as fobj:
178197
magic_bytes.tofile(fobj)
179198

180199
# vertex count, face count (unused), vals per vertex (only 1 supported)
181-
np.array([len(values), 0, 1], dtype='>i4').tofile(fobj)
200+
np.array([len(values), fnum, 1], dtype='>i4').tofile(fobj)
182201

183-
np.array(values, dtype='>f4').tofile(fobj)
202+
array.tofile(fobj)
184203

185204

186205
def read_annot(filepath, orig_ids=False):

0 commit comments

Comments
 (0)