Example #1
0
 def get_session_data(self, ophys_experiment_id: int, fixed: bool = False):
     """
     Note -- This method mocks the behavior of a cache. Future
     development will include an NWB reader to read from
     a true local cache (once nwb files are created).
     TODO: Using `fixed` will raise a NotImplementedError since there
     is no real cache.
     """
     if fixed:
         raise NotImplementedError
     fetch_session = partial(self.fetch_api.get_session_data,
                             ophys_experiment_id)
     return call_caching(
         fetch_session,
         lambda x: x,  # not writing anything
         lazy=False,  # can't actually read from file cache
         read=fetch_session)
    def get_behavior_session_data(self,
                                  behavior_session_id: int,
                                  fixed: bool = False):
        """
        Note -- This method mocks the behavior of a cache. No files are
        actually downloaded for local access. Instead, it adds the
        session id to a csv log. If the "fixed" parameter is true,
        then the API will first check to ensure that the log is present
        in the record before pulling the data.
        """
        # TODO: Future development will include an NWB reader to read from
        # a true local cache (once nwb files are created)
        # For now just check the log if pass `fixed`
        path = self.get_cache_path(None, self.BEHAVIOR_ANALYSIS_LOG_KEY)
        if fixed:
            self.logger.warning(
                "Warning! Passing `fixed=True` does not ensure that the "
                "underlying data has not changed, as no data are actually "
                "cached locally. The log will be updated each time the data "
                "are pulled from the database for tracking purposes.")
            try:
                record = pd.read_csv(path)
            except FileNotFoundError:
                raise MissingDataError(
                    "No analysis log found! Add to the log by getting "
                    "session data with fixed=False.")
            if behavior_session_id not in record["behavior_session_id"].values:
                raise MissingDataError(
                    f"Data for ophys experiment {behavior_session_id} not "
                    "found!")

        fetch_session = partial(self.fetch_api.get_behavior_only_session_data,
                                behavior_session_id)
        write_log = partial(_write_log,
                            path=path,
                            key_name="behavior_session_id",
                            key_value=behavior_session_id)
        return call_caching(
            fetch_session,
            write_log,
            lazy=False,  # can't actually read from file cache
            read=fetch_session)