Metadata-Version: 2.4
Name: whowhen
Version: 0.2.1
Summary: Audit panel data for identity and timing signals a model can exploit without predicting anything
Author: Andrew Hendel
License: Apache-2.0
Project-URL: Repository, https://github.com/mojolearn/whowhen
Keywords: data leakage,evaluation,panel data,dataset audit,time series
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
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: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: joblib>=1.3
Requires-Dist: threadpoolctl>=3.1
Requires-Dist: mojolearn>=0.8.66; (sys_platform == "darwin" and platform_machine == "arm64") or (sys_platform == "linux" and platform_machine == "x86_64")
Requires-Dist: catboost>=1.2; (sys_platform != "darwin" or platform_machine != "arm64") and (sys_platform != "linux" or platform_machine != "x86_64")
Provides-Extra: parquet
Requires-Dist: pyarrow>=14.0; extra == "parquet"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: catboost>=1.2; extra == "test"
Requires-Dist: pyarrow>=14.0; extra == "test"
Dynamic: license-file

# whowhen

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24406266.svg)](https://doi.org/10.5281/zenodo.24406266)

Check a table of repeated observations per unit (patients, machines,
loans, users, vessels) for two signals a model can exploit without
predicting anything:

- **who**: can a row be matched to the unit it came from?
- **when**: can two rows of one unit be put in time order?

If the answer to either is yes, a model evaluated on your data may be
scoring well by recognizing units or reading the clock.

## Install

```
pip install whowhen
```

## Use

From the terminal, give a CSV file and the names of its columns:

```bash
whowhen rows.csv --unit patient_id --time hour --event sepsis
```

If the rows are not ordered in time (several samples per patient,
several measurements per batch), leave the time column out:

```bash
whowhen samples.csv --unit patient_id --event tumour
```

In Python, pass a pandas DataFrame (your data table):

```python
import pandas as pd
import whowhen as ww

df = pd.read_parquet("stays.parquet")
print(ww.report(df, unit="patient_id", time="hour", event="sepsis"))
print(ww.report(samples, unit="patient_id", event="tumour"))   # no time order
```

Both interfaces run the same checks.

| Setting | Required | Default | What it is |
|---|---|---|---|
| `df` | yes | | the table, one row per observation |
| `unit` | yes | | the column naming the unit |
| `time` | no | none | the column giving when the row was observed; leave it out if the rows are not ordered in time |
| `event` | no | none | a column that is nonzero where the event happens |
| `features` | no | every numeric column | the columns to check: a list, or a dict of named lists to compare side by side |
| `horizon` | no | none | for forecasting tasks: how far ahead the event is predicted, in the time column's scale (`12`, or `"12h"` for datetimes) |
| `repeats` | no | 4 | how many times every check is run |
| `random` | no | `False` | draw held-out units at random instead of the fixed groups |
| `n_pairs` | no | 20000 | the most row pairs each run uses; smaller tables use what they have |

What runs depends on what you give:

| You give | You get |
|---|---|
| `unit` only | the identity audit, the individual-or-type test, the per-column ranking for identity |
| `unit` and `event` | plus the overlap check |
| `unit` and `time` | the identity and timing audits, the timing placebo, the individual-or-type test, the audits by short and long gap, the per-column ranking |
| plus `event` | the overlap check, the chronological control, the time-cut split, the clock baseline |
| plus `horizon` | those three run as a forecast: "does the event come within this long after the row?" |

A check that cannot run is listed at the end of the report with the
reason.

To compare choices made before modeling, such as raw values against a
carried-forward or imputed version, put both versions in the table and
name them. Every check then runs once per named set, the audits on the
same pairs and the event checks on the same splits:

```python
ww.report(df, unit="patient_id", time="hour",
          features={"raw": raw_cols, "carried_forward": filled_cols})
```

To see the library work before pointing it at your own data, run it on
a synthetic table where the right answers are known:

```
whowhen demo
```

## What the report contains

**Identity audit and timing audit.** Both train a classifier on pairs
of rows from units with no recorded event, built so that the two
classes look the same unless the signal is there. The gap between the
two rows of a pair is chosen from the data and stated in the report.

| Audit | The pair | Label | AUC 0.5 means |
|---|---|---|---|
| identity | two rows a set time apart | same unit, or two different units seen at the same two times | rows carry nothing that says which unit produced them |
| timing | two rows of one unit a set time apart | which row is the later one | a unit's rows carry nothing that says when they were recorded |

Units are split into training and held-out sides before any pair is
drawn, so the AUC is measured on units the classifier never saw. The
interval comes from resampling held-out units, pooled over repeats.

**Timing placebo.** The timing audit again, with the later row taken
from a different unit observed at that time. If this score is near 0.5,
the timing signal is each unit's own history. If it is near the timing
audit's score, the signal is change shared by all units, such as
calendar drift or a change in how every unit is recorded.

**Individual or type.** The identity audit is reported twice. With
`random` impostors the different unit is any other unit, so a high
score can mean "these two rows are from different kinds of unit" (a
tug and a tanker). With `nearest` impostors the different unit is one
of the units most similar to the first, by per-unit column averages.
If the score stays high, individual units are recognizable. If it
falls toward 0.5, the columns identify the kind of unit, not the
individual.

**Short and long gaps.** Both audits again, once with the two rows of
a pair in the shorter half of the gap range and once in the longer
half. If identity stays high at the long gap, units stay recognizable
as time passes.

**Columns.** For each column: the audit using that column alone, and
how much the full score drops when that column is removed.

**Overlap check.** Needs `event`. It scores the same test rows twice
with the same number of training rows per class: once with the test
units held out of training, once with other rows of those units
swapped in. The gap between the two scores is how much sharing units
between training and test is worth to this classifier on your task.

**Chronological control.** The same check, testing the later half of
each test unit's rows and letting training see the earlier half: does
a unit's own past help predict its own future?

**Time-cut split.** The same check with a single cut in the time
column: test rows come after it, training rows before it. When the
time column is a shared calendar, this is the usual split by date, and
the gap shows how much unit overlap such a split leaves in place.

**Clock baseline.** Many event tasks can be scored well above chance
from one number: how long the unit has been observed. This fits the
same classifier on the clock alone, on your columns alone, and on
both, with units held out, and reports what the columns add once the
clock is known.

## What the numbers do not mean

An audit AUC above 0.5 says the signal is present in the columns. It
does not say your model uses it.

The overlap check does not say so either. It measures what unit overlap
is worth to whowhen's classifier, on your columns and your outcome,
under the split it names. A gap there shows the evaluation is sensitive
to unit overlap. To learn what your own model gains, train it both
ways.

**An AUC near 0.5 does not certify an evaluation.** It says this
classifier, on these columns, found nothing. Leakage through the
label, through preprocessing fitted on test rows, or through columns
not in the table is outside what these checks see.

## When it applies

- many units (hundreds or more), each observed more than once;
- for the identity audit, several units observed at the same time
  values, since the different-unit pair is matched on time;
- without a time column, a pair is two rows of one unit or one row each
  from two units, with no matching on time. If the rows do have an
  order, give the time column: unmatched pairs can also be told apart
  by anything that changed between the times the units were observed;
- with `horizon`, an event column flagged on the row where the event
  happens, not on every row of the unit.

With `horizon`, a unit's last row is taken as the end of its follow-up.
A row of a unit with no event counts as "no event within the horizon"
only if the unit was still observed one horizon later; other such rows
are left out, and the report says how many. Under the chronological and
time-cut splits, a row from before the cut is trained on only if its
horizon has passed by the cut, so no label is used before it could
have been known. If your table ends a unit's rows before its follow-up
truly ends, extend the rows or shorten the horizon.

It does not apply to tables with one row per example and no repeated
unit.

## Repeats and repeatability

`repeats=N` is one number: every check runs N times. Units fall into
N + 1 fixed groups, by a hash of the unit identifier, and each run
holds out a different group:

| `repeats` | Each run tests |
|---|---|
| 1 | one half of the units |
| 2 | one third |
| 3 | one quarter |
| 4 (default) | one fifth |

More repeats give a steadier answer, but each run then tests a smaller
share of the units, so very high values suit only large tables.

By default the same call on the same table gives the same numbers
every time. `random=True` draws the held-out units and the pairs at
random instead, differently on each run, and the report states the
seed that run used.

## The classifier

The checks use [mojolearn](https://github.com/mojolearn/mojolearn)'s
`GradientBoosting` with oblivious (symmetric) trees. mojolearn ships
wheels for macOS on Apple silicon and Linux on x86-64. On other
platforms, including Windows, CatBoost is installed and used instead,
with the same settings. The report names the whowhen version and DOI,
and the classifier that ran with its version and, where the classifier
declares one, its DOI.

Independent runs are spread across all cores. Set the `WHOWHEN_JOBS`
environment variable to use fewer.

## Command line

```
whowhen rows.parquet --unit patient_id --time hour --event sepsis
whowhen demo
```

CSV and Parquet files are supported. Use `whowhen --help` for report
options or `whowhen demo --help` for demo options.

To use a report in a script or a scheduled job, say what counts as a
failure:

```
whowhen rows.parquet --unit patient_id --time hour --event sepsis \
    --fail-if "overlap_gap > 0.02" --fail-if "clock_gain < 0.01"
```

The command exits with 1 if any condition holds, and with 2 if a
condition names a check that did not run. You choose the conditions;
there are no built-in thresholds.

| Name | The number |
|---|---|
| `identity`, `identity_nearest` | identity AUC against any other unit, and against the most similar units |
| `timing`, `placebo` | timing AUC, and its placebo |
| `overlap_gap`, `chronological_gap`, `time_cut_gap` | overlap AUC minus disjoint AUC under each split |
| `clock` | AUC of the clock alone |
| `clock_gain` | what the columns add once the clock is known |

With several named feature sets, each number is the one furthest from
a clean result: the highest AUC, the highest gap, the lowest clock
gain. A high identity AUC is normal for many tables and is not
by itself a failure; the gaps say whether it matters for your task.

## Citing

The method is described in:

> Andrew Hendel. Auditing Identity and Timing Signals in Operational
> Logs. 2026. Under review.

The library is archived on Zenodo. This DOI always points to the newest
version: [10.5281/zenodo.24406266](https://doi.org/10.5281/zenodo.24406266).

See `CITATION.cff`.

## License

Apache License 2.0. See `LICENSE`.
