Ejemplo n.º 1
0
def check_divergence(self):
    """
    Used to terminate the program if a blowup occurs in any segment
    of the solver, resulting in the values becoming infinity or 
    undefined.
    """

    if (af.any_true(af.isinf(self.f)) or af.any_true(af.isnan(self.f))):
        raise SystemExit('Solver Diverging!')
def _get_mask(X, value_to_mask):
    """Compute the boolean mask X == value_to_mask."""
    # BUG: doesnt work properly
    npdtype = typemap(X.dtype())
    if is_scalar_nan(value_to_mask):
        if npdtype.kind == "f":
            return af.isnan(X)
        elif X.dtype.kind in ("i", "u"):
            # can't have NaNs in integer array.
            return af.constant(0, X.shape[0], X.shape[1], dtype=af.Dtype.b8)
        else:
            # np.isnan does not work on object dtypes.
            return _object_dtype_isnan(X)  #todo:fix
    else:
        return X == value_to_mask
Ejemplo n.º 3
0
    def __init__(self, phase_obj_3d, wavelength, **kwargs):
        super().__init__(phase_obj_3d, wavelength, **kwargs)
        self.distance_end_to_center = np.sum(
            self.slice_separation) / 2. + phase_obj_3d.slice_separation[-1]
        self.distance_beginning_to_center = np.sum(self.slice_separation) / 2.
        self.slice_separation = np.append(phase_obj_3d.slice_separation,
                                          [phase_obj_3d.slice_separation[-1]])
        self.slice_separation             = np.asarray([sum(self.slice_separation[x:x+self.slice_binning_factor]) \
                                             for x in range(0,len(self.slice_separation),self.slice_binning_factor)])
        if self.slice_separation.shape[0] != self.shape[2]:
            print(
                "Number of slices does not match with number of separations!")

        # generate green's function convolution kernel
        fxlin, fylin = self._genFrequencyGrid()
        kernel_mask = (self.RI / self.wavelength)**2 > 1.01 * (
            fxlin * af.conjg(fxlin) + fylin * af.conjg(fylin))
        self.green_kernel_2d = -0.25j * af.exp(
            2.0j * np.pi * self.fzlin * self.pixel_size_z) / np.pi / self.fzlin
        self.green_kernel_2d *= kernel_mask
        self.green_kernel_2d[af.isnan(self.green_kernel_2d) == 1] = 0.0
Ejemplo n.º 4
0
def simple_arith(verbose = False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    a = af.randu(3,3,dtype=af.Dtype.u32)
    b = af.constant(4, 3, 3, dtype=af.Dtype.u32)
    display_func(a)
    display_func(b)

    c = a + b
    d = a
    d += b

    display_func(c)
    display_func(d)
    display_func(a + 2)
    display_func(3 + a)


    c = a - b
    d = a
    d -= b

    display_func(c)
    display_func(d)
    display_func(a - 2)
    display_func(3 - a)

    c = a * b
    d = a
    d *= b

    display_func(c * 2)
    display_func(3 * d)
    display_func(a * 2)
    display_func(3 * a)

    c = a / b
    d = a
    d /= b

    display_func(c / 2.0)
    display_func(3.0 / d)
    display_func(a / 2)
    display_func(3 / a)

    c = a % b
    d = a
    d %= b

    display_func(c % 2.0)
    display_func(3.0 % d)
    display_func(a % 2)
    display_func(3 % a)

    c = a ** b
    d = a
    d **= b

    display_func(c ** 2.0)
    display_func(3.0 ** d)
    display_func(a ** 2)
    display_func(3 ** a)

    display_func(a < b)
    display_func(a < 0.5)
    display_func(0.5 < a)

    display_func(a <= b)
    display_func(a <= 0.5)
    display_func(0.5 <= a)

    display_func(a > b)
    display_func(a > 0.5)
    display_func(0.5 > a)

    display_func(a >= b)
    display_func(a >= 0.5)
    display_func(0.5 >= a)

    display_func(a != b)
    display_func(a != 0.5)
    display_func(0.5 != a)

    display_func(a == b)
    display_func(a == 0.5)
    display_func(0.5 == a)

    display_func(a & b)
    display_func(a & 2)
    c = a
    c &= 2
    display_func(c)

    display_func(a | b)
    display_func(a | 2)
    c = a
    c |= 2
    display_func(c)

    display_func(a >> b)
    display_func(a >> 2)
    c = a
    c >>= 2
    display_func(c)

    display_func(a << b)
    display_func(a << 2)
    c = a
    c <<= 2
    display_func(c)

    display_func(-a)
    display_func(+a)
    display_func(~a)
    display_func(a)

    display_func(af.cast(a, af.Dtype.c32))
    display_func(af.maxof(a,b))
    display_func(af.minof(a,b))
    display_func(af.rem(a,b))

    a = af.randu(3,3) - 0.5
    b = af.randu(3,3) - 0.5

    display_func(af.abs(a))
    display_func(af.arg(a))
    display_func(af.sign(a))
    display_func(af.round(a))
    display_func(af.trunc(a))
    display_func(af.floor(a))
    display_func(af.ceil(a))
    display_func(af.hypot(a, b))
    display_func(af.sin(a))
    display_func(af.cos(a))
    display_func(af.tan(a))
    display_func(af.asin(a))
    display_func(af.acos(a))
    display_func(af.atan(a))
    display_func(af.atan2(a, b))

    c = af.cplx(a)
    d = af.cplx(a,b)
    display_func(c)
    display_func(d)
    display_func(af.real(d))
    display_func(af.imag(d))
    display_func(af.conjg(d))

    display_func(af.sinh(a))
    display_func(af.cosh(a))
    display_func(af.tanh(a))
    display_func(af.asinh(a))
    display_func(af.acosh(a))
    display_func(af.atanh(a))

    a = af.abs(a)
    b = af.abs(b)

    display_func(af.root(a, b))
    display_func(af.pow(a, b))
    display_func(af.pow2(a))
    display_func(af.exp(a))
    display_func(af.expm1(a))
    display_func(af.erf(a))
    display_func(af.erfc(a))
    display_func(af.log(a))
    display_func(af.log1p(a))
    display_func(af.log10(a))
    display_func(af.log2(a))
    display_func(af.sqrt(a))
    display_func(af.cbrt(a))

    a = af.round(5 * af.randu(3,3) - 1)
    b = af.round(5 * af.randu(3,3) - 1)

    display_func(af.factorial(a))
    display_func(af.tgamma(a))
    display_func(af.lgamma(a))
    display_func(af.iszero(a))
    display_func(af.isinf(a/b))
    display_func(af.isnan(a/a))

    a = af.randu(5, 1)
    b = af.randu(1, 5)
    c = af.broadcast(lambda x,y: x+y, a, b)
    display_func(a)
    display_func(b)
    display_func(c)

    @af.broadcast
    def test_add(aa, bb):
        return aa + bb

    display_func(test_add(a, b))
    def transform(self, X):
        """Impute all missing values in X.
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The input data to complete.
        """
        check_is_fitted(self)

        X = self._validate_input(X, in_fit=False)
        #X = af.Array.to_ndarray(X)
        X_indicator = super()._transform_indicator(X)

        statistics = self.statistics_

        if X.shape[1] != statistics.shape[0]:
            raise ValueError(
                f"X has {X.shape[1]} features per sample, expected {self.statistics_.shape[0]}"
            )

        # Delete the invalid columns if strategy is not constant
        if self.strategy == "constant":
            valid_statistics = statistics
        else:
            # same as af.isnan but also works for object dtypes
            # invalid_mask = _get_mask(statistics, np.nan)  # BUG: af runtime error
            invalid_mask = af.isnan(statistics)  # FIXME
            valid_mask = invalid_mask.logical_not()
            valid_statistics = statistics[valid_mask]
            valid_statistics_indexes = np.flatnonzero(valid_mask)

            if af.any_true(invalid_mask):
                missing = af.arange(X.shape[1])[invalid_mask]
                if self.verbose:
                    warnings.warn(
                        f"Deleting features without observed values: {missing}"
                    )
                X = X[:, valid_statistics_indexes]

        # Do actual imputation
        if sp.issparse(X):
            if self.missing_values == 0:
                raise ValueError(
                    "Imputation not possible when missing_values == 0 and input is sparse."
                    "Provide a dense array instead.")
            else:
                mask = _get_mask(X.data, self.missing_values)
                indexes = af.repeat(af.arange(len(X.indptr) - 1, dtype=af.int),
                                    af.diff(X.indptr))[mask]

                X.data[mask] = valid_statistics[indexes].astype(X.dtype,
                                                                copy=False)
        else:
            # mask = _get_mask(X, self.missing_values)  # BUG
            mask = af.isnan(X)  # FIXME
            # n_missing = af.sum(mask, axis=0)  # BUG af
            n_missing = af.sum(mask, dim=0)
            coordinates = af.where(mask.T)[::-1]  # BUG
            valid_statistics = valid_statistics.to_ndarray().ravel()
            n_missing = n_missing.to_ndarray().ravel()
            values = np.repeat(valid_statistics, n_missing)  # BUG
            values = af.interop.from_ndarray(values)

            odims = X.dims()
            X = af.flat(X)
            X[coordinates] = values
            X = af.moddims(X, *odims)

        return super()._concatenate_indicator(X, X_indicator)
Ejemplo n.º 6
0
def isnan(x):
    if not isinstance(x, afnumpy.ndarray):
        return numpy.isnan(x)
    s = arrayfire.isnan(x.d_array)
    return afnumpy.ndarray(x.shape, dtype=pu.typemap(s.dtype()), af_array=s)
Ejemplo n.º 7
0
def check_divergence(self):
    if (af.any_true(af.isinf(self.f)) or af.any_true(af.isnan(self.f))):
        raise SystemExit('Solver Diverging!')
Ejemplo n.º 8
0
def simple_arith(verbose=False):
    display_func = _util.display_func(verbose)
    print_func = _util.print_func(verbose)

    a = af.randu(3, 3)
    b = af.constant(4, 3, 3)
    display_func(a)
    display_func(b)

    c = a + b
    d = a
    d += b

    display_func(c)
    display_func(d)
    display_func(a + 2)
    display_func(3 + a)

    c = a - b
    d = a
    d -= b

    display_func(c)
    display_func(d)
    display_func(a - 2)
    display_func(3 - a)

    c = a * b
    d = a
    d *= b

    display_func(c * 2)
    display_func(3 * d)
    display_func(a * 2)
    display_func(3 * a)

    c = a / b
    d = a
    d /= b

    display_func(c / 2.0)
    display_func(3.0 / d)
    display_func(a / 2)
    display_func(3 / a)

    c = a % b
    d = a
    d %= b

    display_func(c % 2.0)
    display_func(3.0 % d)
    display_func(a % 2)
    display_func(3 % a)

    c = a**b
    d = a
    d **= b

    display_func(c**2.0)
    display_func(3.0**d)
    display_func(a**2)
    display_func(3**a)

    display_func(a < b)
    display_func(a < 0.5)
    display_func(0.5 < a)

    display_func(a <= b)
    display_func(a <= 0.5)
    display_func(0.5 <= a)

    display_func(a > b)
    display_func(a > 0.5)
    display_func(0.5 > a)

    display_func(a >= b)
    display_func(a >= 0.5)
    display_func(0.5 >= a)

    display_func(a != b)
    display_func(a != 0.5)
    display_func(0.5 != a)

    display_func(a == b)
    display_func(a == 0.5)
    display_func(0.5 == a)

    a = af.randu(3, 3, dtype=af.Dtype.u32)
    b = af.constant(4, 3, 3, dtype=af.Dtype.u32)

    display_func(a & b)
    display_func(a & 2)
    c = a
    c &= 2
    display_func(c)

    display_func(a | b)
    display_func(a | 2)
    c = a
    c |= 2
    display_func(c)

    display_func(a >> b)
    display_func(a >> 2)
    c = a
    c >>= 2
    display_func(c)

    display_func(a << b)
    display_func(a << 2)
    c = a
    c <<= 2
    display_func(c)

    display_func(-a)
    display_func(+a)
    display_func(~a)
    display_func(a)

    display_func(af.cast(a, af.Dtype.c32))
    display_func(af.maxof(a, b))
    display_func(af.minof(a, b))
    display_func(af.rem(a, b))

    a = af.randu(3, 3) - 0.5
    b = af.randu(3, 3) - 0.5

    display_func(af.abs(a))
    display_func(af.arg(a))
    display_func(af.sign(a))
    display_func(af.round(a))
    display_func(af.trunc(a))
    display_func(af.floor(a))
    display_func(af.ceil(a))
    display_func(af.hypot(a, b))
    display_func(af.sin(a))
    display_func(af.cos(a))
    display_func(af.tan(a))
    display_func(af.asin(a))
    display_func(af.acos(a))
    display_func(af.atan(a))
    display_func(af.atan2(a, b))

    c = af.cplx(a)
    d = af.cplx(a, b)
    display_func(c)
    display_func(d)
    display_func(af.real(d))
    display_func(af.imag(d))
    display_func(af.conjg(d))

    display_func(af.sinh(a))
    display_func(af.cosh(a))
    display_func(af.tanh(a))
    display_func(af.asinh(a))
    display_func(af.acosh(a))
    display_func(af.atanh(a))

    a = af.abs(a)
    b = af.abs(b)

    display_func(af.root(a, b))
    display_func(af.pow(a, b))
    display_func(af.pow2(a))
    display_func(af.sigmoid(a))
    display_func(af.exp(a))
    display_func(af.expm1(a))
    display_func(af.erf(a))
    display_func(af.erfc(a))
    display_func(af.log(a))
    display_func(af.log1p(a))
    display_func(af.log10(a))
    display_func(af.log2(a))
    display_func(af.sqrt(a))
    display_func(af.cbrt(a))

    a = af.round(5 * af.randu(3, 3) - 1)
    b = af.round(5 * af.randu(3, 3) - 1)

    display_func(af.factorial(a))
    display_func(af.tgamma(a))
    display_func(af.lgamma(a))
    display_func(af.iszero(a))
    display_func(af.isinf(a / b))
    display_func(af.isnan(a / a))

    a = af.randu(5, 1)
    b = af.randu(1, 5)
    c = af.broadcast(lambda x, y: x + y, a, b)
    display_func(a)
    display_func(b)
    display_func(c)

    @af.broadcast
    def test_add(aa, bb):
        return aa + bb

    display_func(test_add(a, b))
Ejemplo n.º 9
0
af.display(af.log(a))
af.display(af.log1p(a))
af.display(af.log10(a))
af.display(af.log2(a))
af.display(af.sqrt(a))
af.display(af.cbrt(a))

a = af.round(5 * af.randu(3, 3) - 1)
b = af.round(5 * af.randu(3, 3) - 1)

af.display(af.factorial(a))
af.display(af.tgamma(a))
af.display(af.lgamma(a))
af.display(af.iszero(a))
af.display(af.isinf(a / b))
af.display(af.isnan(a / a))

a = af.randu(5, 1)
b = af.randu(1, 5)
c = af.broadcast(lambda x, y: x + y, a, b)
af.display(a)
af.display(b)
af.display(c)


@af.broadcast
def test_add(aa, bb):
    return aa + bb


af.display(test_add(a, b))
Ejemplo n.º 10
0
def isnan(x):
    if not isinstance(x, afnumpy.ndarray):
        return numpy.isnan(x)
    s = arrayfire.isnan(x.d_array)
    return afnumpy.ndarray(x.shape, dtype=pu.typemap(s.dtype()), af_array=s)
Ejemplo n.º 11
0
 def hasnan(arr):
     return af.any_true(af.isnan(arr))
Ejemplo n.º 12
0
af.display(af.erfc(a))
af.display(af.log(a))
af.display(af.log1p(a))
af.display(af.log10(a))
af.display(af.log2(a))
af.display(af.sqrt(a))
af.display(af.cbrt(a))

a = af.round(5 * af.randu(3,3) - 1)
b = af.round(5 * af.randu(3,3) - 1)

af.display(af.factorial(a))
af.display(af.tgamma(a))
af.display(af.lgamma(a))
af.display(af.iszero(a))
af.display(af.isinf(a/b))
af.display(af.isnan(a/a))

a = af.randu(5, 1)
b = af.randu(1, 5)
c = af.broadcast(lambda x,y: x+y, a, b)
af.display(a)
af.display(b)
af.display(c)

@af.broadcast
def test_add(aa, bb):
    return aa + bb

af.display(test_add(a, b))