Skip to content

publish

publish

Package for the final PDF publication phase for a new version.

report_condensed_v2

Build PDF and equivalent CSV table structures for version 2+ using the new table format.

This module uses the borb library to produce the PDF document.

APOE_MAPPINGS = list(zip('abcdefghij'[:len(constants.DEFAULT_RPS)], sorted(constants.DEFAULT_RPS))) module-attribute

LOCATION_LIMIT = 0 module-attribute

MAX_PAGE_BLOCKS = 4 module-attribute

OUTPUT_FOLDER = Path(RESOURCES_FOLDER).parent / 'reports' / 'v_cbc' module-attribute

PRODUCE_CSV = True module-attribute

SITE_CLASSES = list(constants.SITE_CLASSES.keys()) module-attribute

WATERMARK_ENABLED = True module-attribute

grid_df = combo_grid_table() module-attribute

medium_font = TrueTypeFont.true_type_font_from_file(open(Path(RESOURCES_FOLDER) / 'fonts/static/OpenSans-Medium.ttf', 'rb').read()) module-attribute

named_df = combo_named_table() module-attribute

build_pdf_report_pages(location_df: pd.DataFrame, report_name: str, table_description: str, is_final: bool = False, location_limit: int = 0, modify_locations: bool = True)

Source code in nzssdt_2023/publish/report_condensed_v2.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
def build_pdf_report_pages(
    location_df: pd.DataFrame,
    report_name: str,
    table_description: str,
    is_final: bool = False,
    location_limit: int = 0,
    modify_locations: bool = True,
):
    print("build_pdf_report_pages", report_name)
    ### PDF
    table_rows = generate_table_rows(location_df, modify_locations, location_limit)
    for idx, chunk in enumerate(chunks(table_rows, MAX_PAGE_BLOCKS)):
        yield build_report_page(
            report_name,
            table_description,
            list(chunk),
            table_part=idx + 1,
            is_final=is_final,
        )

build_report_page(table_id: str, table_description, rowdata=List[List], table_part: int = 1, is_final: bool = False, print_footer: bool = False)

Source code in nzssdt_2023/publish/report_condensed_v2.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def build_report_page(
    table_id: str,
    table_description,
    rowdata=List[List],
    table_part: int = 1,
    is_final: bool = False,
    print_footer: bool = False,
):

    # create Document (needed for borbs page layout engine )
    doc: Document = Document()

    # create Page
    PAGE_SIZE = PageSize.A4_LANDSCAPE
    page: Page = Page(
        width=PAGE_SIZE.value[0],
        height=PAGE_SIZE.value[1],
    )
    layout: PageLayout = SingleColumnLayout(page)

    # add Page to Document
    doc.add_page(page)

    # add watermark
    if not is_final and WATERMARK_ENABLED:
        Watermark(
            text="DRAFT " + time.strftime("%Y-%m-%d %H:%M"),
            # font="Helvetica-bold",
            # font_size=Decimal(18),
            # angle_in_degrees=27.5,
            horizontal_alignment=Alignment.CENTERED,
            vertical_alignment=Alignment.MIDDLE,
            font_color=HexColor("070707"),
        ).paint(page, None)

    heading = f"Table {table_id} - Site demand parameters {table_description}"
    if table_part > 1:
        heading += " (continued)"
    layout.add(
        Paragraph(
            heading,
            font="Helvetica-bold",
            font_size=Decimal(10),
            horizontal_alignment=Alignment.LEFT,
        )
    )

    # create a FixedColumnWidthTable
    # print(f"len(rowdata): {len(rowdata)}")
    table = FixedColumnWidthTable(
        number_of_columns=4 + (6 * 4),  # 4 intro, + 4 parameters per siteclass = 28
        number_of_rows=2 + (len(rowdata) * len(constants.DEFAULT_RPS)),
        # adjust the ratios of column widths for this FixedColumnWidthTable
        column_widths=[Decimal(1.7), Decimal(0.8), Decimal(0.5), Decimal(0.5)]
        + 6 * [Decimal(0.5), Decimal(0.5), Decimal(0.5), Decimal(0.5)],
    )

    def add_row_0(table: FixedColumnWidthTable):
        table.add(TableCell(Paragraph(""), column_span=4))
        for sss in SITE_CLASSES:
            table.add(
                TableCell(
                    Paragraph(
                        f"Site Class {sss}",
                        font="Helvetica-bold",
                        font_size=Decimal(9),
                        horizontal_alignment=Alignment.CENTERED,
                        vertical_alignment=Alignment.BOTTOM,
                    ),
                    column_span=4,
                )
            )
        return table

    def searchable_ascii_name(macronised, non_maconrised):
        """return just the macronised substituions for searching"""
        macronised_words = macronised.split(" ")
        non_macronised_words = non_maconrised.split(" ")
        assert len(macronised_words) == len(non_macronised_words)

        res = []
        for idx, word in enumerate(macronised_words):
            if word == non_macronised_words[idx]:
                continue
            else:
                res.append(non_macronised_words[idx])
        # print("searchable name:", " ".join(res))
        return " ".join(res)

    def add_row_1(table: FixedColumnWidthTable):
        table.add(
            TableCell(
                Paragraph(
                    "Location" if table_id == "3.1" else "Grid point (lat-lon)",
                    font="Helvetica-bold",
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.LEFT,
                )
            )
        ).add(
            TableCell(
                Paragraph(
                    "APoE",
                    font="Helvetica-bold",
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.CENTERED,
                )
            )
        ).add(
            TableCell(
                Paragraph(
                    "M",
                    font="Helvetica-bold",
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.CENTERED,
                )
            )
        ).add(
            TableCell(
                Paragraph(
                    "D",
                    font="Helvetica-bold",
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.CENTERED,
                )
            )
        )
        for sss in SITE_CLASSES:
            table.add(
                Paragraph(
                    "PGA",
                    font="Helvetica-bold-oblique",
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.CENTERED,
                )
            ).add(
                HeterogeneousParagraph(
                    [
                        ChunkOfText(
                            "S", font="Helvetica-bold-oblique", font_size=Decimal(8)
                        ),
                        ChunkOfText(
                            "a,s",
                            font="Helvetica-bold",
                            font_size=Decimal(8),
                            vertical_alignment=Alignment.BOTTOM,
                        ),
                    ],
                    horizontal_alignment=Alignment.CENTERED,
                )
            ).add(
                HeterogeneousParagraph(
                    [
                        ChunkOfText(
                            "T",
                            font="Helvetica-bold-oblique",
                            font_size=Decimal(8),
                        ),
                        ChunkOfText(
                            "c",
                            font="Helvetica-bold",
                            font_size=Decimal(8),
                            vertical_alignment=Alignment.BOTTOM,
                        ),
                    ],
                    horizontal_alignment=Alignment.CENTERED,
                )
            ).add(
                HeterogeneousParagraph(
                    [
                        ChunkOfText(
                            "T",
                            font="Helvetica-bold-oblique",
                            font_size=Decimal(8),
                        ),
                        ChunkOfText(
                            "d",
                            font="Helvetica-bold",
                            font_size=Decimal(8),
                            vertical_alignment=Alignment.BOTTOM,
                        ),
                    ],
                    horizontal_alignment=Alignment.CENTERED,
                )
            )
        return table

    # table header rows
    table = add_row_0(table)
    table = add_row_1(table)

    # data rows
    for row in rowdata:
        # row[0] is location
        print("@@@ row:", row)

        if row[0][1] == row[0][0]:
            location_str = row[0][0]
        else:
            location_str = (
                row[0][0] + ', "' + searchable_ascii_name(row[0][0], row[0][1]) + '"'
            )  # add 2nd chunk for searching anglicised names (no macrons)
        print(f"build_report_page location_str: {location_str}")
        table.add(
            TableCell(
                Paragraph(
                    location_str,
                    # font="Helvetica",
                    font=medium_font,
                    font_size=Decimal(8),
                    horizontal_alignment=Alignment.LEFT,
                    vertical_alignment=Alignment.MIDDLE,
                ),
                row_span=len(constants.DEFAULT_RPS),
            )
        )

        for subrow in row[1]:
            for cell in subrow:
                try:
                    table.add(
                        TableCell(
                            Paragraph(
                                str(cell),
                                font="Helvetica",
                                font_size=Decimal(8),
                                horizontal_alignment=Alignment.CENTERED,
                                vertical_alignment=Alignment.MIDDLE,
                            ),
                            # padding_top=Decimal(2.0),
                            # border_width=Decimal(0.5),
                            # preferred_height=Decimal(50)
                        )
                    )
                except Exception:
                    print(f"bang! `{cell}`")

    table.set_padding_on_all_cells(
        padding_top=Decimal(2.5),
        padding_right=Decimal(1.5),
        padding_bottom=Decimal(1.0),
        padding_left=Decimal(1.5),
    )
    table.set_border_width_on_all_cells(border_width=Decimal(0.5))
    layout.add(table)

    # page footer
    if print_footer:
        layout.add(
            Paragraph(
                f"page {table_part}",
                font="Helvetica",
                font_size=Decimal(9),
                horizontal_alignment=Alignment.CENTERED,
                vertical_alignment=Alignment.BOTTOM,
            )
        )

    return page

chunks(items, chunk_size)

Source code in nzssdt_2023/publish/report_condensed_v2.py
396
397
398
399
def chunks(items, chunk_size):
    iterator = iter(items)
    while chunk := list(islice(iterator, chunk_size)):
        yield chunk

combo_grid_table()

Source code in nzssdt_2023/publish/report_condensed_v2.py
522
523
524
525
526
def combo_grid_table():
    filepath = (
        Path(RESOURCES_FOLDER) / "v_cbc" / "first_10_grid_locations_combo.json"
    )
    return pd.read_json(filepath, orient="table")

combo_named_table()

Source code in nzssdt_2023/publish/report_condensed_v2.py
516
517
518
519
520
def combo_named_table():
    filepath = (
        Path(RESOURCES_FOLDER) / "v_cbc" / "first_10_named_locations_combo.json"
    )
    return pd.read_json(filepath, orient="table")

format_D(value, apoe: int) -> Union[str, int]

NB NaN in the source dataframe has different meanings, depending on the apoe...

Source code in nzssdt_2023/publish/report_condensed_v2.py
319
320
321
322
323
def format_D(value, apoe: int) -> Union[str, int]:
    """NB NaN in the source dataframe has different meanings, depending on the apoe..."""
    if pd.isna(value):
        return "n/a" if apoe < 500 else ">20"
    return str(value)

generate_csv_rows(combo_table: pd.DataFrame, modify_locations: bool = False) -> Iterator

Source code in nzssdt_2023/publish/report_condensed_v2.py
385
386
387
388
389
390
391
392
393
def generate_csv_rows(
    combo_table: pd.DataFrame,
    modify_locations: bool = False,
) -> Iterator:
    count = 0
    for location in combo_table.Location.unique():
        for block in generate_location_block(combo_table, location):
            yield list(locations_tuple_padded(location, modify_locations)) + block
        count += 1

generate_location_block(combo_table: pd.DataFrame, location: str) -> Iterator

build a location block, with one row per apoe

Source code in nzssdt_2023/publish/report_condensed_v2.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
def generate_location_block(combo_table: pd.DataFrame, location: str) -> Iterator:
    """build a location block, with one row per apoe"""
    location_df = combo_table[combo_table.Location == location]
    site_classes = location_df["Site Class"].unique().tolist()
    apoes = location_df["APoE (1/n)"].unique().tolist()

    for apoe in apoes:
        rec = location_df[
            (location_df["Site Class"] == "I") & (location_df["APoE (1/n)"] == apoe)
        ]  # get site_class I

        d_str = format_D(rec["D"].to_list()[0], apoe)
        row = [f"1/{apoe}", rec["M"].to_list()[0], d_str]
        for site_class in site_classes:
            apoe_df = location_df[
                (location_df["APoE (1/n)"] == apoe)
                & (location_df["Site Class"] == site_class)
            ]
            for sc_tup in apoe_df.itertuples():
                row += [
                    round(sc_tup.PGA, 2),
                    round(sc_tup.Sas, 2),
                    round(sc_tup.Tc, 2),
                    round(sc_tup.Td, 1),
                ]
        print("generate_location_block -> row", row, location)
        yield (row)

generate_table_rows(combo_table: pd.DataFrame, modify_locations: bool = False, location_limit: int = 0) -> Iterator

Source code in nzssdt_2023/publish/report_condensed_v2.py
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
def generate_table_rows(
    combo_table: pd.DataFrame, modify_locations: bool = False, location_limit: int = 0
) -> Iterator:
    count = 0
    for location in combo_table.Location.unique():

        count += 1
        yield (
            locations_tuple_padded(location, modify_locations),
            generate_location_block(combo_table, location),
        )

        if count % 10 == 0:
            print(f"row count: {count}")

        if location_limit and (count >= location_limit):
            break

locations_tuple_padded(location: str, modify_locations: bool)

Source code in nzssdt_2023/publish/report_condensed_v2.py
355
356
357
358
359
360
361
362
363
def locations_tuple_padded(location: str, modify_locations: bool):
    if not modify_locations:
        return get_name_with_macrons(location), location

    # spaces allow wrapping to work, # non-macronised for searching
    return (
        get_name_with_macrons(location).replace("-", " - "),
        location.replace("-", " - "),
    )

publish_gridded(location_df: pd.DataFrame, output_folder: Path, produce_csv: bool = True, is_final: bool = False, location_limit: int = 0)

Source code in nzssdt_2023/publish/report_condensed_v2.py
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
def publish_gridded(
    location_df: pd.DataFrame,
    output_folder: Path,
    produce_csv: bool = True,
    is_final: bool = False,
    location_limit: int = 0,
):
    publish_table(
        location_df,
        output_folder,
        table_title="3.2",
        table_description="by grid point",
        filename="gridded_location_report",
        produce_csv=produce_csv,
        is_final=is_final,
        location_limit=location_limit,
        modify_locations=False,
    )

publish_named(location_df: pd.DataFrame, output_folder: Path, produce_csv: bool = True, is_final: bool = False, location_limit: int = 0)

Source code in nzssdt_2023/publish/report_condensed_v2.py
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
def publish_named(
    location_df: pd.DataFrame,
    output_folder: Path,
    produce_csv: bool = True,
    is_final: bool = False,
    location_limit: int = 0,
):
    publish_table(
        location_df,
        output_folder,
        table_title="3.1",
        table_description="by location name",
        filename="named_location_report",
        produce_csv=produce_csv,
        is_final=is_final,
        location_limit=location_limit,
        modify_locations=True,
    )

publish_table(location_df: pd.DataFrame, output_folder: Path, table_title: str, table_description: str, filename: str, produce_csv: bool = True, is_final: bool = False, location_limit: int = 0, modify_locations: bool = True)

Source code in nzssdt_2023/publish/report_condensed_v2.py
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
def publish_table(
    location_df: pd.DataFrame,
    output_folder: Path,
    table_title: str,
    table_description: str,
    filename: str,
    produce_csv: bool = True,
    is_final: bool = False,
    location_limit: int = 0,
    modify_locations: bool = True,
):

    if not is_final:
        filename += "-DRAFT"

    print(f"report: {filename}")

    report: Document = Document()

    if produce_csv:
        csv_rows = generate_csv_rows(location_df, modify_locations=False)

        ### CSV
        with open(Path(output_folder, filename + ".csv"), "w") as out_csv:
            writer = csv.writer(out_csv, quoting=csv.QUOTE_NONNUMERIC)
            header = ["location", "location_ascii", "apoe", "M", "D"]
            for sss in SITE_CLASSES:
                for attr in ["PGA", "Sas", "Tc", "Td"]:
                    header.append(f"{sss}-{attr}")
            writer.writerow(header)
            for row in csv_rows:
                writer.writerow(row)

    # PDF
    for page in build_pdf_report_pages(
        location_df,
        table_title,
        table_description,
        is_final,
        location_limit,
        modify_locations,
    ):
        report.add_page(page)

    with open(Path(output_folder, filename + ".pdf"), "wb") as out_file_handle:
        PDF.dumps(out_file_handle, report)