Autofocus

Classes

class pyseq.focus.Autofocus(hs, pos_dict)

Autofocus.

The autofocus will follow the routine set in hs.AF

Autofocus routines:
  • full: Scan the entire section at a predetermined objective position, even if a previous in focus objective position was found. Use for small sections and the zstage has changed positions.

  • partial: Image only center of section at a predetermined objective position, even if a previous in focus objective position was found. Use for large sections and the zstage has changed positions.

  • full once: Scan the entire section at a predetermined objective position, only if a previous in focus objective position has not been found. Use for small sections and the zstage has not changed position.

  • partial once: Image only center of section at a predetermined objective position, only if a previous in focus objective position has not been found. Use for large sections and the zstage has not changed position.

  • preset: An integer objective step can be provided to skip autofocusing and immediately begin imaging.

Attributes:
  • hs (HiSeq): HiSeq object.

  • pos_dict (int): Stage position information of the section.

  • rough_ims (HiSeqImages): Dataarray out of focus images of the section.

  • scale (int): Down scale factor for thumbnails.

  • logger (logger): Logger object to log communication with HiSeq.

delete_focus_images()

Delete images used for focusing.

fit_mixed_gaussian(data)

Fit focus data & return optimal objective focus step.

Focus objective step vs frame JPEG file size is fit to a mixed gaussian model. The optimal objective focus step is returned at step of the max fit JPEG file. If the objective focus step is not returned, False is returned.

Parameters:
  • data (array Nx2): Focus data where the 1st column is the objective step and the 2nd column is the corresponding file size.

Returns:
  • int: Optimal focus objective step if found (if not, False).

format_focus(focus_stack, filename=None)

Return processed focus frame file sizes.

Objective positions are calculated for corresponding frames. The objective position series of JPEG frame sizes from each camera are determined to contain a signal if the kurtosis of the series is significant. Series with signals are summed and normalized.

Parameters:
  • focus_data (Nx4 array): JPEG frame file sizes from the 4 cameras.

Returns:
  • array (Nx2): The 1st column is the objective step and the 2nd column is the corresponding processed frame size.

full_scan(image_name='RoughScan')

Scan entire out of focus section.

The section is imaged at a predefined objective position. Multiple images comprising the section are stitched together and possiblly scaled down (if the images are larger the 256 kb) and then normalized.

Parameters:
  • image_name (str): Common name for images, default is ‘RoughScan’.

Returns:
  • bool: True if scan is succesful, False otherwise

get_focus_data(px_points, n_markers)

Return stage position focal point at each pixel point.

Loop through candidate focus px_points, and take an objective stack at each position until n_markers with an optimal objective position have been found.

Parameters:
  • px_points (2xN array): Row x Column position of candidate focal points

  • n_markers (int): Number of focus markers to find

Returns:
  • 4x*n_markers array*: X-stage step, Y-stage step, Obj step, and index of focal position.

message(*args)

Print output text to console (default) or log file.

If there is no logger, text is printed to the console. If a logger is assigned, and the first argument is False, text is printed to the log file, otherwise text is printed to the console.

partial_scan(image_name='RoughScan')

Out of focus center scan of the section.

The middle full length of the section is imaged at a predefined, probably out of focus, objective position. The channel images are scaled down (if the images are larger the 256 kb) and then normalized.

Parameters:
  • image_name (str): Common name for images, default is ‘RoughScan’.

Returns:
  • bool: True if scan is succesful, False otherwise.

pyseq.focus.autofocus(hs, pos_dict)

Finds and returns the objective step for optimal focus.

The section defined by the pos_dict is imaged at a predefined objective position. Channel images with signal are summed together into 1 image. Ideal positions to focus on are extracted from the summed image. The optimal focus objective step are found from a number of focus positions and the median objective step is returned.

Parameters:
  • hs (HiSeq): HiSeq object.

  • pos_dict (dict): Dictionary of stage position information.

Returns:
  • int: Objective step for optimal focus.

pyseq.focus.autolevel(hs, n_ip, centroid)

Level stage, work in progress.

pyseq.focus.gaussian(x, *args)

Gaussian function for curve fitting.

pyseq.focus.get_jpeg_size(obj_stack)

Return filesizes of frames as jpegs.

pyseq.focus.get_obj_pos(hs, section, cycle)

Read the objective position at cycle number for section.

Used to specify/change the objective position used for imaging or re-autofocus on the section next imaging round. Specifying the objective position at a cycle prior to imaging will skip the autofocus routine and start imaging the section at the specified objective position. If using the ‘partial once’ or ‘full once’ autofocus routine and the objective position is specifed as None at a cycle prior to imaging, the previously used objective position will be discarded and a new objective position will be found with the autofocus routine.

Parameters: - hs (HiSeq): HiSeq object - section (string): Name of the section. - cycle (int): Cycle number.

Returns: - int: Objective position to use (or None if not specified)

pyseq.focus.planeFit(points)

Fit points to a plane.

Code copied from math stackexchange

Given an array, points, of shape (d,…) representing points in d-dimensional space, fit an d-dimensional plane to the points.

Example:

p, n = planeFit(points)
Parameters:
  • points (array): Points of shape (d,…) representing points in

    d-dimensional space.

Returns:
  • array: Point, p, on the plane (the point-cloud centroid)

  • array: Normal, n of the plane

pyseq.focus.res_gaussian(args, xfun, yfun)

Gaussian residual function for curve fitting.

pyseq.focus.userYN(*args)

Ask a user a Yes/No question and return True if Yes, False if No.

pyseq.focus.write_obj_pos(hs, section, cycle, step=None)

Write the objective position used at cycle number for section.

The objective position is written to a config file. Each section in config file corresponds to a section name on a flowcell. Each item in a section is a cycle number with the objective position used to image at that cycle.

Parameters: - hs (HiSeq): HiSeq Object - section (string): Name of the section. - cycle (int): Cycle number. - step (int): Focus objective position to save.

Returns: - file: Handle of the config file.