def delete_internal_leaves_jsome2jall(jall_name, jsome_name): #disconnect vertices from g that are disconnect in h try: g, attributes = graph_analysis.input_graph( jall_name, ["history", "vertex_offsets"]) print("Successfully read in the %s\n" % jall_name) except: print("Error reading in the %s\n" % jall_name) h = graph_analysis.input_graph(jsome_name) if len(g.vertices) != len(h.vertices): print "ERROR: delete_internal_leaves_jsome2jall, the number of vertices of g and h are not equal!!\nAborted!" exit(0) for i in xrange(len(g.vertices)): if len(h.vertices[i].edges) == 0: g.disconnect_vertex(i) out_name = jall_name[:-3] + "_delleave.db" history = '\n>>> %s: delete_internal_leaves on join_all using the join_some result' % ( ctime(time())) ##argv = ['code.py', 'input.db', 'output'] if attributes.has_key("history"): history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(out_name, g, history, attributes) print("Succefully wrote the %s\n" % out_name) return out_name
def retain_loops(g, output_file): #loop is defined: if an edge is broken and the connected graph breaks into 2 components, that edge is not part of a loop while graph_analysis.prune_leaves(g) > 0: pass N = number_of_connected_components(g) edge2remove = [] #edge not part of a loop ne = 0 h = copy.deepcopy(g) for e in g.edge_list(): properties = g.edge_properties(e) h.remove_edge(e) if number_of_connected_components(h) > N: edge2remove.append(e) if number_of_connected_components(h) != N: ne += 1 h.add_edge(e, properties) print ne, " ", len(edge2remove) h = copy.deepcopy(g) for e in edge2remove: h.remove_edge(e) graph_analysis.output_graph(output_file, h) return h
ls = [] if options.labels: ls = options.labels.split(',') ls = [int(l) for l in ls] g, attributes = graph_analysis.input_graph(input_file, ["history", "vertex_offsets"]) occlusions, encompassing_e = detect_internal_segments( g, ls, options.scale, options.offset) for e in occlusions.keys(): if e not in encompassing_e: #this way only edges that have no other edge inside them will be deleted g.remove_edge(e) #NOTE: what we should actually do is to check if all the vertices of an edge are inside another edge => remove that edge # if not, then only disconnect_vertex of those inside, then reconnect the rest of the vertices to the encompassing_e if attributes.has_key("history"): history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(output_file, g, history, attributes) cmd = ("graph2cylinder.py --use_label %s" % (output_file)) os.system(cmd) cmd = ("graph2obj.py %s %s %s " % (output_file, output_file[:-3] + "obj", options.clobber)) os.system(cmd)
e, 'cyl_label', int(np.mean(label_vals[l])) ) #"No label \0" #add label dataset to edge properties and initialize it to None with null-terminted character '\0' ref_graph.set_edge_property( e, 'estimated_label', [l, np.mean(label_vals[l])] ) #"No label \0" #add label dataset to edge properties and initialize it to None with null-terminted character '\0' else: ref_graph.set_edge_property(e, 'cyl_label', int(0)) ref_graph.set_edge_property( e, 'estimated_label', [l, 0] ) #"No label \0" #add label dataset to edge properties and initialize it to None with null-terminted character '\0' history = '>>> %s: %s' % (time.ctime(time.time()), string.join(argv) ) ##argv = ['code.py', 'input.db', 'output'] if attributes.has_key("history"): history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(output_file[:-2] + "db", ref_graph, history, attributes) #graph_analysis.output_graph("simplifiedgraph.db", h, history, attributes) print("Succefully wrote the %s\n" % output_file[:-2] + "db") ## save the final cylinder file into h5 file for the Brainview #output_final=output_file.replace(".db", ".h5") cmd = ("graph2graph.py %s %s --clobber " % (output_file[:-2] + "db", output_file[:-2] + "h5") ) #python /micehome/jgsled/bin/ print(cmd) os.system(cmd)
print len(g.edge_list()), " ", len(g.vertices) sys.stdout.flush() #for e in g.edge_list(): #print e, " ",g.edge_property(e, 'intermediaries') #print " " this_estimate_edge_diameters(g) this_estimate_edge_lengths(g) history = '\n>>> %s: %s' % (time.ctime( time.time()), string.join(argv) + "\nmodified_simplify_graph" ) ##argv = ['code.py', 'input.db', 'output'] if attributes.has_key("history"): history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(output_file[:-3] + "_nofeature_simplified.db", g, history, attributes) print("Succefully wrote the %s\n" % output_file[:-3] + "_nofeature_simplified.db") sys.stdout.flush() #output_file = output_file[:-3]+"_simp.db" #redo feature extraction cmnd = ( "python /projects/souris/sghanavati/src/scripts/cerebrovascular_analysis/feature_extraction.py %s %s %s %s %s" % (output_file[:-3] + "_nofeature_simplified.db", mr_atlas_file, mr_centroids_file, reference_file, output_file[:-3] + "_featured.db") ) #--new_edges print "\n", cmnd, "\n" sys.stdout.flush()
# read in the labeled CT : used to make labling training dataset #labeled_ct=py_minc.ArrayVolume(labeled_CT_file,dim_names=py_minc.FILE_ORDER_DIMENSION_NAMES) #which is in [x,y,z] labeled_ct = py_minc.ArrayVolume( labeled_CT_file ) # without this always [z,y,x], with this :dim_names=py_minc.FILE_ORDER_DIMENSION_NAMES order as the image # g = edge_labeling(g, labeled_ct) #history = history + '\n>>> %s: %s' % (time.ctime(time.time()), "edge_labeling") ##argv = ['code.py', 'input.db', 'output'] history = '\n>>> %s: %s' % (time.ctime(time.time()), string.join(argv) ) ##argv = ['code.py', 'input.db', 'output'] if attributes.has_key("history"): history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(output_file, g, history, attributes) print("Succefully wrote the %s\n" % output_file) g = label_propagate(g, 0) #history = history + '\n>>> %s: %s' % (time.ctime(time.time()), "edge_labeling") ##argv = ['code.py', 'input.db', 'output'] history += '\n>>> %s: %s' % (time.ctime(time.time()), "label_propagate" ) ##argv = ['code.py', 'input.db', 'output'] if attributes.has_key("history"): #history = attributes["history"] + "\n" + history del attributes['history'] graph_analysis.output_graph(output_file[:-3] + "_propagate.db", g, history, attributes) print("Succefully wrote the %s\n" % output_file[:-3] + "_propagate.db") ##### ###if not os.path.exists(input_file[:-3]+"_dt.mnc"):
#h = copy.deepcopy(ref) keep_edges = [] for input_file in input_files: print ">>> ", input_file if os.path.exists(input_file): g, attributes = graph_analysis.input_graph( input_file, ["history", "vertex_offsets"]) for e in g.edge_list(): keep_edges.append(e) for e in ref.edge_list(): if e not in keep_edges: if ref.edge_property(e, 'cyl_label') not in [9, 200 ]: #don't remove pcommAs ref.remove_edge(e) if ref_attributes.has_key("history"): #history = attributes["history"] + "\n>>>Main labels retained." + "\n" + history history = "\n>>>Anatomical length calculations." + "\n" + ref_attributes[ "history"] del ref_attributes['history'] graph_analysis.output_graph(options.output_name, ref, history, ref_attributes) cmd = ("graph2cylinder.py --clobber --use_label %s" % (options.output_name) ) #python /micehome/jgsled/bin/ os.system(cmd)
for j in range(len(e_radius)): e_volume += cyl_volume_calculation(e_radius[j], e_height[j]) total_volume += e_volume #if int(ref.edge_property(e,'label')) != int(g.edge_property(e,'estimated_label')): if int(ref.edge_property(e, 'cyl_label')) != int( g.edge_property(e, 'cyl_label')): h.set_edge_property(e, 'error_label', 2) #error labelling error_volume += e_volume error_num += 1 else: h.set_edge_property(e, 'error_label', 1) #correct labelling else: h.set_edge_property( e, 'error_label', 2) #error labelling (new edge in g that does not exist in ref graph_analysis.output_graph(output_file, h) cmd = ( "\npython /projects/souris/sghanavati/src/scripts/cerebrovascular_analysis/graph2cylinder.py %s %s --use_error_label --clobber " % (output_file, output_file[:-3] + "_error_cyl.h5") ) #python /micehome/jgsled/bin/ print(cmd) os.system(cmd) if (labeled_num > 0) and (total_volume > 0): #otherwise test set was not labeled print( "Out of %d edges that were labeled, %d were labelled correctly with second round of manual labelling. The error is %f%% (volumetric error %f%%). Recognition Rate is %f%% (volumetric RR %f%%)." % (labeled_num, labeled_num - error_num, 100 * float(error_num) / float(labeled_num), 100 * float(error_volume) / float(total_volume),