refactoring
This commit is contained in:
@@ -1,21 +1,19 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
from Utils import get_dictWithKeys_dictWithoutKeys
|
||||||
|
|
||||||
|
|
||||||
class BatchCodeTableIntoHistogramDescriptionTableMerger:
|
class BatchCodeTableIntoHistogramDescriptionTableMerger:
|
||||||
|
|
||||||
# FK-TODO: refactor
|
def __init__(self):
|
||||||
@staticmethod
|
self.HISTOGRAM_DESCRIPTION_columnName = 'HISTOGRAM_DESCRIPTION'
|
||||||
def mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable, histogramDescriptionTable):
|
|
||||||
def merge(src):
|
def mergeBatchCodeTableIntoHistogramDescriptionTable(self, batchCodeTable, histogramDescriptionTable):
|
||||||
dst = src['HISTOGRAM_DESCRIPTION']
|
mergedTable = self._combineTables(batchCodeTable, histogramDescriptionTable)
|
||||||
# dict_3 = {**dict_1, **dict_2}
|
mergedTable = self._merge_columns_into_HISTOGRAM_DESCRIPTION(mergedTable)
|
||||||
dst['Adverse Reaction Reports'] = src['Adverse Reaction Reports']
|
mergedTable['COUNTRY'] = histogramDescriptionTable['COUNTRY']
|
||||||
dst['Deaths'] = src['Deaths']
|
return mergedTable
|
||||||
dst['Disabilities'] = src['Disabilities']
|
|
||||||
dst['Life Threatening Illnesses'] = src['Life Threatening Illnesses']
|
def _combineTables(self, batchCodeTable, histogramDescriptionTable):
|
||||||
dst['Company'] = src['Company']
|
|
||||||
dst['Severe reports'] = src['Severe reports']
|
|
||||||
dst['Lethality'] = src['Lethality']
|
|
||||||
return dst
|
|
||||||
mergedTable = pd.merge(
|
mergedTable = pd.merge(
|
||||||
histogramDescriptionTable,
|
histogramDescriptionTable,
|
||||||
batchCodeTable,
|
batchCodeTable,
|
||||||
@@ -23,8 +21,28 @@ class BatchCodeTableIntoHistogramDescriptionTableMerger:
|
|||||||
left_index=True,
|
left_index=True,
|
||||||
right_index=True,
|
right_index=True,
|
||||||
validate='one_to_one')
|
validate='one_to_one')
|
||||||
mergedTable = mergedTable[['HISTOGRAM_DESCRIPTION', 'Adverse Reaction Reports', 'Deaths', 'Disabilities', 'Life Threatening Illnesses', 'Company', 'Severe reports', 'Lethality']].apply(merge, axis='columns')
|
return mergedTable[
|
||||||
mergedTable.name = 'HISTOGRAM_DESCRIPTION'
|
[
|
||||||
mergedTable = mergedTable.to_frame()
|
self.HISTOGRAM_DESCRIPTION_columnName,
|
||||||
mergedTable['COUNTRY'] = histogramDescriptionTable['COUNTRY']
|
'Adverse Reaction Reports',
|
||||||
return mergedTable
|
'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']))
|
data = ['1808982']))
|
||||||
|
|
||||||
# When
|
# When
|
||||||
mergedTable = BatchCodeTableIntoHistogramDescriptionTableMerger.mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable = batchCodeTable, histogramDescriptionTable = histogramDescriptionTable)
|
mergedTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable(batchCodeTable = batchCodeTable, histogramDescriptionTable = histogramDescriptionTable)
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert_frame_equal(
|
assert_frame_equal(
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ def createAndSaveGlobalHistograms(symptomByBatchcodeTable, batchCodeTable):
|
|||||||
dictByBatchcodeTable = createHistograms(symptomByBatchcodeTable)
|
dictByBatchcodeTable = createHistograms(symptomByBatchcodeTable)
|
||||||
explodedTable = MultiIndexExploder.explodeMultiIndexOfTable(dictByBatchcodeTable)
|
explodedTable = MultiIndexExploder.explodeMultiIndexOfTable(dictByBatchcodeTable)
|
||||||
histogramDescriptionTable = HistogramDescriptionTableFactory.createHistogramDescriptionTable(explodedTable)
|
histogramDescriptionTable = HistogramDescriptionTableFactory.createHistogramDescriptionTable(explodedTable)
|
||||||
histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger.mergeBatchCodeTableIntoHistogramDescriptionTable(
|
histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable(
|
||||||
batchCodeTable = _rearrange(batchCodeTable),
|
batchCodeTable = _rearrange(batchCodeTable),
|
||||||
histogramDescriptionTable = histogramDescriptionTable)
|
histogramDescriptionTable = histogramDescriptionTable)
|
||||||
for country, histogramDescriptionTableForCountry in histogramDescriptionTable.groupby('COUNTRY'):
|
for country, histogramDescriptionTableForCountry in histogramDescriptionTable.groupby('COUNTRY'):
|
||||||
|
|||||||
@@ -8,3 +8,9 @@ def fillLst(lst, desiredLen, fillValue):
|
|||||||
|
|
||||||
def flatten(tuples):
|
def flatten(tuples):
|
||||||
return [item for tuple in tuples for item in tuple]
|
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