Metadata-Version: 2.4
Name: olympiad-lab
Version: 0.1.0
Summary: Checkpoints for AI Olympiad training labs: sends evidence from a notebook to a scorer server and shows the verdict.
Author: olympiad-lab maintainers
License-Expression: MIT
Project-URL: Homepage, https://github.com/SattamAltwaim/olympiad-lab
Project-URL: Repository, https://github.com/SattamAltwaim/olympiad-lab
Project-URL: Issues, https://github.com/SattamAltwaim/olympiad-lab/issues
Project-URL: Changelog, https://github.com/SattamAltwaim/olympiad-lab/blob/main/CHANGELOG.md
Keywords: education,notebook,jupyter,colab,kaggle,machine-learning,olympiad
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Education
Classifier: Framework :: IPython
Classifier: Framework :: Jupyter
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Education
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25
Dynamic: license-file

# olympiad-lab

Checkpoints for AI Olympiad training labs.

A lab notebook calls a few short steps (`ck.load`, `ck.look`, `ck.split`, ...) between the
student's own code. Each step describes what was just built and sends that description to the
scorer server of the course. The server decides whether the step passes and answers with one
line. The library holds no expected value, no rule and no label: the notebook sends evidence, the
server decides.

## Install

```
pip install olympiad-lab
```

The only dependency is `requests`. NumPy, pandas, torch and the other libraries of a lab are
used when the notebook has already imported them, and never imported by this package.

## What this library records

Read this before you use it. From the moment `ck.start()` runs, and only in that notebook:

- **Every cell you run**: the first 4000 characters of its source, how long it took, whether it
  raised, and for an error its type, its message and the last 15 lines of the traceback.
- **What each step describes**: types, shapes, row counts, column names, data types, small number
  summaries (minimum, maximum, mean, count of missing values) and the first 200 characters of how
  the object prints. For `ck.split` the row ids (or their hashes for very large sets). For
  `ck.loop` up to 200 loss values. For `ck.eval` the score.
- **What you hand in**: the submission file, the answers of `ck.reading` (including the source of
  the metric function you pass), and at `ck.finish()` the notebook itself. When the notebook
  program does not hand it over, the cells this kernel ran are sent in its place.
- **Where it runs**: Colab, Kaggle, Jupyter, VS Code or script; the Python and IPython versions;
  the operating system name; the GPU model.

It does not record other notebooks, other files, the values of environment variables, or your
account. Cells that ran before `ck.start()` are not recorded.

Three things are taken out on your computer, before anything is stored or sent, from cell source,
error messages, tracebacks and the notebook:

- **Your student id** becomes `<student_id>`.
- **Anything that looks like a secret** becomes `<redacted>`: access tokens of well-known shapes
  (Hugging Face, OpenAI, GitHub, Google, Slack, AWS, Kaggle, a private key); a value given to a
  name such as `api_key`, `HF_TOKEN` or `os.environ["KAGGLE_KEY"]`, also in a line such as
  `%env KAGGLE_KEY=...`, when it has the shape of a key (twelve or more letters and digits, mixed
  through one another); any value given to a name that ends in `password`; the password inside
  an address and the token of an `Authorization` header.
- **Names**: your home folder is written as `~`, and the account name that Colab writes into a
  notebook is removed.

Code that holds no secret stays exactly as you wrote it: `label_key = "category_name"` and
`stop_token = "<|end_of_text|>"` are not keys, although their names end like one.

This is a safety net, not a guarantee. A secret that looks like any other word cannot be told
apart from one, so do not paste keys into a notebook: use `getpass.getpass()` or the secrets
panel of Colab or Kaggle.

The records go to the scorer server over HTTPS and are used by the instructors of the course for
assessment. A copy is kept next to the notebook in `olympiad_<lab>_<student>.jsonl`.
`ck.start()` says so every time:

```
This lab records cell runs, cell source and errors for assessment.
```

## Use

```python
!pip -q install olympiad-lab
from olympiad import ck

ck.start("nlp-01", "S07-K4QX")                  # the lab id and your student id
ck.load(train=train_df, test=test_df)           # LOAD: the data is in memory
ck.look(train_df.head(), size=len(train_df))    # LOOK: shows a sample, prints the size
ck.split(train_ids=tr.id, val_ids=va.id)        # SPLIT: no row in both sets
ck.batch(X_tr[:32], y_tr[:32])                  # BATCH: one batch or feature table
ck.model(model, X_va[:32])                      # MODEL: runs the model on one batch
ck.loop(losses)                                 # LOOP: the training loss fell
ck.eval(val_f1, metric="macro_f1")              # EVAL: prints the validation score
ck.submit("submission.csv")                     # SUBMIT: a file, a DataFrame or the text of a file
ck.finish()                                     # sends what waits, prints a summary
```

Every step prints a tick or a cross, one sentence, and a strip of the lab's parts:

```
✓ BATCH: Batch shape and data type are right.
Parts 4 of 7: LOAD ✓ | LOOK ✓ | SPLIT ✓ | BATCH ✓ | MODEL - | EVAL - | SUBMIT -
```

In a notebook the same block is shown as a small box that reads the same in a light and in a
dark theme. A part that the lab does not use answers `! LOOP is not part of this lab.` A step
that runs before `ck.start()` does nothing and says:

```
! olympiad: run the first cell (ck.start) before this one.
```

Other steps:

| Step | What it does |
|---|---|
| `ck.reading(metric_fn=..., trivial={...}, rewards="b", format={...}, facts="...")` | Sends the task reading. It is scored after the lab; no score is shown. |
| `ck.model(...)`, `ck.loop(...)`, `ck.eval(...)`, `ck.submit(...)` with `stage="frozen"` | Names the stage in labs that compare a frozen and a fine-tuned model. |
| `ck.drill_start("id")`, `ck.drill_check("id", result)` | A timed drill: the server stamps the start and answers the check. |
| `ck.status()` | The minute, the parts passed, the submissions used and left. |
| `ck.export()` | One file with everything recorded locally. Returns its path. |
| `ck.last` | The last answer as a dictionary. |

Running a cell again is always safe. `ck.start()` never resets a session or its clock, a check
may be repeated as often as you like (the first pass counts), and the same submission file sent
twice uses one attempt. `ck.start()` with another lab id or another student id in the same
notebook says so in one line (`! START: switched from lab nlp-01 to lab cv-02.`) and goes on
with the new session; what the old one still had to send is sent in the background.

The steps take what a notebook naturally holds:

- A batch may be `(inputs, targets)` as a loader yields it, or a dict such as
  `{"image": ..., "label": ...}`.
- `ck.load` takes an `(X, y)` pair, a data loader, or a dict of splits such as
  `{"train": ..., "test": ...}`.
- A loss history may be a list, an array, one record per epoch (a dict, or a pair such as
  `(train_loss, val_loss)`: the training loss is read, the two are never mixed), a table, or the
  history object of Keras or of a Hugging Face trainer.
- A model may return `(logits, hidden)`; a fitted scikit-learn classifier also reports how many
  classes it knows.
- The metric function of `ck.reading` is called as `metric_fn(y_true, y_pred)` with NumPy arrays
  when the notebook has loaded NumPy, and with plain lists when that raises or when NumPy is not
  loaded. A function written for lists that joins the two with `+` is recognised and gets lists.
- `ck.submit` sends a file or the text of a file as it is. A DataFrame or a list of records is
  written as CSV, or as JSON lines (one object per row) in a lab that asks for them.

An answer of `ck.reading` that cannot be read is named in one line while the reading is still open.

## What it never does

- **It never raises into your code.** Whatever you pass and whatever happens to the network, a
  step prints at most one line such as
  `! olympiad: the LOAD step could not finish (TypeError); your code is not affected.`
  Only `KeyboardInterrupt` and `SystemExit` pass through.
- **It never blocks for long.** Each step has a time budget: 15 seconds for `start`, 6 for a
  check, a reading, a drill or the status, 40 for `submit` and `finish`. Cell records travel in a
  background thread. In Colab, `ck.finish()` first asks the notebook page for the notebook and
  says so (`Reading the notebook; this can take a few seconds.`); without an answer it goes on
  after 20 seconds with the cells the kernel ran.
- **It never changes your objects.** The model is run on the batch you give, without training it.
- **It never imports a heavy library.** NumPy, pandas, torch and the others are only looked at
  when your notebook has already imported them.
- **It stays quiet in worker processes.** A worker of `multiprocessing` or of a `DataLoader`,
  forked or spawned, that reaches a step (even `ck.start` at the top of a script) sends nothing
  and prints nothing.

## When the server cannot be reached

The library writes everything to the local file first and sends it second. So when the server
does not answer:

```
! START: no answer from the server. Working offline: everything is saved locally and sent when the connection is back.
! LOAD: no answer from the server yet. Saved locally; it will be sent again.
```

Keep working. Checks, readings and drills that got no answer are sent again in the background
(a check that arrives this way is marked as late, and the server counts every check once however
often it is sent). A submission and `ck.finish()` are not sent again by themselves: run the cell
again when the connection is back. After a connection that failed, the steps of the next ten
seconds do not wait for the server again; they are saved and queued at once.

`ck.export()` is for the case that the connection never comes back. Run it at the end and send
the file to your instructor:

```python
ck.export()
```

It writes `olympiad_export_<lab>_<student>_<utc>.json.gz` into the working folder (in Colab it
also offers the download) and returns the path. The file holds every record of the session,
including the submission files, so the lab can be scored later. The times in it are your
computer's, not the server's.

## Supported environments

- Python 3.9, 3.10, 3.11, 3.12 and 3.13.
- Google Colab, Kaggle notebooks (internet on), Jupyter (Notebook and Lab), VS Code notebooks and
  plain Python scripts. In a script there are no cells, so only the steps are recorded.
- IPython 7.34 and newer with ipykernel 6.17 and newer (the versions of Colab), up to the current
  releases.
- Evidence is built for plain Python objects, NumPy, pandas, torch, scikit-learn, Keras and
  Hugging Face objects, whichever of them the notebook uses.

## Settings

| Setting | Meaning |
|---|---|
| `ck.start(lab, student_id, server="https://...")` | Use another scorer server. |
| `OLYMPIAD_SERVER_URL` | The same, as an environment variable. The argument wins. |
| `OLYMPIAD_STUDENT_ID` | The student id for `ck.start(lab)` without one (automated runs). |
| `OLYMPIAD_AUDIT_DIR` | Audit mode, see below. |

Without a setting the library talks to the server it was written for,
`https://us-central1-ka-ai-olympiad.cloudfunctions.net/api`.

## Audit mode

With `OLYMPIAD_AUDIT_DIR` set to a folder, nothing touches the network. Every call is appended to
`audit.jsonl` in that folder as `{"route", "body", "ts"}`, the body exactly as it would have
been sent; submitted files are copied there as `submission_<n>_<stage>__<filename>` and the
submit body names the copy under `file` instead of carrying the content. Every step prints
`! LOAD recorded (audit mode)`. Instructors use it to run a handed-in notebook again and compare.

No start answer arrives in this mode. A tool that knows the lab can leave its public info in the
folder as `lab.json` (the `lab` object of a start answer); a table handed to `ck.submit` is then
written in the lab's file format and stages are spelled as the lab spells them. Without the file
a table is written as CSV.

## The local file, for tool authors

`olympiad_<lab>_<student>.jsonl` is append-only, one JSON object per line:

```
{"k": "event", "ts": "...Z", "sent": false, "session_id": "...", "boot": "a3f9c2d1", "event": {"seq": 12, "type": "cell_run", ...}}
{"k": "call",  "ts": "...Z", "sent": false, "session_id": "...", "id": "a3f9c2d1-c4", "route": "/v1/check", "body": {...}, "response": null}
{"k": "ack",   "ts": "...Z", "boot": "a3f9c2d1", "seq": [10, 12]}
{"k": "ack",   "ts": "...Z", "id": "a3f9c2d1-c4", "sent": true, "response": {...}}
```

The body of a check carries its `call_id`, the same on every retry. An `ack` line records what
happened later to earlier lines: a range of events the server accepted, or how a call ended.
`ck.export()` folds them in: the exported file is gzip-compressed JSON lines with `event` and
`call` lines only, where `sent` is true for what the server accepted and `response` holds its
answer (or its error). A kernel that was restarted appends to the same file under a new `boot`
id. A `finish` call is kept without the notebook while it is under way; only one that got no
answer is kept in full, as a second line.

The file may be renamed, moved or deleted while the notebook runs: the running kernel still
finds every line for `ck.export()`. A line that was cut off (a kernel that was killed while it
wrote) is skipped by every reader and never swallows the next one. Above 200 MB new cell records
are kept without their source (the notebook says so once), and a submission is kept in full only
while it has no answer.

## Development

```
python -m pip install -e . pytest numpy pandas ipython ipykernel nbclient
python -m pytest
```

The tests run against a small fake scorer on localhost (`tests/fake_server.py`) and cannot reach
the real one: the built-in address is replaced and sockets are confined to the local machine.

## Licence

MIT. See `LICENSE`.
