-
Notifications
You must be signed in to change notification settings - Fork 0
/
iv_save_module.py
721 lines (564 loc) · 20.8 KB
/
iv_save_module.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
713
714
715
716
717
718
719
720
721
# -*- coding: utf-8 -*-
"""The 'save' module loads and saves data, dealing with overwriting.
It could be divided into 3 sections:
(1) making new directories and free files to avoid overwriting
('newDir', 'freeFile')
(2) loading data from PumpProbe experiments ('loadPumpProbe',
'loadNicePumpProbe', 'filenameTo...')
(2) saving data into files with the option of not overwriting
('saveFig', 'saveTxt')
(4) loading data from files ('retrieveHeader', 'retrieveFooter')
@author: Vall
"""
import iv_utilities_module as ivu
import numpy as np
import os
import matplotlib.pyplot as plt
#%%
def newDir(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 filenameCreator(my_file, folder='', sufix=''):
"""Returns same filename in a different folder and with a sufix if given.
Parameters
----------
my_file : str
Tentative filename (must contain full path and extension).
folder='' : str
A new folder or set of folders to be added to the folder.
sufix='' : str
A sufix to be added to the given filename.
Returns
-------
new_file : str
New filename (also contains full path and same extension)
"""
base = os.path.split(my_file)[0]
if folder!='':
base = os.path.join(base, folder)
extension = os.path.splitext(my_file)[-1]
new_file = os.path.join(
base,
os.path.splitext( os.path.split(my_file)[1] )[0] + sufix + extension)
if not os.path.isdir(base):
os.makedirs(base)
return new_file
#%%
def freeFile(my_file, newformat='{}_v{}'):
"""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 filename (must contain full path and extension).
newformat='{}_v{}' : str
A formatter that allows to make new filenames in order to avoid
overwriting. If 'F:\Hola.png' does already exist, new file is saved as
'F:\Hola_v2.png'.
Returns
-------
free_file : str
Unoccupied filename (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 loadPumpProbe(filename):
"""Retrieves data and details from a PumpProbe measurement's file.
Each PumpProbe file starts with some data heading like this:
'''
Formula
Fecha 10/04/2019 13:49
Desde -40.00
Hasta 1320.00
Paso 2.00
Tiempo de Integracion 100.00
Retardo cero -640.00
'''
These files contain time in ps and voltage on V.
Parameters
----------
filename : str
File's root (must include directory and termination).
Returns
-------
data : np.array
Measured data. It has 2*N columns, where N is the number of
experiments. Inside, it holds data ordered as [T1, V1, ..., TN, VN]
where Ti is time in ps and Vi is voltage in V.
details : dict
Details of the measurement, including...
date : str
Date and hour, on 'DD/MM/YYYY HH:HH' format.
time_range : tuple
time_start : float
First time difference, in ps.
time_end : float
Last time difference, in ps.
time_step : float
Minimum time step, in ps.
time_integration : float
Lock-in's integration time, in ps, that defines how much time
will the system retain the same time difference in order to
make an average reading using the lock-in.
time_zero : float
Time reference, in ps.
Raises
------
ValueError : "Columns have different number of rows :("
When a numpy array cannot be made because there's a faulty experiment,
which doesn't hold as much data as it should.
"""
lines = []
other_lines = []
extras = ['Fecha ', 'Desde ', 'Hasta ', 'Paso ',
'Tiempo de Integracion ', 'Retardo cero ']
names = ['date', 'time_range', 'time_step',
'time_integration', 'time_zero']
i = 0
with open(filename, 'r') as f:
for line in f:
if i >= 1 and i < 7: # I must ignore the first line
lines.append(line)
elif i >= 7: # From here on there's only data.
other_lines.append(line)
i = i+1
details = {}
details[names[0]] = lines[0].split(extras[0])[-1].split(' \n')[0]
details[names[1]] = (
float(lines[1].split(extras[1])[-1].split(' \n')[0]),
float(lines[2].split(extras[2])[-1].split(' \n')[0]),
)
details[names[2]] = float(lines[3].split(extras[3])[-1].split(' \n')[0])
details[names[3]] = float(lines[4].split(extras[4])[-1].split(' \n')[0])
details[names[4]] = float(lines[5].split(extras[5])[-1].split(' \n')[0])
# other_lines = [[float(number) for number in line.split('\t')]
# for line in other_lines]
# N = len(other_lines) # Number of measurements each experiment should have.
#
# data = []
# for i in range(N):
# for experiment in range(len(other_lines[0])/2):
# if other_lines[i][]
try:
data = np.array([[float(number) for number in line.split('\t')]
for line in other_lines])
except:
raise ValueError("Columns have different number of rows :(")
return data, details
#%%
def loadNicePumpProbe(filename):
"""Loads nice data and details from a PumpProbe measurement's file.
Returns equispaced time in ps, voltage in uV and also calculates mean voltage
in uV. Moreover, it adds some parameters to the measurement's details.
Parameters
----------
filename : str
File's root (must include directory and termination).
Returns
-------
t : np.array
Equispaced time in ps. It has 'nsize' length.
V : np.array
Measured voltage in uV. It has 'nsize' rows and 'nrepetitions' columns.
details : dict
Details of the measurement, including...
samplerate : float
Sampling rate in Hz.
dt : float
Time step in ps of the equispaced time.
nsize : int
Number of measurements included in each repetition.
nexperiments : int
Number of repetitions of the experiment.
Raises
------
ValueError : "Columns have different number of rows :("
When a numpy array cannot be made because there's a faulty experiment,
which doesn't hold as much data as it should.
See also
--------
loadPumpProbe
"""
# First get data's name
[data, details] = loadPumpProbe(filename)
# Define data size
nrepetitions = int(len(data[0,:]) / 2) # Number of measurements
nsize = len(data[:,0]) # Length of each measurement
# Get time
t = data[:,0] # Consider just one time column
# Define time parameters
T = t[-1] - t[0] # Total time in ps
samplerate = nsize / (10**12 * T) # Sampling rate in Hz
dt = T / nsize # Time step
# Make equispaced time
t = np.linspace(t[0], t[-1], nsize)
# Add uV voltage
V = np.array([1e6 * data[:, 2*i+1] for i in range(nrepetitions)]).T
# Add some other relevant details
details.update(dict(samplerate=samplerate,
dt=dt,
nsize=nsize,
nrepetitions=nrepetitions))
return t, V, details
#%%
def filenameToDate(filename):
"""Given a filename 'M_20190610_01', returns date on '2019-06-10' format"""
date = filename.split('_')[1] # From 'M_20190610_01' take '20190610'
date = '-'.join([date[:4], date[4:6], date[6:]]) # Transfrom to '2019-06-10'
return date
#%%
def filenameToMeasureFilename(filename,
home=os.getcwd()):
"""Given a filename 'M_20190610_01', returns path to fits' data"""
date = filenameToDate(filename) # Transfrom to '2019-06-10'
fits_filename = os.path.join(home, 'Mediciones', date, filename+'.txt')
return fits_filename
#%%
def filenameToFitsFilename(filename,
home=os.getcwd()):
"""Given a filename 'M_20190610_01', returns path to fits' data"""
date = filenameToDate(filename) # Transfrom to '2019-06-10'
fits_filename = os.path.join(home, 'Mediciones', date,
'Ajustes', filename+'.txt')
return fits_filename
#%%
def linearPredictionSave(filename, results, other_results, fit_parameters,
overwrite=False):
"""Saves the data from a linear prediction fit on '.txt' file.
Parameters
----------
filename : str
The name you wish (must include full path and extension)
results : np.array
Parameters that best fit the data. On its columns it holds...
...frequency :math:`f=2\pi\omega` in Hz.
...characteristic time :math:`\tau_i` in ps.
...quality factors :math:`Q_i=\frac{\omega}{2\gamma}=\pi f \tau`
...amplitudes :math:`A_i` in the same units as :math:`x`
...phases :math:`\phi_i` written in multiples of :math:`\pi`
other_results : dict
Other fit parameters...
...chi squared :math:`\chi^2`
...number of significant values :math:`N`
fit_parameters : ivu.InstancesDict
Several fit configuration parameters, including...
use_full_mean=True : bool
Whether to use full mean or not.
send_tail_to_zero=False : bool
Whether to apply a vertical shift to send the last data to zero
or not.
voltage_zero : float, int
Vertical shift.
time_range : tuple
Initial and final time to fit.
overwrite=False
Whether to allow overwriting or not.
Returns
-------
None
Yields
------
.txt file
See also
--------
saveTxt
"""
fit_params = fit_parameters.__dict__ # Because it's an ivu.InstancesDict
footer = {}
footer.update(other_results)
footer.update(fit_params)
saveTxt(filename, results,
header=["F (GHz)", "Tau (ps)", "Q", "A (u.a.)", "Phi (pi rad)"],
footer=footer,
overwrite=overwrite,
folder='Ajustes')
return
#%%
def saveFig(filename, extension='.png', overwrite=False,
newformat='{}_v{}', **kwargs):
"""Saves current matplotlib figure in a compact format.
This function takes per default the current plot and saves it on file. If
'overwrite=False', it checks whether 'filename' exists or not; if it already
exists, it defines a new file in order to not allow overwritting. If
overwrite=True, it saves on 'filename' even if it already exists.
Variables
---------
filename : str
The name you wish (must include full path).
extension='.png' : str
An image extension; i.e.: '.pdf', '.jpg'.
overwrite=False : bool, optional
Indicates whether to overwrite or not.
newformat='{}_v{}' : str
A formatter that allows to make new filenames in order to avoid
overwriting. If 'F:\Hola.png' does already exist, new file is saved as
'F:\Hola_v2.png'.
Other parameters
----------------
folder='' : str
A new folder or set of folders to be added to the folder.
sufix='' : str
A sufix to be added to the given filename.
Return
------
nothing
Yield
-----
image file
See Also
--------
freeFile
"""
filename = os.path.splitext(filename)[0] + extension
filename = filenameCreator(filename, **kwargs)
if not overwrite:
filename = freeFile(filename, newformat=newformat)
plt.savefig(filename, bbox_inches='tight')
print('Imagen guardada en {}'.format(filename))
return
#%%
def saveTxt(filename, datanumpylike, header='', footer='',
overwrite=False, newformat='{}_v{}', **kwargs):
"""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 'filename' exists or not; if it
already exists, it defines a new file in order to not allow
overwritting. If overwrite=True, it saves on 'filename' even if
it already exists.
Variables
---------
filename : 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').
newformat='{}_v{}' : str
A formatter that allows to make new filenames in order to avoid
overwriting. If 'F:\Hola.png' does already exist, new file is saved as
'F:\Hola_v2.png'.
Other parameters
----------------
folder='' : str
A new folder or set of folders to be added to the folder.
sufix='' : str
A sufix to be added to the given filename.
Return
------
nothing
Yield
-----
'.txt' file
See Also
--------
freeFile
"""
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, tuple) and len(value) == 2:
condition = isinstance(value[0], str)
if not condition and isinstance(value[1], str):
value = '"{} {}"'.format(*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')
filename = filenameCreator(filename, **kwargs)
if not overwrite:
filename = freeFile(filename, newformat=newformat)
np.savetxt(filename, np.array(datanumpylike),
delimiter='\t', newline='\n', header=header, footer=footer)
print('Archivo guardado en {}'.format(filename))
return
#%%
def retrieveFooter(filename, comment_marker='#'):
"""Retrieves the footer of a .txt file saved with np.savetxt or saveTxt.
Parameters
----------
filename : 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
--------
saveTxt
"""
with open(filename, '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 = eval('dict({})'.format(last_line))
for key, value in footer.items():
try:
number = ivu.findNumbers(value)
if len(number) == 1:
number = number[0]
if len(value.split(' ')) == 2:
footer[key] = (
number,
value.split(' ')[-1]
)
else:
footer[key] = number
except TypeError:
value = value
except:
footer = last_line
return footer
else:
raise ValueError("No footer found. Sorry!")
#%%
def retrieveHeader(filename, comment_marker='#'):
"""Retrieves the header of a .txt file saved with np.savetxt or saveTxt.
Parameters
----------
filename : 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
--------
saveTxt
"""
with open(filename, 'r') as f:
for line in f:
first_line = line
break
if first_line[0] == comment_marker:
header = first_line.split(comment_marker + ' ')[-1]
header = header.split('\n')[0]
header = header.split('\t')
if len(header) > 1:
return header
else:
return header[0]
else:
raise ValueError("No header found. Sorry!")
#%%
def loadTxt(filename, comment_marker='#', **kwargs):
"""Loads data of a .txt file saved with np.savetxt or saveTxt.
Parameters
----------
filename : 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
-------
data : np.array
File's data.
header : str, list or None
File's header.
footer : str, dict or None
File's footer.
See also
--------
saveTxt
retrieveHeader
retrieveFooter
"""
data = np.loadtxt(filename, comments=comment_marker, **kwargs)
try:
header = retrieveHeader(filename, comment_marker)
except ValueError:
header = None
try:
footer = retrieveFooter(filename, comment_marker)
except ValueError:
footer = None
return data, header, footer