Case study · Medical device company

Turning sensor peaks into an amount of tissue

With a team of three, I built a program that reads very large recordings from a sensor on a surgical suction tube, finds the flashes of light from fluorescent tissue, and turns the peak count into an estimate of how much material was removed.

  • 26 sto analyse a 7.6 GB, 4 h 41 min recording
  • 45 of 45reference pieces found
  • 403unit tests, plus 176 golden checks

Challenge

  • The sensor records 50,000 samples per second, so one four-hour recording is about 6 GB, too big to open or inspect by hand.
  • The peaks are only 3 to 10 ms wide, the base level drifts, and some peaks overlap.
  • A piece that sticks near the sensor makes a flat raised stretch, called a plateau, which looks like hundreds of peaks to a simple detector.
  • It was not known how well a peak count follows the real amount, or whether one calibration would work for every material.

What I built

  • A fast reader that converts each recording once into a memory-mapped binary copy, so a 7.6 GB file opens in about a second afterwards.
  • A detection pipeline with a rolling-median baseline, smoothing, plateau removal, and merging of overlapping peaks into peak groups.
  • A calibration line through zero for each material, which turns peak groups per suction pass into an amount, with a likely range.
  • Leave-one-out tests, so every accuracy figure comes from a recording the line had not seen.
  • Checks that warn when a new recording looks different from the calibration recordings.
  • A one-page web viewer for looking at every detection, estimating volumes and predicting tumour size.
  • A research study of ten other measures of size, run with a frozen procedure and reported openly, including what went wrong in the procedure.

Stack

Python, NumPy, SciPy, pandas, Matplotlib, memory-mapped binary files, a local web page with Plotly charts, unit tests and golden checks, Git and GitHub, LaTeX for the report.

System design

The program is a batch pipeline: each stage takes the previous stage's output and can be tested on its own.

A batch pipeline: each stage takes the previous stage's output and can be tested on its ownRaw recordingConvert onceflat binary array, metadatafileOpen by memory mappingNumPyProcess in overlappingchunksRemove driftrolling medianSmoothlow-pass filterRemove plateausDetect and groupSciPy, peak groupsCount per suction passCalibratea line through zeroCalibration recordingsValidateleave-one-outGuard against driftReportlocal HTML page, Plotly
  1. Convert once: the raw recording is parsed a single time into a flat binary array of samples, with a small metadata file beside it (sample rate, channels, start time).
  2. Open by memory mapping: later runs open the binary copy with NumPy's memory mapping. The operating system loads only the parts being read, which is why a 7.6 GB file opens in about a second and a full analysis stays at about 2 GB of memory.
  3. Process in overlapping chunks: the signal is read in windows that overlap by more than the baseline window and the widest peak, so no peak is cut at a chunk edge. Results from the overlaps are merged so nothing is counted twice.
  4. Remove drift: a rolling median of the signal is taken as the baseline and subtracted.
  5. Smooth: a light low-pass filter reduces noise without flattening peaks that are only 3 to 10 ms wide.
  6. Remove plateaus: stretches that stay raised far longer than any real peak are detected and masked, so a stuck piece is not counted as hundreds of peaks.
  7. Detect and group: peaks are found with SciPy using height, prominence and width limits, then peaks that overlap or sit very close together are merged into peak groups.
  8. Count per suction pass: the recording is split into suction passes, and peak groups are counted in each pass.
  9. Calibrate: for each material, a line through zero links peak groups to amount. Its slope comes from the calibration recordings, and the spread of their errors gives the likely range around each estimate.
  10. Validate: leave-one-out testing refits the line without each recording in turn, then predicts that recording, so every accuracy figure comes from unseen data.
  11. Guard against drift: a new recording's noise level, baseline and peak widths are compared with the calibration set, and the program warns when they fall outside it.
  12. Report: results are written to files that a single local HTML page with Plotly reads, so every detection can be checked by eye without a server.

Unit tests cover each stage, and golden checks compare full outputs on reference recordings against stored expected results, so any change that shifts a number is caught.

Results

  • Speed: a 7.6 GB, 4 h 41 min recording analysed in about 26 s, which is about 650 times faster than real time, using about 2 GB of memory
  • Detection: 45 of 45 reference pieces found, with no false detections in the empty groups
  • Accuracy: typical error for a whole recording of 0.07 ml for the high-concentration phantoms, 0.12 ml for the middle ones, and 0.17 g for tumour samples
  • Hidden sample: a 2 g tumour sample left out of the calibration was estimated as 2.0 g (one sample, so an example and not a proof)
  • Quality: 403 unit tests and 176 golden checks, in a public repository with an example data set
  • An honest finding: a line made on phantoms read tumours about 37% low, and none of ten other measures fitted all materials, so each material needs its own calibration
Back to Work & Research