1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00

Merge pull request #445 from apitiot/2021.2.2

merge from apitiot 2021.2.2
This commit is contained in:
Alain Pitiot 2021-07-26 14:39:56 +02:00 committed by GitHub
commit 3aea78019d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 388 deletions

View File

@ -17,7 +17,7 @@ module.exports = {
"block-spacing": 2,
"brace-style": [2, "allman", { allowSingleLine: true }],
"camelcase": 1,
"capitalized-comments": [1, "always", { ignoreConsecutiveComments: true }],
"capitalized-comments": 0,
"comma-spacing": 2,
"comma-style": 2,
"consistent-return": 1,
@ -47,7 +47,7 @@ module.exports = {
"no-console": 1,
"no-div-regex": 2,
"no-duplicate-imports": 2,
"no-else-return": 2,
"no-else-return": 1,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
@ -65,7 +65,7 @@ module.exports = {
"no-mixed-requires": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { max: 1, maxEOF: 0 }],
"no-multiple-empty-lines": [1, { max: 2, maxEOF: 0 }],
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
@ -74,7 +74,7 @@ module.exports = {
"no-octal-escape": 2,
"no-param-reassign": 1,
"no-path-concat": 2,
"no-plusplus": 2,
"no-plusplus": 0,
"no-proto": 2,
"no-restricted-properties": 2,
"no-return-assign": [2, "except-parens"],
@ -85,14 +85,15 @@ module.exports = {
"no-shadow-restricted-names": 2,
"no-tabs": [1, { allowIndentationTabs: true }],
"no-template-curly-in-string": 2,
"no-throw-literal": 2,
"no-throw-literal": 0,
"no-trailing-spaces": 2,
"no-undef-init": 2,
// https://eslint.org/docs/rules/no-underscore-dangle#disallow-dangling-underscores-in-identifiers-no-underscore-dangle
"no-underscore-dangle": 1,
"no-underscore-dangle": 0,
"no-unmodified-loop-condition": 2,
"no-unneeded-ternary": 2,
"no-unused-expressions": 2,
"no-unused-expressions": 1,
"no-unused-vars": 1,
"no-use-before-define": [2, { functions: false }],
"no-useless-call": 2,
"no-useless-computed-key": 2,
@ -106,7 +107,7 @@ module.exports = {
"object-property-newline": [2, { allowMultiplePropertiesPerLine: true }],
"one-var": [2, "never"],
"one-var-declaration-per-line": 2,
"operator-linebreak": [2, "before"],
"operator-linebreak": [1, "before"],
"padded-blocks": [2, "never"],
"padding-line-between-statements": 2,
"prefer-const": 2,

View File

@ -1,6 +1,6 @@
{
"name": "psychojs",
"version": "2021.2.1",
"version": "2021.2.2",
"private": true,
"description": "Helps run in-browser neuroscience, psychology, and psychophysics experiments",
"license": "MIT",

View File

@ -176,7 +176,7 @@ export class PsychoJS
}
this.logger.info("[PsychoJS] Initialised.");
this.logger.info("[PsychoJS] @version 2021.2.0");
this.logger.info("[PsychoJS] @version 2021.2.2");
// hide the initialisation message:
jQuery("#root").addClass("is-ready");

View File

@ -453,7 +453,7 @@ export class ServerManager extends PsychObject
path,
data: undefined,
});
this._psychoJS.logger.debug("registered resource:", name, path);
this._psychoJS.logger.debug(`registered resource: name= ${name}, path= ${path}`);
resourcesToDownload.add(name);
}
}
@ -476,12 +476,10 @@ export class ServerManager extends PsychObject
{
// to deal with potential CORS issues, we use the pavlovia.org proxy for resources
// not hosted on pavlovia.org:
if (
(path.toLowerCase().indexOf("www.") === 0
|| path.toLowerCase().indexOf("http:") === 0
|| path.toLowerCase().indexOf("https:") === 0)
&& (path.indexOf("pavlovia.org") === -1)
)
if ( (path.toLowerCase().indexOf("www.") === 0 ||
path.toLowerCase().indexOf("http:") === 0 ||
path.toLowerCase().indexOf("https:") === 0) &&
(path.indexOf("pavlovia.org") === -1) )
{
path = "https://pavlovia.org/api/v2/proxy/" + path;
}
@ -491,7 +489,7 @@ export class ServerManager extends PsychObject
path,
data: undefined,
});
this._psychoJS.logger.debug("registered resource:", name, path);
this._psychoJS.logger.debug(`registered resource: name= ${name}, path= ${path}`);
// download resources by default:
if (typeof download === "undefined" || download)

View File

@ -1,348 +0,0 @@
/** @module data */
/**
* Quest Trial Handler
*
* @author Alain Pitiot & Thomas Pronk
* @version 2021.2.0
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2021 Open Science Tools Ltd. (https://opensciencetools.org)
* @license Distributed under the terms of the MIT License
*/
import {TrialHandler} from "./TrialHandler.js";
/**
* <p>A Trial Handler that implements the Quest algorithm for quick measurement of
psychophysical thresholds. QuestHandler relies on the [jsQuest]{@link https://github.com/kurokida/jsQUEST} library, a port of Prof Dennis Pelli's QUEST algorithm by [Daiichiro Kuroki]{@link https://github.com/kurokida}.</p>
*
* @class module.data.QuestHandler
* @extends TrialHandler
* @param {Object} options
* @param {module:core.PsychoJS} options.psychoJS - the PsychoJS instance
* @param {string} options.varName - the name of the variable / intensity / contrast / threshold manipulated by QUEST
* @param {number} options.startVal - initial guess for the threshold
* @param {number} options.startValSd - standard deviation of the initial guess
* @param {number} options.minVal - minimum value for the threshold
* @param {number} options.maxVal - maximum value for the threshold
* @param {number} [options.pThreshold=0.82] - threshold criterion expressed as probability of getting a correct response
* @param {number} options.nTrials - maximum number of trials
* @param {number} options.stopInterval - minimum [5%, 95%] confidence interval required for the loop to stop
* @param {module:data.QuestHandler.Method} options.method - the QUEST method
* @param {number} [options.beta=3.5] - steepness of the QUEST psychometric function
* @param {number} [options.delta=0.01] - fraction of trials with blind responses
* @param {number} [options.gamma=0.5] - fraction of trails that would generate a correct response when the threshold is infinitely small
* @param {number} [options.grain=0.01] - quantization of the internal table
* @param {string} options.name - name of the handler
* @param {boolean} [options.autoLog= false] - whether or not to log
*/
export class QuestHandler extends TrialHandler
{
/**
* @constructor
* @public
*/
constructor({
psychoJS,
varName,
startVal,
startValSd,
minVal,
maxVal,
pThreshold,
nTrials,
stopInterval,
method,
beta,
delta,
gamma,
grain,
name,
autoLog
} = {})
{
super({
psychoJS,
name,
autoLog,
method: TrialHandler.Method.SEQUENTIAL,
trialList: Array(nTrials),
nReps: 1
});
this._addAttribute('varName', varName);
this._addAttribute('startVal', startVal);
this._addAttribute('minVal', minVal, Number.MIN_VALUE);
this._addAttribute('maxVal', maxVal, Number.MAX_VALUE);
this._addAttribute('startValSd', startValSd);
this._addAttribute('pThreshold', pThreshold, 0.82);
this._addAttribute('nTrials', nTrials);
this._addAttribute('stopInterval', stopInterval, Number.MIN_VALUE);
this._addAttribute('beta', beta, 3.5);
this._addAttribute('delta', delta, 0.01);
this._addAttribute('gamma', gamma, 0.5);
this._addAttribute('grain', grain, 0.01);
this._addAttribute('method', method, QuestHandler.Method.QUANTILE);
// setup jsQuest:
this._setupJsQuest();
}
/**
* Add a response and update the PDF.
*
* @name module:data.QuestHandler#addResponse
* @function
* @public
* @param{number} response - the response to the trial, must be either 0 (incorrect or
* non-detected) or 1 (correct or detected).
*/
addResponse(response)
{
// check that response is either 0 or 1:
if (response !== 0 && response !== 1)
{
throw {
origin: 'QuestHandler.addResponse',
context: 'when adding a trial response',
error: `the response must be either 0 or 1, got: ${JSON.stringify(response)}`
};
}
// update the QUEST pdf:
this._jsQuest = jsQUEST.QuestUpdate(this._jsQuest, this._questValue, response);
if (!this._finished)
{
// estimate the next value of the QUEST variable (and update the trial list and snapshots):
this._estimateQuestValue();
}
}
/**
* Simulate a response.
*
* @name module:data.QuestHandler#simulate
* @function
* @public
* @param{number} trueValue - the true, known value of the threshold / contrast / intensity
*/
simulate(trueValue)
{
const response = jsQUEST.QuestSimulate(this._jsQuest, this._questValue, trueValue);
// restrict to limits:
this._questValue = Math.max(this._minVal, Math.min(this._maxVal, this._questValue));
this._psychoJS.logger.debug(`simulated response: ${response}`);
return response;
}
/**
* Get the mean of the Quest posterior PDF.
*
* @name module:data.QuestHandler#mean
* @function
* @public
* @returns {number} the mean
*/
mean()
{
return jsQUEST.QuestMean(this._jsQuest);
}
/**
* Get the standard deviation of the Quest posterior PDF.
*
* @name module:data.QuestHandler#sd
* @function
* @public
* @returns {number} the standard deviation
*/
sd()
{
return jsQUEST.QuestSd(this._jsQuest);
}
/**
* Get the mode of the Quest posterior PDF.
*
* @name module:data.QuestHandler#mode
* @function
* @public
* @returns {number} the mode
*/
mode()
{
const [mode, pdf] = jsQUEST.QuestMode(this._jsQuest);
return mode;
}
/**
* Get the standard deviation of the Quest posterior PDF.
*
* @name module:data.QuestHandler#quantile
* @function
* @public
* @param{number} quantileOrder the quantile order
* @returns {number} the quantile
*/
quantile(quantileOrder)
{
return jsQUEST.QuestQuantile(this._jsQuest, quantileOrder);
}
/**
* Get an estimate of the 5%-95% confidence interval (CI).
*
* @name module:data.QuestHandler#confInterval
* @function
* @public
* @param{boolean} [getDifference=false] if true, return the width of the CI instead of the CI
*/
confInterval(getDifference = false)
{
const CI = [
jsQUEST.QuestQuantile(this._jsQuest, 0.05),
jsQUEST.QuestQuantile(this._jsQuest, 0.95)
];
if (getDifference)
{
return Math.abs(CI[0] - CI[1]);
}
else
{
return CI;
}
}
/**
* Setup the JS Quest object.
*
* @name module:data.QuestHandler#_setupJsQuest
* @function
* @protected
*/
_setupJsQuest()
{
this._jsQuest = jsQUEST.QuestCreate(
this._startVal,
this._startValSd,
this._pThreshold,
this._beta,
this._delta,
this._gamma,
this._grain);
this._estimateQuestValue();
}
/**
* Estimate the next value of the QUEST variable, based on the current value
* and on the selected QUEST method.
*
* @name module:data.QuestHandler#_estimateQuestValue
* @function
* @protected
*/
_estimateQuestValue()
{
// estimate the value based on the chosen QUEST method:
if (this._method === QuestHandler.Method.QUANTILE)
{
this._questValue = jsQUEST.QuestQuantile(this._jsQuest);
}
else if (this._method === QuestHandler.Method.MEAN)
{
this._questValue = jsQUEST.QuestMean(this._jsQuest);
}
else if (this._method === QuestHandler.Method.MODE)
{
const [mode, pdf] = jsQUEST.QuestMode(this._jsQuest);
this._questValue = mode;
}
else
{
throw {
origin: 'QuestHandler._estimateQuestValue',
context: 'when estimating the next value of the QUEST variable',
error: `unknown method: ${this._method}, please use: mean, mode, or quantile`
};
}
this._psychoJS.logger.debug(`estimated value for QUEST variable ${this._varName}: ${this._questValue}`);
// check whether we should finish the trial:
if (this.thisN > 0 &&
(this.nRemaining === 0 || this.confInterval(true) < this._stopInterval))
{
this._finished = true;
// update the snapshots associated with the current trial in the trial list:
for (let t = 0; t < this._trialList.length-1; ++t)
{
// the current trial is the last defined one:
if (typeof this._trialList[t+1] === 'undefined')
{
this._snapshots[t].finished = true;
break;
}
}
return;
}
// update the next undefined trial in the trial list, and the associated snapshot:
for (let t = 0; t < this._trialList.length; ++t)
{
if (typeof this._trialList[t] === 'undefined')
{
this._trialList[t] = { [this._varName]: this._questValue };
if (typeof this._snapshots[t] !== 'undefined')
{
this._snapshots[t][this._varName] = this._questValue;
this._snapshots[t].trialAttributes.push(this._varName);
}
break;
}
}
}
}
/**
* QuestHandler method
*
* @enum {Symbol}
* @readonly
* @public
*/
QuestHandler.Method = {
/**
* Quantile threshold estimate.
*/
QUANTILE: Symbol.for('QUANTILE'),
/**
* Mean threshold estimate.
*/
MEAN: Symbol.for('MEAN'),
/**
* Mode threshold estimate.
*/
MODE: Symbol.for('MODE')
};

View File

@ -19,7 +19,7 @@ import * as util from "../util/Util.js";
*
* @class
* @extends PsychObject
* @param {Object} options
* @param {Object} options - the handler options
* @param {module:core.PsychoJS} options.psychoJS - the PsychoJS instance
* @param {Array.<Object> | String} [options.trialList= [undefined] ] - if it is a string, we treat it as the name of a condition resource
* @param {number} options.nReps - number of repetitions
@ -80,7 +80,7 @@ export class TrialHandler extends PsychObject
this._addAttribute("name", name);
this._addAttribute("autoLog", autoLog);
this._addAttribute("seed", seed);
this._prepareTrialList(trialList);
this._prepareTrialList();
// number of stimuli
this.nStim = this.trialList.length;
@ -520,7 +520,7 @@ export class TrialHandler extends PsychObject
{
try
{
let resourceExtension = resourceName.split(".").pop();
const resourceExtension = resourceName.split(".").pop();
if (["csv", "odp", "xls", "xlsx"].indexOf(resourceExtension) > -1)
{
// (*) read conditions from resource:
@ -551,9 +551,9 @@ export class TrialHandler extends PsychObject
// (*) return the selected conditions as an array of 'object as map':
// [
// {field0: value0-0, field1: value0-1, ...}
// {field0: value1-0, field1: value1-1, ...}
// ...
// {field0: value0-0, field1: value0-1, ...}
// {field0: value1-0, field1: value1-1, ...}
// ...
// ]
let trialList = new Array(selectedRows.length - 1);
for (let r = 0; r < selectedRows.length; ++r)
@ -617,10 +617,11 @@ export class TrialHandler extends PsychObject
/**
* Prepare the trial list.
*
* @function
* @protected
* @param {Array.<Object> | String} trialList - a list of trials, or the name of a condition resource
* @returns {void}
*/
_prepareTrialList(trialList)
_prepareTrialList()
{
const response = {
origin: "TrialHandler._prepareTrialList",
@ -628,28 +629,28 @@ export class TrialHandler extends PsychObject
};
// we treat undefined trialList as a list with a single empty entry:
if (typeof trialList === "undefined")
if (typeof this._trialList === "undefined")
{
this.trialList = [undefined];
}
// if trialList is an array, we make sure it is not empty:
else if (Array.isArray(trialList))
else if (Array.isArray(this._trialList))
{
if (trialList.length === 0)
if (this._trialList.length === 0)
{
this.trialList = [undefined];
}
}
// if trialList is a string, we treat it as the name of the condition resource:
else if (typeof trialList === "string")
else if (typeof this._trialList === "string")
{
this.trialList = TrialHandler.importConditions(this.psychoJS.serverManager, trialList);
this.trialList = TrialHandler.importConditions(this.psychoJS.serverManager, this._trialList);
}
// unknown type:
else
{
throw Object.assign(response, {
error: "unable to prepare trial list: unknown type: " + (typeof trialList),
error: `unable to prepare trial list: unknown type: ${(typeof this._trialList)}`
});
}
}
@ -690,13 +691,13 @@ export class TrialHandler extends PsychObject
// get an array of the indices of the elements of trialList :
const indices = Array.from(this.trialList.keys());
if (this.method === TrialHandler.Method.SEQUENTIAL)
if (this._method === TrialHandler.Method.SEQUENTIAL)
{
this._trialSequence = Array(this.nReps).fill(indices);
// transposed version:
// this._trialSequence = indices.reduce( (seq, e) => { seq.push( Array(this.nReps).fill(e) ); return seq; }, [] );
}
else if (this.method === TrialHandler.Method.RANDOM)
else if (this._method === TrialHandler.Method.RANDOM)
{
this._trialSequence = [];
for (let i = 0; i < this.nReps; ++i)
@ -704,10 +705,10 @@ export class TrialHandler extends PsychObject
this._trialSequence.push(util.shuffle(indices.slice(), this._randomNumberGenerator));
}
}
else if (this.method === TrialHandler.Method.FULL_RANDOM)
else if (this._method === TrialHandler.Method.FULL_RANDOM)
{
// create a flat sequence with nReps repeats of indices:
let flatSequence = [];
const flatSequence = [];
for (let i = 0; i < this.nReps; ++i)
{
flatSequence.push.apply(flatSequence, indices);

View File

@ -1,4 +1,2 @@
export * from './ExperimentHandler.js';
export * from './TrialHandler.js';
export * from './QuestHandler';
//export * from './Shelf.js';
export * from "./ExperimentHandler.js";
export * from "./TrialHandler.js";