Python numpy.atleast_1d() Examples

The following are 30 code examples for showing how to use numpy.atleast_1d(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: baseband   Author: mhvk   File: utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def byte_array(pattern):
    """Convert the pattern to a byte array.

    Parameters
    ----------
    pattern : ~numpy.ndarray, bytes, int, or iterable of int
        Pattern to convert.  If a `~numpy.ndarray` or `bytes` instance,
        a byte array view is taken.  If an (iterable of) int, the integers
        need to be unsigned 32 bit and will be interpreted as little-endian.

    Returns
    -------
    byte_array : `~numpy.ndarray` of byte
        With any elements of pattern stored in little-endian order.
    """
    if isinstance(pattern, (np.ndarray, bytes)):
        # Quick turn-around for input that is OK already:
        return np.atleast_1d(pattern).view('u1')

    pattern = np.array(pattern, ndmin=1)
    if (pattern.dtype.kind not in 'uif'
            or pattern.min() < 0
            or pattern.max() >= 1 << 32):
        raise ValueError('values have to fit in 32 bit unsigned int.')
    return pattern.astype('<u4').view('u1') 
Example 2
Project: recruit   Author: Frank-qlu   File: test_linalg.py    License: Apache License 2.0 6 votes vote down vote up
def do(self, a, b, tags):
        d = linalg.det(a)
        (s, ld) = linalg.slogdet(a)
        if asarray(a).dtype.type in (single, double):
            ad = asarray(a).astype(double)
        else:
            ad = asarray(a).astype(cdouble)
        ev = linalg.eigvals(ad)
        assert_almost_equal(d, multiply.reduce(ev, axis=-1))
        assert_almost_equal(s * np.exp(ld), multiply.reduce(ev, axis=-1))

        s = np.atleast_1d(s)
        ld = np.atleast_1d(ld)
        m = (s != 0)
        assert_almost_equal(np.abs(s[m]), 1)
        assert_equal(ld[~m], -inf) 
Example 3
Project: typhon   Author: atmtools   File: em.py    License: MIT License 6 votes vote down vote up
def blackbody_radiance(self, T, spectral=True):
        """Calculate integrated radiance for blackbody at temperature T

        :param T: Temperature [K].  This can be either a python number, or
            a numpy ndarray, on a ureg quantity encompassing either.
        :param spectral: Parameter to control whether to return spectral
            radiance or radiance.  See self.integrate_radiances for
            details.

        Returns quantity ndarray with blackbody radiance in desired unit.
        Note that this is an ndarray with dimension (1,) even if you
        passin a scalar.
        """
        try:
            T = T.to("K")
        except AttributeError:
            T = ureg.Quantity(T, "K")
        T = ureg.Quantity(numpy.atleast_1d(T), T.u)
        # fails if T is multidimensional
        shp = T.shape
        return self.integrate_radiances(
            self.frequency, planck_f(
                self.frequency[numpy.newaxis, :],
                T.reshape((-1,))[:, numpy.newaxis]),
                spectral=spectral).reshape(shp) 
Example 4
Project: buzzard   Author: airware   File: parameters.py    License: Apache License 2.0 6 votes vote down vote up
def normalize_channels_parameter(channels, channel_count):
    if channels is None:
        if channel_count == 1:
            return [0], True
        else:
            return list(range(channel_count)), False

    indices = np.arange(channel_count)
    indices = indices[channels]
    indices = np.atleast_1d(indices)

    if isinstance(channels, slice):
        return indices.tolist(), False

    channels = np.asarray(channels)
    if not np.issubdtype(channels.dtype, np.number):
        raise TypeError('`channels` should be None or int or slice or list of int')
    if channels.ndim == 0:
        assert len(indices) == 1
        return indices.tolist(), True
    return indices.tolist(), False 
Example 5
Project: mars   Author: mars-project   File: partition.py    License: Apache License 2.0 6 votes vote down vote up
def _validate_partition_arguments(a, kth, axis, kind, order, kw):
    a = astensor(a)
    if axis is None:
        a = a.flatten()
        axis = 0
    else:
        axis = validate_axis(a.ndim, axis)
    if isinstance(kth, (Base, Entity)):
        kth = astensor(kth)
        _check_kth_dtype(kth.dtype)
    else:
        kth = np.atleast_1d(kth)
        kth = _validate_kth_value(kth, a.shape[axis])
    if kth.ndim > 1:
        raise ValueError('object too deep for desired array')
    if kind != 'introselect':
        raise ValueError('{} is an unrecognized kind of select'.format(kind))
    # if a is structure type and order is not None
    order = validate_order(a.dtype, order)
    need_align = kw.pop('need_align', None)
    if len(kw) > 0:
        raise TypeError('partition() got an unexpected keyword '
                        'argument \'{}\''.format(next(iter(kw))))

    return a, kth, axis, kind, order, need_align 
Example 6
Project: lambda-packs   Author: ryfeus   File: filter_design.py    License: MIT License 6 votes vote down vote up
def lp2lp(b, a, wo=1.0):
    """
    Transform a lowpass filter prototype to a different frequency.

    Return an analog low-pass filter with cutoff frequency `wo`
    from an analog low-pass filter prototype with unity cutoff frequency, in
    transfer function ('ba') representation.

    """
    a, b = map(atleast_1d, (a, b))
    try:
        wo = float(wo)
    except TypeError:
        wo = float(wo[0])
    d = len(a)
    n = len(b)
    M = max((d, n))
    pwo = pow(wo, numpy.arange(M - 1, -1, -1))
    start1 = max((n - d, 0))
    start2 = max((d - n, 0))
    b = b * pwo[start1] / pwo[start2:]
    a = a * pwo[start1] / pwo[start1:]
    return normalize(b, a) 
Example 7
Project: lambda-packs   Author: ryfeus   File: minpack.py    License: MIT License 6 votes vote down vote up
def _check_func(checker, argname, thefunc, x0, args, numinputs,
                output_shape=None):
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
    if (output_shape is not None) and (shape(res) != output_shape):
        if (output_shape[0] != 1):
            if len(output_shape) > 1:
                if output_shape[1] == 1:
                    return shape(res)
            msg = "%s: there is a mismatch between the input and output " \
                  "shape of the '%s' argument" % (checker, argname)
            func_name = getattr(thefunc, '__name__', None)
            if func_name:
                msg += " '%s'." % func_name
            else:
                msg += "."
            msg += 'Shape should be %s but it is %s.' % (output_shape, shape(res))
            raise TypeError(msg)
    if issubdtype(res.dtype, inexact):
        dt = res.dtype
    else:
        dt = dtype(float)
    return shape(res), dt 
Example 8
Project: lambda-packs   Author: ryfeus   File: stats.py    License: MIT License 6 votes vote down vote up
def _chk2_asarray(a, b, axis):
    if axis is None:
        a = np.ravel(a)
        b = np.ravel(b)
        outaxis = 0
    else:
        a = np.asarray(a)
        b = np.asarray(b)
        outaxis = axis

    if a.ndim == 0:
        a = np.atleast_1d(a)
    if b.ndim == 0:
        b = np.atleast_1d(b)

    return a, b, outaxis 
Example 9
Project: lambda-packs   Author: ryfeus   File: _continuous_distns.py    License: MIT License 6 votes vote down vote up
def _munp(self, n, beta, m):
        """
        Returns the n-th non-central moment of the crystalball function.
        """
        N = 1.0 / (m/beta / (m-1) * np.exp(-beta**2 / 2.0) + _norm_pdf_C * _norm_cdf(beta))

        def n_th_moment(n, beta, m):
            """
            Returns n-th moment. Defined only if n+1 < m
            Function cannot broadcast due to the loop over n
            """
            A = (m/beta)**m * np.exp(-beta**2 / 2.0)
            B = m/beta - beta
            rhs = 2**((n-1)/2.0) * sc.gamma((n+1)/2) * (1.0 + (-1)**n * sc.gammainc((n+1)/2, beta**2 / 2))
            lhs = np.zeros(rhs.shape)
            for k in range(n + 1):
                lhs += sc.binom(n, k) * B**(n-k) * (-1)**k / (m - k - 1) * (m/beta)**(-m + k + 1)
            return A * lhs + rhs

        return N * _lazywhere(np.atleast_1d(n + 1 < m),
                              (n, beta, m),
                              np.vectorize(n_th_moment, otypes=[np.float]),
                              np.inf) 
Example 10
Project: mlearn   Author: materialsvirtuallab   File: calcs.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _parse(self):
        element = np.atleast_1d(_read_dump('dump.element', 'unicode'))
        b = np.atleast_2d(_read_dump('dump.sna'))
        db = np.atleast_2d(_read_dump('dump.snad'))
        vb = np.atleast_2d(_read_dump('dump.snav'))
        return b, db, vb, element 
Example 11
Project: me-ica   Author: ME-ICA   File: test_utils.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_working_type():
    # Which type do input types with slope and inter cast to in numpy?
    # Wrapper function because we need to use the dtype str for comparison.  We
    # need this because of the very confusing np.int32 != np.intp (on 32 bit).
    def wt(*args, **kwargs):
        return np.dtype(working_type(*args, **kwargs)).str
    d1 = np.atleast_1d
    for in_type in NUMERIC_TYPES:
        in_ts = np.dtype(in_type).str
        assert_equal(wt(in_type), in_ts)
        assert_equal(wt(in_type, 1, 0), in_ts)
        assert_equal(wt(in_type, 1.0, 0.0), in_ts)
        in_val = d1(in_type(0))
        for slope_type in NUMERIC_TYPES:
            sl_val = slope_type(1) # no scaling, regardless of type
            assert_equal(wt(in_type, sl_val, 0.0), in_ts)
            sl_val = slope_type(2) # actual scaling
            out_val = in_val / d1(sl_val)
            assert_equal(wt(in_type, sl_val), out_val.dtype.str)
            for inter_type in NUMERIC_TYPES:
                i_val = inter_type(0) # no scaling, regardless of type
                assert_equal(wt(in_type, 1, i_val), in_ts)
                i_val = inter_type(1) # actual scaling
                out_val = in_val - d1(i_val)
                assert_equal(wt(in_type, 1, i_val), out_val.dtype.str)
                # Combine scaling and intercept
                out_val = (in_val - d1(i_val)) / d1(sl_val)
                assert_equal(wt(in_type, sl_val, i_val), out_val.dtype.str)
    # Confirm that type codes and dtypes work as well
    f32s = np.dtype(np.float32).str
    assert_equal(wt('f4', 1, 0), f32s)
    assert_equal(wt(np.dtype('f4'), 1, 0), f32s) 
Example 12
Project: me-ica   Author: ME-ICA   File: volumeutils.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def _ftype4scaled_finite(tst_arr, slope, inter, direction='read',
                         default=np.float32):
    """ Smallest float type for scaling of `tst_arr` that does not overflow
    """
    assert direction in ('read', 'write')
    if not default in OK_FLOATS and default is np.longdouble:
        # Omitted longdouble
        return default
    def_ind = OK_FLOATS.index(default)
    # promote to arrays to avoid numpy scalar casting rules
    tst_arr = np.atleast_1d(tst_arr)
    slope = np.atleast_1d(slope)
    inter = np.atleast_1d(inter)
    warnings.filterwarnings('ignore', '.*overflow.*', RuntimeWarning)
    try:
        for ftype in OK_FLOATS[def_ind:]:
            tst_trans = tst_arr.copy()
            slope = slope.astype(ftype)
            inter = inter.astype(ftype)
            if direction == 'read': # as in reading of image from disk
                if slope != 1.0:
                    tst_trans = tst_trans * slope
                if inter != 0.0:
                    tst_trans = tst_trans + inter
            elif direction == 'write':
                if inter != 0.0:
                    tst_trans = tst_trans - inter
                if slope != 1.0:
                    tst_trans = tst_trans / slope
            if np.all(np.isfinite(tst_trans)):
                return ftype
    finally:
        warnings.filters.pop(0)
    raise ValueError('Overflow using highest floating point type') 
Example 13
Project: simnibs   Author: simnibs   File: gpc.py    License: GNU General Public License v3.0 5 votes vote down vote up
def create_hdf5(self):
        '''Creates an HDF5 file to store the data '''
        # if the hdf5 file does not exist, create it
        file_exists = os.path.exists(self.fn_hdf5)
        if file_exists:
            raise IOError('Cannot create hdf5 file: {0} '
                          'it already exists!'.format(self.fn_hdf5))

        self.mesh.write_hdf5(self.fn_hdf5, 'mesh/')
        self.mesh_roi.write_hdf5(self.fn_hdf5, 'mesh_roi/')
        self.poslist._write_conductivity_to_hdf5(self.fn_hdf5)
        with h5py.File(self.fn_hdf5, 'a') as f:
            f.create_dataset('roi', data=np.atleast_1d(np.array(self.roi, dtype=int))) 
Example 14
Project: simnibs   Author: simnibs   File: gpc.py    License: GNU General Public License v3.0 5 votes vote down vote up
def record_data_matrix(self, data, name, group):
        ''' Appends or create data to the HDF5 file 

        Parameters:
        -------------
        data: np.ndarray
            Data to be appended. Will be appended along the first dimension
        name: str
            Name of data seet
        group: str
            Group where to place data set
        '''
        data = np.array(data).squeeze()
        data = np.atleast_1d(data)
        with h5py.File(self.fn_hdf5, 'a') as f:
            try:
                g = f.create_group(group)
            except:
                g = f[group]
            if name not in g.keys():
                g.create_dataset(name,
                                 shape=(0, ) + data.shape,
                                 maxshape=(None, ) + data.shape,
                                 dtype=data.dtype,
                                 chunks=(1, ) + data.shape)

            dset = g[name]
            dset.resize((dset.shape[0] + 1, ) + data.shape)
            dset[-1, ...] = data 
Example 15
Project: simnibs   Author: simnibs   File: gpc.py    License: GNU General Public License v3.0 5 votes vote down vote up
def run_simulation(self, random_vars):
        poslist = self._update_poslist(random_vars)
        cond = poslist.cond2elmdata(self.mesh)
        v = fem.tdcs(
            self.mesh, cond, self.el_currents,
            self.el_tags, units='mm')


        self.mesh.nodedata = [v]
        cropped = self.mesh.crop_mesh(self.roi)
        v_c = cropped.nodedata[0]
        self.mesh.nodedata = []

        qois = []
        for qoi_name, qoi_f in self.qoi_function.items():
            qois.append(qoi_f(v_c, random_vars))

        self.record_data_matrix(random_vars, 'random_var_samples', '/')
        self.record_data_matrix(v.value, 'v_samples', 'mesh/data_matrices')
        self.record_data_matrix(v_c.value, 'v_samples', 'mesh_roi/data_matrices')
        for qoi_name, qoi_v in zip(self.qoi_function.keys(), qois):
            self.record_data_matrix(
                qoi_v, qoi_name + '_samples', 'mesh_roi/data_matrices')

        del cropped
        del cond
        del v
        del v_c

        return np.atleast_1d(qois[0]).reshape(-1) 
Example 16
Project: simnibs   Author: simnibs   File: sim_struct.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _prepare(self):
        self.thickness = np.atleast_1d(self.thickness)
        if len(self.thickness) == 0:
            raise ValueError("Electrode thickness not defined!")

        if self.channelnr is None:
            logger.warning('Please connect the electrode to a channel') 
Example 17
Project: tangent   Author: google   File: grads.py    License: Apache License 2.0 5 votes vote down vote up
def atleast_1d(y, x):
  d[x] = numpy.reshape(d[y], numpy.shape(x)) 
Example 18
Project: tangent   Author: google   File: tangents.py    License: Apache License 2.0 5 votes vote down vote up
def tatleast_1d(z, x):
  d[z] = numpy.atleast_1d(d[x]) 
Example 19
Project: PyRadarMet   Author: nguy   File: variables.py    License: GNU General Public License v2.0 5 votes vote down vote up
def zdp(z_h, z_v):
    """
    Reflectivity difference [dB].

    From Rinehart (1997), Eqn 10.3

    Parameters
    ----------
    z_h : float
        Horizontal reflectivity [mm^6/m^3]
    z_v : float
        Horizontal reflectivity [mm^6/m^3]

    Notes
    -----
    Ensure that both powers have the same units!

    Alternating horizontally and linearly polarized pulses are averaged.
    """
    zh = np.atleast_1d(z_h)
    zv = np.atleast_1d(z_v)
    if len(zh) != len(zv):
        raise ValueError('Input variables must be same length')
        return

    zdp = np.full_like(zh, np.nan)
    good = np.where(zh > zv)
    zdp[good] = 10.* np.log10(zh[good] - zv[good])
    return zdp 
Example 20
Project: PyRadarMet   Author: nguy   File: variables.py    License: GNU General Public License v2.0 5 votes vote down vote up
def hdr(dbz_h, zdr):
    """
    Differential reflectivity [dB] hail signature.

    From Aydin et al. (1986), Eqns 4-5

    Parameters
    ----------
    dbz_h : float or array
        Horizontal reflectivity [dBZ]
    zdr : float or array
        Differential reflectivity [dBZ]

    Notes
    -----
    Ensure that both powers have the same units!

    Positive HDR and strong gradients at edges signify ice. The larger HDR,
       the greater likelihood that ice is present.

    Considerations for this equation (see paper for more details):
       1) Standar error of disdrometer data allowed for
       2) Drop oscillation accounted for based on 50% model of Seliga et al (1984)
       3) Lower (27) bound chose to provide constant Zh ref level
       4) Upper cutoff of 60 (may be too low)

    Picca and Ryzhkof (2012) mention that this does not take into account
    the hail melting process.  So use at your own risk!
    """
    zdr = np.atleast_1d(zdr)
    # Set the f(zdr) based upon observations
    f = np.full_like(zdr, np.nan)
    negind = np.where(zdr <= 0)
    lowind = np.where((zdr > 0) & (zdr <= 1.74))
    highind = np.where(zdr > 1.74)
    f[negind] = 27.
    f[lowind] = 19. * zdr[lowind] + 27.
    f[highind] = 60.
    # Calculate HDR
    return np.asarray(dbz_h) - f 
Example 21
Project: python-control   Author: python-control   File: xferfcn.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def minreal(self, tol=None):
        """Remove cancelling pole/zero pairs from a transfer function"""
        # based on octave minreal

        # default accuracy
        from sys import float_info
        sqrt_eps = sqrt(float_info.epsilon)

        # pre-allocate arrays
        num = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
        den = [[[] for j in range(self.inputs)] for i in range(self.outputs)]

        for i in range(self.outputs):
            for j in range(self.inputs):

                # split up in zeros, poles and gain
                newzeros = []
                zeros = roots(self.num[i][j])
                poles = roots(self.den[i][j])
                gain = self.num[i][j][0] / self.den[i][j][0]

                # check all zeros
                for z in zeros:
                    t = tol or \
                        1000 * max(float_info.epsilon, abs(z) * sqrt_eps)
                    idx = where(abs(z - poles) < t)[0]
                    if len(idx):
                        # cancel this zero against one of the poles
                        poles = delete(poles, idx[0])
                    else:
                        # keep this zero
                        newzeros.append(z)

                # poly([]) returns a scalar, but we always want a 1d array
                num[i][j] = np.atleast_1d(gain * real(poly(newzeros)))
                den[i][j] = np.atleast_1d(real(poly(poles)))

        # end result
        return TransferFunction(num, den, self.dt) 
Example 22
Project: rai-python   Author: MarcToussaint   File: transformations.py    License: MIT License 5 votes vote down vote up
def vector_norm(data, axis=None, out=None):
    """Return length, i.e. eucledian norm, of ndarray along axis.
    >>> v = numpy.random.random(3)
    >>> n = vector_norm(v)
    >>> numpy.allclose(n, numpy.linalg.norm(v))
    True
    >>> v = numpy.random.rand(6, 5, 3)
    >>> n = vector_norm(v, axis=-1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=2)))
    True
    >>> n = vector_norm(v, axis=1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> v = numpy.random.rand(5, 4, 3)
    >>> n = numpy.empty((5, 3), dtype=numpy.float64)
    >>> vector_norm(v, axis=1, out=n)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> vector_norm([])
    0.0
    >>> vector_norm([1.0])
    1.0
    """
    data = numpy.array(data, dtype=numpy.float64, copy=True)
    if out is None:
        if data.ndim == 1:
            return math.sqrt(numpy.dot(data, data))
        data *= data
        out = numpy.atleast_1d(numpy.sum(data, axis=axis))
        numpy.sqrt(out, out)
        return out
    else:
        data *= data
        numpy.sum(data, axis=axis, out=out)
        numpy.sqrt(out, out) 
Example 23
Project: rai-python   Author: MarcToussaint   File: transformations.py    License: MIT License 5 votes vote down vote up
def unit_vector(data, axis=None, out=None):
    """Return ndarray normalized by length, i.e. eucledian norm, along axis.
    >>> v0 = numpy.random.random(3)
    >>> v1 = unit_vector(v0)
    >>> numpy.allclose(v1, v0 / numpy.linalg.norm(v0))
    True
    >>> v0 = numpy.random.rand(5, 4, 3)
    >>> v1 = unit_vector(v0, axis=-1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=2)), 2)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = unit_vector(v0, axis=1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=1)), 1)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = numpy.empty((5, 4, 3), dtype=numpy.float64)
    >>> unit_vector(v0, axis=1, out=v1)
    >>> numpy.allclose(v1, v2)
    True
    >>> list(unit_vector([]))
    []
    >>> list(unit_vector([1.0]))
    [1.0]
    """
    if out is None:
        data = numpy.array(data, dtype=numpy.float64, copy=True)
        if data.ndim == 1:
            data /= math.sqrt(numpy.dot(data, data))
            return data
    else:
        if out is not data:
            out[:] = numpy.array(data, copy=False)
        data = out
    length = numpy.atleast_1d(numpy.sum(data*data, axis))
    numpy.sqrt(length, length)
    if axis is not None:
        length = numpy.expand_dims(length, axis)
    data /= length
    if out is None:
        return data 
Example 24
Project: astropy-healpix   Author: astropy   File: healpy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def nest2ring(nside, ipix):
    """Drop-in replacement for healpy `~healpy.pixelfunc.nest2ring`."""
    ipix = np.atleast_1d(ipix).astype(np.int64, copy=False)
    return nested_to_ring(ipix, nside) 
Example 25
Project: astropy-healpix   Author: astropy   File: healpy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ring2nest(nside, ipix):
    """Drop-in replacement for healpy `~healpy.pixelfunc.ring2nest`."""
    ipix = np.atleast_1d(ipix).astype(np.int64, copy=False)
    return ring_to_nested(ipix, nside) 
Example 26
Project: recruit   Author: Frank-qlu   File: mrecords.py    License: Apache License 2.0 5 votes vote down vote up
def fromarrays(arraylist, dtype=None, shape=None, formats=None,
               names=None, titles=None, aligned=False, byteorder=None,
               fill_value=None):
    """
    Creates a mrecarray from a (flat) list of masked arrays.

    Parameters
    ----------
    arraylist : sequence
        A list of (masked) arrays. Each element of the sequence is first converted
        to a masked array if needed. If a 2D array is passed as argument, it is
        processed line by line
    dtype : {None, dtype}, optional
        Data type descriptor.
    shape : {None, integer}, optional
        Number of records. If None, shape is defined from the shape of the
        first array in the list.
    formats : {None, sequence}, optional
        Sequence of formats for each individual field. If None, the formats will
        be autodetected by inspecting the fields and selecting the highest dtype
        possible.
    names : {None, sequence}, optional
        Sequence of the names of each field.
    fill_value : {None, sequence}, optional
        Sequence of data to be used as filling values.

    Notes
    -----
    Lists of tuples should be preferred over lists of lists for faster processing.

    """
    datalist = [getdata(x) for x in arraylist]
    masklist = [np.atleast_1d(getmaskarray(x)) for x in arraylist]
    _array = recfromarrays(datalist,
                           dtype=dtype, shape=shape, formats=formats,
                           names=names, titles=titles, aligned=aligned,
                           byteorder=byteorder).view(mrecarray)
    _array._mask.flat = list(zip(*masklist))
    if fill_value is not None:
        _array.fill_value = fill_value
    return _array 
Example 27
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_mem_custom_float_to_array(self):
        # Ticket 702
        class MyFloat(object):
            def __float__(self):
                return 1.0

        tmp = np.atleast_1d([MyFloat()])
        tmp.astype(float)  # Should succeed 
Example 28
Project: recruit   Author: Frank-qlu   File: test_nanops.py    License: Apache License 2.0 5 votes vote down vote up
def _minmax_wrap(self, value, axis=None, func=None):

        # numpy warns if all nan
        res = func(value, axis)
        if res.dtype.kind == 'm':
            res = np.atleast_1d(res)
        return res 
Example 29
Project: deepchem   Author: deepchem   File: datasets.py    License: MIT License 5 votes vote down vote up
def sparse_shuffle(self):
    """Shuffling that exploits data sparsity to shuffle large datasets.

    Only for 1-dimensional feature vectors (does not work for tensorial
    featurizations).
    """
    time1 = time.time()
    shard_size = self.get_shard_size()
    num_shards = self.get_number_shards()
    X_sparses, ys, ws, ids = [], [], [], []
    num_features = None
    for i in range(num_shards):
      (X_s, y_s, w_s, ids_s) = self.get_shard(i)
      if num_features is None:
        num_features = X_s.shape[1]
      X_sparse = sparsify_features(X_s)
      X_sparses, ys, ws, ids = (X_sparses + [X_sparse], ys + [y_s], ws + [w_s],
                                ids + [np.atleast_1d(np.squeeze(ids_s))])
    # Get full dataset in memory
    (X_sparse, y, w, ids) = (np.vstack(X_sparses), np.vstack(ys), np.vstack(ws),
                             np.concatenate(ids))
    # Shuffle in memory
    num_samples = len(X_sparse)
    permutation = np.random.permutation(num_samples)
    X_sparse, y, w, ids = (X_sparse[permutation], y[permutation],
                           w[permutation], ids[permutation])
    # Write shuffled shards out to disk
    for i in range(num_shards):
      start, stop = i * shard_size, (i + 1) * shard_size
      (X_sparse_s, y_s, w_s, ids_s) = (X_sparse[start:stop], y[start:stop],
                                       w[start:stop], ids[start:stop])
      X_s = densify_features(X_sparse_s, num_features)
      self.set_shard(i, X_s, y_s, w_s, ids_s)
    time2 = time.time()
    logger.info("TIMING: sparse_shuffle took %0.3f s" % (time2 - time1)) 
Example 30
Project: deepchem   Author: deepchem   File: datasets.py    License: MIT License 5 votes vote down vote up
def ids(self):
    """Get the ids vector for this dataset as a single numpy array."""
    if len(self) == 0:
      return np.array([])
    ids = []
    for i in range(self.get_number_shards()):
      ids.append(np.atleast_1d(np.squeeze(self.get_shard_ids(i))))
    return np.concatenate(ids)