Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CiviLink model and Graph API #1510

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1,286 changes: 605 additions & 681 deletions poetry.lock

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions project/core/management/commands/load_graph_dummy_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# data_loader.py
from django.core.management.base import BaseCommand
from threads.models import Civi, CiviLink, Thread # Adjust import to your app's name

from django.contrib.auth import get_user_model

User = get_user_model()


class Command(BaseCommand):
help = "Load dummy data for Civis and CiviLinks"

def handle(self, *args, **kwargs):
# Create a thread
thread, _ = Thread.objects.get_or_create(
title="Net Neutrality",
)
user = User.objects.first()
# Create dummy Civis
civi1 = Civi.objects.create(
title="Civi 1: Importance of Net Neutrality",
body="Net neutrality ensures that all users have equal access to information and services online.",
author=user,
votes_pos=10,
votes_neg=2,
thread=thread,
)

civi2 = Civi.objects.create(
title="Civi 2: Risks of Removing Net Neutrality",
body="Without net neutrality, ISPs could prioritize their own content or the content of those who pay for faster access.",
author=user,
votes_pos=15,
votes_neg=1,
thread=thread,
)

civi3 = Civi.objects.create(
title="Civi 3: Public Opinion on Net Neutrality",
body="A significant portion of the public supports net neutrality regulations to protect free internet access.",
author=user,
votes_pos=20,
votes_neg=3,
thread=thread,
)

# Create dummy CiviLinks
CiviLink.objects.create(
from_civi=civi1, to_civi=civi2, relation_type="response"
)

CiviLink.objects.create(
from_civi=civi2, to_civi=civi1, relation_type="rebuttal"
)

CiviLink.objects.create(from_civi=civi1, to_civi=civi3, relation_type="support")

CiviLink.objects.create(
from_civi=civi3, to_civi=civi2, relation_type="challenge"
)

self.stdout.write(self.style.SUCCESS("Successfully loaded dummy data"))
2 changes: 2 additions & 0 deletions project/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from core.router import CiviWikiRouter
from django.conf import settings
from django.conf.urls.static import static
Expand All @@ -25,6 +26,7 @@
path("admin/", admin.site.urls),
path("api/v1/", include(CiviWikiRouter.urls)),
path("api/", include("threads.urls.api")),
path("api/", include("threads.urls.graph_api")),
path("", include("accounts.urls")),
path("", include("threads.urls.urls")),
path(
Expand Down
87 changes: 87 additions & 0 deletions project/threads/graph_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .graphs import (
load_graph_from_db,
most_caused_problems,
most_effective_solution,
shortest_path_problem_to_solution,
)


@api_view(["GET"])
def get_most_caused_problem(request):
with_score = request.GET.get("with_score", False)
G = load_graph_from_db(with_score)
problem = most_caused_problems(G)
if problem:
if with_score:
return Response(
{
"problem": G.nodes[problem]["label"],
"score": G.nodes[problem]["score"],
}
)
else:
return Response({"problem": G.nodes[problem]["label"]})
return Response({"error": "No problem found"}, status=404)


@api_view(["GET"])
def get_most_effective_solution(request):
with_score = request.GET.get("with_score", False)
G = load_graph_from_db(with_score)
solution = most_effective_solution(G)
if solution:
if with_score:
return Response(
{
"solution": G.nodes[solution]["label"],
"score": G.nodes[solution]["score"],
}
)
else:
return Response({"solution": G.nodes[solution]["label"]})
return Response({"error": "No solution found"}, status=404)


@api_view(["GET"])
def get_shortest_path(request, problem_id, solution_id):
with_score = request.GET.get("with_score", False)
G = load_graph_from_db(with_score)
path = shortest_path_problem_to_solution(G, int(problem_id), int(solution_id))
if path:
path_labels = [G.nodes[node]["label"] for node in path]
return Response({"path_labels": path_labels, "path":path})
return Response({"error": "No path found"}, status=404)




@api_view(['GET'])
def get_graph_data(request):
G = load_graph_from_db() # Load the graph from your DB or other data source
nodes = []
edges = []

# Format nodes and edges to match Cytoscape format
for node, attr in G.nodes(data=True):
nodes.append({
'data': {
'id': node,
'label': attr.get('label', node),
'type': attr.get('type'),
'score': attr.get('score', 0),
}
})

for source, target, attr in G.edges(data=True):
edges.append({
'data': {
'id': f'{source}-{target}',
'source': source,
'target': target,
'label': attr.get('label', 'related')
}
})

return Response({'nodes': nodes, 'edges': edges})
42 changes: 42 additions & 0 deletions project/threads/graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import networkx as nx
from .models import Civi, CiviLink


def load_graph_from_db(with_score:bool=False):
# Create a directed graph
G = nx.DiGraph()

# Add all Civis as nodes
for civi in Civi.objects.all():
node_args, node_kwargs = civi.__node__(with_score=with_score)
G.add_node(*node_args, **node_kwargs)

# Add CiviLinks as edges
for link in CiviLink.objects.all():
edge_args, edge_kwargs = link.__edge__()
G.add_edge(*edge_args, **edge_kwargs)

return G


def most_caused_problems(G, with_score:bool=False):
problem_nodes = [n for n, attr in G.nodes(data=True) if attr["type"] == "Problem"]
if with_score:
return max(problem_nodes, key=lambda n: (G.in_degree(n), G.nodes[n]['score']), default=None)
return max(problem_nodes, key=lambda n: G.in_degree(n), default=None)


def most_effective_solution(G, with_score:bool=False):
solution_nodes = [n for n, attr in G.nodes(data=True) if attr["type"] == "Solution"]
if with_score:
return max(solution_nodes, key=lambda n: (G.out_degree(n), G.nodes[n]['score']), default=None)
return max(solution_nodes, key=lambda n: G.out_degree(n), default=None)


def shortest_path_problem_to_solution(G, problem_id, solution_id, with_score:bool=False):
try:
if with_score:
return nx.shortest_path(G, source=problem_id, target=solution_id, weight='weight')
return nx.shortest_path(G, source=problem_id, target=solution_id)
except nx.NetworkXNoPath:
return None
56 changes: 56 additions & 0 deletions project/threads/migrations/0008_civilink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Generated by Django 4.1.2 on 2024-10-18 05:12

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("threads", "0007_alter_activity_civi_alter_activity_thread_and_more"),
]

operations = [
migrations.CreateModel(
name="CiviLink",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"relation_type",
models.CharField(
choices=[
("causes", "Causes"),
("solves", "Solves"),
("related", "Related"),
],
max_length=50,
),
),
("created", models.DateTimeField(auto_now_add=True)),
(
"from_civi",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="outgoing_links",
to="threads.civi",
),
),
(
"to_civi",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="incoming_links",
to="threads.civi",
),
),
],
),
]
2 changes: 1 addition & 1 deletion project/threads/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0007_alter_activity_civi_alter_activity_thread_and_more
0008_civilink
81 changes: 81 additions & 0 deletions project/threads/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
from calendar import month_name
from typing import Any, Dict, List, Tuple

from categories.models import Category
from common.utils import PathAndRename
Expand Down Expand Up @@ -236,6 +237,7 @@ class Civi(models.Model):
title = models.CharField(max_length=255, blank=False, null=False)
body = models.CharField(max_length=1023, blank=False, null=False)

# todo rename to civi_type??
c_type = models.CharField(max_length=31, default="problem", choices=CIVI_TYPES)

votes_vneg = models.IntegerField(default=0)
Expand All @@ -250,6 +252,34 @@ def __str__(self):
def __unicode__(self):
return self.title

def __node__(self, with_score=False) -> Tuple[List[int], Dict[str, Any]]:
"""
Documentation:
Add to a graph like this:
```python
import networkx as nx
G = nx.DiGraph()
a_civi = Civi.objects.first()
node_args, node_kwargs = a_civi.__node__()
G.add_node(*node_args, **node_kwargs)
```
Args:
with_score (bool, optional): Defaults to False.

Returns:
Tuple[List[int], Dict[str, Any]]: The args and kwargs to be sent to the nx graph `add_node` function
"""
node_kwargs = {
'label':self.title,
'type':self.c_type
}
if with_score:
node_kwargs.update({"score":self.score(), "weight":self.__weight__()})
return [self.id], node_kwargs

def __weight__(self):
return 1 / (1 + self.score())

def _get_votes(self):
activity_votes = Activity.objects.filter(civi=self)

Expand Down Expand Up @@ -395,6 +425,57 @@ def dict_with_score(self, requested_user_id=None):
return data


class CiviLink(models.Model):
"""Extend the existing model to support a graph structure
(i.e., Problem -> Causes -> Problem -> solved_by -> Solution),


Args:
models (_type_): _description_

Returns:
_type_: _description_
"""
RELATION_TYPE_CHOICES = (
('causes', 'Causes'),
('solves', 'Solves'),
('related', 'Related'), # Optional, for general relationships
)

from_civi = models.ForeignKey(Civi, on_delete=models.CASCADE, related_name='outgoing_links')
to_civi = models.ForeignKey(Civi, on_delete=models.CASCADE, related_name='incoming_links')
relation_type = models.CharField(max_length=50, choices=RELATION_TYPE_CHOICES)

created = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f"{self.from_civi.title} {self.get_relation_type_display()} {self.to_civi.title}"

def __edge__(self) -> Tuple[list, Dict[str, str]]:
"""
Documentation:
Add to a graph like this:
```python
import networkx as nx
G = nx.DiGraph()
a_link = CiviLink.objects.first()
edge_args, edge_kwargs = a_link.__edge__()
G.add_edge(*edge_args, **edge_kwargs)
```

Returns:
Tuple[list, Dict[str, str]]: the first is the args, the second the kwargs to the nx graph `add_edge` function
"""
# link.from_civi.id, link.to_civi.id, relation=link.relation_type
edge_kwargs = {
"relation":self.relation_type
}
edge_args = [self.from_civi.id, self.to_civi.id]
return edge_args, edge_kwargs




class Response(models.Model):
author = models.ForeignKey(
get_user_model(),
Expand Down
Loading