LatentLabels.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) 2012 Viktor Gal
00008  * Copyright (C) 2012 Viktor Gal
00009  */
00010 
00011 #include <shogun/labels/LatentLabels.h>
00012 
00013 using namespace shogun;
00014 
00015 CLatentLabels::CLatentLabels()
00016     : CLabels()
00017 {
00018     init();
00019 }
00020 
00021 CLatentLabels::CLatentLabels(int32_t num_samples)
00022     : CLabels()
00023 {
00024     init();
00025     m_latent_labels = new CDynamicObjectArray(num_samples);
00026     SG_REF(m_latent_labels);
00027 }
00028 
00029 CLatentLabels::CLatentLabels(CLabels* labels)
00030     : CLabels()
00031 {
00032     init();
00033     m_labels = labels;
00034     SG_REF(m_labels);
00035     m_latent_labels = new CDynamicObjectArray(m_labels->get_num_labels());
00036     SG_REF(m_latent_labels);
00037 }
00038 
00039 CLatentLabels::~CLatentLabels()
00040 {
00041     SG_UNREF(m_latent_labels);
00042     SG_UNREF(m_labels);
00043 }
00044 
00045 void CLatentLabels::init()
00046 {
00047     SG_ADD((CSGObject**) &m_latent_labels, "m_latent_labels", "The latent labels", MS_NOT_AVAILABLE);
00048     SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels", MS_NOT_AVAILABLE);
00049     m_latent_labels = NULL;
00050     m_labels = NULL;
00051 }
00052 
00053 CDynamicObjectArray* CLatentLabels::get_latent_labels() const
00054 {
00055     SG_REF(m_latent_labels);
00056     return m_latent_labels;
00057 }
00058 
00059 CData* CLatentLabels::get_latent_label(int32_t idx)
00060 {
00061     ASSERT(m_latent_labels != NULL);
00062     if (idx < 0 || idx >= get_num_labels())
00063         SG_ERROR("Out of index!\n");
00064 
00065     return (CData*) m_latent_labels->get_element(idx);
00066 }
00067 
00068 void CLatentLabels::add_latent_label(CData* label)
00069 {
00070     ASSERT(m_latent_labels != NULL);
00071     m_latent_labels->push_back(label);
00072 }
00073 
00074 bool CLatentLabels::set_latent_label(int32_t idx, CData* label)
00075 {
00076     if (idx < get_num_labels())
00077     {
00078         return m_latent_labels->set_element(label, idx);
00079     }
00080     else
00081     {
00082         return false;
00083     }
00084 }
00085 
00086 void CLatentLabels::ensure_valid(const char* context)
00087 {
00088     if (m_latent_labels == NULL)
00089         SG_ERROR("Non-valid LatentLabels in %s", context);
00090 }
00091 
00092 CLatentLabels* CLatentLabels::obtain_from_generic(CLabels* base_labels)
00093 {
00094     ASSERT(base_labels != NULL);
00095     if (base_labels->get_label_type() == LT_LATENT)
00096         return (CLatentLabels*) base_labels;
00097     else
00098         SG_SERROR("base_labels must be of dynamic type CLatentLabels\n");
00099 
00100     return NULL;
00101 }
00102 
00103 int32_t CLatentLabels::get_num_labels()
00104 {
00105     if (!m_latent_labels || !m_labels)
00106         return 0;
00107     int32_t num_labels = m_latent_labels->get_num_elements();
00108 
00109     ASSERT(num_labels == m_labels->get_num_labels());
00110 
00111     return num_labels;
00112 }
00113 
00114 void CLatentLabels::set_labels(CLabels* labels)
00115 {
00116     SG_UNREF(m_labels);
00117     SG_REF(labels);
00118     m_labels = labels;
00119 }
00120 
00121 CLabels* CLatentLabels::get_labels() const
00122 {
00123     SG_REF(m_labels);
00124     return m_labels;
00125 }
00126 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation