pwspy.dataTypes.ImCube

class pwspy.dataTypes.ImCube(data, metadata, processingStatus=None, dtype=<class 'numpy.float32'>)[source]

Bases: pwspy.dataTypes._data.ICRawBase

A class representing a single PWS acquisition. Contains methods for loading and saving to multiple formats as well as common operations used in analysis.

Parameters
  • data – 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.

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

  • processingStatus (Optional[ProcessingStatus]) – An object that keeps track of which processing steps and corrections have been applied to this object.

  • dtype (type) – the data type that the data should be stored as. The default is numpy.float32.

class ProcessingStatus(cameraCorrected=False, normalizedByExposure=False, extraReflectionSubtracted=False, normalizedByReference=False)

Bases: object

Keeps track of which processing steps have been applied to an ICRawBase object. By default none of these things have been done for raw data

classmethod fromDict(d)
toDict()
Return type

dict

correctCameraEffects(correction=None, binning=None)

Subtracts the darkcounts from the data. count is darkcounts per pixel. binning should be specified if it wasn’t saved in the micromanager metadata. Both method arguments should be able to be loaded automatically from the metadata but for older data files they will need to be supplied manually.

Parameters
  • correction (Optional[CameraCorrection]) – The cameracorrection object providing information on how to correct the data.

  • binning (Optional[int]) – The binning that the raw data was imaged at. 2 = 2x2 binning, 3 = 3x3 binning, etc.

classmethod decodeHdf(d)

Load a new instance of ICRawBase from an h5py.Dataset

Parameters

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

Returns

data: The 3D array of data index: A tuple containing the index metadata: A dictionary containing metadata. procStatus: The processing status of the object.

Return type

A tuple containing

filterDust(kernelRadius, pixelSize=None)[source]

This method blurs the data of the ImCube along the X and Y dimensions. This is useful if the ImCube is being used as a reference to normalize other ImCube. It helps blur out dust adn other unwanted small features.

Parameters
  • kernelRadius (float) – The sigma of the gaussian kernel used for blurring. A greater value results in greater blurring. If pixelSize is provided then this is in units of pixelSize, otherwise it is in units of pixels.

  • pixelSize (Optional[float]) – The size (usualy in units of microns) of each pixel in the datacube. This can generally be loaded automatically from the metadata.

Return type

None

classmethod fromHdfDataset(d)[source]

Load an Imcube from an HDF5 dataset.

classmethod fromMetadata(meta, lock=None)[source]

If provided with an ICMetadata object this function will automatically select the correct file loading method and will return the associated ImCube.

Parameters
  • meta (ICMetaData) – The metadata to use to load the object from.

  • lock (Optional[Lock]) – A Lock object used to synchronized IO in multithreading and multiprocessing applications.

Return type

ImCube

Returns

A new instance of ImCube.

classmethod fromNano(directory, metadata=None, lock=None)[source]

Loads from the file format used at NanoCytomics. all data and metadata is contained in a .mat file.

Parameters
  • directory (str) – The directory containing the data files.

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

  • lock (Optional[Lock]) – A Lock object used to synchronized IO in multithreading and multiprocessing applications.

Return type

ImCube

Returns

A new instance of ImCube.

classmethod fromOldPWS(directory, metadata=None, lock=None)[source]

Loads from the file format that was saved by the all-matlab version of the Basis acquisition code. Data was saved in raw binary to a file called image_cube. Some metadata was saved to .mat files called info2 and info3.

Parameters
  • directory (str) – The directory containing the data files.

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

  • lock (Optional[Lock]) – A Lock object used to synchronized IO in multithreading and multiprocessing applications.

Returns

A new instance of ImCube.

classmethod fromTiff(directory, metadata=None, lock=None)[source]

Loads from a 3D tiff file named pws.tif, or in some older data MMStack.ome.tif. Metadata can be stored in the tags of the tiff file but if there is a pwsmetadata.json file found then this is preferred. A multiprocessing.Lock object can be passed to this function so that it will acquire a lock during the hard-drive intensive parts of the function. this is useful in multi-core contexts.

Parameters
  • directory – The directory containing the data files.

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

  • lock (Optional[Lock]) – A Lock object used to synchronized IO in multithreading and multiprocessing applications.

Returns

A new instance of ImCube.

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

static getMetadataClass()[source]

Returns: The metadata class associated with this subclass of ICRawBase

Return type

Type[ICMetaData]

classmethod loadAny(directory, metadata=None, lock=None)[source]

Attempt to load a ImCube for any format of file in directory

Parameters
  • directory (str) – The directory containing the data files.

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

  • lock (Optional[Lock]) – A Lock object used to synchronized IO in multithreading and multiprocessing applications.

Returns

A new instance of ImCube.

normalizeByExposure()

This is one of the first steps in most analysis pipelines. Data is divided by the camera exposure. This way two ImCube that were acquired at different exposure times will still be on equivalent scales.

normalizeByReference(reference)[source]

Normalize the raw data of this data cube by a reference cube to result in data representing arbitrarily scaled reflectance.

Parameters

reference (ImCube) – A reference acquisition (Usually a blank spot on a dish). The data of this acquisition will be divided by the data of the reference

performFullPreProcessing(reference, referenceMaterial, extraReflectance, cameraCorrection=None)

Use the subtractExtraReflection, normalizeByReference, correctCameraEffects, and normalizeByExposure methods to perform the standard pre-processing that is done before analysis.

Note: This will also end up applying corrections to the reference data. If you want to perform pre-processing on a whole batch of data then you should implement your own script based on what is done here.

Parameters
  • reference ('self.__class__') – A data cube to be used as a reference for normalization. Usually an image of a blank dish with cell media or air.

  • referenceMaterial (Material) – The material that was imaged in the reference dish. The theoretically expected reflectance will be calculated assuming a “Glass/{Material}” reflective interface.

  • extraReflectance (ExtraReflectanceCube) – The data cube containing system internal reflectance calibration information about the specific system configuration that the data was taken with.

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)[source]
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

ImCube

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

subtractExtraReflection(extraReflection)[source]

Subtract the portion of the signal that is due to internal reflections of the optical system from the data.

Parameters

extraReflection (ExtraReflectionCube) – A calculated data cube indicating in units of camera counts how much of the data is from unwanted internal reflections of the system.

toHdfDataset(g, name, fixedPointCompression=True)

Save this object into an HDF dataset.

Parameters
  • g (Group) – The h5py.Group object to create a new dataset in.

  • name (str) – The name of the new dataset.

  • fixedPointCompression (bool) – If True then the data will be converted from floating point to 16-bit fixed point. This results in approximately half the storage requirements at a very slight loss in precision.

Return type

Group

Returns

A reference to the h5py.Group passed in as g.

toOldPWS(directory)[source]

Save this object to the old .mat based storage format.

Parameters

directory – The path to the folder to save the data files to.

toTiff(outpath, dtype=<class 'numpy.uint16'>)[source]

Save the ImCube to the standard TIFF file format.

Parameters

outpath (str) – The path to save the new TIFF file to.

property index

Returns: The values of the datacube’s index

Return type

Tuple[float, …]

property wavelengths

A tuple containing the values of the wavelengths for the data.