Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Tool and Component functionality #1052

Merged
merged 26 commits into from
May 20, 2019

Conversation

kosack
Copy link
Contributor

@kosack kosack commented Apr 24, 2019

This PR is part of an effort to make some standard tools for DL1/DL2 production in ctapipe. One of the things I found lacking was the ability to see what the full configuration that was used in a Tool (i.e. the Tool's config attribute only gives you the options the user passed in, not the values used).

To make it possible to generate a fully populated config dictionary for a Component or Tool, this adds a few features:

  • Tools now have a get_current_config() method that returns the full config for an instance (this was only possibly for a Class with traitlets.config.Application). This is automatically registered with the Provenance system during Tool.run()
  • added an export_tool_to_commented_yaml function which takes a tool and its current config and writes out a YAML file with correct comments (it's a bit of a hack based on the traitlets.Application.write_config() code,, so perhaps not ready for production use, but it's a step in the direction of being able to read and write YAML configurations (so far only JSON and Python are supported for reading)
  • Added some nice HTML representations of Tools and Components for Jupyter notebooks
  • Tools must now "register" all sub-components so that their current configuration can be queried
  • the functionality in ctapipe.utils.tools has now moved to ctapipe.core.traits (both packages had similar scope)

e.g.:

class MyTool:
    ...
    def setup(self):
        self.mycomp = self.add_component(MyComponent(parent=self))

Then when one wants to get the full config, it looks like this:

tool = DisplayIntegrator()
...
pprint(tool.get_current_config())
{
         "DisplayIntegrator": {
            "channel": 0,
            "config_file": "",
            "event_index": 0,
            "extractor_product": "NeighborPeakWindowSum",
            "log_datefmt": "%Y-%m-%d %H:%M:%S",
            "log_format": "%(levelname)s: %(message)s [%(name)s.%(funcName)s]",
            "log_level": 10,
            "telescope": 458,
            "use_event_id": false
         },
         "SimTelEventSource": {
            "allowed_tels": [],
            "input_url": "/Users/kkosack/Data/CTA/Prod3b/gamma/gamma_20deg_0deg_run100___cta-prod3_desert-2150m-Paranal-merged.simtel.gz",
            "max_events": null,
            "skip_calibration_events": true
         },
         "EventSeeker": {},
         "NeighborPeakWindowSum": {
            "lwt": 0,
            "window_shift": 3,
            "window_width": 7
         },
         "HESSIOR1Calibrator": {},
         "CameraDL0Reducer": {},
         "CameraDL1Calibrator": {
            "clip_amplitude": null,
            "radius": null
         }
      }

In making this update, I ran into a lot of confusion and undocumented features of traitlets.config, so it might be better later to re-implement traitlets.config.Application altogether (I started a small notebook to explore that, seen here: https://gist.github.com/kosack/8158a2748a458bbece74a010abb02d3c but found that it is too much effort to get all the functionality now. Perhaps somebody else has the time)

kosack added 4 commits April 23, 2019 18:20
- `get_current_config()` returns the *full* configuration of the instance, unlike the `.config` attribute which only returns the non-default settings. This includes even changes made during the running of the program, not from the input configuration files.

- added a nicer HTML repr for notebooks

-
@kosack
Copy link
Contributor Author

kosack commented Apr 24, 2019

For an example of the experimental YAML output:

print(export_tool_config_to_commented_yaml(tool))                                                 
#------------------------------------------------------------------------------
# DisplayIntegrator(Tool) configuration
#------------------------------------------------------------------------------
DisplayIntegrator:
    # Channel to view
    # Choices: any of [0, 1]
    # Default: 0
    channel: 0

    # name of a configuration file with parameters to load in addition
    # to command-line parameters
    # Default: ''
    config_file: ""

    # Event index to view.
    # Default: 0
    event_index: 0

    # ImageExtractor to use.
    # Choices: any of ['FullWaveformSum', 'FixedWindowSum',
    # 'GlobalPeakWindowSum', 'LocalPeakWindowSum',
    # 'NeighborPeakWindowSum',
    # 'BaselineSubtractedNeighborPeakWindowSum'] or None
    # Default: 'NeighborPeakWindowSum'
    extractor_product: "NeighborPeakWindowSum"

    # The date format used by logging formatters for %(asctime)s
    # Default: '%Y-%m-%d %H:%M:%S'
    log_datefmt: "%Y-%m-%d %H:%M:%S"

    # The Logging format template
    # Default: '%(levelname)s [%(name)s] (%(module)s/%(funcName)s):
    # %(message)s'
    log_format: "%(levelname)s [%(name)s] (%(module)s/%(funcName)s): %(message)s"

    # Set the log level by value or name.
    # Choices: any of (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN',
    # 'ERROR', 'CRITICAL')
    # Default: 30
    log_level: 20

    # Telescope to view. Set to None to display the firsttelescope
    # with data.
    # Default: None
    telescope: None

    # event_index will obtain an event using event_id instead of
    # index.
    # Default: False
    use_event_id: False

@codecov
Copy link

codecov bot commented Apr 24, 2019

Codecov Report

Merging #1052 into master will increase coverage by 0.26%.
The diff coverage is 95.7%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1052      +/-   ##
==========================================
+ Coverage   83.44%   83.71%   +0.26%     
==========================================
  Files         181      179       -2     
  Lines       10439    10548     +109     
==========================================
+ Hits         8711     8830     +119     
+ Misses       1728     1718      -10
Impacted Files Coverage Δ
ctapipe/core/plugins.py 100% <ø> (ø) ⬆️
ctapipe/core/container.py 68.31% <ø> (ø) ⬆️
ctapipe/io/simteleventsource.py 98.46% <ø> (ø) ⬆️
ctapipe/tools/plot_charge_resolution.py 89.65% <100%> (-0.35%) ⬇️
ctapipe/core/provenance.py 93.1% <100%> (+0.18%) ⬆️
ctapipe/analysis/camera/charge_resolution.py 100% <100%> (ø) ⬆️
ctapipe/tools/display_dl1.py 90.09% <100%> (+0.09%) ⬆️
ctapipe/core/traits.py 97.22% <100%> (+1.57%) ⬆️
ctapipe/io/eventsource.py 98.11% <100%> (ø) ⬆️
ctapipe/core/tests/test_tool.py 100% <100%> (ø) ⬆️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fea8a57...d15e992. Read the comment docs.

- properly set the default log_format trait
- allow Component *instances* to be registered (so we can get their config later)
- added `get_current_config()`, similar to the one in Component, but returning the actual config for the Tool instance + all registered Component instances
- add the full instance config to the provenance system
- added an HTML repr for notebooks
- fixed a type in test_component_current_config()
- add tool example
- always log the config (as info not debug)
- ensure allowed_tels is a set internally
- make sure set() is JSON serializable
- show difference to default value in written config
- added tests for new tool functionality
- Update notebooks
@kosack kosack force-pushed the feature/improve-tool branch from 1f36b11 to 9e94133 Compare April 26, 2019 12:45
@kosack kosack requested a review from watsonjj April 26, 2019 14:11
@kosack kosack mentioned this pull request May 2, 2019
17 tasks
@kosack kosack self-assigned this May 3, 2019
Copy link
Contributor

@thomasarmstrong thomasarmstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor comments for now

help='The Logging format template'
).tag(config=True)

log_format = Unicode(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this duplicate intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope! I'll fix it

True,
help='Display the photoelectron images on-screen as they '
'are produced.'
True, help="Display the photoelectron images on-screen as they " "are produced."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra "" or new line?

@@ -137,41 +142,31 @@ class DisplayDL1Calib(Tool):
telescope = Int(
None,
allow_none=True,
help='Telescope to view. Set to None to display all '
'telescopes.'
help="Telescope to view. Set to None to display all " "telescopes.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra ""

"Display the photoelectron images on-screen as they "
"are produced."
{"ImagePlotter": {"display": True}},
"Display the photoelectron images on-screen as they " "are produced.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks- seems to be due to reformatting (strings like that get concatenated, but there's no need in this case)

Copy link
Contributor

@watsonjj watsonjj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@kosack kosack merged commit ae9546d into cta-observatory:master May 20, 2019
@kosack kosack deleted the feature/improve-tool branch October 2, 2020 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants