driada 0.0.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.
Files changed (57) hide show
  1. __init__.py +0 -0
  2. dim_reduction/__init__.py +0 -0
  3. dim_reduction/data.py +117 -0
  4. dim_reduction/dim_reduction.py +75 -0
  5. dim_reduction/dr_base.py +111 -0
  6. dim_reduction/embedding.py +485 -0
  7. dim_reduction/graph.py +242 -0
  8. dim_reduction/mvu.py +146 -0
  9. dim_reduction/neural.py +182 -0
  10. dimensionality/__init__.py +0 -0
  11. dimensionality/effective.py +45 -0
  12. dimensionality/utils.py +47 -0
  13. driada-0.0.1.dist-info/METADATA +58 -0
  14. driada-0.0.1.dist-info/RECORD +57 -0
  15. driada-0.0.1.dist-info/WHEEL +5 -0
  16. driada-0.0.1.dist-info/top_level.txt +10 -0
  17. experiment/__init__.py +1 -0
  18. experiment/exp_base.py +614 -0
  19. experiment/exp_build.py +182 -0
  20. experiment/neuron.py +255 -0
  21. experiment/wavelet_event_detection.py +271 -0
  22. experiment/wavelet_ridge.py +107 -0
  23. gdrive/__init__.py +0 -0
  24. gdrive/auth.py +18 -0
  25. gdrive/download.py +173 -0
  26. gdrive/gdrive_utils.py +222 -0
  27. gdrive/upload.py +61 -0
  28. information/__init__.py +0 -0
  29. information/gcmi.py +250 -0
  30. information/info_base.py +239 -0
  31. information/info_utils.py +47 -0
  32. information/ksg.py +162 -0
  33. intense/__init__.py +0 -0
  34. intense/intense_base.py +509 -0
  35. intense/pipelines.py +238 -0
  36. intense/stats.py +193 -0
  37. intense/visual.py +131 -0
  38. network/__init__.py +0 -0
  39. network/drawing.py +205 -0
  40. network/graph_utils.py +75 -0
  41. network/matrix_utils.py +316 -0
  42. network/net_base.py +596 -0
  43. network/quantum.py +55 -0
  44. network/randomization.py +340 -0
  45. network/spectral.py +55 -0
  46. signals/__init__.py +0 -0
  47. signals/complexity.py +19 -0
  48. signals/sig_base.py +144 -0
  49. signals/theory.py +66 -0
  50. utils/__init__.py +0 -0
  51. utils/data.py +143 -0
  52. utils/gif.py +41 -0
  53. utils/matrix.py +54 -0
  54. utils/naming.py +33 -0
  55. utils/output.py +20 -0
  56. utils/plot.py +45 -0
  57. utils/repo.py +45 -0
__init__.py ADDED
File without changes
File without changes
dim_reduction/data.py ADDED
@@ -0,0 +1,117 @@
1
+ # @title Data class { form-width: "200px" }
2
+
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import scipy.sparse as sp
6
+
7
+ from .dr_base import *
8
+ from src.utils.data import correlation_matrix, to_numpy_array, rescale
9
+ from .embedding import Embedding
10
+ from .graph import ProximityGraph
11
+
12
+ # TODO: refactor this
13
+ def check_data_for_errors(d):
14
+ sums = np.sum(np.abs(d), axis=0)
15
+ if len(sums.nonzero()[1]) != d.shape[1]:
16
+ bad_points = np.where(sums == 0)[1]
17
+ print('zero points:', bad_points)
18
+ print(d.A[:, bad_points[0]])
19
+ raise Exception('Data contains zero points!')
20
+
21
+
22
+ class MVData(object):
23
+ '''
24
+ Main class for multivariate data storage & processing
25
+ '''
26
+
27
+ def __init__(self,
28
+ data,
29
+ labels=None,
30
+ distmat=None,
31
+ rescale_rows=False,
32
+ data_name=None,
33
+ downsampling=None):
34
+
35
+ if downsampling is None:
36
+ self.ds = 1
37
+ else:
38
+ self.ds = int(downsampling)
39
+
40
+ self.data = to_numpy_array(data)[:, ::self.ds]
41
+
42
+ # TODO: add support for various preprocessing methods (wvt, med_filt, etc.)
43
+ self.rescale_rows = rescale_rows
44
+ if self.rescale_rows:
45
+ for i, row in enumerate(self.data):
46
+ self.data[i] = rescale(row)
47
+
48
+ self.data_name = data_name
49
+ self.n_dim = self.data.shape[0]
50
+ self.n_points = self.data.shape[1]
51
+
52
+ if labels is None:
53
+ self.labels = np.zeros(self.n_points)
54
+ else:
55
+ self.labels = to_numpy_array(labels)
56
+
57
+ self.distmat = distmat
58
+
59
+ def median_filter(self, window):
60
+ from scipy.signal import medfilt
61
+ d = self.data.A
62
+
63
+ new_d = medfilt(d, window)
64
+
65
+ self.data = sp.csr_matrix(new_d)
66
+
67
+ def corr_mat(self):
68
+ cm = correlation_matrix(self.data)
69
+ return cm
70
+
71
+ def get_distmat(self, method='euclidean'):
72
+ raise NotImplementedError('To be implemented soon')
73
+
74
+ def get_embedding(self, e_params, g_params=None, m_params=None, kwargs=None):
75
+ method = e_params['e_method']
76
+ method_name = e_params['e_method_name']
77
+
78
+ if method_name not in EMBEDDING_CONSTRUCTION_METHODS:
79
+ raise Exception('Unknown embedding construction method!')
80
+
81
+ graph = None
82
+ if method.requires_graph:
83
+ if g_params is None:
84
+ raise ValueError(f'Method {method_name} requires proximity graph, but '
85
+ f'graph params were not provided')
86
+ if g_params['weighted'] and m_params is None:
87
+ raise ValueError(f'Method {method_name} requires weights for proximity graph, but '
88
+ f'metric params were not provided')
89
+
90
+ graph = self.get_proximity_graph(m_params, g_params)
91
+
92
+ if method.requires_distmat and self.distmat is None:
93
+ raise Exception(f'No distmat provided for {method_name} method.'
94
+ f' Try constructing it first with get_distmat() method')
95
+
96
+ emb = Embedding(self.data, self.distmat, self.labels, e_params, g=graph)
97
+ emb.build(kwargs=kwargs)
98
+
99
+ return emb
100
+
101
+ def get_proximity_graph(self, m_params, g_params):
102
+ if g_params['g_method_name'] not in GRAPH_CONSTRUCTION_METHODS:
103
+ raise Exception('Unknown graph construction method!')
104
+
105
+ graph = ProximityGraph(self.data, m_params, g_params)
106
+ # print('Graph succesfully constructed')
107
+ return graph
108
+
109
+ def draw_vector(self, num):
110
+ data = self.data[:, num]
111
+ plt.matshow(data.reshape(1, self.n_dim))
112
+ plt.matshow(self.data)
113
+
114
+ def draw_row(self, num):
115
+ data = self.data[num, :]
116
+ plt.figure(figsize=(12, 10))
117
+ plt.plot(data)
@@ -0,0 +1,75 @@
1
+ from .dr_base import *
2
+ from .data import MVData
3
+ from .graph import ProximityGraph
4
+ from .embedding import Embedding
5
+
6
+
7
+ # TODO: refactor this
8
+ def dr_series(d,
9
+ n_jumps,
10
+ all_metric_params,
11
+ all_graph_params,
12
+ all_embedding_params,
13
+ recalculate_if_error=0):
14
+
15
+ print('--------------------------- JUMP 1 --------------------------------')
16
+ print('Performing jump from dim', d.dim, 'to dim', all_embedding_params['dim'][0], ':')
17
+ metric_params = dict(zip(all_metric_params.keys(), [val[0] for val in all_metric_params.values()]))
18
+ graph_params = dict(zip(all_graph_params.keys(), [val[0] for val in all_graph_params.values()]))
19
+ embedding_params = dict(zip(all_embedding_params.keys(), [val[0] for val in all_embedding_params.values()]))
20
+ embedding_params['e_method'] = METHODS_DICT[embedding_params['e_method_name']]
21
+
22
+ m_params = m_param_filter(metric_params)
23
+ g_params = g_param_filter(graph_params)
24
+ e_params = e_param_filter(embedding_params)
25
+
26
+ maxiter = 20
27
+ n_iter = maxiter * int(recalculate_if_error) + 1
28
+ it = 0
29
+ success = 0
30
+ while it < n_iter and success == 0:
31
+ try:
32
+ emb = d.get_embedding(m_params, g_params, e_params)
33
+ success = 1
34
+ except:
35
+ it += 1
36
+ print("Unexpected error:", sys.exc_info()[0])
37
+ raise
38
+
39
+ if it == n_iter:
40
+ raise Exception('First jump failed after %s attempts' % n_iter)
41
+
42
+ if n_jumps > 1:
43
+ datalist = [d, MVData(emb.coords, labels=d.labels)]
44
+
45
+ for i in range(1, n_jumps):
46
+ print('--------------------------- JUMP ' + str(i + 1) + ' --------------------------------', )
47
+ print('Performing jump from dim', datalist[i].dim, 'to dim', all_embedding_params['dim'][i], ':')
48
+ metric_params = dict(zip(all_metric_params.keys(), [val[i] for val in all_metric_params.values()]))
49
+ graph_params = dict(zip(all_graph_params.keys(), [val[i] for val in all_graph_params.values()]))
50
+ embedding_params = dict(zip(all_embedding_params.keys(), [val[i] for val in all_embedding_params.values()]))
51
+ embedding_params['e_method'] = METHODS_DICT[embedding_params['e_method_name']]
52
+
53
+ m_params = m_param_filter(metric_params)
54
+ g_params = g_param_filter(graph_params)
55
+ e_params = e_param_filter(embedding_params)
56
+
57
+ maxiter = 20
58
+ n_iter = maxiter * (recalculate_if_error) + 1
59
+ it = 0
60
+ success = 0
61
+
62
+ while it < n_iter and success == 0:
63
+ try:
64
+ emb = datalist[i].get_embedding(m_params, g_params, e_params)
65
+ success = 1
66
+ except:
67
+ print('iter', it)
68
+ it += 1
69
+
70
+ if it == n_iter:
71
+ raise Exception('Jump ', str(i + 1), ' failed after %s attempts' % n_iter)
72
+
73
+ datalist.append(MVData(emb.coords, labels=d.labels))
74
+
75
+ return emb
@@ -0,0 +1,111 @@
1
+ from pynndescent.distances import named_distances
2
+ import sys
3
+
4
+ class DRMethod(object):
5
+
6
+ def __init__(self, is_linear, requires_graph, requires_distmat, nn_based):
7
+ self.is_linear = is_linear
8
+ self.requires_graph = requires_graph
9
+ self.requires_distmat = requires_distmat
10
+ self.nn_based = nn_based
11
+
12
+
13
+ METHODS_DICT = {
14
+ 'pca': DRMethod(1, 0, 0, 0),
15
+ 'le': DRMethod(0, 1, 0, 0),
16
+ 'auto_le': DRMethod(0, 1, 0, 0),
17
+ 'dmaps': DRMethod(0, 1, 0, 0),
18
+ 'auto_dmaps': DRMethod(0, 1, 0, 0),
19
+ 'mds': DRMethod(0, 0, 1, 0),
20
+ 'isomap': DRMethod(0, 1, 0, 0),
21
+ 'lle': DRMethod(0, 1, 0, 0),
22
+ 'hlle': DRMethod(0, 1, 0, 0),
23
+ 'mvu': DRMethod(0, 1, 0, 0),
24
+ 'ae': DRMethod(0, 0, 0, 1),
25
+ 'vae': DRMethod(0, 0, 0, 1),
26
+ 'tsne': DRMethod(0, 0, 0, 0),
27
+ 'umap': DRMethod(0, 1, 0, 0)
28
+ }
29
+
30
+ GRAPH_CONSTRUCTION_METHODS = ['knn', 'auto_knn', 'eps', 'eknn', 'umap', 'tsne']
31
+
32
+ EMBEDDING_CONSTRUCTION_METHODS = ['pca',
33
+ 'le',
34
+ 'auto_le',
35
+ 'dmaps',
36
+ 'auto_dmaps',
37
+ 'mds',
38
+ 'isomap',
39
+ 'lle',
40
+ 'hlle',
41
+ 'mvu',
42
+ 'ae',
43
+ 'vae',
44
+ 'tsne',
45
+ 'umap']
46
+
47
+ # TODO: implement random projections
48
+
49
+
50
+ def m_param_filter(para):
51
+ '''
52
+ This function prunes parameters that are excessive for
53
+ chosen distance matrix construction method
54
+ '''
55
+ name = para['metric_name']
56
+ appr_keys = ['metric_name']
57
+
58
+ if not (para['sigma'] is None):
59
+ appr_keys.append('sigma')
60
+
61
+ if name not in named_distances:
62
+ if name == 'hyperbolic':
63
+ # para['metric_name'] = globals()[name]
64
+ pass
65
+ else:
66
+ raise Exception('this custom metric is not implemented!')
67
+
68
+ if name == 'minkowski':
69
+ appr_keys.append('p')
70
+
71
+ return {key: para[key] for key in appr_keys}
72
+
73
+
74
+ def g_param_filter(para):
75
+ '''
76
+ This function prunes parameters that are excessive for
77
+ chosen graph construction method
78
+ '''
79
+ gmethod = para['g_method_name']
80
+ appr_keys = ['g_method_name', 'max_deleted_nodes', 'weighted', 'dist_to_aff']
81
+
82
+ if gmethod in ['knn', 'auto_knn', 'umap']:
83
+ appr_keys.extend(['nn'])
84
+
85
+ elif gmethod == 'eps':
86
+ appr_keys.extend(['eps', 'eps_min'])
87
+
88
+ elif gmethod == 'eknn':
89
+ appr_keys.extend(['eps', 'eps_min', 'nn'])
90
+
91
+ elif gmethod == 'tsne':
92
+ appr_keys.extend(['perplexity'])
93
+
94
+ return {key: para[key] for key in appr_keys}
95
+
96
+
97
+ def e_param_filter(para):
98
+ '''
99
+ This function prunes parameters that are excessive for the
100
+ chosen embedding construction method
101
+ '''
102
+
103
+ appr_keys = ['e_method', 'e_method_name', 'dim']
104
+
105
+ if para['e_method_name'] == 'umap':
106
+ appr_keys.append('min_dist')
107
+
108
+ if para['e_method_name'] in ['dmaps', 'auto_dmaps']:
109
+ appr_keys.append('dm_alpha')
110
+
111
+ return {key: para[key] for key in appr_keys}