comparison utils.R @ 0:4dccc60b3525 draft

Uploaded
author greg
date Tue, 31 Jul 2018 14:36:56 -0400
parents
children 9c5a4d07edb8
comparison
equal deleted inserted replaced
-1:000000000000 0:4dccc60b3525
1 #!/usr/bin/env Rscript
2
3 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {
4 if (!is.null(life_stage_nymph)) {
5 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph);
6 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_");
7 } else if (!is.null(life_stage_adult)) {
8 lsi = get_life_stage_index(life_stage, life_stage_adult=life_stage_adult);
9 file_name = paste(lsi, tolower(life_stage_adult), base_name, sep="_");
10 } else {
11 lsi = get_life_stage_index(life_stage);
12 file_name = paste(lsi, base_name, sep="_");
13 }
14 file_path = paste("output_plots_dir", file_name, sep="/");
15 return(file_path);
16 }
17
18 get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) {
19 # Name collection elements so that they
20 # are displayed in logical order.
21 if (life_stage=="Egg") {
22 lsi = "01";
23 } else if (life_stage=="Nymph") {
24 if (life_stage_nymph=="Young") {
25 lsi = "02";
26 } else if (life_stage_nymph=="Old") {
27 lsi = "03";
28 } else if (life_stage_nymph=="Total") {
29 lsi="04";
30 }
31 } else if (life_stage=="Adult") {
32 if (life_stage_adult=="Pre-vittelogenic") {
33 lsi = "05";
34 } else if (life_stage_adult=="Vittelogenic") {
35 lsi = "06";
36 } else if (life_stage_adult=="Diapausing") {
37 lsi = "07";
38 } else if (life_stage_adult=="Total") {
39 lsi = "08";
40 }
41 } else if (life_stage=="Total") {
42 lsi = "09";
43 }
44 return(lsi);
45 }
46
47 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
48 # P mean.
49 p_m = apply(p_replications, 1, mean);
50 # P standard error.
51 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
52 # F1 mean.
53 f1_m = apply(f1_replications, 1, mean);
54 # F1 standard error.
55 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
56 # F2 mean.
57 f2_m = apply(f2_replications, 1, mean);
58 # F2 standard error.
59 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
60 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
61 }
62
63 get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) {
64 # The R code tries hard not to draw overlapping tick labels, and so
65 # will omit labels where they would abut or overlap previously drawn
66 # labels. This can result in, for example, every other tick being
67 # labelled. We'll keep track of the last tick to make sure all of
68 # the month labels are displayed, and missing ticks are restricted
69 # to Sundays which have no labels anyway.
70 if (last_tick==0) {
71 return(length(ticks)+1);
72 }
73 last_saved_tick = ticks[[length(ticks)]];
74 if (index-last_saved_tick<tick_sep) {
75 last_saved_month = tick_labels[[length(tick_labels)]];
76 if (last_saved_month=="") {
77 # We're safe overwriting a tick
78 # with no label (i.e., a Sunday tick).
79 return(length(ticks));
80 } else {
81 # Don't eliminate a Month label.
82 return(NULL);
83 }
84 }
85 return(length(ticks)+1);
86 }
87
88 get_total_days = function(is_leap_year) {
89 # Get the total number of days in the current year.
90 if (is_leap_year) {
91 return(366);
92 } else {
93 return(365);
94 }
95 }
96
97 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, date_interval=FALSE) {
98 # Generate a list of ticks and labels for plotting the x axis.
99 if (prepend_end_doy_norm > 0) {
100 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
101 } else {
102 prepend_end_norm_row = 0;
103 }
104 if (append_start_doy_norm > 0) {
105 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
106 } else {
107 append_start_norm_row = 0;
108 }
109 num_rows = dim(temperature_data_frame)[1];
110 tick_labels = list();
111 ticks = list();
112 current_month_label = NULL;
113 last_tick = 0;
114 if (date_interval) {
115 tick_sep = 0;
116 } else {
117 tick_sep = 3;
118 }
119 for (i in 1:num_rows) {
120 # Get the year and month from the date which
121 # has the format YYYY-MM-DD.
122 date = format(temperature_data_frame$DATE[i]);
123 # Get the month label.
124 items = strsplit(date, "-")[[1]];
125 month = items[2];
126 month_label = month.abb[as.integer(month)];
127 day = as.integer(items[3]);
128 doy = as.integer(temperature_data_frame$DOY[i]);
129 # We're plotting the entire year, so ticks will
130 # occur on Sundays and the first of each month.
131 if (i == prepend_end_norm_row) {
132 # Add a tick for the end of the 30 year normnals data
133 # that was prepended to the year-to-date data.
134 label_str = "End prepended 30 year normals";
135 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
136 ticks[tick_index] = i;
137 if (date_interval) {
138 # Append the day to label_str
139 tick_labels[tick_index] = paste(label_str, day, sep=" ");
140 } else {
141 tick_labels[tick_index] = label_str;
142 }
143 last_tick = i;
144 } else if (doy == append_start_doy_norm) {
145 # Add a tick for the start of the 30 year normnals data
146 # that was appended to the year-to-date data.
147 label_str = "Start appended 30 year normals";
148 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
149 ticks[tick_index] = i;
150 if (!identical(current_month_label, month_label)) {
151 # Append the month to label_str.
152 label_str = paste(label_str, month_label, spe=" ");
153 current_month_label = month_label;
154 }
155 if (date_interval) {
156 # Append the day to label_str
157 label_str = paste(label_str, day, sep=" ");
158 }
159 tick_labels[tick_index] = label_str;
160 last_tick = i;
161 } else if (i==num_rows) {
162 # Add a tick for the last day of the year.
163 label_str = "";
164 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
165 ticks[tick_index] = i;
166 if (!identical(current_month_label, month_label)) {
167 # Append the month to label_str.
168 label_str = month_label;
169 current_month_label = month_label;
170 }
171 if (date_interval) {
172 # Append the day to label_str
173 label_str = paste(label_str, day, sep=" ");
174 }
175 tick_labels[tick_index] = label_str;
176 } else {
177 if (!identical(current_month_label, month_label)) {
178 # Add a tick for the month.
179 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
180 ticks[tick_index] = i;
181 if (date_interval) {
182 # Append the day to the month.
183 tick_labels[tick_index] = paste(month_label, day, sep=" ");
184 } else {
185 tick_labels[tick_index] = month_label;
186 }
187 current_month_label = month_label;
188 last_tick = i;
189 }
190 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
191 if (!is.null(tick_index)) {
192 if (date_interval) {
193 # Add a tick for every day. The first tick is the
194 # month label, so add a tick only if i is not 1
195 if (i>1 & day>1) {
196 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
197 ticks[tick_index] = i;
198 # Add the day as the label.
199 tick_labels[tick_index] = day;
200 last_tick = i;
201 }
202 } else {
203 # Get the day.
204 day = weekdays(as.Date(date));
205 if (day=="Sunday") {
206 # Add a tick if we're on a Sunday.
207 ticks[tick_index] = i;
208 # Add a blank month label so it is not displayed.
209 tick_labels[tick_index] = "";
210 last_tick = i;
211 }
212 }
213 }
214 }
215 }
216 return(list(ticks, tick_labels));
217 }
218
219 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
220 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
221 life_stages_adult=NULL, life_stages_nymph=NULL) {
222 if (chart_type=="pop_size_by_life_stage") {
223 if (life_stage=="Total") {
224 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
225 legend_text = c("Egg", "Nymph", "Adult");
226 columns = c(4, 2, 1);
227 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
228 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
229 lines(days, group2, lwd=2, lty=1, col=2);
230 lines(days, group3, lwd=2, lty=1, col=4);
231 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
232 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
233 if (plot_std_error=="yes") {
234 # Standard error for group.
235 lines(days, group+group_std_error, lty=2);
236 lines(days, group-group_std_error, lty=2);
237 # Standard error for group2.
238 lines(days, group2+group2_std_error, col=2, lty=2);
239 lines(days, group2-group2_std_error, col=2, lty=2);
240 # Standard error for group3.
241 lines(days, group3+group3_std_error, col=4, lty=2);
242 lines(days, group3-group3_std_error, col=4, lty=2);
243 }
244 } else {
245 if (life_stage=="Egg") {
246 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
247 legend_text = c(life_stage);
248 columns = c(4);
249 } else if (life_stage=="Nymph") {
250 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
251 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
252 legend_text = c(paste(life_stages_nymph, life_stage, sep=" "));
253 columns = c(2);
254 } else if (life_stage=="Adult") {
255 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
256 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
257 legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
258 columns = c(1);
259 }
260 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
261 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
262 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
263 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
264 if (plot_std_error=="yes") {
265 # Standard error for group.
266 lines(days, group+group_std_error, lty=2);
267 lines(days, group-group_std_error, lty=2);
268 }
269 }
270 } else if (chart_type=="pop_size_by_generation") {
271 if (life_stage=="Total") {
272 title_str = ": Total Pop by Gen :";
273 } else if (life_stage=="Egg") {
274 title_str = ": Egg Pop by Gen :";
275 } else if (life_stage=="Nymph") {
276 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
277 } else if (life_stage=="Adult") {
278 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
279 }
280 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
281 legend_text = c("P", "F1", "F2");
282 columns = c(1, 2, 4);
283 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
284 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
285 lines(days, group2, lwd=2, lty=1, col=2);
286 lines(days, group3, lwd=2, lty=1, col=4);
287 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
288 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
289 if (plot_std_error=="yes") {
290 # Standard error for group.
291 lines(days, group+group_std_error, lty=2);
292 lines(days, group-group_std_error, lty=2);
293 # Standard error for group2.
294 lines(days, group2+group2_std_error, col=2, lty=2);
295 lines(days, group2-group2_std_error, col=2, lty=2);
296 # Standard error for group3.
297 lines(days, group3+group3_std_error, col=4, lty=2);
298 lines(days, group3-group3_std_error, col=4, lty=2);
299 }
300 }
301 }
302
303 stop_err = function(msg) {
304 cat(msg, file=stderr());
305 quit(save="no", status=1);
306 }
307
308 validate_date = function(date_str) {
309 valid_date = as.Date(date_str, format="%Y-%m-%d");
310 if( class(valid_date)=="try-error" || is.na(valid_date)) {
311 msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
312 stop_err(msg);
313 }
314 return(valid_date);
315 }