def test_flow_batch_size(barbell, num_powers): batch_size = 2 generator = AdjacencyPowerGenerator(barbell, num_powers=num_powers) for x, y in generator.flow(batch_size=batch_size).take(1): assert x[0].shape == (batch_size, ) assert y.shape == (batch_size, 1, len(barbell.nodes())) assert x[1].shape == (batch_size, num_powers, len(barbell.nodes()))
def test_bad_init(barbell): with pytest.raises(TypeError): generator = AdjacencyPowerGenerator(None, num_powers=5) with pytest.raises(ValueError, match="num_powers: expected.*found -1"): generator = AdjacencyPowerGenerator(barbell, num_powers=-1) with pytest.raises(TypeError, match="num_powers: expected.*found float"): generator = AdjacencyPowerGenerator(barbell, num_powers=1.0)
def test_partial_powers(barbell, num_powers): Aadj = normalize_adj(barbell.to_adjacency_matrix(), symmetric=False).todense() actual_powers = [Aadj] for _ in range(num_powers - 1): actual_powers.append(actual_powers[-1].dot(Aadj)) generator = AdjacencyPowerGenerator(barbell, num_powers=num_powers) dataset = generator.flow(batch_size=1) for i, (x, y) in enumerate(dataset.take(barbell.number_of_nodes())): partial_powers = x[1].numpy() for j in range(num_powers): print(i, j) assert np.allclose(partial_powers[0, j, :], actual_powers[j][i, :])
def test_bad_flow(barbell): generator = AdjacencyPowerGenerator(barbell, num_powers=5) with pytest.raises(ValueError, match="batch_size: expected.*found 0"): generator.flow(0, num_parallel_calls=1) with pytest.raises(TypeError, match="batch_size: expected.*found float"): generator.flow(1.0, num_parallel_calls=1) with pytest.raises(ValueError, match="num_parallel_calls: expected.*found 0"): generator.flow(1, num_parallel_calls=0) with pytest.raises(TypeError, match="num_parallel_calls: expected.*found float"): generator.flow(1, num_parallel_calls=1.01)
def test_init(barbell): generator = AdjacencyPowerGenerator(barbell, num_powers=5) num_nodes = len(barbell.nodes()) assert generator.num_powers == 5 assert generator.Aadj_T.shape == (num_nodes, num_nodes) assert generator.transition_matrix_T.shape == (num_nodes, num_nodes)
def test_partial_powers(barbell, num_powers, weighted): raw_adj = barbell.to_adjacency_matrix(weighted=weighted) Aadj = normalize_adj(raw_adj, symmetric=False).todense() actual_powers = [Aadj] for _ in range(num_powers - 1): actual_powers.append(actual_powers[-1].dot(Aadj)) generator = AdjacencyPowerGenerator(barbell, num_powers=num_powers, weighted=weighted) dataset = generator.flow(batch_size=1) for i, (x, y) in enumerate(dataset.take(barbell.number_of_nodes())): partial_powers = x[1].numpy() for j in range(num_powers): print(i, j) np.testing.assert_allclose(partial_powers[:, j, :], actual_powers[j][i, :], rtol=1e-5, atol=1e-8)
def test_flow(barbell): generator = AdjacencyPowerGenerator(barbell, num_powers=5) dataset = generator.flow(batch_size=1) assert tf.data.experimental.cardinality(dataset).numpy() == -1