minifying histogram JSON files

This commit is contained in:
frankknoll
2023-04-19 09:39:29 +02:00
parent 16c3779f90
commit 099fdd52aa
8 changed files with 81 additions and 15 deletions

View File

@@ -1,13 +1,6 @@
class HistoDescrsProvider { class HistoDescrsProvider {
static getHistoDescrs(batchcode) { static getHistoDescrs(batchcode) {
return fetch(`data/histograms/Global/${batchcode}.json`) return fetch(`data/histograms/Global/${batchcode}.json`).then(response => response.json())
.then(response => response.json())
.then(histoDescrs => {
histoDescrs.histograms.sort((histoDescr1, histoDescr2) => histoDescr1.batchcodes.length - histoDescr2.batchcodes.length);
histoDescrs.histogram = histoDescrs.histograms[0].histogram;
delete histoDescrs.histograms;
return histoDescrs;
});
} }
} }

View File

@@ -28,9 +28,7 @@ class BatchCodeTableIntoHistogramDescriptionTableMerger:
'Deaths', 'Deaths',
'Disabilities', 'Disabilities',
'Life Threatening Illnesses', 'Life Threatening Illnesses',
'Company', 'Company'
'Severe reports',
'Lethality'
]] ]]
def _merge_columns_into_HISTOGRAM_DESCRIPTION(self, table): def _merge_columns_into_HISTOGRAM_DESCRIPTION(self, table):

View File

@@ -55,8 +55,6 @@ class BatchCodeTableIntoHistogramDescriptionTableMergerTest(unittest.TestCase):
'Disabilities': 2, 'Disabilities': 2,
'Life Threatening Illnesses': 2, 'Life Threatening Illnesses': 2,
'Company': 'MODERNA', 'Company': 'MODERNA',
'Severe reports': 2/2 * 100,
'Lethality': np.nan,
'histograms': [ 'histograms': [
{ {
'batchcodes': ['1808982', 'EW0175', 'FD1921'], 'batchcodes': ['1808982', 'EW0175', 'FD1921'],

View File

@@ -0,0 +1,25 @@
import warnings
class HistogramDescriptionTableSelector:
@staticmethod
def selectHistogramsWithShortestBatchcodeCombinations(histogramDescriptionTable):
histogramDescriptionTable['HISTOGRAM_DESCRIPTION'] = histogramDescriptionTable['HISTOGRAM_DESCRIPTION'].map(HistogramDescriptionTableSelector._selectHistogramWithShortestBatchcodeCombination)
return histogramDescriptionTable
@staticmethod
def _selectHistogramWithShortestBatchcodeCombination(histoDescr):
return {
"batchcode": histoDescr["batchcode"],
"histogram": HistogramDescriptionTableSelector._getHistogramWithShortestBatchcodeCombination(histoDescr)
}
@staticmethod
def _getHistogramWithShortestBatchcodeCombination(histoDescr):
histogramsSortedByShortestBatchcodeCombination = sorted(
histoDescr["histograms"],
key = lambda histogram: len(histogram["batchcodes"]))
histogramWithShortestBatchcodeCombination = histogramsSortedByShortestBatchcodeCombination[0]
if len(histogramWithShortestBatchcodeCombination["batchcodes"]) != 1:
warnings.warn(f"batchcode {histoDescr['batchcode']} has non unique batchcode combination {histogramWithShortestBatchcodeCombination['batchcodes']} for it's histogram")
return histogramWithShortestBatchcodeCombination["histogram"]

View File

@@ -0,0 +1,51 @@
import unittest
from pandas.testing import assert_frame_equal
from HistogramDescriptionTableSelector import HistogramDescriptionTableSelector
from TestHelper import TestHelper
import pandas as pd
class HistogramDescriptionTableSelectorTest(unittest.TestCase):
def test_selectHistogramsWithShortestBatchcodeCombinations(self):
# Given
histogramDescriptionTable = TestHelper.createDataFrame(
columns = ['HISTOGRAM_DESCRIPTION'],
data = [ [
{
"batchcode": "1808982",
"histograms": [
{
"batchcodes": ["1808982", "EW0175", "FD1921"],
"histogram": {"Blood pressure orthostatic abnormal": 5, "Chest discomfort": 1}
},
{
"batchcodes": ["015M20A", "1808982"],
"histogram": {"Chest discomfort": 2}
}
]
}
]
],
index = pd.Index(
name = 'VAX_LOT',
data = ['1808982']))
# When
histogramsWithShortestBatchcodeCombinationsTable = HistogramDescriptionTableSelector.selectHistogramsWithShortestBatchcodeCombinations(histogramDescriptionTable)
# Then
assert_frame_equal(
histogramsWithShortestBatchcodeCombinationsTable,
TestHelper.createDataFrame(
columns = ['HISTOGRAM_DESCRIPTION'],
data = [ [
{
"batchcode": "1808982",
"histogram": {"Chest discomfort": 2}
}
]
],
index = pd.Index(
name = 'VAX_LOT',
data = ['1808982'])))

View File

@@ -2,6 +2,7 @@ from HistogramFactory import createHistograms
from HistogramPersister import saveHistograms from HistogramPersister import saveHistograms
from MultiIndexExploder import MultiIndexExploder from MultiIndexExploder import MultiIndexExploder
from HistogramDescriptionTableFactory import HistogramDescriptionTableFactory from HistogramDescriptionTableFactory import HistogramDescriptionTableFactory
from HistogramDescriptionTableSelector import HistogramDescriptionTableSelector
from BatchCodeTableIntoHistogramDescriptionTableMerger import BatchCodeTableIntoHistogramDescriptionTableMerger from BatchCodeTableIntoHistogramDescriptionTableMerger import BatchCodeTableIntoHistogramDescriptionTableMerger
@@ -10,6 +11,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 = HistogramDescriptionTableSelector.selectHistogramsWithShortestBatchcodeCombinations(histogramDescriptionTable)
histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable( histogramDescriptionTable = BatchCodeTableIntoHistogramDescriptionTableMerger().mergeBatchCodeTableIntoHistogramDescriptionTable(
batchCodeTable = _rearrange(batchCodeTable), batchCodeTable = _rearrange(batchCodeTable),
histogramDescriptionTable = histogramDescriptionTable) histogramDescriptionTable = histogramDescriptionTable)

View File

@@ -120,7 +120,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"batchCodeTable = createGlobalBatchCodeTable(\n", "batchCodeTable = createGlobalBatchCodeTable(\n",
" deleteEntriesWithADRsLessThanOrEqual = 1,\n", " deleteEntriesWithADRsLessThanOrEqual = 2,\n",
" minADRsForLethality = 100,\n", " minADRsForLethality = 100,\n",
" batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19))\n", " batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19))\n",
"batchCodeTable" "batchCodeTable"

View File

@@ -4,7 +4,6 @@ FK-FIXME:
FK-TODO: FK-TODO:
- add google captcha to batchCodeTable.html - add google captcha to batchCodeTable.html
- aus den JSON-Dateien lethality und severity entfernen.
anacron job: anacron job:
sudo cp src/intensivstationen_howbadismybatch.sh /etc/cron.daily/intensivstationen_howbadismybatch sudo cp src/intensivstationen_howbadismybatch.sh /etc/cron.daily/intensivstationen_howbadismybatch