I want to create interactive line- and topoplot depending on menu. I figured out how to make red the line chosen in menu, but it doesn't work for topoplot marks (black circles inside topoplot). I can change it manually (cmap[][4] = RGB{N0f8}(1.0,0.0,0.0)), but how to do that interactively?
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
ax = Axis(f[1:3, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")
N = 1:length(pos) #1:4
hidespines!(ax, :t, :r)
GLMakie.xlims!(-0.3, 1.2)
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)
times = range(-0.3, length=size(dat_e,2), step=1 ./ 128)
lines = Dict()
for i in N
mean_trial = mean(dat_e[i,:,:],dims=2)[:,1]
line = lines!(times, mean_trial, color = "black")
lines[i] = line
end
hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
topo_axis = Axis(f[2, 2], width = 178, height = 178, aspect = DataAspect())
Makie.xlims!(low = -0.2, high = 1.2)
Makie.ylims!(low = -0.2, high = 1.2)
topoMatrix = eegHeadMatrix(pos[N], (0.5, 0.5), 0.5)
cmap = Observable(collect(ColorScheme(range(colorant"black", colorant"black", length=30))))
#cmap[][4] = RGB{N0f8}(1.0,0.0,0.0)
topo = eeg_topoplot!(topo_axis, N, # averaging all trial of 30 participants on Xth msec
raw.ch_names[1:30];
positions=pos, # produced automatically from ch_names
interpolation=NullInterpolator(),
enlarge=1,
#colorrange = (0, 1), # add the 0 for the white-first color
colormap = cmap[],
label_text=false)
hidedecorations!(current_axis())
hidespines!(current_axis())
num_prev = 0
menu = Menu(f[3, 2], options = raw.ch_names[1:30], default = nothing)#, default = "second")
on(menu.selection) do selected
if selected != nothing
num = findall(x->x==menu.selection[], raw.ch_names[1:30])[]
if num_prev != 0
lines[num_prev].color = "black"
cmap[][num] = RGB{N0f8}(1.0,0.0,0.0)
end
lines[num].color = "red"
cmap[][num] = RGB{N0f8}(1.0,0.0,0.0)
num_prev = num
end
end
notify(menu.selection)
#print(cmap[])
f
We solved this by putting this string at the end of the menu.selection section:
notify(lines)
It works, because lines() automatically creates Observable.
I want to put text annotations along a line on the top. If I write same numbers (no matter which) in y array text will be aligned across center.
let
f = Figure()
ch = ["Ch2", "Ch3", "Ch17", "Ch18", "Ch19"]
x = Array(0:100:400)
y = [100, 100, 100, 100, 100]
ax = Axis(f[1, 1])
text!(x, y, text = ch, align = (:center, :center),
offset = (0, 0),
color = :black)
f
end
but if I change one item in y array
y = [10, 100, 100, 100, 100]
this happens:
How could I put "Ch2" to the same position on the top with other annotations?
Use ylims!(ax,0,110); to configure your axis.
let
f = Figure(;ylim=[0,110])
ch = ["Ch2", "Ch3", "Ch17", "Ch18", "Ch19"]
x = Array(0:100:400)
y = [100, 100, 100, 100, 100]
ax = Axis(f[1, 1]);
text!(x, y, text = ch, align = (:center, :center),
offset = (0, 0),color = :black)
ylims!(ax,0,110)
f
end
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a simple Gtk/Cairo game:
https://github.com/bigos/cairo-example/blob/9ac6f39d53cd27f36cad53a265e05f3e45564a37/src/Main.hs
I have noticed a problem where sometimes my snake doesn't eat food. I have figured out how to log subsequent versions of global state.
I have this debuggator function, which tests versions dumped by the logger.
debuggator =
let m1 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0),(4,0)], tickInterval = 500.0, seed = 36, width = 600}
m2 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600}
m3 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingRight, height = 400, lastKey = 65363, scale = 25, snake = [(6,1),(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 37, width = 600}
ma = updateGlobalModel Tick m1
mb = updateGlobalModel Tick ma
in
[ma,mb]
which returns the following:
λ> debuggator
[Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600},
Model {debugData = "(\"** eaten **\",(6,2),[(6,2)])", eaten = 6, foodItems = [(2,5),(0,2),(10,4)], gameField = Move, snakeLength = 3, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,1),(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600}]
In this example ma corresponds to m2 which values was copied from the real game. Processing ma leads to mb, which shows that snake has eaten the food, but in real game progressing from m2 to m3 did not lead to snake eating food. the snake head has moved over the food item and the list of food items did not change. In the repl example mb has 3 food items because the last food item was eaten and 3 more were generated.
Why does function respond differently to the same arguments? does calling a pure function from IO have some unexpected effects?
Function that checks id the snake has eaten food is at line 110:
foodEaten :: Model -> Bool
foodEaten model =
any id (map (\c -> (fst c)==cx && (snd c)==cy) (foodItems model))
where hsm = head (snake model)
cx = fst hsm
cy = snd hsm
That in turn is called by cook function at line 293:
cook :: Model -> Model
cook model =
if foodEaten model
then model { gameField = detectCollision model
, snakeLength = (snakeLength model) +3
, foodItems = filter (\c -> not (foodUnderHead c model)) (foodItems model)
, debugData = (debugData model) ++ (show ("** eaten **" :: String, head (snake model), (foodItems model)))
, eaten = (eaten model) + 1 }
else model { gameField = detectCollision model
, snakeLength = shrink (snakeLength model)
, debugData = "" }
For some reason the snake does not detect if the food was eaten. One commenter has suggested to check if my code is thread safe.
Debuggator function at line 273 tries to illustrate simple case that always works. function cook that fails in some situations is called indirectly at main function line line 295. There a timer events are defined which call updateGlobalModel Tick
_ <- GI.GLib.timeoutAdd GI.GLib.Constants.PRIORITY_DEFAULT 500 ( modifyIORef' globalModel (updateGlobalModel Tick) >>
Gtk.widgetQueueDraw canvas >> return True)
Debuggator function illustrates how I can call it in REPL. However, as someone suggested that could be a thread problem. My Haskell skills are still poor and I do not know how to investigate it properly.
I am trying to use the following code (adapted from the code given in Gelman and Hill's Book) to estimate a varying coefficient/intercept ordered probit model in Jags. However, it is giving me a "Observed node inconsistent with unobserved parents at initialization.Try setting appropriate initial values". Where am I going wrong? Could somebody please help me? Thanks in advance !!
rm(list=ls(all=TRUE));
options(warn=-1)
library(mvtnorm)
library(arm)
library(foreign)
library("R2jags")
library(MCMCpack)
set.seed(1)
standardizeCols = function( dataMat ) {
zDataMat = dataMat
for ( colIdx in 1:NCOL( dataMat ) ) {
mCol = mean( dataMat[,colIdx] )
sdCol = sd( dataMat[,colIdx] )
zDataMat[,colIdx] = ( dataMat[,colIdx] - mCol ) / sdCol
}
return( zDataMat )
}
keep<-1
nobs = 150;
nis<-sample(1:40,nobs,replace=T) # number obs per subject
id<-rep(1:nobs,nis)
N<-length(id)
corr_beta = 0.6;
Sigma_beta = matrix(c(1, corr_beta, corr_beta, corr_beta,
corr_beta, 1, corr_beta, corr_beta,
corr_beta, corr_beta, 1, corr_beta,
corr_beta, corr_beta, corr_beta, 1), ncol=4);
betas <- rmvnorm(n=N, mean=c(-1.45, 0.90, 0.25, -2.3), sigma=Sigma_beta);
#Generate the data
x3 = matrix(0, nrow=N,ncol=3);
y3 = matrix(0, nrow=N,ncol=1);
for (i in 1:N) {
error_v = rnorm(1,0,1);
x3[i,1] = rnorm(1,0,1);
x3[i,2] = rnorm(1,0,1);
x3[i,3] = rnorm(1,0,1);
y3[i,1] = betas[id[i], 1] + betas[id[i], 2]*x3[i,1] + betas[id[i], 3]*x3[i,2] + betas[id[i], 4]*x3[i,3] + error_v;
}
cutoff=c(-100, 0, 1.5, 2.4, 100)
k=length(cutoff)-1;
Y3<-cut(y3, br = cutoff, right=TRUE, include.lowest = TRUE, labels = FALSE)
Y3=Y3
X3=x3
m1=max(Y3)
y = as.vector( Y3 )
n = length(y)
J<-length(unique(id))
X = cbind(1, standardizeCols( X3 ))
nPred = NCOL(X)
subjects<-as.vector(as.numeric(id))
K=nPred
W <- diag (K)
# MCMC settings
ni <- 5000; nb <- 2500; nt <- 6; nc <- 3
tau1u=c(0,1,2)
jags_data <- list ("n", "J", "K", "y", "subjects", "X", "W", "m1")
inits <- function (){
list (B.raw=array(rnorm(J*K),c(J,K)), mu.raw=rnorm(K), sigma.y=runif(1), Tau.B.raw=rwish(K+1,diag(K)), xi=runif(K))
}
params <- c ("B", "mu", "sigma.B", "rho.B", "tau1u")
cat("model {
for (i in 1:n){
y.hat[i] <- inprod(B[subjects[i],],X[i,])
y[i] ~ dcat(p[i,])
estar[i]~dnorm (y.hat[i], tau.y);
for (j in 1:(m1-1)) {
Q1[i,j]<-pnorm(tau1[j]-estar[i],0,1)
}
p[i,1] <- Q1[i,1]
for(j in 2:(m1-1)) {
p[i,j] <- Q1[i,j] - Q1[i,j-1]
}
p[i,m1] <- 1 - Q1[i,m1-1]
}
tau.y <- pow(sigma.y, -2)
sigma.y ~ dunif (0, 100)
# thresholds (unordered priors)
for(j in 1:(m1-1)){
tau1u[j] ~ dnorm(0,.01)
}
# ordered thresholds
tau1 <- sort(tau1u)
for (j in 1:J){
for (k in 1:K){
B[j,k] <- xi[k]*B.raw[j,k]
}
B.raw[j,1:K] ~ dmnorm (mu.raw[], Tau.B.raw[,])
}
for (k in 1:K){
mu[k] <- xi[k]*mu.raw[k]
mu.raw[k] ~ dnorm (0, .0001)
xi[k] ~ dunif (0, 100)
}
Tau.B.raw[1:K,1:K] ~ dwish (W[,], df)
df <- K+1
Sigma.B.raw[1:K,1:K] <- inverse(Tau.B.raw[,])
for (k in 1:K){
for (k.prime in 1:K){
rho.B[k,k.prime] <- Sigma.B.raw[k,k.prime]/sqrt(Sigma.B.raw[k,k]*Sigma.B.raw[k.prime,k.prime])
}
sigma.B[k] <- abs(xi[k])*sqrt(Sigma.B.raw[k,k])
}
}", fill=TRUE, file="wishart2.txt")
# Start Gibbs sampler
outj <- jags(jags_data, inits=inits, parameters.to.save=params, model.file="wishart2.txt", n.thin=nt, n.chains=nc, n.burnin=nb, n.iter=ni)
Your initial values function returns random numbers from normal and uniform distributions, which it appears are not close enough to sensible values to allow a non-0 posterior value to be calculated. I think you need to choose your initial values more carefully, and perhaps based on the values generated in the data, to ensure that the model compiles. Do Gelman and Hill give initial values for their model that you could start with?
Update: you could also try removing your 'inits=inits' argument to allow JAGS to select its own initial values, which works for most (although not all) models. I dont use R2JAGS though so I'm not sure if this is allowed for the jags function (but it is for rjags and runjags).