9
9
import mne
10
10
import numpy as np
11
11
from mne_bids .write import _write_raw_brainvision
12
+ import fileinput
13
+
12
14
13
15
def lemon_prepare ():
14
16
"""Download and prepare a few files of the LEMON dataset.
@@ -121,23 +123,24 @@ def lemon_bidscoin_prepare(src_path):
121
123
print ('already done, skipping...' )
122
124
print ('finish' )
123
125
124
- def make_dummy_dataset (PATTERN = 'T%task%/S%session%/sub%subject%_%acquisition%_%run%' ,
126
+ def make_dummy_dataset (EXAMPLE ,
127
+ PATTERN = 'T%task%/S%session%/sub%subject%_%acquisition%_%run%' ,
125
128
DATASET = 'DUMMY' ,
126
129
NSUBS = 2 ,
127
130
NSESSIONS = 2 ,
128
131
NTASKS = 2 ,
129
132
NACQS = 2 ,
130
133
NRUNS = 2 ,
131
- NCHANNELS = 2 ,
132
- SFREQ = 200 ,
133
- STOP = 10 ,
134
- NUMEVENTS = 10 ,
135
134
PREFIXES = {'subject' :'SU' ,'session' :'SE' ,'task' :'TA' ,'acquisition' :'AC' ,'run' :'RU' },
136
- ROOT = None ):
135
+ ROOT = None ,
136
+ ):
137
137
"""Create a dummy dataset given some parameters.
138
138
139
139
Parameters
140
140
----------
141
+ EXAMPLE : str,PathLike|list , required
142
+ Path of the file to replicate as each file in the dummy dataset.
143
+ If a list, it is assumed each item is a file. All of these items are replicated.
141
144
PATTERN : str, optional
142
145
The pattern in placeholder notation using the following fields:
143
146
%dataset%, %task%, %session%, %subject%, %run%, %acquisition%
@@ -153,20 +156,13 @@ def make_dummy_dataset(PATTERN='T%task%/S%session%/sub%subject%_%acquisition%_%r
153
156
Number of acquisitions.
154
157
NRUNS : int, optional
155
158
Number of runs.
156
- NCHANNELS : int, optional
157
- Number of channels.
158
- SFREQ : float, optional
159
- Samplinf frequency of the data.
160
- STOP : float, optional
161
- Time duration of the data in seconds.
162
- NUMEVENTS : int, optional
163
- Number of events along the duration.
164
159
PREFIXES : dict, optional
165
160
Dictionary with the following keys:'subject', 'session', 'task' and 'acquisition'.
166
161
The values are the corresponding prefix. RUN is not present because it has to be a number.
167
162
ROOT : str, optional
168
163
Path where the files will be generated.
169
164
If None, the _data subdir will be used.
165
+
170
166
"""
171
167
172
168
if ROOT is None :
@@ -176,8 +172,6 @@ def make_dummy_dataset(PATTERN='T%task%/S%session%/sub%subject%_%acquisition%_%r
176
172
data_dir = ROOT
177
173
os .makedirs (data_dir ,exist_ok = True )
178
174
179
-
180
-
181
175
sub_zeros = get_num_digits (NSUBS )
182
176
subs = [ PREFIXES ['subject' ]+ str (x ).zfill (sub_zeros ) for x in range (NSUBS )]
183
177
@@ -193,17 +187,6 @@ def make_dummy_dataset(PATTERN='T%task%/S%session%/sub%subject%_%acquisition%_%r
193
187
acq_zeros = get_num_digits (NACQS )
194
188
acquisitions = [ PREFIXES ['acquisition' ]+ str (x ).zfill (acq_zeros ) for x in range (NACQS )]
195
189
196
- # Create some dummy metadata
197
- n_channels = NCHANNELS
198
- sampling_freq = SFREQ # in Hertz
199
- info = mne .create_info (n_channels , sfreq = sampling_freq )
200
-
201
- times = np .linspace (0 , STOP , STOP * sampling_freq , endpoint = False )
202
- data = np .zeros ((NCHANNELS ,times .shape [0 ]))
203
-
204
- raw = mne .io .RawArray (data , info )
205
- raw .set_channel_types ({x :'eeg' for x in raw .ch_names })
206
- new_events = mne .make_fixed_length_events (raw , duration = STOP // NUMEVENTS )
207
190
208
191
for task in tasks :
209
192
for session in sessions :
@@ -218,5 +201,116 @@ def make_dummy_dataset(PATTERN='T%task%/S%session%/sub%subject%_%acquisition%_%r
218
201
dummy = dummy .replace ('%acquisition%' ,acq )
219
202
path = [data_dir ] + dummy .split ('/' )
220
203
fpath = os .path .join (* path )
221
- _write_raw_brainvision (raw ,fpath ,new_events ,overwrite = True )
204
+ dirpath = os .path .join (* path [:- 1 ])
205
+ os .makedirs (dirpath ,exist_ok = True )
206
+ if isinstance (EXAMPLE ,list ):
207
+ for ff in EXAMPLE :
208
+ fname , ext = os .path .splitext (ff )
209
+ shutil .copyfile (ff , fpath + ext )
210
+ if 'vmrk' in ext or 'vhdr' in ext :
211
+ replace_brainvision_filename (fpath + ext ,path [- 1 ])
212
+ else :
213
+ fname , ext = os .path .splitext (EXAMPLE )
214
+ shutil .copyfile (EXAMPLE , fpath + ext )
215
+
216
+ def get_dummy_raw (NCHANNELS = 5 ,
217
+ SFREQ = 200 ,
218
+ STOP = 10 ,
219
+ NUMEVENTS = 10 ,
220
+ ):
221
+ """
222
+ Create a dummy MNE Raw file given some parameters.
223
+
224
+ Parameters
225
+ ----------
226
+ NCHANNELS : int, optional
227
+ Number of channels.
228
+ SFREQ : float, optional
229
+ Sampling frequency of the data.
230
+ STOP : float, optional
231
+ Time duration of the data in seconds.
232
+ NUMEVENTS : int, optional
233
+ Number of events along the duration.
234
+ """
235
+ # Create some dummy metadata
236
+ n_channels = NCHANNELS
237
+ sampling_freq = SFREQ # in Hertz
238
+ info = mne .create_info (n_channels , sfreq = sampling_freq )
239
+
240
+ times = np .linspace (0 , STOP , STOP * sampling_freq , endpoint = False )
241
+ data = np .zeros ((NCHANNELS ,times .shape [0 ]))
242
+
243
+ raw = mne .io .RawArray (data , info )
244
+ raw .set_channel_types ({x :'eeg' for x in raw .ch_names })
245
+ new_events = mne .make_fixed_length_events (raw , duration = STOP // NUMEVENTS )
222
246
247
+ return raw ,new_events
248
+
249
+ def save_dummy_vhdr (fpath ,dummy_args = {}
250
+ ):
251
+ """
252
+ Save a dummy vhdr file.
253
+
254
+ Parameters
255
+ ----------
256
+ fpath : str, required
257
+ Path where to save the file.
258
+ kwargs : dict, optional
259
+ Dictionary with the arguments of the get_dummy_raw function.
260
+
261
+ Returns
262
+ -------
263
+ List with the Paths of the desired vhdr file, if those were succesfully created,
264
+ None otherwise.
265
+ """
266
+
267
+ raw ,new_events = get_dummy_raw (** dummy_args )
268
+ _write_raw_brainvision (raw ,fpath ,new_events ,overwrite = True )
269
+ eegpath = fpath .replace ('.vhdr' ,'.eeg' )
270
+ vmrkpath = fpath .replace ('.vhdr' ,'.vmrk' )
271
+ if all (os .path .isfile (x ) for x in [fpath ,eegpath ,vmrkpath ]):
272
+ return [fpath ,eegpath ,vmrkpath ]
273
+ else :
274
+ return None
275
+
276
+ def save_dummy_cnt (fpath ,
277
+ ):
278
+ """
279
+ Save a dummy cnt file.
280
+
281
+ Parameters
282
+ ----------
283
+ fpath : str, required
284
+ Path where to save the file.
285
+
286
+ Returns
287
+ -------
288
+ Path of the desired file if the file was succesfully created,
289
+ None otherwise.
290
+ """
291
+ fname = 'scan41_short.cnt'
292
+ cnt_dict = {'dataset_name' : 'cnt_sample' ,
293
+ 'archive_name' : 'scan41_short.cnt' ,
294
+ 'hash' : 'md5:7ab589254e83e001e52bee31eae859db' ,
295
+ 'url' : 'https://github.com/mne-tools/mne-testing-data/blob/master/CNT/scan41_short.cnt?raw=true' ,
296
+ 'folder_name' : 'cnt_sample' ,
297
+ }
298
+ data_path = mne .datasets .fetch_dataset (cnt_dict )
299
+ shutil .copyfile (os .path .join (data_path ,'scan41_short.cnt' ), fpath ) #copyfile overwrites by default
300
+ if os .path .isfile (fpath ):
301
+ return fpath
302
+ else :
303
+ return None
304
+
305
+ def replace_brainvision_filename (fpath ,newname ):
306
+ if '.eeg' in newname :
307
+ newname = newname .replace ('.eeg' ,'' )
308
+ if '.vmrk' in newname :
309
+ newname = newname .replace ('.vmrk' ,'' )
310
+ for line in fileinput .input (fpath , inplace = True ):
311
+ if 'DataFile' in line :
312
+ print (f'DataFile={ newname } .eeg' .format (fileinput .filelineno (), line ))
313
+ elif 'MarkerFile' in line :
314
+ print (f'MarkerFile={ newname } .vmrk' .format (fileinput .filelineno (), line ))
315
+ else :
316
+ print ('{}' .format (line ), end = '' )
0 commit comments