Project

General

Profile

« Previous | Next » 

Revision ea73ac07

Added by Benoit Parmentier almost 9 years ago

assessment part3, tracking figures and listing in table

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part3.R
173 173
  year_predicted <- list_param_run_assessment_plotting$year_predicted
174 174
 
175 175
  NA_value <- NA_flag_val 
176

  
176
  metric_name <- "rmse" #to be added to the code later...
177
  
177 178
  ##################### START SCRIPT #################
178 179
  
179 180
  ####### PART 1: Read in data ########
......
187 188

  
188 189
  setwd(out_dir)
189 190
  
190
  list_outfiles <- vector("list", length=29) #collect names of output files, this should be dynamic?
191
  list_outfiles_names <- vector("list", length=29) #collect names of output files
191
  list_outfiles <- vector("list", length=31) #collect names of output files, this should be dynamic?
192
  list_outfiles_names <- vector("list", length=31) #collect names of output files
192 193
  counter_fig <- 0 #index of figure to collect outputs
193 194
  
194 195
  #i <- year_predicted
......
447 448
    list_outfiles[[counter_fig+2]] <- paste("Figure3b_boxplot_overall_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
448 449
  }
449 450
  counter_fig <- counter_fig + length(model_name)
450
  
451
  r6 <-c("figure_3a","Boxplot overall accuracy with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[6]])  
452
  r7 <-c("figure_3b","Boxplot overall accuracy with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[7]])  
453
  r8 <-c("figure_3a","Boxplot overall accuracy with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[8]])
454
  r9 <-c("figure_3b","Boxplot overall accuracy with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
455

  
451 456
  ################ 
452 457
  ### Figure 4: plot predicted tiff for specific date per model
453 458
  
......
516 521
  }
517 522
  
518 523
  counter_fig <- counter_fig + length(model_name)
524
  
525
  r10 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
526
  r11 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
519 527

  
520 528
  ######################
521 529
  ### Figure 6: plot map of average RMSE per tile at centroids
......
523 531
  ### Without 
524 532
  
525 533
  #list_df_ac_mod <- vector("list",length=length(lf_pred_list))
526
  list_df_ac_mod <- vector("list",length=2)
534
  list_df_ac_mod <- vector("list",length=length(model_name))
527 535
  
528 536
  for (i in 1:length(model_name)){
529 537
    
......
557 565
    list_outfiles[[counter_fig+i]] <- fig_filename
558 566
  }
559 567
  counter_fig <- counter_fig+length(model_name)
568

  
569
  r12 <-c("figure_6","Average accuracy metrics map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
570
  r13 <-c("figure_6","Average accuracy metrics map at centroids","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
560 571
  
561 572
  
562 573
  ######################
......
587 598
   #Error in grid.Call.graphics(L_setviewport, pvp, TRUE) : 
588 599
  #non-finite location and/or size for viewport
589 600

  
590
  j<-1 #for model name 1
601
  j<-1 #for model name 1,mod1
591 602
  for(i in 1:length(threshold_missing_day)){
592 603
    
593 604
    #summary_metrics_v$n_missing <- summary_metrics_v$n == 365
......
604 615
      res_pix <- 960
605 616
      col_mfrow <- 1
606 617
      row_mfrow <- 1
618
      #only mod1 right now
607 619
      png(filename=paste("Figure7a_ac_metrics_map_centroids_tile_",model_name[j],"_","missing_day_",threshold_missing_day[i],
608 620
                       "_",out_suffix,".png",sep=""),
609 621
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
......
623 635
    list_outfiles[[counter_fig+i]] <- fig_filename
624 636
  }
625 637
  counter_fig <- counter_fig+length(threshold_missing_day) #currently 4 days...
626
  
638

  
639
  r14 <-c("figure_7","Number of missing days threshold1 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
640
  r15 <-c("figure_7","Number of missing days threshold2 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
641
  r16 <-c("figure_7","Number of missing days threshold3 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
642
  r17 <-c("figure_7","Number of missing days threshold4 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
643

  
627 644
  ### Potential
628 645
  png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
629 646
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
......
634 651
  
635 652
  list_outfiles[[counter_fig+1]] <- paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep="")
636 653
  counter_fig <- counter_fig + 1
654
  r18 <-c("figure_7b","Number of daily predictions per_models","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
637 655
  
638 656
  table(tb$pred_mod)
639 657
  table(tb$index_d)
......
648 666
  as.character(unique(test$tile_id)) #141 tiles
649 667
  
650 668
  dim(subset(test,test$predicted==365 & test$pred_mod=="mod1"))
651
  histogram(subset(test, test$pred_mod=="mod1")$predicted)
669
  #histogram(subset(test, test$pred_mod=="mod1")$predicted)
652 670
  unique(subset(test, test$pred_mod=="mod1")$predicted)
653 671
  table((subset(test, test$pred_mod=="mod1")$predicted))
654 672
  
......
661 679
  
662 680
  list_outfiles[[counter_fig+1]] <- paste("Figure7c_histogram_number_daily_predictions_per_models","_",out_suffix,".png",sep="")
663 681
  counter_fig <- counter_fig + 1
682
  r19 <-c("figure_7c","Histogram number daily predictions per models","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
664 683

  
684
  
665 685
  #table(tb)
666 686
  ## Figure 7b
667 687
  #png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
......
680 700
  ##### Figure 8: Breaking down accuracy by regions!! #####
681 701
  
682 702
  #summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
703
  
704
  ##################
683 705
  ##First plot with all models together
684 706
  
685 707
  ## Figure 8a
......
687 709
  col_mfrow <- 1
688 710
  row_mfrow <- 1
689 711
  
690
  png(filename=paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_","_",out_suffix,".png",sep=""),
712
  fig_filename <- paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_",out_suffix,".png",sep="")
713
  png(filename=fig_filename,
691 714
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
692 715
  
693 716
  p<- bwplot(rmse~pred_mod | reg, data=tb,
......
695 718
  print(p)
696 719
  dev.off()
697 720
  
698
  list_outfiles[[counter_fig+1]] <- paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep="")
721
  list_outfiles[[counter_fig+1]] <- fig_filename
699 722
  counter_fig <- counter_fig + 1
700 723
  
701 724
  ## Figure 8b
702
  png(filename=paste("Figure8b_boxplot_overall_separated_by_region_scaling_","_",out_suffix,".png",sep=""),
725
  fig_filename <- paste("Figure8b_boxplot_overall_separated_by_region_scaling_",out_suffix,".png",sep="")
726
  png(filename=fig_filename,
703 727
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
704 728
  
705 729
  #boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
......
709 733
  print(p)
710 734
  dev.off()
711 735
  
712
  list_outfiles[[counter_fig+1]] <- paste("Figure8b_boxplot_overall_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
736
  list_outfiles[[counter_fig+1]] <- fig_filename
713 737
  counter_fig <- counter_fig + 1
714 738
  
739
  
740
  r20 <-c("figure 8a","Boxplot overall accuracy by model separated by region with outliers",NA,metric_name,region_name,year_predicted,list_outfiles[[20]])  
741
  r21 <-c("figure 8b","Boxplot overall accuracy by model separated by region with scaling",NA,metric_name,region_name,year_predicted,list_outfiles[[21]])  
742

  
743
  #######
715 744
  ##Second, plot for each model separately
716 745
  
717 746
  for(i in 1:length(model_name)){
......
723 752
    col_mfrow <- 1
724 753
    row_mfrow <- 1
725 754
  
726
    fig_filename <- paste("Figure8c_boxplot_overall_separated_by_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep="")
755
    fig_filename <- paste("Figure8c_boxplot_overall_accuracy_separated_by_region_with_outliers_",model_name[i],"_",out_suffix,".png",sep="")
727 756
    png(filename=fig_filename,
728 757
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
729 758
  
......
736 765
    counter_fig <- counter_fig + 1
737 766
  
738 767
    ## Figure 8d
739
    fig_filename <- paste("Figure8d_boxplot_overall_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
768
    fig_filename <- paste("Figure8d_boxplot_overall_accuracy_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
740 769
    png(filename=fig_filename,
741 770
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
742 771
  
......
751 780
    counter_fig <- counter_fig + 1
752 781

  
753 782
  }
754
 
783
  
784
  r22 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[22]])  
785
  r23 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[23]])  
786
  r24 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[24]])  
787
  r25 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[25]])  
755 788

  
756 789
  #####################################################
757 790
  #### Figure 9: plotting boxplot by year and regions ###########
......
761 794
  col_mfrow <- 1
762 795
  row_mfrow <- 1
763 796

  
764
  png(filename=paste("Figure9a_boxplot_overall_separated_by_region_year_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
797
  png(filename=paste("Figure9a_boxplot_overall_separated_by_year_and_model_with_oultiers_",out_suffix,".png",sep=""),
765 798
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
766 799
  #This will need to be changed, the layout is difficult at this stage 
767 800
  #p<- bwplot(rmse~pred_mod + reg + year_predicted, data=tb,
768 801
  #           main="RMSE per model and region over all tiles")
769
  p<- bwplot(rmse~pred_mod | reg + year_predicted, data=tb,
802
  p<- bwplot(rmse~pred_mod | year_predicted, data=tb,
770 803
             main="RMSE per model and region over all tiles")
771 804
  print(p)
772 805
  dev.off()
773 806
  
774 807
  ## Figure 9b
775
  png(filename=paste("Figure8b_boxplot_overall_separated_by_region_year_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
808
  png(filename=paste("Figure9b_boxplot_overall_separated_by_year_and_model_scaling_",out_suffix,".png",sep=""),
776 809
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
777 810
  
778 811
  #boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
779 812
  #title("RMSE per model over all tiles")
780
  p<- bwplot(rmse~pred_mod | reg + year_predicted, data=tb,ylim=c(0,5),
813
  p<- bwplot(rmse~pred_mod | year_predicted, data=tb,ylim=c(0,5),
781 814
             main="RMSE per model and region over all tiles")
782 815
  print(p)
783 816
  dev.off()
784 817

  
785
  list_outfiles[[counter_fig+1]] <- paste("Figure9a_boxplot_overall_separated_by_region_year_with_oultiers_",model_name[i],"_",out_suffix,".png",sep="")
786
  counter_fig <- counter_fig + 1
818
  for(i in 1:length(model_name)){
819
    
820
    tb_subset <- subset(tb,pred_mod==model_name[i])#mod1 is i=1, mod_kr is last
821
    ## Figure 9c
822
    fig_filename <- paste("Figure9c_boxplot_overall_accuracy_separated_by_year_with_outliers_",model_name[i],"_",out_suffix,".png",sep="")
823
    png(filename=fig_filename,
824
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
825
  
826
    boxplot(rmse~year_predicted,data=tb_subset,ylab=metric_name,xlab="year predicted")#,names=tb$pred_mod)
827
    title(paste("Overall accuracy for ", model_name[i], " by year for ",region_name,sep=""))
828
    
829
    #p<- bwplot(rmse~year_predicted | reg , data=tb_subset,ylim=c(0,5),
830
             #main="RMSE per model and region over all tiles")
831
    #print(p)
832
    dev.off()
833
    
834
    list_outfiles[[counter_fig+1]] <- fig_filename
835
    counter_fig <- counter_fig + 1
836

  
837
    fig_filename <- paste("Figure9d_boxplot_overall_separated_by_year_scaling_",model_name[i],"_",out_suffix,".png",sep="")
838
    png(filename=fig_filename,
839
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
840
  
841
    boxplot(rmse~year_predicted,data=tb_subset,ylim=c(0,5),outline=FALSE,ylab=metric_name,xlab="year predicted")#,names=tb$pred_mod)
842
    title(paste("Overall accuracy for ", model_name[i], " by year for ",region_name,sep=""))
843
    #p<- bwplot(rmse~year_predicted | reg , data=tb_subset,ylim=c(0,5),
844
             #main="RMSE per model and region over all tiles")
845
    #print(p)
846
    dev.off()
847

  
848
    list_outfiles[[counter_fig+1]] <- fig_filename
849
    counter_fig <- counter_fig + 1
850
  }
851
  
852
  r26 <-c("figure 9a","Boxplot overall accuracy separated_by year and model with oultiers",NA,metric_name,region_name,year_predicted,list_outfiles[[22]])  
853
  r27 <-c("figure 9b","Boxplot overall accuracy separated_by year and model with scaling",NA,metric_name,region_name,year_predicted,list_outfiles[[23]])  
854
  r28 <-c("figure 9c","Boxplot overall accuracy separated by year with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[24]])  
855
  r29 <-c("figure 9d","Boxplot overall accuracy separated by year with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[25]])  
856
  r30 <-c("figure 9c","Boxplot overall accuracy separated by year with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[24]])  
857
  r31 <-c("figure 9d","Boxplot overall accuracy separated by year with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[25]])  
787 858

  
788 859
  ##############################################################
789 860
  ############## Prepare object to return
790 861
  ############## Collect information from assessment ##########
791 862
  
792 863
  # This is hard coded and can be improved later on for flexibility. It works for now...                                                                 
793
  comments_str <- c("tile processed for the region",
794
  "boxplot with outliers",                                                          
795
  "boxplot with outliers",
796
  "boxplot scaling by tiles",
797
  "boxplot scaling by tiles",
798
  "boxplot overall region with outliers",
799
  "boxplot overall region with scaling",
800
  "boxplot overall region with outliers",
801
  "boxplot overall region with scaling",
802
  "Barplot of accuracy metrics ranked by tile",
803
  "Barplot of accuracy metrics ranked by tile",
804
  "Average accuracy metrics map at centroids",
805
  "Average accuracy metrics map at centroids",
806
  "Number of missing day threshold1 map centroids",
807
  "Number of missing day threshold2 map centroids",
808
  "Number of missing day threshold3 map centroids",
809
  "Number of missing day threshold4 map centroids",
810
  "number_daily_predictions_per_model",
811
  "histogram number_daily_predictions_per_models",
812
  "boxplot overall separated by region with_outliers",
813
  "boxplot overall separated by region with_scaling",
814
  "boxplot overall separated by region with_outliers",
815
  "boxplot overall separated by region with_scaling")
816

  
817
  figure_no <- c("figure_1","figure_2a","figure_2a","figure_2b","figure_2b","figure_3a","figure_3a","figure_3b","figure_3b",
818
                 "figure_5", "figure_5","figure_6","figure_6","Figure_7a","Figure_7a","Figure_7a","Figure_7a","Figure_7b",
819
                 "Figure_7c","Figure 8a","Figure 8a","Figure 8b","Figure 8b")
820

  
821
  col_model_name <- c(NA,"mod1","mod_kr","mod1","mod_kr","mod1","mod_kr","mod1","mod_kr","mod1","mod_kr",
822
                  "mod1","mod_kr","mod1","mod1","mod1","mod1","mod1","mod1",NA,NA,"mod1","mod1")
823
  col_reg <- rep(region_name,length(list_outfiles))
824
  col_year_predicted <- rep(year_predicted,length(list_outfiles))
825
  
826 864
  #This data.frame contains all the files from the assessment
827
  df_assessment_figures_files <- data.frame(figure_no=figure_no,
828
                                            comment = comments_str,
829
                                            model_name=col_model_name,
830
                                            reg=col_reg,
831
                                            year_predicted=col_year_predicted,
832
                                            filename=unlist(list_outfiles))
865

  
866
  #Should have this at the location of the figures!!! will be done later?    
867
  #r1 <-c("figure_1","Tiles processed for the region",NA,NA,region_name,year_predicted,list_outfiles[[1]])
868
  #r2 <-c("figure_2a","Boxplot of accuracy with outliers by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[2]]) 
869
  #r3 <-c("figure_2a","boxplot of accuracy with outliers by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[3]])
870
  #r4 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[4]])  
871
  #r5 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[5]])  
872
  #r6 <-c("figure_3a","Boxplot overall accuracy with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[6]])  
873
  #r7 <-c("figure_3b","Boxplot overall accuracy with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[7]])  
874
  #r8 <-c("figure_3a","Boxplot overall accuracy with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[8]])
875
  #r9 <-c("figure_3b","Boxplot overall accuracy with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
876
  #r10 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[10]])
877
  #r11 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[11]])  
878
  #r12 <-c("figure_6","Average accuracy metrics map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[12]])
879
  #r13 <-c("figure_6","Average accuracy metrics map at centroids","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[13]])  
880
  #r14 <-c("figure_7","Number of missing days threshold1 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[14]])
881
  #r15 <-c("figure_7","Number of missing days threshold2 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[15]])  
882
  #r16 <-c("figure_7","Number of missing days threshold3 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[16]])
883
  #r17 <-c("figure_7","Number of missing days threshold4 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[17]])  
884
  #r18 <-c("figure_7b","Number of daily predictions per_models","mod1",metric_name,region_name,year_predicted,list_outfiles[[18]])  
885
  #r19 <-c("figure_7c","Histogram number daily predictions per models","mod1",metric_name,region_name,year_predicted,list_outfiles[[19]])  
886
  #r20 <-c("figure 8a","Boxplot overall accuracy by model separated by region with outliers",NA,metric_name,region_name,year_predicted,list_outfiles[[20]])  
887
  #r21 <-c("figure 8b","Boxplot overall accuracy by model separated by region with scaling",NA,metric_name,region_name,year_predicted,list_outfiles[[21]])  
888
  #r22 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[22]])  
889
  #r23 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[23]])  
890
  #r24 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[24]])  
891
  #r25 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[25]])  
892

  
893
  #Assemble all the figures description and information in a data.frame for later use
894
  list_rows <-list(r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,
895
                   r25,r26,r27,r28,r29,r30,r31)
896
  df_assessment_figures_files <- as.data.frame(do.call(rbind,list_rows))
897
  names(df_assessment_figures_files) <- c("figure_no","comment","model_name","reg","metric_name","year_predicted","filename")
833 898
  
834 899
  ###Prepare files for copying back?
835 900
  df_assessment_figures_files_names <- file.path(out_dir,paste("df_assessment_figures_files_",region_name,"_",year_predicted,"_",out_suffix,".txt",sep=""))

Also available in: Unified diff