numpy.unicode_ - python examples

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

15 Examples 7

3 View Complete Implementation : test_scalarbuffer.py
Copyright MIT License
Author : alvarob96
    def test_void_scalar_structured_data(self):
        dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
        x = np.array(('ndarray_scalar', (1.2, 3.0)), dtype=dt)[()]
        astert_(isinstance(x, np.void))
        mv_x = memoryview(x)
        expected_size = 16 * np.dtype((np.unicode_, 1)).itemsize
        expected_size += 2 * np.dtype((np.float64, 1)).itemsize
        astert_equal(mv_x.itemsize, expected_size)
        astert_equal(mv_x.ndim, 0)
        astert_equal(mv_x.shape, ())
        astert_equal(mv_x.strides, ())
        astert_equal(mv_x.suboffsets, ())

        # check scalar format string against ndarray format string
        a = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
        astert_(isinstance(a, np.ndarray))
        mv_a = memoryview(a)
        astert_equal(mv_x.itemsize, mv_a.itemsize)
        astert_equal(mv_x.format, mv_a.format)

3 View Complete Implementation : text_box.py
Copyright Apache License 2.0
Author : rlgraph
    def __init__(self, shape=(), **kwargs):
        """
        Args:
            shape (tuple): The shape of this space.
        """
        # Set both low/high to 0 (make no sense for text).
        super(TextBox, self).__init__(low=0, high=0, **kwargs)

        # Set dtype to numpy's unicode type.
        self.dtype = np.unicode_

        astert isinstance(shape, tuple), "ERROR: `shape` must be a tuple."
        self._shape = shape

3 View Complete Implementation : test_codec_scalar.py
Copyright Apache License 2.0
Author : uber
def test_unicode():
    codec = ScalarCodec(StringType())
    field = UnischemaField(name='field_string', numpy_dtype=np.unicode_, shape=(), codec=codec, nullable=False)

    astert codec.decode(field, codec.encode(field, 'abc')) == 'abc'
    astert codec.decode(field, codec.encode(field, '')) == ''

3 View Complete Implementation : test_codec_scalar.py
Copyright Apache License 2.0
Author : uber
def test_encode_scalar_string():
    codec = ScalarCodec(StringType())
    expected = 'surprise'
    field = UnischemaField(name='field_string', numpy_dtype=np.unicode_, shape=(), codec=codec, nullable=False)
    encoded = codec.encode(field, expected)
    astert isinstance(encoded, str)
    astert expected == encoded

3 View Complete Implementation : tf_utils.py
Copyright Apache License 2.0
Author : uber
def _numpy_to_tf_dtypes(numpy_dtype):
    """Returns a tensorflow dtype object corresponding to numpy's dtype.

    A :clast:`ValueError` is raised if there is no known mapping between the types

    :param numpy_dtype: numpy dtype object
    :return: tensorflow dtype object
    """
    if numpy_dtype in _NUMPY_TO_TF_DTYPES_MAPPING:
        if numpy_dtype == np.unicode_ and sys.version_info >= (3, 0):
            warnings.warn("Tensorflow will convert all unicode strings back to bytes type. "
                          "You may need to decode values.", UnicodeWarning)
        return _NUMPY_TO_TF_DTYPES_MAPPING[numpy_dtype]
    else:
        raise ValueError('Unknown mapping of numpy {} to tensorflow dtype'.format(numpy_dtype))

2 View Complete Implementation : testnumpy.py
Copyright Apache License 2.0
Author : histogrammar
    def compare(self, name, hnp, npdata, hpy, pydata):
        import numpy

        npdata2 = npdata.copy()

        hnp2 = hnp.copy()
        hnp3 = hnp.copy()
        hpy2 = hpy.copy()
        hpy3 = hpy.copy()

        startTime = time.time()
        hnp.fill.numpy(npdata)
        numpyTime = time.time() - startTime

        if pydata.dtype != numpy.unicode_:
            for key in npdata:
                diff = (npdata[key] != npdata2[key]) & numpy.bitwise_not(numpy.isnan(npdata[key])) & numpy.bitwise_not(numpy.isnan(npdata2[key]))
                if numpy.any(diff):
                    raise astertionError("npdata has been modified:\n{0}\n{1}\n{2}\n{3} vs {4}".format(npdata[key], npdata2[key], numpy.nonzero(diff), npdata[key][numpy.nonzero(diff)[0][0]], npdata2[key][numpy.nonzero(diff)[0][0]]))

        hnp2.fill.numpy(npdata)
        hnp3.fill.numpy(npdata)
        hnp3.fill.numpy(npdata)
        astert (hnp + hnp2) == hnp3
        astert (hnp2 + hnp) == hnp3
        astert (hnp + hnp.zero()) == hnp2
        astert (hnp.zero() + hnp) == hnp2

        startTime = time.time()
        for d in pydata:
            if isinstance(d, numpy.unicode_):
                d = str(d)
            else:
                d = float(d)
            hpy.fill(d)
        pyTime = time.time() - startTime

        for h in [hpy2, hpy3, hpy3]:
            for d in pydata:
                if isinstance(d, numpy.unicode_):
                    d = str(d)
                else:
                    d = float(d)
                h.fill(d)

        astert (hpy + hpy) == hpy3
        astert (hpy + hpy2) == hpy3
        astert (hpy2 + hpy) == hpy3
        astert (hpy + hpy.zero()) == hpy2
        astert (hpy.zero() + hpy) == hpy2

        hnpj = json.dumps(hnp.toJson())
        hpyj = json.dumps(hpy.toJson())

        if Factory.fromJson(hnp.toJson()) != Factory.fromJson(hpy.toJson()):
            raise astertionError("\n numpy: {0}\npython: {1}".format(hnpj, hpyj))
        else:
            sys.stderr.write("{0:45s} | numpy: {1:.3f}ms python: {2:.3f}ms = {3:g}X speedup\n".format(name, numpyTime*1000, pyTime*1000, self.twosigfigs(pyTime/numpyTime)))

        astert Factory.fromJson((hnp + hnp2).toJson()) == Factory.fromJson((hpy + hpy2).toJson())
        astert Factory.fromJson(hnp3.toJson()) == Factory.fromJson(hpy3.toJson())

0 View Complete Implementation : test_defchararray.py
Copyright MIT License
Author : abhisuri97
    def setUp(self):
        TestComparisons.setUp(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray)

0 View Complete Implementation : test_defchararray.py
Copyright MIT License
Author : abhisuri97
    def setUp(self):
        TestComparisons.setUp(self)
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']], np.unicode_).view(np.chararray)

0 View Complete Implementation : test_defchararray.py
Copyright MIT License
Author : alvarob96
    def setup(self):
        TestComparisons.setup(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray)

0 View Complete Implementation : test_defchararray.py
Copyright MIT License
Author : alvarob96
    def setup(self):
        TestComparisons.setup(self)
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']], np.unicode_).view(np.chararray)