-
Notifications
You must be signed in to change notification settings - Fork 34
Add pairwise distance-based neighbor finding in addition to KDTree #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
candytaco
wants to merge
24
commits into
SugiharaLab:master
Choose a base branch
from
candytaco:main
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5ab86e6
Add NeighborFinder classes
candytaco 3604c8b
factor out KNN exclusion radius checking to property
candytaco e7c6b10
update references to property
candytaco e5b7b66
factor out lib indices validation from FindNeighbors
candytaco 6f8cc9a
factor actual number of neighbors to pquery for (knn_) out to property
candytaco a757306
factor out indexKNN index remapping to class method
candytaco 5e04a06
add option to EDM classes to switch between kDTree and pairwise dista…
candytaco b4a0975
update CCM to make use of pairwise distance matrices in iteration
candytaco 35c720b
update simplex indexing
candytaco 2761149
add neighbor algorithm to API functions
candytaco c45c213
fix typos
candytaco 5ef5011
fix typos, and pdist Simplex initialize distance matrix before CCM
candytaco 260e572
fix indexing function argument order
candytaco 17ed9c2
fix typo in CCM pdist implementation
candytaco 87ce263
move FindNeighbors into EDM class file
candytaco 28bb2b5
use single np.allclose with atol to check array equality instead of c…
candytaco 9dc3ab8
update FindNeighbors docstring to not specifically reference KDTree
candytaco ac56ac9
Merge remote-tracking branch 'origin/main' into main
candytaco 01bfd42
break apart KNN index-to lib index remapping function to separate ind…
candytaco 8e96d56
Add Pdist neighbor finder option to take an exclusion mask
candytaco aecf7ab
add EDM method to build an exclusion mask for neighbors to ignore
candytaco 06e54d9
fix a thing where degenerate neighbors using np.delete can potentiall…
candytaco 1fb6b71
EDM class to give exclusion mask to pdist neighbor finder
candytaco 7c17cd1
CCM to make use of embeded exclusion matrix
candytaco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,9 @@ | |||||
| from pandas import DataFrame, concat | ||||||
| from numpy import array, exp, fmax, divide, mean, nan, roll, sum, zeros | ||||||
| from numpy.random import default_rng | ||||||
| import numpy as np | ||||||
|
|
||||||
| from .NeighborFinder import PairwiseDistanceNeighborFinder | ||||||
| # local modules | ||||||
| from .Simplex import Simplex as SimplexClass | ||||||
| from .AuxFunc import ComputeError, IsIterable | ||||||
|
|
@@ -34,7 +36,8 @@ def __init__( self, | |||||
| ignoreNan = True, | ||||||
| mpMethod = None, | ||||||
| sequential = False, | ||||||
| verbose = False ): | ||||||
| verbose = False, | ||||||
| neighbor_algorithm = 'pdist'): | ||||||
| '''Initialize CCM.''' | ||||||
|
|
||||||
| # Assign parameters from API arguments | ||||||
|
|
@@ -58,6 +61,7 @@ def __init__( self, | |||||
| self.mpMethod = mpMethod | ||||||
| self.sequential = sequential | ||||||
| self.verbose = verbose | ||||||
| self.neighbor_algorithm = neighbor_algorithm | ||||||
|
|
||||||
| # Set full lib & pred | ||||||
| self.lib = self.pred = [ 1, self.Data.shape[0] ] | ||||||
|
|
@@ -88,7 +92,8 @@ def __init__( self, | |||||
| validLib = validLib, | ||||||
| noTime = noTime, | ||||||
| ignoreNan = ignoreNan, | ||||||
| verbose = verbose ) | ||||||
| verbose = verbose, | ||||||
| neighbor_algorithm = neighbor_algorithm) | ||||||
|
|
||||||
| self.RevMap = SimplexClass( dataFrame = dataFrame, | ||||||
| columns = target, | ||||||
|
|
@@ -104,7 +109,8 @@ def __init__( self, | |||||
| validLib = validLib, | ||||||
| noTime = noTime, | ||||||
| ignoreNan = ignoreNan, | ||||||
| verbose = verbose ) | ||||||
| verbose = verbose, | ||||||
| neighbor_algorithm = neighbor_algorithm) | ||||||
|
|
||||||
| #------------------------------------------------------------------- | ||||||
| # Methods | ||||||
|
|
@@ -193,6 +199,9 @@ def CrossMap( self, direction ) : | |||||
| libRhoMap = {} # Output dict libSize key : mean rho value | ||||||
| libStatMap = {} # Output dict libSize key : list of ComputeError dicts | ||||||
|
|
||||||
| if self.neighbor_algorithm == 'pdist': | ||||||
| S.FindNeighbors() # need to initialize the pairwise distance matrix | ||||||
|
|
||||||
| # Loop for library sizes | ||||||
| for libSize in self.libSizes : | ||||||
| rhos = zeros( self.sample ) | ||||||
|
|
@@ -202,31 +211,39 @@ def CrossMap( self, direction ) : | |||||
| # Loop for subsamples | ||||||
| for s in range( self.sample ) : | ||||||
| # Generate library row indices for this subsample | ||||||
| rng_i = RNG.choice( lib_i, size = min( libSize, N_lib_i ), | ||||||
| replace = False ) | ||||||
|
|
||||||
| S.lib_i = rng_i | ||||||
|
|
||||||
| S.FindNeighbors() # Depends on S.lib_i | ||||||
| if self.neighbor_algorithm == 'kdtree': | ||||||
| rng_i = RNG.choice( lib_i, size = min( libSize, N_lib_i ), | ||||||
| replace = False ) | ||||||
| S.lib_i = rng_i | ||||||
| S.FindNeighbors() # Depends on S.lib_i | ||||||
| neighbor_distances = S.knn_distances | ||||||
| neighbor_indices = S.knn_neighbors | ||||||
| else: | ||||||
| rng_i = RNG.choice(np.arange(S.neighbor_finder.distanceMatrix.shape[0]), | ||||||
| size = min(libSize, N_lib_i), | ||||||
| replace = False) | ||||||
| d = S.neighbor_finder.distanceMatrix.copy() | ||||||
| mask = np.ones(d.shape[0], dtype = bool) | ||||||
| mask[rng_i] = False | ||||||
| d[mask, :] = np.inf # artificially make all the other ones far awa | ||||||
|
||||||
| d[mask, :] = np.inf # artificially make all the other ones far awa | |
| d[mask, :] = np.inf # artificially make all the other ones far away |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new public
neighbor_algorithmoption is added forSimplex/SMap, but the test suite does not appear to exerciseneighbor_algorithm='pdist'for these APIs. Add a unit/integration test that runsEDM.Simplex(..., neighbor_algorithm='pdist')andEDM.SMap(..., neighbor_algorithm='pdist')on a small non-degenerate dataset (no tied neighbors) to validate the new code path and prevent regressions.