@@ -278,45 +278,71 @@ def load_h5(path, label):
278
278
279
279
280
280
def store_txt (path , data , sampling_rate = 1000. , resolution = None , date = None ,
281
- precision = 6 ):
281
+ labels = None , precision = 6 ):
282
282
"""Store data to a simple text file.
283
283
284
284
Parameters
285
285
----------
286
286
path : str
287
287
Path to file.
288
288
data : array
289
- Data to store.
289
+ Data to store (up to 2 dimensions) .
290
290
sampling_rate : int, float, optional
291
291
Sampling frequency (Hz).
292
292
resolution : int, optional
293
293
Sampling resolution.
294
294
date : datetime, str, optional
295
295
Datetime object, or an ISO 8601 formatted date-time string.
296
+ labels : list, optional
297
+ Labels for each column of `data`.
296
298
precision : int, optional
297
299
Precision for string conversion.
298
300
301
+ Raises
302
+ ------
303
+ ValueError
304
+ If the number of data dimensions is greater than 2.
305
+ ValueError
306
+ If the number of labels is inconsistent with the data.
307
+
299
308
"""
300
309
301
310
# ensure numpy
302
311
data = np .array (data )
303
312
313
+ # check dimension
314
+ if data .ndim > 2 :
315
+ raise ValueError ("Number of data dimensions cannot be greater than 2." )
316
+
304
317
# build header
305
318
header = "Simple Text Format\n "
306
- header += "Sampling Rate (Hz): %0.2f\n " % sampling_rate
319
+ header += "Sampling Rate (Hz):= %0.2f\n " % sampling_rate
307
320
if resolution is not None :
308
- header += "Resolution: %d\n " % resolution
321
+ header += "Resolution:= %d\n " % resolution
309
322
if date is not None :
310
323
if isinstance (date , basestring ):
311
- header += "Date: %s\n " % date
324
+ header += "Date:= %s\n " % date
312
325
elif isinstance (date , datetime .datetime ):
313
- header += "Date: %s\n " % date .isoformat ()
326
+ header += "Date:= %s\n " % date .isoformat ()
314
327
else :
315
328
ct = datetime .datetime .utcnow ().isoformat ()
316
- header += "Date: %s\n " % ct
329
+ header += "Date:= %s\n " % ct
317
330
318
331
# data type
319
- header += "Data Type: %s" % data .dtype
332
+ header += "Data Type:= %s\n " % data .dtype
333
+
334
+ # labels
335
+ if data .ndim == 1 :
336
+ ncols = 1
337
+ elif data .ndim == 2 :
338
+ ncols = data .shape [1 ]
339
+
340
+ if labels is None :
341
+ labels = ['%d' % i for i in xrange (ncols )]
342
+ elif len (labels ) != ncols :
343
+ raise ValueError ("Inconsistent number of labels." )
344
+
345
+ header += "Labels:= %s" % '\t ' .join (labels )
320
346
321
347
# normalize path
322
348
path = utils .normpath (path )
@@ -361,14 +387,14 @@ def load_txt(path):
361
387
362
388
# extract header
363
389
mdata_tmp = {}
364
- fields = ['Sampling Rate' , 'Resolution' , 'Date' , 'Data Type' ]
390
+ fields = ['Sampling Rate' , 'Resolution' , 'Date' , 'Data Type' , 'Labels' ]
365
391
values = []
366
392
for item in lines :
367
393
if '#' in item :
368
394
# parse comment
369
395
for f in fields :
370
396
if f in item :
371
- mdata_tmp [f ] = item .split (': ' )[1 ].strip ()
397
+ mdata_tmp [f ] = item .split (':= ' )[1 ].strip ()
372
398
fields .remove (f )
373
399
break
374
400
else :
@@ -394,6 +420,11 @@ def load_txt(path):
394
420
mdata ['date' ] = d
395
421
except (KeyError , ValueError ):
396
422
pass
423
+ try :
424
+ labels = mdata_tmp ['Labels' ].split ('\t ' )
425
+ mdata ['labels' ] = labels
426
+ except KeyError :
427
+ pass
397
428
398
429
# load array
399
430
data = np .genfromtxt (values , dtype = dtype , delimiter = '\t ' )
0 commit comments