-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyeMarkovCode4JRSSC.r
821 lines (742 loc) · 19.4 KB
/
eyeMarkovCode4JRSSC.r
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# Library for KNN classification
library(class)
# Library to display PNG images
library(png)
library(MASS);
BW_SCALE = 1.0;
GRID_SIZE= 100;
MCDRAWS = 100;
# Change to wherever the data is kept on your system
#dataDir = "./";
dataDir = "/home/adam/Documents/RESEARCH/RSS2015/files-master/";
# Read in the entire data table
if(!exists("eyeData")){
eyeData <- read.table(
paste(dataDir,'data.csv',sep=""),h=TRUE,sep=','
);
}
# some dimensions for ploting the images
eyeXlim <- c(-1280,1280)/2;
eyeYlim <- c(-1024,1024)/2;
eyeXlim.landscape <- c(-1024,1024)/2;
eyeYlim.landscape <- c(-768,768)/2;
eyeXlim.portrait <- c(-768,768)/2;
eyeYlim.portrait <- c(-1024,1024)/2;
# Returns subsets of data table
rss_getData <- function( imag=F, type=F, subj=F, fixt=F )
{
if(type!=F)
{
chr = switch(
type,
normal = "n",
abnormal = "a",
grayscale= "g"
);
}
return(
subset(
eyeData,
tern(imag==F,T,image==imag) &
tern(type==F,T,condition==type) &
tern(subj==F,T,subject==paste(chr,subj,sep="")) &
tern(fixt==F,T,fixation==fixt)
)
);
}
# Ternary operator
tern <- function( a, b, c)
{
if(a){
return(b);
} else {
return(c);
}
}
#
# Add a cumulative duration column to the data table
#
if( ncol(eyeData)==8 ){
dat = c();
pb = txtProgressBar(style=3,min=0,max=60);
for( i in 1:60 ){
for( j in c("normal","abnormal","grayscale") ){
for( k in 1:10 ){
tmp = rss_getData( i,j,k );
if(nrow(tmp)==0) next;
tmp = tmp[order(tmp[,4]),];
tmp = cbind(tmp,cdur=c(cumsum(tmp[,3])));
#tmp = cbind(tmp,cdur=c(0,cumsum(tmp[,3])[-nrow(tmp)]));
dat = rbind(dat,tmp);
}
}
setTxtProgressBar(pb,i);
}
close(pb);
eyeData=dat;
}
#########
# Functions to fit the model from Section two
# computing Bayes factors different cluster values
#########
#
# Compute optimal clusters for all
# img in images
#
rss_computeAllBayes <- function(
images, CLUSTTYPE=1, dst="eucl",meth="complete", kn=9
){
x11(width=12,height=6);
n = length(images);
# Initialize array of Bayes Factors
# and array of clusters
allBF = c();
bf = matrix(0,n,3);
cl = matrix(1,n,3);
lk = matrix(0,n,30);
offs = min(images)-1;
sTime = Sys.time();
for( img in images )
{
print(paste("Testing Image ",img,"...",sep=""));
nOut = rss_mmBayesFactor( img, 'normal', PRINT=FALSE,
CLUSTTYPE=CLUSTTYPE,dst=dst,meth=meth,kn=kn );
aOut = rss_mmBayesFactor( img, 'abnormal', PRINT=FALSE,
CLUSTTYPE=CLUSTTYPE,dst=dst,meth=meth,kn=kn );
gOut = rss_mmBayesFactor( img, 'grayscale', PRINT=FALSE,
CLUSTTYPE=CLUSTTYPE,dst=dst,meth=meth,kn=kn );
bf[img-offs,1] = min( nOut$bf );
if(bf[img-offs,1]<.15) cl[img-offs,1] = which.min( nOut$bf );
bf[img-offs,2] = min( aOut$bf );
if(bf[img-offs,2]<.15) cl[img-offs,2] = which.min( aOut$bf );
bf[img-offs,3] = min( gOut$bf );
if(bf[img-offs,3]<.15) cl[img-offs,3] = which.min( gOut$bf );
lk[img-offs,] = c(
nOut$like[nOut$kBest,],
aOut$like[aOut$kBest,],
gOut$like[gOut$kBest,]
);
print("Results: ");
print(paste(" Bayes: ",bf[img-offs,]));
print(paste(" Clust: ",cl[img-offs,]));
allBF = rbind(allBF,nOut$bf,aOut$bf,gOut$bf);
}
print(Sys.time()-sTime);
print( paste(
"Average Time per pic:",(Sys.time()-sTime)/(3*n)
) );
return( list(img=images,bf=bf,cl=cl,lk=lk,allBF=allBF) );
}
#
# Compute best clusters for the Markov model
# by computing the Bayes factors for a
# specific image
#
rss_mmBayesFactor <- function( n,type,
DEBUG=FALSE, PRINT=TRUE, PLOT=TRUE, REMOVE=0,
CLUSTTYPE=1,dst="euclidean",meth="complete",kn=3,
cdr=c(0,5000), POSTHOC=FALSE
)
{
# Get data
dat = rss_getData(n,type);
if(ncol(dat)==9){
dat = subset(dat,(cdr[1]<=dat[,9])&(dat[,9]<=cdr[2]));
}
print(paste("Data Size: ",nrow(dat)));
for( s in REMOVE )
dat = subset( dat, subject!=subj(type,s) );
sCnt = length(setdiff(1:10,REMOVE));
# Max number of clusters to test
maxK = 10;
# matrix of likelihoods
like1= matrix(-1000,maxK,10);
# List of Markov posteriors
mrkTran = list();
mrkInit = list();
for( k in 1:maxK ) # For each cluster count
{
mrkInit[[k]] = rep(0,k);
mrkTran[[k]] = matrix(0,k,k);
if(PRINT)
print(paste("Testing k-means ",k,"...",sep=''));
for( s in setdiff(1:10,REMOVE) ) # For each subject (cross validate)
{
# Get Training and Test Data
train = subset( dat, subject!=subj(type,s) );
test = subset( dat, subject==subj(type,s) );
if( nrow(test)<2 ) next;
test = rss_sortFix(test);
# If no clustering (i.e. clusters==1 )
if( k==1 ){
dTotal = kde2d(
train$fx,train$fy,lims = c(eyeXlim, eyeYlim),n=GRID_SIZE
);
like1[k,s] = calcLike0( dTotal, test );
next;
}
# Run k means
kdat = cbind( train$fx, train$fy );
colnames(kdat) <- c("x","y");
if(CLUSTTYPE==1){
km = kmeans(kdat,k,iter.max=20,nstart=10);
} else if( CLUSTTYPE==2 ){
hc = hclust( dist(kdat,method=dst),method=meth );
ct = as.double(cutree( hc, k=k ));
km = list(cluster=ct,centers=c(),size=c());
for( kk in 1:k){
km$size = c(km$size,length(which(ct==kk)));
km$centers = rbind(km$centers,
apply( subset(kdat,ct==kk),2,mean )
);
}
}
# Return if bad clusters
#print(paste(km$size,sum(km$size)));
if( sum( km$size<3 )>0 ) next;
# Assign cluster to Train and Test sets
train = cbind( train, cluster=km$cluster );
test = cbind( test, cluster=0 );
# Count number of transitions between clusters
# Use to update Dirichlet Prior
markv = rss_constrMarkov( n,k,type,train,km );
mrkInit[[k]] = mrkInit[[k]] + markv$iProb;
mrkTran[[k]] = mrkTran[[k]] + markv$tProb;
# Relabel Clusters
km$cluster = markv$km$cluster;
km$centers = markv$km$centers;
train$cluster = km$cluster;
# Pick best cluster for each element of test set
#for( i in 1:nrow(test) )
# test$cluster[i] = chooseClust( test$fx[i],test$fy[i],train );
test$cluster = chooseClust2( test, train, kn );
# Construct empirical density estimate for each cluster
dClust = constrClustDen( train, k );
# Compute likelihood given transition probabilities and
# density estimates
like1[k,s] = calcLike1( dClust, markv, km, test, MCDRAWS );
if(DEBUG)
{
print(paste("Like: ",like1[k,s]));
rss_plotTrainTest(n,k,type,train,test,km);
readline();
}
}
#mrkInit[[k]] = mrkInit[[k]]/10;
#mrkTran[[k]] = mrkTran[[k]]/10;
}
# Choose best number of clusters excluding k=1
kBest = which.max( rowSums((like1[-1,])) )+1;
# Average over all subjects
kLike = (rowSums((like1))/sCnt);
# Compare k>1 clusters to k=1 (i.e. Bayes Factor)
#bf = pmin(exp(kLike[1]-kLike),rep(1));
bf = exp(kLike[1]-kLike);
if( min(bf>0.15) ) kBest=1;
if(PLOT)
{
if(kBest==1){
rss_plotImageData(n,type);
} else {
clDat = rss_clusterDensity(n,kBest,type,min(bf[kBest]),PRINT);
}
}
if(PRINT){
print(signif(kLike,4));
print(signif(pmin(exp(kLike[1]-kLike),rep(1)),4));
print(signif(like1[kBest,],4));
}
if(POSTHOC)
return( posthocMarkov(n,type,kBest,2000,JUSTCNT=TRUE) );
return(list(
like=like1,bf=bf,kBest=kBest,
mInit=mrkInit[[kBest]],#/sum(mrkInit[[kBest]]),
mTran=mrkTran[[kBest]]#/rowSums(mrkTran[[kBest]])
));
}
#
# Compute Likelihood in null case
# that is, the random draw model without
# any Markov transitions.
#
calcLike0 <- function( den, dat )
{
n = nrow(dat);
like = 0
for( i in 1:n )
{
# Find matrix coord cooresponding to actual coord
x = max(1,which(den$x < dat$fx[i]));
y = max(1,which(den$y < dat$fy[i]));
like = like + log(den$z[x,y]);
}
return(like);
}
#
# Compute Likelihood in given kmean clusters km and
# posterior hyperparameters for the Markov chain.
#
calcLike1 <- function( dens, markv, km, dat, mcDraws )
{
n = nrow(dat);
likeTot = 0;
# MC integration of Bayes factor
# over the Dirichlet distribution
for( m in 1:mcDraws )
{
# Draw initial and transition probabilities
# from the Dirichlet dist
iProb = rdirich( markv$iProb );
tProb = rdirich( markv$tProb );
# Calc initial fixation probability
i = 1;
cl = dat$cluster[i];
x = max(1,which(dens[[cl]]$x < dat$fx[i]));
y = max(1,which(dens[[cl]]$y < dat$fy[i]));
like = log(iProb[cl]) + log(dens[[cl]]$z[x,y]);
for( i in 2:n ) # for each subsequent fixation
{
# Calc fixation likelihood and transition probability
old= dat$cluster[i-1];
cl = dat$cluster[i];
x = max(1,which(dens[[cl]]$x < dat$fx[i]));
y = max(1,which(dens[[cl]]$y < dat$fy[i]));
like = like + log(tProb[old,cl]) + log(dens[[cl]]$z[x,y]);
}
likeTot = likeTot + exp(like);
}
return(log(likeTot)-log(mcDraws));
}
#
# Construct a list of density estimates,
# one for each cluster
#
constrClustDen <- function(train,k)
{
dens = list();
for( i in 1:k )
{
clust = subset(train,cluster==i);
#print(paste(bandwidth.nrd(clust$fx),bandwidth.nrd(clust$fy)));
#print(rbind(clust$fx,clust$fy));
dens[[i]] = kde2d(
clust$fx,clust$fy,lims = c(eyeXlim, eyeYlim),n=GRID_SIZE
);
}
return(dens);
}
#
# Use observed data to update Dirichlet priors
#
rss_constrMarkov <- function( n,k,type,dat,km )
{
chr = switch(
type,
normal = "n",
abnormal = "a",
grayscale= "g"
);
trial = list();
# Jeffreys Priors
initP = rep(1/2,k);
trans = matrix(1/2,k,k);
for( i in 1:10 )
{
# Look at ith subject
trial[[i]] = subset(dat,dat$subject==paste(chr,i,sep=""));
trial[[i]] = rss_sortFix(trial[[i]]);
# Skip if no data
if(nrow(trial[[i]])==0) next;
# Update Priors
initP[trial[[i]]$cluster[1]] = initP[trial[[i]]$cluster[1]]+1;
for( j in 2:nrow(trial[[i]]) )
trans[trial[[i]]$cluster[j-1],trial[[i]]$cluster[j]] =
trans[trial[[i]]$cluster[j-1],trial[[i]]$cluster[j]]+1;
}
# Order probabilities and clusters based
# on the most populous cluster
#ord = order(km$size,decreasing=TRUE);
ord = order(km$centers[,1],decreasing=FALSE);
ord2 = order(ord);
initP = initP[ord];
trans = trans[ord,];
trans = trans[,ord];
km$cluster = ord2[km$cluster];
km$centers = km$centers[ord,];
#initP = initP/sum(initP);
#trans = trans/rowSums(trans);
return(list(iProb=initP,tProb=trans,km=km));
}
#
# Plotting Functions
#
rss_plotCluster <- function(n,k,type,kdat,km,NOTITLE=FALSE)
{
img = rss_readImage(n,type);
mycol = rainbow(k);
if(NOTITLE){
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xlab="",ylab="",
type="n",xaxt='n',yaxt='n'
);
} else {
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xlab="",ylab="",
main=paste("Fixations on image",n),type="n",
xaxt='n',yaxt='n'
);
}
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( kdat, pch=19, col=mycol[km$cluster], cex=1.1 );
points( kdat, pch=1, col="black", cex=1.1 );
points( km$centers, col=mycol, pch = 13, cex = 2)
}
rss_plotTrainTest <- function(n,k,type,train,test,km)
{
img = rss_readImage(n,type);
mycol = rainbow(k);
plot(
0, 0, xlim=eyeXlim, ylim=eyeYlim, xlab="x",ylab="y",
main=paste("Fixations on image",n),type="n"
);
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( train$fx,train$fy, col=mycol[train$cluster], cex=1.1 );
points( test$fx,test$fy, pch=19, col=mycol[test$cluster], cex=1.1 );
points( test$fx,test$fy, pch=1, col="black", cex=1.1 );
points( km$centers, col=mycol, pch = 13, cex = 2)
}
rss_clusterDensity <- function(n,k,type,bf,PRINT=TRUE)
{
dat = rss_getData(n,type);
kdat= cbind(dat$fx,dat$fy);
colnames(kdat) <- c("x","y");
km = kmeans(kdat,k,iter.max=20,nstart=10);
## reorder
ord = order(km$centers[,1],decreasing=FALSE);
ord2 = order(ord);
km$cluster = ord2[km$cluster];
km$centers = km$centers[ord,];
##
print(paste("Type:",type));
if(PRINT){
for( i in 1:k )
print(paste(" Cluster ",i,": ",sum(km$cluster==i),sep=""));
}
dat = cbind(dat,cluster=km$cluster);
dens = constrClustDen(dat,k);
rss_plotClusterDensity(n,k,type,dat,km,dens);
title(
paste("Fixations on Image ",n,", Bayes Factor = ",signif(bf,3),sep=""),
line=-2.5,outer=TRUE,cex.main=3
);
return(dat);
}
rss_plotClusterDensity <- function(n,k,type,dat,km,dens)
{
mm = length(dens);
if(mm==1){
par(mfrow=c(1,2));
} else if(mm==2) {
layout(matrix(c(1,1,1,1,2,3), 2, 3, byrow = FALSE));
} else if(mm<5) {
layout(matrix(c(1,1,1,1,2,4,3,5), 2, 4, byrow = FALSE));
} else if(mm<7) {
layout(matrix(c(1,1,1,1,2,5,3,6,4,7), 2, 5, byrow = FALSE));
} else {
layout(matrix(c(rep(1,9),2,5,8,3,6,9,4,7,10), 3, 6, byrow = FALSE));
}
rss_plotCluster(n,k,type,dat,km,NOTITLE=TRUE);
for( i in 1:k )
{
clust = subset(dat,cluster==i);
dens[[i]] = kde2d(
clust$fx,clust$fy,lims = c(eyeXlim, eyeYlim),n=100
);
}
for( i in 1:k )
{
image(
dens[[i]]$x,dens[[i]]$y,log(dens[[i]]$z),
zlim=max(log(dens[[i]]$z))+c(-10,0),
xlab='',ylab='',xaxt='n',yaxt='n'
);
for( j in 1:k )
image(dens[[j]]$x,dens[[j]]$y,log(dens[[j]]$z),
zlim=max(log(dens[[j]]$z))+c(-10,0),
add=TRUE,col=cm.colors(12,alpha=0.5)
);
image(
dens[[i]]$x,dens[[i]]$y,log(dens[[i]]$z),
zlim=max(log(dens[[i]]$z))+c(-10,0),add=TRUE
);
}
}
##########
# Functions related to reading in the data.
##########
#
# Read in Image of #n of type 'type'
# where 'type' is one of "normal", "grayscale", "abnormal"
#
rss_readImage <- function( n, type )
{
typeCode = switch(
type,
normal = "c",
abnormal = "ab",
grayscale= "bw"
);
return(
readPNG(
paste(
dataDir,'images/',type,'/',n,'_',typeCode,".png",sep=""
)
)
);
}
#
# Extract data subset of
# image imag in {1,...,60}
# condition type in {"normal","grayscale","abnormal"}
# subject subj in {1,...,10}
# fixation fixt in {1,...,26}
#
rss_getData <- function( imag=F, type=F, subj=F, fixt=F )
{
if(type!=F)
{
chr = switch(
type,
normal = "n",
abnormal = "a",
grayscale= "g"
);
}
return(
subset(
eyeData,
tern(imag==F,T,image==imag) &
tern(type==F,T,condition==type) &
tern(subj==F,T,subject==paste(chr,subj,sep="")) &
tern(fixt==F,T,fixation==fixt)
)
);
}
#
# Computes Euclidean distance between adjacent fixations
#
rss_computeSacs <- function()
{
dat = rss_getData();
dat = dat[ order(dat$subject,dat$image,dat$fixation), ];
n = nrow(dat);
sac = sqrt(
(dat$fx[2:n]-dat$fx[1:(n-1)])^2 +
(dat$fy[2:n]-dat$fy[1:(n-1)])^2
);
indx= dat$image[1:(n-1)]==dat$image[2:n];
sac = c(0,sac);
indx= c(0,indx);
dat$sac = sac*indx;
return(dat);
}
#
# Similar to rss_getData, but returns Euclidean
# distance between successive fixations
#
rss_getSacData <- function( imag=F, type=F, subj=F, fixt=F )
{
sacData = rss_computeSacs();
if(type!=F)
{
chr = switch(
type,
normal = "n",
abnormal = "a",
grayscale= "g"
);
}
return(
subset(
sacData,
tern(imag==F,T,image==imag) &
tern(type==F,T,condition==type) &
tern(subj==F,T,subject==paste(chr,subj,sep="")) &
tern(fixt==F,T,fixation==fixt)
)
);
}
#
# Sort by fixation
#
rss_sortFix <- function( dat )
{
return( dat[order(dat$fixation),] );
}
#
# Read in image and plot data
#
rss_plotImageData <- function( n, type )
{
img = rss_readImage(n,type);
dat = rss_getData(imag=n,type=type);
plot(
0, 0, xlim=eyeXlim, ylim=eyeYlim, xlab="x",ylab="y",
main=paste("Fixations on image",n),type="n"
);
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( dat$fx, dat$fy, pch=19, col="red", cex=1.1 );
}
rss_plotMultiPics <- function( images, type )
{
n = length(images);
d = ceiling(sqrt(n));
par(mfrow=c(d,d));
for( i in images )
rss_plotImageData( i, type );
}
#
# Misc
#
# Subject String
subj <- function(type,s)
{
chr=switch(
type,
normal = "n",
abnormal = "a",
grayscale= "g"
);
return(paste(chr,s,sep=""));
}
# Ternary operator
tern <- function( a, b, c)
{
if(a){
return(b);
} else {
return(c);
}
}
#
# Choose best Cluster
#
# use k-nearest-neighbours
chooseClust2 <- function( test, train, k=3 )
{
res = knn(
cbind(train$fx,train$fy),cbind(test$fx,test$fy),
train$cluster,k=k
);
}
# use distance to center
chooseClust <- function( x, y, train )
{
dst = rep(0,nrow(km$centers));
for( i in 1:nrow(km$centers) )
{
dst[i] =
(x-km$centers[i,1])^2 + (y-km$centers[i,2])^2;
}
return(which.min(dst));
}
#
# Random Dirichlet draw
#
rdirich <- function( alf )
{
n = length(alf)
out = rgamma(n,alf,1);
if(is.matrix(alf)){
out= matrix(out,nrow(alf),ncol(alf));
return( out/rowSums(out) );
} else {
return( out/sum(out) );
}
}
#
# Row/Column min/max
#
rMin <- function(mat){
return(do.call(pmin, as.data.frame(mat)));
}
rMax <- function(mat){
return(do.call(pmax, as.data.frame(mat)));
}
cMax <- function(mat){
return(do.call(pmax, as.data.frame(t(mat))));
}
cMin <- function(mat){
return(do.call(pmin, as.data.frame(t(mat))));
}
######################################
#### Extra
######################################
rss_plotImageData2 <- function( n, type, NOAXIS=TRUE )
{
img = rss_readImage(n,type);
dat = rss_getData(imag=n,type=type);
if(NOAXIS){
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xaxt='n',yaxt='n',
xlab="",ylab="",type="n"
);
} else {
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xlab="x",ylab="y",
type="n"
);
}
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( dat$fx, dat$fy, pch=19, col="red", cex=1 );
points( dat$fx, dat$fy, pch=1, col="black", cex=1 );
}
rss_plotImageClusters2 <- function(
n, type, k, meth="complete", dst="euclidean"
){
img = rss_readImage(n,type);
dat = rss_getData(imag=n,type=type);
# k-means
kdat = cbind(dat$fx,dat$fy);
colnames(kdat) = c("x","y");
km = kmeans( kdat,k,iter.max=20,nstart=10 );
dat$kClust = km$cluster;
# hierachical
hc = hclust( dist(cbind(dat$fx,dat$fy),method=dst),method=meth );
dat$hClust = cutree(hc, k = k);
par(mfcol=c(1,2));
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xlab="x",ylab="y",
type="n"
);
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( dat$fx, dat$fy, pch=19, col=dat$kClust+1, cex=1 );
points( dat$fx, dat$fy, pch=1, col="black", cex=1 );
title("k-means Clustering",cex.main=2);
plot(
0, 0, xlim=eyeXlim*5/6, ylim=eyeYlim*4/5, xlab="x",ylab="y",
type="n"
);
rasterImage( img ,
eyeXlim.landscape[1], eyeYlim.landscape[1],
eyeXlim.landscape[2], eyeYlim.landscape[2]
);
points( dat$fx, dat$fy, pch=19, col=dat$hClust+1, cex=1 );
points( dat$fx, dat$fy, pch=1, col="black", cex=1 );
title("Hierarchical Clustering",cex.main=2);
}