|
| 1 | +#************************************************************************************************* |
| 2 | +#** |
| 3 | +#** Copyright (c) 2017, 2018 Danny Petschke. All rights reserved. |
| 4 | +#** |
| 5 | +#** Redistribution and use in source and binary forms, with or without modification, |
| 6 | +#** are permitted provided that the following conditions are met: |
| 7 | +#** |
| 8 | +#** 1. Redistributions of source code must retain the above copyright notice, |
| 9 | +#** this list of conditions and the following disclaimer. |
| 10 | +#** |
| 11 | +#** 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +#** this list of conditions and the following disclaimer in the documentation |
| 13 | +#** and/or other materials provided with the distribution. |
| 14 | +#** |
| 15 | +#** 3. Neither the name of the copyright holder "Danny Petschke" nor the names of its |
| 16 | +#** contributors may be used to endorse or promote products derived from this software |
| 17 | +#** without specific prior written permission. |
| 18 | +#** |
| 19 | +#** |
| 20 | +#** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 21 | +#** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | +#** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | +#** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | +#** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +#** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | +#** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 27 | +#** TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +#** EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +#** |
| 30 | +#** Contact: danny.petschke@uni-wuerzburg.de |
| 31 | +#** |
| 32 | +#************************************************************************************************* |
| 33 | + |
| 34 | +import numpy as np |
| 35 | +from scipy.special import beta |
| 36 | +from enum import Enum |
| 37 | + |
| 38 | +#list of available models: |
| 39 | +class ReconvolutionModel(Enum): |
| 40 | + Gaussian = 1 |
| 41 | + Lorentz_Cauchy = 2 |
| 42 | + Pseudovoigt1 = 3 |
| 43 | + Pearson7 = 4 |
| 44 | + |
| 45 | +#definition of available models: |
| 46 | +def Gaussian(x, ampl, sigma, y0, x0, args=()): |
| 47 | + h=np.zeros(x.size) |
| 48 | + N=np.zeros(x.size) |
| 49 | + N=1.0/(sigma*np.sqrt(2*np.pi)) |
| 50 | + h=N*np.exp(-0.5*((x-x0)/sigma)**2); |
| 51 | + return ampl*h+y0 |
| 52 | + |
| 53 | +def Lorentz_Cauchy(x, ampl, a, wing, y0, x0, args=()): |
| 54 | + h=np.zeros(x.size) |
| 55 | + h=wing/(np.pi*((x-x0)*(x-x0) + wing*wing)) |
| 56 | + return ampl*h+y0 |
| 57 | + |
| 58 | +def Pseudovoigt1(x, ampl, a, sigma, wing, y0, x0, args=()): |
| 59 | + G=np.zeros(x.size) |
| 60 | + L=np.zeros(x.size) |
| 61 | + G=(1.0/(sigma*np.sqrt(2*np.pi)))*np.exp(-0.5*((x-x0)/sigma)*((x-x0)/sigma)) |
| 62 | + L=wing/(np.pi*((x-x0)*(x-x0) + wing*wing)) |
| 63 | + return ampl*(a*G+(1-a)*L)+y0 |
| 64 | + |
| 65 | +def Pearson7(x, ampl, alpha, m, y0, x0, args=()): |
| 66 | + h=np.zeros(x.size) |
| 67 | + h=(1/(alpha*beta(m-0.5,0.5)))*(1+((x-x0)/alpha)**2)**(-m) |
| 68 | + return ampl*h+y0 |
| 69 | + |
| 70 | + |
| 71 | +def convolveData(a, b): |
| 72 | + return np.real(np.fft.ifft(np.fft.fft(a)*np.fft.fft(b))) |
| 73 | + |
| 74 | +#1 component expontential distribution: |
| 75 | +def ExpDecay_1(x, ampl1, tau1, y0, x0, args=()): |
| 76 | + h = np.zeros(x.size) |
| 77 | + lengthVec = len(args) |
| 78 | + |
| 79 | + shift_1 = np.remainder(np.remainder(x-np.floor(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 80 | + shift_Incr1 = (1 - x0 + np.floor(x0))*args[shift_1.astype(int)] |
| 81 | + |
| 82 | + shift_2 = np.remainder(np.remainder(x-np.ceil(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 83 | + shift_Incr2 = (x0 - np.floor(x0))*args[shift_2.astype(int)] |
| 84 | + |
| 85 | + irf_shifted = (shift_Incr1 + shift_Incr2) |
| 86 | + irf_norm = irf_shifted/sum(irf_shifted) |
| 87 | + |
| 88 | + h = ampl1*np.exp(-(x)/tau1) |
| 89 | + hConvIrf_norm = convolveData(h, irf_norm) + y0 |
| 90 | + return hConvIrf_norm |
| 91 | + |
| 92 | +#2 component expontential distribution: |
| 93 | +def ExpDecay_2(x, ampl1, tau1, ampl2, tau2, y0, x0, args=()): |
| 94 | + h = np.zeros(x.size) |
| 95 | + lengthVec = len(x) |
| 96 | + |
| 97 | + shift_1 = np.remainder(np.remainder(x-np.floor(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 98 | + shift_Incr1 = (1 - x0 + np.floor(x0))*args[shift_1.astype(int)] |
| 99 | + |
| 100 | + shift_2 = np.remainder(np.remainder(x-np.ceil(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 101 | + shift_Incr2 = (x0 - np.floor(x0))*args[shift_2.astype(int)] |
| 102 | + |
| 103 | + irf_shifted = (shift_Incr1 + shift_Incr2) |
| 104 | + irf_norm = irf_shifted/sum(irf_shifted) |
| 105 | + |
| 106 | + h = ampl1*np.exp(-(x)/tau1) + ampl2*np.exp(-(x)/tau2) |
| 107 | + hConvIrf_norm = convolveData(h, irf_norm) + y0 |
| 108 | + return hConvIrf_norm |
| 109 | + |
| 110 | +#3 component expontential distribution: |
| 111 | +def ExpDecay_3(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, y0, x0, args=()): |
| 112 | + h = np.zeros(x.size) |
| 113 | + lengthVec = len(args) |
| 114 | + |
| 115 | + shift_1 = np.remainder(np.remainder(x-np.floor(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 116 | + shift_Incr1 = (1 - x0 + np.floor(x0))*args[shift_1.astype(int)] |
| 117 | + |
| 118 | + shift_2 = np.remainder(np.remainder(x-np.ceil(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 119 | + shift_Incr2 = (x0 - np.floor(x0))*args[shift_2.astype(int)] |
| 120 | + |
| 121 | + irf_shifted = (shift_Incr1 + shift_Incr2) |
| 122 | + irf_norm = irf_shifted/sum(irf_shifted) |
| 123 | + |
| 124 | + h = ampl1*np.exp(-(x)/tau1) + ampl2*np.exp(-(x)/tau2) + ampl3*np.exp(-(x)/tau3) |
| 125 | + hConvIrf_norm = convolveData(h, irf_norm) + y0 |
| 126 | + return hConvIrf_norm |
| 127 | + |
| 128 | +#4 component expontential distribution: |
| 129 | +def ExpDecay_4(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, ampl4, tau4, y0, x0, args=()): |
| 130 | + h = np.zeros(x.size) |
| 131 | + lengthVec = len(args) |
| 132 | + |
| 133 | + shift_1 = np.remainder(np.remainder(x-np.floor(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 134 | + shift_Incr1 = (1 - x0 + np.floor(x0))*args[shift_1.astype(int)] |
| 135 | + |
| 136 | + shift_2 = np.remainder(np.remainder(x-np.ceil(x0)-1, lengthVec) + lengthVec, lengthVec) |
| 137 | + shift_Incr2 = (x0 - np.floor(x0))*args[shift_2.astype(int)] |
| 138 | + |
| 139 | + irf_shifted = (shift_Incr1 + shift_Incr2) |
| 140 | + irf_norm = irf_shifted/sum(irf_shifted) |
| 141 | + |
| 142 | + h = ampl1*np.exp(-(x)/tau1) + ampl2*np.exp(-(x)/tau2) + ampl3*np.exp(-(x)/tau3) + ampl4*np.exp(-(x)/tau4) |
| 143 | + hConvIrf_norm = convolveData(h, irf_norm) + y0 |
| 144 | + return hConvIrf_norm |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments