forked from ASPP/ASPP-2018-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
117 lines (95 loc) · 3.41 KB
/
tools.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
# -----------------------------------------------------------------------------
# Copyright (C) 2018 Nicolas P. Rougier
# Distributed under the terms of the BSD License.
# -----------------------------------------------------------------------------
from imshow import imshow
def sysinfo():
import sys
import time
import numpy as np
import scipy as sp
import matplotlib
print("Date: %s" % (time.strftime("%D")))
version = sys.version_info
major, minor, micro = version.major, version.minor, version.micro
print("Python: %d.%d.%d" % (major, minor, micro))
print("Numpy: ", np.__version__)
print("Scipy: ", sp.__version__)
print("Matplotlib:", matplotlib.__version__)
def timeit(stmt, globals=globals()):
import numpy as np
import timeit as _timeit
print("Timing '{0}'".format(stmt))
# Rough approximation of a 10 runs
trial = _timeit.timeit(stmt, globals=globals, number=10)/10
# Maximum duration
duration = 5.0
# Number of repeat
repeat = 7
# Compute rounded number of trials
number = max(1,int(10**np.ceil(np.log((duration/repeat)/trial)/np.log(10))))
# Only report best run
times = _timeit.repeat(stmt, globals=globals, number=number, repeat=repeat)
times = np.array(times)/number
mean = np.mean(times)
std = np.std(times)
# Display results
units = {"s": 1, "ms": 1e-3, "us": 1e-6, "ns": 1e-9}
for key,value in units.items():
unit, factor = key, 1/value
if mean > value: break
mean *= factor
std *= factor
print("%.3g %s ± %.3g %s per loop (mean ± std. dev. of %d runs, %d loops each)" %
(mean, unit, std, unit, repeat, number))
def info(Z):
import sys
import numpy as np
endianness = {'=': 'native (%s)' % sys.byteorder,
'<': 'little',
'>': 'big',
'|': 'not applicable'}
print("------------------------------")
print("Interface (item)")
print(" shape: ", Z.shape)
print(" dtype: ", Z.dtype)
print(" length: ", len(Z))
print(" size: ", Z.size)
print(" endianness: ", endianness[Z.dtype.byteorder])
if np.isfortran(Z):
print(" order: ☐ C ☑ Fortran")
else:
print(" order: ☑ C ☐ Fortran")
print("")
print("Memory (byte)")
print(" item size: ", Z.itemsize)
print(" array size: ", Z.size*Z.itemsize)
print(" strides: ", Z.strides)
print("")
print("Properties")
if Z.flags["OWNDATA"]:
print(" own data: ☑ Yes ☐ No")
else:
print(" own data: ☐ Yes ☑ No")
if Z.flags["WRITEABLE"]:
print(" writeable: ☑ Yes ☐ No")
else:
print(" writeable: ☐ Yes ☑ No")
if np.isfortran(Z) and Z.flags["F_CONTIGUOUS"]:
print(" contiguous: ☑ Yes ☐ No")
elif not np.isfortran(Z) and Z.flags["C_CONTIGUOUS"]:
print(" contiguous: ☑ Yes ☐ No")
else:
print(" contiguous: ☐ Yes ☑ No")
if Z.flags["ALIGNED"]:
print(" aligned: ☑ Yes ☐ No")
else:
print(" aligned: ☐ Yes ☑ No")
print("------------------------------")
print()
if __name__ == '__main__':
import numpy as np
sysinfo()
Z = np.arange(9).reshape(3,3)
info(Z)
timeit("Z=np.random.uniform(0,1,1000000)", globals())