Run update-copyright.py
[igor.git] / test / test-igorpy.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of %(project)s.
4 #
5 # %(project)s is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # %(project)s is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with %(project)s.  If not, see <http://www.gnu.org/licenses/>.
17
18 r"""Test the igor.igorpy compatibility layer by loading sample files.
19
20 >>> from pprint import pprint
21 >>> import igor.igorpy as igor
22 >>> igor.ENCODING = 'UTF-8'
23
24 Load a packed experiment:
25
26 >>> path = data_path('polar-graphs-demo.pxp')
27 >>> d = igor.load(path)
28 >>> print(d)
29 <igor.Folder root>
30 >>> dir(d)  # doctest: +ELLIPSIS
31 ['Packages', 'W_plrX5', 'W_plrX6', ..., 'radiusData', 'radiusQ1']
32
33
34 Navigation:
35
36 >>> print(d.Packages)
37 <igor.Folder root/Packages>
38 >>> print(d[0])  # doctest: +ELLIPSIS
39 <igor.igorpy.Variables object at 0x...>
40
41
42 Variables:
43
44 >>> v = d[0]
45 >>> dir(v)  # doctest: +ELLIPSIS
46 ['__class__', ..., 'depstr', 'depvar', 'format', 'sysvar', 'userstr', 'uservar']
47 >>> v.depstr
48 {}
49 >>> v.depvar
50 {}
51 >>> v.format()
52 '<Variables: system 21, user 0, dependent 0>'
53 >>> pprint(v.sysvar)  # doctest: +REPORT_UDIFF
54 {'K0': 0.0,
55  'K1': 0.0,
56  'K10': 0.0,
57  'K11': 0.0,
58  'K12': 0.0,
59  'K13': 0.0,
60  'K14': 0.0,
61  'K15': 0.0,
62  'K16': 0.0,
63  'K17': 0.0,
64  'K18': 0.0,
65  'K19': 0.0,
66  'K2': 0.0,
67  'K20': 128.0,
68  'K3': 0.0,
69  'K4': 0.0,
70  'K5': 0.0,
71  'K6': 0.0,
72  'K7': 0.0,
73  'K8': 0.0,
74  'K9': 0.0}
75 >>> v.userstr
76 {}
77 >>> v.uservar
78 {}
79
80
81 Waves:
82
83 >>> d.W_plrX5
84 <igor.Wave W_plrX5 data (128)>
85 >>> dir(d.W_plrX5)  # doctest: +ELLIPSIS
86 ['__array__', ..., 'axis', 'axis_units', 'data', ..., 'name', 'notes']
87 >>> d.W_plrX5.axis  # doctest: +ELLIPSIS
88 [array([ 0.04908739,  0.04870087,  0.04831436,  0.04792784,  0.04754133,
89         0.04715481,  0.0467683 ,  0.04638178,  0.04599527,  0.04560875,
90         ...
91         0.00077303,  0.00038651,  0.        ]), array([], dtype=float64), array([], dtype=float64), array([], dtype=float64)]
92 >>> d.W_plrX5.data_units
93 (u'', '', '', '')
94 >>> d.W_plrX5.axis_units
95 (u'', '', '', '')
96 >>> d.W_plrX5.data  # doctest: +ELLIPSIS
97 array([  1.83690956e-17,   2.69450769e-02,   7.65399113e-02,
98          1.44305170e-01,   2.23293692e-01,   3.04783821e-01,
99          ...
100         -2.72719120e-03,   5.24539061e-08], dtype=float32)
101
102
103 Dump the whole thing:
104
105 >>> print(d.format())
106 root
107   <Variables: system 21, user 0, dependent 0>
108   <History>
109   radiusData data (128)
110   angleData data (128)
111   W_plrX5 data (128)
112   W_plrY5 data (128)
113   angleQ1 data (64)
114   radiusQ1 data (64)
115   W_plrX6 data (64)
116   W_plrY6 data (64)
117   Packages
118     WMDataBase
119       <Variables: system 21, user 6, dependent 0>
120     PolarGraphs
121       <Variables: system 21, user 38, dependent 0>
122   <Recreation>
123   <GetHistory>
124   <Procedure>
125
126
127 Load a packed experiment without ignoring unknown records:
128
129 >>> d = igor.load(path, ignore_unknown=False)
130 >>> print(d.format())
131 root
132   <Unknown type 11>
133   <Unknown type 12>
134   <Unknown type 13>
135   <Unknown type 13>
136   <Unknown type 13>
137   <Unknown type 13>
138   <Unknown type 13>
139   <Unknown type 13>
140   <Unknown type 13>
141   <Unknown type 14>
142   <Unknown type 15>
143   <Unknown type 16>
144   <Unknown type 16>
145   <Unknown type 17>
146   <Unknown type 17>
147   <Unknown type 17>
148   <Unknown type 17>
149   <Unknown type 17>
150   <Unknown type 17>
151   <Unknown type 16>
152   <Unknown type 17>
153   <Unknown type 17>
154   <Unknown type 17>
155   <Unknown type 17>
156   <Unknown type 17>
157   <Unknown type 17>
158   <Unknown type 18>
159   <Unknown type 11>
160   <Unknown type 26>
161   <Unknown type 26>
162   <Variables: system 21, user 0, dependent 0>
163   <History>
164   radiusData data (128)
165   angleData data (128)
166   W_plrX5 data (128)
167   W_plrY5 data (128)
168   angleQ1 data (64)
169   radiusQ1 data (64)
170   W_plrX6 data (64)
171   W_plrY6 data (64)
172   Packages
173     WMDataBase
174       <Variables: system 21, user 6, dependent 0>
175     PolarGraphs
176       <Variables: system 21, user 38, dependent 0>
177   <Recreation>
178   <GetHistory>
179   <Procedure>
180
181
182 Try to load a binary wave:
183
184 >>> path = data_path('mac-double.ibw')
185 >>> d = igor.load(path)
186 Traceback (most recent call last):
187    ...
188 IOError: final record too long; bad pxp file?
189 """
190
191 import os.path
192
193 from igor import LOG
194
195
196 _this_dir = os.path.dirname(__file__)
197 _data_dir = os.path.join(_this_dir, 'data')
198
199 def data_path(filename):
200     LOG.info('Testing igorpy compatibility {}\n'.format(filename))
201     path = os.path.join(_data_dir, filename)
202     return path