From 8e4cd0ef5380f8e4df2d8eb7dae888582cf0d9ff Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Tue, 13 Dec 2022 13:42:49 +0100
Subject: [PATCH] warning instead of warn and some refactors

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 42 +++++++++----------
 ...IPD_Retrieve_Constants_Precorrection.ipynb | 23 ++--------
 2 files changed, 24 insertions(+), 41 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index c746b776b..e38e3245e 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -122,12 +122,13 @@
    "outputs": [],
    "source": [
     "import itertools\n",
+    "import os\n",
     "import math\n",
     "import multiprocessing\n",
+    "import re\n",
     "import warnings\n",
-    "import os\n",
     "from datetime import timedelta\n",
-    "from logging import warn\n",
+    "from logging import warning\n",
     "from pathlib import Path\n",
     "from time import perf_counter\n",
     "\n",
@@ -439,7 +440,7 @@
     "if gain_mode:\n",
     "    for to_disable in disable_for_fixed_gain:\n",
     "        if corr_bools.get(to_disable, False):\n",
-    "            warn(f\"{to_disable} correction was requested, but does not apply to fixed gain mode\")\n",
+    "            warning(f\"{to_disable} correction was requested, but does not apply to fixed gain mode\")\n",
     "            corr_bools[to_disable] = False"
    ]
   },
@@ -463,7 +464,7 @@
     "        cell_sel = LitFrameSelection(r, train_ids, max_pulses, energy_threshold, use_super_selection)\n",
     "        cell_sel.print_report()\n",
     "    except LitFrameFinderError as err:\n",
-    "        warn(f\"Cannot use AgipdLitFrameFinder due to:\\n{err}\")\n",
+    "        warning(f\"Cannot use AgipdLitFrameFinder due to:\\n{err}\")\n",
     "        cell_sel = CellRange(max_pulses, max_cells=mem_cells)\n",
     "else:\n",
     "    # Use range selection\n",
@@ -490,11 +491,11 @@
     "            photon_energy = (h * c / e) / (wavelength_data.as_single_value(rtol=1e-2) * 1e-6)\n",
     "            print(f'Obtained photon energy {photon_energy:.3f} keV from {use_xgm_device}')\n",
     "        except ValueError:\n",
-    "            warn('XGM source available but photon energy varies greater than 1%, '\n",
+    "            warning('XGM source available but photon energy varies greater than 1%, '\n",
     "                 'photon rounding disabled!')\n",
     "            round_photons = False\n",
     "    else:\n",
-    "        warn('Neither explicit photon energy nor XGM device configured, photon rounding disabled!')\n",
+    "        warning('Neither explicit photon energy nor XGM device configured, photon rounding disabled!')\n",
     "        round_photons = False\n",
     "elif round_photons:\n",
     "    print(f'Photon energy for rounding: {photon_energy:.3f} keV')"
@@ -559,6 +560,9 @@
     "# Retrieve calibration constants to RAM\n",
     "agipd_corr.allocate_constants(modules, (3, mem_cells_db, 512, 128))\n",
     "\n",
+    "metadata = CalibrationMetadata(metadata_folder or out_folder)\n",
+    "# NOTE: this notebook will not overwrite calibration metadata file\n",
+    "const_yaml = metadata.get(\"retrieved-constants\", {})\n",
     "\n",
     "def retrieve_constants(mod):\n",
     "    \"\"\"\n",
@@ -567,12 +571,12 @@
     "    Metadata for constants is taken from yml file or retrieved from the DB\n",
     "    \"\"\"\n",
     "    k_da = module_index_to_karabo_da[mod]\n",
-    "    try:\n",
-    "        # check if there is a yaml file in out_folder that has the device constants.\n",
-    "        if k_da in const_yaml:\n",
-    "            when = agipd_corr.initialize_from_yaml(k_da, const_yaml, mod)\n",
-    "            print(f\"Found constants for {k_da} in calibration_metadata.yml\")\n",
-    "        else:\n",
+    "    # check if there is a yaml file in out_folder that has the device constants.\n",
+    "    if k_da in const_yaml:\n",
+    "        when = agipd_corr.initialize_from_yaml(k_da, const_yaml, mod)\n",
+    "        print(f\"Found constants for {k_da} in calibration_metadata.yml\")\n",
+    "    else:\n",
+    "        try:\n",
     "            # TODO: replace with proper retrieval (as done in pre-correction)\n",
     "            when = agipd_corr.initialize_from_db(\n",
     "                karabo_id=karabo_id,\n",
@@ -589,10 +593,10 @@
     "                only_dark=False,\n",
     "            )\n",
     "            print(f\"Queried CalCat for {k_da}\")\n",
-    "    except Exception as e:\n",
-    "        err = f\"Error: {e}\\nError traceback: {traceback.format_exc()}\"\n",
-    "        when = None\n",
-    "    return err, mod, when, k_da\n",
+    "        except Exception as e:\n",
+    "            warning(f\"Module: {k_da}, {e}\")\n",
+    "            when = None\n",
+    "    return mod, when, k_da\n",
     "\n",
     "\n",
     "print(f'Preparing constants (FF: {agipd_corr.corr_bools.get(\"xray_corr\", False)}, PC: {any(agipd_corr.pc_bools)}, '\n",
@@ -612,11 +616,7 @@
     "# allocate memory for images and hists\n",
     "n_images_max = mem_cells * 256\n",
     "data_shape = (n_images_max, 512, 128)\n",
-    "agipd_corr.allocate_images(data_shape, n_cores_files)\n",
-    "\n",
-    "metadata = CalibrationMetadata(metadata_folder or out_folder)\n",
-    "# NOTE: this notebook will not overwrite calibration metadata file\n",
-    "const_yaml = metadata.get(\"retrieved-constants\", {})"
+    "agipd_corr.allocate_images(data_shape, n_cores_files)"
    ]
   },
   {
diff --git a/notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb b/notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
index 8137e875e..bbb32e928 100644
--- a/notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
+++ b/notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
@@ -335,7 +335,9 @@
     "            mdata_dict[\"physical-detector-unit\"] = mdata_const.device_name\n",
     "        else:\n",
     "            const_mdata[\"file-path\"] = const_dict[const_name][:2]\n",
-    "            const_mdata[\"creation-time\"] = None"
+    "            const_mdata[\"creation-time\"] = None\n",
+    "\n",
+    "    return mdata_dict, k_da, local_acq_rate, local_mem_cells"
    ]
   },
   {
@@ -436,25 +438,6 @@
     "\n",
     "metadata.save()"
    ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "print(\"\\nRetrieved constants for modules:\",\n",
-    "        ', '.join([module_index_to_qm(x) for x in modules]))\n",
-    "print(f\"Operating conditions are:\")\n",
-    "print(f\"• Bias voltage: {bias_voltage}\")\n",
-    "print(f\"• Memory cells: {mem_cells}\")\n",
-    "print(f\"• Acquisition rate: {acq_rate}\")\n",
-    "print(f\"• Gain mode: {gain_mode.name}\")\n",
-    "print(f\"• Gain setting: {gain_setting}\")\n",
-    "print(f\"• Integration time: {integration_time}\")\n",
-    "print(f\"• Photon Energy: 9.2\")\n",
-    "step_timer.done_step(f\"Constant metadata is saved under \\\"retrieved-constants\\\" in {metadata.filename}\")"
-   ]
   }
  ],
  "metadata": {
-- 
GitLab