Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #197

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run(self):
# Run the setup
setup(
name="tigramite",
version="5.0.1.4",
version="5.0.1.5",
packages=["tigramite", "tigramite.independence_tests", "tigramite.toymodels"],
license="GNU General Public License v3.0",
description="Tigramite causal discovery for time series",
Expand Down
9 changes: 8 additions & 1 deletion tigramite/causal_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ def predict_total_effect(self,
intervention_data,
conditions_data=None,
pred_params=None,
return_further_pred_results=False,
):
"""Predict effect of intervention with fitted model.

Expand All @@ -1895,6 +1896,9 @@ def predict_total_effect(self,
Numpy array of shape (time, len(S)) that contains the S=s values.
pred_params : dict, optional
Optional parameters passed on to sklearn prediction function.
return_further_pred_results : bool, optional (default: False)
In case the predictor class returns more than just the expected value,
the entire results can be returned.

Returns
-------
Expand All @@ -1918,7 +1922,8 @@ def predict_total_effect(self,
effect = self.model.get_general_prediction(
intervention_data=intervention_data,
conditions_data=conditions_data,
pred_params=pred_params)
pred_params=pred_params,
return_further_pred_results=return_further_pred_results)

return effect

Expand Down Expand Up @@ -2074,6 +2079,8 @@ def fit_wright_effect(self,

effect[(x, y)] += effect_here

# Make fitted coefficients available as attribute
self.coeffs = coeffs

# Modify and overwrite variables in self.model
self.model.Y = self.listY
Expand Down
4 changes: 2 additions & 2 deletions tigramite/independence_tests/gpdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# License: GNU General Public License v3.0

from __future__ import print_function
import json, warnings
import json, warnings, os, pathlib
import numpy as np
try:
from importlib import metadata
Expand All @@ -14,7 +14,7 @@
try:
import dcor
from sklearn import gaussian_process
with open('../versions.py', 'r') as vfile:
with open(pathlib.Path(os.path.dirname(__file__)) / '../../versions.py', 'r') as vfile:
packages = json.loads(vfile.read())['all']
packages = dict(map(lambda s: s.split('>='), packages))
if metadata.version('dcor') < packages['dcor']:
Expand Down
4 changes: 2 additions & 2 deletions tigramite/independence_tests/gpdc_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# License: GNU General Public License v3.0

from __future__ import print_function
import json, warnings
import json, warnings, os, pathlib
import numpy as np
import gc
try:
Expand All @@ -17,7 +17,7 @@
import torch
import gpytorch
from .LBFGS import FullBatchLBFGS
with open('../versions.py', 'r') as vfile:
with open(pathlib.Path(os.path.dirname(__file__)) / '../../versions.py', 'r') as vfile:
packages = json.loads(vfile.read())['all']
packages = dict(map(lambda s: s.split('>='), packages))
if metadata.version('dcor') < packages['dcor']:
Expand Down
30 changes: 23 additions & 7 deletions tigramite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import print_function
from copy import deepcopy
import json, warnings
import json, warnings, os, pathlib
import numpy as np
try:
from importlib import metadata
Expand All @@ -16,7 +16,7 @@
import sklearn
import sklearn.linear_model
import networkx
with open('../versions.py', 'r') as vfile:
with open(pathlib.Path(os.path.dirname(__file__)) / '../versions.py', 'r') as vfile:
packages = json.loads(vfile.read())['all']
packages = dict(map(lambda s: s.split('>='), packages))
if metadata.version('scikit-learn') < packages['scikit-learn']:
Expand Down Expand Up @@ -207,6 +207,7 @@ def get_general_prediction(self,
intervention_data,
conditions_data=None,
pred_params=None,
return_further_pred_results=False,
):
r"""Predict effect of intervention with fitted model.

Expand All @@ -220,7 +221,9 @@ def get_general_prediction(self,
Numpy array of shape (time, len(S)) that contains the S=s values.
pred_params : dict, optional
Optional parameters passed on to sklearn prediction function.

return_further_pred_results : bool, optional (default: False)
In case the predictor class returns more than just the expected value,
the entire results can be returned.
Returns
-------
Results from prediction.
Expand All @@ -244,6 +247,7 @@ def get_general_prediction(self,
predicted_array = np.zeros((intervention_T, lenY))
pred_dict = {}
for iy, y in enumerate(self.Y):
pred_dict[iy] = {}
# Print message
if self.verbosity > 1:
print("\n## Predicting target %s" % str(y))
Expand Down Expand Up @@ -292,16 +296,28 @@ def get_general_prediction(self,
# predicted_vals = a_transform.transform(X=target_array.T).T
a_conditional_model = deepcopy(self.conditional_model)

a_conditional_model.fit(X=s_array, y=predicted_vals)
if type(predicted_vals) is tuple:
predicted_vals_here = predicted_vals[0]
else:
predicted_vals_here = predicted_vals

a_conditional_model.fit(X=s_array, y=predicted_vals_here)
self.fit_results[y]['conditional_model'] = a_conditional_model

predicted_array[index, iy] = a_conditional_model.predict(
X=conditions_array, **pred_params).mean()
predicted_vals = a_conditional_model.predict(
X=conditions_array, **pred_params)

# print(predicted_vals)
if type(predicted_vals) is tuple:
predicted_array[index, iy] = predicted_vals[0].mean()
pred_dict[iy][index] = predicted_vals
else:
predicted_array[index, iy] = predicted_vals.mean()

return predicted_array
if return_further_pred_results:
return predicted_array, pred_dict
else:
return predicted_array


def get_fit(self, all_parents,
Expand Down
4 changes: 2 additions & 2 deletions tigramite/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
# License: GNU General Public License v3.0

import numpy as np
import json, warnings
import json, warnings, os, pathlib
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # python<=3.7
try:
import matplotlib
import networkx as nx
with open('../versions.py', 'r') as vfile:
with open(pathlib.Path(os.path.dirname(__file__)) / '../versions.py', 'r') as vfile:
packages = json.loads(vfile.read())['all']
packages = dict(map(lambda s: s.split('>='), packages))
if metadata.version('matplotlib') < packages['matplotlib']:
Expand Down