Commit a4503b57 authored by Yori Fournier's avatar Yori Fournier
Browse files

add the tutorials for mytool + add tests + integrated to CI.

parent 726c491f
Pipeline #460 failed with stage
in 4 minutes and 10 seconds
......@@ -17,6 +17,14 @@ myplotlib_import_local_latex_tkagg:
- python -m tests
- ./clean_test_folder.sh
myplotlib_test_mytool:
script:
- git name-rev --name-only HEAD
- ls -l
- cd mytool
- python -m tests
- python -m tutorials
myplotlib_import_network_latex_tkagg:
script:
- git name-rev --name-only HEAD
......
......@@ -140,9 +140,10 @@ elif D_HIERARCHY in ('LOCAL', 'local'):
# MyFig: Overlay on matplotlib.Figure class
from .myFig import MyFig
from .mplGrid import Grid, VerticalGrid, HorizontalGrid
from .empty_fig import EmptyFig
from .empty_axes import EmptyAxes
from .mplGrid import Grid, VerticalGrid, HorizontalGrid
else:
......
from myplotlib import MyFig, VerticalGrid
from myplotlib import MyFig
class EmptyFig(MyFig):
def __init__(self, **kwargs):
vgrid = VerticalGrid()
self.setLayout(vgrid)
MyFig.__init__(self, (), **kwargs)
def addAxes(self):
......
# IMPORT ---------------------------------------------------------------
from .. import MyAxes
from .. import MyAxes, SEVR, INFO
from . import FormatterListToLine
from itertools import repeat
......@@ -14,12 +14,12 @@ D_YLABEL = None
D_LOGY = False
D_LOGX = False
D_COLORS = []
D_MARKERS = []
D_LINESTYLE = []
D_COLORS = repeat(None)
D_MARKERS = repeat('')
D_LINESTYLES = repeat('-')
D_LEGEND = False
D_DATA_LABELS = []
D_LABELS = []
D_LEGEND_POS = None
......@@ -38,9 +38,9 @@ class Ax2dPlots(MyAxes):
'ylabel': D_YLABEL,
'formatter': D_FORMATTER,
'colors': D_COLORS,
'marker': D_MARKERS,
'linestyle': D_LINESTYLE,
'data_labels': D_DATA_LABELS,
'markers': D_MARKERS,
'linestyles': D_LINESTYLES,
'labels': D_LABELS,
'legend_pos': D_LEGEND_POS}
return(True)
......@@ -58,17 +58,33 @@ class Ax2dPlots(MyAxes):
def plotting(self):
# GET ALL PARAMETERS
xRange = self.keywords.get('xRange')
yRange = self.keywords.get('yRange')
xlim = self.keywords.get('xlim')
ylim = self.keywords.get('ylim')
logx = self.keywords.get('logx')
logy = self.keywords.get('logy')
lPos = self.keywords.get('lpos', 'br')
xlabel = self.data.get('xlabel')
ylabel = self.data.get('ylabel')
labels = self.data.get('labels', repeat(None))
colors = self.keywords.get('colors')
markers = self.keywords.get('markers')
linestyles = self.keywords.get('linestyles')
legend = self.keywords.get('legend', None)
legend_pos = self.keywords.get('legend_pos', 'br')
xlabel = self.keywords.get('xlabel')
if xlabel is None:
xlabel = self.data.get('xlabel', None)
ylabel = self.keywords.get('ylabel')
if ylabel is None:
ylabel = self.data.get('ylabel', None)
labels = self.keywords.get('labels')
if labels == []:
labels = self.data.get('labels', repeat(None))
xdata = self.data.get('xdata')
ydatas = self.data.get('ydatas')
# CHECK IF THE DATA EXISTS
if (xdata is None):
print(SEVR + "I need axes.data['xdata'] to plot... got None")
......@@ -80,29 +96,30 @@ class Ax2dPlots(MyAxes):
# TRY TO PLOT
try:
# IF XLIN - YLIN
if (not logx) and (not logy):
for ydata, label in zip(ydatas, labels):
self.plot(xdata, ydata, label=label)
for ydata, label, color, marker, linestyle in zip(ydatas, labels, colors, markers, linestyles):
self.plot(xdata, ydata, label=label, color=color, marker=marker, linestyle=linestyle)
# XLIN - YLOG
elif (not logx) and logy:
for ydata, label in zip(ydatas, labels):
self.semilogy(xdata, ydata, label=label)
for ydata, label, color, marker, linestyle in zip(ydatas, labels, colors, markers, linestyles):
self.semilogy(xdata, ydata, label=label, color=color, marker=marker, linestyle=linestyle)
# XLOG - YLIN
elif (not logy) and logx:
for ydata, label in zip(ydatas, labels):
self.semilogx(xdata, ydata, label=label)
for ydata, label, color, marker, linestyle in zip(ydatas, labels, colors, markers, linestyles):
self.semilogx(xdata, ydata, label=label, color=color, marker=marker, linestyle=linestyle)
# XLOG - YLOG
else:
for ydata, label in zip(ydatas, labels):
self.loglog(xdata, ydata, label=label)
except:
for ydata, label, color, marker, linestyle in zip(ydatas, labels, colors, markers, linestyles):
self.loglog(xdata, ydata, label=label, color=color, marker=marker, linestyle=linestyle)
except Exception as e:
print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
print(INFO + str(e))
return(False)
# SET THE LABELS
......@@ -110,20 +127,20 @@ class Ax2dPlots(MyAxes):
if (ylabel): self.set_ylabel(ylabel)
# SET THE RANGES
if (xRange): self.set_xlim(xRange)
if (yRange): self.set_ylim(yRange)
if lPos == 'ur':
self.legend(bbox_to_anchor=(0.97, 0.97), loc=1, borderpad=0.5, prop={'size': 14})
elif lPos == 'ul':
self.legend(bbox_to_anchor=(0.03, 0.97), loc=2, borderpad=0.5, prop={'size': 14})
elif lPos == 'br':
self.legend(bbox_to_anchor=(0.97, 0.03), loc=4, borderpad=0.5, prop={'size': 14})
elif lPos == 'bl':
self.legend(bbox_to_anchor=(0.03, 0.03), loc=3, borderpad=0.5, prop={'size': 14})
else:
print(WARN + "I could not understand the legen positioning (lPos) should be ['ur', 'ul', 'br', 'bl']. I set it to 'ul'")
self.legend(bbox_to_anchor=(0.03, 0.97), loc=2, borderpad=0.5, prop={'size': 14})
if (xlim): self.set_xlim(xlim)
if (ylim): self.set_ylim(ylim)
if legend:
if legend_pos == 'ur':
self.legend(bbox_to_anchor=(0.97, 0.97), loc=1, borderpad=0.5, prop={'size': 14})
elif legend_pos == 'ul':
self.legend(bbox_to_anchor=(0.03, 0.97), loc=2, borderpad=0.5, prop={'size': 14})
elif legend_pos == 'br':
self.legend(bbox_to_anchor=(0.97, 0.03), loc=4, borderpad=0.5, prop={'size': 14})
elif legend_pos == 'bl':
self.legend(bbox_to_anchor=(0.03, 0.03), loc=3, borderpad=0.5, prop={'size': 14})
else:
print(WARN + "I could not understand the legen positioning (legend_pos) should be ['ur', 'ul', 'br', 'bl']. I set it to 'ul'")
self.legend(bbox_to_anchor=(0.03, 0.97), loc=2, borderpad=0.5, prop={'size': 14})
return(True)
......@@ -37,8 +37,8 @@ class FormatterArrayToLines(Formatter):
def shape(self, rawdata):
if self.xdataIndex is not None:
xdata = rawdata.data.pop(self.xdataIndex)
ydatas = rawdata.data
ydatas = rawdata.data[:]
xdata = ydatas.pop(self.xdataIndex)
else:
ydatas = rawdata.data
xdata = range(0, len(ydatas[0])) # generic xdata
......
import unittest
from test_tutorials import TutorialsTestCase
from __init__ import *
verbose = 0
failed = []
states = {}
# List To Line Formatter
suite_tutorials = unittest.TestLoader().loadTestsFromTestCase(TutorialsTestCase)
states.update({'Tutorials': unittest.TextTestRunner(verbosity=verbose).run(suite_tutorials).wasSuccessful()})
for success in states.items():
if not success[1]:
failed.append(success[0])
if len(failed):
raise(Exception('Failing tests suites: '+str(failed)))
./tuto000.py
./tuto001.py
./tuto002.py
./tuto003.py
#!/usr/bin/env python
import unittest
from tuto000 import script as sc000
from tuto001 import script as sc001
from tuto002 import script as sc002
from tuto003 import script as sc003
class TutorialsTestCase(unittest.TestCase):
def setUp(self):
pass
def test_tuto_000(self):
fig = sc000()
fig.plot()
# Test the curves
self.assertEqual(fig.axes[0].lines[0].get_data()[0].tolist(), [0., 1., 2., 3., 4., 5., 6., 7., 8.])
self.assertEqual(fig.axes[0].lines[0].get_data()[1].tolist(), [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2])
def test_tuto_001(self):
fig = sc001()
fig.plot()
# Test the curves
self.assertEqual(fig.axes[0].lines[0].get_data()[0].tolist(), [0.1, 0.3, 1.0, 3.0, 10., 30., 100., 300., 1000.])
self.assertEqual(fig.axes[0].lines[0].get_data()[1].tolist(), [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2])
# Test the formatter
self.assertEqual(fig.axes[0].keywords['formatter'].__class__.__name__, 'FormatterArrayToLines')
# Test the x/y labels
self.assertEqual(fig.axes[0].get_xlabel(), r'x-axis')
self.assertEqual(fig.axes[0].get_ylabel(), r'y-axis')
# Test the log ticks
self.assertEqual(fig.axes[0].xaxis.get_major_locator().__class__.__name__, 'LogLocator')
def test_tuto_002(self):
fig = sc002()
fig.plot()
# Test the curves
self.assertEqual(fig.axes[0].lines[0].get_data()[0].tolist(), [0.1, 0.3, 1.0, 3.0, 10., 30., 100., 300., 1000.])
self.assertEqual(fig.axes[0].lines[0].get_data()[1].tolist(), [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2])
self.assertEqual(fig.axes[0].lines[1].get_data()[1].tolist(), [0.1, 0.22, 0.44, 0.65, 1.05, 0.96, 1.27, 1.34, 1.28])
self.assertEqual(fig.axes[0].lines[2].get_data()[1].tolist(), [0.12, 0.21, 0.41, 0.59, 1.01, 0.88, 1.17, 1.31, 1.22])
# Test the limits
self.assertAlmostEqual(fig.axes[0].get_xlim()[0], 0.3)
self.assertAlmostEqual(fig.axes[0].get_xlim()[1], 300.)
# Test the legend
self.assertEqual(fig.axes[0].lines[0].get_label(), r'original')
self.assertEqual(fig.axes[0].lines[1].get_label(), r'theoretical')
self.assertEqual(fig.axes[0].lines[2].get_label(), r'data')
self.assertEqual(fig.axes[0].get_legend()._loc_real, 4)
def test_tuto_003(self):
fig = sc003()
fig.plot()
# Test the curves
self.assertEqual(fig.axes[0].lines[0].get_data()[0].tolist(), [0.1, 0.3, 1.0, 3.0, 10., 30., 100., 300., 1000.])
self.assertEqual(fig.axes[0].lines[0].get_data()[1].tolist(), [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2])
self.assertEqual(fig.axes[0].lines[1].get_data()[1].tolist(), [0.12, 0.22, 0.44, 0.65, 1.05, 0.96, 1.27, 1.34, 1.28])
self.assertEqual(fig.axes[0].lines[2].get_data()[1].tolist(), [0.1, 0.21, 0.41, 0.59, 1.01, 0.88, 1.17, 1.31, 1.22])
# Test the styles
self.assertEqual(fig.axes[0].lines[0].get_marker(), '^')
self.assertEqual(fig.axes[0].lines[1].get_marker(), '')
self.assertEqual(fig.axes[0].lines[2].get_marker(), 'o')
self.assertEqual(fig.axes[0].lines[0].get_color(), 'r')
self.assertEqual(fig.axes[0].lines[1].get_color(), 'g')
self.assertEqual(fig.axes[0].lines[2].get_color(), 'b')
self.assertEqual(fig.axes[0].lines[0].get_linestyle(), '--')
self.assertEqual(fig.axes[0].lines[1].get_linestyle(), '-')
self.assertEqual(fig.axes[0].lines[2].get_linestyle(), u'None')
if __name__ == '__main__':
unittest.main()
#!/usr/bin/env python
import sys
def script(with_print=False):
sys.path.append('../../')
import sys
from myplotlib.mytool import Ax2dPlots
from myplotlib import EmptyFig, MyData
sys.path.append('../../')
data = [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2]
from myplotlib.mytool import Ax2dPlots
from myplotlib import EmptyFig, MyData
rawdata = MyData(data=data, name='just-a-list')
data = [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2]
fig = EmptyFig()
fig.layout.appendAxes(Ax2dPlot(fig), 'ax1', rawdata)
rawdata = MyData(data=data, name='just-a-list')
fig.print2file('./tuto000.png')
fig = EmptyFig()
fig.add_axes(Ax2dPlots(fig), 'ax1')
fig.set_rawdata((rawdata,))
if with_print:
fig.print2file('./tuto000.png')
return(fig)
if __name__ == '__main__':
script(True)
#!/usr/bin/env python
def script(with_print=False):
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots, FormatterArrayToLines
from myplotlib import EmptyFig, MyData, VerticalGrid
# rawdata set
data = [[0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2],
[0.1, 0.3, 1.0, 3.0, 10., 30., 100., 300., 1000.]]
# pack into MyData
rawdata = MyData(data=data, name='just-a-list')
# Create an empty figure
fig = EmptyFig()
# Add a grid with margin and padding for the titles
vgrid = VerticalGrid(margin=0.15, padding=0.)
fig.setLayout(vgrid)
# Create a formatter for the rawdata set (set the second list as x-axis)
formatter = FormatterArrayToLines(xdataIndex=1)
# Create an Ax2dPlot
ax = Ax2dPlots(fig, xlabel=r'x-axis', ylabel=r'y-axis', formatter=formatter, logx=True)
# Add the axes into the grid
fig.layout.appendAxes(ax, 'ax1', rawdata)
# Layout the grid
fig.layout.layout()
if with_print:
# save as file
fig.print2file('./tuto001.png')
return(fig)
if __name__ == '__main__':
script(True)
#!/usr/bin/env python
def script(with_print=False):
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots, FormatterArrayToLines
from myplotlib import EmptyFig, MyData, VerticalGrid
# rawdata set
data = [[0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2],
[0.1, 0.22, 0.44, 0.65, 1.05, 0.96, 1.27, 1.34, 1.28],
[0.12, 0.21, 0.41, 0.59, 1.01, 0.88, 1.17, 1.31, 1.22],
[0.1, 0.3, 1.0, 3.0, 10., 30., 100., 300., 1000.]]
# pack into MyData
rawdata = MyData(data=data, name='just-a-list')
# Create an empty figure
fig = EmptyFig()
# Add a grid with margin and padding for the titles
vgrid = VerticalGrid(margin=0.15, padding=0.)
fig.setLayout(vgrid)
# Create a formatter for the rawdata set (set the second list as x-axis)
formatter = FormatterArrayToLines(xdataIndex=-1)
# Create an Ax2dPlot
ax = Ax2dPlots(fig, xlabel=r'x-axis', ylabel=r'y-axis', formatter=formatter, logx=True, xlim=[0.3, 300.], legend=True, legend_pos='br', labels=[r'original', r'theoretical', r'data'])
# Add the axes into the grid
fig.layout.appendAxes(ax, 'ax1', rawdata)
# Layout the grid
fig.layout.layout()
if with_print:
# save as file
fig.print2file('./tuto002.png')
return(fig)
if __name__ == '__main__':
script(True)
#!/usr/bin/env python
def script(with_print=False):
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots, FormatterAppendToLines
from myplotlib import EmptyFig, MyData, VerticalGrid
# rawdata set
data = [[0.1, 0.1, 0.12, 0.1],
[0.3, 0.2, 0.22, 0.21],
[1.0, 0.4, 0.44, 0.41],
[3.0, 0.6, 0.65, 0.59],
[10., 1.0, 1.05, 1.01],
[30., 0.9, 0.96, 0.88],
[100., 1.2, 1.27, 1.17],
[300., 1.3, 1.34, 1.31],
[1000., 1.2, 1.28, 1.22]]
# pack into MyData
rawdata = MyData(data=data, name='just-a-list')
# Create an empty figure
fig = EmptyFig()
# Add a grid with margin and padding for the titles
vgrid = VerticalGrid(margin=0.15, padding=0.)
fig.setLayout(vgrid)
# Create a formatter for the rawdata set (set the second list as x-axis)
formatter = FormatterAppendToLines(xdataIndex=0)
# Create an Ax2dPlot
ax = Ax2dPlots(fig,
xlabel=r'x-axis',
ylabel=r'y-axis',
formatter=formatter,
logx=True,
xlim=[0.03, 3000.],
legend=True,
legend_pos='br',
labels=[r'original', r'theoretical', r'data'],
markers=['^', '', 'o'],
colors=['r', 'g', 'b'],
linestyles=['--', '-', ''])
# Add the axes into the grid
fig.layout.appendAxes(ax, 'ax1', rawdata)
# Layout the grid
fig.layout.layout()
if with_print:
# save as file
fig.print2file('./tuto003.png')
return(fig)
if __name__ == '__main__':
script(True)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment