refactoring

This commit is contained in:
frankknoll
2023-06-06 18:39:31 +02:00
parent 7cb0d10b88
commit 6e8b980037
4 changed files with 67 additions and 32 deletions

View File

@@ -0,0 +1,25 @@
import pandas as pd
class CountryCountsByBatchcodeTable2BarChartDescriptionsConverter:
@staticmethod
def convert2BarChartDescriptionTable(countryCountsByBatchcodeTable):
return (countryCountsByBatchcodeTable
.reset_index(level = 'COUNTRY')
.rename(
columns =
{
'COUNTRY': 'countries',
'COUNTRY_COUNT_BY_VAX_LOT Clicked': 'frequencies guessed',
'COUNTRY_COUNT_BY_VAX_LOT Before Deletion': 'frequencies before deletion'
})
.groupby('VAX_LOT')
.apply(CountryCountsByBatchcodeTable2BarChartDescriptionsConverter._convert2BarChartDescription)
.rename('BAR_CHART_DESCRIPTION')
.to_frame())
@staticmethod
def _convert2BarChartDescription(countryCountsTable):
barChartDescription = countryCountsTable.to_dict('list')
barChartDescription['batchcode'] = countryCountsTable.index.values[0]
return barChartDescription

View File

@@ -2,11 +2,11 @@ import unittest
import pandas as pd
from pandas.testing import assert_frame_equal
from TestHelper import TestHelper
from CountryCountsByBatchcodeTable2JsonConverter import CountryCountsByBatchcodeTable2JsonConverter
from CountryCountsByBatchcodeTable2BarChartDescriptionsConverter import CountryCountsByBatchcodeTable2BarChartDescriptionsConverter
class CountryCountsByBatchcodeTable2JsonConverterTest(unittest.TestCase):
class CountryCountsByBatchcodeTable2BarChartDescriptionsConverterTest(unittest.TestCase):
def test_convertCountryCountsByBatchcodeTable2Json(self):
def test_convert2BarChartDescriptionTable(self):
# Given
countryCountsByBatchcodeTable = TestHelper.createDataFrame(
columns = ['COUNTRY_COUNT_BY_VAX_LOT Clicked', 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion'],
@@ -20,26 +20,28 @@ class CountryCountsByBatchcodeTable2JsonConverterTest(unittest.TestCase):
('# 009C01A', 'Germany')]))
# When
jsonTable = CountryCountsByBatchcodeTable2JsonConverter.convert2Json(countryCountsByBatchcodeTable)
barChartDescriptionTable = CountryCountsByBatchcodeTable2BarChartDescriptionsConverter.convert2BarChartDescriptionTable(countryCountsByBatchcodeTable)
# Then
assert_frame_equal(
jsonTable,
barChartDescriptionTable,
TestHelper.createDataFrame(
columns = ['HISTOGRAM_DESCRIPTION'],
columns = ['BAR_CHART_DESCRIPTION'],
data = [
[
{
"countries": ["Germany", "Hungary"],
"frequencies guessed": [10, 15],
"frequencies before deletion": [20, 30]
'batchcode': '!D0181',
'countries': ['Germany', 'Hungary'],
'frequencies guessed': [10, 15],
'frequencies before deletion': [20, 30]
}
],
[
{
"countries": ["Germany"],
"frequencies guessed": [70],
"frequencies before deletion": [80]
'batchcode': '# 009C01A',
'countries': ['Germany'],
'frequencies guessed': [70],
'frequencies before deletion': [80]
}
]
],

View File

@@ -1,19 +0,0 @@
import pandas as pd
class CountryCountsByBatchcodeTable2JsonConverter:
@staticmethod
def convert2Json(countryCountsByBatchcodeTable):
return (countryCountsByBatchcodeTable
.reset_index(level = 'COUNTRY')
.rename(
columns =
{
'COUNTRY': 'countries',
'COUNTRY_COUNT_BY_VAX_LOT Clicked': 'frequencies guessed',
'COUNTRY_COUNT_BY_VAX_LOT Before Deletion': 'frequencies before deletion'
})
.groupby('VAX_LOT')
.apply(lambda countryCountsTable: countryCountsTable.to_dict('list'))
.rename('HISTOGRAM_DESCRIPTION')
.to_frame())

View File

@@ -167,12 +167,39 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dd9fb2b0",
"id": "29df3cfc",
"metadata": {},
"outputs": [],
"source": [
"countryCountsByBatchcode.to_excel('tmp/countryCountsByBatchcode.xlsx')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf55ee83",
"metadata": {},
"outputs": [],
"source": [
"from CountryCountsByBatchcodeTable2BarChartDescriptionsConverter import CountryCountsByBatchcodeTable2BarChartDescriptionsConverter\n",
"\n",
"barChartDescriptionTable = CountryCountsByBatchcodeTable2BarChartDescriptionsConverter.convert2BarChartDescriptionTable(countryCountsByBatchcode)\n",
"barChartDescriptionTable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce1b2189",
"metadata": {},
"outputs": [],
"source": [
"from IOUtils import IOUtils\n",
"\n",
"IOUtils.saveDataFrameAsJson(\n",
" barChartDescriptionTable,\n",
" '../docs/data/barChartDescriptionTable.json')"
]
}
],
"metadata": {