-
Notifications
You must be signed in to change notification settings - Fork 3
/
loader.py
executable file
·440 lines (321 loc) · 15.9 KB
/
loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import sys
import os
import numpy as np
from misc import make_path, default_maxedge, default_minpers, headfolder
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
'''
Script to load in various datasets whose topology we want to examine,
as well as topological data (e.g. loops) we have computed on these.
NB: both raw and filtered data should, when loaded, have shape (D, time),
where D is the dimension of the system considered.
'''
#The main load function: enter new datasets here with an appropriate name
def load_raw(name, useall=False):
datadir = headfolder + '/Data/Raw'
if name == 'Lorenz63':
fname = '%s/Lorenz63/lorenz_10k_raw.npy' % datadir
if useall:
data = (np.load(fname).T)
else:
data = (np.load(fname).T)[:,10000:10000+100000]
elif name == 'Lorenz96':
fname = '%s/Lorenz96/lorenz96_raw.txt' % datadir
if useall:
data= np.loadtxt(fname)[:,:200000]
else:
data= np.loadtxt(fname)[:,:20000]
elif name == 'CDV':
fname = '%s/CDV/cdv_deterministic.np' % datadir
if useall:
data= np.fromfile(fname).reshape([6,-1])
else:
data= np.fromfile(fname).reshape([6,-1])[:,10000:10000+40000]
elif name == 'JetLat':
lats = np.loadtxt('%s/JetLat/jetlatitude_ERA20C_djf_1901-2010.txt' % datadir)
pc0 = np.loadtxt('%s/JetLat/pc0_ua850_ERA20C_1901-2010.txt' % datadir)
pc1 = np.loadtxt('%s/JetLat/pc1_ua850_ERA20C_1901-2010.txt' % datadir)
lats /= lats.std()
pc0 /= pc0.std()
pc1 /= pc1.std()
data = np.vstack([lats, pc0, pc1])
elif name == 'Gaussian':
fname = '%s/Gaussian/gaussian_raw.txt' % datadir
data = np.loadtxt(fname)
else:
sys.exit("(load_raw) ERROR: unrecognized dataset %s" % name)
return data
#For loading filtered data
def load_filt(name, method='GaussianKDE', identifier=None,\
num_bins=160, perc_to_keep=70,\
reverse_order=False, eofs=False, normalise=False):
if method == 'timeseries':
data = load_raw(name)
from eofs.standard import Eof
EOFs=Eof(data.T)
pcdata=EOFs.pcs(npcs=num_pcs).T
data = pcdata[:,:10000]
else:
if 'Perc' in name:
orig_name, percstr = name.split('-')
perc_to_keep = int(percstr.split('Perc')[-1])
else:
orig_name = name
if eofs:
method += '_EOFs'
if perc_to_keep == 'XXX':
data = load_raw(orig_name)
else:
datadir = headfolder + '/Data/Filtered'
if identifier is None:
if reverse_order:
fname = '%s/%s/%s_filtered_leastdense_%s_percent_%sbins_%s.txt' % (datadir, orig_name, orig_name, perc_to_keep, num_bins, method)
else:
fname = '%s/%s/%s_filtered_densest_%s_percent_%sbins_%s.txt' % (datadir, orig_name, orig_name, perc_to_keep, num_bins, method)
else:
if reverse_order:
fname = '%s/%s/%s_filtered_densest_%s_percent_%sbins_%s_%s.txt' % (datadir, orig_name, orig_name, perc_to_keep, num_bins, method, identifier)
else:
fname = '%s/%s/%s_filtered_leastdense_%s_percent_%sbins_%s_%s.txt' % (datadir, orig_name, orig_name, perc_to_keep, num_bins, method, identifier)
data = np.loadtxt(fname)
if normalise:
num_dims = data.shape[0]
for n in range(num_dims):
data[n,:] = data[n,:]/data[n,:].std()
return data
#Loading the additionally sparsified data using gudhi's pre_sparse argument
def load_from_pers(name, max_edge=None, min_pers=None, sparse=0.7, pre_sparse=None,\
method='GaussianKDE', num_bins=160, reverse_order=False, eofs=False):
if 'Perc' in name:
basename, percstr = name.split('-')
perc_to_keep = percstr.split('Perc')[-1]
else:
basename = name
perc_to_keep = None
if perc_to_keep is None:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed' % basename
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard' % basename
else:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed/%s-Perc%s' % (basename, basename, perc_to_keep)
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard/%s-Perc%s' % (basename, basename, perc_to_keep)
filtdata = load_filt(name, num_bins=num_bins, method=method, reverse_order=reverse_order, eofs=eofs)
if (max_edge is None):
max_edge = default_maxedge(filtdata)
if min_pers is None:
min_pers = default_minpers(filtdata, name)
max_edge_str = "{0:.2f}".format(max_edge) #strip away decimals to avoid overly long filenames
min_pers_str = "{0:.2f}".format(min_pers)
sparse_str = "{0:.2f}".format(sparse)
if pre_sparse is None:
presparse_str = '0'
else:
presparse_str = "{0:.3f}".format(pre_sparse)
if eofs:
fname = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_f.txt' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str, presparse_str)
else:
fname = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_f.txt' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str, presparse_str)
data = []
with open(fname,'r') as f:
for i,l in enumerate(f.readlines()):
if i == 0:
nele = ([int(j) for j in l.split(" ")][-1])
if i > 0 and i <=nele:
data.append([float(j) for j in l.split(" ")])
data = np.array(data).T
return data
#For loading loops produced with PersLoop
def load_loops(name, max_edge=None, min_pers=None, sparse=0.7, pre_sparse=None,\
method='GaussianKDE', reverse_order=False, eofs=False):
import phtools as ph
if 'Perc' in name:
basename, percstr = name.split('-')
perc_to_keep = percstr.split('Perc')[-1]
else:
basename = name
perc_to_keep = None
if perc_to_keep is None:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed' % basename
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard' % basename
else:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed/%s-Perc%s' % (basename, basename, perc_to_keep)
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard/%s-Perc%s' % (basename, basename, perc_to_keep)
filtdata = load_filt(name, method=method, reverse_order=reverse_order, eofs=eofs)
if (max_edge is None):
max_edge = default_maxedge(filtdata)
if min_pers is None:
min_pers = default_minpers(filtdata, name)
max_edge_str = "{0:.2f}".format(max_edge) #strip away decimals to avoid overly long filenames
min_pers_str = "{0:.2f}".format(min_pers)
sparse_str = "{0:.2f}".format(sparse)
if pre_sparse is None:
presparse_str = '0'
else:
presparse_str = "{0:.3f}".format(pre_sparse)
if eofs:
fname = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_floops' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str, presparse_str)
else:
fname = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_floops' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str, presparse_str)
loop_numbers = ph.loopnums(fname)
loop_list = []
for l in loop_numbers:
tmp=ph.getloop(l, fname)
loop_list.append(tmp)
return loop_list
#Loading components
def load_comps(name, num2keep=5,\
max_edge=None, min_pers=None,\
sparse=0.7, pre_sparse=None,\
method='GaussianKDE', reverse_order=False,\
eofs=False):
if 'Perc' in name:
basename, percstr = name.split('-')
perc_to_keep = percstr.split('Perc')[-1]
else:
basename = name
perc_to_keep = None
if perc_to_keep is None:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed' % basename
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard' % basename
else:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed/%s-Perc%s' % (basename, basename, perc_to_keep)
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard/%s-Perc%s' % (basename, basename, perc_to_keep)
filtdata = load_filt(name, method=method, reverse_order=reverse_order, eofs=eofs)
if (max_edge is None):
max_edge = default_maxedge(filtdata)
if min_pers is None:
min_pers = default_minpers(filtdata, name)
max_edge_str = "{0:.2f}".format(max_edge) #strip away decimals to avoid overly long filenames
min_pers_str = "{0:.2f}".format(min_pers)
sparse_str = "{0:.2f}".format(sparse)
if pre_sparse is None:
presparse_str = '0'
else:
presparse_str = "{0:.3f}".format(pre_sparse)
if eofs:
fname = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps.npy' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str,\
presparse_str, num2keep)
else:
fname = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps.npy' % (outpath_root, name, method, max_edge_str, min_pers_str, sparse_str,\
presparse_str, num2keep)
components = np.load(fname, allow_pickle=True)
return components
#Loading deathtimes of components
def load_comp_deaths(name, num2keep=5,\
max_edge=None, min_pers=None,\
sparse=0.7, pre_sparse=None,\
method='GaussianKDE', reverse_order=False,\
eofs=False):
if 'Perc' in name:
basename, percstr = name.split('-')
perc_to_keep = percstr.split('Perc')[-1]
else:
basename = name
perc_to_keep = None
if perc_to_keep is None:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed' % basename
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard' % basename
else:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed/%s-Perc%s' % (basename, basename, perc_to_keep)
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard/%s-Perc%s' % (basename, basename, perc_to_keep)
filtdata = load_filt(name, method=method, reverse_order=reverse_order, eofs=eofs)
if (max_edge is None):
max_edge = default_maxedge(filtdata)
if min_pers is None:
min_pers = default_minpers(filtdata, name)
max_edge_str = "{0:.2f}".format(max_edge) #strip away decimals to avoid overly long filenames
min_pers_str = "{0:.2f}".format(min_pers)
sparse_str = "{0:.2f}".format(sparse)
if pre_sparse is None:
presparse_str = '0'
else:
presparse_str = "{0:.3f}".format(pre_sparse)
if eofs:
fname = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps_deathtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
fname_sizes = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps_sizes.npy' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
else:
fname = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps_deathtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
fname_sizes = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-comps_sizes.npy' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
death_times = np.loadtxt(fname)[:num2keep]
#Replace 0's with min_pers
death_times[death_times == 0.] = min_pers
#Get sizes of components, padding out with zeros
sizes = np.zeros(num2keep)
sizes_real = np.load(fname_sizes, allow_pickle=True)
for n in range(len(sizes_real)):
sizes[n] = sizes_real[n]
return death_times, sizes
#Loading birth/death-times of loops
def load_loop_lives(name, num2keep=5,\
max_edge=None, min_pers=None,\
sparse=0.7, pre_sparse=None,\
method='GaussianKDE', reverse_order=False,
eofs=False):
if 'Perc' in name:
basename, percstr = name.split('-')
perc_to_keep = percstr.split('Perc')[-1]
else:
basename = name
perc_to_keep = None
if perc_to_keep is None:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed' % basename
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard' % basename
else:
if reverse_order:
outpath_root = headfolder + '/Data/Processed/Loops/%s/reversed/%s-Perc%s' % (basename, basename, perc_to_keep)
else:
outpath_root = headfolder + '/Data/Processed/Loops/%s/standard/%s-Perc%s' % (basename, basename, perc_to_keep)
filtdata = load_filt(name, method=method, reverse_order=reverse_order, eofs=eofs)
if (max_edge is None):
max_edge = default_maxedge(filtdata)
if min_pers is None:
min_pers = default_minpers(filtdata, name)
max_edge_str = "{0:.2f}".format(max_edge) #strip away decimals to avoid overly long filenames
min_pers_str = "{0:.2f}".format(min_pers)
sparse_str = "{0:.2f}".format(sparse)
if pre_sparse is None:
presparse_str = '0'
else:
presparse_str = "{0:.3f}".format(pre_sparse)
if eofs:
fname_births = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-loops_birthtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
fname_deaths = '%s/%s_%s_EOFs_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-loops_deathtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
else:
fname_births = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-loops_birthtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
fname_deaths = '%s/%s_%s_maxe_%s_minp_%s_sp_%s_presp_%s_%s-oldest-loops_deathtimes.txt' % (outpath_root, name, method, max_edge_str, min_pers_str,\
sparse_str, presparse_str, num2keep)
birth_times = np.loadtxt(fname_births)
death_times = np.loadtxt(fname_deaths)
if birth_times.shape:
births = birth_times[:num2keep]
else:
births = np.array([birth_times])
if death_times.shape:
deaths = death_times[:num2keep]
else:
deaths = np.array([death_times])
return births, deaths