SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StructuredLabels.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) 2012 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
12 
13 using namespace shogun;
14 
16 : CLabels()
17 {
18  init();
19 }
20 
22 : CLabels()
23 {
24  init();
25  m_labels = new CDynamicObjectArray(num_labels);
27 }
28 
30 {
32 }
33 
35 {
36  if ( base_labels->get_label_type() == LT_STRUCTURED )
37  return (CStructuredLabels*) base_labels;
38  else
39  SG_SERROR("base_labels must be of dynamic type CStructuredLabels\n");
40 
41  return NULL;
42 }
43 
44 void CStructuredLabels::ensure_valid(const char* context)
45 {
46  if ( m_labels == NULL )
47  SG_ERROR("Non-valid StructuredLabels in %s", context);
48 }
49 
51 {
53  return m_labels;
54 }
55 
57 {
58  ensure_valid("CStructuredLabels::get_label(int32_t)");
59  if ( idx < 0 || idx >= get_num_labels() )
60  SG_ERROR("Index must be inside [0, num_labels-1]\n");
61 
62  return (CStructuredData*) m_labels->get_element(idx);
63 }
64 
66 {
67  ensure_valid_sdt(label);
68  m_labels->push_back(label);
69 }
70 
72 {
73  ensure_valid_sdt(label);
74  int32_t real_idx = m_subset_stack->subset_idx_conversion(idx);
75 
76  if ( real_idx < get_num_labels() )
77  {
78  return m_labels->insert_element(label, real_idx);
79  }
80  else
81  {
82  return false;
83  }
84 }
85 
87 {
88  if ( m_labels == NULL )
89  return 0;
90  else
91  return m_labels->get_num_elements();
92 }
93 
94 void CStructuredLabels::init()
95 {
96  SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels", MS_NOT_AVAILABLE);
97 
98  m_labels = NULL;
100 }
101 
102 void CStructuredLabels::ensure_valid_sdt(CStructuredData* label)
103 {
104  if ( m_sdt == SDT_UNKNOWN )
105  {
106  m_sdt = label->get_structured_data_type();
107  }
108  else
109  {
110  REQUIRE(label->get_structured_data_type() == m_sdt, "All the labels must "
111  "belong to the same CStructuredData child class\n");
112  }
113 }

SHOGUN Machine Learning Toolbox - Documentation