2014-05-19 22:23:29 +00:00
|
|
|
"""
|
2019-05-24 04:33:23 +00:00
|
|
|
CSV export test
|
2014-05-19 22:23:29 +00:00
|
|
|
"""
|
2015-07-17 17:31:14 +00:00
|
|
|
from __future__ import division, print_function, absolute_import
|
2014-05-19 22:23:29 +00:00
|
|
|
import pyqtgraph as pg
|
|
|
|
import csv
|
2015-07-17 17:31:14 +00:00
|
|
|
import tempfile
|
2014-05-19 22:23:29 +00:00
|
|
|
|
|
|
|
app = pg.mkQApp()
|
|
|
|
|
2015-07-17 17:31:14 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
def approxeq(a, b):
|
|
|
|
return (a-b) <= ((a + b) * 1e-6)
|
|
|
|
|
2015-07-13 18:14:46 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
def test_CSVExporter():
|
|
|
|
plt = pg.plot()
|
|
|
|
y1 = [1,3,2,3,1,6,9,8,4,2]
|
|
|
|
plt.plot(y=y1, name='myPlot')
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
y2 = [3,4,6,1,2,4,2,3,5,3,5,1,3]
|
|
|
|
x2 = pg.np.linspace(0, 1.0, len(y2))
|
|
|
|
plt.plot(x=x2, y=y2)
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
y3 = [1,5,2,3,4,6,1,2,4,2,3,5,3]
|
|
|
|
x3 = pg.np.linspace(0, 1.0, len(y3)+1)
|
Add "left" and "right" step Modes (#1360)
* Add "lstep" and "rstep" step Modes
stepMode is currently either True or False. If it is True,
it requires the user to make len(x) = len(y)+1. This is
inconvenient because it makes it difficult to change the
stepMode on a given curve (just as one would change, e.g.,
its color).
This commit extends the current situation by introducing
two more step modes: "lstep" and "rstep", which do not require
passing an extra x value. In turn, this modes associate each
y value to either the left or the right boundary of the step.
For example, the "rstep" mode is handy when plotting "life"
digital signals in which x,y data pairs are appended as they
are read.
This commit does not modify the behaviour in case of stepMode=True
* Replace step mode names: lstep,rstep -> left,right
* Improve docs for stepMode
Reword docstring and add it to PlotDataItem class too
* Document left and right stepModes as added in v 0.12.0
TODO: confirm the exact version number to use here
* Add comments stress the need for "is True"
Some conditional statements in the code regarding stepMode are
done with "is True". This is actually required since other
possible values such as "left" also evaluate as true but should
not be caught.
* Deprecate boolean API for stepMode
Introduce stepMode="mid" as a replacement of stepMode=True,
but keeping full backwards compatibility with the old API.
Adapt docs, examples and tests accordingly.
* Raise ValueError on unsupported stepMode values
* Rename "mid" step mode to "center"
* Remove "added in 0.12.0" note
See https://github.com/pyqtgraph/pyqtgraph/pull/1360#discussion_r502746919
* Add deprecation warning when stepMode=True
Issue a DeprecationWarning if stepMode=True is being passed to the
constructor or setData() of PlotDataItem or PlotCurveItem.
Note: warnings module is imported locally so that it is esier to
remove once this check is no longer needed.
* Fix wrong syntax in last commit
Fix usage of "default" kwarg in dict.get()
2020-10-13 15:52:07 +00:00
|
|
|
plt.plot(x=x3, y=y3, stepMode="center")
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
ex = pg.exporters.CSVExporter(plt.plotItem)
|
2021-05-29 04:15:11 +00:00
|
|
|
with tempfile.NamedTemporaryFile(mode="w+t", suffix='.csv', encoding="utf-8", delete=False) as tf:
|
|
|
|
print("using %s as a temporary file" % tf.name)
|
|
|
|
ex.export(fileName=tf.name)
|
|
|
|
lines = [line for line in csv.reader(tf)]
|
2014-05-19 22:23:29 +00:00
|
|
|
header = lines.pop(0)
|
2014-08-07 13:11:34 +00:00
|
|
|
assert header == ['myPlot_x', 'myPlot_y', 'x0001', 'y0001', 'x0002', 'y0002']
|
2021-05-28 14:39:00 +00:00
|
|
|
|
|
|
|
for i, vals in enumerate(lines):
|
2014-05-19 22:23:29 +00:00
|
|
|
vals = list(map(str.strip, vals))
|
2021-05-28 14:39:00 +00:00
|
|
|
assert (i >= len(y1) and vals[0] == '') or approxeq(float(vals[0]), i)
|
2014-05-19 22:23:29 +00:00
|
|
|
assert (i >= len(y1) and vals[1] == '') or approxeq(float(vals[1]), y1[i])
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
assert (i >= len(x2) and vals[2] == '') or approxeq(float(vals[2]), x2[i])
|
|
|
|
assert (i >= len(y2) and vals[3] == '') or approxeq(float(vals[3]), y2[i])
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-05-19 22:23:29 +00:00
|
|
|
assert (i >= len(x3) and vals[4] == '') or approxeq(float(vals[4]), x3[i])
|
|
|
|
assert (i >= len(y3) and vals[5] == '') or approxeq(float(vals[5]), y3[i])
|