Read & Write a DataCube

The hsi-wizard module provides basic functions for reading and writing a DataCube. For more information visit loader chapter.

Supported DataCube File Formats

Extension

Read Supported

Write Supported

.csv

.xlsx

.tdms

.fsm

.folder

.nrrd

.image

.hdr

Note

The wizard.read() function serves as the main entry point for users. It abstracts the file handling process and automatically determines the appropriate handler based on the file type.

Writing a DataCube

csv

This snippet shows how to generate a random DataCube, write it to a .csv file using the wizard._utils._loader.csv module, and read it back into a new DataCube, preserving the original data shape and structure.

"""
Demonstrate DataCube CSV serialization and deserialization.
"""

import wizard
import numpy as np
from wizard._utils._loader import csv

# generate a random DataCube with 22 spectral bands and a 10×8 spatial grid
dc = wizard.DataCube(np.random.rand(22, 10, 8))

# write the DataCube to 'test.csv'
csv._write_csv(dc, filename='test.csv')

# load DataCube directly using wizard.read
new_dc = wizard.read('test.csv')
# csv._read_csv also works
# new_dc = csv._read_csv('test.csv')

# confirm that the read-back DataCube matches the original shape
print(new_dc.shape)

Output:

(22, 10, 8)

xlsx

Similar to the snippet above, this example shows how to generate a random DataCube, write it to a .xlsx file using the wizard._utils._loader.xlsx module, and read it back into a new DataCube.

"""
Demonstrate DataCube XLSX serialization and deserialization.
"""

import os
import wizard
import numpy as np
from wizard._utils._loader import xlsx

# generate a random DataCube with 22 spectral bands and a 10×8 spatial grid
dc = wizard.DataCube(np.random.rand(22, 10, 8))

# write the DataCube to 'test.xlsx'
xlsx._write_xlsx(dc, filename='test.xlsx')

# load DataCube directly from the xlsx file
new_dc = xlsx._read_xlsx('test.xlsx')

# confirm that the read-back DataCube matches the original shape
print(new_dc.shape)

Output:

(22, 10, 8)