@@ -278,45 +278,71 @@ def load_h5(path, label):
278278
279279
280280def store_txt (path , data , sampling_rate = 1000. , resolution = None , date = None ,
281- precision = 6 ):
281+ labels = None , precision = 6 ):
282282 """Store data to a simple text file.
283283
284284 Parameters
285285 ----------
286286 path : str
287287 Path to file.
288288 data : array
289- Data to store.
289+ Data to store (up to 2 dimensions) .
290290 sampling_rate : int, float, optional
291291 Sampling frequency (Hz).
292292 resolution : int, optional
293293 Sampling resolution.
294294 date : datetime, str, optional
295295 Datetime object, or an ISO 8601 formatted date-time string.
296+ labels : list, optional
297+ Labels for each column of `data`.
296298 precision : int, optional
297299 Precision for string conversion.
298300
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+
299308 """
300309
301310 # ensure numpy
302311 data = np .array (data )
303312
313+ # check dimension
314+ if data .ndim > 2 :
315+ raise ValueError ("Number of data dimensions cannot be greater than 2." )
316+
304317 # build header
305318 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
307320 if resolution is not None :
308- header += "Resolution: %d\n " % resolution
321+ header += "Resolution:= %d\n " % resolution
309322 if date is not None :
310323 if isinstance (date , basestring ):
311- header += "Date: %s\n " % date
324+ header += "Date:= %s\n " % date
312325 elif isinstance (date , datetime .datetime ):
313- header += "Date: %s\n " % date .isoformat ()
326+ header += "Date:= %s\n " % date .isoformat ()
314327 else :
315328 ct = datetime .datetime .utcnow ().isoformat ()
316- header += "Date: %s\n " % ct
329+ header += "Date:= %s\n " % ct
317330
318331 # 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 )
320346
321347 # normalize path
322348 path = utils .normpath (path )
@@ -361,14 +387,14 @@ def load_txt(path):
361387
362388 # extract header
363389 mdata_tmp = {}
364- fields = ['Sampling Rate' , 'Resolution' , 'Date' , 'Data Type' ]
390+ fields = ['Sampling Rate' , 'Resolution' , 'Date' , 'Data Type' , 'Labels' ]
365391 values = []
366392 for item in lines :
367393 if '#' in item :
368394 # parse comment
369395 for f in fields :
370396 if f in item :
371- mdata_tmp [f ] = item .split (': ' )[1 ].strip ()
397+ mdata_tmp [f ] = item .split (':= ' )[1 ].strip ()
372398 fields .remove (f )
373399 break
374400 else :
@@ -394,6 +420,11 @@ def load_txt(path):
394420 mdata ['date' ] = d
395421 except (KeyError , ValueError ):
396422 pass
423+ try :
424+ labels = mdata_tmp ['Labels' ].split ('\t ' )
425+ mdata ['labels' ] = labels
426+ except KeyError :
427+ pass
397428
398429 # load array
399430 data = np .genfromtxt (values , dtype = dtype , delimiter = '\t ' )
0 commit comments