Skip to content

Commit

Permalink
mypy fixes csle-agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Aug 17, 2023
1 parent 0e44802 commit 9608040
Show file tree
Hide file tree
Showing 22 changed files with 12 additions and 156 deletions.
3 changes: 2 additions & 1 deletion simulation-system/libs/csle-agents/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Dict, Any
import numpy as np
import pytest
from csle_common.dao.simulation_config.simulation_env_config import SimulationEnvConfig
Expand Down Expand Up @@ -174,7 +175,7 @@ def default_joint_observation_space_config(min_alerts_weighted_by_priority: int
observation_id_to_observation_id_vector = {}
observation_id_to_observation_vector = {}
defender_observations = []
component_observations = {}
component_observations: Dict[str, Any] = {}
component_observations["alerts_weighted_by_priority"] = []
for val in range(min_alerts_weighted_by_priority, max_alerts_weighted_by_priority):
component_observations["alerts_weighted_by_priority"].append(Observation(
Expand Down
7 changes: 0 additions & 7 deletions simulation-system/libs/csle-agents/tests/test_bayes_opt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -22,8 +21,6 @@ class TestBayesOptSuite:
Test suite for the BayesOptAgent
"""

pytest.logger = logging.getLogger("bayes_opt_tests")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -134,10 +131,8 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating Bayesian Optimization Agent")
BayesOptAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -172,9 +167,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
return_value=True)
agent = BayesOptAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of Bayesian Optimization Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -22,8 +21,6 @@ class TestCrossEntropySuite:
Test suite for the CrossEntropyAgent
"""

pytest.logger = logging.getLogger("cross_entropy_tests")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -122,10 +119,8 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating Cross Entropy Agent")
CrossEntropyAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -179,9 +174,7 @@ def test_run_agent(self, mocker, experiment_config: ExperimentConfig,
agent = CrossEntropyAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of Cross Entropy Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -22,8 +21,6 @@ class TestDifferentialEvolutionSuite:
Test suite for the DifferentialEvolutionAgent
"""

pytest.logger = logging.getLogger("differential_evolution_tests")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -130,11 +127,9 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the Differential Evolution Agent")
DifferentialEvolutionAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -173,9 +168,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
agent = DifferentialEvolutionAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of the Differential Evolution Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
7 changes: 0 additions & 7 deletions simulation-system/libs/csle-agents/tests/test_dqn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -20,8 +19,6 @@ class TestDQNAgentSuite:
Test suite for the DQNAgent
"""

pytest.logger = logging.getLogger("dqn_tests")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -159,10 +156,8 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the DQN Agent")
DQNAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -202,9 +197,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
agent = DQNAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of the DQN Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
6 changes: 0 additions & 6 deletions simulation-system/libs/csle-agents/tests/test_dynasec.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging
import pytest
import pytest_mock
from csle_common.dao.training.agent_type import AgentType
from csle_agents.agents.dynasec.dynasec_agent import DynaSecAgent
Expand All @@ -10,8 +8,6 @@ class TestDynaSecSuite:
Test suite for the DynaSecAgent
"""

pytest.logger = logging.getLogger("dynasec_tests")

def test_create_agent(self, mocker: pytest_mock.MockFixture) -> None:
"""
Tests creation of the FPAgent
Expand All @@ -25,8 +21,6 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture) -> None:
experiment_config = mocker.MagicMock()
experiment_config.configure_mock(**{"agent_type": AgentType.DYNA_SEC})
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the DynaSec Agent")
DynaSecAgent(emulation_executions=emulation_executions, simulation_env_config=simulation_env_config,
experiment_config=experiment_config, attacker_sequence=attacker_sequence,
defender_sequence=defender_sequence, system_identification_config=system_id_config)
pytest.logger.info("Agent created successfully")
7 changes: 0 additions & 7 deletions simulation-system/libs/csle-agents/tests/test_fp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -21,8 +20,6 @@ class TestFPSuite:
Test suite for the FPAgent
"""

pytest.logger = logging.getLogger("fp_tests")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -126,10 +123,8 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the FP Agent")
FictitiousPlayAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -167,9 +162,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
agent = FictitiousPlayAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of the Fictitious Play Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
7 changes: 0 additions & 7 deletions simulation-system/libs/csle-agents/tests/test_hsvi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand Down Expand Up @@ -78,8 +77,6 @@ class TestHSVISuite(object):
Test suite for the HSVIAgent
"""

pytest.logger = logging.getLogger("hsvi_tests")

@pytest.fixture
def experiment_config(self, example_simulation_config: SimulationEnvConfig) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -229,9 +226,7 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
:return: None
"""
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the HSVI Agent")
HSVIAgent(simulation_env_config=simulation_env_config, experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -267,9 +262,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
mocker.patch('csle_common.metastore.metastore_facade.MetastoreFacade.save_multi_threshold_stopping_policy',
return_value=True)
agent = HSVIAgent(simulation_env_config=simulation_env_config, experiment_config=experiment_config)
pytest.logger.info("Starting training of the HSVI Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
26 changes: 10 additions & 16 deletions simulation-system/libs/csle-agents/tests/test_hsvi_os_posg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Tuple
from typing import Tuple, Any, Dict
import numpy as np
import logging
import numpy.typing as npt
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -13,35 +13,35 @@
import csle_agents.constants.constants as agents_constants


def states() -> Tuple[np.ndarray, dict]:
def states() -> Tuple[npt.NDArray[Any], Dict[int, str]]:
"""
:return: the set of states and a lookup dict
"""
return np.array([0, 1]), {0: "NO_INTRUSION", 1: "INTRUSION", 2: "TERMINAL"}


def player_1_actions() -> Tuple[np.ndarray, dict]:
def player_1_actions() -> Tuple[npt.NDArray[Any], Dict[int, str]]:
"""
:return: the set of actions of player 1 and a lookup dict
"""
return np.array([0, 1]), {0: "CONTINUE", 1: "STOP"}


def player_2_actions() -> Tuple[np.ndarray, dict]:
def player_2_actions() -> Tuple[npt.NDArray[Any], Dict[int, str]]:
"""
:return: the set of actions of player 2 and a lookup dict
"""
return np.array([0, 1]), {0: "CONTINUE", 1: "STOP"}


def observations() -> Tuple[np.ndarray, dict]:
def observations() -> Tuple[npt.NDArray[Any], Dict[int, str]]:
"""
:return: the set of observations and a lookup dict
"""
return np.array([0, 1, 2]), {0: "NO ALERT", 1: "ONE ALERT", 2: "TERMINAL"}


def observation_tensor() -> np.ndarray:
def observation_tensor() -> npt.NDArray[Any]:
"""
:return: a |A1|x|A2|x|S|x|O| tensor
"""
Expand Down Expand Up @@ -73,7 +73,7 @@ def observation_tensor() -> np.ndarray:
return O


def reward_tensor() -> np.ndarray:
def reward_tensor() -> npt.NDArray[Any]:
"""
:return: return a |A1|x|A2|x|S| tensor
"""
Expand All @@ -96,7 +96,7 @@ def reward_tensor() -> np.ndarray:
return R


def transition_tensor() -> np.ndarray:
def transition_tensor() -> npt.NDArray[Any]:
"""
:return: a |A1|x|A2||S|^2 tensor
"""
Expand Down Expand Up @@ -127,7 +127,7 @@ def transition_tensor() -> np.ndarray:
)


def initial_belief() -> np.ndarray:
def initial_belief() -> npt.NDArray[Any]:
"""
:return: the initial belief point
"""
Expand All @@ -139,8 +139,6 @@ class TestHSVISuite(object):
Test suite for the HSVIAgent
"""

pytest.logger = logging.getLogger("hsvi_os_posg_tests")

@pytest.fixture
def experiment_config(self, example_simulation_config: SimulationEnvConfig) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -231,9 +229,7 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
:return: None
"""
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the HSVI-OS-POSG Agent")
HSVIOSPOSGAgent(simulation_env_config=simulation_env_config, experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig) -> None:
"""
Expand All @@ -258,9 +254,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
mocker.patch('csle_common.metastore.metastore_facade.MetastoreFacade.save_multi_threshold_stopping_policy',
return_value=True)
agent = HSVIOSPOSGAgent(simulation_env_config=simulation_env_config, experiment_config=experiment_config)
pytest.logger.info("Starting training of the HSVI-OS-POSG Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import logging
import pytest
import pytest_mock
import csle_common.constants.constants as constants
Expand All @@ -22,8 +21,6 @@ class TestKieferWolfowitzSuite(object):
Test suite for the KieferWolfoWitzAgent
"""

pytest.logger = logging.getLogger("kieer_wolfowitz_agent")

@pytest.fixture
def experiment_config(self) -> ExperimentConfig:
"""
Expand Down Expand Up @@ -124,10 +121,8 @@ def test_create_agent(self, mocker: pytest_mock.MockFixture, experiment_config:
"""
emulation_env_config = mocker.MagicMock()
simulation_env_config = mocker.MagicMock()
pytest.logger.info("Creating the KieferWolfowitzAgent")
KieferWolfowitzAgent(emulation_env_config=emulation_env_config, simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Agent created successfully")

def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: ExperimentConfig,
pomdp_config: StoppingGameDefenderPomdpConfig) -> None:
Expand Down Expand Up @@ -166,9 +161,7 @@ def test_run_agent(self, mocker: pytest_mock.MockFixture, experiment_config: Exp
agent = KieferWolfowitzAgent(emulation_env_config=emulation_env_config,
simulation_env_config=simulation_env_config,
experiment_config=experiment_config)
pytest.logger.info("Starting training of the KieferWolfowitz Agent")
experiment_execution = agent.train()
pytest.logger.info("Training completed succesfully")
assert experiment_execution is not None
assert experiment_execution.descr != ""
assert experiment_execution.id is not None
Expand Down
Loading

0 comments on commit 9608040

Please sign in to comment.