Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
DataItem.h
1 /*
2  * DataItem.h
3  *
4  * Created by Marcel Luethi
5  *
6  * Copyright (c) 2011 University of Basel
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * Neither the name of the project's author nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef __SAMPLE_DATA_H
39 #define __SAMPLE_DATA_H
40 
41 #include "CommonTypes.h"
42 #include "HDF5Utils.h"
43 #include "Representer.h"
44 
45 namespace statismo {
46 /* \class DataItem
47  * \brief Holds all the information for a given sample.
48  * Use GetSample() to recover a Sample
49  * \warning This method generates a new object containing the sample. If the Representer does not provide a smart pointer, the user is responsible for releasing memory.
50  */
51 template <typename T>
52 class DataItem {
53  public:
54  typedef Representer<T> RepresenterType;
55  typedef typename RepresenterType::DatasetPointerType DatasetPointerType;
56 
60  static DataItem* Create(const RepresenterType* representer, const std::string& URI, const VectorType& sampleVector) {
61  return new DataItem(representer, URI, sampleVector);
62  }
63 
67  virtual ~DataItem() {}
68 
72  static DataItem* Load(const RepresenterType* representer, const H5::Group& dsGroup);
76  virtual void Save(const H5::Group& dsGroup) const;
77 
81  std::string GetDatasetURI() const {
82  return m_URI;
83  }
84 
88  const RepresenterType* GetRepresenter() const {
89  return m_representer;
90  }
91 
95  const VectorType& GetSampleVector() const {
96  return m_sampleVector;
97  }
98 
103  const DatasetPointerType GetSample() const {
104  return m_representer->SampleVectorToSample(m_sampleVector);
105  }
106 
107  protected:
108 
109  DataItem(const RepresenterType* representer, const std::string& URI, const VectorType& sampleVector)
110  : m_representer(representer), m_URI(URI), m_sampleVector(sampleVector) {
111  }
112 
113  DataItem(const RepresenterType* representer) : m_representer(representer) {
114  }
115 
116  // loads the internal state from the hdf5 file
117  virtual void LoadInternal(const H5::Group& dsGroup) {
118  VectorType v;
119  HDF5Utils::readVector(dsGroup, "./samplevector", m_sampleVector);
120  m_URI = HDF5Utils::readString(dsGroup, "./URI");
121  }
122 
123  virtual void SaveInternal(const H5::Group& dsGroup) const {
124  HDF5Utils::writeVector(dsGroup, "./samplevector", m_sampleVector);
125  HDF5Utils::writeString(dsGroup, "./URI", m_URI);
126  }
127 
128 
129  const RepresenterType* m_representer;
130  std::string m_URI;
131  VectorType m_sampleVector;
132 };
133 
134 
135 
136 
137 /* \class DataItemWithSurrogates
138  * \brief Holds all the information for a given sample.
139  * Use GetSample() to recover a Sample
140  * \warning This method generates a new object containing the sample. If the Representer does not provide a smart pointer, the user is responsible for releasing memory.
141  * In particular, it enables to associate categorical or continuous variables with a sample, in a vectorial representation.
142  * The vector is provided by a file providing the values in ascii format (empty space or EOL separating the values)
143  * \sa DataItem
144  * \sa DataManagerWithSurrogates
145  */
146 
147 template <typename T>
148 class DataItemWithSurrogates : public DataItem<T> {
149  friend class DataItem<T>;
150  typedef Representer<T> RepresenterType;
151 
152  public:
153 
154  enum SurrogateType {
155  Categorical = 0,
156  Continuous = 1
157  };
158 
159 
160  typedef std::vector<SurrogateType> SurrogateTypeVectorType;
161 
162 
163 
164 
165  static DataItemWithSurrogates* Create(const RepresenterType* representer,
166  const std::string& datasetURI,
167  const VectorType& sampleVector,
168  const std::string& surrogateFilename,
169  const VectorType& surrogateVector) {
170  return new DataItemWithSurrogates(representer, datasetURI, sampleVector, surrogateFilename, surrogateVector);
171  }
172 
173 
174 
175 
176  virtual ~DataItemWithSurrogates() {}
177 
178  const VectorType& GetSurrogateVector() const {
179  return m_surrogateVector;
180  }
181  const std::string& GetSurrogateFilename() const {
182  return m_surrogateFilename;
183  }
184 
185  private:
186 
187  DataItemWithSurrogates(const RepresenterType* representer,
188  const std::string& datasetURI,
189  const VectorType& sampleVector,
190  const std::string& surrogateFilename,
191  const VectorType& surrogateVector)
192  : DataItem<T>(representer, datasetURI, sampleVector),
193  m_surrogateFilename(surrogateFilename),
194  m_surrogateVector(surrogateVector) {
195  }
196 
197  DataItemWithSurrogates(const RepresenterType* r) : DataItem<T>(r) {}
198 
199  // loads the internal state from the hdf5 file
200  virtual void LoadInternal(const H5::Group& dsGroup) {
201  DataItem<T>::LoadInternal(dsGroup);
202  VectorType v;
203  HDF5Utils::readVector(dsGroup, "./surrogateVector", this->m_surrogateVector);
204  m_surrogateFilename = HDF5Utils::readString(dsGroup, "./surrogateFilename");
205  }
206 
207  virtual void SaveInternal(const H5::Group& dsGroup) const {
208  DataItem<T>::SaveInternal(dsGroup);
209  HDF5Utils::writeVector(dsGroup, "./surrogateVector", this->m_surrogateVector);
210  HDF5Utils::writeString(dsGroup, "./surrogateFilename", this->m_surrogateFilename);
211  }
212 
213  std::string m_surrogateFilename;
214  VectorType m_surrogateVector;
215 };
216 
217 
218 } // namespace statismo
219 
220 #include "DataItem.hxx"
221 
222 #endif // __SAMPLE_DATA_H
223 
A trivial representer, that does no representation at all, but works directly with vectorial data...
Definition: TrivialVectorialRepresenter.h:83
static std::string readString(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:379
static H5::DataSet writeString(const H5::CommonFG &fg, const char *name, const std::string &s)
Definition: HDF5Utils.hxx:369
static void readVector(const H5::CommonFG &fg, const char *name, unsigned nElements, VectorType &vector)
Definition: HDF5Utils.hxx:296
static H5::DataSet writeVector(const H5::CommonFG &fg, const char *name, const VectorType &vector)
Definition: HDF5Utils.hxx:363