lcms2.CGATS

class CGATS[source]

Bases: object

A CGATS.17 / IT8.7 file: one or more tables of named columns and patch rows.

All accessors act on the current table; change it with select() or the table property. Create one with CGATS() (empty), load(), from_bytes() or from_columns().

Examples

>>> import numpy as np
>>> from lcms2.cgats import CGATS
>>> it8 = CGATS.from_columns({"SAMPLE_ID": ["A1", "A2"], "LAB_L": [10.0, 20.0]})
>>> it8.columns
['SAMPLE_ID', 'LAB_L']
>>> it8.column("LAB_L").tolist()
[10.0, 20.0]
>>> CGATS.from_bytes(it8.to_bytes())["A2", "LAB_L"]
20.0
__init__()[source]

Methods

__init__()

add_table()

Append an empty table, make it current and return its index.

column(name)

One column: a float64 array if every cell is numeric, else an array of strings.

define_table(columns, n_rows)

Declare the column names and row count of the (empty) current table.

from_bytes(data)

Parse CGATS text.

from_columns(data[, sheet_type, properties])

Build a single-table CGATS from a mapping of column name to values.

get(patch, column)

Value of one cell: a float when it is numeric, otherwise a string (None if empty).

load(path)

Read a CGATS file from path (the file is read in Python and then closed).

save(path)

Write the file to path.

select(n)

Make table n current and return self.

set(patch, column, value)

Set one cell from a number or string; see get() for the addressing.

to_bytes()

Serialise every table to CGATS text.

to_dict()

Plain-Python snapshot of the whole file (JSON friendly).

Attributes

columns

Column names (the DATA_FORMAT) of the current table.

data

All columns of the current table as {name: array} (see column()).

n_rows

Number of patches (rows) of the current table.

patches

Patch names, from the SAMPLE_ID column (or the first column when absent).

sheet_type

Sheet type of the current table (for example "CGATS.17" or "IT8.7/2").

table

Index of the current table.

classmethod from_bytes(data: bytes) → CGATS[source]

Parse CGATS text.

Raises:

CMSError – If the data is not valid CGATS (the message names the offending line).

classmethod load(path: str | PathLike) → CGATS[source]

Read a CGATS file from path (the file is read in Python and then closed).

classmethod from_columns(data: Mapping[str, Sequence[Any]], sheet_type: str | None = None, properties: Mapping[str, str | float] | None = None) → CGATS[source]

Build a single-table CGATS from a mapping of column name to values.

Parameters:
  • data (mapping of str to sequence) – Columns of equal length; numbers are stored as numbers, anything else as text.

  • sheet_type (str, optional) – Sheet type (the first line of the file); defaults to "CGATS.17".

  • properties (mapping, optional) – Header properties to set.

to_bytes() → bytes[source]

Serialise every table to CGATS text.

save(path: str | PathLike) → None[source]

Write the file to path.

property table: int

Index of the current table.

select(n: int) → CGATS[source]

Make table n current and return self.

Raises:

IndexError – If n is not a table of the file (0 <= n < len(self)).

add_table() → int[source]

Append an empty table, make it current and return its index.

property sheet_type: str | None

Sheet type of the current table (for example "CGATS.17" or "IT8.7/2").

define_table(columns: Iterable[str], n_rows: int) → None[source]

Declare the column names and row count of the (empty) current table.

Raises:

CMSError – If the table already has columns; call add_table() first.

property columns: list[str]

Column names (the DATA_FORMAT) of the current table.

property n_rows: int

Number of patches (rows) of the current table.

property patches: list[str]

Patch names, from the SAMPLE_ID column (or the first column when absent).

get(patch: str | int, column: str | int) → float | str | None[source]

Value of one cell: a float when it is numeric, otherwise a string (None if empty).

Parameters:
  • patch (str or int) – Patch name (SAMPLE_ID) or row index.

  • column (str or int) – Column name (case-insensitive) or index.

set(patch: str | int, column: str | int, value: str | float) → None[source]

Set one cell from a number or string; see get() for the addressing.

column(name: str | int) → ndarray[source]

One column: a float64 array if every cell is numeric, else an array of strings.

Empty numeric cells become nan.

property data: dict[str, ndarray]

All columns of the current table as {name: array} (see column()).

to_dict() → dict[source]

Plain-Python snapshot of the whole file (JSON friendly).

Returns:

dict – {"tables": [{"sheet_type", "properties", "columns": {name: list}}, ...]}.