1
- import numpy as np
2
1
from copy import deepcopy
3
- from phyid .calculate import calc_PhiID
2
+
3
+ import numpy as np
4
+
4
5
from goofi .data import Data , DataType
5
6
from goofi .node import Node
6
7
from goofi .params import FloatParam , StringParam
7
8
8
9
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" ],
27
16
}
28
17
29
18
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" ],
42
23
}
43
24
25
+
44
26
class PhiID (Node ):
45
27
46
28
def config_input_slots ():
47
29
return {"matrix" : DataType .ARRAY }
48
30
49
31
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 }
53
33
54
34
def config_params ():
55
35
return {
56
36
"PhiID" : {
57
37
"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" ),
62
44
}
63
45
}
64
46
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
+
65
58
def process (self , matrix : Data ):
66
59
# If no input, do nothing
67
60
if matrix is None or matrix .data is None :
@@ -78,10 +71,24 @@ def process(self, matrix: Data):
78
71
redundancy = self .params ["PhiID" ]["redudancy" ].value
79
72
80
73
# 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
+ ]
85
92
n_atoms = len (atom_names )
86
93
87
94
# Prepare output array: one row per channel, one col per atom
@@ -100,7 +107,7 @@ def process(self, matrix: Data):
100
107
# TODO
101
108
102
109
# 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 )
104
111
# add 'str', 'stx', 'sty', 'sts' together
105
112
106
113
# Each atoms_res[name] is a vector length n_time - tau
@@ -132,22 +139,10 @@ def process(self, matrix: Data):
132
139
channel_labels = matrix .meta ["channels" ]["dim0" ]
133
140
else :
134
141
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 }
139
143
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 ())}
144
145
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 ())}
153
147
148
+ return {"PhiID" : (PhiID_vals , out_meta ), "inf_dyn" : (inf_dyn_vals , out_phi ), "IIT" : (IIT_vals , out_IIT )}
0 commit comments