def nanmin(a, axis=None):
    """Find the minimium over the given axis, ignoring NaNs.
    """
    y = array(a,subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = _nx.inf
    return y.min(axis)
def nanargmax(a, axis=None):
    """Find the maximum over the given axis ignoring NaNs.
    """
    y = array(a,subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = -_nx.inf
    return y.argmax(axis)
Example #3
0
def nansum(a, axis=None):
    """Sum the array over the given axis, treating NaNs as 0.
    """
    y = array(a, subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = 0
    return y.sum(axis)
def nansum(a, axis=None):
    """Sum the array over the given axis, treating NaNs as 0.
    """
    y = array(a,subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = 0
    return y.sum(axis)
Example #5
0
def nanmin(a, axis=None):
    """Find the minimium over the given axis, ignoring NaNs.
    """
    y = array(a, subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = _nx.inf
    return y.min(axis)
Example #6
0
def nanargmax(a, axis=None):
    """Find the maximum over the given axis ignoring NaNs.
    """
    y = array(a, subok=True)
    if not issubclass(y.dtype.type, _nx.integer):
        y[isnan(a)] = -_nx.inf
    return y.argmax(axis)
def label_recall(confusion, label):
    try:
        recall = float(confusion[label, label]) / confusion[:, label].sum()
    except:
        recall = 0.0

    return recall if not isnan(recall) else 0.0
def label_precision(confusion, label):
    try:
        precision = float(confusion[label, label]) / confusion[label, :].sum()
    except:
        precision = 0.0

    return precision if not isnan(precision) else 0.0
Example #9
0
 def print_nonfinite(x):
     with errstate(invalid='ignore'):
         if umath.isnan(x):
             ret = ('+' if sign == '+' else '') + nanstr
         else:  # isinf
             infsgn = '-' if x < 0 else '+' if sign == '+' else ''
             ret = infsgn + infstr
         return ' ' * (pad_left + pad_right + 1 - len(ret)) + ret
def f1_macro(confusion, none_index, exclude_none=True):
    p = precision_macro(confusion, none_index, exclude_none)
    r = recall_macro(confusion, none_index, exclude_none)
    try:
        f1 = 2.0 * p * r / (p + r)
    except:
        f1 = 0.0

    return f1 if not isnan(f1) else 0.0
Example #11
0
def _clip_dep_is_scalar_nan(a):
    # guarded to protect circular imports
    from numpy.core.fromnumeric import ndim
    if ndim(a) != 0:
        return False
    try:
        return um.isnan(a)
    except TypeError:
        return False
def label_f1(confusion, label):
    seterr(all='ignore')
    p = label_precision(confusion, label)
    r = label_recall(confusion, label)
    try:
        f1 = 2.0 * p * r / (p + r)
    except:
        f1 = 0.0

    return f1 if not isnan(f1) else 0.0
def precision_micro(confusion, none_index, exclude_none=True):
    n = confusion.shape[0]
    num = sum([confusion[i, i] for i in range(n) if i != none_index or not exclude_none])
    den = sum([confusion[i, :].sum() for i in range(n) if i != none_index or not exclude_none])
    try:
        precision = float(num) / den
    except:
        precision = 0.0

    return precision if not isnan(precision) else 0.0
def recall_micro(confusion, none_index, exclude_none=True):
    n = confusion.shape[0]
    num = sum([confusion[i, i] for i in range(n) if i != none_index or not exclude_none])
    den = sum([confusion[:, i].sum() for i in range(n) if i != none_index or not exclude_none])

    try:
        recall = float(num) / den
    except:
        recall = 0.0

    return recall if not isnan(recall) else 0.0
Example #15
0
    def zsl_insert(self, score, ele):
        """
        Insert a new node in the skiplist. Assumes the element does not already
        exist (up to the caller to enforce that). The skiplist takes ownership
        of the passed string 'ele'.
        :param score:
        :param ele:
        :return:
        """
        update = [None] * ZSKIPLIST_MAXLEVEL
        rank = [0] * ZSKIPLIST_MAXLEVEL

        assert (not isnan(score)), 'score can not be NaN'
        x = self.header

        i = self.level - 1
        while i >= 0:
            rank[i] = 0 if i == (self.level - 1) else rank[i + 1]
            while (x.level[i].forward
                   and (x.level[i].forward.score < score or
                        (x.level[i].forward.score == score
                         and elecmp(x.level[i].forward.ele, ele) < 0))):
                rank[i] += x.level[i].span
                x = x.level[i].forward
            update[i] = x
            i -= 1

        level = zsl_random_level()
        if level > self.level:
            for i in range(self.level, level):
                rank[i] = 0
                update[i] = self.header
                update[i].level[i].span = self.length
            self.level = level
        x = ZskiplistNode(level=level, score=score, ele=ele)
        for i in range(0, level):
            x.level[i].forward = update[i].level[i].forward
            update[i].level[i].forward = x
            x.level[i].span = update[i].level[i].span - (rank[0] - rank[i])
            update[i].level[i].span = (rank[0] - rank[i]) + 1

        for i in range(level, self.level):
            update[i].level[i].span += 1

        x.backward = None if (update[0] is self.header) else update[0]

        if x.level[0].forward:
            x.level[0].forward.backward = x
        else:
            self.tail = x
        self.length += 1
        return x
Example #16
0
def ecef_to_lat_lon_alt1(R, deg=True):
    """
    Fukushima implementation of the Bowring algorithm (2006),
    see [4] --
    :param R: (X,Y,Z) -- coordinates in ECEF (numpy array)
    :param deg: if True then lat and lon are returned in degrees (rad otherwise)
    :return:  (φ,θ,h) -- lat [deg], lon [deg], alt in WGS84 (numpy array)
    """
    # WGS 84 constants
    a = 6378137.0  # Equatorial Radius [m]
    b = 6356752.314245  # Polar Radius [m]
    e = 0.08181919092890624  # e = sqrt(1-b²/a²)
    E = e ** 2
    e1 = sqrt(1 - e ** 2)  # e' = sqrt(1 - e²)
    if isinstance(R, list): R = np.array(R)
    p = sqrt(R[0] ** 2 + R[1] ** 2)  # (1) - sqrt(X² + Y²)
    az = abs(R[2])
    Z = e1 * az / a
    P = p / a
    S, C = Z or 1, e1 * P or 1  # (C8) - zero approximation
    max_iter = 5
    for i in range(max_iter):
        A = sqrt(S ** 2 + C ** 2)
        Cn = P * A ** 3 - E * C ** 3
        Sn = Z * A ** 3 + E * S ** 3
        delta = abs(Sn / Cn - S / C) * C / S
        if isnan(delta):
            return ecef_to_lat_lon_alt1(R)
        if abs(delta) < 1e-10 or i == max_iter - 1:
            break
        S, C = Sn, Cn
    theta = np.math.atan2(R[1], R[0])
    Cc = e1 * Cn
    phi = np.sign(R[2]) * np.math.atan2(Sn, Cc)
    h = (p * Cc + az * Sn - b * sqrt(Sn ** 2 + Cn ** 2)) / sqrt(Cc ** 2 + Sn ** 2)
    if deg:
        out = np.array([np.degrees(phi), np.degrees(theta), h])
    else:
        out = np.array([phi, theta, h])
    # if filter(isnan, out):
    #     return ecef_to_lat_lon_alt1(R)
    return out
Example #17
0
def ecef_to_lat_lon_alt1(R, deg=True):
    """
    Fukushima implementation of the Bowring algorithm (2006),
    see [4] --
    :param R: (X,Y,Z) -- coordinates in ECEF (numpy array)
    :param deg: if True then lat and lon are returned in degrees (rad otherwise)
    :return:  (φ,θ,h) -- lat [deg], lon [deg], alt in WGS84 (numpy array)
    """
    # WGS 84 constants
    a = 6378137.0  # Equatorial Radius [m]
    b = 6356752.314245  # Polar Radius [m]
    e = 0.08181919092890624  # e = sqrt(1-b²/a²)
    E = e**2
    e1 = sqrt(1 - e**2)  # e' = sqrt(1 - e²)
    if isinstance(R, list): R = np.array(R)
    p = sqrt(R[0]**2 + R[1]**2)  # (1) - sqrt(X² + Y²)
    az = abs(R[2])
    Z = e1 * az / a
    P = p / a
    S, C = Z or 1, e1 * P or 1  # (C8) - zero approximation
    max_iter = 5
    for i in range(max_iter):
        A = sqrt(S**2 + C**2)
        Cn = P * A**3 - E * C**3
        Sn = Z * A**3 + E * S**3
        delta = abs(Sn / Cn - S / C) * C / S
        if isnan(delta):
            return ecef_to_lat_lon_alt1(R)
        if abs(delta) < 1e-10 or i == max_iter - 1:
            break
        S, C = Sn, Cn
    theta = np.math.atan2(R[1], R[0])
    Cc = e1 * Cn
    phi = np.sign(R[2]) * np.math.atan2(Sn, Cc)
    h = (p * Cc + az * Sn - b * sqrt(Sn**2 + Cn**2)) / sqrt(Cc**2 + Sn**2)
    if deg:
        out = np.array([np.degrees(phi), np.degrees(theta), h])
    else:
        out = np.array([phi, theta, h])
    # if filter(isnan, out):
    #     return ecef_to_lat_lon_alt1(R)
    return out
Example #18
0
def isnan(x):
    if isinstance(x, IDataDescriptor):
        return _um.isnan(dd_as_py(x))
    else:
        return _um.isnan(x)
Example #19
0
def isnan(x):
    # hacks to remove when isnan/isinf are available for data descriptors
    if isinstance(x, IDataDescriptor):
        return _um.isnan(dd_as_py(x))
    else:
        return _um.isnan(x)
Example #20
0
    def get_format_func(self, elem, **options):
        missing_opt = self.check_options(**options)
        if missing_opt:
            raise Exception("Missing options: {}".format(missing_opt))

        floatmode = options['floatmode']
        precision = None if floatmode == 'unique' else options['precision']
        suppress_small = options['suppress_small']
        sign = options['sign']
        infstr = options['infstr']
        nanstr = options['nanstr']
        exp_format = False
        pad_left, pad_right = 0, 0

        # only the finite values are used to compute the number of digits
        finite = umath.isfinite(elem)
        finite_vals = elem[finite]
        nonfinite_vals = elem[~finite]

        # choose exponential mode based on the non-zero finite values:
        abs_non_zero = umath.absolute(finite_vals[finite_vals != 0])
        if len(abs_non_zero) != 0:
            max_val = np.max(abs_non_zero)
            min_val = np.min(abs_non_zero)
            with np.errstate(over='ignore'):  # division can overflow
                if max_val >= 1.e8 or (not suppress_small and
                                       (min_val < 0.0001
                                        or max_val / min_val > 1000.)):
                    exp_format = True

        # do a first pass of printing all the numbers, to determine sizes
        if len(finite_vals) == 0:
            trim, exp_size, unique = '.', -1, True
        elif exp_format:
            trim, unique = '.', True
            if floatmode == 'fixed':
                trim, unique = 'k', False
            strs = (format_float_scientific(x,
                                            precision=precision,
                                            unique=unique,
                                            trim=trim,
                                            sign=sign == '+')
                    for x in finite_vals)
            frac_strs, _, exp_strs = zip(*(s.partition('e') for s in strs))
            int_part, frac_part = zip(*(s.split('.') for s in frac_strs))
            exp_size = max(len(s) for s in exp_strs) - 1

            trim = 'k'
            precision = max(len(s) for s in frac_part)

            # this should be only 1 or 2. Can be calculated from sign.
            pad_left = max(len(s) for s in int_part)
            # pad_right is only needed for nan length calculation
            pad_right = exp_size + 2 + precision

            unique = False
        else:
            trim, unique = '.', True
            if floatmode == 'fixed':
                trim, unique = 'k', False
            strs = (format_float_positional(x,
                                            precision=precision,
                                            fractional=True,
                                            unique=unique,
                                            trim=trim,
                                            sign=sign == '+')
                    for x in finite_vals)
            int_part, frac_part = zip(*(s.split('.') for s in strs))
            pad_left = max(len(s) for s in int_part)
            pad_right = max(len(s) for s in frac_part)
            exp_size = -1

            if floatmode in ['fixed', 'maxprec_equal']:
                precision = pad_right
                unique = False
                trim = 'k'
            else:
                unique = True
                trim = '.'

        # account for sign = ' ' by adding one to pad_left
        if sign == ' ' and not any(np.signbit(finite_vals)):
            pad_left += 1

        # account for nan and inf in pad_left
        if len(nonfinite_vals) != 0:
            nanlen, inflen = 0, 0
            if np.any(umath.isinf(nonfinite_vals)):
                neginf = sign != '-' or np.any(np.isneginf(nonfinite_vals))
                inflen = len(infstr) + neginf
            if np.any(umath.isnan(elem)):
                nanlen = len(nanstr)
            offset = pad_right + 1  # +1 for decimal pt
            pad_left = max(nanlen - offset, inflen - offset, pad_left)

        def print_nonfinite(x):
            with errstate(invalid='ignore'):
                if umath.isnan(x):
                    ret = ('+' if sign == '+' else '') + nanstr
                else:  # isinf
                    infsgn = '-' if x < 0 else '+' if sign == '+' else ''
                    ret = infsgn + infstr
                return ' ' * (pad_left + pad_right + 1 - len(ret)) + ret

        if exp_format:

            def print_finite(x):
                return format_float_scientific(x,
                                               precision=precision,
                                               unique=unique,
                                               trim=trim,
                                               sign=sign == '+',
                                               pad_left=pad_left,
                                               exp_digits=exp_size)
        else:

            def print_finite(x):
                return format_float_positional(x,
                                               precision=precision,
                                               unique=unique,
                                               fractional=True,
                                               trim=trim,
                                               sign=sign == '+',
                                               pad_left=pad_left,
                                               pad_right=pad_right)

        def fmt(x):
            if umath.isfinite(x):
                return print_finite(x)
            else:
                return print_nonfinite(x)

        return fmt
Example #21
0
    def zset_add(self, score, ele, flags):
        """
        Add a new element or update the score of an existing element in a sorted
        set.
 
        The set of flags change the command behavior. They are passed with an integer
        and will return other flags to indicate different conditions.

        The input flags are the following:

        ZADD_INCR: Increment the current element score by 'score' instead of updating
                 the current element score. If the element does not exist, we
                 assume 0 as previous score.
        ZADD_NX:   Perform the operation only if the element does not exist.

        When ZADD_INCR is used, the new score of the element is returned with result.

        The returned flags are the following:

        ZADD_NAN:     The resulting score is not a number.
        ZADD_ADDED:   The element was added (not present before the call).
        ZADD_UPDATED: The element score was updated.
        ZADD_NOP:     No operation was performed because of NX.

        Return value:

        The function returns 1 on success, and sets the appropriate flags
        ADDED or UPDATED to signal what happened during the operation (note that
        none could be set if we re-added an element using the same score it used
        to have, or in the case a zero increment is used).

        The function returns 0 on erorr, currently only when the increment
        produces a NAN condition, or when the 'score' value is NAN since the
        start.
        :param score:
        :param ele:
        :param flags:
        :return:
        """
        incr = (flags & ZADD_INCR) != 0
        nx = (flags & ZADD_NX) != 0

        if isnan(score):
            return 0, ZADD_NAN, 0
        dict_ = self.dict_
        de = dict_.get(ele, None)
        if de is not None:
            if nx:
                return 1, ZADD_NOP, 0
            curscore = de
            if incr:
                score += curscore
                if isnan(score):
                    return 0, ZADD_NAN, 0
            # Remove and re-insert when score changes.
            if score != curscore:
                result = self.zsl.zsl_delete(score=curscore, ele=ele)
                assert result == 1
                result = self.zsl.zsl_insert(score=score, ele=ele)
                assert result == 1
                dict_[ele] = score

            return 1, ZADD_UPDATED, score
        else:
            result = self.zsl.zsl_insert(score=score, ele=ele)
            assert result == 1
            assert dict_.setdefault(ele, score) == score
            return 1, ZADD_ADDED, score
Example #22
0
def isnan(x):
    # hacks to remove when isnan/isinf are available for data descriptors
    if isinstance(x, IDataDescriptor):
        return _um.isnan(dd_as_py(x))
    else:
        return _um.isnan(x)
Example #23
0
def tm_ransac5rows(d, sys):
    class solstruct():
        pass

    sol = solstruct()

    maxnrinl = 0

    for iii in range(0, sys.ransac_k):

        d2 = d ** 2
        inl = (isfinite(d2)).astype(int)
        r_c = d2.shape
        m = r_c[0]
        tmprows = random.permutation(m)
        tmprows = tmprows[0:5]
        auxvar1 = inl[tmprows, :]
        auxvar2 = ((np.all(auxvar1, axis=0)).astype(int)).T
        okcol = (np.flatnonzero(auxvar2)).T

        B = d2[np.ix_(tmprows, okcol)]

        ntmp = B.shape[1]
        tmp2 = random.permutation(ntmp)

        if ntmp > 5:
            tmp21tup = tmp2[0:4]
            tmp21 = np.reshape(tmp21tup, (1, -1))
            tmp22tup = tmp2[4:, ]
            tmp22 = np.reshape(tmp22tup, (1, -1))
            cl, _ = compactionmatrix(5)

            cr, _ = compactionmatrix(tmp2.shape[0])
            Btmp1 = np.dot(cl, B[:, tmp2])
            Btmp = np.dot(Btmp1, cr.conj().T)

            B1 = Btmp[:, 0:3]
            B2 = Btmp[:, 3:]

            u, s, v = linalg.svd(B1)
            u4tup = u[:, 3]
            u4 = np.reshape(u4tup, (-1, 1))

            if 0:
                abs((u4.conj().T) * B2)

            Imiss = isnan(d)
            auxvar3 = abs(np.dot((u4.conj().T), B2))
            okindtup = (auxvar3 > sys.ransac_threshold).nonzero()
            okindmat = np.asarray(okindtup)
            okind = np.reshape(okindmat, (1, -1))
            inlim = zeros(d.shape)
            inlim = inlim - Imiss
            tmpconcat = concatenate((tmp21, tmp22[0, okind - 1]), 1)
            tmprows = np.reshape(tmprows, (-1, 1))
            inlim[tmprows, okcol[tmpconcat]] = ones((5, 4 + okind.size))
            nrinl = 4 + okind.size

            if nrinl > maxnrinl:
                maxnrinl = nrinl

                sol.rows = tmprows
                concatmat = concatenate((tmp21, tmp22[0, okind - 1]), 1)
                sol.cols = okcol[(concatmat)]
                sol.row1 = sol.rows[1]
                sol.col1 = sol.cols[0, 0]
                sol.inlmatrix = inlim
                B = d2[sol.rows, sol.cols]
                cl, dl = compactionmatrix(B.shape[0])
                cr, dr = compactionmatrix(B.shape[1])
                Bhatdotprod = np.dot(dl, B)
                Bhat = np.dot(Bhatdotprod, dr.conj().T)
                Btildedotprod = np.dot(cl, B)
                Btilde = np.dot(Btildedotprod, cr.conj().T)
                u, s, vh = linalg.svd(Btilde)
                v = vh.T
                s[3:, ] = zeros(s.shape[0] - 3, s.shape[1])
                Btilde = u * s * (v.conj().T)
                Bhat[1:, 1:] = Btilde
                sol.Bhat = Bhat
                sol.dl = dl
                sol.dr = dr

    return sol, maxnrinl