refactoring
This commit is contained in:
@@ -2,12 +2,6 @@ from SymptomHistogramByBatchcodeTableFactory import SymptomHistogramByBatchcodeT
|
||||
from HistogramTable2DictTableConverter import HistogramTable2DictTableConverter
|
||||
|
||||
|
||||
def createGlobalHistograms(symptomByBatchcodeTable):
|
||||
symptomHistogramByBatchcodeTable = SymptomHistogramByBatchcodeTableFactory.createGlobalSymptomHistogramByBatchcodeTable(symptomByBatchcodeTable)
|
||||
dictByBatchcodeTable = HistogramTable2DictTableConverter.convertGlobalHistogramTable2DictTable(symptomHistogramByBatchcodeTable)
|
||||
return dictByBatchcodeTable
|
||||
|
||||
|
||||
def createHistograms(symptomByBatchcodeTable):
|
||||
symptomHistogramByBatchcodeTable = SymptomHistogramByBatchcodeTableFactory.createSymptomHistogramByBatchcodeTable(symptomByBatchcodeTable)
|
||||
dictByBatchcodeTable = HistogramTable2DictTableConverter.convertHistogramTable2DictTable(symptomHistogramByBatchcodeTable)
|
||||
|
||||
@@ -9,13 +9,6 @@ class HistogramTable2DictTableConverter:
|
||||
.reset_index(level = 'COUNTRY')
|
||||
[['SYMPTOM_COUNT_BY_VAX_LOT', 'COUNTRY']])
|
||||
|
||||
@staticmethod
|
||||
def convertGlobalHistogramTable2DictTable(globalSymptomHistogramByBatchcodeTable):
|
||||
vax_lot_columns = globalSymptomHistogramByBatchcodeTable.index.names.difference(['SYMPTOM'])
|
||||
return (globalSymptomHistogramByBatchcodeTable
|
||||
.groupby(vax_lot_columns)
|
||||
.agg(lambda histogram_with_vax_lots: HistogramTable2DictTableConverter._histogram_to_json(histogram_with_vax_lots, vax_lot_columns)))
|
||||
|
||||
@staticmethod
|
||||
def _histogram_to_json(histogram_with_vax_lots, vax_lot_columns):
|
||||
histogram = histogram_with_vax_lots.reset_index(level = vax_lot_columns, drop = True)
|
||||
|
||||
@@ -93,10 +93,10 @@ class HistogramTable2DictTableConverterTest(unittest.TestCase):
|
||||
def test_convertGlobalHistogramTable2DictTable(self):
|
||||
# Given
|
||||
globalHistogramTable = TestHelper.createDataFrame(
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT'],
|
||||
data = [ [5],
|
||||
[1],
|
||||
[2]],
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT', 'COUNTRY'],
|
||||
data = [ [5, 'Global'],
|
||||
[1, 'Global'],
|
||||
[2, 'Global']],
|
||||
index = pd.MultiIndex.from_tuples(
|
||||
names = ['VAX_LOT1', 'SYMPTOM'],
|
||||
tuples = [['1808982', 'Blood pressure orthostatic abnormal'],
|
||||
@@ -104,23 +104,25 @@ class HistogramTable2DictTableConverterTest(unittest.TestCase):
|
||||
['EW0175', 'Chest discomfort']]))
|
||||
|
||||
# When
|
||||
dictTable = HistogramTable2DictTableConverter.convertGlobalHistogramTable2DictTable(globalHistogramTable)
|
||||
dictTable = HistogramTable2DictTableConverter.convertHistogramTable2DictTable(globalHistogramTable)
|
||||
|
||||
# Then
|
||||
assert_frame_equal(
|
||||
dictTable,
|
||||
TestHelper.createDataFrame(
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT'],
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT', 'COUNTRY'],
|
||||
data = [ [
|
||||
{
|
||||
"Blood pressure orthostatic abnormal": 5,
|
||||
"Chest discomfort": 1
|
||||
}
|
||||
},
|
||||
'Global'
|
||||
],
|
||||
[
|
||||
{
|
||||
"Chest discomfort": 2
|
||||
}
|
||||
},
|
||||
'Global'
|
||||
]],
|
||||
index = pd.Index(
|
||||
name = 'VAX_LOT1',
|
||||
|
||||
@@ -8,10 +8,3 @@ class SymptomHistogramByBatchcodeTableFactory:
|
||||
.to_frame(name = 'SYMPTOM_COUNT_BY_VAX_LOT')
|
||||
.reset_index(level = 'COUNTRY')
|
||||
[['SYMPTOM_COUNT_BY_VAX_LOT', 'COUNTRY']])
|
||||
|
||||
@staticmethod
|
||||
def createGlobalSymptomHistogramByBatchcodeTable(symptomByBatchcodeTable):
|
||||
return (symptomByBatchcodeTable
|
||||
.groupby(symptomByBatchcodeTable.index.names)
|
||||
['SYMPTOM'].value_counts()
|
||||
.to_frame(name = 'SYMPTOM_COUNT_BY_VAX_LOT'))
|
||||
|
||||
@@ -67,24 +67,24 @@ class SymptomHistogramByBatchcodeTableFactoryTest(unittest.TestCase):
|
||||
# Given
|
||||
symptomByBatchcodeTable = TestHelper.createDataFrame(
|
||||
columns = ['SYMPTOM', 'COUNTRY'],
|
||||
data = [ ['Blood pressure orthostatic abnormal', 'Germany'],
|
||||
['Blood pressure orthostatic abnormal', 'Germany'],
|
||||
['Blood pressure orthostatic abnormal', 'Russian Federation'],
|
||||
['Headache', 'Germany']],
|
||||
data = [ ['Blood pressure orthostatic abnormal', 'Global'],
|
||||
['Blood pressure orthostatic abnormal', 'Global'],
|
||||
['Blood pressure orthostatic abnormal', 'Global'],
|
||||
['Headache', 'Global']],
|
||||
index = pd.MultiIndex.from_tuples(
|
||||
names = ['VAX_LOT1', 'VAX_LOT2'],
|
||||
tuples = [['1808982', 'EW0175']] * 4))
|
||||
|
||||
# When
|
||||
globalSymptomHistogramByBatchcodeTable = SymptomHistogramByBatchcodeTableFactory.createGlobalSymptomHistogramByBatchcodeTable(symptomByBatchcodeTable)
|
||||
globalSymptomHistogramByBatchcodeTable = SymptomHistogramByBatchcodeTableFactory.createSymptomHistogramByBatchcodeTable(symptomByBatchcodeTable)
|
||||
|
||||
# Then
|
||||
assert_frame_equal(
|
||||
globalSymptomHistogramByBatchcodeTable,
|
||||
TestHelper.createDataFrame(
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT'],
|
||||
data = [ [3],
|
||||
[1]],
|
||||
columns = ['SYMPTOM_COUNT_BY_VAX_LOT', 'COUNTRY'],
|
||||
data = [ [3, 'Global'],
|
||||
[1, 'Global']],
|
||||
index = pd.MultiIndex.from_tuples(
|
||||
names = ['VAX_LOT1', 'VAX_LOT2', 'SYMPTOM'],
|
||||
tuples = [['1808982', 'EW0175', 'Blood pressure orthostatic abnormal'],
|
||||
|
||||
Reference in New Issue
Block a user