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.º 5
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.º 6
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.º 7
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.º 8
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