|
| 1 | +/* |
| 2 | + For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | + The MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2015 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 | + |
| 29 | +/* |
| 30 | + * EcgsimFileToMatrix_Plugin.cc |
| 31 | + * |
| 32 | + * Written by: |
| 33 | + * Jeroen Stinstra |
| 34 | + * Department of Computer Science |
| 35 | + * University of Utah |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | + |
| 40 | +#include <Core/ImportExport/Matrix/MatrixIEPlugin.h> |
| 41 | +#include <Core/IEPlugin/IgbmFileToMatrix_Plugin.h> |
| 42 | +#include <Core/Datatypes/DenseMatrix.h> |
| 43 | +#include <Core/Datatypes/MatrixTypeConversions.h> |
| 44 | +#include <Core/Utils/Legacy/StringUtil.h> |
| 45 | +#include <Core/Logging/LoggerInterface.h> |
| 46 | +#include <Core/ImportExport/Matrix/MatrixIEPlugin.h> |
| 47 | +#include <Core/Datatypes/DenseMatrix.h> |
| 48 | + |
| 49 | +#include <iostream> |
| 50 | +#include <fstream> |
| 51 | +#include <sstream> |
| 52 | + |
| 53 | +using namespace SCIRun; |
| 54 | +using namespace SCIRun::Core; |
| 55 | +using namespace SCIRun::Core::Logging; |
| 56 | +using namespace SCIRun::Core::Datatypes; |
| 57 | + |
| 58 | +MatrixHandle SCIRun::IgbFileMatrix_reader(LoggerHandle pr, const char *filename) |
| 59 | +{ |
| 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 |
| 79 | + { |
| 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) |
| 122 | + { |
| 123 | + nrows++; |
| 124 | + if (ncols > 0) |
| 125 | + { |
| 126 | + if (ncols != line_ncols) |
| 127 | + { |
| 128 | + if (pr) pr->error("Improper format of text file, not every line contains the same amount of numbers"); |
| 129 | + return (result); |
| 130 | + } |
| 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 | + } |
| 144 | + } |
| 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 | + |
| 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; |
| 169 | + |
| 170 | + try |
| 171 | + { |
| 172 | + inputfile.open(filename); |
| 173 | + |
| 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 | + { |
| 202 | + } |
| 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 | +} |
| 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 | + |
| 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 | + |
| 263 | + return (true); |
| 264 | +} |
| 265 | + |
| 266 | + |
| 267 | + |
0 commit comments