From 7dd9b0716c5981ab6dae1674c6f2052c408b2a43 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Thu, 14 Nov 2024 17:50:26 +0000 Subject: [PATCH] delaunay plotter unitt est --- autoarray/config/visualize/mat_wrap_2d.yaml | 4 +-- autoarray/plot/mat_plot/two_d.py | 26 +++++++++---------- autoarray/plot/wrap/two_d/delaunay_drawer.py | 23 ++++++---------- .../plot/wrap/two_d/test_delaunay_drawer.py | 26 +++++++++++++++++++ 4 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 test_autoarray/plot/wrap/two_d/test_delaunay_drawer.py diff --git a/autoarray/config/visualize/mat_wrap_2d.yaml b/autoarray/config/visualize/mat_wrap_2d.yaml index 1e1cf8d8..3cd6f4b4 100644 --- a/autoarray/config/visualize/mat_wrap_2d.yaml +++ b/autoarray/config/visualize/mat_wrap_2d.yaml @@ -146,7 +146,7 @@ VectorYXQuiver: # wrapper for `plt.quiver()`: customize (y,x) vectors appea linewidth: 5 pivot: middle units: xy -DelaunayDrawer: # wrapper for `plt.fill()`: customize the appearance of Voronoi mesh's. +DelaunayDrawer: # wrapper for `plt.fill()`: customize the appearance of Delaunay mesh's. figure: alpha: 0.7 edgecolor: k @@ -154,7 +154,7 @@ DelaunayDrawer: # wrapper for `plt.fill()`: customize the appearance of Vo subplot: alpha: 0.7 edgecolor: k - linewidth: 0.0 + linewidth: 0.0 VoronoiDrawer: # wrapper for `plt.fill()`: customize the appearance of Voronoi mesh's. figure: alpha: 0.7 diff --git a/autoarray/plot/mat_plot/two_d.py b/autoarray/plot/mat_plot/two_d.py index 8d54bca3..965c5c76 100644 --- a/autoarray/plot/mat_plot/two_d.py +++ b/autoarray/plot/mat_plot/two_d.py @@ -48,7 +48,7 @@ def __init__( vector_yx_quiver: Optional[w2d.VectorYXQuiver] = None, patch_overlay: Optional[w2d.PatchOverlay] = None, interpolated_reconstruction: Optional[w2d.InterpolatedReconstruction] = None, - delaunay_drawer : Optional[w2d.DelaunayDrawer] = None, + delaunay_drawer: Optional[w2d.DelaunayDrawer] = None, voronoi_drawer: Optional[w2d.VoronoiDrawer] = None, origin_scatter: Optional[w2d.OriginScatter] = None, mask_scatter: Optional[w2d.MaskScatter] = None, @@ -662,21 +662,21 @@ def _plot_delaunay_mapper( interpolation_array = None if interpolate_to_uniform: - - interpolation_array = self.interpolated_reconstruction.imshow_reconstruction( - mapper=mapper, - pixel_values=pixel_values, - units=self.units, - cmap=self.cmap, - colorbar=self.colorbar, - colorbar_tickparams=self.colorbar_tickparams, - aspect=aspect_inv, - ax=ax, - use_log10=self.use_log10, + interpolation_array = ( + self.interpolated_reconstruction.imshow_reconstruction( + mapper=mapper, + pixel_values=pixel_values, + units=self.units, + cmap=self.cmap, + colorbar=self.colorbar, + colorbar_tickparams=self.colorbar_tickparams, + aspect=aspect_inv, + ax=ax, + use_log10=self.use_log10, + ) ) else: - self.delaunay_drawer.draw_delaunay_pixels( mapper=mapper, pixel_values=pixel_values, diff --git a/autoarray/plot/wrap/two_d/delaunay_drawer.py b/autoarray/plot/wrap/two_d/delaunay_drawer.py index d34e24b0..5d168213 100644 --- a/autoarray/plot/wrap/two_d/delaunay_drawer.py +++ b/autoarray/plot/wrap/two_d/delaunay_drawer.py @@ -9,10 +9,7 @@ from autoarray.plot.wrap import base as wb -def facecolors_from( - values, - simplices -): +def facecolors_from(values, simplices): facecolors = np.zeros(shape=simplices.shape[0]) for i in range(simplices.shape[0]): facecolors[i] = np.sum(1.0 / 3.0 * values[simplices[i, :]]) @@ -65,6 +62,11 @@ def draw_delaunay_pixels( If `True`, the colorbar is plotted using a log10 scale. """ + if pixel_values is None: + raise ValueError( + "pixel_values input to DelaunayPlotter are None and thus cannot be plotted." + ) + if ax is None: ax = plt.gca() @@ -72,10 +74,7 @@ def draw_delaunay_pixels( simplices = mapper.delaunay.simplices - facecolors = facecolors_from( - values=pixel_values, - simplices=simplices - ) + facecolors = facecolors_from(values=pixel_values, simplices=simplices) norm = cmap.norm_from(array=pixel_values, use_log10=use_log10) @@ -89,15 +88,9 @@ def draw_delaunay_pixels( color_values = np.where(pixel_values > vmax, vmax, pixel_values) color_values = np.where(pixel_values < vmin, vmin, color_values) - if vmax != vmin: - color_array = (color_values - vmin) / (vmax - vmin) - else: - color_array = np.ones(color_values.shape[0]) - cmap = plt.get_cmap(cmap.cmap) if colorbar is not None: - cb = colorbar.set_with_color_values( units=units, norm=norm, @@ -120,4 +113,4 @@ def draw_delaunay_pixels( vmin=vmin, vmax=vmax, **self.config_dict - ) \ No newline at end of file + ) diff --git a/test_autoarray/plot/wrap/two_d/test_delaunay_drawer.py b/test_autoarray/plot/wrap/two_d/test_delaunay_drawer.py new file mode 100644 index 00000000..f7f9e452 --- /dev/null +++ b/test_autoarray/plot/wrap/two_d/test_delaunay_drawer.py @@ -0,0 +1,26 @@ +import autoarray.plot as aplt + +import numpy as np + + +def test__draws_delaunay_pixels_for_sensible_input(delaunay_mapper_9_3x3): + delaunay_drawer = aplt.DelaunayDrawer(linewidth=0.5, edgecolor="r", alpha=1.0) + + delaunay_drawer.draw_delaunay_pixels( + mapper=delaunay_mapper_9_3x3, + pixel_values=np.ones(9), + units=aplt.Units(), + cmap=aplt.Cmap(), + colorbar=None, + ) + + values = np.ones(9) + values[0] = 0.0 + + delaunay_drawer.draw_delaunay_pixels( + mapper=delaunay_mapper_9_3x3, + pixel_values=values, + units=aplt.Units(), + cmap=aplt.Cmap(), + colorbar=aplt.Colorbar(fraction=0.1, pad=0.05), + )