basis_functions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python
  2. """
  3. This script is used to generate basis sources for the
  4. kCSD method Jan et.al (2012) for 1D,2D and 3D cases.
  5. Two 'types' are described here, gaussian and step source,
  6. These can be easily extended.
  7. These scripts are based on Grzegorz Parka's,
  8. Google Summer of Code 2014, INFC/pykCSD
  9. This was written by :
  10. Michal Czerwinski, Chaitanya Chintaluri
  11. Laboratory of Neuroinformatics,
  12. Nencki Institute of Experimental Biology, Warsaw.
  13. """
  14. from __future__ import division
  15. import numpy as np
  16. def gauss(d, stdev, dim):
  17. """Gaussian function
  18. Parameters
  19. ----------
  20. d : floats or np.arrays
  21. Distance array to the point of evaluation
  22. stdev : float
  23. cutoff range
  24. dim : int
  25. dimension of the gaussian function
  26. Returns
  27. -------
  28. Z : floats or np.arrays
  29. function evaluated
  30. """
  31. Z = np.exp(-(d**2) / (2* stdev**2) ) / (np.sqrt(2*np.pi)*stdev)**dim
  32. return Z
  33. def step_1D(d, R):
  34. """Returns normalized 1D step function.
  35. Parameters
  36. ----------
  37. d : floats or np.arrays
  38. Distance array to the point of evaluation
  39. R : float
  40. cutoff range
  41. Returns
  42. -------
  43. s : Value of the function (d <= R) / R
  44. """
  45. s = (d <= R)
  46. s = s / R #normalize with width
  47. return s
  48. def gauss_1D(d, three_stdev):
  49. """Returns normalized gaussian 2D scale function
  50. Parameters
  51. ----------
  52. d : floats or np.arrays
  53. Distance array to the point of evaluation
  54. three_stdev : float
  55. 3 * standard deviation of the distribution
  56. Returns
  57. -------
  58. Z : (three_std/3)*(1/2*pi)*(exp(-0.5)*stddev**(-2) *(d**2))
  59. """
  60. stdev = three_stdev/3.0
  61. Z = gauss(d, stdev, 1)
  62. return Z
  63. def gauss_lim_1D(d, three_stdev):
  64. """Returns gausian 2D function cut off after 3 standard deviations.
  65. Parameters
  66. ----------
  67. d : floats or np.arrays
  68. Distance array to the point of evaluation
  69. three_stdev : float
  70. 3 * standard deviation of the distribution
  71. Returns
  72. -------
  73. Z : (three_std/3)*(1/2*pi)*(exp(-0.5)*stddev**(-2) *((x-mu)**2)),
  74. cut off = three_stdev
  75. """
  76. Z = gauss_1D(d, three_stdev)
  77. Z *= (d < three_stdev)
  78. return Z
  79. def step_2D(d, R):
  80. """Returns normalized 2D step function.
  81. Parameters
  82. ----------
  83. d : float or np.arrays
  84. Distance array to the point of evaluation
  85. R : float
  86. cutoff range
  87. Returns
  88. -------
  89. s : step function
  90. """
  91. s = (d <= R) / (np.pi*(R**2))
  92. return s
  93. def gauss_2D(d, three_stdev):
  94. """Returns normalized gaussian 2D scale function
  95. Parameters
  96. ----------
  97. d : floats or np.arrays
  98. distance at which we need the function evaluated
  99. three_stdev : float
  100. 3 * standard deviation of the distribution
  101. Returns
  102. -------
  103. Z : function
  104. Normalized gaussian 2D function
  105. """
  106. stdev = three_stdev/3.0
  107. Z = gauss(d, stdev, 2)
  108. return Z
  109. def gauss_lim_2D(d, three_stdev):
  110. """Returns gausian 2D function cut off after 3 standard deviations.
  111. Parameters
  112. ----------
  113. d : floats or np.arrays
  114. distance at which we need the function evaluated
  115. three_stdev : float
  116. 3 * standard deviation of the distribution
  117. Returns
  118. -------
  119. Z : function
  120. Normalized gaussian 2D function cut off after three_stdev
  121. """
  122. Z = (d <= three_stdev)*gauss_2D(d, three_stdev)
  123. return Z
  124. def gauss_3D(d, three_stdev):
  125. """Returns normalized gaussian 3D scale function
  126. Parameters
  127. ----------
  128. d : floats or np.arrays
  129. distance at which we need the function evaluated
  130. three_stdev : float
  131. 3 * standard deviation of the distribution
  132. Returns
  133. -------
  134. Z : funtion
  135. Normalized gaussian 3D function
  136. """
  137. stdev = three_stdev/3.0
  138. Z = gauss(d, stdev, 3)
  139. return Z
  140. def gauss_lim_3D(d, three_stdev):
  141. """Returns normalized gaussian 3D scale function cut off after 3stdev
  142. Parameters
  143. ----------
  144. d : floats or np.arrays
  145. distance at which we need the function evaluated
  146. three_stdev : float
  147. 3 * standard deviation of the distribution
  148. Returns
  149. -------
  150. Z : funtion
  151. Normalized gaussian 3D function cutoff three_Stdev
  152. """
  153. Z = gauss_3D(d, three_stdev)
  154. Z = Z * (d < (three_stdev))
  155. return Z
  156. def step_3D(d, R):
  157. """Returns normalized 3D step function.
  158. Parameters
  159. ----------
  160. d : floats or np.arrays
  161. distance at which we need the function evaluated
  162. R : float
  163. cutoff range
  164. Returns
  165. -------
  166. s : step function in 3D
  167. """
  168. s = 3/(4*np.pi*R**3)*(d <= R)
  169. return s
  170. basis_1D = {
  171. "step": step_1D,
  172. "gauss": gauss_1D,
  173. "gauss_lim": gauss_lim_1D,
  174. }
  175. basis_2D = {
  176. "step": step_2D,
  177. "gauss": gauss_2D,
  178. "gauss_lim": gauss_lim_2D,
  179. }
  180. basis_3D = {
  181. "step": step_3D,
  182. "gauss": gauss_3D,
  183. "gauss_lim": gauss_lim_3D,
  184. }