braindecode 0.1.4.7__tar.gz → 0.1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/PKG-INFO +10 -6
  2. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/SOURCES.txt +4 -0
  3. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/requires.txt +3 -2
  4. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/PKG-INFO +10 -6
  5. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/README.rst +9 -5
  6. Braindecode-0.1.5/braindecode/datasets/__init__.py +3 -0
  7. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/datasets/bbci.py +75 -13
  8. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/datasets/multiple.py +4 -1
  9. Braindecode-0.1.5/braindecode/datautil/__init__.py +3 -0
  10. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/datautil/iterators.py +146 -58
  11. Braindecode-0.1.5/braindecode/datautil/signal_target.py +38 -0
  12. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/datautil/signalproc.py +74 -3
  13. Braindecode-0.1.5/braindecode/datautil/splitters.py +74 -0
  14. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/datautil/trial_segment.py +117 -25
  15. Braindecode-0.1.5/braindecode/datautil/util.py +37 -0
  16. Braindecode-0.1.5/braindecode/experiments/__init__.py +3 -0
  17. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/experiments/experiment.py +187 -1
  18. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/experiments/monitors.py +132 -10
  19. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/experiments/stopcriteria.py +51 -2
  20. Braindecode-0.1.5/braindecode/mne_ext/__init__.py +3 -0
  21. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/mne_ext/signalproc.py +37 -5
  22. Braindecode-0.1.5/braindecode/models/__init__.py +3 -0
  23. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/models/deep4.py +12 -0
  24. Braindecode-0.1.5/braindecode/models/eegnet.py +111 -0
  25. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/models/shallow_fbcsp.py +12 -0
  26. Braindecode-0.1.5/braindecode/models/util.py +39 -0
  27. Braindecode-0.1.5/braindecode/torch_ext/__init__.py +3 -0
  28. Braindecode-0.1.5/braindecode/torch_ext/constraints.py +27 -0
  29. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/torch_ext/functions.py +1 -1
  30. Braindecode-0.1.5/braindecode/torch_ext/init.py +23 -0
  31. Braindecode-0.1.5/braindecode/torch_ext/losses.py +31 -0
  32. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/torch_ext/modules.py +12 -2
  33. Braindecode-0.1.5/braindecode/torch_ext/util.py +42 -0
  34. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/util.py +0 -42
  35. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/Makefile +2 -2
  36. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/conf.py +54 -5
  37. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/index.rst +15 -1
  38. Braindecode-0.1.5/docs/notebooks/BBCI_Data.ipynb +933 -0
  39. Braindecode-0.1.5/docs/notebooks/BBCI_Data_Start_Stop.ipynb +842 -0
  40. Braindecode-0.1.5/docs/notebooks/BBCI_Data_Trialwise.ipynb +820 -0
  41. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/notebooks/Cropped_Decoding.ipynb +137 -103
  42. Braindecode-0.1.5/docs/notebooks/Experiment_Class.ipynb +807 -0
  43. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/notebooks/TrialWise_Decoding.ipynb +216 -141
  44. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.datasets.rst +5 -7
  45. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.datautil.rst +5 -7
  46. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.experiments.rst +5 -7
  47. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.mne_ext.rst +5 -7
  48. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.models.rst +18 -4
  49. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.rst +5 -7
  50. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/braindecode.torch_ext.rst +13 -7
  51. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/setup.py +3 -2
  52. Braindecode-0.1.4.7/braindecode/datasets/__init__.py +0 -0
  53. Braindecode-0.1.4.7/braindecode/datautil/__init__.py +0 -0
  54. Braindecode-0.1.4.7/braindecode/datautil/signal_target.py +0 -20
  55. Braindecode-0.1.4.7/braindecode/datautil/splitters.py +0 -33
  56. Braindecode-0.1.4.7/braindecode/datautil/util.py +0 -2
  57. Braindecode-0.1.4.7/braindecode/experiments/__init__.py +0 -0
  58. Braindecode-0.1.4.7/braindecode/mne_ext/__init__.py +0 -0
  59. Braindecode-0.1.4.7/braindecode/models/__init__.py +0 -0
  60. Braindecode-0.1.4.7/braindecode/torch_ext/__init__.py +0 -0
  61. Braindecode-0.1.4.7/braindecode/torch_ext/constraints.py +0 -13
  62. Braindecode-0.1.4.7/braindecode/torch_ext/losses.py +0 -11
  63. Braindecode-0.1.4.7/braindecode/torch_ext/util.py +0 -33
  64. Braindecode-0.1.4.7/docs/notebooks/BBCI_Data.ipynb +0 -683
  65. Braindecode-0.1.4.7/docs/notebooks/BBCI_Data_Start_Stop.ipynb +0 -877
  66. Braindecode-0.1.4.7/docs/notebooks/Experiment_Class.ipynb +0 -819
  67. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/dependency_links.txt +0 -0
  68. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/not-zip-safe +0 -0
  69. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/Braindecode.egg-info/top_level.txt +0 -0
  70. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/LICENSE.txt +0 -0
  71. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/MANIFEST.in +0 -0
  72. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/braindecode/__init__.py +0 -0
  73. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/docs/source/modules.rst +0 -0
  74. {Braindecode-0.1.4.7 → Braindecode-0.1.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.1
2
2
  Name: Braindecode
3
- Version: 0.1.4.7
3
+ Version: 0.1.5
4
4
  Summary: A deep learning toolbox to decode raw time-domain EEG.
5
5
  Home-page: https://github.com/robintibor/braindecode
6
6
  Author: Robin Tibor Schirrmeister
@@ -12,14 +12,17 @@ Description: Note: The old braindecode repository has been moved to
12
12
  Braindecode
13
13
  ===========
14
14
 
15
- A deep learning for raw time-domain EEG decoding toolbox.
15
+ **Under construction**
16
+
17
+ A deep learning toolbox to decode raw time-domain EEG.
18
+
16
19
 
17
20
  Installation
18
21
  ============
19
22
 
20
- First, install pytorch from http://pytorch.org/.
23
+ 1. Install pytorch from http://pytorch.org/ (you don't need to install torchvision).
21
24
 
22
- Second install newest version of python-mne:
25
+ 2. Install newest version of python-mne:
23
26
 
24
27
  .. code-block:: bash
25
28
 
@@ -28,17 +31,18 @@ Description: Note: The old braindecode repository has been moved to
28
31
  python setup.py install
29
32
 
30
33
 
31
- Then, install braindecode via pip:
34
+ 3. Install braindecode via pip:
32
35
 
33
36
  .. code-block:: bash
34
37
 
35
38
  pip install braindecode
36
39
 
37
40
 
41
+
38
42
  Documentation
39
43
  =============
40
44
 
41
- Documentation is online under http://braindecode.readthedocs.io/
45
+ Documentation is online under https://robintibor.github.io/braindecode/
42
46
 
43
47
  Keywords: eeg deep-learning brain-state-decoding
44
48
  Platform: UNKNOWN
@@ -28,10 +28,13 @@ braindecode/mne_ext/__init__.py
28
28
  braindecode/mne_ext/signalproc.py
29
29
  braindecode/models/__init__.py
30
30
  braindecode/models/deep4.py
31
+ braindecode/models/eegnet.py
31
32
  braindecode/models/shallow_fbcsp.py
33
+ braindecode/models/util.py
32
34
  braindecode/torch_ext/__init__.py
33
35
  braindecode/torch_ext/constraints.py
34
36
  braindecode/torch_ext/functions.py
37
+ braindecode/torch_ext/init.py
35
38
  braindecode/torch_ext/losses.py
36
39
  braindecode/torch_ext/modules.py
37
40
  braindecode/torch_ext/util.py
@@ -40,6 +43,7 @@ docs/conf.py
40
43
  docs/index.rst
41
44
  docs/notebooks/BBCI_Data.ipynb
42
45
  docs/notebooks/BBCI_Data_Start_Stop.ipynb
46
+ docs/notebooks/BBCI_Data_Trialwise.ipynb
43
47
  docs/notebooks/Cropped_Decoding.ipynb
44
48
  docs/notebooks/Experiment_Class.ipynb
45
49
  docs/notebooks/TrialWise_Decoding.ipynb
@@ -1,7 +1,8 @@
1
1
  mne
2
- torch
3
- h5py
4
2
  numpy
5
3
  pandas
4
+ scipy
6
5
  resampy
7
6
  matplotlib
7
+ h5py
8
+ torch
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.1
2
2
  Name: Braindecode
3
- Version: 0.1.4.7
3
+ Version: 0.1.5
4
4
  Summary: A deep learning toolbox to decode raw time-domain EEG.
5
5
  Home-page: https://github.com/robintibor/braindecode
6
6
  Author: Robin Tibor Schirrmeister
@@ -12,14 +12,17 @@ Description: Note: The old braindecode repository has been moved to
12
12
  Braindecode
13
13
  ===========
14
14
 
15
- A deep learning for raw time-domain EEG decoding toolbox.
15
+ **Under construction**
16
+
17
+ A deep learning toolbox to decode raw time-domain EEG.
18
+
16
19
 
17
20
  Installation
18
21
  ============
19
22
 
20
- First, install pytorch from http://pytorch.org/.
23
+ 1. Install pytorch from http://pytorch.org/ (you don't need to install torchvision).
21
24
 
22
- Second install newest version of python-mne:
25
+ 2. Install newest version of python-mne:
23
26
 
24
27
  .. code-block:: bash
25
28
 
@@ -28,17 +31,18 @@ Description: Note: The old braindecode repository has been moved to
28
31
  python setup.py install
29
32
 
30
33
 
31
- Then, install braindecode via pip:
34
+ 3. Install braindecode via pip:
32
35
 
33
36
  .. code-block:: bash
34
37
 
35
38
  pip install braindecode
36
39
 
37
40
 
41
+
38
42
  Documentation
39
43
  =============
40
44
 
41
- Documentation is online under http://braindecode.readthedocs.io/
45
+ Documentation is online under https://robintibor.github.io/braindecode/
42
46
 
43
47
  Keywords: eeg deep-learning brain-state-decoding
44
48
  Platform: UNKNOWN
@@ -4,14 +4,17 @@ https://github.com/robintibor/braindevel.
4
4
  Braindecode
5
5
  ===========
6
6
 
7
- A deep learning for raw time-domain EEG decoding toolbox.
7
+ **Under construction**
8
+
9
+ A deep learning toolbox to decode raw time-domain EEG.
10
+
8
11
 
9
12
  Installation
10
13
  ============
11
14
 
12
- First, install pytorch from http://pytorch.org/.
15
+ 1. Install pytorch from http://pytorch.org/ (you don't need to install torchvision).
13
16
 
14
- Second install newest version of python-mne:
17
+ 2. Install newest version of python-mne:
15
18
 
16
19
  .. code-block:: bash
17
20
 
@@ -20,14 +23,15 @@ Second install newest version of python-mne:
20
23
  python setup.py install
21
24
 
22
25
 
23
- Then, install braindecode via pip:
26
+ 3. Install braindecode via pip:
24
27
 
25
28
  .. code-block:: bash
26
29
 
27
30
  pip install braindecode
28
31
 
29
32
 
33
+
30
34
  Documentation
31
35
  =============
32
36
 
33
- Documentation is online under http://braindecode.readthedocs.io/
37
+ Documentation is online under https://robintibor.github.io/braindecode/
@@ -0,0 +1,3 @@
1
+ """
2
+ Loader code for some datasets.
3
+ """
@@ -11,18 +11,34 @@ log = logging.getLogger(__name__)
11
11
 
12
12
 
13
13
  class BBCIDataset(object):
14
+ """
15
+ Loader class for files created by saving BBCI files in matlab (make
16
+ sure to save with '-v7.3' in matlab, see
17
+ https://de.mathworks.com/help/matlab/import_export/mat-file-versions.html#buk6i87
18
+ )
19
+
20
+ Parameters
21
+ ----------
22
+ filename: str
23
+ load_sensor_names: list of str, optional
24
+ Also speeds up loading if you only load some sensors.
25
+ None means load all sensors.
26
+ check_class_names: bool, optional
27
+ check if the class names are part of some known class names at
28
+ Translational NeuroTechnology Lab, AG Ball, Freiburg, Germany.
29
+ """
14
30
  def __init__(self, filename, load_sensor_names=None, check_class_names=False):
15
31
  self.__dict__.update(locals())
16
32
  del self.self
17
33
 
18
34
  def load(self):
19
- cnt = self.load_continuous_signal()
20
- cnt = self.add_markers(cnt)
35
+ cnt = self._load_continuous_signal()
36
+ cnt = self._add_markers(cnt)
21
37
  return cnt
22
38
 
23
- def load_continuous_signal(self):
24
- wanted_chan_inds, wanted_sensor_names = self.determine_sensors()
25
- fs = self.determine_samplingrate()
39
+ def _load_continuous_signal(self):
40
+ wanted_chan_inds, wanted_sensor_names = self._determine_sensors()
41
+ fs = self._determine_samplingrate()
26
42
  with h5py.File(self.filename, 'r') as h5file:
27
43
  samples = int(h5file['nfo']['T'][0, 0])
28
44
  cnt_signal_shape = (samples, len(wanted_chan_inds))
@@ -48,7 +64,7 @@ class BBCIDataset(object):
48
64
  cnt = mne.io.RawArray(continuous_signal.T, info)
49
65
  return cnt
50
66
 
51
- def determine_sensors(self):
67
+ def _determine_sensors(self):
52
68
  all_sensor_names = self.get_all_sensors(self.filename, pattern=None)
53
69
  if self.load_sensor_names is None:
54
70
 
@@ -71,11 +87,11 @@ class BBCIDataset(object):
71
87
  len(eeg_sensor_names) == 16), (
72
88
  "Recheck this code if you have different sensors...")
73
89
  self.load_sensor_names = eeg_sensor_names
74
- chan_inds = self.determine_chan_inds(all_sensor_names,
75
- self.load_sensor_names)
90
+ chan_inds = self._determine_chan_inds(all_sensor_names,
91
+ self.load_sensor_names)
76
92
  return chan_inds, self.load_sensor_names
77
93
 
78
- def determine_samplingrate(self):
94
+ def _determine_samplingrate(self):
79
95
  with h5py.File(self.filename, 'r') as h5file:
80
96
  fs = h5file['dat']['fs'][0, 0]
81
97
  assert isinstance(fs, int) or fs.is_integer()
@@ -83,7 +99,7 @@ class BBCIDataset(object):
83
99
  return fs
84
100
 
85
101
  @staticmethod
86
- def determine_chan_inds(all_sensor_names, sensor_names):
102
+ def _determine_chan_inds(all_sensor_names, sensor_names):
87
103
  assert sensor_names is not None
88
104
  chan_inds = [all_sensor_names.index(s) for s in sensor_names]
89
105
  assert len(chan_inds) == len(sensor_names), ("All"
@@ -94,6 +110,20 @@ class BBCIDataset(object):
94
110
 
95
111
  @staticmethod
96
112
  def get_all_sensors(filename, pattern=None):
113
+ """
114
+ Get all sensors that exist in the given file.
115
+
116
+ Parameters
117
+ ----------
118
+ filename: str
119
+ pattern: str, optional
120
+ Only return those sensor names that match the given pattern.
121
+
122
+ Returns
123
+ -------
124
+ sensor_names: list of str
125
+ Sensor names that match the pattern or all sensor names in the file.
126
+ """
97
127
  with h5py.File(filename, 'r') as h5file:
98
128
  clab_set = h5file['dat']['clab'][:].squeeze()
99
129
  all_sensor_names = [''.join(chr(c) for c in h5file[obj_ref]) for
@@ -104,7 +134,7 @@ class BBCIDataset(object):
104
134
  all_sensor_names)
105
135
  return all_sensor_names
106
136
 
107
- def add_markers(self, cnt):
137
+ def _add_markers(self, cnt):
108
138
  with h5py.File(self.filename, 'r') as h5file:
109
139
  event_times_in_ms = h5file['mrk']['time'][:].squeeze()
110
140
  event_classes = h5file['mrk']['event']['desc'][:].squeeze()
@@ -115,7 +145,7 @@ class BBCIDataset(object):
115
145
  for obj_ref in class_name_set]
116
146
 
117
147
  if self.check_class_names:
118
- check_class_names(
148
+ _check_class_names(
119
149
  all_class_names, event_times_in_ms, event_classes,)
120
150
 
121
151
  event_times_in_samples = event_times_in_ms * cnt.info['sfreq'] / 1000.0
@@ -151,7 +181,20 @@ class BBCIDataset(object):
151
181
  return cnt
152
182
 
153
183
 
154
- def check_class_names(all_class_names, event_times_in_ms, event_classes,):
184
+ def _check_class_names(all_class_names, event_times_in_ms, event_classes, ):
185
+ """
186
+ Checks if the class names are part of some known class names used in
187
+ translational neurotechnology lab, AG Ball, Freiburg.
188
+
189
+ Logs warning in case class names are not known.
190
+
191
+ Parameters
192
+ ----------
193
+ all_class_names: list of str
194
+ event_times_in_ms: list of number
195
+ event_classes: list of number
196
+
197
+ """
155
198
  if all_class_names == ['Right Hand', 'Left Hand', 'Rest', 'Feet']:
156
199
  pass
157
200
  elif ((all_class_names == ['1', '10', '11', '111', '12', '13',
@@ -176,6 +219,8 @@ def check_class_names(all_class_names, event_times_in_ms, event_classes,):
176
219
  event_classes[left_mask] = 2
177
220
  event_classes[rest_mask] = 3
178
221
  event_classes[feet_mask] = 4
222
+ log.warn("Swapped class names {:s}... might cause problems...".format(
223
+ all_class_names))
179
224
  elif all_class_names == ['Right Hand Start', 'Left Hand Start',
180
225
  'Rest Start', 'Feet Start',
181
226
  'Right Hand End',
@@ -320,7 +365,24 @@ def check_class_names(all_class_names, event_times_in_ms, event_classes,):
320
365
  log.warn("Unknown class names {:s}".format(
321
366
  all_class_names))
322
367
 
368
+
323
369
  def load_bbci_sets_from_folder(folder, runs='all'):
370
+ """
371
+ Load bbci datasets from files in given folder.
372
+
373
+ Parameters
374
+ ----------
375
+ folder: str
376
+ Folder with .BBCI.mat files inside
377
+ runs: list of int
378
+ If you only want to load specific runs.
379
+ Assumes filenames with such kind of part: S001R02 for Run 2.
380
+ Tries to match this regex: ``'S[0-9]{3,3}R[0-9]{2,2}_'``.
381
+
382
+ Returns
383
+ -------
384
+
385
+ """
324
386
  bbci_mat_files = sorted(glob(os.path.join(folder, '*.BBCI.mat')))
325
387
  if runs != 'all':
326
388
  file_run_numbers = [int(re.search('S[0-9]{3,3}R[0-9]{2,2}_', f).group()[5:7])
@@ -9,11 +9,14 @@ log = logging.getLogger(__name__)
9
9
  class MultipleSetLoader(object):
10
10
  """
11
11
  Class to load multiple sets.
12
- Resamples sets down as necessary.
12
+
13
+ Resamples individual sets down to lowest common sampling frequency,
14
+ if necessary.
13
15
 
14
16
  Parameters
15
17
  ----------
16
18
  set_loaders: object with load method: () -> MNE Raw object
19
+ The individual set loaders.
17
20
  """
18
21
 
19
22
  def __init__(self, set_loaders):
@@ -0,0 +1,3 @@
1
+ """
2
+ Utilities for data manipulation.
3
+ """
@@ -4,7 +4,8 @@ from numpy.random import RandomState
4
4
 
5
5
  def get_balanced_batches(n_trials, rng, shuffle, n_batches=None,
6
6
  batch_size=None):
7
- """Create indices for batches balanced in size (batches will have maximum size difference of 1).
7
+ """Create indices for batches balanced in size
8
+ (batches will have maximum size difference of 1).
8
9
  Supply either batch size or number of batches. Resulting batches
9
10
  will not have the given batch size but rather the next largest batch size
10
11
  that allows to split the set into balanced batches (maximum size difference 1).
@@ -13,14 +14,12 @@ def get_balanced_batches(n_trials, rng, shuffle, n_batches=None,
13
14
  ----------
14
15
  n_trials : int
15
16
  Size of set.
16
- rng :
17
+ rng : RandomState
17
18
 
18
- shuffle :
19
+ shuffle : bool
19
20
  Whether to shuffle indices before splitting set.
20
- n_batches :
21
- (Default value = None)
22
- batch_size :
23
- (Default value = None)
21
+ n_batches : int, optional
22
+ batch_size : int, optional
24
23
 
25
24
  Returns
26
25
  -------
@@ -89,28 +88,89 @@ class BalancedBatchSizeIterator(object):
89
88
  self.rng = RandomState(328774)
90
89
 
91
90
 
91
+ class ClassBalancedBatchSizeIterator(object):
92
+ """
93
+ Create batches of balanced size, that are also balanced per class, i.e.
94
+ each class should be sampled roughly with the same frequency during
95
+ training.
96
+
97
+ Parameters
98
+ ----------
99
+ batch_size: int
100
+ Resulting batches will not necessarily have the given batch size
101
+ but rather the next largest batch size that allows to split the set into
102
+ balanced batches (maximum size difference 1).
103
+ """
104
+
105
+ def __init__(self, batch_size):
106
+ self.batch_size = batch_size
107
+ self.rng = RandomState(328774)
108
+
109
+ def get_batches(self, dataset, shuffle):
110
+ n_trials = dataset.X.shape[0]
111
+ batches = get_balanced_batches(n_trials,
112
+ batch_size=self.batch_size,
113
+ rng=self.rng,
114
+ shuffle=shuffle)
115
+ if shuffle:
116
+ n_classes = np.max(dataset.y) + 1
117
+ class_probabilities = [np.mean(dataset.y == i_class)
118
+ for i_class in range(n_classes)]
119
+ class_probabilities = np.array(class_probabilities)
120
+ # choose trials in inverse probability of class
121
+ trial_probabilities = [1.0 / class_probabilities[y]
122
+ for y in dataset.y]
123
+ trial_probabilities = np.array(trial_probabilities) / np.sum(
124
+ trial_probabilities)
125
+ i_trial_to_balanced = self.rng.choice(n_trials, n_trials,
126
+ p=trial_probabilities)
127
+
128
+ for batch_inds in batches:
129
+ if shuffle:
130
+ batch_inds = [i_trial_to_balanced[i_trial] for i_trial in
131
+ batch_inds]
132
+ batch_X = dataset.X[batch_inds]
133
+ batch_y = dataset.y[batch_inds]
134
+
135
+ # add empty fourth dimension if necessary
136
+ if batch_X.ndim == 3:
137
+ batch_X = batch_X[:, :, :, None]
138
+ yield (batch_X, batch_y)
139
+
140
+ def reset_rng(self):
141
+ self.rng = RandomState((4, 7, 2017))
142
+
143
+
92
144
  class CropsFromTrialsIterator(object):
93
- def __init__(self, batch_size, input_time_length, n_preds_per_input,
94
- check_preds_smaller_trial_len=True):
95
- # TODO: remove this check preds smaller trila len and check that
96
- # trials are smaller than input length
97
- """
98
-
99
- Parameters
100
- ----------
101
- batch_size: int
102
- input_time_length: int
103
- Input time length of the ConvNet, determines size of batches in
104
- 3rd dimension.
105
- n_preds_per_input: int
106
- Number of predictions ConvNet makes per one input. Can be computed
107
- by making a forward pass with the given input time length, the
108
- output length in 3rd dimension is n_preds_per_input.
109
- """
145
+ """
146
+ Iterator sampling crops out the trials so that each sample
147
+ (after receptive size of the ConvNet) in each trial is predicted.
148
+
149
+ Predicting the given input batches can lead to some samples
150
+ being predicted multiple times, if the receptive field size
151
+ (input_time_length - n_preds_per_input + 1) is not a divisor
152
+ of the trial length. :func:`compute_preds_per_trial_for_set`
153
+ can help with removing the overlapped predictions again for evaluation.
154
+
155
+ Parameters
156
+ ----------
157
+ batch_size: int
158
+ input_time_length: int
159
+ Input time length of the ConvNet, determines size of batches in
160
+ 3rd dimension.
161
+ n_preds_per_input: int
162
+ Number of predictions ConvNet makes per one input. Can be computed
163
+ by making a forward pass with the given input time length, the
164
+ output length in 3rd dimension is n_preds_per_input.
165
+
166
+ See Also
167
+ --------
168
+ braindecode.experiments.monitors.compute_preds_per_trial_for_set : Assigns predictions to trials, removes overlaps.
169
+ """
170
+ def __init__(self, batch_size, input_time_length, n_preds_per_input):
110
171
  self.batch_size = batch_size
111
172
  self.input_time_length = input_time_length
112
173
  self.n_preds_per_input = n_preds_per_input
113
- self.check_preds_smaller_trial_len = check_preds_smaller_trial_len
114
174
  self.rng = RandomState((2017, 6, 28))
115
175
 
116
176
  def reset_rng(self):
@@ -123,21 +183,25 @@ class CropsFromTrialsIterator(object):
123
183
  i_trial_starts = [n_receptive_field - 1] * len(dataset.X)
124
184
  i_trial_stops = [trial.shape[1] for trial in dataset.X]
125
185
 
126
- if self.check_preds_smaller_trial_len:
127
- check_trial_bounds(i_trial_starts, i_trial_stops,
128
- self.n_preds_per_input)
129
- start_stop_blocks_per_trial = compute_start_stop_block_inds(
186
+ # Check whether input lengths ok
187
+ input_lens = i_trial_stops
188
+ for i_trial, input_len in enumerate(input_lens):
189
+ assert input_len >= self.input_time_length, (
190
+ "Input length {:d} of trial {:d} is smaller than the "
191
+ "input time length {:d}".format(input_len, i_trial,
192
+ self.input_time_length))
193
+ start_stop_blocks_per_trial = _compute_start_stop_block_inds(
130
194
  i_trial_starts, i_trial_stops, self.input_time_length,
131
- self.n_preds_per_input, self.check_preds_smaller_trial_len)
195
+ self.n_preds_per_input, check_preds_smaller_trial_len=True)
132
196
  for i_trial, trial_blocks in enumerate(start_stop_blocks_per_trial):
133
197
  assert trial_blocks[0][0] == 0
134
198
  assert trial_blocks[-1][1] == i_trial_stops[i_trial]
135
199
 
136
- return self.yield_block_batches(dataset.X, dataset.y,
200
+ return self._yield_block_batches(dataset.X, dataset.y,
137
201
  start_stop_blocks_per_trial,
138
202
  shuffle=shuffle)
139
203
 
140
- def yield_block_batches(self, X, y, start_stop_blocks_per_trial, shuffle):
204
+ def _yield_block_batches(self, X, y, start_stop_blocks_per_trial, shuffle):
141
205
  # add trial nr to start stop blocks and flatten at same time
142
206
  i_trial_start_stop_block = [(i_trial, start, stop)
143
207
  for i_trial, block in
@@ -146,8 +210,6 @@ class CropsFromTrialsIterator(object):
146
210
  i_trial_start_stop_block = np.array(i_trial_start_stop_block)
147
211
  if i_trial_start_stop_block.ndim == 1:
148
212
  i_trial_start_stop_block = i_trial_start_stop_block[None,:]
149
- #if shuffle:
150
- # self.rng.shuffle(i_trial_start_stop_block)
151
213
 
152
214
  blocks_per_batch = get_balanced_batches(len(i_trial_start_stop_block),
153
215
  batch_size=self.batch_size,
@@ -155,29 +217,39 @@ class CropsFromTrialsIterator(object):
155
217
  shuffle=shuffle)
156
218
  for i_blocks in blocks_per_batch:
157
219
  start_stop_blocks = i_trial_start_stop_block[i_blocks]
158
- batch = create_batch_from_i_trial_start_stop_blocks(
159
- X, y, start_stop_blocks, self.n_preds_per_input)
220
+ batch = _create_batch_from_i_trial_start_stop_blocks(
221
+ X, y, start_stop_blocks)
160
222
  yield batch
161
223
 
162
224
 
163
- def check_trial_bounds(i_trial_starts, i_trial_stops, n_preds_per_input):
164
- for start, stop in zip(i_trial_starts, i_trial_stops):
165
- assert stop - start >= n_preds_per_input, (
166
- "Trial should be longer or equal than number of sample preds, "
167
- "Trial length: {:d}, sample preds {:d}...".
168
- format(stop - start, n_preds_per_input))
169
-
170
-
171
- def compute_start_stop_block_inds(i_trial_starts, i_trial_stops,
172
- input_time_length, n_preds_per_input,
173
- check_preds_smaller_trial_len):
225
+ def _compute_start_stop_block_inds(i_trial_starts, i_trial_stops,
226
+ input_time_length, n_preds_per_input,
227
+ check_preds_smaller_trial_len):
228
+ """
229
+ Compute start stop block inds for all trials
230
+ Parameters
231
+ ----------
232
+ i_trial_starts: 1darray/list of int
233
+ Indices of first samples to predict(!).
234
+ i_trial_stops: 1daray/list of int
235
+ Indices one past last sample to predict.
236
+ input_time_length: int
237
+ n_preds_per_input: int
238
+ check_preds_smaller_trial_len: bool
239
+ Check whether predictions fit inside trial
240
+ Returns
241
+ -------
242
+ start_stop_blocks_per_trial: list of list of (int, int)
243
+ Per trial, a list of 2-tuples indicating start and stop index
244
+ of the inputs needed to predict entire trial.
245
+ """
174
246
  # create start stop indices for all batches still 2d trial -> start stop
175
247
  start_stop_blocks_per_trial = []
176
248
  for i_trial in range(len(i_trial_starts)):
177
- trial_start = i_trial_starts[i_trial]
178
- trial_stop = i_trial_stops[i_trial]
179
- start_stop_blocks = get_start_stop_blocks_for_trial(
180
- trial_start, trial_stop, input_time_length,
249
+ i_trial_start = i_trial_starts[i_trial]
250
+ i_trial_stop = i_trial_stops[i_trial]
251
+ start_stop_blocks = _get_start_stop_blocks_for_trial(
252
+ i_trial_start, i_trial_stop, input_time_length,
181
253
  n_preds_per_input)
182
254
 
183
255
  if check_preds_smaller_trial_len:
@@ -196,22 +268,38 @@ def compute_start_stop_block_inds(i_trial_starts, i_trial_stops,
196
268
  return start_stop_blocks_per_trial
197
269
 
198
270
 
199
- def get_start_stop_blocks_for_trial(trial_start, trial_stop, input_time_length,
200
- n_preds_per_input):
271
+ def _get_start_stop_blocks_for_trial(i_trial_start, i_trial_stop,
272
+ input_time_length, n_preds_per_input):
273
+
274
+ """
275
+ Compute start stop block inds for one trial
276
+ Parameters
277
+ ----------
278
+ i_trial_start: int
279
+ Index of first sample to predict(!).
280
+ i_trial_stops: 1daray/list of int
281
+ Index one past last sample to predict.
282
+ input_time_length: int
283
+ n_preds_per_input: int
284
+ Returns
285
+ -------
286
+ start_stop_blocks: list of (int, int)
287
+ A list of 2-tuples indicating start and stop index
288
+ of the inputs needed to predict entire trial.
289
+ """
201
290
  start_stop_blocks = []
202
- i_window_stop = trial_start # now when we add sample preds in loop,
291
+ i_window_stop = i_trial_start # now when we add sample preds in loop,
203
292
  # first sample of trial corresponds to first prediction
204
- while i_window_stop < trial_stop:
293
+ while i_window_stop < i_trial_stop:
205
294
  i_window_stop += n_preds_per_input
206
- i_adjusted_stop = min(i_window_stop, trial_stop)
295
+ i_adjusted_stop = min(i_window_stop, i_trial_stop)
207
296
  i_window_start = i_adjusted_stop - input_time_length
208
297
  start_stop_blocks.append((i_window_start, i_adjusted_stop))
209
298
 
210
299
  return start_stop_blocks
211
300
 
212
301
 
213
- def create_batch_from_i_trial_start_stop_blocks(X, y, i_trial_start_stop_block,
214
- n_preds_per_input):
302
+ def _create_batch_from_i_trial_start_stop_blocks(X, y, i_trial_start_stop_block):
215
303
  Xs = []
216
304
  ys = []
217
305
  for i_trial, start, stop in i_trial_start_stop_block: