forked from GrupoFWP/DAQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fwp_utils.py
184 lines (134 loc) · 4.1 KB
/
fwp_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
# -*- coding: utf-8 -*-
"""
This module contains some generic tools.
@author: GrupoFWP
"""
import numpy as np
#%%
def find(L, L0):
"""Takes an array of data and searches the index(es) of value L0.
Parameters
----------
L : list, np.array
Array where I search
L0 : int, float
Value I search
Returns
-------
ind : list
Indexes of L that match L0.
Raises
------
"L must be a list or similar" : TypeError
If L is not of an allowed type.
"""
if not isinstance(L, list):
try:
L = list(L)
except TypeError:
L = [].append(L)
else:
return TypeError("L must be a list or similar")
ind = []
N = -1
while N < len(L):
val = L[N+1 : len(L)]
try:
# Write the index on L and not on val
N = val.index(L0) + len(L) - len(val)
except ValueError:
break
ind.append(N)
return ind
#%%
def clip_between(value, lower=0, upper=100):
'''Clips value to the (lower, upper) interval, i.e. if value
is less than lower, it return lower, if its grater than upper,
it return upper, else, it returns value unchanged.'''
value = max(lower, value)
value = min(upper, value)
return value
#%%
def clear_queue(queue):
"""Clears a queue and returns all elements erased"""
d = []
while not queue.empty():
data = queue.get()
d.append(data)
return d
#%%
def zeros(size, dtype=np.float64):
"""Analog to np.zeros but reshapes to N if size=(1, N)"""
try:
len(size)
size = tuple(size)
except TypeError:
pass
if isinstance(size, tuple):
if size[0] == 1:
size = size[1]
return np.zeros(size, dtype=dtype)
def multiappend(nparray, new_nparray, fast_speed=True):
"""Analog to np.append but with 2D np.arrays"""
try:
nrows = len(new_nparray[:,0])
except IndexError:
nrows = 1
if not fast_speed:
try:
nrows0 = len(np.nparray[:,0])
except IndexError:
nrows = 1
if nrows0 != nrows:
raise IndexError("Different number of rows.")
if not nparray:
return new_nparray
elif nrows == 1:
return np.append(nparray, new_nparray)
else:
construct = []
for i in range(nrows):
row = nparray[i,:]
row = np.append(row, new_nparray[i,:])
construct.append(row)
return np.array(construct)
#%%
class TypedList(list):
"""A list that only appends a certain type"""
def __init__(self, Class):
self.instance = Class
def append(self, item):
if not isinstance(item, self.instance):
raise TypeError('Can only append {}'.format(self.instance))
super().append(item)
# Simple, but apparently not the best way...
# https://stackoverflow.com/questions/3487434/overriding-append-method-after-inheriting-from-a-python-list
class NotCertainTypeList(list):
"""A list that doesn't append a certain type of elements"""
def __init__(self, *iterable, show_exceptions=True):
super().__init__(iterable)
self.show_exceptions = show_exceptions
def append(self, item):
if not isinstance(item, self.instance):
super().append(item)
else:
if self.show_exceptions:
raise TypeError("Can't append {}".format(
self.instance))
#%%
class ObjectView(object):
def __init__(self, d):
self.__dict__ = d
class ObjectDict(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError("No such attribute: " + name)
def __setattr__(self, name, value):
self[name] = value
def __delattr__(self, name):
if name in self:
del self[name]
else:
raise AttributeError("No such attribute: " + name)