from allensdk.api.queries.glif_api import GlifApi import random import pprint import os pp = pprint.PrettyPrinter(indent=4) api = GlifApi() models = api.list_neuronal_models() max = 5 def write_to_file(directory, file_name, jstring): file_path = '%s/%s' % (directory, file_name) print("Writing to: %s" % file_path) if not os.path.isdir(directory): os.mkdir(directory) f = open(file_path, 'w') #info = str(jstring) pretty = pp.pformat(jstring) pretty = pretty.replace('\'', '"') pretty = pretty.replace('u"', '"') pretty = pretty.replace('None', 'null') pretty = pretty.replace('False', 'false') pretty = pretty.replace('True', 'true') f.write(pretty) f.close()
def download_glif_models(cell_ids, base_dir, incl_ephys=True, force_overwrite=False): """Goes through the list of cell_ids and downloads cell config and ephys data in base_dir/cell_<ID>. Then looks up all possible models and downloads model files int base_dir/cell_<ID>/<MODEL_TYPE>_<MODEL_ID>/ """ # Determine the best url for connecting to cell-types db try: # see if we can connect to interal cell-types db request = requests.get('http://icelltypes/') if request.status_code == 200: base_uri = 'http://icelltypes/' else: base_uri = None except Exception: base_uri = None # use the default url base_dir = base_dir if base_dir.endswith('/') else base_dir + '/' valid_cells = [] ct_api = CellTypesApi(base_uri) for cell in ct_api.list_cells(): if cell['id'] in cell_ids: # create directory for cell cell_home = '{}cell_{}/'.format(base_dir, cell['id']) if not os.path.exists(cell_home): os.makedirs(cell_home) # save metadata cell_metadata_file = cell_home + 'cell_metadata.json' if force_overwrite or not os.path.exists(cell_metadata_file): print('Saving metadata for cell {} in {}'.format( cell['id'], cell_metadata_file)) json_utilities.write(cell_metadata_file, cell) else: print('File {} already exists. Skipping'.format( cell_metadata_file)) # save ephys data if incl_ephys: cell_ephys_file = cell_home + 'ephys_data.nwb' if force_overwrite or not os.path.exists(cell_ephys_file): print('Saving ephys data for cell {} in {}'.format( cell['id'], cell_ephys_file)) ct_api.save_ephys_data(cell['id'], cell_ephys_file) else: print('File {} already exists. Skipping'.format( cell_ephys_file)) # save sweeps file sweeps_file = cell_home + 'ephys_sweeps.json' if force_overwrite or not os.path.exists(sweeps_file): print('- Saving sweeps file to {}'.format(sweeps_file)) ephys_sweeps = ct_api.get_ephys_sweeps(cell['id']) json_utilities.write(sweeps_file, ephys_sweeps) else: print('- File {} already exits. Skipping.'.format(sweeps_file)) # keep track of valid ids valid_cells.append(cell['id']) cell_ids.remove(cell['id']) for cid in cell_ids: print('Warning: cell #{} was not found in cell-types database'.format( cid)) # Iterate through each all available models and find ones correspoding to cell list glif_models = {} # map model-id to their directory glif_api = GlifApi(base_uri=base_uri) for model in glif_api.list_neuronal_models(): if model['specimen_id'] in valid_cells: # save model files <BASE_DIR>/cell_<CELL_ID>/<MODEL_TYPE>_<MODEL_ID>/ cell_id = model['specimen_id'] model_id = model['id'] model_type = model[ 'neuronal_model_template_id'] #['id'] # type of model, GLIF-LIF, GLIF-ASC, etc type_name = model_id2name.get(model_type, None) if type_name is None: print( 'Warning: Unknown model type {} ({}) for cell/model {}/{}'. format(model_type, model['neuronal_model_template']['name'], cell_id, model_id)) type_name = model_type model_home_dir = '{}cell_{}/{}_{}/'.format(base_dir, cell_id, type_name, model_id) glif_models[model_id] = model_home_dir # go through all the found models, download necessary files n_models = len(glif_models) for i, (gid, home_dir) in enumerate(glif_models.iteritems()): print('Processing model {} ({} of {})'.format(gid, (i + 1), n_models)) model_metadata = glif_api.get_neuronal_model(gid) if not os.path.exists(home_dir): os.makedirs(home_dir) # save model metadata metadata_file = home_dir + 'metadata.json' if force_overwrite or not os.path.exists(metadata_file): print('- Saving metadata file to {}'.format(metadata_file)) #print type(metadata_file) with open(metadata_file, 'wb') as fp: json.dump(model_metadata, fp, indent=2) else: print('- File {} already exits. Skipping.'.format(metadata_file)) # get neuron configuration file config_file = home_dir + 'config.json' if force_overwrite or not os.path.exists(config_file): print('- Saving configuration file to {}'.format(config_file)) neuron_config = glif_api.get_neuron_config() json_utilities.write(config_file, neuron_config) else: print('- File {} already exits. Skipping.'.format(config_file))