julien-klaus / leapp

GNU Lesser General Public License v2.1
0 stars 0 forks source link

Learn Probabilistic Programs (leapp)

This is a prototype compiling Bayesian networks into probabilistic program languages. For learning the Bayesian networks we use bnlearn. Currently both PyMC3 and Blog code is generated. Unfortunately the bnlearn package for Python is not well designed, so a roundabout via R is used.

Install and Requirements

Create a new conda environment with

conda env create -f environment.yml

and activate it with

conda activate leapp

delete it with

conda env remove -n leapp

Please install the package using pip install .

We need the following requirements for Python (3.8):

Further we need an R (3.6.1) installation with the following packages:

Usage

Example

Try the run the example.py file. This creates a simple PyMC3 code fragment for the cars data set.

Terminal

python leapp.py csv_file

Python

After installing the package one can learn the probabilistic programm.

from leapp import LearnPP

lp = LearnPP()
lp.fit(data)

The LearnPP object allows you to get PyMC3 and Blog code

print(lp.get_pymc_code())

print(lp.get_blog_code())

Troubleshooting

1. Error during learning

Error in [[<-.data.frame(*tmp*, var, value = numeric(0)) : replacement has 0 rows ...

The data object has an error. Maybe there is an index column or there are problematic column names.

Solution Check the data frame or look into the R code and print there the data variable to see if there are some missmatches.

2. PyMC contains NaN

Solution We encounter this with the loglik score. May another score can fix this.

3. OSError with R

OSError: cannot load library 'C:\Program Files\R\R-3.6.1\bin\x64\R.dll'

Solution Please set the path variables for your R.dll. Especially R_HOME and PATH or try adding to your code.

import os
os.environ["R_HOME"] = r"C:\Program Files\R\R-3.6.1"
os.environ["PATH"]   = r"C:\Program Files\R\R-3.6.1\bin\x64" + ";" + os.environ["PATH"]

4. 'NoneType' object has no attribute 'get_nodes'

There is something wrong with you R installation. Probably some R packages are not installed. Please copy the generated R-code and try it in an R environment. It should give you a hint.