bugger.
All challenges/Pythonmediumgsoc-ignite-metric-state-v1
THE INVESTIGATION

The metric that forgets between reads

Repeated metric reads disagree, while an epoch reset keeps the previous epoch alive.

MLPyTorch-IgniteMetric lifecycle

What’s going wrong

Original ML practice for NumFOCUS / PyTorch-Ignite contribution preparation. This is not an upstream bug reproduction.

A small evaluator records a binary confusion matrix across batches. Calling compute twice should be harmless, and reset should begin a fresh epoch. Currently those operations change the wrong state. Repair the Metric lifecycle while preserving its counting and output formulas.

Read one JSON object {"events":[{"op":"update","truth":[0,1],"pred":[0,1]},{"op":"compute"}]} from stdin. There are 0 to 128 events. The only operations are update, compute and reset. An update has truth and pred arrays of equal length from 0 to 128; every entry is integer 0 or 1 and is already a class label, not a probability. Inputs are valid.

The metric initially contains zero counts. update adds one observation per corresponding pair. Rows of the 2 by 2 matrix are true labels and columns are predicted labels, so the matrix is [[TN,FP],[FN,TP]]. reset clears all counts. compute must not change counts and emits one snapshot. Print an array containing a snapshot for each compute event in input order; updates and resets do not emit results. A snapshot is {"matrix":[[TN,FP],[FN,TP]],"accuracy":value,"precision":value,"recall":value}. Accuracy is (TN+TP)/(TN+FP+FN+TP), precision is TP/(TP+FP), and recall is TP/(TP+FN). Round each ratio with Python round(value, 6), or use null for a zero denominator. With no compute events, print []. Each snapshot must retain its original matrix even when later updates or resets occur. Use only the Python standard library.

Expected behavior

Accumulate binary confusion counts across updates, let compute read without mutation, and clear all counts only on reset.

Reproduction

01 Repeated reads must keep the same epoch
{"events":[{"op":"update","truth":[0,1],"pred":[0,1]},{"op":"compute"},{"op":"compute"}]}
[{"matrix":[[1,0],[0,1]],"accuracy":1.0,"precision":1.0,"recall":1.0},{"matrix":[[1,0],[0,1]],"accuracy":1.0,"precision":1.0,"recall":1.0}]
02 A single computation is a passing control
{"events":[{"op":"update","truth":[0,1],"pred":[0,1]},{"op":"compute"}]}
[{"matrix":[[1,0],[0,1]],"accuracy":1.0,"precision":1.0,"recall":1.0}]
03 An empty evaluator has undefined ratios
{"events":[{"op":"compute"}]}
[{"matrix":[[0,0],[0,0]],"accuracy":null,"precision":null,"recall":null}]

Before you begin

  • The weighted-loss lesson and per-epoch metric state
  • Python classes and mutable nested lists
  • Binary confusion matrices and zero-denominator ratios

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.