Skip to content
Snippets Groups Projects
Commit 35d0b3db authored by Danilo Ferreira de Lima's avatar Danilo Ferreira de Lima
Browse files

Corrected PCA fit.

parent 3eccafc4
No related branches found
No related tags found
1 merge request!5Check consistency per channel
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment