Skip to content

Commit

Permalink
exposes agglomeration routine to python
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Plaza committed Nov 20, 2015
1 parent e87c855 commit dc3c5c2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
25 changes: 25 additions & 0 deletions integration_tests/testpyagglom.py
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 11 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)

56 changes: 54 additions & 2 deletions python/src/Agglomeration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,66 @@

#include "converters.hpp"


#include <boost/algorithm/string/predicate.hpp>
#include <Utilities/ScopeTime.h>
#include <Classifier/vigraRFclassifier.h>
#include <Classifier/opencvRFclassifier.h>

#include <FeatureManager/FeatureMgr.h>
#include <BioPriors/BioStack.h>
#include <BioPriors/StackAgglomAlgs.h>

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<VolumeProbPtr> 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<VolumeProbPtr> 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)
Expand Down

0 comments on commit dc3c5c2

Please sign in to comment.