diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb index 8f5bc62e55b21bd0cd552d14c8b1c55339ea6a49..9bd9950e4c85fafcbd770e76dbb55927218230fa 100644 --- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb @@ -54,6 +54,8 @@ "# Parameters for plotting\n", "skip_plots = False # exit after writing corrected files\n", "\n", + "# Parameters for ROI selection and reduction\n", + "roi_definitions = [-1] # List with groups of 6 values defining ROIs, e.g. [3, 120, 180, 200, 550, -2] for module 3 (JNGFR03), slice 120:180, 200:550, average along axis -2 (slow scan, or -1 for fast scan)\n", "\n", "def balance_sequences(in_folder, run, sequences, sequences_per_node, karabo_da):\n", " from xfel_calibrate.calibrate import balance_sequences as bs\n", @@ -67,6 +69,7 @@ "outputs": [], "source": [ "import multiprocessing\n", + "import sys\n", "import warnings\n", "from functools import partial\n", "from pathlib import Path\n", @@ -127,7 +130,12 @@ " print(f\"Using {creation_time} as creation time\")\n", "\n", "if karabo_id_control == \"\":\n", - " karabo_id_control = karabo_id" + " karabo_id_control = karabo_id\n", + " \n", + "if any(axis_no not in {-2, -1, 2, 3} for axis_no in roi_definitions[5::6]):\n", + " print(\"ROI averaging must be on axis 2/3 (or equivalently -2/-1). \"\n", + " f\"Axis numbers given: {roi_definitions[5::6]}\")\n", + " sys.exit(1)" ] }, { @@ -378,6 +386,63 @@ "print(f\"Using {n_cpus} workers for correction.\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def save_reduced_rois(ofile, data_corr, mask_corr, karabo_da):\n", + " \"\"\"If ROIs are defined for this karabo_da, reduce them and save to the output file\"\"\"\n", + " rois_defined = 0\n", + " module_no = int(karabo_da[-2:])\n", + " params_source = f'{karabo_id}/ROIPROC/{karabo_da}'\n", + " rois_source = f'{params_source}:output/data'\n", + " \n", + " for i in range(len(roi_definitions) // 6):\n", + " roi_module, a1, a2, b1, b2, mean_axis = roi_definitions[i*6 : (i+1)*6]\n", + " if roi_module == module_no:\n", + " rois_defined += 1\n", + " # Apply the mask and average remaining pixels to 1D\n", + " roi_data = data_corr[..., a1:a2, b1:b2].mean(\n", + " axis=mean_axis, where=(mask_corr[..., a1:a2, b1:b2] == 0)\n", + " )\n", + " ofile.create_dataset(\n", + " f'INSTRUMENT/{rois_source}/roi{rois_defined}/data',\n", + " data=roi_data\n", + " )\n", + " ofile.create_group(f'CONTROL/{params_source}')\n", + " params_grp = ofile.create_group(f'RUN/{params_source}/roi{rois_defined}')\n", + " params_grp['region'] = np.array([[a1, a2, b1, b2]])\n", + " params_grp['reduce_axis'] = np.array([mean_axis])\n", + " \n", + " if rois_defined:\n", + " # Copy the index for the new source\n", + " ofile.copy(f'INDEX/{karabo_id}/DET/{karabo_da}:daqOutput/data',\n", + " f'INDEX/{rois_source}')\n", + " ntrains = ofile['INDEX/trainId'].shape[0]\n", + " ofile.create_dataset(f'INDEX/{params_source}/count', shape=(ntrains,), dtype=np.uint64)\n", + " ofile.create_dataset(f'INDEX/{params_source}/first', shape=(ntrains,), dtype=np.uint64)\n", + " \n", + " # Add the new source to the list in METADATA\n", + " if 'dataSourceId' in ofile['METADATA']:\n", + " # Older file format\n", + " data_sources_grp = ofile['METADATA'] \n", + " else:\n", + " # Newer file format\n", + " data_sources_grp = ofile['METADATA/dataSources']\n", + " \n", + " def extend(dset, values):\n", + " dset.resize(dset.shape[0] + len(values), axis=0)\n", + " dset[-len(values):] = values\n", + " \n", + " extend(data_sources_grp['root'], ['CONTROL', 'INSTRUMENT'])\n", + " extend(data_sources_grp['deviceId'], [params_source, rois_source])\n", + " extend(data_sources_grp['dataSourceId'], [\n", + " f'CONTROL/{params_source}', f'INSTRUMENT/{rois_source}']\n", + " )\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -490,6 +555,7 @@ " dataset_path=f\"{data_path}/mask\",\n", " comp_threads=n_cpus,\n", " )\n", + " save_reduced_rois(ofile, data_corr, mask_corr, local_karabo_da)\n", "\n", " step_timer.done_step(f'Saving data time.')\n", "\n", @@ -811,7 +877,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.11" + "version": "3.8.10" } }, "nbformat": 4,