740 lines
33 KiB
Plaintext
740 lines
33 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",
|
|
" ['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 BatchCodeTableHelper:\n",
|
|
" \n",
|
|
" def __init__(self, dataFrame : pd.DataFrame):\n",
|
|
" self.dataFrame = dataFrame \n",
|
|
" self._convertColumnsOfDataFrameToNumerics(['DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT'])\n",
|
|
"\n",
|
|
" def createBatchCodeTable(self):\n",
|
|
" batchCodeTable = self.dataFrame.groupby('VAX_LOT').agg(\n",
|
|
" {\n",
|
|
" 'DIED': ['sum', 'size'],\n",
|
|
" 'L_THREAT': 'sum',\n",
|
|
" 'DISABLE': 'sum'\n",
|
|
" })\n",
|
|
" self._flattenColumns(batchCodeTable)\n",
|
|
" batchCodeTable = batchCodeTable.rename(\n",
|
|
" columns =\n",
|
|
" {\n",
|
|
" \"DIED_size\": \"ADRs\",\n",
|
|
" \"DIED_sum\": \"DEATHS\",\n",
|
|
" \"L_THREAT_sum\": \"LIFE THREATENING ILLNESSES\",\n",
|
|
" \"DISABLE_sum\": \"DISABILITIES\"\n",
|
|
" })[['ADRs', 'DEATHS', 'DISABILITIES', 'LIFE THREATENING ILLNESSES']]\n",
|
|
" return batchCodeTable.sort_values(by = 'ADRs', ascending = False)\n",
|
|
"\n",
|
|
" # create table from https://www.howbadismybatch.com/combined.html\n",
|
|
" def createSevereEffectsBatchCodeTable(self):\n",
|
|
" batchCodeTable = self.dataFrame.groupby('VAX_LOT').agg(\n",
|
|
" {\n",
|
|
" 'DIED': ['sum', 'size'],\n",
|
|
" 'L_THREAT': 'sum',\n",
|
|
" 'DISABLE': 'sum',\n",
|
|
" 'HOSPITAL': 'sum',\n",
|
|
" 'ER_VISIT': 'sum'\n",
|
|
" })\n",
|
|
" self._flattenColumns(batchCodeTable)\n",
|
|
" batchCodeTable = batchCodeTable.rename(\n",
|
|
" columns =\n",
|
|
" {\n",
|
|
" \"DIED_size\": \"ADRs\",\n",
|
|
" \"DIED_sum\": \"DEATHS\",\n",
|
|
" \"L_THREAT_sum\": \"LIFE THREATENING ILLNESSES\",\n",
|
|
" \"DISABLE_sum\": \"DISABILITIES\",\n",
|
|
" 'HOSPITAL_sum': 'HOSPITALISATIONS',\n",
|
|
" 'ER_VISIT_sum': 'EMERGENCY ROOM OR DOCTOR VISITS'\n",
|
|
" })[['ADRs', 'DEATHS', 'DISABILITIES', 'LIFE THREATENING ILLNESSES', 'HOSPITALISATIONS', 'EMERGENCY ROOM OR DOCTOR VISITS']]\n",
|
|
" batchCodeTable = batchCodeTable.sort_values(by = 'ADRs', ascending = False)\n",
|
|
" return self._addCompanyColumn(batchCodeTable, self._createCompanyByBatchCodeTable())\n",
|
|
"\n",
|
|
" def _addCompanyColumn(self, 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",
|
|
" def _createCompanyByBatchCodeTable(self):\n",
|
|
" return self._createManufacturerByBatchCodeTable().rename(columns = {\"VAX_MANU\": \"COMPANY\"})\n",
|
|
"\n",
|
|
" def _createManufacturerByBatchCodeTable(self):\n",
|
|
" manufacturerByBatchCodeTable = self.dataFrame[['VAX_LOT', 'VAX_MANU']]\n",
|
|
" manufacturerByBatchCodeTable = manufacturerByBatchCodeTable.drop_duplicates(subset = ['VAX_LOT'])\n",
|
|
" return manufacturerByBatchCodeTable.set_index('VAX_LOT')\n",
|
|
"\n",
|
|
" def _convertColumnsOfDataFrameToNumerics(self, columns):\n",
|
|
" for column in columns:\n",
|
|
" self._convertColumnOfDataFrameToNumeric(column)\n",
|
|
"\n",
|
|
" def _convertColumnOfDataFrameToNumeric(self, column):\n",
|
|
" self.dataFrame[column] = np.where(self.dataFrame[column] == 'Y', 1, 0)\n",
|
|
"\n",
|
|
" def _flattenColumns(self, batchCodeTable):\n",
|
|
" batchCodeTable.columns = [\"_\".join(a) for a in batchCodeTable.columns.to_flat_index()]\n",
|
|
"\n",
|
|
"\n",
|
|
"class BatchCodeTableFactory:\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def createBatchCodeTable(dataFrame : pd.DataFrame, manufacturer, dose):\n",
|
|
" filteredDataFrame = DataFrameFilter(dataFrame).filterBy(manufacturer = manufacturer, dose = dose)\n",
|
|
" return BatchCodeTableHelper(filteredDataFrame).createBatchCodeTable()\n",
|
|
"\n",
|
|
" # create table from https://www.howbadismybatch.com/combined.html\n",
|
|
" @staticmethod\n",
|
|
" def createSevereEffectsBatchCodeTable(dataFrame : pd.DataFrame, dose):\n",
|
|
" severeEffectsDataFrame = DataFrameFilter(dataFrame).filterForSevereEffects(dose)\n",
|
|
" return BatchCodeTableHelper(severeEffectsDataFrame).createSevereEffectsBatchCodeTable()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "41d4fa30",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class DoseAnalysis:\n",
|
|
" \n",
|
|
" @staticmethod\n",
|
|
" def getDoseTable(dataFrame):\n",
|
|
" # FK-TODO: _convertColumnsOfDataFrameToNumerics() sollte schon während des Einlesens aus den CSV-Dateien durchgeführt werden\n",
|
|
" # FK-TODO: bitte alle DataFrames als unmutable behandeln und nicht inplace ändern.\n",
|
|
" DoseAnalysis._convertColumnsOfDataFrameToNumerics(dataFrame, ['DIED', 'L_THREAT', 'DISABLE'])\n",
|
|
" return pd.DataFrame(DoseAnalysis._getNthDoseDict(dataFrame))\n",
|
|
"\n",
|
|
" # FK-TODO: inline method\n",
|
|
" @staticmethod\n",
|
|
" def _getNthDoseDict(df):\n",
|
|
" doseTable = df.groupby('VAX_DOSE_SERIES').agg(\n",
|
|
" {\n",
|
|
" 'DIED': ['sum', 'size'],\n",
|
|
" 'L_THREAT': 'sum',\n",
|
|
" 'DISABLE': 'sum'\n",
|
|
" })\n",
|
|
" DoseAnalysis._flattenColumns(doseTable)\n",
|
|
" doseTable = doseTable.rename(\n",
|
|
" columns =\n",
|
|
" {\n",
|
|
" \"DIED_size\": \"Total reports\",\n",
|
|
" \"DIED_sum\": \"Deaths\",\n",
|
|
" \"L_THREAT_sum\": \"Life Threatening Illnesses\",\n",
|
|
" \"DISABLE_sum\": \"Disabilities\"\n",
|
|
" })[['Total reports', 'Deaths', 'Disabilities', 'Life Threatening Illnesses']]\n",
|
|
" doseTable['Severe reports (%)'] = (doseTable['Deaths'] + doseTable['Disabilities'] + doseTable['Life Threatening Illnesses']) / doseTable['Total reports'] * 100\n",
|
|
" return doseTable\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def _count(dataFrame, column):\n",
|
|
" return len(dataFrame[dataFrame[column] == 'Y'])\n",
|
|
"\n",
|
|
" # FK-TODO: DRY with BatchCodeTableHelper\n",
|
|
" @staticmethod\n",
|
|
" def _convertColumnsOfDataFrameToNumerics(dataFrame, columns):\n",
|
|
" for column in columns:\n",
|
|
" DoseAnalysis._convertColumnOfDataFrameToNumeric(dataFrame, column)\n",
|
|
"\n",
|
|
" # FK-TODO: DRY with BatchCodeTableHelper\n",
|
|
" @staticmethod\n",
|
|
" def _convertColumnOfDataFrameToNumeric(dataFrame, column):\n",
|
|
" dataFrame[column] = np.where(dataFrame[column] == 'Y', 1, 0)\n",
|
|
"\n",
|
|
" # FK-TODO: DRY with BatchCodeTableHelper\n",
|
|
" @staticmethod\n",
|
|
" def _flattenColumns(batchCodeTable):\n",
|
|
" batchCodeTable.columns = [\"_\".join(a) for a in batchCodeTable.columns.to_flat_index()]\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_filterByFirstDose(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_filterBySecondDose(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 BatchCodeTableFactoryTest(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",
|
|
" # When\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createSevereEffectsBatchCodeTable(dataFrame, '1')\n",
|
|
"\n",
|
|
" # Then\n",
|
|
" batchCodeTableExpected = pd.DataFrame(\n",
|
|
" data = {\n",
|
|
" 'ADRs': [1, 1],\n",
|
|
" 'DEATHS': [0, 1],\n",
|
|
" 'DISABILITIES': [1, 0],\n",
|
|
" 'LIFE THREATENING ILLNESSES': [0, 1],\n",
|
|
" 'HOSPITALISATIONS': [0, 1],\n",
|
|
" 'EMERGENCY ROOM OR DOCTOR VISITS': [1, 1],\n",
|
|
" 'COMPANY': ['PFIZER\\BIONTECH', 'MODERNA']\n",
|
|
" },\n",
|
|
" index = pd.Index(['025L20A', '037K20A'], name = 'VAX_LOT'))\n",
|
|
" assert_frame_equal(batchCodeTable, batchCodeTableExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def test_createBatchCodeTable2(self):\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'HOSPITAL', 'ER_VISIT'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN, np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y', np.NaN, np.NaN]],\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', 'HOSPITAL', 'ER_VISIT'],\n",
|
|
" data = [ [np.NaN, np.NaN, np.NaN, np.NaN, np.NaN],\n",
|
|
" [np.NaN, np.NaN, 'Y', np.NaN, np.NaN]],\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",
|
|
" self._test_createBatchCodeTable(dataFrame, \"MODERNA\", '1')\n",
|
|
"\n",
|
|
" def test_createBatchCodeTable(self):\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(\n",
|
|
" VaersDescrReader(dataDir = \"test/VAERS\").readAllVaersDescrs())\n",
|
|
" self._test_createBatchCodeTable(dataFrame, \"MODERNA\", '1')\n",
|
|
"\n",
|
|
" def _test_createBatchCodeTable(self, dataFrame, manufacturer, dose):\n",
|
|
" # When\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createBatchCodeTable(dataFrame, manufacturer, dose)\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": "44c121ec",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pandas.testing import assert_frame_equal\n",
|
|
"\n",
|
|
"class DoseAnalysisTest(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_getDoseTable(self):\n",
|
|
" # Given\n",
|
|
" dataFrame = self.createDataFrame(\n",
|
|
" columns = ['DIED', 'L_THREAT', 'DISABLE', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT', 'VAX_DOSE_SERIES'],\n",
|
|
" data = [ ['Y', np.NaN, np.NaN,\t 'COVID19', 'MODERNA', '016M20A', '2'],\n",
|
|
" ['Y', np.NaN, np.NaN, 'COVID19', 'MODERNA', '030L20A', '1'],\n",
|
|
" ['Y', 'Y', 'Y', 'COVID19', 'MODERNA', '030L20B', '1']],\n",
|
|
" index = [\n",
|
|
" \"1048786\",\n",
|
|
" \"1048786\",\n",
|
|
" \"4711\"],\n",
|
|
" dtypes = {'VAX_DOSE_SERIES': \"string\"})\n",
|
|
" \n",
|
|
" # When\n",
|
|
" doseTable = DoseAnalysis.getDoseTable(dataFrame)\n",
|
|
"\n",
|
|
" # Then\n",
|
|
" assert_frame_equal(\n",
|
|
" doseTable,\n",
|
|
" pd.DataFrame(\n",
|
|
" data = {\n",
|
|
" 'Total reports': [2, 1],\n",
|
|
" 'Deaths': [2, 1],\n",
|
|
" 'Disabilities': [1, 0],\n",
|
|
" 'Life Threatening Illnesses': [1, 0],\n",
|
|
" 'Severe reports (%)': [(2 + 1 + 1)/2 * 100, (1 + 0 + 0)/1 * 100]\n",
|
|
" },\n",
|
|
" index = pd.Index(['1', '2'], dtype = \"string\", name = 'VAX_DOSE_SERIES')))\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(dataDir = \"VAERS\").readAllVaersDescrs()\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(vaersDescrs)\n",
|
|
" batchCodeTable = BatchCodeTableFactory.createBatchCodeTable(dataFrame, 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(dataDir = \"VAERS\").readAllVaersDescrs()\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(vaersDescrs)\n",
|
|
" severeEffectsBatchCodeTable = BatchCodeTableFactory.createSevereEffectsBatchCodeTable(dataFrame, 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')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1b228a16",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Variation in Effect of First and Second Doses"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "202f7c3f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# https://www.howbadismybatch.com/firstsecond.html\n",
|
|
"\n",
|
|
"def getDoseTable():\n",
|
|
" vaersDescrs = VaersDescrReader(dataDir = \"VAERS\").readAllVaersDescrs()\n",
|
|
" dataFrame = VaersDescr2DataFrameConverter.createDataFrameFromDescrs(vaersDescrs)\n",
|
|
" return DoseAnalysis.getDoseTable(dataFrame)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "394fa19d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"getDoseTable()"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|