From cd310e8de704f2b01e15ad515f79f1ebb619b275 Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Wed, 28 Feb 2024 22:15:11 +0100 Subject: [PATCH] Updated the graphs, added ids, made sure they are propagated among categories --- Demo_graph.ipynb | 113 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 9 deletions(-) diff --git a/Demo_graph.ipynb b/Demo_graph.ipynb index 6e6cc4bc4..c8f87cc6e 100644 --- a/Demo_graph.ipynb +++ b/Demo_graph.ipynb @@ -4382,7 +4382,7 @@ "\n", "# Convert NetworkX graph to a Pandas DataFrame\n", "edges = nx.to_pandas_edgelist(CONNECTED_GRAPH)\n", - "graphistry.register(api=3, username='Vasilije1990', password='Q@HLdgv5SMUsGxy') \n", + "graphistry.register(api=3, username='Vasilije1990', password='') \n", "\n", "# Visualize the graph\n", "graphistry.edges(edges, 'source', 'target').plot()" @@ -4408,7 +4408,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 382, "id": "36354c1f-17c3-419a-9c71-4618d2bde8ed", "metadata": {}, "outputs": [], @@ -4418,7 +4418,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 383, "id": "8cfe6574-e079-495b-b820-1b361d62c25d", "metadata": {}, "outputs": [], @@ -4453,7 +4453,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 385, "id": "17d27c7b-5a6d-4ef4-b785-76f5c239afc1", "metadata": {}, "outputs": [], @@ -4463,14 +4463,86 @@ }, { "cell_type": "code", - "execution_count": 325, + "execution_count": 388, + "id": "de42394d-7a4c-46ac-9a08-6fb2911c11b9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'id': 1, 'name': 'Social media posts and comments', 'data_type': 'TEXT'},\n", + " {'id': 2, 'name': 'Conversational Data', 'data_type': 'TEXT'},\n", + " {'id': 3, 'name': 'Personal narratives and stories', 'data_type': 'TEXT'}]" + ] + }, + "execution_count": 388, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "relevant_categories.dict()['cognitive_subgroups']" + ] + }, + { + "cell_type": "code", + "execution_count": 445, "id": "217fcdd1-e1f7-48f3-a835-cfd003bd6da9", "metadata": {}, "outputs": [], "source": [ - "def get_relevant_content_categories_from_graph(graph, weight_threshold=0.9):\n", + "def get_relevant_content_categories_from_graph(graph, classifier_categories, weight_threshold=0.9):\n", " \"\"\" Gets relevant content categories from the graph \"\"\"\n", - " pass" + " filtered_nodes = []\n", + " target_neighbors = []\n", + "\n", + " # Iterate through each node and its attributes in the graph\n", + " for node, attrs in graph.nodes(data=True):\n", + " for category in relevant_categories.dict()['cognitive_subgroups']:\n", + "\n", + " if category['name'] in node:\n", + " print(node)\n", + "\n", + " target_relationship_type = 'detail'\n", + " \n", + " # Initialize a list to hold the neighbors connected by the target relationship type\n", + " \n", + " \n", + " # Iterate over all edges connected to the target node\n", + " for neighbor in graph.neighbors(node):\n", + " \n", + " # Get the edge data between target node and its neighbor\n", + " edge_data = graph.get_edge_data(node, neighbor)\n", + "\n", + " try:\n", + " val= edge_data[0]['relationship']\n", + " \n", + " except:\n", + " val = ''\n", + " \n", + " # Check if the edge has the desired relationship type\n", + " if val == target_relationship_type:\n", + " target_neighbors.append(neighbor)\n", + " return target_neighbors\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 447, + "id": "f974c87e-4117-4f93-a96c-e4fcd741aed9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Natural Language Text:Personal narratives and stories\n" + ] + } + ], + "source": [ + "example_context = get_relevant_content_categories_from_graph(CONNECTED_GRAPH, relevant_categories)" ] }, { @@ -4503,11 +4575,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 400, "id": "cca2637a-eace-4763-ada4-0ce925afd7ce", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "target_node_id = 'Natural Language Text:Personal narratives and stories'\n", + "\n", + "# Use the neighbors() function to get all nodes connected to the target node\n", + "connected_nodes = list(CONNECTED_GRAPH.neighbors(target_node_id))" + ] + }, + { + "cell_type": "code", + "execution_count": 401, + "id": "e67c6679-f339-4ad1-b1bc-0c896a973abe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Lee Parkin, 50, owner of the late terrier-spaniel cross Izzy, suffers from PTSD - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 493b9555-a150-4b2f-9dbc-0b3f699f7249', 'Izzy, a terrier-spaniel cross owned by Lee Parkin, killed by an XL bully - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 646af2be-f8ac-45f3-beba-b3ce21bfa019', 'An XL bully attacked and killed Izzy during a walk with Lee Parkin in Doncaster - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 3774e1cd-3728-419b-a2a7-7b89c4522366', 'Doncaster, the location where Izzy was attacked and killed by an XL bully - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 967f6c80-01f0-4a96-860d-cff5cdc4a5bc', \"Naevia, Marie Hay's Siberian husky, survived an attack by two XL bullies with life-changing injuries - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - c72d5b7c-67be-43b8-b3e8-feccd8ba4a7d\", 'Marie Hay, owner of Naevia, who survived an XL bully attack with mental and physical scars - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 996ac877-2ddd-45c3-b7ba-7aba6bcd0f81', 'Two XL bullies attacked Naevia on a beach in Redcar, causing severe injuries - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 5bd9aee2-5f56-45ef-abee-b952e013f772', 'Redcar Beach on the North Yorkshire coast, where Naevia was attacked by two XL bullies - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 18209f5f-6965-4029-97d0-8f0a6e4121c2', 'XL bully, a breed involved in attacks on Izzy and Naevia, seen as dangerously out of control - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 4f8a3746-3c0d-4e3f-badb-55f89270aa05', 'An incoming ban on XL bullies supported by Lee Parkin and Marie Hay but with reservations on its effectiveness - 269f0df8-8091-442c-9392-0b710e4dc350 - 27a76d52-5b5e-4e8e-8726-29be55c8b2f1 - 2f4d11f2-6e19-4ec6-a655-7a3ed7d9329a', 'Lee Parkin - 50-year-old dog owner, suffers from post-traumatic stress disorder after his dog was killed. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - 1b71310b-7bae-4f7a-953e-756cce8f880f', 'Izzy - Terrier-spaniel cross, pet dog owned by Lee Parkin for nearly 10 years, killed in dog attack. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - 69dea689-2e52-4c6c-ab25-2621ef27c6d3', 'Doncaster - The location near where Lee Parkin was walking his dog Izzy when she was attacked. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - a9cb9715-8f25-45cd-beca-71ea3afcb28c', 'XL Bully - Breed of dog that attacked and killed Izzy, described as dangerously out of control. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - 3bd1d070-b76c-4bc4-948c-0cb8f608e49e', 'Marie Hay - Dog owner whose Siberian Husky, Naevia, survived an attack but suffered life-changing injuries. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - 0fc6079d-5b2c-4d90-8e05-69a5a5109d7c', 'Naevia - Siberian Husky owned by Marie Hay, survived a savage attack by two XL Bullies. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - 5a6df745-7ecd-4ac9-8f6a-5f1dc547a9ac', 'Redcar Beach on the North Yorkshire coast - The location where Naevia was attacked by two XL Bullies. - 269f0df8-8091-442c-9392-0b710e4dc350 - c306c248-bbab-44a9-9ae2-448f992e415c - e39ec471-29bf-4581-8542-b358300312a4', 'Lee Parkin, a 50-year-old man and owner of the dog Izzy. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - 606800f1-fc9f-43a5-997c-4966b0b77fe8', 'Izzy, a terrier-spaniel cross owned by Lee Parkin for nearly 10 years. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - e44e53b3-120f-4f63-b20f-a60bb24c3ef2', 'Attack by an XL bully on Izzy resulting in her death. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - 855a05eb-c5ca-434e-a25c-534a608b2568', 'Lee Parkin was diagnosed with post-traumatic stress disorder after the attack. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - 6513305f-ca46-4708-b869-6688fa6552c5', 'Marie Hay, owner of seven-year-old Siberian husky Naevia, also victim of an XL bully attack. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - b2a03c68-609a-4839-a85f-b586be5d2e8f', 'Naevia, a seven-year-old Siberian husky owned by Marie Hay, survived an attack by two XL bullies with life-changing injuries. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - 9d464c7c-16ff-4081-b630-f3288519ccfc', 'The incoming ban on XL bullies that both Lee Parkin and Marie Hay support. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - 61d8f944-8c52-4ee3-8252-a5ae2c898d12', 'Anxiety and fear faced by Lee Parkin and Marie Hay when walking dogs post-attack. - 269f0df8-8091-442c-9392-0b710e4dc350 - dbcbc6c6-db82-48f3-8fa9-e3d372ff3483 - f8cccf2f-0b34-4dd3-92b0-8ac8a34c5c0a']\n" + ] + } + ], + "source": [ + "print(connected_nodes)" + ] }, { "cell_type": "code",