<commit_before>import numpy as np
import peakutils
from Isotope_identification import Cs_134_g_e


def ROI_Maker(spectrum, energy,sub_regions='auto'):
    """
    Takes in a measured spectrum and identifies peaks in terms of channel number
    and energy. Uses peak_finder to localize the peak more precisely from the
    given energy. Also returns peak and compton ROIs.
    """

    peak_energy = peak_finder(spectrum, energy)

    E0 = spectrum.energy_cal[0]
    Eslope = spectrum.energy_cal[1]
    energy_channel = int((peak_energy - E0) / Eslope)

    region_size = 1.3
    compton_distance = 4

    # Rough estimate of FWHM.
    fwhm = 0.05*peak_energy**0.5

    region_width = int(region_size * (fwhm - E0) / Eslope)
    region_separation = int(compton_distance*region_width)

    peak_ch = (energy_channel - region_width, energy_channel + region_width)

    left_center = energy_channel - region_separation
    left_ch = (left_center - region_width, left_center + region_width)

    right_center = energy_channel + region_separation
    right_ch = (right_center - region_width, right_center + region_width)

    if sub_regions == 'auto':
        if energy == Cs_134_g_e[2]:
            sub_regions = 'Cs134'
        else:
            sub_regions = 'both'

    if sub_regions == 'left':
        side_region_list = [left_ch]
    elif sub_regions == 'right':
        side_region_list = [right_ch]
    elif sub_regions == 'Cs134':
        # Cs134 compton region using Bi214 609 peak.
        bi_fwhm = 0.05 * (609.31)**0.5
        bi_region_width = int(region_size * (bi_fwhm - E0) / Eslope)
        bi_region_separation = int(compton_distance*bi_region_width)
        bi_peak_channel = int((609.31 - E0) / Eslope)
        bi_right_peak = bi_peak_channel + bi_region_separation
        bi_right_ch = (bi_right_peak - region_width, bi_right_peak + region_width)
        side_region_list = [left_ch, bi_right_ch]
    elif sub_regions == 'none':
        side_region_list = []
    else:
        side_region_list = [left_ch, right_ch]

    return peak_ch, side_region_list


def peak_finder(spectrum, energy):<commit_msg>Updated ROI_Maker.py<commit_after>import numpy as np
import peakutils
from Isotope_identification import Cs_134_g_e


def ROI_Maker(spectrum, energy,sub_regions='auto'):
    """
    Takes in a measured spectrum and identifies peaks in terms of channel number
    and energy. Uses peak_finder to localize the peak more precisely from the
    given energy. Also returns peak and compton ROIs.
    """

    peak_energy = peak_finder(spectrum, energy)

    E0 = spectrum.energy_cal[0]
    Eslope = spectrum.energy_cal[1]
    energy_ch = int((peak_energy - E0) / Eslope)

    region_size = 1.3
    separation_parameter = 2

    # Rough estimate of FWHM.
    fwhm_kev = 0.05*peak_energy**0.5
    fwhm_ch = (fwhm_kev - E0)/Eslope

    region_half_width = int(region_size * fwhm_ch)
    region_separation = int(separation_parameter*region_half_width)

    peak_ch = (energy_ch - region_half_width, energy_ch + region_half_width)

    left_ch = (energy_ch - region_separation - 3*region_half_width,
               energy_ch - region_separation - region_half_width)

    right_ch = (energy_ch + region_separation + region_half_width,
                energy_ch + region_separation + 3*region_half_width)

    if sub_regions == 'auto':
        if energy == Cs_134_g_e[2]:
            sub_regions = 'Cs134'
        else:
            sub_regions = 'both'

    if sub_regions == 'left':
        side_region_list = [left_ch]
    elif sub_regions == 'right':
        side_region_list = [right_ch]
    elif sub_regions == 'Cs134':
        # Cs134 compton region using Bi214 609 peak.
        bi_fwhm_kev = 0.05 * (609.31)**0.5
        bi_fwhm_ch = (bi_fwhm_kev - E0)/Eslope
        bi_region_half_width = int(region_size * bi_fwhm_ch) 
        bi_region_separation = int(separation_parameter*bi_region_half_width)
        bi_energy_ch = int((609.31 - E0) / Eslope)
        bi_right_ch = (bi_energy_ch + bi_region_separation + bi_region_half_width,
                       bi_energy_ch + bi_region_separation + 3*bi_region_half_width)
        side_region_list = [left_ch, bi_right_ch]
    elif sub_regions == 'none':
        side_region_list = []
    else:
        side_region_list = [left_ch, right_ch]

    return peak_ch, side_region_list


def peak_finder(spectrum, energy):
{'fraction_non_alphanumeric': 0.08497030607583371, 'fraction_numerical': 0.024440383736866148, 'mean_word_length': 3.081081081081081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4171403', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1456, 'n_words': 329}