Exemple #1
0
def test__in_mem_knn__refresh__underlying_data_model_and_similarity_is_refreshed(
):
    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(return_value=[])
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(return_value=1.0)
    fake_sim.refresh = mock.Mock()
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(return_value=[])

    sut = InMemoryRecordNeighbourhood(
        fake_model,
        fake_sim,
        50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    sut = InMemoryRecordNeighbourhood(fake_model, fake_sim, 50)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert fake_sim in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
    assert fake_sim.refresh.call_count == 1
Exemple #2
0
def test__record_similarity__get_similarity():
    expected_sim = 1.0

    record_1 = 'caesar'
    record_2 = 'brutus'
    preferences = {
        record_1: {
            1: Preference(1.0, None),
            2: Preference(2.0, None),
            3: Preference(2.0, None),
            4: Preference(1.0, None),
        },
        record_2: {
            1: Preference(1.0, None),
            4: Preference(2.0, None),
            5: Preference(2.0, None),
        },
    }

    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_record = mock.Mock(
        side_effect=lambda r: preferences[r])
    fake_sim = AbstractPreferenceSimilarity()
    fake_sim.get_similarity = mock.Mock(
        return_value=expected_sim)

    sut = RecordSimilarity(fake_model, fake_sim)
    sim = sut.get_similarity(record_1, record_2)

    assert sim == expected_sim
    fake_sim.get_similarity.assert_called_with(
        preferences[record_1],
        preferences[record_2])
Exemple #3
0
def test__record_similarity__get_similarity():
    expected_sim = 1.0

    record_1 = 'caesar'
    record_2 = 'brutus'
    preferences = {
        record_1: {
            1: Preference(1.0, None),
            2: Preference(2.0, None),
            3: Preference(2.0, None),
            4: Preference(1.0, None),
        },
        record_2: {
            1: Preference(1.0, None),
            4: Preference(2.0, None),
            5: Preference(2.0, None),
        },
    }

    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_record = mock.Mock(
        side_effect=lambda r: preferences[r])
    fake_sim = AbstractPreferenceSimilarity()
    fake_sim.get_similarity = mock.Mock(return_value=expected_sim)

    sut = RecordSimilarity(fake_model, fake_sim)
    sim = sut.get_similarity(record_1, record_2)

    assert sim == expected_sim
    fake_sim.get_similarity.assert_called_with(preferences[record_1],
                                               preferences[record_2])
Exemple #4
0
def create_k_nearest_neighbourhood(k, doc_sims, docs):
    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(return_value=docs)
    record_sim = AbstractRecordSimilarity()
    record_sim.get_similarity = mock.Mock(
        side_effect=lambda from_id, to_id: doc_sims[to_id])

    return KNearestRecordNeighbourhood(k=k,
                                       data_model=fake_model,
                                       record_sim=record_sim)
Exemple #5
0
def create_k_nearest_neighbourhood(k, doc_sims, docs):
    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(
        return_value=docs)
    record_sim = AbstractRecordSimilarity()
    record_sim.get_similarity = mock.Mock(
        side_effect=lambda from_id, to_id: doc_sims[to_id])

    return KNearestRecordNeighbourhood(
        k=k, data_model=fake_model, record_sim=record_sim)
Exemple #6
0
def test__in_mem_knn__get_nbours():
    record_caesar = 'caesar'
    record_brutus = 'brutus'
    record_napoleon = 'napoleon'

    nbours = {
        record_caesar: [
            record_brutus
        ],
        record_brutus: [
            record_caesar
        ],
        record_napoleon: []
    }
    sims = {
        record_caesar: {
            record_brutus: 0.9
        },
        record_brutus: {
            record_caesar: 0.8
        },
        record_napoleon: {}
    }

    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(return_value=nbours.keys())
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(
        side_effect=lambda from_r, to_r: sims[from_r][to_r])
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(
        side_effect=lambda r: nbours[r])

    sut = InMemoryRecordNeighbourhood(
        fake_model, fake_sim, 50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    assert sut.get_neighbours(record_caesar) == nbours[record_caesar]
    assert sut.get_neighbours(record_brutus) == nbours[record_brutus]
    assert sut.get_neighbours(record_napoleon) == nbours[record_napoleon]

    assert sut.get_neighbours('unknown') == []

    assert sut.get_similarity(record_caesar, record_brutus) ==\
        sims[record_caesar][record_brutus]
    assert sut.get_similarity(record_brutus, record_caesar) ==\
        sims[record_brutus][record_caesar]

    assert math.isnan(sut.get_similarity(record_caesar, record_napoleon))
    assert math.isnan(sut.get_similarity(record_napoleon, record_caesar))

    assert math.isnan(sut.get_similarity('unknown', record_napoleon))
    assert math.isnan(sut.get_similarity(record_caesar, 'unknown'))
Exemple #7
0
def test__record_similarity__refresh__underlying_data_model_is_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractPreferenceSimilarity()

    sut = RecordSimilarity(fake_model, fake_sim)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
Exemple #8
0
def test__record_similarity__refresh__underlying_data_model_is_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractPreferenceSimilarity()

    sut = RecordSimilarity(fake_model, fake_sim)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
Exemple #9
0
def create_recommender(preferences=default_prefs, doc_sims=doc_sims):
    data_model = AbstractRecordDataModel()
    data_model.get_preferences_of_session = mock.Mock(
        side_effect=lambda s_id: preferences)
    record_sim = AbstractRecordSimilarity()
    record_sim.get_similarity = mock.Mock(
        side_effect=lambda from_id, to_id: doc_sims[from_id][to_id])
    record_nhood = AbstractRecordNeighbourhood()
    record_nhood.get_neighbours = mock.Mock(
        side_effect=lambda doc_id: doc_sims[doc_id].keys())

    return RecordBasedRecommender(
        data_model, record_nhood, record_sim)
Exemple #10
0
def test__in_mem_knn__get_nbours():
    record_caesar = 'caesar'
    record_brutus = 'brutus'
    record_napoleon = 'napoleon'

    nbours = {
        record_caesar: [record_brutus],
        record_brutus: [record_caesar],
        record_napoleon: []
    }
    sims = {
        record_caesar: {
            record_brutus: 0.9
        },
        record_brutus: {
            record_caesar: 0.8
        },
        record_napoleon: {}
    }

    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(return_value=nbours.keys())
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(
        side_effect=lambda from_r, to_r: sims[from_r][to_r])
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(side_effect=lambda r: nbours[r])

    sut = InMemoryRecordNeighbourhood(
        fake_model,
        fake_sim,
        50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    assert sut.get_neighbours(record_caesar) == nbours[record_caesar]
    assert sut.get_neighbours(record_brutus) == nbours[record_brutus]
    assert sut.get_neighbours(record_napoleon) == nbours[record_napoleon]

    assert sut.get_neighbours('unknown') == []

    assert sut.get_similarity(record_caesar, record_brutus) ==\
        sims[record_caesar][record_brutus]
    assert sut.get_similarity(record_brutus, record_caesar) ==\
        sims[record_brutus][record_caesar]

    assert math.isnan(sut.get_similarity(record_caesar, record_napoleon))
    assert math.isnan(sut.get_similarity(record_napoleon, record_caesar))

    assert math.isnan(sut.get_similarity('unknown', record_napoleon))
    assert math.isnan(sut.get_similarity(record_caesar, 'unknown'))
Exemple #11
0
def test__knn__refresh__underlying_data_model_and_similarity_is_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.refresh = mock.Mock()

    sut = KNearestRecordNeighbourhood(5, fake_model, fake_sim)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert fake_sim in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
    assert fake_sim.refresh.call_count == 1
Exemple #12
0
def test__knn__refresh__underlying_data_model_and_similarity_is_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.refresh = mock.Mock()

    sut = KNearestRecordNeighbourhood(5, fake_model, fake_sim)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert fake_sim in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
    assert fake_sim.refresh.call_count == 1
Exemple #13
0
def test__in_mem_dm__refresh__underlying_data_model_is_refreshed():
    preferences = {}
    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_records = mock.Mock(
        return_value=preferences.iteritems())
    fake_model.refresh = mock.Mock()

    sut = InMemoryRecordDataModel(fake_model)

    sut = InMemoryRecordDataModel(fake_model)
    refreshed_components = set()
    sut.refresh(refreshed_components)

    fake_model.refresh.assert_called_once_with(refreshed_components)
    assert fake_model in refreshed_components
    assert sut in refreshed_components
Exemple #14
0
def test__in_mem_dm__get_preferences_of_session__session_not_present():
    preferences = {
        record_caesar: {
            session_alice: Preference(1.0, datetime(1999, 1, 1)),
            session_bob: Preference(2.0, datetime(1999, 1, 1)),
        },
        record_brutus: {
            session_alice: Preference(1.0, datetime(1999, 1, 1)),
        }
    }
    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_records = mock.Mock(
        return_value=preferences.iteritems())

    sut = InMemoryRecordDataModel(fake_model)

    assert sut.get_preferences_of_session('dogma') == {}
Exemple #15
0
def test__in_mem_dm__get_preferences_of_session():
    preferences = {
        record_caesar: {
            session_alice: Preference(1.0, datetime(1999, 1, 1)),
            session_bob: Preference(2.0, datetime(1999, 1, 1)),
        },
        record_brutus: {
            session_alice: Preference(1.0, datetime(1999, 1, 1)),
        }
    }
    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_records = mock.Mock(
        return_value=preferences.iteritems())

    sut = InMemoryRecordDataModel(fake_model)

    sess_prefs = sut.get_preferences_of_session(session_alice)
    for record_id, pref in sess_prefs.iteritems():
        assert preferences[record_id][session_alice] == pref
Exemple #16
0
def test__in_mem_dm__refresh__data_is_reloaded():
    preferences = {}
    fake_model = AbstractRecordDataModel()
    fake_model.get_preferences_for_records = mock.Mock(
        return_value=preferences.iteritems())
    fake_model.refresh = mock.Mock()

    sut = InMemoryRecordDataModel(fake_model)

    assert {k: v for k, v in sut.get_preferences_for_records()} == {}

    preferences[record_caesar] = {
        session_alice: Preference(1.0, datetime(1999, 1, 1)),
    }

    sut.refresh(set())

    for record_id, rec_prefs in sut.get_preferences_for_records():
        for session_id, pref in rec_prefs.iteritems():
            assert preferences[record_id][session_id] == pref
Exemple #17
0
def test__in_mem_knn__refresh__data_is_reloaded():
    record_caesar = 'caesar'
    record_brutus = 'brutus'

    nbours = {
    }
    sims = {
    }

    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(
        side_effect=lambda: nbours.keys())
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(
        side_effect=lambda from_r, to_r: sims[from_r][to_r])
    fake_sim.refresh = mock.Mock()
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(
        side_effect=lambda r: nbours[r])

    sut = InMemoryRecordNeighbourhood(
        fake_model, fake_sim, 50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    assert sut.get_neighbours(record_caesar) == []
    assert math.isnan(sut.get_similarity(record_caesar, record_brutus))

    nbours[record_caesar] = [record_brutus]
    nbours[record_brutus] = [record_caesar]
    sims[record_caesar] = {record_brutus: 0.9}
    sims[record_brutus] = {record_caesar: 0.8}

    sut.refresh(set())

    assert sut.get_neighbours(record_caesar) == nbours[record_caesar]
    assert sut.get_similarity(record_caesar, record_brutus) ==\
        sims[record_caesar][record_brutus]
Exemple #18
0
def test__refresh__underlying_components_are_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.refresh = mock.Mock()
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.refresh = mock.Mock()

    sut = RecordBasedRecommender(
        data_model=fake_model,
        record_sim=fake_sim,
        record_nhood=fake_nhood)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert fake_sim in refreshed_components
    assert fake_nhood in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
    assert fake_sim.refresh.call_count == 1
    assert fake_nhood.refresh.call_count == 1
Exemple #19
0
def test__in_mem_knn__refresh__data_is_reloaded():
    record_caesar = 'caesar'
    record_brutus = 'brutus'

    nbours = {}
    sims = {}

    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(side_effect=lambda: nbours.keys())
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(
        side_effect=lambda from_r, to_r: sims[from_r][to_r])
    fake_sim.refresh = mock.Mock()
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(side_effect=lambda r: nbours[r])

    sut = InMemoryRecordNeighbourhood(
        fake_model,
        fake_sim,
        50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    assert sut.get_neighbours(record_caesar) == []
    assert math.isnan(sut.get_similarity(record_caesar, record_brutus))

    nbours[record_caesar] = [record_brutus]
    nbours[record_brutus] = [record_caesar]
    sims[record_caesar] = {record_brutus: 0.9}
    sims[record_brutus] = {record_caesar: 0.8}

    sut.refresh(set())

    assert sut.get_neighbours(record_caesar) == nbours[record_caesar]
    assert sut.get_similarity(record_caesar, record_brutus) ==\
        sims[record_caesar][record_brutus]
Exemple #20
0
def test__in_mem_knn__refresh__underlying_data_model_and_similarity_is_refreshed():
    fake_model = AbstractRecordDataModel()
    fake_model.get_records = mock.Mock(return_value=[])
    fake_model.refresh = mock.Mock()
    fake_sim = AbstractRecordSimilarity()
    fake_sim.get_similarity = mock.Mock(return_value=1.0)
    fake_sim.refresh = mock.Mock()
    fake_nhood = AbstractRecordNeighbourhood()
    fake_nhood.get_neighbours = mock.Mock(return_value=[])

    sut = InMemoryRecordNeighbourhood(
        fake_model, fake_sim, 50,
        nhood_factory=lambda dm, sim, num_nh: fake_nhood)

    sut = InMemoryRecordNeighbourhood(fake_model, fake_sim, 50)

    refreshed_components = set()
    sut.refresh(refreshed_components)

    assert fake_model in refreshed_components
    assert fake_sim in refreshed_components
    assert sut in refreshed_components
    assert fake_model.refresh.call_count == 1
    assert fake_sim.refresh.call_count == 1