Skip to content

CSV

Read a graph from a CSV file or a dataframe, and write its edges to CSV. from_csv and from_dataframe detect the layout of the table: an edge list, a hyperedge table, an incidence matrix, an adjacency matrix or neighbor lists. If the detection is wrong, give the layout with schema=. A column that does not describe the structure becomes an attribute.

annnet.io.csv_format.from_csv

from_csv(
    path,
    *,
    graph=None,
    schema="auto",
    default_slice=None,
    default_directed=None,
    default_weight=1.0,
    infer_schema_length=10000,
    encoding=None,
    null_values=None,
    low_memory=True,
    **kwargs
)

Load a CSV and construct/augment an AnnNet.

Parameters:

Name Type Description Default
path str

Path to the CSV file.

required
graph AnnNet or None

If provided, mutate this graph; otherwise create a new AnnNet using AnnNet(**kwargs).

None
schema ('auto', 'edge_list', 'hyperedge', 'incidence', 'adjacency', 'lil')

Parsing mode. 'auto' tries to infer the schema from columns and types.

'auto','edge_list','hyperedge','incidence','adjacency','lil'
default_slice str or None

slice to register nodes/edges when none is specified in the data.

None
default_directed bool or None

Default directedness for binary edges when not implied by data.

None
default_weight float

Default weight when not specified.

1.0
infer_schema_length int

Row count used by backends that support bounded schema inference.

10000
encoding str or None

File encoding override.

None
null_values list[str] or None

Additional strings to interpret as nulls.

None
low_memory bool

Pass to backends that support low-memory CSV reading.

True
**kwargs Any

Passed to AnnNet constructor if graph is None.

{}

Returns:

Type Description
AnnNet

The populated graph instance.

Raises:

Type Description
RuntimeError

If no AnnNet can be constructed or imported.

ValueError

If schema is unknown or parsing fails.

annnet.io.csv_format.from_dataframe

from_dataframe(
    df,
    *,
    graph=None,
    schema="auto",
    default_slice=None,
    default_directed=None,
    default_weight=1.0,
    **kwargs
)

Build/augment an AnnNet from a Narwhals-compatible dataframe.

Parameters:

Name Type Description Default
df DataFrame - like

Input table parsed from CSV.

required
graph AnnNet or None

If provided, mutate this graph; otherwise create a new AnnNet using AnnNet(**kwargs).

None
schema ('auto', 'edge_list', 'hyperedge', 'incidence', 'adjacency', 'lil')

Parsing mode. 'auto' tries to infer the schema.

'auto','edge_list','hyperedge','incidence','adjacency','lil'
default_slice str or None

Fallback slice if no slice is specified in the data.

None
default_directed bool or None

Default directedness for binary edges when not implied by data.

None
default_weight float

Weight to use when no explicit weight is present.

1.0

Returns:

Type Description
AnnNet

The populated graph instance.

annnet.io.csv_format.to_csv

to_csv(graph, path, *, sidecar=True)

Write the graph as an edge list from_csv reads back.

annnet.io.csv_format.edges_to_csv

edges_to_csv(G, path, slice=None)

Export the binary edge subgraph to a CSV [Comma-Separated Values] file.

Parameters:

Name Type Description Default
G AnnNet

AnnNet instance to export. Must support edges_view with columns compatible with binary endpoints (e.g., 'source', 'target').

required
path str or Path

Output path for the CSV file.

required
slice str

Restrict the export to a specific slice. If None, all slices are exported.

None

Returns:

Type Description
None
Notes
  • Only binary edges are exported. Hyperedges (edges connecting more than two entities) are ignored.
  • Output columns include: 'source', 'target', 'weight', 'directed', and 'slice'.
  • If a weight column does not exist, a default weight of 1.0 is written.
  • If a directedness column is absent, it will be written as None.
  • This format is compatible with from_csv(schema="edge_list").

annnet.io.csv_format.hyperedges_to_csv

hyperedges_to_csv(G, path, slice=None, directed=None)

Export hyperedges from the graph to a CSV [Comma-Separated Values] file.

Parameters:

Name Type Description Default
G AnnNet

AnnNet instance to export. Must support edges_view exposing either 'members' (for undirected hyperedges) or 'head'/'tail' (for directed hyperedges).

required
path str or Path

Output path for the CSV file.

required
slice str

Restrict the export to a specific slice. If None, all slices are exported.

None
directed bool

Force treatment of hyperedges as directed or undirected. If None, the function attempts to infer directedness from the graph.

None

Returns:

Type Description
None
Notes
  • If the graph exposes a 'members' column, the output will contain one row per undirected hyperedge.
  • If 'head' and 'tail' columns are present, the output will contain one row per directed hyperedge. If directed=False is passed, 'head' and 'tail' are merged into a 'members' column.
  • A 'weight' column is included if available; otherwise, all weights default to 1.0.
  • A 'slice' column is included if present or if slice is specified.
  • This format is compatible with from_csv(schema="hyperedge").
  • If the graph does not expose hyperedge columns, a ValueError is raised.