UNPKG

aiom_pack

Version:

Framework for interdependent (mcmc-like) behavioral experiments

114 lines (95 loc) 3.69 kB
const fs = require('fs'); const path = require('path'); const dotenv = require('dotenv'); class ExperimentConfig { constructor(experimentPath) { this.experimentPath = experimentPath; this.config = {}; this.loadConfig(); } loadConfig() { // Load .env file from experiment directory const envPath = path.join(this.experimentPath, '.env'); if (fs.existsSync(envPath)) { dotenv.config({ path: envPath }); } const packageJsonPath = path.join(this.experimentPath, 'package.json'); let experimentType = 'blockwise-MCMCP'; if (fs.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); if (packageJson.description) { experimentType = packageJson.description; } } // Define all possible configuration with defaults this.defaults = { experiment: experimentType, categorization: 'true', production: 'true', production_mode: 'webcam', mode: 'image', trial_per_participant_per_class: '100', n_rest: '10', class_questions: 'Who looks happier?|Who looks sadder?', classes: 'happy|sad', dim: '3', lower_bound: '-29', upper_bound: '29', n_chain: '1', proposal_cov: '15', attention_check: 'true', attention_check_rate: '0.008', attention_check_dir: 'public/stimuli/attention_check', gatekeeper: 'Custom', gatekeeper_dir: 'models/gatekeepers', stuck_patience: '20', group_table_name: 'group', consensus_n: '3', prolific: 'false' }; // Merge with environment variables for (const [key, defaultValue] of Object.entries(this.defaults)) { this.config[key] = process.env[key] || defaultValue; } } set(key, value) { this.config[key] = value; } get(key) { return this.config[key]; } getArray(key, separator = '|') { const value = this.get(key); return value ? value.split(separator) : []; } getBoolean(key) { return this.get(key) === 'true'; } getNumber(key) { return parseInt(this.get(key), 10); } getTextContent(filename) { // First look in experiment's customized_text directory let filePath = path.join(this.experimentPath, 'custom_text', filename); if (!fs.existsSync(filePath)) { // Fall back to package default texts filePath = path.join(__dirname, '..', 'src', 'static', 'custom_text', filename) } if (fs.existsSync(filePath)) { return fs.readFileSync(filePath, 'utf8'); } return `Text file not found: ${filename}`; } validate() { const errors = []; // Add validation rules based on your requirements if (!['blockwise-MCMCP', 'consensus-MCMCP', 'GSP', 'GSP-prior'].includes(this.get('experiment'))) { errors.push(`Invalid experiment type: ${this.get('experiment')}`); } if (this.getNumber('trial_per_participant_per_class') < 1) { errors.push('trial_per_participant_per_class must be positive'); } return errors; } } module.exports = { ExperimentConfig };