示例#1
0
    def post(self):
        fig_data = self.get_fig_data()
        if fig_data is None:
            return self.visible_error(403, 'Broken connection to server.')

        if not self.request.files:
            return self.visible_error(403, 'No file uploaded.')

        f = self.request.files['query'][0]
        fname = f['filename']
        logging.info('Parsing file: %s', fname)
        fh = BytesIO(f['body'])
        try:
            query = parse_spectrum(fh)
        except Exception:
            try:
                fh = StringIO(f['body'].decode('utf-8', 'ignore'),
                              newline=None)
                query = parse_spectrum(fh)
            except Exception:
                logging.exception('Spectrum parse failed.')
                # XXX: save failed uploads for debugging purposes
                open('logs/badupload-' + fname, 'w').write(f['body'])
                return self.visible_error(415, 'Spectrum upload failed.')
        ds = UploadedSpectrumDataset(fname, query)
        fig_data.set_selected(ds.view(), title=fname)
        axlimits = fig_data.plot()
        return self.write_json(axlimits)
示例#2
0
def main():
  ap = ArgumentParser()
  ap.add_argument('--algorithm', default='als', choices=BL_CLASSES.keys(),
                  help='Algorithm type. [%(default)s]')
  ap.add_argument('-s', '--segment', action='store_true',
                  help='Auto-detect band segments and run on each separately.')
  ap.add_argument('-o', '--out-dir', help='If provided, write corrected files'
                                          ' to disk instead of plotting them.')
  ap.add_argument('files', type=open, nargs='+')
  args = ap.parse_args()

  for f in args.files:
    S = parse_spectrum(f)
    bands, intensities = S.T
    bl = BL_CLASSES[args.algorithm]()
    corrected = bl.fit_transform(bands, intensities, segment=args.segment)

    if args.out_dir:
      out_file = os.path.join(args.out_dir, os.path.basename(f.name))
      if os.path.exists(out_file):
        print 'Skipping existing file:', out_file
        continue
      header = ('Baseline corrected data from original file %s\n'
                'BL Algorithm: %s (default params)' % (f.name, args.algorithm))
      traj = np.column_stack((bands, corrected))
      np.savetxt(out_file, traj, fmt='%f', header=header)
    else:
      _, (ax1,ax2) = pyplot.subplots(nrows=2)
      ax1.plot(bands, intensities, 'r-')
      ax1.plot(bands, bl.baseline, 'k-')
      ax2.plot(bands, corrected, '-')
      pyplot.suptitle(f.name)
  pyplot.show()
示例#3
0
def _traj_ds(fh, ds_name, ds_kind, meta_kwargs, meta_pkeys, resample,
             description):
    zf = ZipFile(fh)
    traj_data = {}
    for subfile in zf.infolist():
        if subfile.file_size <= 0:
            continue
        # ignore directory prefixes
        fname = os.path.basename(subfile.filename)
        # ignore hidden files
        if fname.startswith('.'):
            continue
        # read and wrap, because the ZipExtFile object isn't seekable
        sub_fh = BytesIO(zf.open(subfile).read())
        try:
            # TODO: ensure each traj has wavelengths in increasing order
            traj_data[fname] = parse_spectrum(sub_fh)
        except Exception:
            logging.exception('bad spectrum subfile: ' + fname)
            return (415, 'Unable to parse spectrum file: %s' % fname)

    num_meta = len(meta_pkeys)
    num_traj = len(traj_data)

    if num_meta == 0:
        meta_pkeys = traj_data.keys()
    elif num_meta != num_traj:
        return (415, 'Failed: %d metadata entries for %d spectra' %
                (num_meta, num_traj))
    else:
        for pkey in meta_pkeys:
            if pkey not in traj_data:
                return (415, 'Failed: %r not in spectra.' % pkey)

    if resample is None:
        _load = _make_loader_function(description, meta_pkeys, traj_data,
                                      **meta_kwargs)
        WebTrajDataset(ds_name, ds_kind, _load)
    else:
        lb, ub, step = map(_maybe_float, resample)
        waves = [t[:, 0] for t in traj_data.values()]
        if lb is None:
            lb = max(w[0] for w in waves)
        if ub is None:
            ub = min(w[-1] for w in waves)
        if step is None:
            step = min(np.diff(w).min() for w in waves)

        wave = np.arange(lb, ub + step / 2, step, dtype=waves[0].dtype)
        spectra = np.zeros((len(waves), len(wave)), dtype=wave.dtype)
        for i, key in enumerate(meta_pkeys):
            traj = traj_data[key]
            spectra[i] = np.interp(wave, traj[:, 0], traj[:, 1])
        pkey = PrimaryKeyMetadata(meta_pkeys)

        _load = _make_loader_function(description,
                                      wave,
                                      spectra,
                                      pkey=pkey,
                                      **meta_kwargs)
        WebVectorDataset(ds_name, ds_kind, _load)

    return None
示例#4
0
def _parse(infile):
    return infile, parse_spectrum(infile)
示例#5
0
                dest='legend',
                help='Turn off figure legend')
ap.add_argument('--pp', default='normalize:max', help='Preprocess string')
ap.add_argument('--baseline',
                default='none',
                help='Baseline algorithm',
                choices=BL_CLASSES.keys() + ['none'])
ap.add_argument('--type',
                choices=PARSERS,
                help=('Parser type to use for all files. If not provided, '
                      'all parsers will be attempted.'))
ap.add_argument('file', nargs='+', help='Spectrum file(s).')
args = ap.parse_args()

fig, ax = pyplot.subplots(figsize=(12, 6))
for f in args.file:
    print('Parsing', f)
    bands, intensities = parse_spectrum(f, filetype=args.type).T
    if args.baseline != 'none':
        bl_alg = BL_CLASSES[args.baseline]()
        intensities = bl_alg.fit_transform(bands, intensities)
    intensities = preprocess(intensities[None], args.pp).ravel()
    ax.plot(bands, intensities, label=basename(f))
ax.yaxis.set_ticklabels([])
ax.set_xlabel('Wavenumber (1/cm)')
ax.set_ylabel('Intensity')
if args.legend:
    pyplot.legend(loc='best')
pyplot.tight_layout()
pyplot.show()