-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
551 lines (457 loc) · 15.5 KB
/
utils.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
from __future__ import absolute_import, division, print_function
import os
import sys
import numpy as np
from qtpy import PYQT5
from qtpy.QtCore import Qt, QVariant
from qtpy.QtGui import QIcon, QColor, QFont, QKeySequence, QLinearGradient
from qtpy.QtWidgets import QAction, QDialog, QVBoxLayout
if PYQT5:
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
else:
from matplotlib.backends.backend_qt4agg import FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
dependencies = ['numpy', 'pandas', 'matplotlib', 'larray', 'larray_eurostat', 'pytables', 'xlwings', 'xlsxwriter',
'xlrd', 'openpyxl']
urls = {"fpb": "http://www.plan.be/index.php?lang=en",
"GPL3": "https://www.gnu.org/licenses/gpl-3.0.html",
"doc_stable": "http://larray.readthedocs.io/en/stable/",
"doc_tutorial": "http://larray.readthedocs.io/en/stable/tutorial.html",
"doc_api": "http://larray.readthedocs.io/en/stable/api.html",
"new_issue": "https://github.com/liam2/larray/issues/new",
"announce_group": "https://groups.google.com/d/forum/larray-announce",
"users_group": "https://groups.google.com/d/forum/larray-users"}
PY2 = sys.version[0] == '2'
if PY2:
def commonpath(paths):
return os.path.dirname(os.path.commonprefix(paths))
else:
commonpath = os.path.commonpath
def get_package_version(package_name):
"""Return the version of a package if installed, N/A otherwise"""
try:
from importlib import import_module
package = import_module(package_name)
if '__version__' in dir(package):
return package.__version__
elif '__VERSION__' in dir(package):
return package.__VERSION__
else:
return 'N/A'
except ImportError:
return 'N/A'
def get_versions():
"""Get version information for components used by the Editor"""
import platform
from qtpy import API_NAME, PYQT_VERSION
from qtpy.QtCore import __version__ as qtpy_version
from larray_editor import __version__ as editor_version
versions = {
'editor': editor_version,
'python': platform.python_version(),
'bitness': 64 if sys.maxsize > 2**32 else 32,
'qt': qtpy_version,
'qt_api': API_NAME, # PyQt5 or PyQt4
'qt_api_ver': PYQT_VERSION,
}
if not sys.platform == 'darwin': # To avoid a crash with our Mac app
versions['system'] = platform.system() # Linux, Windows, ...
else:
versions['system'] = 'Darwin'
for dep in dependencies:
versions[dep] = get_package_version(dep)
return versions
# Note: string and unicode data types will be formatted with '%s' (see below)
SUPPORTED_FORMATS = {
'object': '%s',
'single': '%.2f',
'double': '%.2f',
'float_': '%.2f',
'longfloat': '%.2f',
'float32': '%.2f',
'float64': '%.2f',
'float96': '%.2f',
'float128': '%.2f',
'csingle': '%r',
'complex_': '%r',
'clongfloat': '%r',
'complex64': '%r',
'complex128': '%r',
'complex192': '%r',
'complex256': '%r',
'byte': '%d',
'short': '%d',
'intc': '%d',
'int_': '%d',
'longlong': '%d',
'intp': '%d',
'int8': '%d',
'int16': '%d',
'int32': '%d',
'int64': '%d',
'ubyte': '%d',
'ushort': '%d',
'uintc': '%d',
'uint': '%d',
'ulonglong': '%d',
'uintp': '%d',
'uint8': '%d',
'uint16': '%d',
'uint32': '%d',
'uint64': '%d',
'bool_': '%r',
'bool8': '%r',
'bool': '%r',
}
def _get_font(family, size, bold=False, italic=False):
weight = QFont.Bold if bold else QFont.Normal
font = QFont(family, size, weight)
if italic:
font.setItalic(True)
return to_qvariant(font)
def is_float(dtype):
"""Return True if datatype dtype is a float kind"""
return ('float' in dtype.name) or dtype.name in ['single', 'double']
def is_number(dtype):
"""Return True is datatype dtype is a number kind"""
return is_float(dtype) or ('int' in dtype.name) or ('long' in dtype.name) or ('short' in dtype.name)
def get_font(section):
return _get_font('Calibri', 11)
def to_qvariant(obj=None):
return obj
def from_qvariant(qobj=None, pytype=None):
# FIXME: force API level 2 instead of handling this
if isinstance(qobj, QVariant):
assert pytype is str
return pytype(qobj.toString())
return qobj
def _(text):
return text
def to_text_string(obj, encoding=None):
"""Convert `obj` to (unicode) text string"""
if PY2:
# Python 2
if encoding is None:
return unicode(obj)
else:
return unicode(obj, encoding)
else:
# Python 3
if encoding is None:
return str(obj)
elif isinstance(obj, str):
# In case this function is not used properly, this could happen
return obj
else:
return str(obj, encoding)
def keybinding(attr):
"""Return keybinding"""
ks = getattr(QKeySequence, attr)
return QKeySequence.keyBindings(ks)[0]
def create_action(parent, text, icon=None, triggered=None, shortcut=None, statustip=None):
"""Create a QAction"""
action = QAction(text, parent)
if triggered is not None:
action.triggered.connect(triggered)
if icon is not None:
action.setIcon(icon)
if shortcut is not None:
action.setShortcut(shortcut)
if statustip is not None:
action.setStatusTip(statustip)
# action.setShortcutContext(Qt.WidgetShortcut)
return action
def clear_layout(layout):
for i in reversed(range(layout.count())):
item = layout.itemAt(i)
widget = item.widget()
if widget is not None:
# widget.setParent(None)
widget.deleteLater()
layout.removeItem(item)
def get_idx_rect(index_list):
"""Extract the boundaries from a list of indexes"""
rows = [i.row() for i in index_list]
cols = [i.column() for i in index_list]
return min(rows), max(rows), min(cols), max(cols)
class IconManager(object):
_icons = {'larray': 'larray.ico'}
_icon_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'images')
def icon(self, ref):
if ref in self._icons:
icon_path = os.path.join(self._icon_dir, self._icons[ref])
return QIcon(icon_path)
else:
# By default, only X11 will support themed icons. In order to use
# themed icons on Mac and Windows, you will have to bundle a compliant
# theme in one of your PySide.QtGui.QIcon.themeSearchPaths() and set the
# appropriate PySide.QtGui.QIcon.themeName() .
return QIcon.fromTheme(ref)
ima = IconManager()
class LinearGradient(object):
"""
I cannot believe I had to roll my own class for this when PyQt already
contains QLinearGradient... but you cannot get intermediate values out of
QLinearGradient!
Parameters
----------
stop_points: list/tuple, optional
List containing pairs (stop_position, colors_HsvF).
`colors` is a 4 elements list containing `hue`, `saturation`, `value` and `alpha-channel`
"""
def __init__(self, stop_points=None, nan_color=None):
if stop_points is None:
stop_points = []
# sort by position
stop_points = sorted(stop_points, key=lambda x: x[0])
positions, colors = zip(*stop_points)
positions = np.array(positions)
# check positions are unique and between 0 and 1
assert len(np.unique(positions)) == len(positions)
assert np.all((0 <= positions) & (positions <= 1))
self.positions = positions
self.colors = np.array(colors)
if nan_color is None:
nan_color = QColor(Qt.gray)
self.nan_color = nan_color
def as_qgradient(self):
qgradient = QLinearGradient(0, 0, 100, 0)
for pos, color in zip(self.positions, self.colors):
qgradient.setColorAt(pos, QColor.fromHsvF(*color))
return qgradient
def __getitem__(self, key):
"""
Parameters
----------
key : float
must be between 0 and 1
Returns
-------
QColor
"""
if np.isnan(key):
return self.nan_color
# this is enough to also avoid nan, inf & -inf
assert 0 <= key <= 1
pos_idx = np.searchsorted(self.positions, key, side='right') - 1
# if we are exactly on one of the bounds
if pos_idx > 0 and key in self.positions:
pos_idx -= 1
pos0, pos1 = self.positions[pos_idx:pos_idx + 2]
# col0 and col1 are ndarrays
col0, col1 = self.colors[pos_idx:pos_idx + 2]
assert pos1 > pos0
color = col0 + (col1 - col0) * (key - pos0) / (pos1 - pos0)
return to_qvariant(QColor.fromHsvF(*color))
class PlotDialog(QDialog):
def __init__(self, canvas, parent=None):
super(PlotDialog, self).__init__(parent)
toolbar = NavigationToolbar(canvas, self)
# set the layout
layout = QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(canvas)
self.setLayout(layout)
canvas.draw()
def show_figure(parent, figure):
canvas = FigureCanvas(figure)
main = PlotDialog(canvas, parent)
main.show()
class Product(object):
"""
Represents the `cartesian product` of several arrays.
Parameters
----------
arrays : iterable of array
List of arrays on which to apply the cartesian product.
Examples
--------
>>> p = Product([['a', 'b', 'c'], [1, 2]])
>>> for i in range(len(p)):
... print(p[i])
('a', 1)
('a', 2)
('b', 1)
('b', 2)
('c', 1)
('c', 2)
>>> p[1:4]
[('a', 2), ('b', 1), ('b', 2)]
>>> list(p)
[('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
"""
def __init__(self, arrays):
self.arrays = arrays
assert len(arrays)
shape = [len(a) for a in self.arrays]
self.div_mod = [(int(np.prod(shape[i + 1:])), shape[i])
for i in range(len(shape))]
self.length = np.prod(shape)
def to_tuple(self, key):
if key >= self.length:
raise IndexError("index %d out of range for Product of length %d" % (key, self.length))
return tuple(key // div % mod for div, mod in self.div_mod)
def __len__(self):
return self.length
def __getitem__(self, key):
if isinstance(key, (int, np.integer)):
return tuple(array[i]
for array, i in zip(self.arrays, self.to_tuple(key)))
else:
assert isinstance(key, slice), \
"key (%s) has invalid type (%s)" % (key, type(key))
start, stop, step = key.start, key.stop, key.step
if start is None:
start = 0
if stop is None:
stop = self.length
if step is None:
step = 1
return [tuple(array[i]
for array, i in zip(self.arrays, self.to_tuple(i)))
for i in range(start, stop, step)]
class _LazyLabels(object):
def __init__(self, arrays):
self.prod = Product(arrays)
def __getitem__(self, key):
return ' '.join(self.prod[key])
def __len__(self):
return len(self.prod)
class _LazyDimLabels(object):
"""
Examples
--------
>>> p = Product([['a', 'b', 'c'], [1, 2]])
>>> list(p)
[('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
>>> l0 = _LazyDimLabels(p, 0)
>>> l1 = _LazyDimLabels(p, 1)
>>> for i in range(len(p)):
... print(l0[i], l1[i])
a 1
a 2
b 1
b 2
c 1
c 2
>>> l0[1:4]
['a', 'b', 'b']
>>> l1[1:4]
[2, 1, 2]
>>> list(l0)
['a', 'a', 'b', 'b', 'c', 'c']
>>> list(l1)
[1, 2, 1, 2, 1, 2]
"""
def __init__(self, prod, i):
self.prod = prod
self.i = i
def __iter__(self):
return iter(self.prod[i][self.i] for i in range(len(self.prod)))
def __getitem__(self, key):
key_prod = self.prod[key]
if isinstance(key, slice):
return [p[self.i] for p in key_prod]
else:
return key_prod[self.i]
def __len__(self):
return len(self.prod)
class _LazyRange(object):
def __init__(self, length, offset):
self.length = length
self.offset = offset
def __getitem__(self, key):
if key >= self.offset:
return key - self.offset
else:
return ''
def __len__(self):
return self.length + self.offset
class _LazyNone(object):
def __init__(self, length):
self.length = length
def __getitem__(self, key):
return ' '
def __len__(self):
return self.length
def replace_inf(value):
"""Replace -inf/+inf in array with respectively min(array_without_inf)/max(array_without_inf).
Parameters
----------
value : np.ndarray or any compatible type
Input array.
Returns
-------
(np.ndarray, float, float)
array with replaced values and minimum and maximum values excluding NaN and infinite
Examples
--------
>>> replace_inf(np.array([-5, np.inf, 0, -np.inf, -4, 5]))
(array([-5., 5., 0., -5., -4., 5.]), -5.0, 5.0)
"""
value = value.copy()
# replace -inf by min(value)
notneginf = value != -np.inf
minvalue = np.nanmin(value[notneginf])
value[~notneginf] = minvalue
# replace +inf by max(value)
notposinf = value != np.inf
maxvalue = np.nanmax(value[notposinf])
value[~notposinf] = maxvalue
return value, minvalue, maxvalue
def scale_to_01range(value, vmin, vmax):
"""Scale value to 0-1 range based on vmin and vmax.
NaN are left intact, but -inf and +inf are converted to 0 and 1 respectively.
Parameters
----------
value : any numeric type
Value to scale.
vmin : any numeric type
Minimum used to do the scaling. This is the minimum value that is valid for value, *excluding -inf*.
vmin must be <= vmax.
vmax : any numeric type
Maximum used to do the scaling. This is the maximum value that is valid for value, *excluding +inf*.
vmax must be >= vmin.
Returns
-------
float or np.ndarray
Examples
--------
>>> scale_to_01range(5, 0, 10)
0.5
>>> scale_to_01range(1, 0, 10)
0.1
>>> scale_to_01range(np.nan, 0, 10)
nan
>>> scale_to_01range(+np.inf, 0, 10)
1.0
>>> scale_to_01range(-np.inf, 0, 10)
0.0
>>> scale_to_01range(5, 5, 5)
0.0
>>> scale_to_01range(np.array([-5, np.inf, 0, -np.inf, -4, 5]), -5, 5)
array([ 0. , 1. , 0.5, 0. , 0.1, 1. ])
"""
if hasattr(value, 'shape') and value.shape:
if vmin == vmax:
return np.where(np.isnan(value), np.nan, 0)
else:
assert vmin < vmax
with np.errstate(divide='ignore', invalid='ignore'):
res = (value - vmin) / (vmax - vmin)
res[value == -np.inf] = 0
res[value == +np.inf] = 1
return res
else:
if np.isnan(value):
return np.nan
elif value == -np.inf:
return 0.0
elif value == +np.inf:
return 1.0
elif vmin == vmax:
return 0.0
else:
assert vmin < vmax
return (value - vmin) / (vmax - vmin)