DsL commited on
Commit
b31a43e
·
1 Parent(s): bec6900

Add explicit config list

Browse files
Files changed (1) hide show
  1. dataset.py +134 -0
dataset.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import glob
3
+ from pathlib import Path
4
+ from typing import List
5
+
6
+ import datasets
7
+
8
+ _CITATION = """\
9
+ @article{liu2025causal3d,
10
+ title={CAUSAL3D: A Comprehensive Benchmark for Causal Learning from Visual Data},
11
+ author={Liu, Disheng and Qiao, Yiran and Liu, Wuche and Lu, Yiren and Zhou, Yunlai and Liang, Tuo and Yin, Yu and Ma, Jing},
12
+ journal={arXiv preprint arXiv:2503.04852},
13
+ year={2025}
14
+ }
15
+ """
16
+
17
+ _DESCRIPTION = """\
18
+ Causal3D is a benchmark for evaluating causal reasoning in physical and hypothetical visual scenes.
19
+ It includes both real-world recordings and rendered synthetic scenes demonstrating causal interactions.
20
+ """
21
+
22
+ _HOMEPAGE = "https://huggingface.co/datasets/LLDDSS/Causal3D"
23
+ _LICENSE = "CC-BY-4.0"
24
+
25
+ # Automatically discover configs from folder names
26
+ def _discover_configs(base_dir: str) -> List[datasets.BuilderConfig]:
27
+ configs = []
28
+ for root_folder in ["real_scenes", "hypothetical_scenes"]:
29
+ full_path = os.path.join(base_dir, root_folder)
30
+ if not os.path.exists(full_path):
31
+ continue
32
+ for scene in sorted(os.listdir(full_path)):
33
+ scene_path = os.path.join(full_path, scene)
34
+ if os.path.isdir(scene_path):
35
+ config_name = f"{root_folder}_{scene}"
36
+ configs.append(
37
+ datasets.BuilderConfig(
38
+ name=config_name,
39
+ version=datasets.Version("1.0.0"),
40
+ description=f"{scene} in {root_folder}",
41
+ )
42
+ )
43
+ return configs
44
+
45
+
46
+ def _discover_configs(base_dir: str) -> List[datasets.BuilderConfig]:
47
+ configs = []
48
+ for root_folder in ["real_scenes", "hypothetical_scenes"]:
49
+ full_path = os.path.join(base_dir, root_folder)
50
+ if not os.path.exists(full_path):
51
+ continue
52
+ for scene in sorted(os.listdir(full_path)):
53
+ scene_path = os.path.join(full_path, scene)
54
+ if os.path.isdir(scene_path):
55
+ config_name = f"{root_folder}_{scene}"
56
+ configs.append(
57
+ datasets.BuilderConfig(
58
+ name=config_name,
59
+ version=datasets.Version("1.0.0"),
60
+ description=f"{scene} in {root_folder}",
61
+ )
62
+ )
63
+ return configs
64
+
65
+
66
+ class Causal3D(datasets.GeneratorBasedBuilder):
67
+ BUILDER_CONFIGS = [
68
+ datasets.BuilderConfig(
69
+ name="hypothetical_scenes_Hypothetic_v2_linear",
70
+ version=datasets.Version("1.0.0"),
71
+ description="Hypothetic v2 linear scene with images and csv."
72
+ ),
73
+ datasets.BuilderConfig(
74
+ name="real_scenes_Real_Parabola",
75
+ version=datasets.Version("1.0.0"),
76
+ description="Real Parabola scene."
77
+ ),
78
+ ]
79
+
80
+ def _info(self):
81
+ return datasets.DatasetInfo(
82
+ description=_DESCRIPTION,
83
+ features=datasets.Features({
84
+ "image": datasets.Image(),
85
+ "file_name": datasets.Value("string"),
86
+ "metadata": datasets.Value("string"), # optionally replace with structured fields
87
+ }),
88
+ homepage=_HOMEPAGE,
89
+ license=_LICENSE,
90
+ citation=_CITATION,
91
+ )
92
+
93
+ def _split_generators(self, dl_manager):
94
+ parts = self.config.name.split("_", 2)
95
+ category = parts[0] + "_" + parts[1] # real_scenes or hypothetical_scenes
96
+ scene = parts[2]
97
+ data_dir = os.path.join(category, scene)
98
+
99
+ return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN,
102
+ gen_kwargs={"data_dir": data_dir},
103
+ )
104
+ ]
105
+
106
+ def _generate_examples(self, data_dir):
107
+ # Find the .csv file
108
+ csv_files = list(Path(data_dir).rglob("*.csv"))
109
+ if not csv_files:
110
+ raise FileNotFoundError(f"No CSV metadata found in {data_dir}")
111
+ csv_path = csv_files[0]
112
+ df = pd.read_csv(csv_path)
113
+
114
+ # Make sure a 'file_name' column exists
115
+ if "file_name" not in df.columns:
116
+ raise ValueError(f"'file_name' column not found in {csv_path.name}")
117
+
118
+ # Load image paths
119
+ image_files = {}
120
+ for ext in ("*.png", "*.jpg", "*.jpeg"):
121
+ for img_path in Path(data_dir).rglob(ext):
122
+ relative_path = str(img_path.relative_to(data_dir))
123
+ image_files[relative_path] = str(img_path)
124
+
125
+ # Match CSV rows with image paths
126
+ for idx, row in df.iterrows():
127
+ fname = row["file_name"]
128
+ if fname not in image_files:
129
+ continue # skip missing images
130
+ yield idx, {
131
+ "image": image_files[fname],
132
+ "file_name": fname,
133
+ "metadata": row.to_json(),
134
+ }