URL of experiment: Virginia Penhune / Melody_combined · GitLab
I’ve added in some custom code to quasi-randomize stimulus presentation so the same stimulus type isn’t presented too many times in a row. (See this issue)
Seems to work offline but online I just get a loud tone in place of my stimuli (wav files). I suspect there is an issue with some of the auto conversion from python to JS. (Unless I need to pass full file paths of my stimuli? Since I’m not getting my file names from a loop. Only other thing I can think of.)
I will try present the code here but it’s a bit of a mess. I appreciate any help. I’ve tried to make this clear but if I can present it in a better way or give more detail let me know. I haven’t mentioned the structure of the experiment (e.g. loop placement, whether code is at start of experiment vs routine, etc.) because I suspect it’s just a typo in the JS code but I can give more detail if needed.
CODE:
This code creates some initial objects, loads in the data from a csv and also creates psychoJS equivalents for range and append functions (in the JS code only).
Python:
stimulus_list=[]
stim_type=[]
index = [x for x in range(48)]
psychoJS:
function range(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
Array.prototype.append = [].push
stimulus_list = [];
stim_type = [];
index = function () {
var _pj_a = [], _pj_b = range(48);
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var x = _pj_b[_pj_c];
_pj_a.push(x);
}
return _pj_a;
}
.call(this);
Python:
stimulus_list.append(str(melody))
stim_type.append(str(type_stim))
psychoJS:
stimulus_list.append(melody.toString());
stim_type.append(type_stim.toString());
Here is the big one that does the randomization.
Python:
shuffle(index)
stim_type = [stim_type[i] for i in index]
# make sure the same target doesn't appear on consecutive trials
double_stim = False
list_ok = False
while list_ok == False:
for i in range(len(stim_type) - 1):
# check for 4 equal neighbours:
if stim_type[i] == stim_type[i + 1] and stim_type[i + 1] == stim_type[i + 2] and stim_type[i + 2] == stim_type[i + 3]:
break # can stop checking on this run
if double_stim == True:
shuffle(index) # try a new order
double_stim = False
else:
list_ok = True # stop loop
stimulus_list = [stimulus_list[i] for i in index]
stimulus = stimulus_list[0]
i = 0
psychoJS:
util.shuffle(index);
stim_type = function () {
var _pj_a = [], _pj_b = index;
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var i = _pj_b[_pj_c];
_pj_a.push(stim_type[i]);
}
return _pj_a;
}
.call(this);
double_stim = false;
list_ok = false;
while ((list_ok === false)) {
for (var i = 0, _pj_a = (stim_type.length - 1); (i < _pj_a); i += 1) {
if ((((stim_type[i] === stim_type[(i + 1)]) && (stim_type[(i + 1)] === stim_type[(i + 2)])) && (stim_type[(i + 2)] === stim_type[(i + 3)]))) {
break;
}
}
if ((double_stim === true)) {
util.shuffle(index);
double_stim = false;
} else {
list_ok = true;
}
}
stimulus_list = function () {
var _pj_a = [], _pj_b = index;
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var i = _pj_b[_pj_c];
_pj_a.push(stimulus_list[i]);
}
return _pj_a;
}
.call(this);
stimulus = stimulus_list[0];
i = 0;
Lastly, this is the code that runs with the sound component. It selects each element from the randomized list. And then ‘$stimulus’ is the value set in the builder component. It also saves the order of the stimuli in the data.
Python:
thisExp.addData('stimuli', stimulus_list)
if melodies.thisN != 0:
stimulus = stimulus_list[i+1]
i = i+1
psychoJS:
psychoJS.experiment.addData("stimuli", stimulus_list);
if ((melodies.thisN !== 0)) {
stimulus = stimulus_list[(i + 1)];
i = (i + 1);
}