In [12]:
import os
from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache
import numpy as np
import h5py
import time
import nrrd
from voxnet.conn2d import map_to_surface

drive_path = os.path.join(os.getenv('HOME'), 'work/allen/data/sdk_new_100')

# When downloading 3D connectivity data volumes, what resolution do you want (in microns)?  
# Options are: 10, 25, 50, 100
resolution_um = 10

# Drop list criterion, in percent difference
volume_fraction = 20

# The manifest file is a simple JSON file that keeps track of all of
# the data that has already been downloaded onto the hard drives.
# If you supply a relative path, it is assumed to be relative to your
# current working directory.
manifest_file = os.path.join(drive_path, "manifest.json")

# Start processing data
mcc = MouseConnectivityCache(manifest_file = manifest_file,
                             resolution = resolution_um)
ontology = mcc.get_ontology()
# Injection structure of interest
isocortex = ontology['Isocortex']

# open up a pandas dataframe of all of the experiments
experiments = mcc.get_experiments(dataframe = True, 
                                  injection_structure_ids = [isocortex['id'].values[0]], 
                                  cre = False)
print "%d total experiments" % len(experiments)

view_paths_fn = os.path.join(os.getenv('HOME'), 'work/allen/data/TopView/top_view_paths_10.h5')
view_paths_file = h5py.File(view_paths_fn, 'r')
view_lut = view_paths_file['view lookup'][:]
view_paths = view_paths_file['paths'][:]
view_paths_file.close()

## Compute size of each path to convert path averages to sums
norm_lut = np.zeros(view_lut.shape, dtype=int)
ind = np.where(view_lut != -1)
ind = zip(ind[0], ind[1])
for curr_ind in ind:
    curr_path_id = view_lut[curr_ind]
    curr_path = view_paths[curr_path_id, :]
    norm_lut[curr_ind] = np.sum(curr_path > 0)

t0 = time.time()
expt_drop_list = []
full_vols = []
flat_vols = []


/home/kamdh/anaconda2/lib/python2.7/site-packages/ipykernel_launcher.py:27: VisibleDeprecationWarning: Function get_ontology is deprecated. Use get_structure_tree instead.
126 total experiments

In [13]:
eid = experiments.iloc[5].id
row = experiments.iloc[5]
print eid
print row


180916954
gender                                                                   M
id                                                               180916954
injection-coordinates                                   [4800, 1220, 5930]
injection-structures     [{u'abbreviation': u'ACAd', u'color': u'40A666...
product-id                                                               5
strain                                                            C57BL/6J
structure-abbrev                                                       MOs
structure-color                                                     1F9D5A
structure-id                                                           993
structure-name                                        Secondary motor area
transgenic-line                                                           
Name: 180916954, dtype: object

In [14]:
data_dir = os.path.join(os.getenv('HOME'),
                            "work/allen/data/sdk_new_100/experiment_%d/" % eid)
    # get and remap injection data
    print "getting injection density"
    in_d, in_info = mcc.get_injection_density(eid)
    print "mapping to surface"
    in_d_s = map_to_surface(in_d, view_lut, view_paths, scale = resolution_um/10., fun=np.mean)
    flat_vol = np.nansum(in_d_s * norm_lut) * (10./1000.)**3
    flat_vols.append(flat_vol)
    full_vol = np.nansum(in_d) * (10./1000.)**3
    full_vols.append(full_vol)


getting injection density
mapping to surface

In [15]:
expt_union = mcc.get_experiment_structure_unionizes(eid, hemisphere_ids = [2], is_injection = True,
                                                        structure_ids = [ontology['grey']['id'].values[0]])

In [16]:
flat_vols


Out[16]:
[0.89908089262803303]

In [17]:
full_vols


Out[17]:
[0.51017865625000014]

In [18]:
full_vol2 = float(expt_union['projection_volume'])
print expt_union
print full_vol2


      hemisphere_id         id  is_injection  max_voxel_density  max_voxel_x  \
2206              2  533667735          True                1.0         4800   

      max_voxel_y  max_voxel_z  normalized_projection_volume  \
2206         1220         5930                      0.991312   

      projection_density  projection_energy  projection_intensity  \
2206            0.999986            12435.6               12435.8   

      projection_volume  experiment_id  structure_id  sum_pixel_intensity  \
2206             0.5557      180916954             8         5.641290e+12   

       sum_pixels  sum_projection_pixel_intensity  sum_projection_pixels  \
2206  453639000.0                    5.641280e+12            453632000.0   

        volume  
2206  0.555708  
0.5557

In [19]:
print np.nansum(in_d_s * norm_lut)


899080.892628

In [20]:
in_d_sum = map_to_surface(in_d, view_lut, view_paths, scale = resolution_um/10., fun=np.sum)
np.nansum(in_d_sum) * (10./1000.)**3


Out[20]:
0.89908087500000011

In [21]:
print np.abs(flat_vol - full_vol)/full_vol*100
print np.abs(flat_vol - full_vol2)/full_vol2*100


76.2286370889
61.7924946244

In [23]:
import matplotlib.pyplot as plt
%matplotlib inline
tmp = norm_lut.astype(float)
tmp[tmp == 0] = np.nan

fig = plt.figure(figsize = (20,20))
ax = fig.add_subplot(121)
h = ax.imshow(in_d_s)
fig.colorbar(h)

a2 = fig.add_subplot(122)
h2 = a2.imshow(np.sum(in_d, axis=1))
fig.colorbar(h2)


Out[23]:
<matplotlib.colorbar.Colorbar at 0x7fd5ec53ff50>

In [20]:
experiments


Out[20]:
gender id injection-coordinates injection-structures product-id strain structure-abbrev structure-color structure-id structure-name transgenic-line
id
180435652 M 180435652 [7820, 4250, 9870] [{u'abbreviation': u'TEa', u'color': u'15B0B3'... 5 C57BL/6J ECT 0D9F91 895 Ectorhinal area
180436360 M 180436360 [4800, 4720, 8980] [{u'abbreviation': u'AId', u'color': u'219866'... 5 C57BL/6J VISC 11AD83 677 Visceral area
180719293 M 180719293 [3140, 3330, 7390] [{u'abbreviation': u'AId', u'color': u'219866'... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
180709942 M 180709942 [3360, 3120, 7520] [{u'abbreviation': u'MOp', u'color': u'1F9D5A'... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
180917660 M 180917660 [5570, 4540, 9540] [{u'abbreviation': u'AIp', u'color': u'219866'... 5 C57BL/6J VISC 11AD83 677 Visceral area
180916954 M 180916954 [4800, 1220, 5930] [{u'abbreviation': u'ACAd', u'color': u'40A666... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
100141780 M 100141780 [4070, 2600, 7500] [{u'abbreviation': u'MOp', u'color': u'1F9D5A'... 5 C57BL/6J MOp 1F9D5A 985 Primary motor area
180720175 M 180720175 [5710, 670, 6420] [{u'abbreviation': u'SSp-ll', u'color': u'1880... 5 C57BL/6J MOp 1F9D5A 985 Primary motor area
180717881 M 180717881 [4580, 3610, 8670] [{u'abbreviation': u'SSp-m', u'color': u'18806... 5 C57BL/6J SSp-m 188064 345 Primary somatosensory area, mouth
127084296 M 127084296 [4880, 1800, 6920] [{u'abbreviation': u'MOp', u'color': u'1F9D5A'... 5 C57BL/6J MOp 1F9D5A 985 Primary motor area
112306316 M 112306316 [3000, 3600, 6690] [{u'abbreviation': u'FRP', u'color': u'268F45'... 5 C57BL/6J ORBl 248A5E 723 Orbital area, lateral part
180709230 M 180709230 [2360, 3870, 7100] [{u'abbreviation': u'AId', u'color': u'219866'... 5 C57BL/6J ORBl 248A5E 723 Orbital area, lateral part
180074890 M 180074890 [6760, 4320, 9510] [{u'abbreviation': u'AIp', u'color': u'219866'... 5 C57BL/6J VISC 11AD83 677 Visceral area
112952510 M 112952510 [3430, 1930, 6140] [{u'abbreviation': u'ACAd', u'color': u'40A666... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
112596790 M 112596790 [3840, 4160, 8250] [{u'abbreviation': u'AId', u'color': u'219866'... 5 C57BL/6J AId 219866 104 Agranular insular area, dorsal part
112951804 M 112951804 [6890, 2260, 8670] [{u'abbreviation': u'SSp-bfd', u'color': u'188... 5 C57BL/6J SSp-bfd 188064 329 Primary somatosensory area, barrel field
141603190 M 141603190 [4100, 1810, 6110] [{u'abbreviation': u'ACAd', u'color': u'40A666... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
180718587 M 180718587 [5200, 1310, 7300] [{u'abbreviation': u'SSp-ll', u'color': u'1880... 5 C57BL/6J SSp-ul 188064 369 Primary somatosensory area, upper limb
113036264 M 113036264 [5600, 3510, 9100] [{u'abbreviation': u'SSp-m', u'color': u'18806... 5 C57BL/6J SSs 188064 378 Supplemental somatosensory area
100141796 M 100141796 [9370, 2450, 9080] [{u'abbreviation': u'VISl', u'color': u'08858C... 5 C57BL/6J VISli 08858C 312782574 Laterointermediate area
112514915 M 112514915 [6600, 3010, 9160] [{u'abbreviation': u'SSp-bfd', u'color': u'188... 5 C57BL/6J SSs 188064 378 Supplemental somatosensory area
146593590 M 146593590 [5180, 1790, 5800] [{u'abbreviation': u'ACAd', u'color': u'40A666... 5 C57BL/6J ACAd 40A666 39 Anterior cingulate area, dorsal part
180296424 M 180296424 [9570, 1750, 8510] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
117298988 M 117298988 [6140, 3530, 9340] [{u'abbreviation': u'SSs', u'color': u'188064'... 5 C57BL/6J SSs 188064 378 Supplemental somatosensory area
170721670 M 170721670 [2410, 3150, 6820] [{u'abbreviation': u'AOB', u'color': u'9DF0D2'... 5 C57BL/6J ORBl 248A5E 723 Orbital area, lateral part
157710335 M 157710335 [2440, 2750, 7050] [{u'abbreviation': u'FRP', u'color': u'268F45'... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
126907302 M 126907302 [7210, 1280, 8030] [{u'abbreviation': u'SSp-bfd', u'color': u'188... 5 C57BL/6J SSp-bfd 188064 329 Primary somatosensory area, barrel field
126861679 M 126861679 [7450, 910, 7080] [{u'abbreviation': u'VISam', u'color': u'08858... 5 C57BL/6J VISam 08858C 394 Anteromedial visual area
127089669 M 127089669 [9270, 2930, 9310] [{u'abbreviation': u'SUB', u'color': u'4FC244'... 5 C57BL/6J VISpor 08858C 312782628 Postrhinal area
141602484 M 141602484 [4030, 1830, 7090] [{u'abbreviation': u'MOp', u'color': u'1F9D5A'... 5 C57BL/6J MOs 1F9D5A 993 Secondary motor area
... ... ... ... ... ... ... ... ... ... ... ...
112595376 M 112595376 [8350, 1390, 6600] [{u'abbreviation': u'RSPd', u'color': u'1AA698... 5 C57BL/6J RSPv 1AA698 886 Retrosplenial area, ventral part
100141473 M 100141473 [7370, 2110, 9000] [{u'abbreviation': u'SSp-bfd', u'color': u'188... 5 C57BL/6J SSp-bfd 188064 329 Primary somatosensory area, barrel field
158435116 M 158435116 [2810, 3870, 6420] [{u'abbreviation': u'ORBl', u'color': u'248A5E... 5 C57BL/6J ORBvl 248A5E 746 Orbital area, ventrolateral part
126862385 M 126862385 [9640, 620, 7060] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
100149109 M 100149109 [7540, 2950, 9550] [{u'abbreviation': u'AUDp', u'color': u'019399... 5 C57BL/6J AUDp 019399 1002 Primary auditory area
139519496 M 139519496 [7660, 2720, 9530] [{u'abbreviation': u'SSs', u'color': u'188064'... 5 C57BL/6J AUDd 019399 1011 Dorsal auditory area
112229103 M 112229103 [7500, 770, 6790] [{u'abbreviation': u'VISam', u'color': u'08858... 5 C57BL/6J RSPagl 1AA698 894 Retrosplenial area, lateral agranular part
157062358 M 157062358 [9260, 2830, 9490] [{u'abbreviation': u'TEa', u'color': u'15B0B3'... 5 C57BL/6J VISpor 08858C 312782628 Postrhinal area
100141219 M 100141219 [8940, 1420, 7840] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
304565427 M 304565427 [9280, 2040, 8630] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
304586645 M 304586645 [8650, 1350, 8030] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
272916915 M 272916915 [8510, 760, 6570] [{u'abbreviation': u'RSPd', u'color': u'1AA698... 5 C57BL/6J RSPd 1AA698 879 Retrosplenial area, dorsal part
272782668 M 272782668 [9590, 950, 7230] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
112373124 M 112373124 [5420, 2600, 8480] [{u'abbreviation': u'SSp-m', u'color': u'18806... 5 C57BL/6J SSp-n 188064 353 Primary somatosensory area, nose
116903230 M 116903230 [7950, 3110, 9730] [{u'abbreviation': u'AUDp', u'color': u'019399... 5 C57BL/6J AUDp 019399 1002 Primary auditory area
100147853 M 100147853 [9290, 1520, 8230] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
112424813 M 112424813 [8030, 510, 6310] [{u'abbreviation': u'RSPd', u'color': u'1AA698... 5 C57BL/6J RSPd 1AA698 879 Retrosplenial area, dorsal part
174361040 M 174361040 [9850, 850, 7110] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
277714322 M 277714322 [8230, 1130, 8130] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
180403712 M 180403712 [7470, 4370, 9850] [{u'abbreviation': u'TEa', u'color': u'15B0B3'... 5 C57BL/6J ECT 0D9F91 895 Ectorhinal area
277616630 M 277616630 [8510, 1040, 7910] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
277713580 M 277713580 [8130, 1110, 8120] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
277712166 M 277712166 [8350, 1190, 8110] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
180073473 M 180073473 [8400, 2500, 9540] [{u'abbreviation': u'TEa', u'color': u'15B0B3'... 5 C57BL/6J AUDpo 019399 1027 Posterior auditory area
304564721 M 304564721 [8530, 1350, 8370] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
304585910 M 304585910 [9230, 1370, 8200] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
148964212 M 148964212 [5760, 1440, 7630] [{u'abbreviation': u'SSp-ll', u'color': u'1880... 5 C57BL/6J SSp-ul 188064 369 Primary somatosensory area, upper limb
272916202 M 272916202 [9750, 2070, 8980] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISpl 08858C 425 Posterolateral visual area
304762965 M 304762965 [8840, 1410, 8330] [{u'abbreviation': u'VISp', u'color': u'08858C... 5 C57BL/6J VISp 08858C 385 Primary visual area
180404418 M 180404418 [5190, 4350, 9320] [{u'abbreviation': u'SSs', u'color': u'188064'... 5 C57BL/6J GU 009C75 1057 Gustatory areas

126 rows × 11 columns


In [ ]: