Commit e2ddde43 authored by Phil Jones's avatar Phil Jones Committed by Sean Leavey
Browse files

Add max value highlighting to sensing matrices.

The `display` method of `SensingMatrixSolution` now has options for
highlighting the largest values if Pandas is found at runtime.
parent 35ed1d52
......@@ -1321,13 +1321,22 @@ class SensingMatrixSolution(BaseSolution):
Printing :class:`.SensingMatrixSolution` will show an ASCII table of the data.
"""
def display(self, dofs=None, readouts=None, tablefmt="html", floatfmt=".2G"):
"""Displays a HTML table of the sensing matrix.
def display(
self,
dofs=None,
readouts=None,
tablefmt="pandas",
floatfmt=".2G",
highlight="dof",
highlight_color="#FFD54F",
):
"""Displays a HTML table of the sensing matrix, with the largest absolute value
for each readout highlighted.
Notes
-----
Only works when called from an IPython environmeny with the `display`
method available.
Only works when called from an IPython environment with the
`display` method available. Pandas is required for highlighting.
Parameters
----------
......@@ -1335,11 +1344,55 @@ class SensingMatrixSolution(BaseSolution):
Names of degrees of freedom to show, defaults to all if None
readouts : iterable[str], optional
Names of readouts to show, defaults to all if None
tablefmt : str, optional
Either 'pandas' for pandas formatting, or anything else to
pass on to tabulate. Defaults to 'pandas' if available,
falling back to 'html'.
floatfmt : str, optional
Format to print numbers in, defaults to '.2G'.
highlight : str, optional
Either 'dof' to highlight the readout that gives the largest
output for each dof, or 'readout' to highlight the dof for
which each readout gives the largest output. Defaults to
'dof'.
highlight_color : str, optional
Color to highlight the maximum values with, or None for no
highlighting. Pandas is required for this to have an effect.
Defaults to pale orange.
"""
from IPython.display import display
B, dofs, readouts = self.matrix_data(dofs, readouts)
if tablefmt == "html":
if tablefmt == "pandas":
try:
import pandas as pd
except ModuleNotFoundError:
tablefmt = "html"
if tablefmt == "pandas":
def highlight_max(data):
return np.where(
abs(data) == abs(data).max(),
f"background-color: {highlight_color}",
"",
)
if highlight == "dof":
axis = 1
elif highlight == "readout":
axis = 0
else:
raise ValueError(
"Argument 'highlight' must be one of 'dof' or 'readout'."
)
B = pd.DataFrame(B, index=dofs, columns=readouts)
display(
B.style.apply(highlight_max, axis=axis).format("{:" + floatfmt + "}")
)
elif tablefmt == "html":
display(
tabulate(
B,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment