超图#

超图是边可以连接两个以上节点的图,例如涉及多个实体的事件。

Graphistry将超图编码为两种形式的常规图。一种是超节点和常规节点之间的二分图,通过超边连接。另一种是通过超边连接的常规节点。在这两种情况下,每个超边都由多个常规的src/dst边编码。

超图#

graphistry.PlotterBase.PlotterBase.hypergraph = <function PlotterBase.hypergraph>

将数据框转换为超图。

Parameters:
  • raw_events (pandas.DataFrame) – 要转换的数据框(pandas 或 cudf)。

  • entity_types (可选[列表]) – 要转换为节点的列(字符串),None 表示所有

  • opts (dict) – 请参见下文

  • drop_edge_attrs (bool) – 是否在边上包含每行的属性,默认为 False(包含)

  • verbose (bool) – 是否打印大小信息

  • direct (bool) – 省略超节点,而是在事件中强连接节点

  • engine (bool) – 用于指定引擎的字符串(pandas, cudf, …)

  • npartitions (可选[int]) – 对于分布式引擎,将事件分成多少粗粒度的部分

  • chunksize (可选[int]) – 对于分布式引擎,在chunksize行后分割事件

  • drop_na (布尔值)

从数据框中创建图形,并将图形组件作为数据框返回,以及可渲染的结果Plotter。超图揭示了行之间和列值之间的关系。此转换对于事件列表、样本、关系和其他结构化高维数据非常有用。

通过传递engine=’pandas’、‘cudf’、‘dask’、‘dask_cudf’(默认:‘pandas’)来指定本地计算引擎。 如果事件不是该引擎的格式,它们将被转换为该格式。

转换会为entity_types列中的每个唯一值创建一个节点(默认:所有列)。 如果direct=False(默认),每一行也会被转换为一个节点。 添加边以连接每个表格单元格到其原始行的节点,或者如果direct=True,则连接到同一行的其他节点。 节点被赋予与原始列名对应的'type'属性,或者在行的情况下,赋予'EventID'属性。 选项进一步控制转换,例如列类别定义,用于控制不同列中重复出现的值是否应被视为一个节点, 或者是否仅在特定列类型对之间绘制边。

考虑一个事件列表。每一行代表一个独特的事件,每一列代表事件的某些元数据。如果多个事件有共同的元数据,它们将通过这些元数据值进行传递连接。布局算法将尝试将事件聚集在一起。相反,如果一个事件有独特的元数据,独特的元数据将变成只与事件节点连接的节点,聚类算法将使它们围绕事件节点形成一个环。

最佳实践是将EVENTID设置为行的唯一ID, 将SKIP设置为所有非分类列(或将entity_types设置为所有分类列), 并将CATEGORY设置为具有相同类型值的列组。

为了防止为null值创建节点,设置drop_na=True。 一些数据框引擎可能具有不理想的null处理方式, 并建议用np.nan替换None值。

可选的 opts={...} 配置选项包括:

  • ‘EVENTID’: 用于检查行ID的列名。默认情况下,使用行索引。

  • ‘CATEGORIES’: 字典将类别名称映射到包含的列。例如,{‘IP’: [‘srcAddress’, ‘dstAddress’]}。如果相同的IP出现在两列中,这将使转换为其生成一个节点,而不是为每列生成一个节点。

  • ‘DELIM’: 在创建节点ID时,定义列名和节点值之间使用的分隔符

  • ‘SKIP’: 不转换为节点的列名列表。例如,日期和数字通常会被跳过。

  • ‘EDGES’:对于direct=True,不是创建所有边,而是选择列对。例如,{‘a’: [‘b’, ‘d’], ‘d’: [‘d’]} 创建列 a->b 和 a->d 之间的边,以及自边 d->d。

Returns:

{'实体': DF, '事件': DF, '边': DF, '节点': DF, '图': Plotter}

Return type:

字典

Parameters:
  • entity_types (列表[字符串] | )

  • opts (dict)

  • drop_na (布尔值)

  • drop_edge_attrs (bool)

  • verbose (bool)

  • direct (bool)

  • engine (str)

  • npartitions (int | None)

  • chunksize (int | None)

示例:连接用户<-行->老板

import graphistry
users_df = pd.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_df)
g = h['graph'].plot()

示例:连接用户->老板

import graphistry
users_df = pd.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_df, direct=True)
g = h['graph'].plot()

示例:连接用户<->老板

import graphistry
users_df = pd.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_df, direct=True, opts={'EDGES': {'user': ['boss'], 'boss': ['user']}})
g = h['graph'].plot()

示例:仅考虑节点的某些列

import graphistry
users_df = pd.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_df, entity_types=['boss'])
g = h['graph'].plot()

示例:将匹配的 user:: 和 boss:: 节点折叠为一个人:: 节点

import graphistry
users_df = pd.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_df, opts={'CATEGORIES': {'person': ['user', 'boss']}})
g = h['graph'].plot()

示例:使用 cudf 引擎代替 pandas

import cudf, graphistry
users_gdf = cudf.DataFrame({'user': ['a','b','x'], 'boss': ['x', 'x', 'y']})
h = graphistry.hypergraph(users_gdf, engine='cudf')
g = h['graph'].plot()
hypergraph

函数 graphistry.hyper_dask.hypergraph() 的主要别名。

class graphistry.hyper_dask.HyperBindings(TITLE='nodeTitle', DELIM='::', NODEID='nodeID', ATTRIBID='attribID', EVENTID='EventID', EVENTTYPE='event', SOURCE='src', DESTINATION='dst', CATEGORY='category', NODETYPE='type', EDGETYPE='edgeType', NULLVAL='null', SKIP=None, CATEGORIES={}, EDGES=None)#

基础类:object

Parameters:
  • 标题 (str)

  • DELIM (str)

  • NODEID (str)

  • ATTRIBID (str)

  • EVENTID (str)

  • EVENTTYPE (str)

  • SOURCE (str)

  • 目的地 (str)

  • 类别 (str)

  • NODETYPE (str)

  • EDGETYPE (str)

  • NULLVAL (str)

  • SKIP (列表[字符串] | )

  • 分类 (字典[字符串, 列表[字符串]])

  • EDGES (字典[字符串, 列表[字符串]] | )

class graphistry.hyper_dask.Hypergraph(g, defs, entities, event_entities, edges, source, destination, engine=Engine.PANDAS, debug=False)#

基础类:object

Parameters:
  • 实体 (任意)

  • event_entities (任意)

  • edges (任意)

  • (字符串)

  • 目的地 (str)

  • engine (Engine)

  • debug (bool)

graphistry.hyper_dask.clean_events(events, defs, engine, npartitions=None, chunksize=None, dropna=False, debug=False)#

复制并重置索引为目标引擎格式

Parameters:
  • 事件 (任意)

  • defs (HyperBindings)

  • engine (Engine)

  • npartitions (int | None)

  • chunksize (int | None)

  • dropna (布尔值)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.coerce_col_safe(s, to_dtype)#
graphistry.hyper_dask.col2cat(cat_lookup, col)#
Parameters:
  • cat_lookup (Dict[str, str])

  • col (str)

graphistry.hyper_dask.concat(dfs, engine, debug=False)#
Parameters:
  • dfs (列表[任意类型])

  • engine (Engine)

graphistry.hyper_dask.df_coercion(df, engine, npartitions=None, chunksize=None, debug=False)#

从df到选择的引擎

Supported coercions:

pd <- pd cudf <- pd, cudf ddf <- pd, ddf dgdf <- pd, cudf, dgdf

Parameters:
  • df (任意)

  • engine (Engine)

  • npartitions (int | None)

  • chunksize (int | None)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.direct_edgelist_shape(entity_types, defs)#

边的格式为 {src_col: [dest_col1, dest_col2], ….} 如果为 None,则创建所有到所有的连接,方向由算法决定

Parameters:
Return type:

Dict[str, List[str]]

graphistry.hyper_dask.format_direct_edges(engine, events, entity_types, defs, edge_shape, drop_na, drop_edge_attrs, debug=False)#
Parameters:
  • engine (Engine)

  • 事件 (任意)

  • defs (HyperBindings)

  • drop_na (布尔值)

  • drop_edge_attrs (bool)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.format_entities(events, entity_types, defs, direct, drop_na, engine, npartitions, chunksize, debug=False)#
Parameters:
  • 事件 (任意)

  • entity_types (List[str])

  • defs (HyperBindings)

  • direct (bool)

  • drop_na (布尔值)

  • engine (Engine)

  • npartitions (int | None)

  • chunksize (int | None)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.format_entities_from_col(defs, cat_lookup, drop_na, engine, col_name, df_with_col, meta, debug)#
For unique v in column col, create [{col: str(v), title: str(v), nodetype: col, nodeid: <cat><delim><v>}]
  • 尊重 drop_na

  • 尊重列名覆盖

  • 根据引擎接收并返回 pd.DataFrame / cudf.DataFrame

Parameters:
  • defs (HyperBindings)

  • cat_lookup (Dict[str, str])

  • drop_na (布尔值)

  • engine (Engine)

  • col_name (str)

  • df_with_col (Any)

  • meta (数据框)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.format_hyperedges(engine, events, entity_types, defs, drop_na, drop_edge_attrs, debug=False)#
Parameters:
  • engine (Engine)

  • 事件 (任意)

  • entity_types (List[str])

  • defs (HyperBindings)

  • drop_na (布尔值)

  • drop_edge_attrs (bool)

  • debug (bool)

Return type:

任何

graphistry.hyper_dask.format_hypernodes(events, defs, drop_na)#
graphistry.hyper_dask.get_df_cons(engine)#
Parameters:

engine (Engine)

graphistry.hyper_dask.get_series_cons(engine, dtype='int32')#
Parameters:

engine (Engine)

graphistry.hyper_dask.hyperbinding(g, defs, entities, event_entities, edges, source, destination)#
graphistry.hyper_dask.hypergraph(g, raw_events, entity_types=None, opts={}, drop_na=True, drop_edge_attrs=False, verbose=True, direct=False, engine='pandas', npartitions=None, chunksize=None, debug=False)#
Internal details:
  • 当前ID为字符串:${namespace(col)}${delim}${str(val)}

  • 调试:尽早使用 persist() 来捕捉错误

Parameters:
  • raw_events (任意)

  • entity_types (列表[字符串] | )

  • opts (dict)

  • drop_na (布尔值)

  • drop_edge_attrs (bool)

  • verbose (bool)

  • direct (bool)

  • engine (str)

  • npartitions (int | None)

  • chunksize (int | None)

  • debug (bool)

graphistry.hyper_dask.make_reverse_lookup(categories)#
graphistry.hyper_dask.mt_df(engine)#
Parameters:

engine (Engine)

graphistry.hyper_dask.mt_nodes(defs, events, entity_types, direct, engine)#
Parameters:
  • defs (HyperBindings)

  • 事件 (任意)

  • entity_types (List[str])

  • direct (bool)

  • engine (Engine)

Return type:

数据框

graphistry.hyper_dask.mt_series(engine, dtype='int32')#
Parameters:

engine (Engine)

graphistry.hyper_dask.screen_entities(events, entity_types, defs)#

列出实体列:如果提供了用户指定的实体,则列出未跳过的用户指定实体,否则列出未跳过的列

Parameters:
  • 事件 (任意)

  • entity_types (列表[字符串] | )

  • defs (HyperBindings)

Return type:

列表[str]

graphistry.hyper_dask.series_cons(engine, arr, dtype='int32', npartitions=None, chunksize=None)#
Parameters:
  • engine (Engine)

  • arr (列表)

graphistry.hyper_dask.shallow_copy(df, engine, debug=False)#
Parameters:
  • df (任意)

  • engine (Engine)

  • debug (bool)

Return type:

任何