def test_normalize_btn_release(mock_show): x = np.linspace(0, 6, 10000) y = np.cos(x) obj = DraggableEnvelope(x, y, mode="u") assert obj._ind is None y_transform = obj.get_data() np.testing.assert_allclose(y_transform[100:9900], np.ones(9800), atol=1, rtol=1)
def test_normalize_keypress_cb(mock_show): x = np.linspace(0, 6, 1000) y = np.cos(x) obj = DraggableEnvelope(x, y, mode="u") mck = mock_event(xdata=50, ydata=50, button="d", key="d", fig=obj.fig, canvas=obj.fig.canvas, inaxes=True) obj.key_press_callback(event=mck)
def test_normalize_motion_notify_cb(mock_show): x = np.linspace(0, 6, 1000) y = np.cos(x) obj = DraggableEnvelope(x, y, mode="l") mck = mock_event(xdata=5, ydata=0.5, button="i", key="i", fig=obj.fig, canvas=obj.fig.canvas, inaxes=obj.ax) obj.motion_notify_callback(event=mck) mock_show.assert_called()
def test_normalize_keypress_cb4(mock_show): x = np.linspace(0, 6, 1000) y = np.cos(x) obj = DraggableEnvelope(x, y, mode="l") mck = mock_event(xdata=2, ydata=0.5, button="i", key="i", fig=obj.fig, canvas=obj.fig.canvas, inaxes=True) obj.key_press_callback(event=mck) assert 2 in obj.x_env assert 0.5 in obj.y_env mock_show.assert_called()
def test_normalize_k_press_cb2(mock_show): x = np.linspace(0, 6, 1000) y = np.cos(x) obj = DraggableEnvelope(x, y, mode="l") obj.epsilon = 1000000 obj.x_env = np.array([56, 5]) obj.y_env = np.array([60, 0.5]) xy_pixels = obj.ax.transData.transform([5, 0.5]) xpix, ypix = xy_pixels mck = mock_event(xdata=xpix, ydata=ypix, button="d", key="d", fig=obj.fig, canvas=obj.fig.canvas, inaxes=obj.ax) obj.key_press_callback(event=mck) assert 5 not in obj.x_env assert 0.5 not in obj.y_env mock_show.assert_called()
def normalize(self, filename=None, smoothing_level=0): """ Normalize the interferogram by finding upper and lower envelope on an interactive matplotlib editor. Points can be deleted with key `d` and inserted with key `i`. Also points can be dragged using the mouse. On complete just close the window. Must be called with interactive backend. The best practice is to call this function inside `~pysprint.interactive` context manager. Parameters ---------- filename : str, optional Save the normalized interferogram named by filename in the working directory. If not given it will not be saved. Default None. smoothing_level : int, optional The smoothing level used on the dataset before finding the envelopes. It applies Savitzky-Golay filter under the hood. Default is 0. """ x, y, _, _ = self._safe_cast() if smoothing_level != 0: x, y = savgol(x, y, [], [], window=smoothing_level) _l_env = DraggableEnvelope(x, y, "l") y_transform = _l_env.get_data() _u_env = DraggableEnvelope(x, y_transform, "u") y_final = _u_env.get_data() self.y = y_final self.y_norm = y_final self._is_normalized = True self.plt.title("Final") self.plot() self.show() if filename: if not filename.endswith(".txt"): filename += ".txt" np.savetxt(filename, np.column_stack((self.x, self.y)), delimiter=",") print(f"Successfully saved as {filename}.") return self
def test_normalize_keypress_cb3(mock_show): x = np.linspace(0, 6, 1000) y = np.cos(x) with pytest.raises(ValueError): DraggableEnvelope(x, y, mode="invalid")