|
| 1 | +/* |
| 2 | + For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | + The MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2011 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 | +/* |
| 31 | + * CalcTMP.cc: class to calcuate transmenbrane potential |
| 32 | + * using algorithm from ECGSim. |
| 33 | + * |
| 34 | + * Written by: |
| 35 | + * Michael Steffen |
| 36 | + * Scientific Computing and Imaging Institute |
| 37 | + * May 2011 |
| 38 | + */ |
| 39 | + |
| 40 | +#include <Packages/BioPSE/Core/Algorithms/NumApproximation/CalcTMP.h> |
| 41 | +#include <Core/Datatypes/MatrixTypeConverter.h> |
| 42 | + |
| 43 | +namespace BioPSE { |
| 44 | + |
| 45 | +using namespace SCIRun; |
| 46 | + |
| 47 | +bool CalcTMPAlgo::calc_single_TMP( |
| 48 | + const double amplitude, |
| 49 | + const double dep, |
| 50 | + const double depslope, |
| 51 | + const double platslope, |
| 52 | + const double rep, |
| 53 | + const double repslope, |
| 54 | + const double rest, |
| 55 | + DenseMatrix& TMP_values) |
| 56 | +{ |
| 57 | + /* |
| 58 | + Inputs: tdep: Depolarization Time |
| 59 | + trep: Repolarization Time |
| 60 | + depslope: Depolarization Slope |
| 61 | + platslope: Plateau Slope |
| 62 | + repslope: Repolarization Slope |
| 63 | + res: |
| 64 | + amplitude: Amplitude |
| 65 | + Output: Dense Matrix (1xsize of matrix) containing the transmembrane potential |
| 66 | + */ |
| 67 | + |
| 68 | + size_type nrows = TMP_values.nrows(); |
| 69 | + size_type ncols = TMP_values.ncols(); |
| 70 | + |
| 71 | + if(nrows != 1){ |
| 72 | + error("CalcTMPAlgo: TMP_values size incorrect"); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if(ncols <= 0){ |
| 77 | + error("CalcTMPAlgo: TMP_values size incorrect"); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + double tdep = -dep; |
| 82 | + double trep = -rep; |
| 83 | + double maxAmpl = 1e-6; |
| 84 | + |
| 85 | + for(index_type t = 0; t < ncols; ++t) |
| 86 | + { |
| 87 | + double vali = 1.0 / (1.0 + exp(-depslope * tdep)) * |
| 88 | + (1.0 / (1.0 + exp(platslope * trep))) * |
| 89 | + (1.0 / (1.0 + exp(repslope * trep))); |
| 90 | + TMP_values.put(0,t,vali); |
| 91 | + if(vali > maxAmpl) |
| 92 | + { |
| 93 | + maxAmpl = vali; |
| 94 | + } |
| 95 | + |
| 96 | + tdep += 1.0; |
| 97 | + trep += 1.0; |
| 98 | + } |
| 99 | + double ampl = (amplitude - rest) / maxAmpl; |
| 100 | + for(index_type t = 0; t < ncols; ++t) |
| 101 | + { |
| 102 | + TMP_values.put(0,t,TMP_values.get(0,t)*ampl + rest); |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +bool CalcTMPAlgo::calc_all_TMPs( |
| 109 | + const DenseMatrix& amplitudes, |
| 110 | + const DenseMatrix& deps, |
| 111 | + const DenseMatrix& depslopes, |
| 112 | + const DenseMatrix& platslopes, |
| 113 | + const DenseMatrix& reps, |
| 114 | + const DenseMatrix& repslopes, |
| 115 | + const DenseMatrix& rests, |
| 116 | + DenseMatrix& TMP_values) |
| 117 | +{ |
| 118 | + if(amplitudes.ncols() != 1 || |
| 119 | + deps.ncols() != 1 || |
| 120 | + depslopes.ncols() != 1 || |
| 121 | + platslopes.ncols() != 1 || |
| 122 | + reps.ncols() != 1 || |
| 123 | + repslopes.ncols() != 1 || |
| 124 | + rests.ncols() != 1) |
| 125 | + { |
| 126 | + error("CalcTMPAlgo: All inputs must be of size 1 x nodes"); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + size_type nnodes = amplitudes.nrows(); |
| 131 | + if(nnodes <= 0){ |
| 132 | + error("CalcTMPAlgo: Size of inputs must be > 0"); |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + if(deps.nrows() != nnodes || |
| 137 | + depslopes.nrows() != nnodes || |
| 138 | + platslopes.nrows() != nnodes || |
| 139 | + reps.nrows() != nnodes || |
| 140 | + repslopes.nrows() != nnodes || |
| 141 | + rests.nrows() != nnodes) |
| 142 | + { |
| 143 | + error("CalcTMPAlgo: All inputs sizes must match"); |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + if(TMP_values.nrows() != nnodes) |
| 148 | + { |
| 149 | + error("CalcTMPAlgo: MP_values size does not match number of nodes"); |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + if(TMP_values.ncols() <= 0) |
| 154 | + { |
| 155 | + error("CalcTMPAlgo: Number of columns in TMP_values must be greater than 0 for output"); |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + DenseMatrix mat(1, TMP_values.ncols()); |
| 160 | + for(index_type node = 0; node < nnodes; ++node) |
| 161 | + { |
| 162 | + if (!calc_single_TMP(amplitudes.get(node,0), |
| 163 | + deps.get(node,0), |
| 164 | + depslopes.get(node,0), |
| 165 | + platslopes.get(node,0), |
| 166 | + reps.get(node,0), |
| 167 | + repslopes.get(node,0), |
| 168 | + rests.get(node,0), |
| 169 | + mat)) |
| 170 | + { |
| 171 | + return false; |
| 172 | + } |
| 173 | + for(index_type t = 0; t < TMP_values.ncols(); ++t) |
| 174 | + { |
| 175 | + TMP_values.put(node, t, mat.get(0, t)); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | +} |
| 181 | + |
| 182 | +bool CalcTMPAlgo::calc_TMPs(MatrixHandle amplitudes, |
| 183 | + MatrixHandle deps, |
| 184 | + MatrixHandle depslopes, |
| 185 | + MatrixHandle platslopes, |
| 186 | + MatrixHandle reps, |
| 187 | + MatrixHandle repslopes, |
| 188 | + MatrixHandle rests, |
| 189 | + unsigned int nsamples, |
| 190 | + MatrixHandle& output) |
| 191 | +{ |
| 192 | + if(matrix_is::sparse(amplitudes) || |
| 193 | + matrix_is::sparse(deps) || |
| 194 | + matrix_is::sparse(depslopes) || |
| 195 | + matrix_is::sparse(platslopes) || |
| 196 | + matrix_is::sparse(reps) || |
| 197 | + matrix_is::sparse(repslopes) || |
| 198 | + matrix_is::sparse(rests)) |
| 199 | + { |
| 200 | + error("CalcTMPAlgo: Sparse matrices not supported."); |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + output = new DenseMatrix(amplitudes->nrows(), nsamples); |
| 205 | + |
| 206 | + if(!calc_all_TMPs(*(amplitudes->dense()), |
| 207 | + *(deps->dense()), |
| 208 | + *(depslopes->dense()), |
| 209 | + *(platslopes->dense()), |
| 210 | + *(reps->dense()), |
| 211 | + *(repslopes->dense()), |
| 212 | + *(rests->dense()), |
| 213 | + *(output->dense()))) |
| 214 | + { |
| 215 | + return false; |
| 216 | + } |
| 217 | + return true; |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +} // end namespace BioPSE |
0 commit comments