tramdag.plots¶
tramdag.plots ¶
Figures of a TRAM-DAG: the DAG, the marginals, the training curve, a VC's beta.
matplotlib is an optional dependency: pip install "tramdag[plots]". It is
imported on the first call, so importing tramdag never needs it.
plot_dag ¶
plot_dag(
spec_or_flow,
*,
labels=True,
legend=True,
node_kind=True,
modifiers="edge",
style=None,
ax=None,
path=None,
title=None
)
Draw the labelled DAG of a spec (or of a fitted flow).
Layers run left to right, a node one layer past its parents. Continuous
nodes are ellipses, ordinal nodes rounded boxes with their level count.
Each edge is drawn by the term that owns it: LS thin gray, CS thick
blue, a complex intercept (CI) dashed orange, a VC treatment edge
red; a multi-parent CS/CI is labelled joint. A VC modifier is a dotted
red arrow onto the treatment edge it modifies. A term without an entry in
EDGE_STYLE draws dotted gray. EDGE_LABEL maps the to_matrix tag
VCm to the label VC modifier.
The layout units are NODE_H for the node height and ROW_DY for the
distance between rows. A new figure measures INCH_PER_UNIT inches per
unit, and the fonts fit it. An edge that skips a layer bends out by
BULGE per skipped layer. Given axes shrink the fonts with the space
they have per layout unit. A modifier points at the midpoint of its
treatment edge, a position of its own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_or_flow
|
dict[str, NodeSpec] | CausalFlowDAG
|
The DAG to draw. A flow draws its |
required |
labels
|
bool
|
Write the term name on each edge, by default True. |
True
|
legend
|
bool
|
Add a legend of the terms used, by default True. |
True
|
node_kind
|
bool
|
Write the node kind under each name, by default True. |
True
|
modifiers
|
str
|
|
'edge'
|
style
|
str | dict | None
|
|
None
|
ax
|
Axes | None
|
Draw into this axes; by default a new figure sized to the layout. |
None
|
path
|
str | Path | None
|
Save the figure here (150 dpi) after drawing, by default None (not saved). |
None
|
title
|
str | None
|
Axes title, by default none. |
None
|
Returns:
| Type | Description |
|---|---|
Axes
|
The axes drawn into. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the spec has no node. |
Source code in src/tramdag/plots.py
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 | |
plot_marginals ¶
plot_marginals(
flow,
df,
*,
ncols=3,
seed=None,
colors=("C0", "C1"),
legend="axes",
ax=None,
path=None,
title="observed vs sampled marginals"
)
Observed vs sampled marginal of every node, one panel each.
Ordinal nodes compare level proportions side by side; continuous nodes a
density histogram of the data with the flow's sample as a step outline.
The sample has as many rows as df.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
CausalFlowDAG
|
The fitted flow. |
required |
df
|
DataFrame
|
The data to compare against (the validation split, typically). |
required |
ncols
|
int
|
Panels per row of a new figure, by default 3. |
3
|
seed
|
int | None
|
Seed of the flow's sample, by default None. |
None
|
colors
|
tuple[str, str]
|
Colours of the data and of the flow's sample, by default
|
('C0', 'C1')
|
legend
|
str | None
|
|
'axes'
|
ax
|
sequence of matplotlib.axes.Axes | None
|
Draw into these panels, one per node in the flow's order; by default a new figure. |
None
|
path
|
str | Path | None
|
Save the figure here (150 dpi) after drawing, by default None (not saved). |
None
|
title
|
str | None
|
Title of a new figure, by default |
'observed vs sampled marginals'
|
Returns:
| Type | Description |
|---|---|
numpy.ndarray of matplotlib.axes.Axes
|
The panels, in the flow's node order (unused panels of a new figure are switched off). |
Source code in src/tramdag/plots.py
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 | |
plot_training ¶
plot_training(
flow, *, stops=False, ax=None, path=None, title=None
)
Draw the summed train and validation NLL per epoch.
flow.history accumulates across fit calls, so the curves cover
every epoch the flow has trained, not only the last call. Each validation
entry is drawn at the epoch it was measured in, which is what keeps the two
curves aligned when one fit validated and another did not: an
unvalidated fit in between leaves a gap instead of shifting the curve back
to epoch 1.
The y axis zooms past the initial drop: its top keeps each curve after 10 % of its epochs, so a validation curve that rises later stays in view. The limits use finite values only, so a diverged epoch does not blow them up. The legend sits below the axes, off the curves, and the title stays left of the stop names. The stop marks come after the zoom, so they hang from the visible top.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
CausalFlowDAG
|
The fitted flow; |
required |
stops
|
bool
|
Mark each node's stop epoch, the length of its |
False
|
ax
|
Axes | None
|
Draw into this axes; by default a new figure. |
None
|
path
|
str | Path | None
|
Save the figure here (150 dpi) after drawing, by default None (not saved). |
None
|
title
|
str | None
|
Axes title, by default |
None
|
Returns:
| Type | Description |
|---|---|
Axes
|
The axes drawn into. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/tramdag/plots.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
plot_varying_coef ¶
plot_varying_coef(
flow, df, node, *, by, t=None, ax=None, path=None
)
Draw a VC term's effect \(\beta(x)\) along one modifier.
The grid spans by over its range in df, or its levels for an
ordinal by; every other node column is held at an observed median
in df, so an ordinal value stays a level. The values come from
varying_coef, on the node's
latent scale. A dotted line marks \(\beta = 0\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
CausalFlowDAG
|
The fitted flow. |
required |
df
|
DataFrame
|
Rows that give the range of |
required |
node
|
str
|
The node that carries the VC term. |
required |
by
|
str
|
The modifier along which to draw. |
required |
t
|
str | None
|
The treatment of the VC term; optional when the node has one. |
None
|
ax
|
Axes | None
|
Draw into this axes; by default a new figure. |
None
|
path
|
str | Path | None
|
Save the figure here (150 dpi) after drawing, by default None (not saved). |
None
|
Returns:
| Type | Description |
|---|---|
Axes
|
The axes drawn into. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/tramdag/plots.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | |