SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
PlifMatrix.cpp
Go to the documentation of this file.
5 
6 using namespace shogun;
7 
8 CPlifMatrix::CPlifMatrix() : m_PEN(NULL), m_num_plifs(0), m_num_limits(0),
9  m_num_states(0), m_feat_dim3(0), m_plif_matrix(NULL), m_state_signals(NULL)
10 {
11 }
12 
14 {
15  for (int32_t i=0; i<m_num_plifs; i++)
16  delete m_PEN[i];
17  SG_FREE(m_PEN);
18 
19  for (int32_t i=0; i<m_num_states*m_num_states; i++)
20  delete m_plif_matrix[i];
21 
22  SG_FREE(m_plif_matrix);
23 
24  SG_FREE(m_state_signals);
25 }
26 
27 void CPlifMatrix::create_plifs(int32_t num_plifs, int32_t num_limits)
28 {
29  for (int32_t i=0; i<m_num_plifs; i++)
30  delete m_PEN[i];
31  SG_FREE(m_PEN);
32  m_PEN=NULL;
33 
34  m_num_plifs=num_plifs;
35  m_num_limits=num_limits;
36  m_PEN = SG_MALLOC(CPlif*, num_plifs);
37  for (int32_t i=0; i<num_plifs; i++)
38  m_PEN[i]=new CPlif(num_limits) ;
39 }
40 
42 {
43  if (plif_ids.vlen!=m_num_plifs)
44  SG_ERROR("plif_ids size mismatch (num_ids=%d vs.num_plifs=%d)\n", plif_ids.vlen, m_num_plifs)
45 
47  m_ids.set_array(plif_ids.vector, plif_ids.vlen, true, true);
48 
49  for (int32_t i=0; i<m_num_plifs; i++)
50  {
51  int32_t id=get_plif_id(i);
52  m_PEN[id]->set_id(id);
53  }
54 }
55 
57 {
58  if (min_values.vlen!=m_num_plifs)
59  SG_ERROR("plif_values size mismatch (num_values=%d vs.num_plifs=%d)\n", min_values.vlen, m_num_plifs)
60 
61  for (int32_t i=0; i<m_num_plifs; i++)
62  {
63  int32_t id=get_plif_id(i);
64  m_PEN[id]->set_min_value(min_values.vector[i]);
65  }
66 }
67 
69 {
70  if (max_values.vlen!=m_num_plifs)
71  SG_ERROR("plif_values size mismatch (num_values=%d vs.num_plifs=%d)\n", max_values.vlen, m_num_plifs)
72 
73  for (int32_t i=0; i<m_num_plifs; i++)
74  {
75  int32_t id=get_plif_id(i);
76  m_PEN[id]->set_max_value(max_values.vector[i]);
77  }
78 }
79 
81 {
82  if (use_cache.vlen!=m_num_plifs)
83  SG_ERROR("plif_values size mismatch (num_values=%d vs.num_plifs=%d)\n", use_cache.vlen, m_num_plifs)
84 
85  for (int32_t i=0; i<m_num_plifs; i++)
86  {
87  int32_t id=get_plif_id(i);
88  m_PEN[id]->set_use_cache(use_cache.vector[i]);
89  }
90 }
91 
93 {
94  if (use_svm.vlen!=m_num_plifs)
95  SG_ERROR("plif_values size mismatch (num_values=%d vs.num_plifs=%d)\n", use_svm.vlen, m_num_plifs)
96 
97  for (int32_t i=0; i<m_num_plifs; i++)
98  {
99  int32_t id=get_plif_id(i);
100  m_PEN[id]->set_use_svm(use_svm.vector[i]);
101  }
102 }
103 
105 {
106  if (limits.num_rows!=m_num_plifs || limits.num_cols!=m_num_limits)
107  {
108  SG_ERROR("limits size mismatch expected (%d,%d) got (%d,%d)\n",
109  m_num_plifs, m_num_limits, limits.num_rows, limits.num_cols);
110  }
111 
112  for (int32_t i=0; i<m_num_plifs; i++)
113  {
115  for (int32_t k=0; k<m_num_limits; k++)
116  lim[k] = limits.matrix[i*m_num_limits+k];
117 
118  int32_t id=get_plif_id(i);
119  m_PEN[id]->set_plif_limits(lim);
120  }
121 }
122 
124 {
125  if (penalties.num_rows!=m_num_plifs || penalties.num_cols!=m_num_limits)
126  {
127  SG_ERROR("penalties size mismatch expected (%d,%d) got (%d,%d)\n",
128  m_num_plifs, m_num_limits, penalties.num_rows, penalties.num_cols);
129  }
130 
131  for (int32_t i=0; i<m_num_plifs; i++)
132  {
134 
135  for (int32_t k=0; k<m_num_limits; k++)
136  pen[k] = penalties.matrix[i*m_num_limits+k];
137 
138  int32_t id=get_plif_id(i);
139  m_PEN[id]->set_plif_penalty(pen);
140  }
141 }
142 
143 void CPlifMatrix::set_plif_names(SGString<char>* names, int32_t num_values, int32_t maxlen)
144 {
145  if (num_values!=m_num_plifs)
146  SG_ERROR("names size mismatch (num_values=%d vs.num_plifs=%d)\n", num_values, m_num_plifs)
147 
148  for (int32_t i=0; i<m_num_plifs; i++)
149  {
150  int32_t id=get_plif_id(i);
152  m_PEN[id]->set_plif_name(name);
153  SG_FREE(name);
154  }
155 }
156 
157 void CPlifMatrix::set_plif_transform_type(SGString<char>* transform_type, int32_t num_values, int32_t maxlen)
158 {
159  if (num_values!=m_num_plifs)
160  SG_ERROR("transform_type size mismatch (num_values=%d vs.num_plifs=%d)\n", num_values, m_num_plifs)
161 
162  for (int32_t i=0; i<m_num_plifs; i++)
163  {
164  int32_t id=get_plif_id(i);
165  char* transform_str=CStringFeatures<char>::get_zero_terminated_string_copy(transform_type[i]);
166 
167  if (!m_PEN[id]->set_transform_type(transform_str))
168  {
169  SG_FREE(m_PEN);
170  m_PEN=NULL;
171  m_num_plifs=0;
172  m_num_limits=0;
173  SG_ERROR("transform type not recognized ('%s')\n", transform_str)
174  }
175  SG_FREE(transform_str);
176  }
177 }
178 
179 
181 {
182  CPlif** PEN = get_PEN();
183  int32_t num_states = penalties_array.dims[0];
184  int32_t num_plifs = get_num_plifs();
185 
186  for (int32_t i=0; i<m_num_states*m_num_states; i++)
187  delete m_plif_matrix[i];
188  SG_FREE(m_plif_matrix);
189 
190  m_num_states = num_states;
191  m_plif_matrix = SG_MALLOC(CPlifBase*, num_states*num_states);
192 
193  CDynamicArray<float64_t> penalties(penalties_array.array, num_states, num_states, penalties_array.dims[2], true, true) ;
194 
195  for (int32_t i=0; i<num_states; i++)
196  {
197  for (int32_t j=0; j<num_states; j++)
198  {
199  CPlifArray * plif_array = NULL;
200  CPlif * plif = NULL ;
201  for (int32_t k=0; k<penalties_array.dims[2]; k++)
202  {
203  if (penalties.element(i,j,k)==0)
204  continue ;
205 
206  if (!plif_array)
207  {
208  plif_array = new CPlifArray() ;
209  plif_array->clear() ;
210  }
211 
212  int32_t id = (int32_t) penalties.element(i,j,k)-1 ;
213 
214  if ((id<0 || id>=num_plifs) && (id!=-1))
215  {
216  SG_ERROR("id out of range\n")
217  CPlif::delete_penalty_struct(PEN, num_plifs) ;
218  return false ;
219  }
220  plif = PEN[id] ;
221 
222  plif_array->add_plif(plif) ;
223  }
224 
225  if (!plif_array)
226  {
227  m_plif_matrix[i+j*num_states] = NULL ;
228  }
229  else if (plif_array->get_num_plifs()==1)
230  {
231  SG_UNREF(plif_array);
232  ASSERT(plif!=NULL)
233  m_plif_matrix[i+j*num_states] = plif ;
234  }
235  else
236  m_plif_matrix[i+j*num_states] = plif_array ;
237 
238  }
239  }
240  return true;
241 }
242 
244 {
245  int32_t Nplif = get_num_plifs();
246  CPlif** PEN = get_PEN();
247 
248  SG_FREE(m_state_signals);
249  m_feat_dim3 = state_signals.num_rows;
250 
251  CPlifBase **PEN_state_signal = SG_MALLOC(CPlifBase*, state_signals.num_rows*state_signals.num_cols);
252  for (int32_t i=0; i<state_signals.num_cols*state_signals.num_rows; i++)
253  {
254  int32_t id = (int32_t) state_signals.matrix[i]-1 ;
255  if ((id<0 || id>=Nplif) && (id!=-1))
256  {
257  SG_ERROR("id out of range\n")
258  CPlif::delete_penalty_struct(PEN, Nplif) ;
259  return false ;
260  }
261  if (id==-1)
262  PEN_state_signal[i]=NULL ;
263  else
264  PEN_state_signal[i]=PEN[id] ;
265  }
266  m_state_signals=PEN_state_signal;
267  return true;
268 }
269 
271  int32_t *plif_id_matrix, int32_t m, int32_t max_num_signals)
272 {
273  if (m!=m_num_plifs)
274  SG_ERROR("plif_state_signal_matrix size does not match previous info %i!=%i\n", m, m_num_plifs)
275 
276  /*if (m_seq.get_dim3() != max_num_signals)
277  SG_ERROR("size(plif_state_signal_matrix,2) does not match with size(m_seq,3): %i!=%i\nSorry, Soeren... interface changed\n", m_seq.get_dim3(), max_num_signals)
278 
279  CArray2<int32_t> id_matrix(plif_id_matrix, m_num_plifs, max_num_signals, false, false) ;
280  m_PEN_state_signals.resize_array(m_num_plifs, max_num_signals) ;
281  for (int32_t i=0; i<m_num_plifs; i++)
282  {
283  for (int32_t j=0; j<max_num_signals; j++)
284  {
285  if (id_matrix.element(i,j)>=0)
286  m_PEN_state_signals.element(i,j)=m_plif_list[id_matrix.element(i,j)] ;
287  else
288  m_PEN_state_signals.element(i,j)=NULL ;
289  }
290  }*/
291 }
void set_plif_penalty(SGVector< float64_t > p_penalties)
Definition: Plif.h:272
CPlifBase ** m_state_signals
Definition: PlifMatrix.h:218
CDynamicArray< int32_t > m_ids
Definition: PlifMatrix.h:212
void set_max_value(float64_t p_max_value)
Definition: Plif.h:331
void set_plif_name(char *p_name)
Definition: Plif.cpp:97
void set_plif_transform_type(SGString< char > *transform_type, int32_t num_values, int32_t maxlen=0)
Definition: PlifMatrix.cpp:157
#define SG_ERROR(...)
Definition: SGIO.h:129
index_t num_cols
Definition: SGMatrix.h:376
void set_plif_use_cache(SGVector< bool > use_cache)
Definition: PlifMatrix.cpp:80
int32_t get_num_plifs()
Definition: PlifArray.h:45
void set_plif_limits(SGVector< float64_t > p_limits)
Definition: Plif.h:257
class Plif
Definition: Plif.h:40
index_t num_rows
Definition: SGMatrix.h:374
void set_plif_state_signal_matrix(int32_t *plif_id_matrix, int32_t m, int32_t n)
Definition: PlifMatrix.cpp:270
index_t vlen
Definition: SGVector.h:494
bool compute_plif_matrix(SGNDArray< float64_t > penalties_array)
Definition: PlifMatrix.cpp:180
#define ASSERT(x)
Definition: SGIO.h:201
void set_use_cache(int32_t p_use_cache)
Definition: Plif.h:209
void set_plif_penalties(SGMatrix< float64_t > penalties)
Definition: PlifMatrix.cpp:123
void set_plif_limits(SGMatrix< float64_t > limits)
Definition: PlifMatrix.cpp:104
void add_plif(CPlifBase *new_plif)
Definition: PlifArray.cpp:36
void set_plif_min_values(SGVector< float64_t > min_values)
Definition: PlifMatrix.cpp:56
bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
Definition: DynamicArray.h:387
class PlifBase
Definition: PlifBase.h:23
void set_plif_ids(SGVector< int32_t > ids)
Definition: PlifMatrix.cpp:41
void set_plif_use_svm(SGVector< int32_t > use_svm)
Definition: PlifMatrix.cpp:92
int32_t get_plif_id(int32_t idx)
Definition: PlifMatrix.h:156
void set_id(int32_t p_id)
Definition: Plif.h:154
void set_array(T *p_array, int32_t p_num_elements, int32_t array_size)
Definition: DynamicArray.h:419
int32_t get_num_plifs()
Definition: PlifMatrix.h:74
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
index_t * dims
Definition: SGNDArray.h:177
void set_plif_max_values(SGVector< float64_t > max_values)
Definition: PlifMatrix.cpp:68
void set_use_svm(int32_t p_use_svm)
Definition: Plif.h:181
void set_plif_names(SGString< char > *names, int32_t num_values, int32_t maxlen=0)
Definition: PlifMatrix.cpp:143
const T & element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
Definition: DynamicArray.h:224
bool compute_signal_plifs(SGMatrix< int32_t > state_signals)
Definition: PlifMatrix.cpp:243
CPlif ** get_PEN()
Definition: PlifMatrix.h:48
void set_min_value(float64_t p_min_value)
Definition: Plif.h:350
CPlifBase ** m_plif_matrix
Definition: PlifMatrix.h:215
class PlifArray
Definition: PlifArray.h:25
static void delete_penalty_struct(CPlif **PEN, int32_t P)
Definition: Plif.cpp:115
void create_plifs(int32_t num_plifs, int32_t num_limits)
Definition: PlifMatrix.cpp:27
static ST * get_zero_terminated_string_copy(SGString< ST > str)

SHOGUN Machine Learning Toolbox - Documentation