示例#1
0
def Get_feat_test(ind,h,w, M_mean,nb_features):
    img=np.empty([h,w,3])
    GRAY_IM=np.empty([h,w])
    img=M_mean[:,:,:][ind]
    
    gray_img = rgb2gray(img)
    gray_img = (gray_img * 255 ).astype( np.uint8 )
    GRAY_IM[:,:]=gray_img-np.mean(gray_img)
    GRAY_IM
    
    FV=np.empty([nb_features])
    matrix = PyImageMatrix()
    matrix.allocate(h, w)
    numpy_matrix = matrix.as_ndarray()
    numpy_matrix[:] = GRAY_IM[:,:]
    fv = FeatureVector( name='stufff', long=True, original_px_plane=matrix )
    t1 = time.time()
    fv.GenerateFeatures(quiet=True, write_to_disk=False)
    t2 = time.time()
    
    FV[:]=fv.values
    Names=fv.feature_names
    FV
    FV=FV.astype(float)
    return(FV)
示例#2
0
def calc_features(img_arr, plane_tag, long=False):
    assert len(img_arr.shape) == 2
    pychrm_matrix = PyImageMatrix()
    pychrm_matrix.allocate(img_arr.shape[1], img_arr.shape[0])
    numpy_matrix = pychrm_matrix.as_ndarray()
    numpy_matrix[:] = img_arr
    signatures = FeatureVector(basename=plane_tag, long=long)
    signatures.original_px_plane = pychrm_matrix
    signatures.GenerateFeatures(write_to_disk=False)
    return signatures
示例#3
0
def calc_features(img_array, tag, long=False, w=None, h=None,
                  dx=None, dy=None, ox=None, oy=None):
    if len(img_array.shape) != 2:
        raise ValueError("array must be two-dimensional")
    for i, j, tile in gen_tiles(
            img_array, w=w, h=h, dx=dx, dy=dy, ox=ox, oy=oy):
        signatures = FeatureVector(basename=tag, long=long)
        signatures.original_px_plane = get_image_matrix(tile)
        signatures.GenerateFeatures(write_to_disk=False)
        signatures.x, signatures.y = j, i
        signatures.h, signatures.w = tile.shape
        yield signatures
示例#4
0
    def test_WND5_all_features(self):
        epsilon = 0.00001

        # Define paths to original files
        test_sig_path = join(test_dir, 't1_s01_c05_ij-l_precalculated.sig')
        test_fit_path = join(test_dir, 'test-l.fit')
        test_feat_wght_path = join(test_dir, 'test_fit-l.weights')
        test_tif_path = join(test_dir, 't1_s01_c05_ij.tif')

        # Here are the correct values that Python API needs to return:
        # wndchrm classify -l -f0.75 test-l.fit t1_s01_c05_ij.tif
        # t1_s01_c05_ij.tif    1.6e-27    0.083    0.917    *    4cell    3.835
        # wndchrm classify -l test-l.fit t1_s01_c05_ij.tif
        # t1_s01_c05_ij.tif    3.19e-27    0.076    0.924    *    4cell    3.848
        # wndchrm classify -l -f0.05 test-l.fit t1_s01_c05_ij.tif
        # t1_s01_c05_ij.tif    1.06e-26    0.066    0.934    *    4cell    3.869

        correct_marg_probs = {}
        correct_marg_probs[2189] = [0.083, 0.917]
        correct_marg_probs[438] = [0.076, 0.924]
        correct_marg_probs[146] = [0.066, 0.934]

        # Load the original files once and only once for all this class's tests
        feature_set = FeatureSpace.NewFromFitFile(test_fit_path)
        fs1 = feature_set.feature_names
        feature_set.Normalize()
        fs2 = feature_set.feature_names
        self.assertSequenceEqual(fs1, fs2)

        test_sample = FeatureVector(source_filepath=test_tif_path, long=True)
        test_sample.LoadSigFile(test_sig_path)
        self.assertSequenceEqual(feature_set.feature_names,
                                 test_sample.feature_names)
        test_sample.Normalize(feature_set)

        all_weights = FisherFeatureWeights.NewFromFile(test_feat_wght_path)

        def Check(num_feats):
            weights = all_weights.Threshold(num_feats)
            feat_set = feature_set.FeatureReduce(weights)
            sample = test_sample.FeatureReduce(weights)
            result = SingleSampleClassification.NewWND5(
                feat_set, weights, sample)
            result_marg_probs = [ round( val, 3 ) \
                    for val in result.marginal_probabilities ]
            for target_val, res_val in zip(correct_marg_probs[num_feats],
                                           result_marg_probs):
                self.assertAlmostEqual(target_val, res_val, delta=epsilon)

        for num_feats in correct_marg_probs:
            Check(num_feats)
示例#5
0
def Features_calcul_np_GrayscaleIm_WND(ind, nb_features, w, h, GRAY):
    " calculate WND Charm Features from grayscale images (2919 features)"
    FV = np.empty([nb_features])
    matrix = PyImageMatrix()
    matrix.allocate(h, w)
    numpy_matrix = matrix.as_ndarray()
    numpy_matrix[:] = GRAY[:, :][ind]
    fv = FeatureVector(name='stufff', long=True, original_px_plane=matrix)
    t1 = time.time()
    fv.GenerateFeatures(quiet=True, write_to_disk=False)
    t2 = time.time()

    FV[:] = fv.values
    Names = fv.feature_names
    FV
    return (FV, Names)
示例#6
0
    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 )
示例#7
0
    def test_LoadSubsetFromFile( self ):
        """Calculate one feature family, store to sig, load sig, and use to create larger fs"""

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

        full_list = list( ('Pixel Intensity Statistics () [3]',
                'Pixel Intensity Statistics (Fourier ()) [3]',) )

        tempdir = mkdtemp()

        from shutil import copy
        
        try:
            # copy the tiff to the tempdir so the .sig files end up there too
            copy( orig_img_filepath, tempdir )
            input_img_path = tempdir + sep + img_filename

            kwargs = {}
            kwargs[ 'source_filepath' ] = input_img_path
            kwargs[ 'tile_num_cols' ] = 6
            kwargs[ 'tile_num_rows' ] = 5
            kwargs[ 'tiling_scheme' ] = '5x6'
            kwargs[ 'tile_col_index' ] = 0
            kwargs[ 'tile_row_index' ] = 0
            kwargs[ 'feature_names' ] = full_list[1:]

            fv1 = FeatureVector( **kwargs ).GenerateFeatures(quiet=False)

            # modify the sig value and write to sig file to make sure subsequent loading
            # used the value from disk and not recalculated it:
            fv1.values[0] = -9999
            fv1.ToSigFile(quiet=False)

            # Now, ask for more features:
            kwargs[ 'feature_names' ] = full_list
            fv2 = FeatureVector( **kwargs )
            with self.assertRaises( IncompleteFeatureSetError ):
                fv2.LoadSigFile()

            #import pdb; pdb.set_trace()
            fv2.GenerateFeatures()
            #self.assertEqual( fv1.values[0], fv2.values[0] )

        finally:
            rmtree( tempdir )
示例#8
0
    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 ) )
示例#9
0
    def test_LoadSubsetFromFile(self):
        """Calculate one feature family, store to sig, load sig, and use to create larger fs"""

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

        full_list = list((
            'Pixel Intensity Statistics () [3]',
            'Pixel Intensity Statistics (Fourier ()) [3]',
        ))

        tempdir = mkdtemp()

        from shutil import copy

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

            kwargs = {}
            kwargs['source_filepath'] = input_img_path
            kwargs['tile_num_cols'] = 6
            kwargs['tile_num_rows'] = 5
            kwargs['tiling_scheme'] = '5x6'
            kwargs['tile_col_index'] = 0
            kwargs['tile_row_index'] = 0
            kwargs['feature_names'] = full_list[1:]

            fv1 = FeatureVector(**kwargs).GenerateFeatures(quiet=False)

            # modify the sig value and write to sig file to make sure subsequent loading
            # used the value from disk and not recalculated it:
            fv1.values[0] = -9999
            fv1.ToSigFile(quiet=False)

            # Now, ask for more features:
            kwargs['feature_names'] = full_list
            fv2 = FeatureVector(**kwargs)
            with self.assertRaises(IncompleteFeatureSetError):
                fv2.LoadSigFile()

            #import pdb; pdb.set_trace()
            fv2.GenerateFeatures()
            #self.assertEqual( fv1.values[0], fv2.values[0] )

        finally:
            rmtree(tempdir)
示例#10
0
def calc_features(img_array,
                  tag,
                  long=False,
                  w=None,
                  h=None,
                  dx=None,
                  dy=None):
    if len(img_array.shape) != 2:
        raise ValueError("array must be two-dimensional")
    for i, j, tile in gen_tiles(img_array, w=w, h=h, dx=dx, dy=dy):
        signatures = FeatureVector(basename=tag, long=long)
        signatures.original_px_plane = get_image_matrix(tile)
        signatures.GenerateFeatures(write_to_disk=False)
        signatures.x, signatures.y = j, i
        signatures.h, signatures.w = tile.shape
        yield signatures
示例#11
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)
示例#12
0
    def test_ParallelTiling(self):
        """Specify bounding box to FeatureVector, calc features, then compare
        with C++ implementation-calculated feats."""

        import zipfile
        from shutil import copy
        from tempfile import NamedTemporaryFile

        refdir = mkdtemp(prefix='ref')
        targetdir = mkdtemp(prefix='target')

        try:
            reference_feats = pychrm_test_dir + sep + 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E_t6x5_REFERENCE_SIGFILES.zip'
            zf = zipfile.ZipFile(reference_feats, mode='r')
            zf.extractall(refdir)

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

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

            with NamedTemporaryFile(mode='w',
                                    dir=refdir,
                                    prefix='ref',
                                    delete=False) as temp:
                ref_fof = temp.name
                temp.write('reference_samp\ttest_class\t{}\t{{}}\n'.format(
                    refdir + sep + img_filename))
            with NamedTemporaryFile(mode='w',
                                    dir=targetdir,
                                    prefix='target',
                                    delete=False) as temp:
                target_fof = temp.name
                temp.write(
                    'test_samp\ttest_class\t{}\t{{}}\n'.format(targetdir +
                                                               sep +
                                                               img_filename))

            global_sampling_options = \
                FeatureVector( long=True, tile_num_cols=6, tile_num_rows=5 )

            # Should just load reference sigs
            ref_fs = FeatureSpace.NewFromFileOfFiles(
                ref_fof,
                quiet=False,
                global_sampling_options=global_sampling_options)
            target_fs = FeatureSpace.NewFromFileOfFiles(
                target_fof,
                n_jobs=True,
                quiet=False,
                global_sampling_options=global_sampling_options)

            #from numpy.testing import assert_allclose
            #self.assertTrue( assert_allclose( ref_fs.data_matrix, target_fs.data_matrix ) )
            from wndcharm.utils import compare
            for row_num, (ref_row, test_row) in enumerate(
                    zip(ref_fs.data_matrix, target_fs.data_matrix)):
                retval = compare(ref_row, test_row)
                if retval == False:
                    print "error in sample row", row_num
                    print "FIT: ", ref_fs._contiguous_sample_names[
                        row_num], "FOF", target_fs._contiguous_sample_names[
                            row_num]
                self.assertTrue(retval)
        finally:
            rmtree(refdir)
            rmtree(targetdir)
示例#13
0
 def __get_exp_features(self, a, long=False):
     fn = os.path.join(self.wd, "img.tiff")
     dump_to_tiff(a, fn)
     fv = FeatureVector(source_filepath=fn, long=long)
     return fv.GenerateFeatures(write_to_disk=False)
示例#14
0
    def test_GetPreprocessedFullPixelPlane( self ):
        """Exercise pixel intensity mean/std shift, and downsample"""

        debug = False
        if debug:
            from wndcharm import package_versions
            print package_versions
            print '==========================='

        img = wndchrm_test_dir + sep + "t1_s01_c05_ij.tif"
        # px plane	min	max	mean	std
        # np orig:	411.00	1465.00	912.19	129.31

        samp = FeatureVector( source_filepath=img )
        orig = samp.GetOriginalPixelPlane( cache=True )
        orig_np = orig.as_ndarray()

        target_mean = 1000
        target_std = 100

        if debug:
            frmt = "{}:\t{:0.2f}\t{:0.2f}\t{:0.2f}\t{:0.2f}"
            print "target mean: {}, target std: {}".format( target_mean, target_std )
            print "px plane\tmin\tmax\tmean\tstd"
            d = "np orig", orig_np.min(), orig_np.max(), orig_np.mean(), orig_np.std(),
            print frmt.format( *d )

        # Mean shift only
        samp.pixel_intensity_mean = target_mean
        mean_shifted = samp.GetPreprocessedFullPixelPlane( cache=False )
        mean_shifted_np = mean_shifted.as_ndarray()
        if debug:
            d = ( "mean   ", mean_shifted_np.min(), mean_shifted_np.max(), mean_shifted_np.mean(), mean_shifted_np.std() )
            print frmt.format( *d )
        self.assertEqual( target_mean, int( mean_shifted_np.mean() ) )

        # Mean and std shift
        samp.pixel_intensity_stdev = target_std
        mean_std_shifted = samp.GetPreprocessedFullPixelPlane( cache=False )
        mean_std_shifted_np = mean_std_shifted.as_ndarray()
        if debug:
            d = ( "mean&std", mean_std_shifted_np.min(), mean_std_shifted_np.max(),
            mean_std_shifted_np.mean(), mean_std_shifted_np.std() )
            print frmt.format( *d )

        self.assertEqual( target_mean, int( round( mean_std_shifted_np.mean() ) ) )
        self.assertEqual( target_std, int( round( mean_std_shifted_np.std() ) ) )

        # downsample
        samp.pixel_intensity_mean = None
        samp.pixel_intensity_std = None

        w = orig.width
        h = orig.height

        downsample = 25
        samp.downsample = downsample
        target_w = int( w * float( downsample ) / 100 )
        target_h = int( h * float( downsample ) / 100 )

        ds = samp.GetPreprocessedFullPixelPlane( cache=False )
        ds_np = ds.as_ndarray()

        new_h, new_w = ds_np.shape

        if debug:
            print "orig dimensions: {}x{}".format( w, h )
            print "downsample {}%: {}x{}".format( downsample, new_w, new_h )

        self.assertEqual( target_w, new_w )
        self.assertEqual( target_h, new_h )
示例#15
0
def Get_proba(ind, h, w, M_mean, nb_features):
    img = np.empty([h, w, 3])
    GRAY_IM = np.empty([h, w])
    img = M_mean[:, :, :][ind]

    gray_img = rgb2gray(img)
    gray_img = (gray_img * 255).astype(np.uint8)
    GRAY_IM[:, :] = gray_img - np.mean(gray_img)
    GRAY_IM

    FV = np.empty([nb_features])
    matrix = PyImageMatrix()
    matrix.allocate(h, w)
    numpy_matrix = matrix.as_ndarray()
    numpy_matrix[:] = GRAY_IM[:, :]
    fv = FeatureVector(name='stufff', long=True, original_px_plane=matrix)
    t1 = time.time()
    fv.GenerateFeatures(quiet=True, write_to_disk=False)
    t2 = time.time()

    FV[:] = fv.values
    Names = fv.feature_names
    FV
    FV = FV.astype(float)

    pca = decomposition.PCA()
    RFC = RandomForestClassifier()

    estimators = [('reduce_dim', pca), ('Random_Forest', RFC)]
    pipe = Pipeline(estimators)

    params = dict(reduce_dim__n_components=90,
                  Random_Forest__n_estimators=200,
                  Random_Forest__random_state=0)

    filename_Features_two_blocs = projectpath + 'io/Output/Features_two_blocs.npy'
    FV_N = np.load(filename_Features_two_blocs)
    X = FV_N

    Data_FRAMES = pd.read_pickle(projectpath + 'io/Output/Dataframe_.pkl')
    yr = Get_true_y(Data_FRAMES)

    filename_yr = projectpath + 'io/Output/yr.npy'
    np.save(filename_yr, yr)
    yr = np.load(filename_yr)

    RFC = RandomForestClassifier(n_estimators=200, random_state=0)

    predictedVAL = cross_val_predict(RFC, X, yr, n_jobs=-1)
    metrics.accuracy_score(yr, predictedVAL)
    Conf_Mat = confusion_matrix(yr, predictedVAL)

    RFC.fit(X, yr)

    predict_probab = np.ones([M_mean.shape[0], 3])

    predict_proba = RFC.predict_proba(FV)

    predict_probab[ind, 0] = predict_proba[:, 0]
    predict_probab[ind, 1] = predict_proba[:, 1]
    predict_probab[ind, 2] = predict_proba[:, 2]

    return (predict_probab)
示例#16
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

        # 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 )
示例#17
0
    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)