【问题标题】:Can pydicom tags be accessed by dot notation?可以通过点符号访问 pydicom 标签吗?
【发布时间】:2019-03-21 19:11:49
【问题描述】:

一个非常简单的pydicom 示例涉及读取文件,然后输出Series Time

import pydicom
info = pydicom.dcmread("file.dcm")
print(info.SeriesTime)

因此,对于已读取的对象,点表示法可以替代更复杂的结构,例如print(info[pydicom.tag.Tag((0x0008, 0x0031))])print(info[0x0008, 0x0031])。不错。

现在,pydicom 仅支持读取部分标签以提高性能:

info = pydicom.dcmread("file.dcm", specific_tags=['SeriesTime', ])

我不喜欢在代码中使用字符串常量。那么在上面的例子中,'SeriesTime' 的可读替代品是什么? specific_tags=[pydicom.tag.Tag((0x0008, 0x0031)), ] 不是特别可读。

理想情况下,我希望能够通过点符号访问标签字典,例如pydicom.allTags.SeriesTime,但我似乎找不到。

【问题讨论】:

    标签: pydicom


    【解决方案1】:

    这是另一种相对简单的方法来访问 pydicom 字典:

    from pydicom.datadict import tag_for_keyword
    from pydicom.tag import Tag
    
    class DottedDcmDict(object):
        def __getattr__(self, name):
            tag = tag_for_keyword(name)
            if tag:
                return Tag(tag)
            raise AttributeError("Not a valid DICOM identifier")
    
    dd = DottedDcmDict()
    print(dd.SeriesTime)
    (0008, 0031)
    
    

    【讨论】:

      【解决方案2】:

      您正在寻找的是数据字典。在 Pydicom 中,您可以在这里找到它: pydicom.datadict.DicomDictionary 但这是一个元组字典,并不适合通过点符号访问。例如,要检索“SeriesTime”,您必须编写 pydicom.datadict.DicomDictionary[524337][4],它既不可读,也不保证在 Pydicom 的更新中保持有效(尽管我怀疑它是否会经常更改)。

      您可以使用命名元组自己实现:

      from collections import namedtuple
      import pydicom
      
      def get_dict_as_namedtuple(pdict):
          # some keywords are empty, check for those
          keywords_list = [pdict[i][4] for i in pdict if pdict[i][4].strip() != '']
          Keywords = namedtuple('Keywords', keywords_list)
          # unpack the list as positional arguments
          return Keywords(*keywords_list)
      
      dicom_tags = get_dict_as_namedtuple(pydicom.datadict.DicomDictionary)
      
      print(dicom_tags.SeriesTime)
      # prints 'SeriesTime'
      
      

      或者我最初的不太优雅的解决方案(选项卡自动完成不适用于此解决方案):
      (通过this answer 找到的Fabric codebase 中的AttributeDict 示例代码)

      import pydicom
      class AttributeDict(dict):
      
          def __getattr__(self, key):
              try:
                  return self[key]
              except KeyError:
                  # to conform with __getattr__ spec
                  raise AttributeError(key)
      
          def __setattr__(self, key, value):
              self[key] = value
      
          def first(self, *names):
              for name in names:
                  value = self.get(name)
                  if value:
                      return value
      
      def get_data_dict(pydicom_data_dict):
              data_dict = AttributeDict({})
              for entry in pydicom_data_dict:
                  keyword = pydicom_data_dict[entry][4]
                  data_dict[keyword] = keyword
              return data_dict
      
      pydicom_data_dict = get_data_dict(pydicom.datadict.DicomDictionary)
      print(pydicom_data_dict.SeriesTime)
      #prints 'SeriesTime'
      

      【讨论】:

        猜你喜欢
        • 2012-03-05
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-04
        • 1970-01-01
        相关资源
        最近更新 更多