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
Official PyTorch implementation of "Camera Distance-aware Top-down Approach for 3D Multi-person Pose Estimation from a Single RGB Image", ICCV 2019

PoseNet of "Camera Distance-aware Top-down Approach for 3D Multi-person Pose Estimation from a Single RGB Image" Introduction This repo is official Py

Gyeongsik Moon 677 Dec 25, 2022
Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more

Apache MXNet (incubating) for Deep Learning Apache MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to m

The Apache Software Foundation 20.2k Jan 08, 2023
Official repository for the ICLR 2021 paper Evaluating the Disentanglement of Deep Generative Models with Manifold Topology

Official repository for the ICLR 2021 paper Evaluating the Disentanglement of Deep Generative Models with Manifold Topology Sharon Zhou, Eric Zelikman

Stanford Machine Learning Group 34 Nov 16, 2022
Two-stage CenterNet

Probabilistic two-stage detection Two-stage object detectors that use class-agnostic one-stage detectors as the proposal network. Probabilistic two-st

Xingyi Zhou 1.1k Jan 03, 2023
Implementations of paper Controlling Directions Orthogonal to a Classifier

Classifier Orthogonalization Implementations of paper Controlling Directions Orthogonal to a Classifier , ICLR 2022, Yilun Xu, Hao He, Tianxiao Shen,

Yilun Xu 33 Dec 01, 2022
A benchmark dataset for emulating atmospheric radiative transfer in weather and climate models with machine learning (NeurIPS 2021 Datasets and Benchmarks Track)

ClimART - A Benchmark Dataset for Emulating Atmospheric Radiative Transfer in Weather and Climate Models Official PyTorch Implementation Using deep le

21 Dec 31, 2022
A new GCN model for Point Cloud Analyse

Pytorch Implementation of PointNet and PointNet++ This repo is implementation for VA-GCN in pytorch. Classification (ModelNet10/40) Data Preparation D

12 Feb 02, 2022
Official code of ICCV2021 paper "Residual Attention: A Simple but Effective Method for Multi-Label Recognition"

CSRA This is the official code of ICCV 2021 paper: Residual Attention: A Simple But Effective Method for Multi-Label Recoginition Demo, Train and Vali

163 Dec 22, 2022
A cross-lingual COVID-19 fake news dataset

CrossFake An English-Chinese COVID-19 fake&real news dataset from the ICDMW 2021 paper below: Cross-lingual COVID-19 Fake News Detection. Jiangshu Du,

Yingtong Dou 11 Dec 01, 2022
Unified Pre-training for Self-Supervised Learning and Supervised Learning for ASR

UniSpeech The family of UniSpeech: UniSpeech (ICML 2021): Unified Pre-training for Self-Supervised Learning and Supervised Learning for ASR UniSpeech-

Microsoft 282 Jan 09, 2023
An open software package to develop BCI based brain and cognitive computing technology for recognizing user's intention using deep learning

An open software package to develop BCI based brain and cognitive computing technology for recognizing user's intention using deep learning

deepbci 272 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
[ICCV 2021] Self-supervised Monocular Depth Estimation for All Day Images using Domain Separation

ADDS-DepthNet This is the official implementation of the paper Self-supervised Monocular Depth Estimation for All Day Images using Domain Separation I

LIU_LINA 52 Nov 24, 2022
A real-time motion capture system that estimates poses and global translations using only 6 inertial measurement units

TransPose Code for our SIGGRAPH 2021 paper "TransPose: Real-time 3D Human Translation and Pose Estimation with Six Inertial Sensors". This repository

Xinyu Yi 261 Dec 31, 2022
Capstone-Project-2 - A game program written in the Python language

Capstone-Project-2 My Pygame Game Information: Description This Pygame project i

Nhlakanipho Khulekani Hlophe 1 Jan 04, 2022
A new video text spotting framework with Transformer

TransVTSpotter: End-to-end Video Text Spotter with Transformer Introduction A Multilingual, Open World Video Text Dataset and End-to-end Video Text Sp

weijiawu 67 Jan 03, 2023
This Deep Learning Model Predicts that from which disease you are suffering.

Deep-Learning-Project This Deep Learning Model Predicts that from which disease you are suffering. This Project Covers the Topics of Deep Learning Int

Jai Viral Doshi 0 Jan 20, 2022
TextBPN Adaptive Boundary Proposal Network for Arbitrary Shape Text Detection

TextBPN Adaptive Boundary Proposal Network for Arbitrary Shape Text Detection; Accepted by ICCV2021. Note: The complete code (including training and t

S.X.Zhang 84 Dec 13, 2022
AISTATS 2019: Confidence-based Graph Convolutional Networks for Semi-Supervised Learning

Confidence-based Graph Convolutional Networks for Semi-Supervised Learning Source code for AISTATS 2019 paper: Confidence-based Graph Convolutional Ne

MALL Lab (IISc) 56 Dec 03, 2022
SpecAugmentPyTorch - A Pytorch (support batch and channel) implementation of GoogleBrain's SpecAugment: A Simple Data Augmentation Method for Automatic Speech Recognition

SpecAugment An implementation of SpecAugment for Pytorch How to use Install pytorch, version=1.9.0 (new feature (torch.Tensor.take_along_dim) is used

IMLHF 3 Oct 11, 2022