PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/statsmodels/iolib/tests/test_table.py

https://github.com/danielballan/statsmodels
Python | 188 lines | 185 code | 2 blank | 1 comment | 0 complexity | bb4c69e70d6f0d32d007949d9deb2cf3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import numpy as np
  2. import unittest
  3. from statsmodels.iolib.table import SimpleTable, default_txt_fmt
  4. from statsmodels.iolib.table import default_latex_fmt
  5. from statsmodels.iolib.table import default_html_fmt
  6. import pandas
  7. from statsmodels.regression.linear_model import OLS
  8. ltx_fmt1 = default_latex_fmt.copy()
  9. html_fmt1 = default_html_fmt.copy()
  10. class TestSimpleTable(unittest.TestCase):
  11. def test_SimpleTable_1(self):
  12. """Basic test, test_SimpleTable_1"""
  13. desired = '''
  14. =====================
  15. header1 header2
  16. ---------------------
  17. stub1 1.30312 2.73999
  18. stub2 1.95038 2.65765
  19. ---------------------
  20. '''
  21. test1data = [[1.30312, 2.73999],[1.95038, 2.65765]]
  22. test1stubs = ('stub1', 'stub2')
  23. test1header = ('header1', 'header2')
  24. actual = SimpleTable(test1data, test1header, test1stubs,
  25. txt_fmt=default_txt_fmt)
  26. actual = '\n%s\n' % actual.as_text()
  27. self.assertEqual(desired, str(actual))
  28. def test_SimpleTable_2(self):
  29. """ Test SimpleTable.extend_right()"""
  30. desired = '''
  31. =============================================================
  32. header s1 header d1 header s2 header d2
  33. -------------------------------------------------------------
  34. stub R1 C1 10.30312 10.73999 stub R1 C2 50.95038 50.65765
  35. stub R2 C1 90.30312 90.73999 stub R2 C2 40.95038 40.65765
  36. -------------------------------------------------------------
  37. '''
  38. data1 = [[10.30312, 10.73999], [90.30312, 90.73999]]
  39. data2 = [[50.95038, 50.65765], [40.95038, 40.65765]]
  40. stubs1 = ['stub R1 C1', 'stub R2 C1']
  41. stubs2 = ['stub R1 C2', 'stub R2 C2']
  42. header1 = ['header s1', 'header d1']
  43. header2 = ['header s2', 'header d2']
  44. actual1 = SimpleTable(data1, header1, stubs1, txt_fmt=default_txt_fmt)
  45. actual2 = SimpleTable(data2, header2, stubs2, txt_fmt=default_txt_fmt)
  46. actual1.extend_right(actual2)
  47. actual = '\n%s\n' % actual1.as_text()
  48. self.assertEqual(desired, str(actual))
  49. def test_SimpleTable_3(self):
  50. """ Test SimpleTable.extend() as in extend down"""
  51. desired = '''
  52. ==============================
  53. header s1 header d1
  54. ------------------------------
  55. stub R1 C1 10.30312 10.73999
  56. stub R2 C1 90.30312 90.73999
  57. header s2 header d2
  58. ------------------------------
  59. stub R1 C2 50.95038 50.65765
  60. stub R2 C2 40.95038 40.65765
  61. ------------------------------
  62. '''
  63. data1 = [[10.30312, 10.73999], [90.30312, 90.73999]]
  64. data2 = [[50.95038, 50.65765], [40.95038, 40.65765]]
  65. stubs1 = ['stub R1 C1', 'stub R2 C1']
  66. stubs2 = ['stub R1 C2', 'stub R2 C2']
  67. header1 = ['header s1', 'header d1']
  68. header2 = ['header s2', 'header d2']
  69. actual1 = SimpleTable(data1, header1, stubs1, txt_fmt=default_txt_fmt)
  70. actual2 = SimpleTable(data2, header2, stubs2, txt_fmt=default_txt_fmt)
  71. actual1.extend(actual2)
  72. actual = '\n%s\n' % actual1.as_text()
  73. self.assertEqual(desired, str(actual))
  74. def test_SimpleTable_4(self):
  75. """Basic test, test_SimpleTable_4
  76. test uses custom txt_fmt"""
  77. txt_fmt1 = dict(data_fmts = ['%3.2f', '%d'],
  78. empty_cell = ' ',
  79. colwidths = 1,
  80. colsep=' * ',
  81. row_pre = '* ',
  82. row_post = ' *',
  83. table_dec_above='*',
  84. table_dec_below='*',
  85. header_dec_below='*',
  86. header_fmt = '%s',
  87. stub_fmt = '%s',
  88. title_align='r',
  89. header_align = 'r',
  90. data_aligns = "r",
  91. stubs_align = "l",
  92. fmt = 'txt'
  93. )
  94. ltx_fmt1 = default_latex_fmt.copy()
  95. html_fmt1 = default_html_fmt.copy()
  96. cell0data = 0.0000
  97. cell1data = 1
  98. row0data = [cell0data, cell1data]
  99. row1data = [2, 3.333]
  100. table1data = [ row0data, row1data ]
  101. test1stubs = ('stub1', 'stub2')
  102. test1header = ('header1', 'header2')
  103. tbl = SimpleTable(table1data, test1header, test1stubs,txt_fmt=txt_fmt1,
  104. ltx_fmt=ltx_fmt1, html_fmt=html_fmt1)
  105. def test_txt_fmt1(self):
  106. """Limited test of custom txt_fmt"""
  107. desired = """
  108. *****************************
  109. * * header1 * header2 *
  110. *****************************
  111. * stub1 * 0.00 * 1 *
  112. * stub2 * 2.00 * 3 *
  113. *****************************
  114. """
  115. actual = '\n%s\n' % tbl.as_text()
  116. #print(actual)
  117. #print(desired)
  118. self.assertEqual(actual, desired)
  119. def test_ltx_fmt1(self):
  120. """Limited test of custom ltx_fmt"""
  121. desired = r"""
  122. \begin{tabular}{lcc}
  123. \toprule
  124. & \textbf{header1} & \textbf{header2} \\
  125. \midrule
  126. \textbf{stub1} & 0.0 & 1 \\
  127. \textbf{stub2} & 2 & 3.333 \\
  128. \bottomrule
  129. \end{tabular}
  130. """
  131. actual = '\n%s\n' % tbl.as_latex_tabular()
  132. #print(actual)
  133. #print(desired)
  134. self.assertEqual(actual, desired)
  135. def test_html_fmt1(self):
  136. """Limited test of custom html_fmt"""
  137. desired = """
  138. <table class="simpletable">
  139. <tr>
  140. <td></td> <th>header1</th> <th>header2</th>
  141. </tr>
  142. <tr>
  143. <th>stub1</th> <td>0.0</td> <td>1</td>
  144. </tr>
  145. <tr>
  146. <th>stub2</th> <td>2</td> <td>3.333</td>
  147. </tr>
  148. </table>
  149. """
  150. actual = '\n%s\n' % tbl.as_html()
  151. self.assertEqual(actual, desired)
  152. def test_regression_with_tuples(self):
  153. i = pandas.Series( [1,2,3,4]*10 , name="i")
  154. y = pandas.Series( [1,2,3,4,5]*8, name="y")
  155. x = pandas.Series( [1,2,3,4,5,6,7,8]*5, name="x")
  156. df = pandas.DataFrame( index=i.index )
  157. df = df.join( i )
  158. endo = df.join( y )
  159. exo = df.join( x )
  160. endo_groups = endo.groupby( ("i",) )
  161. exo_groups = exo.groupby( ("i",) )
  162. exo_Df = exo_groups.agg( [np.sum, np.max] )
  163. endo_Df = endo_groups.agg( [np.sum, np.max] )
  164. reg = OLS(exo_Df[[("x", "sum")]],endo_Df).fit()
  165. interesting_lines = []
  166. for line in str( reg.summary() ).splitlines():
  167. if "('" in line:
  168. interesting_lines.append( line[:38] )
  169. desired = ["Dep. Variable: ('x', 'sum') ",
  170. "('y', 'sum') 1.4595 0.209 ",
  171. "('y', 'amax') 0.2432 0.035 "]
  172. self.assertEqual(sorted(desired), sorted(interesting_lines) )
  173. if __name__ == "__main__":
  174. #unittest.main()
  175. pass