SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SegmentLoss.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 
5 #include <shogun/lib/config.h>
6 #include <shogun/io/SGIO.h>
8 #include <shogun/base/SGObject.h>
9 //# define DEBUG
10 
11 using namespace shogun;
12 
14  :CSGObject(),
15  m_segment_loss_matrix(1,1),
16  m_segment_loss(1,1,2),
17  m_segment_ids(NULL),
18  m_segment_mask(NULL),
19  m_num_segment_types(0)
20 {
21 }
23 {
24 }
25 
26 void CSegmentLoss::set_segment_loss(float64_t* segment_loss, int32_t m, int32_t n)
27 {
28  // here we need two matrices. Store it in one: 2N x N
29  if (2*m!=n)
30  SG_ERROR("segment_loss should be 2 x quadratic matrix: %i!=%i\n", 2*m, n)
31 
33 
34  m_segment_loss.set_array(segment_loss, m, n/2, 2, true, true) ;
35 }
36 
38 {
39  m_segment_ids = segment_ids;
40 }
41 
43 {
44  m_segment_mask = segment_mask;
45 }
46 
47 void CSegmentLoss::compute_loss(int32_t* all_pos, int32_t len)
48 {
49 #ifdef DEBUG
50  SG_PRINT("compute loss: len: %i, m_num_segment_types: %i\n", len, m_num_segment_types)
51  SG_PRINT("m_segment_mask->element(0):%f \n", m_segment_mask->element(0))
52  SG_PRINT("m_segment_ids->element(0):%i \n", m_segment_ids->element(0))
53 #endif
56 
58 
59  for (int seg_type=0; seg_type<m_num_segment_types; seg_type++)
60  {
61  float32_t value = 0;
62  int32_t last_id = -1;
63  int32_t last_pos = all_pos[len-1];
64  for (int pos=len-1;pos>=0; pos--)
65  {
66  int32_t cur_id = m_segment_ids->element(pos);
67  if (cur_id!=last_id)
68  {
69  // segment contribution
70  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 0);
71  last_id = cur_id;
72  }
73  //length contribution (nucleotide loss)
74  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 1)*(last_pos-all_pos[pos]);
75  last_pos = all_pos[pos];
76  m_segment_loss_matrix.element(seg_type, pos)=value;
77  }
78  }
79 #ifdef DEBUG
81 #endif
82 }
83 
#define SG_ERROR(...)
Definition: SGIO.h:129
CDynamicArray< int32_t > * m_segment_ids
Definition: SegmentLoss.h:104
void compute_loss(int32_t *all_pos, int32_t len)
Definition: SegmentLoss.cpp:47
#define SG_PRINT(...)
Definition: SGIO.h:137
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
void set_segment_ids(CDynamicArray< int32_t > *segment_ids)
Definition: SegmentLoss.cpp:37
double float64_t
Definition: common.h:50
bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
Definition: DynamicArray.h:387
float float32_t
Definition: common.h:49
void set_array(T *p_array, int32_t p_num_elements, int32_t array_size)
Definition: DynamicArray.h:419
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CDynamicArray< float64_t > * m_segment_mask
Definition: SegmentLoss.h:107
CDynamicArray< float64_t > m_segment_loss
Definition: SegmentLoss.h:101
const T & element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
Definition: DynamicArray.h:224
CDynamicArray< float32_t > m_segment_loss_matrix
Definition: SegmentLoss.h:95
int32_t m_num_segment_types
Definition: SegmentLoss.h:110
void set_segment_loss(float64_t *segment_loss, int32_t m, int32_t n)
Definition: SegmentLoss.cpp:26
void set_segment_mask(CDynamicArray< float64_t > *segment_mask)
Definition: SegmentLoss.cpp:42

SHOGUN Machine Learning Toolbox - Documentation