Commit 6cc585e8 authored by Yori Fournier's avatar Yori Fournier
Browse files

Made some addition in the appending formatter now it can be labeled

parent d4dc9ee9
Pipeline #462 passed with stage
in 4 minutes and 21 seconds
......@@ -53,25 +53,39 @@ class FormatterArrayToLines(Formatter):
class FormatterAppendToLines(Formatter):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None):
def __init__(self, xdataIndex=None, xlabel=None, ylabel=None, with_label=False):
self.xlabel = xlabel
self.ylabel = ylabel
self.xdataIndex = xdataIndex
self.with_label = with_label
def shape(self, rawdata):
ydatas = []
xdata = []
for item in rawdata.data[0]:
if self.with_label:
labels = rawdata.data[0]
iindex = 1
else:
iindex = 0
labels = None
for item in rawdata.data[iindex]:
ydatas.append([item])
for line in rawdata.data[1:]:
for line in rawdata.data[iindex+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 labels is not None:
if self.xlabel is None:
self.xlabel = labels.pop(self.xdataIndex)
else:
labels.pop(self.xdataIndex)
else:
xdata = range(0, len(ydatas[0])) # generic xdata
......@@ -79,6 +93,10 @@ class FormatterAppendToLines(Formatter):
'xdata': xdata,
'xlabel': self.xlabel,
'ylabel': self.ylabel,
'labels': labels,
'name': rawdata.name}
return(data)
class FormatterDictToLines(Formatter):
pass
......@@ -97,6 +97,15 @@ class FormatterAppendToLinesTestCase(unittest.TestCase):
[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')
def test_formatter_append_to_lines(self):
formatter = FormatterAppendToLines()
......@@ -112,13 +121,33 @@ class FormatterAppendToLinesTestCase(unittest.TestCase):
'xdata': [0., 1., 2., 3., 4., 5.],
'xlabel': None,
'ylabel': None,
'labels': None,
'name': 'data_cols'}
self.assertEqual(data, correctdata)
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)
def test_formatter_append_to_lines_with_kw(self):
formatter = FormatterAppendToLines(with_label=True, xdataIndex=0)
data = formatter.shape(self.data_labeled_cols)
self.assertEqual(formatter.xlabel, 'y0')
self.assertIsNone(formatter.ylabel)
self.assertEqual(formatter.xdataIndex, 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'}
self.assertEqual(data, correctdata)
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)
data = formatter.shape(self.data_labeled_cols)
self.assertEqual(formatter.xlabel, r'x-axis')
self.assertEqual(formatter.ylabel, r'y-axis')
......@@ -129,9 +158,29 @@ class FormatterAppendToLinesTestCase(unittest.TestCase):
'xdata': [0., 2., 4., 6., 8., 10.],
'xlabel': r'x-axis',
'ylabel': r'y-axis',
'name': 'data_cols'}
'labels': ['y0', 'y2'],
'name': 'data_labeled_cols'}
self.assertEqual(data, correctdata)
def test_formatter_append_to_lines_with_kw_3(self):
formatter = FormatterAppendToLines(xlabel=r'x-axis', ylabel=r'y-axis', 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.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': r'x-axis',
'ylabel': r'y-axis',
'labels': ['y0', 'y1', 'y2'],
'name': 'data_labeled_cols'}
self.assertEqual(data, correctdata)
if __name__ == '__main__':
unittest.main()
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