-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.py
400 lines (362 loc) · 13.1 KB
/
misc.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
#!/usr/bin/env python
"""
Miscellaneous functionality used by various other modules.
"""
# This file is part of Owner Credit
#
# Owner Credit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Owner Credit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Owner Credit. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
__author__ = "Perry Kundert"
__email__ = "perry@kundert.ca"
__copyright__ = "Copyright (c) 2006 Perry Kundert"
__license__ = "Dual License: GPLv3 (or later) and Commercial (see LICENSE)"
import math
import sys
import time
#
# misc.timer
#
# Select platform appropriate timer function
#
if sys.platform == 'win32':
# On Windows, the best timer is time.clock
timer = time.clock
else:
# On most other platforms the best timer is time.time
timer = time.time
#
# misc.nan -- IEEE NaN (Not a Number)
# misc.isnan -- True iff the provided value is nan
# misc.inf -- IEEE inf (Infinity)
# misc.isinf -- True iff the provided value is inf
#
# Augment math with some useful constants. Note that IEEE NaN is the
# only floating point number that won't equal itself.
#
# Numpy has these, but we can't assume it is available.
#
if hasattr( math, 'nan' ):
nan = math.nan
else:
nan = float( 'nan' )
math.nan = nan
if hasattr( math, 'isnan' ):
isnan = math.isnan
else:
def isnan( f ):
return f != f
math.isnan = isnan
if hasattr( math, 'inf' ):
inf = math.inf
else:
inf = float( 'inf' )
math.inf = inf
if hasattr( math, 'isinf' ):
isinf = math.isinf
else:
def isinf( f ):
return abs( f ) == inf
math.isinf = isinf
#
# near -- True iff the specified values are within 'significance' of each-other
#
def near( a, b, significance = 1.0e-4 ):
""" Returns True iff the difference between the values is within the factor 'significance' of
one of the original values. Default is to within 4 decimal places. """
return abs( a - b ) <= significance * max( abs( a ), abs( b ))
#
# clamp -- Clamps a value to within a tuple of limits.
#
# Limits that are math.nan are automatically ignored, with no special code (comparisons
# against NaN always return False).
#
# The ordering of 'lim' is assumed to be (min, max). We don't attempt to reorder, because 'lim'
# may contain NaN.
#
def clamp( val, lim ):
""" Limit val to between 2 (optional, if nan, because no value is < or > nan) limits """
if ( val < lim[0] ):
return lim[0]
if ( val > lim[1] ):
return lim[1]
return val
#
# scale -- Transform a value from one range to another, without clipping
#
# No math.nan allowed or zero-sized domains or ranges. Works for either increasing or
# decreasing ordering of domains or ranges. If clamped, we will ensure that the rng is (re)ordered
# appropriately.
#
# If non-unity exponent is provided, then the input domain is raised to the appropriate power
# during the mapping. This allows us to map something like (25,40)->(0,1) with a curve such as:
#
# 1 | .
# | .
# | ..
# | ...
# | .....
# 0 +---------------
# 2 4
# 5 0
#
#
def scale( val, dom, rng, clamped=False, exponent=1 ):
"""Map 'val' from domain 'dom', to new range 'rng', optionally with an exponential scaling. If a
non-unity exponent is provided, then the input value is also clamped to the input domain (and
its order is asserted) since raising -'ve values to arbitrary exponents will usually have very
unexpected results. Otherwise, at unity exponent, allow -'ve values and out-of-order ranges.
"""
if exponent != 1:
assert dom[1] > dom[0], "Scaling %s non-linearly requires an ordered domain: %s" % ( val, dom )
if clamped:
val = clamp( val, (min(dom),max(dom)) )
else:
assert dom[0] <= val <= dom[1], "Scaling %s non-linearly requires value in domain: %s" % ( val, dom )
else:
assert dom[1] != dom[0], "Scaling %s requires a non-zero domain: %s" % ( val, dom )
result = ( rng[0]
+ ( val - dom[0] ) ** exponent
* ( rng[1] - rng[0] )
/ ( dom[1] - dom[0] ) ** exponent )
if clamped:
result = clamp( result, (min(rng),max(rng)))
return result
#
# magnitude -- Return the approximate base magnitude of the value, in 'base' ( 10 )
#
# Handy for computing up/down modifiers for values. For example:
#
# 23 ==> 1.
# .23 ==> .1
#
# The magnitude shifts to the next higher value about 1/4 of the way
# past each multiple of base.
def magnitude( val, base = 10 ):
if val <= 0:
return nan
return pow( base, round( math.log( val, base )) - 1 )
#
# sort order key=... methods
#
# natural -- Strings containing numbers sort in natural order
# nan_first -- NaN/None sorts lower than any number
# nan_last -- NaN/None sorts higher than any number
#
def natural( string ):
'''
A natural sort key helper function for sort() and sorted() without
using regular expressions or exceptions.
>>> items = ('Z', 'a', '10th', '1st', '9')
>>> sorted(items)
['10th', '1st', '9', 'Z', 'a']
>>> sorted(items, key=natural)
['1st', '9', '10th', 'a', 'Z']
'''
it = type( 1 )
r = []
for c in string:
if c.isdigit():
d = int( c )
if r and type( r[-1] ) == it:
r[-1] = r[-1] * 10 + d
else:
r.append( d )
else:
r.append( c.lower() )
return r
def non_value( number ):
return number is None or isnan( number )
def nan_first( number ):
if non_value( number ):
return -inf
return number
def nan_last( number ):
if non_value( number ):
return inf
return number
#
# misc.value -- Base class for things that should generally act like a float/int
#
class value( object ):
"""
Acts like an integer or float in most use cases. Use as a base class for things that want to
have a simple integer or float value type interface for arithmetic expressions. Also handles
several non-values correctly: None, misc.nan. If supplied
By default, uses a fake Lock which exposes acquisition semantics, but does nothing. If uses in
the multithreaded environment, it is recommended that a threading.RLock be used.
Great care must be taken to ensure that only one lock is held during updates or access (ie. to
obtain self-consistent value and now times). If multiples locks can ever be held during access
of the current value of an object, it will be possible to "deadlock" any program that ever has
two threads lock the same locks in opposite order.
"""
__slots__ = [ 'value', 'now', 'lock' ]
class NoOpRLock( object ):
def acquire(self, *args, **kwargs):
pass
__enter__ = acquire
def release(self):
pass
def __exit__(self, *args, **kwargs):
self.release()
def __init__( self,
value = 0,
now = None,
lock = NoOpRLock()):
"""
The base constructor initializes the value and time, and if a non-None value is provided,
uses it for the initial sample invocation. The default is zero, but None or math.nan is
appropriate, if the user is prepared to handle non-numeric values until the first sample is
provided.
"""
if now is None:
now = timer()
self.lock = lock
with self.lock:
self.now = now
self.value = value
if not non_value( value ):
self.sample( value, now )
def sample( self,
value = None,
now = None ):
"""
The default sample method simply assigns the given value and time. The new value
may be a legitimate float or int value, or a non-value (eg. None or NaN).
"""
if isinstance( value, self.__class__ ):
# Another misc.value; we'll compute its current value relative to the timestamp we're
# given (if None; obtain from other value, holding its lock for consistency)
with value.lock:
if now is None:
now = value.now
value = value.compute( now=now )
# Now, update ourself with the (possibly computed) value and time
if now is None:
now = timer()
with self.lock:
self.now = now
self.value = value
return self.value
def compute( self,
now = None ):
"""
Compute an updated value, relative to the specified time. The default method just returns
the current value.
"""
return self.value
# Rich comparison
def __eq__( self, rhs ):
if isinstance( rhs, value ):
return self.value == rhs.value
return self.value == rhs
def __ne__( self, rhs ):
if isinstance( rhs, value ):
return self.value != rhs.value
return self.value != rhs
def __lt__( self, rhs ):
if isinstance( rhs, value ):
return self.value < rhs.value
return self.value < rhs
def __le__( self, rhs ):
if isinstance( rhs, value ):
return self.value <= rhs.value
return self.value <= rhs
def __gt__( self, rhs ):
if isinstance( rhs, value ):
return self.value > rhs.value
return self.value > rhs
def __ge__( self, rhs ):
if isinstance( rhs, value ):
return self.value >= rhs.value
return self.value >= rhs
# Casts
def __bool__( self ):
return bool( self.value )
def __str__( self ):
return str( self.value )
def __int__( self ):
return int( self.value )
def __float__( self ):
return float( self.value )
# Arithmetic operators. The in-place operators (+=, etc.) use the apply the rhs value as a
# sample; if the rhs is a misc.value, then it also knows to use the 'now' time.
def __sub__( self, rhs ):
return self.value - rhs
def __rsub__( self, lhs ):
return lhs - self.value
def __isub__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value - rhs.value, rhs.now )
else:
self.sample( self.value - rhs )
return self
def __add__( self, rhs ):
return self.value + rhs
def __radd__( self, lhs ):
return lhs + self.value
def __iadd__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value + rhs.value, rhs.now )
else:
self.sample( self.value + rhs )
return self
def __mul__( self, rhs ):
return self.value * rhs
def __rmul__( self, lhs ):
return lhs * self.value
def __imul__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value * rhs.value, rhs.now )
else:
self.sample( self.value * rhs )
return self
def __div__( self, rhs ):
return self.value / rhs
def __rdiv__( self, lhs ):
return lhs / self.value
def __idiv__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value / rhs.value, rhs.now )
else:
self.sample( self.value / rhs )
return self
def __truediv__( self, rhs ):
return self.value / rhs
def __rtruediv__( self, lhs ):
return lhs / self.value
def __itruediv__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value / rhs.value, rhs.now )
else:
self.sample( self.value / rhs )
return self
def __floordiv__( self, rhs ):
return self.value // rhs
def __rfloordiv__( self, lhs ):
return lhs // self.value
def __ifloordiv__( self, rhs ):
if isinstance( rhs, value ):
self.sample( self.value // rhs.value, rhs.now )
else:
self.sample( self.value // rhs )
return self
# Various mathematical operators
def __abs__( self ):
return abs( self.value )
def __neg__( self ):
return -self.value
def __pos__( self ):
return +self.value