Calculate log() giving NaN as answer

I am using the log formula in my xlsform. This is a part of a big form. When the relevant questions are isolated in a separate new xls file, the kobotoolbox preview is displaying the results correctly. But when it is a part of the full file, it shows NaN as the calculated value. I am unable to attach the file. This works as expected in the file below.

type name label required relevant calculation
select_one genders gender Gender
decimal weight Weight true
calculate ebv ${weight}*if(selected(${gender},‘female’),65, 75)
decimal hb Hemoglobin (g/dl)
calculate mabl20 ${ebv}*0.2
calculate mabl10 hb>10 ${ebv}*log(${hb} div 10)
calculate mabl9 hb>9 ${ebv}*log(${hb} div 9)
calculate mabl8 hb>8 ${ebv}*log(${hb} div 8)
calculate text_mabl concat(if(${hb} > 10, concat('Hb drop to 10 is ', round(${mabl10}, 0), ’ ml. '), ‘’), if(${hb} > 9, concat('Hb drop to 9 is ', round(${mabl9}, 0), ’ ml. '), ‘’), if(${hb} > 8, concat('Hb drop to 8 is ‘, round(${mabl8}, 0), ’ ml.’), ‘’))
note mabl_disp Estimated blood volume is ${ebv}, 20% drop in volume = ${mabl20}, ${text_mabl}

But it shows Nan as the values of mabl10, mabl9 and mab8 in the file below. ebv and mabl20 are correctly shown

type name label::English (en) hint::English (en) required constraint relevant calculation
date date Date true
text hin Hospital ID true regex(.,‘^[0-9A-Z]{7}$’)
integer age Age true
text age_0 Enter age of Infant age = 0
select_one gender_list gender Gender true
decimal height Height
decimal weight Weight true
calculate bmi round(${weight}*10000 div ${height} div ${height}, 1)
calculate ebv ${weight}*if(selected(${gender},‘female’),65, 75)
note bmi_display BMI is ${bmi}
select_multiple asa_grade asa ASA status true
text diagnosis Diagnosis true
text surgery Surgery true
select_multiple comorbidities_list comorbidities Co-Morbidities true
text comorbidities_1 Other Co-morbidities selected(${comorbidities},‘others’)
text medications Medications the patient is taking not(selected(${comorbidities},‘nil’))
select_one yes_no uri_lri Did the patient have respiratory infection recently? (past month)
select_multiple rcri_criteria rcri RCRI Criteria selected(${comorbidities},‘cad’)
calculate rcri_score count-selected(${rcri})
calculate rcri_risk if((${rcri_score}<1), ‘Class 1 risk of MACE 0.4%(0.05-1.5%)’, if((${rcri_score}=1), ‘Class 2 risk of MACE 0.9%(0.3-2.1%)’, if((${rcri_score}=2), ‘Class 3 risk of MACE 6.6%(3.9-10.3%)’, ‘Class 4 risk of MACE >= 11%’)))
note rcri_display RCRI risk score is ${rcri_score} falling in ${rcri_risk} risk selected(${comorbidities},‘cad’)
integer colds_score Enter COLDS score (Score >= 19 suggestive of high risk for Perioperative respiratory adverse events) age<= 6 and selected(${uri_lri}, ‘yes’)
select_multiple habit_list habits Habits true
text prev_surg Previous surgery true
text allergy Any Allergies true
select_one yes_no diff_airway Is the airway difficult
select_multiple difficult_bmv_list diff_bmv Difficult BMV factors selected(${diff_airway},‘yes’)
select_multiple difficult_ett_list diff_ett Difficult ETT insertion factors selected(${diff_airway},‘yes’)
select_multiple difficult_lma_list diff_lma Difficult LMA insertion factors selected(${diff_airway},‘yes’)
select_multiple difficult_surgical_airway_list diff_surgical_airway Difficult surgical airway factors selected(${diff_airway},‘yes’)
decimal hb Hemoglobin (g/dl)
calculate mabl20 ${ebv}*0.2
calculate mabl10 hb>10 ${ebv}*log(${hb} div 10)
calculate mabl9 hb>9 ${ebv}*log(${hb} div 9)
calculate mabl8 hb>8 ${ebv}*log(${hb} div 8)
calculate text_mabl concat(if(${hb} > 10, concat('Hb drop to 10 is ', round(${mabl10}, 0), ’ ml. '), ‘’), if(${hb} > 9, concat('Hb drop to 9 is ', round(${mabl9}, 0), ’ ml. '), ‘’), if(${hb} > 8, concat('Hb drop to 8 is ‘, round(${mabl8}, 0), ’ ml.’), ‘’))
note mabl_disp Estimated blood volume is ${ebv}, 20% drop in volume = ${mabl20}, ${text_mabl}
decimal platelet Platelets (lakhs/ml)
decimal creat Creatinine (mg/dl)
text bbvs BBVS
text ecg ECG
text cxr Chest X ray
text other_inv Other investigations (if any)
select_multiple dept_list departments Departments involved in the surgery true
select_multiple anesthesia_type anesthesia Anesthesia provided true
select_one yes_no rsi Rapid Sequence Induction? selected(${anesthesia},‘ga’)
select_one airway_device airway Ventilation Device selected(${anesthesia},‘ga’)
select_one bmv_grades bmv_grade Bag Mask Ventilation grading selected(${anesthesia},‘ga’) and selected(${rsi},‘no’)
select_one lma_types lma_type LMA used selected(${airway},‘lma’)
select_multiple et_technique_list et_technique Intubated using selected(${airway},‘et’) or selected(${airway},‘dlt’)
select_one et_grade_list et_grade Scopy Grade selected(${et_technique},‘laryngoscopy’)
select_one agent_type agent Agent used to maintain anesthesia selected(${anesthesia},‘ga’)
select_one neuraxial_positions neuraxial_position Neuraxial block given in selected(${anesthesia},‘sab’) or selected(${anesthesia},‘epi’) or selected(${anesthesia},‘comb’)
select_multiple regional_blocks regional_block Block given selected(${anesthesia},‘nerve_block’)
text approach Approach to the ${regional_block} block selected(${anesthesia},‘nerve_block’)
text spinal_drug Drugs for spinal anesthesia selected(${anesthesia},‘sab’)
text unlisted_block Block given selected(${regional_block},‘other_block’)
decimal surgery_duration Duration of surgery
select_multiple positioning position Position of patient
select_multiple assist_list assists Adjuncts used true
text tourniquet_time Tourniquet time selected(${assists},‘tourniquet’)
select_multiple fluid_list fluids_given Select all products given
integer crystalloid Amount of crystalloids (ml) administered
integer colloids Amount of colloids (ml) administered selected(${fluids_given},‘colloidal’)
select_multiple blood_product_list blood_products_ Select all blood products administered selected(${fluids_given},‘blood_prod’)
integer blood_products_volume Volume (ml) of blood products administered selected(${fluids_given},‘blood_prod’)
calculate fluid_administered coalesce(${crystalloid},0) + coalesce(${colloids},0)+coalesce(${blood_products_volume},0)
note tfa Total fluids administered is ${fluid_administered} ml
note mabl_disp Estimated blood volume is ${ebv}, 20% drop in volume = ${mabl20}, ${text_mabl}
text blood_loss Estimated blood loss
select_one supervision_list supervision Supervision A - one on one, B - Remote, C - Independent true
text supervisor_name Supervisor Name
select_multiple analgesia_list analgesia Post Operative Analgesia true
select_multiple regional_blocks postOp_block Block given selected(${analgesia},‘regional_catheter’) or selected(${analgesia},‘regional_singleShot’)
text postOp_unlisted Block given selected(${postOp_block},‘other_block’)
select_one transfer_to transfer Patient transferred to true
select_multiple icu_transfer_indications icu_indications Reason for transfer to ICU selected(${transfer},‘icu’)
text icu_other_indications Enter indication for ICU transfer selected(${icu_indications},‘other_indication’)
text comments Comments

Not sure why this is happening. Kindly help.

Welcome back to the community, @cruderags! You may need to share your XLSForm with the community so they can help troubleshoot your issue. Doing so will make it easier and more effective for the community to assist you.

Your calculation for ${mabl20} - which is dependent on ${ebv} - is:

${ebv}*0.2

And it turn, ${ebv} is a calculation dependent on ${weight}:

${weight}*if(selected(${gender},‘female’),65, 75)

So presumably your weight has been set otherwise both these derived calculation results would be NaN.

However, your calculation for the problematic ${mabl10} - and for that matter, ${mabl9} and ${mab8} too - is:

${ebv}*log(${hb} div 10)

Since we already know ${ebv} is OK - from the above - that would imply ${hb} is probably null.

I see you do have a relevant condition on these calculations, to prevent evaluating them unless “hb > 10”, but that syntax is incorrect - it should be "${hb} > 10

Try fixing the relevant conditions on these 3 calculations - mab10, mab9 and mab8 - and see if that helps.

So I suspect the issue is less that log() is giving you NaN, than the fact the parameter given to log() is already NaN - because the sub-expression ${hb} div 10 is NaN, because Question hb hasnt been answered yet (it’s not required!), and this calculation isn’t being skipped over - as was intended - ‘cause the relevant expression is malformed!… Oh my! :slightly_smiling_face:

2 Likes

I tried uploading the file. But I was unable to do so as a new user.

I’ve bumped you up, so please have another go.