Skip to content

Commit

Permalink
Merge pull request #6 from OKN-CollabNext/add-author-nodes
Browse files Browse the repository at this point in the history
Add author nodes and affiliation edges
  • Loading branch information
AbigailDawson authored Apr 15, 2024
2 parents 67d2289 + a8743ec commit 5b38b33
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
57 changes: 50 additions & 7 deletions observable/docs/data/graph.json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pyalex
from dotenv import load_dotenv
from pyalex import Institutions
from pyalex import Authors, Institutions

# Load Secrets
load_dotenv()
Expand All @@ -19,18 +19,61 @@
y for x in institutions for y in x["associated_institutions"]
]

# Combine all institutions
all_institutions = [*institutions, *associated_institutions]
# Combine all unique institutions
seen = set()
all_institutions = [
x
for x in [*institutions, *associated_institutions]
if not (x["id"] in seen or seen.add(x["id"]))
]

# Create nodes
nodes = [{"id": x["id"], "label": x["display_name"]} for x in all_institutions]
institution_nodes = [
{"id": x["id"], "label": x["display_name"], "type": "INSTITUTION"}
for x in all_institutions
]

# Get unique affiliated authors
seen = set()
authors = [
y
for x in all_institutions
for y in Authors().filter(affiliations={"institution": {"id": x["id"]}}).get()
if not (y["id"] in seen or seen.add(y["id"]))
]

# Get unique authors affiliated with each institution
author_nodes = [
{"id": x["id"], "label": x["display_name"], "type": "AUTHOR"} for x in authors
]

nodes = [*institution_nodes, *author_nodes]

# Create associated institution edges
edges = [
{"id": x["id"], "start": x["id"], "end": y["id"], "label": "ASSOCIATED"}
associated_institution_edges = [
{
"id": f"""{x["id"]}-{y["id"]}""",
"start": x["id"],
"end": y["id"],
"label": "ASSOCIATED",
"start_type": "INSTITUTION",
"end_type": "INSTITUTION",
}
for x in institutions
for y in x["associated_institutions"]
]

affiliated_author_edges = [
{
"id": f"""{x["id"]}-{y["institution"]["id"]}""",
"start": x["id"],
"end": y["institution"]["id"],
"label": "AFFILIATED",
"start_type": "AUTHOR",
"end_type": "INSTITUTION",
}
for x in authors
for y in x["affiliations"]
]
edges = [*associated_institution_edges, *affiliated_author_edges]

print(json.dumps({"nodes": nodes, "edges": edges}))
6 changes: 3 additions & 3 deletions observable/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ orb.data.setDefaultStyle({
size: 6,
};

if (node.data.label === "Node A") {
if (node.data.type === "AUTHOR") {
return {
...basicStyle,
size: 10,
color: "#00FF2B",
color: "#0df2c9",
zIndex: 1,
};
}
Expand Down Expand Up @@ -85,4 +85,4 @@ orb.view.render(() => {
});
```

<div id="graph"></div>
<div id="graph" style="width:100%; height:800px"></div>

0 comments on commit 5b38b33

Please sign in to comment.