class FigureFormatterTest(FormatterBaseTestCase): def setUp(self): super(FigureFormatterTest, self).setUp() self.figLatex = '\\begin{tabular}[l]{|c|}\n\hline\n\\vcenteredhbox{\includegraphics[width=100.00in,height=100.00in,keepaspectratio]{temp_test_files/test_figure.pdf}} \\\\ \hline\n\end{tabular}\n' stateSpace = [ 2 ] # Hilbert space has dimension 2; density matrix is a 2x2 matrix spaceLabels = [ ('Q0', ) ] #interpret the 2x2 density matrix as a single qubit named 'Q0' gx = pc.build_gate(stateSpace, spaceLabels, "X(pi/2,Q0)") reportFig = pplt.gate_matrix_boxplot(gx, mxBasis="pp", mxBasisDims=2, xlabel="testX", ylabel="testY", title="mytitle", boxLabels=True) figInfo = (reportFig, 'test_figure', 100, 100) # Fig, Name, Size, Size headings = [figInfo] formatters = ['Figure'] self.table = ReportTable(headings, formatters) def test_figure_formatting(self): self.assertEqual( self.table.render('latex', scratchDir='temp_test_files'), self.figLatex) def test_unsupplied_scratchdir(self): with self.assertRaises(ValueError): self.table.render('latex')
def setUp(self): super(PrecisionFormatterBase, self).setUp() n = 1.819743 # Six digits after decimal self.table = ReportTable([n], ['Normal']) self.options = dict(precision=self.precision, polarprecision=3) format_n = "{{:.{}f}}".format(self.precision).format(n) self.expected_LaTeX = self.expected_LaTeX_fmt.format(format_n) self.expected_HTML = self.expected_HTML_fmt.format(n, format_n)
def standard_table(self, fmt): # Render table = ReportTable(self.customHeadings, ['Normal'] * 4) # Four formats table.addrow(['1.0'], ['Normal']) table.render(fmt) table.finish()
class CustomHeadingTableTester(TableInstanceTester): def setUp(self): self.table = ReportTable([0.1], ['Normal'], self.custom_headings) def test_labels(self): self.table.add_row(['1.0'], ['Normal']) self.assertTrue('1.0' in self.table) rowLabels = list(self.table.keys()) self.assertEqual(rowLabels, self.table.row_names) self.assertEqual(len(rowLabels), self.table.num_rows) self.assertTrue(rowLabels[0] in self.table) row1Data = self.table[rowLabels[0]] colLabels = list(row1Data.keys()) self.assertEqual(colLabels, self.table.col_names) self.assertEqual(len(colLabels), self.table.num_cols)
class PrecisionFormatterBase(object): expected_LaTeX_fmt = '\\begin{{tabular}}[l]{{|c|}}\n\hline\n{0} \\\\ \hline\n\end{{tabular}}\n' expected_HTML_fmt = '<table><thead><tr><th> <span title="{0}">{1}</span> </th></tr></thead><tbody></tbody></table>' def setUp(self): super(PrecisionFormatterBase, self).setUp() n = 1.819743 # Six digits after decimal self.table = ReportTable([n], ['Normal']) self.options = dict(precision=self.precision, polarprecision=3) format_n = "{{:.{}f}}".format(self.precision).format(n) self.expected_LaTeX = self.expected_LaTeX_fmt.format(format_n) self.expected_HTML = self.expected_HTML_fmt.format(n, format_n) def test_render_precision_LaTeX(self): self.assertEqual( self.table.render('latex', **self.options)['latex'], self.expected_LaTeX) def test_render_precision_HTML(self): self.assertEqual( self.table.render('html', **self.options)['html'], self.expected_HTML)
class FigureFormatterTest(FormatterBaseTestCase): def setUp(self): super(FigureFormatterTest, self).setUp() self.figLatex = '\\begin{tabular}[l]{|c|}\n\hline\n\\vcenteredhbox{\includegraphics[width=100.00in,height=100.00in,keepaspectratio]{temp_test_files/test_figure.pdf}} \\\\ \hline\n\end{tabular}\n' stateSpace = [2] # Hilbert space has dimension 2; density matrix is a 2x2 matrix spaceLabels = [('Q0',)] #interpret the 2x2 density matrix as a single qubit named 'Q0' gx = pc.build_gate(stateSpace,spaceLabels,"X(pi/2,Q0)") reportFig = pplt.gate_matrix_boxplot(gx, mxBasis="pp", mxBasisDims=2, xlabel="testX", ylabel="testY", title="mytitle", boxLabels=True) figInfo = (reportFig, 'test_figure', 100, 100) # Fig, Name, Size, Size headings = [figInfo] formatters = ['Figure'] self.table = ReportTable(headings, formatters) def test_figure_formatting(self): self.assertEqual(self.table.render('latex', scratchDir='temp_test_files'), self.figLatex) def test_unsupplied_scratchdir(self): with self.assertRaises(ValueError): self.table.render('latex')
def custom_headings_no_format(self, fmt): table = ReportTable(self.customHeadings, None) table.render(fmt)
def render_pair(self, heading, formatter, formattype='latex', **kwargs): headings = [heading] formatters = [formatter] table = ReportTable(headings, formatters) return table.render(formattype, **kwargs)[formattype]
def test_general(self): table = ReportTable(['1.0'], ['Normal']) table.addrow(['1.0'], ['Normal']) table.render('text') self.assertTrue(table.has_key('1.0')) rowLabels = list(table.keys()) row1Data = table[rowLabels[0]] colLabels = list(row1Data.keys()) self.assertTrue(rowLabels, table.row_names) self.assertTrue(colLabels, table.col_names) self.assertTrue(len(rowLabels), table.num_rows) self.assertTrue(len(colLabels), table.num_cols) el00 = table[rowLabels[0]][colLabels[0]] self.assertTrue(rowLabels[0] in table) self.assertTrue(rowLabels[0] in table) table_len = len(table) self.assertEqual(table_len, table.num_rows) table_as_str = str(table) row1a = table.row(key=rowLabels[0]) col1a = table.col(key=colLabels[0]) row1b = table.row(index=0) col1b = table.col(index=0) self.assertEqual(row1a, row1b) self.assertEqual(col1a, col1b) with self.assertRaises(KeyError): table['foobar'] with self.assertRaises(KeyError): table.row(key='foobar') #invalid key with self.assertRaises(ValueError): table.row(index=100000) #out of bounds with self.assertRaises(ValueError): table.row() #must specify key or index with self.assertRaises(ValueError): table.row(key='foobar', index=1) #cannot specify key and index with self.assertRaises(KeyError): table.col(key='foobar') #invalid key with self.assertRaises(ValueError): table.col(index=100000) #out of bounds with self.assertRaises(ValueError): table.col() #must specify key or index with self.assertRaises(ValueError): table.col(key='foobar', index=1) #cannot specify key and index with self.assertRaises(ValueError): table.render(fmt="foobar") #invalid format
class TableInstanceTester(BaseCase): custom_headings = {'html': 'test', 'python': 'test', 'latex': 'test'} def setUp(self): self.table = ReportTable(self.custom_headings, ['Normal'] * 4) # Four formats def test_element_accessors(self): self.table.add_row(['1.0'], ['Normal']) self.assertTrue('1.0' in self.table) self.assertEqual(len(self.table), self.table.num_rows) row_by_key = self.table.row(key=self.table.row_names[0]) row_by_idx = self.table.row(index=0) self.assertEqual(row_by_key, row_by_idx) col_by_key = self.table.col(key=self.table.col_names[0]) col_by_idx = self.table.col(index=0) self.assertEqual(col_by_key, col_by_idx) def test_to_string(self): s = str(self.table) # TODO assert correctness def test_render_HTML(self): self.table.add_row(['1.0'], ['Normal']) self.table.add_row(['1.0'], ['Normal']) render = self.table.render('html') # TODO assert correctness def test_render_LaTeX(self): self.table.add_row(['1.0'], ['Normal']) self.table.add_row(['1.0'], ['Normal']) render = self.table.render('latex') # TODO assert correctness def test_finish(self): self.table.add_row(['1.0'], ['Normal']) self.table.finish() # TODO assert correctness def test_render_raises_on_unknown_format(self): with self.assertRaises(NotImplementedError): self.table.render('foobar') def test_raise_on_invalid_accessor(self): # XXX are these neccessary? EGN: maybe not - checks invalid inputs, which maybe shouldn't need testing? with self.assertRaises(KeyError): self.table['foobar'] with self.assertRaises(KeyError): self.table.row(key='foobar') # invalid key with self.assertRaises(ValueError): self.table.row(index=100000) # out of bounds with self.assertRaises(ValueError): self.table.row() # must specify key or index with self.assertRaises(ValueError): self.table.row(key='foobar', index=1) # cannot specify key and index with self.assertRaises(KeyError): self.table.col(key='foobar') # invalid key with self.assertRaises(ValueError): self.table.col(index=100000) # out of bounds with self.assertRaises(ValueError): self.table.col() # must specify key or index with self.assertRaises(ValueError): self.table.col(key='foobar', index=1) # cannot specify key and index
def custom_headings(self, fmt): table = ReportTable(self.headings, self.formatters, self.customHeadings) table.render(fmt)
def test_general(self): table = ReportTable(['1.0'], ['Normal']) table.addrow(['1.0'], ['Normal']) table.render('text') self.assertTrue(table.has_key('1.0')) rowLabels = list(table.keys()) row1Data = table[rowLabels[0]] colLabels = list(row1Data.keys()) self.assertTrue(rowLabels, table.row_names) self.assertTrue(colLabels, table.col_names) self.assertTrue(len(rowLabels), table.num_rows) self.assertTrue(len(colLabels), table.num_cols) el00 = table[rowLabels[0]][colLabels[0]] self.assertTrue( rowLabels[0] in table ) self.assertTrue( rowLabels[0] in table ) table_len = len(table) self.assertEqual(table_len, table.num_rows) table_as_str = str(table) row1a = table.row(key=rowLabels[0]) col1a = table.col(key=colLabels[0]) row1b = table.row(index=0) col1b = table.col(index=0) self.assertEqual(row1a,row1b) self.assertEqual(col1a,col1b) with self.assertRaises(KeyError): table['foobar'] with self.assertRaises(KeyError): table.row(key='foobar') #invalid key with self.assertRaises(ValueError): table.row(index=100000) #out of bounds with self.assertRaises(ValueError): table.row() #must specify key or index with self.assertRaises(ValueError): table.row(key='foobar',index=1) #cannot specify key and index with self.assertRaises(KeyError): table.col(key='foobar') #invalid key with self.assertRaises(ValueError): table.col(index=100000) #out of bounds with self.assertRaises(ValueError): table.col() #must specify key or index with self.assertRaises(ValueError): table.col(key='foobar',index=1) #cannot specify key and index with self.assertRaises(ValueError): table.render(fmt="foobar") #invalid format
def render_pair(self, heading, formatter, formattype='latex', **kwargs): headings = [heading] formatters = [formatter] table = ReportTable(headings, formatters) return table.render(formattype, **kwargs)
def setUp(self): self.table = ReportTable(self.custom_headings, ['Normal'] * 4) # Four formats
def setUp(self): self.table = ReportTable(self.custom_headings, None)
def setUp(self): self.table = ReportTable([0.1], ['Normal'], self.custom_headings)
def render_pair(heading, formatter, formattype='latex', **kwargs): # TODO render directly instead of through ReportTable table = ReportTable([heading], [formatter]) return table.render(formattype, **kwargs)[formattype]
def standard_table(self, fmt): # Render table = ReportTable(self.customHeadings, ['Normal']*4) # Four formats table.addrow(['1.0'], ['Normal']) table.render(fmt) table.finish()