1
2
3
4
5 """Choose and import using numpy, Numeric, or numarray modules.
6
7 Sets the following module variables accordingly:
8
9 N = All numpy/Numeric/numarray functions, usage e.g.
10 N.arange(4).
11 MA = All numpy/Numeric/numarray masked array functions,
12 usage e.g. MA.masked_array(data).
13 MLab = All numpy/Numeric/numarray Matlab compatibility
14 functions, usage e.g. MLab.squeeze(data). For numpy,
15 (it appears) many of these function are in the regular
16 core library, so MLab for numpy is just numpy.
17 isscalar = Function to emulate numpy's isscalar function.
18 typecode = Function to emulate Numeric's typecode() method
19 for Numeric objects.
20 typecodes = Dictionary, with additions, that builds off of a
21 copy of the N.typecodes dictionary.
22
23 Thus, by importing using this package you will be able to code
24 to a generic naming convention, regardless of what array package
25 is used. The module tries importing numpy first, then numarray,
26 and finally Numeric. The later packages are imported only if
27 the earlier ones return an ImportError. Currently, if you have
28 numpy installed, MA and MLab will not be defined, since aliases
29 for the numpy versions of those packages have not yet been
30 programmed into this module.
31 """
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 import os, sys
62 if (__name__ == "__main__") or \
63 ("pydoc" in os.path.basename(sys.argv[0])):
64 import user
65 del os, sys
66
67
68
69
70 import package_version as _package_version
71 __version__ = _package_version.version
72 __author__ = _package_version.author
73 __date__ = _package_version.date
74 __credits__ = _package_version.credits
75
76
77
78
79
80
81
82
83
84 try:
85 import numpy as N
86 try:
87 import numpy.core.ma as MA
88 except ImportError:
89 import numpy.ma as MA
90 import numpy as MLab
92 """Return typecode of arg if arg was a numpy array.
93
94 Input argument:
95 * arg: Any argument that can be read converted by numpy into
96 an array.
97 """
98 return N.array(arg).dtype.char
99
100 except ImportError:
101 try:
102 import numarray
103 import numarray.ma.MA as MA
104 import numarray.linear_algebra.mlab as MLab
105 import numarray as N
106 del numarray
108 """Return typecode of arg if arg was a numarray array.
109
110 Input argument:
111 * arg: Any argument that can be read converted by Numeric
112 into an array.
113 """
114 return N.array(arg).typecode()
116 raise ValueError, 'isscalar not yet implemented for numarray'
117
118 except ImportError:
119 try:
120 import Numeric as N
121 import MA
122 import MLab
124 """Return typecode of arg if arg was a Numeric array.
125
126 Input argument:
127 * arg: Any argument that can be read converted by Numeric
128 into an array.
129 """
130 return N.array(arg).typecode()
132 raise ValueError, 'isscalar not yet implemented for Numeric'
133
134 except:
135 raise ImportError, 'No array packages found'
136
137
138
139
140 typecodes = N.typecodes
141 if 'S' not in typecodes['Character']:
142 typecodes['Character'] = typecodes['Character'] + 'S'
143 if 'c' not in typecodes['Character']:
144 typecodes['Character'] = typecodes['Character'] + 'c'
145
146
147
148
149
150
151 if __name__ == "__main__":
152 """Test the module documentation strings."""
153 import doctest, sys, os
154 sys.path.append(os.pardir)
155 doctest.testmod(sys.modules[__name__])
156
157
158
159
160
161