File size: 9,167 Bytes
b0f9716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
b0f9716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
b0f9716
 
 
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
 
b0f9716
 
 
 
 
13e3d7a
 
 
 
b0f9716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
b0f9716
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
 
b0f9716
13e3d7a
 
 
 
 
 
 
 
 
b0f9716
 
 
 
13e3d7a
 
b0f9716
 
 
 
 
 
 
 
 
 
 
 
 
13e3d7a
b0f9716
 
 
 
 
 
 
 
 
 
c8490fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TransitPulse</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  :root { --ink: #1f2933; --muted: #52606d; --line: #d9e2ec; --accent: #d9480f; --bar: #4c72b0; --bg: #ffffff; --panel: #f5f7fa; --error: #b00020; }
  @media (prefers-color-scheme: dark) {
    :root { --ink: #e4e7eb; --muted: #9aa5b1; --line: #3e4c59; --accent: #ff8a4c; --bar: #7da2e0; --bg: #14181d; --panel: #1f262e; --error: #ff6b81; }
  }
  * { box-sizing: border-box; }
  body { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; max-width: 780px; margin: 0 auto; padding: 1.5rem 16px 3rem; color: var(--ink); background: var(--bg); line-height: 1.5; }
  h1 { margin: 0 0 0.2rem; font-size: 1.8rem; }
  .lead { margin: 0 0 1.2rem; color: var(--muted); }
  .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; }
  label { display: block; margin-top: 0.7rem; font-weight: 600; font-size: 0.92rem; }
  input, select, button { font: inherit; }
  input, select { padding: 0.45rem; width: 100%; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); }
  .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.7rem; }
  .check { display: flex; gap: 0.5rem; align-items: center; font-weight: 400; margin-top: 1rem; }
  .check input { width: auto; }
  button { margin-top: 1rem; padding: 0.6rem 1.4rem; border: 0; border-radius: 6px; background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; }
  button:disabled { opacity: 0.5; cursor: wait; }
  #error { margin-top: 1rem; color: var(--error); font-weight: 600; }
  #result { margin-top: 1.4rem; }
  .big { font-size: 2.4rem; font-weight: 700; line-height: 1.1; }
  .sub { color: var(--muted); }
  canvas { display: block; width: 100%; height: 260px; margin-top: 1rem; }
  .warn { margin-top: 0.6rem; padding: 0.5rem 0.75rem; border-left: 4px solid var(--accent); background: var(--panel); }
  footer { margin-top: 2rem; font-size: 0.9rem; color: var(--muted); }
  a { color: var(--accent); }
</style>
</head>
<body>
<h1>🚌 TransitPulse</h1>
<p class="lead">Predict hourly bus ridership for one MTA bus route in New York, to help planners decide where buses are needed.</p>

<div class="panel">
  <label for="route">Route</label>
  <select id="route" disabled><option>Loading the model…</option></select>

  <div class="grid">
    <div><label for="date">Date</label><input id="date" type="date"></div>
    <div><label for="hour">Hour (0-23)</label><input id="hour" type="number" min="0" max="23" step="1" value="8"></div>
  </div>

  <label class="check"><input type="checkbox" id="customWeather"> Enter the weather myself (otherwise: recorded weather, or Open-Meteo's forecast for the next 16 days)</label>
  <div class="grid" id="weatherBox" hidden>
    <div><label for="temperature_2m">Temperature (°C)</label><input id="temperature_2m" type="number" step="0.1" value="15"></div>
    <div><label for="precipitation">Rain (mm/h)</label><input id="precipitation" type="number" step="0.1" value="0"></div>
    <div><label for="snowfall">Snow (cm/h)</label><input id="snowfall" type="number" step="0.1" value="0"></div>
    <div><label for="wind_speed_10m">Wind (km/h)</label><input id="wind_speed_10m" type="number" step="0.1" value="10"></div>
  </div>

  <button id="go" disabled>Predict</button>
  <div id="error" role="alert"></div>
</div>

<div id="result" hidden>
  <div class="sub" id="resultLabel"></div>
  <div class="big" id="resultValue"></div>
  <div class="sub" id="resultRange"></div>
  <div id="warnings"></div>
  <canvas id="chart" aria-label="Predicted riders for each hour of the day"></canvas>
  <div class="sub">Predicted riders for every hour of that day; the chosen hour is highlighted.</div>
</div>

<footer>
  <p><strong>Limits:</strong> the model only knows the 24 routes it was trained on, and it learned from January to September 2025.
  Later dates use Open-Meteo weather (forecasts reach 16 days ahead) and are rough guesses, especially for March and October to December, which the model never saw.
  Typical error on the test period was about 65 riders per route-hour (about 14.5% of an average hour); the ± range uses the validation error (56 riders).
  Weather data by <a href="https://open-meteo.com/">Open-Meteo.com</a> (CC BY 4.0).</p>
  <p>Gradient Boosting model trained on MTA bus ridership, MTA schedules and Open-Meteo weather.
  The same model runs here in your browser: its 500 trees were exported from scikit-learn, and the predictions were checked to match the Python model.</p>
  <p><a href="https://huggingface.co/ayanjawed/transitpulse-planning-model">Model card</a> ·
  <a href="https://github.com/ayanmirza99/ML-Project">Code</a> ·
  <a href="https://ml-project-wuwhhjv3aqsnuxh5x5tdwi.streamlit.app/">Streamlit version</a> ·
  CT-378 Machine Learning course project</p>
</footer>

<script src="predict.js"></script>
<script>
const $ = (id) => document.getElementById(id);
const WEATHER_FIELDS = ["temperature_2m", "precipitation", "snowfall", "wind_speed_10m"];
let model = null, data = null;

function showError(message) {
  $("error").textContent = message;
  $("result").hidden = true;
}

function drawChart(profile, chosenHour) {
  const canvas = $("chart");
  const ratio = window.devicePixelRatio || 1;
  const width = canvas.clientWidth, height = canvas.clientHeight;
  canvas.width = width * ratio;
  canvas.height = height * ratio;
  const ctx = canvas.getContext("2d");
  ctx.scale(ratio, ratio);
  const styles = getComputedStyle(document.documentElement);
  const color = (name) => styles.getPropertyValue(name).trim();

  const left = 44, bottom = 22, top = 8;
  const plotHeight = height - bottom - top;
  const max = Math.max(...profile.map((p) => p.predicted_riders), 1);
  const slot = (width - left) / 24;

  ctx.font = "11px system-ui, sans-serif";
  ctx.fillStyle = color("--muted");
  ctx.strokeStyle = color("--line");
  ctx.textAlign = "right";
  [0, 0.5, 1].forEach((f) => {
    const y = top + plotHeight * (1 - f);
    ctx.beginPath(); ctx.moveTo(left, y); ctx.lineTo(width, y); ctx.stroke();
    ctx.fillText(Math.round(max * f).toLocaleString(), left - 6, y + 4);
  });
  ctx.textAlign = "center";
  profile.forEach((p) => {
    const h = (p.predicted_riders / max) * plotHeight;
    ctx.fillStyle = p.hour === chosenHour ? color("--accent") : color("--bar");
    ctx.fillRect(left + p.hour * slot + 2, top + plotHeight - h, Math.max(slot - 4, 1), h);
    if (p.hour % 3 === 0) {
      ctx.fillStyle = color("--muted");
      ctx.fillText(String(p.hour), left + p.hour * slot + slot / 2, height - 6);
    }
  });
}

async function predict() {
  $("error").textContent = "";
  const hourText = $("hour").value.trim();
  const hour = /^-?\d+$/.test(hourText) ? Number(hourText) : NaN;
  let weather = null;
  if ($("customWeather").checked) {
    weather = {};
    for (const field of WEATHER_FIELDS) {
      const text = $(field).value.trim();
      weather[field] = text === "" ? NaN : Number(text);
    }
  }
  try {
    $("go").disabled = true;
    const result = await TransitPulse.predict(model, data, $("route").value, $("date").value, hour, weather);
    $("resultLabel").textContent = `Predicted riders on ${result.route} at ${result.hour}:00 on ${result.date}`;
    $("resultValue").textContent = `${result.predicted_riders.toLocaleString()} ± ${result.typical_error}`;
    $("resultRange").textContent = `Likely between ${result.likely_range[0].toLocaleString()} and ${result.likely_range[1].toLocaleString()} riders. Weather used: ${result.weather_source}.`;
    $("warnings").innerHTML = "";
    for (const text of result.warnings) {
      const box = document.createElement("div");
      box.className = "warn";
      box.textContent = text;
      $("warnings").appendChild(box);
    }
    $("result").hidden = false;
    drawChart(result.daily_profile, result.hour);
  } catch (error) {
    showError(error instanceof TransitPulse.InputError ? error.message : "Something went wrong: " + error.message);
  } finally {
    $("go").disabled = false;
  }
}

Promise.all([fetch("model.json"), fetch("data.json")])
  .then((responses) => Promise.all(responses.map((r) => { if (!r.ok) throw new Error(r.statusText); return r.json(); })))
  .then(([loadedModel, loadedData]) => {
    model = loadedModel;
    data = loadedData;
    $("route").innerHTML = data.route_names.map((r) => `<option>${r}</option>`).join("");
    $("route").value = data.route_names[0];
    $("route").disabled = false;
    $("date").min = data.first_date;
    $("date").max = data.last_date;
    $("date").value = TransitPulse.defaultDate();
    $("go").disabled = false;
    predict();
  })
  .catch((error) => showError("Could not load the model files: " + error.message));

$("customWeather").addEventListener("change", (e) => { $("weatherBox").hidden = !e.target.checked; });
$("go").addEventListener("click", predict);
window.addEventListener("resize", () => { if (!$("result").hidden) predict(); });
</script>
</body>
</html>