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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use std::cmp::Ordering;
use std::fmt::Display;

use abstutil::{abbreviated_format, prettyprint_usize, CloneableAny};
use geom::{Angle, Distance, Duration, Line, Polygon, Pt2D, Time};
use map_gui::tools::ColorScale;
use sim::{Problem, TripID, TripMode};
use widgetry::{
    ClickOutcome, Color, DrawWithTooltips, GeomBatch, GeomBatchStack, StackAlignment, Text, Widget,
};

use crate::{App, EventCtx};

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ProblemType {
    IntersectionDelay,
    ComplexIntersectionCrossing,
    OvertakeDesired,
    ArterialIntersectionCrossing,
}

impl From<&Problem> for ProblemType {
    fn from(problem: &Problem) -> Self {
        match problem {
            Problem::IntersectionDelay(_, _) => Self::IntersectionDelay,
            Problem::ComplexIntersectionCrossing(_) => Self::ComplexIntersectionCrossing,
            Problem::OvertakeDesired(_) => Self::OvertakeDesired,
            Problem::ArterialIntersectionCrossing(_) => Self::ArterialIntersectionCrossing,
        }
    }
}

impl ProblemType {
    pub fn count(self, problems: &[(Time, Problem)]) -> usize {
        let mut cnt = 0;
        for (_, problem) in problems {
            if self == ProblemType::from(problem) {
                cnt += 1;
            }
        }
        cnt
    }

    pub fn all() -> Vec<ProblemType> {
        vec![
            ProblemType::IntersectionDelay,
            ProblemType::ComplexIntersectionCrossing,
            ProblemType::OvertakeDesired,
            ProblemType::ArterialIntersectionCrossing,
        ]
    }
}

pub trait TripProblemFilter {
    fn includes_mode(&self, mode: &TripMode) -> bool;
    fn include_no_changes(&self) -> bool;

    // Returns:
    // 1) trip ID
    // 2) trip duration after changes
    // 3) difference in number of matching problems, where positive means MORE problems after
    //    changes
    fn trip_problems(
        &self,
        app: &App,
        problem_type: ProblemType,
    ) -> Vec<(TripID, Duration, isize)> {
        let before = app.prebaked();
        let after = app.primary.sim.get_analytics();
        let empty = Vec::new();

        let mut points = Vec::new();
        for (id, _, time_after, mode) in after.both_finished_trips(app.primary.sim.time(), before) {
            if self.includes_mode(&mode) {
                let count_before = problem_type
                    .count(before.problems_per_trip.get(&id).unwrap_or(&empty))
                    as isize;
                let count_after =
                    problem_type.count(after.problems_per_trip.get(&id).unwrap_or(&empty)) as isize;
                if !self.include_no_changes() && count_after == count_before {
                    continue;
                }
                points.push((id, time_after, count_after - count_before));
            }
        }
        points
    }

    fn finished_trip_count(&self, app: &App) -> usize {
        let before = app.prebaked();
        let after = app.primary.sim.get_analytics();

        let mut count = 0;
        for (_, _, _, mode) in after.both_finished_trips(app.primary.sim.time(), before) {
            if self.includes_mode(&mode) {
                count += 1;
            }
        }
        count
    }
}

lazy_static::lazy_static! {
    static ref CLEAR_COLOR_SCALE: ColorScale = ColorScale(vec![Color::CLEAR, Color::CLEAR]);
}

/// The caller should handle Outcome::ClickCustom with Vec<TripID> for clicked cells.
pub fn problem_matrix(
    ctx: &mut EventCtx,
    app: &App,
    trips: Vec<(TripID, Duration, isize)>,
) -> Widget {
    let duration_buckets = vec![
        Duration::ZERO,
        Duration::minutes(5),
        Duration::minutes(15),
        Duration::minutes(30),
        Duration::hours(1),
        Duration::hours(2),
    ];

    let num_buckets = 7;
    let mut matrix = Matrix::new(duration_buckets, bucketize_isizes(num_buckets, &trips));
    for (id, x, y) in trips {
        matrix.add_pt(id, x, y);
    }
    matrix.draw(
        ctx,
        app,
        MatrixOptions {
            total_width: 600.0,
            total_height: 600.0,
            color_scale_for_bucket: Box::new(|app, _, n| match n.cmp(&0) {
                std::cmp::Ordering::Equal => &CLEAR_COLOR_SCALE,
                std::cmp::Ordering::Less => &app.cs.good_to_bad_green,
                std::cmp::Ordering::Greater => &app.cs.good_to_bad_red,
            }),
            fmt_y_axis: Box::new(|lower_bound: isize, upper_bound: isize| -> Text {
                if lower_bound + 1 == upper_bound {
                    Text::from(lower_bound.abs().to_string())
                } else if lower_bound.is_negative() {
                    Text::from(format!("{}-{}", upper_bound.abs() + 1, lower_bound.abs()))
                } else {
                    Text::from(format!("{}-{}", lower_bound.abs(), upper_bound.abs() - 1))
                }
            }),
            tooltip_for_bucket: Box::new(|(t1, t2), (problems1, problems2), count| {
                let trip_string = if count == 1 {
                    "1 trip".to_string()
                } else {
                    format!("{} trips", prettyprint_usize(count))
                };
                let duration_string = match (t1, t2) {
                    (None, Some(end)) => format!("shorter than {}", end),
                    (Some(start), None) => format!("longer than {}", start),
                    (Some(start), Some(end)) => format!("between {} and {}", start, end),
                    (None, None) => {
                        unreachable!("at least one end of the duration range must be specified")
                    }
                };
                let mut txt = Text::from(format!("{} {}", trip_string, duration_string));
                txt.add_line(match problems1.cmp(&0) {
                    std::cmp::Ordering::Equal => {
                        "had no change in the number of problems encountered.".to_string()
                    }
                    std::cmp::Ordering::Less => {
                        if problems1.abs() == problems2.abs() + 1 {
                            if problems1.abs() == 1 {
                                "encountered 1 fewer problem.".to_string()
                            } else {
                                format!("encountered {} fewer problems.", problems1.abs())
                            }
                        } else {
                            format!(
                                "encountered {}-{} fewer problems.",
                                problems2.abs() + 1,
                                problems1.abs()
                            )
                        }
                    }
                    std::cmp::Ordering::Greater => {
                        if problems1 == problems2 - 1 {
                            if problems1 == 1 {
                                "encountered 1 more problems.".to_string()
                            } else {
                                format!("encountered {} more problems.", problems1,)
                            }
                        } else {
                            format!("encountered {}-{} more problems.", problems1, problems2 - 1)
                        }
                    }
                });
                txt
            }),
        },
    )
}

/// Aka a 2D histogram. Tracks matching IDs in each cell.
struct Matrix<ID, X, Y> {
    entries: Vec<Vec<ID>>,
    buckets_x: Vec<X>,
    buckets_y: Vec<Y>,
}

impl<
        ID: 'static + CloneableAny + Clone,
        X: Copy + PartialOrd + Display,
        Y: Copy + PartialOrd + Display,
    > Matrix<ID, X, Y>
{
    fn new(buckets_x: Vec<X>, buckets_y: Vec<Y>) -> Matrix<ID, X, Y> {
        Matrix {
            entries: std::iter::repeat_with(Vec::new)
                .take(buckets_x.len() * buckets_y.len())
                .collect(),
            buckets_x,
            buckets_y,
        }
    }

    fn add_pt(&mut self, id: ID, x: X, y: Y) {
        // Find its bucket
        // TODO Unit test this
        let x_idx = self
            .buckets_x
            .iter()
            .position(|min| *min > x)
            .unwrap_or(self.buckets_x.len())
            - 1;
        let y_idx = self
            .buckets_y
            .iter()
            .position(|min| *min > y)
            .unwrap_or(self.buckets_y.len())
            - 1;
        let idx = self.idx(x_idx, y_idx);
        self.entries[idx].push(id);
    }

    fn idx(&self, x: usize, y: usize) -> usize {
        // Row-major
        y * self.buckets_x.len() + x
    }

    fn draw(mut self, ctx: &mut EventCtx, app: &App, opts: MatrixOptions<X, Y>) -> Widget {
        let mut grid_batch = GeomBatch::new();
        let mut tooltips = Vec::new();
        let cell_width = opts.total_width / (self.buckets_x.len() as f64);
        let cell_height = opts.total_height / (self.buckets_y.len() as f64);
        let cell = Polygon::rectangle(cell_width, cell_height);

        let max_count = self.entries.iter().map(|list| list.len()).max().unwrap() as f64;

        for x in 0..self.buckets_x.len() - 1 {
            for y in 0..self.buckets_y.len() - 1 {
                let is_first_xbucket = x == 0;
                let is_last_xbucket = x == self.buckets_x.len() - 2;
                let is_middle_ybucket = y + 1 == self.buckets_y.len() / 2;
                let idx = self.idx(x, y);
                let count = self.entries[idx].len();
                let color = if count == 0 {
                    widgetry::Color::CLEAR
                } else {
                    let density_pct = (count as f64) / max_count;
                    (opts.color_scale_for_bucket)(app, self.buckets_x[x], self.buckets_y[y])
                        .eval(density_pct)
                };
                let x1 = cell_width * (x as f64);
                let y1 = cell_height * (y as f64);
                let rect = cell.clone().translate(x1, y1);
                grid_batch.push(color, rect.clone());
                grid_batch.append(
                    Text::from(if count == 0 && is_middle_ybucket {
                        "-".to_string()
                    } else {
                        abbreviated_format(count)
                    })
                    .change_fg(if count == 0 || is_middle_ybucket {
                        ctx.style().text_primary_color
                    } else {
                        Color::WHITE
                    })
                    .render(ctx)
                    .centered_on(Pt2D::new(x1 + cell_width / 2.0, y1 + cell_height / 2.0)),
                );

                if count != 0 || !is_middle_ybucket {
                    tooltips.push((
                        rect,
                        (opts.tooltip_for_bucket)(
                            (
                                if is_first_xbucket {
                                    None
                                } else {
                                    Some(self.buckets_x[x])
                                },
                                if is_last_xbucket {
                                    None
                                } else {
                                    Some(self.buckets_x[x + 1])
                                },
                            ),
                            (self.buckets_y[y], self.buckets_y[y + 1]),
                            count,
                        ),
                        if count != 0 {
                            Some(ClickOutcome::Custom(Box::new(std::mem::take(
                                &mut self.entries[idx],
                            ))))
                        } else {
                            None
                        },
                    ));
                }
            }
        }
        {
            let bottom = cell_height * (self.buckets_y.len() - 1) as f64;
            let right = cell_width * (self.buckets_x.len() - 1) as f64;

            let border_lines = vec![
                Line::must_new(Pt2D::zero(), Pt2D::new(right, 0.0)),
                Line::must_new(Pt2D::new(right, 0.0), Pt2D::new(right, bottom)),
                Line::must_new(Pt2D::new(right, bottom), Pt2D::new(0.0, bottom)),
                Line::must_new(Pt2D::new(0.0, bottom), Pt2D::zero()),
            ];
            for line in border_lines {
                let border_poly = line.make_polygons(Distance::meters(3.0));
                grid_batch.push(ctx.style().text_secondary_color, border_poly);
            }
        }

        // Draw the axes
        let y_axis_batch = {
            let mut y_axis_scale = GeomBatch::new();
            for y in 0..self.buckets_y.len() - 1 {
                let x1 = 0.0;
                let mut y1 = cell_height * y as f64;

                let middle_bucket = self.buckets_y.len() / 2 - 1;
                let y_offset = match y.cmp(&middle_bucket) {
                    Ordering::Less => cell_height,
                    Ordering::Greater => 0.0,
                    Ordering::Equal => cell_height / 2.0,
                };

                let y_label = (opts.fmt_y_axis)(self.buckets_y[y], self.buckets_y[y + 1])
                    .change_fg(ctx.style().text_secondary_color)
                    .render(ctx)
                    .centered_on(Pt2D::new(x1 + cell_width / 2.0, y1 + 0.5 * cell_height));
                y_axis_scale.append(y_label);

                if y != middle_bucket {
                    y1 += y_offset;
                    let tick_length = 8.0;
                    let tick_thickness = 2.0;
                    let start = Pt2D::new(x1 + cell_width - tick_length, y1 - tick_thickness / 2.0);
                    let line = Line::must_new(start, start.offset(tick_length, 0.0))
                        .make_polygons(Distance::meters(tick_thickness));
                    y_axis_scale.push(ctx.style().text_secondary_color, line);
                }
            }
            let mut y_axis_label = Text::from("More Problems <--------> Fewer Problems")
                .change_fg(ctx.style().text_secondary_color)
                .render(ctx)
                .rotate(Angle::degrees(-90.0));

            y_axis_label.autocrop_dims = true;
            y_axis_label = y_axis_label.autocrop();

            y_axis_label = y_axis_label.centered_on(Pt2D::new(
                8.0,
                cell_height * (self.buckets_y.len() as f64 / 2.0 - 1.0),
            ));

            GeomBatchStack::horizontal(vec![y_axis_label, y_axis_scale]).batch()
        };

        let x_axis_batch = {
            let mut x_axis_scale = GeomBatch::new();
            for x in 1..self.buckets_x.len() - 1 {
                let x1 = cell_width * x as f64;
                let y1 = 0.0;

                x_axis_scale.append(
                    Text::from(format!("{}", self.buckets_x[x]))
                        .change_fg(ctx.style().text_secondary_color)
                        .render(ctx)
                        .centered_on(Pt2D::new(x1, y1 + cell_height / 2.0)),
                );
                let tick_length = 8.0;
                let tick_thickness = 2.0;
                let start = Pt2D::new(x1, y1 - 2.0);
                let line = Line::must_new(start, start.offset(0.0, tick_length))
                    .make_polygons(Distance::meters(tick_thickness));
                x_axis_scale.push(ctx.style().text_secondary_color, line);
            }
            let x_axis_label = Text::from("Short Trips <--------> Long Trips")
                .change_fg(ctx.style().text_secondary_color)
                .render(ctx)
                .centered_on(Pt2D::new(
                    cell_width * ((self.buckets_x.len() as f64) / 2.0 - 0.5),
                    cell_height,
                ));

            x_axis_scale.append(x_axis_label);

            x_axis_scale
        };

        for (polygon, _, _) in &mut tooltips {
            let mut translated = polygon.translate(y_axis_batch.get_bounds().width(), 0.0);
            std::mem::swap(&mut translated, polygon);
        }
        let mut col = GeomBatchStack::vertical(vec![grid_batch, x_axis_batch]);
        col.set_alignment(StackAlignment::Left);

        let mut chart = GeomBatchStack::horizontal(vec![y_axis_batch, col.batch()]);
        chart.set_alignment(StackAlignment::Top);

        DrawWithTooltips::new_widget(ctx, chart.batch(), tooltips, Box::new(|_| GeomBatch::new()))
    }
}

struct MatrixOptions<X, Y> {
    total_width: f64,
    total_height: f64,
    // (lower_bound, upper_bound) -> Cell Label
    fmt_y_axis: Box<dyn Fn(Y, Y) -> Text>,
    color_scale_for_bucket: Box<dyn Fn(&App, X, Y) -> &ColorScale>,
    tooltip_for_bucket: Box<dyn Fn((Option<X>, Option<X>), (Y, Y), usize) -> Text>,
}

fn bucketize_isizes(max_buckets: usize, pts: &[(TripID, Duration, isize)]) -> Vec<isize> {
    debug_assert!(
        max_buckets % 2 == 1,
        "num_buckets must be odd to have a symmetrical number of buckets around axis"
    );
    debug_assert!(max_buckets >= 3, "num_buckets must be at least 3");

    let positive_buckets = (max_buckets - 1) / 2;
    // uniformly sized integer buckets
    let max = match pts.iter().max_by_key(|(_, _, cnt)| cnt.abs()) {
        Some(t) if (t.2.abs() as usize) >= positive_buckets => t.2.abs(),
        _ => {
            // Enforce a bucket width of at least 1.
            let negative_buckets = -(positive_buckets as isize);
            return (negative_buckets..=(positive_buckets as isize + 1)).collect();
        }
    };

    let bucket_size = (max as f64 / positive_buckets as f64).ceil() as isize;

    // we start with a 0-based bucket, and build the other buckets out from that.
    let mut buckets = vec![0];

    for i in 0..=positive_buckets {
        // the first positive bucket starts at `1`, to ensure that the 0 bucket stands alone
        buckets.push(1 + (i as isize) * bucket_size);
    }
    for i in 1..=positive_buckets {
        buckets.push(-(i as isize) * bucket_size);
    }
    buckets.sort_unstable();
    debug!("buckets: {:?}", buckets);

    buckets
}

#[cfg(test)]
mod tests {
    use super::*;

    const TRIP: TripID = TripID(42);

    #[test]
    fn test_bucketize_isizes() {
        let buckets = bucketize_isizes(
            7,
            &vec![
                (TRIP, Duration::minutes(3), -3),
                (TRIP, Duration::minutes(3), -3),
                (TRIP, Duration::minutes(3), -1),
                (TRIP, Duration::minutes(3), 2),
                (TRIP, Duration::minutes(3), 5),
            ],
        );
        // there should be an even number of buckets on either side of zero so as to center
        // our x-axis.
        //
        // there should always be a 0-1 bucket, ensuring that only '0' falls into the zero-bucket.
        //
        // all other buckets edges should be evenly spaced from the zero bucket
        assert_eq!(buckets, vec![-6, -4, -2, 0, 1, 3, 5, 7])
    }

    #[test]
    fn test_bucketize_empty_isizes() {
        let buckets = bucketize_isizes(7, &vec![]);
        assert_eq!(buckets, vec![-3, -2, -1, 0, 1, 2, 3, 4])
    }

    #[test]
    fn test_bucketize_small_isizes() {
        let buckets = bucketize_isizes(
            7,
            &vec![
                (TRIP, Duration::minutes(3), -1),
                (TRIP, Duration::minutes(3), -1),
                (TRIP, Duration::minutes(3), 0),
                (TRIP, Duration::minutes(3), -1),
                (TRIP, Duration::minutes(3), 0),
            ],
        );
        assert_eq!(buckets, vec![-3, -2, -1, 0, 1, 2, 3, 4])
    }
}