Providing raw RegEx to debug BIDS file naming / conversion issues with Heudiconv

The BIDS-validator app is useful for quick validation, but the debug information it generates does not help in determining exactly how to fix file naming scripts in heudiconv. The BIDS spec sections 7 & 8 goes into some detail, but it’s not useful for debugging code. The code of the BIDS validator in this file contains RegEx written with javascript helper functions.

There are several online RegEx testing tools, like https://www.debuggex.com/ or https://www.regextester.com/ that can be used to debug RegEx tests and would make it easier to debug heuristic code. What is needed is to convert the JS into an expanded RegEx expression. This topic is being posted in order to share full RegEx for each acquisition type, so developers can copy-paste them into the tool of their choice.

Below is my first attempt to convert the isFieldMap() function at line 167 into an expanded RegEx. I’ll be updating this and posting others below as I work through it.

FMAP path validator Regex - click here for debugging tool

\/(sub-[a-zA-Z0-9]+)\/(?:(ses-[a-zA-Z0-9]+)\/)?fmap\/1(_2)?(?:_acq-[a-zA-Z0-9]+)?(?:_rec-[a-zA-Z0-9]+)?(?:_dir-[a-zA-Z0-9]+)?(?:_run-[0-9]+)?_(?:phasediff|phase1|phase2|magnitude1|magnitude2|magnitude|fieldmap|epi).(nii.gz|nii|json)
1 Like

Thanks for taking the initiative. Could you elaborate on what is an “extended regular expression” and why the expressions found in type.js cannot be used in online regex debuggers without modifications?

I’m not suggesting that type.js needs to change.
I’m testing heudiconv generated BIDS scans against the online BIDS-validator and getting many occurrences of the error:

Files with such naming scheme are not part of BIDS specification. This error is most commonly caused by typos in file names that make them not BIDS compatible…

So I need to debug the heuristic so that file names will match the validator’s regex. The validator doesn’t provide fine grained debug information about exactly what part of the file name & path are wrong, but there are online regex tools that do, click here for example. In order to use those tools, the full regular expression is needed, whereas type.js expresses the regex like this:

 isFieldMap: function (path) {
        var suffixes = ["phasediff", "phase1", "phase2", "magnitude1", "magnitude2", "magnitude", "fieldmap", "epi"];
        var anatRe = new RegExp('^\\/(sub-[a-zA-Z0-9]+)' +
            '\\/(?:(ses-[a-zA-Z0-9]+)' +
            '\\/)?fmap' +
            '\\/\\1(_\\2)?(?:_acq-[a-zA-Z0-9]+)?(?:_rec-[a-zA-Z0-9]+)?(?:_dir-[a-zA-Z0-9]+)?(?:_run-[0-9]+)?_(?:'
            + suffixes.join("|")
            + ').(nii.gz|nii|json)$');
        return conditionalMatch(anatRe, path);
    },
1 Like

Refactored RegEx pushed into separate repository.