Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cisc/dr/dr.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (27 sloc)
858 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Python wrapper for the matlab implementation of Causal Inference on Discrete Data using Additive Noise Models. | |
""" | |
import os | |
import io | |
import sys | |
import matlab.engine | |
print("loading matlab engine ... "), | |
sys.stdout.flush() | |
cur_dir = os.path.dirname(__file__) | |
mengine = matlab.engine.start_matlab() | |
mengine.addpath(cur_dir) | |
out = io.StringIO() | |
err = io.StringIO() | |
print("\b[✓]") | |
sys.stdout.flush() | |
def dr(X, Y, level): | |
# remove stdout arg for full debug in matlab | |
fct_fw, p_fw, fct_bw, p_bw = mengine.fit_both_dir_discrete( | |
X, 0, Y, 0, level, 0, nargout=4, stdout=out, stderr=err) | |
return p_fw, p_bw | |
if __name__ == "__main__": | |
import random | |
X = [random.randint(1, 4) for i in range(1000)] | |
Y = [X[i] + random.randint(1, 3) for i in range(1000)] | |
print(dr(X, Y, 0.05)) |