Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Created April 10, 2020 21:37
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 thomasaarholt/bfaed87a353d11e56d41e266743c3ded to your computer and use it in GitHub Desktop.
Save thomasaarholt/bfaed87a353d11e56d41e266743c3ded to your computer and use it in GitHub Desktop.
hyperspy rotation of axes calibrated
%matplotlib widget
import hyperspy.api as hs
import numpy as np
import matplotlib.pyplot as plt
s = hs.datasets.example_signals.object_hologram()
ax0 = s.axes_manager[0]
ax1 = s.axes_manager[1]
ax0.scale=2
ax1.scale=1
A = np.repeat(s.axes_manager[0].axis[:, None], 2, axis=1)
B = np.repeat(s.axes_manager[1].axis[:, None], 2, axis=1)
A[:,1] = ax1.low_value
B[:,0] = ax0.low_value
def rotmatrix(theta):
theta = np.deg2rad(theta)
c,s = np.cos(theta), np.sin(theta)
return np.array(
[[c, -s],[s, c]]
)
def rotate_axis(theta, ax1, ax2):
center = np.array(
[ax1.low_value + (ax1.high_value - ax1.low_value)/2,
ax2.low_value + (ax2.high_value - ax2.low_value)/2]
)
offset = np.array([ax1.offset, ax2.offset])
ax1_new_offset, ax2_new_offset = rotmatrix(theta) @ (offset - center) + center
ax1_new_scale, ax2_new_scale = np.abs(rotmatrix(theta) @ (ax1.scale, ax2.scale))
return (ax1_new_offset, ax1_new_scale), (ax2_new_offset, ax2_new_scale)
theta = -100
center = np.array(
[ax0.low_value + (ax0.high_value - ax0.low_value)/2,
ax1.low_value + (ax1.high_value - ax1.low_value)/2]
)
A2 = (rotmatrix(theta) @ (A - center).T).T + center
B2 = (rotmatrix(theta) @ (B - center).T).T + center
((xo, xs), (yo, ys)) = rotate_axis(theta, ax0, ax1)
plt.figure()
plt.plot(*A.T, color='blue')
plt.plot(*A2.T, color='red')
plt.plot(*B.T, color='blue')
plt.plot(*B2.T, color='red')
plt.scatter(xo, yo)
plt.xlim(ax0.low_value-100, ax0.high_value+100)
plt.ylim(ax1.low_value-100, ax1.high_value+100)
plt.axis('equal')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment