def long_file(data_file, dataformat, sample_list, savedir=None, srm_id=None, **autorange_args):
if isinstance(sample_list, str):<EOL><INDENT>if os.path.exists(sample_list):<EOL><INDENT>sample_list = np.genfromtxt(sample_list, dtype=str)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>elif not isinstance(sample_list, (list, np.ndarray)):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if srm_id is not None:<EOL><INDENT>srm_replace = []<EOL>for s in sample_list:<EOL><INDENT>if srm_id in s:<EOL><INDENT>s = srm_id<EOL><DEDENT>srm_replace.append(s)<EOL><DEDENT>sample_list = srm_replace<EOL><DEDENT>_, _, dat, meta = read_data(data_file, dataformat=dataformat, name_mode='<STR_LIT:file>')<EOL>if '<STR_LIT:date>' in meta:<EOL><INDENT>d = dateutil.parser.parse(meta['<STR_LIT:date>'])<EOL><DEDENT>else:<EOL><INDENT>d = datetime.datetime.now()<EOL><DEDENT>bkg, sig, trn, _ = autorange(dat['<STR_LIT>'], dat['<STR_LIT>'], **autorange_args)<EOL>ns = np.zeros(sig.size)<EOL>ns[sig] = np.cumsum((sig ^ np.roll(sig, <NUM_LIT:1>)) & sig)[sig]<EOL>n = int(max(ns))<EOL>if len(sample_list) != n:<EOL><INDENT>warn('<STR_LIT>' + <EOL>'<STR_LIT>')<EOL><DEDENT>bounds = []<EOL>lower = <NUM_LIT:0><EOL>sn = <NUM_LIT:0><EOL>next_sample = '<STR_LIT>'<EOL>for ni in range(n-<NUM_LIT:1>):<EOL><INDENT>sample = sample_list[sn]<EOL>next_sample = sample_list[sn + <NUM_LIT:1>]<EOL>if sample != next_sample:<EOL><INDENT>current_end = np.argwhere(dat['<STR_LIT>'] == dat['<STR_LIT>'][ns == ni + <NUM_LIT:1>].max())[<NUM_LIT:0>]<EOL>next_start = np.argwhere(dat['<STR_LIT>'] == dat['<STR_LIT>'][ns == ni + <NUM_LIT:2>].min())[<NUM_LIT:0>]<EOL>upper = (current_end + next_start) // <NUM_LIT:2><EOL>bounds.append((sample, (int(lower), int(upper))))<EOL>lower = upper + <NUM_LIT:1><EOL><DEDENT>sn += <NUM_LIT:1><EOL><DEDENT>bounds.append((sample_list[-<NUM_LIT:1>], (int(upper) + <NUM_LIT:1>, len(ns))))<EOL>sections = {}<EOL>seen = {}<EOL>for s, (lo, hi) in bounds:<EOL><INDENT>if s not in seen:<EOL><INDENT>seen[s] = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>seen[s] += <NUM_LIT:1><EOL>s += '<STR_LIT>'.format(seen[s])<EOL><DEDENT>sections[s] = {'<STR_LIT>': dat['<STR_LIT>'][lo:hi]}<EOL>sections[s]['<STR_LIT>'] = sections[s]['<STR_LIT>'] - np.nanmin(sections[s]['<STR_LIT>'])<EOL>sections[s]['<STR_LIT>'] = {}<EOL>for k, v in dat['<STR_LIT>'].items():<EOL><INDENT>sections[s]['<STR_LIT>'][k] = v[lo:hi]<EOL><DEDENT>sections[s]['<STR_LIT>'] = d + datetime.timedelta(seconds=np.nanmin(sections[s]['<STR_LIT>']))<EOL><DEDENT>if savedir is None:<EOL><INDENT>savedir = os.path.join(os.path.dirname(os.path.abspath(data_file)), os.path.splitext(os.path.basename(data_file))[<NUM_LIT:0>] + '<STR_LIT>')<EOL><DEDENT>if not os.path.isdir(savedir):<EOL><INDENT>os.makedirs(savedir)<EOL><DEDENT>header = ['<STR_LIT>'.format(datetime.datetime.now().strftime('<STR_LIT>'))]<EOL>if '<STR_LIT:date>' not in meta:<EOL><INDENT>header.append('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>header.append('<STR_LIT>')<EOL>header.append('<STR_LIT>')<EOL>header.append('<STR_LIT>')<EOL><DEDENT>flist = [savedir]<EOL>for s, dat in sections.items():<EOL><INDENT>iheader = header.copy()<EOL>iheader.append('<STR_LIT>'.format(s))<EOL>iheader.append('<STR_LIT>'.format(dat['<STR_LIT>'].strftime('<STR_LIT>')))<EOL>iheader = '<STR_LIT:\n>'.join(iheader) + '<STR_LIT:\n>'<EOL>out = pd.DataFrame({analyte_2_namemass(k): v for k, v in dat['<STR_LIT>'].items()}, index=dat['<STR_LIT>'])<EOL>out.index.name = '<STR_LIT>'<EOL>csv = out.to_csv()<EOL>with open('<STR_LIT>'.format(savedir, s), '<STR_LIT:w>') as f:<EOL><INDENT>f.write(iheader)<EOL>f.write(csv)<EOL><DEDENT>flist.append('<STR_LIT>'.format(s))<EOL><DEDENT>print("<STR_LIT>".format(n, '<STR_LIT:\n>'.join(flist)))<EOL>return None<EOL>
TODO: Check for existing files in savedir, don't overwrite?