grafx.data

class NodeConfigs(config)

An object that stores configurations of node types and handles various utility tasks, e.g., converting each node type str to int, or vice versa. By default, the utility types, "in", "out", and "mix", are automatically included.

Parameters:

config (Union[list, dict]) – The configuration data for the nodes. If a list, it is assumed to contain node types. If a dict, it should map node types to their specific configurations.

Variables:
  • node_type_dict (Dict[str, dict]) – The full configuration for the given node types.

  • node_types (list of str) – List of node types.

  • node_type_to_index (Dict[str, int]) – Mapping from node types to their indices.

  • num_node_types (int) – The total number of node types.

  • num_inlets (Dict[str, int]) – Number of inlets for each node type.

  • num_outlets (Dict[str, int]) – Number of outlets for each node type.

  • siso_only (bool) – Indicates if we only have single-input single-output (SISO) systems.

  • max_num_inlets (int when siso_only=False) – Maximum number of inlets.

  • max_num_outlets (int when siso_only=False) – Maximum number of outlets.

  • inlet_to_index (Dict[str, Dict[str, int]] when siso_only=False) – Nested dict of type-to-inlet-to-index.

  • outlet_to_index (Dict[str, Dict[str, int]] when siso_only=False) – Nested dict of type-to-outlet-to-index.

class GRAFX(config=None, invalid_op='error')

A base class for audio processing graph. It can be used for creating and modifying a graph. It inherits MultiDiGraph class from networkx.

Parameters:
  • config (NodeConfigs, optional) – Node type configurations (default: None).

  • invalid_op (str, optional) – Behavior when an invalid operation is performed (“error”, “warn”, “mute”) (default: "error").

Variables:
  • counter (Union[List[int], int]) – A counter (for each graph, if it is a batched graph) for the number of nodes.

  • consecutive_ids (bool) – Indicates if node IDs are consecutive. This is useful when converting the graph to a tensor with the preserved order.

  • batch (bool) – Indicates if the graph is a single large disconnected graph created by batching multiple graphs.

  • config (NodeConfigs) – Node type configurations.

  • config_hash (int) – Hash value of the configuration.

  • invalid_op (str) – Behavior when an invalid operation is performed.

  • rendering_order_method (str) – Method used for determining the rendering order. Set to None unless running a return_render_ordered_graph().

  • type_sequence (list) – Node type sequence for the output audio rendering. Set to None unless running a return_render_ordered_graph().

add(node_type, parameters=None, name=None)

Adds a new node to the graph.

Parameters:
  • node_type (str) – The type of the node to be added.

  • parameters (PARAMETER_TYPE, optional) – Parameters for the node (default: None).

  • name (str, optional) – Name of the node (default: None).

Returns:

The ID of the newly added node.

Return type:

int

remove(node_id)

Removes a node from the graph and returns its connected edges.

Parameters:

node_id (int) – The ID of the node to be removed.

Returns:

Incoming edges and outgoing edges of the removed node.

Return type:

Tuple[list, list]

connect(source_id, dest_id, outlet='main', inlet='main')

Connects two nodes in the graph.

Parameters:
  • source_id (int) – The ID of the source node.

  • dest_id (int) – The ID of the destination node.

  • outlet (str, optional) – The outlet of the source node (default: "main").

  • inlet (str, optional) – The inlet of the destination node (default: "main").

Returns:

None

add_serial_chain(node_list)

Adds a serial chain of nodes.

Parameters:

node_list (List[Union[str, dict]]) – A list of nodes, each given as a type or a dictionary that forms keyword arguments for the add() method.

Returns:

The IDs of the first and last nodes in the chain.

Return type:

Tuple[int, int]

class GRAFXTensor(node_types: LongTensor, edge_indices: LongTensor, counter: int, batch: bool, config: NodeConfigs, config_hash: str, invalid_op: str, edge_types: LongTensor | None = None, rendering_order_method: str | None = None, rendering_orders: LongTensor | None = None, type_sequence: LongTensor | None = None)

A dataclass representing a tensor-based graph for audio processing.

Parameters:
  • node_types (LongTensor) – Tensor of node types.

  • edge_indices (LongTensor) – Tensor of edge indices.

  • counter (Union[int, LongTensor]) – Counter for the number of nodes.

  • batch (bool) – Indicates if the tensor is part of a batch.

  • config (NodeConfigs) – Configuration for the nodes.

  • config_hash (str) – Hash of the configuration.

  • invalid_op (str) – Behavior when an invalid operation is performed.

  • edge_types (Union[LongTensor, None], optional) – Tensor of edge types (default: None).

  • rendering_order_method (Union[str, None], optional) – Method for determining the rendering order (default: None).

  • rendering_orders (Union[LongTensor, None], optional) – Tensor of rendering orders (default: None).

  • type_sequence (Union[LongTensor, None], optional) – Tensor of type sequences (default: None).

Variables:
  • num_nodes (int) – The number of nodes in the graph.

  • num_edges (int) – The number of edges in the graph.

to(device)

Moves all tensor attributes to the specified device.

Parameters:

device (torch.device) – The device to move the tensors to.

Returns:

None

convert_to_tensor(G)

Convert a graph to a collection of tensors.

Parameters:

G (GRAFX) – The graph to convert.

Returns:

A tensor representation of the given graph.

Return type:

GRAFXTensor

batch_grafx(G_list)

Batch a list of graphs into a single large disconnected graph.

Parameters:

G_list (List[GRAFX]) – A list of graphs to batch.

Returns:

A single batched graph.

Return type:

GRAFX