How to use the gala.agglo.classifier_probability function in gala

To help you get started, we’ve selected a few gala examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github janelia-flyem / gala / tests / _util / generate-test-results.py View on Github external
g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
g_test.agglomerate(0.5)
seg_test1 = g_test.get_segmentation()
imio.write_h5_stack(seg_test1, 'example-data/test-seg1.lzf.h5', compression='lzf')
g_train4 = agglo.Rag(ws_train, p4_train, feature_manager=fc)
np.random.RandomState(0)
(X4, y4, w4, merges4) = map(np.copy, map(np.ascontiguousarray,
                            g_train4.learn_agglomerate(gt_train, fc)[0]))
print X4.shape
np.savez('example-data/train-set4.npz', X=X4, y=y4)
y4 = y4[:, 0]
rf4 = classify.DefaultRandomForest()
np.random.RandomState(0)
rf4 = rf4.fit(X4, y4)
classify.save_classifier(rf4, 'example-data/rf-4.joblib')
learned_policy4 = agglo.classifier_probability(fc, rf4)
g_test4 = agglo.Rag(ws_test, p4_test, learned_policy4, feature_manager=fc)
g_test4.agglomerate(0.5)
seg_test4 = g_test4.get_segmentation()
imio.write_h5_stack(seg_test4, 'example-data/test-seg4.lzf.h5', compression='lzf')

results = np.vstack((
    ev.split_vi(ws_test, gt_test),
    ev.split_vi(seg_test1, gt_test),
    ev.split_vi(seg_test4, gt_test)
    ))

np.save('example-data/vi-results.npy', results)
github janelia-flyem / gala / tests / example-data / example.py View on Github external
# create a feature manager
fm = features.moments.Manager()
fh = features.histogram.Manager()
fc = features.base.Composite(children=[fm, fh])

# create graph and obtain a training dataset
g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc)
(X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc)[0]
y = y[:, 0] # gala has 3 truth labeling schemes, pick the first one
print((X.shape, y.shape)) # standard scikit-learn input format

# train a classifier, scikit-learn syntax
rf = classify.DefaultRandomForest().fit(X, y)
# a policy is the composition of a feature map and a classifier
learned_policy = agglo.classifier_probability(fc, rf)

# get the test data and make a RAG with the trained policy
pr_test, ws_test = (map(imio.read_h5_stack,
                        ['test-p1.lzf.h5', 'test-ws.lzf.h5']))
g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
g_test.agglomerate(0.5) # best expected segmentation
seg_test1 = g_test.get_segmentation()

# the same approach works with a multi-channel probability map
p4_train = imio.read_h5_stack('train-p4.lzf.h5')
# note: the feature manager works transparently with multiple channels!
g_train4 = agglo.Rag(ws_train, p4_train, feature_manager=fc)
(X4, y4, w4, merges4) = g_train4.learn_agglomerate(gt_train, fc)[0]
y4 = y4[:, 0]
print((X4.shape, y4.shape))
rf4 = classify.DefaultRandomForest().fit(X4, y4)
github janelia-flyem / gala / tests / test_gala.py View on Github external
def test_segment_with_classifer_1_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-1.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf1-py3.joblib')
        with tar_extract(fn) as fn:
            rf = joblib.load(fn)
    learned_policy = agglo.classifier_probability(fc, rf)
    g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    #imio.write_h5_stack(seg_test, 'example-data/test-seg-1.lzf.h5')
    seg_expected = imio.read_h5_stack(
        os.path.join(rundir, 'example-data/test-seg-1.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)
github janelia-flyem / gala / gala / test_package.py View on Github external
def testAggloRFBuild(self):
        from gala import agglo
        from gala import features
        from gala import classify
        self.datadir = os.path.abspath(os.path.dirname(sys.modules["gala"].__file__)) + "/testdata/"

        cl = classify.load_classifier(self.datadir + "agglomclassifier.rf.h5")
        fm_info = json.loads(str(cl.feature_description))
        fm = features.io.create_fm(fm_info)
        mpf = agglo.classifier_probability(fm, cl)

        watershed, dummy, prediction = self.gen_watershed()
        stack = agglo.Rag(watershed, prediction, mpf, feature_manager=fm, nozeros=True)
        self.assertEqual(stack.number_of_nodes(), 3630)
        stack.agglomerate(0.1)
        self.assertEqual(stack.number_of_nodes(), 88)
        stack.remove_inclusions()
        self.assertEqual(stack.number_of_nodes(), 86)
github janelia-flyem / gala / gala / serve.py View on Github external
def relearn(self):
        """Learn a new merge policy using data gathered so far.

        This resets the state of the RAG to contain only the merges and
        separations received over the course of its history.
        """
        clf = classify.DefaultRandomForest().fit(self.features, self.targets)
        self.policy = agglo.classifier_probability(self.feature_manager, clf)
        self.rag = self.original_rag.copy()
        self.rag.merge_priority_function = self.policy
        self.rag.rebuild_merge_queue()
        for i, (s0, s1) in enumerate(self.separate):
            self.rag.node[s0]['exclusions'].add(i)
            self.rag.node[s1]['exclusions'].add(i)
github janelia-flyem / gala / benchmarks / bench_gala.py View on Github external
memory['feature caches'] = asizeof(g) - memory['base RAG']
    with timer() as t_flat:
        _ignore = g.learn_flat(gttr, em)
    times['learn flat'] = t_flat[0]
    with timer() as t_gala:
        (X, y, w, e), allepochs = g.learn_agglomerate(gttr, em,
                                                      min_num_epochs=5)
        y = y[:, 0]  # ignore rand-sign and vi-sign schemes
    memory['training data'] = asizeof((X, y, w, e))
    times['learn agglo'] = t_gala[0]
    with timer() as t_train_classifier:
        cl = classify.DefaultRandomForest()
        cl.fit(X, y)
    times['classifier training'] = t_train_classifier[0]
    memory['classifier training'] = asizeof(cl)
    policy = agglo.classifier_probability(em, cl)
    wsts, prts, gtts = tsdata()
    gtest = agglo.Rag(wsts, prts, merge_priority_function=policy,
                      feature_manager=em)
    with timer() as t_segment:
        gtest.agglomerate(np.inf)
    times['segment test volume'] = t_segment[0]
    memory['segment test volume'] = asizeof(gtest)
    return times, memory
github janelia-flyem / gala / gala / segmentation_pipeline.py View on Github external
if fm_info is None or fm_info["neuroproof_features"] is None:
            raise Exception("agglomeration classifier to old to be used") 
        if options.use_neuroproof:
            if not fm_info["neuroproof_features"]:
                raise Exception("random forest created not using neuroproof") 
            agglom_stack = stack_np.Stack(supervoxels, prediction,
                single_channel=False, classifier=cl, feature_info=fm_info, 
                synapse_file=options.synapse_file, master_logger=master_logger) 
        else:
            if fm_info["neuroproof_features"]:
                master_logger.warning("random forest created using neuroproof features -- should still work") 
            fm = features.io.create_fm(fm_info)
            if options.expected_vi:
                mpf = agglo.expected_change_vi(fm, cl, beta=options.vi_beta)
            else:
                mpf = agglo.classifier_probability(fm, cl)
            
            agglom_stack = agglo.Rag(supervoxels, prediction, mpf,
                    feature_manager=fm, show_progress=True, nozeros=True, 
                    exclusions=synapse_volume)
        master_logger.info("Finished building RAG")
    else:
        master_logger.info("Building RAG")
        boundary = grab_boundary(prediction, options.bound_channels, master_logger)   
        agglom_stack = agglo.Rag(supervoxels, boundary, 
                merge_priority_function=agglo.boundary_median,
                show_progress=True, nozeros=True, exclusions=synapse_volume)
        master_logger.info("Finished building RAG")


    # remove inclusions 
    if options.inclusion_removal: