andrey.StructureOutput#
- class andrey.StructureOutput[source]#
The result of every structure-learning method: the learned graph plus run details.
Each method returns one (
multi_group_direct_lingamreturns one per group).structureholds the graph, aGraphStructureorTemporalStructure. The other fields depend on the method, and each method’s Returns section lists them:orderingis a causal order,weighted_adjacencyholds edge weights, andmetadataholds values such as the algorithm name and score.print(out)shows the graph kind, the edges, the order, and the metadata.savewrites the result to a file andloadreads it back. The result is read-only.- classmethod new(structure, *, ordering=None, weighted_adjacency=None, metadata=None)[source]#
Build a result from a structure and optional run details.
Use it to wrap a graph from custom code so that it prints, saves, and loads like a method’s result.
- Parameters:
structure (
Structure) – The graph: aGraphStructureorTemporalStructure.ordering (
tuple[int,...] |list[int] |None) – Causal order as node indices, causes first; a permutation ofrange(structure.n_nodes).weighted_adjacency (
ndarray|None) – Edge weights,W[i, j]fori -> j, stored sparsely.Nonegives no weights.metadata (
Mapping[str,str|bool|int|float|None|list|dict] |None) – Run values by name. Keys are strings; values arestr,bool,int,float,None, or lists and dicts of them.
- Returns:
The result.
- Return type:
- Raises:
TypeError – If
metadatahas a non-string key or a value of another type, such as a tuple, a set, or a NumPy array or integer.ValueError – If
weighted_adjacencyis not(n_nodes, n_nodes),orderingis not a permutation ofrange(n_nodes), or a graph instructurefailsvalidate().
- property structure: Structure#
a
GraphStructure, or aTemporalStructurefor time series.- Type:
The learned graph
- property ordering: tuple[int, ...] | None#
Causal order as node indices, causes first;
Noneunless the method finds one.Set by
direct_lingam,ica_lingam,multi_group_direct_lingam, andvarma_lingam(the order within one time step).
- property metadata: Mapping[str, str | bool | int | float | None | list | dict]#
Method-specific values such as
algorithmandscore, as a read-only mapping.
- property weighted_adjacency: ndarray | None#
Edge weights as a new dense
(n_nodes, n_nodes)array, orNonewithout weights.W[i, j]is the weight ofi -> j, so rows are causes: the transpose ofBinx = Bx + e. Set bycalm,direct_lingam,ica_lingam, andmulti_group_direct_lingam; a time-series result keeps its weights instructure.lag_weights.
- to_scipy_sparse(*, weights=True)[source]#
Return the edge weights as a
scipy.sparse.csr_array, without a dense array.- Parameters:
weights (
bool) – Must beTrue. For the graph’s endpoint marks, callstructure.to_scipy_sparse().- Returns:
The
weighted_adjacencyvalues: entry[i, j]is the weight ofi -> j.- Return type:
- Raises:
ValueError – If
weightsisFalse, or the method gives no edge weights.
- save(path, *, fmt='json')[source]#
Write the result to
path;StructureOutput.loadreads it back.- Parameters:
- Raises:
ValueError – If
fmtis neither"json"nor"npz", orfmt="npz"andpathdoes not end in.npz.NotImplementedError – If
fmt="npz"and the structure is aTemporalStructure.TypeError – If the structure is a
SummaryGraph; save theTemporalStructureit came from.
Notes
- Return type:
The JSON file is one object with sorted keys.
format_versionis0, or1for a panel result that keeps its occasions;loadrejects any other version.structure_typeis"graph"or"temporal". A graph is stored as its sparse arraysindptr,indices, andmarks, besidekind,n_nodes,labels,node_types,ordering,weighted, andmetadata; a temporal result stores one such graph per lag underlags. The npz file holds the same arrays, with the other fields as one JSON string, andloadreads it without pickle, so loading a file never runs code from it.
- classmethod load(path)[source]#
Read a result written by
save.After
out.save(path),StructureOutput.load(path) == out.- Parameters:
path (
str|PathLike) – File to read. A.npzsuffix reads the npz format; any other suffix reads JSON.- Returns:
The saved result.
- Return type:
- Raises:
FileNotFoundError – If
pathdoes not exist.ValueError – If the file has an unsupported format version or an unknown structure type, or holds a result that
newwould reject.