ewoksxas.converters.orange.Converter#

class ewoksxas.converters.orange.Converter[source]#

Bases: object

Helper class to convert between spectroscopic data and Orange Tables.

Builder methods that populate or modify a Converter in place (add_features, add_meta, add_metas_from, add_target) return self for chaining. Methods that produce a new row set (with_features, take_rows, concatenate) return a new Converter and leave the original untouched.

For efficiency the builder methods and the read accessors (features, metas, targets) avoid copying where they can: they store and return references to the underlying arrays, so callers must not mutate data they pass in or read out. Discrete metas and targets are the write-side exception: their data is re-encoded into a fresh integer-index array rather than stored by reference. add_metas_from (and thus with_features) shares the Column objects of the source, whereas take_rows and concatenate each return a fully independent copy. On the read side, get_meta_values builds a fresh array for a discrete meta and returns a reference to the stored array otherwise, while get_meta_row returns a dict of decoded per-row scalar values.

add_features(names, data, attributes=None)[source]#

Set the feature data (x-axis and intensities).

Any existing feature block is replaced.

Args:

names: 1D array of x-axis values (e.g., energy, wavenumber). data: 2D array of spectral intensities with shape (n_rows, n_features).

If 1D, treated as a single row and reshaped to (1, n_features).

attributes: subtitles or meta information for column header.

Returns:

Self for method chaining.

Raises:

ValueError: If the number of names does not match the data columns.

Parameters:
  • names (ndarray[tuple[int, ...], dtype[Any]])

  • data (ndarray[tuple[int, ...], dtype[float64]])

  • attributes (list[dict[str, str]] | None)

Return type:

Converter

add_meta(name, data, var_type=VarType.AUTO, attributes=None, **kwargs)[source]#

Add a metadata variable, replacing any existing column of that name.

Metas can be ContinuousVariable, StringVariable, or DiscreteVariable. A meta is identified by its name, so adding one whose name already exists replaces that column in place (keeping its position) rather than appending a duplicate. When the replaced column keeps its variable type, its category values and constructor arguments are reused unless overridden.

Args:

name: Name of the metadata variable. data: Array of values, one for each row. var_type: Orange Variable type. attributes: subtitles or meta information for column header. **kwargs: Additional arguments for the Variable constructor.

Returns:

Self for chaining.

Parameters:
  • name (str)

  • data (ndarray[tuple[int, ...], dtype[Any]])

  • var_type (VarType)

  • attributes (dict[str, str] | None)

  • kwargs (Any)

Return type:

Converter

add_metas_from(source, *, overwrite=False)[source]#

Carry over meta columns from another converter.

Each source meta is carried over by reference: source and destination share the Column objects and their underlying arrays. Metas whose name already exists here are skipped unless overwrite is True, so task-computed metas take precedence over carried-over ones.

Args:

source: Converter to carry metas over from. overwrite: Replace existing same-named metas instead of skipping.

Returns:

Self for chaining.

Raises:

ValueError: If the source has metas but a different row count.

Parameters:
Return type:

Converter

add_target(name, data, var_type=VarType.AUTO, attributes=None, **kwargs)[source]#

Add a target variable, replacing any existing column of that name.

Targets can only be ContinuousVariable or DiscreteVariable. Name collisions are resolved like in add_meta: the existing column is replaced in place, keeping its position and (when the variable type is unchanged) its category values and constructor arguments.

Args:

name: Name of the target variable. data: Array of values, one for each row. var_type: Optional Orange Variable type. attributes: subtitles or meta information for column header. **kwargs: Additional arguments for the Variable constructor.

Returns:

Self for chaining.

Parameters:
  • name (str)

  • data (ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]])

  • var_type (VarType)

  • attributes (dict[str, str] | None)

  • kwargs (Any)

Return type:

Converter

classmethod concatenate(converters)[source]#

Concatenate several converters row-wise into a new one.

Each part is an existing Converter contributing its rows. All parts must share the same feature x-axis and the same column structure (matching meta/target names in the same order).

Args:

converters: The parts to assemble, in order.

Returns:

A new Converter holding the rows of every part in sequence.

Raises:

ValueError: If the sequence is empty or the parts are incompatible.

Parameters:

converters (Sequence[Converter])

Return type:

Converter

property features: tuple[ndarray[tuple[int, ...], dtype[Any]], ndarray[tuple[int, ...], dtype[float64]]]#

Get the feature names and data.

Returns:

A tuple containing feature names and feature data. The names hold x-axis values that may be floats or strings.

Raises:

ValueError: If no features have been set.

classmethod from_table(table)[source]#

Create a Converter instance from an existing Orange Table.

Extracts features, targets, and metadata from the table. Feature names are recovered from the table domain attributes.

Args:

table: The source Orange Table.

Parameters:

table (Table)

Return type:

Converter

get_column(name)[source]#

Get the meta or target column by name.

Args:

name: the header or column title of the desired column.

Parameters:

name (str)

Return type:

Column

get_group_id(id_columns=('Filename', 'Scan Name'), default_by_row=True)[source]#

Get an array of identifiers based on the content of one or more columns.

If one column is provided, the identifiers are the values of that column. Two or more columns have their values joined and hashed to create identifiers.

Args:

id_columns: meta column names to join for determining group ids. default_by_row: separate groups for each row (True) or one group with all

rows (False).

Parameters:
  • id_columns (Sequence[str])

  • default_by_row (bool)

Return type:

ndarray[tuple[int, ...], dtype[Any]]

get_meta_names()[source]#

Get the names of all metadata columns, in order.

Return type:

list[str]

get_meta_row(index)[source]#

Get a dictionary of meta values for the row at the given index.

Args:

index: row index

Raises:

IndexError: If index is out of range for the current rows.

Parameters:

index (int)

Return type:

dict[str, Any]

get_meta_values(name, default=None)[source]#

Get the array of values for a specific meta.

Discrete metas are decoded back to their category labels. When the meta is missing, the default determines the result: a scalar is broadcast to one value per row, and an array-like with one value per row is used verbatim. A default of None means no fallback: a missing meta raises.

Args:

name: Meta name. default: Fallback used when the meta is missing.

Raises:

KeyError: If the meta is missing and no default was given.

Parameters:
  • name (str)

  • default (Any)

Return type:

ndarray[tuple[int, ...], dtype[Any]]

property metas: list[Column]#

Get the list of metadata column definitions.

property n_rows: int#

Return the number of rows in the table.

take_rows(indices)[source]#

Return a new Converter containing only the given rows.

Rows may be reordered or repeated by listing their indices in the desired order. The feature x-axis and every column definition (names, types, category values) are preserved; only the per-row data is subset.

Args:

indices: Row positions to keep, in the desired output order.

Returns:

A new Converter with the selected rows.

Raises:

IndexError: If any index is out of range for the current rows.

Parameters:

indices (Sequence[int])

Return type:

Converter

property targets: list[Column]#

Get the list of target column definitions.

to_table()[source]#

Construct and return the Orange Table.

The feature variables (the domain attributes) are ContinuousVariables named by the x-axis values; the intensity matrix becomes the X block. Meta and target variables hold their column data as the metas and Y blocks. Targets are typically not used in spectroscopic data.

Return type:

Table

with_features(names, data, attributes=None)[source]#

Return a new Converter with the given features and the metas carried over.

This is the common shape of a task output: spectral data derived from the rows of this converter, keeping the metadata. The metas are carried over by reference (see add_metas_from); task-computed metas can then be added or replaced on the result with add_meta. Only metas are carried; any targets on this converter are dropped.

Args:

names: 1D array of x-axis values (e.g., energy, wavenumber). data: 2D array of spectral intensities with shape (n_rows, n_features). attributes: subtitles or meta information for column header.

Returns:

A new Converter with the given features and the metas of this one.

Parameters:
  • names (ndarray[tuple[int, ...], dtype[Any]])

  • data (ndarray[tuple[int, ...], dtype[float64]])

  • attributes (list[dict[str, str]] | None)

Return type:

Converter