Nnsearch
Index
Bases: JSONSerializable
base Index class. currently no function besides typing, warning of unimplemented features.
Subclasses of Index implement nearest neighbor search with different cababilities and performance tradeoffs.
Source code in src/anguilla/nnsearch.py
IndexBrute
Bases: Index
Optimized for simplicity and flexibility, may not scale to large datasets.
NOTE: currently no batching support.
Source code in src/anguilla/nnsearch.py
__init__(d=None, metric=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
int
|
optional, dimension of feature |
None
|
metric |
Callable
|
distance metric, default to squared euclidean |
None
|
Source code in src/anguilla/nnsearch.py
add(feature, id=None)
add a new feature, return its ID. Args: feature: the feature to add id: if not supplied, generate a new ID; otherwise, use the supplied id. supply an existing id to replace.
Source code in src/anguilla/nnsearch.py
get(id)
remove(id)
search(feature, k=3)
get feature(s) and IDs by proximity
Source code in src/anguilla/nnsearch.py
IndexFast
Bases: Index
Optimized for fast search
on large vectors / datasets.
Only L2 distance supported.
remove
may be slow.
This is currently a wrapper around faiss.FlatIndexL2
which provides stable ids when using remove
.
In the future could support dot product and/or approximate search indices.
Source code in src/anguilla/nnsearch.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
__init__(d, metric=sqL2)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
int
|
dimension of feature |
required |
metric |
Callable
|
|
sqL2
|
Source code in src/anguilla/nnsearch.py
add(feature, id=None)
add a new feature, return its ID. Args: feature: the feature to add id: if not supplied, generate a new ID; otherwise, use the supplied id. supply an existing id to replace.
Source code in src/anguilla/nnsearch.py
get(id)
remove(id)
remove a feature by ID
Source code in src/anguilla/nnsearch.py
search(feature, k=3)
get feature(s) and IDs by proximity
Source code in src/anguilla/nnsearch.py
Metric
Bases: JSONSerializable
define a distance between two points.
Relative distances will be used to find nearest neighbors,
and the distances to neighbors will be passed to Interpolate
.
Source code in src/anguilla/nnsearch.py
NNSearch
Bases: JSONSerializable
This class is the mid-level interface for neighbor search,
providing some common utilities over the Index subclasses.
Users will generally use IML.search
instead of calling NNSearch
directly.
Source code in src/anguilla/nnsearch.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
__call__(feature, k=None)
find the k-nearest neighbors of feature
Args:
feature: query feature vector
k: maximum number of neighbors to return
Returns:
ids: ids of neighbors
scores: similarity scores of neighbors (higher is more similar)
Source code in src/anguilla/nnsearch.py
__init__(index, k=10)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
Index
|
instance of |
required |
k |
default k-nearest neighbors (but can be overridden later) |
10
|
__iter__()
add(feature, id=None)
distance(a, b)
get(id)
items()
remove(id, batch=False)
Remove point(s) from the index by ID
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
Union[PairID, PairIDs]
|
id or sequence of ids |
required |
batch |
bool
|
True if removing a batch of ids, False if a single id. |
False
|
Source code in src/anguilla/nnsearch.py
remove_near(feature, k=None)
Remove point(s) from the index by proximity. Use k=1 to remove a single point.