def set_data_test(self, data_shape='3D'): x = utils.linspace_step(0, 20, 1) y = utils.linspace_step(0, 30, 1) t = utils.linspace_step(0, 200, 1) z = utils.linspace_step(0, 200, 1) datas = np.zeros((len(y), len(x), len(t), len(z))) amp = utils.gauss2D(x, 7, 5, y, 12, 10) for indx in range(len(x)): for indy in range(len(y)): datas[indy, indx, :, :] = amp[indy, indx] * ( utils.gauss2D(z, 50 + indx * 2, 20, t, 50 + 3 * indy, 30) + np.random.rand(len(t), len(z)) / 10) nav_axis = dict(nav00=Axis(data=y, nav_index=0, label='y_axis', units='yunits'), nav01=Axis(data=x, nav_index=1, label='x_axis', units='xunits'), nav02=Axis(data=t, nav_index=2, label='t_axis', units='tunits'), nav03=Axis(data=z, nav_index=3, label='z_axis', units='zunits')) if data_shape == '4D': nav_axes = [2, 3] self.show_data(datas, temp_data=False, nav_axes=nav_axes, **nav_axis) elif data_shape == '3D': self.show_data(np.sum(datas, axis=3), temp_data=False, nav_axes=[0, 1], **nav_axis) elif data_shape == '2D': self.show_data(np.sum(datas, axis=(2, 3)), **nav_axis) elif data_shape == '1D': self.show_data(np.sum(datas, axis=(1, 2, 3)), **nav_axis)
def test_find_common_index(self): x = utils.linspace_step(0, 100, 1) y = utils.linspace_step(0, 1, 0.01) x0 = 28.4 y0 = 0.275 ind, x_val, y_val = utils.find_common_index(x, y, x0, y0) assert ind == 28 and x_val == x[28] and y_val == y[28]
def test_DataFromPlugins(self): data = [ utils.linspace_step(1, 100, 1), utils.linspace_step(0.01, 1, 0.01) ] nav_axes = ["test"] x_axis = utils.Axis(data=utils.linspace_step(1, 100, 1)) y_axis = utils.Axis(data=utils.linspace_step(1, 100, 1)) data_test = utils.DataFromPlugins(data=data, nav_axes=nav_axes, nav_x_axis=x_axis, nav_y_axis=y_axis) assert isinstance(data_test, utils.DataFromPlugins) assert data_test['data'] == data assert data_test['nav_axes'] == nav_axes assert data_test['nav_x_axis'] == x_axis assert data_test['nav_y_axis'] == y_axis assert data_test['dim'] == 'Data1D' data = [np.array([1])] data_test = utils.DataFromPlugins(data=data) assert data_test['dim'] == 'Data0D' data = [np.array([[1, 1], [1, 2]])] data_test = utils.DataFromPlugins(data=data) assert data_test['dim'] == 'Data2D' data = [np.array([[[1, 1], [1, 2]], [[2, 1], [2, 2]]])] data_test = utils.DataFromPlugins(data=data) assert data_test['dim'] == 'DataND' with pytest.raises(TypeError): utils.DataFromPlugins(data=[1, 2, 3, 4, 5]) with pytest.raises(TypeError): utils.DataFromPlugins(data="str")
def test_Data(self): name = 'data_test' x = utils.linspace_step(1, 100, 1) y = utils.linspace_step(0.01, 1, 0.01) data_test = utils.Data(name=name, x_axis=x, y_axis=y) assert isinstance(data_test, utils.Data) assert data_test['name'] == name assert data_test['x_axis'] == utils.Axis(data=x) assert data_test['y_axis'] == utils.Axis(data=y) x = utils.Axis(x) y = utils.Axis(y) kwargs = [1, 2.0, 'kwargs', True, None] data_test = utils.Data(name=name, x_axis=x, y_axis=y, kwargs=kwargs) assert data_test['x_axis'] == x assert data_test['y_axis'] == y assert data_test['kwargs'] == kwargs with pytest.raises(TypeError): utils.Data(name=None) with pytest.raises(TypeError): utils.Data(source=None) with pytest.raises(ValueError): utils.Data(source='source') with pytest.raises(TypeError): utils.Data(distribution=None) with pytest.raises(ValueError): utils.Data(distribution='distribution') with pytest.raises(TypeError): utils.Data(x_axis=10) with pytest.raises(TypeError): utils.Data(y_axis=10)
def update_steps_calculation(self): init = False values = np.array([]) for ind, data in enumerate(self._table_model.raw_data): if not init and self._table_model.is_checked(ind): values = utils.linspace_step(*data) init = True elif self._table_model.is_checked(ind): values = np.concatenate((values, utils.linspace_step(*data))) return values
def set_x_axis(self): Npts = self.settings.child('x_axis', 'Npts').value() x0 = self.settings.child('x_axis', 'x0').value() dx = self.settings.child('x_axis', 'dx').value() self.x_axis['data'] = linspace_step(x0 - (Npts - 1) * dx / 2, x0 + (Npts - 1) * dx / 2, dx) self.emit_x_axis()
def do_calibration(self): Naverage = self.settings.child('calibration', 'calibration_move', 'average').value() steps = linspace_step(self.settings.child('calibration', 'calibration_move', 'start').value(), self.settings.child('calibration', 'calibration_move', 'stop').value(), self.settings.child('calibration', 'calibration_move', 'step').value()) self.detector_data = np.zeros((steps.size,2)) self.detector_data_average = np.zeros((steps.size, 2)) for mod in self.pid_controller.actuator_modules: mod.move_done_signal.connect(self.move_done) for mod in self.pid_controller.detector_modules: mod.grab_done_signal.connect(self.det_done) self.viewer_calib.x_axis = steps for ind_average in range(Naverage): for ind_step, step in enumerate(steps): self.move_done_flag = False self.pid_controller.actuator_modules[0].move_Abs(step) self.wait_for_move_done() self.det_done_flag = False self.pid_controller.detector_modules[0].grab_data() self.wait_for_det_done() for ind_data, name in enumerate(self.data_names): self.detector_data[ind_step, ind_data] = \ self.pid_controller.detector_modules[0].data_to_save_export[name[1]][name[2]] if not self.running: break self.detector_data_average = (ind_average*self.detector_data_average+self.detector_data)/(ind_average+1) self.viewer_calib.show_data([self.detector_data_average[:,0], self.detector_data_average[:,1]]) self.lsqe.fit([self.detector_data_average[:,0], self.detector_data_average[:,1]]) center, width, height, theta = self.lsqe.parameters() ellipse_x, ellipse_y = self.get_ellipse_fit(center, width, height, theta) self.viewer_ellipse.plot_channels[0].setData(x=self.detector_data_average[:,0], y=self.detector_data_average[:,1]) self.viewer_ellipse.plot_channels[1].setData(x=ellipse_x, y=ellipse_y) self.settings.child('calibration', 'calibration_ellipse', 'x0').setValue(center[0]) self.settings.child('calibration', 'calibration_ellipse', 'y0').setValue(center[1]) self.settings.child('calibration', 'calibration_ellipse', 'dx').setValue(width) self.settings.child('calibration', 'calibration_ellipse', 'dy').setValue(height) self.settings.child('calibration', 'calibration_ellipse', 'theta').setValue(np.rad2deg(theta)) if not self.running: break QtWidgets.QApplication.processEvents() self.pid_controller.setpoint_sb.setValue(self.pid_controller.setpoint_sb.value()) self.settings.child('calibration', 'do_calibration').setValue(False) for mod in self.pid_controller.actuator_modules: mod.move_done_signal.disconnect(self.move_done) for mod in self.pid_controller.detector_modules: mod.grab_done_signal.disconnect(self.det_done)
def test_my_moment(self): x = utils.linspace_step(0, 100, 1) y = utils.gauss1D( x, 42.321, 13.5 ) # relation between dx in the gauss1D and in the moment is np.sqrt(4*np.log(2)) x0, dx = utils.my_moment(x, y) assert x0 == pytest.approx(42.321) assert dx * np.sqrt(4 * np.log(2)) == pytest.approx(13.5)
def update_positions(self): xini = self.settings.child('ini_pos').value() sat = self.settings.child('satu_pos').value() start = self.settings.child('start_scan').value() stop = self.settings.child('stop_scan').value() step = self.settings.child('step_scan').value() return np.concatenate( (np.array([xini, sat]), utils.linspace_step(start, stop, step), np.array([xini])))
def set_data_test(self,data_shape='3D'): x=utils.linspace_step(0,20,1) y=utils.linspace_step(0,30,1) t=utils.linspace_step(0,200,1) z=utils.linspace_step(0,200,1) datas=np.zeros((len(y),len(x),len(t),len(z))) amp=utils.gauss2D(x,7,5,y,12,10) for indx in range(len(x)): for indy in range(len(y)): datas[indy,indx,:,:]=amp[indy,indx]*(utils.gauss2D(z,50+indx*2,20,t,50+3*indy,30)+np.random.rand(len(t),len(z))/10) if data_shape=='4D': self.show_data(datas,temp_data=False,nav_axes=[2,3]) elif data_shape=='3D': self.show_data(np.sum(datas,axis=3),temp_data=False,nav_axes=[0,1]) elif data_shape=='2D': self.show_data(np.sum(datas,axis=(2,3))) elif data_shape=='1D': self.show_data(np.sum(datas,axis=(1,2,3)))
def test_gauss2D(self): x = utils.linspace_step(-1.0, 1, 0.1) x0 = -0.55 dx = 0.1 y = utils.linspace_step(-2.0, -1, 0.1) y0 = -1.55 dy = 0.2 n = 1 assert np.all( utils.gauss2D(x, x0, dx, y, y0, dy, n) == pytest.approx( np.transpose( np.outer(utils.gauss1D(x, x0, dx, n), utils.gauss1D(y, y0, dy, n))))) assert np.all( utils.gauss2D(x, x0, dx, y, y0, dy, n) == pytest.approx( utils.gauss2D(x, x0, dx, y, y0, dy, n, 180))) assert np.all( utils.gauss2D(x, x0, dx, y, y0, dy, n, -90) == pytest.approx( utils.gauss2D(x, x0, dx, y, y0, dy, n, 90))) assert np.all( utils.gauss2D(x, x0, dx, y, y0, dy, n) == pytest.approx( utils.gauss2D(x, x0, dy, y, y0, dx, n, 90)))
def test_gauss1D(self): x = utils.linspace_step(1.0, -1, -0.13) x0 = -0.55 dx = 0.1 n = 1 assert np.all( utils.gauss1D(x, x0, dx, n) == pytest.approx( np.exp(-2 * np.log(2)**(1 / n) * (((x - x0) / dx))**(2 * n)))) with pytest.raises(ValueError): utils.gauss1D(x, x0, -0., 1) with pytest.raises(TypeError): utils.gauss1D(x, x0, 0.1, 1.1) with pytest.raises(ValueError): utils.gauss1D(x, x0, 0.1, -1)
def test_linspace_step(self): assert np.all( utils.linspace_step(-1.0, 10, 1) == np.array( [-1., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])) assert np.all( utils.linspace_step(1.0, -1, -0.13) == pytest.approx( np.array([ 1., 0.87, 0.74, 0.61, 0.48, 0.35, 0.22, 0.09, -0.04, -0.17, -0.3, -0.43, -0.56, -0.69, -0.82, -0.95 ]))) with pytest.raises(ValueError): utils.linspace_step(45, 45, 1) with pytest.raises(ValueError): utils.linspace_step(0, 10, -1) with pytest.raises(ValueError): utils.linspace_step(0, 10, 0.)
def test_find_index(self): # get closest value and index x = utils.linspace_step(1.0, -1, -0.13) assert utils.find_index(x, -0.55) == [(12, -0.56)] assert utils.find_index(x, [-0.55, 0.741]) == [(12, -0.56), (2, 0.74)] assert utils.find_index(x, 10) == [(0, 1.)]
def set_scan(self): if self.settings.child('scan_options', 'scan_type').value() == "Scan1D": scan_parameters = utils.ScanParameters() start = self.settings.child('scan_options', 'scan1D_settings', 'start_1D').value() stop = self.settings.child('scan_options', 'scan1D_settings', 'stop_1D').value() step = self.settings.child('scan_options', 'scan1D_settings', 'step_1D').value() if self.settings.child('scan_options', 'scan1D_settings', 'scan1D_selection').value() == 'Manual': steps_x = utils.linspace_step(start, stop, step) steps_y = np.array([]) else: # from ROI viewer = self.scan_selector.scan_selector_source positions = self.scan_selector.scan_selector.getArrayIndexes(spacing=step) steps_x, steps_y = zip(*positions) steps_x, steps_y = viewer.scale_axis(np.array(steps_x), np.array(steps_y)) if self.settings.child('scan_options', 'scan1D_settings', 'scan1D_type').value() == "Linear": scan_parameters.axis_2D_1 = steps_x scan_parameters.axis_2D_2 = steps_y elif self.settings.child('scan_options', 'scan1D_settings', 'scan1D_type').value() == 'Linear back to start': stepss_x = [] stepss_y = [] for stepx in steps_x: stepss_x.extend([stepx, start]) for stepy in steps_y: stepss_y.extend([stepy, start]) scan_parameters.axis_2D_1 = np.array(stepss_x) scan_parameters.axis_2D_2 = np.array(stepss_y) elif self.settings.child('scan_options', 'scan1D_settings', 'scan1D_type').value() == 'Random': positions = list(zip(steps_x, steps_y)) np.random.shuffle(positions) x, y = zip(*positions) scan_parameters.axis_2D_1 = list(x) scan_parameters.axis_2D_2 = list(y) scan_parameters.positions = list( itertools.zip_longest(scan_parameters.axis_2D_1, scan_parameters.axis_2D_2, fillvalue=None)) scan_parameters.Nsteps = len(scan_parameters.positions) elif self.settings.child('scan_options', 'scan_type').value() == "Scan2D": start_axis1 = self.settings.child('scan_options', 'scan2D_settings', 'start_2d_axis1').value() start_axis2 = self.settings.child('scan_options', 'scan2D_settings', 'start_2d_axis2').value() if self.settings.child('scan_options', 'scan2D_settings', 'scan2D_type').value() == 'Spiral': Rstep_2d = self.settings.child('scan_options', 'scan2D_settings', 'Rstep_2d').value() Rmax = self.settings.child('scan_options', 'scan2D_settings', 'Rmax_2d').value() scan_parameters = utils.set_scan_spiral(start_axis1, start_axis2, Rmax, Rstep_2d) else: stop_axis1 = self.settings.child('scan_options', 'scan2D_settings', 'stop_2d_axis1').value() step_axis1 = self.settings.child('scan_options', 'scan2D_settings', 'step_2d_axis1').value() stop_axis2 = self.settings.child('scan_options', 'scan2D_settings', 'stop_2d_axis2').value() step_axis2 = self.settings.child('scan_options', 'scan2D_settings', 'step_2d_axis2').value() if self.settings.child('scan_options', 'scan2D_settings', 'scan2D_type').value() == 'back&forth': scan_parameters = utils.set_scan_linear(start_axis1, start_axis2, stop_axis1, stop_axis2, step_axis1, step_axis2, back_and_force=True) elif self.settings.child('scan_options', 'scan2D_settings', 'scan2D_type').value() == 'Linear': scan_parameters = utils.set_scan_linear( start_axis1, start_axis2, stop_axis1, stop_axis2, step_axis1, step_axis2, back_and_force=False) elif self.settings.child('scan_options', 'scan2D_settings', 'scan2D_type').value() == 'Random': scan_parameters = utils.set_scan_random(start_axis1, start_axis2, stop_axis1, stop_axis2, step_axis1, step_axis2) self.settings.child('scan_options', 'Nsteps').setValue(scan_parameters.Nsteps) self.scan_params_signal.emit(scan_parameters) return scan_parameters
def test_normalize(self): x = utils.linspace_step(0, 100, 1) ind = np.random.randint(1, 100 + 1) assert utils.normalize(x)[ind] == \ pytest.approx(utils.linspace_step(0, 1, 0.01)[ind])