File: test_dictarray.rst

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (52 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
>>> import numpy
>>> from cogent import DNA
>>> from cogent.util.dict_array import DictArrayTemplate
>>> a = numpy.identity(3, int)
>>> b = DictArrayTemplate('abc', 'ABC').wrap(a)
>>> b[0]
===========
A    B    C
-----------
1    0    0
-----------
>>> b['a']
===========
A    B    C
-----------
1    0    0
-----------
>>> b.keys()
['a', 'b', 'c']
>>> row = b['a']
>>> row.keys()
['A', 'B', 'C']
>>> list(row)
[1, 0, 0]
>>> sum(row)
1
>>> # Dimensions can also be ordinay integers
>>> b = DictArrayTemplate(3, 3).wrap(a)
>>> b.keys()
[0, 1, 2]
>>> b[0].keys()
[0, 1, 2]
>>> sum(b[0])
1
>>> # Or a mix
>>> b = DictArrayTemplate('ABC', 3).wrap(a)
>>> b.keys()
['A', 'B', 'C']
>>> b['A'].keys()
[0, 1, 2]

``DictArray`` should work properly in ``numpy`` operations.

>>> darr = DictArrayTemplate(list(DNA), list(DNA)).wrap([[.7,.1,.1,.1],
...                                                      [.1,.7,.1,.1],
...                                                      [.1,.1,.7,.1],
...                                                      [.1,.1,.1,.7]])
>>> mprobs = numpy.array([0.25, 0.25, 0.25, 0.25])
>>> print mprobs.dot(darr)
[ 0.25  0.25  0.25  0.25]
>>> print numpy.dot(mprobs, darr)
[ 0.25  0.25  0.25  0.25]