ObservationGroups

class gammapy.data.ObservationGroups(axes)[source]

Bases: object

Observation groups.

Class to define observation groups useful for organizing observation lists into groups of observations with similar properties.

The properties and their binning are specified via ObservationGroupAxis objects.

The class takes as input a list of ObservationGroupAxis objects and defines one group for each possible combination of the bins defined in all axes (cartesian product). The groups are identified by a unique GROUP_ID int value.

The definitions of the groups are internally stored as a Table object, the obs_groups_table member.

The axis parameters should be either dimensionless or castable into Quantity objects.

For details on the grouping of observations in a list, please refer to the apply method.

See also Observation grouping.

Parameters:

axes : list of ObservationGroupAxis

List of observation group axes.

Examples

Define an observation grouping:

alt = Angle([0, 30, 60, 90], 'deg')
az = Angle([-90, 90, 270], 'deg')
ntels = np.array([3, 4])
obs_groups = ObservationGroups([
    ObservationGroupAxis('ALT', alt, fmt='edges'),
    ObservationGroupAxis('AZ', az, fmt='edges'),
    ObservationGroupAxis('N_TELS', ntels, fmt='values'),
])

Print the observation group table (group definitions):

>>> print(obs_groups.obs_groups_table)

Print the observation group axes:

>>> print(obs_groups.info)

Group the observations of an observation list and print them:

>>> obs_table_grouped = obs_groups.apply(obs_table)
>>> print(obs_table_grouped)

Get the observations of a particular group and print them:

>>> obs_table_group8 = obs_groups.get_group_of_observations(obs_table_grouped, 8)
>>> print(obs_table_group8)

Attributes Summary

info Info string (str).
list_of_groups List of groups (ndarray).
n_groups Number of groups (int).

Methods Summary

apply(obs_table) Group observations in a list according to the defined groups.
axes_to_table(axes) Fill the observation group axes into a table.
get_group_of_observations(obs_table, group) Select the runs corresponding to a particular group.
info_group(group_id) Group info string.
read(filename) Read observation group definitions from ECSV file.
table_to_axes(table) Define observation group axis list from a table.
write(outfile[, overwrite]) Write observation group definitions to ECSV file.

Attributes Documentation

info

Info string (str).

list_of_groups

List of groups (ndarray).

n_groups

Number of groups (int).

Methods Documentation

apply(obs_table)[source]

Group observations in a list according to the defined groups.

The method returns the same observation table with an extra column in the 1st position indicating the group ID of each observation.

The algorithm expects the same format (naming and variable definition range) for both the grouping axis definition and the corresponding variable in the table. For instance, if the azimuth axis binning is defined as AZ with bin edges [-90, 90, 270] (North and South bins), the input obs table should have an azimuth column defined as AZ and wrapped at 270 deg. This can easily be done by calling:

>>> obs_table['AZ'] = Angle(obs_table['AZ']).wrap_at(Angle(270., 'deg'))
Parameters:

obs_table : ObservationTable

Observation list to group.

Returns:

obs_table_grouped : ObservationTable

Grouped observation list.

static axes_to_table(axes)[source]

Fill the observation group axes into a table.

Define one row for each possible combination of the observation group axis bins. Each row will represent an observation group.

Parameters:

axes : ObservationGroupAxis

List of observation group axes.

Returns:

table : Table

Table containing the observation group definitions.

get_group_of_observations(obs_table, group, inverted=False, apply_grouping=False)[source]

Select the runs corresponding to a particular group.

If the inverted flag is activated, the selection is applied to exclude the indicated group and keep all others.

Parameters:

obs_table : ObservationTable

Observation list to select from.

group : int

Group ID to select.

inverted : bool, optional

Invert selection: exclude the indicated group and keep the rest.

apply_grouping : bool, optional

Flag to indicate if the observation grouping should take place.

Returns:

obs_table_group : ObservationTable

Observation list of a specific group.

info_group(group_id)[source]

Group info string.

Parameters:

group_id : int

ID of the group to gather info on.

Returns:

s : str

Group info string.

classmethod read(filename)[source]

Read observation group definitions from ECSV file.

Using Table and ascii.

Parameters:

filename : str

Name of the file.

Returns:

obs_groups : ObservationGroups

Observation groups object.

static table_to_axes(table)[source]

Define observation group axis list from a table.

Interpret the combinations of bins from a table of groups in order to define the corresponding observation group axes.

Parameters:

table : Table

Table containing the observation group definitions.

Returns:

axes : ObservationGroupAxis

List of observation group axes.

write(outfile, overwrite=False)[source]

Write observation group definitions to ECSV file.

Using Table and ascii.

Parameters:

outfile : str

Name of the file.

overwrite : bool, optional

Flag to control file overwriting.