Skip to content

Display choices edgecase nodes#16

Open
yara-elkady wants to merge 12 commits intomainfrom
display-choices-edgecase-nodes
Open

Display choices edgecase nodes#16
yara-elkady wants to merge 12 commits intomainfrom
display-choices-edgecase-nodes

Conversation

@yara-elkady
Copy link
Contributor

@yara-elkady yara-elkady commented Mar 6, 2026

Overview

This PR improves how link labels are generated for calculate nodes in the Mermaid form diagram. Previously, calculate nodes link labels didn’t appear. The main change in this PR is that now it retrieves choice labels directly from the Excel configuration (choices_config) when they are not available in the tree, while preserving the original behavior for other cases.

Summary of Changes

1. Retrieve labels from Excel configuration for calculate variables
Calculate variables do not contain choices in the generated form tree (reason why is not clear). However, their corresponding labels may exist in the Excel configuration used to build the form. The logic now checks if the calculate node already has choices attached in the tree. If not, it attempts to retrieve the choices from choices_config, which is populated from the Excel file. The retrieved choices are converted into Choice objects and filtered using the existing filter_choices logic based on the CART rule. If matching labels are found, they are used for the diagram link.

Examples from IDN TT (these labels were missing before)
image
image

2. Attach choices to dead-end nodes (exit_deadends) - (TODO: Test This)
In options.py, the exit_deadends function was updated to set New_node.question.choices_from_parent when generating nodes for dead-end paths. Dead-end nodes are created when the CART tree reaches a branch without a valid next node. Since these nodes are not produced through the standard CART branching logic, they do not automatically carry the choice labels required for diagram edges. By attaching choices_from_parent, the diagram generation logic can correctly display the labels for these edges.

3. Handle empty or missing cart_value in filter_choices
The filter_choices function was updated to skip choices with missing target values. This prevents runtime errors when the code attempts to convert an empty or missing value to a numeric type during CART rule comparisons (e.g. when evaluating conditions like > 1.5).
Without this safeguard, the filtering logic raises exceptions when encountering non-numeric values.

An example of this would be the vehic_index_cat variable in IDN TT in which is a 'calculate' node but doesn't have target values and the variable vehic_index is not present in the excel configuration so it would raise an exception.

image

Current Issue (Not resolved)

  • There is a case in which if a calculate node is pointing to a question node then the label dissapears
image

Notes
There were changes to the create_xlsform pipeline too and its PR should be merged before this one to avoid errors. PR Link

return ", ".join(labels)
# Fallback
if node.parent.question.type == "calculate" and node.cart_rule:
return f"'{node.cart_rule.operator} {node.cart_rule.value}'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were quoting the label with single quotes because the rendering in Whimsical was broken without. Did they fix that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I wasn't aware, I just thought it looked better without quotes and its working without errors so it seems to be fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants