Published
Edited
Apr 21, 2021
1 fork
Insert cell
md`# graph`
Insert cell
Insert cell
input_nodes = FileAttachment("nodes.json").json()
Insert cell
Insert cell
secondNetwork = {
var nodes = data.nodes; // filtered set of nodes goes here

const topic = nodes.filter(d => d.labels[0] === "Topic").slice(0, 1);
// const authors = nodes.filter(d => d.labels[0] === "Author").slice(0,20);
// const orgs = nodes.filter(d => d.labels[0] === "Organization").slice(0,20);
// const twitters = nodes.filter(d => d.labels[0] === "TwitterAccount").slice(0,20);
const tweets = nodes.filter(d => d.labels[0] === "Tweet");
// const pubs = nodes.filter(d => d.labels[0] === "Publication");
const trials = nodes.filter(d => d.labels[0] === "ClinicalTrial");
const products = nodes.filter(d => d.labels[0] === "Product");

const candidateNodes = topic.concat(
/* authors,
orgs,
twitters,*/
tweets,
//pubs,
trials,
products
);
const nodeIds = candidateNodes.map(d => d.id);

function checkId(a, v) {
return a.some(av => v === av);
}

const candidateLinks = [];
const links = data.links;

for (let i in links) {
if (
checkId(nodeIds, links[i].source) &&
checkId(nodeIds, links[i].target)
) {
candidateLinks.push(links[i]);
}
}

const filteredLinks = candidateLinks; //.filter(d => d.label === "isRelatedTo"); // d.label === "worksOn");

const filteredNodes = Object.values(
filteredLinks.reduce(function(t, v) {
if (!t[v.source]) {
t[v.source] = candidateNodes.filter(o => o.id === v.source)[0];
}
if (!t[v.target]) {
t[v.target] = candidateNodes.filter(o => o.id === v.target)[0];
}
return t;
}, {})
);

return { nodes: filteredNodes, links: filteredLinks };
}
Insert cell
data.nodes
Insert cell
groupedNodes = {
let grups = d3.group(
data.nodes,

d =>
d.labels[0] === "Author"
? d.labels[0] === "Author"
: d.labels[0] === "TwitterAccount"
? d.labels[0] === "TwitterAccount"
: d.labels[0] === "Organization"
? d.labels[0] === "Organization"
: d.labels[0] === "Topic"
);

let grouped = d3.group(grups.get(true), d => d.labels[0]);

return { grouped }; //, size: grups.map(d => d[1].length) };
}
Insert cell
hierarchy = d3.hierarchy(groupedNodes)
Insert cell
unique(groupedNodes['grouped_nodes'].map(d => d.labels))
Insert cell
)
Insert cell
function unique(data) {
let unique = [...new Set(data.map(d => d[0]))];
return unique;
}
Insert cell
subset1 = {
// FILTER LINKS, THEN NODES //
const filteredLinksToNodes = data.links //.slice(0,10000)
.filter(d1 => d1.label === "worksOn")
.filter(d2 => d2.score > 0.85);

let filteredNodes = Object.values(
filteredLinksToNodes.reduce(function(t, v) {
if (!t[v.source]) {
t[v.source] = data.nodes.filter(o => o.id === v.source)[0];
}
if (!t[v.target]) {
t[v.target] = data.nodes.filter(o => o.id === v.target)[0];
}
return t;
}, {})
);

return { nodes: filteredNodes, links: filteredLinksToNodes };
}
Insert cell
data = {
let nodes = input_nodes.map(d => d.n);
let conns = input_links.map(d => d.r);

let links = conns.map(d => {
return {
id: d.id,
type: d.type,
label: d.label,
source: d.start.id,
source_index: nodes.map(a=>a.id).indexOf(d.start.id), // gets from nodes the indexOf(id in link)

source_label: d.start.labels[0],
target: d.end.id,
target_index: nodes.map(a=>a.id).indexOf(d.end.id),
target_label: d.end.labels[0],
score: typeof d?.properties?.score == "undefined" ? 0 : d.properties.score
}
});

return { nodes: nodes, links: links};
}
Insert cell
Insert cell
Insert cell
Insert cell
// subset = {
// const ids = input_nodes.map(n => n.n.id);
// console.log(ids.length, ids);
// let relationships = input_links.map((d, index) => {
// let { label, start, end } = d.r;
// //return {index, label, startId: start.id, endId: end.id}
// return { index, label, start, end };
// });
// console.log(relationships);
// let rels = [];
// ids.forEach((id, i) => {
// let idOthers = ids.filter(i => i !== id);
// let tempRels = relationships.filter(
// r =>
// (r.start.id === id && idOthers.includes(r.end.id)) ||
// (r.end.id === id && idOthers.includes(r.start.id))
// );
// // console.log(tempRels.map(l => l.index));
// rels = rels.concat(tempRels);
// });
// const indexAll = rels
// .map(l => l.index)
// .filter((v, i, a) => a.indexOf(v) === i);
// // console.log(indexAll.length, indexAll);
// //sandbox: find interesting nodes
// const candidates = input_links //.slice(0,10000)
// .filter(d1 => d1.r.label === "worksOn")
// .filter(d2 => d2.r.properties.score > 0.95);
// /* console.log(
// candidates.map(c => {
// const d = c.r;
// return [d.start.labels, d.end.labels];
// })
// );
// */
// // const relTypes = data //.slice(0, 10)
// // .map(d1 => d1.r.label) //{lable: d1.r.label/*, hasProperites: d1.r.properties*/})
// // .filter((d2, i, a) => a.indexOf(d2) === i);
// // console.log(relTypes);
// /*
// ["preferredMappedTo", //topics
// "isRelatedTo", //tweet, pub -> topic
// "worksOn", //author, twitterAcc, org -> topic, score
// "hasSuggestedTopic", //topics
// "replies",
// "retweets",
// "tweets",
// "mentions",
// "quotes",
// "cites",
// "isAuthor",
// "isSponsor",
// "hasPublication",
// "hasRelatedOrganization", //orgs
// "hasProduct",
// "pharmacologicalAction"]
// */
// /*
// - split into 3 categoreis
// - worksOn
// - only 1 connection
// */
// const links = indexAll.map(i => data[i]);
// //console.log(links)
// return { nodes: ids, links: links };
// }
Insert cell
height = 500
Insert cell
Insert cell
Insert cell
d3 = require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more