Jump To …

download_data.py

Copyright 2013 Allen Institute for Brain Science Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Download the spatial search lines emanating from the injection site and the coordinates of grid voxels inside of the injection site mask. Output all of this to a json file.

import api
import numpy as np
import json

DATA_SET_ID = 156394513
DENSITY_RANGE = [0.04, 0.3]
INJECTION_MASK_THRESHOLD = 0.5

Download the spatial search lines for a data set. This is done by searching the density volume for that data set for voxels within a range of density values. A relatively low range is chosen by default to avoid fiber tracts. Voxels in the fiber tract annotation mask are skipped for the same reason.

def DownloadLines(dataSetId, densityRange):
    print "downloading density volume"
    densityHeader, densityArr, densityMeta = api.DownloadDataSetVolume(dataSetId, 'density')
    densitySpacing = np.array(densityMeta['ElementSpacing'])

    print "downloading fiber tract volume"
    ftHeader, ftArr, ftMeta = api.DownloadFiberTractVolume()
    ftSpacing = np.array(ftMeta['ElementSpacing'])

    

The fiber tract annotation volume has 25um spacing vs 100um for the density volume. Converting from the coordinates of one to the other requires a simple scale factor.

    ftScale = densitySpacing / ftSpacing.astype(np.float32)  
    ftDims = ftArr.shape
    

Find the voxel indices with density within the specified density range.

    indices = np.argwhere((densityArr >= densityRange[0]) & (densityArr <= densityRange[1]))

    dataSetLines = []
    
    for index in indices:

Convert the density volume voxel index to fiber tract volume coordinates.

        ftIndex = np.array(index * ftScale, dtype=np.int64)
        try:
            ftVal = ftArr[ftIndex[0], ftIndex[1], ftIndex[2]]
        except IndexError as e:
            print index, "outside fiber tract mask" 
            continue

If the mask value is 0, the voxel is not in a fiber tract. Convert to micron units and download the path from the injection site to this voxel.

        if ftVal == 0:
            coord = index * densitySpacing
            lines = api.DownloadTargetLines(coord, dataSetId)
            dataSetLines += lines
        else:
            print index, "inside fiber tract mask"

    return dataSetLines

Download the coordinates of voxels within the injection site.

def DownloadInjectionCoordinates(dataSetId, injectionMaskThreshold):
    print "downloading injection mask coordinates"
    header, arr, meta = api.DownloadDataSetVolume(dataSetId, 'injection')

convert from 100um image coordinates to 1um coordinates.

    spacing = np.array(meta['ElementSpacing'])
    coords = np.argwhere(arr > injectionMaskThreshold) * spacing
    
    return coords.tolist()

if __name__ == "__main__":

    data = {
        "lines": DownloadLines(DATA_SET_ID, DENSITY_RANGE),
        "injectionCoordinates": DownloadInjectionCoordinates(DATA_SET_ID, INJECTION_MASK_THRESHOLD)
    }

    with open('data.json', 'wb') as f:
        f.write(json.dumps(data))