<commit_before>"""
Implements the base client that is used by other classes to make requests
"""
from __future__ import division
from datetime import datetime, timezone
import math

from requests import Session
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

from .__version__ import __version__ as ClientVersion

class CustomerIOException(Exception):
    pass

class ClientBase(object):
    def __init__(self, retries=3, timeout=10, backoff_factor=0.02):
        self.timeout = timeout
        self.retries = retries

        self.http = Session()
        self.http.headers['User-Agent'] = "Customer.io Python Client/{version}".format(version=ClientVersion)

        # Retry request a number of times before raising an exception
        # also define backoff_factor to delay each retry
        self.http.mount('https://', HTTPAdapter(max_retries=Retry(
            total=retries, backoff_factor=backoff_factor)))

    def send_request(self, method, url, data):
        '''Dispatches the request and returns a response'''

        try:
            response = self.http.request(
                method, url=url, json=self._sanitize(data), timeout=self.timeout)
        except Exception as e:
            # Raise exception alerting user that the system might be
            # experiencing an outage and refer them to system status page.
            message = '''Failed to receive valid reponse after {count} retries.
Check system status at http://status.customer.io.
Last caught exception -- {klass}: {message}
            '''.format(klass=type(e), message=e, count=self.retries)
            raise CustomerIOException(message)

        result_status = response.status_code
        if result_status != 200:
            raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text))
        return response.text

    def _sanitize(self, data):
        for k, v in data.items():
            if isinstance(v, datetime):
                data[k] = self._datetime_to_timestamp(v)
            if isinstance(v, float) and math.isnan(v):
                data[k] = None
        return data

    def _datetime_to_timestamp(self, dt):
        return int(dt.replace(tzinfo=timezone.utc).timestamp())

    def _stringify_list(self, customer_ids):
        customer_string_ids = []
        for v in customer_ids:
            if isinstance(v, str):
                customer_string_ids.append(v)
            elif isinstance(v, int):
                customer_string_ids.append(str(v))
            else:
                raise CustomerIOException(
                    'customer_ids cannot be {type}'.format(type=type(v)))
        return customer_string_ids
<commit_msg>fix spelling goof<commit_after>"""
Implements the base client that is used by other classes to make requests
"""
from __future__ import division
from datetime import datetime, timezone
import math

from requests import Session
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

from .__version__ import __version__ as ClientVersion

class CustomerIOException(Exception):
    pass

class ClientBase(object):
    def __init__(self, retries=3, timeout=10, backoff_factor=0.02):
        self.timeout = timeout
        self.retries = retries

        self.http = Session()
        self.http.headers['User-Agent'] = "Customer.io Python Client/{version}".format(version=ClientVersion)

        # Retry request a number of times before raising an exception
        # also define backoff_factor to delay each retry
        self.http.mount('https://', HTTPAdapter(max_retries=Retry(
            total=retries, backoff_factor=backoff_factor)))

    def send_request(self, method, url, data):
        '''Dispatches the request and returns a response'''

        try:
            response = self.http.request(
                method, url=url, json=self._sanitize(data), timeout=self.timeout)
        except Exception as e:
            # Raise exception alerting user that the system might be
            # experiencing an outage and refer them to system status page.
            message = '''Failed to receive valid response after {count} retries.
Check system status at http://status.customer.io.
Last caught exception -- {klass}: {message}
            '''.format(klass=type(e), message=e, count=self.retries)
            raise CustomerIOException(message)

        result_status = response.status_code
        if result_status != 200:
            raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text))
        return response.text

    def _sanitize(self, data):
        for k, v in data.items():
            if isinstance(v, datetime):
                data[k] = self._datetime_to_timestamp(v)
            if isinstance(v, float) and math.isnan(v):
                data[k] = None
        return data

    def _datetime_to_timestamp(self, dt):
        return int(dt.replace(tzinfo=timezone.utc).timestamp())

    def _stringify_list(self, customer_ids):
        customer_string_ids = []
        for v in customer_ids:
            if isinstance(v, str):
                customer_string_ids.append(v)
            elif isinstance(v, int):
                customer_string_ids.append(str(v))
            else:
                raise CustomerIOException(
                    'customer_ids cannot be {type}'.format(type=type(v)))
        return customer_string_ids
<|endoftext|>


<commit_before># seleniumbase package
__version__ = "4.2.0"
<commit_msg>Version 4.3.0<commit_after># seleniumbase package
__version__ = "4.3.0"
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import json

from emarsys import EmarsysError

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.forms import Form
from django.utils.http import urlencode
from django.views.generic.detail import SingleObjectMixin, DetailView
from django.views.generic.edit import FormView

from django_tables2 import SingleTableView

from oscar.core.compat import get_user_model
from oscar.views.generic import ObjectLookupView

from apps.views import SearchMixin, SearchFormMixin

from django_emarsys.models import Event, EventInstance
from django_emarsys.context_provider_registry import ContextProviderException

from .forms import EventTriggerForm
from .tables import EventTable, EventInstanceTable

class EventListView(SearchFormMixin, SearchMixin, SingleTableView):
    model = Event
    table_class = EventTable
    template_name = 'dashboard/emarsys/event_list.html'

    search = {
        'name': 'name__icontains',
        'id': 'emarsys_id',
    }

    search_types = {
        'id': int
    }

class EventsSyncView(FormView):
    form_class = Form

    def form_valid(self, form):
        try:
            Event.objects.sync_events()
            messages.success(self.request, 'Events gesynct.')
        except EmarsysError as e:
            messages.error(self.request, 'Emarsys error: {}'.format(e))
        return super(EventsSyncView, self).form_valid(form)

    def get_success_url(self):
        return "{}?{}".format(reverse('dashboard:emarsys-event-list'),
                              urlencode(self.request.GET, doseq=True))

class EventTriggerMixin(object):
    model = Event
    context_object_name = 'event'
    form_class = EventTriggerForm
    template_name = 'dashboard/emarsys/event_trigger.html'

    def get(self, *args, **kwargs):
        self.event = self.object = self.get_object()
        return super(EventTriggerMixin, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        self.event = self.object = self.get_object()
        return super(EventTriggerMixin, self).post(*args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(EventTriggerMixin, self).get_form_kwargs()
        kwargs['event'] = self.event
        return kwargs

class EventTriggerView(EventTriggerMixin, SingleObjectMixin, FormView):
    def get_context_data(self, **kwargs):
        context = super(EventTriggerView, self).get_context_data(**kwargs)
        form = kwargs.get('form')
        if form and form.is_bound:
            data = self.event.get_placeholder_data(**form.cleaned_data)
            context['placeholder_data'] = sorted(data.items())
        return context

    def form_valid(self, form):
        if not self.event.emarsys_id:
            Event.objects.sync_events()
            self.event = Event.objects.get(pk=self.event.pk)

        recipient_email = form.cleaned_data.pop('recipient_email')
        user = form.cleaned_data.pop('user')

        event_instance = self.event.trigger(
            recipient_email=recipient_email or user.email,
            user=user,
            data=form.cleaned_data,
            source='manual')

        if event_instance.state != 'success':
            messages.error(self.request, event_instance.result)
        else:
            messages.success(self.request,
                             "Event '{}' wurde getriggert.".format(self.event))

        # redisplay form even if it was valid - this allows the user to easily
        # trigger the same event again for testing
        return super(EventTriggerView, self).form_invalid(form)

class EventPlaceholderDataView(EventTriggerMixin, SingleObjectMixin, FormView):
    template_name = 'dashboard/emarsys/partials/placeholder_data.html'

    def get_context_data(self, **kwargs):
        context = super(EventPlaceholderDataView, self).get_context_data(
            **kwargs)
        form = kwargs.get('form')
        if form and form.is_bound:
            try:
                data = self.event.get_placeholder_data(**form.cleaned_data)
            except ContextProviderException as e:
                context['placeholder_data_error'] = e
            else:
                context['placeholder_data'] = sorted(data.items())
        return context

    def form_valid(self, form):
        # Always just display the template
        return self.form_invalid(form)

class EventDataLookupView(EventTriggerMixin, SingleObjectMixin,
                          ObjectLookupView):
    def get(self, *args, **kwargs):
        self.kwarg_name = kwargs.pop('name')
        return super(EventDataLookupView, self).get(*args, **kwargs)

    def get_lookup_queryset(self):
        if self.kwarg_name == 'user':
            self.model = get_user_model()
        else:
            event_data = self.event.eventdata_set.get(
                kwarg_name=self.kwarg_name)
            self.model = event_data.content_type.model_class()

        queryset = super(EventDataLookupView, self).get_lookup_queryset()

        if hasattr(queryset, "emarsys_kwarg_name_filter"):
            queryset = queryset.emarsys_kwarg_name_filter(self.kwarg_name)

        return queryset

    def lookup_filter(self, queryset, query):
        if hasattr(queryset, 'emarsys_filter'):
            return queryset.emarsys_filter(query)
        return queryset.filter(pk=query)

class EventInstanceListView(SearchFormMixin, SearchMixin, SingleTableView):
    model = EventInstance
    table_class = EventInstanceTable
    template_name = 'dashboard/emarsys/event_instance_list.html'

    search = {
        'firstname': 'user__first_name__icontains',
        'lastname': 'user__last_name__icontains',
        'email': 'recipient_email__icontains',
        'event': 'event__name__icontains',
        'id': 'event__emarsys_id',
    }

    search_types = {
        'id': int
    }

class EventInstanceView(DetailView):
    model = EventInstance
    context_object_name = 'event_instance'
    template_name = 'dashboard/emarsys/event_instance.html'

    def get_context_data(self, **kwargs):
        context = super(EventInstanceView, self).get_context_data(**kwargs)
        context['placeholder_data'] = sorted(json.loads(
            context['event_instance'].context)['global'].items())
        return context
<commit_msg>Allow overriding how trigger data values are formatted<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import json

from emarsys import EmarsysError

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.forms import Form
from django.utils.http import urlencode
from django.views.generic.detail import SingleObjectMixin, DetailView
from django.views.generic.edit import FormView

from django_tables2 import SingleTableView

from oscar.core.compat import get_user_model
from oscar.views.generic import ObjectLookupView

from apps.views import SearchMixin, SearchFormMixin

from django_emarsys.models import Event, EventInstance
from django_emarsys.context_provider_registry import ContextProviderException

from .forms import EventTriggerForm
from .tables import EventTable, EventInstanceTable

class EventListView(SearchFormMixin, SearchMixin, SingleTableView):
    model = Event
    table_class = EventTable
    template_name = 'dashboard/emarsys/event_list.html'

    search = {
        'name': 'name__icontains',
        'id': 'emarsys_id',
    }

    search_types = {
        'id': int
    }

class EventsSyncView(FormView):
    form_class = Form

    def form_valid(self, form):
        try:
            Event.objects.sync_events()
            messages.success(self.request, 'Events gesynct.')
        except EmarsysError as e:
            messages.error(self.request, 'Emarsys error: {}'.format(e))
        return super(EventsSyncView, self).form_valid(form)

    def get_success_url(self):
        return "{}?{}".format(reverse('dashboard:emarsys-event-list'),
                              urlencode(self.request.GET, doseq=True))

class EventTriggerMixin(object):
    model = Event
    context_object_name = 'event'
    form_class = EventTriggerForm
    template_name = 'dashboard/emarsys/event_trigger.html'

    def get(self, *args, **kwargs):
        self.event = self.object = self.get_object()
        return super(EventTriggerMixin, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        self.event = self.object = self.get_object()
        return super(EventTriggerMixin, self).post(*args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(EventTriggerMixin, self).get_form_kwargs()
        kwargs['event'] = self.event
        return kwargs

class EventTriggerView(EventTriggerMixin, SingleObjectMixin, FormView):
    def get_context_data(self, **kwargs):
        context = super(EventTriggerView, self).get_context_data(**kwargs)
        form = kwargs.get('form')
        if form and form.is_bound:
            data = self.event.get_placeholder_data(**form.cleaned_data)
            context['placeholder_data'] = sorted(data.items())
        return context

    def form_valid(self, form):
        if not self.event.emarsys_id:
            Event.objects.sync_events()
            self.event = Event.objects.get(pk=self.event.pk)

        recipient_email = form.cleaned_data.pop('recipient_email')
        user = form.cleaned_data.pop('user')

        event_instance = self.event.trigger(
            recipient_email=recipient_email or user.email,
            user=user,
            data=form.cleaned_data,
            source='manual')

        if event_instance.state != 'success':
            messages.error(self.request, event_instance.result)
        else:
            messages.success(self.request,
                             "Event '{}' wurde getriggert.".format(self.event))

        # redisplay form even if it was valid - this allows the user to easily
        # trigger the same event again for testing
        return super(EventTriggerView, self).form_invalid(form)

class EventPlaceholderDataView(EventTriggerMixin, SingleObjectMixin, FormView):
    template_name = 'dashboard/emarsys/partials/placeholder_data.html'

    def get_context_data(self, **kwargs):
        context = super(EventPlaceholderDataView, self).get_context_data(
            **kwargs)
        form = kwargs.get('form')
        if form and form.is_bound:
            try:
                data = self.event.get_placeholder_data(**form.cleaned_data)
            except ContextProviderException as e:
                context['placeholder_data_error'] = e
            else:
                context['placeholder_data'] = sorted(data.items())
        return context

    def form_valid(self, form):
        # Always just display the template
        return self.form_invalid(form)

class EventDataLookupView(EventTriggerMixin, SingleObjectMixin,
                          ObjectLookupView):
    def get(self, *args, **kwargs):
        self.kwarg_name = kwargs.pop('name')
        return super(EventDataLookupView, self).get(*args, **kwargs)

    def get_lookup_queryset(self):
        if self.kwarg_name == 'user':
            self.model = get_user_model()
        else:
            event_data = self.event.eventdata_set.get(
                kwarg_name=self.kwarg_name)
            self.model = event_data.content_type.model_class()

        queryset = super(EventDataLookupView, self).get_lookup_queryset()

        if hasattr(queryset, "emarsys_kwarg_name_filter"):
            queryset = queryset.emarsys_kwarg_name_filter(self.kwarg_name)

        return queryset

    def format_object(self, obj):
        if not hasattr(obj, 'emarsys_format'):
            return super(EventDataLookupView, self).format_object(obj)

        return {
            'id': obj.pk,
            'text': obj.emarsys_format
        }

    def lookup_filter(self, queryset, query):
        if hasattr(queryset, 'emarsys_filter'):
            return queryset.emarsys_filter(query)
        return queryset.filter(pk=query)

class EventInstanceListView(SearchFormMixin, SearchMixin, SingleTableView):
    model = EventInstance
    table_class = EventInstanceTable
    template_name = 'dashboard/emarsys/event_instance_list.html'

    search = {
        'firstname': 'user__first_name__icontains',
        'lastname': 'user__last_name__icontains',
        'email': 'recipient_email__icontains',
        'event': 'event__name__icontains',
        'id': 'event__emarsys_id',
    }

    search_types = {
        'id': int
    }

class EventInstanceView(DetailView):
    model = EventInstance
    context_object_name = 'event_instance'
    template_name = 'dashboard/emarsys/event_instance.html'

    def get_context_data(self, **kwargs):
        context = super(EventInstanceView, self).get_context_data(**kwargs)
        context['placeholder_data'] = sorted(json.loads(
            context['event_instance'].context)['global'].items())
        return context
<|endoftext|>


<commit_before>__author__ = 'jerzydem'
import sys

def prepare_page_url(name):
    import urllib
    refine_name = name.replace('?', ' ').replace('-', ' ').replace('(', ' ').replace(')', ' ');
    # add name values to table
    names_table = refine_name.split()

    # prepare url query
    name_query = ''
    for token in names_table:
        # ignore token "ksiądz"
        if token != "ksiądz":
            name_query += urllib.parse.quote(token)
            name_query += "+"

    encoded_url = "https://bs.sejm.gov.pl/F/?func=find-b&request=" + name_query + \
                  "&find_code=WPN&adjacent=N&x=27&y=6&local_base=ars10"
    return encoded_url

def get_page(name, index):
    # init page url preparation and save html in destination folder

    import urllib.request
    import urllib.parse
    import sys
    import time
    from slugify import slugify

    DESTINATION_FOLDER = 'results3/'

    url = prepare_page_url(name)

    response = urllib.request.urlopen(url)
    web_content = response.read()
    size = sys.getsizeof(web_content)

    # 5 seconds timeout to limit server pauses
    time.sleep(5)

    # detect if server is down. When server is down, result pages are very small
    if size < 1000:
        # BS Server down
        print('SERVER OFF {:>4} {} SIZE: {}'.format(index, encoded_url, size))
        time.sleep(5)
        get_page(name, index)
    else:
        # Page size is enough. Save page
        print(index, " Name: ", name, " ", url)
        f = open(DESTINATION_FOLDER + str(index) + "_" + slugify(name) + ".html", 'wb')
        f.write(web_content)
        f.close

def csv_get_all_pages(csv_file, start_line):
    # get names of members of Sejm from csv file and init single page getter
    import csv

    input_file = open(csv_file)
    # open csv and get rows
    reader = csv.reader(input_file, delimiter=',')

    rows_num = 0
    for row in reader:
        # ignore row if its number is lower then start_line (to start from specific number)
        if rows_num > start_line-1:
            name = row[1]
            get_page(name, rows_num)
        rows_num += 1

    input_file.close()

def init_getter():
    # set init values and start getter

    # CONSTANT VALUES
    DEPUTIES_CSV = "files/sejm_ustawodawczy.csv"
    START_LINE = 0

    # INLINE ARGUMENTS
    # use inline arguments if exist
    start_row = int(sys.argv[1]) if len(sys.argv) > 1 else START_LINE
    input_csv = sys.argv[2] if len(sys.argv) > 2 else DEPUTIES_CSV

    csv_get_all_pages(input_csv, start_row)

init_getter()
<commit_msg>add getter hotfix<commit_after>__author__ = 'jerzydem'
import sys

def prepare_page_url(name):
    import urllib
    refine_name = name.replace('?', ' ').replace('-', ' ').replace('(', ' ').replace(')', ' ');
    # add name values to table
    names_table = refine_name.split()

    # prepare url query
    name_query = ''
    for token in names_table:
        # ignore token "ksiądz"
        if token != "ksiądz":
            name_query += urllib.parse.quote(token)
            name_query += "+"

    encoded_url = "https://bs.sejm.gov.pl/F/?func=find-b&request=" + name_query + \
                  "&find_code=WPN&adjacent=N&x=27&y=6&local_base=ars10"
    return encoded_url

def get_page(name, index):
    # init page url preparation and save html in destination folder

    import urllib.request
    import urllib.parse
    import sys
    import time
    from slugify import slugify

    DESTINATION_FOLDER = 'results3/'

    url = prepare_page_url(name)

    response = urllib.request.urlopen(url)
    web_content = response.read()
    size = sys.getsizeof(web_content)

    # 5 seconds timeout to limit server pauses
    time.sleep(5)

    # detect if server is down. When server is down, result pages are very small
    if size < 1000:
        # BS Server down
        print('SERVER OFF {:>4} {} SIZE: {}'.format(index, url, size))
        time.sleep(5)
        get_page(name, index)
    else:
        # Page size is enough. Save page
        print(index, " Name: ", name, " ", url)
        f = open(DESTINATION_FOLDER + str(index) + "_" + slugify(name) + ".html", 'wb')
        f.write(web_content)
        f.close

def csv_get_all_pages(csv_file, start_line):
    # get names of members of Sejm from csv file and init single page getter
    import csv

    input_file = open(csv_file)
    # open csv and get rows
    reader = csv.reader(input_file, delimiter=',')

    rows_num = 0
    for row in reader:
        # ignore row if its number is lower then start_line (to start from specific number)
        if rows_num > start_line-1:
            name = row[1]
            get_page(name, rows_num)
        rows_num += 1

    input_file.close()

def init_getter():
    # set init values and start getter

    # CONSTANT VALUES
    DEPUTIES_CSV = "files/sejm_ustawodawczy.csv"
    START_LINE = 0

    # INLINE ARGUMENTS
    # use inline arguments if exist
    start_row = int(sys.argv[1]) if len(sys.argv) > 1 else START_LINE
    input_csv = sys.argv[2] if len(sys.argv) > 2 else DEPUTIES_CSV

    csv_get_all_pages(input_csv, start_row)

init_getter()
<|endoftext|>


<commit_before>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2002-2007 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Christian Reis <kiko@async.com.br>
#            Lorenzo Gil Sanchez <lgs@sicem.biz>
#            Gustavo Rahal <gustavo@async.com.br>
#            Johan Dahlin <jdahlin@async.com.br>
#

"""This module defines the Proxy class, which is a facility that can be used
to keep the state of a model object synchronized with a View.
"""

import gobject
import gtk

from kiwi import ValueUnset
from kiwi.accessor import kgetattr, ksetattr, clear_attr_cache
from kiwi.datatypes import converter
from kiwi.decorators import deprecated
from kiwi.interfaces import IProxyWidget, IValidatableProxyWidget
from kiwi.log import Logger

log = Logger('proxy')

class ProxyError(Exception):
    pass

def block_widget(widget):
    """Blocks the signal handler of the 'content-changed' signal on widget"""
    connection_id = widget.get_data('content-changed-id')
    if connection_id:
        widget.handler_block(connection_id)

def unblock_widget(widget):
    """Unblocks the signal handler of the 'content-changed' signal on widget"""
    connection_id = widget.get_data('content-changed-id')
    if connection_id:
        widget.handler_unblock(connection_id)

def _get_widget_data_type(widget):
    data_type = widget.get_property('data-type')
    c = converter.get_converter(data_type)
    return c.type

class Proxy:
    """ A Proxy is a class that 'attaches' an instance to an interface's
    widgets, and transparently manipulates that instance's attributes as
    the user alters the content of the widgets.

    The Proxy takes the widget list and detects what widgets are to be
    attached to the model by looking if it is a KiwiWidget and if it
    has the model-attribute set.
    """

    def __init__(self, view, model=None, widgets=()):
        """
        Create a new Proxy object.
        :param view:    view attched to the slave
        :type  view:    a :class:`kiwi.ui.views.BaseView` subclass
        :param model:   model attached to proxy
        :param widgets: the widget names
        :type  widgets: list of strings
        """
        self._view = view
        self._model = model
        self._model_attributes = {}

        for widget_name in widgets:
            widget = getattr(self._view, widget_name, None)
            if widget is None:
                raise AttributeError("The widget %s was not found in the "
                                     "view %s" % (
                    widget_name, self._view.__class__.__name__))

            self._setup_widget(widget_name, widget)

    # Private API

    def _reset_widget(self, attribute, widget):
        if self._model is None:
            # if we have no model, leave value unset so we pick up
            # the widget default below.
            value = ValueUnset
        else:
            # if we have a model, grab its value to update the widgets
            self._register_proxy_in_model(attribute)
            value = kgetattr(self._model, attribute, ValueUnset)

        self.update(attribute, value, block=True)

        # The initial value of the model is set, at this point
        # do a read, it'll trigger a validation for widgets who
        # supports it.
        if not IValidatableProxyWidget.providedBy(widget):
            return

        widget.validate(force=True)

    def _setup_widget(self, widget_name, widget):
        if not IProxyWidget.providedBy(widget):
            raise ProxyError("The widget %s (%r), in view %s is not "
                             "a kiwi widget and cannot be added to a proxy"
                             % (widget_name, widget,
                                self._view.__class__.__name__))

        data_type = _get_widget_data_type(widget)
        if data_type is None:
            raise ProxyError("The kiwi widget %s (%r) in view %s should "
                             "have a data type set" % (
                widget_name, widget, self._view.__class__.__name__))

        attribute = widget.get_property('model-attribute')
        if not attribute:
            attribute = widget_name
            widget.set_property('model-attribute', widget_name)

        # Do a isinstance here instead of in the callback,
        # as an optimization, it'll never change in runtime anyway
        connection_id = widget.connect(
            'content-changed',
            self._on_widget__content_changed,
            attribute,
            IValidatableProxyWidget.providedBy(widget))
        widget.set_data('content-changed-id', connection_id)

        if IValidatableProxyWidget.providedBy(widget):
            connection_id = widget.connect(
                'notify::visible',
                self._on_widget__notify)
            widget.set_data('notify-visible-id', connection_id)

            connection_id = widget.connect(
                'notify::sensitive',
                self._on_widget__notify)
            widget.set_data('notify-sensitive-id', connection_id)

        model_attributes = self._model_attributes
        # save this widget in our map
        if (attribute in model_attributes and
            # RadioButtons are allowed several times
            not gobject.type_is_a(widget, 'GtkRadioButton')):
            old_widget = model_attributes[attribute]
            raise KeyError("The widget %s (%r) in view %s is already in "
                           "the proxy, defined by widget %s (%r)" % (
                widget_name, widget, self._view.__class__.__name__,
                old_widget.name, old_widget))

        model_attributes[attribute] = widget
        self._reset_widget(attribute, widget)

    def _register_proxy_in_model(self, attribute):
        model = self._model
        if not hasattr(model, "register_proxy_for_attribute"):
            return
        try:
            model.register_proxy_for_attribute(attribute, self)
        except AttributeError:
            msg = ("Failed to run register_proxy() on Model %s "
                   "(that was supplied to  %s. \n"
                   "(Hint: if this model also inherits from ZODB's "
                   "Persistent class, this problem occurs if you haven't "
                   "set __setstate__() up correctly.  __setstate__() "
                   "should call Model.__init__() (and "
                   "Persistent.__setstate__() of course) to rereset "
                   "things properly.)")
            raise TypeError(msg % (model, self))

    def _unregister_proxy_in_model(self):
        if self._model and hasattr(self._model, "unregister_proxy"):
            self._model.unregister_proxy(self)

    # Callbacks

    def _on_widget__content_changed(self, widget, attribute, validate):
        """This is called as soon as the content of one of the widget
        changes, the widgets tries fairly hard to not emit when it's not
        neccessary"""

        # skip updates for model if there is none, right?
        if self._model is None:
            return

        if validate:
            value = widget.validate()
        else:
            value = widget.read()

        log('%s.%s = %r' % (self._model.__class__.__name__,
                            attribute, value))

        # only update the model if the data is correct
        if value is ValueUnset:
            return

        model = self._model
        # XXX: one day we might want to queue and unique updates?
        if hasattr(model, "block_proxy"):
            model.block_proxy(self)
            ksetattr(model, attribute, value)
            model.unblock_proxy(self)
        else:
            ksetattr(model, attribute, value)

        # Call global update hook
        self.proxy_updated(widget, attribute, value)

    # notify::sensitive and notify::visible are connected here
    def _on_widget__notify(self, widget, pspec):
        widget.emit('validation-changed', widget.is_valid())

    # Properties

    def _get_model(self):
        return self._model
    model = property(_get_model)

    # Public API

    def proxy_updated(self, widget, attribute, value):
        """ This is a hook that is called whenever the proxy updates the
        model. Implement it in the inherited class to perform actions that
        should be done each time the user changes something in the interface.
        This hook by default does nothing.
        :param widget:
        :param attribute:
        :param value:
        """

    def update_many(self, attributes, value=ValueUnset, block=False):
        """
        Like :class:`update` but takes a sequence of attributes

        :param attributes: sequence of attributes to update
        :param value: see :class:`update`
        :param block: see :class:`update`
        """

        for attribute in attributes:
            self.update(attribute, value, block)

    def update(self, attribute, value=ValueUnset, block=False):
        """ Generic frontend function to update the contentss of a widget based
        on its model attribute name using the internal update functions.

        :param attribute: the name of the attribute whose widget we wish to
          updated.  If accessing a radiobutton, specify its group
          name.
        :param value: specifies the value to set in the widget. If
          unspecified, it defaults to the current model's value
          (through an accessor, if it exists, or getattr).
        :param block: defines if we are to block cascading proxy updates
          triggered by this update. You should use block if you are
          calling update on *the same attribute that is currently
          being updated*.
          This means if you have hooked to a signal of the widget
          associated to that attribute, and you call update() for
          the *same attribute*, use block=True. And pray. 8). If
          block is set to False, the normal update mechanism will
          occur (the model being updated in the end, hopefully).
        """

        if value is ValueUnset:
        # We want to obtain a value from our model
            if self._model is None:
                # We really want to avoid trying to update our UI if our
                # model doesn't exist yet and no value was provided.
                # update() is also called by user code, but it should be
                # safe to return here since you shouldn't need to code
                # around the lack of a model in your callbacks if you
                # can help it.
                value = ValueUnset
            else:
                value = kgetattr(self._model, attribute, ValueUnset)

        widget = self._model_attributes.get(attribute, None)

        if widget is None:
            raise AttributeError("Called update for `%s', which isn't "
                                 "attached to the proxy %s. Valid "
                                 "attributes are: %s (you may have "
                                 "forgetten to add `:' to the name in "
                                 "the widgets list)"
                                 % (attribute, self,
                                    self._model_attributes.keys()))

        # The type of value should match the data-type property. The two
        # exceptions to this rule are ValueUnset and None
        if not (value is ValueUnset or value is None):
            data_type = _get_widget_data_type(widget)
            value_type = type(value)
            if not isinstance(value, data_type):
                raise TypeError(
                    "attribute %s of model %r requires a value of "
                    "type %s, not %s" % (
                    attribute, self._model,
                    data_type.__name__,
                    value_type.__name__))

        if block:
            block_widget(widget)
            self._view.handler_block(widget)
            widget.update(value)
            self._view.handler_unblock(widget)
            unblock_widget(widget)
        else:
            widget.update(value)
        return True

    def set_model(self, model, relax_type=False):
        """
        Updates the model instance of the proxy.
        Allows a proxy interface to change model without the need to destroy
        and recreate the UI (which would cause flashing, at least)

        :param model:
        :param relax_type:
        """
        if self._model is not None and model is not None:
            if (not relax_type and
                type(model) != type(self._model) and
                not isinstance(model, self._model.__class__)):
                raise TypeError("model has wrong type %s, expected %s"
                                % (type(model), type(self._model)))

        # the following isn't strictly necessary, but it currently works
        # around a bug with reused ids in the attribute cache and also
        # makes a lot of sense for most applications (don't want a huge
        # eternal cache pointing to models that you're not using anyway)
        clear_attr_cache()

        # unregister previous proxy
        self._unregister_proxy_in_model()

        self._model = model

        for attribute, widget in self._model_attributes.items():
            self._reset_widget(attribute, widget)

    def add_widget(self, name, widget):
        """
        Adds a new widget to the proxy

        :param name: name of the widget
        :param widget: widget, must be a gtk.Widget subclass
        """
        if name in self._model_attributes:
            raise TypeError("there is already a widget called %s" % name)

        if not isinstance(widget, gtk.Widget):
            raise TypeError("%r must be a gtk.Widget subclass" % widget)

        self._setup_widget(name, widget)

    def remove_widget(self, name):
        """
        Removes a widget from the proxy

        :param name: the name of the widget to remove
        """
        if not name in self._model_attributes:
            raise TypeError("there is no widget called %s" % name)

        widget = self._model_attributes.pop(name)
        widget.disconnect(widget.get_data('content-changed-id'))

        if IValidatableProxyWidget.providedBy(widget):
            for data_name in ('notify-visible-id',
                              'notify-sensitive-id'):
                widget.disconnect(widget.get_data(data_name))

    # Backwards compatibility

    def new_model(self, model, relax_type=False):
        self.set_model(model)
    new_model = deprecated('set_model', log)(new_model)
<commit_msg>accept unicode for strings and strings for unicode<commit_after>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2002-2007 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Christian Reis <kiko@async.com.br>
#            Lorenzo Gil Sanchez <lgs@sicem.biz>
#            Gustavo Rahal <gustavo@async.com.br>
#            Johan Dahlin <jdahlin@async.com.br>
#

"""This module defines the Proxy class, which is a facility that can be used
to keep the state of a model object synchronized with a View.
"""

import gobject
import gtk

from kiwi import ValueUnset
from kiwi.accessor import kgetattr, ksetattr, clear_attr_cache
from kiwi.datatypes import converter
from kiwi.decorators import deprecated
from kiwi.interfaces import IProxyWidget, IValidatableProxyWidget
from kiwi.log import Logger

log = Logger('proxy')

class ProxyError(Exception):
    pass

def block_widget(widget):
    """Blocks the signal handler of the 'content-changed' signal on widget"""
    connection_id = widget.get_data('content-changed-id')
    if connection_id:
        widget.handler_block(connection_id)

def unblock_widget(widget):
    """Unblocks the signal handler of the 'content-changed' signal on widget"""
    connection_id = widget.get_data('content-changed-id')
    if connection_id:
        widget.handler_unblock(connection_id)

def _get_widget_data_type(widget):
    data_type = widget.get_property('data-type')
    c = converter.get_converter(data_type)
    if c.type is str or c.type is unicode:
        return basestring
    return c.type

class Proxy:
    """ A Proxy is a class that 'attaches' an instance to an interface's
    widgets, and transparently manipulates that instance's attributes as
    the user alters the content of the widgets.

    The Proxy takes the widget list and detects what widgets are to be
    attached to the model by looking if it is a KiwiWidget and if it
    has the model-attribute set.
    """

    def __init__(self, view, model=None, widgets=()):
        """
        Create a new Proxy object.
        :param view:    view attched to the slave
        :type  view:    a :class:`kiwi.ui.views.BaseView` subclass
        :param model:   model attached to proxy
        :param widgets: the widget names
        :type  widgets: list of strings
        """
        self._view = view
        self._model = model
        self._model_attributes = {}

        for widget_name in widgets:
            widget = getattr(self._view, widget_name, None)
            if widget is None:
                raise AttributeError("The widget %s was not found in the "
                                     "view %s" % (
                    widget_name, self._view.__class__.__name__))

            self._setup_widget(widget_name, widget)

    # Private API

    def _reset_widget(self, attribute, widget):
        if self._model is None:
            # if we have no model, leave value unset so we pick up
            # the widget default below.
            value = ValueUnset
        else:
            # if we have a model, grab its value to update the widgets
            self._register_proxy_in_model(attribute)
            value = kgetattr(self._model, attribute, ValueUnset)

        self.update(attribute, value, block=True)

        # The initial value of the model is set, at this point
        # do a read, it'll trigger a validation for widgets who
        # supports it.
        if not IValidatableProxyWidget.providedBy(widget):
            return

        widget.validate(force=True)

    def _setup_widget(self, widget_name, widget):
        if not IProxyWidget.providedBy(widget):
            raise ProxyError("The widget %s (%r), in view %s is not "
                             "a kiwi widget and cannot be added to a proxy"
                             % (widget_name, widget,
                                self._view.__class__.__name__))

        data_type = _get_widget_data_type(widget)
        if data_type is None:
            raise ProxyError("The kiwi widget %s (%r) in view %s should "
                             "have a data type set" % (
                widget_name, widget, self._view.__class__.__name__))

        attribute = widget.get_property('model-attribute')
        if not attribute:
            attribute = widget_name
            widget.set_property('model-attribute', widget_name)

        # Do a isinstance here instead of in the callback,
        # as an optimization, it'll never change in runtime anyway
        connection_id = widget.connect(
            'content-changed',
            self._on_widget__content_changed,
            attribute,
            IValidatableProxyWidget.providedBy(widget))
        widget.set_data('content-changed-id', connection_id)

        if IValidatableProxyWidget.providedBy(widget):
            connection_id = widget.connect(
                'notify::visible',
                self._on_widget__notify)
            widget.set_data('notify-visible-id', connection_id)

            connection_id = widget.connect(
                'notify::sensitive',
                self._on_widget__notify)
            widget.set_data('notify-sensitive-id', connection_id)

        model_attributes = self._model_attributes
        # save this widget in our map
        if (attribute in model_attributes and
            # RadioButtons are allowed several times
            not gobject.type_is_a(widget, 'GtkRadioButton')):
            old_widget = model_attributes[attribute]
            raise KeyError("The widget %s (%r) in view %s is already in "
                           "the proxy, defined by widget %s (%r)" % (
                widget_name, widget, self._view.__class__.__name__,
                old_widget.name, old_widget))

        model_attributes[attribute] = widget
        self._reset_widget(attribute, widget)

    def _register_proxy_in_model(self, attribute):
        model = self._model
        if not hasattr(model, "register_proxy_for_attribute"):
            return
        try:
            model.register_proxy_for_attribute(attribute, self)
        except AttributeError:
            msg = ("Failed to run register_proxy() on Model %s "
                   "(that was supplied to  %s. \n"
                   "(Hint: if this model also inherits from ZODB's "
                   "Persistent class, this problem occurs if you haven't "
                   "set __setstate__() up correctly.  __setstate__() "
                   "should call Model.__init__() (and "
                   "Persistent.__setstate__() of course) to rereset "
                   "things properly.)")
            raise TypeError(msg % (model, self))

    def _unregister_proxy_in_model(self):
        if self._model and hasattr(self._model, "unregister_proxy"):
            self._model.unregister_proxy(self)

    # Callbacks

    def _on_widget__content_changed(self, widget, attribute, validate):
        """This is called as soon as the content of one of the widget
        changes, the widgets tries fairly hard to not emit when it's not
        neccessary"""

        # skip updates for model if there is none, right?
        if self._model is None:
            return

        if validate:
            value = widget.validate()
        else:
            value = widget.read()

        log('%s.%s = %r' % (self._model.__class__.__name__,
                            attribute, value))

        # only update the model if the data is correct
        if value is ValueUnset:
            return

        model = self._model
        # XXX: one day we might want to queue and unique updates?
        if hasattr(model, "block_proxy"):
            model.block_proxy(self)
            ksetattr(model, attribute, value)
            model.unblock_proxy(self)
        else:
            ksetattr(model, attribute, value)

        # Call global update hook
        self.proxy_updated(widget, attribute, value)

    # notify::sensitive and notify::visible are connected here
    def _on_widget__notify(self, widget, pspec):
        widget.emit('validation-changed', widget.is_valid())

    # Properties

    def _get_model(self):
        return self._model
    model = property(_get_model)

    # Public API

    def proxy_updated(self, widget, attribute, value):
        """ This is a hook that is called whenever the proxy updates the
        model. Implement it in the inherited class to perform actions that
        should be done each time the user changes something in the interface.
        This hook by default does nothing.
        :param widget:
        :param attribute:
        :param value:
        """

    def update_many(self, attributes, value=ValueUnset, block=False):
        """
        Like :class:`update` but takes a sequence of attributes

        :param attributes: sequence of attributes to update
        :param value: see :class:`update`
        :param block: see :class:`update`
        """

        for attribute in attributes:
            self.update(attribute, value, block)

    def update(self, attribute, value=ValueUnset, block=False):
        """ Generic frontend function to update the contentss of a widget based
        on its model attribute name using the internal update functions.

        :param attribute: the name of the attribute whose widget we wish to
          updated.  If accessing a radiobutton, specify its group
          name.
        :param value: specifies the value to set in the widget. If
          unspecified, it defaults to the current model's value
          (through an accessor, if it exists, or getattr).
        :param block: defines if we are to block cascading proxy updates
          triggered by this update. You should use block if you are
          calling update on *the same attribute that is currently
          being updated*.
          This means if you have hooked to a signal of the widget
          associated to that attribute, and you call update() for
          the *same attribute*, use block=True. And pray. 8). If
          block is set to False, the normal update mechanism will
          occur (the model being updated in the end, hopefully).
        """

        if value is ValueUnset:
        # We want to obtain a value from our model
            if self._model is None:
                # We really want to avoid trying to update our UI if our
                # model doesn't exist yet and no value was provided.
                # update() is also called by user code, but it should be
                # safe to return here since you shouldn't need to code
                # around the lack of a model in your callbacks if you
                # can help it.
                value = ValueUnset
            else:
                value = kgetattr(self._model, attribute, ValueUnset)

        widget = self._model_attributes.get(attribute, None)

        if widget is None:
            raise AttributeError("Called update for `%s', which isn't "
                                 "attached to the proxy %s. Valid "
                                 "attributes are: %s (you may have "
                                 "forgetten to add `:' to the name in "
                                 "the widgets list)"
                                 % (attribute, self,
                                    self._model_attributes.keys()))

        # The type of value should match the data-type property. The two
        # exceptions to this rule are ValueUnset and None
        if not (value is ValueUnset or value is None):
            data_type = _get_widget_data_type(widget)
            value_type = type(value)
            if not isinstance(value, data_type):
                raise TypeError(
                    "attribute %s of model %r requires a value of "
                    "type %s, not %s" % (
                    attribute, self._model,
                    data_type.__name__,
                    value_type.__name__))

        if block:
            block_widget(widget)
            self._view.handler_block(widget)
            widget.update(value)
            self._view.handler_unblock(widget)
            unblock_widget(widget)
        else:
            widget.update(value)
        return True

    def set_model(self, model, relax_type=False):
        """
        Updates the model instance of the proxy.
        Allows a proxy interface to change model without the need to destroy
        and recreate the UI (which would cause flashing, at least)

        :param model:
        :param relax_type:
        """
        if self._model is not None and model is not None:
            if (not relax_type and
                type(model) != type(self._model) and
                not isinstance(model, self._model.__class__)):
                raise TypeError("model has wrong type %s, expected %s"
                                % (type(model), type(self._model)))

        # the following isn't strictly necessary, but it currently works
        # around a bug with reused ids in the attribute cache and also
        # makes a lot of sense for most applications (don't want a huge
        # eternal cache pointing to models that you're not using anyway)
        clear_attr_cache()

        # unregister previous proxy
        self._unregister_proxy_in_model()

        self._model = model

        for attribute, widget in self._model_attributes.items():
            self._reset_widget(attribute, widget)

    def add_widget(self, name, widget):
        """
        Adds a new widget to the proxy

        :param name: name of the widget
        :param widget: widget, must be a gtk.Widget subclass
        """
        if name in self._model_attributes:
            raise TypeError("there is already a widget called %s" % name)

        if not isinstance(widget, gtk.Widget):
            raise TypeError("%r must be a gtk.Widget subclass" % widget)

        self._setup_widget(name, widget)

    def remove_widget(self, name):
        """
        Removes a widget from the proxy

        :param name: the name of the widget to remove
        """
        if not name in self._model_attributes:
            raise TypeError("there is no widget called %s" % name)

        widget = self._model_attributes.pop(name)
        widget.disconnect(widget.get_data('content-changed-id'))

        if IValidatableProxyWidget.providedBy(widget):
            for data_name in ('notify-visible-id',
                              'notify-sensitive-id'):
                widget.disconnect(widget.get_data(data_name))

    # Backwards compatibility

    def new_model(self, model, relax_type=False):
        self.set_model(model)
    new_model = deprecated('set_model', log)(new_model)
<|endoftext|>


<commit_before># Filename: calib.py
# pylint: disable=locally-disabled
"""
Calibration.

"""

import numpy as np
from pandas import DataFrame

from .core import Module
from .hardware import Detector
from .dataclasses import Table
from .dataclass_templates import TEMPLATES
from .logger import logging

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = ["Thomas Heid"]
__license__ = "MIT"
__maintainer__ = "Tamas Gal"
__email__ = "tgal@km3net.de"
__status__ = "Development"

log = logging.getLogger(__name__)  # pylint: disable=C0103
# log.setLevel(logging.DEBUG)

try:
    import numba as nb
except (ImportError, OSError):
    log.debug("No Numba support")
    HAVE_NUMBA = False
else:
    log.debug("Running with Numba support")
    HAVE_NUMBA = True

class Calibration(Module):
    """A very simple, preliminary Module which gives access to the calibration.

    Parameters
    ----------
    apply: bool, optional [default=False]
        Apply the calibration to the hits (add position/direction/t0)?
    filename: str, optional [default=None]
        DetX file with detector description.
    det_id: int, optional
        .detx ID of detector (when retrieving from database).
    t0set: optional
        t0set (when retrieving from database).
    calibration: optional
        calibration (when retrieving from database).
    """
    __name__ = 'Calibration'
    name = 'Calibration'

    def configure(self):
        self._should_apply = self.get('apply', default=False)
        self.filename = self.get('filename')
        self.det_id = self.get('det_id')
        self.t0set = self.get('t0set')
        self.calibration = self.get('calibration')
        self.detector = self.get('detector')
        self._pos_dom_channel = None
        self._dir_dom_channel = None
        self._t0_dom_channel = None
        self._pos_pmt_id = None
        self._dir_pmt_id = None
        self._t0_pmt_id = None
        self._lookup_tables = None  # for Numba

        if self.filename or self.det_id:
            if self.filename is not None:
                self.detector = Detector(filename=self.filename)
            if self.det_id:
                self.detector = Detector(det_id=self.det_id,
                                         t0set=self.t0set,
                                         calibration=self.calibration)

        if self.detector is not None:
            log.debug("Creating lookup tables")
            self._create_dom_channel_lookup()
            self._create_pmt_id_lookup()
        else:
            log.critical("No detector information loaded.")

    def process(self, blob, key='Hits', outkey='CalibHits'):
        if self._should_apply:
            blob[outkey] = self.apply(blob[key])
        return blob

    def get_detector(self):
        """Return the detector"""
        return self.detector

    def apply_t0(self, hits):
        """Apply only t0s"""
        if HAVE_NUMBA:
            apply_t0_nb(hits.time, hits.dom_id, hits.channel_id,
                        self._lookup_tables)
        else:
            n = len(hits)
            cal = np.empty(n)
            lookup = self._calib_by_dom_and_channel
            for i in range(n):
                calib = lookup[hits['dom_id']
                               [i]][hits['channel_id'][i]]
                cal[i] = calib[6]
            hits.time += cal
        return hits

    def apply(self, hits):
        """Add x, y, z, t0 (and du, floor if DataFrame) columns to the hits.

        """
        if isinstance(hits, DataFrame):
            # do we ever see McHits here?
            hits = Table.from_template(hits, 'Hits')
        if hasattr(hits, 'dom_id') and hasattr(hits, 'channel_id'):
            return self._apply_to_hits(hits)
        elif hasattr(hits, 'pmt_id'):
            return self._apply_to_mchits(hits)
        else:
            raise TypeError("Don't know how to apply calibration to '{0}'. "
                            "We need at least 'dom_id' and 'channel_id', or "
                            "'pmt_id'."
                            .format(hits.name))

    def _apply_to_hits(self, hits):
        """Append the position, direction and t0 columns and add t0 to time"""
        n = len(hits)
        cal = np.empty((n, 9))
        lookup = self._calib_by_dom_and_channel
        for i in range(n):
            calib = lookup[hits['dom_id'][i]][hits['channel_id'][i]]
            cal[i] = calib
        h = np.empty(n, TEMPLATES['CalibHits']['dtype'])
        h['channel_id'] = hits.channel_id
        h['dir_x'] = cal[:, 3]
        h['dir_y'] = cal[:, 4]
        h['dir_z'] = cal[:, 5]
        h['dom_id'] = hits.dom_id
        h['du'] = cal[:, 7]
        h['floor'] = cal[:, 8]
        h['pos_x'] = cal[:, 0]
        h['pos_y'] = cal[:, 1]
        h['pos_z'] = cal[:, 2]
        h['t0'] = cal[:, 6]
        h['time'] = hits.time + cal[:, 6]
        h['tot'] = hits.tot
        h['triggered'] = hits.triggered
        h['group_id'] = hits['group_id']
        return Table.from_template(h, 'CalibHits')

    def _apply_to_mchits(self, hits):
        """Append the position, direction and t0 columns and add t0 to time"""
        n_hits = len(hits)
        cal = np.empty((n_hits, 9))
        for i in range(n_hits):
            lookup = self._calib_by_pmt_id
            cal[i] = lookup[hits['pmt_id'][i]]
        dir_x = cal[:, 3]
        dir_y = cal[:, 4]
        dir_z = cal[:, 5]
        du = cal[:, 7]
        floor = cal[:, 8]
        pos_x = cal[:, 0]
        pos_y = cal[:, 1]
        pos_z = cal[:, 2]
        t0 = cal[:, 6]

        hits.time += t0

        return hits.append_columns(
            ['dir_x', 'dir_y', 'dir_z', 'du', 'floor',
             'pos_x', 'pos_y', 'pos_z', 't0'],
            [dir_x, dir_y, dir_z, du, floor, pos_x, pos_y, pos_z, t0]
        )

    def _create_dom_channel_lookup(self):
        data = {}
        for dom_id, pmts in self.detector._pmts_by_dom_id.items():
            for pmt in pmts:
                if dom_id not in data:
                    data[dom_id] = np.zeros((31, 9))
                data[dom_id][pmt.channel_id] = [pmt.pos[0],
                                                pmt.pos[1],
                                                pmt.pos[2],
                                                pmt.dir[0],
                                                pmt.dir[1],
                                                pmt.dir[2],
                                                pmt.t0,
                                                pmt.omkey[0],
                                                pmt.omkey[1]]
        self._calib_by_dom_and_channel = data
        if HAVE_NUMBA:
            self._lookup_tables = [(dom, cal) for dom, cal in data.items()]

    def _create_pmt_id_lookup(self):
        data = {}
        for pmt_id, pmt in self.detector._pmts_by_id.items():
            data[pmt_id] = np.array((pmt.pos[0],
                                     pmt.pos[1],
                                     pmt.pos[2],
                                     pmt.dir[0],
                                     pmt.dir[1],
                                     pmt.dir[2],
                                     pmt.t0,
                                     pmt.omkey[0],
                                     pmt.omkey[1],
                                     ))
        self._calib_by_pmt_id = data

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return "Calibration: det_id({0})".format(self.det_id)

if HAVE_NUMBA:
    log.info("Initialising Numba JIT functions")

    @nb.jit
    def apply_t0_nb(times, dom_ids, channel_ids, lookup_tables):
        """Apply t0s using a lookup table of tuples (dom_id, calib)"""
        dom_id = 0
        lookup = np.empty((31, 9))
        for i in range(len(times)):
            cur_dom_id = dom_ids[i]
            if cur_dom_id != dom_id:
                dom_id = cur_dom_id
                for (d, m) in lookup_tables:
                    if d == dom_id:
                        np.copyto(lookup, m)
            t0 = lookup[channel_ids[i]][6]
            times[i] += t0
<commit_msg>Use module logger<commit_after># Filename: calib.py
# pylint: disable=locally-disabled
"""
Calibration.

"""

import numpy as np
from pandas import DataFrame

from .core import Module
from .hardware import Detector
from .dataclasses import Table
from .dataclass_templates import TEMPLATES

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = ["Thomas Heid"]
__license__ = "MIT"
__maintainer__ = "Tamas Gal"
__email__ = "tgal@km3net.de"
__status__ = "Development"

try:
    import numba as nb
except (ImportError, OSError):
    HAVE_NUMBA = False
else:
    HAVE_NUMBA = True

class Calibration(Module):
    """A very simple, preliminary Module which gives access to the calibration.

    Parameters
    ----------
    apply: bool, optional [default=False]
        Apply the calibration to the hits (add position/direction/t0)?
    filename: str, optional [default=None]
        DetX file with detector description.
    det_id: int, optional
        .detx ID of detector (when retrieving from database).
    t0set: optional
        t0set (when retrieving from database).
    calibration: optional
        calibration (when retrieving from database).
    """
    __name__ = 'Calibration'
    name = 'Calibration'

    def configure(self):
        self._should_apply = self.get('apply', default=False)
        self.filename = self.get('filename')
        self.det_id = self.get('det_id')
        self.t0set = self.get('t0set')
        self.calibration = self.get('calibration')
        self.detector = self.get('detector')
        self._pos_dom_channel = None
        self._dir_dom_channel = None
        self._t0_dom_channel = None
        self._pos_pmt_id = None
        self._dir_pmt_id = None
        self._t0_pmt_id = None
        self._lookup_tables = None  # for Numba

        if self.filename or self.det_id:
            if self.filename is not None:
                self.detector = Detector(filename=self.filename)
            if self.det_id:
                self.detector = Detector(det_id=self.det_id,
                                         t0set=self.t0set,
                                         calibration=self.calibration)

        if self.detector is not None:
            self.log.debug("Creating lookup tables")
            self._create_dom_channel_lookup()
            self._create_pmt_id_lookup()
        else:
            self.log.critical("No detector information loaded.")

    def process(self, blob, key='Hits', outkey='CalibHits'):
        if self._should_apply:
            blob[outkey] = self.apply(blob[key])
        return blob

    def get_detector(self):
        """Return the detector"""
        return self.detector

    def apply_t0(self, hits):
        """Apply only t0s"""
        if HAVE_NUMBA:
            apply_t0_nb(hits.time, hits.dom_id, hits.channel_id,
                        self._lookup_tables)
        else:
            n = len(hits)
            cal = np.empty(n)
            lookup = self._calib_by_dom_and_channel
            for i in range(n):
                calib = lookup[hits['dom_id']
                               [i]][hits['channel_id'][i]]
                cal[i] = calib[6]
            hits.time += cal
        return hits

    def apply(self, hits):
        """Add x, y, z, t0 (and du, floor if DataFrame) columns to the hits.

        """
        if isinstance(hits, DataFrame):
            # do we ever see McHits here?
            hits = Table.from_template(hits, 'Hits')
        if hasattr(hits, 'dom_id') and hasattr(hits, 'channel_id'):
            return self._apply_to_hits(hits)
        elif hasattr(hits, 'pmt_id'):
            return self._apply_to_mchits(hits)
        else:
            raise TypeError("Don't know how to apply calibration to '{0}'. "
                            "We need at least 'dom_id' and 'channel_id', or "
                            "'pmt_id'."
                            .format(hits.name))

    def _apply_to_hits(self, hits):
        """Append the position, direction and t0 columns and add t0 to time"""
        n = len(hits)
        cal = np.empty((n, 9))
        lookup = self._calib_by_dom_and_channel
        for i in range(n):
            calib = lookup[hits['dom_id'][i]][hits['channel_id'][i]]
            cal[i] = calib
        h = np.empty(n, TEMPLATES['CalibHits']['dtype'])
        h['channel_id'] = hits.channel_id
        h['dir_x'] = cal[:, 3]
        h['dir_y'] = cal[:, 4]
        h['dir_z'] = cal[:, 5]
        h['dom_id'] = hits.dom_id
        h['du'] = cal[:, 7]
        h['floor'] = cal[:, 8]
        h['pos_x'] = cal[:, 0]
        h['pos_y'] = cal[:, 1]
        h['pos_z'] = cal[:, 2]
        h['t0'] = cal[:, 6]
        h['time'] = hits.time + cal[:, 6]
        h['tot'] = hits.tot
        h['triggered'] = hits.triggered
        h['group_id'] = hits['group_id']
        return Table.from_template(h, 'CalibHits')

    def _apply_to_mchits(self, hits):
        """Append the position, direction and t0 columns and add t0 to time"""
        n_hits = len(hits)
        cal = np.empty((n_hits, 9))
        for i in range(n_hits):
            lookup = self._calib_by_pmt_id
            cal[i] = lookup[hits['pmt_id'][i]]
        dir_x = cal[:, 3]
        dir_y = cal[:, 4]
        dir_z = cal[:, 5]
        du = cal[:, 7]
        floor = cal[:, 8]
        pos_x = cal[:, 0]
        pos_y = cal[:, 1]
        pos_z = cal[:, 2]
        t0 = cal[:, 6]

        hits.time += t0

        return hits.append_columns(
            ['dir_x', 'dir_y', 'dir_z', 'du', 'floor',
             'pos_x', 'pos_y', 'pos_z', 't0'],
            [dir_x, dir_y, dir_z, du, floor, pos_x, pos_y, pos_z, t0]
        )

    def _create_dom_channel_lookup(self):
        data = {}
        for dom_id, pmts in self.detector._pmts_by_dom_id.items():
            for pmt in pmts:
                if dom_id not in data:
                    data[dom_id] = np.zeros((31, 9))
                data[dom_id][pmt.channel_id] = [pmt.pos[0],
                                                pmt.pos[1],
                                                pmt.pos[2],
                                                pmt.dir[0],
                                                pmt.dir[1],
                                                pmt.dir[2],
                                                pmt.t0,
                                                pmt.omkey[0],
                                                pmt.omkey[1]]
        self._calib_by_dom_and_channel = data
        if HAVE_NUMBA:
            self._lookup_tables = [(dom, cal) for dom, cal in data.items()]

    def _create_pmt_id_lookup(self):
        data = {}
        for pmt_id, pmt in self.detector._pmts_by_id.items():
            data[pmt_id] = np.array((pmt.pos[0],
                                     pmt.pos[1],
                                     pmt.pos[2],
                                     pmt.dir[0],
                                     pmt.dir[1],
                                     pmt.dir[2],
                                     pmt.t0,
                                     pmt.omkey[0],
                                     pmt.omkey[1],
                                     ))
        self._calib_by_pmt_id = data

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return "Calibration: det_id({0})".format(self.det_id)

if HAVE_NUMBA:
    @nb.jit
    def apply_t0_nb(times, dom_ids, channel_ids, lookup_tables):
        """Apply t0s using a lookup table of tuples (dom_id, calib)"""
        dom_id = 0
        lookup = np.empty((31, 9))
        for i in range(len(times)):
            cur_dom_id = dom_ids[i]
            if cur_dom_id != dom_id:
                dom_id = cur_dom_id
                for (d, m) in lookup_tables:
                    if d == dom_id:
                        np.copyto(lookup, m)
            t0 = lookup[channel_ids[i]][6]
            times[i] += t0
<|endoftext|>