Skip to content

Instantly share code, notes, and snippets.

@dyf
dyf / structures_rgb.py
Created January 3, 2017 17:43
convert the color_hex_triplet values in mouse connectivity's structures.csv to 8-bit RGB values
import csv
def hex_str_to_rgb(hex_str):
""" convert a hex string (e.g. "FFFFFF") to an RGB dictionary """
val = int(hex_str, 16)
return {
'r': val & 0x0000ff,
'g': (val & 0x00ff00) >> 8,
'b': val >> 16
}
@dyf
dyf / download_aging_ish.py
Last active January 6, 2017 01:01
Download Aging ISH Images
from allensdk.config import enable_console_log
from allensdk.api.queries.rma_api import RmaApi
from allensdk.api.queries.image_download_api import ImageDownloadApi
# print download messages to your console
enable_console_log()
# instantiate api classes for downloading metadata/images
image_api = ImageDownloadApi()
rma_api = RmaApi()
@dyf
dyf / download_ccf.py
Created March 29, 2017 23:24
download latest CCF
from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache
# pick from 10, 25, 50, 100 for resolution
mcc = MouseConnectivityCache(manifest_file='mcc/manifest.json', resolution=10)
# careful, these will get read into memory
av, _ = mcc.get_annotation_volume()
tv, _ = mcc.get_template_volume()
@dyf
dyf / ontology_fun.py
Created April 14, 2017 01:14
parsing ontologies
import json
from allensdk.core.structure_tree import StructureTree
file_name = "C:/Users/davidf/Downloads/mouse-brain-ontology.json"
structures = json.load(open(file_name,'r'))
# workaround for a bug in StructureTree.clean_structures that I just found
for s in structures:
s['id'] = int(s['id'])
s['structure_sets'] = []
@dyf
dyf / jupyter_gif.py
Created April 14, 2017 03:49
jupyter gif
import requests
import shutil
from IPython.display import HTML
url = 'https://media.giphy.com/media/demgpwJ6rs2DS/giphy.gif'
file_name = 'img.gif'
# download
response = requests.get(url, stream=True)
with open(file_name, 'wb') as out_file:
@dyf
dyf / load_obs_analysis.py
Created June 15, 2017 16:55
create stimulus analysis objects from NWB + analysis file
from allensdk.brain_observatory.locally_sparse_noise import LocallySparseNoise
from allensdk.core.brain_observatory_cache import BrainObservatoryCache
import allensdk.brain_observatory.stimulus_info as stiminfo
experiment_id = 12345 # dummy id
analysis_file = '/path/to/analysis.h5'
boc = BrainObservatoryCache(manifest_file='boc/manifest.json')
data_set = boc.get_ophys_experiment_data(experiment_id)
@dyf
dyf / tagged_containers.py
Created June 21, 2017 21:48
find tagged experiment containers
from allensdk.core.brain_observatory_cache import BrainObservatoryCache
boc = BrainObservatoryCache(manifest_file='boc/manifest.json')
containers = boc.get_experiment_containers(include_failed=True)
tagged_containers = [ c for c in containers if c['tags'] ]
for tc in tagged_containers:
print(tc['id'], tc['tags'])
@dyf
dyf / find_eye_tracking_experiments.py
Created August 15, 2017 17:47
find eye tracking experiments
from allensdk.core.brain_observatory_cache import BrainObservatoryCache
from allensdk.config import enable_console_log
from allensdk.api.queries.rma_api import RmaApi
import matplotlib.pyplot as plt
# print out API queries
enable_console_log()
# download experiments with passing eye tracking
results = RmaApi().model_query('OphysExperiment',
@dyf
dyf / nrrd_uncompressed.py
Created August 24, 2017 02:41
save nrrd uncompressed
from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache
import nrrd
mcc = MouseConnectivityCache(manifest_file="/data/dynamic-brain-workshop/mouse_connectivity_cache/mouse_connectivity_manifest.json")
exps = mcc.get_experiments()
pd, _ = mcc.get_projection_density(exps[0]['id'])
nrrd.write('some_file.nrrd', pd, options={ 'encoding': 'raw' })
@dyf
dyf / read_swc.rb
Last active November 27, 2017 16:34
ruby read SWC
require 'csv'
def read_swc(file_path)
rows = CSV.read(file_path, col_sep: ' ')
good_rows = rows.select { |row| not row[0].starts_with?('#') }
return good_rows.collect do |row|
{
'id' => row[0],