Unified data loading interface. Loads and processes data from Hugging Face Hub. Each task's dataset is grouped by a 'group' key. The function returns a dictionary mapping each group key to a tuple of (features, labels). - features (X): A numpy array of shape (n_samples, n_features
(
app_name: str,
train: bool = True,
)
| 50 | } |
| 51 | |
| 52 | def load_data( |
| 53 | app_name: str, |
| 54 | train: bool = True, |
| 55 | ) -> Dict[Any, Tuple[np.ndarray, np.ndarray]]: |
| 56 | """ |
| 57 | Unified data loading interface. Loads and processes data from Hugging Face Hub. |
| 58 | |
| 59 | Each task's dataset is grouped by a 'group' key. The function returns a |
| 60 | dictionary mapping each group key to a tuple of (features, labels). |
| 61 | - features (X): A numpy array of shape (n_samples, n_features). |
| 62 | - labels (y): A numpy array of shape (n_samples,) or (n_samples, n_targets). |
| 63 | |
| 64 | Args: |
| 65 | app_name: The name of the task (e.g., 'sft_scaling_law'). |
| 66 | train: If True, load training data; otherwise, load test data. |
| 67 | |
| 68 | Returns: |
| 69 | A dictionary containing the prepared data, structured by group. |
| 70 | """ |
| 71 | if app_name not in TASK_SCHEMA_MAP: |
| 72 | raise ValueError(f"Task '{app_name}' not found in TASK_SCHEMA_MAP. Available tasks: {list(TASK_SCHEMA_MAP.keys())}") |
| 73 | |
| 74 | split = 'train' if train else 'test' |
| 75 | schema = TASK_SCHEMA_MAP[app_name] |
| 76 | |
| 77 | try: |
| 78 | # Load the specific task dataset from the Hugging Face Hub |
| 79 | dataset = datasets.load_dataset(HUB_REPO_ID, name=app_name, split=split) |
| 80 | except Exception as e: |
| 81 | raise IOError(f"Failed to load dataset '{app_name}' with split '{split}' from '{HUB_REPO_ID}'. Reason: {e}") |
| 82 | |
| 83 | # Ensure target_name is a list for consistent processing |
| 84 | feature_names = schema["feature_names"] |
| 85 | target_names = schema["target_name"] |
| 86 | if not isinstance(target_names, list): |
| 87 | target_names = [target_names] |
| 88 | |
| 89 | processed_data = {} |
| 90 | |
| 91 | # The datasets are partitioned by a 'group' column |
| 92 | unique_groups = sorted(list(set(dataset['group']))) |
| 93 | |
| 94 | for group_key in unique_groups: |
| 95 | # Filter the dataset for the current group |
| 96 | group_data = dataset.filter(lambda example: example['group'] == group_key) |
| 97 | |
| 98 | # Extract features (X) and stack them into a single numpy array |
| 99 | X_list = [np.array(group_data[fname]) for fname in feature_names] |
| 100 | X = np.stack(X_list, axis=1) |
| 101 | |
| 102 | # Extract targets (y) |
| 103 | y_list = [np.array(group_data[tname]) for tname in target_names] |
| 104 | y_stacked = np.stack(y_list, axis=1) |
| 105 | |
| 106 | # Squeeze the last dimension if there is only one target |
| 107 | y = y_stacked.squeeze(axis=1) if y_stacked.shape[1] == 1 else y_stacked |
| 108 | |
| 109 | processed_data[group_key] = (X, y) |
no outgoing calls
no test coverage detected