diff --git a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb index ee6e5c160964ce530ecc99fae80ff6b7ea362039..fbd93209f6fbd32753332082808d8ae70e005769 100644 --- a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb +++ b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb @@ -102,20 +102,15 @@ "run_nums = [run_high, run_med, run_low]\n", "in_folder = Path(in_folder)\n", "out_folder = Path(out_folder)\n", - "out_folder.mkdir(exist_ok=True)\n", - "\n", + "out_folder.mkdir(parents=True, exist_ok=True)\n", "print(f\"Process modules: {karabo_da}\")\n", "\n", - "run_dc = RunDirectory(in_folder / f\"r{run_high:04d}\")\n", - "file_loc = f\"proposal:{run_dc.run_metadata()['proposalNumber']} runs:{run_high} {run_med} {run_low}\" # noqa\n", - "\n", - "instrument_src = instrument_source_template.format(karabo_id, receiver_template)\n", "ctrl_src = ctrl_source_template.format(karabo_id_control, control_template)\n", "\n", "# Read report path to associate it later with injected constants.\n", "report = get_report(out_folder)\n", "\n", - "\n", + "creation_time = None\n", "if overwrite_creation_time:\n", " creation_time = datetime.datetime.strptime(\n", " overwrite_creation_time, \"%Y-%m-%d %H:%M:%S.%f\"\n", @@ -129,6 +124,19 @@ " karabo_id_control = karabo_id" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "c176a86f", + "metadata": {}, + "outputs": [], + "source": [ + "run_dc = RunDirectory(in_folder / f\"r{run_high:04d}\")\n", + "file_loc = f\"proposal:{run_dc.run_metadata()['proposalNumber']} runs:{run_high} {run_med} {run_low}\" # noqa\n", + "\n", + "receivers = list(run_dc.select(f'{karabo_id}/DET/{receiver_template}*').all_sources)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -193,7 +201,15 @@ "single_photon = conditions[\"single_photon\"].pop()\n", "print(\"Single photon: \", single_photon)\n", "acquisition_rate = conditions[\"acquisition_rate\"].pop()\n", - "print(\"Acquisition rate: \", acquisition_rate)" + "print(\"Acquisition rate: \", acquisition_rate)\n", + "\n", + "# Decide if GH2 is 25um or 50um\n", + "gh2_hostname = run_dc.get_run_value(ctrl_src, \"rxHostname\")\n", + "# gh2_hostname is a vector of bytes objects.\n", + "# GH2 25um has two host-names unlike 50um which has one.\n", + "if gh2_hostname[1].decode(\"utf-8\"): # For 25um use virtual karabo_das for CALCAT data mapping.\n", + " karabo_da = [f\"{karabo_da[0]}/1\", f\"{karabo_da[0]}/2\"]\n", + " print(\"Processing 25um Gotthard2.\")" ] }, { @@ -204,9 +220,7 @@ "outputs": [], "source": [ "def specify_trains_to_process(\n", - " img_key_data: \"extra_data.KeyData\", # noqa\n", - " max_trains: int = 0,\n", - " min_trains: int = 0,\n", + " img_key_data: \"extra_data.KeyData\",\n", "):\n", " \"\"\"Specify total number of trains to process.\n", " Based on given min_trains and max_trains, if given.\n", @@ -218,7 +232,7 @@ " n_trains = img_key_data.shape[0]\n", " all_trains = len(img_key_data.train_ids)\n", " print(\n", - " f\"{mod} has {all_trains - n_trains} \"\n", + " f\"{receiver} has {all_trains - n_trains} \"\n", " f\"trains with empty frames out of {all_trains} trains\"\n", " )\n", "\n", @@ -272,7 +286,7 @@ "def convert_train(wid, index, tid, d):\n", " \"\"\"Convert a Gotthard2 train from 12bit to 10bit.\"\"\"\n", " gotthard2algs.convert_to_10bit(\n", - " d[instr_mod_src][\"data.adc\"], lut, data_10bit[index, ...]\n", + " d[receiver][\"data.adc\"], lut, data_10bit[index, ...]\n", " )" ] }, @@ -293,7 +307,7 @@ " np.uint16\n", ")\n", "empty_lut = np.stack(1280 * [np.stack([empty_lut] * 2)], axis=0)\n", - "for mod in karabo_da:\n", + "for mod, receiver in zip(karabo_da, receivers):\n", "\n", " # Retrieve LUT constant\n", " lut, time = get_constant_from_db_and_time(\n", @@ -308,10 +322,7 @@ " print_once=False,\n", " )\n", " print(f\"Retrieved LUT constant with creation-time {time}\")\n", - " # Path to pixels ADC values\n", - " instr_mod_src = instrument_src.format(int(mod[-2:]))\n", "\n", - " # TODO: Validate the final shape to store constants.\n", " cshape = (1280, 2, 3)\n", "\n", " offset_map[mod] = context.alloc(shape=cshape, dtype=np.float32)\n", @@ -320,10 +331,10 @@ "\n", " for run_num, [gain, run_dc] in run_dcs_dict.items():\n", " step_timer.start()\n", - " n_trains = specify_trains_to_process(run_dc[instr_mod_src, \"data.adc\"])\n", + " n_trains = specify_trains_to_process(run_dc[receiver, \"data.adc\"])\n", "\n", " # Select requested number of trains to process.\n", - " dc = run_dc.select(instr_mod_src, require_all=True).select_trains(\n", + " dc = run_dc.select(receiver, require_all=True).select_trains(\n", " np.s_[:n_trains]\n", " )\n", "\n", @@ -332,7 +343,7 @@ " step_timer.start()\n", " # Convert 12bit data to 10bit\n", " data_10bit = context.alloc(\n", - " shape=dc[instr_mod_src, \"data.adc\"].shape, dtype=np.float32\n", + " shape=dc[receiver, \"data.adc\"].shape, dtype=np.float32\n", " )\n", " context.map(convert_train, dc)\n", " step_timer.done_step(\"convert to 10bit\")\n", @@ -360,7 +371,7 @@ " context.map(offset_noise_cell, (even_data, odd_data))\n", "\n", " # Split even and odd gain data.\n", - " data_gain = dc[instr_mod_src, \"data.gain\"].ndarray()\n", + " data_gain = dc[receiver, \"data.gain\"].ndarray()\n", " even_gain = data_gain[:, 20::2, :]\n", " odd_gain = data_gain[:, 21::2, :]\n", " raw_g = 3 if gain == 2 else gain\n", @@ -444,10 +455,10 @@ " ax.set_xticks(np.arange(0, 1281, 80))\n", " ax.set_xlabel(\"Stripes #\")\n", " ax.set_xlabel(\"BadPixels\")\n", - " ax.set_title(f\"Cell {cell} - Module {mod} ({pdu})\")\n", + " ax.set_title(f\"BadPixels map - Cell {cell} - Module {mod} ({pdu})\")\n", " ax.set_ylim([0, 5])\n", " ax.legend()\n", - " pass\n", + " plt.show()\n", "step_timer.done_step(f\"Creating bad pixels constant and plotting it.\")" ] }, @@ -458,8 +469,10 @@ "metadata": {}, "outputs": [], "source": [ - "for mod, pdu in zip(karabo_da, db_modules):\n", - " for cons, cname in zip([offset_map, noise_map], [\"Offset\", \"Noise\"]):\n", + "for cons, cname in zip([offset_map, noise_map], [\"Offset\", \"Noise\"]):\n", + " for mod, pdu in zip(karabo_da, db_modules):\n", + " display(Markdown(f\"### {cname} for module {mod}:\"))\n", + "\n", " for cell in [0, 1]:\n", " fig, ax = plt.subplots(figsize=(10, 5))\n", " for g_idx in [0, 1, 2]:\n", @@ -469,7 +482,7 @@ " ax.set_xlabel(cname)\n", " ax.set_title(f\"{cname} map - Cell {cell} - Module {mod} ({pdu})\")\n", " ax.legend()\n", - " pass" + " plt.show()" ] }, {