def __init__(self, archive_dir=None): self.bp = BiophysicalApi('http://api.brain-map.org') self.bp.cache_stimulus = True # change to False to not download the large stimulus NWB file self.cta = CellTypesApi() self.rma = RmaApi() self.neuronal_model_download_endpoint = 'http://celltypes.brain-map.org/neuronal_model/download/' self.template_names = {} self.nwb_list = [] if archive_dir == None: archive_dir = '.' self.archive_dir = archive_dir
def GetAllenCells(neuronal_model_ids): from allensdk.api.queries.biophysical_api import BiophysicalApi bp = BiophysicalApi() bp.cache_stimulus = False # change to True to download the large stimulus NWB file for neuronal_model_id in neuronal_model_ids: os.system('mkdir models/' + str(neuronal_model_id)) bp.cache_data(neuronal_model_id, working_directory=str(neuronal_model_id)) os.system('cd ' + str(neuronal_model_id) + '; nrnivmodl ./modfiles; cd ../../')
def prepareModelRun(model_filename, ephys_filename, swc_filename, sweeps, sweeps_by_type): bp = BiophysicalApi() bp.create_manifest(fit_path=os.path.basename(model_filename), model_type='Biophysical - all active', stimulus_filename=os.path.basename(ephys_filename), swc_morphology_path=os.path.basename(swc_filename), sweeps=sweeps) bp.manifest['runs'][0]['sweeps_by_type'] = sweeps_by_type manifest_file = os.path.join(os.path.dirname(model_filename), 'manifest.json') with open(manifest_file, 'w') as manifest_json: json.dump(bp.manifest, manifest_json, indent=2) return manifest_file
def getCells(): from allensdk.api.queries.biophysical_api import BiophysicalApi bp = BiophysicalApi() bp.cache_stimulus = True # change to False to not download the large stimulus NWB file # # E2 cell (L2/3 Pyr) - https://senselab.med.yale.edu/modeldb/showmodel.cshtml?model=184161 neuronal_model_id = 472299294 # get this from the web site as above bp.cache_data(neuronal_model_id, working_directory='E2full') os.system('cd E2full; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'E2full/.')) os.system('cp %s %s' % ('E2/cell_utils.py', 'E2full/.')) # E4 cell (L4 Pyr) - https://senselab.med.yale.edu/modeldb/showmodel.cshtml?model=184142 neuronal_model_id = 329321704 # get this from the web site as above bp.cache_data(neuronal_model_id, working_directory='E4') os.system('cd E4; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'E4/.')) os.system('cp %s %s' % ('E2/cell_utils.py', 'E4/.')) # E5 cell (L5 Pyr) - https://senselab.med.yale.edu/modeldb/showmodel.cshtml?model=184159 neuronal_model_id = 471087975 # get this from the web site as above bp.cache_data(neuronal_model_id, working_directory='E5') os.system('cd E5; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'E5/.')) os.system('cp %s %s' % ('E2/cell_utils.py', 'E5/.')) # IF cell (L5 Parv) - https://senselab.med.yale.edu/modeldb/showmodel.cshtml?model=184152 neuronal_model_id = 471085845 # get this from the web site as above bp.cache_data(neuronal_model_id, working_directory='IF') os.system('cd IF; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'IF/.')) os.system('cp %s %s' % ('E2/cell_utils.py', 'IF/.')) # IL cell (L5 Sst) - https://senselab.med.yale.edu/modeldb/showmodel.cshtml?model=184163 neuronal_model_id = 472299363 # get this from the web site as above bp.cache_data(neuronal_model_id, working_directory='IL') os.system('cd IL; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'IL/.')) os.system('cp %s %s' % ('E2/cell_utils.py', 'IL/.')) # AA cell (all active model L5 Pyr) - http://celltypes.brain-map.org/experiment/electrophysiology/485574832 neuronal_model_id = 497232312 bp.cache_data(neuronal_model_id, working_directory='AA') os.system('cd AA; nrnivmodl ./modfiles; cd ..') os.system('cp %s %s' % ('E2/cell_template.hoc', 'AA/.')) os.system('nrnivmodl ./E2/modfiles')
def __init__(self, model_id, cache_stim=False): self.model_id = model_id self.bp = BiophysicalApi() self.model_dir = os.path.join(os.getcwd(), 'model_' + str(model_id), '') self.bp.cache_stimulus = cache_stim self.h = None self.utils = None self.description = None self.section_output = {} ############################ self.reference_sweep = None self.reference_output = None self.j_history = [] self.theta_history = [] self.theta = None self.theta_name = '' self.fit_section = None
def model_props(cell_id): cell_metadata = {} # download existing model (if exists) bp = BiophysicalApi() try: model_list = bp.get_neuronal_models(cell_id) model_dict = {key: '' for key in template_model_dict.keys()} for model_type, template_id in template_model_dict.items(): for model_meta in model_list: if model_meta['neuronal_model_template_id'] == template_id: model_dict[model_type] = model_meta['id'] except: logger.debug('No biophysical model available') api = allensdk.api.queries.rma_api.RmaApi() # Get the model metadata for model_type, model_id in model_dict.items(): if model_id != '': bp.cache_data(model_id, working_directory=model_dir[model_type]) model_metadata = api.model_query( "NeuronalModelRun", criteria="[neuronal_model_id$eq%s]" % model_id) model_metadata_select = [ model_metadata[0]['rheobase_feature_average'], model_metadata[0]['explained_variance_ratio'] ] model_path = glob.glob('%s/*fit*.json' % model_dir[model_type])[0] for file_ in glob.glob('%s/*' % model_dir[model_type]): if file_ != model_path: try: os.remove(file_) except: shutil.rmtree(file_, ignore_errors=True) cell_metadata['model_path_{}'.format( model_type)] = os.path.abspath(model_path) cell_metadata['{}_id'.format( model_dir[model_type])] = str(model_id) for i, metadata_key in enumerate(opt_metric_dict[model_type]): cell_metadata[metadata_key] = model_metadata_select[i] return cell_metadata
def test_biophysical(): neuronal_model_id = 472451419 # get this from the web site model_directory = '.' bp = BiophysicalApi('http://api.brain-map.org') bp.cache_stimulus = False # don't want to download the large stimulus NWB file bp.cache_data(neuronal_model_id, working_directory=model_directory) os.system('nrnivmodl modfiles') description = Config().load('manifest.json') utils = Utils(description) h = utils.h manifest = description.manifest morphology_path = manifest.get_path('MORPHOLOGY') utils.generate_morphology(morphology_path.encode('ascii', 'ignore')) utils.load_cell_parameters() stim = h.IClamp(h.soma[0](0.5)) stim.amp = 0.18 stim.delay = 1000.0 stim.dur = 1000.0 h.tstop = 3000.0 vec = utils.record_values() h.finitialize() h.run() output_path = 'output_voltage.dat' junction_potential = description.data['fitting'][0]['junction_potential'] mV = 1.0e-3 ms = 1.0e-3 output_data = (numpy.array(vec['v']) - junction_potential) * mV output_times = numpy.array(vec['t']) * ms DatUtilities.save_voltage(output_path, output_data, output_times) assert numpy.count_nonzero(output_data) > 0
s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): shutil.copytree(s, d, symlinks, ignore) else: shutil.copy2(s, d) #%% Pick a cell and a sweep to run from the available set of protocols cell_id = 468193142 # get this from the web site: http://celltypes.brain-map.org sweep_num = 46 # Select a Long Square sweep : 1s DC #%% Download all-acive model sdk_model_templates = {'all_active': 491455321, 'perisomatic': 329230710} bp = BiophysicalApi() bp.cache_stimulus = True model_list = bp.get_neuronal_models(cell_id, model_type_ids=[sdk_model_templates['all_active']]) # Only get the all-active model for the cell model_dict = model_list[0] model_dir = 'all_active_models' bp.cache_data(model_dict['id'], working_directory=model_dir) new_model_file = 'fit_parameters_new.json' shutil.copyfile(new_model_file, os.path.join(model_dir, new_model_file)) copytree('modfiles', os.path.join(model_dir, 'modfiles')) #%% Running the legacy all-active models os.chdir(model_dir) subprocess.check_call(['nrnivmodl', 'modfiles/']) manifest_file = 'manifest.json'
def biophys_api(): endpoint = 'http://twarehouse-backup' return BiophysicalApi(endpoint)
rma::criteria,[m__biophys_perisomatic$gt0],rma::criteria,[donor__species$il%27homo%20sapiens%27]' tree = ET.fromstring(urllib.urlopen(url_spec).read()) neurons = [] # here we go through all the results and ask for the neuronal-model for spec in tree.iter('specimen--id'): url_model = "http://api.brain-map.org/api/v2/data/query.xml?criteria=model::NeuronalModel,\ rma::critera,[specimen_id$eq" + spec.text + "],neuronal_model_template[name$il'*Bio*']" neurons.append(ET.fromstring(urllib.urlopen(url_model).read()).iter('id').next().text) print("Found {0} neurons").format(len(neurons)) base_dir = os.getcwd() working_directory = 'morphologies/ABI/' # where the model files will be downloaded bp = BiophysicalApi('http://api.brain-map.org') bp.cache_stimulus = False # set to True to download the large stimulus NWB file for i, neuron in enumerate(neurons): # os.mkdir(os.path.join(working_directory, neuron)) bp.cache_data(int(neuron), working_directory=os.path.join(working_directory, neuron)) # change directory os.chdir(os.path.join(working_directory, neuron)) # compile and load NEURON NMODL files os.system('nrnivmodl modfiles') print("{} mechanism(s) compiled out of {}").format(i + 1, len(neurons)) os.chdir(base_dir) # neuronal_model_id = 472451419 # get this from the web site as above
def download(): neuronal_model_ids = [ '478047737', '472430904', '479234506', '478513451', '472352327', '478043748', '480633088', '478513443', '489931963', '478513224', '478047816', '480632594', '472440759', '485694403', '478513459', '471081668', '477840124', '479234530', '473835796', '329322394', '486557284', '479234670', '478513445', '472306616', '486509010', '478045347', '486509232', '480051220', '485611914', '480631402', '472451419', '483108201', '496930338', '482525264', '479427516', '488461970', '477876583', '479427369', '472306544', '478810017', '482657381', '478513187', '486511108', '479694721', '476630478', '486052412', '482655070', '472306460', '472442377', '478513396', '482525598', '476637699', '487245869', '485510685', '478513398', '477494382', '482657528', '478513461', '485507735', '478513411', '480629276', '473863510', '485591806', '488462820', '472447460', '480633809', '476637723', '478047588', '482524837', '477839613', '478809991', '486556597', '480624051', '473465456', '472300877', '476679102', '480630211', '480633479', '480631048', '479427443', '478045081', '477313251', '483156585', '329321704', '472299363', '480613522', '482934212', '485591776', '472434498', '472304676', '488462783', '489932435', '473863578', '486052435', '486508758', '486556811', '473834758', '477880244', '478047618', '485602029', '478513407', '485513184', '472421285', '477510985', '480631286', '491517388', '483066358', '479234441', '488763268', '473872986', '488462965', '472455509', '489932597', '480624251', '480056382', '477876559', '478809612', '473863035', '480632564', '473862421', '486556714', '485909152', '486508647', '473871773', '471086533', '482529696', '478045202', '489931686', '473465774', '471085845', '480624414', '480631187', '480624126', '485476031', '480630857', '473862496', '471087975', '485909125', '472912177', '478513437', '477838913', '478045226', '486558444', '478048947', '486909496', '477878284', '476630516', '484628276', '479694359', '485513255', '484620556', '479694384', '483106906', '489932183', '485510712', '477510918', '472363762', '473862845', '486144663', '479695152', '483109057', '478513415', '482583564', '488759006', '472912107', '485904766', '480630395', '480361288', '485720616', '477878554', '476637796', '479694856', '478049069', '476637747', '472450023', '478513441', '472299294' ] neuronal_model_ids = [472450023, 483108201, 486556811] print("---- Downloading %i cell models..." % len(neuronal_model_ids)) for neuronal_model_id in neuronal_model_ids: print("---- Downloading cell model: %s..." % neuronal_model_id) try: bp = BiophysicalApi('http://api.brain-map.org') bp.cache_stimulus = False # change to False to not download the large stimulus NWB file working_directory = '%i' % neuronal_model_id bp.cache_data(neuronal_model_id, working_directory=working_directory) print("---- Saved model into %s, included NWB file: %s" % (working_directory, bp.cache_stimulus)) with open(working_directory + '/manifest.json', "r") as json_file: manifest_info = json.load(json_file) metadata = {} exp_id = int(manifest_info["biophys"][0]["model_file"][1][:9]) metadata['exp_id'] = exp_id metadata[ 'URL'] = 'http://celltypes.brain-map.org/mouse/experiment/electrophysiology/%s' % exp_id for m in manifest_info['manifest']: if m['key'] == "output_path": nwb_file = working_directory + '/' + m['spec'] if os.path.isfile(nwb_file): print("---- Extracting metadate from NWB file: %s" % (nwb_file)) h5file = tables.open_file(nwb_file, mode='r') metadata['AIBS:aibs_dendrite_type'] = str( h5file.root.general.aibs_dendrite_type.read()) metadata['AIBS:aibs_cre_line'] = str( h5file.root.general.aibs_cre_line.read()) metadata['AIBS:aibs_specimen_id'] = str( h5file.root.general.aibs_specimen_id.read()) metadata['AIBS:aibs_specimen_name'] = str( h5file.root.general.aibs_specimen_name.read()) metadata[ 'AIBS:intracellular_ephys:Electrode 1:location'] = str( h5file.root.general.intracellular_ephys. _v_children['Electrode 1'].location.read()) metadata['AIBS:session_id'] = str( h5file.root.general.session_id.read()) metadata['AIBS:subject:age'] = str( h5file.root.general.subject.age.read()) metadata['AIBS:subject:description'] = str( h5file.root.general.subject.description.read()) metadata['AIBS:subject:genotype'] = str( h5file.root.general.subject.genotype.read()) metadata['AIBS:subject:sex'] = str( h5file.root.general.subject.sex.read()) metadata['AIBS:subject:species'] = str( h5file.root.general.subject.species.read()) else: print("---- Can't find NWB file: %s!" % (nwb_file)) print(' Metadata:') pp.pprint(metadata) with open(working_directory + '/metadata.json', 'w') as f: json.dump(metadata, f, indent=4) except IndexError: print("Problem!")