コード例 #1
0
    def test_pixel_cleaner_keepcoordinates_from(self):
        """Test the pixel cleaner to ensure that multiple keep coordinates retrieved from a dicom field are appropriately retained."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "keepcoordinates_from.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[1000:2000, 0:1000] = 0
        inputpixels[0:1000, 1000:2000] = 0

        compare = inputpixels[0:2000, 0:2000] == outputpixels[0:2000, 0:2000]
        self.assertTrue(compare.all())
コード例 #2
0
    def test_pixel_cleaner_remove_all(self):
        """Test the pixel cleaner to ensure it appropriately clears all pixels."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "remove_all.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[:, :] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #3
0
    def test_3d_Greyscale_cine_clip(self):
        """
        Test the pixel cleaner to ensure pixels are appropriately deidentified
        on "3D" images - greyscale cine clips.  Pixel data will have the shape (frames, X, Y)
        """
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset, "GREYSCALE_CINE.zip", self.tmpdir)
        deid = os.path.join(self.deidpath, "remove_coordinates_us.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[:, 0:500, 0:500] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #4
0
    def test_pixel_cleaner_remove_multiple_filters(self):
        """Test the pixel cleaner to ensure that multiple remove coordinates in different filters remove the appropriate pixels."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath,
                            "remove_coordinates_multiple_filters.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[0:10, 0:10] = 0
        inputpixels[10:20, 10:20] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #5
0
    def test_2d_Greyscale_image_all(self):
        """
        Test the pixel cleaner to ensure pixels are appropriately deidentified - all keyword -
        on "2D" images - Greyscale images.  Pixel data will have the shape (X, Y)
        """
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset, "GREYSCALE_IMAGE.dcm")
        deid = os.path.join(self.deidpath, "remove_coordinates_us_all.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[:, :] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #6
0
    def test_4d_RGB_cine_clip_all(self):
        """
        Test the pixel cleaner to ensure pixels are appropriately deidentified - all keyword -
        on "4D" images - RGB cine clips.  Pixel data will have the shape (frames, X, Y, channel)
        """
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset, "RGB_CINE.zip", self.tmpdir)
        deid = os.path.join(self.deidpath, "remove_coordinates_us_all.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[:, :, :, :] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #7
0
    def test_3d_RGB_image(self):
        """
        Test the pixel cleaner to ensure pixels are appropriately deidentified
        on "3D" images - RGB images.  Pixel data will have the shape (X, Y, channel)
        """
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset, "RGB_IMAGE.dcm")
        deid = os.path.join(self.deidpath, "remove_coordinates_us.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        inputpixels[0:500, 0:500, :] = 0
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #8
0
    def test_filter_multiple_rule_innerop_false(self):
        """Test the DicomCleaner.detect to ensure multiple rules within a filter evaluated to false detects appropriately."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "filter_multiple_rule_innerop_false.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertFalse(out["flagged"])
コード例 #9
0
    def test_filter_single_rule_innerop_true(self):
        """Test the DicomCleaner.detect to ensure a single rule with an inner operator evaluated to true detects appropriately."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "filter_single_rule_innerop_true.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])
コード例 #10
0
    def test_filter_multiple_second_filter_match(self):
        """Test the DicomCleaner.detect to ensure multiple detected filters are appropriately detected."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "filter_multiple_second_filter_match.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        matchgroups = set()

        for r in out["results"]:
            matchgroups.add(r["group"])

        self.assertIn("ShouldMatch", matchgroups)
        self.assertNotIn("ShouldNotMatch", matchgroups)
コード例 #11
0
    def test_pixel_cleaner_keepcoordinates_noaction(self):
        """Test the pixel cleaner to ensure that a keepcoordinates with no removecoordinates has no impact on the pixels."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "keepcoordinates_noaction.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertTrue(compare.all())
コード例 #12
0
    def test_pixel_cleaner_keepcoordinates(self):
        """Test the pixel cleaner to ensure that a keepcoordinates retains appropriate pixels."""
        from deid.dicom import DicomCleaner

        dicom_file = get_file(self.dataset)
        deid = os.path.join(self.deidpath, "keepcoordinates.dicom")

        client = DicomCleaner(output_folder=self.tmpdir, deid=deid)
        out = client.detect(dicom_file)
        self.assertTrue(out["flagged"])

        client.clean()
        cleanedfile = client.save_dicom()

        outputfile = read_file(cleanedfile)
        outputpixels = outputfile.pixel_array

        inputfile = read_file(dicom_file)
        inputpixels = inputfile.pixel_array
        compare = inputpixels == outputpixels
        self.assertFalse(compare.all())

        compare = inputpixels[0:1024, 0:1024] == outputpixels[0:1024, 0:1024]
        self.assertTrue(compare.all())
コード例 #13
0
ファイル: main.py プロジェクト: qdubious/dicom-cleaner
def main():

    parser = get_parser()

    try:
        args = parser.parse_args()
    except:
        sys.exit(0)

    from deid.dicom import (get_files, DicomCleaner)
    from logger import bot

    if args.folder is None:
        bot.error("Please provide a folder with dicom files with --input.")
        sys.exit(1)

    # This is an iterator, so must convert to list to get length
    dicom_files = get_files(args.folder)

    # Create a Dicom Cleaner client
    number_files = len(list(get_files(args.folder)))

    client = DicomCleaner(output_folder=args.outfolder, deid=args.deid)
    bot.info('Processing [images]%s [output-folder]%s' %
             (number_files, client.output_folder))
    outcomes = {True: 'flagged', False: '  clean'}

    # Keep a list of flagged and clean
    flagged = []
    clean = []
    summary = dict()

    # We will move images into respective folders
    if args.save is "pdf":
        pdf_report = '%s/deid-clean-%s.pdf' % (args.outfolder, number_files)
        pp = PdfPages(pdf_report)

    # Perform detection one at a time
    for dicom_file in dicom_files:

        dicom_name = os.path.basename(dicom_file)

        # detect --> clean
        result = client.detect(dicom_file)
        client.clean()
        summary[dicom_name] = result

        # Generate title/description for result
        title = '%s: %s' % (outcomes[result['flagged']], dicom_name)
        bot.info(title)

        # How does the user want to save data?
        if args.save == "dicom":
            outfile = client.save_dicom()

        elif args.save == "png":
            outfile = client.save_png(title=title)

        # pdf (default)
        else:
            plt = client.get_figure(title=title)
            fig = plt.gcf()
            pp.savefig(fig)
            plt.close(fig)

        # Whether dicom or png, append to respective list
        if args.save is not "pdf":
            if result['flagged']:
                flagged.append(outfile)
            else:
                clean.append(outfile)

    # Save summary json file
    summary_json = '%s/deid-clean-%s.json' % (args.outfolder, number_files)
    write_json(summary, summary_json)
    bot.info('json data written to %s' % summary_json)

    # When we get here, if saving pdf, close it.
    if args.save == "pdf":
        bot.info('pdf report written to %s' % pdf_report)
        pp.close()

    # Otherwise, move files into respective folders
    else:
        move_files(files=flagged, dest='%s/flagged' % args.outfolder)
        move_files(files=cleaned, dest='%s/clean' % args.outfolder)
コード例 #14
0
#########################################

# This will get a set of example cookie dicoms
from deid.dicom import get_files
from deid.data import get_dataset

base = get_dataset("dicom-cookies")
dicom_files = list(
    get_files(base))  # todo : consider using generator functionality
dicom_file = dicom_files[3]

#########################################
# 2. Create Client
#########################################

client = DicomCleaner()

# You can set the output folder if you want, otherwis tmpdir is used
client = DicomCleaner(output_folder="/home/vanessa/Desktop")

# Steps are to detect, clean, and save in desired format, one image
# at a time.
# client.detect(dicom_file)
# client.clean()
# client.save_<format>

#########################################
# 3. Detect
#########################################

# Detect means using the deid recipe to parse headers