pydicom 中 ds.get() 和 ds.get_item() 有什么区别
What is the difference between ds.get() and ds.get_item() in pydicom
有谁知道FileDataset.get()
和FileDataset.get_item()
这两种方法在Pydicom中有什么区别?
谢谢!
我想您的答案在这两个函数的源代码中。看起来 get()
处理字符串和数据元素作为输入。
def get(self, key, default=None):
"""Extend dict.get() to handle DICOM DataElement keywords.
Parameters
----------
key : str or pydicom.tag.Tag
The element keyword or Tag or the class attribute name to get.
default : obj or None
If the DataElement or class attribute is not present, return
`default` (default None).
Returns
-------
value
If `key` is the keyword for a DataElement in the Dataset then
return the DataElement's value.
pydicom.dataelem.DataElement
If `key` is a tag for a DataElement in the Dataset then return the
DataElement instance.
value
If `key` is a class attribute then return its value.
"""
if isinstance(key, (str, compat.text_type)):
try:
return getattr(self, key)
except AttributeError:
return default
else:
# is not a string, try to make it into a tag and then hand it
# off to the underlying dict
if not isinstance(key, BaseTag):
try:
key = Tag(key)
except Exception:
raise TypeError("Dataset.get key must be a string or tag")
try:
return_val = self.__getitem__(key)
except KeyError:
return_val = default
return return_val
def get_item(self, key):
"""Return the raw data element if possible.
It will be raw if the user has never accessed the value, or set their
own value. Note if the data element is a deferred-read element,
then it is read and converted before being returned.
Parameters
----------
key
The DICOM (group, element) tag in any form accepted by
pydicom.tag.Tag such as [0x0010, 0x0010], (0x10, 0x10), 0x00100010,
etc. May also be a slice made up of DICOM tags.
Returns
-------
pydicom.dataelem.DataElement
"""
if isinstance(key, slice):
return self._dataset_slice(key)
if isinstance(key, BaseTag):
tag = key
else:
tag = Tag(key)
data_elem = dict.__getitem__(self, tag)
# If a deferred read, return using __getitem__ to read and convert it
if isinstance(data_elem, tuple) and data_elem.value is None:
return self[key]
return data_elem
这两个在用户代码中都不经常使用。 Dataset.get
相当于 python 的 dict.get;它允许您在字典中查询一个项目,但如果数据集中不存在该项目,则为 return 默认值。从数据集中获取项目的更常用方法是使用点符号,例如
dataset.PatientName
或通过标签号获取DataElement
对象,例如
dataset[0x100010]
Dataset.get_item
是一个更底层的例程,主要用于一些传入的数据有问题,需要在"raw data element"值转换为[=32=之前进行更正] 标准类型(整数、浮点数、字符串类型等)。
当与关键字一起使用时,Dataset.get()
return 是一个 值 ,而不是 DataElement
实例。 Dataset.get_item
始终 return 是 DataElement
实例或 RawDataElement
实例。
有谁知道FileDataset.get()
和FileDataset.get_item()
这两种方法在Pydicom中有什么区别?
谢谢!
我想您的答案在这两个函数的源代码中。看起来 get()
处理字符串和数据元素作为输入。
def get(self, key, default=None):
"""Extend dict.get() to handle DICOM DataElement keywords.
Parameters
----------
key : str or pydicom.tag.Tag
The element keyword or Tag or the class attribute name to get.
default : obj or None
If the DataElement or class attribute is not present, return
`default` (default None).
Returns
-------
value
If `key` is the keyword for a DataElement in the Dataset then
return the DataElement's value.
pydicom.dataelem.DataElement
If `key` is a tag for a DataElement in the Dataset then return the
DataElement instance.
value
If `key` is a class attribute then return its value.
"""
if isinstance(key, (str, compat.text_type)):
try:
return getattr(self, key)
except AttributeError:
return default
else:
# is not a string, try to make it into a tag and then hand it
# off to the underlying dict
if not isinstance(key, BaseTag):
try:
key = Tag(key)
except Exception:
raise TypeError("Dataset.get key must be a string or tag")
try:
return_val = self.__getitem__(key)
except KeyError:
return_val = default
return return_val
def get_item(self, key):
"""Return the raw data element if possible.
It will be raw if the user has never accessed the value, or set their
own value. Note if the data element is a deferred-read element,
then it is read and converted before being returned.
Parameters
----------
key
The DICOM (group, element) tag in any form accepted by
pydicom.tag.Tag such as [0x0010, 0x0010], (0x10, 0x10), 0x00100010,
etc. May also be a slice made up of DICOM tags.
Returns
-------
pydicom.dataelem.DataElement
"""
if isinstance(key, slice):
return self._dataset_slice(key)
if isinstance(key, BaseTag):
tag = key
else:
tag = Tag(key)
data_elem = dict.__getitem__(self, tag)
# If a deferred read, return using __getitem__ to read and convert it
if isinstance(data_elem, tuple) and data_elem.value is None:
return self[key]
return data_elem
这两个在用户代码中都不经常使用。 Dataset.get
相当于 python 的 dict.get;它允许您在字典中查询一个项目,但如果数据集中不存在该项目,则为 return 默认值。从数据集中获取项目的更常用方法是使用点符号,例如
dataset.PatientName
或通过标签号获取DataElement
对象,例如
dataset[0x100010]
Dataset.get_item
是一个更底层的例程,主要用于一些传入的数据有问题,需要在"raw data element"值转换为[=32=之前进行更正] 标准类型(整数、浮点数、字符串类型等)。
当与关键字一起使用时,Dataset.get()
return 是一个 值 ,而不是 DataElement
实例。 Dataset.get_item
始终 return 是 DataElement
实例或 RawDataElement
实例。