nystrom-ncut 0.0.8__py3-none-any.whl → 0.0.9__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
nystrom_ncut/common.py CHANGED
@@ -22,3 +22,40 @@ def lazy_normalize(x: torch.Tensor, n: int = 1000, **normalize_kwargs: Any) -> t
22
22
  return x
23
23
  else:
24
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
nystrom_ncut/nystrom.py CHANGED
@@ -2,7 +2,9 @@ from typing import Literal, Tuple
2
2
 
3
3
  import torch
4
4
 
5
- from .common import ceildiv
5
+ from .common import (
6
+ ceildiv,
7
+ )
6
8
 
7
9
 
8
10
  EigSolverOptions = Literal["svd_lowrank", "lobpcg", "svd", "eigh"]
@@ -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, knn: int,
32
- seed: int, device: str,
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] = _identity,
37
- pre_smooth: bool = True,
34
+ transform_func: Callable[[torch.Tensor], torch.Tensor],
35
+ seed: int,
36
+ device: str,
38
37
  ) -> Tuple[torch.Tensor, torch.Tensor]:
39
38
 
40
- if pre_smooth:
41
- features = extrapolate_knn(
42
- features,
43
- features,
44
- features,
45
- distance="cosine",
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, knn=knn,
110
- seed=seed, device=device,
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, knn=knn,
152
- seed=seed, device=device,
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, knn=knn,
209
- seed=seed, device=device,
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, knn=knn,
244
- seed=seed, device=device,
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, knn=knn,
287
- seed=seed, device=device,
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
- transform_func=transform_func
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, knn=knn,
325
- seed=seed, device=device,
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.8
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,11 @@
1
+ nystrom_ncut/__init__.py,sha256=Vlc_iAlfvTNUiJXpZLWUOaL2Q-YqZqgr7WoG6cVnD0g,439
2
+ nystrom_ncut/common.py,sha256=RMPQvg9R2s7V-q7zAStN9YCZt7gpc5Ut-KSKtvELBQ4,1934
3
+ nystrom_ncut/ncut_pytorch.py,sha256=-SKs9AdkafJSGkeYt4LwhbKZr8oq9JA5caAqjiVDAzU,11220
4
+ nystrom_ncut/nystrom.py,sha256=1ngKzRrY9i2jEDo0EP8I9RsNTQzy4S7pmvkpayIkUOQ,8811
5
+ nystrom_ncut/propagation_utils.py,sha256=AEKgWVw7x_podLEzELdCQeMmTJfAYfG4TiPuKbrN8Sw,10279
6
+ nystrom_ncut/visualize_utils.py,sha256=p4wXXg47vqMn8c1N6NxGxtmPNwOzqquDvpMEjZVdq34,17196
7
+ nystrom_ncut-0.0.9.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
8
+ nystrom_ncut-0.0.9.dist-info/METADATA,sha256=umrvGtKsci280oi3D5I_BeLIt8ajBogC5veHYrRln48,6058
9
+ nystrom_ncut-0.0.9.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
+ nystrom_ncut-0.0.9.dist-info/top_level.txt,sha256=j7g_j0S048EvguFFnGgD5Ewd3r2H6klsxd5A4dd-wHw,13
11
+ nystrom_ncut-0.0.9.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- nystrom_ncut/__init__.py,sha256=Vlc_iAlfvTNUiJXpZLWUOaL2Q-YqZqgr7WoG6cVnD0g,439
2
- nystrom_ncut/common.py,sha256=G6w_8_BfBUMc6r8WFgA0NH4K6am7AzZCSdrQEVjra7U,671
3
- nystrom_ncut/ncut_pytorch.py,sha256=-SKs9AdkafJSGkeYt4LwhbKZr8oq9JA5caAqjiVDAzU,11220
4
- nystrom_ncut/nystrom.py,sha256=-l26oiJ0oPReSGlMlYV3gftszgFdAAHAi7OFtGPZ4Ic,8802
5
- nystrom_ncut/propagation_utils.py,sha256=0d2VhT0JrLRurd44hZbnxBvBh-QscPKxtV7VrwYtTdo,11569
6
- nystrom_ncut/visualize_utils.py,sha256=jDjuyZ9rdd25jqrPObJgK8zCLHc3Oms0fQnaIetHk-U,17112
7
- nystrom_ncut-0.0.8.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
8
- nystrom_ncut-0.0.8.dist-info/METADATA,sha256=zQpx3REOOckpJSuc7N6UNpXZoqgsM5UoFWV6__DuaRQ,6058
9
- nystrom_ncut-0.0.8.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
- nystrom_ncut-0.0.8.dist-info/top_level.txt,sha256=j7g_j0S048EvguFFnGgD5Ewd3r2H6klsxd5A4dd-wHw,13
11
- nystrom_ncut-0.0.8.dist-info/RECORD,,