icoa-cli 2.19.714 → 2.19.716

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{sprintTaskForComp as e}from"./sprint-meta.js";export function parseKaggleTaskList(e){const t=e??{},s=(t.data??t).tasks;if(!Array.isArray(s))return[];const i=[];for(const e of s){if(!e||"object"!=typeof e)continue;const t=e;"string"==typeof t.comp_id&&""!==t.comp_id&&i.push({comp_id:t.comp_id,title:"string"==typeof t.title?t.title:t.comp_id,metric:"string"==typeof t.metric?t.metric:"accuracy",description:"string"==typeof t.description&&""!==t.description?t.description:void 0,kaggle_url:"string"==typeof t.kaggle_url?t.kaggle_url:null,licence:"string"==typeof t.licence?t.licence:void 0,attribution:"string"==typeof t.attribution?t.attribution:void 0,data_base:"string"==typeof t.data_base&&""!==t.data_base?t.data_base:null,id_col:"string"==typeof t.id_col?t.id_col:void 0,target_col:"string"==typeof t.target_col?t.target_col:void 0,n_train:"number"==typeof t.n_train?t.n_train:void 0,n_test:"number"==typeof t.n_test?t.n_test:void 0,judge:"string"==typeof t.judge&&""!==t.judge?t.judge:void 0,board_event:"string"==typeof t.board_event&&""!==t.board_event?t.board_event:null,needs_gpu:!0===t.needs_gpu})}return i}export function dataFileUrls(e){const t=e.data_base;if("string"!=typeof t||""===t)return null;const s=t.replace(/\/$/,"");return{train:`${s}/train.csv`,test:`${s}/test.csv`,sample_submission:`${s}/sample_submission.csv`}}export function buildStarterCells(t,s){const i=s.id_col??"id",a=s.target_col??"target";var n;n=s.metric,/rmse|mse|mae|error/i.test(n);const o="icoa-holdout"===s.judge,r=e=>({kind:"markdown",source:e,result:null}),l=e=>({kind:"code",source:e.join("\n"),result:null}),c=[`# ${s.title}`,"",...s.description?[s.description,""]:[],`Kaggle-style practice — metric: **${s.metric}**.`,"","- `train.csv` — labelled training data",`- \`test.csv\` — rows to predict (no \`${a}\` column)`,"- `sample_submission.csv` — the exact format Kaggle expects","","Run all cells top to bottom: a baseline model trains on your CPU and","`submission.csv` appears next to the data. Then improve the model cell",...o?["and re-run — `icoa kaggle submit` scores it and ranks you on the board."]:["and re-run — your score is whatever Kaggle says it is."],"","**AI help:** split your terminal (`Ctrl-b %`), run `icoa` in the right pane","and type `ai4ioai` for a live Gemma 3n E4B. Ask it for feature ideas or bug fixes —","then verify every suggestion with the val score. A tip that lowers it, drop it.",...s.licence?["",`**Data licence:** ${s.licence}`]:[],...s.attribution?[`**Attribution:** ${s.attribution}`]:[]].join("\n"),p=l(["# setup — load the data (leave as is)","import os","import numpy as np","import pandas as pd","from pandas.api.types import is_numeric_dtype","from sklearn.feature_extraction.text import TfidfVectorizer","from sklearn.model_selection import train_test_split","from sklearn.metrics import accuracy_score, mean_squared_error","","# same code everywhere: in a GPU session these env vars point at the","# box's own dataset copy + workspace — locally they are unset.",`DIR = os.environ.get("ICOA_KAGGLE_DATA") or r"${t}"`,'OUT = os.environ.get("ICOA_KAGGLE_OUT") or DIR',...s.data_base?[`DATA_BASE = "${s.data_base.replace(/\/$/,"")}"`,'if not os.path.exists(DIR + "/train.csv"):'," import urllib.request"," os.makedirs(DIR, exist_ok=True)",' for f in ("train.csv", "test.csv", "sample_submission.csv"):',' urllib.request.urlretrieve(DATA_BASE + "/" + f, DIR + "/" + f)']:[],'train = pd.read_csv(DIR + "/train.csv")','test = pd.read_csv(DIR + "/test.csv")','sub_fmt = pd.read_csv(DIR + "/sample_submission.csv")',"","# 🔴 The submission column names are NOT always the training column names.","# On tasks imported from other platforms the two genuinely differ: train.csv",'# labels the answer "class" or "result" while sample_submission.csv calls it','# "answer". Hard-coding the submission name here used to raise KeyError on the',"# very first cell. So trust the DATA: whatever column train has and test lacks","# is the thing to predict.",`SUB_ID, SUB_TARGET = "${i}", "${a}" # names the submission file wants`,"only_train = [c for c in train.columns if c not in test.columns]","TARGET = only_train[-1] if only_train else SUB_TARGET","shared = [c for c in train.columns if c in test.columns]","# Only treat a column as the row id when the submission actually names it AND",'# both files carry it. Falling back to "first shared column" silently spent a',"# real feature as an id on tasks whose id lives only in test.csv.","ID = SUB_ID if SUB_ID in shared else None",'assert TARGET in train.columns, f"cannot find the label column in {list(train.columns)}"',"","# Regression or classification? Decided from the labels, not from metadata —","# several tasks ship continuous labels, and handing those to a classifier",'# raises "Unknown label type: continuous".',"REG = is_numeric_dtype(train[TARGET]) and train[TARGET].nunique() > 20",'print(f"train {train.shape} · test {test.shape} · predicting {TARGET!r} "'," f\"({'regression' if REG else 'classification'})\")","# 前几行 —— 看一眼真实数据长什么样,比看列名有用得多","print(train.head(3).to_string(max_colwidth=24)[:700])"]),u=e(s.comp_id),d=u?.media,m="audio"===d?.kind?"scipy.io.wavfile.read(path) -> numpy, then scipy.signal for a spectrogram":"image"===d?.kind?"PIL.Image.open(path) -> numpy":"weights"===d?.kind?"torch.load(path, weights_only=True) — the model itself is the input here":"unpack the archive inside the pack, then read what is in it",h=l(["# FEATURES — turn raw columns into numbers a model can use (improve THIS cell).","# Numbers: fill gaps with the median. Category (<= 20 distinct): one-hot.","# Free text: TF-IDF — on a text task the sentences ARE the signal, and dropping",'# them as "high-cardinality" leaves nothing to train on (that used to crash the','# whole notebook with "No objects to concatenate").',"# only columns present in BOTH files — a train-only column cannot be a feature","# at prediction time, and indexing test with it raises KeyError.","feat = [c for c in shared if c not in (ID, TARGET)]","Xtr_raw, Xte_raw = train[feat].copy(), test[feat].copy()","num = [c for c in feat if is_numeric_dtype(Xtr_raw[c])]","cat = [c for c in feat if c not in num and Xtr_raw[c].nunique() <= 20]","# Everything else is vectorised as text. Requiring a long average length here","# used to throw away high-cardinality SHORT strings — ingredient names, single","# words, category codes — which on several tasks are the entire signal, leaving","# the notebook with zero features.","txt = [c for c in feat if c not in num and c not in cat]","for c in num:"," med = Xtr_raw[c].median()"," Xtr_raw[c] = Xtr_raw[c].fillna(med)"," Xte_raw[c] = Xte_raw[c].fillna(med)","blocks_tr, blocks_te = [], []","if num or cat:"," d_tr = pd.get_dummies(Xtr_raw[num + cat], columns=cat, dummy_na=True)"," d_te = pd.get_dummies(Xte_raw[num + cat], columns=cat, dummy_na=True)",' d_tr, d_te = d_tr.align(d_te, join="left", axis=1, fill_value=0)'," blocks_tr.append(d_tr.to_numpy(dtype=float))"," blocks_te.append(d_te.to_numpy(dtype=float))","for c in txt:"," # fillna BEFORE astype(str): a real NaN reaching the vectoriser raises",' # "np.nan is an invalid document", and astype alone does not catch it.',' tr_txt = Xtr_raw[c].fillna("").astype(str)',' te_txt = Xte_raw[c].fillna("").astype(str)'," # min_df=1: on a short-token column (one ingredient per row) min_df=2 can"," # filter the vocabulary down to nothing."," long_col = tr_txt.str.len().mean() >= 12"," # Try the good settings, then a permissive fallback, then give up on THIS"," # column only. A column whose vocabulary prunes to nothing used to abort the",' # whole run with "After pruning, no terms remain".',' for kw in ({"min_df": 2 if long_col else 1,',' "analyzer": "word" if long_col else "char_wb"},',' {"min_df": 1, "analyzer": "char_wb"}):'," try:"," vec = TfidfVectorizer(max_features=3000, ngram_range=(1, 2), **kw)"," b_tr = vec.fit_transform(tr_txt).toarray()"," blocks_tr.append(b_tr)"," blocks_te.append(vec.transform(te_txt).toarray())"," break"," except ValueError as e:"," last = e"," else:",' print(f" skipped text column {c!r}: {last}")',"if not blocks_tr:"," raise SystemExit(",' f"No usable feature columns. The shared columns are {feat} — none of them "',' f"is numeric, categorical or text.\\n"',...d?[` "The signal for this task is in ${d.kind} files, not in the CSVs.\\n"`,` " 1. fetch them (${d.mb}MB): teleload ${d.pack}\\n"`,...d.glob?[` " files land at <pack dir>/${d.glob}\\n"`]:[],` " 2. replace this cell: ${m}\\n"`,' " build one feature row per file, then keep the rest of the notebook.\\n"']:[' f"Two kinds of task land here, and both are real work rather than a bug:\\n"',' f" 1. audio/image — train.csv only lists sample ids and labels and the "',' f"signal is in media files. Read them with scipy.io.wavfile (audio) or "',' f"PIL (images) and turn each file into a feature row.\\n"',' f" 2. train.csv and test.csv describe different things (a retrieval or "',' f"matching task). Read both files, work out what links a test row to a "',' f"train row, and build that mapping yourself.\\n"'],...d?[' f"columns — train={list(train.columns)} test={list(test.columns)}"']:[' f"Either way: replace this cell. train={list(train.columns)} "',' f"test={list(test.columns)}"']," )","X = np.hstack(blocks_tr)","X_test = np.hstack(blocks_te)","y = train[TARGET]","X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=0)",'print(f"{X.shape[1]} model-ready features "',' f"({len(num)} numeric, {len(cat)} one-hot, {len(txt)} TF-IDF text)")']),f=l(["# EDA — look at the labels before you model. Produces ONE figure:","# left = what you are predicting (class balance / value spread)","# right = what the FEATURES cell will have to work with","# icoa ipynb saves it as a .png and opens your image viewer (type `open` to reopen).","import matplotlib.pyplot as plt","try:"," fig, ax = plt.subplots(1, 2, figsize=(11, 4))"," if REG:",' vals = pd.to_numeric(train[TARGET], errors="coerce").dropna()'," ax[0].hist(vals, bins=30)",' ax[0].set_title(f"{TARGET} — {len(vals)} values, mean {vals.mean():.3g}")'," else:"," vc = train[TARGET].astype(str).value_counts().head(12)"," ax[0].bar(range(len(vc)), vc.values)"," ax[0].set_xticks(range(len(vc)))",' ax[0].set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' # An imbalanced target is the single most common reason a "good"'," # accuracy means nothing — show the majority share right in the title."," top = vc.iloc[0] / len(train)",' ax[0].set_title(f"{TARGET} — {train[TARGET].nunique()} classes · biggest {top:.0%}")',' kinds = {"numeric": 0, "category": 0, "text": 0}'," for c in [c for c in shared if c not in (ID, TARGET)]:"," if is_numeric_dtype(train[c]):",' kinds["numeric"] += 1'," elif train[c].nunique() <= 20:",' kinds["category"] += 1'," else:",' kinds["text"] += 1'," miss = train.isna().mean()"," miss = miss[miss > 0].sort_values(ascending=False).head(10)"," if len(miss) > 0:"," ax[1].barh(range(len(miss)), miss.values)"," ax[1].set_yticks(range(len(miss)))"," ax[1].set_yticklabels([s[:16] for s in miss.index])",' ax[1].set_title("missing rate (top columns)")'," else:"," ax[1].bar(list(kinds), list(kinds.values()))",' ax[1].set_title(f"{sum(kinds.values())} usable columns · no missing values")'," plt.tight_layout()"," plt.show()",' print("columns by kind:", kinds)',"except Exception as e:"," # EDA is never allowed to stop the run — `run all` halts on the first"," # error, and a plotting hiccup must not cost the student their baseline.",' print("EDA skipped:", type(e).__name__, e)']),g=l(["# MODEL — a working baseline. Improve THIS cell to climb the board.","from sklearn.linear_model import LogisticRegression, Ridge","# REG was decided from the labels back in setup.","model = Ridge() if REG else LogisticRegression(max_iter=2000)"]),_=l(["# validate — quick local score on a held-out fifth of train","model.fit(X_tr, y_tr)","p_val = model.predict(X_val)","if REG:",' print("val RMSE:", round(mean_squared_error(y_val, p_val) ** 0.5, 4))',"else:"," acc = accuracy_score(y_val, p_val)",' print("val accuracy:", round(acc, 4))'," if acc < 0.3:"," # A near-zero baseline is the expected result on several of these tasks,"," # not a broken notebook — say so, or the student reads it as a bug.",' print(" A score this low usually means the linear baseline cannot "',' "represent the pattern at all (combination rules, sequences, "',' "spelling). That IS the task: build features that expose it, "',' "or switch to a model that can. The chain itself works — "',' "submission.csv is written either way.")']),b=l(["# submission — retrain on ALL data, write submission.csv (Kaggle format)","model.fit(X, y)","pred = model.predict(X_test)",'out = OUT + "/submission.csv"',"# Copy the layout of sample_submission.csv and swap in the predictions, rather","# than rebuilding the file from column names. Some tasks want more than two","# columns (e.g. subtaskID,datapointID,answer) and a hand-built 2-column frame","# is rejected by the grader even when every prediction is right.","if len(sub_fmt) == len(pred):"," sub = sub_fmt.copy()"," sub[sub.columns[-1]] = pred","else:"," keys = {}"," if ID is not None and ID in test.columns:"," keys[SUB_ID] = test[ID]"," keys[SUB_TARGET] = pred"," sub = pd.DataFrame(keys)",' print(f"NOTE: sample_submission has {len(sub_fmt)} rows but test has "',' f"{len(pred)} — built the file from column names instead; "',' f"check it against the task page before submitting.")',"sub.to_csv(out, index=False)",'print("wrote", out, "·", list(sub.columns))',o?`print("score it: icoa kaggle submit ${s.comp_id}")`:'print("format check: icoa kaggle check")']),y=o?["## Submit to ICOA (scored on the server)","",`1. In the ICOA CLI run \`icoa kaggle submit ${s.comp_id}\``,"2. The server scores `submission.csv` against the secret test labels"," and your rank lands on the live board (`icoa board`).","3. Improve the MODEL cell, re-run, submit again — best score counts."]:"nitro"===s.judge?["## Where this one gets scored","","This task comes from **judge.nitro-ai.org** and the answer key stays on","their server, so ICOA cannot score it for you. That does not make it a","practice stub — it is a real task, just judged elsewhere.","",`1. \`icoa kaggle check ${s.comp_id}\` — checks the FORMAT of`," `submission.csv` (headers, row count, ids). Format errors are the most"," common way a correct answer still scores zero.","2. Watch the **val score** printed above — that is your real feedback loop"," during the sprint, and it needs no account anywhere.","3. Want the official number? Register at judge.nitro-ai.org and submit"," there. Otherwise compare against the official solution, or ask"," `ai4ioai` to review your approach."]:s.kaggle_url?["## Submit on Kaggle (manual, your own account)","",`1. Open ${s.kaggle_url}`,"2. **Submit Predictions** → upload `submission.csv`","3. Read your leaderboard score, come back, improve the MODEL cell, repeat."]:["## Submit on Kaggle (manual, your own account)","","_This dataset has no live Kaggle page linked yet._","The loop is identical: upload `submission.csv`, read the score, improve","the MODEL cell, repeat. Meanwhile the **val score** printed above is a","real signal you can work against with no account at all."];return[r(c),p,f,h,g,_,b,r(y.join("\n"))]}export function parseSubmitResponse(e){const t=e&&"object"==typeof e?e:{},s=t.data??t,i=s&&"object"==typeof s?s:{};return"number"==typeof i.score&&"string"==typeof i.metric?{ok:!0,score:i.score,metric:i.metric,rank:"number"==typeof i.rank_at_time?i.rank_at_time:null,fieldSize:"number"==typeof i.field_size?i.field_size:null,deduped:!0===i.deduped,sameCode:!0===i.same_code,submissionId:"string"==typeof i.submission_id?i.submission_id:null,leaderboard:"string"==typeof i.leaderboard?i.leaderboard:null,boardEvent:"string"==typeof i.board_event?i.board_event:null,used:"number"==typeof i.submissions_used?i.submissions_used:null,cap:"number"==typeof i.submissions_cap?i.submissions_cap:null,parts:i.parts&&"object"==typeof i.parts?Object.entries(i.parts).filter(e=>"number"==typeof e[1]).slice(0,24):void 0}:"string"==typeof i.reason&&""!==i.reason?{ok:!1,reason:i.reason,error:"string"==typeof i.error?i.error:void 0,problems:Array.isArray(i.problems)?i.problems.filter(e=>"string"==typeof e):void 0}:{ok:!1,reason:"network"}}export function validateSubmissionText(e,t){const s=[],i=e.trim().split(/\r?\n/),a=t.trim().split(/\r?\n/),n=i[0]??"",o=a[0]??"";n.trim()!==o.trim()&&s.push(`header mismatch: expected "${o}", got "${n}"`);const r=i.slice(1).filter(e=>""!==e.trim()),l=a.slice(1).filter(e=>""!==e.trim());r.length!==l.length&&s.push(`row count mismatch: expected ${l.length}, got ${r.length}`);const c=e=>new Set(e.map(e=>(e.split(",")[0]??"").trim())),p=c(r),u=c(l),d=[...u].filter(e=>!p.has(e)),m=[...p].filter(e=>!u.has(e));if(d.length>0||m.length>0){const e=[];d.length>0&&e.push(`${d.length} missing (e.g. ${d[0]})`),m.length>0&&e.push(`${m.length} unknown (e.g. ${m[0]})`),s.push(`id mismatch vs sample_submission: ${e.join(", ")}`)}for(let e=0;e<r.length;e++)if(r[e].split(",").some(e=>""===e.trim())){s.push(`empty cell in data row ${e+1}`);break}return{ok:0===s.length,problems:s}}export function buildMediaStarterCells(e,t){const s=e=>({kind:"markdown",source:e,result:null}),i=e=>({kind:"code",source:e.join("\n"),result:null}),a=t.media?.kind??"archive",n=t.title??"ICOA sprint task",o=[`# ${t.seq?`${t.seq} · `:""}${n}`,"",...t.topic_cn?[t.topic_cn,""]:[],"This task ships **data, not a baseline** — the CSV starter cannot help here",`because the signal lives in the ${a} files you just pulled.`,"","Run all cells: they inventory the pack and draw one picture of what is inside,","so you start from evidence instead of from the folder name. The last cell says","what you have to build — that part is the task.","","**Reading the files:** on your laptop the notebook env has numpy / scipy /","pandas / scikit-learn / torch / transformers / Pillow — but NOT librosa,","torchaudio or soundfile. The GPU contest box has librosa and torchaudio too."].join("\n"),r=i(["# inventory — what is actually in this pack (counts, sizes, a few real names)","import os","import numpy as np",`DIR = os.environ.get("ICOA_PACK_DIR") or r"${e}"`,"by_ext, total, sample = {}, 0, {}","for root, _dirs, files in os.walk(DIR):"," for f in files:",' ext = os.path.splitext(f)[1].lower() or "(no ext)"'," p = os.path.join(root, f)"," by_ext[ext] = by_ext.get(ext, 0) + 1"," total += os.path.getsize(p)"," sample.setdefault(ext, p)",'print(f"{DIR}\\n{sum(by_ext.values())} files · {total/1048576:.0f} MB")',"for ext, n in sorted(by_ext.items(), key=lambda kv: -kv[1])[:8]:",' print(f" {ext:>10} {n:>6} e.g. {os.path.relpath(sample[ext], DIR)}")',"# 🔴 a leftover .zip means the pack still needs unpacking before you can read it",'zips = [p for e, p in sample.items() if e == ".zip"]',"if zips:",' print("\\nNOTE: there is still a .zip in here — unpack it first:")',' print(f" import zipfile; zipfile.ZipFile(r\\"{zips[0]}\\").extractall(DIR)")']),l=["# EDA — one picture of what is inside. icoa ipynb saves it as a .png and","# opens your image viewer (`open` reopens it).","import matplotlib.pyplot as plt","try:"];"image"===a?l.push(" from PIL import Image"," imgs = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".jpg", ".jpeg", ".png"):'," imgs.append(os.path.join(root, f))"," if len(imgs) >= 9:"," break"," fig, axes = plt.subplots(3, 3, figsize=(7, 7))"," for ax, p in zip(axes.ravel(), imgs[:9]):"," ax.imshow(Image.open(p))"," ax.set_title(os.path.basename(p)[:18], fontsize=8)"," for ax in axes.ravel():",' ax.axis("off")',' plt.suptitle(f"{len(imgs)}+ images · first 9")'):"weights"===a?l.push(" import torch"," wfiles = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".pt", ".pth"):'," wfiles.append(os.path.join(root, f))"," fig, ax = plt.subplots(1, max(1, min(2, len(wfiles))), figsize=(10, 4), squeeze=False)"," for i, p in enumerate(wfiles[:2]):"," # weights_only=True: never unpickle arbitrary objects from a data file",' obj = torch.load(p, map_location="cpu", weights_only=True)',' t = obj if hasattr(obj, "flatten") else list(obj.values())[0]'," v = np.asarray(t, dtype=float).ravel()"," ax[0][i].hist(v, bins=60)",' ax[0][i].set_title(f"{os.path.basename(p)} · shape {tuple(np.shape(t))}", fontsize=9)',' print(os.path.basename(p), "shape", tuple(np.shape(t)))'):l.push(" import pandas as pd"," csvs = []"," for root, _d, files in os.walk(DIR):"," for f in files:",' if f.lower().endswith(".csv"):'," csvs.append(os.path.join(root, f))"," csvs.sort(key=os.path.getsize, reverse=True)"," df = pd.read_csv(csvs[0])"," print(os.path.basename(csvs[0]), df.shape)"," print(df.head(3).to_string(max_colwidth=24)[:600])"," # the last low-cardinality column is the usual label; if there is none,"," # plot the row-length spread so the picture still says something true"," cand = [c for c in df.columns if df[c].nunique() <= 30]"," fig, ax = plt.subplots(figsize=(8, 4))"," if cand:"," vc = df[cand[-1]].astype(str).value_counts().head(12)"," ax.bar(range(len(vc)), vc.values)"," ax.set_xticks(range(len(vc)))",' ax.set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' ax.set_title(f"{cand[-1]} — {df[cand[-1]].nunique()} values")'," else:"," ax.hist([len(str(x)) for x in df[df.columns[-1]]], bins=30)",' ax.set_title(f"length of {df.columns[-1]}")'),l.push(" plt.tight_layout()"," plt.show()","except Exception as e:"," # never stop the run on a plotting problem — `run all` halts on first error",' print("EDA skipped:", type(e).__name__, e)');const c=["## Your job","","There is no baseline to beat here — you build the first one. The shape of it:","","1. **turn each file into a row of numbers** — that is the whole trick","image"===a?" (`PIL.Image.open` → resize → `np.asarray` → flatten, or a pretrained encoder)":"weights"===a?" (`torch.load(..., weights_only=True)` → the tensors themselves ARE the features)":" (read the CSVs / unpack the archive, then vectorise the text columns)","2. keep a held-out fifth of the data and print a val score every time","3. write your predictions in exactly the format the task page asks for","","`add` makes a new code cell · `add md` a note cell · `run all` runs everything ·","a cell that raises offers `ai N`, which sends that cell and its traceback to the AI."].join("\n");return[s(o),r,i(l),s(c)]}
1
+ import{sprintTaskForComp as e}from"./sprint-meta.js";export function parseKaggleTaskList(e){const t=e??{},s=(t.data??t).tasks;if(!Array.isArray(s))return[];const i=[];for(const e of s){if(!e||"object"!=typeof e)continue;const t=e;"string"==typeof t.comp_id&&""!==t.comp_id&&i.push({comp_id:t.comp_id,title:"string"==typeof t.title?t.title:t.comp_id,metric:"string"==typeof t.metric?t.metric:"accuracy",description:"string"==typeof t.description&&""!==t.description?t.description:void 0,kaggle_url:"string"==typeof t.kaggle_url?t.kaggle_url:null,licence:"string"==typeof t.licence?t.licence:void 0,attribution:"string"==typeof t.attribution?t.attribution:void 0,data_base:"string"==typeof t.data_base&&""!==t.data_base?t.data_base:null,id_col:"string"==typeof t.id_col?t.id_col:void 0,target_col:"string"==typeof t.target_col?t.target_col:void 0,n_train:"number"==typeof t.n_train?t.n_train:void 0,n_test:"number"==typeof t.n_test?t.n_test:void 0,judge:"string"==typeof t.judge&&""!==t.judge?t.judge:void 0,board_event:"string"==typeof t.board_event&&""!==t.board_event?t.board_event:null,needs_gpu:!0===t.needs_gpu})}return i}export function dataFileUrls(e){const t=e.data_base;if("string"!=typeof t||""===t)return null;const s=t.replace(/\/$/,"");return{train:`${s}/train.csv`,test:`${s}/test.csv`,sample_submission:`${s}/sample_submission.csv`}}export function buildStarterCells(t,s){const i=s.id_col??"id",a=s.target_col??"target";var n;n=s.metric,/rmse|mse|mae|error/i.test(n);const o="icoa-holdout"===s.judge,r=e=>({kind:"markdown",source:e,result:null}),l=e=>({kind:"code",source:e.join("\n"),result:null}),c=[`# ${s.title}`,"",...s.description?[s.description,""]:[],`Kaggle-style practice — metric: **${s.metric}**.`,"","- `train.csv` — labelled training data",`- \`test.csv\` — rows to predict (no \`${a}\` column)`,"- `sample_submission.csv` — the exact format Kaggle expects","","Run all cells top to bottom: a baseline model trains on your CPU and","`submission.csv` appears next to the data. Then improve the model cell",...o?["and re-run — `icoa kaggle submit` scores it and ranks you on the board."]:["and re-run — your score is whatever Kaggle says it is."],"","**AI help:** split your terminal (`Ctrl-b %`), run `icoa` in the right pane","and type `ai4ioai` for a live Gemma 3n E4B. Ask it for feature ideas or bug fixes —","then verify every suggestion with the val score. A tip that lowers it, drop it.",...s.licence?["",`**Data licence:** ${s.licence}`]:[],...s.attribution?[`**Attribution:** ${s.attribution}`]:[]].join("\n"),p=l(["# setup — load the data (leave as is)","import os","import numpy as np","import pandas as pd","from pandas.api.types import is_numeric_dtype","from sklearn.feature_extraction.text import TfidfVectorizer","from sklearn.model_selection import train_test_split","from sklearn.metrics import accuracy_score, mean_squared_error","","# same code everywhere: in a GPU session these env vars point at the","# box's own dataset copy + workspace — locally they are unset.",`DIR = os.environ.get("ICOA_KAGGLE_DATA") or r"${t}"`,'OUT = os.environ.get("ICOA_KAGGLE_OUT") or DIR',...s.data_base?[`DATA_BASE = "${s.data_base.replace(/\/$/,"")}"`,'if not os.path.exists(DIR + "/train.csv"):'," import urllib.request"," os.makedirs(DIR, exist_ok=True)",' for f in ("train.csv", "test.csv", "sample_submission.csv"):',' urllib.request.urlretrieve(DATA_BASE + "/" + f, DIR + "/" + f)']:[],'train = pd.read_csv(DIR + "/train.csv")','test = pd.read_csv(DIR + "/test.csv")','sub_fmt = pd.read_csv(DIR + "/sample_submission.csv")',"","# 🔴 The submission column names are NOT always the training column names.","# On tasks imported from other platforms the two genuinely differ: train.csv",'# labels the answer "class" or "result" while sample_submission.csv calls it','# "answer". Hard-coding the submission name here used to raise KeyError on the',"# very first cell. So trust the DATA: whatever column train has and test lacks","# is the thing to predict.",`SUB_ID, SUB_TARGET = "${i}", "${a}" # names the submission file wants`,"only_train = [c for c in train.columns if c not in test.columns]","TARGET = only_train[-1] if only_train else SUB_TARGET","shared = [c for c in train.columns if c in test.columns]","# Only treat a column as the row id when the submission actually names it AND",'# both files carry it. Falling back to "first shared column" silently spent a',"# real feature as an id on tasks whose id lives only in test.csv.","ID = SUB_ID if SUB_ID in shared else None",'assert TARGET in train.columns, f"cannot find the label column in {list(train.columns)}"',"","# Regression or classification? Decided from the labels, not from metadata —","# several tasks ship continuous labels, and handing those to a classifier",'# raises "Unknown label type: continuous".',"REG = is_numeric_dtype(train[TARGET]) and train[TARGET].nunique() > 20",'print(f"train {train.shape} · test {test.shape} · predicting {TARGET!r} "'," f\"({'regression' if REG else 'classification'})\")","# 前几行 —— 看一眼真实数据长什么样,比看列名有用得多","print(train.head(3).to_string(max_colwidth=24)[:700])"]),u=e(s.comp_id),d=u?.media,m="audio"===d?.kind?"scipy.io.wavfile.read(path) -> numpy, then scipy.signal for a spectrogram":"image"===d?.kind?"PIL.Image.open(path) -> numpy":"weights"===d?.kind?"torch.load(path, weights_only=True) — the model itself is the input here":"unpack the archive inside the pack, then read what is in it",h=l(["# FEATURES — turn raw columns into numbers a model can use (improve THIS cell).","# Numbers: fill gaps with the median. Category (<= 20 distinct): one-hot.","# Free text: TF-IDF — on a text task the sentences ARE the signal, and dropping",'# them as "high-cardinality" leaves nothing to train on (that used to crash the','# whole notebook with "No objects to concatenate").',"# only columns present in BOTH files — a train-only column cannot be a feature","# at prediction time, and indexing test with it raises KeyError.","feat = [c for c in shared if c not in (ID, TARGET)]","Xtr_raw, Xte_raw = train[feat].copy(), test[feat].copy()","num = [c for c in feat if is_numeric_dtype(Xtr_raw[c])]","cat = [c for c in feat if c not in num and Xtr_raw[c].nunique() <= 20]","# Everything else is vectorised as text. Requiring a long average length here","# used to throw away high-cardinality SHORT strings — ingredient names, single","# words, category codes — which on several tasks are the entire signal, leaving","# the notebook with zero features.","txt = [c for c in feat if c not in num and c not in cat]","for c in num:"," med = Xtr_raw[c].median()"," Xtr_raw[c] = Xtr_raw[c].fillna(med)"," Xte_raw[c] = Xte_raw[c].fillna(med)","blocks_tr, blocks_te = [], []","if num or cat:"," d_tr = pd.get_dummies(Xtr_raw[num + cat], columns=cat, dummy_na=True)"," d_te = pd.get_dummies(Xte_raw[num + cat], columns=cat, dummy_na=True)",' d_tr, d_te = d_tr.align(d_te, join="left", axis=1, fill_value=0)'," blocks_tr.append(d_tr.to_numpy(dtype=float))"," blocks_te.append(d_te.to_numpy(dtype=float))","for c in txt:"," # fillna BEFORE astype(str): a real NaN reaching the vectoriser raises",' # "np.nan is an invalid document", and astype alone does not catch it.',' tr_txt = Xtr_raw[c].fillna("").astype(str)',' te_txt = Xte_raw[c].fillna("").astype(str)'," # min_df=1: on a short-token column (one ingredient per row) min_df=2 can"," # filter the vocabulary down to nothing."," long_col = tr_txt.str.len().mean() >= 12"," # Try the good settings, then a permissive fallback, then give up on THIS"," # column only. A column whose vocabulary prunes to nothing used to abort the",' # whole run with "After pruning, no terms remain".',' for kw in ({"min_df": 2 if long_col else 1,',' "analyzer": "word" if long_col else "char_wb"},',' {"min_df": 1, "analyzer": "char_wb"}):'," try:"," vec = TfidfVectorizer(max_features=3000, ngram_range=(1, 2), **kw)"," b_tr = vec.fit_transform(tr_txt).toarray()"," blocks_tr.append(b_tr)"," blocks_te.append(vec.transform(te_txt).toarray())"," break"," except ValueError as e:"," last = e"," else:",' print(f" skipped text column {c!r}: {last}")',"if not blocks_tr:"," raise SystemExit(",' f"No usable feature columns. The shared columns are {feat} — none of them "',' f"is numeric, categorical or text.\\n"',...d?[` "The signal for this task is in ${d.kind} files, not in the CSVs.\\n"`,` " 1. fetch them (${d.mb}MB): teleload ${d.pack}\\n"`,...d.glob?[` " files land at <pack dir>/${d.glob}\\n"`]:[],` " 2. replace this cell: ${m}\\n"`,' " build one feature row per file, then keep the rest of the notebook.\\n"']:[' f"Two kinds of task land here, and both are real work rather than a bug:\\n"',' f" 1. audio/image — train.csv only lists sample ids and labels and the "',' f"signal is in media files. Read them with scipy.io.wavfile (audio) or "',' f"PIL (images) and turn each file into a feature row.\\n"',' f" 2. train.csv and test.csv describe different things (a retrieval or "',' f"matching task). Read both files, work out what links a test row to a "',' f"train row, and build that mapping yourself.\\n"'],...d?[' f"columns — train={list(train.columns)} test={list(test.columns)}"']:[' f"Either way: replace this cell. train={list(train.columns)} "',' f"test={list(test.columns)}"']," )","X = np.hstack(blocks_tr)","X_test = np.hstack(blocks_te)","y = train[TARGET]","X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=0)",'print(f"{X.shape[1]} model-ready features "',' f"({len(num)} numeric, {len(cat)} one-hot, {len(txt)} TF-IDF text)")']),f=l(["# EDA — look at the labels before you model. Produces ONE figure:","# left = what you are predicting (class balance / value spread)","# right = what the FEATURES cell will have to work with","# icoa ipynb shows it in the terminal and saves a .png (type `open` for the full-size picture).","import matplotlib.pyplot as plt","try:"," fig, ax = plt.subplots(1, 2, figsize=(11, 4))"," if REG:",' vals = pd.to_numeric(train[TARGET], errors="coerce").dropna()'," ax[0].hist(vals, bins=30)",' ax[0].set_title(f"{TARGET} — {len(vals)} values, mean {vals.mean():.3g}")'," else:"," vc = train[TARGET].astype(str).value_counts().head(12)"," ax[0].bar(range(len(vc)), vc.values)"," ax[0].set_xticks(range(len(vc)))",' ax[0].set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' # An imbalanced target is the single most common reason a "good"'," # accuracy means nothing — show the majority share right in the title."," top = vc.iloc[0] / len(train)",' ax[0].set_title(f"{TARGET} — {train[TARGET].nunique()} classes · biggest {top:.0%}")',' kinds = {"numeric": 0, "category": 0, "text": 0}'," for c in [c for c in shared if c not in (ID, TARGET)]:"," if is_numeric_dtype(train[c]):",' kinds["numeric"] += 1'," elif train[c].nunique() <= 20:",' kinds["category"] += 1'," else:",' kinds["text"] += 1'," miss = train.isna().mean()"," miss = miss[miss > 0].sort_values(ascending=False).head(10)"," if len(miss) > 0:"," ax[1].barh(range(len(miss)), miss.values)"," ax[1].set_yticks(range(len(miss)))"," ax[1].set_yticklabels([s[:16] for s in miss.index])",' ax[1].set_title("missing rate (top columns)")'," else:"," ax[1].bar(list(kinds), list(kinds.values()))",' ax[1].set_title(f"{sum(kinds.values())} usable columns · no missing values")'," plt.tight_layout()"," plt.show()",' print("columns by kind:", kinds)',"except Exception as e:"," # EDA is never allowed to stop the run — `run all` halts on the first"," # error, and a plotting hiccup must not cost the student their baseline.",' print("EDA skipped:", type(e).__name__, e)']),g=l(["# MODEL — a working baseline. Improve THIS cell to climb the board.","from sklearn.linear_model import LogisticRegression, Ridge","# REG was decided from the labels back in setup.","model = Ridge() if REG else LogisticRegression(max_iter=2000)"]),b=l(["# validate — quick local score on a held-out fifth of train","model.fit(X_tr, y_tr)","p_val = model.predict(X_val)","if REG:",' print("val RMSE:", round(mean_squared_error(y_val, p_val) ** 0.5, 4))',"else:"," acc = accuracy_score(y_val, p_val)",' print("val accuracy:", round(acc, 4))'," if acc < 0.3:"," # A near-zero baseline is the expected result on several of these tasks,"," # not a broken notebook — say so, or the student reads it as a bug.",' print(" A score this low usually means the linear baseline cannot "',' "represent the pattern at all (combination rules, sequences, "',' "spelling). That IS the task: build features that expose it, "',' "or switch to a model that can. The chain itself works — "',' "submission.csv is written either way.")']),_=l(["# submission — retrain on ALL data, write submission.csv (Kaggle format)","model.fit(X, y)","pred = model.predict(X_test)",'out = OUT + "/submission.csv"',"# Copy the layout of sample_submission.csv and swap in the predictions, rather","# than rebuilding the file from column names. Some tasks want more than two","# columns (e.g. subtaskID,datapointID,answer) and a hand-built 2-column frame","# is rejected by the grader even when every prediction is right.","if len(sub_fmt) == len(pred):"," sub = sub_fmt.copy()"," sub[sub.columns[-1]] = pred","else:"," keys = {}"," if ID is not None and ID in test.columns:"," keys[SUB_ID] = test[ID]"," keys[SUB_TARGET] = pred"," sub = pd.DataFrame(keys)",' print(f"NOTE: sample_submission has {len(sub_fmt)} rows but test has "',' f"{len(pred)} — built the file from column names instead; "',' f"check it against the task page before submitting.")',"sub.to_csv(out, index=False)",'print("wrote", out, "·", list(sub.columns))',o?`print("score it: icoa kaggle submit ${s.comp_id}")`:'print("format check: icoa kaggle check")']),y=o?["## Submit to ICOA (scored on the server)","",`1. In the ICOA CLI run \`icoa kaggle submit ${s.comp_id}\``,"2. The server scores `submission.csv` against the secret test labels"," and your rank lands on the live board (`icoa board`).","3. Improve the MODEL cell, re-run, submit again — best score counts."]:"nitro"===s.judge?["## Where this one gets scored","","This task comes from **judge.nitro-ai.org** and the answer key stays on","their server, so ICOA cannot score it for you. That does not make it a","practice stub — it is a real task, just judged elsewhere.","",`1. \`icoa kaggle check ${s.comp_id}\` — checks the FORMAT of`," `submission.csv` (headers, row count, ids). Format errors are the most"," common way a correct answer still scores zero.","2. Watch the **val score** printed above — that is your real feedback loop"," during the sprint, and it needs no account anywhere.","3. Want the official number? Register at judge.nitro-ai.org and submit"," there. Otherwise compare against the official solution, or ask"," `ai4ioai` to review your approach."]:s.kaggle_url?["## Submit on Kaggle (manual, your own account)","",`1. Open ${s.kaggle_url}`,"2. **Submit Predictions** → upload `submission.csv`","3. Read your leaderboard score, come back, improve the MODEL cell, repeat."]:["## Submit on Kaggle (manual, your own account)","","_This dataset has no live Kaggle page linked yet._","The loop is identical: upload `submission.csv`, read the score, improve","the MODEL cell, repeat. Meanwhile the **val score** printed above is a","real signal you can work against with no account at all."];return[r(c),p,f,h,g,b,_,r(y.join("\n"))]}export function parseSubmitResponse(e){const t=e&&"object"==typeof e?e:{},s=t.data??t,i=s&&"object"==typeof s?s:{};return"number"==typeof i.score&&"string"==typeof i.metric?{ok:!0,score:i.score,metric:i.metric,rank:"number"==typeof i.rank_at_time?i.rank_at_time:null,fieldSize:"number"==typeof i.field_size?i.field_size:null,deduped:!0===i.deduped,sameCode:!0===i.same_code,submissionId:"string"==typeof i.submission_id?i.submission_id:null,leaderboard:"string"==typeof i.leaderboard?i.leaderboard:null,boardEvent:"string"==typeof i.board_event?i.board_event:null,used:"number"==typeof i.submissions_used?i.submissions_used:null,cap:"number"==typeof i.submissions_cap?i.submissions_cap:null,parts:i.parts&&"object"==typeof i.parts?Object.entries(i.parts).filter(e=>"number"==typeof e[1]).slice(0,24):void 0}:"string"==typeof i.reason&&""!==i.reason?{ok:!1,reason:i.reason,error:"string"==typeof i.error?i.error:void 0,problems:Array.isArray(i.problems)?i.problems.filter(e=>"string"==typeof e):void 0,maxBytes:"number"==typeof i.max_bytes?i.max_bytes:void 0}:{ok:!1,reason:"network"}}export function validateSubmissionText(e,t){const s=[],i=e.trim().split(/\r?\n/),a=t.trim().split(/\r?\n/),n=i[0]??"",o=a[0]??"";n.trim()!==o.trim()&&s.push(`header mismatch: expected "${o}", got "${n}"`);const r=i.slice(1).filter(e=>""!==e.trim()),l=a.slice(1).filter(e=>""!==e.trim());r.length!==l.length&&s.push(`row count mismatch: expected ${l.length}, got ${r.length}`);const c=e=>new Set(e.map(e=>(e.split(",")[0]??"").trim())),p=c(r),u=c(l),d=[...u].filter(e=>!p.has(e)),m=[...p].filter(e=>!u.has(e));if(d.length>0||m.length>0){const e=[];d.length>0&&e.push(`${d.length} missing (e.g. ${d[0]})`),m.length>0&&e.push(`${m.length} unknown (e.g. ${m[0]})`),s.push(`id mismatch vs sample_submission: ${e.join(", ")}`)}for(let e=0;e<r.length;e++)if(r[e].split(",").some(e=>""===e.trim())){s.push(`empty cell in data row ${e+1}`);break}return{ok:0===s.length,problems:s}}export function buildMediaStarterCells(e,t){const s=e=>({kind:"markdown",source:e,result:null}),i=e=>({kind:"code",source:e.join("\n"),result:null}),a=t.media?.kind??"archive",n=t.title??"ICOA sprint task",o=[`# ${t.seq?`${t.seq} · `:""}${n}`,"",...t.topic_cn?[t.topic_cn,""]:[],"This task ships **data, not a baseline** — the CSV starter cannot help here",`because the signal lives in the ${a} files you just pulled.`,"","Run all cells: they inventory the pack and draw one picture of what is inside,","so you start from evidence instead of from the folder name. The last cell says","what you have to build — that part is the task.","","**Reading the files:** on your laptop the notebook env has numpy / scipy /","pandas / scikit-learn / torch / transformers / Pillow — but NOT librosa,","torchaudio or soundfile. The GPU contest box has librosa and torchaudio too."].join("\n"),r=i(["# inventory — what is actually in this pack (counts, sizes, a few real names)","import os","import numpy as np",`DIR = os.environ.get("ICOA_PACK_DIR") or r"${e}"`,"by_ext, total, sample = {}, 0, {}","for root, _dirs, files in os.walk(DIR):"," for f in files:",' ext = os.path.splitext(f)[1].lower() or "(no ext)"'," p = os.path.join(root, f)"," by_ext[ext] = by_ext.get(ext, 0) + 1"," total += os.path.getsize(p)"," sample.setdefault(ext, p)",'print(f"{DIR}\\n{sum(by_ext.values())} files · {total/1048576:.0f} MB")',"for ext, n in sorted(by_ext.items(), key=lambda kv: -kv[1])[:8]:",' print(f" {ext:>10} {n:>6} e.g. {os.path.relpath(sample[ext], DIR)}")',"# 🔴 a leftover .zip means the pack still needs unpacking before you can read it",'zips = [p for e, p in sample.items() if e == ".zip"]',"if zips:",' print("\\nNOTE: there is still a .zip in here — unpack it first:")',' print(f" import zipfile; zipfile.ZipFile(r\\"{zips[0]}\\").extractall(DIR)")']),l=["# EDA — one picture of what is inside. icoa ipynb shows it in the terminal and","# saves a .png (`open` shows it full size).","import matplotlib.pyplot as plt","try:"];"image"===a?l.push(" from PIL import Image"," imgs = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".jpg", ".jpeg", ".png"):'," imgs.append(os.path.join(root, f))"," if len(imgs) >= 9:"," break"," fig, axes = plt.subplots(3, 3, figsize=(7, 7))"," for ax, p in zip(axes.ravel(), imgs[:9]):"," ax.imshow(Image.open(p))"," ax.set_title(os.path.basename(p)[:18], fontsize=8)"," for ax in axes.ravel():",' ax.axis("off")',' plt.suptitle(f"{len(imgs)}+ images · first 9")'):"weights"===a?l.push(" import torch"," wfiles = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".pt", ".pth"):'," wfiles.append(os.path.join(root, f))"," fig, ax = plt.subplots(1, max(1, min(2, len(wfiles))), figsize=(10, 4), squeeze=False)"," for i, p in enumerate(wfiles[:2]):"," # weights_only=True: never unpickle arbitrary objects from a data file",' obj = torch.load(p, map_location="cpu", weights_only=True)',' t = obj if hasattr(obj, "flatten") else list(obj.values())[0]'," v = np.asarray(t, dtype=float).ravel()"," ax[0][i].hist(v, bins=60)",' ax[0][i].set_title(f"{os.path.basename(p)} · shape {tuple(np.shape(t))}", fontsize=9)',' print(os.path.basename(p), "shape", tuple(np.shape(t)))'):l.push(" import pandas as pd"," csvs = []"," for root, _d, files in os.walk(DIR):"," for f in files:",' if f.lower().endswith(".csv"):'," csvs.append(os.path.join(root, f))"," csvs.sort(key=os.path.getsize, reverse=True)"," df = pd.read_csv(csvs[0])"," print(os.path.basename(csvs[0]), df.shape)"," print(df.head(3).to_string(max_colwidth=24)[:600])"," # the last low-cardinality column is the usual label; if there is none,"," # plot the row-length spread so the picture still says something true"," cand = [c for c in df.columns if df[c].nunique() <= 30]"," fig, ax = plt.subplots(figsize=(8, 4))"," if cand:"," vc = df[cand[-1]].astype(str).value_counts().head(12)"," ax.bar(range(len(vc)), vc.values)"," ax.set_xticks(range(len(vc)))",' ax.set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' ax.set_title(f"{cand[-1]} — {df[cand[-1]].nunique()} values")'," else:"," ax.hist([len(str(x)) for x in df[df.columns[-1]]], bins=30)",' ax.set_title(f"length of {df.columns[-1]}")'),l.push(" plt.tight_layout()"," plt.show()","except Exception as e:"," # never stop the run on a plotting problem — `run all` halts on first error",' print("EDA skipped:", type(e).__name__, e)');const c=["## Your job","","There is no baseline to beat here — you build the first one. The shape of it:","","1. **turn each file into a row of numbers** — that is the whole trick","image"===a?" (`PIL.Image.open` → resize → `np.asarray` → flatten, or a pretrained encoder)":"weights"===a?" (`torch.load(..., weights_only=True)` → the tensors themselves ARE the features)":" (read the CSVs / unpack the archive, then vectorise the text columns)","2. keep a held-out fifth of the data and print a val score every time","3. write your predictions in exactly the format the task page asks for","","`add` makes a new code cell · `add md` a note cell · `run all` runs everything ·","a cell that raises offers `ai N`, which sends that cell and its traceback to the AI."].join("\n");return[s(o),r,i(l),s(c)]}
@@ -1 +1 @@
1
- import{createHash as e}from"node:crypto";export const METRIC_KIND={accuracy:"class",balanced_accuracy:"class",macro_f1:"class",weighted_f1:"class",mcc:"class",f1:"binary",qwk:"grade",auc:"prob",ap:"prob",logloss:"prob",brier:"prob",mse:"reg",rmse:"reg",mae:"reg",rmsle:"reg",mape:"reg",smape:"reg",r2:"reg",pearson:"reg",spearman:"reg"};export const METRICS=Object.keys(METRIC_KIND);export const LOWER_IS_BETTER=new Set(["mse","rmse","mae","rmsle","mape","smape","logloss","brier"]);export function metricGroups(){const e=(...e)=>METRICS.filter(t=>e.includes(METRIC_KIND[t]));return[{kind:"classes",names:e("class","binary","grade")},{kind:"probability of 1",names:e("prob")},{kind:"numbers",names:e("reg")}]}export const CUSTOM="custom";export const CUSTOM_WORDS=["weighted","custom","multi"];export const SCORER_FILE="scorer.py";export const HELPER_FILE="icoa_metrics.py";export function predColumns(e){return e.columns?.length?e.columns:[e.label]}function t(e,t,s){if(0===e.length)return`${s}: list at least one column`;const n=e.find(e=>""===e||/[,"\s]/.test(e));return void 0!==n?`${s}: "${n}" is not a usable column name (no spaces, commas or quotes)`:e.includes(t)?`${s}: "${t}" is the id column — it cannot also be predicted`:new Set(e).size!==e.length?`${s}: a column is listed twice`:null}export function scorerColumns(e){const t=/^COLUMNS[ \t]*=[ \t]*\[([^\]\n]*)\]/m.exec(e);return t?[...t[1].matchAll(/"([^"]*)"|'([^']*)'/g)].map(e=>(e[1]??e[2]).trim()):null}export function withScorerColumns(e,s){if(e.metric!==CUSTOM||void 0===s)return{meta:e,problems:[]};const n=scorerColumns(s);if(!n)return{meta:e,problems:[]};const r=t(n,e.id,"scorer.py COLUMNS");return r?{meta:e,problems:[r]}:e.columns&&e.columns.join(",")!==n.join(",")?{meta:e,problems:[`scorer.py says COLUMNS = ${n.join(", ")} but task.json says columns = ${e.columns.join(", ")} — keep one (remove "columns" from task.json)`]}:{meta:{...e,columns:n,label:n[0]},problems:[]}}export function parseCsv(e){const t=[],s=e.replace(/^/,"").split(/\r?\n/).filter(e=>""!==e.trim());if(0===s.length)return{table:{header:[],rows:[]},problems:["file is empty"]};e.includes('"')&&t.push('quoted fields (") are not supported — write plain comma-separated values');const n=s[0].split(",").map(e=>e.trim()),r=[];for(let e=1;e<s.length;e++){const o=s[e].split(",").map(e=>e.trim());if(o.length!==n.length){t.push(`row ${e+1} has ${o.length} fields, the header has ${n.length}`);break}r.push(o)}return{table:{header:n,rows:r},problems:t}}export function validName(e){return/^[a-z0-9][a-z0-9-]{1,38}[a-z0-9]$/.test(e)}export function parseTaskMeta(e){let s;try{s=JSON.parse(e)}catch{return{meta:null,problems:["task.json is not valid JSON"]}}const n=[],r=String(s.name??""),o=String(s.metric??""),a=o===CUSTOM,i=String(s.id||"id");let c;if(void 0!==s.columns)if(!Array.isArray(s.columns)||s.columns.some(e=>"string"!=typeof e))n.push('task.json columns: a list of column names, e.g. ["rain", "mm"]');else{c=s.columns.map(e=>e.trim());const e=t(c,i,"task.json columns");e?n.push(e):a||1===c.length||n.push(`task.json columns: metric ${o} scores one column — several columns need "metric": "${CUSTOM}" (setter new <name> weighted)`)}const l=String(s.label??"")||c?.[0]||(a?"label":"");if(validName(r)||n.push("task.json name: 3–40 characters of a–z, 0–9 and -"),a||METRICS.includes(o)||n.push(`task.json metric: one of ${METRICS.join(" / ")} / ${CUSTOM}`),l||n.push("task.json label: the name of the column to predict"),n.length)return{meta:null,problems:n};const u=Number(s.per_day),m={name:r,title:String(s.title||r),metric:o,label:l,id:i,per_day:Number.isFinite(u)&&u>=1?Math.floor(u):5,description:"string"==typeof s.description?s.description:void 0};return c&&(m.columns=c,a||(m.label=c[0])),{meta:m,problems:[]}}const s={train:"train.csv",testA:"test_a.csv",labelsA:"labels_a.csv",testB:"test_b.csv",labelsB:"labels_b.csv"};export function checkTask(e,t){const n={ok:!1,problems:[],warnings:[],nTrain:0,nA:0,nB:0,features:[],classes:[]},r={};for(const e of Object.keys(s)){const o=t[e];if(void 0===o){n.problems.push(`${s[e]} is missing`);continue}const{table:a,problems:i}=parseCsv(o);for(const t of i)n.problems.push(`${s[e]}: ${t}`);r[e]=a}if(n.problems.length)return n;const o=r.train,a=e.id,i=predColumns(e),c=e.metric===CUSTOM;if(!o.header.includes(a)||i.some(e=>!o.header.includes(e)))return n.problems.push(1===i.length?`train.csv needs both "${a}" and "${i[0]}" columns — its header is ${o.header.join(", ")}`:`train.csv needs "${a}" and every prediction column (${i.join(", ")}) — its header is ${o.header.join(", ")}`),n;const l=o.header.filter(e=>!i.includes(e));n.features=l.filter(e=>e!==a);const u=o.header.indexOf(a),m=o.header.indexOf(e.label),p=new Set;for(const e of o.rows){if(p.has(e[u]))return n.problems.push(`train.csv: duplicate id ${e[u]}`),n;p.add(e[u])}const h=new Map,d=new Map;let f=null;const b=new Map,g={A:[],B:[]};for(const[e,t,o]of[["A","testA","labelsA"],["B","testB","labelsB"]]){const u=r[t],m=r[o],p=s[t],$=s[o],w=i.find(e=>u.header.includes(e));if(void 0!==w)return n.problems.push(`${p} carries the label column "${w}" — remove it, labels go in ${$}`),n;if(u.header.join(",")!==l.join(","))return n.problems.push(`${p} columns differ from train.csv minus the label — test: ${u.header.join(", ")} · train: ${l.join(", ")}`),n;if(c){const e=[a,...i].find(e=>!m.header.includes(e));if(void 0!==e)return n.problems.push(`${$} has no "${e}" column — it needs ${[a,...i].join(", ")} (extra columns for the scorer are fine); got ${m.header.join(", ")}`),n;if(new Set(m.header).size!==m.header.length)return n.problems.push(`${$} has a column name twice — got ${m.header.join(", ")}`),n;if(null!==f&&f!==m.header.join(","))return n.problems.push(`labels_a.csv and labels_b.csv must have the same columns — A: ${f.split(",").join(", ")} · B: ${m.header.join(", ")}`),n;f=m.header.join(",")}else if(2!==m.header.length||m.header[0]!==a||m.header[1]!==i[0])return n.problems.push(`${$} must have exactly two columns (${a}, ${i[0]}) — got ${m.header.join(", ")}`),n;const v=m.header.indexOf(a),M=i.map(e=>m.header.indexOf(e)),y=new Set,k=u.header.indexOf(a);for(const e of u.rows){if(y.has(e[k]))return n.problems.push(`${p} has duplicate ids (${e[k]})`),n;y.add(e[k])}const S=new Map;for(const e of m.rows){const t=e[v],s=M.map(t=>e[t]);if(""===t||s.includes(""))return n.problems.push(`${$}: empty id or label near id ${t||"(blank)"}`),n;if(S.has(t))return n.problems.push(`${$}: duplicate id ${t}`),n;S.set(t,s)}const x=[...y].filter(e=>S.has(e)).length;if(x!==y.size||x!==S.size)return n.problems.push(`${p}: ${y.size} ids in the test file, ${S.size} in ${$}, ${x} in both — they must be the same set`),n;for(const t of y){const s=b.get(t);if(s)return n.problems.push(`id ${t} is in both ${s} and ${p}`),n;b.set(t,p),d.set(t,S.get(t)),h.set(t,S.get(t)[0]),g[e].push(t)}}const $=[...b.keys()].filter(e=>p.has(e));if($.length)return n.problems.push(`${$.length} train ids are reused in test (e.g. ${$.slice(0,3).join(", ")}) — ids must be unique across files`),n;if(n.nTrain=o.rows.length,n.nA=g.A.length,n.nB=g.B.length,0===n.nA||0===n.nB)return n.problems.push("one board is empty — both test_a.csv and test_b.csv need rows"),n;const w=e.metric===CUSTOM?CUSTOM:METRIC_KIND[e.metric];if(w===CUSTOM);else if("reg"!==w){const t=new Map;for(const e of o.rows)t.set(e[m],(t.get(e[m])||0)+1);n.classes=[...t.entries()].sort((e,t)=>t[1]-e[1]).slice(0,12);const s=new Set(t.keys()),r=[...new Set(h.values())].filter(e=>!s.has(e));r.length&&n.warnings.push(`test labels contain ${r.length} value(s) never seen in train (e.g. ${r.slice(0,3).join(", ")})`),t.size>Math.max(50,n.nTrain/2)&&n.warnings.push(`${t.size} distinct label values with a classification metric — is "${e.label}" really a class?`);const a=[...t.keys(),...h.values()];if("binary"===w||"prob"===w){const t=a.find(e=>""===e.trim()||0!==Number(e)&&1!==Number(e));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs labels that are 0 or 1 — found "${t}"`),n}if("grade"===w){const t=a.find(e=>""===e.trim()||!Number.isInteger(Number(e)));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs whole-number grades as labels — found "${t}"`),n}}else{const t=[...h.values()].find(e=>!Number.isFinite(Number(e)));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs numeric labels — found "${t}"`),n;const s=[...h.values()].map(Number);if("rmsle"===e.metric&&s.some(e=>e<0))return n.problems.push("metric rmsle needs labels that are 0 or more — found a negative one"),n;"mape"===e.metric&&s.some(e=>0===e)&&n.warnings.push("some labels are exactly 0 — mape divides by the label, one such row dominates the score (smape or mae are safer)")}return(n.nA<100||n.nB<100)&&n.warnings.push(`a board with under 100 rows (A ${n.nA}, B ${n.nB}) moves a lot on a single row`),n.nB<n.nA&&n.warnings.push(`B (${n.nB}) is smaller than A (${n.nA}) — the final ranking usually rests on the larger set`),n.ok=!0,n.parsed={train:o,testA:r.testA,testB:r.testB,labels:h,truth:d,aIds:g.A,bIds:g.B},n}const n=(e,t)=>`${[e,...t].map(e=>e.join(",")).join("\n")}\n`;export function studentBundle(t,s){if(!s.ok||!s.parsed)throw new Error("check the task first");const{train:r,testA:o,testB:a}=s.parsed,i=o.header.indexOf(t.id),c=s=>e("sha256").update(`${t.name}:order:${s}`).digest("hex"),l=[...o.rows,...a.rows].sort((e,t)=>c(e[i])<c(t[i])?-1:1),u=predColumns(t);let m;if(t.metric===CUSTOM)m=u.map(e=>{const t=r.header.indexOf(e),s=new Map;for(const e of r.rows)s.set(e[t],(s.get(e[t])||0)+1);return[...s.entries()].sort((e,t)=>t[1]-e[1])[0]?.[0]??"0"});else{const e=r.header.indexOf(u[0]),n=METRIC_KIND[t.metric];m=["prob"===n?"0.5":"reg"===n||"binary"===n?"0":s.classes[0]?.[0]??r.rows[0]?.[e]??"0"]}return{train:n(r.header,r.rows),test:n(o.header,l),sample:n([t.id,...u],l.map(e=>[e[i],...m]))}}export function perfectSubmission(e,t){if(!t.ok||!t.parsed)throw new Error("check the task first");const{truth:s,aIds:r,bIds:o}=t.parsed;return n([e.id,...predColumns(e)],[...r,...o].map(e=>[e,...s.get(e)]))}function r(e,t){if(0===t.size)return Number.POSITIVE_INFINITY;let s=0;for(const[n,r]of t){const t=e.get(n);s+=((void 0!==t&&""!==t.trim()&&Number.isFinite(Number(t))?Number(t):0)-Number(r))**2}return s/t.size}function o(e){if(void 0===e||""===e.trim())return 0;const t=Number(e);return Number.isFinite(t)?t:0}function a(e){return void 0!==e&&""!==e.trim()&&1===Number(e)}function i(...[e,t]){return[...t].map(([t,s])=>[o(e.get(t)),Number(s)])}function c(...[e,t]){return[...t].map(([t,s])=>[o(e.get(t)),a(s)?1:0])}function l(...[e,t]){return[...t].map(([t,s])=>[(e.get(t)??"").trim(),s.trim()])}const u=e=>e.reduce((e,t)=>e+t,0);function m(e,t){return 0===e.length?Number.POSITIVE_INFINITY:u(e.map(([e,s])=>t(e,s)))/e.length}function p(e,t){const s=e.length;if(0===s)return 0;const n=u(e)/s,r=u(t)/s,o=u(e.map((e,s)=>(e-n)*(t[s]-r))),a=u(e.map(e=>(e-n)**2)),i=u(t.map(e=>(e-r)**2));return 0===a||0===i?0:o/Math.sqrt(a*i)}function h(e){const t=e.map((e,t)=>t).sort((t,s)=>e[t]-e[s]),s=new Array(e.length).fill(0);let n=0;for(;n<t.length;){let r=n;for(;r+1<t.length&&e[t[r+1]]===e[t[n]];)r++;for(let e=n;e<=r;e++)s[t[e]]=(n+r)/2+1;n=r+1}return s}function d(e){return[...new Set(e.map(([,e])=>e))].sort().map(t=>({lab:t,tp:e.filter(([e,s])=>e===t&&s===t).length,fp:e.filter(([e,s])=>e===t&&s!==t).length,fn:e.filter(([e,s])=>e!==t&&s===t).length}))}export function metricValue(e,t,s){switch(e){case CUSTOM:throw new Error("a custom task is scored by its scorer.py, not by a built-in metric");case"accuracy":return function(e,t){if(0===t.size)return 0;let s=0;for(const[n,r]of t)(e.get(n)??"").trim()===r.trim()&&s++;return s/t.size}(t,s);case"macro_f1":return function(e,t){const s=[...new Set([...t.values()].map(e=>e.trim()))].sort();if(0===s.length)return 0;let n=0;for(const r of s){let s=0,o=0,a=0;for(const[n,i]of t){const t=(e.get(n)??"").trim(),c=i.trim();t===r&&c===r?s++:t===r&&c!==r?o++:t!==r&&c===r&&a++}const i=2*s+o+a;n+=i?2*s/i:0}return n/s.length}(t,s);case"mse":return r(t,s);case"rmse":return Math.sqrt(r(t,s));case"balanced_accuracy":return function(e){const t=d(e);return 0===t.length?0:u(t.map(e=>e.tp/(e.tp+e.fn)))/t.length}(l(t,s));case"weighted_f1":return function(e){const t=d(e);return 0===t.length?0:u(t.map(e=>{const t=2*e.tp+e.fp+e.fn;return(e.tp+e.fn)*(t?2*e.tp/t:0)}))/e.length}(l(t,s));case"mcc":return function(e){const t=e.length;if(0===t)return 0;const s=new Map,n=new Map;let r=0;for(const[t,o]of e)s.set(o,(s.get(o)||0)+1),n.set(t,(n.get(t)||0)+1),t===o&&r++;const o=r*t-u([...s].map(([e,t])=>t*(n.get(e)||0))),a=t*t-u([...s.values()].map(e=>e*e)),i=t*t-u([...n.values()].map(e=>e*e));return 0===a||0===i?0:o/Math.sqrt(a*i)}(l(t,s));case"f1":return function(...[e,t]){let s=0,n=0,r=0;for(const[o,i]of t){const t=a(e.get(o)),c=a(i);t&&c?s++:t&&!c?n++:c&&!t&&r++}const o=2*s+n+r;return o?2*s/o:0}(t,s);case"qwk":return function(...[e,t]){const s=[...t.values()].map(e=>Math.floor(Number(e)+.5));if(0===s.length)return 0;const n=Math.min(...s),r=Math.max(...s),a=r-n+1;if(a<2)return 0;const i=[...t.keys()].map(t=>Math.min(Math.max(Math.floor(o(e.get(t))+.5),n),r)),c=new Array(a).fill(0),l=new Array(a).fill(0);let u=0;s.forEach((e,t)=>{c[e-n]++,l[i[t]-n]++,u+=(i[t]-e)**2});let m=0;for(let e=0;e<a;e++)for(let t=0;t<a;t++)m+=(e-t)**2*c[e]*l[t];return m/=s.length,0===m?0:1-u/m}(t,s);case"auc":return function(e){const t=u(e.map(([,e])=>e)),s=e.length-t;if(0===t||0===s)return.5;const n=h(e.map(([e])=>e));return(u(n.filter((t,s)=>1===e[s][1]))-t*(t+1)/2)/(t*s)}(c(t,s));case"ap":return function(e){const t=[...e].sort((e,t)=>t[0]-e[0]),s=u(t.map(([,e])=>e));if(0===s)return 0;let n=0,r=0,o=0,a=0,i=0;for(;i<t.length;){let e=i;for(;e<t.length&&t[e][0]===t[i][0];)r+=t[e][1],o++,e++;const c=r/s;n+=r/o*(c-a),a=c,i=e}return n}(c(t,s));case"logloss":return m(c(t,s),(e,t)=>{const s=Math.min(Math.max(e,1e-15),1-1e-15);return 1===t?-Math.log(s):-Math.log(1-s)});case"brier":return m(c(t,s),(e,t)=>(e-t)**2);case"mae":return m(i(t,s),(e,t)=>Math.abs(e-t));case"rmsle":return Math.sqrt(m(i(t,s),(e,t)=>(Math.log1p(Math.max(e,0))-Math.log1p(Math.max(t,0)))**2));case"mape":return m(i(t,s),(e,t)=>Math.abs(e-t)/Math.max(Math.abs(t),2220446049250313e-31));case"smape":return m(i(t,s),(e,t)=>{const s=Math.abs(e)+Math.abs(t);return s?2*Math.abs(e-t)/s:0});case"r2":return function(e){if(0===e.length)return 0;const t=u(e.map(([,e])=>e))/e.length,s=u(e.map(([,e])=>(e-t)**2));return 0===s?0:1-u(e.map(([e,t])=>(e-t)**2))/s}(i(t,s));case"pearson":{const e=i(t,s);return p(e.map(([e])=>e),e.map(([,e])=>e))}case"spearman":{const e=i(t,s);return p(h(e.map(([e])=>e)),h(e.map(([,e])=>e)))}}}export function readSubmission(e,t,s){const n={ok:!1,problems:[],nMissing:0,pred:new Map};if(!t.ok||!t.parsed)return n.problems.push("check the task first"),n;const{table:r,problems:o}=parseCsv(s);if(o.length)return n.problems.push(...o),n;const a=[e.id,...predColumns(e)];if(r.header.join(",")!==a.join(","))return n.problems.push(2===a.length?`the submission needs exactly two columns: ${a.join(",")} — got ${r.header.join(",")}`:`the submission needs exactly these columns, in this order: ${a.join(",")} — got ${r.header.join(",")}`),n;const i=n.pred;for(const e of r.rows){if(i.has(e[0]))return n.problems.push(`duplicate id ${e[0]} in the submission`),n;i.set(e[0],e.slice(1))}const{labels:c}=t.parsed,l=[...i.keys()].filter(e=>!c.has(e));return l.length?(n.problems.push(`${l.length} ids are not in the test set (e.g. ${l.slice(0,3).join(", ")})`),n):(n.nMissing=c.size-i.size,n.nMissing>0?(n.problems.push(`${n.nMissing} test rows have no prediction — the judge refuses an incomplete submission`),n):(n.ok=!0,n))}export function scoreSubmission(e,t,s){const n={ok:!1,problems:[],a:0,b:0,all:0,gap:0,nMissing:0},r=readSubmission(e,t,s);if(n.nMissing=r.nMissing,!r.ok||!t.parsed)return n.problems.push(...r.problems),n;if(e.metric===CUSTOM)return n.problems.push("this task is scored by its scorer.py — setter score runs it"),n;const o=new Map([...r.pred].map(([e,t])=>[e,t[0]])),{labels:a,aIds:i,bIds:c}=t.parsed,l=e=>new Map(e.map(e=>[e,a.get(e)]));return n.a=metricValue(e.metric,o,l(i)),n.b=metricValue(e.metric,o,l(c)),n.all=metricValue(e.metric,o,a),n.gap=LOWER_IS_BETTER.has(e.metric)?n.b-n.a:n.a-n.b,n.ok=!0,n}export function verdictLines(e,t,s,n){const r=[],o=void 0===n?LOWER_IS_BETTER.has(e.metric):!n,a=(e,t)=>o?e<t:e>t,i=Math.abs(t.gap);if(i/Math.max(Math.abs(t.all),1e-9)>.05?r.push(`A and B disagree by ${i.toFixed(4)} on the baseline — check that the two test sets come from the same distribution`):r.push(`A and B agree on the baseline (difference ${i.toFixed(4)})`),s){if(a(s.b,t.b)){const e=Math.abs(s.b-t.b);r.push(`room to climb on B: ${e.toFixed(4)} (baseline ${t.b.toFixed(4)} → reference ${s.b.toFixed(4)})`),e<2*i&&r.push("that room is smaller than twice the A/B difference — the ranking may come down to noise")}else r.push("the reference does NOT beat the baseline on B — contestants have nothing to climb");a(s.a,t.a)!==a(s.b,t.b)&&r.push("baseline and reference rank differently on A and on B — the public board would mislead")}return r}export function tarArchive(e){const t=[],s=new TextEncoder;for(const n of e){const e=new Uint8Array(512),r=(t,n,r)=>{e.set(s.encode(t).subarray(0,r),n)};if(s.encode(n.name).length>99)throw new Error(`file name too long for the archive: ${n.name}`);r(n.name,0,100),r("0000644\0",100,8),r("0000000\0",108,8),r("0000000\0",116,8),r(`${n.data.length.toString(8).padStart(11,"0")}\0`,124,12),r("00000000000\0",136,12),r(" ",148,8),r("0",156,1),r("ustar\0",257,6),r("00",263,2);let o=0;for(const t of e)o+=t;r(`${o.toString(8).padStart(6,"0")}\0 `,148,8),t.push(e,n.data);const a=(512-n.data.length%512)%512;a&&t.push(new Uint8Array(a))}t.push(new Uint8Array(1024));const n=t.reduce((e,t)=>e+t.length,0),r=new Uint8Array(n);let o=0;for(const e of t)r.set(e,o),o+=e.length;return r}export const BASELINE_FILE="baseline.ipynb";export const TASK_FILES=["task.json","train.csv","test_a.csv","labels_a.csv","test_b.csv","labels_b.csv",BASELINE_FILE];export function scorerTemplate(e=["label"]){const t=e[0];return`# scorer.py — how this task is scored: several metrics, each with a weight.\n# 只改标了 1 2 3 的三处,其余不用动。\n# Edit the three numbered places and nothing else, then type: setter check\n# scorer.py may only use Python's maths modules and icoa_metrics.\nfrom icoa_metrics import * # the ready-made metrics; the list is at the top of icoa_metrics.py\n\nCOLUMNS = [${e.map(e=>`"${e}"`).join(", ")}] # 1. what a contestant submits (besides id)\nHIGHER_IS_BETTER = True\n\n\ndef score(pred, truth):\n # pred[column] = the contestant's values, row by row\n # truth[column] = the same rows of your labels file (every column of it, also ones only you have)\n return weighted({\n # 2. one line per metric 3. its weight — the weights add up to 1\n "accuracy": (0.70, accuracy(pred["${t}"], truth["${t}"])),\n "macro_f1": (0.30, macro_f1(pred["${t}"], truth["${t}"])),\n })\n`}export const INLINE_LIMIT_BYTES=5242880;export function baselineCells(e){if(e.metric===CUSTOM)return[{kind:"markdown",source:`# ${e.title}\n\nDescribe the task here: what one row is, what each column to predict means, and how a submission is scored (the parts and their weights).\n\nThis notebook is the baseline every contestant starts from. Keep it short and make sure it runs top to bottom.`},{kind:"code",source:'import pandas as pd\n\ntrain = pd.read_csv("train.csv")\ntest = pd.read_csv("test.csv")\ntargets = [c for c in train.columns if c not in test.columns] # the columns to predict\nprint(train.shape, test.shape, targets)\ntrain.head()'},{kind:"code",source:`# Baseline: predict the most common training value of every column, for every row.\nsub = pd.DataFrame({"${e.id}": test["${e.id}"]})\nfor c in targets:\n sub[c] = train[c].mode()[0]`},{kind:"code",source:'sub.to_csv("submission.csv", index=False)\nprint("wrote submission.csv —", len(sub), "rows,", "columns:", list(sub.columns))'}];const t=METRIC_KIND[e.metric],s="reg"===t||"prob"===t;return[{kind:"markdown",source:`# ${e.title}\n\nDescribe the task here: what one row is, what "${e.label}" means, and how a submission is scored (**${e.metric}**).\n\nThis notebook is the baseline every contestant starts from. Keep it short and make sure it runs top to bottom.`},{kind:"code",source:'import pandas as pd\n\ntrain = pd.read_csv("train.csv")\ntest = pd.read_csv("test.csv")\nprint(train.shape, test.shape)\ntrain.head()'},{kind:"code",source:s?`# Baseline: predict the training mean for every row${"prob"===t?" (the share of 1s — a probability)":""}.\npred = [train["${e.label}"].mean()] * len(test)`:`# Baseline: predict the most common class for every row.\npred = [train["${e.label}"].mode()[0]] * len(test)`},{kind:"code",source:`pd.DataFrame({"${e.id}": test["${e.id}"], "${e.label}": pred}).to_csv("submission.csv", index=False)\nprint("wrote submission.csv —", len(pred), "rows")`}]}export const SQUARE_GIB=3.5;export const CARD_UNITS={l4:6,ada:5,t4:4};export function capsulePlan(e,t,s,n){if(!s)return{tier:"cpu",squares:0,fits:!0,capGib:0,perCard:{l4:0,ada:0,t4:0}};const r=[1,2,3].find(t=>e<3.5*t-.5);if(!r)return{tier:"gpu",squares:3,fits:!1,capGib:10,perCard:{l4:0,ada:0,t4:0}};const o=(e,t)=>3===r&&"l4"!==t?0:Math.floor(e/r),a={l4:o(CARD_UNITS.l4,"l4"),ada:o(CARD_UNITS.ada,"ada"),t4:o(CARD_UNITS.t4,"t4")},i={tier:"gpu",squares:r,fits:!0,capGib:3.5*r-.5,perCard:a},c=e=>({l4:e(a.l4),ada:e(a.ada),t4:e(a.t4)});return n&&n>0&&(i.cards=c(e=>e?Math.ceil(n/e):0)),t>0&&(i.runsPerCardHour=c(e=>Math.floor(60*e/t))),i}
1
+ import{createHash as e}from"node:crypto";import{existsSync as t,readdirSync as n,statSync as s}from"node:fs";import{join as r}from"node:path";export const METRIC_KIND={accuracy:"class",balanced_accuracy:"class",macro_f1:"class",weighted_f1:"class",mcc:"class",f1:"binary",qwk:"grade",auc:"prob",ap:"prob",logloss:"prob",brier:"prob",mse:"reg",rmse:"reg",mae:"reg",rmsle:"reg",mape:"reg",smape:"reg",r2:"reg",pearson:"reg",spearman:"reg"};export const METRICS=Object.keys(METRIC_KIND);export const LOWER_IS_BETTER=new Set(["mse","rmse","mae","rmsle","mape","smape","logloss","brier"]);export function metricGroups(){const e=(...e)=>METRICS.filter(t=>e.includes(METRIC_KIND[t]));return[{kind:"classes",names:e("class","binary","grade")},{kind:"probability of 1",names:e("prob")},{kind:"numbers",names:e("reg")}]}export const CUSTOM="custom";export const CUSTOM_WORDS=["weighted","custom","multi"];export const SEGMENT_WORDS=["segmentation","segment","seg","mask","masks"];export const DATA_DIR="data";export const SCORER_FILE="scorer.py";export const HELPER_FILE="icoa_metrics.py";export function predColumns(e){return e.columns?.length?e.columns:[e.label]}function o(e,t,n){if(0===e.length)return`${n}: list at least one column`;const s=e.find(e=>""===e||/[,"\s]/.test(e));return void 0!==s?`${n}: "${s}" is not a usable column name (no spaces, commas or quotes)`:e.includes(t)?`${n}: "${t}" is the id column — it cannot also be predicted`:new Set(e).size!==e.length?`${n}: a column is listed twice`:null}export function scorerColumns(e){const t=/^COLUMNS[ \t]*=[ \t]*\[([^\]\n]*)\]/m.exec(e);return t?[...t[1].matchAll(/"([^"]*)"|'([^']*)'/g)].map(e=>(e[1]??e[2]).trim()):null}export function withScorerColumns(e,t){if(e.metric!==CUSTOM||void 0===t)return{meta:e,problems:[]};const n=scorerColumns(t);if(!n)return{meta:e,problems:[]};const s=o(n,e.id,"scorer.py COLUMNS");return s?{meta:e,problems:[s]}:e.columns&&e.columns.join(",")!==n.join(",")?{meta:e,problems:[`scorer.py says COLUMNS = ${n.join(", ")} but task.json says columns = ${e.columns.join(", ")} — keep one (remove "columns" from task.json)`]}:{meta:{...e,columns:n,label:n[0]},problems:[]}}export function parseCsv(e){const t=[],n=e.replace(/^/,"").split(/\r?\n/).filter(e=>""!==e.trim());if(0===n.length)return{table:{header:[],rows:[]},problems:["file is empty"]};e.includes('"')&&t.push('quoted fields (") are not supported — write plain comma-separated values');const s=n[0].split(",").map(e=>e.trim()),r=[];for(let e=1;e<n.length;e++){const o=n[e].split(",").map(e=>e.trim());if(o.length!==s.length){t.push(`row ${e+1} has ${o.length} fields, the header has ${s.length}`);break}r.push(o)}return{table:{header:s,rows:r},problems:t}}export function validName(e){return/^[a-z0-9][a-z0-9-]{1,38}[a-z0-9]$/.test(e)}export function parseTaskMeta(e){let t;try{t=JSON.parse(e)}catch{return{meta:null,problems:["task.json is not valid JSON"]}}const n=[],s=String(t.name??""),r=String(t.metric??""),a=r===CUSTOM,i=String(t.id||"id");let c;if(void 0!==t.columns)if(!Array.isArray(t.columns)||t.columns.some(e=>"string"!=typeof e))n.push('task.json columns: a list of column names, e.g. ["rain", "mm"]');else{c=t.columns.map(e=>e.trim());const e=o(c,i,"task.json columns");e?n.push(e):a||1===c.length||n.push(`task.json columns: metric ${r} scores one column — several columns need "metric": "${CUSTOM}" (setter new <name> weighted)`)}const l=String(t.label??"")||c?.[0]||(a?"label":"");if(validName(s)||n.push("task.json name: 3–40 characters of a–z, 0–9 and -"),a||METRICS.includes(r)||n.push(`task.json metric: one of ${METRICS.join(" / ")} / ${CUSTOM}`),l||n.push("task.json label: the name of the column to predict"),n.length)return{meta:null,problems:n};const u=Number(t.per_day),m={name:s,title:String(t.title||s),metric:r,label:l,id:i,per_day:Number.isFinite(u)&&u>=1?Math.floor(u):5,description:"string"==typeof t.description?t.description:void 0};return c&&(m.columns=c,a||(m.label=c[0])),{meta:m,problems:[]}}const a={train:"train.csv",testA:"test_a.csv",labelsA:"labels_a.csv",testB:"test_b.csv",labelsB:"labels_b.csv"};export function checkTask(e,t){const n={ok:!1,problems:[],warnings:[],nTrain:0,nA:0,nB:0,features:[],classes:[]},s={};for(const e of Object.keys(a)){const r=t[e];if(void 0===r){n.problems.push(`${a[e]} is missing`);continue}const{table:o,problems:i}=parseCsv(r);for(const t of i)n.problems.push(`${a[e]}: ${t}`);s[e]=o}if(n.problems.length)return n;const r=s.train,o=e.id,i=predColumns(e),c=e.metric===CUSTOM;if(!r.header.includes(o)||i.some(e=>!r.header.includes(e)))return n.problems.push(1===i.length?`train.csv needs both "${o}" and "${i[0]}" columns — its header is ${r.header.join(", ")}`:`train.csv needs "${o}" and every prediction column (${i.join(", ")}) — its header is ${r.header.join(", ")}`),n;const l=r.header.filter(e=>!i.includes(e));n.features=l.filter(e=>e!==o);const u=r.header.indexOf(o),m=r.header.indexOf(e.label),h=new Set;for(const e of r.rows){if(h.has(e[u]))return n.problems.push(`train.csv: duplicate id ${e[u]}`),n;h.add(e[u])}const p=new Map,d=new Map;let f=null;const b=new Map,g={A:[],B:[]};for(const[e,t,r]of[["A","testA","labelsA"],["B","testB","labelsB"]]){const u=s[t],m=s[r],h=a[t],w=a[r],$=i.find(e=>u.header.includes(e));if(void 0!==$)return n.problems.push(`${h} carries the label column "${$}" — remove it, labels go in ${w}`),n;if(u.header.join(",")!==l.join(","))return n.problems.push(`${h} columns differ from train.csv minus the label — test: ${u.header.join(", ")} · train: ${l.join(", ")}`),n;if(c){const e=[o,...i].find(e=>!m.header.includes(e));if(void 0!==e)return n.problems.push(`${w} has no "${e}" column — it needs ${[o,...i].join(", ")} (extra columns for the scorer are fine); got ${m.header.join(", ")}`),n;if(new Set(m.header).size!==m.header.length)return n.problems.push(`${w} has a column name twice — got ${m.header.join(", ")}`),n;if(null!==f&&f!==m.header.join(","))return n.problems.push(`labels_a.csv and labels_b.csv must have the same columns — A: ${f.split(",").join(", ")} · B: ${m.header.join(", ")}`),n;f=m.header.join(",")}else if(2!==m.header.length||m.header[0]!==o||m.header[1]!==i[0])return n.problems.push(`${w} must have exactly two columns (${o}, ${i[0]}) — got ${m.header.join(", ")}`),n;const y=m.header.indexOf(o),k=i.map(e=>m.header.indexOf(e)),v=new Set,M=u.header.indexOf(o);for(const e of u.rows){if(v.has(e[M]))return n.problems.push(`${h} has duplicate ids (${e[M]})`),n;v.add(e[M])}const x=new Map;for(const e of m.rows){const t=e[y],s=k.map(t=>e[t]);if(""===t||s.includes(""))return n.problems.push(`${w}: empty id or label near id ${t||"(blank)"}`),n;if(x.has(t))return n.problems.push(`${w}: duplicate id ${t}`),n;x.set(t,s)}const _=[...v].filter(e=>x.has(e)).length;if(_!==v.size||_!==x.size)return n.problems.push(`${h}: ${v.size} ids in the test file, ${x.size} in ${w}, ${_} in both — they must be the same set`),n;for(const t of v){const s=b.get(t);if(s)return n.problems.push(`id ${t} is in both ${s} and ${h}`),n;b.set(t,h),d.set(t,x.get(t)),p.set(t,x.get(t)[0]),g[e].push(t)}}const w=[...b.keys()].filter(e=>h.has(e));if(w.length)return n.problems.push(`${w.length} train ids are reused in test (e.g. ${w.slice(0,3).join(", ")}) — ids must be unique across files`),n;if(n.nTrain=r.rows.length,n.nA=g.A.length,n.nB=g.B.length,0===n.nA||0===n.nB)return n.problems.push("one board is empty — both test_a.csv and test_b.csv need rows"),n;const $=e.metric===CUSTOM?CUSTOM:METRIC_KIND[e.metric];if($===CUSTOM);else if("reg"!==$){const t=new Map;for(const e of r.rows)t.set(e[m],(t.get(e[m])||0)+1);n.classes=[...t.entries()].sort((e,t)=>t[1]-e[1]).slice(0,12);const s=new Set(t.keys()),o=[...new Set(p.values())].filter(e=>!s.has(e));o.length&&n.warnings.push(`test labels contain ${o.length} value(s) never seen in train (e.g. ${o.slice(0,3).join(", ")})`),t.size>Math.max(50,n.nTrain/2)&&n.warnings.push(`${t.size} distinct label values with a classification metric — is "${e.label}" really a class?`);const a=[...t.keys(),...p.values()];if("binary"===$||"prob"===$){const t=a.find(e=>""===e.trim()||0!==Number(e)&&1!==Number(e));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs labels that are 0 or 1 — found "${t}"`),n}if("grade"===$){const t=a.find(e=>""===e.trim()||!Number.isInteger(Number(e)));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs whole-number grades as labels — found "${t}"`),n}}else{const t=[...p.values()].find(e=>!Number.isFinite(Number(e)));if(void 0!==t)return n.problems.push(`metric ${e.metric} needs numeric labels — found "${t}"`),n;const s=[...p.values()].map(Number);if("rmsle"===e.metric&&s.some(e=>e<0))return n.problems.push("metric rmsle needs labels that are 0 or more — found a negative one"),n;"mape"===e.metric&&s.some(e=>0===e)&&n.warnings.push("some labels are exactly 0 — mape divides by the label, one such row dominates the score (smape or mae are safer)")}return(n.nA<100||n.nB<100)&&n.warnings.push(`a board with under 100 rows (A ${n.nA}, B ${n.nB}) moves a lot on a single row`),n.nB<n.nA&&n.warnings.push(`B (${n.nB}) is smaller than A (${n.nA}) — the final ranking usually rests on the larger set`),n.ok=!0,n.parsed={train:r,testA:s.testA,testB:s.testB,labels:p,truth:d,aIds:g.A,bIds:g.B},n}const i=(e,t)=>`${[e,...t].map(e=>e.join(",")).join("\n")}\n`;export function studentBundle(t,n){if(!n.ok||!n.parsed)throw new Error("check the task first");const{train:s,testA:r,testB:o}=n.parsed,a=r.header.indexOf(t.id),c=n=>e("sha256").update(`${t.name}:order:${n}`).digest("hex"),l=[...r.rows,...o.rows].sort((e,t)=>c(e[a])<c(t[a])?-1:1),u=predColumns(t);let m;if(t.metric===CUSTOM)m=u.map(e=>{const t=s.header.indexOf(e),n=new Map;for(const e of s.rows)n.set(e[t],(n.get(e[t])||0)+1);return[...n.entries()].sort((e,t)=>t[1]-e[1])[0]?.[0]??"0"});else{const e=s.header.indexOf(u[0]),r=METRIC_KIND[t.metric];m=["prob"===r?"0.5":"reg"===r||"binary"===r?"0":n.classes[0]?.[0]??s.rows[0]?.[e]??"0"]}return{train:i(s.header,s.rows),test:i(r.header,l),sample:i([t.id,...u],l.map(e=>[e[a],...m]))}}export function perfectSubmission(e,t){if(!t.ok||!t.parsed)throw new Error("check the task first");const{truth:n,aIds:s,bIds:r}=t.parsed;return i([e.id,...predColumns(e)],[...s,...r].map(e=>[e,...n.get(e)]))}function c(e,t){if(0===t.size)return Number.POSITIVE_INFINITY;let n=0;for(const[s,r]of t){const t=e.get(s);n+=((void 0!==t&&""!==t.trim()&&Number.isFinite(Number(t))?Number(t):0)-Number(r))**2}return n/t.size}function l(e){if(void 0===e||""===e.trim())return 0;const t=Number(e);return Number.isFinite(t)?t:0}function u(e){return void 0!==e&&""!==e.trim()&&1===Number(e)}function m(...[e,t]){return[...t].map(([t,n])=>[l(e.get(t)),Number(n)])}function h(...[e,t]){return[...t].map(([t,n])=>[l(e.get(t)),u(n)?1:0])}function p(...[e,t]){return[...t].map(([t,n])=>[(e.get(t)??"").trim(),n.trim()])}const d=e=>e.reduce((e,t)=>e+t,0);function f(e,t){return 0===e.length?Number.POSITIVE_INFINITY:d(e.map(([e,n])=>t(e,n)))/e.length}function b(e,t){const n=e.length;if(0===n)return 0;const s=d(e)/n,r=d(t)/n,o=d(e.map((e,n)=>(e-s)*(t[n]-r))),a=d(e.map(e=>(e-s)**2)),i=d(t.map(e=>(e-r)**2));return 0===a||0===i?0:o/Math.sqrt(a*i)}function g(e){const t=e.map((e,t)=>t).sort((t,n)=>e[t]-e[n]),n=new Array(e.length).fill(0);let s=0;for(;s<t.length;){let r=s;for(;r+1<t.length&&e[t[r+1]]===e[t[s]];)r++;for(let e=s;e<=r;e++)n[t[e]]=(s+r)/2+1;s=r+1}return n}function w(e){return[...new Set(e.map(([,e])=>e))].sort().map(t=>({lab:t,tp:e.filter(([e,n])=>e===t&&n===t).length,fp:e.filter(([e,n])=>e===t&&n!==t).length,fn:e.filter(([e,n])=>e!==t&&n===t).length}))}export function metricValue(e,t,n){switch(e){case CUSTOM:throw new Error("a custom task is scored by its scorer.py, not by a built-in metric");case"accuracy":return function(e,t){if(0===t.size)return 0;let n=0;for(const[s,r]of t)(e.get(s)??"").trim()===r.trim()&&n++;return n/t.size}(t,n);case"macro_f1":return function(e,t){const n=[...new Set([...t.values()].map(e=>e.trim()))].sort();if(0===n.length)return 0;let s=0;for(const r of n){let n=0,o=0,a=0;for(const[s,i]of t){const t=(e.get(s)??"").trim(),c=i.trim();t===r&&c===r?n++:t===r&&c!==r?o++:t!==r&&c===r&&a++}const i=2*n+o+a;s+=i?2*n/i:0}return s/n.length}(t,n);case"mse":return c(t,n);case"rmse":return Math.sqrt(c(t,n));case"balanced_accuracy":return function(e){const t=w(e);return 0===t.length?0:d(t.map(e=>e.tp/(e.tp+e.fn)))/t.length}(p(t,n));case"weighted_f1":return function(e){const t=w(e);return 0===t.length?0:d(t.map(e=>{const t=2*e.tp+e.fp+e.fn;return(e.tp+e.fn)*(t?2*e.tp/t:0)}))/e.length}(p(t,n));case"mcc":return function(e){const t=e.length;if(0===t)return 0;const n=new Map,s=new Map;let r=0;for(const[t,o]of e)n.set(o,(n.get(o)||0)+1),s.set(t,(s.get(t)||0)+1),t===o&&r++;const o=r*t-d([...n].map(([e,t])=>t*(s.get(e)||0))),a=t*t-d([...n.values()].map(e=>e*e)),i=t*t-d([...s.values()].map(e=>e*e));return 0===a||0===i?0:o/Math.sqrt(a*i)}(p(t,n));case"f1":return function(...[e,t]){let n=0,s=0,r=0;for(const[o,a]of t){const t=u(e.get(o)),i=u(a);t&&i?n++:t&&!i?s++:i&&!t&&r++}const o=2*n+s+r;return o?2*n/o:0}(t,n);case"qwk":return function(...[e,t]){const n=[...t.values()].map(e=>Math.floor(Number(e)+.5));if(0===n.length)return 0;const s=Math.min(...n),r=Math.max(...n),o=r-s+1;if(o<2)return 0;const a=[...t.keys()].map(t=>Math.min(Math.max(Math.floor(l(e.get(t))+.5),s),r)),i=new Array(o).fill(0),c=new Array(o).fill(0);let u=0;n.forEach((e,t)=>{i[e-s]++,c[a[t]-s]++,u+=(a[t]-e)**2});let m=0;for(let e=0;e<o;e++)for(let t=0;t<o;t++)m+=(e-t)**2*i[e]*c[t];return m/=n.length,0===m?0:1-u/m}(t,n);case"auc":return function(e){const t=d(e.map(([,e])=>e)),n=e.length-t;if(0===t||0===n)return.5;const s=g(e.map(([e])=>e));return(d(s.filter((t,n)=>1===e[n][1]))-t*(t+1)/2)/(t*n)}(h(t,n));case"ap":return function(e){const t=[...e].sort((e,t)=>t[0]-e[0]),n=d(t.map(([,e])=>e));if(0===n)return 0;let s=0,r=0,o=0,a=0,i=0;for(;i<t.length;){let e=i;for(;e<t.length&&t[e][0]===t[i][0];)r+=t[e][1],o++,e++;const c=r/n;s+=r/o*(c-a),a=c,i=e}return s}(h(t,n));case"logloss":return f(h(t,n),(e,t)=>{const n=Math.min(Math.max(e,1e-15),1-1e-15);return 1===t?-Math.log(n):-Math.log(1-n)});case"brier":return f(h(t,n),(e,t)=>(e-t)**2);case"mae":return f(m(t,n),(e,t)=>Math.abs(e-t));case"rmsle":return Math.sqrt(f(m(t,n),(e,t)=>(Math.log1p(Math.max(e,0))-Math.log1p(Math.max(t,0)))**2));case"mape":return f(m(t,n),(e,t)=>Math.abs(e-t)/Math.max(Math.abs(t),2220446049250313e-31));case"smape":return f(m(t,n),(e,t)=>{const n=Math.abs(e)+Math.abs(t);return n?2*Math.abs(e-t)/n:0});case"r2":return function(e){if(0===e.length)return 0;const t=d(e.map(([,e])=>e))/e.length,n=d(e.map(([,e])=>(e-t)**2));return 0===n?0:1-d(e.map(([e,t])=>(e-t)**2))/n}(m(t,n));case"pearson":{const e=m(t,n);return b(e.map(([e])=>e),e.map(([,e])=>e))}case"spearman":{const e=m(t,n);return b(g(e.map(([e])=>e)),g(e.map(([,e])=>e)))}}}export function readSubmission(e,t,n){const s={ok:!1,problems:[],nMissing:0,pred:new Map};if(!t.ok||!t.parsed)return s.problems.push("check the task first"),s;const{table:r,problems:o}=parseCsv(n);if(o.length)return s.problems.push(...o),s;const a=[e.id,...predColumns(e)];if(r.header.join(",")!==a.join(","))return s.problems.push(2===a.length?`the submission needs exactly two columns: ${a.join(",")} — got ${r.header.join(",")}`:`the submission needs exactly these columns, in this order: ${a.join(",")} — got ${r.header.join(",")}`),s;const i=s.pred;for(const e of r.rows){if(i.has(e[0]))return s.problems.push(`duplicate id ${e[0]} in the submission`),s;i.set(e[0],e.slice(1))}const{labels:c}=t.parsed,l=[...i.keys()].filter(e=>!c.has(e));return l.length?(s.problems.push(`${l.length} ids are not in the test set (e.g. ${l.slice(0,3).join(", ")})`),s):(s.nMissing=c.size-i.size,s.nMissing>0?(s.problems.push(`${s.nMissing} test rows have no prediction — the judge refuses an incomplete submission`),s):(s.ok=!0,s))}export function scoreSubmission(e,t,n){const s={ok:!1,problems:[],a:0,b:0,all:0,gap:0,nMissing:0},r=readSubmission(e,t,n);if(s.nMissing=r.nMissing,!r.ok||!t.parsed)return s.problems.push(...r.problems),s;if(e.metric===CUSTOM)return s.problems.push("this task is scored by its scorer.py — setter score runs it"),s;const o=new Map([...r.pred].map(([e,t])=>[e,t[0]])),{labels:a,aIds:i,bIds:c}=t.parsed,l=e=>new Map(e.map(e=>[e,a.get(e)]));return s.a=metricValue(e.metric,o,l(i)),s.b=metricValue(e.metric,o,l(c)),s.all=metricValue(e.metric,o,a),s.gap=LOWER_IS_BETTER.has(e.metric)?s.b-s.a:s.a-s.b,s.ok=!0,s}export function verdictLines(e,t,n,s){const r=[],o=void 0===s?LOWER_IS_BETTER.has(e.metric):!s,a=(e,t)=>o?e<t:e>t,i=Math.abs(t.gap);if(i/Math.max(Math.abs(t.all),1e-9)>.05?r.push(`A and B disagree by ${i.toFixed(4)} on the baseline — check that the two test sets come from the same distribution`):r.push(`A and B agree on the baseline (difference ${i.toFixed(4)})`),n){if(a(n.b,t.b)){const e=Math.abs(n.b-t.b);r.push(`room to climb on B: ${e.toFixed(4)} (baseline ${t.b.toFixed(4)} → reference ${n.b.toFixed(4)})`),e<2*i&&r.push("that room is smaller than twice the A/B difference — the ranking may come down to noise")}else r.push("the reference does NOT beat the baseline on B — contestants have nothing to climb");a(n.a,t.a)!==a(n.b,t.b)&&r.push("baseline and reference rank differently on A and on B — the public board would mislead")}return r}export function tarArchive(e){const t=[],n=new TextEncoder;for(const s of e){const e=new Uint8Array(512),r=(t,s,r)=>{e.set(n.encode(t).subarray(0,r),s)};if(n.encode(s.name).length>99)throw new Error(`file name too long for the archive: ${s.name}`);r(s.name,0,100),r("0000644\0",100,8),r("0000000\0",108,8),r("0000000\0",116,8),r(`${s.data.length.toString(8).padStart(11,"0")}\0`,124,12),r("00000000000\0",136,12),r(" ",148,8),r("0",156,1),r("ustar\0",257,6),r("00",263,2);let o=0;for(const t of e)o+=t;r(`${o.toString(8).padStart(6,"0")}\0 `,148,8),t.push(e,s.data);const a=(512-s.data.length%512)%512;a&&t.push(new Uint8Array(a))}t.push(new Uint8Array(1024));const s=t.reduce((e,t)=>e+t.length,0),r=new Uint8Array(s);let o=0;for(const e of t)r.set(e,o),o+=e.length;return r}export const BASELINE_FILE="baseline.ipynb";export const TASK_FILES=["task.json","train.csv","test_a.csv","labels_a.csv","test_b.csv","labels_b.csv",BASELINE_FILE];export function scorerTemplate(e=["label"]){const t=e[0];return`# scorer.py — how this task is scored: several metrics, each with a weight.\n# 只改标了 1 2 3 的三处,其余不用动。\n# Edit the three numbered places and nothing else, then type: setter check\n# scorer.py may only use Python's maths modules and icoa_metrics.\nfrom icoa_metrics import * # the ready-made metrics; the list is at the top of icoa_metrics.py\n\nCOLUMNS = [${e.map(e=>`"${e}"`).join(", ")}] # 1. what a contestant submits (besides id)\nHIGHER_IS_BETTER = True\n\n\ndef score(pred, truth):\n # pred[column] = the contestant's values, row by row\n # truth[column] = the same rows of your labels file (every column of it, also ones only you have)\n return weighted({\n # 2. one line per metric 3. its weight — the weights add up to 1\n "accuracy": (0.70, accuracy(pred["${t}"], truth["${t}"])),\n "macro_f1": (0.30, macro_f1(pred["${t}"], truth["${t}"])),\n })\n`}export function segmentScorerTemplate(){return'# scorer.py — how this segmentation task is scored: mask overlap + outline agreement.\n# 只改标了 1 2 3 的三处,其余不用动。\n# Edit the three numbered places and nothing else, then type: setter check\n# scorer.py may only use Python\'s maths modules and icoa_metrics.\n#\n# One row = one image. A mask is run-length text: "start length start length ..."\n# (pixels numbered from 1, down each column first, then the next column), and "-" for an empty mask.\n# labels_a.csv / labels_b.csv: id,mask,height,width\nfrom icoa_metrics import * # mask_iou · mask_dice · boundary_iou and the rest; the list is at the top\n\nCOLUMNS = ["mask"] # 1. what a contestant submits (besides id)\nHIGHER_IS_BETTER = True\n\n\ndef score(pred, truth):\n h, w = truth["height"], truth["width"] # the size of each image, from your labels file\n return weighted({\n # 2. one line per metric 3. its weight — the weights add up to 1\n "overlap": (0.70, mask_iou(pred["mask"], truth["mask"], h, w)),\n "outline": (0.30, boundary_iou(pred["mask"], truth["mask"], h, w)),\n })\n'}export function segmentBaselineCells(e){return[{kind:"markdown",source:`# ${e.title}\n\nDescribe the task here: what the images show, what the mask marks, and how a submission is scored (the parts and their weights).\n\nOne row = one image. A mask is run-length text: \`start length start length ...\`, pixels numbered from 1 down each column first, and \`-\` for an empty mask.\n\nThis notebook is the baseline every contestant starts from. Keep it short and make sure it runs top to bottom.`},{kind:"code",source:'import numpy as np\nimport pandas as pd\n\ntrain = pd.read_csv("train.csv")\ntest = pd.read_csv("test.csv")\nprint(train.shape, test.shape, list(test.columns))\ntrain.head()'},{kind:"code",source:'def rle_encode(mask):\n """2-D array of 0 / 1 -> run-length text (down each column first)."""\n flat = np.concatenate([[0], np.asarray(mask, dtype=np.uint8).T.flatten(), [0]])\n runs = np.where(flat[1:] != flat[:-1])[0] + 1\n runs[1::2] -= runs[::2]\n return " ".join(map(str, runs)) or "-"\n\n\ndef rle_decode(text, height, width):\n """run-length text -> 2-D array of 0 / 1."""\n flat = np.zeros(height * width, dtype=np.uint8)\n nums = [] if str(text).strip() in ("", "-") else list(map(int, str(text).split()))\n for start, length in zip(nums[::2], nums[1::2]):\n flat[start - 1:start - 1 + length] = 1\n return flat.reshape(width, height).T'},{kind:"code",source:`# Look at one training mask. The images themselves are in data/ — open one with:\n# from PIL import Image; img = Image.open("data/" + train["image"][0])\nimport matplotlib.pyplot as plt\n\nrow = train.iloc[0]\nplt.imshow(rle_decode(row["${e.label}"], int(row["height"]), int(row["width"])), cmap="gray")\nplt.title(str(row["${e.id}"]))\nplt.show()`},{kind:"code",source:'# Baseline: one box in the middle of every image, half as tall and half as wide.\ndef centre_box(height, width):\n mask = np.zeros((height, width), dtype=np.uint8)\n mask[height // 4:height - height // 4, width // 4:width - width // 4] = 1\n return mask\n\n\npred = [rle_encode(centre_box(int(h), int(w))) for h, w in zip(test["height"], test["width"])]'},{kind:"code",source:`pd.DataFrame({"${e.id}": test["${e.id}"], "${e.label}": pred}).to_csv("submission.csv", index=False)\nprint("wrote submission.csv —", len(pred), "rows")`}]}export const FILE_COLUMNS=["image","file","path","audio"];export function listDataFolder(e){const o=r(e,"data");if(!t(o)||!s(o).isDirectory())return null;const a=[],i=(e,t)=>{for(const o of n(e,{withFileTypes:!0})){if(o.name.startsWith("."))continue;const n=t?`${t}/${o.name}`:o.name;o.isDirectory()?i(r(e,o.name),n):o.isFile()&&a.push({rel:n,bytes:s(r(e,o.name)).size})}};return i(o,""),a.sort((e,t)=>e.rel<t.rel?-1:e.rel>t.rel?1:0),{files:a,bytes:a.reduce((e,t)=>e+t.bytes,0)}}export function missingDataFiles(e,t){const n=new Set(t.files.map(e=>e.rel)),s=FILE_COLUMNS.find(t=>e.some(e=>e.header.includes(t)))??null,r=new Set;if(null!==s)for(const t of e){const e=t.header.indexOf(s);if(!(e<0))for(const s of t.rows){const t=(s[e]??"").replace(/\\/g,"/").replace(new RegExp("^(\\./)?(data/)?"),"");n.has(t)||r.add(s[e]??"")}}return{column:s,count:r.size,examples:[...r].slice(0,3)}}export const INLINE_LIMIT_BYTES=5242880;export function baselineCells(e){if(e.metric===CUSTOM)return[{kind:"markdown",source:`# ${e.title}\n\nDescribe the task here: what one row is, what each column to predict means, and how a submission is scored (the parts and their weights).\n\nThis notebook is the baseline every contestant starts from. Keep it short and make sure it runs top to bottom.`},{kind:"code",source:'import pandas as pd\n\ntrain = pd.read_csv("train.csv")\ntest = pd.read_csv("test.csv")\ntargets = [c for c in train.columns if c not in test.columns] # the columns to predict\nprint(train.shape, test.shape, targets)\ntrain.head()'},{kind:"code",source:`# Baseline: predict the most common training value of every column, for every row.\nsub = pd.DataFrame({"${e.id}": test["${e.id}"]})\nfor c in targets:\n sub[c] = train[c].mode()[0]`},{kind:"code",source:'sub.to_csv("submission.csv", index=False)\nprint("wrote submission.csv —", len(sub), "rows,", "columns:", list(sub.columns))'}];const t=METRIC_KIND[e.metric],n="reg"===t||"prob"===t;return[{kind:"markdown",source:`# ${e.title}\n\nDescribe the task here: what one row is, what "${e.label}" means, and how a submission is scored (**${e.metric}**).\n\nThis notebook is the baseline every contestant starts from. Keep it short and make sure it runs top to bottom.`},{kind:"code",source:'import pandas as pd\n\ntrain = pd.read_csv("train.csv")\ntest = pd.read_csv("test.csv")\nprint(train.shape, test.shape)\ntrain.head()'},{kind:"code",source:n?`# Baseline: predict the training mean for every row${"prob"===t?" (the share of 1s — a probability)":""}.\npred = [train["${e.label}"].mean()] * len(test)`:`# Baseline: predict the most common class for every row.\npred = [train["${e.label}"].mode()[0]] * len(test)`},{kind:"code",source:`pd.DataFrame({"${e.id}": test["${e.id}"], "${e.label}": pred}).to_csv("submission.csv", index=False)\nprint("wrote submission.csv —", len(pred), "rows")`}]}export const SQUARE_GIB=3.5;export const CARD_UNITS={l4:6,ada:5,t4:4};export function capsulePlan(e,t,n,s){if(!n)return{tier:"cpu",squares:0,fits:!0,capGib:0,perCard:{l4:0,ada:0,t4:0}};const r=[1,2,3].find(t=>e<3.5*t-.5);if(!r)return{tier:"gpu",squares:3,fits:!1,capGib:10,perCard:{l4:0,ada:0,t4:0}};const o=(e,t)=>3===r&&"l4"!==t?0:Math.floor(e/r),a={l4:o(CARD_UNITS.l4,"l4"),ada:o(CARD_UNITS.ada,"ada"),t4:o(CARD_UNITS.t4,"t4")},i={tier:"gpu",squares:r,fits:!0,capGib:3.5*r-.5,perCard:a},c=e=>({l4:e(a.l4),ada:e(a.ada),t4:e(a.t4)});return s&&s>0&&(i.cards=c(e=>e?Math.ceil(s/e):0)),t>0&&(i.runsPerCardHour=c(e=>Math.floor(60*e/t))),i}
@@ -1 +1 @@
1
- import{inflateSync as t}from"node:zlib";import{colorMode as e,encodeFrame as n}from"./term-video.js";const r=[137,80,78,71,13,10,26,10];export function isPng(t){return t.length>8&&r.every((e,n)=>t[n]===e)}export function decodePng(e){try{if(!isPng(e))return null;const n=new DataView(e.buffer,e.byteOffset,e.byteLength);let r=8,o=0,s=0,l=0,a=0,i=0,c=null,f=null;const u=[];for(;r+8<=e.length;){const t=n.getUint32(r),h=String.fromCharCode(e[r+4],e[r+5],e[r+6],e[r+7]),m=e.subarray(r+8,r+8+t);if("IHDR"===h)o=n.getUint32(r+8),s=n.getUint32(r+12),l=e[r+16],a=e[r+17],i=e[r+20];else if("PLTE"===h)c=m;else if("tRNS"===h)f=m;else if("IDAT"===h)u.push(m);else if("IEND"===h)break;r+=12+t}const h={0:1,2:3,3:1,4:2,6:4}[a];if(!o||!s||!h||0!==i||8!==l&&16!==l||o*s>4e7)return null;if(3===a&&(!c||8!==l))return null;const m=h*(l/8),g=o*m;let p=0;for(const t of u)p+=t.length;const d=new Uint8Array(p);let b=0;for(const t of u)d.set(t,b),b+=t.length;const M=t(d);if(M.length<(g+1)*s)return null;const w=new Uint8Array(g);let x=new Uint8Array(g);const y=new Uint8Array(o*s*4),A=l/8;for(let t=0;t<s;t++){const e=t*(g+1),n=M[e];for(let t=0;t<g;t++){const r=M[e+1+t],o=t>=m?w[t-m]:0,s=x[t],l=t>=m?x[t-m]:0;let a;if(0===n)a=r;else if(1===n)a=r+o;else if(2===n)a=r+s;else if(3===n)a=r+(o+s>>1);else{if(4!==n)return null;{const t=Math.abs(s-l),e=Math.abs(o-l),n=Math.abs(o+s-2*l);a=r+(t<=e&&t<=n?o:e<=n?s:l)}}w[t]=255&a}for(let e=0;e<o;e++){const n=e*m,r=4*(t*o+e);if(0===a)y[r]=y[r+1]=y[r+2]=w[n],y[r+3]=255;else if(2===a)y[r]=w[n],y[r+1]=w[n+A],y[r+2]=w[n+2*A],y[r+3]=255;else if(3===a){const t=3*w[n];y[r]=c[t]??0,y[r+1]=c[t+1]??0,y[r+2]=c[t+2]??0,y[r+3]=f&&w[n]<f.length?f[w[n]]:255}else 4===a?(y[r]=y[r+1]=y[r+2]=w[n],y[r+3]=w[n+A]):(y[r]=w[n],y[r+1]=w[n+A],y[r+2]=w[n+2*A],y[r+3]=w[n+3*A])}x=w.slice()}return{w:o,h:s,data:y}}catch{return null}}export function thumbSize(t,e,n,r=22){const o=Math.max(16,Math.min(100,n-6)),s=2*r,l=Math.min(o/t,s/e,4),a=Math.max(2,Math.round(t*l));let i=Math.max(2,Math.round(e*l));return i%2&&(i+=1),{w:a,h:i}}export function resample(t,e,n){const r=new Uint8Array(e*n*3);for(let o=0;o<n;o++){const s=Math.floor(o*t.h/n),l=Math.max(s+1,Math.floor((o+1)*t.h/n));for(let n=0;n<e;n++){const a=Math.floor(n*t.w/e),i=Math.max(a+1,Math.floor((n+1)*t.w/e));let c=0,f=0,u=0,h=0;for(let e=s;e<l&&e<t.h;e++)for(let n=a;n<i&&n<t.w;n++){const r=4*(e*t.w+n),o=t.data[r+3]/255;c+=t.data[r]*o+255*(1-o),f+=t.data[r+1]*o+255*(1-o),u+=t.data[r+2]*o+255*(1-o),h++}const m=3*(o*e+n);r[m]=h?Math.round(c/h):255,r[m+1]=h?Math.round(f/h):255,r[m+2]=h?Math.round(u/h):255}}return r}export function pngThumbLines(t,e,r,o=22){if("none"===r)return[];const s=decodePng(t);if(!s)return[];const{w:l,h:a}=thumbSize(s.w,s.h,e,o);return n(resample(s,l,a),0,l,a,r)}export function thumbForTerminal(t,n=22){const r=process.stdout.columns||80;return pngThumbLines(t,r,e(process.env,r,!!process.stdout.isTTY),n)}export function mdImages(t){const e=[];let n=!1;for(const r of t.split("\n"))if(/^\s*(```|~~~)/.test(r))n=!n;else if(!n){for(const t of r.matchAll(/!\[([^\]]*)\]\(\s*<?([^)\s>]+)>?[^)]*\)/g))e.push({alt:t[1],target:t[2]});for(const t of r.matchAll(/<img\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/gi)){const n=/\balt\s*=\s*["']([^"']*)["']/i.exec(t[0]);e.push({alt:n?n[1]:"",target:t[1]})}}return e}export function mdWithoutImages(t){return t.replace(/!\[([^\]]*)\]\(\s*<?[^)\s>]+>?[^)]*\)/g,(t,e)=>`[image: ${e||"picture"}]`).replace(/<img\b[^>]*>/gi,t=>{const e=/\balt\s*=\s*["']([^"']*)["']/i.exec(t);return`[image: ${e?.[1]||"picture"}]`})}
1
+ import{inflateSync as t}from"node:zlib";import{colorMode as e,encodeFrame as n}from"./term-video.js";const r=[137,80,78,71,13,10,26,10];export function isPng(t){return t.length>8&&r.every((e,n)=>t[n]===e)}export function decodePng(e){try{if(!isPng(e))return null;const n=new DataView(e.buffer,e.byteOffset,e.byteLength);let r=8,o=0,s=0,l=0,i=0,a=0,c=null,u=null;const f=[];for(;r+8<=e.length;){const t=n.getUint32(r),h=String.fromCharCode(e[r+4],e[r+5],e[r+6],e[r+7]),m=e.subarray(r+8,r+8+t);if("IHDR"===h)o=n.getUint32(r+8),s=n.getUint32(r+12),l=e[r+16],i=e[r+17],a=e[r+20];else if("PLTE"===h)c=m;else if("tRNS"===h)u=m;else if("IDAT"===h)f.push(m);else if("IEND"===h)break;r+=12+t}const h={0:1,2:3,3:1,4:2,6:4}[i];if(!o||!s||!h||0!==a||8!==l&&16!==l||o*s>4e7)return null;if(3===i&&(!c||8!==l))return null;const m=h*(l/8),g=o*m;let p=0;for(const t of f)p+=t.length;const d=new Uint8Array(p);let M=0;for(const t of f)d.set(t,M),M+=t.length;const b=t(d);if(b.length<(g+1)*s)return null;const x=new Uint8Array(g);let w=new Uint8Array(g);const y=new Uint8Array(o*s*4),A=l/8;for(let t=0;t<s;t++){const e=t*(g+1),n=b[e];for(let t=0;t<g;t++){const r=b[e+1+t],o=t>=m?x[t-m]:0,s=w[t],l=t>=m?w[t-m]:0;let i;if(0===n)i=r;else if(1===n)i=r+o;else if(2===n)i=r+s;else if(3===n)i=r+(o+s>>1);else{if(4!==n)return null;{const t=Math.abs(s-l),e=Math.abs(o-l),n=Math.abs(o+s-2*l);i=r+(t<=e&&t<=n?o:e<=n?s:l)}}x[t]=255&i}for(let e=0;e<o;e++){const n=e*m,r=4*(t*o+e);if(0===i)y[r]=y[r+1]=y[r+2]=x[n],y[r+3]=255;else if(2===i)y[r]=x[n],y[r+1]=x[n+A],y[r+2]=x[n+2*A],y[r+3]=255;else if(3===i){const t=3*x[n];y[r]=c[t]??0,y[r+1]=c[t+1]??0,y[r+2]=c[t+2]??0,y[r+3]=u&&x[n]<u.length?u[x[n]]:255}else 4===i?(y[r]=y[r+1]=y[r+2]=x[n],y[r+3]=x[n+A]):(y[r]=x[n],y[r+1]=x[n+A],y[r+2]=x[n+2*A],y[r+3]=x[n+3*A])}w=x.slice()}return{w:o,h:s,data:y}}catch{return null}}export function thumbSize(t,e,n,r=22){const o=Math.max(16,Math.min(100,n-6)),s=2*r,l=Math.min(o/t,s/e,4),i=Math.max(2,Math.round(t*l));let a=Math.max(2,Math.round(e*l));return a%2&&(a+=1),{w:i,h:a}}export function resample(t,e,n){const r=new Uint8Array(e*n*3);for(let o=0;o<n;o++){const s=Math.floor(o*t.h/n),l=Math.max(s+1,Math.floor((o+1)*t.h/n));for(let n=0;n<e;n++){const i=Math.floor(n*t.w/e),a=Math.max(i+1,Math.floor((n+1)*t.w/e));let c=0,u=0,f=0,h=0;for(let e=s;e<l&&e<t.h;e++)for(let n=i;n<a&&n<t.w;n++){const r=4*(e*t.w+n),o=t.data[r+3]/255;c+=t.data[r]*o+255*(1-o),u+=t.data[r+1]*o+255*(1-o),f+=t.data[r+2]*o+255*(1-o),h++}const m=3*(o*e+n);r[m]=h?Math.round(c/h):255,r[m+1]=h?Math.round(u/h):255,r[m+2]=h?Math.round(f/h):255}}return r}export function pngThumbLines(t,e,r,o=22){if("none"===r)return[];const s=decodePng(t);if(!s)return[];const{w:l,h:i}=thumbSize(s.w,s.h,e,o);return n(resample(s,l,i),0,l,i,r)}export function thumbMode(t,n,r){const o=e(t,n,r);return"truecolor"===o&&"truecolor"!==t.ICOA_THUMB?"256":o}export function thumbForTerminal(t,e=22){const n=process.stdout.columns||80;return pngThumbLines(t,n,thumbMode(process.env,n,!!process.stdout.isTTY),e)}export function mdImages(t){const e=[];let n=!1;for(const r of t.split("\n"))if(/^\s*(```|~~~)/.test(r))n=!n;else if(!n){for(const t of r.matchAll(/!\[([^\]]*)\]\(\s*<?([^)\s>]+)>?[^)]*\)/g))e.push({alt:t[1],target:t[2]});for(const t of r.matchAll(/<img\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/gi)){const n=/\balt\s*=\s*["']([^"']*)["']/i.exec(t[0]);e.push({alt:n?n[1]:"",target:t[1]})}}return e}export function mdWithoutImages(t){return t.replace(/!\[([^\]]*)\]\(\s*<?[^)\s>]+>?[^)]*\)/g,(t,e)=>`[image: ${e||"picture"}]`).replace(/<img\b[^>]*>/gi,t=>{const e=/\balt\s*=\s*["']([^"']*)["']/i.exec(t);return`[image: ${e?.[1]||"picture"}]`})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "icoa-cli",
3
- "version": "2.19.714",
3
+ "version": "2.19.716",
4
4
  "description": "ICOA CLI — AI-native competition runtime of the International Cyber Olympiad in AI: learn, practise, and compete in AI & cybersecurity from the terminal (AI4CTF · CTF4AI · CTF4EAI)",
5
5
  "author": "Charlie Zhu",
6
6
  "type": "module",