Section

class astrodata.Section(*args, **kwargs)[source]

Bases: tuple

A class to handle n-dimensional sections.

Instantiate a new Section object.

Expects a sequence of pairs of start and end coordinates for each axis. This can be passed in order of axis (e.g., x1, x2, y1, y2) or as a set of keyword arguments (e.g., x1=0, x2=10, y1=0, y2=10).

Parameters:
  • x1 (int) –

    The start and end coordinates for each axis. If passed as positional arguments, they should be in order of axis. Otherwise, they can be passed as keyword arguments, such as:

    section = Section(x1=0, x2=10, y1=0, y2=10)
    

  • x2 (int) –

    The start and end coordinates for each axis. If passed as positional arguments, they should be in order of axis. Otherwise, they can be passed as keyword arguments, such as:

    section = Section(x1=0, x2=10, y1=0, y2=10)
    

  • y1 (int) –

    The start and end coordinates for each axis. If passed as positional arguments, they should be in order of axis. Otherwise, they can be passed as keyword arguments, such as:

    section = Section(x1=0, x2=10, y1=0, y2=10)
    

  • y2 (int) –

    The start and end coordinates for each axis. If passed as positional arguments, they should be in order of axis. Otherwise, they can be passed as keyword arguments, such as:

    section = Section(x1=0, x2=10, y1=0, y2=10)
    

  • ... (int) –

    The start and end coordinates for each axis. If passed as positional arguments, they should be in order of axis. Otherwise, they can be passed as keyword arguments, such as:

    section = Section(x1=0, x2=10, y1=0, y2=10)
    

Attributes Summary

axis_dict

Return a dictionary with the axis names as keys.

ndim

The number of dimensions in the section.

Methods Summary

asIRAFsection()

Produce string with '[x1:x2,y1:y2]' 1-indexed and end-inclusive.

as_iraf_section()

Produce string with '[x1:x2,y1:y2]' 1-indexed and end-inclusive.

asslice([add_dims])

Return the Section object as a slice/list of slices.

contains(section)

Return True if the section is entirely within this Section.

from_shape(value)

Produce a Section object defining a given shape.

from_string(value)

Produce a Section object from a string.

is_same_size(section)

Return True if the Sections are the same size, otherwise False.

overlap(section)

Return the overlap between two sections, or None if no overlap.

shift(*shifts)

Shift a section in each direction by the specified amount.

Attributes Documentation

axis_dict

Return a dictionary with the axis names as keys.

ndim

The number of dimensions in the section.

Methods Documentation

asIRAFsection()[source]

Produce string with ‘[x1:x2,y1:y2]’ 1-indexed and end-inclusive.

Deprecated, see as_iraf_section().

as_iraf_section()[source]

Produce string with ‘[x1:x2,y1:y2]’ 1-indexed and end-inclusive.

This is the format used by IRAF for sections.

For example, >>> Section(0, 10, 0, 10).as_iraf_section() ‘[1:10,1:10]’

asslice(add_dims=0)[source]

Return the Section object as a slice/list of slices.

Higher dimensionality can be achieved with the add_dims parameter.

Parameters:

add_dims (int) – The number of dimensions to add to the slice.

contains(section)[source]

Return True if the section is entirely within this Section.

Parameters:

section (Section) – The Section to check for containment.

Returns:

True if the Section is entirely within this Section, otherwise False.

Return type:

bool

Raises:

ValueError – If the Sections have different dimensionality.

Examples

>>> Section(0, 10, 0, 10).contains(Section(1, 9, 1, 9))
True
>>> Section(0, 10, 0, 10).contains(Section(1, 11, 1, 9))
False
>>> Section(0, 10, 0, 10).contains(Section(1, 9, 1, 11))
False
>>> Section(0, 10, 0, 10).contains(Section(1, 3, 1, 7))
True
>>> Section(0, 10, 0, 10).contains(Section(1, 3, 1, 11))
False
static from_shape(value)[source]

Produce a Section object defining a given shape.

Examples

>>> Section.from_shape((10, 10))
Section(x1=0, x2=10, y1=0, y2=10)
>>> Section.from_shape((10, 10, 10))
Section(x1=0, x2=10, y1=0, y2=10, z1=0, z2=10)
static from_string(value)[source]

Produce a Section object from a string.

is_same_size(section)[source]

Return True if the Sections are the same size, otherwise False.

Examples

>>> Section(0, 10, 0, 10).is_same_size(Section(0, 10, 0, 10))
True
>>> Section(0, 10, 0, 10).is_same_size(Section(0, 10, 0, 11))
False
overlap(section)[source]

Return the overlap between two sections, or None if no overlap.

Determine whether the two sections overlap. If so, the Section common to both is returned, otherwise None.

Examples

>>> Section(0, 10, 0, 10).overlap(Section(1, 9, 1, 9))
Section(x1=1, x2=9, y1=1, y2=9)
>>> Section(0, 10, 0, 10).overlap(Section(1, 11, 1, 9))
Section(x1=1, x2=10, y1=1, y2=9)
>>> Section(0, 10, 0, 10).overlap(Section(1, 9, 1, 11))
Section(x1=1, x2=9, y1=1, y2=10)
>>> Section(0, 10, 0, 10).overlap(Section(1, 3, 1, 7))
Section(x1=1, x2=3, y1=1, y2=7)
>>> Section(4, 6, 4, 6).overlap(Section(1, 3, 1, 2))
None
Raises:

ValueError – If the Sections have different dimensionality.

Notes

If sections do not overlap, a warning is logged when None is returned. This is to help with debugging, as it is often not an error condition.

shift(*shifts)[source]

Shift a section in each direction by the specified amount.

Parameters:

shifts (positional arguments) – The amount to shift the section in each direction.

Returns:

The shifted section.

Return type:

Section

Raises:

ValueError – If the number of shifts is not equal to the number of dimensions.

Examples

>>> Section(0, 10, 0, 10).shift(1, 1)
Section(x1=1, x2=11, y1=1, y2=11)
>>> Section(0, 10, 0, 10).shift(1, 1, 1)
Traceback (most recent call last):
...
ValueError: Number of shifts 3 incompatible with dimensionality 2