def __init__(self, behavior_session_id: int, lims_credentials: Optional[DbCredentials] = None, mtrain_credentials: Optional[DbCredentials] = None): super().__init__() if mtrain_credentials: self.mtrain_db = PostgresQueryMixin( dbname=mtrain_credentials.dbname, user=mtrain_credentials.user, host=mtrain_credentials.host, port=mtrain_credentials.port, password=mtrain_credentials.password) else: self.mtrain_db = (credential_injector(MTRAIN_DB_CREDENTIAL_MAP) (PostgresQueryMixin)()) if lims_credentials: self.lims_db = PostgresQueryMixin( dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, port=lims_credentials.port, password=lims_credentials.password) else: self.lims_db = (credential_injector(LIMS_DB_CREDENTIAL_MAP) (PostgresQueryMixin)()) self.behavior_session_id = behavior_session_id ids = self._get_ids() self.ophys_experiment_ids = ids.get("ophys_experiment_ids") self.ophys_session_id = ids.get("ophys_session_id") self.behavior_training_id = ids.get("behavior_training_id") self.foraging_id = ids.get("foraging_id") self.ophys_container_id = ids.get("ophys_container_id")
def default( cls, lims_credentials: Optional[DbCredentials] = None, mtrain_credentials: Optional[DbCredentials] = None, app_kwargs: Optional[Dict[str, Any]] = None) -> \ "BehaviorProjectLimsApi": """Construct a BehaviorProjectLimsApi instance with default postgres and app engines. Parameters ---------- lims_credentials: Optional[DbCredentials] Credentials to pass to the postgres connector to the lims database. If left unspecified, will check environment variables for the appropriate values. mtrain_credentials: Optional[DbCredentials] Credentials to pass to the postgres connector to the mtrain database. If left unspecified, will check environment variables for the appropriate values. app_kwargs: Dict Dict of arguments to pass to the app engine. Currently unused. Returns ------- BehaviorProjectLimsApi """ _app_kwargs = {"scheme": "http", "host": "lims2"} if app_kwargs: _app_kwargs.update(app_kwargs) if lims_credentials: lims_engine = PostgresQueryMixin( dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, password=lims_credentials.password, port=lims_credentials.port) else: # Currying is equivalent to decorator syntactic sugar lims_engine = (credential_injector(LIMS_DB_CREDENTIAL_MAP)( PostgresQueryMixin)()) if mtrain_credentials: mtrain_engine = PostgresQueryMixin( dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, password=lims_credentials.password, port=lims_credentials.port) else: # Currying is equivalent to decorator syntactic sugar mtrain_engine = (credential_injector(MTRAIN_DB_CREDENTIAL_MAP)( PostgresQueryMixin)()) app_engine = HttpEngine(**_app_kwargs) return cls(lims_engine, mtrain_engine, app_engine)
def get_ophys_experiment_df(): api = PostgresQueryMixin() query = ''' SELECT oec.visual_behavior_experiment_container_id as container_id, oec.ophys_experiment_id, oe.workflow_state, d.full_genotype as full_genotype, id.depth as imaging_depth, st.acronym as targeted_structure, os.name as session_name, equipment.name as equipment_name FROM ophys_experiments_visual_behavior_experiment_containers oec LEFT JOIN ophys_experiments oe ON oe.id = oec.ophys_experiment_id LEFT JOIN ophys_sessions os ON oe.ophys_session_id = os.id LEFT JOIN specimens sp ON sp.id=os.specimen_id LEFT JOIN donors d ON d.id=sp.donor_id LEFT JOIN imaging_depths id ON id.id=os.imaging_depth_id LEFT JOIN structures st ON st.id=oe.targeted_structure_id LEFT JOIN equipment ON equipment.id=os.equipment_id ''' return pd.read_sql(query, api.get_connection())
def default( cls, pg_kwargs: Optional[Dict[str, Any]] = None, app_kwargs: Optional[Dict[str, Any]] = None) -> \ "BehaviorProjectLimsApi": """Construct a BehaviorProjectLimsApi instance with default postgres and app engines. :param pg_kwargs: dict of keyword arguments to pass to the PostgresQueryMixin class instance. Valid arguments include: "dbname", "user", "host", "password", "port". Will use defaults in PostGresQueryMixin.__init__ if unspecified. :type pg_kwargs: dict :param app_kwargs: dict of keyword arguments to pass to the HTTPEngine class instance. Valid arguments include: "scheme", "host". Will default to scheme=http, host=lims2 if left unspecified. :type app_kwargs: dict :rtype: BehaviorProjectLimsApi """ _pg_kwargs = pg_kwargs or dict() _app_kwargs = {"scheme": "http", "host": "lims2"} if app_kwargs: _app_kwargs.update(app_kwargs) pg_engine = PostgresQueryMixin(**_pg_kwargs) app_engine = HttpEngine(**_app_kwargs) return cls(pg_engine, app_engine)
def _get_behavior_stage_table( self, behavior_session_ids: Optional[List[int]] = None, mtrain_db: Optional[PostgresQueryMixin] = None): # Select fewer rows if possible via behavior_session_id if behavior_session_ids: foraging_ids = self._get_foraging_ids_from_behavior_session( behavior_session_ids) # Otherwise just get the full table from mtrain else: foraging_ids = None foraging_ids_query = self._build_in_list_selector_query( "bs.id", foraging_ids) # TODO: this password has already been exposed in code but we really # need to move towards using a secrets database if not mtrain_db: mtrain_db = PostgresQueryMixin(dbname="mtrain", user="******", host="prodmtrain1", port=5432, password="******") query = f""" SELECT stages.name as session_type, bs.id AS foraging_id FROM behavior_sessions bs JOIN stages ON stages.id = bs.state_id {foraging_ids_query}; """ return mtrain_db.select(query)
def default(cls, lims_credentials: Optional[DbCredentials] = None, app_kwargs=None, asynchronous=True): """ Construct a "straightforward" lims api that can fetch data from lims2. Parameters ---------- lims_credentials : DbCredentials Credentials and configuration for postgres queries against the LIMS database. If left unspecified will attempt to provide credentials from environment variables. app_kwargs : dict High-level configuration for http requests. See allensdk.brain_observatory.ecephys.ecephys_project_api.http_engine.HttpEngine and AsyncHttpEngine for details. asynchronous : bool If true, (http) queries will be made asynchronously. Returns ------- EcephysProjectLimsApi """ _app_kwargs = { "scheme": "http", "host": "lims2", "asynchronous": asynchronous } if app_kwargs is not None: if "asynchronous" in app_kwargs: raise TypeError( "please specify asynchronicity option at the api level rather than for the http engine" ) _app_kwargs.update(app_kwargs) app_engine_cls = AsyncHttpEngine if _app_kwargs[ "asynchronous"] else HttpEngine app_engine = app_engine_cls(**_app_kwargs) if lims_credentials is not None: pg_engine = PostgresQueryMixin(dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, password=lims_credentials.password, port=lims_credentials.port) else: # Currying is equivalent to decorator syntactic sugar pg_engine = (credential_injector(LIMS_DB_CREDENTIAL_MAP)( PostgresQueryMixin)()) return cls(pg_engine, app_engine)
def __init__(self, ophys_experiment_id: int, lims_credentials: Optional[DbCredentials] = None): self.ophys_experiment_id = ophys_experiment_id if lims_credentials: self.lims_db = PostgresQueryMixin( dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, password=lims_credentials.password, port=lims_credentials.port) else: # Currying is equivalent to decorator syntactic sugar self.lims_db = (credential_injector(LIMS_DB_CREDENTIAL_MAP) (PostgresQueryMixin)())
def default(cls, pg_kwargs=None, app_kwargs=None): _pg_kwargs = {} if pg_kwargs is not None: _pg_kwargs.update(pg_kwargs) _app_kwargs = {"scheme": "http", "host": "lims2"} if app_kwargs is not None: _app_kwargs.update(app_kwargs) pg_engine = PostgresQueryMixin(**_pg_kwargs) app_engine = HttpEngine(**_app_kwargs) return cls(pg_engine, app_engine)
def default(cls, pg_kwargs=None, app_kwargs=None): _pg_kwargs = {} if pg_kwargs is not None: _pg_kwargs.update(pg_kwargs) _app_kwargs = {"scheme": "http", "host": "lims2", "asynchronous": True} if app_kwargs is not None: _app_kwargs.update(app_kwargs) app_engine_cls = AsyncHttpEngine if _app_kwargs[ "asynchronous"] else HttpEngine pg_engine = PostgresQueryMixin(**_pg_kwargs) app_engine = app_engine_cls(**_app_kwargs) return cls(pg_engine, app_engine)
def __init__(self, behavior_session_id): super().__init__() # TODO: this password has been exposed in code but we really need # to move towards using a secrets database self.mtrain_db = PostgresQueryMixin(dbname="mtrain", user="******", host="prodmtrain1", port=5432, password="******") self.behavior_session_id = behavior_session_id ids = self._get_ids() self.ophys_experiment_ids = ids.get("ophys_experiment_ids") self.ophys_session_id = ids.get("ophys_session_id") self.behavior_training_id = ids.get("behavior_training_id") self.foraging_id = ids.get("foraging_id") self.ophys_container_id = ids.get("ophys_container_id")
def default(cls, pg_kwargs=None, app_kwargs=None, asynchronous=True): """ Construct a "straightforward" lims api that can fetch data from lims2. Parameters ---------- pg_kwargs : dict High-level configuration for postgres queries. See allensdk.internal.api.PostgresQueryMixin for details. app_kwargs : dict High-level configuration for http requests. See allensdk.brain_observatory.ecephys.ecephys_project_api.http_engine.HttpEngine and AsyncHttpEngine for details. asynchronous : bool If true, (http) queries will be made asynchronously. Returns ------- EcephysProjectLimsApi """ _pg_kwargs = {} if pg_kwargs is not None: _pg_kwargs.update(pg_kwargs) _app_kwargs = { "scheme": "http", "host": "lims2", "asynchronous": asynchronous } if app_kwargs is not None: if "asynchronous" in app_kwargs: raise TypeError( "please specify asynchronicity option at the api level rather than for the http engine" ) _app_kwargs.update(app_kwargs) app_engine_cls = AsyncHttpEngine if _app_kwargs[ "asynchronous"] else HttpEngine pg_engine = PostgresQueryMixin(**_pg_kwargs) app_engine = app_engine_cls(**_app_kwargs) return cls(pg_engine, app_engine)
def get_containers_df(only_passed=True): api = PostgresQueryMixin() if only_passed is True: query = ''' SELECT * FROM visual_behavior_experiment_containers vbc WHERE workflow_state IN ('container_qc','publish'); ''' else: query = ''' SELECT * FROM visual_behavior_experiment_containers vbc ''' return pd.read_sql( query, api.get_connection()).rename(columns={'id': 'container_id'})[[ 'container_id', 'specimen_id', 'workflow_state' ]]
def __init__(self, behavior_experiment_id: int, lims_credentials: Optional[DbCredentials] = None): """ Notes ----- - behavior_experiment_id is the same as behavior_session_id which is in lims - behavior_experiment_id is associated with foraging_id in lims - foraging_id in lims is the same as behavior_session_uuid in mtrain which is the same as session_uuid in the pickle returned by behavior_stimulus_file """ self.behavior_experiment_id = behavior_experiment_id if lims_credentials: self.lims_db = PostgresQueryMixin( dbname=lims_credentials.dbname, user=lims_credentials.user, host=lims_credentials.host, password=lims_credentials.password, port=lims_credentials.port) else: # Use default credentials from provider # Currying is equivalent to decorator syntactic sugar self.lims_db = ( credential_injector(LIMS_DB_CREDENTIAL_MAP) (PostgresQueryMixin)())
def behavior_session_id_to_foraging_id(behavior_session_id): '''maps behavior_session_id to foraging_id''' api = PostgresQueryMixin() query = '''select foraging_id from behavior_sessions where id = '{}';'''.format( behavior_session_id) return api.fetchone(query, strict=True)