# Collect all cut files
cutcsvfiles = glob.glob(os.path.join(trialfolder, '*_cut*.csv'))
cutwavfiles = glob.glob(os.path.join(trialfolder, '*', '*_cut*.wav'), recursive=True)
cutwavfiles += glob.glob(os.path.join(trialfolder, '*_cut*.wav'), recursive=True)
cutdenoisedfiles = glob.glob(os.path.join(trialfolder, '*', '*_cut*denoised*.wav'), recursive=True)
cutdenoisedfiles += glob.glob(os.path.join(trialfolder, '*_cut*denoised*.csv'), recursive=True)
cutavifiles = glob.glob(os.path.join(trialfolder, '*_cut*.avi'))
# Summarize
cutfilesall = cutcsvfiles + cutwavfiles + cutdenoisedfiles + cutavifiles
cutfilesall = list(set(cutfilesall))
# Collect original files
origfilesall = glob.glob(os.path.join(trialfolder, '*.csv'))
origfilesall += glob.glob(os.path.join(trialfolder, '*', '*.wav'), recursive=True)
origfilesall += glob.glob(os.path.join(trialfolder, '*.wav'))
origfilesall += glob.glob(os.path.join(trialfolder, '*', '*denoised*.wav'), recursive=True)
origfilesall += glob.glob(os.path.join(trialfolder, '*denoised*.wav'))
origfilesall += glob.glob(os.path.join(trialfolder, '*.avi'))
origfilesall = [f for f in origfilesall if 'cut' not in os.path.basename(f)]
# Summarize
origfilesall = list(set(origfilesall))
# Create orig folder if it doesn't exist
orig_folder = os.path.join(trialfolder, 'orig_todel')
os.makedirs(orig_folder, exist_ok=True)
# Collect trial_ids
trials_id = []
for f in cutfilesall:
base = os.path.basename(f)
parts = base.split('_')
if 'rein' in parts:
trial_id = '_'.join(parts[:5])
else:
trial_id = '_'.join(parts[:4])
trials_id.append(trial_id)
trials_id = [t + '_' for t in trials_id]
trials_id = list(set(trials_id))
print(len(trials_id), "unique trials")
missingcuts = []
missingorigs = []
for trial in trials_id:
print(f"Processing trial {trial}")
sessionid = trial.split('_')[0] + '_' + trial.split('_')[1] + '_'
move = True
# Find all cut files
related_files = [f for f in cutfilesall if os.path.basename(f).startswith(trial)]
if sessionid != '16_2_':
if len(related_files) != 7:
print(f"Warning: Trial {trial} does not have exactly 7 cut files, found {len(related_files)}")
move = False
missingcuts.append((trial, related_files))
continue
else:
if len(related_files) != 6:
print(f"Warning: Trial {trial} does not have exactly 6 cut files, found {len(related_files)}")
move = False
missingcuts.append((trial, related_files))
continue
# Find now the original files per each file, which is the same name just without _cutX
for file in related_files:
base = os.path.basename(file)
if 'Mic' in base and base.endswith('.csv') :
mic_file = file
mic_origfile = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'Mic' in os.path.basename(f) and f.endswith('.csv')][0]
print(mic_origfile)
elif 'WebcamFrameStream' in base:
webcam_file = file
webcam_origfile = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'WebcamFrameStream' in os.path.basename(f)][0]
print(webcam_origfile)
elif 'BalanceBoard' in base:
bb_file = file
bb_origfile = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'BalanceBoard' in os.path.basename(f)][0]
print(bb_origfile)
elif 'video_raw.avi' in base:
video_file = file
video_origfile = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'video_raw.avi' in os.path.basename(f)][0]
print(video_origfile)
elif 'denoised' in base and base.endswith('.wav') and 'srate16000' in base:
denoised_file_16 = file
denoised_origfile = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'denoised' in os.path.basename(f) and 'srate16000' in os.path.basename(f)][0]
print(denoised_origfile)
elif base.endswith('.wav') and 'denoised' not in base and 'srate16000' in base:
raw_file_16 = file
raw_origfile_16 = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'srate16000' in os.path.basename(f) and f.endswith('.wav') and 'denoised' not in os.path.basename(f)][0]
print(raw_origfile_16)
elif 'srate48000' in base and base.endswith('.wav'):
raw_file_48 = file
raw_origfile_48 = [f for f in origfilesall if os.path.basename(f).startswith(trial) and 'srate48000' in os.path.basename(f) and f.endswith('.wav')][0]
print(raw_origfile_48)
# Check if all cutfiles are in
if any(v is None for v in [mic_file, webcam_file, bb_file, video_file, denoised_file_16, raw_file_16, raw_file_48]):
if sessionid != '16_2_':
print(f"Warning: Trial {trial} is missing one or more cut files")
move = False
missingcuts.append(trial)
continue
# Check if any of the variables is none
if any(v is None for v in [mic_origfile, webcam_origfile, bb_origfile, video_origfile, denoised_origfile, raw_origfile_16, raw_origfile_48]):
if sessionid != '16_2_':
print(f"Warning: Trial {trial} is missing one or more original files")
move = False
missingorigs.append(trial)
continue
if move:
# Move original files to orig folder
if sessionid != '16_2_':
for orig in [mic_origfile, webcam_origfile, bb_origfile, video_origfile, denoised_origfile, raw_origfile_16, raw_origfile_48]:
newloc = os.path.join(orig_folder, os.path.basename(orig))
os.rename(orig, newloc)
print(f"Moved original file {orig} to {newloc}")
else:
for orig in [mic_origfile, webcam_origfile, bb_origfile, video_origfile, denoised_origfile, raw_origfile_16]:
newloc = os.path.join(orig_folder, os.path.basename(orig))
os.rename(orig, newloc)
print(f"Moved original file {orig} to {newloc}")
print(len(missingcuts))
print(missingcuts)
##### Aftermath ####
# Get the trial ids from missing cuts
missingcuts_ids = [t[0] for t in missingcuts]
missingcuts_sessions = [t.split('_')[0] + '_' + t.split('_')[1] for t in missingcuts_ids]
missingcuts_sessions = list(set(missingcuts_sessions))
print(len(missingcuts_sessions), "unique sessions with missing cuts")
print(missingcuts_sessions)