def sparse_matrix_to_long_dataframe( matrix, index_name="index", var_name="column", value_name="value", index_map=None, var_map=None, reverse_maps=False, ): matrix = matrix.todok() df = pd.DataFrame.from_records( list(map(lambda x: (x[0][0], x[0][1], x[1]), matrix.items())) ) df.columns = [index_name, var_name, value_name] if index_map: if reverse_maps: index_map = itemmap(reversed, index_map) df[index_name] = df[index_name].map(index_map) if var_map: if reverse_maps: var_map = itemmap(reversed, var_map) df[var_name] = df[var_name].map(var_map) return df
def getStarredIndex(self): for elt in ast.walk(self.node): if isinstance(elt, ast.Name): indice = self.getIndice(elt) if hasattr(elt, "starred"): self.starred.append(indice) self.starredIndex = itemmap( lambda kv: (kv[0], (int(kv[0] in self.starred), )), self.index)
def subgraph(self, nodes=None, vertex_filter=None, edge_filter=None, copy_positions=True): if nodes is not None: view = graph_tool.GraphView( self.network, vfilt=lambda x: self.id_to_label[x] in nodes).copy() elif vertex_filter is not None or edge_filter is not None: view = graph_tool.GraphView(self.network, vfilt=vertex_filter, efilt=edge_filter).copy() else: raise ValueError("at least one filter must be specified") old_vertex_ids = set(map(int, view.vertices())) if copy_positions and self.node_layout is not None: vertex_positions = [ self.node_layout.get_position(v_id) for v_id in view.vertices() ] else: vertex_positions = None node_map_keys = valfilter(lambda x: x in old_vertex_ids, self.node_map).keys() # TODO: vertex ids also change # TODO: vertex weights if self.edge_weight is not None: edge_weight = [self.edge_weight[e_id] for e_id in view.edges()] else: edge_weight = None view.purge_vertices() view.purge_edges() if edge_weight is not None: weight_prop = view.new_edge_property("double") weight_prop.a = edge_weight else: weight_prop = None result = Network(view, edge_weight=weight_prop) result.node_map = dict(zip(node_map_keys, map(int, view.vertices()))) # print(result.node_map) result.id_to_label = itemmap(reversed, result.node_map) if vertex_positions: result.layout_nodes(method="precomputed", positions=vertex_positions) return result
def getFuncsIndex(self): for elt in ast.walk(self.node): if isinstance(elt, ast.Name): indice = self.getIndice(elt) if hasattr(elt, "func"): self.funcs = merge_with(lambda x: reduce(add, x, []), self.funcs, {elt.func: [indice]}) for key in self.funcs.keys(): self.funcsIndex[key] = itemmap(lambda kv, key=key: (kv[0], (int(kv[0] in self.funcs[key]), )), self.index)
def treemap(tree, mapfn, key=None, path=()): res = tree if isinstance(tree, dict): res = keyfilter( identity, itemmap( lambda item: treemap(item[1], mapfn, item[0], path + (item[0], )), tree), ) elif isinstance(tree, list): res = list(map(lambda t: treemap(t, mapfn, None, path), tree)) return xform(key, res, mapfn, path)
def intersection( cls, *slicebuilders: SliceBuilder, identifier: Identifier = None ) -> SliceBuilder: """Combine a list of slicemakers using an intersection.""" # Group the slicemakers based on their class grouped_slicemakers = tz.groupby(lambda s: s.__class__, slicebuilders) # Join the slicemakers corresponding to each class, and flatten slicebuilders = list( tz.concat( tz.itemmap( lambda item: (item[0], item[0].join(*item[1])), grouped_slicemakers ).values() ) ) def apply_fn(slice_membership, batch, columns, *args, **kwargs): # Determine the size of the batch batch_size = len(batch[list(batch.keys())[0]]) # Keep track of all the slice labels all_slice_membership = [] # Run each slicemaker on the batch for slicebuilder in slicebuilders: all_slice_membership.append( slicebuilder.apply( slice_membership=np.zeros( (batch_size, slicebuilder.num_slices), dtype=np.int32 ), batch=batch, columns=columns, ) ) # Concatenate all the slice labels slice_membership = np.concatenate(all_slice_membership, axis=1) # Take the union over the slices (columns) slice_membership = np.all(slice_membership, axis=1).astype(np.int32)[ :, np.newaxis ] return slice_membership return Subpopulation(identifiers=[identifier], apply_fn=apply_fn)
def from_edgelist( cls, df: pd.DataFrame, source="source", target="target", directed=True, weight=None, ): source_attr = f"{source}__mapped__" target_attr = f"{target}__mapped__" node_values = set(df[source].unique()) node_values = node_values | set(df[target].unique()) node_map = dict(zip(sorted(node_values), range(len(node_values)))) df_mapped = df.assign( **{ source_attr: df[source].map(node_map), target_attr: df[target].map(node_map), }) if weight is not None: network, edge_weight = cls._parse_edgelist( df_mapped, source=source_attr, target=target_attr, weight=weight, directed=directed, ) else: network = cls._parse_edgelist( df_mapped, source=source_attr, target=target_attr, weight=None, directed=directed, ) edge_weight = None result = cls(network, edge_weight=edge_weight) result.node_map = node_map result.id_to_label = itemmap(reversed, node_map) return result
def itemmap(self, f): return fgenerator(cytoolz.itemmap(f, self))