Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
chemreac.util.table -------------------
Convenience functions for presenting reaction systems in tables.
"""
'document': { 'default': r""" \documentclass{article} \pagestyle{empty} %(usepkg)s \begin{document} %(begins)s %(table)s %(ends)s \end{document} """ }, 'table': { 'default': r""" \begin{%(table_env)s} \centering \label{tab:%(label)s} \caption[%(short_cap)s]{%(long_cap)s} \begin{tabular}{%(alignment)s} \toprule %(header)s \midrule %(body)s \bottomrule \end{tabular} \end{%(table_env)s}""", 'longtable': r""" \begin{%(table_env)s}{%(alignment)s} \caption[%(short_cap)s]{%(long_cap)s \label{tab:%(label)s}}\\ \toprule %(header)s \midrule %(body)s \bottomrule \end{%(table_env)s}""" } }
'batchmode', texfname] stdout=logfile, stderr=logfile) stdout=logfile, stderr=logfile) fmtstr = "{}\n returned with exit status {}" raise RuntimeError(fmtstr.format(' '.join(cmds), retcode)) else: finally: pass else: else: # interpret path to copy pdf to. shutil.copy(pdfpath, save)
tex=True, rxnarrow=r'$\rightarrow$', ref_fmt='{}', units=None, unit_fmt='{}'): """ Generates a table representation of a ReactionSystem.
Parameters ---------- rsys: ReactionSystem substances: sequence of Substance instances rref0: integer default start of index counter (default: 1) coldelim: string column delimiter (default: ' & ') tex: bool use latex formated output (default: True) rxnarrow: string default: '\$\\rightarrow\$' ref_fmt: string or callable format string of ``ref`` attribute of reactions units: unit registry optional (default: None) """
kunit = (get_derived_unit(units, 'concentration')**(1-rxn.order) / get_derived_unit(units, 'time')) k = to_unitless(rxn.k, kunit) else: str(rref0+ri), ' + '.join([('' if num == 1 else str(num)) + _get_name(sn) for sn, num in rxn.reactants.items() if num > 0]), rxnarrow, ' + '.join([('' if num == 1 else str(num)) + _get_name(sn) for sn, num in rxn.products.items() if num > 0]), param_fmt.format(k), unit_fmt.format(kunit), ref_fmt(rxn.ref) if callable(ref_fmt) else ref_fmt.format(rxn.ref) ]))
table_template_dict=None, **kwargs): r""" Renders user provided table_template with table_template_dict which also has 'body' entry generated from `rsys2tablines`.
Defaults is LaTeX table requiring booktabs package to be used (add \usepackage{booktabs} to preamble).
Parameters ========== rsys: ReactionSystem substances: sequence of Substance instances table_template: string table_tempalte_dict: dict used to render table_template (excl. "body") longtable: bool use longtable in defaults. (default: False) **kwargs: passed onto rsys2tablines """ 'table_env': 'longtable' if kwargs.pop( 'longtable', False) else 'table', 'alignment': 'llllSll' if siunitx else 'lllllll', 'header': kwargs.get('coldelim', ' & ').join([ 'Id.', 'Reactants', '', 'Products', '{Rate constant}', 'Unit', 'Ref' ]) + line_term, 'short_cap': rsys.name, 'long_cap': rsys.name, 'label': (rsys.name or 'None').lower() }
else: for k, v in defaults: if k not in table_template_dict: table_template_dict[k] = v
raise KeyError("There is already a 'body' key in table_template_dict") rsys, substances, **kwargs)) + line_term
else:
doc_template_dict=None, save=True, landscape=False, **kwargs): """ Convenience function to render a ReactionSystem as e.g. a pdf using e.g. pdflatex.
Parameters ========== rsys: ReactionSystem substances: sequence of Substance instances output_dir: path to output directory (default: system's temporary folder) doc_template: string LaTeX boiler plate temlpate including preamble, document environment etc. doc_template_dict: dict (string -> string) dict used to render temlpate (excl. 'table') longtable: bool use longtable in defaults. (default: False) **kwargs: passed on to `rsys2table` """ _pkgs += ['siunitx'] 'usepkg': '\n'.join([r'\usepackage{%s}' % pkg for pkg in _pkgs]), 'begins': '\n'.join([r'\begin{%s}' % env for env in _envs]), 'ends': '\n'.join([r'\end{%s}' % env for env in _envs[::-1]]) }
else: for k, v in defaults: if k not in doc_template_dict: doc_template_dict[k] = v
raise KeyError("There is already a 'table' key in doc_template_dict")
siunitx=False, fmtstr='{0:.3f}', **kwargs): line_term) if units is not None: gunit = get_derived_unit(units, 'radiolytic_yield') cur_gs = to_unitless(cur_gs, gunit) lines.append(col_delim.join(map( lambda v: fmtstr.format(v), cur_gs)) + line_term) 'table_env': 'table', 'alignment': ('@{}S' if siunitx else '@{}l')*rd.n, 'header': header, 'short_cap': 'G-values', 'long_cap': 'G-values', 'label': 'none', 'body': '\n'.join(lines) }
'usepkg': '\n'.join([r'\usepackage{%s}' % pkg for pkg in _pkgs]), 'begins': '\n'.join([r'\begin{%s}' % env for env in _envs]), 'ends': '\n'.join([r'\end{%s}' % env for env in _envs[::-1]]), 'table': table } output_dir, save) |