PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ExtractOptionCommand.cc 00004 // 00005 // Copyright (C) 2008 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 // Authors: Pascal Vincent 00036 00040 #include "ExtractOptionCommand.h" 00041 #include <plearn/io/openFile.h> 00042 #include <plearn/io/openString.h> 00043 #include <plearn/vmat/FileVMatrix.h> 00044 #include <plearn/math/TMat_maths.h> 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00050 PLearnCommandRegistry ExtractOptionCommand::reg_(new ExtractOptionCommand); 00051 00052 ExtractOptionCommand::ExtractOptionCommand() 00053 : PLearnCommand( 00054 "extract_option", 00055 "Extracts an option from a saved plearn object (.psave) and saves it to its own file.", 00056 " extract_option <objfile.psave> <optionname> <optionfile.psave>\n" 00057 "OR extract_option <objfile.psave> <optionname> <optionfile.pmat> <transpose> \n" 00058 "The first form will output the option serialized in plearn_ascii format\n" 00059 "The second form is available only for Mat or Vec options, and will save it as a .pmat file.\n" 00060 "For Mat, if transpose is 0 then the matrix won't be transposed. If it's 1 it will be transposed.\n" 00061 "For Vec, if transpose is 0 then it will be saved as a row matrix. If it's 1, i will be saved as a column matrix.\n" 00062 ) 00063 {} 00064 00066 void ExtractOptionCommand::run(const vector<string>& args) 00067 { 00068 if(args.size()==3) 00069 { 00070 string objfile = args[0]; 00071 string optionname = args[1]; 00072 string optionfile = args[2]; 00073 PP<Object> obj = loadObject(objfile); 00074 PStream out = openFile(optionfile, PStream::plearn_ascii, "w"); 00075 obj->writeOptionVal(out, optionname); 00076 out = 0; 00077 perr << "Option " << optionname << " has been written to file " << optionfile << endl; 00078 } 00079 else if(args.size()==4) 00080 { 00081 string objfile = args[0]; 00082 string optionname = args[1]; 00083 string optionfile = args[2]; 00084 int do_transpose = toint(args[3]); 00085 PP<Object> obj = loadObject(objfile); 00086 string optionval = obj->getOption(optionname); 00087 bool ismat = false; 00088 Mat m; 00089 try 00090 { 00091 PStream in = openString(optionval, PStream::plearn_ascii); 00092 in >> m; 00093 perr << "Extracted a " << m.length() << " x " << m.width() << " matrix" << endl; 00094 ismat = true; 00095 } 00096 catch(const PLearnError& e) 00097 { ismat = false; } 00098 00099 if(!ismat) 00100 { 00101 Vec v; 00102 PStream in = openString(optionval, PStream::plearn_ascii); 00103 in >> v; 00104 perr << "Extracted a vector of length " << v.length() << endl; 00105 m = v.toMat(1,v.length()); 00106 } 00107 00108 if(do_transpose==1) 00109 m = transpose(m); 00110 FileVMatrix vmat(optionfile, m.length(), m.width()); 00111 vmat.putMat(0, 0, m); 00112 vmat.flush(); 00113 perr << "Option " << optionname << " has been written to file " << optionfile << endl; 00114 } 00115 else 00116 PLERROR("Wrong number of argumens, please consult help"); 00117 } 00118 00119 } // end of namespace PLearn 00120 00121 00122 /* 00123 Local Variables: 00124 mode:c++ 00125 c-basic-offset:4 00126 c-file-style:"stroustrup" 00127 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00128 indent-tabs-mode:nil 00129 fill-column:79 00130 End: 00131 */ 00132 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :