refactoring

This commit is contained in:
frankknoll
2023-10-13 18:55:36 +02:00
parent 334b14fa31
commit bc1406e39f
9 changed files with 172 additions and 74 deletions

View File

@@ -1,5 +0,0 @@
class PrrByVaccineBySymptomFactory:
@staticmethod
def getPrrByVaccineBySymptom(prrByVaccineAndSymptom):
return prrByVaccineAndSymptom.apply(lambda prrByVaccine: prrByVaccine.to_dict())

View File

@@ -1,32 +0,0 @@
import unittest
from pandas.testing import assert_series_equal
from TestHelper import TestHelper
import pandas as pd
from SymptomsCausedByVaccines.PrrByVaccineBySymptomFactory import PrrByVaccineBySymptomFactory
class PrrByVaccineBySymptomFactoryTest(unittest.TestCase):
def test_getPrrByVaccineBySymptom(self):
# Given
prrByVaccineAndSymptom = TestHelper.createDataFrame(
columns = ['11-beta-hydroxylase deficiency', '17-hydroxyprogesterone'],
data = [ [0.6, 1.5],
[0.3, 3.0]],
index = pd.Index(
name = 'VAX_TYPE',
data = [
'6VAX-F',
'ADEN'
]))
# When
prrByVaccineBySymptom = PrrByVaccineBySymptomFactory.getPrrByVaccineBySymptom(prrByVaccineAndSymptom)
# Then
assert_series_equal(
prrByVaccineBySymptom,
pd.Series(
{
'11-beta-hydroxylase deficiency': {'6VAX-F': 0.6, 'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5, 'ADEN': 3.0}
}))

View File

@@ -1,6 +0,0 @@
class PrrByVaccineBySymptomTransformer:
@staticmethod
def removeNonZeroPrrs(prrByVaccineBySymptom):
return prrByVaccineBySymptom.map(
lambda prrByVaccine: {vaccine: prr for vaccine, prr in prrByVaccine.items() if prr != 0})

View File

@@ -1,26 +0,0 @@
import unittest
from pandas.testing import assert_series_equal
import pandas as pd
from SymptomsCausedByVaccines.PrrByVaccineBySymptomTransformer import PrrByVaccineBySymptomTransformer
class PrrByVaccineBySymptomTransformerTest(unittest.TestCase):
def test_filterByNonZeroPrrs(self):
# Given
prrByVaccineBySymptom = pd.Series(
{
'11-beta-hydroxylase deficiency': {'6VAX-F': 0.0, 'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5, 'ADEN': 0.0}
})
# When
prrByVaccineBySymptomWithoutZeroPrrs = PrrByVaccineBySymptomTransformer.removeNonZeroPrrs(prrByVaccineBySymptom)
# Then
assert_series_equal(
prrByVaccineBySymptomWithoutZeroPrrs,
pd.Series(
{
'11-beta-hydroxylase deficiency': {'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5}
}))

View File

@@ -0,0 +1,11 @@
class PrrSeriesFactory:
@staticmethod
def getPrrByVaccineBySymptom(prrByVaccineAndSymptom):
return prrByVaccineAndSymptom.apply(lambda prrByVaccine: prrByVaccine.to_dict())
@staticmethod
def getPrrBySymptomByVaccine(prrByVaccineAndSymptom):
return prrByVaccineAndSymptom.apply(
lambda prrBySymptom: prrBySymptom.to_dict(),
axis = 'columns')

View File

@@ -0,0 +1,58 @@
import unittest
from pandas.testing import assert_series_equal
from TestHelper import TestHelper
import pandas as pd
from SymptomsCausedByVaccines.PrrSeriesFactory import PrrSeriesFactory
class PrrSeriesFactoryTest(unittest.TestCase):
def test_getPrrByVaccineBySymptom(self):
# Given
prrByVaccineAndSymptom = TestHelper.createDataFrame(
columns = ['11-beta-hydroxylase deficiency', '17-hydroxyprogesterone'],
data = [ [0.6, 1.5],
[0.3, 3.0]],
index = pd.Index(
name = 'VAX_TYPE',
data = [
'6VAX-F',
'ADEN'
]))
# When
prrByVaccineBySymptom = PrrSeriesFactory.getPrrByVaccineBySymptom(prrByVaccineAndSymptom)
# Then
assert_series_equal(
prrByVaccineBySymptom,
pd.Series(
{
'11-beta-hydroxylase deficiency': {'6VAX-F': 0.6, 'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5, 'ADEN': 3.0}
}))
def test_getPrrBySymptomByVaccine(self):
# Given
prrByVaccineAndSymptom = TestHelper.createDataFrame(
columns = ['11-beta-hydroxylase deficiency', '17-hydroxyprogesterone'],
data = [ [0.6, 1.5],
[1.3, 2.5]],
index = pd.Index(
name = 'VAX_TYPE',
data = [
'6VAX-F',
'ADEN'
]))
# When
prrBySymptomByVaccine = PrrSeriesFactory.getPrrBySymptomByVaccine(prrByVaccineAndSymptom)
# Then
assert_series_equal(
prrBySymptomByVaccine,
TestHelper.createSeries(
indexName = 'VAX_TYPE',
data = {
'6VAX-F': {'11-beta-hydroxylase deficiency': 0.6, '17-hydroxyprogesterone': 1.5},
'ADEN': {'11-beta-hydroxylase deficiency': 1.3, '17-hydroxyprogesterone': 2.5}
}))

View File

@@ -0,0 +1,18 @@
class PrrSeriesTransformer:
@staticmethod
def filterByNonZeroPrrs(prrByVaccineBySymptom):
return PrrSeriesTransformer._filterPrrsBy(
prrByVaccineBySymptom,
lambda prr: prr != 0)
@staticmethod
def filterByHighPrrs(prrBySymptomByVaccine):
return PrrSeriesTransformer._filterPrrsBy(
prrBySymptomByVaccine,
lambda prr: prr > 1)
@staticmethod
def _filterPrrsBy(prrByKeyByOtherKey, prrFilter):
return prrByKeyByOtherKey.map(
lambda prrByKey: {key: prr for key, prr in prrByKey.items() if prrFilter(prr)})

View File

@@ -0,0 +1,46 @@
import unittest
from pandas.testing import assert_series_equal
import pandas as pd
from SymptomsCausedByVaccines.PrrSeriesTransformer import PrrSeriesTransformer
class PrrSeriesTransformerTest(unittest.TestCase):
def test_filterByNonZeroPrrs(self):
# Given
prrByVaccineBySymptom = pd.Series(
{
'11-beta-hydroxylase deficiency': {'6VAX-F': 0.0, 'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5, 'ADEN': 0.0}
})
# When
prrByVaccineBySymptomWithoutZeroPrrs = PrrSeriesTransformer.filterByNonZeroPrrs(prrByVaccineBySymptom)
# Then
assert_series_equal(
prrByVaccineBySymptomWithoutZeroPrrs,
pd.Series(
{
'11-beta-hydroxylase deficiency': {'ADEN': 0.3},
'17-hydroxyprogesterone': {'6VAX-F': 1.5}
}))
def test_filterByHighPrrs(self):
# Given
prrBySymptomByVaccine = pd.Series(
{
'6VAX-F': {'11-beta-hydroxylase deficiency': 0.6, '17-hydroxyprogesterone': 1.5},
'ADEN': {'11-beta-hydroxylase deficiency': 1.3, '17-hydroxyprogesterone': 0.9}
})
# When
prrBySymptomByVaccineWithHighPrrs = PrrSeriesTransformer.filterByHighPrrs(prrBySymptomByVaccine)
# Then
assert_series_equal(
prrBySymptomByVaccineWithHighPrrs,
pd.Series(
{
'6VAX-F': {'17-hydroxyprogesterone': 1.5},
'ADEN': {'11-beta-hydroxylase deficiency': 1.3}
}))