ctapipe is not stable yet, so expect large and rapid changes to structure and functionality as we explore various design choices before the 1.0 release.

Container#

class ctapipe.core.Container(prefix=None, **fields)[source]#

Bases: object

Generic class that can hold and accumulate data to be passed

between Components.

The purpose of this class is to provide a flexible data structure that works a bit like a dict or blank Python class, but prevents the user from accessing members that have not been defined a priori (more like a C struct), and also keeps metadata information such as a description, defaults, and units for each item in the container.

Containers can transform the data into a dict using the as_dict method. This allows them to be written to an output table for example, where each Field defines a column. The dict conversion can be made recursively and even flattened so that a nested set of Containers can be translated into a set of columns in a flat table without naming conflicts (the name of the parent Field is prepended).

Only members of instance Field will be used as output. For hierarchical data structures, Field can use Container subclasses or a Map as the default value.

>>> import astropy.units as u
>>> class MyContainer(Container):
...     x = Field(100, "The X value")
...     energy = Field(-1, "Energy measurement", unit=u.TeV)
...
>>> cont = MyContainer()
>>> print(cont.x)
100
>>> # metadata will become header keywords in an output file:
>>> cont.meta["KEY"] = "value"

Fields inside Containers can contain instances of other containers, to allow for a hierarchy of containers, and can also contain a Map for the case where one wants e.g. a set of sub-classes indexed by a value like the telescope_id. Examples of this can be found in ctapipe.containers

Container works by shadowing all class variables (which must be instances of Field) with instance variables of the same name that hold the actual data. If reset is called, all instance variables are reset to their default values as defined in the class.

Finally, a Container can have associated metadata via its meta attribute, which is a dict of keywords to values.

Attributes:
metadict

dict of attached metadata

prefixstr

Prefix attached to column names when saved to a table or file

Attributes Summary

default_prefix

fields

meta

prefix

Methods Summary

as_dict([recursive, flatten, add_prefix, ...])

Convert the Container into a dictionary

items([add_prefix])

Generator over (key, value) pairs for the items

keys()

Get the keys of the container

reset()

Reset all values back to their default values

update(**values)

update more than one parameter at once (e.g. update(x=3,y=4) or update(**dict_of_values)).

validate()

Check that all fields in the Container have the expected characteristics (as defined by the Field metadata).

values()

Get the keys of the container

Attributes Documentation

default_prefix = ''#
fields = {}#
meta#
prefix#

Methods Documentation

as_dict(recursive=False, flatten=False, add_prefix=False, add_key=False)[source]#

Convert the Container into a dictionary

Parameters:
recursive: bool

sub-Containers should also be converted to dicts

flatten: type

return a flat dictionary, with any sub-field keys generated by appending the sub-Container name.

add_prefix: bool

include the container’s prefix in the name of each item

add_key: bool

include map key

items(add_prefix=False)[source]#

Generator over (key, value) pairs for the items

keys()[source]#

Get the keys of the container

reset()[source]#

Reset all values back to their default values

Parameters:
recursive: bool

If true, also reset all sub-containers

update(**values)[source]#

update more than one parameter at once (e.g. update(x=3,y=4) or update(**dict_of_values))

validate()[source]#

Check that all fields in the Container have the expected characteristics (as defined by the Field metadata). This is not intended to be run every time a Container is filled, since it is slow, only for testing a first event.

Raises:
ValueError:

if the Container’s values are not valid

values()[source]#

Get the keys of the container