Skip to content
cheminfoPublic

Repository files navigation

xy-parser

Parse a CSV / TSV / whitespace-separated text file containing scientific data — a spectrum, diffractogram, chromatogram, two-column dump from an instrument, etc. — and return numeric x and y arrays.

This is the lightweight building block: if you only need {x, y} from a text file, use this. If you also need the technique-aware Analysis wrapper (variables, units, transmittance ↔ absorbance, peak picking), reach for common-spectrum or a domain wrapper like uv-spectrum / ir-spectrum / xrd-analysis, all of which use xy-parser under the hood. For known structured formats use the dedicated parser instead (fromJcamp for JCAMP-DX, spc-parser for Thermo Galactic SPC, …).

Zakodium logo

Maintained by Zakodium

NPM version Test coverage npm download DOI

Installation

npm install xy-parser

Usage

import { parseXY, parseXYAndKeepInfo } from 'xy-parser';

const data = `My file
1   2
3   4
5   6
7   8`;

const result = parseXY(data);
/* result ->
  {
    x: [1, 3, 5, 7],
    y: [2, 4, 6, 8],
  }
*/

const resultWithInfo = parseXYAndKeepInfo(data);
/* resultWithInfo ->
  {
    info: [{ position: 0, value: 'My file' }],
    data: {
      x: [1, 3, 5, 7],
      y: [2, 4, 6, 8],
    },
  }
*/

Columns may be separated by tabs, spaces, commas or semicolons. When columns are separated by tabs, spaces or semicolons, a comma inside a number is read as the decimal separator (1,5\t2,3 gives x: [1.5], y: [2.3]).

Options

Option Default Description
xColumn 0 Index of the column used for x.
yColumn 1 Index of the column used for y.
bestGuess false Try to determine which columns should be used (see below).
numberColumns Number.MAX_SAFE_INTEGER Reflow rows into this many columns (e.g. 2 for an x1 y1 x2 y2 … layout).
minNumberColumns 2 Skip lines with fewer columns.
maxNumberColumns Number.MAX_SAFE_INTEGER Skip lines with more columns.
uniqueX false Merge repeated x values, summing their y values.
rescale false Divide y by its maximum so that the largest value is 1.

The bestGuess option will try to determine which columns should be used.

If there are 3 columns and the first column is a sequential number starting at '1' it looks like this is a line number, we will ignore it.

If there are many columns maybe we have a format like X1, Y1, X2, Y2, ... in this cases if one column on two is a monotone series we will parse it correctly.

License

MIT

Releases

Packages

Used by

Contributors

Languages