# -------------------------------------------------------------------------- # Authors: # ------ MATlAB ------ # Paolo Toffanin , RuG, UMCG, Groningen, NL # Jacqueline Libert # Leanne Nagels < leanne.nagels@rug.nl > # Etienne Gaudrain - 2017-12-05 # CNRS, CRNL, Lyon, FR | RuG, UMCG, Groningen, NL # ------ Python ------ # 2020-04-13: Luke Meyer < l.meyer.5@student.rug.nl > # Copyright MRC-CBSU(UK), CNRS(FR), UMCG(NL) and the author(s). # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY # without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see < http: // www.gnu.org/licenses/>. # -------------------------------------------------------------------------- import numpy as np from scipy import io as sio import warnings import os import re from tools.load_matrix import loadmat def expe_build_conditions(options): ''' Creates the EXPE structure that contains the trials for the PICKA Emotion experiment. Note: the emotion names listed in options[test][emotions] must coincide with the file names of the sound material, as well as with the file names of the sprite images. ''' # options["sound_folder"] = "./stimuli/emotion/en" options["sound_folder"] = "./stimuli/emotion/" options["sound_filemask"] = ".wav" mat = loadmat('./calibration.mat') cal = mat["calibration"] options["attenuation_dB"] = cal["emotion"] # Parse the corpus options = parse_corpus(options) # Select emotions for test options["test"]["emotions"] = ['angry', 'sad', 'happy'] options["order_button_emotions"] = np.random.permutation( options["test"]["emotions"]) # Select emotions for training options["training"]["emotions"] = ['angry', 'sad', 'happy'] options["order_button_emotions"] = np.random.permutation( options["training"]["emotions"]) for e in range(len(options["test"]["emotions"])): if options["test"]["emotions"][e] not in options["test"]["corpus"]: raise ValueError("Emotion '{}' was not found in the selected corpus...".format( options["test"]["emotions"][e])) # Number of repetitions per emotion options["test"]["n_repeat"] = float("Inf") # === Create the test trials === test = {} for i_emotion in range(len(options["test"]["emotions"])): emotion = options["test"]["emotions"][i_emotion] n = len(options["test"]["corpus"][emotion]) if np.isinf(options["test"]["n_repeat"]): item_order = np.array(range(0, n)) else: item_order = [] while len(item_order) < options["test"]["n_repeat"]: item_order.append(np.random.permutation( np.min([options["test"]["n_repeat"], n]))) for ir in range(int(np.min([options["test"]["n_repeat"], len(item_order)]))): trial = options["test"]["corpus"][emotion][item_order[ir]] trial["emotion"] = emotion trial["i_repeat"] = ir trial["done"] = 0 if not "trials" in test: test["trials"] = [trial] else: test["trials"].append(trial) expe = {} expe["test"] = {} expe["test"]["trials"] = np.random.permutation(test["trials"]) # === Create the training trials === training = {} for i_trial in range(len(options["training"]["corpus"])): trial = options["training"]["corpus"][i_trial] trial["done"] = 0 if not "trials" in training: training["trials"] = [trial] else: training["trials"].append(trial) expe["training"] = {} expe["training"]["trials"] = np.random.permutation(training["trials"]) if "res_filename" in options: sio.savemat(options["res_filename"], mdict={ 'expe': expe, 'options': options}) else: warnings.warn("The test file was not saved: no filename provided.") return expe, options def parse_corpus(options): options["sounds_for_calibration"] = {} if not "test" in options: options["test"] = {} if not "training" in options: options["training"] = {} options["test"]["corpus"] = {} lst = [f for f in os.listdir(os.path.join( options["sound_folder"])) if f.endswith(options["sound_filemask"])] for k in range(len(lst)): options["sounds_for_calibration"][k] = os.path.join( options["sound_folder"], lst[k]) _, fname = os.path.split(lst[k]) tokens = re.split("[" + re.escape("_") + "]", fname) if tokens[0] == "training": item = {} item["file"] = lst[k] item["talker"] = float(tokens[1][1:]) item["emotion"] = tokens[2] item["sentence"] = float(tokens[3][1:]) item["utterance"] = float(tokens[4][1:].split(".")[0]) if not "corpus" in options["training"]: options["training"]["corpus"] = [item] else: options["training"]["corpus"].append(item) else: item = {} item["file"] = lst[k] item["talker"] = float(tokens[0][1:]) emotion = tokens[1] item["sentence"] = float(tokens[2][1:]) item["utterance"] = float(tokens[3][1:].split(".")[0]) if not emotion in options["test"]["corpus"]: options["test"]["corpus"][emotion] = [item] else: options["test"]["corpus"][emotion].append(item) return options def explode(sep, string): rem = string table = [] while rem: tokens = re.split("[" + re.escape(sep) + "]", rem) token = tokens[0] rem = sep.join(tokens[1:]) table.append(token) return table