Module:Medical cases data
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
This module inserts charts and maps of medical cases related to a pandemic, broken down by subregions.
caseTable
Inserts a table of cases and deaths by region.
Usage: {{#invoke:Medical cases data|caseTable|config=Configuration}}
Lua error in package.lua at line 80: module 'Module:User:Mxn/COVID-19 pandemic data/San Francisco Bay Area' not found.
map
Inserts an interactive map of cases and deaths by region.
Usage: {{#invoke:Medical cases data|map|config=Configuration|frameWidth=Frame width in pixels|frameHeight=Frame height in pixels|caption=Caption as wikitext}}
ERROR: Invalid module function: map
Configuration
Run this Wikidata Query Service query to obtain a list of localized outbreaks to add to the regions
property in the table.
return {
caption = "Cases by county", -- Table or map caption as wikitext
outbreakItem = "Q94050008", -- QID of the Wikidata item representing the
regionTerm = "Counties", -- Term for each region, used as the first column's header
regionNamePattern = "(.+) County", -- Naming pattern for regions, applied to each region's item's label to display a short name in first column; see [[mw:Extension:Scribunto/Lua reference manual#Patterns]]
populationDate = "2020-01-01", -- Date of the population figures used to calculate per-capita infection rates
regions = {
-- Add a table for each region's outbreak
{
-- QID of the outbreak
entity = "Q94259368",
-- If there is tabular data for the outbreak, specify any column names that differ from the defaults
columns = {cases = "AC_CumulCases", deaths = "AC_CumulDeaths"},
-- Optional footnote for the region as wikitext
note = "Including cases in the City of Berkeley, which are reported by the Berkeley Public Health Division.",
},
},
columnNotes = {
cases = "Cumulative cases reported by each county's health department.", -- (Optional) Footnote for the "Cases" column as wikitext
recoveries = "Counties differ in what they consider to be a recovery.", -- (Optional) Footnote for the "Recoveries" column as wikitext
deaths = "Includes suspected cases.", -- (Optional) Footnote for the "Deaths" column as wikitext
}
}
Available configurations
local p = {}
local lang = mw.getContentLanguage()
local tabularData = require("Module:Tabular data")
local wd = require("Module:wd")
local function round(x)
return (math.modf(x + (x < 0 and -0.5 or 0.5)))
end
function p._regionData(outbreakItem, columnsByRegion, populationDate)
local outbreakEntityStatements = mw.wikibase.getBestStatements(outbreakItem, "P527")
local regions = {}
for i, statement in ipairs(outbreakEntityStatements) do
local outbreakEntity = statement.mainsnak.datavalue.value.id
local locationEntity = mw.wikibase.getBestStatements(outbreakEntity, "P276")[1].mainsnak.datavalue.value.id
local dataTableName = mw.wikibase.getBestStatements(outbreakEntity, "P8204")[1].mainsnak.datavalue.value
local dataTable = mw.ext.data.get((dataTableName:gsub("^Data:", "")))
local columns = columnsByRegion[outbreakEntity] or columnsByRegion[locationEntity]
local region = {
name = mw.wikibase.getLabel(locationEntity),
link = mw.wikibase.getSitelink(locationEntity),
population = tonumber(wd._property({
"raw",
locationEntity,
"P1082",
P585 = populationDate,
})),
dataTableName = dataTableName,
cases = tabularData._cell({
data = dataTable,
output_row = -1,
output_column = columns and columns.cases or "totalConfirmedCases",
}) + (columns and columns.cases2 and tabularData._cell({
data = dataTable,
output_row = -1,
output_column = columns.cases2,
}) or 0),
deaths = tabularData._cell({
data = dataTable,
output_row = -1,
output_column = columns and columns.deaths or "deaths",
}) or tabularData._lookup({
data = dataTable,
search_pattern = "%d",
search_column = columns and columns.deaths or "deaths",
occurrence = -1,
output_column = columns and columns.deaths or "deaths",
}),
recoveries = columns and columns.recoveries and tabularData._cell({
data = dataTable,
output_row = -1,
output_column = columns.recoveries,
}),
}
table.insert(regions, region)
end
return regions
end
-- Usage: =p._caseTable({config="San Francisco Bay Area"})
function p._caseTable(args)
local config = args.config and mw.loadData("Module:User:Mxn/COVID-19 pandemic data/" .. args.config)
local populationDate = config and config.populationDate or args.populationDate
local regions = p._regionData(
config and config.outbreakItem or args.outbreakItem,
config and config.columnsByRegion,
populationDate)
table.sort(regions, function (left, right)
return left.cases > right.cases
end)
local htmlTable = mw.html.create("table")
:addClass("wikitable")
:addClass("sortable")
local headerRow = htmlTable
:tag("tr")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "string")
:wikitext(args.regionTerm or "Regions")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "number")
:wikitext("Cases")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "number")
:wikitext("Deaths")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "number")
:wikitext("Recoveries")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "number")
:wikitext(populationDate and mw.ustring.format("Population (%d)", lang:formatDate("Y", populationDate)) or "Population")
headerRow
:tag("th")
:attr("scope", "col")
:attr("data-sort-type", "number")
:tag("abbr")
:attr("title", "Cases per 10,000 inhabitants")
:wikitext("Cases/10k")
headerRow
:tag("th")
:attr("scope", "col")
:addClass("unsortable")
:tag("abbr")
:attr("title", "Reference")
:wikitext("Ref.")
for i, region in ipairs(regions) do
local row = htmlTable:tag("tr")
row
:tag("th")
:attr("scope", "row")
:wikitext(mw.ustring.format("[[%s|%s]]", region.link, region.name))
row
:tag("td")
:wikitext(lang:formatNum(region.cases))
row
:tag("td")
:wikitext(lang:formatNum(region.deaths))
if region.recoveries then
row
:tag("td")
:wikitext(lang:formatNum(region.recoveries))
else
row
:tag("td")
:addClass("unknown")
:addClass("table-unknown")
:wikitext("?")
end
row
:tag("td")
:wikitext(lang:formatNum(region.population))
row
:tag("td")
:wikitext(lang:formatNum(round(region.cases / region.population * 10000 * 100) / 100))
row
:tag("td")
:wikitext(mw.ustring.format("[[c:%s|view]]", region.dataTableName))
end
return htmlTable
end
function p.caseTable(frame)
return p._caseTable(frame.args)
end
return p