Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if j_connect not in segs_adjacency_nodes[jseg]:
segs_adjacency_nodes[jseg][j_connect] = []
idx = segs_adjacency_nodes[jseg][j_connect].index((point_connect, kseg_list[trunk]))
segs_adjacency_nodes[jseg][j_connect][idx] = (point_connect, kseg_list[not_trunk])
if point_connect not in segs_adjacency_nodes[kseg_list[not_trunk]]:
segs_adjacency_nodes[kseg_list[not_trunk]][point_connect] = []
segs_adjacency_nodes[kseg_list[not_trunk]][point_connect].append((j_connect, jseg))
# clean up the dictionary for trunk
idx = segs_adjacency_nodes[kseg_list[trunk]][point_connect].index((j_connect, jseg))
segs_adjacency_nodes[kseg_list[trunk]][point_connect].pop(idx)
if len(segs_adjacency_nodes[kseg_list[trunk]][point_connect]) == 0:
del segs_adjacency_nodes[kseg_list[trunk]][point_connect]
connectedness[not_trunk] += score
distances = [1/c if c > 0 else np.inf for c in connectedness]
# distances = [1/(1+c) for c in connectedness]
logg.msg(' ', jseg, '-', kseg_list, '->', distances, v=5)
return distances
'(would produce cycle)', v=4)
# we still have the other new segment to inspect so it's not
# a drama that we couldn't establish a new connection
if kseg != kseg_list[-1]:
logg.msg(' continue', v=4)
continue
# we do not add add a new link
else:
logg.msg(' do not add another link', v=4)
continue_after_distance_compute = True
if jseg_min in kseg_list and not do_not_attach_ksegs_with_each_other:
segs_adjacency[jseg_min].append(kseg)
segs_adjacency[kseg].append(jseg_min)
# we're already done as we found the new connection
continue_after_distance_compute = True
logg.msg(' attaching new segment',
kseg, 'with new segment', jseg_min, v=4)
return segs_distances
new_tip = new_tips.pop(tip_idx_max)
dist_max = dists.pop(tip_idx_max)
for iclus, clus in enumerate(self.clusters_precomputed):
if new_tip in set(clus):
new_seg = clus
clus_name = self.clusters_precomputed_names[iclus]
break
pos_new_seg = np.in1d(seg, new_seg, assume_unique=True)
ssegs = [new_seg, seg[~pos_new_seg]]
ssegs_tips = [[new_tip], new_tips]
sizes = [len(ssegs[0]), len(ssegs[1])]
np.set_printoptions(precision=4)
logg.msg(' new tip', new_tip, 'with distance', dist_max,
'using constraints {} with distances'
.format(new_tips), v=4)
logg.msg(' ', dists, v=4)
logg.msg(' new sizes {} and {}'
.format(sizes[0], sizes[1]), v=4)
return iseg, seg, ssegs, ssegs_tips, sizes, clus_name
median_distances = []
if self.attachedness_measure != 'connectedness':
result = self.compute_attachedness(jseg, kseg_list, segs, segs_tips, segs_adjacency_nodes)
distances, median_distances, measure_points_in_jseg, measure_points_in_kseg = result
segs_distances[jseg, kseg_list] = distances
segs_distances[kseg_list, jseg] = distances
distances = segs_distances[jseg, kseg_list]
# in case we do not have convincing evidence for a connection based on the maximal distances
if (median_distances
and ((max(distances) < 0.1 and min(distances) / max(distances) >= 0.4)
# all distances are very small, we require significant statistical evidence here
or (min(distances) >= 0.1 and min(distances) / max(distances) >= self.minimal_distance_evidence))
# distances are larger
and min(median_distances) / max(median_distances) < self.minimal_distance_evidence):
# require median_distances to actually provide better evidence
logg.msg(' no convincing evidence in minimal distances, consider median distance', v=4)
idx = np.argmin(median_distances)
else:
idx = np.argmin(distances)
kseg_min = kseg_list[idx]
pos = segs_adjacency[jseg].index(iseg)
segs_adjacency[jseg][pos] = kseg_min
pos_2 = segs_adjacency[iseg].index(jseg)
segs_adjacency[iseg].pop(pos_2)
segs_adjacency[kseg_min].append(jseg)
logg.msg(' group {} is now attached to {}'.format(jseg, kseg_min), v=4)
# in case the segment we split should correspond to two "clusters", we
# need to check whether the new segments connect to any of the other old
# segments
# if not, we add a link between the new segments, if yes, we add two
# links to connect them at the correct old segments
# logg.info('... treat new connections')
dists.append(np.max(dtip_j))
dtip_others += dtip_j
tip_idx_max = np.argmax(dists)
new_tip = new_tips.pop(tip_idx_max)
dist_max = dists.pop(tip_idx_max)
new_seg = np.ones(len(seg), dtype=bool)
for constraint_tip in new_tips:
new_seg[self.Dchosen[new_tip, seg] > self.Dchosen[constraint_tip, seg]] = False
ssegs = [seg[new_seg], seg[~new_seg]]
ssegs_tips = [[new_tip], new_tips]
sizes = [len(ssegs[0]), len(ssegs[1])]
np.set_printoptions(precision=4)
logg.msg(' new tip', new_tip, 'with distance', dist_max,
'using constraints {} with distances'
.format(new_tips), v=4)
logg.msg(' ', dists, v=4)
logg.msg(' new sizes {} and {}'
.format(sizes[0], sizes[1]), v=4)
return iseg, seg, ssegs, ssegs_tips, sizes
do_not_attach_ksegs_with_each_other = False
continue_after_distance_compute = False
for kseg in kseg_list:
jseg_list = [jseg for jseg in range(len(segs))
if jseg != kseg and jseg not in segs_adjacency[kseg]] # prev_connecting_segments] # if it's a cluster split, this is allowed?
if self.attachedness_measure != 'connectedness':
result = self.compute_attachedness(kseg, jseg_list, segs, segs_tips, segs_adjacency_nodes)
distances, median_distances, measure_points_in_kseg, measure_points_in_jseg = result
segs_distances[kseg, jseg_list] = distances
segs_distances[jseg_list, kseg] = distances
if continue_after_distance_compute: continue
idx = np.argmin(segs_distances[kseg, jseg_list])
# candidate for the segment to which we attach would attach the new
# segment
jseg_min = jseg_list[idx]
logg.msg(' consider connecting', kseg, 'to', jseg_min, v=4)
# if the closest segment is not among the two new segments
if jseg_min not in kseg_list:
segs_adjacency_sparse = sp.sparse.lil_matrix(
(len(segs), len(segs)), dtype=float)
for i, neighbors in enumerate(segs_adjacency):
segs_adjacency_sparse[i, neighbors] = 1
G = nx.Graph(segs_adjacency_sparse)
paths_all = nx.single_source_dijkstra_path(G, source=kseg)
# we can attach the new segment to an old segment
if jseg_min not in paths_all:
segs_adjacency[jseg_min].append(kseg)
segs_adjacency[kseg].append(jseg_min)
logg.msg(' attaching new segment',
kseg, 'at', jseg_min, v=4)
# if we establish the new connection with an old segment
# we should not add a new connection to the second new segment
def propose_nodes_to_contract(adjacency_tree_confidence, node_groups):
# nodes with two edges
n_edges_per_seg = np.sum(adjacency_tree_confidence > 0, axis=1).A1
for i in range(adjacency_tree_confidence.shape[0]):
if n_edges_per_seg[i] == 2:
neighbors = adjacency_tree_confidence[i].nonzero()[1]
for neighbors_edges in range(1, 20):
for n_cnt, n in enumerate(neighbors):
if n_edges_per_seg[n] == neighbors_edges:
logg.msg('merging node {} into {} (two edges)'
.format(i, n), v=4)
return i, n
# node groups with a very small cell number
for i in range(adjacency_tree_confidence.shape[0]):
if node_groups[str(i) == node_groups].size < min_group_size:
neighbors = adjacency_tree_confidence[i].nonzero()[1]
neighbor_sizes = [node_groups[str(n) == node_groups].size for n in neighbors]
n = neighbors[np.argmax(neighbor_sizes)]
logg.msg('merging node {} into {} '
'(smaller than `min_group_size` = {})'
.format(i, n, min_group_size), v=4)
return i, n
return 0, 0
dists.append(np.max(dtip_j))
dtip_others += dtip_j
tip_idx_max = np.argmax(dists)
new_tip = new_tips.pop(tip_idx_max)
dist_max = dists.pop(tip_idx_max)
for iclus, clus in enumerate(self.clusters_precomputed):
if new_tip in set(clus):
new_seg = clus
clus_name = self.clusters_precomputed_names[iclus]
break
pos_new_seg = np.in1d(seg, new_seg, assume_unique=True)
ssegs = [new_seg, seg[~pos_new_seg]]
ssegs_tips = [[new_tip], new_tips]
sizes = [len(ssegs[0]), len(ssegs[1])]
np.set_printoptions(precision=4)
logg.msg(' new tip', new_tip, 'with distance', dist_max,
'using constraints {} with distances'
.format(new_tips), v=4)
logg.msg(' ', dists, v=4)
logg.msg(' new sizes {} and {}'
.format(sizes[0], sizes[1]), v=4)
return iseg, seg, ssegs, ssegs_tips, sizes, clus_name