apopt.comCBE 493R Data-Driven Athletics

Course / Tutorials / Week 12 / Q-34

Quick tutorial Q-34 · Week 12 · about 15 min · Foundation

A calculation in a web page

Write the foot-plant force formula in Python and in JavaScript, run both on three hand-checked cases, and confirm they give the same numbers.

  1. 1Gather
  2. 2Statistics
  3. 3Visualize
  4. 4Cleanse
  5. 5Features
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy (used)
  1. Ask
  2. Collect
  3. Check
  4. Interpret
  5. Coach (this week)
  6. Reassess

2 of 3 cells run in this page. The rest need a model that does not run in the browser: run them in the notebook on your computer or in Colab (File → Upload notebook); their output from the course build is shown, and the files they write are published so the later cells still run here.

The decision

Before a calculation goes into a page an athlete will open, check that the page's JavaScript gives the same numbers as the Python you trusted, on cases whose answers you know.

The raw data

Try it

A calculation reaches an athlete when it runs in a page on their phone. The Python cells on this page already run in your browser: they use Pyodide, a version of Python compiled to WebAssembly, so nothing is sent to a server. A page an athlete opens, though, is usually written in JavaScript, the browser's own language, and loads in a fraction of a second. So the same calculation is written twice, and the two must agree.

The calculation is the force of a foot plant from its timing (Section 3.7.2): over a step the ground's push must average one body weight, and with a half-sine shape the peak is

F_peak / body weight = (π/2) × (1 + flight time / contact time).

The Python version, on three test cases

The three cases were typed before any code was written, from the book's examples, so each has a hand answer to check against.

PythonThe function in Python

Type a prediction to enable Run.

Show the expected result without running
       case  contact_s  flight_s  python_bw
book runner       0.25     0.099     2.1928
course clip       0.20     0.150     2.7489
     sprint       0.10     0.120     3.4558

The same function in JavaScript

This is what goes in the page. Read it line by line against the Python: Math.PI is math.pi, and export lets the page and a test file import it.

// force.js: peak force of a foot plant, in body weights (Section 3.7.2)
export function peakForceBW(contactS, flightS) {
  return (Math.PI / 2) * (1 + flightS / contactS);
}

Run the JavaScript on the same cases

This cell runs the JavaScript with Node.js, the JavaScript engine for a computer, so it is marked to run on your computer, not in the page. It writes the answers to a file that the next cell, back in the browser, compares with Python.

Run force.js with Node.js

Runs in the notebook (needs a model that does not run in the browser). Output from the course build:

import subprocess, os

JS = """export function peakForceBW(contactS, flightS) {
  return (Math.PI / 2) * (1 + flightS / contactS);
}"""
CHECK = """import { readFileSync, writeFileSync } from 'node:fs';
import { peakForceBW } from './force.mjs';
const rows = readFileSync('raw/test_cases.csv', 'utf8').trim().split('\\n').slice(1);
const out = rows.map((r) => { const [name, c, f] = r.split(',');
  return `${name},${peakForceBW(Number(c), Number(f))}`; });
writeFileSync('clean/force_js.csv', 'case,js_bw\\n' + out.join('\\n') + '\\n');"""
os.makedirs("clean", exist_ok=True)
open("force.mjs", "w").write(JS + "\n")
open("check_force.mjs", "w").write(CHECK + "\n")
run = subprocess.run(["node", "check_force.mjs"], capture_output=True, text=True)
print("node exit code", run.returncode, run.stderr.strip())
print(open("clean/force_js.csv").read().strip())
node exit code 0 
case,js_bw
book runner,2.1928316722056755
course clip,2.748893571891069
sprint,3.455751918948773

Do they agree?

PythonCompare the two, and draw the curve

Type a prediction to enable Run.

Show the expected result without running
       case  python_bw    js_bw  difference
book runner   2.192832 2.192832         0.0
course clip   2.748894 2.748894         0.0
     sprint   3.455752 3.455752         0.0
largest difference: 0.0
Peak force in body weights against the ratio of flight to contact time; a solid line for the Python function and open squares for the JavaScript answers on three cases, which sit on the line.

Both languages use the same 64-bit floating-point numbers, so for a formula this simple the answers agree to every printed digit. For longer calculations they can differ in the last digit or two, so tests compare with a tolerance, such as 0.0001 body weights, not with ==.

For the athlete: "The page on your phone uses the same formula we checked here: with a 0.25 s contact and a 0.1 s flight, each foot plant pushes about 2.2 times your body weight."

Check yourself

  1. Where do the Python cells on this page run?

  2. Why compare the two versions on cases typed before the code was written?

  3. Contact 0.20 s, flight 0.15 s. What peak force does the formula give, in body weights?

    body weights

0 of 3 answered · 0 right the first time

References

Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.