示例#1
0
    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)
示例#2
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)
示例#3
0
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
示例#4
0
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
示例#5
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
示例#6
0
    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)
示例#7
0
    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)
示例#8
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)
    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 )
示例#10
0
    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)
示例#11
0
    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)
示例#12
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)
def extractFeatures(ftb, ds, newOnly, chNames, imageId = None, im = None):
    message = ''
    tc = ftb.tc

    # dataset must be explicitly provided because an image can be linked to
    # multiple datasets in which case im.getDataset() doesn't work
    if not im:
        if not imageId:
            #raise Exception('No input image')
            raise omero.ServerError('No input image')

        im = ftb.conn.getObject('Image', imageId)
        if not im:
            return 'Image id:%d not found\n' % imageId
    else:
        imageId = im.getId()

    tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds)
    if tid:
        if not ftb.openTable(tid):
            return message + '\nERROR: Table not opened\n'
        version = unwrap(ftb.versiontag.getTextValue())
        # version seems to be in unicode
        message += 'Opened table id:%d version:%s\n' % (tid, str(version))

        if newOnly and ftb.tableContainsId(imageId):
            return message + 'Image id:%d features already in table' % imageId

    # FIXME: default is convert multichannel to greyscale unless user input

    # Calculate features for an image channel
    # Optionally prepend the channel label to each feature name and combine
    ftall = None
    for c in xrange( len( chNames ) ):
	    
        wndcharm_matrix = PyImageMatrix()
        wndcharm_matrix.allocate( im.getSizeX(), im.getSizeY() )
        numpy_matrix = wndcharm_matrix.as_ndarray()

        numpy_matrix[:] = im.getPrimaryPixels().getPlane(theZ=0,theC=c,theT=0)
        feature_plan = wndcharm.StdFeatureComputationPlans.getFeatureSet();
        options = "" # This is where you can tell wnd-charm to normalize pixel intensities, 
                     # take ROIs etc. ... leave blank for now.
        ft = Signatures.NewFromFeatureComputationPlan( wndcharm_matrix, feature_plan, options )

        ft.names = [WndcharmStorage.insert_channel_name(
                    n, chNames[c]) for n in ft.names]
        ft.source_path = im.getName()
        if not ftall:
            ftall = ft
        else:
            ftall.names += ft.names
            ftall.values += ft.values

    # Save the features to a table
    if not tid:
        ftb.createTable(ftall.names, ft.version)
        version = unwrap(ftb.versiontag.getTextValue())
        message += 'Created new table id:%d version:%s\n' % (
            ftb.tc.tableId, version)
        message += WndcharmStorage.addFileAnnotationTo(tc, ds)

    if version != ft.version:
        return message + 'Incompatible version: Stored=%s Calculated=%s' % (
            version, ft.version)
    ftb.saveFeatures(imageId, ftall)

    return message + 'Extracted features from Image id:%d\n' % imageId