From de94a3041e891222f3110047d0c56e7fe3060c58 Mon Sep 17 00:00:00 2001 From: David Hammer <dhammer@mailbox.org> Date: Thu, 25 Feb 2021 13:57:22 +0100 Subject: [PATCH] Additional refactoring --- .../Characterize_AGIPD_Gain_Darks_NBC.ipynb | 72 +++++++++++-------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb index dfab44ce1..4672a39e2 100644 --- a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb +++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb @@ -117,7 +117,6 @@ "source": [ "gains = np.arange(3)\n", "\n", - "IL_MODE = interlaced\n", "max_cells = mem_cells\n", " \n", "offset_runs = OrderedDict()\n", @@ -127,7 +126,7 @@ "\n", "creation_time=None\n", "if use_dir_creation_date:\n", - " creation_time = get_dir_creation_date(in_folder, run_high, verbosity=2)\n", + " creation_time = get_dir_creation_date(in_folder, run_high)\n", "\n", "print(f\"Using {creation_time} as creation time of constant.\")\n", "\n", @@ -165,8 +164,8 @@ "source": [ "runs = [run_high, run_med, run_low]\n", "\n", - "if \"{\" in h5path_ctrl:\n", - " h5path_ctrl = h5path_ctrl.format(karabo_id_control)\n", + "# insert control device if format string (does nothing otherwise)\n", + "h5path_ctrl = h5path_ctrl.format(karabo_id_control)\n", "\n", "if gain_setting == 0.1:\n", " if creation_time.replace(tzinfo=None) < dateutil.parser.parse('2020-01-31'):\n", @@ -218,7 +217,7 @@ "print(f\"Memory cells: {mem_cells}/{max_cells}\")\n", "print(\"Runs: {}\".format([ v for v in offset_runs.values()]))\n", "print(f\"Sequences: {sequences}\")\n", - "print(f\"Interlaced mode: {IL_MODE}\")\n", + "print(f\"Interlaced mode: {interlaced}\")\n", "print(f\"Using DB: {db_output}\")\n", "print(f\"Input: {in_folder}\")\n", "print(f\"Output: {out_folder}\")\n", @@ -246,6 +245,16 @@ "print(f\"Will process a total of {total_sequences} files.\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def index_to_qm(i: int):\n", + " return f\"Q{i//4+1}M{i%4+1}\"" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -261,9 +270,24 @@ "metadata": {}, "outputs": [], "source": [ - "def characterize_module(inp: Tuple[str, int, int]) -> Tuple[np.array, np.array, np.array, np.array, int, np.array, int, float]:\n", - " fast_data_filename, channel, gg = inp\n", + "if thresholds_offset_hard == [0, 0]:\n", + " thresholds_offset_hard = [thresholds_offset_hard_hg, thresholds_offset_hard_mg, thresholds_offset_hard_lg]\n", + "else:\n", + " thresholds_offset_hard = [thresholds_offset_hard] * 3\n", "\n", + "if thresholds_noise_hard == [0, 0]:\n", + " thresholds_noise_hard = [thresholds_noise_hard_hg, thresholds_noise_hard_mg, thresholds_noise_hard_lg]\n", + "else:\n", + " thresholds_noise_hard = [thresholds_noise_hard] * 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def characterize_module(fast_data_filename: str, channel: int, gg: int) -> Tuple[np.array, np.array, np.array, np.array, int, np.array, int, float]:\n", " if max_cells == 0:\n", " num_cells = get_num_cells(fast_data_filename, karabo_id, channel)\n", " else:\n", @@ -301,7 +325,7 @@ " im = np.array(infile[f\"{h5path_f}/data\"][first_index:last_index,...])\n", " cellIds = np.squeeze(infile[f\"{h5path_f}/cellId\"][first_index:last_index,...])\n", "\n", - " if IL_MODE:\n", + " if interlaced:\n", " ga = im[1::2, 0, ...]\n", " im = im[0::2, 0, ...].astype(np.float32)\n", " cellIds = cellIds[::2]\n", @@ -349,33 +373,22 @@ "\n", " return offset, noise, gains, gains_std, gg, bp, num_cells, local_acq_rate\n", "\n", + "\n", "offset_g = OrderedDict()\n", "noise_g = OrderedDict()\n", "gain_g = OrderedDict()\n", "gainstd_g = OrderedDict()\n", "badpix_g = OrderedDict()\n", - "gg = 0\n", "\n", "start = datetime.now()\n", "all_cells = []\n", "all_acq_rate = []\n", "\n", - "if thresholds_offset_hard == [0, 0]:\n", - " thresholds_offset_hard = [thresholds_offset_hard_hg, thresholds_offset_hard_mg, thresholds_offset_hard_lg]\n", - "else:\n", - " thresholds_offset_hard = [thresholds_offset_hard] * 3\n", - "\n", - "if thresholds_noise_hard == [0, 0]:\n", - " thresholds_noise_hard = [thresholds_noise_hard_hg, thresholds_noise_hard_mg, thresholds_noise_hard_lg]\n", - "else:\n", - " thresholds_noise_hard = [thresholds_noise_hard] * 3\n", - "\n", - "\n", "inp = []\n", - "for gain, mapped_files in gain_mapped_files.items():\n", + "for gg, (gain, mapped_files) in enumerate(gain_mapped_files.items()):\n", " dones = []\n", " for i in modules:\n", - " qm = f\"Q{i//4+1}M{i%4+1}\"\n", + " qm = index_to_qm(i)\n", " if qm in mapped_files and not mapped_files[qm].empty():\n", " fname_in = mapped_files[qm].get()\n", " print(\"Process file: \", fname_in)\n", @@ -384,17 +397,14 @@ " continue\n", " inp.append((fname_in, i, gg))\n", "\n", - " gg += 1\n", - "\n", "with multiprocessing.Pool() as pool:\n", - " results = pool.map(characterize_module, inp)\n", + " results = pool.starmap(characterize_module, inp)\n", "\n", - "for ii, r in enumerate(results):\n", - " offset, noise, gains, gains_std, gg, bp, thiscell, thisacq = r\n", + "for offset, noise, gains, gains_std, gg, bp, thiscell, thisacq in results:\n", " all_cells.append(thiscell)\n", " all_acq_rate.append(thisacq)\n", " for i in modules:\n", - " qm = f\"Q{i//4+1}M{i%4+1}\"\n", + " qm = index_to_qm(i)\n", " if qm not in offset_g:\n", " offset_g[qm] = np.zeros((offset.shape[0], offset.shape[1], offset.shape[2], 3))\n", " noise_g[qm] = np.zeros_like(offset_g[qm])\n", @@ -461,7 +471,7 @@ "source": [ "res = OrderedDict()\n", "for i in modules:\n", - " qm = \"Q{}M{}\".format(i//4+1, i%4+1)\n", + " qm = index_to_qm(i)\n", " res[qm] = {'Offset': offset_g[qm],\n", " 'Noise': noise_g[qm],\n", " 'ThresholdsDark': thresholds_g[qm],\n", @@ -492,7 +502,7 @@ "# Create the modules dict of karabo_das and PDUs\n", "qm_dict = OrderedDict()\n", "for i, k_da in zip(modules, karabo_da):\n", - " qm = f\"Q{i//4+1}M{i%4+1}\"\n", + " qm = index_to_qm(i)\n", " qm_dict[qm] = {\"karabo_da\": k_da,\n", " \"db_module\": \"\"}" ] @@ -596,7 +606,7 @@ "source": [ "mnames=[]\n", "for i in modules:\n", - " qm = f\"Q{i//4+1}M{i % 4+1}\"\n", + " qm = index_to_qm(i)\n", " mnames.append(qm)\n", " display(Markdown(f'## Position of the module {qm} and its ASICs##'))\n", "show_processed_modules(dinstance, constants=None, mnames=mnames, mode=\"position\")" -- GitLab