refactoring
This commit is contained in:
@@ -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
|
||||||
@@ -2,11 +2,11 @@ import unittest
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pandas.testing import assert_frame_equal
|
from pandas.testing import assert_frame_equal
|
||||||
from TestHelper import TestHelper
|
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
|
# Given
|
||||||
countryCountsByBatchcodeTable = TestHelper.createDataFrame(
|
countryCountsByBatchcodeTable = TestHelper.createDataFrame(
|
||||||
columns = ['COUNTRY_COUNT_BY_VAX_LOT Clicked', 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion'],
|
columns = ['COUNTRY_COUNT_BY_VAX_LOT Clicked', 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion'],
|
||||||
@@ -20,26 +20,28 @@ class CountryCountsByBatchcodeTable2JsonConverterTest(unittest.TestCase):
|
|||||||
('# 009C01A', 'Germany')]))
|
('# 009C01A', 'Germany')]))
|
||||||
|
|
||||||
# When
|
# When
|
||||||
jsonTable = CountryCountsByBatchcodeTable2JsonConverter.convert2Json(countryCountsByBatchcodeTable)
|
barChartDescriptionTable = CountryCountsByBatchcodeTable2BarChartDescriptionsConverter.convert2BarChartDescriptionTable(countryCountsByBatchcodeTable)
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert_frame_equal(
|
assert_frame_equal(
|
||||||
jsonTable,
|
barChartDescriptionTable,
|
||||||
TestHelper.createDataFrame(
|
TestHelper.createDataFrame(
|
||||||
columns = ['HISTOGRAM_DESCRIPTION'],
|
columns = ['BAR_CHART_DESCRIPTION'],
|
||||||
data = [
|
data = [
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"countries": ["Germany", "Hungary"],
|
'batchcode': '!D0181',
|
||||||
"frequencies guessed": [10, 15],
|
'countries': ['Germany', 'Hungary'],
|
||||||
"frequencies before deletion": [20, 30]
|
'frequencies guessed': [10, 15],
|
||||||
|
'frequencies before deletion': [20, 30]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"countries": ["Germany"],
|
'batchcode': '# 009C01A',
|
||||||
"frequencies guessed": [70],
|
'countries': ['Germany'],
|
||||||
"frequencies before deletion": [80]
|
'frequencies guessed': [70],
|
||||||
|
'frequencies before deletion': [80]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@@ -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())
|
|
||||||
@@ -167,12 +167,39 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "dd9fb2b0",
|
"id": "29df3cfc",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"countryCountsByBatchcode.to_excel('tmp/countryCountsByBatchcode.xlsx')"
|
"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": {
|
"metadata": {
|
||||||
|
|||||||
Reference in New Issue
Block a user