From 35d0b3db91fdd6613df2130a2cf960b02483111a Mon Sep 17 00:00:00 2001
From: Danilo Ferreira de Lima <danilo.enoque.ferreira.de.lima@xfel.de>
Date: Tue, 31 Jan 2023 17:49:28 +0100
Subject: [PATCH] Corrected PCA fit.

---
 pes_to_spec/model.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/pes_to_spec/model.py b/pes_to_spec/model.py
index d45e477..55bd17d 100644
--- a/pes_to_spec/model.py
+++ b/pes_to_spec/model.py
@@ -600,18 +600,23 @@ class Model(TransformerMixin, BaseEstimator):
 
         Returns: Smoothened high resolution spectrum.
         """
+        print("Fitting PCA on low-resolution data.")
         x_t = self.x_model.fit_transform(low_res_data)
+        print("Fitting PCA on high-resolution data.")
         y_t = self.y_model.fit_transform(high_res_data, smoothen__energy=high_res_photon_energy)
         #self.fit_model.set_params(fex__gamma=1.0/float(x_t.shape[0]))
+        print("Fitting model.")
         self.fit_model.fit(x_t, y_t)
 
         # calculate the effect of the PCA
+        print("Calculate PCA unc. on high-resolution data.")
         high_res = self.y_model['smoothen'].transform(high_res_data)
         high_pca = self.y_model['pca'].transform(high_res)
         high_pca_rec = self.y_model['pca'].inverse_transform(high_pca)
         high_pca_unc = np.sqrt(np.mean((high_res - high_pca_rec)**2, axis=0, keepdims=True))
         self.y_model['unc'].set_uncertainty(high_pca_unc)
 
+        print("Calculate PCA unc. on low-resolution data.")
         low_res = self.x_model['select'].transform(low_res_data)
         pca_model = self.x_model['pca']
         if 'fex' in self.x_model.named_steps:
@@ -622,13 +627,16 @@ class Model(TransformerMixin, BaseEstimator):
         self.x_model['unc'].set_uncertainty(low_pca_unc)
 
         # for consistency check per channel
+        print("Calculate PCA per channel on low-resolution data.")
         selection_model = self.x_model['select']
         low_res = selection_model.transform(low_res_data, keep_dictionary_structure=True)
         for channel in self.get_channels():
+            print(f"Calculate PCA on {channel}")
             low_pca = self.channel_pca_model[channel].named_steps["pca"].fit_transform(low_res[channel])
             low_pca_rec = self.channel_pca_model[channel].named_steps["pca"].inverse_transform(low_pca)
-            low_pca_unc =  np.mean(np.sqrt(np.mean((low_res - low_pca_rec)**2, axis=1, keepdims=True)), axis=0, keepdims=True)
+            low_pca_unc =  np.mean(np.sqrt(np.mean((low_res[channel] - low_pca_rec)**2, axis=1, keepdims=True)), axis=0, keepdims=True)
             self.channel_pca_model[channel]['unc'].set_uncertainty(low_pca_unc)
+        print("End of fit.")
 
         return high_res
 
-- 
GitLab