Replies: 2 comments 1 reply
-
The following doesn't yield exactly the same output as above, but is fairly similar. Could this be adapted to your needs? def as_dict(ds):
return {
el.keyword: (
el.value if el.VR != "SQ" else tuple(as_dict(ds) for ds in el.value)
)
for el in ds
if el.keyword != "PixelData"
}
if __name__ == "__main__":
from pydicom.data import get_testdata_file
from pydicom import dcmread
from pprint import pprint
ds = dcmread(get_testdata_file("CT_small.dcm"))
ds.remove_private_tags()
pprint(as_dict(ds), sort_dicts=False) which gives:
Your point about the |
Beta Was this translation helpful? Give feedback.
-
I had to run earlier, so there were a couple of thoughts I didn't put in the last post. One is, in constructing the dict comprehension Similarly you could wrap the key as well to handle private elements, or just handle that directly, e.g. def as_dict(ds):
return {
el.keyword if not el.tag.is_private else f"{el.tag:08X}": (
el.value if el.VR != "SQ" else list(as_dict(ds) for ds in el.value)
)
for el in ds
if el.keyword != "PixelData"
} Or, the comprehension form could be abandoned and just a regular loop used with multiple And on the pydicom Also, I've changed the |
Beta Was this translation helpful? Give feedback.
-
To follow up on our discussion on twitter:
I feel like it is too complicated to construct a metadata dataframe using pydicom.
I want to have the tag keywords as column names (not the tags itself) and the value of the tag as the value in the dataframe.
The code I'm currently using is as follows (heavily inspired by fastai):
(it ignores multivalues currently)
Iterating over the keys and then extracting the keywords and values with
.value
and.keyword
seems a bit convoluted.It would be nice if
.to_json_dict()
had an argument that allows the tag keywords as dict keys and only the values as dict values.Or am I misunderstanding something and there is a straightforward way to get what I want?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions