Skip to content

Commit a033032

Browse files
2 parents b2f0dd9 + 6656db2 commit a033032

File tree

2 files changed

+56
-61
lines changed

2 files changed

+56
-61
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ license = {file = "LICENSE"}
2727
name = "goofi"
2828
readme = {file = "README.md", content-type = "text/markdown"}
2929
requires-python = ">=3.9"
30-
version = "2.1.16"
30+
version = "2.1.18"

src/goofi/nodes/analysis/phiid.py

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,60 @@
1-
import numpy as np
21
from copy import deepcopy
3-
from phyid.calculate import calc_PhiID
2+
3+
import numpy as np
4+
45
from goofi.data import Data, DataType
56
from goofi.node import Node
67
from goofi.params import FloatParam, StringParam
78

89
information_dynamics_metrics = {
9-
"Storage": [
10-
"rtr", "xtx", "yty", "sts"
11-
],
12-
"Copy": [
13-
"xtx", "yty"
14-
],
15-
"Transfer": [
16-
"xty", "ytx"
17-
],
18-
"Erasure": [
19-
"rtx", "rty"
20-
],
21-
"Downward causation": [
22-
"sty", "stx", "str"
23-
],
24-
"Upward causation": [
25-
"xts", "yts", "rts"
26-
]
10+
"Storage": ["rtr", "xtx", "yty", "sts"],
11+
"Copy": ["xtx", "yty"],
12+
"Transfer": ["xty", "ytx"],
13+
"Erasure": ["rtx", "rty"],
14+
"Downward causation": ["sty", "stx", "str"],
15+
"Upward causation": ["xts", "yts", "rts"],
2716
}
2817

2918
IIT_metrics = {
30-
"Information storage": [
31-
"xtx", "yty", "rtr", "sts"
32-
],
33-
"Transfer entropy": [
34-
"xty", "xtr", "str", "sty"
35-
],
36-
"Causal density": [
37-
"xtr", "ytr", "sty", "str" ,"str", "xty", "ytx", "stx"
38-
],
39-
"Integrated information": [
40-
"rts", "xts", "sts", "sty", "str", "yts", "ytx", "stx", "xty"
41-
]
19+
"Information storage": ["xtx", "yty", "rtr", "sts"],
20+
"Transfer entropy": ["xty", "xtr", "str", "sty"],
21+
"Causal density": ["xtr", "ytr", "sty", "str", "str", "xty", "ytx", "stx"],
22+
"Integrated information": ["rts", "xts", "sts", "sty", "str", "yts", "ytx", "stx", "xty"],
4223
}
4324

25+
4426
class PhiID(Node):
4527

4628
def config_input_slots():
4729
return {"matrix": DataType.ARRAY}
4830

4931
def config_output_slots():
50-
return {"PhiID": DataType.ARRAY,
51-
'inf_dyn': DataType.ARRAY,
52-
'IIT': DataType.ARRAY}
32+
return {"PhiID": DataType.ARRAY, "inf_dyn": DataType.ARRAY, "IIT": DataType.ARRAY}
5333

5434
def config_params():
5535
return {
5636
"PhiID": {
5737
"tau": FloatParam(5, 1, 100, doc="Time lag for the PhiID algorithm"),
58-
"kind": StringParam("gaussian", options=["gaussian", "discrete"],
59-
doc="Kind of data (continuous Gaussian or discrete-binarized)"),
60-
"redudancy": StringParam("MMI", options=["MMI", "CCS"],
61-
doc="Redundancy measure to use"),
38+
"kind": StringParam(
39+
"gaussian",
40+
options=["gaussian", "discrete"],
41+
doc="Kind of data (continuous Gaussian or discrete-binarized)",
42+
),
43+
"redudancy": StringParam("MMI", options=["MMI", "CCS"], doc="Redundancy measure to use"),
6244
}
6345
}
6446

47+
def setup(self):
48+
try:
49+
from phyid.calculate import calc_PhiID
50+
except ImportError:
51+
raise ImportError(
52+
"The phyid package is not installed. Please install it with the following command:\n"
53+
"pip install git+https://github.com/Imperial-MIND-lab/integrated-info-decomp.git"
54+
)
55+
56+
self.calc_PhiID = calc_PhiID
57+
6558
def process(self, matrix: Data):
6659
# If no input, do nothing
6760
if matrix is None or matrix.data is None:
@@ -78,10 +71,24 @@ def process(self, matrix: Data):
7871
redundancy = self.params["PhiID"]["redudancy"].value
7972

8073
# List of atom names in fixed order
81-
atom_names = ['rtr', 'rtx', 'rty', 'rts',
82-
'xtr', 'xtx', 'xty', 'xts',
83-
'ytr', 'ytx', 'yty', 'yts',
84-
'str', 'stx', 'sty', 'sts']
74+
atom_names = [
75+
"rtr",
76+
"rtx",
77+
"rty",
78+
"rts",
79+
"xtr",
80+
"xtx",
81+
"xty",
82+
"xts",
83+
"ytr",
84+
"ytx",
85+
"yty",
86+
"yts",
87+
"str",
88+
"stx",
89+
"sty",
90+
"sts",
91+
]
8592
n_atoms = len(atom_names)
8693

8794
# Prepare output array: one row per channel, one col per atom
@@ -100,7 +107,7 @@ def process(self, matrix: Data):
100107
# TODO
101108

102109
# Run the PhiID calculation
103-
atoms_res, _ = calc_PhiID(src, trg, tau, kind=kind, redundancy=redundancy)
110+
atoms_res, _ = self.calc_PhiID(src, trg, tau, kind=kind, redundancy=redundancy)
104111
# add 'str', 'stx', 'sty', 'sts' together
105112

106113
# Each atoms_res[name] is a vector length n_time - tau
@@ -132,22 +139,10 @@ def process(self, matrix: Data):
132139
channel_labels = matrix.meta["channels"]["dim0"]
133140
else:
134141
channel_labels = [f"ch{i}" for i in range(n_channels)]
135-
out_meta["channels"] = {
136-
"dim0": channel_labels,
137-
"dim1": atom_names
138-
}
142+
out_meta["channels"] = {"dim0": channel_labels, "dim1": atom_names}
139143
out_phi = {}
140-
out_phi['channels'] = {
141-
"dim0": channel_labels,
142-
"dim1": list(information_dynamics_metrics.keys())
143-
}
144+
out_phi["channels"] = {"dim0": channel_labels, "dim1": list(information_dynamics_metrics.keys())}
144145
out_IIT = {}
145-
out_IIT['channels'] = {
146-
"dim0": channel_labels,
147-
"dim1": list(IIT_metrics.keys())
148-
}
149-
150-
return {"PhiID": (PhiID_vals, out_meta),
151-
"inf_dyn": (inf_dyn_vals, out_phi),
152-
"IIT": (IIT_vals, out_IIT)}
146+
out_IIT["channels"] = {"dim0": channel_labels, "dim1": list(IIT_metrics.keys())}
153147

148+
return {"PhiID": (PhiID_vals, out_meta), "inf_dyn": (inf_dyn_vals, out_phi), "IIT": (IIT_vals, out_IIT)}

0 commit comments

Comments
 (0)