A static analysis library for computing graph representations of Python programs suitable for use with graph neural networks.

Overview

python_graphs

This package is for computing graph representations of Python programs for machine learning applications. It includes the following modules:

  • control_flow For computing control flow graphs statically from Python programs.
  • data_flow For computing data flow analyses of Python programs.
  • program_graph For computing graphs statically to represent arbitrary Python programs or functions.
  • cyclomatic_complexity For computing the cyclomatic complexity of a Python function.

Installation

To install python_graphs with pip, run: pip install python_graphs.

To install python_graphs from source, run: python setup.py develop.

Common Tasks

Generate a control flow graph from a function fn:

from python_graphs import control_flow
graph = control_flow.get_control_flow_graph(fn)

Generate a program graph from a function fn:

from python_graphs import program_graph
graph = program_graph.get_program_graph(fn)

Compute the cyclomatic complexity of a function fn:

from python_graphs import control_flow
from python_graphs import cyclomatic_complexity
graph = control_flow.get_control_flow_graph(fn)
value = cyclomatic_complexity.cyclomatic_complexity(graph)

This is not an officially supported Google product.

Comments
  • Can you provide a quick start example?

    Can you provide a quick start example?

    Super cool project! Love the idea and think it has a lot of potential.

    it would be awesome to have an examples/ directory containing some sample usages - maybe even just plotting the graphs with networkX and matplotlib.

    question 
    opened by LukeWood 5
  • How do we solve the error when installing python-graphs?

    How do we solve the error when installing python-graphs?

    Hello,

    I encountered an error "fatal error: 'graphviz/cgraph.h' file not found" when trying to install python_graphs. How do I solve this issue, please? Thanks.

    question 
    opened by fraolBatole 2
  • How to generate a Holistic Data Flow Graph for a given Function ?

    How to generate a Holistic Data Flow Graph for a given Function ?

    @dbieber, Thanks for this awesome work.

    Question

    control_flow.get_control_flow_graph, returns a Control Flow Graph for a given Function Object. There is one data_flow class, Is there a way to generate a complete Data Flow Graph given a Function Object?

    Thanks.

    opened by reshinthadithyan 2
  • Rename fn to get_test_components to eliminate extra test from logs

    Rename fn to get_test_components to eliminate extra test from logs

    The function test_components was being registered as an unsupported test, when in reality it was meant as a helper function for tests. Renaming resolves this.

    opened by dbieber 0
  • get_start_control_flow_node, next_from_end, raise edges, and labels in branches

    get_start_control_flow_node, next_from_end, raise edges, and labels in branches

    • Adds get_start_control_flow_node to ControlFlowGraph
    • Adds next_from_end to ControlFlowNode
    • Uses labels (e.g. '' and '' strings) to indicate these special nodes
    • Support keyword only arguments without defaults
    • Add non-interrupting edges from raise statements
    • Bump version number
    opened by dbieber 0
  • Separate branch kinds

    Separate branch kinds

    Splits "branches" into branches, except_branches, and reraise_branches.

    branches are you're usual branch decisions: ifs, fors, and whiles. except_branches are at "except E:" statements, with True indicating the exception matches and False indicating it does not reraise_branches are at the end of "finally:" blocks, with True indicating the path taken after finally if an error has been raised previously, and False indicating the path taken if there's nothing to reraise at the end of the finally.

    opened by dbieber 0
  • Add module frame to catch raises in top-level code.

    Add module frame to catch raises in top-level code.

    Add module frame to catch raises in top-level code. Also marks except expressions and finally blocks as branch points.

    An "except A:"'s branch decision is whether the current exception matches A. At the end of a finally block, the branch decision is whether an exception is currently being raised.

    This includes https://github.com/google-research/python-graphs/pull/3: Splits "branches" into branches, except_branches, and reraise_branches.

    branches are your usual branch decisions: ifs, fors, and whiles. except_branches are at "except E:" statements, with True indicating the exception matches and False indicating it does not reraise_branches are at the end of "finally:" blocks, with True indicating the path taken after finally if an error has been raised previously, and False indicating the path taken if there's nothing to reraise at the end of the finally.

    opened by dbieber 0
  • KeyError when trying to get program_graph

    KeyError when trying to get program_graph

    When I try to create a program graph, I encounter a KeyError. If I remove all the and and or expressions from the python file (buggy.py) the error does not occur.

    This is how I use the library:

    graph = program_graph.get_program_graph(code)
    program_graph_graphviz.render(graph, path='source.png')
    

    where code is simply the code in the attached file buggy.py.txt.

    I have also attached the log file log.txt.

    buggy.py.txt

    log.txt

    More information: python 3.9.5 commit head=44c15b92197f374c3550353ff827997ef1c1d857 gast 0.5.3

    opened by ppashakhanloo 1
Releases(v1.2.3)
  • v1.2.3(Oct 7, 2021)

    get_start_control_flow_node, next_from_end, raise edges, and labels in branches (#6)

    * Adds get_start_control_flow_node to ControlFlowGraph
    * Adds next_from_end to ControlFlowNode
    * Uses labels (e.g. '<exit>' and '<raise>' strings) to indicate these special nodes
    * Support keyword only arguments without defaults
    * Add non-interrupting edges from raise statements
    * Bump version number
    
    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Oct 5, 2021)

    Introduce get_branches API on control flow nodes. Previously the new branch types (except_branches and reraise_branches) were only accessible on basic blocks, not on individual nodes.

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Oct 5, 2021)

    1. Adds a module frame to catch raises in top-level code.
    2. Also marks except expressions and finally blocks as branch points.

    The branch kinds are: branches, except_branches, and reraise_branches.

    • branches are your usual branch decisions: ifs, fors, and whiles.
    • except_branches are at "except E:" statements, with True indicating the exception matches and False indicating it does not
    • reraise_branches are at the end of "finally:" blocks, with True indicating the path taken after finally if an error has been raised previously, and False indicating the path taken if there's nothing to reraise at the end of the finally.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(May 7, 2021)

  • v1.0.0(Apr 12, 2021)

    v1.0.0

    Initial public release of the python_graphs library.

    Core features:

    • control flow graph generation
    • data flow analyses
    • program graph construction
    • cyclomatic complexity calculation
    • a solid test suite for all the above
    • visualizations using graphviz for each of the graph representations
    Source code(tar.gz)
    Source code(zip)
Owner
Google Research
Google Research
Sky Computing: Accelerating Geo-distributed Computing in Federated Learning

Sky Computing Introduction Sky Computing is a load-balanced framework for federated learning model parallelism. It adaptively allocate model layers to

HPC-AI Tech 72 Dec 27, 2022
This repo is a C++ version of yolov5_deepsort_tensorrt. Packing all C++ programs into .so files, using Python script to call C++ programs further.

yolov5_deepsort_tensorrt_cpp Introduction This repo is a C++ version of yolov5_deepsort_tensorrt. And packing all C++ programs into .so files, using P

null 41 Dec 27, 2022
Static Features Classifier - A static features classifier for Point-Could clusters using an Attention-RNN model

Static Features Classifier This is a static features classifier for Point-Could

ABDALKARIM MOHTASIB 1 Jan 25, 2022
A lightweight Python-based 3D network multi-agent simulator. Uses a cell-based congestion model. Calculates risk, loudness and battery capacities of the agents. Suitable for 3D network optimization tasks.

AMAZ3DSim AMAZ3DSim is a lightweight python-based 3D network multi-agent simulator. It uses a cell-based congestion model. It calculates risk, battery

Daniel Hirsch 13 Nov 4, 2022
Python project to take sound as input and output as RGB + Brightness values suitable for DMX

sound-to-light Python project to take sound as input and output as RGB + Brightness values suitable for DMX Current goals: Get one pixel working: Vary

Bobby Cox 1 Nov 17, 2021
ESGD-M - A stochastic non-convex second order optimizer, suitable for training deep learning models, for PyTorch

ESGD-M - A stochastic non-convex second order optimizer, suitable for training deep learning models, for PyTorch

Katherine Crowson 53 Dec 29, 2022
Library for implementing reservoir computing models (echo state networks) for multivariate time series classification and clustering.

Framework overview This library allows to quickly implement different architectures based on Reservoir Computing (the family of approaches popularized

Filippo Bianchi 249 Dec 21, 2022
Code for ICML 2021 paper: How could Neural Networks understand Programs?

OSCAR This repository contains the source code of our ICML 2021 paper How could Neural Networks understand Programs?. Environment Run following comman

Dinglan Peng 115 Dec 17, 2022
Lyapunov-guided Deep Reinforcement Learning for Stable Online Computation Offloading in Mobile-Edge Computing Networks

PyTorch code to reproduce LyDROO algorithm [1], which is an online computation offloading algorithm to maximize the network data processing capability subject to the long-term data queue stability and average power constraints. It applies Lyapunov optimization to decouple the multi-stage stochastic MINLP into deterministic per-frame MINLP subproblems and solves each subproblem via DROO algorithm. It includes:

Liang HUANG 87 Dec 28, 2022
xitorch: differentiable scientific computing library

xitorch is a PyTorch-based library of differentiable functions and functionals that can be widely used in scientific computing applications as well as deep learning.

null 24 Apr 15, 2021
Differentiable scientific computing library

xitorch: differentiable scientific computing library xitorch is a PyTorch-based library of differentiable functions and functionals that can be widely

null 98 Dec 26, 2022
Complex-Valued Neural Networks (CVNN)Complex-Valued Neural Networks (CVNN)

Complex-Valued Neural Networks (CVNN) Done by @NEGU93 - J. Agustin Barrachina Using this library, the only difference with a Tensorflow code is that y

youceF 1 Nov 12, 2021
The source code of the paper "Understanding Graph Neural Networks from Graph Signal Denoising Perspectives"

GSDN-F and GSDN-EF This repository provides a reference implementation of GSDN-F and GSDN-EF as described in the paper "Understanding Graph Neural Net

Guoji Fu 18 Nov 14, 2022
Some tentative models that incorporate label propagation to graph neural networks for graph representation learning in nodes, links or graphs.

Some tentative models that incorporate label propagation to graph neural networks for graph representation learning in nodes, links or graphs.

zshicode 1 Nov 18, 2021
On Size-Oriented Long-Tailed Graph Classification of Graph Neural Networks

On Size-Oriented Long-Tailed Graph Classification of Graph Neural Networks We provide the code (in PyTorch) and datasets for our paper "On Size-Orient

Zemin Liu 4 Jun 18, 2022
A Research-oriented Federated Learning Library and Benchmark Platform for Graph Neural Networks. Accepted to ICLR'2021 - DPML and MLSys'21 - GNNSys workshops.

FedGraphNN: A Federated Learning System and Benchmark for Graph Neural Networks A Research-oriented Federated Learning Library and Benchmark Platform

FedML-AI 175 Dec 1, 2022
Bayesian-Torch is a library of neural network layers and utilities extending the core of PyTorch to enable the user to perform stochastic variational inference in Bayesian deep neural networks

Bayesian-Torch is a library of neural network layers and utilities extending the core of PyTorch to enable the user to perform stochastic variational inference in Bayesian deep neural networks. Bayesian-Torch is designed to be flexible and seamless in extending a deterministic deep neural network architecture to corresponding Bayesian form by simply replacing the deterministic layers with Bayesian layers.

Intel Labs 210 Jan 4, 2023
This repository contains notebook implementations of the following Neural Process variants: Conditional Neural Processes (CNPs), Neural Processes (NPs), Attentive Neural Processes (ANPs).

The Neural Process Family This repository contains notebook implementations of the following Neural Process variants: Conditional Neural Processes (CN

DeepMind 892 Dec 28, 2022