From f857edc66fddd6f206fbc780b00885a948f1a653 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Wed, 27 Nov 2024 12:03:29 +0000
Subject: [PATCH] Check for non-reproducible parameters in xfel-calibrate

Allow explicitly passing default values for non-reproducible parameters
---
 notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb |  1 +
 src/xfel_calibrate/nb_args.py                  | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 1eabbfbda..4feebe7a4 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -1631,6 +1631,7 @@
   }
  ],
  "metadata": {
+  "euxfel_not_reproducible_params": ["slopes_ff_from_path"],
   "kernelspec": {
    "display_name": "Python 3",
    "language": "python",
diff --git a/src/xfel_calibrate/nb_args.py b/src/xfel_calibrate/nb_args.py
index 45833dccf..98fe0feb1 100644
--- a/src/xfel_calibrate/nb_args.py
+++ b/src/xfel_calibrate/nb_args.py
@@ -469,6 +469,7 @@ def parse_argv_and_load_nb(argv) -> Tuple[Dict, NBDetails]:
         extend_params(nb, ext_func, argv)
 
     default_params = extract_parameters(nb, lang='python')
+    default_values = {p.name: p.value for p in default_params}
 
     parser = make_initial_parser()
     parser.description = make_epilog(nb)
@@ -476,6 +477,19 @@ def parse_argv_and_load_nb(argv) -> Tuple[Dict, NBDetails]:
 
     arg_dict = deconsolize_args(vars(parser.parse_args(argv[1:])))
 
+    not_reproducible_params = [
+        p for p in nb.metadata.get("euxfel_not_reproducible_params", [])
+        if p in arg_dict and arg_dict[p] != default_values[p]
+    ]
+    if not_reproducible_params:
+        print(f"WARNING: you have specified parameters ({', '.join(not_reproducible_params)}) "
+              f"which may interfere with reproducibility, potentially making it "
+              f"impossible to reconstruct the resulting data in the future.")
+        if not args.not_reproducible:
+            sys.exit("To continue anyway, re-run the command with the "
+                     "--not-reproducible flag.")
+
+
     user_venv = nb_info.get("user", {}).get("venv")
     if user_venv is not None:
         user_venv = Path(user_venv.format(**arg_dict))
-- 
GitLab