-
Notifications
You must be signed in to change notification settings - Fork 5
/
analysis.py
67 lines (58 loc) · 2.35 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from annoy import AnnoyIndex
from .data import _typecheck,_pathfilter,_featfilter,seq_types
from .plot import show,montage
import numpy as np
"""
Currently this function will use show() to display k nearest neighbors of i,
in feature space X. It will print the indices as well. Future iterations could
return some of this data for use in other processes, but for now it's merely a
quick (and approximate) visual analysis tool.
"""
def nearest(X=None,
i=None,
pathcol=None,
k=4,
notecol=None,
thumb=False,
bg='white',
plot=True):
if isinstance(pathcol,int): # allowable for show(), blocked by _paste()
raise TypeError("'pathcol' must be a pandas Series")
if X is None:
raise ValueError("Must supply feature matrix 'X'")
if i is None:
i = np.random.choice(X.index)
if isinstance(i,seq_types): # can be seq in cut()
raise ValueError("Must choose a single 'i' as target")
_typecheck(**locals())
pathcol = _pathfilter(pathcol)
notecol = _featfilter(pathcol,notecol)
f = X.shape[1] # number of columns in X
t = AnnoyIndex(f) # Length of item vector that will be indexed
for i,item in enumerate(X.index):
t.add_item(i,list(X.loc[item]))
idmap = dict(zip(X.index,list(range(len(X)))))
idmapReverse = dict(zip(list(range(len(X))),X.index))
t.build(10) # 10 trees
nnsAnnoy = t.get_nns_by_item(idmap[i],k,include_distances=False) # dists?
nnsNative = [idmapReverse[item] for item in nnsAnnoy]
if plot in [True,'show']:
print(nnsNative)
if notecol is None:
return show(pathcol=pathcol.loc[nnsNative],idx=True,thumb=thumb,bg=bg)
elif notecol is not None:
return show(pathcol=pathcol.loc[nnsNative],
idx=True,
notecol=notecol.loc[nnsNative],
thumb=thumb,bg=bg)
elif plot=='montage':
print(nnsNative)
if notecol is None:
return montage(pathcol=pathcol.loc[nnsNative],idx=True,thumb=thumb,bg=bg)
elif notecol is not None:
return montage(pathcol=pathcol.loc[nnsNative],
idx=True,
notecol=notecol.loc[nnsNative],
thumb=thumb,bg=bg)
elif plot==False:
return nnsNative