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 )
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)
reduced_fisher_weights = full_fisher_weights.Threshold( num_features ) # 6. Reduce the training set feature space to contain only those top 200 features reduced_training_set = full_training_set.FeatureReduce( reduced_fisher_weights.names ) # 7. Save your work: reduced_training_set.PickleMe( os.path.splitext(input_filename)[0] + ".fit.pickled" ) reduced_fisher_weights.PickleMe( os.path.splitext(input_filename)[0] + "_w"+str(num_features)+".weights.pickled" ) else: # I've already done all that, just proceed from here: 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 ):
# 7. Save your work: reduced_training_set.PickleMe( os.path.splitext(input_filename)[0] + ".fit.pickled") reduced_fisher_weights.PickleMe( os.path.splitext(input_filename)[0] + "_w" + str(num_features) + ".weights.pickled") else: # I've already done all that, just proceed from here: 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)