def test_get_correction_affine_and_rotation(self, calgen): affine = np.array( [ [0.97579077, 0.01549655, 0.0], [0.01549655, 0.99008051, 0.0], [0.0, 0.0, 1.0], ] ) calgen.affine_matrix = affine real_line = Line2DROI( x1=2.69824, y1=81.4867, x2=229.155, y2=61.6898, linewidth=3 ) recip_line = Line2DROI( x1=-0.30367, y1=-1.21457, x2=0.344978, y2=1.24927, linewidth=0.115582 ) value = calgen.get_rotation_calibration( real_line=real_line, reciprocal_line=recip_line ) corr = calgen.get_correction_matrix() np.testing.assert_almost_equal( corr, np.array( [ [0.1805777, 0.9783954, 0.0], [-0.9590618, 0.1524534, 0.0], [0.0, 0.0, 1.0], ] ), decimal=3 )
def test_get_rotation_calibration(self, calgen): real_line = Line2DROI( x1=2.69824, y1=81.4867, x2=229.155, y2=61.6898, linewidth=3 ) recip_line = Line2DROI( x1=-0.30367, y1=-1.21457, x2=0.344978, y2=1.24927, linewidth=0.115582 ) value = calgen.get_rotation_calibration( real_line=real_line, reciprocal_line=recip_line ) np.testing.assert_almost_equal(value, -80.24669411537899,decimal=3)
def test_line2droi_angle(self): # 1. Testing quantitative measurement for different quadrants: r = self.r r_angles = np.array([rr.angle() for rr in r]) angles_h = np.array([ -135., -150., 150., 135., -120., -135., 135., 120., -60., -45., 45., 60., -45., -30, 30., 45. ]) angles_v = np.array([ -135., -120., -60., -45., -150., -135., -45., -30., 150., 135., 45., 30., 135., 120., 60., 45. ]) assert np.allclose(r_angles, angles_h) r_angles = np.array([rr.angle(axis='vertical') for rr in r]) assert np.allclose(r_angles, angles_v) # 2. Testing unit conversation r = Line2DROI(np.random.rand(), np.random.rand(), np.random.rand(), np.random.rand()) assert r.angle(units='degrees') == (r.angle(units='radians') / np.pi * 180.) # 3. Testing raises: with pytest.raises(ValueError): r.angle(units='meters') with pytest.raises(ValueError): r.angle(axis='z')
def test_get_navigation_calibration_no_data(self, empty_calgen): line = Line2DROI(x1=2.5, y1=13.0, x2=193.0, y2=12.5, linewidth=3.5) empty_calgen.get_navigation_calibration(line_roi=line, x1=12.0, x2=172.0, n=1, xspace=500.0)
def test_get_navigation_calibration_no_data(self, empty_calgen): line = Line2DROI(x1=2.5, y1=13.0, x2=193.0, y2=12.5, linewidth=3.5) with pytest.raises(ValueError, match="requires an Au X-grating image"): empty_calgen.get_navigation_calibration( line_roi=line, x1=12.0, x2=172.0, n=1, xspace=500.0 )
def test_get_navigation_calibration(self, calgen): line = Line2DROI(x1=2.5, y1=13.0, x2=193.0, y2=12.5, linewidth=3.5) value = calgen.get_navigation_calibration(line_roi=line, x1=12.0, x2=172.0, n=1, xspace=500.0) np.testing.assert_almost_equal(calgen.navigation_calibration, value)
def test_plot_calibrated_data_im(self, calgen): line = Line2DROI(x1=2.5, y1=13.0, x2=193.0, y2=12.5, linewidth=3.5) calgen.get_navigation_calibration(line_roi=line, x1=12.0, x2=172.0, n=1, xspace=500.0) calgen.plot_calibrated_data(data_to_plot="au_x_grating_im")
def test_repr_vals(self): repr(Point1DROI(1.1)) repr(Point2DROI(1.1, 2.1)) repr(Line2DROI(0, 0, 1, 1, 0.1)) repr(RectangularROI(0, 0, 1, 1)) repr(SpanROI(3., 5.)) repr(CircleROI(5, 5, 3)) repr(CircleROI(5, 5, 3, 1))
def accept(self): if self.is_on(): if self.ndim == 1: roi = SpanROI(0, 1) elif self.ndim > 1: roi = Line2DROI(0, 0, 1, 1, 1) roi._on_widget_change(self.widget) # ROI gets coords from widget self.accepted[BaseInteractiveROI].emit(roi) self.accepted[BaseInteractiveROI, SignalFigureTool].emit(roi, self)
def get_diffraction_calibration(self, mask_length, linewidth): """Determine the diffraction pattern pixel size calibration in units of reciprocal Angsstroms per pixel. Parameters ---------- mask_length : float Halfwidth of the region excluded from peak finding around the diffraction pattern center. linewidth : float Width of Line2DROI used to obtain line trace from distortion corrected diffraction pattern. Returns ------- diff_cal : float Diffraction calibration in reciprocal Angstroms per pixel. """ # Check that necessary calibration data is provided if self.calibration_data.au_x_grating_dp is None: raise ValueError("This method requires an Au X-grating diffraction " "pattern to be provided. Please update the " "CalibrationDataLibrary.") if self.affine_matrix is None: raise ValueError("This method requires a distortion matrix to have " "been determined. Use get_elliptical_distortion " "to determine this matrix.") dpeg = self.calibration_data.au_x_grating_dp size = dpeg.data.shape[0] dpegs = stack_method([dpeg, dpeg, dpeg, dpeg]) dpegs = ElectronDiffraction2D(dpegs.data.reshape((2, 2, size, size))) dpegs.apply_affine_transformation(self.affine_matrix, preserve_range=True, inplace=True) dpegm = dpegs.mean((0, 1)) # Define line roi along which to take trace for calibration line = Line2DROI(x1=5, y1=5, x2=250, y2=250, linewidth=linewidth) # Obtain line trace trace = line(dpegm) trace = trace.as_signal1D(0) # Find peaks in line trace either side of direct beam db = (np.sqrt(2) * 128) - (5 * np.sqrt(2)) pka = trace.isig[db + mask_length:].find_peaks1D_ohaver()[0]['position'] pkb = trace.isig[:db - mask_length].find_peaks1D_ohaver()[0]['position'] # Determine predicted position of 022 peak of Au pattern d022=1.437 au_pre = db - (self.ring_params[0] / 1.437) au_post = db + (self.ring_params[0] / 1.437) # Calculate differences between predicted and measured positions prediff = np.abs(pkb - au_pre) postdiff = np.abs(pka - au_post) # Calculate new calibration value based on most accurate peak positions dc = (2 / 1.437) / (pka[postdiff == min(postdiff)] - pkb[prediff == min(prediff)]) # Store diffraction calibration value as attribute self.diffraction_calibration = dc[0] return dc[0]
def setup_method(self, method): np.random.seed(0) # Same random every time, Line2DROi test requires it self.s_s = Signal1D(np.random.rand(50, 60, 4)) self.s_s.axes_manager[0].scale = 5 self.s_s.axes_manager[0].units = 'nm' self.s_s.axes_manager[1].scale = 5 self.s_s.axes_manager[1].units = 'nm' # 4D dataset self.s_i = Signal2D(np.random.rand(100, 100, 4, 4)) # Generate ROI for test of angle measurements self.r = [] t = np.tan(30. / 180. * np.pi) for x in [-1., -t, t, 1]: for y in [-1., -t, t, 1]: self.r.append(Line2DROI(x1=0., x2=x, y1=0., y2=y))
def test_interactive_snap(self, snap): kwargs = {} if snap != 'default': kwargs['snap'] = snap else: # default is True snap = True s = self.s r = RectangularROI(left=3, right=7, top=2, bottom=5) s.plot() _ = r.interactive(s, **kwargs) for w in r.widgets: old_position = w.position new_position = (3.25, 2.2) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap p1 = Point1DROI(4) _ = p1.interactive(s, **kwargs) for w in p1.widgets: old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_position == snap p2 = Point2DROI(4, 5) _ = p2.interactive(s, **kwargs) for w in p2.widgets: old_position = w.position new_position = (4.3, 5.3) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_position == snap span = SpanROI(4.01, 5) _ = span.interactive(s, **kwargs) for w in span.widgets: old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap # check that changing snap is working fine new_snap = not snap w.snap_all = new_snap old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if new_snap else new_position line2d = Line2DROI(4, 5, 6, 6, 1) _ = line2d.interactive(s, **kwargs) for w in line2d.widgets: old_position = w.position new_position = ([4.3, 5.3], [6.0, 6.0]) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap
def test_line2droi_length(self): line = Line2DROI(x1=0., x2=2, y1=0., y2=2) np.testing.assert_allclose(line.length, np.sqrt(8))
def test_2d_line_getitem(self): r = Line2DROI(10, 10, 150, 50, 5) assert tuple(r) == (10, 10, 150, 50, 5)
def test_2d_line_spec_plot(self): r = Line2DROI(10, 10, 150, 50, 5) s = self.s_s s2 = r(s) np.testing.assert_allclose( s2.data, np.array([[0.96779467, 0.5468849, 0.27482357, 0.59223042], [0.89676116, 0.40673335, 0.55207828, 0.27165277], [0.27734027, 0.52437981, 0.11738029, 0.15984529], [0.04680635, 0.97073144, 0.00386035, 0.17857997], [0.61286675, 0.0813696, 0.8818965, 0.71962016], [0.96638997, 0.50763555, 0.30040368, 0.54950057], [0.22956744, 0.50686296, 0.73685316, 0.09767637], [0.5149222, 0.93841202, 0.22864655, 0.67714114], [0.5149222, 0.93841202, 0.22864655, 0.67714114], [0.59288027, 0.0100637, 0.4758262, 0.70877039], [0.80546244, 0.58610794, 0.56928692, 0.51208072], [0.97176308, 0.36384478, 0.78791575, 0.55529411], [0.39563367, 0.95546593, 0.59831597, 0.11891694], [0.4175392, 0.78158173, 0.69374702, 0.91634033], [0.44679332, 0.83699037, 0.22182403, 0.49394526], [0.92961874, 0.66721471, 0.79807902, 0.55099397], [0.98046646, 0.58866215, 0.04551071, 0.1979828], [0.70340703, 0.35307496, 0.15442542, 0.31268984], [0.88432423, 0.95853234, 0.20751273, 0.78846839], [0.27334874, 0.88713154, 0.16554561, 0.66595992], [0.08421126, 0.97389332, 0.70063334, 0.84181574], [0.15946909, 0.41702974, 0.42681952, 0.26810926], [0.13159685, 0.03921054, 0.02523183, 0.27155029], [0.13159685, 0.03921054, 0.02523183, 0.27155029], [0.46185344, 0.72624328, 0.4748717, 0.90405082], [0.52917427, 0.54280647, 0.71405379, 0.51655594], [0.13307599, 0.77345467, 0.4062725, 0.96309389], [0.28351378, 0.26307878, 0.3335074, 0.57231702], [0.89486974, 0.17628164, 0.2796788, 0.58167984], [0.64937273, 0.5006921, 0.28355772, 0.2861476], [0.31342052, 0.19085, 0.90192363, 0.85839813]]), rtol=0.05) r.linewidth = 50 s3 = r(s) np.testing.assert_allclose( s3.data, np.array([[0.40999384, 0.27111487, 0.3345655, 0.47553854], [0.44475117, 0.40330205, 0.48113292, 0.26780132], [0.57911599, 0.38999298, 0.38509116, 0.37418655], [0.29175157, 0.37856367, 0.34420691, 0.48316543], [0.55975912, 0.57155145, 0.57640677, 0.39718605], [0.41300845, 0.45929259, 0.27489573, 0.40120352], [0.46271229, 0.60908378, 0.25796662, 0.46526239], [0.37843991, 0.54919334, 0.40469436, 0.48612034], [0.44717148, 0.44934708, 0.29064827, 0.51334849], [0.3966089, 0.59853786, 0.50392157, 0.39123649], [0.50281456, 0.62863149, 0.43051921, 0.32015553], [0.40527468, 0.44258442, 0.55694228, 0.41142292], [0.47856163, 0.49720026, 0.62012372, 0.47537808], [0.46695064, 0.5159018, 0.53532036, 0.4691573], [0.44267241, 0.46886762, 0.37363574, 0.54369291], [0.76138395, 0.54406653, 0.47305104, 0.45083095], [0.74812744, 0.53414434, 0.38487816, 0.44611049], [0.59011489, 0.5456799, 0.41782293, 0.5948403], [0.47546595, 0.52536805, 0.39267032, 0.58787463], [0.39387115, 0.4784124, 0.36765754, 0.46951847], [0.54076839, 0.69257203, 0.44540576, 0.39236971], [0.41195904, 0.5148879, 0.51199686, 0.63694563], [0.44885787, 0.46886977, 0.42150512, 0.52556669], [0.60826081, 0.3987657, 0.55875628, 0.5293137], [0.44151911, 0.4188617, 0.37734811, 0.51166705], [0.52878209, 0.41050467, 0.57149806, 0.52577575], [0.50474464, 0.3294767, 0.63519013, 0.56126315], [0.37607782, 0.58086952, 0.45089019, 0.62929377], [0.59956085, 0.5173887, 0.64790597, 0.49865165], [0.57646846, 0.46468029, 0.45267259, 0.44889072], [0.4382186, 0.49576157, 0.6192481, 0.45031413]]))
def test_plot_au_x_grating_dp_with_roi(self, library): line = Line2DROI(x1=1, y1=1, x2=3, y2=3, linewidth=1.0) library.plot_calibration_data(data_to_plot="au_x_grating_dp", roi=line)
def test_2d_line_img_plot(self): s = self.s_i r = Line2DROI(0, 0, 4, 4, 1) s2 = r(s) nt.assert_true( np.allclose( s2.data, np.array([[[0.5646904, 0.83974605, 0.37688365, 0.499676], [0.08130241, 0.3241552, 0.91565131, 0.85345237], [0.5941565, 0.90536555, 0.42692772, 0.93761072], [0.9458708, 0.56996783, 0.05020319, 0.88466194]], [[0.55342858, 0.71776076, 0.9698018, 0.84684608], [0.77676046, 0.32998726, 0.49284904, 0.63849364], [0.94969472, 0.99393561, 0.79184028, 0.60493951], [0.99584095, 0.83632682, 0.51592399, 0.53049253]], [[0.55342858, 0.71776076, 0.9698018, 0.84684608], [0.77676046, 0.32998726, 0.49284904, 0.63849364], [0.94969472, 0.99393561, 0.79184028, 0.60493951], [0.99584095, 0.83632682, 0.51592399, 0.53049253]], [[0.32270396, 0.28878038, 0.64165074, 0.92820531], [0.24836647, 0.37477366, 0.18406007, 0.11019336], [0.38678734, 0.9174347, 0.47658793, 0.45095935], [0.95232706, 0.96468026, 0.5158903, 0.69112322]], [[0.72414297, 0.64417135, 0.17938658, 0.12279276], [0.90632348, 0.90345183, 0.21473533, 0.34087282], [0.2579504, 0.65663038, 0.27606922, 0.33695786], [0.46466925, 0.34991125, 0.73593611, 0.32203574]], [[0.72414297, 0.64417135, 0.17938658, 0.12279276], [0.90632348, 0.90345183, 0.21473533, 0.34087282], [0.2579504, 0.65663038, 0.27606922, 0.33695786], [0.46466925, 0.34991125, 0.73593611, 0.32203574]], [[0.97259866, 0.13527587, 0.48531393, 0.31607768], [0.13656701, 0.40578067, 0.64221493, 0.46036815], [0.30466093, 0.88706533, 0.30914269, 0.01833664], [0.56143007, 0.09026307, 0.81898535, 0.4518825]]]))) r.linewidth = 10 s3 = r(s) nt.assert_true( np.allclose( s3.data, np.array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0.12385935, 0.17534623, 0.08266437, 0.08533342], [0.06072978, 0.18213069, 0.13162582, 0.14526251], [0.11950599, 0.09530544, 0.05814531, 0.10613925], [0.13243216, 0.13388253, 0.15641767, 0.07678893]], [[0.10387718, 0.18591981, 0.21704829, 0.16594489], [0.26554947, 0.27280648, 0.23534874, 0.15751378], [0.11329239, 0.16440693, 0.19378236, 0.23418843], [0.20414672, 0.24669051, 0.08809065, 0.21252996]], [[0.32737802, 0.24354627, 0.25713232, 0.42447693], [0.22132115, 0.34440789, 0.1769873, 0.18348862], [0.32205928, 0.29038094, 0.22570116, 0.20305065], [0.45399669, 0.29687212, 0.313637, 0.27469796]], [[0.38104394, 0.2654458, 0.51666151, 0.47973295], [0.34333797, 0.36907303, 0.34349318, 0.25681538], [0.32849871, 0.27963978, 0.47319042, 0.37358476], [0.48767599, 0.23022751, 0.32004745, 0.37714935]], [[0.59093609, 0.54976286, 0.54934114, 0.54753303], [0.48284716, 0.35797562, 0.49739056, 0.46934957], [0.29954848, 0.45448276, 0.50639968, 0.56140708], [0.55790493, 0.55105139, 0.40859302, 0.47408336]], [[0.63293155, 0.38872956, 0.55044015, 0.37731745], [0.49091568, 0.54173188, 0.51292652, 0.53813843], [0.56463766, 0.73848284, 0.41183566, 0.37515417], [0.48426503, 0.23582684, 0.45947953, 0.49322732]]])))
def test_2d_line_spec_plot(self): r = Line2DROI(10, 10, 150, 50, 5) s = self.s_s s2 = r(s) np.testing.assert_almost_equal( s2.data, np.array([[0.96779467, 0.5468849, 0.27482357, 0.59223042], [0.75928245, 0.36454463, 0.50106317, 0.37638916], [0.93916091, 0.50631222, 0.99980858, 0.19725947], [0.8075638, 0.05100731, 0.62716071, 0.50245307], [0.88118825, 0.91641901, 0.2715511, 0.60754536], [0.8996517, 0.11646325, 0.16318171, 0.6962192], [0.01497626, 0.4573887, 0.64439714, 0.06037948], [0.72362309, 0.28890677, 0.97364152, 0.85953663], [0.72362309, 0.28890677, 0.97364152, 0.85953663], [0.24783865, 0.1928807, 0.21518258, 0.33911841], [0.50438632, 0.55765081, 0.31787285, 0.614175], [0.04143613, 0.40084015, 0.79034035, 0.64846836], [0.08067386, 0.79973013, 0.16217087, 0.19457452], [0.54601389, 0.8121854, 0.81069635, 0.71423997], [0.83030369, 0.1979076, 0.10475427, 0.05690645], [0.30969465, 0.60847027, 0.76278635, 0.54082552], [0.61242728, 0.11588176, 0.60404942, 0.83680764], [0.57727334, 0.36670814, 0.04252509, 0.43254571], [0.89750871, 0.44035511, 0.6426097, 0.38146028], [0.32126274, 0.6520685, 0.47213129, 0.05101303], [0.30668186, 0.51515877, 0.69551083, 0.28720246], [0.95568239, 0.12785748, 0.77676417, 0.31861386], [0.80813283, 0.93128198, 0.28999035, 0.39246662], [0.80813283, 0.93128198, 0.28999035, 0.39246662], [0.3329224, 0.95850972, 0.8414333, 0.28555722], [0.84350086, 0.93051062, 0.82974137, 0.52569971], [0.97002278, 0.82518232, 0.92972355, 0.52315373], [0.23035238, 0.43235565, 0.04142909, 0.04025669], [0.54124516, 0.89630549, 0.5923778, 0.25663082], [0.60156, 0.51353417, 0.78213733, 0.8870782], [0.08783705, 0.59293514, 0.39825928, 0.55032904]])) r.linewidth = 50 s3 = r(s) np.testing.assert_almost_equal( s3.data, np.array([[0.44265776, 0.35745439, 0.27446296, 0.41595865], [0.26916342, 0.37715306, 0.32971154, 0.33530845], [0.35426084, 0.47282874, 0.44090125, 0.35714432], [0.4460463, 0.44976548, 0.40195731, 0.53014602], [0.5426024, 0.53311424, 0.54084761, 0.3422711], [0.45614115, 0.43188745, 0.25545383, 0.44532886], [0.46822001, 0.62948798, 0.3153703, 0.43942564], [0.47173014, 0.67098345, 0.25334107, 0.37146341], [0.51756393, 0.37361861, 0.43797954, 0.4143497], [0.42342347, 0.54374975, 0.63824925, 0.46078933], [0.59427019, 0.49465564, 0.50841493, 0.64876998], [0.55886454, 0.48120671, 0.53824089, 0.59540268], [0.57945739, 0.64865535, 0.44827414, 0.50337253], [0.53121262, 0.47216267, 0.55630321, 0.4016824], [0.43845159, 0.47476225, 0.40063072, 0.35417257], [0.45185363, 0.42844148, 0.5125228, 0.47386537], [0.45610424, 0.56858719, 0.60593161, 0.47611862], [0.47861857, 0.57419121, 0.51392978, 0.59486733], [0.47641588, 0.54820991, 0.52233701, 0.57604036], [0.34994652, 0.4514264, 0.57328452, 0.57024109], [0.34159438, 0.59016962, 0.56304279, 0.4982093], [0.55053388, 0.65012605, 0.55998794, 0.47428968], [0.51392427, 0.56827423, 0.74809453, 0.60156568], [0.64152041, 0.56110823, 0.57792954, 0.49621454], [0.53466093, 0.55640288, 0.42431698, 0.50720362], [0.59924828, 0.39606699, 0.56290688, 0.59880921], [0.43478479, 0.46415729, 0.41994764, 0.5724431], [0.26896758, 0.41500659, 0.47386699, 0.56557835], [0.62045775, 0.49978237, 0.62594581, 0.48231989], [0.3378933, 0.62744914, 0.61643326, 0.57795806], [0.4226275, 0.53682311, 0.58369682, 0.43471438]]))