示例#1
0
def test_server_with_id_service(dummy_data):
    frag, gt, fman = dummy_data
    id_service_port = 5600
    config = {'client_url': 'tcp://*:5590',
              'id_service_url': 'tcp://localhost:%i' % id_service_port,
              'solver_url': 'tcp://localhost:5590'}
    with temporary_file('.json') as config_filename:
        with open(config_filename, 'w') as fout:
            json.dump(config, fout)
        solver = serve.Solver(frag, feature_manager=fman,
                              config_file=config_filename)
    starting_id = 23461
    id_thread = threading.Thread(target=id_serve, name='id-service',
                                 daemon=True,
                                 kwargs=dict(port=id_service_port,
                                             curr_id=starting_id))
    id_thread.start()
    thread = threading.Thread(target=solver.listen, name='solver')
    thread.start()
    host, port = config['solver_url'].rsplit(':', maxsplit=1)
    _, dst = serve.proofread(frag, gt, host=host, port=int(port),
                             num_operations=2, stop_when_finished=True,
                             random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) <
            ev.vi(frag, gt, ignore_x=[], ignore_y=[]))
    # test 2: make sure ID service worked: starting ID should be as above
    # should be equal but boundary ID messes things up
    assert np.min(result) > starting_id
    thread.join()
示例#2
0
def test_server_long(data):
    frag, gt, pr = data
    host, port = 'tcp://localhost', 5590
    solver = serve.Solver(frag, pr, port=port, host='tcp://*')
    thread = threading.Thread(target=solver.listen, name='solver')
    thread.start()
    _, dst = serve.proofread(frag, gt, host=host, port=port,
                             stop_when_finished=True, random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) <
            ev.vi(frag, gt, ignore_x=[], ignore_y=[]))
    thread.join()
示例#3
0
def test_server_imperfect_fragments(dummy_data2):
    frag, gt, fman = dummy_data2
    host, port = 'tcp://localhost', 5589
    solver = serve.Solver(frag, feature_manager=fman,
                          address='tcp://*:' + str(port))
    thread = threading.Thread(target=solver.listen, name='solver')
    thread.start()
    _, dst = serve.proofread(frag, gt, host=host, port=port, num_operations=2,
                             stop_when_finished=True, random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) <
            ev.vi(frag, gt, ignore_x=[], ignore_y=[]))
    thread.join()
示例#4
0
def test_no_dam_agglomeration():
    i = 3
    g = agglo.Rag(wss[i], probs[i], agglo.boundary_mean,
        normalize_probabilities=True)
    g.agglomerate(0.75)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='No dam agglomeration failed.')
示例#5
0
def test_no_dam_agglomeration():
    i = 3
    g = agglo.Rag(wss[i], probs[i], agglo.boundary_mean, 
        normalize_probabilities=True)
    g.agglomerate(0.75)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='No dam agglomeration failed.')
示例#6
0
def test_ladder_agglomeration():
    i = 2
    g = agglo.Rag(wss[i], probs[i], agglo.boundary_mean,
        normalize_probabilities=True)
    g.agglomerate_ladder(3)
    g.agglomerate(0.51)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='Ladder agglomeration failed.')
示例#7
0
def test_server_long(data):
    frag, gt, pr = data
    host, port = 'tcp://localhost', 5590
    solver = serve.Solver(frag, pr, port=port, host='tcp://*')
    thread = threading.Thread(target=solver.listen, name='solver')
    thread.start()
    _, dst = serve.proofread(frag,
                             gt,
                             host=host,
                             port=port,
                             stop_when_finished=True,
                             random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) < ev.vi(
        frag, gt, ignore_x=[], ignore_y=[]))
    thread.join()
示例#8
0
def test_ladder_agglomeration():
    i = 2
    g = agglo.Rag(wss[i], probs[i], agglo.boundary_mean,
        normalize_probabilities=True)
    g.agglomerate_ladder(3)
    g.agglomerate(0.51)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='Ladder agglomeration failed.')
示例#9
0
def test_server_with_periodic_send(dummy_data):
    frag, gt, fman = dummy_data
    id_service_port = 5601
    config = {
        'client_url': 'tcp://*:5591',
        'id_service_url': 'tcp://localhost:%i' % id_service_port,
        'solver_url': 'tcp://localhost:5591'
    }
    with temporary_file('.json') as config_filename:
        with open(config_filename, 'w') as fout:
            json.dump(config, fout)
        solver = serve.Solver(frag,
                              feature_manager=fman,
                              config_file=config_filename)
    starting_id = 23461
    id_thread = threading.Thread(target=id_serve,
                                 name='id-service',
                                 daemon=True,
                                 kwargs=dict(port=id_service_port,
                                             curr_id=starting_id))
    id_thread.start()
    thread = threading.Thread(target=solver.listen,
                              name='solver',
                              daemon=True,
                              kwargs=dict(send_every=10))
    thread.start()
    host, port = config['solver_url'].rsplit(':', maxsplit=1)
    _, dst = serve.proofread(frag,
                             gt,
                             host=host,
                             port=int(port),
                             num_operations=2,
                             stop_when_finished=True,
                             request_seg=False,
                             random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) < ev.vi(
        frag, gt, ignore_x=[], ignore_y=[]))
    # test 2: make sure ID service worked: starting ID should be as above
    # should be equal but boundary ID messes things up
    assert np.min(result) > starting_id
示例#10
0
def test_server_imperfect_fragments(dummy_data2):
    frag, gt, fman = dummy_data2
    host, port = 'tcp://localhost', 5589
    solver = serve.Solver(frag,
                          feature_manager=fman,
                          address='tcp://*:' + str(port))
    thread = threading.Thread(target=solver.listen, name='solver')
    thread.start()
    _, dst = serve.proofread(frag,
                             gt,
                             host=host,
                             port=port,
                             num_operations=2,
                             stop_when_finished=True,
                             random_state=0)
    result = np.array(dst)[frag]
    # test: resulting segmentation should be improvement over fragments alone
    assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) < ev.vi(
        frag, gt, ignore_x=[], ignore_y=[]))
    thread.join()
示例#11
0
def test_segment_with_gala_classifer(dummy_data):
    frag, gt, g, fman = dummy_data
    np.random.seed(5)
    summary, allepochs = g.learn_agglomerate(gt, fman,
                                             learning_mode='strict',
                                             classifier='logistic regression')
    feat, target, weights, edges = summary
    ffeat, ftarget, fweights, fedges = allepochs[0]  # flat
    lr = LR().fit(feat, target[:, 0])
    gala_policy = agglo.classifier_probability(fman, lr)
    flr = LR().fit(ffeat, ftarget[:, 0])
    flat_policy = agglo.classifier_probability(fman, flr)

    gtest = agglo.Rag(frag, feature_manager=fman,
                      merge_priority_function=gala_policy)
    gtest.agglomerate(0.5)
    assert ev.vi(gtest.get_segmentation(), gt) == 0
    gtest_flat = agglo.Rag(frag, feature_manager=fman,
                           merge_priority_function=flat_policy)
    assert ev.vi(gtest_flat.get_segmentation(0.5), gt) == 1.5
示例#12
0
def test_segment_with_classifier_4_channel():
    fn = os.path.join(rundir, 'example-data/rf4-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, p4_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    seg_expected = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg-4.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)
示例#13
0
def test_segment_with_gala_classifer(dummy_data_fast):
    frag, gt, g, fman = dummy_data_fast
    np.random.seed(5)
    summary, allepochs = g.learn_agglomerate(gt, fman,
                                             learning_mode='strict',
                                             classifier='logistic regression',
                                             min_num_epochs=5)
    feat, target, weights, edges = summary
    ffeat, ftarget, fweights, fedges = allepochs[0]  # flat
    lr = LR().fit(feat, target[:, 0])
    gala_policy = agglo.classifier_probability(fman, lr)
    flr = LR().fit(ffeat, ftarget[:, 0])
    flat_policy = agglo.classifier_probability(fman, flr)

    gtest = agglo.Rag(frag, feature_manager=fman,
                      merge_priority_function=gala_policy)
    gtest.agglomerate(0.5)
    assert ev.vi(gtest.get_segmentation(), gt) == 0
    gtest_flat = agglo.Rag(frag, feature_manager=fman,
                           merge_priority_function=flat_policy)
    assert ev.vi(gtest_flat.get_segmentation(0.5), gt) == 1.5
示例#14
0
def test_mito():
    i = 5
    def frozen(g, i):
        "hardcoded frozen nodes representing mitochondria"
        return i in [3, 4]
    g = agglo.Rag(wss[i], probs[i], agglo.no_mito_merge(agglo.boundary_mean),
                  normalize_probabilities=True, isfrozennode=frozen)
    g.agglomerate(0.15)
    g.merge_priority_function = agglo.mito_merge()
    g.rebuild_merge_queue()
    g.agglomerate(1.0)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='Mito merge failed')
示例#15
0
def test_mito():
    i = 5
    def frozen(g, i):
        "hardcoded frozen nodes representing mitochondria"
        return i in [3, 4]
    g = agglo.Rag(wss[i], probs[i], agglo.no_mito_merge(agglo.boundary_mean),
                  normalize_probabilities=True, isfrozennode=frozen)
    g.agglomerate(0.15)
    g.merge_priority_function = agglo.mito_merge()
    g.rebuild_merge_queue()
    g.agglomerate(1.0)
    assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
                    err_msg='Mito merge failed')
示例#16
0
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)
示例#17
0
def test_segment_with_classifier_4_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-4.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf4-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, p4_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    seg_expected = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg-4.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)
示例#18
0
def test_mean_agglo_fast_rag(dummy_data):
    frag, gt, g = dummy_data
    g.agglomerate(0.5)
    assert ev.vi(g.get_segmentation(), gt) == 0
示例#19
0
def test_vi():
    seg = np.array([1, 2, 3, 4])
    gt = np.array([1, 1, 8, 8])
    assert_equal(ev.vi(seg, gt), 1)
示例#20
0
def test_mean_agglo_fast_rag(dummy_data):
    frag, gt, g = dummy_data
    g.agglomerate(0.5)
    assert ev.vi(g.get_segmentation(), gt) == 0
示例#21
0
def test_vi():
    seg = np.array([1, 2, 3, 4])
    gt = np.array([1, 1, 8, 8])
    assert_equal(ev.vi(seg, gt), 1)