-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcltk.R
More file actions
168 lines (143 loc) · 4.65 KB
/
tcltk.R
File metadata and controls
168 lines (143 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
### Interactive density plots. Based on TCL version by Guido Masarotto
###
### Intending to use this for a surface viewer, MDSumner 21 April 2004
###
### Well, that was OK for a first go, now I want it for the chain.
### Function:
## default is Grid size 10
## Overlay "Lines"
## Window - first twilight
## Type - "image"
## Twilights to smooth - 1
##
## actions Change grid size - replot with new n
## Change overlay - replot with new option
## Change window - regen with different subset
## Change display - replot with new option
## Change ## twi - regen with different subset
library(tcltk)
library(MASS)
#source("C:/Documents and Settings/mdsumner/My Documents/CODE/R/src/sMat.r")
#mat <- increMat(sMat(),c(-50,100))
d <- read.csv("track.csv")
lons <- d[,2]
lats <- d[,1]
load("Chain.Rdata")
npars <- 3
npts <- dim(ps)[2]/npars
xx <- NULL
yy <- NULL
for(i in 1:npts) {
yy <- c(yy,ps[,(i-1)*npars+1])
xx <- c(xx,ps[,(i-1)*npars+2])
}
require(tcltk) || stop("tcltk support is absent")
require(MASS) || stop("MASS support is absent")
y <- NULL
x <- NULL
size <- tclVar(10)
disp <- tclVar(1)
overlay<- tclVar(1)
twi <- tclVar(1)
twi.sav <- 1
wind <- tclVar(1)
wind.sav <- 1
h <- NULL
xlim <<- range(lons) + c(-2,2)
ylim <<- range(lats) + c(-2,2)
replot <- function(...)
{
sz <- as.numeric(tclObj(size))
if (is.null((h)))
h <- c(bandwidth.nrd(x), bandwidth.nrd(y))
h <- h/6
plot(lons,lats,type="n")
if (tclvalue(disp)=="1"){
#eval(substitute(image(kde2d(x,y,n=sz),xlim=xlim,ylim=ylim)))
image(kde2d(x,y,h=h,n=sz),add=T)
}else {
#eval(substitute(contour(kde2d(x,y,n=sz),xlim=xlim,ylim=y)))
contour(kde2d(x,y,h=h,n=sz),add=T)
}
if (tclvalue(overlay)=="1"){
eval(substitute(lines(lons,lats)))
}
if (tclvalue(overlay)=="2"){
eval(substitute(points(lons,lats)))
}
}
## replot.maybe
## <- function(...)
# {
# if (as.numeric(tclvalue(twi.n)) != twi.n.sav)
# {
# regen()
# }
#}
regen <- function(...) {
wind.sav <<- wind.n <- as.numeric(tclObj(wind))
twi.sav <<- twi.n <- as.numeric(tclObj(twi))
rngST <- (wind.n-1)*1000 + 1
rng <- rngST:(min(npts*1000,rngST+twi.n*1000))
h <- NULL
x <<- xx[rng]
y <<- yy[rng]
replot()
}
regen.maybe <- function(...)
{
if((as.numeric(tclvalue(wind)) != wind.sav) | (as.numeric(tclvalue(twi)) != twi.sav))
{
regen()
}
}
base <- tktoplevel()
tkwm.title(base, "mcmc plot")
spec.frm <- tkframe(base,borderwidth=2)
left.frm <- tkframe(spec.frm)
right.frm <- tkframe(spec.frm)
## frame 1 - set the grid size
frame1 <- tkframe(left.frm, relief="groove", borderwidth=2)
tkpack(tklabel(frame1, text="Grid size n"))
for ( i in c(10,20,50,100) ) {
tmp <- tkradiobutton(frame1, command=replot,
text=i,value=i,variable=size)
tkpack(tmp, anchor="w")
}
## frame 2 - set the window position (which twilight are we centred on)
frame2 <-tkframe(left.frm, relief="groove", borderwidth=2)
tkpack(tklabel (frame2, text="Window"))
tkpack(tkscale(frame2,command=regen.maybe, from=1, to=npts,
showvalue=F, variable=wind,
resolution=1, orient="horiz"))
## frame 3 - set the display "image" or "contour"
frame3 <-tkframe(left.frm, relief="groove", borderwidth=2)
tkpack(tklabel(frame3, text="Type of Display"))
tkpack(tkradiobutton(frame3, command=replot, text="Image",
value=1, variable=disp), anchor="w")
tkpack(tkradiobutton(frame3, command=replot, text="Contour",
value=2, variable=disp), anchor="w")
## frame 4 - set the overlays
frame4 <-tkframe(right.frm, relief="groove", borderwidth=2)
tkpack(tklabel(frame4, text="Overlay Track"))
tkpack(tkradiobutton(frame4, command=replot, text="Lines",
value=1, variable=overlay), anchor="w")
tkpack(tkradiobutton(frame4, command=replot, text="Points",
value=2, variable=overlay), anchor="w")
tkpack(tkradiobutton(frame4, command=replot, text="Off",
value=3, variable=overlay), anchor="w")
## frame 5 - set the number of twilights to include
frame5 <- tkframe(left.frm, relief="groove", borderwidth=2)
tkpack(tklabel(frame5, text="Twilights to smooth"))
tkpack(tkentry(frame5,textvariable=twi))
## pack the frames together
tkpack(frame1, frame2, fill="x")
tkpack(frame3,frame4, fill="x")
tkpack(frame5,fill="x")
tkpack(left.frm, right.frm,side="left", anchor="n")
## `Bottom frame' (on base):
q.but <- tkbutton(base,text="Quit",
command=function(){tkdestroy(base);dev.off()})
tkpack(spec.frm, q.but)
#regen()
## }) ## end local comment out