Teaching machines to understand data science code by semantic enrichment of dataflow graphs

Evan Patterson, Ioana Baldini, Aleksandra Mojsilovic, Kush R. VarshneyarXiv 2018

TL;DR

This paper introduces an AI system that translates data science code into high-level, language-independent "semantic flow graphs." By mapping raw execution traces to a specialized Data Science Ontology using a new language called Monocl, the system can recognize that syntactically different programs—such as k-means clustering implemented in Python versus R—are semantically equivalent.

What Problem Does This Paper Address?

While computers are excellent at executing code, they lack a conceptual understanding of what that code means. In the rapidly growing field of data science, this is a major hurdle. Data scientists often work across different languages (Python, R, Julia) and a dizzying array of libraries (Scikit-learn, Pandas, Tidyverse).

Existing program analysis tools are typically language-specific and focus on low-level syntax or control flow. This makes it difficult to build intelligent assistants that can automatically document code, visualize complex workflows, or perform "meta-analyses" across different research projects. The authors aim to bridge this gap by creating a system that understands the underlying data science concepts—like "fitting a model" or "loading a table"—regardless of how they are implemented.

Methods

The authors developed a multi-stage pipeline for "semantic enrichment" of code:

  1. Dynamic Program Analysis: The system executes the user's code and records the actual function calls and data objects. This produces a Raw Flow Graph, which is a language-dependent map of how data moves through specific library functions.
  2. Monocl Ontology Language: To represent these concepts rigorously, the authors created Monocl (MONoidal Ontology and Computing Language). Based on category theory, Monocl uses "wiring diagrams" to represent types as wires and functions as boxes.
  3. Data Science Ontology: They built a knowledge base containing abstract concepts (e.g., "k-means clustering") and annotations that map concrete library functions (e.g., sklearn.cluster.KMeans.fit) to these abstract concepts.
  4. Semantic Enrichment Algorithm: This is the core innovation. It takes the Raw Flow Graph and applies two transformations:
  5. Expansion: Replaces concrete library calls with their abstract definitions from the ontology.
  6. Contraction: Simplifies the graph by grouping unannotated or "unknown" code into single blocks, ensuring the final graph remains readable.
Figure 2. System architecture

Datasets

The paper demonstrates the system's utility using two primary sources:

  • Pedagogical Examples: Three versions of a k-means clustering analysis performed on the Iris dataset. One version uses Python with NumPy/SciPy, another uses Python with Pandas/Scikit-learn, and the third uses R.
  • Real-World Challenge Data: R source code submitted by a top-ranking team for the Rheumatoid Arthritis DREAM Challenge, a biomedical data science competition. This code is significantly more complex than the toy examples, involving data "munging," variable selection (VIF regression), and predictive modeling (Cubist regression).

Key Insights and Ideas

  • Semantic Equivalence: The system's biggest "aha moment" is its ability to generate the exact same semantic flow graph for three syntactically different programs. Whether you use an object-oriented style in Python or a functional style in R, the system recognizes the shared conceptual workflow.
  • Category Theory as a Bridge: By using monoidal categories, the authors provide a rigorous mathematical foundation for dataflow. This allows them to handle complex programming features like subtyping (e.g., a "matrix" is a subtype of an "array") and polymorphism through "implicit conversions."
  • Graceful Degradation: The system does not require a complete ontology to be useful. If it encounters code it doesn't recognize, it labels it with a question mark and continues, preserving the overall structure of the known parts of the analysis.
Figure 1. Semantic flow graph for three versions of k-means clustering analysis (Listings 1, 2 and 3)

Key Results

  • Cross-Language Understanding: The system successfully mapped diverse implementations of k-means clustering to a single, unified semantic representation.
  • Complexity Management: In the DREAM Challenge example, the system distilled a complex R script into a high-level graph that clearly separated the "clinical" and "genetic" model branches, making the analysis easier for a human to audit or for a machine to process.
  • Open Source Contribution: The authors released their program analysis tools for Python and R, the semantic enrichment algorithm (written in Julia), and the Data Science Ontology itself as open-source projects.

Limitations and Future Work

  • Dynamic Analysis Requirement: Currently, the system must run the code to analyze it. This requires a working environment and the original data, which isn't always available.
  • Ontology Coverage: While the ontology covers many core machine learning concepts, it is still thin on "data munging" (preprocessing) operations, which make up the bulk of real-world data science code.
  • Inference Power: The current ontology is "freely generated," meaning it doesn't yet handle complex logical equations between different functions, which limits its ability to perform deeper reasoning.
  • Future Applications: The authors suggest this work could eventually lead to AI assistants that can automatically translate code into natural language summaries or perform automated meta-analyses across thousands of scientific papers.

Summary updated

BibTeX
@misc{patterson2018teaching,
  author = {Evan Patterson and Ioana Baldini and Aleksandra Mojsilovic and Kush R. Varshney},
  title = {{Teaching machines to understand data science code by semantic enrichment of dataflow graphs}},
  year = {2018},
  eprint = {1807.05691},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1807.05691},
}
Figure