Skip to content

Instantly share code, notes, and snippets.

@tferr
Created June 18, 2019 16:06
Show Gist options
  • Save tferr/d36d6129cd6052f378d145c990299e06 to your computer and use it in GitHub Desktop.
Save tferr/d36d6129cd6052f378d145c990299e06 to your computer and use it in GitHub Desktop.
# @LogService log
# @SNTService snt
# @UIService ui
"""
file: Autotrace_OP_1.py
"""
import time
from sc.fiji.snt import (Path, SNT, Tree)
from sc.fiji.snt.util import PointInImage
image = r"/home/tferr/code/OP_1/OP_1.tif"
gold_standard = r"/home/tferr/code/OP_1/OP_1-GS-NoRadius.swc"
traced_result = r"/home/tferr/code/OP_1/OP_1-autotrace.swc"
def run():
# Exit if SNT is already busy doing something
if snt.isActive() and snt.getUI():
ui.showDialog("Please close SNT before running this headless script", "Error")
return
# Prepare plugin for auto-tracing
plugin = snt.initialize(image, False) # image file path, wether ui should be displayed
plugin.enableAstar(True)
plugin.startHessian("primary", 1.15, 14)
tree = Tree(gold_standard)
#tree.scale(0.3296485, 0.3296485, 0.9988000) # uncomment ig GS swc is unscaled and image scaled
new_tree = Tree()
for path in tree.list():
end_point = path.getNode(path.size() - 1)
if path.getStartJoinsPoint() is None:
start_point = path.getNode(0)
primary_path = plugin.autoTrace(start_point, end_point, None)
new_tree.add(primary_path)
else:
# TODO: Simplify this. It is very confusing
source_tree_start_point = path.getStartJoinsPoint()
fork_path_id = path.getStartJoins().getID()
fork_path = new_tree.get(fork_path_id)
closest_index = fork_path.indexNearestTo(source_tree_start_point.x, source_tree_start_point.y,
source_tree_start_point.z, float('inf'))
fork_point = fork_path.getNode(closest_index)
child = plugin.autoTrace([fork_point, end_point], fork_point)
new_tree.add(child)
success = "File saved" if snt.save(traced_result) else "unsaved result. I/O error?"
log.info(success)
snt.dispose()
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment