Skip to content

Commit

Permalink
adding data
Browse files Browse the repository at this point in the history
  • Loading branch information
manishsaggar1 committed Mar 8, 2024
1 parent 987028b commit f5cb6f0
Show file tree
Hide file tree
Showing 9 changed files with 2,945 additions and 330 deletions.
260 changes: 260 additions & 0 deletions examples/haxby_decoding/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "55dcd7ea",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/saggar/opt/anaconda3/lib/python3.9/site-packages/nilearn/input_data/__init__.py:23: FutureWarning: The import path 'nilearn.input_data' is deprecated in version 0.9. Importing from 'nilearn.input_data' will be possible at least until release 0.13.0. Please import from 'nilearn.maskers' instead.\n",
" warnings.warn(message, FutureWarning)\n",
"/Users/saggar/opt/anaconda3/lib/python3.9/site-packages/nilearn/image/resampling.py:493: UserWarning: The provided image has no sform in its header. Please check the provided file. Results may not be as expected.\n",
" warnings.warn(\n",
"/Users/saggar/opt/anaconda3/lib/python3.9/site-packages/joblib/memory.py:349: FutureWarning: The default strategy for standardize is currently 'zscore' which incorrectly uses population std to calculate sample zscores. The new strategy 'zscore_sample' corrects this behavior by using the sample std. In release 0.13, the default strategy will be replaced by the new strategy and the 'zscore' option will be removed. Please use 'zscore_sample' instead.\n",
" return self.func(*args, **kwargs)\n"
]
}
],
"source": [
"import numpy as np \n",
"import pandas as pd\n",
"from nilearn.datasets import fetch_haxby\n",
"from nilearn.input_data import NiftiMasker\n",
"\n",
"# Fetch dataset, extract time-series from ventral temporal (VT) mask\n",
"dataset = fetch_haxby(subjects=[2])\n",
"masker = NiftiMasker(\n",
" dataset.mask_vt[0], \n",
" standardize=True, detrend=True, smoothing_fwhm=4.0,\n",
" low_pass=0.09, high_pass=0.008, t_r=2.5,\n",
" memory=\"nilearn_cache\")\n",
"X = masker.fit_transform(dataset.func[0])\n",
"\n",
"# Encode labels as integers\n",
"df = pd.read_csv(dataset.session_target[0], sep=\" \")\n",
"target, labels = pd.factorize(df.labels.values)\n",
"timing = pd.DataFrame().assign(task=target, task_name=labels[target])\n",
"timing_onehot = pd.DataFrame({l:1*(target==i) for i,l in enumerate(labels)})\n",
"\n",
"# Save X and y\n",
"np.save('SBJ02_mask_vt.npy', X)\n",
"timing.to_csv('SBJ02_timing_labels.tsv', sep='\\t', index=0)\n",
"timing_onehot.to_csv('SBJ02_timing_onehot.tsv', sep='\\t', index=0)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "89ddf47e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 0.0\n",
"2 0.0\n",
"3 0.0\n",
"4 0.0\n",
"5 0.0\n",
"6 0.0\n",
"7 0.0\n",
"8 0.0\n",
"9 0.0\n",
"Name: 0, dtype: float64"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"timing_onehot = pd.read_csv('timing_onehot.tsv',header=None,delim_whitespace=True) \n",
"timing_onehot[0][1:10]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "975e31be",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>label</th>\n",
" <th>group</th>\n",
" <th>value</th>\n",
" <th>row_count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>rest</td>\n",
" <td>0</td>\n",
" <td>588</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>scissors</td>\n",
" <td>1</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>face</td>\n",
" <td>2</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>cat</td>\n",
" <td>3</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>shoe</td>\n",
" <td>4</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>house</td>\n",
" <td>5</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>scrambledpix</td>\n",
" <td>6</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>bottle</td>\n",
" <td>7</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>chair</td>\n",
" <td>8</td>\n",
" <td>108</td>\n",
" <td>1452</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Already serving localhost:None \n",
"[Force Graph] file:///Users/saggar/Dropbox/git_bdl/neumapper/examples/haxby_decoding/haxby_decoding_neumapper_dyneusr.html\n"
]
},
{
"data": {
"text/html": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DyNeuGraph()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">DyNeuGraph</label><div class=\"sk-toggleable__content\"><pre>DyNeuGraph()</pre></div></div></div></div></div>"
],
"text/plain": [
"DyNeuGraph()"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np \n",
"import pandas as pd\n",
"import networkx as nx\n",
"import scipy as sp\n",
"from sklearn.utils import Bunch\n",
"from scipy.io import loadmat\n",
"from dyneusr.core import DyNeuGraph \n",
"from collections import defaultdict\n",
"\n",
"\n",
"## Load the NeuMapper result\n",
"mat = loadmat('haxby_decoding_neumapper.mat')\n",
"res = mat['res'][0][0]\n",
"res = Bunch(**{k:res[i] for i,k in enumerate(res.dtype.names)})\n",
"res = res.get('res', res.get('var', res))\n",
"\n",
"# load one-hot encoding matrix of timing labels \n",
"timing_onehot = pd.read_csv('SBJ02_timing_onehot.tsv', sep='\\t') \n",
"\n",
"\n",
"## Convert to KeplerMapper format\n",
"membership = res.clusterBins\n",
"adjacency = membership @ membership.T\n",
"np.fill_diagonal(adjacency, 0)\n",
"adjacency = (adjacency > 0).astype(int)\n",
"\n",
"# get node link data \n",
"G = nx.Graph(adjacency)\n",
"graph = nx.node_link_data(G)\n",
"\n",
"# update format of nodes e.g. {node: [row_i, ...]}\n",
"nodes = defaultdict(list) \n",
"for n, node in enumerate(membership):\n",
" nodes[n] = node.nonzero()[0].tolist()\n",
"\n",
"# update format of links e.g. {source: [target, ...]}\n",
"links = defaultdict(list) \n",
"for link in graph['links']:\n",
" u, v = link['source'], link['target']\n",
" if u != v:\n",
" links[u].append(v)\n",
"\n",
"# update graph data\n",
"graph['nodes'] = nodes\n",
"graph['links'] = links\n",
"\n",
"\n",
"## Visualize the shape graph using DyNeuSR's DyNeuGraph\n",
"dG = DyNeuGraph(G=graph, y=timing_onehot)\n",
"dG.visualize('haxby_decoding_neumapper_dyneusr.html')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/haxby_decoding/haxby_decoding_neumapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@

%% Save outputs
save('haxby_decoding_neumapper.mat','res');
saveas(gcf, 'haxby_decoding_neumapper.png');
saveas(gcf, 'haxby_decoding_neumapper.png');
Binary file modified examples/haxby_decoding/haxby_decoding_neumapper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
478 changes: 149 additions & 329 deletions examples/haxby_decoding/haxby_decoding_neumapper_dyneusr.html

Large diffs are not rendered by default.

Loading

0 comments on commit f5cb6f0

Please sign in to comment.