0
|
1 #!/usr/bin/env Rscript
|
|
2 args = commandArgs(trailingOnly=TRUE)
|
|
3
|
|
4 if(length(args) < 1){
|
|
5 stop("USE: Rscript --vanilla plot.R <matrix>")
|
|
6 }
|
|
7
|
|
8
|
|
9 path_mat = args[1]
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14 data <- as.matrix(read.csv(path_mat, sep = " "))
|
|
15 percentage <- 0.1
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20 d <- col(data) - row(data)
|
|
21 groups <- split(data, d)
|
|
22 acum <- c()
|
|
23 for(i in 1:length(groups)){
|
|
24 acum <- c(acum, sum(unlist(groups[i])))
|
|
25 }
|
|
26
|
|
27
|
|
28 # reverse
|
|
29 data_r <- data
|
|
30 for(i in 1:length(data_r[,1])){
|
|
31 data_r[i,] <- rev(data_r[i,])
|
|
32 }
|
|
33 d <- col(data_r) - row(data_r)
|
|
34 groups <- split(data_r, d)
|
|
35 acum_r <- c()
|
|
36 for(i in 1:length(groups)){
|
|
37 acum_r <- c(acum, sum(unlist(groups[i]))/length(unlist(groups[i])) )
|
|
38 }
|
|
39
|
|
40 indexes <- matrix(0, nrow=length(acum), ncol = 3)
|
|
41 indexes[,1] <- c(1:length(acum))
|
|
42 indexes[,2] <- acum
|
|
43
|
|
44 #sort
|
|
45 indexes[order(indexes[,2], decreasing=TRUE),]
|
|
46
|
|
47 #crop by percentage
|
|
48 n_percent <- percentage * length(acum)
|
|
49
|
|
50 for(i in 1:n_percent){
|
|
51 indexes[i,3] <- 1
|
|
52 }
|
|
53
|
|
54 # Now resort based on first column to have them again in order
|
|
55 indexes[order(indexes[,1], decreasing=FALSE),]
|
|
56
|
|
57
|
|
58 # same in reverse
|
|
59 #put into indexes to sort
|
|
60 indexes_r <- matrix(0, nrow=length(acum_r), ncol = 3)
|
|
61 indexes_r[,1] <- c(1:length(acum_r))
|
|
62 indexes_r[,2] <- acum_r
|
|
63
|
|
64 #sort
|
|
65 indexes_r[order(indexes_r[,2], decreasing=TRUE),]
|
|
66
|
|
67 #crop by percentage
|
|
68 n_percent <- percentage * length(acum_r)
|
|
69
|
|
70 for(i in 1:n_percent){
|
|
71 indexes_r[i,3] <- 1
|
|
72 }
|
|
73
|
|
74 # Now resort based on first column to have them again in order
|
|
75 indexes_r[order(indexes_r[,1], decreasing=FALSE),]
|
|
76
|
|
77
|
|
78
|
|
79
|
|
80 finalmat <- matrix(0, nrow=length(data[,1]), ncol=length(data[1,]))
|
|
81
|
|
82
|
|
83 for(i in length(data[1,]):1){
|
|
84 x <- i
|
|
85 y <- 1
|
|
86
|
|
87 if(indexes[i,3] == 1){
|
|
88 while(x < length(data[1,]) && y <= length(data[,1])){
|
|
89
|
|
90
|
|
91 if(x < length(data[1,]) && y <= length(data[,1])){
|
|
92 finalmat[x,y] <- 1
|
|
93 }
|
|
94
|
|
95 y <- y + 1
|
|
96 x <- x + 1
|
|
97
|
|
98
|
|
99 }
|
|
100 }
|
|
101
|
|
102 }
|
|
103
|
|
104 for(i in 2:length(data[,1])){
|
|
105 x <- 1
|
|
106 y <- i
|
|
107
|
|
108 if(indexes[i,3] == 1){
|
|
109
|
|
110 while(x < length(data[1,]) && y <= length(data[,1])){
|
|
111
|
|
112 if(x < length(data[1,]) && y <= length(data[,1])){
|
|
113 finalmat[x,y] <- 1
|
|
114 }
|
|
115 y <- y + 1
|
|
116 x <- x + 1
|
|
117
|
|
118 }
|
|
119 }
|
|
120 }
|
|
121
|
|
122
|
|
123 #same for reverse
|
|
124 for(i in length(data_r[1,]):1){
|
|
125 x <- i
|
|
126 y <- 1
|
|
127
|
|
128 if(indexes[i,3] == 1){
|
|
129 while(x < length(data_r[1,]) && y <= length(data_r[,1])){
|
|
130
|
|
131
|
|
132 if(x < length(data_r[1,]) && y <= length(data_r[,1])){
|
|
133 finalmat[x,length(data_r[,1])-y] <- 1
|
|
134 }
|
|
135
|
|
136 y <- y + 1
|
|
137 x <- x + 1
|
|
138
|
|
139
|
|
140 }
|
|
141 }
|
|
142
|
|
143 }
|
|
144
|
|
145 for(i in 2:length(data_r[,1])){
|
|
146 x <- 1
|
|
147 y <- i
|
|
148
|
|
149 if(indexes[i,3] == 1){
|
|
150
|
|
151 while(x < length(data_r[1,]) && y <= length(data_r[,1])){
|
|
152
|
|
153 if(x < length(data_r[1,]) && y <= length(data_r[,1])){
|
|
154 finalmat[x,length(data_r[,1])-y] <- 1
|
|
155 }
|
|
156 y <- y + 1
|
|
157 x <- x + 1
|
|
158
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162
|
|
163 # fully write last
|
|
164
|
|
165 for(i in 1:length(data[,1])){
|
|
166 for(j in 1:length(data[1,])){
|
|
167 if(finalmat[i,j] == 0){
|
|
168
|
|
169 data[i,j] <- 0
|
|
170 }
|
|
171 }
|
|
172 }
|
|
173
|
|
174 png(paste(path_mat, ".png", sep=""), width = length(data[,1]), height = length(data[,1]))
|
|
175 image((data), col = grey(seq(1, 0, length = 256)), xaxt='n', yaxt='n')
|
|
176 dev.off()
|
|
177
|
|
178
|
|
179
|