def study(request, study_iuid): # Patient Name response = {} study_iuid_tag = gdcm.Tag(0x20, 0xD) study_iuid_element = gdcm.DataElement(study_iuid_tag) study_descr_tag = gdcm.Tag(0x8, 0x1030) study_descr_element = gdcm.DataElement(study_descr_tag) study_iuid_element.SetByteValue(str(study_iuid), gdcm.VL(len(study_iuid))) ds = gdcm.DataSet() ds.Insert(study_iuid_element) ds.Insert(study_descr_element) cnf = gdcm.CompositeNetworkFunctions() theQuery = cnf.ConstructQuery(gdcm.eStudyRootType, gdcm.eStudy, ds) # prepare the variable for output ret = gdcm.DataSetArrayType() # Execute the C-FIND query cnf.CFind(settings.SC_DICOM_SERVER, settings.SC_DICOM_PORT, theQuery, ret, 'GDCM_PYTHON', settings.AET) response["description"] = str( ret[0].GetDataElement(study_descr_tag).GetValue()) ds = gdcm.DataSet() series_descr_tag = gdcm.Tag(0x8, 0x103E) series_descr_element = gdcm.DataElement(series_descr_tag) series_iuid_tag = gdcm.Tag(0x20, 0xE) series_iuid_element = gdcm.DataElement(series_iuid_tag) series_number_tag = gdcm.Tag(0x20, 0x11) series_number_element = gdcm.DataElement(series_number_tag) ds.Insert(study_iuid_element) ds.Insert(series_descr_element) ds.Insert(series_iuid_element) ds.Insert(series_number_element) series_query = cnf.ConstructQuery(gdcm.eStudyRootType, gdcm.eSeries, ds) ret = gdcm.DataSetArrayType() cnf.CFind(settings.SC_DICOM_SERVER, settings.SC_DICOM_PORT, series_query, ret, 'GDCM_PYTHON', settings.AET) sorted_ret = sorted( ret, key=lambda x: int(str(x.GetDataElement(series_number_tag).GetValue()))) response["series"] = [{ "description": str(x.GetDataElement(series_descr_tag).GetValue()), "uid": str(x.GetDataElement(series_iuid_tag).GetValue()) } for x in sorted_ret] json_response = json.dumps(response) if request.GET.has_key('callback'): json_response = "(function(){%s(%s);})();" % (request.GET['callback'], json_response) return HttpResponse(json_response, content_type="application/json")
def downloadSeries(self, studyUID, seriesUID, outputdir, patientID=None): hashdir = hashlib.sha224(studyUID + "/" + seriesUID).hexdigest() targetdir = outputdir + "/" + hashdir + "/" if not os.path.exists(outputdir): os.makedirs(outputdir) if not os.path.exists(targetdir): os.makedirs(targetdir) self.de_infmodel = gdcm.DataElement(self.tag_infmodel) self.de_infmodel.SetByteValue('SERIES', gdcm.VL(len('SERIES'))) self.de_StudyUID = gdcm.DataElement(self.tag_StudyUID) self.de_StudyUID.SetByteValue(studyUID, gdcm.VL(len(studyUID))) self.de_SeriesUID = gdcm.DataElement(self.tag_SeriesUID) self.de_SeriesUID.SetByteValue(seriesUID, gdcm.VL(len(seriesUID))) self.dataset_seriesQuery = gdcm.DataSet() self.dataset_seriesQuery.Insert(self.de_StudyUID) self.dataset_seriesQuery.Insert(self.de_infmodel) self.dataset_seriesQuery.Insert(self.de_SeriesUID) self.cnf_series = gdcm.CompositeNetworkFunctions() self.theSeriesQuery = self.cnf_series.ConstructQuery( gdcm.eStudyRootType, gdcm.eSeries, self.dataset_seriesQuery) # Execute the C-FIND query self.cnf_series.CMove(self.server, self.remoteport, self.theSeriesQuery, self.localport, self.aetitle, self.caller, targetdir) return targetdir
def RunCMove(self, values): ds = gdcm.DataSet() #for v in values: tg_patient = gdcm.Tag(0x0010, 0x0020) tg_serie = gdcm.Tag(0x0020, 0x000e) de_patient = gdcm.DataElement(tg_patient) de_serie = gdcm.DataElement(tg_serie) patient_id = str(values[0]) serie_id = str(values[1]) print "(0x0010, 0x0020)",patient_id print "(0x0020, 0x000e)",serie_id print "\n\n" de_patient.SetByteValue(patient_id, gdcm.VL(len(patient_id))) de_serie.SetByteValue(serie_id, gdcm.VL(len(serie_id))) ds.Insert(de_patient) ds.Insert(de_serie) cnf = gdcm.CompositeNetworkFunctions() theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImageOrFrame, ds) #ret = gdcm.DataSetArrayType() """ CMove (const char *remote, uint16_t portno, const BaseRootQuery *query, uint16_t portscp, const char *aetitle=NULL, const char *call=NULL, const char *outputdir=NULL)""" print ">>>>>", self.address, int(self.port), theQuery, 11112, self.aetitle,\ self.aetitle_call, "/home/phamorim/Desktop/output/" cnf.CMove(self.address, int(self.port), theQuery, 11112, self.aetitle,\ self.aetitle_call, "/home/phamorim/Desktop/") print "BAIXOUUUUUUUU"
def construct_dataset(self, dic_keys, dataset_rsp=None): #fill dataset dataset = gdcm.DataSet() for tags in dic_keys: group = tags[0] element = tags[1] de = gdcm.DataElement(gdcm.Tag(group, element)) if dataset_rsp and group == 0x0020 and element == 0x000D: de = dataset_rsp.GetDataElement(gdcm.Tag(group, element)) value = str(de.GetByteValue()) else: value = dic_keys[tags] if value: value_length = len(value) de.SetByteValue(value, gdcm.VL(value_length)) #de.SetVR(gdcm.VR(gdcm.VR.PN)) dataset.Insert(de) return dataset
def series(request, series_iuid): series_iuid_tag = gdcm.Tag(0x20, 0xE) series_iuid_element = gdcm.DataElement(series_iuid_tag) series_iuid_element.SetByteValue(str(series_iuid), gdcm.VL(len(series_iuid))) instance_uid_tag = gdcm.Tag(0x8, 0x18) instance_uid_element = gdcm.DataElement(instance_uid_tag) instance_number_tag = gdcm.Tag(0x20, 0x13) instance_number_element = gdcm.DataElement(instance_number_tag) ds = gdcm.DataSet() ds.Insert(series_iuid_element) ds.Insert(instance_uid_element) ds.Insert(instance_number_element) cnf = gdcm.CompositeNetworkFunctions() instance_query = cnf.ConstructQuery(gdcm.eStudyRootType, gdcm.eImage, ds) ret = gdcm.DataSetArrayType() cnf.CFind(settings.SC_DICOM_SERVER, settings.SC_DICOM_PORT, instance_query, ret, 'GDCM_PYTHON', settings.AET) sorted_ret = sorted( ret, key=lambda x: int(str( x.GetDataElement(instance_number_tag).GetValue()))) response = [ str(x.GetDataElement(instance_uid_tag).GetValue()) for x in sorted_ret ] json_response = json.dumps(response) if request.GET.has_key('callback'): json_response = "(function(){%s(%s);})();" % (request.GET['callback'], json_response) return HttpResponse(json_response, content_type="application/json")
def RunCFind(self): tags = [(0x0010, 0x0010), (0x0010, 0x1010), (0x0010,0x0040), (0x0008,0x1030),\ (0x0008,0x0060), (0x0008,0x0022), (0x0008,0x0080), (0x0010,0x0030),\ (0x0008,0x0050), (0x0008,0x0090), (0x0008,0x103E), (0x0008,0x0033),\ (0x0008,0x0032), (0x0020,0x000d)] ds = gdcm.DataSet() for tag in tags: tg = gdcm.Tag(tag[0], tag[1]) de = gdcm.DataElement(tg) if self.search_type == 'patient' and tag == (0x0010, 0x0010): bit_size = len(self.search_word) + 1 de.SetByteValue(str(self.search_word + '*'), gdcm.VL(bit_size)) else: de.SetByteValue('*', gdcm.VL(1)) ds.Insert(de) cnf = gdcm.CompositeNetworkFunctions() theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImageOrFrame, ds) ret = gdcm.DataSetArrayType() cnf.CFind(self.address, int(self.port), theQuery, ret, self.aetitle,\ self.aetitle_call) patients = {} exist_images = False c = 0 for i in range(0, ret.size()): patient_id = str(ret[i].GetDataElement(gdcm.Tag( 0x0010, 0x0020)).GetValue()) serie_id = str(ret[i].GetDataElement(gdcm.Tag(0x0020, 0x000e)).GetValue()) if not (patient_id in patients.keys()): patients[patient_id] = {} if not (serie_id in patients[patient_id]): rt = ret[i] name = self.GetValueFromDICOM(rt, (0x0010, 0x0010)) age = self.GetValueFromDICOM(rt, (0x0010, 0x1010)) gender = self.GetValueFromDICOM(rt, (0x0010, 0x0040)) study_description = self.GetValueFromDICOM( rt, (0x0008, 0x1030)) modality = self.GetValueFromDICOM(rt, (0x0008, 0x0060)) institution = self.GetValueFromDICOM(rt, (0x0008, 0x0080)) date_of_birth = utils.format_date( self.GetValueFromDICOM(rt, (0x0010, 0x0030))) acession_number = self.GetValueFromDICOM(rt, (0x0008, 0x0050)) ref_physician = self.GetValueFromDICOM(rt, (0x0008, 0x0090)) serie_description = self.GetValueFromDICOM( rt, (0x0008, 0x103E)) acquisition_time = utils.format_time( self.GetValueFromDICOM(rt, (0x0008, 0x0032))) acquisition_date = utils.format_date( self.GetValueFromDICOM(rt, (0x0008, 0x0022))) teste = self.GetValueFromDICOM(rt, (0x0020, 0x000d)) patients[patient_id][serie_id] = {'name':name, 'age':age, 'gender':gender,\ 'study_description':study_description,\ 'modality':modality, \ 'acquisition_time':acquisition_time,\ 'acquisition_date':acquisition_date,\ 'institution':institution,\ 'date_of_birth':date_of_birth,\ 'acession_number':acession_number,\ 'ref_physician':ref_physician,\ 'serie_description':serie_description} patients[patient_id][serie_id]['n_images'] = 1 else: patients[patient_id][serie_id]['n_images'] += 1 return patients
Usage: python FindAllPatientName.py """ import gdcm # Patient Name tag = gdcm.Tag(0x10, 0x10) de = gdcm.DataElement(tag) # Search all patient name where string match 'F*' de.SetByteValue('F*', gdcm.VL(2)) ds = gdcm.DataSet() ds.Insert(de) cnf = gdcm.CompositeNetworkFunctions() theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.ePatient, ds) #print theQuery.ValidateQuery() # prepare the variable for output ret = gdcm.DataSetArrayType() # Execute the C-FIND query cnf.CFind('dicom.example.com', 11112, theQuery, ret, 'GDCM_PYTHON', 'ANY-SCP') for i in range(0, ret.size()): print "Patient #", i
def get_query_and_level(self, dic_keys): """Esta funcion retorna un dataset con los maching key y return key y el nivel de consulta QR""" # Estos diccionarios pueden estar en ficheros level_patient = { "PatientName": (0x0010, 0x0010), "PatientID": (0x0010, 0x0020), "PatientSex": (0x0010, 0x0040), "PatientAge": (0x0010, 0x1010), "PatientBirthDate": (0010, 0030) } level_study = { "StudyDescription": (0x0008, 0x1030), "StudyDate": (0x0008, 0x0020), "StudyTime": (0x0008, 0x0030), "StudyInstanceUID": (0x0020, 0x000D), "NumberStudyRelatedInstances": (0x0020, 0x1208) } level_series = { "SeriesInstanceUID": (0x0020, 0x000E), "Modality": (0x0008, 0x0060), "SeriesDate": (0x0008, 0x0021), "SeriesTime": (0x0008, 0x0031), "NumberSeriesRelatedInstances": (0x0020, 0x1209) } level_image = { "SopInstanceUID": (0x0008, 0x0018), "InstanceNumber": (0x0020, 0x0013), "SopClassUID": (0x0008, 0x0016) } alias_tags = {} alias_tags.update(level_patient) alias_tags.update(level_study) alias_tags.update(level_series) alias_tags.update(level_image) query_level = -1 for attr_key in dic_keys: value = dic_keys[attr_key] if attr_key in level_image: if value: query_level = gdcm.eImageOrFrame break elif attr_key in level_patient: if value and query_level < gdcm.ePatient: query_level = gdcm.ePatient elif attr_key in level_study: if value and query_level < gdcm.eStudy: query_level = gdcm.eStudy elif attr_key in level_series: if value and query_level < gdcm.eSeries: query_level = gdcm.eSeries else: raise Exception("Invaliud Tag") keys_dataset = gdcm.DataSet() #fill dataset for attr_key in dic_keys: group = alias_tags[attr_key][0] element = alias_tags[attr_key][1] de = gdcm.DataElement(gdcm.Tag(group, element)) value = dic_keys[attr_key] if value: value_length = len(value) de.SetByteValue(value, gdcm.VL(value_length)) #de.SetVR(gdcm.VR(gdcm.VR.PN)) keys_dataset.Insert(de) return (query_level, keys_dataset)
def __init__(self): self.dataset = gdcm.DataSet() pass