From 68bac05c095e58ed8e73ea2c448207af15b981cf Mon Sep 17 00:00:00 2001
From: Danilo Ferreira de Lima <danilo.enoque.ferreira.de.lima@xfel.de>
Date: Mon, 22 Jan 2024 18:36:52 +0100
Subject: [PATCH] Try-catch on resolution fit to fallback to FWHM.

---
 notebook/Effect of pre-smoothing.ipynb |  2 +-
 notebook/Toy resolution.ipynb          |  2 +-
 pes_to_spec/model.py                   | 14 +++++++++-----
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/notebook/Effect of pre-smoothing.ipynb b/notebook/Effect of pre-smoothing.ipynb
index a7ad7ad..bf4e1ad 100644
--- a/notebook/Effect of pre-smoothing.ipynb	
+++ b/notebook/Effect of pre-smoothing.ipynb	
@@ -3746,7 +3746,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.9.16"
+   "version": "3.10.13"
   },
   "widgets": {
    "application/vnd.jupyter.widget-state+json": {
diff --git a/notebook/Toy resolution.ipynb b/notebook/Toy resolution.ipynb
index 63cea2b..1c04c60 100644
--- a/notebook/Toy resolution.ipynb	
+++ b/notebook/Toy resolution.ipynb	
@@ -2680,7 +2680,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.9.16"
+   "version": "3.10.13"
   },
   "widgets": {
    "application/vnd.jupyter.widget-state+json": {
diff --git a/pes_to_spec/model.py b/pes_to_spec/model.py
index 88f3d78..f287409 100644
--- a/pes_to_spec/model.py
+++ b/pes_to_spec/model.py
@@ -263,8 +263,13 @@ def get_resolution(y: np.ndarray, y_hat: np.ndarray, e: np.ndarray,
     results["e_axis"] = e_axis
     results["fwhm"] = fwhm(e_axis, results["h"])
     results["std"] = np.sqrt(np.sum(results["h"]*e_axis**2)/np.sum(results["h"]))
-    results["fit"] = fit_gaussian(e_axis, results["h"])
-    results["fit_success"] = results["fit"].covar is not None
+    results["fit"] = None
+    results["fit_success"] = False
+    try:
+        results["fit"] = fit_gaussian(e_axis, results["h"])
+        results["fit_success"] = results["fit"].covar is not None
+    except ValueError:
+        pass
     return results
 
 class PromptNotFoundError(Exception):
@@ -1167,7 +1172,6 @@ class Model(TransformerMixin, BaseEstimator):
         # get average resolution
         result = get_resolution(y, y_hat, e)
 
-        #self.resolution = np.exp(result["fit"].best_values["log_sigma"])*2.355 if result["fit_success"] else -1.0
         self.resolution = result["fwhm"]
         self.snr = result["snr"]
         print(f"Resolution = {self.resolution:.2f} eV, S/R = {self.snr:.2f}")
@@ -1218,8 +1222,8 @@ class Model(TransformerMixin, BaseEstimator):
                 width += [np.exp(result["fit"].best_values["log_sigma"])*2.355]
                 width_unc += [np.sqrt(result["fit"].covar[2,2])*2.355]
             else:
-                width += [np.nan]
-                width_unc += [np.nan]
+                width += [result["fwhm"]]
+                width_unc += [0]
         width = np.array(width)
         width_unc = np.array(width_unc)
         self.resolution_per_energy = width
-- 
GitLab