Carnegie Mellon University

Tetrad in Python

We have made Tetrad Java code directly available in Python using the JPype Python tool, through the py-tetrad project.

JPype allows arbitrary Tetrad code to be incorporated into Python using Python syntax. Py-tetrad gives numerous examples of using the JPype tool to access arbitrary Tetrad code from Python.  The most up-to-date Tetrad jar is maintained in the py-tetrad GitHub repository, so py-tetrad can be updated to the latest version with a simple "git pull" call. For information about available Tetrad classes and their structure and fields, our most recent Javadocs are available online; as well as the package of search algorithms

Data and graphs are represented differently in Python than in Tetrad and must be translated between the two platforms; py-tetrad provides efficient tools for doing this. Py-tetrad assumes that data are stored in pandas data frames in Python, one feature of which (as opposed to numpy arrays) is that continuous and discrete columns may be differentially represented.  Tools are provided to translate pandas data frames from Python into DataSet objects in Tetrad and vice-versa. Graphs are also represented differently in the two platforms, and tools are available to do these translations easily. With these two additions, doing Tetrad searches in Python using JPype is made simple and efficient.

For those who do not wish to do explicit JPype programming, we also provide a Python module, TetradSearch.py, which encapsulates most of the search functionality in Tetrad. This can be used directly in Python or imported into R and used there. Any functionality that's missing and needed will be added upon request. 

The examples in the py-tetrad repository can be modified to suit the user's projects. 

While new as of April 2023, Py-tetrad has received good user feedback. Python 3.5+ is required for JPype. We have yet to make this project installable via pip, but we are working on it. 

A working paper describing py-tetrad and rpy-tetrad may be found on arXiv here: 

Ramsey, J. D., & Andrews, B. (2023). Py-Tetrad and RPy-Tetrad: A New Python Interface with R Support for Tetrad Causal Search. arXiv preprint arXiv:2308.07346.