Script to Convert Focused Debug Output To Merge Coordinates

Pipeline Before Running This Script

  • Received from Ting a list of bodies that were unexamined and candidate bodies that connect to them
  • Extracted a subset of these candidate edges (limited to those that would not result in future tracing)
  • Proofreaders performed focused debug protocol on these candidate edges producing a sessionpriority json files encoding 0/1 decisions
  • Proofreaders also generated bookmarks for locations that will need to be manually examined
  • Coordinate system corresponds to original 13 layer stitched volume

What this script does

  • Takes new coordinate system
  • Takes bookmark glob list and outputs one bookmark file in new coordinate system
  • Takes session json glob list and outputs one bookmark file in new coordinate system showing merge points
  • Uses Ting's original unexamined list to determine the number of recovered annotations and the optimal body locations for merge
All input/output files are located at: /groups/flyem/data/medulla-FIB-Z1211-25-production/align2/base_stacks/shinya1-13_20140516

In [1]:
prefix="/groups/flyem/data/medulla-FIB-Z1211-25-production/align2/base_stacks/shinya1-13_20140516"

In [2]:
def process_focused_output(xshift, yshift, session_files, bookmark_files, original_orphan_file, result_directory):
    import glob
    import json
    import os
    
    # create bookmark output directory and file
    bookmark_list = glob.glob(bookmark_files)
    try:
        os.makedirs(result_directory)
    except OSError:
        pass
    bookmark_output = open(result_directory + "/bookmarks.json", 'w')
    bookmarks = []
    
    num_bookmarks = 0
    
    # read and accumulate bookmarks
    for bookmark_file in bookmark_list:
        data = json.load(open(bookmark_file))
        for bookmark in data["data"]:
            bookmark["location"][0] += xshift
            bookmark["location"][1] += yshift
            bookmarks.append(bookmark)
            num_bookmarks += 1
    
    # write bookmark file
    bookmark_data = {}
    bookmark_data["data"] = bookmarks
    bookmark_data["metadata"] = {"file version": 1, "description": "bookmarks"}
    bookmark_output.write(json.dumps(bookmark_data, indent=4))
    bookmark_output.close()

    # retrieve all body locations
    orphan_data = json.load(open(original_orphan_file))
    
    body_locations = {}
    unexamined_bodies = set()
    num_synapses = {}
    for body in orphan_data["Bodies"]:
        body_locations[body["id"]] = body["marker"]
        if not body["cross_substack"] and body["size"] < 100000:
            unexamined_bodies.add(body["id"])
            num_synapses[body["id"]] = body["num_synapses"]
    
    # create accumulation file
    merge_output = open(result_directory + "/pointstolink.json", 'w')
    
    num_merges = 0
    num_total = 0
    
    bodylink_list = []
    
    # accumulate all merges
    total_synapses_fixed = 0
    session_list = glob.glob(session_files)
    for session_file in session_list:
        data = json.load(open(session_file))
        for edge in data["edge_list"]:
            num_total += 1
            if edge["weight"] == 0.0:
                num_merges += 1
                point_pair = []
                
                # copy points out and then shift so we don't shift stored data
                #   via references:
                x1, y1, z1 = body_locations[edge["node1"]]
                x2, y2, z2 = body_locations[edge["node2"]]
                x1 += xshift
                x2 += xshift
                y1 += yshift
                y2 += yshift
                             
                
                # make sure unexamined node is second
                if edge["node1"] in unexamined_bodies:
                    point_pair.append([x2, y2, z2])
                    point_pair.append([x1, y1, z1])
                    total_synapses_fixed += num_synapses[edge["node1"]]
                else:
                    if edge["node2"] not in unexamined_bodies:
                        print "Node must not be examined to go first"
                    point_pair.append([x1, y1, z1])
                    point_pair.append([x2, y2, z2])
                    total_synapses_fixed += num_synapses[edge["node2"]]
                    
                bodylink_list.append(point_pair)
    
    bodylink_data = {}
    bodylink_data["data"] = bodylink_list
    bodylink_data["metadata"] = {"file version": 1, "description": "link-save export"}
    merge_output.write(json.dumps(bodylink_data, indent=4))
    merge_output.close()
    
    print "Number of bookmarks: ", num_bookmarks
    print "Number of total edges: ", num_total
    print "Number of merged edges: ", num_merges
    print "Number of synapse annotations added to examined volume: ", total_synapses_fixed
Run command with input files from the default prefix, output to focused-debug-results

In [3]:
# call function with x offset 1878 and y offset 2364
process_focused_output(1878, 2364, prefix + "/focused-debug-output/sessionpriority_debug_orphan/*.json",
                       prefix + "/focused-debug-output/bookmarks_debug_orphan/*.json",
                       prefix + "/focused-debug/input/input.json", prefix + "/focused-debug-results")


Number of bookmarks:  227
Number of total edges:  8888
Number of merged edges:  4644
Number of synapse annotations added to examined volume:  7157