Ejemplo n.º 1
0
    def test_HeatMap_w_FeatureComputationPlan( self ):
        """Classification results using SampleImageTiles method and FOF should be the same.
        """

        # chris@NIA-LG-01778617 ~/src/wnd-charm/tests/pywndcharm_tests
        # $ tiffinfo lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif
        # TIFF Directory at offset 0x18ea9c (1632924)
        #   Image Width: 1388 Image Length: 1040
        #   Bits/Sample: 8
        #   Compression Scheme: LZW
        #   Photometric Interpretation: min-is-black
        #   Samples/Pixel: 1
        #   Rows/Strip: 5
        #   Planar Configuration: single image plane

        # 5x6 tiling scheme => tile dims 208 x 231.33 each
        scan_x = 231
        scan_y = 208

        #num_features = 200

        # Inflate the zipped test fit into a temp file
        tempdir = mkdtemp()
        
        try:
            import zipfile
            reference_sigs = pychrm_test_dir + sep + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E_REFERENCE_SIGFILES.zip'
            zf = zipfile.ZipFile( reference_sigs, mode='r' )
            zf.extractall( tempdir )

            img_filename = "lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif"
            orig_img_filepath = pychrm_test_dir + sep + img_filename

            from shutil import copy

            # copy the tiff to the tempdir so the .sig files end up there too
            copy( orig_img_filepath, tempdir )
            input_image_path = tempdir + sep + img_filename

            # create the tile image iterator
            image_iter = SampleImageTiles( input_image_path, scan_x, scan_y, True)
            print "Number of samples = " + str( image_iter.samples )

            base, ext = splitext( input_image_path )

            # Just grab the first tile:
            import pdb; pdb.set_trace()
            tile_cropped_px_plane = image_iter.sample()

            kwargs = {}
            kwargs[ 'name' ] = input_image_path
            kwargs[ 'source_filepath' ] = tile_cropped_px_plane
            #kwargs[ 'feature_names' ] = fw.feature_names
            #kwargs[ 'feature_computation_plan' ] = comp_plan
            kwargs[ 'long' ] = True
            kwargs[ 'tile_num_cols' ] = image_iter.tiles_x
            kwargs[ 'tile_num_rows' ] = image_iter.tiles_y
            kwargs[ 'tiling_scheme' ] = '{0}x{1}'.format( image_iter.tiles_x, image_iter.tiles_y )
            kwargs[ 'tile_col_index' ] = image_iter.current_col
            kwargs[ 'tile_row_index' ] = image_iter.current_row
            kwargs[ 'sample_group_id' ] = 0

            top_left_tile_feats = FeatureVector( **kwargs ).GenerateFeatures( quiet=False, write_to_disk=False )

            top_left_tile_reference_feats = FeatureVector.NewFromSigFile( tempdir + sep + 'sj-05-3362-R2_001_E-t5x6_0_0-l.sig' ) 

            # Remember we're reading these values in from strings. and the ranges are so wide
            # you only have 6 sig figs. Better apples to apples comparison is to
            # compare strings.
            self.assertTrue( compare( top_left_tile_feats.values, top_left_tile_reference_feats.values ) )

            # Setting feature_names initiates the feature reduce from
            # the larger set of features that comes back from computation
            #kwargs[ 'feature_names' ] = fw.feature_names
            # if these are set, then the code will try to take a ROI of a ROI:
            #kwargs[ 'x' ] = image_iter.current_x
            #kwargs[ 'y' ] = image_iter.current_y
            #kwargs[ 'w' ] = image_iter.tile_width
            #kwargs[ 'h' ] = image_iter.tile_height

        finally:
            rmtree( tempdir )
Ejemplo n.º 2
0
    def test_HeatMap_w_FeatureComputationPlan(self):
        """Classification results using SampleImageTiles method and FOF should be the same.
        """

        # chris@NIA-LG-01778617 ~/src/wnd-charm/tests/pywndcharm_tests
        # $ tiffinfo lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif
        # TIFF Directory at offset 0x18ea9c (1632924)
        #   Image Width: 1388 Image Length: 1040
        #   Bits/Sample: 8
        #   Compression Scheme: LZW
        #   Photometric Interpretation: min-is-black
        #   Samples/Pixel: 1
        #   Rows/Strip: 5
        #   Planar Configuration: single image plane

        # 5x6 tiling scheme => tile dims 208 x 231.33 each
        scan_x = 231
        scan_y = 208

        #num_features = 200

        # Inflate the zipped test fit into a temp file
        tempdir = mkdtemp()

        try:
            import zipfile
            reference_sigs = pychrm_test_dir + sep + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E_REFERENCE_SIGFILES.zip'
            zf = zipfile.ZipFile(reference_sigs, mode='r')
            zf.extractall(tempdir)

            img_filename = "lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif"
            orig_img_filepath = pychrm_test_dir + sep + img_filename

            from shutil import copy

            # copy the tiff to the tempdir so the .sig files end up there too
            copy(orig_img_filepath, tempdir)
            input_image_path = tempdir + sep + img_filename

            # create the tile image iterator
            image_iter = SampleImageTiles(input_image_path, scan_x, scan_y,
                                          True)
            print "Number of samples = " + str(image_iter.samples)

            base, ext = splitext(input_image_path)

            # Just grab the first tile:
            import pdb
            pdb.set_trace()
            tile_cropped_px_plane = image_iter.sample()

            kwargs = {}
            kwargs['name'] = input_image_path
            kwargs['source_filepath'] = tile_cropped_px_plane
            #kwargs[ 'feature_names' ] = fw.feature_names
            #kwargs[ 'feature_computation_plan' ] = comp_plan
            kwargs['long'] = True
            kwargs['tile_num_cols'] = image_iter.tiles_x
            kwargs['tile_num_rows'] = image_iter.tiles_y
            kwargs['tiling_scheme'] = '{0}x{1}'.format(image_iter.tiles_x,
                                                       image_iter.tiles_y)
            kwargs['tile_col_index'] = image_iter.current_col
            kwargs['tile_row_index'] = image_iter.current_row
            kwargs['sample_group_id'] = 0

            top_left_tile_feats = FeatureVector(**kwargs).GenerateFeatures(
                quiet=False, write_to_disk=False)

            top_left_tile_reference_feats = FeatureVector.NewFromSigFile(
                tempdir + sep + 'sj-05-3362-R2_001_E-t5x6_0_0-l.sig')

            # Remember we're reading these values in from strings. and the ranges are so wide
            # you only have 6 sig figs. Better apples to apples comparison is to
            # compare strings.
            self.assertTrue(
                compare(top_left_tile_feats.values,
                        top_left_tile_reference_feats.values))

            # Setting feature_names initiates the feature reduce from
            # the larger set of features that comes back from computation
            #kwargs[ 'feature_names' ] = fw.feature_names
            # if these are set, then the code will try to take a ROI of a ROI:
            #kwargs[ 'x' ] = image_iter.current_x
            #kwargs[ 'y' ] = image_iter.current_y
            #kwargs[ 'w' ] = image_iter.tile_width
            #kwargs[ 'h' ] = image_iter.tile_height

        finally:
            rmtree(tempdir)
Ejemplo n.º 3
0
    reduced_training_set = FeatureSet_Discrete.NewFromPickleFile( pickled_features )
    reduced_fisher_weights = FisherFeatureWeights.NewFromPickleFile( pickled_weights )


# create the tile image iterator
image_iter = SampleImageTiles (input_image, scan_x, scan_y, True)
print "Number of samples = "+str (image_iter.samples)

# Create a list of zero'd out, image-sized, 2-D byte numpys
masks = [] 
for i in range( reduced_training_set.num_classes ):
    masks.append ( np.zeros (shape=(image_iter.image.height,image_iter.image.width), dtype='uint8') )


# iterate over the image, classifying each tile
for sample in image_iter.sample():
    try:
        test_image_signatures = Signatures.NewFromFeatureNameList( sample, reduced_fisher_weights.names )
        test_image_signatures.Normalize( reduced_training_set )
        result = DiscreteImageClassificationResult.NewWND5( reduced_training_set, reduced_fisher_weights, test_image_signatures )
        for i in range( reduced_training_set.num_classes ):
            mask_val = int (result.marginal_probabilities[i] * 255.0)
            # Write the mask value into the numpy
            masks[i][image_iter.current_y:image_iter.current_y + image_iter.tile_height,
                image_iter.current_x:image_iter.current_x + image_iter.tile_width] = mask_val
            print "{0} ({1},{2}) {3}: {4}".format (
                i, image_iter.current_x, image_iter.current_y, reduced_training_set.classnames_list[i], mask_val)
    except:
        x_y_str = "{0}_{1}".format (image_iter.current_x, image_iter.current_y)
        tif_path = os.path.join (os.path.abspath(os.path.dirname(input_filename)), os.path.splitext(os.path.basename(image_path))[0] + "_" + x_y_str + ".tiff")
        print "Could not classify sample at ({0},{1}), saving tiff file {2}".format (
Ejemplo n.º 4
0
    reduced_fisher_weights = FisherFeatureWeights.NewFromPickleFile(
        pickled_weights)

# create the tile image iterator
image_iter = SampleImageTiles(input_image, scan_x, scan_y, True)
print "Number of samples = " + str(image_iter.samples)

# Create a list of zero'd out, image-sized, 2-D byte numpys
masks = []
for i in range(reduced_training_set.num_classes):
    masks.append(
        np.zeros(shape=(image_iter.image.height, image_iter.image.width),
                 dtype='uint8'))

# iterate over the image, classifying each tile
for sample in image_iter.sample():
    try:
        test_image_signatures = Signatures.NewFromFeatureNameList(
            sample, reduced_fisher_weights.names)
        test_image_signatures.Normalize(reduced_training_set)
        result = DiscreteImageClassificationResult.NewWND5(
            reduced_training_set, reduced_fisher_weights,
            test_image_signatures)
        for i in range(reduced_training_set.num_classes):
            mask_val = int(result.marginal_probabilities[i] * 255.0)
            # Write the mask value into the numpy
            masks[i][image_iter.current_y:image_iter.current_y +
                     image_iter.tile_height,
                     image_iter.current_x:image_iter.current_x +
                     image_iter.tile_width] = mask_val
            print "{0} ({1},{2}) {3}: {4}".format(