continuing

This commit is contained in:
frankknoll
2023-06-06 23:37:24 +02:00
parent 50659a8b82
commit 3b5412ec37
3 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import shutil
from IOUtils import IOUtils
def saveBarChartDescriptionTable(barChartDescriptionTable):
directory = '../docs/data/barChartDescriptionTables'
shutil.rmtree(directory, ignore_errors = True)
for row in barChartDescriptionTable.itertuples():
batchcode = row.BAR_CHART_DESCRIPTION['batchcode']
barChartDescription = row.BAR_CHART_DESCRIPTION
IOUtils.saveDictAsJson(barChartDescription, f'{directory}/{batchcode}.json')

View File

@@ -0,0 +1,25 @@
import pandas as pd
class CountryCountsByBatchcodeTable2BarChartDescriptionTableConverter:
@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(CountryCountsByBatchcodeTable2BarChartDescriptionTableConverter._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

@@ -0,0 +1,54 @@
import unittest
import pandas as pd
from pandas.testing import assert_frame_equal
from TestHelper import TestHelper
from CountryCountsByBatchcodeTable2BarChartDescriptionTableConverter import CountryCountsByBatchcodeTable2BarChartDescriptionTableConverter
class CountryCountsByBatchcodeTable2BarChartDescriptionTableConverterTest(unittest.TestCase):
def test_convert2BarChartDescriptionTable(self):
# Given
countryCountsByBatchcodeTable = TestHelper.createDataFrame(
columns = ['COUNTRY_COUNT_BY_VAX_LOT Clicked', 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion'],
data = [ [10, 20],
[15, 30],
[70, 80]],
index = pd.MultiIndex.from_tuples(
names = ['VAX_LOT', 'COUNTRY'],
tuples = [('!D0181', 'Germany'),
('!D0181', 'Hungary'),
('# 009C01A', 'Germany')]))
# When
barChartDescriptionTable = CountryCountsByBatchcodeTable2BarChartDescriptionTableConverter.convert2BarChartDescriptionTable(countryCountsByBatchcodeTable)
# Then
assert_frame_equal(
barChartDescriptionTable,
TestHelper.createDataFrame(
columns = ['BAR_CHART_DESCRIPTION'],
data = [
[
{
'batchcode': '!D0181',
'countries': ['Germany', 'Hungary'],
'frequencies guessed': [10, 15],
'frequencies before deletion': [20, 30]
}
],
[
{
'batchcode': '# 009C01A',
'countries': ['Germany'],
'frequencies guessed': [70],
'frequencies before deletion': [80]
}
]
],
index = pd.Index(
[
'!D0181',
'# 009C01A'
],
name = 'VAX_LOT')),
check_dtype = True)