Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)