-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutilities.py
188 lines (132 loc) · 4.27 KB
/
utilities.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
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import pdb
import re
import numpy as np
def save_to_interactive(dct):
"""Save variables from debugger to main interpreter
import utilities
utilities.save_to_interactive({'F1':F1, 'F2':F2, 'M1':M1, 'M2':M2})
"""
# Be safe and define a maximum of frames we're trying to walk up
MAX_FRAMES = 20
n = 0
# Walk up the stack looking for '__name__'
# with a value of '__main__' in frame globals
for n in range(MAX_FRAMES):
cur_frame = sys._getframe(n)
name = cur_frame.f_globals.get('__name__')
if name == '__main__':
# Yay - we're in the stack frame of the interactive interpreter!
# So we update its frame globals with the dict containing our data
cur_frame.f_globals.update(dct)
break
def ismember_rows(a, b):
'''Equivalent of 'ismember' from Matlab
a.shape = (nRows_a, nCol)
b.shape = (nRows_b, nCol)
return the idx where b[idx] == a
'''
invalid = -1
indx = np.full(a.shape[0], invalid, dtype='int')
indxa, indxb = np.nonzero(np.all(b == a[:, np.newaxis], axis=2))
indx[indxa] = indxb
indx[indxb] = indxa
return indx
def ismember(a, b):
indx = np.nonzero(np.all(b == a[:, np.newaxis], axis=2))[0]
return indx
def asvoid(arr):
"""
View the array as dtype np.void (bytes)
This views the last axis of ND-arrays as bytes so you can perform comparisons on
the entire row.
http://stackoverflow.com/a/16840350/190597 (Jaime, 2013-05)
Warning: When using asvoid for comparison, note that float zeros may compare UNEQUALLY
>>> asvoid([-0.]) == asvoid([0.])
array([False], dtype=bool)
"""
arr = np.ascontiguousarray(arr)
return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))
def in1d_index(a, b):
voida, voidb = map(asvoid, (a, b))
return np.where(np.in1d(voidb, voida))[0]
def ismember_index(a, b):
r"""Finds matching elements between two arrays
index = ismember_index(a, b)
Parameters
----------
a : array_like
b : array_like
Returns
-------
index : array_like
Notes
-----
This allows for the comparison of multidimensional arrays (n x m).
It first creates a byte object for each row, then compares that to find
the matching elements
Author
------
Shankar Kulumani GWU [email protected]
"""
invalid = -1
a[a == -0.0] = 0
b[b == -0.0] = 0
voida, voidb = map(asvoid, (a, b))
index = np.full(a.shape[0], invalid, dtype='int')
for ii in range(a.shape[0]):
match = np.where(voida[ii] == voidb)[0]
if match.size:
index[ii] = match[0]
return index
def search_index(a, b):
r"""Memory intensive way to find matches in single dimensional array
inda, indb = search_index(a, b)
Parameters
----------
a : array_like
b : array_like
Both should be single dimensional (n,)
Returns
-------
inda : array_liek
indb :
Notes
-----
https://stackoverflow.com/questions/8251541/numpy-for-every-element-in-one-array-find-the-index-in-another-array
inda - index for each element corresponds to the match given in indb
so a[inda[0]] = b[indb[0]] and a[inda[1]] = b[indb[1]]
Author
------
Shankar Kulumani GWU [email protected]
"""
invalid = -1
lenb = len(b)
lena = len(a)
ae = np.broadcast_to(b, (lena, lenb))
be = np.broadcast_to(a, (lenb, lena)).T
inda, indb = np.where(np.equal(ae, be))
return inda, indb
def sorted_nicely(l):
r"""Sort the iterable into a human expected fashion
l_out = sorted_nicely(l_in)
Parameters
----------
l_in : iterable
Array of mixed alphanumeric items
Returns
-------
l_out : iterable
Sorted list in a human fashion way
Author
------
Shankar Kulumani GWU [email protected]
"""
def convert(text):
return int(text) if text.isdigit() else text
def alphanum_key(key):
return [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
if __name__ == "__main__":
print("Some versions of trying to duplicate Matlab's ismember function.")