Esempio n. 1
0
    def get_packaging_table_as_tabularx(self):
        table = Tabularx("|X|X|X|", row_height=1.25)
        table.add_hline()
        table.add_row(bold("Type"), bold("Størrelse"), bold("Antall"))
        for child_id in self.packaging_table.get_children():
            child = self.packaging_table.item(child_id)
            if (text := child["text"]):
                table.add_hline()

            table.add_row(text, child["values"][0], child["values"][1])
Esempio n. 2
0
    def _write_tables(self, tables: List[FeSummaryTable], model_ids: Set[FEID],
                      datasets: Dict[FEID, Tuple[Set[str], Any]]) -> None:
        """Insert a LaTeX representation of a list of tables into the current doc.

        Args:
            tables: The tables to write into the doc.
            model_ids: The ids of any known models.
            datasets: A mapping like {ID: ({modes}, dataset)}. Useful for augmenting the displayed information.
        """
        for tbl in tables:
            name_override = None
            toc_ref = None
            extra_rows = None
            if tbl.fe_id in model_ids:
                # Link to a later detailed model description
                name_override = Hyperref(Marker(name=str(tbl.name),
                                                prefix="subsec"),
                                         text=NoEscape(r'\textcolor{blue}{') +
                                         bold(tbl.name) + NoEscape('}'))
            if tbl.fe_id in datasets:
                modes, dataset = datasets[tbl.fe_id]
                title = ", ".join([s.capitalize() for s in modes])
                name_override = bold(f'{tbl.name} ({title})')
                # Enhance the dataset summary
                if isinstance(dataset, FEDataset):
                    extra_rows = list(dataset.summary().__getstate__().items())
                    for idx, (key, val) in enumerate(extra_rows):
                        key = f"{prettify_metric_name(key)}:"
                        if isinstance(val, dict) and val:
                            if isinstance(
                                    list(val.values())[0],
                                (int, float, str, bool, type(None))):
                                val = jsonpickle.dumps(val, unpicklable=False)
                            else:
                                subtable = Tabularx(
                                    'l|X',
                                    width_argument=NoEscape(r'\linewidth'))
                                for k, v in val.items():
                                    if hasattr(v, '__getstate__'):
                                        v = jsonpickle.dumps(v,
                                                             unpicklable=False)
                                    subtable.add_row((k, v))
                                # To nest TabularX, have to wrap it in brackets
                                subtable = ContainerList(data=[
                                    NoEscape("{"), subtable,
                                    NoEscape("}")
                                ])
                                val = subtable
                        extra_rows[idx] = (key, val)
            tbl.render_table(self.doc,
                             name_override=name_override,
                             toc_ref=toc_ref,
                             extra_rows=extra_rows)
Esempio n. 3
0
def test_table():
    # Tabular
    t = Tabular(table_spec='|c|c|', data=None, pos=None, width=2)

    t.add_hline(start=None, end=None)

    t.add_row((1, 2), escape=False, strict=True, mapper=[bold])
    t.add_row(1, 2, escape=False, strict=True, mapper=[bold])

    # MultiColumn/MultiRow.
    t.add_row((MultiColumn(size=2, align='|c|', data='MultiColumn'), ),
              strict=True)

    # One multiRow-cell in that table would not be proper LaTeX,
    # so strict is set to False
    t.add_row((MultiRow(size=2, width='*', data='MultiRow'), ), strict=False)

    repr(t)

    # TabularX
    tabularx = Tabularx(table_spec='X X X',
                        width_argument=NoEscape(r"\textwidth"))
    tabularx.add_row(["test1", "test2", "test3"])

    # Long Table
    longtable = LongTable(table_spec='c c c')
    longtable.add_row(["test", "test2", "test3"])
    longtable.end_table_header()

    # Colored Tabu
    coloredtable = Tabu(table_spec='X[c] X[c]')
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabu with 'spread'
    coloredtable = Tabu(table_spec='X[c] X[c]', spread="1in")
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabu with 'to'
    coloredtable = Tabu(table_spec='X[c] X[c]', to="5in")
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabularx
    coloredtable = Tabularx(table_spec='X[c] X[c]')
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Column
    column = ColumnType("R", "X", r"\raggedleft", parameters=2)
    repr(column)
Esempio n. 4
0
def test_table():
    # Tabular
    t = Tabular(table_spec='|c|c|', data=None, pos=None, width=2)

    t.add_hline(start=None, end=None)

    t.add_row((1, 2), escape=False, strict=True, mapper=[bold])
    t.add_row(1, 2, escape=False, strict=True, mapper=[bold])

    # MultiColumn/MultiRow.
    t.add_row((MultiColumn(size=2, align='|c|', data='MultiColumn'),),
              strict=True)

    # One multiRow-cell in that table would not be proper LaTeX,
    # so strict is set to False
    t.add_row((MultiRow(size=2, width='*', data='MultiRow'),), strict=False)

    repr(t)

    # TabularX
    tabularx = Tabularx(table_spec='X X X',
                        width_argument=NoEscape(r"\textwidth"))
    tabularx.add_row(["test1", "test2", "test3"])

    # Long Table
    longtable = LongTable(table_spec='c c c')
    longtable.add_row(["test", "test2", "test3"])
    longtable.end_table_header()

    # Colored Tabu
    coloredtable = Tabu(table_spec='X[c] X[c]')
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabu with 'spread'
    coloredtable = Tabu(table_spec='X[c] X[c]', spread="1in")
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabu with 'to'
    coloredtable = Tabu(table_spec='X[c] X[c]', to="5in")
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Colored Tabularx
    coloredtable = Tabularx(table_spec='X[c] X[c]')
    coloredtable.add_row(["test", "test2"], color="gray", mapper=bold)

    # Column
    column = ColumnType("R", "X", r"\raggedleft", parameters=2)
    repr(column)
Esempio n. 5
0
def get_latex_table_type3(
    doc,
    results=None,
    dataset_name=WDEReprSTL10,
    arch_names=["MaxMin", "ReLU"],
    lr="0.0001",
):
    method_names = ["OSSN", "RKO", "BCOP"]
    num_archs = len(arch_names)
    num_methods = len(method_names)
    fmt = "cc" + "c" * num_methods
    num_cols = 2 + num_methods
    with doc.create(Tabularx(fmt)) as data_table:
        headers = tuple(
            map(lambda x: NoEscape(boldify(x)),
                ("", "") + tuple(method_names)))
        data_table.add_row(headers)
        cmidrule(data_table, 1, num_cols)
        multi_row_outer = MultiRow(num_archs, data=NoEscape(dataset_name))
        for i, arch_name in enumerate(arch_names):
            if i == 0:
                row = (multi_row_outer, )
            else:
                row = ("", )
            row += (NoEscape(boldify(arch_name)), )
            row += populate_numbers([
                results[(dataset_name, arch_name, method_name, lr, "Loss",
                         None)] for method_name in method_names
            ])
            data_table.add_row(row)
Esempio n. 6
0
def add_header(doc, meal):
    doc.append(
        Command('renewcommand',
                arguments=Command('arraystretch'),
                extra_arguments='1.75'))
    doc.append(NoEscape(r'\definecolor{light-gray}{gray}{0.85}'))
    doc.append(Command('arrayrulecolor', arguments=NoEscape(r'light-gray')))

    with doc.create(Table()):
        with doc.create(Tabularx(
                "X r",
                width_argument=NoEscape(r'\textwidth'))) as table_content:
            # add header
            table_content.add_row([
                NoEscape(r'\LARGE \textbf{' + meal['meal_name'] + '}'),
                NoEscape(r'\color{gray} \large \textbf{' +
                         meal['meal_date'].strftime("%a, %d. %b") + '}')
            ])

            table_content.add_row([
                NoEscape(r'\small \textit{(' + meal['meal_weekview_name'] +
                         ')}'),
                NoEscape(r'\color{gray} \large \textbf{' +
                         meal['meal_used_as'] + '}')
            ])
            table_content.add_hline()
Esempio n. 7
0
def generate_user_story(user_story: UserStory, locale: LocaleDictionary,
                        paragraph: Paragraph) -> Paragraph:
    with paragraph.create(Tabularx(table_spec="|X|X|",
                                   row_height="1.4")) as tabularx:
        tabularx: Tabularx

        definitions_of_done = Itemize()
        for definition_of_done in user_story.definitions_of_done:
            definitions_of_done.add_item(definition_of_done)
        comments = Itemize()
        for comment in user_story.comments:
            comments.add_item(comment)

        tabularx.add_row(
            [MultiColumn(2, data=bold(user_story.name), color="gray")])
        tabularx.add_row([f"{locale.as_user}: ", f"{locale.user_want}: "])
        tabularx.add_row([user_story.user, user_story.action])
        if user_story.description is not None:
            tabularx.add_row([
                MultiColumn(
                    2,
                    align=NoEscape("|p{\\rowWidth}|"),
                    data=[f"{locale.description}: ", user_story.description],
                    color="gray")
            ])
        if len(user_story.definitions_of_done) > 0:
            tabularx.add_row([
                MultiColumn(2,
                            align=NoEscape("|p{\\rowWidth}|"),
                            data=[
                                f"{locale.definition_of_done}: ",
                                definitions_of_done
                            ])
            ])
        if len(user_story.assignments) > 0:
            tabularx.add_row([
                MultiColumn(2,
                            align=NoEscape("|p{\\rowWidth}|"),
                            data=[
                                f"{locale.assignation}: ",
                                ", ".join(user_story.assignments)
                            ],
                            color="gray")
            ])
        tabularx.add_row([
            f"{locale.estimated_duration}: ",
            f"{user_story.estimated_duration:g} {locale.man_days} ({int(user_story.estimated_duration * 8)}"
            f" {locale.hours})"
        ])
        tabularx.add_row(
            [f"{locale.status}: ",
             user_story.status.translate(locale)])
        if len(user_story.comments) > 0:
            tabularx.add_row([
                MultiColumn(2,
                            align=NoEscape("|p{\\rowWidth}|"),
                            data=[f"{locale.comments}: ", comments],
                            color="gray")
            ])
    return paragraph
Esempio n. 8
0
def tabela_alternativas(doc, model, table_spec, id=None, hline=True):
    from pylatex import Tabularx, NoEscape

    # Conta a quantidade de 'X', 'l', 'c', 'r' etc.
    nro_colunas = sum(char.isalpha() for char in table_spec)

    with doc.create(Tabularx(table_spec,
                             width_argument=WIDTH_ARGUMENT)) as tab:
        if hline:
            tab.add_hline()

        row = []
        for i, model in enumerate(model.objects.all(), 1):
            if id and model.id == id:
                row.append(NoEscape(r'({}) {}'.format(TIMES, model.nome)))
            else:
                row.append(NoEscape(r'({}) {} '.format(PHANTOM, model.nome)))

            if i % nro_colunas == 0:
                tab.add_row(row)
                del row[:]

        # Adiciona o resto dos itens à tabela
        if len(row):
            for _ in range(nro_colunas - len(row)):
                row.append('')
            tab.add_row(row, strict=False)

        if hline:
            tab.add_hline()
Esempio n. 9
0
    def get_general_info_as_tabularx(self):
        table = Tabularx("|X|X|X|X|", row_height=1.25)
        table.add_hline()
        table.add_row(bold("Kundenavn"), self.customer_name.get(),
                      bold("Ordrenummer"), self.order_number.get())

        table.add_hline()
        total_lengde = str(self.get_total_length_l())
        if (total_length_r := self.get_total_length_r()):
            total_lengde += f" x {total_length_r}"
Esempio n. 10
0
def add_recipe(doc, recipe):
    doc.append(Command('vspace', arguments='0.75cm'))

    doc.append(
        Command('renewcommand',
                arguments=Command('arraystretch'),
                extra_arguments='1.25'))

    if 'ingredients' in recipe and len(recipe['ingredients']) > 0:
        with doc.create(Table(position='h')) as table:

            table.add_caption(recipe['recipe_name'] + ' (für ' +
                              str(recipe['recipe_participants']) + ' Per.)')

            # add section for 'recipe_description' or 'recipe_notes' if one is none-empty
            if recipe['recipe_description'] + recipe['recipe_notes'] != '':
                with table.create(
                        Tabularx('l X', width_argument=NoEscape(
                            r'\textwidth'))) as table_content:
                    if recipe['recipe_description'] != '':
                        table_content.add_row(
                            ['Beschreibung: ', recipe['recipe_description']])
                    if recipe['recipe_notes'] != '':
                        table_content.add_row(
                            ['Notizen:', recipe['recipe_notes']])

            table.append(NoEscape(r'\par'))

            with table.create(Tabularx('| r | r | l | l | X |', width_argument=NoEscape(r'\textwidth'))) \
                    as table_content:
                table_content.add_hline()
                table_content.add_row([
                    NoEscape(r'\tiny{1 Per.}'),
                    NoEscape(r'\tiny{' + str(recipe['recipe_participants']) +
                             ' Per.}'),
                    NoEscape(r'\tiny{Einheit}'),
                    NoEscape(r'\tiny{Lebensmittel}'),
                    NoEscape(r'\tiny{Kommentar}')
                ])
                table_content.add_hline()
                for ingredient in recipe['ingredients']:
                    add_ingredient(table_content, ingredient)
                    table_content.add_hline()
Esempio n. 11
0
def get_latex_table_type1(
    doc,
    results=None,
    method_names=["OSSN", "RKO", "SVCM", "BCOP"],
    config_names=["Clean", "Robust"],
    eps=["1.58", "0.1411764706"],
):

    dataset_names = [MNISTRepr, CIFAR10Repr]
    arch_names = ["Small", "Large"]
    num_archs = len(arch_names)
    num_configs = len(config_names)
    num_methods = len(method_names)
    fmt = "c|cc|" + "c" * num_methods
    num_cols = 3 + num_methods
    with doc.create(Tabularx(fmt)) as data_table:
        headers = tuple(
            map(
                lambda x: NoEscape(boldify(x)),
                ("Dataset", "", "") + tuple(method_names),
            ))
        data_table.add_row(headers)
        cmidrule(data_table, 1, num_cols)
        for dataset_name, ep in zip(dataset_names, eps):
            multi_row_outer = MultiRow(num_archs * num_configs,
                                       data=NoEscape(dataset_name))
            for i, arch_name in enumerate(arch_names):
                multi_row_inner = MultiRow(num_configs,
                                           data=NoEscape(boldify(arch_name)))
                for j, config_name in enumerate(config_names):

                    row = ()
                    if i == 0 and j == 0:
                        row += (multi_row_outer, )
                    else:
                        row += ("", )
                    if j == 0:
                        row += (multi_row_inner, )
                    else:
                        row += ("", )
                    row += (config_name, )
                    row += populate_numbers([
                        results[(
                            dataset_name,
                            arch_name,
                            method_name,
                            None,
                            config_name,
                            ep,
                        )] for method_name in method_names
                    ])
                    data_table.add_row(row)
                if i != num_archs - 1:
                    cmidrule(data_table, 2, num_cols)
            cmidrule(data_table, 1, num_cols)
    def render_table(self,
                     doc: Document,
                     name_override: Optional[LatexObject] = None,
                     toc_ref: Optional[str] = None,
                     extra_rows: Optional[List[Tuple[str, Any]]] = None) -> None:
        """Write this table into a LaTeX document.

        Args:
            doc: The LaTeX document to be appended to.
            name_override: An optional replacement for this table's name field.
            toc_ref: A reference to be added to the table of contents.
            extra_rows: Any extra rows to be added to the table before the kwargs.
        """
        with doc.create(Table(position='htp!')) as table:
            table.append(NoEscape(r'\refstepcounter{table}'))
            table.append(Label(Marker(name=str(self.fe_id), prefix="tbl")))
            if toc_ref:
                table.append(NoEscape(r'\addcontentsline{toc}{subsection}{' + escape_latex(toc_ref) + '}'))
            with doc.create(Tabularx('|lX|', booktabs=True)) as tabular:
                package = Package('xcolor', options='table')
                if package not in tabular.packages:
                    # Need to invoke a table color before invoking TextColor (bug?)
                    tabular.packages.append(package)
                package = Package('seqsplit')
                if package not in tabular.packages:
                    tabular.packages.append(package)
                tabular.add_row((name_override if name_override else bold(self.name),
                                 MultiColumn(size=1, align='r|', data=TextColor('blue', self.fe_id))))
                tabular.add_hline()
                type_str = f"{self.type}"
                match = re.fullmatch(r'^<.* \'(?P<typ>.*)\'>$', type_str)
                type_str = match.group("typ") if match else type_str
                tabular.add_row(("Type: ", escape_latex(type_str)))
                if self.path:
                    if isinstance(self.path, LatexObject):
                        tabular.add_row(("", self.path))
                    else:
                        tabular.add_row(("", escape_latex(self.path)))
                for k, v in self.fields.items():
                    tabular.add_hline()
                    tabular.add_row((f"{k.capitalize()}: ", v))
                if self.args:
                    tabular.add_hline()
                    tabular.add_row(("Args: ", self.args))
                if extra_rows:
                    for (key, val) in extra_rows:
                        tabular.add_hline()
                        tabular.add_row(key, val)
                if self.kwargs:
                    tabular.add_hline()
                    for idx, (kwarg, val) in enumerate(self.kwargs.items()):
                        tabular.add_row((italic(kwarg), val), color='white' if idx % 2 else 'black!5')
Esempio n. 13
0
def weekview_table(doc: Document, camp: Camp):
    # content for this page
    days = camp.get_days()

    # add packages
    doc.packages.add(Package('caption', options='tableposition=top'))
    doc.packages.add(Package('graphicx'))
    doc.packages.add(Package('float'))
    doc.packages.add(Package('floatpag'))

    with doc.create(Sidewaystable(options='p!')):

        doc.append(Command('small'))

        # define table look
        doc.append(Command('caption*', arguments=Arguments(NoEscape(r'\textbf{Wochenplan Sommerlager 2021} Achtung Datum falsch!!!'))))
        doc.append(Command('centering'))
        doc.append(Command('newcolumntype', arguments='Y',
                           extra_arguments=Arguments(NoEscape(r'>{\centering\arraybackslash}X'))))
        doc.append(Command('newcommand*'))
        doc.append(Command('rot', arguments=Command('rotatebox', options='origin=c', arguments='270')))
        doc.append(Command('renewcommand', arguments=Command('arraystretch'), extra_arguments='2.25'))

        column_template = r'| >{\bfseries}Y |'
        for _ in days:
            column_template += ' Y | '

        doc.append(NoEscape(r'\makebox[\textwidth]{'))  # open makebox

        with doc.create(Tabularx(NoEscape(column_template),
                                 width_argument=NoEscape(r'1.15\textwidth'))) as table_content:
            # reset with since we use custom columntypes
            table_content.width = len(days) + 1

            # add header
            table_content.add_hline()
            table_content.add_row([''] + days)
            table_content.add_hline()
            table_content.add_hline()

            # get meal data
            meals = camp.get_meals_for_weekview()

            # add meals
            for meal_name in meals.keys():
                table_content.add_row([meal_name] + meals[meal_name])
                table_content.add_hline()

        doc.append(Command('thisfloatpagestyle', arguments='empty'))
        doc.append(NoEscape(r'}%'))  # close makebox
Esempio n. 14
0
def generate_document_description(schema: PLDSchema, locale: LocaleDictionary,
                                  document: Document) -> Document:
    document.append(NewPage())
    total_man_days, man_days_distribution = generate_stats(schema)
    with document.create(MiniPage()) as minipage:
        minipage: MiniPage
        with minipage.create(
                Section(title=locale.document_description)) as section:
            section: Section
            with section.create(Tabularx(table_spec="|l|X|",
                                         row_height=1.4)) as tabularx:
                tabularx: Tabularx
                tabularx.add_row([
                    MultiColumn(1, data=locale.title, color="gray"),
                    schema.title
                ])
                tabularx.add_row([
                    MultiColumn(1, data=locale.description, color="gray"),
                    schema.description
                ])
                tabularx.add_row([
                    MultiColumn(1, data=locale.authors, color="gray"),
                    ", ".join(schema.authors)
                ])
                tabularx.add_row([
                    MultiColumn(1, data=locale.updated_date, color="gray"),
                    schema.versions[-1].date
                ])
                tabularx.add_row([
                    MultiColumn(1, data=locale.model_version, color="gray"),
                    schema.versions[-1].version
                ])
                tabularx.add_row(
                    [MultiColumn(2, color="gray", data=bold(locale.stats))])
                tabularx.add_row([
                    MultiColumn(1,
                                data=locale.man_days_distribution,
                                color="gray"),
                    NoEscape(f"{NewLine().dumps()} ".join([
                        f"{author}: {score:g}"
                        for author, score in man_days_distribution.items()
                    ]))
                ])
                tabularx.add_row([
                    MultiColumn(1, data=locale.total_man_days, color="gray"),
                    f"{total_man_days:g}"
                ])
    return document
Esempio n. 15
0
    def constroiCabecalho(self):
        self.firstPage = PageStyle('firstpage')

        with self.firstPage.create(Head("L")) as header_left:
            with header_left.create(
                    MiniPage(width=NoEscape(r"0.3\textwidth"),
                             pos='l')) as logo_wrapper:
                with logo_wrapper.create(
                        Tabularx('X X',
                                 width_argument=NoEscape(
                                     r"0.5\textwidth"))) as logo:
                    logo.add_row([MultiColumn(2, align='c')])
                    logo_file = os.path.join(os.getcwd(), 'Telas', 'Imagens',
                                             'nautilusDash.png')
                    imagem = StandAloneGraphic(image_options="width=36px",
                                               filename=logo_file)
                    textLogo = MiniPage(width=NoEscape(r"0.6\textwidth"),
                                        content_pos='t',
                                        align='l')

                    textLogo.append(LargeText(bold('Nautilus')))
                    textLogo.append(LineBreak())
                    textLogo.append('Navegue sem medo')
                    textLogo.append(LineBreak())
                    textLogo.append('\n')

                    logo.add_row([imagem, textLogo])

        with self.firstPage.create(Head("C")) as headerCenter:
            with headerCenter.create(
                    MiniPage(width=NoEscape(r"0.4\textwidth"),
                             pos='c')) as centro:
                centro.append(LargeText(bold('Relatório geral de clientes')))

        # Add document title
        with self.firstPage.create(Head("R")) as right_header:
            with right_header.create(
                    MiniPage(width=NoEscape(r"0.3\textwidth"),
                             pos='R',
                             align='r')) as title_wrapper:
                title_wrapper.append(
                    LargeText(bold(self.usuarioModel.nomeEmpresa)))
                title_wrapper.append(LineBreak())
                title_wrapper.append(
                    MediumText(mascaraMeses(data=datetime.date.today())))
Esempio n. 16
0
def get_latex_table_type1b(
    doc,
    results=None,
    method_arch_names=[
        ("BCOP", "Large"),
        ("FC", "3"),
        ("KW", "Large "),
        ("KW", "Resnet"),
    ],
    eps=["1.58", "0.1411764706"],
    config_names=["Clean", "Robust"],
):
    dataset_names = [MNISTRepr, CIFAR10Repr]
    num_configs = len(config_names)
    num_methods = len(method_arch_names)
    fmt = "c|c|" + "c" * num_methods
    num_cols = 2 + num_methods
    with doc.create(Tabularx(fmt)) as data_table:
        headers = (NoEscape(boldify("Dataset")), "") + tuple(
            map(lambda x: NoEscape(boldify("{}-{}".format(*x))),
                method_arch_names))
        data_table.add_row(headers)
        cmidrule(data_table, 1, num_cols)
        for dataset_name, ep in zip(dataset_names, eps):
            multi_row_outer = MultiRow(num_configs,
                                       data=NoEscape(dataset_name))
            for j, config_name in enumerate(config_names):
                row = ()
                if j == 0:
                    row += (multi_row_outer, )
                else:
                    row += ("", )
                row += (config_name, )
                row += populate_numbers([
                    results[(
                        dataset_name,
                        arch_name,
                        method_name,
                        None,
                        config_name,
                        ep,
                    )] for method_name, arch_name in method_arch_names
                ])
                data_table.add_row(row)
            cmidrule(data_table, 1, num_cols)
Esempio n. 17
0
    def _document_summary_table(self, pass_num: int, fail_num: int) -> None:
        """Document a summary table.

        Args:
            pass_num: Total number of passed tests.
            fail_num: Total number of failed tests.
        """
        with self.doc.create(Tabularx('|Y|Y|Y|', booktabs=True)) as tabular:
            package = Package('seqsplit')
            if package not in tabular.packages:
                tabular.packages.append(package)

            # add table heading
            tabular.add_row(("Total Tests", "Total Passed ", "Total Failed"),
                            strict=False)
            tabular.add_hline()

            tabular.add_row((pass_num + fail_num, pass_num, fail_num),
                            strict=False)
Esempio n. 18
0
def generate_deliverables(schema: PLDSchema, locale: LocaleDictionary,
                          document: Document) -> Document:
    with document.create(MiniPage()) as minipage:
        minipage: MiniPage
        with minipage.create(Section(title=locale.deliverable_map)) as section:
            section: Section

            for deliverable in schema.deliverables:
                with section.create(
                        Subsection(title=deliverable.name)) as subsection:
                    subsection: Subsection
                    subsets_length = len(deliverable.subsets)
                    tabular_length = subsets_length if subsets_length != 0 else 1
                    table_spec = f"|{'|'.join(['X'] * tabular_length)}|"
                    with subsection.create(
                            Tabularx(table_spec=table_spec,
                                     row_height="1.4")) as tabularx:
                        tabularx: Tabularx
                        tabularx.add_row([
                            MultiColumn(tabular_length,
                                        data=deliverable.name,
                                        color="gray")
                        ])

                        tabularx_contents: List[List[str]] = [[
                            f"{subset_index} {subset.name}"
                        ] + [
                            f"{subset_index}.{user_story_index} {user_story.name}"
                            for user_story_index, user_story in enumerate(
                                subset.user_stories, start=1)
                        ] for subset_index, subset in enumerate(
                            deliverable.subsets, start=1)]
                        max_size = max(
                            [len(content) for content in tabularx_contents])
                        tabularx_contents: List[List[str]] = [
                            content +
                            ["" for _ in range(len(content), max_size)]
                            for content in tabularx_contents
                        ]
                        for row_contents in zip(*tabularx_contents):
                            tabularx.add_row(
                                [row_content for row_content in row_contents])
    return document
Esempio n. 19
0
def tabela_membros(doc, projeto_extensao):
    table_spec = NoEscape(r'''|>{\centering\arraybackslash}X|
                              >{\centering\arraybackslash}X|
                              @{  }c@{  }|
                              @{  }c@{  }|
                              >{\centering\arraybackslash}X|
                              >{\centering\arraybackslash}X|
                              @{  }c@{  }|
                          ''')
    cabecalho_tabela = [
        'NOME COMPLETO', 'ENTIDADE', 'CPF', 'DATA NASC.', 'FUNÇÃO',
        'C/H SEMANAL', 'TELEFONE E E-MAIL'
    ]

    doc.append(NoEscape('{\scriptsize'))

    with doc.create(Tabularx(table_spec,
                             width_argument=WIDTH_ARGUMENT)) as tab:
        tab.add_hline()
        tab.add_row(cabecalho_tabela)
        tab.add_hline()

        membros = MembroComunidade_CursoExtensao.objects.filter(
            curso_extensao_id=projeto_extensao.id)
        for membro in membros:
            linha = [
                escape_latex(membro.nome),
                escape_latex(membro.entidade),
                escape_latex(membro.cpf),
                membro.data_nascimento.strftime('%d/%m/%Y'),
                escape_latex(membro.funcao),
                membro.carga_horaria_semanal,
                # TODO: hifenizar email
                NoEscape(r'\makecell{{ {}; \\ {} }}'.format(
                    escape_latex(membro.telefone), escape_latex(membro.email)))
            ]
            tab.add_row(linha)
            tab.add_hline()

    mdframed_plano_trabalho(doc, membros)

    doc.append(NoEscape('}'))  # volta com tamanho da fonte normal
Esempio n. 20
0
    def cria_primeira_secao(self, cabecalho):
        colunas_section_1 = Tabularx('c c c')
        colunas_section_1.add_row([MultiColumn(3, align='c')])

        info_aluno = MiniPage(width=NoEscape(r'0.25\textwidth'),
                              pos='b',
                              align='l')
        graf_media = StandAloneGraphic(filename=self.path_graf_medias,
                                       image_options='width = 180 px')
        box_medias_aluno = MiniPage(width=NoEscape(r'0.23\textwidth'),
                                    pos='b',
                                    align='l')

        info_aluno.append(f'Nome: {self.resumo[1]} {self.resumo[2]}')
        info_aluno.append(NewLine())
        info_aluno.append(f'Número: {self.resumo[5]}')
        info_aluno.append(NewLine())
        info_aluno.append(f'Série: {self.resumo[4]}º ano')
        info_aluno.append(NewLine())
        info_aluno.append(f'Turma: {self.resumo[3]}')
        info_aluno.append(NewLine())
        info_aluno.append(NewLine())
        if self.resumo[9] >= 45:
            info_aluno.append(LargeText(f'Aprovado'))
        else:
            info_aluno.append(LargeText(f'Reprovado'))

        for i in range(3):
            info_aluno.append(NewLine())

        media_final = LargeText(f'Média final: {self.resumo[9]}')
        info_aluno_medias = Tabular('c | c | c', pos='b')
        info_aluno_medias.add_row(cabecalho, mapper=[bold])
        info_aluno_medias.add_hline()
        info_aluno_medias.add_empty_row()
        info_aluno_medias.add_row(
            [self.resumo[6], self.resumo[7], self.resumo[8]])

        box_medias_aluno.append(media_final)
        for i in range(3):
            box_medias_aluno.append(NewLine())

        box_medias_aluno.append(info_aluno_medias)
        for i in range(3):
            box_medias_aluno.append(NewLine())

        colunas_section_1.add_row([info_aluno, graf_media, box_medias_aluno])

        return colunas_section_1
Esempio n. 21
0
def generate_document_versions_table(schema: PLDSchema,
                                     locale: LocaleDictionary,
                                     document: Document) -> Document:
    with document.create(Section(title=locale.revision_table)) as section:
        section: Section
        with section.create(
                Tabularx(table_spec="|l|l|X|X|X|",
                         row_height="1.4")) as tabularx:
            tabularx: Tabularx
            tabularx.add_row([
                locale.date, locale.version, locale.authors, locale.sections,
                locale.comment
            ],
                             color="gray")
            for version in schema.versions:
                tabularx.add_row([
                    version.date, version.version, ', '.join(version.authors),
                    version.sections, version.comment
                ])
    return document
Esempio n. 22
0
def tabela_discentes(doc, projeto_extensao):
    table_spec = NoEscape(r'''|>{\centering\arraybackslash}X|
                              >{\centering\arraybackslash}X|
                              @{  }c@{  }|
                              @{  }c@{  }|
                              >{\centering\arraybackslash}X|
                              @{  }c@{  }|
                          ''')
    cabecalho_tabela = [
        'NOME COMPLETO', 'CURSO', 'SÉRIE', 'TURNO', 'C/H SEMANAL',
        'TELEFONE E E-MAIL'
    ]

    doc.append(NoEscape('{\scriptsize'))

    with doc.create(Tabularx(table_spec,
                             width_argument=WIDTH_ARGUMENT)) as tab:
        tab.add_hline()
        tab.add_row(cabecalho_tabela)
        tab.add_hline()

        discentes = Discente_CursoExtensao.objects.filter(
            curso_extensao_id=projeto_extensao.id)
        for discente in discentes:
            linha = [
                escape_latex(discente.nome),
                escape_latex(discente.curso.nome),
                discente.serie,
                discente.turno.nome,
                discente.carga_horaria_semanal,
                # TODO: hifenizar email
                NoEscape(r'\makecell{{ {}; \\ {} }}'.format(
                    escape_latex(discente.telefone),
                    escape_latex(discente.email)))
            ]
            tab.add_row(linha)
            tab.add_hline()

    mdframed_plano_trabalho(doc, discentes)

    doc.append(NoEscape('}'))  # volta com tamanho normal da fonte
Esempio n. 23
0
def tabela_certificados(doc, id=None):
    with doc.create(Enumerate()) as enum:
        enum.add_item(
            NoEscape(
                r'Relacionar o nome dos participantes com direito a certificados. \\'
            ))
        table_spec = NoEscape(r'''|>{\centering\arraybackslash}X|
                                  @{  }c@{  }|
                                  @{  }c@{  }|
                                  @{  }c@{  }|
                              ''')
        cabecalho_tabela = ['NOME', 'FUNÇÃO', 'FREQUÊNCIA (%)', 'C/H TOTAL']

        with doc.create(Tabularx(table_spec,
                                 width_argument=WIDTH_ARGUMENT)) as tab:
            tab.add_hline()
            tab.add_row(cabecalho_tabela)
            tab.add_hline()

            certificados = CertificadoRelatorio.objects.filter(relatorio_id=id)
            for certificado in certificados:
                if certificado:
                    linha = [
                        escape_latex(certificado.nome), certificado.funcao,
                        certificado.frequencia, certificado.carga_horaria_total
                    ]
                    tab.add_row(linha)
                    tab.add_hline()

        doc.append(LineBreak())

        # TODO: Item 9.2: Inserir onde o certificado sera gerado: PROEX ou Centro de Coordenação / Órgão Promotor
        enum.add_item(
            NoEscape(r'Informar se os certificados devem ser emitidos: \\'))
        doc.append(
            NoEscape(
                '({}) pela PROEX \hfill ({}) pelo Centro da Coordenação ou Órgão Promotor'
                .format(PHANTOM, PHANTOM)))
Esempio n. 24
0
def tabela_palavras_chave(doc, enum, palavras):
    item(doc, enum, NoEscape(r'PALAVRAS-CHAVE: \\'))

    nro_colunas = 3
    with doc.create(Tabularx('|X|X|X|', width_argument=WIDTH_ARGUMENT)) as tab:
        tab.add_hline()

        row = []
        for i, palavra in enumerate(palavras, 1):
            row.append(
                NoEscape('{} -- {}'.format(str(i),
                                           escape_latex(palavra.nome))))

            if i % nro_colunas == 0:
                tab.add_row(row)
                del row[:]

        # Adiciona o resto dos itens à tabela
        for _ in range(nro_colunas - len(row)):
            row.append('')
        tab.add_row(row)

        tab.add_hline()
Esempio n. 25
0
def get_latex_table_type2(
    doc,
    results=None,
    dataset_name=CIFAR10Repr,
    config_names=["Clean", "PGD", "FGSM"],
    eps="0.1411764706",
):
    arch_names = ["Small", "Large"]
    method_names = ["KW", "BCOP"]
    num_archs = len(arch_names)
    num_methods = len(method_names)
    fmt = "l" + "c" * num_methods
    num_cols = 1 + num_methods
    with doc.create(Tabularx(fmt)) as data_table:
        headers = tuple(
            map(lambda x: NoEscape(boldify(x)), ("", ) + tuple(method_names)))
        data_table.add_row(headers)
        cmidrule(data_table, 1, num_cols)
        for i, arch_name in enumerate(arch_names):
            data_table.add_row((arch_name, ) + ("", ) * num_methods)
            data_table.append(
                UnsafeCommand("cmidrule", "{}-{}".format(1, num_cols)))
            for j, config_name in enumerate(config_names):
                row = (config_name, )
                row += populate_numbers([
                    results[(
                        dataset_name,
                        arch_name,
                        method_name,
                        None,
                        config_name,
                        eps,
                    )] for method_name in method_names
                ])
                data_table.add_row(row)
            if i != num_archs - 1:
                cmidrule(data_table, 1, num_cols)
Esempio n. 26
0
def add_table(doc, path, caption):
    table_data = pd.read_csv(path)

    alignment = 'X'
    n_columns = table_data.shape[1]
    fmt = '{}|{}'.format(alignment, alignment * (n_columns - 1))

    with doc.create(Table()) as table:
        with doc.create(Tabularx(fmt)) as tabularx:
            # Table header
            tabularx.add_hline()
            empty_title_columns = table_data.columns.str.contains('unnamed',
                                                                  case=False)
            title_columns = table_data.columns.values
            title_columns[empty_title_columns] = ''
            tabularx.add_row(title_columns)
            tabularx.add_hline()

            # Data
            for row in table_data.itertuples(index=False):
                tabularx.add_row(row)
            tabularx.add_hline()

        table.add_caption(caption)
Esempio n. 27
0
def save_report(model, file_name, detailed_traces=2):
    print('Saving analytics report to {}.tex and {}.pdf'.format(
        file_name, file_name))

    inference_network = model._inference_network
    iter_per_sec = inference_network._total_train_iterations / inference_network._total_train_seconds
    traces_per_sec = inference_network._total_train_traces / inference_network._total_train_seconds
    traces_per_iter = inference_network._total_train_traces / inference_network._total_train_iterations
    train_loss_initial = inference_network._history_train_loss[0]
    train_loss_final = inference_network._history_train_loss[-1]
    train_loss_change = train_loss_final - train_loss_initial
    train_loss_change_per_sec = train_loss_change / inference_network._total_train_seconds
    train_loss_change_per_iter = train_loss_change / inference_network._total_train_iterations
    train_loss_change_per_trace = train_loss_change / inference_network._total_train_traces
    valid_loss_initial = inference_network._history_valid_loss[0]
    valid_loss_final = inference_network._history_valid_loss[-1]
    valid_loss_change = valid_loss_final - valid_loss_initial
    valid_loss_change_per_sec = valid_loss_change / inference_network._total_train_seconds
    valid_loss_change_per_iter = valid_loss_change / inference_network._total_train_iterations
    valid_loss_change_per_trace = valid_loss_change / inference_network._total_train_traces

    sys.stdout.write(
        'Generating report...                                           \r')
    sys.stdout.flush()

    geometry_options = {
        'tmargin': '1.5cm',
        'lmargin': '1cm',
        'rmargin': '1cm',
        'bmargin': '1.5cm'
    }
    doc = Document('basic', geometry_options=geometry_options)
    doc.preamble.append(NoEscape(r'\usepackage[none]{hyphenat}'))
    doc.preamble.append(NoEscape(r'\usepackage{float}'))
    # doc.preamble.append(NoEscape(r'\renewcommand{\familydefault}{\ttdefault}'))

    doc.preamble.append(Command('title', 'pyprob analytics: ' + model.name))
    doc.preamble.append(
        Command(
            'date',
            NoEscape(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))))
    doc.append(NoEscape(r'\maketitle'))
    # doc.append(NoEscape(r'\small'))

    print('Analytics: Current system')
    with doc.create(Section('Current system', numbering=False)):
        with doc.create(Tabularx('ll')) as table:
            table.add_row(('pyprob version', __version__))
            table.add_row(('PyTorch version', torch.__version__))

    # doc.append(NoEscape(r'\newpage'))
    print('Analytics: Inference network')
    with doc.create(Section('Inference network', numbering=False)):
        print('Analytics: Inference network.File')
        with doc.create(Section('File')):
            with doc.create(Tabularx('ll')) as table:
                # table.add_row(('File name', file_name))
                # file_size = '{:,}'.format(os.path.getsize(file_name))
                # table.add_row(('File size', file_size + ' Bytes'))
                table.add_row(('Created', inference_network._created))
                table.add_row(('Modified', inference_network._modified))
                table.add_row(('Updates to file', inference_network._updates))
        print('Analytics: Inference network.Training')
        with doc.create(Section('Training')):
            with doc.create(Tabularx('ll')) as table:
                table.add_row(
                    ('pyprob version', inference_network._pyprob_version))
                table.add_row(
                    ('PyTorch version', inference_network._torch_version))
                table.add_row(('Trained on', inference_network._trained_on))
                table.add_row(('Total training time', '{0}'.format(
                    util.days_hours_mins_secs_str(
                        inference_network._total_train_seconds))))
                table.add_row(
                    ('Total training traces',
                     '{:,}'.format(inference_network._total_train_traces)))
                table.add_row(('Traces / s', '{:,.2f}'.format(traces_per_sec)))
                table.add_row(
                    ('Traces / iteration', '{:,.2f}'.format(traces_per_iter)))
                table.add_row(
                    ('Iterations',
                     '{:,}'.format(inference_network._total_train_iterations)))
                table.add_row(
                    ('Iterations / s', '{:,.2f}'.format(iter_per_sec)))
                table.add_row(('Optimizer', inference_network._optimizer_type))
                table.add_row(('Validation set size',
                               inference_network._valid_batch.length))
        print('Analytics: Inference network.Training loss')
        with doc.create(Subsection('Training loss')):
            with doc.create(Tabularx('ll')) as table:
                table.add_row(
                    ('Initial loss', '{:+.6e}'.format(train_loss_initial)))
                table.add_row(
                    ('Final loss', '{:+.6e}'.format(train_loss_final)))
                table.add_row(('Loss change / s',
                               '{:+.6e}'.format(train_loss_change_per_sec)))
                table.add_row(('Loss change / iteration',
                               '{:+.6e}'.format(train_loss_change_per_iter)))
                table.add_row(('Loss change / trace',
                               '{:+.6e}'.format(train_loss_change_per_trace)))
        print('Analytics: Inference network.Validation loss')
        with doc.create(Subsection('Validation loss')):
            with doc.create(Tabularx('ll')) as table:
                table.add_row(
                    ('Initial loss', '{:+.6e}'.format(valid_loss_initial)))
                table.add_row(
                    ('Final loss', '{:+.6e}'.format(valid_loss_final)))
                table.add_row(('Loss change / s',
                               '{:+.6e}'.format(valid_loss_change_per_sec)))
                table.add_row(('Loss change / iteration',
                               '{:+.6e}'.format(valid_loss_change_per_iter)))
                table.add_row(('Loss change / trace',
                               '{:+.6e}'.format(valid_loss_change_per_trace)))
        with doc.create(Figure(position='H')) as plot:
            fig = plt.figure(figsize=(10, 6))
            ax = plt.subplot(111)
            ax.plot(inference_network._history_train_loss_trace,
                    inference_network._history_train_loss,
                    label='Training')
            ax.plot(inference_network._history_valid_loss_trace,
                    inference_network._history_valid_loss,
                    label='Validation')
            ax.legend()
            plt.xlabel('Training traces')
            plt.ylabel('Loss')
            plt.grid()
            fig.tight_layout()
            plot.add_plot(width=NoEscape(r'\textwidth'))
            plot.add_caption('Loss plot.')

        print('Analytics: Inference network.Neural network modules')
        with doc.create(Section('Neural network modules')):
            with doc.create(Tabularx('ll')) as table:
                table.add_row(
                    ('Total trainable parameters',
                     '{:,}'.format(inference_network._history_num_params[-1])))
                # table.add_row(('Softmax boost', inference_network.softmax_boost))
                # table.add_row(('Dropout', inference_network.dropout))
                # table.add_row(('Standardize inputs', inference_network.standardize_observes))
            with doc.create(Figure(position='H')) as plot:
                fig = plt.figure(figsize=(10, 4))
                ax = plt.subplot(111)
                ax.plot(inference_network._history_num_params_trace,
                        inference_network._history_num_params)
                plt.xlabel('Training traces')
                plt.ylabel('Number of parameters')
                plt.grid()
                fig.tight_layout()
                plot.add_plot(width=NoEscape(r'\textwidth'))
                plot.add_caption('Number of parameters.')

            doc.append(NoEscape(r'\newpage'))
            print(
                'Analytics: Inference network.Neural network modules.All modules'
            )
            with doc.create(Subsection('All modules')):
                doc.append(str(inference_network))

            for m_name, m in inference_network.named_modules():
                if (m_name != ''):
                    regex = r'(sample_embedding_layer\(\S*\)._)|(proposal_layer\(\S*\)._)|(_observe_embedding_layer.)|(_lstm)'
                    if len(list(re.finditer(regex, m_name))) > 0:
                        # if ('_observe_embedding_layer.' in m_name) or ('sample_embedding_layer.' in m_name) or ('proposal_layer.' in m_name):
                        doc.append(NoEscape(r'\newpage'))
                        with doc.create(Subsubsection(m_name)):
                            doc.append(str(m))
                            for p_name, p in m.named_parameters():
                                if not 'bias' in p_name:
                                    with doc.create(
                                            Figure(position='H')) as plot:
                                        fig = plt.figure(figsize=(10, 10))
                                        ax = plt.subplot(111)
                                        plt.imshow(np.transpose(
                                            util.weights_to_image(p),
                                            (1, 2, 0)),
                                                   interpolation='none')
                                        plt.axis('off')
                                        plot.add_plot(
                                            width=NoEscape(r'\textwidth'))
                                        plot.add_caption(m_name + '_' + p_name)
            # doc.append(NoEscape(r'\newpage'))
            # print('Analytics: Inference network.Neural network modules.Address embeddings')
            # with doc.create(Subsection('Address embeddings')):
            #     for p_name, p in inference_network.named_parameters():
            #         if ('address_embedding' in p_name):
            #             with doc.create(Figure(position='H')) as plot:
            #                 fig = plt.figure(figsize=(10,10))
            #                 ax = plt.subplot(111)
            #                 plt.imshow(np.transpose(util.weights_to_image(p),(1,2,0)), interpolation='none')
            #                 plt.axis('off')
            #                 plot.add_plot(width=NoEscape(r'\textwidth'))
            #                 plot.add_caption(FootnoteText(p_name.replace('::', ':: ')))

        gc.collect()
        doc.append(NoEscape(r'\newpage'))
        print('Analytics: Inference network.Traces')
        with doc.create(Section('Traces')):
            with doc.create(Tabularx('ll')) as table:
                table.add_row(
                    ('Total training traces',
                     '{:,}'.format(inference_network._total_train_traces)))
            print(
                'Analytics: Inference network.Traces.Distributions encountered'
            )
            with doc.create(Subsection('Distributions encountered')):
                with doc.create(Tabularx('ll')) as table:
                    # print([v[2] for v in inference_network._address_stats.values()])
                    distributions = set([
                        v[2]
                        for v in inference_network._address_stats.values()
                    ])
                    num_distributions = len(distributions)
                    table.add_row(
                        ('Number of distributions', num_distributions))
                    table.add_empty_row()
                    for distribution in distributions:
                        table.add_row((distribution, ''))
            print('Analytics: Inference network.Traces.Addresses encountered')
            with doc.create(Subsection('Addresses encountered')):
                with doc.create(Tabularx('lX')) as table:
                    num_addresses_all = len(
                        inference_network._address_stats.keys())
                    table.add_row(('Number of addresses', num_addresses_all))
                    num_addresses_controlled = len([
                        k for k, v in inference_network._address_stats.items()
                        if v[3]
                    ])
                    num_addresses_replaced = len([
                        k for k, v in inference_network._address_stats.items()
                        if v[3] and v[4]
                    ])
                    num_addresses_observed = len([
                        k for k, v in inference_network._address_stats.items()
                        if v[5]
                    ])
                    table.add_row(
                        (TextColor('red', 'Number of addresses (controlled)'),
                         TextColor('red', num_addresses_controlled)))
                    table.add_row((TextColor('green',
                                             'Number of addresses (replaced)'),
                                   TextColor('green', num_addresses_replaced)))
                    table.add_row((TextColor('blue',
                                             'Number of addresses (observed)'),
                                   TextColor('blue', num_addresses_observed)))
                    table.add_row(
                        ('Number of addresses (uncontrolled)',
                         num_addresses_all - num_addresses_controlled -
                         num_addresses_observed))
                    table.add_empty_row()
                doc.append('\n')
                with doc.create(LongTable('llllllp{12cm}')) as table:
                    # table.add_empty_row()
                    table.add_row(FootnoteText('Count'), FootnoteText('ID'),
                                  FootnoteText('Distrib.'),
                                  FootnoteText('Ctrl.'),
                                  FootnoteText('Replace'),
                                  FootnoteText('Obs.'),
                                  FootnoteText('Address'))
                    table.add_hline()

                    # address_to_abbrev = {}
                    # abbrev_to_address =
                    # abbrev_i = 0
                    # sorted_addresses = sorted(inference_network.address_histogram.items(), key=lambda x:x[1], reverse=True)
                    plt_all_addresses = []
                    plt_all_counts = []
                    plt_all_colors = []
                    plt_controlled_addresses = []
                    plt_controlled_counts = []
                    plt_controlled_colors = []
                    address_id_to_count = {}
                    address_id_to_color = {}
                    address_id_count_total = 0
                    for address, vals in inference_network._address_stats.items(
                    ):
                        address = address.replace('::', ':: ')
                        count = vals[0]
                        address_id = vals[1]
                        distribution = vals[2]
                        control = vals[3]
                        replace = vals[4]
                        observed = vals[5]
                        plt_all_addresses.append(address_id)
                        plt_all_counts.append(1 if replace else count)
                        address_id_to_count[address_id] = count
                        address_id_count_total += count
                        if control:
                            if replace:
                                color = 'green'
                                plt_controlled_counts.append(1)
                            else:
                                color = 'red'
                                plt_controlled_counts.append(count)

                            plt_controlled_addresses.append(address_id)
                            plt_controlled_colors.append(color)
                        elif observed:
                            color = 'blue'
                            plt_controlled_addresses.append(address_id)
                            plt_controlled_colors.append(color)
                            plt_controlled_counts.append(count)
                        else:
                            color = 'black'
                        table.add_row(
                            (TextColor(color,
                                       FootnoteText('{:,}'.format(count))),
                             TextColor(color, FootnoteText(address_id)),
                             TextColor(color, FootnoteText(distribution)),
                             TextColor(color, FootnoteText(control)),
                             TextColor(color, FootnoteText(replace)),
                             TextColor(color, FootnoteText(observed)),
                             TextColor(color, FootnoteText(address))))
                        plt_all_colors.append(color)
                        address_id_to_color[address_id] = color

                gc.collect()
                with doc.create(Figure(position='H')) as plot:
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    plt_x = range(len(plt_all_addresses))
                    ax.bar(plt_x, plt_all_counts, color=plt_all_colors)
                    plt.xticks(plt_x, plt_all_addresses)
                    plt.xlabel('Address ID')
                    plt.ylabel('Count')
                    # plt.grid()
                    fig.tight_layout()
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption(
                        'Histogram of all addresses. Red: controlled, green: replaced, black: uncontrolled, blue: observed.'
                    )

                with doc.create(Figure(position='H')) as plot:
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    plt_x = range(len(plt_controlled_addresses))
                    ax.bar(plt_x,
                           plt_controlled_counts,
                           color=plt_controlled_colors)
                    plt.xticks(plt_x, plt_controlled_addresses)
                    plt.xlabel('Address ID')
                    plt.ylabel('Count')
                    # plt.grid()
                    fig.tight_layout()
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption(
                        'Histogram of controlled and observed addresses. Red: controlled, green: replaced, blue: observed.'
                    )

            gc.collect()
            print('Analytics: Inference network.Traces.Trace lengths')
            with doc.create(Subsection('Trace lengths')):
                with doc.create(Tabularx('ll')) as table:
                    trace_lengths_controlled = [
                        v[3] for v in inference_network._trace_stats.values()
                    ]
                    trace_lengths_controlled_min = min(
                        trace_lengths_controlled)
                    trace_lengths_controlled_max = max(
                        trace_lengths_controlled)
                    trace_lengths_all = [
                        v[2] for v in inference_network._trace_stats.values()
                    ]
                    trace_lengths_all_min = min(trace_lengths_all)
                    trace_lengths_all_max = max(trace_lengths_all)
                    s_controlled = 0
                    s_all = 0
                    total_count = 0
                    for _, v in inference_network._trace_stats.items():
                        trace_length_controlled = v[3]
                        trace_length_all = v[2]
                        count = v[0]
                        s_controlled += trace_length_controlled * count
                        total_count += count
                        s_all += trace_length_all * count
                    trace_length_controlled_mean = s_controlled / total_count
                    trace_length_all_mean = s_all / total_count
                    table.add_row(('Trace length min',
                                   '{:,}'.format(trace_lengths_all_min)))
                    table.add_row(('Trace length max',
                                   '{:,}'.format(trace_lengths_all_max)))
                    table.add_row(('Trace length mean',
                                   '{:.2f}'.format(trace_length_all_mean)))
                    table.add_row(
                        ('Controlled trace length min',
                         '{:,}'.format(trace_lengths_controlled_min)))
                    table.add_row(
                        ('Controlled trace length max',
                         '{:,}'.format(trace_lengths_controlled_max)))
                    table.add_row(
                        ('Controlled trace length mean',
                         '{:.2f}'.format(trace_length_controlled_mean)))
                with doc.create(Figure(position='H')) as plot:
                    plt_counter = dict(Counter(trace_lengths_all))
                    plt_lengths = [
                        i for i in range(0, trace_lengths_all_max + 1)
                    ]
                    plt_counts = [
                        plt_counter[i] if i in plt_counter else 0
                        for i in range(0, trace_lengths_all_max + 1)
                    ]
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    ax.bar(plt_lengths,
                           plt_counts,
                           width=trace_lengths_all_max / 500.)
                    plt.xlabel('Length')
                    plt.ylabel('Count')
                    # plt.yscale('log')
                    # plt.grid()
                    fig.tight_layout()
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption('Histogram of trace lengths.')
                with doc.create(Figure(position='H')) as plot:
                    plt_counter = dict(Counter(trace_lengths_controlled))
                    plt_lengths = [
                        i for i in range(0, trace_lengths_controlled_max + 1)
                    ]
                    plt_counts = [
                        plt_counter[i] if i in plt_counter else 0
                        for i in range(0, trace_lengths_controlled_max + 1)
                    ]
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    ax.bar(plt_lengths, plt_counts)
                    plt.xlabel('Length')
                    plt.ylabel('Count')
                    # plt.yscale('log')
                    # plt.grid()
                    fig.tight_layout()
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption('Histogram of controlled trace lengths.')

            gc.collect()
            print(
                'Analytics: Inference network.Traces.Unique traces encountered'
            )
            with doc.create(Subsection('Unique traces encountered')):
                detailed_traces = min(len(inference_network._trace_stats),
                                      detailed_traces)
                with doc.create(Tabularx('ll')) as table:
                    table.add_row(
                        ('Unique traces encountered',
                         '{:,}'.format(len(inference_network._trace_stats))))
                    table.add_row(('Unique traces rendered in detail',
                                   '{:,}'.format(detailed_traces)))
                doc.append('\n')
                with doc.create(LongTable('llllp{15cm}')) as table:
                    # table.add_empty_row()
                    table.add_row(FootnoteText('Count'), FootnoteText('ID'),
                                  FootnoteText('Len.'),
                                  FootnoteText('Ctrl. len.'),
                                  FootnoteText('Unique trace'))
                    table.add_hline()

                    plt_traces = []
                    plt_counts = []
                    for trace_str, vals in inference_network._trace_stats.items(
                    ):
                        count = vals[0]
                        trace_id = vals[1]
                        length_all = vals[2]
                        length_controlled = vals[3]
                        addresses_controlled = vals[4]
                        addresses_controlled_str = ' '.join(
                            addresses_controlled)
                        plt_traces.append(trace_id)
                        plt_counts.append(count)
                        table.add_row(
                            (FootnoteText('{:,}'.format(count)),
                             FootnoteText(trace_id),
                             FootnoteText('{:,}'.format(length_all)),
                             FootnoteText('{:,}'.format(length_controlled)),
                             FootnoteText(addresses_controlled_str)))

                with doc.create(Figure(position='H')) as plot:
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    plt_x = range(len(plt_traces))
                    ax.bar(plt_x, plt_counts)
                    plt.xticks(plt_x, plt_traces)
                    plt.xlabel('Trace ID')
                    plt.ylabel('Count')
                    # plt.grid()
                    fig.tight_layout()
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption('Histogram of unique traces.')

                with doc.create(Figure(position='H')) as plot:
                    sorted_trace_stats = OrderedDict(
                        sorted(dict(inference_network._trace_stats).items(),
                               key=lambda x: x[1],
                               reverse=True))
                    master_trace_pairs = {}
                    transition_count_total = 0
                    for trace_str, vals in sorted_trace_stats.items():
                        count = vals[0]
                        ta = vals[4]
                        for left, right in zip(ta, ta[1:]):
                            if (left, right) in master_trace_pairs:
                                master_trace_pairs[(left, right)] += count
                            else:
                                master_trace_pairs[(left, right)] = count
                            transition_count_total += count
                    fig = plt.figure(figsize=(10, 5))
                    ax = plt.subplot(111)
                    master_graph = pydotplus.graphviz.Dot(graph_type='digraph',
                                                          rankdir='LR')
                    transition_count_max = 0
                    for p, count in master_trace_pairs.items():
                        if count > transition_count_max:
                            transition_count_max = count
                        nodes = master_graph.get_node(p[0])
                        if len(nodes) > 0:
                            n0 = nodes[0]
                        else:
                            n0 = pydotplus.Node(p[0])
                            master_graph.add_node(n0)
                        nodes = master_graph.get_node(p[1])
                        if len(nodes) > 0:
                            n1 = nodes[0]
                        else:
                            n1 = pydotplus.Node(p[1])
                            master_graph.add_node(n1)
                        master_graph.add_edge(
                            pydotplus.Edge(n0, n1, weight=count))
                    for node in master_graph.get_nodes():
                        node.set_color('gray')
                        node.set_fontcolor('gray')
                    for edge in master_graph.get_edges():
                        edge.set_color('gray')

                    master_graph_annotated = pydotplus.graphviz.graph_from_dot_data(
                        master_graph.to_string())
                    for node in master_graph_annotated.get_nodes():
                        # color = util.rgb_to_hex(util.rgb_blend((1, 1, 1), (1, 0, 0), address_id_to_count[node.obj_dict['name']] / address_id_count_total))
                        address_id = node.obj_dict['name']
                        node.set_style('filled')
                        node.set_fillcolor(address_id_to_color[address_id])
                        node.set_color('black')
                        node.set_fontcolor('black')
                    for edge in master_graph_annotated.get_edges():
                        (left, right) = edge.obj_dict['points']
                        count = master_trace_pairs[(left, right)]
                        edge.set_label(count)
                        # color = util.rgb_to_hex((1.5*(count/transition_count_total), 0, 0))
                        edge.set_color('black')
                        edge.set_penwidth(2.5 * count / transition_count_max)

                    png_str = master_graph_annotated.create_png(
                        prog=['dot', '-Gsize=90', '-Gdpi=600'])
                    bio = BytesIO()
                    bio.write(png_str)
                    bio.seek(0)
                    img = np.asarray(mpimg.imread(bio))
                    plt.imshow(util.crop_image(img), interpolation='bilinear')
                    plt.axis('off')
                    plot.add_plot(width=NoEscape(r'\textwidth'))
                    plot.add_caption(
                        'Succession of controlled addresses (accumulated over all traces). Red: controlled, green: replaced, blue: observed.'
                    )

                for trace_str, vals in OrderedDict(
                        islice(sorted_trace_stats.items(), 0,
                               detailed_traces)).items():
                    count = vals[0]
                    trace_id = vals[1]
                    doc.append(NoEscape(r'\newpage'))
                    with doc.create(Subsubsection('Unique trace ' + trace_id)):
                        sys.stdout.write(
                            'Rendering unique trace {0}...                                       \r'
                            .format(trace_id))
                        sys.stdout.flush()

                        addresses = len(plt_controlled_addresses)
                        trace_addresses = vals[4]

                        with doc.create(Tabularx('ll')) as table:
                            table.add_row(FootnoteText('Count'),
                                          FootnoteText('{:,}'.format(count)))
                            table.add_row(
                                FootnoteText('Controlled length'),
                                FootnoteText('{:,}'.format(
                                    len(trace_addresses))))
                        doc.append('\n')

                        im = np.zeros((addresses, len(trace_addresses)))
                        for i in range(len(trace_addresses)):
                            address = trace_addresses[i]
                            address_i = plt_controlled_addresses.index(address)
                            im[address_i, i] = 1
                        truncate = 100
                        for col_start in range(0, len(trace_addresses),
                                               truncate):
                            col_end = min(col_start + truncate,
                                          len(trace_addresses))
                            with doc.create(Figure(position='H')) as plot:
                                fig = plt.figure(figsize=(20 * (
                                    (col_end + 4 - col_start) / truncate), 4))
                                ax = plt.subplot(111)
                                # ax.imshow(im,cmap=plt.get_cmap('Greys'))
                                sns.heatmap(
                                    im[:, col_start:col_end],
                                    cbar=False,
                                    linecolor='lightgray',
                                    linewidths=.5,
                                    cmap='Greys',
                                    yticklabels=plt_controlled_addresses,
                                    xticklabels=np.arange(col_start, col_end))
                                plt.yticks(rotation=0)
                                fig.tight_layout()
                                plot.add_plot(
                                    width=NoEscape(r'{0}\textwidth'.format(
                                        (col_end + 4 - col_start) / truncate)),
                                    placement=NoEscape(r'\raggedright'))

                        with doc.create(Figure(position='H')) as plot:
                            pairs = {}
                            for left, right in zip(trace_addresses,
                                                   trace_addresses[1:]):
                                if (left, right) in pairs:
                                    pairs[(left, right)] += 1
                                else:
                                    pairs[(left, right)] = 1

                            fig = plt.figure(figsize=(10, 5))
                            ax = plt.subplot(111)
                            graph = pydotplus.graphviz.graph_from_dot_data(
                                master_graph.to_string())

                            trace_address_to_count = {}
                            for address in trace_addresses:
                                if address in trace_address_to_count:
                                    trace_address_to_count[address] += 1
                                else:
                                    trace_address_to_count[address] = 1

                            transition_count_max = 0
                            for p, count in pairs.items():
                                if count > transition_count_max:
                                    transition_count_max = count
                                left_node = graph.get_node(p[0])[0]
                                right_node = graph.get_node(p[1])[0]
                                edge = graph.get_edge(p[0], p[1])[0]

                                # color = util.rgb_to_hex(util.rgb_blend((1,1,1), (1,0,0), trace_address_to_count[p[0]] / len(trace_addresses)))
                                left_node.set_style('filled')
                                left_node.set_fillcolor(
                                    address_id_to_color[p[0]])
                                left_node.set_color('black')
                                left_node.set_fontcolor('black')

                                # color = util.rgb_to_hex(util.rgb_blend((1,1,1), (1,0,0), trace_address_to_count[p[0]] / len(trace_addresses)))
                                right_node.set_style('filled')
                                right_node.set_fillcolor(
                                    address_id_to_color[p[1]])
                                right_node.set_color('black')
                                right_node.set_fontcolor('black')

                                # (left, right) = edge.obj_dict['points']
                                edge.set_label(count)
                                # color = util.rgb_to_hex((1.5*(count/len(trace_addresses)),0,0))
                                edge.set_color('black')
                                edge.set_penwidth(2.5 * count /
                                                  transition_count_max)

                            png_str = graph.create_png(
                                prog=['dot', '-Gsize=90', '-Gdpi=600'])
                            bio = BytesIO()
                            bio.write(png_str)
                            bio.seek(0)
                            img = np.asarray(mpimg.imread(bio))
                            plt.imshow(util.crop_image(img),
                                       interpolation='bilinear')
                            plt.axis('off')
                            plot.add_plot(width=NoEscape(r'\textwidth'))
                            plot.add_caption(
                                'Succession of controlled addresses (for one trace of type '
                                + trace_id +
                                '). Red: controlled, green: replaced, blue: observed.'
                            )

                        with doc.create(Tabularx('lp{16cm}')) as table:
                            table.add_row(
                                FootnoteText('Trace'),
                                FootnoteText(' '.join(trace_addresses)))

    doc.generate_pdf(file_name, clean_tex=False)
    sys.stdout.write(
        '                                                               \r')
    sys.stdout.flush()
Esempio n. 28
0
def generate_unique():
    geometry_options = {
        "head": "40pt",
        "margin": "0.5in",
        "bottom": "0.6in",
        "includeheadfoot": True
    }
    doc = Document(geometry_options=geometry_options)

    # Generating first page style
    first_page = PageStyle("firstpage")

    # Header image
    with first_page.create(Head("L")) as header_left:
        with header_left.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"),
                         pos='c')) as logo_wrapper:
            logo_file = os.path.join(os.path.dirname(__file__),
                                     'sample-logo.png')
            logo_wrapper.append(
                StandAloneGraphic(image_options="width=120px",
                                  filename=logo_file))

    # Add document title
    with first_page.create(Head("R")) as right_header:
        with right_header.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"), pos='c',
                         align='r')) as title_wrapper:
            title_wrapper.append(LargeText(bold("Bank Account Statement")))
            title_wrapper.append(LineBreak())
            title_wrapper.append(MediumText(bold("Date")))

    # Add footer
    with first_page.create(Foot("C")) as footer:
        message = "Important message please read"
        with footer.create(
                Tabularx(
                    "X X X X",
                    width_argument=NoEscape(r"\textwidth"))) as footer_table:

            footer_table.add_row(
                [MultiColumn(4, align='l', data=TextColor("blue", message))])
            footer_table.add_hline(color="blue")
            footer_table.add_empty_row()

            branch_address = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                      pos='t')
            branch_address.append("960 - 22nd street east")
            branch_address.append("\n")
            branch_address.append("Saskatoon, SK")

            document_details = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                        pos='t',
                                        align='r')
            document_details.append("1000")
            document_details.append(LineBreak())
            document_details.append(simple_page_number())

            footer_table.add_row([
                branch_address, branch_address, branch_address,
                document_details
            ])

    doc.preamble.append(first_page)
    # End first page style

    # Add customer information
    with doc.create(Tabu("X[l] X[r]")) as first_page_table:
        customer = MiniPage(width=NoEscape(r"0.49\textwidth"), pos='h')
        customer.append("Verna Volcano")
        customer.append("\n")
        customer.append("For some Person")
        customer.append("\n")
        customer.append("Address1")
        customer.append("\n")
        customer.append("Address2")
        customer.append("\n")
        customer.append("Address3")

        # Add branch information
        branch = MiniPage(width=NoEscape(r"0.49\textwidth"),
                          pos='t!',
                          align='r')
        branch.append("Branch no.")
        branch.append(LineBreak())
        branch.append(bold("1181..."))
        branch.append(LineBreak())
        branch.append(bold("TIB Cheque"))

        first_page_table.add_row([customer, branch])
        first_page_table.add_empty_row()

    doc.change_document_style("firstpage")
    doc.add_color(name="lightgray", model="gray", description="0.80")

    # Add statement table
    with doc.create(LongTabu("X[l] X[2l] X[r] X[r] X[r]",
                             row_height=1.5)) as data_table:
        data_table.add_row(
            ["date", "description", "debits($)", "credits($)", "balance($)"],
            mapper=bold,
            color="lightgray")
        data_table.add_empty_row()
        data_table.add_hline()
        row = ["2016-JUN-01", "Test", "$100", "$1000", "-$900"]
        for i in range(30):
            if (i % 2) == 0:
                data_table.add_row(row, color="lightgray")
            else:
                data_table.add_row(row)

    doc.append(NewPage())

    # Add cheque images
    with doc.create(LongTabu("X[c] X[c]")) as cheque_table:
        cheque_file = os.path.join(os.path.dirname(__file__),
                                   'chequeexample.png')
        cheque = StandAloneGraphic(cheque_file, image_options="width=200px")
        for i in range(0, 20):
            cheque_table.add_row([cheque, cheque])

    doc.generate_pdf("complex_report", clean_tex=False)
Esempio n. 29
0
    def save_latex(self,
                   uiObj,
                   Design_Check,
                   reportsummary,
                   filename,
                   rel_path,
                   Disp_2d_image,
                   Disp_3d_image,
                   module=''):
        companyname = str(reportsummary["ProfileSummary"]['CompanyName'])
        companylogo = str(reportsummary["ProfileSummary"]['CompanyLogo'])
        groupteamname = str(reportsummary["ProfileSummary"]['Group/TeamName'])
        designer = str(reportsummary["ProfileSummary"]['Designer'])
        projecttitle = str(reportsummary['ProjectTitle'])
        subtitle = str(reportsummary['Subtitle'])
        jobnumber = str(reportsummary['JobNumber'])
        client = str(reportsummary['Client'])

        does_design_exist = reportsummary['does_design_exist']
        osdagheader = '/ResourceFiles/images/Osdag_header_report.png'
        # Add document header
        geometry_options = {
            "top": "5cm",
            "hmargin": "2cm",
            "headheight": "100pt",
            "footskip": "100pt",
            "bottom": "5cm"
        }
        doc = Document(geometry_options=geometry_options, indent=False)
        doc.packages.append(Package('amsmath'))
        doc.packages.append(Package('graphicx'))
        doc.packages.append(Package('needspace'))
        doc.append(pyl.Command('fontsize', arguments=[8, 12]))
        doc.append(pyl.Command('selectfont'))

        doc.add_color('OsdagGreen', 'RGB', '153,169,36')
        doc.add_color('PassColor', 'RGB', '153,169,36')
        doc.add_color('Red', 'RGB', '255,0,0')
        doc.add_color('Green', 'RGB', '0,200,0')
        doc.add_color('FailColor', 'HTML', '933A16')
        header = PageStyle("header")
        # Create center header
        with header.create(Head("C")):
            with header.create(Tabularx('|l|p{4cm}|l|X|')) as table:
                table.add_hline()
                # MultiColumn(4)
                table.add_row((
                    MultiColumn(
                        2,
                        align='|c|',
                        data=('' if companylogo is '' else StandAloneGraphic(
                            image_options="height=0.95cm",
                            filename=companylogo))),
                    MultiColumn(2,
                                align='|c|',
                                data=[
                                    'Created with',
                                    StandAloneGraphic(
                                        image_options="width=4.0cm,height=1cm",
                                        filename=rel_path + osdagheader)
                                ]),
                ))
                table.add_hline()
                table.add_row(('Company Name', companyname, 'Project Title',
                               projecttitle),
                              color='OsdagGreen')
                table.add_hline()
                table.add_row(
                    ('Group/Team Name', groupteamname, 'Subtitle', subtitle),
                    color='OsdagGreen')
                table.add_hline()
                table.add_row(('Designer', designer, 'Job Number', jobnumber),
                              color='OsdagGreen')
                table.add_hline()
                table.add_row(
                    ('Date', time.strftime("%d /%m /%Y"), 'Client', client),
                    color='OsdagGreen')
                table.add_hline()

        # Create right footer
        with header.create(Foot("R")):
            header.append(NoEscape(r'Page \thepage'))
        #
        # doc.preamble.append(header)
        # doc.change_document_style("header")

        # Add Heading
        # with doc.create(MiniPage(align='c')):

        doc.preamble.append(header)
        doc.change_document_style("header")
        with doc.create(Section('Input Parameters')):
            with doc.create(
                    LongTable('|p{5cm}|p{2.5cm}|p{1.5cm}|p{3cm}|p{3.5cm}|',
                              row_height=1.2)) as table:
                table.add_hline()
                for i in uiObj:
                    # row_cells = ('9', MultiColumn(3, align='|c|', data='Multicolumn not on left'))
                    if i == "Selected Section Details" or i == KEY_DISP_ANGLE_LIST or i == KEY_DISP_TOPANGLE_LIST or i == KEY_DISP_CLEAT_ANGLE_LIST:
                        # if type(uiObj[i]) == list:
                        continue
                    if type(uiObj[i]) == dict:
                        table.add_hline()
                        sectiondetails = uiObj[i]
                        image_name = sectiondetails[KEY_DISP_SEC_PROFILE]

                        Img_path = '/ResourceFiles/images/' + image_name + '.png'
                        if (len(sectiondetails)) % 2 == 0:
                            # merge_rows = int(round_up(len(sectiondetails),2)/2 + 2)
                            merge_rows = int((len(sectiondetails) / 2)) + 2
                        else:
                            merge_rows = round_up((len(sectiondetails) / 2), 2)
                        if (len(sectiondetails)) % 2 == 0:
                            sectiondetails[''] = ''

                        a = list(sectiondetails.keys())
                        # index=0
                        for x in range(1, (merge_rows + 1)):
                            # table.add_row("Col.Det.",i,columndetails[i])
                            if x == 1:
                                table.add_row((
                                    MultiRow(
                                        merge_rows,
                                        data=StandAloneGraphic(
                                            image_options=
                                            "width=5cm,height=5cm",
                                            filename=rel_path + Img_path)),
                                    MultiColumn(2, align='|c|', data=a[x]),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=sectiondetails[a[x]]),
                                ))
                            elif x <= 4:
                                table.add_row((
                                    '',
                                    MultiColumn(2,
                                                align='|c|',
                                                data=NoEscape(a[x])),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=NoEscape(
                                                    sectiondetails[a[x]])),
                                ))
                            else:
                                table.add_row((
                                    '',
                                    NoEscape(a[x]),
                                    sectiondetails[a[x]],
                                    NoEscape(a[merge_rows + x - 4]),
                                    sectiondetails[a[merge_rows + x - 4]],
                                ))
                            table.add_hline(2, 5)
                    elif uiObj[i] == "TITLE":
                        table.add_hline()
                        table.add_row((MultiColumn(
                            5,
                            align='|c|',
                            data=bold(i),
                        ), ))
                        table.add_hline()
                    elif i == 'Section Size*':
                        table.add_hline()
                        table.add_row((
                            MultiColumn(
                                3,
                                align='|c|',
                                data=i,
                            ),
                            MultiColumn(2,
                                        align='|c|',
                                        data="Ref List of Input Section"),
                        ))
                        table.add_hline()
                    elif len(str(uiObj[i])) > 55 and type(
                            uiObj[i]) != pyl.math.Math:
                        str_len = len(str(uiObj[i]))
                        loop_len = round_up((str_len / 55), 1, 1)
                        for j in range(1, loop_len + 1):
                            b = 55 * j + 1
                            if j == 1:
                                table.add_row((
                                    MultiColumn(3,
                                                align='|c|',
                                                data=MultiRow(loop_len,
                                                              data=i)),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=uiObj[i][0:b]),
                                ))
                            else:
                                table.add_row((
                                    MultiColumn(3,
                                                align='|c|',
                                                data=MultiRow(loop_len,
                                                              data="")),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=uiObj[i][b - 55:b]),
                                ))
                        table.add_hline()
                    else:
                        table.add_hline()
                        table.add_row((
                            MultiColumn(3, align='|c|', data=NoEscape(i)),
                            MultiColumn(2, align='|c|', data=uiObj[i]),
                        ))
                        table.add_hline()
            for i in uiObj:
                if i == 'Section Size*' or i == KEY_DISP_ANGLE_LIST or i == KEY_DISP_TOPANGLE_LIST or i == KEY_DISP_CLEAT_ANGLE_LIST:
                    with doc.create(Subsection("List of Input Section")):
                        # with doc.create(LongTable('|p{8cm}|p{8cm}|', row_height=1.2)) as table:
                        with doc.create(Tabularx('|p{4cm}|X|',
                                                 row_height=1.2)) as table:
                            table.add_hline()
                            table.add_row((
                                MultiColumn(
                                    1,
                                    align='|c|',
                                    data=i,
                                ),
                                MultiColumn(1,
                                            align='|X|',
                                            data=uiObj[i].strip("[]")),
                            ))
                            # str_len = len(uiObj[i])
                            # loop_len = round_up((str_len/100),1,1)
                            # table.add_hline()
                            # for j in range(1,loop_len+1):
                            #     b= 100*j+1
                            #     if j ==1:
                            #         table.add_row((MultiColumn(1, align='|c|', data=i, ),
                            #                        MultiColumn(1, align='|X|', data=uiObj[i][0:b]),))
                            #     else:
                            #         table.add_row((MultiColumn(1, align='|c|', data=" ", ),
                            #                        MultiColumn(1, align='|X|', data=uiObj[i][b-100:b]),))
                            table.add_hline()

        doc.append(
            pyl.Command('Needspace', arguments=NoEscape(r'10\baselineskip')))
        doc.append(NewPage())
        count = 0
        with doc.create(Section('Design Checks')):
            with doc.create(
                    Tabularx(
                        r'|>{\centering}p{12.5cm}|>{\centering\arraybackslash}X|',
                        row_height=1.2)) as table:
                table.add_hline()
                # Fail = TextColor("FailColor", bold("Fail"))
                # Pass = TextColor("PassColor", bold("Pass"))

                if does_design_exist != True:
                    table.add_row(bold('Design Status'),
                                  color_cell("Red", bold("Fail")))
                else:
                    table.add_row(bold('Design Status'),
                                  color_cell("OsdagGreen", bold("Pass")))
                table.add_hline()

            for check in Design_Check:

                if check[0] == 'SubSection':
                    if count >= 1:
                        # doc.append(NewPage())
                        doc.append(
                            pyl.Command(
                                'Needspace',
                                arguments=NoEscape(r'10\baselineskip')))
                    with doc.create(Subsection(check[1])):
                        #########################
                        # if uiObj== "WELDImage":
                        #     table.add_hline()
                        #     table.add_row((MultiColumn(5, align='|c|', data=bold(i), ),))
                        #     table.add_hline()
                        # else:
                        #########################
                        with doc.create(LongTable(check[2], row_height=1.2)
                                        ) as table:  # todo anjali remove
                            table.add_hline()
                            table.add_row(
                                ('Check', 'Required', 'Provided', 'Remarks'),
                                color='OsdagGreen')
                            table.add_hline()
                            table.end_table_header()
                            table.add_hline()
                            count = count + 1
                elif check[0] == "Selected":
                    if count >= 1:
                        # doc.append(NewPage())
                        doc.append(
                            pyl.Command(
                                'Needspace',
                                arguments=NoEscape(r'10\baselineskip')))
                    with doc.create(Subsection(check[1])):
                        with doc.create(LongTable(check[2],
                                                  row_height=1.2)) as table:
                            table.add_hline()
                            for i in uiObj:
                                # row_cells = ('9', MultiColumn(3, align='|c|', data='Multicolumn not on left'))

                                print(i)
                                if type(
                                        uiObj[i]
                                ) == dict and i == 'Selected Section Details':
                                    table.add_hline()
                                    sectiondetails = uiObj[i]
                                    image_name = sectiondetails[
                                        KEY_DISP_SEC_PROFILE]
                                    Img_path = '/ResourceFiles/images/' + image_name + '.png'
                                    if (len(sectiondetails)) % 2 == 0:
                                        # merge_rows = int(round_up(len(sectiondetails),2)/2 + 2)
                                        merge_rows = int(
                                            round_up((len(sectiondetails) /
                                                      2), 1, 0) + 2)
                                    else:
                                        merge_rows = int(
                                            round_up((len(sectiondetails) /
                                                      2), 1, 0) + 1)
                                    print('Hi',
                                          len(sectiondetails) / 2,
                                          round_up(len(sectiondetails), 2) / 2,
                                          merge_rows)
                                    if (len(sectiondetails)) % 2 == 0:
                                        sectiondetails[''] = ''
                                    a = list(sectiondetails.keys())
                                    # index=0
                                    for x in range(1, (merge_rows + 1)):
                                        # table.add_row("Col.Det.",i,columndetails[i])
                                        if x == 1:
                                            table.add_row((
                                                MultiRow(
                                                    merge_rows,
                                                    data=StandAloneGraphic(
                                                        image_options=
                                                        "width=5cm,height=5cm",
                                                        filename=rel_path +
                                                        Img_path)),
                                                MultiColumn(2,
                                                            align='|c|',
                                                            data=NoEscape(
                                                                a[x])),
                                                MultiColumn(
                                                    2,
                                                    align='|c|',
                                                    data=NoEscape(
                                                        sectiondetails[a[x]])),
                                            ))
                                        elif x <= 4:
                                            table.add_row((
                                                '',
                                                MultiColumn(2,
                                                            align='|c|',
                                                            data=NoEscape(
                                                                a[x])),
                                                MultiColumn(
                                                    2,
                                                    align='|c|',
                                                    data=sectiondetails[a[x]]),
                                            ))
                                        else:
                                            table.add_row((
                                                '',
                                                NoEscape(a[x]),
                                                sectiondetails[a[x]],
                                                NoEscape(a[merge_rows + x -
                                                           4]),
                                                sectiondetails[a[merge_rows +
                                                                 x - 4]],
                                            ))

                                        table.add_hline(2, 5)
                            table.add_hline()
                        count = count + 1
                else:

                    if check[3] == 'Fail':
                        table.add_row((NoEscape(check[0])), check[1], check[2],
                                      TextColor("Red", bold(check[3])))
                    else:
                        table.add_row((NoEscape(check[0])), check[1], check[2],
                                      TextColor("OsdagGreen", bold(check[3])))
                    table.add_hline()

        # 2D images
        if len(Disp_2d_image) != 0:

            if module == KEY_DISP_BCENDPLATE or module == KEY_DISP_BB_EP_SPLICE:
                if does_design_exist and sys.platform != 'darwin':
                    doc.append(NewPage())
                    weld_details = rel_path + Disp_2d_image[0]
                    detailing_details = rel_path + Disp_2d_image[1]
                    stiffener_details = rel_path + Disp_2d_image[2]

                    with doc.create(Section('2D Drawings (Typical)')):

                        with doc.create(Figure()) as image:
                            image.add_image(weld_details,
                                            width=NoEscape(r'0.7\textwidth'),
                                            placement=NoEscape(r'\centering'))
                            image.add_caption(
                                'Typical Weld Details -- Beam to End Plate Connection'
                            )
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_2:
                            image_2.add_image(
                                detailing_details,
                                width=NoEscape(r'0.7\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_2.add_caption('Typical Detailing')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_3:
                            image_3.add_image(
                                stiffener_details,
                                width=NoEscape(r'0.9\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_3.add_caption('Typical Stiffener Details')
                            # doc.append(NewPage())

            elif module == KEY_DISP_BASE_PLATE:
                if does_design_exist and sys.platform != 'darwin':
                    doc.append(NewPage())
                    bp_sketch = rel_path + Disp_2d_image[0]
                    bp_detailing = rel_path + Disp_2d_image[1]
                    bp_weld = rel_path + Disp_2d_image[2]
                    bp_anchor = rel_path + Disp_2d_image[3]
                    bp_key = rel_path + Disp_2d_image[4]

                    with doc.create(Section('2D Drawings (Typical)')):
                        with doc.create(Figure()) as image_1:
                            image_1.add_image(
                                bp_sketch,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_1.add_caption('Typical Base Plate Details')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_2:
                            image_2.add_image(
                                bp_detailing,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_2.add_caption('Typical Base Plate Detailing')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_3:
                            image_3.add_image(
                                bp_weld,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_3.add_caption('Typical Weld Details')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_4:
                            image_4.add_image(
                                bp_anchor,
                                width=NoEscape(r'0.5\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_4.add_caption('Typical Anchor Bolt Details')
                            # doc.append(NewPage())

                        if len(Disp_2d_image[-1]) > 0:
                            with doc.create(Figure()) as image_5:
                                image_5.add_image(
                                    bp_key,
                                    width=NoEscape(r'0.9\textwidth'),
                                    placement=NoEscape(r'\centering'))
                                image_5.add_caption(
                                    'Typical Shear Key Details')
                                # doc.append(NewPage())

        if does_design_exist and sys.platform != 'darwin':
            doc.append(NewPage())
            Disp_top_image = "/ResourceFiles/images/top.png"
            Disp_side_image = "/ResourceFiles/images/side.png"
            Disp_front_image = "/ResourceFiles/images/front.png"
            view_3dimg_path = rel_path + Disp_3d_image
            view_topimg_path = rel_path + Disp_top_image
            view_sideimg_path = rel_path + Disp_side_image
            view_frontimg_path = rel_path + Disp_front_image
            with doc.create(Section('3D Views')):
                with doc.create(
                        Tabularx(
                            r'|>{\centering}X|>{\centering\arraybackslash}X|',
                            row_height=1.2)) as table:
                    view_3dimg_path = rel_path + Disp_3d_image
                    view_topimg_path = rel_path + Disp_top_image
                    view_sideimg_path = rel_path + Disp_side_image
                    view_frontimg_path = rel_path + Disp_front_image
                    table.add_hline()
                    table.add_row([
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_3dimg_path),
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_topimg_path)
                    ])
                    table.add_row('(a) 3D View', '(b) Top View')
                    table.add_hline()
                    table.add_row([
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_sideimg_path),
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_frontimg_path)
                    ])
                    table.add_row('(c) Side View', '(d) Front View')
                    table.add_hline()
                # with doc.create(Figure(position='h!')) as view_3D:
                #     view_3dimg_path = rel_path + Disp_3d_image
                #     # view_3D.add_image(filename=view_3dimg_path, width=NoEscape(r'\linewidth'))
                #
                #     view_3D.add_image(filename=view_3dimg_path,width=NoEscape(r'\linewidth,height=6.5cm'))
                #
                #     view_3D.add_caption('3D View')

        with doc.create(Section('Design Log')):
            doc.append(
                pyl.Command('Needspace',
                            arguments=NoEscape(r'10\baselineskip')))
            logger_msgs = reportsummary['logger_messages'].split('\n')
            for msg in logger_msgs:
                if ('WARNING' in msg):
                    colour = 'blue'
                elif ('INFO' in msg):
                    colour = 'OsdagGreen'
                elif ('ERROR' in msg):
                    colour = 'red'
                else:
                    continue
                doc.append(TextColor(colour, '\n' + msg))
        try:
            doc.generate_pdf(filename, compiler='pdflatex', clean_tex=False)
        except:
            pass
Esempio n. 30
0
def generate_unique_dw(final_hvs1, final_hvs2, hl1_ch1, hl2_ch1, hl1_ch2,
                       hl2_ch2, sectors):
    geometry_options = {
        "head": "40pt",
        "margin": "0.5in",
        "bottom": "1.0in",
        "includeheadfoot": True
    }
    doc = Document(indent=False, geometry_options=geometry_options)

    # Generating first page style
    first_page = PageStyle("firstpage")

    # Header image
    with first_page.create(Head("L")) as header_left:
        with header_left.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"),
                         pos='c')) as logo_wrapper:
            logo_file = os.path.join(os.path.dirname(__file__),
                                     '../cernlogo.png')
            logo_wrapper.append(
                StandAloneGraphic(image_options="width=80px",
                                  filename=logo_file))

    # Add document title
    with first_page.create(Head("R")) as right_header:
        with right_header.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"), pos='c',
                         align='r')) as title_wrapper:
            title_wrapper.append(LargeText(bold("ATLAS New Small Wheel")))
            title_wrapper.append(LineBreak())
            title_wrapper.append(bold("Integration Tests"))
            title_wrapper.append(LineBreak())
            title_wrapper.append(bold(now.strftime("%d-%m-%Y")))
            title_wrapper.append(LineBreak())
            title_wrapper.append("\n")
            title_wrapper.append(
                LargeText(bold("Double Wedge: " + str(DW_name))))
            title_wrapper.append(LineBreak())

    # Add footer
    with first_page.create(Foot("C")) as footer:

        with footer.create(
                Tabularx(
                    "X X X ",
                    width_argument=NoEscape(r"\textwidth"))) as footer_table:

            footer_table.add_empty_row()

            footer_table.add_hline(color="blue")

            branch_address1 = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                       pos='t')
            branch_address1.append("Lorenzo Pezzotti")
            branch_address1.append("\n")
            branch_address1.append("*****@*****.**")

            branch_address2 = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                       pos='t')
            branch_address2.append("Alan Peyaud")
            branch_address2.append("\n")
            branch_address2.append("*****@*****.**")

            branch_address3 = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                       pos='t')
            branch_address3.append("Ivan Gnesi")
            branch_address3.append("\n")
            branch_address3.append("*****@*****.**")

            document_details = MiniPage(width=NoEscape(r"0.2\textwidth"),
                                        pos='t',
                                        align='r')
            document_details.append(" ")
            document_details.append(LineBreak())
            document_details.append(simple_page_number())

            footer_table.add_row(
                [branch_address1, branch_address2, branch_address3])

    doc.append(first_page)
    # End first page style
    redcircle = glob.glob("redcircle.png")
    redcircle = StandAloneGraphic(redcircle, image_options="width=220px")

    # Add customer information
    with doc.create(Tabu("X[r]")) as first_page_table:
        first_page_table.add_empty_row()

    doc.change_document_style("firstpage")
    doc.add_color(name="lightgray", model="gray", description="0.80")

    doc.append("\n")
    doc.append(timeslot)
    doc.append(LineBreak())

    # IP
    with doc.create(Section('IP SIDE', numbering=False)):
        # first chamber
        # Verify if its SM1, LM1, SM1 or LM2
        if chambername1IP[0:3] == "SM1" or chambername2HO[0:3] == "LM1":
            limit = 10
        else:
            limit = 6

        with doc.create(
                Subsection("Chambers: " + chambername1IP + " + " +
                           chambername2IP,
                           numbering=False)):
            with doc.create(Subsection(chambername1IP, numbering=False)):
                with doc.create(
                        LongTabu("|X[l]|X[r]|X[r]|X[r]|X[r]|X[r]|X[r]|",
                                 row_height=1.5)) as data_table:
                    data_table.add_hline()
                    data_table.add_row(
                        ["Sector", "L1", "L2", "L3", "L4", "HL1", "HL2"],
                        mapper=bold,
                        color="lightgray")
                    data_table.add_hline()
                    row = ["blank", "l1", "l2", "l3", "l4", "hl1", "hl2"]
                    for i, hv in enumerate(final_hvs1[0]):
                        hl1_str = ""
                        hl2_str = ""
                        l1 = ""
                        l2 = ""
                        l3 = ""
                        l4 = ""
                        # assign each sector to a line
                        if hv == hl1_ch1:
                            hl1_str = str(hl1_ch1)
                        elif hv == hl2_ch1:
                            hl2_str = str(hl2_ch1)
                        elif i > limit - 1 + limit * 2:
                            l4 = "570"
                        elif i > limit - 1 + limit:
                            l3 = "570"
                        elif i > limit - 1:
                            l2 = "570"
                        else:
                            l1 = "570"
                        if (i % 2) == 0:
                            data_table.add_row([
                                str(sectors[0][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ])
                        else:
                            data_table.add_row([
                                str(sectors[0][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ],
                                               color="lightgray")
                    data_table.add_hline()

            # second chamber
            if chambername2IP[0:3] == "SM1" or chambername2HO[0:3] == "LM1":
                limit = 10
            else:
                limit = 6
            with doc.create(Subsection(chambername2IP, numbering=False)):
                with doc.create(
                        LongTabu("|X[l]|X[r]|X[r]|X[r]|X[r]|X[r]|X[r]|",
                                 row_height=1.5)) as data_table2:
                    data_table2.add_hline()
                    data_table2.add_row(
                        ["Sector", "L1", "L2", "L3", "L4", "HL1", "HL2"],
                        mapper=bold,
                        color="lightgray")
                    data_table2.add_hline()
                    row = ["blank", "l1", "l2", "l3", "l4", "hl1", "hl2"]
                    for i, hv in enumerate(final_hvs1[1]):
                        hl1_str = ""
                        hl2_str = ""
                        l1 = ""
                        l2 = ""
                        l3 = ""
                        l4 = ""
                        # assign each sector to a line
                        if hv == hl1_ch1:
                            hl1_str = str(hl1_ch1)
                        elif hv == hl2_ch1:
                            hl2_str = str(hl2_ch1)
                        elif i > limit - 1 + limit * 2:
                            l4 = "570"
                        elif i > limit - 1 + limit:
                            l3 = "570"
                        elif i > limit - 1:
                            l2 = "570"
                        else:
                            l1 = "570"

                        if (i % 2) == 0:
                            data_table2.add_row([
                                str(sectors[1][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ])
                        else:
                            data_table2.add_row([
                                str(sectors[1][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ],
                                                color="lightgray")
                    data_table2.add_hline()
    # HO
    # Swap R an L
    final_hvs2[0] = swap(final_hvs2[0])
    final_hvs2[1] = swap(final_hvs2[1])
    if chambername1HO[0:3] == "SM1" or chambername2HO[0:3] == "LM1":
        limit = 10
    else:
        limit = 6
    doc.append(NewPage())
    with doc.create(Section('HO SIDE', numbering=False)):
        # first chamber
        with doc.create(
                Subsection("Chambers: " + chambername1HO + " + " +
                           chambername2HO,
                           numbering=False)):
            with doc.create(Subsection(chambername1HO, numbering=False)):
                with doc.create(
                        LongTabu("|X[l]|X[r]|X[r]|X[r]|X[r]|X[r]|X[r]|",
                                 row_height=1.5)) as data_table3:
                    data_table3.add_hline()
                    data_table3.add_row(
                        ["Sector", "L1", "L2", "L3", "L4", "HL1", "HL2"],
                        mapper=bold,
                        color="lightgray")
                    data_table3.add_hline()
                    row = ["blank", "l1", "l2", "l3", "l4", "hl1", "hl2"]
                    for i, hv in enumerate(final_hvs2[0]):
                        hl1_str = ""
                        hl2_str = ""
                        l1 = ""
                        l2 = ""
                        l3 = ""
                        l4 = ""
                        # assign each sector to a line
                        if hv == hl1_ch2:
                            hl1_str = str(hl1_ch2)
                        elif hv == hl2_ch2:
                            hl2_str = str(hl2_ch2)
                        elif i > limit - 1 + limit * 2:
                            l4 = "570"
                        elif i > limit - 1 + limit:
                            l3 = "570"
                        elif i > limit - 1:
                            l2 = "570"
                        else:
                            l1 = "570"
                        if (i % 2) == 0:
                            data_table3.add_row([
                                str(sectors[2][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ])
                        else:
                            data_table3.add_row([
                                str(sectors[2][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ],
                                                color="lightgray")
                    data_table3.add_hline()

            # second chamber
            if chambername2HO[0:3] == "SM1" or chambername2HO[0:3] == "LM1":
                limit = 10
            else:
                limit = 6
            with doc.create(Subsection(chambername2HO, numbering=False)):
                with doc.create(
                        LongTabu("|X[l]|X[r]|X[r]|X[r]|X[r]|X[r]|X[r]|",
                                 row_height=1.5)) as data_table4:
                    data_table4.add_hline()
                    data_table4.add_row(
                        ["Sector", "L1", "L2", "L3", "L4", "HL1", "HL2"],
                        mapper=bold,
                        color="lightgray")
                    data_table4.add_hline()
                    row = ["blank", "l1", "l2", "l3", "l4", "hl1", "hl2"]
                    for i, hv in enumerate(final_hvs2[1]):
                        hl1_str = ""
                        hl2_str = ""
                        l1 = ""
                        l2 = ""
                        l3 = ""
                        l4 = ""
                        # assign each sector to a line
                        if hv == hl1_ch2:
                            hl1_str = str(hl1_ch2)
                        elif hv == hl2_ch2:
                            hl2_str = str(hl2_ch2)
                        elif i > limit - 1 + limit * 2:
                            l4 = "570"
                        elif i > limit - 1 + limit:
                            l3 = "570"
                        elif i > limit - 1:
                            l2 = "570"
                        else:
                            l1 = "570"

                        if (i % 2) == 0:
                            data_table4.add_row([
                                str(sectors[3][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ])
                        else:
                            data_table4.add_row([
                                str(sectors[3][i]), l1, l2, l3, l4, hl1_str,
                                hl2_str
                            ],
                                                color="lightgray")
                    data_table4.add_hline()
    doc.generate_pdf("complex_report_DW", clean_tex=False, compiler='pdflatex')
Esempio n. 31
0
    def generar(self, Carrera, Asignatura, Curso, listaPreguntas, aleatorio):

        today = str(self.__fecha)

        geometry_options = {
            "head": "48pt",
            "margin": "0.5in",
            "bottom": "0.6in",
            "includeheadfoot": True
        }

        doc = Document(geometry_options=geometry_options)

        header = PageStyle("header")  # DEFINIMOS LA VARIABLE CON ESTILO

        #image_filename = os.path.join(os.path.dirname("__file__"), '../static/zigmap.png') # IMAGEN UNAB

        #data_folder = os.path.join("../", "static/zigmap.png")

        #file_to_open = os.path.join(data_folder, "zigmap.png")

        #image_filename = data_folder
        image_filename = "zigmap.png"

        # Generating first page style
        first_page = PageStyle("firstpage")

        # Header image
        with first_page.create(Head("L")) as header_left:

            with header_left.create(
                    SubFigure(
                        position='L',
                        width=NoEscape(r'0.10\linewidth'))) as logo_wrapper:

                print("IMAGEN")
                #logo_wrapper.add_image('zigmap.png', width=NoEscape(r'\linewidth'))

        # Add document title
        with first_page.create(Head("C")) as center_header:
            with center_header.create(
                    MiniPage(width=NoEscape(r"0.49\textwidth"),
                             pos='c',
                             align='c')) as title_wrapper:
                title_wrapper.append(LargeText(bold(self.__nombre)))

        # Add document title
        with first_page.create(Head("R")) as right_header:
            with right_header.create(
                    MiniPage(width=NoEscape(r"0.49\textwidth"),
                             pos='c',
                             align='r')) as title_wrapper:
                #title_wrapper.append(LargeText(bold("Solemne II")))
                #title_wrapper.append(LineBreak())
                title_wrapper.append(MediumText(bold(
                    Asignatura.get_nombreA())))

        # Add footer
        with first_page.create(Foot("C")) as footer:
            message = "Programación II"
            with footer.create(
                    Tabularx("X X X X", width_argument=NoEscape(
                        r"\textwidth"))) as footer_table:

                footer_table.add_row([
                    MultiColumn(4, align='l', data=TextColor("black", message))
                ])
                footer_table.add_hline(color="black")
                footer_table.add_empty_row()

                branch_address = MiniPage(width=NoEscape(r"0.45\textwidth"),
                                          pos='t',
                                          align='l')
                branch_address.append(
                    MediumText("Facultad de " + Carrera.get_facultad()))
                branch_address.append("\n")
                branch_address.append("")

                branch_address2 = MiniPage(width=NoEscape(r"0.10\textwidth"),
                                           pos='t')
                branch_address2.append("")
                branch_address2.append("\n")
                branch_address2.append("")

                document_details = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                            pos='t',
                                            align='r')
                document_details.append(self.__fecha)
                document_details.append(LineBreak())
                document_details.append("")  # simple_page_number()

                footer_table.add_row([
                    branch_address, branch_address2, branch_address2,
                    document_details
                ])

        doc.preamble.append(first_page)
        # End first page style

        ############################################

        ####################################################
        # Add customer information
        with doc.create(Tabu("X[l] X[r]")) as first_page_table:
            customer = MiniPage(width=NoEscape(r"0.55\textwidth"), pos='h')
            customer.append("Nombre: ___________________________________")
            customer.append("\n")
            customer.append("    Rut:  _______________")
            customer.append("\n")
            customer.append("Nota: ______________")
            customer.append("\n")
            customer.append("\n")

            # Add branch information
            branch = MiniPage(width=NoEscape(r"0.35\textwidth"),
                              pos='t!',
                              align='r')

            if self.__unidad == "unidad1":
                branch.append(bold("Unidad: 1"))
            elif self.__unidad == "unidad2":
                branch.append(bold("Unidad: 2"))
            else:
                branch.append(bold("Unidades: 1 & 2"))
            branch.append(LineBreak())

            branch.append(bold("Curso: "))
            branch.append(bold(Curso.get_cod()))
            branch.append(LineBreak())

            branch.append(bold("Ponderación: "))
            branch.append(bold(self.__ponderacion + "%"))
            branch.append(LineBreak())

            first_page_table.add_row([customer, branch])

        doc.append(LargeText(bold("Indicaciones:")))

        # INDICACIONES

        with doc.create(Itemize()) as itemize:
            doc.append(LineBreak())
            itemize.add_item(
                " Lea atentamente los enunciados de cada pregunta antes de contestar."
            )
            itemize.add_item(
                " Conteste su evaluación en silencio, está prohibido conversar o gesticulizar en la sala durante la prueba."
            )
            itemize.add_item(
                " Dispone de 90 minutos para realizar esta evaluación.")
            itemize.add_item(
                " Marque la alternativa con lapiz pasta, no se aceptan marcadas con lapiz grafito."
            )
            itemize.add_item(
                " Utilice solamente sus materiales, está prohibido solicitar materiales a sus compañeros."
            )
            # you can append to existing items
            itemize.append(Command("ldots"))

        doc.change_document_style("firstpage")
        doc.add_color(name="lightgray", model="gray", description="0.80")
        customer.append(LineBreak())

        now = str(datetime.today())

        for p in listaPreguntas:
            with doc.create(Section('Pregunta - ' + p.get_tipo())):

                doc.append(p.get_contenido())

        doc.append(NewPage())

        nombreFile = str(
            self.__nombre) + " - " + self.get_fecha() + " - " + str(aleatorio)

        print(
            "--------------------------ANTES DE DIR------------------------------"
        )

        try:
            # GENERANDO PDF
            doc.generate_pdf(filepath="tests/" + nombreFile,
                             clean_tex=True,
                             clean=True)
            #doc.generate_pdf(dirName + "/" + nombreFile, clean_tex=False)

        except FileNotFoundError as e:
            doc.generate_pdf(filepath="../tests/" + nombreFile,
                             clean_tex=True,
                             clean=True)