SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixIO.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Core/Datatypes/MatrixIO.h
29 
30 #ifndef CORE_DATATYPES_MATRIX_IO_H
31 #define CORE_DATATYPES_MATRIX_IO_H
32 
33 #include <Core/Utils/Exception.h>
34 #include <Core/Datatypes/Matrix.h>
39 #include <vector>
40 #include <ostream>
41 #include <istream>
42 
43 namespace SCIRun {
44 namespace Core {
45 namespace Datatypes {
46 
47  template <typename T>
48  std::istream& operator>>(std::istream& istr, DenseMatrixGeneric<T>& m)
49  {
50  std::vector<std::vector<T> > values;
51 
52  std::string line;
53 
54  while (!std::getline(istr, line, '\n').eof())
55  {
56  std::istringstream reader(line);
57 
58  std::vector<T> lineData;
59 
60  while (!reader.eof())
61  {
62  T val;
63  reader >> val;
64 
65  if (reader.fail())
66  {
67  //THROW_INVALID_ARGUMENT("Matrix reading failed: stream failed");
68  break;
69  }
70 
71  lineData.push_back(val);
72  }
73 
74  if (!lineData.empty())
75  {
76  if (!values.empty() && values.back().size() != lineData.size())
77  THROW_INVALID_ARGUMENT("Improper format of matrix text stream: not every line contains the same amount of numbers.");
78 
79  values.push_back(lineData);
80  }
81  }
82 
83  m.resize(values.size(), values[0].size());
84  m.setZero();
85  for (int i = 0; i < m.rows(); ++i)
86  {
87  for (int j = 0; j < m.cols(); ++j)
88  {
89  m(i,j) = values[i][j];
90  }
91  }
92 
93  return istr;
94  }
95 
96  template <typename T>
97  std::string matrix_to_string(const T& m)
98  {
99  std::ostringstream o;
100  o << m;
101  return o.str();
102  }
103 
104 #define DENSEMATRIX_VERSION 4
105 
106  template <typename T>
108  {
109  int version=stream.begin_class("DenseMatrix", DENSEMATRIX_VERSION);
110  // Do the base class first...
111  MatrixIOBase::io(stream);
112 
113  if (version < 4)
114  {
115  int nrows = static_cast<int>(this->rows());
116  int ncols = static_cast<int>(this->cols());
117  stream.io(nrows);
118  stream.io(ncols);
119  this->resize(nrows, ncols);
120  }
121  else
122  {
123  long long nrows = static_cast<long long>(this->rows());
124  long long ncols = static_cast<long long>(this->cols());
125  stream.io(nrows);
126  stream.io(ncols);
127  if(stream.reading())
128  {
129  this->resize(nrows, ncols);
130  }
131  }
132 
133  stream.begin_cheap_delim();
134 
135  int split;
136  if (stream.reading())
137  {
138  if (version > 2)
139  {
140  split = this->separate_raw_;
141  Pio(stream, split);
142  if (this->separate_raw_)
143  {
144  Pio(stream, this->raw_filename_);
145  FILE *f=fopen(this->raw_filename_.c_str(), "r");
146  if (f)
147  {
148  fread(this->data(), sizeof(T), this->rows() * this->cols(), f);
149  fclose(f);
150  }
151  else
152  {
153  const std::string errmsg = "Error reading separated file '" + this->raw_filename_ + "'";
154  std::cerr << errmsg << "\n";
155  BOOST_THROW_EXCEPTION(SCIRun::Core::ExceptionBase() << FileNotFound(errmsg));
156  }
157  }
158  }
159  else
160  {
161  this->separate_raw_ = false;
162  }
163  split = this->separate_raw_;
164  }
165  else
166  { // writing
167  std::string filename = this->raw_filename_;
168  split = this->separate_raw_;
169  if (split)
170  {
171  if (filename == "")
172  {
173  if (stream.file_name.c_str())
174  {
175  size_t pos = stream.file_name.rfind('.');
176  if (pos == std::string::npos) filename = stream.file_name + ".raw";
177  else filename = stream.file_name.substr(0,pos) + ".raw";
178  }
179  else
180  {
181  split=0;
182  }
183  }
184  }
185  Pio(stream, split);
186  if (split)
187  {
188  Pio(stream, filename);
189  FILE *f = fopen(filename.c_str(), "w");
190  fwrite(this->data(), sizeof(T), this->rows() * this->cols(), f);
191  fclose(f);
192  }
193  }
194 
195  if (!split)
196  {
197  size_t block_size = this->rows() * this->cols();
198  if (!stream.block_io(this->data(), sizeof(T), block_size))
199  {
200  for (size_t i = 0; i < block_size; i++)
201  {
202  stream.io(this->data()[i]);
203  }
204  }
205  }
206  stream.end_cheap_delim();
207  stream.end_class();
208  }
209 
210 
211 #define SPARSEROWMATRIX_VERSION 2
212 
213  template <typename T>
215  {
216  int version = stream.begin_class("SparseRowMatrix", SPARSEROWMATRIX_VERSION);
217  // Do the base class first...
218  MatrixBase<T>::io(stream);
219 
220  if (version < 2)
221  {
222  int r = static_cast<int>(this->nrows());
223  int c = static_cast<int>(this->ncols());
224  int n = static_cast<int>(this->nonZeros());
225  stream.io(r);
226  stream.io(c);
227  stream.io(n);
228  this->resize(r,c);
229  this->resizeNonZeros(n);
230  }
231  else
232  {
233  auto r = this->nrows();
234  Pio_size(stream, r);
235  auto c = this->ncols();
236  Pio_size(stream, c);
237  auto n = this->nonZeros();
238  Pio_size(stream, n);
239  if (stream.reading())
240  {
241  this->resize(r,c);
242  this->resizeNonZeros(n);
243  }
244  }
245 
246  stream.begin_cheap_delim();
247  Pio_index(stream, this->outerIndexPtr(), this->nrows() + 1);
248  stream.end_cheap_delim();
249 
250  stream.begin_cheap_delim();
251  Pio_index(stream, this->innerIndexPtr(), this->nonZeros());
252  stream.end_cheap_delim();
253 
254  stream.begin_cheap_delim();
255  Pio(stream, this->valuePtr(), this->nonZeros());
256  stream.end_cheap_delim();
257 
258  stream.end_class();
259  }
260 
261 #define COLUMNMATRIX_VERSION 3
262 
263  template <typename T>
265  {
266  int version = stream.begin_class("ColumnMatrix", COLUMNMATRIX_VERSION);
267 
268  if (version > 1)
269  {
270  // New version inherits from Matrix
271  MatrixBase<T>::io(stream);
272  }
273 
274  if (version < 3)
275  {
276  int nrows = static_cast<int>(this->nrows());
277  stream.io(nrows);
278  this->resize(static_cast<size_type>(nrows), 1);
279  }
280  else
281  {
282  long long nrows= static_cast<long long>(this->nrows());
283  stream.io(nrows);
284  if (stream.reading())
285  {
286  this->resize(static_cast<size_type>(nrows), 1);
287  }
288  }
289 
290  if (!stream.block_io(this->data(), sizeof(T), this->nrows()))
291  {
292  for (size_t i=0; i<this->nrows(); i++)
293  stream.io(this->data()[i]);
294  }
295  stream.end_class();
296  }
297 
298 }}}
299 
300 
301 #endif
virtual void io(Piostream &)
Definition: MatrixIO.h:107
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
virtual void io(Piostream &)
Definition: Matrix.cc:38
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
#define COLUMNMATRIX_VERSION
Definition: MatrixIO.h:261
virtual void io(Piostream &)
Definition: MatrixIO.h:264
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
#define DENSEMATRIX_VERSION
Definition: MatrixIO.h:104
list values
Definition: readAllFields.py:22
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
Manage dynamic properties of persistent objects.
#define THROW_INVALID_ARGUMENT(message)
Definition: Exception.h:71
Definition: Exception.h:46
void Pio_index(Piostream &stream, index_type *data, size_type size)
Definition: Persistent.cc:606
dictionary data
Definition: eabLatVolData.py:11
virtual void io(Piostream &)
Definition: MatrixIO.h:214
virtual void end_cheap_delim()
Definition: Persistent.cc:188
std::istream & operator>>(std::istream &istr, DenseMatrixGeneric< T > &m)
Definition: MatrixIO.h:48
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
virtual void end_class()
Definition: Persistent.cc:178
boost::error_info< struct tag_file_not_found, std::string > FileNotFound
Definition: Exception.h:54
int n
Definition: eab.py:9
std::string file_name
Definition: Persistent.h:135
std::string matrix_to_string(const T &m)
Definition: MatrixIO.h:97
void Pio_size(Piostream &stream, Size &size)
Definition: Persistent.h:273
#define SPARSEROWMATRIX_VERSION
Definition: MatrixIO.h:211