Coverage for modules/QF_utilities.py: 10%
201 statements
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
1"""
2Copyright 1999 Illinois Institute of Technology
4Permission is hereby granted, free of charge, to any person obtaining
5a copy of this software and associated documentation files (the
6"Software"), to deal in the Software without restriction, including
7without limitation the rights to use, copy, modify, merge, publish,
8distribute, sublicense, and/or sell copies of the Software, and to
9permit persons to whom the Software is furnished to do so, subject to
10the following conditions:
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL ILLINOIS INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY
19CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23Except as contained in this notice, the name of Illinois Institute
24of Technology shall not be used in advertising or otherwise to promote
25the sale, use or other dealings in this Software without prior written
26authorization from Illinois Institute of Technology.
27"""
29from numba import jit, cuda
30from math import exp, sqrt, floor, ceil, atan
31import numpy as np
33@jit(target_backend='cuda')
34def get_avg_fold_float32(quadrants, nQuadrant, fold_height, fold_width, threshold):
35 result = np.zeros((fold_height, fold_width))
36 if nQuadrant > 0:
37 for x in range(fold_width):
38 for y in range(fold_height):
39 sum_val = 0.0
40 n_fold = 0
41 for i in range(nQuadrant):
42 fold = quadrants[i]
43 if fold[y,x] > threshold :
44 sum_val += fold[y,x]
45 n_fold += 1
46 if n_fold == 0 :
47 result[y,x] = 0
48 else:
49 result[y,x] = sum_val/n_fold
50 return result
52@jit(target_backend='cuda')
53def createAngularBG(width, height, subtr, nBins):
54 backgound = np.zeros((height, width), dtype = np.float32)
55 centerX = width - 1
56 centerY = height - 1
57 theta_size = 90./nBins
59 for x in range(0, width):
60 for y in range(0, height):
61 rad = qfdistance(centerX, centerY, x, y)
62 floor_rad = floor(rad)
63 ceil_rad = ceil(rad)
64 ifloor_rad = int(floor_rad)
65 iceil_rad = int(ceil_rad)
67 if ifloor_rad == iceil_rad:
68 beta_rad = 0.5
69 alpha_rad = 0.5
70 else:
71 alpha_rad = 1. - (rad - floor_rad)
72 beta_rad = 1. - (ceil_rad - rad)
74 deltax = float(abs(x - centerX))
75 if deltax == 0.0:
76 deg = 90.0
77 else:
78 deltay = float(abs(y - centerY))
79 slope = deltay / deltax
80 deg = atan(slope)*180.0/np.pi
82 fbin = 1.*deg/theta_size
83 ibin = int(round(fbin))
85 if ibin == 0:
86 backgound[y, x] = alpha_rad*subtr[ibin, ifloor_rad] + beta_rad*subtr[ibin, iceil_rad]
87 elif ibin == nBins:
88 backgound[y, x] = alpha_rad*subtr[ibin-1, ifloor_rad] + beta_rad*subtr[ibin-1, iceil_rad]
89 else:
90 floor_bin = floor(fbin)
91 ceil_bin = ceil(fbin)
92 alpha = 1. - (fbin - floor_bin)
93 beta = 1. - (ceil_bin - fbin)
95 if alpha == 1.0 and beta == 1.0:
96 alpha = 0.5
97 beta = 0.5
98 ifloor = int(floor_bin - 1.0)
99 iceil = int(ceil_bin)
100 elif alpha > beta :
101 floor_bin = floor_bin - 0.5
102 ceil_bin = ceil_bin - 0.5
103 alpha = 1. - (fbin - floor_bin)
104 beta = 1. - (ceil_bin - fbin)
105 ifloor = int(floor(floor_bin))
106 iceil = int(floor(ceil_bin))
107 else:
108 floor_bin = floor_bin + 0.5
109 ceil_bin = ceil_bin + 0.5
110 alpha = 1. - (fbin - floor_bin)
111 beta = 1. - (ceil_bin - fbin)
112 ifloor = int(floor(floor_bin))
113 iceil = int(floor(ceil_bin))
115 backgound[y, x] = alpha * (alpha_rad * subtr[ifloor, ifloor_rad] + beta_rad * subtr[ifloor, iceil_rad])+ beta * (alpha_rad * subtr[iceil, ifloor_rad] + beta_rad * subtr[iceil, iceil_rad])
117 return backgound
119@jit(target_backend='cuda')
120def createCircularlySymBG(width, height, spline):
121 backgound = np.zeros((height, width), dtype = np.float32)
122 centerX = width - 0.5
123 centerY = height - 0.5
125 for x in range(width):
126 for y in range(height):
127 fx = float(x)
128 fy = float(y)
129 rad = sqrt((fx-centerX)**2+(fy-centerY)**2)
130 ffloor = floor(rad)
131 fceil = ceil(rad)
132 alpha = 1.-(rad-ffloor)
133 beta = 1.-(fceil-rad)
134 ifloor = int(ffloor)
135 iceil = int(fceil)
137 if ifloor == iceil:
138 alpha = 0.5
139 beta = 0.5
141 backgound[y, x] = alpha*spline[ifloor] + beta*spline[iceil]
142 return backgound
144@jit(target_backend='cuda')
145def replaceRmin(img, rmin, val):
146 height = img.shape[0]
147 width = img.shape[1]
148 centerX = width
149 centerY = height
150 frmin = float(rmin)
151 replace_val = float(val)
153 for x in range(width-rmin-1, width):
154 float_x = float(x)
155 for y in range(height-rmin-1, height):
156 float_y = float(y)
157 distance = sqrt((float_x-centerX)**2+(float_y-centerY)**2)
158 if distance <= frmin:
159 img[y, x] = replace_val
160 return img
162@jit(target_backend='cuda')
163def getCircularDiscreteBackground(img, rmin, start_p, end_p, radial_bin, nBin, max_pts):
164 height = img.shape[0]
165 width = img.shape[1]
166 xs = np.zeros(nBin, dtype = np.float32)
167 ys = np.zeros(nBin, dtype = np.float32)
168 all_pts = np.zeros(max_pts, dtype = np.float32)
169 centerX = width - 0.5
170 centerY = height - 0.5
171 nPoints = 0
173 for bin in range(0, nBin):
174 nPoints = 0
175 d1 = float(rmin + bin*radial_bin)
176 d2 = d1 + float(radial_bin)
178 # Get all points in a bin
179 for x in range(width):
180 float_x = float(x)
181 for y in range(height):
182 float_y = float(y)
183 distance = sqrt((float_x-centerX)**2+(float_y-centerY)**2)
184 if d1 <= distance and distance < d2 and nPoints < max_pts:
185 all_pts[nPoints] = img[y, x]
186 nPoints = nPoints+1
188 # Sort all pixels
189 sorted_pts = all_pts[:nPoints]
190 sorted_pts.sort()
192 # Get background value from all points (between percentage of start_p and end_p)
193 start_ind = int(round(float(nPoints)*start_p/100))
194 end_ind = int(round(float(nPoints)*end_p/100.))
196 if start_ind < end_ind:
197 sumVal = 0.0
198 for i in range(start_ind, end_ind):
199 sumVal = sumVal + sorted_pts[i]
200 ys[bin] = sumVal/float(end_ind-start_ind)
201 else:
202 ys[bin] = all_pts[start_ind]
204 xs[bin] = (d1+d2)/2.
205 return xs, ys
207@jit(target_backend='cuda')
208def make2DConvexhullBG2(pchipLines, width, height, centerX, centerY, rmin, rmax):
209 backgound = np.zeros((height, width), dtype = np.float32)
210 zero = 0.0
212 for x in range(width):
213 for y in range(height):
215 rad = qfdistance(centerX, centerY, x, y)
216 ceil_rad = ceil(rad)
217 floor_rad = floor(rad)
218 irad_ceil = int(ceil_rad)
219 irad_floor = int(floor_rad)
221 if irad_floor == irad_ceil:
222 beta_rad = 0.5
223 alpha_rad = 0.5
224 else:
225 alpha_rad = 1. - (rad - floor_rad)
226 beta_rad = 1. - (ceil_rad - rad)
228 deltax = float(abs(x - centerX))
229 if deltax == zero:
230 deg = 90.0
231 else:
232 deltay = float(abs(y - centerY))
233 slope = deltay / deltax
234 deg = atan(slope)*180.0/np.pi
236 floor_deg = floor(deg)
237 ceil_deg = ceil(deg)
238 ifloor = int(floor_deg)
239 iceil = int(ceil_deg)
241 if ifloor == iceil:
242 alpha = 0.5
243 beta = 0.5
244 else:
245 alpha = 1. - (deg - floor_deg)
246 beta = 1. - (ceil_deg - deg)
248 if irad_ceil < rmax and irad_floor >= rmin:
249 pos1 = irad_floor - rmin
250 pos2 = irad_ceil - rmin
251 backgound[y, x] = alpha * (alpha_rad * pchipLines[ifloor, pos1] + beta_rad * pchipLines[ifloor, pos2]) \
252 + beta * (alpha_rad * pchipLines[iceil, pos1] + beta_rad * pchipLines[iceil, pos2])
253 return backgound
255@jit(target_backend='cuda')
256def combine_bgsub_float32(img1, img2, center_x, center_y, sigmoid_k, radius):
257 img_height = img1.shape[0]
258 img_width = img1.shape[1]
259 result = np.zeros((img_height, img_width), dtype = np.float32)
261 for x in range(img_width):
262 for y in range(img_height):
263 r = qfdistance(x, y, center_x, center_y)
264 tophat_ratio = sigmoid(sigmoid_k, radius, r)
265 radial_ratio = 1.0 - tophat_ratio
266 tophat_val = tophat_ratio * img2[y,x]
267 radial_val = radial_ratio * img1[y,x]
268 result[y,x] = tophat_val+radial_val
269 return result
271@jit(target_backend='cuda')
272def qfdistance(x1, y1, x2, y2):
273 return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
275@jit(target_backend='cuda')
276def sigmoid(k, x0, x):
277 return 1.0 / (1.0 + exp(-k * (x - x0)))