SourceGroups#

class photutils.psf.SourceGroups(x, y, groups)[source]#

Bases: object

Container for source grouping results with analysis methods.

This class stores the results of grouping sources and provides methods to analyze and query the grouping.

Parameters:
x, y1D float ndarray

The 1D arrays of the x and y coordinates of the sources.

groups1D int ndarray

A 1D array of the group IDs, in the same order as the input x and y coordinates.

Attributes:
xndarray

The x coordinates of the sources.

yndarray

The y coordinates of the sources.

groupsndarray

The group IDs for each source.

n_sourcesint

Total number of sources.

n_groupsint

Total number of groups.

See also

SourceGrouper

Examples

Create a SourceGroups object:

>>> from photutils.psf import SourceGroups
>>> import numpy as np
>>> x = np.array([10, 15, 50, 55, 100])
>>> y = np.array([20, 25, 60, 65, 90])
>>> groups = np.array([1, 1, 2, 2, 3])
>>> source_groups = SourceGroups(x, y, groups)
>>> print(source_groups)
<SourceGroups(n_sources=5, n_groups=3)>

Access properties of the SourceGroups object:

>>> print(f'Number of groups: {source_groups.n_groups}')
Number of groups: 3
>>> source_groups.size_map
{1: 2, 2: 2, 3: 1}
>>> source_groups.sizes
array([2, 2, 2, 2, 1])
>>> source_groups.group_centers
{1: (12.5, 22.5), 2: (52.5, 62.5), 3: (100.0, 90.0)}
>>> x_group1, y_group1 = source_groups.get_group_sources(1)
>>> print(x_group1, y_group1)
[10 15] [20 25]

Attributes Summary

group_centers

Centroid coordinates of each group.

size_map

Mapping of group ID to group size.

sizes

Size of each group for each source.

Methods Summary

get_group_sources(group_id)

Get the coordinates of all sources in a specific group.

plot(radius, *[, ax, cmap, seed, ...])

Plot circular apertures around sources, color-coded by group.

Attributes Documentation

group_centers#

Centroid coordinates of each group.

Returns:
group_centersdict

A dictionary where keys are group IDs and values are tuples of (x_center, y_center) representing the centroid of each group.

size_map#

Mapping of group ID to group size.

Returns:
size_mapdict

A dictionary where keys are group IDs and values are the corresponding group sizes.

sizes#

Size of each group for each source.

Returns:
group_sizes1D int ndarray

A 1D array of the group sizes, in the same order as the sources. Each element indicates how many sources are in the same group as the corresponding source.

Methods Documentation

get_group_sources(group_id)[source]#

Get the coordinates of all sources in a specific group.

Parameters:
group_idint

The group ID to retrieve sources for.

Returns:
x, yndarray

Arrays of x and y coordinates for all sources in the specified group.

plot(radius, *, ax=None, cmap=None, seed=0, label_groups=False, label_kwargs=None, label_offset=(0, 0), **kwargs)[source]#

Plot circular apertures around sources, color-coded by group.

Parameters:
radiusfloat

The radius of the circles to plot around each source (in pixels).

axAxes, optional

The matplotlib axes on which to plot. If None, uses the current axes.

cmapColormap or str, optional

The colormap to use for group colors. If None, a random colormap is generated.

seedint, optional

Random seed for generating the colormap if cmap is None.

label_groupsbool, optional

Whether to label each group with its group ID at the group center.

label_kwargsdict, optional

Keyword arguments passed to ax.text for plotting group ID labels.

label_offsettuple of float, optional

Offset (dx, dy) in pixels for positioning labels relative to group centers. Positive values move right/up, negative values move left/down. Default is (0, 0).

**kwargs

Additional keyword arguments passed to plot.

Returns:
axAxes

The matplotlib axes object.