roksana.attack_methods package

Submodules

roksana.attack_methods.base_attack module

BaseAttack Module

This module provides an abstract base class for implementing graph attack methods. All attack methods must inherit from BaseAttack and implement the required methods.

Classes:
  • BaseAttack: Abstract base class for all attack methods.

class roksana.attack_methods.base_attack.BaseAttack(data: Any, **kwargs)[source]

Bases: ABC

Abstract BaseAttack Class

Defines the interface for attack methods. All attack methods must inherit from this class and implement the __init__ and attack methods.

data

The graph dataset used by the attack method.

Type:

Any

params

Additional parameters for the attack method.

Type:

dict

abstract __init__(data: Any, **kwargs)[source]

Initialize the attack method with the given dataset.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional keyword arguments specific to the attack method.

abstract attack(query_node: int, perturbations: int = 1) Dict[str, Any][source]

Perform an attack on the specified query node.

Parameters:
  • query_node (int) – Index of the node to attack.

  • perturbations (int, optional) – Number of perturbations to apply. Defaults to 1.

Returns:

A dictionary containing details of the attack, such as removed edges or modifications made.

Return type:

Dict[str, Any]

roksana.attack_methods.custom_attack module

MyCustomAttack Module

This module provides an example of how to implement a custom graph attack algorithm. Custom routines should inherit from the BaseAttack class and override the execute method to define the attack logic.

Classes:
  • MyCustomAttack: An example implementation of a custom attack.

Usage:

from custom_attack import MyCustomAttack

attack = MyCustomAttack() result = attack.execute(data, params={‘custom_param’: ‘value’}) ———————————————————————-

class roksana.attack_methods.custom_attack.MyCustomAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

MyCustomAttack Class

Example implementation of a custom attack. This class demonstrates how to inherit from the BaseAttack class and implement the execute method for modifying a graph dataset.

- execute

Executes the custom attack on the input data.

execute(data: Any, params: dict) Any[source]

Execute the custom attack logic.

Parameters:
  • data (Any) – The input graph dataset to modify.

  • params (dict) – A dictionary of parameters to configure the attack.

Returns:

The modified graph dataset after applying the custom attack.

Return type:

Any

roksana.attack_methods.degree module

DegreeAttack Module

This module implements the DegreeAttack class for adversarial attacks on graphs by selectively removing edges connected to nodes with the highest degree.

Classes:
  • DegreeAttack: A class to perform degree-based edge removal attacks on a graph dataset.

class roksana.attack_methods.degree.DegreeAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

DegreeAttack Class

Implements an adversarial attack that removes edges based on the degree of connected nodes.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the DegreeAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the degree-based attack on the graph dataset.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal. Must be a 1D tensor.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

roksana.attack_methods.pagerank module

PageRankAttack Module

This module implements the PageRankAttack class for adversarial attacks on graphs by selectively removing edges connected to nodes based on their PageRank scores.

Classes:
  • PageRankAttack: A class to perform PageRank-based edge removal attacks on a graph dataset.

class roksana.attack_methods.pagerank.PageRankAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

PageRankAttack Class

Implements an adversarial attack that removes edges connected to nodes based on their PageRank scores.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the PageRankAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the PageRank-based attack on the graph dataset.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal. Must be a 1D tensor.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

roksana.attack_methods.random module

RandomAttack Module

This module implements the RandomAttack class for adversarial attacks on graphs by randomly removing edges connected to selected nodes.

Classes:
  • RandomAttack: A class to perform random edge removal attacks on a graph dataset.

class roksana.attack_methods.random.RandomAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

RandomAttack Class

Implements an adversarial attack that randomly removes edges connected to specified nodes in a graph.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the RandomAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the random edge removal attack.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes for which edges are to be removed. Can be a single node or a tensor of nodes.

Returns:

  • The modified graph dataset with updated edges.

  • A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

roksana.attack_methods.registry module

Attack Methods Registry

This module provides a registry for managing attack methods. It includes predefined attack methods and utilities to register custom attack methods dynamically.

Classes:
  • DegreeAttack: Implements degree-based edge removal.

  • PageRankAttack: Implements PageRank-based edge removal.

  • RandomAttack: Implements random edge removal.

  • VikingAttack: Implements Viking perturbation attack.

Functions:
  • register_attack_method: Decorator to register a custom attack method.

  • get_attack_method: Retrieve an instance of a registered attack method.

roksana.attack_methods.registry.get_attack_method(name: str, data: Any, **kwargs) BaseAttack[source]

Retrieve an instance of the specified attack method.

Parameters:
  • name (str) – Name of the attack method (e.g., ‘degree’, ‘pagerank’, ‘random’, ‘viking’).

  • data (Any) – The graph dataset.

  • **kwargs – Additional keyword arguments for initializing the attack method.

Returns:

An instance of the requested attack method.

Return type:

BaseAttack

Raises:

ValueError – If the specified attack method is not registered.

Example

>>> from roksana.attack_methods.registry import get_attack_method
>>> attack = get_attack_method('degree', data=my_graph, param1=value1)
roksana.attack_methods.registry.register_attack_method(name: str)[source]

Decorator to register an attack method class with a given name.

Parameters:

name (str) – The name to register the attack method under.

Returns:

A decorator function that registers the attack method.

Return type:

Callable

roksana.attack_methods.viking module

VikingAttack Module

This module implements the VikingAttack class for adversarial attacks on graphs by perturbing edges involving selected nodes.

Classes:
  • VikingAttack: A class to perform perturbation-based edge removal attacks on a graph dataset.

class roksana.attack_methods.viking.VikingAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

VikingAttack Class

Implements an adversarial attack by perturbing edges involving specified nodes in a graph.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the VikingAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Execute the Viking perturbation attack.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

perturbation_attack(data: Any, selected_nodes: Tensor) Tuple[Tensor, List[Tuple[int, int]]][source]

Perform the Viking perturbation attack by removing edges involving selected nodes.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal.

Returns:

  • retained_edges (torch.Tensor): The edge index after removal of edges.

  • edges_to_remove (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[torch.Tensor, List[Tuple[int, int]]]

Module contents

Attack Methods Package

This package provides various attack methods for adversarial modifications of graph datasets. It includes both predefined attack methods and utilities for registering and retrieving custom attacks.

Modules:
  • base_attack: Defines the abstract base class for all attack methods.

  • registry: Manages registration and retrieval of attack methods.

  • degree: Implements degree-based edge removal.

  • pagerank: Implements PageRank-based edge removal.

  • random: Implements random edge removal.

  • viking: Implements Viking perturbation attack.

- BaseAttack

Abstract base class for all attack methods.

- get_attack_method

Retrieve a registered attack method by name.

- ATTACK_METHODS

Dictionary of registered attack methods.

- DegreeAttack

Implements degree-based attack logic.

- PageRankAttack

Implements PageRank-based attack logic.

- RandomAttack

Implements random attack logic.

- VikingAttack

Implements Viking perturbation attack logic.

class roksana.attack_methods.BaseAttack(data: Any, **kwargs)[source]

Bases: ABC

Abstract BaseAttack Class

Defines the interface for attack methods. All attack methods must inherit from this class and implement the __init__ and attack methods.

data

The graph dataset used by the attack method.

Type:

Any

params

Additional parameters for the attack method.

Type:

dict

abstract __init__(data: Any, **kwargs)[source]

Initialize the attack method with the given dataset.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional keyword arguments specific to the attack method.

abstract attack(query_node: int, perturbations: int = 1) Dict[str, Any][source]

Perform an attack on the specified query node.

Parameters:
  • query_node (int) – Index of the node to attack.

  • perturbations (int, optional) – Number of perturbations to apply. Defaults to 1.

Returns:

A dictionary containing details of the attack, such as removed edges or modifications made.

Return type:

Dict[str, Any]

class roksana.attack_methods.DegreeAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

DegreeAttack Class

Implements an adversarial attack that removes edges based on the degree of connected nodes.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the DegreeAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the degree-based attack on the graph dataset.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal. Must be a 1D tensor.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

class roksana.attack_methods.PageRankAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

PageRankAttack Class

Implements an adversarial attack that removes edges connected to nodes based on their PageRank scores.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the PageRankAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the PageRank-based attack on the graph dataset.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal. Must be a 1D tensor.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

class roksana.attack_methods.RandomAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

RandomAttack Class

Implements an adversarial attack that randomly removes edges connected to specified nodes in a graph.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the RandomAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Perform the random edge removal attack.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes for which edges are to be removed. Can be a single node or a tensor of nodes.

Returns:

  • The modified graph dataset with updated edges.

  • A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

class roksana.attack_methods.VikingAttack(data: Any, **kwargs)[source]

Bases: BaseAttack

VikingAttack Class

Implements an adversarial attack by perturbing edges involving specified nodes in a graph.

data

The graph dataset.

Type:

Any

params

Additional parameters for the attack.

Type:

dict

__init__(data: Any, **kwargs)[source]

Initialize the VikingAttack method.

Parameters:
  • data (Any) – The graph dataset.

  • **kwargs – Additional parameters for the attack.

attack(data: Any, selected_nodes: Tensor) Tuple[Any, List[Tuple[int, int]]][source]

Execute the Viking perturbation attack.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal.

Returns:

  • updated_data (Any): The modified graph dataset with updated edges.

  • removed_edges (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[Any, List[Tuple[int, int]]]

perturbation_attack(data: Any, selected_nodes: Tensor) Tuple[Tensor, List[Tuple[int, int]]][source]

Perform the Viking perturbation attack by removing edges involving selected nodes.

Parameters:
  • data (Any) – The graph dataset.

  • selected_nodes (torch.Tensor) – Nodes to target for edge removal.

Returns:

  • retained_edges (torch.Tensor): The edge index after removal of edges.

  • edges_to_remove (List[Tuple[int, int]]): A list of removed edges.

Return type:

Tuple[torch.Tensor, List[Tuple[int, int]]]

roksana.attack_methods.get_attack_method(name: str, data: Any, **kwargs) BaseAttack[source]

Retrieve an instance of the specified attack method.

Parameters:
  • name (str) – Name of the attack method (e.g., ‘degree’, ‘pagerank’, ‘random’, ‘viking’).

  • data (Any) – The graph dataset.

  • **kwargs – Additional keyword arguments for initializing the attack method.

Returns:

An instance of the requested attack method.

Return type:

BaseAttack

Raises:

ValueError – If the specified attack method is not registered.

Example

>>> from roksana.attack_methods.registry import get_attack_method
>>> attack = get_attack_method('degree', data=my_graph, param1=value1)