SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DenseLabels.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Written (W) 2011 Heiko Strathmann
10  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
13 #include <shogun/labels/Labels.h>
15 #include <shogun/lib/common.h>
16 #include <shogun/io/File.h>
17 #include <shogun/io/SGIO.h>
19 #include <shogun/base/Parameter.h>
20 
21 using namespace shogun;
22 
24 : CLabels()
25 {
26  init();
27 }
28 
30 : CLabels()
31 {
32  init();
33  m_labels = SGVector<float64_t>(num_lab);
35 }
36 
38 : CLabels()
39 {
40  init();
41  load(loader);
42 }
43 
45 {
46 }
47 
48 void CDenseLabels::init()
49 {
50  SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
51 }
52 
54 {
55  set_to_const(1.0);
56 }
57 
59 {
60  set_to_const(0.0);
61 }
62 
64 {
66  index_t subset_size=get_num_labels();
67  for (int32_t i=0; i<subset_size; i++)
68  {
71  }
72 }
73 
75 {
77  SG_ERROR("A subset is set, cannot set labels\n")
78 
79  m_labels = v;
80 }
81 
83 {
85  return get_labels_copy();
86 
87  return m_labels;
88 }
89 
91 {
93  return m_labels.clone();
94 
95  index_t num_labels = get_num_labels();
96  SGVector<float64_t> result(num_labels);
97 
98  /* copy element wise because of possible subset */
99  for (index_t i=0; i<num_labels; i++)
100  result[i] = get_label(i);
101 
102  return result;
103 }
104 
106 {
108 
109  for (int32_t i=0; i<get_num_labels(); i++)
110  intlab.vector[i] = get_int_label(i);
111 
112  return intlab;
113 }
114 
116 {
118  SG_ERROR("set_int_labels() is not possible on subset")
119 
121 
122  for (int32_t i=0; i<lab.vlen; i++)
123  set_int_label(i, lab.vector[i]);
124 }
125 
126 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
128 {
130  SG_ERROR("set_int_labels() is not possible on subset")
131 
133 
134  for (int32_t i=0; i<lab.vlen; i++)
135  set_int_label(i, lab.vector[i]);
136 }
137 #endif
138 
139 void CDenseLabels::ensure_valid(const char* context)
140 {
141  if (m_labels.vector == NULL)
142  SG_ERROR("%s%sempty content (NULL) for labels\n", context?context:"", context?": ":"")
143 }
144 
146 {
147  remove_subset();
149  m_labels.load(loader);
150 }
151 
153 {
155  SG_ERROR("save() is not possible on subset")
156 
157  m_labels.save(writer);
158 }
159 
160 bool CDenseLabels::set_label(int32_t idx, float64_t label)
161 {
162  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
163  if (m_labels.vector && real_num<get_num_labels())
164  {
165  m_labels.vector[real_num]=label;
166  return true;
167  }
168  else
169  return false;
170 }
171 
172 bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
173 {
174  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
175  if (m_labels.vector && real_num<get_num_labels())
176  {
177  m_labels.vector[real_num] = (float64_t)label;
178  return true;
179  }
180  else
181  return false;
182 }
183 
185 {
186  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
188  return m_labels.vector[real_num];
189 }
190 
191 int32_t CDenseLabels::get_int_label(int32_t idx)
192 {
193  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
195  if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num]))))
196  SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num])
197 
198  return int32_t(m_labels.vector[real_num]);
199 }
200 
202 {
203  return m_subset_stack->has_subsets()
205 }
SGVector< float64_t > m_labels
Definition: DenseLabels.h:274
void set_int_labels(SGVector< int32_t > labels)
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:62
index_t get_size() const
Definition: SubsetStack.h:80
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:129
CSubsetStack * m_subset_stack
Definition: Labels.h:140
float64_t get_label(int32_t idx)
SGVector< float64_t > get_labels_copy()
Definition: DenseLabels.cpp:90
bool set_label(int32_t idx, float64_t label)
SGVector< float64_t > get_labels()
Definition: DenseLabels.cpp:82
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:50
virtual void remove_subset()
Definition: Labels.cpp:49
A File access base class.
Definition: File.h:34
void save(CFile *saver)
Definition: SGVector.cpp:866
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
void set_to_const(float64_t c)
Definition: DenseLabels.cpp:63
SGVector< int32_t > get_int_labels()
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void load(CFile *loader)
Definition: SGVector.cpp:846
virtual void ensure_valid(const char *context=NULL)
bool set_int_label(int32_t idx, int32_t label)
SGVector< T > clone() const
Definition: SGVector.cpp:207
virtual bool has_subsets() const
Definition: SubsetStack.h:89
SGVector< float64_t > m_current_values
Definition: Labels.h:143
#define SG_ADD(...)
Definition: SGObject.h:84
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:74
virtual void load(CFile *loader)
virtual void save(CFile *writer)

SHOGUN Machine Learning Toolbox - Documentation