def test_NewFromDirectory( self ): """""" ref_sig_path = 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_5_4-l.sig' ref_fv = FeatureVector.NewFromSigFile( pychrm_test_dir + sep + ref_sig_path ) from shutil import copy tempdir = mkdtemp() img_filename = "lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_5_4-l.tiff" orig_img_filepath = pychrm_test_dir + sep + img_filename copy( orig_img_filepath, tempdir ) try: fs = FeatureSpace.NewFromDirectory( tempdir, quiet=False ) self.assertTrue( compare( fs.data_matrix[0], ref_fv.values ) ) #from numpy.testing import assert_allclose #assert_allclose( ref_fv.values, fs.data_matrix[0], rtol=1e-05 ) finally: rmtree( tempdir ) from os import mkdir toptempdir = mkdtemp() try: class_names = [] for letter in 'CBA': dirname = toptempdir + sep + letter mkdir( dirname ) copy( orig_img_filepath, dirname ) fs = FeatureSpace.NewFromDirectory( toptempdir, quiet=False, ) self.assertEqual( fs.class_names, ['A', 'B', 'C' ] ) for row_of_features in fs.data_matrix: self.assertTrue( compare( row_of_features, ref_fv.values ) ) finally: rmtree( toptempdir )
def test_LargeFeatureSetGrayscale( self ): """Large feature set, grayscale image""" reference_sample = FeatureVector.NewFromSigFile( self.sig_file_path, image_path=self.test_tif_path ) target_sample = FeatureVector( source_filepath=self.test_tif_path, long=True).GenerateFeatures( write_to_disk=False ) # This doesn't work since the ranges of features are so wide # Tried using relative tolerance, but no dice: # from numpy.testing import assert_allclose # assert_allclose( reference_sample.values, target_sample.values, rtol=1e-3 ) # 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( target_sample.values, reference_sample.values ) )
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_FeatureComputationFromROI(self): """Specify bounding box to FeatureVector, calc features, then compare with C++ implementation-calculated feats.""" # orig image lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif # has size=1388x1040 # WND-CHARM command line specifies via -tCxR param # where C is columns and R is rows, ergo 5 rows, 6 cols = -t6x5 # tile dims => w=1388/6 cols = 231.33px wide, h=1040/5 rows = 208 px tall ROI_width = 231 ROI_height = 208 # 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_t6x5_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 kwargs = {} kwargs['name'] = img_filename kwargs['source_filepath'] = input_image_path #kwargs[ 'feature_names' ] = fw.feature_names #kwargs[ 'feature_computation_plan' ] = comp_plan kwargs['long'] = True kwargs['x'] = 0 kwargs['y'] = 0 kwargs['w'] = ROI_width kwargs['h'] = ROI_height 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 + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_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.assertEqual(top_left_tile_feats.feature_names, top_left_tile_reference_feats.feature_names) self.assertTrue( compare(top_left_tile_feats.values, top_left_tile_reference_feats.values)) kwargs['x'] = 1155 kwargs['y'] = 832 bot_right_tile_feats = FeatureVector(**kwargs).GenerateFeatures( quiet=False, write_to_disk=False) bot_right_tile_reference_feats = FeatureVector.NewFromSigFile( tempdir + sep + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_5_4-l.sig' ) self.assertEqual(bot_right_tile_feats.feature_names, bot_right_tile_reference_feats.feature_names) self.assertTrue( compare(bot_right_tile_feats.values, bot_right_tile_reference_feats.values)) 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 # WND-CHARM command line specifies via -tCxR param # where C is columns and R is rows, ergo 5 rows, 6 cols = -t6x5 # tile dims => w=1388/6 cols = 231.33px wide, h=1040/5 rows = 208 px tall #scan_x = 231 #scan_y = 208 #num_features = 200 # Inflate the zipped test fit into a temp file sourcedir = mkdtemp() targetdir = mkdtemp() try: import zipfile reference_sigs = pychrm_test_dir + sep + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E_t6x5_REFERENCE_SIGFILES.zip' zf = zipfile.ZipFile( reference_sigs, mode='r' ) zf.extractall( targetdir ) 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, sourcedir ) input_image_path = sourcedir + sep + img_filename # Create sliding window that emulates 6x5 tiling: kwargs = {} kwargs[ 'source_filepath' ] = input_image_path kwargs[ 'tile_num_cols' ] = 6 kwargs[ 'tile_num_rows' ] = 5 kwargs[ 'long' ] = True window = SlidingWindow( **kwargs ) print "Number of samples = " + str( window.num_positions ) base, ext = splitext( input_image_path ) ref_file = 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_{}_{}-l.sig' # top left: for test_feats in window.sample(): test_feats.GenerateFeatures( quiet=False, write_to_disk=False, cache=True ) reference_feats = FeatureVector.NewFromSigFile( targetdir + sep + ref_file.format(0,0) ) self.assertTrue( compare( test_feats.values, reference_feats.values ) ) break # below top left: #window.GenerateFeatures( quiet=False, write_to_disk=False, cache=True ) #reference_feats = FeatureVector.NewFromSigFile( targetdir + sep + ref_file.format(0,1) ) #self.assertTrue( compare( window.values, 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 finally: rmtree( sourcedir ) rmtree( targetdir )