# %% Imports
import os, pickle, sys
import numpy as np
import pandas as pd
from great_tables import GT, style, loc
# Paths
cwd = os.getcwd()
baseDir = os.path.join(cwd,'..')
dataDir = os.path.join(baseDir,'data')
funcDir = os.path.join(baseDir,'functions')
sys.path.append(funcDir)
# Custom import
from gt_tex import make_latex, insert_rows, fix_reference, delete_rows, replace_latex_table_cell, insert_multicolumn
#%% Extract parameters
# to-do bshape
params = np.array([
['a', '$a$', 'N', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['b', '$b$', 'mm', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['vfactmin', '$b_{scale}^{min}$', '-', 'Contraction dynamics', '@van_soest_contribution_1993'],
['bshape', '$b_{shape}$', '-', 'Contraction dynamics', '@van_soest_contribution_1993'],
['fasymp', '$F_{asymp}$', '-', 'Contraction dynamics', '@rijkelijkhuizen_forcevelocity_2003'],
['fmax', '$F_{CE}^{max}$', 'N', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['kpee', '$k_{PEE}$', 'N/mm<sup>2</sup>', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['ksee', '$k_{SEE}$', 'N/mm<sup>2</sup>', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['lce_opt', '$L_{CE}^{opt}$', 'mm', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['lpee0', '$L_{PEE}^0$', 'mm', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['lsee0', '$L_{SEE}^0$', 'mm', 'Contraction dynamics', '@van_zandwijk_evaluation_1997'],
['r_as', '$r_{as}$', '-', 'Contraction dynamics', 'Arbitrary small value'],
['slopfac', '$r_{slope}$', 'mm', 'Contraction dynamics', '@katz_relation_1939'],
['w', '$w$', '-', 'Contraction dynamics', '@burkholder_sarcomere_2001'],
['a_act', '$a_{act}$', '?', 'Excitation dynamics', '@bortolotto_mhc_2000'],
['b_act1', '$b_{act,1}$', '?', 'Excitation dynamics', '@bortolotto_mhc_2000'],
['b_act2', '$b_{act,2}$', '?', 'Excitation dynamics', '@stephenson_effects_1982'],
['b_act3', '$b_{act,3}$', '?', 'Excitation dynamics', '@stephenson_effects_1982'],
['kCa', '$kCa$', 'mol/L', 'Excitation dynamics', '@kistemaker_length-dependent_2005'],
['gamma_0', '$\\gamma_0$', '-', 'Excitation dynamics', 'Arbitrary small value'],
['q0', '$q_0$', '-', 'Excitation dynamics', '@hatze_myocybernetic_1981'],
['tact', '$\\tau_{act}$', 'ms', 'Excitation dynamics', '@van_zandwijk_evaluation_1997'],
['tdeact', '$\\tau_{deact}$', 'ms', 'Excitation dynamics', '@van_zandwijk_evaluation_1997'],
])
citation_map = {
"van_zandwijk_evaluation_1997": "van Zandwijk et al.",
"van_soest_contribution_1993": "van Soest and Bobbert",
"rijkelijkhuizen_forcevelocity_2003": "Rijkelhuizen et al.",
"katz_relation_1939": "Katz (1939)",
"burkholder_sarcomere_2001": "Burkholder and Lieber",
"bortolotto_mhc_2000": "Bortolotto et al.",
"stephenson_effects_1982": "Stephenson and Williams",
"kistemaker_length-dependent_2005": "Kistemaker et al.",
"hatze_myocybernetic_1981": "Hatze"
}
def replace_citation(x):
if x.startswith('@'):
key = x[1:]
display = citation_map.get(key, key)
# extract the year = last underscore-separated part
parts = key.split('_')
year = parts[-1] if parts[-1].isdigit() else "year"
return (
f'<span class="citation" data-cites="{key}">'
f'{display} (<a href="#ref-{key}" role="doc-biblioref" aria-expanded="false">{year}</a>)'
f'</span>'
)
return x
params = np.array([[replace_citation(x) for x in row] for row in params])
orParms = []
for mus in ['GMs1','GMs2','GMs3']:
orPar = pickle.load(open(os.path.join(dataDir,mus,'parameters',mus+'_OR.pkl'), 'rb'))
# b_act is an array, extract the values
orPar['b_act1'], orPar['b_act2'], orPar['b_act3'] = orPar['b_act']
# This is a magic number in the functin, well anyway add it here!
orPar['bshape'] = 22
# Compute r_as, this is the the increase in relative CE force (Fce/Fmax) per unit of relative CE velocity (vce/lce_opt)
orPar['r_as'] = (orPar['slopfac']*0.005*0.0975*(1+orPar['a']/orPar['fmax'])) / (orPar['vfactmin']*orPar['b']/orPar['lce_opt'])
parms = [orPar[k] for k in params[:,0]]
orParms.append(parms)
#%% Create pandas dataframe
df = pd.DataFrame(list(zip(*orParms)), columns=['GM1', 'GM2', 'GM3'], index=params[:,0])
# Some are in other units so..
for parm in ['b', 'lce_opt', 'lpee0', 'lsee0', 'tact', 'tdeact']:
df.loc[parm] = df.loc[parm]*1e3
for parm in ['kpee', 'ksee']:
df.loc[parm] = df.loc[parm]/1e3
# Add partype
par_type = ['Contraction dynamics']*8 + 2*['Excitation dynamics']
df.insert(0, "Parameter", params[:,1])
df.insert(1, "Unit", params[:,2])
df.insert(2, "Partype", params[:,3])
df.insert(6, "Reference", params[:,4])
# %% TeX table
df_tex = df.drop('Partype', axis=1) # Remove Partype for LateX table
gt_table = (GT(df_tex)
#.tab_stub(rowname_col="Parameter", groupname_col="Partype")
.cols_align(align='left', columns="Parameter")
.cols_align(align='center', columns=["GM1", "GM2", "GM3"])
.fmt_number(columns=["GM1", "GM2", "GM3"], n_sigfig=3, sep_mark='',)
.fmt_scientific(columns=["GM1", "GM2", "GM3"], n_sigfig=3, rows=['$k_{PEE}$, $k_{SEE}$, $r_{as}$', '$\\gamma_0$', '$kCa$', '$q_0$'])
.cols_width(cases={"GM1": "100px", "GM2": "100px", "GM3": "100px"})
)
# Transform to LateX table
latex_str = make_latex(gt_table.as_latex())
latex_str = delete_rows(latex_str, row_numbers=[0])
add_rows = {
0: r" \bfseries Parameter & \bfseries Unit & \bfseries GM1 & \bfseries GM2 & \bfseries GM3 & \bfseries Reference \\ \hline",
1: r"\multicolumn{6}{|l|}{\itshape Contraction dynamics} \\ \hline",
14: r"\multicolumn{6}{|l|}{\itshape Excitation dynamics} \\ \hline"
}
latex_str = insert_rows(latex_str, add_rows)
citation_map = {
"van_zandwijk_evaluation_1997": "van Zandwijk et al. (1996)",
"van_soest_contribution_1993": "van Soest and Bobbert (1993)",
"rijkelijkhuizen_forcevelocity_2003": "Rijkelhuizen et al. (2003)",
"katz_relation_1939": "Katz (1939)",
"burkholder_sarcomere_2001": "Burkholder and Lieber (2001)",
"bortolotto_mhc_2000": "Bortolotto et al. (2000)",
"stephenson_effects_1982": "Stephenson and Williams (1982)",
"kistemaker_length-dependent_2005": "Kistemaker et al. (2005)",
"hatze_myocybernetic_1981": "Hatze (1981)"
}
latex_str = fix_reference(latex_str, citation_map)
latex_str = replace_latex_table_cell(latex_str, row=7, col=1, new_text=r'N/mm\textsuperscript{2}')
latex_str = replace_latex_table_cell(latex_str, row=8, col=1, new_text=r'N/mm\textsuperscript{2}')
latex_str = replace_latex_table_cell(latex_str, row=12, col=2, new_text=r'$3.71 \cdot 10^{-3}$')
latex_str = replace_latex_table_cell(latex_str, row=12, col=3, new_text=r'$5.45 \cdot 10^{-3}$')
latex_str = replace_latex_table_cell(latex_str, row=12, col=4, new_text=r'$3.16 \cdot 10^{-3}$')
latex_str = insert_multicolumn(latex_str=latex_str, row=3, col_start=2, col_span=3, text="0.100", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=4, col_start=2, col_span=3, text="22.0", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=5, col_start=2, col_span=3, text="1.50", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=13, col_start=2, col_span=3, text="2.00", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=14, col_start=2, col_span=3, text="0.50", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=15, col_start=2, col_span=3, text="-7.37", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=16, col_start=2, col_span=3, text="5.17", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=17, col_start=2, col_span=3, text="0.596", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=18, col_start=2, col_span=3, text="0.00", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=19, col_start=2, col_span=3, text=r"$8.00 \cdot 10^{-6}$", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=20, col_start=2, col_span=3, text=r"$1.00 \cdot 10^{-5}$", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=21, col_start=2, col_span=3, text=r"$5.00 \cdot 10^{-3}$", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=22, col_start=2, col_span=3, text="27.0", align='c|')
latex_str = insert_multicolumn(latex_str=latex_str, row=23, col_start=2, col_span=3, text="27.0", align='c|')
# Write to a .tex file
with open("supptbl-overview.tex", "w", encoding="utf-8") as f:
f.write(latex_str)
# %% GT
df_gt = df.copy()
gt_table = (GT(df_gt)
.tab_stub(rowname_col="Parameter", groupname_col="Partype")
.cols_align(align='left', columns="Parameter")
.cols_align(align='center', columns=["GM1", "GM2", "GM3"])
.tab_style(style = style.text(style = "italic"), locations = loc.row_groups())
.tab_style(style = style.borders(sides="right", color="#D3D3D3", weight = "0px", style = "solid"), locations = loc.body(columns="Parameter"))
.tab_style(style = style.borders(sides="right", color="#D3D3D3", weight = "2px", style = "solid"), locations = loc.body(columns=["Unit", "GM1", "GM2", "GM3"]))
.fmt_number(columns=["GM1", "GM2", "GM3"], n_sigfig=3)
.fmt_scientific(columns=["GM1", "GM2", "GM3"], n_sigfig=3, rows=['$k_{PEE}$, $k_{SEE}$, $r_{as}$', '$\\gamma_0$', '$kCa$', '$q_0$'])
#.fmt_scientific(columns=["GM1", "GM2", "GM3"], n_sigfig=3)
.cols_width(cases={"GM1": "100px", "GM2": "100px", "GM3": "100px"})
)
#print(gt_table.as_raw_html())
gt_table