Repository: mne-tools/mne-python

Issue:
Issues loading fieldtrip data with MNE python
Sorry, I did not know where else I could ask this:
I am having challenges loading fieldtrip data with MNE python. When I enter the code below

```
import mne
original_data = mne.io.read_raw_fif('data.fif', preload=False)
original_info = original_data.info
data_from_ft = mne.read_epochs_fieldtrip('data.mat', original_info)
```
I get this error:

`TypeError: only size-1 arrays can be converted to Python scalars `


Discussion:
Can you paste the full traceback? From just the last line it's hard to tell where the error comes from
> Sorry, I did not know where else I could ask this

FYI, MNE-Python's website has a [how to get help page](https://mne.tools/stable/overview/get_help.html) that provides links to [our mailing list](http://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis) and our [gitter channel](https://gitter.im/mne-tools/mne-python).
`<ipython-input-2-ff985abfd0ca>:1: RuntimeWarning: This filename (../data/ML_Subject08_P2_tsss_mc.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz or _meg.fif
  original_data = mne.io.read_raw_fif('../data/ML_Subject08_P2_tsss_mc.fif', preload=False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-ff985abfd0ca> in <module>
      1 original_data = mne.io.read_raw_fif('../data/ML_Subject08_P2_tsss_mc.fif', preload=False)
      2 original_info = original_data.info
----> 3 data_from_ft = mne.read_epochs_fieldtrip('../data/dat_ft.mat', original_info)

~/miniconda3/envs/srtt/lib/python3.8/site-packages/mne/io/fieldtrip/fieldtrip.py in read_epochs_fieldtrip(fname, info, data_name, trialinfo_column)
    118     _validate_ft_struct(ft_struct)
    119 
--> 120     info = _create_info(ft_struct, info)  # create info structure
    121     data = np.array(ft_struct['trial'])  # create the epochs data array
    122     events = _create_events(ft_struct, trialinfo_column)

~/miniconda3/envs/srtt/lib/python3.8/site-packages/mne/io/fieldtrip/utils.py in _create_info(ft_struct, raw_info)
     72         info['sfreq'] = sfreq
     73         ch_idx = [info['ch_names'].index(ch) for ch in ch_names]
---> 74         pick_info(info, ch_idx, copy=False)
     75     else:
     76         montage = _create_montage(ft_struct)

<decorator-gen-8> in pick_info(info, sel, copy, verbose)

~/miniconda3/envs/srtt/lib/python3.8/site-packages/mne/io/pick.py in pick_info(info, sel, copy, verbose)
    468     from .meas_info import _bad_chans_comp
    469 
--> 470     info._check_consistency()
    471     info = info.copy() if copy else info
    472     if sel is None:

~/miniconda3/envs/srtt/lib/python3.8/site-packages/mne/io/meas_info.py in _check_consistency(self, prepend_error)
    730         for key in ('sfreq', 'highpass', 'lowpass'):
    731             if self.get(key) is not None:
--> 732                 self[key] = float(self[key])
    733 
    734         # Ensure info['chs'] has immutable entries (copies much faster)

TypeError: only size-1 arrays can be converted to Python scalars`

Resolving patch:
diff --git a/mne/io/fieldtrip/fieldtrip.py b/mne/io/fieldtrip/fieldtrip.py
--- a/mne/io/fieldtrip/fieldtrip.py
+++ b/mne/io/fieldtrip/fieldtrip.py
@@ -43,7 +43,7 @@ def read_raw_fieldtrip(fname, info, data_name='data'):
     raw : instance of RawArray
         A Raw Object containing the loaded data.
     """
-    from ...externals.pymatreader.pymatreader import read_mat
+    from ...externals.pymatreader import read_mat
 
     ft_struct = read_mat(fname,
                          ignore_fields=['previous'],
@@ -107,7 +107,7 @@ def read_epochs_fieldtrip(fname, info, data_name='data',
     epochs : instance of EpochsArray
         An EpochsArray containing the loaded data.
     """
-    from ...externals.pymatreader.pymatreader import read_mat
+    from ...externals.pymatreader import read_mat
     ft_struct = read_mat(fname,
                          ignore_fields=['previous'],
                          variable_names=[data_name])
@@ -164,7 +164,7 @@ def read_evoked_fieldtrip(fname, info, comment=None,
     evoked : instance of EvokedArray
         An EvokedArray containing the loaded data.
     """
-    from ...externals.pymatreader.pymatreader import read_mat
+    from ...externals.pymatreader import read_mat
     ft_struct = read_mat(fname,
                          ignore_fields=['previous'],
                          variable_names=[data_name])
diff --git a/mne/io/fieldtrip/utils.py b/mne/io/fieldtrip/utils.py
--- a/mne/io/fieldtrip/utils.py
+++ b/mne/io/fieldtrip/utils.py
@@ -203,12 +203,18 @@ def _set_sfreq(ft_struct):
         sfreq = ft_struct['fsample']
     except KeyError:
         try:
-            t1 = ft_struct['time'][0]
-            t2 = ft_struct['time'][1]
-            difference = abs(t1 - t2)
-            sfreq = 1 / difference
+            time = ft_struct['time']
         except KeyError:
             raise ValueError('No Source for sfreq found')
+        else:
+            t1, t2 = float(time[0]), float(time[1])
+            sfreq = 1 / (t2 - t1)
+    try:
+        sfreq = float(sfreq)
+    except TypeError:
+        warn('FieldTrip structure contained multiple sample rates, trying the '
+             f'first of:\n{sfreq} Hz')
+        sfreq = float(sfreq.ravel()[0])
     return sfreq
 
 


Test patch:
diff --git a/mne/io/fieldtrip/tests/test_fieldtrip.py b/mne/io/fieldtrip/tests/test_fieldtrip.py
--- a/mne/io/fieldtrip/tests/test_fieldtrip.py
+++ b/mne/io/fieldtrip/tests/test_fieldtrip.py
@@ -86,7 +86,7 @@ def test_read_evoked(cur_system, version, use_info):
 # byte order '<' via buffer interface"
 @pytest.mark.skipif(os.getenv('AZURE_CI_WINDOWS', 'false').lower() == 'true',
                     reason='Pandas problem on Azure CI')
-def test_read_epochs(cur_system, version, use_info):
+def test_read_epochs(cur_system, version, use_info, monkeypatch):
     """Test comparing reading an Epochs object and the FieldTrip version."""
     pandas = _check_pandas_installed(strict=False)
     has_pandas = pandas is not False
@@ -127,6 +127,19 @@ def test_read_epochs(cur_system, version, use_info):
     check_data(mne_data, ft_data, cur_system)
     check_info_fields(mne_epoched, epoched_ft, use_info)
 
+    # weird sfreq
+    from mne.externals.pymatreader import read_mat
+
+    def modify_mat(fname, variable_names=None, ignore_fields=None):
+        out = read_mat(fname, variable_names, ignore_fields)
+        if 'fsample' in out['data']:
+            out['data']['fsample'] = np.repeat(out['data']['fsample'], 2)
+        return out
+
+    monkeypatch.setattr(mne.externals.pymatreader, 'read_mat', modify_mat)
+    with pytest.warns(RuntimeWarning, match='multiple'):
+        mne.io.read_epochs_fieldtrip(cur_fname, info)
+
 
 @testing.requires_testing_data
 # Reading the sample CNT data results in a RuntimeWarning because it cannot