Network¶
Network representation with sparse CSC incidence matrix.
- class micropurc.network.NodeIndex(raw_ids, id_to_idx)[source]¶
Bases:
objectMapping between raw node IDs and internal 0..V-1 indices.
- Variables:
raw_ids (numpy.ndarray) – Raw node IDs in internal-index order, shape (V,).
id_to_idx (dict[Any, int]) – Inverse mapping, raw ID to internal index.
- Parameters:
- class micropurc.network.Network(nodes, edges, Z, A, node_index=None, attribute_names=None)[source]¶
Bases:
objectDirected network with sparse incidence and link attributes.
- Variables:
nodes (numpy.ndarray) – Internal node indices (0..V-1), shape (V,).
edges (numpy.ndarray) – Directed edges in internal indices, shape (L, 2).
Z (numpy.ndarray) – Link attribute matrix, shape (L, K).
A (scipy.sparse._csc.csc_matrix) – Node-link incidence matrix, sparse CSC, shape (V, L). Convention: A[i, ell] = -1 (tail), A[j, ell] = +1 (head).
node_index (micropurc.network.NodeIndex | None) – Mapping between raw node IDs and internal indices,
Nonefor a network built without raw IDs.attribute_names (list[str] | None) – Name of each column of Z, length K. Defaults to
attr_0, attr_1, ...when not supplied. Model specifications reference attributes by these names.tail – Tail node indices, shape (L,).
head – Head node indices, shape (L,).
- Parameters:
- A: csc_matrix¶
- classmethod grid(rows, cols, K, **kwargs)[source]¶
Construct a
rowsxcolsgrid network with synthetic attributes.Every grid neighbour pair contributes two directed links, one per direction.
- Parameters:
- Returns:
The grid network, with node IDs equal to their internal indices.
- Raises:
ValueError – If
rowsorcolsis not positive.- Return type:
- classmethod from_csv(csv_path, *, tail_col='init_node', head_col='term_node', attribute_cols=None)[source]¶
Construct a Network from a link CSV file, one row per directed link.
- Parameters:
csv_path (str) – Path to the link file.
tail_col (str) – Column holding the raw tail node ID.
head_col (str) – Column holding the raw head node ID.
attribute_cols (list[str] | None) – Columns to load into
Z. When omitted, every numeric column apart fromtail_colandhead_colis taken, in file order.
- Returns:
The network, with raw node IDs reachable through
node_index.- Raises:
ValueError – If a tail, head, or attribute column is absent, or if no attribute column is left to load.
- Return type: