dynamic properties via JS in dash-cytoscape

This commit is contained in:
Florian Förster
2024-06-12 16:06:30 +02:00
parent b3e35e7dd1
commit c2714b8060
6 changed files with 328 additions and 114 deletions

View File

@@ -130,7 +130,7 @@ def convert_graph_to_cytoscape(
cyto_data: list[CytoscapeData] = []
# iterate over nodes
nodes = cast(Iterable[NodeTitle], graph.nodes)
for i, node in enumerate(nodes):
for node in nodes:
node_data: CytoscapeData = {
'data': {
'id': node,
@@ -151,7 +151,7 @@ def convert_graph_to_cytoscape(
],
graph.edges.data('weight', default=1), # type: ignore
)
for i, (source, target, weight) in enumerate(edges):
for source, target, weight in edges:
weights.add(weight)
edge_data: CytoscapeData = {
'data': {
@@ -288,27 +288,35 @@ class TokenGraph(DiGraph):
def filter_by_edge_weight(
self,
threshold: int,
bound_lower: int | None,
bound_upper: int | None,
) -> Self:
"""filters all edges which are below the given threshold
"""filters all edges which are within the provided bounds
Parameters
----------
threshold : int
edges with weights smaller than this value will be removed
bound_lower : int | None
lower bound for edge weights, edges with weight equal to this value are retained
bound_upper : int | None
upper bound for edge weights, edges with weight equal to this value are retained
Returns
-------
Self
a copy of the graph with filtered edges
"""
# filter edges by weight
original_graph_edges = copy.deepcopy(self.edges)
filtered_graph = self.copy()
if not any([bound_lower, bound_upper]):
logger.warning('No bounds provided, returning original graph.')
return filtered_graph
for edge in original_graph_edges:
weight = typing.cast(int, filtered_graph[edge[0]][edge[1]]['weight'])
if weight < threshold:
if bound_lower is not None and weight < bound_lower:
filtered_graph.remove_edge(edge[0], edge[1])
if bound_upper is not None and weight > bound_upper:
filtered_graph.remove_edge(edge[0], edge[1])
if filtered_graph._undirected is not None:
@@ -320,14 +328,17 @@ class TokenGraph(DiGraph):
def filter_by_node_degree(
self,
threshold: int,
bound_lower: int | None,
bound_upper: int | None,
) -> Self:
"""filters all nodes which have a degree below the given threshold
"""filters all nodes which are within the provided bounds by their degree
Parameters
----------
threshold : int
nodes with a degree smaller than this value will be removed
bound_lower : int | None
lower bound for node degree, nodes with degree equal to this value are retained
bound_upper : int | None
upper bound for node degree, nodes with degree equal to this value are retained
Returns
-------
@@ -338,9 +349,15 @@ class TokenGraph(DiGraph):
original_graph_nodes = copy.deepcopy(self.nodes)
filtered_graph = self.copy()
if not any([bound_lower, bound_upper]):
logger.warning('No bounds provided, returning original graph.')
return filtered_graph
for node in original_graph_nodes:
degree = filtered_graph.degree[node] # type: ignore
if degree < threshold:
if bound_lower is not None and degree < bound_lower:
filtered_graph.remove_node(node)
if bound_upper is not None and degree > bound_upper:
filtered_graph.remove_node(node)
if filtered_graph._undirected is not None:

View File

@@ -21,7 +21,7 @@ class STFRDeviceTypes(enum.StrEnum):
GPU = 'cuda'
# ** datatsets
# ** datasets
PandasIndex: TypeAlias = int | np.int64
ObjectID: TypeAlias = int
Embedding: TypeAlias = SpacyDoc | Tensor