TTK
Loading...
Searching...
No Matches
dimensionReduction.py
Go to the documentation of this file.
1def doIt(X, method, ncomponents, nneighbors, njobs, rstate, params):
2
3 import importlib.util
4
5 # if numpy, scipy and sklearn are not found
6 not_found = False
7
8 # check if modules are installed
9 for mod in ["numpy", "scipy", "sklearn"]:
10 if importlib.util.find_spec(mod) is None:
11 not_found = True
12 print("[DimensionReduction] Python error: " + mod + " module not found.")
13
14 if not_found:
15 # at least one module is not installed, aborting...
16 return 0
17
18 from sklearn import manifold
19 from sklearn import decomposition
20 import numpy as np
21 from sys import platform
22
23 if platform == "darwin":
24 import sklearn
25
26 sklearn.utils.parallel_backend("threading")
27
28 if rstate > 0:
29 np.random.seed(0)
30
31 try:
32 if method == 0:
33 seParams = params[0]
34 if seParams[2] == "None":
35 se = manifold.SpectralEmbedding(
36 n_components=ncomponents,
37 affinity=seParams[0],
38 gamma=seParams[1],
39 eigen_solver=None,
40 n_neighbors=nneighbors,
41 n_jobs=njobs,
42 )
43 else:
44 se = manifold.SpectralEmbedding(
45 n_components=ncomponents,
46 affinity=seParams[0],
47 gamma=seParams[1],
48 eigen_solver=seParams[2],
49 n_neighbors=nneighbors,
50 n_jobs=njobs,
51 )
52 Y = se.fit_transform(X)
53 elif method == 1:
54 lleParams = params[1]
55 lle = manifold.LocallyLinearEmbedding(
56 n_neighbors=nneighbors,
57 n_components=ncomponents,
58 reg=lleParams[0],
59 eigen_solver=lleParams[1],
60 tol=lleParams[2],
61 max_iter=lleParams[3],
62 method=lleParams[4],
63 hessian_tol=lleParams[5],
64 modified_tol=lleParams[6],
65 neighbors_algorithm=lleParams[7],
66 n_jobs=njobs,
67 )
68 Y = lle.fit_transform(X)
69 elif method == 2:
70 mdsParams = params[2]
71 mds = manifold.MDS(
72 n_components=ncomponents,
73 metric=mdsParams[0],
74 n_init=mdsParams[1],
75 max_iter=mdsParams[2],
76 verbose=mdsParams[3],
77 eps=mdsParams[4],
78 dissimilarity=mdsParams[5],
79 n_jobs=njobs,
80 )
81 Y = mds.fit_transform(X)
82 elif method == 3:
83 tsneParams = params[3]
84 tsne = manifold.TSNE(
85 n_components=ncomponents,
86 perplexity=tsneParams[0],
87 early_exaggeration=tsneParams[1],
88 learning_rate=tsneParams[2],
89 n_iter=tsneParams[3],
90 n_iter_without_progress=tsneParams[4],
91 min_grad_norm=tsneParams[5],
92 metric=tsneParams[6],
93 init=tsneParams[7],
94 verbose=tsneParams[8],
95 method=tsneParams[9],
96 angle=tsneParams[10],
97 )
98 Y = tsne.fit_transform(X)
99 elif method == 4:
100 isoParams = params[4]
101 iso = manifold.Isomap(
102 n_neighbors=nneighbors,
103 n_components=ncomponents,
104 eigen_solver=isoParams[0],
105 tol=isoParams[1],
106 max_iter=isoParams[2],
107 path_method=isoParams[3],
108 neighbors_algorithm=isoParams[4],
109 metric=isoParams[5],
110 n_jobs=njobs,
111 )
112 Y = iso.fit_transform(X)
113 elif method == 5:
114 pcaParams = params[5]
115 if pcaParams[4] == "auto":
116 pca = decomposition.PCA(
117 n_components=ncomponents,
118 copy=pcaParams[0],
119 whiten=pcaParams[1],
120 svd_solver=pcaParams[2],
121 tol=pcaParams[3],
122 iterated_power="auto",
123 )
124 else:
125 pca = decomposition.PCA(
126 n_components=ncomponents,
127 copy=pcaParams[0],
128 whiten=pcaParams[1],
129 svd_solver=pcaParams[2],
130 tol=pcaParams[3],
131 iterated_power=int(pcaParams[4]),
132 )
133 Y = pca.fit_transform(X)
134
135 L = [Y.shape[0], Y.shape[1], np.ravel(Y, "F")]
136 return L
137 except Exception as inst:
138 print(
139 "[DimensionReduction] Error: unexpected behaviour detected in the python script."
140 )
141 print(type(inst)) # the exception instance
142 print(inst.args) # arguments stored in .args
143 print(inst)
144 return []
doIt(X, method, ncomponents, nneighbors, njobs, rstate, params)