Skip to content

Compare grRules as logic in diff_models (order-insensitive) - #74

Merged
edkerk merged 2 commits into
developfrom
docs/diffmodels-gpr-backport
Jul 18, 2026
Merged

Compare grRules as logic in diff_models (order-insensitive)#74
edkerk merged 2 commits into
developfrom
docs/diffmodels-gpr-backport

Conversation

@edkerk

@edkerk edkerk commented Jul 17, 2026

Copy link
Copy Markdown
Member

diff_models' GPR check compared grRules as text_normalise_gpr only lowercased and collapsed whitespace — so it flagged two logically identical rules that differed only in operand order (a and b vs b and a) as a difference. MATLAB RAVEN's diffModels (RAVEN #686, itself a port of this diff.py) already compares them as logic. This brings raven-toolbox into line.

Change

_canonical_gpr DNF-expands each rule (via the existing manipulation.gpr_to_dnf), sorts the genes within each isozyme clause and sorts the clauses, so:

  • a and b == b and a (AND operand order)
  • a or b == b or a (isozyme order)
  • (A or B) and C == C and (b or a) (nested, reordered, case-insensitive)
  • a and a == a, and duplicate clauses collapse

...while a real logic change (a and b vs a or b) still registers.

It reads the reaction's already-parsed cobra .gpr (no re-parse) and reports the original rule strings in the diff message while comparing canonically. A rule cobra cannot parse falls back to the old whitespace/lowercase comparison, so malformed rules are still compared rather than silently equated to empty.

Tests

tests/test_comparison_diff.py gains order-insensitivity cases (AND/OR reorder, nesting, dedupe), real-difference cases (AND vs OR, different gene), and two end-to-end diff_models checks: operand-order-only models compare equal, an AND→OR change is flagged. pytest for comparison + expand: 55 passed; ruff, mypy, and the -W docs build clean.

Replaces this branch's earlier doc-only note with the implementation, and drops the now-resolved pending-back-port entry.

edkerk added 2 commits July 17, 2026 22:00
MATLAB RAVEN's diffModels compares grRules as logic (DNF + sort, so
"a and b" == "b and a"), while raven-toolbox's diff_models._normalise_gpr
only lowercases and collapses whitespace. Record the AST-based comparison as
a pending back-port into raven-toolbox.
Implements the back-port this branch previously only documented.
diff_models' GPR check DNF-expands each rule via the existing
manipulation.gpr_to_dnf, sorts genes within each isozyme clause and sorts
the clauses, so operand order no longer registers as a difference:
"a and b" == "b and a", "a or b" == "b or a". The previous _normalise_gpr
only lowercased and collapsed whitespace, flagging logically identical
rules that differed only in operand order.

Uses the reaction's already-parsed cobra .gpr (no re-parse) and reports
the original rule strings in the diff message while comparing canonically.
A rule cobra cannot parse falls back to the old string comparison, so
malformed rules are still compared rather than silently equated to empty.
Brings diff_models in line with MATLAB RAVEN's diffModels (RAVEN #686);
removes the now-resolved pending-back-port note.
@edkerk edkerk changed the title docs: note diffModels grRule comparison to back-port into raven-toolbox Compare grRules as logic in diff_models (order-insensitive) Jul 18, 2026
@edkerk
edkerk merged commit 7ab2c12 into develop Jul 18, 2026
8 checks passed
@edkerk
edkerk deleted the docs/diffmodels-gpr-backport branch July 18, 2026 22:15
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.

1 participant