def __init__(self, fe_id: FEID, name: str, link_prefix: str = 'tbl', id_in_name: bool = True, bold_name: bool = False): self.content_separator = '' self.packages.add(Package('hyperref', options='hidelinks')) self.packages.add(Package('ulem')) self.packages.add(Package('xcolor', options='table')) self.fe_id = fe_id self.name = name data = [ NoEscape(r'\hyperref['), escape_latex(f"{link_prefix}:"), fe_id, NoEscape(r']{\textcolor{blue}{\uline{') ] if id_in_name: data.append(fe_id) if name: data.append(": ") if name: data.append( bold(escape_latex(name)) if bold_name else escape_latex(name)) data.append(NoEscape("}}}")) super().__init__(data=data)
def dumps(self) -> str: """Get a string representation of this cell. Returns: A string representation of itself. """ if len(self.data) > self.threshold: return NoEscape(r'\seqsplit{' + escape_latex(self.data) + '}') else: return escape_latex(self.data)
def _serialize_role(_role: List[Script], _lexeme: Lexeme, descriptors: Descriptors, language: LANGUAGES, color: str): flexion = _lexeme.pm_flexion.constant content = _lexeme.pm_content.constant res = '' for lex, r, fl, ctt in zip_longest([_lexeme], [_role], flexion, content): if lex is None: lexeme = '' else: lexeme = "\\textbf{{{}}}".format(', '.join( descriptors.get_values(lex, language, 'translations'))) if r is None: role = '' else: role = "\\textbf{{{}}}".format(_render_role(r, language)) if fl is None: flexion = '' else: flexion = "\\textbf{{{}}}".format(', '.join( descriptors.get_values(fl, language, 'translations'))) if ctt is None: content = '' else: content = "\\textbf{{{}}}".format(', '.join( descriptors.get_values(ctt, language, 'translations'))) res += LINE_TEMPLATE.format( **{ 'lexeme': lexeme, 'role': role if r is not None else '', 'flexion': flexion if fl and not fl.empty else '', 'content': content if ctt and not ctt.empty else '', 'color': color }) res += LINE_TEMPLATE.format( **{ 'lexeme': '', 'role': escape_latex(' '.join(map(str, r)) if r is not None else ''), 'flexion': escape_latex(str(fl) if fl and not fl.empty else ''), 'content': escape_latex(str(ctt) if ctt and not ctt.empty else ''), 'color': color }) return res
def textsc(s, escape=True): r"""Make a string appear textsc in LaTeX formatting. textsc() wraps a given string in the LaTeX command \textsc{}. Args ---- s : str The string to be formatted. escape: bool If true the textsc text will be escaped Returns ------- NoEscape The formatted string. Examples -------- >>> textsc("hello") '\\textsc{hello}' >>> print(textsc("hello")) \textsc{hello} """ if escape: s = pylatex.escape_latex(s) return pylatex.NoEscape(r'\textsc{' + s + '}')
def input(s, escape=True): r"""Make LaTeX to read from a file. input() wraps a given string in the LaTeX command \input{}. Args ---- s : str The string to be formatted. escape: bool If true the input text will be escaped Returns ------- NoEscape The formatted string. Examples -------- >>> input("hello.tex") '\\input{hello.tex}' >>> print(input("hello.tex")) \input{hello.tex} """ if escape: s = pylatex.escape_latex(s) return pylatex.NoEscape(r'\input{' + s + '}')
def _document_test_result(self) -> None: """Document test results including test summary, passed tests, and failed tests. """ self.test_id = 1 instance_pass_tests, aggregate_pass_tests, instance_fail_tests, aggregate_fail_tests = [], [], [], [] for test in self.json_summary["tests"]: if test["test_type"] == "per-instance" and test["passed"]: instance_pass_tests.append(test) elif test["test_type"] == "per-instance" and not test["passed"]: instance_fail_tests.append(test) elif test["test_type"] == "aggregate" and test["passed"]: aggregate_pass_tests.append(test) elif test["test_type"] == "aggregate" and not test["passed"]: aggregate_fail_tests.append(test) with self.doc.create(Section("Test Summary")): with self.doc.create(Itemize()) as itemize: itemize.add_item( escape_latex("Execution time: {:.2f} seconds".format( self.json_summary['execution_time(s)']))) with self.doc.create(Table(position='H')) as table: table.append(NoEscape(r'\refstepcounter{table}')) self._document_summary_table( pass_num=len(instance_pass_tests) + len(aggregate_pass_tests), fail_num=len(instance_fail_tests) + len(aggregate_fail_tests)) if instance_fail_tests or aggregate_fail_tests: with self.doc.create(Section("Failed Tests")): if len(aggregate_fail_tests) > 0: with self.doc.create(Subsection("Failed Aggregate Tests")): self._document_aggregate_table( tests=aggregate_fail_tests) if len(instance_fail_tests) > 0: with self.doc.create( Subsection("Failed Per-Instance Tests")): self._document_instance_table( tests=instance_fail_tests, with_id=bool(self.data_id)) if instance_pass_tests or aggregate_pass_tests: with self.doc.create(Section("Passed Tests")): if aggregate_pass_tests: with self.doc.create(Subsection("Passed Aggregate Tests")): self._document_aggregate_table( tests=aggregate_pass_tests) if instance_pass_tests: with self.doc.create( Subsection("Passed Per-Instance Tests")): self._document_instance_table( tests=instance_pass_tests, with_id=bool(self.data_id)) self.doc.append(NoEscape(r'\newpage')) # For QMS report
def _document_aggregate_table(self, tests: List[Dict[str, Any]]) -> None: """Document a result table of aggregate tests. Args: tests: List of corresponding test dictionary to make a table. """ with self.doc.create(LongTable('|c|p{8cm}|p{7.3cm}|', booktabs=True)) as tabular: package = Package('seqsplit') if package not in tabular.packages: tabular.packages.append(package) # add table heading tabular.add_row((MultiColumn(size=1, align='|c|', data="Test ID"), MultiColumn(size=1, align='c|', data="Test Description"), MultiColumn(size=1, align='c|', data="Input Value"))) # add table header and footer tabular.add_hline() tabular.end_table_header() tabular.add_hline() tabular.add_row((MultiColumn(3, align='r', data='Continued on Next Page'), )) tabular.add_hline() tabular.end_table_footer() tabular.end_table_last_footer() for idx, test in enumerate(tests): if idx > 0: tabular.add_hline() inp_data = [ f"{arg}={self.sanitize_value(value)}" for arg, value in test["inputs"].items() ] inp_data = [WrapText(data=x, threshold=27) for x in inp_data] des_data = [ WrapText(data=x, threshold=27) for x in test["description"].split(" ") ] row_cells = [ self.test_id, IterJoin(data=des_data, token=" "), IterJoin(data=inp_data, token=escape_latex(", \n")), ] tabular.add_row(row_cells) self.test_id += 1
def _document_sys_config(self) -> None: """Add a system config summary to the traceability document. """ with self.doc.create(Section("System Config")): with self.doc.create(Itemize()) as itemize: itemize.add_item( escape_latex(f"FastEstimator {fe.__version__}")) itemize.add_item( escape_latex(f"Python {platform.python_version()}")) itemize.add_item(escape_latex(f"OS: {sys.platform}")) itemize.add_item( f"Number of GPUs: {torch.cuda.device_count()}") if fe.fe_deterministic_seed is not None: itemize.add_item( escape_latex( f"Deterministic Seed: {fe.fe_deterministic_seed}")) with self.doc.create(LongTable('|lr|', pos=['h!'], booktabs=True)) as tabular: tabular.add_row((bold("Module"), bold("Version"))) tabular.add_hline() tabular.end_table_header() tabular.add_hline() tabular.add_row((MultiColumn(2, align='r', data='Continued on Next Page'), )) tabular.add_hline() tabular.end_table_footer() tabular.end_table_last_footer() color = True for name, module in humansorted(sys.modules.items(), key=lambda x: x[0]): if "." in name: continue # Skip sub-packages if name.startswith("_"): continue # Skip private packages if isinstance(module, Base): continue # Skip fake packages we mocked if hasattr(module, '__version__'): tabular.add_row( (escape_latex(name), escape_latex(str(module.__version__))), color='black!5' if color else 'white') color = not color elif hasattr(module, 'VERSION'): tabular.add_row((escape_latex(name), escape_latex(str(module.VERSION))), color='black!5' if color else 'white') color = not color
def generate_leak_page(self, leak_docs): color = hex(getIconColorById(leak_docs.leak.meta.flag))[2:] subsection_title = pylatex.escape_latex( f"{leak_docs.leak.name} in {leak_docs.fn_name}" ) subsection = TextColorExt(color, subsection_title, options="HTML") with self.doc.create(Subsection(NoEscape(subsection.dumps()))): self.status(leak_docs.leak, color) self.criticality(leak_docs.leak) if leak_docs.src is not None: self.src_dump(leak_docs.src) self.asm_dump(leak_docs.asm) self.comment(leak_docs.leak) self.statistics(leak_docs.leak) self.doc.append(NewPage())
def word_to_latex(w: Word, descriptors: Descriptors, language: LANGUAGES): prefix = () if w.syntagmatic_fun.__class__ == ProcessSyntagmaticFunction: pass elif w.syntagmatic_fun.__class__ == DependantQualitySyntagmaticFunction: prefix = (DEPENDANT_QUALITY, ) elif w.syntagmatic_fun.__class__ == IndependantQualitySyntagmaticFunction: prefix = (INDEPENDANT_QUALITY, ) res = """ \\begin{figure} \\centering \\begin{tabular}{|llll|} \\hline lexeme & role & flexion & content \\\\ \\hline """ for role, lex in sorted(w.syntagmatic_fun.actors.items(), key=lambda e: e[0]): if isinstance(lex, JunctionSyntagmaticFunction): continue color = color_from_role(prefix + role.constant, prefix + role.constant == w.role.constant) res += _serialize_role(prefix + role.constant, lex.actor, descriptors, language, color) res += '\\hline\n' res += """ \\end{{tabular}} \\caption{{{caption}}} \\label{{fig:word_ieml}} \\end{{figure}} """.format(caption="\\textbf{{{trans}}} \\small{{{ieml}}}".format( **{ 'trans': ', '.join( descriptors.get_values(w, language, 'translations')), 'ieml': escape_latex(str(w)) })) # print(res) return res
def add_image(self, filename, *, width=plx.NoEscape(r'0.8\textwidth'), page=None, placement=plx.NoEscape(r'\centering')): """Add an image to the figure. Args ---- filename: str Filename of the image. width: str The width of the image page: int The page number of the PDF file for the image placement: str Placement of the figure, `None` is also accepted. """ if width is not None: if self.escape: width = plx.escape_latex(width) image_options = 'width=' + str(width) if page is not None: image_options = [image_options, f'page={page:d}'] if placement is not None: self.append(placement) self.append( plx.StandAloneGraphic(image_options=image_options, filename=plx.utils.fix_filename(filename)))
def in_code(s): return NoEscape(r"\inlang{{\small{{{}}}}}".format(escape_latex(s)))
def delete_line(s, *, escape=True): if escape: s = escape_latex(s) return NoEscape(r"\sout{{{}}}".format(s))
def doit(self): def render_metric(value, best, second, decimals, format_string): if isinstance(value, numbers.Number): rendered = format_string.format(value, prec=decimals) if value == best: rendered = bold(rendered) elif value == second: rendered = italic(rendered) return rendered else: return value values = np.empty((self.num_metrics, self.num_seqs, self.num_exps)) for i, seq in enumerate(self.seq_names): for j, s in enumerate(self.experiment_specs): values[:, i, j] = np.array( self.get_metrics(self.exps[s.name], seq, s.it)) means = np.empty((self.num_metrics, self.num_exps)) for i, m in enumerate(self.metrics): if m.geometric_mean: means[i, :] = scipy.stats.gmean(values[i, :, :], axis=0) else: means[i, :] = np.mean(values[i, :, :], axis=0) t = Tabular('l' + 'c' * self.num_exps) t.add_hline() escape_header_fun = lambda text: text if self.spec.escape_latex_header else NoEscape( text) if self.spec.rotate_header: t.add_row([self.spec.header] + [ NoEscape(r"\rotatebox{90}{%s}" % escape_latex( escape_header_fun(s.display_name(self.exps[s.name])))) for s in self.experiment_specs ]) else: t.add_row([self.spec.header] + [ escape_header_fun(s.display_name(self.exps[s.name])) for s in self.experiment_specs ]) t.add_hline() for i, m in enumerate(self.metrics): row_values = np.around(means[i, :], m.decimals) top_values = best_two_non_repeating(row_values, reverse=m.larger_is_better) row = [m.display_name] for v in row_values: # TODO: use NoEscape only if certain flag is enabled? row.append( NoEscape( render_metric(v, top_values[0], top_values[1], m.effective_display_decimals(), m.format_string))) t.add_row(row) t.add_hline() if self.spec.export_latex: os.makedirs(self.export_basepath, exist_ok=True) t.generate_tex( os.path.join(self.export_basepath, self.spec.export_latex)) with self.create(Subsection(self.spec.name, numbering=False)) as p: p.append(FootnoteText(t))
def under_line(s, *, escape=True): if escape: s = escape_latex(s) return NoEscape(r"\underline{{{}}}".format(s))
with d.create(Figure()) as figure: path = PurePath.joinpath(BASE_PATH, "img", romanized_title + ".jpg") figure.add_image(str(path)) d.append( Command( command="begin", arguments="parcolumns", options="sloppy", extra_arguments=2, )) for paragraph, words in zip(story["paragraphs"], story["words"]): paragraph[0] = escape_latex(paragraph[0]) footnotemark = NoEscape(r"\footnotemark") if words else "" d.append( Command(command="colchunk", arguments=paragraph[0] + footnotemark)) if words: words = map(escape_latex, words) words = r"\\".join(words) d.append( Command(command="footnotetext", arguments=NoEscape(words))) d.append(Command(command="colchunk", arguments=paragraph[1])) d.append(Command(command="colplacechunks")) d.append(Command(command="end", arguments="parcolumns")) path = PurePath.joinpath(BASE_PATH, "stories")