def create_dcm_file(self): suffix = '.dcm' filename_little_endian = tempfile.NamedTemporaryFile( suffix=suffix).name filename_big_endian = tempfile.NamedTemporaryFile(suffix=suffix).name print("Setting file meta information...") file_meta = Dataset() file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' file_meta.MediaStorageSOPInstanceUID = "1.2.3" file_meta.ImplementationClassUID = "1.2.3.4" print("Setting dataset values...") ds = FileDataset(filename_little_endian, {}, file_meta=file_meta, preamble=b"\0" * 128) ds.PatientName = self.get_patient_name( ) + " " + self.get_patient_surname() ds.PatientID = self.get_patient_id() ds.PatientSex = self.get_patient_sex() ds.PatientAge = self.get_patient_age() ds.PatientWeight = self.get_patient_weight() ds.ImageComment = self.get_patient_comment() ds.PatientBirthDate = self.get_patient_birth() # Set the transfer syntax ds.is_little_endian = True ds.is_implicit_VR = True # Set creation date/time dt = datetime.datetime.now() ds.ContentDate = dt.strftime('%Y%m%d') timeStr = dt.strftime('%H%M%S.%f') # long format with micro seconds ds.ContentTime = timeStr ds.BitsAllocated = 16 ds.Rows = self.image.shape[0] ds.Columns = self.image.shape[1] ds.PixelRepresentation = 0 ds.SamplesPerPixel = 1 ds.PhotometricInterpretation = "MONOCHROME2" image = self.image image *= 255 image = image.astype("uint16") ds.PixelData = Image.fromarray(image).tobytes() print("Writing test file", filename_little_endian) ds.save_as(filename_little_endian) print("File saved.") ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRBigEndian ds.is_little_endian = False ds.is_implicit_VR = False print("Writing test file as Big Endian Explicit VR", filename_big_endian) ds.save_as(filename_big_endian) return ds
def on_c_store(self, ds: Dataset, context, info): logger.info('C-Store processing') file_meta = Dataset() file_meta.TransferSyntaxUID = ImplicitVRLittleEndian file_meta.MediaStorageSOPClassUID = ds.SOPClassUID file_meta.MediaStorageSOPInstanceUID = ds.SOPInstanceUID file_meta.ImplementationClassUID = '1.3.6.1.4.1.9590.100.1.0.100.4.0' fds = FileDataset(None, {}, file_meta=file_meta, preamble=b'\0' * 128) fds.update(ds) fds.is_little_endian = True fds.is_implicit_VR = True DicomSaver.save(fds) logger.info('C-Store succeeded') return 0x0000
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