Repository 'insect_phenology_model'
hg clone https://toolshed.g2.bx.psu.edu/repos/greg/insect_phenology_model

Changeset 59:892cf703be62 (2018-11-21)
Previous changeset 58:2194155309f4 (2018-11-09) Next changeset 60:393085589438 (2018-12-20)
Commit message:
Uploaded
modified:
insect_phenology_model.R
insect_phenology_model.xml
utils.R
b
diff -r 2194155309f4 -r 892cf703be62 insect_phenology_model.R
--- a/insect_phenology_model.R Fri Nov 09 13:05:31 2018 -0500
+++ b/insect_phenology_model.R Wed Nov 21 11:42:37 2018 -0500
[
b'@@ -250,63 +250,51 @@\n     return(mortality.probability)\n }\n \n-#mortality.egg = function(temperature, adj=0) {\n-#    # If no input from adjustment, default\n-#    # value is 0 (data from Nielsen, 2008).\n-#    T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35);\n-#    egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100);\n-#    # Calculates slopes and intercepts for lines.\n-#    slopes = NULL;\n-#    intercepts = NULL;\n-#    for (i in 1:length(T.mortality)) {\n-#        slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]);\n-#        intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i];\n-#    }\n-#    # Calculates mortality based on temperature.\n-#    mortality.probability = NULL;\n-#    for (j in 1:length(temperature)) {\n-#        mortality.probability[j] = if(temperature[j] <= T.mortality[2]) {\n-#                           temperature[j] * slopes[1] + intercepts[1];\n-#                       } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) {\n-#                           temperature[j] * slopes[2] + intercepts[2];\n-#                       } else if (temperature[j] > T.mortality[3] && temperature[j] <= T.mortality[4]) {\n-#                           temperature[j] * slopes[3] + intercepts[3];\n-#                       } else if (temperature[j] > T.mortality[4] && temperature[j] <= T.mortality[5]) {\n-#                           temperature[j] * slopes[4] + intercepts[4];\n-#                       } else if (temperature[j] > T.mortality[5] && temperature[j] <= T.mortality[6]) {\n-#                           temperature[j] * slopes[5] + intercepts[5];\n-#                       } else if (temperature[j] > T.mortality[6] && temperature[j] <= T.mortality[7]) {\n-#                           temperature[j] * slopes[6] + intercepts[6];\n-#                       } else if (temperature[j] > T.mortality[7]) {\n-#                           temperature[j] * slopes[7] + intercepts[7];\n-#                       }\n-#        # If mortality > 100, make it equal to 100.\n-#        mortality.probability[mortality.probability>100] = 100;\n-#        # If mortality <0, make equal to 0.\n-#        mortality.probability[mortality.probability<0] = 0;\n-#    }\n-#    # Make mortality adjustments based on adj parameter.\n-#    mortality.probability = (100 - mortality.probability) * adj + mortality.probability;\n-#    # if mortality > 100, make it equal to 100.\n-#    mortality.probability[mortality.probability>100] = 100;\n-#    # If mortality <0, make equal to 0.\n-#    mortality.probability[mortality.probability<0] = 0;\n-#    # Change percent to proportion.\n-#    mortality.probability = mortality.probability / 100;\n-#    return(mortality.probability)\n-#}\n-\n-mortality.egg = function(temperature) {\n-    if (temperature < 12.7) {\n-        mortality.probability = 0.8;\n+mortality.egg = function(temperature, adj=0) {\n+    # If no input from adjustment, default\n+    # value is 0 (data from Nielsen, 2008).\n+    T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35);\n+    egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100);\n+    # Calculates slopes and intercepts for lines.\n+    slopes = NULL;\n+    intercepts = NULL;\n+    for (i in 1:length(T.mortality)) {\n+        slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]);\n+        intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i];\n     }\n-    else {\n-        mortality.probability = 0.8 - temperature / 40.0;\n-        if (mortality.probability < 0) {\n-            mortality.probability = 0.01;\n-        }\n+    # Calculates mortality based on temperature.\n+    mortality.probability = NULL;\n+    for (j in 1:length(temperature)) {\n+        mortality.probability[j] = if(temperature[j] <= T.mortality[2]) {\n+                           temperature[j] * slopes[1] + intercepts[1];\n+                       } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) {\n+                           temperature[j] * slopes[2] + intercepts[2];'..b'                     if (vector.individual[3] >= (200+opt$adult_accumulation)) {\n+                            # From old to adult, degree_days requirement met.\n+                            current.gen = vector.individual[1];\n+                            if (vector.individual[5] == 0) {\n+                                # Previttelogenic.\n+                                vector.individual = c(current.gen, 3, 0, 0, 0);\n+                            }\n+                            else {\n+                                # Diapausing.\n+                                vector.individual = c(current.gen, 5, 0, 0, 1);\n+                            }\n+                        }\n+                        else {\n+                            # Add 1 day in current stage.\n+                            vector.individual[4] = vector.individual[4] + 1;\n+                        }\n+                        vector.matrix[i,] = vector.individual;\n                     }\n-                    vector.matrix[i,] = vector.individual;\n-                }\n-                # Growing of diapausing adult (unimportant, but still necessary).\n-                if (vector.individual[2] == 5) {\n-                    vector.individual[3] = vector.individual[3] + averages.temp;\n-                    vector.individual[4] = vector.individual[4] + 1;\n-                    vector.matrix[i,] = vector.individual;\n-                }\n-            } # Else if it is still alive.\n-        } # End of the individual bug loop.\n+                    # Growing of diapausing adult (unimportant, but still necessary).\n+                    if (vector.individual[2] == 5) {\n+                        vector.individual[3] = vector.individual[3] + averages.temp;\n+                        vector.individual[4] = vector.individual[4] + 1;\n+                        vector.matrix[i,] = vector.individual;\n+                    }\n+                } # Else if it is still alive.\n+            } # End of the individual bug loop.\n \n-        # Number of deaths.\n-        num_insects.death = length(death.vector);\n-        if (num_insects.death > 0) {\n-            # Remove record of dead.\n-            vector.matrix = vector.matrix[-death.vector,];\n+            # Number of deaths.\n+            num_insects.death = length(death.vector);\n+            if (num_insects.death > 0) {\n+                # Remove record of dead.\n+                vector.matrix = vector.matrix[-death.vector,];\n+            }\n+            # Number of births.\n+            num_insects.newborn = length(birth.vector[,1]);\n+            vector.matrix = rbind(vector.matrix, birth.vector);\n+            # Update population size for the next day.\n+            num_insects = num_insects - num_insects.death + num_insects.newborn;\n+        } else {\n+            if (is.null(doy_zero_insects)) {\n+                # Only set the doy for zero insects if\n+                # it has not yet been set.\n+                doy_zero_insects = doy;\n+            }\n         }\n-        # Number of births.\n-        num_insects.newborn = length(birth.vector[,1]);\n-        vector.matrix = rbind(vector.matrix, birth.vector);\n-        # Update population size for the next day.\n-        num_insects = num_insects - num_insects.death + num_insects.newborn;\n \n         # Aggregate results by day.  Due to multiple transpose calls\n         # on vector.matrix above, the columns of vector.matrix\n@@ -1547,7 +1539,12 @@\n     write.csv(temperature_data_frame_F2, file=file_path, row.names=F);\n }\n \n+# Get the ticks date labels for plots.\n+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm=prepend_end_doy_norm, append_start_doy_norm=append_start_doy_norm, date_interval=FALSE, doy_zero_insects=doy_zero_insects);\n+ticks = c(unlist(ticks_and_labels[1]));\n+date_labels = c(unlist(ticks_and_labels[2]));\n total_days_vector = c(1:dim(temperature_data_frame)[1]);\n+\n if (plot_generations_separately) {\n     for (life_stage in life_stages) {\n         if (life_stage == "Egg") {\n'
b
diff -r 2194155309f4 -r 892cf703be62 insect_phenology_model.xml
--- a/insect_phenology_model.xml Fri Nov 09 13:05:31 2018 -0500
+++ b/insect_phenology_model.xml Wed Nov 21 11:42:37 2018 -0500
b
@@ -82,7 +82,7 @@
             <option value="BMSB" selected="True">Brown Marmorated Stink Bug</option>
         </param>
         <param name="replications" type="integer" value="10" min="2" label="Number of replications"/>
-        <param name="insects_per_replication" type="integer" value="1000" min="1" label="Number of insects with which to start each replication"/>
+        <param name="insects_per_replication" type="integer" value="1000" min="100" label="Number of insects with which to start each replication"/>
         <param name="photoperiod" type="float" value="13.5" min="0" label="Critical photoperiod for diapause induction/termination"/>
         <param name="egg_mortality" type="float" value="1" min="0" max="1" label="Adjustment rate for egg mortality" help="Floating point value between 0 and 1"/>
         <param name="nymph_mortality" type="float" value="1" min="0" label="Adjustment rate for nymph mortality"/>
b
diff -r 2194155309f4 -r 892cf703be62 utils.R
--- a/utils.R Fri Nov 09 13:05:31 2018 -0500
+++ b/utils.R Wed Nov 21 11:42:37 2018 -0500
[
@@ -96,7 +96,7 @@
     }
 }
 
-get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm=0, append_start_doy_norm=0, date_interval=FALSE) {
+get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm=0, append_start_doy_norm=0, date_interval=FALSE, doy_zero_insects=NULL) {
     # Generate a list of ticks and labels for plotting the x axis.
     if (prepend_end_doy_norm > 0) {
         prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
@@ -108,6 +108,11 @@
     } else {
         append_start_norm_row = 0;
     }
+    if (is.null(doy_zero_insects)) {
+        zero_insects_row = 0;
+    } else {
+        zero_insects_row = which(temperature_data_frame$DOY==doy_zero_insects);
+    }
     num_rows = dim(temperature_data_frame)[1];
     tick_labels = list();
     ticks = list();
@@ -130,11 +135,24 @@
         doy = as.integer(temperature_data_frame$DOY[i]);
         # We're plotting the entire year, so ticks will
         # occur on Sundays and the first of each month.
-        if (i == prepend_end_norm_row) {
+        if (i == zero_insects_row) {
+            # Add a tick for the day on which the number of insects
+            # per replication is 0.
+            label_str = "Number insects is 0";
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
+            ticks[tick_index] = i;
+            if (date_interval) {
+                # Append the day to label_str
+                tick_labels[tick_index] = paste(label_str, day, sep=" ");
+            } else {
+                tick_labels[tick_index] = label_str;
+            }
+            last_tick = i;
+        } else if (i == prepend_end_norm_row) {
             # Add a tick for the end of the 30 year normnals data
             # that was prepended to the year-to-date data.
             label_str = "End prepended 30 year normals";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
             ticks[tick_index] = i;
             if (date_interval) {
                 # Append the day to label_str
@@ -147,7 +165,7 @@
             # Add a tick for the start of the 30 year normnals data
             # that was appended to the year-to-date data.
             label_str = "Start appended 30 year normals";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
             ticks[tick_index] = i;
             if (!identical(current_month_label, month_label)) {
                 # Append the month to label_str.
@@ -163,7 +181,7 @@
         } else if (i==num_rows) {
             # Add a tick for the last day of the year.
             label_str = "";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
             ticks[tick_index] = i;
             if (!identical(current_month_label, month_label)) {
                 # Append the month to label_str.
@@ -178,7 +196,7 @@
         } else {
             if (!identical(current_month_label, month_label)) {
                 # Add a tick for the month.
-                tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+                tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
                 ticks[tick_index] = i;
                 if (date_interval) {
                     # Append the day to the month.