From 2acfd6ad1d6816dd2b763d1274c064d4c5d1399b Mon Sep 17 00:00:00 2001 From: Jenny Fothergill Date: Fri, 8 Apr 2022 14:19:31 -0600 Subject: [PATCH 1/5] Allow users to specify a colormap when using plot --- gixstapose/diffractometer.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/gixstapose/diffractometer.py b/gixstapose/diffractometer.py index 071f23d..81f1f9e 100644 --- a/gixstapose/diffractometer.py +++ b/gixstapose/diffractometer.py @@ -296,7 +296,7 @@ def diffract(self, rot, cutout=True): self.dp = dp return dp - def plot(self): + def plot(self, cmap=None): """Plot the diffraction pattern. The plot will have units in inverse Angstrom calculated from the @@ -304,6 +304,12 @@ def plot(self): This function will also rotate the diffraction pattern according to the `up` attribute of the camera if `diffract_from_camera` was used. + Parameters + ---------- + cmap : str, default None + Name of matplotlib colormap. If None is given, the default colormap + for matplotlib.pyplot.imshow will be used. + Returns ------- matplotlib.figure.Figure, matplotlib.axes._subplots.AxesSubplot @@ -333,7 +339,7 @@ def plot(self): cval=np.log10(self.bot), order=1, ) - ax.imshow(dp, extent=[-extent, extent, -extent, extent]) + ax.imshow(dp, cmap=cmap, extent=[-extent, extent, -extent, extent]) ax.set_xlabel(r"$q_{xy} (1/\AA)$", fontsize=20) ax.set_ylabel(r"$q_{z} (1/\AA)$", fontsize=20) ticks = ticks = [ From b6ac6b2cac1641cc68088d26d812a3f340053111 Mon Sep 17 00:00:00 2001 From: Jenny Fothergill Date: Fri, 8 Apr 2022 14:29:49 -0600 Subject: [PATCH 2/5] Add factor of 2 pi --- gixstapose/diffractometer.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gixstapose/diffractometer.py b/gixstapose/diffractometer.py index 81f1f9e..bb5896f 100644 --- a/gixstapose/diffractometer.py +++ b/gixstapose/diffractometer.py @@ -327,8 +327,10 @@ def plot(self, cmap=None): """ ) fig, ax = plt.subplots(figsize=(8, 8)) - extent = (self.N / 2 / self.zoom + 1) / ( - np.max(self.box) * self.length_scale + extent = ( + (self.N / self.zoom + 1) + * np.pi + / (np.max(self.box) * self.length_scale) ) dp = self.dp if self.up_ang is not None: From 18e68cb5210f5234a1a29160ca43ba40af8bc873 Mon Sep 17 00:00:00 2001 From: Jenny Fothergill Date: Fri, 8 Apr 2022 15:12:13 -0600 Subject: [PATCH 3/5] Add crop kwarg to allow finer zooming without zoom argument --- gixstapose/diffractometer.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/gixstapose/diffractometer.py b/gixstapose/diffractometer.py index bb5896f..300a302 100644 --- a/gixstapose/diffractometer.py +++ b/gixstapose/diffractometer.py @@ -296,7 +296,7 @@ def diffract(self, rot, cutout=True): self.dp = dp return dp - def plot(self, cmap=None): + def plot(self, cmap=None, crop=None): """Plot the diffraction pattern. The plot will have units in inverse Angstrom calculated from the @@ -309,6 +309,9 @@ def plot(self, cmap=None): cmap : str, default None Name of matplotlib colormap. If None is given, the default colormap for matplotlib.pyplot.imshow will be used. + crop : float, default None + For small systems where zoom does not give enough precision, crop + can be used to zoom the plot to (-crop, crop) in Angstroms. Returns ------- @@ -333,9 +336,22 @@ def plot(self, cmap=None): / (np.max(self.box) * self.length_scale) ) dp = self.dp + if crop is not None: + pts = np.linspace(-extent, extent, self.N) + left_idx = np.searchsorted(pts, -crop) + right_idx = np.searchsorted(pts, crop) + new_dp = dp[left_idx:right_idx, left_idx:right_idx] + idbig = self.circle_cutout(new_dp) + new_dp[np.unravel_index(idbig, new_dp.shape)] = np.log10(self.bot) + dp = new_dp + extent = ( + (new_dp.shape[0] / self.zoom + 1) + * np.pi + / (np.max(self.box) * self.length_scale) + ) if self.up_ang is not None: dp = rotate( - self.dp, + dp, self.up_ang, reshape=False, cval=np.log10(self.bot), From ece7383a8aee0553917710724929e6b530e5c11e Mon Sep 17 00:00:00 2001 From: Jenny Fothergill Date: Fri, 8 Apr 2022 15:34:26 -0600 Subject: [PATCH 4/5] Allow user to set tick spacing --- gixstapose/diffractometer.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gixstapose/diffractometer.py b/gixstapose/diffractometer.py index 300a302..dde2376 100644 --- a/gixstapose/diffractometer.py +++ b/gixstapose/diffractometer.py @@ -296,7 +296,7 @@ def diffract(self, rot, cutout=True): self.dp = dp return dp - def plot(self, cmap=None, crop=None): + def plot(self, cmap=None, crop=None, tickspacing=0.5): """Plot the diffraction pattern. The plot will have units in inverse Angstrom calculated from the @@ -311,7 +311,9 @@ def plot(self, cmap=None, crop=None): for matplotlib.pyplot.imshow will be used. crop : float, default None For small systems where zoom does not give enough precision, crop - can be used to zoom the plot to (-crop, crop) in Angstroms. + can be used to zoom the plot to (-crop, crop) in 1/Angstroms. + tickspacing : float, default 0.5 + Spacing between x and x tick values in 1/Angstroms. Returns ------- @@ -360,12 +362,10 @@ def plot(self, cmap=None, crop=None): ax.imshow(dp, cmap=cmap, extent=[-extent, extent, -extent, extent]) ax.set_xlabel(r"$q_{xy} (1/\AA)$", fontsize=20) ax.set_ylabel(r"$q_{z} (1/\AA)$", fontsize=20) - ticks = ticks = [ - -round(extent, 2), - -round(extent / 2, 2), - 0, - round(extent / 2, 2), - round(extent, 2), + maxtick = extent - (extent % tickspacing) + ticks = [ + round(i, 1) + for i in np.arange(-maxtick, maxtick + tickspacing / 2, tickspacing) ] ax.set_xticks(ticks) ax.set_yticks(ticks) From 016f90deaad0cff2efe347dd6e73ebc1154dd302 Mon Sep 17 00:00:00 2001 From: Jenny Fothergill Date: Fri, 8 Apr 2022 15:53:09 -0600 Subject: [PATCH 5/5] Scale test values by 2 pi this is probably not a good test... --- gixstapose/tests/test_diffract.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gixstapose/tests/test_diffract.py b/gixstapose/tests/test_diffract.py index c4ea997..1d45731 100644 --- a/gixstapose/tests/test_diffract.py +++ b/gixstapose/tests/test_diffract.py @@ -27,7 +27,7 @@ def test_diffract_plot_camera(self, positions_and_box, camera100): fig, ax = d.plot() assert isinstance(ax, plt.Axes) assert isinstance(fig, plt.Figure) - assert (-65, 65) == ax.get_xlim() + assert all(np.isclose(ax.get_xlim(), (-405, 405), atol=1)) def test_diffract_plot_rot(self, positions_and_box, rot100): d = Diffractometer(length_scale=2.0) @@ -36,7 +36,7 @@ def test_diffract_plot_rot(self, positions_and_box, rot100): fig, ax = d.plot() assert isinstance(ax, plt.Axes) assert isinstance(fig, plt.Figure) - assert (-32.5, 32.5) == ax.get_xlim() + assert all(np.isclose(ax.get_xlim(), (-202, 202), atol=1)) def test_diffract_plot_raises(self, positions_and_box): with pytest.raises(ValueError):