Files
greater_tables_project/tests/tables.qmd
T

413 lines
11 KiB
Plaintext

---
title: All Tables Test - New TestDFGenerator test_suite
author:
- name: Stephen J. Mildenhall
orcid: 0000-0001-6956-0098
corresponding: true
email: mynl@me.com
date: last-modified
colorlinks: true
link-citations: true
link-bibliography: true
tbl-align: left
number-sections: true
number-offset: 0
number-depth: 3
code-line-numbers: false
code-copy: true
code-overflow: wrap
code-fold: true
fig-format: svg
fig-align: left
format:
html:
html-table-processing: none
theme: litera
fontsize: 0.9em
css: styles.css
include-in-header: pmir-header.html
smooth-scroll: true
toc-title: 'In this chapter:'
citations-hover: true
crossrefs-hover: false
fig-responsive: true
footnotes-hover: true
lightbox: true
link-external-icon: true
link-external-newwindow: true
page-layout: article
page-navigation: true
reference-section-title: ' '
page-footer:
left: 'Stephen J. Mildenhall. License: [CC BY-SA 2.0](https://creativecommons.org/licenses/by-sa/2.0/).'
twitter-card: true
open-graph: true
toc: true
toc-depth: 3
math: mathjax
pdf:
include-in-header: prefobnicate.tex
documentclass: scrartcl
papersize: a4
fontsize: 11pt
keep-tex: true
geometry: margin=0.8in
pdf-engine: lualatex
pdf-engine-opts:
- '-interaction=nonstopmode'
toc: false
execute:
eval: true
echo: true
cache: true
cache-type: jupyter
freeze: false
kernel: python3
engine: jupyter
daemon: 1200
jupyter:
jupytext:
formats: ipynb,qmd:quarto
text_representation:
extension: .qmd
format_name: quarto
format_version: '1.0'
jupytext_version: 1.16.4
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---
```{python}
#| echo: true
#| label: setup
from IPython.display import HTML, display
import matplotlib as mpl
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import greater_tables as gter
import greater_tables.utilities as gtu
from greater_tables import GT, sGT
gter.logger.setLevel(gter.logging.WARNING)
```
...code build completed.
# A Hard-Rules table
Second level index has mixed types. Range of magnitudes. Picking out years.
\footnotesize
```{python}
#| label: tbl-hard-rules
#| tbl-cap: Default display output (Quarto generated caption)
level_1 = ["A", "A", "B", "B", 'C']
level_2 = ['Int', 'Float', 'Float', 3, 'Longer Text']
multi_index = pd.MultiIndex.from_arrays([level_1, level_2],
names=["Level 1", "Level 2"])
start = pd.Timestamp.today().normalize() # Today's date, normalized to midnight
end = pd.Timestamp(f"{start.year}-12-31") # End of the year
hard = pd.DataFrame(
{'years!': np.arange(2000, 2025, dtype=int),
'a': np.array(np.round(np.linspace(-100000, 100000, 25), 0), dtype=int),
'b': 9.3 ** np.linspace(-12, 12, 25),
'c': np.linspace(-1601, 4000, 25),
'd': pd.date_range(start=start, end=end, periods=25),
'e': ('once upon a time, risk is hard to define, not in Kansas anymore, '
'neutrinos are hard to detect, '
'Adam Smith is the father of economics'.split(',') * 5)
}).set_index('years!')
# hard = hard.head()
hard.columns = multi_index
hard
```
\normalsize
@tbl-hard-rules shows the default output and @tbl-hard-rules-2 the `sGT` format output.
```{python}
#| label: tbl-hard-rules-2
#| tbl-cap: Greater Tables output (Quarto generated caption)
sGT(hard, 'A table with varied columns.')
```
Here are some alternatives:
* @tbl-hard-rules-3a hrules no vrules
* @tbl-hard-rules-3b change date and integer formats and
* @tbl-hard-rules-3c change padding and debug mode.
```{python}
#| echo: fenced
#| label: tbl-hard-rules-3a
#| tbl-cap: No V rules but hrules (Quarto generated caption)
display(sGT(hard.sample(5).sort_index(),
caption='GT caption No v rules, but h rules',
vrule_widths=(0,0,0),
hrule_widths=(1,0,0)))
```
```{python}
#| echo: fenced
#| label: tbl-hard-rules-3b
#| tbl-cap: Change date and integer formats (Quarto generated caption)
display(sGT(hard.sample(5).sort_index(),
caption='Change default date and integer formats',
default_date_str='%m-%d', default_integer_str='[{x:d}]'))
```
```{python}
#| echo: fenced
#| label: tbl-hard-rules-3c
#| tbl-cap: Change padding and debug mode, boxes (Quarto generated caption)
display(sGT(hard.sample(5).sort_index(),
caption='Change padding, debug mode lines',
padding_trbl=(10, 10, 20, 20), debug=True))
```
Here is the raw HTML and LaTeX output.
\footnotesize
```{python}
#| label: raw-output
f = sGT(hard.head(4), debug=True)
print('HTML output\n')
print(f._repr_html_())
print('\n\n\nTeX output\n')
print(f._repr_latex_())
```
\normalsize
# A Table with TeX Content
```{python}
#| label: tbl-tex
#| tbl-cap: '(Quarto generated caption): table displayed by default routine.'
index = pd.Index(["A", "B", "$C_1$", "C_2 not tex", '$\\cos(A)$'])
tex = pd.DataFrame(
{'x': np.arange(2020, 2025, dtype=int),
'b': np.random.random(5),
'a1': [f'$x^{i}$' for i in range(5,10)],
'a2': [f'$\\sin({i}x\\pi/n)$' for i in range(5,10)],
'a3': [f'$x^{i}$' for i in range(5,10)],
'a4': [f'\\(x^{i}\\)' for i in range(5,10)],
}).set_index('x')
tex = tex.head()
tex.columns = index
tex
```
```{python}
#| label: tbl-tex-2
#| tbl-cap: GT output (Quarto generated caption)
sGT(tex, 'GT Caption')
```
Ratio columns.
```{python}
#| label: tbl-tex-3
#| tbl-cap: greater table output
tex.columns = ["A (%)", "B", "$C_1$", "C_2 not tex", '$\\cos(A)$']
sGT(tex, 'Ratio columns in A', ratio_cols='A (%)')
```
# Greater_tables Test Suite
```{python}
#| echo: true
#| label: greater-tables-test
test_gen = gtu.TestDFGenerator(0, 0)
ans = test_gen.test_suite()
```
## Test Table: basic
```{python}
#| echo: true
#| label: tbl-greater-tables-test-0
#| tbl-cap: GT output for test table basic
hrw = (0, 0, 0)
sGT(ans['basic'], "Basic", ratio_cols='z', aligners={'w': 'l'},
hrule_widths=hrw)
```
Comments go here.
## Test Table: timeseries
```{python}
#| echo: true
#| label: tbl-greater-tables-test-1
#| tbl-cap: GT output for test table timeseries
hrw = (0, 0, 0)
sGT(ans['timeseries'], "Timeseries", ratio_cols='z', aligners={'w': 'l'},
hrule_widths=hrw)
```
Comments go here.
## Test Table: multiindex
```{python}
#| echo: true
#| label: tbl-greater-tables-test-2
#| tbl-cap: GT output for test table multiindex
hrw = (1.5, 1.0, 0.5)
sGT(ans['multiindex'], "Multiindex", ratio_cols='z', aligners={'w': 'l'},
hrule_widths=hrw)
```
Comments go here.
## Test Table: multicolumns
```{python}
#| echo: true
#| label: tbl-greater-tables-test-3
#| tbl-cap: GT output for test table multicolumns
hrw = (0, 0, 0)
sGT(ans['multicolumns'], "Multicolumns", ratio_cols='z', aligners={'w': 'l'},
hrule_widths=hrw)
```
Comments go here.
## Test Table: complex
```{python}
#| echo: true
#| label: tbl-greater-tables-test-4
#| tbl-cap: GT output for test table complex
hrw = (1.5, 1.0, 0.5)
sGT(ans['complex'], "Complex", ratio_cols='z', aligners={'w': 'l'},
hrule_widths=hrw)
```
Comments go here.
# Other input formats
## Markown
| **Insured group or insurance product** | **Sat** | **RP** | **RF** |
|:------------------------------------------------------------|:-------:|:------:|:------:|
| Non-standard auto | x | | |
| General liability for judgment proof corporation | x | | |
| Term life insurance | | x | |
| Catastrophe Reinsurance, outside rating agency bounds | | x | |
| High limit property per risk reinsurance | | x | |
| Personal lines for affluent individuals | x | x | |
| Small commercial lines | x | x | |
| Catastrophe reinsurance, within rating agency bounds | x | x | |
| Large account captive reinsurance | | | x |
| Structured quota share, requiring a risk transfer test | x | | x |
| Working layer casualty excess of loss | | x | x |
| Surplus relief quota share on cat exposed line | x | x | x |
| Middle market commercial lines work comp or commercial auto | x | x | x |
```{python}
#| echo: true
#| label: tbl-greater-tables-test-5
#| tbl-cap: GT from markdown table input
txt = '''
| **Insured group or insurance product** | **Sat** | **RP** | **RF** |
|:------------------------------------------------------------|:-------:|:------:|:------:|
| Non-standard auto | x | | |
| General liability for judgment proof corporation | x | | |
| Term life insurance | | x | |
| Catastrophe Reinsurance, outside rating agency bounds | | x | |
| High limit property per risk reinsurance | | x | |
| Personal lines for affluent individuals | x | x | |
| Small commercial lines | x | x | |
| Catastrophe reinsurance, within rating agency bounds | x | x | |
| Large account captive reinsurance | | | x |
| Structured quota share, requiring a risk transfer test | x | | x |
| Working layer casualty excess of loss | | x | x |
| Surplus relief quota share on cat exposed line | x | x | x |
| Middle market commercial lines work comp or commercial auto | x | x | x |
'''
GT(txt)
```
## List of lists
```{python}
x = None
if x:
print(123)
```
```{python}
#| echo: true
#| label: tbl-greater-tables-test-6
#| tbl-cap: GT output for list of lists input
lol = [['a', 'b', 'c', 'd'], ['west', 10, 20, 30], ['east', 10, 200, 30], ['north', 10, 20, 300], ['south', 100, 20, 30]]
GT(lol)
```
```{python}
f = GT(lol)
f
```
```{python}
tbl = '''
Var | Amount
:---|------:
A | 100.0
B | 0.123
C | A string
'''
def ff(x):
if abs(x) < 1:
return f'{x:.1%}'
else:
return f'{x:,.2f}'
sGT(tbl, table_float_format=ff)
```
```{python}
P = 1000 * 1.075**-10 + 120
L = 1000
ry = .1
v = 1/(1+ry)
T = 10
pv = P - v**T * L
fv = pv / v**T
pv, fv
```