1
2 import empro as _empro
3 from empro import graphing as _graphing
4 from empro.toolkit.dataset import _isDataSet
5
7 '''
8 makeXYGraph(dataset1, dataset2, ..., **kwargs) -> L{empro.graphing.Graph}
9 makeXYGraph(L{DataSetMatrix}, ..., **kwargs) -> L{empro.graphing.Graph}
10 makeXYGraph(L{CitiFile}, ..., **kwargs) -> L{empro.graphing.Graph}
11
12 returns a XY graph of one or more datasets, dataset matrice or citi files.
13
14 Optional keyword arguments are:
15 - graph: if specified the plot is added to the graph, else a new graph is created
16 - title: string to be displayed as title of graph
17 - names: sequence of dataset names to be used in legend.
18 - xUnit, yUnit: unit per axis
19 - xBounds, yBounds: (min, max) bounds per axis.
20 - xLabel, yLabel: caption per axis.
21 '''
22 datasets = _sanitizeDataSets(datasets)
23
24 n = len(datasets)
25 dimensions = [d.dimension(0) for d in datasets if d.numberOfDimensions() > 0]
26 names = (kwargs.get('names', []) + [None] * n)[:n]
27 optionalPlotInfos = (kwargs.get('optionalPlotInfos', []) + [None] * n)[:n]
28 try:
29 graph = kwargs.get('graph')
30 _setLinearAxis(graph, 'xAxis', dimensions, kwargs.get('xUnit'), kwargs.get('xBounds'), kwargs.get('xLabel'))
31 except:
32 graph = _graphing.XYGraph()
33 _setLinearAxis(graph, 'xAxis', dimensions, kwargs.get('xUnit'), kwargs.get('xBounds'), kwargs.get('xLabel'))
34 _setLinearAxis(graph, 'yAxis', datasets, kwargs.get('yUnit'), kwargs.get('yBounds'), kwargs.get('yLabel'))
35 isAbsSParam = _determineResultType(datasets) == 'SParameters' and all([ds.query.complexPart != 'Phase' for ds in datasets if _hasQuery(ds)])
36 isYLog = isAbsSParam or graph.yAxis.unit.logScale() in ('Amplitude_dB', 'Power_dB')
37 for dataset, name, optionalPlotInfo in zip(datasets, names, optionalPlotInfos):
38 _addPlot(_graphing.XYPlot, 'xyData', graph, dataset, name, isYLog, optionalPlotInfo)
39 if isAbsSParam:
40 if not 'yUnit' in kwargs or kwargs['yUnit'] == 'SCALAR':
41 graph.yAxis.setUnit(_empro.units.unitByAbbreviation("dBa"))
42 if not 'yBounds' in kwargs:
43 graph.yAxis.autoBounding = 'Lower'
44 graph.yAxis.setBounds((0, 1))
45 if not 'yLabel' in kwargs:
46 graph.yAxis.setTitle('| S |')
47 graph.title = 'S-parameters'
48 if 'title' in kwargs:
49 graph.title = kwargs['title']
50 graph.name = graph.title
51 return graph
52
53
55 '''
56 showXYGraph(*args, **kwargs) -> L{empro.graphing.Graph}
57
58 Equivalent to:
59 graph = makeXYGraph(*args, **kwargs)
60 return showGraph(graph)
61 '''
62 return showGraph(makeXYGraph(*args, **kwargs))
63
64
66 '''
67 makeSmithGraph(dataset1, dataset2, ..., **kwargs) -> L{empro.graphing.Graph}
68 '''
69 datasets = _sanitizeDataSets(datasets)
70 assert all([d.numberOfDimensions() == 1 for d in datasets]), "All datasets must be one-dimensional."
71 n = len(datasets)
72 dimensions = [d.dimension(0) for d in datasets]
73 names = (kwargs.get('names', []) + [None] * n)[:n]
74 graph = _graphing.SmithGraph()
75
76
77
78 for dataset, name in zip(datasets, names):
79 plot = _graphing.SmithPlot()
80 plot.realData = dataset.real
81 plot.imaginaryData = dataset.imag
82 plot.name = name or dataset.name
83 graph.addPlot(plot)
84 if 'title' in kwargs:
85 graph.title = kwargs['title']
86 graph.name = graph.title
87 return graph
88
89
91 '''
92 showSmithGraph(*args, **kwargs) -> L{empro.graphing.Graph}
93
94 Equivalent to:
95 graph = makeSmithGraph(*args, **kwargs)
96 return showGraph(graph)
97 '''
98 return showGraph(makeSmithGraph(*args, **kwargs))
99
100
102 datasets = _sanitizeDataSets(datasets)
103 assert all([d.numberOfDimensions() == 1 for d in datasets]), "All datasets must be one-dimensional."
104 n = len(datasets)
105 dimensions = [d.dimension(0) for d in datasets]
106 names = (kwargs.get('names', []) + [None] * n)[:n]
107 graph = _graphing.PolarGraph()
108 _setLinearAxis(graph, 'radialAxis', datasets, kwargs.get('radialUnit'), kwargs.get('radialBounds'), kwargs.get('radialLabel'))
109 isAbsSParam = _determineResultType(datasets) == 'SParameters' and all([ds.query.complexPart != 'Phase' for ds in datasets if _hasQuery(ds)])
110 isYLog = isAbsSParam or graph.radialAxis.unit.logScale() in ('Amplitude_dB', 'Power_dB')
111 for dataset, name in zip(datasets, names):
112 _addPlot(_graphing.PolarPlot, 'polarData', graph, dataset, name, isYLog)
113 if 'title' in kwargs:
114 graph.title = kwargs['title']
115 graph.name = graph.title
116 return graph
117
118
120 '''
121 showPolarGraph(*args, **kwargs) -> L{empro.graphing.Graph}
122
123 Equivalent to:
124 graph = makePolarGraph(*args, **kwargs)
125 return showGraph(graph)
126 '''
127 return showGraph(makePolarGraph(*args, **kwargs))
128
129
131 '''
132 showGraph(graph) -> L{empro.graphing.Graph}
133
134 Add graph to the active project's graphs list and return a reference to it.
135 The return object is not necessarily the same one as the argument as a copy
136 can be made.
137 '''
138 graphs = _empro.activeProject.graphs()
139 index = graphs.size()
140 graphs.insert(index, graph)
141 return graphs.at(index)
142
143
144
145
147 try:
148 dataset.query
149 except AttributeError:
150 return False
151 return True
152
153
155 datasets = []
156 for arg in args:
157
158 try:
159 values = [arg[key] for key in arg.keys()]
160 except AttributeError:
161 pass
162 else:
163 datasets += _sanitizeDataSets(values)
164 continue
165
166 try:
167 isSequenceOfDataSets = any(_isDataSet(x) for x in arg)
168 except:
169 pass
170 else:
171 if isSequenceOfDataSets:
172 datasets += _sanitizeDataSets(arg)
173 continue
174
175 assert _isDataSet(arg), "%r is neither a dataset, a sequence of datasets or a mapping of datasets" % arg
176 try:
177 arg.numberOfDimensions()
178 except AttributeError:
179 raise
180
181
182
183
184
185
186
187 datasets.append(arg)
188 return datasets
189
190
191 -def _setLinearAxis(graph, attr, datasets, defaultUnit, bounds, label=None):
192 unit = _determineUnit(datasets, defaultUnit)
193 if not unit:
194 return
195 axis = _graphing.LinearGraphAxis()
196 axis.setUnit(unit)
197 if bounds:
198 axis.autoBounding = 'None'
199 axis.setBounds(bounds)
200 if label:
201 axis.setTitle(label)
202 graph.__setattr__(attr, axis)
203
204
216 if isinstance(default, basestring):
217 try:
218 default = _empro.units.unitByAbbreviation(default)
219 except KeyError:
220 pass
221 return reduce(op, datasets, default)
222
223
240 return reduce(op, datasets, None)
241
242
243 -def _addPlot(PlotType, attr, graph, dataset, name=None, isYLog=False, optionalPlotInfo=None):
244 assert _isDataSet(dataset), dataset
245 name = name or dataset.name
246 try:
247 real, imag = dataset.real, dataset.imag
248 except AttributeError:
249 plot = PlotType()
250 setattr(plot, attr, dataset)
251 plot.name = name or dataset.name
252 if optionalPlotInfo:
253 for valueKey in optionalPlotInfo:
254 key = valueKey[0]
255 value = valueKey[1]
256 plot.setUserInfo(key, value)
257 graph.addPlot(plot)
258 else:
259 if isYLog:
260 _addPlot(PlotType, attr, graph, abs(dataset), ("| %s |" % name, None)[name is None], optionalPlotInfo=optionalPlotInfo)
261 else:
262 _addPlot(PlotType, attr, graph, real, ("Re( %s )" % name, None)[name is None], optionalPlotInfo=optionalPlotInfo)
263 _addPlot(PlotType, attr, graph, imag, ("Im( %s )" % name, None)[name is None], optionalPlotInfo=optionalPlotInfo)
264