playnano.processing.filters module

2D filters for flattening and smoothing AFM frames.

Each filter takes one frame as a 2D numpy.ndarray and returns the result; the input is not changed. The ProcessingPipeline applies them to every frame of a stack. Masked versions of the flattening filters are in playnano.processing.masked_filters.

playnano.processing.filters.gaussian_filter(data: ndarray, sigma: float = 1.0) → ndarray[source]

Smooth the image with a Gaussian filter to reduce high-frequency noise.

Parameters:
  • data (numpy.ndarray) – 2D AFM image.

  • sigma (float, optional) – Standard deviation of the Gaussian kernel, in pixels. Default is 1.0.

Returns:

Smoothed image, with the same dtype as data.

Return type:

numpy.ndarray

See also

scipy.ndimage.gaussian_filter

The function used.

playnano.processing.filters.polynomial_flatten(data: ndarray, order: int = 2) → ndarray[source]

Fit a 2D polynomial surface to the image and subtract it.

Removes tilt (order 1) and curvature (order 2 and above) from the frame.

Parameters:
  • data (numpy.ndarray) – 2D AFM image.

  • order (int, optional) – Total order of the polynomial: 1 is a plane, 2 is quadratic. Default is 2.

Returns:

Image with the polynomial background removed, as float64.

Return type:

numpy.ndarray

Raises:
  • ValueError – If data is not a 2D array, if order is not a positive integer, or if fewer pixels are finite than the polynomial has terms ((order + 1) * (order + 2) / 2).

  • RuntimeError – If the least-squares fit fails.

Notes

NaN and infinite pixels are left out of the fit and stay NaN or infinite in the output. Version 0.2.0 scales x and y to [-1, 1] before fitting, so orders of 3 and above fit correctly on large frames; before, terms were silently dropped and the background was not fully removed.

playnano.processing.filters.register_filters()[source]

Return the built-in 2D filters.

Returns:

Maps each step name (str) to its function, for example "remove_plane" to remove_plane().

Return type:

dict

playnano.processing.filters.remove_plane(data: ndarray) → ndarray[source]

Fit a plane to the image by least squares and subtract it.

The plane z = a*x + b*y + c removes the overall tilt of the frame.

Parameters:

data (numpy.ndarray) – 2D AFM image.

Returns:

Image with the plane removed, as float64.

Return type:

numpy.ndarray

Raises:

ValueError – If fewer than 3 pixels are finite.

Notes

NaN and infinite pixels are left out of the fit and stay NaN or infinite in the output. Version 0.1.1 fits with NumPy instead of scikit-learn (same result) and handles NaN pixels instead of raising.

playnano.processing.filters.row_median_align(data: ndarray) → ndarray[source]

Subtract the median of each row from that row to remove horizontal banding.

Parameters:

data (numpy.ndarray) – 2D AFM image.

Returns:

Row-aligned image, as float64.

Return type:

numpy.ndarray

playnano.processing.filters.vertical_flip(data: ndarray) → ndarray[source]

Flip the image vertically, reversing the order of the rows.

Parameters:

data (numpy.ndarray) – 2D AFM image.

Returns:

Flipped image (a view of data).

Return type:

numpy.ndarray

playnano.processing.filters.zero_mean(data: ndarray) → ndarray[source]

Subtract the mean height of the image so it is centred on zero.

Parameters:

data (numpy.ndarray) – 2D AFM image.

Returns:

Zero-mean image, as float64.

Return type:

numpy.ndarray

See also

playnano.processing.masked_filters.zero_mean_masked

The same, with the mean taken over background pixels only.

playnano.processing.filters.zero_median(data: ndarray) → ndarray[source]

Subtract the median height of the image so it is centred on zero.

Parameters:

data (numpy.ndarray) – 2D AFM image.

Returns:

Zero-median image, as float64.

Return type:

numpy.ndarray

See also

playnano.processing.masked_filters.zero_median_masked

The same, with the median taken over background pixels only.