Create a DataCube
A DataCube is a specialized class for hyperspectral data, combining spectral and spatial information into a single object. Internally, it stores your measurements as a NumPy array of shape (v, x, y), where:
v is the number of spectral channels (e.g., different wavelengths),
x and `y are the spatial dimensions (e.g., image width and height).
The optinonal accompanying attribute wavelength is a list of length v, in which the wavelength corresponding to the individual spectral slices is specified. If no wavelengths are passed, a wavelength attribute from 0 to v is automatically set.
In many workflows, your data may already reside in a NumPy array. This example demonstrates how to wrap an existing array in a DataCube, making it easy to integrate with analysis routines without first writing out to a file. Simply pass your array and the matching wavelength list directly to the constructor.
Warning
Warning: DataCube does not perform automatic validation of the array’s shape or check that you’ve used the correct (v, x, y) ordering. It assumes you know your data structure and provides no spectral/spatial checks under the hood. Incorrectly ordered or shaped arrays may lead to unexpected results.
Example
"""
Create and demonstrate DataCube creation and indexing.
This example shows how to wrap an existing NumPy array in a DataCube,
inspect its core attributes, specify custom wavelength values, and
extract spatial slices at a given wavelength index using two indexing methods.
"""
import wizard
import numpy as np
# Generate a dummy hyperspectral dataset with 20 bands and 8×9 pixels
data = np.zeros(shape=(20, 8, 9))
# Create a DataCube with default wavelengths (0–19)
dc = wizard.DataCube(data, name='Hello DataCube')
print(dc)
# Inspect core attributes
print("Wavelength values:", dc.wavelengths)
# Create a DataCube with custom wavelength assignments (400–700 nm)
custom_wls = np.linspace(400, 700, data.shape[0], dtype='int').tolist()
dc_vis = wizard.DataCube(data, name='Visible Spectrum Cube', wavelengths=custom_wls)
print("\n", dc_vis)
print("Wavelength values:", dc_vis.wavelengths)
Here’s what our DataCube prints out:
Name: Hello DataCube Shape: (20, 8, 9) Wavelengths: Len: 20 From: 0 To: 19 Wavelength values: [ 0 1 2 … 19]Name: Visible Spectrum Cube Shape: (20, 8, 9) Wavelengths: Len: 20 From: 400 To: 700 Wavelength values: [400 415 431 … 700]