Skip to main content

Infer communities, hierarchies, and their connection in directed graphs

Project description

directedstructure

Documentation Status PyPI version PyPI platforms

Infer communities, hierarchies, and their connection in directed graphs

Maximilian Jerdee, Elizabeth Bruch, Mark Newman

This Python package uses Bayesian inference to identify communities and hierarchies of nodes in a directed network, as well as measure the interaction between those structures.

We model community structure using a stochastic block model, hierarchy structure with a Bradley-Terry model, and their interaction according to our work (link forthcoming).

Installation

pip install directedstructure

Or build locally by cloning this repository and running

pip install .

in the base directory (requires a C++ compiler).

Usage

The public API has three objects: Config, fit, and Result.

Load a network

import directedstructure as ds
import networkx as nx

G = nx.read_gml("examples/data/networks/friends.gml")

Edges may carry a type attribute ('dominant' or 'neutral') to distinguish directed pairwise comparisons from symmetric ties. If no type information is provided all edges are treated as dominant.

Configure the model

config = ds.Config(
    groups_model="general_canonical",   # community structure preset
    hierarchy_model="bradley_terry_ties",  # auto-selected if omitted
    interaction="coupled",              # coupling between communities and hierarchy
)

Config is a frozen dataclass — see the API docs for the full list of parameters (mixing variation, degree correction, individual/group depth, etc.).

Run inference

result = ds.fit(
    config, G,
    num_samples=1000,       # posterior samples to collect
    sweeps_per_sample=10,   # MCMC sweeps between samples
    timeout=60.0,           # wall-clock limit in seconds
    seed=42,
)

Inspect results

# Per-node hierarchy scores: posterior mean and uncertainty
scores = result.score_means    # np.ndarray, shape [n_nodes]
uncertainty = result.score_stds  # np.ndarray, shape [n_nodes]

# Network-level parameter summary: mean and std of each inferred parameter
net_df = result.network_properties()

# Integer community labels for the consensus sample
partition = result.consensus_partition()

# n×n co-assignment frequency matrix
comatrix = result.coincidence_matrix()

# High-level summary dict
print(result.summary)
# {'num_samples': 500, 'mean_num_groups': 3.2, 'mdl_value': 412.1}

# Full posterior samples DataFrame
samples_df = result.samples_df()

Visualization

import matplotlib.pyplot as plt

fig, ax = ds.plot_node_properties(result)
plt.savefig("node_scores.png")

fig, ax = ds.plot_MCMC_entropy(result)   # convergence diagnostic

Export the graph with inferred attributes for use in Gephi or other tools:

ds.write_gml_with_inferences(G, "output.gml", result)

Fixing model components

Pass fixed values in Config to test submodels. For example, to fix group structure from node attributes and infer hierarchy only:

config = ds.Config(group_attribute="department", interaction="independent")
result = ds.fit(config, G)

Or fix the number of groups:

config = ds.Config(num_groups=4)
result = ds.fit(config, G)

Parallel tempering

For complex posteriors, enable parallel tempering:

result = ds.fit(config, G, num_tempering_chains=4, beta=1.0)

Further usage examples can be found in the examples/python/ directory of the repository and the package documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

directedstructure-0.2.3.tar.gz (4.4 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

directedstructure-0.2.3-cp313-cp313-win_amd64.whl (225.7 kB view details)

Uploaded CPython 3.13Windows x86-64

directedstructure-0.2.3-cp313-cp313-musllinux_1_2_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

directedstructure-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (259.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

directedstructure-0.2.3-cp313-cp313-macosx_11_0_arm64.whl (200.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

directedstructure-0.2.3-cp312-cp312-win_amd64.whl (225.7 kB view details)

Uploaded CPython 3.12Windows x86-64

directedstructure-0.2.3-cp312-cp312-musllinux_1_2_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

directedstructure-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (258.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

directedstructure-0.2.3-cp312-cp312-macosx_11_0_arm64.whl (200.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

directedstructure-0.2.3-cp311-cp311-win_amd64.whl (223.2 kB view details)

Uploaded CPython 3.11Windows x86-64

directedstructure-0.2.3-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

directedstructure-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (258.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

directedstructure-0.2.3-cp311-cp311-macosx_11_0_arm64.whl (199.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

directedstructure-0.2.3-cp310-cp310-win_amd64.whl (222.1 kB view details)

Uploaded CPython 3.10Windows x86-64

directedstructure-0.2.3-cp310-cp310-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

directedstructure-0.2.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (257.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

directedstructure-0.2.3-cp310-cp310-macosx_11_0_arm64.whl (197.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file directedstructure-0.2.3.tar.gz.

File metadata

  • Download URL: directedstructure-0.2.3.tar.gz
  • Upload date:
  • Size: 4.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for directedstructure-0.2.3.tar.gz
Algorithm Hash digest
SHA256 09ef74a3ca0bddac4f1aca9398d75546bcdfe666e7d0f7210d171e9644b1ace0
MD5 3484a50fcd534fe521bfba99f4f9e1a3
BLAKE2b-256 c244a20c9acc71912f0f76182839210947ee1b4256240e3d591dacae5cbfd406

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3.tar.gz:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 80e58cb7eb7ceb264c35bfe5f9bf33960adf84ba8b4a0390ecf087fe1687d2db
MD5 109b30b721102ceb56f0bcc90e8b4767
BLAKE2b-256 27753915269787ee41d1c2731898b0375af63676caeac4ff8f632600b5a3591c

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp313-cp313-win_amd64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a7ee7600bb08588c22dfd892d4890949ab1209c822a55dd2da0d97fd86190675
MD5 953abdf326c1d42ecd1aa8a451c142eb
BLAKE2b-256 6dfd1b79118362295f66de69f0b3aef7cf64393ee4a665ee3dfe5d61e1b13d50

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a6be7fc579bc545d9a6eaa57f46efff13e6fd3987916457cbabf33106ce92b3b
MD5 af0baf59c16d14fb59951ba10e172bd8
BLAKE2b-256 ea417d292e9fd14d990f7174d728b1abbf3b4db2c441cee1954d843bd7e2436e

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8c0e3a50533fbaf4a533dcf928b6bbf6052a5ada40e9cb1d16e7200d200c8886
MD5 aeed7f96344760ab25638098618b7502
BLAKE2b-256 6724c9b8be6322b3983469fbe8eb099042a806f637454de90bd02ca6bc1b4353

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8e8fc7d80d6b3ac614612dd4da6b564bdfc8aafc63f0e587afdfd5e4ace3e15a
MD5 be14238175646874133a7fb0c97bd40f
BLAKE2b-256 895b44d426fb09bac4598e79aaaefee61574c91f7f96a47494d2379c70da5722

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp312-cp312-win_amd64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e69ad8b2f672e563148cafed4d13773a66c0b8c83a2c9943dba0c4c4dbce6a30
MD5 aa91a5fe38fb8cdffce5ccaedaf1b857
BLAKE2b-256 20db51916aa7138f1c0a04bbc712514506bd26c2851d4c3b3ea1e6a992c537a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 23591c72f3d6b77e454ea0899e83ba7ea1397379857defcd7b1000da80299522
MD5 14e0ad27d6996bd77928a9693d188b84
BLAKE2b-256 03b2444ba86e6ed7ebfa0df601da188da0a416a7a0bffec6e9e4793480570907

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d0ba69216001aa6d799e70281cf6a34d81ae5d43b4e22003c18962c50424fe8
MD5 1e481fa3d2bb8e862fa19e18536366fa
BLAKE2b-256 94c4f21a702ecaa9fec831d76ebb7a5a2a6aef762aad7477cdfb731a05049036

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 40f5c617d0eb41b34899ffe90aac0d50daf854e2f59774dbe9a95bbc713d2544
MD5 00b65e00d96fbcef3b153dd20bd6ce7a
BLAKE2b-256 1406ac313225beb2e9aae0ea9be04d93360a8666856675b967d311a2e588b670

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp311-cp311-win_amd64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b96a41fd02c1b4b0841442a87fa8c513363959896e5166f948d8e265da75ac51
MD5 6e54d31067d8b0778077357231416c52
BLAKE2b-256 ee2a6f721e650d2ea7c9dea955d0c6ec44c5d34dc381a7722696ae9b82a2dfe3

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 33d4c1d9dfd97cc115162838cd251de70ac47126718d6999e03a60043abc698b
MD5 3567383d8fce40821b27f26372070364
BLAKE2b-256 418061fe3ce4d0e0a15475d3c515e07c9a34cc4326b4b27a784016de5edca591

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ea5984feb370b27e0eebf546244925072911592106949bd4759de1ec63b6507a
MD5 d47999c1e1ef9435fdad05db6eb54287
BLAKE2b-256 8bc88aa05ab59ece336885780ecc0d37b3591df5e69dcebd159b76ef33693e93

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b92aae51f1975705c7511c0d376dd3b6885393f6afb5d276ac2a3df07945a855
MD5 82a750b0cdbfd35e421eb3a0f9d85b3e
BLAKE2b-256 a3aa1f9288d1dd9e33404bb3c5a508940695432098b57b31b4d0925f42eb431b

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp310-cp310-win_amd64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2fa8b6548f6b95fff90cc2da530d114bc175c6681b6585acf777ce523cd72e02
MD5 753b823037bc6c2f02e095ceecf9ab0a
BLAKE2b-256 ae5d926ab5fa97c8677fe5950136f718b3d1ab7840d0e5153c9cf920b214f772

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp310-cp310-musllinux_1_2_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a7dbf235ee9a24447912406403df02be6772773ef39d98f8f5745cf8a086c385
MD5 a1effc6fc3eedb0b34032407646b2d39
BLAKE2b-256 842a395f73f4aaea0f0b469b08a1697f17090f912b656b9c4ee41f4686a800ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file directedstructure-0.2.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for directedstructure-0.2.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bbf726653b56a7b81c518497e07dd785d3538675ddd56181249506bfba03793a
MD5 5331c8ea20de5e8f89756fa7613db398
BLAKE2b-256 b009d7cc5555915f5372c493ed0dfb5d13fd02d02e2311444732dfc4a2711c78

See more details on using hashes here.

Provenance

The following attestation bundles were made for directedstructure-0.2.3-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: release.yml on maxjerdee/directedstructure

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page