added new graph metrics

This commit is contained in:
Florian Förster
2024-12-19 16:26:01 +01:00
parent 123869e203
commit 80a35c4658
24 changed files with 826 additions and 97 deletions

View File

@@ -155,7 +155,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 1,
"id": "0a48d11d-1f2b-475e-9ddf-bb9a3f67accb",
"metadata": {},
"outputs": [],
@@ -165,53 +165,471 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 2,
"id": "e340377a-0df4-44ca-b18e-8b354e273eb9",
"metadata": {},
"outputs": [],
"source": [
"save_pth = Path.cwd() / 'test.graphml'"
"save_pth = Path.cwd() / 'tk_graph_built.pkl'\n",
"pth_export = Path.cwd() / 'tk_graph_built'\n",
"assert save_pth.exists()"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "66677ad0-a1e5-4772-a0ba-7fbeeda55297",
"execution_count": 3,
"id": "8aba87b2-e924-4748-98c6-05c4676f3c08",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking iteratively for config file. Start: A:\\Arbeitsaufgaben\\lang-main\\src\\lang_main, stop folder: src\n",
"Loaded TOML config file successfully.\n",
"Loaded config from: >>A:\\Arbeitsaufgaben\\lang-main\\lang_main_config.toml<<\n",
"Library path is: A:\\Arbeitsaufgaben\\lang-main\n",
"Root path is: A:\\Arbeitsaufgaben\n"
]
}
],
"source": [
"nx.write_graphml(G, save_pth)"
"from lang_main import io"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "f01ebe25-56b9-410a-a2bf-d5a6e211de7a",
"execution_count": 5,
"id": "728fdce3-cfe0-4c4b-bcbf-c3d61547e94f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-12-19 12:24:12 +0000 | lang_main:io:INFO | Loaded file successfully.\n"
]
}
],
"source": [
"G_load = nx.read_graphml(save_pth, node_type=int)"
"G = io.load_pickle(save_pth)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "10bfad35-1f96-41a1-9014-578313502e6c",
"execution_count": 6,
"id": "fcb74134-4192-4d68-a535-f5a502d02b67",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"OutEdgeView([(1, 2), (1, 3), (1, 4), (2, 4), (2, 1), (3, 4)])"
"<networkx.classes.graph.Graph at 0x222d01224d0>"
]
},
"execution_count": 85,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G_load.edges"
"G.undirected"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "024560e5-373a-46f4-b0f7-7794535daa78",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"id": "65745c09-e834-47c9-b761-aabd5fa03e57",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-12-19 11:41:58 +0000 | lang_main:graphs:INFO | Successfully saved graph as GraphML file under A:\\Arbeitsaufgaben\\lang-main\\notebooks\\tk_graph_built.graphml.\n"
]
}
],
"source": [
"G.to_GraphML(Path.cwd(), 'tk_graph_built')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "8da93a34-7bb1-4b9c-851b-0793c2c483bf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TokenGraph(name: TokenGraph, number of nodes: 13, number of edges: 9)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "1cc98c6e-4fa5-49ed-bb97-c62d8e103ccc",
"metadata": {},
"outputs": [],
"source": [
"G_copy = G.copy()\n",
"G_copy = G_copy.undirected"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a5044a6f-8903-4e33-a56e-abf647019d8a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'degree_weighted': 2}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 4}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 2}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n",
"{'degree_weighted': 1}\n"
]
}
],
"source": [
"for node in G_copy.nodes:\n",
" print(G_copy.nodes[node])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "720015a4-7338-4fce-ac6c-38e691e0efa4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 76,
"id": "b21a34c8-5748-42b1-947e-76c3ba02a240",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 77,
"id": "1bcd4a31-2ea5-4123-8fc3-27017acd7259",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'weight': 1}\n",
"{'weight': 1}\n",
"{'weight': 20}\n",
"{'weight': 15}\n",
"{'weight': 10}\n",
"{'weight': 6}\n",
"{'weight': 1}\n",
"{'weight': 1}\n",
"{'weight': 1}\n"
]
}
],
"source": [
"for edge in G_copy.edges:\n",
" print(G_copy.edges[edge])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfe5bacb-1f51-45d8-b920-ca9cba01282c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 32,
"id": "48e14d11-0319-47a9-81be-c696f69d55b3",
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "479deea9-a44f-48eb-95d3-7a326a83f62c",
"metadata": {},
"outputs": [],
"source": [
"mapping = nx.betweenness_centrality(G_copy, normalized=True)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "2299dc98-0a82-4009-8b38-790ffe3a3fd8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Kontrolle': 0.015151515151515152,\n",
" 'Lichtschranke': 0.0,\n",
" 'Überprüfung': 0.09090909090909091,\n",
" 'Spannrolle': 0.0,\n",
" 'Druckventil': 0.0,\n",
" 'Schmiernippel': 0.0,\n",
" 'Inspektion': 0.015151515151515152,\n",
" 'Förderbänder': 0.0,\n",
" 'Reinigung': 0.0,\n",
" 'Luftfilter': 0.0,\n",
" 'Schutzabdeckung': 0.0,\n",
" 'Ölstand': 0.0,\n",
" 'Hydraulik': 0.0}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mapping"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "4c00dfe4-29d3-45d8-9eec-6d8e1fbda910",
"metadata": {},
"outputs": [],
"source": [
"nx.set_node_attributes(G_copy, mapping, name='BC')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "0e1119e1-700d-4ae0-bb36-6d4a8fe57698",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'degree_weighted': 2, 'BC': 0.015151515151515152}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 4, 'BC': 0.09090909090909091}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 2, 'BC': 0.015151515151515152}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0}\n"
]
}
],
"source": [
"nodes_prop_mapping = {}\n",
"for node in G_copy.nodes:\n",
" node_data = G_copy.nodes[node]\n",
" prio = node_data['degree_weighted'] * node_data['BC']\n",
" nodes_prop_mapping[node] = prio\n",
" print(G_copy.nodes[node])"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "2970267e-5c9e-472f-857b-382e2406f34d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Kontrolle': 0.030303030303030304,\n",
" 'Lichtschranke': 0.0,\n",
" 'Überprüfung': 0.36363636363636365,\n",
" 'Spannrolle': 0.0,\n",
" 'Druckventil': 0.0,\n",
" 'Schmiernippel': 0.0,\n",
" 'Inspektion': 0.030303030303030304,\n",
" 'Förderbänder': 0.0,\n",
" 'Reinigung': 0.0,\n",
" 'Luftfilter': 0.0,\n",
" 'Schutzabdeckung': 0.0,\n",
" 'Ölstand': 0.0,\n",
" 'Hydraulik': 0.0}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nodes_prop_mapping"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "ef2a1065-9a47-4df4-a7d9-ccfe30dbcdc7",
"metadata": {},
"outputs": [],
"source": [
"nx.set_node_attributes(G_copy, nodes_prop_mapping, name='prio')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "2b77597a-be7a-498a-8374-f98324574619",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'degree_weighted': 2, 'BC': 0.015151515151515152, 'prio': 0.030303030303030304}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 4, 'BC': 0.09090909090909091, 'prio': 0.36363636363636365}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 2, 'BC': 0.015151515151515152, 'prio': 0.030303030303030304}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n",
"{'degree_weighted': 1, 'BC': 0.0, 'prio': 0.0}\n"
]
}
],
"source": [
"for node in G_copy.nodes:\n",
" print(G_copy.nodes[node])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "089ae831-f307-473d-8a70-e72bc580ca61",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "81d9e8ad-4372-4e3b-a42b-e5b343802936",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ea0a1fc3-571f-4ce2-8561-b0a8f93e5072",
"metadata": {},
"outputs": [],
"source": [
"def build_init_graph():\n",
" edge_weights = [\n",
" {'weight': 1},\n",
" {'weight': 2},\n",
" {'weight': 3},\n",
" {'weight': 4},\n",
" {'weight': 5},\n",
" {'weight': 6},\n",
" ]\n",
" edges = [\n",
" (1, 2),\n",
" (1, 3),\n",
" (2, 4),\n",
" (3, 4),\n",
" (1, 4),\n",
" (2, 1),\n",
" ]\n",
" edges_to_add = []\n",
" for i, edge in enumerate(edges):\n",
" edge = list(edge)\n",
" edge.append(edge_weights[i]) # type: ignore\n",
" edges_to_add.append(tuple(edge))\n",
"\n",
" G = nx.DiGraph()\n",
"\n",
" G.add_edges_from(edges_to_add)\n",
"\n",
" return G"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6e97af5f-f629-4c71-a1e4-2310ad2f3caa",
"metadata": {},
"outputs": [],
"source": [
"G_init = build_init_graph()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "4ff8fb6b-a10b-448f-b8a5-9989f4bee81e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 0.16666666666666666, 2: 0.0, 3: 0.0, 4: 0.0}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.betweenness_centrality(G_init, normalized=True)"
]
},
{

View File

@@ -0,0 +1,73 @@
<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="d1" for="edge" attr.name="weight" attr.type="long" />
<key id="d0" for="node" attr.name="degree_weighted" attr.type="long" />
<graph edgedefault="undirected">
<node id="Kontrolle">
<data key="d0">2</data>
</node>
<node id="Lichtschranke">
<data key="d0">1</data>
</node>
<node id="Überprüfung">
<data key="d0">4</data>
</node>
<node id="Spannrolle">
<data key="d0">1</data>
</node>
<node id="Druckventil">
<data key="d0">1</data>
</node>
<node id="Schmiernippel">
<data key="d0">1</data>
</node>
<node id="Inspektion">
<data key="d0">2</data>
</node>
<node id="Förderbänder">
<data key="d0">1</data>
</node>
<node id="Reinigung">
<data key="d0">1</data>
</node>
<node id="Luftfilter">
<data key="d0">1</data>
</node>
<node id="Schutzabdeckung">
<data key="d0">1</data>
</node>
<node id="Ölstand">
<data key="d0">1</data>
</node>
<node id="Hydraulik">
<data key="d0">1</data>
</node>
<edge source="Kontrolle" target="Lichtschranke">
<data key="d1">1</data>
</edge>
<edge source="Kontrolle" target="Schmiernippel">
<data key="d1">1</data>
</edge>
<edge source="Überprüfung" target="Spannrolle">
<data key="d1">1</data>
</edge>
<edge source="Überprüfung" target="Druckventil">
<data key="d1">1</data>
</edge>
<edge source="Überprüfung" target="Ölstand">
<data key="d1">1</data>
</edge>
<edge source="Überprüfung" target="Hydraulik">
<data key="d1">1</data>
</edge>
<edge source="Inspektion" target="Förderbänder">
<data key="d1">1</data>
</edge>
<edge source="Inspektion" target="Schutzabdeckung">
<data key="d1">1</data>
</edge>
<edge source="Reinigung" target="Luftfilter">
<data key="d1">1</data>
</edge>
</graph>
</graphml>

Binary file not shown.