Commit 726c491f authored by Yori Fournier's avatar Yori Fournier
Browse files

add formatters and ax2dPlots into mytool

made the tests on the formatters

made tuto000 but it requires the grids!

changed emptyfig to have a default grid but need grids
parent cdb2c470
Pipeline #459 failed with stage
in 3 minutes
from myplotlib import MyFig
from myplotlib import MyFig, VerticalGrid
class EmptyFig(MyFig):
def __init__(self, **kwargs):
vgrid = VerticalGrid()
self.setLayout(vgrid)
MyFig.__init__(self, (), **kwargs)
def addAxes(self):
......
......@@ -12,5 +12,11 @@ from .myTool import closeAllWindows
from .myTool import closeWindow
from .figOneAxes import FigOneAxes
from .formatter import FormatterListToLine
from .formatter import FormatterArrayToLines
from .formatter import FormatterAppendToLines
from .ax2dPlots import Ax2dPlots
from .axPlot2D import AxPlot2D
from .axPlots2D import AxPlots2D
# IMPORT ---------------------------------------------------------------
from .. import MyAxes
from . import FormatterListToLine
from itertools import repeat
D_FORMATTER = FormatterListToLine()
D_XLIM = None
D_YLIM = None
D_XLABEL = None
D_YLABEL = None
D_LOGY = False
D_LOGX = False
D_COLORS = []
D_MARKERS = []
D_LINESTYLE = []
D_LEGEND = False
D_DATA_LABELS = []
D_LEGEND_POS = None
# Class Ax2dPlots overwriting MyAxes
class Ax2dPlots(MyAxes):
# DECLARE KEYWORDS --------------------------------------------------
def declareKeywords(self):
self.keywords = {'xlim': D_XLIM,
'ylim': D_YLIM,
'logx': D_LOGX,
'logy': D_LOGY,
'legend': D_LEGEND,
'xlabel': D_XLABEL,
'ylabel': D_YLABEL,
'formatter': D_FORMATTER,
'colors': D_COLORS,
'marker': D_MARKERS,
'linestyle': D_LINESTYLE,
'data_labels': D_DATA_LABELS,
'legend_pos': D_LEGEND_POS}
return(True)
# DEFAULT FORMAT RAWDAT ... to be overwritten ------------------------
def formatRawData(self, rawdata):
formatter = self.keywords.get('formatter', FormatterListToLine())
self.data = formatter.shape(rawdata)
return(True)
# PLOTTING -----------------------------------------------------------
# the plotting function (need to be overwrite from child
def plotting(self):
# GET ALL PARAMETERS
xRange = self.keywords.get('xRange')
yRange = self.keywords.get('yRange')
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))
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")
return(False)
if (ydatas is None):
print(SEVR + "I need axes.data['ydatas'] to plot... got None")
return(False)
# 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)
# XLIN - YLOG
elif (not logx) and logy:
for ydata, label in zip(ydatas, labels):
self.semilogy(xdata, ydata, label=label)
# XLOG - YLIN
elif (not logy) and logx:
for ydata, label in zip(ydatas, labels):
self.semilogx(xdata, ydata, label=label)
# XLOG - YLOG
else:
for ydata, label in zip(ydatas, labels):
self.loglog(xdata, ydata, label=label)
except:
print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
return(False)
# SET THE LABELS
if (xlabel): self.set_xlabel(xlabel)
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})
return(True)
class Formatter(object):
def __init__(self):
pass
def shape(self, rawdata):
pass
class FormatterListToLine(Formatter):
def __init__(self, xlabel=None, ylabel=None):
self.xlabel = xlabel
self.ylabel = ylabel
def shape(self, rawdata):
ydata = rawdata.data
xdata = range(0, len(ydata))
data = {'ydatas': [ydata],
'xdata': xdata,
'xlabel': self.xlabel,
'ylabel': self.ylabel,
'name': rawdata.name}
return(data)
class FormatterArrayToLines(Formatter):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None):
self.xlabel = xlabel
self.ylabel = ylabel
self.xdataIndex = xdataIndex
def shape(self, rawdata):
if self.xdataIndex is not None:
xdata = rawdata.data.pop(self.xdataIndex)
ydatas = rawdata.data
else:
ydatas = rawdata.data
xdata = range(0, len(ydatas[0])) # generic xdata
data = {'ydatas': ydatas,
'xdata': xdata,
'xlabel': self.xlabel,
'ylabel': self.ylabel,
'name': rawdata.name}
return(data)
class FormatterAppendToLines(Formatter):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None):
self.xlabel = xlabel
self.ylabel = ylabel
self.xdataIndex = xdataIndex
def shape(self, rawdata):
ydatas = []
xdata = []
for item in rawdata.data[0]:
ydatas.append([item])
for line in rawdata.data[1:]:
for i in range(0, len(line)):
ydatas[i].append(line[i])
if self.xdataIndex is not None:
xdata = ydatas.pop(self.xdataIndex)
else:
xdata = range(0, len(ydatas[0])) # generic xdata
data = {'ydatas': ydatas,
'xdata': xdata,
'xlabel': self.xlabel,
'ylabel': self.ylabel,
'name': rawdata.name}
return(data)
import unittest
from test_formatter import FormatterListToLineTestCase
from test_formatter import FormatterArrayToLinesTestCase
from test_formatter import FormatterAppendToLinesTestCase
from __init__ import *
verbose = 0
failed = []
states = {}
# List To Line Formatter
suite_list_to_line = unittest.TestLoader().loadTestsFromTestCase(FormatterListToLineTestCase)
states.update({'FormatterListToLine': unittest.TextTestRunner(verbosity=verbose).run(suite_list_to_line).wasSuccessful()})
# Array to lines Formatter
suite_array_to_lines = unittest.TestLoader().loadTestsFromTestCase(FormatterArrayToLinesTestCase)
states.update({'FormatterArrayToLines': unittest.TextTestRunner(verbosity=verbose).run(suite_array_to_lines).wasSuccessful()})
# Append to lines Formatter
suite_append_to_lines = unittest.TestLoader().loadTestsFromTestCase(FormatterAppendToLinesTestCase)
states.update({'FormatterAppendToLines': unittest.TextTestRunner(verbosity=verbose).run(suite_append_to_lines).wasSuccessful()})
for success in states.items():
if not success[1]:
failed.append(success[0])
if len(failed):
raise(Exception('Failing tests suites: '+str(failed)))
#!/usr/bin/env python
import os, sys
import unittest
sys.path.append('../../')
from myplotlib import MyData
from myplotlib.mytool import FormatterListToLine, FormatterArrayToLines, FormatterAppendToLines
class FormatterListToLineTestCase(unittest.TestCase):
def setUp(self):
self.data_line = MyData(data=[0., 1., 2., 3., 4., 5.,],
name='data_line')
def test_formatter_list_to_line(self):
formatter = FormatterListToLine()
data = formatter.shape(self.data_line)
self.assertIsNone(formatter.xlabel)
self.assertIsNone(formatter.ylabel)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.,]],
'xdata': [0., 1., 2., 3., 4., 5.,],
'xlabel': None,
'ylabel': None,
'name': 'data_line'}
self.assertEqual(data, correctdata)
def test_formatter_list_to_line_with_kw(self):
formatter = FormatterListToLine(xlabel=r'x-axis', ylabel=r'y-axis')
data = formatter.shape(self.data_line)
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.]],
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': r'x-axis',
'ylabel': r'y-axis',
'name': 'data_line'}
self.assertEqual(data, correctdata)
class FormatterArrayToLinesTestCase(unittest.TestCase):
def setUp(self):
self.data_lines = MyData(data=[[0., 1., 2., 3., 4., 5.],
[0., 2., 4., 6., 8., 10.],
[0., 3., 6., 9., 12., 15.]],
name='data_lines')
def test_formatter_array_to_lines(self):
formatter = FormatterArrayToLines()
data = formatter.shape(self.data_lines)
self.assertIsNone(formatter.xlabel)
self.assertIsNone(formatter.ylabel)
self.assertIsNone(formatter.xdataIndex)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.],
[0., 2., 4., 6., 8., 10.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': None,
'ylabel': None,
'name': 'data_lines'}
self.assertEqual(data, correctdata)
def test_formatter_array_to_lines_with_kw(self):
formatter = FormatterArrayToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdataIndex=1)
data = formatter.shape(self.data_lines)
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
self.assertEqual(formatter.xdataIndex, 1)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 2., 4., 6., 8., 10.],
'xlabel': r'x-axis',
'ylabel': r'y-axis',
'name': 'data_lines'}
self.assertEqual(data, correctdata)
class FormatterAppendToLinesTestCase(unittest.TestCase):
def setUp(self):
self.data_cols = MyData(data=[[0., 0., 0.],
[1., 2., 3.],
[2., 4., 6.],
[3., 6., 9.],
[4., 8., 12.],
[5., 10., 15.]],
name='data_cols')
def test_formatter_append_to_lines(self):
formatter = FormatterAppendToLines()
data = formatter.shape(self.data_cols)
self.assertIsNone(formatter.xlabel)
self.assertIsNone(formatter.ylabel)
self.assertIsNone(formatter.xdataIndex)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.,],
[0., 2., 4., 6., 8., 10.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': None,
'ylabel': None,
'name': 'data_cols'}
self.assertEqual(data, correctdata)
def test_formatter_array_to_lines_with_kw(self):
formatter = FormatterAppendToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdataIndex=1)
data = formatter.shape(self.data_cols)
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
self.assertEqual(formatter.xdataIndex, 1)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 2., 4., 6., 8., 10.],
'xlabel': r'x-axis',
'ylabel': r'y-axis',
'name': 'data_cols'}
self.assertEqual(data, correctdata)
if __name__ == '__main__':
unittest.main()
#!/usr/bin/env python
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots
from myplotlib import EmptyFig, MyData
data = [0.1, 0.2, 0.4, 0.6, 1., 0.9, 1.2, 1.3, 1.2]
rawdata = MyData(data=data, name='just-a-list')
fig = EmptyFig()
fig.layout.appendAxes(Ax2dPlot(fig), 'ax1', rawdata)
fig.print2file('./tuto000.png')
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