import urllib.request
from bs4 import BeautifulSoup
from termcolor import colored

def get_html(url):
   req = urllib.request.Request(url, headers={'User_Agent':'Mozilla/5.0'})
   return urllib.request.urlopen(req)

def list_titles(url):
   titles = []
   soup = BeautifulSoup(get_html(url))
   for link in soup.findAll('span'):
      if 'title' in str(link.get('class')):
         game_title = str(link)
         titles.append(game_title[20:-7])
   return titles

def list_discounts(url):
   discounts = []
   soup = BeautifulSoup(get_html(url))
   for link in soup.findAll('div'):
      if 'search_discount' in str(link.get('class')):
         game_discount = str(link)
         discounts.append(''.join(c for c in game_discount if c in '-%0123456789'))
   return discounts

def list_original_prices(url):
   original_prices = []
   soup = BeautifulSoup(get_html(url))
   for link in soup.findAll('strike'):
      if '€' in str(link):
         price = str(link)
         original_prices.append(''.join(c for c in price if c in ',€0123456789'))
   return original_prices

def get_links():
   base_url = 'http://store.steampowered.com/search/?specials=1&os=win#sort_by=_ASC&os=win%2Cmac%2Clinux&specials=1&page=1'
   links = [base_url]
   soup = BeautifulSoup(get_html(base_url))
   for link in soup.findAll('a'):
      if 'SearchLinkClick' in str(link.get('onclick')):
         href = str(link.get('href'))
         links.append(href)
   return links[:-1]

def to_float(d):
   return float(''.join(c for c in d if c in '0123456789'))

def get_discounted_price(price, discount):
   return to_float(price)/100.0 - ((to_float(discount)/100.0) * to_float(price))/100.0

for url in get_links():
   games = zip(list_titles(url), list_discounts(url), list_original_prices(url))
   for title, discount, o_price in games:
      if to_float(discount) < 50:
         print(title, colored(discount, 'blue') + ' from ' + o_price + ' to ' + '%.2f€' % (get_discounted_price(o_price, discount)))
      elif to_float(discount) > 50 and to_float(discount) < 75:
         print(title, colored(discount, 'yellow') + ' from ' + o_price + ' to ' + '%.2f€' % (get_discounted_price(o_price, discount)))
      elif to_float(discount) > 75:
         print(title, colored(discount, 'green') + ' from ' + o_price + ' to ' + '%.2f€' % (get_discounted_price(o_price, discount)))


import statistics

def readNumbersFromFile(x):
    a=open(x,"r")
    b=0
    c=0
    lista=[]
    for i in a:
        b=b+int(i)
        c=c+1
        list.append(int(i))
    st=statistics.stdev(list)
    print("The total of the value is: ",b)
    print("The number of the value is: ,",c)
    print("The arithmetic mean of the values is: ",(b/c))
    print("The standard deviation of the values is: ",st)

readNumbersFromFile("numbers.txt")


from django.contrib.auth import User
from django.db import models
from django.utils.translation import ugettext_lazy as __

from mezzanine.pages.models import RichTextPage

class Project(RichTextPage, AdminTumbMixin, ):
    image = models.ImageField(
        verbose_name=__("Image"),
        upload_to='projects/' + self.title +'/images',
        height_field=700,
        width_field=350,
        help_text=__("The image used for the project's page"))

    icon = models.ImageField(
        verbose_name=__("Icon"),
        upload_to='projects/' + self.title + '/images',
        height_field=60,
        width_field=60,
        help_text=__("An icon that represents the project"))

    site_url = models.URLField(
        verbose_name=__("Project Website URL"),
        help_text=__("URL pointing to the projects own site"))

    version = models.DecimalField(
        verbose_name=__("Version"),
        max_digits=5,
        decimal_places=2,
        help_text=__("The projects current version"))

    overview = models.TextField(
        help_text=__("An overview of the project"))

    class Meta:
        verbose_name = __("Project")
        verbose_name_plural = __("Projects")


"""Presentation message handler."""

from .....core.oob_processor import OobMessageProcessor
from .....ledger.error import LedgerError
from .....messaging.base_handler import BaseHandler, HandlerException
from .....messaging.models.base import BaseModelError
from .....messaging.request_context import RequestContext
from .....messaging.responder import BaseResponder
from .....storage.error import StorageError
from .....utils.tracing import trace_event, get_timer

from .. import problem_report_for_record
from ..manager import PresentationManager
from ..messages.presentation import Presentation
from ..messages.presentation_problem_report import ProblemReportReason

class PresentationHandler(BaseHandler):
    """Message handler class for presentations."""

    async def handle(self, context: RequestContext, responder: BaseResponder):
        """Message handler logic for presentations.

        Args:
            context: request context
            responder: responder callback

        """
        r_time = get_timer()
        profile = context.profile
        self._logger.debug("PresentationHandler called with context %s", context)
        assert isinstance(context.message, Presentation)
        self._logger.info(
            "Received presentation message: %s",
            context.message.serialize(as_string=True),
        )

        # If connection is present it must be ready for use
        if context.connection_record and not context.connection_ready:
            raise HandlerException("Connection used for presentation not ready")

        # Find associated oob record. If the presentation request was created as an oob
        # attachment the presentation exchange record won't have a connection id (yet)
        oob_processor = context.inject(OobMessageProcessor)
        oob_record = await oob_processor.find_oob_record_for_inbound_message(context)

        # Normally we would do a check here that there is either a connection or
        # an associated oob record. However as present proof supported receiving
        # presentation without oob record or connection record
        # (aip-1 style connectionless) we can't perform this check here

        presentation_manager = PresentationManager(profile)

        presentation_exchange_record = await presentation_manager.receive_presentation(
            context.message, context.connection_record, oob_record
        )  # mgr saves record state null if need be and possible

        r_time = trace_event(
            context.settings,
            context.message,
            outcome="PresentationHandler.handle.END",
            perf_counter=r_time,
        )

        # Automatically move to next state if flag is set
        if (
            presentation_exchange_record
            and presentation_exchange_record.auto_verify
            or context.settings.get("debug.auto_verify_presentation")
        ):
            try:
                await presentation_manager.verify_presentation(
                    presentation_exchange_record, responder
                )
            except (BaseModelError, LedgerError, StorageError) as err:
                self._logger.exception(err)
                if presentation_exchange_record:
                    async with profile.session() as session:
                        await presentation_exchange_record.save_error_state(
                            session,
                            reason=err.roll_up,  # us: be specific
                        )
                    await responder.send_reply(
                        problem_report_for_record(
                            presentation_exchange_record,
                            ProblemReportReason.ABANDONED.value,  # them: be vague
                        )
                    )

            trace_event(
                context.settings,
                presentation_exchange_record,
                outcome="PresentationHandler.handle.VERIFY",
                perf_counter=r_time,
            )


from lib.list import Node, create_palindrome_list

def is_palindrome(head):
    base = head
    runner = head
    stack = []

    while runner is not None and runner.next is not None:
        stack.append(base.data)
        base = base.next
        runner = runner.next.next

    if runner is not None:
        base = base.next

    while base is not None:
        if stack.pop() != base.data:
            return False
        base = base.next

    return True

if __name__ == '__main__':
    head = create_palindrome_list(7)
    print(f'Palindrome: {head}')
    print(is_palindrome(head))


#!/usr/bin/env python
"""
Tests for geneve tool.
"""

import json
import os.path
import pytest
from eve.io.mongo import Validator
from evegenie import EveGenie

test_data = {
    'user': {
        'name': 'Turtle Man',
        'age': 71,
        'alive': True,
        'title': 'Champion of Sea Dwellers',
        'inventory': ['map', 'apple', 'sword', 'potion'],
        'primary_artifact': 'objectid:artifact',
        'secondary_artifacts': ['objectid: artifact', 'objectid:artifact'],
        'address': {
            'address': '123 Pacific Ocean',
            'city': 'Neptunville',
            'state': 'wet',
        },
        'attack_bonus': '1-10',
        'difficulty': '0.0-1.0',
    },
    'artifact': {
        'name': 'Sword of Speed',
        'cost': 501.01,
        'color': 'red',
        'stats': {
            'weight': 200.01,
            'length': 3.01,
            'powers': {
                'strike': 1,
                'deflect': 1,
                'speed': 3,
            },
        }
    }
}

if not os.path.isfile('test.json'):
    with open('test.json', 'w') as ofile:
        ofile.write(json.dumps(test_data, indent=4, separators=(',',' : ')))

test_data_answer = {
    'user': {
        'name': {
            'type': 'string',
        },
        'age': {
            'type': 'integer',
        },
        'alive': {
            'type': 'boolean'
        },
        'title': {
            'type': 'string'
        },
        'inventory': {
            'type': 'list',
            'schema': {
                'type': 'string'
            }
        },
        'primary_artifact': {
            'type': 'objectid',
            'data_relation': {
                'resource': 'artifact',
                'field': '_id',
                'embeddable': True,
            },
        },
        'secondary_artifacts': {
            'type': 'list',
            'schema': {
                'type': 'objectid',
                'data_relation': {
                    'resource': 'artifact',
                    'field': '_id',
                    'embeddable': True,
                },
            },
        },
        'address': {
            'type': 'dict',
            'schema': {
                'address': {'type': 'string'},
                'city': {'type': 'string'},
                'state': {'type': 'string'}
            },
        },
        'attack_bonus': {
            'max': 10,
            'min': 1,
            'type': 'integer'
        },
        'difficulty': {
            'max': 1.0,
            'min': 0.0,
            'type': 'float'
        }
    },
    'artifact': {
        'name': {
            'type': 'string'
        },
        'cost': {
            'type': 'float'
        },
        'color': {
            'type': 'string'
        },
        'stats': {
            'type': 'dict',
            'schema': {
                'weight': {'type': 'float'},
                'length': {'type': 'float'},
                'powers': {
                    'type': 'dict',
                    'schema': {
                        'strike': {'type': 'integer'},
                        'deflect': {'type': 'integer'},
                        'speed': {'type': 'integer'},
                    }
                },
            }
        }
    }
}

simple_test_data = {
    'user': {
        'name': 'Turtle Man',
        'age': 71,
        'alive': True,
        'title': 'Champion of Sea Dwellers',
    },
}

test_data_answer_string = json.dumps(test_data_answer)

def test_input_string():
    """
    Make sure data loaded from string is parsed as expected.

    :return:
    """
    eg = EveGenie(data=json.dumps(test_data))
    assert(dict(eg) == test_data_answer)

def test_input_dict():
    """
    Make sure data loaded as a dict is parsed as expected.

    :return:
    """
    eg = EveGenie(data=test_data)
    assert(dict(eg) == test_data_answer)

def test_input_file():
    """
    Make sure data loaded from file is parsed as expected.

    :return:
    """
    eg = EveGenie(filename='test.json')
    assert(dict(eg) == test_data_answer)

def test_input_both_inputs():
    """
    Make sure when both data types are passed the data is still parsed as expected.

    :return:
    """
    eg = EveGenie(data=test_data, filename='test.json')
    assert(dict(eg) == test_data_answer)

def test_simple_endpoint_validation():
    """
    Test that the endpoint schema generated will validate when used in Eve.

    :return:
    """
    eg = EveGenie(data=simple_test_data)
    data = dict(eg)
    for endpoint in data:
        v = Validator(data[endpoint])
        assert(v.validate(simple_test_data[endpoint]))

if __name__ == '__main__':
    pytest.main('egtest.py')


import os
from ast import literal_eval

import kaldi_io
import numpy as np
import pandas as pd

from classifier.classifier_lda_plda import EmbeddingSource

__author__ = "Kailash Karthik S"
__uni__ = "ks3740"
__email__ = "kailashkarthik.s@columbia.edu"
__status__ = "Development"

class SessionArkReactor:
    def __init__(self, ark_files, embeddings_file, embedding_source: EmbeddingSource):
        self.embedding_source = embedding_source
        self.speech_arks = [(os.path.basename(file_), kaldi_io.read_vec_flt_ark(file_)) for file_ in ark_files]
        self.multi_modal_embeddings = pd.read_csv(embeddings_file)
        self.multi_modal_embeddings[embedding_source.value] = self.multi_modal_embeddings[embedding_source.value].apply(
            literal_eval)

    @staticmethod
    def cluster_arks(arks):
        combined_ark = {
            '1': list(),
            '2': list(),
            '3': list(),
            '4': list(),
            '5': list(),
        }
        for ark_file, ark in arks:
            for key, vector in ark:
                combined_ark[SessionArkReactor.get_session_number(key)].append((key, vector))
        return combined_ark

    @staticmethod
    def get_session_number(ark_key):
        # anger/disgust-test-01F-improvisation-01-000-M
        return SessionArkReactor.decompose_ark_key(ark_key)[0]

    @staticmethod
    def decompose_ark_key(key):
        # anger/disgust-test-01F-improvisation-01-000-M
        key_components = key.split('-')
        return key_components[2][1], key_components[2][2], key_components[3], key_components[4], key_components[5], \
               key_components[6]

    def get_modified_file_name(self, session):
        return self.embedding_source.value + '_xvector.' + session + '.ark'

    def get_modified_ark_components(self, speech_arks):
        modified_arks = []
        for session, speech_ark in speech_arks.items():
            ark = []
            for key, array_ in speech_ark:
                session_number, mocap_source, dialogue_type, dialogue_number, utterance_number, speaker = \
                    self.decompose_ark_key(key)
                embeddings_record = self.multi_modal_embeddings[
                    (self.multi_modal_embeddings['Session_Number'] == int(session_number)) & (
                            self.multi_modal_embeddings['Mocap_Source'] == mocap_source) & (
                            self.multi_modal_embeddings['Dialogue_Type'] == dialogue_type) & (
                            self.multi_modal_embeddings['Dialogue_Number'] == dialogue_number) & (
                            self.multi_modal_embeddings['Utterance_Number'] == int(utterance_number)) & (
                            self.multi_modal_embeddings['Speaker'] == speaker)]
                embedding = embeddings_record[self.embedding_source.value].values[0]
                ark.append((key, embedding))
            modified_arks.append((session, ark))
        return modified_arks

    def create_session_wise_arks(self):
        session_wise_speech_arks = self.cluster_arks(self.speech_arks)
        modified_ark_components = self.get_modified_ark_components(session_wise_speech_arks)
        for session, components in modified_ark_components:
            with open('iemocap/data/kaldi/modified/' + self.get_modified_file_name(session), 'wb') as file_:
                for key, vector in components:
                    kaldi_io.write_vec_flt(file_, np.array(vector), key)

if __name__ == '__main__':
    ark_files_ = [
        'iemocap/data/kaldi/xvector.1.ark',
        'iemocap/data/kaldi/xvector.2.ark',
        'iemocap/data/kaldi/xvector.3.ark',
        'iemocap/data/kaldi/xvector.4.ark',
        'iemocap/data/kaldi/xvector.5.ark',
    ]
    embeddings_file_ = 'iemocap/data/dataset_with_multi_modal_embeddings.csv'
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.TEXT)
    reactor.create_session_wise_arks()
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.SPEECH)
    reactor.create_session_wise_arks()
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.MULTIMODAL)
    reactor.create_session_wise_arks()


# http://qiita.com/tkazusa/items/b8e4d287f94f8b87ed52

import matplotlib.pyplot as plt
from pylab import *
import numpy as np
import random

def heaviside(x):
    return 0.5 * (np.sign(x) + 1)

def NN(x_train, t, n_imput, n_hidden, n_output, eta, W1, W2, n_loop):
    fp_w1 = open("W1.txt", "w")
    fp_w2 = open("W2.txt", "w")
    fp_diff1 = open("diff1.txt", "w")
    fp_diff2 = open("diff2.txt", "w")

    for n in range(n_loop):
        header = "# loop %d\n" % n
        fp_w1.write(header)
        fp_w2.write(header)
        fp_diff1.write(header)
        fp_diff2.write(header)

        for n in range(len(x_train)):
            x = np.array([x_train[n]])

            #feedforward
            X = np.insert(x, 0, 1) #Insert fixed term

            A = np.dot(W1, X) #(5.62)
            Z = np.tanh(A)  #(5.63)
            Z[0] = 1.0
            Y = np.dot(W2, Z) #(5.64)

            #Backprobagation
            D2 = Y - t[n]#(5.65)
            D1 = (1-Z**2)*W2*D2 #(5.66)

            diff_w1 = W1 - eta*D1.T*X
            diff_w2 = W2 - eta*D2.T*Z

            W1 = diff_w1 #(5.67), (5.43)
            W2 = diff_w2 #(5.67), (5.43)

            str_w1 = "%s\n" % str(W1)
            str_w2 = "%s\n" % str(W2)
            str_diff1 = "%s\n" % str(diff_w1)
            str_diff2 = "%s\n" % str(diff_w2)
            fp_w1.write(str_w1)
            fp_w2.write(str_w2)
            fp_diff1.write(str_diff1)
            fp_diff2.write(str_diff2)

    fp_w1.close()
    fp_w2.close()
    fp_diff1.close()
    fp_diff2.close()

    return  W1, W2

def output(x, W1, W2):
    X = np.insert(x, 0, 1) #Insert fixed term

    A = np.dot(W1, X) #(5.62)
    Z = np.tanh(A)  #(5.63)
    Z[0] = 1.0 #Insert fixed term
    Y = np.dot(W2, Z) #(5.64)
    return Y, Z

def print_weight(w, name):
    fp = open(name+".txt", "w")
    s = str(w)
    fp.write(s)
    fp.close()

if __name__ == "__main__":
#    global fp_w1, fp_w2
    #Set form of nueral network
    n_imput = 2
    n_hidden = 4
    n_output = 1
    eta = 0.1
    W1 = np.random.random((n_hidden, n_imput))
    W2 = np.random.random((n_output, n_hidden))
    print_weight(W1, "W1_init")
    print_weight(W2, "W2_init")

    n_loop = 1000

    #Set train data
    x_train = np.linspace(-4, 4, 300).reshape(300, 1)
    y_train_1 = x_train * x_train
    y_train_2 = np.sin(x_train)
    y_train_3 = np.abs(x_train)
    y_train_4 = heaviside(x_train)

    fp_xtrain = open("xtrain.dat", "w")
    fp_xtrain_1 = open("xtrain_1.dat", "w")
    for x in x_train:
        fp_xtrain.write("%f\n" % x)
    for x in y_train_1:
        fp_xtrain_1.write("%f\n" % x)
    fp_xtrain.close()
    fp_xtrain_1.close()

    W1_1, W2_1= NN(x_train, y_train_1, n_imput, n_hidden, n_output, eta, W1, W2, n_loop)
    # W1_2, W2_2= NN(x_train, y_train_2, n_imput, n_hidden, n_output, eta, W1, W2, n_loop)
    # W1_3, W2_3= NN(x_train, y_train_3, n_imput, n_hidden, n_output, eta, W1, W2, n_loop)
    # W1_4, W2_4= NN(x_train, y_train_4, n_imput, n_hidden, n_output, eta, W1, W2, n_loop)
    print_weight(W1_1, "W1_1_last")
    print_weight(W2_1, "W2_2_last")

    Y_1 = np.zeros((len(x_train), n_output))
    Z_1 = np.zeros((len(x_train), n_hidden))

#    Y_2 = np.zeros((len(x_train), n_output))
#    Z_2 = np.zeros((len(x_train), n_hidden))

#    Y_3 = np.zeros((len(x_train), n_output))
#    Z_3 = np.zeros((len(x_train), n_hidden))

#    Y_4 = np.zeros((len(x_train), n_output))
#    Z_4 = np.zeros((len(x_train), n_hidden))

    for n in range(len(x_train)):
        Y_1[n], Z_1[n] =output(x_train[n], W1_1, W2_1)
       # Y_2[n], Z_2[n] =output(x_train[n], W1_2, W2_2)
       # Y_3[n], Z_3[n] =output(x_train[n], W1_3, W2_3)
       # Y_4[n], Z_4[n] =output(x_train[n], W1_4, W2_4)

    plt.plot(x_train, Y_1, "r-")
    plt.plot(x_train, y_train_1, "bo", markersize=3)
    for i in range(n_hidden):
        plt.plot(x_train, Z_1[:,i], 'm--')
    xlim([-1,1])
    ylim([0, 1])
    title("Figure 5.3(a)")
    show()

    # plt.plot(x_train, Y_2, "r-")
    # plt.plot(x_train, y_train_2, "bo", markersize=2)
    # for i in range(n_hidden):
    #     plt.plot(x_train, Z_2[:,i], 'm--')
    # xlim([-3.14,3.14])
    # ylim([-1, 1])
    # title("Figure 5.3(b)")
    # show()
    #
    #
    # plt.plot(x_train, Y_3, "r-")
    # plt.plot(x_train, y_train_3, "bo", markersize=4)
    # for i in range(n_hidden):
    #     plt.plot(x_train, Z_3[:,i], 'm--')
    # xlim([-1,1])
    # ylim([0, 1])
    # title("Figure 5.3(c)")
    # show()
    #
    #
    # plt.plot(x_train, Y_4, "r-")
    # plt.plot(x_train, y_train_4, "bo" ,markersize=2)
    # for i in range(n_hidden):
    #     plt.plot(x_train, Z_4[:,i], 'm--')
    # xlim([-2,2])
    # ylim([-0.05, 1.05])
    # title("Figure 5.3(d)")
    # show()


from django.conf.urls.defaults import *
from django.contrib import admin
from isobar.urls import urlpatterns
from isobar.apps.transurlvania.defaults import *
from django.views.generic.simple import direct_to_template
from isobar.apps.work.models import *
from isobar.apps.contact.models import *
from isobar.apps.about.models import *
from isobar.apps.home.models import *
from isobar.apps.people.models import Person as PeoplePerson
from isobar.apps.people.models import Department
from django.contrib.flatpages.models import *

# remove older models in favor of new ones so the old ones don't show up on the admin panel:
# example: work vs workisobar, about vs aboutisobar
admin.site.unregister(About)
admin.site.unregister(CaseStudy)
admin.site.unregister(Person)
admin.site.unregister(Location)
admin.site.unregister(Link)
admin.site.unregister(Map)
admin.site.unregister(Department)
admin.site.unregister(PeoplePerson)
admin.site.unregister(Layout)

admin.site.unregister(FlatPage)

localpatterns = lang_prefixed_patterns('aboutisobar.views',
	url(r'^about/services/$', 'services', name='service_view'),
	url(r'^about/services/ajax/$', 'services_ajax', name='service_ajax'),
	url(r'^about/profiles/$', 'profiles', name='profile_view'),
	url(r'^about/partners/$', 'partners', name='partner_view'),
  url(r'^about/partners/ajax/$', 'partners_ajax', name='partner_ajax'),
  url(r'^about/profiles/ajax/$', 'profiles_ajax', name='profile_ajax'),
	url(r'^about/$', 'profiles', name='profile_view'),
)

localpatterns += lang_prefixed_patterns('workisobar.views',
    url(r'^work/$', 'index', name="work_view"),
    url(r'^work/casestudy/(?P<slug>[a-zA-Z0-9-_]*)/$', 'index', name="specific_work_view"),
    url(r'^work/clients/$', 'clients', name="work_clients_view"),
    url(r'^work/clients/json/$', 'clients_json', name="work_clients_json"),
    url(r'^work/clients/ajax/$', 'clients_ajax', name="work_clients_ajax"),
    url(r'^work/casestudies/ajax/$', 'index_ajax', name="work_ajax"),
)

localpatterns += lang_prefixed_patterns('contactisobar.views',
    #url(r'^contact/$', 'index_proxy_contact', name='index_contact_proxy'),
    #url(r'^contact/(?P<location>[a-zA-Z-_,. ]+)/$', 'index_proxy_contact', name='index_contact_proxy'),
    url(r'^contact/$', 'index', name='index'),
    url(r'^contact/(?P<location>[a-zA-Z-_,. ]+)/$', 'index', name='index'),
    url(r'^contact_test/(?P<location>[a-zA-Z-_,. ]+)/$', 'index', name='index_contact_test'),
	url(r'^contact_data/(?P<location>[a-zA-Z-_,. ]+)/$', 'contact_data', name='contact_data_view'),
    url(r'^common_contact/$', 'common_contact', name='common_contact_view'),
    url(r'^shelf_proxy/$', 'shelf_proxy_contact', name='shelf_contact_proxy'),
    url(r'^shelf_contact/$', 'shelf_contact', name='shelf_contact_view'),
	url(r'^office_contact/(?P<name>[a-zA-Z-_,. ]+)/$', 'office_contact', name='office_contact_view'),
)

localpatterns += lang_prefixed_patterns('labsisobar.views',
    url(r'^nowlab/$', 'index', name='labs_view'),
)

localpatterns += patterns('homeisobar.views',
    url(r'^$', 'index', name="home_view"),
)

localpatterns += lang_prefixed_patterns('homeisobar.views',
	url(r'^$', 'index', name="home_view"),
)

localpatterns += lang_prefixed_patterns('careerisobar.views',
	url(r'^career/jobs/job/(?P<job>[a-zA-Z0-9-_]+)/$', 'job_item', name='job_item'),
	url(r'^career/jobs/(?P<region>[a-zA-Z-_,. ]+)/$', 'job_list_json', name='job_list_json'),

	#url(r'^career/$', 'job_view', name="job_view"),
	url(r'^career/jobs/$', 'job_list', name="job_list"),
)
urlpatterns = localpatterns + urlpatterns


import smcmodel_localize.model
import numpy as np
import scipy.optimize
import tqdm

class LocalizationModelMultilateration:

    def __init__(
        self,
        num_objects,
        num_anchors,
        anchor_positions,
        initial_position_guesses,
        num_moving_object_dimensions = 3,
        num_fixed_object_dimensions = 0,
        fixed_object_positions = None,
        measurement_value_name = 'range'
    ):
        num_object_dimensions = num_moving_object_dimensions + num_fixed_object_dimensions
        anchor_positions = np.asarray(anchor_positions)
        initial_position_guesses = np.asarray(initial_position_guesses)
        if anchor_positions.shape[0] != num_anchors:
            raise ValueError('Number of anchors specified as {} but positions specified for {} anchors'.format(
                num_anchors,
                anchor_positions.shape[0]
            ))
        if anchor_positions.shape[1] != num_object_dimensions:
            raise ValueError('Specified anchor positions have {} spatial dimensions but model is specified to have {} + {} = {} object dimensions'.format(
                anchor_positions.shape[1],
                num_moving_object_dimensions,
                num_fixed_object_dimensions,
                num_object_dimensions
            ))
        if initial_position_guesses.shape[0] != num_objects:
            raise ValueError('Number of objects specified as {} but initial position guesses specified for {} objects'.format(
                num_objects,
                initial_position_guesses.shape[0]
            ))
        if initial_position_guesses.shape[1] != num_moving_object_dimensions:
            raise ValueError('Number of moving object dimensions specified as {} but initial position guesses have {} spatial dimensions'.format(
                num_moving_object_dimensions,
                initial_position_guesses.shape[1]
            ))
        if num_fixed_object_dimensions != 0 and fixed_object_positions is None:
            raise ValueError('Number of fixed object dimensions specified as {} but no fixed object positions specified')
        if fixed_object_positions is not None:
            fixed_object_positions = np.asarray(fixed_object_positions)
            if fixed_object_positions.shape[0] != num_objects:
                raise ValueError('Number of objects specified as {} but fixed object object positions specified for {} objects'.format(
                    num_objects,
                    fixed_object_positions.shape[0]
                ))
            if fixed_object_positions.shape[1] != num_fixed_object_dimensions:
                raise ValueError('Number of fixed object dimensions specified as {} but specified fixed object positions have {} spatial dimensions'.format(
                    num_fixed_object_dimensions,
                    fixed_object_positions.shape[1]
                ))
        self.num_objects = num_objects
        self.num_anchors = num_anchors
        self.anchor_positions = anchor_positions
        self.initial_position_guesses = initial_position_guesses
        self.num_moving_object_dimensions = num_moving_object_dimensions
        self.num_fixed_object_dimensions = num_fixed_object_dimensions
        self.num_object_dimensions = num_object_dimensions
        self.fixed_object_positions = fixed_object_positions
        self.measurement_value_name = measurement_value_name
        self.observation_structure = smcmodel_localize.model.observation_structure_generator(
            self.num_anchors,
            self.num_objects,
            self.measurement_value_name
        )
        self.state_summary_structure = state_summary_structure_generator_multilateration(
            self.num_objects,
            self.num_moving_object_dimensions
        )

    def estimate_state_time_series_multilateration(
        self,
        observation_data_queue,
        state_summary_database,
        progress_bar = False,
        num_observations = None
    ):
        position_guesses = self.initial_position_guesses
        if progress_bar:
            if num_observations is not None:
                remaining_iterations = num_observations
            else:
                remaining_iterations = None
            t = tqdm.tqdm(total = remaining_iterations)
        for timestamp, observation_arrays in observation_data_queue:
            observation = observation_arrays[self.measurement_value_name]
            if observation.shape[1] != self.num_anchors:
                raise ValueError('Number of anchors specified as {} but observation data contains ranges for {} anchors'.format(
                    self.num_anchors,
                    observation.shape[1],
                ))
            if observation.shape[2] != self.num_objects:
                raise ValueError('Number of objects specified as {} but observation data contains ranges for {} objects'.format(
                    self.num_objects,
                    observation.shape[2],
                ))
            if position_guesses.shape[0] != self.num_objects:
                raise ValueError('Number of objects specified as {} but initial position guesses specified for {} objects'.format(
                    self.num_objects,
                    position_guesses.shape[0]
                ))
            if position_guesses.shape[1] != self.num_moving_object_dimensions:
                raise ValueError('Number of moving object dimensions specified as {} but initial position guesses have {} spatial dimensions'.format(
                    self.num_moving_object_dimensions,
                    position_guesses.shape[1]
                ))
            if self.fixed_object_positions is not None:
                initial_guesses = np.concatenate(
                    (position_guesses, self.fixed_object_positions),
                    axis = -1
                )
            else:
                initial_guesses = position_guesses
            moving_object_positions = np.empty((self.num_objects, self.num_object_dimensions))
            for object_index in range(self.num_objects):
                moving_object_positions[object_index] = self.estimated_position(
                    observation[0, :, object_index],
                    position_guesses[object_index]
                )
            moving_object_positions_expanded = np.expand_dims(moving_object_positions, axis = 0)
            state_summary = {
                'moving_object_positions_multilateration': moving_object_positions_expanded,
            }
            state_summary_database.write_data(
                timestamp, state_summary)
            position_guesses = moving_object_positions[:, :self.num_moving_object_dimensions]
            if progress_bar:
                t.update()
        if progress_bar:
            t.close()

    def estimated_position(self,measured_ranges, initial_guess):
        solution = scipy.optimize.minimize(
            fun = self.mean_squared_error,
            x0 = initial_guess,
            args = (measured_ranges))
        return solution.x

    def mean_squared_error(self, object_position, measured_ranges):
        num_measured_ranges = np.sum(np.logical_not(np.isnan(measured_ranges)))
        calculated_ranges = self.ranges(object_position)
        errors = measured_ranges - calculated_ranges
        sum_squared_errors = np.nansum(np.square(errors))
        mean_squared_error = sum_squared_errors/num_measured_ranges
        return mean_squared_error

    def ranges(self, object_position):
        object_position_expanded = np.expand_dims(object_position, 0)
        return np.linalg.norm(object_position_expanded - self.anchor_positions, axis = -1)

def state_summary_structure_generator_multilateration(
    num_objects,
    num_moving_object_dimensions
):
    state_summary_structure = {
        'moving_object_positions_multilateration': {
            'shape': [num_objects, num_moving_object_dimensions],
            'type': 'float32'
        }
    }
    return state_summary_structure


#-*- coding:utf-8 -*-
"""
@Author: Jeff Zhang
@Time:   17-8-30 下午3:14
@File:   zhihuSpider.py
"""

import scrapy
import re
import json
from scrapy.http import Request
from zhihu.items import ZhihuItem,RelationItem,AnswerItem,QuestionItem,ArticleItem
from zhihu.scrapy_redis.spiders import RedisSpider
from scrapy import Spider
import time

class ZhihuSpider(Spider):
    name = "zhihuspider"
    redis_key = "zhihuspider:start_urls"
    allowed_domains = ['www.zhihu.com']
    start_urls = ['http://zhihu.com/']
    start_user_id = ['excited-vczh']

    user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
    user_query = 'locations,employments,industry_category,gender,educations,business,follower_count,following_count,description,badge[?(type=best_answerer)].topics'

    follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
    follows_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

    followers_url = 'https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}'
    followers_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

    answers_url = ''

    def start_requests(self):
        for user_id in self.start_user_id:
            yield Request(
                'https://www.zhihu.com/api/v4/members/' + user_id + '?include=locations,employments,industry_category,gender,educations,business,follower_count,following_count,description,badge[?(type=best_answerer)].topics',
                meta={'user_id': user_id}, callback=self.parse_user)

    def parse_user(self, response):
        json_results = json.loads(response.text)
        item = ZhihuItem()
        # 获取性别
        if json_results['gender'] == 1:
            item['gender'] = u'男'
        elif json_results['gender'] == 0:
            item['gender'] = u'女'
        else:
            item['gender'] = u'未知'
        item['user_id'] = json_results['url_token']
        item['user_image_url'] = json_results['avatar_url']
        item['name'] = json_results['name']

        # 获取居住地
        item['locations'] = []
        try:
            for location in json_results['locations']:
                item['locations'].append(location['name'])
        except:
            pass

        # 获取行业
        try:
            item['business'] = json_results['business']['name']
        except:
            try:
                item['business'] = json_results['industry_category']
            except:
                item['business'] = u'未知'

        #获取教育情况
        item['education'] = []
        for element in json_results['educations']:
            try:
                education = element['school']['name'] + "-" + element['major']['name']
                item['education'].append(education)
            except:
                try:
                    education = element['school']['name']
                    item['education'].append(education)
                except:
                    pass

        item['followees_num'] = json_results['following_count']
        item['followers_num'] = json_results['follower_count']

        # 获取工作情况
        item['employments'] = []
        for element in json_results['employments']:
            try:
                employment = element['company']['name'] + "-" + element['job']['name']
                item['employments'].append(employment)
            except:
                try:
                    employment = element['company']['name']
                    item['employments'].append(employment)
                except:
                    pass

        yield item

        item = RelationItem()
        user_id = response.meta['user_id']
        item['relations_id'] = []
        item['user_id'] = user_id
        item['relation_type'] = ''

        yield Request('https://www.zhihu.com/api/v4/members/'+user_id+'/followers?include=data[*].answer_count,badge[?(type=best_answerer)].topics&limit=20&offset=0',callback=self.parse_relation,meta={'item':item, 'offset':0, 'relation_type':'followers'})
        yield Request('https://www.zhihu.com/api/v4/members/'+user_id+'/followees?include=data[*].answer_count,badge[?(type=best_answerer)].topics&limit=20&offset=0',callback=self.parse_relation,meta={'item':item, 'offset':0, 'relation_type':'followees'})
        yield Request('https://www.zhihu.com/api/v4/members/'+user_id+'/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,collapsed_by,suggest_edit,comment_count,can_comment,content,voteup_count,reshipment_settings,comment_permission,mark_infos,created_time,updated_time,review_info,question,excerpt,relationship.is_authorized,voting,is_author,is_thanked,is_nothelp,upvoted_followees;data[*].author.badge[?(type=best_answerer)].topics&limit=20&offset=0',callback=self.parse_answer,meta={'answer_user_id':user_id,'offset':0})
        yield Request('https://www.zhihu.com/api/v4/members/'+user_id+'/questions?include=data[*].created,answer_count,follower_count,author,admin_closed_comment&limit=20&offset=0',callback=self.parse_question,meta={'ask_user_id':user_id,'offset':0})
        yield Request('https://www.zhihu.com/api/v4/members/'+user_id+'/articles?include=data[*].comment_count,can_comment,comment_permission,admin_closed_comment,content,voteup_count,created,updated,upvoted_followees,voting,review_info;data[*].author.badge[?(type=best_answerer)].topics&limit=20&offset=0',callback=self.parse_article,meta={'author_id':user_id,'offset':0})

    def parse_relation(self, response):
        json_results = json.loads(response.text)
        relations_id = []
        for user_info in json_results['data']:
            relations_id.append(user_info['url_token'])
        response.meta['item']['relations_id'] = relations_id

        if response.meta['offset'] == 0:
            response.meta['item']['relation_type'] = response.meta['relation_type']
        else:
            response.meta['item']['relation_type'] = 'next:' + response.meta['relation_type']

        yield response.meta['item']

        for user_id in response.meta['item']['relations_id']:
            yield Request(
                'https://www.zhihu.com/api/v4/members/' + user_id + '?include=locations,employments,industry_category,gender,educations,business,follower_count,following_count,description,badge[?(type=best_answerer)].topics',
                meta={'user_id': user_id}, callback=self.parse_user)

        if json_results['paging']['is_end'] == False:
            offset = response.meta['offset'] + 20
            next_page = re.findall('(.*offset=)\d+', response.url)[0]
            yield Request(next_page + str(offset), callback=self.parse_relation,
                          meta={'item': response.meta['item'], 'offset': offset,
                                'relation_type': response.meta['relation_type']})

    def parse_article(self, response):
        json_results = json.loads(response.text)
        for result in json_results['data']:
            item = ArticleItem()
            item['author_id'] = response.meta['author_id']
            item['title'] = result['title']
            item['article_id'] = result['id']
            item['content'] = result['content']
            item['cretated_time'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result['created']))
            item['updated_time'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result['updated']))
            item['voteup_count'] = result['voteup_count']
            item['comment_count'] = result['comment_count']
            yield item
            if json_results['paging']['is_end'] == False:
                offset = response.meta['offset'] + 20
                next_page = re.findall('(.*offset=)\d+', response.url)[0]
                yield Request(next_page + str(offset), callback=self.parse_article,
                              meta={'author_id': response.meta['author_id'], 'offset': offset})

    def parse_answer(self, response):
        json_results = json.loads(response.text)
        for result in json_results['data']:
            item = AnswerItem()
            item['answer_user_id'] = response.meta['answer_user_id']
            item['answer_id'] = result['id']
            item['cretated_time'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result['created_time']))
            item['updated_time'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result['updated_time']))
            item['voteup_count'] = result['voteup_count']
            item['comment_count'] = result['comment_count']
            item['content'] = result['content']
            yield item
        if json_results['paging']['is_end'] == False:
            offset = response.meta['offset'] + 20
            next_page = re.findall('(.*offset=)\d+', response.url)[0]
            yield Request(next_page + str(offset), callback=self.parse_answer,
                          meta={'answer_user_id': response.meta['answer_user_id'], 'offset': offset})

    def parse_question(self, response):
        json_results = json.loads(response.text)
        for result in json_results['data']:
            item = QuestionItem()
            item['ask_user_id'] = response.meta['ask_user_id']
            item['title'] = result['title']
            item['question_id'] = result['id']
            item['ask_time'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result['created']))
            item['answer_count'] = result['answer_count']
            item['followees_count'] = result['follower_count']
            yield item
        if json_results['paging']['is_end'] == False:
            offset = response.meta['offset'] + 20
            next_page = re.findall('(.*offset=)\d+', response.url)[0]
            yield Request(next_page + str(offset), callback=self.parse_question,
                          meta={'ask_user_id': response.meta['ask_user_id'], 'offset': offset})


from django.test import TestCase
from django.contrib.auth.models import User
from .models import Environment, Project
import os
from django.conf import settings
from .helpers import create_settings_file, decrypt_key
import yaml

class ProjectTestCase(TestCase):
    def setUp(self):
        user = User.objects.create_user("admin")
        environment = Environment.objects.create(
            name="test-project-env",
            image="test-project-env"
        )
        Project.objects.create(
            name="test-project",
            slug="test-project",
            owner=user,
            project_key="a2V5",
            project_secret="c2VjcmV0",
            environment=environment
        )

    def test_create_settings_file(self):
        project = Project.objects.filter(name="test-project").first()

        proj_settings = dict()
        proj_settings['auth_url'] = os.path.join('https://' + settings.DOMAIN, 'api/api-token-auth')
        proj_settings['access_key'] = decrypt_key(project.project_key)
        proj_settings['username'] = "admin"
        proj_settings['token'] = "api_token"
        proj_settings['so_domain_name'] = settings.DOMAIN

        proj_settings['Project'] = dict()
        proj_settings['Project']['project_name'] = project.name
        proj_settings['Project']['project_slug'] = project.slug

        self.assertEqual(create_settings_file(project, "admin", "api_token"), yaml.dump(proj_settings))

    def test_decrypt_key(self):
        project = Project.objects.filter(name="test-project").first()

        self.assertEqual(decrypt_key(project.project_key), "key")
        self.assertEqual(decrypt_key(project.project_secret), "secret")


import unittest

from sqlalchemy import MetaData, Table, Column, Integer, String, select
from sqlalchemy.dialects import postgresql
from sqlalchemy.schema import CreateIndex

from pedsnetdcc.concept_name_transform import ConceptNameTransform
from pedsnetdcc.tests.transform_test_utils import clean

class ConceptNameTest(unittest.TestCase):

    def setUp(self):
        self.metadata = MetaData()

        foo_col = Column('foo_concept_id', Integer)
        bar_col = Column('bar_concept_id', Integer)

        self.table1 = Table('table1', self.metadata,
                            foo_col,
                            bar_col)

        baz_col = Column('baz_concept_id', Integer)

        self.table2 = Table('table2', self.metadata,
                            baz_col)

        # Create and add the `concept` table to the sqlalchemy metadata
        self.concept = Table('concept', self.metadata,
                             Column('concept_id', Integer),
                             Column('concept_name', String(512)))

    def test_modify_select(self):

        select_obj = select([self.table1])
        join_obj = self.table1

        select_obj, join_obj = ConceptNameTransform.modify_select(
            self.metadata,
            'user',
            select_obj,
            join_obj)

        select_obj = select_obj.select_from(join_obj)

        new_sql = str(select_obj.compile(dialect=postgresql.dialect()))

        expected = clean("""
          SELECT table1.foo_concept_id, table1.bar_concept_id,
          concept_1.concept_name AS foo_concept_name, concept_2.concept_name
          AS bar_concept_name
          {NL}FROM table1
          LEFT OUTER JOIN concept AS concept_1
              ON concept_1.concept_id = foo_concept_id
          LEFT OUTER JOIN concept AS concept_2
              ON concept_2.concept_id = bar_concept_id
        """)

        self.maxDiff = None
        self.assertEqual(expected, new_sql)

    def test_modify_metadata(self):

        metadata = ConceptNameTransform.modify_metadata(self.metadata)

        indexes = metadata.tables['table1'].indexes
        self.assertEqual(len(indexes), 2, 'Indexes created')

        for index in indexes:
            index_sql = str(CreateIndex(index).compile(
                dialect=postgresql.dialect()))
            if index.name == 'tab_fcn_81387a6132eed7f69b5_ix':
                expected = clean("""
                  CREATE INDEX tab_fcn_81387a6132eed7f69b5_ix
                    ON table1 (foo_concept_name)
                """)
                self.assertEqual(index_sql, expected)
            elif index.name == 'tab_bcn_7baa5e16ad1f8129b90_ix':
                expected = clean("""
                  CREATE INDEX tab_bcn_7baa5e16ad1f8129b90_ix
                    ON table1 (bar_concept_name)
                """)
                self.assertEqual(index_sql, expected)
            else:
                self.fail(
                    'Unexpected index encountered: {}'.format(index.name))


#!/usr/bin/env python3
# -*- encoding=utf-8 -*-

from bs4 import BeautifulSoup
import requests
import time

html = open('classinfo.htm','r',encoding='utf-8')
html_txt = html.read()

soup = BeautifulSoup(html_txt)

all_people_list=[]

for i in range(1,97):
    #one_info = {'QQnum':None,'QQname':None,'QQrealname':None,'QQpicurl':None}
    one_info = {}
    one_QQnum = soup.find_all('tr')[i].find_all('td')[4].string
    one_QQnum = one_QQnum.strip()
    #one_info['QQnum'] = one_QQnum

    one_QQname = soup.find_all('tr')[i].find_all('td')[2].contents[5].string
    one_QQname = one_QQname.strip()

    #one_info['QQname']=one_QQname

    one_QQrealname = soup.find_all('tr')[i].find_all('td')[3].contents[1].string
    #print(one_QQrealname)
    #print(type(one_QQrealname))
    one_QQrealname = str(one_QQrealname)
    one_QQrealname = one_QQrealname.strip()

    #one_QQrealname = one_QQrealname.strip()
    #one_info['QQrealname'] = one_QQrealname

    one_QQpicurl = 'https://q4.qlogo.cn/g?b=qq&nk='+one_QQnum+'&s=140'

    one_info = {'QQnum':one_QQnum,'QQname':one_QQname,'QQrealname':one_QQrealname,'QQpicurl':one_QQpicurl}
    all_people_list.append(one_info)

#print(all_people_list)

for i in range(len(all_people_list)):
    the_QQnum = all_people_list[i]['QQnum']
    the_QQname = all_people_list[i]['QQname']
    the_QQrealname = all_people_list[i]['QQrealname']
    pic_name = the_QQnum + '-' + the_QQname + '-' + the_QQrealname

    ir = requests.get(all_people_list[i]['QQpicurl'],headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'})
    if ir.status_code==200:
        open(picname,'wb').write(ir.content)

    time.sleep(1)
    print('one file success!')


from django.conf.urls import url
from django.views.generic import TemplateView
from views import templates_list,templates_change,templates_preview,essential_tags,templates_delete

urlpatterns = [
    url(r'templates/$', templates_list, name="templates_list"),
    url(r'^templates/essential-tags/$',essential_tags, name="essential-tags"),
    url(r'templates/add/$', templates_change, name="templates_change"),
    url(r'templates/change/(?P<slug>.+)/$', templates_change, name="templates_change"),
    url(r'templates/preview/(?P<slug>.+)/$', templates_preview, name="templates_preview"),
    url(r'templates/delete/(?P<slug>.+)/$', templates_delete, name="templates_delete"),

]


from Generic_TriggerSource import *
import telnetlib
from pkg_resources import require

require("numpy")
import numpy as np
import warnings

class Agilent33220A_trigsrc(Generic_TrigSource):
    """Rigol3030DSG RFSigGen, Child of Generic_RFSigGen.

    This class is for communicating with the Agilent33220A over telnet. The specific API calls abstract the SCPI
    commands needed to speak with the instrument. Each of these calls is an override of the Generic_RFSigGen.

    Attributes:
        *Inherited from parent.
    """

    # Private Methods
    def _telnet_query(self, message):
        """Private method that will send a message over telnet to the Rigol3030 and return the reply

        Args:
            message (str): SCPI message to be sent to the Agilent33220A

        Returns:
            str: Reply message from the Rigol3030
        """
        self._telnet_write(message)
        return self._telnet_read()

    def _telnet_write(self, message):
        """Private method that will send a message over telnet to the Agilent33220A

        Args:
            message (str): SCPI message to be sent to the Agilent33220A

        Returns:

        """
        # Checks that the telnet message is a string
        if type(message) != str:
            raise TypeError

        self.tn.write(message + "\r\n")  # Writes a telnet message with termination characters

    def _telnet_read(self):
        """Private method that will read a telnet reply from the Agilent33220A

        Args:

        Returns:
            str: Reply message from the Agilent33220A
        """
        return self.tn.read_until("\n", self.timeout).rstrip('\n')  # Telnet reply, with termination chars removed

    # Constructor and Deconstructor

    def __init__(self, ipaddress, port=5024, timeout=1):
        """Initialises and opens the connection to the Agilent33220A over telnet and informs the user

        Args:
            ipaddress (str): The IP address of the Agilent33220A
            port (int/str): The port number for the messages to be sent on (default 5555)
            timeout (float): The timeout for telnet commands in seconds (default 1)

        Returns:

        """
        self.timeout = timeout  # timeout for the telnet comms
        self.tn = telnetlib.Telnet(ipaddress, port, self.timeout)  # connects to the telnet device
        self.DeviceID = self.get_device_id()  # gets the device of the telnet device, makes sure its the right one
        self.turn_off_RF()  # turn off the RF output

        print("Opened connection to Trigger source" + self.DeviceID)  # tells the user the device has been connected to

    def __del__(self):
        """Closes the telnet connection to the Agilent33220A
        """
        self.turn_off_RF()  # make sure the RF output is off
        self.tn.close()  # close the telnet link
        print("Closed connection to " + self.DeviceID)  # tell the user the telnet link has closed

    # API Calls
    def get_device_id(self):
        """Override method that will return the device ID.

        Uses the SCPI command "*IDN?" to get the device ID.

        Args:

        Returns:
            str: The device_id of the SigGen.
        """
        device_id = self._telnet_query("*IDN?")  # gets the device information
        test_string = "Welcome to Agilent's 33220A Waveform Generator"
        if device_id[0:len(test_string)] != test_string:  # checks it's the right device
            raise ValueError("Wrong hardware device connected")
        return device_id

    def set_up_trigger_pulse(self, freq):
        """Override method that will set up a pulse suitable for a trigger signal.

                Args:
                    freq (float)
                """
        if type(freq) != float and type(freq) != int \
                and np.float64 != np.dtype(freq) and np.int64 != np.dtype(freq):
            raise TypeError
        self._telnet_write("FUNC:PULS:DCYC 50")
        self._telnet_write(''.join(("APPL:PULS ", str(freq), ', 1, 0')))
        self.turn_on_RF()

    def turn_on_RF(self):
        """Override method that will turn on the RF device output.

        Uses SCPI command "OUTP ON" to turn on the device.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        self._telnet_write("OUTP ON")  # turns on the output
        return self.get_output_state()

    def turn_off_RF(self):
        """Override method that will turn off the RF device output.

        Uses SCPI command "OUTP OFF" to turn off the device.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        self._telnet_write("OUTP OFF")  # turns off the output
        return self.get_output_state()

    def get_output_state(self):
        """Override method that will get the current output state.

        This method send the SCPI command "OUTP?" to request the output state from
        the Rigol3030.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        # checks output state
        if self._telnet_query("OUTP?") == "1":
            self.Output_State = True  # output must be on
            return True
        else:
            self.Output_State = False  # output must be off
            return False


import pygame
from settings import Settings
from pygame.sprite import Sprite, Group

settings = Settings()

class Map():
    def __init__(self, screen):
        self.screen = screen
        self.objects = []
        self.objects_view = []
        self.objects_blocking = Group()

        self.grass_image = pygame.image.load('img/grass.png')

    def blit_obj(self):
        for i in range(19):
            for j in range(19):
                self.screen.blit(self.grass_image, (i * settings.block_size , j * settings.block_size))
        for obj in self.objects_view:
            obj.blit_obj()
        for obj in self.objects_blocking:
            obj.blit_obj()

    def create_test_map(self):
        for i in range(14):
            self.objects_view.append(Road(self.screen, 5, 5 + i, '13'))
            self.objects_view.append(Road(self.screen, 5 + i, 5, '02'))
        self.objects_view.append(Road(self.screen, 5, 5, '23'))

        self.objects_blocking.add(Mountain(self.screen, 1, 4))
        self.objects_blocking.add(Mountain(self.screen, 2, 4))
        self.objects_blocking.add(Mountain(self.screen, 3, 4))
        self.objects_blocking.add(Mountain(self.screen, 4, 5))

class Road():
    def __init__(self, screen, x, y, direction):
        self.screen = screen
        self.x = x * settings.block_size
        self.y = y * settings.block_size
        self.image = pygame.image.load('img/road_' + direction + '.png')

    def blit_obj(self):
        self.screen.blit(self.image, (self.x, self.y))

class Mountain(Sprite):
    def __init__(self, screen, x, y):
        super().__init__()

        self.screen = screen

        self.image = pygame.image.load('img/mountain.png')

        self.rect = self.image.get_rect()
        self.rect.centerx = x * settings.block_size - settings.block_size // 2
        self.rect.centery = y * settings.block_size - settings.block_size // 2

    def blit_obj(self):
        self.screen.blit(self.image, self.rect)


from collections import deque
k=int(input())
m, n=map(int, input().split())
list_x=[]
d_1=[[2, 1], [2, -1], [-2, 1], [-2, -1], [1, 2], [1, -2], [-1, 2], [-1, -2]]
d_2=[[1, 0], [0, 1], [-1, 0], [0, -1]]
visit=[]
for i in range(k+1):
    visit.append([[0]*m for j in range(n)])
for i in range(n):
    list_x.append(list(map(int, input().split())))
q=deque()
q.append([0, 0, 0])
visit[0][0][0]=1
while q:
    q_pop=q.popleft()
    now_i=q_pop[0]
    now_j=q_pop[1]
    cost=q_pop[2]
    if now_i==n-1 and now_j==m-1:
        break
    if cost<k:
        for x in d_1:
            i=now_i+x[0]
            j=now_j+x[1]
            if 0<=i<n and 0<=j<m:
                if list_x[i][j]==0 and visit[cost+1][i][j]==0:
                    q.append([i, j, cost+1])
                    visit[cost+1][i][j]=visit[cost][now_i][now_j]+1
    for x in d_2:
        i=now_i+x[0]
        j=now_j+x[1]
        if 0<=i<n and 0<=j<m:
            if list_x[i][j]==0 and visit[cost][i][j]==0:
                q.append([i, j, cost])
                visit[cost][i][j]=1+visit[cost][now_i][now_j]
print(visit[cost][n-1][m-1]-1)


# -*- coding: utf-8 -*-
import tempfile
import time
import threading
import unittest
from unittest.mock import MagicMock, patch

from lucena.client import RemoteClient
from lucena.exceptions import ServiceAlreadyStarted, ServiceNotStarted, \
    IOTimeout
from lucena.service import Service, create_service
from lucena.io2.socket import Response
from lucena.worker import Worker

class MyWorker(Worker):
    def __init__(self, *args, **kwargs):
        super(MyWorker, self).__init__(*args, **kwargs)
        self.bind_handler({'$req': 'sleep'}, MyWorker.handler_sleep)

    @staticmethod
    def handler_sleep(message):
        time.sleep(1)
        response = {}
        response.update(message)
        response.update({'$rep': 'sleep 1 sec'})
        return response

class TestClientService(unittest.TestCase):
    def setUp(self):
        super(TestClientService, self).setUp()
        self.endpoint = "ipc://{}.ipc".format(
            tempfile.NamedTemporaryFile().name
        )
        self.service = create_service(
            'MyService',
            worker_factory=MyWorker,
            number_of_workers=4,
            endpoint=self.endpoint
        )

    def client_task(self, message):
        client = RemoteClient(default_timeout=500)
        client.connect(self.endpoint)
        response = client.resolve(message)
        client.close()
        return response

    def test_total_client_requests(self):
        client_requests = 1
        self.service.start()
        for i in range(client_requests):
            response = self.client_task({"$req": "HELLO"})
            self.assertEqual(
                response,
                {"$req": "HELLO", "$rep": None, "$error": "No handler match"}
            )
        response = self.service.resolve({
            '$req': 'eval',
            '$attr': 'total_client_requests'
        })
        self.assertEqual(client_requests, response.get('$rep'))
        self.service.stop()

    def test_service_restart(self):
        for i in range(10):
            self.service.start()
            self.service.stop()

    def test_pending_workers(self):
        self.service.start()
        response = self.service.resolve({
            '$req': 'eval',
            '$attr': 'pending_workers'
        })
        self.assertEqual(response.get('$rep'), False)
        self.service.stop()

    def test_req_timeout(self):
        self.service.start()
        with self.assertRaises(IOTimeout):
            self.client_task({"$req": "sleep"})
        self.service.stop()

class TestServiceController(unittest.TestCase):

    def test_service_controller_start_thread(self):
        controller = Service.Controller(worker_factory=Worker)
        rv = Response({"$signal": "ready"}, worker=b'$service', client=b'$controller', uuid=b'$uuid')
        with patch.object(controller.control_socket, 'recv_from_worker', return_value=rv):
            with patch.object(threading, 'Thread', return_value=MagicMock()) as m_thread:
                controller.start()
                m_thread.assert_called_once()

    def test_start_service_fails_if_already_started(self):
        controller = Service.Controller(worker_factory=Worker)
        controller.start()
        self.assertRaises(ServiceAlreadyStarted, controller.start)
        controller.stop()

    def test_resolve_fails_if_not_started(self):
        controller = Service.Controller(worker_factory=Worker)
        self.assertRaises(
            ServiceNotStarted,
            controller.resolve,
            {'$req': 'hello'}
        )


from django.shortcuts import render
from django.http import HttpResponse

import urllib2
import urllib
import base64
import json
import unicodedata

def _decode_list(data):
    rv = []
    for item in data:
        if isinstance(item, unicode):
            item = item.encode('utf-8')
        elif isinstance(item, list):
            item = _decode_list(item)
        elif isinstance(item, dict):
            item = _decode_dict(item)
        rv.append(item)
    return rv

def _decode_dict(data):
    rv = {}
    for key, value in data.iteritems():
        if isinstance(key, unicode):
            key = key.encode('utf-8')
        if isinstance(value, unicode):
            value = value.encode('utf-8')
        elif isinstance(value, list):
            value = _decode_list(value)
        elif isinstance(value, dict):
            value = _decode_dict(value)
        rv[key] = value
    return rv

#def index(request):
#    context = {'xyz': '123'}
#    return render(request, 'trip/index.html', context)

def index2(request):
    if request.method == 'GET':
        if 'a' in request.GET :
            context = {'xyz': request.GET['a']}
        else :
            context = {'xyz': 'no input'}
        return render(request, 'trip/index.html', context)
def gmap(request):
    return render(request, 'trip/gmap.html')
def hello_id(request, question_id):
    return HttpResponse("You're looking at id : %s." % question_id)

def index(request):
    if (('src' in request.GET) & ('dst' in request.GET)):

        isValid=checkValidAirportCode(request.GET['src'],request.GET['dst'])

        if(False==isValid):
            context = {'src_airport': request.GET['src'], 'dst_airport': request.GET['dst'] }
            return render(request, 'trip/notSupported.html', context)

        x=trip_details(request)

        ss = x.content
        context = {'lowest': ss}
        return render(request, 'trip/lowest.html', context)

    else:
        context = {'src_airport': 'empty', 'dst_airport': 'empty' }
        return render(request, 'trip/notSupported.html', context)

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def trip_details(request):
    if request.method == 'GET':
        a=request.GET['src']
        src=a.encode('utf-8')
        x=str(type(src))
        b=request.GET['dst']
        dst=b.encode('utf-8')
    token="Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/ACPCRTD!ICESMSLB\/CRT.LB!-0123456789012345678!123456!0!ABCDEFGHIJKLM!E2E-1"
    lstay="7"

    query_args = { 'origin':src, 'destination':dst,'lengthofstay': lstay,'pointofsalecountry':'US'} # you have to pass in a dictionary
    encoded_args = urllib.urlencode(query_args)

    #print 'Encoded:', encoded_args
    l = []
    l.append('https://api.test.sabre.com/v1/shop/flights/fares?')
    l.append(encoded_args)
    url=''.join(l)
    request = urllib2.Request(url)
    request.add_header('Authorization', 'Bearer %s' % token)
    request.add_header("Accept", "application/json")

    response=urllib2.urlopen(request).read()
    result=json.loads(response, object_hook=_decode_dict)
    c1=result["FareInfo"][10]["LowestFare"]
    c2=result["FareInfo"][10]["LowestNonStopFare"]
    return HttpResponse(c1)

def trip_options(request):
    return

#Check if the airport code is valid
def checkValidAirportCode(src,dst):
    valid_aiports= ['FLL', 'SFO' ,'LAX','JFK','LGA','ORD']
    src_is_in_list=src in valid_aiports
    if(False==src_is_in_list):
        return False
    dst_is_in_list=dst in valid_aiports
    if(False==dst_is_in_list):
        return False
    return True


import os.path
import subprocess
import tarfile
from tempfile import TemporaryDirectory
from typing import Optional, Tuple

from flask import current_app
from arxiv.base import logging

from compiler.services.filemanager import FileManagementService

logger = logging.getLogger(__name__)

def compile_upload(upload_id: str, output_format: str='pdf',
                   fms_endpoint: Optional[str]=None,
                   preferred_compiler: Optional[str]=None,
                   output_endpoint: Optional[str]=None):
    """
    Retrieves an upload, submits to the converter service, uploads results.

    More or less, the ``main()`` function for compiler. It operates in a
    three-step process:
    1.  Retrieve the source package for ``upload_id`` from
        ``FileManagementService``.
    2.  Use the ``preferred_compiler`` to generate ``output_format``.
    3.  Upload the results to ``output_endpoint``.

    Parameters
    ------------
    upload_id: str
        Required. The upload to retrieve.
    output_format: str
        The desired output format. Default: "pdf". Other potential values:
        "dvi", "html", "ps"
    fms_endpoint: str
        The API endpoint for the FileManagementService.
    preferred_compiler: str
        The preferred tex compiler for use with the source package.
    output_endpoint: str
        The API endpoint for uploading compiled files.
    """

    if fms_endpoint is None:
        fms_endpoint = current_app.config['FMS_ENDPOINT']

    if output_endpoint is None:
        output_endpoint = current_app.config['OUTPUT_ENDPOINT']

    # 1. Retrieve the source package
    fms = FileManagementService(fms_endpoint)

    body, headers = fms.get_upload_content(upload_id)
    etag = headers['ETag']

    # 2. Generate the compiled files
    # 3. Upload the results to output_endpoint
    with TemporaryDirectory(prefix='arxiv') as source_dir,\
         TemporaryDirectory(prefix='arxiv') as output_dir:
        with tarfile.open(fileobj=body, mode='r:gz') as tar: # type: ignore
            tar.extractall(path=source_dir)

        # 2. Generate the compiled files
        compile_source(source_dir, output_dir)

        # 3. Upload the results to output_endpoint
        # TODO: upload the output somewhere(?)

def compile_source(source_dir: str, output_dir: str, image: Optional[str]=None):
    if image is None:
        image = current_app.config['COMPILER_DOCKER_IMAGE']

    run_docker(image)#, volumes=[(source_dir, '/src'), (output_dir '/out')]

def run_docker(image: str, volumes: list = [], ports: list = [],
               args: str = '', daemon: bool = False) -> subprocess.CompletedProcess:
    """
    Run a generic docker image.

    In our uses, we wish to set the userid to that of running process (getuid)
    by default. Additionally, we do not expose any ports for running services
    making this a rather simple function.

    Parameters
    ----------
    image : str
        Name of the docker image in the format 'repository/name:tag'
    volumes : list of tuple of str
        List of volumes to mount in the format [host_dir, container_dir].
    args : str
        Arguments to the image's run cmd (set by Dockerfile CMD)
    daemon : boolean
        If True, launches the task to be run forever
    """
    # we are only running strings formatted by us, so let's build the command
    # then split it so that it can be run by subprocess
    opt_user = '-u {}'.format(os.getuid())
    opt_volumes = ' '.join(['-v {}:{}'.format(hd, cd) for hd, cd in volumes])
    opt_ports = ' '.join(['-p {}:{}'.format(hp, cp) for hp, cp in ports])
    cmd = 'docker run --rm {} {} {} {} {}'.format(
        opt_user, opt_ports, opt_volumes, image, args
    )
    result = subprocess.run(cmd, stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE, shell=True)
    if result.returncode:
        logger.error(f"Docker image call '{cmd}' exited {result.returncode}")
        logger.error(f"STDOUT: {result.stdout}\nSTDERR: {result.stderr}")
        result.check_returncode()

    return result


# -*- coding: utf-8 -*-
#
# Created on 9/1/15 by maersu
from core.utils import SYSTEMS, XLIFF, LOCALES, LOCALE_EN
from django.db import models
from django_extensions.db.models import TimeStampedModel
from django.db.models.signals import post_save
from django.dispatch import receiver
from openpyxl import Workbook, load_workbook
from openpyxl.writer.excel import save_virtual_workbook
from translations.models import Translation
from django.core.files.base import ContentFile
import logging
import json

logger = logging.getLogger(__name__)

class InputFile(TimeStampedModel):
    system = models.CharField(max_length=30, choices=SYSTEMS, default=XLIFF)
    locale = models.CharField(max_length=3, choices=LOCALES, default=LOCALE_EN)

    input_file = models.FileField(upload_to='uploads')

    input_processed = models.BooleanField(default=False)
    output_processed = models.BooleanField(default=False)

    output_de = models.FileField(upload_to='downloads', blank=True, null=True)
    output_en = models.FileField(upload_to='downloads', blank=True, null=True)
    output_fr = models.FileField(upload_to='downloads', blank=True, null=True)

    class Meta:
        ordering = ['-created']

    def __unicode__(self):
        return '%s' % self.input_file

class TranslationExport(TimeStampedModel):
    download_processed = models.BooleanField(default=False)
    upload_processed = models.BooleanField(default=False)

    download = models.FileField(upload_to='downloads', blank=True, null=True)
    upload = models.FileField(upload_to='uploads', blank=True, null=True)

    details = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['-created']

DEFAULT_LANGUAGE = 'de'
LANGUAGES = ['de', 'en'] # , 'fr'

# method for updating
@receiver(post_save, sender=TranslationExport, dispatch_uid="create_download")
def create_download(sender, instance, **kwargs):
    if kwargs.get('created'):

        wb = Workbook()
        ws = wb.active
        ws.append(['id'] + LANGUAGES)

        ids = []

        details = {}
        for l in LANGUAGES:
            details[l] = {'WordCount': 0, 'FuzzyCount': 0}

        for l in LANGUAGES:
            word_count = 0
            for t in Translation.objects.filter(**{'%s_fuzzy' % l: True}).order_by('id'):
                if t.id not in ids:
                    word_count += len(getattr(t, l).split())
                    ids.append(t.id)
                    cols = [t.id]
                    for l2 in LANGUAGES:
                        text = getattr(t, l2)
                        cols.append(text)
                        details[l2]['WordCount'] += len(text.split())
                        if getattr(t, '%s_fuzzy' % l2):
                            details[l2]['FuzzyCount'] += 1

                    ws.append(cols)

        instance.download.save('translations_%s_%s.xlsx' % ('-'.join(LANGUAGES), instance.id),
                               ContentFile(save_virtual_workbook(wb)))

        instance.details = json.dumps(
            details, sort_keys=True,
            indent=4, separators=(',', ': ')
        )
        instance.download_processed = True
        instance.save()

        logger.info('%s Translation exported to TranslationExport' % len(ids))

    elif not instance.upload_processed and instance.upload:

        ms = load_workbook(instance.upload)
        ws = ms.active

        first = True
        count = 0

        for row in ws.iter_rows():
            if first:
                first = False
                continue

            if row[0]:

                if row[1].value and row[2].value:
                    updated = Translation.objects.filter(id=row[0].value).update(
                        de=row[1].value,
                        de_fuzzy=False,
                        en=row[2].value,
                        en_fuzzy=False
                    )

                    if not updated:
                        logger.error('Translation with %s not found (%s)' % (row[0].value, row[1].value))

                    count += updated

                else:
                    logger.error('Translation for %s is missing' % row[0].value)

        instance.upload_processed = True
        instance.save()
        logger.info('%s Translation updated from TranslationExport' % updated)


# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.3'
#       jupytext_version: 0.8.6
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import os, sys, subprocess
import warnings

import pandas as pd
import numpy as np

import scipy.stats
import scipy.optimize

import numba
import bebi103
import tidy_data

data_path = "../data/"
rg = np.random.default_rng()
# -

def log_like_iid_gamma(params, n):
    """
    Calculates log likelihood for iid gamma measurements.

    Inputs:
        params : tuple or list
            Must have 2 elements. In order, mean of alpha, beta
        n : ndarray of observed catastrophe times
    Outputs:
        log-likelihood.
    """
    for param in params:
        if param <= 0:
            return -np.inf
    alpha, beta = params

    return np.sum(scipy.stats.gamma.logpdf(n, alpha, loc=0, scale=1 / beta))

# +
def mle_iid_gamma(n, x0):
    """
    Calculates the MLE for a gamma distribution.

    Inputs:
        n: ndarray of observations / data that we are modeling with the parameters
        x0 : 1x2 ndarray
            Initial estimates for the optimization.
            Must have 2 elements. In order, alpha, beta.
    Returns:
        res.x : list
            MLE for each parameter, in same order as x0.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        res = scipy.optimize.minimize(
            fun=lambda params, n: -log_like_iid_gamma(params, n),
            x0=x0,
            args=(n,),
            method='Powell'
        )

    if res.success:
        return res.x
    else:
        raise RuntimeError('Convergence failed with message', res.message)

#uncomment to run
#initial param estimates
# alpha = 2
# # find rate of arrivals
# beta = alpha / np.mean(times_to_catastrophe)
# init_params = [alpha, beta]
# # test to check that we get reasonable MLEs
# mle = mle_iid_gamma(times_to_catastrophe, init_params)
# output
#array([2.40754885, 0.00546287])

# -

def gen_gamma(params, size, rg):
    """
    generating function for gamma distribution
    Inputs:
        params (ndarray) alpha, beta
        size : number of points to draw
        rg : random number generator (np.random.default_rng)
    Outputs:
        values from gamma distribution, parametrized by params
    """
    alpha, beta = params
    return rg.gamma(alpha, 1 / beta, size=size)

# +
def get_conf_intrvl_gamma(array, init_param, size):
    """
    draw conf ints for alpha and beta of a gamma distribution
    using bebi103.bootstrap.draw_bs_reps_mle
    Inputs:
        array : observed data
        init_param : tuple of initial estimates
        size : bootstrap rep size
    Outputs:
        95% conf int
    """
    mle_bs_reps = bebi103.bootstrap.draw_bs_reps_mle(
                    mle_iid_gamma, #func with scipy.optimize
                    gen_gamma, #generating function, after parameter estimates
                    array,
                    mle_args=(init_param,), #additional args to pass into mle_iid_gamma
                    size = size,
                    progress_bar = False,
                    n_jobs = 1)

    return np.transpose(np.percentile(mle_bs_reps, [2.5, 97.5], axis=0))

#uncomment to run
# conf_intrvls = get_conf_intrvl_gamma(times_to_catastrophe, init_params, 1001)
# conf_intrvls
# #outputs
# alpha conf int: [1.99190, 2.90202]
# beta conf int : [0.00454, 0.00676]
# -

def log_like_iid_two_events(params, n, original_data):
    """
    Calculates log likelihood for iid gamma measurements.

    Inputs:
        params : tuple or list
            Must have 2 elements. In order, beta_1, delta_beta
        n : the data that we have
        y : n x 1 numpy array

    Outputs:
        log-likelihood.
    """
    #beta1 and delta_beta can't be negative.
    #Return -inf since we want to maximize positive values only
    for param in params:
        if param < 0:
            return -np.inf

    beta_1, delta_beta = params
    if beta_1 > 2 / np.mean(original_data):
        return -np.inf

    #if deltabeta approches 0, the model collapses to a gamma distribution
    # gamma distribution with alpha = 2 and beta = beta1 (or beta2)
    if np.isclose(delta_beta, 0):
        return np.sum(scipy.stats.gamma.logpdf(n, 2, loc=0, scale=1 / beta_1))

    #if not close, we'll return the actual log likelihood of the two-process story
    length = len(n)
    tmp1 = length * np.log(beta_1) + length * np.log(beta_1 + delta_beta) - length * np.log(delta_beta)
    tmp2 = -np.sum(beta_1 * n) + np.sum(np.log(1 - np.exp(-delta_beta * n)))
    return tmp1 + tmp2

def mle_iid_two_events(n, x0, original_data):
    """
    Calculates the MLE for a gamma distribution.

    Inputs:
        n  : ndarray of observations of data
        x0 : 1x2 ndarray
            Initial estimates for the optimization.
            Must have 2 elements. In order, beta_1, delta_beta
    Returns:
        res.x : list
            MLE for each parameter, in same order as x0.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        res = scipy.optimize.minimize(
            fun=lambda params, n, original_data: -log_like_iid_two_events(params, n, original_data),
            x0=x0,
            args=(n, original_data),
            method='Powell',
            tol=1e-8,
        )

    if res.success:
        return res.x
    else:
        raise RuntimeError('Convergence failed with message', res.message)

# +
# #uncomment to test out
# #set initial parameters for two arrival story
# beta1 = 2 / np.sum(times_to_catastrophe) * len(times_to_catastrophe) - 0.00000001
# init_params_two_events = [beta1, .00000001]

# # first do beta_1 and delta_beta
# mle_two_events = mle_iid_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe)

# # test to check that we get reasonable MLEs
# mle_two_events = np.array([
#     mle_two_events[0],
#     mle_two_events[0] + mle_two_events[1]
# ])

# mle_two_events

# # # outputs
# # # beta1 mle: 0.0045381223
# # # delta beta mle: 0.0000000062

# # # outputs
# # # beta1 mle: 0.0045381223
# # # beta2 mle: 0.0045381285
# -

def gen_two_events(params, n, size, rg):
    """
    Inputs:
        params: ndarray, beta_1, delta_beta = params
        size : number of bootstrap samples

    """
    beta_1, delta_beta = params
    beta_2 = beta_1 + delta_beta
    return rg.exponential(1/beta_1, size = size) + rg.exponential(1/beta_2, size = size)

# +
def get_conf_intrvl_two_events(array, init_param, original_data, size):
    """
    draw conf ints for beta1 and beta2 of two story distribution
    using bebi103.bootstrap.draw_bs_reps_mle
    Inputs:
        array : observed data
        init_param : tuple of initial estimates
        size : bootstrap rep size
    Outputs:
        95% conf int
    """
    mle_bs_reps = bebi103.bootstrap.draw_bs_reps_mle(
                    mle_iid_two_events,
                    gen_two_events,
                    array,
                    mle_args=(init_param, original_data,),
                    gen_args = (array, ),
                    size = size,
                    progress_bar = False,
                    n_jobs = 1)
    #work with individual arrays containing all estimates of a single parameter
    mle_bs_reps = np.transpose(mle_bs_reps)

    beta_1s = mle_bs_reps[0]
    beta_2s = beta_1s + mle_bs_reps[1]

    return np.percentile(np.array(
        [beta_1s, beta_2s]
        ), [2.5, 97.5], axis=1)

# #uncomment to run
# conf_intrvls_two_events = get_conf_intrvl_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe, 1001)
# conf_intrvls_two_events = np.transpose(conf_intrvls_two_events)

# #output:
# # beta1 conf int: [0.003227, 0.004538]
# # beta2 conf int : [0.004132, 0.008896]

# +
def main():
    df = tidy_data.tidy_dic()

    #Labelled data
    labeled_data = df.loc[df['labeled'] == True, 'time to catastrophe (s)']

    #convert to ndarray. Will use in part b as well.
    times_to_catastrophe = labeled_data.to_numpy()

    ###### gamma:
    alpha = 2
    # find rate of arrivals
    beta = alpha / np.mean(times_to_catastrophe)
    init_params = [alpha, beta]
    # test to check that we get reasonable MLEs
    mle = mle_iid_gamma(times_to_catastrophe, init_params)
    #confints
    conf_intrvls = get_conf_intrvl_gamma(times_to_catastrophe, init_params, 1001)

    ####### two events:
    #set initial parameters for two arrival story
    beta1 = 2 / np.sum(times_to_catastrophe) * len(times_to_catastrophe) - 0.00000001
    init_params_two_events = [beta1, .00000001]

    # first do beta_1 and delta_beta
    mle_two_events = mle_iid_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe)

    # test to check that we get reasonable MLEs
    mle_two_events = np.array([
        mle_two_events[0],
        mle_two_events[0] + mle_two_events[1]
    ])
    #confints
    conf_intrvls_two_events = get_conf_intrvl_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe, 1001)
    conf_intrvls_two_events = np.transpose(conf_intrvls_two_events)

    ######### print statements

    #gamma first
    print("""initial parameters for \nalpha: {0} \nbeta: {1:.6f}""".format(*init_params))

    print(
    """
    alpha conf int: [{0:.5f}, {1:.5f}] \n
    beta conf int : [{2:.5f}, {3:.5f}]
    """.format(
    *conf_intrvls.flatten()
    )
    )

    #two story
    print(
    """
    beta1 mle: {0:.10f}\n
    beta2 mle: {1:.10f}
    """.format(
    mle_two_events[0],
    mle_two_events[1],
    )
    )

    print(
        """
        beta1 conf int: [{0:.6f}, {1:.6f}] \n
        beta2 conf int : [{2:.6f}, {3:.6f}]
        """.format(
        *conf_intrvls_two_events.flatten()
        )
    )
    return True

if __name__ == '__main__': main()
# -

#!jupytext --to python parameter_estimates.ipynb