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
《Dual-Resolution Correspondence Network》(NeurIPS 2020)

Dual-Resolution Correspondence Network Dual-Resolution Correspondence Network, NeurIPS 2020 Dependency All dependencies are included in asset/dualrcne

Active Vision Laboratory 45 Nov 21, 2022
Real-time LIDAR-based Urban Road and Sidewalk detection for Autonomous Vehicles 🚗

urban_road_filter: a real-time LIDAR-based urban road and sidewalk detection algorithm for autonomous vehicles Dependency ROS (tested with Kinetic and

JKK - Vehicle Industry Research Center 180 Dec 12, 2022
Ladder Variational Autoencoders (LVAE) in PyTorch

Ladder Variational Autoencoders (LVAE) PyTorch implementation of Ladder Variational Autoencoders (LVAE) [1]: where the variational distributions q at

Andrea Dittadi 63 Dec 22, 2022
Fully Automatic Page Turning on Real Scores

Fully Automatic Page Turning on Real Scores This repository contains the corresponding code for our extended abstract Henkel F., Schwaiger S. and Widm

Florian Henkel 7 Jan 02, 2022
Classifies galaxy morphology with Bayesian CNN

Zoobot Zoobot classifies galaxy morphology with deep learning. This code will let you: Reproduce and improve the Galaxy Zoo DECaLS automated classific

Mike Walmsley 39 Dec 20, 2022
A fast, distributed, high performance gradient boosting (GBT, GBDT, GBRT, GBM or MART) framework based on decision tree algorithms, used for ranking, classification and many other machine learning tasks.

Light Gradient Boosting Machine LightGBM is a gradient boosting framework that uses tree based learning algorithms. It is designed to be distributed a

Microsoft 14.5k Jan 08, 2023
PaddleViT: State-of-the-art Visual Transformer and MLP Models for PaddlePaddle 2.0+

PaddlePaddle Vision Transformers State-of-the-art Visual Transformer and MLP Models for PaddlePaddle 🤖 PaddlePaddle Visual Transformers (PaddleViT or

1k Dec 28, 2022
A Dynamic Residual Self-Attention Network for Lightweight Single Image Super-Resolution

DRSAN A Dynamic Residual Self-Attention Network for Lightweight Single Image Super-Resolution Karam Park, Jae Woong Soh, and Nam Ik Cho Environments U

4 May 10, 2022
Neurons Dataset API - The official dataloader and visualization tools for Neurons Datasets.

Neurons Dataset API - The official dataloader and visualization tools for Neurons Datasets. Introduction We propose our dataloader API for loading and

1 Nov 19, 2021
Zero-shot Learning by Generating Task-specific Adapters

Code for "Zero-shot Learning by Generating Task-specific Adapters" This is the repository containing code for "Zero-shot Learning by Generating Task-s

INK Lab @ USC 11 Dec 17, 2021
Hard cater examples from Hopper ICLR paper

CATER-h Honglu Zhou*, Asim Kadav, Farley Lai, Alexandru Niculescu-Mizil, Martin Renqiang Min, Mubbasir Kapadia, Hans Peter Graf (*Contact: honglu.zhou

NECLA ML Group 6 May 11, 2021
Code for our paper "MG-GAN: A Multi-Generator Model Preventing Out-of-Distribution Samples in Pedestrian Trajectory Prediction" published at ICCV 2021.

MG-GAN: A Multi-Generator Model Preventing Out-of-Distribution Samples in Pedestrian Trajectory Prediction This repository contains the code for the p

Sven 30 Jan 05, 2023
CLIP + VQGAN / PixelDraw

clipit Yet Another VQGAN-CLIP Codebase This started as a fork of @nerdyrodent's VQGAN-CLIP code which was based on the notebooks of @RiversWithWings a

dribnet 276 Dec 12, 2022
Which Style Makes Me Attractive? Interpretable Control Discovery and Counterfactual Explanation on StyleGAN

Interpretable Control Exploration and Counterfactual Explanation (ICE) on StyleGAN Which Style Makes Me Attractive? Interpretable Control Discovery an

Bo Li 11 Dec 01, 2022
This GitHub repository contains code used for plots in NeurIPS 2021 paper 'Stochastic Multi-Armed Bandits with Control Variates.'

About Repository This repository contains code used for plots in NeurIPS 2021 paper 'Stochastic Multi-Armed Bandits with Control Variates.' About Code

Arun Verma 1 Nov 09, 2021
A pre-trained model with multi-exit transformer architecture.

ElasticBERT This repository contains finetuning code and checkpoints for ElasticBERT. Towards Efficient NLP: A Standard Evaluation and A Strong Baseli

fastNLP 48 Dec 14, 2022
QMagFace: Simple and Accurate Quality-Aware Face Recognition

Quality-Aware Face Recognition 26.11.2021 start readme QMagFace: Simple and Accurate Quality-Aware Face Recognition Research Paper Implementation - To

Philipp Terhörst 59 Jan 04, 2023
Does MAML Only Work via Feature Re-use? A Data Set Centric Perspective

Does-MAML-Only-Work-via-Feature-Re-use-A-Data-Set-Centric-Perspective Does MAML Only Work via Feature Re-use? A Data Set Centric Perspective Installin

2 Nov 07, 2022
PyTorch implementation of UNet++ (Nested U-Net).

PyTorch implementation of UNet++ (Nested U-Net) This repository contains code for a image segmentation model based on UNet++: A Nested U-Net Architect

4ui_iurz1 642 Jan 04, 2023
👨‍💻 run nanosaur in simulation with Gazebo/Ingnition

🦕 👨‍💻 nanosaur_gazebo nanosaur The smallest NVIDIA Jetson dinosaur robot, open-source, fully 3D printable, based on ROS2 & Isaac ROS. Designed & ma

nanosaur 9 Jul 19, 2022