From 0593157e14ae44c5a79525fcc7b46bc2c3bb3f7b Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 13:59:50 -0400 Subject: [PATCH 1/9] [hail] fix expression show --- .../hail/expr/expressions/base_expression.py | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/hail/python/hail/expr/expressions/base_expression.py b/hail/python/hail/expr/expressions/base_expression.py index 4271da82532..eebd93027bc 100644 --- a/hail/python/hail/expr/expressions/base_expression.py +++ b/hail/python/hail/expr/expressions/base_expression.py @@ -593,6 +593,9 @@ def __ne__(self, other): return self._compare_op("!=", other) def _to_table(self, name): + return self._to_relational(self, name, force_table=True) + + def _to_relational(self, name, force_table=False): source = self._indices.source axes = self._indices.axes if not self._aggregations.empty(): @@ -650,15 +653,17 @@ def _to_table(self, name): assert len(axes) == 2 assert isinstance(source, hail.MatrixTable) source = source.select_entries(**{name: self}).select_rows().select_cols() - to_return = source.key_cols_by().entries().select_globals() + if force_table: + to_return = source.key_cols_by().entries().select_globals() + else: + to_return = source.select_globals() assert self.dtype == to_return[name].dtype, f'type mismatch:\n' \ f' Actual: {self.dtype}\n' \ f' Should be: {to_return[name].dtype}' return to_return - @typecheck_method(n=int, width=int, truncate=nullable(int), types=bool, handler=anyfunc) - def show(self, n=10, width=90, truncate=None, types=True, handler=print): + def show(self, n=10, width=90, truncate=None, types=True, handler=None): """Print the first few rows of the table to the console. Examples @@ -701,33 +706,33 @@ def show(self, n=10, width=90, truncate=None, types=True, handler=print): types : :obj:`bool` Print an extra header line with the type of each field. """ - handler(self._show(n, width, truncate, types)) + self._to_relational_preserving_rows_and_cols()._show(n, width, truncate, types, handler) - def _show(self, n, width, truncate, types): + def _to_relational_preserving_rows_and_cols(self): name = '' source = self._indices.source if isinstance(source, hl.Table): if self is source.row: - return source._show(n, width, truncate, types) + return source elif self is source.key: - return source.select()._show(n, width, truncate, types) + return source.select() elif isinstance(source, hl.MatrixTable): if self is source.row: - return source.rows()._show(n, width, truncate, types) + return source.rows() elif self is source.row_key: - return source.rows().select()._show(n, width, truncate, types) + return source.rows().select() if self is source.col: - return source.cols()._show(n, width, truncate, types) + return source.cols() elif self is source.col_key: - return source.cols().select()._show(n, width, truncate, types) + return source.cols().select() if self is source.entry: - return source.select_rows().select_cols().entries()._show(n, width, truncate, types) + return source.select_rows().select_cols() if source is not None: name = source._fields_inverse.get(self, name) - t = self._to_table(name) - if name in t.key: - t = t.order_by(*t.key).select(name) - return t._show(n, width, truncate, types) + x = self._to_relational(name) + if isinstance(x, hl.Table) and name in x.key: + return x.order_by(*x.key).select(name) + return x @typecheck_method(n=int, _localize=bool) From b88d3abb32dac9bcbc0b66dd67b0bfab2cecff82 Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 14:31:43 -0400 Subject: [PATCH 2/9] harmonize _show interfaces --- hail/python/hail/expr/expressions/base_expression.py | 5 +++-- hail/python/hail/table.py | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/hail/python/hail/expr/expressions/base_expression.py b/hail/python/hail/expr/expressions/base_expression.py index eebd93027bc..caac7f8618a 100644 --- a/hail/python/hail/expr/expressions/base_expression.py +++ b/hail/python/hail/expr/expressions/base_expression.py @@ -662,7 +662,7 @@ def _to_relational(self, name, force_table=False): f' Should be: {to_return[name].dtype}' return to_return - @typecheck_method(n=int, width=int, truncate=nullable(int), types=bool, handler=anyfunc) + @typecheck_method(n=int, width=int, truncate=nullable(int), types=bool, handler=nullable(anyfunc)) def show(self, n=10, width=90, truncate=None, types=True, handler=None): """Print the first few rows of the table to the console. @@ -706,7 +706,8 @@ def show(self, n=10, width=90, truncate=None, types=True, handler=None): types : :obj:`bool` Print an extra header line with the type of each field. """ - self._to_relational_preserving_rows_and_cols()._show(n, width, truncate, types, handler) + self._to_relational_preserving_rows_and_cols()._show( + n_rows=n, width=width, truncate=truncate, types=types, handler=handler) def _to_relational_preserving_rows_and_cols(self): name = '' diff --git a/hail/python/hail/table.py b/hail/python/hail/table.py index b9b23d2207c..407d47ae192 100644 --- a/hail/python/hail/table.py +++ b/hail/python/hail/table.py @@ -1396,8 +1396,8 @@ def format_line(values): return s - @typecheck_method(n=nullable(int), width=nullable(int), truncate=nullable(int), types=bool, handler=nullable(anyfunc)) - def show(self, n=None, width=None, truncate=None, types=True, handler=None): + @typecheck_method(n=nullable(int), width=nullable(int), truncate=nullable(int), types=bool, handler=nullable(anyfunc), n_rows=nullable(int)) + def show(self, n=None, width=None, truncate=None, types=True, handler=None, n_rows=None): """Print the first few rows of the table to the console. Examples @@ -1418,7 +1418,7 @@ def show(self, n=None, width=None, truncate=None, types=True, handler=None): Parameters ---------- - n : :obj:`int` + n or n_rows : :obj:`int` Maximum number of rows to show. width : :obj:`int` Horizontal width at which to break fields. @@ -1430,6 +1430,11 @@ def show(self, n=None, width=None, truncate=None, types=True, handler=None): handler : Callable[[str], Any] Handler function for data string. """ + if n_rows is not None and n is not None: + raise ValueError(f'specify one of n_rows or n, recieved {n_rows} and {n}') + if n_rows is not None: + n = n_rows + del n_rows if handler is None: try: from IPython.display import display From b07c47c6f12f9237de2ab7c042d408802f5d642b Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 14:33:46 -0400 Subject: [PATCH 3/9] fix --- hail/python/hail/expr/expressions/base_expression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hail/python/hail/expr/expressions/base_expression.py b/hail/python/hail/expr/expressions/base_expression.py index caac7f8618a..84843c529e9 100644 --- a/hail/python/hail/expr/expressions/base_expression.py +++ b/hail/python/hail/expr/expressions/base_expression.py @@ -706,7 +706,7 @@ def show(self, n=10, width=90, truncate=None, types=True, handler=None): types : :obj:`bool` Print an extra header line with the type of each field. """ - self._to_relational_preserving_rows_and_cols()._show( + self._to_relational_preserving_rows_and_cols().show( n_rows=n, width=width, truncate=truncate, types=types, handler=handler) def _to_relational_preserving_rows_and_cols(self): From 4895201cad10a6bd48c5e660245b7fc5c00cad7c Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 14:42:28 -0400 Subject: [PATCH 4/9] add some show specific tests --- hail/python/test/hail/expr/test_show.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 hail/python/test/hail/expr/test_show.py diff --git a/hail/python/test/hail/expr/test_show.py b/hail/python/test/hail/expr/test_show.py new file mode 100644 index 00000000000..79e6a61a3bd --- /dev/null +++ b/hail/python/test/hail/expr/test_show.py @@ -0,0 +1,21 @@ +import hail as hl + +setUpModule = startTestHailContext +tearDownModule = stopTestHailContext + +class Tests(unittest.TestCase): + def test(): + mt = hl.balding_nichols_model(3, 10, 10) + t = hl.utils.range_table(10) + mt.GT.show() + mt.locus.show() + mt.af.show() + mt.pop.show() + mt.sample_idx.show() + mt.bn.show() + mt.bn.fst.show() + mt.GT.n_alt_alleles().show() + (mt.GT.n_alt_alleles() * mt.GT.n_alt_alleles()).show() + (mt.af * mt.GT.n_alt_alleles()).show() + t.af.show() + (t.af * 3).show() From 87a8faad6d1436c2aa8379045de2e29f59cc0eb9 Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 14:49:56 -0400 Subject: [PATCH 5/9] fix test --- hail/python/test/hail/expr/test_show.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hail/python/test/hail/expr/test_show.py b/hail/python/test/hail/expr/test_show.py index 79e6a61a3bd..6dba6b25950 100644 --- a/hail/python/test/hail/expr/test_show.py +++ b/hail/python/test/hail/expr/test_show.py @@ -6,7 +6,7 @@ class Tests(unittest.TestCase): def test(): mt = hl.balding_nichols_model(3, 10, 10) - t = hl.utils.range_table(10) + t = mt.rows() mt.GT.show() mt.locus.show() mt.af.show() From a426db61027bca311847046be1c89e8b0e3dedb6 Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 14:52:27 -0400 Subject: [PATCH 6/9] missing imports --- hail/python/test/hail/expr/test_show.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hail/python/test/hail/expr/test_show.py b/hail/python/test/hail/expr/test_show.py index 6dba6b25950..d21e185124b 100644 --- a/hail/python/test/hail/expr/test_show.py +++ b/hail/python/test/hail/expr/test_show.py @@ -1,3 +1,6 @@ +from ..helpers import startTestHailContext, stopTestHailContext +import unittest.TestCase + import hail as hl setUpModule = startTestHailContext From 5f06db5c10591610a202a2bae0d648fd9f6ee8bc Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 17:13:31 -0400 Subject: [PATCH 7/9] Update base_expression.py --- hail/python/hail/expr/expressions/base_expression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hail/python/hail/expr/expressions/base_expression.py b/hail/python/hail/expr/expressions/base_expression.py index 84843c529e9..f13a26c0500 100644 --- a/hail/python/hail/expr/expressions/base_expression.py +++ b/hail/python/hail/expr/expressions/base_expression.py @@ -593,7 +593,7 @@ def __ne__(self, other): return self._compare_op("!=", other) def _to_table(self, name): - return self._to_relational(self, name, force_table=True) + return self._to_relational(name, force_table=True) def _to_relational(self, name, force_table=False): source = self._indices.source From a9878f2691c86ceb39b6e826e683d2e7273daecd Mon Sep 17 00:00:00 2001 From: Daniel King Date: Thu, 9 May 2019 18:53:42 -0400 Subject: [PATCH 8/9] i know how to python --- hail/python/test/hail/expr/test_show.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hail/python/test/hail/expr/test_show.py b/hail/python/test/hail/expr/test_show.py index d21e185124b..53206f7f808 100644 --- a/hail/python/test/hail/expr/test_show.py +++ b/hail/python/test/hail/expr/test_show.py @@ -1,11 +1,12 @@ from ..helpers import startTestHailContext, stopTestHailContext -import unittest.TestCase +import unittest import hail as hl setUpModule = startTestHailContext tearDownModule = stopTestHailContext + class Tests(unittest.TestCase): def test(): mt = hl.balding_nichols_model(3, 10, 10) From 8fb1dceaec8831e18fac240ff846c84a8c5f60ae Mon Sep 17 00:00:00 2001 From: Daniel King Date: Fri, 10 May 2019 12:41:34 -0400 Subject: [PATCH 9/9] i do not know how to python --- hail/python/test/hail/expr/test_show.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hail/python/test/hail/expr/test_show.py b/hail/python/test/hail/expr/test_show.py index 53206f7f808..6dfe3deb95b 100644 --- a/hail/python/test/hail/expr/test_show.py +++ b/hail/python/test/hail/expr/test_show.py @@ -8,7 +8,7 @@ class Tests(unittest.TestCase): - def test(): + def test(self): mt = hl.balding_nichols_model(3, 10, 10) t = mt.rows() mt.GT.show()