Module 12 in course: adding code to show how to conduct clustering on an FC adjacency matrix.

This commit is contained in:
Joseph Lizier 2025-11-05 15:14:00 +11:00
parent 880b0132c5
commit aba2f5f258
2 changed files with 40 additions and 4 deletions

View File

@ -90,7 +90,25 @@
"7. Look at the results for the adjacency matrix for the MI functional network in figure 1 (left):\n",
" * What do you observe in terms of the functional network structure? Which sources have the highest MI to each target?\n",
" * How does it compare to what you know the underlying causal structure to be? Is that what you expected to see?\n",
" * Can you interpret the features of the network via the dynamics that you can see in figure 2 (right)? What generates the modular block structure?"
" * Can you interpret the features of the network via the dynamics that you can see in figure 2 (right)? What generates the modular block structure?\n",
"\n",
"**A note** for when you are constructing functional connectivity for other data sets which don't have an obvious ordinal layout like the CA data does: you might consider running a _clustering_ method over the FC adjacency matrix in order to group variables with similar relationships to others. This will give you visual insights into the modularity in the FC. To do this, you would use code like the following:\n",
"```python\n",
"fc = results\n",
"# Import relevant libraries\n",
"from scipy.cluster.hierarchy import linkage, optimal_leaf_ordering, leaves_list\n",
"from scipy.spatial.distance import pdist\n",
"# Perform hierarchical clustering using average linkage\n",
"distances = numpy.max(fc) - fc\n",
"Z = linkage(distances, method='average')\n",
"# Compute the optimal leaf ordering\n",
"optimal_Z = optimal_leaf_ordering(Z, distances)\n",
"# Get the order of the leaves\n",
"optimal_order = leaves_list(optimal_Z)\n",
"# ...\n",
"# And utilise this clustering to reorder the variables in your imshow plot call with:\n",
"plt.imshow(fc[optimal_order,:][:,optimal_order],cmap=cm.coolwarm, norm=mplcolors.CenteredNorm())\n",
"```"
]
},
{
@ -104,7 +122,7 @@
"\n",
"1. Start by copying the above two code cells for the functional network with MI (and plots), and pasting it into the code cell below that we will edit for TE.\n",
"2. Open the TE AutoAnalyser - we're just going to generate some dummy code to grab a few lines from. Select a Discrete estimator, select your CA data file, tick the checkbox for `All pairs?`, set the `k_HISTORY` at 4 (we won't have enough data here for it to be larger, since we're assuming _heterogeneous_ variables here), and -- most importantly -- untick the checkbox for `Compute result?` (we don't want to run this computation, it will take a lot of time!), and click `Generate Code`.\n",
"3. Copy the lines constructing the TE calculator, and paste it into your Matlab script, replacing where the MI calculator was previously constructed.\n",
"3. Copy the lines constructing the TE calculator, and paste it into your code cell below, replacing where the MI calculator was previously constructed.\n",
"4. Down the bottom of the code where the adjacency matrix is plotted, in the `title()` and `set_label()` functions replace \"MI\" with \"TE\"."
]
},

View File

@ -141,7 +141,25 @@
"7. Look at the results for the adjacency matrix for the MI functional network in figure 1 (left):\n",
" * What do you observe in terms of the functional network structure? Which sources have the highest MI to each target?\n",
" * How does it compare to what you know the underlying causal structure to be? Is that what you expected to see?\n",
" * Can you interpret the features of the network via the dynamics that you can see in figure 2 (right)? What generates the modular block structure?"
" * Can you interpret the features of the network via the dynamics that you can see in figure 2 (right)? What generates the modular block structure?\n",
"\n",
"**A note** for when you are constructing functional connectivity for other data sets which don't have an obvious ordinal layout like the CA data does: you might consider running a _clustering_ method over the FC adjacency matrix in order to group variables with similar relationships to others. This will give you visual insights into the modularity in the FC. To do this, you would use code like the following:\n",
"```python\n",
"fc = results\n",
"# Import relevant libraries\n",
"from scipy.cluster.hierarchy import linkage, optimal_leaf_ordering, leaves_list\n",
"from scipy.spatial.distance import pdist\n",
"# Perform hierarchical clustering using average linkage\n",
"distances = numpy.max(fc) - fc\n",
"Z = linkage(distances, method='average')\n",
"# Compute the optimal leaf ordering\n",
"optimal_Z = optimal_leaf_ordering(Z, distances)\n",
"# Get the order of the leaves\n",
"optimal_order = leaves_list(optimal_Z)\n",
"# ...\n",
"# And utilise this clustering to reorder the variables in your imshow plot call with:\n",
"plt.imshow(fc[optimal_order,:][:,optimal_order],cmap=cm.coolwarm, norm=mplcolors.CenteredNorm())\n",
"```"
]
},
{
@ -155,7 +173,7 @@
"\n",
"1. Start by copying the above two code cells for the functional network with MI (and plots), and pasting it into the code cell below that we will edit for TE.\n",
"2. Open the TE AutoAnalyser - we're just going to generate some dummy code to grab a few lines from. Select a Discrete estimator, select your CA data file, tick the checkbox for `All pairs?`, set the `k_HISTORY` at 4 (we won't have enough data here for it to be larger, since we're assuming _heterogeneous_ variables here), and -- most importantly -- untick the checkbox for `Compute result?` (we don't want to run this computation, it will take a lot of time!), and click `Generate Code`.\n",
"3. Copy the lines constructing the TE calculator, and paste it into your Matlab script, replacing where the MI calculator was previously constructed.\n",
"3. Copy the lines constructing the TE calculator, and paste it into your code cell below, replacing where the MI calculator was previously constructed.\n",
"4. Down the bottom of the code where the adjacency matrix is plotted, in the `title()` and `set_label()` functions replace \"MI\" with \"TE\"."
]
},