SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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/lib/Array.h>
9 #include <shogun/lib/Array2.h>
10 #include <shogun/lib/Array3.h>
11 #include <shogun/base/SGObject.h>
12 //# define DEBUG
13 
14 using namespace shogun;
15 
17  :CSGObject(),
18  m_segment_loss_matrix(1,1),
19  m_segment_loss(1,1,2),
20  m_segment_ids(NULL),
21  m_segment_mask(NULL),
22  m_num_segment_types(0)
23 {
24 }
26 {
27 }
28 
29 void CSegmentLoss::set_segment_loss(float64_t* segment_loss, int32_t m, int32_t n)
30 {
31  // here we need two matrices. Store it in one: 2N x N
32  if (2*m!=n)
33  SG_ERROR( "segment_loss should be 2 x quadratic matrix: %i!=%i\n", 2*m, n) ;
34 
36 
37  m_segment_loss.set_array(segment_loss, m, n/2, 2, true, true) ;
38 }
39 
41 {
42  m_segment_ids = segment_ids;
43 }
44 
46 {
47  m_segment_mask = segment_mask;
48 }
49 
50 void CSegmentLoss::compute_loss(int32_t* all_pos, int32_t len)
51 {
52 #ifdef DEBUG
53  SG_PRINT("compute loss: len: %i, m_num_segment_types: %i\n", len, m_num_segment_types);
54  SG_PRINT("m_segment_mask->element(0):%f \n", m_segment_mask->element(0));
55  SG_PRINT("m_segment_ids->element(0):%i \n", m_segment_ids->element(0));
56 #endif
57  ASSERT(m_segment_ids->get_dim1()==len);
59 
61 
62  for (int seg_type=0; seg_type<m_num_segment_types; seg_type++)
63  {
64  float32_t value = 0;
65  int32_t last_id = -1;
66  int32_t last_pos = all_pos[len-1];
67  for (int pos=len-1;pos>=0; pos--)
68  {
69  int32_t cur_id = m_segment_ids->element(pos);
70  if (cur_id!=last_id)
71  {
72  // segment contribution
73  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 0);
74  last_id = cur_id;
75  }
76  //length contribution (nucleotide loss)
77  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 1)*(last_pos-all_pos[pos]);
78  last_pos = all_pos[pos];
79  m_segment_loss_matrix.element(seg_type, pos)=value;
80  }
81  }
82 #ifdef DEBUG
84 #endif
85 }
86 
CArray< int32_t > * m_segment_ids
Definition: SegmentLoss.h:106
CArray3< float64_t > m_segment_loss
Definition: SegmentLoss.h:103
bool resize_array(int32_t dim1, int32_t dim2)
Definition: Array2.h:139
void set_array(T *p_array, int32_t dim1, int32_t dim2, int32_t dim3, bool p_free_array, bool copy_array=false)
Definition: Array3.h:148
void set_segment_mask(CArray< float64_t > *segment_mask)
Definition: SegmentLoss.cpp:45
const T & element(int32_t idx1, int32_t idx2) const
Definition: Array2.h:179
#define SG_ERROR(...)
Definition: SGIO.h:75
void display_array() const
Definition: Array2.h:243
CArray< float64_t > * m_segment_mask
Definition: SegmentLoss.h:109
void compute_loss(int32_t *all_pos, int32_t len)
Definition: SegmentLoss.cpp:50
CArray2< float32_t > m_segment_loss_matrix
Definition: SegmentLoss.h:97
const T & element(int32_t idx1, int32_t idx2, int32_t idx3) const
Definition: Array3.h:209
#define SG_PRINT(...)
Definition: SGIO.h:81
#define ASSERT(x)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
double float64_t
Definition: common.h:56
void set_segment_ids(CArray< int32_t > *segment_ids)
Definition: SegmentLoss.cpp:40
float float32_t
Definition: common.h:55
const T & element(int32_t idx1) const
Definition: Array.h:214
int32_t get_dim1()
Definition: Array.h:164
int32_t m_num_segment_types
Definition: SegmentLoss.h:112
void set_segment_loss(float64_t *segment_loss, int32_t m, int32_t n)
Definition: SegmentLoss.cpp:29

SHOGUN Machine Learning Toolbox - Documentation