brainlife / ezbids

A web service for semi-automated conversion of raw imaging data to BIDS

Home Page:https://brainlife.io/ezbids

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iterating across dictionary returns keys as strings, which cannot be indexed by strings

DanNBullock opened this issue · comments

suffixes = [x for y in [x["suffixes"] for x in rule] for x in y]

Trying to run the relevant sub-component of this line returns this error:

[x["suffixes"] for x in rule]
Traceback (most recent call last):

File "/tmp/ipykernel_84714/87350421.py", line 1, in
[x["suffixes"] for x in rule]

File "/tmp/ipykernel_84714/87350421.py", line 1, in
[x["suffixes"] for x in rule]

TypeError: string indices must be integers

To confirm this issue I ran:

[type(x) for x in rule]
Out[65]: [str, str, str, str, str, str, str, str, str]

Maybe extracting rule.keys() and iterating across that list with a regular for loop would achieve the desired outcome?

rule in this case is a list of dictionaries, so the line of code is rather specific to the data structure of rule

It's the result of reading in one of the BIDS specification rules files (example here) via yaml.load()

Maybe something is going on with how mine is loading on this line, but it is definitely showing up as a dict:

type(rule)
Out[69]: dict

len(rule)
Out[70]: 9

rule.keys()
Out[71]: dict_keys(['nonparametric', 'parametric', 'defacemask', 'multiecho', 'multiflip', 'multiinversion', 'mp2rage', 'vfamt', 'mtr'])

datatype
Out[72]: 'anat'

datatype_suffix_rules
Out[73]: '../bids-specification/src/schema/rules/datatypes'

Is something going wrong with how I'm loading on line 799?

Edit: as an addendum, this worked for me: suffixes = [x for y in [rule[x]["suffixes"] for x in rule] for x in y]

double Edit: one way to create some robustness to this potential edge case would be:

                if isinstance(rule,list):
                    suffixes = [x for y in [x["suffixes"] for x in rule] for x in y]
                elif isinstance(rule,dict):
                    suffixes = [x for y in [rule[x]["suffixes"] for x in rule] for x in y]

If you're running the sourcecode locally, can you change lines 36-40 to this:

datatypes_yaml = yaml.load(open("../../bids-specification/src/schema/objects/datatypes.yaml"), Loader=yaml.FullLoader)
entities_yaml = yaml.load(open("../../bids-specification/src/schema/objects/entities.yaml"), Loader=yaml.FullLoader)
suffixes_yaml = yaml.load(open("../../bids-specification/src/schema/objects/suffixes.yaml"), Loader=yaml.FullLoader)
datatype_suffix_rules = "../../bids-specification/src/schema/rules/datatypes"
entity_ordering_file = "../../bids-specification/src/schema/rules/entities.yaml"

So they updated the structures of bids-specification recently. We are still using the older version of bids specification because we haven't had the time to upgrade our code to use the new structure. Is this maybe caused by ezBIDS code expecting old structure and Dan B. is using the latest version of bids-specification?

Ah, right. I'll work on updating the analyzer to handle the new structure next week

We also need to update our UI codes.

Yes, I was using the latest pull from the Bids repo, so that may be the relevant factor.

Edit: The code snippet at the bottom of #54 (comment) could deal with either BIDS repo case without having to make substantial changes, I think