refactoring
This commit is contained in:
@@ -1,30 +1,48 @@
|
||||
import pandas as pd
|
||||
from Utils import get_dictWithKeys_dictWithoutKeys
|
||||
|
||||
|
||||
class BatchCodeTableIntoHistogramDescriptionTableMerger:
|
||||
|
||||
# FK-TODO: refactor
|
||||
@staticmethod
|
||||
def mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable, histogramDescriptionTable):
|
||||
def merge(src):
|
||||
dst = src['HISTOGRAM_DESCRIPTION']
|
||||
# dict_3 = {**dict_1, **dict_2}
|
||||
dst['Adverse Reaction Reports'] = src['Adverse Reaction Reports']
|
||||
dst['Deaths'] = src['Deaths']
|
||||
dst['Disabilities'] = src['Disabilities']
|
||||
dst['Life Threatening Illnesses'] = src['Life Threatening Illnesses']
|
||||
dst['Company'] = src['Company']
|
||||
dst['Severe reports'] = src['Severe reports']
|
||||
dst['Lethality'] = src['Lethality']
|
||||
return dst
|
||||
mergedTable = pd.merge(
|
||||
histogramDescriptionTable,
|
||||
batchCodeTable,
|
||||
how = 'left',
|
||||
left_index = True,
|
||||
right_index = True,
|
||||
validate = 'one_to_one')
|
||||
mergedTable = mergedTable[['HISTOGRAM_DESCRIPTION', 'Adverse Reaction Reports', 'Deaths', 'Disabilities', 'Life Threatening Illnesses', 'Company', 'Severe reports', 'Lethality']].apply(merge, axis='columns')
|
||||
mergedTable.name = 'HISTOGRAM_DESCRIPTION'
|
||||
mergedTable = mergedTable.to_frame()
|
||||
def __init__(self):
|
||||
self.HISTOGRAM_DESCRIPTION_columnName = 'HISTOGRAM_DESCRIPTION'
|
||||
|
||||
def mergeBatchCodeTableIntoHistogramDescriptionTable(self, batchCodeTable, histogramDescriptionTable):
|
||||
mergedTable = self._combineTables(batchCodeTable, histogramDescriptionTable)
|
||||
mergedTable = self._merge_columns_into_HISTOGRAM_DESCRIPTION(mergedTable)
|
||||
mergedTable['COUNTRY'] = histogramDescriptionTable['COUNTRY']
|
||||
return mergedTable
|
||||
|
||||
def _combineTables(self, batchCodeTable, histogramDescriptionTable):
|
||||
mergedTable = pd.merge(
|
||||
histogramDescriptionTable,
|
||||
batchCodeTable,
|
||||
how='left',
|
||||
left_index=True,
|
||||
right_index=True,
|
||||
validate='one_to_one')
|
||||
return mergedTable[
|
||||
[
|
||||
self.HISTOGRAM_DESCRIPTION_columnName,
|
||||
'Adverse Reaction Reports',
|
||||
'Deaths',
|
||||
'Disabilities',
|
||||
'Life Threatening Illnesses',
|
||||
'Company',
|
||||
'Severe reports',
|
||||
'Lethality'
|
||||
]]
|
||||
|
||||
def _merge_columns_into_HISTOGRAM_DESCRIPTION(self, table):
|
||||
table = table.apply(
|
||||
self.__merge_columns_into_HISTOGRAM_DESCRIPTION,
|
||||
axis='columns')
|
||||
table.name = self.HISTOGRAM_DESCRIPTION_columnName
|
||||
return table.to_frame()
|
||||
|
||||
def __merge_columns_into_HISTOGRAM_DESCRIPTION(self, src):
|
||||
dict_with_HISTOGRAM_DESCRIPTION, dict_without_HISTOGRAM_DESCRIPTION = get_dictWithKeys_dictWithoutKeys(
|
||||
src.to_dict(),
|
||||
{self.HISTOGRAM_DESCRIPTION_columnName})
|
||||
HISTOGRAM_DESCRIPTION = dict_with_HISTOGRAM_DESCRIPTION[self.HISTOGRAM_DESCRIPTION_columnName]
|
||||
return {**HISTOGRAM_DESCRIPTION, **dict_without_HISTOGRAM_DESCRIPTION}
|
||||
|
||||
@@ -40,7 +40,7 @@ class BatchCodeTableIntoHistogramDescriptionTableMergerTest(unittest.TestCase):
|
||||
data = ['1808982']))
|
||||
|
||||
# When
|
||||
mergedTable = BatchCodeTableIntoHistogramDescriptionTableMerger.mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable = batchCodeTable, histogramDescriptionTable = histogramDescriptionTable)
|
||||
mergedTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable = batchCodeTable, histogramDescriptionTable = histogramDescriptionTable)
|
||||
|
||||
# Then
|
||||
assert_frame_equal(
|
||||
|
||||
@@ -10,7 +10,7 @@ def createAndSaveGlobalHistograms(symptomByBatchcodeTable, batchCodeTable):
|
||||
dictByBatchcodeTable = createHistograms(symptomByBatchcodeTable)
|
||||
explodedTable = MultiIndexExploder.explodeMultiIndexOfTable(dictByBatchcodeTable)
|
||||
histogramDescriptionTable = HistogramDescriptionTableFactory.createHistogramDescriptionTable(explodedTable)
|
||||
histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger.mergeBatchCodeTableIntoHistogramDescriptionTable(
|
||||
histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable(
|
||||
batchCodeTable = _rearrange(batchCodeTable),
|
||||
histogramDescriptionTable = histogramDescriptionTable)
|
||||
for country, histogramDescriptionTableForCountry in histogramDescriptionTable.groupby('COUNTRY'):
|
||||
|
||||
@@ -8,3 +8,9 @@ def fillLst(lst, desiredLen, fillValue):
|
||||
|
||||
def flatten(tuples):
|
||||
return [item for tuple in tuples for item in tuple]
|
||||
|
||||
|
||||
def get_dictWithKeys_dictWithoutKeys(dict, keys):
|
||||
dictWithKeys = {key: dict[key] for key in keys}
|
||||
dictWithoutKeys = {key: dict[key] for key in dict.keys() - keys}
|
||||
return dictWithKeys, dictWithoutKeys
|
||||
|
||||
Reference in New Issue
Block a user