From 89c2cf05e6cc862114510d54ebc7d50683431920 Mon Sep 17 00:00:00 2001 From: Luis Arias Date: Mon, 15 Apr 2024 22:20:41 +0200 Subject: [PATCH 1/4] Add author nodes --- observable/docs/data/graph.json.py | 25 ++++++++++++++++++++++--- observable/docs/index.md | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/observable/docs/data/graph.json.py b/observable/docs/data/graph.json.py index 3822df4..7205b3f 100644 --- a/observable/docs/data/graph.json.py +++ b/observable/docs/data/graph.json.py @@ -3,7 +3,7 @@ import pyalex from dotenv import load_dotenv -from pyalex import Institutions +from pyalex import Authors, Institutions # Load Secrets load_dotenv() @@ -23,11 +23,30 @@ all_institutions = [*institutions, *associated_institutions] # 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 authors affiliated with each institution +author_nodes = [ + {"id": y["id"], "label": y["display_name"], "type": "AUTHOR"} + for x in all_institutions + for y in Authors().filter(affiliations={"institution": {"id": x["id"]}}).get() +] + +nodes = [*institution_nodes, *author_nodes] # Create associated institution edges edges = [ - {"id": x["id"], "start": x["id"], "end": y["id"], "label": "ASSOCIATED"} + { + "id": x["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"] ] diff --git a/observable/docs/index.md b/observable/docs/index.md index 6c2a683..03c9b80 100644 --- a/observable/docs/index.md +++ b/observable/docs/index.md @@ -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, }; } From df12e87591fa2aa75d561b4377e1cbbe7eddc114 Mon Sep 17 00:00:00 2001 From: Luis Arias Date: Mon, 15 Apr 2024 22:26:26 +0200 Subject: [PATCH 2/4] Only use unique institutions and authors --- observable/docs/data/graph.json.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/observable/docs/data/graph.json.py b/observable/docs/data/graph.json.py index 7205b3f..8a59c9b 100644 --- a/observable/docs/data/graph.json.py +++ b/observable/docs/data/graph.json.py @@ -19,8 +19,13 @@ 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 institution_nodes = [ @@ -28,11 +33,13 @@ for x in all_institutions ] -# Get authors affiliated with each institution +# Get unique authors affiliated with each institution +seen = set() author_nodes = [ {"id": y["id"], "label": y["display_name"], "type": "AUTHOR"} 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"])) ] nodes = [*institution_nodes, *author_nodes] From 767a22842ba1a5a2521cb15166fdf05e83b83b15 Mon Sep 17 00:00:00 2001 From: Luis Arias Date: Mon, 15 Apr 2024 22:39:00 +0200 Subject: [PATCH 3/4] Add author affiliation edges --- observable/docs/data/graph.json.py | 29 +++++++++++++++++++++++------ observable/docs/index.md | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/observable/docs/data/graph.json.py b/observable/docs/data/graph.json.py index 8a59c9b..ea688ad 100644 --- a/observable/docs/data/graph.json.py +++ b/observable/docs/data/graph.json.py @@ -33,21 +33,26 @@ for x in all_institutions ] -# Get unique authors affiliated with each institution +# Get unique affiliated authors seen = set() -author_nodes = [ - {"id": y["id"], "label": y["display_name"], "type": "AUTHOR"} +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 = [ +associated_institution_edges = [ { - "id": x["id"], + "id": f"""{x["id"]}-{y["id"]}""", "start": x["id"], "end": y["id"], "label": "ASSOCIATED", @@ -57,6 +62,18 @@ 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})) diff --git a/observable/docs/index.md b/observable/docs/index.md index 03c9b80..f08c514 100644 --- a/observable/docs/index.md +++ b/observable/docs/index.md @@ -85,4 +85,4 @@ orb.view.render(() => { }); ``` -
+
From a8743ecc72dad89d8a989d15280be19a13141842 Mon Sep 17 00:00:00 2001 From: Luis Arias Date: Mon, 15 Apr 2024 22:39:45 +0200 Subject: [PATCH 4/4] Widen graph display --- observable/docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/observable/docs/index.md b/observable/docs/index.md index f08c514..6c5a4c5 100644 --- a/observable/docs/index.md +++ b/observable/docs/index.md @@ -85,4 +85,4 @@ orb.view.render(() => { }); ``` -
+