Scan

lumicks.pylake.scan.Scan

class Scan(name, file, start, stop, metadata)

A confocal scan exported from Bluelake

Parameters:
  • name (str) – Scan name

  • file (lumicks.pylake.File) – Parent file. Contains the channel data.

  • start (int) – Start point in the relevant info wave.

  • stop (int) – End point in the relevant info wave.

  • metadata (ScanMetaData) – Metadata.

__getitem__(item)

Returns specific scan frame(s) and/or cropped scans.

The first item refers to the scan frames; both indexing (by integer) and slicing are allowed, but using steps or slicing by list is not. The last two items refer to the spatial dimensions in pixel rows and columns. Only full slices (without steps) are allowed. All values should be given in pixels.

Examples

import lumicks.pylake as lk

file = lk.File("example.h5")
scan = file.scans["my_scan"]

scan[5]  # Gets the 6th frame of the scan (0 is the first).
scan[1:5]  # Get scan frames 1, 2, 3 and 4.
scan[:, 10:50, 20:50]  # Gets all frames cropped from row 11 to 50 and column 21 to 50.
scan[:, 10:50]  # Gets all frames and all columns but crops from row 11 to 50.
scan[5, 10:20, 10:20]  # Obtains the 6th frame and crops it.

scan[[1, 3, 4]]  # Produces an error, lists are not allowed.
scan[1:5:2]  # Produces an error, steps are not allowed.
scan[1, 3, 5]   # Error, integer indices are not allowed for the spatial dimensions.
scan[1, 3:5:2]  # Produces an error, steps are not allowed when slicing.

scan["1s":"5s"]  # Gets the scan frames from first to the fifth second
scan[:"-5s"]  # Gets the scan frames up to the last 5 seconds

scan[file.force1x.start:file.force1x.stop]  # Gets frames overlapping with force1x
crop_by_pixels(x_min, x_max, y_min, y_max) Scan

Crop the image stack by pixel values.

Parameters:
  • x_min (int) – minimum x pixel (inclusive, optional)

  • x_max (int) – maximum x pixel (exclusive, optional)

  • y_min (int) – minimum y pixel (inclusive, optional)

  • y_max (int) – maximum y pixel (exclusive, optional)

export_tiff(filename, *, dtype=<class 'numpy.float32'>, clip=False)

Save the RGB photon counts to a TIFF image

Parameters:
  • filename (str | os.PathLike) – The name of the TIFF file where the image will be saved.

  • dtype (np.dtype) – The data type of a single color channel in the resulting image.

  • clip (bool) – If enabled, the photon count data will be clipped to fit into the desired dtype. This option is disabled by default: an error will be raise if the data does not fit.

export_video(channel, file_name, *, start_frame=None, stop_frame=None, fps=15, adjustment=<lumicks.pylake.adjustments.ColorAdjustment object>, scale_bar=None, channel_slice=None, vertical=True, **kwargs)

Export a video

Parameters:
  • channel (str) – Color channel(s) to use “red”, “green”, “blue” or “rgb”.

  • file_name (str) – File name to export to.

  • start_frame (int) – First frame in exported video.

  • stop_frame (int) – Last frame in exported video.

  • fps (int) – Frame rate.

  • adjustment (lk.ColorAdjustment) – Color adjustments to apply to the output image.

  • scale_bar (lk.ScaleBar) – Scale bar to add to the figure.

  • channel_slice (lk.Slice, optional) – When specified, we export a video correlated to channel data

  • vertical (bool, optional) – Render with the plots vertically aligned (default: True).

  • **kwargs – Forwarded to matplotlib.pyplot.imshow().

Examples

import lumicks.pylake as lk

# Note that the examples are shown for an ImageStack, but the API for Scan stacks is
# identical.
imgs = lk.ImageStack("stack.tiff")
imgs.plot_correlated(f.force1x, frame=5, vertical=False)

# Perform a basic export of the full stack
imgs.export_video("rgb", "test.gif")

# Export the first 10 frames
imgs[:10].export_video("rgb", "test.gif")

# Export a cropped video (cropping from pixel 10 to 50 along each axis)
imgs[:, 10:50, 10:50].export_video("rgb", "test.gif")

# Export with a color adjustment using percentile based adjustment.
imgs.export_video(
    "rgb", "test.gif", adjustment=lk.ColorAdjustment(2, 98, mode="percentile"
)

# Export a gif at 50 fps
imgs.export_video("rgb", "test.gif", fps=50)

# We can also export with correlated channel data
h5_file = lk.File("stack.h5")

# Export video with correlated force data. A marker will indicate the frame.
imgs.export_video("rgb", "test.gif", channel_slice=h5_file.force1x)

# If you want the subplots side by side, pass the extra argument `vertical=False`.
imgs.export_video("rgb", "test.gif", channel_slice=h5_file.force1x, vertical=False)

# Export to a mp4 file, note that this needs ffmpeg to be installed. See:
# https://lumicks-pylake.readthedocs.io/en/latest/install.html#optional-dependencies
# for more information.
imgs.export_video("rgb", "test.mp4")
frame_timestamp_ranges(*, include_dead_time=False)

Get start and stop timestamp of each frame in the scan.

Note: The stop timestamp for each frame is defined as the first sample past the end of the relevant data such that the timestamps can be used for slicing directly.

Parameters:

include_dead_time (bool) – Include dead time at the end of each frame (default: False).

Examples

from lumicks import pylake

file = pylake.File("example.h5")
scan = file.scans["my scan"]

# Grab start and stop timestamp of the first scan.
start, stop = scan.frame_timestamp_ranges()[0]

# Plot the force data corresponding to the first scan.
file.force1x[start:stop].plot()
classmethod from_dataset(h5py_dset, file)

Construct a confocal class from dataset.

Parameters:
  • h5py_dset (h5py.Dataset) – The original HDF5 dataset containing confocal scan information

  • file (lumicks.pylake.File) – The parent file. Used to loop up channel data

get_image(channel='rgb') ndarray

Get image data for the full stack as an ndarray.

Parameters:

channel ({'red', 'green', 'blue', 'rgb'}) – The color channel of the requested data.

plot(channel='rgb', *, frame=0, adjustment=<lumicks.pylake.adjustments.ColorAdjustment object>, axes=None, image_handle=None, show_title=True, show_axes=True, scale_bar=None, **kwargs)

Plot a scan frame for the requested color channel(s).

Parameters:
  • channel ({"red", "green", "blue", "rgb"}, optional) – Color channel to plot.

  • frame (int, optional) – Index of the frame to plot.

  • adjustment (lk.ColorAdjustment) – Color adjustments to apply to the output image.

  • axes (matplotlib.axes.Axes, optional) – If supplied, the axes instance in which to plot.

  • image_handle (matplotlib.image.AxesImage or None) – Optional image handle which is used to update plots with new data rather than reconstruct them (better for performance).

  • show_title (bool, optional) – Controls display of auto-generated plot title

  • show_axes (bool, optional) – Setting show_axes to False hides the axes.

  • scale_bar (lk.ScaleBar, optional) – Scale bar to add to the figure.

  • **kwargs – Forwarded to matplotlib.pyplot.imshow(). These arguments are ignored if image_handle is provided.

Returns:

The image handle representing the plotted image.

Return type:

matplotlib.image.AxesImage

plot_correlated(channel_slice, frame=0, reduce=<function mean>, channel='rgb', figure_scale=0.75, adjustment=<lumicks.pylake.adjustments.ColorAdjustment object>, *, vertical=False, include_dead_time=False, return_frame_setter=False)

Downsample channel on a frame by frame basis and plot the results. The downsampling function (e.g. np.mean) is evaluated for the time between a start and end time of a frame. Note: In environments which support interactive figures (e.g. jupyter notebook with ipywidgets or interactive python) this plot will be interactive.

Actions:

left-click in the left axes – Show the corresponding image frame in the right axes.

Parameters:
  • channel_slice (pylake.channel.Slice) – Data slice that we with to downsample.

  • frame (int) – Frame to show.

  • reduce (callable) – The function which is going to reduce multiple samples into one. The default is numpy.mean(), but numpy.sum() could also be appropriate for some cases e.g. photon counts.

  • channel ('rgb', 'red', 'green', 'blue', None; optional) – Channel to plot for RGB images (None defaults to ‘rgb’) Not used for grayscale images

  • figure_scale (float) – Scaling of the figure width and height. Values greater than one increase the size of the figure.

  • adjustment (lk.ColorAdjustment) – Color adjustments to apply to the output image.

  • vertical (bool, optional) – Align plots vertically.

  • include_dead_time (bool, optional) – Include dead time between scan frames.

  • return_frame_setter (bool, optional) – Whether to return a handle that allows updating the plotted frame.

Examples

from lumicks import pylake

file = pylake.File("example.h5")
scan = file.scans["my scan"]
scan.plot_correlated(file.force1x, channel="red")
property center_point_um

Returns a dictionary of the x/y/z center coordinates of the scan (w.r.t. brightfield field of view)

property fast_axis

The axis that was scanned (“X” or “Y”)

property lines_per_frame

Number of scanned lines in each frame

property num_frames

Number of available frames

property pixel_time_seconds

Pixel dwell time in seconds

property pixels_per_line

Number of pixels in each line

property pixelsize_um

Returns a List of axes dimensions in um. The length of the list corresponds to the number of scan axes.

property shape

Shape of the reconstructed Scan image

property size_um

Returns a List of scan sizes in um along axes. The length of the list corresponds to the number of scan axes.

property timestamps: ndarray

Timestamps for each image pixel.

The returned array has the same shape as the color_image arrays. Timestamps are defined at the mean of the timestamps.