Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track original triple IDs in KGDataset.from_triples #37

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions besskge/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class KGDataset:
#: {part: int32[n_triple, {h,r,t}]}
triples: Dict[str, NDArray[np.int32]]

#: IDs of the triples in KGDataset.triples wrt
#: the ordering in the original array/dataframe
#: from where the triples originate.
original_triple_ids: Dict[str, NDArray[np.int32]]

#: Entity labels by ID; str[n_entity]
entity_dict: Optional[List[str]] = None

Expand Down Expand Up @@ -90,6 +95,9 @@ def from_triples(
and relations have already been assigned. Note that, if entities have
types, entities of the same type need to have contiguous IDs.
Triples are randomly split in train/validation/test sets.
The attribute `KGDataset.original_triple_ids` stores the IDs
of the triples in each split wrt the original ordering in `data`.

If a pre-defined train/validation/test split is wanted, the KGDataset
class should be instantiated manually.

Expand All @@ -114,22 +122,28 @@ class should be instantiated manually.
num_valid = int(num_triples * split[1])

rng = np.random.default_rng(seed=seed)
rng.shuffle(data, axis=0)

triples = dict()
triples["train"], triples["valid"], triples["test"] = np.split(
data, (num_train, num_train + num_valid), axis=0
id_shuffle = rng.permutation(np.arange(num_triples))
triple_ids = dict()
triple_ids["train"], triple_ids["valid"], triple_ids["test"] = np.split(
id_shuffle, (num_train, num_train + num_valid), axis=0
)
triples = dict()
triples["train"] = data[triple_ids["train"]]
triples["valid"] = data[triple_ids["valid"]]
triples["test"] = data[triple_ids["test"]]

return cls(
ds = cls(
n_entity=data[:, [0, 2]].max() + 1,
n_relation_type=data[:, 1].max() + 1,
entity_dict=entity_dict,
relation_dict=relation_dict,
type_offsets=type_offsets,
triples=triples,
original_triple_ids=triple_ids,
)

return ds

@classmethod
def from_dataframe(
cls,
Expand Down Expand Up @@ -219,6 +233,9 @@ def from_dataframe(
relation_dict=relation_dict,
type_offsets=type_offsets,
triples=triples,
original_triple_ids={
k: np.arange(v.shape[0]) for k, v in triples.items()
},
)

@classmethod
Expand Down Expand Up @@ -280,6 +297,7 @@ def build_ogbl_biokg(cls, root: Path) -> "KGDataset":
relation_dict=rel_dict,
type_offsets=type_offsets,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=neg_heads,
neg_tails=neg_tails,
)
Expand Down Expand Up @@ -329,6 +347,7 @@ def build_ogbl_wikikg2(cls, root: Path) -> "KGDataset":
relation_dict=rel_dict,
type_offsets=None,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=neg_heads,
neg_tails=neg_tails,
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_batch_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
relation_dict=None,
type_offsets=None,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=None,
neg_tails=None,
)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_bess.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_bess_inference(
relation_dict=None,
type_offsets=None,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=neg_heads,
neg_tails=neg_tails,
)
Expand Down Expand Up @@ -306,6 +307,7 @@ def test_bess_topk_prediction(
relation_dict=None,
type_offsets=None,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=neg_heads,
neg_tails=neg_tails,
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_all_scores_pipeline(
relation_dict=None,
type_offsets=None,
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
)

partition_mode = "h_shard" if corruption_scheme == "t" else "t_shard"
Expand Down
1 change: 1 addition & 0 deletions tests/test_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
relation_dict=None,
type_offsets={str(i): o for i, o in enumerate(type_offsets)},
triples=triples,
original_triple_ids={k: np.arange(v.shape[0]) for k, v in triples.items()},
neg_heads=neg_heads,
neg_tails=neg_tails,
)
Expand Down