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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""Package benchmark tasks as the SimplexTasks-12 release artifact."""
from __future__ import annotations

import argparse
import json
import shutil
from pathlib import Path

import numpy as np
import yaml

import sys
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from scripts.run_age_ldl import extract_image_features, get_age_predictions, load_utkface
from scripts.run_hyperspectral import load_hyperspectral, unmix_nmf
from scripts.run_topics import prepare_topic_data
from src.data import build_prediction_matrix, load_affective_text
from src.dgp.deconv import nnls_deconv
from src.dgp.discrete_groups import DiscreteGroupsDGP
from src.dgp.heavy_tail import HeavyTailDGP
from src.dgp.high_k import HighKDGP
from src.dgp.model_bias import ModelBiasDGP
from src.dgp.pseudobulk import generate_pseudobulk
from src.dgp.pure_scale import PureScaleDGP

PACKAGE_NAME = "SimplexTasks-12"
PACKAGE_SLUG = "simplextasks-12"
VERSION = "0.1.0"
SEED = 2026

DGP_MAP = {
    "pure_scale": PureScaleDGP,
    "discrete_groups": DiscreteGroupsDGP,
    "model_bias": ModelBiasDGP,
    "heavy_tail": HeavyTailDGP,
    "high_k": HighKDGP,
}

SYNTHETIC_SPECS = [
    ("d1_homogeneous", "configs/synthetic/d1_homogeneous.yaml", "Homogeneous negative control"),
    ("d2_pure_scale", "configs/synthetic/d2_pure_scale.yaml", "Smooth scale heterogeneity"),
    ("d3_discrete_groups_aligned", "configs/synthetic/d3_discrete_groups_aligned.yaml", "Aligned discrete groups"),
    ("d4_model_bias", "configs/synthetic/d4_model_bias.yaml", "Bias-type heterogeneity"),
    ("d5_heavy_tail", "configs/synthetic/d5_heavy_tail.yaml", "Heavy-tail robustness"),
    ("d6_high_k", "configs/synthetic/d6_high_k.yaml", "High-dimensional simplex"),
]


def _float32(x: np.ndarray) -> np.ndarray:
    return x.astype(np.float32) if isinstance(x, np.ndarray) and x.dtype.kind == "f" else x


def save_npz(path: Path, arrays: dict[str, np.ndarray]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    np.savez_compressed(path, **{k: _float32(v) for k, v in arrays.items()})


def write_json(path: Path, payload: dict) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(payload, indent=2), encoding="utf-8")


def write_text(path: Path, text: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(text.strip() + "\n", encoding="utf-8")


def build_cifar(root: Path) -> tuple[dict[str, np.ndarray], dict]:
    cache_path = root / "data/processed/cifar10_resnet18_softmax.npz"
    data = np.load(cache_path)
    arrays = {
        "Y": data["Y"],
        "U": data["U"],
        "class_names": data["class_names"],
        "label_index": np.argmax(data["Y"], axis=1).astype(np.int16),
    }
    meta = {
        "task_id": "cifar10_softmax",
        "task_name": "CIFAR-10 softmax",
        "subset": "real",
        "n_samples": int(arrays["Y"].shape[0]),
        "simplex_dim": int(arrays["Y"].shape[1]),
        "source_asset": "CIFAR-10",
        "predictor": "Frozen ResNet-18 softmax cache",
        "redistribution": "derived-only",
        "notes": "No raw images are redistributed in this package.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_topics() -> tuple[dict[str, np.ndarray], dict]:
    Y, U = prepare_topic_data(K=10, seed=SEED)
    arrays = {
        "Y": Y,
        "U": U,
        "doc_index": np.arange(len(Y), dtype=np.int32),
    }
    meta = {
        "task_id": "topics_20ng",
        "task_name": "20 Newsgroups topics",
        "subset": "real",
        "n_samples": int(Y.shape[0]),
        "simplex_dim": int(Y.shape[1]),
        "source_asset": "20 Newsgroups",
        "predictor": "TF-IDF to topic-mixture kNN regressor",
        "redistribution": "derived-only",
        "notes": "This release exposes only derived simplex arrays, not the raw text corpus.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_samson(root: Path) -> tuple[dict[str, np.ndarray], dict]:
    data = load_hyperspectral(str(root / "data/raw/hyperspectral"), "samson")
    U = unmix_nmf(data["pixels"], data["K"], seed=SEED)
    arrays = {
        "Y": data["abundances"],
        "U": U,
        "pixels": data["pixels"],
        "endmembers": data["endmembers"],
        "endmember_names": np.asarray(data["names"]),
        "image_shape": np.asarray(data["shape"], dtype=np.int32),
    }
    meta = {
        "task_id": "samson_unmixing",
        "task_name": "Samson hyperspectral unmixing",
        "subset": "real",
        "n_samples": int(arrays["Y"].shape[0]),
        "simplex_dim": int(arrays["Y"].shape[1]),
        "source_asset": "Samson ROI hyperspectral benchmark",
        "predictor": "NMF abundance estimator",
        "redistribution": "source-cited",
        "notes": "The public bundle did not ship an explicit license file; keep attribution with any downstream reuse.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_pbmc(root: Path) -> tuple[dict[str, np.ndarray], dict]:
    import scanpy as sc
    from sklearn.cluster import KMeans
    from sklearn.decomposition import PCA

    h5ad_path = root / "data/pbmc3k_raw.h5ad"
    adata = sc.read_h5ad(h5ad_path)
    expr = adata.X
    if hasattr(expr, "toarray"):
        expr = expr.toarray()
    expr = np.asarray(expr, dtype=np.float64)

    celltype_key = "cell_type"
    if celltype_key in adata.obs.columns:
        labels = adata.obs[celltype_key].values
    else:
        pca = PCA(n_components=30, random_state=42)
        X_pca = pca.fit_transform(expr)
        kmeans = KMeans(n_clusters=8, random_state=42, n_init=10)
        labels = np.asarray([f"ct_{v}" for v in kmeans.fit_predict(X_pca)])
    cell_type_names = sorted(np.unique(labels).tolist())
    gene_names = adata.var_names.to_numpy()

    pb = generate_pseudobulk(
        expr=expr,
        labels=labels,
        cell_type_names=cell_type_names,
        gene_names=gene_names.tolist(),
        n_samples=5000,
        cells_per_sample=200,
        concentration=1.0,
        noise_sd=0.1,
        seed=SEED,
    )
    U = nnls_deconv(pb.bulk, pb.signature)
    arrays = {
        "Y": pb.proportions,
        "U": U,
        "bulk_expression": pb.bulk,
        "signature": pb.signature,
        "cell_type_names": np.asarray(pb.cell_type_names),
        "gene_names": np.asarray(pb.gene_names),
    }
    meta = {
        "task_id": "pbmc3k_pseudobulk",
        "task_name": "PBMC3K pseudobulk deconvolution",
        "subset": "real",
        "n_samples": int(arrays["Y"].shape[0]),
        "simplex_dim": int(arrays["Y"].shape[1]),
        "source_asset": "PBMC3K single-cell reference",
        "predictor": "NNLS deconvolution from pseudobulk mixtures",
        "redistribution": "derived-only",
        "notes": "This package exports pseudobulk-derived arrays and signatures rather than the raw single-cell matrix.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_utkface(root: Path) -> tuple[dict[str, np.ndarray], dict]:
    data_dir = root / "data/raw/UTKFace"
    ages, Y, image_paths = load_utkface(str(data_dir), K=10, sigma=2.0)
    U = get_age_predictions(ages, Y, image_paths, K=10, method="image_knn", seed=SEED)
    X_feat = extract_image_features(image_paths, image_size=16, cache_name=f"utkface_imgfeat_{len(image_paths)}_s16.npz")
    arrays = {
        "Y": Y,
        "U": U,
        "age": ages.astype(np.int16),
        "image_features": X_feat,
    }
    meta = {
        "task_id": "utkface_age_ldl",
        "task_name": "UTKFace age label distributions",
        "subset": "real",
        "n_samples": int(arrays["Y"].shape[0]),
        "simplex_dim": int(arrays["Y"].shape[1]),
        "source_asset": "UTKFace aligned-and-cropped images",
        "predictor": "Thumbnail feature PCA+kNN age-distribution regressor",
        "redistribution": "derived-only",
        "notes": "The package omits raw face images and keeps only derived features and simplex arrays.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_affective(root: Path) -> tuple[dict[str, np.ndarray], dict]:
    data_dir = root / "data/raw/AffectiveText.Semeval.2007"
    cache_path = root / "data/processed/affective_text_predictions.jsonl"
    data = load_affective_text(data_dir)
    pred_raw, U = build_prediction_matrix(data["ids"], cache_path)
    arrays = {
        "Y": data["Y"],
        "U": U,
        "gold_raw_scores": data["raw_scores"],
        "pred_raw_scores": pred_raw,
        "instance_id": np.asarray(data["ids"]),
        "emotion_names": np.asarray(data["emotions"]),
    }
    meta = {
        "task_id": "affectivetext_emotions",
        "task_name": "SemEval AffectiveText emotions",
        "subset": "real",
        "n_samples": int(arrays["Y"].shape[0]),
        "simplex_dim": int(arrays["Y"].shape[1]),
        "source_asset": "SemEval-2007 Task 14 AffectiveText",
        "predictor": "Frozen zero-shot emotion scorer; open TF-IDF+SVD+kNN fallback script available",
        "redistribution": "derived-only",
        "notes": "The package omits raw headlines and raw API responses, keeps only derived score arrays, and provides an open fallback cache builder in scripts/cache_affective_text_open_predictions.py.",
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta


def build_synthetic_task(root: Path, task_id: str, config_rel: str, regime_label: str) -> tuple[dict[str, np.ndarray], dict, Path]:
    cfg_path = root / config_rel
    cfg = yaml.safe_load(cfg_path.read_text(encoding="utf-8"))
    dgp_cfg = dict(cfg["dgp"])
    dgp_name = dgp_cfg.pop("name")
    dgp = DGP_MAP[dgp_name](**dgp_cfg)

    data_cfg = cfg["data"]
    n_train = int(data_cfg.get("n_train", 0))
    n_scale = int(data_cfg.get("n_scale_est", 0))
    n_cal = int(data_cfg.get("n_cal", 0))
    n_test = int(data_cfg.get("n_test", 0))
    n_total = n_train + n_scale + n_cal + n_test

    rng = np.random.default_rng(SEED)
    sample = dgp.sample(n_total, rng)
    split = np.concatenate(
        [
            np.repeat("train", n_train),
            np.repeat("scale", n_scale),
            np.repeat("cal", n_cal),
            np.repeat("test", n_test),
        ]
    )
    arrays = {
        "X": sample.X,
        "Y": sample.Y,
        "U": sample.U,
        "R": sample.R,
        "sigma_true": sample.sigma_true if sample.sigma_true is not None else np.full(n_total, np.nan),
        "split": split.astype("U8"),
    }
    meta = {
        "task_id": task_id,
        "task_name": task_id.replace("_", " "),
        "subset": "synthetic",
        "n_samples": int(sample.Y.shape[0]),
        "simplex_dim": int(sample.Y.shape[1]),
        "source_asset": "Synthetic DGP",
        "predictor": "Oracle mean predictor from the configured DGP",
        "redistribution": "open",
        "regime_label": regime_label,
        "config_file": config_rel,
        "seed": SEED,
        "available_arrays": sorted(arrays.keys()),
    }
    return arrays, meta, cfg_path


def write_task(task_dir: Path, arrays: dict[str, np.ndarray], metadata: dict) -> None:
    save_npz(task_dir / "task.npz", arrays)
    write_json(task_dir / "metadata.json", metadata)


def build_package_readme(manifest: dict) -> str:
    real_lines = []
    for task in manifest["real_tasks"]:
        real_lines.append(f"| `{task['task_id']}` | {task['task_name']} | {task['n_samples']} | {task['simplex_dim']} | {task['predictor']} |")
    synthetic_lines = []
    for task in manifest["synthetic_tasks"]:
        synthetic_lines.append(f"| `{task['task_id']}` | {task['regime_label']} | {task['n_samples']} | {task['simplex_dim']} |")
    return f"""
---
pretty_name: {PACKAGE_NAME}
license: other
task_categories:
- other
tags:
- conformal-prediction
- uncertainty-estimation
- simplex
- benchmark
- task-collection
---

# {PACKAGE_NAME}

{PACKAGE_NAME} is the processed task collection behind the SimplexUQ benchmark. It packages 12 simplex-valued prediction tasks: 6 real tasks with fixed predictors and 6 synthetic regimes with canonical reference draws.

## What is inside

- Standardized `task.npz` files with at least `Y` and `U` for every task.
- Per-task `metadata.json` files with provenance, redistribution notes, and task-specific semantics.
- Canonical synthetic configs copied alongside the synthetic tasks.
- A `manifest.json` file that summarizes the full release.

## Real Tasks

| Task ID | Task | Samples | K | Predictor |
| --- | --- | ---: | ---: | --- |
{chr(10).join(real_lines)}

## Synthetic Tasks

| Task ID | Regime | Samples | K |
| --- | --- | ---: | ---: |
{chr(10).join(synthetic_lines)}

## Redistribution Notes

This package is release-oriented rather than raw-data-complete. Some tasks include only derived simplex arrays or derived features because the underlying source assets carry their own terms of use. In particular, raw UTKFace images, raw AffectiveText headlines, and the original CIFAR-10 image archive are not redistributed here.

## Loading Example

```python
from pathlib import Path
import numpy as np

root = Path("{PACKAGE_SLUG}")
task = np.load(root / "real/cifar10_softmax/task.npz")
Y = task["Y"]
U = task["U"]
```
"""


def build_license_notes() -> str:
    return """
# License and Usage Notes

SimplexTasks-12 is a processed task collection. It should not be interpreted as a license override for the underlying source assets.

- CIFAR-10: this release exposes derived simplex arrays only.
- 20 Newsgroups: this release exposes derived topic-mixture arrays only.
- AffectiveText: this release omits raw headlines and raw API responses.
- Samson: keep attribution with the source benchmark bundle.
- PBMC3K: this release exports derived pseudobulk and deconvolution artifacts.
- UTKFace: this release omits raw face images and keeps only derived features and simplex arrays.
"""


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--output-dir", default=f"release/{PACKAGE_SLUG}")
    parser.add_argument("--force", action="store_true")
    args = parser.parse_args()

    root = Path(__file__).resolve().parent.parent
    out_dir = root / args.output_dir
    if out_dir.exists():
        if not args.force:
            raise FileExistsError(f"{out_dir} already exists. Use --force to overwrite.")
        shutil.rmtree(out_dir)
    (out_dir / "real").mkdir(parents=True, exist_ok=True)
    (out_dir / "synthetic").mkdir(parents=True, exist_ok=True)

    real_builders = [
        ("cifar10_softmax", lambda: build_cifar(root)),
        ("topics_20ng", build_topics),
        ("samson_unmixing", lambda: build_samson(root)),
        ("pbmc3k_pseudobulk", lambda: build_pbmc(root)),
        ("utkface_age_ldl", lambda: build_utkface(root)),
        ("affectivetext_emotions", lambda: build_affective(root)),
    ]

    real_manifest = []
    for task_id, builder in real_builders:
        arrays, metadata = builder()
        task_dir = out_dir / "real" / task_id
        write_task(task_dir, arrays, metadata)
        real_manifest.append(metadata)

    synthetic_manifest = []
    for task_id, cfg_rel, regime_label in SYNTHETIC_SPECS:
        arrays, metadata, cfg_path = build_synthetic_task(root, task_id, cfg_rel, regime_label)
        task_dir = out_dir / "synthetic" / task_id
        write_task(task_dir, arrays, metadata)
        shutil.copy2(cfg_path, task_dir / "config.yaml")
        synthetic_manifest.append(metadata)

    manifest = {
        "name": PACKAGE_NAME,
        "slug": PACKAGE_SLUG,
        "version": VERSION,
        "seed": SEED,
        "task_count": len(real_manifest) + len(synthetic_manifest),
        "real_tasks": real_manifest,
        "synthetic_tasks": synthetic_manifest,
    }
    write_json(out_dir / "manifest.json", manifest)
    write_text(out_dir / "README.md", build_package_readme(manifest))
    write_text(out_dir / "LICENSE_NOTES.md", build_license_notes())


if __name__ == "__main__":
    main()