def create_results_dict(): results = {} for test_set in ALL_VOLUME_IDS: if test_set not in results: results[test_set] = {} gt_path = get_paths("gala-evaluate", "train", SIZE, test_set, "XX", "XX", "XX", "XX")["groundtruth"] gt = io.read_image_stack(gt_path) for train_set in ALL_VOLUME_IDS: if train_set not in results[test_set]: results[test_set][train_set] = {} for feature in ALL_FEATURES: if feature not in results[test_set][train_set]: results[test_set][train_set][feature] = {} for cue in ALL_CUES: seg_path = get_paths("gala-evaluate", "train", SIZE, test_set, cue, feature, "", train_set)["segmentation"] try: seg = io.read_image_stack(seg_path) err = rand_error(seg,gt) print "test %s, train %s, %s, %s: %f" % (test_set, train_set, feature, cue, err) results[test_set][train_set][feature][cue] = err except: print " -- missing: test %s, train %s, %s, %s: %s" % ( test_set, train_set, feature, cue, seg_path) return results
def test(index, policy): ws = imio.read_image_stack('watershed-%i.lzf.h5' % index) pr = imio.read_image_stack('probabilities-inv-norm-%i.lzf.h5' % index) g = agglo.Rag(ws, pr, merge_priority_function=policy, feature_manager=fman) g.agglomerate(np.inf) return g.tree
def main(): parser = argparse.ArgumentParser() parser.add_argument("seg", type=str, help="Path to the segmentation") parser.add_argument("gt", type=str, help="Path to the groundtruth") parser.add_argument("--solid", action="store_true", help="show worst merge") parser.add_argument("--direction", action="store_true", help="show directions") parser.add_argument("--plot", action="store_true", help="show vi breakdown plot") args = parser.parse_args() seg = imio.read_image_stack(args.seg) gt = imio.read_image_stack(args.gt) if args.solid: show_greatest_vi(seg, gt) if args.direction: visualize_direction(gt, 59) if args.plot: plot_vi_breakdown(seg, gt)
def train(index): out_fn = 'training-data-%i.h5' % index if os.path.exists(out_fn): data, labels = classify.load_training_data_from_disk(out_fn, names=['data', 'labels']) else: ws_tr = imio.read_image_stack('watershed-%i.lzf.h5' % index) pr_tr = imio.read_image_stack('probabilities-%i.lzf.h5' % index) / 255 gt_tr = imio.read_image_stack('ground-truth-%i.lzf.h5' % index) g = agglo.Rag(ws_tr, pr_tr, feature_manager=fman) data, labels = g.learn_agglomerate(gt_tr, fman, min_num_epochs=4)[0][:2] classify.save_training_data_to_disk([data, labels], fn='training-data-%i.h5' % index, names=['data', 'labels']) print('total training data:', data.shape) print('size in MB:', data.size * data.itemsize / 1e6) rf = classify.DefaultRandomForest() rf.fit(data, labels[:, 0]) policy = agglo.classifier_probability(fman, rf) return policy
def run_gala_evaluate(traintest, size, volume_id, cues_id, features_id, exec_id, classifier_volume_id): """ Parameters ---------- traintest: {"train", "test"} whether to evaluate the output of the segmentation of the training or testing data size: the size of the dataset being evaluated and the groundtruth cues_id: a '+' separated list of cues used in training and segmentation features_id: the id of the features used in training and segmentation """ if classifier_volume_id == "all": for new_cl_volume_id in ALL_VOLUME_IDS: run_gala_evaluate(traintest, size, volume_id, cues_id, features_id, exec_id, new_cl_volume_id) return if volume_id == "all": for new_volume_id in ALL_VOLUME_IDS: run_gala_evaluate(traintest, size, new_volume_id, cues_id, features_id, exec_id, classifier_volume_id) return paths = get_paths("gala-evaluate", traintest, size, volume_id, cues_id, features_id, exec_id, classifier_volume_id) for file_use in ["segmentation", "groundtruth"]: if not os.path.isfile(paths[file_use]): print "Unable to find %s file for %s %s %s %s %s classifier tested on %s" % ( file_use, classifier_volume_id, size, cues_id, features_id, exec_id, volume_id) return seg = read_image_stack(paths["segmentation"]) gt = read_image_stack(paths["groundtruth"]) error = rand_error(seg, gt) print "For classifier trained on %s %s %s %s %s and tested on %s %s" % ( classifier_volume_id, size, cues_id, features_id, exec_id, volume_id, size) print "\tfound Rand error: %f" % (error)
def gen_watershed(self): from gala import imio import numpy from skimage import morphology as skmorph from scipy.ndimage import label self.datadir = os.path.abspath(os.path.dirname(sys.modules["gala"].__file__)) + "/testdata/" prediction = imio.read_image_stack(self.datadir +"pixelprobs.h5", group='/volume/prediction', single_channel=False) boundary = prediction[...,0] seeds = label(boundary==0)[0] supervoxels = skmorph.watershed(boundary, seeds) return supervoxels, boundary, prediction
def gen_watershed(self): from gala import imio import numpy from skimage import morphology as skmorph from scipy.ndimage import label self.datadir = os.path.abspath( os.path.dirname(sys.modules["gala"].__file__)) + "/testdata/" prediction = imio.read_image_stack(self.datadir + "pixelprobs.h5", group='/volume/prediction', single_channel=False) boundary = prediction[..., 0] seeds = label(boundary == 0)[0] supervoxels = skmorph.watershed(boundary, seeds) return supervoxels, boundary, prediction
def write_saalfeld(fn, raw, labels, res=np.array([12., 1, 1])): imio.write_h5_stack(raw, fn, group='raw') imio.write_h5_stack(labels, fn, group='labels') f = h5py.File(fn, 'a') f['/raw'].attrs['resolution'] = res f['/labels'].attrs['resolution'] = res f.close() if __name__ == '__main__': index_pairs = [(3 - ts, ts) for ts in range(4)] trees = joblib.Parallel(n_jobs=4)(joblib.delayed(train_test_pair)(*p) for p in index_pairs) trees = dict(zip(index_pairs, trees)) with open('results.pickle', 'wb') as fout: pickle.dump(trees, fout, protocol=-1) images = imio.read_image_stack('/groups/saalfeld/saalfeldlab/concha/sample_A/crop/raw/*.tiff') wss = [imio.read_image_stack('watershed-%i.lzf.h5' % i) for i in range(4)] maps = [t.get_map(0.5) for t in trees] segs = [m[ws] for m, ws in zip(maps, wss)] seg = np.zeros(images.shape, dtype=np.uint64) seg[:, :625, :625] = segs[0] seg[:, :625, 625:] = segs[1] + np.max(segs[0]) seg[:, 625:, :625] = segs[2] + np.max(segs[0]) + np.max(segs[1]) seg[:, 625:, 625:] = segs[3] + np.max(segs[0]) + np.max(segs[1]) + np.max(segs[2]) write_saalfeld('/groups/saalfeld/saalfeldlab/concha/sample_A/juan/corners-segments2.h5', images, seg)
from gala import morpho from gala import imio import numpy as np pr = imio.read_image_stack('membrane/*.tiff') pr = 1 - pr / np.max(pr) ws = morpho.watershed_sequence(pr, axis=0, n_jobs=4, connectivity=2, smooth_thresh=0.04, minimum_seed_size=0) imio.write_h5_stack(ws, 'watershed.lzf.h5', compression='lzf') slices = [(slice(None), slice(None, 625), slice(None, 625)), (slice(None), slice(None, 625), slice(625, None)), (slice(None), slice(625, None), slice(None, 625)), (slice(None), slice(625, None), slice(625, None))] wss = [ws[s] for s in slices] from skimage.measure import label for i, vol in enumerate(wss): fn = 'watershed-%i.lzf.h5' % i vol_relabel = label(vol) print(np.max(vol_relabel)) imio.write_h5_stack(vol_relabel, fn, compression='lzf')
# IPython log file from gala import agglo2, imio frag = imio.read_image_stack('tests/example-data/train-ws.lzf.h5') pr = imio.read_image_stack('tests/example-data/train-p1.lzf.h5') g = agglo2.SparseRAG(frag, pr, [tz.identity, np.square]) g.graph[1, 2] g.compute_feature_caches() from importlib import reload reload(agglo2) g = agglo2.SparseRAG(frag, pr, [tz.identity, np.square]) g.compute_feature_caches() g.compute_features(1, 2) from gala import Rag from gala import agglo np.sum(pr[frag == 1]) np.sum(frag == 1) np.sum(frag == 2) np.mean(pr[frag == 1]) np.mean(np.square(pr)[frag == 1]) from gala import features gorig = agglo.Rag(frag, pr, feature_manager=features.default.moments_hist()) mh = features.moments.moments_hist() mh = features.default.moments_hist() mh(gorig, 1, 2) mm = features.moments.Manager() get_ipython().set_next_input('mm = features.moments.Manager');get_ipython().magic('pinfo features.moments.Manager') mm = features.moments.Manager(nmoments=2, use_diff_features=False) gorig = agglo.Rag(frag, pr, feature_manager=mm) mm(gorig, 1, 2)
def deploy(args): #probability map print("Deploying through driver") if args.prob_file.endswith('.hdf5'): mem = imio.read_image_stack(args.prob_file, single_channel=False) else: mem = np.load(args.prob_file) #X,Y,Z mem = np.transpose(np.squeeze(mem),(2,0,1)) #gala wants z,x,y? pr_test = np.zeros_like(mem) for z in range(0,mem.shape[0]): pr_test[z,:,:] = dilation(mem[z,:,:], disk(10)) pr_test[z,:,:] = erosion(mem[z,:,:], disk(4)) seg_out = np.zeros(pr_test.shape) pr_dim = pr_test.shape xsize = pr_dim[1] ysize = pr_dim[2] zsize = pr_dim[0] print(pr_dim) print(pr_dim[0]) print(np.int(pr_dim[0]/zsize)) print("Starting loop") for iz in range(0,np.int(pr_dim[0]/zsize)): for ix in range(0,np.int(pr_dim[1]/xsize)): for iy in range(0,np.int(pr_dim[2]/ysize)): p0 = pr_test[iz*zsize+0:iz*zsize+zsize,ix*xsize+0:ix*xsize+xsize,iy*ysize+0:iy*ysize+ysize] p0 = np.around(p0,decimals=2) print(p0) #get trained classifier #npzfile = np.load(args.train_file) #rf = npzfile['rf'] #fc = npzfile['fc'] binary_file = open(args.train_file,mode='rb') print(binary_file) temp = pickle.load(binary_file) fc = temp[0] rf = temp[1] binary_file.close() learned_policy = agglo.classifier_probability(fc, rf) #pr_test = (map(imio.read_h5_stack, # ['test-p1.lzf.h5'])) print('watershed') seeds = label(p0==0)[0] seeds_cc_threshold = args.seeds_cc_threshold seeds = morpho.remove_small_connected_components(seeds, seeds_cc_threshold) ws_test = skmorph.watershed(p0, seeds) g_test = agglo.Rag(ws_test, p0, learned_policy, feature_manager=fc) g_test.agglomerate(args.agg_threshold) #This is a map of labels of the same shape as the original image. seg_test1 = g_test.get_segmentation() seg_out[iz*zsize+0:iz*zsize+zsize,ix*xsize+0:ix*xsize+xsize,iy*ysize+0:iy*ysize+ysize] = seg_test1 seg_out = np.transpose(seg_out,(1,2,0)) with open(args.outfile, 'wb') as f: np.save(f,seg_out) return
# IPython log file import numpy as np array = np.array from skimage import data, color from skimage import filter as imfilter import sys sys.path.append('/Users/nuneziglesiasj/projects/gala') from gala import morpho from gala import viz import pdb from skimage.segmentation import slic from gala import imio dorsal = imio.read_image_stack('/Users/nuneziglesiasj/Data/armi-data/cyril-sample-images/dorsal1-d5/png/*.png') dorsal.shape dorsal.max() dorsal = dorsal.astype(float)/255 #sdors = slic(dorsal, 1000, ratio=5, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) #sdors = slic(dorsal, 500, ratio=20, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) #sdors = slic(dorsal, 500, ratio=40, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) #sdors = slic(dorsal, 500, ratio=1, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) #sdors = slic(dorsal, 500, ratio=3.5, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) sdors = slic(dorsal, 1000, ratio=3.5, sigma=array([0.2, 1, 1, 0]), multichannel=True, convert2lab=False) sdorsim = viz.draw_seg(sdors, dorsal)