Skip to content

Commit

Permalink
bug: add irreducibles to graph
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-grim committed Jan 16, 2024
1 parent 7d9f21a commit ad1e143
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/deep_neurographs/intake.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ def build_neurograph_from_local(
smooth=SMOOTH,
):
# Process swc files
t0 = time()
assert swc_dir or swc_paths, "Provide swc_dir or swc_paths!"
bbox = utils.get_bbox(img_patch_origin, img_patch_shape)
paths = get_paths(swc_dir) if swc_dir else swc_paths
t0 = time()
swc_dicts = process_local_paths(paths, min_size, bbox=bbox)

# Build neurograph
Expand Down Expand Up @@ -243,19 +241,21 @@ def build_neurograph(
print("# nodes:", utils.reformat_number(n_nodes))
print("# edges:", utils.reformat_number(n_edges))
neurograph = NeuroGraph(bbox=bbox, img_path=img_path, swc_paths=swc_paths)
start_ids = get_start_ids(swc_dicts)
t0, t1 = utils.init_timers()
for key in swc_dicts.keys():
n_components = len(irreducibles.keys())
chunk_size = int(n_components) * 0.05
cnt = 1
for i, key in enumerate(irreducibles.keys()):
neurograph.add_immutables(
irreducibles[key], key
)
del swc_dicts[key]
if i > cnt * chunk_size:
cnt, t1 = report_progress(i, len(swc_dicts), chunk_size, cnt, t0, t1)
cnt, t1 = report_progress(i, n_components, chunk_size, cnt, t0, t1)
print(f"add_irreducibles(): {time() - t0} seconds")

"""
t0 = time()
start_ids = get_start_ids(swc_dicts)
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(
Expand Down
48 changes: 26 additions & 22 deletions src/deep_neurographs/neurograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,28 @@ def init_densegraph(self):

# --- Add nodes or edges ---
def add_immutables(self, irreducibles, swc_id, start_id=None):
# Initializations
node_id = dict()
# Nodes
node_ids = dict()
cur_id = start_id if start_id else len(self.nodes)
leaf_ids = irreducibles["leafs"].keys()
junction_ids = irreducibles["junctions"].keys()
for i in leaf_ids + junction_ids:
node_id[i] = cur_id
self.add_node(
node_id[i],
xyz=irreducibles[i]["xyz"],
radius=irreducibles[i]["radius"],
swc_id=swc_id,
)
cur_id += 1

# Update leafs and junctions
for leaf in irreducibles["leafs"].keys():
self.leafs.add(node_id[leaf])

for junction in irreducibles["junctions"].keys():
self.junctions.add(node_id[junction])
node_ids, cur_id = self.__add_nodes(
irreducibles, "leafs", node_ids, cur_id, swc_id
)
node_ids, cur_id = self.__add_nodes(
irreducibles, "junctions", node_ids, cur_id, swc_id
)

# Add edges
edges = irreducibles["edges"]
for i, j in edges.keys():
# Get edge
edge = (node_id[i], node_id[j])
edge = (node_ids[i], node_ids[j])
xyz = np.array(edges[(i, j)]["xyz"])
radii = np.array(edges[(i, j)]["radius"])

# Add edge
self.immutable_edges.add(frozenset(edge))
self.add_edge(
node_id[i], node_id[j], xyz=xyz, radius=radii, swc_id=swc_id
node_ids[i], node_ids[j], xyz=xyz, radius=radii, swc_id=swc_id
)
xyz_to_edge = dict((tuple(xyz), edge) for xyz in xyz)
check_xyz = set(xyz_to_edge.keys())
Expand All @@ -125,6 +113,22 @@ def add_immutables(self, irreducibles, swc_id, start_id=None):
for xyz in collisions:
del xyz_to_edge[xyz]
self.xyz_to_edge.update(xyz_to_edge)

def __add_nodes(self, nodes, key, node_ids, cur_id, swc_id):
for i in nodes[key].keys():
node_ids[i] = cur_id
self.add_node(
node_ids[i],
xyz=nodes[key][i]["xyz"],
radius=nodes[key][i]["radius"],
swc_id=swc_id,
)
if key == "leafs":
self.leafs.add(cur_id)
else:
self.junctions.add(cur_id)
cur_id += 1
return node_ids, cur_id

# --- Proposal Generation ---
def generate_proposals(
Expand Down

0 comments on commit ad1e143

Please sign in to comment.