/usr/lib/python3/dist-packages/pytools/obj_array.py is in python3-pytools 2017.6-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 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 | from __future__ import absolute_import, division
import numpy as np
from pytools import my_decorator as decorator, MovedFunctionDeprecationWrapper
__doc__ = """
Handling :mod:`numpy` Object Arrays
===================================
.. autofunction:: oarray_real
.. autofunction:: oarray_imag
.. autofunction:: oarray_real_copy
.. autofunction:: oarray_imag_copy
Creation
--------
.. autofunction:: join_fields
.. autofunction:: make_obj_array
Mapping
-------
.. autofunction:: with_object_array_or_scalar
.. autofunction:: with_object_array_or_scalar_n_args
"""
def gen_len(expr):
from pytools.obj_array import is_obj_array
if is_obj_array(expr):
return len(expr)
else:
return 1
def gen_slice(expr, slice):
result = expr[slice]
if len(result) == 1:
return result[0]
else:
return result
def is_obj_array(val):
try:
return isinstance(val, np.ndarray) and val.dtype == object
except AttributeError:
return False
def to_obj_array(ary):
ls = log_shape(ary)
result = np.empty(ls, dtype=object)
from pytools import indices_in_shape
for i in indices_in_shape(ls):
result[i] = ary[i]
return result
def is_field_equal(a, b):
if is_obj_array(a):
return is_obj_array(b) and (a.shape == b.shape) and (a == b).all()
else:
return not is_obj_array(b) and a == b
def make_obj_array(res_list):
result = np.empty((len(res_list),), dtype=object)
for i, v in enumerate(res_list):
result[i] = v
return result
def setify_field(f):
from hedge.tools import is_obj_array
if is_obj_array(f):
return set(f)
else:
return set([f])
def obj_array_to_hashable(f):
if is_obj_array(f):
return tuple(f)
else:
return f
hashable_field = MovedFunctionDeprecationWrapper(obj_array_to_hashable)
def obj_array_equal(a, b):
a_is_oa = is_obj_array(a)
assert a_is_oa == is_obj_array(b)
if a_is_oa:
return np.array_equal(a, b)
else:
return a == b
field_equal = MovedFunctionDeprecationWrapper(obj_array_equal)
def join_fields(*args):
res_list = []
for arg in args:
if isinstance(arg, list):
res_list.extend(arg)
elif isinstance(arg, np.ndarray):
if log_shape(arg) == ():
res_list.append(arg)
else:
res_list.extend(arg.flat)
else:
res_list.append(arg)
return make_obj_array(res_list)
def log_shape(array):
"""Returns the "logical shape" of the array.
The "logical shape" is the shape that's left when the node-depending
dimension has been eliminated."""
try:
if array.dtype.char == "O":
return array.shape
else:
return array.shape[:-1]
except AttributeError:
return ()
def with_object_array_or_scalar(f, field, obj_array_only=False):
if obj_array_only:
if is_obj_array(field):
ls = field.shape
else:
ls = ()
else:
ls = log_shape(field)
if ls != ():
from pytools import indices_in_shape
result = np.zeros(ls, dtype=object)
for i in indices_in_shape(ls):
result[i] = f(field[i])
return result
else:
return f(field)
as_oarray_func = decorator(with_object_array_or_scalar)
def with_object_array_or_scalar_n_args(f, *args):
oarray_arg_indices = []
for i, arg in enumerate(args):
if is_obj_array(arg):
oarray_arg_indices.append(i)
if not oarray_arg_indices:
return f(*args)
leading_oa_index = oarray_arg_indices[0]
ls = log_shape(args[leading_oa_index])
if ls != ():
from pytools import indices_in_shape
result = np.zeros(ls, dtype=object)
new_args = list(args)
for i in indices_in_shape(ls):
for arg_i in oarray_arg_indices:
new_args[arg_i] = args[arg_i][i]
result[i] = f(*new_args)
return result
else:
return f(*args)
as_oarray_func_n_args = decorator(with_object_array_or_scalar_n_args)
def cast_field(field, dtype):
return with_object_array_or_scalar(
lambda f: f.astype(dtype), field)
def oarray_real(ary):
return with_object_array_or_scalar(lambda x: x.real, ary)
def oarray_imag(ary):
return with_object_array_or_scalar(lambda x: x.imag, ary)
def oarray_real_copy(ary):
return with_object_array_or_scalar(lambda x: x.real.copy(), ary)
def oarray_imag_copy(ary):
return with_object_array_or_scalar(lambda x: x.imag.copy(), ary)
|