nystrom-ncut 0.2.2__py3-none-any.whl → 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
- import logging
2
- from typing import Any, Callable, Dict, Literal, Union
1
+ from typing import Any, Callable, Dict, Union
3
2
 
4
3
  import numpy as np
5
4
  import torch
@@ -13,7 +12,8 @@ from .common import (
13
12
  quantile_normalize,
14
13
  )
15
14
  from .distance_utils import (
16
- DistanceOptions,
15
+ AffinityOptions,
16
+ AFFINITY_TO_DISTANCE,
17
17
  to_euclidean,
18
18
  affinity_from_features,
19
19
  )
@@ -27,7 +27,7 @@ def extrapolate_knn(
27
27
  anchor_features: torch.Tensor, # [n x d]
28
28
  anchor_output: torch.Tensor, # [n x d']
29
29
  extrapolation_features: torch.Tensor, # [m x d]
30
- distance: DistanceOptions,
30
+ affinity_type: AffinityOptions,
31
31
  knn: int = 10, # k
32
32
  affinity_focal_gamma: float = 1.0,
33
33
  chunk_size: int = 8192,
@@ -41,7 +41,7 @@ def extrapolate_knn(
41
41
  anchor_output (torch.Tensor): output from subgraph, shape (num_sample, D)
42
42
  extrapolation_features (torch.Tensor): features from existing nodes, shape (new_num_samples, n_features)
43
43
  knn (int): number of KNN to propagate eige nvectors
44
- distance (str): distance metric, 'cosine' (default) or 'euclidean', 'rbf'
44
+ affinity_type (str): distance metric, 'cosine' (default) or 'euclidean', 'rbf'
45
45
  chunk_size (int): chunk size for matrix multiplication
46
46
  device (str): device to use for computation, if None, will not change device
47
47
  Returns:
@@ -66,7 +66,7 @@ def extrapolate_knn(
66
66
  for _v in torch.chunk(extrapolation_features, n_chunks, dim=0):
67
67
  _v = _v.to(device) # [_m x d]
68
68
 
69
- _A = affinity_from_features(anchor_features, _v, affinity_focal_gamma, distance).mT # [_m x n]
69
+ _A = affinity_from_features(anchor_features, _v, affinity_focal_gamma, affinity_type).mT # [_m x n]
70
70
  if knn is not None:
71
71
  _A, indices = _A.topk(k=knn, dim=-1, largest=True) # [_m x k], [_m x k]
72
72
  _anchor_output = anchor_output[indices] # [_m x k x d]
@@ -90,7 +90,7 @@ def extrapolate_knn_with_subsampling(
90
90
  full_output: torch.Tensor, # [n x d']
91
91
  extrapolation_features: torch.Tensor, # [m x d]
92
92
  sample_config: SampleConfig,
93
- distance: DistanceOptions,
93
+ affinity_type: AffinityOptions,
94
94
  knn: int = 10, # k
95
95
  affinity_focal_gamma: float = 1.0,
96
96
  chunk_size: int = 8192,
@@ -122,7 +122,7 @@ def extrapolate_knn_with_subsampling(
122
122
  # sample subgraph
123
123
  anchor_indices = subsample_features(
124
124
  features=full_features,
125
- disttype=distance,
125
+ distance_type=AFFINITY_TO_DISTANCE[affinity_type],
126
126
  config=sample_config,
127
127
  )
128
128
 
@@ -135,7 +135,7 @@ def extrapolate_knn_with_subsampling(
135
135
  anchor_features,
136
136
  anchor_output,
137
137
  extrapolation_features,
138
- distance,
138
+ affinity_type,
139
139
  knn=knn,
140
140
  affinity_focal_gamma=affinity_focal_gamma,
141
141
  chunk_size=chunk_size,
@@ -148,7 +148,7 @@ def extrapolate_knn_with_subsampling(
148
148
  def _rgb_with_dimensionality_reduction(
149
149
  features: torch.Tensor,
150
150
  num_sample: int,
151
- disttype: Literal["cosine", "euclidean"],
151
+ affinity_type: AffinityOptions,
152
152
  rgb_func: Callable[[torch.Tensor, float], torch.Tensor],
153
153
  q: float,
154
154
  knn: int,
@@ -162,26 +162,26 @@ def _rgb_with_dimensionality_reduction(
162
162
  if True:
163
163
  _subgraph_indices = subsample_features(
164
164
  features=features,
165
- disttype=disttype,
165
+ distance_type=AFFINITY_TO_DISTANCE[affinity_type],
166
166
  config=SampleConfig(method="fps"),
167
167
  )
168
168
  features = extrapolate_knn(
169
169
  anchor_features=features[_subgraph_indices],
170
170
  anchor_output=features[_subgraph_indices],
171
171
  extrapolation_features=features,
172
- distance=disttype,
172
+ affinity_type=affinity_type,
173
173
  )
174
174
 
175
175
  subgraph_indices = subsample_features(
176
176
  features=features,
177
- disttype=disttype,
177
+ distance_type=AFFINITY_TO_DISTANCE[affinity_type],
178
178
  config=SampleConfig(method="fps", num_sample=num_sample),
179
179
  )
180
180
 
181
181
  _inp = features[subgraph_indices].numpy(force=True)
182
182
  _subgraph_embed = torch.tensor(reduction(
183
183
  n_components=reduction_dim,
184
- metric=disttype,
184
+ metric=AFFINITY_TO_DISTANCE[affinity_type],
185
185
  random_state=seed,
186
186
  **reduction_kwargs
187
187
  ).fit_transform(_inp), device=features.device, dtype=features.dtype)
@@ -190,7 +190,7 @@ def _rgb_with_dimensionality_reduction(
190
190
  features[subgraph_indices],
191
191
  _subgraph_embed,
192
192
  features,
193
- disttype,
193
+ affinity_type,
194
194
  knn=knn,
195
195
  device=device,
196
196
  move_output_to_cpu=True
@@ -201,7 +201,7 @@ def _rgb_with_dimensionality_reduction(
201
201
  def rgb_from_tsne_2d(
202
202
  features: torch.Tensor,
203
203
  num_sample: int = 1000,
204
- disttype: Literal["cosine", "euclidean"] = "cosine",
204
+ affinity_type: AffinityOptions = "cosine",
205
205
  perplexity: int = 150,
206
206
  q: float = 0.95,
207
207
  knn: int = 10,
@@ -220,16 +220,12 @@ def rgb_from_tsne_2d(
220
220
  "sklearn import failed, please install `pip install scikit-learn`"
221
221
  )
222
222
  num_sample = min(num_sample, features.shape[0])
223
- if perplexity > num_sample // 2:
224
- logging.warning(
225
- f"perplexity is larger than num_sample, set perplexity to {num_sample // 2}"
226
- )
227
- perplexity = num_sample // 2
223
+ perplexity = min(perplexity, num_sample // 2)
228
224
 
229
225
  rgb = _rgb_with_dimensionality_reduction(
230
226
  features=features,
231
227
  num_sample=num_sample,
232
- disttype=disttype,
228
+ affinity_type=affinity_type,
233
229
  rgb_func=rgb_from_2d_colormap,
234
230
  q=q,
235
231
  knn=knn,
@@ -245,7 +241,7 @@ def rgb_from_tsne_2d(
245
241
  def rgb_from_tsne_3d(
246
242
  features: torch.Tensor,
247
243
  num_sample: int = 1000,
248
- disttype: Literal["cosine", "euclidean"] = "cosine",
244
+ affinity_type: AffinityOptions = "cosine",
249
245
  perplexity: int = 150,
250
246
  q: float = 0.95,
251
247
  knn: int = 10,
@@ -264,16 +260,12 @@ def rgb_from_tsne_3d(
264
260
  "sklearn import failed, please install `pip install scikit-learn`"
265
261
  )
266
262
  num_sample = min(num_sample, features.shape[0])
267
- if perplexity > num_sample // 2:
268
- logging.warning(
269
- f"perplexity is larger than num_sample, set perplexity to {num_sample // 2}"
270
- )
271
- perplexity = num_sample // 2
263
+ perplexity = min(perplexity, num_sample // 2)
272
264
 
273
265
  rgb = _rgb_with_dimensionality_reduction(
274
266
  features=features,
275
267
  num_sample=num_sample,
276
- disttype=disttype,
268
+ affinity_type=affinity_type,
277
269
  rgb_func=rgb_from_3d_rgb_cube,
278
270
  q=q,
279
271
  knn=knn,
@@ -289,7 +281,7 @@ def rgb_from_tsne_3d(
289
281
  def rgb_from_euclidean_tsne_3d(
290
282
  features: torch.Tensor,
291
283
  num_sample: int = 1000,
292
- disttype: Literal["cosine", "euclidean"] = "cosine",
284
+ affinity_type: AffinityOptions = "cosine",
293
285
  perplexity: int = 150,
294
286
  q: float = 0.95,
295
287
  knn: int = 10,
@@ -308,19 +300,15 @@ def rgb_from_euclidean_tsne_3d(
308
300
  "sklearn import failed, please install `pip install scikit-learn`"
309
301
  )
310
302
  num_sample = min(num_sample, features.shape[0])
311
- if perplexity > num_sample // 2:
312
- logging.warning(
313
- f"perplexity is larger than num_sample, set perplexity to {num_sample // 2}"
314
- )
315
- perplexity = num_sample // 2
303
+ perplexity = min(perplexity, num_sample // 2)
316
304
 
317
305
  def rgb_func(X_3d: torch.Tensor, q: float) -> torch.Tensor:
318
- return rgb_from_3d_rgb_cube(to_euclidean(X_3d, disttype), q=q)
306
+ return rgb_from_3d_rgb_cube(to_euclidean(X_3d, AFFINITY_TO_DISTANCE[affinity_type]), q=q)
319
307
 
320
308
  rgb = _rgb_with_dimensionality_reduction(
321
309
  features=features,
322
310
  num_sample=num_sample,
323
- disttype="cosine",
311
+ affinity_type=affinity_type,
324
312
  rgb_func=rgb_func,
325
313
  q=q,
326
314
  knn=knn,
@@ -336,7 +324,7 @@ def rgb_from_euclidean_tsne_3d(
336
324
  def rgb_from_umap_2d(
337
325
  features: torch.Tensor,
338
326
  num_sample: int = 1000,
339
- disttype: Literal["cosine", "euclidean"] = "cosine",
327
+ affinity_type: AffinityOptions = "cosine",
340
328
  n_neighbors: int = 150,
341
329
  min_dist: float = 0.1,
342
330
  q: float = 0.95,
@@ -357,7 +345,7 @@ def rgb_from_umap_2d(
357
345
  rgb = _rgb_with_dimensionality_reduction(
358
346
  features=features,
359
347
  num_sample=num_sample,
360
- disttype=disttype,
348
+ affinity_type=affinity_type,
361
349
  rgb_func=rgb_from_2d_colormap,
362
350
  q=q,
363
351
  knn=knn,
@@ -374,7 +362,7 @@ def rgb_from_umap_2d(
374
362
  def rgb_from_umap_sphere(
375
363
  features: torch.Tensor,
376
364
  num_sample: int = 1000,
377
- disttype: Literal["cosine", "euclidean"] = "cosine",
365
+ affinity_type: AffinityOptions = "cosine",
378
366
  n_neighbors: int = 150,
379
367
  min_dist: float = 0.1,
380
368
  q: float = 0.95,
@@ -402,7 +390,7 @@ def rgb_from_umap_sphere(
402
390
  rgb = _rgb_with_dimensionality_reduction(
403
391
  features=features,
404
392
  num_sample=num_sample,
405
- disttype=disttype,
393
+ affinity_type=affinity_type,
406
394
  rgb_func=rgb_func,
407
395
  q=q,
408
396
  knn=knn,
@@ -420,7 +408,7 @@ def rgb_from_umap_sphere(
420
408
  def rgb_from_umap_3d(
421
409
  features: torch.Tensor,
422
410
  num_sample: int = 1000,
423
- disttype: Literal["cosine", "euclidean"] = "cosine",
411
+ affinity_type: AffinityOptions = "cosine",
424
412
  n_neighbors: int = 150,
425
413
  min_dist: float = 0.1,
426
414
  q: float = 0.95,
@@ -441,7 +429,7 @@ def rgb_from_umap_3d(
441
429
  rgb = _rgb_with_dimensionality_reduction(
442
430
  features=features,
443
431
  num_sample=num_sample,
444
- disttype=disttype,
432
+ affinity_type=affinity_type,
445
433
  rgb_func=rgb_from_3d_rgb_cube,
446
434
  q=q,
447
435
  knn=knn,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: nystrom_ncut
3
- Version: 0.2.2
3
+ Version: 0.3.1
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,18 @@
1
+ __init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ nystrom_ncut/__init__.py,sha256=tKq9-2QRNFetckHY77qAaKEMjMCYTYcorS2f74aNtvk,540
3
+ nystrom_ncut/common.py,sha256=eie19AHTMk6AGTxNnYq1UcFkHJVimeywAUYryXwaiHk,2428
4
+ nystrom_ncut/distance_utils.py,sha256=pJA8NcIKyS7-YDpRGOkc7mwBQQEsYVemdkHiTjyU4n8,4300
5
+ nystrom_ncut/sampling_utils.py,sha256=6lP8F6gftl4mgkavPsD7Vuk4erj4RtgILPhcj3YqLXk,4840
6
+ nystrom_ncut/visualize_utils.py,sha256=Sfi_kKpvFFzBFoJnbo-pQpH2jhs-A6tH64SV_WGoq58,22740
7
+ nystrom_ncut/nystrom/__init__.py,sha256=1aUXK87g4cXRXqNt6XkZsfyauw1-yv3sv0NmdmkWo-8,42
8
+ nystrom_ncut/nystrom/distance_realization.py,sha256=RTI1_Q8fCUGAPSbXaVuNA-2B-11CEAfy2CwKWPJj6xQ,5830
9
+ nystrom_ncut/nystrom/normalized_cut.py,sha256=jB_QALMY3l5CFfZPsrOFpEaquTrJP17muTrDZXxzUA8,7177
10
+ nystrom_ncut/nystrom/nystrom_utils.py,sha256=hksDO8uuAb9xKoA1ZafGwXDlQN_gZJn_qHscaSoO8JE,14120
11
+ nystrom_ncut/transformer/__init__.py,sha256=jjXjcNp3LrxeF6mqG9VY5k3asrqaY6bXzJz6wTpH78Q,105
12
+ nystrom_ncut/transformer/axis_align.py,sha256=j3LlAPrp8O_jQAlwZz-gu3D7n_wICEJranye-YK5wvA,4880
13
+ nystrom_ncut/transformer/transformer_mixin.py,sha256=YAjrDWTL5Hjnk9J2OsoxvtwT2N0u8IdgMSx0rRFmZzE,1653
14
+ nystrom_ncut-0.3.1.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
15
+ nystrom_ncut-0.3.1.dist-info/METADATA,sha256=Ykb_jNjxA3yzh7-LhDeA_DO1bMTZhSK-sUaDO4bZbVM,6058
16
+ nystrom_ncut-0.3.1.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
17
+ nystrom_ncut-0.3.1.dist-info/top_level.txt,sha256=gM8IWWHYysIRTCvCTcdS4RShOyl9pxpylgSwPUZR2XM,22
18
+ nystrom_ncut-0.3.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (76.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,18 +0,0 @@
1
- __init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- nystrom_ncut/__init__.py,sha256=tKq9-2QRNFetckHY77qAaKEMjMCYTYcorS2f74aNtvk,540
3
- nystrom_ncut/common.py,sha256=_PGJoImSk_Fb_5Ri-e_IsFoCcSfbGS8CxYUUHVoNM50,2036
4
- nystrom_ncut/distance_utils.py,sha256=p-pYdpRrJsIhzxM_IxUqja7N8okngx52WGXD9pu_Aec,3129
5
- nystrom_ncut/sampling_utils.py,sha256=oMmhFcd_N_D15Ht7F0rCGPSgLeitJszAKMD3ICKwHNU,3105
6
- nystrom_ncut/visualize_utils.py,sha256=d3VXjzJPZPPyUMg_b8hKLQoBaRWvutu6u7l36S2gmIM,23007
7
- nystrom_ncut/nystrom/__init__.py,sha256=lAoO00i4FG5xqGKDO_OYcSvO4qPK64x_X_hDNBvuLUc,105
8
- nystrom_ncut/nystrom/distance_realization.py,sha256=InajllGtRVnLVlZoipZNbHFTGHaTs3zxizKe3kI2Los,5815
9
- nystrom_ncut/nystrom/normalized_cut.py,sha256=2ocwc4U3A6GGFs0cuL0DO1yNvt59SJ3uDtj00U0foPM,5906
10
- nystrom_ncut/nystrom/nystrom_utils.py,sha256=MMRMq7N_JYM2whSHIhCoPA-SQ28wb9hC8u2CZNmRRN8,12836
11
- nystrom_ncut/transformer/__init__.py,sha256=jjXjcNp3LrxeF6mqG9VY5k3asrqaY6bXzJz6wTpH78Q,105
12
- nystrom_ncut/transformer/axis_align.py,sha256=8PYtSTChHDTrh5SYdhl1ALsUUPJHd9ojQRM1e6KTbHc,3579
13
- nystrom_ncut/transformer/transformer_mixin.py,sha256=kVjODpIHB6noC7yGY-QPf67Ep58o53wrEMKSNjhhChI,1714
14
- nystrom_ncut-0.2.2.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
15
- nystrom_ncut-0.2.2.dist-info/METADATA,sha256=RGX2HMT2uF9bUB_4qecpX87bY6N1gg6-QKXvhJVnzIo,6058
16
- nystrom_ncut-0.2.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
17
- nystrom_ncut-0.2.2.dist-info/top_level.txt,sha256=gM8IWWHYysIRTCvCTcdS4RShOyl9pxpylgSwPUZR2XM,22
18
- nystrom_ncut-0.2.2.dist-info/RECORD,,