Difference between revisions of "MediaWiki:Gadget-calculator-anatomyPhysiology.js"

From WikiAnesthesia
 
(105 intermediate revisions by the same user not shown)
Line 1: Line 1:
( function() {
( function() {
     var moduleId = 'patientStats';
     mw.calculators.addUnitsBases( {
        bpm: {
            toString: function( units ) {
                units = units.replace( 'bpm', 'beats/min' );


    mw.calculators.addUnitsBases( {
                return units;
            }
        },
         hgb: {
         hgb: {
             toString: function( units ) {
             toString: function( units ) {
                 units = units.replace( 'hgbperdL', '/dL' );
                 units = units.replace( 'hgbperdL', '/dL' );
                 units = units.replace( 'pcthct', '%' );
                 units = units.replace( /\s?pcthct/, '%' );
 
                return units;
            }
        },
        o2: {
            toString: function( units ) {
                units = units.replace( /\s?pcto2/, '%' );
 
                return units;
            }
        },
        temperature: {
            toString: function( units ) {
                units = units.replace( 'deg', '°' );


                 return units;
                 return units;
Line 14: Line 33:


     mw.calculators.addUnits( {
     mw.calculators.addUnits( {
         ghgbperdL: {
         bpm: {
             baseName: 'hgb',
             baseName: 'bpm'
            prefixes: 'short'
         },
         },
         pcthct: {
         pcthct: {
            baseName: 'hgb'
        },
        pcto2: {
            baseName: 'o2'
        },
        ghgbperdL: {
             baseName: 'hgb',
             baseName: 'hgb',
             definition: '3 ghgbperdL'
            prefixes: 'short',
             definition: '3 pcthct'
         }
         }
     } );
     } );


     mw.calculators.addVariables( {
     mw.calculators.addVariables( {
         hct: {
         fiO2: {
             name: 'Current hematocrit / hemoglobin',
             name: 'FiO<sub>2</sub>',
            type: 'number',
            abbreviation: 'FiO<sub>2</sub>',
            minValue: '10 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '21 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        heartRate: {
            name: 'Heart rate',
            type: 'number',
            abbreviation: 'HR',
            defaultValue: '60 bpm',
            maxLength: 4,
            units: [
                'bpm'
            ]
        },
        hgb: {
            name: 'Hemoglobin/hematocrit',
            type: 'number',
            abbreviation: 'HgB',
            minValue: '3 ghgbperdL',
            maxValue: '25 ghgbperdL',
            defaultValue: '13 ghgbperdL',
            maxLength: 4,
            units: [
                'pcthct',
                'ghgbperdL'
            ]
        },
        paCO2: {
            name: 'PaCO<sub>2</sub>',
            type: 'number',
            abbreviation: 'PaCO<sub>2</sub>',
            minValue: '20 mmHg',
            defaultValue: '40 mmHg',
            maxLength: 3,
            units: [
                'mmHg'
            ]
        },
        paO2: {
            name: 'PaO<sub>2</sub>',
            type: 'number',
            abbreviation: 'PaO<sub>2</sub>',
            minValue: '25 mmHg',
            defaultValue: '100 mmHg',
            maxLength: 3,
            units: [
                'mmHg'
            ]
        },
        pAtm: {
            name: 'Atmospheric pressure',
            type: 'number',
            abbreviation: 'P<sub>atm</sub>',
            minValue: '0 mmHg',
            defaultValue: '760 mmHg',
            maxLength: 4,
            units: [
                'mmHg'
            ]
        },
        saO2: {
            name: 'SaO<sub>2</sub>',
            type: 'number',
            abbreviation: 'SaO<sub>2</sub>',
            minValue: '25 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '100 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        smvO2: {
            name: 'SmvO<sub>2</sub>',
            type: 'number',
            abbreviation: 'SmvO<sub>2</sub>',
            minValue: '25 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '75 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        raceSpirometry: {
            name: 'Race',
            type: 'string',
            abbreviation: 'Race',
            defaultValue: 'unknown',
            options: {
                unknown: 'Unknown',
                black: 'Black',
                caucasian: 'Caucasian',
                mexican: 'Mexican-American'
            }
        },
        temperature: {
            name: 'Temperature',
            type: 'number',
            abbreviation: 'Temp',
            minValue: '20 degC',
            maxValue: '44 degC',
            defaultValue: '37 degC',
            maxLength: 5,
            units: [
                'degC',
                'degF'
            ]
        },
        weightBasedTidalVolumePerKgMin: {
            name: 'Minimum tidal volume',
             type: 'number',
             type: 'number',
             abbreviation: 'Hct/hgb',
             abbreviation: 'Min TV',
             defaultValue: '45',
            minValue: '3 mL/kgwt',
             maxLength: 4
            maxValue: '12 mL/kgwt',
             defaultValue: '6 mL/kgwt',
             maxLength: 2,
            units: [
                'mL/kgwt'
            ]
         },
         },
         minHct: {
         weightBasedTidalVolumePerKgMax: {
             name: 'Minimum allowable hematocrit/hemoglobin',
             name: 'Maximum tidal volume',
             type: 'number',
             type: 'number',
             abbreviation: 'Min hct/hgb',
             abbreviation: 'Max TV',
             defaultValue: '21',
            minValue: '3 mL/kgwt',
             maxLength: 4
            maxValue: '12 mL/kgwt',
             defaultValue: '8 mL/kgwt',
             maxLength: 2,
            units: [
                'mL/kgwt'
            ]
         }
         }
     } );
     } );


 
    // Force re-render of ibw and lbw. This is necessary to remove the additional inputs for patient variables from the
    // calculation which are now provided by the patient input toolbar.
    mw.calculators.calculations.ibw.render();
    mw.calculators.calculations.lbw.render();


     mw.calculators.addCalculations( {
     mw.calculators.addCalculations( {
         bladeMacSize: {
         bmi: {
             name: 'Laryngoscope blade size (MAC)',
             name: 'Body mass index',
             abbreviation: 'Blade (MAC)',
            abbreviation: 'BMI',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 0,
            units: 'kg/m^2',
            formula: '<math>\\mathrm{BMI} = \\frac{\\mathrm{mass_{kg}}}{{(\\mathrm{height_{m}}})^2}</math>',
            link: '[[Body mass index]]',
            references: [],
            calculate: function( data ) {
                return data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 );
            }
        },
        bsa: {
            name: 'Body surface area',
             abbreviation: 'BSA',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 2,
            units: 'm^2',
            formula: '<math>\\mathrm{BSA} = \\sqrt{\\frac{\\mathrm{weight_{kg}}*\\mathrm{height_{cm}}}{3600}}</math>',
            link: false,
            references: [
                'Mosteller RD. Simplified calculation of body-surface area. N Engl J Med. 1987 Oct 22;317(17):1098. doi: 10.1056/NEJM198710223171717. PMID: 3657876.'
            ],
            calculate: function( data ) {
                return Math.sqrt( data.height.toNumber( 'cm' ) * data.weight.toNumber( 'kgwt' ) / 3600 );
            }
        },
        systolicBloodPressure: {
            name: 'Systolic blood pressure',
            abbreviation: 'SBP',
             data: {
             data: {
                 variables: {
                 variables: {
Line 54: Line 245:
             type: 'string',
             type: 'string',
             references: [
             references: [
                 'Smith\'s Anesthesia for Infants and Children. 8e. p356'
                 'Baby Miller 6e, ch. 16, pg. 550'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 var age = data.age.toNumber( 'yo' );
                 var age = data.age.toNumber( 'yo' );


                 if( age >= 6 ) {
                 var systolicMin, systolicMax, diastolicMin, diastolicMax, meanMin, meanMax;
                     return '3';
 
                if( age >= 16 ) {
                    systolicMin = 100;
                    systolicMax = 125;
                } else if( age >= 13 ) {
                    systolicMin = 95;
                    systolicMax = 120;
                } else if( age >= 9 ) {
                    systolicMin = 90;
                    systolicMax = 115;
                } else if( age >= 6 ) {
                     systolicMin = 85;
                    systolicMax = 105;
                } else if( age >= 3 ) {
                    systolicMin = 80;
                    systolicMax = 100;
                 } else if( age >= 1 ) {
                 } else if( age >= 1 ) {
                     // ( age + 16 ) / 4
                     systolicMin = 75;
                     // To nicely display in increments of 0.5, double the calculation, round (floor), then divide by 2
                    systolicMax = 95;
                     return '2';
                } else if( age >= 6 / 12 ) {
                    systolicMin = 70;
                     systolicMax = 90;
                } else if( age >= 1 / 12 ) {
                    systolicMin = 65;
                     systolicMax = 85;
                 } else {
                 } else {
                     return '-';
                     systolicMin = 60;
                    systolicMax = 75;
                 }
                 }
             }
             }
         },
         }
         bladeMillerSize: {
    } );
             name: 'Laryngoscope blade size (Miller)',
 
             abbreviation: 'Blade (Miller)',
    // Cardiovascular
    mw.calculators.addCalculations( {
         vO2: {
             name: 'Rate of oxygen consumption (VO<sub>2</sub>)',
             abbreviation: 'VO<sub>2</sub>',
             data: {
             data: {
                calculations: {
                    required: [ 'bsa' ]
                },
                 variables: {
                 variables: {
                     required: [ 'age' ]
                     optional: [ 'age' ]
                 }
                 }
             },
             },
             type: 'string',
             units: 'mL/min',
             references: [
             formula: '<math>\\mathrm{VO_2} = \\begin{cases} 125 * \\mathrm{BSA}  & \\text{if age} < 70 \\\\ 110 * \\mathrm{BSA} & \\text{if age} \\geq 70 \\end{cases}</math>',
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
             references: [],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 var age = data.age.toNumber( 'yo' );
                var bsa = data.bsa.toNumber();
                 var age = data.age ? data.age.toNumber( 'yr' ) : null;


                 if( age >= 2 ) {
                 if( age < 70 ) {
                     return '2';
                     return 125 * bsa;
                } else if( age >= 1 / 12 ) {
                    return '1';
                } else if( age >= 0 ) {
                    return '0-1';
                 } else {
                 } else {
                     return '0';
                     return 110 * bsa;
                 }
                 }
             }
             }
         },
         },
         bmi: {
         cardiacOutputFick: {
             name: 'Body mass index',
             name: 'Cardiac output (Fick)',
             abbreviation: 'BMI',
             abbreviation: 'CO (Fick)',
             data: {
             data: {
                 variables: {
                 variables: {
                     required: [ 'weight', 'height' ]
                     required: [ 'saO2', 'smvO2', 'hgb' ]
                },
                calculations: {
                    required: [ 'vO2' ]
                }
            },
            units: 'L/min',
            formula: '<math>\\mathrm{CO_{Fick}}=\\frac{\\mathrm{VO_2}}{(\\mathrm{S_aO_2} - \\mathrm{S_{mv}O_2}) * H_b * 13.4}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var vO2 = data.vO2.toNumber( 'mL/min' );
                var saO2 = data.saO2.toNumber() / 100;
                var smvO2 = data.smvO2.toNumber() / 100;
                var hgb = data.hgb.toNumber( 'ghgbperdL' );
 
                return vO2 / ( ( saO2 - smvO2 ) * hgb * 13.4 );
            }
        },
        cardiacIndex: {
            name: 'Cardiac index',
            abbreviation: 'CI',
            data: {
                calculations: {
                    required: [ 'bsa', 'cardiacOutputFick' ]
                 }
                 }
             },
             },
            digits: 0,
             units: 'L/min/m^2',
             units: 'kg/m^2',
             formula: '<math>\\mathrm{CI}=\\frac{\\mathrm{CO}}{\\mathrm{BSA}}</math>',
             formula: '<math>\\mathrm{BMI} = \\frac{\\text{mass}_\\text{kg}}{{\\text{height}_\\text{m}}^2}</math>',
             link: false,
             link: '[[Body mass index]]',
             references: [],
             references: [],
             calculate: function( data ) {
             calculate: function( data ) {
                 return data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 );
                 var cardiacOutput = data.cardiacOutputFick.toNumber( 'L/min' );
                var bsa = data.bsa.toNumber( 'm^2' );
 
                return cardiacOutput / bsa;
             }
             }
         },
         },
         bsa: {
         strokeVolume: {
             name: 'Body surface area',
             name: 'Stroke volume',
             abbreviation: 'BSA',
             abbreviation: 'SV',
             data: {
             data: {
                 variables: {
                 variables: {
                     required: [ 'weight', 'height' ]
                     required: [ 'heartRate' ]
                },
                calculations: {
                    required: [ 'cardiacOutputFick' ]
                 }
                 }
             },
             },
             formula: '<math>\\mathrm{BSA} = 0.007184 \\times W^{0.425} \\times H^{0.725}</math>',
            units: 'mL',
             formula: '<math>\\mathrm{SV}=\\frac{\\mathrm{CO}}{\\mathrm{HR}}</math>',
             link: false,
             link: false,
             references: [
             references: [],
                'Verbraecken, J; Van de Heyning P; De Backer W; Van Gaal L (Apr 2006). "Body surface area in normal-weight, overweight, and obese adults. A comparison study". Metabolism: Clinical and Experimental. 55 (4): 515–24. doi:10.1016/j.metabol.2005.11.004. PMID 16546483'
            ],
             calculate: function( data ) {
             calculate: function( data ) {
                 return Math.pow( data.weight.toNumber( 'kgwt' ), 0.425 ) * Math.pow( data.height.toNumber( 'cm' ), 0.725 );
                 var cardiacOutput = data.cardiacOutputFick.toNumber( 'mL/min' );
                var heartRate = data.heartRate.toNumber();
 
                return cardiacOutput / heartRate;
             }
             }
         },
         }
         ebv: {
    } );
             name: 'Estimated blood volume',
 
            abbreviation: 'EBV',
    // Neuro
    mw.calculators.addCalculations( {
         brainMass: {
             name: 'Brain mass',
             data: {
             data: {
                 variables: {
                 variables: {
                     required: [ 'weight', 'age' ]
                     optional: [ 'age', 'gender' ]
                 }
                 }
             },
             },
             digits: 0,
             digits: 0,
             units: 'mL',
             units: 'gwt',
             formula: '',
             description: 'This calculation will give a more precise estimate of brain mass if age and/or gender are provided.',
             references: [
             references: [
                 'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
                 'Dekaban AS. Changes in brain weights during the span of human life: relation of brain weights to body heights and body weights. Ann Neurol. 1978 Oct;4(4):345-56. doi: 10.1002/ana.410040410. PMID: 727739.'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 var weight = data.weight.toNumber( 'kgwt' );
                 var age = data.age ? data.age.toNumber( 'yr' ) : null;
                 var age = data.age.toNumber( 'yo' );
                 var gender = data.gender ? data.gender : null;
 
                var brainMassFemale = 1290;
                var brainMassMale = 1450;


                 var ebvPerKg;
                 if( age !== null ) {
                    if( age <= 10 / 365 ) {
                        // <=10 days
                        brainMassFemale = 360;
                        brainMassMale = 380;
                    } else if( age <= 4 * 30 / 365 ) {
                        // Less than 4 months. This is a gap in the reported data of the paper, so linearly interpolate?
                        var ageFactor = 1 - ( 4 * 30 / 365 - age ) / ( 4 * 30 / 365 - 10 / 365 );


                if( age >= 1 ) {
                        brainMassFemale = 360 + ageFactor * ( 580 - 360 );
                     if( data.gender === 'F' ) {
                        brainMassMale = 380 + ageFactor * ( 640 - 380 );
                         ebvPerKg = 65;
                    } else if( age <= 8 * 30 / 365 ) {
                        // <=8 months
                        brainMassFemale = 580;
                        brainMassMale = 640;
                     } else if( age <= 18 * 30 / 365 ) {
                        // <=18 months
                        brainMassFemale = 940;
                        brainMassMale = 970;
                    } else if( age <= 30 * 30 / 365 ) {
                         // <=30 months
                        brainMassFemale = 1040;
                        brainMassMale = 1120;
                    } else if( age <= 43 * 30 / 365 ) {
                        // <=43 months
                        brainMassFemale = 1090;
                        brainMassMale = 1270;
                    } else if( age <= 5 ) {
                        brainMassFemale = 1150;
                        brainMassMale = 1300;
                    } else if( age <= 7 ) {
                        brainMassFemale = 1210;
                        brainMassMale = 1330;
                    } else if( age <= 9 ) {
                        brainMassFemale = 1180;
                        brainMassMale = 1370;
                    } else if( age <= 12 ) {
                        brainMassFemale = 1260;
                        brainMassMale = 1440;
                    } else if( age <= 15 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 18 ) {
                        brainMassFemale = 1340;
                        brainMassMale = 1440;
                    } else if( age <= 21 ) {
                        brainMassFemale = 1310;
                        brainMassMale = 1450;
                    } else if( age <= 30 ) {
                        brainMassFemale = 1300;
                        brainMassMale = 1440;
                    } else if( age <= 40 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1440;
                    } else if( age <= 50 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1430;
                    } else if( age <= 55 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 60 ) {
                        brainMassFemale = 1250;
                        brainMassMale = 1370;
                    } else if( age <= 65 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1370;
                    } else if( age <= 70 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1360;
                    } else if( age <= 75 ) {
                        brainMassFemale = 1230;
                        brainMassMale = 1350;
                    } else if( age <= 80 ) {
                        brainMassFemale = 1190;
                        brainMassMale = 1330;
                    } else if( age <= 85 ) {
                        brainMassFemale = 1170;
                        brainMassMale = 1310;
                     } else {
                     } else {
                         ebvPerKg = 75;
                         brainMassFemale = 1140;
                        brainMassMale = 1290;
                     }
                     }
                 } else if( age >= 1/12 ) {
                 }
                     ebvPerKg = 80;
 
                 } else if( age >= 0 ) {
                if( gender === 'F' ) {
                     ebvPerKg = 85;
                     return brainMassFemale;
                 } else if( gender === 'M' ) {
                     return brainMassMale;
                 } else {
                 } else {
                     ebvPerKg = 95;
                     return ( brainMassFemale + brainMassMale ) / 2;
                 }
                 }
                return weight * ebvPerKg;
             }
             }
         },
         },
         ettSize: {
         cerebralBloodVolume: {
             name: 'Endotracheal tube size',
             name: 'Cerebral blood volume',
             abbreviation: 'ETT size',
             abbreviation: 'CBV',
             data: {
             data: {
                 variables: {
                 calculations: {
                     required: [ 'age' ],
                     required: [ 'brainMass' ]
                    optional: [ 'height' ]
                 }
                 }
             },
             },
             type: 'string',
             units: 'mL',
            description: '4 mL per 100g of brain mass',
             references: [
             references: [
                 'Smith\'s Anesthesia for Infants and Children. 8e. p356'
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 var age = data.age.toNumber( 'yo' );
                 var brainMass = data.brainMass.toNumber( 'gwt' );


                 if( age >= 12 ) {
                 return 4 * brainMass / 100;
                    return '6+';
                } else if( age >= 1 ) {
                    // ( age + 16 ) / 4
                    // To nicely display in increments of 0.5, double the calculation, round (floor), then divide by 2
                    return String( Math.floor( 2 * ( ( age + 16 ) / 4 ) ) / 2 );
                } else if (age >= 0.5 ) {
                    return '3.5-4';
                } else if( age >= 0 ) {
                    return '3-3.5';
                } else {
                    return '2.5-3';
                }
             }
             }
         },
         },
         fluidMaintenanceRate: {
         cerebralMetabolicRateFactor: {
             name: 'Fluid maintenance rate',
             name: 'Cerebral metabolic rate factor',
             abbreviation: 'Fluid maint.',
             abbreviation: '%CMR',
             data: {
             data: {
                 variables: {
                 variables: {
                     required: [ 'weight' ]
                     optional: [ 'temperature' ]
                 }
                 }
             },
             },
             digits: 0,
             description: '7% change in CMR for every 1 &deg;C change in temperature',
            units: 'mL/hr',
            formula: '',
             references: [
             references: [
                 'Miller\'s Anesthesia 7e, section IV, pg. 1728'
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 var weight = data.weight.toNumber( 'kgwt' );
                 var temperature = data.temperature ? data.temperature.toNumber( 'degC' ) : null;
 
                // Uses 4-2-1 rule
                var maintenanceRate = 4 * Math.min( weight, 10 );


                 if( weight > 10 ) {
                 var cerebralMetabolicRateFactor = 1;
                    maintenanceRate += 2 * Math.min( weight - 10, 10 );
                }


                 if( weight > 20) {
                 if( temperature ) {
                     maintenanceRate += weight - 20;
                     cerebralMetabolicRateFactor += 0.07 * ( temperature - 37 );
                 }
                 }


                 return maintenanceRate;
                 return cerebralMetabolicRateFactor;
             }
             }
         },
         },
         ibw: {
         cerebralMetabolicRateO2: {
             name: 'Ideal body weight',
             name: 'Cerebral metabolic rate (O<sub>2</sub>)',
             abbreviation: 'IBW',
             abbreviation: 'CMRO<sub>2</sub>',
             data: {
             data: {
                calculations: {
                    required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                },
                 variables: {
                 variables: {
                     required: [ 'height', 'gender' ]
                     optional: [ 'temperature' ]
                 }
                 }
             },
             },
             units: 'kg',
             units: 'mL/min',
             link: 'https://en.wikipedia.org/wiki/Human_body_weight#Ideal_body_weight',
             description: '<ul><li>3 mL O<sub>2</sub>/min per 100g of brain mass</li><li>If temperature is provided, adjusts estimate using 7% change in CMR for every 1 &deg;C change in temperature</li></ul>',
             references: [
             references: [
                 'Devine BJ. Gentamicin therapy. Drug Intell Clin Pharm. 1974;8:650–655.'
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 if( data.height.toNumber( 'cm' ) < 152 ) {
                 // Temperature is included as an optional variable to generate the input.
                    throw new Error( 'Ideal body weight may only be applied to persons 152 cm (60 inches) or taller' );
                // It is used by cerebralMetabolicRateFactor, which is an internal calculation not typically shown.
                }
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();


                var baseWeight = data.gender === 'F' ? 45.5 : 50;
                 return cerebralMetabolicRateFactor * 3 * brainMass / 100;
 
                // baseWeight + 2.3 kg for every inch over 5 feet
                 return baseWeight + 2.3 * ( data.height.toNumber( 'in' ) - 60 );
             }
             }
         },
         },
         lbw: {
         cerebralMetabolicRateGlucose: {
             name: 'Lean body weight',
             name: 'Cerebral metabolic rate (Glucose)',
             abbreviation: 'LBW',
             abbreviation: 'CMR<sub>glu</sub>',
             data: {
             data: {
                 variables: {
                 calculations: {
                     required: [ 'gender', 'height', 'weight' ]
                     required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                 }
                 }
             },
             },
             units: 'kg',
             units: 'mg/min',
             link: 'https://en.wikipedia.org/wiki/Lean_body_mass',
             description: '<ul><li>5 mg glucose/min per 100g of brain mass</li><li>7% change in CMR for every 1 &deg;C change in temperature</li></ul>',
             references: [
             references: [
                 'Janmahasatian S, Duffull SB, Ash S, Ward LC, Byrne NM, Green B. Quantification of lean bodyweight. Clin Pharmacokinet. 2005; 44(10): 1051-65.'
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 if( data.gender === 'F' ) {
                 var brainMass = data.brainMass.toNumber( 'gwt' );
                    return 9270 * data.weight.toNumber( 'kgwt' ) / ( 8780 + 244 * data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 ) );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();
                 } else {
 
                    return 9270 * data.weight.toNumber( 'kgwt' ) / ( 6680 + 216 * data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 ) );
                 return cerebralMetabolicRateFactor * 5 * brainMass / 100;
                }
             }
             }
         },
         },
         maxAbl: {
         cerebralBloodFlow: {
             name: 'Maximum allowable blood loss',
             name: 'Cerebral blood flow',
             abbreviation: 'Max ABL',
             abbreviation: 'CBF',
             data: {
             data: {
                 calculations: {
                 calculations: {
                     required: [ 'ebv' ]
                     required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                 },
                 },
                 variables: {
                 variables: {
                     required: [ 'weight', 'age', 'hct', 'minHct' ]
                     optional: [ 'paCO2' ]
                 }
                 }
             },
             },
            digits: 0,
             units: 'mL/min',
             units: 'mL',
             description: '<ul><li>50 mL/min per 100g of brain mass.</li><li>Every mmHg in PaCO2 changes CBF by 1.5 mL/min per 100g of brain mass.</li><li>Cerebral blood flow and cerebral metabolic rate are coupled. Factors that alter CMR (e.g. temperature) will proportionally alter CBF.</li>',
             formula: '',
             references: [
             references: [
                 'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001',
                'Brian JE Jr. Carbon dioxide and the cerebral circulation. Anesthesiology. 1998 May;88(5):1365-86. doi: 10.1097/00000542-199805000-00029. PMID: 9605698.'
             ],
             ],
             calculate: function( data ) {
             calculate: function( data ) {
                 console.log(data);
                 var brainMass = data.brainMass.toNumber( 'gwt' );
                 return 0;
                 var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();
            },
                 var paCO2 = data.paCO2.toNumber( 'mmHg' );
            onRendered: function( $selector ) {
 
                 $selector.after( mw.calculators.getVariable( 'hct' ).createInput() );
                var cerebralBloodFlow = cerebralMetabolicRateFactor * 50 * brainMass / 100;
                $selector.after( mw.calculators.getVariable( 'minHct' ).createInput() );
 
                if( paCO2 ) {
                    // CO2 reductions don't reduce CBF more than 50%
                    var minCerebralBloodFlow = cerebralBloodFlow / 2;
 
                    cerebralBloodFlow += 1.5 * brainMass / 100 * ( paCO2 - 40 );
 
                    cerebralBloodFlow = math.max( cerebralBloodFlow, minCerebralBloodFlow );
                }
 
                return cerebralBloodFlow;
             }
             }
         }
         }
     } );
     } );


    var tableMaxWidth = 600;


     mw.calculators.addCalculators( moduleId, {
 
         anatomy: {
    // Pulmonary
             name: 'Patient statistics',
     mw.calculators.addCalculations( {
             calculations: [
         paO2Predicted: {
                 'bmi',
             name: 'PaO<sub>2</sub> (predicted)',
                 'bsa',
             abbreviation: 'PaO<sub>2</sub> pred.',
                'ibw',
            data: {
                 'lbw'
                 variables: {
                    required: [ 'pAtm', 'fiO2', 'paCO2' ]
                 }
            },
            units: 'mmHg',
            formula: '<math>P_{aO_2Predicted} = FiO_2*(P_{atm}-P_{H_{2}O})-\\frac{P_{aCO_2}}{R}</math>',
            references: [
                 'McFarlane MJ, Imperiale TF. Use of the alveolar-arterial oxygen gradient in the diagnosis of pulmonary embolism. Am J Med. 1994 Jan;96(1):57-62. doi: 10.1016/0002-9343(94)90116-3. PMID: 8304364.'
             ],
             ],
             css: {
             calculate: function( data ) {
                 'max-width': tableMaxWidth
                 var fiO2 = data.fiO2.toNumber( 'pcto2' ) / 100;
                var pAtm = data.pAtm.toNumber( 'mmHg' );
                var pH2O = 47; // mmHg
                var paCO2 = data.paCO2.toNumber( 'mmHg' );
                var r = 0.8;
 
                return fiO2 * ( pAtm - pH2O ) - paCO2 / r;
            }
        },
        aaGradientO2: {
            name: 'A-a O<sub>2</sub> gradient',
            abbreviation: 'A-a O<sub>2</sub>',
            data: {
                calculations: {
                    required: [ 'paO2Predicted' ]
                },
                variables: {
                    required: [ 'paO2' ]
                }
             },
             },
             table: true
             units: 'mmHg',
            formula: '<math>\\text{A-a gradient}_{\\mathrm{O}_2} = P_{aO_2Predicted}-P_{aO_2} </math>',
            references: [
                'McFarlane MJ, Imperiale TF. Use of the alveolar-arterial oxygen gradient in the diagnosis of pulmonary embolism. Am J Med. 1994 Jan;96(1):57-62. doi: 10.1016/0002-9343(94)90116-3. PMID: 8304364.'
            ],
            calculate: function( data ) {
                var paO2Predicted = data.paO2Predicted.toNumber( 'mmHg' );
                var paO2 = data.paO2.toNumber( 'mmHg' );
 
                return math.round( paO2Predicted - paO2 );
            }
         },
         },
         equipment: {
         aaGradientO2Predicted: {
             name: 'Equipment',
             name: 'A-a O<sub>2</sub> gradient (predicted)',
             calculations: [
             abbreviation: 'A-a O<sub>2</sub> pred.',
                 'bladeMacSize',
            data: {
                'bladeMillerSize',
                variables: {
                 'ettSize'
                    required: [ 'age' ]
                 }
            },
            units: 'mmHg',
            formula: '<math>\\text{Predicted A-a gradient}_{\\mathrm{O}_2} = \\frac{(\\mathrm{age_{yr} + 10)}}{4}</math>',
            references: [
                 'Hantzidiamantis PJ, Amaro E. Physiology, Alveolar to Arterial Oxygen Gradient. 2021 Feb 22. In: StatPearls [Internet]. Treasure Island (FL): StatPearls Publishing; 2021 Jan–. PMID: 31424737.'
             ],
             ],
             css: {
             calculate: function( data ) {
                 'max-width': tableMaxWidth
                 var age = data.age.toNumber( 'yr' );
 
                return ( age + 10 ) / 4;
            }
        },
        weightBasedTidalVolume: {
            name: 'Weight-based tidal volume',
            abbreviation: 'Weight-based TV',
            data: {
                calculations: {
                    required: [ 'ibw' ]
                },
                variables: {
                    required: [ 'weightBasedTidalVolumePerKgMin', 'weightBasedTidalVolumePerKgMax' ]
                }
             },
             },
             table: true
             type: 'string',
        },
             description: '<ul><li>Calculated using ideal body weight</li><li>Low tidal volume uses 6-8 mL/kg<sup>1</sup><ul><li>Current evidence does not show benefit of intraoperative low tidal volumes for patients without pulmonary injury<sup>2</sup></li></ul></li>',
        fluidManagement: {
             references: [
             name: 'Fluid management',
                 'Acute Respiratory Distress Syndrome Network, Brower RG, Matthay MA, Morris A, Schoenfeld D, Thompson BT, Wheeler A. Ventilation with lower tidal volumes as compared with traditional tidal volumes for acute lung injury and the acute respiratory distress syndrome. N Engl J Med. 2000 May 4;342(18):1301-8. doi: 10.1056/NEJM200005043421801. PMID: 10793162.',
             calculations: [
                 'Karalapillai D, Weinberg L, Peyton P, Ellard L, Hu R, Pearce B, Tan CO, Story D, O\'Donnell M, Hamilton P, Oughton C, Galtieri J, Wilson A, Serpa Neto A, Eastwood G, Bellomo R, Jones DA. Effect of Intraoperative Low Tidal Volume vs Conventional Tidal Volume on Postoperative Pulmonary Complications in Patients Undergoing Major Surgery: A Randomized Clinical Trial. JAMA. 2020 Sep 1;324(9):848-858. doi: 10.1001/jama.2020.12866. PMID: 32870298; PMCID: PMC7489812.'
                 'ebv',
                 'fluidMaintenanceRate',
                'maxAbl'
             ],
             ],
             css: {
             calculate: function( data ) {
                 'max-width': tableMaxWidth
                 var ibw = data.ibw.toNumber( 'kgwt' );
             },
                var weightBasedTidalVolumePerKgMin = data.weightBasedTidalVolumePerKgMin.toNumber( 'mL/kgwt' );
            table: true
                var weightBasedTidalVolumePerKgMax = data.weightBasedTidalVolumePerKgMax.toNumber( 'mL/kgwt' );
 
                return math.round( weightBasedTidalVolumePerKgMin * ibw ) + '-' + math.round( weightBasedTidalVolumePerKgMax * ibw ) + ' mL';
             }
         }
         }
     } );
     } );
}() );
}() );

Latest revision as of 06:27, 19 March 2022

( function() {
    mw.calculators.addUnitsBases( {
        bpm: {
            toString: function( units ) {
                units = units.replace( 'bpm', 'beats/min' );

                return units;
            }
        },
        hgb: {
            toString: function( units ) {
                units = units.replace( 'hgbperdL', '/dL' );
                units = units.replace( /\s?pcthct/, '%' );

                return units;
            }
        },
        o2: {
            toString: function( units ) {
                units = units.replace( /\s?pcto2/, '%' );

                return units;
            }
        },
        temperature: {
            toString: function( units ) {
                units = units.replace( 'deg', '&deg;' );

                return units;
            }
        }
    } );

    mw.calculators.addUnits( {
        bpm: {
            baseName: 'bpm'
        },
        pcthct: {
            baseName: 'hgb'
        },
        pcto2: {
            baseName: 'o2'
        },
        ghgbperdL: {
            baseName: 'hgb',
            prefixes: 'short',
            definition: '3 pcthct'
        }
    } );

    mw.calculators.addVariables( {
        fiO2: {
            name: 'FiO<sub>2</sub>',
            type: 'number',
            abbreviation: 'FiO<sub>2</sub>',
            minValue: '10 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '21 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        heartRate: {
            name: 'Heart rate',
            type: 'number',
            abbreviation: 'HR',
            defaultValue: '60 bpm',
            maxLength: 4,
            units: [
                'bpm'
            ]
        },
        hgb: {
            name: 'Hemoglobin/hematocrit',
            type: 'number',
            abbreviation: 'HgB',
            minValue: '3 ghgbperdL',
            maxValue: '25 ghgbperdL',
            defaultValue: '13 ghgbperdL',
            maxLength: 4,
            units: [
                'pcthct',
                'ghgbperdL'
            ]
        },
        paCO2: {
            name: 'PaCO<sub>2</sub>',
            type: 'number',
            abbreviation: 'PaCO<sub>2</sub>',
            minValue: '20 mmHg',
            defaultValue: '40 mmHg',
            maxLength: 3,
            units: [
                'mmHg'
            ]
        },
        paO2: {
            name: 'PaO<sub>2</sub>',
            type: 'number',
            abbreviation: 'PaO<sub>2</sub>',
            minValue: '25 mmHg',
            defaultValue: '100 mmHg',
            maxLength: 3,
            units: [
                'mmHg'
            ]
        },
        pAtm: {
            name: 'Atmospheric pressure',
            type: 'number',
            abbreviation: 'P<sub>atm</sub>',
            minValue: '0 mmHg',
            defaultValue: '760 mmHg',
            maxLength: 4,
            units: [
                'mmHg'
            ]
        },
        saO2: {
            name: 'SaO<sub>2</sub>',
            type: 'number',
            abbreviation: 'SaO<sub>2</sub>',
            minValue: '25 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '100 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        smvO2: {
            name: 'SmvO<sub>2</sub>',
            type: 'number',
            abbreviation: 'SmvO<sub>2</sub>',
            minValue: '25 pcto2',
            maxValue: '100 pcto2',
            defaultValue: '75 pcto2',
            maxLength: 3,
            units: [
                'pcto2'
            ]
        },
        raceSpirometry: {
            name: 'Race',
            type: 'string',
            abbreviation: 'Race',
            defaultValue: 'unknown',
            options: {
                unknown: 'Unknown',
                black: 'Black',
                caucasian: 'Caucasian',
                mexican: 'Mexican-American'
            }
        },
        temperature: {
            name: 'Temperature',
            type: 'number',
            abbreviation: 'Temp',
            minValue: '20 degC',
            maxValue: '44 degC',
            defaultValue: '37 degC',
            maxLength: 5,
            units: [
                'degC',
                'degF'
            ]
        },
        weightBasedTidalVolumePerKgMin: {
            name: 'Minimum tidal volume',
            type: 'number',
            abbreviation: 'Min TV',
            minValue: '3 mL/kgwt',
            maxValue: '12 mL/kgwt',
            defaultValue: '6 mL/kgwt',
            maxLength: 2,
            units: [
                'mL/kgwt'
            ]
        },
        weightBasedTidalVolumePerKgMax: {
            name: 'Maximum tidal volume',
            type: 'number',
            abbreviation: 'Max TV',
            minValue: '3 mL/kgwt',
            maxValue: '12 mL/kgwt',
            defaultValue: '8 mL/kgwt',
            maxLength: 2,
            units: [
                'mL/kgwt'
            ]
        }
    } );

    // Force re-render of ibw and lbw. This is necessary to remove the additional inputs for patient variables from the
    // calculation which are now provided by the patient input toolbar.
    mw.calculators.calculations.ibw.render();
    mw.calculators.calculations.lbw.render();

    mw.calculators.addCalculations( {
        bmi: {
            name: 'Body mass index',
            abbreviation: 'BMI',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 0,
            units: 'kg/m^2',
            formula: '<math>\\mathrm{BMI} = \\frac{\\mathrm{mass_{kg}}}{{(\\mathrm{height_{m}}})^2}</math>',
            link: '[[Body mass index]]',
            references: [],
            calculate: function( data ) {
                return data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 );
            }
        },
        bsa: {
            name: 'Body surface area',
            abbreviation: 'BSA',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 2,
            units: 'm^2',
            formula: '<math>\\mathrm{BSA} = \\sqrt{\\frac{\\mathrm{weight_{kg}}*\\mathrm{height_{cm}}}{3600}}</math>',
            link: false,
            references: [
                'Mosteller RD. Simplified calculation of body-surface area. N Engl J Med. 1987 Oct 22;317(17):1098. doi: 10.1056/NEJM198710223171717. PMID: 3657876.'
            ],
            calculate: function( data ) {
                return Math.sqrt( data.height.toNumber( 'cm' ) * data.weight.toNumber( 'kgwt' ) / 3600 );
            }
        },
        systolicBloodPressure: {
            name: 'Systolic blood pressure',
            abbreviation: 'SBP',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            type: 'string',
            references: [
                'Baby Miller 6e, ch. 16, pg. 550'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yo' );

                var systolicMin, systolicMax, diastolicMin, diastolicMax, meanMin, meanMax;

                if( age >= 16 ) {
                    systolicMin = 100;
                    systolicMax = 125;
                } else if( age >= 13 ) {
                    systolicMin = 95;
                    systolicMax = 120;
                } else if( age >= 9 ) {
                    systolicMin = 90;
                    systolicMax = 115;
                } else if( age >= 6 ) {
                    systolicMin = 85;
                    systolicMax = 105;
                } else if( age >= 3 ) {
                    systolicMin = 80;
                    systolicMax = 100;
                } else if( age >= 1 ) {
                    systolicMin = 75;
                    systolicMax = 95;
                } else if( age >= 6 / 12 ) {
                    systolicMin = 70;
                    systolicMax = 90;
                } else if( age >= 1 / 12 ) {
                    systolicMin = 65;
                    systolicMax = 85;
                } else {
                    systolicMin = 60;
                    systolicMax = 75;
                }
            }
        }
    } );

    // Cardiovascular
    mw.calculators.addCalculations( {
        vO2: {
            name: 'Rate of oxygen consumption (VO<sub>2</sub>)',
            abbreviation: 'VO<sub>2</sub>',
            data: {
                calculations: {
                    required: [ 'bsa' ]
                },
                variables: {
                    optional: [ 'age' ]
                }
            },
            units: 'mL/min',
            formula: '<math>\\mathrm{VO_2} = \\begin{cases} 125 * \\mathrm{BSA}  & \\text{if age} < 70 \\\\ 110 * \\mathrm{BSA} & \\text{if age} \\geq 70 \\end{cases}</math>',
            references: [],
            calculate: function( data ) {
                var bsa = data.bsa.toNumber();
                var age = data.age ? data.age.toNumber( 'yr' ) : null;

                if( age < 70 ) {
                    return 125 * bsa;
                } else {
                    return 110 * bsa;
                }
            }
        },
        cardiacOutputFick: {
            name: 'Cardiac output (Fick)',
            abbreviation: 'CO (Fick)',
            data: {
                variables: {
                    required: [ 'saO2', 'smvO2', 'hgb' ]
                },
                calculations: {
                    required: [ 'vO2' ]
                }
            },
            units: 'L/min',
            formula: '<math>\\mathrm{CO_{Fick}}=\\frac{\\mathrm{VO_2}}{(\\mathrm{S_aO_2} - \\mathrm{S_{mv}O_2}) * H_b * 13.4}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var vO2 = data.vO2.toNumber( 'mL/min' );
                var saO2 = data.saO2.toNumber() / 100;
                var smvO2 = data.smvO2.toNumber() / 100;
                var hgb = data.hgb.toNumber( 'ghgbperdL' );

                return vO2 / ( ( saO2 - smvO2 ) * hgb * 13.4 );
            }
        },
        cardiacIndex: {
            name: 'Cardiac index',
            abbreviation: 'CI',
            data: {
                calculations: {
                    required: [ 'bsa', 'cardiacOutputFick' ]
                }
            },
            units: 'L/min/m^2',
            formula: '<math>\\mathrm{CI}=\\frac{\\mathrm{CO}}{\\mathrm{BSA}}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var cardiacOutput = data.cardiacOutputFick.toNumber( 'L/min' );
                var bsa = data.bsa.toNumber( 'm^2' );

                return cardiacOutput / bsa;
            }
        },
        strokeVolume: {
            name: 'Stroke volume',
            abbreviation: 'SV',
            data: {
                variables: {
                    required: [ 'heartRate' ]
                },
                calculations: {
                    required: [ 'cardiacOutputFick' ]
                }
            },
            units: 'mL',
            formula: '<math>\\mathrm{SV}=\\frac{\\mathrm{CO}}{\\mathrm{HR}}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var cardiacOutput = data.cardiacOutputFick.toNumber( 'mL/min' );
                var heartRate = data.heartRate.toNumber();

                return cardiacOutput / heartRate;
            }
        }
    } );

    // Neuro
    mw.calculators.addCalculations( {
        brainMass: {
            name: 'Brain mass',
            data: {
                variables: {
                    optional: [ 'age', 'gender' ]
                }
            },
            digits: 0,
            units: 'gwt',
            description: 'This calculation will give a more precise estimate of brain mass if age and/or gender are provided.',
            references: [
                'Dekaban AS. Changes in brain weights during the span of human life: relation of brain weights to body heights and body weights. Ann Neurol. 1978 Oct;4(4):345-56. doi: 10.1002/ana.410040410. PMID: 727739.'
            ],
            calculate: function( data ) {
                var age = data.age ? data.age.toNumber( 'yr' ) : null;
                var gender = data.gender ? data.gender : null;

                var brainMassFemale = 1290;
                var brainMassMale = 1450;

                if( age !== null ) {
                    if( age <= 10 / 365 ) {
                        // <=10 days
                        brainMassFemale = 360;
                        brainMassMale = 380;
                    } else if( age <= 4 * 30 / 365 ) {
                        // Less than 4 months. This is a gap in the reported data of the paper, so linearly interpolate?
                        var ageFactor = 1 - ( 4 * 30 / 365 - age ) / ( 4 * 30 / 365 - 10 / 365 );

                        brainMassFemale = 360 + ageFactor * ( 580 - 360 );
                        brainMassMale = 380 + ageFactor * ( 640 - 380 );
                    } else if( age <= 8 * 30 / 365 ) {
                        // <=8 months
                        brainMassFemale = 580;
                        brainMassMale = 640;
                    } else if( age <= 18 * 30 / 365 ) {
                        // <=18 months
                        brainMassFemale = 940;
                        brainMassMale = 970;
                    } else if( age <= 30 * 30 / 365 ) {
                        // <=30 months
                        brainMassFemale = 1040;
                        brainMassMale = 1120;
                    } else if( age <= 43 * 30 / 365 ) {
                        // <=43 months
                        brainMassFemale = 1090;
                        brainMassMale = 1270;
                    } else if( age <= 5 ) {
                        brainMassFemale = 1150;
                        brainMassMale = 1300;
                    } else if( age <= 7 ) {
                        brainMassFemale = 1210;
                        brainMassMale = 1330;
                    } else if( age <= 9 ) {
                        brainMassFemale = 1180;
                        brainMassMale = 1370;
                    } else if( age <= 12 ) {
                        brainMassFemale = 1260;
                        brainMassMale = 1440;
                    } else if( age <= 15 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 18 ) {
                        brainMassFemale = 1340;
                        brainMassMale = 1440;
                    } else if( age <= 21 ) {
                        brainMassFemale = 1310;
                        brainMassMale = 1450;
                    } else if( age <= 30 ) {
                        brainMassFemale = 1300;
                        brainMassMale = 1440;
                    } else if( age <= 40 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1440;
                    } else if( age <= 50 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1430;
                    } else if( age <= 55 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 60 ) {
                        brainMassFemale = 1250;
                        brainMassMale = 1370;
                    } else if( age <= 65 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1370;
                    } else if( age <= 70 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1360;
                    } else if( age <= 75 ) {
                        brainMassFemale = 1230;
                        brainMassMale = 1350;
                    } else if( age <= 80 ) {
                        brainMassFemale = 1190;
                        brainMassMale = 1330;
                    } else if( age <= 85 ) {
                        brainMassFemale = 1170;
                        brainMassMale = 1310;
                    } else {
                        brainMassFemale = 1140;
                        brainMassMale = 1290;
                    }
                }

                if( gender === 'F' ) {
                    return brainMassFemale;
                } else if( gender === 'M' ) {
                    return brainMassMale;
                } else {
                    return ( brainMassFemale + brainMassMale ) / 2;
                }
            }
        },
        cerebralBloodVolume: {
            name: 'Cerebral blood volume',
            abbreviation: 'CBV',
            data: {
                calculations: {
                    required: [ 'brainMass' ]
                }
            },
            units: 'mL',
            description: '4 mL per 100g of brain mass',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var brainMass = data.brainMass.toNumber( 'gwt' );

                return 4 * brainMass / 100;
            }
        },
        cerebralMetabolicRateFactor: {
            name: 'Cerebral metabolic rate factor',
            abbreviation: '%CMR',
            data: {
                variables: {
                    optional: [ 'temperature' ]
                }
            },
            description: '7% change in CMR for every 1 &deg;C change in temperature',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var temperature = data.temperature ? data.temperature.toNumber( 'degC' ) : null;

                var cerebralMetabolicRateFactor = 1;

                if( temperature ) {
                    cerebralMetabolicRateFactor += 0.07 * ( temperature - 37 );
                }

                return cerebralMetabolicRateFactor;
            }
        },
        cerebralMetabolicRateO2: {
            name: 'Cerebral metabolic rate (O<sub>2</sub>)',
            abbreviation: 'CMRO<sub>2</sub>',
            data: {
                calculations: {
                    required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                },
                variables: {
                    optional: [ 'temperature' ]
                }
            },
            units: 'mL/min',
            description: '<ul><li>3 mL O<sub>2</sub>/min per 100g of brain mass</li><li>If temperature is provided, adjusts estimate using 7% change in CMR for every 1 &deg;C change in temperature</li></ul>',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                // Temperature is included as an optional variable to generate the input.
                // It is used by cerebralMetabolicRateFactor, which is an internal calculation not typically shown.
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();

                return cerebralMetabolicRateFactor * 3 * brainMass / 100;
            }
        },
        cerebralMetabolicRateGlucose: {
            name: 'Cerebral metabolic rate (Glucose)',
            abbreviation: 'CMR<sub>glu</sub>',
            data: {
                calculations: {
                    required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                }
            },
            units: 'mg/min',
            description: '<ul><li>5 mg glucose/min per 100g of brain mass</li><li>7% change in CMR for every 1 &deg;C change in temperature</li></ul>',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();

                return cerebralMetabolicRateFactor * 5 * brainMass / 100;
            }
        },
        cerebralBloodFlow: {
            name: 'Cerebral blood flow',
            abbreviation: 'CBF',
            data: {
                calculations: {
                    required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                },
                variables: {
                    optional: [ 'paCO2' ]
                }
            },
            units: 'mL/min',
            description: '<ul><li>50 mL/min per 100g of brain mass.</li><li>Every mmHg in PaCO2 changes CBF by 1.5 mL/min per 100g of brain mass.</li><li>Cerebral blood flow and cerebral metabolic rate are coupled. Factors that alter CMR (e.g. temperature) will proportionally alter CBF.</li>',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001',
                'Brian JE Jr. Carbon dioxide and the cerebral circulation. Anesthesiology. 1998 May;88(5):1365-86. doi: 10.1097/00000542-199805000-00029. PMID: 9605698.'
            ],
            calculate: function( data ) {
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();
                var paCO2 = data.paCO2.toNumber( 'mmHg' );

                var cerebralBloodFlow = cerebralMetabolicRateFactor * 50 * brainMass / 100;

                if( paCO2 ) {
                    // CO2 reductions don't reduce CBF more than 50%
                    var minCerebralBloodFlow = cerebralBloodFlow / 2;

                    cerebralBloodFlow += 1.5 * brainMass / 100 * ( paCO2 - 40 );

                    cerebralBloodFlow = math.max( cerebralBloodFlow, minCerebralBloodFlow );
                }

                return cerebralBloodFlow;
            }
        }
    } );



    // Pulmonary
    mw.calculators.addCalculations( {
        paO2Predicted: {
            name: 'PaO<sub>2</sub> (predicted)',
            abbreviation: 'PaO<sub>2</sub> pred.',
            data: {
                variables: {
                    required: [ 'pAtm', 'fiO2', 'paCO2' ]
                }
            },
            units: 'mmHg',
            formula: '<math>P_{aO_2Predicted} = FiO_2*(P_{atm}-P_{H_{2}O})-\\frac{P_{aCO_2}}{R}</math>',
            references: [
                'McFarlane MJ, Imperiale TF. Use of the alveolar-arterial oxygen gradient in the diagnosis of pulmonary embolism. Am J Med. 1994 Jan;96(1):57-62. doi: 10.1016/0002-9343(94)90116-3. PMID: 8304364.'
            ],
            calculate: function( data ) {
                var fiO2 = data.fiO2.toNumber( 'pcto2' ) / 100;
                var pAtm = data.pAtm.toNumber( 'mmHg' );
                var pH2O = 47; // mmHg
                var paCO2 = data.paCO2.toNumber( 'mmHg' );
                var r = 0.8;

                return fiO2 * ( pAtm - pH2O ) - paCO2 / r;
            }
        },
        aaGradientO2: {
            name: 'A-a O<sub>2</sub> gradient',
            abbreviation: 'A-a O<sub>2</sub>',
            data: {
                calculations: {
                    required: [ 'paO2Predicted' ]
                },
                variables: {
                    required: [ 'paO2' ]
                }
            },
            units: 'mmHg',
            formula: '<math>\\text{A-a gradient}_{\\mathrm{O}_2} = P_{aO_2Predicted}-P_{aO_2} </math>',
            references: [
                'McFarlane MJ, Imperiale TF. Use of the alveolar-arterial oxygen gradient in the diagnosis of pulmonary embolism. Am J Med. 1994 Jan;96(1):57-62. doi: 10.1016/0002-9343(94)90116-3. PMID: 8304364.'
            ],
            calculate: function( data ) {
                var paO2Predicted = data.paO2Predicted.toNumber( 'mmHg' );
                var paO2 = data.paO2.toNumber( 'mmHg' );

                return math.round( paO2Predicted - paO2 );
            }
        },
        aaGradientO2Predicted: {
            name: 'A-a O<sub>2</sub> gradient (predicted)',
            abbreviation: 'A-a O<sub>2</sub> pred.',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            units: 'mmHg',
            formula: '<math>\\text{Predicted A-a gradient}_{\\mathrm{O}_2} = \\frac{(\\mathrm{age_{yr} + 10)}}{4}</math>',
            references: [
                'Hantzidiamantis PJ, Amaro E. Physiology, Alveolar to Arterial Oxygen Gradient. 2021 Feb 22. In: StatPearls [Internet]. Treasure Island (FL): StatPearls Publishing; 2021 Jan–. PMID: 31424737.'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yr' );

                return ( age + 10 ) / 4;
            }
        },
        weightBasedTidalVolume: {
            name: 'Weight-based tidal volume',
            abbreviation: 'Weight-based TV',
            data: {
                calculations: {
                    required: [ 'ibw' ]
                },
                variables: {
                    required: [ 'weightBasedTidalVolumePerKgMin', 'weightBasedTidalVolumePerKgMax' ]
                }
            },
            type: 'string',
            description: '<ul><li>Calculated using ideal body weight</li><li>Low tidal volume uses 6-8 mL/kg<sup>1</sup><ul><li>Current evidence does not show benefit of intraoperative low tidal volumes for patients without pulmonary injury<sup>2</sup></li></ul></li>',
            references: [
                'Acute Respiratory Distress Syndrome Network, Brower RG, Matthay MA, Morris A, Schoenfeld D, Thompson BT, Wheeler A. Ventilation with lower tidal volumes as compared with traditional tidal volumes for acute lung injury and the acute respiratory distress syndrome. N Engl J Med. 2000 May 4;342(18):1301-8. doi: 10.1056/NEJM200005043421801. PMID: 10793162.',
                'Karalapillai D, Weinberg L, Peyton P, Ellard L, Hu R, Pearce B, Tan CO, Story D, O\'Donnell M, Hamilton P, Oughton C, Galtieri J, Wilson A, Serpa Neto A, Eastwood G, Bellomo R, Jones DA. Effect of Intraoperative Low Tidal Volume vs Conventional Tidal Volume on Postoperative Pulmonary Complications in Patients Undergoing Major Surgery: A Randomized Clinical Trial. JAMA. 2020 Sep 1;324(9):848-858. doi: 10.1001/jama.2020.12866. PMID: 32870298; PMCID: PMC7489812.'
            ],
            calculate: function( data ) {
                var ibw = data.ibw.toNumber( 'kgwt' );
                var weightBasedTidalVolumePerKgMin = data.weightBasedTidalVolumePerKgMin.toNumber( 'mL/kgwt' );
                var weightBasedTidalVolumePerKgMax = data.weightBasedTidalVolumePerKgMax.toNumber( 'mL/kgwt' );

                return math.round( weightBasedTidalVolumePerKgMin * ibw ) + '-' + math.round( weightBasedTidalVolumePerKgMax * ibw ) + ' mL';
            }
        }
    } );
}() );