cogant.markov
The cogant.markov package partitions a program graph into internal (mu), sensory (s), active (a), and external (eta) nodes — the four disjoint sets of an Active Inference Markov blanket.
Package
cogant.markov
COGANT Markov blanket extraction.
This submodule partitions a program graph into an Active-Inference-style Markov blanket: a triple of (internal, blanket={sensory, active}, external) states that together determine the conditional independence structure of a system of interest.
The canonical Active Inference decomposition is::
μ — internal states (only interact with sensory/active/other μ)
s — sensory states (internal ← external, blanket)
a — active states (internal → external, blanket)
η — external states (only interact with sensory/active/other η)
A Markov blanket is the minimal set B = s ∪ a such that μ ⊥ η | B.
Public API
- :class:
MarkovBlanket— dataclass holding the partition and metadata. - :class:
MarkovBlanketExtractor— builds a blanket from a :class:ProgramGraphgiven a user-supplied "system of interest" seed set or an automatic seed chosen by cohesion/coupling scoring. - :func:
partition_by_seeds— low-level partitioning primitive. - :func:
serialize_blanket— JSON-ready dictionary form for the GNN bundle.
See :mod:cogant.markov.blanket, :mod:cogant.markov.extractor, and
:mod:cogant.markov.network for implementation details.
BlanketNetwork
dataclass
Collapsed four-node Active Inference network view of a blanket.
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) net = build_blanket_network(graph, blanket) isinstance(net, BlanketNetwork) True "graph LR" in net.to_mermaid() True
Source code in py/cogant/markov/network.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
aggregate_edges
instance-attribute
Dict of (from_role, to_role) → edge count.
edge_kind_breakdown
instance-attribute
Same shape as aggregate_edges but bucketed by EdgeKind.
role_counts
instance-attribute
Number of concrete nodes per role (internal, sensory, …).
role_members
instance-attribute
Node ids per role, sorted.
to_dict()
Serialize the network as a JSON-friendly dictionary.
Source code in py/cogant/markov/network.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
to_mermaid()
Render the collapsed view as a Mermaid graph LR block.
The four roles are drawn with bracketed labels summarising the node count in that role. Aggregate edges are labelled with the number of underlying concrete edges.
Source code in py/cogant/markov/network.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
BlanketRole
Bases: StrEnum
Active Inference role of a node within a Markov blanket.
Source code in py/cogant/markov/blanket.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
ACTIVE = 'active'
class-attribute
instance-attribute
a — boundary node with outgoing edges to external states.
EXTERNAL = 'external'
class-attribute
instance-attribute
η — outside the system of interest.
INTERNAL = 'internal'
class-attribute
instance-attribute
μ — inside the system of interest with no external adjacency.
SENSORY = 'sensory'
class-attribute
instance-attribute
s — boundary node with incoming edges from external states.
MarkovBlanket
dataclass
Active-Inference Markov blanket partition of a program graph.
Instances are usually produced by
:meth:MarkovBlanketExtractor.extract or by the low-level
:func:partition_by_seeds helper. The dataclass is intentionally
serialization-friendly: serialize_blanket returns a dictionary
suitable for JSON, parquet, or YAML output.
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=set()) isinstance(blanket, MarkovBlanket) True blanket.boundary_ids set()
Attributes:
| Name | Type | Description |
|---|---|---|
roles |
dict[str, BlanketRole]
|
Mapping of node id → :class: |
seeds |
set[str]
|
The seed set supplied to the partitioner. |
internal_ids |
set[str]
|
Convenience accessor for μ nodes. |
sensory_ids |
set[str]
|
Convenience accessor for s nodes. |
active_ids |
set[str]
|
Convenience accessor for a nodes. |
external_ids |
set[str]
|
Convenience accessor for η nodes. |
boundary_ids |
set[str]
|
Union of sensory and active (the blanket B). |
rationale |
dict[str, str]
|
Per-node short string explaining why the role was
assigned (e.g. |
stats |
dict[str, Any]
|
Counts and coverage ratios, convenient for reports. |
metadata |
dict[str, Any]
|
Free-form extra information (strategy used, seed heuristic, graph hash, etc.). |
Source code in py/cogant/markov/blanket.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
boundary_ids
property
Union of sensory and active nodes — the Markov blanket B.
blanket_summary()
Return a summary of blanket node counts and key statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
summary = blanket.blanket_summary() print(f"System size: {summary['system_size']}")
Source code in py/cogant/markov/blanket.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
get_active_states()
Return all active boundary states (information flowing OUT).
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of active node ids. |
Example
active = blanket.get_active_states() print(f"{len(active)} active nodes")
Source code in py/cogant/markov/blanket.py
143 144 145 146 147 148 149 150 151 152 153 | |
get_sensory_states()
Return all sensory boundary states (information flowing IN).
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of sensory node ids. |
Example
sensory = blanket.get_sensory_states() print(f"{len(sensory)} sensory nodes")
Source code in py/cogant/markov/blanket.py
131 132 133 134 135 136 137 138 139 140 141 | |
ids_by_role(role)
Return the set of node ids that carry a given role.
Source code in py/cogant/markov/blanket.py
121 122 123 124 125 126 127 128 129 | |
merge(other)
Merge two Markov blanket partitions.
Combines the seed sets (union) and recomputes partition boundaries by taking the union of internal/sensory/active/external nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
MarkovBlanket
|
Another MarkovBlanket to merge with this one. |
required |
Returns:
| Type | Description |
|---|---|
MarkovBlanket
|
A new MarkovBlanket with merged partitions. |
Example
b1 = partition_by_seeds(graph, seeds=[n1]) b2 = partition_by_seeds(graph, seeds=[n2]) merged = b1.merge(b2)
Source code in py/cogant/markov/blanket.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
role_of(node_id)
Return the role of node_id, defaulting to EXTERNAL.
Source code in py/cogant/markov/blanket.py
117 118 119 | |
to_mermaid()
Export the Markov blanket partition as a Mermaid diagram.
Returns:
| Type | Description |
|---|---|
str
|
String containing valid Mermaid graph syntax. |
Example
blanket = partition_by_seeds(graph, seeds=[...]) diagram = blanket.to_mermaid() print(diagram) graph LR subgraph Internal ...
Source code in py/cogant/markov/blanket.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
validate()
Validate partition consistency.
Checks: * All nodes appear exactly once across all role sets. * No overlap between role sets. * Seeds are a subset of internal + boundary nodes.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of validation issue strings (empty if all checks pass). |
Example
issues = blanket.validate() if issues: ... for issue in issues: ... print(f"Error: {issue}")
Source code in py/cogant/markov/blanket.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
visualize()
Export a visualization-friendly dictionary of the partition.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
vis = blanket.visualize() print(f"{len(vis['internal'])} internal nodes")
Source code in py/cogant/markov/blanket.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
MarkovBlanketExtractor
Build Markov blankets from program graphs.
The extractor is stateless beyond a reference to the source graph
and a handful of default knobs. Invoke :meth:extract with the
desired strategy and parameters and it returns a
:class:MarkovBlanket ready to feed into the GNN bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph to partition. |
required |
prefer_boundary
|
int
|
When set, a node marked as boundary AND
present in at least |
1
|
Source code in py/cogant/markov/extractor.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
extract(*, strategy='auto', seeds=None, module_names=None, kinds=None, mapping_kinds=None, semantic_mappings=None)
Compute a Markov blanket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
SeedStrategy
|
Seed selection strategy. See module docstring. |
'auto'
|
seeds
|
Iterable[str] | None
|
Explicit node ids (required when |
None
|
module_names
|
Sequence[str] | None
|
Module/package names to anchor on
( |
None
|
kinds
|
Sequence[NodeKind] | None
|
Node kinds to aggregate ( |
None
|
mapping_kinds
|
Sequence[str] | None
|
Mapping-kind strings such as |
None
|
semantic_mappings
|
dict[str, Any] | None
|
Optional mapping dict from
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
MarkovBlanket
|
class: |
MarkovBlanket
|
strategy and inputs that produced it. |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.extractor import MarkovBlanketExtractor graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) extractor = MarkovBlanketExtractor(graph) blanket = extractor.extract(strategy="auto") blanket.metadata["strategy"] 'auto'
Source code in py/cogant/markov/extractor.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
build_blanket_network(graph, blanket)
Collapse graph into a four-role aggregate network.
Every concrete edge in graph is mapped to a pair
(role_of_source, role_of_target) and counted. Edges are also
bucketed by :class:EdgeKind so that downstream tooling can see,
e.g., how many of the s→μ edges are READS vs CALLS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph used to build |
required |
blanket
|
MarkovBlanket
|
The Markov blanket partition. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
BlanketNetwork
|
class: |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) network = build_blanket_network(graph, blanket) sorted(network.role_counts.keys()) ['active', 'external', 'internal', 'sensory']
Source code in py/cogant/markov/network.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
partition_by_seeds(graph, seeds, *, adjacency=None)
Assign every node in graph a Markov blanket role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph to partition. |
required |
seeds
|
Iterable[str]
|
Iterable of node ids that define the "system of interest". Nodes NOT in this set are considered environmental. |
required |
adjacency
|
Mapping[str, tuple[set[str], set[str]]] | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
MarkovBlanket
|
A fully populated :class: |
MarkovBlanket
|
graph will appear exactly once across |
MarkovBlanket
|
|
The partitioning rule is purely topological:
.. code-block:: text
node ∈ seeds and all neighbours ∈ seeds → INTERNAL
node ∈ seeds and ∃ external neighbour → SENSORY/ACTIVE
node ∉ seeds → EXTERNAL
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) blanket.stats["total_nodes"] 0
Source code in py/cogant/markov/blanket.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
serialize_blanket(blanket, graph, *, include_rationale=True, max_nodes_per_role=None)
Convert a :class:MarkovBlanket into a JSON-friendly dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blanket
|
MarkovBlanket
|
The partition to serialize. |
required |
graph
|
ProgramGraph
|
The graph used to produce the partition. Needed so that
each node id can be enriched with its human-readable
|
required |
include_rationale
|
bool
|
Whether to include the per-node rationale
string. Defaults to |
True
|
max_nodes_per_role
|
int | None
|
Optional cap on how many nodes to emit per role (useful for very large graphs). Capping is applied deterministically by sorting node ids first. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict with the schema:: { "schema_version": "1.0.0", "seeds": [...], "stats": {...}, "roles": { "internal": [{id, kind, name, path, rationale?}, ...], "sensory": [...], "active": [...], "external": [...], }, "metadata": {...} } |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) doc = serialize_blanket(blanket, graph) doc["schema_version"] '1.0.0' sorted(doc["roles"].keys()) ['active', 'external', 'internal', 'sensory']
Source code in py/cogant/markov/blanket.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |
Blanket
The pure-function partitioner. partition_by_seeds is deterministic for a given graph and seed set.
cogant.markov.blanket
Markov blanket data model and partitioning primitive.
This module defines the :class:MarkovBlanket dataclass and a pure-function
partitioning primitive, :func:partition_by_seeds, that assigns every node
of a :class:~cogant.schemas.graph.ProgramGraph to exactly one Active
Inference role: INTERNAL, SENSORY, ACTIVE, or EXTERNAL.
The implementation is deliberately graph-theoretic rather than heuristic:
given a seed set S (the "system of interest"), every non-seed node
is labelled by looking at its bidirectional adjacency with S.
No language- or domain-specific knowledge is required, so the primitive
can be re-used for any repo translated by COGANT.
Partitioning rules (applied to the undirected projection of the graph):
- A node
n ∈ Swith no edge leavingSis internal (μ). Its only neighbours are other internal or boundary nodes inS. - A node
n ∈ Swith at least one neighbour outsideSis a boundary node. We further split boundary nodes into sensory and active based on directed edge flow:sensory— there exists a directed edge from an external node inton(information flowing IN).active— there exists a directed edge fromnto an external node (information flowing OUT).- A node that both reads and writes across the boundary is tagged
as
activeby default (because it has causal influence outward) and reported asbidirectionalin metadata.
- A node
n ∉ Swith at least one neighbour inSis part of the immediate external neighbourhood (externalwithneighbourmetadata flag). - All other nodes are external with no special flag.
The result is a complete, mutually-exclusive partition of the graph, which is exactly what is required to populate the Markov blanket section of the GNN export bundle. The function is deterministic: given the same graph and seed set, it always produces the same partition.
BlanketRole
Bases: StrEnum
Active Inference role of a node within a Markov blanket.
Source code in py/cogant/markov/blanket.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
ACTIVE = 'active'
class-attribute
instance-attribute
a — boundary node with outgoing edges to external states.
EXTERNAL = 'external'
class-attribute
instance-attribute
η — outside the system of interest.
INTERNAL = 'internal'
class-attribute
instance-attribute
μ — inside the system of interest with no external adjacency.
SENSORY = 'sensory'
class-attribute
instance-attribute
s — boundary node with incoming edges from external states.
MarkovBlanket
dataclass
Active-Inference Markov blanket partition of a program graph.
Instances are usually produced by
:meth:MarkovBlanketExtractor.extract or by the low-level
:func:partition_by_seeds helper. The dataclass is intentionally
serialization-friendly: serialize_blanket returns a dictionary
suitable for JSON, parquet, or YAML output.
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=set()) isinstance(blanket, MarkovBlanket) True blanket.boundary_ids set()
Attributes:
| Name | Type | Description |
|---|---|---|
roles |
dict[str, BlanketRole]
|
Mapping of node id → :class: |
seeds |
set[str]
|
The seed set supplied to the partitioner. |
internal_ids |
set[str]
|
Convenience accessor for μ nodes. |
sensory_ids |
set[str]
|
Convenience accessor for s nodes. |
active_ids |
set[str]
|
Convenience accessor for a nodes. |
external_ids |
set[str]
|
Convenience accessor for η nodes. |
boundary_ids |
set[str]
|
Union of sensory and active (the blanket B). |
rationale |
dict[str, str]
|
Per-node short string explaining why the role was
assigned (e.g. |
stats |
dict[str, Any]
|
Counts and coverage ratios, convenient for reports. |
metadata |
dict[str, Any]
|
Free-form extra information (strategy used, seed heuristic, graph hash, etc.). |
Source code in py/cogant/markov/blanket.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
boundary_ids
property
Union of sensory and active nodes — the Markov blanket B.
blanket_summary()
Return a summary of blanket node counts and key statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
summary = blanket.blanket_summary() print(f"System size: {summary['system_size']}")
Source code in py/cogant/markov/blanket.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
get_active_states()
Return all active boundary states (information flowing OUT).
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of active node ids. |
Example
active = blanket.get_active_states() print(f"{len(active)} active nodes")
Source code in py/cogant/markov/blanket.py
143 144 145 146 147 148 149 150 151 152 153 | |
get_sensory_states()
Return all sensory boundary states (information flowing IN).
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of sensory node ids. |
Example
sensory = blanket.get_sensory_states() print(f"{len(sensory)} sensory nodes")
Source code in py/cogant/markov/blanket.py
131 132 133 134 135 136 137 138 139 140 141 | |
ids_by_role(role)
Return the set of node ids that carry a given role.
Source code in py/cogant/markov/blanket.py
121 122 123 124 125 126 127 128 129 | |
merge(other)
Merge two Markov blanket partitions.
Combines the seed sets (union) and recomputes partition boundaries by taking the union of internal/sensory/active/external nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
MarkovBlanket
|
Another MarkovBlanket to merge with this one. |
required |
Returns:
| Type | Description |
|---|---|
MarkovBlanket
|
A new MarkovBlanket with merged partitions. |
Example
b1 = partition_by_seeds(graph, seeds=[n1]) b2 = partition_by_seeds(graph, seeds=[n2]) merged = b1.merge(b2)
Source code in py/cogant/markov/blanket.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
role_of(node_id)
Return the role of node_id, defaulting to EXTERNAL.
Source code in py/cogant/markov/blanket.py
117 118 119 | |
to_mermaid()
Export the Markov blanket partition as a Mermaid diagram.
Returns:
| Type | Description |
|---|---|
str
|
String containing valid Mermaid graph syntax. |
Example
blanket = partition_by_seeds(graph, seeds=[...]) diagram = blanket.to_mermaid() print(diagram) graph LR subgraph Internal ...
Source code in py/cogant/markov/blanket.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
validate()
Validate partition consistency.
Checks: * All nodes appear exactly once across all role sets. * No overlap between role sets. * Seeds are a subset of internal + boundary nodes.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of validation issue strings (empty if all checks pass). |
Example
issues = blanket.validate() if issues: ... for issue in issues: ... print(f"Error: {issue}")
Source code in py/cogant/markov/blanket.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
visualize()
Export a visualization-friendly dictionary of the partition.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
vis = blanket.visualize() print(f"{len(vis['internal'])} internal nodes")
Source code in py/cogant/markov/blanket.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
partition_by_seeds(graph, seeds, *, adjacency=None)
Assign every node in graph a Markov blanket role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph to partition. |
required |
seeds
|
Iterable[str]
|
Iterable of node ids that define the "system of interest". Nodes NOT in this set are considered environmental. |
required |
adjacency
|
Mapping[str, tuple[set[str], set[str]]] | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
MarkovBlanket
|
A fully populated :class: |
MarkovBlanket
|
graph will appear exactly once across |
MarkovBlanket
|
|
The partitioning rule is purely topological:
.. code-block:: text
node ∈ seeds and all neighbours ∈ seeds → INTERNAL
node ∈ seeds and ∃ external neighbour → SENSORY/ACTIVE
node ∉ seeds → EXTERNAL
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) blanket.stats["total_nodes"] 0
Source code in py/cogant/markov/blanket.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
serialize_blanket(blanket, graph, *, include_rationale=True, max_nodes_per_role=None)
Convert a :class:MarkovBlanket into a JSON-friendly dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blanket
|
MarkovBlanket
|
The partition to serialize. |
required |
graph
|
ProgramGraph
|
The graph used to produce the partition. Needed so that
each node id can be enriched with its human-readable
|
required |
include_rationale
|
bool
|
Whether to include the per-node rationale
string. Defaults to |
True
|
max_nodes_per_role
|
int | None
|
Optional cap on how many nodes to emit per role (useful for very large graphs). Capping is applied deterministically by sorting node ids first. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict with the schema:: { "schema_version": "1.0.0", "seeds": [...], "stats": {...}, "roles": { "internal": [{id, kind, name, path, rationale?}, ...], "sensory": [...], "active": [...], "external": [...], }, "metadata": {...} } |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) doc = serialize_blanket(blanket, graph) doc["schema_version"] '1.0.0' sorted(doc["roles"].keys()) ['active', 'external', 'internal', 'sensory']
Source code in py/cogant/markov/blanket.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |
Extractor
The seeded extractor with five strategies (auto, module, class, cluster, explicit).
cogant.markov.extractor
High-level Markov blanket extractor for COGANT program graphs.
:class:MarkovBlanketExtractor is the primary user-facing entry point.
It wraps :func:cogant.markov.blanket.partition_by_seeds with a set of
seed-selection strategies so that callers can either supply their own
"system of interest" or ask COGANT to discover one automatically.
Supported strategies:
"explicit"
Use an explicit list of node ids provided by the caller.
"module"
Treat a named module (or modules) and everything they transitively
contain as the system of interest. This is the most common choice
when analysing a library or package.
"kind"
Treat every node of a given :class:NodeKind (e.g. every
CLASS node, every FUNCTION node) as part of the seed set.
"auto"
Pick the module with the highest internal cohesion / external
coupling ratio, which usually corresponds to the repo's "core"
package. This is deterministic and depends only on the graph
structure.
"mapping_kind"
Seed from semantic mappings: every node that participates in a
mapping whose :class:~cogant.schemas.semantic.MappingKind is in
a user-specified set (default: HIDDEN_STATE) is included.
This links the Markov blanket back to the Active Inference state
space produced earlier in the pipeline.
Every strategy returns a fully-populated :class:MarkovBlanket, so
callers never need to know which branch was taken to get back a
partition ready for serialization into the GNN bundle.
MarkovBlanketExtractor
Build Markov blankets from program graphs.
The extractor is stateless beyond a reference to the source graph
and a handful of default knobs. Invoke :meth:extract with the
desired strategy and parameters and it returns a
:class:MarkovBlanket ready to feed into the GNN bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph to partition. |
required |
prefer_boundary
|
int
|
When set, a node marked as boundary AND
present in at least |
1
|
Source code in py/cogant/markov/extractor.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
extract(*, strategy='auto', seeds=None, module_names=None, kinds=None, mapping_kinds=None, semantic_mappings=None)
Compute a Markov blanket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
SeedStrategy
|
Seed selection strategy. See module docstring. |
'auto'
|
seeds
|
Iterable[str] | None
|
Explicit node ids (required when |
None
|
module_names
|
Sequence[str] | None
|
Module/package names to anchor on
( |
None
|
kinds
|
Sequence[NodeKind] | None
|
Node kinds to aggregate ( |
None
|
mapping_kinds
|
Sequence[str] | None
|
Mapping-kind strings such as |
None
|
semantic_mappings
|
dict[str, Any] | None
|
Optional mapping dict from
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
MarkovBlanket
|
class: |
MarkovBlanket
|
strategy and inputs that produced it. |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.extractor import MarkovBlanketExtractor graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) extractor = MarkovBlanketExtractor(graph) blanket = extractor.extract(strategy="auto") blanket.metadata["strategy"] 'auto'
Source code in py/cogant/markov/extractor.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
Network
Network-level helpers for multi-blanket analysis.
cogant.markov.network
Reduced network representations derived from a Markov blanket.
A :class:~cogant.markov.blanket.MarkovBlanket tells us the role of every
node in the graph, but consumers of COGANT (Active Inference modellers,
architecture reviewers, diff tools) usually want a reduced network
that collapses the full program graph to the canonical four-node
Active Inference schematic:
.. code-block:: text
┌──────┐ ┌──────┐
│ η │◀──────▶│ s │──────┐
└──────┘ └──────┘ ▼
┌──────┐
│ μ │
└──────┘
┌──────┐ ┌──────┐ ▲
│ η │◀──────│ a │──────┘
└──────┘ └──────┘
:func:build_blanket_network walks the original program graph and
produces:
- Aggregate edges between the four roles (η→s, s→μ, μ→a, a→η)
with counts and per-:class:
~cogant.schemas.core.EdgeKindbreakdowns. - Role bags listing which concrete nodes are behind each aggregate vertex, so the reduced view is always traceable to the full graph.
The result is serializable to JSON, Mermaid, and Cytoscape formats so that the bundle can render the Markov blanket as a diagram or load it into a network analysis tool without a second traversal.
BlanketNetwork
dataclass
Collapsed four-node Active Inference network view of a blanket.
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) net = build_blanket_network(graph, blanket) isinstance(net, BlanketNetwork) True "graph LR" in net.to_mermaid() True
Source code in py/cogant/markov/network.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
aggregate_edges
instance-attribute
Dict of (from_role, to_role) → edge count.
edge_kind_breakdown
instance-attribute
Same shape as aggregate_edges but bucketed by EdgeKind.
role_counts
instance-attribute
Number of concrete nodes per role (internal, sensory, …).
role_members
instance-attribute
Node ids per role, sorted.
to_dict()
Serialize the network as a JSON-friendly dictionary.
Source code in py/cogant/markov/network.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
to_mermaid()
Render the collapsed view as a Mermaid graph LR block.
The four roles are drawn with bracketed labels summarising the node count in that role. Aggregate edges are labelled with the number of underlying concrete edges.
Source code in py/cogant/markov/network.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
build_blanket_network(graph, blanket)
Collapse graph into a four-role aggregate network.
Every concrete edge in graph is mapped to a pair
(role_of_source, role_of_target) and counted. Edges are also
bucketed by :class:EdgeKind so that downstream tooling can see,
e.g., how many of the s→μ edges are READS vs CALLS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ProgramGraph
|
The program graph used to build |
required |
blanket
|
MarkovBlanket
|
The Markov blanket partition. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
BlanketNetwork
|
class: |
Example
from cogant.schemas.graph import ProgramGraph, GraphMetadata from cogant.markov.blanket import partition_by_seeds graph = ProgramGraph(metadata=GraphMetadata(repo_uri="demo")) blanket = partition_by_seeds(graph, seeds=[]) network = build_blanket_network(graph, blanket) sorted(network.role_counts.keys()) ['active', 'external', 'internal', 'sensory']
Source code in py/cogant/markov/network.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |