<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Sep 22 20:27:04 2014

@author: <NAME>
"""

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future.builtins import (ascii, bytes, chr, dict, filter, hex, input,
                             int, map, next, oct, open, pow, range, round,
                             str, super, zip)
"""Python 3.x compatibility"""

import numpy as np
import scipy.optimize as opt


def identify_singularties(a, b, points, k, m, b_z_spl, b_theta_spl, offset,
                          tol):
    """
    Return list of singular points.

    Parameters
    ----------
    a : float
        radial start of pinch
    b : float
        radial end of pinch
    points : int
        number of points through which to divide f
    k : float
        axial periodicity number
    m : float
        azimuthal periodicity number
    b_z_spl : scipy spline object
        axial magnetic field
    b_theta_spl : scipy spline object
        azimuthal magnetic field

    Returns
    -------
    zero_positions: ndarray of floats (M)
        radial positions at which r equals zero.

    Notes
    -----
    Singular points are found by dividing f into intervals checking for sign
    changes and then running a zero funding method from the scipy optimize
    module.
    """
    params = (k, m, b_z_spl, b_theta_spl)
    r = np.linspace(a, b, points)
    zero_positions = []

    sign = np.sign(f_relevant_part(r, k, m, b_z_spl, b_theta_spl))
    for i in range(points-1):
        if np.allclose(sign[i] + sign[i+1], 0.):
            zero_pos = opt.brentq(f_relevant_part, r[i], r[i+1], args=params)
            zero = f_relevant_part(r[i], k, m, b_z_spl, b_theta_spl)
            if np.isnan(zero) or abs(zero) > 1e-2:
                continue
            else:
                zero_positions.append(zero_pos)

    sings = np.array(zero_positions)

    if not sings.size == 0 and sings[0] == 0.:
        sings_wo_0 = np.delete(sings, 0)
    else:
        sings_wo_0 = sings

    integration_points = np.insert((float(a), float(b)), 1, sings_wo_0)
    intervals = [[integration_points[i],
                  integration_points[i+1]]
                 for i in range(integration_points.size-1)]

    return sings, sings_wo_0, intervals


def f_relevant_part(r, k, m, b_z_spl, b_theta_spl):
    """
    Return relevant part of f for singularity detection.

    Parameters
    ----------<commit_msg>Replace with hardcoded value with variable, use numpy comparison.<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Sep 22 20:27:04 2014

@author: <NAME>
"""

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future.builtins import (ascii, bytes, chr, dict, filter, hex, input,
                             int, map, next, oct, open, pow, range, round,
                             str, super, zip)
"""Python 3.x compatibility"""

import numpy as np
import scipy.optimize as opt


def identify_singularties(a, b, points, k, m, b_z_spl, b_theta_spl, offset,
                          tol):
    """
    Return list of singular points.

    Parameters
    ----------
    a : float
        radial start of pinch
    b : float
        radial end of pinch
    points : int
        number of points through which to divide f
    k : float
        axial periodicity number
    m : float
        azimuthal periodicity number
    b_z_spl : scipy spline object
        axial magnetic field
    b_theta_spl : scipy spline object
        azimuthal magnetic field

    Returns
    -------
    zero_positions: ndarray of floats (M)
        radial positions at which r equals zero.

    Notes
    -----
    Singular points are found by dividing f into intervals checking for sign
    changes and then running a zero funding method from the scipy optimize
    module.
    """
    params = (k, m, b_z_spl, b_theta_spl)
    r = np.linspace(a, b, points)
    zero_positions = []

    sign = np.sign(f_relevant_part(r, k, m, b_z_spl, b_theta_spl))
    for i in range(points-1):
        if np.allclose(sign[i] + sign[i+1], 0.):
            zero_pos = opt.brentq(f_relevant_part, r[i], r[i+1], args=params)
            zero = f_relevant_part(r[i], k, m, b_z_spl, b_theta_spl)
            if np.isnan(zero) or abs(zero) > tol:
                continue
            else:
                zero_positions.append(zero_pos)

    sings = np.array(zero_positions)

    if not sings.size == 0 and np.allclose(sings[0], 0.):
        sings_wo_0 = np.delete(sings, 0)
    else:
        sings_wo_0 = sings

    integration_points = np.insert((float(a), float(b)), 1, sings_wo_0)
    intervals = [[integration_points[i],
                  integration_points[i+1]]
                 for i in range(integration_points.size-1)]

    return sings, sings_wo_0, intervals


def f_relevant_part(r, k, m, b_z_spl, b_theta_spl):
    """
    Return relevant part of f for singularity detection.

    Parameters
    ----------
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.011928429423459244, 'mean_word_length': 2.1983471074380163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '617352', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1550, 'n_words': 500}