def main(): args = parse_arguments() logging.getLogger().setLevel(args.log_level) glif_api = None if (args.neuron_config_file is None or args.sweeps_file is None or args.ephys_file is None): assert args.neuronal_model_id is not None, Exception( "A neuronal model id is required if no neuron config file, sweeps file, or ephys data file is provided." ) glif_api = GlifApi() glif_api.get_neuronal_model(args.neuronal_model_id) if args.neuron_config_file: neuron_config = json_utilities.read(args.neuron_config_file) else: neuron_config = glif_api.get_neuron_config() if args.sweeps_file: sweeps = json_utilities.read(args.sweeps_file) else: sweeps = glif_api.get_ephys_sweeps() if args.ephys_file: ephys_file = args.ephys_file else: ephys_file = 'stimulus_%d.nwb' % args.neuronal_model_id if not os.path.exists(ephys_file): logging.info("Downloading stimulus to %s." % ephys_file) glif_api.cache_stimulus_file(ephys_file) else: logging.warning("Reusing %s because it already exists." % ephys_file) if args.output_ephys_file: output_ephys_file = args.output_ephys_file else: logging.warning( "Overwriting input file data with simulated data in place.") output_ephys_file = ephys_file neuron = GlifNeuron.from_dict(neuron_config) # filter out test sweeps sweep_numbers = [ s['sweep_number'] for s in sweeps if s['stimulus_name'] != 'Test' ] simulate_neuron(neuron, sweep_numbers, ephys_file, output_ephys_file, args.spike_cut_value)
def main(): args = parse_arguments() logging.getLogger().setLevel(args.log_level) glif_api = None if (args.neuron_config_file is None or args.sweeps_file is None or args.ephys_file is None): assert args.neuronal_model_id is not None, Exception("A neuronal model id is required if no neuron config file, sweeps file, or ephys data file is provided.") glif_api = GlifApi() glif_api.get_neuronal_model(args.neuronal_model_id) if args.neuron_config_file: neuron_config = json_utilities.read(args.neuron_config_file) else: neuron_config = glif_api.get_neuron_config() if args.sweeps_file: sweeps = json_utilities.read(args.sweeps_file) else: sweeps = glif_api.get_ephys_sweeps() if args.ephys_file: ephys_file = args.ephys_file else: ephys_file = 'stimulus_%d.nwb' % args.neuronal_model_id if not os.path.exists(ephys_file): logging.info("Downloading stimulus to %s." % ephys_file) glif_api.cache_stimulus_file(ephys_file) else: logging.warning("Reusing %s because it already exists." % ephys_file) if args.output_ephys_file: output_ephys_file = args.output_ephys_file else: logging.warning("Overwriting input file data with simulated data in place.") output_ephys_file = ephys_file neuron = GlifNeuron.from_dict(neuron_config) # filter out test sweeps sweep_numbers = [ s['sweep_number'] for s in sweeps if s['stimulus_name'] != 'Test' ] simulate_neuron(neuron, sweep_numbers, ephys_file, output_ephys_file, args.spike_cut_value)
def test_download(): if os.path.exists(OUTPUT_DIR): shutil.rmtree(OUTPUT_DIR) os.makedirs(OUTPUT_DIR) glif_api = GlifApi() glif_api.get_neuronal_model(NEURONAL_MODEL_ID) glif_api.cache_stimulus_file(os.path.join(OUTPUT_DIR, '%d.nwb' % NEURONAL_MODEL_ID)) neuron_config = glif_api.get_neuron_config() json_utilities.write(os.path.join(OUTPUT_DIR, '%d_neuron_config.json' % NEURONAL_MODEL_ID), neuron_config) ephys_sweeps = glif_api.get_ephys_sweeps() json_utilities.write(os.path.join(OUTPUT_DIR, 'ephys_sweeps.json'), ephys_sweeps)
pretty = pretty.replace('None', 'null') pretty = pretty.replace('False', 'false') pretty = pretty.replace('True', 'true') f.write(pretty) f.close() random.seed(123) models_to_use = random.sample(models, max) extras = [472308324] for model in models: if model['id'] in extras: models_to_use.append(model) for model in models_to_use: model_id = str(model['id']) print("\n=====================================") print("Model %s: %s" % (model_id, model['name'])) info = api.get_neuronal_model(model['id']) write_to_file(model_id, 'ephys_sweeps.json', info['ephys_sweeps']) write_to_file(model_id, 'model_metadata.json', info['neuronal_model']) api.get_neuron_config(output_file_name='%s/neuron_config.json' % model_id) print("Done with %i models" % len(models))
from allensdk.api.queries.glif_api import GlifApi import allensdk.core.json_utilities as json_utilities neuronal_model_id = 472423251 glif_api = GlifApi() glif_api.get_neuronal_model(neuronal_model_id) glif_api.cache_stimulus_file('stimulus.nwb') neuron_config = glif_api.get_neuron_config() json_utilities.write('neuron_config.json', neuron_config) ephys_sweeps = glif_api.get_ephys_sweeps() json_utilities.write('ephys_sweeps.json', ephys_sweeps)
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))