Python numpy.chararray() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

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 2

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 3

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

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 5

def getHaploidIndividualSequence(self,msa,ind):
		"""
		Extract individuals "ind" sequence(s) from MSA dictionary
		------------------------------------------------------------------------
		Parameters:
		- msa: dictionary
		- ind: dict(indexREP,seqDEscription)
		Returns:
		- sequence of the individual
		"""
		# ind: [indID,indexREP,seqDescription]
		self.appLogger.debug("getHaploidIndividualSequence(self,msa,ind)")
		seqSize=len(msa["{0}_{1}".format(str(1),str(0))][str(0)]['sequence'])
		fullInd=None; speciesID=None; tipID=None; tmp=None
		fullInd=np.chararray(shape=(1,seqSize), itemsize=1)
		speciesID=ind["spID"].strip()
		locusID=ind["locID"].strip()
		tipID=ind["geneID"].strip()
		tmp=list(msa["{0}_{1}".format(str(speciesID), str(locusID))][str(tipID)]['sequence'])
		fullInd=[item for item in tmp]
		return fullInd 

Example 6

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 7

def lisa_sig_vals(pvals, quads, threshold):
    """
        Produce Moran's I classification based of n
    """

    sig = (pvals <= threshold)

    lisa_sig = np.empty(len(sig), np.chararray)

    for idx, val in enumerate(sig):
        if val:
            lisa_sig[idx] = map_quads(quads[idx])
        else:
            lisa_sig[idx] = 'Not significant'

    return lisa_sig 

Example 8

def lisa_sig_vals(pvals, quads, threshold):
    """
        Produce Moran's I classification based of n
    """

    sig = (pvals <= threshold)

    lisa_sig = np.empty(len(sig), np.chararray)

    for idx, val in enumerate(sig):
        if val:
            lisa_sig[idx] = map_quads(quads[idx])
        else:
            lisa_sig[idx] = 'Not significant'

    return lisa_sig 

Example 9

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 10

def converttochars(pixarray):
    #array of chars in increasing darnkess
    chars = [' ', '.','-','~','=','!',']','}','#','$','%','&','@',]
    procarray = numpy.chararray(pixarray.shape)
    k = 0
    for row in pixarray:
        j = 0
        for val in row:
            val = 255.0-val
            i =  ((len(chars)-1)*(val/255.0))
            i = int(round(i))
            procarray[k][j] = chars[i]
            j+=1
        k+=1
    return procarray


#get array of  pixel values 

Example 11

def teams_to_seat_arr(teams, seats_arr, allocated_seats):

    if isinstance(teams.values()[0], int):
        # plot the team dist
        teams_seats_arr = np.zeros(seats_arr.shape)
    else:
        teams_seats_arr = np.chararray(seats_arr.shape)

    for person, seat in allocated_seats.iteritems():
        # get location for the seat
        y, x = np.where(seats_arr == seat)
        # now get the team for the
        team = teams[person]
        teams_seats_arr[y, x] = team

    return teams_seats_arr 

Example 12

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 13

def test_chararray_rstrip(self,level=rlevel):
        # Ticket #222
        x = np.chararray((1,), 5)
        x[0] = asbytes('a   ')
        x = x.rstrip()
        assert_equal(x[0], asbytes('a')) 

Example 14

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 15

def setUp(self):
        self.A = np.array('abc1', dtype='c').view(np.chararray) 

Example 16

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 17

def setUp(self):
        TestComparisons.setUp(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray) 

Example 18

def setUp(self):
        self.A = np.array([[' abc ', ''],
                           ['12345', 'MixedCase'],
                           ['123 \t 345 \0 ', 'UPPER']]).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 19

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 20

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 21

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 22

def test_radd(self):
        QA = np.array([['qabc', 'q123'],
                       ['q789', 'qxyz']]).view(np.chararray)
        assert_array_equal(QA, ('q' + self.A)) 

Example 23

def test_rmul(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, (r * self.A))

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

Example 24

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 25

def test_rmod(self):
        assert_(("%s" % self.A) == str(self.A))
        assert_(("%r" % self.A) == repr(self.A))

        for ob in [42, object()]:
            try:
                ob % self.A
            except TypeError:
                pass
            else:
                self.fail("chararray __rmod__ should fail with "
                          "non-string objects") 

Example 26

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 27

def test_chararray_rstrip(self,level=rlevel):
        # Ticket #222
        x = np.chararray((1,), 5)
        x[0] = asbytes('a   ')
        x = x.rstrip()
        assert_equal(x[0], asbytes('a')) 

Example 28

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 29

def setUp(self):
        self.A = np.array('abc1', dtype='c').view(np.chararray) 

Example 30

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 31

def setUp(self):
        TestComparisons.setUp(self)
        self.B = np.array([['efg', '123  '],
                           ['051', 'tuv']], np.unicode_).view(np.chararray) 

Example 32

def setUp(self):
        self.A = np.array([[' abc ', ''],
                           ['12345', 'MixedCase'],
                           ['123 \t 345 \0 ', 'UPPER']]).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 33

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 34

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 35

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 36

def test_radd(self):
        QA = np.array([['qabc', 'q123'],
                       ['q789', 'qxyz']]).view(np.chararray)
        assert_array_equal(QA, ('q' + self.A)) 

Example 37

def test_rmul(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, (r * self.A))

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

Example 38

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 39

def test_rmod(self):
        assert_(("%s" % self.A) == str(self.A))
        assert_(("%r" % self.A) == repr(self.A))

        for ob in [42, object()]:
            try:
                ob % self.A
            except TypeError:
                pass
            else:
                self.fail("chararray __rmod__ should fail with "
                          "non-string objects") 

Example 40

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 41

def __init__(self, filename):
        self.filename = filename
        self.xyzfile = open(self.filename, 'r')
        self.offsets = []
        self.n_atoms = self._n_atoms()
        self.n_frames = self._n_frames()
        self.box_size = np.empty([self.n_frames, 3], dtype=np.float)
        self.atom_names = np.chararray([self.n_frames, self.n_atoms, 1], itemsize=3)
        self.coords = np.empty([self.n_frames, self.n_atoms, 3], dtype=np.float)
        self.read_all_frames() 

Example 42

def echantillonnage (fichier):

    data = np.genfromtxt(fichier,delimiter=',')
 
    clen, rlen = data.shape   
    new_tab=np.chararray([clen,rlen],itemsize=25)
 
    for c in range(0,rlen):
        petit=str(c)+'_1'
        moyen=str(c)+'_2'
        grand=str(c)+'_3'

        #----------------- sur le rang --------------------------
        data_sort=np.sort(data[:,c])
        x=len(data_sort)/3.
        tiers=data_sort[int(x)]
        deux_tiers=data_sort[2*int(x)]   
        #-----------------------------------------------------------        
        
        
        for l in range(0,clen):
            if data[l,c]<tiers:
                new_tab[l,c]=petit
                
            elif data[l,c]<deux_tiers:
                new_tab[l,c]=moyen
            else :
                new_tab[l,c]=grand
    
    return new_tab 

Example 43

def echantillonnage_glucose (fichier):

    data = np.genfromtxt(fichier,delimiter=',')  
    clen, rlen = data.shape   
    rlen=rlen+1
    diab= np.genfromtxt('glucose_a_traiter.csv',delimiter=',')
    new_tab=np.chararray([clen,rlen],itemsize=25)
 
    for c in range(0,rlen-1):
        petit=str(c)+'_1'
        moyen=str(c)+'_2'
        grand=str(c)+'_3'

        #----------------- sur le rang --------------------------
        data_sort=np.sort(data[:,c])
        x=len(data_sort)/3.
        tiers=data_sort[int(x)]
        deux_tiers=data_sort[2*int(x)]   
        #-----------------------------------------------------------        
        
        
        for l in range(0,clen):
            if data[l,c]<tiers:
                new_tab[l,c]=petit
                
            elif data[l,c]<deux_tiers:
                new_tab[l,c]=moyen
            else :
                new_tab[l,c]=grand
                
    for l in range (0,clen):        
        if diab[0,l]<6.5:            
            if diab[1,l]==0 and diab[2,l]==0 :
                new_tab[l,-1]=petit               
            else : 
                new_tab[l,-1]=moyen            
        else : new_tab[l,-1]=grand      
    
    return new_tab 

Example 44

def fill_matrix(matrix, width, value='n/a'):
    if matrix.shape[0] < width:
        nraters = matrix.shape[1]
        nas = np.chararray((1, nraters), itemsize=len(value))
        nas[:] = value
        matrix = np.vstack(tuple([matrix] + [nas] * (width - matrix.shape[0])))
    return matrix 

Example 45

def one_hot_encoding_sequences(seqs):
	CHARS = 'acgt'
	CHARS_COUNT = len(CHARS)

	maxlen = max(map(len, seqs))
	res = numpy.zeros((len(seqs), CHARS_COUNT * maxlen), dtype=numpy.uint8)

	for si, seq in enumerate(seqs):
	    seqlen = len(seq)
	    arr = numpy.chararray((seqlen,), buffer=seq)
	    for ii, char in enumerate(CHARS):
	        res[si][ii*seqlen:(ii+1)*seqlen][arr == char] = 1

	return res 

Example 46

def one_hot_encoding_sequences(seqs, sequenceLength):
	"""
	input: genome sequences
	output: one_hot encoded sequence array
	"""

	le = preprocessing.LabelEncoder()

	one_hot_sequences = []

	le.fit_transform(['a','t','g','c'])
	for si, seq in enumerate(seqs):
	    seqlen = len(seq)
	    arr = np.chararray((seqlen,), buffer=seq)
	    a = le.transform(arr)
	    
	    a = np.array(a)
	    
	    b = np.zeros((len(a), 4))
	    b[np.arange(len(a)), a] = 1

	    #b = np.array(b)
	    b = b.transpose()

	    if b.shape[1] == sequenceLength:
	    	one_hot_sequences.append(b)

	return one_hot_sequences

#print one_hot_encoding_sequences(['atgctgc','gctatgc'])

#print numpy.arange(8).reshape((4,8/4), order = 'F') 

Example 47

def write_kmers(kmers, filename):
    char_kmers = np.chararray(kmers.shape)
    for _char, _int in six.iteritems(ALPHABET):
        char_kmers[kmers == _int] = _char

    with open(filename, 'w') as fh:
        for i, kmer in enumerate(char_kmers):
            print('>%d' % i, file=fh)
            print(kmer.tostring().decode(), file=fh) 

Example 48

def test_chararray_rstrip(self,level=rlevel):
        # Ticket #222
        x = np.chararray((1,), 5)
        x[0] = asbytes('a   ')
        x = x.rstrip()
        assert_equal(x[0], asbytes('a')) 

Example 49

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 50

def setUp(self):
        self.A = np.array('abc1', dtype='c').view(np.chararray) 
点赞