Go to the documentation of this file.00001 
00002 #include <bprb/bprb_func_process.h>
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 #include <brdb/brdb_value.h>
00016 #include <bprb/bprb_parameters.h>
00017 
00018 #include <bxml/bsvg/bsvg_plot.h>
00019 #include <vul/vul_awk.h>
00020 #include <vul/vul_string.h>
00021 #include <bxml/bxml_write.h>
00022 
00023 #include <bbas_pro/bbas_1d_array_float.h>
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 bool bsvg_plot_roc_process_cons(bprb_func_process& pro)
00039 {
00040   
00041   bool ok=false;
00042   vcl_vector<vcl_string> input_types;
00043   input_types.push_back("vcl_string");  
00044   input_types.push_back("vcl_string");  
00045   ok = pro.set_input_types(input_types);
00046   if (!ok) return ok;
00047 
00048   
00049   vcl_vector<vcl_string> output_types;
00050   ok = pro.set_output_types(output_types);
00051   return ok;
00052 }
00053 
00054 
00055 bool bsvg_plot_roc_process(bprb_func_process& pro)
00056 {
00057   
00058   if (pro.n_inputs() < 2) {
00059     vcl_cerr << "bsvg_plot_roc_process - invalid inputs\n";
00060     return false;
00061   }
00062 
00063   
00064   unsigned i = 0;
00065   vcl_string roc_path = pro.get_input<vcl_string>(i++);
00066   vcl_string out_name = pro.get_input<vcl_string>(i++);
00067 
00068   bsvg_plot p(1200, 600);
00069   p.set_margin(40);
00070   p.set_font_size(30);
00071   p.add_axes(0, 1, 0, 1);
00072   p.add_title("ROC Plot");
00073   
00074   p.add_y_increments(0.1f);
00075 
00076   vcl_vector<float> xs, ys;
00077 
00078   vcl_ifstream ifs(roc_path.c_str());
00079   int line_cnt = 0;
00080   for (vul_awk awk(ifs); awk; ++awk) {
00081     if (!awk.NF())
00082       continue;
00083     vcl_string field0 = awk[0];
00084     if (!field0.size())
00085       continue;
00086     else if (field0[0] == '#')
00087       continue;
00088     line_cnt++;
00089     if (line_cnt == 2) {  
00090       for (int j = 0; j < awk.NF(); j++) {
00091         float fpr = (float)vul_string_atof(awk[j]);
00092         xs.push_back(fpr);
00093       }
00094     }
00095     else if (line_cnt == 3) {
00096       for (int j = 0; j < awk.NF(); j++) {
00097         float tpr = (float)vul_string_atof(awk[j]);
00098         ys.push_back(tpr);
00099       }
00100       break;
00101     }
00102   }
00103 
00104   xs.erase(xs.end()-1); 
00105   ys.erase(ys.end()-1);
00106   p.add_line(xs, ys, "red");
00107   bxml_write(out_name, p);
00108 
00109   return true;
00110 }
00111 
00112 
00113 
00114 bool bsvg_plot_roc_process2_cons(bprb_func_process& pro)
00115 {
00116   
00117   bool ok=false;
00118   vcl_vector<vcl_string> input_types;
00119   input_types.push_back("bbas_1d_array_float_sptr");  
00120   input_types.push_back("bbas_1d_array_float_sptr");  
00121   input_types.push_back("vcl_string");  
00122   ok = pro.set_input_types(input_types);
00123   if (!ok) return ok;
00124 
00125   
00126   vcl_vector<vcl_string> output_types;
00127   ok = pro.set_output_types(output_types);
00128   return ok;
00129 }
00130 
00131 
00132 bool bsvg_plot_roc_process2(bprb_func_process& pro)
00133 {
00134   
00135   if (pro.n_inputs() < 3) {
00136     vcl_cerr << "bsvg_plot_roc_process2 - invalid inputs\n";
00137     return false;
00138   }
00139 
00140   
00141   unsigned i = 0;
00142   bbas_1d_array_float_sptr tpr_vals = pro.get_input<bbas_1d_array_float_sptr>(i++);
00143   bbas_1d_array_float_sptr fpr_vals = pro.get_input<bbas_1d_array_float_sptr>(i++);
00144   vcl_string out_name = pro.get_input<vcl_string>(i++);
00145 
00146   bsvg_plot p(1200, 600);
00147   p.set_margin(40);
00148   p.set_font_size(30);
00149   p.add_axes(0, 1, 0, 1);
00150   p.add_title("ROC Plot");
00151   
00152   p.add_y_increments(0.1f);
00153 
00154   if (tpr_vals->data_array.size() != fpr_vals->data_array.size()) {
00155     vcl_cout << "In bsvg_plot_roc_process2_cons : inconsistent tpr and fpr array sizes!\n";
00156     return false;
00157   }
00158 
00159   vcl_vector<float> xs, ys;
00160   for (vbl_array_1d<float>::iterator iter = tpr_vals->data_array.begin(), iter2 = fpr_vals->data_array.begin(); 
00161        iter != tpr_vals->data_array.end(); iter++, iter2++) {
00162     ys.push_back(*iter);
00163     xs.push_back(*iter2);
00164     vcl_cout << "tp: " << *iter << " fp: " << *iter2 << vcl_endl;
00165   }
00166   vcl_cout << vcl_endl;
00167 
00168   
00169   
00170   p.add_line(xs, ys, "red");
00171   bxml_write(out_name, p);
00172 
00173   return true;
00174 }
00175 
00176 
00177 
00178 
00179 bool bsvg_plot_initialize_process_cons(bprb_func_process& pro)
00180 {
00181   
00182   bool ok=false;
00183   vcl_vector<vcl_string> input_types;
00184   input_types.push_back("vcl_string");  
00185   input_types.push_back("int");  
00186   input_types.push_back("int");  
00187   input_types.push_back("int");  
00188   input_types.push_back("int");  
00189   ok = pro.set_input_types(input_types);
00190   if (!ok) return ok;
00191 
00192   brdb_value_sptr idw = new brdb_value_t<int>(1200);
00193   pro.set_input(1, idw);
00194   brdb_value_sptr idh = new brdb_value_t<int>(600);
00195   pro.set_input(2, idh);
00196   brdb_value_sptr idm = new brdb_value_t<int>(40);
00197   pro.set_input(3, idm);
00198   brdb_value_sptr idfs = new brdb_value_t<int>(30);
00199   pro.set_input(4, idfs);
00200 
00201   
00202   vcl_vector<vcl_string> output_types;
00203   output_types.push_back("bxml_document_sptr");
00204   ok = pro.set_output_types(output_types);
00205   return ok;
00206 }
00207 
00208 bool bsvg_plot_initialize_process(bprb_func_process& pro)
00209 {
00210   
00211   if (pro.n_inputs() < 1) {
00212     vcl_cerr << "bsvg_roc_plot_initialize_process - invalid inputs\n";
00213     return false;
00214   }
00215 
00216   
00217   unsigned i = 0;
00218   vcl_string title = pro.get_input<vcl_string>(i++);
00219   int w = pro.get_input<int>(i++);
00220   int h = pro.get_input<int>(i++);
00221   int m = pro.get_input<int>(i++);
00222   int fs = pro.get_input<int>(i++);
00223 
00224   bsvg_plot* p = new bsvg_plot((float)w, (float)h);
00225   p->set_margin((float)m);
00226   p->set_font_size(fs);
00227   p->add_axes(0, 1, 0, 1);
00228   p->add_y_increments(0.1f);
00229   p->add_title(title);
00230   bxml_document_sptr pd = p;
00231   pro.set_output_val<bxml_document_sptr>(0, pd);
00232   return true;
00233 }
00234 
00235 
00236 bool bsvg_roc_plot_add_process_cons(bprb_func_process& pro)
00237 {
00238   
00239   bool ok=false;
00240   vcl_vector<vcl_string> input_types;
00241   input_types.push_back("bxml_document_sptr");
00242   input_types.push_back("bbas_1d_array_float_sptr");  
00243   input_types.push_back("bbas_1d_array_float_sptr");  
00244   input_types.push_back("vcl_string");  
00245   ok = pro.set_input_types(input_types);
00246   if (!ok) return ok;
00247 
00248   
00249   vcl_vector<vcl_string> output_types;
00250   ok = pro.set_output_types(output_types);
00251   return ok;
00252 }
00253 
00254 bool bsvg_roc_plot_add_process(bprb_func_process& pro)
00255 {
00256   
00257   if (pro.n_inputs() < 4) {
00258     vcl_cerr << "bsvg_plot_roc_process2 - invalid inputs\n";
00259     return false;
00260   }
00261 
00262   
00263   unsigned i = 0;
00264   bxml_document_sptr doc = pro.get_input<bxml_document_sptr>(i++);
00265   bbas_1d_array_float_sptr tpr_vals = pro.get_input<bbas_1d_array_float_sptr>(i++);
00266   bbas_1d_array_float_sptr fpr_vals = pro.get_input<bbas_1d_array_float_sptr>(i++);
00267   vcl_string color = pro.get_input<vcl_string>(i++);
00268 
00269   if (tpr_vals->data_array.size() != fpr_vals->data_array.size()) {
00270     vcl_cout << "In bsvg_plot_roc_process2_cons : inconsistent tpr and fpr array sizes!\n";
00271     return false;
00272   }
00273   vcl_vector<float> xs, ys;
00274   for (vbl_array_1d<float>::iterator iter = tpr_vals->data_array.begin(), iter2 = fpr_vals->data_array.begin(); 
00275        iter != tpr_vals->data_array.end(); iter++, iter2++) {
00276     xs.push_back(*iter2);
00277     ys.push_back(*iter);
00278   }
00279   bsvg_plot* p = dynamic_cast<bsvg_plot*>(doc.ptr());
00280   p->add_line(xs, ys, color);
00281   return true;
00282 }
00283