Esempio n. 1
0
 def set_host_items(self):
     if self.selected_db_type == 'sqlite':
         db_files = [basename(f) for f in get_file_paths(DATA_DIR, extension='.db') if is_file_sqlite_db(f)]
         db_files.sort()
         self.input['host'].SetItems(db_files)
     else:
         self.input['host'].SetItems(self.ip_history)
Esempio n. 2
0
    def get_queue(self):
        file_paths = get_file_paths(self.start_path, search_subfolders=self.search_subfolders)
        file_count = len(file_paths)
        queue = Queue()

        for file_index, file_path in enumerate(file_paths):
            msg = {'label': "File Name: %s" % os.path.basename(file_path),
                   'gauge': file_index / file_count}
            queue.put((file_path, msg))

        return queue
Esempio n. 3
0
    def run(self):
        """
        Begin the thread to parse directory. Returns plan_file_sets and dicom_file_paths through pubsub
        """

        file_paths = get_file_paths(self.start_path,
                                    search_subfolders=self.search_subfolders)
        file_count = len(file_paths)

        other_dicom_files = {}

        for file_index, file_path in enumerate(file_paths):
            msg = {
                'label': "File Name: %s" % os.path.basename(file_path),
                'gauge': file_index / file_count
            }
            wx.CallAfter(pub.sendMessage,
                         "dicom_directory_parser_update",
                         msg=msg)

            try:
                ds = dicom.read_file(file_path,
                                     stop_before_pixels=True,
                                     force=True)
            except InvalidDicomError:
                ds = None

            if ds is not None:

                if not self.is_data_set_valid(ds):
                    print('Cannot parse %s\nOne of these tags is missing: %s' %
                          (file_path, ', '.join(self.req_tags)))
                else:
                    modality = ds.Modality.lower()
                    timestamp = os.path.getmtime(file_path)

                    self.dicom_tag_values[file_path] = {
                        'timestamp': timestamp,
                        'study_instance_uid': ds.StudyInstanceUID,
                        'sop_instance_uid': ds.SOPInstanceUID,
                        'patient_name': ds.PatientName,
                        'mrn': ds.PatientID
                    }
                    if modality not in self.file_types:
                        if ds.StudyInstanceUID not in other_dicom_files.keys():
                            other_dicom_files[ds.StudyInstanceUID] = []
                        other_dicom_files[ds.StudyInstanceUID].append(
                            file_path)  # Store these to move after import
                    else:
                        self.dicom_files[modality].append(file_path)

                        # All RT Plan files need to be found first
                        if modality == 'rtplan':
                            uid = ds.ReferencedStructureSetSequence[
                                0].ReferencedSOPInstanceUID
                            mrn = self.dicom_tag_values[file_path]['mrn']
                            self.uid_to_mrn[uid] = ds.PatientID
                            self.dicom_tag_values[file_path][
                                'ref_sop_instance'] = {
                                    'type': 'struct',
                                    'uid': uid
                                }
                            study_uid = ds.StudyInstanceUID
                            plan_uid = ds.SOPInstanceUID
                            if mrn not in list(self.plan_file_sets):
                                self.plan_file_sets[mrn] = {}

                            if study_uid not in list(self.plan_file_sets[mrn]):
                                self.plan_file_sets[mrn][study_uid] = {}

                            self.plan_file_sets[mrn][study_uid][plan_uid] = {
                                'rtplan': {
                                    'file_path': file_path,
                                    'sop_instance_uid': plan_uid
                                },
                                'rtstruct': {
                                    'file_path': None,
                                    'sop_instance_uid': None
                                },
                                'rtdose': {
                                    'file_path': None,
                                    'sop_instance_uid': None
                                }
                            }
                            if plan_uid not in self.dicom_file_paths.keys():
                                self.dicom_file_paths[plan_uid] = {
                                    key: []
                                    for key in self.file_types + ['other']
                                }
                            self.dicom_file_paths[plan_uid]['rtplan'] = [
                                file_path
                            ]

                        elif modality == 'rtdose':
                            uid = ds.ReferencedRTPlanSequence[
                                0].ReferencedSOPInstanceUID
                            self.dicom_tag_values[file_path][
                                'ref_sop_instance'] = {
                                    'type': 'plan',
                                    'uid': uid
                                }
                        else:
                            self.dicom_tag_values[file_path][
                                'ref_sop_instance'] = {
                                    'type': None,
                                    'uid': None
                                }

        # associate appropriate rtdose files to plans
        for file_index, dose_file in enumerate(self.dicom_files['rtdose']):
            dose_tag_values = self.dicom_tag_values[dose_file]
            ref_plan_uid = dose_tag_values['ref_sop_instance']['uid']
            study_uid = dose_tag_values['study_instance_uid']
            mrn = dose_tag_values['mrn']
            for plan_file_set in self.plan_file_sets[mrn][study_uid].values():
                plan_uid = plan_file_set['rtplan']['sop_instance_uid']
                if plan_uid == ref_plan_uid:
                    plan_file_set['rtdose'] = {
                        'file_path': dose_file,
                        'sop_instance_uid': dose_tag_values['sop_instance_uid']
                    }
                    if 'rtdose' in self.dicom_file_paths[plan_uid].keys():
                        self.dicom_file_paths[plan_uid]['rtdose'].append(
                            dose_file)
                    else:
                        self.dicom_file_paths[plan_uid]['rtdose'] = [dose_file]

        # associate appropriate rtstruct files to plans
        for mrn_index, mrn in enumerate(list(self.plan_file_sets)):
            for study_uid in list(self.plan_file_sets[mrn]):
                for plan_uid, plan_file_set in self.plan_file_sets[mrn][
                        study_uid].items():
                    plan_file = plan_file_set['rtplan']['file_path']
                    ref_struct_uid = self.dicom_tag_values[plan_file][
                        'ref_sop_instance']['uid']
                    for struct_file in self.dicom_files['rtstruct']:
                        struct_uid = self.dicom_tag_values[struct_file][
                            'sop_instance_uid']
                        if struct_uid == ref_struct_uid:
                            plan_file_set['rtstruct'] = {
                                'file_path': struct_file,
                                'sop_instance_uid': struct_uid
                            }
                            if 'rtstruct' in self.dicom_file_paths[
                                    plan_uid].keys():
                                self.dicom_file_paths[plan_uid][
                                    'rtstruct'].append(struct_file)
                            else:
                                self.dicom_file_paths[plan_uid]['rtstruct'] = [
                                    struct_file
                                ]

        wx.CallAfter(pub.sendMessage,
                     'dicom_directory_parser_set_file_tree',
                     tree=self.plan_file_sets,
                     file_paths=self.dicom_file_paths,
                     other_dicom_files=other_dicom_files)