From 2e89af5996442dfd3ddecc716168b37638874b4e Mon Sep 17 00:00:00 2001
From: David Hammer <david.hammer@xfel.eu>
Date: Wed, 23 Jun 2021 09:48:33 +0200
Subject: [PATCH] Dropping legacy_colorbar to avoid warning, fixing label color
 bug, making bad pixel plot binary

---
 src/cal_tools/plotting.py | 63 +++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 22 deletions(-)

diff --git a/src/cal_tools/plotting.py b/src/cal_tools/plotting.py
index 8c43424ef..d2434ffaf 100644
--- a/src/cal_tools/plotting.py
+++ b/src/cal_tools/plotting.py
@@ -14,8 +14,12 @@ from matplotlib import colors
 from matplotlib.patches import Patch
 from mpl_toolkits.axes_grid1 import AxesGrid
 
+plt.rcParams["mpl_toolkits.legacy_colorbar"] = False
 
-def show_overview(d, cell_to_preview, gain_to_preview, out_folder=None, infix=None):
+
+def show_overview(
+        d, cell_to_preview, gain_to_preview, out_folder=None, infix=None
+):
     """
     Show an overview
     :param d: A dict with the number of modules and
@@ -40,8 +44,9 @@ def show_overview(d, cell_to_preview, gain_to_preview, out_folder=None, infix=No
                         cbar_size="7%",
                         cbar_pad="2%",
                         )
-        i = 0
-        for key, item in data.items():
+
+        items = list(data.items())
+        for ax, cbar_ax, (key, item) in zip(grid, grid.cbar_axes, items):
             cf = 0
             if "ThresholdsDark" in key:
                 cf = -1
@@ -60,11 +65,14 @@ def show_overview(d, cell_to_preview, gain_to_preview, out_folder=None, infix=No
                    item[..., cell_to_preview, gain_to_preview + cf].size > 0.01):  # noqa
                 bound *= 2
 
-            if "BadPixelsDark" in key:
-                im = grid[i].imshow(np.log2(item[..., cell_to_preview,
-                                                 gain_to_preview + cf]),
-                                    interpolation="nearest", vmin=0, vmax=8,
-                                    aspect='auto')
+            is_badpixels = "BadPixels" in key
+
+            if is_badpixels:
+                im = ax.imshow(
+                    item[..., cell_to_preview, gain_to_preview + cf] != 0,
+                    cmap=plt.cm.colors.ListedColormap(["w", "k"]),
+                    aspect="auto",
+                )
             else:
 
                 if len(item.shape) == 4:
@@ -78,27 +86,33 @@ def show_overview(d, cell_to_preview, gain_to_preview, out_folder=None, infix=No
                         im_prev = np.moveaxis(item[..., cell_to_preview], 0, 1)
                     vmax = med + np.abs(bound * medscale)
 
-                im = grid[i].imshow(im_prev, interpolation="nearest",
-                                    vmin=med - np.abs(bound * medscale),
-                                    vmax=vmax, aspect='auto')
+                im = ax.imshow(im_prev, interpolation="nearest",
+                               vmin=med - np.abs(bound * medscale),
+                               vmax=vmax, aspect='auto')
+
+            cb = cbar_ax.colorbar(im)
+            if is_badpixels:
+                cb.set_ticks([0.25, 0.75])
+                cb.set_ticklabels(["good", "bad"])
+            else:
+                cb.set_label("ADU")
 
-            cb = grid.cbar_axes[i].colorbar(im)
-            cb.set_label_text("ADU" if key != "BadPixels" else "Bad Pixel Code")
+            ax.text(
+                5, 20, key, color="k" if is_badpixels else "w", fontsize=20
+            )
 
-            grid[i].text(5, 20, key, color="w" if key != "BadPixels" else "k", fontsize=20)  # noqa
+        grid[0].text(5, 50, module, color="k" if "BadPixels" in items[0][0] else "r", fontsize=20)  # noqa
 
-            i += 1
-        grid[0].text(5, 50, module, color="r" if key != "BadPixels" else "k", fontsize=20)  # noqa
         if out_folder and infix:
             fig.savefig(f"{out_folder}/"
                         f"dark_analysis_{infix}_module_{module}.png")
 
 
 def rebin(a, *args):
-    '''rebin ndarray data into a smaller ndarray of the same rank whose dimensions
-    are factors of the original dimensions. eg. An array with 6 columns and 4 rows
-    can be reduced to have 6,3,2 or 1 columns and 4,2 or 1 rows.
-    example usages:
+    '''rebin ndarray data into a smaller ndarray of the same rank whose
+    dimensions are factors of the original dimensions. eg. An array with 6
+    columns and 4 rows can be reduced to have 6,3,2 or 1 columns and 4,2 or 1
+    rows. example usages:
     https://scipy-cookbook.readthedocs.io/items/Rebinning.html
     >>> a=rand(6,4); b=rebin(a,3,2)
     >>> a=rand(6); b=rebin(a,2)
@@ -116,7 +130,12 @@ def rebin(a, *args):
 
 def plot_badpix_3d(data, definitions, title=None, rebin_fac=2, azim=22.5):
     od = data
-    d, dims = rebin(od.astype(np.uint32), od.shape[0] // rebin_fac, od.shape[1] // rebin_fac, od.shape[2])
+    d, dims = rebin(
+        od.astype(np.uint32),
+        od.shape[0] // rebin_fac,
+        od.shape[1] // rebin_fac,
+        od.shape[2],
+    )
     xx, yy, zz = dims
     voxels = d.astype(np.bool)
     colors = np.full(voxels.shape, '#FFFFFF')
@@ -137,7 +156,7 @@ def plot_badpix_3d(data, definitions, title=None, rebin_fac=2, azim=22.5):
     ax.set_zlim(0, np.max(zz))
 
     for k, c in cols.items():
-        ax.plot([-1,], [-1,], color=c[1], label=c[0])
+        ax.plot([-1, ], [-1, ], color=c[1], label=c[0])
     ax.legend()
     if title:
         ax.set_title(title)
-- 
GitLab