ICEWS14 / ICEWS14.py
linxy's picture
Upload ICEWS14.py
fbe90c5 verified
# Copyright 2023 Xueyuan Lin
# Apache 2.0 License
"""Loading script for DiffusionDB."""
from typing import List, Dict
import json
import os
from huggingface_hub import hf_hub_url
import datasets
_CITATION = """\
@inproceedings{
xueyuan2023tflex,
title={TFLEX: Temporal Feature-Logic Embedding Framework for Complex Reasoning over Temporal Knowledge Graph},
author={Lin Xueyuan and Haihong E and Chengjin Xu and Gengxian Zhou and Haoran Luo and Tianyi Hu and Fenglong Su and Ningyuan Li and Mingzhi Sun},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023},
url={https://openreview.net/forum?id=oaGdsgB18L}
}\
"""
_DESCRIPTION = """\
TL;DR: The datasets for temporal knowledge graph reasoning task.
[[Github]](https://github.com/LinXueyuanStdio/TFLEX)
[[OpenReview]](https://openreview.net/forum?id=oaGdsgB18L)
[[arXiv]](https://arxiv.org/abs/2205.14307)
- Built over ICEWS and GDELT, which are widly used benchmarks in TKGC.
- First introduced in paper "TFLEX: Temporal Feature-Logic Embedding Framework for Complex Reasoning over Temporal Knowledge Graph"
- Please refer to the original paper for more details.
"""
_HOMEPAGE = "https://github.com/LinXueyuanStdio/TFLEX"
_LICENSE = "[Apache License 2.0](https://github.com/LinXueyuanStdio/TFLEX/blob/main/LICENSE)"
query_name_to_args: Dict[str, List[str]] = {
# 1. 1-hop Pe and Pt, manually
"Pe": ["e1", "r1", "t1"],
"Pt": ["e1", "r1", "e2"],
# 2. entity multi-hop
"Pe2": ["e1", "r1", "t1", "r2", "t2"],
"Pe3": ["e1", "r1", "t1", "r2", "t2", "r3", "t3"],
# 3. time multi-hop
"aPt": ["s", "r", "o"],
"bPt": ["s", "r", "o"],
"Pt_sPe": ["e1", "r1", "t1", "r2", "e2"],
"Pt_oPe": ["e1", "r1", "e2", "r2", "t1"],
"Pe_Pt": ["e1", "r1", "e2", "r2", "e3"],
"Pe_aPt": ["e1", "r1", "e2", "r2", "e3"],
"Pe_bPt": ["e1", "r1", "e2", "r2", "e3"],
"Pe_nPt": ["e1", "r1", "e2", "r2", "e3"],
"Pt_sPe_Pt": ["s1", "r1", "s2", "r2", "o1", "r3", "o2"],
"Pt_oPe_Pt": ["s1", "r1", "s2", "r2", "s3", "r3", "o1"],
# 4. entity and & time and
"e2i": ["e1", "r1", "t1", "e2", "r2", "t2"],
"e3i": ["e1", "r1", "t1", "e2", "r2", "t2", "e3", "r3", "t3"],
"t2i": ["e1", "r1", "e2", "e3", "r2", "e4"],
"t3i": ["e1", "r1", "e2", "e3", "r2", "e4", "e5", "r3", "e6"],
# 5. complex time and
"e2i_Pe": ["e1", "r1", "t1", "r2", "t2", "e2", "r3", "t3"],
"Pe_e2i": ["e1", "r1", "t1", "e2", "r2", "t2", "r3", "t3"],
"Pt_se2i": ["e1", "r1", "t1", "e2", "r2", "t2", "r3", "e3"],
"Pt_oe2i": ["e1", "r1", "e2", "r2", "t1", "e3", "r3", "t2"],
"t2i_Pe": ["e1", "r1", "t1", "r2", "e2", "e3", "r3", "e4"],
"Pe_t2i": ["e1", "r1", "e2", "r2", "e3", "e4", "r3", "e5"],
"Pe_at2i": ["e1", "r1", "e2", "r2", "e3", "e4", "r3", "e5"],
"Pe_bt2i": ["e1", "r1", "e2", "r2", "e3", "e4", "r3", "e5"],
"Pe_nt2i": ["e1", "r1", "e2", "r2", "e3", "e4", "r3", "e5"],
"between": ["e1", "r1", "e2", "e3", "r2", "e4"],
# 5. entity not
"e2i_N": ["e1", "r1", "t1", "e2", "r2", "t2"],
"e3i_N": ["e1", "r1", "t1", "e2", "r2", "t2", "e3", "r3", "t3"],
"Pe_e2i_Pe_NPe": ["e1", "r1", "t1", "e2", "r2", "t2", "r3", "t3"],
"e2i_NPe": ["e1", "r1", "t1", "r2", "t2", "e2", "r3", "t3"],
"e2i_PeN": ["e1", "r1", "t1", "r2", "t2", "e2", "r3", "t3"],
# 6. time not
"t2i_N": ["e1", "r1", "e2", "e3", "r2", "e4"],
"t3i_N": ["e1", "r1", "e2", "e3", "r2", "e4", "e5", "r3", "e6"],
"Pe_t2i_PtPe_NPt": ["e1", "r1", "e2", "r2", "t2", "r3", "e3", "e4", "r4", "e5"],
"t2i_NPt": ["e1", "r1", "t1", "r2", "e2", "e3", "r3", "e4"],
"t2i_PtN": ["e1", "r1", "t1", "r2", "e2", "e3", "r3", "e4"],
# 7. entity union & time union
"e2u": ["e1", "r1", "t1", "e2", "r2", "t2"],
"Pe_e2u": ["e1", "r1", "t1", "e2", "r2", "t2", "r3", "t3"],
"t2u": ["e1", "r1", "e2", "e3", "r2", "e4"],
"Pe_t2u": ["e1", "r1", "e2", "r2", "e3", "e4", "r3", "e5"],
}
query_structures: Dict[str, str] = {
# 1. 1-hop Pe and Pt, manually
"Pe": "def Pe(e1, r1, t1): return Pe(e1, r1, t1)", # 1p
"Pt": "def Pt(e1, r1, e2): return Pt(e1, r1, e2)", # 1p, temporal
# 2. entity multi-hop
"Pe2": "def Pe2(e1, r1, t1, r2, t2): return Pe(Pe(e1, r1, t1), r2, t2)", # 2p
"Pe3": "def Pe3(e1, r1, t1, r2, t2, r3, t3): return Pe(Pe(Pe(e1, r1, t1), r2, t2), r3, t3)", # 3p
# 3. time multi-hop
"aPt": "def aPt(s, r, o): return after(Pt(s, r, o))", # a for after
"bPt": "def bPt(s, r, o): return before(Pt(s, r, o))", # b for before
"Pt_lPe": "def Pt_lPe(e1, r1, t1, r2, e2): return Pt(Pe(e1, r1, t1), r2, e2)", # l for left (as head entity)
"Pt_rPe": "def Pt_rPe(e1, r1, e2, r2, t1): return Pt(e1, r1, Pe(e2, r2, t1))", # r for right (as tail entity)
"Pt_sPe": "def Pt_sPe(e1, r1, t1, r2, e2): return Pt(Pe(e1, r1, t1), r2, e2)", # l for left (as head entity)
"Pt_oPe": "def Pt_oPe(e1, r1, e2, r2, t1): return Pt(e1, r1, Pe(e2, r2, t1))", # r for right (as tail entity)
"Pe_Pt": "def Pe_Pt(e1, r1, e2, r2, e3): return Pe(e1, r1, Pt(e2, r2, e3))", # at
"Pe_aPt": "def Pe_aPt(e1, r1, e2, r2, e3): return Pe(e1, r1, after(Pt(e2, r2, e3)))", # a for after
"Pe_bPt": "def Pe_bPt(e1, r1, e2, r2, e3): return Pe(e1, r1, before(Pt(e2, r2, e3)))", # b for before
"Pe_nPt": "def Pe_nPt(e1, r1, e2, r2, e3): return Pe(e1, r1, next(Pt(e2, r2, e3)))", # n for next
"Pt_sPe_Pt": "def Pt_sPe_Pt(s1, r1, s2, r2, o1, r3, o2): return Pt(Pe(s1, r1, Pt(s2, r2, o1)), r3, o2)",
"Pt_oPe_Pt": "def Pt_oPe_Pt(s1, r1, s2, r2, s3, r3, o1): return Pt(s1, r1, Pe(s2, r2, Pt(s3, r3, o1)))",
# 4. entity and & time and
"e2i": "def e2i(e1, r1, t1, e2, r2, t2): return And(Pe(e1, r1, t1), Pe(e2, r2, t2))", # 2i
"e3i": "def e3i(e1, r1, t1, e2, r2, t2, e3, r3, t3): return And3(Pe(e1, r1, t1), Pe(e2, r2, t2), Pe(e3, r3, t3))", # 3i
"t2i": "def t2i(e1, r1, e2, e3, r2, e4): return TimeAnd(Pt(e1, r1, e2), Pt(e3, r2, e4))", # t-2i
"t3i": "def t3i(e1, r1, e2, e3, r2, e4, e5, r3, e6): return TimeAnd3(Pt(e1, r1, e2), Pt(e3, r2, e4), Pt(e5, r3, e6))", # t-3i
# 5. complex time and
"e2i_Pe": "def e2i_Pe(e1, r1, t1, r2, t2, e2, r3, t3): return And(Pe(Pe(e1, r1, t1), r2, t2), Pe(e2, r3, t3))", # pi
"Pe_e2i": "def Pe_e2i(e1, r1, t1, e2, r2, t2, r3, t3): return Pe(e2i(e1, r1, t1, e2, r2, t2), r3, t3)", # ip
"Pt_le2i": "def Pt_le2i(e1, r1, t1, e2, r2, t2, r3, e3): return Pt(e2i(e1, r1, t1, e2, r2, t2), r3, e3)", # mix ip
"Pt_re2i": "def Pt_re2i(e1, r1, e2, r2, t1, e3, r3, t2): return Pt(e1, r1, e2i(e2, r2, t1, e3, r3, t2))", # mix ip
"Pt_se2i": "def Pt_se2i(e1, r1, t1, e2, r2, t2, r3, e3): return Pt(e2i(e1, r1, t1, e2, r2, t2), r3, e3)", # mix ip
"Pt_oe2i": "def Pt_oe2i(e1, r1, e2, r2, t1, e3, r3, t2): return Pt(e1, r1, e2i(e2, r2, t1, e3, r3, t2))", # mix ip
"t2i_Pe": "def t2i_Pe(e1, r1, t1, r2, e2, e3, r3, e4): return TimeAnd(Pt(Pe(e1, r1, t1), r2, e2), Pt(e3, r3, e4))", # t-pi
"Pe_t2i": "def Pe_t2i(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, t2i(e2, r2, e3, e4, r3, e5))", # t-ip
"Pe_at2i": "def Pe_at2i(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, after(t2i(e2, r2, e3, e4, r3, e5)))",
"Pe_bt2i": "def Pe_bt2i(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, before(t2i(e2, r2, e3, e4, r3, e5)))",
"Pe_nt2i": "def Pe_nt2i(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, next(t2i(e2, r2, e3, e4, r3, e5)))",
"between": "def between(e1, r1, e2, e3, r2, e4): return TimeAnd(after(Pt(e1, r1, e2)), before(Pt(e3, r2, e4)))", # between(t1, t2) == after t1 and before t2
# 5. entity not
"e2i_N": "def e2i_N(e1, r1, t1, e2, r2, t2): return And(Pe(e1, r1, t1), Not(Pe(e2, r2, t2)))", # 2in
"e3i_N": "def e3i_N(e1, r1, t1, e2, r2, t2, e3, r3, t3): return And3(Pe(e1, r1, t1), Pe(e2, r2, t2), Not(Pe(e3, r3, t3)))", # 3in
"Pe_e2i_Pe_NPe": "def Pe_e2i_Pe_NPe(e1, r1, t1, e2, r2, t2, r3, t3): return Pe(And(Pe(e1, r1, t1), Not(Pe(e2, r2, t2))), r3, t3)", # inp
"e2i_PeN": "def e2i_PeN(e1, r1, t1, r2, t2, e2, r3, t3): return And(Pe(Pe(e1, r1, t1), r2, t2), Not(Pe(e2, r3, t3)))", # pin
"e2i_NPe": "def e2i_NPe(e1, r1, t1, r2, t2, e2, r3, t3): return And(Not(Pe(Pe(e1, r1, t1), r2, t2)), Pe(e2, r3, t3))", # pni = e2i_N(Pe(e1, r1, t1), r2, t2, e2, r3, t3)
# 6. time not
"t2i_N": "def t2i_N(e1, r1, e2, e3, r2, e4): return TimeAnd(Pt(e1, r1, e2), TimeNot(Pt(e3, r2, e4)))", # t-2in
"t3i_N": "def t3i_N(e1, r1, e2, e3, r2, e4, e5, r3, e6): return TimeAnd3(Pt(e1, r1, e2), Pt(e3, r2, e4), TimeNot(Pt(e5, r3, e6)))", # t-3in
"Pe_t2i_PtPe_NPt": "def Pe_t2i_PtPe_NPt(e1, r1, e2, r2, t2, r3, e3, e4, r4, e5): return Pe(e1, r1, TimeAnd(Pt(Pe(e2, r2, t2), r3, e3), TimeNot(Pt(e4, r4, e5))))", # t-inp
"t2i_PtN": "def t2i_PtN(e1, r1, t1, r2, e2, e3, r3, e4): return TimeAnd(Pt(Pe(e1, r1, t1), r2, e2), TimeNot(Pt(e3, r3, e4)))", # t-pin
"t2i_NPt": "def t2i_NPt(e1, r1, t1, r2, e2, e3, r3, e4): return TimeAnd(TimeNot(Pt(Pe(e1, r1, t1), r2, e2)), Pt(e3, r3, e4))", # t-pni
# 7. entity union & time union
"e2u": "def e2u(e1, r1, t1, e2, r2, t2): return Or(Pe(e1, r1, t1), Pe(e2, r2, t2))", # 2u
"Pe_e2u": "def Pe_e2u(e1, r1, t1, e2, r2, t2, r3, t3): return Pe(Or(Pe(e1, r1, t1), Pe(e2, r2, t2)), r3, t3)", # up
"t2u": "def t2u(e1, r1, e2, e3, r2, e4): return TimeOr(Pt(e1, r1, e2), Pt(e3, r2, e4))", # t-2u
"Pe_t2u": "def Pe_t2u(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, TimeOr(Pt(e2, r2, e3), Pt(e4, r3, e5)))", # t-up
# 8. union-DM
"e2u_DM": "def e2u_DM(e1, r1, t1, e2, r2, t2): return Not(And(Not(Pe(e1, r1, t1)), Not(Pe(e2, r2, t2))))", # 2u-DM
"Pe_e2u_DM": "def Pe_e2u_DM(e1, r1, t1, e2, r2, t2, r3, t3): return Pe(Not(And(Not(Pe(e1, r1, t1)), Not(Pe(e2, r2, t2)))), r3, t3)", # up-DM
"t2u_DM": "def t2u_DM(e1, r1, e2, e3, r2, e4): return TimeNot(TimeAnd(TimeNot(Pt(e1, r1, e2)), TimeNot(Pt(e3, r2, e4))))", # t-2u-DM
"Pe_t2u_DM": "def Pe_t2u_DM(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, TimeNot(TimeAnd(TimeNot(Pt(e2, r2, e3)), TimeNot(Pt(e4, r3, e5)))))", # t-up-DM
# 9. union-DNF
"e2u_DNF": "def e2u_DNF(e1, r1, t1, e2, r2, t2): return Pe(e1, r1, t1), Pe(e2, r2, t2)", # 2u_DNF
"Pe_e2u_DNF": "def Pe_e2u_DNF(e1, r1, t1, e2, r2, t2, r3, t3): return Pe(Pe(e1, r1, t1), r3, t3), Pe(Pe(e2, r2, t2), r3, t3)", # up_DNF
"t2u_DNF": "def t2u_DNF(e1, r1, e2, e3, r2, e4): return Pt(e1, r1, e2), Pt(e3, r2, e4)", # t-2u_DNF
"Pe_t2u_DNF": "def Pe_t2u_DNF(e1, r1, e2, r2, e3, e4, r3, e5): return Pe(e1, r1, Pt(e2, r2, e3)), Pe(e1, r1, Pt(e4, r3, e5))", # t-up_DNF
}
union_query_structures: List[str] = [
"e2u",
"Pe_e2u", # 2u, up
"t2u",
"Pe_t2u", # t-2u, t-up
]
train_query_structures: List[str] = [
# entity
"Pe",
"Pe2",
"Pe3",
"e2i",
"e3i", # 1p, 2p, 3p, 2i, 3i
"e2i_NPe",
"e2i_PeN",
"Pe_e2i_Pe_NPe",
"e2i_N",
"e3i_N", # npi, pni, inp, 2in, 3in
# time
"Pt",
"Pt_lPe",
"Pt_rPe",
"Pe_Pt",
"Pe_aPt",
"Pe_bPt",
"Pe_nPt", # t-1p, t-2p
"t2i",
"t3i",
"Pt_le2i",
"Pt_re2i",
"Pe_t2i",
"Pe_at2i",
"Pe_bt2i",
"Pe_nt2i",
"between", # t-2i, t-3i
"t2i_NPt",
"t2i_PtN",
"Pe_t2i_PtPe_NPt",
"t2i_N",
"t3i_N", # t-npi, t-pni, t-inp, t-2in, t-3in
]
test_query_structures: List[str] = train_query_structures + [
# entity
"e2i_Pe",
"Pe_e2i", # pi, ip
"e2u",
"Pe_e2u", # 2u, up
# time
"t2i_Pe",
"Pe_t2i", # t-pi, t-ip
"t2u",
"Pe_t2u", # t-2u, t-up
# union-DM
"e2u_DM",
"Pe_e2u_DM", # 2u-DM, up-DM
"t2u_DM",
"Pe_t2u_DM", # t-2u-DM, t-up-DM
]
_AUTHOR = "linxy"
_DATASET = "ICEWS14"
_URLS = {
name: hf_hub_url(f"{_AUTHOR}/{_DATASET}", filename=f"zips/{name}.zip", repo_type="dataset")
for name in ["all"] + list(query_name_to_args.keys())
} | {
"meta": hf_hub_url(f"{_AUTHOR}/{_DATASET}", filename="meta.json", repo_type="dataset")
}
class ICEWS14Dataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
STANDARD_BUILDER_CONFIGS = [
datasets.BuilderConfig(
name=query_name,
version=datasets.Version("1.0.0"),
description=query_structures[query_name],
)
for query_name in list(query_name_to_args.keys())
]
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="meta",
version=VERSION,
description=f"The meta of data, including entity/relation/timestamp count, entity2idx, relation2idx, timestamp2idx, etc.",
),
datasets.BuilderConfig(
name="all",
version=VERSION,
description=f"All types of queries. Train: {train_query_structures}, Valid | Test: {test_query_structures}",
),
] + STANDARD_BUILDER_CONFIGS
DEFAULT_CONFIG_NAME = "all" # It's not mandatory to have a default configuration. Just use one if it make sense.
def _info(self):
if self.config.name == "meta":
features = datasets.Features(
{
"dataset": datasets.Value("string"),
"entity_count": datasets.Value("int32"),
"relation_count": datasets.Value("int32"),
"timestamp_count": datasets.Value("int32"),
"valid_triples_count": datasets.Value("int32"),
"test_triples_count": datasets.Value("int32"),
"train_triples_count": datasets.Value("int32"),
"triple_count": datasets.Value("int32"),
"query_meta": datasets.Sequence(
feature={
"query_name": datasets.Value("string"),
"queries_count": datasets.Value("int32"),
"avg_answers_count": datasets.Value("float"),
"train": {
"queries_count": datasets.Value("int32"),
"avg_answers_count": datasets.Value("float"),
},
"valid": {
"queries_count": datasets.Value("int32"),
"avg_answers_count": datasets.Value("float"),
},
"test": {
"queries_count": datasets.Value("int32"),
"avg_answers_count": datasets.Value("float"),
},
}
),
"entity2idx": datasets.Sequence(
feature={
"name": datasets.Value("string"),
"id": datasets.Value("int32"),
}
),
"relation2idx": datasets.Sequence(
feature={
"name": datasets.Value("string"),
"id": datasets.Value("int32"),
}
),
"timestamp2idx": datasets.Sequence(
feature={
"name": datasets.Value("string"),
"id": datasets.Value("int32"),
}
),
}
)
else:
features = datasets.Features(
{
"query_name": datasets.Value("string"),
"definition": datasets.Value("string"),
"query": datasets.Sequence(feature=datasets.Value("int32")),
"answer": datasets.Sequence(feature=datasets.Value("int32")),
"easy_answer": datasets.Sequence(feature=datasets.Value("int32")),
"args": datasets.Sequence(feature=datasets.Value("string")),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager: datasets.download.DownloadManager):
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
url = _URLS[self.config.name]
if self.config.name == "meta":
data_file = dl_manager.download(_URLS["meta"])
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_file,
"split": "meta",
},
)
]
data_dir = dl_manager.download_and_extract(url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(data_dir, "train.jsonl"),
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(data_dir, "valid.jsonl"),
"split": "valid",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(data_dir, "test.jsonl"),
"split": "test",
},
),
]
def _generate_examples(self, filepath, split):
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
# This method yields (key, example) tuples from the dataset.
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
if not os.path.exists(filepath):
return
if split == "meta":
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
yield 0, data
return
with open(filepath, "r", encoding="utf-8") as f:
for key, row in enumerate(f):
data = json.loads(row)
query_name = data["query_name"]
easy_answer = data["easy_answer"] if "easy_answer" in data else []
yield key, {
"query_name": query_name,
"query": data["query"],
"answer": data["answer"],
"easy_answer": easy_answer,
"args": query_name_to_args[query_name],
"definition": query_structures[query_name],
}