Create dataset.py
Browse files- dataset.py +98 -0
dataset.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import h5py
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import datasets
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BubbleMLConfig(datasets.BuilderConfig):
|
| 9 |
+
def __init__(self, subset_dirs, train_files_dict, test_files_dict, **kwargs):
|
| 10 |
+
super().__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 11 |
+
self.subset_dirs = subset_dirs # e.g., ["SingleBubble-Saturated-FC72-2D", "SingleBubble-Saturated-R515B-2D"]
|
| 12 |
+
self.train_files_dict = train_files_dict # dict mapping dir -> list of filenames
|
| 13 |
+
self.test_files_dict = test_files_dict
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class BubbleMLDataset(datasets.GeneratorBasedBuilder):
|
| 17 |
+
BUILDER_CONFIGS = [
|
| 18 |
+
BubbleMLConfig(
|
| 19 |
+
name="single-bubble",
|
| 20 |
+
description="Combined dataset for single bubble boiling (FC72 and R515B)",
|
| 21 |
+
subset_dirs=["SingleBubble-Saturated-FC72-2D", "SingleBubble-Saturated-R515B-2D"],
|
| 22 |
+
train_files_dict={
|
| 23 |
+
"SingleBubble-Saturated-FC72-2D": [
|
| 24 |
+
"Twall_90.hdf5", "Twall_91.hdf5", "Twall_92.hdf5", "Twall_94.hdf5",
|
| 25 |
+
"Twall_96.hdf5", "Twall_98.hdf5", "Twall_99.hdf5", "Twall_100.hdf5"
|
| 26 |
+
],
|
| 27 |
+
"SingleBubble-Saturated-R515B-2D": [
|
| 28 |
+
"Twall_13.hdf5", "Twall_14.hdf5", "Twall_15.hdf5", "Twall_17.hdf5",
|
| 29 |
+
"Twall_19.hdf5", "Twall_21.hdf5", "Twall_22.hdf5", "Twall_23.hdf5"
|
| 30 |
+
],
|
| 31 |
+
},
|
| 32 |
+
test_files_dict={
|
| 33 |
+
"SingleBubble-Saturated-FC72-2D": ["Twall_87.hdf5", "Twall_95.hdf5", "Twall_103.hdf5"],
|
| 34 |
+
"SingleBubble-Saturated-R515B-2D": ["Twall_10.hdf5", "Twall_18.hdf5", "Twall_26.hdf5"],
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
def _info(self):
|
| 40 |
+
return datasets.DatasetInfo(
|
| 41 |
+
description="BubbleML: Boiling simulations with predefined train/test splits used in BubbleFormer.",
|
| 42 |
+
features=datasets.Features({
|
| 43 |
+
"input": datasets.Array3D(shape=(5, 4, None, None), dtype="float32"),
|
| 44 |
+
"output": datasets.Array3D(shape=(5, 4, None, None), dtype="float32"),
|
| 45 |
+
"fluid_params": datasets.Sequence(datasets.Value("float32"))
|
| 46 |
+
}),
|
| 47 |
+
supervised_keys=None,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
def _split_generators(self, dl_manager):
|
| 51 |
+
config = self.config
|
| 52 |
+
base_dir = dl_manager.download_and_extract(".")
|
| 53 |
+
|
| 54 |
+
def resolve_paths(files_dict):
|
| 55 |
+
resolved = []
|
| 56 |
+
for subdir, files in files_dict.items():
|
| 57 |
+
for f in files:
|
| 58 |
+
resolved.append(os.path.join(base_dir, subdir, f))
|
| 59 |
+
return resolved
|
| 60 |
+
|
| 61 |
+
train_files = resolve_paths(config.train_files_dict)
|
| 62 |
+
test_files = resolve_paths(config.test_files_dict)
|
| 63 |
+
|
| 64 |
+
return [
|
| 65 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": train_files}),
|
| 66 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": test_files}),
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
def _generate_examples(self, files):
|
| 70 |
+
for idx, fpath in enumerate(files):
|
| 71 |
+
with h5py.File(fpath, "r") as h5f:
|
| 72 |
+
input_data = np.stack([h5f[k][:5] for k in ["dfun", "temperature", "velx", "vely"]])
|
| 73 |
+
output_data = np.stack([h5f[k][5:10] for k in ["dfun", "temperature", "velx", "vely"]])
|
| 74 |
+
|
| 75 |
+
# Read matching JSON metadata
|
| 76 |
+
json_path = fpath.replace(".hdf5", ".json")
|
| 77 |
+
if os.path.exists(json_path):
|
| 78 |
+
with open(json_path, "r") as jf:
|
| 79 |
+
params = json.load(jf)
|
| 80 |
+
fluid_params = [
|
| 81 |
+
params["inv_reynolds"],
|
| 82 |
+
params["cpgas"],
|
| 83 |
+
params["mugas"],
|
| 84 |
+
params["rhogas"],
|
| 85 |
+
params["thcogas"],
|
| 86 |
+
params["stefan"],
|
| 87 |
+
params["prandtl"],
|
| 88 |
+
params["heater"]["nucWaitTime"],
|
| 89 |
+
params["heater"]["wallTemp"],
|
| 90 |
+
]
|
| 91 |
+
else:
|
| 92 |
+
fluid_params = []
|
| 93 |
+
|
| 94 |
+
yield idx, {
|
| 95 |
+
"input": input_data.astype(np.float32),
|
| 96 |
+
"output": output_data.astype(np.float32),
|
| 97 |
+
"fluid_params": fluid_params
|
| 98 |
+
}
|