-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfwp_format.py
295 lines (244 loc) · 8.87 KB
/
fwp_format.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
# -*- coding: utf-8 -*-
"""
This module formats plots and values for Latex.
Some of its most useful tools are:
value_error : function
Rounds up value and error of a measure. Also makes a latex string.
plot_style : function
Gives a specific style to figure.
@author: Vall
"""
from matplotlib import rcParams, ticker
import matplotlib.pyplot as plt
from tkinter import Tk
#%%
def copy(string):
"""Copies a string to the clipboard.
Parameters
----------
string : str
The string to be copied.
Returns
-------
nothing
"""
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(string)
r.update() # now it stays on the clipboard
r.destroy()
print("Copied")
#%%
def error_value(X, dX, error_digits=2, units='',
string_scale=True, one_point_scale=False, legend=False):
"""Rounds up value and error of a measure. Also makes a latex string.
This function takes a measure and its error as input. Then, it
rounds up both of them in order to share the same amount of decimal
places.
After that, it generates a latex string containing the rounded up
measure. For that, it can rewrite both value and error so that the
classical prefix scale of units can be applied.
Parameters
----------
X : float
Measurement's value.
dX : float
Measurement's associated error.
error_digits=2 : int, optional.
Desired number of error digits.
units='' : str, optional.
Measurement's units.
string_scale=True : bool, optional.
Whether to apply the classical prefix scale or not.
one_point_scale=False : bool, optional.
Applies prefix with one order less.
legend=False : bool, optional.
Says whether it is for the legend of a plot or not.
Returns
-------
latex_str : str
Latex string containing value and error.
Examples
--------
>> error_value(1.325412, 0.2343413)
'(1.33$\\pm$0.23)'
>> error_value(1.325412, 0.2343413, error_digits=3)
'(1.325$\\pm$0.234)'
>> error_value(.133432, .00332, units='V')
'\\mbox{(133.4$\\pm$3.3) mV}'
>> error_value(.133432, .00332, one_point_scale=True, units='V')
'\\mbox{(0.1334$\\pm$0.0033) V}'
>> error_value(.133432, .00332, string_scale=False, units='V')
'\\mbox{(1.334$\\pm$0.033)$10^-1$ V}'
See Also
--------
copy
"""
# First, I string-format the error to scientific notation with a
# certain number of digits
if error_digits >= 1:
aux = '{:.' + str(error_digits) + 'E}'
else:
print("Unvalid 'number_of_digits'! Changed to 1 digit")
aux = '{:.0E}'
error = aux.format(dX)
error = error.split("E") # full error (string)
error_order = int(error[1]) # error's order (int)
error_value = error[0] # error's value (string)
# Now I string-format the measure to scientific notation
measure = '{:E}'.format(X)
measure = measure.split("E") # full measure (string)
measure_order = int(measure[1]) # measure's order (int)
measure_value = float(measure[0]) # measure's value (string)
# Second, I choose the scale I will put both measure and error on
# If I want to use the string scale...
if -12 <= measure_order < 12 and string_scale:
prefix = ['p', 'n', r'$\mu$', 'm', '', 'k', 'M', 'G']
scale = [-12, -9, -6, -3, 0, 3, 6, 9, 12]
for i in range(8):
if not one_point_scale:
if scale[i] <= measure_order < scale[i+1]:
prefix = prefix[i] # prefix to the unit
scale = scale[i] # order of both measure and error
break
else:
if scale[i]-1 <= measure_order < scale[i+1]-1:
prefix = prefix [i]
scale = scale[i]
break
used_string_scale = True
# ...else, if I don't or can't...
else:
scale = measure_order
prefix = ''
used_string_scale = False
# Third, I actually scale measure and error according to 'scale'
# If error_order is smaller than scale...
if error_order < scale:
if error_digits - error_order + scale - 1 >= 0:
aux = '{:.' + str(error_digits - error_order + scale - 1)
aux = aux + 'f}'
error_value = aux.format(
float(error_value) * 10**(error_order - scale))
measure_value = aux.format(
measure_value * 10**(measure_order - scale))
else:
error_value = float(error_value) * 10**(error_order - scale)
measure_value = float(measure_value)
measure_value = measure_value * 10**(measure_order - scale)
# Else, if error_order is equal or bigger than scale...
else:
aux = '{:.' + str(error_digits - 1) + 'f}'
error_value = aux.format(
float(error_value) * 10**(error_order - scale))
measure_value = aux.format(
float(measure_value) * 10**(measure_order - scale))
# Forth, I generate a latex string. Ex.: '(1.34$pm$0.32) kV'
latex_str = r'({}$\pm${})'.format(measure_value, error_value)
if not used_string_scale and measure_order != 0:
latex_str = latex_str + r'$10^{' +'{:.0f}'.format(scale) + r'}$'
elif used_string_scale:
latex_str = latex_str + ' ' + prefix
if units != '':
if latex_str[-1] == ' ':
latex_str = latex_str + units
else:
latex_str = latex_str + ' ' + units
if units != '' or prefix:
if not legend:
latex_str = r'\mbox{' + latex_str + '}'
return latex_str
#%%
def plot_text(text, text_position='up', figure_id=None):
"""Prints some text on a matplotlib.pyplot figure.
Parameters
----------
text : str
Text to be printed.
text_position : tuple, str {'up', 'dowm'}
Position of the text to be printed.
figure_id=None : int
ID of the figure where the text will be printed.
If none is given, the current figure is taken as default.
Returns
-------
nothing
See Also
--------
plot_style
matplotlib.pyplot.gcf
"""
if figure_id is None:
plt.gcf()
else:
plt.figure(figure_id)
if text_position == 'up':
plt.annotate(text, (0.02,0.9), xycoords="axes fraction")
elif text_position == 'down':
plt.annotate(text, (0.02,0.05), xycoords="axes fraction")
else:
plt.annotate(text, text_position, xycords="axes fraction")
plt.show()
#%%
def plot_style(figure_id=None, **kwargs):
"""Gives a specific style to figure.
This function...
...increases font size;
...increases linewidth;
...increases markersize;
...gives format to axis ticks if specified;
...stablishes new figure dimensions if specified;
...activates grid.
Parameters
----------
figure_id : int, optional.
ID of the figure where the text will be printed.
If none is given, the current figure is taken as default.
Other Parameters
----------------
xaxisformat : format-like str, optional.
Used to update x axis ticks format; i.e.: '%.2e'
yaxisformat : format-like str, optional.
Used to update y axis ticks format; i.e.: '%.2e'
dimensions: list with length 4, optional.
Used to update plot dimensions: [xmin, xmax, ymin, ymax]. Each
one should be a number expressed as a fraction of current
dimensions.
See Also
--------
matplotlib.pyplot.axis
matplotlib.pyplot.gcf
"""
if figure_id is None:
fig = plt.gcf()
else:
fig = plt.figure(figure_id)
ax = fig.axes
rcParams.update({'font.size': 14})
rcParams.update({'lines.linewidth': 3})
rcParams.update({'lines.markersize': 6})
kwargs_list = ['xaxisformat', 'yaxisformat', 'dimensions']
for key in kwargs_list:
try:
kwargs[key]
except KeyError:
kwargs[key] = None
if kwargs['xaxisformat'] is not None:
for a in ax:
a.xaxis.set_major_formatter(ticker.FormatStrFormatter(
kwargs['xaxisformat']))
if kwargs['yaxisformat'] is not None:
for a in ax:
a.yaxis.set_major_formatter(ticker.FormatStrFormatter(
kwargs['yaxisformat']))
if kwargs['dimensions'] is not None:
for a in ax:
box = a.get_position()
a.set_position([kwargs['dimensions'][0]*box.x0,
kwargs['dimensions'][1]*box.y0,
kwargs['dimensions'][2]*box.width,
kwargs['dimensions'][3]*box.height])
for a in ax:
a.grid()
plt.show()