Skip to content

Commit

Permalink
Switch on doctests in the CI (cta-observatory#1731)
Browse files Browse the repository at this point in the history
* Switch on doctests in the CI

* Fix doctests failures
  • Loading branch information
maxnoe authored and nbiederbeck committed Aug 3, 2021
1 parent c73eea9 commit 378ae63
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 40 deletions.
22 changes: 12 additions & 10 deletions ctapipe/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,16 @@ class Container(metaclass=ContainerMeta):
For hierarchical data structures, Field can use `Container`
subclasses or a `Map` as the default value.
>>> class MyContainer(Container):
>>> x = Field(100,"The X value")
>>> energy = Field(-1, "Energy measurement", unit=u.TeV)
>>>
>>> cont = MyContainer()
>>> print(cont.x)
>>> # metadata will become header keywords in an output file:
>>> cont.meta['KEY'] = 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 <Field>`_ inside `Containers <Container>`_ can contain instances of other
containers, to allow for a hierarchy of containers, and can also
Expand All @@ -220,8 +222,8 @@ class Container(metaclass=ContainerMeta):
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 the
hold the value expected. If ``reset`` is called, all
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.
Expand Down
1 change: 1 addition & 0 deletions ctapipe/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def recursive_update(d1, d2, copy=False):
{'a': {'b': 'foo', 'c': 'foo'}}
>>> # As opposed to
>>> d1.update(d2)
>>> d1
{'a': {'c': 'foo'}}
"""
if not isinstance(d1, Mapping) or not isinstance(d2, Mapping):
Expand Down
7 changes: 5 additions & 2 deletions ctapipe/image/toymodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
>>> from ctapipe.instrument import CameraGeometry
>>> geom = CameraGeometry.make_rectangular(20, 20)
>>> showermodel = Gaussian(x=0.25 * u.m, y=0.0 * u.m,
length=0.1 * u.m, width=0.02 * u.m, psi='40d')
>>> showermodel = Gaussian(
... x=0.25 * u.m, y=0.0 * u.m,
... length=0.1 * u.m, width=0.02 * u.m,
... psi='40d'
... )
>>> image, signal, noise = showermodel.generate_image(geom, intensity=1000)
>>> print(image.shape)
(400,)
Expand Down
12 changes: 7 additions & 5 deletions ctapipe/io/eventseeker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ class EventSeeker(Component):
filled with the event information using the methods defined in the
event_source for that file format.
To obtain a particular event in a hessio file:
To obtain a particular event in a simtel file:
>>> from ctapipe.io import SimTelEventSource
>>> event_source = SimTelEventSource(input_url="/path/to/file")
>>> event_source = SimTelEventSource(input_url="dataset://gamma_test_large.simtel.gz")
>>> seeker = EventSeeker(event_source=event_source)
>>> event = seeker.get_event_index(2)
>>> print(event.count)
2
To obtain a particular event in a hessio file from its event_id:
To obtain a particular event in a simtel file from its event_id:
>>> from ctapipe.io import SimTelEventSource
>>> event_source = SimTelEventSource(input_url="/path/to/file")
>>> event_source = SimTelEventSource(input_url="dataset://gamma_test_large.simtel.gz", back_seekable=True)
>>> seeker = EventSeeker(event_source=event_source)
>>> event = seeker.get_event_id(101)
>>> event = seeker.get_event_id(31007)
>>> print(event.count)
1
**NOTE**: Event_index refers to the number associated to the event
assigned by ctapipe (``event.count``), based on the order the events are
Expand Down
27 changes: 15 additions & 12 deletions ctapipe/io/eventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,33 @@ class EventSource(Component):
appropriate subclass if a compatible source is found for the given
``input_url``.
>>> dataset = get_dataset_path('gamma_test_large.simtel.gz')
>>> event_source = EventSource(input_url=dataset)
<ctapipe.io.simteleventsource.SimTelEventSource at ...>
>>> EventSource(input_url="dataset://gamma_test_large.simtel.gz")
<ctapipe.io.simteleventsource.SimTelEventSource ...>
An ``EventSource`` can also be created through the configuration system,
by passing ``config`` or ``parent`` as appropriate.
E.g. if using ``EventSource`` inside of a ``Tool``, you would do:
>>> self.event_source = EventSource(parent=self)
>>> self.source = EventSource(parent=self) # doctest: +SKIP
To loop through the events in a file:
>>> event_source = EventSource(input_url="/path/to/file")
>>> for event in event_source:
>>> print(event.count)
>>> source = EventSource(input_url="dataset://gamma_test_large.simtel.gz", max_events=2)
>>> for event in source:
... print(event.count)
0
1
**NOTE**: Every time a new loop is started through the event_source,
**NOTE**: Every time a new loop is started through the source,
it tries to restart from the first event, which might not be supported
by the event source.
It is encouraged to use ``EventSource`` in a context manager to ensure
the correct cleanups are performed when you are finished with the event_source:
the correct cleanups are performed when you are finished with the source:
>>> with EventSource(input_url="/path/to/file") as event_source:
>>> for event in event_source:
>>> print(event.count)
>>> with EventSource(input_url="dataset://gamma_test_large.simtel.gz", max_events=2) as source:
... for event in source:
... print(event.count)
0
1
**NOTE**: For effiency reasons, most sources only use a single ``ArrayEvent`` instance
and update it with new data on iteration, which might lead to surprising
Expand Down
8 changes: 1 addition & 7 deletions ctapipe/utils/fitshistogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Histogram:
--------
>>> hist = Histogram(nbins=(10,10), ranges=[[-1,1], [-1,1]])
>>> data = np.random.normal(shape=(2*100)) # make 100 random 2D events
>>> data = np.random.normal(size=(100, 2)) # make 100 random 2D events
>>> hist.fill(data)
Expand Down Expand Up @@ -180,12 +180,6 @@ def to_fits(self):
"""
Convert the `Histogram` into an `astropy.io.fits.ImageHDU`,
suitable for writing to a file.
Examples
--------
>>> myhist.to_fits().writeto("outputfile.fits.gz", overwrite=True)
"""
ohdu = fits.ImageHDU(data=self.data.transpose())
ohdu.name = self.name
Expand Down
2 changes: 0 additions & 2 deletions docs/ctapipe_api/instrument/camerageometry_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@
plt.subplot(1, 2, 2)
plt.scatter(geom.pix_x, geom.pix_y)
plt.title("Pixel Positions")

plt.show()
5 changes: 3 additions & 2 deletions examples/stereo_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

from ctapipe.io import EventSource
from ctapipe.calib import CameraCalibrator
from ctapipe.image.cleaning import tailcuts_clean, number_of_islands
from ctapipe.image.cleaning import tailcuts_clean
from ctapipe.image.morphology import number_of_islands
from ctapipe.image import leakage, hillas_parameters
from ctapipe.image.timing_parameters import timing_parameters
from ctapipe.image.timing import timing_parameters
from ctapipe.reco import HillasReconstructor
from ctapipe.utils.datasets import get_dataset_path

Expand Down

0 comments on commit 378ae63

Please sign in to comment.