def main(): app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() prog = Viewer1D(Form) from pymodaq.daq_utils.daq_utils import gauss1D x = np.linspace(0, 200, 201) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) tau_half = 27 tau2 = 100 x0 = 50 dx = 20 ydata_expodec = np.zeros((len(x))) ydata_expodec[:50] = 1 * gauss1D(x[:50], x0, dx, 2) ydata_expodec[50:] = 1 * np.exp( -(x[50:] - x0) / (tau_half / np.log(2))) # +1*np.exp(-(x[50:]-x0)/tau2) ydata_expodec += 0.1 * np.random.rand(len(x)) # x = np.sin(np.linspace(0,6*np.pi,201)) # y = np.sin(np.linspace(0, 6*np.pi, 201)+np.pi/2) Form.show() prog.ui.Do_math_pb.click() QtWidgets.QApplication.processEvents() prog.x_axis = x # prog.show_data([y, y+2]) prog.show_data([y1, y2, ydata_expodec]) QtWidgets.QApplication.processEvents() prog.update_labels(['coucou', 'label2']) sys.exit(app.exec_())
def test_update_channels(self, init_prog): prog, qtbot = init_prog x = np.linspace(0, 200, 2) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) for ind, data in enumerate(y1): prog.show_data([[data], [y2[ind]]]) QtWidgets.QApplication.processEvents() assert prog.plot_channels prog.update_channels() assert prog.plot_channels is None
def set_Mock_data(self): """ For each parameter of the settings tree : * compute linspace numpy distribution with local parameters values * shift right the current data of ind_data position * add computed results to the data_mock list Returns ------- list The computed data_mock list. """ ind = -1 self.data_mock=[] data = np.zeros(self.x_axis['data'].shape) for param in self.settings.children():# if 'Mock' in param.name(): ind+=1 data_tmp=param.child(('Amp')).value()*gauss1D(self.x_axis['data'],param.child(('x0')).value(),param.child(('dx')).value(),param.child(('n')).value()) if ind == 0: data_tmp = data_tmp * np.sin(self.x_axis['data'] / 4) ** 2 data_tmp+=param.child(('amp_noise')).value()*np.random.rand((len(self.x_axis['data']))) data_tmp=self.settings.child(('exposure_ms')).value()/1000*np.roll(data_tmp,self.ind_data*self.settings.child(('rolling')).value()) if self.settings.child(('multi')).value(): self.data_mock.append(data_tmp) else: data+=data_tmp if not self.settings.child(('multi')).value(): self.data_mock.append(data) self.ind_data+=1 return self.data_mock
def test_update_labels(self, init_prog): prog, qtbot = init_prog x = np.linspace(0, 200, 2) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) for ind, data in enumerate(y1): prog.show_data([[data], [y2[ind]]]) QtWidgets.QApplication.processEvents() assert len(prog.plot_channels) == 2 labels = ['axis_1', 'axis_2'] prog.labels = labels for item, label in zip(prog.legend.items, labels): assert item[1].text == label
def set_1D_Mock_data(self): self.data_mock x = np.linspace(0, 99, 100) data_tmp = 10 * gauss1D(x, 50, 10, 1) + 1 * np.random.rand((100)) self.ind_data += 1 self.data_mock = np.roll(data_tmp, self.ind_data) self.data_mock = self.data_mock.astype( self.settings.child(('data_type')).value())
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 test_clear_data(self, init_prog): prog, qtbot = init_prog x = np.linspace(0, 200, 201) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) for ind, data in enumerate(y1): prog.show_data([[data], [y2[ind]]]) QtWidgets.QApplication.processEvents() for data in prog.datas: assert len(data) > 0 assert len(prog.x_axis) > 0 prog.clear_data() for data in prog.datas: assert data.size == 0 assert prog.x_axis.size == 0
def test_clear_pb(self, init_prog): prog, qtbot = init_prog x = np.linspace(0, 200, 201) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) for ind, data in enumerate(y1): prog.show_data([[data], [y2[ind]]]) QtWidgets.QApplication.processEvents() for data in prog.datas: assert data.size != 0 assert prog.x_axis.size != 0 qtbot.mouseClick(prog.ui.clear_pb, QtCore.Qt.LeftButton) for data in prog.datas: assert data.size == 0 assert prog.x_axis.size == 0
def test_ift(self): omega_max = utils.l2w(300) omega0 = utils.l2w(800) Npts = 2**10 omega_grid, time_grid = utils.ftAxis(Npts, omega_max) signal_temp = np.sin(omega0 * time_grid) * utils.gauss1D( time_grid, 0, 100, 1) signal_omega = utils.ft(signal_temp) assert np.all( signal_temp == pytest.approx(np.real(utils.ift(signal_omega)))) with pytest.raises(Exception): utils.ift(signal_temp, 2)
def set_Mock_data(self): """ For each parameter of the settings tree compute linspace numpy distribution with local parameters values and add computed results to the data_mock list. """ self.data_mock=[] for param in self.settings.children(): # the first one is ROIselect only valid in the 2D case if 'Mock' in param.name(): x = np.linspace(0,param.children()[0].value()-1, param.children()[0].value()) self.data_mock.append(param.children()[1].value()*gauss1D(x, param.children()[2].value(), param.children()[3].value(), param.children()[4].value()) +param.children()[5].value()*np.random.rand((param.children()[0].value())))
def test_ft(self): omega_max = utils.l2w(300) omega0 = utils.l2w(800) Npts = 2**10 omega_grid, time_grid = utils.ftAxis(Npts, omega_max) signal_temp = np.sin(omega0 * time_grid) * utils.gauss1D( time_grid, 0, 100, 1) signal_omega = utils.ft(signal_temp) assert np.abs(omega_grid[np.argmax( np.abs(signal_omega))]) == pytest.approx(omega0, rel=1e-2) with pytest.raises(Exception): utils.ft(signal_temp, 2)
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 update_filter(self): try: xmin, xmax = self.viewer1D.ROIfft.getRegion() self.filter = gauss1D(self.xaxisft, np.mean([xmin, xmax]), xmax - xmin) self.data = np.real(ift(self.filter * self.data_fft)) index = np.argmax(self.filter * self.data_fft) self.frequency = self.xaxisft[index] self.phase = np.angle(self.data_fft[index]) self.filter_changed.emit(dict(frequency=self.frequency, phase=self.phase)) self.update_plot() except Exception as e: logger.exception(str(e))
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 main_unsorted(): app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QWidget() prog = Viewer1D(widget) from pymodaq.daq_utils.daq_utils import gauss1D x = np.linspace(0, 200, 201) xaxis = np.concatenate((x, x[::-1])) y = gauss1D(x, 75, 25) yaxis = np.concatenate((y, -y)) widget.show() prog.show_data([yaxis], x_axis=xaxis) sys.exit(app.exec_())
def main_nans(): app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QWidget() prog = Viewer1D(widget) from pymodaq.daq_utils.daq_utils import gauss1D x = np.linspace(0, 200, 201) y = gauss1D(x, 75, 25) y[100:150] = np.nan widget.show() prog.show_data([y], x_axis=x) sys.exit(app.exec_())
def set_Mock_data(self): """ For each parameter of the settings tree : * compute linspace numpy distribution with local parameters values * shift right the current data of ind_data position * add computed results to the data_mock list Returns ------- list The computed data_mock list. """ param = self.settings.child('Mock1') #the first one is ROIselect only valid in the 2D case self.signal_axis=np.linspace(0,param.children()[0].value()-1,param.children()[0].value()) data_tmp=param.children()[1].value()*gauss1D(self.signal_axis,param.children()[2].value(),param.children()[3].value(),param.children()[4].value()) data_tmp = data_tmp * np.sin(self.signal_axis / 4) ** 2 data_tmp+=param.children()[5].value()*np.random.rand((param.children()[0].value())) data_tmp=np.roll(data_tmp,self.ind_data*self.settings.child(('rolling')).value()) self.data_mock = data_tmp self.ind_data+=1 return self.data_mock
if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) prog = FourierFilterer() from pymodaq.daq_utils.daq_utils import gauss1D xdata = np.linspace(0, 400, 401) x0 = 50 dx = 20 tau = 27 tau2 = 100 ydata_gauss = 10 * gauss1D(xdata, x0, dx) + np.random.rand(len(xdata)) ydata_expodec = np.zeros((len(xdata))) ydata_expodec[:50] = 10 * gauss1D(xdata[:50], x0, dx, 2) ydata_expodec[50:] = 10 * np.exp( -(xdata[50:] - x0) / tau) # +10*np.exp(-(xdata[50:]-x0)/tau2) ydata_expodec += 2 * np.random.rand(len(xdata)) ydata_sin = 10 + 2 * np.sin(2 * np.pi * 0.1 * xdata - np.deg2rad( 55)) + np.sin(2 * np.pi * 0.008 * xdata - np.deg2rad(-10)) + 2 * np.random.rand(len(xdata)) prog.show_data(dict(data=ydata_sin, xaxis=xdata)) sys.exit(app.exec_()) class LSqEllipse: def fit(self, data):
sub_data - offset, 0.37 * N0)[0][0]] - x0 data_lo.append(time) return data_lo except: return [] if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() prog = Viewer1D(Form) from pymodaq.daq_utils.daq_utils import gauss1D x = np.linspace(0, 200, 201) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) tau_half = 27 tau2 = 100 x0 = 50 dx = 20 ydata_expodec = np.zeros((len(x))) ydata_expodec[:50] = 1 * gauss1D(x[:50], x0, dx, 2) ydata_expodec[50:] = 1 * np.exp( -(x[50:] - x0) / (tau_half / np.log(2))) #+1*np.exp(-(x[50:]-x0)/tau2) ydata_expodec += 0.1 * np.random.rand(len(x)) # x = np.sin(np.linspace(0,6*np.pi,201)) # y = np.sin(np.linspace(0, 6*np.pi, 201)+np.pi/2) Form.show()
def random_hypergaussians1D(x, coeff=1): signal = 0. for ind in range(Nstruct): signal += amp[ind] * utils.gauss1D(x, x0s[ind], coeff * dx[ind], 2) return signal
def set_1D_Mock_data(self): self.data_mock x = np.linspace(0, 99, 100) data_tmp = 10 * gauss1D(x, 50, 10, 1) + 1 * np.random.rand((100)) self.ind_data += 1 self.data_mock = np.roll(data_tmp, self.ind_data)
def set_Mock_data(self): """ | Set the x_axis and y_axis with a linspace distribution from settings parameters. | Once done, set the data mock with parameters : * **Amp** : The amplitude * **x0** : the origin of x * **dx** : the derivative x pos * **y0** : the origin of y * **dy** : the derivative y pos * **n** : ??? * **amp_noise** : the noise amplitude Returns ------- The computed data mock. """ image = np.zeros( (self.settings.child('spatial_settings', 'Ny').value(), self.settings.child('spatial_settings', 'Nx').value(), self.settings.child('temp_settings', 'Nt').value())) self.time_axis = np.linspace( 0, self.settings.child('temp_settings', 'Nt').value(), self.settings.child('temp_settings', 'Nt').value(), endpoint=False) if self.settings.child('ROIselect', 'use_ROI').value(): self.x_axis = np.linspace( self.settings.child('ROIselect', 'x0').value(), self.settings.child('ROIselect', 'x0').value() + self.settings.child('ROIselect', 'width').value(), self.settings.child('ROIselect', 'width').value(), endpoint=False) self.y_axis = np.linspace( self.settings.child('ROIselect', 'y0').value(), self.settings.child('ROIselect', 'y0').value() + self.settings.child('ROIselect', 'height').value(), self.settings.child('ROIselect', 'height').value(), endpoint=False) data_mock = self.settings.child( 'spatial_settings', 'amp').value() * (utils.gauss2D( self.x_axis, self.settings.child('spatial_settings', 'x0').value(), self.settings.child('spatial_settings', 'dx').value(), self.y_axis, self.settings.child('spatial_settings', 'y0').value(), self.settings.child('spatial_settings', 'dy').value(), self.settings.child('spatial_settings', 'n').value() )) + self.settings.child( ('amp_noise')).value() * np.random.rand( len(self.y_axis), len(self.x_axis)) for indy in range(data_mock.shape[0]): data_mock[indy, :] = data_mock[indy, :] * np.sin( self.x_axis / self.settings.child('spatial_settings', 'lambda').value())**2 data_mock = np.roll(data_mock, self.ind_data * self.settings.child('rolling').value(), axis=1) try: self.image[ self.settings.child('ROIselect', 'y0').value(): self.settings.child('ROIselect', 'y0').value() + self.settings.child('ROIselect', 'height').value(), self.settings.child('ROIselect', 'x0').value(): self.settings.child('ROIselect', 'x0').value() + self.settings.child('ROIselect', 'width').value()] \ = data_mock except Exception as e: self.emit_status( ThreadCommand('Update_Status', [getLineInfo() + str(e), 'log'])) else: self.x_axis = np.linspace( 0, self.settings.child('spatial_settings', 'Nx').value(), self.settings.child('spatial_settings', 'Nx').value(), endpoint=False) self.y_axis = np.linspace( 0, self.settings.child('spatial_settings', 'Ny').value(), self.settings.child('spatial_settings', 'Ny').value(), endpoint=False) data_mock = self.settings.child('spatial_settings', 'amp').value() * ( utils.gauss2D(self.x_axis, self.settings.child('spatial_settings', 'x0').value(), self.settings.child('spatial_settings', 'dx').value(), self.y_axis, self.settings.child('spatial_settings', 'y0').value(), self.settings.child('spatial_settings', 'dy').value(), self.settings.child('spatial_settings', 'n').value())) + \ self.settings.child(('amp_noise')).value() * \ np.random.rand(len(self.y_axis), len(self.x_axis)) for indy in range(data_mock.shape[0]): data_mock[indy, :] = data_mock[indy, :] * np.sin( self.x_axis / self.settings.child('spatial_settings', 'lambda').value())**2 ind = 0 for indy in range(data_mock.shape[0]): for indx in range(data_mock.shape[1]): image[indy, indx, :] = data_mock[indy, indx] * \ utils.gauss1D(self.time_axis, self.settings.child('temp_settings', 't0').value(), self.settings.child('temp_settings', 'dt').value(), self.settings.child('temp_settings', 'n').value()) * \ np.sin(np.roll(self.time_axis, ind) / 4) ** 2 ind += 1 image = np.roll(image, self.ind_data * self.settings.child( ('rolling')).value(), axis=1) self.image = image self.ind_data += 1 QThread.msleep(100) return self.image
def update_x_axis(self, Nhistory): self.Nsamples = Nhistory self.x_axis = np.linspace(0, self.Nsamples - 1, self.Nsamples) @property def labels(self): return self._labels @labels.setter def labels(self, labels): self._labels = labels self.update_labels(labels) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() prog = Viewer0D(Form) from pymodaq.daq_utils.daq_utils import gauss1D x = np.linspace(0, 200, 201) y1 = gauss1D(x, 75, 25) y2 = gauss1D(x, 120, 50, 2) Form.show() for ind, data in enumerate(y1): prog.show_data([[data], [y2[ind]]]) QtWidgets.QApplication.processEvents() sys.exit(app.exec_())