File size: 5,570 Bytes
e11c92f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9dbe513",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%matplotlib widget\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.cm as cm\n",
    "import mat73\n",
    "import pickle\n",
    "\n",
    "import MARBLE\n",
    "from MARBLE import plotting\n",
    "\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "import neo\n",
    "from elephant.statistics import instantaneous_rate\n",
    "from elephant.kernels import GaussianKernel\n",
    "from quantities import ms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fd6f954",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data compiled into matlab cell array\n",
    "!wget -nc https://dataverse.harvard.edu/api/access/datafile/6963157 -O data/conditions_spiking_data.mat\n",
    "spiking_data = mat73.loadmat('data/conditions_spiking_data.mat')['all_results']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c578b742",
   "metadata": {},
   "outputs": [],
   "source": [
    "trial = 1\n",
    "session = 9\n",
    "\n",
    "spikes, colors = [], []\n",
    "for cond in range(7):\n",
    "    st = spiking_data[session][cond][trial][0][:,:]\n",
    "    spikes += [np.where(st[ch,:])[0] for ch in range(24)]\n",
    "    colors += [cm.viridis(cond / 6) for _ in range(24)]\n",
    "\n",
    "_, ax = plt.subplots(figsize=(5,4))\n",
    "ax.eventplot(spikes, color=colors)\n",
    "\n",
    "_, ax = plt.subplots(figsize=(5,4))\n",
    "gk = GaussianKernel(100 * ms) # increase this for smoother signals (previously used auto)\n",
    "\n",
    "for sp in spikes[:24]:\n",
    "    st = neo.SpikeTrain(sp, units='ms', t_stop=1200)\n",
    "                        \n",
    "    inst_rate = instantaneous_rate(st, kernel=gk, sampling_period=1 * ms).magnitude\n",
    "    ax.plot(inst_rate, 'C0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4424eb17",
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget -nc https://dataverse.harvard.edu/api/access/datafile/7062086 -O data/raw_data_session_9_3D.pkl\n",
    "pos, vel, time, _ = pickle.load(open('data/raw_data_session_9_3D.pkl','rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d8a958a",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10,5))\n",
    "n_traj=10\n",
    "from matplotlib.colors import LinearSegmentedColormap\n",
    "from mpl_toolkits.mplot3d.art3d import Line3DCollection\n",
    "\n",
    "for i,cond in enumerate([1,4,6]):\n",
    "    ax = fig.add_subplot(int('13{}'.format(i+1)), projection='3d')\n",
    "    ax.view_init(elev=10., azim=90)\n",
    "    starts = np.where(time[cond]==0)[0]\n",
    "    for j in range(n_traj):\n",
    "        t = range(starts[j], starts[j+1]-1)\n",
    "        p = pos[cond][t]\n",
    "        segments = np.stack([p[:-1], p[1:]], axis=1)\n",
    "        \n",
    "        colors = [(0, 0, 0), cm.viridis(cond/6)] # first color is black, last is red\n",
    "        cmap = LinearSegmentedColormap.from_list(\"Custom\", colors, N=len(time[cond][t]))\n",
    "        r = cmap(np.linspace(0,1,len(time[cond][t])))\n",
    "        \n",
    "        ax.add_collection(Line3DCollection(segments,colors=list(r)))\n",
    "        ax.set_xlim([min(pos[cond][:,0]), max(pos[cond][:,0])])\n",
    "        ax.set_ylim([min(pos[cond][:,1]), max(pos[cond][:,1])])\n",
    "        ax.set_zlim([min(pos[cond][:,2]), max(pos[cond][:,2])])\n",
    "        \n",
    "        ax.scatter(pos[cond][starts[j],0],pos[cond][starts[j],1],pos[cond][starts[j],2],color=colors[0])\n",
    "        ax.scatter(pos[cond][starts[j+1]-1,0],pos[cond][starts[j+1]-1,1],pos[cond][starts[j+1]-1,2],color=colors[1])\n",
    "        ax.get_xaxis().set_ticks([])\n",
    "        ax.get_yaxis().set_ticks([])\n",
    "        ax.get_zaxis().set_ticks([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3b0b713",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = MARBLE.construct_dataset(pos, features=vel, graph_type='cknn', k=10, stop_crit=0.05, local_gauges=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb29a0b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_plot = data.to_data_list()\n",
    "for i in [1,4,6]:\n",
    "    d = data_plot[i]\n",
    "    c = [(0, 0, 0), cm.viridis(i/6)] # first color is black, last is Ci\n",
    "    cmap = LinearSegmentedColormap.from_list(\"Custom\", c, N=140)\n",
    "    ind = np.linspace(0,1,140)\n",
    "    colors = cmap(ind[time[i][d.sample_ind].astype(int)])\n",
    "    plotting.fields([d], view=(10,90), figsize=(3,3), scale=2, width=7., color=colors, axes_visible=False)\n",
    "    plt.axis('on')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43d0816b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}