<commit_before>"""The detectors module define classes to manipulate X-ray detectors.
"""
import os
import numpy as np
from matplotlib import pyplot as plt, cm, rcParams
from pymicro.file.file_utils import HST_read, HST_write
from pymicro.external.tifffile import TiffFile


class Detector2d:
    """Class to handle 2D detectors.

    2D detectors produce array like images and may have different geometries. In particular the pixel arrangement may
    not necessarily be flat or regular. This abstract class regroup the generic method for those kind of detectors.
    """

    def __init__(self, size=(2048, 2048), data_type=np.uint16):
        """
        Initialization of a Detector2d instance with a given image size (2048 pixels square array by default).
        The instance can be positioned in the laboratory frame using the ref_pos attribute which is the position in
        the laboratory frame of the middle of the detector.
        """
        self.size = size
        self.data_type = data_type
        self.ref_pos = np.array([0., 0., 0.])  # mm
        self.ucen = self.size[0] // 2
        self.vcen = self.size[1] // 2
        self.pixel_size = 1.  # mm
        self.calib = 1.  # pixel by degree
        self.mask_flag = 0  # use a mask
        self.mask_size_increase = 0
        self.image_path = None
        self.save_path = '.'
        self.correction = 'none'  # could be none, bg, flat
        self.orientation = 'horizontal'  # either 'horizontal' or 'vertical'

    def azimuthal_regroup(self, two_theta_mini=None, two_theta_maxi=None, two_theta_step=None,
                          psi_mask=None, psi_min=None, psi_max=None, write_txt=False,
                          output_image=False, debug=False):
        # assign default values if needed
        if not two_theta_mini:
            two_theta_mini = self.two_thetas.min()
        if not two_theta_maxi:
            two_theta_maxi = self.two_thetas.max()
        if not two_theta_step:
            two_theta_step = 1. / self.calib
        if (psi_mask is None) and (psi_min or psi_max):
            psi_mask = (self.psis > psi_min) & (self.psis < psi_max)
        n_bins = int((two_theta_maxi - two_theta_mini) / two_theta_step)
        print('* Azimuthal regroup (two theta binning)')
        print('  delta range = [%.1f-%.1f] with a %g deg step (%d bins)' % (
            two_theta_mini, two_theta_maxi, two_theta_step, n_bins))

        bin_edges = np.linspace(two_theta_mini, two_theta_maxi, 1 + n_bins)
        two_theta_values = bin_edges[:-1] + 0.5 * two_theta_step
        intensityResult = np.zeros(n_bins)  # this will be the summed intensity
        counts = np.zeros(n_bins)  # this will be the number of pixel contributing to each point

        # calculating bin indices for each pixel
        bin_id = np.floor((self.two_thetas - two_theta_mini) / two_theta_step).astype(np.int16)<commit_msg>added clear_data method and call it when creating a new detector<commit_after>"""The detectors module define classes to manipulate X-ray detectors.
"""
import os
import numpy as np
from matplotlib import pyplot as plt, cm, rcParams
from pymicro.file.file_utils import HST_read, HST_write
from pymicro.external.tifffile import TiffFile


class Detector2d:
    """Class to handle 2D detectors.

    2D detectors produce array like images and may have different geometries. In particular the pixel arrangement may
    not necessarily be flat or regular. This abstract class regroup the generic method for those kind of detectors.
    """

    def __init__(self, size=(2048, 2048), data_type=np.uint16):
        """
        Initialization of a Detector2d instance with a given image size (2048 pixels square array by default).
        The instance can be positioned in the laboratory frame using the ref_pos attribute which is the position in
        the laboratory frame of the middle of the detector.
        """
        self.size = size
        self.data_type = data_type
        self.data = np.zeros(self.size, dtype=self.data_type)
        self.ref_pos = np.array([0., 0., 0.])  # mm
        self.ucen = self.size[0] // 2
        self.vcen = self.size[1] // 2
        self.pixel_size = 1.  # mm
        self.calib = 1.  # pixel by degree
        self.mask_flag = 0  # use a mask
        self.mask_size_increase = 0
        self.image_path = None
        self.save_path = '.'
        self.correction = 'none'  # could be none, bg, flat
        self.orientation = 'horizontal'  # either 'horizontal' or 'vertical'

    def clear_data(self):
        """Simply set all pixels to zeros."""
        self.data = np.zeros(self.size, dtype=self.data_type)

    def azimuthal_regroup(self, two_theta_mini=None, two_theta_maxi=None, two_theta_step=None,
                          psi_mask=None, psi_min=None, psi_max=None, write_txt=False,
                          output_image=False, debug=False):
        # assign default values if needed
        if not two_theta_mini:
            two_theta_mini = self.two_thetas.min()
        if not two_theta_maxi:
            two_theta_maxi = self.two_thetas.max()
        if not two_theta_step:
            two_theta_step = 1. / self.calib
        if (psi_mask is None) and (psi_min or psi_max):
            psi_mask = (self.psis > psi_min) & (self.psis < psi_max)
        n_bins = int((two_theta_maxi - two_theta_mini) / two_theta_step)
        print('* Azimuthal regroup (two theta binning)')
        print('  delta range = [%.1f-%.1f] with a %g deg step (%d bins)' % (
            two_theta_mini, two_theta_maxi, two_theta_step, n_bins))

        bin_edges = np.linspace(two_theta_mini, two_theta_maxi, 1 + n_bins)
        two_theta_values = bin_edges[:-1] + 0.5 * two_theta_step
        intensityResult = np.zeros(n_bins)  # this will be the summed intensity
        counts = np.zeros(n_bins)  # this will be the number of pixel contributing to each point

        # calculating bin indices for each pixel
        bin_id = np.floor((self.two_thetas - two_theta_mini) / two_theta_step).astype(np.int16)
{'fraction_non_alphanumeric': 0.08629273149684699, 'fraction_numerical': 0.01261201460338533, 'mean_word_length': 2.606822262118492, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2328210', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1851, 'n_words': 579}