-
Notifications
You must be signed in to change notification settings - Fork 4
/
v_save.py
executable file
·712 lines (535 loc) · 19.9 KB
/
v_save.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# -*- coding: utf-8 -*-
"""This module saves and retrieves data, dealing with overwriting.
It could be divided into 2 main sections:
(1) making new directories and free files to avoid overwriting
(`new_dir`, `free_file`)
(2) saving data into files with the option of not overwriting
(`saveplot`, `savetext`, `savewav`)
(3) loading or retrieving data saved with this module
(`retrieve_header`, `retrieve_footer`)
new_dir : function
Makes and returns a new related directory to avoid overwriting.
free_file : function
Returns a name for a new file to avoid overwriting.
saveplot : function
Saves a matplotlib.pyplot plot on an image file (i.e: 'png').
savetxt : function
Saves some np.array like data on a '.txt' file.
savewav : function
Saves a PyAudio encoded audio on a '.wav' file.
saveanimation : function
Saves a matplotlib.animation object as '.gif' or '.mp4'.
@author: Vall
"""
from datetime import datetime
import h5py as h5
import matplotlib.pyplot as plt
import numpy as np
import os
import re
from socket import gethostname
import v_utilities as vu
#%%
def init_system(interactive=False):
if interactive:
sysname = input("Choose a nickname for your current PC; i.e. MC\n\n")
answer = input(f"Is this repository's directory correct?\n{os.getcwd()}\nAnswer yes or no (Y/N)\n\n")
if "y" in answer.lower():
syshome = os.getcwd()
else:
syshome = input("Copy and paste your code repository's full directory;\n"+
"i.e. C:\\Users\\Me\\SimplePixelSimulations\n\n")
home = input("Copy and paste your results folder full directory;\n"+
"i.e. C:\\Users\\Me\\SimplePixelSimulationsResults\n\n")
if not os.path.isdir(home):
os.makedirs(home)
print("Results folder was not found so it has been created")
sysname_definition = {gethostname(): sysname}
syshome_definition = {gethostname(): syshome}
home_definition = {gethostname(): home}
else:
sysname_definition = {"Nano": "SC", "vall": "MC", "else": "TC"}
home_definition = {"Nano": "/home/nanofisica/Documents/Vale/PyMeepResults",
"SC": "/home/vall/Documents/Thesis/PyMeepResults",
"vall": "/nfs/home/vpais/PyMeepResults"}
syshome_definition = {"Nano": "/home/nanofisica/Documents/Vale/PyMeepPlasmonics",
"SC": "/home/vall/Documents/Thesis/PyMeepPlasmonics",
"vall": "/nfs/home/vpais/PyMeepPlasmonics"}
default_system = dict(sysname_definition=sysname_definition,
home_definition=home_definition,
syshome_definition=syshome_definition)
try:
savetxt(os.path.join(syshome, "SystemDirectories.txt"),
np.array([]), footer=default_system, overwrite=True)
except UnboundLocalError:
savetxt(os.path.join(os.getcwd(), "SystemDirectories.txt"),
np.array([]), footer=default_system, overwrite=True)
return
#%%
def add_or_edit_system(sysname, syshome, home=None):
default_path = os.getcwd()
current_system = retrieve_footer(os.path.join(default_path,
"SystemDirectories.txt"))
hostname = gethostname()
if hostname in current_system["sysname"].keys():
print("Current host's system will be updated")
else:
print("New system entry will be created for current host")
if home is None:
home = current_system["home"][hostname]
current_system["sysname"][hostname] = sysname
current_system["syshome"][hostname] = syshome
current_system["home"][hostname] = home
#%%
def setup_system():
default_path = os.getcwd()
current_system = retrieve_footer(os.path.join(default_path,
"SystemDirectories.txt"))
sysname_definition = current_system["sysname_definition"]
home_definition = current_system["home_definition"]
syshome_definition = current_system["syshome_definition"]
return sysname_definition, home_definition, syshome_definition
#%%
def get_sys_name():
"""Returns system name according to which CPU is running"""
string = gethostname()
try: return sysname_definition[string]
except:
try: return sysname_definition["else"]
except: raise ValueError("Your PC must appear inside sysname definition")
#%%
def get_home():
"""Returns home path for results according to which CPU is running"""
string = gethostname()
try: return home_definition[string]
except:
try: return home_definition["else"]
except: raise ValueError("Your PC must appear inside home definition")
#%%
def get_sys_home():
"""Returns home path for repository according to which CPU is running"""
string = gethostname()
try: return syshome_definition[string]
except:
try: return syshome_definition["else"]
except: raise ValueError("Your PC must appear inside syshome definition")
#%%
def datetime_dir(path, strftime="(%Y-%m-%d)(%H:%M:%S)"):
"""Returns a path decorated with timestamp at the end.
Takes the path to both a file or a directory.
Parameters
----------
path : str
Original path.
strftime="(%Y-%m-%d)(%H:%M:%S)" : str
Date and time formatter.
Returns
-------
new_dir : str
New path, containing the timestamp.
See Also
--------
datetime.date.strftime
"""
date = datetime.now().strftime(strftime)
base = os.path.split(path)[0]
name = os.path.splitext(os.path.split(path)[-1])[0]
extension = os.path.splitext(path)[-1]
new_path = os.path.join(base, name+date+extension)
return new_path
#%%
def new_dir(my_dir, newformat='{}_{}'):
"""Makes and returns a new directory to avoid overwriting.
Takes a directory name 'my_dir' and checks whether it already
exists. If it doesn't, it returns 'dirname'. If it does, it
returns a related unoccupied directory name. In both cases,
the returned directory is initialized.
Parameters
----------
my_dir : str
Desired directory (should also contain full path).
Returns
-------
new_dir : str
New directory (contains full path)
Yields
------
new_dir : directory
"""
sepformat = newformat.split('{}')
base = os.path.split(my_dir)[0]
new_dir = my_dir
while os.path.isdir(new_dir):
new_dir = os.path.basename(new_dir)
new_dir = new_dir.split(sepformat[-2])[-1]
try:
new_dir = new_dir.split(sepformat[-1])[0]
except ValueError:
new_dir = new_dir
try:
new_dir = newformat.format(my_dir, str(int(new_dir)+1))
except ValueError:
new_dir = newformat.format(my_dir, 2)
new_dir = os.path.join(base, new_dir)
os.makedirs(new_dir)
return new_dir
#%%
def free_file(my_file, newformat='{}_{}'):
"""Returns a name for a new file to avoid overwriting.
Takes a file name 'my_file'. It returns a related unnocupied
file name 'free_file'. If necessary, it makes a new
directory to agree with 'my_file' path.
Parameters
----------
my_file : str
Tentative file name (must contain full path and extension).
newformat='{}_{}' : str
Format string that indicates how to make new names.
Returns
-------
new_fname : str
Unoccupied file name (also contains full path and extension).
"""
base = os.path.split(my_file)[0]
extension = os.path.splitext(my_file)[-1]
if not os.path.isdir(base):
os.makedirs(base)
free_file = my_file
else:
sepformat = newformat.split('{}')[-2]
free_file = my_file
while os.path.isfile(free_file):
free_file = os.path.splitext(free_file)[0]
free_file = free_file.split(sepformat)
number = free_file[-1]
free_file = free_file[0]
try:
free_file = newformat.format(
free_file,
str(int(number)+1),
)
except ValueError:
free_file = newformat.format(
os.path.splitext(my_file)[0],
2)
free_file = os.path.join(base, free_file+extension)
return free_file
#%%
def new_name(name, newseparator='_'):
'''Returns a name of a unique file or directory so as to not overwrite.
If proposed name existed, will return name + newseparator + number.
Parameters:
-----------
name : str (path)
proposed file or directory name influding file extension
nweseparator : str
separator between original name and index that gives unique name
Returns:
--------
name : str
unique namefile using input 'name' as template
'''
#if file is a directory, extension will be empty
base, extension = os.path.splitext(name)
i = 2
while os.path.exists(name):
name = base + newseparator + str(i) + extension
i += 1
return name
#%%
def saveplot(file, overwrite=False):
"""Saves a plot on an image file.
This function saves the current matplotlib.pyplot plot on a file.
If 'overwrite=False', it checks whether 'file' exists or not; if it
already exists, it defines a new file in order to not allow
overwritting. If overwrite=True, it saves the plot on 'file' even if
it already exists.
Variables
---------
file : string
The name you wish (must include full path and extension)
overwrite=False : bool
Indicates whether to overwrite or not.
Returns
-------
nothing
Yields
------
an image file
See Also
--------
free_file()
"""
if not os.path.isdir(os.path.split(file)[0]):
os.makedirs(os.path.split(file)[0])
if not overwrite:
file = free_file(file)
plt.savefig(file, bbox_inches='tight')
print('Archivo guardado en {}'.format(file))
#%%
def savetxt(file, datanumpylike, overwrite=False, header='', footer=''):
"""Takes some array-like data and saves it on a '.txt' file.
This function takes some data and saves it on a '.txt' file.
If 'overwrite=False', it checks whether 'file' exists or not; if it
already exists, it defines a new file in order to not allow
overwritting. If overwrite=True, it saves the plot on 'file' even if
it already exists.
Variables
---------
file : string
The name you wish (must include full path and extension)
datanumpylike : array, list
The data to be saved.
overwrite=False : bool, optional
Indicates whether to overwrite or not.
header='' : list, str, optional
Data's descriptor. Its elements should be str, one per column.
But header could also be a single string.
footer='' : dict, str, optional
Data's specifications. Its elements and keys should be str.
But footer could also be a single string. Otherwise, an element
could be a tuple containing value and units; i.e.: (100, 'Hz').
Return
------
nothing
Yield
-----
'.txt' file
See Also
--------
free_file()
"""
base = os.path.split(file)[0]
if not os.path.isdir(base):
os.makedirs(base)
if header != '':
if not isinstance(header, str):
try:
header = '\t'.join(header)
except:
TypeError('Header should be a list or a string')
if footer != '':
if not isinstance(footer, str):
try:
aux = []
for key, value in footer.items():
if isinstance(value, np.ndarray) or isinstance(value, tuple):
value = str(list(value))
elif isinstance(value, str):
value = '"{}"'.format(value)
aux.append('{}={}'.format(key, value) + ', ')
footer = ''.join(aux)
except:
TypeError('Header should be a dict or a string')
file = os.path.join(
base,
(os.path.splitext(os.path.basename(file))[0] + '.txt'),
)
if not overwrite:
file = free_file(file)
np.savetxt(file, np.array(datanumpylike),
delimiter='\t', newline='\n', header=header, footer=footer)
print('Archivo guardado en {}'.format(file))
return
#%%
def saveanimation(file,
animation,
frames_per_second=30,
overwrite=False):
"""Saves a matplotlib.animation object as '.gif' or '.mp4'.
Variables
---------
file : str
Desired file (must include full path and extension).
animation : matplotlib.animation object
Animation to save.
frames_per_second=30 : int
Animation's frames per second.
overwrite=False : bool
Indicates wheter to overwrite or not.
Returns
-------
nothing
Yields
------
video file
Warnings
--------
To save '.gif' you must have ImageMagick installed.
To save '.mp4' you must have FFPG installed.
See Also
--------
free_file()
fwp_plot.animation_2D()
"""
if not os.path.isdir(os.path.split(file)[0]):
os.makedirs(os.path.split(file)[0])
if not overwrite:
file = free_file(file)
extension = os.path.splitext(file)[-1]
if extension == '.mp4':
animation.save(file,
extra_args=['-vcodec', 'libx264'])
elif extension == '.gif':
animation.save(file,
dpi=50,
writer='imagemagick')
print('Archivo guardado en {}'.format(file))
return
#%%
def save_slice_generator(sim, filename, datanames, get_slices):
"""Generates a Meep stepfunction to save slices or outputs to HDF5.
Parameters
----------
sim : mp.Simulation
Meep simulation instance. Must be initialized so that ground set of
data can be saved.
filename : str
Filename of the HDF5 file to be created. Must include full path and .h5
extension. Beware! If already in existance, old file is replaced.
datanames : str or list of str
HDF5 dataset name.
get_slices : function or list of functions
Function 'get_slice(sim, state)' that takes 'sim' Meep simulation
instance as required argument and may take a second argument 'state',
which is a string with value 'step' or 'finish'. Must return the
desired array to be saved for each time step that the stepfunction
is called on.
Returns
-------
file : h5.File
HDF5 file instance of module h5py, initialized as 'w'. Will be closed
once the simulation is finished. Meanwhile, it must exist as a global
variable.
save_slice_stepfun : function
Stepfunction that will execute the list of functions 'get_slice' on
each step and save the results, appending each of them to the
associated 'dataname' dataset inside the 'filename' HDF5 file.
See also
--------
mp.Simulation
h5.File
"""
if type(datanames)!=list:
datanames = [datanames]
if type(get_slices)!=list:
get_slices = [get_slices]
if len(datanames)!=len(get_slices):
raise ValueError("Must have as many datanames as step functions")
file = h5.File(filename, 'w', libver='latest')
shapes = []
dueshapes = []
for get_slice, dataname in zip(get_slices, datanames):
data = get_slice(sim) # Data zero
shape = data.shape
dueshape = (1,*shape)
file.create_dataset(dataname, chunks=dueshape,
maxshape=tuple(None for d in dueshape),
data=data.reshape(dueshape))
shapes.append(shape)
dueshapes.append(dueshape)
file.swmr_mode = True
def save_slice_stepfun(sim, state):
if state=="step":
for i in range(len(datanames)):
data_now = get_slices[i](sim)
dim_before = file[datanames[i]].shape[0]
shape_now = ( dim_before+1, *shapes[i] )
file[datanames[i]].resize( shape_now )
file[datanames[i]][dim_before,] = data_now.reshape(dueshapes[i])
file[datanames[i]].flush()
# Notify the reader process that new data has been written
elif state=="finish":
file.close()
return
return file, save_slice_stepfun
#%%
def retrieve_footer(file, comment_marker='#'):
"""Retrieves the footer of a .txt file saved with np.savetxt.
Parameters
----------
file : str
File's root (must include directory and termination).
comment_marker='#' : str, optional
Sign that indicates a line is a comment on np.savetxt.
Returns
-------
last_line : str, dict
File's footer
Raises
------
ValueError : "Footer not found. Sorry!"
When the last line doesn't begin with 'comment_marker'.
See Also
--------
fwp_save.savetxt
"""
with open(file, 'r') as f:
for line in f:
last_line = line
if last_line[0] == comment_marker:
try:
last_line = last_line.split(comment_marker + ' ')[-1]
last_line = last_line.split('\n')[0]
footer = vu.string_to_dict(last_line)
except:
footer = last_line
return footer
else:
raise ValueError("No footer found. Sorry!")
#%%
def retrieve_header(file, comment_marker='#'):
"""Retrieves the header of a .txt file saved with np.savetxt.
Parameters
----------
file : str
File's root (must include directory and termination).
comment_marker='#' : str, optional
Sign that indicates a line is a comment on np.savetxt.
Returns
-------
last_line : str, list
File's header
Raises
------
ValueError : "Header not found. Sorry!"
When the first line doesn't begin with 'comment_marker'.
See Also
--------
fwp_save.savetxt
"""
with open(file, 'r') as f:
for line in f:
first_line = line
break
if comment_marker in first_line:
header = first_line.split(comment_marker + ' ')[-1]
header = header.split('\n')[0]
header = header.split('\t')
if len(header) > 1:
return header
else:
split_header = re.split(r' ', header[0])
new_header = []
for h in split_header:
new_header.append('')
for s in h:
if s!=" ":
new_header[-1] += s
if len(new_header) > 1:
return new_header
else:
return header[0]
else:
raise ValueError("No header found. Sorry!")
#%%
if __name__ == '__main__':
init_system(interactive=True)
else:
while True:
try:
current_system = setup_system()
break
except:
init_system()
sysname_definition, home_definition, syshome_definition = current_system