SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DenseLabels.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-2009 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Written (W) 2011 Heiko Strathmann
00010  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00011  */
00012 
00013 #include <shogun/labels/Labels.h>
00014 #include <shogun/labels/DenseLabels.h>
00015 #include <shogun/lib/common.h>
00016 #include <shogun/io/File.h>
00017 #include <shogun/io/SGIO.h>
00018 #include <shogun/mathematics/Math.h>
00019 #include <shogun/base/Parameter.h>
00020 
00021 using namespace shogun;
00022 
00023 CDenseLabels::CDenseLabels()
00024 : CLabels()
00025 {
00026     init();
00027 }
00028 
00029 CDenseLabels::CDenseLabels(int32_t num_lab)
00030 : CLabels()
00031 {
00032     init();
00033     m_labels = SGVector<float64_t>(num_lab);
00034     m_current_values=SGVector<float64_t>(num_lab);
00035 }
00036 
00037 CDenseLabels::CDenseLabels(CFile* loader)
00038 : CLabels()
00039 {
00040     init();
00041     load(loader);
00042 }
00043 
00044 CDenseLabels::~CDenseLabels()
00045 {
00046 }
00047 
00048 void CDenseLabels::init()
00049 {
00050     SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
00051 }
00052 
00053 void CDenseLabels::set_to_one()
00054 {
00055     set_to_const(1.0);
00056 }
00057 
00058 void CDenseLabels::zero()
00059 {
00060     set_to_const(0.0);
00061 }
00062 
00063 void CDenseLabels::set_to_const(float64_t c)
00064 {
00065     ASSERT(m_labels.vector)
00066     index_t subset_size=get_num_labels();
00067     for (int32_t i=0; i<subset_size; i++)
00068     {
00069         m_labels.vector[m_subset_stack->subset_idx_conversion(i)]=c;
00070         m_current_values.vector[m_subset_stack->subset_idx_conversion(i)]=c;
00071     }
00072 }
00073 
00074 void CDenseLabels::set_labels(SGVector<float64_t> v)
00075 {
00076     if (m_subset_stack->has_subsets())
00077         SG_ERROR("A subset is set, cannot set labels\n")
00078 
00079     m_labels = v;
00080 }
00081 
00082 SGVector<float64_t> CDenseLabels::get_labels()
00083 {
00084     if (m_subset_stack->has_subsets())
00085         return get_labels_copy();
00086 
00087     return m_labels;
00088 }
00089 
00090 SGVector<float64_t> CDenseLabels::get_labels_copy()
00091 {
00092     if (!m_subset_stack->has_subsets())
00093         return m_labels.clone();
00094 
00095     index_t num_labels = get_num_labels();
00096     SGVector<float64_t> result(num_labels);
00097 
00098     /* copy element wise because of possible subset */
00099     for (index_t i=0; i<num_labels; i++)
00100         result[i] = get_label(i);
00101 
00102     return result;
00103 }
00104 
00105 SGVector<int32_t> CDenseLabels::get_int_labels()
00106 {
00107     SGVector<int32_t> intlab(get_num_labels());
00108 
00109     for (int32_t i=0; i<get_num_labels(); i++)
00110         intlab.vector[i] = get_int_label(i);
00111 
00112     return intlab;
00113 }
00114 
00115 void CDenseLabels::set_int_labels(SGVector<int32_t> lab)
00116 {
00117     if (m_subset_stack->has_subsets())
00118         SG_ERROR("set_int_labels() is not possible on subset")
00119 
00120     m_labels = SGVector<float64_t>(lab.vlen);
00121 
00122     for (int32_t i=0; i<lab.vlen; i++)
00123         set_int_label(i, lab.vector[i]);
00124 }
00125 
00126 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
00127 void CDenseLabels::set_int_labels(SGVector<int64_t> lab)
00128 {
00129     if (m_subset_stack->has_subsets())
00130         SG_ERROR("set_int_labels() is not possible on subset")
00131 
00132     m_labels = SGVector<float64_t>(lab.vlen);
00133 
00134     for (int32_t i=0; i<lab.vlen; i++)
00135         set_int_label(i, lab.vector[i]);
00136 }
00137 #endif
00138 
00139 void CDenseLabels::ensure_valid(const char* context)
00140 {
00141     if (m_labels.vector == NULL)
00142         SG_ERROR("%s%sempty content (NULL) for labels\n", context?context:"", context?": ":"")
00143 }
00144 
00145 void CDenseLabels::load(CFile* loader)
00146 {
00147     remove_subset();
00148     m_labels = SGVector<float64_t>();
00149     m_labels.load(loader);
00150 }
00151 
00152 void CDenseLabels::save(CFile* writer)
00153 {
00154     if (m_subset_stack->has_subsets())
00155         SG_ERROR("save() is not possible on subset")
00156 
00157     m_labels.save(writer);
00158 }
00159 
00160 bool CDenseLabels::set_label(int32_t idx, float64_t label)
00161 {
00162     int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
00163     if (m_labels.vector && real_num<get_num_labels())
00164     {
00165         m_labels.vector[real_num]=label;
00166         return true;
00167     }
00168     else
00169         return false;
00170 }
00171 
00172 bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
00173 {
00174     int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
00175     if (m_labels.vector && real_num<get_num_labels())
00176     {
00177         m_labels.vector[real_num] = (float64_t)label;
00178         return true;
00179     }
00180     else
00181         return false;
00182 }
00183 
00184 float64_t CDenseLabels::get_label(int32_t idx)
00185 {
00186     int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
00187     ASSERT(m_labels.vector && idx<get_num_labels())
00188     return m_labels.vector[real_num];
00189 }
00190 
00191 int32_t CDenseLabels::get_int_label(int32_t idx)
00192 {
00193     int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
00194     ASSERT(m_labels.vector && idx<get_num_labels())
00195     if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num]))))
00196         SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num])
00197 
00198     return int32_t(m_labels.vector[real_num]);
00199 }
00200 
00201 int32_t CDenseLabels::get_num_labels() const
00202 {
00203     return m_subset_stack->has_subsets()
00204             ? m_subset_stack->get_size() : m_labels.vlen;
00205 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation