add recommender to repository

This commit is contained in:
hannes.kuchelmeister
2020-05-04 10:49:59 +02:00
parent 67ae83e0bd
commit 719f3d5ea7
51 changed files with 5792 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
from model.configuration_model import ConfigurationModel, ConfigurationVariablesModel
import math
import pytest
class TestConfigurationModel:
def test_simple_parsing(self):
data = {
'configuration': ['code1', 'code2'],
'variables': [
{
'code': 'abc',
'value': 1
}
]
}
conf = ConfigurationModel(data)
assert len(conf.configuration) == 2
assert len(conf.variables) == 1
class TestConfigurationVariableModel:
def test_simple_parsing(self):
data = {
'code': 'abc',
'value': 1
}
var = ConfigurationVariablesModel(data)
assert var.code == 'abc'
assert var.value == 1

View File

@@ -0,0 +1,113 @@
from model.preferences_model import Preferences, Rating, UserPreference
import math
import pytest
class TestRating:
def test_range_conversion_minus_one_to_zero(self):
data = {
'code': 'abs',
'value': 0
}
assert math.isclose(0, Rating(data).getValue())
def test_range_conversion_one_to_one(self):
data = {
'code': 'abs',
'value': 1
}
assert math.isclose(1, Rating(data).getValue())
def test_range_conversion_zero_to_half(self):
data = {
'code': 'abs',
'value': 0.5
}
assert math.isclose(0.5, Rating(data).getValue())
def test_value_to_large(self):
with pytest.raises(ValueError):
data = {
'code': 'abs',
'value': 1.1
}
rating = Rating(data)
def test_value_to_small(self):
with pytest.raises(ValueError):
data = {
'code': 'abs',
'value': -0.1
}
rating = Rating(data)
class TestUserPreference:
data = {
'user': "user0",
'ratings':[ {
'code': 'abs',
'value': 0
}, {
'code': '2',
'value': 1
}, {
'code': '3',
'value': 0.5
}
]
}
def test_get_all_ratings(self):
user_pref = UserPreference(self.data)
assert len(user_pref.getAllRatings()) == 3
def test_get_rating_by_code(self):
user_pref = UserPreference(self.data)
rating = user_pref.getRatingByCode('2')
assert rating.code == '2'
assert rating.getValue() == 1
def test_get_rating_by_code_default(self):
user_pref = UserPreference(self.data)
rating = user_pref.getRatingByCode('notFOUND')
assert rating.code == 'notFOUND'
assert rating.getValue() == 0.5
class TestPreferences:
preferences = Preferences({
'preferences': [
{
'user': "user0",
'ratings':[ {
'code': 'in_both',
'value': 0
}, {
'code': 'only_in_one',
'value': 1
}, {
'code': '3',
'value': 0.5
}
]
},
{
'user': "user1",
'ratings':[ {
'code': 'in_both',
'value': 1
}, {
'code': '3',
'value': 1
}
]
}
]
})
def test_get_all_user_preferences(self):
assert len(self.preferences.getAllUserPreferences()) == 2
def test_get_all_rating_by_code(self):
assert len(self.preferences.getAllRatingsByCode('only_in_one')) == 2
assert len(self.preferences.getAllRatingsByCode('in_both')) == 2
def test_get_all_users(self):
assert len(self.preferences.getAllUsers()) == 2
assert "user0" in self.preferences.getAllUsers()
assert "user1" in self.preferences.getAllUsers()
def test_get_rating_by_user_and_code(self):
assert self.preferences.getRatingValueByUserAndCode("user0", "only_in_one") == 1
def test_empty_preferences(self):
assert len(Preferences({ 'preferences' : []}).getAllUsers()) == 0
def test_getIndividual_preferences(self):
assert len(self.preferences.getIndividualPreferences()) == 2

View File

@@ -0,0 +1,100 @@
from model.product_structure_model import ProductStructureModel, ProductStructureElementModel, ProductStructureTypeEnum
class TestProductStructureModelEnum:
def test_string_characteristic(self):
assert ProductStructureTypeEnum['CHARACTERISTIC'] == ProductStructureTypeEnum.CHARACTERISTIC
def test_string_cluster(self):
assert ProductStructureTypeEnum['CLUSTER'] == ProductStructureTypeEnum.CLUSTER
def test_string_variable(self):
assert ProductStructureTypeEnum['VARIABLE'] == ProductStructureTypeEnum.VARIABLE
def test_string_feature(self):
assert ProductStructureTypeEnum['FEATURE'] == ProductStructureTypeEnum.FEATURE
class TestProductStructureElementModel:
def test_get_list_of_all(self):
data = {
'elementId': 'parent',
'name': 'parent_element',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'child',
'name': 'child',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
}
element = ProductStructureElementModel(data)
assert len(element.get_list_of_all(ProductStructureTypeEnum.CHARACTERISTIC)) == 1
class TestProductStructureModel:
def test_get_list_of_features(self):
data = {
'ProductStructure': [
{
'elementId': 'parent',
'name': 'parent_element',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'child',
'name': 'child',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
}
]
}
ps_structure = ProductStructureModel(data)
assert len(ps_structure.get_list_of_features()) == 1
def test_get_list_of_characteristics(self):
data = {
'ProductStructure': [
{
'elementId': 'parent',
'name': 'parent_element',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'child',
'name': 'child',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
}
]
}
ps_structure = ProductStructureModel(data)
assert len(ps_structure.get_list_of_characteristics()) == 1
def test_is_characteristic(self):
data = {
'ProductStructure': [
{
'elementId': 'parent',
'name': 'parent_element',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'child',
'name': 'child',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
}
]
}
ps_structure = ProductStructureModel(data)
assert ps_structure.isCharacteristic('child') == True
assert ps_structure.isCharacteristic('parent') == False

View File

@@ -0,0 +1,14 @@
from scoring.list_functions import Average, Product
import math
class TestListToValueFunctionAverage:
def test_simple_average(self):
function = Average()
list = [0.0, 1.0]
assert math.isclose(0.5, function.convertToFloat(list))
class TestListToValueFunctionProduct:
def test_simple_product(self):
function = Product()
list = [0.5, 0.5]
assert math.isclose(0.25, function.convertToFloat(list))

View File

@@ -0,0 +1,114 @@
from scoring.preferences_functions import PerUserPerFeatureDistanceAverageToListFunction, SimplePerUserToListFunction, PreferencesToListFunction, FlattenPreferencesToListFunction
from scoring.list_functions import Min
from model.configuration_model import ConfigurationModel
from model.preferences_model import Preferences
from model.product_structure_model import ProductStructureModel
preferences = Preferences({
'preferences': [
{
'user': "user0",
'ratings':[ {
'code': 'A1',
'value': 0
}, {
'code': 'A2',
'value': 1
}, {
'code': 'B1',
'value': 0.5
}
]
},
{
'user': "user1",
'ratings':[ {
'code': 'A1',
'value': 1
}, {
'code': 'B2',
'value': 1
}
]
}
]
})
toRate = ConfigurationModel({
'configuration': ['A1', 'B2'],
'variables': []
})
product_structure = ProductStructureModel({
'ProductStructure': [
{
'elementId': 'A',
'name': 'parent_element A',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'A1',
'name': 'child A1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'A2',
'name': 'child A2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},{
'elementId': 'B',
'name': 'parent_element B',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'B1',
'name': 'child B1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'B2',
'name': 'child B2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},
]
})
def float_lists_same(first, second):
for element in first:
if element not in second:
return False
second.remove(element)
if len(second) != 0:
return False
return True
class TestPreferencesToListFunction:
def test_should_be_empty_list(self):
function = PreferencesToListFunction()
assert len(function.convertToList(preferences, toRate)) == 0
class TestFlattenPreferencesToListFunction():
def test_simple_example(self):
function = FlattenPreferencesToListFunction()
assert float_lists_same([1.0,0.5,0.0,0.0,1.0], function.convertToList(preferences, toRate))
class TestSimplePerUserToListFunction():
def test_simple_example(self):
function = SimplePerUserToListFunction(Min())
assert float_lists_same([0.0, 1.0], function.convertToList(preferences, toRate))
class TestPerUserPerFeatureDistanceAverageToListFunction():
def test_simple_example(self):
function = PerUserPerFeatureDistanceAverageToListFunction(Min(), product_structure)
assert float_lists_same([0.25, 0.625], function.convertToList(preferences, toRate))

View File

@@ -0,0 +1,124 @@
from scoring.scoring_functions import PreferenceScoring, RatioCharacteristicConfigurationPenalty, WeightedFeaturePenalty, ReduceScoring
from scoring.value_functions import ValueToValueFunction
from model.configuration_model import ConfigurationModel
from model.preferences_model import Preferences
from scoring.list_functions import Min, Average
from scoring.preferences_functions import FlattenPreferencesToListFunction
from model.product_structure_model import ProductStructureModel
preferences = Preferences({
'preferences': [
{
'user': "user0",
'ratings':[ {
'code': 'A1',
'value': 0
}, {
'code': 'A2',
'value': 1
}, {
'code': 'B1',
'value': 0.5
}
]
},
{
'user': "user1",
'ratings':[ {
'code': 'A1',
'value': 1
}, {
'code': 'B2',
'value': 1
}
]
}
]
})
currentConfiguration = ConfigurationModel({
'configuration': ['A2', 'B2'],
'variables': []
})
toRate = ConfigurationModel({
'configuration': ['A1', 'B2'],
'variables': []
})
product_structure = ProductStructureModel({
'ProductStructure': [
{
'elementId': 'A',
'name': 'parent_element A',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'A1',
'name': 'child A1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'A2',
'name': 'child A2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},{
'elementId': 'B',
'name': 'parent_element B',
'type': "FEATURE",
'additionalData': [],
'children': [
{
'elementId': 'B1',
'name': 'child B1',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
},
{
'elementId': 'B2',
'name': 'child B2',
'children': [],
'additionalData': [],
'type': "CHARACTERISTIC"
}
],
},
]
})
class TestRatioCharacteristicConfigurationPenalty:
def test_simple_example(self):
function = RatioCharacteristicConfigurationPenalty(product_structure, [ValueToValueFunction()])
assert 0.5 == function.calc_score(currentConfiguration, preferences, toRate)
class TestWeightedFeaturePenalty:
def test_simple_example(self):
function = WeightedFeaturePenalty(product_structure, Min(), Average())
assert 0.375 == function.calc_score(currentConfiguration, preferences, toRate)
class TestReduceScoring:
def test_combined(self):
function = ReduceScoring([
RatioCharacteristicConfigurationPenalty(product_structure, [ValueToValueFunction()]),
WeightedFeaturePenalty(product_structure, Min(), Average())
])
assert 0.875 == function.calc_score(currentConfiguration, preferences, toRate)
def test_none(self):
function = ReduceScoring([])
assert 0 == function.calc_score(currentConfiguration, preferences, toRate)
class TestPreferenceScoring:
def test_simple_example(self):
function = PreferenceScoring(
FlattenPreferencesToListFunction(),
Min()
)
assert 0 == function.calc_score(currentConfiguration, preferences, toRate)

View File

@@ -0,0 +1,44 @@
from scoring.value_functions import MapToPercent, ValueToValueFunction, HighpassFilterFunction, LowpassFilterFunction, PowerFunction
import math
class TestMapToPercent:
def test_range_conversion(self):
function = MapToPercent(-20, -40)
assert math.isclose(1, function.applyToValue(-20))
assert math.isclose(0, function.applyToValue(-40))
assert math.isclose(0.5, function.applyToValue(-30))
class TestValueToValueFunction:
def test_same_value(self):
function = ValueToValueFunction()
assert math.isclose(10, function.applyToValue(10))
class TestHighpassFilterFunction:
def test_higher_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0.8, function.applyToValue(0.8))
def test_lower_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0, function.applyToValue(0.3))
def test_same_value(self):
function = HighpassFilterFunction(0.5)
assert math.isclose(0.5, function.applyToValue(0.5))
class TestLowpassFilterFunction:
def test_higher_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(1, function.applyToValue(0.8))
def test_lower_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(0.3, function.applyToValue(0.3))
def test_same_value(self):
function = LowpassFilterFunction(0.5)
assert math.isclose(0.5, function.applyToValue(0.5))
class TestPowerFunction:
def test_power_one(self):
function = PowerFunction(10)
assert math.isclose(1, function.applyToValue(1))
def test_power_small_number(self):
function = PowerFunction(4)
assert math.isclose(0.0001, function.applyToValue(0.1))

3
tests/test_simple.py Normal file
View File

@@ -0,0 +1,3 @@
class TestSimple:
def test_simple(self):
assert True