2

I have entries which contain a drop down list of items that I need to run calculations on. In my template, I am filtering the entries by the drop down field so I can get the sum total of the entries that meet the criteria and then run calculations based on that. I am running into a "Division by zero" error when I try to output some calculations. I believe I'm getting that error because there are no results for some of my entries (meaning an item in the drop down list was not selected in any of the entries). I believe I need to add |default(0) so that if the entry search turns up with no results then the number 0 is used in the calculation for that result.

Please refer below for the simplified version of my code. I'll pull one area out as an example. If there are no results for entriesLogInterpretationTrueNegative then I get the "Division by zero" error. In this example, there are no entries in the database where "True Negative" is selected in the drop down menu. I believe I need to say that if there are no results then use "0".

<table id="" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="c-center c-font-sbold">Sensitivity</th>
            <th class="c-center c-font-sbold">Specificity</th>
            <th class="c-center c-font-sbold">Accuracy</th>
        </tr>
    </thead>

    <tbody>

        <!-- Loop through all study type groups. (Global Variable set in Base Layout). -->    
        {% for keywordStudyType in GVstudyTypeGroup %}

            <!-- Get count total for result for logged in sonographer. -->
            {% set entriesLogInterpretationTruePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:truePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationTrueNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:trueNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalsePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falsePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalseNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falseNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            <!-- Get interpretation score. -->
            {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
            {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
            {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}


            <!-- Table Rows -->
            <tr>
                <td class="c-center">{{ interpretationSensitivity|round(3, 'common') }}</td>
                <td class="c-center">{{ interpretationSpecificity|round(3, 'common') }}</td>
                <td class="c-center">{{ interpretationAccuracy|round(3, 'common') }}</td>
            </tr>

        {% endfor %}

    </tbody>
</table>
1

Calling total() is already going to return a 0 if no results are found.

I believe you've just got a math problem. In your denominators, the sums of either:

entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative

or

entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive

or

entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative

are returning 0 calling the division to fail (since you can't divide by 0).

I'm not sure what the actual number you're trying to calculate here it, but you can guard against dividing by 0 with something like:

{% set denominator = entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative %}

{% if denominator > 0 %}
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / denominator %}
{% endif %}
|improve this answer|||||
  • Thanks @bradbell. Let's say I have 3 entries. To calculate Sensitivity, I need to count all entries that have a drop down value of "true positive" and "false negative". If any of the entries don't contain either value then default value is 0 for that item. Lets say out of 3 entries, 2 have a value of "true positive", 1 entry has "true negative" (which is not used in the Sensitivity calculation so it's ignored). My code breaks because I don't have a result for "false negative" which is needed. If it worked it should calculate, 2 "true positive" / (2 "true positive" + 0 "false negative") = 1. – Lori Aug 26 '16 at 22:03
  • Yeah, I get that. But the error indicates the math isn't working out like you're expecting. Enable devMode and when it blows up, it'll show a stack trace where you can figure out the line number of your template it's choking on. (Can add a pic of it to your original question if you want). Also, try outputting the values into your template to make sure they're what you expect. i.e. entriesLogInterpretationTruePositive = {{ entriesLogInterpretationTruePositive }}, etc. – Brad Bell Aug 26 '16 at 22:15
  • Brad @bradbell, looks like it's if none of the entries have results. So in my example, if Sensitivity = 0 / (0 + 0) then I get the Division by zero error. – Lori Aug 26 '16 at 23:06
  • It just hit me what you said Brad. I have the template working just fine and that its actually impossible in the math realm to divide by zero. I have test data in and didn't account for that when I entered in test data. So sorry about that. Should I delete my post since it isn't really a problem? – Lori Aug 26 '16 at 23:45
  • Ahhh... no worries. I'd probably just add your solution as an answer. Entirely possible someone else might have a similar situation. Glad you're all sorted! – Brad Bell Aug 27 '16 at 0:08
1

Turns out my code was correct. My problem was that the test data I had entered in my Interpretation field just happened to calculate where the denominator was "0" which was causing a mathematical error.

What I ended up doing was checking to make sure the denominators sum were greater than 0. If they were then run the calculation code.

<!-- Prevent Division by zero error -->
{% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative > 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive > 0) %}

    <!-- Get interpretation score. -->
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
    {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
    {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}

{% endif %}

Output section:

    <!-- Table Rows -->
    <!-- Prevent Division by zero error -->
    {% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative > 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive > 0) %}
        <td class="c-center">{{ interpretationSensitivity|round(3, 'common') }}</td>
        <td class="c-center">{{ interpretationSpecificity|round(3, 'common') }}</td>
        <td class="c-center">{{ interpretationAccuracy|round(3, 'common') }}</td>
    {% else %}
        <td class="c-center">N/A</td>
        <td class="c-center">N/A</td>
        <td class="c-center">N/A</td>
    {% endif %}

{% endfor %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.