timelink.networks package
Generation and analysis of Networks in Timelink
Submodules
timelink.networks.network_draw module
- timelink.networks.network_draw.draw_network(Graph: Graph, title='Timelink network', node_tooltips=None, edge_tooltips=None, iterations=50, scale=1, node_color_attr='type', node_colors: dict | None = None, color_palette: dict | None = None, circle_size=10, line_color='#CCCCCC', line_alpha=0.8, line_width=2, filename=None, width=500, height=500)[source]
Draws a network using Bokeh and networkx layout
- Parameters:
Graph – a networkx graph object
title – the title of the plot
node_tooltips – a list of tuples with the information to show when hovering over nodes defaults to [(“desc”, “@desc”), (“type”, “@type”)]
edge_tooltips – a list of tuples with the information to show when hovering over edges defaults to None, which means no tooltips for edges
iterations – number of iterations for the spring layout
node_color_attr – the attribute to color nodes by (default is “type”)
node_colors – a dictionary mapping node types to colors. If None, a default color palette is used.
color_palette – a dictionary mapping node types to colors. If none a default color palette is used.
filename – if provided, saves the plot to this file
width – width of the plot
height – height of the plot
- Returns:
None, but saves the plot to a file if filename is provided
- Note on coloring of nodes:
Coloring is done by the attribute specified in node_color_attr. Each node is inspected to get its attribute node_color_attr. If node_colors is provided, it uses that dictionary to set the color. node_colors should be a dictionary mapping attributes to colors. Colors are set to “gray” if the attribute is not found in node_colors. If node_colors is None, this functions will inspect the nodes to get the number of different values for node_color_attr and build a color palette using bokeh.palettes.Category10 or a custom color_palette if provided in the arguments.
timelink.networks.network_generation module
Generation of networks
- timelink.networks.network_generation.network_from_attribute(attribute: str, ignore_values: list[str] | None = None, mode='cliques', by_year=False, user=None, db: TimelinkDatabase | None = None, session=None) Graph[source]
Generate a network from common attribute values.
This function will generate a network connecting the entities that have the same value for the attribute given in the parameter.
- Parameters:
attribute (str) – The name (type) of the attribute used for generating the graph.
ignore_values (list[str], optional) – A list of values to ignore when generating the network. Defaults to None.
mode (str, optional) – The topology of the generated network (see bellow). Valid values are “cliques” and “value-node”. Defaults to “cliques”.
user (str, optional) – Use real persons identified by this user. Defaults to “none”.
db (TimelinkDatase, optional) – The TimelinkDatase object. Defaults to None. Either db or session must be provided.
session (object, optional) – The session object for the database connection. Defaults to None.
- Raises:
None –
- Topology the generated network:
If mode = “cliques” all the entities with attribute will be nodes in the graph and edges will be created between the entities with the same value of the attribute.
If mode = “value-node” a node will be created for each different value of the attribute a and edges will be created linking that node to the entities which have that value in attribute a.
In both cases entities with several values for the attribute contribute to the overall connectivity of the graph, by linking clusters of same value entities.
- Returns:
The generated network as a networkx Graph object.
- Return type:
networkx.classes.graph.Graph
Each node will have an associated dictionary of attributes:
“id”: (entity id)
“type”: “value_node” or entity class in the database.
- “desc”: a description of the node, automatically fetched
from the database (names of persons for instance)
- “is_real”: a flag stating if the “id” key refers to a real
entity or to an occurrence.
- “url”: a link to the entity information in the database
in the format http://localhost:8080/mhk/database/id/entityID
Each edge will have associated the following key-value pairs:
“date1”: date of the atribute in the left most node
“date2”: date of the attribute in the right most node
“attribute”: the type of the attribute
“value”: the value of the attribute
Examples:
- Generate a network of people that graduated in the same place
G = network_from_attribute("graduated_at", mode="value-node")