Repository: mne-tools/mne-python

Issue:
example fails with TypeError on Windows, 32/64 bitness issue
Trying to run plot_topo_compare_conditions on a Windows machine, it bombs with:

```
...
  File "C:\Users\Vicon123\projects\mne-python\mne\epochs.py", line 829, in _name
    count = np.bincount(self.events[:, 2])

TypeError: Cannot cast array data from dtype('>u4') to dtype('int32') according to the rule 'safe'
```

It looks like events is created as 64-bit unsigned ints, which numpy refuses to cast to 32 bit.
Probably caused by running a 32-bit Python interpreter on a 64-bit machine (I don't have 64-bit Python handy to test with)





Discussion:
windows ints are 32 bits even on win64

can it be tested with appveyor?

I think we can test 32 bit there, but I doubt that the environment file
will work (although we can try)

is it really a win32 issue?

In that example we load the events from disk. Maybe that ends up being the problem. @jjnurminen what is the full traceback?
```
Current compensation grade : 0
288 matching events found
Created an SSP operator (subspace dimension = 3)
4 projection items activated
    Rejecting  epoch based on MAG : [u'MEG 1711']
Traceback (most recent call last):

  File "<ipython-input-2-e18713e46c19>", line 1, in <module>
    runfile('C:/Users/Vicon123/projects/mne-python/examples/visualization/plot_topo_compare_conditions.py', wdir='C:/Users/Vicon123/projects/mne-python/examples/visualization')

  File "C:\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Vicon123/projects/mne-python/examples/visualization/plot_topo_compare_conditions.py", line 58, in <module>
    evokeds = [epochs[name].average() for name in ('left', 'right')]

  File "C:\Users\Vicon123\projects\mne-python\mne\epochs.py", line 751, in average
    return self._compute_mean_or_stderr(picks, 'ave')

  File "C:\Users\Vicon123\projects\mne-python\mne\epochs.py", line 820, in _compute_mean_or_stderr
    kind, self._name)

  File "C:\Users\Vicon123\projects\mne-python\mne\epochs.py", line 828, in _name
    count = np.bincount(self.events[:, 2])

TypeError: Cannot cast array data from dtype('>u4') to dtype('int32') according to the rule 'safe'
```
what if you change the dtype of self.events something like

self.events = self.events.astype(int)

or anything that works.

I can't replicate on Windows 10. Which Windows version are you on?

Resolving patch:
diff --git a/mne/epochs.py b/mne/epochs.py
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -260,9 +260,10 @@ def __init__(self, info, data, events, event_id=None, tmin=-0.2, tmax=0.5,
         del event_id
 
         if events is not None:  # RtEpochs can have events=None
-
+            events = np.asarray(events)
             if events.dtype.kind not in ['i', 'u']:
                 raise ValueError('events must be an array of type int')
+            events = events.astype(int)
             if events.ndim != 2 or events.shape[1] != 3:
                 raise ValueError('events must be 2D with 3 columns')
             for key, val in self.event_id.items():
diff --git a/mne/event.py b/mne/event.py
--- a/mne/event.py
+++ b/mne/event.py
@@ -243,14 +243,14 @@ def read_events(filename, include=None, exclude=None, mask=None,
     ext = splitext(filename)[1].lower()
     if ext == '.fif' or ext == '.gz':
         fid, tree, _ = fiff_open(filename)
-        try:
-            event_list, _ = _read_events_fif(fid, tree)
-        finally:
-            fid.close()
+        with fid as f:
+            event_list, _ = _read_events_fif(f, tree)
+        # hack fix for windows to avoid bincount problems
+        event_list = event_list.astype(int)
     else:
         #  Have to read this in as float64 then convert because old style
         #  eve/lst files had a second float column that will raise errors
-        lines = np.loadtxt(filename, dtype=np.float64).astype(np.uint32)
+        lines = np.loadtxt(filename, dtype=np.float64).astype(int)
         if len(lines) == 0:
             raise ValueError('No text lines found')
 


Test patch: