Public
Edited
Feb 21, 2023
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
funcLRT = py`import numpy as np
import pandas as pd
def flogLRv(x, fnidot, fndotj, fndotdot):
logLRvr = x * (np.log(x) - np.log(fnidot))
logLRvr = logLRvr + (fndotj - x) * (np.log(fndotj - x) - np.log(fndotdot - fnidot))
logLRvr = logLRvr - fndotj * (np.log(fndotj) - np.log(fndotdot));
logLRvr.fillna(0)
pj = x / fnidot
qj = (fndotj - x) / (fndotdot - fnidot)
logLRvr[ pj < qj ] = 0
return logLRvr

def fLRT(qnum, number, alpha = 0.05, alphaSpending = 1):
mylist = qnum
mylist["ntrt"] = pd.to_numeric(mylist["ntrt"])
mylist["nidot"] = pd.to_numeric(mylist["nidot"])
mylist["ndotj"] = pd.to_numeric(mylist["ndotj"])
mylist["ndotdot"] = pd.to_numeric(mylist["ndotdot"])
mylist["RR"] = (mylist["ntrt"])*((mylist["ndotdot"]-mylist["nidot"])/(mylist["nidot"]*(mylist["ndotj"]-mylist["ntrt"])))
mylist["stdRR"] = 1/(mylist["ntrt"]) - 1/mylist["nidot"] + 1/(mylist["ndotj"]-mylist["ntrt"]) - 1/(mylist["ndotdot"]-mylist["nidot"])
mylist["stdRR"] = np.sqrt(mylist["stdRR"])
mylist["lowRR"] = np.log(mylist["RR"])- 1.96*(mylist["stdRR"])
mylist["lowRR"] = np.exp(mylist["lowRR"])
mylist["uppRR"] = np.log(mylist["RR"])+ 1.96*(mylist["stdRR"])
mylist["uppRR"] = np.exp(mylist["uppRR"])
mylist["logLR"] = flogLRv(mylist["ntrt"], mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0])
mylist = mylist.sort_values("logLR", ascending=False)
cnstR = number
np.random.seed(123)
mylist["Pvector"] = mylist["nidot"] / mylist["ndotdot"]
mylist["Pobs"] = mylist["ntrt"] / mylist["ndotj"]
sim_cntdata = np.random.multinomial(mylist["ndotj"][0], mylist["Pvector"], size = cnstR)
logLRsim = np.array([flogLRv(i, mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0]) for i in sim_cntdata])
maxlogLRsim = np.array([np.max(i) for i in logLRsim])
mylist["T_alpha"] = np.quantile(maxlogLRsim, 1 - alpha)
mylist["pvalue"] = np.array([ np.mean(maxlogLRsim > i) for i in mylist["logLR"]])
if alphaSpending >= 1:
mylist["is_signal"] = (mylist["logLR"] > mylist["T_alpha"])
else:
mylist["alpha"] = alphaSpending
mylist["is_signal"] = (mylist["pvalue"] < alphaSpending)
mylist = mylist.sort_values("logLR", ascending=False)
return mylist

fLRT`
Insert cell
Insert cell
LRTResult = py`import pandas as pd
${funcLRT}(pd.read_json(${JSON.stringify(LRTData)}),${numberOfSimulation}).to_json(orient="records")`
Insert cell
LRTData
Insert cell
Insert cell
Insert cell
LRTYearData
Insert cell
Insert cell
Insert cell
LRTResultYear = py`import pandas as pd
import numpy as np
LRT_result_Year = pd.DataFrame()

for k in range(len(${yearList})):
LRTYearData = pd.read_json(${JSON.stringify(LRTYearData)})
countAeListYeark = LRTYearData[LRTYearData["Year"] <= ${yearList}[k]].groupby("AE").agg({"ntrt": "sum"})
countAeListTotalYeark = LRTYearData[LRTYearData["Year"] <= ${yearList}[k]].groupby("AE").agg({"nidot": "sum"})
combine_data_Yeark = pd.concat([countAeListYeark, countAeListTotalYeark], axis=1, join="inner")
combine_data_Yeark["ndotj"] = np.sum(combine_data_Yeark["ntrt"])
combine_data_Yeark["ndotdot"] = np.sum(combine_data_Yeark["nidot"])
alphaSpending = 0.05 / (2**k)
LRT_result_Yeark = ${funcLRT}(combine_data_Yeark, ${numberOfSimulation}, alphaSpending = alphaSpending)
LRT_result_Yeark["year"] = str(${yearList}[k])
LRT_result_Year = LRT_result_Year.append(LRT_result_Yeark)
LRT_result_Year['AE'] = LRT_result_Year.index

LRT_result_Year.to_json(orient="records")`
Insert cell
Insert cell
{
return d3.selectAll('table tr').each(function() {
var container = [];
d3.select(this).selectAll('td').each(function() {
container.push(d3.select(this).html());
});
console.log(container);
container.indexOf('true') > -1 ? d3.select(this).selectAll('td').style('color', 'blue') :d3.select(this).selectAll('td').style('color', 'black')
})
}
Insert cell
Insert cell
heatmap = {
var chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
var svg = d3.create('svg')
.attr("width", chartWidth + margin.left + margin.right)
.attr("height", chartHeight + margin.top + margin.bottom);
var vis = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var toolTip = d3.select("body").append("div").attr("class", "toolTip");

// Labels of row and columns
var xAxisLabel = [...new Set(JSON.parse(LRTResultYear).map(n => n.year))];
var yAxisLabel = [...new Set(JSON.parse(LRTResultYear).map(n => n.AE))].sort();

// Build X scales and axis:
var x = d3.scaleBand()
.range([ 0, chartWidth ])
.domain(xAxisLabel)
.padding(0.01);
vis.append("g")
.attr("transform", "translate(0," + chartHeight + ")")
.call(d3.axisBottom(x))
// Build X scales and axis:
var y = d3.scaleBand()
.range([ chartHeight, 0 ])
.domain(yAxisLabel)
.padding(0.01);
vis.append("g")
.call(d3.axisLeft(y));
// Build color scale
var myColor = d3.scaleLinear()
.range(["#fc836a", "#3BAFDA", "lightblue"])
.domain([0, 0.06, 1]);
// .range(["lightblue", "#FC6E51"])
// .domain(d3.extent(JSON.parse(LRTResultYear), d => Math.floor(d.logLR/1000)*1000 ));

var rect = vis.selectAll()
.data(JSON.parse(LRTResultYear), function(d) {return d.year+':'+d.AE;})
.enter()
.append("rect");
rect.attr("x", function(d) { return x(d.year) })
.attr("y", function(d) { return y(d.AE) })
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", "lightblue")
// .style('opacity', 0.8)
.on('mousemove', nodeMouseOver)
.on('mouseout', nodeMouseOut )
.transition()
.duration(600)
.style("fill", function(d) { return myColor(d.pvalue)} );

vis.selectAll(".signalLabel")
.data(JSON.parse(LRTResultYear), function(d) {return d.year+':'+d.AE;})
.enter().append("text")
.attr("class", "signalLabel")
.attr("x", function(d) { return x(d.year) + x.bandwidth()/2 ; })
.attr("y", function(d) { return y(d.AE) + y.bandwidth()/2 + 5 ; })
.text(d => d.is_signal == true ? '❕' : '' )
.style('font-size', '0.65rem')
.style('opacity', 0.6);

function nodeMouseOver(event, d){
toolTip.style("left", event.pageX + (18) + "px")
.style("top", event.pageY + (18) + "px")
.style("display", "block")
.html(`<center><b>${d.AE}</b></center>
<center><b>Year: ${d.year}</b></center>
<center><b>Is Signal: ${d.is_signal}</b></center>
<center>logLR: ${parseFloat(d.logLR).toFixed(2)}</center>
<center>P-value: ${d.pvalue}</center>
<center>alpha: ${d.alpha}</center>`);
// Optional cursor change on target
d3.select(event.target).style("cursor", "pointer");
// Optional highlight effects on target
d3.select(event.target)
.transition()
.attr('stroke', '#aeaeae')
.attr('stroke-width', 2);
}
function nodeMouseOut(event, d){
toolTip.style("display", "none");
d3.select(event.target).style("cursor", "default");
d3.select(event.target)
.transition()
.attr('stroke', '#fff')
.attr('stroke-width', 0);
}

return svg.node()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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