Giter Site home page Giter Site logo

Causal influences and Pandas 1.5 about msa HOT 2 CLOSED

kuffmode avatar kuffmode commented on June 20, 2024
Causal influences and Pandas 1.5

from msa.

Comments (2)

ShreyDixit avatar ShreyDixit commented on June 20, 2024

Thanks for pointing it out. Can you provide a MCVE so that I can get started.

from msa.

kuffmode avatar kuffmode commented on June 20, 2024
import numpy as np
from msapy import msa
import matplotlib.pyplot as plt
import networkx as nx
from copy import deepcopy
import seaborn as sns

We need to simulate a system so these functions:

def event_maker(
    n_units: int,
    timesteps: int,
    probability: float = 1,
    rng: np.random.Generator = np.random.default_rng(seed=2023),
) -> np.ndarray:

    if probability < 1:
        input = rng.choice(
            [0, 1], p=[1 - probability, probability], size=(n_units, timesteps)
        )
        input = input.astype(float)
    else:
        input = np.zeros((n_units, timesteps))
        for node in range(n_units):
            event_timepoints = rng.integers(0, timesteps, 1)
            input[node, event_timepoints] += 1

    return input

def identity(x):
    return x

def simulate_dynamical_system_parallel(adjacency_matrix:np.ndarray,
                       input_matrix:np.ndarray, 
                       coupling:float=1, 
                       dt:float=0.001, 
                       duration:int=10, 
                       timeconstant:float=0.01,
                       function = identity, 
                       )->np.ndarray:
    
    N = input_matrix.shape[0]
    T = np.arange(1, duration/dt + 1) # holds time steps
    X = np.zeros((N, len(T)+1)) # holds variable x

    dt = dt/timeconstant
    for timepoint in range(len(T)):
        for node in range(N):
            if timepoint == 0:
                X[node, timepoint] = input_matrix[node, timepoint]
            else:
                inputs = np.dot(coupling * adjacency_matrix[node, :], X[:, timepoint-1]) + input_matrix[node, timepoint]
                X[node, timepoint] = (1 - dt) * X[node, timepoint-1] + dt * function(inputs)

    return X

def lesion_simple_nodes(complements,
                        adjacency_matrix: np.ndarray,
                        index: int,
                        input: np.ndarray,
                        model = simulate_dynamical_system_parallel,
                        model_kwargs:dict={},
                        ) -> np.ndarray:

    lesioned_connectivity = deepcopy(adjacency_matrix)
    for target in complements:
        lesioned_connectivity[:, target] = 0.0
        lesioned_connectivity[target, :] = 0.0

    dynamics = model(adjacency_matrix = lesioned_connectivity,
                        input_matrix=input,
                        **model_kwargs)
    lesioned_signal = dynamics[index]
    return lesioned_signal

And then these parameters:

rng = np.random.default_rng(seed=2023)
chain = nx.to_numpy_array(nx.generators.path_graph(6,nx.DiGraph())).T
chain *= 0.9
timesteps = 100
example_input = event_maker(6,timesteps)

NOISE_STRENGTH = 0.05
DELTA = 0.01
TAU = 0.02
G = 0.74
DURATION = 1
model_params = {'dt':DELTA,'timeconstant':TAU,'coupling':G,'duration':DURATION}

After running the game you see the zeroth column went after 5 in pandas 1.5 and 2 but not 1.3.5.

lesion_params = {'adjacency_matrix': chain, 'input': example_input, 'model_kwargs': model_params}
ci_chain = msa.estimate_causal_influences(elements=list(range(len(chain))),
                                        objective_function=lesion_simple_nodes,
                                        objective_function_params=lesion_params,
                                        n_permutations=128, n_cores=1)

from msa.

Related Issues (6)

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.