<commit_before>"""Functions and classes related to coordinate system conversion"""

import numpy as np


def _shapend(args, dims=None):
    """Helper to abstract out handling shape"""
    if len(args) is 1:  # received Nx2 or 2XN array
        coord = np.asarray(args[0])
        if not dims in coord.shape or len(coord.shape) > dims:
            raise ValueError('Must either be Nx2 or 2XN array.')
        return_array = True
        return_transpose = False
        if coord.ndim > 1 and \
                        coord.shape[0] is dims and \
                        coord.shape[1] is not dims:
            coord = np.transpose(coord)  # act on
            return_transpose = True
    elif len(args) is dims:  # received separate x, y
        return_array = False
        return_transpose = False
        if dims is 2:
            coord = np.array((args[0], args[1])).transpose()  # x,y
        elif dims is 3:
            coord = np.array((args[0], args[1], args[2])).transpose()  # x,y,z
    else:
        raise ValueError('Coordinates wrong.')
    return coord, return_array, return_transpose


def cart2pol(*args, **kwargs):
    """Convert cartesian coordinates to polar coordinates.

    Args:
        *args: Either separate x and y in a format coercible to a numpy array,
            or a 2 by N or N by 2 array, where each row or column is a [x, y] pair.

            If given a 2 by 2 array, we assume the each row is a [x, y] pair.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 2-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0).

    Returns:
        [theta, radius] pairs in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0))
    if len(ref) != 2:
        raise ValueError('Reference must be of length 2.')
    coord, return_array, return_transpose = _shapend(args, 2)
    coord -= ref
    if coord.ndim > 1:
        radius = np.hypot(coord[:, 0], coord[:, 1])
        theta = np.arctan2(coord[:, 1], coord[:, 0])
    else:
        radius = np.hypot(coord[0], coord[1])
        theta = np.arctan2(coord[1], coord[0])
    if units in ('deg', 'degs', 'degree', 'degrees'):
        theta *= 180 / np.pi
    if return_array:
        res = np.asarray((theta, radius))
        if return_transpose:
            return res
        return res.transpose()
    return theta, radius


def pol2cart(*args, **kwargs):
    """Convert polar coordinates to cartesian coordinates.

    Args:
        *args: Either separate theta and radius in a format coercible to a numpy array,
            or a 2 by N or N by 2 array, where each row or column is a [theta, radius] pair.

            If given a 2 by 2 array, we assume the each row is a [theta, radius] pair.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 2-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0).

    Returns:
        [x, y] pairs in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0))
    if len(ref) != 2:
        raise ValueError('Reference must be of length 2.')
    coord, return_array, return_transpose = _shapend(args, 2)
    if units in ('deg', 'degs', 'degree', 'degrees'):
        if coord.ndim > 1:
            coord[:, 0] *= np.pi / 180.0
        else:
            coord[0] *= np.pi / 180.0
    if coord.ndim > 1:
        xx = coord[:, 1] * np.cos(coord[:, 0]) + ref[0]
        yy = coord[:, 1] * np.sin(coord[:, 0]) + ref[1]
    else:
        xx = coord[1] * np.cos(coord[0]) + ref[0]
        yy = coord[1] * np.sin(coord[0]) + ref[1]
    if return_array:
        res = np.asarray((xx, yy))
        if return_transpose:
            return res
        return res.transpose()
    return xx, yy


def cart2sph(*args, **kwargs):
    """Convert cartesian coordinates to spherical coordinates.

    Args:
        *args: Either separate x, y, z in a format coercible to a numpy array,
            or a 3 by N or N by 3 array, where each row or column is a [x, y, z] trio.

            If given a 3 by 3 array, we assume the each row is a [x, y, z] trio.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 3-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0, 0).

    Returns:
        [elevation, azimuth, radius] trios in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0, 0))
    if len(ref) != 3:
        raise ValueError('Reference must be of length 3.')
    coord, return_array, return_transpose = _shapend(args, 3)
    coord -= ref
    if coord.ndim > 1:
        radius = np.sqrt((coord ** 2).sum(axis=1))
        azimuth = np.arctan2(coord[:, 1], coord[:, 0])
        elevation = np.arctan2(coord[:, 2], np.sqrt(coord[:, 0] ** 2 + coord[:, 1] ** 2))
    else:
        radius = np.sqrt((coord ** 2).sum())
        azimuth = np.arctan2(coord[1], coord[0])
        elevation = np.arctan2(coord[2], np.sqrt(coord[0] ** 2 + coord[1] ** 2))
    if units in ('deg', 'degs', 'degree', 'degrees'):
        azimuth *= 180.0 / np.pi
        elevation *= 180.0 / np.pi
    if return_array:
        sphere = np.asarray([elevation, azimuth, radius])<commit_msg>fixes for coordinate tools<commit_after>"""Functions and classes related to coordinate system conversion"""

import numpy as np


def _shapend(args, dims=None):
    """Helper to abstract out handling shape"""
    if len(args) is 1:  # received Nx2 or 2XN array
        output = np.array(args[0])
        if not dims in output.shape or len(output.shape) > dims:
            raise ValueError('Must either be Nx2 or 2XN array.')
        return_array = True
        return_transpose = False
        if output.ndim > 1 and \
                        output.shape[0] is dims and \
                        output.shape[1] is not dims:
            output = np.transpose(output)  # act on
            return_transpose = True
    elif len(args) is dims:  # received separate x, y
        return_array = False
        return_transpose = False
        output = np.asarray(args[:dims]).transpose()
        print(output)
    else:
        raise ValueError('Input has incorrect dimensions.')
    return output, return_array, return_transpose


def cart2pol(*args, **kwargs):
    """Convert cartesian coordinates to polar coordinates.

    Args:
        *args: Either separate x and y in a format coercible to a numpy array,
            or a 2 by N or N by 2 array, where each row or column is a [x, y] pair.

            If given a 2 by 2 array, we assume the each row is a [x, y] pair.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 2-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0).

    Returns:
        [theta, radius] pairs in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0))
    if len(ref) != 2:
        raise ValueError('Reference must be of length 2.')
    coord, return_array, return_transpose = _shapend(args, 2)
    if coord.ndim > 1:
        coord[:, 0] -= ref[0]
        coord[:, 1] -= ref[1]
        radius = np.hypot(coord[:, 0], coord[:, 1])
        theta = np.arctan2(coord[:, 1], coord[:, 0])
    else:
        coord[0] -= ref[0]
        coord[1] -= ref[1]
        radius = np.hypot(coord[0], coord[1])
        theta = np.arctan2(coord[1], coord[0])
    if units in ('deg', 'degs', 'degree', 'degrees'):
        theta *= 180.0 / np.pi
    if return_array:
        res = np.asarray((theta, radius))
        if return_transpose:
            return res
        return res.transpose()
    return theta, radius


def pol2cart(*args, **kwargs):
    """Convert polar coordinates to cartesian coordinates.

    Args:
        *args: Either separate theta and radius in a format coercible to a numpy array,
            or a 2 by N or N by 2 array, where each row or column is a [theta, radius] pair.

            If given a 2 by 2 array, we assume the each row is a [theta, radius] pair.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 2-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0).

    Returns:
        [x, y] pairs in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0))
    if len(ref) != 2:
        raise ValueError('Reference must be of length 2.')
    coord, return_array, return_transpose = _shapend(args, 2)
    if units in ('deg', 'degs', 'degree', 'degrees'):
        if coord.ndim > 1:
            coord[:, 0] *= np.pi / 180.0
        else:
            coord[0] *= np.pi / 180.0
    if coord.ndim > 1:
        xx = coord[:, 1] * np.cos(coord[:, 0]) + ref[0]
        yy = coord[:, 1] * np.sin(coord[:, 0]) + ref[1]
    else:
        xx = coord[1] * np.cos(coord[0]) + ref[0]
        yy = coord[1] * np.sin(coord[0]) + ref[1]
    if return_array:
        res = np.asarray((xx, yy))
        if return_transpose:
            return res
        return res.transpose()
    return xx, yy


def cart2sph(*args, **kwargs):
    """Convert cartesian coordinates to spherical coordinates.

    Args:
        *args: Either separate x, y, z in a format coercible to a numpy array,
            or a 3 by N or N by 3 array, where each row or column is a [x, y, z] trio.

            If given a 3 by 3 array, we assume the each row is a [x, y, z] trio.

        **kwargs:
            units: A string, used to specify either degrees or radians.
                Defaults to 'deg' for degrees.
            ref: A 3-element tuple or list or numpy array, which is the location of the reference
                point in cartesian coordinates. Defaults to (0, 0, 0).

    Returns:
        [elevation, azimuth, radius] trios in a format matching the input format.
    """
    units = kwargs.pop('units', 'deg')
    ref = kwargs.pop('ref', (0, 0, 0))
    if len(ref) != 3:
        raise ValueError('Reference must be of length 3.')
    coord, return_array, return_transpose = _shapend(args, 3)
    if coord.ndim > 1:
        coord[:, 0] -= ref[0]
        coord[:, 1] -= ref[1]
        coord[:, 2] -= ref[2]
        radius = np.sqrt((coord ** 2).sum(axis=1))
        azimuth = np.arctan2(coord[:, 1], coord[:, 0])
        elevation = np.arctan2(coord[:, 2], np.sqrt(coord[:, 0] ** 2 + coord[:, 1] ** 2))
    else:
        coord[0] -= ref[0]
        coord[1] -= ref[1]
        coord[2] -= ref[2]
        radius = np.sqrt((coord ** 2).sum())
        azimuth = np.arctan2(coord[1], coord[0])
        elevation = np.arctan2(coord[2], np.sqrt(coord[0] ** 2 + coord[1] ** 2))
    if units in ('deg', 'degs', 'degree', 'degrees'):
        azimuth *= 180.0 / np.pi
        elevation *= 180.0 / np.pi
    if return_array:
        sphere = np.asarray([elevation, azimuth, radius])
{'fraction_non_alphanumeric': 0.12235597592433362, 'fraction_numerical': 0.022699914015477215, 'mean_word_length': 2.115724618269488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1438988', 'n_tokens_mistral': 3782, 'n_tokens_neox': 3580, 'n_words': 1351}