Repository: neherlab/covid19_scenarios
File: src/algorithms/preparePlotData.ts (TypeScript)

Code before:
import type { Trajectory, PlotDatum } from './types/Result.types'
import { verifyPositive, verifyTuple } from '../components/Main/Results/Utils'
// import { linesToPlot, areasToPlot, DATA_POINTS } from '../components/Main/Results/ChartCommon'

export function preparePlotData(trajectory: Trajectory): PlotDatum[] {
  const { lower, middle, upper } = trajectory

  return middle.map((x, i) => ({
    time: x.time,
    lines: {
      susceptible: verifyPositive(x.current.susceptible.total),
      infectious: verifyPositive(x.current.infectious.total),
      severe: verifyPositive(x.current.severe.total),
      critical: verifyPositive(x.current.critical.total),
      overflow: verifyPositive(x.current.overflow.total),
      recovered: verifyPositive(x.cumulative.recovered.total),
      fatality: verifyPositive(x.cumulative.fatality.total),
      weeklyFatality: verifyPositive(x.cumulative.fatality.total - middle[i > 6 ? i - 7 : 0].cumulative.fatality.total),
    },
    // Error bars
    areas: {
      susceptible: verifyTuple(
        [verifyPositive(lower[i].current.susceptible.total), verifyPositive(upper[i].current.susceptible.total)],
        x.current.susceptible.total,
      ),
      infectious: verifyTuple(
        [verifyPositive(lower[i].current.infectious.total), verifyPositive(upper[i].current.infectious.total)],
        x.current.infectious.total,
      ),
      severe: verifyTuple(
        [verifyPositive(lower[i].current.severe.total), verifyPositive(upper[i].current.severe.total)],
        x.current.severe.total,
      ),
      critical: verifyTuple(
        [verifyPositive(lower[i].current.critical.total), verifyPositive(upper[i].current.critical.total)],
        x.current.critical.total,
      ),
      overflow: verifyTuple(
        [verifyPositive(lower[i].current.overflow.total), verifyPositive(upper[i].current.overflow.total)],
        x.current.overflow.total,
      ),
      recovered: verifyTuple(
        [verifyPositive(lower[i].cumulative.recovered.total), verifyPositive(upper[i].cumulative.recovered.total)],

Review comment (refactor):
@rneher 
If I read it correctly, the conditionals `i > 6 ? i - 7 : 0`, or even the entire subtraction expressions seem to be duplicated. Could we extract them into a variable and bubble it up ? For the sake of self-documentation, some good name for this variable would be nice to have as well. I'm not sure if I correctly understand what this expression calculates and why.

This refactoring will probably require introducing a function scope with `return` statement and all this code will be indented to the right, so we may do this refactoring afterwards, to keep the current diff clear, as it is.


Code after:
import type { Trajectory, PlotDatum } from './types/Result.types'
import { verifyPositive, verifyTuple } from '../components/Main/Results/Utils'
// import { linesToPlot, areasToPlot, DATA_POINTS } from '../components/Main/Results/ChartCommon'

export function preparePlotData(trajectory: Trajectory): PlotDatum[] {
  const { lower, middle, upper } = trajectory

  return middle.map((x, day) => {
    const previousDay = day > 6 ? day - 7 : 0
    const centerWeeklyDeaths = x.cumulative.fatality.total - middle[previousDay].cumulative.fatality.total
    // NOTE: this is using the upper and lower trajectories
    const extremeWeeklyDeaths1 = upper[day].cumulative.fatality.total - upper[previousDay].cumulative.fatality.total
    const extremeWeeklyDeaths2 = lower[day].cumulative.fatality.total - lower[previousDay].cumulative.fatality.total
    const upperWeeklyDeaths = extremeWeeklyDeaths1 > extremeWeeklyDeaths2 ? extremeWeeklyDeaths1 : extremeWeeklyDeaths2
    const lowerWeeklyDeaths = extremeWeeklyDeaths1 > extremeWeeklyDeaths2 ? extremeWeeklyDeaths2 : extremeWeeklyDeaths1

    return {
      time: x.time,
      lines: {
        susceptible: verifyPositive(x.current.susceptible.total),
        infectious: verifyPositive(x.current.infectious.total),
        severe: verifyPositive(x.current.severe.total),
        critical: verifyPositive(x.current.critical.total),
        overflow: verifyPositive(x.current.overflow.total),
        recovered: verifyPositive(x.cumulative.recovered.total),
        fatality: verifyPositive(x.cumulative.fatality.total),
        weeklyFatality: verifyPositive(centerWeeklyDeaths),
      },
      // Error bars
      areas: {
        susceptible: verifyTuple(
          [verifyPositive(lower[day].current.susceptible.total), verifyPositive(upper[day].current.susceptible.total)],
          x.current.susceptible.total,
        ),
        infectious: verifyTuple(
          [verifyPositive(lower[day].current.infectious.total), verifyPositive(upper[day].current.infectious.total)],
          x.current.infectious.total,
        ),
        severe: verifyTuple(
          [verifyPositive(lower[day].current.severe.total), verifyPositive(upper[day].current.severe.total)],
          x.current.severe.total,
        ),
        critical: verifyTuple(