def voi(self, prob, label): prob = np.int64(1 - prob) f_image, n_labels = mahotas.label(prob) f_image = np.int64(f_image.flatten()) f_label = np.int64(label.flatten()) return n_labels, partition_comparison.variation_of_information( f_image, f_label)
def create_vi_plot(initial_segmentation, new_segmentation, vi_s, filepath=None): # create plot new_segmentation_target = Util.propagate_max_overlap( new_segmentation, initial_segmentation) before_vi = partition_comparison.variation_of_information( new_segmentation.ravel(), initial_segmentation.ravel()) target_vi = partition_comparison.variation_of_information( new_segmentation_target.ravel(), initial_segmentation.ravel()) bins = np.arange(0, len(vi_s)) before_vi = [before_vi] * len(vi_s) target_vi = [target_vi] * len(vi_s) fig, ax = plt.subplots() ax.plot(bins, target_vi, 'k--', label='Target VI') ax.plot(bins, vi_s_, 'k', label='Variation of Information') ax.plot(bins, before_vi, 'k:', label='VI before') # ax.set_yscale('log') # Now add the legend with some customizations. legend = ax.legend(loc='upper center', shadow=True) ax.set_ylim([-0.5, 1.5]) # The frame is matplotlib.patches.Rectangle instance surrounding the legend. frame = legend.get_frame() frame.set_facecolor('0.90') # Set the fontsize for label in legend.get_texts(): label.set_fontsize('large') for label in legend.get_lines(): label.set_linewidth(1.5) # the legend line width if filepath: plt.savefig(filepath)
def apply(cls, label_array): ''' Apply the metric to a label_array with shape Y,X,Z. ''' vi_sum = 0. done = 0. print 'Calulating for', label_array.shape[0], 'slices.' for z in range(label_array.shape[0] - 1): vi_sum += partition_comparison.variation_of_information( label_array[z, :, :].astype(np.uint64).ravel(), label_array[z + 1, :, :].astype(np.uint64).ravel()) done += 1 percentage = int(((done) / (label_array.shape[0] - 1)) * 100) if (percentage % 10 == 0): print percentage, '% done' vi_sum /= label_array.shape[0] return vi_sum
for thresh in thresh_range: below_thresh = smooth_output < np.uint16(max_smooth * thresh) #below_thresh = mahotas.morph.close(below_thresh.astype(np.bool), disc) #below_thresh = mahotas.morph.open(below_thresh.astype(np.bool), disc) seeds,nseeds = mahotas.label(below_thresh) if nseeds == 0: continue ws = np.uint32(mahotas.cwatershed(smooth_output, seeds)) voi_score = partition_comparison.variation_of_information(target_segs.ravel(), ws.ravel()) thresh_voi_results.append(voi_score) print 's={0:0.2f}, t={1:0.2f}, voi_score={2:0.4f}.'.format(smooth_sigma, thresh, voi_score) dx, dy = np.gradient(ws) result = np.logical_or(dx!=0, dy!=0) figsize(20,20) imshow(result, cmap=cm.gray) plt.show() if voi_score < best_score: best_score = voi_score best_sigma = smooth_sigma
# print out.shape # print np.where(segs.ravel() != 0) # print np.where(gt.ravel() != 0) # print np.where(out.ravel() != 0) # crop the data to only image data x = 210 y = 60 z = 50 # we dont need since we have already cut the z dim_x = dim_y = 400 segs = segs[:, y:y + dim_y, x:x + dim_x] gt = gt[:, y:y + dim_y, x:x + dim_x] #out = out[:,y:y+dim_y,x:x+dim_x] print 'VI groundtruth vs. groundtruth:', pc.variation_of_information( gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'VI groundtruth vs. pipeline:', pc.variation_of_information( gt.astype(np.uint32).ravel(), segs.ravel()) print 'VI groundtruth vs. proofread:', pc.variation_of_information( gt.astype(np.uint32).ravel(), out.ravel()) print 'Adjusted RandIndex groundtruth vs. groundtruth:', metrics.adjusted_rand_score( gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. pipeline:', metrics.adjusted_rand_score( gt.astype(np.uint32).ravel(), segs.ravel()) print 'Adjusted RandIndex groundtruth vs. proofread:', metrics.adjusted_rand_score( gt.astype(np.uint32).ravel(), out.ravel())
import numpy as np from sklearn import metrics segs = tif.imread('/home/d/TMP/DOJO/mojo_400_5_orig/cut-train-segs.tif') gt = tif.imread('/home/d/TMP/DOJO/mojo_400_5_orig/cut-train-labels.tif') exp = tif.imread('/home/d/dong.tif') exp[exp==0] = 2000 # crop the data to only image data x = 210 y = 60 z = 50 # we dont need since we have already cut the z dim_x = dim_y = 400 segs = segs[:,y:y+dim_y,x:x+dim_x] gt = gt[:,y:y+dim_y,x:x+dim_x] #out = out[:,y:y+dim_y,x:x+dim_x] print 'VI groundtruth vs. groundtruth:', pc.variation_of_information(gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'VI groundtruth vs. pipeline:',pc.variation_of_information(gt.astype(np.uint32).ravel(), segs.ravel()) # print 'VI groundtruth vs. proofread:', pc.variation_of_information(gt.astype(np.uint32).ravel(), out.ravel()) print 'VI groundtruth vs. expert:', pc.variation_of_information(gt.astype(np.uint32).ravel(), exp.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. groundtruth:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. pipeline:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), segs.ravel()) # print 'Adjusted RandIndex groundtruth vs. proofread:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), out.ravel()) print 'Adjusted RandIndex groundtruth vs. expert:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), exp.astype(np.uint32).ravel())
def vi(array1, array2): ''' ''' return partition_comparison.variation_of_information( array1.ravel(), array2.ravel())
# out = np.dstack([out, im]) # else: # out = im # out_is_there = True # print segs.shape # print gt.shape # print out.shape # print np.where(segs.ravel() != 0) # print np.where(gt.ravel() != 0) # print np.where(out.ravel() != 0) # crop the data to only image data x = 210 y = 60 z = 50 # we dont need since we have already cut the z dim_x = dim_y = 400 segs = segs[:,y:y+dim_y,x:x+dim_x] gt = gt[:,y:y+dim_y,x:x+dim_x] #out = out[:,y:y+dim_y,x:x+dim_x] print 'VI groundtruth vs. groundtruth:', pc.variation_of_information(gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'VI groundtruth vs. pipeline:',pc.variation_of_information(gt.astype(np.uint32).ravel(), segs.ravel()) print 'VI groundtruth vs. proofread:', pc.variation_of_information(gt.astype(np.uint32).ravel(), out.ravel()) print 'Adjusted RandIndex groundtruth vs. groundtruth:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. pipeline:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), segs.ravel()) print 'Adjusted RandIndex groundtruth vs. proofread:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), out.ravel())
def run(slice): DATA_PATH = '/Volumes/DATA1/EMQM_DATA/ac3x75/' GOLD_PATH = os.path.join(DATA_PATH,'gold/') RHOANA_PATH = os.path.join(DATA_PATH,'rhoana/') IMAGE_PATH = os.path.join(DATA_PATH,'input/') PROB_PATH = os.path.join(DATA_PATH,'prob/') PATCH_PATH = os.path.join(DATA_PATH,'test_rhoana/') OUTPUT_PATH = os.path.join(DATA_PATH,'fix_rhoana/') gold = _metrics.Util.read(GOLD_PATH+'*.tif') rhoana = _metrics.Util.read(RHOANA_PATH+'*.tif') images = _metrics.Util.read(IMAGE_PATH+'*.tif') probs = _metrics.Util.read(PROB_PATH+'*.tif') SLICE = slice print 'Running on slice', SLICE image = images[SLICE] prob = probs[SLICE] seg = _metrics.Util.normalize_labels(rhoana[SLICE])[0] # fill and normalize gold gold_zeros = _metrics.Util.threshold(gold[SLICE], 0) gold_filled = _metrics.Util.fill(gold[SLICE], gold_zeros.astype(np.bool)) gold_filled_relabeled = skimage.measure.label(gold_filled).astype(np.uint64) gold_normalized = _metrics.Util.normalize_labels(gold_filled_relabeled)[0] # color both segmentations cm = _metrics.Util.load_colormap('/Volumes/DATA1/ac3x75/mojo/ids/colorMap.hdf5') colored_rhoana_normalized = cm[seg % len(cm)] colored_gold_normalized = cm[gold_normalized % len(cm)] print 'Find all patches' t0 = time.time() p = loop(image, prob, seg) print time.time()-t0, 'seconds for', len(p), 'patches' print 'Setting up CNN' val_fn = setup_n() print 'Creating Matrix' m_new = create_matrix(val_fn, seg, p) out = seg sureness = 0. surenesses = [] vi_s = [] images = [] before_VI = partition_comparison.variation_of_information(gold_normalized.ravel(), seg.ravel()) max_counter = 0 while m_new.max() >= sureness: max_counter += 1 print 'Iteration', max_counter print 'Merging for sureness s=', m_new.max() surenesses.append(m_new.max()) m_new, out = merge(val_fn, image, out, prob, m_new) vi = partition_comparison.variation_of_information(out.ravel(), gold_normalized.ravel()) vi_s.append(vi) images.append(np.array(out)) print 'New VI', vi print '-'*80 before_VI = partition_comparison.variation_of_information(gold_normalized.ravel(), seg.ravel()) index = vi_s.index(min(vi_s)) print 'Before VI', before_VI print 'Lowest VI', vi_s[index] print 'Iterations', index print 'Sureness', surenesses[index] # create plot new_rhoana = propagate_max_overlap(seg, gold_normalized) target_vi = partition_comparison.variation_of_information(new_rhoana.ravel(), gold_normalized.ravel()) bins = np.arange(0, len(vi_s))#np.arange(len(vi_s), 0, -1) # bins /= 100 vi_s_ = vi_s #[x for (y,x) in sorted(zip(surenesses, vi_s))] surenesses_ = bins #surenesses#[y for (y,x) in sorted(zip(surenesses, vi_s))] original_vi = [before_VI]*len(vi_s) target_vi = [target_vi]*len(vi_s) fig, ax = plt.subplots() ax.plot(surenesses_, target_vi, 'k--', label='Target VI') ax.plot(surenesses_, vi_s_, 'k', label='Variation of Information') ax.plot(surenesses_, original_vi, 'k:', label='Rhoana VI before') # ax.set_yscale('log') # Now add the legend with some customizations. legend = ax.legend(loc='upper center', shadow=True) ax.set_ylim([0.6,1.5]) # The frame is matplotlib.patches.Rectangle instance surrounding the legend. frame = legend.get_frame() frame.set_facecolor('0.90') # Set the fontsize for label in legend.get_texts(): label.set_fontsize('large') for label in legend.get_lines(): label.set_linewidth(1.5) # the legend line width plt.savefig(OUTPUT_PATH+os.sep+'graph_'+str(slice)+'.png') mh.imsave(OUTPUT_PATH+os.sep+'best_'+str(slice)+'.tif', images[index]) mh.imsave(OUTPUT_PATH+os.sep+'last_'+str(slice)+'.tif', images[-1]) print 'All done.'
def partition_VI(putative, gold): mask = gold != 0 VI_res = partition_comparison.variation_of_information(gold[mask], putative[mask].astype(gold.dtype)) return VI_res
exp = tif.imread('/home/d/dong.tif') exp[exp == 0] = 2000 # crop the data to only image data x = 210 y = 60 z = 50 # we dont need since we have already cut the z dim_x = dim_y = 400 segs = segs[:, y:y + dim_y, x:x + dim_x] gt = gt[:, y:y + dim_y, x:x + dim_x] #out = out[:,y:y+dim_y,x:x+dim_x] print 'VI groundtruth vs. groundtruth:', pc.variation_of_information( gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'VI groundtruth vs. pipeline:', pc.variation_of_information( gt.astype(np.uint32).ravel(), segs.ravel()) # print 'VI groundtruth vs. proofread:', pc.variation_of_information(gt.astype(np.uint32).ravel(), out.ravel()) print 'VI groundtruth vs. expert:', pc.variation_of_information( gt.astype(np.uint32).ravel(), exp.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. groundtruth:', metrics.adjusted_rand_score( gt.astype(np.uint32).ravel(), gt.astype(np.uint32).ravel()) print 'Adjusted RandIndex groundtruth vs. pipeline:', metrics.adjusted_rand_score( gt.astype(np.uint32).ravel(), segs.ravel()) # print 'Adjusted RandIndex groundtruth vs. proofread:', metrics.adjusted_rand_score(gt.astype(np.uint32).ravel(), out.ravel()) print 'Adjusted RandIndex groundtruth vs. expert:', metrics.adjusted_rand_score(