/usr/lib/python2.7/dist-packages/pyFAI/resources/openCL/sigma_clip.cl is in python-pyfai 0.15.0+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | /*
* Project: Azimuthal integration
* https://github.com/silx-kit/pyFAI
*
* Copyright (C) 2015-2018 European Synchrotron Radiation Facility, Grenoble, France
*
* Principal author: Jerome Kieffer (Jerome.Kieffer@ESRF.eu)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
// Functions to be called from an actual kernel.
// check for NANs and discard them, count the number of valid values
static inline float2 is_valid(float value, float count)
{
if (isfinite(value))
{
count += 1.0f;
}
else
{
value = 0.0f;
}
return (float2)(value, count);
}
// sum_vector return sum(x_i), err(sum(x_i)), sum(x_i^2 ), err(sum(x_i^2 ))
static float8 sum_vector(float8 data)
{
// implements Kahan summation:
// see https://en.wikipedia.org/wiki/Kahan_summation_algorithm
float2 tmp, sum1, sum2;
float value;
tmp = is_valid(data.s0, 0.0f);
value = tmp.s0;
sum1 = (float2)(value, 0.0f);
sum2 = (float2)(value * value, 0.0f);
tmp = is_valid(data.s1, tmp.s1);
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s2, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s3, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s4, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s5, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s6, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
tmp = is_valid(data.s7, tmp.s1);
value = tmp.s0;
value = tmp.s0;
sum1 = kahan_sum(sum1, value);
sum2 = kahan_sum(sum2, value*value);
return (float8)(sum1.s0, sum1.s1, sum2.s0, sum2.s1, tmp.s1, 0.0f, 0.0f, 0.0f);
}
// calculate the mean and the standard deviation sigma with reductions.
static float2 mean_and_deviation(uint local_id,
uint local_size,
float8 input,
local float *l_data)
{
// inspired from: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
float8 map = sum_vector(input);
l_data[local_id * 5 ] = map.s0;
l_data[local_id * 5 + 1] = map.s1;
l_data[local_id * 5 + 2] = map.s2;
l_data[local_id * 5 + 3] = map.s3;
l_data[local_id * 5 + 4] = map.s4;
uint stride_size = local_size / 2;
barrier(CLK_LOCAL_MEM_FENCE);
// Start parallel reduction
while (stride_size > 0)
{
if (local_id < stride_size)
{
float2 sum1, sum2;
int local_pos, remote_pos;
local_pos = 5 * local_id;
remote_pos = 5 * (local_id + stride_size);
sum1 = compensated_sum((float2)(l_data[local_pos], l_data[local_pos+1]),
(float2)(l_data[remote_pos], l_data[remote_pos + 1]));
sum2 = compensated_sum((float2)(l_data[local_pos + 2], l_data[local_pos+3]),
(float2)(l_data[remote_pos + 2], l_data[remote_pos + 3]));
l_data[local_pos] = sum1.s0;
l_data[local_pos + 1] = sum1.s1;
l_data[local_pos + 2] = sum2.s0;
l_data[local_pos + 3] = sum2.s1;
l_data[local_pos + 4] += l_data[remote_pos + 4];
}
barrier(CLK_LOCAL_MEM_FENCE);
stride_size /=2;
}
// Here we perform the Kahan summation for the variance
float std, mean, n;
n = l_data[4];
// if (local_id==0) printf("%.1f %.1f %.1f %.1f %.1f\n",l_data[0], l_data[1],l_data[2],l_data[3],l_data[4]);
// if (local_id==0) printf("(%d, %d) %.1f %.1f %.1f %.1f %.1f\n",get_global_id(0), get_global_id(1), l_data[0], l_data[1], l_data[2], l_data[3], l_data[4]);
if (fabs(n) < 0.5f)
{
mean = NAN;
std = NAN;
}
else
{
mean = l_data[0] / n;
float2 sum1, sum2, sum;
sum1 = (float2)(l_data[0], l_data[1]);
sum2 = (float2)(l_data[2], l_data[3]);
// here we perform the Kahan summation
// sigma**2 = (sum_x2 - (sum_x)**2/n )/n
sum = kahan_sum(sum2, -sum1.s0*sum1.s0/n);
sum = kahan_sum(sum, -sum1.s0*sum1.s1/n);
sum = kahan_sum(sum, -sum1.s1*sum1.s1/n);
std = sqrt(sum.s0/n);
}
return (float2) (mean, std);
}
static inline float8 clip8(float8 input, float2 mean_std,
float sigma_lo, float sigma_hi,
local int* discarded)
{
union
{
float array[8];
float8 vector;
} elements;
elements.vector = input;
for (int i=0; i<8; i++)
{
if (!isfinite(elements.array[i]) || mean_std.s1 == 0.0f)
{
elements.array[i] = NAN;
}
else
{
float ratio = (elements.array[i] - mean_std.s0) / mean_std.s1;
if (ratio > sigma_hi)
{
elements.array[i] = NAN;
atomic_inc(discarded);
}
else if (-ratio > sigma_lo)
{
elements.array[i] = NAN;
atomic_inc(discarded);
}
}
}
return elements.vector;
}
/*
mean_std_vertical calculate the mean and the standard deviation along a column,
vertical line.
:param src: 2D array of floats of size width*height
:param mean: 1D array of floats of size height
:param std: 1D array of floats of size height
:param width:
:param height:
:param dummy: value of the invalid data
Each workgroup works on a complete column, using subsequent reductions (sum) for
mean calculation and standard deviation
dim0 = y: wg=number_of_element/8
dim1 = x: wg=1
Shared memory: requires 5 floats (20 bytes) of shared memory per work-item
*/
kernel void mean_std_vertical(global float *src,
global float *mean,
global float *std,
float dummy,
local float *l_data
)
{
// we need to read 8 float position along the vertical axis
float8 input;
float2 result;
uint id, global_start, padding;
float value;
// Find global address
padding = get_global_size(1);
id = get_local_id(0) * 8 * padding + get_global_id(1);
global_start = get_group_id(0) * get_local_size(0) * 8 * padding + id;
value = src[global_start];
input.s0 = (value==dummy)?NAN:value;
value = src[global_start + padding];
input.s1 = (value==dummy)?NAN:value;
value = src[global_start + 2*padding];
input.s2 = (value==dummy)?NAN:value;
value = src[global_start + 3*padding];
input.s3 = (value==dummy)?NAN:value;
value = src[global_start + 4*padding];
input.s4 = (value==dummy)?NAN:value;
value = src[global_start + 5*padding];
input.s5 = (value==dummy)?NAN:value;
value = src[global_start + 6*padding];
input.s6 = (value==dummy)?NAN:value;
value = src[global_start + 7*padding];
input.s7 = (value==dummy)?NAN:value;
result = mean_and_deviation(get_local_id(0), get_local_size(0),
input, l_data);
if (get_local_id(0) == 0)
{
mean[get_global_id(1)] = result.s0;
std[get_global_id(1)] = result.s1;
}
}
/*
mean_std_horizontal calculate the mean and the standard deviation along a row,
horizontal line.
:param src: 2D array of floats of size width*height
:param mean: 1D array of floats of size height
:param std: 1D array of floats of size height
:param width:
:param height:
:param dummy: value of the invalid data
Each workgroup works on a complete row, using subsequent reductions (sum) for
mean calculation and standard deviation
dim0 = y: wg=1
dim1 = x: wg=number_of_element/8
Shared memory: requires 5 float (20 bytes) of shared memory per work-item
*/
kernel void mean_std_horizontal(global float *src,
global float *mean,
global float *std,
float dummy,
local float *l_data)
{
float8 input;
float2 result;
float value;
uint global_start, offset;
// Find global address
offset = get_global_size(1) * get_global_id(0) * 8;
global_start = offset + get_group_id(1) * get_local_size(1) * 8 + get_local_id(1) * 8;
value = src[global_start];
input.s0 = (value==dummy)?NAN:value;
value = src[global_start + 1];
input.s1 = (value==dummy)?NAN:value;
value = src[global_start + 2];
input.s2 = (value==dummy)?NAN:value;
value = src[global_start + 3];
input.s3 = (value==dummy)?NAN:value;
value = src[global_start + 4];
input.s4 = (value==dummy)?NAN:value;
value = src[global_start + 5];
input.s5 = (value==dummy)?NAN:value;
value = src[global_start + 6];
input.s6 = (value==dummy)?NAN:value;
value = src[global_start + 7];
input.s7 = (value==dummy)?NAN:value;
result = mean_and_deviation(get_local_id(1), get_local_size(1),
input, l_data);
if (get_local_id(1) == 0)
{
mean[get_global_id(0)] = result.s0;
std[get_global_id(0)] = result.s1;
}
}
/*
sigma_clip_vertical reject iteratively all point at n sigma from the mean along
a vertical line.
:param src: 2D array of floats of size width*height
:param mean: 1D array of floats of size width containing the mean of the array
along the vertical direction
:param mean: 1D array of floats of size width containing the standard deviation
of the array along the vertical direction
:param dummy: value of the invalid data
:param sigma_lo: lower cut-of for <I> - I > sigma_lo * sigma
:param sigma_hi: higher cut-of for I - <I> > sigma_hi * sigma
:param max_iter: Max number of iteration
Each workgroup works on a complete column, using subsequent reductions (sum) for
mean calculation and standard deviation
dim0 = y: wg=number_of_element/8
dim1 = x: wg=1
Shared memory: requires 5 floats (20 bytes) of shared memory per work-item
*/
kernel void sigma_clip_vertical(global float *src,
global float *mean,
global float *std,
float dummy,
float sigma_lo,
float sigma_hi,
int max_iter,
local float *l_data)
{
// we need to read 8 float position along the vertical axis
float8 input;
float2 result;
uint id, global_start, padding, i;
float value;
local int discarded[1];
// Find global address
padding = get_global_size(1);
id = get_local_id(0) * 8 * padding + get_global_id(1);
global_start = get_group_id(0) * get_local_size(0) * 8 * padding + id;
value = src[global_start];
input.s0 = (value==dummy)?NAN:value;
value = src[global_start + padding];
input.s1 = (value==dummy)?NAN:value;
value = src[global_start + 2*padding];
input.s2 = (value==dummy)?NAN:value;
value = src[global_start + 3*padding];
input.s3 = (value==dummy)?NAN:value;
value = src[global_start + 4*padding];
input.s4 = (value==dummy)?NAN:value;
value = src[global_start + 5*padding];
input.s5 = (value==dummy)?NAN:value;
value = src[global_start + 6*padding];
input.s6 = (value==dummy)?NAN:value;
value = src[global_start + 7*padding];
input.s7 = (value==dummy)?NAN:value;
result = mean_and_deviation(get_local_id(0), get_local_size(0),
input, l_data);
for (i=0; i<max_iter; i++)
{
if (get_local_id(0) == 0)
{
discarded[0] = 0;
}
barrier(CLK_LOCAL_MEM_FENCE);
input = clip8(input, result, sigma_lo, sigma_hi, discarded);
barrier(CLK_LOCAL_MEM_FENCE);
if (discarded[0] == 0){
break;
}
else
{
result = mean_and_deviation(get_local_id(0), get_local_size(0),
input, l_data);
}
}
if (get_local_id(0) == 0)
{
//printf("Discarded %d %d %d\n", get_global_id(1), i, discarded[0]);
mean[get_global_id(1)] = result.s0;
std[get_global_id(1)] = result.s1;
}
}
/*
sigma_clip_horizontal reject iteratively all point at n sigma from the mean along
a horizontal line.
:param src: 2D array of floats of size width*height
:param mean: 1D array of floats of size width containing the mean of the array
along the horizontal direction
:param mean: 1D array of floats of size width containing the standard deviation
of the array along the horizontal direction
:param dummy: value of the invalid data
:param sigma_lo: lower cut-of for <I> - I > sigma_lo * sigma
:param sigma_hi: higher cut-of for I - <I> > sigma_hi * sigma
:param max_iter: Max number of iteration
Each workgroup works on a complete column, using subsequent reductions (sum) for
mean calculation and standard deviation
dim0 = y: wg=1
dim1 = x: wg=number_of_element/8
Shared memory: requires 5 float (20 bytes) of shared memory per work-item
*/
kernel void sigma_clip_horizontal(global float *src,
global float *mean,
global float *std,
float dummy,
float sigma_lo,
float sigma_hi,
int max_iter,
local float *l_data)
{
// we need to read 8 float position along the vertical axis
float8 input;
float2 result;
float value;
uint global_start, offset, i;
local int discarded[1];
// Find global address
offset = get_global_size(1) * get_global_id(0) * 8;
global_start = offset + get_group_id(1) * get_local_size(1) * 8 + get_local_id(1) * 8;
value = src[global_start];
input.s0 = (value==dummy)?NAN:value;
value = src[global_start + 1];
input.s1 = (value==dummy)?NAN:value;
value = src[global_start + 2];
input.s2 = (value==dummy)?NAN:value;
value = src[global_start + 3];
input.s3 = (value==dummy)?NAN:value;
value = src[global_start + 4];
input.s4 = (value==dummy)?NAN:value;
value = src[global_start + 5];
input.s5 = (value==dummy)?NAN:value;
value = src[global_start + 6];
input.s6 = (value==dummy)?NAN:value;
value = src[global_start + 7];
input.s7 = (value==dummy)?NAN:value;
result = mean_and_deviation(get_local_id(1), get_local_size(1),
input, l_data);
for (i=0; i<max_iter; i++)
{
if (get_local_id(1) == 0)
{
discarded[0] = 0;
}
barrier(CLK_LOCAL_MEM_FENCE);
input = clip8(input, result, sigma_lo, sigma_hi, discarded);
barrier(CLK_LOCAL_MEM_FENCE);
if (discarded[0] == 0){
break;
}
else
{
result = mean_and_deviation(get_local_id(1), get_local_size(1),
input, l_data);
}
}
if (get_local_id(1) == 0)
{
// printf("Discarded (%d,%d) %d %d\n", get_global_id(0), get_global_id(1), i, discarded[0]);
mean[get_global_id(0)] = result.s0;
std[get_global_id(0)] = result.s1;
}
}
|