def createStructDS(self, planData, ImageInfoUIDs, setupPosition, roiShiftVector): print("Creating Data structure") # create RS SOPInstanceUID structSOPInstanceUID = pydicom.uid.generate_uid() structSeriesInstanceUID = pydicom.uid.generate_uid() # get image header info from ImageSet_0.ImageInfo structFrameUID = '' structStudyInstanceUID = '' structSeriesUID = '' structClassUID = '1.2.840.10008.5.1.4.1.1.481.3' if "ImageInfoList" in ImageInfoUIDs: structFrameUID = ImageInfoUIDs.ImageInfoList[0].FrameUID structStudyInstanceUID = ImageInfoUIDs.ImageInfoList[ 0].StudyInstanceUID structSeriesUID = ImageInfoUIDs.ImageInfoList[0].SeriesUID # structClassUID = ImageInfoUIDs.ImageInfoList[0].ClassUID # Populate required values for file meta information file_meta = Dataset() # RT Structure Set Storage file_meta.MediaStorageSOPClassUID = structClassUID file_meta.MediaStorageSOPInstanceUID = structSOPInstanceUID structfilename = "RS." + structSOPInstanceUID + ".dcm" # this value remains static since implementation for creating file is the same file_meta.ImplementationClassUID = '1.2.826.0.1.3680043.8.498.75006884747854523615841001' # Create the FileDataset instance (initially no data elements, but file_meta supplied) ds = FileDataset(structfilename, {}, file_meta=file_meta, preamble=b'\x00' * 128) # print(file_meta.preamble) # add info_data,basic patientinfo # [0008,0005] - [0008,0018] ds.SpecificCharacterSet = 'ISO_IR 100' ds.InstanceCreationDate = time.strftime("%Y%m%d") ds.InstanceCreationTime = time.strftime("%H%M%S") ds.SOPClassUID = structClassUID ds.SOPInstanceUID = structSOPInstanceUID ds.Modality = 'RTSTRUCT' ds.AccessionNumber = "" ds.Manufacturer = 'Pinnalce3' # from sample dicom file, maybe should change? # not sure where to get information for this element can find this and read in from ds.StationName = "adacp3u7" # ds.ManufacturersModelName = 'Pinnacle3' ds = self.modifyPatientInfo(ds, planData) # [0008,1110] ds.ReferencedStudySequence = Sequence() ReferencedStudy1 = Dataset() ds.ReferencedStudySequence.append(ReferencedStudy1) # Study Component Management SOP Class (chosen from template) ds.ReferencedStudySequence[ 0].ReferencedSOPClassUID = '1.2.840.10008.3.1.2.3.2' ds.ReferencedStudySequence[ 0].ReferencedSOPInstanceUID = structStudyInstanceUID # ds.StudyInstanceUID = StudyInstanceUID print("Setting structure file study instance: " + str(structStudyInstanceUID)) # [0020,000d] ds.StudyInstanceUID = structStudyInstanceUID # [0020,000e] ds.SeriesInstanceUID = structSeriesInstanceUID # [3006,0010] ds.ReferencedFrameOfReferenceSequence = Sequence() ReferencedFrameofReference1 = Dataset() ds.ReferencedFrameOfReferenceSequence.append( ReferencedFrameofReference1) ds.ReferencedFrameOfReferenceSequence[ 0].FrameofReferenceUID = structFrameUID # [3006,0012] ds.ReferencedFrameOfReferenceSequence[ 0].RTReferencedStudySequence = Sequence() RTReferencedStudy1 = Dataset() ds.ReferencedFrameOfReferenceSequence[ 0].RTReferencedStudySequence.append(RTReferencedStudy1) ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].ReferencedSOPClassUID = '1.2.840.10008.3.1.2.3.2' ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].ReferencedSOPInstanceUID = structStudyInstanceUID # ds.StudyInstanceUID = StudyInstanceUID # [3006,0014] ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence = Sequence() RTReferencedSeries1 = Dataset() ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence.append(RTReferencedSeries1) ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence[ 0].SeriesInstanceUID = structSeriesUID # [3006,0016] ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence[0].ContourImageSequence = Sequence() # [fffe,e000] for i, value in enumerate(ImageInfoUIDs.ImageInfoList, 1): exec("ContourImage%d = Dataset()" % i) exec( "ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[0].RTReferencedSeriesSequence[0].ContourImageSequence.append(ContourImage%d)" % i) ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence[0].ContourImageSequence[ i - 1].ReferencedSOPClassUID = value.ClassUID ds.ReferencedFrameOfReferenceSequence[0].RTReferencedStudySequence[ 0].RTReferencedSeriesSequence[0].ContourImageSequence[ i - 1].ReferencedSOPInstanceUID = value.InstanceUID # exec("del ContourImage%d" % i) # [3006,0020] roiListData = planData.planROIsRawData.roiList ds.StructureSetROISequence = Sequence() for i, value in enumerate(roiListData, 1): exec("ROISet%d = Dataset()" % i) exec("ds.StructureSetROISequence.append(ROISet%d)" % i) ds.StructureSetROISequence[i - 1].ROIName = value.name ds.StructureSetROISequence[i - 1].ROINumber = i ds.StructureSetROISequence[ i - 1].ReferencedFrameOfReferenceUID = structFrameUID if 'volume' in value: ds.StructureSetROISequence[i - 1].ROIVolume = value.volume ds.StructureSetROISequence[ i - 1].ROIGenerationAlgorithm = value.roiinterpretedtype # [3006,0039]get each ROI ds.ROIContourSequence = Sequence() for i, value in enumerate(roiListData, 1): exec("ContourSequence%d = Dataset()" % i) exec("ds.ROIContourSequence.append(ContourSequence%d)" % i) ds.ROIContourSequence[i - 1].ROIDisplayColor = [0, 255, 0] ds.ROIContourSequence[i - 1].ReferencedROINumber = i # get all curves in current ROI ds.ROIContourSequence[i - 1].ContourSequence = Sequence() planROIsCurvesList = value.num_curve # get each ROI_Curvers for j, data in enumerate(planROIsCurvesList, 1): exec("CurvesPoint%d = Dataset()" % j) exec( "ds.ROIContourSequence[i - 1].ContourSequence.append(CurvesPoint%d)" % j) # [3006,0040] ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourImageSequence = Sequence() coutourImage1 = Dataset() ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourImageSequence.append(coutourImage1) ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourImageSequence[ 0].ReferencedSOPClassUID = structClassUID ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourImageSequence[ 0].ReferencedSOPInstanceUID = self.getCTInstanceUID( data.Points[0], setupPosition, ImageInfoUIDs) # [3006,0042] ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourGeometricType = "CLOSED_PLANAR" ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].NumberOfContourPoints = data.num_points # get each ROI_Curves_Points, using data.Points ds.ROIContourSequence[i - 1].ContourSequence[ j - 1].ContourData = self.getContourCurvePoints( data.Points, setupPosition, roiShiftVector) # [3006,0080] ds.RTROIObservationsSequence = Sequence() for i, current_roi in enumerate(ds.StructureSetROISequence, 1): exec("Observation%d = Dataset()" % i) exec("ds.RTROIObservationsSequence.append(Observation%d)" % i) ds.RTROIObservationsSequence[ i - 1].ObservationNumber = current_roi.ROINumber ds.RTROIObservationsSequence[ i - 1].ReferencedROINumber = current_roi.ROINumber ds.RTROIObservationsSequence[i - 1].RTROIInterpretedType = 'ORGAN' ds.RTROIObservationsSequence[i - 1].ROIInterpreter = "" # find out where to get if its been approved or not ds.ApprovalStatus = 'UNAPPROVED' # Set the transfer syntax ds.is_little_endian = True ds.is_implicit_VR = True # # Create the FileDataset instance (initially no data elements, but file_meta supplied) # structds = FileDataset(structfilename, {}, # file_meta=ds, preamble=b'\x00' * 128) # structfilepath=outputfolder + patientfolder + "/" + structfilename # structds.save_as("structfilepath") # print("Structure file being saved\n") ds.save_as(os.getenv('HOME') + '/PinnWork/' + structfilename) # # dcmds = pydicom.dcmread(ds) # print(dcmds) return ds
def generate_common_dicom_dataset_series(self, slice_count: int, system: Modality) -> list: output_dataset = [] slice_pos = 0 slice_thickness = 0 study_uid = generate_uid() series_uid = generate_uid() frame_of_ref_uid = generate_uid() date_ = datetime.now().date() age = timedelta(days=45 * 365) time_ = datetime.now().time() cols = 2 rows = 2 bytes_per_voxel = 2 for i in range(0, slice_count): file_meta = Dataset() pixel_array = b"\0" * cols * rows * bytes_per_voxel file_meta.MediaStorageSOPClassUID = sop_classes[system][1] file_meta.MediaStorageSOPInstanceUID = generate_uid() file_meta.ImplementationClassUID = generate_uid() tmp_dataset = FileDataset('', {}, file_meta=file_meta, preamble=pixel_array) tmp_dataset.file_meta.TransferSyntaxUID = "1.2.840.10008.1.2.1" tmp_dataset.SliceLocation = slice_pos + i * slice_thickness tmp_dataset.SliceThickness = slice_thickness tmp_dataset.WindowCenter = 1 tmp_dataset.WindowWidth = 2 tmp_dataset.AcquisitionNumber = 1 tmp_dataset.InstanceNumber = i tmp_dataset.SeriesNumber = 1 tmp_dataset.ImageOrientationPatient = [ 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000 ] tmp_dataset.ImagePositionPatient = [ 0.0, 0.0, tmp_dataset.SliceLocation ] tmp_dataset.ImageType = ['ORIGINAL', 'PRIMARY', 'AXIAL'] tmp_dataset.PixelSpacing = [1, 1] tmp_dataset.PatientName = 'John Doe' tmp_dataset.FrameOfReferenceUID = frame_of_ref_uid tmp_dataset.SOPClassUID = sop_classes[system][1] tmp_dataset.SOPInstanceUID = generate_uid() tmp_dataset.SeriesInstanceUID = series_uid tmp_dataset.StudyInstanceUID = study_uid tmp_dataset.BitsAllocated = bytes_per_voxel * 8 tmp_dataset.BitsStored = bytes_per_voxel * 8 tmp_dataset.HighBit = (bytes_per_voxel * 8 - 1) tmp_dataset.PixelRepresentation = 1 tmp_dataset.Columns = cols tmp_dataset.Rows = rows tmp_dataset.SamplesPerPixel = 1 tmp_dataset.AccessionNumber = '2' tmp_dataset.AcquisitionDate = date_ tmp_dataset.AcquisitionTime = datetime.now().time() tmp_dataset.AdditionalPatientHistory = 'UTERINE CA PRE-OP EVAL' tmp_dataset.ContentDate = date_ tmp_dataset.ContentTime = datetime.now().time() tmp_dataset.Manufacturer = 'Mnufacturer' tmp_dataset.ManufacturerModelName = 'Model' tmp_dataset.Modality = sop_classes[system][0] tmp_dataset.PatientAge = '064Y' tmp_dataset.PatientBirthDate = date_ - age tmp_dataset.PatientID = 'ID0001' tmp_dataset.PatientIdentityRemoved = 'YES' tmp_dataset.PatientPosition = 'FFS' tmp_dataset.PatientSex = 'F' tmp_dataset.PhotometricInterpretation = 'MONOCHROME2' tmp_dataset.PixelData = pixel_array tmp_dataset.PositionReferenceIndicator = 'XY' tmp_dataset.ProtocolName = 'some protocole' tmp_dataset.ReferringPhysicianName = '' tmp_dataset.SeriesDate = date_ tmp_dataset.SeriesDescription = 'test series ' tmp_dataset.SeriesTime = time_ tmp_dataset.SoftwareVersions = '01' tmp_dataset.SpecificCharacterSet = 'ISO_IR 100' tmp_dataset.StudyDate = date_ tmp_dataset.StudyDescription = 'test study' tmp_dataset.StudyID = '' if (system == Modality.CT): tmp_dataset.RescaleIntercept = 0 tmp_dataset.RescaleSlope = 1 tmp_dataset.StudyTime = time_ output_dataset.append(tmp_dataset) return output_dataset