nystrom-ncut 0.0.8__tar.gz → 0.0.9__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {nystrom_ncut-0.0.8/src/nystrom_ncut.egg-info → nystrom_ncut-0.0.9}/PKG-INFO +1 -1
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/pyproject.toml +1 -1
- nystrom_ncut-0.0.9/src/nystrom_ncut/common.py +61 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut/nystrom.py +3 -1
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut/propagation_utils.py +0 -38
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut/visualize_utils.py +55 -46
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9/src/nystrom_ncut.egg-info}/PKG-INFO +1 -1
- nystrom_ncut-0.0.8/src/nystrom_ncut/common.py +0 -24
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/LICENSE +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/MANIFEST.in +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/README.md +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/requirements.txt +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/setup.cfg +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut/__init__.py +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut/ncut_pytorch.py +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut.egg-info/SOURCES.txt +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut.egg-info/dependency_links.txt +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/src/nystrom_ncut.egg-info/top_level.txt +0 -0
- {nystrom_ncut-0.0.8 → nystrom_ncut-0.0.9}/tests/test.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: nystrom_ncut
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.9
|
4
4
|
Summary: Normalized Cut and Nyström Approximation
|
5
5
|
Author-email: Huzheng Yang <huze.yann@gmail.com>, Wentinn Liao <wentinn.liao@gmail.com>
|
6
6
|
Project-URL: Documentation, https://github.com/JophiArcana/Nystrom-NCUT/
|
@@ -0,0 +1,61 @@
|
|
1
|
+
from typing import Any, Literal
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
import torch
|
5
|
+
import torch.nn.functional as Fn
|
6
|
+
|
7
|
+
|
8
|
+
DistanceOptions = Literal["cosine", "euclidean", "rbf"]
|
9
|
+
SampleOptions = Literal["farthest", "random"]
|
10
|
+
|
11
|
+
|
12
|
+
def ceildiv(a: int, b: int) -> int:
|
13
|
+
return -(-a // b)
|
14
|
+
|
15
|
+
|
16
|
+
def lazy_normalize(x: torch.Tensor, n: int = 1000, **normalize_kwargs: Any) -> torch.Tensor:
|
17
|
+
numel = np.prod(x.shape[:-1])
|
18
|
+
n = min(n, numel)
|
19
|
+
random_indices = torch.randperm(numel)[:n]
|
20
|
+
_x = x.flatten(0, -2)[random_indices]
|
21
|
+
if torch.allclose(torch.norm(_x, **normalize_kwargs), torch.ones(n, device=x.device)):
|
22
|
+
return x
|
23
|
+
else:
|
24
|
+
return Fn.normalize(x, **normalize_kwargs)
|
25
|
+
|
26
|
+
|
27
|
+
def quantile_min_max(x, q1=0.01, q2=0.99, n_sample=10000):
|
28
|
+
if x.shape[0] > n_sample:
|
29
|
+
np.random.seed(0)
|
30
|
+
random_idx = np.random.choice(x.shape[0], n_sample, replace=False)
|
31
|
+
vmin, vmax = x[random_idx].quantile(q1), x[random_idx].quantile(q2)
|
32
|
+
else:
|
33
|
+
vmin, vmax = x.quantile(q1), x.quantile(q2)
|
34
|
+
return vmin, vmax
|
35
|
+
|
36
|
+
|
37
|
+
def quantile_normalize(x, q=0.95):
|
38
|
+
"""normalize each dimension of x to [0, 1], take 95-th percentage, this robust to outliers
|
39
|
+
</br> 1. sort x
|
40
|
+
</br> 2. take q-th quantile
|
41
|
+
</br> min_value -> (1-q)-th quantile
|
42
|
+
</br> max_value -> q-th quantile
|
43
|
+
</br> 3. normalize
|
44
|
+
</br> x = (x - min_value) / (max_value - min_value)
|
45
|
+
|
46
|
+
Args:
|
47
|
+
x (torch.Tensor): input tensor, shape (n_samples, n_features)
|
48
|
+
normalize each feature to 0-1 range
|
49
|
+
q (float): quantile, default 0.95
|
50
|
+
|
51
|
+
Returns:
|
52
|
+
torch.Tensor: quantile normalized tensor
|
53
|
+
"""
|
54
|
+
# normalize x to 0-1 range, max value is q-th quantile
|
55
|
+
# quantile makes the normalization robust to outliers
|
56
|
+
if isinstance(x, np.ndarray):
|
57
|
+
x = torch.tensor(x)
|
58
|
+
vmax, vmin = quantile_min_max(x, q, 1 - q)
|
59
|
+
x = (x - vmin) / (vmax - vmin)
|
60
|
+
x = x.clamp(0, 1)
|
61
|
+
return x
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import logging
|
2
|
-
from typing import Literal
|
3
2
|
|
4
3
|
import numpy as np
|
5
4
|
import torch
|
@@ -256,40 +255,3 @@ def extrapolate_knn_with_subsampling(
|
|
256
255
|
device=device
|
257
256
|
)
|
258
257
|
return new_eigenvectors
|
259
|
-
|
260
|
-
|
261
|
-
def quantile_min_max(x, q1=0.01, q2=0.99, n_sample=10000):
|
262
|
-
if x.shape[0] > n_sample:
|
263
|
-
np.random.seed(0)
|
264
|
-
random_idx = np.random.choice(x.shape[0], n_sample, replace=False)
|
265
|
-
vmin, vmax = x[random_idx].quantile(q1), x[random_idx].quantile(q2)
|
266
|
-
else:
|
267
|
-
vmin, vmax = x.quantile(q1), x.quantile(q2)
|
268
|
-
return vmin, vmax
|
269
|
-
|
270
|
-
|
271
|
-
def quantile_normalize(x, q=0.95):
|
272
|
-
"""normalize each dimension of x to [0, 1], take 95-th percentage, this robust to outliers
|
273
|
-
</br> 1. sort x
|
274
|
-
</br> 2. take q-th quantile
|
275
|
-
</br> min_value -> (1-q)-th quantile
|
276
|
-
</br> max_value -> q-th quantile
|
277
|
-
</br> 3. normalize
|
278
|
-
</br> x = (x - min_value) / (max_value - min_value)
|
279
|
-
|
280
|
-
Args:
|
281
|
-
x (torch.Tensor): input tensor, shape (n_samples, n_features)
|
282
|
-
normalize each feature to 0-1 range
|
283
|
-
q (float): quantile, default 0.95
|
284
|
-
|
285
|
-
Returns:
|
286
|
-
torch.Tensor: quantile normalized tensor
|
287
|
-
"""
|
288
|
-
# normalize x to 0-1 range, max value is q-th quantile
|
289
|
-
# quantile makes the normalization robust to outliers
|
290
|
-
if isinstance(x, np.ndarray):
|
291
|
-
x = torch.tensor(x)
|
292
|
-
vmax, vmin = quantile_min_max(x, q, 1 - q)
|
293
|
-
x = (x - vmin) / (vmax - vmin)
|
294
|
-
x = x.clamp(0, 1)
|
295
|
-
return x
|
@@ -7,15 +7,13 @@ import torch.nn.functional as F
|
|
7
7
|
from sklearn.base import BaseEstimator
|
8
8
|
|
9
9
|
from .common import (
|
10
|
-
DistanceOptions,
|
11
10
|
lazy_normalize,
|
11
|
+
quantile_min_max,
|
12
|
+
quantile_normalize,
|
12
13
|
)
|
13
14
|
from .propagation_utils import (
|
14
15
|
run_subgraph_sampling,
|
15
16
|
extrapolate_knn,
|
16
|
-
extrapolate_knn_with_subsampling,
|
17
|
-
quantile_min_max,
|
18
|
-
quantile_normalize
|
19
17
|
)
|
20
18
|
|
21
19
|
|
@@ -28,22 +26,22 @@ def _rgb_with_dimensionality_reduction(
|
|
28
26
|
num_sample: int,
|
29
27
|
metric: Literal["cosine", "euclidean"],
|
30
28
|
rgb_func: Callable[[torch.Tensor, float], torch.Tensor],
|
31
|
-
q: float,
|
32
|
-
|
29
|
+
q: float,
|
30
|
+
knn: int,
|
33
31
|
reduction: Callable[..., BaseEstimator],
|
34
32
|
reduction_dim: int,
|
35
33
|
reduction_kwargs: Dict[str, Any],
|
36
|
-
transform_func: Callable[[torch.Tensor], torch.Tensor]
|
37
|
-
|
34
|
+
transform_func: Callable[[torch.Tensor], torch.Tensor],
|
35
|
+
seed: int,
|
36
|
+
device: str,
|
38
37
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
39
38
|
|
40
|
-
|
41
|
-
features
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
)
|
39
|
+
features = extrapolate_knn(
|
40
|
+
features,
|
41
|
+
features,
|
42
|
+
features,
|
43
|
+
distance="cosine",
|
44
|
+
)
|
47
45
|
|
48
46
|
subgraph_indices = run_subgraph_sampling(
|
49
47
|
features,
|
@@ -78,10 +76,10 @@ def rgb_from_tsne_2d(
|
|
78
76
|
num_sample: int = 1000,
|
79
77
|
perplexity: int = 150,
|
80
78
|
metric: Literal["cosine", "euclidean"] = "cosine",
|
81
|
-
device: str = None,
|
82
|
-
seed: int = 0,
|
83
79
|
q: float = 0.95,
|
84
80
|
knn: int = 10,
|
81
|
+
seed: int = 0,
|
82
|
+
device: str = None,
|
85
83
|
):
|
86
84
|
"""
|
87
85
|
Returns:
|
@@ -106,11 +104,13 @@ def rgb_from_tsne_2d(
|
|
106
104
|
num_sample=num_sample,
|
107
105
|
metric=metric,
|
108
106
|
rgb_func=rgb_from_2d_colormap,
|
109
|
-
q=q,
|
110
|
-
|
107
|
+
q=q,
|
108
|
+
knn=knn,
|
111
109
|
reduction=TSNE, reduction_dim=2, reduction_kwargs={
|
112
110
|
"perplexity": perplexity,
|
113
|
-
},
|
111
|
+
}, transform_func=_identity,
|
112
|
+
seed=seed,
|
113
|
+
device=device,
|
114
114
|
)
|
115
115
|
return x2d, rgb
|
116
116
|
|
@@ -120,10 +120,10 @@ def rgb_from_tsne_3d(
|
|
120
120
|
num_sample: int = 1000,
|
121
121
|
perplexity: int = 150,
|
122
122
|
metric: Literal["cosine", "euclidean"] = "cosine",
|
123
|
-
device: str = None,
|
124
|
-
seed: int = 0,
|
125
123
|
q: float = 0.95,
|
126
124
|
knn: int = 10,
|
125
|
+
seed: int = 0,
|
126
|
+
device: str = None,
|
127
127
|
):
|
128
128
|
"""
|
129
129
|
Returns:
|
@@ -148,11 +148,13 @@ def rgb_from_tsne_3d(
|
|
148
148
|
num_sample=num_sample,
|
149
149
|
metric=metric,
|
150
150
|
rgb_func=rgb_from_3d_rgb_cube,
|
151
|
-
q=q,
|
152
|
-
|
151
|
+
q=q,
|
152
|
+
knn=knn,
|
153
153
|
reduction=TSNE, reduction_dim=3, reduction_kwargs={
|
154
154
|
"perplexity": perplexity,
|
155
|
-
},
|
155
|
+
}, transform_func=_identity,
|
156
|
+
seed=seed,
|
157
|
+
device=device,
|
156
158
|
)
|
157
159
|
return x3d, rgb
|
158
160
|
|
@@ -161,10 +163,10 @@ def rgb_from_cosine_tsne_3d(
|
|
161
163
|
features: torch.Tensor,
|
162
164
|
num_sample: int = 1000,
|
163
165
|
perplexity: int = 150,
|
164
|
-
device: str = None,
|
165
|
-
seed: int = 0,
|
166
166
|
q: float = 0.95,
|
167
167
|
knn: int = 10,
|
168
|
+
seed: int = 0,
|
169
|
+
device: str = None
|
168
170
|
):
|
169
171
|
"""
|
170
172
|
Returns:
|
@@ -205,11 +207,13 @@ def rgb_from_cosine_tsne_3d(
|
|
205
207
|
num_sample=num_sample,
|
206
208
|
metric="cosine",
|
207
209
|
rgb_func=rgb_from_cosine,
|
208
|
-
q=q,
|
209
|
-
|
210
|
+
q=q,
|
211
|
+
knn=knn,
|
210
212
|
reduction=TSNE, reduction_dim=3, reduction_kwargs={
|
211
213
|
"perplexity": perplexity,
|
212
|
-
},
|
214
|
+
}, transform_func=_identity,
|
215
|
+
seed=seed,
|
216
|
+
device=device,
|
213
217
|
)
|
214
218
|
return x3d, rgb
|
215
219
|
|
@@ -220,10 +224,10 @@ def rgb_from_umap_2d(
|
|
220
224
|
n_neighbors: int = 150,
|
221
225
|
min_dist: float = 0.1,
|
222
226
|
metric: Literal["cosine", "euclidean"] = "cosine",
|
223
|
-
device: str = None,
|
224
|
-
seed: int = 0,
|
225
227
|
q: float = 0.95,
|
226
228
|
knn: int = 10,
|
229
|
+
seed: int = 0,
|
230
|
+
device: str = None,
|
227
231
|
):
|
228
232
|
"""
|
229
233
|
Returns:
|
@@ -240,12 +244,14 @@ def rgb_from_umap_2d(
|
|
240
244
|
num_sample=num_sample,
|
241
245
|
metric=metric,
|
242
246
|
rgb_func=rgb_from_2d_colormap,
|
243
|
-
q=q,
|
244
|
-
|
247
|
+
q=q,
|
248
|
+
knn=knn,
|
245
249
|
reduction=UMAP, reduction_dim=2, reduction_kwargs={
|
246
250
|
"n_neighbors": n_neighbors,
|
247
251
|
"min_dist": min_dist,
|
248
|
-
},
|
252
|
+
}, transform_func=_identity,
|
253
|
+
seed=seed,
|
254
|
+
device=device,
|
249
255
|
)
|
250
256
|
return x2d, rgb
|
251
257
|
|
@@ -256,10 +262,10 @@ def rgb_from_umap_sphere(
|
|
256
262
|
n_neighbors: int = 150,
|
257
263
|
min_dist: float = 0.1,
|
258
264
|
metric: Literal["cosine", "euclidean"] = "cosine",
|
259
|
-
device: str = None,
|
260
|
-
seed: int = 0,
|
261
265
|
q: float = 0.95,
|
262
266
|
knn: int = 10,
|
267
|
+
seed: int = 0,
|
268
|
+
device: str = None,
|
263
269
|
):
|
264
270
|
"""
|
265
271
|
Returns:
|
@@ -283,14 +289,15 @@ def rgb_from_umap_sphere(
|
|
283
289
|
num_sample=num_sample,
|
284
290
|
metric=metric,
|
285
291
|
rgb_func=rgb_from_3d_rgb_cube,
|
286
|
-
q=q,
|
287
|
-
|
292
|
+
q=q,
|
293
|
+
knn=knn,
|
288
294
|
reduction=UMAP, reduction_dim=2, reduction_kwargs={
|
289
295
|
"n_neighbors": n_neighbors,
|
290
296
|
"min_dist": min_dist,
|
291
297
|
"output_metric": "haversine",
|
292
|
-
},
|
293
|
-
|
298
|
+
}, transform_func=transform_func,
|
299
|
+
seed=seed,
|
300
|
+
device=device,
|
294
301
|
)
|
295
302
|
return x3d, rgb
|
296
303
|
|
@@ -301,10 +308,10 @@ def rgb_from_umap_3d(
|
|
301
308
|
n_neighbors: int = 150,
|
302
309
|
min_dist: float = 0.1,
|
303
310
|
metric: Literal["cosine", "euclidean"] = "cosine",
|
304
|
-
device: str = None,
|
305
|
-
seed: int = 0,
|
306
311
|
q: float = 0.95,
|
307
312
|
knn: int = 10,
|
313
|
+
seed: int = 0,
|
314
|
+
device: str = None,
|
308
315
|
):
|
309
316
|
"""
|
310
317
|
Returns:
|
@@ -321,12 +328,14 @@ def rgb_from_umap_3d(
|
|
321
328
|
num_sample=num_sample,
|
322
329
|
metric=metric,
|
323
330
|
rgb_func=rgb_from_3d_rgb_cube,
|
324
|
-
q=q,
|
325
|
-
|
331
|
+
q=q,
|
332
|
+
knn=knn,
|
326
333
|
reduction=UMAP, reduction_dim=3, reduction_kwargs={
|
327
334
|
"n_neighbors": n_neighbors,
|
328
335
|
"min_dist": min_dist,
|
329
|
-
},
|
336
|
+
}, transform_func=_identity,
|
337
|
+
seed=seed,
|
338
|
+
device=device,
|
330
339
|
)
|
331
340
|
return x3d, rgb
|
332
341
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: nystrom_ncut
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.9
|
4
4
|
Summary: Normalized Cut and Nyström Approximation
|
5
5
|
Author-email: Huzheng Yang <huze.yann@gmail.com>, Wentinn Liao <wentinn.liao@gmail.com>
|
6
6
|
Project-URL: Documentation, https://github.com/JophiArcana/Nystrom-NCUT/
|
@@ -1,24 +0,0 @@
|
|
1
|
-
from typing import Any, Literal
|
2
|
-
|
3
|
-
import numpy as np
|
4
|
-
import torch
|
5
|
-
import torch.nn.functional as Fn
|
6
|
-
|
7
|
-
|
8
|
-
DistanceOptions = Literal["cosine", "euclidean", "rbf"]
|
9
|
-
SampleOptions = Literal["farthest", "random"]
|
10
|
-
|
11
|
-
|
12
|
-
def ceildiv(a: int, b: int) -> int:
|
13
|
-
return -(-a // b)
|
14
|
-
|
15
|
-
|
16
|
-
def lazy_normalize(x: torch.Tensor, n: int = 1000, **normalize_kwargs: Any) -> torch.Tensor:
|
17
|
-
numel = np.prod(x.shape[:-1])
|
18
|
-
n = min(n, numel)
|
19
|
-
random_indices = torch.randperm(numel)[:n]
|
20
|
-
_x = x.flatten(0, -2)[random_indices]
|
21
|
-
if torch.allclose(torch.norm(_x, **normalize_kwargs), torch.ones(n, device=x.device)):
|
22
|
-
return x
|
23
|
-
else:
|
24
|
-
return Fn.normalize(x, **normalize_kwargs)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|