def read_data(data_file, dataformat, name_mode):
with open(data_file) as f:<EOL><INDENT>lines = f.readlines()<EOL><DEDENT>if '<STR_LIT>' in dataformat.keys():<EOL><INDENT>meta = Bunch()<EOL>for k, v in dataformat['<STR_LIT>'].items():<EOL><INDENT>try:<EOL><INDENT>out = re.search(v[-<NUM_LIT:1>], lines[int(k)]).groups()<EOL><DEDENT>except:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(v[-<NUM_LIT:1>], lines[int(k)]))<EOL><DEDENT>for i in np.arange(len(v[<NUM_LIT:0>])):<EOL><INDENT>meta[v[<NUM_LIT:0>][i]] = out[i]<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>meta = {}<EOL><DEDENT>if name_mode == '<STR_LIT>':<EOL><INDENT>sample = os.path.basename(data_file).split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL><DEDENT>elif name_mode == '<STR_LIT>':<EOL><INDENT>sample = meta['<STR_LIT:name>']<EOL><DEDENT>else:<EOL><INDENT>sample = name_mode<EOL><DEDENT>columns = np.array(lines[dataformat['<STR_LIT>']['<STR_LIT>']].strip().split(<EOL>dataformat['<STR_LIT>']['<STR_LIT>']))<EOL>if '<STR_LIT>' in dataformat['<STR_LIT>'].keys():<EOL><INDENT>pr = re.compile(dataformat['<STR_LIT>']['<STR_LIT>'])<EOL>analytes = [pr.match(c).groups()[<NUM_LIT:0>] for c in columns if pr.match(c)]<EOL><DEDENT>if '<STR_LIT>' in dataformat.keys():<EOL><INDENT>with open(data_file) as f:<EOL><INDENT>fbuffer = f.read()<EOL><DEDENT>for k, v in dataformat['<STR_LIT>'].items():<EOL><INDENT>fbuffer = re.sub(k, v, fbuffer)<EOL><DEDENT>read_data = np.genfromtxt(BytesIO(fbuffer.encode()),<EOL>**dataformat['<STR_LIT>']).T<EOL><DEDENT>else:<EOL><INDENT>read_data = np.genfromtxt(data_file,<EOL>**dataformat['<STR_LIT>']).T<EOL><DEDENT>dind = np.zeros(read_data.shape[<NUM_LIT:0>], dtype=bool)<EOL>for a in analytes:<EOL><INDENT>dind[columns == a] = True<EOL><DEDENT>data = Bunch()<EOL>data['<STR_LIT>'] = read_data[dataformat['<STR_LIT>']['<STR_LIT>']]<EOL>if '<STR_LIT>' in dataformat['<STR_LIT>']:<EOL><INDENT>if isinstance(dataformat['<STR_LIT>']['<STR_LIT>'], (float, int)):<EOL><INDENT>time_mult = dataformat['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>elif isinstance(dataformat['<STR_LIT>']['<STR_LIT>'], str):<EOL><INDENT>unit_multipliers = {'<STR_LIT>': <NUM_LIT:1>/<NUM_LIT:1000>,<EOL>'<STR_LIT>': <NUM_LIT>/<NUM_LIT:1>,<EOL>'<STR_LIT:s>': <NUM_LIT:1>}<EOL>try:<EOL><INDENT>time_mult = unit_multipliers[dataformat['<STR_LIT>']['<STR_LIT>']]<EOL><DEDENT>except:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>data['<STR_LIT>'] *= time_mult<EOL><DEDENT>data['<STR_LIT>'] = Bunch(zip(analytes, read_data[dind]))<EOL>data['<STR_LIT>'] = np.nansum(read_data[dind], <NUM_LIT:0>)<EOL>return sample, analytes, data, meta<EOL>
Load data_file described by a dataformat dict.

Parameters
----------
data_file : str
    Path to data file, including extension.
dataformat : dict
    A dataformat dict, see example below.
name_mode : str
    How to identyfy sample names. If 'file_names' uses the
    input name of the file, stripped of the extension. If
    'metadata_names' uses the 'name' attribute of the 'meta'
    sub-dictionary in dataformat. If any other str, uses this
    str as the sample name.

Example
-------
>>>
{'genfromtext_args': {'delimiter': ',',
                      'skip_header': 4},  # passed directly to np.genfromtxt
 'column_id': {'name_row': 3,  # which row contains the column names
               'delimiter': ',',  # delimeter between column names
               'timecolumn': 0,  # which column contains the 'time' variable
               'pattern': '([A-z]{1,2}[0-9]{1,3})'},  # a regex pattern which captures the column names
 'meta_regex': {  # a dict of (line_no: ([descriptors], [regexs])) pairs
                0: (['path'], '(.*)'),
                2: (['date', 'method'],  # MUST include date
                 '([A-Z][a-z]+ [0-9]+ [0-9]{4}[ ]+[0-9:]+ [amp]+).* ([A-z0-9]+\.m)')
               }
}

Returns
-------
sample, analytes, data, meta : tuple