Пример #1
0
    def from_url(cls, input_url, **kwargs):
        """
        Find compatible EventSource for input_url via the `is_compatible`
        method of the EventSource

        Parameters
        ----------
        input_url : str
            Filename or URL pointing to an event file
        kwargs
            Named arguments for the EventSource

        Returns
        -------
        instance
            Instance of a compatible EventSource subclass
        """
        detect_and_import_io_plugins()
        available_classes = non_abstract_children(cls)

        for subcls in available_classes:
            if subcls.is_compatible(input_url):
                return subcls(input_url=input_url, **kwargs)

        raise ValueError(
            'Cannot find compatible EventSource for \n'
            '\turl:{}\n'
            'in available EventSources:\n'
            '\t{}'.format(input_url, [c.__name__ for c in available_classes])
        )
Пример #2
0
def test_non_abstract_children():
    """check that we can find all constructable children"""
    from ctapipe.core import non_abstract_children

    class AbstractBase(ABC):
        @abstractmethod
        def method(self):
            pass

    class Child1(AbstractBase):
        def method(self):
            print("method of Child1")

    class Child2(AbstractBase):
        def method(self):
            print("method of Child2")

    class GrandChild(Child2):
        def method(self):
            print("method of GrandChild")

    class AbstractChild(AbstractBase):
        pass

    kids = non_abstract_children(AbstractBase)
    assert Child1 in kids
    assert Child2 in kids
    assert GrandChild in kids
    assert AbstractChild not in kids
Пример #3
0
    def from_url(cls, input_url, **kwargs):
        """
        Find compatible EventSource for input_url via the `is_compatible`
        method of the EventSource

        Parameters
        ----------
        input_url : str
            Filename or URL pointing to an event file
        kwargs
            Named arguments for the EventSource

        Returns
        -------
        instance
            Instance of a compatible EventSource subclass
        """
        if input_url == "" or input_url is None:
            raise ToolConfigurationError(
                "EventSource: No input_url was specified")

        detect_and_import_io_plugins()
        available_classes = non_abstract_children(cls)

        for subcls in available_classes:
            if subcls.is_compatible(input_url):
                return subcls(input_url=input_url, **kwargs)

        raise ValueError("Cannot find compatible EventSource for \n"
                         "\turl:{}\n"
                         "in available EventSources:\n"
                         "\t{}".format(input_url,
                                       [c.__name__
                                        for c in available_classes]))
Пример #4
0
    def from_url(cls, input_url, **kwargs):
        """
        Find compatible EventSource for input_url via the `is_compatible`
        method of the EventSource

        Parameters
        ----------
        input_url : str
            Filename or URL pointing to an event file
        kwargs
            Named arguments for the EventSource

        Returns
        -------
        instance
            Instance of a compatible EventSource subclass
        """
        available_classes = non_abstract_children(cls)

        for subcls in available_classes:
            if subcls.is_compatible(input_url):
                return subcls(input_url=input_url, **kwargs)

        raise ValueError('Cannot find compatible EventSource for \n'
                         '\turl:{}\n'
                         'in available EventSources:\n'
                         '\t{}'.format(input_url,
                                       [c.__name__
                                        for c in available_classes]))
Пример #5
0
def enum_trait(base_class, default, help_str=None):
    '''create a configurable CaselessStrEnum traitlet from baseclass

    the enumeration should contain all names of non_abstract_children()
    of said baseclass and the default choice should be given by
    `base_class._default` name.

    default must be specified and must be the name of one child-class
    '''
    if help_str is None:
        help_str = '{} to use.'.format(base_class.__name__)

    choices = [
        cls.__name__
        for cls in non_abstract_children(base_class)
    ]
    if default not in choices:
        raise ValueError(
            '{default} is not in choices: {choices}'.format(
                default=default,
                choices=choices,
            )
        )

    return CaselessStrEnum(
        choices,
        default,
        allow_none=True,
        help=help_str
    ).tag(config=True)
Пример #6
0
def test_non_abstract_children():
    from ctapipe.core import non_abstract_children

    class AbstractBase(ABC):
        @abstractmethod
        def method(self):
            pass

    class Child1(AbstractBase):
        def method(self):
            print('method of Child1')

    class Child2(AbstractBase):
        def method(self):
            print('method of Child2')

    class GrandChild(Child2):
        def method(self):
            print('method of GrandChild')

    class AbstractChild(AbstractBase):
        pass

    kids = non_abstract_children(AbstractBase)
    assert Child1 in kids
    assert Child2 in kids
    assert GrandChild in kids
    assert AbstractChild not in kids
Пример #7
0
def test_non_abstract_children():
    from ctapipe.core import non_abstract_children

    class AbstractBase(ABC):
        @abstractmethod
        def method(self):
            pass

    class Child1(AbstractBase):
        def method(self):
            print('method of Child1')

    class Child2(AbstractBase):
        def method(self):
            print('method of Child2')

    class GrandChild(Child2):
        def method(self):
            print('method of GrandChild')

    class AbstractChild(AbstractBase):
        pass

    kids = non_abstract_children(AbstractBase)
    assert Child1 in kids
    assert Child2 in kids
    assert GrandChild in kids
    assert AbstractChild not in kids
Пример #8
0
def test_available_sources():
    from ctapipe.io.eventsource import EventSource
    from ctapipe.core import non_abstract_children

    # make this before the explicit imports to make sure
    # all classes are avaialble even if not explicitly imported
    children = non_abstract_children(EventSource)

    from ctapipe.io.simteleventsource import SimTelEventSource

    assert SimTelEventSource in children
Пример #9
0
def enum_trait(base_class, default, help_str=None):
    '''create a configurable CaselessStrEnum traitlet from baseclass

    the enumeration should contain all names of non_abstract_children()
    of said baseclass and the default choice should be given by
    `base_class._default` name.

    default must be specified and must be the name of one child-class
    '''
    if help_str is None:
        help_str = '{} to use.'.format(base_class.__name__)

    choices = [cls.__name__ for cls in non_abstract_children(base_class)]
    if default not in choices:
        raise ValueError('{default} is not in choices: {choices}'.format(
            default=default,
            choices=choices,
        ))

    return CaselessStrEnum(choices, default, allow_none=True,
                           help=help_str).tag(config=True)
Пример #10
0
from traitlets.config.loader import Config

from ctapipe.core import non_abstract_children
from ctapipe.image.extractor import (
    extract_around_peak,
    neighbor_average_waveform,
    subtract_baseline,
    integration_correction,
    ImageExtractor,
    FixedWindowSum,
    NeighborPeakWindowSum,
)
from ctapipe.image.toymodel import WaveformModel
from ctapipe.instrument import SubarrayDescription, TelescopeDescription

extractors = non_abstract_children(ImageExtractor)
# FixedWindowSum has no peak finding and need to be set manually
extractors.remove(FixedWindowSum)


@pytest.fixture(scope="module")
def subarray():
    subarray = SubarrayDescription(
        "test array",
        tel_positions={
            1: np.zeros(3) * u.m,
            2: np.zeros(3) * u.m
        },
        tel_descriptions={
            1:
            TelescopeDescription.from_name(optics_name="SST-ASTRI",
Пример #11
0
from ctapipe.core import non_abstract_children
from ctapipe.image.extractor import (
    extract_around_peak,
    neighbor_average_waveform,
    subtract_baseline,
    integration_correction,
    ImageExtractor,
    FixedWindowSum,
    NeighborPeakWindowSum,
    TwoPassWindowSum,
)
from ctapipe.image.toymodel import WaveformModel
from ctapipe.instrument import SubarrayDescription, TelescopeDescription

extractors = non_abstract_children(ImageExtractor)
# FixedWindowSum has no peak finding and need to be set manually
extractors.remove(FixedWindowSum)


@pytest.fixture(scope="module")
def subarray():
    subarray = SubarrayDescription(
        "test array",
        tel_positions={
            1: np.zeros(3) * u.m,
            2: np.zeros(3) * u.m
        },
        tel_descriptions={
            1:
            TelescopeDescription.from_name(optics_name="SST-ASTRI",
Пример #12
0
from sstcam_simulation import Photoelectrons, SSTCameraMapping
from sstcam_simulation.camera.spe import single_gaussian, sipm_gentile_spe, \
    SPESpectrum, optical_crosstalk_probability, SiPMDelayed, SiPMReflectedOCT
from ctapipe.core import non_abstract_children
import numpy as np
import pytest

subclasses = non_abstract_children(SPESpectrum)
subclasses.remove(SiPMReflectedOCT)


def test_pmt():
    x = np.linspace(0, 3, 10000)
    pdf = single_gaussian(x, 0.2)
    avg = np.average(x, weights=pdf)
    var = np.average((x - avg)**2, weights=pdf)
    np.testing.assert_allclose(avg, 1, rtol=1e-5)
    np.testing.assert_allclose(var, 0.04, rtol=1e-5)


def test_optical_crosstalk_probability():
    k = 1
    assert optical_crosstalk_probability(k, 0.3) == 1 - 0.3

    k = np.arange(1, 250)
    assert optical_crosstalk_probability(k, 0.2).sum() == 1

    assert optical_crosstalk_probability(0, 0.3) == 0


@pytest.mark.parametrize("opct", [0.1, 0.3, 0.5, 0.7, 0.9])
Пример #13
0
def classes_with_traits(base_class):
    all_classes = [base_class] + non_abstract_children(base_class)
    return [cls for cls in all_classes if has_traits(cls)]
Пример #14
0
def classes_with_traits(base_class):
    all_classes = [base_class] + non_abstract_children(base_class)
    return [cls for cls in all_classes if has_traits(cls)]