From 19d6fc3b9bd31a9b4cae53177a8e261cdbf20161 Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Tue, 21 Jan 2020 13:28:05 +0100
Subject: [PATCH] Plot JF with 16 mem cells

---
 cal_tools/cal_tools/ana_tools.py          | 12 +++--
 notebooks/generic/PlotFromCalDB_NBC.ipynb | 63 +++++++++++++----------
 reportservice/report_conf.yaml            | 28 ++++++++--
 3 files changed, 68 insertions(+), 35 deletions(-)

diff --git a/cal_tools/cal_tools/ana_tools.py b/cal_tools/cal_tools/ana_tools.py
index a01475006..907c1682d 100644
--- a/cal_tools/cal_tools/ana_tools.py
+++ b/cal_tools/cal_tools/ana_tools.py
@@ -374,7 +374,7 @@ def get_range(data, scale, threshold = -1000):
     return med - scale * mad, med + scale * mad
 
 
-def hm_combine(data, fname=None, htype=None, **kwargs):
+def hm_combine(data, mem_cells=32, fname=None, htype=None, **kwargs):
     """
     Plot heatmap for calibration report
 
@@ -385,6 +385,7 @@ def hm_combine(data, fname=None, htype=None, **kwargs):
     HMType.INSET_AXIS insets axis for memory cells
 
     :param data: 2D numpy.array of data points to plot
+    :param mem_cells: number of memory cells shown in data
     :param fname: file name of output file
     :param htype: type of figure
     :param kwargs: other keywords supported by pyDetLib heatmapPlot
@@ -490,9 +491,12 @@ def hm_combine(data, fname=None, htype=None, **kwargs):
                                 use_axis=ax,
                                 y_log=False)
     if htype == HMType.INSET_AXIS:
-
-        ax2 = plt.axes([pad_l, pad_b, w_frame, h_frame / 16.], frame_on=False,
-                       yticks=range(3), yticklabels=[32, 16, 0],
+        # Group y axis by pixels and memory cells
+        # Axis inset shows memory cells
+        nypix = data.shape[0] / mem_cells
+        ax2 = plt.axes([pad_l, pad_b, w_frame, h_frame / nypix],
+                       frame_on=False, yticks=range(2),
+                       yticklabels=[mem_cells, 0],
                        xticks=[], xticklabels=[]
                        )
         ax2.yaxis.set_label_position("right")
diff --git a/notebooks/generic/PlotFromCalDB_NBC.ipynb b/notebooks/generic/PlotFromCalDB_NBC.ipynb
index 3e3ba7791..8240ddf2f 100644
--- a/notebooks/generic/PlotFromCalDB_NBC.ipynb
+++ b/notebooks/generic/PlotFromCalDB_NBC.ipynb
@@ -22,36 +22,36 @@
    "outputs": [],
    "source": [
     "cluster_profile = \"noDB\" # The ipcluster profile to use\n",
-    "start_date = \"2019-06-30\" # date to start investigation interval from\n",
+    "start_date = \"2019-01-01\" # date to start investigation interval from\n",
     "end_date = \"NOW\" # date to end investigation interval at, can be \"now\"\n",
-    "dclass=\"LPD\" # Detector class\n",
-    "modules = [\"LPD1M1\"] # detector entry in the DB to investigate\n",
+    "dclass=\"jungfrau\" # Detector class\n",
+    "modules = [\"Jungfrau_M035\"] # detector entry in the DB to investigate\n",
     "submodules = [2] # module index of a modular detector (1 for Q1M1 of AGIPD), range allowed \n",
-    "constants = ['Noise'] # constants to plot\n",
-    "nconstants = 7 # Number of time stamps to plot. If not 0, overcome start_date.\n",
+    "constants = ['RelativeGain'] # constants to plot\n",
+    "nconstants = 20 # Number of time stamps to plot. If not 0, overcome start_date.\n",
     "max_time = 15 # max time margin in minutes to match bad pixels\n",
-    "nMemToShow = 32 # Number of memory cells to be shown in plots\n",
+    "nMemToShow = 16 # Number of memory cells to be shown in plots\n",
     "\n",
-    "gain_setting = [0] # gain stages\n",
-    "bias_voltage = [250, 500] # Bias voltage\n",
+    "gain_setting = [0,1,2] # gain stages\n",
+    "bias_voltage = [90, 180] # Bias voltage\n",
     "temperature = [291] # Operation temperature\n",
-    "integration_time = [250, 50] # Integration time\n",
+    "integration_time = [4.96, 10, 50, 250] # Integration time\n",
     "pixels_x=[1024] # number of pixels along X axis\n",
-    "pixels_y=[512, 1024] # number of pixels along Y axis\n",
+    "pixels_y=[512] # number of pixels along Y axis\n",
     "in_vacuum = [0] # 0 if detector is operated in room pressure\n",
-    "memory_cells = [1, 512] # number of memory cells\n",
+    "memory_cells = [1, 16] # number of memory cells\n",
     "acquisition_rate = [1.1] # aquisition rate\n",
-    "parameter_names = ['bias_voltage', 'memory_cells'] # names of parameters\n",
+    "parameter_names = ['bias_voltage', 'integration_time', 'pixels_x', 'pixels_y', 'temperature', 'memory_cells'] # names of parameters\n",
     "\n",
-    "separate_plot = ['gain_setting'] # Plot on separate plots\n",
+    "separate_plot = ['gain_setting', 'memory_cells', 'integration_time'] # Plot on separate plots\n",
     "x_labels = ['Sensor Temperature', 'Integration Time'] # parameters to be shown on X axis: Acquisition rate, Memory cells, Sensor Temperature, Integration Time\n",
     "photon_energy = 9.2 # Photon energy of the beam\n",
-    "out_folder = \"/gpfs/exfel/data/scratch/karnem/test_bla3/\" # output folder\n",
+    "out_folder = \"/gpfs/exfel/data/scratch/karnem/test_bla4/\" # output folder\n",
     "use_existing = \"\" # If not empty, constants stored in given folder will be used\n",
     "cal_db_interface = \"tcp://max-exfl016:8016\" # the database interface to use\n",
     "cal_db_timeout = 180000 # timeout on caldb requests\",\n",
     "plot_range = 3 # range for plotting in units of median absolute deviations\n",
-    "spShape = [64, 64] # Shape of superpixel\n",
+    "spShape = [256, 64] # Shape of superpixel\n",
     "sp_name = 'ASIC IDs' # name of superpixel\n",
     "gain_titles = ['High gain', 'Medium gain', 'Low gain'] # Title inset related to gain"
    ]
@@ -96,7 +96,7 @@
     "    submodules = ['']\n",
     "\n",
     "# 0 is considered as None.\n",
-    "acquisition_rate = [x if x>0 else None for x in acquisition_rate]\n",
+    "acquisition_rate = [x if x > 0 else None for x in acquisition_rate]\n",
     "\n",
     "nMem = max(memory_cells) # Number of mem Cells to store\n",
     "\n",
@@ -106,7 +106,7 @@
     "if separate_plot == ['']:\n",
     "    separate_plot = []\n",
     "\n",
-    "# Mapping between consatnts and their bad pixel maps \n",
+    "# Mapping between consatnts and their bad pixel maps\n",
     "constantsDark = {\"SlopesFF\": 'BadPixelsFF',\n",
     "                 'SlopesPC': 'BadPixelsPC',\n",
     "                 'SlopesCI': 'BadPixelsCI',\n",
@@ -134,7 +134,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "parameter_list = combine_lists(*parameters, names = parameter_names)\n",
+    "parameter_list = combine_lists(*parameters, names=parameter_names)\n",
     "print(parameter_list)"
    ]
   },
@@ -172,7 +172,7 @@
     "        # Loop over parameters\n",
     "        for pars in parameter_list:\n",
     "\n",
-    "            if (const in [\"Offset\", \"Noise\", \"SlopesPC\", \"SlopesCI\"] or \"DARK\" in const.upper()):\n",
+    "            if (const in [\"Offset\", \"Noise\", \"SlopesPC\", \"SlopesCI\", \"RelativeGain\"] or \"DARK\" in const.upper()):\n",
     "                dcond = Conditions.Dark\n",
     "                mcond = getattr(dcond, dclass)(**pars)\n",
     "            else:\n",
@@ -708,10 +708,17 @@
     "        for key, item in keys.items():\n",
     "            if item[0] in data:\n",
     "                rdata[key] = np.array(data[item[0]])[sort_ind]\n",
+    "                \n",
+    "        # Limit number of memory cells to show\n",
+    "        # with respect to one available in data\n",
+    "        if len(rdata['Mean'].shape)==4:\n",
+    "            nMem = min(nMemToShow, rdata['Mean'].shape[3])\n",
+    "        if len(rdata['Mean'].shape)<4:\n",
+    "            nMem = 1\n",
     "\n",
     "        nTimes = rdata['Mean'].shape[0]\n",
     "        nPixels = rdata['Mean'].shape[1] * rdata['Mean'].shape[2]\n",
-    "        nBins = nMemToShow * nPixels\n",
+    "        nBins = nMem * nPixels\n",
     "\n",
     "        # Avoid too low values\n",
     "        if const[0] in [\"Noise10Hz\", \"Offset10Hz\"]:\n",
@@ -728,16 +735,18 @@
     "        for key in rdata:\n",
     "            if len(rdata[key].shape)<3:\n",
     "                continue\n",
-    "            if dclass in ['AGIPD', 'LPD']:\n",
-    "                pdata[key] = rdata[key][:, :, :, :nMemToShow].reshape(\n",
+    "            if len(rdata[key].shape)==4:\n",
+    "                # take first nMem memory cells\n",
+    "                pdata[key] = rdata[key][:, :, :, :nMem].reshape(\n",
     "                nTimes, nBins).swapaxes(0, 1)\n",
     "            else:\n",
+    "                # case of no memory cells\n",
     "                pdata[key] = rdata[key].reshape(nTimes, nBins).swapaxes(0, 1)\n",
     "\n",
     "        # Summary over ASICs\n",
     "        adata = {}\n",
     "        for key in rdata:\n",
-    "            if len(rdata[key].shape)<3 or nMemToShow==1:\n",
+    "            if len(rdata[key].shape)<3 or nMem==1:\n",
     "                continue\n",
     "            adata[key] = np.nanmean(rdata[key], axis=(1, 2)).swapaxes(0, 1)\n",
     "\n",
@@ -780,13 +789,15 @@
     "                fname = '{}_{}'.format(fname, item)\n",
     "            fname = '{}_ASIC_{}.png'.format(fname, key)\n",
     "\n",
-    "            if nMemToShow>1:\n",
+    "            if nMem>1:\n",
     "                htype=HMType.INSET_AXIS\n",
     "            else:      \n",
     "                htype=HMType.mro\n",
     "\n",
-    "            hm_combine(pdata[key][::-1].astype(float), htype=htype,\n",
+    "            hm_combine(pdata[key][::-1].astype(float), htype=htype, mem_cells=nMem,\n",
     "                      x_label='Creation Time', y_label=sp_name,\n",
+    "                      y_ticks=np.arange(0, nBins+1, nBins//16),\n",
+    "                       y_ticklabels=np.arange(0, nPixels+1, nPixels//16),\n",
     "                      x_ticklabels=ctimes_ticks,\n",
     "                      x_ticks=np.arange(len(ctimes_ticks))+0.3,\n",
     "                      title=title, cb_label=cb_label,\n",
@@ -794,7 +805,7 @@
     "                      fname=fname,\n",
     "                      pad=[0.125, 0.125, 0.12, 0.185])\n",
     "\n",
-    "            if nMemToShow>1:\n",
+    "            if nMem>1:\n",
     "                vmin,vmax = get_range(adata[key][::-1].flatten(), plot_range)\n",
     "                hm_combine(adata[key].astype(float), htype=HMType.mro,\n",
     "                      x_label='Creation Time', y_label='Memory cell ID',\n",
diff --git a/reportservice/report_conf.yaml b/reportservice/report_conf.yaml
index c272c6755..4a6ade68e 100644
--- a/reportservice/report_conf.yaml
+++ b/reportservice/report_conf.yaml
@@ -81,17 +81,22 @@ SPB:
         constants:
             - "Noise"
             - "Offset"
+            - "RelativeGain"
         dclass: "jungfrau"
-        nMemToShow: 1
+        nMemToShow: 16
         modules:
             - "Jungfrau_M035"
             - "Jungfrau_M203"
             - "Jungfrau_M221"
+            - "Jungfrau_M275"
+            - "Jungfrau_M273"
+            - "Jungfrau_M267"
         bias-voltage:
             - 90
             - 180
         memory-cells:
             - 1
+            - 16
         pixels-x:
             - 1024
         pixels-y:
@@ -100,19 +105,22 @@ SPB:
         temperature:
             - 291
         integration-time:
+            - 4.96
             - 50
             - 250
         gain-setting:
             - 0
+            - 1
+            - 2
         separate-plot:
             - "integration_time"
             - "gain_setting"
+            - "memory_cells"
         parameter-names:
             - "bias_voltage"
             - "integration_time"
             - "pixels_x"
             - "pixels_y"
-            - "gain_setting"
             - "temperature"
             - "memory_cells"
         spShape:
@@ -291,6 +299,7 @@ FXE:
         constants:
             - "Noise"
             - "Offset"
+            - "RelativeGain"
         dclass: "jungfrau"
         modules:
             - "Jungfrau_M233"
@@ -301,6 +310,7 @@ FXE:
             - 180
         memory-cells:
             - 1
+            - 16
         pixels-x:
             - 1024
         pixels-y:
@@ -309,19 +319,22 @@ FXE:
         temperature:
             - 291
         integration-time:
+            - 4.96
             - 50
             - 250
         gain-setting:
             - 0
+            - 1
+            - 2
         separate-plot:
             - "integration_time"
             - "gain_setting"
+            - "memory_cells"
         parameter-names:
             - "bias_voltage"
             - "integration_time"
             - "pixels_x"
             - "pixels_y"
-            - "gain_setting"
             - "temperature"
             - "memory_cells"
         spShape:
@@ -511,7 +524,7 @@ SQS:
         x-labels:
             - "Sensor Temperature"
             - "Integration Time"
-        sp-name: "ASICs id"
+        sp-name: "Supercolumn 256*256"
         photon-energy: 9.2
         use-existing: "''"
         out-folder: "/gpfs/exfel/data/scratch/xcal/report_service/tmp/{instrument}/{detector}/"
@@ -571,6 +584,7 @@ HED:
         constants:
             - "Noise"
             - "Offset"
+            - "RelativeGain"
         dclass: "jungfrau"
         nMemToShow: 1
         modules:
@@ -581,6 +595,7 @@ HED:
             - 180
         memory-cells:
             - 1
+            - 16
         pixels-x:
             - 1024
         pixels-y:
@@ -589,19 +604,22 @@ HED:
         temperature:
             - 291
         integration-time:
+            - 4.96
             - 50
             - 250
         gain-setting:
             - 0
+            - 1
+            - 2
         separate-plot:
             - "integration_time"
             - "gain_setting"
+            - "memory_cells"
         parameter-names:
             - "bias_voltage"
             - "integration_time"
             - "pixels_x"
             - "pixels_y"
-            - "gain_setting"
             - "temperature"
             - "memory_cells"
         spShape:
-- 
GitLab