From c61a162516a6d3562c6ee7e7d1ea7d075dd7fe71 Mon Sep 17 00:00:00 2001 From: Casper van der Wel Date: Thu, 12 Apr 2018 22:01:48 +0200 Subject: [PATCH] Consistent null particle addition and sorting --- trackpy/linking/find_link.py | 6 ++++-- trackpy/linking/linking.py | 7 +++++-- trackpy/linking/subnet.py | 3 +++ trackpy/linking/subnetlinker.py | 14 -------------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/trackpy/linking/find_link.py b/trackpy/linking/find_link.py index 5acfaed8..63539b44 100644 --- a/trackpy/linking/find_link.py +++ b/trackpy/linking/find_link.py @@ -475,8 +475,10 @@ def assign_links(self): else: new_cands = set() - for sp in source_set: - sp.forward_cands.sort(key=lambda x: x[1]) + # sort candidates and add in penalty for not linking + for _s in source_set: + _s.forward_cands.sort(key=lambda x: x[1]) + _s.forward_cands.append((None, self.search_range)) # link sn_spl, sn_dpl = self.subnet_linker(source_set, dest_set, diff --git a/trackpy/linking/linking.py b/trackpy/linking/linking.py index 5b9e111e..c0415851 100644 --- a/trackpy/linking/linking.py +++ b/trackpy/linking/linking.py @@ -500,8 +500,11 @@ def next_level(self, coords, t, extra_data=None): def assign_links(self): spl, dpl = [], [] for source_set, dest_set in self.subnets: - for sp in source_set: - sp.forward_cands.sort(key=lambda x: x[1]) + # sort candidates and add in penalty for not linking before the + # subnetlinker, preventing repeats occuring during adaptive linking + for _s in source_set: + _s.forward_cands.sort(key=lambda x: x[1]) + _s.forward_cands.append((None, self.search_range)) sn_spl, sn_dpl = self.subnet_linker(source_set, dest_set, self.search_range) diff --git a/trackpy/linking/subnet.py b/trackpy/linking/subnet.py index 6856c95e..ba75d3e4 100644 --- a/trackpy/linking/subnet.py +++ b/trackpy/linking/subnet.py @@ -294,6 +294,9 @@ def split_subnet(source, dest, new_range): # if dp is None: # continue assign_subnet(sp, dp, subnets=subnets) + + # the null particle was removed: re-add it here + sp.forward_cands.append((None, new_range)) return (subnets[key] for key in subnets) diff --git a/trackpy/linking/subnetlinker.py b/trackpy/linking/subnetlinker.py index 261d3f72..d17bdf23 100644 --- a/trackpy/linking/subnetlinker.py +++ b/trackpy/linking/subnetlinker.py @@ -386,11 +386,6 @@ def subnet_linker_recursive(source_set, dest_set, search_range, **kwargs): # particle is lost. Not possible with default Linker implementation. return [source_set.pop()], [None] - # sort candidates and add in penalty for not linking - for _s in source_set: - _s.forward_cands.sort(key=lambda x: x[1]) - _s.forward_cands.append((None, search_range)) - snl = SubnetLinker(source_set, len(dest_set), search_range, **kwargs) sn_spl, sn_dpl = [list(particles) for particles in zip(*snl.best_pairs)] @@ -413,11 +408,6 @@ def subnet_linker_nonrecursive(source_set, dest_set, search_range, **kwargs): # particle is lost. Not possible with default Linker implementation. return [source_set.pop()], [None] - # sort candidates and add in penalty for not linking - for _s in source_set: - _s.forward_cands.sort(key=lambda x: x[1]) - _s.forward_cands.append((None, search_range)) - sn_spl, sn_dpl = nonrecursive_link(source_set, len(dest_set), search_range, **kwargs) for dp in dest_set - set(sn_dpl): @@ -453,10 +443,6 @@ def subnet_linker_numba(source_set, dest_set, search_range, # particle is lost. Not possible with default Linker implementation. return [source_set.pop()], [None] - # Forward candidates were already sorted by Linker.assign_links() - for _s in source_set: - _s.forward_cands.append((None, search_range)) - # Shortcut for small subnets, because the numba linker has significant overhead if (lds == 1 or lss == 1 or (lds <= 3 and lss <= 3)) and hybrid: sn_spl, sn_dpl = recursive_linker_obj(source_set, lds, search_range, **kwargs)