Ejemplo n.º 1
0
def _get_mrk_meas_date(mrk):
    """Find the measurement date from a KIT marker file."""
    info = get_kit_info(mrk, False)[0]
    meas_date = info.get('meas_date', None)
    if isinstance(meas_date, (tuple, list, np.ndarray)):
        meas_date = meas_date[0]
    if meas_date is not None:
        meas_datetime = datetime.fromtimestamp(meas_date)
    else:
        meas_datetime = datetime.min
    return meas_datetime
Ejemplo n.º 2
0
def test_unknown_format(tmp_path):
    """Test our warning about an unknown format."""
    fname = tmp_path / op.basename(ricoh_path)
    _, kit_info = get_kit_info(ricoh_path, allow_unknown_format=False)
    n_before = kit_info['dirs'][KIT.DIR_INDEX_SYSTEM]['offset']
    with open(fname, 'wb') as fout:
        with open(ricoh_path, 'rb') as fin:
            fout.write(fin.read(n_before))
            version, revision = np.fromfile(fin, '<i4', 2)
            assert version > 2  # good
            version = 1  # bad
            np.array([version, revision], '<i4').tofile(fout)
            fout.write(fin.read())
    with pytest.raises(ValueError, match='SQD file format V1R000 is not offi'):
        read_raw_kit(fname)
    # it's not actually an old file, so it actually raises an exception later
    # about an unknown datatype
    with pytest.raises(Exception):
        with pytest.warns(RuntimeWarning, match='Force loading'):
            read_raw_kit(fname, allow_unknown_format=True)
Ejemplo n.º 3
0
def test_kit(_bids_validate):
    """Test functionality of the write_raw_bids conversion for KIT data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'kit', 'tests', 'data')
    raw_fname = op.join(data_path, 'test.sqd')
    events_fname = op.join(data_path, 'test-eve.txt')
    hpi_fname = op.join(data_path, 'test_mrk.sqd')
    hpi_pre_fname = op.join(data_path, 'test_mrk_pre.sqd')
    hpi_post_fname = op.join(data_path, 'test_mrk_post.sqd')
    electrode_fname = op.join(data_path, 'test_elp.txt')
    headshape_fname = op.join(data_path, 'test_hsp.txt')
    event_id = dict(cond=1)

    kit_bids_basename = bids_basename.replace('_acq-01', '')

    raw = mne.io.read_raw_kit(raw_fname,
                              mrk=hpi_fname,
                              elp=electrode_fname,
                              hsp=headshape_fname)
    write_raw_bids(raw,
                   kit_bids_basename,
                   output_path,
                   events_data=events_fname,
                   event_id=event_id,
                   overwrite=False)

    _bids_validate(output_path)
    assert op.exists(op.join(output_path, 'participants.tsv'))

    read_raw_bids(kit_bids_basename + '_meg.sqd', output_path)

    # test anonymize
    output_path = _test_anonymize(raw, kit_bids_basename, events_fname,
                                  event_id)
    _bids_validate(output_path)

    # ensure the channels file has no STI 014 channel:
    channels_tsv = make_bids_basename(subject=subject_id,
                                      session=session_id,
                                      task=task,
                                      run=run,
                                      suffix='channels.tsv',
                                      prefix=op.join(output_path, 'sub-01',
                                                     'ses-01', 'meg'))
    data = _from_tsv(channels_tsv)
    assert 'STI 014' not in data['name']

    # ensure the marker file is produced in the right place
    marker_fname = make_bids_basename(subject=subject_id,
                                      session=session_id,
                                      task=task,
                                      run=run,
                                      suffix='markers.sqd',
                                      prefix=op.join(output_path, 'sub-01',
                                                     'ses-01', 'meg'))
    assert op.exists(marker_fname)

    # test attempts at writing invalid event data
    event_data = np.loadtxt(events_fname)
    # make the data the wrong number of dimensions
    event_data_3d = np.atleast_3d(event_data)
    other_output_path = _TempDir()
    with pytest.raises(ValueError, match='two dimensions'):
        write_raw_bids(raw,
                       bids_basename,
                       other_output_path,
                       events_data=event_data_3d,
                       event_id=event_id,
                       overwrite=True)
    # remove 3rd column
    event_data = event_data[:, :2]
    with pytest.raises(ValueError, match='second dimension'):
        write_raw_bids(raw,
                       bids_basename,
                       other_output_path,
                       events_data=event_data,
                       event_id=event_id,
                       overwrite=True)
    # test correct naming of marker files
    raw = mne.io.read_raw_kit(raw_fname,
                              mrk=[hpi_pre_fname, hpi_post_fname],
                              elp=electrode_fname,
                              hsp=headshape_fname)
    write_raw_bids(raw,
                   kit_bids_basename.replace('sub-01', 'sub-%s' % subject_id2),
                   output_path,
                   events_data=events_fname,
                   event_id=event_id,
                   overwrite=False)

    _bids_validate(output_path)
    # ensure the marker files are renamed correctly
    marker_fname = make_bids_basename(subject=subject_id2,
                                      session=session_id,
                                      task=task,
                                      run=run,
                                      suffix='markers.sqd',
                                      acquisition='pre',
                                      prefix=os.path.join(
                                          output_path, 'sub-02', 'ses-01',
                                          'meg'))
    info = get_kit_info(marker_fname, False)[0]
    assert info['meas_date'] == get_kit_info(hpi_pre_fname,
                                             False)[0]['meas_date']
    marker_fname = marker_fname.replace('acq-pre', 'acq-post')
    info = get_kit_info(marker_fname, False)[0]
    assert info['meas_date'] == get_kit_info(hpi_post_fname,
                                             False)[0]['meas_date']

    # check that providing markers in the wrong order raises an error
    raw = mne.io.read_raw_kit(raw_fname,
                              mrk=[hpi_post_fname, hpi_pre_fname],
                              elp=electrode_fname,
                              hsp=headshape_fname)
    with pytest.raises(ValueError, match='Markers'):
        write_raw_bids(raw,
                       kit_bids_basename.replace('sub-01',
                                                 'sub-%s' % subject_id2),
                       output_path,
                       events_data=events_fname,
                       event_id=event_id,
                       overwrite=True)