GUILabels.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/ui/GUILabels.h>
00012 #include <shogun/ui/SGInterface.h>
00013 
00014 #include <shogun/lib/config.h>
00015 #include <shogun/io/SGIO.h>
00016 #include <shogun/io/AsciiFile.h>
00017 #include <shogun/labels/Labels.h>
00018 #include <shogun/labels/BinaryLabels.h>
00019 #include <shogun/labels/MulticlassLabels.h>
00020 #include <shogun/labels/RegressionLabels.h>
00021 
00022 #include <string.h>
00023 
00024 using namespace shogun;
00025 
00026 CGUILabels::CGUILabels(CSGInterface* ui_)
00027 : CSGObject(), ui(ui_), train_labels(NULL), test_labels(NULL)
00028 {
00029 }
00030 
00031 CGUILabels::~CGUILabels()
00032 {
00033     SG_UNREF(train_labels);
00034     SG_UNREF(test_labels);
00035 }
00036 
00037 bool CGUILabels::load(char* filename, char* target)
00038 {
00039     CLabels* labels=NULL;
00040 
00041     if (strncmp(target, "TEST", 4)==0)
00042         labels=test_labels;
00043     else if (strncmp(target, "TRAIN", 5)==0)
00044         labels=train_labels;
00045     else
00046         SG_ERROR("Invalid target %s.\n", target);
00047 
00048     if (labels)
00049     {
00050         SG_UNREF(labels);
00051         CAsciiFile* file=new CAsciiFile(filename);
00052         labels=new CRegressionLabels(file);
00053         SGVector<float64_t> labs = ((CRegressionLabels*) labels)->get_labels();
00054         float64_t* lab=SGVector<float64_t>::clone_vector(labs.vector, labs.vlen);
00055         labels=infer_labels(lab, labs.vlen);
00056 
00057         if (labels)
00058         {
00059             if (strncmp(target, "TEST", 4)==0)
00060                 set_test_labels(labels);
00061             else
00062                 set_train_labels(labels);
00063 
00064             return true;
00065         }
00066         else
00067             SG_ERROR("Loading labels failed.\n");
00068 
00069         SG_UNREF(file);
00070     }
00071 
00072     return false;
00073 }
00074 
00075 bool CGUILabels::save(char* param)
00076 {
00077     bool result=false;
00078     return result;
00079 }
00080 
00081 CLabels* CGUILabels::infer_labels(float64_t* lab, int32_t len)
00082 {
00083     CLabels* labels=NULL;
00084 
00085     bool binary=true;
00086     bool multiclass=true;
00087     for (int32_t i=0; i<len; i++)
00088     {
00089         if (lab[i]!=-1 && lab[i]!=+1)
00090             binary=false;
00091 
00092         if (lab[i]<0 || lab[i]!=int(lab[i]))
00093             multiclass=false;
00094 
00095         if (binary == false && multiclass == false)
00096         {
00097             labels=new CRegressionLabels(SGVector<float64_t>(lab, len));
00098             break;
00099         }
00100     }
00101 
00102     if (multiclass)
00103         labels=new CMulticlassLabels(SGVector<float64_t>(lab, len));
00104     if (binary)
00105         labels=new CBinaryLabels(SGVector<float64_t>(lab, len));
00106 
00107     return labels;
00108 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation