diff --git a/src/BatchCodeTableHtmlUpdater.py b/src/BatchCodeTableHtmlUpdater.py new file mode 100644 index 00000000000..9fccbff0c72 --- /dev/null +++ b/src/BatchCodeTableHtmlUpdater.py @@ -0,0 +1,22 @@ +from bs4 import BeautifulSoup +from HtmlTransformerUtil import HtmlTransformerUtil +from CountryOptionsSetter import CountryOptionsSetter +from DateProvider import DateProvider + +def saveCountryOptions(countryOptions): + HtmlTransformerUtil().applySoupTransformerToFile( + file = "../docs/batchCodeTable.html", + soupTransformer = + lambda soup: + BeautifulSoup( + CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions), + 'lxml')) + +def saveLastUpdatedBatchCodeTable(lastUpdated): + def setLastUpdated(soup): + soup.find(id = "last_updated").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT)) + return soup + + HtmlTransformerUtil().applySoupTransformerToFile( + file = "../docs/batchCodeTable.html", + soupTransformer = setLastUpdated) \ No newline at end of file diff --git a/src/BatchCodeTablePersister.py b/src/BatchCodeTablePersister.py new file mode 100644 index 00000000000..3ab1df1dda9 --- /dev/null +++ b/src/BatchCodeTablePersister.py @@ -0,0 +1,15 @@ +from IOUtils import IOUtils +import numpy as np + +def createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality = None): + batchCodeTable = createBatchCodeTableForCountry(country) + batchCodeTable.index.set_names("Batch", inplace = True) + if minADRsForLethality is not None: + batchCodeTable.loc[batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality, ['Severe reports', 'Lethality']] = [np.nan, np.nan] + IOUtils.saveDataFrame(batchCodeTable, '../docs/data/batchCodeTables/' + country) + # display(country + ":", batchCodeTable) + display(country) + +def createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality = None): + for country in countries: + createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality) \ No newline at end of file diff --git a/src/HowBadIsMyBatch.ipynb b/src/HowBadIsMyBatch.ipynb index 1bfa869641f..1712b872fe1 100644 --- a/src/HowBadIsMyBatch.ipynb +++ b/src/HowBadIsMyBatch.ipynb @@ -7,7 +7,6 @@ "metadata": {}, "outputs": [], "source": [ - "import numpy as np\n", "import pandas as pd\n", "\n", "pd.set_option('display.max_rows', 100)\n", @@ -124,26 +123,6 @@ "from BatchCodeTableFactory import BatchCodeTableFactory" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "4db36933", - "metadata": {}, - "outputs": [], - "source": [ - "from HtmlTransformerUtil import HtmlTransformerUtil" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "32d4eecf", - "metadata": {}, - "outputs": [], - "source": [ - "from CountryOptionsSetter import CountryOptionsSetter" - ] - }, { "cell_type": "code", "execution_count": null, @@ -151,44 +130,7 @@ "metadata": {}, "outputs": [], "source": [ - "from bs4 import BeautifulSoup\n", - "\n", - "\n", - "def saveCountryOptions(countryOptions):\n", - " HtmlTransformerUtil().applySoupTransformerToFile(\n", - " file = \"../docs/batchCodeTable.html\",\n", - " soupTransformer =\n", - " lambda soup:\n", - " BeautifulSoup(\n", - " CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions),\n", - " 'lxml'))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f02dddfe", - "metadata": {}, - "outputs": [], - "source": [ - "def saveLastUpdatedBatchCodeTable(lastUpdated):\n", - " def setLastUpdated(soup):\n", - " soup.find(id = \"last_updated\").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT))\n", - " return soup\n", - "\n", - " HtmlTransformerUtil().applySoupTransformerToFile(\n", - " file = \"../docs/batchCodeTable.html\",\n", - " soupTransformer = setLastUpdated)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6aa28541", - "metadata": {}, - "outputs": [], - "source": [ - "from IOUtils import IOUtils" + "from BatchCodeTableHtmlUpdater import saveCountryOptions, saveLastUpdatedBatchCodeTable" ] }, { @@ -220,18 +162,7 @@ "metadata": {}, "outputs": [], "source": [ - "def createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality = None):\n", - " batchCodeTable = createBatchCodeTableForCountry(country)\n", - " batchCodeTable.index.set_names(\"Batch\", inplace = True)\n", - " if minADRsForLethality is not None:\n", - " batchCodeTable.loc[batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality, ['Severe reports', 'Lethality']] = [np.nan, np.nan]\n", - " IOUtils.saveDataFrame(batchCodeTable, '../docs/data/batchCodeTables/' + country)\n", - " # display(country + \":\", batchCodeTable)\n", - " display(country)\n", - "\n", - "def createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality = None):\n", - " for country in countries:\n", - " createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality)" + "from BatchCodeTablePersister import createAndSaveBatchCodeTableForCountry, createAndSaveBatchCodeTablesForCountries" ] }, { @@ -241,11 +172,7 @@ "metadata": {}, "outputs": [], "source": [ - "def getCountryOptions(countries):\n", - " return [getCountryOption(country) for country in countries]\n", - "\n", - "def getCountryOption(country):\n", - " return ''.format(country = country)" + "from HtmlUtils import getCountryOptions" ] }, { @@ -256,7 +183,7 @@ "outputs": [], "source": [ "countries = sorted(internationalVaersCovid19['COUNTRY'].unique())\n", - "countryOptions = [''] + getCountryOptions(countries)" + "countryOptions = getCountryOptions(countries)\n" ] }, { diff --git a/src/HtmlUtils.py b/src/HtmlUtils.py new file mode 100644 index 00000000000..6bd9ca8f537 --- /dev/null +++ b/src/HtmlUtils.py @@ -0,0 +1,10 @@ +def getCountryOptions(countries): + return [''] + _getCountryOptions(countries) + + +def _getCountryOptions(countries): + return [_getCountryOption(country) for country in countries] + + +def _getCountryOption(country): + return ''.format(country=country)