Load samples for a specific task type from the unified config.
(self, task_config)
| 201 | self.comparison_samples = self._load_task_samples(cfg["compare"]) |
| 202 | |
| 203 | def _load_task_samples(self, task_config): |
| 204 | """Load samples for a specific task type from the unified config.""" |
| 205 | samples = [] |
| 206 | |
| 207 | if not isinstance(task_config, list): |
| 208 | task_config = [task_config] |
| 209 | |
| 210 | for ds in task_config: |
| 211 | path = ds.get("json_path") |
| 212 | strategy = ds.get("sampling_strategy", "all") |
| 213 | image_root = ds.get("image_root") |
| 214 | sample = ds.get("sample") # New field for sample count |
| 215 | random_sample = ds.get("random", False) # New field for random sampling |
| 216 | |
| 217 | if path.endswith(".jsonl"): |
| 218 | data_list = [json.loads(line) for line in open(path, "r")] |
| 219 | elif path.endswith(".json"): |
| 220 | data_list = json.load(open(path, "r")) |
| 221 | else: |
| 222 | raise ValueError(f"Unsupported file type: {path}") |
| 223 | |
| 224 | # Handle sampling strategy |
| 225 | count = None |
| 226 | if ":" in strategy: |
| 227 | strat, num_s = strategy.split(":") |
| 228 | if "%" in num_s: |
| 229 | count = math.ceil(int(num_s.rstrip("%")) * len(data_list) / 100) |
| 230 | else: |
| 231 | count = int(num_s) |
| 232 | strategy = strat |
| 233 | elif sample is not None: |
| 234 | # Use the new 'sample' field if available |
| 235 | count = sample |
| 236 | strategy = "random" if random_sample else "first" |
| 237 | |
| 238 | if strategy == "first" and count is not None: |
| 239 | data_list = data_list[:count] |
| 240 | elif strategy == "end" and count is not None: |
| 241 | data_list = data_list[-count:] |
| 242 | elif strategy == "random" and count is not None: |
| 243 | random.shuffle(data_list) |
| 244 | data_list = data_list[:count] |
| 245 | |
| 246 | for sample in data_list: |
| 247 | sample['image_root'] = image_root |
| 248 | |
| 249 | samples.extend(data_list) |
| 250 | return samples |
| 251 | |
| 252 | def _load_samples_from_yaml(self, data_path: str): |
| 253 | samples = [] |
no test coverage detected