enhanced timeline, improved handling of odd cases

This commit is contained in:
Florian Förster
2024-07-24 16:49:19 +02:00
parent 578c543a3e
commit 9197146d2c
21 changed files with 5900 additions and 234 deletions

View File

@@ -17,7 +17,7 @@ from lang_main.constants import (
EDGE_WEIGHT_DECIMALS,
PROPERTY_NAME_DEGREE_WEIGHTED,
)
from lang_main.errors import EdgePropertyNotContainedError
from lang_main.errors import EdgePropertyNotContainedError, EmptyEdgesError, EmptyGraphError
from lang_main.io import load_pickle, save_pickle
from lang_main.loggers import logger_graphs as logger
from lang_main.types import (
@@ -381,9 +381,12 @@ def normalise_array_linear(
npt.NDArray[np.float32]
min/max normalised array
"""
arr_norm = (array - array.min()) / (array.max() - array.min())
return arr_norm.astype(np.float32)
div = array.max() - array.min()
if div != 0:
arr_norm = (array - array.min()) / div
return arr_norm.astype(np.float32)
else:
return np.zeros(shape=array.shape, dtype=np.float32)
def weight_scaling(
@@ -459,6 +462,8 @@ def rescale_edge_weights(
weight_property: str = 'weight',
) -> Graph | DiGraph | TokenGraph:
graph = graph.copy()
# check non-emptiness
verify_non_empty_graph(graph, including_edges=True)
# check if all edges contain weight property
verify_property(graph, property=weight_property)
@@ -473,6 +478,33 @@ def rescale_edge_weights(
return graph
def verify_non_empty_graph(
graph: DiGraph | Graph,
including_edges: bool = True,
) -> None:
"""check if the given graph is empty, presence of nodes is checked first,
then of edges
Parameters
----------
graph : DiGraph | Graph
graph to check for emptiness
including_edges : bool, optional
whether to check for non-existence of edges, by default True
Raises
------
EmptyGraphError
if graph does not contain any nodes and therefore edges
EmptyEdgesError
if graph does not contain any edges
"""
if not tuple(graph.nodes):
raise EmptyGraphError(f'Graph object >>{graph}<< does not contain any nodes.')
elif including_edges and not tuple(graph.edges):
raise EmptyEdgesError(f'Graph object >>{graph}<< does not contain any edges.')
# ** ---------------------------------------
class TokenGraph(DiGraph):
def __init__(

View File

@@ -1,128 +1,128 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vizmap id="VizMap-2024_07_12-08_08" documentVersion="3.1">
<vizmap id="VizMap-2024_07_24-08_54" documentVersion="3.1">
<visualStyle name="lang_main">
<network>
<visualProperty default="0.0" name="NETWORK_CENTER_X_LOCATION"/>
<visualProperty default="0.0" name="NETWORK_CENTER_Y_LOCATION"/>
<visualProperty default="0.0" name="NETWORK_CENTER_Z_LOCATION"/>
<visualProperty default="false" name="NETWORK_ANNOTATION_SELECTION"/>
<visualProperty default="1.0" name="NETWORK_SCALE_FACTOR"/>
<visualProperty default="false" name="NETWORK_NODE_LABEL_SELECTION"/>
<visualProperty default="400.0" name="NETWORK_HEIGHT"/>
<visualProperty default="true" name="NETWORK_NODE_SELECTION"/>
<visualProperty default="550.0" name="NETWORK_WIDTH"/>
<visualProperty default="0.0" name="NETWORK_DEPTH"/>
<visualProperty default="false" name="NETWORK_FORCE_HIGH_DETAIL"/>
<visualProperty default="" name="NETWORK_TITLE"/>
<visualProperty default="true" name="NETWORK_EDGE_SELECTION"/>
<visualProperty default="#F7FFFF" name="NETWORK_BACKGROUND_PAINT"/>
<visualProperty default="0.0" name="NETWORK_DEPTH"/>
<visualProperty default="0.0" name="NETWORK_CENTER_Z_LOCATION"/>
<visualProperty default="true" name="NETWORK_EDGE_SELECTION"/>
<visualProperty default="false" name="NETWORK_ANNOTATION_SELECTION"/>
<visualProperty default="" name="NETWORK_TITLE"/>
<visualProperty default="false" name="NETWORK_FORCE_HIGH_DETAIL"/>
<visualProperty default="true" name="NETWORK_NODE_SELECTION"/>
<visualProperty default="0.0" name="NETWORK_CENTER_Y_LOCATION"/>
<visualProperty default="0.0" name="NETWORK_CENTER_X_LOCATION"/>
<visualProperty default="false" name="NETWORK_NODE_LABEL_SELECTION"/>
<visualProperty default="550.0" name="NETWORK_WIDTH"/>
<visualProperty default="400.0" name="NETWORK_HEIGHT"/>
</network>
<node>
<dependency value="true" name="nodeCustomGraphicsSizeSync"/>
<dependency value="true" name="nodeSizeLocked"/>
<visualProperty default="ROUND_RECTANGLE" name="NODE_LABEL_BACKGROUND_SHAPE"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_9"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_7"/>
<visualProperty default="true" name="NODE_NESTED_NETWORK_IMAGE_VISIBLE"/>
<visualProperty default="0.0" name="NODE_LABEL_ROTATION"/>
<visualProperty default="175" name="NODE_LABEL_BACKGROUND_TRANSPARENCY"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_8"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_2"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_6"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_7"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_1"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_4"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_9"/>
<visualProperty default="ROUND_RECTANGLE" name="COMPOUND_NODE_SHAPE"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_5"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_9"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_5"/>
<visualProperty default="10.0" name="COMPOUND_NODE_PADDING"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_3"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_6"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_8"/>
<visualProperty default="SE,NW,c,-2.00,3.00" name="NODE_LABEL_POSITION"/>
<visualProperty default="ELLIPSE" name="NODE_SHAPE"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_3"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_4"/>
<visualProperty default="SansSerif.plain,plain,12" name="NODE_LABEL_FONT_FACE"/>
<visualProperty default="#D1F5BE" name="NODE_BORDER_PAINT"/>
<visualProperty default="40.0" name="NODE_HEIGHT"/>
<visualProperty default="255" name="NODE_LABEL_TRANSPARENCY"/>
<visualProperty default="#E1E1E1" name="NODE_LABEL_BACKGROUND_COLOR"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_6"/>
<visualProperty default="false" name="NODE_SELECTED"/>
<visualProperty default="0.0" name="NODE_DEPTH"/>
<visualProperty default="SOLID" name="NODE_BORDER_STROKE"/>
<visualProperty default="" name="NODE_TOOLTIP"/>
<visualProperty default="7.0" name="NODE_BORDER_WIDTH"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_2"/>
<visualProperty default="#A63C06" name="NODE_LABEL_COLOR"/>
<visualProperty default="0.0" name="NODE_X_LOCATION"/>
<visualProperty default="18.0" name="NODE_SIZE"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_8"/>
<visualProperty default="0.0" name="NODE_Z_LOCATION"/>
<visualProperty default="#FE9929" name="NODE_FILL_COLOR"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_1"/>
<visualProperty default="255" name="NODE_BORDER_TRANSPARENCY"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_1"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_2"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_3"/>
<visualProperty default="60.0" name="NODE_WIDTH"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_6"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_9"/>
<visualProperty default="18.0" name="NODE_SIZE"/>
<visualProperty default="#A63C06" name="NODE_LABEL_COLOR"/>
<visualProperty default="0.0" name="NODE_LABEL_ROTATION"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_3"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_3"/>
<visualProperty default="#E1E1E1" name="NODE_LABEL_BACKGROUND_COLOR"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_4"/>
<visualProperty default="#D1F5BE" name="NODE_BORDER_PAINT"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_8"/>
<visualProperty default="40.0" name="NODE_HEIGHT"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_7"/>
<visualProperty default="255" name="NODE_LABEL_TRANSPARENCY"/>
<visualProperty default="" name="NODE_TOOLTIP"/>
<visualProperty default="false" name="NODE_SELECTED"/>
<visualProperty default="255" name="NODE_BORDER_TRANSPARENCY"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_5"/>
<visualProperty default="14" name="NODE_LABEL_FONT_SIZE"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_2"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_6"/>
<visualProperty default="" name="NODE_LABEL">
<passthroughMapping attributeName="name" attributeType="string"/>
</visualProperty>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_5"/>
<visualProperty default="500.0" name="NODE_LABEL_WIDTH"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_4"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_7"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_2"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_2"/>
<visualProperty default="#FE9929" name="NODE_FILL_COLOR"/>
<visualProperty default="#FFFF00" name="NODE_SELECTED_PAINT"/>
<visualProperty default="0.0" name="NODE_Y_LOCATION"/>
<visualProperty default="true" name="NODE_VISIBLE"/>
<visualProperty default="255" name="NODE_TRANSPARENCY"/>
<visualProperty default="14" name="NODE_LABEL_FONT_SIZE"/>
<visualProperty default="500.0" name="NODE_LABEL_WIDTH"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_8"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_5"/>
<visualProperty default="60.0" name="NODE_WIDTH"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_4"/>
<visualProperty default="10.0" name="COMPOUND_NODE_PADDING"/>
<visualProperty default="SansSerif.plain,plain,12" name="NODE_LABEL_FONT_FACE"/>
<visualProperty default="0.0" name="NODE_Y_LOCATION"/>
<visualProperty default="ELLIPSE" name="NODE_SHAPE"/>
<visualProperty default="SE,NW,c,-2.00,3.00" name="NODE_LABEL_POSITION"/>
<visualProperty default="true" name="NODE_VISIBLE"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_1"/>
<visualProperty default="ROUND_RECTANGLE" name="NODE_LABEL_BACKGROUND_SHAPE"/>
<visualProperty default="0.0" name="NODE_Z_LOCATION"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_6"/>
<visualProperty default="ROUND_RECTANGLE" name="COMPOUND_NODE_SHAPE"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_9"/>
<visualProperty default="SOLID" name="NODE_BORDER_STROKE"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_5"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_7"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_8"/>
<visualProperty default="0.0" name="NODE_X_LOCATION"/>
<visualProperty default="5.0" name="NODE_BORDER_WIDTH"/>
<visualProperty default="true" name="NODE_NESTED_NETWORK_IMAGE_VISIBLE"/>
<visualProperty default="org.cytoscape.cg.model.NullCustomGraphics,0,[ Remove Graphics ]," name="NODE_CUSTOMGRAPHICS_3"/>
<visualProperty default="175" name="NODE_LABEL_BACKGROUND_TRANSPARENCY"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_1"/>
<visualProperty default="C,C,c,0.00,0.00" name="NODE_CUSTOMGRAPHICS_POSITION_4"/>
<visualProperty default="0.0" name="NODE_DEPTH"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_7"/>
<visualProperty default="0.0" name="NODE_CUSTOMGRAPHICS_SIZE_9"/>
</node>
<edge>
<dependency value="true" name="arrowColorMatchesEdge"/>
<visualProperty default="false" name="EDGE_SELECTED"/>
<visualProperty default="255" name="EDGE_TRANSPARENCY"/>
<visualProperty default="10" name="EDGE_LABEL_FONT_SIZE"/>
<visualProperty default="#577399" name="EDGE_UNSELECTED_PAINT"/>
<visualProperty default="" name="EDGE_LABEL"/>
<visualProperty default="#FFFFFF" name="EDGE_STROKE_UNSELECTED_PAINT"/>
<visualProperty default="200.0" name="EDGE_LABEL_WIDTH"/>
<visualProperty default="#000000" name="EDGE_LABEL_COLOR"/>
<visualProperty default="SansSerif.plain,plain,10" name="EDGE_LABEL_FONT_FACE"/>
<visualProperty default="0.728545744495502,-0.684997151948455,0.6456513365424503" name="EDGE_BEND"/>
<visualProperty default="#B6B6B6" name="EDGE_LABEL_BACKGROUND_COLOR"/>
<visualProperty default="AUTO_BEND" name="EDGE_STACKING"/>
<visualProperty default="#000000" name="EDGE_TARGET_ARROW_UNSELECTED_PAINT"/>
<visualProperty default="0.5" name="EDGE_STACKING_DENSITY"/>
<visualProperty default="NONE" name="EDGE_TARGET_ARROW_SHAPE"/>
<visualProperty default="true" name="EDGE_VISIBLE"/>
<visualProperty default="C,C,c,0.00,0.00" name="EDGE_LABEL_POSITION"/>
<visualProperty default="0.0" name="EDGE_LABEL_ROTATION"/>
<visualProperty default="" name="EDGE_TOOLTIP"/>
<visualProperty default="0.0" name="EDGE_Z_ORDER"/>
<visualProperty default="#FFFF00" name="EDGE_TARGET_ARROW_SELECTED_PAINT"/>
<visualProperty default="#FF0000" name="EDGE_STROKE_SELECTED_PAINT"/>
<visualProperty default="NONE" name="EDGE_SOURCE_ARROW_SHAPE"/>
<visualProperty default="#FFFF00" name="EDGE_SOURCE_ARROW_SELECTED_PAINT"/>
<visualProperty default="false" name="EDGE_LABEL_AUTOROTATE"/>
<visualProperty default="true" name="EDGE_CURVED"/>
<visualProperty default="#000000" name="EDGE_SOURCE_ARROW_UNSELECTED_PAINT"/>
<visualProperty default="255" name="EDGE_LABEL_TRANSPARENCY"/>
<visualProperty default="6.0" name="EDGE_TARGET_ARROW_SIZE"/>
<visualProperty default="NONE" name="EDGE_LABEL_BACKGROUND_SHAPE"/>
<visualProperty default="255" name="EDGE_LABEL_BACKGROUND_TRANSPARENCY"/>
<visualProperty default="SOLID" name="EDGE_LINE_TYPE"/>
<visualProperty default="6.0" name="EDGE_SOURCE_ARROW_SIZE"/>
<visualProperty default="" name="EDGE_TOOLTIP"/>
<visualProperty default="10" name="EDGE_LABEL_FONT_SIZE"/>
<visualProperty default="255" name="EDGE_TRANSPARENCY"/>
<visualProperty default="#000000" name="EDGE_TARGET_ARROW_UNSELECTED_PAINT"/>
<visualProperty default="200.0" name="EDGE_LABEL_WIDTH"/>
<visualProperty default="SansSerif.plain,plain,10" name="EDGE_LABEL_FONT_FACE"/>
<visualProperty default="0.728545744495502,-0.684997151948455,0.6456513365424503" name="EDGE_BEND"/>
<visualProperty default="0.5" name="EDGE_STACKING_DENSITY"/>
<visualProperty default="false" name="EDGE_SELECTED"/>
<visualProperty default="3.0" name="EDGE_WIDTH">
<continuousMapping attributeName="weight" attributeType="float">
<continuousMappingPoint attrValue="0.09520000219345093" equalValue="2.0" greaterValue="2.0" lesserValue="1.0"/>
<continuousMappingPoint attrValue="1.0" equalValue="10.0" greaterValue="1.0" lesserValue="10.0"/>
</continuousMapping>
</visualProperty>
<visualProperty default="#577399" name="EDGE_UNSELECTED_PAINT"/>
<visualProperty default="6.0" name="EDGE_SOURCE_ARROW_SIZE"/>
<visualProperty default="255" name="EDGE_LABEL_BACKGROUND_TRANSPARENCY"/>
<visualProperty default="#000000" name="EDGE_LABEL_COLOR"/>
<visualProperty default="SOLID" name="EDGE_LINE_TYPE"/>
<visualProperty default="#FF0000" name="EDGE_STROKE_SELECTED_PAINT"/>
<visualProperty default="" name="EDGE_LABEL"/>
<visualProperty default="true" name="EDGE_VISIBLE"/>
<visualProperty default="255" name="EDGE_LABEL_TRANSPARENCY"/>
<visualProperty default="#000000" name="EDGE_SOURCE_ARROW_UNSELECTED_PAINT"/>
<visualProperty default="#B6B6B6" name="EDGE_LABEL_BACKGROUND_COLOR"/>
<visualProperty default="true" name="EDGE_CURVED"/>
<visualProperty default="#FFFFFF" name="EDGE_STROKE_UNSELECTED_PAINT"/>
<visualProperty default="0.0" name="EDGE_LABEL_ROTATION"/>
<visualProperty default="AUTO_BEND" name="EDGE_STACKING"/>
<visualProperty default="#FFFF00" name="EDGE_SOURCE_ARROW_SELECTED_PAINT"/>
<visualProperty default="NONE" name="EDGE_TARGET_ARROW_SHAPE"/>
<visualProperty default="NONE" name="EDGE_SOURCE_ARROW_SHAPE"/>
<visualProperty default="6.0" name="EDGE_TARGET_ARROW_SIZE"/>
<visualProperty default="NONE" name="EDGE_LABEL_BACKGROUND_SHAPE"/>
<visualProperty default="false" name="EDGE_LABEL_AUTOROTATE"/>
<visualProperty default="C,C,c,0.00,0.00" name="EDGE_LABEL_POSITION"/>
</edge>
</visualStyle>
</vizmap>

View File

@@ -1,2 +1,12 @@
class EdgePropertyNotContainedError(Exception):
"""Error raised if a needed edge property is not contained in graph edges"""
class EmptyGraphError(Exception):
"""Error raised if an operation should be performed on the graph,
but it does not contain any nodes or edges"""
class EmptyEdgesError(EmptyGraphError):
"""Error raised if action should be performed on a graph's edges, but
it does not contain any"""

View File

@@ -71,6 +71,15 @@ def encode_to_base64_str(
return b64_bytes.decode(encoding=encoding)
def encode_file_to_base64_str(
path: Path,
encoding: str = 'utf-8',
) -> str:
with open(path, 'rb') as file:
b64_bytes = base64.b64encode(file.read())
return b64_bytes.decode(encoding=encoding)
def decode_from_base64_str(
b64_str: str,
encoding: str = 'utf-8',
@@ -83,8 +92,9 @@ def decode_from_base64_str(
def get_entry_point(
saving_path: Path,
filename: str,
file_ext: str = '.pkl',
) -> Path:
entry_point_path = (saving_path / filename).with_suffix('.pkl')
entry_point_path = (saving_path / filename).with_suffix(file_ext)
if not entry_point_path.exists():
raise FileNotFoundError(
f'Could not find provided entry data under path: >>{entry_point_path}<<'

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from lang_main.analysis import graphs
from lang_main.analysis.preprocessing import (
analyse_feature,
@@ -168,39 +170,77 @@ def build_tk_graph_post_pipe() -> Pipeline:
return pipe_graph_postprocessing
def build_tk_graph_rescaling_pipe() -> Pipeline:
def build_tk_graph_rescaling_pipe(
save_result: bool,
exit_point: EntryPoints,
) -> Pipeline:
pipe_graph_rescaling = Pipeline(name='Graph_Rescaling', working_dir=SAVE_PATH_FOLDER)
pipe_graph_rescaling.add(
graphs.pipe_rescale_graph_edge_weights,
)
pipe_graph_rescaling.add(
graphs.pipe_add_graph_metrics,
save_result=True,
filename=EntryPoints.TK_GRAPH_ANALYSIS_RESCALED,
save_result=save_result,
filename=exit_point,
# filename=EntryPoints.TK_GRAPH_ANALYSIS_RESCALED,
)
return pipe_graph_rescaling
# ** token analysis: rendering
def build_tk_graph_rendering_pipe() -> Pipeline:
def build_tk_graph_render_pipe(
with_subgraphs: bool,
export_folder: Path = SAVE_PATH_FOLDER,
base_network_name: str = CYTO_BASE_NETWORK_NAME,
) -> Pipeline:
pipe_graph_rendering = Pipeline(
name='Graph_Static-Rendering',
working_dir=SAVE_PATH_FOLDER,
)
pipe_graph_rendering.add(cyto.import_to_cytoscape)
pipe_graph_rendering.add(cyto.layout_network)
pipe_graph_rendering.add(cyto.apply_style_to_network)
pipe_graph_rendering.add(
cyto.import_to_cytoscape,
{
'network_name': base_network_name,
},
)
pipe_graph_rendering.add(
cyto.layout_network,
{
'network_name': base_network_name,
},
)
pipe_graph_rendering.add(
cyto.apply_style_to_network,
{
'network_name': base_network_name,
},
)
pipe_graph_rendering.add(
cyto.export_network_to_image,
{'filename': CYTO_BASE_NETWORK_NAME},
)
pipe_graph_rendering.add(cyto.get_subgraph_node_selection)
pipe_graph_rendering.add(
cyto.build_subnetworks,
{'export_image': True},
{
'filename': base_network_name,
'target_folder': export_folder,
'network_name': base_network_name,
},
)
if with_subgraphs:
pipe_graph_rendering.add(
cyto.get_subgraph_node_selection,
{
'network_name': base_network_name,
},
)
pipe_graph_rendering.add(
cyto.build_subnetworks,
{
'export_image': True,
'target_folder': export_folder,
'network_name': base_network_name,
},
)
return pipe_graph_rendering

View File

@@ -1,7 +1,7 @@
import time
from collections.abc import Iterable
from pathlib import Path
from typing import cast
from typing import Literal, cast
import py4cytoscape as p4c
from networkx import DiGraph, Graph
@@ -55,6 +55,7 @@ def verify_connection():
def import_to_cytoscape(
graph: DiGraph | Graph,
network_name: str = CYTO_BASE_NETWORK_NAME,
) -> None:
"""Cytoscape: import NetworkX graph as new network collection
@@ -65,15 +66,49 @@ def import_to_cytoscape(
"""
logger.debug('Checking Cytoscape connection...')
verify_connection()
logger.debug('Importing network to Cytoscape...')
logger.debug('Importing to and analysing network in Cytoscape...')
p4c.delete_all_networks()
p4c.create_network_from_networkx(
graph,
title=CYTO_BASE_NETWORK_NAME,
title=network_name,
collection=CYTO_COLLECTION_NAME,
)
p4c.analyze_network(directed=False)
logger.debug('Importing network to Cytoscape successful.')
analyse_network(network_name=network_name)
logger.debug('Import and analysis of network to Cytoscape successful.')
def verify_table_property(
property: str,
table_type: Literal['node', 'edge', 'network'] = 'node',
network_name: str = CYTO_BASE_NETWORK_NAME,
) -> bool:
table = p4c.get_table_columns(table=table_type, network=network_name)
return property in table.columns
def analyse_network(
property_degree_weighted: str = PROPERTY_NAME_DEGREE_WEIGHTED,
network_name: str = CYTO_BASE_NETWORK_NAME,
) -> None:
node_table = p4c.get_table_columns(table='node', network=network_name)
net_analyse_possible: bool = True
if len(node_table) < 4:
net_analyse_possible = False
if net_analyse_possible:
p4c.analyze_network(directed=False)
node_table = p4c.get_table_columns(table='node', network=network_name)
node_table['stress_norm'] = node_table['Stress'] / node_table['Stress'].max()
node_table[CYTO_SELECTION_PROPERTY] = (
node_table[property_degree_weighted]
* node_table['BetweennessCentrality']
* node_table['stress_norm']
)
else:
node_table[CYTO_SELECTION_PROPERTY] = 1
p4c.load_table_data(node_table, data_key_column='name', network=network_name)
def reset_current_network_to_base() -> None:
@@ -83,6 +118,7 @@ def reset_current_network_to_base() -> None:
def export_network_to_image(
filename: str,
target_folder: Path = SAVE_PATH_FOLDER,
filetype: CytoExportFileTypes = 'SVG',
network_name: str = CYTO_BASE_NETWORK_NAME,
pdf_export_page_size: CytoExportPageSizes = 'A4',
@@ -102,7 +138,6 @@ def export_network_to_image(
by default 'A4'
"""
logger.debug('Exporting image to file...')
target_folder = SAVE_PATH_FOLDER
if not target_folder.exists():
target_folder.mkdir(parents=True)
file_pth = target_folder / filename
@@ -138,7 +173,8 @@ def layout_network(
necessarily match the name in the Cytoscape UI),
by default CYTO_LAYOUT_NAME
layout_properties : CytoLayoutProperties, optional
configuration of parameters for the given layout algorithm, by default CYTO_LAYOUT_PROPERTIES
configuration of parameters for the given layout algorithm,
by default CYTO_LAYOUT_PROPERTIES
network_name : str, optional
network to apply the layout algorithm on, by default CYTO_BASE_NETWORK_NAME
"""
@@ -153,6 +189,9 @@ def apply_style_to_network(
style_name: str = CYTO_STYLESHEET_NAME,
pth_to_stylesheet: Path = CYTO_PATH_STYLESHEET,
network_name: str = CYTO_BASE_NETWORK_NAME,
node_size_property: str = 'node_selection',
min_node_size: int = 15,
max_node_size: int = 40,
) -> None:
"""Cytoscape: apply a chosen Cytoscape style to the defined network
@@ -185,14 +224,36 @@ def apply_style_to_network(
p4c.import_visual_styles(str(pth_to_stylesheet))
p4c.set_visual_style(style_name, network=network_name)
time.sleep(1) # if not waited image export could be without applied style
# node size mapping, only if needed property is available
# TODO check removal
# size_prop_available = verify_table_property(
# property=node_size_property,
# network_name=network_name,
# )
# if size_prop_available:
scheme = p4c.scheme_c_number_continuous(
start_value=min_node_size, end_value=max_node_size
)
node_size_map = p4c.gen_node_size_map(
node_size_property,
number_scheme=scheme,
mapping_type='c',
style_name='lang_main',
default_number=min_node_size,
)
p4c.set_node_size_mapping(**node_size_map)
# else:
# node_table = p4c.get_table_columns(table='node', network=network_name)
# nodes_SUID = node_table['SUID'].to_list()
# p4c.set_node_size_bypass(nodes_SUID, new_sizes=min_node_size, network=network_name)
# p4c.set_visual_style(style_name, network=network_name)
# time.sleep(1) # if not waited image export could be without applied style
p4c.fit_content(selected_only=False, network=network_name)
logger.debug('Style application to network successful.')
def get_subgraph_node_selection(
network_name: str = CYTO_BASE_NETWORK_NAME,
property_degree_weighted: str = PROPERTY_NAME_DEGREE_WEIGHTED,
num_subgraphs: int = CYTO_NUMBER_SUBGRAPHS,
) -> list[CytoNodeID]:
"""Cytoscape: obtain the relevant nodes for iterative subgraph generation
@@ -214,14 +275,9 @@ def get_subgraph_node_selection(
list containing all relevant Cytoscape nodes
"""
logger.debug('Selecting nodes for subgraph generation...')
node_table = p4c.get_table_columns(network=network_name)
node_table['stress_norm'] = node_table['Stress'] / node_table['Stress'].max()
node_table[CYTO_SELECTION_PROPERTY] = (
node_table[property_degree_weighted]
* node_table['BetweennessCentrality']
* node_table['stress_norm']
)
node_table = p4c.get_table_columns(table='node', network=network_name)
node_table = node_table.sort_values(by=CYTO_SELECTION_PROPERTY, ascending=False)
p4c.load_table_data(node_table, data_key_column='name', network=network_name)
node_table_choice = node_table.iloc[:num_subgraphs, :]
logger.debug('Selection of nodes for subgraph generation successful.')
@@ -264,6 +320,7 @@ def make_subnetwork(
index: int,
network_name: str = CYTO_BASE_NETWORK_NAME,
export_image: bool = True,
target_folder: Path = SAVE_PATH_FOLDER,
) -> None:
"""Cytoscape: generate a new subnetwork based on the currently
selected nodes and edges
@@ -289,7 +346,11 @@ def make_subnetwork(
p4c.fit_content(selected_only=False, network=subnetwork_name)
if export_image:
time.sleep(1)
export_network_to_image(filename=subnetwork_name, network_name=subnetwork_name)
export_network_to_image(
filename=subnetwork_name,
target_folder=target_folder,
network_name=subnetwork_name,
)
logger.debug('Generation of subnetwork with index %d successful.', index)
@@ -298,6 +359,7 @@ def build_subnetworks(
nodes_to_analyse: Iterable[CytoNodeID],
network_name: str = CYTO_BASE_NETWORK_NAME,
export_image: bool = True,
target_folder: Path = SAVE_PATH_FOLDER,
) -> None:
"""Cytoscape: iteratively build subnetworks from a collection of nodes
and their respective neighbouring nodes
@@ -316,5 +378,10 @@ def build_subnetworks(
logger.debug('Generating all subnetworks for node selection...')
for idx, node in enumerate(nodes_to_analyse):
select_neighbours_of_node(node=node, network_name=network_name)
make_subnetwork(index=idx, network_name=network_name, export_image=export_image)
make_subnetwork(
index=idx,
network_name=network_name,
export_image=export_image,
target_folder=target_folder,
)
logger.debug('Generation of all subnetworks for node selection successful.')

View File

@@ -33,6 +33,7 @@ ResultHandling: TypeAlias = list[tuple[bool, str | None]]
class EntryPoints(enum.StrEnum):
TIMELINE = 'TIMELINE'
TIMELINE_POST = 'TIMELINE_POSTPROCESSING'
TIMELINE_TK_GRAPH_RESCALED = 'TIMELINE_TK_GRAPH_RESCALED'
TK_GRAPH_POST = 'TK-GRAPH_POSTPROCESSING'
TK_GRAPH_ANALYSIS = 'TK-GRAPH_ANALYSIS'
TK_GRAPH_ANALYSIS_RESCALED = 'TK-GRAPH_ANALYSIS_RESCALED'