Difference between revisions of "MediaWiki:Gadget-calculator-drugs-core.js"
From WikiAnesthesia
Chris Rishel (talk | contribs) |
Chris Rishel (talk | contribs) |
||
Line 413: | Line 413: | ||
// dosage will be converted to optional. | // dosage will be converted to optional. | ||
var requiredInputData = new mw.calculators.objectClasses.CalculationData(); | var requiredInputData = new mw.calculators.objectClasses.CalculationData(); | ||
// Need a way to tell the first iteration of the loop to initialize the required variables to a value that | |||
// is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't | |||
// reinitialize). | |||
for( var iDataType in requiredInputData ) { | |||
requiredInputData[ iDataType ].required = null; | |||
} | |||
for( var iDosage in this.drug.dosages ) { | for( var iDosage in this.drug.dosages ) { | ||
Line 420: | Line 427: | ||
for( var iDataType in requiredInputData ) { | for( var iDataType in requiredInputData ) { | ||
if( requiredInputData[ iDataType ].required === null) { | |||
requiredInputData[ iDataType ].required = inputData[ iDataType ].required; | |||
} else { | |||
requiredInputData[ iDataType ].required = requiredInputData[ iDataType ].required.filter( function( index ) { | |||
return dosageInputData.required.indexOf( n ) !== -1; | |||
} ); | |||
} | |||
console.log(requiredInputData[ iDataType ].required); | console.log(requiredInputData[ iDataType ].required); | ||
} | } | ||
} | } |
Revision as of 21:51, 9 August 2021
/** * @author Chris Rishel */ ( function() { var DEFAULT_DRUG_COLOR = 'default'; var DEFAULT_DRUG_POPULATION = 'general'; /** * Define units */ mw.calculators.addUnitsBases( { concentration: { toString: function( units ) { units = units.replace( ' pct', '%' ); return units; } } } ); mw.calculators.addUnits( { mcg: { definition: '1 ug' }, pct: { baseName: 'concentration', definition: '10 mg/mL' } } ); /** * DrugColor */ mw.calculators.drugColors = {}; mw.calculators.addDrugColors = function( drugColorData ) { var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData ); for( var drugColorId in drugColors ) { mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ]; } }; mw.calculators.getDrugColor = function( drugColorId ) { if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) { return mw.calculators.drugColors[ drugColorId ]; } else { return null; } }; /** * Class DrugColor * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugColor} * @constructor */ mw.calculators.objectClasses.DrugColor = function( propertyValues ) { var properties = { required: [ 'id' ], optional: [ 'parentColor', 'primaryColor', 'highlightColor', 'striped' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); if( !this.primaryColor && !this.parentColor ) { throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' ); } }; mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); mw.calculators.objectClasses.DrugColor.getParentDrugColor = function() { if( !this.parentColor ) { return null; } var parentDrugColor = mw.calculators.getDrugColor( this.parentColor ); if( !parentDrugColor ) { throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' ); } return parentDrugColor; }; mw.calculators.objectClasses.DrugColor.getHighlightColor = function() { if( this.highlightColor ) { return this.highlightColor; } else if( this.parentColor ) { return this.getParentDrugColor().getHighlightColor(); } }; mw.calculators.objectClasses.DrugColor.getPrimaryColor = function() { if( this.primaryColor ) { return this.primaryColor; } else if( this.parentColor ) { return this.getParentDrugColor().getPrimaryColor(); } }; mw.calculators.objectClasses.DrugColor.isStriped = function() { if( this.striped !== null ) { return this.striped; } else if( this.parentColor ) { return this.getParentDrugColor().isStriped(); } }; /** * DrugPopulation */ mw.calculators.drugPopulations = {}; mw.calculators.addDrugPopulations = function( drugPopulationData ) { var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData ); for( var drugPopulationId in drugPopulations ) { mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ]; } }; mw.calculators.getDrugPopulation = function( drugPopulationId ) { if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) { return mw.calculators.drugPopulations[ drugPopulationId ]; } else { return null; } }; /** * Class DrugPopulation * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugPopulation} * @constructor */ mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) { var properties = { required: [ 'id', 'name' ], optional: [ 'abbreviation', 'variables' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); if( this.variables ) { for( var variableId in this.variables ) { if( !mw.calculators.getVariable( variableId ) ) { throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' ); } } } else { this.variables = {}; } }; mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); mw.calculators.objectClasses.DrugPopulation.prototype.getInputData = function() { var inputData = new mw.calculators.objectClasses.CalculationData(); for( var variableId in this.variables ) { inputData.variables.required.push( variableId ); } return inputData; }; /** * DrugIndication */ mw.calculators.drugIndications = {}; mw.calculators.addDrugIndications = function( drugIndicationData ) { var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData ); for( var drugIndicationId in drugIndications ) { mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ]; } }; mw.calculators.getDrugIndication = function( drugIndicationId ) { if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) { return mw.calculators.drugIndications[ drugIndicationId ]; } else { return null; } }; /** * Class DrugIndication * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugIndication} * @constructor */ mw.calculators.objectClasses.DrugIndication = function( propertyValues ) { var properties = { required: [ 'id', 'name' ], optional: [ 'abbreviation' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); }; mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); /** * Drug */ mw.calculators.drugs = {}; mw.calculators.addDrugs = function( drugData ) { var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData ); for( var drugId in drugs ) { mw.calculators.drugs[ drugId ] = drugs[ drugId ]; } }; mw.calculators.addDrugDosages = function( drugId, drugDosageData ) { var drug = mw.calculators.getDrug( drugId ); if( !drug ) { throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' ); } drug.addDosages( drugDosageData ); }; mw.calculators.getDrug = function( drugId ) { if( mw.calculators.drugs.hasOwnProperty( drugId ) ) { return mw.calculators.drugs[ drugId ]; } else { return null; } }; /** * Class Drug * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.Drug} * @constructor */ mw.calculators.objectClasses.Drug = function( propertyValues ) { var properties = { required: [ 'id', 'name' ], optional: [ 'color' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); if( !this.color ) { this.color = DEFAULT_DRUG_COLOR; } this.dosages = []; this.preparations = []; }; mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); mw.calculators.objectClasses.Drug.prototype.addDosages = function( drugDosageData ) { var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData ); for( var dosageId in dosages ) { dosages[ dosageId ].id = this.dosages.length; this.dosages.push( dosages[ dosageId ] ); } var calculationId = 'drugDosage-' + this.id; var calculation = mw.calculators.getCalculation( calculationId ); if( !calculation ) { var calculationData = {}; calculationData[ calculationId ] = { calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate, drug: this.id, type: 'drug' }; mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' ); calculation = mw.calculators.getCalculation( calculationId ); } }; /** * DrugPreparation */ mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) { if( !mw.calculators.getDrug( drugId ) ) { throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' ); } for( var drugPreparationId in drugPreparationData ) { drugPreparationData[ drugPreparationId ].drug = drugId; } var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData ); for( var drugPreparationId in drugPreparations ) { mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ]; } }; /** * Class DrugPreparation * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugPreparation} * @constructor */ mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) { var properties = { required: [ 'drug', 'id', 'concentration' ], optional: [ 'dilutionRequired', 'commonDilution' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); }; mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); /** * Class DrugDosageCalculation * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugDosageCalculation} * @constructor */ mw.calculators.objectClasses.DrugDosageCalculation = function( propertyValues ) { mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); this.initialize(); }; mw.calculators.objectClasses.DrugDosageCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype ); mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate = function( data ) { console.log( data ); }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.getInputData = function() { var inputData = new mw.calculators.objectClasses.CalculationData(); // Data is only actually required if it is required by every dosage for the drug. // Data marked as required by an individual dosage that does not appear in every // dosage will be converted to optional. var requiredInputData = new mw.calculators.objectClasses.CalculationData(); // Need a way to tell the first iteration of the loop to initialize the required variables to a value that // is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't // reinitialize). for( var iDataType in requiredInputData ) { requiredInputData[ iDataType ].required = null; } for( var iDosage in this.drug.dosages ) { var dosageInputData = this.drug.dosages[ iDosage ].getInputData(); inputData = inputData.merge( dosageInputData ); for( var iDataType in requiredInputData ) { if( requiredInputData[ iDataType ].required === null) { requiredInputData[ iDataType ].required = inputData[ iDataType ].required; } else { requiredInputData[ iDataType ].required = requiredInputData[ iDataType ].required.filter( function( index ) { return dosageInputData.required.indexOf( n ) !== -1; } ); } console.log(requiredInputData[ iDataType ].required); } } console.log(requiredInputData); return inputData; }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() { var labelHtml = this.name; labelHtml = $( '<a>', { href: mw.util.getUrl( this.name ), text: labelHtml } )[ 0 ].outerHTML; return labelHtml; }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() { var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties(); return this.mergeProperties( inheritedProperties, { required: [ 'drug' ], optional: [] } ); }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() { mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this ); var drug = mw.calculators.getDrug( this.drug ); if( !drug ) { throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' ); } this.drug = drug; }; /** * Class DrugDosage * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugDosage} * @constructor */ mw.calculators.objectClasses.DrugDosage = function( propertyValues ) { var properties = { required: [ 'dose', 'id', 'indication' ], optional: [ 'population' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); var drugIndication = mw.calculators.getDrugIndication( this.indication ); if( !drugIndication ) { throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' ); } this.indication = drugIndication; this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION; var drugPopulation = mw.calculators.getDrugPopulation( this.population ); if( !drugPopulation ) { throw new Error( 'Invalid population "' + this.population + '" for drug dosage' ); } this.population = drugPopulation; var drugDoseData = this.dose; this.dose = []; this.addDoses( drugDoseData ); }; mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) { // Each dosage can have one or more associated doses. Ensure this value is an array. if( !Array.isArray( drugDoseData ) ) { drugDoseData = [ drugDoseData ]; } var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData ); for( var doseId in doses ) { doses[ doseId ].id = this.dose.length; this.dose.push( doses[ doseId ] ); } }; mw.calculators.objectClasses.DrugDosage.prototype.getInputData = function() { var inputData = new mw.calculators.objectClasses.CalculationData(); inputData = inputData.merge( this.population.getInputData() ); for( var iDose in this.dose ) { inputData = inputData.merge( this.dose[ iDose ].getInputData() ); } return inputData; }; /** * Class DrugDose * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugDose} * @constructor */ mw.calculators.objectClasses.DrugDose = function( propertyValues ) { mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); var mathProperties = this.getMathProperties(); for( var iMathProperty in mathProperties ) { var mathProperty = mathProperties[ iMathProperty ]; this[ mathProperty ] = this[ mathProperty ] ? math.unit( this[ mathProperty ] ) : null; } if( this.weightCalculation ) { var weightCalculation = mw.calculators.getCalculation( this.weightCalculation ); if( !weightCalculation ) { throw new Error( 'Drug dose references weight calculation "' + this.weightCalculation + '" which is not defined' ); } this.weightCalculation = weightCalculation; } }; mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); mw.calculators.objectClasses.DrugDose.prototype.getInputData = function() { var calculationData = new mw.calculators.objectClasses.CalculationData(); var mathProperties = this.getMathProperties(); // Look at dose properties to identify any variable dependence (e.g. weight-dependence) for( var iMathProperty in mathProperties ) { var mathProperty = mathProperties[ iMathProperty ]; var dosePropertyValue = this[ mathProperty ]; // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future if( dosePropertyValue && dosePropertyValue.formatUnits().match( /\/[\s(]*?kg/ ) && calculationData.variables.required.indexOf( 'weight' ) === -1 ) { calculationData.variables.required.push( 'weight' ); } } if( this.weightCalculation ) { calculationData.calculations.optional.push( this.weightCalculation.id ); } return calculationData; }; mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() { return [ 'dose', 'min', 'max', 'absoluteMin', 'absoluteMax' ]; }; mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() { return { required: [ 'id' ], optional: [ 'absoluteMin', 'absoluteMax', 'dose', 'min', 'max', 'name', 'route', 'weightCalculation' ] }; }; /******* * BEGIN DRUG DATA *******/ /** * DrugColor data */ mw.calculators.addDrugColors( { anticholinergic: { primaryColor: '#00ac8c' }, benzodiazepine: { primaryColor: '#ff6c2f' }, benzodiazepineReversal: { parentColor: 'benzodiazepine', striped: true }, cardiovascularAgonist: { primaryColor: '#ba93df' }, cardiovascularAntagonist: { parentColor: 'cardiovascularAgonist', striped: true }, default: { primaryColor: '#fff' }, desflurane: { primaryColor: '#0ab8fd' }, enflurane: { primaryColor: '#f58733' }, epinephrine: { parentColor: 'cardiovascularAntagonist', highlightColor: '#000' }, halothane: { primaryColor: '#b20107' }, isoflurane: { primaryColor: '#ca7fc0' }, localAnesthetic: { primaryColor: '#dad9d6' }, neuromuscularBlocker: { primaryColor: '#fe5442' }, neuromuscularBlockerReversal: { parentColor: 'neuromuscularBlocker', striped: true }, nitrousOxide: { primaryColor: '#2d549f' }, opioid: { primaryColor: '#6cd1ef' }, opioidReversal: { parentColor: 'opioid', striped: true }, sedativeHypnotic: { primaryColor: '#ffe800' }, sevoflurane: { primaryColor: '#f8da00' }, succinylcholine: { parentColor: 'neuromuscularBlocker', highlightColor: '#000' } } ); /** * DrugPopulation data */ mw.calculators.addDrugPopulations( { general: { name: 'General', abbreviation: 'Gen.' }, neonatal: { name: 'Neonatal', abbreviation: 'Neo.', variables: { age: { max: '0 yo' } } }, pediatric: { name: 'Pediatric', abbreviation: 'Ped.', variables: { age: { min: '0 yo', max: '17.9 yo' } } }, elderly: { name: 'Elderly', abbreviation: 'Eld.', variables: { age: { min: '65 yo' } } } } ); /** * DrugIndication data */ mw.calculators.addDrugIndications( { abxProphylaxis: { name: 'Antimicrobial prophylaxis', abbreviation: 'Abx.' }, generalAnesthesia: { name: 'General anesthesia', abbreviation: 'GA' }, mac: { name: 'Monitored anesthesia care', abbreviation: 'MAC' } } ); /** * Drug data */ /** * Cefazolin */ mw.calculators.addDrugs( { cefazolin: { name: 'Cefazolin' } } ); mw.calculators.addDrugDosages( 'cefazolin', [ { indication: 'abxProphylaxis', population: 'general', dose: { dose: '2 g' } } ] ); /** * Ketamine */ mw.calculators.addDrugs( { ketamine: { name: 'Ketamine', color: 'sedativeHypnotic' } } ); mw.calculators.addDrugPreparations( 'ketamine', [ { concentration: '10 mg/mL' }, { concentration: '50 mg/mL' }, { concentration: '100 mg/mL' } ] ); /** * Lidocaine */ mw.calculators.addDrugs( { lidocaine: { name: 'Lidocaine', color: 'localAnesthetic' } } ); mw.calculators.addDrugPreparations( 'lidocaine', [ { concentration: '1 pct' }, { concentration: '2 pct' } ] ); /** * Propofol */ mw.calculators.addDrugs( { propofol: { name: 'Propofol', color: 'sedativeHypnotic' } } ); mw.calculators.addDrugPreparations( 'propofol', [ { concentration: '10 mg/mL' } ] ); mw.calculators.addDrugDosages( 'propofol', [ { indication: 'generalAnesthesia', population: 'general', dose: [ { name: 'Induction', min: '1 mg/kg', max: '2.5 mg/kg', weightCalculation: 'lbw' }, { name: 'Maintenance', min: '100 mcg/kg/min', max: '200 mcg/kg/min', route: 'IV' } ] }, { indication: 'generalAnesthesia', population: 'pediatric', dose: [ { name: 'Induction', min: '2.5 mg/kg', max: '3.5 mg/kg', weightCalculation: 'lbw' }, { name: 'Maintenance', min: '125 mcg/kg/min', max: '300 mcg/kg/min' } ] }, { indication: 'generalAnesthesia', population: 'elderly', dose: [ { name: 'Induction', min: '1 mg/kg', max: '1.5 mg/kg', weightCalculation: 'lbw' }, { name: 'Maintenance', min: '50 mcg/kg/min', max: '100 mcg/kg/min' } ] }, { indication: 'mac', population: 'general', dose: [ { min: '25 mcg/kg/min', max: '75 mcg/kg/min' } ] } ] ); /** * DrugDosage * * Structure is: * * drugId: { * indicationId: { * doseId: { * * } * } * } */ var drugDosages = { cefazolin: { abxProphylaxis: { general: { population: 'general', dose: '2 g' }, general120kg: { population: { id: 'general', variables: { weight: { min: '120 kg' } } }, dose: '3 g' }, pediatric: { dose: { absoluteMax: '2 g', dose: '30 mg/kg' } }, pediatric120kg: { population: { id: 'pediatric', variables: { weight: { min: '120 kg' } } }, dose: { dose: '3 g' } } } } }; }() );