contrib/brl/bbas/bxml/bsvg/pro/processes/bsvg_plot_roc_process.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bxml/bsvg/pro/processes/bsvg_plot_roc_process.cxx
00002 #include <bprb/bprb_func_process.h>
00003 //:
00004 // \file
00005 // \brief Processes for plotting
00006 //
00007 // \author Ozge Can Ozcanli
00008 // \date June 19, 2009
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   none yet
00013 // \endverbatim
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 //: Plot ROC process:
00026 //    input 0: the path to a text file which has the following format:
00027 //  # any number of commented lines which start with character: '#'
00028 //  #line 1: threshold values
00029 //  #line 2: FPR values for thresholds
00030 //  #line 3: TPR values (The number of values in all the lines have to be equal)
00031 //  0  0.0001      0.0002     0.0003     0.0004     . . .
00032 //  0  0.00728813  0.0078134  0.0078134  0.0078134  . . .
00033 //  0  0.416157    0.519584   0.519584   0.519584   . . .
00034 //    input 1: the path to the output SVG plot ( x.svg )
00035 //
00036 
00037 //: Constructor
00038 bool bsvg_plot_roc_process_cons(bprb_func_process& pro)
00039 {
00040   //inputs
00041   bool ok=false;
00042   vcl_vector<vcl_string> input_types;
00043   input_types.push_back("vcl_string");  // file with 3 lines: threshold values, TPRs and FPRs
00044   input_types.push_back("vcl_string");  // name of the output svg file
00045   ok = pro.set_input_types(input_types);
00046   if (!ok) return ok;
00047 
00048   //output
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   // Sanity check
00058   if (pro.n_inputs() < 2) {
00059     vcl_cerr << "bsvg_plot_roc_process - invalid inputs\n";
00060     return false;
00061   }
00062 
00063   // get input
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   //p.add_x_increments(0.1f);
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) {  // second line is FPR values, read them from the fields
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); // erase the last element, which is (1,1) pair as a convention
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 //: Constructor
00114 bool bsvg_plot_roc_process2_cons(bprb_func_process& pro)
00115 {
00116   //inputs
00117   bool ok=false;
00118   vcl_vector<vcl_string> input_types;
00119   input_types.push_back("bbas_1d_array_float_sptr");  // vector of TPR values
00120   input_types.push_back("bbas_1d_array_float_sptr");  // vector of FPR values
00121   input_types.push_back("vcl_string");  // name of the output svg file
00122   ok = pro.set_input_types(input_types);
00123   if (!ok) return ok;
00124 
00125   //output
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   // Sanity check
00135   if (pro.n_inputs() < 3) {
00136     vcl_cerr << "bsvg_plot_roc_process2 - invalid inputs\n";
00137     return false;
00138   }
00139 
00140   // get input
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   //p.add_x_increments(0.1f);
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   //xs.erase(xs.end()-1); // erase the last element, which is (1,1) pair as a convention
00169   //ys.erase(ys.end()-1);
00170   p.add_line(xs, ys, "red");
00171   bxml_write(out_name, p);
00172 
00173   return true;
00174 }
00175 
00176 
00177 //: Constructor
00178 //  initialize a bar plot with no bars, new bars will be added by the add_bar process
00179 bool bsvg_plot_initialize_process_cons(bprb_func_process& pro)
00180 {
00181   //inputs
00182   bool ok=false;
00183   vcl_vector<vcl_string> input_types;
00184   input_types.push_back("vcl_string");  // title of the plot
00185   input_types.push_back("int");  // width
00186   input_types.push_back("int");  // height
00187   input_types.push_back("int");  // margin
00188   input_types.push_back("int");  // fs
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   //output
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   // Sanity check
00211   if (pro.n_inputs() < 1) {
00212     vcl_cerr << "bsvg_roc_plot_initialize_process - invalid inputs\n";
00213     return false;
00214   }
00215 
00216   // get input
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 //: Constructor
00236 bool bsvg_roc_plot_add_process_cons(bprb_func_process& pro)
00237 {
00238   //inputs
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");  // vector of TPR values
00243   input_types.push_back("bbas_1d_array_float_sptr");  // vector of FPR values
00244   input_types.push_back("vcl_string");  // color of the line
00245   ok = pro.set_input_types(input_types);
00246   if (!ok) return ok;
00247 
00248   //output
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   // Sanity check
00257   if (pro.n_inputs() < 4) {
00258     vcl_cerr << "bsvg_plot_roc_process2 - invalid inputs\n";
00259     return false;
00260   }
00261 
00262   // get input
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