cuvis_ai.distance.distance.AbstractDistance

class cuvis_ai.distance.distance.AbstractDistance(ref_spectra: list = [])[source]

Bases: Node, CubeConsumer

Abstract class for data preprocessing.

There are two ways spectral distance can be used

  1. Distance relative to a reference spectra, which returns a single scored image

2) Distance relative to multiple reference spectral, which returns an array of scored images (This can then be passed into a decider algorithm) # TODO: Should this behavior be allowed? This might break the notion of input/output dimensions - If the spectra are known, they can be stored with the object initially

Parameters:

Node (Node) – Defines the distance measure as a type of node.

__init__(ref_spectra: list = [])[source]

Initialize distance metric

Parameters:

ref_spectra (list, optional) – List of reference spectra to compare against, by default []

Methods

__init__([ref_spectra])

Initialize distance metric

check_input_dim(X)

Check that the parameters for the input data data match user expectations

check_output_dim(X)

Check that the parameters for the output data data match user expectations

fit(X)

forward(X[, ref_spectra])

Pass the data through comparative function

get_fit_requested_meta()

get_forward_requested_meta()

load(params[, filepath])

Load dumped parameters to recreate the distance object

score(data, X)

Abstract distance method implemented by every distance type

serialize(working_dir)

Convert distance node to serializable format

set_fit_meta_request(**kwargs)

set_forward_meta_request(**kwargs)

spectra_to_array(ref_spectra)

Convert list of spectra to a numpy array

Attributes

input_dim

Get required input dimension

output_dim

Get required output dimension

check_input_dim(X)

Check that the parameters for the input data data match user expectations

Parameters: X (array-like): Input data.

Returns: (Bool) Valid data

check_output_dim(X)

Check that the parameters for the output data data match user expectations

Parameters: X (array-like): Input data.

Returns: (Bool) Valid data

fit(X)[source]
forward(X: ndarray, ref_spectra: list | None = None) ndarray[source]

Pass the data through comparative function

Parameters:
  • X (np.ndarray) – Input data.

  • ref_spectra (list, optional) – List of spectra to compare against

Returns:

Distance maps for each of the reference spectra.

Return type:

np.ndarray

Raises:
  • ValueError – Mismatch in input data and reference spectra provided on function call.

  • ValueError – Mismatch in input data and reference spectra provided on node initialization.

  • ValueError – No reference spectra provided in init or on forward function pass.

get_fit_requested_meta()
get_forward_requested_meta()
property input_dim: list

Get required input dimension

Returns:

List defining which input dimensions should be checked in graph.

Return type:

list

load(params: dict, filepath: str | None = None)[source]

Load dumped parameters to recreate the distance object

Parameters:
  • params (dict) – Dictionary containing node values

  • filepath (str, optional) – Directory containing node metadata, by default None

property output_dim

Get required output dimension

Returns:

List defining which input dimensions should be checked in graph.

Return type:

list

abstract score(data: ndarray, X: ndarray)[source]

Abstract distance method implemented by every distance type

Parameters:
  • data (np.ndarray) – Current data to compare.

  • X (np.ndarray) – Reference to compare data against.

serialize(working_dir: str) str[source]

Convert distance node to serializable format

Parameters:

working_dir (str) – Directory where node metadata should be saved.

Returns:

YAML parameterization of node.

Return type:

str

set_fit_meta_request(**kwargs)
set_forward_meta_request(**kwargs)
static spectra_to_array(ref_spectra: ndarray | list) ndarray[source]

Convert list of spectra to a numpy array

Parameters:

ref_spectra (np.ndarray | list) – Object of reference spectra

Returns:

Spectra stored in singular, indexable, sequential array.

Return type:

np.ndarray