def test_submatrix(self): """crop""" tempdir = mkdtemp() # crop the test image (size=1388x1040) to be bottom right tile of the test image # tiled with 6 cols and 5 rows => ROI= 231x208+1155+832 orig_big = join( pychrm_test_dir, 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif') orig_cropped = join( pychrm_test_dir, 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_5_4-l.tiff' ) test_cropped_path = join(tempdir, 'TEST_submatrix.tif') x = 1155 y = 832 w = 231 h = 208 x1 = x y1 = y x2 = x1 + w - 1 y2 = y1 + h - 1 # setting mean = 0 is flag to not use mean in ImageMatrix::OpenImage() downsample = 0 bb = None mean = 0.0 stddev = 0.0 try: origim = PyImageMatrix() if 1 != origim.OpenImage(orig_big, downsample, bb, mean, stddev): self.fail('Could not build an ImageMatrix from ' + orig_big) # API calls for copying desired pixels into empty ImageMatrix instance: # the_tiff is garbage collected on return cropped_im = PyImageMatrix() # void ImageMatrix::submatrix (const ImageMatrix &matrix, const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2) cropped_im.submatrix(origim, x1, y1, x2, y2) # no retval cropped_im.SaveTiff(test_cropped_path) orig_pixels = plt.imread(orig_cropped) cropped_pixels = plt.imread(test_cropped_path) assert_equal(orig_pixels, cropped_pixels) finally: rmtree(tempdir)
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)
def test_SaveImage(self): """SaveImage pixels to file.""" tempdir = mkdtemp() orig = join( pychrm_test_dir, 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif') # setting mean = 0 is flag to not use mean in ImageMatrix::OpenImage() downsample = 0 bb = None mean = 0.0 stddev = 0.0 try: origim = PyImageMatrix() if 1 != origim.OpenImage(orig, downsample, bb, mean, stddev): self.fail('Could not build an ImageMatrix from ' + orig) copied_tiff = join(tempdir, 'TEST1.tif') origim.SaveTiff(copied_tiff) orig_pixels = plt.imread(orig) copied_pixels = plt.imread(copied_tiff) assert_equal(orig_pixels, copied_pixels) finally: rmtree(tempdir)
def get_image_matrix(img_array): if len(img_array.shape) != 2: raise ValueError("array must be two-dimensional") image_matrix = PyImageMatrix() image_matrix.allocate(img_array.shape[1], img_array.shape[0]) numpy_matrix = image_matrix.as_ndarray() numpy_matrix[:] = img_array return image_matrix
def test_OpenImage(self): """Testing ROI open functionality""" tempdir = mkdtemp() # crop the test image (size=1388x1040) to be bottom right tile of the test image # tiled with 6 cols and 5 rows => ROI= 231x208+1155+832 orig_big = join( pychrm_test_dir, 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E.tif') orig_cropped = join( pychrm_test_dir, 'lymphoma_eosin_channel_MCL_test_img_sj-05-3362-R2_001_E-t6x5_5_4-l.tiff' ) test_cropped_path = join(tempdir, 'TEST_OpenImageROI.tif') x = 1155 y = 832 w = 231 h = 208 from wndcharm import rect bb = rect() bb.x = x bb.y = y bb.w = w bb.h = h # setting mean = 0 is flag to not use mean in ImageMatrix::OpenImage() downsample = 0 #bb = None mean = 0.0 stddev = 0.0 try: cropped_on_load_im = PyImageMatrix() if 1 != cropped_on_load_im.OpenImage(orig_big, downsample, bb, mean, stddev): self.fail('Could not build an ImageMatrix from ' + orig_big) cropped_on_load_im.SaveTiff(test_cropped_path) orig_pixels = plt.imread(orig_cropped) cropped_pixels = plt.imread(test_cropped_path) assert_equal(orig_pixels, cropped_pixels) finally: rmtree(tempdir)
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)
def test_allocate(self): """make an empty pixel plane""" tempdir = mkdtemp() test_path = join(tempdir, "TEST_allocate.tif") num_rows, num_cols = shape = (123, 456) try: origim = PyImageMatrix() # virtual void allocate (unsigned int w, unsigned int h); origim.allocate(num_cols, num_rows) origim.SaveTiff(test_path) pixels = plt.imread(test_path) self.assertEqual(pixels.shape, shape) finally: rmtree(tempdir)
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)