Skip to content

Instantly share code, notes, and snippets.

@alexanderwallin
Created August 22, 2017 11:43
Show Gist options
  • Save alexanderwallin/a6b61426e245e50b31280dfd1b7dc1a9 to your computer and use it in GitHub Desktop.
Save alexanderwallin/a6b61426e245e50b31280dfd1b7dc1a9 to your computer and use it in GitHub Desktop.
import { map, max, mean, sortBy, values } from 'lodash'
import { TEST_FREQUENCIES } from 'src/constants.js'
const SPLtoHLMap = {
'125': 45,
'250': 27,
'500': 13.5,
'1000': 7.5,
'2000': 9,
'4000': 12,
'8000': 15.5,
}
/**
* Maps hearing loss code curve types to relative audiogram values
*/
const CodeCurveTypeScales = {
A: [0, 0, 0, 0, 0, 1 / 2, 1],
B: [0, 0, 0, 0, 1 / 2, 1, 1],
C: [0, 0, 0, 1 / 2, 1, 1, 1],
D: [0, 0, 1 / 2, 1, 1, 1, 1],
E: [0, 1 / 2, 1, 1, 1, 1, 1],
F: [0, 1 / 6, 2 / 6, 3 / 6, 4 / 6, 5 / 6, 1],
G: [0, 0, 1 / 5, 2 / 5, 3 / 5, 4 / 5, 1],
H: [0, 0, 0, 1 / 4, 2 / 4, 3 / 4, 1],
I: [0, 0, 0, 0, 1 / 3, 2 / 3, 1],
}
/**
* Maps hearing loss code severity levels to a maximum dB HL value
*/
const CodeSeverityValues = {
'0': 10,
'1': 21,
'2': 33,
'3': 48,
'4': 63,
'5': 81,
'6': 91,
}
function convertSPLtoHL(frequency, dbSPL) {
return dbSPL - SPLtoHLMap[frequency]
}
function getAverageDistance(arr1, arr2) {
return mean(arr1.map((x, i) => Math.abs(x - arr2[i])))
}
export function calculateAudiogramFromHearingTestResult(earVolumes) {
return TEST_FREQUENCIES.map(frequency => earVolumes[frequency])
.map(frequencyResults => mean(values(frequencyResults)))
.map((volume, i) => convertSPLtoHL(TEST_FREQUENCIES[i], volume))
.reduce(
(aggr, dbHL) => (aggr.length === 0 ? [dbHL] : [...aggr, dbHL - aggr[0]]),
[]
)
.map((dbHL, i) => (i === 0 ? 0 : dbHL))
}
export function calculateHearingLossCodeFromHearingTestResult(earVolumes) {
const audiogram = calculateAudiogramFromHearingTestResult(earVolumes)
const highestAudiogramValue = max(audiogram)
const normalizedAudiogram = audiogram.map(x => x / highestAudiogramValue)
const distances = map(CodeCurveTypeScales, (scaleValues, scaleName) => ({
scaleName,
distance: getAverageDistance(normalizedAudiogram, scaleValues),
}))
const closestScale = distances.reduce(
(closest, comparison) =>
comparison.distance < closest.distance ? comparison : closest,
distances[0]
)
const severities = map(CodeSeverityValues, (severityValue, severityKey) => ({
severityKey,
closeness: Math.abs(severityValue - highestAudiogramValue),
}))
const closestSeverity = sortBy(severities, x => x.closeness)[0]
return `${closestScale.scaleName}${closestSeverity.severityKey}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment