numpy.recarray

Here are the examples of the python api numpy.recarray taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

71 Examples 7

Page 1 Selected Page 2

Example 1

Project: flopy Source File: util_list.py
Function: to_file
    def __tofile(self, f, data):
        # Write the recarray (data) to the file (or file handle) f
        assert isinstance(data, np.recarray), "MfList.__tofile() data arg " + \
                                              "not a recarray"

        # Add one to the kij indices
        lnames = [name.lower() for name in self.dtype.names]
        # --make copy of data for multiple calls
        d = np.recarray.copy(data)
        for idx in ['k', 'i', 'j', 'node']:
            if (idx in lnames):
                d[idx] += 1
        np.savetxt(f, d, fmt=self.fmt_string, delimiter='')

Example 2

Project: numba Source File: test_record_dtype.py
Function: test_record_write_2d_array
    @tag('important')
    def test_record_write_2d_array(self):
        '''
        Test writing to a 2D array within a structured type
        '''
        nbval = np.recarray(1, dtype=recordwith2darray)
        nbrecord = numpy_support.from_dtype(recordwith2darray)
        cfunc = self.get_cfunc(record_write_2d_array, (nbrecord,))
        cfunc(nbval[0])

        expected = np.recarray(1, dtype=recordwith2darray)
        expected[0].i = 3
        expected[0].j[:] = np.asarray([5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
                                      np.float32).reshape(3, 2)
        np.testing.assert_equal(expected, nbval)

Example 3

Project: insulaudit Source File: glucose.py
def np_file( filename ):
  """
    >>> len( np_file( 'test.txt' ) )
    10

    chokes on wonky separators
  """
  converters = { 'time': text2date }
  return np.genfromtxt( filename, dtype=DTYPES,
                        converters=converters ).view(np.recarray)

Example 4

Project: statsmodels Source File: test_tools.py
Function: test_recarray1d_drop
    def test_recarray1d_drop(self):
        instr = self.structdes['str_instr'].view(np.recarray)
        dum = tools.categorical(instr, drop=True)
        test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names]))
        assert_array_equal(test_dum, self.dummy)
        assert_equal(len(dum.dtype.names), 5)

Example 5

Project: hdf5storage Source File: test_write_readback.py
    def check_numpy_recarray(self, dimensions):
        # Makes a random structured ndarray of the given type, converts
        # it to a recarray, writes it and reads it back, and then
        # compares it.
        shape = random_numpy_shape(dimensions, \
            max_structured_ndarray_axis_length)
        data = random_structured_numpy_array(shape).view(np.recarray).copy()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

Example 6

Project: pyBAST Source File: sdss.py
    def recquery(self,sql,url=dr_url,fmt=def_fmt):
        """
        makes a recarray of the query results
        """
        rez = self._query(sql,url=url,fmt=fmt)
        #print rez.readlines()
        #rez.seek(0)
        tmp = rez.readline()
        rez.seek(0)
        if len(tmp) == 0 or tmp.find("error_message") != -1 or tmp.find("ERROR") != -1:
            print "rez:"
            print rez.readlines()
            return np.zeros((1,)).view(np.recarray)
       
        try:
            return csv2rec(rez)
        except:
            print "err"
            print rez.readlines()

Example 7

Project: numba Source File: test_iteration.py
    def test_array_1d_record_mutate_npm(self, flags=no_pyobj_flags):
        pyfunc = record_iter_mutate_usecase
        item_type = numpy_support.from_dtype(record_dtype)
        cr = compile_isolated(pyfunc, (types.Array(item_type, 1, 'A'),),
                              flags=flags)
        cfunc = cr.entry_point
        arr = np.recarray(3, dtype=record_dtype)
        for i in range(3):
            arr[i].a = float(i * 2)
            arr[i].b = i + 2
        expected = arr.copy()
        pyfunc(expected)
        got = arr.copy()
        cfunc(got)
        self.assertPreciseEqual(expected, got)

Example 8

Project: statsmodels Source File: test_data.py
Function: setup_class
    @classmethod
    def setupClass(cls):
        super(TestRecarrays, cls).setupClass()
        cls.endog = np.random.random(9).view([('y_1',
                                         'f8')]).view(np.recarray)
        exog = np.random.random(9*3).view([('const', 'f8'),('x_1', 'f8'),
                                ('x_2', 'f8')]).view(np.recarray)
        exog['const'] = 1
        cls.exog = exog
        cls.data = sm_data.handle_data(cls.endog, cls.exog)
        cls.xnames = ['const', 'x_1', 'x_2']
        cls.ynames = 'y_1'

Example 9

Project: numba Source File: test_record_dtype.py
Function: createsamplearrays
    def _createSampleArrays(self):
        '''
        Two different versions of the data structures are required because Numba
        supports attribute access on structured arrays, whereas Numpy does not.

        However, the semantics of recarrays and structured arrays are equivalent
        for these tests so Numpy with recarrays can be used for comparison with
        Numba using structured arrays.
        '''

        self.refsample1d = np.recarray(3, dtype=recordtype)
        self.refsample1d2 = np.recarray(3, dtype=recordtype2)
        self.refsample1d3 = np.recarray(3, dtype=recordtype)

        self.nbsample1d = np.zeros(3, dtype=recordtype)
        self.nbsample1d2 = np.zeros(3, dtype=recordtype2)
        self.nbsample1d3 = np.zeros(3, dtype=recordtype)

Example 10

Project: root_numpy Source File: tests.py
Function: test_slice
def test_slice():
    a = rnp.root2array(load('single1.root'), stop=10).view(np.recarray)
    assert_equal(len(a), 10)
    assert_equal(a.n_int[-1], 10)

    a = rnp.root2array(load('single1.root'), stop=11, start=1).view(np.recarray)
    assert_equal(len(a), 10)
    assert_equal(a.n_int[-1], 11)

    a = rnp.root2array(load('single1.root'), stop=105, start=95).view(np.recarray)
    assert_equal(len(a), 5)
    assert_equal(a.n_int[-1], 100)

Example 11

Project: statsmodels Source File: test_tools.py
Function: test_recarray1d
    def test_recarray1d(self):
        instr = self.structdes['str_instr'].view(np.recarray)
        dum = tools.categorical(instr)
        test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]]))
        assert_array_equal(test_dum, self.dummy)
        assert_equal(len(dum.dtype.names), 6)

Example 12

Project: statsmodels Source File: test_tools.py
Function: test_recarray1d
    def test_recarray1d(self):
        instr = self.structdes['instrument'].view(np.recarray)
        dum = tools.categorical(instr)
        test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names[-5:]]))
        assert_array_equal(test_dum, self.dummy)
        assert_equal(len(dum.dtype.names), 6)

Example 13

Project: numba Source File: test_recarray_usecases.py
    def _setup_usecase2to5(self, dtype):
        N = 5
        a = np.recarray(N, dtype=dtype)
        a.f1 = np.arange(N)
        a.f2 = np.arange(2, N + 2)
        a.s1 = np.array(['abc'] * a.shape[0], dtype='|S3')
        return a

Example 14

Project: flopy Source File: util_list.py
    def get_itmp(self, kper):
        if kper not in list(self.__data.keys()):
            return None
        # If an external file, have to load it
        if self.__vtype[kper] == str:
            return self.__fromfile(self.__data[kper]).shape[0]
        if self.__vtype[kper] == np.recarray:
            return self.__data[kper].shape[0]
        # If not any of the above, it must be an int
        return self.__data[kper]

Example 15

Project: flopy Source File: pakbase.py
Function: get_item
    def __getitem__(self, item):
        if hasattr(self, 'stress_period_data'):
            # added this check because stress_period_data also used in Oc and Oc88 but is not a MfList
            if isinstance(item, MfList):
                if not isinstance(item, list) and not isinstance(item, tuple):
                    assert item in list(
                            self.stress_period_data.data.keys()), "package.__getitem__() kper " + str(
                            item) + " not in data.keys()"
                    return self.stress_period_data[item]
                else:
                    if item[1] not in self.dtype.names:
                        raise Exception(
                                "package.__getitem(): item \'" + item + "\' not in dtype names " + str(
                                        self.dtype.names))
                    assert item[0] in list(
                            self.stress_period_data.data.keys()), "package.__getitem__() kper " + str(
                            item[0]) + " not in data.keys()"
                    if self.stress_period_data.vtype[item[0]] == np.recarray:
                        return self.stress_period_data[item[0]][item[1]]

Example 16

Project: numba Source File: test_globals.py
    def test_global_record(self):
        # (see github issue #1081)
        x = np.recarray(1, dtype=x_dt)[0]
        x.a = 1
        res = global_record_func(x)
        self.assertEqual(True, res)
        x.a = 2
        res = global_record_func(x)
        self.assertEqual(False, res)

Example 17

Project: numba Source File: test_iteration.py
    def test_array_1d_record(self, flags=force_pyobj_flags):
        pyfunc = record_iter_usecase
        item_type = numpy_support.from_dtype(record_dtype)
        cr = compile_isolated(pyfunc, (types.Array(item_type, 1, 'A'),),
                              flags=flags)
        cfunc = cr.entry_point
        arr = np.recarray(3, dtype=record_dtype)
        for i in range(3):
            arr[i].a = float(i * 2)
            arr[i].b = i + 2
        got = pyfunc(arr)
        self.assertPreciseEqual(cfunc(arr), got)

Example 18

Project: numba Source File: test_record_dtype.py
Function: createsamplearrays
    def _createSampleArrays(self):
        '''
        Set up the data structures to be used with the Numpy and Numba
        versions of functions.

        In this case, both accept recarrays.
        '''
        self.refsample1d = np.recarray(3, dtype=recordtype)
        self.refsample1d2 = np.recarray(3, dtype=recordtype2)
        self.refsample1d3 = np.recarray(3, dtype=recordtype)

        self.nbsample1d = np.recarray(3, dtype=recordtype)
        self.nbsample1d2 = np.recarray(3, dtype=recordtype2)
        self.nbsample1d3 = np.recarray(3, dtype=recordtype)

Example 19

Project: scikit-learn Source File: hetero_feature_union.py
Function: transform
    def transform(self, posts):
        features = np.recarray(shape=(len(posts),),
                               dtype=[('subject', object), ('body', object)])
        for i, text in enumerate(posts):
            headers, _, bod = text.partition('\n\n')
            bod = strip_newsgroup_footer(bod)
            bod = strip_newsgroup_quoting(bod)
            features['body'][i] = bod

            prefix = 'Subject:'
            sub = ''
            for line in headers.split('\n'):
                if line.startswith(prefix):
                    sub = line[len(prefix):]
                    break
            features['subject'][i] = sub

        return features

Example 20

Project: numba Source File: test_record_dtype.py
    def test_record_two_arrays(self):
        """
        Tests that comparison of NestedArrays by key is working correctly. If
        the two NestedArrays in recordwith2arrays compare equal (same length and
        ndim but different shape) incorrect code will be generated for one of the
        functions.
        """
        nbrecord = numpy_support.from_dtype(recordwith2arrays)
        rec = np.recarray(1, dtype=recordwith2arrays)[0]
        rec.k[:] = np.arange(200).reshape(10,20)
        rec.l[:] = np.arange(72).reshape(6,12)

        pyfunc = record_read_first_arr
        cfunc = self.get_cfunc(pyfunc, (nbrecord,))
        self.assertEqual(cfunc(rec), pyfunc(rec))

        pyfunc = record_read_second_arr
        cfunc = self.get_cfunc(pyfunc, (nbrecord,))
        self.assertEqual(cfunc(rec), pyfunc(rec))

Example 21

Project: numba Source File: test_record_dtype.py
    def test_record_read_array(self):
        '''
        Test reading from a 1D array within a structured type
        '''
        nbval = np.recarray(1, dtype=recordwitharray)
        nbval[0].h[0] = 15.0
        nbval[0].h[1] = 25.0
        nbrecord = numpy_support.from_dtype(recordwitharray)
        cfunc = self.get_cfunc(record_read_array0, (nbrecord,))
        res = cfunc(nbval[0])
        np.testing.assert_equal(res, nbval[0].h[0])

        cfunc = self.get_cfunc(record_read_array1, (nbrecord,))
        res = cfunc(nbval[0])
        np.testing.assert_equal(res, nbval[0].h[1])

Example 22

Project: statsmodels Source File: test_data.py
Function: check
    def check(self, dataset_name):
        dataset = __import__('statsmodels.datasets.' + dataset_name, fromlist=[''])
        data = dataset.load()
        assert_true(isinstance(data, Dataset))
        assert_true(isinstance(data.data, np.recarray))

        df_data = dataset.load_pandas()
        assert_true(isinstance(data, Dataset))
        assert_true(isinstance(df_data.data, pd.DataFrame))

Example 23

Project: statsmodels Source File: test_data.py
Function: setup_class
    @classmethod
    def setupClass(cls):
        super(TestStructarrays, cls).setupClass()
        cls.endog = np.random.random(9).view([('y_1',
                                         'f8')]).view(np.recarray)
        exog = np.random.random(9*3).view([('const', 'f8'),('x_1', 'f8'),
                                ('x_2', 'f8')]).view(np.recarray)
        exog['const'] = 1
        cls.exog = exog
        cls.data = sm_data.handle_data(cls.endog, cls.exog)
        cls.xnames = ['const', 'x_1', 'x_2']
        cls.ynames = 'y_1'

Example 24

Project: hdf5storage Source File: test_write_readback.py
    def check_numpy_recarray_empty(self, dimensions):
        # Makes a random structured ndarray of the given type, converts
        # it to a recarray, writes it and reads it back, and then
        # compares it.
        shape = random_numpy_shape(dimensions, \
            max_structured_ndarray_axis_length)
        data = random_structured_numpy_array(shape, (1, 0)).view(np.recarray).copy()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

Example 25

Project: statsmodels Source File: data.py
def load():
    """
    Load the data and return a Dataset class instance.

    Returns
    -------
    Dataset instance:
        See DATASET_PROPOSAL.txt for more information.
    """
    data = _get_data()
    names = data.columns.tolist()
    dtype = lzip(names, ['a45', 'a3', 'a40', 'a14'] + ['<f8'] * 54)
    data = lmap(tuple, data.values.tolist())
    dataset = du.Dataset(data=np.array(data, dtype=dtype).view(np.recarray), names=names)
    return dataset

Example 26

Project: vcfnp Source File: test_array.py
def test_svlen():
    v = variants('fixture/test13.vcf').view(np.recarray)
    assert hasattr(v, 'svlen')
    eq_(0, v.svlen[0])
    eq_(1, v.svlen[1])
    eq_(-1, v.svlen[2])
    eq_(3, v.svlen[3])
    eq_(3, v.svlen[4])
    v = variants('fixture/test13.vcf', arities={'svlen': 2}).view(np.recarray)
    assert hasattr(v, 'svlen')
    eq_((3, 0), tuple(v.svlen[3]))
    eq_((3, -2), tuple(v.svlen[4]))

Example 27

Project: hdf5storage Source File: test_write_readback.py
    def test_numpy_structured_array_unicode_fields(self):
        # Makes a random 1d structured ndarray with non-ascii characters
        # in its fields, writes it and reads it back, and then compares
        # it.
        shape = random_numpy_shape(1, \
            max_structured_ndarray_axis_length)
        data = random_structured_numpy_array(shape, \
            nonascii_fields=True).view(np.recarray).copy()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

Example 28

Project: flopy Source File: util_list.py
    def __cast_recarray(self, kper, d):
        assert d.dtype == self.__dtype, "MfList error: recarray dtype: " + \
                                        str(d.dtype) + " doesn't match " + \
                                        "self dtype: " + str(self.dtype)
        self.__data[kper] = d
        self.__vtype[kper] = np.recarray

Example 29

Project: Barak Source File: io.py
def readtabfits(filename, ext=None):
    """ Read fits binary table data, such as that written by
    `writetabfits()`.

    Consider using `atpy.Table(filename)` instead.
    """
    try:
        import pyfits
    except ImportError:
        import astropy.io.fits as pyfits

    if ext is not None:
        return pyfits.getdata(filename, ext=ext).view(np.recarray)
    else:
        return pyfits.getdata(filename).view(np.recarray)

Example 30

Project: numba Source File: test_record_dtype.py
    def test_record_write_array(self):
        '''
        Testing writing to a 1D array within a structured type
        '''
        nbval = np.recarray(1, dtype=recordwitharray)
        nbrecord = numpy_support.from_dtype(recordwitharray)
        cfunc = self.get_cfunc(record_write_array, (nbrecord,))
        cfunc(nbval[0])

        expected = np.recarray(1, dtype=recordwitharray)
        expected[0].g = 2
        expected[0].h[0] = 3.0
        expected[0].h[1] = 4.0
        np.testing.assert_equal(expected, nbval)

Example 31

Project: odo Source File: test_h5py.py
@pytest.mark.parametrize('ext', ['h5', 'hdf5'])
def test_resource_with_hdfstore_extension_works(ext):
    fn = 'hdfstore:foo.%s' % ext
    with pytest.raises(NotImplementedError):
        odo(fn, np.recarray)
    assert not os.path.exists(fn)

Example 32

Project: statsmodels Source File: test_tools.py
Function: test_recarray1d_drop
    def test_recarray1d_drop(self):
        instr = self.structdes['instrument'].view(np.recarray)
        dum = tools.categorical(instr, drop=True)
        test_dum = np.column_stack(([dum[_] for _ in dum.dtype.names]))
        assert_array_equal(test_dum, self.dummy)
        assert_equal(len(dum.dtype.names), 5)

Example 33

Project: petl Source File: numpy.py
Function: to_recarray
def torecarray(*args, **kwargs):
    """
    Convenient shorthand for ``toarray(*args, **kwargs).view(np.recarray)``.

    """

    import numpy as np
    return toarray(*args, **kwargs).view(np.recarray)

Example 34

Project: robothon Source File: mrecords.py
def addfield(mrecord, newfield, newfieldname=None):
    """Adds a new field to the masked record array, using `newfield` as data
and `newfieldname` as name. If `newfieldname` is None, the new field name is
set to 'fi', where `i` is the number of existing fields.
    """
    _data = mrecord._data
    _mask = mrecord._mask
    if newfieldname is None or newfieldname in reserved_fields:
        newfieldname = 'f%i' % len(_data.dtype)
    newfield = ma.array(newfield)
    # Get the new data ............
    # Create a new empty recarray
    newdtype = np.dtype(_data.dtype.descr + [(newfieldname, newfield.dtype)])
    newdata = recarray(_data.shape, newdtype)
    # Add the exisintg field
    [newdata.setfield(_data.getfield(*f),*f)
         for f in _data.dtype.fields.values()]
    # Add the new field
    newdata.setfield(newfield._data, *newdata.dtype.fields[newfieldname])
    newdata = newdata.view(MaskedRecords)
    # Get the new mask .............
    # Create a new empty recarray
    newmdtype = np.dtype([(n,bool_) for n in newdtype.names])
    newmask = recarray(_data.shape, newmdtype)
    # Add the old masks
    [newmask.setfield(_mask.getfield(*f),*f)
         for f in _mask.dtype.fields.values()]
    # Add the mask of the new field
    newmask.setfield(getmaskarray(newfield),
                     *newmask.dtype.fields[newfieldname])
    newdata._fieldmask = newmask
    return newdata

Example 35

Project: root_numpy Source File: _tree.py
def tree2rec(tree,
             branches=None,
             selection=None,
             object_selection=None,
             start=None,
             stop=None,
             step=None,
             include_weight=False,
             weight_name='weight',
             cache_size=-1):  # pragma: no cover
    """View the result of :func:`tree2array` as a record array.

    .. warning:: ``tree2rec`` is deprecated and will be removed in
       release 5.0.0. Instead use ``tree2array(...).view(np.recarray)``.

    Notes
    -----
    * This is equivalent to::

        tree2array(treename, branches).view(np.recarray)

    * Refer to the :ref:`type conversion table <conversion_table>`.

    See Also
    --------
    tree2array

    """
    warnings.warn("tree2rec is deprecated and will be removed in 5.0.0. "
                  "Instead use tree2array(...).view(np.recarray)",
                  DeprecationWarning)
    return tree2array(tree,
                      branches=branches,
                      selection=selection,
                      object_selection=object_selection,
                      start=start,
                      stop=stop,
                      step=step,
                      include_weight=include_weight,
                      weight_name=weight_name,
                      cache_size=cache_size).view(np.recarray)

Example 36

Project: flopy Source File: t027_test.py
def test_make_package():
    m4 = flopy.modflow.Modflow('mnw2example', model_ws=cpth)
    dis = flopy.modflow.ModflowDis(nrow=5, ncol=5, nlay=3, nper=3, top=10, botm=0, model=m4)

    # make the package from the tables (ztop, zbotm format)
    node_data = np.array([(0, 1, 1, 9.5, 7.1, 'well1', 'skin', -1, 0, 0, 0, 1.0, 2.0, 5.0, 6.2),
                          (1, 1, 1, 7.1, 5.1, 'well1', 'skin', -1, 0, 0, 0, 0.5, 2.0, 5.0, 6.2),
                          (2, 3, 3, 9.1, 3.7, 'well2', 'skin', -1, 0, 0, 0, 1.0, 2.0, 5.0, 4.1)],
                          dtype=[('index', '<i8'), ('i', '<i8'), ('j', '<i8'),
                                 ('ztop', '<f8'), ('zbotm', '<f8'),
                                 ('wellid', 'O'), ('losstype', 'O'), ('pumploc', '<i8'),
                                 ('qlimit', '<i8'), ('ppflag', '<i8'), ('pumpcap', '<i8'),
                                 ('rw', '<f8'), ('rskin', '<f8'), ('kskin', '<f8'),
                                 ('zpump', '<f8')]).view(np.recarray)

    stress_period_data = {0: np.array([(0, 0, 'well1', 0), (1, 0, 'well2', 0)],
           dtype=[('index', '<i8'), ('per', '<i8'), ('wellid', 'O'), ('qdes', '<i8')]).view(np.recarray),
                          1: np.array([(2, 1, 'well1', 100), (3, 1, 'well2', 1000)],
           dtype=[('index', '<i8'), ('per', '<i8'), ('wellid', 'O'), ('qdes', '<i8')]).view(np.recarray)}

    mnw2_4 = flopy.modflow.ModflowMnw2(model=m4, mnwmax=2, nodtot=3,
                 node_data=node_data,
                 stress_period_data=stress_period_data,
                 itmp=[2, 2, -1], # reuse second per pumping for last stress period
                 )
    m4.write_input()

    # make the package from the tables (k, i, j format)
    node_data = np.array([(0, 3, 1, 1, 'well1', 'skin', -1, 0, 0, 0, 1.0, 2.0, 5.0, 6.2),
                          (1, 2, 1, 1, 'well1', 'skin', -1, 0, 0, 0, 0.5, 2.0, 5.0, 6.2),
                          (2, 1, 3, 3, 'well2', 'skin', -1, 0, 0, 0, 1.0, 2.0, 5.0, 4.1)],
                          dtype=[('index', '<i8'), ('k', '<i8'), ('i', '<i8'), ('j', '<i8'),
                                 ('wellid', 'O'), ('losstype', 'O'), ('pumploc', '<i8'),
                                 ('qlimit', '<i8'), ('ppflag', '<i8'), ('pumpcap', '<i8'),
                                 ('rw', '<f8'), ('rskin', '<f8'), ('kskin', '<f8'),
                                 ('zpump', '<f8')]).view(np.recarray)

    stress_period_data = {0: np.array([(0, 0, 'well1', 0), (1, 0, 'well2', 0)],
           dtype=[('index', '<i8'), ('per', '<i8'), ('wellid', 'O'), ('qdes', '<i8')]).view(np.recarray),
                          1: np.array([(2, 1, 'well1', 100), (3, 1, 'well2', 1000)],
           dtype=[('index', '<i8'), ('per', '<i8'), ('wellid', 'O'), ('qdes', '<i8')]).view(np.recarray)}

    mnw2_4 = flopy.modflow.ModflowMnw2(model=m4, mnwmax=2, nodtot=3,
                 node_data=node_data,
                 stress_period_data=stress_period_data,
                 itmp=[2, 2, -1], # reuse second per pumping for last stress period
                 )
    spd = m4.mnw2.stress_period_data[0]
    inds = spd.k, spd.i, spd.j
    assert np.array_equal(np.array(inds).transpose(), np.array([(2, 1, 1), (1, 3, 3)]))
    m4.write_input()


    # make the package from the objects
    mnw2fromobj = flopy.modflow.ModflowMnw2(model=m4, mnwmax=2,
                 mnw=mnw2_4.mnw,
                 itmp=[2, 2, -1], # reuse second per pumping for last stress period
                 )
    # verify that the two input methods produce the same results
    assert np.array_equal(mnw2_4.stress_period_data[1], mnw2fromobj.stress_period_data[1])

Example 37

Project: flopy Source File: mbase.py
Function: check
    def check(self, f=None, verbose=True, level=1):
        """
        Check model data for common errors.

        Parameters
        ----------
        f : str or file handle
            String defining file name or file handle for summary file
            of check method output. If a string is passed a file handle
            is created. If f is None, check method does not write
            results to a summary file. (default is None)
        verbose : bool
            Boolean flag used to determine if check method results are
            written to the screen
        level : int
            Check method analysis level. If level=0, summary checks are
            performed. If level=1, full checks are performed.

        Returns
        -------
        None

        Examples
        --------

        >>> import flopy
        >>> m = flopy.modflow.Modflow.load('model.nam')
        >>> m.check()
        """

        results = {}
        for p in self.packagelist:
            results[p.name[0]] = p.check(f=None, verbose=False,
                                         level=level - 1)

        # check instance for model-level check
        chk = utils.check(self, f=f, verbose=verbose, level=level)

        # model level checks
        # solver check
        if self.version in chk.solver_packages.keys():
            solvers = set(chk.solver_packages[self.version]).intersection(
                set(self.get_package_list()))
            if not solvers:
                chk._add_to_summary('Error', desc='\r    No solver package',
                                    package='model')
            elif len(list(solvers)) > 1:
                for s in solvers:
                    chk._add_to_summary('Error',
                                        desc='\r    Multiple solver packages',
                                        package=s)
            else:
                chk.passed.append('Compatible solver package')

        # check for unit number conflicts
        package_units = {}
        duplicate_units = {}
        for p in self.packagelist:
            for i in range(len(p.name)):
                if p.unit_number[i] != 0:
                    if p.unit_number[i] in package_units.values():
                        duplicate_units[p.name[i]] = p.unit_number[i]
                        otherpackage = [k for k, v in package_units.items()
                                        if v == p.unit_number[i]][0]
                        duplicate_units[otherpackage] = p.unit_number[i]
        if len(duplicate_units) > 0:
            for k, v in duplicate_units.items():
                chk._add_to_summary('Error', package=k, value=v,
                                    desc='unit number conflict')
        else:
            chk.passed.append('Unit number conflicts')

        # add package check results to model level check summary
        for k, r in results.items():
            if r is not None and r.summary_array is not None:  # currently SFR doesn't have one
                chk.summary_array = np.append(chk.summary_array,
                                              r.summary_array).view(
                    np.recarray)
                chk.passed += ['{} package: {}'.format(r.package.name[0], psd)
                               for psd in r.passed]
        chk.summarize()
        return chk

Example 38

Project: root_numpy Source File: tests.py
def test_variable_length_arrays():
    f = load(['vary1.root', 'vary2.root'])
    a = rnp.root2array(f).view(np.recarray)

    assert_equal(
        a.dtype,
        [('len_n', '<i4'), ('len_f', '<i4'), ('len_d', '<i4'),
         ('n_char', 'O'), ('n_uchar', 'O'),
         ('n_short', 'O'), ('n_ushort', 'O'),
         ('n_int', 'O'), ('n_uint', 'O'),
         ('n_long', 'O'), ('n_ulong', 'O'),
         ('f_float', 'O'), ('d_double', 'O'),
         ('n2_int', 'O'), ('f2_float', 'O'), ('d2_double', 'O')])

    # check lengths
    for i in range(len(a)):
        assert_equal(a.len_n[i], len(a.n_int[i]))
        assert_equal(a.len_f[i], len(a.f_float[i]))
        assert_equal(a.len_d[i], len(a.d_double[i]))

        assert_equal((a.len_n[i], 2), a.n2_int[i].shape)
        assert_equal((a.len_f[i], 3), a.f2_float[i].shape)
        assert_equal((a.len_d[i], 4), a.d2_double[i].shape)

    # check elements
    assert_equal(a.len_n[0], 0)
    assert_equal(a.len_f[0], 1)
    assert_equal(a.len_d[0], 2)
    assert_equal(a.n_int[-1][-1], 417)
    assert_equal(a.f_float[-1][0], 380.5)
    assert_equal(a.f_float[-1][-1], 456.5)
    assert_equal(a.d_double[-1][0], 380.25)
    assert_equal(a.d_double[-1][-1], 497.25)

    # read only array without "length leaf"
    b = rnp.root2array(f, branches='n_int')
    for i in range(len(b)):
        assert_equal(len(b[i]), a.len_n[i])

Example 39

Project: statsmodels Source File: tools.py
def categorical(data, col=None, dictnames=False, drop=False, ):
    '''
    Returns a dummy matrix given an array of categorical variables.

    Parameters
    ----------
    data : array
        A structured array, recarray, or array.  This can be either
        a 1d vector of the categorical variable or a 2d array with
        the column specifying the categorical variable specified by the col
        argument.
    col : 'string', int, or None
        If data is a structured array or a recarray, `col` can be a string
        that is the name of the column that contains the variable.  For all
        arrays `col` can be an int that is the (zero-based) column index
        number.  `col` can only be None for a 1d array.  The default is None.
    dictnames : bool, optional
        If True, a dictionary mapping the column number to the categorical
        name is returned.  Used to have information about plain arrays.
    drop : bool
        Whether or not keep the categorical variable in the returned matrix.

    Returns
    --------
    dummy_matrix, [dictnames, optional]
        A matrix of dummy (indicator/binary) float variables for the
        categorical data.  If dictnames is True, then the dictionary
        is returned as well.

    Notes
    -----
    This returns a dummy variable for EVERY distinct variable.  If a
    a structured or recarray is provided, the names for the new variable is the
    old variable name - underscore - category name.  So if the a variable
    'vote' had answers as 'yes' or 'no' then the returned array would have to
    new variables-- 'vote_yes' and 'vote_no'.  There is currently
    no name checking.

    Examples
    --------
    >>> import numpy as np
    >>> import statsmodels.api as sm

    Univariate examples

    >>> import string
    >>> string_var = [string.lowercase[0:5], string.lowercase[5:10],   \
                string.lowercase[10:15], string.lowercase[15:20],   \
                string.lowercase[20:25]]
    >>> string_var *= 5
    >>> string_var = np.asarray(sorted(string_var))
    >>> design = sm.tools.categorical(string_var, drop=True)

    Or for a numerical categorical variable

    >>> instr = np.floor(np.arange(10,60, step=2)/10)
    >>> design = sm.tools.categorical(instr, drop=True)

    With a structured array

    >>> num = np.random.randn(25,2)
    >>> struct_ar = np.zeros((25,1), dtype=[('var1', 'f4'),('var2', 'f4'),  \
                    ('instrument','f4'),('str_instr','a5')])
    >>> struct_ar['var1'] = num[:,0][:,None]
    >>> struct_ar['var2'] = num[:,1][:,None]
    >>> struct_ar['instrument'] = instr[:,None]
    >>> struct_ar['str_instr'] = string_var[:,None]
    >>> design = sm.tools.categorical(struct_ar, col='instrument', drop=True)

    Or

    >>> design2 = sm.tools.categorical(struct_ar, col='str_instr', drop=True)
    '''
    if isinstance(col, (list, tuple)):
        try:
            assert len(col) == 1
            col = col[0]
        except:
            raise ValueError("Can only convert one column at a time")

    # TODO: add a NameValidator function
    # catch recarrays and structured arrays
    if data.dtype.names or data.__class__ is np.recarray:
        if not col and np.squeeze(data).ndim > 1:
            raise IndexError("col is None and the input array is not 1d")
        if isinstance(col, (int, long)):
            col = data.dtype.names[col]
        if col is None and data.dtype.names and len(data.dtype.names) == 1:
            col = data.dtype.names[0]

        tmp_arr = np.unique(data[col])

        # if the cols are shape (#,) vs (#,1) need to add an axis and flip
        _swap = True
        if data[col].ndim == 1:
            tmp_arr = tmp_arr[:, None]
            _swap = False
        tmp_dummy = (tmp_arr == data[col]).astype(float)
        if _swap:
            tmp_dummy = np.squeeze(tmp_dummy).swapaxes(1, 0)

        if not tmp_arr.dtype.names:  # how do we get to this code path?
            tmp_arr = [asstr2(item) for item in np.squeeze(tmp_arr)]
        elif tmp_arr.dtype.names:
            tmp_arr = [asstr2(item) for item in np.squeeze(tmp_arr.tolist())]

        # prepend the varname and underscore, if col is numeric attribute
        # lookup is lost for recarrays...
        if col is None:
            try:
                col = data.dtype.names[0]
            except:
                col = 'var'
        # TODO: the above needs to be made robust because there could be many
        # var_yes, var_no varaibles for instance.
        tmp_arr = [col + '_' + item for item in tmp_arr]
        # TODO: test this for rec and structured arrays!!!

        if drop is True:
            if len(data.dtype) <= 1:
                if tmp_dummy.shape[0] < tmp_dummy.shape[1]:
                    tmp_dummy = np.squeeze(tmp_dummy).swapaxes(1, 0)
                dt = lzip(tmp_arr, [tmp_dummy.dtype.str]*len(tmp_arr))
                # preserve array type
                return np.array(lmap(tuple, tmp_dummy.tolist()),
                                dtype=dt).view(type(data))

            data = nprf.drop_fields(data, col, usemask=False,
                                    asrecarray=type(data) is np.recarray)
        data = nprf.append_fields(data, tmp_arr, data=tmp_dummy,
                                  usemask=False,
                                  asrecarray=type(data) is np.recarray)
        return data

    # handle ndarrays and catch array-like for an error
    elif data.__class__ is np.ndarray or not isinstance(data, np.ndarray):
        if not isinstance(data, np.ndarray):
            raise NotImplementedError("Array-like objects are not supported")

        if isinstance(col, (int, long)):
            offset = data.shape[1]          # need error catching here?
            tmp_arr = np.unique(data[:, col])
            tmp_dummy = (tmp_arr[:, np.newaxis] == data[:, col]).astype(float)
            tmp_dummy = tmp_dummy.swapaxes(1, 0)
            if drop is True:
                offset -= 1
                data = np.delete(data, col, axis=1).astype(float)
            data = np.column_stack((data, tmp_dummy))
            if dictnames is True:
                col_map = _make_dictnames(tmp_arr, offset)
                return data, col_map
            return data
        elif col is None and np.squeeze(data).ndim == 1:
            tmp_arr = np.unique(data)
            tmp_dummy = (tmp_arr[:, None] == data).astype(float)
            tmp_dummy = tmp_dummy.swapaxes(1, 0)
            if drop is True:
                if dictnames is True:
                    col_map = _make_dictnames(tmp_arr)
                    return tmp_dummy, col_map
                return tmp_dummy
            else:
                data = np.column_stack((data, tmp_dummy))
                if dictnames is True:
                    col_map = _make_dictnames(tmp_arr, offset=1)
                    return data, col_map
                return data
        else:
            raise IndexError("The index %s is not understood" % col)

Example 40

Project: insulaudit Source File: glucose.py
Function: parse_text
def parse_text( text ):
  """
  A glucose record is a tuple of the time and glucose.
    ( datetime.datetime, int )

  >>> len( parse_text(  '''2011-01-01 01:02  076''' )[ 0 ] )
  2

  # spaces
  >>> date, value = parse_text(  '''2011-01-01 01:02  076''' )[ 0 ]
  ... #
  >>> date.isoformat( )
  '2011-01-01T01:02:00'
  >>> value
  76

  # tabs
  >>> date, value = parse_text(  '''2011-01-01T01:02	076 ''' )[ 0 ]
  >>> (date.isoformat( ), value)
  ('2011-01-01T01:02:00', 76)

  # T
  >>> date, value = parse_text(  '''2011-01-01T01:02	076''' )[ 0 ]
  >>> date.isoformat( ), value
  ('2011-01-01T01:02:00', 76)

  # PM/AM
  >>> date, value = parse_text(  '''2011-01-01 01:02AM 076''' )[ 0 ]
  >>> date.isoformat( ), value
  ('2011-01-01T01:02:00', 76)
  >>> date, value = parse_text(  '''2011-01-01 01:02PM 076''' )[ 0 ]
  >>> date.isoformat( ), value
  ('2011-01-01T13:02:00', 76)
  >>> date, value = parse_text(  '''2011-01-01	01:02PM 076''' )[ 0 ]
  >>> date.isoformat( ), value
  ('2011-01-01T13:02:00', 76)


  """
  # TODO: sensitivity to timezones!
  results = [ ]
  for datum in text.splitlines( ):
    frags = datum.strip( ).split( )
    if frags == [ ]: continue
    log.info( frags )
    #frags = map( string.strip, datum.strip( ).split( ) )
    value = int( frags[ -1 ] )
    date  = None
    try:
      date = text2date( ' '.join( frags[ 0:-1 ] ) )
      results.append( ( date, value ) )
    except IndexError, e:
      log.error( 'error %s' % ( e ) )

  return np.array( results, dtype=DTYPES ).view( np.recarray, )

Example 41

Project: insulaudit Source File: glucose.py
def l2np( L ):
  return np.array( L, dtype=DTYPES ).view( np.recarray, )

Example 42

Project: pgmpy Source File: NUTS.py
Function: sample
    def sample(self, initial_pos, num_samples, stepsize=None):
        """
        Method to return samples using No U Turn Sampler

        Parameters
        ----------
        initial_pos: A 1d array like object
            Vector representing values of parameter position, the starting
            state in markov chain.

        num_samples: int
            Number of samples to be generated

        stepsize: float , defaults to None
            The stepsize for proposing new values of position and momentum in simulate_dynamics
            If None, then will be choosen suitably

        Returns
        -------
        Returns two different types (based on installations)

        pandas.DataFrame: Returns samples as pandas.DataFrame if environment has a installation of pandas

        numpy.recarray: Returns samples in form of numpy recorded arrays (numpy.recarray)

        Examples
        ---------
        >>> # If environment has a installation of pandas
        >>> from pgmpy.inference.continuous import NoUTurnSampler as NUTS, GradLogPDFGaussian, LeapFrog
        >>> from pgmpy.factors import JointGaussianDistribution as JGD
        >>> import numpy as np
        >>> mean = np.array([0, 0, 0])
        >>> covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]])
        >>> model = JGD(['x', 'y', 'z'], mean, covariance)
        >>> sampler = NUTS(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=LeapFrog)
        >>> samples = sampler.sample(initial_pos=np.array([1, 1, 1]), num_samples=10, stepsize=0.4)
        >>> samples
                  x         y         z
        0  1.000000  1.000000  1.000000
        1  1.760756  0.271543 -0.613309
        2  1.883387  0.990745 -0.611720
        3  0.980812  0.340336 -0.916283
        4  0.781338  0.647220 -0.948640
        5  0.040308 -1.391406  0.412201
        6  1.179549 -1.450552  1.105216
        7  1.100320 -1.313926  1.207815
        8  1.484520 -1.349247  0.768599
        9  0.934942 -1.894589  0.471772
        """
        initial_pos = _check_1d_array_object(initial_pos, 'initial_pos')
        _check_length_equal(initial_pos, self.model.variables, 'initial_pos', 'model.variables')

        if stepsize is None:
            stepsize = self._find_reasonable_stepsize(initial_pos)

        types = [(var_name, 'float') for var_name in self.model.variables]
        samples = np.zeros(num_samples, dtype=types).view(np.recarray)

        samples[0] = tuple(initial_pos)
        position_m = initial_pos

        for i in range(1, num_samples):
            # Genrating sample
            position_m = self._sample(position_m, stepsize)
            samples[i] = position_m

        if HAS_PANDAS is True:
            return pd.DataFrame.from_records(samples)

        return samples

Example 43

Project: root_numpy Source File: tests.py
Function: test_vector
def test_vector():
    a = rnp.root2array(load('vector.root')).view(np.recarray)
    types = [
        ('v_i', 'O'),
        ('v_f', 'O'),
        ('v_F', 'O'),
        ('v_d', 'O'),
        ('v_l', 'O'),
        ('v_c', 'O'),
        ('v_b', 'O'),
        ('vv_i', 'O'),
        ('vv_f', 'O'),
        ('vv_F', 'O'),
        ('vv_d', 'O'),
        ('vv_l', 'O'),
        ('vv_c', 'O'),
        ('vv_b', 'O'),
    ]
    assert_equal(a.dtype, types)

    assert_equal(a.v_i[0].dtype, np.int32)
    assert_equal(a.v_f[0].dtype, np.float32)
    assert_equal(a.v_F[0].dtype, np.float32)
    assert_equal(a.v_d[0].dtype, np.float64)
    assert_equal(a.v_l[0].dtype, np.int64)
    assert_equal(a.v_c[0].dtype, np.int8)
    assert_equal(a.v_b[0].dtype, np.bool)

    # assert that wrapper array is np.object
    assert_equal(a.vv_i[0].dtype, np.object)
    assert_equal(a.vv_f[0].dtype, np.object)
    assert_equal(a.vv_F[0].dtype, np.object)
    assert_equal(a.vv_d[0].dtype, np.object)
    assert_equal(a.vv_l[0].dtype, np.object)
    assert_equal(a.vv_c[0].dtype, np.object)
    assert_equal(a.vv_b[0].dtype, np.object)

    assert_equal(a.vv_i[0][0].dtype, np.int32)
    assert_equal(a.vv_f[0][0].dtype, np.float32)
    assert_equal(a.vv_F[0][0].dtype, np.float32)
    assert_equal(a.vv_d[0][0].dtype, np.float64)
    assert_equal(a.vv_l[0][0].dtype, np.int64)
    assert_equal(a.vv_c[0][0].dtype, np.int8)
    assert_equal(a.vv_b[0][0].dtype, np.bool)

    # check a few values
    assert_equal(a.v_i[0][0], 1)
    assert_equal(a.v_i[1][1], 3)
    assert_equal(a.v_i[-2][0], 9)
    assert_equal(a.v_i[-2][-1], 17)

    assert_equal(a.v_f[0][0], 2.0)
    assert_equal(a.v_f[1][1], 5.0)
    assert_equal(a.v_f[-2][0], 18.0)
    assert_equal(a.v_f[-2][-1], 26.0)

    assert_equal(a.v_F[0][0], 2.0)
    assert_equal(a.v_F[1][1], 5.0)
    assert_equal(a.v_F[-2][0], 18.0)
    assert_equal(a.v_F[-2][-1], 26.0)

    # more strict conditioning for numpy arrays
    def assert_equal_array(arr1, arr2):
        return assert_equal((arr1 == arr2).all(), True,
            "array mismatch: {0} != {1}".format(arr1, arr2))

    assert_equal_array(a.vv_i[0][0], np.array([1], dtype=np.int32) )
    assert_equal_array(a.vv_i[1][1], np.array([2, 3], dtype=np.int32) )
    assert_equal_array(a.vv_i[-2][0], np.array([9], dtype=np.int32) )
    assert_equal_array(a.vv_i[-2][-1],
                       np.array([ 9, 10, 11, 12, 13, 14, 15, 16, 17],
                                dtype=np.int32))

    assert_equal_array(a.vv_f[0][0], np.array([ 2.], dtype=np.float32) )
    assert_equal_array(a.vv_f[1][1], np.array([ 4.,  5.], dtype=np.float32) )
    assert_equal_array(a.vv_f[-2][0], np.array([ 18.], dtype=np.float32) )
    assert_equal_array(a.vv_f[-2][-1],
                       np.array([ 18.,  19.,  20.,  21.,  22.,
                                  23.,  24.,  25.,  26.],
                                dtype=np.float32))

    assert_equal_array(a.vv_F[0][0], np.array([ 2.], dtype=np.float32) )
    assert_equal_array(a.vv_F[1][1], np.array([ 4.,  5.], dtype=np.float32) )
    assert_equal_array(a.vv_F[-2][0], np.array([ 18.], dtype=np.float32) )
    assert_equal_array(a.vv_F[-2][-1],
                       np.array([ 18.,  19.,  20.,  21.,  22.,
                                  23.,  24.,  25.,  26.],
                                dtype=np.float32))

Example 44

Project: blaze Source File: numpy.py
@compute_up.register(Distinct, np.recarray)
def recarray_distinct(t, rec, **kwargs):
    return pd.DataFrame.from_records(rec).drop_duplicates(
        subset=t.on or None).to_records(index=False).astype(rec.dtype)

Example 45

Project: odo Source File: convert.py
@convert.register(np.recarray, np.ndarray, cost=0.0)
def ndarray_to_recarray(x, **kwargs):
    return x.view(np.recarray)

Example 46

Project: trtools Source File: pytables.py
def convert_frame(df):
    """
        Input: DataFrame
        Output: pytable table description and pytable compatible recarray
    """
    sdict = OrderedDict()
    atoms = OrderedDict()
    types = OrderedDict()

    #index
    index_name = df.index.name or 'pd_index'
    converted, inferred_type, atom = _convert_obj(df.index)
    atoms[index_name] = atom
    sdict[index_name] = converted
    types[index_name] = inferred_type


    # columns
    for col in df.columns:
        converted, inferred_type, atom = _convert_obj(df[col])
        atoms[col] = atom
        sdict[col] = converted
        types[col] = inferred_type

    # create table desc
    desc = OrderedDict() 
    for pos, data in enumerate(atoms.items()):
        k, atom = data
        col = tb.Col.from_atom(atom, pos=pos) 
        desc[str(k)] = col

    # create recarray
    dtypes = [(str(k), v.dtype) for k, v in list(sdict.items())]
    recs = np.recarray(shape=len(df), dtype=dtypes)
    for k, v in list(sdict.items()):
        recs[str(k)] = v
    return desc, recs, types

Example 47

Project: pgmpy Source File: HMC.py
Function: sample
    def sample(self, initial_pos, num_samples, trajectory_length, stepsize=None):
        """
        Method to return samples using Hamiltonian Monte Carlo

        Parameters
        ----------
        initial_pos: A 1d array like object
            Vector representing values of parameter position, the starting
            state in markov chain.

        num_samples: int
            Number of samples to be generated

        trajectory_length: int or float
            Target trajectory length, stepsize * number of steps(L),
            where L is the number of steps taken per HMC iteration,
            and stepsize is step size for splitting time method.

        stepsize: float , defaults to None
            The stepsize for proposing new values of position and momentum in simulate_dynamics
            If None, then will be choosen suitably


        Returns
        -------
        Returns two different types (based on installations)

        pandas.DataFrame: Returns samples as pandas.DataFrame if environment has a installation of pandas

        numpy.recarray: Returns samples in form of numpy recorded arrays (numpy.recarray)

        Examples
        --------
        >>> # Example if pandas is installed in working environment
        >>> from pgmpy.sampling import HamiltonianMC as HMC, GradLogPDFGaussian, ModifiedEuler
        >>> from pgmpy.factors.continuous import JointGaussianDistribution as JGD
        >>> import numpy as np
        >>> mean = np.array([1, -1])
        >>> covariance = np.array([[1, 0.2], [0.2, 1]])
        >>> model = JGD(['x', 'y'], mean, covariance)
        >>> sampler = HMC(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=ModifiedEuler)
        >>> samples = sampler.sample(np.array([1, 1]), num_samples = 5,
        ...                          trajectory_length=6, stepsize=0.25)
        >>> samples
                       x              y
        0   1.000000e+00   1.000000e+00
        1   1.592133e+00   1.152911e+00
        2   1.608700e+00   1.315349e+00
        3   1.608700e+00   1.315349e+00
        4   6.843856e-01   6.237043e-01
        >>> mean = np.array([4, 1, -1])
        >>> covariance = np.array([[1, 0.7 , 0.8], [0.7, 1, 0.2], [0.8, 0.2, 1]])
        >>> model = JGD(['x', 'y', 'z'], mean, covariance)
        >>> sampler = HMC(model=model, grad_log_pdf=GLPG)
        >>> samples = sampler.sample(np.array([1, 1]), num_samples = 10000,
        ...                          trajectory_length=6, stepsize=0.25)
        >>> np.cov(samples.values.T)
        array([[ 1.00795398,  0.71384233,  0.79802097],
               [ 0.71384233,  1.00633524,  0.21313767],
               [ 0.79802097,  0.21313767,  0.98519017]])
        """

        self.accepted_proposals = 1.0
        initial_pos = _check_1d_array_object(initial_pos, 'initial_pos')
        _check_length_equal(initial_pos, self.model.variables, 'initial_pos', 'model.variables')

        if stepsize is None:
            stepsize = self._find_reasonable_stepsize(initial_pos)

        types = [(var_name, 'float') for var_name in self.model.variables]
        samples = np.zeros(num_samples, dtype=types).view(np.recarray)

        # Assigning after converting into tuple because value was being changed after assignment
        # Reason for this is unknown
        samples[0] = tuple(initial_pos)
        position_m = initial_pos

        lsteps = int(max(1, round(trajectory_length / stepsize, 0)))
        for i in range(1, num_samples):

            # Genrating sample
            position_m, _ = self._sample(position_m, trajectory_length, stepsize, lsteps)
            samples[i] = position_m

        self.acceptance_rate = self.accepted_proposals / num_samples

        if HAS_PANDAS is True:
            return pd.DataFrame.from_records(samples)

        return samples

Example 48

Project: zipline Source File: mixins.py
    def _allocate_output(self, windows, shape):
        """
        Allocate an output array whose rows should be passed to `self.compute`.

        The resulting array must have a shape of ``shape``.

        If we have standard outputs (i.e. self.outputs is NotSpecified), the
        default is an empty ndarray whose dtype is ``self.dtype``.

        If we have an outputs tuple, the default is an empty recarray with
        ``self.outputs`` as field names. Each field will have dtype
        ``self.dtype``.

        This can be overridden to control the kind of array constructed
        (e.g. to produce a LabelArray instead of an ndarray).
        """
        missing_value = self.missing_value
        outputs = self.outputs
        if outputs is not NotSpecified:
            out = recarray(
                shape,
                formats=[self.dtype.str] * len(outputs),
                names=outputs,
            )
            out[:] = missing_value
        else:
            out = full(shape, missing_value, dtype=self.dtype)
        return out

Example 49

Project: root_numpy Source File: _tree.py
def root2rec(filenames,
             treename=None,
             branches=None,
             selection=None,
             object_selection=None,
             start=None,
             stop=None,
             step=None,
             include_weight=False,
             weight_name='weight',
             cache_size=-1,
             warn_missing_tree=False):  # pragma: no cover
    """View the result of :func:`root2array` as a record array.

    .. warning:: ``root2rec`` is deprecated and will be removed in
       release 5.0.0. Instead use ``root2array(...).view(np.recarray)``.

    Notes
    -----
    * This is equivalent to::

        root2array(filenames, treename, branches).view(np.recarray)

    * Refer to the :ref:`type conversion table <conversion_table>`.

    See Also
    --------
    root2array

    """
    warnings.warn("root2rec is deprecated and will be removed in 5.0.0. "
                  "Instead use root2array(...).view(np.recarray)",
                  DeprecationWarning)
    return root2array(filenames, treename,
                      branches, selection, object_selection,
                      start, stop, step,
                      include_weight,
                      weight_name,
                      cache_size,
                      warn_missing_tree).view(np.recarray)

Example 50

Project: hdf5storage Source File: test_write_readback.py
    def check_numpy_recarray_field_special_char(self, ch,
                                                leading=False):
        # Makes a random 1d structured ndarray with the character
        # in one field, converts it to a recarray, writes it and reads
        # it back, and then compares it.
        field_names = [random_str_ascii(max_dict_key_length)
                       for i in range(2)]
        if leading:
            field_names[1] = ch + field_names[1]
        else:
            field_names[1] = field_names[1][0] + ch + field_names[1][1:]
        if sys.hexversion < 0x03000000:
            for i in range(len(field_names)):
                field_names[i] = field_names[i].encode('UTF-8')
        shape = random_numpy_shape(1, \
            max_structured_ndarray_axis_length)
        data = random_structured_numpy_array(shape, names=field_names).view(np.recarray).copy()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)
See More Examples - Go to Next Page
Page 1 Selected Page 2