diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py index c2c738e9a61429c2097e107ef99bbc019fa29666..e797a3bdea662b5ef7fa47c01dbf9f824f52073d 100644 --- a/cal_tools/cal_tools/agipdlib.py +++ b/cal_tools/cal_tools/agipdlib.py @@ -5,6 +5,8 @@ from typing import Any, Dict, Optional, Tuple import h5py import numpy as np import sharedmem +from iCalibrationDB import Conditions, Constants + from cal_tools.agipdutils import (assemble_constant_dict, baseline_correct_via_noise, baseline_correct_via_stripe, @@ -12,11 +14,9 @@ from cal_tools.agipdutils import (assemble_constant_dict, correct_baseline_via_hist_asic, make_noisy_adc_mask, match_asic_borders, melt_snowy_pixels) +from cal_tools.cython import agipdalgs as calgs from cal_tools.enums import BadPixels, SnowResolution from cal_tools.tools import get_constant_from_db_and_time -from iCalibrationDB import Conditions, Constants - -from cal_tools.cython import agipdalgs as calgs def get_num_cells(fname, loc, module): @@ -465,7 +465,7 @@ class AgipdCorrections: for c in cells: std = np.nanstd(data[cellid == c, ...], axis=0) - mask_std[:, c, std == 0] |= BadPixels.DATA_STD_IS_ZERO.value + mask_std[:, c, std == 0] |= BadPixels.DATA_STD_IS_ZERO def offset_correction(self, i_proc: int, first: int, last: int): """ @@ -664,7 +664,7 @@ class AgipdCorrections: if self.corr_bools.get("zero_nans"): bidx = ~np.isfinite(data) data[bidx] = 0 - msk[bidx] |= BadPixels.VALUE_IS_NAN.value + msk[bidx] |= BadPixels.VALUE_IS_NAN del bidx # Add pixels with unrealistically high and low values to the mask. @@ -672,7 +672,7 @@ class AgipdCorrections: if self.corr_bools.get("zero_orange"): bidx = (data < -1e7) | (data > 1e7) data[bidx] = 0 - msk[bidx] |= BadPixels.VALUE_OUT_OF_RANGE.value + msk[bidx] |= BadPixels.VALUE_OUT_OF_RANGE del bidx # Mask entire ADC if they are noise above a threshold diff --git a/cal_tools/cal_tools/agipdutils.py b/cal_tools/cal_tools/agipdutils.py index 85bc81bb3d8e60e1104da6b9e792fd0aea464b3c..ae06e49bf9d661a6cc90265b445c93860bfee030 100644 --- a/cal_tools/cal_tools/agipdutils.py +++ b/cal_tools/cal_tools/agipdutils.py @@ -2,11 +2,12 @@ import copy from typing import Tuple import numpy as np -from cal_tools.enums import BadPixels, SnowResolution from scipy.signal import cwt, ricker from sklearn.mixture import GaussianMixture from sklearn.preprocessing import StandardScaler +from cal_tools.enums import BadPixels, SnowResolution + def assemble_constant_dict(corr_bools, pc_bools, memory_cells, bias_voltage, gain_setting, acquisition_rate, @@ -429,8 +430,8 @@ def make_noisy_adc_mask(bmask, noise_threshold): """ Mask entire ADC if they are noise above a relative threshold """ msk = np.count_nonzero( - ((bmask & BadPixels.NOISE_OUT_OF_THRESHOLD.value != 0) - | (bmask & BadPixels.OFFSET_NOISE_EVAL_ERROR.value != 0)).astype( + ((bmask & BadPixels.NOISE_OUT_OF_THRESHOLD != 0) + | (bmask & BadPixels.OFFSET_NOISE_EVAL_ERROR != 0)).astype( np.int), axis=0) fmask = np.zeros_like(msk).astype(np.uint32) @@ -443,7 +444,7 @@ def make_noisy_adc_mask(bmask, noise_threshold): bmask.shape[0] // 32) if adc_cnt / (adc_i * adc_j) >= noise_threshold: fmask[i * adc_i:(i + 1) * adc_i, - j * adc_j:(j + 1) * adc_j] = BadPixels.NOISY_ADC.value + j * adc_j:(j + 1) * adc_j] = BadPixels.NOISY_ADC return fmask @@ -633,7 +634,7 @@ def melt_snowy_pixels(raw, im, gain, rgain, resolution=None): mim[labels == implabel] = mimb[labels == implabel] mimg[labels == implabel] = 0 # identify these pixels in a bad pixel mask - mmsk[labels == implabel] = BadPixels.INTERPOLATED.value + mmsk[labels == implabel] = BadPixels.INTERPOLATED # now set back to data asic[fidx] = mim diff --git a/cal_tools/cal_tools/agipdutils_ff.py b/cal_tools/cal_tools/agipdutils_ff.py index 97d38ce87718cc5759ac807fd5a506decdffa02d..6774e5a0e85a619796e8d1c510e3a5a8b52852bf 100644 --- a/cal_tools/cal_tools/agipdutils_ff.py +++ b/cal_tools/cal_tools/agipdutils_ff.py @@ -1,9 +1,10 @@ from typing import Any, Dict, List, Optional, Tuple import numpy as np -from cal_tools.enums import BadPixelsFF from iminuit import Minuit +from cal_tools.enums import BadPixelsFF + def any_in(mask: np.ndarray, bits: int) -> bool: return mask.astype(np.uint) & bits > 0 @@ -204,7 +205,7 @@ def get_mask(fit_summary: Dict[str, Any], :return: Bad pixel mask """ if not fit_summary['is_valid']: - return BadPixelsFF.FIT_FAILED.value + return BadPixelsFF.FIT_FAILED m0 = fit_summary['g0mean'] s0 = fit_summary['g0sigma'] @@ -215,19 +216,19 @@ def get_mask(fit_summary: Dict[str, Any], mask = 0 if not fit_summary['is_valid']: - mask |= BadPixelsFF.FIT_FAILED.value + mask |= BadPixelsFF.FIT_FAILED if not fit_summary['has_accurate_covar']: - mask |= BadPixelsFF.ACCURATE_COVAR.value + mask |= BadPixelsFF.ACCURATE_COVAR if not peak_lim[0] <= m0 <= peak_lim[1]: - mask |= BadPixelsFF.NOISE_PEAK_THRESHOLD.value + mask |= BadPixelsFF.NOISE_PEAK_THRESHOLD if not d0_lim[0] <= d01 <= d0_lim[1]: - mask |= BadPixelsFF.GAIN_THRESHOLD.value + mask |= BadPixelsFF.GAIN_THRESHOLD if not chi2_lim[0] <= chi2_ndof <= chi2_lim[1]: - mask |= BadPixelsFF.CHI2_THRESHOLD.value + mask |= BadPixelsFF.CHI2_THRESHOLD width_lim = peak_width_lim[0] * s0 inside_s1 = width_lim[0] <= s1 <= width_lim[1] @@ -236,6 +237,6 @@ def get_mask(fit_summary: Dict[str, Any], inside_s2 = width_lim[0] <= s2 <= width_lim[1] if not inside_s1 and inside_s2: - mask |= BadPixelsFF.PEAK_WIDTH_THRESHOLD.value + mask |= BadPixelsFF.PEAK_WIDTH_THRESHOLD return mask diff --git a/cal_tools/cal_tools/dssclib.py b/cal_tools/cal_tools/dssclib.py index 7e17f2011471f185e39f38fedcfcfab0cb25523d..8eabb3f42e4b3ae7be67ea39941ee38c5c408186 100644 --- a/cal_tools/cal_tools/dssclib.py +++ b/cal_tools/cal_tools/dssclib.py @@ -1,12 +1,13 @@ import binascii -import h5py -from hashlib import blake2b -import numpy as np import os import re import struct +from hashlib import blake2b from typing import Dict, Tuple +import h5py +import numpy as np + def get_pulseid_checksum(fname, h5path, h5path_idx): """generates hash value from pulse pattern (veto defined).""" diff --git a/cal_tools/cal_tools/enums.py b/cal_tools/cal_tools/enums.py index 19420caeacf38580e808ee0da4a76c508e29e464..4abafdc10b646d7d81c92de9912f8b29f27f1e13 100644 --- a/cal_tools/cal_tools/enums.py +++ b/cal_tools/cal_tools/enums.py @@ -1,7 +1,7 @@ -from enum import Enum +from enum import Enum, IntFlag -class BadPixels(Enum): +class BadPixels(IntFlag): """ The European XFEL Bad Pixel Encoding """ @@ -28,7 +28,7 @@ class BadPixels(Enum): NON_LIN_RESPONSE_REGION = 0b100000000000000000000 # bit 21 -class BadPixelsFF(Enum): +class BadPixelsFF(IntFlag): """ The SLopesFF Bad Pixel Encoding """ diff --git a/cal_tools/cal_tools/lpdlib.py b/cal_tools/cal_tools/lpdlib.py index 716637a5261cbfddeb752758290644c0c1707c0c..217c769fcc6faef919697b2b671a1c9e3a65c488 100644 --- a/cal_tools/cal_tools/lpdlib.py +++ b/cal_tools/cal_tools/lpdlib.py @@ -3,11 +3,10 @@ from typing import Optional, Tuple import h5py import numpy as np +from iCalibrationDB import Conditions, Constants, Detectors from cal_tools.enums import BadPixels -from cal_tools.tools import (get_constant_from_db, - get_constant_from_db_and_time) -from iCalibrationDB import Conditions, Constants, Detectors +from cal_tools.tools import get_constant_from_db, get_constant_from_db_and_time class LpdCorrections: @@ -267,7 +266,7 @@ class LpdCorrections: for gl, lr in enumerate(self.linear_between): midx = (gain == gl) & ((im < lr[0]) | (im > lr[1])) - msk[midx] = BadPixels.NON_LIN_RESPONSE_REGION.value + msk[midx] = BadPixels.NON_LIN_RESPONSE_REGION numnonlin = np.count_nonzero(midx, axis=(1,2)) nlf += numnonlin nlf = nlf/float(im.shape[0] * im.shape[1]) @@ -314,12 +313,12 @@ class LpdCorrections: # create bad pixels masks, here non-finite values bidx = ~np.isfinite(im) im[bidx] = 0 - msk[bidx] = BadPixels.VALUE_IS_NAN.value + msk[bidx] = BadPixels.VALUE_IS_NAN # values which are unphysically large or small bidx = (im < -1e7) | (im > 1e7) im[bidx] = 0 - msk[bidx] = BadPixels.VALUE_OUT_OF_RANGE.value + msk[bidx] = BadPixels.VALUE_OUT_OF_RANGE # on first iteration we create histograms for the report if cidx == 0: diff --git a/cal_tools/cal_tools/pnccdlib.py b/cal_tools/cal_tools/pnccdlib.py index ec8b92e7f65dfb21a09fa099df3a749d55833fc7..785d289c359ffe6d3e62aa15bb6e41de56d4145d 100644 --- a/cal_tools/cal_tools/pnccdlib.py +++ b/cal_tools/cal_tools/pnccdlib.py @@ -5,7 +5,6 @@ from typing import Tuple import h5py - VALID_GAINS = { "a" : 1.0, "b" : 4.0,