def test_2d_diagonal(self): array = np.asarray([np.arange(6) for _ in range(6)]) expected = np.array([ 0, 0.71428571, 1.42857143, 2.14285714, 2.85714286, 3.57142857, 4.28571429, 5.0, ]) output = gu.linecut(array, indices=[(0, 5), (0, 5)], interp_order=1) self.assertTrue(np.allclose(output, expected))
def test_3d_diagonal(self): array = np.asarray([[np.arange(6) for _ in range(6)] for _ in range(6)]) expected = np.array([ 0, 0.55555556, 1.11111111, 1.66666667, 2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5.0, ]) output = gu.linecut(array, indices=[(0, 5), (0, 5), (0, 5)], interp_order=1) self.assertTrue(np.allclose(output, expected))
def on_click(event): """ Function to interact with a plot, return the position of clicked pixel. If flag_pause==1 or if the mouse is out of plot axes, it will not register the click :param event: mouse click event """ global diff_pattern, starting_point, endpoint, distance, fig_diff, ax0, ax1, ax2 global q_text, load_qvalues, vmin, vmax, qx, qy, qz, ax3, plt0, plt1, plt2, cut global plt0_start, plt1_start, plt2_start, plt0_stop, plt1_stop, plt2_stop if event.inaxes == ax3: # print the distance value at the mouse position q_text.remove() if load_qvalues: q_text = fig_diff.text( 0.55, 0.43, f"distance = {event.xdata:.3f} (1/A)", size=10 ) else: q_text = fig_diff.text(0.55, 0.43, f"distance = {event.xdata:.1f}", size=10) plt.draw() update_cut = False else: if event.button == 1: # left button if event.inaxes == ax0: # hor=X, ver=Y starting_point[2], starting_point[1] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True elif event.inaxes == ax1: # hor=X, ver=rocking curve starting_point[2], starting_point[0] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True elif event.inaxes == ax2: # hor=Y, ver=rocking curve starting_point[1], starting_point[0] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True else: update_cut = False elif event.button == 3: # right button if event.inaxes == ax0: # hor=X, ver=Y endpoint[2], endpoint[1] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True elif event.inaxes == ax1: # hor=X, ver=rocking curve endpoint[2], endpoint[0] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True elif event.inaxes == ax2: # hor=Y, ver=rocking curve endpoint[1], endpoint[0] = ( int(np.rint(event.xdata)), int(np.rint(event.ydata)), ) update_cut = True else: update_cut = False else: update_cut = False if update_cut: q_text.remove() q_text = fig_diff.text(0.55, 0.25, "", size=10) print(f"starting_point = {starting_point}, endpoint = {endpoint}") cut = gu.linecut( diff_pattern, indices=list(zip(starting_point, endpoint)), interp_order=1, ) if qx.ndim == 1: d_q = np.sqrt( (qx[endpoint[0]] - qx[starting_point[0]]) ** 2 + (qz[endpoint[1]] - qz[starting_point[1]]) ** 2 + (qy[endpoint[2]] - qy[starting_point[2]]) ** 2 ) else: d_q = np.sqrt( ( qx[endpoint[0], endpoint[1], endpoint[2]] - qx[starting_point[0], starting_point[1], starting_point[2]] ) ** 2 + ( qz[endpoint[0], endpoint[1], endpoint[2]] - qz[starting_point[0], starting_point[1], starting_point[2]] ) ** 2 + ( qy[endpoint[0], endpoint[1], endpoint[2]] - qy[starting_point[0], starting_point[1], starting_point[2]] ) ** 2 ) distance = np.linspace(0, d_q, num=len(cut)) plt0.remove() plt1.remove() plt2.remove() (plt0,) = ax0.plot( [starting_point[2], endpoint[2]], [starting_point[1], endpoint[1]], "r-" ) # sum axis 0 (plt1,) = ax1.plot( [starting_point[2], endpoint[2]], [starting_point[0], endpoint[0]], "r-" ) # sum axis 1 (plt2,) = ax2.plot( [starting_point[1], endpoint[1]], [starting_point[0], endpoint[0]], "r-" ) # sum axis 2 plt0_start.remove() plt1_start.remove() plt2_start.remove() plt0_stop.remove() plt1_stop.remove() plt2_stop.remove() (plt0_start,) = ax0.plot( [starting_point[2]], [starting_point[1]], "bo" ) # sum axis 0 (plt0_stop,) = ax0.plot([endpoint[2]], [endpoint[1]], "ro") # sum axis 0 (plt1_start,) = ax1.plot( [starting_point[2]], [starting_point[0]], "bo" ) # sum axis 1 (plt1_stop,) = ax1.plot([endpoint[2]], [endpoint[0]], "ro") # sum axis 1 (plt2_start,) = ax2.plot( [starting_point[1]], [starting_point[0]], "bo" ) # sum axis 2 (plt2_stop,) = ax2.plot([endpoint[1]], [endpoint[0]], "ro") # sum axis 2 ax3.cla() ax3.plot(distance, np.log10(cut), "-or", markersize=3) if load_qvalues: ax3.set_xlabel("distance along the linecut (1/A)") else: ax3.set_xlabel("distance along the linecut (pixels)") ax3.set_ylabel("Int (A.U.)") ax3.axis("auto") ax3.set_ylim(bottom=-2) plt.tight_layout() plt.draw()
raise ValueError("q components should be 1D or 3D arrays") if load_qvalues: print(f"COM[qx, qz, qy] = {qxCOM:.3f} 1/A, {qzCOM:.3f} 1/A, {qyCOM:.3f} 1/A") else: print(f"COM[qx, qz, qy] = {qxCOM:.3f}, {qzCOM:.3f}, {qyCOM:.3f}") #################### # interactive plot # #################### plt.ioff() starting_point = starting_point or [nz // 2, 0, nx // 2] endpoint = endpoint or [nz // 2, ny - 1, nx // 2] cut = gu.linecut( diff_pattern, indices=list(zip(starting_point, endpoint)), interp_order=1, ) if qx.ndim == 1: dq = np.sqrt( (qx[endpoint[0]] - qx[starting_point[0]]) ** 2 + (qz[endpoint[1]] - qz[starting_point[1]]) ** 2 + (qy[endpoint[2]] - qy[starting_point[2]]) ** 2 ) else: dq = np.sqrt( ( qx[endpoint[0], endpoint[1], endpoint[2]] - qx[starting_point[0], starting_point[1], starting_point[2]] ) ** 2
def on_click(event): """ Function to interact with a plot, return the position of clicked pixel. If flag_pause==1 or if the mouse is out of plot axes, it will not register the click :param event: mouse click event """ global linecut_prtf, z0, y0, x0, endpoint, distances_q global fig_prtf, ax0, ax1, ax2, ax3, plt0, plt1, plt2, cut, res_text if event.inaxes == ax0: # hor=X, ver=Y endpoint[2], endpoint[1] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax1: # hor=X, ver=rocking curve endpoint[2], endpoint[0] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax2: # hor=Y, ver=rocking curve endpoint[1], endpoint[0] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax3: # print the resolution at the mouse position res_text.remove() res_text = fig_prtf.text( 0.55, 0.25, f"Resolution={2*np.pi/(10*event.xdata):.1f} nm", size=10) plt.draw() update_cut = False else: update_cut = False if update_cut: res_text.remove() res_text = fig_prtf.text(0.55, 0.25, "", size=10) print(f"endpoint = {endpoint}") cut = gu.linecut( linecut_prtf, indices=list(zip(starting_point, endpoint)), interp_order=1, ) plt0.remove() plt1.remove() plt2.remove() (plt0, ) = ax0.plot([x0, endpoint[2]], [y0, endpoint[1]], "ro-") # sum axis 0 (plt1, ) = ax1.plot([x0, endpoint[2]], [z0, endpoint[0]], "ro-") # sum axis 1 (plt2, ) = ax2.plot([y0, endpoint[1]], [z0, endpoint[0]], "ro-") # sum axis 2 ax3.cla() ax3.plot( np.linspace( distances_q[z0, y0, x0], distances_q[endpoint[0], endpoint[1], endpoint[2]], num=len(cut), ), cut, "-or", markersize=3, ) ax3.axhline(y=1 / np.e, linestyle="dashed", color="k", linewidth=1) ax3.set_xlabel("q (1/A)") ax3.set_ylabel("PRTF") ax3.axis("auto") plt.tight_layout() plt.draw()
scale="linear", vmin=0, reciprocal_space=True, ) np.savez_compressed(setup.detector.savedir + "linecut_prtf.npz", data=linecut_prtf) else: linecut_prtf = prtf_matrix plt.ioff() max_colorbar = 5 starting_point = [z0, y0, x0] endpoint = [0, 0, 0] cut = gu.linecut( prtf_matrix, indices=list(zip(starting_point, endpoint)), interp_order=1, ) diff_pattern[np.isnan(diff_pattern)] = 0 # discard nans fig_prtf, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2, figsize=(12, 6)) fig_prtf.canvas.mpl_disconnect( fig_prtf.canvas.manager.key_press_handler_id) ax0.imshow(np.log10(diff_pattern.sum(axis=0)), vmin=0, vmax=max_colorbar, cmap=my_cmap) ax1.imshow(np.log10(diff_pattern.sum(axis=1)), vmin=0, vmax=max_colorbar,
if load_qvalues: print( f'COM[qx, qz, qy] = {qxCOM:.3f} 1/A, {qzCOM:.3f} 1/A, {qyCOM:.3f} 1/A') else: print(f'COM[qx, qz, qy] = {qxCOM:.3f}, {qzCOM:.3f}, {qyCOM:.3f}') #################### # interactive plot # #################### plt.ioff() starting_point = starting_point or [nz // 2, 0, nx // 2] endpoint = endpoint or [nz // 2, ny - 1, nx // 2] cut = gu.linecut(diff_pattern, start_indices=starting_point, stop_indices=endpoint, interp_order=1, debugging=False) if qx.ndim == 1: dq = np.sqrt((qx[endpoint[0]] - qx[starting_point[0]])**2 + (qz[endpoint[1]] - qz[starting_point[1]])**2 + (qy[endpoint[2]] - qy[starting_point[2]])**2) else: dq = np.sqrt( (qx[endpoint[0], endpoint[1], endpoint[2]] - qx[starting_point[0], starting_point[1], starting_point[2]])**2 + (qz[endpoint[0], endpoint[1], endpoint[2]] - qz[starting_point[0], starting_point[1], starting_point[2]])**2 + (qy[endpoint[0], endpoint[1], endpoint[2]] - qy[starting_point[0], starting_point[1], starting_point[2]])**2) distance = np.linspace(0, dq, num=len(cut))
def on_click(event): """ Function to interact with a plot, return the position of clicked pixel. If flag_pause==1 or if the mouse is out of plot axes, it will not register the click :param event: mouse click event """ global linecut_prtf, z0, y0, x0, endpoint, distances_q global fig_prtf, ax0, ax1, ax2, ax3, plt0, plt1, plt2, cut, res_text if event.inaxes == ax0: # hor=X, ver=Y endpoint[2], endpoint[1] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax1: # hor=X, ver=rocking curve endpoint[2], endpoint[0] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax2: # hor=Y, ver=rocking curve endpoint[1], endpoint[0] = int(np.rint(event.xdata)), int( np.rint(event.ydata)) update_cut = True elif event.inaxes == ax3: # print the resolution at the mouse position res_text.remove() res_text = fig_prtf.text( 0.55, 0.25, f'Resolution={2*np.pi/(10*event.xdata):.1f} nm', size=10) plt.draw() update_cut = False else: update_cut = False if update_cut: res_text.remove() res_text = fig_prtf.text(0.55, 0.25, '', size=10) print(f'endpoint = {endpoint}') cut = gu.linecut(linecut_prtf, start_indices=(z0, y0, x0), stop_indices=endpoint, interp_order=1, debugging=False) plt0.remove() plt1.remove() plt2.remove() plt0, = ax0.plot([x0, endpoint[2]], [y0, endpoint[1]], 'ro-') # sum axis 0 plt1, = ax1.plot([x0, endpoint[2]], [z0, endpoint[0]], 'ro-') # sum axis 1 plt2, = ax2.plot([y0, endpoint[1]], [z0, endpoint[0]], 'ro-') # sum axis 2 ax3.cla() ax3.plot(np.linspace(distances_q[z0, y0, x0], distances_q[endpoint[0], endpoint[1], endpoint[2]], num=len(cut)), cut, '-or', markersize=3) ax3.axhline(y=1 / np.e, linestyle='dashed', color='k', linewidth=1) ax3.set_xlabel('q (1/A)') ax3.set_ylabel('PRTF') ax3.axis('auto') plt.tight_layout() plt.draw()
cmap=my_cmap, title='prtf_matrix after interpolation', scale='linear', vmin=0, reciprocal_space=True) np.savez_compressed(detector.savedir + 'linecut_prtf.npz', data=linecut_prtf) else: linecut_prtf = prtf_matrix plt.ioff() max_colorbar = 5 endpoint = [0, 0, 0] cut = gu.linecut(prtf_matrix, start_indices=(z0, y0, x0), stop_indices=endpoint, interp_order=1, debugging=False) diff_pattern[np.isnan(diff_pattern)] = 0 # discard nans fig_prtf, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2, figsize=(12, 6)) fig_prtf.canvas.mpl_disconnect( fig_prtf.canvas.manager.key_press_handler_id) ax0.imshow(np.log10(diff_pattern.sum(axis=0)), vmin=0, vmax=max_colorbar, cmap=my_cmap) ax1.imshow(np.log10(diff_pattern.sum(axis=1)), vmin=0, vmax=max_colorbar,
def test_2d_vertical(self): array = np.asarray([np.ones(6) * idx for idx in range(5)]) expected = np.arange(1, 5) output = gu.linecut(array, indices=[(1, 4), (2, 2)], interp_order=1) self.assertTrue(np.allclose(output, expected))
def test_2d_horizontal(self): array = np.asarray([np.arange(6) for _ in range(5)]) expected = np.arange(1, 5) output = gu.linecut(array, indices=[(1, 1), (1, 4)], interp_order=1) self.assertTrue(np.allclose(output, expected))
def test_1d(self): array = np.arange(5) expected = np.array([1.0, 2.0, 3.0]) output = gu.linecut(array, indices=[(1, 3)], interp_order=1) self.assertTrue(np.allclose(output, expected))
def test_interp_order_null(self): with self.assertRaises(ValueError): gu.linecut(np.ones(3), indices=[(1, 2)], interp_order=0)
def test_interp_order_wrong_type(self): with self.assertRaises(TypeError): gu.linecut(np.ones(3), indices=[(1, 2)], interp_order=1.2)