Skip to content

Commit

Permalink
Merge pull request #10 from OKN-CollabNext/add-work-nodes
Browse files Browse the repository at this point in the history
Add work nodes
  • Loading branch information
lewlefton committed Apr 16, 2024
2 parents 0d8c1e8 + c108141 commit 042f5eb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
17 changes: 16 additions & 1 deletion collabnext/openalex/edges.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyalex import Author, Institution
from pyalex import Author, Institution, Work


def make_associated_institution_edges(institutions: list[Institution]) -> list[dict]:
Expand Down Expand Up @@ -29,3 +29,18 @@ def make_affiliated_author_edges(authors: list[Author]) -> list[dict]:
for x in authors
for y in x["affiliations"]
]


def make_work_author_edges(works: list[Work]) -> list[dict]:
return [
{
"id": f"{work['id']}-{authorship['author']['id']}",
"start": work['id'],
"end": authorship['author']['id'],
"label": "AUTHORED",
"start_type": "WORK",
"end_type": "AUTHOR"
}
for work in works
for authorship in work.get('authorships', [])
]
8 changes: 7 additions & 1 deletion collabnext/openalex/nodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyalex import Author, Institution
from pyalex import Author, Institution, Work


def make_institution_nodes(institutions: list[Institution]) -> list[dict]:
Expand All @@ -12,3 +12,9 @@ def make_author_nodes(authors: list[Author]) -> list[dict]:
return [
{"id": x["id"], "label": x["display_name"], "type": "AUTHOR"} for x in authors
]


def make_work_nodes(works: list[Work]) -> list[dict]:
return [
{"id": x["id"], "label": x["title"], "type": "WORK"} for x in works
]
8 changes: 8 additions & 0 deletions collabnext/openalex/works.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pyalex import Works, Work, Author

def get_works_by_authors(authors: list[Author]) -> list[Work]:
works_by_authors = []
for author in authors:
works = Works().filter(authorships={"author": {"id": author["id"]}}).get()
works_by_authors.extend(works)
return works_by_authors
23 changes: 19 additions & 4 deletions observable/docs/data/graph.json.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import json

from collabnext.openalex.authors import get_affiliated_authors
from collabnext.openalex.works import get_works_by_authors
from collabnext.openalex.edges import (
make_affiliated_author_edges,
make_associated_institution_edges,
make_work_author_edges,
)
from collabnext.openalex.institutions import (
dedup_institutions,
get_associated_institutions,
get_institutions,
)
from collabnext.openalex.nodes import make_author_nodes, make_institution_nodes
from collabnext.openalex.nodes import (
make_author_nodes,
make_institution_nodes,
make_work_nodes,
)

# Get institutions
institutions = get_institutions()
Expand All @@ -34,8 +40,17 @@
associated_institution_edges = make_associated_institution_edges(institutions)
affiliated_author_edges = make_affiliated_author_edges(authors)

# Get works by authors
works = get_works_by_authors(authors)

# Create work nodes
work_nodes = make_work_nodes(works)

# Create work author edges
work_author_edges = make_work_author_edges(works)

# Group all nodes and edges together
nodes = [*institution_nodes, *author_nodes]
edges = [*associated_institution_edges, *affiliated_author_edges]
nodes = [*institution_nodes, *author_nodes, *work_nodes]
edges = [*associated_institution_edges, *affiliated_author_edges, *work_author_edges]

print(json.dumps({"nodes": nodes, "edges": edges}))
print(json.dumps({"nodes": nodes, "edges": edges}))
9 changes: 9 additions & 0 deletions observable/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ orb.data.setDefaultStyle({
color: "#0df2c9",
zIndex: 1,
};
}

if (node.data.type === "WORK") {
return {
...basicStyle,
size: 10,
color: "#245cc3",
zIndex: 1,
};
}

return {
Expand Down

0 comments on commit 042f5eb

Please sign in to comment.