Implementing tone stimulus presentation edits
This commit is contained in:
+49
-22
@@ -19,6 +19,10 @@ from config import socketio
|
|||||||
import csv
|
import csv
|
||||||
import pdb
|
import pdb
|
||||||
import dill
|
import dill
|
||||||
|
import re
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
symb_dict = {
|
symb_dict = {
|
||||||
True: 10003,
|
True: 10003,
|
||||||
@@ -39,9 +43,9 @@ 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='./tone_stim/',
|
stimFolder='./tone_stim/stimulus',
|
||||||
noiseFilepath="./tone_stim/noise/wav/noise/noise.wav",
|
noiseFilepath="./tone_stim/noise/wav/noise/noise.wav",
|
||||||
noiseRMSFilepath="./tone_stim/noise/rms/noise_rms.npy",
|
noiseintensityFilepath="./tone_stim/noise/intensity/noise_intensity.npy",
|
||||||
red_coef="./calibration/out/reduction_coefficients/tone_red_coef.npy",
|
red_coef="./calibration/out/reduction_coefficients/tone_red_coef.npy",
|
||||||
cal_coef="./calibration/out/calibration_coefficients/tone_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,
|
||||||
@@ -49,23 +53,24 @@ class DaTestThread(BaseThread):
|
|||||||
|
|
||||||
self.reduction_coef = np.load(red_coef)*np.load(cal_coef)
|
self.reduction_coef = np.load(red_coef)*np.load(cal_coef)
|
||||||
self.noise_path = noiseFilepath
|
self.noise_path = noiseFilepath
|
||||||
self.noise_rms = np.load(noiseRMSFilepath)
|
self.noise_intensity = np.load(noiseintensityFilepath)
|
||||||
self.stim_folder = stimFolder
|
self.stim_folder = stimFolder
|
||||||
self.stim_paths = []
|
self.stim_paths = []
|
||||||
|
|
||||||
self.test_name = 'da_test'
|
self.participant = participant
|
||||||
|
self.participant_parameters = self.participant.parameters
|
||||||
|
|
||||||
|
self.test_name = 'tone_test'
|
||||||
self.nTrials = nTrials
|
self.nTrials = nTrials
|
||||||
self.trial_ind = 0
|
self.trial_ind = 0
|
||||||
self._stopevent = Event()
|
self._stopevent = Event()
|
||||||
# (Completely clean stimulus and stimulus at +10dB added by default later)
|
|
||||||
self.si = np.array([19.0, 50.0, 81.0])
|
|
||||||
|
|
||||||
super(DaTestThread, self).__init__(self.test_name,
|
super(DaTestThread, self).__init__(self.test_name,
|
||||||
sessionFilepath=sessionFilepath,
|
sessionFilepath=sessionFilepath,
|
||||||
socketio=socketio,
|
socketio=socketio,
|
||||||
participant=participant)
|
participant=participant)
|
||||||
|
|
||||||
self.toSave = ['stim_paths', 'trial_ind', 'nTrials', 'test_name', 'si']
|
self.toSave = ['stim_paths', 'trial_ind', 'nTrials', 'test_name']
|
||||||
|
|
||||||
self.socketio.on_event('finalise_results', self.finaliseResults, namespace='/main')
|
self.socketio.on_event('finalise_results', self.finaliseResults, namespace='/main')
|
||||||
|
|
||||||
@@ -84,10 +89,16 @@ class DaTestThread(BaseThread):
|
|||||||
self.waitForPartReady()
|
self.waitForPartReady()
|
||||||
if self._stopevent.isSet() or self.finishTest:
|
if self._stopevent.isSet() or self.finishTest:
|
||||||
break
|
break
|
||||||
# Play concatenated matrix sentences at set SNR
|
logger.info("-"*78)
|
||||||
|
_, freq, snr_fs = re.findall(".tone_(\d+)_(\d+)Hz_([-+]?\d*\.\d+|\d+).", wav)[0]
|
||||||
|
snr = float(snr_fs) - self.participant.data['mat_test']['srt_50']
|
||||||
|
logger.info("{0:<25}".format("Current trial:") + f" {self.trial_ind}")
|
||||||
|
logger.info("{0:<25}".format("Current SNR:") + f"{snr}")
|
||||||
|
logger.info("{0:<25}".format("Current frequency:") + f"{freq}")
|
||||||
self.playStimulusWav(wav)
|
self.playStimulusWav(wav)
|
||||||
self.trial_ind += 1
|
self.trial_ind += 1
|
||||||
self.saveState(out=self.backupFilepath)
|
self.saveState(out=self.backupFilepath)
|
||||||
|
logger.info("-"*78)
|
||||||
if not self._stopevent.isSet():
|
if not self._stopevent.isSet():
|
||||||
self.unsetPageLoaded()
|
self.unsetPageLoaded()
|
||||||
self.socketio.emit('processing-complete', namespace='/main')
|
self.socketio.emit('processing-complete', namespace='/main')
|
||||||
@@ -130,19 +141,35 @@ class DaTestThread(BaseThread):
|
|||||||
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
|
snrs = np.squeeze(self.participant.data['parameters']['tone_SNRs'])
|
||||||
stim_dirs = self.participant.data['parameters']['decoder_test_stim_dirs']
|
snrs[~np.isinf(snrs)] += srt_50
|
||||||
|
self.snr_fs = 10**(-snrs/20)
|
||||||
|
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))
|
||||||
|
|
||||||
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)
|
||||||
breakpoint()
|
|
||||||
|
|
||||||
for ind, dir_name in enumerate(stim_dirs):
|
stim_dirs = [x for x in os.listdir(self.stim_folder) if os.path.isdir(os.path.join(self.stim_folder, x))]
|
||||||
stim_dir = os.path.join(self.listDir, dir_name)
|
|
||||||
wavs = globDir(stim_dir, "*.wav")[0] * len(snrs)
|
ordered_stim_dirs = []
|
||||||
rms_files = globDir(stim_dir, "*.npy")[0] * len(snrs)
|
for freq in self.participant_parameters['tone_freqs']:
|
||||||
|
for folder in stim_dirs:
|
||||||
|
if re.match(f'tone_({int(freq)})', folder):
|
||||||
|
ordered_stim_dirs.append(folder)
|
||||||
|
ordered_stim_dirs *= int(len(snrs))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for ind, dir_name in enumerate(ordered_stim_dirs):
|
||||||
|
stim_dir = os.path.join(self.stim_folder, dir_name)
|
||||||
|
wavs = globDir(stim_dir, "*.wav")
|
||||||
|
intensity_files = globDir(stim_dir, "*intensity.npy")
|
||||||
|
|
||||||
self.socketio.emit('test_stim_load', namespace='/main')
|
self.socketio.emit('test_stim_load', namespace='/main')
|
||||||
# Add noise to audio files at set SNRs and write to participant
|
# Add noise to audio files at set SNRs and write to participant
|
||||||
@@ -150,18 +177,18 @@ class DaTestThread(BaseThread):
|
|||||||
|
|
||||||
with open(out_info, 'w') as csvfile:
|
with open(out_info, 'w') as csvfile:
|
||||||
writer = csv.writer(csvfile)
|
writer = csv.writer(csvfile)
|
||||||
writer.writerow(['wav', 'snr_fs', 'rms', 'si', 'snr'])
|
writer.writerow(['wav', 'snr_fs', 'intensity', 'snr'])
|
||||||
for wav, snr_fs, rms, si, snr in zip(wavs, self.snr_fs, rms_files, self.si, snrs):
|
for wav, snr_fs, intensity, snr in zip(wavs, self.snr_fs, intensity_files, snrs):
|
||||||
fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr)
|
fp = os.path.splitext(os.path.basename(wav))[0]+"_{}.wav".format(snr)
|
||||||
out_wavpath = os.path.join(out_dir, fp)
|
out_wavpath = os.path.join(out_dir, fp)
|
||||||
stim_rms = np.load(rms)
|
stim_intensity = np.load(intensity)
|
||||||
match_ratio = stim_rms/self.noise_rms
|
match_ratio = stim_intensity/self.noise_intensity
|
||||||
block_mix_wavs(wav, self.noise_path, out_wavpath,
|
block_mix_wavs(wav, self.noise_path, out_wavpath,
|
||||||
self.reduction_coef,
|
self.reduction_coef,
|
||||||
snr_fs*match_ratio*self.reduction_coef,
|
snr_fs*match_ratio*self.reduction_coef,
|
||||||
mute_left=True)
|
mute_left=False)
|
||||||
self.stim_paths.extend([out_wavpath] * self.nTrials)
|
self.stim_paths.extend([out_wavpath])
|
||||||
writer.writerow([wav, snr_fs, rms, si, snr])
|
writer.writerow([wav, snr_fs, intensity, snr])
|
||||||
# TODO: Output SI/snrs of each file to a CSV file
|
# TODO: Output SI/snrs of each file to a CSV file
|
||||||
#audio, fs, enc, fmt = sndio.read(wav, return_format=True)
|
#audio, fs, enc, fmt = sndio.read(wav, return_format=True)
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ class EEGMatTrainThread(BaseThread):
|
|||||||
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))
|
||||||
|
|
||||||
|
|
||||||
wavs = globDir(self.stim_folder, "*.wav")
|
wavs = globDir(self.stim_folder, "*.wav")
|
||||||
questions = globDir(self.stim_folder, "stim_questions_*.csv")
|
questions = globDir(self.stim_folder, "stim_questions_*.csv")
|
||||||
if not len(questions):
|
if not len(questions):
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ class EEGStoryTrainThread(BaseThread):
|
|||||||
self.nTrials = nTrials
|
self.nTrials = nTrials
|
||||||
self.trial_ind = 0
|
self.trial_ind = 0
|
||||||
|
|
||||||
|
self.participant = participant
|
||||||
|
self.participant_parameters = self.participant.parameters
|
||||||
|
|
||||||
self.selected_q = []
|
self.selected_q = []
|
||||||
self.question = []
|
self.question = []
|
||||||
self.answers = [''] * 8
|
self.answers = [''] * 8
|
||||||
|
|||||||
+2
-2
@@ -97,7 +97,7 @@ class Participant:
|
|||||||
"notes": ''
|
"notes": ''
|
||||||
},
|
},
|
||||||
|
|
||||||
"da_test": {
|
"tone_test": {
|
||||||
"notes": ''
|
"notes": ''
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ class Participant:
|
|||||||
def generate_folder_hierachy(self):
|
def generate_folder_hierachy(self):
|
||||||
'''
|
'''
|
||||||
'''
|
'''
|
||||||
sub_dirs = ["mat_test", "da_test", "pta", "click_test", "info",
|
sub_dirs = ["mat_test", "tone_test", "pta", "click_test", "info",
|
||||||
"eeg_story_train", "eeg_mat_train", "eeg_test",
|
"eeg_story_train", "eeg_mat_train", "eeg_test",
|
||||||
"eeg_test/stimulus", "parameters"]
|
"eeg_test/stimulus", "parameters"]
|
||||||
for dir_name in sub_dirs:
|
for dir_name in sub_dirs:
|
||||||
|
|||||||
@@ -75,9 +75,9 @@
|
|||||||
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
||||||
<a class="dropdown-item" href="/pta_test">PTA</a>
|
<a class="dropdown-item" href="/pta_test">PTA</a>
|
||||||
<a class="dropdown-item" href="/tympanometry">Tympanometry</a>
|
<a class="dropdown-item" href="/tympanometry">Tympanometry</a>
|
||||||
|
<a class="dropdown-item" href="/da/setup">Tone EEG recording</a>
|
||||||
<a class="dropdown-item" href="/matrix_test">Behavioral Matrix Test</a>
|
<a class="dropdown-item" href="/matrix_test">Behavioral Matrix Test</a>
|
||||||
<a class="dropdown-item" href="/eeg">Decoder EEG recording</a>
|
<a class="dropdown-item" href="/eeg">Decoder EEG recording</a>
|
||||||
<a class="dropdown-item" href="/da/setup">/da/ EEG recording</a>
|
|
||||||
<a class="dropdown-item" href="/click/setup">Tone EEG recording</a>
|
<a class="dropdown-item" href="/click/setup">Tone EEG recording</a>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
Reference in New Issue
Block a user