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

Now mytool tests and tutorials works

parent 6cad3c49
from .myTool import usage
from .myTool import print2file
from .myTool import print2screen
from .myTool import getCurrentWindowIDs
from .myTool import getFigOnWindow
from .myTool import getWindow
from .myTool import printListCurrentWindows
from .myTool import window_exists
from .myTool import giveDataToWindow
from .myTool import drawFigOnWindow
from .myTool import closeAllWindows
from .myTool import closeWindow
from .mpl_tools import usage
from .mpl_tools import print2file
from .mpl_tools import print2screen
from .mpl_tools import getCurrentWindowIDs
from .mpl_tools import getFigOnWindow
from .mpl_tools import getWindow
from .mpl_tools import printListCurrentWindows
from .mpl_tools import window_exists
from .mpl_tools import giveDataToWindow
from .mpl_tools import drawFigOnWindow
from .mpl_tools import closeAllWindows
from .mpl_tools import closeWindow
from .figOneAxes import FigOneAxes
# from .figOneAxes import FigOneAxes
from .formatter import Formatter
from .formatter import FormatterListToLine
from .formatter import FormatterArrayToLines
from .formatter import FormatterAppendToLines
from .mplWeightedGrid import WeightedGrid
from .mplWeightedGrid import VerticalWeightedGrid
from .mplWeightedGrid import HorizontalWeightedGrid
from .mpl_weighted_grid import WeightedGrid
from .mpl_weighted_grid import VerticalWeightedGrid
from .mpl_weighted_grid import HorizontalWeightedGrid
from .ax2dPlots import Ax2dPlots
from .ax_2d_plots import Ax2dPlots
# IMPORT ---------------------------------------------------------------
from .. import MyAxes, SEVR, INFO, WARN, SPCE
from .. import MplAxes, SEVR, INFO, WARN, SPCE
from . import FormatterListToLine
from itertools import repeat
......@@ -24,10 +24,10 @@ D_LEGEND_POS = None
# Class Ax2dPlots overwriting MyAxes
class Ax2dPlots(MyAxes):
class Ax2dPlots(MplAxes):
# DECLARE KEYWORDS --------------------------------------------------
def declareKeywords(self):
def declare_keywords(self):
self.keywords = {'xlim': D_XLIM,
'ylim': D_YLIM,
......@@ -46,10 +46,10 @@ class Ax2dPlots(MyAxes):
return(True)
# DEFAULT FORMAT RAWDAT ... to be overwritten ------------------------
def formatRawData(self, rawdata):
def format_rawdata(self, rawdata):
formatter = self.keywords.get('formatter', FormatterListToLine())
formatter.setAxes(self)
formatter.set_axes(self)
self.data = formatter.shape(rawdata)
return(True)
......
......@@ -8,7 +8,7 @@ class Formatter(object):
def shape(self, rawdata):
pass
def setAxes(self, axes):
def set_axes(self, axes):
self.axes = axes
class FormatterListToLine(Formatter):
......@@ -35,19 +35,19 @@ class FormatterListToLine(Formatter):
class FormatterArrayToLines(Formatter):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None):
def __init__(self, xdata_index=None, xlabel=None, ylabel=None):
Formatter.__init__(self)
self.xlabel = xlabel
self.ylabel = ylabel
self.xdataIndex = xdataIndex
self.xdata_index = xdata_index
def shape(self, rawdata):
if self.xdataIndex is not None:
if self.xdata_index is not None:
ydatas = rawdata.data[:]
xdata = ydatas.pop(self.xdataIndex)
xdata = ydatas.pop(self.xdata_index)
else:
ydatas = rawdata.data
xdata = range(0, len(ydatas[0])) # generic xdata
......@@ -62,10 +62,10 @@ class FormatterArrayToLines(Formatter):
class FormatterAppendToLines(Formatter):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None, with_label=False):
def __init__(self, xdata_index=None, xlabel=None, ylabel=None, with_label=False):
self.xlabel = xlabel
self.ylabel = ylabel
self.xdataIndex = xdataIndex
self.xdata_index = xdata_index
self.with_label = with_label
def shape(self, rawdata):
......@@ -75,26 +75,26 @@ class FormatterAppendToLines(Formatter):
if self.with_label:
labels = rawdata.data[0]
iindex = 1
i_index = 1
else:
iindex = 0
i_index = 0
labels = []
for item in rawdata.data[iindex]:
for item in rawdata.data[i_index]:
ydatas.append([item])
for line in rawdata.data[iindex+1:]:
for line in rawdata.data[i_index+1:]:
for i in range(0, len(line)):
ydatas[i].append(line[i])
if self.xdataIndex is not None:
xdata = ydatas.pop(self.xdataIndex)
if self.xdata_index is not None:
xdata = ydatas.pop(self.xdata_index)
if labels != []:
if self.xlabel is None:
self.xlabel = labels.pop(self.xdataIndex)
self.xlabel = labels.pop(self.xdata_index)
else:
labels.pop(self.xdataIndex)
labels.pop(self.xdata_index)
else:
labels = repeat(None)
else:
......
......@@ -19,8 +19,8 @@ class WeightedGrid(Grid):
else:
self.weights = weights
def appendAxes(self, ax, axlabel, rawdata, weight=None, weights=None):
Grid.appendAxes(self, ax, axlabel, rawdata)
def append_axes(self, ax, ax_label, rawdata, weight=None, weights=None):
Grid.append_axes(self, ax, ax_label, rawdata)
if weights is not None:
self.weights = weights
......@@ -30,8 +30,8 @@ class WeightedGrid(Grid):
else:
self.weights.append(1.)
def appendGrid(self, grid, weight=None, weights=None):
Grid.appendGrid(self, grid)
def append_grid(self, grid, weight=None, weights=None):
Grid.append_grid(self, grid)
if weights is not None:
self.weights = weights
......@@ -41,10 +41,10 @@ class WeightedGrid(Grid):
else:
self.weights.append(1.)
def getItemWeight(self, index):
def get_item_weight(self, index):
return(self.weights[index])
def getWeights(self):
def get_weights(self):
return(self.weights)
class VerticalWeightedGrid(WeightedGrid):
......
......@@ -5,14 +5,14 @@ import unittest
sys.path.append('../../')
from myplotlib import MyData
from myplotlib import MplData
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')
self.data_line = MplData(data=[0., 1., 2., 3., 4., 5.,],
name='data_line')
def test_formatter_list_to_line(self):
formatter = FormatterListToLine()
......@@ -21,13 +21,13 @@ class FormatterListToLineTestCase(unittest.TestCase):
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'}
correct_data = {'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)
self.assertEqual(data, correct_data)
def test_formatter_list_to_line_with_kw(self):
formatter = FormatterListToLine(xlabel=r'x-axis', ylabel=r'y-axis')
......@@ -36,21 +36,21 @@ class FormatterListToLineTestCase(unittest.TestCase):
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'}
correct_data = {'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)
self.assertEqual(data, correct_data)
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')
self.data_lines = MplData(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()
......@@ -58,9 +58,9 @@ class FormatterArrayToLinesTestCase(unittest.TestCase):
self.assertIsNone(formatter.xlabel)
self.assertIsNone(formatter.ylabel)
self.assertIsNone(formatter.xdataIndex)
self.assertIsNone(formatter.xdata_index)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.],
correct_data = {'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.],
......@@ -68,44 +68,44 @@ class FormatterArrayToLinesTestCase(unittest.TestCase):
'ylabel': None,
'name': 'data_lines'}
self.assertEqual(data, correctdata)
self.assertEqual(data, correct_data)
def test_formatter_array_to_lines_with_kw(self):
formatter = FormatterArrayToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdataIndex=1)
formatter = FormatterArrayToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdata_index=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(formatter.xdata_index, 1)
correct_data = {'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)
self.assertEqual(data, correct_data)
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')
self.data_labeled_cols = MyData(data=[['y0', 'y1', 'y2'],
[0., 0., 0.],
[1., 2., 3.],
[2., 4., 6.],
[3., 6., 9.],
[4., 8., 12.],
[5., 10., 15.]],
name='data_labeled_cols')
self.data_cols = MplData(data=[[0., 0., 0.],
[1., 2., 3.],
[2., 4., 6.],
[3., 6., 9.],
[4., 8., 12.],
[5., 10., 15.]],
name='data_cols')
self.data_labeled_cols = MplData(data=[['y0', 'y1', 'y2'],
[0., 0., 0.],
[1., 2., 3.],
[2., 4., 6.],
[3., 6., 9.],
[4., 8., 12.],
[5., 10., 15.]],
name='data_labeled_cols')
def test_formatter_append_to_lines(self):
formatter = FormatterAppendToLines()
......@@ -113,55 +113,55 @@ class FormatterAppendToLinesTestCase(unittest.TestCase):
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,
'labels': [],
'name': 'data_cols'}
self.assertIsNone(formatter.xdata_index)
correct_data = {'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,
'labels': [],
'name': 'data_cols'}
self.assertEqual(data, correctdata)
self.assertEqual(data, correct_data)
def test_formatter_append_to_lines_with_kw(self):
formatter = FormatterAppendToLines(with_label=True, xdataIndex=0)
formatter = FormatterAppendToLines(with_label=True, xdata_index=0)
data = formatter.shape(self.data_labeled_cols)
self.assertEqual(formatter.xlabel, 'y0')
self.assertIsNone(formatter.ylabel)
self.assertEqual(formatter.xdataIndex, 0)
self.assertEqual(formatter.xdata_index, 0)
self.assertTrue(formatter.with_label)
correctdata = {'ydatas': [[0., 2., 4., 6., 8., 10.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': 'y0',
'ylabel': None,
'labels': ['y1', 'y2'],
'name': 'data_labeled_cols'}
correct_data = {'ydatas': [[0., 2., 4., 6., 8., 10.],
[0., 3., 6., 9., 12., 15.]],
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': 'y0',
'ylabel': None,
'labels': ['y1', 'y2'],
'name': 'data_labeled_cols'}
self.assertEqual(data, correctdata)
self.assertEqual(data, correct_data)
def test_formatter_append_to_lines_with_kw_2(self):
formatter = FormatterAppendToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdataIndex=1, with_label=True)
formatter = FormatterAppendToLines(xlabel=r'x-axis', ylabel=r'y-axis', xdata_index=1, with_label=True)
data = formatter.shape(self.data_labeled_cols)
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
self.assertEqual(formatter.xdataIndex, 1)
self.assertEqual(formatter.xdata_index, 1)
correctdata = {'ydatas': [[0., 1., 2., 3., 4., 5.],
correct_data = {'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',
'labels': ['y0', 'y2'],
'name': 'data_labeled_cols'}
'xdata': [0., 2., 4., 6., 8., 10.],
'xlabel': r'x-axis',
'ylabel': r'y-axis',
'labels': ['y0', 'y2'],
'name': 'data_labeled_cols'}
self.assertEqual(data, correctdata)
self.assertEqual(data, correct_data)
def test_formatter_append_to_lines_with_kw_3(self):
formatter = FormatterAppendToLines(xlabel=r'x-axis', ylabel=r'y-axis', with_label=True)
......@@ -169,18 +169,18 @@ class FormatterAppendToLinesTestCase(unittest.TestCase):
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
self.assertIsNone(formatter.xdataIndex)
self.assertIsNone(formatter.xdata_index)
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': r'x-axis',
'ylabel': r'y-axis',
'labels': ['y0', 'y1', 'y2'],
'name': 'data_labeled_cols'}
correct_data = {'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': r'x-axis',
'ylabel': r'y-axis',
'labels': ['y0', 'y1', 'y2'],
'name': 'data_labeled_cols'}
self.assertEqual(data, correctdata)
self.assertEqual(data, correct_data)
if __name__ == '__main__':
unittest.main()
......@@ -5,18 +5,18 @@ import unittest
sys.path.append('../../')
from myplotlib import EmptyFig, MyData
from myplotlib import EmptyFig, MplData
from myplotlib.mytool import WeightedGrid, Ax2dPlots
class WeightedGridTestCase(unittest.TestCase):
def setUp(self):
self.rawdata = MyData(data=[-1., 1.])
self.fooFig = EmptyFig()
self.fooGrid = WeightedGrid()
self.fooGrid2 = WeightedGrid()
self.fooAxes = Ax2dPlots(self.fooFig)
self.fooAxes2 = Ax2dPlots(self.fooFig)
self.rawdata = MplData(data=[-1., 1.])
self.foo_fig = EmptyFig()
self.foo_grid = WeightedGrid()
self.foo_grid2 = WeightedGrid()
self.foo_axes = Ax2dPlots(self.foo_fig)
self.foo_axes2 = Ax2dPlots(self.foo_fig)
def test_default_constructor(self):
wgrid = WeightedGrid()
......@@ -28,107 +28,107 @@ class WeightedGridTestCase(unittest.TestCase):
def test_append_axes(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendAxes(self.fooAxes, 'fooAxes', self.rawdata)
self.foo_fig.set_grid(wgrid)
wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata)
self.assertEqual(wgrid.weights, [1.])
wgrid.appendAxes(self.fooAxes2, 'fooAxes2', self.rawdata)
wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata)
self.assertEqual(wgrid.weights, [1., 1.])
def test_append_axes_with_weight(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendAxes(self.fooAxes, 'fooAxes', self.rawdata, weight=0.6)
self.foo_fig.set_grid(wgrid)
wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata, weight=0.6)
self.assertEqual(wgrid.weights, [0.6])
wgrid.appendAxes(self.fooAxes2, 'fooAxes2', self.rawdata, weight=0.4)
wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata, weight=0.4)
self.assertEqual(wgrid.weights, [0.6, 0.4])
def test_append_axes_with_weights(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendAxes(self.fooAxes, 'fooAxes', self.rawdata)
self.foo_fig.set_grid(wgrid)
wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata)
self.assertEqual(wgrid.weights, [1.])
wgrid.appendAxes(self.fooAxes2, 'fooAxes2', self.rawdata, weights=[0.4, 0.6])
wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata, weights=[0.4, 0.6])
self.assertEqual(wgrid.weights, [0.4, 0.6])
def test_append_grid(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendGrid(self.fooGrid)
self.foo_fig.set_grid(wgrid)
wgrid.append_grid(self.foo_grid)
self.assertEqual(wgrid.weights, [1.])
wgrid.appendGrid(self.fooGrid2)
wgrid.append_grid(self.foo_grid2)
self.assertEqual(wgrid.weights, [1., 1.])
def test_append_grid_with_weight(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendGrid(self.fooGrid, weight=0.6)
self.foo_fig.set_grid(wgrid)
wgrid.append_grid(self.foo_grid, weight=0.6)
self.assertEqual(wgrid.weights, [0.6])
wgrid.appendGrid(self.fooGrid2, weight=0.4)
wgrid.append_grid(self.foo_grid2, weight=0.4)
self.assertEqual(wgrid.weights, [0.6, 0.4])
def test_append_grid_with_weights(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
wgrid.appendGrid(self.fooGrid)
self.foo_fig.set_grid(wgrid)
wgrid.append_grid(self.foo_grid)
self.assertEqual(wgrid.weights, [1.])
wgrid.appendGrid(self.fooGrid2, weights=[0.4, 0.6])
wgrid.append_grid(self.foo_grid2, weights=[0.4, 0.6])
self.assertEqual(wgrid.weights, [0.4, 0.6])
def test_get_item_weight(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05, weights=[0.5, 0.3, 0.6])
self.assertEqual(wgrid.getItemWeight(1), 0.3)
self.assertEqual(wgrid.get_item_weight(1), 0.3)
def test_get_weights(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05, weights=[0.5, 0.3, 0.6])
self.assertEqual(wgrid.getWeights(), [0.5, 0.3, 0.6])
self.assertEqual(wgrid.get_weights(), [0.5, 0.3, 0.6])
def test_looping_grids(self):
wgrid = WeightedGrid(margin=0.15, padding=0.05)
wgrid2 = WeightedGrid(margin=0.15, padding=0.05)
self.fooFig.setGrid(wgrid)
self.foo_fig.set_grid(wgrid)
wgrid.appendGrid(wgrid2)
wgrid2.appendGrid(wgrid)
wgrid.append_grid(wgrid2)
wgrid2.append_grid(wgrid)
with self.assertRaises(RuntimeError):
self.fooFig.layout()
self.foo_fig.layout()
class HorizontalWeightedGridTestCase(unittest.TestCase):
def setUp(self):
self.rawdata = MyData(data=[-1., 1.])
self.fooFig = EmptyFig()
self.fooGrid = HorizontalWeightedGrid()
self.fooAxes = Ax2dPlots(self.fooFig)