AISDL-SNU-Admin commited on
Commit
c91543d
·
verified ·
1 Parent(s): 53d39d8

Add LithoBench_PDE.py

Browse files
Files changed (1) hide show
  1. LithoBench_PDE.py +133 -0
LithoBench_PDE.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ LithoBench-PDE: PyTorch Dataset for computational lithography PDE benchmark.
3
+
4
+ Usage (local):
5
+ from LithoBench_PDE import LithoBenchPDE
6
+ ds = LithoBenchPDE("./LithoBench_PDE", categories=["Contact_I"])
7
+ sample = ds[0] # dict with M, E, h, m, R, T, sample_id, category
8
+
9
+ Usage (from HuggingFace Hub):
10
+ from LithoBench_PDE import LithoBenchPDE
11
+ ds = LithoBenchPDE.from_hub("AISDL-SNU/LithoBench-PDE", categories=["Contact_I"])
12
+ sample = ds[0]
13
+ """
14
+
15
+ import os
16
+ import glob
17
+ from typing import Optional
18
+
19
+ import torch
20
+ from torch.utils.data import Dataset
21
+
22
+
23
+ CATEGORIES = [
24
+ "Contact_I",
25
+ "Contact_T",
26
+ "Metal_I",
27
+ "Metal_T",
28
+ ]
29
+
30
+ TASKS = {
31
+ "maxwell": ("M", "E"),
32
+ "reaction_diffusion": ("h", "m"),
33
+ "eikonal": ("R", "T"),
34
+ }
35
+
36
+
37
+ class LithoBenchPDE(Dataset):
38
+ """PyTorch Dataset for LithoBench-PDE.
39
+
40
+ Args:
41
+ root: Path to the root directory containing category subdirectories.
42
+ categories: List of categories to include. None = all categories.
43
+ task: If set, one of 'maxwell', 'reaction_diffusion', 'eikonal'.
44
+ Returns only (input, target) for that task instead of all fields.
45
+ """
46
+
47
+ def __init__(
48
+ self,
49
+ root: str,
50
+ categories: Optional[list[str]] = None,
51
+ task: Optional[str] = None,
52
+ ):
53
+ self.root = root
54
+ self.task = task
55
+
56
+ if task is not None and task not in TASKS:
57
+ raise ValueError(f"Unknown task '{task}'. Choose from {list(TASKS.keys())}")
58
+
59
+ cats = categories or CATEGORIES
60
+ self.files = []
61
+ for cat in cats:
62
+ cat_dir = os.path.join(root, cat)
63
+ if not os.path.isdir(cat_dir):
64
+ raise FileNotFoundError(f"Category directory not found: {cat_dir}")
65
+ pt_files = sorted(glob.glob(os.path.join(cat_dir, "*.pt")))
66
+ for f in pt_files:
67
+ self.files.append((cat, f))
68
+
69
+ @classmethod
70
+ def from_hub(
71
+ cls,
72
+ repo_id: str = "AISDL-SNU/LithoBench-PDE",
73
+ categories: Optional[list[str]] = None,
74
+ task: Optional[str] = None,
75
+ cache_dir: Optional[str] = None,
76
+ ):
77
+ """Download from HuggingFace Hub and create dataset.
78
+
79
+ Args:
80
+ repo_id: HuggingFace dataset repository ID.
81
+ categories: List of categories to download. None = all.
82
+ task: Optional PDE task filter.
83
+ cache_dir: Local cache directory for downloaded files.
84
+ """
85
+ from huggingface_hub import snapshot_download
86
+
87
+ cats = categories or CATEGORIES
88
+ patterns = [f"LithoBench_PDE/{cat}/*.pt" for cat in cats]
89
+
90
+ local_dir = snapshot_download(
91
+ repo_id,
92
+ repo_type="dataset",
93
+ allow_patterns=patterns,
94
+ cache_dir=cache_dir,
95
+ )
96
+ root = os.path.join(local_dir, "LithoBench_PDE")
97
+ return cls(root, categories=categories, task=task)
98
+
99
+ def __len__(self):
100
+ return len(self.files)
101
+
102
+ def __getitem__(self, idx):
103
+ category, filepath = self.files[idx]
104
+ sample_id = os.path.splitext(os.path.basename(filepath))[0]
105
+ data = torch.load(filepath, map_location="cpu", weights_only=False)
106
+
107
+ if self.task is not None:
108
+ inp_key, tgt_key = TASKS[self.task]
109
+ return {
110
+ "input": data[inp_key],
111
+ "target": data[tgt_key],
112
+ "sample_id": sample_id,
113
+ "category": category,
114
+ }
115
+
116
+ return {
117
+ "M": data["M"],
118
+ "E": data["E"],
119
+ "h": data["h"],
120
+ "m": data["m"],
121
+ "R": data["R"],
122
+ "T": data["T"],
123
+ "sample_id": sample_id,
124
+ "category": category,
125
+ }
126
+
127
+ def __repr__(self):
128
+ cats = sorted(set(cat for cat, _ in self.files))
129
+ task_str = f", task='{self.task}'" if self.task else ""
130
+ return (
131
+ f"LithoBenchPDE(samples={len(self)}, "
132
+ f"categories={cats}{task_str})"
133
+ )