def test_invalid_n_mean(self): """Test if function raises a ``ValueError`` when the mean photon number is specified to be negative.""" g = nx.complete_graph(5) with pytest.raises(ValueError, match="Mean photon number must be non-negative"): similarity.feature_vector_events(g, [2, 4], 2, n_mean=-1)
def test_correct_vector_returned(self, monkeypatch): """Test if function correctly constructs the feature vector. The ``prob_event_exact`` and ``prob_event_mc`` function called within ``feature_vector_events`` are monkeypatched to return hard-coded outputs that depend only on the orbit.""" with monkeypatch.context() as m: m.setattr( similarity, "prob_event_mc", lambda _graph, photons, max_count, n_mean, samples, loss: 1.0 / photons, ) g = nx.complete_graph(8) assert similarity.feature_vector_events(g, [2, 4, 8], 1, samples=1) == [ 0.5, 0.25, 0.125, ] with monkeypatch.context() as m: m.setattr( similarity, "prob_event_exact", lambda _graph, photons, max_count, n_mean, loss: 0.5 * (1.0 / photons), ) g = nx.complete_graph(8) assert similarity.feature_vector_events( g, [2, 4, 8], 1) == [0.25, 0.125, 0.0625]
def test_low_count(self): """Test if function raises a ``ValueError`` if ``max_count_per_mode`` is negative.""" g = nx.complete_graph(5) with pytest.raises( ValueError, match="Maximum number of photons per mode must be non-negative" ): similarity.feature_vector_events(g, [2, 4], max_count_per_mode=-1)
def test_bad_event_photon_numbers(self): """Test if function raises a ``ValueError`` when input a minimum photon number that is below zero.""" with pytest.raises( ValueError, match="Cannot request events with photon number below zero"): graph = nx.complete_graph(5) similarity.feature_vector_events(graph, [-1, 4], 2)
def test_invalid_loss(self): """Test if function raises a ``ValueError`` when the loss parameter is specified outside of range.""" g = nx.complete_graph(5) with pytest.raises( ValueError, match="Loss parameter must take a value between zero and one"): similarity.feature_vector_events(g, [2, 4], 2, loss=2)
def test_invalid_event_photon_numbers(self): """Test if function raises a ``ValueError`` when the list of event photons numbers is empty.""" g = nx.complete_graph(5) with pytest.raises( ValueError, match="List of photon numbers must have at least one element"): similarity.feature_vector_events(g, [], 1)
def test_known_result(self): """Tests if the probability for known cases is correctly reproduced.""" g = nx.complete_graph(4) p = similarity.feature_vector_events(g, [2, 4], 2, 4) assert np.allclose(p, [0.21087781178526066, 0.11998024483275889]) graph = nx.complete_graph(20) assert np.allclose( similarity.feature_vector_events(graph, [18, 20], 1, 1, samples=10), [0, 0])
def test_calls_exact_for_zero_samples(self): """Test if function calls the exact function for zero samples""" g = nx.complete_graph(5) assert similarity.feature_vector_events( g, [2], 1, samples=0) == similarity.prob_event_exact(g, 2, 1)