Datasets
dg_data
Classes:
-
DGData–Container for dynamic graph data to be ingested by
DGStorage.
DGData
dataclass
DGData(
time_delta: TimeDeltaDG | str,
time: Tensor,
edge_mask: Tensor,
edge_index: Tensor,
edge_x: Tensor | None = None,
node_x_mask: Tensor | None = None,
node_x_nids: Tensor | None = None,
node_x: Tensor | None = None,
node_y_mask: Tensor | None = None,
node_y_nids: Tensor | None = None,
node_y: Tensor | None = None,
static_node_x: Tensor | None = None,
edge_type: Tensor | None = None,
node_type: Tensor | None = None,
_split_strategy: SplitStrategy | None = None,
)
Container for dynamic graph data to be ingested by DGStorage.
Stores edge and node events, their timestamps, features, and optional split strategy. Provides methods to split, discretize, and clone the data.
Attributes:
-
time_delta(TimeDeltaDG | str) –Time granularity of the graph.
-
time(Tensor) –1D tensor of all event timestamps [num_edge_events + num_node_events + num_node_labels].
-
edge_mask(Tensor) –Mask of edge events within
time. -
edge_index(Tensor) –Edge connections [num_edge_events, 2].
-
edge_x(Tensor | None) –Optional edge features [num_edge_events, D_edge].
-
node_x_mask(Tensor | None) –Mask of dynamic node features within
time. -
node_x_nids(Tensor | None) –Node IDs corresponding to dynamic node features [num_node_events].
-
node_x(Tensor | None) –Dynamic Node features over time [num_node_events, D_node_dynamic].
-
node_y_mask(Tensor | None) –Mask of node labels within
time. -
node_y_nids(Tensor | None) –Node IDs corresponding to node labels [num_node_labels].
-
node_y(Tensor | None) –Node labels over time [num_node_labels, D_node_dynamic].
-
static_node_x(Tensor | None) –Node features invariant over time [num_nodes, D_node_static].
-
edge_type(Tensor | None)) –Type of relation of each edge event in edge_index [num_edge_events].
-
node_type(Tensor | None)) –Type of each node [num_nodes].
Raises:
-
InvalidNodeIDError–If an edge or node ID match
PADDED_NODE_ID. -
InvalidNodeIDError–If node labels exists with node IDs outside the graph's node ID range.
-
ValueError–If any data attributes have non-well defined tensor shapes.
-
EmptyGraphError–If attempting to initialize an empty graph.
Notes
- Timestamps must be non-negative and sorted; DGData will sort automatically if necessary.
- Cloning creates a deep copy of tensors to prevent in-place modifications.
- Edge type is only applicable for Heterogeneous & Knowledge graph.
- Node type is only applicable for Knowledge graph.
Methods:
-
clone–Deep copy all tensor and non-tensor fields to create a new DGData object.
-
discretize–Return a copy of the dataset discretized to a coarser time granularity.
-
from_csv–Construct a DGData from CSV files containing edge and optional node events.
-
from_pandas–Construct a DGData from Pandas DataFrames.
-
from_raw–Construct a DGData from raw tensors for edges, nodes, and features.
-
from_tgb–Load a DGData from a TGB dataset.
-
from_tgb_seq–Load a DGData from a TGB-SEQ dataset.
-
split–Split the dataset according to a strategy.
num_nodes
property
num_nodes: int
Global number of nodes in the dataset.
Note: Assumes node ids span a contiguous range, returning max(node_id) + 1.
clone
clone() -> DGData
Deep copy all tensor and non-tensor fields to create a new DGData object.
Source code in tgm/data/dg_data.py
566 567 568 569 570 571 572 573 574 575 576 | |
discretize
discretize(
time_delta: TimeDeltaDG | str | None,
reduce_op: str = 'first',
) -> DGData
Return a copy of the dataset discretized to a coarser time granularity.
Parameters:
-
time_delta(TimeDeltaDG | str | None) –Target time granularity.
-
reduce_op(str, default:'first') –Aggregation method for multiple events per bucket. Default 'first'.
Returns:
-
DGData(DGData) –New dataset with discretized timestamps and features.
Raises:
-
EventOrderedConversionError–If discretization is incompatible with event-ordered granularity
-
InvalidDiscretizationError–If the target granularity is finer than the current granularity.
Source code in tgm/data/dg_data.py
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 | |
from_csv
classmethod
from_csv(
edge_file_path: str | Path,
edge_src_col: str,
edge_dst_col: str,
edge_time_col: str,
edge_x_col: List[str] | None = None,
node_x_file_path: str | Path | None = None,
node_x_nids_col: str | None = None,
node_x_time_col: str | None = None,
node_x_col: List[str] | None = None,
node_y_file_path: str | Path | None = None,
node_y_nids_col: str | None = None,
node_y_time_col: str | None = None,
node_y_col: List[str] | None = None,
static_node_x_file_path: str | Path | None = None,
static_node_x_col: List[str] | None = None,
time_delta: TimeDeltaDG | str = 'r',
edge_type_col: str | None = None,
node_type_col: str | None = None,
) -> DGData
Construct a DGData from CSV files containing edge and optional node events.
Parameters:
-
edge_file_path(str | Path) –Path to CSV file containing edges.
-
edge_src_col(str) –Column name for src nodes.
-
edge_dst_col(str) –Column name for dst nodes.
-
edge_time_col(str) –Column name for edge times.
-
edge_x_col(List[str] | None, default:None) –Optional edge feature columns.
-
node_x_file_path(str | Path | None, default:None) –Optional CSV file for dynamic node features.
-
node_x_nids_col(str | None, default:None) –Column name for dynamic node event node ids. Required if node_file_path is specified.
-
node_x_time_col(str | None, default:None) –Column name for dynamic node event times. Required if node_file_path is specified.
-
node_x_col(List[str] | None, default:None) –Optional dynamic node feature columns.
-
node_y_file_path(str | Path | None, default:None) –Optional CSV file for dynamic node labels.
-
node_y_nids_col(str | None, default:None) –Column name for dynamic node label node ids.
-
node_y_time_col(str | None, default:None) –Column name for dynamic node label times.
-
node_y_col(List[str] | None, default:None) –Optional dynamic node label feature columns.
-
static_node_x_file_path(str | Path | None, default:None) –Optional CSV file for static node features.
-
static_node_x_col(List[str] | None, default:None) –Required if static_node_x_file_path is specified.
-
time_delta(TimeDeltaDG | str, default:'r') –Time granularity.
-
edge_type_col(str | None, default:None) –Column name for edge types.
-
node_type_col(str | None, default:None) –Column name for node types.
Raises:
-
InvalidNodeIDError–If an edge or node ID match
PADDED_NODE_ID. -
InvalidNodeIDError–If node labels exists with node IDs outside the graph's node ID range.
-
ValueError–If any data attributes have non-well defined tensor shapes.
-
EmptyGraphError–If attempting to initialize an empty graph.
Source code in tgm/data/dg_data.py
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 732 733 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 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
from_pandas
classmethod
from_pandas(
edge_df: 'pandas.DataFrame',
edge_src_col: str,
edge_dst_col: str,
edge_time_col: str,
edge_x_col: List[str] | None = None,
node_x_df: 'pandas.DataFrame' | None = None,
node_x_nids_col: str | None = None,
node_x_time_col: str | None = None,
node_x_col: List[str] | None = None,
node_y_df: 'pandas.DataFrame' | None = None,
node_y_nids_col: str | None = None,
node_y_time_col: str | None = None,
node_y_col: List[str] | None = None,
static_node_x_df: 'pandas.DataFrame' | None = None,
static_node_x_col: List[str] | None = None,
time_delta: TimeDeltaDG | str = 'r',
edge_type_col: str | None = None,
node_type_col: str | None = None,
) -> DGData
Construct a DGData from Pandas DataFrames.
Parameters:
-
edge_df('pandas.DataFrame') –DataFrame of edges.
-
edge_src_col(str) –Column name for src nodes.
-
edge_dst_col(str) –Column name for dst nodes.
-
edge_time_col(str) –Column name for edge times.
-
edge_x_col(List[str] | None, default:None) –Optional edge feature columns.
-
node_x_df('pandas.DataFrame' | None, default:None) –Optional DataFrame of dynamic node events.
-
node_x_nids_col(str | None, default:None) –Column name for dynamic node event node ids. Required if node_file_path is specified.
-
node_x_time_col(str | None, default:None) –Column name for dynamic node event times. Required if node_file_path is specified.
-
node_x_col(List[str] | None, default:None) –Optional node feature columns.
-
node_y_df('pandas.DataFrame' | None, default:None) –Optional DataFrame of dynamic node labels.
-
node_y_nids_col(str | None, default:None) –Column name for dynamic node label node ids.
-
node_y_time_col(str | None, default:None) –Column name for dynamic node label times.
-
node_y_col(List[str] | None, default:None) –Optional node label feature columns.
-
static_node_x_df('pandas.DataFrame' | None, default:None) –Optional static node features DataFrame.
-
static_node_x_col(List[str] | None, default:None) –Required if static_node_x_df is specified.
-
time_delta(TimeDeltaDG | str, default:'r') –Time granularity.
-
edge_type_col(str | None, default:None) –Column name for edge types.
-
node_type_col(str | None, default:None) –Column name for node types.
Raises:
-
InvalidNodeIDError–If an edge or node ID match
PADDED_NODE_ID. -
InvalidNodeIDError–If node labels exists with node IDs outside the graph's node ID range.
-
ValueError–If any data attributes have non-well defined tensor shapes.
-
ImportError–If the Pandas package is not resolved in the current python environment.
Source code in tgm/data/dg_data.py
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 | |
from_raw
classmethod
from_raw(
edge_time: Tensor,
edge_index: Tensor,
edge_x: Tensor | None = None,
node_x_time: Tensor | None = None,
node_x_nids: Tensor | None = None,
node_x: Tensor | None = None,
node_y_time: Tensor | None = None,
node_y_nids: Tensor | None = None,
node_y: Tensor | None = None,
static_node_x: Tensor | None = None,
time_delta: TimeDeltaDG | str = 'r',
edge_type: Tensor | None = None,
node_type: Tensor | None = None,
) -> DGData
Construct a DGData from raw tensors for edges, nodes, and features.
Automatically combines edge and node timestamps, computes event indices, and validates tensor shapes.
Raises:
-
InvalidNodeIDError–If an edge or node ID match
PADDED_NODE_ID. -
InvalidNodeIDError–If node labels exists with node IDs outside the graph's node ID range.
-
ValueError–If any data attributes have non-well defined tensor shapes.
-
EmptyGraphError–If attempting to initialize an empty graph.
Source code in tgm/data/dg_data.py
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 667 668 669 670 671 672 673 674 | |
from_tgb
classmethod
Load a DGData from a TGB dataset.
Parameters:
-
name(str) –Name of the TGB dataset, e.g., 'tgbl-xxxx' or 'tgbn-xxxx'.
-
kwargs(Any, default:{}) –Additional dataset-specific arguments.
Returns:
-
DGData(DGData) –Dataset with
_split_strategyautomatically set toTGBSplit.
Raises:
-
ImportError–If the TGB package is not resolved in the current python environment.
Notes
- TGBLinkPrediction (
tgbl-) and TGBNodePrediction (tgbn-) are supported. - The split strategy of a TGB dataset cannot be modified.
Source code in tgm/data/dg_data.py
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 | |
from_tgb_seq
classmethod
Load a DGData from a TGB-SEQ dataset.
Parameters:
-
name(str) –Name of the TGB-SEQ dataset.
-
kwargs(Any, default:{}) –Additional dataset-specific arguments.
Returns:
-
DGData(DGData) –Dataset with
_split_strategyautomatically set toTGBSplit.
Raises:
-
ImportError–If the TGB-SEQ package is not resolved in the current python environment.
Notes
- The split strategy of a TGB-SEQ dataset cannot be modified.
- If a data root location is not specified, we default store location to './data'.
Source code in tgm/data/dg_data.py
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 | |
split
split(
strategy: SplitStrategy | None = None,
) -> Tuple[DGData, ...]
Split the dataset according to a strategy.
Parameters:
-
strategy(SplitStrategy | None, default:None) –Optional strategy to override the default. If None, uses
_split_strategyor defaults toTemporalRatioSplit.
Returns:
Raises:
-
ValueError–If attempting to override the split strategy for TGB datasets.
Notes
- Splits preserve the underlying storage; only indices are filtered.
Source code in tgm/data/dg_data.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
Dataset Splitting
SplitStrategy
Bases: ABC
Abstract base class for splitting temporal graph datasets.
Implementations of this class define the logic for dividing a DGData object
into one or more subsets (train/val/test) based on temporal information.
Methods:
-
apply–Split the dataset and return one or more subsets.
apply
abstractmethod
apply(data: 'DGData') -> Tuple['DGData', ...]
Split the dataset and return one or more subsets.
Parameters:
-
data(DGData) –The temporal graph dataset to split.
Returns:
-
Tuple['DGData', ...]–Tuple[DGData, ...]: Split datasets according to the strategy.
Source code in tgm/data/split.py
22 23 24 25 26 27 28 29 30 31 | |
TemporalSplit
dataclass
Bases: SplitStrategy
Split a temporal graph dataset based on absolute timestamp boundaries.
Parameters:
-
val_time(int) –The timestamp separating training and validation data.
-
test_time(int) –The timestamp separating validation and test data.
Raises:
-
ValueError–If not 0 <= val_time <= test_time.
Note
This strategy assigns edges and nodes to splits based on whether their timestamps fall within the corresponding intervals: - Train: (-inf, val_time) - Validation: [val_time, test_time) - Test: [test_time, inf)
TemporalRatioSplit
dataclass
Bases: SplitStrategy
Split a temporal graph dataset according to relative ratios of time.
Parameters:
-
train_ratio(float, default:0.7) –Fraction of data to assign to training. Defaults to 0.7.
-
val_ratio(float, default:0.15) –Fraction of data to assign to validation. Defaults to 0.15.
-
test_ratio(float, default:0.15) –Fraction of data to assign to test. Defaults to 0.15.
Raises:
-
ValueError–If any ratio is negative.
-
ValueError–If the ratios do not sum to 1.0 within tolerance.
Note
The dataset timestamps are assumed to be sorted. The ratios are applied cumulatively to the total temporal span to determine absolute split boundaries.
TGBSplit
dataclass
Bases: SplitStrategy
Split a temporal graph dataset using pre-specified edge time bounds.
Parameters:
-
split_bounds(Dict[str, Tuple[int, int]]) –Mapping from split names ('train', 'val', 'test') to (min_time, max_time) intervals for edges.
Note
Nodes are included in a split if their timestamps fall within the corresponding edge interval (or slightly before the min_time of edges).