Giter VIP home page Giter VIP logo

causaldag's Introduction

PyPI version

This package is nearing a V1 release, with potential (minor) breaking changes. After the release, future breaking changes will occur less frequently and with more notice. Please raise issues as needed.

causaldag is common wrapper for the following packages:

Installing and importing causaldag should be sufficient for most use cases.

CausalDAG is a Python package for the creation, manipulation, and learning of Causal DAGs. CausalDAG requires Python 3.5+

Install

Install the latest version of CausalDAG:

$ pip3 install causaldag

Cite

You may use the following bibtex for citing causaldag:

@manual{squires2018causaldag,
  title={{\texttt{causaldag}: creation, manipulation, and learning of causal models}},
  author={{Chandler Squires}},
  year={2018},
  url={https://github.com/uhlerlab/causaldag},
}

Or the following text:

Chandler Squires. causaldag: creation, manipulation, and learning of causal models, 2018. URL https://github.com/uhlerlab/causaldag

Documentation

Documentation for each subpackage is available at:

Examples for specific algorithms can be found at https://uhlerlab.github.io/causaldag/

Simple Example

Find the CPDAG (complete partially directed acyclic graph, AKA the essential graph) corresponding to a DAG:

>>> from causaldag import rand, partial_correlation_suffstat, partial_correlation_test, MemoizedCI_Tester, gsp
>>> import numpy as np
>>> np.random.seed(12312)
>>> nnodes = 5
>>> nodes = set(range(nnodes))
>>> dag = rand.directed_erdos(nnodes, .5)
>>> gdag = rand.rand_weights(dag)
>>> samples = gdag.sample(100)
>>> suffstat = partial_correlation_suffstat(samples)
>>> ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
>>> est_dag = gsp(nodes, ci_tester)
>>> dag.shd_skeleton(est_dag)

License

Released under the 3-Clause BSD license (see LICENSE.txt):

Copyright (C) 2018
Chandler Squires <[email protected]>

causaldag's People

Contributors

anastasiyabel avatar basilnsaeed avatar chandlersquires avatar csquires avatar kpj avatar s-karnik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

causaldag's Issues

Problem related to the "graphical_model_learning" package

Problem 1: Here is the error message that I got when I try to import the graphical_model_learning package and run its gsp function.

Traceback (most recent call last):
File "C:...\new.py", line 1, in
import graphical_model_learning
File "C:...\venv\lib\site-packages\graphical_model_learning_init_.py", line 3, in
from .samplers import *
ModuleNotFoundError: No module named 'graphical_model_learning.samplers'

How should I fix that?

Problem 2: I would like to use the non-greedy version of the sparest permutation algorithm. It was available in the previous version of causaldag (i.e., 0.1a160). But is it gone for good? Or would it be re-implemented in a later version? Thanks!

error when running DCI gene expression tutorial

Hello,
I am trying to run the tutorial for DCI for gene expression data: https://github.com/uhlerlab/causaldag/blob/master/docs/dci_tutorial.markdown

However, when I run d_dag = dci(X1, X2, alpha_ug = 0.001, alpha_skeleton=0.5, alpha_orient=0.001, max_set_size=3, verbose=1) as specified by the tutorial, I get NameError: name 'difference_ug' is not defined. Do you know what may be causing this error? I am using the same input files as the tutorial and installed DCI into my jupyter notebook using !pip install --user causaldag.

Thanks!

Best,
Elaine

Error in Simple Example

In the code provided in the readme for Simple Example, the line
iv = dag.optimal_intervention(cpdag=cpdag) seems to cause an error due to missing function optimal_intervention().

Upon further investigation, I tried replacing this function with with dag.greedy_optimal_single_node_intervention() or dag.greedy_optimal_fully_orienting_interventions(), another error arises:

Traceback (most recent call last):
icpdag = dag.interventional_cpdag([iv], cpdag=cpdag)
"causaldag/causaldag/classes/dag.py", line 1660, in interventional_cpdag
cut_edges.update({(i, j) for i, j in self._arcs if len({i, j} & set(iv_nodes)) == 1})
"causaldag/causaldag/classes/dag.py", line 1660, in
cut_edges.update({(i, j) for i, j in self._arcs if len({i, j} & set(iv_nodes)) == 1})
TypeError: unhashable type: 'list'

Error in sparsest_permutation

I tried the sample code from the causaldag documentation. It didn't work because both partial_correlation_test and partial_correlation_suffstat are not in causaldag.utils.ci_tests. So I ran the following instead.

import causaldag as cd
from causaldag.utils.ci_tests import MemoizedCI_Tester, gauss_ci
from causaldag import gauss_ci_suffstat
import random
import numpy as np
random.seed(1212)
np.random.seed(12131)
nnodes = 7
d = cd.rand.directed_erdos(nnodes, exp_nbrs=4)
g = cd.rand.rand_weights(d)
samples = g.sample(1000)
suffstat = gauss_ci_suffstat(samples)
ci_tester = MemoizedCI_Tester(gauss_ci, suffstat, alpha=1e-3)
est_dag = cd.sparsest_permutation(set(range(nnodes)), ci_tester)
print(est_dag)

Here is the error message:
Traceback (most recent call last):
File "C:...\Test.py", line 14, in
est_dag = cd.sparsest_permutation(set(range(nnodes)), ci_tester, progress=True)
File "C:...\Python\Python37\lib\site-packages\causaldag\structure_learning\dag\gsp.py", line 152, in sparsest_permutation
dag = perm2dag(list(perm), ci_tester)
File "C:...\Python\Python37\lib\site-packages\causaldag\structure_learning\dag\gsp.py", line 107, in perm2dag
is_ci = ci_tester.is_ci(pi_i, pi_j, mb)
File "C:...\Python\Python37\lib\site-packages\causaldag\utils\ci_tests\ci_tester.py", line 60, in is_ci
test_results = self.ci_test(self.suffstat, i, j, cond_set=cond_set, **self.kwargs)
TypeError: 'module' object is not callable

I see the same kind of error when I call cd.gsp instead. Please advice. Thank!

ValueError: On entry to DLASCL parameter number 4 had an illegal value

Thanks for this very promising package. I am trying to apply this tool on single cell RNA seq data, of once cell type that has been sorted out. I have treatment vs control samples. However on following this tutorial, I encounter the following error. I do not have any NAs in the data set. Please how could I fix this?

!pip install --user causaldag
from causaldag import dci, dci_stability_selection
from causaldag import get_directed_and_undirected_edges
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
from sklearn.preprocessing import StandardScaler

# Matplotlib command for Jupyter notebooks only
%matplotlib inline


path_in = '/home/power/docker_scratch'
gene_exp_diff_1 = pd.read_csv(os.path.join(path_in, 'log_norm_plus1_counts_control.csv'), index_col=0)
gene_exp_diff_2 = pd.read_csv(os.path.join(path_in, 'log_norm_plus1_counts_treated.csv'), index_col=0)
gene_names = gene_exp_diff_1.columns.values

scaler = StandardScaler(with_std=False)
X1 = scaler.fit_transform(gene_exp_diff_1.values)
X2 = scaler.fit_transform(gene_exp_diff_2.values)

d_dag = dci(X1, X2, alpha_ug = 0.001, alpha_skeleton=0.5, alpha_orient=0.001, max_set_size=3, verbose=1)

ValueError: On entry to DLASCL parameter number 4 had an illegal value

DCI code

Hello,
Is code available for the DCI module?

Thanks, Sandra

Code to Sample From Networkx Graphs

import numpy as np
from networkx import barabasi_albert_graph
from causaldag import DAG

def directed_random_graph(nnodes, random_graph_model):
edges = random_graph_model(nnodes).edges
random_permutation = np.arange(nnodes)
np.random.shuffle(random_permutation)
arcs = []
for edge in edges:
node1, node2 = edge
node1_position = np.where(random_permutation == node1)[0][0]
node2_position = np.where(random_permutation == node2)[0][0]
if node1_position < node2_position:
source = node1
endpoint = node2
else:
source = node2
endpoint = node1
arcs.append((source, endpoint))
return DAG(nodes=set(range(nnodes)), arcs=arcs)

def directed_barbasi(nnodes, nattach):
random_graph_model = lambda nnodes: barabasi_albert_graph(nnodes, nattach)
return directed_random_graph(nnodes, random_graph_model)

Looking for a causaldag module compatible with DeCAMFounder

Hello,

I would need the causaldag module to run the DeCAMFounder implementation from https://github.com/uhlerlab/decamfound.

However it seems that the causaldag module implementation has changed a lot since the development of decamfound (in particular the source code has been divided into different modules).
So I cannot use the latest causaldag module installed with the command pip3 install causaldag.

Would you know which version of the causaldag module was used to develop the decamfound source code?

Thank you very much for your help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.