Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
primitive.AffectedSOPClassUID = '1.1.1'
primitive.AffectedSOPInstanceUID = '1.2.1'
primitive.Priority = 0x02
primitive.MoveOriginatorApplicationEntityTitle = 'UNITTEST'
primitive.MoveOriginatorMessageID = 3
# Test encode without dataset
dimse_msg = C_STORE_RQ()
dimse_msg.primitive_to_message(primitive)
p_data_list = dimse_msg.encode_msg(12, 16)
self.assertEqual(p_data_list[0].presentation_data_value_list[0][1][0:1], b'\x01')
self.assertEqual(p_data_list[-1].presentation_data_value_list[0][1][0:1], b'\x03')
self.assertEqual(dimse_msg.ID, 12)
# Test encode with dataset
ds = Dataset()
ds.PatientID = 'Test1101'
ds.PatientName = 'Tube^HeNe'
primitive.DataSet = BytesIO(encode(ds, True, True))
dimse_msg = C_STORE_RQ()
dimse_msg.primitive_to_message(primitive)
p_data_list = dimse_msg.encode_msg(13, 10)
self.assertEqual(p_data_list[0].presentation_data_value_list[0][1][0:1], b'\x01')
self.assertEqual(p_data_list[-1].presentation_data_value_list[0][1][0:1], b'\x02')
self.assertEqual(p_data_list[-2].presentation_data_value_list[0][1][0:1], b'\x00')
self.assertEqual(p_data_list[-10].presentation_data_value_list[0][1][0:1], b'\x03')
self.assertEqual(dimse_msg.ID, 13)
p_data_list = dimse_msg.encode_msg(1, 31682)
self.assertEqual(p_data_list[0].presentation_data_value_list[0][1], c_store_rq_cmd)
def setUp(self):
"""Run prior to each test"""
self.ds = Dataset()
self.ds.PatientName = '*'
self.ds.QueryRetrieveLevel = "PATIENT"
self.assertEqual(ds.LUTDescriptor[0], 1)
self.assertEqual(ds[0x00283002].VR, 'US')
self.assertEqual(ds.LUTData, 256)
self.assertEqual(ds[0x00283006].VR, 'US')
# If LUTDescriptor[0] is not 1 then LUTData VR is 'OW'
ref_ds.LUTDescriptor = b'\x02\x00\x00\x01\x10\x00' # 2\256\16
ref_ds.LUTData = b'\x00\x01\x00\x02'
ds = correct_ambiguous_vr(deepcopy(ref_ds), True) # Little endian
self.assertEqual(ds.LUTDescriptor[0], 2)
self.assertEqual(ds[0x00283002].VR, 'US')
self.assertEqual(ds.LUTData, b'\x00\x01\x00\x02')
self.assertEqual(ds[0x00283006].VR, 'OW')
# If no LUTDescriptor then VR should be unchanged
ref_ds = Dataset()
ref_ds.LUTData = b'\x00\x01'
ds = correct_ambiguous_vr(deepcopy(ref_ds), True)
self.assertEqual(ds.LUTData, b'\x00\x01')
self.assertEqual(ds[0x00283006].VR, 'US or OW')
"""Test reading only preamble and command set"""
preamble = b'\x00' * 128
prefix = b'DICM'
command = (b'\x00\x00\x00\x00\x04\x00\x00\x00\x38'
b'\x00\x00\x00\x00\x00\x02\x00\x12\x00\x00'
b'\x00\x31\x2e\x32\x2e\x38\x34\x30\x2e\x31'
b'\x30\x30\x30\x38\x2e\x31\x2e\x31\x00\x00'
b'\x00\x00\x01\x02\x00\x00\x00\x30\x00\x00'
b'\x00\x10\x01\x02\x00\x00\x00\x07\x00\x00'
b'\x00\x00\x08\x02\x00\x00\x00\x01\x01')
bytestream = preamble + prefix + command
fp = BytesIO(bytestream)
ds = read_file(fp, force=True)
self.assertTrue('MessageID' in ds)
self.assertEqual(ds.file_meta, Dataset())
def test_meta_unchanged(self):
"""Test that the meta dataset doesn't change when writing it"""
# Empty
meta = Dataset()
write_file_meta_info(self.fp, meta, enforce_standard=False)
self.assertEqual(meta, Dataset())
# Incomplete
meta = Dataset()
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
ref_meta = deepcopy(meta)
write_file_meta_info(self.fp, meta, enforce_standard=False)
self.assertEqual(meta, ref_meta)
# Conformant
meta = Dataset()
meta.FileMetaInformationGroupLength = 62 # Correct length
def test_file_meta_unchanged(self):
"""Test no file_meta elements are added if missing."""
ds = read_file(rtplan_name)
ds.file_meta = Dataset()
ds.save_as(self.fp, write_like_original=True)
self.assertEqual(ds.file_meta, Dataset())
block_ds2.BlockName = "Block2"
beam = Dataset()
# note that you should add beam data elements like BeamName, etc; these are
# skipped in this example
plan_ds = Dataset()
# starting from scratch since we did not read a file
plan_ds.BeamSequence = Sequence([beam])
plan_ds.BeamSequence[0].BlockSequence = Sequence([block_ds1, block_ds2])
plan_ds.BeamSequence[0].NumberOfBlocks = 2
beam0 = plan_ds.BeamSequence[0]
print('Number of blocks: {}'.format(beam0.BlockSequence))
# create a new data set
block_ds3 = Dataset()
# add data elements to it as above and don't forget to update Number of Blocks
# data element
beam0.BlockSequence.append(block_ds3)
del plan_ds.BeamSequence[0].BlockSequence[1]
def json2pydicom(jsn):
""" Convert the supplied json dict into
a PyDicom object """
dataset = pydicom.dataset.Dataset()
# Don't try to convert couch specific tags
dicom_keys = [
key for key in jsn.keys()
if key not in ['_rev', '_id', '_attachments', 'file_meta']
]
for key in dicom_keys:
dataset.add(__dicomify(key, jsn[key]))
file_meta = pydicom.dataset.Dataset()
for key in jsn['file_meta']:
file_meta.add(__dicomify(key, jsn['file_meta'][key]))
dataset.file_meta = file_meta
return dataset
Working with sequences
======================
This examples illustrates how to work with sequences.
"""
from pydicom.sequence import Sequence
from pydicom.dataset import Dataset
# create to toy datasets
block_ds1 = Dataset()
block_ds1.BlockType = "APERTURE"
block_ds1.BlockName = "Block1"
block_ds2 = Dataset()
block_ds2.BlockType = "APERTURE"
block_ds2.BlockName = "Block2"
beam = Dataset()
# note that you should add beam data elements like BeamName, etc; these are
# skipped in this example
plan_ds = Dataset()
# starting from scratch since we did not read a file
plan_ds.BeamSequence = Sequence([beam])
plan_ds.BeamSequence[0].BlockSequence = Sequence([block_ds1, block_ds2])
plan_ds.BeamSequence[0].NumberOfBlocks = 2
beam0 = plan_ds.BeamSequence[0]
print('Number of blocks: {}'.format(beam0.BlockSequence))
# create a new data set
def _generate_dicom_roi_info(plan, frame_data, roi_data):
plan.StructureSetROISequence = []
plan.ROIContourSequence = []
for roi_number in sorted(roi_data.keys()):
roi = roi_data[roi_number]
roi_ds = dicom.dataset.Dataset()
roi_ds.ROINumber = roi_number
roi_ds.ROIName = roi['name']
roi_ds.ReferencedFrameOfReferenceUID = frame_data['FrameOfReferenceUID']
plan.StructureSetROISequence.append(roi_ds)
contour_ds = dicom.dataset.Dataset()
contour_ds.ReferencedROINumber = roi_number
contour_ds.ROIDisplayColor = _string_list(roi['color'])
contour_ds.ContourSequence = []
image_num = 1
for frame_id in sorted(_float_list(roi['contour'].keys())):
for points in roi['contour'][str(frame_id)]:
image_ds = dicom.dataset.Dataset()
image_ds.ContourGeometricType = 'CLOSED_PLANAR'
image_ds.ContourNumber = str(image_num)