<commit_before>#! /usr/bin/env python

"""
Module with cosmetics procedures. Contains the function for bad pixel fixing. 
Also functions for cropping cubes. 
"""

from __future__ import division, print_function

__author__ = '<NAME>, <NAME>'
__all__ = ['cube_crop_frames',
           'cube_drop_frames',
           'frame_crop',
           'cube_correct_nan',
           'approx_stellar_position']


import numpy as np
from astropy.stats import sigma_clipped_stats
from ..stats import sigma_filter
from ..var import frame_center, get_square


def cube_crop_frames(array, size, xy=None, force=False, verbose=True,
                     full_output=False):
    """Crops frames in a cube (3d or 4d array).
    
    Parameters
    ----------
    array : array_like 
        Input 3d or 4d array.
    size : int
        Size of the desired central sub-array in each frame, in pixels.
    xy : tuple of ints
        X, Y coordinates of new frame center. If you are getting the
        coordinates from ds9 subtract 1, python has 0-based indexing.
    force : bool, optional
        ``size`` and the original size of the frames must be both even or odd.
        With ``force`` set to True this condition can be avoided.
    verbose : bool optional
        If True message of completion is showed.
    full_output: bool optional
        If true, returns cenx and ceny in addition to array_view.

    Returns
    -------
    array_out : array_like
        Cube with cropped frames.
        
    """
    if array.ndim == 3:
        temp_fr = array[0]
    elif array.ndim == 4:
        temp_fr = array[0, 0]
    else:
        raise TypeError('`Array` is not a cube (3d or 4d numpy.ndarray)')

    if xy is not None:
        cenx, ceny = xy
    else:
        ceny, cenx = frame_center(temp_fr)
    _, y0, x0 = get_square(temp_fr, size, y=ceny, x=cenx, position=True,
                           force=force, verbose=verbose)

    if not force:
        if temp_fr.shape[0] % 2 == 0:
            if size % 2 != 0:
                size += 1
        else:
            if size % 2 == 0:
                size += 1
    y1 = int(y0 + size)
    x1 = int(x0 + size)

    if array.ndim == 3:
        array_out = array[:, y0:y1, x0:x1]
    elif array.ndim == 4:
        array_out = array[:, :, y0:y1, x0:x1]

    if verbose:
        msg = "New shape: {}"
        print(msg.format(array_out.shape))

    if full_output:
        return array_out, cenx, ceny
    else:
        return array_out


def frame_crop(array, size, cenxy=None, force=False, verbose=True):
    """ Crops a square frame (2d array). Uses the ``get_square`` function.

    Parameters
    ----------
    array : array_like
        Input frame.
    size : int, odd
        Size of the subframe.
    cenxy : tuple, optional
        Coordinates of the center of the subframe.
    force : bool, optional
        Size and the size of the 2d array must be both even or odd. With
        ``force`` set to True this condition can be avoided.
    verbose : bool optional
        If True, a message of completion is shown.
        
    Returns
    -------
    array_view : array_like
        Sub array.
        
    """
    if array.ndim != 2:
        raise TypeError('`Array` is not a frame or 2d array')

    if not cenxy:
        ceny, cenx = frame_center(array)
    else:
        cenx, ceny = cenxy
    array_view = get_square(array, size, ceny, cenx, force=force,
                            verbose=verbose)

    if verbose:
        msg = "New shape: {}"
        print(msg.format(array_view.shape))
    return array_view


def cube_drop_frames(array, n, m, parallactic=None, verbose=True):
    """Discards frames at the beginning or the end of a cube (axis 0).

    Parameters
    ----------
    array : array_like
        Input 3d array, cube.
    n : int
        Index of the first frame to be kept. Frames before this one are dropped.
    m : int
        Index of the last frame to be kept. Frames after this one are dropped.
    parallactic : 1d ndarray, optional
        parallactic angles vector. If provided, a modified copy of
        ``parallactic`` is returned additionally.

    Returns
    -------
    array_view : array_like
        Cube with new size (axis 0).
    parallactic : 1d array_like
        New parallactic angles. Only returned when the ``parallactic`` input
        parameter was provided.

    """
    if m > array.shape[0]:
        raise TypeError('End index must be smaller than the # of frames')

    array_view = array[n:m+1, :, :].copy()
    if parallactic is not None:
        if not parallactic.ndim == 1:
            raise ValueError('Parallactic angles vector has wrong shape')
        parallactic = parallactic[n+1:m+1]
<commit_msg>FIX: indices should be 1-based<commit_after>#! /usr/bin/env python

"""
Module with cosmetics procedures. Contains the function for bad pixel fixing. 
Also functions for cropping cubes. 
"""

from __future__ import division, print_function

__author__ = '<NAME>, <NAME>'
__all__ = ['cube_crop_frames',
           'cube_drop_frames',
           'frame_crop',
           'cube_correct_nan',
           'approx_stellar_position']


import numpy as np
from astropy.stats import sigma_clipped_stats
from ..stats import sigma_filter
from ..var import frame_center, get_square


def cube_crop_frames(array, size, xy=None, force=False, verbose=True,
                     full_output=False):
    """Crops frames in a cube (3d or 4d array).
    
    Parameters
    ----------
    array : array_like 
        Input 3d or 4d array.
    size : int
        Size of the desired central sub-array in each frame, in pixels.
    xy : tuple of ints
        X, Y coordinates of new frame center. If you are getting the
        coordinates from ds9 subtract 1, python has 0-based indexing.
    force : bool, optional
        ``size`` and the original size of the frames must be both even or odd.
        With ``force`` set to True this condition can be avoided.
    verbose : bool optional
        If True message of completion is showed.
    full_output: bool optional
        If true, returns cenx and ceny in addition to array_view.

    Returns
    -------
    array_out : array_like
        Cube with cropped frames.
        
    """
    if array.ndim == 3:
        temp_fr = array[0]
    elif array.ndim == 4:
        temp_fr = array[0, 0]
    else:
        raise TypeError('`Array` is not a cube (3d or 4d numpy.ndarray)')

    if xy is not None:
        cenx, ceny = xy
    else:
        ceny, cenx = frame_center(temp_fr)
    _, y0, x0 = get_square(temp_fr, size, y=ceny, x=cenx, position=True,
                           force=force, verbose=verbose)

    if not force:
        if temp_fr.shape[0] % 2 == 0:
            if size % 2 != 0:
                size += 1
        else:
            if size % 2 == 0:
                size += 1
    y1 = int(y0 + size)
    x1 = int(x0 + size)

    if array.ndim == 3:
        array_out = array[:, y0:y1, x0:x1]
    elif array.ndim == 4:
        array_out = array[:, :, y0:y1, x0:x1]

    if verbose:
        msg = "New shape: {}"
        print(msg.format(array_out.shape))

    if full_output:
        return array_out, cenx, ceny
    else:
        return array_out


def frame_crop(array, size, cenxy=None, force=False, verbose=True):
    """ Crops a square frame (2d array). Uses the ``get_square`` function.

    Parameters
    ----------
    array : array_like
        Input frame.
    size : int, odd
        Size of the subframe.
    cenxy : tuple, optional
        Coordinates of the center of the subframe.
    force : bool, optional
        Size and the size of the 2d array must be both even or odd. With
        ``force`` set to True this condition can be avoided.
    verbose : bool optional
        If True, a message of completion is shown.
        
    Returns
    -------
    array_view : array_like
        Sub array.
        
    """
    if array.ndim != 2:
        raise TypeError('`Array` is not a frame or 2d array')

    if not cenxy:
        ceny, cenx = frame_center(array)
    else:
        cenx, ceny = cenxy
    array_view = get_square(array, size, ceny, cenx, force=force,
                            verbose=verbose)

    if verbose:
        msg = "New shape: {}"
        print(msg.format(array_view.shape))
    return array_view


def cube_drop_frames(array, n, m, parallactic=None, verbose=True):
    """Discards frames at the beginning or the end of a cube (axis 0).

    Parameters
    ----------
    array : array_like
        Input 3d array, cube.
    n : int
        Index of the first frame to be kept. Frames before this one are dropped.
    m : int
        Index of the last frame to be kept. Frames after this one are dropped.
    parallactic : 1d ndarray, optional
        parallactic angles vector. If provided, a modified copy of
        ``parallactic`` is returned additionally.

    Returns
    -------
    array_view : array_like
        Cube with new size (axis 0).
    parallactic : 1d array_like
        New parallactic angles. Only returned when the ``parallactic`` input
        parameter was provided.

    """
    if m > array.shape[0]:
        raise TypeError('End index must be smaller than the # of frames')

    array_view = array[n-1:m, :, :].copy()
    if parallactic is not None:
        if not parallactic.ndim == 1:
            raise ValueError('Parallactic angles vector has wrong shape')
        parallactic = parallactic[n+1:m+1]

{'fraction_non_alphanumeric': 0.09417753738466433, 'fraction_numerical': 0.01134796903171068, 'mean_word_length': 2.0468497576736673, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2986272', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2841, 'n_words': 1040}