UNFmontreal / Dcm2Bids

Reorganising NIfTI files from dcm2niix into the Brain Imaging Data Structure
https://UNFmontreal.github.io/Dcm2Bids/
GNU General Public License v3.0
160 stars 82 forks source link

Crash when a tag that usually contains a list is not in the JSON sidecar but present in the criteria of the config file #321

Closed po09i closed 1 day ago

po09i commented 2 weeks ago

Summary

Dcm2bids crashes when the configuration file is set up with a list as a criteria but the tag is not defined in the JSON sidecar. I'm assuming that this is unintended behaviour and it should not match the criteria rather than crash the program.

Terminal output

``` lib/python3.10/site-packages/dcm2bids/dcm2bids_gen.py:116: in run parser.build_graph() lib/python3.10/site-packages/dcm2bids/sidecar.py:274: in build_graph if criteria and self.isLink(sidecar.data, criteria): lib/python3.10/site-packages/dcm2bids/sidecar.py:400: in isLink result.append(compare(name, pattern)) lib/python3.10/site-packages/dcm2bids/sidecar.py:294: in compare return bool(re.match(pattern, name)) lib/python3.10/re.py:190: in match return _compile(pattern, flags).match(string) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ pattern = ['.*', '.*', 'M', '.*'], flags = 0 def _compile(pattern, flags): # internal: compile pattern if isinstance(flags, RegexFlag): flags = flags.value try: > return _cache[type(pattern), pattern, flags] E TypeError: unhashable type: 'list' lib/python3.10/re.py:293: TypeError ```

To reproduce:

Example config file:

{
    "search_method": "re",
    "case_sensitive": false,
    "descriptions": [
        {
            "datatype": "anat",
            "suffix": "magnitude1",
            "criteria": {
                "ImageType": [".*", ".*", "M", ".*"]
            }
        }
    ]
}

Example JSON sidecar (CRASH)

{
    "Modality": "MR"
}

Example JSON sidecar (NO CRASH)

{
    "Modality": "MR"
         "ImageType": []
}

Suggestion

I believe the problem comes from here where the default value is set to a string if the tag does not exist but is later compared to a list which creates the crash. The default value should probably be an empty list for these tags.

Linked files

I uploaded my test files for easier replication crash.json pass.json dcm2bids_test.json

Related

Related to https://github.com/shimming-toolbox/shimming-toolbox/issues/557

arnaudbore commented 4 days ago

Thank you @po09i, I'll take a look and fix it by the end of next week !

arnaudbore commented 1 day ago

fixed in dev.