diff --git a/da_test_thread.py b/da_test_thread.py index 2769bd8..86ff73b 100644 --- a/da_test_thread.py +++ b/da_test_thread.py @@ -39,16 +39,15 @@ class DaTestThread(BaseThread): Thread for running server side matrix test operations ''' def __init__(self, sessionFilepath=None, - stimFolder='./da_stim/', - noiseFilepath="./da_stim/noise/wav/noise/noise_norm.wav", - noiseRMSFilepath="./da_stim/noise/rms/noise_rms.npy", - red_coef="./calibration/out/reduction_coefficients/da_red_coef.npy", - cal_coef="./calibration/out/calibration_coefficients/da_cal_coef.npy", + stimFolder='./tone_stim/', + noiseFilepath="./tone_stim/noise/wav/noise/noise.wav", + noiseRMSFilepath="./tone_stim/noise/rms/noise_rms.npy", + red_coef="./calibration/out/reduction_coefficients/tone_red_coef.npy", + cal_coef="./calibration/out/calibration_coefficients/tone_cal_coef.npy", nTrials=2, socketio=None, participant=None, srt_50=None, s_50=None): self.reduction_coef = np.load(red_coef)*np.load(cal_coef) - self.wav_file = os.path.join(stimFolder, '3000_da.wav') self.noise_path = noiseFilepath self.noise_rms = np.load(noiseRMSFilepath) self.stim_folder = stimFolder @@ -66,7 +65,7 @@ class DaTestThread(BaseThread): socketio=socketio, participant=participant) - self.toSave = ['stim_paths', 'trial_ind', 'nTrials', 'wav_file', 'test_name', 'si'] + self.toSave = ['stim_paths', 'trial_ind', 'nTrials', 'test_name', 'si'] self.socketio.on_event('finalise_results', self.finaliseResults, namespace='/main') @@ -115,6 +114,7 @@ class DaTestThread(BaseThread): #reduction_coef = float(np.load(os.path.join(self.listDir, 'reduction_coef.npy'))) # Calculate SNRs based on behavioural measures + ''' s_50 *= 0.01 shuffle(self.si) x = logit(self.si * 0.01) @@ -128,36 +128,41 @@ class DaTestThread(BaseThread): self.snr_fs[self.snr_fs == np.inf] = 0. if (self.snr_fs == -np.inf).any(): raise ValueError("Noise infinitely louder than signal for an SNR (SNRs: {})".format(self.snr_fs)) + ''' - - wavs = globDir(self.stim_folder, "3000_da.wav") * len(snrs) - rms_files = globDir(self.stim_folder, "overall_da_rms.npy") * len(snrs) - - self.socketio.emit('test_stim_load', namespace='/main') - # Add noise to audio files at set SNRs and write to participant - # directory + snrs = self.participant.data['parameters']['decoder_test_SNRs'] + srt_50 + stim_dirs = self.participant.data['parameters']['decoder_test_stim_dirs'] self.data_path = self.participant.data_paths[self.test_name] out_dir = os.path.join(self.data_path, "stimulus") delete_if_exists(out_dir) out_info = os.path.join(out_dir, "stim_info.csv") dir_must_exist(out_dir) - with open(out_info, 'w') as csvfile: - writer = csv.writer(csvfile) - writer.writerow(['wav', 'snr_fs', 'rms', 'si', 'snr']) - for wav, snr_fs, rms, si, snr in zip(wavs, self.snr_fs, rms_files, self.si, snrs): - fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr) - out_wavpath = os.path.join(out_dir, fp) - stim_rms = np.load(rms) - match_ratio = stim_rms/self.noise_rms - block_mix_wavs(wav, self.noise_path, out_wavpath, - 1.*self.reduction_coef, - snr_fs*match_ratio*self.reduction_coef, - mute_left=True) - self.stim_paths.extend([out_wavpath] * self.nTrials) - writer.writerow([wav, snr_fs, rms, si, snr]) - # TODO: Output SI/snrs of each file to a CSV file - #audio, fs, enc, fmt = sndio.read(wav, return_format=True) + for ind, dir_name in enumerate(stim_dirs): + stim_dir = os.path.join(self.listDir, dir_name) + wavs = globDir(stim_dir, "*.wav")[0] * len(snrs) + rms_files = globDir(stim_dir, "*.npy")[0] * len(snrs) + + self.socketio.emit('test_stim_load', namespace='/main') + # Add noise to audio files at set SNRs and write to participant + # directory + + with open(out_info, 'w') as csvfile: + writer = csv.writer(csvfile) + writer.writerow(['wav', 'snr_fs', 'rms', 'si', 'snr']) + for wav, snr_fs, rms, si, snr in zip(wavs, self.snr_fs, rms_files, self.si, snrs): + fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr) + out_wavpath = os.path.join(out_dir, fp) + stim_rms = np.load(rms) + match_ratio = stim_rms/self.noise_rms + block_mix_wavs(wav, self.noise_path, out_wavpath, + self.reduction_coef, + snr_fs*match_ratio*self.reduction_coef, + mute_left=True) + self.stim_paths.extend([out_wavpath] * self.nTrials) + writer.writerow([wav, snr_fs, rms, si, snr]) + # TODO: Output SI/snrs of each file to a CSV file + #audio, fs, enc, fmt = sndio.read(wav, return_format=True) def saveState(self, out="test_state.pkl"): diff --git a/gen_participants.py b/gen_participants.py index b078b58..3514ba3 100755 --- a/gen_participants.py +++ b/gen_participants.py @@ -43,7 +43,7 @@ def gen_participant_num(participants, N = 100): taken_nums = [] for part_key in participants.keys(): participant = participants[part_key] - taken_nums.append(int(participant['info']['number'][0])) + taken_nums.append(int(participant['info']['number'])) inds = np.arange(N)+1 taken_inds = np.in1d(inds, taken_nums) @@ -190,6 +190,7 @@ def main(): # Is the hearing loss simulator active for this participant? # Even numbers yes, odd numbers no hl_sim_active = (i-1 % 2) == 0 + participant_params['hl_sim_active'] = hl_sim_active # What order are the decoder stories presented? # What order are the behavioral test stimuli presented? # What order are the tone SNRs presented at? @@ -207,11 +208,12 @@ def main(): key = "participant_{}".format(i) - participants[key] = Participant(participant_dir="./participant_data/{}".format(key), parameters=participant_params) + print(f"Generating: {key}") + participants[key] = Participant(participant_dir="./participant_data/{}".format(key), number=i, parameters=participant_params) participants[key].save("info") + print(f"Generated {part_nums.size} new participant databases") - breakpoint() if __name__ == '__main__': main() diff --git a/route.py b/route.py index 0ff5f13..d23d909 100644 --- a/route.py +++ b/route.py @@ -19,6 +19,7 @@ import numpy as np import random from pysndfile import sndio from scipy.optimize import minimize +from natsort import natsorted from app import generate_matrix_stimulus from matrix_test.helper_modules.filesystem import globDir, organiseWavs, prepareOutDir @@ -99,7 +100,7 @@ Participant routing def manage_participant_page(): # Find all pre-existing participants participants = find_participants() - return render_template("manage_participants.html", part_keys=participants.keys()) + return render_template("manage_participants.html", part_keys=natsorted(participants.keys())) @server.route('/participant/create') def create_participant_page(): @@ -121,7 +122,7 @@ def create_participant_submit(): key = "participant_{}".format(data['number']) participants[key] = Participant(participant_dir="./participant_data/{}".format(key), **data) participants[key].save("info") - return render_template("manage_participants.html", part_keys = participants.keys()) + return render_template("manage_participants.html", part_keys = natsorted(participants.keys())) ''' EEG routing @@ -129,7 +130,7 @@ EEG routing @server.route('/eeg') def eeg_setup(): participants = find_participants() - return render_template("eeg_setup.html", part_keys=participants.keys()) + return render_template("eeg_setup.html", part_keys=natsorted(participants.keys())) @server.route('/eeg/test/run') def eeg_test_run(): @@ -185,7 +186,7 @@ Matrix behavioral test routing @server.route('/matrix_test') def matrix_test_setup(): participants = find_participants() - return render_template("matrix_test_setup.html", part_keys=participants.keys()) + return render_template("matrix_test_setup.html", part_keys=natsorted(participants.keys())) @server.route('/matrix_test/run') def run_matrix_test(): @@ -213,7 +214,7 @@ Click stimulus routing @server.route('/click/setup') def click_setup(): participants = find_participants() - return render_template("click_test_setup.html", part_keys=participants.keys()) + return render_template("click_test_setup.html", part_keys=natsorted(participants.keys())) @server.route('/click/clinician/run') def click_clinician_run(): @@ -238,7 +239,7 @@ def click_complete(): @server.route('/da/setup') def da_setup(): participants = find_participants() - return render_template("da_test_setup.html", part_keys=participants.keys()) + return render_template("da_test_setup.html", part_keys=natsorted(participants.keys())) @server.route('/da/clinician/run') def da_clinician_run(): diff --git a/socket_handlers.py b/socket_handlers.py index 06df7d7..0bd5c75 100644 --- a/socket_handlers.py +++ b/socket_handlers.py @@ -105,7 +105,7 @@ Participant socket handlers def manage_participant_delete(participant_str): shutil.rmtree(participants[participant_str].participant_dir) del participants[participant_str] - return render_template("manage_participants.html", part_keys=participants.keys()) + return render_template("manage_participants.html", part_keys=natsorted(participants.keys())) @socketio.on('update_participant_info', namespace='/main') def manage_participant_save(data): diff --git a/templates/matrix_test_setup.html b/templates/matrix_test_setup.html index 4e27e24..18ebf6c 100644 --- a/templates/matrix_test_setup.html +++ b/templates/matrix_test_setup.html @@ -11,17 +11,6 @@ {% endfor %} -