All files / owid-grapher/coreTable OwidTableSynthesizers.ts

95.4% Statements 83/87
100% Branches 9/9
50% Functions 1/2
95.4% Lines 83/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 5x 5x 5x 5x 5x 8x 8x 8x 32x 32x 32x 1760x 1760x 1760x 1760x 1760x 1760x 1760x 1760x 1760x 32x 32x 8x 8x 8x 8x 8x 8x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                  
import {
    sampleFrom,
    getRandomNumberGenerator,
    range,
} from "../clientUtils/Util"
import { countries } from "../clientUtils/countries"
import { TimeRange } from "./CoreTableConstants"
import { ColumnTypeNames } from "./CoreColumnDef"
import { OwidVariableDisplayConfigInterface } from "../clientUtils/OwidVariableDisplayConfigInterface"
import { OwidTable } from "./OwidTable"
import { OwidColumnDef, OwidTableSlugs } from "./OwidTableConstants"
import { ColumnSlug } from "../clientUtils/owidTypes"
 
interface SynthOptions {
    entityCount: number
    entityNames: string[]
    timeRange: TimeRange
    columnDefs: OwidColumnDef[]
}
 
const SynthesizeOwidTable = (
    options?: Partial<SynthOptions>,
    seed = Date.now()
): OwidTable => {
    const finalOptions: SynthOptions = {
        entityNames: [],
        entityCount: 2,
        timeRange: [1950, 2020],
        columnDefs: [],
        ...options,
    }
    const { entityCount, columnDefs, timeRange, entityNames } = finalOptions
    const colSlugs = (
        [
            OwidTableSlugs.entityName,
            OwidTableSlugs.entityCode,
            OwidTableSlugs.entityId,
            OwidTableSlugs.year,
        ] as ColumnSlug[]
    ).concat(columnDefs.map((col) => col.slug!))
 
    const entities = entityNames.length
        ? entityNames.map((name) => {
              return {
                  name,
                  code: name.substr(0, 3).toUpperCase(),
              }
          })
        : sampleFrom(countries, entityCount, seed)
 
    const rows = entities.map((entity, index) => {
        let values = columnDefs.map((def) => def.generator!())
        return range(timeRange[0], timeRange[1])
            .map((year) => {
                values = columnDefs.map((def, index) =>
                    Math.round(
                        values[index] * (1 + def.growthRateGenerator!() / 100)
                    )
                )
                return [entity.name, entity.code, index, year, ...values].join(
                    ","
                )
            })
            .join("\n")
    })
 
    return new OwidTable(
        `${colSlugs.join(",")}\n${rows.join("\n")}`,
        columnDefs
    )
}
 
export const SynthesizeNonCountryTable = (
    options?: Partial<SynthOptions>,
    seed = Date.now()
): OwidTable =>
    SynthesizeOwidTable(
        {
            entityNames: ["Fire", "Earthquake", "Tornado"],
            columnDefs: [
                {
                    slug: SampleColumnSlugs.Disasters,
                    type: ColumnTypeNames.Integer,
                    generator: getRandomNumberGenerator(0, 20, seed),
                    growthRateGenerator: getRandomNumberGenerator(
                        -50,
                        50,
                        seed
                    ),
                },
            ],
            ...options,
        },
        seed
    )
 
export enum SampleColumnSlugs {
    Population = "Population",
    GDP = "GDP",
    LifeExpectancy = "LifeExpectancy",
    Fruit = "Fruit",
    Vegetables = "Vegetables",
    Disasters = "Disasters",
}
 
export const SynthesizeGDPTable = (
    options?: Partial<SynthOptions>,
    seed = Date.now(),
    display?: OwidVariableDisplayConfigInterface
): OwidTable =>
    SynthesizeOwidTable(
        {
            columnDefs: [
                {
                    ...SynthSource(SampleColumnSlugs.Population),
                    slug: SampleColumnSlugs.Population,
                    type: ColumnTypeNames.Population,
                    generator: getRandomNumberGenerator(1e7, 1e9, seed),
                    growthRateGenerator: getRandomNumberGenerator(-5, 5, seed),
                    display,
                },
                {
                    ...SynthSource(SampleColumnSlugs.GDP),
                    slug: SampleColumnSlugs.GDP,
                    type: ColumnTypeNames.Currency,
                    generator: getRandomNumberGenerator(1e9, 1e12, seed),
                    growthRateGenerator: getRandomNumberGenerator(
                        -15,
                        15,
                        seed
                    ),
                    display,
                },
                {
                    ...SynthSource(SampleColumnSlugs.LifeExpectancy),
                    slug: SampleColumnSlugs.LifeExpectancy,
                    type: ColumnTypeNames.Age,
                    generator: getRandomNumberGenerator(60, 90, seed),
                    growthRateGenerator: getRandomNumberGenerator(-2, 2, seed),
                    display,
                },
            ],
            ...options,
        },
        seed
    )
 
const SynthSource = (
    name: string
): {
    sourceName: string
    sourceLink: string
    dataPublishedBy: string
    dataPublisherSource: string
    retrievedDate: string
    additionalInfo: string
} => {
    return {
        // id: name.charCodeAt(0) + name.charCodeAt(1) + name.charCodeAt(2),
        sourceName: `${name} Almanac`,
        sourceLink: "http://foo.example",
        dataPublishedBy: `${name} Synthetic Data Team`,
        dataPublisherSource: `${name} Institute`,
        retrievedDate: "1/1/2000",
        additionalInfo: `Downloaded via FTP`,
    }
}
 
export const SynthesizeFruitTable = (
    options?: Partial<SynthOptions>,
    seed = Date.now()
): OwidTable =>
    SynthesizeOwidTable(
        {
            columnDefs: [
                {
                    ...SynthSource(SampleColumnSlugs.Fruit),
                    slug: SampleColumnSlugs.Fruit,
                    type: ColumnTypeNames.Numeric,
                    generator: getRandomNumberGenerator(500, 1000, seed),
                    growthRateGenerator: getRandomNumberGenerator(
                        -10,
                        10,
                        seed
                    ),
                },
                {
                    ...SynthSource(SampleColumnSlugs.Vegetables),
                    slug: SampleColumnSlugs.Vegetables,
                    type: ColumnTypeNames.Numeric,
                    generator: getRandomNumberGenerator(400, 1000, seed),
                    growthRateGenerator: getRandomNumberGenerator(
                        -10,
                        12,
                        seed
                    ),
                },
            ],
            ...options,
        },
        seed
    )
 
export const SynthesizeFruitTableWithNonPositives = (
    options?: Partial<SynthOptions>,
    howManyNonPositives = 20,
    seed = Date.now()
): OwidTable => {
    const rand = getRandomNumberGenerator(-1000, 0)
    return SynthesizeFruitTable(options, seed).replaceRandomCells(
        howManyNonPositives,
        [SampleColumnSlugs.Fruit, SampleColumnSlugs.Vegetables],
        undefined,
        () => rand()
    )
}
 
const stringValues = ["NA", "inf", "..", "/", "-", "#VALUE!"]
 
export const SynthesizeFruitTableWithStringValues = (
    options?: Partial<SynthOptions>,
    howMany = 20,
    seed = Date.now()
): OwidTable => {
    return SynthesizeFruitTable(options, seed).replaceRandomCells(
        howMany,
        [SampleColumnSlugs.Fruit, SampleColumnSlugs.Vegetables],
        undefined,
        () => sampleFrom(stringValues, 1, Date.now())[0]
    )
}