Published
Edited
Apr 5, 2020
Fork of 2lot - draft
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// create trials
trials = {
let temp = [];

for (let x = 1; x <= 126; x++) {
temp.push(x);
}
return temp;
}
Insert cell
Insert cell
shuffled_trials = shuffle(trials);
Insert cell
Insert cell
Insert cell
matrix_values_low = {
const repeat = 18;
// repeat the vectors above 18 times to get to 126 trials
// create empty arrays to hold the values
let low_values = [];
const possible_values_low = [145, 160, 175, 190, 205, 220, 235];

for (let i = 0; i < repeat; i++) {
possible_values_low.forEach(function(d) { low_values.push(d); });
}
return low_values;
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function createScalebar1(low_values, high_values) {
// create an array
let scalebar_line1 = [];
// add two empty dimensions
scalebar_line1.push([]);
scalebar_line1.push([]);


// now we fill it 126 times
for (let x = 0; x < trial_num; x++) {
// get random number
let r = trials[x];
// get a low and high value, adjust for zero index
let low_value = low_values[r-1];
let high_value = high_values[r-1];

// add to scalebar
scalebar_line1[0].push(low_value);
scalebar_line1[1].push(high_value);
}


return scalebar_line1;

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function createScalebar2(scalebar1) {
let scalebar_line2 = [];
scalebar_line2.push([]);
scalebar_line2.push([]);
// do this once for each trial
for (let x = 0; x < trial_num; x++) {
// get the low value from scalebar 1 and add low_var
let scalebar1_low = scalebar_line1[0][x] + low_var;
let scalebar1_high = scalebar_line1[1][x] + high_var;
// add them to scalebar2 with the low number in the first dimension
scalebar_line2[0].push(scalebar1_low);
scalebar_line2[1].push(scalebar1_high);
}


return scalebar_line2;
}
Insert cell
Insert cell
Insert cell
random_jitters = {
// we have 3 values here and 126 trials so we need to repeat these 42 times
let jitters = [-10, 0, 10];
const repeats = 42;

let random_jitters = [];

for (let x = 0; x < repeats; x++) {
random_jitters.push(jitters[0]);
random_jitters.push(jitters[1]);
random_jitters.push(jitters[2]);
}
//shuffle
shuffle(random_jitters);
return random_jitters;
}
Insert cell
Insert cell
function jitter(scalebar_line1, scalebar_line2) {
/* from MATLAB
• Apply the jitters to the scalebar values, adding to the first row and subtracting from the second row. By doing so, we ensure that the guess value can always be 500:
for i = 1:trial_num
scalebar_line1(1,i) = scalebar_line1(1,i)+jit(i);
scalebar_line1(2,i) = scalebar_line1(2,i)-jit(i);

scalebar_line2(1,i) = scalebar_line2(1,i)+jit(i);
scalebar_line2(2,i) = scalebar_line2(2,i)-jit(i);
end
*/
let scalebar_line1_jitter = [];
let scalebar_line2_jitter = [];
scalebar_line1_jitter.push([]);
scalebar_line1_jitter.push([]);
scalebar_line2_jitter.push([]);
scalebar_line2_jitter.push([]);


for (let x = 0; x < trial_num; x++) {
// get a random jitter
let j = random_jitters[x];
// get the low item in scalebar 1 and add jitter
scalebar_line1_jitter[0][x] = scalebar_line1[0][x] + j;
// get the high item and subtract jitter
scalebar_line1_jitter[1][x] = scalebar_line1[1][x] - j;
// same for scalebar_line2
scalebar_line2_jitter[0][x] = scalebar_line2[0][x] + j;
scalebar_line2_jitter[1][x] = scalebar_line2[1][x] + j;
}

return [scalebar_line1_jitter,
scalebar_line2_jitter];

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = jitter(scalebar_line1, scalebar_line2);
Insert cell
Insert cell
even_mix = {
let even_mix = [];
let even = true;
for (let x = 0; x < trial_num; x++) {
(even) ? even_mix.push(0) : even_mix.push(1)
even = !even;
}

// shuffle
shuffle(even_mix);
lb(1);

return even_mix;
}
Insert cell
Insert cell
Insert cell
Insert cell
scalebar_line1_final = data[0]
Insert cell
scalebar_line2_final = data[1]
Insert cell
trial_data = {

/*
• Generate which side each scalebar (high range/variance versus low range/variance) appears on for each trial:
for i = 1:trial_num
if rand_side(i) == 1
left_line1(i) = scalebar_line1(1,i);
right_line1(i) = scalebar_line1(2,i);
left_line2(i) = scalebar_line2(1,i);
right_line2(i) = scalebar_line2(2,i);
else
right_line1(i) = scalebar_line1(1,i);
left_line1(i) = scalebar_line1(2,i);
right_line2(i) = scalebar_line2(1,i);
left_line2(i) = scalebar_line2(2,i);
end
end
*/
// assign new variables here for readability
let scalebar_line1 = scalebar_line1_final;
let scalebar_line2 = scalebar_line2_final;
let left_line1 = []; let left_line2 = [];
let right_line1 = []; let right_line2 = [];

for (let i = 0; i < trial_num; i++) {
let e = even_mix[i];
if (e == 1) {
left_line1.push(scalebar_line1[0][i]);
right_line1.push(scalebar_line1[1][i]);
left_line2.push(scalebar_line2[0][i]);
right_line2.push(scalebar_line2[1][i]);
} else {
right_line1.push(scalebar_line1[0][i]);
left_line1.push(scalebar_line1[1][i]);
left_line2.push(scalebar_line2[0][i]);
right_line2.push(scalebar_line2[1][i]);
}
}

// return and object with the keys corresponding to the lines
let data = {};
data.left_line1 = left_line1;
data.left_line2 = left_line2;
data.right_line1 = right_line1;
data.right_line2 = right_line2;
return data;
}
Insert cell
Insert cell
trial_data
Insert cell
Insert cell
Insert cell
{
/*

• Calculate means for each scalebar:
right_mean = (right_line1 + right_line2)/2;
left_mean = (left_line1 + left_line_2)/2;
• In this code:
• right_line1 = the value of the lower line on the right scalebar for each trial
• right_line2 = the value of the higher line on the right scalebar for each trial
• left_line1 = the value of the lower line on the left scalebar for each trial
• left_line2 = the value of the higher line on the left scalebar for each trial
• right_mean = the mean of the two lines on the right scalebar for each trial
• left_mean = the mean of the two lines on the left scalebar for each trial

*/
}
Insert cell
function getMean(line1, line2) {
return (line1+line2) / 2;
}
Insert cell
Insert cell
left_means = {

let left_line1 = trial_data.left_line1;
let left_line2 = trial_data.left_line2;
let temp = [];
for (let i = 0; i < left_line1.length; i++) {
let mean = getMean(left_line1[i], left_line2[i]);
temp.push(mean);
}
return temp;
}
Insert cell
Insert cell
right_means = {
let right_line1 = trial_data.right_line1;
let right_line2 = trial_data.right_line2;
let temp = [];
for (let i = 0; i < right_line1.length; i++) {
let mean = getMean(right_line1[i], right_line2[i]);
temp.push(mean);
}
return temp;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more