From 5b5bb5fbccf4bd3c511f16c987789d0730406b1b Mon Sep 17 00:00:00 2001 From: pablomc88 Date: Thu, 28 Nov 2024 11:22:32 +0100 Subject: [PATCH] Fix bug when saving features to a file. --- .../Hagen_model/simulation/compute_features.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/Hagen_model/simulation/compute_features.py b/examples/Hagen_model/simulation/compute_features.py index 19ec9e1..3b79dc6 100644 --- a/examples/Hagen_model/simulation/compute_features.py +++ b/examples/Hagen_model/simulation/compute_features.py @@ -52,7 +52,13 @@ # Split CDM data into 10 chunks when computing EEGs to avoid memory issues if compute_EEG: - all_CDM_data = np.array_split(CDM_data,10) + # Check if the features have already been computed for this file + if os.path.isfile(os.path.join(features_path, method, 'tmp', + 'sim_X_'+file.split('_')[-1]+'_0')) == False: + all_CDM_data = np.array_split(CDM_data, 10) + else: + print(f'Features have already been computed for CDM data {file.split("_")[-1]}.') + continue else: all_CDM_data = [CDM_data] @@ -61,7 +67,7 @@ print(f'Computing features for CDM data chunk {ii+1}/{len(all_CDM_data)}') # Computation of EEGs if compute_EEG: - # Check if the features have already been computed + # Check if the features have already been computed for this chunk if os.path.isfile(os.path.join(features_path, method, 'tmp', 'all_features_' + file.split('_')[-1] + '_' + str(ii))) == False: print(f'Computing EEGs for CDM data chunk {ii+1}/{len(all_CDM_data)}') @@ -144,13 +150,16 @@ 'all_features_' + file.split('_')[-1] + '_' + str(ii))) # Save the features to a file + print('\nSaving EEG features to files.') for i in range(20): elec_data = [] for j in range(len(all_features)): elec_data.append(all_features[j][i]) - pickle.dump(np.array(elec_data),open(os.path.join(features_path, method, 'tmp', - 'sim_X_'+file.split('_')[-1]+'_'+str(i)), 'wb')) + pickle.dump(np.array(elec_data),open(os.path.join(features_path, method, 'tmp', + 'sim_X_'+file.split('_')[-1]+'_'+str(i)), 'wb')) + # Kill the process + os._exit(0) else: df = all_features[0]