Python numpy.chararray() Examples

The following are 30 code examples for showing how to use numpy.chararray(). 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: reinforcement_learning   Author: yrlu   File: gridworld.py    License: MIT License 6 votes vote down vote up
def display_policy_grid(self, policy):
    """
    prints a nice table of the policy in grid
    input:
      policy    a dictionary of the optimal policy {<state, action_dist>}
    """
    print "==Display policy grid=="

    policy_grid = np.chararray((len(self.grid), len(self.grid[0])))
    for k in self.get_states():
      if self.is_terminal((k[0], k[1])) or self.grid[k[0]][k[1]] == 'x':
        policy_grid[k[0]][k[1]] = '-'
      else:
        # policy_grid[k[0]][k[1]] = self.dirs[agent.get_action((k[0], k[1]))]
        policy_grid[k[0]][k[1]] = self.dirs[policy[(k[0], k[1])][0][0]]

    row_format = '{:>20}' * (len(self.grid[0]))
    for row in policy_grid:
      print row_format.format(*row) 
Example 2
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 6 votes vote down vote up
def test_slice(self):
        """Regression test for https://github.com/numpy/numpy/issues/5982"""

        arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
                       dtype='S4').view(np.chararray)
        sl1 = arr[:]
        assert_array_equal(sl1, arr)
        assert_(sl1.base is arr)
        assert_(sl1.base.base is arr.base)

        sl2 = arr[:, :]
        assert_array_equal(sl2, arr)
        assert_(sl2.base is arr)
        assert_(sl2.base.base is arr.base)

        assert_(arr[0, 0] == b'abc') 
Example 3
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 6 votes vote down vote up
def test_slice(self):
        """Regression test for https://github.com/numpy/numpy/issues/5982"""

        arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
                       dtype='S4').view(np.chararray)
        sl1 = arr[:]
        assert_array_equal(sl1, arr)
        assert_(sl1.base is arr)
        assert_(sl1.base.base is arr.base)

        sl2 = arr[:, :]
        assert_array_equal(sl2, arr)
        assert_(sl2.base is arr)
        assert_(sl2.base.base is arr.base)

        assert_(arr[0, 0] == asbytes('abc')) 
Example 4
Project: coded   Author: bullocke   File: postprocess_utils.py    License: MIT License 6 votes vote down vote up
def min_max_years(config, image, before):
    """ Exclude data outside of min and max year desired """
    min_year = int(config['postprocessing']['minimum_year'])
    if not min_year:
	min_year = 1980

    max_year = int(config['postprocessing']['maximum_year'])
    if not max_year:
	max_year = 2200

    year_image = image[0,:,:].astype(np.str).view(np.chararray).ljust(4)
    year_image = np.array(year_image).astype(np.float) 

    bad_indices = np.logical_or(year_image < min_year, year_image > max_year)
    for i in range(image.shape[0] - 1):
        image[i,:,:][bad_indices] = 0

    image[image.shape[0]-1,:,:][bad_indices] = before[bad_indices]

    return image 
Example 5
Project: vnpy_crypto   Author: birforce   File: test_defchararray.py    License: MIT License 6 votes vote down vote up
def test_slice(self):
        """Regression test for https://github.com/numpy/numpy/issues/5982"""

        arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
                       dtype='S4').view(np.chararray)
        sl1 = arr[:]
        assert_array_equal(sl1, arr)
        assert_(sl1.base is arr)
        assert_(sl1.base.base is arr.base)

        sl2 = arr[:, :]
        assert_array_equal(sl2, arr)
        assert_(sl2.base is arr)
        assert_(sl2.base.base is arr.base)

        assert_(arr[0, 0] == b'abc') 
Example 6
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        self.A = np.array([['abc ', '123  '],
                           ['789 ', 'xyz ']]).view(np.chararray)
        self.B = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        self.A = np.array('abc1', dtype='c').view(np.chararray) 
Example 8
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']]).view(np.chararray) 
Example 9
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        TestComparisons.setup(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray) 
Example 10
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        TestComparisons.setup(self)
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']], np.unicode_).view(np.chararray) 
Example 11
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        self.A = np.array([[' abc ', ''],
                           ['12345', 'MixedCase'],
                           ['123 \t 345 \0 ', 'UPPER']],
                          dtype='S').view(np.chararray)
        self.B = np.array([[u' \u03a3 ', u''],
                           [u'12345', u'MixedCase'],
                           [u'123 \t 345 \0 ', u'UPPER']]).view(np.chararray) 
Example 12
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def setup(self):
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray)
        self.B = np.array([['efg', '456'],
                           ['051', 'tuv']]).view(np.chararray) 
Example 13
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_add(self):
        AB = np.array([['abcefg', '123456'],
                       ['789051', 'xyztuv']]).view(np.chararray)
        assert_array_equal(AB, (self.A + self.B))
        assert_(len((self.A + self.B)[0][0]) == 6) 
Example 14
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_radd(self):
        QA = np.array([['qabc', 'q123'],
                       ['q789', 'qxyz']]).view(np.chararray)
        assert_array_equal(QA, ('q' + self.A)) 
Example 15
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_mul(self):
        A = self.A
        for r in (2, 3, 5, 7, 197):
            Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
                           [A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)

            assert_array_equal(Ar, (self.A * r))

        for ob in [object(), 'qrs']:
            with assert_raises_regex(ValueError,
                                     'Can only multiply by integers'):
                A*ob 
Example 16
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_mod(self):
        """Ticket #856"""
        F = np.array([['%d', '%f'], ['%s', '%r']]).view(np.chararray)
        C = np.array([[3, 7], [19, 1]])
        FC = np.array([['3', '7.000000'],
                       ['19', '1']]).view(np.chararray)
        assert_array_equal(FC, F % C)

        A = np.array([['%.3f', '%d'], ['%s', '%r']]).view(np.chararray)
        A1 = np.array([['1.000', '1'], ['1', '1']]).view(np.chararray)
        assert_array_equal(A1, (A % 1))

        A2 = np.array([['1.000', '2'], ['3', '4']]).view(np.chararray)
        assert_array_equal(A2, (A % [[1, 2], [3, 4]])) 
Example 17
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_rmod(self):
        assert_(("%s" % self.A) == str(self.A))
        assert_(("%r" % self.A) == repr(self.A))

        for ob in [42, object()]:
            with assert_raises_regex(
                    TypeError, "unsupported operand type.* and 'chararray'"):
                ob % self.A 
Example 18
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_empty_indexing():
    """Regression test for ticket 1948."""
    # Check that indexing a chararray with an empty list/array returns an
    # empty chararray instead of a chararray with a single empty string in it.
    s = np.chararray((4,))
    assert_(s[[]].size == 0) 
Example 19
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_chararray_rstrip(self):
        # Ticket #222
        x = np.chararray((1,), 5)
        x[0] = b'a   '
        x = x.rstrip()
        assert_equal(x[0], b'a') 
Example 20
Project: deepchem   Author: deepchem   File: utils.py    License: MIT License 5 votes vote down vote up
def get_sequence_strings(encoded_sequences):
  """
    Converts encoded sequences into an array with sequence strings
    """
  num_samples, _, _, seq_length = np.shape(encoded_sequences)
  sequence_characters = np.chararray((num_samples, seq_length))
  sequence_characters[:] = 'N'
  for i, letter in enumerate(['A', 'C', 'G', 'T']):
    letter_indxs = (encoded_sequences[:, :, i, :] == 1).squeeze()
    sequence_characters[letter_indxs] = letter
  # return 1D view of sequence characters
  return sequence_characters.view('S%s' % (seq_length)).ravel() 
Example 21
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        self.A = np.array([['abc ', '123  '],
                           ['789 ', 'xyz ']]).view(np.chararray)
        self.B = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray) 
Example 22
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        self.A = np.array('abc1', dtype='c').view(np.chararray) 
Example 23
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']]).view(np.chararray) 
Example 24
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        TestComparisons.setUp(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray) 
Example 25
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        TestComparisons.setUp(self)
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']], np.unicode_).view(np.chararray) 
Example 26
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        self.A = np.array([[' abc ', ''],
                           ['12345', 'MixedCase'],
                           ['123 \t 345 \0 ', 'UPPER']],
                          dtype='S').view(np.chararray)
        self.B = np.array([[sixu(' \u03a3 '), sixu('')],
                           [sixu('12345'), sixu('MixedCase')],
                           [sixu('123 \t 345 \0 '), sixu('UPPER')]]).view(np.chararray) 
Example 27
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        self.A = np.array([['abc', '123'],
                           ['789', 'xyz']]).view(np.chararray)
        self.B = np.array([['efg', '456'],
                           ['051', 'tuv']]).view(np.chararray) 
Example 28
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def test_add(self):
        AB = np.array([['abcefg', '123456'],
                       ['789051', 'xyztuv']]).view(np.chararray)
        assert_array_equal(AB, (self.A + self.B))
        assert_(len((self.A + self.B)[0][0]) == 6) 
Example 29
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def test_radd(self):
        QA = np.array([['qabc', 'q123'],
                       ['q789', 'qxyz']]).view(np.chararray)
        assert_array_equal(QA, ('q' + self.A)) 
Example 30
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    License: MIT License 5 votes vote down vote up
def test_mul(self):
        A = self.A
        for r in (2, 3, 5, 7, 197):
            Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
                           [A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)

            assert_array_equal(Ar, (self.A * r))

        for ob in [object(), 'qrs']:
            try:
                A * ob
            except ValueError:
                pass
            else:
                self.fail("chararray can only be multiplied by integers")