Skip to content

Instantly share code, notes, and snippets.

@neuromusic
Created February 22, 2018 06:11
Show Gist options
  • Save neuromusic/7d3e5595d7b6cb3a5f1d1cde658adcf8 to your computer and use it in GitHub Desktop.
Save neuromusic/7d3e5595d7b6cb3a5f1d1cde658adcf8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## requirements\n",
"# allensdk\n",
"# scikit-learn > 0.19\n",
"# xarray"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from allensdk.core.brain_observatory_cache import BrainObservatoryCache"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"boc = BrainObservatoryCache(manifest_file='/local1/data/boc/manifest.json',)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"oeid = 541206592\n",
"\n",
"# Initializations:\n",
"nwb_dataset = boc.get_ophys_experiment_data(oeid)\n",
"\n",
"# # Get Data:\n",
"# timestamps, dff = nwb_dataset.get_dff_traces()\n",
"# neuron_ids = nwb_dataset.get_cell_specimen_ids()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import xarray as xr\n",
"\n",
"from allensdk.brain_observatory.natural_scenes import NaturalScenes\n",
"\n",
"def get_ns_msrx(nwb_dataset):\n",
" ns = NaturalScenes(nwb_dataset)\n",
" mean_sweep_response = ns.mean_sweep_response.copy()\n",
" \n",
" # I don't know what dx is. goodbye!\n",
" mean_sweep_response.drop('dx',axis=1,inplace=True)\n",
" \n",
" # annotate the dataframe with useful indices and columns\n",
" time = pd.Series(\n",
" ns.timestamps[ns.stim_table['start']],\n",
" name='time',\n",
" )\n",
" neurons = pd.Series(\n",
" ns.cell_id,\n",
" name='neuron',\n",
" )\n",
" mean_sweep_response.set_index(time,inplace=True)\n",
" mean_sweep_response.columns = neurons\n",
" \n",
" # convert this to xarray & annotate images\n",
" srx = xr.DataArray(mean_sweep_response)\n",
" srx.coords['natural_image'] = ('time',ns.stim_table['frame'])\n",
" \n",
" return srx"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.DataArray (time: 5950, neuron: 154)>\n",
"array([[ 2.750398, 3.113332, 3.283231, ..., 3.246232, 17.838305,\n",
" 44.883263],\n",
" [ 5.472741, 4.520462, 1.848134, ..., 3.700325, 41.864319,\n",
" 55.052734],\n",
" [ 4.938696, 1.872071, 0.822514, ..., 1.446858, 19.081219,\n",
" -0.731454],\n",
" ..., \n",
" [ 0.984122, -1.035176, 2.519683, ..., -0.894795, 14.030047,\n",
" 1.291967],\n",
" [ 1.705522, 1.379635, 1.116845, ..., 0.169055, 6.26528 ,\n",
" 1.80034 ],\n",
" [ 0.420941, 2.543081, 2.991403, ..., 0.980473, -0.720777,\n",
" 4.779262]])\n",
"Coordinates:\n",
" * time (time) float64 545.2 545.5 545.7 546.0 546.2 546.5 546.7 ...\n",
" * neuron (neuron) int64 541510267 541510270 541510307 541510405 ...\n",
" natural_image (time) int64 92 27 52 37 103 1 54 19 54 -1 74 115 44 88 ...\n"
]
}
],
"source": [
"msrx = get_ns_msrx(nwb_dataset)\n",
"print msrx"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def decode(msrx):\n",
" \n",
" # get features and output\n",
" X = msrx.data\n",
" y = msrx['natural_image']\n",
" \n",
" # split training & testing\n",
" X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,stratify=y)\n",
" \n",
" # do the classification\n",
" lm = LogisticRegression(\n",
" solver='saga',\n",
" multi_class='ovr',\n",
" penalty='l1',\n",
" n_jobs=-1,\n",
" )\n",
" lm.fit(X_train,y_train)\n",
" return lm.score(X_test,y_test) * len(np.unique(y))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"19.9\n",
"1 loop, best of 1: 50.1 s per loop\n"
]
}
],
"source": [
"%%timeit -n 1 -r 1\n",
"print decode(msrx)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:jk]",
"language": "python",
"name": "conda-env-jk-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment