From dc3c5c28d6e26f8b5c47c3f1e54271b5e9679643 Mon Sep 17 00:00:00 2001 From: Stephen Plaza Date: Fri, 20 Nov 2015 02:26:31 -0500 Subject: [PATCH] exposes agglomeration routine to python --- integration_tests/testpyagglom.py | 25 ++++++++++++++ python/CMakeLists.txt | 12 ++++++- python/src/Agglomeration.cpp | 56 +++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 integration_tests/testpyagglom.py diff --git a/integration_tests/testpyagglom.py b/integration_tests/testpyagglom.py new file mode 100644 index 0000000..f49a2f6 --- /dev/null +++ b/integration_tests/testpyagglom.py @@ -0,0 +1,25 @@ +import sys +import h5py +import numpy + +segh5 = sys.argv[1] +predh5 = sys.argv[2] +classifier = sys.argv[3] +threshold = float(sys.argv[4]) + +from neuroproof import Agglomeration + +# open as uint32 and float respectively +seg = numpy.array(h5py.File(segh5)['stack'], numpy.uint32) +pred = numpy.array(h5py.File(predh5)['volume/predictions'], numpy.float32) + +pred = pred.transpose((2,1,0,3)) +pred = pred.copy() + +res = Agglomeration.agglomerate(seg, pred, classifier, threshold) + +# should be 232 unique labels (including 0) in the resulting segmentation +if len(numpy.unique(res)) != 232: + exit(1) + +print "SUCCESS" diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 50e048f..660bb86 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -166,7 +166,7 @@ add_library( _agglomeration_python SHARED src/Agglomeration.cpp) target_link_libraries( _classifier_python Classifier ${NEUROPROOF_EXT_LIBS} ${boostpython_LIB} ${PYTHON_LIBRARIES} ) set_target_properties( _classifier_python PROPERTIES PREFIX "" DEBUG_POSTFIX "" ) -target_link_libraries( _agglomeration_python Stack ${NEUROPROOF_EXT_LIBS} ${boostpython_LIB} ${PYTHON_LIBRARIES} ) +target_link_libraries( _agglomeration_python ${NEUROPROOF_INT_LIBS} ${NEUROPROOF_EXT_LIBS} ${boostpython_LIB} ${PYTHON_LIBRARIES} ) set_target_properties( _agglomeration_python PROPERTIES PREFIX "") if (APPLE) @@ -194,3 +194,13 @@ install(FILES neuroproof/Agglomeration/__init__.py DESTINATION ${LIBDVID_PYTHON_INSTALL_DIR}/neuroproof/Agglomeration) enable_testing() + +add_test("test_agglom_python" + ${PYTHON_EXE} + ${CMAKE_SOURCE_DIR}/integration_tests/testpyagglom.py + ${CMAKE_SOURCE_DIR}/integration_tests/inputs/samp1_labels.h5 + ${CMAKE_SOURCE_DIR}/integration_tests/inputs/samp1_prediction.h5 + ${CMAKE_SOURCE_DIR}/integration_tests/inputs/250-1_agglo_itr1_trial1_opencv_rf_tr255.xml + 0.2 +) + diff --git a/python/src/Agglomeration.cpp b/python/src/Agglomeration.cpp index f16b29b..7c074e8 100644 --- a/python/src/Agglomeration.cpp +++ b/python/src/Agglomeration.cpp @@ -9,14 +9,66 @@ #include "converters.hpp" + +#include +#include +#include +#include + +#include +#include +#include + using namespace boost::python; +using namespace boost::algorithm; using std::vector; +using std::cout; using std::endl; namespace NeuroProof { namespace python { -VolumeLabelPtr agglomerate(VolumeLabelPtr labels, vector prob_array) +// ?! build classifier in function for now -- use Classifier object in the future +// Note: always assumes mito is third channel +// TODO: allow segmentation constraints (e.g., synapses) +VolumeLabelPtr agglomerate(VolumeLabelPtr labels, + vector prob_array, std::string fn, double threshold) { - return labels; + ScopeTime timer; + // create classifier from file + EdgeClassifier* eclfr; + if (ends_with(fn, ".h5")) { + eclfr = new VigraRFclassifier(fn.c_str()); + } else if (ends_with(fn, ".xml")) { + eclfr = new OpencvRFclassifier(fn.c_str()); + } + + // create feature manager and load classifier + FeatureMgrPtr feature_manager(new FeatureMgr(prob_array.size())); + feature_manager->set_basic_features(); + feature_manager->set_classifier(eclfr); + + // create stack to hold segmentation state + BioStack stack(labels); + stack.set_feature_manager(feature_manager); + stack.set_prob_list(prob_array); + + // build graph + stack.build_rag(); + cout<< "Initial number of regions: "<< stack.get_num_labels()<< endl; + + // remove inclusions + stack.remove_inclusions(); + cout<< "Regions after inclusion removal: "<< stack.get_num_labels()<< endl; + + // perform agglomeration + agglomerate_stack(stack, threshold, true); + cout<< "Regions after agglomeration: "<< stack.get_num_labels()<< endl; + stack.remove_inclusions(); + cout<< "Regions after inclusion removal: "<< stack.get_num_labels()<< endl; + + agglomerate_stack_mito(stack); + cout<< "Regions after mito agglomeration: "<< stack.get_num_labels()<< endl; + + return stack.get_labelvol(); } BOOST_PYTHON_MODULE(_agglomeration_python)