XMM - Probabilistic Models for Motion Recognition and Mapping

xmmPhrase.hpp
Go to the documentation of this file.
1 /*
2  * xmmPhrase.hpp
3  *
4  * Multimodal data phrase
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmPhrase_h
34 #define xmmPhrase_h
35 
36 #include "../common/xmmAttribute.hpp"
37 #include "../common/xmmEvents.hpp"
38 #include "../common/xmmJson.hpp"
39 #include <cmath>
40 
41 namespace xmm {
50 enum class MemoryMode {
55  OwnMemory,
56 
62 };
63 
68 enum class Multimodality {
73  Unimodal,
74 
79  Bimodal
80 };
81 
82 class Phrase;
83 
88 class PhraseEvent {
89  public:
93  enum class Type {
97  LabelChanged,
98  };
99 
105  PhraseEvent(Phrase* phrase_, Type type_) : phrase(phrase_), type(type_) {}
106 
111  PhraseEvent(PhraseEvent const& src) : phrase(src.phrase), type(src.type) {}
112 
117 
122 };
123 
133 class Phrase : public Writable {
134  public:
135  friend class TrainingSet;
136 
143  Multimodality multimodality = Multimodality::Unimodal);
144 
149  Phrase(Phrase const& src);
150 
155  explicit Phrase(Json::Value const& root);
156 
161  Phrase& operator=(Phrase const& src);
162 
168  virtual ~Phrase();
169 
171 
178  bool ownMemory() const;
179 
185  bool bimodal() const;
186 
188 
190 
196  unsigned int size() const;
197 
202  unsigned int inputSize() const;
203 
208  unsigned int outputSize() const;
209 
214  bool empty() const;
215 
223  float getValue(unsigned int index, unsigned int dim) const;
224 
235  float* getPointer(unsigned int index) const;
236 
247  float* getPointer_input(unsigned int index) const;
248 
260  float* getPointer_output(unsigned int index) const;
261 
263 
265 
276  void connect(float* pointer_to_data, unsigned int length);
277 
290  void connect(float* pointer_to_data_input, float* pointer_to_data_output,
291  unsigned int length);
292 
303  void connect_input(float* pointer_to_data, unsigned int length);
304 
315  void connect_output(float* pointer_to_data, unsigned int length);
316 
324  void disconnect();
325 
327 
329 
342  void record(std::vector<float> const& observation);
343 
355  void record_input(std::vector<float> const& observation);
356 
368  void record_output(std::vector<float> const& observation);
369 
378  void clear();
379  void clearInput();
380  void clearOutput();
381 
383 
385 
391  Json::Value toJson() const;
392 
398  void fromJson(Json::Value const& root);
399 
401 
403 
409  std::vector<float> mean() const;
410 
416  std::vector<float> standardDeviation() const;
417 
422  std::vector<std::pair<float, float>> minmax() const;
423 
429  void rescale(std::vector<float> offset, std::vector<float> gain);
430 
432 
437 
442 
447 
451  std::vector<std::string> column_names;
452 
453  protected:
457  void trim();
458 
465  void reallocateLength();
466 
470  virtual void onAttributeChange(AttributeBase* attr_pointer);
471 
472  static const unsigned int AllocationBlockSize = 256;
473 
478 
482  bool bimodal_;
483 
487  bool empty_;
488 
493  unsigned int length_;
494 
498  unsigned int input_length_;
499 
503  unsigned int output_length_;
504 
508  unsigned int max_length_;
509 
514  float** data_;
515 
517 };
518 
526 template <typename T>
527 T* reallocate(T* src, unsigned int dim_src, unsigned int dim_dst) {
528  T* dst = new T[dim_dst];
529 
530  if (!src) return dst;
531 
532  if (dim_dst > dim_src) {
533  std::copy(src, src + dim_src, dst);
534  } else {
535  std::copy(src, src + dim_dst, dst);
536  }
537  delete[] src;
538  return dst;
539 }
540 }
541 
542 #endif
Data phrase.
Definition: xmmPhrase.hpp:133
Multimodality
Number of modalities in the data phrase.
Definition: xmmPhrase.hpp:68
EventGenerator< PhraseEvent > events
Definition: xmmPhrase.hpp:516
unsigned int output_length_
Length of the array of the output modality.
Definition: xmmPhrase.hpp:503
Attribute< std::string > label
Main label of the phrase.
Definition: xmmPhrase.hpp:446
PhraseEvent(Phrase *phrase_, Type type_)
Default constructor.
Definition: xmmPhrase.hpp:105
memory is shared with other data structures
unsigned int length_
Length of the phrase. If bimodal, it is the minimal length between modalities.
Definition: xmmPhrase.hpp:493
unsigned int input_length_
Length of the array of the input modality.
Definition: xmmPhrase.hpp:498
float ** data_
Pointer to the Data arrays.
Definition: xmmPhrase.hpp:514
Base Class for Generic Attributes.
Definition: xmmAttribute.hpp:105
PhraseEvent(PhraseEvent const &src)
Copy constructor.
Definition: xmmPhrase.hpp:111
Phrase * phrase
pointer to the source phrase
Definition: xmmPhrase.hpp:116
std::vector< std::string > column_names
labels of the columns of the phrase (e.g. descriptor names)
Definition: xmmPhrase.hpp:451
two modalities (i.e. 2 data arrays)
Base class for the definition of training sets.
Definition: xmmTrainingSet.hpp:46
Generator class for a specific type of events.
Definition: xmmEvents.hpp:47
bool bimodal_
Defines if the phrase is bimodal (true) or unimodal (false)
Definition: xmmPhrase.hpp:482
Attribute< unsigned int > dimension_input
Used in bimodal mode: dimension of the input modality.
Definition: xmmPhrase.hpp:441
Abstract class for handling JSON + File I/O.
Definition: xmmJson.hpp:50
Attribute< unsigned int > dimension
Total dimension of the phrase.
Definition: xmmPhrase.hpp:436
Type
Type of event.
Definition: xmmPhrase.hpp:93
Event that can be thrown by a phrase to a training set.
Definition: xmmPhrase.hpp:88
Definition: xmmAttribute.hpp:42
unsigned int max_length_
Allocated length (only used in own memory mode)
Definition: xmmPhrase.hpp:508
bool own_memory_
Defines if the phrase stores the data itself.
Definition: xmmPhrase.hpp:477
Type type
Type of event.
Definition: xmmPhrase.hpp:121
T * reallocate(T *src, unsigned int dim_src, unsigned int dim_dst)
Reallocate a C-like array (using c++ std::copy)
Definition: xmmPhrase.hpp:527
single modality (i.e. 1 data array)
bool empty_
true if the phrase does not contain any data
Definition: xmmPhrase.hpp:487
MemoryMode
Type of memory management for training sets and phrases.
Definition: xmmPhrase.hpp:50
memory is owned by the Phrase container.