272 lines
10 KiB
Plaintext
272 lines
10 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": "7b5d6df0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def _createDataFrame(vaersDescrs, manufacturer):\n",
|
|
" def vaersDescr2DataFrame(vaersDescr):\n",
|
|
" return pd.merge(vaersDescr['VAERSDATA'], vaersDescr['VAERSVAX'], left_index = True, right_index = True)\n",
|
|
"\n",
|
|
" df = pd.concat(map(vaersDescr2DataFrame, vaersDescrs))\n",
|
|
" return df[(df[\"VAX_TYPE\"] == \"COVID19\") & (df[\"VAX_MANU\"] == manufacturer)]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "233bc590",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def createDataFrame(dataDir, manufacturer):\n",
|
|
" def readVaersDescr(year):\n",
|
|
" def read_csv(file, usecols):\n",
|
|
" return pd.read_csv(file, index_col = 'VAERS_ID', encoding = 'latin1', low_memory = False, usecols = usecols)\n",
|
|
"\n",
|
|
" folder = dataDir + \"/\" + year + \"VAERSData/\"\n",
|
|
" return {\n",
|
|
" 'VAERSDATA': read_csv(folder + year + \"VAERSDATA.csv\", ['VAERS_ID', 'DIED', 'L_THREAT', 'DISABLE']),\n",
|
|
" 'VAERSVAX': read_csv(folder + year + \"VAERSVAX.csv\", ['VAERS_ID', 'VAX_DOSE_SERIES', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'])\n",
|
|
" }\n",
|
|
"\n",
|
|
" return _createDataFrame(\n",
|
|
" [readVaersDescr(\"2021\"), readVaersDescr(\"2022\")],\n",
|
|
" manufacturer)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "99945ca8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def createBatchCodeTable(df : pd.DataFrame):\n",
|
|
" def filter(df, col):\n",
|
|
" return df[df[col] == 'Y'][['VAX_LOT']]\n",
|
|
"\n",
|
|
" batchCodeTableDict = {\n",
|
|
" 'ADRs': df[['VAX_LOT']].value_counts(),\n",
|
|
" 'DEATHS': filter(df, 'DIED').value_counts(),\n",
|
|
" 'DISABILITIES': filter(df, 'DISABLE').value_counts(),\n",
|
|
" 'LIFE THREATENING ILLNESSES': filter(df, 'L_THREAT').value_counts()\n",
|
|
" }\n",
|
|
" return pd.concat(batchCodeTableDict, axis = 1).replace(to_replace = np.nan, value = 0)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3dacedfd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import unittest"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pandas.testing import assert_frame_equal\n",
|
|
"\n",
|
|
"class CreateDataFrameTest(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_createDataFrame(self):\n",
|
|
" # Given\n",
|
|
" vaersDescrs = [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" [ 'DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" {\n",
|
|
" '0916600': ['Y', np.NaN, np.NaN],\n",
|
|
" '0916601': [np.NaN, np.NaN, 'Y']\n",
|
|
" }),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" [ 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" {\n",
|
|
" '0916600': ['COVID19', 'MODERNA', '037K20A'],\n",
|
|
" '0916601': ['COVID19', 'MODERNA', '025L20A']\n",
|
|
" })\n",
|
|
" },\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" [ 'DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" {\n",
|
|
" '1996873': [np.NaN, np.NaN, np.NaN],\n",
|
|
" '1996874': [np.NaN, np.NaN, 'Y']\n",
|
|
" }),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" [ 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" {\n",
|
|
" '1996873': ['HPV9', 'MERCK & CO. INC.', 'R017624'],\n",
|
|
" '1996874': ['COVID19', 'MODERNA', '025L20A']\n",
|
|
" })\n",
|
|
" }\n",
|
|
" ]\n",
|
|
" \n",
|
|
" # When\n",
|
|
" dataFrame = _createDataFrame(vaersDescrs, \"MODERNA\")\n",
|
|
" \n",
|
|
" # Then\n",
|
|
" dataFrameExpected = self.createDataFrame(\n",
|
|
" [ 'DIED', 'L_THREAT', 'DISABLE', 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" {\n",
|
|
" '0916600': ['Y', np.NaN, np.NaN, 'COVID19', 'MODERNA', '037K20A'],\n",
|
|
" '0916601': [np.NaN, np.NaN, 'Y', 'COVID19', 'MODERNA', '025L20A'],\n",
|
|
" '1996874': [np.NaN, np.NaN, 'Y', 'COVID19', 'MODERNA', '025L20A']\n",
|
|
" })\n",
|
|
" assert_frame_equal(dataFrame, dataFrameExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def createDataFrame(self, columns, data):\n",
|
|
" return pd.DataFrame.from_dict(data, columns = columns, orient = 'index')\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 = _createDataFrame(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" [ 'DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" {\n",
|
|
" '0916600': ['Y', np.NaN, np.NaN],\n",
|
|
" '0916601': [np.NaN, np.NaN, 'Y']\n",
|
|
" }),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" [ 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" {\n",
|
|
" '0916600': ['COVID19', 'MODERNA', '037K20A'],\n",
|
|
" '0916601': ['COVID19', 'MODERNA', '025L20A']\n",
|
|
" })\n",
|
|
" },\n",
|
|
" {\n",
|
|
" 'VAERSDATA': self.createDataFrame(\n",
|
|
" [ 'DIED', 'L_THREAT', 'DISABLE'],\n",
|
|
" {\n",
|
|
" '1996873': [np.NaN, np.NaN, np.NaN],\n",
|
|
" '1996874': [np.NaN, np.NaN, 'Y']\n",
|
|
" }),\n",
|
|
" 'VAERSVAX': self.createDataFrame(\n",
|
|
" [ 'VAX_TYPE', 'VAX_MANU', 'VAX_LOT'],\n",
|
|
" {\n",
|
|
" '1996873': ['HPV9', 'MERCK & CO. INC.', 'R017624'],\n",
|
|
" '1996874': ['COVID19', 'MODERNA', '025L20A']\n",
|
|
" })\n",
|
|
" }\n",
|
|
" ],\n",
|
|
" \"MODERNA\")\n",
|
|
"\n",
|
|
" self._test_createBatchCodeTable(dataFrame);\n",
|
|
"\n",
|
|
" def test_createBatchCodeTable(self):\n",
|
|
" self._test_createBatchCodeTable(createDataFrame(\"test/VAERS\", \"MODERNA\"));\n",
|
|
"\n",
|
|
" def _test_createBatchCodeTable(self, dataFrame):\n",
|
|
" # When\n",
|
|
" batchCodeTable = 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.MultiIndex.from_arrays([['025L20A', '037K20A']], names = ('VAX_LOT',)))\n",
|
|
" assert_frame_equal(batchCodeTable, batchCodeTableExpected, check_dtype = False)\n",
|
|
"\n",
|
|
" def createDataFrame(self, columns, data):\n",
|
|
" return pd.DataFrame.from_dict(data, columns = columns, orient = 'index')\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",
|
|
" batchCodeTable = createBatchCodeTable(createDataFrame(\"VAERS\", manufacturer))\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\")"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|