Source code for ipfx.stimulus
import os
import logging
import warnings
import allensdk.core.json_utilities as ju
[docs]class Stimulus(object):
def __init__(self, tag_sets):
self.tag_sets = tag_sets
[docs] def has_tag(self, tag, tag_type=None):
return tag in self.tags(tag_type=tag_type, flat=True)
[docs]class StimulusOntology(object):
DEFAULT_STIMULUS_ONTOLOGY_FILE = os.path.join(
os.path.dirname(__file__),
"defaults",
"stimulus_ontology.json"
)
def __init__(self, stim_ontology_tags=None):
"""
Parameters
----------
stim_ontology_tags: nested list of stimuli ontology properties
"""
self.stimuli = list(Stimulus(s) for s in stim_ontology_tags)
self.ramp_names = ( "Ramp",)
self.long_square_names = ( "Long Square",
"Long Square Threshold",
"Long Square SupraThreshold",
"Long Square SubThreshold" )
self.coarse_long_square_names = ( "C1LSCOARSE",)
self.short_square_triple_names = ( "Short Square - Triple", )
self.short_square_names = ( "Short Square",
"Short Square Threshold",
"Short Square - Hold -60mV",
"Short Square - Hold -70mV",
"Short Square - Hold -80mV" )
self.search_names = ("Search",)
self.test_names = ("Test",)
self.blowout_names = ( 'EXTPBLWOUT', )
self.bath_names = ( 'EXTPINBATH', )
self.seal_names = ( 'EXTPCllATT', )
self.breakin_names = ( 'EXTPBREAKN', )
self.extp_names = ( 'EXTP', )
[docs] def find(self, tag, tag_type=None):
"""
Find stimuli matching a given tag
Parameters
----------
tag: str
tag_type: str
Returns
-------
matching_stims: list of Stimuli objects
"""
matching_stims = [ s for s in self.stimuli if s.has_tag(tag, tag_type=tag_type) ]
if not matching_stims:
warnings.warn("Could not find stimulus: %s" % tag)
matching_stims = [Stimulus([["code", "unknown"], ["name", "Unknown"]])]
return matching_stims
[docs] def find_one(self, tag, tag_type=None):
matching_stims = self.find(tag, tag_type)
if len(matching_stims) > 1:
warnings.warn("Multiple stimuli match '%s', one expected" % tag)
return matching_stims[0]
[docs] @classmethod
def default(cls):
"""Construct an ontology object using default tags
"""
ontology_data = ju.read(cls.DEFAULT_STIMULUS_ONTOLOGY_FILE)
return cls(ontology_data)