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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
# -*- coding: utf-8 -*-
#cython: embedsignature=True, language_level=3
#cython: boundscheck=False, wraparound=False, cdivision=True, initializedcheck=False,
################################################################################
# #This is for developping
# #cython: profile=True, warn.undeclared=True, warn.unused=True, warn.unused_result=False, warn.unused_arg=True
################################################################################
#
# Project: Fast Azimuthal integration
# https://github.com/silx-kit/pyFAI
#
# Copyright (C) 2011-2020 European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme 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.
"""
Contains a preprocessing function in charge of the dark-current subtraction,
flat-field normalization... taking care of masked values and normalization.
"""
__author__ = "Jerome Kieffer"
__license__ = "MIT"
__date__ = "29/04/2020"
__copyright__ = "2011-2020, ESRF"
__contact__ = "jerome.kieffer@esrf.fr"
include "regrid_common.pxi"
import cython
from cython.parallel import prange
from libc.math cimport fabs
from .isnan cimport isnan
from cython cimport floating
cdef floating[::1]c1_preproc(floating[::1] data,
floating[::1] dark=None,
floating[::1] flat=None,
floating[::1] solidangle=None,
floating[::1] polarization=None,
floating[::1] absorption=None,
any_int_t[::1] mask=None,
floating dummy=0.0,
floating delta_dummy=0.0,
bint check_dummy=False,
floating normalization_factor=1.0,
) with gil:
"""Common preprocessing step for all routines: C-implementation
:param data: raw value, as a numpy array, 1D or 2D
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param normalization_factor: final value is divided by this
NaN are always considered as invalid
if neither empty nor dummy is provided, empty pixels are 0
"""
cdef:
int size, i
bint check_mask, do_dark, do_flat, do_solidangle, do_absorption,
bint do_polarization, is_valid
floating[::1] result
floating one_num, one_den, one_flat
size = data.shape[0]
do_dark = dark is not None
do_flat = flat is not None
do_solidangle = solidangle is not None
do_absorption = absorption is not None
do_polarization = polarization is not None
check_mask = mask is not None
result = numpy.zeros(size, dtype=data.base.dtype)
for i in prange(size, nogil=True, schedule="static"):
one_num = data[i]
one_den = normalization_factor
is_valid = not isnan(one_num)
if is_valid and check_mask:
is_valid = (mask[i] == 0)
if is_valid and check_dummy:
if delta_dummy == 0:
is_valid = (one_num != dummy)
else:
is_valid = fabs(one_num - dummy) > delta_dummy
if is_valid and do_flat:
one_flat = flat[i]
if delta_dummy == 0:
is_valid = (one_flat != dummy)
else:
is_valid = fabs(one_flat - dummy) > delta_dummy
if is_valid:
# Do not use "/=" as they mean reduction for cython
if do_dark:
one_num = one_num - dark[i]
if do_flat:
one_den = one_den * one_flat
if do_polarization:
one_den = one_den * polarization[i]
if do_solidangle:
one_den = one_den * solidangle[i]
if do_absorption:
one_den = one_den * absorption[i]
if (isnan(one_num) or isnan(one_den) or (one_den == 0)):
result[i] += dummy
else:
result[i] += one_num / one_den
else:
result[i] += dummy
return result
cdef floating[:, ::1]c2_preproc(floating[::1] data,
floating[::1] dark=None,
floating[::1] flat=None,
floating[::1] solidangle=None,
floating[::1] polarization=None,
floating[::1] absorption=None,
any_int_t[::1] mask=None,
floating dummy=0,
floating delta_dummy=0,
bint check_dummy=False,
floating normalization_factor=1.0,
) with gil:
"""Common preprocessing step for all routines: C-implementation
with split_result without variance
:param data: raw value, as a numpy array, 1D or 2D
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param normalization_factor: final value is divided by this
NaN are always considered as invalid
Empty pixels are 0 both num and den
"""
cdef:
int size, i
bint check_mask, do_dark, do_flat, do_solidangle, do_absorption, do_polarization
bint is_valid
floating[:, ::1] result
floating one_num, one_flat, one_den
size = data.shape[0]
do_dark = dark is not None
do_flat = flat is not None
do_solidangle = solidangle is not None
do_absorption = absorption is not None
do_polarization = polarization is not None
check_mask = mask is not None
result = numpy.zeros((size, 2), dtype=data.base.dtype)
for i in prange(size, nogil=True, schedule="static"):
one_num = data[i]
one_den = normalization_factor
is_valid = not isnan(one_num)
if is_valid and check_mask:
is_valid = (mask[i] == 0)
if is_valid and check_dummy:
if delta_dummy == 0:
is_valid = (one_num != dummy)
else:
is_valid = fabs(one_num - dummy) > delta_dummy
if is_valid and do_flat:
one_flat = flat[i]
if delta_dummy == 0:
is_valid = (one_flat != dummy)
else:
is_valid = fabs(one_flat - dummy) > delta_dummy
if is_valid:
# Do not use "/=" as they mean reduction for cython
if do_dark:
one_num = one_num - dark[i]
if do_flat:
one_den = one_den * flat[i]
if do_polarization:
one_den = one_den * polarization[i]
if do_solidangle:
one_den = one_den * solidangle[i]
if do_absorption:
one_den = one_den * absorption[i]
if (isnan(one_num) or isnan(one_den) or (one_den == 0)):
one_num = 0.0
one_den = 0.0
else:
one_num = 0.0
one_den = 0.0
result[i, 0] += one_num
result[i, 1] += one_den
return result
cdef floating[:, ::1]c3_preproc(floating[::1] data,
floating[::1] dark=None,
floating[::1] flat=None,
floating[::1] solidangle=None,
floating[::1] polarization=None,
floating[::1] absorption=None,
any_int_t[::1] mask=None,
floating dummy=0.0,
floating delta_dummy=0.0,
bint check_dummy=False,
floating normalization_factor=1.0,
floating[::1] variance=None,
floating[::1] dark_variance=None,
bint poissonian=False,
) with gil:
"""Common preprocessing step for all routines: C-implementation
with split_result with variance in second position: (signal, variance, normalization)
:param data: raw value, as a numpy array, 1D or 2D
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param normalization_factor: final value is divided by this, settles on the denominator
:param variance: variance of the data
:param dark_variance: variance of the dark
:param poissonian: if True, the variance is the signal (minimum 1)
NaN are always considered as invalid
Empty pixels are 0.0 for both signal, variance and normalization
"""
cdef:
int size, i
bint check_mask, do_dark, do_flat, do_solidangle, do_absorption,
bint is_valid, do_polarization, do_variance, do_dark_variance
floating[:, ::1] result
floating one_num, one_flat, one_den, one_var
size = data.shape[0]
do_dark = dark is not None
do_flat = flat is not None
do_solidangle = solidangle is not None
do_absorption = absorption is not None
do_polarization = polarization is not None
check_mask = mask is not None
do_variance = variance is not None
do_dark_variance = dark_variance is not None
result = numpy.zeros((size, 3), dtype=data.base.dtype)
for i in prange(size, nogil=True, schedule="static"):
one_num = data[i]
one_den = normalization_factor
if poissonian:
one_var = max(one_num, 1.0)
elif do_variance:
one_var = variance[i]
else:
one_var = 0.0
is_valid = not isnan(one_num)
if is_valid and check_mask:
is_valid = (mask[i] == 0)
if is_valid and check_dummy:
if delta_dummy == 0:
is_valid = (one_num != dummy)
else:
is_valid = fabs(one_num - dummy) > delta_dummy
if is_valid and do_flat:
one_flat = flat[i]
if delta_dummy == 0:
is_valid = (one_flat != dummy)
else:
is_valid = fabs(one_flat - dummy) > delta_dummy
if is_valid:
# Do not use "/=" as they mean reduction for cython
if do_dark:
one_num = one_num - dark[i]
if do_dark_variance:
one_var = one_var + dark_variance[i]
if do_flat:
one_den = one_den * flat[i]
if do_polarization:
one_den = one_den * polarization[i]
if do_solidangle:
one_den = one_den * solidangle[i]
if do_absorption:
one_den = one_den * absorption[i]
if (isnan(one_num) or isnan(one_den) or isnan(one_var) or (one_den == 0)):
one_num = 0.0
one_var = 0.0
one_den = 0.0
else:
one_num = 0.0
one_var = 0.0
one_den = 0.0
result[i, 0] += one_num
result[i, 1] += one_var
result[i, 2] += one_den
return result
cdef floating[:, ::1]c4_preproc(floating[::1] data,
floating[::1] dark=None,
floating[::1] flat=None,
floating[::1] solidangle=None,
floating[::1] polarization=None,
floating[::1] absorption=None,
any_int_t[::1] mask=None,
floating dummy=0.0,
floating delta_dummy=0.0,
bint check_dummy=False,
floating normalization_factor=1.0,
floating[::1] variance=None,
floating[::1] dark_variance=None,
bint poissonian=False,
) with gil:
"""Common preprocessing step for all routines: C-implementation
with split_result to return (signal, variance, normalization, count)
:param data: raw value, as a numpy array, 1D or 2D
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param normalization_factor: final value is divided by this, settles on the denominator
:param variance: variance of the data
:param dark_variance: variance of the dark
:param poissonian: if True, the variance is the signal (minimum 1)
NaN are always considered as invalid
Empty pixels are 0.0 for both signal, variance, normalization and count
"""
cdef:
int size, i
bint check_mask, do_dark, do_flat, do_solidangle, do_absorption,
bint is_valid, do_polarization, do_variance, do_dark_variance
floating[:, ::1] result
floating one_num, one_flat, one_den, one_var, one_count
size = data.shape[0]
do_dark = dark is not None
do_flat = flat is not None
do_solidangle = solidangle is not None
do_absorption = absorption is not None
do_polarization = polarization is not None
check_mask = mask is not None
do_variance = variance is not None
do_dark_variance = dark_variance is not None
result = numpy.zeros((size, 4), data.base.dtype)
for i in prange(size, nogil=True, schedule="static"):
one_num = data[i]
one_den = normalization_factor
if poissonian:
one_var = max(one_num, 1.0)
elif do_variance:
one_var = variance[i]
else:
one_var = 0.0
is_valid = not isnan(one_num)
if is_valid and check_mask:
is_valid = (mask[i] == 0)
if is_valid and check_dummy:
if delta_dummy == 0:
is_valid = (one_num != dummy)
else:
is_valid = fabs(one_num - dummy) > delta_dummy
if is_valid and do_flat:
one_flat = flat[i]
if delta_dummy == 0:
is_valid = (one_flat != dummy)
else:
is_valid = fabs(one_flat - dummy) > delta_dummy
if is_valid:
# Do not use "/=" as they mean reduction for cython
if do_dark:
one_num = one_num - dark[i]
if do_dark_variance:
one_var = one_var + dark_variance[i]
if do_flat:
one_den = one_den * flat[i]
if do_polarization:
one_den = one_den * polarization[i]
if do_solidangle:
one_den = one_den * solidangle[i]
if do_absorption:
one_den = one_den * absorption[i]
if (isnan(one_num) or isnan(one_den) or isnan(one_var) or (one_den == 0)):
one_num = 0.0
one_var = 0.0
one_den = 0.0
one_count = 0.0
else:
one_count = 1.0
else:
one_num = 0.0
one_var = 0.0
one_den = 0.0
one_count = 0.0
result[i, 0] += one_num
result[i, 1] += one_var
result[i, 2] += one_den
result[i, 3] += one_count
return result
def _preproc(floating[::1] raw,
tuple shape,
bint check_dummy,
floating dummy,
floating delta_dummy,
floating normalization_factor,
floating[::1] dark=None,
floating[::1] flat=None,
floating[::1] solidangle=None,
floating[::1] polarization=None,
floating[::1] absorption=None,
any_int_t[::1] mask=None,
int split_result=0,
floating[::1] variance=None,
floating[::1] dark_variance=None,
bint poissonian=False,
):
"""specialized preprocessing step for all corrections
:param raw: raw value, as a numpy array, 1D with specialized dtype
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param normalization_factor: final value is divided by this
:param empty: value to be given for empty bins
:param variance: variance of the data
:param dark_variance: variance of the dark
:param poissonian: set to True to consider the variance is equal to raw signal (minimum 1)
All calculation are performed in the precision of raw dtype
NaN are always considered as invalid
"""
cdef:
floating[::1] res1d
floating[:, ::1] res2d
list out_shape
if split_result or (variance is not None) or poissonian:
out_shape = list(shape)
if split_result == 4:
out_shape += [4]
res2d = c4_preproc(raw, dark, flat, solidangle, polarization, absorption,
mask, dummy, delta_dummy, check_dummy, normalization_factor,
variance, dark_variance, poissonian)
elif (variance is not None) or poissonian:
out_shape += [3]
res2d = c3_preproc(raw, dark, flat, solidangle, polarization, absorption,
mask, dummy, delta_dummy, check_dummy, normalization_factor,
variance, dark_variance, poissonian)
else:
out_shape += [2]
res2d = c2_preproc(raw, dark, flat, solidangle, polarization, absorption,
mask, dummy, delta_dummy, check_dummy, normalization_factor)
res = numpy.asarray(res2d)
res.shape = out_shape
else:
res1d = c1_preproc(raw, dark, flat, solidangle, polarization, absorption,
mask, dummy, delta_dummy, check_dummy, normalization_factor)
res = numpy.asarray(res1d)
res.shape = shape
return res
def preproc(raw,
dark=None,
flat=None,
solidangle=None,
polarization=None,
absorption=None,
mask=None,
dummy=None,
delta_dummy=None,
normalization_factor=None,
empty=None,
split_result=False,
variance=None,
dark_variance=None,
bint poissonian=False,
dtype=numpy.float32
):
"""Common preprocessing step for all
:param raw: raw value, as a numpy array, 1D or 2D
:param mask: array non null where data should be ignored
:param dummy: value of invalid data
:param delta_dummy: precision for invalid data
:param dark: array containing the value of the dark noise, to be subtracted
:param flat: Array containing the flatfield image. It is also checked for dummies if relevant.
:param solidangle: the value of the solid_angle. This processing may be performed during the rebinning instead. left for compatibility
:param polarization: Correction for polarization of the incident beam
:param absorption: Correction for absorption in the sensor volume
:param normalization_factor: final value is divided by this
:param empty: value to be given for empty bins
:param variance: variance of the data
:param dark_variance: variance of the dark
:param poissonian: set to True to consider the variance is equal to raw signal (minimum 1)
:param dtype: type for working: float32 or float64
All calculation are performed in the `dtype` precision
NaN are always considered as invalid
if neither empty nor dummy is provided, empty pixels are 0
"""
cdef:
bint check_dummy
tuple shape
int size
shape = raw.shape
size = raw.size
raw = numpy.ascontiguousarray(raw.ravel(), dtype=dtype)
if dark is not None:
assert dark.size == size, "Dark array size is correct"
dark = numpy.ascontiguousarray(dark.ravel(), dtype=dtype)
if flat is not None:
assert flat.size == size, "Flat array size is correct"
flat = numpy.ascontiguousarray(flat.ravel(), dtype=dtype)
if polarization is not None:
assert polarization.size == size, "Polarization array size is correct"
polarization = numpy.ascontiguousarray(polarization.ravel(), dtype=dtype)
if solidangle is not None:
assert solidangle.size == size, "Solid angle array size is correct"
solidangle = numpy.ascontiguousarray(solidangle.ravel(), dtype=dtype)
if absorption is not None:
assert absorption.size == size, "Absorption array size is correct"
absorption = numpy.ascontiguousarray(absorption.ravel(), dtype=dtype)
if variance is not None:
assert variance.size == size, "Variance array size is correct"
variance = numpy.ascontiguousarray(variance.ravel(), dtype=dtype)
if dark_variance is not None:
assert dark_variance.size == size, "Dark_variance array size is correct"
dark_variance = numpy.ascontiguousarray(dark_variance.ravel(), dtype=dtype)
if (dummy is None):
check_dummy = False
dummy = delta_dummy = dtype(empty or 0.0)
else:
check_dummy = True
dummy = dtype(dummy)
if (delta_dummy is None):
delta_dummy = dtype(0.0)
else:
delta_dummy = dtype(delta_dummy)
if normalization_factor is not None:
normalization_factor = dtype(normalization_factor)
else:
normalization_factor = dtype(1.0)
if (mask is None) or (mask is False):
mask = None
else:
assert mask.size == size, "Mask array size is correct"
mask = numpy.ascontiguousarray(mask.ravel(), dtype=numpy.int8)
return _preproc(raw,
shape,
check_dummy,
dummy,
delta_dummy,
normalization_factor,
dark,
flat,
solidangle,
polarization,
absorption,
mask,
split_result,
variance,
dark_variance,
poissonian)
|