Skip to content

Add expression interpreter#71

Draft
ubdbra001 wants to merge 13 commits intobids-standard:mainfrom
ubdbra001:i48-add-espression-interpreter
Draft

Add expression interpreter#71
ubdbra001 wants to merge 13 commits intobids-standard:mainfrom
ubdbra001:i48-add-espression-interpreter

Conversation

@ubdbra001
Copy link
Copy Markdown
Collaborator

Here's the initial attempt at adding all the parts of the expression language interpreter.

These successfully cover the full range of tests in the bidsschematools expression tests.
Two things that still need addressing:

  • exists passes the tests but needs more work to actually work as described in the schema tools docs
  • I'm not sure yet how to integrate them with the Context

@ubdbra001 ubdbra001 requested a review from effigies March 10, 2026 22:51
@ubdbra001 ubdbra001 self-assigned this Mar 10, 2026
@ubdbra001 ubdbra001 force-pushed the i48-add-espression-interpreter branch from 4229095 to 19cdfd0 Compare March 11, 2026 09:59
@ubdbra001 ubdbra001 force-pushed the i48-add-espression-interpreter branch from 19cdfd0 to 5bda559 Compare March 11, 2026 10:06
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 20.27027% with 118 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.38%. Comparing base (1963ba3) to head (39d7085).

Additional details and impacted files
@@             Coverage Diff             @@
##             main      #71       +/-   ##
===========================================
- Coverage   90.89%   80.38%   -10.52%     
===========================================
  Files          13       14        +1     
  Lines         846      994      +148     
  Branches      124      175       +51     
===========================================
+ Hits          769      799       +30     
- Misses         47      165      +118     
  Partials       30       30               
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

=== Do not change lines below ===
{
 "chain": [],
 "cmd": "uv lock",
 "exit": 0,
 "extra_inputs": [],
 "inputs": [
  "pyproject.toml",
  "uv.lock"
 ],
 "outputs": [
  "uv.lock"
 ],
 "pwd": "."
}
^^^ Do not change lines above ^^^
=== Do not change lines below ===
{
 "chain": [],
 "cmd": "uv lock",
 "exit": 0,
 "extra_inputs": [],
 "inputs": [
  "pyproject.toml",
  "uv.lock"
 ],
 "outputs": [
  "uv.lock"
 ],
 "pwd": "."
}
^^^ Do not change lines above ^^^
@effigies
Copy link
Copy Markdown
Contributor

effigies commented Mar 16, 2026

From today's call, here's a quick-and-dirty proxy to allow you to provide multiple namespaces and treat them as one:

class LookupProxy:
    def __init__(self, *objs):
        self.objs = objs

    def __getitem__(self, key):
        for obj in self.objs:
            if isinstance(obj, dict) and key in obj:
                return obj[key]
            if hasattr(obj, key):
                return getattr(obj, key)
        raise AttributeError

    # Provide either attr or item lookup
    __getattr__ = __getitem__

Example usage:

import bids_validator as bv
import bids_validator.context
from bids_validator import expression_language as el
from bids_validator.types.files import FileTree
from bidsschematools.schema import load_schema

schema = load_schema()

root = FileTree.read_from_filesystem('tests/data/bids-examples/ds000117/')
ds = bv.context.Dataset(root, schema)

file = root / 'sub-01' / 'ses-mri' / 'anat' / 'sub-01_ses-mri_acq-mprage_T1w.nii.gz'
context = bv.context.Context(file, ds, None)

namespace = LookupProxy(el.ids, context)

@ubdbra001
Copy link
Copy Markdown
Collaborator Author

The LookupProxy class suggested works as is, but I had to make a couple of tweaks to the new_evaluator function to get it working with both the expression tests and actual data.
I had a go with parsing and evaluating some rules from the schema, and, of the limited subset I tried, they all produced the correct output.

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