Skip to main content

Local graph estimation with pathwise feature selection

Local graph estimation is a framework for discovering local graph/network structure around specific variables of interest. Pathwise feature selection (PFS) is an algorithm for performing local graph estimation with pathwise false discovery control.

Associated paper

Installation

pip install localgraph

Usage

from localgraph import pfs, plot_graph

# Load n-by-p data matrix X (n samples, p features)

# Specify the target features (list of indices)
target_features = [0, 1]

# Specify the pathwise q-value threshold
qpath_max = 0.2

# Optional: specify the maximum radius of the local graph (default is 3)
max_radius = 3

# Optional: specify the neighborhood FDR thresholds for nodes in each radius
fdr_local = [0.2, 0.1, 0.1]

# Run PFS (uses all available cores by default; set n_jobs=1 to disable parallelism)
Q = pfs(X, target_features, qpath_max=qpath_max, max_radius=max_radius, fdr_local=fdr_local)

# Plot the estimated subgraph
plot_graph(graph=Q, target_features=target_features, radius=max_radius)

Outputs

  • Q: Dictionary mapping edges (i,j) to q-values. Edges are undirected, so (i,j) and (j,i) are included.

What PFS does

  • Expands the local graph outward, layer by layer, starting from target variables.
  • Performs neighborhood selection with FDR control using integrated path stability selection.
  • Controls pathwise false discoveries by summing q-values along candidate paths.

Full list of pfs arguments

Required arguments:

  • X: n-by-p data matrix (NumPy array). Each column is a feature/variable.
  • target_features: Feature index or list of indices to center the graph around.
  • qpath_max: Maximum allowed sum of q-values along any path.

Optional arguments:

  • max_radius: Maximum number of expansion layers around each target (int; default 3).
  • fdr_local: Neighborhood FDR threshold at each radius (list of length max_radius; default [qpath_max]*max_radius).
  • custom_nbhd: Dictionary specifying custom FDR cutoffs for certain features (dict; default None).
  • feature_names: List of feature names; required if custom_nbhd is provided (list of strings).
  • criterion: Rule for resolving multiple edges (default 'min').
  • n_jobs: Number of cores to use for parallel computation (int; default -1 uses all available cores). Passed directly to qvalue_method; has no effect for custom methods that do not support n_jobs. On machines with multiple cores this can substantially reduce runtime.
  • qvalue_method: A method for computing q-values (function)
  • method_args: Dictionary of arguments to pass to qvalue_method (dict; default None). Any n_jobs key here takes precedence over the n_jobs argument.
  • verbose: Whether to print progress during selection (bool; default False)

Graph plotting

Use plot_graph to visualize a local graph up to the specified radius around one or more target features.

from localgraph import plot_graph

# Plot local graph around target_features using output Q from pfs
plot_graph(graph=Q, target_features=target_features, radius=3)

Features and customization

plot_graph visualizes a local graph of a user-specified radius around one or more target features. It supports:

  • Flexible input formats: edge dictionary, adjacency matrix, or NetworkX graph
  • Automatic subgraph extraction around the targets
  • Node coloring by distance from the target(s) (default), or user-specified colors (e.g., by variable type)
  • Several layout algorithms ('kamada_kawai', 'spring', 'circular', etc.)
  • Customizable node size, font sizes, and edge thickness
  • Optional display of q-values; edge widths can reflect q-value strength (edge_widths='q_value')
  • False positives shown in red if the true graph is provided
  • Integration with custom plots via ax or pos
  • Optional saving of figures (save_fig) and graphs (save_graph)

For a full list of arguments, see the plot_graph docstring.

Returns

The function returns a dictionary containing:

  • feature_radius_list: List of (feature name, radius) pairs for all nodes in the graph.
  • graph: The NetworkX subgraph used for plotting.
  • positions: Dictionary of node coordinates.
  • figure: The matplotlib figure object (only if the function creates the figure).

Further customization

To manually adjust node positions for publication-quality figures, you can export graphs to Gephi, edit them interactively, and re-import the updated layout into Python. See: gephi_instructions.md for a full walkthrough.

Examples

The examples/ folder contains scripts that demonstrate end-to-end usage:

  • simple_example.py: Simulate data, run PFS, and visualize the result.

Evaluation tools

The evaluation/ folder contains helper functions for measuring subgraph recovery in simulation settings.

  • The eval.py script contains one function:
    • tp_and_fp: Count true and false positives compared to ground truth

These are useful for benchmarking PFS and other graph estimation methods when the true graph is known.

Metadata

Release files for localgraph 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for localgraph 0.1.5
File Size Uploaded Provenance Actions
localgraph-0.1.5.tar.gz 15.5 kB Without provenance Details for localgraph-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for localgraph 0.1.5
File Interpreter ABI Platform Actions
localgraph-0.1.5-py3-none-any.whl Python 3 none any Details for localgraph-0.1.5-py3-none-any.whl

Total release size: 32.0 kB

Release files / localgraph-0.1.5.tar.gz

Download URL localgraph-0.1.5.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
00ee04ab54067485fb7b6908f7888236e2b7cd525a604f6d042681d7cfcb3be9
BLAKE2b-256 checksum
How to use checksums
c07d27d47532de785fd3f666b4b661cd859d7b4410efa46216e88445c442ccd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / localgraph-0.1.5-py3-none-any.whl

Download URL localgraph-0.1.5-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50ba30269b23c9e4bcd247ac84120344a94be85eff2899b73a649bf9b25f5b92
BLAKE2b-256 checksum
How to use checksums
a67e2ae1af6ff43fd2e0d39dbeb1893cf9cbf607ba88744dafbae2c13b081098
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for localgraph 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page