Similarity Metrics¶
spectrakit.similarity.similarity_cosine ¶
Compute cosine similarity between spectra.
For single spectra (1-D), returns a scalar. For a query (1-D) against a library (2-D), returns an array of similarities. For a batch of queries (2-D) against a library (2-D), returns a similarity matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
ndarray
|
Query spectrum, shape |
required |
reference
|
ndarray
|
Reference spectrum shape |
required |
Returns:
| Type | Description |
|---|---|
float | ndarray
|
Cosine similarity in [-1, 1]. |
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If query or reference is not 1-D or 2-D. |
EmptySpectrumError
|
If inputs have zero elements. |
Source code in src/spectrakit/similarity/cosine.py
spectrakit.similarity.similarity_pearson ¶
Compute Pearson correlation between spectra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
ndarray
|
Query spectrum, shape |
required |
reference
|
ndarray
|
Reference spectrum shape |
required |
Returns:
| Type | Description |
|---|---|
float | ndarray
|
Pearson r in [-1, 1]. Returns |
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If query or reference is not 1-D or 2-D. |
EmptySpectrumError
|
If inputs have zero elements. |
Note
The 2-D × 2-D case uses matrix multiplication (O(M*N*W)).
For large M or N, consider batching queries to manage memory.
Source code in src/spectrakit/similarity/pearson.py
spectrakit.similarity.similarity_spectral_angle ¶
Compute Spectral Angle Mapper (SAM) between spectra.
Returns the angle in radians between spectral vectors. Smaller angle means more similar. Range: [0, pi].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
ndarray
|
Query spectrum, shape |
required |
reference
|
ndarray
|
Reference spectrum shape |
required |
Returns:
| Type | Description |
|---|---|
float | ndarray
|
Angle in radians in [0, pi]. |
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If query or reference is not 1-D or 2-D. |
EmptySpectrumError
|
If inputs have zero elements. |
Source code in src/spectrakit/similarity/spectral_angle.py
spectrakit.similarity.similarity_euclidean ¶
Compute Euclidean distance between spectra.
Lower values indicate greater similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
ndarray
|
Query spectrum, shape |
required |
reference
|
ndarray
|
Reference spectrum shape |
required |
Returns:
| Type | Description |
|---|---|
float | ndarray
|
Euclidean distance in [0, inf). |
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
float | ndarray
|
|
Raises:
| Type | Description |
|---|---|
SpectrumShapeError
|
If query or reference is not 1-D or 2-D. |
EmptySpectrumError
|
If inputs have zero elements. |
Note
The 2-D × 2-D case allocates an (M, N, W) intermediate array.
For large M, N, or W, consider batching queries to manage memory.