diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb index 2cbf2883903625f971734edb684e66e5c8011853..a5b646c7c26529cc5f7f4c8213059917515cb46a 100644 --- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb @@ -834,12 +834,12 @@ "\n", "fig, ax = plt.subplots(figsize=(18, 10))\n", "raw_mean = np.mean(raw, axis=1)\n", + "vmin, vmax = np.percentile(raw_mean, [5, 95])\n", "geom.plot_data_fast(\n", " raw_mean,\n", " ax=ax,\n", - " vmin=min(0.75*np.median(raw_mean[raw_mean > 0]), 2000),\n", - " vmax=max(1.5*np.median(raw_mean[raw_mean > 0]), 16000),\n", - " cmap=\"jet\",\n", + " vmin=vmin,\n", + " vmax=vmax,\n", " colorbar={'shrink': 1, 'pad': 0.01},\n", ")\n", "ax.set_title(f'{karabo_id} - Mean RAW', size=18)\n", @@ -863,12 +863,9 @@ "\n", "fig, ax = plt.subplots(figsize=(18, 10))\n", "corrected_mean = np.mean(corrected, axis=1)\n", - "_corrected_vmin = min(0.75*np.median(corrected_mean[corrected_mean > 0]), -0.5)\n", - "_corrected_vmax = max(2.*np.median(corrected_mean[corrected_mean > 0]), 100)\n", + "vmin, vmax = np.percentile(corrected_mean, [5, 95])\n", "\n", - "mean_plot_kwargs = dict(\n", - " vmin=_corrected_vmin, vmax=_corrected_vmax, cmap=\"jet\"\n", - ")\n", + "mean_plot_kwargs = dict(vmin=vmin, vmax=vmax)\n", "\n", "if strixel_sensor:\n", " if strixel_sensor == \"A1256\":\n", @@ -929,11 +926,11 @@ "display(Markdown((f\"#### A single image from train {tid}\")))\n", "\n", "fig, ax = plt.subplots(figsize=(18, 10))\n", + "vmin, vmax = np.percentile(corrected_train, [5, 95])\n", "\n", "single_plot_kwargs = dict(\n", - " vmin=min(0.75 * np.median(corrected_train[corrected_train > 0]), -0.5),\n", - " vmax=max(2.0 * np.median(corrected_train[corrected_train > 0]), 100),\n", - " cmap=\"jet\"\n", + " vmin=vmin,\n", + " vmax=vmax,\n", ")\n", "\n", "if not strixel_sensor:\n", @@ -1060,7 +1057,6 @@ "geom.plot_data_fast(\n", " gain_max,\n", " ax=ax,\n", - " cmap=\"jet\",\n", " colorbar={'shrink': 1, 'pad': 0.01},\n", ")\n", "plt.show()" @@ -1081,7 +1077,14 @@ "outputs": [], "source": [ "table = []\n", - "for item in BadPixels:\n", + "badpixels = [\n", + " BadPixels.OFFSET_OUT_OF_THRESHOLD,\n", + " BadPixels.NOISE_OUT_OF_THRESHOLD,\n", + " BadPixels.OFFSET_NOISE_EVAL_ERROR,\n", + " BadPixels.NO_DARK_DATA,\n", + " BadPixels.WRONG_GAIN_VALUE,\n", + " ]\n", + "for item in badpixels:\n", " table.append(\n", " (item.name, f\"{item.value:016b}\"))\n", "md = display(Latex(tabulate.tabulate(\n", @@ -1107,16 +1110,17 @@ "display(Markdown(f\"#### Bad pixels image for train {tid}\"))\n", "\n", "fig, ax = plt.subplots(figsize=(18, 10))\n", + "vmin, vmax = (0, sorted([bp.value for bp in badpixels])[-2])\n", "if not strixel_sensor:\n", " geom.plot_data_fast(\n", " np.log2(mask_train),\n", " ax=ax,\n", - " vmin=0, vmax=32, cmap=\"jet\",\n", + " vmin=vmin, vmax=vmax,\n", " colorbar={'shrink': 1, 'pad': 0.01},\n", " )\n", "else:\n", " mask = ax.imshow(\n", - " mask_train.squeeze(), vmin=0, vmax=32, cmap='jet', aspect=aspect)\n", + " mask_train.squeeze(), vmin=vmin, vmax=vmax, aspect=aspect)\n", " plt.colorbar(mask)\n", "\n", "plt.show()" diff --git a/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb index 6da1df8b914372e6bcfb7489438fd9cba056c0fd..08494dc42112fba9e6135c7291795ff1d4c4e048 100644 --- a/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb @@ -56,9 +56,13 @@ "matplotlib.use(\"agg\")\n", "%matplotlib inline\n", "\n", + "import tabulate\n", + "from IPython.display import Latex, Markdown, display\n", + "from XFELDetAna.plotting.simpleplot import simplePlot\n", + "\n", + "from cal_tools.enums import BadPixels\n", "from cal_tools.plotting import init_jungfrau_geom, show_processed_modules_jungfrau\n", - "from cal_tools.tools import CalibrationMetadata\n", - "from XFELDetAna.plotting.simpleplot import simplePlot" + "from cal_tools.tools import CalibrationMetadata" ] }, { @@ -188,55 +192,60 @@ "source": [ "gainstages = 3\n", "gain_names = [\"High Gain\", \"Medium Gain\", \"Low Gain\" ]\n", - "const_range = {\n", - " \"Offset\": [(0, 8000), (8000, 16000), (8000, 16000)],\n", - " \"Noise\": [(0., 50.), (0., 50.), (0., 50.)],\n", - " \"BadPixelsDark\": [(0., 5.), (0., 5.), (0., 5.)],\n", - "}\n", - "# vmin and vmax are different for Offset for fixed gain constants. \n", - "if fixed_gain:\n", - " const_range[\"Offset\"] = [(0, 8000), (0, 8000), (0, 8000)]\n", - "\n", - "diff_const_range = {\n", - " \"Offset\": [(0, 500), (0, 500), (0, 500)],\n", - " \"Noise\": [(0., 5.), (0., 5.), (0., 5.)],\n", - " \"BadPixelsDark\": [(0., 5.), (0., 5.), (0., 5.)],\n", - "}\n", - "percentage_range = (0, 100)\n", - "perc_const_range = {c: [percentage_range]*3 for c in dark_constants}\n", + "\n", "gs = gridspec.GridSpec(2, 4)\n", "\n", "axes = {\n", - " \"ax0\": {\n", + " \"map\": {\n", " \"gs\": gs[0, 1:3],\n", " \"shrink\": 0.7,\n", " \"pad\": 0.05,\n", " \"label\": \"ADCu\",\n", " \"title\": \"{}\",\n", " \"location\": \"right\",\n", - " \"range\": const_range,\n", " },\n", - " \"ax1\": {\n", + " \"diff\": {\n", " \"gs\": gs[1, :2],\n", " \"shrink\": 0.7,\n", " \"pad\": 0.02,\n", " \"label\": \"ADCu\",\n", " \"location\": \"left\",\n", " \"title\": \"Difference with previous {}\",\n", - " \"range\": diff_const_range,\n", " },\n", - " \"ax2\": {\n", + " \"diff_frac\": {\n", " \"gs\": gs[1, 2:],\n", " \"shrink\": 0.7,\n", " \"pad\": 0.02,\n", " \"label\": \"%\",\n", " \"location\": \"right\",\n", " \"title\": \"Difference with previous {} %\",\n", - " \"range\": perc_const_range,\n", " },\n", " }" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def badpx(constant_name):\n", + " return \"bad\" in constant_name.lower()\n", + "\n", + "\n", + "def bp_entry(bp):\n", + " return [f\"{bp.name:<30s}\", f\"{bp.value:032b}\", f\"{int(bp.value)}\"]\n", + "\n", + "\n", + "badpixels = [\n", + " BadPixels.OFFSET_OUT_OF_THRESHOLD,\n", + " BadPixels.NOISE_OUT_OF_THRESHOLD,\n", + " BadPixels.OFFSET_NOISE_EVAL_ERROR,\n", + " BadPixels.NO_DARK_DATA,\n", + " BadPixels.WRONG_GAIN_VALUE,\n", + " ]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -253,19 +262,38 @@ "outputs": [], "source": [ "for cname, const in curr_constants.items():\n", + " if badpx(cname):\n", + " table = [bp_entry(bp) for bp in badpixels]\n", + " display(Markdown(\"\"\"**The bad pixel** mask is encoded as a bit mask.\"\"\"))\n", + " display(Latex(\n", + " tabulate.tabulate(\n", + " table,\n", + " tablefmt='latex',\n", + " headers=[\"Name\", \"bit value\", \"integer value\"]\n", + " )))\n", "\n", " # Prepare the stacked mean of constant,\n", " # the difference with the previous constant\n", " # and the fraction of that difference.\n", + "\n", " mean_const = np.nanmean(const, axis=3)\n", - " mean_diff = np.abs(np.nanmean(const, axis=3) - np.nanmean(prev_constants[cname], axis=3)) # noqa\n", - " mean_frac = np.abs(mean_diff / mean_const) * 100\n", - " \n", + " mean_diff = np.abs(\n", + " np.nanmean(const, axis=3) - np.nanmean(\n", + " prev_constants[cname],\n", + " axis=3)\n", + " )\n", + " mean_frac = np.divide(\n", + " mean_diff,\n", + " mean_const,\n", + " out=np.zeros_like(mean_const),\n", + " where=(mean_const != 0)\n", + " ) * 100\n", + "\n", " for gain in range(gainstages):\n", " data_to_plot = {\n", - " f'ax0': mean_const[..., gain],\n", - " f'ax1': mean_diff[..., gain],\n", - " f'ax2': mean_frac[..., gain],\n", + " f'map': mean_const[..., gain],\n", + " f'diff': mean_diff[..., gain],\n", + " f'diff_frac': mean_frac[..., gain],\n", " }\n", "\n", " # Plotting constant overall modules.\n", @@ -279,18 +307,25 @@ "\n", " # Avoid difference plots if previous constants\n", " # are missing for the detector.\n", - " if cname in exculded_constants and axname != \"ax0\":\n", + " if cname in exculded_constants and axname != \"map\":\n", " break\n", " ax = fig.add_subplot(axv[\"gs\"])\n", - " vmin, vmax = axv[\"range\"][cname][gain]\n", + "\n", + " if badpx(cname):\n", + " vmin, vmax = (0, sorted([bp.value for bp in badpixels])[-2])\n", + " else:\n", + " vmin, vmax = np.percentile(data_to_plot[axname], [5, 95])\n", + "\n", " geom.plot_data(\n", " data_to_plot[axname],\n", - " vmin=vmin, vmax=vmax, ax=ax, \n", + " vmin=vmin,\n", + " vmax=vmax,\n", + " ax=ax, \n", " colorbar={\n", " \"shrink\": axv[\"shrink\"],\n", " \"pad\": axv[\"pad\"],\n", " \"location\": axv[\"location\"],\n", - " }\n", + " },\n", " )\n", "\n", " colorbar = ax.images[0].colorbar\n", @@ -300,7 +335,7 @@ " ax.set_title(axv[\"title\"].format(\n", " f\"{cname} {gain_names[gain]}\"), fontsize=15)\n", " \n", - " if axname == \"ax0\":\n", + " if axname == \"map\":\n", " ax.set_xlabel('Columns', fontsize=15)\n", " ax.set_ylabel('Rows', fontsize=15)\n", " ax.tick_params(labelsize=15)\n",