Repository: cogent3/cogent3
Pull request #954 (2021-09-30 06:51:31)

enhancement


Patch:
diff --git a/doc/cookbook/loading_tabular.rst b/doc/cookbook/loading_tabular.rst
index 3fa5a8fc9..7426ed1f6 100644
--- a/doc/cookbook/loading_tabular.rst
+++ b/doc/cookbook/loading_tabular.rst
@@ -191,7 +191,7 @@ A ``Table`` can be indexed like a dict if you designate a column as the index (a
 
     table["NP_055852", "Region"]
 
-.. note:: The ``index`` argument also applies when using ``make_table()``.
+.. note:: The ``index_name`` argument also applies when using ``make_table()``.
 
 Create a table from a ``pandas.DataFrame``
 ==========================================
diff --git a/doc/cookbook/tables.rst b/doc/cookbook/tables.rst
index c86e01887..82c4e4ada 100644
--- a/doc/cookbook/tables.rst
+++ b/doc/cookbook/tables.rst
@@ -352,7 +352,7 @@ You can also specify column(s) are categories
 Get a table of counts as a contingency table
 ============================================
 
-If our table consists of counts data, the ``Table`` can convert it into a ``CategoryCount`` instance that can be used for performing basic contingency table statistical tests, e.g. chisquare, G-test of independence, etc.. To do this, we must specify which column contains the row names using the ``index`` argument.
+If our table consists of counts data, the ``Table`` can convert it into a ``CategoryCount`` instance that can be used for performing basic contingency table statistical tests, e.g. chisquare, G-test of independence, etc.. To do this, we must specify which column contains the row names using the ``index_name`` argument.
 
 .. jupyter-execute::
 
@@ -369,6 +369,13 @@ If our table consists of counts data, the ``Table`` can convert it into a ``Cate
     g_test = contingency.G_independence()
     g_test
 
+Alternatively, you could also specify the ``index_name`` of the category column as
+
+.. jupyter-execute::
+    
+    table = make_table(data={"Ts": [31, 58], "Tv": [36, 138], "": ["syn", "nsyn"]})
+    contingency = table.to_categorical(["Ts", "Tv"], index_name="")
+
 Appending tables
 ================
 
@@ -770,39 +777,7 @@ Getting a table as html
     from cogent3 import load_table
 
     table = load_table("data/stats.tsv")
-    straight_html = table.to_rich_html(compact=True)
-
-We can provide customised formatting via a callback function.
-
-.. jupyter-execute::
-
-    def format_cell(value, row_num, col_num):
-        style = 'style="background: rgba(176, 245, 102, 0.25);"' if value else ""
-        return f"<td {style}>{value}</td>"
-
-    rich_html = table.to_rich_html(row_cell_func=format_cell, compact=False)
-
-Which produces the following...
-
-.. jupyter-execute::
-    :hide-code:
-
-    from IPython.core.display import HTML
-    HTML(rich_html)
-
-We could also use control html element format.
-
-.. jupyter-execute::
-
-    element_format = dict(thead=f'<thead style="background: rgba(0, 250, 0, 0.1);">')
-    rich_html = table.to_rich_html(element_formatters=element_format)
-
-Which produces the following...
-
-.. jupyter-execute::
-    :hide-code:
-
-    HTML(rich_html)
+    straight_html = table.to_html()
 
 What formats can be written?
 ============================
diff --git a/src/cogent3/util/table.py b/src/cogent3/util/table.py
index 5b7635926..a975f8375 100644
--- a/src/cogent3/util/table.py
+++ b/src/cogent3/util/table.py
@@ -2115,7 +2115,7 @@ class Table:
         draw.layout |= default_layout
         return draw
 
-    def to_categorical(self, columns=None):
+    def to_categorical(self, columns=None, index_name=None):
         """construct object that can be used for statistical tests
 
         Parameters
@@ -2138,6 +2138,7 @@ class Table:
         from cogent3.maths.stats.contingency import CategoryCounts
         from cogent3.util.dict_array import DictArrayTemplate
 
+        self.index_name = index_name if index_name is not None else self.index_name
         if self.index_name is None:
             raise ValueError("requires index_name be set")
 


Test patch:
diff --git a/tests/test_util/test_table.py b/tests/test_util/test_table.py
index a50d57d80..6777175c8 100644
--- a/tests/test_util/test_table.py
+++ b/tests/test_util/test_table.py
@@ -1927,10 +1927,10 @@ class TableTests(TestCase):
         data = {"Ts": [31, 58], "Tv": [36, 138], "": ["syn", "nsyn"]}
         table = make_table(header=["", "Ts", "Tv"], data=data)
         with self.assertRaises(ValueError):
+            # did not set an index_name
             table.to_categorical(columns=["Ts", "Tv"])
 
-        table.index_name = ""
-        got = table.to_categorical(columns=["Ts", "Tv"])
+        got = table.to_categorical(columns=["Ts", "Tv"], index_name="")
         assert_equal(got.observed, table[:, 1:].array)
 
         got = table.to_categorical(["Ts"])