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

Refactor initialization of telescope parameters, move to own module #2190

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ctapipe/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Core functionality of ctapipe
"""

from .component import Component, TelescopeComponent, non_abstract_children
from .component import Component, non_abstract_children
from .container import Container, DeprecatedField, Field, FieldValidationError, Map
from .feature_generator import FeatureGenerator
from .provenance import Provenance, get_module_version
from .qualityquery import QualityCriteriaError, QualityQuery
from .telescope_component import TelescopeComponent
from .tool import Tool, ToolConfigurationError, run_tool

__all__ = [
Expand Down
63 changes: 4 additions & 59 deletions ctapipe/core/component.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
""" Class to handle configuration for algorithms """
import warnings
from abc import ABCMeta
from inspect import isabstract
from inspect import cleandoc, isabstract
from logging import getLogger
from docutils.core import publish_parts
from inspect import cleandoc

from docutils.core import publish_parts
from traitlets import TraitError
from traitlets.config import Configurable

import warnings


__all__ = ["non_abstract_children", "Component", "TelescopeComponent"]
__all__ = ["non_abstract_children", "Component"]


def find_config_in_hierarchy(parent, class_name, trait_name):
Expand Down Expand Up @@ -261,55 +258,3 @@ def _repr_html_(self):
lines.append("</table>")
lines.append("</div>")
return "\n".join(lines)


class TelescopeComponent(Component):
"""
A component that needs a `~ctapipe.instrument.SubarrayDescription` to be constructed,
and which contains configurable `~ctapipe.core.traits.TelescopeParameter` fields
that must be configured on construction.
"""

def __init__(self, subarray, config=None, parent=None, **kwargs):
super().__init__(config, parent, **kwargs)

self.subarray = subarray
# configure all of the TelescopeParameters
for trait in list(self.class_traits()):
try:
getattr(self, trait).attach_subarray(subarray)
except (AttributeError, TypeError):
pass

@classmethod
def from_name(cls, name, subarray, config=None, parent=None, **kwargs):
"""
Obtain an instance of a subclass via its name

Parameters
----------
name : str
Name of the subclass to obtain
subarray: ctapipe.instrument.SubarrayDescription
The current subarray for this TelescopeComponent.
config : traitlets.loader.Config
Configuration specified by config file or cmdline arguments.
Used to set traitlet values.
This argument is typically only specified when using this method
from within a Tool.
parent : ctapipe.core.Tool
Tool executable that is calling this component.
Passes the correct logger and configuration to the component.
This argument is typically only specified when using this method
from within a Tool (config need not be passed if parent is used).
kwargs

Returns
-------
instace
Instance of subclass to this class
"""
requested_subclass = cls.non_abstract_subclasses()[name]
return requested_subclass(
subarray=subarray, config=config, parent=parent, **kwargs
)
Loading