Cell Types Database

This notebook demonstrates most of the features of the AllenSDK that help manipulate data in the Cell Types Database. The main entry point will be through the CellTypesCache class.

CellTypesCache is responsible for downloading Cell Types Database data to a standard directory structure on your hard drive. If you use this class, you will not have to keep track of where your data lives, other than a root directory.

In [1]:
from allensdk.core.cell_types_cache import CellTypesCache

# Instantiate the CellTypesCache instance.  The manifest_file argument
# tells it where to store the manifest, which is a JSON file that tracks
# file paths.  If you supply a relative path (like this), it will go
# into your current working directory
ctc = CellTypesCache(manifest_file='cell_types/manifest.json')

# this saves the NWB file to 'cell_types/specimen_464212183/ephys.nwb'
data_set = ctc.get_ephys_data(464212183)

The data_set variable is an NwbDataSet instance, which has some methods we can use to access the injected current stimulus waveform and the voltage response waveform for all experimental sweeps. Let's pull one sweep out and plot it.

In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

sweep_number = 30
sweep_data = data_set.get_sweep(sweep_number)

index_range = sweep_data["index_range"]
i = sweep_data["stimulus"][0:index_range[1]+1] # in A
v = sweep_data["response"][0:index_range[1]+1] # in V
i *= 1e12 # to pA
v *= 1e3 # to mV

sampling_rate = sweep_data["sampling_rate"] # in Hz
t = np.arange(0, len(v)) * (1.0 / sampling_rate)

plt.style.use('ggplot')
fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].plot(t, v, color='black')
axes[1].plot(t, i, color='gray')
axes[0].set_ylabel("mV")
axes[1].set_ylabel("pA")
axes[1].set_xlabel("seconds")
Out[2]:
<matplotlib.text.Text at 0x7f4695f1e890>

Filtering Cells via Metadata

Cell records in the Cell Types Database come with a large amount of metadata. We have exposed the most commonly used of these are arguments to CellTypesCache.get_cells.

In [3]:
from allensdk.core.cell_types_cache import CellTypesCache
from allensdk.core.cell_types_cache import ReporterStatus as RS

ctc = CellTypesCache(manifest_file='cell_types/manifest.json', base_uri='http://testwarehouse:9000')

# this downloads metadata for all cells with morphology images
cells = ctc.get_cells(require_morphology = True)
print "Cells with morphology images: ", len(cells)

# cells with reconstructions
cells = ctc.get_cells(require_reconstruction = True)
print "Cells with reconstructions: ", len(cells)

# all cre positive cells
cells = ctc.get_cells(reporter_status = RS.POSITIVE)
print "Cre-positive cells: ", len(cells)

# cre negative cells with reconstructions
cells = ctc.get_cells(require_reconstruction = True, 
                      reporter_status = RS.NEGATIVE)
print "Cre-negative cells with reconstructions: ", len(cells)
Cells with morphology images:  511
Cells with reconstructions:  217
Cre-positive cells:  675
Cre-negative cells with reconstructions:  24

Cell Morphology Reconstructions

The Cell Types Database also contains 3D reconstructions of neuronal morphologies. The data are presented in the SWC format. We'll download a particular cell's reconstrution here.

The AllenSDK contains a module that makes it easier to work with the SWC files. We'll see how the data is contained in the file by looking at the first node.

In [4]:
# download and open an SWC file
cell_id = 480114344
morphology = ctc.get_reconstruction(cell_id) 

# the compartment list has all of the nodes in the file
print morphology.compartment_list[0]
{'parent': -1, 'children': [1, 1763, 2012, 2089, 2421, 2604, 2821, 3147, 3440, 3491, 3552, 4015], 'radius': 7.6078, 'tree_id': 0, 'y': 503.0168, 'x': 444.3296, 'z': 31.92, 'type': 1, 'id': 0}

Note that the type field refers to the type of neuronal compartment. The values can be 1 for the soma, 2 for the axon, 3 for dendrites, and 4 for apical dendrites (if present).

Morphologies now also come with marker files, which contains points of interest in the reconstruction. The marker file contains locations where dendrites have been truncated due to slicing and when axons were not reconstructed. The name field indicates the type of marker (10 for dendrite truncation, 20 for no reconstruction).

In [5]:
# download and open a marker file
markers = ctc.get_reconstruction_markers(cell_id) 
print len(markers)
print markers[0]
21
{'y': 496.4319, 'x': 527.5029999999999, 'z': 12.4555, 'name': 10}

We can use this data to draw lines between each node and all its children to get a drawing of the cell. We'll do it looking at it from the front and from the side.

In [6]:
from allensdk.core.swc import Marker
fig, axes = plt.subplots(1, 2, sharey=True, sharex=True)
axes[0].set_aspect('equal', 'box-forced')
axes[1].set_aspect('equal', 'box-forced')

# Make a line drawing of x-y and y-z views
for n in morphology.compartment_list:
    for c in morphology.children_of(n):
        axes[0].plot([n['x'], c['x']], [n['y'], c['y']], color='black')
        axes[1].plot([n['z'], c['z']], [n['y'], c['y']], color='black')

# cut dendrite markers
dm = [ m for m in markers if m['name'] == Marker.CUT_DENDRITE ]

axes[0].scatter([m['x'] for m in dm], [m['y'] for m in dm], color='#3333ff')
axes[1].scatter([m['z'] for m in dm], [m['y'] for m in dm], color='#3333ff')

# no reconstruction markers
nm = [ m for m in markers if m['name'] == Marker.NO_RECONSTRUCTION ]

axes[0].scatter([m['x'] for m in nm], [m['y'] for m in nm], color='#333333')
axes[1].scatter([m['z'] for m in nm], [m['y'] for m in nm], color='#333333')

axes[0].set_ylabel('y')
axes[0].set_xlabel('x')
axes[1].set_xlabel('z')
Out[6]:
<matplotlib.text.Text at 0x7f4695f8b810>

Electrophysiology Features

The Cell Types Database contains a set of features that have already been computed, which could serve as good starting points for analysis. We can query the database using the SDK to get these features.

In [7]:
import pprint
pp = pprint.PrettyPrinter(indent=2)

# download all electrophysiology features for all cells
ephys_features = ctc.get_ephys_features()

# filter down to a specific cell
specimen_id = 464212183
cell_ephys_features = [f for f in ephys_features if f['specimen_id'] == specimen_id]

pp.pprint(cell_ephys_features)
[ { u'adaptation': 0.0106601916957407,
    u'avg_isi': 50.9122222222222,
    u'electrode_0_pa': -73.0906168922019,
    u'f_i_curve_slope': 0.387014762165118,
    u'fast_trough_t_long_square': 0.223855,
    u'fast_trough_t_ramp': 2.85047,
    u'fast_trough_t_short_square': 0.00812666666666673,
    u'fast_trough_v_long_square': -58.59375,
    u'fast_trough_v_ramp': -56.6354192097982,
    u'fast_trough_v_short_square': -62.0520858764648,
    u'has_burst': False,
    u'has_delay': False,
    u'has_pause': False,
    u'id': 464366365,
    u'input_resistance_mohm': 129.36264,
    u'latency': 30.78,
    u'peak_t_long_square': 0.22139,
    u'peak_t_ramp': 2.84821,
    u'peak_t_short_square': 0.0036491666666667,
    u'peak_v_long_square': 10.71875,
    u'peak_v_ramp': 26.4895833333333,
    u'peak_v_short_square': 16.0000006357829,
    u'rheobase_sweep_id': 464306919,
    u'rheobase_sweep_number': 45,
    u'ri': 223.437574040889,
    u'sag': 0.200237980367221,
    u'seal_gohm': 1.80903808,
    u'slow_trough_t_long_square': 0.22639,
    u'slow_trough_t_ramp': 2.85321,
    u'slow_trough_t_short_square': 0.253295,
    u'slow_trough_v_long_square': -56.6875,
    u'slow_trough_v_ramp': -54.4687525431315,
    u'slow_trough_v_short_square': -65.7291679382324,
    u'specimen_id': 464212183,
    u'tau': 19.0359532943262,
    u'threshold_i_long_square': 70.0,
    u'threshold_i_ramp': 71.25,
    u'threshold_i_short_square': 380.0,
    u'threshold_t_long_square': 0.220845,
    u'threshold_t_ramp': 2.84760166666667,
    u'threshold_t_short_square': 0.00310750000000004,
    u'threshold_v_long_square': -40.53125,
    u'threshold_v_ramp': -39.3958346048991,
    u'threshold_v_short_square': -40.7604179382324,
    u'thumbnail_sweep_id': 464306887,
    u'trough_t_long_square': 0.223855,
    u'trough_t_ramp': 2.85047,
    u'trough_t_short_square': 0.253295,
    u'trough_v_long_square': -58.59375,
    u'trough_v_ramp': -56.6354192097982,
    u'trough_v_short_square': -65.7291679382324,
    u'upstroke_downstroke_ratio_long_square': 1.91568368870623,
    u'upstroke_downstroke_ratio_ramp': 2.04554102487431,
    u'upstroke_downstroke_ratio_short_square': 1.82117838780592,
    u'vm_for_sag': -90.46875,
    u'vrest': -65.7716649373372}]

That's how to get all the ephys features for a given specimen - what if we want a particular feature for all cells?

In [8]:
updown = np.array([f['upstroke_downstroke_ratio_long_square'] for f in ephys_features], dtype=float)
fasttrough = np.array([f['fast_trough_v_long_square'] for f in ephys_features], dtype=float)

plt.figure()
plt.scatter(fasttrough, updown, color='#2ca25f')
plt.ylabel("upstroke-downstroke ratio")
plt.xlabel("fast trough depth (mV)")
Out[8]:
<matplotlib.text.Text at 0x7f469111d2d0>

Let's use numpy to fit a regression line to these data and plot it.

In [9]:
A = np.vstack([fasttrough, np.ones_like(updown)]).T
print "First 5 rows of A:"
print A[:5, :]

m, c = np.linalg.lstsq(A, updown)[0]
print "m", m, "c", c

plt.figure()
plt.scatter(fasttrough, updown, color='#2ca25f')
plt.plot(fasttrough, m * fasttrough + c, c='gray')
plt.ylabel("upstroke-downstroke ratio")
plt.xlabel("fast trough depth (mV)")
First 5 rows of A:
[[-59.53125381   1.        ]
 [-50.25         1.        ]
 [-51.53125      1.        ]
 [-43.96875381   1.        ]
 [-38.25000381   1.        ]]
m 0.108437886192 c 8.39002615421
Out[9]:
<matplotlib.text.Text at 0x7f4695e91d50>

It looks like there may be roughly two clusters in the data above. Maybe they relate to whether the cells are presumably excitatory (spiny) cells or inhibitory (aspiny) cells. Let's query the API and split up the two sets to see.

In [10]:
cells = ctc.get_cells()

cell_index = { c['id']: c for c in cells}

dendrite_types = ['spiny', 'aspiny']
data = {}

# group fast trough depth and upstroke downstroke ratio values by cell dendrite type
for dendrite_type in dendrite_types:
    type_features = [f for f in ephys_features if cell_index[f['specimen_id']]['dendrite_type'] == dendrite_type]
    data[dendrite_type] = {
        "fasttrough": [f['fast_trough_v_long_square'] for f in type_features],
        "updown": [f['upstroke_downstroke_ratio_short_square'] for f in type_features],
    }
    
plt.figure()
for a_type, color in zip(dendrite_types, ["#d95f02", "#7570b3"]):
    plt.scatter(data[a_type]['fasttrough'], data[a_type]['updown'], color=color, label=a_type)
plt.legend(loc='best')
plt.ylabel("upstroke-downstroke ratio")
plt.xlabel("fast trough depth (mV)")
Out[10]:
<matplotlib.text.Text at 0x7f4685d66310>

Morphology Features

The Cell Types Database contains a set of precomputed morphological features for cells that have reconstructions. You can access morphology features by themselves, or combined with the electrophysiology features.

In [11]:
# download all morphology features for cells with reconstructions
morphology_features = ctc.get_morphology_features()

# or download both morphology and ephys features
# this time we'll ask the cache to return a pandas dataframe
all_features = ctc.get_all_features(dataframe=True, require_reconstruction=True)
all_features
Out[11]:
adaptation avg_isi electrode_0_pa f_i_curve_slope fast_trough_t_long_square fast_trough_t_ramp fast_trough_t_short_square fast_trough_v_long_square fast_trough_v_ramp fast_trough_v_short_square ... overall_depth overall_height overall_width scale_factor_x scale_factor_y scale_factor_z soma_surface total_length total_surface total_volume
0 0.032340 55.895000 -55.964379 0.280000 0.094755 1.833668 0.008380 -50.250000 -52.593754 -59.161462 ... 653.230947 237.978924 128.514422 0.1144 0.1144 0.28 408.738892 2680.535033 4196.270924 602.041561
1 0.162880 59.473000 5.602500 0.091032 0.048620 5.704792 0.008326 -51.531250 -53.104169 -59.296878 ... 573.451370 493.755936 100.580305 0.1144 0.1144 0.28 963.795060 3071.541428 4361.353214 578.994384
2 0.016037 20.354773 -106.373119 0.458636 0.034275 1.919597 0.008306 -55.343750 -56.718751 -59.531253 ... 535.828883 142.142834 56.275331 0.1144 0.1144 0.28 595.891706 1272.840750 2874.356377 752.221511
3 0.047695 62.894000 -20.613126 0.162090 0.098805 3.154135 0.007852 -46.000004 -47.968754 -48.262502 ... 502.983314 279.561199 90.843955 0.1144 0.1144 0.28 484.149806 3025.334300 4830.323252 729.345924
4 0.262938 160.955000 9.615625 0.113985 0.134700 2.129865 0.008336 -54.500004 -55.718750 -60.531251 ... 225.257388 287.797962 80.772293 0.1144 0.1144 0.28 472.997736 1553.518641 1882.705244 205.082828
5 0.058364 16.679000 6.144375 0.234333 0.045330 9.122870 0.008223 -53.687500 -48.625000 -62.943752 ... 239.091637 306.035617 65.803484 0.1144 0.1144 0.28 495.894936 2161.865463 2687.937552 305.144975
6 0.002757 12.574038 6.654375 1.028846 0.009590 10.050993 0.008100 -60.531250 -52.479168 -66.710939 ... 300.367350 289.100172 83.925599 0.1144 0.1144 0.28 431.568567 2096.006044 2233.145151 211.695050
7 0.015226 85.318000 -19.323125 0.164266 0.148215 7.652193 0.008350 -43.468750 -44.604169 -48.679690 ... 607.033616 346.054784 129.854580 0.1144 0.1144 0.28 582.760965 3555.127773 5008.344160 640.288788
8 0.094251 94.870000 20.307500 0.156434 0.088055 6.237637 0.008315 -49.250004 -50.041668 -54.256252 ... 678.645398 265.505535 119.427840 0.1144 0.1144 0.28 803.015497 3499.230438 8789.318010 2070.267168
9 0.002545 8.300889 2.671250 1.297212 0.009550 11.556347 0.008419 -65.281250 -57.270836 -67.210943 ... 458.358963 236.059028 86.804944 0.1144 0.1144 0.28 593.664922 1989.397951 2172.397762 213.002294
10 0.025413 59.215625 10.571250 0.391661 0.155690 3.509217 0.004336 -55.843750 -54.781253 -59.307295 ... 338.360236 223.658346 68.553431 0.1144 0.1144 0.28 594.587349 2504.297527 2944.303545 323.793934
11 0.000337 11.155114 0.000000 1.020000 0.010705 13.100170 0.008372 -60.718754 -54.859375 -70.489586 ... 243.566333 264.690962 90.991272 0.1144 0.1144 0.28 875.689521 2368.137007 2536.232362 227.113869
12 0.031105 62.496667 2.825000 0.282226 0.252190 1.398345 0.008329 -52.968750 -52.250004 -54.981254 ... 389.845108 198.085907 77.292857 0.1144 0.1144 0.28 222.491471 1873.325244 2390.845095 300.451372
13 0.117267 67.903333 -46.703749 0.199413 0.071450 5.513640 0.008346 -50.218750 -52.041668 -56.601564 ... 504.739683 308.677120 80.813314 0.1144 0.1144 0.28 371.517864 3122.040442 3811.495256 403.254072
14 0.054231 108.420000 -11.760625 0.201852 0.164395 5.215017 0.008342 -51.531250 -52.114586 -56.331253 ... 560.074313 164.071151 91.006056 0.1144 0.1144 0.28 429.486371 1286.255181 1495.093324 151.330010
15 0.001041 15.797736 -36.656251 0.737292 0.864765 17.678270 0.008275 -52.937500 -63.234377 -72.800003 ... 387.527152 350.776547 115.684744 0.1144 0.1144 0.28 855.481599 3619.553468 3930.266909 395.897076
16 0.025550 26.229000 9.296874 0.494713 0.041130 6.979590 0.008272 -60.843754 -58.552086 -64.770835 ... 384.316420 501.600805 86.914674 0.1144 0.1144 0.28 568.431860 1919.464884 3709.366810 664.549122
17 0.010660 50.912222 -73.090617 0.387015 0.223855 2.850470 0.008127 -58.593750 -56.635419 -62.052086 ... 244.223441 330.932299 95.479144 0.1144 0.1144 0.28 387.001625 2445.186641 3080.224593 366.614318
18 0.001346 20.491444 -21.703127 0.898805 0.920800 14.596970 0.008320 -61.781254 -60.843753 -72.500003 ... 338.373896 181.531876 53.388140 0.1144 0.1144 0.28 421.100447 1776.437870 2677.733791 367.056565
19 NaN NaN 17.478751 0.045268 0.069150 4.287580 0.005389 -47.437504 -50.343752 -52.335941 ... 814.496654 208.454992 133.716132 0.1144 0.1144 0.28 972.831483 2475.023746 4634.423998 823.209376
20 0.074486 97.842778 -54.256880 0.175714 0.102365 3.558005 0.008442 -46.437500 -47.635418 -48.932294 ... 537.328669 259.743094 102.703365 0.1144 0.1144 0.28 605.553756 2743.672673 3411.311101 385.122593
21 0.008947 9.046000 -5.978125 1.555833 0.009135 10.680760 0.008242 -61.500000 -57.479169 -68.989588 ... 226.510688 275.208415 79.149596 0.1144 0.1144 0.28 605.784964 1203.033963 1535.216898 182.311450
22 0.158752 81.548000 -40.439999 0.109963 0.095725 2.645368 0.008368 -49.750004 -49.895836 -54.687502 ... 634.786109 319.594989 77.711328 0.1144 0.1144 0.28 458.959874 3976.251713 5004.683092 566.911454
23 NaN 64.985000 17.576876 0.074549 0.080415 5.381855 0.008483 -48.468754 -50.802086 -56.062503 ... 556.697329 265.179942 106.203603 0.1144 0.1144 0.28 1029.157900 4537.213322 5175.812742 549.894763
24 0.016570 61.639167 6.993751 0.198678 0.059565 4.703713 0.008275 -49.437500 -53.729170 -53.741071 ... 396.797203 186.515981 87.723365 0.1144 0.1144 0.28 428.235092 1557.223771 2392.902646 323.459970
25 0.013284 44.057381 17.628125 0.427210 0.088980 1.627812 0.004335 -59.000004 -60.583337 -61.656254 ... 264.719091 158.070904 60.271685 0.1144 0.1144 0.28 381.823271 1168.579450 1293.158288 125.731662
26 NaN 49.125000 -52.387497 0.126211 0.063775 3.554650 0.008286 -50.437504 -49.395833 -55.736607 ... 966.764186 382.548544 119.738389 0.1144 0.1144 0.28 1185.234872 4746.553181 6765.253925 928.352012
27 NaN 6.680000 -77.923744 1.057234 0.010860 NaN 0.008176 -65.218758 NaN -69.325003 ... 404.460034 426.605371 48.425321 0.1144 0.1144 0.28 563.901991 2066.426541 2266.311212 224.042081
28 0.012081 56.176765 -18.041249 0.314655 0.334255 1.901240 0.008413 -54.093754 -53.000004 -57.093753 ... 524.730306 214.211768 89.324040 0.1144 0.1144 0.28 501.099624 2654.702816 5102.887108 929.323888
29 0.032048 63.995357 17.658123 0.170000 0.108430 4.455838 0.008447 -50.718750 -50.937503 -54.239587 ... 342.716999 200.716220 120.924534 0.1144 0.1144 0.28 336.298759 2056.461755 1745.563174 184.193730
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
187 NaN 54.240000 -29.443125 0.046795 0.068555 6.486273 0.008295 -47.656250 -48.989585 -51.109375 ... 401.339336 239.321904 66.525366 0.1144 0.1144 0.28 510.626515 2432.590814 3386.398058 427.332501
188 0.109238 152.815000 21.684375 0.193931 0.023075 7.053842 0.008292 -49.781250 -48.697920 -58.937503 ... 538.815337 508.151691 162.257228 0.1144 0.1144 0.28 1807.151257 4342.347023 7415.269688 1103.915009
189 0.118105 27.653750 1.560000 0.291842 0.084910 2.706083 0.008438 -53.125000 -55.000001 -59.087502 ... 111.897800 311.052500 431.618600 0.1144 0.1144 0.28 230.260349 1557.246118 2532.990296 435.217786
190 -0.014975 59.914667 -26.825625 0.189221 0.388850 6.038623 0.008381 -53.562504 -54.041668 -62.475002 ... 391.369739 362.887717 124.884712 0.1144 0.1144 0.28 2215.529160 3556.152157 9190.377310 2042.709024
191 0.077498 168.123000 -24.603750 0.105000 0.259580 3.150612 0.008570 -43.125004 -46.906253 -50.875004 ... 623.636166 353.709088 88.438294 0.1144 0.1144 0.28 748.332869 4725.213039 5555.487651 596.833970
192 NaN 10.850000 5.455000 1.090363 0.013725 9.124020 0.005160 -64.156250 -59.406250 -66.015627 ... 260.442307 136.486635 26.899105 0.1144 0.1144 0.28 591.379543 1595.397754 2314.623329 302.103532
193 0.065383 88.283500 -50.815002 0.173862 0.134770 3.189058 0.008359 -52.937504 -51.822919 -53.364586 ... 492.942141 307.104642 77.067526 0.1144 0.1144 0.28 252.145175 3043.280348 4766.725047 724.883590
194 0.054479 164.649000 1.979375 0.036915 0.137170 1.044247 0.004850 -58.937500 -58.208337 -61.781254 ... 204.700773 228.549249 75.440318 0.1144 0.1144 0.28 672.510266 798.299791 1157.554352 180.949682
195 NaN 41.485000 16.541874 0.052790 0.056290 5.778418 0.008449 -48.968750 -52.458337 -54.632813 ... 390.390550 227.532096 76.255795 0.1144 0.1144 0.28 750.669015 2615.611127 5199.431382 964.101750
196 0.027302 98.919444 -17.070000 0.171280 0.089805 4.132640 0.008473 -45.000000 -47.572919 -55.150004 ... 762.282211 264.933890 179.537921 0.1144 0.1144 0.28 956.788538 4768.622290 8003.693754 1330.059979
197 0.003120 30.758833 -109.973114 0.429167 0.137040 5.283087 0.008463 -58.687500 -58.968751 -70.177086 ... 322.216475 246.640460 65.442460 0.1144 0.1144 0.28 459.054472 2271.834988 3793.672398 591.608729
198 0.042162 79.650909 -78.186249 0.219318 0.315745 3.881518 0.008410 -51.875004 -50.416668 -53.743752 ... 483.530122 255.406131 106.729986 0.1144 0.1144 0.28 603.431342 3793.563530 6513.819846 1149.252492
199 NaN 22.750000 -81.054365 0.015760 0.039555 1.794898 0.008305 -52.593754 -45.531253 -55.906250 ... 775.232684 241.015527 87.510714 0.1144 0.1144 0.28 398.470728 2763.019109 4494.294956 678.951441
200 0.003542 15.533492 28.868125 0.900457 0.025125 5.215942 0.008380 -61.312504 -58.843751 -64.768756 ... 296.604989 191.335907 57.588653 0.1144 0.1144 0.28 540.874112 1656.870911 2064.177333 229.705911
201 NaN 40.300000 24.364375 0.040861 0.085650 6.577685 0.004520 -55.593750 -50.812504 -58.242190 ... 469.819806 345.396890 97.754735 0.1144 0.1144 0.28 2413.939311 5890.132266 7711.321973 967.424983
202 0.026631 100.808333 -19.891875 0.129762 0.143885 5.443575 0.008522 -50.406250 -50.843751 -55.656252 ... 712.531381 296.160052 159.176910 0.1144 0.1144 0.28 926.931343 5386.989885 6836.554264 775.553937
203 0.082536 103.197000 -20.010625 0.083122 0.212675 4.996077 0.008307 -49.656254 -47.552083 -51.187501 ... 581.592187 300.825319 91.436820 0.1144 0.1144 0.28 427.452649 3602.357326 4041.719013 394.652243
204 0.026536 97.820000 -62.933748 0.130357 0.130905 4.988785 0.008490 -45.687500 -47.020835 -51.109378 ... 655.077839 247.304380 202.120968 0.1144 0.1144 0.28 892.056940 5440.652990 7162.580897 833.080330
205 NaN NaN 2.535625 0.571591 0.792580 21.869873 0.008293 -58.187504 -58.447920 -68.537503 ... 375.411274 192.710180 82.540132 0.1144 0.1144 0.28 447.521879 1928.227774 1863.117969 157.549440
206 0.016399 29.422424 -8.445624 0.470219 0.072620 1.983080 0.008490 -51.781254 -53.687500 -56.906254 ... 396.444727 170.808546 118.167639 0.1144 0.1144 0.28 304.418266 1935.231353 2138.750481 204.107894
207 0.018206 52.890556 -8.875001 0.207784 0.092525 1.293038 0.005143 -47.437504 -48.510420 -52.643754 ... 683.395299 282.361544 81.366049 0.1144 0.1144 0.28 721.717285 2742.436802 5903.642856 1148.017573
208 NaN 26.865000 21.707500 0.057228 0.039110 13.153143 0.008365 -53.625004 -49.395836 -60.796877 ... 377.762794 202.758689 60.211308 0.1144 0.1144 0.28 235.957859 1579.874877 2258.238334 299.593960
209 0.000193 21.479333 3.681250 0.743582 0.202900 6.943950 0.008273 -62.343754 -62.385419 -66.750002 ... 390.786848 280.898069 65.426541 0.1144 0.1144 0.28 398.179045 1978.877478 2459.260781 267.462955
210 NaN 5.570000 7.002500 0.679275 0.011700 13.722295 0.008217 -57.281254 -51.812501 -65.351562 ... 399.740075 228.029325 81.832747 0.1144 0.1144 0.28 354.310335 2280.248798 3038.223875 359.910511
211 0.288858 147.790000 -13.231875 0.159756 0.031790 2.557195 0.008505 -49.281250 -46.750000 -52.906254 ... 504.171337 219.681611 108.891458 0.1144 0.1144 0.28 475.469168 2956.462684 3936.017811 490.473898
212 0.008371 138.005000 -30.640001 0.173458 0.467745 3.377910 0.008270 -52.125004 -51.937502 -57.031253 ... 703.266557 233.408549 165.522909 0.1144 0.1144 0.28 814.359936 4000.180860 6483.407692 1031.800874
213 0.030534 9.653333 -30.427501 0.364712 0.019310 11.732247 0.008331 -55.906250 -45.302085 -66.968752 ... 331.748591 225.793982 80.606924 0.1144 0.1144 0.28 442.653657 1710.425988 1892.287072 185.262020
214 0.006343 90.144000 -30.894374 0.143931 0.171475 5.346075 0.008512 -49.000004 -50.822920 -57.531251 ... 939.065580 568.569136 243.487424 0.1144 0.1144 0.28 765.509836 7473.965977 9967.182394 1353.084392
215 0.013217 30.818065 -4.082500 0.497095 0.079415 2.454622 0.008294 -48.343750 -47.510417 -55.812502 ... 323.862502 226.159421 127.331622 0.1144 0.1144 0.28 703.618317 2206.656685 2979.117571 346.368485
216 0.012130 41.533043 18.836875 0.455522 0.073730 5.038321 0.008295 -58.562504 -58.007816 -62.781252 ... 337.054642 321.415005 101.910310 0.1144 0.1144 0.28 479.179681 2079.954471 2047.935138 171.779336

217 rows × 82 columns

Computing Electrophysiology Features

The AllenSDK contains the code used to compute the electrophysiology features you accessed above. You can run it yourself like this.

In [12]:
from allensdk.ephys.feature_extractor import EphysFeatureExtractor

sweep_number = 35
sweep_data = data_set.get_sweep(sweep_number)

index_range = sweep_data["index_range"]
i = sweep_data["stimulus"][0:index_range[1]+1] # in A
v = sweep_data["response"][0:index_range[1]+1] # in V
i *= 1e12 # to pA
v *= 1e3 # to mV

sampling_rate = sweep_data["sampling_rate"] # in Hz
t = np.arange(0, len(v)) * (1.0 / sampling_rate)

fx = EphysFeatureExtractor()

stim_start = 1.0
stim_duration = 1.0

fx.process_instance("", v, i, t, stim_start, stim_duration, "")
feature_data = fx.feature_list[0].mean
print "Avg spike width: {:.2f} ms".format(feature_data['width'])
print "Avg spike threshold: {:.1f} mV".format(feature_data["threshold"])
Avg spike width: 0.93 ms
Avg spike threshold: -35.6 mV
In [13]:
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(feature_data["spikes"][0])
{ 'downstroke': -114.5881505524148,
  'downstroke_i': 210.0,
  'downstroke_idx': 205802,
  'downstroke_t': 1.02901,
  'downstroke_v': -7.9375005,
  'f_fast_ahp': -55.5,
  'f_fast_ahp_i': 210.0,
  'f_fast_ahp_t': 1.0306000000000002,
  'f_fast_ahp_v': -55.5,
  'f_peak': 18.15625,
  'f_peak_i': 210.0,
  'f_peak_t': 1.02868,
  'f_slow_ahp': -49.8125,
  'f_slow_ahp_t': 1.0336800000000002,
  'f_slow_ahp_time': 0.40799673602611697,
  'f_trough': -55.5,
  'half_height_width': 0.00059500000000012321,
  'peak_idx': 205736,
  'rise_time': 0.00055999999999989392,
  't': 1.0281100000000001,
  't_idx': 205622,
  't_idx_n30': 205666,
  't_n30': 1.0283300000000002,
  'thresh_ramp': 60.584553175236103,
  'threshold': -40.781254,
  'threshold_i': 210.0,
  'threshold_idx': 205622,
  'threshold_t': 1.0281100000000001,
  'threshold_v': -40.781254,
  'trough_i': 210.0,
  'trough_idx': 206120,
  'trough_t': 1.0306000000000002,
  'trough_v': -55.5,
  'upstroke': 216.04000847544683,
  'upstroke_i': 210.0,
  'upstroke_idx': 205690,
  'upstroke_t': 1.0284500000000001,
  'upstroke_v': -9.937501,
  'width': 0.69500000000011219}

A list comprehension is an easy way to pull out the spike times.

In [14]:
spike_times = [s["t"] for s in feature_data["spikes"]]

print spike_times[:5] 
[1.0281100000000001, 1.0409350000000002, 1.0536750000000001, 1.0686450000000001, 1.082835]
In [15]:
plt.figure()
plt.plot(t, v, color='black')

min_v = v.min()

min_v -= 5.0

plt.scatter(spike_times, np.ones(len(spike_times)) * min_v, c='r')
plt.xlim(0.9, 1.2)
Out[15]:
(0.9, 1.2)