Abstract
We give novel Python and R interfaces for the (Java) Tetrad project for causal modeling, search, and estimation. The Tetrad project is a mainstay in the literature, having been under consistent development for over 30 years. Some of its algorithms are now classics, like PC and FCI; others are recent developments. It is increasingly the case, however, that researchers need to access the underlying Java code from Python or R. Existing methods for doing this are inadequate. We provide new, up-to-date methods using the JPype Python-Java interface and the Reticulate Python-R interface, directly solving these issues. With the addition of some simple tools and the provision of working examples for both Python and R, using JPype and Reticulate to interface Python and R with Tetrad is straightforward and intuitive.
Introduction
Causal discovery—the process of inferring cause-and-effect relationships from observational data—has become a cornerstone of modern scientific inquiry. For over three decades, the Tetrad project has served as a premier software suite for this task, housing a vast library of algorithms ranging from the foundational PC and FCI to contemporary advancements. However, Tetrad’s implementation in Java has created a significant barrier for the modern data science community, which primarily operates within Python and R ecosystems.
Previous attempts to bridge this gap, such as the py-causal and r-causal libraries, have struggled to maintain pace with evolving operating systems and language versions. These legacy tools often relied on brittle technologies like javabridge or rjava, which are notoriously difficult to configure on modern systems (particularly macOS) and typically only exposed a fraction of Tetrad’s capabilities. To address these limitations, we introduce Py-Tetrad and RPy-Tetrad, providing a robust, modern interface to the full Tetrad engine.
The Challenge of Cross-Language Integration
The primary obstacle in causal discovery software is fragmentation. While Tetrad contains decades of refined Java code, the researchers who need these tools are often working in environments optimized for machine learning (Python) or statistical analysis (R). When existing wrappers fall into disrepair, researchers are forced to choose between using outdated algorithms or undertaking the steep learning curve of Java.
Furthermore, causal search is rarely a "black box" process. It requires the integration of background knowledge, the handling of mixed data types (continuous and discrete), and the ability to perform stability analyses like bootstrapping. An effective interface must not only call functions but also facilitate the seamless transfer of complex data structures—such as Pandas DataFrames, R data frames, and NetworkX graphs—between the host language and the Java Virtual Machine (JVM).
Architecture: Py-Tetrad and RPy-Tetrad
Our solution utilizes two modern bridging technologies to create a stable and comprehensive interface:
1. Py-Tetrad (Python-to-Java)
Py-Tetrad leverages JPype, a library that allows Python to call Java code using native Python syntax. Unlike previous wrappers that required specific API exports, JPype provides access to the entire Tetrad codebase. We have supplemented this with a high-level TetradSearch class to simplify common workflows and specialized "translators" that convert data and graph objects between Python and Java formats.
2. RPy-Tetrad (R-to-Python-to-Java)
To ensure stability in the R environment, we adopted a "middleware" approach. Instead of a direct R-to-Java bridge, RPy-Tetrad uses the Reticulate package to route R commands through the Python interface. This architecture significantly reduces the number of moving parts and ensures that any improvements or updates made to the Python library are immediately available to R users, bypassing the long-standing installation issues associated with rjava.
Practical Applications and Key Features
The utility of these interfaces is demonstrated through their ability to handle complex, real-world scenarios:
- Mixed-Data Handling: Using Apple Watch and Fitbit data, we demonstrate the
SP-FCIalgorithm’s ability to process datasets containing both discrete (e.g., device type) and continuous (e.g., heart rate) variables. This is a critical requirement for modern health and behavioral data. - Background Knowledge and Temporal Tiers: Causal search accuracy is significantly improved when researchers can provide constraints. Using NASA Airfoil Self-Noise data, we show how users can implement "temporal tiers"—for example, ensuring that an effect cannot precede its cause—directly within the R interface.
- Bootstrapping and Stability: The interfaces simplify the execution of bootstrapped studies. By running a search multiple times on different data samples, researchers can calculate the stability and confidence of specific causal edges, moving beyond single-point estimates to more reliable causal inferences.
Results and Compatibility
Py-Tetrad and RPy-Tetrad represent a significant leap in compatibility, supporting Tetrad version 7.5.0+, Python 3.5+, and R 4+. By exposing the full Java library, these tools ensure that when a new algorithm is added to the Tetrad core, it is instantly accessible to Python and R users without requiring a manual update to the wrapper code.
Conclusion and Future Work
While these new interfaces solve the primary technical hurdles of integration, some challenges remain. The initial setup of the Java environment and bridging libraries can be cumbersome for novice users. Future efforts will focus on streamlining installation via standard package managers like pip and CRAN. Additionally, we aim to improve native visualization support to reduce reliance on external plotting libraries.
By bridging the gap between Java’s computational power and the flexibility of Python and R, Py-Tetrad and RPy-Tetrad provide the causal discovery community with a modern, extensible, and stable platform for research and application.
Summary updated