Skip to content

Commit

Permalink
refactor: extract fixed image patch in features
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-grim committed Oct 7, 2024
1 parent 513d022 commit 7e0114f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/deep_neurographs/generate_proposals.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def run(
if trim_endpoints_bool:
radius /= RADIUS_SCALING_FACTOR
long_range, in_range = separate_proposals(neurograph, radius)
neurograph = run_trimming(neurograph, long_range, radius)
neurograph = run_trimming(neurograph, in_range, radius)
neurograph = run_trimming(neurograph, long_range, radius, progress_bar)
neurograph = run_trimming(neurograph, in_range, radius, progress_bar)


def init_kdtree(neurograph, complex_bool):
Expand Down Expand Up @@ -297,7 +297,7 @@ def separate_proposals(neurograph, radius):


# --- Trim Endpoints ---
def run_trimming(neurograph, proposals, radius):
def run_trimming(neurograph, proposals, radius, progress_bar):
n_endpoints_trimmed = 0
long_radius = radius * RADIUS_SCALING_FACTOR
for proposal in deepcopy(proposals):
Expand All @@ -312,7 +312,8 @@ def run_trimming(neurograph, proposals, radius):
elif neurograph.dist(i, j) > radius:
neurograph.remove_proposal(proposal)
n_endpoints_trimmed += 1 if trim_bool else 0
print("# Endpoints Trimmed:", n_endpoints_trimmed)
if progress_bar:
print("# Endpoints Trimmed:", n_endpoints_trimmed)
return neurograph


Expand Down
5 changes: 2 additions & 3 deletions src/deep_neurographs/groundtruth_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from deep_neurographs import geometry
from deep_neurographs.geometry import dist as get_dist
from deep_neurographs.utils import graph_util as gutil
from deep_neurographs.utils import util

ALIGNED_THRESHOLD = 4
Expand Down Expand Up @@ -132,8 +131,8 @@ def is_component_aligned(target_graph, pred_graph, nodes, kdtree):
Returns
-------
bool
Indication of whether connected component "nodes" is aligned to a connected
component in "target_graph".
Indication of whether connected component "nodes" is aligned to a
connected component in "target_graph".
"""
# Compute distances
Expand Down
11 changes: 9 additions & 2 deletions src/deep_neurographs/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def run(self, fragments_pointer):
t, unit = util.time_writer(time() - t0)
print(f"Total Runtime: {round(t, 4)} {unit}\n")

def run_schedule(self, fragments_pointer, search_radius_schedule):
def run_schedule(
self, fragments_pointer, search_radius_schedule, save_all_rounds=False
):
t0 = time()
self.report_experiment()
self.build_graph(fragments_pointer)
Expand All @@ -161,7 +163,12 @@ def run_schedule(self, fragments_pointer, search_radius_schedule):
round_id += 1
self.generate_proposals(search_radius)
self.run_inference()
if save_all_rounds:
self.save_results(round_id=round_id)

if not save_all_rounds:
self.save_results(round_id=round_id)

t, unit = util.time_writer(time() - t0)
print(f"Total Runtime: {round(t, 4)} {unit}\n")

Expand Down Expand Up @@ -263,7 +270,7 @@ def run_inference(self):
)
self.accepted_proposals.extend(accepts)
print("# Accepted:", util.reformat_number(len(accepts)))
print("% Accepted:", len(accepts) / n_proposals)
print("% Accepted:", round(len(accepts) / n_proposals, 4))

t, unit = util.time_writer(time() - t0)
print(f"Module Runtime: {round(t, 4)} {unit}\n")
Expand Down
4 changes: 2 additions & 2 deletions src/deep_neurographs/machine_learning/feature_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from deep_neurographs.utils import img_util, util

CHUNK_SIZE = [64, 64, 64]
CHUNK_SIZE = [48, 48, 48]
N_BRANCH_PTS = 50
N_PROFILE_PTS = 16 # 10
N_SKEL_FEATURES = 22
Expand Down Expand Up @@ -192,7 +192,7 @@ def get_profile_specs(xyz_1, xyz_2, downsample_factor):
voxel_2 = img_util.to_voxels(xyz_2, downsample_factor=downsample_factor)

# Store local coordinates
bbox = img_util.get_minimal_bbox(np.vstack([voxel_1, voxel_2]), buffer=1)
bbox = img_util.get_fixed_bbox(np.vstack([voxel_1, voxel_2]), CHUNK_SIZE)
start = [voxel_1[i] - bbox["min"][i] for i in range(3)]
end = [voxel_2[i] - bbox["min"][i] for i in range(3)]
specs = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ def proposal_skeletal(neurograph, proposals, radius):
neurograph.proposal_length(proposal) / radius,
neurograph.n_nearby_leafs(proposal, radius),
neurograph.proposal_radii(proposal),
neurograph.proposal_directionals(proposal, 8),
neurograph.proposal_directionals(proposal, 16),
neurograph.proposal_directionals(proposal, 32),
neurograph.proposal_directionals(proposal, 64),
neurograph.proposal_directionals(proposal, 128),
),
axis=None,
)
Expand Down
6 changes: 2 additions & 4 deletions src/deep_neurographs/neurograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,14 @@ def proposal_directionals(self, proposal, window):

def merge_proposal(self, proposal):
i, j = tuple(proposal)
somas_check = not (self.is_soma(i) and self.is_soma(j))
somas_check = not (self.is_soma(i) and self.is_soma(j))
if somas_check and self.check_proposal_degrees(i, j):
# Dense attributes
attrs = dict()
self.nodes[i]["radius"] = 7.0
self.nodes[j]["radius"] = 7.0
for k in ["xyz", "radius"]:
combine = np.vstack if k == "xyz" else np.array
self.nodes[i][k][-1] = 8.0
self.nodes[j][k][0] = 8.0
attrs[k] = combine([self.nodes[i][k], self.nodes[j][k]])

# Sparse attributes
Expand Down Expand Up @@ -701,7 +699,7 @@ def check_proposal_degrees(self, i, j):
one_leaf = self.degree[i] == 1 or self.degree[j] == 1
branching = self.degree[i] > 2 or self.degree[j] > 2
return one_leaf and not branching

def upd_ids(self, swc_id, r):
"""
Updates the swc_id of all nodes connected to "r".
Expand Down
2 changes: 1 addition & 1 deletion src/deep_neurographs/utils/graph_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def prune_branch(graph, leaf, prune_depth):
# Check whether to stop
if np.sum(node_dists) > prune_depth:
break
return branch[0:min(4, len(branch))]
return branch[0:min(5, len(branch))]


def smooth_branch(swc_dict, attrs, edges, nbs, root, j):
Expand Down
18 changes: 13 additions & 5 deletions src/deep_neurographs/utils/img_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def read_tensorstore_with_bbox(img, bbox):
try:
shape = [bbox["max"][i] - bbox["min"][i] for i in range(3)]
return read_tensorstore(img, bbox["min"], shape, from_center=False)
except Exception as e:
print(type(e), e)
except Exception:
return np.zeros(shape)


Expand Down Expand Up @@ -186,7 +185,7 @@ def read_intensities(img, voxels):
Image intensities.
"""
return [img[tuple(voxel)] for voxel in voxels]
return [img[voxel] for voxel in map(tuple, voxels)]


def get_start_end(voxel, shape, from_center=True):
Expand Down Expand Up @@ -405,7 +404,7 @@ def get_bbox(origin, shape):
return None


def get_minimal_bbox(voxels, buffer=0):
def get_minimal_bbox(voxels):
"""
Gets the min and max coordinates of a bounding box that contains "voxels".
Expand All @@ -425,7 +424,16 @@ def get_minimal_bbox(voxels, buffer=0):
"""
bbox = {
"min": np.floor(np.min(voxels, axis=0) - 1).astype(int),
"max": np.ceil(np.max(voxels, axis=0) + buffer + 1).astype(int),
"max": np.ceil(np.max(voxels, axis=0) + 1).astype(int),
}
return bbox


def get_fixed_bbox(voxels, shape):
centroid = np.round(np.mean(voxels, axis=0)).astype(int)
bbox = {
"min": [centroid[i] - shape[i] // 2 for i in range(3)],
"max": [centroid[i] + shape[i] // 2 for i in range(3)],
}
return bbox

Expand Down

0 comments on commit 7e0114f

Please sign in to comment.