Skip to content

labqc.report

Records and reports: keep what was calculated, with what, by which version, and when.

A RunRecord holds one calculation: the labqc function, its inputs and settings, the result, a short description of the method, the labqc version, a UTC timestamp and optionally the analyst. Records round-trip through JSON, so a saved budget can be reloaded later, and can be written to Excel (one sheet per record) or to a one-page PDF.

Excel and PDF output need the [reports] extra: pip install "labqc[reports]".

RunRecord dataclass

One calculation, kept as a record.

function: the labqc function that made the result, e.g. "labqc.uncertainty.combine". inputs, settings, result: plain dictionaries (numbers, strings, lists, and lists of row dicts for tables). method: one or two sentences naming the formula or standard.

Source code in src/labqc/report.py
 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
@dataclass
class RunRecord:
    """One calculation, kept as a record.

    function: the labqc function that made the result, e.g. "labqc.uncertainty.combine".
    inputs, settings, result: plain dictionaries (numbers, strings, lists, and lists of
    row dicts for tables). method: one or two sentences naming the formula or standard.
    """

    function: str
    inputs: dict[str, Any]
    settings: dict[str, Any] = field(default_factory=dict)
    result: dict[str, Any] = field(default_factory=dict)
    method: str = ""
    title: str = ""
    analyst: str | None = None
    labqc_version: str = __version__
    timestamp: str = field(default_factory=_now)

    def __post_init__(self) -> None:
        self.inputs = plain(self.inputs)
        self.settings = plain(self.settings)
        self.result = plain(self.result)

    def to_json(self) -> str:
        """The record as indented JSON (UTF-8 text, keys in a fixed order)."""
        return json.dumps(asdict(self), indent=2, ensure_ascii=False, allow_nan=False) + "\n"

    @classmethod
    def from_json(cls, text: str | bytes) -> RunRecord:
        """Read a record saved with `to_json`. Raises ValueError for anything else."""
        try:
            data = json.loads(text)
        except (json.JSONDecodeError, UnicodeDecodeError) as err:
            raise ValueError(f"Not a valid labqc record: {err}") from None
        if not isinstance(data, dict):
            raise ValueError("Not a valid labqc record: expected a JSON object")
        known = {f.name for f in fields(cls)}
        missing = {"function", "inputs"} - set(data)
        if missing:
            raise ValueError(f"Not a valid labqc record: missing {', '.join(sorted(missing))}")
        unknown = set(data) - known
        if unknown:
            raise ValueError(
                f"Not a valid labqc record: unknown field(s) {', '.join(sorted(unknown))}"
            )
        return cls(**data)

    def label(self) -> str:
        return self.title or self.function.rsplit(".", 1)[-1]

to_json

to_json() -> str

The record as indented JSON (UTF-8 text, keys in a fixed order).

Source code in src/labqc/report.py
95
96
97
def to_json(self) -> str:
    """The record as indented JSON (UTF-8 text, keys in a fixed order)."""
    return json.dumps(asdict(self), indent=2, ensure_ascii=False, allow_nan=False) + "\n"

from_json classmethod

from_json(text: str | bytes) -> RunRecord

Read a record saved with to_json. Raises ValueError for anything else.

Source code in src/labqc/report.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@classmethod
def from_json(cls, text: str | bytes) -> RunRecord:
    """Read a record saved with `to_json`. Raises ValueError for anything else."""
    try:
        data = json.loads(text)
    except (json.JSONDecodeError, UnicodeDecodeError) as err:
        raise ValueError(f"Not a valid labqc record: {err}") from None
    if not isinstance(data, dict):
        raise ValueError("Not a valid labqc record: expected a JSON object")
    known = {f.name for f in fields(cls)}
    missing = {"function", "inputs"} - set(data)
    if missing:
        raise ValueError(f"Not a valid labqc record: missing {', '.join(sorted(missing))}")
    unknown = set(data) - known
    if unknown:
        raise ValueError(
            f"Not a valid labqc record: unknown field(s) {', '.join(sorted(unknown))}"
        )
    return cls(**data)

plain

plain(value: Any) -> Any

Turn numpy, pandas and float edge cases into plain JSON values.

Infinity becomes the string "inf" (JSON has no infinity), NaN becomes None, DataFrames become lists of row dicts and tuples become lists.

Source code in src/labqc/report.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def plain(value: Any) -> Any:
    """Turn numpy, pandas and float edge cases into plain JSON values.

    Infinity becomes the string "inf" (JSON has no infinity), NaN becomes None,
    DataFrames become lists of row dicts and tuples become lists.
    """
    if isinstance(value, pd.DataFrame):
        return [plain(row) for row in value.to_dict(orient="records")]
    if isinstance(value, Mapping):
        return {str(k): plain(v) for k, v in value.items()}
    if isinstance(value, (list, tuple, np.ndarray, pd.Series)):
        return [plain(v) for v in list(value)]
    if isinstance(value, (np.bool_, bool)):
        return bool(value)
    if isinstance(value, (np.integer,)):
        return int(value)
    if isinstance(value, (float, np.floating)):
        value = float(value)
        if math.isnan(value):
            return None
        if math.isinf(value):
            return "inf" if value > 0 else "-inf"
        return value
    if value is None or isinstance(value, (str, int)):
        return value
    return str(value)

record_budget

record_budget(components: Sequence[Component], result: Result, coverage: float | None = None, fixed_k: float | None = None, unit: str = '', title: str = 'Uncertainty budget', analyst: str | None = None, correlation: Mapping[tuple[str, str], float] | None = None) -> RunRecord

Record a budget made with combine (or propagate) so it can be reported and reloaded.

correlation is the one passed to combine; it is stored as [name, name, r] rows.

Source code in src/labqc/report.py
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
def record_budget(
    components: Sequence[Component],
    result: Result,
    coverage: float | None = None,
    fixed_k: float | None = None,
    unit: str = "",
    title: str = "Uncertainty budget",
    analyst: str | None = None,
    correlation: Mapping[tuple[str, str], float] | None = None,
) -> RunRecord:
    """Record a budget made with `combine` (or `propagate`) so it can be reported and reloaded.

    `correlation` is the one passed to `combine`; it is stored as [name, name, r] rows.
    """
    return RunRecord(
        function=BUDGET_FUNCTION,
        title=title,
        analyst=analyst,
        method=BUDGET_METHOD,
        inputs={
            "value": result.value,
            "unit": unit,
            "components": [
                {
                    "name": c.name,
                    "value": c.value,
                    "distribution": c.distribution,
                    "k": c.k,
                    "sensitivity": c.sensitivity,
                    "dof": c.dof,
                    "unit": c.unit,
                }
                for c in components
            ],
            **(
                {"correlation": [[a, b, float(r)] for (a, b), r in correlation.items()]}
                if correlation
                else {}
            ),
        },
        settings={
            "coverage": result.coverage if coverage is None else coverage,
            "fixed_k": fixed_k,
        },
        result={
            "combined_u_c": result.combined,
            "effective_dof": result.effective_dof,
            "k": result.k,
            "expanded_U": result.expanded,
            "relative_U": result.relative_expanded if result.value else None,
            "budget": result.table,
        },
    )

budget_from_record

budget_from_record(record: RunRecord) -> tuple[pd.DataFrame, dict[str, Any]]

The components table and settings of a recorded budget, ready for combine again.

Returns (table with the columns of components_from_table, dict with value, unit, coverage, fixed_k and correlation, a {(name, name): r} dict or None). Raises ValueError if the record is not an uncertainty budget.

Source code in src/labqc/report.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def budget_from_record(record: RunRecord) -> tuple[pd.DataFrame, dict[str, Any]]:
    """The components table and settings of a recorded budget, ready for `combine` again.

    Returns (table with the columns of `components_from_table`, dict with value, unit,
    coverage, fixed_k and correlation, a {(name, name): r} dict or None). Raises ValueError
    if the record is not an uncertainty budget.
    """
    if record.function != BUDGET_FUNCTION or "components" not in record.inputs:
        raise ValueError(
            f"This record is not an uncertainty budget (it was made by {record.function})"
        )
    table = pd.DataFrame(record.inputs["components"]).reindex(columns=list(TABLE_COLUMNS))
    table["dof"] = [
        None if d in ("inf", None) else float(d) for d in table["dof"]
    ]  # blank = infinite
    info = {
        "value": record.inputs.get("value"),
        "unit": record.inputs.get("unit", ""),
        "coverage": record.settings.get("coverage", 0.95),
        "fixed_k": record.settings.get("fixed_k"),
        "correlation": {(a, b): float(r) for a, b, r in record.inputs.get("correlation", [])}
        or None,
    }
    return table, info

to_excel

to_excel(records: Sequence[RunRecord], path: PathOrFile) -> None

Write records to an .xlsx workbook, one sheet per record (needs openpyxl).

Source code in src/labqc/report.py
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
def to_excel(records: Sequence[RunRecord], path: PathOrFile) -> None:
    """Write records to an .xlsx workbook, one sheet per record (needs openpyxl)."""
    try:
        from openpyxl import Workbook
        from openpyxl.styles import Font
    except ImportError:  # pragma: no cover - depends on the environment
        raise ImportError('to_excel needs openpyxl: pip install "labqc[reports]"') from None
    if not records:
        raise ValueError("No records to write")
    wb = Workbook()
    wb.remove(wb.active)
    used: set[str] = set()
    bold = Font(bold=True)
    for record in records:
        ws = wb.create_sheet(_sheet_name(record.label(), used))
        ws.append([record.label()])
        ws["A1"].font = Font(bold=True, size=14)
        for key, value in _meta(record):
            ws.append([key, value])
        ws.append(["Method", record.method])
        for heading, rows, tables in _sections(record):
            if not rows and not tables:
                continue
            ws.append([])
            ws.append([heading])
            ws.cell(ws.max_row, 1).font = bold
            for key, value in rows:
                ws.append([key, value if isinstance(value, (int, float)) else _fmt(value)])
            for name, table in tables:
                columns = list(dict.fromkeys(k for row in table for k in row))
                ws.append([name])
                ws.cell(ws.max_row, 1).font = Font(italic=True)
                ws.append(columns)
                for cell in ws[ws.max_row]:
                    cell.font = bold
                for row in table:
                    ws.append([_cell(row.get(c)) for c in columns])
        ws.column_dimensions["A"].width = 32
        for col in "BCDEFGH":
            ws.column_dimensions[col].width = 16
    wb.save(path)

to_pdf

to_pdf(record: RunRecord, path: PathOrFile) -> None

Write one record as a PDF: title, meta data, method, inputs, result (needs reportlab).

Source code in src/labqc/report.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def to_pdf(record: RunRecord, path: PathOrFile) -> None:
    """Write one record as a PDF: title, meta data, method, inputs, result (needs reportlab)."""
    try:
        from reportlab.lib import colors
        from reportlab.lib.pagesizes import A4
        from reportlab.lib.styles import getSampleStyleSheet
        from reportlab.lib.units import mm
        from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, Table, TableStyle
    except ImportError:  # pragma: no cover - depends on the environment
        raise ImportError('to_pdf needs reportlab: pip install "labqc[reports]"') from None

    styles = getSampleStyleSheet()
    small = styles["BodyText"].clone("small", fontSize=8, leading=10)
    grid = TableStyle(
        [
            ("FONTSIZE", (0, 0), (-1, -1), 8),
            ("GRID", (0, 0), (-1, -1), 0.25, colors.grey),
            ("VALIGN", (0, 0), (-1, -1), "TOP"),
        ]
    )
    header = TableStyle([("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#e8eef2"))])

    def p(value: Any, style=small) -> Paragraph:
        return Paragraph(_pdf_text(value), style)

    story = [
        Paragraph(_pdf_text(record.label()), styles["Title"]),
        Table([[p(k), p(v)] for k, v in _meta(record)], colWidths=[40 * mm, 130 * mm], style=grid),
        Spacer(1, 4 * mm),
        Paragraph("Method", styles["Heading3"]),
        p(record.method or "-", styles["BodyText"]),
    ]
    for heading, rows, tables in _sections(record):
        if not rows and not tables:
            continue
        story += [Spacer(1, 3 * mm), Paragraph(heading, styles["Heading3"])]
        if rows:
            story.append(
                Table([[p(k), p(v)] for k, v in rows], colWidths=[60 * mm, 110 * mm], style=grid)
            )
        for name, table in tables:
            columns = list(dict.fromkeys(k for row in table for k in row))
            data = [[p(c) for c in columns]] + [[p(row.get(c)) for c in columns] for row in table]
            story += [Spacer(1, 2 * mm), p(name, styles["Italic"])]
            t = Table(data, repeatRows=1, style=grid)
            t.setStyle(header)
            story.append(t)
    story += [
        Spacer(1, 6 * mm),
        p(
            f"Calculated with labqc {record.labqc_version}, validated against published and "
            "hand-calculated reference cases. Validate it for your own use (ISO/IEC 17025 7.11)."
        ),
    ]
    SimpleDocTemplate(
        str(path) if isinstance(path, Path) else path,  # reportlab wants str or a file
        pagesize=A4,
        leftMargin=18 * mm,
        rightMargin=18 * mm,
        topMargin=16 * mm,
        bottomMargin=16 * mm,
        title=record.label(),
        author=record.analyst or "",
        creator=f"labqc {record.labqc_version}",
    ).build(story)