pwspy.dataTypes.KCube

class pwspy.dataTypes.KCube(data, wavenumbers, metadata=None)[source]

Bases: pwspy.dataTypes._data.ICBase

A class representing an ImCube after being transformed from being described in terms of wavelength to wavenumber (k-space). Much of the analysis operated in terms of k-space.

Parameters
  • data (ndarray) – A 3-dimensional array containing the data. The dimensions should be [Y, X, Z] where X and Y are the spatial coordinates of the image and Z corresponds to the index dimension, e.g. wavelength, wavenumber, time, etc.

  • wavenumbers (Tuple[float]) – A sequence indicating the wavenumber associated with each 2D slice along the 3rd axis of the data array.

  • metadata (Optional[ICMetaData]) – The metadata object associated with this data object.

classmethod decodeHdf(d)

Load a new instance of ICBase from an h5py.Dataset

Parameters

d (Dataset) – The dataset that the ICBase has been saved to

Returns

(data: The 3D array of data, index: A tuple containing the index)

Return type

A tuple containing

filterDust(sigma, pixelSize)

Blurs the data cube in the X and Y dimensions. Often used to remove the effects of dust on a normalization.

Parameters
  • sigma (float) – This specifies the radius of the gaussian filter used for blurring. The units of the value are determined by pixelSize

  • pixelSize (float) – The pixel size in microns. Settings this to 1 will effectively causes sigma to be in units of pixels rather than microns.

classmethod fromHdfDataset(dataset)[source]

Load the KCube object from an h5py.Dataset in an HDF5 file

Parameters

dataset (Dataset) – The h5py.Dataset that the KCube data is stored in.

Returns

A new instance of this class.

Return type

KCube

classmethod fromImCube(cube)[source]

Convert an ImCube into a KCube. Data is converted from wavelength to wavenumber (1/lambda), interpolation is then used to linearize the data in terms of wavenumber.

Parameters

cube (ImCube) – The ImCube object to generate a KCube object from.

Return type

KCube

Returns

A new instance of KCube

static fromOpd(opd, xVals, useHannWindow)[source]

WARNING: This function is untested. it almost certainly doesn’t work. Create a KCube from and opd in the form returned by KCube.getOpd. This is useful if you want to do spectral manipulation and then transform back.

getAutoCorrelation(isAutocorrMinSub, stopIndex)[source]

The autocorrelation of a signal is the covariance of a signal with a lagged version of itself, normalized so that the covariance at zero-lag is equal to 1.0 (c[0] = 1.0). The same process without normalization is the autocovariance.

A fast method for determining the autocovariance of a signal with itself is to utilize fast-fourier transforms. In this method, the signal is converted to the frequency domain using fft. The frequency-domain signal is then convolved with itself. The inverse fft is performed on this self-convolution, yielding the autocorrelation.

In this instance, the autocorrelation is determined for a series of lags, Z. Z is equal to [-P+1:P-1], where P is the quantity of measurements in each signal (the quantity of wavenumbers). Thus, the quantity of lags is equal to (2*P)-1. The fft process is fastest when performed on signals with a length equal to a power of 2. To take advantage of this property, a Z-point fft is performed on the signal, where Z is a number greater than (2*P)-1 that is also a power of 2.

Return type

Tuple[ndarray, ndarray]

getMeanSpectra(mask=None)

Calculate the average spectra within a region of the data.

Parameters

mask (Union[Roi, ndarray, None]) – An optional other.Roi or boolean numpy array used to select pixels from the X and Y dimensions of the data array. If left as None then the full data array will be used as the region.

Return type

Tuple[ndarray, ndarray]

Returns

The average spectra within the region, the standard deviation of the spectra within the region

getOpd(isHannWindow, indexOpdStop=None, mask=None)[source]

Calculate the Fourier transform of each spectra. This can be used to get the distance (in terms of OPD) to objects that are reflecting light.

Parameters
  • isHannWindow (bool) – If True, apply a Hann window to the data before the FFT. This reduces spectral resolution but improves dynamic range and reduces “frequency leakage”.

  • indexOpdStop (Optional[int]) – This parameter is a holdover from the original MATLAB implementation. Truncates the 3rd axis of the OPD array.

  • mask (Optional[ndarray]) – A 2D boolean numpy array indicating which pixels should be processed.

Returns

opd: The 3D array of values, opdIndex: The sequence of OPD values associated with each

2D slice along the 3rd axis of the opd data.

Return type

A tuple containing

getRMSFromOPD(lowerOPD, upperOPD, useHannWindow=False)[source]

Use Parseval’s Theorem to calculate our signal RMS from the OPD (magnitude of fourier transform). This allows us to calculate RMS using only contributions from certain OPD ranges which ideally are correlated with a specific depth into the sample. In practice the large frequency leakage due to our limited bandwidth of measurement causes this assumption to break down, but it can still be useful if taken with a grain of salt.

Parameters
  • lowerOPD (float) – RMS will be integrated starting at this lower limit of OPD. Note for a reflectance setup like PWS sampleDepth = OPD / (2 * meanSampleRI)

  • upperOPD (float) – RMS will be integrated up to this upper OPD limit.

  • useHannWindow (bool) – If False then use no windowing on the FFT to calculate OPD. If True then use and Hann window.

Return type

ndarray

Returns

A 2d numpy array of the signal RMS at each XY location in the image.

plotMean()
Return type

Tuple[Figure, Axes]

Returns

A figure and attached axes plotting the mean of the data along the index axis.

corresponds to the mean reflectance in most cases.

selIndex(start, stop)
Parameters
  • start (float) – The beginning value of the index in the new object.

  • stop (float) – The ending value of the index in the new object.

Return type

Tuple[ndarray, Sequence]

Returns

A new instance of ICBase with only data from start to stop in the index.

selectLassoROI(displayIndex=None, clim=None)

Allow the user to draw a freehand ROI on an image of the acquisition.

Parameters

displayIndex (Optional[int]) – Display a particular z-slice of the array for mask drawing. If None then the mean along Z is displayed.

Return type

ndarray

Returns

An array of vertices of the polygon drawn.

selectRectangleROI(displayIndex=None)

Allow the user to draw a rectangular ROI on an image of the acquisition.

Parameters

displayIndex (int) – is used to display a particular z-slice for mask drawing. If None then the mean along Z is displayed. Returns an array of vertices of the rectangle.

Returns

An array of the 4 XY vertices of the rectangle.

Return type

np.ndarray

toHdfDataset(g, name, fixedPointCompression=True, compression=None)

Save the data of this class to a new HDF dataset.

Parameters
  • g (h5py.Group) – the parent HDF Group of the new dataset.

  • name (str) – the name of the new HDF dataset in group g.

  • fixedPointCompression (bool) – if True then save the data in a special 16bit fixed-point format. Testing has shown that this has a maximum conversion error of 1.4e-3 percent. Saving is ~10% faster but requires only 50% the hard drive space.

  • compression (Optional[str]) – The value of this argument will be passed to h5py.create_dataset for numpy arrays. See h5py documentation for available options.

Returns

This is the the same h5py.Group that was passed in a g. It should now have a new dataset by the name of ‘name’

Return type

h5py.Group

property index

Returns: The values of the datacube’s index

Return type

Tuple[float, …]