621 lines
27 KiB
Plaintext
621 lines
27 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9de5907f-18f5-4cb1-903e-26028ff1fa03",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"pd.set_option('display.max_rows', 100)\n",
|
|
"pd.set_option('display.max_columns', None)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a271254b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"class VaersDescrReader:\n",
|
|
" \n",
|
|
" def __init__(self, dataDir):\n",
|
|
" self.dataDir = dataDir \n",
|
|
"\n",
|
|
" def readAllVaersDescrs(self):\n",
|
|
" return self.readVaersDescrs([\"2021\", \"2022\"])\n",
|
|
" \n",
|
|
" def readVaersDescrs(self, years):\n",
|
|
" return [self.readVaersDescr(year) for year in years]\n",
|
|
"\n",
|
|
" def readVaersDescr(self, year):\n",
|
|
" folder = self.dataDir + \"/\" + year + \"VAERSData/\"\n",
|
|
" return {\n",
|
|
" 'VAERSDATA':\n",
|
|
" self._read_csv(\n",
|
|
" folder + year + \"VAERSDATA.csv\",\n",
|
|
" # FK-TODO: use Column enum\n",
|
|
" ['VAERS_ID', 'DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT']),\n",
|
|
" 'VAERSVAX':\n",
|
|
" self._read_csv(\n",
|
|
" folder + year + \"VAERSVAX.csv\",\n",
|
|
" ['VAERS_ID', 'VAX_DOSE_SERIES', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" dtype = {\"VAX_DOSE_SERIES\": \"string\"})\n",
|
|
" }\n",
|
|
"\n",
|
|
" def _read_csv(self, file, usecols, dtype = {}):\n",
|
|
" return pd.read_csv(\n",
|
|
" file,\n",
|
|
" index_col = 'VAERS_ID',\n",
|
|
" encoding = 'latin1',\n",
|
|
" low_memory = False,\n",
|
|
" usecols = usecols,\n",
|
|
" dtype = dtype)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7b5d6df0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"class VaersDescr2DataFrameConverter:\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def createDataFrameFromDescr(vaersDescr):\n",
|
|
" return pd.merge(\n",
|
|
" vaersDescr['VAERSDATA'],\n",
|
|
" vaersDescr['VAERSVAX'],\n",
|
|
" how = 'left',\n",
|
|
" left_index = True,\n",
|
|
" right_index = True,\n",
|
|
" validate = 'one_to_many')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def createDataFrameFromDescrs(vaersDescrs):\n",
|
|
" dataFrames = [VaersDescr2DataFrameConverter.createDataFrameFromDescr(vaersDescr) for vaersDescr in vaersDescrs]\n",
|
|
" return pd.concat(dataFrames)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3ebcba86",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"class DataFrameFilter:\n",
|
|
" \n",
|
|
" def __init__(self, dataFrame):\n",
|
|
" self.dataFrame = dataFrame \n",
|
|
"\n",
|
|
" def filterBy(self, manufacturer = None, dose = None):\n",
|
|
" return self.dataFrame[self._isCovid19() & self._isManufacturer(manufacturer) & self._isDose(dose)]\n",
|
|
"\n",
|
|
" def filterForSevereEffects(self, dose):\n",
|
|
" return self.filterBy(dose = dose)\n",
|
|
"\n",
|
|
" def _isCovid19(self):\n",
|
|
" return self.dataFrame[\"VAX_TYPE\"] == \"COVID19\"\n",
|
|
"\n",
|
|
" def _isManufacturer(self, manufacturer):\n",
|
|
" return self.dataFrame[\"VAX_MANU\"] == manufacturer if manufacturer is not None else True\n",
|
|
"\n",
|
|
" def _isDose(self, dose):\n",
|
|
" return self.dataFrame[\"VAX_DOSE_SERIES\"].str.contains(dose) if dose is not None else True\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "99945ca8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"class BatchCodeTableFactory:\n",
|
|
" \n",
|
|
" @staticmethod\n",
|
|
" def createBatchCodeTable(df : pd.DataFrame):\n",
|
|
" return BatchCodeTableFactory._asDataFrame(\n",
|
|
" {\n",
|
|
" 'ADRs': BatchCodeTableFactory._getADRs(df),\n",
|
|
" 'DEATHS': BatchCodeTableFactory._getDEATHS(df),\n",
|
|
" 'DISABILITIES': BatchCodeTableFactory._getDISABILITIES(df),\n",
|
|
" 'LIFE THREATENING ILLNESSES': BatchCodeTableFactory._getLIFE_THREATENING_ILLNESSES(df)\n",
|
|
" })\n",
|
|
"\n",
|
|
" # create table from https://www.howbadismybatch.com/combined.html\n",
|
|
" @staticmethod\n",
|
|
" def createSevereEffectsBatchCodeTable(df : pd.DataFrame):\n",
|
|
" return BatchCodeTableFactory._addCompanyColumn(\n",
|
|
" BatchCodeTableFactory._asDataFrame(\n",
|
|
" {\n",
|
|
" 'ADRs': BatchCodeTableFactory._getADRs(df),\n",
|
|
" 'DEATHS': BatchCodeTableFactory._getDEATHS(df),\n",
|
|
" 'DISABILITIES': BatchCodeTableFactory._getDISABILITIES(df),\n",
|
|
" 'LIFE THREATENING ILLNESSES': BatchCodeTableFactory._getLIFE_THREATENING_ILLNESSES(df),\n",
|
|
" 'HOSPITALISATIONS': BatchCodeTableFactory._getHOSPITALISATIONS(df),\n",
|
|
" 'EMERGENCY ROOM OR DOCTOR VISITS': BatchCodeTableFactory._getER_VISITs(df)\n",
|
|
" }),\n",
|
|
" BatchCodeTableFactory._createCompanyByBatchCodeTable(df))\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getADRs(df):\n",
|
|
" return df['VAX_LOT'].value_counts()\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getDEATHS(df):\n",
|
|
" return BatchCodeTableFactory._countValues(df, 'DIED')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getDISABILITIES(df):\n",
|
|
" return BatchCodeTableFactory._countValues(df, 'DISABLE')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getLIFE_THREATENING_ILLNESSES(df):\n",
|
|
" return BatchCodeTableFactory._countValues(df, 'L_THREAT')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getHOSPITALISATIONS(df):\n",
|
|
" return BatchCodeTableFactory._countValues(df, 'HOSPITAL')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _getER_VISITs(df):\n",
|
|
" return BatchCodeTableFactory._countValues(df, 'ER_VISIT')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _countValues(df, column):\n",
|
|
" return df[df[column] == 'Y']['VAX_LOT'].value_counts()\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _asDataFrame(dict):\n",
|
|
" dataFrame = pd.concat(dict, axis = 'columns')\n",
|
|
" dataFrame.index.name = 'VAX_LOT'\n",
|
|
" return dataFrame.replace(to_replace = np.nan, value = 0)\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _addCompanyColumn(batchCodeTable, companyByBatchCodeTable):\n",
|
|
" return pd.merge(\n",
|
|
" batchCodeTable,\n",
|
|
" companyByBatchCodeTable,\n",
|
|
" how = 'left',\n",
|
|
" left_index = True,\n",
|
|
" right_index = True,\n",
|
|
" validate = 'one_to_one')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _createManufacturerByBatchCodeTable(df):\n",
|
|
" manufacturerByBatchCodeTable = df[['VAX_LOT', 'VAX_MANU']]\n",
|
|
" manufacturerByBatchCodeTable = manufacturerByBatchCodeTable.drop_duplicates(subset = ['VAX_LOT'])\n",
|
|
" return manufacturerByBatchCodeTable.set_index('VAX_LOT')\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _createCompanyByBatchCodeTable(df):\n",
|
|
" return BatchCodeTableFactory._createManufacturerByBatchCodeTable(df).rename(columns = {\"VAX_MANU\": \"COMPANY\"})\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3dacedfd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import unittest"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e59a1825",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pandas.testing import assert_frame_equal\n",
|
|
"\n",
|
|
"class DataFrameFilterTest(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_filterBy(self):\n",
|
|
" # Given\n",
|
|
" dataFrameFilter = DataFrameFilter(\n",
|
|
" VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" ['COVID19', 'MODERNA', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" },\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ [np.NaN, np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"1996873\",\n",
|
|
" \"1996874\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['HPV9', 'MERCK & CO. INC.', 'R017624', 'UNK'],\n",
|
|
" ['COVID19', 'MODERNA', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"1996873\",\n",
|
|
" \"1996874\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ]))\n",
|
|
" \n",
|
|
" # When\n",
|
|
" dataFrame = dataFrameFilter.filterBy(manufacturer = \"MODERNA\", dose = '1')\n",
|
|
" \n",
|
|
" # Then\n",
|
|
" dataFrameExpected = self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN, 'COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" [np.NaN, np.NaN, 'Y', 'COVID19', 'MODERNA', '025L20A', '1'],\n",
|
|
" [np.NaN, np.NaN, 'Y', 'COVID19', 'MODERNA', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\",\n",
|
|
" \"1996874\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" assert_frame_equal(dataFrame, dataFrameExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def test_filterForSevereEffects(self):\n",
|
|
" # Given\n",
|
|
" dataFrameFilter = DataFrameFilter(\n",
|
|
" VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT'],\n",
|
|
" data = [ ['Y', 'Y', np.NaN, 'Y', 'Y'],\n",
|
|
" [np.NaN, np.NaN, 'Y', np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" ['COVID19', 'PFIZER\\BIONTECH', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ]))\n",
|
|
"\n",
|
|
" # When\n",
|
|
" dataFrame = dataFrameFilter.filterForSevereEffects(dose = '1')\n",
|
|
" \n",
|
|
" # Then\n",
|
|
" dataFrameExpected = self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['Y', 'Y', np.NaN, 'Y', 'Y', 'COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" [np.NaN, np.NaN, 'Y', np.NaN, 'Y', 'COVID19', 'PFIZER\\BIONTECH', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" assert_frame_equal(dataFrame, dataFrameExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def test_createAndFilterDataFrameFromDescrsWithFirstDose(self):\n",
|
|
" # Given\n",
|
|
" dataFrameFilter = DataFrameFilter(\n",
|
|
" VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN]],\n",
|
|
" index = [\n",
|
|
" \"1048786\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '016M20A', '2'],\n",
|
|
" ['COVID19', 'MODERNA', '030L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"1048786\",\n",
|
|
" \"1048786\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ]))\n",
|
|
" \n",
|
|
" # When\n",
|
|
" dataFrame = dataFrameFilter.filterBy(manufacturer = \"MODERNA\", dose = '1')\n",
|
|
" \n",
|
|
" # Then\n",
|
|
" dataFrameExpected = self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN, 'COVID19', 'MODERNA', '030L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"1048786\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" assert_frame_equal(dataFrame, dataFrameExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def test_createAndFilterDataFrameFromDescrsWithSecondDose(self):\n",
|
|
" # Given\n",
|
|
" dataFrameFilter = DataFrameFilter(\n",
|
|
" VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN]],\n",
|
|
" index = [\n",
|
|
" \"1048786\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '016M20A', '2'],\n",
|
|
" ['COVID19', 'MODERNA', '030L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"1048786\",\n",
|
|
" \"1048786\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ]))\n",
|
|
"\n",
|
|
" # When\n",
|
|
" dataFrame = dataFrameFilter.filterBy(manufacturer = \"MODERNA\", dose = '2')\n",
|
|
" \n",
|
|
" # Then\n",
|
|
" dataFrameExpected = self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN, 'COVID19', 'MODERNA', '016M20A', '2']],\n",
|
|
" index = [\n",
|
|
" \"1048786\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" assert_frame_equal(dataFrame, dataFrameExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def createDataFrame(self, index, columns, data, dtypes = {}):\n",
|
|
" return pd.DataFrame(index = index, columns = columns, data = data).astype(dtypes)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e14465d7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pandas.testing import assert_frame_equal\n",
|
|
"\n",
|
|
"class BatchCodeTableTest(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_createBatchCodeTable2(self):\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" ['COVID19', 'MODERNA', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" },\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" data = [ [np.NaN, np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"1996873\",\n",
|
|
" \"1996874\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['HPV9', 'MERCK & CO. INC.', 'R017624', 'UNK'],\n",
|
|
" ['COVID19', 'MODERNA', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"1996873\",\n",
|
|
" \"1996874\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ])\n",
|
|
" dataFrame = DataFrameFilter(dataFrame).filterBy(manufacturer = \"MODERNA\", dose = '1')\n",
|
|
" self._test_createBatchCodeTable(dataFrame);\n",
|
|
"\n",
|
|
" def test_createBatchCodeTable(self):\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" VaersDescrReader(\"test/VAERS\").readAllVaersDescrs())\n",
|
|
" self._test_createBatchCodeTable(\n",
|
|
" DataFrameFilter(dataFrame).filterBy(\n",
|
|
" manufacturer = \"MODERNA\",\n",
|
|
" dose = '1'))\n",
|
|
"\n",
|
|
" def _test_createBatchCodeTable(self, dataFrame):\n",
|
|
" # When\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createBatchCodeTable(dataFrame)\n",
|
|
"\n",
|
|
" # Then\n",
|
|
" batchCodeTableExpected = pd.DataFrame(\n",
|
|
" data = {\n",
|
|
" 'ADRs': [2, 1],\n",
|
|
" 'DEATHS': [0, 1],\n",
|
|
" 'DISABILITIES': [2, 0],\n",
|
|
" 'LIFE THREATENING ILLNESSES': [0, 0]\n",
|
|
" },\n",
|
|
" index = pd.Index(['025L20A', '037K20A'], name = 'VAX_LOT'))\n",
|
|
" assert_frame_equal(batchCodeTable, batchCodeTableExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def createDataFrame(self, index, columns, data, dtypes = {}):\n",
|
|
" return pd.DataFrame(index = index, columns = columns, data = data).astype(dtypes)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ded70c87",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pandas.testing import assert_frame_equal\n",
|
|
"\n",
|
|
"class SevereEffectsBatchCodeTableTest(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_createSevereEffectsBatchCodeTable(self):\n",
|
|
" # Given\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT'],\n",
|
|
" data = [ ['Y', 'Y', np.NaN, 'Y', 'Y'],\n",
|
|
" [np.NaN, np.NaN, 'Y', np.NaN, 'Y']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"]),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" columns = ['VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['COVID19', 'MODERNA', '037K20A', '1'],\n",
|
|
" ['COVID19', 'PFIZER\\BIONTECH', '025L20A', '1']],\n",
|
|
" index = [\n",
|
|
" \"0916600\",\n",
|
|
" \"0916601\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" }\n",
|
|
" ]\n",
|
|
" )\n",
|
|
" dataFrame = DataFrameFilter(dataFrame).filterForSevereEffects(dose = '1')\n",
|
|
"\n",
|
|
" # When\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createSevereEffectsBatchCodeTable(dataFrame)\n",
|
|
"\n",
|
|
" # Then\n",
|
|
" batchCodeTableExpected = pd.DataFrame(\n",
|
|
" data = {\n",
|
|
" 'ADRs': [1, 1],\n",
|
|
" 'DEATHS': [1, 0],\n",
|
|
" 'DISABILITIES': [0, 1],\n",
|
|
" 'LIFE THREATENING ILLNESSES': [1, 0],\n",
|
|
" 'HOSPITALISATIONS': [1, 0],\n",
|
|
" 'EMERGENCY ROOM OR DOCTOR VISITS': [1, 1],\n",
|
|
" 'COMPANY': ['MODERNA', 'PFIZER\\BIONTECH']\n",
|
|
" },\n",
|
|
" index = pd.Index(['037K20A', '025L20A'], name='VAX_LOT'))\n",
|
|
" assert_frame_equal(batchCodeTable, batchCodeTableExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def createDataFrame(self, index, columns, data, dtypes = {}):\n",
|
|
" return pd.DataFrame(index = index, columns = columns, data = data).astype(dtypes)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5a8bff1b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"unittest.main(argv = [''], verbosity = 2, exit = False)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "86e0e4f2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def saveBatchCodeTable(manufacturer, excelFile):\n",
|
|
" vaersDescrs = VaersDescrReader(\"VAERS\").readAllVaersDescrs()\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(vaersDescrs)\n",
|
|
" dataFrameFilter = DataFrameFilter(dataFrame)\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createBatchCodeTable(dataFrameFilter.filterBy(manufacturer = manufacturer, dose = '1'))\n",
|
|
" display(manufacturer, batchCodeTable)\n",
|
|
" batchCodeTable.to_excel(excelFile)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ab170c16",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"saveBatchCodeTable(\"MODERNA\", \"results/moderna.xlsx\")\n",
|
|
"saveBatchCodeTable(\"PFIZER\\BIONTECH\", \"results/pfizer.xlsx\")\n",
|
|
"saveBatchCodeTable(\"JANSSEN\", \"results/janssen.xlsx\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bc56831d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def saveSevereEffectsBatchCodeTable(excelFile):\n",
|
|
" vaersDescrs = VaersDescrReader(\"VAERS\").readAllVaersDescrs()\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(vaersDescrs)\n",
|
|
" dataFrameFilter = DataFrameFilter(dataFrame)\n",
|
|
" severeEffectsBatchCodeTable = BatchCodeTableFactory.createSevereEffectsBatchCodeTable(dataFrameFilter.filterForSevereEffects(dose = '1'))\n",
|
|
" display('severeEffectsBatchCodeTable', severeEffectsBatchCodeTable)\n",
|
|
" severeEffectsBatchCodeTable.to_excel(excelFile)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ace3fed9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"saveSevereEffectsBatchCodeTable('results/severeEffects.xlsx')"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|