1f88927e358cddc6efcd18e4abf1821471cbd7c5
[igor.git] / test / test-igorpy.py
1 # Copyright
2
3 r"""Test the igor.igorpy compatibility layer by loading sample files.
4
5 >>> from pprint import pprint
6 >>> import igor.igorpy as igor
7 >>> igor.ENCODING = 'UTF-8'
8
9 Load a packed experiment:
10
11 >>> path = data_path('polar-graphs-demo.pxp')
12 >>> d = igor.load(path)
13 >>> print(d)
14 <igor.Folder root>
15 >>> dir(d)  # doctest: +ELLIPSIS
16 ['Packages', 'W_plrX5', 'W_plrX6', ..., 'radiusData', 'radiusQ1']
17
18
19 Navigation:
20
21 >>> print(d.Packages)
22 <igor.Folder root/Packages>
23 >>> print(d[0])  # doctest: +ELLIPSIS
24 <igor.igorpy.Variables object at 0x...>
25
26
27 Variables:
28
29 >>> v = d[0]
30 >>> dir(v)  # doctest: +ELLIPSIS
31 ['__class__', ..., 'depstr', 'depvar', 'format', 'sysvar', 'userstr', 'uservar']
32 >>> v.depstr
33 {}
34 >>> v.depvar
35 {}
36 >>> v.format()
37 '<Variables: system 21, user 0, dependent 0>'
38 >>> pprint(v.sysvar)  # doctest: +REPORT_UDIFF
39 {'K0': 0.0,
40  'K1': 0.0,
41  'K10': 0.0,
42  'K11': 0.0,
43  'K12': 0.0,
44  'K13': 0.0,
45  'K14': 0.0,
46  'K15': 0.0,
47  'K16': 0.0,
48  'K17': 0.0,
49  'K18': 0.0,
50  'K19': 0.0,
51  'K2': 0.0,
52  'K20': 128.0,
53  'K3': 0.0,
54  'K4': 0.0,
55  'K5': 0.0,
56  'K6': 0.0,
57  'K7': 0.0,
58  'K8': 0.0,
59  'K9': 0.0}
60 >>> v.userstr
61 {}
62 >>> v.uservar
63 {}
64
65
66 Waves:
67
68 >>> d.W_plrX5
69 <igor.Wave W_plrX5 data (128)>
70 >>> dir(d.W_plrX5)  # doctest: +ELLIPSIS
71 ['__array__', ..., 'axis', 'axis_units', 'data', ..., 'name', 'notes']
72 >>> d.W_plrX5.axis  # doctest: +ELLIPSIS
73 [array([ 0.04908739,  0.04870087,  0.04831436,  0.04792784,  0.04754133,
74         0.04715481,  0.0467683 ,  0.04638178,  0.04599527,  0.04560875,
75         ...
76         0.00077303,  0.00038651,  0.        ]), array([], dtype=float64), array([], dtype=float64), array([], dtype=float64)]
77 >>> d.W_plrX5.data_units
78 (u'', '', '', '')
79 >>> d.W_plrX5.axis_units
80 (u'', '', '', '')
81 >>> d.W_plrX5.data  # doctest: +ELLIPSIS
82 array([  1.83690956e-17,   2.69450769e-02,   7.65399113e-02,
83          1.44305170e-01,   2.23293692e-01,   3.04783821e-01,
84          ...
85         -2.72719120e-03,   5.24539061e-08], dtype=float32)
86
87
88 Dump the whole thing:
89
90 >>> print(d.format())
91 root
92   <Variables: system 21, user 0, dependent 0>
93   <History>
94   radiusData data (128)
95   angleData data (128)
96   W_plrX5 data (128)
97   W_plrY5 data (128)
98   angleQ1 data (64)
99   radiusQ1 data (64)
100   W_plrX6 data (64)
101   W_plrY6 data (64)
102   Packages
103     WMDataBase
104       <Variables: system 21, user 6, dependent 0>
105     PolarGraphs
106       <Variables: system 21, user 38, dependent 0>
107   <Recreation>
108   <GetHistory>
109   <Procedure>
110
111
112 Load a packed experiment without ignoring unknown records:
113
114 >>> d = igor.load(path, ignore_unknown=False)
115 >>> print(d.format())
116 root
117   <Unknown type 11>
118   <Unknown type 12>
119   <Unknown type 13>
120   <Unknown type 13>
121   <Unknown type 13>
122   <Unknown type 13>
123   <Unknown type 13>
124   <Unknown type 13>
125   <Unknown type 13>
126   <Unknown type 14>
127   <Unknown type 15>
128   <Unknown type 16>
129   <Unknown type 16>
130   <Unknown type 17>
131   <Unknown type 17>
132   <Unknown type 17>
133   <Unknown type 17>
134   <Unknown type 17>
135   <Unknown type 17>
136   <Unknown type 16>
137   <Unknown type 17>
138   <Unknown type 17>
139   <Unknown type 17>
140   <Unknown type 17>
141   <Unknown type 17>
142   <Unknown type 17>
143   <Unknown type 18>
144   <Unknown type 11>
145   <Unknown type 26>
146   <Unknown type 26>
147   <Variables: system 21, user 0, dependent 0>
148   <History>
149   radiusData data (128)
150   angleData data (128)
151   W_plrX5 data (128)
152   W_plrY5 data (128)
153   angleQ1 data (64)
154   radiusQ1 data (64)
155   W_plrX6 data (64)
156   W_plrY6 data (64)
157   Packages
158     WMDataBase
159       <Variables: system 21, user 6, dependent 0>
160     PolarGraphs
161       <Variables: system 21, user 38, dependent 0>
162   <Recreation>
163   <GetHistory>
164   <Procedure>
165
166
167 Try to load a binary wave:
168
169 >>> path = data_path('mac-double.ibw')
170 >>> d = igor.load(path)
171 Traceback (most recent call last):
172    ...
173 IOError: final record too long; bad pxp file?
174 """
175
176 import os.path
177
178 from igor import LOG
179
180
181 _this_dir = os.path.dirname(__file__)
182 _data_dir = os.path.join(_this_dir, 'data')
183
184 def data_path(filename):
185     LOG.info('Testing igorpy compatibility {}\n'.format(filename))
186     path = os.path.join(_data_dir, filename)
187     return path