示例#1
0
def test_create_rtss(test_object):
    """
    Test for creating an RT Struct file if one does not exist.
    :param test_object: test_object function, for accessing the shared
                        TestIso2Roi object.
    """
    # Create file path
    file_path = test_object.patient_dict_container.filepaths.values()
    file_path = Path(os.path.commonpath(file_path))
    file_path = str(file_path.joinpath("rtss.dcm"))

    # Get CT UID list
    ct_uid_list = ImageLoading.get_image_uid_list(
        test_object.patient_dict_container.dataset)

    # Generate RTSS
    rtss = ROI.create_initial_rtss_from_ct(
        test_object.patient_dict_container.dataset[0], file_path, ct_uid_list)

    # Get test dataset
    test_ds = test_object.patient_dict_container.dataset[0]

    # Assert that the ds exists and is not empty
    assert rtss

    # Assert that certain values are correct
    assert rtss.PatientName == test_ds.PatientName
    assert rtss.PatientID == test_ds.PatientID
    assert rtss.PatientBirthDate == test_ds.PatientBirthDate
    assert rtss.PatientSex == test_ds.PatientSex
    assert rtss.StudyInstanceUID == test_ds.StudyInstanceUID
    assert rtss.Modality == 'RTSTRUCT'
    assert rtss.SOPClassUID == '1.2.840.10008.5.1.4.1.1.481.3'
示例#2
0
def test_create_initial_rtss_from_ct(qtbot, test_object, init_config):
    # Create a test rtss
    path = test_object.patient_dict_container.path
    rtss_path = Path(path).joinpath('rtss.dcm')
    uid_list = ImageLoading.get_image_uid_list(
        test_object.patient_dict_container.dataset)
    rtss = create_initial_rtss_from_ct(
        test_object.patient_dict_container.dataset[1], rtss_path, uid_list)

    # type 1 tags - must exist and not be empty
    type_1_tags: list = [Tag("StudyInstanceUID"),
                         Tag("Modality"),
                         Tag("SeriesInstanceUID"),
                         Tag("StructureSetLabel"),
                         Tag("SOPClassUID"),
                         Tag("SOPInstanceUID")
                         ]
    # type 2 tags - must exist and be at least an empty string
    type_2_tags: list = [Tag("PatientName"),
                         Tag("PatientBirthDate"),
                         Tag("PatientSex"),
                         Tag("StudyDate"),
                         Tag("StudyTime"),
                         Tag("AccessionNumber"),
                         Tag("ReferringPhysicianName"),
                         Tag("StudyID"),
                         Tag("OperatorsName"),
                         Tag("SeriesNumber"),
                         Tag("Manufacturer"),
                         Tag("StructureSetDate"),
                         Tag("StructureSetTime")
                         ]

    # type 1 sequence tags - must exist
    type_1_sequence_tags: list = [Tag("StructureSetROISequence"),
                                  Tag("ROIContourSequence"),
                                  Tag("RTROIObservationsSequence")
                                  ]

    # Checking type 1 tags
    for tag in type_1_tags:
        assert (tag in rtss) is True
        assert rtss[tag].is_empty is False

    # Checking type 2 tags
    for tag in type_2_tags:
        assert (tag in rtss) is True
        if rtss[tag].value != "":
            assert rtss[tag].is_empty is False

    # Checking type 1 sequence tags
    for tag in type_1_sequence_tags:
        assert (tag in rtss) is True
示例#3
0
    def create_new_rtstruct(cls, progress_callback):
        """
        Generates a new RTSS and edits the patient dict container. Used
        for batch processing.
        """
        # Get common directory
        patient_dict_container = PatientDictContainer()
        file_path = patient_dict_container.filepaths.values()
        file_path = Path(os.path.commonpath(file_path))

        # Get new RT Struct file path
        file_path = str(file_path.joinpath("rtss.dcm"))

        # Create RT Struct file
        progress_callback.emit(("Generating RT Structure Set", 60))
        ct_uid_list = ImageLoading.get_image_uid_list(
            patient_dict_container.dataset)
        ds = ROI.create_initial_rtss_from_ct(patient_dict_container.dataset[0],
                                             file_path, ct_uid_list)
        ds.save_as(file_path)

        # Add RT Struct file path to patient dict container
        patient_dict_container.filepaths['rtss'] = file_path
        filepaths = patient_dict_container.filepaths

        # Add RT Struct dataset to patient dict container
        patient_dict_container.dataset['rtss'] = ds
        dataset = patient_dict_container.dataset

        # Set some patient dict container attributes
        patient_dict_container.set("file_rtss", filepaths['rtss'])
        patient_dict_container.set("dataset_rtss", dataset['rtss'])

        dicom_tree_rtss = DicomTree(filepaths['rtss'])
        patient_dict_container.set("dict_dicom_tree_rtss",
                                   dicom_tree_rtss.dict)

        dict_pixluts = ImageLoading.get_pixluts(patient_dict_container.dataset)
        patient_dict_container.set("pixluts", dict_pixluts)

        rois = ImageLoading.get_roi_info(ds)
        patient_dict_container.set("rois", rois)

        patient_dict_container.set("selected_rois", [])
        patient_dict_container.set("dict_polygons_axial", {})

        patient_dict_container.set("rtss_modified", True)
示例#4
0
    def load_temp_rtss(self, path, progress_callback, interrupt_flag):
        """
        Generate a temporary rtss and load its data into
        MovingDictContainer
        :param path: str. The common root folder of all DICOM files.
        :param progress_callback: A signal that receives the current
        progress of the loading.
        :param interrupt_flag: A threading.Event() object that tells the
        function to stop loading.
        """
        progress_callback.emit(("Generating temporary rtss...", 20))
        moving_dict_container = MovingDictContainer()
        rtss_path = Path(path).joinpath('rtss.dcm')
        uid_list = ImageLoading.get_image_uid_list(
            moving_dict_container.dataset)
        rtss = create_initial_rtss_from_ct(moving_dict_container.dataset[0],
                                           rtss_path, uid_list)

        if interrupt_flag.is_set():  # Stop loading.
            print("stopped")
            return False

        progress_callback.emit(("Loading temporary rtss...", 50))
        # Set ROIs
        rois = ImageLoading.get_roi_info(rtss)
        moving_dict_container.set("rois", rois)

        # Set pixluts
        dict_pixluts = ImageLoading.get_pixluts(moving_dict_container.dataset)
        moving_dict_container.set("pixluts", dict_pixluts)

        # Add RT Struct file path and dataset to moving dict container
        moving_dict_container.filepaths['rtss'] = rtss_path
        moving_dict_container.dataset['rtss'] = rtss

        # Set some moving dict container attributes
        moving_dict_container.set("file_rtss", rtss_path)
        moving_dict_container.set("dataset_rtss", rtss)
        ordered_dict = DicomTree(None).dataset_to_dict(rtss)
        moving_dict_container.set("dict_dicom_tree_rtss", ordered_dict)
        moving_dict_container.set("selected_rois", [])
def test_merge_rtss(qtbot, test_object):
    """Test merging rtss. This function creates a new rtss, then merges
    the new rtss with the old rtss and asserts that duplicated ROIs
    will be overwritten when the other being merged.

    :param test_object: test_object function, for accessing the shared
    TestStructureTab object.
    """
    patient_dict_container = PatientDictContainer()

    # Create a new rtss
    dataset = patient_dict_container.dataset[0]
    rtss_path = Path(patient_dict_container.path).joinpath('rtss.dcm')
    new_rtss = create_initial_rtss_from_ct(
        dataset, rtss_path,
        ImageLoading.get_image_uid_list(patient_dict_container.dataset))

    # Set ROIs
    rois = ImageLoading.get_roi_info(new_rtss)
    patient_dict_container.set("rois", rois)

    # Add a new ROI into the new rtss with the name of the first ROI in
    # the old rtss
    roi_name = test_object.rois.get(1)["name"]
    roi_coordinates = [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0]
    new_rtss = create_roi(new_rtss, roi_name, [{
        'coords': roi_coordinates,
        'ds': dataset
    }])

    # Add a new ROI with a new name
    roi_name = "NewTestROI"
    new_rtss = create_roi(new_rtss, roi_name, [{
        'coords': roi_coordinates,
        'ds': dataset
    }])

    # Set ROIs
    rois = ImageLoading.get_roi_info(new_rtss)
    patient_dict_container.set("rois", rois)
    patient_dict_container.set("existing_file_rtss",
                               patient_dict_container.get("file_rtss"))
    patient_dict_container.set("dataset_rtss", new_rtss)

    # Merge the old and new rtss
    structure_tab = StructureTab()
    structure_tab.show_modified_indicator()
    qtbot.addWidget(structure_tab)

    def test_message_window():
        messagebox = structure_tab.findChild(QtWidgets.QMessageBox)
        assert messagebox is not None
        yes_button = messagebox.buttons()[1]
        qtbot.mouseClick(yes_button, QtCore.Qt.LeftButton, delay=1)

    QtCore.QTimer.singleShot(1000, test_message_window)

    structure_tab.save_new_rtss_to_fixed_image_set(auto=True)

    merged_rtss = pydicom.read_file(patient_dict_container.get("file_rtss"))
    merged_rois = ImageLoading.get_roi_info(merged_rtss)
    assert (len(test_object.rois) + 1 == len(merged_rois))