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)

i = sweep_data["stimulus"] # in A
v = sweep_data["response"] # 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 0x1112a61d0>

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.

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

ctc = CellTypesCache()

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

# download and open an SWC file
cell_id = 464212183
morphology = ctc.get_reconstruction(cell_id) 
print morphology
Cells with reconstructions:  73
<allensdk.core.swc.Morphology object at 0x10893dd10>

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]:
compartment_list = morphology.compartment_list
print compartment_list[0]
{'parent': '-1', 'children': ['2', '1827', '1037', '2637'], 'radius': 5.9088, 'y': 343.3144, 'x': 444.3296, 'z': 48.72, 'type': 1, 'id': '1'}

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).

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 [5]:
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:
    child_nodes = [c for c in morphology.compartment_list if c['id'] in n['children']] 
    for c in child_nodes:
        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')

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

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 [6]:
# 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]

print cell_ephys_features
[{u'tau': 19.0359532881798, u'threshold_t_long_square': 0.220845, u'thumbnail_sweep_id': 464306887, u'threshold_v_short_square': -40.7604179382324, u'threshold_v_ramp': -39.3958346048991, u'peak_v_short_square': 16.0000006357829, u'avg_isi': 50.9122222222222, u'sag': 0.200237980367221, u'blowout_voltage': -1.0164100676775, u'trough_t_ramp': 2.85047, u'threshold_t_ramp': 2.84760166666667, u'slow_trough_v_ramp': -54.4687525431315, u'adaptation': 0.0106601916957408, u'has_pause': False, u'electrode_0_pa': -73.0906168922019, u'trough_v_long_square': -58.59375, u'input_resistance_mohm': 129.36264, u'id': 464366365, u'latency': 30.7799999999998, u'fast_trough_v_ramp': -56.6354192097982, u'vm_for_sag': -90.46875, u'slow_trough_v_long_square': -56.6875, u'rheobase_sweep_id': 464306919, u'peak_t_ramp': 2.84821, u'threshold_t_short_square': 0.00310749999999993, u'has_burst': False, u'upstroke_downstroke_ratio_short_square': 1.82117838780572, u'slow_trough_t_long_square': 0.22639, u'threshold_v_long_square': -40.53125, u'fast_trough_t_long_square': 0.223855, u'ri': 223.43757404089, u'has_delay': False, u'upstroke_downstroke_ratio_ramp': 2.0455410245709, u'trough_t_long_square': 0.223855, u'specimen_id': 464212183, u'threshold_i_long_square': 70.0, u'initial_access_resistance': 9.780094, u'threshold_i_short_square': 380.0, u'slow_trough_t_ramp': 2.85321, u'peak_v_ramp': 26.4895833333333, u'fast_trough_v_short_square': -62.0520858764648, u'fast_trough_t_short_square': 0.00812666666666658, u'peak_t_short_square': 0.00364916666666659, u'threshold_i_ramp': 71.25, u'slow_trough_v_short_square': -65.7291679382324, u'fast_trough_t_ramp': 2.85047, u'slow_trough_t_short_square': 0.253295, u'trough_v_short_square': -65.7291679382324, u'f_i_curve_slope': 0.387014762165118, u'trough_t_short_square': 0.253295, u'peak_t_long_square': 0.22139, u'seal_gohm': 1.80903808, u'fast_trough_v_long_square': -58.59375, u'upstroke_downstroke_ratio_long_square': 1.91568368870623, u'trough_v_ramp': -56.6354192097982, u'peak_v_long_square': 10.71875, u'vrest': -65.7716620309012}]

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

In [7]:
updown = np.array([f['upstroke_downstroke_ratio_short_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[7]:
<matplotlib.text.Text at 0x1112b0f50>

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

In [8]:
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:
[[-51.40625      1.        ]
 [-64.28125763   1.        ]
 [-59.09375      1.        ]
 [-44.96875      1.        ]
 [-51.59375      1.        ]]
m 0.107924372039 c 8.48403250554
Out[8]:
<matplotlib.text.Text at 0x108958ad0>

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 [9]:
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[9]:
<matplotlib.text.Text at 0x1112290d0>

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 [10]:
# 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[10]:
adaptation avg_isi blowout_voltage 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 ... 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.001518 5.576489 0.235004 -32.447503 1.519671 0.008010 11.551695 0.008274 -64.281258 -57.416668 ... 67.4800 299.750 318.947 0.1144 0.1144 0.28 725.795 2776.78 3223.14 355.789
1 0.262938 160.955000 -0.397382 9.615625 0.113985 0.197705 2.129865 0.008336 -52.906254 -55.718750 ... 68.3312 280.509 213.702 0.1144 0.1144 0.28 443.954 1738.01 2043.80 225.805
2 NaN NaN -3.529527 57.681249 0.774324 0.712640 NaN 0.008280 -56.031250 NaN ... 91.3063 525.217 337.526 0.1144 0.1144 0.28 440.585 2440.56 2811.61 304.924
3 0.006230 58.665625 -3.332600 0.000000 0.318779 0.145310 3.524472 0.007039 -66.781250 -61.895835 ... 149.0330 348.577 432.546 0.1144 0.1144 0.28 618.676 4948.57 6284.62 701.404
4 0.047238 11.168750 -2.094461 -29.520626 1.354663 0.009690 15.584375 0.008377 -65.375000 -61.281254 ... 112.2990 506.792 366.080 0.1144 0.1144 0.28 518.812 3872.34 6587.47 1032.670
5 -0.012286 12.748043 -0.412319 11.470626 1.493236 0.959935 14.848308 0.008265 -56.500004 -56.406253 ... 94.5891 420.420 302.174 0.1144 0.1144 0.28 602.785 3526.53 5306.98 692.118
6 0.058364 16.679000 -0.143573 6.144375 0.234333 0.045330 9.122870 0.008223 -53.687500 -48.625000 ... 64.1594 270.291 279.492 0.1144 0.1144 0.28 481.712 2403.80 2985.06 336.246
7 NaN NaN 0.929497 -23.622500 0.251905 0.153160 3.023893 0.008583 -50.500000 -53.010417 ... 122.0110 526.240 256.027 0.1144 0.1144 0.28 641.059 3342.78 4720.35 596.709
8 0.029716 49.585526 0.325366 -31.433123 0.233046 0.056430 1.456000 0.008472 -51.500000 -49.046877 ... 103.4100 463.378 246.074 0.1144 0.1144 0.28 610.486 2159.88 2869.99 363.123
9 0.145805 93.673333 3.636507 -2.874375 0.197433 0.101025 2.442425 0.008392 -47.906250 -49.208335 ... 60.5338 280.407 307.497 0.1144 0.1144 0.28 332.412 1911.04 2330.19 264.918
10 0.017522 75.903750 -2.601302 -28.577500 0.142028 0.273425 8.014652 0.008431 -56.218750 -57.718750 ... 44.7689 174.689 157.341 0.1144 0.1144 0.28 360.946 2864.31 2360.63 194.550
11 0.001093 9.344811 -0.607454 -38.457501 0.916073 0.012460 NaN 0.008332 -57.687504 NaN ... 38.9200 244.930 213.699 0.1144 0.1144 0.28 339.429 1528.38 2327.83 305.516
12 0.092932 101.343333 -5.041907 -15.786875 0.149167 0.133195 2.727092 0.008369 -51.468754 -51.828125 ... 57.9600 372.507 413.761 0.1144 0.1144 0.28 488.771 2637.77 4389.64 652.599
13 0.045002 90.260000 -3.797424 22.956250 0.170833 0.119585 5.239152 0.008480 -51.812500 -52.239585 ... 70.5978 458.380 271.877 0.1144 0.1144 0.28 309.340 2620.75 3294.68 357.876
14 0.036296 74.822500 2.840852 -18.065001 0.216659 0.120990 2.927895 0.008492 -46.531254 -47.218751 ... 67.9151 425.776 283.941 0.1144 0.1144 0.28 489.289 2692.54 3579.97 415.400
15 0.288858 147.790000 1.350741 -13.231875 0.150430 0.031790 2.557195 0.008505 -49.281250 -46.750000 ... 117.5430 540.861 389.825 0.1144 0.1144 0.28 657.662 3599.75 5633.82 823.960
16 NaN 49.125000 -5.862282 -52.387497 0.126211 0.090405 3.554650 0.008286 -49.968750 -49.395833 ... 95.4797 544.773 851.594 0.1144 0.1144 0.28 961.695 5157.46 7327.24 944.544
17 0.016570 61.639167 4.169163 6.993751 0.198678 0.059565 4.703713 0.008275 -49.437500 -53.729170 ... 69.9135 380.609 293.894 0.1144 0.1144 0.28 521.270 1918.80 3245.81 481.863
18 0.042079 57.273529 -0.496271 22.189375 0.236621 0.031575 1.549093 0.008517 -52.093750 -51.385417 ... 150.7780 806.177 724.724 0.1144 0.1144 0.28 896.973 4546.40 6885.29 931.916
19 0.003120 30.758833 -4.670493 -109.973114 0.429167 0.137040 5.283087 0.008463 -58.687500 -58.968751 ... 58.1708 318.833 359.481 0.1144 0.1144 0.28 563.179 2908.18 4395.65 594.713
20 0.002761 14.364348 -3.271736 -35.132501 0.865470 0.012060 13.059262 0.008355 -65.562500 -60.072918 ... 108.5820 431.860 375.003 0.1144 0.1144 0.28 1224.650 4343.15 6586.90 897.120
21 NaN 26.865000 -3.413867 21.707500 0.057228 0.039110 13.153143 0.008365 -53.625000 -49.395833 ... 53.8906 401.338 236.579 0.1144 0.1144 0.28 259.984 2191.18 2919.90 337.204
22 0.029513 11.278333 1.914367 -14.565625 1.347392 0.014825 9.883427 0.008283 -61.625000 -58.177083 ... 101.0730 271.814 216.559 0.1144 0.1144 0.28 495.219 1981.93 2523.92 282.635
23 0.026536 97.820000 -1.814114 -62.933748 0.130357 0.130905 4.988785 0.008490 -45.687500 -47.020835 ... 156.8360 573.602 349.735 0.1144 0.1144 0.28 689.158 5815.49 7751.99 911.016
24 0.055722 99.903333 -0.415791 -7.551875 0.153571 0.174365 3.404842 0.008437 -49.781250 -49.822918 ... 101.2050 429.458 221.562 0.1144 0.1144 0.28 431.495 3713.59 5634.23 743.050
25 NaN NaN 1.969520 18.666874 0.047947 0.008200 21.124605 0.008272 -62.593750 -57.140625 ... 83.2446 357.271 303.503 0.1144 0.1144 0.28 1041.950 2976.64 5078.05 797.409
26 0.118105 27.653750 0.041172 1.560000 0.303822 0.084910 2.706083 0.008438 -53.125000 -55.000001 ... 111.8980 311.052 431.619 0.1144 0.1144 0.28 230.260 1633.31 1545.42 126.959
27 0.199393 151.493333 4.582179 -19.218750 0.130826 0.101300 4.448990 0.008416 -45.500004 -46.447918 ... 119.8840 645.467 483.382 0.1144 0.1144 0.28 997.979 4784.66 6412.74 784.050
28 0.008947 9.046000 -1.535927 -5.978125 1.555833 0.009135 10.680760 0.008242 -61.500000 -57.479167 ... 64.2701 205.679 309.872 0.1144 0.1144 0.28 580.010 1550.39 2121.25 261.250
29 0.000000 345.865000 -0.454242 -11.633750 0.218182 0.025180 3.017725 0.007870 -50.937500 -51.093750 ... 76.7071 184.727 185.021 0.1144 0.1144 0.28 359.439 1687.70 1918.63 198.652
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
43 NaN NaN 0.577920 -5.084375 0.013540 0.026910 NaN 0.008657 -48.968750 NaN ... 106.6800 360.703 298.355 0.1144 0.1144 0.28 787.169 4509.98 6474.43 810.754
44 0.043698 72.699583 0.538024 -18.366251 0.201667 0.097005 3.090128 0.008339 -49.187500 -49.291669 ... 150.3550 365.279 345.030 0.1144 0.1144 0.28 642.334 2989.80 3736.31 405.028
45 NaN 6.745000 0.432242 14.523125 0.226853 0.011095 NaN 0.007961 -64.281258 NaN ... 97.0178 302.757 212.795 0.1144 0.1144 0.28 331.921 2357.50 3219.24 387.242
46 0.006478 15.225846 2.326974 -38.090624 0.269417 0.011950 2.206965 0.004187 -65.437500 -60.437500 ... 61.6860 215.758 261.862 0.1144 0.1144 0.28 111.175 1353.24 1416.64 128.295
47 0.036117 59.855312 0.277426 -5.624375 0.259244 0.172020 1.760542 0.008347 -53.468754 -52.906251 ... 109.7870 405.090 453.303 0.1144 0.1144 0.28 463.999 2354.60 4070.90 640.349
48 0.150682 117.956667 0.016911 14.493750 0.101095 0.103155 6.139455 0.008592 -45.937500 -44.375001 ... 166.5090 488.831 619.679 0.1144 0.1144 0.28 667.151 7270.68 11070.80 1491.810
49 NaN 47.555000 -0.130235 18.617498 0.042223 0.063035 3.421732 0.008332 -48.500000 -48.875000 ... 104.3840 571.657 300.714 0.1144 0.1144 0.28 566.582 2762.84 4220.03 595.443
50 0.113163 57.697500 1.808235 -21.385000 0.191071 0.131360 4.139913 0.008687 -57.531254 -58.697918 ... 121.8740 534.934 431.402 0.1144 0.1144 0.28 779.430 5113.31 7695.98 1017.170
51 0.030463 57.286250 2.242934 93.904995 0.220765 0.150235 3.113553 0.008342 -49.718754 -49.458335 ... 132.2040 530.611 365.551 0.1144 0.1144 0.28 767.462 3863.16 5524.98 747.990
52 NaN 144.600000 -2.898555 -0.890625 0.154754 0.069225 3.487732 0.008377 -50.125004 -51.458335 ... 100.3980 574.155 550.493 0.1144 0.1144 0.28 710.955 4275.76 6747.58 920.177
53 0.055158 128.938333 4.703926 -33.551249 0.156709 0.231750 3.356398 0.008716 -48.187500 -51.145833 ... 112.9490 632.925 451.745 0.1144 0.1144 0.28 792.070 4710.91 7778.30 1153.620
54 0.001311 86.009091 3.557742 -10.295625 0.214309 0.147125 6.046665 0.008459 -51.718750 -54.458333 ... 40.5782 136.794 192.993 0.1144 0.1144 0.28 424.367 1864.46 2251.61 234.500
55 0.004441 44.726190 -0.504202 -12.593124 0.338719 0.232495 4.802973 0.008442 -52.812500 -53.989585 ... 59.3600 314.442 290.198 0.1144 0.1144 0.28 504.651 3685.02 4460.71 473.176
56 NaN 86.730000 3.818755 6.264375 0.110261 0.054055 2.582140 0.008377 -39.437504 -46.156250 ... 45.1954 282.339 225.427 0.1144 0.1144 0.28 499.266 2815.13 2800.95 240.332
57 0.002114 13.683538 -1.634016 -2.520000 0.853677 0.071360 7.375193 0.008244 -55.968750 -55.406250 ... 48.3767 261.061 255.798 0.1144 0.1144 0.28 597.851 3175.09 4307.09 511.537
58 0.001659 11.203977 -0.901136 0.000000 1.058386 0.024495 6.637177 0.008140 -62.906250 -60.859375 ... 174.1690 294.809 201.585 0.1144 0.1144 0.28 658.863 1510.83 2536.94 408.050
59 0.120438 116.592500 2.468208 -5.702500 0.180874 0.073685 6.673720 0.008421 -47.500000 -48.510417 ... 176.3970 392.506 388.156 0.1144 0.1144 0.28 542.344 3145.56 5292.90 772.192
60 0.024170 69.278846 2.004471 3.445625 0.203064 0.182725 3.169958 0.008454 -49.187500 -48.640625 ... 140.6640 384.155 356.699 0.1144 0.1144 0.28 372.267 4772.48 6802.16 871.296
61 0.109119 95.111000 1.046015 -40.757498 0.202751 0.100890 5.640913 0.008352 -41.906250 -41.958333 ... 95.9885 906.284 284.149 0.1144 0.1144 0.28 456.167 3916.43 5944.94 824.895
62 NaN NaN -2.600230 15.493750 0.029064 0.044185 8.889285 0.008233 -56.781250 -51.234375 ... 102.9030 338.166 371.800 0.1144 0.1144 0.28 808.314 4189.11 6288.68 833.916
63 0.036174 39.774474 2.389944 -0.356250 0.275206 0.073520 0.670755 0.004553 -56.218750 -55.015625 ... 41.1600 136.136 242.185 0.1144 0.1144 0.28 673.741 1005.78 1148.93 112.756
64 0.030534 9.653333 -0.285056 -30.427501 0.364712 0.019310 11.732247 0.008331 -55.906250 -45.302085 ... 71.3384 315.172 199.628 0.1144 0.1144 0.28 416.010 1795.34 1938.07 185.072
65 0.032048 63.995357 -2.155722 17.658123 0.170000 0.108430 4.455838 0.008447 -50.718750 -50.937503 ... 120.0940 337.231 298.766 0.1144 0.1144 0.28 401.974 2451.05 2067.72 150.435
66 0.042162 79.650909 -4.080432 -78.186249 0.219318 0.246045 3.881518 0.008410 -52.531250 -50.416667 ... 80.7805 388.731 283.092 0.1144 0.1144 0.28 474.195 3600.33 5181.25 705.483
67 0.009289 60.184333 -1.952525 2.371250 0.221328 0.451480 5.221863 0.008523 -50.437500 -50.197917 ... 178.9760 233.516 395.839 0.1144 0.1144 0.28 424.469 2681.33 4482.73 659.123
68 0.002545 8.300889 -0.956501 2.671250 1.297212 0.009550 11.556347 0.008419 -65.281250 -57.270833 ... 82.7243 464.235 267.696 0.1144 0.1144 0.28 618.570 2481.58 2825.76 283.834
69 NaN 225.130000 -0.183169 26.153125 0.118851 0.150680 3.267555 0.008625 -46.531254 -47.395833 ... 82.7336 319.176 577.148 0.1144 0.1144 0.28 784.406 5337.68 7817.47 1007.680
70 0.010660 50.912222 -1.016410 -73.090617 0.387015 0.223855 2.850470 0.008127 -58.593750 -56.635419 ... 82.0400 263.407 422.708 0.1144 0.1144 0.28 438.741 3191.21 4265.51 535.470
71 0.051146 41.763913 -0.245698 -16.136250 0.382184 0.113045 1.391870 0.004919 -60.031250 -57.916667 ... 86.2400 269.770 285.657 0.1144 0.1144 0.28 329.652 2789.51 4161.64 565.034
72 0.016399 29.422424 -0.602468 -8.445624 0.470219 0.072620 1.983080 0.008490 -51.781254 -53.687500 ... 99.2967 424.087 269.298 0.1144 0.1144 0.28 323.629 2103.66 2286.05 218.921

73 rows × 83 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 [11]:
from allensdk.ephys.feature_extractor import EphysFeatureExtractor

sweep_number = 35

sweep_data = data_set.get_sweep(sweep_number)
i = sweep_data["stimulus"] # in A
v = sweep_data["response"] # 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 [12]:
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(feature_data["spikes"][0])
{ 'downstroke': -114.58815026547136,
  'downstroke_i': 210.0,
  'downstroke_idx': 205802,
  'downstroke_t': 1.02901,
  'downstroke_v': -7.9375001774169549,
  'f_fast_ahp': -55.500001240521698,
  'f_fast_ahp_i': 210.0,
  'f_fast_ahp_t': 1.0306000000000002,
  'f_fast_ahp_v': -55.500001240521698,
  'f_peak': 18.156250405823823,
  'f_peak_i': 210.0,
  'f_peak_t': 1.02868,
  'f_slow_ahp': -49.812501113396166,
  'f_slow_ahp_t': 1.0336800000000002,
  'f_slow_ahp_time': 0.40799673602611697,
  'f_trough': -55.500001240521698,
  '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.584564019287804,
  'threshold': -40.781250911531991,
  'threshold_i': 210.0,
  'threshold_idx': 205622,
  'threshold_t': 1.0281100000000001,
  'threshold_v': -40.781250911531991,
  'trough_i': 210.0,
  'trough_idx': 206120,
  'trough_t': 1.0306000000000002,
  'trough_v': -55.500001240521698,
  'upstroke': 216.04000901008584,
  'upstroke_i': 210.0,
  'upstroke_idx': 205690,
  'upstroke_t': 1.0284500000000001,
  'upstroke_v': -9.9375002221204394,
  'width': 0.69500000000011219}

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

In [13]:
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 [14]:
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[14]:
(0.9, 1.2)