|
27 | 27 | */ |
28 | 28 |
|
29 | 29 | /* |
30 | | - * EcgsimFileToMatrix_Plugin.cc |
| 30 | + * IgbFiletoMatrix_Plugin.cc |
31 | 31 | * |
32 | 32 | * Written by: |
33 | | - * Jeroen Stinstra |
34 | | - * Department of Computer Science |
| 33 | + * Karli Gillette |
| 34 | + * Department of Bioengineering |
35 | 35 | * University of Utah |
36 | 36 | * |
37 | 37 | */ |
|
46 | 46 | #include <Core/ImportExport/Matrix/MatrixIEPlugin.h> |
47 | 47 | #include <Core/Datatypes/DenseMatrix.h> |
48 | 48 |
|
| 49 | +#include <string.h> |
| 50 | +#include <stdlib.h> |
| 51 | +#include <string> |
| 52 | +#include <stdio.h> |
| 53 | +#include <vector> |
| 54 | +#include <boost/tokenizer.hpp> |
| 55 | +#include <boost/foreach.hpp> |
| 56 | +#include <boost/algorithm/string.hpp> |
| 57 | +#include <boost/algorithm/string/iter_find.hpp> |
| 58 | +#include <cmath> |
| 59 | + |
49 | 60 | #include <iostream> |
50 | 61 | #include <fstream> |
51 | 62 | #include <sstream> |
52 | 63 |
|
| 64 | +using namespace std; |
53 | 65 | using namespace SCIRun; |
54 | 66 | using namespace SCIRun::Core; |
55 | 67 | using namespace SCIRun::Core::Logging; |
56 | 68 | using namespace SCIRun::Core::Datatypes; |
57 | 69 |
|
58 | 70 | MatrixHandle SCIRun::IgbFileMatrix_reader(LoggerHandle pr, const char *filename) |
59 | 71 | { |
60 | | - DenseMatrixHandle result; |
61 | | - |
62 | | - int ncols = 0; |
63 | | - int nrows = 0; |
64 | | - int line_ncols = 0; |
65 | | - int header_rows = 0; |
66 | | - int header_cols = 0; |
67 | | - |
68 | | - std::string line; |
69 | | - double data; |
70 | | - |
71 | | - // STAGE 1 - SCAN THE FILE TO DETERMINE THE DIMENSIONS OF THE MATRIX |
72 | | - // AND CHECK THE FILE'S INTEGRITY. |
73 | | - |
74 | | - { |
75 | | - std::ifstream inputfile; |
76 | | - inputfile.exceptions( std::ifstream::badbit ); |
77 | | - |
78 | | - try |
| 72 | + |
| 73 | + string line; |
| 74 | + vector<string> strs; |
| 75 | + int x_size=0; |
| 76 | + int t_size=0; |
| 77 | + int count=0; |
| 78 | + |
| 79 | + |
| 80 | + ifstream myfile (filename); |
| 81 | + if (myfile.is_open()) |
79 | 82 | { |
80 | | - inputfile.open(filename); |
81 | | - |
82 | | - // get header information |
83 | | - getline(inputfile,line,'\n'); |
84 | | - for (size_t p = 0;p<line.size();p++) |
85 | | - { |
86 | | - if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' '; |
87 | | - } |
88 | | - std::istringstream iss(line); |
89 | | - iss >> header_rows; |
90 | | - iss >> header_cols; |
91 | | - |
92 | | - while( getline(inputfile,line,'\n')) |
93 | | - { |
94 | | - if (line.size() > 0) |
95 | | - { |
96 | | - // block out comments |
97 | | - if ((line[0] == '#')||(line[0] == '%')) continue; |
98 | | - } |
99 | | - |
100 | | - // replace comma's and tabs with white spaces |
101 | | - for (size_t p = 0;p<line.size();p++) |
102 | | - { |
103 | | - if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' '; |
104 | | - } |
105 | | - std::istringstream iss(line); |
106 | | - iss.exceptions( std::ifstream::failbit | std::ifstream::badbit); |
107 | | - |
108 | | - try |
109 | | - { |
110 | | - line_ncols = 0; |
111 | | - while(1) |
112 | | - { |
113 | | - iss >> data; |
114 | | - line_ncols++; |
115 | | - } |
116 | | - } |
117 | | - catch(...) |
118 | | - { |
119 | | - } |
120 | | - |
121 | | - if (line_ncols > 0) |
| 83 | + //while ( myfile.good() ) |
| 84 | + for(int i=0; i<5; i++) |
122 | 85 | { |
123 | | - nrows++; |
124 | | - if (ncols > 0) |
125 | | - { |
126 | | - if (ncols != line_ncols) |
| 86 | + getline (myfile,line); |
| 87 | + |
| 88 | + boost::split(strs,line,boost::is_any_of(":, ")); |
| 89 | + size_t sz; |
| 90 | + sz=line.size(); |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + for(int p=0;p<sz;p++) |
127 | 95 | { |
128 | | - if (pr) pr->error("Improper format of text file, not every line contains the same amount of numbers"); |
129 | | - return (result); |
| 96 | + |
| 97 | + std::cout << p << std::endl; |
| 98 | + |
| 99 | + if (boost::iequals(strs[p], "x")) |
| 100 | + { |
| 101 | + x_size=atoi(strs[p+1].c_str()); |
| 102 | + |
| 103 | + |
| 104 | + count += 1; |
| 105 | + |
| 106 | + } |
| 107 | + if (boost::iequals(strs[p], "t")) |
| 108 | + { |
| 109 | + t_size=atoi(strs[p+1].c_str()); |
| 110 | + |
| 111 | + |
| 112 | + count +=1; |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + if (count ==2 ) break; |
| 117 | + |
| 118 | + |
130 | 119 | } |
131 | | - } |
132 | | - else |
133 | | - { |
134 | | - ncols = line_ncols; |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | - |
139 | | - if(ncols*nrows != header_cols*header_rows) |
140 | | - { |
141 | | - if (pr) pr->error("Data does not match header information."); |
142 | | - return(result); |
143 | | - } |
| 120 | + |
| 121 | + |
| 122 | + } |
| 123 | + myfile.close(); |
144 | 124 | } |
145 | | - catch (...) |
146 | | - { |
147 | | - if (pr) pr->error("Could not open file: "+std::string(filename)); |
148 | | - return (result); |
149 | | - } |
150 | | - |
151 | | - inputfile.close(); |
152 | | - } |
153 | | - |
154 | | - // STAGE 2 - NOW ACTUALLY READ AND STORE THE MATRIX |
155 | 125 |
|
156 | | - { |
157 | | - std::ifstream inputfile; |
158 | | - inputfile.exceptions( std::ifstream::badbit ); |
159 | | - |
160 | | - result.reset(new DenseMatrix(header_rows,header_cols)); |
161 | | - if (!result) |
162 | | - { |
163 | | - if (pr) pr->error("Could not allocate matrix"); |
164 | | - return(result); |
165 | | - } |
166 | | - |
167 | | - double* dataptr = result->data(); |
168 | | - int k = 0; |
| 126 | + |
| 127 | + streamoff length=0; |
| 128 | + streamsize numbyts=0; |
| 129 | + //char * buffer; |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + ifstream is; |
| 135 | + is.open (filename, ios::in | ios::binary ); |
| 136 | + if (is.is_open()) |
| 137 | + { |
| 138 | + // get length of file: |
| 139 | + is.seekg (0, ios::end); |
| 140 | + length = is.tellg(); |
| 141 | + is.seekg (1024, ios::beg); |
| 142 | + |
| 143 | + vector<float> vec; |
| 144 | + vec.resize(x_size*t_size); |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + is.read ((char*)&vec[0],x_size * t_size * sizeof(float)); |
| 149 | + if (!is) |
| 150 | + { |
| 151 | + numbyts=is.gcount(); |
| 152 | + cout << "Error reading binary data. Number of bytes read: " << numbyts << endl; |
169 | 153 |
|
170 | | - try |
171 | | - { |
172 | | - inputfile.open(filename); |
| 154 | + } |
| 155 | + is.close(); |
| 156 | + |
| 157 | + |
| 158 | + // auto result(boost::make_shared<DenseMatrix>(x_size,t_size)); |
| 159 | + |
| 160 | + DenseMatrixHandle result; |
| 161 | + |
| 162 | + result.reset(new DenseMatrix(x_size,t_size)); |
| 163 | + |
| 164 | + for(size_t p=0;p<t_size;p++ ) |
| 165 | + { |
| 166 | + |
| 167 | + for(size_t pp=0;pp<x_size;pp++ ) |
| 168 | + { |
| 169 | + |
| 170 | + |
| 171 | + (*result)(pp, p) = vec[(p*x_size)+pp]; |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | + |
173 | 176 |
|
174 | | - // get header information |
175 | | - getline(inputfile,line,'\n'); |
176 | | - |
177 | | - while( getline(inputfile,line,'\n')) |
178 | | - { |
179 | | - if (line.size() > 0) |
180 | | - { |
181 | | - // block out comments |
182 | | - if ((line[0] == '#')||(line[0] == '%')) continue; |
183 | | - } |
184 | | - |
185 | | - // replace comma's and tabs with white spaces |
186 | | - for (size_t p = 0;p<line.size();p++) |
187 | | - { |
188 | | - if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' '; |
189 | | - } |
190 | | - std::istringstream iss(line); |
191 | | - iss.exceptions( std::ifstream::failbit | std::ifstream::badbit); |
192 | | - try |
193 | | - { |
194 | | - while(1) |
195 | | - { |
196 | | - iss >> data; |
197 | | - dataptr[k++] = data; |
198 | | - } |
199 | | - } |
200 | | - catch(...) |
201 | | - { |
| 177 | + return(result); |
202 | 178 | } |
203 | | - } |
204 | | - } |
205 | | - catch (...) |
206 | | - { |
207 | | - if (pr) pr->error("Could not open and read data in file: " + std::string(filename)); |
208 | | - return (result); |
209 | | - } |
210 | | - |
211 | | - inputfile.close(); |
212 | | - } |
213 | | - return(result); |
214 | 179 | } |
215 | | - |
216 | | - |
217 | | -bool SCIRun::IgbFileMatrix_writer(LoggerHandle pr, MatrixHandle matrixInput, const char *filename) |
218 | | -{ |
219 | | - |
220 | | - std::ofstream outputfile; |
221 | | - outputfile.exceptions( std::ofstream::failbit | std::ofstream::badbit ); |
222 | | - |
223 | | - DenseMatrixHandle matrix = castMatrix::toDense(matrixInput); |
224 | 180 |
|
225 | | - if (!matrix) |
226 | | - { |
227 | | - if (pr) pr->error("Empty matrix detected"); |
228 | | - return(false); |
229 | | - } |
230 | | - |
231 | | - double* dataptr = matrix->data(); |
232 | | - if (!dataptr) |
233 | | - { |
234 | | - if (pr) pr->error("Empty matrix detected"); |
235 | | - return(false); |
236 | | - } |
237 | | - |
238 | | - try |
239 | | - { |
240 | | - outputfile.open(filename); |
241 | | - } |
242 | | - catch (...) |
243 | | - { |
244 | | - if (pr) pr->error("Could not open file: "+std::string(filename)); |
245 | | - return (false); |
246 | | - } |
247 | | - |
248 | | - // output header line |
249 | | - //int rows = matrix->nrows(); |
250 | | - //int cols = matrix->ncols(); |
251 | | - outputfile << matrix->nrows() << " " << matrix->ncols() << std::endl; |
252 | | - |
253 | | - size_t k = 0; |
254 | | - for (int p=0; p<matrix->nrows(); p++) |
255 | | - { |
256 | | - for (int q=0; q<matrix->ncols(); q++) |
257 | | - { |
258 | | - outputfile << dataptr[k++] << " "; |
259 | | - } |
260 | | - outputfile << "\n"; |
261 | | - } |
262 | 181 |
|
263 | | - return (true); |
264 | | -} |
| 182 | + |
| 183 | + |
265 | 184 |
|
266 | 185 |
|
267 | 186 |
|
0 commit comments