<commit_before># Licensed under an MIT open source license - see LICENSE

import numpy as np
import astropy.units as u
import networkx as nx
import networkx.algorithms.isomorphism as iso
import warnings
import scipy.ndimage as nd
from astropy.nddata import extract_array
import astropy.modeling as mod
from astropy.modeling.models import Gaussian1D, Const1D
import sys

if sys.version_info[0] >= 3:
    import _pickle as pickle
else:
    import cPickle as pickle

from .length import (init_lengths, main_length, make_final_skeletons,
                     pre_graph, longest_path, prune_graph)
from .pixel_ident import pix_identify
from .utilities import pad_image, in_ipynb, red_chisq
from .base_conversions import UnitConverter
from .rollinghough import rht
from .width import (radial_profile, gaussian_model, fit_radial_model,
                    nonparam_width)


class FilamentNDBase(object):
    """
    Analysis and properties of a single filament object.
    """
    @property
    def pixel_coords(self):
        return self._pixel_coords

    @property
    def pixel_extents(self):
        return [tuple([coord.min() for coord in self._orig_pixel_coords]),
                tuple([coord.max() for coord in self._orig_pixel_coords])]

    def position(self, world_coord=False):
        '''
        Return the centre position of the filament based on the pixel
        coordinates.
        '''
        centres = [np.median(coord) for coord in self._orig_pixel_coords]

        if world_coord:
            if hasattr(self._converter, '_wcs'):
                wcs = self._converter._wcs
                # Convert to world coordinates
                w_centres = wcs.all_pix2world(*centres, 0)

                # Attach units
                wu_centres = [val * u.Unit(wcs.wcs.cunit[i]) for i, val
                              in enumerate(w_centres)]
                return wu_centres
            else:
                warnings.warn("No WCS information given. Returning pixel"
                              " position.")
                return [centre * u.pix for centre in centres]
        else:
            return [centre * u.pix for centre in centres]


class Filament2D(FilamentNDBase):
    """
    Analysis and properties of a 2D filament.

    Parameters
    ----------
    pixel_coords : tuple of `~np.ndarray`
        Pixel coordinates as a set of arrays (i.e., the output from
        `~numpy.where`).
    converter : `~fil_finder.base_conversions.UnitConverter`, optional
        Unit converter class.
    wcs : `~astropy.wcs.WCS`, optional
        WCS information for the pixel set.
    distance : `~astropy.units.Quantity`, optional
        Distance to the region described by the pixel set. Requires for
        conversions to physical units.
    """
    def __init__(self, pixel_coords, converter=None, wcs=None, distance=None):<commit_msg>Fix unpacking arguments for older python versions<commit_after># Licensed under an MIT open source license - see LICENSE

import numpy as np
import astropy.units as u
import networkx as nx
import networkx.algorithms.isomorphism as iso
import warnings
import scipy.ndimage as nd
from astropy.nddata import extract_array
import astropy.modeling as mod
from astropy.modeling.models import Gaussian1D, Const1D
import sys

if sys.version_info[0] >= 3:
    import _pickle as pickle
else:
    import cPickle as pickle

from .length import (init_lengths, main_length, make_final_skeletons,
                     pre_graph, longest_path, prune_graph)
from .pixel_ident import pix_identify
from .utilities import pad_image, in_ipynb, red_chisq
from .base_conversions import UnitConverter
from .rollinghough import rht
from .width import (radial_profile, gaussian_model, fit_radial_model,
                    nonparam_width)


class FilamentNDBase(object):
    """
    Analysis and properties of a single filament object.
    """
    @property
    def pixel_coords(self):
        return self._pixel_coords

    @property
    def pixel_extents(self):
        return [tuple([coord.min() for coord in self._orig_pixel_coords]),
                tuple([coord.max() for coord in self._orig_pixel_coords])]

    def position(self, world_coord=False):
        '''
        Return the centre position of the filament based on the pixel
        coordinates.
        '''
        centres = [np.median(coord) for coord in self._orig_pixel_coords]

        if world_coord:
            if hasattr(self._converter, '_wcs'):
                wcs = self._converter._wcs
                # Convert to world coordinates
                posn_tuple = centres + [0]
                w_centres = wcs.all_pix2world(*posn_tuple)

                # Attach units
                wu_centres = [val * u.Unit(wcs.wcs.cunit[i]) for i, val
                              in enumerate(w_centres)]
                return wu_centres
            else:
                warnings.warn("No WCS information given. Returning pixel"
                              " position.")
                return [centre * u.pix for centre in centres]
        else:
            return [centre * u.pix for centre in centres]


class Filament2D(FilamentNDBase):
    """
    Analysis and properties of a 2D filament.

    Parameters
    ----------
    pixel_coords : tuple of `~np.ndarray`
        Pixel coordinates as a set of arrays (i.e., the output from
        `~numpy.where`).
    converter : `~fil_finder.base_conversions.UnitConverter`, optional
        Unit converter class.
    wcs : `~astropy.wcs.WCS`, optional
        WCS information for the pixel set.
    distance : `~astropy.units.Quantity`, optional
        Distance to the region described by the pixel set. Requires for
        conversions to physical units.
    """
    def __init__(self, pixel_coords, converter=None, wcs=None, distance=None):
{'fraction_non_alphanumeric': 0.07644521138912856, 'fraction_numerical': 0.0027610008628127696, 'mean_word_length': 2.4154390100176784, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3911633', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1619, 'n_words': 521}