NICE
Northeastern Interactive Clustering Engine
util.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2016 Northeastern University
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef CPP_INCLUDE_UTIL_H_
24 #define CPP_INCLUDE_UTIL_H_
25 
26 #include <cstdlib>
27 #include <string>
28 #include <fstream>
29 #include <iostream>
30 #include <algorithm>
31 #include <sstream>
32 #include <vector>
33 
34 #include "include/matrix.h"
35 #include "include/vector.h"
36 
37 namespace Nice {
38 
39 namespace util {
40 
54 template<typename T>
55 Matrix<T> FromFile(const std::string &input_file_path,
56  int num_rows, int num_cols,
57  const std::string delimiter = " ") {
58  // Reads in the file from "input_file_path"
59  std::ifstream input_file(input_file_path, std::ifstream::in);
60  Matrix<T> m(num_rows, num_cols);
61  T coef;
62  if (input_file) {
63  int i = 0;
64  // Iterates over every line in the input file
65  while ( !input_file.eof() ) {
66  std::string line;
67  getline(input_file, line);
68  if (line.find_first_not_of(' ') == std::string::npos)
69  continue;
70  if (delimiter == " " && line.find_first_of(',') != std::string::npos) {
71  std::cerr << "File uses different delimiter than parameter! Use ','!";
72  exit(1);
73  } else if (delimiter == ",") {
74  if (line.find_first_of(',') == std::string::npos) {
75  std::cerr << "File uses different delimiter than parameter! Use ' '!";
76  exit(1);
77  }
78  // Replaces every instance of the "delimiter" with whitespace for comma
79  std::replace(line.begin(), line.end(), ',', ' ');
80  } else if (delimiter != " ") {
81  // If the function is called with an invalid delimiter, create an error
82  std::cerr << "'" + delimiter + "' isn't an accepted delimiter";
83  exit(1);
84  }
85  // Creates a stringstream out of every line in the file
86  std::stringstream stream(line);
87  int j = 0;
88  // Reads in every coefficient in the string stream and puts it into
89  // the matrix
90  while (stream >> coef) {
91  m(i, j) = coef;
92  ++j;
93  }
94  ++i;
95  }
96  return m;
97  } else {
98  // Error for when the file doesn't exist
99  std::cerr << "Cannot open file " + input_file_path + ", exiting...";
100  exit(1);
101  }
102 }
103 
113 template<typename T>
114 Matrix<T> FromFile(const std::string &input_file_path,
115  const std::string delimiter = " ") {
116  // Reads in the file from "input_file_path"
117  std::ifstream input_file(input_file_path, std::ifstream::in);
118  Matrix<T> m;
119  std::string line;
120  std::vector<T> temp_buffer;
121  T coef;
122  int num_cols = 0;
123  int num_rows = 0;
124  int cols_in_row;
125  if (input_file) {
126  // Iterates over every line in the input file
127  while (!input_file.eof()) {
128  getline(input_file, line);
129  if (line.find_first_not_of(' ') == std::string::npos)
130  continue;
131  if (delimiter == " " && line.find_first_of(',') != std::string::npos) {
132  std::cerr << "File uses different delimiter than parameter! Use ','!";
133  exit(1);
134  } else if (delimiter == ",") {
135  if (line.find_first_of(',') == std::string::npos) {
136  std::cerr << "File uses different delimiter than parameter! Use ' '!";
137  exit(1);
138  }
139  // Replaces every instance of the "delimiter" with whitespace for comma
140  std::replace(line.begin(), line.end(), ',', ' ');
141  } else if (delimiter != " ") {
142  // If the function is called with an invalid delimiter, create an error
143  std::cerr << "'" + delimiter + "' isn't an accepted delimiter";
144  exit(1);
145  }
146  // Creates a stringstream out of every line in the file
147  std::stringstream stream(line);
148  cols_in_row = 0;
149  // Reads every coefficient in the stringstream into the temporary buffer
150  while (stream >> coef) {
151  temp_buffer.push_back(coef);
152  ++cols_in_row;
153  }
154  // If the number of columns in the matrix hasn't been set, make it the
155  // current number of columns in the row
156  if (num_cols == 0) {
157  num_cols = cols_in_row;
158  // If the matrix in the file is shaped incorrectly, throw an error
159  } else if (num_cols != cols_in_row) {
160  std::cerr << "Problem with Matrix in: " + input_file_path +
161  ", exiting...";
162  exit(1);
163  }
164  ++num_rows;
165  }
166  // Instantiate the matrix's size and feed it the coefficients in the
167  // temporary buffer
168  m.resize(num_rows, num_cols);
169  for (int i = 0; i < num_rows; ++i)
170  for (int j = 0; j < num_cols; ++j)
171  m(i, j) = temp_buffer[i * num_cols + j];
172 
173  return m;
174  } else {
175  // Error for when the file doesn't exist
176  std::cerr << "Cannot open file " + input_file_path + ", exiting...";
177  exit(1);
178  }
179 }
180 
181 template<typename T>
182 Vector<T> FromFile(const std::string &input_file_path,
183  int num_elements) {
184  std::ifstream input_file(input_file_path, std::ifstream::in);
185  Vector<T> m(num_elements);
186  if (input_file) {
187  for (int i = 0; i < num_elements; i++)
188  input_file >> m(i);
189  return m;
190  } else {
191  std::cerr << "Cannot open file " + input_file_path + ", exiting..." <<
192  std::endl;
193  exit(1);
194  }
195 }
196 
197 template<typename T>
198 static T reciprocal(T x) {
199  return T(1) / x;
200 }
201 
202 } // namespace util
203 
204 } // namespace Nice
205 
206 #endif // CPP_INCLUDE_UTIL_H_
Definition: cpu_operations.h:36
Matrix< T > FromFile(const std::string &input_file_path, int num_rows, int num_cols, const std::string delimiter=" ")
Definition: util.h:55
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Definition: vector.h:31
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition: matrix.h:31