From ffd2f4a1bd0d1dc4d211241da6949948053f64f7 Mon Sep 17 00:00:00 2001 From: ahmedk <karim.ahmed@xfel.eu> Date: Wed, 31 Jan 2024 11:44:41 +0100 Subject: [PATCH] unite the raw_data_location string --- notebooks/LPD/LPDChar_Darks_NBC.ipynb | 3 ++- .../LPDMini/LPD_Mini_Char_Darks_NBC.ipynb | 3 ++- src/cal_tools/tools.py | 21 +++++++++++++++++++ tests/test_cal_tools.py | 14 +++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/notebooks/LPD/LPDChar_Darks_NBC.ipynb b/notebooks/LPD/LPDChar_Darks_NBC.ipynb index 0248b247b..37e658536 100644 --- a/notebooks/LPD/LPDChar_Darks_NBC.ipynb +++ b/notebooks/LPD/LPDChar_Darks_NBC.ipynb @@ -118,6 +118,7 @@ " map_gain_stages,\n", " module_index_to_qm,\n", " parse_runs,\n", + " raw_data_location_string,\n", " reorder_axes,\n", " run_prop_seq_from_path,\n", " save_const_to_h5,\n", @@ -343,7 +344,7 @@ "source": [ "# Read report path and create file location tuple to add with the injection\n", "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n", - "file_loc = 'proposal:{} runs:{}'.format(proposal, \", \".join(map(str, run_nums))) \n", + "file_loc = raw_data_location_string(proposal, run_nums)\n", "\n", "report = get_report(metadata_folder)" ] diff --git a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb index c226964be..f3ccaa445 100644 --- a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb +++ b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb @@ -98,6 +98,7 @@ " calcat_creation_time,\n", " get_from_db,\n", " get_report,\n", + " raw_data_location_string,\n", " run_prop_seq_from_path,\n", " save_const_to_h5,\n", " send_to_db,\n", @@ -341,7 +342,7 @@ "source": [ "# Read report path and create file location tuple to add with the injection\n", "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n", - "file_loc = 'proposal:{} runs:{}'.format(proposal, \", \".join(map(str, run_nums)))\n", + "file_loc = raw_data_location_string(proposal, run_nums)\n", "\n", "report = get_report(metadata_folder)" ] diff --git a/src/cal_tools/tools.py b/src/cal_tools/tools.py index 88ab28d1e..d9e5e5f9e 100644 --- a/src/cal_tools/tools.py +++ b/src/cal_tools/tools.py @@ -1044,3 +1044,24 @@ def reorder_axes(a, from_order, to_order): from_order = list(from_order) order = tuple([from_order.index(lbl) for lbl in to_order]) return a.transpose(order) + + +def raw_data_location_string(proposal: str, runs: List[int]): + """Create RAW data location string to inject as a + metadata along with the calibration parameters. + + Args: + proposal (string): The proposal number including the preceding `p`. + e.g. p900203 + runs (list): A list of the run numbers + + Returns: + str: The string for raw data_location. + e.g. `proposal p900203 runs: 9008, 9009, 90010` + """ + if not isinstance(proposal, str) or proposal[0] != "p": + raise ValueError( + "Invalid proposal format. The proposal should be a string with" + " a preceding 'p'. Example: 'p900203'") + + return f"proposal:{proposal} runs:{' '.join(map(str, runs))}" diff --git a/tests/test_cal_tools.py b/tests/test_cal_tools.py index 3eb962b0e..577a82a4f 100644 --- a/tests/test_cal_tools.py +++ b/tests/test_cal_tools.py @@ -18,6 +18,7 @@ from cal_tools.tools import ( get_pdu_from_db, map_seq_files, module_index_to_qm, + raw_data_location_string, recursive_update, send_to_db, write_constants_fragment, @@ -567,3 +568,16 @@ def test_reorder_axes(): to_order = ('gain', 'fast_scan', 'slow_scan', 'cells') assert reorder_axes(a, from_order, to_order).shape == (3, 256, 32, 10) + + +def test_raw_data_location_string(): + raw_data_loc = raw_data_location_string("p900203", [9008, 9009, 9010]) + assert raw_data_loc == "proposal:p900203 runs:9008 9009 9010" + + +def test_raise_raw_data_location_string(): + with pytest.raises(ValueError): + raw_data_location_string(900203, [9008, 9009, 9010]) + + with pytest.raises(ValueError): + raw_data_location_string("900203", [9008, 9009, 9010]) -- GitLab