refactoring

This commit is contained in:
frankknoll
2022-11-22 15:51:53 +01:00
parent 8d1f38dfb6
commit 447c2ed552
5 changed files with 60 additions and 95 deletions

View File

@@ -2,6 +2,17 @@ from bs4 import BeautifulSoup
from HtmlTransformerUtil import HtmlTransformerUtil from HtmlTransformerUtil import HtmlTransformerUtil
from CountryOptionsSetter import CountryOptionsSetter from CountryOptionsSetter import CountryOptionsSetter
from DateProvider import DateProvider from DateProvider import DateProvider
from HtmlUtils import getCountryOptions, getCountries
from DateProvider import DateProvider
from BatchCodeTablePersister import createAndSaveBatchCodeTables
def updateBatchCodeTableHtmlFile(internationalVaersCovid19):
countryOptions = getCountryOptions(getCountries(internationalVaersCovid19))
saveCountryOptions(countryOptions)
saveLastUpdatedBatchCodeTable(DateProvider().getLastUpdatedDataSource())
createAndSaveBatchCodeTables(internationalVaersCovid19, minADRsForLethality=100)
def saveCountryOptions(countryOptions): def saveCountryOptions(countryOptions):
HtmlTransformerUtil().applySoupTransformerToFile( HtmlTransformerUtil().applySoupTransformerToFile(
@@ -12,6 +23,7 @@ def saveCountryOptions(countryOptions):
CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions), CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions),
'lxml')) 'lxml'))
def saveLastUpdatedBatchCodeTable(lastUpdated): def saveLastUpdatedBatchCodeTable(lastUpdated):
def setLastUpdated(soup): def setLastUpdated(soup):
soup.find(id = "last_updated").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT)) soup.find(id = "last_updated").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT))
@@ -20,3 +32,4 @@ def saveLastUpdatedBatchCodeTable(lastUpdated):
HtmlTransformerUtil().applySoupTransformerToFile( HtmlTransformerUtil().applySoupTransformerToFile(
file = "../docs/batchCodeTable.html", file = "../docs/batchCodeTable.html",
soupTransformer = setLastUpdated) soupTransformer = setLastUpdated)

View File

@@ -1,15 +1,38 @@
from IOUtils import IOUtils from IOUtils import IOUtils
from BatchCodeTableFactory import BatchCodeTableFactory
import numpy as np import numpy as np
from HtmlUtils import getCountries
def createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality = None):
def createAndSaveBatchCodeTables(internationalVaersCovid19, minADRsForLethality):
batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19)
_createAndSaveBatchCodeTablesForCountries(
createBatchCodeTableForCountry=lambda country: batchCodeTableFactory.createBatchCodeTableByCountry(
country),
countries=getCountries(internationalVaersCovid19),
minADRsForLethality=minADRsForLethality)
_createAndSaveBatchCodeTableForCountry(
createBatchCodeTableForCountry=lambda country: batchCodeTableFactory.createGlobalBatchCodeTable(),
country='Global',
minADRsForLethality=minADRsForLethality)
def _createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality=None):
batchCodeTable = createBatchCodeTableForCountry(country) batchCodeTable = createBatchCodeTableForCountry(country)
batchCodeTable.index.set_names("Batch", inplace=True) batchCodeTable.index.set_names("Batch", inplace=True)
if minADRsForLethality is not None: if minADRsForLethality is not None:
batchCodeTable.loc[batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality, ['Severe reports', 'Lethality']] = [np.nan, np.nan] batchCodeTable.loc[
IOUtils.saveDataFrame(batchCodeTable, '../docs/data/batchCodeTables/' + country) batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality,
['Severe reports', 'Lethality']
] = [np.nan, np.nan]
IOUtils.saveDataFrame(
batchCodeTable,
'../docs/data/batchCodeTables/' + country)
# display(country + ":", batchCodeTable) # display(country + ":", batchCodeTable)
display(country) display(country)
def createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality = None):
def _createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality=None):
for country in countries: for country in countries:
createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality) _createAndSaveBatchCodeTableForCountry(
createBatchCodeTableForCountry, country, minADRsForLethality)

View File

@@ -103,26 +103,6 @@
" downloadVAERSFileAndUnzip('NonDomesticVAERSData.zip', workingDirectory)" " downloadVAERSFileAndUnzip('NonDomesticVAERSData.zip', workingDirectory)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "3ebcba86",
"metadata": {},
"outputs": [],
"source": [
"from DataFrameFilter import DataFrameFilter"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71456a79",
"metadata": {},
"outputs": [],
"source": [
"from BatchCodeTableFactory import BatchCodeTableFactory"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -130,17 +110,17 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from BatchCodeTableHtmlUpdater import saveCountryOptions, saveLastUpdatedBatchCodeTable" "from BatchCodeTableHtmlUpdater import updateBatchCodeTableHtmlFile"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "86e0e4f2", "id": "62132e68",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from VaersReader import getVaersForYears, getNonDomesticVaers" "from InternationalVaersCovid19Provider import getInternationalVaersCovid19"
] ]
}, },
{ {
@@ -150,42 +130,10 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"internationalVaers = pd.concat([getVaersForYears([2020, 2021, 2022]), getNonDomesticVaers()])\n", "internationalVaersCovid19 = getInternationalVaersCovid19([2020, 2021, 2022])\n",
"internationalVaersCovid19 = DataFrameFilter().filterByCovid19(internationalVaers)\n",
"internationalVaersCovid19" "internationalVaersCovid19"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "ff259a35",
"metadata": {},
"outputs": [],
"source": [
"from BatchCodeTablePersister import createAndSaveBatchCodeTableForCountry, createAndSaveBatchCodeTablesForCountries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc1ef82a",
"metadata": {},
"outputs": [],
"source": [
"from HtmlUtils import getCountryOptions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c4d04fb",
"metadata": {},
"outputs": [],
"source": [
"countries = sorted(internationalVaersCovid19['COUNTRY'].unique())\n",
"countryOptions = getCountryOptions(countries)\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -193,38 +141,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"saveCountryOptions(countryOptions)" "updateBatchCodeTableHtmlFile(internationalVaersCovid19)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c7485b5",
"metadata": {},
"outputs": [],
"source": [
"saveLastUpdatedBatchCodeTable(dateProvider.getLastUpdatedDataSource())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e7e01a5",
"metadata": {},
"outputs": [],
"source": [
"minADRsForLethality = 100\n",
"batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19)\n",
"\n",
"createAndSaveBatchCodeTablesForCountries(\n",
" createBatchCodeTableForCountry = lambda country: batchCodeTableFactory.createBatchCodeTableByCountry(country),\n",
" countries = countries,\n",
" minADRsForLethality = minADRsForLethality)\n",
"\n",
"createAndSaveBatchCodeTableForCountry(\n",
" createBatchCodeTableForCountry = lambda country: batchCodeTableFactory.createGlobalBatchCodeTable(),\n",
" country = 'Global',\n",
" minADRsForLethality = minADRsForLethality)"
] ]
}, },
{ {

View File

@@ -1,3 +1,7 @@
def getCountries(internationalVaersCovid19):
return sorted(internationalVaersCovid19['COUNTRY'].unique())
def getCountryOptions(countries): def getCountryOptions(countries):
return ['<option value="Global" selected>Global</option>'] + _getCountryOptions(countries) return ['<option value="Global" selected>Global</option>'] + _getCountryOptions(countries)

View File

@@ -0,0 +1,8 @@
from DataFrameFilter import DataFrameFilter
from VaersReader import getVaersForYears, getNonDomesticVaers
import pandas as pd
def getInternationalVaersCovid19(years):
internationalVaers = pd.concat([getVaersForYears(years), getNonDomesticVaers()])
internationalVaersCovid19 = DataFrameFilter().filterByCovid19(internationalVaers)
return internationalVaersCovid19