From e7463962c16af2a4d5ce4e312f9f01705880370e Mon Sep 17 00:00:00 2001 From: frankknoll Date: Thu, 16 Feb 2023 16:11:48 +0100 Subject: [PATCH] refactoring --- src/HistogramFactory.py | 6 ------ src/HistogramTable2DictTableConverter.py | 7 ------- src/HistogramTable2DictTableConverterTest.py | 18 ++++++++++-------- src/SymptomHistogramByBatchcodeTableFactory.py | 7 ------- ...ptomHistogramByBatchcodeTableFactoryTest.py | 16 ++++++++-------- 5 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/HistogramFactory.py b/src/HistogramFactory.py index 29ca053a0a6..5e8a519ad01 100644 --- a/src/HistogramFactory.py +++ b/src/HistogramFactory.py @@ -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) diff --git a/src/HistogramTable2DictTableConverter.py b/src/HistogramTable2DictTableConverter.py index 04cadab45fe..4d1db1d8b1b 100644 --- a/src/HistogramTable2DictTableConverter.py +++ b/src/HistogramTable2DictTableConverter.py @@ -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) diff --git a/src/HistogramTable2DictTableConverterTest.py b/src/HistogramTable2DictTableConverterTest.py index d8524151ff5..e18a0ea21e8 100644 --- a/src/HistogramTable2DictTableConverterTest.py +++ b/src/HistogramTable2DictTableConverterTest.py @@ -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', diff --git a/src/SymptomHistogramByBatchcodeTableFactory.py b/src/SymptomHistogramByBatchcodeTableFactory.py index 4b109fe0f04..4f701179b88 100644 --- a/src/SymptomHistogramByBatchcodeTableFactory.py +++ b/src/SymptomHistogramByBatchcodeTableFactory.py @@ -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')) diff --git a/src/SymptomHistogramByBatchcodeTableFactoryTest.py b/src/SymptomHistogramByBatchcodeTableFactoryTest.py index a925952b45a..23e171d6e95 100644 --- a/src/SymptomHistogramByBatchcodeTableFactoryTest.py +++ b/src/SymptomHistogramByBatchcodeTableFactoryTest.py @@ -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'],