本文整理汇总了Python中pydicom.dataset.Dataset.add_new方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.add_new方法的具体用法?Python Dataset.add_new怎么用?Python Dataset.add_new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydicom.dataset.Dataset
的用法示例。
在下文中一共展示了Dataset.add_new方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_message_classes
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def _build_message_classes(message_name):
"""
Create a new subclass instance of DIMSEMessage for the given DIMSE
`message_name`.
Parameters
----------
message_name : str
The name/type of message class to construct, one of the following:
* C-ECHO-RQ
* C-ECHO-RSP
* C-STORE-RQ
* C-STORE-RSP
* C-FIND-RQ
* C-FIND-RSP
* C-GET-RQ
* C-GET-RSP
* C-MOVE-RQ
* C-MOVE-RSP
* C-CANCEL-RQ
* N-EVENT-REPORT-RQ
* N-EVENT-REPORT-RSP
* N-GET-RQ
* N-GET-RSP
* N-SET-RQ
* N-SET-RSP
* N-ACTION-RQ
* N-ACTION-RSP
* N-CREATE-RQ
* N-CREATE-RSP
* N-DELETE-RQ
* N-DELETE-RSP
"""
def __init__(self):
DIMSEMessage.__init__(self)
# Create new subclass of DIMSE Message using the supplied name
# but replace hyphens with underscores
cls = type(message_name.replace('-', '_'),
(DIMSEMessage,),
{"__init__": __init__})
# Create a new Dataset object for the command_set attributes
d = Dataset()
for elem_tag in command_set_elem[message_name]:
tag = Tag(elem_tag)
vr = dcm_dict[elem_tag][0]
# If the required command set elements are expanded this will need
# to be checked to ensure it functions OK
try:
d.add_new(tag, vr, None)
except:
d.add_new(tag, vr, '')
cls.command_set = d
globals()[cls.__name__] = cls
return cls
示例2: testValidAssignment
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def testValidAssignment(self):
"""Sequence: ensure ability to assign a Dataset to a Sequence item"""
ds = Dataset()
ds.add_new((1, 1), 'IS', 1)
# Create a single element Sequence first
seq = Sequence([Dataset(), ])
seq[0] = ds
self.assertEqual(seq[0], ds, "Dataset modified during assignment")
示例3: test_data_element
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_data_element(self):
"""Test Dataset.data_element."""
ds = Dataset()
ds.CommandGroupLength = 120
ds.SkipFrameRangeFlag = 'TEST'
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.BeamSequence = [Dataset()]
ds.BeamSequence[0].PatientName = 'ANON'
assert ds.data_element('CommandGroupLength') == ds[0x00000000]
assert ds.data_element('BeamSequence') == ds[0x300A00B0]
assert ds.data_element('not an element keyword') is None
示例4: test_remove_private_tags
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_remove_private_tags(self):
"""Test Dataset.remove_private_tags"""
ds = Dataset()
ds.CommandGroupLength = 120 # 0000,0000
ds.SkipFrameRangeFlag = 'TEST' # 0008,9460
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.add_new(0x00090010, 'PN', 'CITIZEN^10')
ds.PatientName = 'CITIZEN^Jan' # 0010,0010
ds.remove_private_tags()
self.assertEqual(ds[0x00090000:0x00100000], Dataset())
self.assertTrue('CommandGroupLength' in ds)
self.assertTrue('SkipFrameRangeFlag' in ds)
self.assertTrue('PatientName' in ds)
示例5: test_iterall
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_iterall(self):
"""Test Dataset.iterall"""
ds = Dataset()
ds.CommandGroupLength = 120
ds.SkipFrameRangeFlag = 'TEST'
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.BeamSequence = [Dataset()]
ds.BeamSequence[0].PatientName = 'ANON'
elem_gen = ds.iterall()
self.assertEqual(
ds.data_element('CommandGroupLength'), next(elem_gen))
self.assertEqual(
ds.data_element('SkipFrameRangeFlag'), next(elem_gen))
self.assertEqual(ds[0x00090001], next(elem_gen))
self.assertEqual(
ds.data_element('BeamSequence'), next(elem_gen))
self.assertEqual(
ds.BeamSequence[0].data_element('PatientName'),
next(elem_gen))
示例6: test_getitem_slice_ffff
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_getitem_slice_ffff(self):
"""Test slicing with (FFFF,FFFF)"""
# Issue #92
ds = Dataset()
ds.CommandGroupLength = 120 # 0000,0000
ds.CommandLengthToEnd = 111 # 0000,0001
ds.Overlays = 12 # 0000,51B0
ds.LengthToEnd = 12 # 0008,0001
ds.SOPInstanceUID = '1.2.3.4' # 0008,0018
ds.SkipFrameRangeFlag = 'TEST' # 0008,9460
ds.add_new(0xFFFF0001, 'PN', 'CITIZEN^1')
ds.add_new(0xFFFF0002, 'PN', 'CITIZEN^2')
ds.add_new(0xFFFF0003, 'PN', 'CITIZEN^3')
ds.add_new(0xFFFFFFFE, 'PN', 'CITIZEN^4')
ds.add_new(0xFFFFFFFF, 'PN', 'CITIZEN^5')
assert ds[:][0xFFFFFFFF].value == 'CITIZEN^5'
assert 0xFFFFFFFF not in ds[0x1000:0xFFFFFFFF]
assert 0xFFFFFFFF not in ds[(0x1000):(0xFFFF, 0xFFFF)]
示例7: write_file
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def write_file(filename, dataset, write_like_original=True):
"""Store a FileDataset to the filename specified.
Parameters
----------
filename : str
Name of file to save new DICOM file to.
dataset : FileDataset
Dataset holding the DICOM information; e.g. an object
read with read_file().
write_like_original : boolean
If True (default), preserves the following information from
the dataset:
-preamble -- if no preamble in read file, than not used here
-hasFileMeta -- if writer did not do file meta information,
then don't write here either
-seq.is_undefined_length -- if original had delimiters, write them now too,
instead of the more sensible length characters
- is_undefined_length_sequence_item -- for datasets that belong to a
sequence, write the undefined length delimiters if that is
what the original had.
If False, produces a "nicer" DICOM file for other readers,
where all lengths are explicit.
See Also
--------
pydicom.dataset.FileDataset
Dataset class with relevant attrs and information.
pydicom.dataset.Dataset.save_as
Write a DICOM file from a dataset that was read in with read_file().
save_as wraps write_file.
Notes
-----
Set dataset.preamble if you want something other than 128 0-bytes.
If the dataset was read from an existing dicom file, then its preamble
was stored at read time. It is up to the user to ensure the preamble is still
correct for its purposes.
If there is no Transfer Syntax tag in the dataset, then set
dataset.is_implicit_VR and dataset.is_little_endian
to determine the transfer syntax used to write the file.
"""
# Decide whether to write DICOM preamble. Should always do so unless trying to mimic the original file read in
preamble = getattr(dataset, "preamble", None)
if not preamble and not write_like_original:
preamble = b"\0" * 128
file_meta = dataset.file_meta
if file_meta is None:
file_meta = Dataset()
if 'TransferSyntaxUID' not in file_meta:
if dataset.is_little_endian and dataset.is_implicit_VR:
file_meta.add_new((2, 0x10), 'UI', ImplicitVRLittleEndian)
elif dataset.is_little_endian and not dataset.is_implicit_VR:
file_meta.add_new((2, 0x10), 'UI', ExplicitVRLittleEndian)
elif not dataset.is_little_endian and not dataset.is_implicit_VR:
file_meta.add_new((2, 0x10), 'UI', ExplicitVRBigEndian)
else:
raise NotImplementedError("pydicom has not been verified for Big Endian with Implicit VR")
caller_owns_file = True
# Open file if not already a file object
if isinstance(filename, compat.string_types):
fp = DicomFile(filename, 'wb')
# caller provided a file name; we own the file handle
caller_owns_file = False
else:
fp = DicomFileLike(filename)
try:
if preamble:
fp.write(preamble) # blank 128 byte preamble
_write_file_meta_info(fp, file_meta)
# Set file VR, endian. MUST BE AFTER writing META INFO (which changes to Explicit LittleEndian)
fp.is_implicit_VR = dataset.is_implicit_VR
fp.is_little_endian = dataset.is_little_endian
write_dataset(fp, dataset)
finally:
if not caller_owns_file:
fp.close()
示例8: dummy_dataset
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def dummy_dataset(self):
# This dataset is used by many of the tests
ds = Dataset()
ds.add_new((0x300A, 0x00B2), "SH", "unit001") # TreatmentMachineName
return ds
示例9: test_delitem_slice
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_delitem_slice(self):
"""Test Dataset.__delitem__ using slices."""
ds = Dataset()
ds.CommandGroupLength = 120 # 0000,0000
ds.CommandLengthToEnd = 111 # 0000,0001
ds.Overlays = 12 # 0000,51B0
ds.LengthToEnd = 12 # 0008,0001
ds.SOPInstanceUID = '1.2.3.4' # 0008,0018
ds.SkipFrameRangeFlag = 'TEST' # 0008,9460
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.add_new(0x00090002, 'PN', 'CITIZEN^2')
ds.add_new(0x00090003, 'PN', 'CITIZEN^3')
ds.add_new(0x00090004, 'PN', 'CITIZEN^4')
ds.add_new(0x00090005, 'PN', 'CITIZEN^5')
ds.add_new(0x00090006, 'PN', 'CITIZEN^6')
ds.add_new(0x00090007, 'PN', 'CITIZEN^7')
ds.add_new(0x00090008, 'PN', 'CITIZEN^8')
ds.add_new(0x00090009, 'PN', 'CITIZEN^9')
ds.add_new(0x00090010, 'PN', 'CITIZEN^10')
ds.PatientName = 'CITIZEN^Jan' # 0010,0010
ds.PatientID = '12345' # 0010,0010
ds.ExaminedBodyThickness = 1.223 # 0010,9431
ds.BeamSequence = [Dataset()] # 300A,00B0
ds.BeamSequence[0].PatientName = 'ANON'
# Delete the 0x0009 group
del ds[0x00090000:0x00100000]
self.assertTrue('SkipFrameRangeFlag' in ds)
self.assertFalse(0x00090001 in ds)
self.assertFalse(0x00090010 in ds)
self.assertTrue('PatientName' in ds)
示例10: test_getitem_slice
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_getitem_slice(self):
"""Test Dataset.__getitem__ using slices."""
ds = Dataset()
ds.CommandGroupLength = 120 # 0000,0000
ds.CommandLengthToEnd = 111 # 0000,0001
ds.Overlays = 12 # 0000,51B0
ds.LengthToEnd = 12 # 0008,0001
ds.SOPInstanceUID = '1.2.3.4' # 0008,0018
ds.SkipFrameRangeFlag = 'TEST' # 0008,9460
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.add_new(0x00090002, 'PN', 'CITIZEN^2')
ds.add_new(0x00090003, 'PN', 'CITIZEN^3')
ds.add_new(0x00090004, 'PN', 'CITIZEN^4')
ds.add_new(0x00090005, 'PN', 'CITIZEN^5')
ds.add_new(0x00090006, 'PN', 'CITIZEN^6')
ds.add_new(0x00090007, 'PN', 'CITIZEN^7')
ds.add_new(0x00090008, 'PN', 'CITIZEN^8')
ds.add_new(0x00090009, 'PN', 'CITIZEN^9')
ds.add_new(0x00090010, 'PN', 'CITIZEN^10')
ds.PatientName = 'CITIZEN^Jan' # 0010,0010
ds.PatientID = '12345' # 0010,0010
ds.ExaminedBodyThickness = 1.223 # 0010,9431
ds.BeamSequence = [Dataset()] # 300A,00B0
ds.BeamSequence[0].PatientName = 'ANON'
# Slice all items - should return original dataset
self.assertEqual(ds[:], ds)
# Slice starting from and including (0008,0001)
test_ds = ds[0x00080001:]
self.assertFalse('CommandGroupLength' in test_ds)
self.assertFalse('CommandLengthToEnd' in test_ds)
self.assertFalse('Overlays' in test_ds)
self.assertTrue('LengthToEnd' in test_ds)
self.assertTrue('BeamSequence' in test_ds)
# Slice ending at and not including (0009,0002)
test_ds = ds[:0x00090002]
self.assertTrue('CommandGroupLength' in test_ds)
self.assertTrue('CommandLengthToEnd' in test_ds)
self.assertTrue('Overlays' in test_ds)
self.assertTrue('LengthToEnd' in test_ds)
self.assertTrue(0x00090001 in test_ds)
self.assertFalse(0x00090002 in test_ds)
self.assertFalse('BeamSequence' in test_ds)
# Slice with a step - every second tag
# Should return zeroth tag, then second, fourth, etc...
test_ds = ds[::2]
self.assertTrue('CommandGroupLength' in test_ds)
self.assertFalse('CommandLengthToEnd' in test_ds)
self.assertTrue(0x00090001 in test_ds)
self.assertFalse(0x00090002 in test_ds)
# Slice starting at and including (0008,0018) and ending at and not
# including (0009,0008)
test_ds = ds[0x00080018:0x00090008]
self.assertTrue('SOPInstanceUID' in test_ds)
self.assertTrue(0x00090007 in test_ds)
self.assertFalse(0x00090008 in test_ds)
# Slice starting at and including (0008,0018) and ending at and not
# including (0009,0008), every third element
test_ds = ds[0x00080018:0x00090008:3]
self.assertTrue('SOPInstanceUID' in test_ds)
self.assertFalse(0x00090001 in test_ds)
self.assertTrue(0x00090002 in test_ds)
self.assertFalse(0x00090003 in test_ds)
self.assertFalse(0x00090004 in test_ds)
self.assertTrue(0x00090005 in test_ds)
self.assertFalse(0x00090006 in test_ds)
self.assertFalse(0x00090008 in test_ds)
# Slice starting and ending (and not including) (0008,0018)
self.assertEqual(
ds[(0x0008, 0x0018):(0x0008, 0x0018)],
Dataset())
# Test slicing using other acceptable Tag initialisations
self.assertTrue(
'SOPInstanceUID' in ds[(0x00080018):(0x00080019)])
self.assertTrue(
'SOPInstanceUID' in ds[(0x0008, 0x0018):(0x0008, 0x0019)])
self.assertTrue(
'SOPInstanceUID' in ds['0x00080018':'0x00080019'])
示例11: test_get_item_slice
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
def test_get_item_slice(self):
"""Test Dataset.get_item with slice argument"""
# adapted from test_getitem_slice
ds = Dataset()
ds.CommandGroupLength = 120 # 0000,0000
ds.CommandLengthToEnd = 111 # 0000,0001
ds.Overlays = 12 # 0000,51B0
ds.LengthToEnd = 12 # 0008,0001
ds.SOPInstanceUID = '1.2.3.4' # 0008,0018
ds.SkipFrameRangeFlag = 'TEST' # 0008,9460
ds.add_new(0x00090001, 'PN', 'CITIZEN^1')
ds.add_new(0x00090002, 'PN', 'CITIZEN^2')
ds.add_new(0x00090003, 'PN', 'CITIZEN^3')
elem = RawDataElement(0x00090004, 'PN', 9, b'CITIZEN^4', 0, True, True)
ds.__setitem__(0x00090004, elem)
elem = RawDataElement(0x00090005, 'PN', 9, b'CITIZEN^5', 0, True, True)
ds.__setitem__(0x00090005, elem)
elem = RawDataElement(0x00090006, 'PN', 9, b'CITIZEN^6', 0, True, True)
ds.__setitem__(0x00090006, elem)
ds.PatientName = 'CITIZEN^Jan' # 0010,0010
elem = RawDataElement(0x00100020, 'LO', 5, b'12345', 0, True, True)
ds.__setitem__(0x00100020, elem) # Patient ID
ds.ExaminedBodyThickness = 1.223 # 0010,9431
ds.BeamSequence = [Dataset()] # 300A,00B0
ds.BeamSequence[0].PatientName = 'ANON'
# Slice all items - should return original dataset
assert ds.get_item(slice(None, None)) == ds
# Slice starting from and including (0008,0001)
test_ds = ds.get_item(slice(0x00080001, None))
assert 'CommandGroupLength' not in test_ds
assert 'CommandLengthToEnd' not in test_ds
assert 'Overlays' not in test_ds
assert 'LengthToEnd' in test_ds
assert 'BeamSequence' in test_ds
# Slice ending at and not including (0009,0002)
test_ds = ds.get_item(slice(None, 0x00090002))
assert 'CommandGroupLength' in test_ds
assert 'CommandLengthToEnd' in test_ds
assert 'Overlays' in test_ds
assert 'LengthToEnd' in test_ds
assert 0x00090001 in test_ds
assert 0x00090002 not in test_ds
assert 'BeamSequence' not in test_ds
# Slice with a step - every second tag
# Should return zeroth tag, then second, fourth, etc...
test_ds = ds.get_item(slice(None, None, 2))
assert 'CommandGroupLength' in test_ds
assert 'CommandLengthToEnd' not in test_ds
assert 0x00090001 in test_ds
assert 0x00090002 not in test_ds
# Slice starting at and including (0008,0018) and ending at and not
# including (0009,0008)
test_ds = ds.get_item(slice(0x00080018, 0x00090006))
assert 'SOPInstanceUID' in test_ds
assert 0x00090005 in test_ds
assert 0x00090006 not in test_ds
# Slice starting at and including (0008,0018) and ending at and not
# including (0009,0006), every third element
test_ds = ds.get_item(slice(0x00080018, 0x00090008, 3))
assert 'SOPInstanceUID' in test_ds
assert 0x00090001 not in test_ds
assert 0x00090002 in test_ds
assert not test_ds.get_item(0x00090002).is_raw
assert 0x00090003 not in test_ds
assert 0x00090004 not in test_ds
assert 0x00090005 in test_ds
assert test_ds.get_item(0x00090005).is_raw
assert 0x00090006 not in test_ds
# Slice starting and ending (and not including) (0008,0018)
assert ds.get_item(slice((0x0008, 0x0018),
(0x0008, 0x0018))) == Dataset()
# Test slicing using other acceptable Tag initialisations
assert 'SOPInstanceUID' in ds.get_item(slice(0x00080018, 0x00080019))
assert 'SOPInstanceUID' in ds.get_item(slice((0x0008, 0x0018),
(0x0008, 0x0019)))
assert 'SOPInstanceUID' in ds.get_item(slice('0x00080018',
'0x00080019'))
示例12: __init__
# 需要导入模块: from pydicom.dataset import Dataset [as 别名]
# 或者: from pydicom.dataset.Dataset import add_new [as 别名]
class DIMSEMessage:
def __init__(self):
self.CommandSet = None
self.EncodedDataSet = None
self.DataSet = None
self.encoded_command_set = ''
self.ID = id
self.ts = ImplicitVRLittleEndian # imposed by standard.
if self.__class__ != DIMSEMessage:
self.CommandSet = Dataset()
for ii in self.CommandFields:
self.CommandSet.add_new(ii[1], ii[2], '')
def Encode(self, id, maxpdulength):
"""Returns the encoded message as a series of P-DATA service
parameter objects"""
self.ID = id
pdatas = []
encoded_command_set = dsutils.encode(
self.CommandSet, self.ts.is_implicit_VR, self.ts.is_little_endian)
# fragment command set
pdvs = fragment(maxpdulength, encoded_command_set)
assert ''.join(pdvs) == encoded_command_set
for ii in pdvs[:-1]:
# send only one pdv per pdata primitive
pdata = P_DATA_ServiceParameters()
# not last command fragment
pdata.PresentationDataValueList = [[self.ID, pack('b', 1) + ii]]
pdatas.append(pdata)
# last command fragment
pdata = P_DATA_ServiceParameters()
# last command fragment
pdata.PresentationDataValueList = [[self.ID, pack('b', 3) + pdvs[-1]]]
pdatas.append(pdata)
# fragment data set
#if self.__dict__.has_key('DataSet') and self.DataSet:
if 'DataSet' in self.__dict__ and self.DataSet is not None:
pdvs = fragment(maxpdulength, self.DataSet)
assert ''.join(pdvs) == self.DataSet
for ii in pdvs[:-1]:
pdata = P_DATA_ServiceParameters()
# not last data fragment
pdata.PresentationDataValueList = [
[self.ID, pack('b', 0) + ii]]
pdatas.append(pdata)
pdata = P_DATA_ServiceParameters()
# last data fragment
pdata.PresentationDataValueList = [
[self.ID, pack('b', 2) + pdvs[-1]]]
pdatas.append(pdata)
return pdatas
def Decode(self, pdata):
"""Constructs itself receiving a series of P-DATA primitives.
Returns True when complete, False otherwise."""
if pdata.__class__ != P_DATA_ServiceParameters:
# not a pdata
return False
if pdata is None:
return False
ii = pdata
for vv in ii.PresentationDataValueList:
# must be able to read P-DATA with several PDVs
self.ID = vv[0]
if unpack('b', vv[1][0])[0] in (1, 3):
logger.debug(" command fragment %s", self.ID)
self.encoded_command_set += vv[1][1:]
if unpack('b', vv[1][0])[0] == 3:
logger.debug(" last command fragment %s", self.ID)
self.CommandSet = dsutils.decode(
self.encoded_command_set, self.ts.is_implicit_VR,
self.ts.is_little_endian)
self.__class__ = MessageType[
self.CommandSet[(0x0000, 0x0100)].value]
if self.CommandSet[(0x0000, 0x0800)].value == 0x0101:
# response: no dataset
return True
elif unpack('b', vv[1][0])[0] in (0, 2):
if self.DataSet is None:
self.DataSet = ''
self.DataSet += vv[1][1:]
logger.debug(" data fragment %s", self.ID)
if unpack('b', vv[1][0])[0] == 2:
logger.debug(" last data fragment %s", self.ID)
return True
else:
raise "Error"
return False
def SetLength(self):
# compute length
l = 0
for ii in self.CommandSet.values()[1:]:
l += len(dsutils.encode_element(ii,
#.........这里部分代码省略.........