Aggregate all explicit activities and active forms of Agents.

        This function iterates over self.statements and extracts explicitly
        stated activity types and active forms for Agents.
511187
def gather_explicit_activities(self):
        
        for stmt in self.statements:
            agents = stmt.agent_list()
            # Activity types given as ActivityConditions
            for agent in agents:
                if agent is not None and agent.activity is not None:
                    agent_base = self._get_base(agent)
                    agent_base.add_activity(agent.activity.activity_type)
            # Object activities given in RegulateActivity statements
            if isinstance(stmt, RegulateActivity):
                if stmt.obj is not None:
                    obj_base = self._get_base(stmt.obj)
                    obj_base.add_activity(stmt.obj_activity)
            # Activity types given in ActiveForms
            elif isinstance(stmt, ActiveForm):
                agent_base = self._get_base(stmt.agent)
                agent_base.add_activity(stmt.activity)
                if stmt.is_active:
                    agent_base.add_active_state(stmt.activity, stmt.agent,
                                                stmt.evidence)
                else:
                    agent_base.add_inactive_state(stmt.activity, stmt.agent,
                                                  stmt.evidence)
12655
def extract_statements(self):
        """"""
        for _, (tf, target, effect, refs) in self.df.iterrows():
            tf_agent = get_grounded_agent(tf)
            target_agent = get_grounded_agent(target)
            if effect == 'Activation':
                stmt_cls = IncreaseAmount
            elif effect == 'Repression':
                stmt_cls = DecreaseAmount
            else:
                continue
            pmids = refs.split(';')
            for pmid in pmids:
                stmt = make_stmt(stmt_cls, tf_agent, target_agent, pmid)
                self.statements.append(stmt)
39574
protected <T extends BasicInclude> List<T> aggregateBasicIncludes(List<T> original, AggregatorCallback<T> callback) throws IOException {
        final List<T> result = new LinkedList<T>();
        final Deque<T> currentAggregateList = new LinkedList<T>();
        for (final T originalElement : original) {
            // handle first loop iteration
            if (currentAggregateList.isEmpty()) {
                currentAggregateList.add(originalElement);
            }
            else {
                // test if 'originalElement' will aggregate with head element in currentAggregate 
                final T baseElement = currentAggregateList.getFirst();
                if (callback.willAggregate(originalElement, baseElement)) {
                    // matches current criteria, add to currentAggregate
                    currentAggregateList.add(originalElement);
                } 
                else {
                    // doesn't match criteria
                    // generate new single aggregate from currentAggregateList
                    final T aggregate = callback.aggregate(currentAggregateList);
                    if (null != aggregate) {
                        // push result
                        result.add(aggregate);
                    }
                    else {
                        this.logger.warn("Generated 0 byte aggregate from: " + generatePathList(currentAggregateList));
                    }

                    // zero out currentAggregateList
                    currentAggregateList.clear();

                    // add originalElement to empty list
                    currentAggregateList.add(originalElement);
                }
            }
        }
        
        // flush the currentAggregateList
        if (currentAggregateList.size() > 0) {
            final T aggregate = callback.aggregate(currentAggregateList);
            if (null != aggregate) {
                result.add(aggregate);
            }
            else {
                this.logger.warn("Generated 0 byte aggregate from: " + generatePathList(currentAggregateList));
            }
        }
        
        return result;
	}
551481
def aggregate(type, ts)
      Octo::Enterprise.each do |enterprise|
        aggregate_baseline enterprise.id, type, ts
      end
    end
301005
def count_duplicate_activities(activity, after)
      entities = { }

      activity.entities.each do |entity_name, entity|
        entities[entity_name.to_sym] = entity.model_id
      end

      self.count_activities({
        :only     => activity.class,
        :entities => entities,
        :after    => after,
      })
    end
1035024
def replace_activities(self):
        """"""
        logger.debug('Running PySB Preassembler replace activities')
        # TODO: handle activity hierarchies
        new_stmts = []

        def has_agent_activity(stmt):
            """Return True if any agents in the Statement have activity."""
            for agent in stmt.agent_list():
                if isinstance(agent, Agent) and agent.activity is not None:
                    return True
            return False
        # First collect all explicit active forms
        self._gather_active_forms()
        # Iterate over all statements
        for j, stmt in enumerate(self.statements):
            logger.debug('%d/%d %s' % (j + 1, len(self.statements), stmt))
            # If the Statement doesn't have any activities, we can just
            # keep it and move on
            if not has_agent_activity(stmt):
                new_stmts.append(stmt)
                continue
            stmt_agents = stmt.agent_list()
            num_agents = len(stmt_agents)
            # Make a list with an empty list for each Agent so that later
            # we can build combinations of Agent forms
            agent_forms = [[] for a in stmt_agents]
            for i, agent in enumerate(stmt_agents):
                # This is the case where there is an activity flag on an
                # Agent which we will attempt to replace with an explicit
                # active form
                if agent is not None and isinstance(agent, Agent) and \
                        agent.activity is not None:
                    base_agent = self.agent_set.get_create_base_agent(agent)
                    # If it is an "active" state
                    if agent.activity.is_active:
                        active_forms = base_agent.active_forms
                        # If no explicit active forms are known then we use
                        # the generic one
                        if not active_forms:
                            active_forms = [agent]
                    # If it is an "inactive" state
                    else:
                        active_forms = base_agent.inactive_forms
                        # If no explicit inactive forms are known then we use
                        # the generic one
                        if not active_forms:
                            active_forms = [agent]
                    # We now iterate over the active agent forms and create
                    # new agents
                    for af in active_forms:
                        new_agent = fast_deepcopy(agent)
                        self._set_agent_context(af, new_agent)
                        agent_forms[i].append(new_agent)
                # Otherwise we just copy over the agent as is
                else:
                    agent_forms[i].append(agent)
            # Now create all possible combinations of the agents and create new
            # statements as needed
            agent_combs = itertools.product(*agent_forms)
            for agent_comb in agent_combs:
                new_stmt = fast_deepcopy(stmt)
                new_stmt.set_agent_list(agent_comb)
                new_stmts.append(new_stmt)
        self.statements = new_stmts
203574
def document_agents(p):
    
    p.comment('agent.py', 'base agent class')
    p.comment('run_agents.py', 'Top level function to run the agents')
    
    p.comment('agent_image_metadata.py', 'agent to collect file picture metadata')
    p.comment('agent_learn_aixi.py', '')
    p.comment('dummy_learn_1.py', 'sample (but stub only) learning algorithm to be called as test below')

    p.comment('agent_explore_grid.py', 'working prototype of agent to move through a grid world, using very simple path finding.')
    p.comment('agent_email.py', 'Agent that reads emails (currently only gmail)')
    p.comment('agent_filelist.py', 'TOK - correctly scans and logs filelists from an agent')
    p.comment('collect_Win_processes.py', 'script to collect windows processes. Currently not part of agent process, more an exercise on what can be logged')
    p.comment('log_PC_usage.py', 'script to read current window title to be used as part of context to see what user is doing')
    p.comment('log_browser_history.py', 'script to dump chrome browser history to CSV - not used')
    p.comment('agg_context.py', 'detects context of user and computer')
1131002
def _assemble(self):
        """"""
        for stmt in self.statements:
            pol = self.processed_policies[stmt.uuid]
            if _is_whitelisted(stmt):
                self._dispatch(stmt, 'assemble', self.model, self.agent_set,
                               pol.parameters)
162728
def all_entities(self, is_active=True):
        
        return self.get_all_entities(return_models=True, is_active=is_active)
749542
def aggregate_in(self, On=None, AggFuncDict=None, AggFunc=None,
                 AggList=None, interspersed=True):
        
        data = spreadsheet.aggregate_in(Data=self, On=On, 
               AggFuncDict=AggFuncDict, AggFunc=AggFunc, 
               AggList = AggList, interspersed=interspersed)
        data = data.view(tabarray)
        data.view = self.coloring
        return data
1356592
function execAnySubFns(actions) {
  return actions.map(function (action) {
    return Object.keys(action).reduce(function (accum, key) {
      // if it is list of sub keys, and is a fn, then bind with tokens
      if (SUBSTITUTED_KEYS.indexOf(key) !== -1 && typeof action[key] === 'function') {
        accum[key] = action[key](); // exec fn to get value
      } else {
        accum[key] = action[key];
      }
      return accum;
    }, {});
  });
}
794382
def actions(self, s):
        ''''''
        # we try to generate every possible state and then filter those
        # states that are valid
        return [a for a in self._actions if self._is_valid(self.result(s, a))]
1285481
def aggregate(self):
        
        for report in self.reportset:
            printtime('Processing {}'.format(report.split('.')[0]), self.start)
            # Initialise the header for each report - MLST is different, as the header is different for each
            # MLST scheme. This provides a generic header instead
            header = '' if report != 'mlst.csv' else 'Strain,Genus,SequenceType,Matches,1,2,3,4,5,6,7\n'
            # Initialise a string to hold the data for each report
            data = ''
            # Open the aggregated report
            with open(os.path.join(self.reportpath, report), 'w') as aggregate:
                for sample in self.runmetadata.samples:
                    # Try to open the report for this run
                    try:
                        #
                        with open(os.path.join(sample.general.reportpath, report), 'r') as runreport:
                            # Only get the header from the first file
                            if not header:
                                header = runreport.readline()
                            else:
                                for row in runreport:
                                    # The final entry in a report does not have a newline character. Add \n as required
                                    if not row.endswith('\n'):
                                        row += '\n'
                                    # For certain reports, the header row is printed above each strain - ignore multiple
                                    # instances of the header
                                    if row.split(',')[0] != header.split(',')[0]:
                                        # Add the row to the string of data
                                        data += row
                    except IOError:
                        pass
                # Write the strings to the aggregate report file
                aggregate.write(header)
                aggregate.write(data)
359986
def get_activations(self):
        """"""
        act_events = self.tree.findall("EVENT/[type='ONT::ACTIVATE']")
        inact_events = self.tree.findall("EVENT/[type='ONT::DEACTIVATE']")
        inact_events += self.tree.findall("EVENT/[type='ONT::INHIBIT']")
        for event in (act_events + inact_events):
            event_id = event.attrib['id']
            if event_id in self._static_events:
                continue
            # Get the activating agent in the event
            agent = event.find(".//*[@role=':AGENT']")
            if agent is None:
                continue
            agent_id = agent.attrib.get('id')
            if agent_id is None:
                logger.debug(
                    'Skipping activation with missing activator agent')
                continue
            activator_agent = self._get_agent_by_id(agent_id, event_id)
            if activator_agent is None:
                continue

            # Get the activated agent in the event
            affected = event.find(".//*[@role=':AFFECTED']")
            if affected is None:
                logger.debug(
                    'Skipping activation with missing affected agent')
                continue
            affected_id = affected.attrib.get('id')
            if affected_id is None:
                logger.debug(
                    'Skipping activation with missing affected agent')
                continue

            affected_agent = self._get_agent_by_id(affected_id, event_id)
            if affected_agent is None:
                logger.debug(
                    'Skipping activation with missing affected agent')
                continue

            is_activation = True
            if _is_type(event, 'ONT::ACTIVATE'):
                self._add_extracted('ONT::ACTIVATE', event.attrib['id'])
            elif _is_type(event, 'ONT::INHIBIT'):
                is_activation = False
                self._add_extracted('ONT::INHIBIT', event.attrib['id'])
            elif _is_type(event, 'ONT::DEACTIVATE'):
                is_activation = False
                self._add_extracted('ONT::DEACTIVATE', event.attrib['id'])
            ev = self._get_evidence(event)
            location = self._get_event_location(event)
            for a1, a2 in _agent_list_product((activator_agent,
                                               affected_agent)):
                if is_activation:
                    st = Activation(a1, a2, evidence=[deepcopy(ev)])
                else:
                    st = Inhibition(a1, a2, evidence=[deepcopy(ev)])
                _stmt_location_to_agents(st, location)
                self.statements.append(st)
884420
def activities(self):
        
        if self._activities is None:
            self._activities = ActivityList(self._version, workspace_sid=self._solution['sid'], )
        return self._activities
871128
def aggregate(self, *args, **kwargs):
        
        if self.query.distinct_fields:
            raise NotImplementedError("aggregate() + distinct(fields) not implemented.")
        for arg in args:
            # The default_alias property may raise a TypeError, so we use
            # a try/except construct rather than hasattr in order to remain
            # consistent between PY2 and PY3 (hasattr would swallow
            # the TypeError on PY2).
            try:
                arg.default_alias
            except (AttributeError, TypeError):
                raise TypeError("Complex aggregates require an alias")
            kwargs[arg.default_alias] = arg

        if django.VERSION < (2, 0):
            query = self.query.clone(CTEAggregateQuery)
        else:
            query = self.query.chain(CTEAggregateQuery)
        for (alias, aggregate_expr) in kwargs.items():
            query.add_annotation(aggregate_expr, alias, is_summary=True)
            if not query.annotations[alias].contains_aggregate:
                raise TypeError("%s is not an aggregate expression" % alias)
        return query.get_aggregation(self.db, kwargs.keys())
650706
def extract_statement_from_query_result(self, res):
        
        agent_start, agent_end, affected_start, affected_end = res

        # Convert from rdflib literals to python integers so we can use
        # them to index strings
        agent_start = int(agent_start)
        agent_end = int(agent_end)
        affected_start = int(affected_start)
        affected_end = int(affected_end)

        # Find the text corresponding to these indices
        agent = self.text[agent_start:agent_end]
        affected = self.text[affected_start:affected_end]

        # Strip off surrounding whitespace
        agent = agent.lstrip().rstrip()
        affected = affected.lstrip().rstrip()

        # Make an Agent object for both the subject and the object
        subj = Agent(agent, db_refs={'TEXT': agent})
        obj = Agent(affected, db_refs={'TEXT': affected})

        statement = Influence(subj=subj, obj=obj)

        # Add the statement to the list of statements
        self.statements.append(statement)