Repository: mne-tools/mne-bids

Issue:
add tests
We need tests. Reminder for anyone who is bored

and also continuous integration


Discussion:

Resolving patch:
diff --git a/mne_bids/__init__.py b/mne_bids/__init__.py
--- a/mne_bids/__init__.py
+++ b/mne_bids/__init__.py
@@ -1,2 +1,2 @@
-from .bids_meg import raw_to_bids
-from .bids_meg import _mkdir_p
+from .meg_bids import raw_to_bids
+from .meg_bids import _mkdir_p
diff --git a/mne_bids/bids_meg.py b/mne_bids/meg_bids.py
similarity index 90%
rename from mne_bids/bids_meg.py
rename to mne_bids/meg_bids.py
--- a/mne_bids/bids_meg.py
+++ b/mne_bids/meg_bids.py
@@ -10,6 +10,7 @@
 import shutil as sh
 import pandas as pd
 import json
+from collections import defaultdict
 
 import numpy as np
 from mne import read_events, find_events, io
@@ -20,12 +21,14 @@
 
 
 orientation = {'.sqd': 'ALS', '.con': 'ALS', '.fif': 'RAS', '.gz': 'RAS',
-               '.pdf': 'ALS'}
+               '.pdf': 'ALS', '.ds': 'ALS'}
 
-units = {'.sqd': 'm', '.con': 'm', '.fif': 'm', '.gz': 'm', '.pdf': 'm'}
+units = {'.sqd': 'm', '.con': 'm', '.fif': 'm', '.gz': 'm', '.pdf': 'm',
+         '.ds': 'cm'}
 
 manufacturers = {'.sqd': 'KIT/Yokogawa', '.con': 'KIT/Yokogawa',
-                 '.fif': 'Elekta', '.gz': 'Elekta', '.pdf': '4D Magnes'}
+                 '.fif': 'Elekta', '.gz': 'Elekta', '.pdf': '4D Magnes',
+                 '.ds': 'CTF'}
 
 
 def _mkdir_p(path):
@@ -41,9 +44,11 @@ def _mkdir_p(path):
 def _channel_tsv(raw, fname, verbose):
     """Create channel tsv."""
 
-    map_chs = dict(grad='MEGGRAD', mag='MEGMAG', stim='TRIG', eeg='EEG',
+    map_chs = defaultdict(lambda: 'OTHER')
+    map_chs.update(grad='MEGGRAD', mag='MEGMAG', stim='TRIG', eeg='EEG',
                    eog='EOG', ecg='ECG', misc='MISC', ref_meg='REFMEG')
-    map_desc = dict(grad='Gradiometer', mag='Magnetometer',
+    map_desc = defaultdict(lambda: 'Other type of channel')
+    map_desc.update(grad='Gradiometer', mag='Magnetometer',
                     stim='Trigger',
                     eeg='ElectroEncephaloGram',
                     ecg='ElectroCardioGram',
@@ -249,6 +254,9 @@ def raw_to_bids(subject_id, session_id, run, task, raw_fname, output_path,
     """
 
     fname, ext = os.path.splitext(raw_fname)
+    # BTi data is the only file format that does have a file extension
+    if ext == '':
+        ext = '.pdf'
     ses_path = op.join(output_path, 'sub-%s' % subject_id,
                        'ses-%s' % session_id)
     meg_path = op.join(ses_path, 'meg')
@@ -288,11 +296,12 @@ def raw_to_bids(subject_id, session_id, run, task, raw_fname, output_path,
     elif ext == '.pdf':
         if os.path.isfile(raw_fname):
             raw = io.read_raw_bti(raw_fname, config_fname=config,
+                                  head_shape_fname=hsp,
                                   preload=False, verbose=verbose)
 
     # CTF systems
-    elif ext == '':
-        pass
+    elif ext == '.ds':
+        raw = io.read_raw_ctf(raw_fname)
 
     # save stuff
     _scans_tsv(raw, raw_fname_bids, scans_fname, verbose)
@@ -311,7 +320,23 @@ def raw_to_bids(subject_id, session_id, run, task, raw_fname, output_path,
     # for files with multiple parts
     if ext in ['.fif', '.gz']:
         raw.save(raw_fname_bids, overwrite=overwrite)
+    elif ext == '.ds':
+        try:
+            sh.copytree(raw_fname, raw_fname_bids)
+        except FileExistsError as err:
+            if overwrite:
+                sh.rmtree(raw_fname_bids)
+                sh.copytree(raw_fname, raw_fname_bids)
+            else:
+                raise err
     else:
-        sh.copyfile(raw_fname, raw_fname_bids)
+        try:
+            sh.copyfile(raw_fname, raw_fname_bids)
+        except FileExistsError as err:
+            if overwrite:
+                os.remove(raw_fname_bids)
+                sh.copyfile(raw_fname, raw_fname_bids)
+            else:
+                raise err
 
     return output_path


Test patch:
diff --git a/tests/test_bids_meg.py b/tests/test_bids_meg.py
deleted file mode 100644
--- a/tests/test_bids_meg.py
+++ /dev/null
@@ -1,38 +0,0 @@
-# Author: Mainak Jas <mainak.jas@telecom-paristech.fr>
-# License: BSD (3-clause)
-
-import os.path as op
-
-import mne
-from mne.datasets import sample
-from mne.utils import _TempDir
-
-from mne_bids import raw_to_bids
-
-data_path = sample.data_path()
-event_id = {'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3,
-            'Visual/Right': 4, 'Smiley': 5, 'Button': 32}
-
-raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
-events_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
-
-
-def test_bids_meg():
-    """Smoke test BIDS converter."""
-    output_path = _TempDir()
-    raw_to_bids(subject_id='01', run='01', task='audiovisual',
-                raw_fname=raw_fname, events_fname=events_fname,
-                output_path=output_path, event_id=event_id,
-                overwrite=True)
-
-    # let's do some modifications to meas_date
-    raw = mne.io.read_raw_fif(raw_fname)
-    raw.info['meas_date'] = None
-    data_path2 = _TempDir()
-    raw_fname2 = op.join(data_path2, 'sample_audvis_raw.fif')
-    raw.save(raw_fname2)
-
-    raw_to_bids(subject_id='01', run='01', task='audiovisual',
-                raw_fname=raw_fname2, events_fname=events_fname,
-                output_path=output_path, event_id=event_id,
-                overwrite=True)
diff --git a/tests/test_meg_bids.py b/tests/test_meg_bids.py
new file mode 100644
--- /dev/null
+++ b/tests/test_meg_bids.py
@@ -0,0 +1,103 @@
+""" Testing MNE BIDS converter
+
+There are four supported MEG systems in MNE.
+We should test their functionality with their sample data.
+"""
+# Authors: Mainak Jas <mainak.jas@telecom-paristech.fr>
+#          Teon L Brooks <teon.brooks@gmail.com>
+# License: BSD (3-clause)
+
+import os.path as op
+
+import mne
+from mne.datasets import sample, testing
+from mne.utils import _TempDir
+
+from mne_bids import raw_to_bids
+
+base_path = op.join(op.dirname(mne.__file__), 'io')
+subject_id = '01'
+session_id = '01'
+run = '01'
+task = 'testing'
+
+
+def test_fif():
+    """
+    Testing the functionality of the raw_to_bids conversion for
+    Neuromag data.
+    """
+
+    output_path = _TempDir()
+    data_path = sample.data_path()
+    raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
+
+    event_id = {'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3,
+                'Visual/Right': 4, 'Smiley': 5, 'Button': 32}
+    events_fname = op.join(data_path, 'MEG', 'sample',
+                           'sample_audvis_raw-eve.fif')
+
+    raw_to_bids(subject_id=subject_id, session_id=session_id, run=run,
+                task=task, raw_fname=raw_fname, events_fname=events_fname,
+                output_path=output_path, event_id=event_id,
+                overwrite=True)
+
+    # let's do some modifications to meas_date
+    raw = mne.io.read_raw_fif(raw_fname)
+    raw.anonymize()
+    data_path2 = _TempDir()
+    raw_fname2 = op.join(data_path2, 'sample_audvis_raw.fif')
+    raw.save(raw_fname2)
+
+    raw_to_bids(subject_id=subject_id, run=run, task=task,
+                session_id=session_id, raw_fname=raw_fname2,
+                events_fname=events_fname, output_path=output_path,
+                event_id=event_id, overwrite=True)
+
+
+def test_kit():
+    """
+    Testing the functionality of the raw_to_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')
+    electrode_fname = op.join(data_path, 'test_elp.txt')
+    headshape_fname = op.join(data_path, 'test_hsp.txt')
+
+    raw_to_bids(subject_id=subject_id, session_id=session_id, run=run,
+                task=task, raw_fname=raw_fname, events_fname=events_fname,
+                event_id=event_id, hpi=hpi_fname, electrode=electrode_fname,
+                hsp=headshape_fname, output_path=output_path,
+                overwrite=True)
+
+
+def test_ctf():
+    """
+    Testing the functionality of the raw_to_bids conversion for CTF data.
+    """
+    output_path = _TempDir()
+    data_path = op.join(testing.data_path(download=False), 'CTF')
+    raw_fname = op.join(data_path, 'testdata_ctf.ds')
+
+    raw_to_bids(subject_id=subject_id, session_id=session_id, run=run,
+                task=task, raw_fname=raw_fname, output_path=output_path,
+                overwrite=True)
+
+
+def test_bti():
+    """
+    Testing the functionality of the raw_to_bids conversion for BTi data.
+    """
+    output_path = _TempDir()
+    data_path = op.join(base_path, 'bti', 'tests', 'data')
+    raw_fname = op.join(data_path, 'test_pdf_linux')
+    config_fname = op.join(data_path, 'test_config_linux')
+    headshape_fname = op.join(data_path, 'test_hs_linux')
+
+    raw_to_bids(subject_id=subject_id, session_id=session_id, run=run,
+                task=task, raw_fname=raw_fname, config=config_fname,
+                hsp=headshape_fname, output_path=output_path,
+                overwrite=True)