Exemple #1
0
def test_criterion_applied():
    """Test sampling w/ criterion."""
    # Prepare
    bridge = MagicMock()
    client1 = GrpcClientProxy(cid="train_client_1", bridge=bridge)
    client2 = GrpcClientProxy(cid="train_client_2", bridge=bridge)
    client3 = GrpcClientProxy(cid="test_client_1", bridge=bridge)
    client4 = GrpcClientProxy(cid="test_client_2", bridge=bridge)

    client_manager = SimpleClientManager()
    client_manager.register(client1)
    client_manager.register(client2)
    client_manager.register(client3)
    client_manager.register(client4)

    class TestCriterion(Criterion):
        """Criterion to select only test clients."""
        def select(self, client: ClientProxy) -> bool:
            return client.cid.startswith("test_")

    # Execute
    sampled_clients = client_manager.sample(2, criterion=TestCriterion())

    # Assert
    assert client3 in sampled_clients
    assert client4 in sampled_clients
Exemple #2
0
    def test_get_parameters(self):
        """This test is currently quite simple and should be improved"""
        # Prepare
        client = GrpcClientProxy(cid="1", bridge=self.bridge_mock)

        # Execute
        value: flower.ParametersRes = client.get_parameters()

        # Assert
        assert value.parameters.tensors == []
Exemple #3
0
    def test_evaluate(self):
        """This test is currently quite simple and should be improved"""
        # Prepare
        client = GrpcClientProxy(cid="1", bridge=self.bridge_mock)
        parameters = flower.Parameters(tensors=[], tensor_type="np")
        evaluate_ins: flower.EvaluateIns = (parameters, {})

        # Execute
        value = client.evaluate(evaluate_ins)

        # Assert
        assert (0, 0.0) == value
Exemple #4
0
    def test_fit(self):
        """This test is currently quite simple and should be improved"""
        # Prepare
        client = GrpcClientProxy(cid="1", bridge=self.bridge_mock)
        parameters = flower.weights_to_parameters([np.ones((2, 2))])
        ins: flower.FitIns = (parameters, {})

        # Execute
        parameters_prime, num_examples, _ = client.fit(ins=ins)

        # Assert
        assert parameters_prime.tensor_type == "np"
        assert flower.parameters_to_weights(parameters_prime) == []
        assert num_examples == 10
Exemple #5
0
def test_simple_client_manager_unregister():
    """Tests if the unregister method works correctly"""
    # Prepare
    cid = "1"
    bridge = MagicMock()
    client = GrpcClientProxy(cid=cid, bridge=bridge)
    client_manager = SimpleClientManager()
    client_manager.register(client)

    # Execute
    client_manager.unregister(client)

    # Assert
    assert len(client_manager) == 0
Exemple #6
0
def test_criterion_not_applied():
    """Test sampling w/o criterion."""
    # Prepare
    bridge = MagicMock()
    client1 = GrpcClientProxy(cid="train_client_1", bridge=bridge)
    client2 = GrpcClientProxy(cid="train_client_2", bridge=bridge)
    client3 = GrpcClientProxy(cid="test_client_1", bridge=bridge)
    client4 = GrpcClientProxy(cid="test_client_2", bridge=bridge)

    client_manager = SimpleClientManager()
    client_manager.register(client1)
    client_manager.register(client2)
    client_manager.register(client3)
    client_manager.register(client4)

    # Execute
    sampled_clients = client_manager.sample(4)

    # Assert
    assert client1 in sampled_clients
    assert client2 in sampled_clients
    assert client3 in sampled_clients
    assert client4 in sampled_clients
Exemple #7
0
def default_grpc_client_factory(cid: str,
                                bridge: GRPCBridge) -> GrpcClientProxy:
    """Return GrpcClientProxy instance."""
    return GrpcClientProxy(cid=cid, bridge=bridge)