|
21 | 21 | from .open import fiff_open |
22 | 22 | from .tree import dir_tree_find |
23 | 23 | from .tag import read_tag, find_tag, _coord_dict |
24 | | -from .proj import _read_proj, _write_proj, _uniquify_projs, _normalize_proj |
| 24 | +from .proj import (_read_proj, _write_proj, _uniquify_projs, _normalize_proj, |
| 25 | + Projection) |
25 | 26 | from .ctf_comp import read_ctf_comp, write_ctf_comp |
26 | 27 | from .write import (start_file, end_file, start_block, end_block, |
27 | 28 | write_string, write_dig_points, write_float, write_int, |
|
31 | 32 | from ..transforms import invert_transform, Transform, _coord_frame_name |
32 | 33 | from ..utils import (logger, verbose, warn, object_diff, _validate_type, |
33 | 34 | _stamp_to_dt, _dt_to_stamp, _pl, _is_numeric) |
34 | | -from ._digitization import (_format_dig_points, _dig_kind_proper, |
| 35 | +from ._digitization import (_format_dig_points, _dig_kind_proper, DigPoint, |
35 | 36 | _dig_kind_rev, _dig_kind_ints, _read_dig_fif) |
36 | 37 | from ._digitization import write_dig as _dig_write_dig |
37 | 38 | from .compensator import get_current_comp |
@@ -190,6 +191,16 @@ def set_montage(self, montage, raise_if_subset=DEPRECATED_PARAM, |
190 | 191 | return self |
191 | 192 |
|
192 | 193 |
|
| 194 | +def _format_trans(obj, key): |
| 195 | + try: |
| 196 | + t = obj[key] |
| 197 | + except KeyError: |
| 198 | + pass |
| 199 | + else: |
| 200 | + if t is not None: |
| 201 | + obj[key] = Transform(t['from'], t['to'], t['trans']) |
| 202 | + |
| 203 | + |
193 | 204 | # XXX Eventually this should be de-duplicated with the MNE-MATLAB stuff... |
194 | 205 | class Info(dict, MontageMixin): |
195 | 206 | """Measurement information. |
@@ -528,9 +539,24 @@ class Info(dict, MontageMixin): |
528 | 539 |
|
529 | 540 | def __init__(self, *args, **kwargs): |
530 | 541 | super(Info, self).__init__(*args, **kwargs) |
531 | | - t = self.get('dev_head_t', None) |
532 | | - if t is not None and not isinstance(t, Transform): |
533 | | - self['dev_head_t'] = Transform(t['from'], t['to'], t['trans']) |
| 542 | + # Deal with h5io writing things as dict |
| 543 | + for key in ('dev_head_t', 'ctf_head_t', 'dev_ctf_t'): |
| 544 | + _format_trans(self, key) |
| 545 | + for res in self.get('hpi_results', []): |
| 546 | + _format_trans(res, 'coord_trans') |
| 547 | + if self.get('dig', None) is not None and len(self['dig']) and \ |
| 548 | + not isinstance(self['dig'][0], DigPoint): |
| 549 | + self['dig'] = _format_dig_points(self['dig']) |
| 550 | + for pi, proj in enumerate(self.get('projs', [])): |
| 551 | + if not isinstance(proj, Projection): |
| 552 | + self['projs'][pi] = Projection(proj) |
| 553 | + # Old files could have meas_date as tuple instead of datetime |
| 554 | + try: |
| 555 | + meas_date = self['meas_date'] |
| 556 | + except KeyError: |
| 557 | + pass |
| 558 | + else: |
| 559 | + self['meas_date'] = _ensure_meas_date_none_or_dt(meas_date) |
534 | 560 |
|
535 | 561 | def copy(self): |
536 | 562 | """Copy the instance. |
@@ -1362,11 +1388,7 @@ def read_meas_info(fid, tree, clean_bads=False, verbose=None): |
1362 | 1388 | info['proj_name'] = proj_name |
1363 | 1389 | if meas_date is None: |
1364 | 1390 | meas_date = (info['meas_id']['secs'], info['meas_id']['usecs']) |
1365 | | - if np.array_equal(meas_date, DATE_NONE): |
1366 | | - meas_date = None |
1367 | | - else: |
1368 | | - meas_date = _stamp_to_dt(meas_date) |
1369 | | - info['meas_date'] = meas_date |
| 1391 | + info['meas_date'] = _ensure_meas_date_none_or_dt(meas_date) |
1370 | 1392 | info['utc_offset'] = utc_offset |
1371 | 1393 |
|
1372 | 1394 | info['sfreq'] = sfreq |
@@ -1408,6 +1430,14 @@ def read_meas_info(fid, tree, clean_bads=False, verbose=None): |
1408 | 1430 | return info, meas |
1409 | 1431 |
|
1410 | 1432 |
|
| 1433 | +def _ensure_meas_date_none_or_dt(meas_date): |
| 1434 | + if meas_date is None or np.array_equal(meas_date, DATE_NONE): |
| 1435 | + meas_date = None |
| 1436 | + elif not isinstance(meas_date, datetime.datetime): |
| 1437 | + meas_date = _stamp_to_dt(meas_date) |
| 1438 | + return meas_date |
| 1439 | + |
| 1440 | + |
1411 | 1441 | def _check_dates(info, prepend_error=''): |
1412 | 1442 | """Check dates before writing as fif files. |
1413 | 1443 |
|
|
0 commit comments