End of evening backup

This commit is contained in:
2019-12-08 19:51:20 +00:00
parent b8c4fb64ea
commit af4e77c8c7
5 changed files with 48 additions and 51 deletions
+35 -30
View File
@@ -39,16 +39,15 @@ class DaTestThread(BaseThread):
Thread for running server side matrix test operations Thread for running server side matrix test operations
''' '''
def __init__(self, sessionFilepath=None, def __init__(self, sessionFilepath=None,
stimFolder='./da_stim/', stimFolder='./tone_stim/',
noiseFilepath="./da_stim/noise/wav/noise/noise_norm.wav", noiseFilepath="./tone_stim/noise/wav/noise/noise.wav",
noiseRMSFilepath="./da_stim/noise/rms/noise_rms.npy", noiseRMSFilepath="./tone_stim/noise/rms/noise_rms.npy",
red_coef="./calibration/out/reduction_coefficients/da_red_coef.npy", red_coef="./calibration/out/reduction_coefficients/tone_red_coef.npy",
cal_coef="./calibration/out/calibration_coefficients/da_cal_coef.npy", cal_coef="./calibration/out/calibration_coefficients/tone_cal_coef.npy",
nTrials=2, socketio=None, participant=None, srt_50=None, nTrials=2, socketio=None, participant=None, srt_50=None,
s_50=None): s_50=None):
self.reduction_coef = np.load(red_coef)*np.load(cal_coef) 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_path = noiseFilepath
self.noise_rms = np.load(noiseRMSFilepath) self.noise_rms = np.load(noiseRMSFilepath)
self.stim_folder = stimFolder self.stim_folder = stimFolder
@@ -66,7 +65,7 @@ class DaTestThread(BaseThread):
socketio=socketio, socketio=socketio,
participant=participant) 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') 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'))) #reduction_coef = float(np.load(os.path.join(self.listDir, 'reduction_coef.npy')))
# Calculate SNRs based on behavioural measures # Calculate SNRs based on behavioural measures
'''
s_50 *= 0.01 s_50 *= 0.01
shuffle(self.si) shuffle(self.si)
x = logit(self.si * 0.01) x = logit(self.si * 0.01)
@@ -128,36 +128,41 @@ class DaTestThread(BaseThread):
self.snr_fs[self.snr_fs == np.inf] = 0. self.snr_fs[self.snr_fs == np.inf] = 0.
if (self.snr_fs == -np.inf).any(): if (self.snr_fs == -np.inf).any():
raise ValueError("Noise infinitely louder than signal for an SNR (SNRs: {})".format(self.snr_fs)) raise ValueError("Noise infinitely louder than signal for an SNR (SNRs: {})".format(self.snr_fs))
'''
snrs = self.participant.data['parameters']['decoder_test_SNRs'] + srt_50
wavs = globDir(self.stim_folder, "3000_da.wav") * len(snrs) stim_dirs = self.participant.data['parameters']['decoder_test_stim_dirs']
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
self.data_path = self.participant.data_paths[self.test_name] self.data_path = self.participant.data_paths[self.test_name]
out_dir = os.path.join(self.data_path, "stimulus") out_dir = os.path.join(self.data_path, "stimulus")
delete_if_exists(out_dir) delete_if_exists(out_dir)
out_info = os.path.join(out_dir, "stim_info.csv") out_info = os.path.join(out_dir, "stim_info.csv")
dir_must_exist(out_dir) dir_must_exist(out_dir)
with open(out_info, 'w') as csvfile: for ind, dir_name in enumerate(stim_dirs):
writer = csv.writer(csvfile) stim_dir = os.path.join(self.listDir, dir_name)
writer.writerow(['wav', 'snr_fs', 'rms', 'si', 'snr']) wavs = globDir(stim_dir, "*.wav")[0] * len(snrs)
for wav, snr_fs, rms, si, snr in zip(wavs, self.snr_fs, rms_files, self.si, snrs): rms_files = globDir(stim_dir, "*.npy")[0] * len(snrs)
fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr)
out_wavpath = os.path.join(out_dir, fp) self.socketio.emit('test_stim_load', namespace='/main')
stim_rms = np.load(rms) # Add noise to audio files at set SNRs and write to participant
match_ratio = stim_rms/self.noise_rms # directory
block_mix_wavs(wav, self.noise_path, out_wavpath,
1.*self.reduction_coef, with open(out_info, 'w') as csvfile:
snr_fs*match_ratio*self.reduction_coef, writer = csv.writer(csvfile)
mute_left=True) writer.writerow(['wav', 'snr_fs', 'rms', 'si', 'snr'])
self.stim_paths.extend([out_wavpath] * self.nTrials) for wav, snr_fs, rms, si, snr in zip(wavs, self.snr_fs, rms_files, self.si, snrs):
writer.writerow([wav, snr_fs, rms, si, snr]) fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr)
# TODO: Output SI/snrs of each file to a CSV file out_wavpath = os.path.join(out_dir, fp)
#audio, fs, enc, fmt = sndio.read(wav, return_format=True) 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"): def saveState(self, out="test_state.pkl"):
+5 -3
View File
@@ -43,7 +43,7 @@ def gen_participant_num(participants, N = 100):
taken_nums = [] taken_nums = []
for part_key in participants.keys(): for part_key in participants.keys():
participant = participants[part_key] participant = participants[part_key]
taken_nums.append(int(participant['info']['number'][0])) taken_nums.append(int(participant['info']['number']))
inds = np.arange(N)+1 inds = np.arange(N)+1
taken_inds = np.in1d(inds, taken_nums) taken_inds = np.in1d(inds, taken_nums)
@@ -190,6 +190,7 @@ def main():
# Is the hearing loss simulator active for this participant? # Is the hearing loss simulator active for this participant?
# Even numbers yes, odd numbers no # Even numbers yes, odd numbers no
hl_sim_active = (i-1 % 2) == 0 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 decoder stories presented?
# What order are the behavioral test stimuli presented? # What order are the behavioral test stimuli presented?
# What order are the tone SNRs presented at? # What order are the tone SNRs presented at?
@@ -207,11 +208,12 @@ def main():
key = "participant_{}".format(i) 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") participants[key].save("info")
print(f"Generated {part_nums.size} new participant databases")
breakpoint()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
+7 -6
View File
@@ -19,6 +19,7 @@ import numpy as np
import random import random
from pysndfile import sndio from pysndfile import sndio
from scipy.optimize import minimize from scipy.optimize import minimize
from natsort import natsorted
from app import generate_matrix_stimulus from app import generate_matrix_stimulus
from matrix_test.helper_modules.filesystem import globDir, organiseWavs, prepareOutDir from matrix_test.helper_modules.filesystem import globDir, organiseWavs, prepareOutDir
@@ -99,7 +100,7 @@ Participant routing
def manage_participant_page(): def manage_participant_page():
# Find all pre-existing participants # Find all pre-existing participants
participants = find_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') @server.route('/participant/create')
def create_participant_page(): def create_participant_page():
@@ -121,7 +122,7 @@ def create_participant_submit():
key = "participant_{}".format(data['number']) key = "participant_{}".format(data['number'])
participants[key] = Participant(participant_dir="./participant_data/{}".format(key), **data) participants[key] = Participant(participant_dir="./participant_data/{}".format(key), **data)
participants[key].save("info") 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 EEG routing
@@ -129,7 +130,7 @@ EEG routing
@server.route('/eeg') @server.route('/eeg')
def eeg_setup(): def eeg_setup():
participants = find_participants() 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') @server.route('/eeg/test/run')
def eeg_test_run(): def eeg_test_run():
@@ -185,7 +186,7 @@ Matrix behavioral test routing
@server.route('/matrix_test') @server.route('/matrix_test')
def matrix_test_setup(): def matrix_test_setup():
participants = find_participants() 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') @server.route('/matrix_test/run')
def run_matrix_test(): def run_matrix_test():
@@ -213,7 +214,7 @@ Click stimulus routing
@server.route('/click/setup') @server.route('/click/setup')
def click_setup(): def click_setup():
participants = find_participants() 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') @server.route('/click/clinician/run')
def click_clinician_run(): def click_clinician_run():
@@ -238,7 +239,7 @@ def click_complete():
@server.route('/da/setup') @server.route('/da/setup')
def da_setup(): def da_setup():
participants = find_participants() 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') @server.route('/da/clinician/run')
def da_clinician_run(): def da_clinician_run():
+1 -1
View File
@@ -105,7 +105,7 @@ Participant socket handlers
def manage_participant_delete(participant_str): def manage_participant_delete(participant_str):
shutil.rmtree(participants[participant_str].participant_dir) shutil.rmtree(participants[participant_str].participant_dir)
del participants[participant_str] 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') @socketio.on('update_participant_info', namespace='/main')
def manage_participant_save(data): def manage_participant_save(data):
-11
View File
@@ -11,17 +11,6 @@
{% endfor %} {% endfor %}
</select> </select>
</div> </div>
<div class="form-group">
<label for="open-mat-folder-container">Matrix data folder: </label>
<br>
<input type="text" id="mat-dir" name='mat_dir' value="./matrix_test/speech_components/" style="width:85%"></input>
<button type="button" id="mat-dir-button" class="btn btn-primary">Browse...</button>
</div>
<div class="form-group">
<label for="listN">Number of lists:</label>
<br>
<input id="listN" name="listN" type="number" value="5" max="18" min="1" style="width:85%">
</div>
<div class="form-group d-flex justify-content-center"> <div class="form-group d-flex justify-content-center">
<button type="button" id="submit" class="btn btn-primary mx-3">Start new test</button> <button type="button" id="submit" class="btn btn-primary mx-3">Start new test</button>
<button type="button" id="load-saved" class="btn btn-primary mx-3">Load saved session</button> <button type="button" id="load-saved" class="btn btn-primary mx-3">Load saved session</button>