Skip to content

Add 3.7 features to python wrapper #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 9, 2020
1 change: 1 addition & 0 deletions arrayfire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from .timer import *
from .random import *
from .sparse import *
from .ml import *

# do not export default modules as part of arrayfire
del ct
Expand Down
192 changes: 192 additions & 0 deletions arrayfire/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ def _nan_reduce_all(a, c_func, nan_val):
imag = imag.value
return real if imag == 0 else real + imag * 1j

def _FNSD(dim, dims):
if dim >= 0:
return int(dim)

fnsd = 0
for i, d in enumerate(dims):
if d > 1:
fnsd = i
break
return int(fnsd)

def _rbk_dim(keys, vals, dim, c_func):
keys_out = Array()
vals_out = Array()
rdim = _FNSD(dim, vals.dims())
print(rdim)
safe_call(c_func(c_pointer(keys_out.arr), c_pointer(vals_out.arr), keys.arr, vals.arr, c_int_t(rdim)))
return keys_out, vals_out

def _nan_rbk_dim(a, dim, c_func, nan_val):
keys_out = Array()
vals_out = Array()
rdim = _FNSD(dim, vals.dims())
print(rdim)
safe_call(c_func(c_pointer(keys_out.arr), c_pointer(vals_out.arr), keys.arr, vals.arr, c_int_t(rdim), c_double_t(nan_val)))
return keys_out, vals_out

def sum(a, dim=None, nan_val=None):
"""
Calculate the sum of all the elements along a specified dimension.
Expand Down Expand Up @@ -74,6 +101,34 @@ def sum(a, dim=None, nan_val=None):
else:
return _reduce_all(a, backend.get().af_sum_all)


def sumByKey(keys, vals, dim=-1, nan_val=None):
"""
Calculate the sum of elements along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the sum will occur.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array

Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The sum of all elements in `vals` along dimension `dim` according to keys
"""
if (nan_val is not None):
return _nan_rbk_dim(keys, vals, dim, backend.get().af_sum_by_key_nan, nan_val)
else:
return _rbk_dim(keys, vals, dim, backend.get().af_sum_by_key)

def product(a, dim=None, nan_val=None):
"""
Calculate the product of all the elements along a specified dimension.
Expand Down Expand Up @@ -104,6 +159,33 @@ def product(a, dim=None, nan_val=None):
else:
return _reduce_all(a, backend.get().af_product_all)

def productByKey(keys, vals, dim=-1, nan_val=None):
"""
Calculate the product of elements along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the product will occur.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array

Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The product of all elements in `vals` along dimension `dim` according to keys
"""
if (nan_val is not None):
return _nan_rbk_dim(keys, vals, dim, backend.get().af_product_by_key_nan, nan_val)
else:
return _rbk_dim(keys, vals, dim, backend.get().af_product_by_key)

def min(a, dim=None):
"""
Find the minimum value of all the elements along a specified dimension.
Expand All @@ -126,6 +208,28 @@ def min(a, dim=None):
else:
return _reduce_all(a, backend.get().af_min_all)

def minByKey(keys, vals, dim=-1):
"""
Calculate the min of elements along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the min will occur.

Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The min of all elements in `vals` along dimension `dim` according to keys
"""
return _rbk_dim(keys, vals, dim, backend.get().af_min_by_key)

def max(a, dim=None):
"""
Find the maximum value of all the elements along a specified dimension.
Expand All @@ -148,6 +252,28 @@ def max(a, dim=None):
else:
return _reduce_all(a, backend.get().af_max_all)

def maxByKey(keys, vals, dim=-1):
"""
Calculate the max of elements along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the max will occur.

Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The max of all elements in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_max_by_key)

def all_true(a, dim=None):
"""
Check if all the elements along a specified dimension are true.
Expand All @@ -170,6 +296,28 @@ def all_true(a, dim=None):
else:
return _reduce_all(a, backend.get().af_all_true_all)

def allTrueByKey(keys, vals, dim=-1):
"""
Calculate if all elements are true along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the all true check will occur.

Returns
-------
keys: af.Array or scalar number
The reduced keys of all true check in `vals` along dimension `dim`.
values: af.Array or scalar number
Booleans denoting if all elements are true in `vals` along dimension `dim` according to keys
"""
return _rbk_dim(keys, vals, dim, backend.get().af_all_true_by_key)

def any_true(a, dim=None):
"""
Check if any the elements along a specified dimension are true.
Expand All @@ -192,6 +340,28 @@ def any_true(a, dim=None):
else:
return _reduce_all(a, backend.get().af_any_true_all)

def anyTrueByKey(keys, vals, dim=-1):
"""
Calculate if any elements are true along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the any true check will occur.

Returns
-------
keys: af.Array or scalar number
The reduced keys of any true check in `vals` along dimension `dim`.
values: af.Array or scalar number
Booleans denoting if any elements are true in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_any_true_by_key)

def count(a, dim=None):
"""
Count the number of non zero elements in an array along a specified dimension.
Expand All @@ -214,6 +384,28 @@ def count(a, dim=None):
else:
return _reduce_all(a, backend.get().af_count_all)

def countByKey(keys, vals, dim=-1):
"""
Counts non-zero elements along a specified dimension according to a key.

Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which to count elements.

Returns
-------
keys: af.Array or scalar number
The reduced keys of count in `vals` along dimension `dim`.
values: af.Array or scalar number
Count of non-zero elements in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_count_by_key)

def imin(a, dim=None):
"""
Find the value and location of the minimum value along a specified dimension
Expand Down
20 changes: 20 additions & 0 deletions arrayfire/arith.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,26 @@ def sqrt(a):
"""
return _arith_unary_func(a, backend.get().af_sqrt)

def rsqrt(a):
"""
Reciprocal or inverse square root of each element in the array.

Parameters
----------
a : af.Array
Multi dimensional arrayfire array.

Returns
--------
out : af.Array
array containing the inverse square root of each value from `a`.

Note
-------
`a` must not be complex.
"""
return _arith_unary_func(a, backend.get().af_rsqrt)

def cbrt(a):
"""
Cube root of each element in the array.
Expand Down
51 changes: 51 additions & 0 deletions arrayfire/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,57 @@ def replace(lhs, cond, rhs):
else:
safe_call(backend.get().af_replace_scalar(lhs.arr, cond.arr, c_double_t(rhs)))

def pad(a, beginPadding, endPadding, padFillType = PAD.ZERO):
"""
Pad an array

This function will pad an array with the specified border size and tiling scheme

Parameters
----------

a: af.Array
A multi dimensional input arrayfire array.

beginPadding: tuple of ints. default: (0, 0, 0, 0).

endPadding: tuple of ints. default: (0, 0, 0, 0).

padFillType: optional af.PAD default: af.PAD.ZERO
specifies type of values to fill padded border with

Returns
-------
output: af.Array
A padded array

Examples
---------
>>> import arrayfire as af
>>> a = af.randu(3,3)
>>> af.display(a)
[3 3 1 1]
0.4107 0.1794 0.3775
0.8224 0.4198 0.3027
0.9518 0.0081 0.6456

>>> padded = af.pad(a, (1, 1), (1, 1), af.ZERO)
>>> af.display(padded)
[5 5 1 1]
0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.4107 0.1794 0.3775 0.0000
0.0000 0.8224 0.4198 0.3027 0.0000
0.0000 0.9518 0.0081 0.6456 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000
"""
out = Array()
begin_dims = dim4(beginPadding[0], beginPadding[1], beginPadding[2], beginPadding[3])
end_dims = dim4(endPadding[0], endPadding[1], endPadding[2], endPadding[3])

safe_call(backend.get().af_pad(c_pointer(out.arr), a.arr, 4, c_pointer(begin_dims), 4, c_pointer(end_dims), padFillType.value))
return out


def lookup(a, idx, dim=0):
"""
Lookup the values of input array based on index.
Expand Down
43 changes: 43 additions & 0 deletions arrayfire/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,49 @@ def regions(image, conn = CONNECTIVITY.FOUR, out_type = Dtype.f32):
conn.value, out_type.value))
return output

def confidenceCC(image, seedx, seedy, radius, multiplier, iters, segmented_value):
"""
Find the confidence connected components in the image.

Parameters
----------
image : af.Array
- A 2 D arrayfire array representing an image.
Expects non-integral type

seedx : af.Array
- An array with x-coordinates of seed points

seedy : af.Array
- An array with y-coordinates of seed points

radius : scalar
- The neighborhood region to be considered around
each seed point

multiplier : scalar
- Controls the threshold range computed from
the mean and variance of seed point neighborhoods

iters : scalar
- is number of iterations

segmented_value : scalar
- the value to which output array valid
pixels are set to.

Returns
---------

output : af.Array
- Output array with resulting connected components

"""
output = Array()
safe_call(backend.get().af_confidence_cc(c_pointer(output.arr), image.arr, seedx.arr, seedy.arr,
c_uint_t(radius), c_uint_t(multiplier), c_int_t(iters), c_double_t(segmented_value)))
return output

def sobel_derivatives(image, w_len=3):
"""
Find the sobel derivatives of the image.
Expand Down
Loading