Skip to content

Commit

Permalink
Support fractional num_after_dots
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Aug 22, 2023
1 parent b665880 commit fe9191a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
90 changes: 48 additions & 42 deletions doc/demo-jupyter-screenshots.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
"text/html": [
"<div>\n",
"<style scoped>\n",
" thead.head_no_border {\n",
" border: 0px;\n",
" }\n",
" tbody tr th {\n",
" font-size: smaller;\n",
" vertical-align: middle;\n",
Expand Down Expand Up @@ -150,17 +153,17 @@
"</style>\n",
"<p>1000×1000, 212345 'float64' elements, coo</p>\n",
"<table >\n",
" <thead>\n",
" <thead class=\"head_no_border\">\n",
" <tr>\n",
" <th></th>\n",
" <th style=\"text-align: center;\">0</th>\n",
" <th style=\"text-align: center;\">1</th>\n",
" <th style=\"text-align: center;\">2</th>\n",
" <th style=\"text-align: center;\">3</th>\n",
" <th style=\"text-align: center;\">4</th>\n",
" <th style=\"text-align: center;\">5</th>\n",
" <th style=\"text-align: center;\">6</th>\n",
" <th style=\"text-align: center;\"></th>\n",
" <th style=\"text-align: center;\">996</th>\n",
" <th style=\"text-align: center;\">997</th>\n",
" <th style=\"text-align: center;\">998</th>\n",
" <th style=\"text-align: center;\">999</th>\n",
" </tr>\n",
Expand All @@ -173,10 +176,10 @@
" <td></td>\n",
" <td></td>\n",
" <td>0.3876</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td>0.9522</td>\n",
" <td></td>\n",
" <td class=\"right_cell\">0.8628</td>\n",
" </tr>\n",
" <tr>\n",
Expand All @@ -186,9 +189,9 @@
" <td></td>\n",
" <td>0.8927</td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td>0.796</td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
Expand All @@ -199,9 +202,9 @@
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td class=\"right_cell\">0.9224</td>\n",
" </tr>\n",
Expand All @@ -212,9 +215,9 @@
" <td>0.7142</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td>0.1396</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
Expand All @@ -225,63 +228,63 @@
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>0.8631</td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td>0.063</td>\n",
" <td></td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td class=\"left_cell\">0.7863</td>\n",
" <td>0.1298</td>\n",
" <td>0.9918</td>\n",
" <td>0.71</td>\n",
" <th></th>\n",
" <td class=\"left_cell\">&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&dtdot;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td class=\"right_cell\">&vellip;</td>\n",
" </tr>\n",
" <tr>\n",
" <th>995</th>\n",
" <td class=\"left_cell\"></td>\n",
" <td></td>\n",
" <td>0.4087</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td class=\"right_cell\">0.2446</td>\n",
" <td>0.8479</td>\n",
" <td>0.2453</td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <th>996</th>\n",
" <td class=\"left_cell\"></td>\n",
" <td>0.463</td>\n",
" <td></td>\n",
" <td>0.9481</td>\n",
" <td></td>\n",
" <td>0.08107</td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>0.9609</td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <th>997</th>\n",
" <td class=\"left_cell\"></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>0.09361</td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td class=\"right_cell\">0.8263</td>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <td class=\"left_cell\">&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&vellip;</td>\n",
" <td>&dtdot;</td>\n",
" <td>&vellip;</td>\n",
" <td class=\"right_cell\">&vellip;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td class=\"right_cell\">0.868</td>\n",
" </tr>\n",
" <tr>\n",
" <th>998</th>\n",
Expand All @@ -290,9 +293,9 @@
" <td></td>\n",
" <td>0.3552</td>\n",
" <td>0.03616</td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>&ctdot;</td>\n",
" <td>0.7221</td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
Expand All @@ -303,9 +306,9 @@
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>0.2624</td>\n",
" <td>&ctdot;</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>0.729</td>\n",
" <td class=\"right_cell\"></td>\n",
" </tr>\n",
Expand Down Expand Up @@ -368,6 +371,9 @@
"text/html": [
"<div>\n",
"<style scoped>\n",
" thead.head_no_border {\n",
" border: 0px;\n",
" }\n",
" tbody tr th {\n",
" font-size: smaller;\n",
" vertical-align: middle;\n",
Expand Down Expand Up @@ -395,7 +401,7 @@
"</style>\n",
"<p>1000×1000, 212345 'float64' elements, coo</p>\n",
"<table >\n",
" <thead>\n",
" <thead class=\"head_no_border\">\n",
" <tr>\n",
" <th></th>\n",
" <th style=\"text-align: center;\">0</th>\n",
Expand Down
Binary file modified doc/images/html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/jupyter_register.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions matrepr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class MatReprParams:
max_cols: int = 15
"""Maximum number of columns in HTML and Latex output."""

num_after_dots: int = 2
num_after_dots: Union[int, float] = 0.5
"""
If a matrix has more rows or columns than allowed then an ellipsis (three dots) is emitted to cover the excess.
This parameter controls how many rows/columns are drawn at the end of the matrix.
For example, a value of 1 means the final row and final column are emitted in addition to the top-left corner.
A value of 2 means the final two rows and columns are emitted, with a correspondingly smaller top-left corner.
Note: ignored for matrix formats without fast row/column indexing, such as COO.
Note: May be ignored for very large matrices without fast row/column indexing.
"""

cell_align: str = "center"
Expand Down
20 changes: 14 additions & 6 deletions matrepr/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ def __init__(self, orig_shape: Tuple[int, int], display_shape: Tuple[int, int],

if self.display_shape[0] < self.orig_shape[0]:
# need to truncate rows
self.dot_row = max(0, self.display_shape[0] - 1 - num_after_dots)
if 0 < num_after_dots < 1:
self.dot_row = int(self.display_shape[0] * num_after_dots)
else:
self.dot_row = max(0, self.display_shape[0] - 1 - int(num_after_dots))

if self.display_shape[1] < self.orig_shape[1]:
# need to truncate columns
self.dot_col = max(0, self.display_shape[1] - 1 - num_after_dots)
if 0 < num_after_dots < 1:
self.dot_col = int(self.display_shape[1] * num_after_dots)
else:
self.dot_col = max(0, self.display_shape[1] - 1 - int(num_after_dots))

def describe(self) -> str:
return self.description
Expand Down Expand Up @@ -198,7 +204,7 @@ def to_trunc(mat: MatrixAdapter, max_rows, max_cols, num_after_dots) -> Truncate
raise ValueError("Only 1 or 2 dimensional matrices supported at this time.")

if isinstance(mat, MatrixAdapterCoo):
if max_rows == nrows and max_cols == ncols:
if max_rows >= nrows and max_cols >= ncols:
num_after_dots = 0

import time
Expand Down Expand Up @@ -226,10 +232,12 @@ def to_trunc(mat: MatrixAdapter, max_rows, max_cols, num_after_dots) -> Truncate
trunc.set(row, col, val)

# fetch the other three quadrants
dot_row = trunc.dot_row if trunc.dot_row else 0
dot_col = trunc.dot_col if trunc.dot_col else 0
for row_range, col_range in [
((0, max_rows), (ncols - num_after_dots, ncols)), # top-right
((nrows - num_after_dots, nrows), (0, max_cols)), # bottom-right
((nrows - num_after_dots, nrows), (ncols - num_after_dots, ncols)), # bottom-right
((0, dot_row), (dot_col + 1, ncols)), # top-right
((dot_row + 1, nrows), (0, dot_col)), # bottom-right
((dot_row + 1, nrows), (dot_col + 1, ncols)), # bottom-right
]:
for row, col, val in mat.get_coo(row_range=row_range, col_range=col_range):
trunc.set(row, col, val)
Expand Down

0 comments on commit fe9191a

Please sign in to comment.