def calculatePropensities(self, emo=True, role=True): for edge in self.edges: # for every edge, calculate propensities and append as an attribute attributeDict = {} emoPropList = propensities.propCalc(self, edge)[0] if emo else None if len(emoPropList) > 0: attributeDict['Emotion'] = emoPropList attributeDict['emoWeight'] = propensities.aggregateProps( emoPropList) rolePropList = propensities.propCalc(self, edge)[1] if role else None if len(rolePropList) > 0: attributeDict['Role'] = rolePropList attributeDict['roleWeight'] = propensities.aggregateProps( rolePropList) inflPropList = propensities.propCalc(self, edge)[2] if role else None if len(inflPropList) > 0: attributeDict['Influence'] = inflPropList attributeDict['inflWeight'] = propensities.aggregateProps( inflPropList) self.G[edge[0]][edge[1]] = attributeDict self.edges = nx.edges(self.G)
def calculatePropensities(self, propToggle={ 'emo': True, 'infl': True, 'role': True }): self.propToggle = propToggle for edge in self.edges: # for every edge, calculate propensities and append as an attribute attributeDict = {"propsFlag": True} IO, emoPropList, rolePropList, inflPropList = propensities.propCalc( self, edge, propToggle) attributeDict['IO'] = IO if propToggle['emo'] and len(emoPropList) > 0: attributeDict['Emotion'] = emoPropList if propToggle['role'] and len(rolePropList) > 0: attributeDict['Role'] = rolePropList if propToggle['infl'] and len(inflPropList) > 0: attributeDict['Influence'] = inflPropList self.G[edge[0]][edge[1]].update(attributeDict) self.edges = nx.edges(self.G)
def drag_predict(self, node=None): # ERGM generates probability matrix where order is G.nodes() x G.nodes() ergm_prob_mat = ergm.probability(G=self.G) if node is not None: # Assigning propensities probabilities and generating add_node links for target in self.G.nodes_iter(): IO, emoProps, roleProps, inflProps = propensities.propCalc( self, (node, target)) if len(emoProps) > 0: w = [] for prop in emoProps: if len(prop) > 1: w.append( prop[1] ) # add the product of the attribute weights to a list for each prop w_avg = np.average( w) # find average propensity product weight prob = np.random.binomial( 1, w_avg * 1 / 2 if w_avg * 1 / 2 > 0 else 0) # use w_avg as the probability for a bernoulli distribution if prob: self.G.add_edge(node, target) self.G[node][target]['Emotion'] = emoProps self.G[node][target]['Role'] = roleProps if len( roleProps) > 0 else None self.G[node][target]['Influence'] = inflProps if len( inflProps) > 0 else None self.G[node][target]['Predicted'] = True # iterate through all possible edges for i, j in product(range(len(self.G.nodes())), repeat=2): if i != j: node = self.G.nodes()[i] target = self.G.nodes()[j] prob = ergm_prob_mat[i, j] * 0.05 # check props if self.G[node].get(target) is not None: ## check emo props to modify adjacency prob matrix if present if self.G[node][target].get('Emotion') is not None: w = [] for prop in emoProps: w.append( prop[4] * prop[5] ) # add the product of the attribute weights to a list for each prop w_avg = np.average(w) prob = (prob + w_avg * 0.5) / 2 presence = np.random.binomial(1, prob) if prob < 1 else 1 # use adjacency prob mat as the probability for a bernoulli distribution if presence: self.G.add_edge(node, target) self.G[node][target]['Predicted'] = True self.feedbackUpdate()