Skip to content

Commit

Permalink
Merge pull request #24 from yeatmanlab/enh/fixed-order-method
Browse files Browse the repository at this point in the history
Fixed order method
  • Loading branch information
richford authored Aug 23, 2024
2 parents 3c66e76 + d1523fc commit a8dd604
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
22 changes: 5 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Stimulus } from '../type';
import seedrandom from 'seedrandom';

describe('Cat', () => {
let cat1: Cat, cat2: Cat, cat3: Cat, cat4: Cat, cat5: Cat, cat6: Cat, cat7: Cat;
let cat1: Cat, cat2: Cat, cat3: Cat, cat4: Cat, cat5: Cat, cat6: Cat, cat7: Cat, cat8: Cat;
let rng = seedrandom();
beforeEach(() => {
cat1 = new Cat();
Expand Down Expand Up @@ -52,6 +52,8 @@ describe('Cat', () => {
],
[0, 0],
);

cat8 = new Cat({ nStartItems: 0, itemSelect: 'FIXED' });
});

const s1: Stimulus = { difficulty: 0.5, c: 0.5, word: 'looking' };
Expand Down Expand Up @@ -116,6 +118,13 @@ describe('Cat', () => {
expect(received).toEqual(expected);
});

it('correctly suggests the next item (fixed method)', () => {
expect(cat8.itemSelect).toBe('fixed');
const expected = { nextStimulus: s1, remainingStimuli: [s2, s3, s4, s5] };
const received = cat8.findNextItem(stimuli);
expect(received).toEqual(expected);
});

it('correctly suggests the next item (random method)', () => {
let received;
const stimuliSorted = stimuli.sort((a: Stimulus, b: Stimulus) => a.difficulty - b.difficulty);
Expand Down
26 changes: 23 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Cat {

private static validateItemSelect(itemSelect: string) {
const lowerItemSelect = itemSelect.toLowerCase();
const validItemSelect: Array<string> = ['mfi', 'random', 'closest'];
const validItemSelect: Array<string> = ['mfi', 'random', 'closest', 'fixed'];
if (!validItemSelect.includes(lowerItemSelect)) {
throw new Error('The itemSelector you provided is not in the list of valid methods');
}
Expand All @@ -116,7 +116,7 @@ export class Cat {

private static validateStartSelect(startSelect: string) {
const lowerStartSelect = startSelect.toLowerCase();
const validStartSelect: Array<string> = ['random', 'middle']; // TO DO: add staircase
const validStartSelect: Array<string> = ['random', 'middle', 'fixed']; // TO DO: add staircase
if (!validStartSelect.includes(lowerStartSelect)) {
throw new Error('The startSelect you provided is not in the list of valid methods');
}
Expand Down Expand Up @@ -213,9 +213,10 @@ export class Cat {
if (this.nItems < this.nStartItems) {
selector = this.startSelect;
}
if (selector !== 'mfi') {
if (selector !== 'mfi' && selector !== 'fixed') {
// for mfi, we sort the arr by fisher information in the private function to select the best item,
// and then sort by difficulty to return the remainingStimuli
// for fixed, we want to keep the corpus order as input
arr.sort((a: Stimulus, b: Stimulus) => a.difficulty - b.difficulty);
}

Expand All @@ -226,6 +227,8 @@ export class Cat {
return this.selectorClosest(arr);
} else if (selector === 'random') {
return this.selectorRandom(arr);
} else if (selector === 'fixed') {
return this.selectorFixed(arr);
} else {
return this.selectorMFI(arr);
}
Expand Down Expand Up @@ -289,6 +292,23 @@ export class Cat {
};
}

/**
* Picks the next item in line from the given list of stimuli.
* It grabs the first item from the list, removes it, and then returns it along with the rest of the list.
*
* @param arr - The list of stimuli to choose from.
* @returns {Object} - An object with the next item and the updated list.
* @returns {Stimulus} return.nextStimulus - The item that was picked from the list.
* @returns {Stimulus[]} return.remainingStimuli - The list of what's left after picking the item.
*/
private selectorFixed(arr: Stimulus[]) {
const nextItem = arr.shift() ?? null;
return {
nextStimulus: nextItem,
remainingStimuli: arr,
};
}

/**
* return a random integer between min and max
* @param min - The minimum of the random number range (include)
Expand Down
2 changes: 1 addition & 1 deletion validation/simulation/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function convertListToCSV(list) {
const rows = list.map(obj => keys.map(key => obj[key]).join(',')).join('\n');
return header + rows;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const csvContent = await convertListToCSV(await shuffleAnswerRobot(answerRobot, 1));

// export const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8' });

0 comments on commit a8dd604

Please sign in to comment.