Comparison¶
fleet.compare pairs a candidate dataset against a base on keys, matches variables by name and returns a comparison report; differences never raise. The .compare() verb on every namespace (SharedVerbsMixin) wraps it for pipelines, and fleet compare wraps it for the shell. See Comparing Datasets for the rules of equality and the exit codes. A comparison that cannot run raises ComparisonError, documented with the other exceptions.
compare¶
fleet.comparison.compare
¶
compare(
base,
candidate,
*,
keys,
by=None,
pairs=None,
variables=None,
abs_tol=0.0,
rel_tol=0.0,
ignore_trailing_whitespace=False,
blank_is_missing=False,
allow_duplicates=False,
max_print=50,
base_name=None,
candidate_name=None
)
Compare a candidate dataset against a base, PROC COMPARE style.
Observations pair on keys; variables match by name. Equality is
exact by default: null equals null, NaN equals NaN, null against NaN
differs. abs_tol and rel_tol widen it on the numeric variables
and ignore_trailing_whitespace on the string ones; nulls and NaN
keep their rule under both. Variables of the same kind (numeric,
string, temporal, boolean) but a different dtype compare by value after
casting to a common supertype and are listed as dtype differences;
variables of different kinds are type conflicts, listed and not
compared. An all-null column, which Polars types Null, adopts the
other side's dtype. Keys of one kind but different dtypes are cast the
same way before pairing. Row order and column order are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
DataFrame | LazyFrame
|
The reference dataset, as a DataFrame or LazyFrame. |
required |
candidate
|
DataFrame | LazyFrame
|
The dataset measured against the base. |
required |
keys
|
str | Sequence[str]
|
Variable(s) that identify one observation on each side. Required; the compare refuses to run when a key is missing or not unique on either side. |
required |
by
|
str | Sequence[str] | None
|
Variable(s) the report is sectioned by. They are prepended to
|
None
|
pairs
|
Mapping[str, str] | None
|
Base variable names mapped to the candidate names that carry them, applied on top of the automatic name matching, so a renamed column is compared rather than reported missing. The report names a paired variable by its base name. A pair whose candidate name is absent reports the base variable as missing from the candidate. |
None
|
variables
|
str | Sequence[str] | None
|
Restrict the comparison to these variables, in base names. A variable outside the restriction is ignored on both sides: it is neither compared nor reported as missing. The default compares every variable that is not a key. A restriction that names only keys is refused. |
None
|
abs_tol
|
float
|
Absolute tolerance on the numeric variables. |
0.0
|
rel_tol
|
float
|
Relative tolerance on the numeric variables, as a fraction
of the base value. |
0.0
|
ignore_trailing_whitespace
|
bool
|
Strip trailing whitespace from both
sides of a string before comparing, so SAS's padded character
values equal Fleet's stripped ones. String keys are stripped
too, before the duplicate-key check, so |
False
|
blank_is_missing
|
bool
|
Read an empty string as null on both sides, keys
included, as SAS does: SAS has one character missing value, the
blank. Use it when one side comes from a tool that kept |
False
|
allow_duplicates
|
bool
|
Pair duplicate keys by their position within each
key group instead of refusing to run. The observation summary
then counts the duplicates per side, the |
False
|
max_print
|
MaxPrint
|
Print limits for the text renderer (differences per
variable, or a |
50
|
base_name
|
str | None
|
Label for the base in the report, when known. |
None
|
candidate_name
|
str | None
|
Label for the candidate in the report, when known. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ComparisonReport
|
class: |
ComparisonReport
|
raise; check |
Raises:
| Type | Description |
|---|---|
ComparisonError
|
A key or BY variable is missing, duplicated (without
|
VerbUsageError
|
An input is not a Polars frame, or an argument has the wrong type. |
Examples:
Prove an independently programmed ADAE reached the production result::
report = fleet.compare(
adae_prod,
adae_qc,
keys=["USUBJID", "AESEQ"],
base_name="adae (prod)",
candidate_name="adae (qc)",
)
report.print_report()
assert report.is_equal, report.raised_flags
Section the report by treatment arm, so a difference in one arm stands out::
report = fleet.compare(adsl_prod, adsl_qc, keys="USUBJID", by="TRT01P")
report.observations.per_group
Compare a legacy dataset that named two variables differently, on the variables that are finished::
report = fleet.compare(
advs_legacy,
advs_wip,
keys=["USUBJID", "PARAMCD"],
pairs={"AVAL": "RESULT"},
variables=["AVAL", "AVISIT"],
)
Source code in src/fleet/comparison/engine.py
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 | |
options: show_root_heading: true heading_level: 3
ComparisonReport¶
fleet.comparison.ComparisonReport
dataclass
¶
Outcome of :func:fleet.compare.
Attributes:
| Name | Type | Description |
|---|---|---|
datasets |
DatasetSummary
|
Name, rows and variables per side. |
variables |
VariablesSummary
|
Common, one-sided, conflicting and differently typed variables. |
observations |
ObservationSummary
|
Matched and one-sided observation counts, with the key values of the one-sided observations, the duplicate counts per side and the per-BY-group breakdown. |
values |
ValuesSummary
|
Differences per compared variable, and per BY group. |
listing |
DataFrame
|
Long frame of every differing value, sorted by keys: the key
columns, then |
statistics |
DataFrame
|
One row per differing variable, in compared order, with
the columns of :data: |
flags |
frozenset[str]
|
The subset of :data: |
settings |
ComparisonSettings
|
The settings the comparison ran with. |
version |
str
|
The Fleet version that produced the report. |
Source code in src/fleet/comparison/report.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
summary
property
¶
One line: verdict, sides, matched observations, compared variables, flags.
warnings
property
¶
What every renderer says above the report; empty when nothing needs saying.
Pairing duplicate keys by position is a weaker guarantee than pairing on keys, and a compare that checked no values can still be EQUAL (ADR-0004), so a reader is told before the first number.
to_text
¶
Render the report as plain text in PROC COMPARE section order.
Sections: data set summary, variables summary, observation summary,
values summary, summary statistics, value listing grouped per
observation, verdict. The print limits cap the value listing (per
variable first, then overall) and the one-sided observation lists;
the report's own listing frame is never truncated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_print
|
MaxPrint | _Unset
|
Differences to print per variable, a
|
_USE_SETTINGS
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The rendered report, one section per block. |
Examples:
Keep the full text of a comparison next to a build::
report = fleet.compare(adsl_prod, adsl_qc, keys=["USUBJID"])
Path("adsl_compare.txt").write_text(report.to_text(max_print=None))
Source code in src/fleet/comparison/report.py
print_report
¶
Emit :meth:to_text through the fleet.comparison logger.
By default the report reaches stdout (INFO level); use the standard
:mod:logging API or :func:fleet.core.log.disable_console to route
or silence it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_print
|
MaxPrint | _Unset
|
As for :meth: |
_USE_SETTINGS
|
Examples:
Print a QC comparison and gate a script on it::
report = fleet.compare(adsl_prod, adsl_qc, keys=["USUBJID"])
report.print_report()
assert report.is_equal, report.raised_flags
Source code in src/fleet/comparison/report.py
to_json
¶
Render the report as one JSON document for agents, CI jobs and editors.
Top-level keys, in order: :data:JSON_KEYS. Field names mirror the
report attributes and the contract is additive-only within a major
version (keys are added, never removed or renamed). The value
listing and the one-sided key lists are truncated to the print
limits and carry a truncated marker; :meth:save_listing writes
the complete listing. Strict JSON: non-finite numbers are null,
temporal values are ISO 8601 strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_print
|
MaxPrint | _Unset
|
As for :meth: |
_USE_SETTINGS
|
indent
|
int | None
|
Passed to :func: |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The JSON document. |
Examples:
Let an agent reason about the differences without parsing prose::
report = fleet.compare(adsl_prod, adsl_qc, keys=["USUBJID"])
document = json.loads(report.to_json())
assert document["is_equal"], document["flags"]
Source code in src/fleet/comparison/report.py
save_json
¶
Write :meth:to_json to path (UTF-8, trailing newline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Where to write the document. |
required |
max_print
|
MaxPrint | _Unset
|
As for :meth: |
_USE_SETTINGS
|
Examples:
Archive the outcome of a QC comparison next to the build::
report = fleet.compare(adsl_prod, adsl_qc, keys=["USUBJID"])
report.save_json("build/adsl_compare.json")
Source code in src/fleet/comparison/report.py
to_html
¶
Render the report as one self-contained HTML page for a reviewer.
The page needs no Python and fetches nothing: its style is embedded
and there is no script, font or image. Structural findings (the
raised flags: one-sided variables and observations, type conflicts,
...) are chips at the top; below, only the mismatched observations
are listed, base and candidate side by side with both cells
highlighted, one collapsible block per differing variable opening on
that variable's summary statistics, and one section per BY group when
the comparison ran with by.
Nothing is truncated: every unequal value is on the page, whatever
max_print the comparison ran with. For listings beyond a few
thousand rows, :meth:save_listing is the better archive.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The HTML document. |
Examples:
Hand a reviewer the page instead of a terminal transcript::
report = fleet.compare(adae_prod, adae_qc, keys=["USUBJID", "AESEQ"])
Path("adae_compare.html").write_text(report.to_html(), encoding="utf-8")
Source code in src/fleet/comparison/report.py
save_html_report
¶
Write :meth:to_html to path (UTF-8).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Where to write the page. |
required |
Examples:
Produce the sign-off page at the end of a QC comparison::
report = fleet.compare(adae_prod, adae_qc, keys=["USUBJID", "AESEQ"])
report.save_html_report("build/adae_compare.html")
Source code in src/fleet/comparison/report.py
save_listing
¶
Write the complete value listing to path as parquet.
Never truncated: every differing value, with the key columns in their
own dtypes followed by :data:LISTING_COLUMNS (group,
variable, base and candidate as strings, difference
and percent_difference as floats). An equal comparison writes an
empty frame with the same schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Where to write the parquet file. |
required |
Examples:
Keep every difference for a reviewer, then query it lazily::
report = fleet.compare(adae_prod, adae_qc, keys=["USUBJID", "AESEQ"])
report.save_listing("build/adae_differences.parquet")
worst = pl.scan_parquet("build/adae_differences.parquet").filter(
pl.col("percent_difference").abs() > 10
)
Source code in src/fleet/comparison/report.py
options: show_root_heading: true heading_level: 3 filters: ["!^_"]
Summaries¶
fleet.comparison.DatasetSummary
dataclass
¶
options: show_root_heading: true heading_level: 3 filters: ["!^_"]
fleet.comparison.VariablesSummary
dataclass
¶
Which variables were common, one-sided, conflicting or merely differently typed.
Keys are never counted here; they identify observations rather than being compared.
Source code in src/fleet/comparison/report.py
options: show_root_heading: true heading_level: 3 filters: ["!^_"]
fleet.comparison.ObservationSummary
dataclass
¶
How observations paired on the keys, with the key values of the unpaired ones.
Source code in src/fleet/comparison/report.py
options: show_root_heading: true heading_level: 3 filters: ["!^_"]
fleet.comparison.ValuesSummary
dataclass
¶
Differences per compared variable over the matched observations.
Source code in src/fleet/comparison/report.py
options: show_root_heading: true heading_level: 3 filters: ["!^_"]
Settings¶
The options a comparison ran with, so a run can be reproduced. abs_tol, rel_tol and ignore_trailing_whitespace are the three that change what counts as equal — see tolerances and trailing whitespace. by sections the report per BY group, allow_duplicates pairs duplicate keys by position, and pairs and variables rename and restrict what is compared — see variable pairs and restriction.
fleet.comparison.ComparisonSettings
dataclass
¶
The settings a comparison ran with, so a run can be reproduced.
Three of them decide what counts as equal, so this is also where the
renderers read the rule in effect from (:attr:equality).
Source code in src/fleet/comparison/report.py
tolerant
property
¶
True when either tolerance is set, so numeric values compare within it.
pair_labels
property
¶
The variable pairs as BASE = CANDIDATE, for the report headers; empty when none.
equality
property
¶
The equality rule in effect, as one phrase for the report headers.
"exact" by default, otherwise the tolerances in effect, followed
by "ignoring trailing whitespace" when strings were stripped —
a qualifier rather than a second verdict, so the phrase reads as one
rule::
"exact"
"abs_tol=1e-06, rel_tol=0.001"
"exact, ignoring trailing whitespace"
options: show_root_heading: true heading_level: 3 filters: ["!^_"]