Ejemplo n.º 1
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())
Ejemplo n.º 2
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())
Ejemplo n.º 3
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())
    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())
    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())
    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())
    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())
Ejemplo n.º 8
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())
Ejemplo n.º 9
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())
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
#########################################
# 3. Detect
#########################################

# Detect means using the deid recipe to parse headers

# If we try to clean before we detect, we can't
# client.clean()
# WARNING Use <deid.dicom.pixels.clean.DicomCleaner object at 0x7fafb70b9cf8>.detect() to find coordinates first.

client.detect(dicom_file)

# {'flagged': True,
# 'results': [{'coordinates': [],
#   'group': 'blacklist',
#   'reason': ' ImageType missing  or ImageType empty '}]}

#########################################
# 4. Clean and save
#########################################

client.clean()

# If there are coordinates, they are blanked. Otherwise, no change.
# Blanking 0 coordinate results

# Default output folder is temporary, unless specified at client onset
# or directly to saving functions
client.save_png()
client.save_dicom()