Commit 8d27cb55 authored by Andrei-Claudiu Roibu's avatar Andrei-Claudiu Roibu 🖥
Browse files

fixed bugs + deleting existing data files to prevent overwrite

parent 5e6bb054
......@@ -38,8 +38,9 @@ def directory_reader(folder_location, subject_number=None, write_txt=False):
Returns:
subDirectoryList (list): A list of strings containing the available sub-directories. This is also printed out as a .txt file
"""
if write_txt == True:
out_file = open("files.txt", 'w')
out_file = open("files.txt", 'w')
subDirectoryList = []
number_of_subjects = 0
......@@ -48,6 +49,7 @@ def directory_reader(folder_location, subject_number=None, write_txt=False):
subject_number = len(os.listdir(os.path.join(
os.path.expanduser("~"), folder_location)))
for directory in os.listdir(folder_location):
if number_of_subjects < subject_number:
if os.path.isdir(os.path.join(folder_location, directory)) and os.path.exists(os.path.join(folder_location, directory, "dMRI/autoptx_preproc/")) and os.path.exists(os.path.join(folder_location, directory, "fMRI/rfMRI_25.dr/")):
......@@ -119,24 +121,37 @@ def data_test_train_validation_split(data_folder_name, test_percentage, subject_
train_data, test = train_test_split(
subDirectoryList, test_size=test_percentage/100, random_state=42, shuffle=True)
if os.path.exists(os.path.join(data_folder_name, 'test.txt')):
os.remove(os.path.join(data_folder_name, 'test.txt'))
np.savetxt(os.path.join(data_folder_name, 'test.txt'), test, fmt='%s')
if K_fold is None:
train, validation = train_test_split(
train_data, test_size=int(len(test)), random_state=42, shuffle=True)
if os.path.exists(os.path.join(data_folder_name, 'train.txt')):
os.remove(os.path.join(data_folder_name,'train.txt'))
np.savetxt(os.path.join(data_folder_name,
'train.txt'), train, fmt='%s')
if os.path.exists(os.path.join(data_folder_name, 'validation.txt')):
os.remove(os.path.join(data_folder_name,'validation.txt'))
np.savetxt(os.path.join(data_folder_name, 'validation.txt'),
validation, fmt='%s')
validation, fmt='%s')
else:
k_fold = KFold(n_splits=K_fold)
k = 0
for train_index, validation_index in k_fold.split(train_data):
train, validation = train_data[train_index], train_data[validation_index]
if os.path.exists(os.path.join(data_folder_name, 'train' + str(k+1)+ '.txt')):
os.remove(os.path.join(data_folder_name,'train' + str(k+1)+ '.txt'))
np.savetxt(os.path.join(data_folder_name, 'train' +
str(k+1)+'.txt'), train, fmt='%s')
if os.path.exists(os.path.join(data_folder_name, 'validation' + str(k+1)+ '.txt')):
os.remove(os.path.join(data_folder_name,'validation' + str(k+1)+ '.txt'))
np.savetxt(os.path.join(data_folder_name, 'validation' +
str(k+1)+'.txt'), validation, fmt='%s')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment