|
1 | 1 | from __future__ import division, print_function, absolute_import |
2 | 2 |
|
| 3 | +import warnings |
3 | 4 | import numpy as np |
4 | 5 | import getpass |
5 | 6 | import time |
@@ -107,20 +108,31 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False): |
107 | 108 |
|
108 | 109 | extra = fobj.read() if read_metadata else b'' |
109 | 110 | if extra: |
| 111 | + volume_info = OrderedDict() |
| 112 | + |
110 | 113 | if extra[:4] != b'\x00\x00\x00\x14': |
111 | 114 | warnings.warn("Unknown extension code.") |
112 | | - volume_info = OrderedDict() |
113 | | - try: |
114 | | - for line in extra[4:].split(b'\n'): |
115 | | - key, val = map(bytes.strip, line.split(b'=', 1)) |
116 | | - key = key.decode('utf-8') |
117 | | - if key in ('voxelsize', 'xras', 'yras', 'zras'): |
118 | | - val = np.fromstring(val, sep=' ') |
119 | | - elif key == 'volume': |
120 | | - val = np.fromstring(val, sep=' ', dtype=np.uint) |
121 | | - volume_info[key] = val |
122 | | - except ValueError: |
123 | | - raise ValueError("Error parsing volume info") |
| 115 | + else: |
| 116 | + try: |
| 117 | + for line in extra[4:].split(b'\n'): |
| 118 | + if len(line) == 0: |
| 119 | + continue |
| 120 | + key, val = map(bytes.strip, line.split(b'=', 1)) |
| 121 | + print(key, val) |
| 122 | + key = key.decode('utf-8') |
| 123 | + if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'): |
| 124 | + val = np.fromstring(val, sep=' ') |
| 125 | + val = val.astype(np.float) |
| 126 | + elif key == 'volume': |
| 127 | + val = np.fromstring(val, sep=' ', dtype=np.uint) |
| 128 | + val = val.astype(np.int) |
| 129 | + volume_info[key] = val |
| 130 | + except ValueError: |
| 131 | + raise ValueError("Error parsing volume info") |
| 132 | + |
| 133 | + if len(volume_info) == 0: |
| 134 | + warnings.warn("Volume geometry info is either " |
| 135 | + "not contained or not valid.") |
124 | 136 |
|
125 | 137 | else: |
126 | 138 | raise ValueError("File does not appear to be a Freesurfer surface") |
@@ -160,10 +172,10 @@ def write_geometry(filepath, coords, faces, create_stamp=None, |
160 | 172 | time.ctime()) |
161 | 173 |
|
162 | 174 | postlude = b'' |
163 | | - if volume_info is not None: |
| 175 | + if volume_info is not None and len(volume_info) > 0: |
164 | 176 | postlude = [b'\x00\x00\x00\x14'] |
165 | 177 | for key, val in volume_info.items(): |
166 | | - if key in ('voxelsize', 'xras', 'yras', 'zras'): |
| 178 | + if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'): |
167 | 179 | val = '{:.3f} {:.3f} {:.3f}'.format(*val) |
168 | 180 | elif key == 'volume': |
169 | 181 | val = '{:d} {:d} {:d}'.format(*val) |
|
0 commit comments