Skip to content

Commit

Permalink
1c PM - alpha correction
Browse files Browse the repository at this point in the history
  • Loading branch information
qnater committed Jan 8, 2025
1 parent b8792a2 commit dbe9131
Show file tree
Hide file tree
Showing 32 changed files with 243 additions and 93 deletions.
64 changes: 29 additions & 35 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified imputegap/recovery/__pycache__/benchmark.cpython-312.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions imputegap/recovery/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def generate_heatmap(self, scores_list, algos, sets, save_dir="./reports", displ
plt.savefig(filepath, dpi=300, bbox_inches='tight') # Save in HD with tight layout

# Show the plot
if display :
if display:
plt.tight_layout()
plt.show()
plt.close()
Expand Down Expand Up @@ -692,6 +692,6 @@ def eval(self, algorithms=["cdrec"], datasets=["eeg-alcohol"], patterns=["mcar"]
print("============================================================================\n\n\n\n\n\n")

scores_list, algos, sets = self.avg_results(*run_storage)
_ = Benchmark().generate_heatmap(scores_list, algos, sets, save_dir=save_dir)
_ = Benchmark().generate_heatmap(scores_list, algos, sets, save_dir=save_dir, display=False)

return run_storage, scores_list
46 changes: 23 additions & 23 deletions imputegap/recovery/explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,17 @@ def print(shap_values, shap_details=None):
None
"""

if shap_details is not None:
print("\n\nx_data (with", len(shap_details), "elements) : ")
for i, (input, _) in enumerate(shap_details):
print("\tFEATURES VALUES", i, "(", len(input), ") : ", *input)

print("\ny_data (with", len(shap_details), "elements) : ")

for i, (_, output) in enumerate(shap_details):
print(f"\tRMSE SERIES {i:<5} : {output:<15}")
#if shap_details is not None:
#print("\n\nx_data (with", len(shap_details), "elements) : ")
#for i, (input, _) in enumerate(shap_details):
# print("\tFEATURES VALUES", i, "(", len(input), ") : ", *input)
#print("\ny_data (with", len(shap_details), "elements) : ")
#for i, (_, output) in enumerate(shap_details):
# print(f"\tRMSE SERIES {i:<5} : {output:<15}")

print("\n\nSHAP Results details : ")
for (x, algo, rate, description, feature, category, mean_features) in shap_values:
print(
f"\tFeature : {x:<5} {algo:<10} with a score of {rate:<10} {category:<18} {description:<75} {feature}\n")
print(f"\tFeature : {x:<5} {algo:<10} with a score of {rate:<10} {category:<18} {description:<75} {feature}\n")

def convert_results(tmp, file, algo, descriptions, features, categories, mean_features, to_save):
"""
Expand Down Expand Up @@ -243,14 +240,14 @@ def execute_shap_model(x_dataset, x_information, y_dataset, file, algorithm, spl
Results of the SHAP explainer model.
"""

print("\n\nInitilization of the SHAP model with ", np.array(x_information).shape)
print("\n\nInitialization of the SHAP model with dimension", np.array(x_information).shape)

path_file = "./assets/shap/"
if not os.path.exists(path_file):
path_file = "./imputegap" + path_file[1:]

x_features, x_categories, x_descriptions = [], [], []
x_fs, x_cs, x_ds = [], [], []
x_fs, x_cs, x_ds, alphas = [], [], [], []

for current_time_series in x_information:
x_fs.clear()
Expand Down Expand Up @@ -310,28 +307,28 @@ def execute_shap_model(x_dataset, x_information, y_dataset, file, algorithm, spl
os.makedirs(path_file, exist_ok=True)
plt.savefig(alpha)
plt.close()
print("\n\n\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.summary_plot(np.array(shval).T, np.array(x_test).T, feature_names=series_names, show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_shap_reverse_plot.png")
plt.title("SHAP Features by Series")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.plots.waterfall(shval_x[0], show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_DTL_Waterfall.png")
plt.title("SHAP Waterfall Results")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.plots.beeswarm(shval_x, show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_DTL_Beeswarm.png")
plt.title("SHAP Beeswarm Results")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

total_weights_for_all_algorithms = []

Expand Down Expand Up @@ -388,31 +385,31 @@ def execute_shap_model(x_dataset, x_information, y_dataset, file, algorithm, spl
plt.title("SHAP details of geometry")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.summary_plot(np.array(transformation).T, np.array(transformationT).T, plot_size=(20, 10),
feature_names=transformationDesc, show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_shap_transformation_plot.png")
plt.title("SHAP details of transformation")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.summary_plot(np.array(correlation).T, np.array(correlationT).T, plot_size=(20, 10),
feature_names=correlationDesc, show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_shap_correlation_plot.png")
plt.title("SHAP details of correlation")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.summary_plot(np.array(trend).T, np.array(trendT).T, plot_size=(20, 8), feature_names=trendDesc,
show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_shap_trend_plot.png")
plt.title("SHAP details of Trend")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

aggregation_features.append(np.mean(geometry, axis=0))
aggregation_features.append(np.mean(correlation, axis=0))
Expand All @@ -434,15 +431,15 @@ def execute_shap_model(x_dataset, x_information, y_dataset, file, algorithm, spl
plt.gca().axes.get_xaxis().set_visible(False)
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha)
alphas.append(alpha)

shap.summary_plot(np.array(aggregation_features).T, np.array(aggregation_test).T, feature_names=series_names,
show=display)
alpha = os.path.join(path_file + file + "_" + algorithm + "_shap_aggregate_reverse_plot.png")
plt.title("SHAP Aggregation Features by Series")
plt.savefig(alpha)
plt.close()
print("\t\t\tGRAPH has benn computed : ", alpha, "\n\n")
alphas.append(alpha)

if verbose:
print("\t\tSHAP Families details :")
Expand All @@ -461,6 +458,9 @@ def execute_shap_model(x_dataset, x_information, y_dataset, file, algorithm, spl

total_weights_for_all_algorithms = np.append(total_weights_for_all_algorithms, total_weights_percent)

for alpha in alphas:
print("\n\n\t\t\tplot has been saved : ", alpha)

results_shap = Explainer.convert_results(total_weights_for_all_algorithms, file, algorithm, x_descriptions,
x_features, x_categories, mean_features,
to_save=path_file + file + "_" + algorithm)
Expand Down
6 changes: 3 additions & 3 deletions params/optimal_parameters_e_eeg-alcohol_cdrec.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[cdrec]
rank = 9
epsilon = 0.0010055574182674504
iteration = 489
rank = 2
epsilon = 2.9103006766169248e-5
iteration = 976
6 changes: 3 additions & 3 deletions params/optimal_parameters_e_eeg-alcohol_stmvl.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[stmvl]
window_size = 70
gamma = 1.0621347452712664e-6
alpha = 2
window_size = 40
gamma = 0.22504996184885026
alpha = 6
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ numpy==1.26.4
matplotlib==3.7.5
toml==0.10.2
scikit-learn==1.3.2
scipy==1.14.1
#scipy==1.14.1
setuptools==75.1.0
tensorflow==2.17.0
shap==0.44.1
Expand All @@ -11,6 +11,11 @@ scikit-optimize==0.10.2
pyswarms==1.3.0
pytest==8.3.3
xlsxwriter==3.2.0

# Specify scipy versions conditionally
scipy==1.10.1; python_version < "3.10"
scipy==1.14.1; python_version >= "3.10"

types-toml
types-setuptools
wheel
Binary file modified tests/__pycache__/test_benchmarking.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_explainer.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file modified tests/assets/shap/chlorine_cdrec_DTL_Beeswarm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_DTL_Waterfall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions tests/assets/shap/chlorine_cdrec_results.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
Feature : 6 cdrec with a score of 22.22 Geometry Proportion of high incremental changes in the series MD_hrv_classic_pnn40
Feature : 15 cdrec with a score of 10.24 Transformation Power in the lowest 20% of frequencies SP_Summaries_welch_rect_area_5_1
Feature : 10 cdrec with a score of 9.27 Geometry Goodness of exponential fit to embedding distance distribution CO_Embed2_Dist_tau_d_expfit_meandiff
Feature : 0 cdrec with a score of 9.07 Geometry 5-bin histogram mode DN_HistogramMode_5
Feature : 5 cdrec with a score of 8.31 Correlation Time reversibility CO_trev_1_num
Feature : 21 cdrec with a score of 7.84 Trend Error of 3-point rolling mean forecast FC_LocalSimple_mean3_stderr
Feature : 2 cdrec with a score of 7.65 Correlation First 1/e crossing of the ACF CO_f1ecac
Feature : 14 cdrec with a score of 6.67 Geometry Negative outlier timing DN_OutlierInclude_n_001_mdrmd
Feature : 1 cdrec with a score of 5.0 Geometry 10-bin histogram mode DN_HistogramMode_10
Feature : 8 cdrec with a score of 3.75 Geometry Transition matrix column variance SB_TransitionMatrix_3ac_sumdiagcov
Feature : 4 cdrec with a score of 3.11 Correlation Histogram-based automutual information (lag 2, 5 bins) CO_HistogramAMI_even_2_5
Feature : 17 cdrec with a score of 2.06 Trend Entropy of successive pairs in symbolized series SB_MotifThree_quantile_hh
Feature : 3 cdrec with a score of 1.63 Correlation First minimum of the ACF CO_FirstMin_ac
Feature : 12 cdrec with a score of 1.58 Correlation Change in autocorrelation timescale after incremental differencing FC_LocalSimple_mean1_tauresrat
Feature : 13 cdrec with a score of 1.55 Geometry Positive outlier timing DN_OutlierInclude_p_001_mdrmd
Feature : 9 cdrec with a score of 0.04 Trend Wangs periodicity metric PD_PeriodicityWang_th0_01
Feature : 1 cdrec with a score of 90.54 Geometry 10-bin histogram mode DN_HistogramMode_10
Feature : 12 cdrec with a score of 3.99 Correlation Change in autocorrelation timescale after incremental differencing FC_LocalSimple_mean1_tauresrat
Feature : 5 cdrec with a score of 3.83 Correlation Time reversibility CO_trev_1_num
Feature : 18 cdrec with a score of 0.57 Geometry Rescaled range fluctuation analysis (low-scale scaling) SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
Feature : 13 cdrec with a score of 0.37 Geometry Positive outlier timing DN_OutlierInclude_p_001_mdrmd
Feature : 3 cdrec with a score of 0.33 Correlation First minimum of the ACF CO_FirstMin_ac
Feature : 14 cdrec with a score of 0.29 Geometry Negative outlier timing DN_OutlierInclude_n_001_mdrmd
Feature : 6 cdrec with a score of 0.09 Geometry Proportion of high incremental changes in the series MD_hrv_classic_pnn40
Feature : 0 cdrec with a score of 0.0 Geometry 5-bin histogram mode DN_HistogramMode_5
Feature : 2 cdrec with a score of 0.0 Correlation First 1/e crossing of the ACF CO_f1ecac
Feature : 4 cdrec with a score of 0.0 Correlation Histogram-based automutual information (lag 2, 5 bins) CO_HistogramAMI_even_2_5
Feature : 7 cdrec with a score of 0.0 Geometry Longest stretch of above-mean values SB_BinaryStats_mean_longstretch1
Feature : 8 cdrec with a score of 0.0 Geometry Transition matrix column variance SB_TransitionMatrix_3ac_sumdiagcov
Feature : 9 cdrec with a score of 0.0 Trend Wangs periodicity metric PD_PeriodicityWang_th0_01
Feature : 10 cdrec with a score of 0.0 Geometry Goodness of exponential fit to embedding distance distribution CO_Embed2_Dist_tau_d_expfit_meandiff
Feature : 11 cdrec with a score of 0.0 Correlation First minimum of the AMI function IN_AutoMutualInfoStats_40_gaussian_fmmi
Feature : 15 cdrec with a score of 0.0 Transformation Power in the lowest 20% of frequencies SP_Summaries_welch_rect_area_5_1
Feature : 16 cdrec with a score of 0.0 Geometry Longest stretch of decreasing values SB_BinaryStats_diff_longstretch0
Feature : 18 cdrec with a score of 0.0 Geometry Rescaled range fluctuation analysis (low-scale scaling) SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
Feature : 17 cdrec with a score of 0.0 Trend Entropy of successive pairs in symbolized series SB_MotifThree_quantile_hh
Feature : 19 cdrec with a score of 0.0 Geometry Detrended fluctuation analysis (low-scale scaling) SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
Feature : 20 cdrec with a score of 0.0 Transformation Centroid frequency SP_Summaries_welch_rect_centroid
Feature : 21 cdrec with a score of 0.0 Trend Error of 3-point rolling mean forecast FC_LocalSimple_mean3_stderr
Binary file modified tests/assets/shap/chlorine_cdrec_shap_aggregate_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_correlation_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_geometry_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_reverse_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_transformation_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/assets/shap/chlorine_cdrec_shap_trend_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reports/benchmarking_rmse.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reports/run_0/eegalcohol_mcar_MAE.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reports/run_0/eegalcohol_mcar_MI.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reports/run_0/eegalcohol_mcar_RMSE.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dbe9131

Please sign in to comment.