/usr/lib/python2.7/dist-packages/dipy/reconst/benchmarks/bench_squash.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """ Benchmarks for fast squashing
Run all benchmarks with::
import dipy.reconst as dire
dire.bench()
If you have doctests enabled by default in nose (with a noserc file or
environment variable), and you have a numpy version <= 1.6.1, this will also run
the doctests, let's hope they pass.
Run this benchmark with:
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_squash.py
"""
from __future__ import division, print_function, absolute_import
from functools import reduce
import numpy as np
from dipy.core.ndindex import ndindex
from dipy.reconst.quick_squash import quick_squash
from numpy.testing import measure, dec
def old_squash(arr, mask=None, fill=0):
"""Try and make a standard array from an object array
This function takes an object array and attempts to convert it to a more
useful dtype. If array can be converted to a better dtype, Nones are
replaced by `fill`. To make the behaviour of this function more clear, here
are the most common cases:
1. `arr` is an array of scalars of type `T`. Returns an array like
`arr.astype(T)`
2. `arr` is an array of arrays. All items in `arr` have the same shape
`S`. Returns an array with shape `arr.shape + S`.
3. `arr` is an array of arrays of different shapes. Returns `arr`.
4. Items in `arr` are not ndarrys or scalars. Returns `arr`.
Parameters
----------
arr : array, dtype=object
The array to be converted.
mask : array, dtype=bool, optional
Where arr has Nones.
fill : number, optional
Nones are replaced by fill.
Returns
-------
result : array
Examples
--------
>>> arr = np.empty(3, dtype=object)
>>> arr.fill(2)
>>> old_squash(arr)
array([2, 2, 2])
>>> arr[0] = None
>>> old_squash(arr)
array([0, 2, 2])
>>> arr.fill(np.ones(2))
>>> r = old_squash(arr)
>>> r.shape
(3, 2)
>>> r.dtype
dtype('float64')
"""
if mask is None:
mask = arr != np.array(None)
not_none = arr[mask]
# all None, just return arr
if not_none.size == 0:
return arr
first = not_none[0]
# If the first item is an ndarray
if type(first) is np.ndarray:
shape = first.shape
try:
# Check the shapes of all items
all_same_shape = all(item.shape == shape for item in not_none)
except AttributeError:
return arr
# If items have different shapes just return arr
if not all_same_shape:
return arr
# Find common dtype. np.result_type can do this more simply, but it is
# only available for numpy 1.6.0
dtypes = set(a.dtype for a in not_none)
tiny_arrs = [np.zeros((1,), dtype=dt) for dt in dtypes]
dtype = reduce(np.add, tiny_arrs).dtype
# Create output array and fill
result = np.empty(arr.shape + shape, dtype=dtype)
result.fill(fill)
for ijk in ndindex(arr.shape):
if mask[ijk]:
result[ijk] = arr[ijk]
return result
# If the first item is a scalar
elif np.isscalar(first):
"first is not an ndarray"
all_scalars = all(np.isscalar(item) for item in not_none)
if not all_scalars:
return arr
# See comment about np.result_type above. We sum against the smallest
# possible type, bool, and let numpy type promotion find the best common
# type. The values might all be Python scalars so we need to cast to
# numpy type at the end to be sure of having a dtype.
dtype = np.asarray(sum(not_none, False)).dtype
temp = arr.copy()
temp[~mask] = fill
return temp.astype(dtype)
else:
return arr
def bench_quick_squash():
# nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench'
repeat = 10
shape = (300, 200)
arrs = np.zeros(shape, dtype=object)
scalars = np.zeros(shape, dtype=object)
for ijk in ndindex(arrs.shape):
arrs[ijk] = np.ones((3, 5))
scalars[ijk] = np.float32(0)
print('\nSquashing benchmarks')
for name, objs in (
('floats', np.zeros(shape, float).astype(object)),
('ints', np.zeros(shape, int).astype(object)),
('arrays', arrs),
('scalars', scalars),
):
print(name)
timed0 = measure("quick_squash(objs)", repeat)
timed1 = measure("old_squash(objs)", repeat)
print("fast %4.2f; slow %4.2f" % (timed0, timed1))
objs[50, 50] = None
timed0 = measure("quick_squash(objs)", repeat)
timed1 = measure("old_squash(objs)", repeat)
print("With None: fast %4.2f; slow %4.2f" % (timed0, timed1))
msk = objs != np.array(None)
timed0 = measure("quick_squash(objs, msk)", repeat)
timed1 = measure("old_squash(objs, msk)", repeat)
print("With mask: fast %4.2f; slow %4.2f" % (timed0, timed1))
objs[50, 50] = np.float32(0)
timed0 = measure("quick_squash(objs, msk)", repeat)
timed1 = measure("old_squash(objs, msk)", repeat)
print("Other dtype: fast %4.2f; slow %4.2f" % (timed0, timed1))
|