-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_par.rb
303 lines (246 loc) · 9.9 KB
/
plot_par.rb
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
# encoding: UTF-8
#input
#total_threshold
#condition on threshold
require 'rinruby'
require_relative 'read_cmd.rb'
require_relative 'date_tools.rb'
require_relative 'math_tools.rb'
gran_hash = {"m" => "-----m"}
qtype = "time"
query = "time"
def extract_data(corpus_and_label,inputdir,variable,username,gran_addendum,whattoplot,nvariants,total_threshold,window)
maincorpus = corpus_and_label.split("-")[0]
subcorpus = corpus_and_label.split("-")[1]
file = File.open("#{inputdir}\\#{variable}#{gran_addendum}\\#{maincorpus}\\#{subcorpus}\\#{username}.tsv","r:utf-8")
#verb = variable.split("_")[1]
#file = File.open("results\\ss30dia\\#{verb}_t10.tsv","r:utf-8")
#file = File.open("C:\\Sasha\\D\\DGU\\CassandraMy\\hbt_authors\\Varnagel.tsv","r:utf-8")
header = []
years = []
labels = []
values = []
values_copy = []
values_copy2 = []
whattoplot_id = nil
period_id = nil
#absmonth_id = nil
array_ndatapoints = []
file.each_line.with_index do |line,index|
if index > 0
line1 = line.split("\t")
if gran_addendum.nil?
years << line1[period_id].to_i
elsif gran_addendum == "-----m"
labels << line1[period_id]
years << absmonth(line1[period_id])
end
ndatapoints = line1[1].to_i
array_ndatapoints << ndatapoints
if nvariants == 1 or (nvariants == 2 and ndatapoints >= total_threshold)
values << line1[whattoplot_id].to_f
values_copy2 << line1[whattoplot_id].to_f
else
values << "NA"
end
values_copy << line1[whattoplot_id].to_f
else
header = line.strip.split("\t")
whattoplot_id = header.index(whattoplot)
period_id = header.index("period")
#absmonth_id = header.index("absmonth")
end
end
file.close
if window > 1
values = smooth(values_copy,window,array_ndatapoints,total_threshold)
end
return [years,values,labels,maincorpus,subcorpus,values_copy2]
end
outhash = process_cmd
corpus_and_label = outhash["corpus_and_label"]
more_corpora_and_labels = outhash["more_corpora_and_labels"].to_s.split(",")
more_variables = outhash["more_variables"].to_s.split(",")
whattoplot = outhash["whattoplot"]
max_predef = outhash["max"]
var_output = outhash["var_output"]
if var_output.to_s == ""
dir = outhash["dir"]
else
dir = "#{var_output}\\#{outhash["dir"]}"
end
nyl_year = outhash["nyl_year"]
#variable = outhash["variable"]
username = outhash["username"]
nvariants = outhash["nvariants"]
only_process_local = outhash["only_process_local"]
granularity = outhash["granularity"]
total_threshold = outhash["total_threshold"]
format = outhash["format"]
showplot = outhash["showplot"]
window = outhash["window"]
defaultyaxis = outhash["defaultyaxis"]
#dir = "#{var_output}#{dir}"
if !Dir.exist?(dir)
Dir.mkdir(dir)
end
R.eval "setwd('#{dir}')"
R.eval "#{format}(file='ss12_#{corpus_and_label}_all-users_#{whattoplot}_#{granularity}_#{window}_dyaxis#{defaultyaxis}.#{format}')"
plotfilename = "#{dir}\\ss12_#{corpus_and_label}_all-users_#{whattoplot}_#{granularity}_#{window}_dyaxis#{defaultyaxis}.#{format}"
R.eval "par(mfrow=c(4,3))"
if ARGV.include?("nyordslistor")
inputdir = "#{var_output}variables\\nyordslistor2"
else
inputdir = "#{var_output}variables"
end
variables = ["ss90_behaga", "ss90_fortsätta", "ss90_försöka", "ss90_glömma", "ss90_komma", "ss90_lova", "ss90_planera", "ss90_riskera","ss90_slippa", "ss90_sluta", "ss90_vägra"]
gran_addendum = gran_hash[granularity]
variables.each do |variable|
verb = variable.split("_")[1].encode("windows-1252")
STDERR.puts verb
more_years = []
more_values = []
var_more_values = []
#more_labels = []
colors = ["black","blue","green","red","gray","magenta","brown","orange"]
ltys = [1,5,3,4,2]
plot_data = extract_data(corpus_and_label,inputdir,variable,username,gran_addendum,whattoplot,nvariants,total_threshold,window)
years = plot_data[0]
values = plot_data[1]
labels = plot_data[2]
maincorpus = plot_data[3]
subcorpus = plot_data[4]
values_no_nas = plot_data[5]
if values_no_nas.empty?
maxvalue2 = 1
minvalue2 = 0
else
maxvalue2 = values_no_nas.max.round(2)+0.01
if maxvalue2 > 1
maxvalue2 = 1
end
minvalue2 = values_no_nas.min.round(2)-0.01
if minvalue2 < 0
minvalue2 = 0
end
end
R.assign "maxvalue2",maxvalue2
R.assign "minvalue2",minvalue2
if defaultyaxis == "no"
yinfo = "ylim = c(0,maxvalue), "
elsif defaultyaxis == "yes"
yinfo = "ylim = c(minvalue2,maxvalue2), "
end
if !years.empty?
R.assign "years", years
R.assign "values", values
R.assign "labels",labels
all_names = ["#{maincorpus}_#{subcorpus}"]
all_values = [values]
all_years = [years]
var_all_names = [variable]
if !more_corpora_and_labels.empty?
#all_values = [values]
more_corpora_and_labels.each.with_index do |extra_corpus_and_label,index|
plot_data = extract_data(extra_corpus_and_label,inputdir,variable,username,gran_addendum,whattoplot,nvariants,total_threshold,window)
more_years[index] = plot_data[0]
more_values[index] = plot_data[1]
#more_labels[index] = plot_data[2]
all_values << plot_data[1]
all_years << plot_data[0]
all_names << "#{plot_data[3]}_#{plot_data[4]}"
end
end
if !more_variables.empty?
more_variables.each.with_index do |extra_variable,index|
plot_data = extract_data(corpus_and_label,inputdir,extra_variable,username,gran_addendum,whattoplot,nvariants,total_threshold,window)
var_more_values[index] = plot_data[1]
all_values << plot_data[1]
var_all_names << extra_variable.gsub(":","_colon_")
end
end
all_values.flatten!
all_years.flatten!
all_years.uniq!
minyear = all_years.min
maxyear = all_years.max
#STDERR.puts minyear
#STDERR.puts maxyear
R.assign "minyear",minyear
R.assign "maxyear",maxyear
namelist = all_names.join("_")
var_namelist = var_all_names.join("_")
if max_predef != nil
max = max_predef
#elsif (nvariants == 1 or whattoplot == "total") and more_corpora_and_labels.empty? and more_variables.
# max = values.max
elsif (nvariants == 1 or whattoplot == "total") #and !more_corpora_and_labels.empty?
max = all_values.max
elsif nvariants == 2
max = 1
end
#STDERR.puts max
R.assign "maxvalue", max
#R.eval "#{format}(file='#{var_namelist}_#{namelist}_#{username.gsub(":","_colon_")}_#{whattoplot}_#{granularity}_#{window}.#{format}')"
#plotfilename = "#{dir}\\#{var_namelist}_#{namelist}_#{username.gsub(":","_colon_")}_#{whattoplot}_#{granularity}_#{window}.#{format}"
if nvariants == 1
ylab = "ipm"
else
ylab = "proportion incoming"
end
if granularity == "y"
#R.eval "plot(0,0,xlab = 'HEY', ylab = '#{ylab}', xlim = c(minyear,maxyear), ylim = c(0,maxvalue),frame.plot=FALSE)"
#R.eval "plot(values~years, type='b',xlab = 'time', ylab = '#{ylab}', xlim = c(minyear,maxyear), #{yinfo}lwd =2, main = #{verb})"
R.eval "plot(values~years, type='b', xlab = '', ylab = '', xlim = c(minyear,maxyear), #{yinfo}main = \"#{verb}\", yaxt='n')"
if defaultyaxis == "no"
R.eval "axis(2, at=c(0,maxvalue),labels = c(0,maxvalue))"
elsif
R.eval "axis(2, at=c(minvalue2,maxvalue2),labels = c(minvalue2,maxvalue2))"
else
end
elsif granularity == "m" #does not work for plot_par
R.eval "plot(years, values, type='l',xaxt='n', ylab = '#{ylab}', xlim = c(minyear,maxyear), #{yinfo}lwd =2)"
R.eval "axis(1, at=years,labels = labels)"
end
if !more_corpora_and_labels.empty?
for i in 0..more_corpora_and_labels.length-1
R.assign "years2", more_years[i]
R.assign "values2", more_values[i]
color = colors[i+1]
if granularity == "y"
type = "b"
elsif granularity == "m"
type = "l"
end
R.eval "lines(years2, values2, type='#{type}', col = '#{color}', lwd = 2)"
end
R.assign "namelist",all_names
R.assign "colors",colors
R.eval "legend('topleft', legend=namelist,col=colors,lty=1,bty='n')"
end
if !more_variables.empty?
for i in 0..more_variables.length-1
R.assign "values2", var_more_values[i]
lty = ltys[i+1]
if granularity == "y"
type = "b"
elsif granularity == "m"
type = "l"
end
R.eval "lines(years, values2, type='#{type}', lty = #{lty}, lwd = 2)"
end
R.assign "namelist",var_all_names
R.assign "ltys",ltys
R.eval "legend('topleft', legend=namelist,lty=ltys,bty='n')"
end
if !nyl_year.nil?
R.eval "abline(v=#{nyl_year}, col = 'red', lwd = 2)"
end
#R.eval "dev.off()"
#STDERR.puts plotfilename
end
end
R.eval "dev.off()"
if showplot != "no"
system "start #{plotfilename}"
end