Python numpy.asfarray() Examples

The following are 30 code examples of numpy.asfarray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: complex_watson.py    From pb_bss with MIT License 6 votes vote down vote up
def log_norm_low_concentration(scale, dimension):
        """ Calculates logarithm of pdf function.
        Good at very low concentrations but starts to drop of at 20.
        """
        scale = np.asfarray(scale)
        shape = scale.shape
        scale = scale.ravel()

        # Mardia1999Watson Equation 4, Taylor series
        b_range = range(dimension, dimension + 20 - 1 + 1)
        b_range = np.asarray(b_range)[None, :]

        return (
            np.log(2)
            + dimension * np.log(np.pi)
            - np.log(math.factorial(dimension - 1))
            + np.log(1 + np.sum(np.cumprod(scale[:, None] / b_range, -1), -1))
        ).reshape(shape) 
Example #2
Source File: basic.py    From lambda-packs with MIT License 6 votes vote down vote up
def _asfarray(x):
    """Like numpy asfarray, except that it does not modify x dtype if x is
    already an array with a float dtype, and do not cast complex types to
    real."""
    if hasattr(x, "dtype") and x.dtype.char in numpy.typecodes["AllFloat"]:
        # 'dtype' attribute does not ensure that the
        # object is an ndarray (e.g. Series class
        # from the pandas library)
        if x.dtype == numpy.half:
            # no half-precision routines, so convert to single precision
            return numpy.asarray(x, dtype=numpy.float32)
        return numpy.asarray(x, dtype=x.dtype)
    else:
        # We cannot use asfarray directly because it converts sequences of
        # complex to sequence of real
        ret = numpy.asarray(x)
        if ret.dtype == numpy.half:
            return numpy.asarray(ret, dtype=numpy.float32)
        elif ret.dtype.char not in numpy.typecodes["AllFloat"]:
            return numpy.asfarray(x)
        return ret 
Example #3
Source File: make_clean_mask.py    From prefactor with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, x, y):
        if len(x) != len(y):
            raise IndexError('x and y must be equally sized.')
        self.x = np.asfarray(x)
        self.y = np.asfarray(y)

        # Closes the polygon if were open
        x1, y1 = x[0], y[0]
        xn, yn = x[-1], y[-1]
        if x1 != xn or y1 != yn:
            self.x = np.concatenate((self.x, [x1]))
            self.y = np.concatenate((self.y, [y1]))

        # Anti-clockwise coordinates
        if _det(self.x, self.y) < 0:
            self.x = self.x[::-1]
            self.y = self.y[::-1] 
Example #4
Source File: basic.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _asfarray(x):
    """Like numpy asfarray, except that it does not modify x dtype if x is
    already an array with a float dtype, and do not cast complex types to
    real."""
    if hasattr(x, "dtype") and x.dtype.char in numpy.typecodes["AllFloat"]:
        # 'dtype' attribute does not ensure that the
        # object is an ndarray (e.g. Series class
        # from the pandas library)
        if x.dtype == numpy.half:
            # no half-precision routines, so convert to single precision
            return numpy.asarray(x, dtype=numpy.float32)
        return numpy.asarray(x, dtype=x.dtype)
    else:
        # We cannot use asfarray directly because it converts sequences of
        # complex to sequence of real
        ret = numpy.asarray(x)
        if ret.dtype == numpy.half:
            return numpy.asarray(ret, dtype=numpy.float32)
        elif ret.dtype.char not in numpy.typecodes["AllFloat"]:
            return numpy.asfarray(x)
        return ret 
Example #5
Source File: array_utils.py    From scikit-ued with MIT License 6 votes vote down vote up
def complex_array(real, imag):
    """
    Combine two real ndarrays into a complex array.

    Parameters
    ----------
    real, imag : array_like
        Real and imaginary parts of a complex array.
    
    Returns
    -------
    complex : `~numpy.ndarray`
        Complex array.
    """
    real, imag = np.asfarray(real), np.asfarray(imag)
    comp = real.astype(np.complex)
    comp += 1j * imag
    return comp 
Example #6
Source File: targetpixelfile.py    From lightkurve with MIT License 6 votes vote down vote up
def _estimate_centroids_via_quadratic(self, aperture_mask):
        """Estimate centroids by fitting a 2D quadratic to the brightest pixels;
        this is a helper method for `estimate_centroids()`."""
        aperture_mask = self._parse_aperture_mask(aperture_mask)
        col_centr, row_centr = [], []
        for idx in range(len(self.time)):
            col, row = centroid_quadratic(self.flux[idx], mask=aperture_mask)
            col_centr.append(col)
            row_centr.append(row)
        # Finally, we add .5 to the result bellow because the convention is that
        # pixels are centered at .5, 1.5, 2.5, ...
        col_centr = np.asfarray(col_centr) + self.column + .5
        row_centr = np.asfarray(row_centr) + self.row + .5
        col_centr = Quantity(col_centr, unit='pixel')
        row_centr = Quantity(row_centr, unit='pixel')
        return col_centr, row_centr 
Example #7
Source File: metrics.py    From neural_graph_collaborative_filtering with MIT License 6 votes vote down vote up
def dcg_at_k(r, k, method=1):
    """Score is discounted cumulative gain (dcg)
    Relevance is positive real values.  Can use binary
    as the previous methods.
    Returns:
        Discounted cumulative gain
    """
    r = np.asfarray(r)[:k]
    if r.size:
        if method == 0:
            return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
        elif method == 1:
            return np.sum(r / np.log2(np.arange(2, r.size + 2)))
        else:
            raise ValueError('method must be 0 or 1.')
    return 0. 
Example #8
Source File: metrics.py    From knowledge_graph_attention_network with MIT License 6 votes vote down vote up
def dcg_at_k(r, k, method=1):
    """Score is discounted cumulative gain (dcg)
    Relevance is positive real values.  Can use binary
    as the previous methods.
    Returns:
        Discounted cumulative gain
    """
    r = np.asfarray(r)[:k]
    if r.size:
        if method == 0:
            return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
        elif method == 1:
            return np.sum(r / np.log2(np.arange(2, r.size + 2)))
        else:
            raise ValueError('method must be 0 or 1.')
    return 0. 
Example #9
Source File: bg_cache.py    From FRETBursts with GNU General Public License v2.0 6 votes vote down vote up
def _load_bg_data(d, bg_calc_kwargs, h5file):
    """Load background data from a HDF5 file."""
    group_name = bg_to_signature(d, **bg_calc_kwargs)
    if group_name not in h5file.root.background:
        msg = 'Group "%s" not found in the HDF5 file.' % group_name
        raise ValueError(msg)
    bg_auto_th_us0 = None
    bg_group = h5file.get_node('/background/', group_name)

    pprint('\n - Loading bakground data: ')
    bg = {}
    for node in bg_group._f_iter_nodes():
        if node._v_name.startswith('BG_'):
            ph_sel = Ph_sel.from_str(node._v_name[len('BG_'):])
            bg[ph_sel] = [np.asfarray(b) for b in node.read()]

    Lim = bg_group.Lim.read()
    Ph_p = bg_group.Ph_p.read()
    if 'bg_auto_th_us0' in bg_group:
        bg_auto_th_us0 = bg_group.bg_auto_th_us0.read()
    return bg, Lim, Ph_p, bg_auto_th_us0 
Example #10
Source File: io_utils.py    From m3gm with GNU General Public License v3.0 6 votes vote down vote up
def load_embeddings(filename, a2i, emb_size=DEFAULT_EMBEDDING_DIM):
    """
    loads embeddings for synsets ("atoms") from existing file,
    or initializes them to uniform random
    """
    atom_to_embed = {}
    if filename is not None:
        if filename.endswith('npy'):
            return np.load(filename)
        with codecs.open(filename, "r", "utf-8") as f:
            for line in f:
                split = line.split()
                if len(split) > 2:
                    atom = split[0]
                    vec = split[1:]
                    atom_to_embed[atom] = np.asfarray(vec)
        embedding_dim = len(atom_to_embed[list(atom_to_embed.keys())[0]])
    else:
        embedding_dim = emb_size
    out = np.random.uniform(-0.8, 0.8, (len(a2i), embedding_dim))
    if filename is not None:
        for atom, embed in list(atom_to_embed.items()):
            if atom in a2i:
                out[a2i[atom]] = np.array(embed)
    return out 
Example #11
Source File: routines.py    From emva1288 with GNU General Public License v3.0 6 votes vote down vote up
def LinearB(Xi, Yi):
    X = np.asfarray(Xi)
    Y = np.asfarray(Yi)

    # we want a function y = m * x + b
    def fp(v, x):
        return x * v[0] + v[1]

    # the error of the function e = x - y
    def e(v, x, y):
        return (fp(v, x) - y)

    # the initial value of m, we choose 1, because we thought YODA would
    # have chosen 1
    v0 = np.array([1.0, 1.0])

    vr, _success = leastsq(e, v0, args=(X, Y))

    # compute the R**2 (sqrt of the mean of the squares of the errors)
    err = np.sqrt(sum(np.square(e(vr, X, Y))) / (len(X) * len(X)))

#    print vr, success, err
    return vr, err 
Example #12
Source File: constructor.py    From CNN_Own_Dataset with MIT License 6 votes vote down vote up
def next_batch(self, batch_size=10):

        datas = np.empty((0, self._height, self._width, self._dimension), int)
        labels = np.empty((0, self._class_len), int)


        for idx in range(batch_size):
            random.randint(0, len(self._datas)-1)
            tmp_img = scipy.misc.imread(self._datas[idx])
            tmp_img = scipy.misc.imresize(tmp_img, (self._height, self._width))
            tmp_img = tmp_img.reshape(1, self._height, self._width, self._dimension)

            datas = np.append(datas, tmp_img, axis=0)
            labels = np.append(labels, np.eye(self._class_len)[int(np.asfarray(self._labels[idx]))].reshape(1, self._class_len), axis=0)


        return datas, labels 
Example #13
Source File: basic.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def _asfarray(x):
    """Like numpy asfarray, except that it does not modify x dtype if x is
    already an array with a float dtype, and do not cast complex types to
    real."""
    if hasattr(x, "dtype") and x.dtype.char in numpy.typecodes["AllFloat"]:
        # 'dtype' attribute does not ensure that the
        # object is an ndarray (e.g. Series class
        # from the pandas library)
        if x.dtype == numpy.half:
            # no half-precision routines, so convert to single precision
            return numpy.asarray(x, dtype=numpy.float32)
        return numpy.asarray(x, dtype=x.dtype)
    else:
        # We cannot use asfarray directly because it converts sequences of
        # complex to sequence of real
        ret = numpy.asarray(x)
        if ret.dtype == numpy.half:
            return numpy.asarray(ret, dtype=numpy.float32)
        elif ret.dtype.char not in numpy.typecodes["AllFloat"]:
            return numpy.asfarray(x)
        return ret 
Example #14
Source File: eval.py    From deepBoosting with MIT License 5 votes vote down vote up
def compute_psnr(im1, im2):
	if im1.shape != im2.shape:
		raise Exception('the shapes of two images are not equal')
	rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
	psnr = 20 * np.log10(255.0 / rmse)
	return psnr 
Example #15
Source File: eval.py    From deepBoosting with MIT License 5 votes vote down vote up
def compute_psnr(im1, im2):
	if im1.shape != im2.shape:
		raise Exception('the shapes of two images are not equal')
	rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
	psnr = 20 * np.log10(255.0 / rmse)
	return psnr 
Example #16
Source File: eval.py    From deepBoosting with MIT License 5 votes vote down vote up
def compute_psnr(im1, im2):
	if im1.shape != im2.shape:
		raise Exception('the shapes of two images are not equal')
	rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
	psnr = 20 * np.log10(255.0 / rmse)
	return psnr 
Example #17
Source File: eval.py    From deepBoosting with MIT License 5 votes vote down vote up
def compute_psnr(im1, im2):
	if im1.shape != im2.shape:
		raise Exception('the shapes of two images are not equal')
	rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
	psnr = 20 * np.log10(255.0 / rmse)
	return psnr 
Example #18
Source File: metrics.py    From Azimuth with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dcg_at_k(r, k, method=0):
    """Score is discounted cumulative gain (dcg)

    Relevance is positive real values.  Can use binary
    as the previous methods.

    Example from
    http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
    >>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0]
    >>> dcg_at_k(r, 1)
    3.0
    >>> dcg_at_k(r, 1, method=1)
    3.0
    >>> dcg_at_k(r, 2)
    5.0
    >>> dcg_at_k(r, 2, method=1)
    4.2618595071429155
    >>> dcg_at_k(r, 10)
    9.6051177391888114
    >>> dcg_at_k(r, 11)
    9.6051177391888114

    Args:
        r: Relevance scores (list or numpy) in rank order
            (first element is the first item)
        k: Number of results to consider
        method: If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, ...]
                If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, ...]

    Returns:
        Discounted cumulative gain
    """
    r = np.asfarray(r)[:k]
    if r.size:
        if method == 0:
            return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
        elif method == 1:
            return np.sum(r / np.log2(np.arange(2, r.size + 2)))
        else:
            raise ValueError('method must be 0 or 1.')
    return 0. 
Example #19
Source File: endurance.py    From fatpack with ISC License 5 votes vote down vote up
def ensure_array(method):
    @wraps(method)
    def wrapped_method(self, x):
        x_is_float_or_int = isinstance(x, float) or isinstance(x, int)
        if x_is_float_or_int:
            xm = np.array([x])
        else:
            xm = np.asfarray(x)
        ym = method(self, xm)
        if x_is_float_or_int:
            ym = ym[0]
        return ym
    return wrapped_method 
Example #20
Source File: test_regression.py    From pySINDy with MIT License 5 votes vote down vote up
def test_asfarray_none(self):
        # Test for changeset r5065
        assert_array_equal(np.array([np.nan]), np.asfarray([None])) 
Example #21
Source File: jacobian.py    From pysaliency with MIT License 5 votes vote down vote up
def jac(self, x, *args, **kwargs):
        self.log('G[')
        x0 = np.asfarray(x)
        #print x0
        dxs = np.zeros((len(x0), 2*len(x0)))
        for i in range(len(x0)):
            dxs[i, i] = -self.epsilon
            dxs[i, len(x0)+i] = self.epsilon
        results = [self(*(x0 + dxs[:, i], ) + args, **kwargs) for i in range(2*len(x0))]
        jac = np.zeros([len(x0), len(np.atleast_1d(results[0]))])
        for i in range(len(x0)):
            jac[i] = (results[len(x0)+i] - results[i]) / (2*self.epsilon)
        self.log(']')
        return jac.transpose() 
Example #22
Source File: jacobian.py    From pysaliency with MIT License 5 votes vote down vote up
def jac(self, x, *args, **kwargs):
        self.log('G[')
        x0 = np.asfarray(x)
        #print x0
        dxs = np.zeros((len(x0), len(x0) + 1))
        for i in range(len(x0)):
            dxs[i, i + 1] = self.epsilon
        results = [self(*(x0 + dxs[:, i], ) + args, **kwargs) for i in range(len(x0) + 1)]
        jac = np.zeros([len(x0), len(np.atleast_1d(results[0]))])
        for i in range(len(x0)):
            jac[i] = (results[i + 1] - results[0]) / self.epsilon
        self.log(']')
        return jac.transpose() 
Example #23
Source File: test_regression.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_asfarray_none(self):
        # Test for changeset r5065
        assert_array_equal(np.array([np.nan]), np.asfarray([None])) 
Example #24
Source File: dummy.py    From postpic with GNU General Public License v3.0 5 votes vote down vote up
def simextent(self, axis):
        g = self.grid(None, axis)
        return np.asfarray([g[0], g[-1]]) 
Example #25
Source File: gradient_checker.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _compute_gradient(x,
                      x_shape,
                      dx,
                      y,
                      y_shape,
                      dy,
                      x_init_value=None,
                      delta=1e-3):
  """Computes the theoretical and numerical jacobian."""
  t = dtypes.as_dtype(x.dtype)
  allowed_types = [dtypes.float16, dtypes.float32, dtypes.float64,
                   dtypes.complex64, dtypes.complex128]
  assert t.base_dtype in allowed_types, "Don't support type %s for x" % t.name
  t2 = dtypes.as_dtype(y.dtype)
  assert t2.base_dtype in allowed_types, "Don't support type %s for y" % t2.name

  if x_init_value is not None:
    i_shape = list(x_init_value.shape)
    assert(list(x_shape) == i_shape), "x_shape = %s, init_data shape = %s" % (
        x_shape, i_shape)
    x_data = x_init_value
  else:
    if t == dtypes.float16:
      dtype = np.float16
    elif t == dtypes.float32:
      dtype = np.float32
    else:
      dtype = np.float64
    x_data = np.asfarray(np.random.random_sample(x_shape), dtype=dtype)

  jacob_t = _compute_theoretical_jacobian(x, x_shape, x_data, dy, y_shape, dx)
  jacob_n = _compute_numeric_jacobian(x, x_shape, x_data, y, y_shape, delta)
  return jacob_t, jacob_n 
Example #26
Source File: metrics.py    From chameleon_recsys with MIT License 5 votes vote down vote up
def _ndcg_at_k(r, k):
        #Based on https://gist.github.com/bwhite/3726239, but with alternative formulation of DCG
        #which places stronger emphasis on retrieving relevant documents (used in Kaggle)
        def dcg_at_k(r, k):
            r = np.asfarray(r)[:k]
            if r.size:
                return np.sum((np.power(2,r)-1) / np.log2(np.arange(2, r.size + 2)))
            return 0.
    
        dcg_max = dcg_at_k(sorted(r, reverse=True), k)
        if not dcg_max:
            return 0.
        return dcg_at_k(r, k) / dcg_max 
Example #27
Source File: complex_watson.py    From pb_bss with MIT License 5 votes vote down vote up
def log_norm_high_concentration(scale, dimension):
        """ Calculates logarithm of pdf function.
        High concentration above 10 and dimension below 8.
        """
        scale = np.asfarray(scale)
        shape = scale.shape
        scale = scale.ravel()

        return (
            np.log(2.)
            + dimension * np.log(np.pi)
            + (1. - dimension) * np.log(scale)
            + scale
        ).reshape(shape) 
Example #28
Source File: complex_watson.py    From pb_bss with MIT License 5 votes vote down vote up
def log_norm_medium_concentration(scale, dimension):
        """ Calculates logarithm of pdf function.
        Almost complete range of interest and dimension below 8.
        """
        scale = np.asfarray(scale)
        shape = scale.shape
        scale = scale.flatten()

        # Function is unstable at zero.
        # Scale needs to be float for this to work.
        scale[scale < 1e-2] = 1e-2

        r_range = range(dimension - 2 + 1)
        r = np.asarray(r_range)[None, :]

        # Mardia1999Watson Equation 3
        temp = (
            scale[:, None] ** r
            * np.exp(-scale[:, None])
            / np.asarray([math.factorial(_r) for _r in r_range])
        )

        return (
            np.log(2.)
            + dimension * np.log(np.pi)
            + (1. - dimension) * np.log(scale)
            + scale
            + np.log(1. - np.sum(temp, -1))
        ).reshape(shape) 
Example #29
Source File: eval.py    From nextitnet with MIT License 5 votes vote down vote up
def dcg_at_k(r, k):
    r = np.asfarray(r)[:k]
    return np.sum(r / np.log2(np.arange(2, r.size + 2))) 
Example #30
Source File: metrics.py    From knowledge_graph_attention_network with MIT License 5 votes vote down vote up
def recall_at_k(r, k, all_pos_num):
    r = np.asfarray(r)[:k]
    return np.sum(r) / all_pos_num