audiogram-beta/src/store.js

305 lines
10 KiB
JavaScript
Raw Normal View History

2022-12-10 10:27:23 -05:00
import React from "react";
2022-12-05 23:22:37 -05:00
2022-12-03 09:36:01 -05:00
const size = 1/6;
2022-12-04 16:19:22 -05:00
/** @type {Array<Store.ColumnMapping>} */
2022-11-27 23:56:37 -05:00
export const ColumnMapping = [
[ 125, size*0.0, true ],
[ 250, size*1.0, true ],
[ 500, size*2.0, true ],
[1000, size*3.0, true ],
[2000, size*4.0, true ],
[3000, size*4.5, false],
[4000, size*5.0, true ],
[6000, size*5.5, false],
[8000, size*6.0, true ]
];
2022-12-10 09:56:35 -05:00
/** Looks up a frequency in ColumnMapping
* @type {(inFrequency:number)=>Store.ColumnMapping|false} */
2022-11-27 23:56:37 -05:00
export const ColumnLookup =(inFrequency)=>
{
for(let i=0; i<ColumnMapping.length; i++)
2022-11-24 12:16:57 -05:00
{
2022-11-27 23:56:37 -05:00
const map = ColumnMapping[i];
if(map[0] == inFrequency){ return map; }
2022-11-24 12:16:57 -05:00
}
2022-11-27 23:56:37 -05:00
return false;
2022-11-28 18:42:41 -05:00
};
2022-12-10 13:24:31 -05:00
/** @type {(inDecimal:number)=>string} */
const Perc =(inDecimal)=> `${inDecimal*100}%`;
2022-12-10 09:56:35 -05:00
/** Creates a new Store.Context object that contain the current selections
* @type {(inState:Store.State, inTest?:Store.Test)=>Store.Context} */
2022-12-10 00:33:38 -05:00
const Reselect =(inState, inTest)=>
2022-11-28 18:42:41 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.Context} */
const output = { Test:inTest??inState.Live.Test };
const column = ColumnMapping[inState.Freq.Value];
2022-12-10 09:56:35 -05:00
if(column && output.Test)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
const hz = column[0];
2022-12-10 09:56:35 -05:00
for(let i=0; i<output.Test.Plot.length; i++)
2022-11-27 23:56:37 -05:00
{
2022-12-10 09:56:35 -05:00
const plot = output.Test.Plot[i];
2022-12-10 00:33:38 -05:00
if(plot.Hz == hz)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
output.Freq = plot;
output.Mark = plot[`User${inState.Chan.Value ? "R" : "L"}`];
2022-11-27 23:56:37 -05:00
}
}
2022-11-28 10:00:18 -05:00
}
2022-12-10 00:33:38 -05:00
return output;
2022-11-28 10:00:18 -05:00
};
2022-12-03 09:36:01 -05:00
2022-12-10 09:56:35 -05:00
/** Creates a new Store.DrawGroup object for the given Test and settings
* @type {(inTest:Store.Test|undefined, inChan:number, inStim:Store.Range, inIsUser:boolean)=>Store.DrawGroup} */
2022-12-10 00:33:38 -05:00
const Redraw =(inTest, inChan, inStim, inIsUser)=>
2022-12-03 09:36:01 -05:00
{
2022-12-04 16:19:22 -05:00
/** @type {Store.DrawGroup} */
2022-12-03 13:32:04 -05:00
const output = {Points:[], Paths:[]};
2022-12-10 00:33:38 -05:00
if(inTest)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
let plot;
for(let i=0; i<inTest.Plot.length; i++)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
plot = inTest.Plot[i];
2022-12-10 09:56:35 -05:00
const mark = plot[`${inIsUser ? "User" : "Test"}${inChan ? "R" : "L"}`]
2022-12-10 00:33:38 -05:00
if(mark)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
const lookup = ColumnLookup(plot.Hz);
if(lookup)
{
/** @type {Store.DrawPoint} */
const point = {
2022-12-10 13:24:31 -05:00
X: Perc(lookup[1]),
Y: Perc((mark.Stim - inStim.Min)/(inStim.Max - inStim.Min)),
2022-12-10 00:33:38 -05:00
Mark: mark
};
output.Points.push(point);
}
2022-12-03 09:36:01 -05:00
}
}
2022-12-10 00:33:38 -05:00
for(let i=1; i<output.Points.length; i++)
2022-12-05 23:22:37 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.DrawLine} */
const line = {Head:output.Points[i-1], Tail:output.Points[i]};
2022-12-10 13:24:31 -05:00
if(line.Head.Mark?.Resp && line.Tail.Mark?.Resp)
2022-12-10 00:33:38 -05:00
{
output.Paths.push(line);
}
2022-12-05 23:22:37 -05:00
}
}
2022-12-03 13:32:04 -05:00
return output;
2022-12-10 13:24:31 -05:00
};
/** Create a new cursor position from the state
* @type {(inState:Store.State)=>Store.DrawPoint} */
const Reposition =(inState)=> ({
X: Perc(ColumnMapping[inState.Freq.Value][1]),
Y: Perc((inState.Stim.Value - inState.Stim.Min)/(inState.Stim.Max - inState.Stim.Min))
});
2022-12-03 09:36:01 -05:00
2022-12-04 16:19:22 -05:00
/** @type {Store.Reducer} */
2022-11-28 10:00:18 -05:00
export function Reducer(inState, inAction)
{
const clone = {...inState};
2022-11-28 18:42:41 -05:00
const {Name, Data} = inAction;
2022-11-28 10:00:18 -05:00
2022-11-28 18:42:41 -05:00
if(Name == "Test")
{
2022-12-10 14:45:15 -05:00
const test = clone.Test[Data];
if(test)
2022-12-10 13:24:31 -05:00
{
2022-12-10 14:45:15 -05:00
clone.TestIndex = Data;
clone.Live = Reselect(clone, test);
clone.Draw =
{
Cross: Reposition(clone),
UserL: Redraw(test, 0, clone.Stim, true ),
UserR: Redraw(test, 1, clone.Stim, true ),
TestL: Redraw(test, 0, clone.Stim, false),
TestR: Redraw(test, 1, clone.Stim, false)
};
}
2022-11-28 18:42:41 -05:00
}
else if (Name == "Mark")
2022-11-28 10:00:18 -05:00
{
2022-12-03 19:05:26 -05:00
if(clone.Live.Test && clone.Live.Freq)
2022-11-28 18:42:41 -05:00
{
2022-12-10 00:33:38 -05:00
const key = clone.Chan.Value == 0 ? "UserL" : "UserR";
clone.Live.Mark = Data !== null ? {Stim:clone.Stim.Value, Resp:Data} : undefined;
clone.Live.Freq[key] = clone.Live.Mark;
clone.Draw[key] = Redraw(clone.Live.Test, clone.Chan.Value, clone.Stim, true);
2022-11-28 18:42:41 -05:00
}
}
else if( Name=="Stim" || Name=="Chan" || Name=="Freq")
{
2022-12-10 11:57:45 -05:00
const tone = clone[Name];
2022-12-03 21:40:23 -05:00
tone.Value += Data*tone.Step;
2022-12-10 09:56:35 -05:00
tone.Value = Math.max(tone.Value, tone.Min);
tone.Value = Math.min(tone.Value, tone.Max);
2022-12-10 13:24:31 -05:00
clone.Draw.Cross = Reposition(clone);
2022-12-10 11:57:45 -05:00
if(Name != "Stim")
{
clone.Live = Reselect(clone);
}
2022-11-27 23:56:37 -05:00
}
2022-12-12 15:57:49 -05:00
else if (Name == "ShowCursor")
{
clone.Show.Cursor = Data;
}
else if (Name == "ShowAnswer")
{
clone.Show.Answer = Data;
}
2022-11-28 18:42:41 -05:00
2022-11-27 23:56:37 -05:00
return clone;
2022-12-05 23:22:37 -05:00
}
2022-12-10 09:56:35 -05:00
/** @type {Store.State} */
2022-12-10 13:24:31 -05:00
export const Initial = Reducer(
2022-12-10 09:56:35 -05:00
{
Chan: { Min:0, Max:1, Value:0, Step:1 },
2022-12-12 16:16:38 -05:00
Freq: { Min:2, Max:8, Value:3, Step:1 },
2022-12-10 09:56:35 -05:00
Stim: { Min:-10, Max:120, Value:30, Step:5 },
Live:
{
Test: undefined,
Freq: undefined,
Mark: undefined
},
Draw:
{
UserL:{Points:[], Paths:[]},
UserR:{Points:[], Paths:[]},
TestL:{Points:[], Paths:[]},
TestR:{Points:[], Paths:[]}
},
2022-12-12 15:57:49 -05:00
Show:
{
Cursor:true,
Answer:false
},
2022-12-10 14:45:15 -05:00
TestIndex: 0,
Test: [
2022-12-10 09:56:35 -05:00
{
Name: "Patient A Asymmetric Notch",
Plot:
[
2022-12-12 16:16:38 -05:00
{ Hz: 500, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 2000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 20, Resp: true } },
{ Hz: 3000, TestL: { Stim: 30, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 4000, TestL: { Stim: 40, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 6000, TestL: { Stim: 35, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 8000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 15, Resp: true } }
]
},
{
Name: "Patient B High Freq Hearing Loss",
Plot:
[
{ Hz: 500, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 2000, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 15, Resp: true } },
{ Hz: 3000, TestL: { Stim: 25, Resp: true }, TestR: { Stim: 20, Resp: true } },
{ Hz: 4000, TestL: { Stim: 35, Resp: true }, TestR: { Stim: 35, Resp: true } },
2022-12-10 09:56:35 -05:00
{ Hz: 6000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
2022-12-12 16:16:38 -05:00
{ Hz: 8000, TestL: { Stim: 80, Resp: true }, TestR: { Stim: 75, Resp: true } }
]
},
{
Name: "Patient C Unilateral Hearing Loss",
Plot:
[
{ Hz: 500, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 1000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 50, Resp: true } },
{ Hz: 2000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 65, Resp: true } },
{ Hz: 3000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 70, Resp: true } },
{ Hz: 4000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 65, Resp: true } },
{ Hz: 6000, TestL: { Stim: 25, Resp: true }, TestR: { Stim: 60, Resp: true } },
{ Hz: 8000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 45, Resp: true } }
2022-12-10 09:56:35 -05:00
]
2022-12-10 14:45:15 -05:00
},
{
2022-12-12 16:16:38 -05:00
Name: "Patient D Normal Hearing",
2022-12-10 14:45:15 -05:00
Plot:
[
2022-12-12 16:16:38 -05:00
{ Hz: 500, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 0, Resp: true }, TestR: { Stim: 5, Resp: true } },
{ Hz: 2000, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 5, Resp: true } },
{ Hz: 3000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 4000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 15, Resp: true } },
{ Hz: 6000, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 8000, TestL: { Stim: 0, Resp: true }, TestR: { Stim: 5, Resp: true } }
2022-12-10 14:45:15 -05:00
]
2022-12-10 09:56:35 -05:00
}
]
2022-12-10 13:24:31 -05:00
}, {Name:"Test", Data:0});
2022-12-05 23:22:37 -05:00
/** @type {preact.Context<Store.Binding>} */
export const Context = React.createContext([Initial, (_a)=>{}]);
2022-12-10 09:56:35 -05:00
2022-12-05 23:22:37 -05:00
/** @type {(props:{children:preact.ComponentChildren})=>preact.VNode} */
export const Provider =(props)=>
{
/** @type {Store.Binding} */
2022-12-10 13:24:31 -05:00
const reducer = React.useReducer(Reducer, Initial);
2022-12-05 23:22:37 -05:00
return React.createElement(Context.Provider, {value:reducer, children:props.children});
2022-12-10 09:56:35 -05:00
};
2022-12-05 23:22:37 -05:00
/** @type {()=>Store.Binding} */
2022-12-11 14:56:43 -05:00
export const Consumer =()=> React.useContext(Context);
/** @type {(inTest:Store.Test|undefined)=>Store.Grade} */
export const Grade =(inTest)=>
{
/** @type {Store.Grade} */
const output = { Total:0, Done:0, Score:0 };
/** @type {(inGoal:number, inResult:number)=>number} */
const Mapper =(inGoal, inResult)=>
{
const err = Math.abs(inGoal-inResult);
if(err == 0){ return 1; }
else if(err > 0 && err <= 5){ return 0.9; }
else if(err > 5 && err <= 10){ return 0.7; }
else if(err > 10 && err <= 15){ return 0.2; }
else{ return 0; }
}
if(inTest)
{
for(let i=0; i<inTest.Plot.length; i++)
{
const {TestL, TestR, UserL, UserR} = inTest.Plot[i];
if(TestL)
{
output.Total ++;
if(UserL)
{
output.Done++;
output.Score += Mapper(TestL.Stim, UserL.Stim);
}
}
if(TestR)
{
output.Total ++;
if(UserR)
{
output.Done++;
output.Score += Mapper(TestR.Stim, UserR.Stim);
}
}
}
}
if(output.Done > 0)
{
output.Score = Math.floor((output.Score/output.Done) * 10000)/100;
}
return output;
}