Published
Edited
Apr 22, 2021
Fork of graph
Insert cell
Insert cell
input_nodes = FileAttachment("nodes.json").json()
Insert cell
Insert cell
Insert cell
subset = {
//sandbox: find interesting nodes
const rels = data.links;
const worksOn = rels.filter(d => d.label === "worksOn").filter(d => d.score > 0.9);
const replies = rels.filter(d => d.label === "replies").slice(0,20);
const retweets = rels.filter(d => d.label === "retweets").slice(0,20);
const tweets = rels.filter(d => d.label === "tweets").slice(0,40);
const mentions = rels.filter(d => d.label === "mentions").slice(0,20);
const quotes = rels.filter(d => d.label === "quotes").slice(0,20);
const cites = rels.filter(d => d.label === "cites").slice(0,40);
const isAuthor = rels.filter(d => d.label === "isAuthor").slice(0,40);
const isSponsor = rels.filter(d => d.label === "isSponsor").slice(0,40);
const hasPublication = rels.filter(d => d.label === "hasPublication").slice(0,20);
const hasRelatedOrganization = rels.filter(d => d.label === "hasRelatedOrganization").slice(0,20);
const hasProduct = rels.filter(d => d.label === "hasProduct").slice(0,20);
const candidates = worksOn.concat(replies, retweets, tweets, mentions, quotes, cites, isAuthor, isSponsor, hasPublication, hasRelatedOrganization, hasProduct);

let filteredNodes = Object.values(
candidates.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: candidates };
}
Insert cell
nodes_ids = testNodes.map(d => d.id)
Insert cell
{
var value = "26181215"
var testRemoval = nodes.filter(item => item.id != value)
return testRemoval
}
Insert cell
nodes = data.nodes;
Insert cell
tweets = data.nodes.filter(d => d.labels[0] === "Tweet").slice(0,100);
Insert cell
val = data.nodes[0].id
Insert cell
{
var value = 15;
var string = String(value)
return String(value)
}
Insert cell
testNodesArray = data.nodes.slice(0,10)
Insert cell
subset_reversed.nodes.filter(d => d.labels[0] == "Topic")
Insert cell
subset_reversed = {
const nodes = data.nodes;
const links = data.links;
const topic = nodes.filter(d => d.labels[0] === "Topic").slice(0,1);
const authors = nodes.filter(d => d.labels[0] === "Author").slice(0,75);
const orgs = nodes.filter(d => d.labels[0] === "Organization").slice(0,75);
const twitters = nodes.filter(d => d.labels[0] === "TwitterAccount").slice(0,75);
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 coreNodes = authors.concat(orgs, twitters);
const coreNodeIds = coreNodes.map(d => d.id);
const otherNodes = tweets.concat(pubs, trials, products);
function checkId(a, v) {
return a.some(av => v === av);
};


// function getConnections(nodeArr) {
// var connections = [];
// for (let i in links) {
// if (nodeArr.id === links[i].target) {
// connections.push(links[i].source)
// }
// }
// return connections;
// }
function getConnectionsReverse(nodeArr) {
var connections = [];
for (let i in links) {
if (nodeArr.id === links[i].source) {
connections.push(links[i].target)
}
}
return connections;
}

// function deleteFloaters(arr) {
// for (let i in arr) {
// var conns = getConnections(arr[i]);

// for(let e in conns) {
// var value = conns[e]

// if (!checkId(coreNodeIds, value)) {
// var filteredArr = arr.filter(d => d.id != value)
// }
// }
// }
// return filteredArr;
// }
function findNodes(nodeArr) {
var connectedNodes = []
for (let i in nodeArr) {
var connectedNodeIds = getConnectionsReverse(nodeArr[i]);
for (let e in otherNodes) {
if (checkId(connectedNodeIds, otherNodes[e].id)) {
connectedNodes.push(otherNodes[e])
}
}
}
return connectedNodes;
}
const candidateNodes = topic.concat(authors, orgs, twitters, findNodes(authors), findNodes(orgs), findNodes(twitters));
const nodeIds = candidateNodes.map(d => d.id);
var candidateLinks = [];
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");
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.links.filter(d => d.label != "isRelatedTo")
Insert cell
tester = {
var testNode = testNodes[0]

testNodes.map(v => v.id).includes("26181215")
}
Insert cell
testNodes = data.nodes.slice(0,20)
Insert cell
data.nodes[0].labels[0]
Insert cell
data.nodes.filter(d => d.labels[0] === "Author")
Insert cell
data = {
let points = input_nodes.map(d => d.n);
let conns = input_links.map(d => d.r);

function levelPicker(label) {
if (label == "Topic") { return 0 }
if (label == "Author" | label == "Organization" | label == "TwitterAccount") { return 1 }
if (label == "Tweet" | label == "Product" | label == "ClinicalTrial" | label == "Publication") { return 2 }
}
let nodes = points.map(d => {
return {
type: d.type,
id: d.id,
labels: d.labels,
level: levelPicker(d.labels[0]),
properties: d.properties
}
});
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
subset_reversed_smaller = {
const nodes = data.nodes;
const links = data.links;
const topic = nodes.filter(d => d.labels[0] === "Topic").slice(0,1);
const authors = nodes.filter(d => d.labels[0] === "Author").slice(0,30);
const orgs = nodes.filter(d => d.labels[0] === "Organization").slice(0,30);
const twitters = nodes.filter(d => d.labels[0] === "TwitterAccount").slice(0,30);
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 coreNodes = authors.concat(orgs, twitters);
const coreNodeIds = coreNodes.map(d => d.id);
const otherNodes = tweets.concat(pubs, trials, products);
function checkId(a, v) {
return a.some(av => v === av);
};


// function getConnections(nodeArr) {
// var connections = [];
// for (let i in links) {
// if (nodeArr.id === links[i].target) {
// connections.push(links[i].source)
// }
// }
// return connections;
// }
function getConnectionsReverse(nodeArr) {
var connections = [];
for (let i in links) {
if (nodeArr.id === links[i].source) {
connections.push(links[i].target)
}
}
return connections;
}

// function deleteFloaters(arr) {
// for (let i in arr) {
// var conns = getConnections(arr[i]);

// for(let e in conns) {
// var value = conns[e]

// if (!checkId(coreNodeIds, value)) {
// var filteredArr = arr.filter(d => d.id != value)
// }
// }
// }
// return filteredArr;
// }
function findNodes(nodeArr) {
var connectedNodes = []
for (let i in nodeArr) {
var connectedNodeIds = getConnectionsReverse(nodeArr[i]);
for (let e in otherNodes) {
if (checkId(connectedNodeIds, otherNodes[e].id)) {
connectedNodes.push(otherNodes[e])
}
}
}
return connectedNodes;
}
const candidateNodes = topic.concat(authors, orgs, twitters, findNodes(authors), findNodes(orgs), findNodes(twitters));
const nodeIds = candidateNodes.map(d => d.id);
var candidateLinks = [];
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");
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
subset1 = {
// console.log(relationships);

// let rels = [];
// ids.forEach((id, i) => {
// let idOthers = ids.filter(i => i !== id);

// let tempRels = relationships.filter(
// r =>
// (r.source === id && idOthers.includes(r.target)) ||
// (r.target === id && idOthers.includes(r.source))
// );
// //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 = data.links //.slice(0,10000)
.filter(d1 => d1.label === "worksOn")
.filter(d2 => d2.score > 0.85);
// console.log(
// candidates.map(c => {
// const d = c;
// return [d.source_label, d.target_label];
// })
// );

// const relTypes = data.links //.slice(0, 10)
// .map(d1 => d1.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
*/
let filteredNodes = Object.values(
candidates.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;
}, {})
);

// console.log(filteredNodes);

// const links = indexAll.map(i => data.links[i]);

return { nodes: filteredNodes, links: candidates };
}
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