Skip to content

Commit

Permalink
Fix flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
roaffix committed Nov 13, 2020
1 parent 5e036b9 commit 0d9164c
Show file tree
Hide file tree
Showing 20 changed files with 602 additions and 658 deletions.
56 changes: 31 additions & 25 deletions arrayfire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@
"""

from .algorithm import (
accum, all_true, allTrueByKey, any_true, anyTrueByKey, count, countByKey, diff1, diff2, imax, imin, max, maxByKey, min, minByKey, product, productByKey, scan, scan_by_key, set_intersect,
set_union, set_unique, sort, sort_by_key, sort_index, sum, sumByKey, where)
accum, all_true, allTrueByKey, any_true, anyTrueByKey, count, countByKey, diff1, diff2, imax, imin, max, maxByKey,
min, minByKey, product, productByKey, scan, scan_by_key, set_intersect, set_union, set_unique, sort, sort_by_key,
sort_index, sum, sumByKey, where)
from .arith import (
abs, acos, acosh, arg, asin, asinh, atan, atan2, atanh, cast, cbrt, ceil, clamp, conjg, cos, cosh, cplx, erf, erfc, exp,
expm1, factorial, floor, hypot, imag, isinf, isnan, iszero, lgamma, log, log1p, log2, log10, maxof, minof,
abs, acos, acosh, arg, asin, asinh, atan, atan2, atanh, cast, cbrt, ceil, clamp, conjg, cos, cosh, cplx, erf, erfc,
exp, expm1, factorial, floor, hypot, imag, isinf, isnan, iszero, lgamma, log, log1p, log2, log10, maxof, minof,
mod, pow,
pow2, real, rem, root, round, rsqrt, sigmoid, sign, sin, sinh, sqrt, tan, tanh, tgamma, trunc)
from .array import (
Expand All @@ -64,45 +65,50 @@
from .blas import dot, matmul, matmulNT, matmulTN, matmulTT, gemm
from .cuda import get_native_id, get_stream, set_native_id
from .data import (
constant, diag, flat, flip, identity, iota, join, lookup, lower, moddims, pad, range, reorder, replace, select, shift,
tile, upper)
constant, diag, flat, flip, identity, iota, join, lookup, lower, moddims, pad, range, reorder, replace, select,
shift, tile, upper)
from .library import (
BACKEND, BINARYOP, CANNY_THRESHOLD, COLORMAP, CONNECTIVITY, CONV_DOMAIN, CONV_GRADIENT, CONV_MODE, CSPACE, DIFFUSION, ERR, FLUX,
HOMOGRAPHY, IMAGE_FORMAT, INTERP, ITERATIVE_DECONV, INVERSE_DECONV, MARKER, MATCH, MATPROP, MOMENT, NORM, PAD, RANDOM_ENGINE, STORAGE, TOPK, VARIANCE, YCC_STD, Dtype, Source, AF_VER_MAJOR, FORGE_VER_MAJOR)
BACKEND, BINARYOP, CANNY_THRESHOLD, COLORMAP, CONNECTIVITY, CONV_DOMAIN, CONV_GRADIENT, CONV_MODE, CSPACE,
DIFFUSION, ERR, FLUX, HOMOGRAPHY, IMAGE_FORMAT, INTERP, ITERATIVE_DECONV, INVERSE_DECONV, MARKER, MATCH, MATPROP,
MOMENT, NORM, PAD, RANDOM_ENGINE, STORAGE, TOPK, VARIANCE, YCC_STD, Dtype, Source, AF_VER_MAJOR, FORGE_VER_MAJOR)
from .device import (
alloc_device, alloc_host, alloc_pinned, device_gc, device_info, device_mem_info, eval, free_device, free_host,
free_pinned, get_device, get_device_count, get_device_ptr, get_manual_eval_flag, info,
info_str, init, is_dbl_supported, is_half_supported, is_locked_array, lock_array, lock_device_ptr, print_mem_info, set_device,
set_manual_eval_flag, sync, unlock_array, unlock_device_ptr)
info_str, init, is_dbl_supported, is_half_supported, is_locked_array, lock_array, lock_device_ptr, print_mem_info,
set_device, set_manual_eval_flag, sync, unlock_array, unlock_device_ptr)
from .graphics import Window
from .image import (
anisotropic_diffusion, bilateral, canny, color_space, confidenceCC, dilate, dilate3, erode, erode3, gaussian_kernel, gradient,
gray2rgb, hist_equal, histogram, hsv2rgb, is_image_io_available, iterativeDeconv, inverseDeconv, load_image, load_image_native, maxfilt,
mean_shift, minfilt, moments, regions, resize, rgb2gray, rgb2hsv, rgb2ycbcr, rotate, sat, save_image,
save_image_native, scale, skew, sobel_derivatives, sobel_filter, transform, translate, unwrap, wrap, ycbcr2rgb)
anisotropic_diffusion, bilateral, canny, color_space, confidenceCC, dilate, dilate3, erode, erode3,
gaussian_kernel, gradient, gray2rgb, hist_equal, histogram, hsv2rgb, is_image_io_available, iterativeDeconv,
inverseDeconv, load_image, load_image_native, maxfilt, mean_shift, minfilt, moments, regions, resize, rgb2gray,
rgb2hsv, rgb2ycbcr, rotate, sat, save_image, save_image_native, scale, skew, sobel_derivatives, sobel_filter,
transform, translate, unwrap, wrap, ycbcr2rgb)
from .index import Index, ParallelRange, Seq
from .interop import AF_NUMBA_FOUND, AF_NUMPY_FOUND, AF_PYCUDA_FOUND, AF_PYOPENCL_FOUND, to_array
from .lapack import (
cholesky, cholesky_inplace, det, inverse, is_lapack_available, lu, lu_inplace, norm, pinverse, qr, qr_inplace, rank, solve,
solve_lu, svd, svd_inplace)
cholesky, cholesky_inplace, det, inverse, is_lapack_available, lu, lu_inplace, norm, pinverse, qr, qr_inplace,
rank, solve, solve_lu, svd, svd_inplace)
from .library import (
get_active_backend, get_available_backends, get_backend, get_backend_count, get_backend_id, get_device_id,
get_size_of, safe_call, set_backend)
get_size_of, safe_call, set_backend, to_str)
from .ml import convolve2GradientNN
from .random import (
Random_Engine, get_default_random_engine, get_seed, randn, randu, set_default_random_engine_type,
set_seed)
from .signal import (
approx1, approx1_uniform, approx2, approx2_uniform, convolve, convolve1, convolve2, convolve2NN, convolve2_separable, convolve3, dft, fft, fft2, fft2_c2r,
fft2_inplace, fft2_r2c, fft3, fft3_c2r, fft3_inplace, fft3_r2c, fft_c2r, fft_convolve, fft_convolve1,
fft_convolve2, fft_convolve3, fft_inplace, fft_r2c, fir, idft, ifft, ifft2, ifft2_inplace, ifft3, ifft3_inplace,
ifft_inplace, iir, medfilt, medfilt1, medfilt2, set_fft_plan_cache_size)
approx1, approx1_uniform, approx2, approx2_uniform, convolve, convolve1, convolve2, convolve2NN,
convolve2_separable, convolve3, dft, fft, fft2, fft2_c2r, fft2_inplace, fft2_r2c, fft3, fft3_c2r, fft3_inplace,
fft3_r2c, fft_c2r, fft_convolve, fft_convolve1, fft_convolve2, fft_convolve3, fft_inplace, fft_r2c, fir, idft,
ifft, ifft2, ifft2_inplace, ifft3, ifft3_inplace, ifft_inplace, iir, medfilt, medfilt1, medfilt2,
set_fft_plan_cache_size)
from .sparse import (
convert_sparse, convert_sparse_to_dense, create_sparse, create_sparse_from_dense, create_sparse_from_host,
sparse_get_col_idx, sparse_get_info, sparse_get_nnz, sparse_get_row_idx, sparse_get_storage, sparse_get_values)
from .statistics import corrcoef, cov, mean, meanvar, median, stdev, topk, var
from .timer import timeit
from .util import dim4, dim4_to_tuple, implicit_dtype, number_dtype, to_str, get_reversion, get_version, to_dtype, to_typecode, to_c_type
from .util import (
dim4, dim4_to_tuple, implicit_dtype, number_dtype, get_reversion, get_version, to_dtype, to_typecode,
to_c_type)

try:
# FIXME: pycuda imported but unused
Expand Down Expand Up @@ -133,7 +139,7 @@
"broadcast",
# blas
"dot", "matmul", "matmulNT", "matmulTN", "matmulTT", "gemm",
#cuda
# cuda
"get_native_id", "get_stream", "set_native_id",
# data
"constant", "diag", "flat", "flip", "identity", "iota", "join", "lookup",
Expand Down Expand Up @@ -169,7 +175,7 @@
"norm", "pinverse", "qr", "qr_inplace", "rank", "solve", "solve_lu", "svd", "svd_inplace",
# library
"get_active_backend", "get_available_backends", "get_backend", "get_backend_count",
"get_backend_id", "get_device_id", "get_size_of", "safe_call", "set_backend",
"get_backend_id", "get_device_id", "get_size_of", "safe_call", "set_backend", "to_str",
# ml
"convolve2GradientNN",
# random
Expand All @@ -192,6 +198,6 @@
# timer
"timeit",
# util
"dim4", "dim4_to_tuple", "implicit_dtype", "number_dtype", "to_str", "get_reversion",
"dim4", "dim4_to_tuple", "implicit_dtype", "number_dtype", "get_reversion",
"get_version", "to_dtype", "to_typecode", "to_c_type"
]
67 changes: 40 additions & 27 deletions arrayfire/algorithm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#######################################################
# Copyright (c) 2019, ArrayFire
# Copyright (c) 2020, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
Expand All @@ -14,11 +14,13 @@
from .array import Array
from .library import backend, safe_call, BINARYOP, c_bool_t, c_double_t, c_int_t, c_pointer, c_uint_t


def _parallel_dim(a, dim, c_func):
out = Array()
safe_call(c_func(c_pointer(out.arr), a.arr, c_int_t(dim)))
return out


def _reduce_all(a, c_func):
real = c_double_t(0)
imag = c_double_t(0)
Expand All @@ -29,11 +31,13 @@ def _reduce_all(a, c_func):
imag = imag.value
return real if imag == 0 else real + imag * 1j


def _nan_parallel_dim(a, dim, c_func, nan_val):
out = Array()
safe_call(c_func(c_pointer(out.arr), a.arr, c_int_t(dim), c_double_t(nan_val)))
return out


def _nan_reduce_all(a, c_func, nan_val):
real = c_double_t(0)
imag = c_double_t(0)
Expand All @@ -44,6 +48,7 @@ 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)
Expand All @@ -55,20 +60,26 @@ def _FNSD(dim, dims):
break
return int(fnsd)


def _rbk_dim(keys, vals, dim, c_func):
keys_out = Array()
vals_out = Array()
rdim = _FNSD(dim, vals.dims())
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()
# FIXME: vals is undefined
rdim = _FNSD(dim, vals.dims())
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)))
# FIXME: keys is undefined
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 All @@ -88,18 +99,16 @@ def sum(a, dim=None, nan_val=None):
The sum of all elements in `a` along dimension `dim`.
If `dim` is `None`, sum of the entire Array is returned.
"""
if nan_val is not None:
if dim is not None:
if nan_val:
if dim:
return _nan_parallel_dim(a, dim, backend.get().af_sum_nan, nan_val)
return _nan_reduce_all(a, backend.get().af_sum_nan_all, nan_val)

if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_sum)
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.
Expand All @@ -122,10 +131,10 @@ def sumByKey(keys, vals, dim=-1, nan_val=None):
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):
if nan_val:
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)
return _rbk_dim(keys, vals, dim, backend.get().af_sum_by_key)


def product(a, dim=None, nan_val=None):
"""
Expand All @@ -146,12 +155,12 @@ def product(a, dim=None, nan_val=None):
The product of all elements in `a` along dimension `dim`.
If `dim` is `None`, product of the entire Array is returned.
"""
if nan_val is not None:
if dim is not None:
if nan_val:
if dim:
return _nan_parallel_dim(a, dim, backend.get().af_product_nan, nan_val)
return _nan_reduce_all(a, backend.get().af_product_nan_all, nan_val)

if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_product)
return _reduce_all(a, backend.get().af_product_all)

Expand All @@ -178,10 +187,10 @@ def productByKey(keys, vals, dim=-1, nan_val=None):
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):
if nan_val:
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)
return _rbk_dim(keys, vals, dim, backend.get().af_product_by_key)


def min(a, dim=None):
"""
Expand All @@ -200,7 +209,7 @@ def min(a, dim=None):
The minimum value of all elements in `a` along dimension `dim`.
If `dim` is `None`, minimum value of the entire Array is returned.
"""
if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_min)
return _reduce_all(a, backend.get().af_min_all)

Expand All @@ -227,6 +236,7 @@ def minByKey(keys, vals, dim=-1):
"""
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 @@ -244,7 +254,7 @@ def max(a, dim=None):
The maximum value of all elements in `a` along dimension `dim`.
If `dim` is `None`, maximum value of the entire Array is returned.
"""
if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_max)
return _reduce_all(a, backend.get().af_max_all)

Expand All @@ -271,6 +281,7 @@ def maxByKey(keys, vals, dim=-1):
"""
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 @@ -288,7 +299,7 @@ def all_true(a, dim=None):
Af.array containing True if all elements in `a` along the dimension are True.
If `dim` is `None`, output is True if `a` does not have any zeros, else False.
"""
if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_all_true)
return _reduce_all(a, backend.get().af_all_true_all)

Expand All @@ -315,6 +326,7 @@ def allTrueByKey(keys, vals, dim=-1):
"""
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 @@ -332,10 +344,10 @@ def any_true(a, dim=None):
Af.array containing True if any elements in `a` along the dimension are True.
If `dim` is `None`, output is True if `a` does not have any zeros, else False.
"""
if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_any_true)
else:
return _reduce_all(a, backend.get().af_any_true_all)
return _reduce_all(a, backend.get().af_any_true_all)


def anyTrueByKey(keys, vals, dim=-1):
"""
Expand All @@ -359,6 +371,7 @@ def anyTrueByKey(keys, vals, dim=-1):
"""
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 @@ -376,10 +389,9 @@ def count(a, dim=None):
The count of non zero elements in `a` along `dim`.
If `dim` is `None`, the total number of non zero elements in `a`.
"""
if dim is not None:
if dim:
return _parallel_dim(a, dim, backend.get().af_count)
else:
return _reduce_all(a, backend.get().af_count_all)
return _reduce_all(a, backend.get().af_count_all)


def countByKey(keys, vals, dim=-1):
Expand All @@ -404,6 +416,7 @@ def countByKey(keys, vals, dim=-1):
"""
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 All @@ -422,7 +435,7 @@ def imin(a, dim=None):
`idx` contains the location of where `val` occurs in `a` along `dim`.
If `dim` is `None`, `val` and `idx` value and location of global minimum.
"""
if dim is not None:
if dim:
out = Array()
idx = Array()
safe_call(backend.get().af_imin(c_pointer(out.arr), c_pointer(idx.arr), a.arr, c_int_t(dim)))
Expand Down Expand Up @@ -456,7 +469,7 @@ def imax(a, dim=None):
`idx` contains the location of where `val` occurs in `a` along `dim`.
If `dim` is `None`, `val` and `idx` value and location of global maximum.
"""
if dim is not None:
if dim:
out = Array()
idx = Array()
safe_call(backend.get().af_imax(c_pointer(out.arr), c_pointer(idx.arr), a.arr, c_int_t(dim)))
Expand Down
Loading

0 comments on commit 0d9164c

Please sign in to comment.