Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Last active June 25, 2019 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GenevieveBuckley/9b3a3df34b942f8a959dbfc4322015a5 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/9b3a3df34b942f8a959dbfc4322015a5 to your computer and use it in GitHub Desktop.
Autoscript crosshair pattern for drift correction reference
import numpy as np
import autoscript_toolkit.vision as vision_toolkit
def cut_crosshair_patch(center_coordinate_pixels, image):
"""Returns cropped image region with crosshair drift correction reference.
Parameters
----------
center_coordinate_pixels : List-like
Row, column format & units of pixels.
image : AdornedImage
Returns
-------
cropped_image
AdornedImage of cropped region centered at position of crosshairs.
"""
center_y_pixels, center_x_pixels = center_coordinate_pixels
cropped_image_size = [0.1 * np.min(image.data.shape)] * 2
cropped_image = vision_toolkit.cut_image(image, center=np.flip(center_coordinate_pixels, axis=0), size=cropped_image_size)
return cropped_image
def crosshair_milling(center_coordinate_pixels, image, milling_depth=2e-6,
display_matplotlib=False):
"""Returns crosshair milling lines for drift correction reference.
Parameters
----------
center_coordinate_pixels : List-like
Row, column format & units of pixels.
image : AdornedImage
milling_depth : float, optional
Depth to mill crosshair pattern, by default 2e-6
display_matplotlib : bool, optional
Visualize crosshair pattern with matplotlib, by default False
Returns
-------
crosshair_horizontal, crosshair_vertical
Autoscript line patterns for ion beam milling.
"""
microscope.patterning.clear_patterns()
center_y_pixels, center_x_pixels = center_coordinate_pixels
half_length_pixels = 0.05 * np.min(image.data.shape)
# Pixel coordinates
# Horizontal line
start_horizontal_x_pixels = numpy.clip(
center_x_pixels - half_length_pixels, 1, image.data.shape[1] - 1)
start_horizontal_y_pixels = numpy.clip(
center_y_pixels, 1, image.data.shape[0])
end_horizontal_x_pixels = numpy.clip(
center_x_pixels + half_length_pixels, 1, image.data.shape[1] - 1)
end_horizontal_y_pixels = numpy.clip(
center_y_pixels, 1, image.data.shape[0] - 1)
# Vertical line
start_vertical_x_pixels = numpy.clip(
center_x_pixels, 1, image.data.shape[1] - 1)
start_vertical_y_pixels = numpy.clip(
center_y_pixels - half_length_pixels, 1, image.data.shape[0] - 1)
end_vertical_x_pixels = numpy.clip(
center_x_pixels, 1, image.data.shape[1] - 1)
end_vertical_y_pixels = numpy.clip(
center_y_pixels + half_length_pixels, 1, image.data.shape[0] - 1)
# Visualize
if display_matplotlib:
plt.imshow(image.data, cmap='gray')
plt.plot([start_horizontal_x_pixels, end_horizontal_x_pixels],
[start_horizontal_y_pixels, end_horizontal_y_pixels], 'y')
plt.plot([start_vertical_x_pixels, end_vertical_x_pixels],
[start_vertical_y_pixels, end_vertical_y_pixels], 'y')
plt.show()
# Realspace coordinates
pixel_size = image.metadata.binary_result.pixel_size
half_length_realspace = half_length_pixels * pixel_size.x
center_realspace = _pixel_coord_to_realspace(
center_coordinate_pixels, pixel_size, image.data.shape)
# Horizontal milling line pattern
start_horizontal_y, start_horizontal_x = _pixel_coord_to_realspace(
[start_horizontal_y_pixels, start_horizontal_x_pixels],
pixel_size, image.data.shape)
end_horizontal_y, end_horizontal_x = _pixel_coord_to_realspace(
[end_horizontal_y_pixels, end_horizontal_x_pixels],
pixel_size, image.data.shape)
# Vertical milling line pattern
start_vertical_y, start_vertical_x = _pixel_coord_to_realspace(
[start_vertical_y_pixels, start_vertical_x_pixels],
pixel_size, image.data.shape)
end_vertical_y, end_vertical_x = _pixel_coord_to_realspace(
[end_vertical_y_pixels, end_vertical_x_pixels],
pixel_size, image.data.shape)
# Create Autoscript miling patterns for crosshairs
crosshair_horizontal = microscope.patterning.create_line(
start_horizontal_x, start_horizontal_y,
end_horizontal_x, end_horizontal_y, milling_depth)
crosshair_vertical = microscope.patterning.create_line(
start_vertical_x, start_vertical_y,
end_vertical_x, end_vertical_y, milling_depth)
return crosshair_horizontal, crosshair_vertical
def select_point(image):
fig, ax = quick_plot(image)
ax = plt.gca()
xy = plt.ginput(1)
return xy[0]
import numphy as np
import matplotlib.pyplot as plt
import numpy as np
from autoscript_sdb_microscope_client import SdbMicroscopeClient
from autoscript_sdb_microscope_client.enumerations import *
from autoscript_sdb_microscope_client.structures import *
microscope = SdbMicroscopeClient()
microscope.connect('10.0.0.1')
microscope.imaging.set_active_device(ImagingDevice.ION_BEAM)
image = microscope.imaging.get_image()
coord = select_point(image)
height = 0.12 * image.data.shape[1] * pixelsize.x
width = 0.03 * image.data.shape[1] * pixelsize.x
rect = microscope.patterning.create_rectangle(coord[0], coord[1], width, height,
1e-6)
rect2 = microscope.patterning.create_rectangle(coord[0], coord[1], height, width
, 1e-6)
import matplotlib.pyplot as plt
def select_line(image):
"""Interactively select a line with matplotlib.
Adapted from this StackOverflow answer by user dawe:
https://stackoverflow.com/questions/9136938/matplotlib-interactive-graphing-manually-drawing-lines-on-a-graph
Parameters
----------
image : AdornedImage
Returns
-------
line_coordinates
Row, column format & units of pixels.
"""
image = fibsem.conversions._convert_to_numpy(image)
fig, ax = plt.subplots(1)
ax.imshow(image, cmap='gray')
ax = plt.gca()
xy = plt.ginput(2)
x = [p[0] for p in xy]
y = [p[1] for p in xy]
line = plt.plot(x, y, 'y')
ax.figure.canvas.draw()
line_coordinates = [(xy[0][1], xy[0][0]), (xy[1][1], xy[1][0])]
return line_coordinates
# Note that this deprecation warning is produced:
# site-packages\matplotlib\backend_bases.py:2445: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
# warnings.warn(str, mplDeprecation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment