SHOGUN
v2.0.0
|
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 } 00035 00036 CDenseLabels::CDenseLabels(CFile* loader) 00037 : CLabels() 00038 { 00039 init(); 00040 load(loader); 00041 } 00042 00043 CDenseLabels::~CDenseLabels() 00044 { 00045 } 00046 00047 void CDenseLabels::init() 00048 { 00049 SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE); 00050 } 00051 00052 void CDenseLabels::set_to_one() 00053 { 00054 set_to_const(1.0); 00055 } 00056 00057 void CDenseLabels::zero() 00058 { 00059 set_to_const(0.0); 00060 } 00061 00062 void CDenseLabels::set_to_const(float64_t c) 00063 { 00064 ASSERT(m_labels.vector); 00065 index_t subset_size=get_num_labels(); 00066 for (int32_t i=0; i<subset_size; i++) 00067 m_labels.vector[m_subset_stack->subset_idx_conversion(i)]=c; 00068 } 00069 00070 void CDenseLabels::set_labels(SGVector<float64_t> v) 00071 { 00072 if (m_subset_stack->has_subsets()) 00073 SG_ERROR("A subset is set, cannot set labels\n"); 00074 00075 m_labels = v; 00076 } 00077 00078 SGVector<float64_t> CDenseLabels::get_labels() 00079 { 00080 if (m_subset_stack->has_subsets()) 00081 SG_ERROR("get_labels() is not possible on subset"); 00082 00083 return m_labels; 00084 } 00085 00086 SGVector<float64_t> CDenseLabels::get_labels_copy() 00087 { 00088 if (!m_subset_stack->has_subsets()) 00089 return m_labels.clone(); 00090 00091 index_t num_labels = get_num_labels(); 00092 SGVector<float64_t> result(num_labels); 00093 00094 /* copy element wise because of possible subset */ 00095 for (index_t i=0; i<num_labels; i++) 00096 result[i] = get_label(i); 00097 00098 return result; 00099 } 00100 00101 SGVector<int32_t> CDenseLabels::get_int_labels() 00102 { 00103 SGVector<int32_t> intlab(get_num_labels()); 00104 00105 for (int32_t i=0; i<get_num_labels(); i++) 00106 intlab.vector[i] = get_int_label(i); 00107 00108 return intlab; 00109 } 00110 00111 void CDenseLabels::set_int_labels(SGVector<int32_t> lab) 00112 { 00113 if (m_subset_stack->has_subsets()) 00114 SG_ERROR("set_int_labels() is not possible on subset"); 00115 00116 m_labels = SGVector<float64_t>(lab.vlen); 00117 00118 for (int32_t i=0; i<lab.vlen; i++) 00119 set_int_label(i, lab.vector[i]); 00120 } 00121 00122 void CDenseLabels::ensure_valid(const char* context) 00123 { 00124 if (m_labels.vector == NULL) 00125 SG_ERROR("%s%sempty content (NULL) for labels\n", context?context:"", context?": ":""); 00126 } 00127 00128 void CDenseLabels::load(CFile* loader) 00129 { 00130 remove_subset(); 00131 00132 SG_SET_LOCALE_C; 00133 m_labels = SGVector<float64_t>(); 00134 ASSERT(loader); 00135 loader->get_vector(m_labels.vector, m_labels.vlen); 00136 SG_RESET_LOCALE; 00137 } 00138 00139 void CDenseLabels::save(CFile* writer) 00140 { 00141 if (m_subset_stack->has_subsets()) 00142 SG_ERROR("save() is not possible on subset"); 00143 00144 SG_SET_LOCALE_C; 00145 ASSERT(writer); 00146 ASSERT(m_labels.vector && m_labels.vlen>0); 00147 writer->set_vector(m_labels.vector, m_labels.vlen); 00148 SG_RESET_LOCALE; 00149 } 00150 00151 bool CDenseLabels::set_label(int32_t idx, float64_t label) 00152 { 00153 int32_t real_num=m_subset_stack->subset_idx_conversion(idx); 00154 if (m_labels.vector && real_num<get_num_labels()) 00155 { 00156 m_labels.vector[real_num]=label; 00157 return true; 00158 } 00159 else 00160 return false; 00161 } 00162 00163 bool CDenseLabels::set_int_label(int32_t idx, int32_t label) 00164 { 00165 int32_t real_num=m_subset_stack->subset_idx_conversion(idx); 00166 if (m_labels.vector && real_num<get_num_labels()) 00167 { 00168 m_labels.vector[real_num] = (float64_t)label; 00169 return true; 00170 } 00171 else 00172 return false; 00173 } 00174 00175 float64_t CDenseLabels::get_label(int32_t idx) 00176 { 00177 int32_t real_num=m_subset_stack->subset_idx_conversion(idx); 00178 ASSERT(m_labels.vector && idx<get_num_labels()); 00179 return m_labels.vector[real_num]; 00180 } 00181 00182 int32_t CDenseLabels::get_int_label(int32_t idx) 00183 { 00184 int32_t real_num=m_subset_stack->subset_idx_conversion(idx); 00185 ASSERT(m_labels.vector && idx<get_num_labels()); 00186 if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num])))) 00187 SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num]); 00188 00189 return int32_t(m_labels.vector[real_num]); 00190 } 00191 00192 int32_t CDenseLabels::get_num_labels() 00193 { 00194 return m_subset_stack->has_subsets() 00195 ? m_subset_stack->get_size() : m_labels.vlen; 00196 }