text:
#-------------------------
#3d Renderer
#Daniel Miron
#7/5/2013
#
#Allows 3d viewing of nerve cord or neuron stacks.
#Includes ability to fully rotate image in 3 dimensions and to mark locations in 3-space
#-------------------------

import sys
sys.path.append(r'c:\Python27\Lib\site-packages')
sys.path.append('.')
import h5py
import numpy as np
import glob
import os

import pickle
import math
import re
import time
import threading
from Queue import Queue
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import arcball as arc

from pysqlite2 import dbapi2 as sqlite

import cv2
import select

import extractor
import input_handler as handler

from ctypes import util
try:
    from OpenGL.platform import win32
except AttributeError:
    pass

class Viewer:
    def __init__(self, location, in_q, directory, max_x, max_y):
        self.st = time.time()
        self.arcball = None

        self.directory = directory
        self.in_q =in_q
        self.max_x = max_x
        self.max_y = max_y

        self.rows = 0
        self.columns = 0
        self.layers = 0

        self.win_h = 0
        self.win_w = 0

        self.left = None
        self.slice = None
        self.pick_location = location

        self.display_list_idx = 2 #count from 1 and use first index for box
        self.display_list_dict = dict() #COLOR as key, display_list indices as value
        self.marker_color = [1., 1., 1.] #initial marker is white

        self.first = True #used to control display list flow

    def set_dimensions(self, rows, columns, layers, w):
        self.rows = rows
        self.columns = columns
        self.layers = layers
        self.pick_location = (self.pick_location[0]/pow(2, w) - 1, self.pick_location[1]/pow(2, w) - 1, self.pick_location[2])

    def main(self):
        #self.contours = self.load_contours(contour_file)
        #set window height and width
        self.win_h = 1000
        self.win_w = 1000
        self.arcball = self.create_arcball()

        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(self.win_w, self.win_h) #width, height
        glutCreateWindow("3D View")

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(65, 1, 1, 10)
        self.fov = 65
        glMatrixMode(GL_MODELVIEW)

        self.back_tesselator = gluNewTess()
        gluTessCallback(self.back_tesselator, GLU_TESS_BEGIN, glBegin)
        gluTessCallback(self.back_tesselator, GLU_TESS_END, glEnd)
        gluTessCallback(self.back_tesselator, GLU_TESS_VERTEX, self.back_vertex)

        self.front_tesselator = gluNewTess()
        gluTessCallback(self.front_tesselator, GLU_TESS_BEGIN, glBegin)
        gluTessCallback(self.front_tesselator, GLU_TESS_END, glEnd)
        gluTessCallback(self.front_tesselator, GLU_TESS_VERTEX, self.front_vertex)

        glShadeModel(GL_SMOOTH)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_NORMALIZE)
        glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION)
        glEnable(GL_COLOR_MATERIAL)

        glutDisplayFunc(self.draw)
        glutKeyboardFunc(self.keyboard)
        glutMouseFunc(self.on_click)
        glutMotionFunc(self.on_drag)
        glutMouseWheelFunc(self.on_scroll)
        glutIdleFunc(self.on_idle)
        glutReshapeFunc(self.on_resize)

        glutMainLoop()
        return

    def on_resize(self, w, h):
        self.win_h = h
        self.win_w = w
        glViewport(0,0, w,h)
        self.arcball.place([self.win_w/2, self.win_h/2], self.win_w/2)

    def on_idle(self):
            while(not self.in_q.empty()):
                temp = self.in_q.get()
                if type(temp) is list:
                    #draw contours
                    contours =  temp[0]
                    color = temp[1]
                    primary_label = temp[2]
                    normals = temp[3]
                    self.make_display_lists(contours, color/255.0, primary_label, normals)
                    self.draw()
                else:
                    #add new extractor
                    args = temp.split()
                    location = (int(args[0]), int(args[1]), int(args[2]))

                    primary_id = []
                    secondary_ids = []
                    split_str = re.split(":", args[3])
                    primary_id = [int(split_str[0])]
                    if split_str[1] != "":
                        secondary_ids = [int(label) for label in re.split(',', split_str[1])]
                    ids = [primary_id + secondary_ids]

                    extr = extractor.Extractor(self.in_q, self.directory, ids, location, max_x, max_y)
                    extracting_worker = threading.Thread(target = extr.run, name = "extr")
                    extracting_worker.daemon = True
                    extracting_worker.start()

    def refresh(self):
        #first display list is for the box
        glDeleteLists(2, self.display_list_idx)
        self.display_list_idx = 2
        self.draw()

    def undo(self):
        label = self.display_list_dict.keys()[0]
        for display_list in self.display_list_dict[label]:
            sys.stdout.flush()
            glDeleteLists(display_list, 1) #delete back and front lists
            self.display_list_idx
            self.draw()

    def create_arcball(self):
        arcball = arc.Arcball()
        #locate the arcball center at center of window with radius half the width
        arcball.place([self.win_w/2, self.win_h/2], self.win_w/2)
        return arcball

    def make_display_lists(self, contours, color, label, normals):
        if self.first:
            display_list = glGenLists(1)
            self.axes()
            self.make_box_list()
            self.first = False
        self.display_lists = glGenLists(2) #first list for front, second for back
        if label in self.display_list_dict:
            self.display_list_dict[label] = self.display_list_dict[label] + [self.display_list_idx, self.display_list_idx+1]
        else:
            self.display_list_dict[label] = [self.display_list_idx, self.display_list_idx+1]
        self.make_front_list(contours, color, normals)
        self.make_back_list(contours)
        self.display_list_idx +=2

    def make_back_list(self, contours):
        '''Creates a display list to encode color for image. Not seen by user'''
        glNewList(self.display_list_idx+1, GL_COMPILE)
        glDisable(GL_LIGHTING)

        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glTranslatef(-.9, .9, .9)
        glScalef(1.8/self.columns, -1.8/self.rows, -1.8/self.layers)
        #draw the layers
        for cnt in contours:
            gluTessBeginPolygon(self.back_tesselator, None)
            gluTessBeginContour(self.back_tesselator)
            for vtx in cnt:
                gluTessVertex(self.back_tesselator, vtx, vtx)
            gluTessEndContour(self.back_tesselator)
            gluTessEndPolygon(self.back_tesselator)

        glPopMatrix()

        glEnable(GL_LIGHTING)

        glEndList()

    def make_front_list(self, contours, color, normals):
        '''Creates a display list to draw a box and the data scaled to .9*the size of the window.
        This list deals with the display seen by the user'''

        glNewList(self.display_list_idx, GL_COMPILE)

        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glTranslatef(-.9, .9, .9)
        glScalef(1.8/self.columns, -1.8/self.rows, -1.8/self.layers)

        #draw the layers
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color)
        #glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color)
        #glColor3f(*color)
        for cnt, normal in zip(contours, normals):
        #for cnt in contours:
            gluTessBeginPolygon(self.front_tesselator, None)
            gluTessBeginContour(self.front_tesselator)
            for vtx, norm in zip(cnt, normal):
            #for vtx in cnt:
                norm /= np.linalg.norm(norm)
                #print norm
                gluTessVertex(self.front_tesselator, vtx, [vtx, norm])
            gluTessEndContour(self.front_tesselator)
            gluTessEndPolygon(self.front_tesselator)

        '''glBegin(GL_LINES)
        for cnt, normal in zip(contours, normals):
            for vtx, norm in zip(cnt, normal):
                norm/=np.linalg.norm(norm)
                #print norm
                glVertex3fv(vtx)
                glVertex3fv(vtx + 20*norm)
        glEnd()'''

        glPopMatrix()

        glEndList()

    def make_box_list(self):
        glNewList(1, GL_COMPILE)

        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()

        #make a box around the image
        glBegin(GL_LINES)
        glColor3f(1.0, 0, 0) #x in red
        glVertex3f(-.9, -.9, -.9)
        glVertex3f(.9, -.9, -.9)
        glVertex3f(-.9, .9, -.9)
        glVertex3f(.9, .9, -.9)
        glVertex3f(-.9, -.9, .9)
        glVertex3f(.9, -.9, .9)
        glVertex3f(-.9, .9, .9)
        glVertex3f(.9, .9, .9)
        glColor3f(0,1.0, 0) #y in green
        glVertex3f(-.9, -.9, -.9)
        glVertex3f(-.9, .9, -.9)
        glVertex3f(.9, -.9, -.9)
        glVertex3f(.9, .9, -.9)
        glVertex3f(-.9, .9, .9)
        glVertex3f(-.9, -.9, .9)
        glVertex3f(.9, -.9, .9)
        glVertex3f(.9, .9, .9)
        glColor3f(0,0,1.0) #z in blue
        glVertex3f(-.9, -.9, -.9)
        glVertex3f(-.9, -.9, .9)
        glVertex3f(.9, -.9, -.9)
        glVertex3f(.9, -.9, .9)
        glVertex3f(-.9, .9, -.9)
        glVertex3f(-.9, .9, .9)
        glVertex3f(.9, .9, -.9)
        glVertex3f(.9, .9, .9)
        glEnd()

        glColor3f(0.5, 0.5, 0.5)
        #Get someone to explain to me why the inital size is so large
        glScalef(.0005,.0005,.0005)
        glTranslatef(-2000, 1830, 1800)
        glutStrokeString(GLUT_STROKE_ROMAN, "(0,0,0)")
        glTranslatef(3400, 0,0)
        glutStrokeString(GLUT_STROKE_ROMAN, "x=" + str(self.columns))
        glTranslatef(-3930, -3600, 0)
        glutStrokeString(GLUT_STROKE_ROMAN, "y=" + str(self.rows))
        glTranslatef(-350, 3600, -3600)
        glutStrokeString(GLUT_STROKE_ROMAN, "z=" + str(self.layers))

        glPopMatrix()
        glEndList()

    def front_vertex(self, vert_norm):
        glNormal3fv(vert_norm[1])
        glVertex3fv(vert_norm[0])

    def back_vertex(self, vertex):
        '''sets the color of a single vertex and draws it'''
        #scale by dim-1 to include black
        #multiply by -1 and add 1 to invert color axis
        glColor3f(1.0*vertex[0]/(self.columns-1), -1.0*vertex[1]/(self.rows-1)+1.0, -1.0*vertex[2]/(self.layers-1)+1.0)
        glVertex3fv(vertex)

    def draw(self, pick=False):
        '''draws an image'''

        glMatrixMode(GL_MODELVIEW)
        glDrawBuffer(GL_BACK)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        gluLookAt(0, 0, 3, 0, 0, 2, 0,1,0)
        glMultMatrixd(self.arcball.matrix().T)

        glCallList(1)#draw the box

        if not pick:
            #even numbers for display
            for idx in range(2, self.display_list_idx+1, 2):
                glCallList(idx)
            self.draw_marker()
            glCallList(1)#draw the box
            glutSwapBuffers()
        else:
            #odd numbers for picking
            for idx in range(3, self.display_list_idx+1, 2):
                glCallList(idx)
            glFlush()

        return

    def draw_marker(self):
        '''Draws a sphere around the chosen point. Color is inverse of chosen pixel'''
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        location = self.pick_location
        glTranslatef(1.8*location[0]/self.columns-.9,
                    -(1.8*location[1]/self.rows-.9),
                    -(1.8*location[2]/self.layers-.9))
        glColor3f(*self.marker_color)
        glutSolidSphere(.01, 50, 50)

        z = 1.8*location[2]/self.layers-.9
        glBegin(GL_LINES)
        glColor3f(1.0, 0, 0) #x in red
        glVertex3f(-.9, -.9, z )
        glVertex3f(.9, -.9, z)
        glVertex3f(-.9, .9, z)
        glVertex3f(.9, .9, z)
        glColor3f(0,1.0, 0) #y in green
        glVertex3f(-.9, -.9, z)
        glVertex3f(-.9, .9, z)
        glVertex3f(.9, -.9, z)
        glVertex3f(.9, .9, z)
        glVertex3f(-.9, .9, z)
        glVertex3f(-.9, -.9, z)
        glVertex3f(.9, -.9, z)
        glVertex3f(.9, .9, z)
        glEnd()

        glPopMatrix()

    def keyboard(self, key, x, y):
        if key == chr(27): #escape to quit
            sys.exit()
        if key == chr(8): #backspace to refresh/clear
            self.refresh()
        if key == chr(117):
            self.undo()
        return

    def on_scroll(self, wheel, direction, x, y):
        '''zooms in and out on mouse scroll wheel'''
        if direction == 1:
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            self.fov = self.fov - 1
            gluPerspective(self.fov, 1, 1, 10)
            self.draw()
        else:
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            self.fov = self.fov + 1
            gluPerspective(self.fov, 1, 1, 10)
            self.draw()
            return

    def on_click(self, button, state, x, y):
        #Left click for arcball rotation
        if (button == GLUT_LEFT_BUTTON and state == GLUT_DOWN):
            self.left = True #turn on dragging rotation
            self.arcball.down((x,y))
        #right click to select a pixel location
        elif (button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN):
            self.left = False #turn off dragging rotation
            self.draw(pick=True)
            self.pick_location, self.marker_color = self.pick(x,y)
            print self.pick_location
            sys.stdout.flush()
            self.has_marker = True

    def pick(self, x,y):
        '''gets the (x,y,z) location in the full volume of a chosen pixel'''
        click_color = None
        glReadBuffer(GL_BACK)
        click_color = glReadPixels(x,self.win_h-y, 1,1, GL_RGB, GL_FLOAT)[0][0]
        location  = [int(click_color[0]*(self.columns-1)),
                    int(-(click_color[1]-1)*(self.rows-1)), int(-(click_color[2]-1)*((self.layers-1)))]
        glReadBuffer(GL_FRONT)
        marker_color_neg = glReadPixels(x,self.win_h-y, 1,1, GL_RGB, GL_FLOAT)[0][0]
        marker_color = 1-marker_color_neg
        return location, marker_color

    def on_drag(self, x, y):
        '''rotates image on dragging with left mouse down'''
        if self.left:
            self.arcball.drag((x,y))
            self.draw()

    def read_chunk_map(self, chunk_file):
        return pickle.load(open(chunk_file, "rb"))

    def save_contours(self, contour_file):
        pickle.dump(self.contours, open(contour_file, "wb"))
        return

    def load_contours(self, contour_file):
        return pickle.load(open(contour_file, "rb"))

    def axes(self):
        '''generates vertices for a box'''
        self.x_axis = [[[0,0,0], [self.columns, 0,0]], [[0,self.rows,0], [self.columns, self.rows, 0]],
                        [[0,0,self.layers], [self.columns,0,self.layers]], [[0, self.rows, self.layers], [self.columns, self.rows, self.layers]]]
        self.y_axis = [[[0,0,0], [0, self.rows,0]], [[self.columns,0,0],[self.columns, self.rows, 0]], [[0,0,self.layers], [0,self.rows, self.layers]],
                        [[self.columns, 0, self.layers],[self.columns, self.rows, self.layers]]]
        self.z_axis = [[[0,0,0], [0,0,self.layers]], [[self.columns,0,0],[self.columns, 0, self.layers]],
                        [[0, self.rows,0], [0, self.rows, self.layers]],[[self.columns, self.rows, 0],[self.columns, self.rows, self.layers]]]

if __name__ == '__main__':
    display_queue = Queue()
    sys.argv.pop(0)

    #extract command line arguments
    directory = sys.argv[0]

    location = (int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])) #x,y,z
    #resolution_level = int(sys.argv[4]) #w
    max_x =int(sys.argv[4])
    max_y = int(sys.argv[5])

    ids = []
    for label_set in (sys.argv[6:len(sys.argv)]):
        primary_id = []
        secondary_ids = []
        split_str = re.split(":", label_set)
        primary_id = [int(split_str[0])]
        if split_str[1] != "":
            secondary_ids = [int(label) for label in re.split(',', split_str[1])]
        ids += [primary_id + secondary_ids]

    extr = extractor.Extractor(display_queue, directory, ids, location, max_x, max_y)
    viewer  = Viewer(location, display_queue, directory, max_x, max_y)
    handler = handler.Input_Handler(display_queue)

    viewer.set_dimensions(extr.rows, extr.columns, extr.layers, extr.w)

    extracting_worker = threading.Thread(target = extr.run, name = "extr")
    input_worker = threading.Thread(target = handler.run, name = "input_worker")

    input_worker.daemon =True
    extracting_worker.daemon = True
    extracting_worker.start()
    input_worker.start()

    viewer.main()

max_stars_repo_name: Rhoana/rhoana

max_issues_repo_name: andudu/rhoana

max_forks_repo_name: andudu/rhoana


text:
from abc import abstractmethod
from typing import Mapping, Dict, Any
from entity_gym.environment.environment import CategoricalAction

import numpy as np
from entity_gym.environment import (
    Environment,
    Action,
    Observation,
    ActionSpace,
    EpisodeStats,
    CategoricalActionMask,
    ObsSpace,
)

import numpy.typing as npt

class GriddlyEnv(Environment):
    def __init__(self) -> None:

        # Create an instantiation of the griddly envs
        self._env = self.__class__._griddly_env()

        self._env.reset()

        self._obs_space = self.__class__.obs_space()
        self._action_space = self.__class__.action_space()

        self._current_g_state = self._env.get_state()

        self._entity_observer = self._env.game.get_entity_observer()

    @classmethod
    @abstractmethod
    def _griddly_env(cls) -> Any:
        pass

    @classmethod
    @abstractmethod
    def obs_space(cls) -> ObsSpace:
        pass

    @classmethod
    @abstractmethod
    def action_space(cls) -> Dict[str, ActionSpace]:
        pass

    def _to_griddly_action(self, action: Mapping[str, Action]) -> np.ndarray:

        if len(self._env.action_space_parts) > 2:
            entity_actions = []
            for action_name, a in action.items():
                action_type = self._env.action_names.index(action_name)
                for entity_id, action_id in a.items():
                    entity_location = self.entity_locations[entity_id]
                    entity_actions.append(
                        np.array(
                            [
                                entity_location[0],
                                entity_location[1],
                                action_type,
                                action_id,
                            ]
                        )
                    )

            return np.stack(entity_actions)
        else:

            for action_name, a in action.items():
                action_type = self._env.action_names.index(action_name)
                assert isinstance(a, CategoricalAction)
                # TODO: this only works if we have a single entity, otherwise we have to map the entityID to an x,y coordinate
                action_id = a.actions[0]

            return np.array([action_type, action_id])

    def _make_observation(self, reward: int = 0, done: bool = False) -> Observation:
        griddly_entity_observation = self._entity_observer.observe(1)
        entities = griddly_entity_observation["Entities"]
        entity_ids = griddly_entity_observation["EntityIds"]
        entity_masks = griddly_entity_observation["EntityMasks"]

        self.entity_locations = griddly_entity_observation["EntityLocations"]

        entities = {
            name: np.array(obs, dtype=np.float32) for name, obs in entities.items()
        }

        action_masks = {}
        for action_name, entity_mask in entity_masks.items():
            action_masks[action_name] = CategoricalActionMask(
                actor_ids=entity_mask["ActorEntityIds"],
                mask=np.array(entity_mask["Masks"]).astype(np.bool_),
            )

        return Observation(
            features=entities,
            ids=entity_ids,
            actions=action_masks,
            reward=reward,
            done=done,
            end_of_episode_info=EpisodeStats(self.step, self.total_reward)
            if done
            else None,
        )

    def reset(self) -> Observation:
        self._env.reset()

        self.total_reward = 0
        self.step = 0

        return self._make_observation()

    def act(self, action: Mapping[str, Action]) -> Observation:
        g_action = self._to_griddly_action(action)
        _, reward, done, info = self._env.step(g_action)

        self.total_reward += reward
        self.step += 1

        return self._make_observation(reward, done)

    def render(self, **kwargs: Any) -> npt.NDArray[np.uint8]:
        return self._env.render(**kwargs, observer="global")  # type: ignore

ext: py

max_stars_repo_name: Miffyli/incubator

max_issues_repo_name: Miffyli/incubator

max_forks_repo_name: Miffyli/incubator

reason: import numpy


text:
import numpy as np

from party_analysis import visualization_parties as vsp
from party_analysis import pie_related_plots as prp
from party_analysis import bar_related_plots as brp
from dst_characterize import identify_party as dtf_pt

country_name_dict = {"0": "US/Local", "1": "Abroad", "-1": "Unknown"}
country_color_dict = {"0": 'Reds', "1": 'Blues', "-1": 'Greens'}
country_name_too_long = {"0": 'Other US or local destinations',
                         "1": 'Other abroad destinations',
                         "-1": 'Other unknown destinations'}
patch_dict = {"0": "0",
              "1": "1",
              "2": "-1"}
protocol_details = {"TCP port: 443": "Https",
                    "TCP port: 80": "Http",
                    "UDP port: 80": "Http"}

def run(previous_data: list, company: str, fig_dir: str, fig_dpi: int,
        dst_types: list, plot_types: list, linear: bool):
    country_info_sld, \
    country_info_fqdn, \
    country_info_org, \
    abroad_all_info_sld, \
    abroad_all_info_fqdn, \
    abroad_all_info_org = read_dst_countries(previous_data)

    fig_dir = vsp.check_dir_exist(ori_path=fig_dir, new_dir="country_analysis")

    def make_pie_plot(dst_type_name, party_t_dict, pie_plot_dpi, pie_fig_dir):
        pie_fig_dir = vsp.check_dir_exist(pie_fig_dir, "pie")
        pie_fig_dir = vsp.check_dir_exist(pie_fig_dir, dst_type_name)
        dst_type_name = dst_type_name.upper()

        non_data1 = party_t_dict["1"].__len__() == 0
        non_data2 = party_t_dict["-1"].__len__() == 0
        if not non_data1 or not non_data2:
            prp.plot_dst(party_hosts_traffic=party_t_dict, party_bar_plot=["1", "2"],
                         save_name=pie_fig_dir + "/" + company + "_pie_"
                                   + dst_type_name + "_abroad_traffic.png",
                         title='The amount of traffic sent to each destination ' + dst_type_name
                               + ' in the USA and abroad (' + company.capitalize() + ' device)',
                         name_dict=country_name_dict,
                         third_party_color=[country_color_dict["1"], country_color_dict["-1"]],
                         host_name_too_long=country_name_too_long,
                         empty_parties=[non_data1, non_data2],
                         fig_dpi=pie_plot_dpi, patch_dict=patch_dict)

    def make_bar_h_plot(party_t_dict, dst_type_name, fig_dpi, barh_fig_dir):
        barh_fig_dir = vsp.check_dir_exist(barh_fig_dir, "barH")
        barh_fig_dir = vsp.check_dir_exist(barh_fig_dir, dst_type_name)
        dst_type_name = dst_type_name.upper()

        if party_t_dict.__len__() != 0:
            other_h_t = 0
            too_small_h = []
            for host in party_t_dict:
                current_t = party_t_dict[host]
                all_hosts_len = party_t_dict.__len__()
                all_t = int(np.array(list(party_t_dict.values())).sum())
                if all_hosts_len > 25 and current_t / all_t <= 0.0001:
                    other_h_t += current_t
                    too_small_h.append(host)
            if other_h_t > 0:
                party_t_dict["other abroad destinations"] = other_h_t
                for h in too_small_h:
                    del party_t_dict[h]
            brp.bar_h_plot(data=list(party_t_dict.values()), names=list(party_t_dict.keys()),
                           title="The percentages of traffic sent "
                                 "abroad to each destination " + dst_type_name
                                 + " and protocol&port and party (" + company.capitalize() + ")",
                           color_p=country_color_dict["1"], fig_dpi=fig_dpi,
                           num_name="Amount of traffic shown using log scale (Bytes)",
                           save_name=barh_fig_dir + "/" + company + "_bar_" + dst_type_name
                                     + "_abroad_traffic.png")

    def make_plot(input_plot_type: str, input_dst_type: str, all_plot_dpi: int):
        if input_plot_type == "pieplot":
            if input_dst_type == "sld":
                make_pie_plot(dst_type_name=input_dst_type, party_t_dict=country_info_sld,
                              pie_plot_dpi=all_plot_dpi, pie_fig_dir=fig_dir)

            elif input_dst_type == "fqdn":
                make_pie_plot(dst_type_name=input_dst_type, party_t_dict=country_info_fqdn,
                              pie_plot_dpi=all_plot_dpi, pie_fig_dir=fig_dir)

            elif input_dst_type == "org":
                make_pie_plot(dst_type_name=input_dst_type, party_t_dict=country_info_org,
                              pie_plot_dpi=all_plot_dpi, pie_fig_dir=fig_dir)

        elif input_plot_type == "barhplot":
            if input_dst_type == "sld":
                make_bar_h_plot(party_t_dict=abroad_all_info_sld, dst_type_name=input_dst_type,
                                fig_dpi=all_plot_dpi, barh_fig_dir=fig_dir)

            elif input_dst_type == "fqdn":
                make_bar_h_plot(party_t_dict=abroad_all_info_fqdn, dst_type_name=input_dst_type,
                                fig_dpi=all_plot_dpi, barh_fig_dir=fig_dir)

            elif input_dst_type == "org":
                make_bar_h_plot(party_t_dict=abroad_all_info_org, dst_type_name=input_dst_type,
                                fig_dpi=all_plot_dpi, barh_fig_dir=fig_dir)

    if linear:
        for plot_type, dst_type in zip(plot_types, dst_types):
            make_plot(plot_type, dst_type, fig_dpi)

    else:
        for plot_type in plot_types:
            for dst_type in dst_types:
                make_plot(plot_type, dst_type, fig_dpi)

def read_dst_countries(result):
    def add_to_pie_groups(group_dict, dst: str):
        c_name = country + ' (' + dst + ')'
        if dtf_pt.detect_local_host(host=country) or country == "US":
            if c_name in group_dict["0"]:
                group_dict["0"][c_name] += traffic
            else:
                group_dict["0"][c_name] = traffic
        elif country.startswith("Likely") \
                or country == "Unknown":
            if c_name in group_dict["-1"]:
                group_dict["-1"][c_name] += traffic
            else:
                group_dict["-1"][c_name] = traffic
        else:
            if c_name in group_dict["1"]:
                group_dict["1"][c_name] += traffic
            else:
                group_dict["1"][c_name] = traffic

    def add_to_bar_plot(plot_group, c_name):
        c_name = country + " (" + c_name + "-" + protocol_port + ")"
        if c_name in plot_group:
            plot_group[c_name] += traffic
        else:
            plot_group[c_name] = traffic

    country_info_sld = {"0": {}, "1": {}, "-1": {}}
    country_info_fqdn = {"0": {}, "1": {}, "-1": {}}
    country_info_org = {"0": {}, "1": {}, "-1": {}}
    abroad_all_info_sld = {}
    abroad_all_info_fqdn = {}
    abroad_all_info_org = {}
    for dst_pro in result:
        host = dst_pro.host.host
        host_full = dst_pro.host.host_full
        org = dst_pro.host.organization
        protocol_port = dst_pro.protocol_port.protocol_port
        traffic = dst_pro.snd
        country: str = dst_pro.host.country

        if protocol_port in protocol_details:
            protocol_port = protocol_details[protocol_port]

        add_to_pie_groups(country_info_sld, host)
        add_to_pie_groups(country_info_fqdn, host_full)
        add_to_pie_groups(country_info_org, org)

        if country != "US" and country != "Local" and not country.startswith("Unknown"):
            add_to_bar_plot(abroad_all_info_sld, host)
            add_to_bar_plot(abroad_all_info_fqdn, host_full)
            add_to_bar_plot(abroad_all_info_org, org)

    return country_info_sld, \
           country_info_fqdn, \
           country_info_org, \
           abroad_all_info_sld, \
           abroad_all_info_fqdn, \
           abroad_all_info_org


text:
import mne
import numpy as np
import os.path as op

def score_acdc(p):
    raw_dir = p.work_dir
    f_name = p.run_names[0]
    subject = p.subjects[0]
    standard = []
    oddball1 = []
    oddball2 = []
    raw_path = op.join(raw_dir, subject, 'raw_fif', f_name % subject + '_raw.fif')
    event_fname=op.join(raw_dir, subject, 'lists', 'ALL_acdc_eeg_meg_adult_actual-eve.lst')
    raw = mne.io.read_raw_fif(raw_path, allow_maxshield=True)
    ones = mne.find_events(raw, mask=1)
    events = mne.find_events(raw)
    for one in ones:
        category=0
        event = [e for e in events if e[0]==one[0]]
        event_idx = int(np.where(events[:, 0] == event[0][0])[0])
        one_back = events[event_idx-1]
        try:
            two_back = events[event_idx-2]
        except Error:
            two_back = [0, 0, 1]
        add_ob = 3 if one_back[2]==8 else 1
        add_tb = 1 if two_back[2]==4 else 0
        category += add_ob
        category += add_tb
        one[2]=category
    assert (np.diff(ones[:, 0]) > 0).all()
    mne.write_events(event_fname, ones)

def score_acdc_eeg(p):
    raw_dir = p.work_dir
    f_name = p.run_names[0]
    subject = p.subjects[0]
    raw_path = op.join(raw_dir, subject, 'raw_fif', f_name % subject + '_raw.fif')
    event_fname=op.join(raw_dir, subject, 'lists', 'ALL_acdc_eeg_meg_adult_actual-eve.lst')
    raw = mne.io.read_raw_fif(raw_path, allow_maxshield=True)
    events = mne.find_events(raw, stim_channel='STI 014')
    events[:, 2] = events[:, 2]*10
    onsets = [e for e in events if e[2]==20]
    for onset in onsets:
        category=0
        event = [e for e in events if e[0]==onset[0]]
        event_idx = int(np.where(events[:, 0] == event[0][0])[0])
        one_back = events[event_idx-1]
        try:
            two_back = events[event_idx-2]
        except Error:
            two_back = [0, 0, 2]
        add_ob = 3 if one_back[2]==30 else 1
        add_tb = 1 if two_back[2]==10 else 0
        category += add_ob
        category += add_tb
        onset[2]=category
    assert (np.diff(onsets[:, 0]) > 0).all()
    mne.write_events(event_fname, onsets)


text:
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

import copy

import pandas as pd
import numpy as np
import numpy.testing as npt

from skbio.util._testing import assert_data_frame_almost_equal

class MetadataMixinTests:
    def test_constructor_invalid_type(self):
        for md in (0, 'a', ('f', 'o', 'o'), np.array([]), pd.DataFrame()):
            with self.assertRaisesRegex(TypeError, 'metadata must be a dict'):
                self._metadata_constructor_(metadata=md)

    def test_constructor_no_metadata(self):
        for md in None, {}:
            obj = self._metadata_constructor_(metadata=md)

            self.assertEqual(obj.metadata, {})

    def test_constructor_with_metadata(self):
        obj = self._metadata_constructor_(metadata={'foo': 'bar'})
        self.assertEqual(obj.metadata, {'foo': 'bar'})

        obj = self._metadata_constructor_(
                metadata={'': '', 123: {'a': 'b', 'c': 'd'}})
        self.assertEqual(obj.metadata, {'': '', 123: {'a': 'b', 'c': 'd'}})

    def test_constructor_makes_shallow_copy_of_metadata(self):
        md = {'foo': 'bar', 42: []}
        obj = self._metadata_constructor_(metadata=md)

        self.assertEqual(obj.metadata, md)
        self.assertIsNot(obj.metadata, md)

        md['foo'] = 'baz'
        self.assertEqual(obj.metadata, {'foo': 'bar', 42: []})

        md[42].append(True)
        self.assertEqual(obj.metadata, {'foo': 'bar', 42: [True]})

    def test_eq(self):
        self.assertReallyEqual(
                self._metadata_constructor_(metadata={'foo': 42}),
                self._metadata_constructor_(metadata={'foo': 42}))

        self.assertReallyEqual(
                self._metadata_constructor_(metadata={'foo': 42, 123: {}}),
                self._metadata_constructor_(metadata={'foo': 42, 123: {}}))

    def test_eq_missing_metadata(self):
        self.assertReallyEqual(self._metadata_constructor_(),
                               self._metadata_constructor_())
        self.assertReallyEqual(self._metadata_constructor_(),
                               self._metadata_constructor_(metadata={}))
        self.assertReallyEqual(self._metadata_constructor_(metadata={}),
                               self._metadata_constructor_(metadata={}))

    def test_ne(self):
        # Both have metadata.
        obj1 = self._metadata_constructor_(metadata={'id': 'foo'})
        obj2 = self._metadata_constructor_(metadata={'id': 'bar'})
        self.assertReallyNotEqual(obj1, obj2)

        # One has metadata.
        obj1 = self._metadata_constructor_(metadata={'id': 'foo'})
        obj2 = self._metadata_constructor_()
        self.assertReallyNotEqual(obj1, obj2)

    def test_copy_metadata_none(self):
        obj = self._metadata_constructor_()
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {})
        self.assertEqual(obj_copy.metadata, {})
        self.assertIsNot(obj.metadata, obj_copy.metadata)

    def test_copy_metadata_empty(self):
        obj = self._metadata_constructor_(metadata={})
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {})
        self.assertEqual(obj_copy.metadata, {})
        self.assertIsNot(obj.metadata, obj_copy.metadata)

    def test_copy_with_metadata(self):
        obj = self._metadata_constructor_(metadata={'foo': [1]})
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {'foo': [1]})
        self.assertEqual(obj_copy.metadata, {'foo': [1]})
        self.assertIsNot(obj.metadata, obj_copy.metadata)
        self.assertIs(obj.metadata['foo'], obj_copy.metadata['foo'])

        obj_copy.metadata['foo'].append(2)
        obj_copy.metadata['foo2'] = 42

        self.assertEqual(obj_copy.metadata, {'foo': [1, 2], 'foo2': 42})
        self.assertEqual(obj.metadata, {'foo': [1, 2]})

    def test_deepcopy_metadata_none(self):
        obj = self._metadata_constructor_()
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {})
        self.assertEqual(obj_copy.metadata, {})
        self.assertIsNot(obj.metadata, obj_copy.metadata)

    def test_deepcopy_metadata_empty(self):
        obj = self._metadata_constructor_(metadata={})
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {})
        self.assertEqual(obj_copy.metadata, {})
        self.assertIsNot(obj.metadata, obj_copy.metadata)

    def test_deepcopy_with_metadata(self):
        obj = self._metadata_constructor_(metadata={'foo': [1]})
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        self.assertEqual(obj.metadata, {'foo': [1]})
        self.assertEqual(obj_copy.metadata, {'foo': [1]})
        self.assertIsNot(obj.metadata, obj_copy.metadata)
        self.assertIsNot(obj.metadata['foo'], obj_copy.metadata['foo'])

        obj_copy.metadata['foo'].append(2)
        obj_copy.metadata['foo2'] = 42

        self.assertEqual(obj_copy.metadata, {'foo': [1, 2], 'foo2': 42})
        self.assertEqual(obj.metadata, {'foo': [1]})

    def test_deepcopy_memo_is_respected(self):
        # Basic test to ensure deepcopy's memo is passed through to recursive
        # deepcopy calls.
        obj = self._metadata_constructor_(metadata={'foo': 'bar'})
        memo = {}
        copy.deepcopy(obj, memo)
        self.assertGreater(len(memo), 2)

    def test_metadata_getter(self):
        obj = self._metadata_constructor_(
                metadata={42: 'foo', ('hello', 'world'): 43})

        self.assertIsInstance(obj.metadata, dict)
        self.assertEqual(obj.metadata, {42: 'foo', ('hello', 'world'): 43})

        obj.metadata[42] = 'bar'
        self.assertEqual(obj.metadata, {42: 'bar', ('hello', 'world'): 43})

    def test_metadata_getter_no_metadata(self):
        obj = self._metadata_constructor_()

        self.assertIsInstance(obj.metadata, dict)
        self.assertEqual(obj.metadata, {})

    def test_metadata_setter(self):
        obj = self._metadata_constructor_()
        self.assertEqual(obj.metadata, {})

        obj.metadata = {'hello': 'world'}
        self.assertEqual(obj.metadata, {'hello': 'world'})

        obj.metadata = {}
        self.assertEqual(obj.metadata, {})

    def test_metadata_setter_makes_shallow_copy(self):
        obj = self._metadata_constructor_()

        md = {'foo': 'bar', 42: []}
        obj.metadata = md

        self.assertEqual(obj.metadata, md)
        self.assertIsNot(obj.metadata, md)

        md['foo'] = 'baz'
        self.assertEqual(obj.metadata, {'foo': 'bar', 42: []})

        md[42].append(True)
        self.assertEqual(obj.metadata, {'foo': 'bar', 42: [True]})

    def test_metadata_setter_invalid_type(self):
        obj = self._metadata_constructor_(metadata={123: 456})

        for md in (None, 0, 'a', ('f', 'o', 'o'), np.array([]),
                   pd.DataFrame()):
            with self.assertRaisesRegex(TypeError, 'metadata must be a dict'):
                obj.metadata = md
            self.assertEqual(obj.metadata, {123: 456})

    def test_metadata_deleter(self):
        obj = self._metadata_constructor_(metadata={'foo': 'bar'})

        self.assertEqual(obj.metadata, {'foo': 'bar'})

        del obj.metadata
        self.assertEqual(obj.metadata, {})

        # Delete again.
        del obj.metadata
        self.assertEqual(obj.metadata, {})

        obj = self._metadata_constructor_()

        self.assertEqual(obj.metadata, {})
        del obj.metadata
        self.assertEqual(obj.metadata, {})

    def test_has_metadata(self):
        obj = self._metadata_constructor_()

        self.assertFalse(obj.has_metadata())

        self.assertFalse(
                self._metadata_constructor_(metadata={}).has_metadata())

        self.assertTrue(
                self._metadata_constructor_(metadata={'': ''}).has_metadata())
        self.assertTrue(
                self._metadata_constructor_(
                        metadata={'foo': 42}).has_metadata())

class PositionalMetadataMixinTests:
    def test_constructor_invalid_positional_metadata_type(self):
        with self.assertRaisesRegex(TypeError,
                                    'Invalid positional metadata. Must be '
                                    'consumable by `pd.DataFrame` constructor.'
                                    ' Original pandas error message: '):
            self._positional_metadata_constructor_(0, positional_metadata=2)

    def test_constructor_positional_metadata_len_mismatch(self):
        # Zero elements.
        with self.assertRaisesRegex(ValueError, '\(0\).*\(4\)'):
            self._positional_metadata_constructor_(4, positional_metadata=[])

        # Not enough elements.
        with self.assertRaisesRegex(ValueError, '\(3\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=[2, 3, 4])

        # Too many elements.
        with self.assertRaisesRegex(ValueError, '\(5\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=[2, 3, 4, 5, 6])

        # Series not enough rows.
        with self.assertRaisesRegex(ValueError, '\(3\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=pd.Series(range(3)))

        # Series too many rows.
        with self.assertRaisesRegex(ValueError, '\(5\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=pd.Series(range(5)))

        # DataFrame not enough rows.
        with self.assertRaisesRegex(ValueError, '\(3\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=pd.DataFrame({'quality': range(3)}))

        # DataFrame too many rows.
        with self.assertRaisesRegex(ValueError, '\(5\).*\(4\)'):
            self._positional_metadata_constructor_(
                4, positional_metadata=pd.DataFrame({'quality': range(5)}))

        # Empty DataFrame wrong size.
        with self.assertRaisesRegex(ValueError, '\(2\).*\(3\)'):
            self._positional_metadata_constructor_(
                3, positional_metadata=pd.DataFrame(index=range(2)))

    def test_constructor_no_positional_metadata(self):
        # Length zero with missing/empty positional metadata.
        for empty in None, {}, pd.DataFrame():
            obj = self._positional_metadata_constructor_(
                0, positional_metadata=empty)

            assert_data_frame_almost_equal(obj.positional_metadata,
                                           pd.DataFrame(index=np.arange(0)))

        # Nonzero length with missing positional metadata.
        obj = self._positional_metadata_constructor_(
            3, positional_metadata=None)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=np.arange(3)))

    def test_constructor_with_positional_metadata_len_zero(self):
        for data in [], (), np.array([]):
            obj = self._positional_metadata_constructor_(
                0, positional_metadata={'foo': data})

            assert_data_frame_almost_equal(
                obj.positional_metadata,
                pd.DataFrame({'foo': data}, index=np.arange(0)))

    def test_constructor_with_positional_metadata_len_one(self):
        for data in [2], (2, ), np.array([2]):
            obj = self._positional_metadata_constructor_(
                1, positional_metadata={'foo': data})

            assert_data_frame_almost_equal(
                obj.positional_metadata,
                pd.DataFrame({'foo': data}, index=np.arange(1)))

    def test_constructor_with_positional_metadata_len_greater_than_one(self):
        for data in ([0, 42, 42, 1, 0, 8, 100, 0, 0],
                     (0, 42, 42, 1, 0, 8, 100, 0, 0),
                     np.array([0, 42, 42, 1, 0, 8, 100, 0, 0])):
            obj = self._positional_metadata_constructor_(
                9, positional_metadata={'foo': data})

            assert_data_frame_almost_equal(
                obj.positional_metadata,
                pd.DataFrame({'foo': data}, index=np.arange(9)))

    def test_constructor_with_positional_metadata_multiple_columns(self):
        obj = self._positional_metadata_constructor_(
            5, positional_metadata={'foo': np.arange(5),
                                    'bar': np.arange(5)[::-1]})

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': np.arange(5),
                          'bar': np.arange(5)[::-1]}, index=np.arange(5)))

    def test_constructor_with_positional_metadata_custom_index(self):
        df = pd.DataFrame({'foo': np.arange(5), 'bar': np.arange(5)[::-1]},
                          index=['a', 'b', 'c', 'd', 'e'])
        obj = self._positional_metadata_constructor_(
            5, positional_metadata=df)

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': np.arange(5),
                          'bar': np.arange(5)[::-1]}, index=np.arange(5)))

    def test_constructor_makes_shallow_copy_of_positional_metadata(self):
        df = pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                          index=['a', 'b', 'c'])
        obj = self._positional_metadata_constructor_(
            3, positional_metadata=df)

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))
        self.assertIsNot(obj.positional_metadata, df)

        # Original df is not mutated.
        orig_df = pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                               index=['a', 'b', 'c'])
        assert_data_frame_almost_equal(df, orig_df)

        # Change values of column (using same dtype).
        df['foo'] = [42, 42, 42]
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))

        # Change single value of underlying data.
        df.values[0][0] = 10
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))

        # Mutate list (not a deep copy).
        df['bar'][0].append(42)
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[42], [], []]},
                         index=np.arange(3)))

    def test_eq_basic(self):
        obj1 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})
        obj2 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})
        self.assertReallyEqual(obj1, obj2)

    def test_eq_from_different_source(self):
        obj1 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': np.array([1, 2, 3])})
        obj2 = self._positional_metadata_constructor_(
            3, positional_metadata=pd.DataFrame({'foo': [1, 2, 3]},
                                                index=['foo', 'bar', 'baz']))
        self.assertReallyEqual(obj1, obj2)

    def test_eq_missing_positional_metadata(self):
        for empty in None, {}, pd.DataFrame(), pd.DataFrame(index=[]):
            obj = self._positional_metadata_constructor_(
                0, positional_metadata=empty)

            self.assertReallyEqual(
                obj,
                self._positional_metadata_constructor_(0))
            self.assertReallyEqual(
                obj,
                self._positional_metadata_constructor_(
                    0, positional_metadata=empty))

        for empty in None, pd.DataFrame(index=['a', 'b']):
            obj = self._positional_metadata_constructor_(
                2, positional_metadata=empty)

            self.assertReallyEqual(
                obj,
                self._positional_metadata_constructor_(2))
            self.assertReallyEqual(
                obj,
                self._positional_metadata_constructor_(
                    2, positional_metadata=empty))

    def test_ne_len_zero(self):
        # Both have positional metadata.
        obj1 = self._positional_metadata_constructor_(
            0, positional_metadata={'foo': []})
        obj2 = self._positional_metadata_constructor_(
            0, positional_metadata={'foo': [], 'bar': []})
        self.assertReallyNotEqual(obj1, obj2)

        # One has positional metadata.
        obj1 = self._positional_metadata_constructor_(
            0, positional_metadata={'foo': []})
        obj2 = self._positional_metadata_constructor_(0)
        self.assertReallyNotEqual(obj1, obj2)

    def test_ne_len_greater_than_zero(self):
        # Both have positional metadata.
        obj1 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})
        obj2 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 2]})
        self.assertReallyNotEqual(obj1, obj2)

        # One has positional metadata.
        obj1 = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})
        obj2 = self._positional_metadata_constructor_(3)
        self.assertReallyNotEqual(obj1, obj2)

    def test_ne_len_mismatch(self):
        obj1 = self._positional_metadata_constructor_(
            3, positional_metadata=pd.DataFrame(index=range(3)))
        obj2 = self._positional_metadata_constructor_(
            2, positional_metadata=pd.DataFrame(index=range(2)))
        self.assertReallyNotEqual(obj1, obj2)

    def test_copy_positional_metadata_none(self):
        obj = self._positional_metadata_constructor_(3)
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        assert_data_frame_almost_equal(obj_copy.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        self.assertIsNot(obj.positional_metadata, obj_copy.positional_metadata)

    def test_copy_positional_metadata_empty(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata=pd.DataFrame(index=range(3)))
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        assert_data_frame_almost_equal(obj_copy.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        self.assertIsNot(obj.positional_metadata, obj_copy.positional_metadata)

    def test_copy_with_positional_metadata(self):
        obj = self._positional_metadata_constructor_(
            4, positional_metadata={'bar': [[], [], [], []],
                                    'baz': [42, 42, 42, 42]})
        obj_copy = copy.copy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'bar': [[], [], [], []],
                          'baz': [42, 42, 42, 42]}, index=range(4)))
        assert_data_frame_almost_equal(
            obj_copy.positional_metadata,
            pd.DataFrame({'bar': [[], [], [], []],
                          'baz': [42, 42, 42, 42]}, index=range(4)))

        self.assertIsNot(obj.positional_metadata,
                         obj_copy.positional_metadata)
        self.assertIsNot(obj.positional_metadata.values,
                         obj_copy.positional_metadata.values)
        self.assertIs(obj.positional_metadata.loc[0, 'bar'],
                      obj_copy.positional_metadata.loc[0, 'bar'])

        obj_copy.positional_metadata.loc[0, 'bar'].append(1)
        obj_copy.positional_metadata.loc[0, 'baz'] = 43

        assert_data_frame_almost_equal(
            obj_copy.positional_metadata,
            pd.DataFrame({'bar': [[1], [], [], []],
                          'baz': [43, 42, 42, 42]}))
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'bar': [[1], [], [], []],
                          'baz': [42, 42, 42, 42]}))

    def test_deepcopy_positional_metadata_none(self):
        obj = self._positional_metadata_constructor_(3)
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        assert_data_frame_almost_equal(obj_copy.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        self.assertIsNot(obj.positional_metadata, obj_copy.positional_metadata)

    def test_deepcopy_positional_metadata_empty(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata=pd.DataFrame(index=range(3)))
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        assert_data_frame_almost_equal(obj_copy.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        self.assertIsNot(obj.positional_metadata, obj_copy.positional_metadata)

    def test_deepcopy_with_positional_metadata(self):
        obj = self._positional_metadata_constructor_(
            4, positional_metadata={'bar': [[], [], [], []],
                                    'baz': [42, 42, 42, 42]})
        obj_copy = copy.deepcopy(obj)

        self.assertEqual(obj, obj_copy)
        self.assertIsNot(obj, obj_copy)

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'bar': [[], [], [], []],
                          'baz': [42, 42, 42, 42]}, index=range(4)))
        assert_data_frame_almost_equal(
            obj_copy.positional_metadata,
            pd.DataFrame({'bar': [[], [], [], []],
                          'baz': [42, 42, 42, 42]}, index=range(4)))

        self.assertIsNot(obj.positional_metadata, obj_copy.positional_metadata)
        self.assertIsNot(obj.positional_metadata.values,
                         obj_copy.positional_metadata.values)
        self.assertIsNot(obj.positional_metadata.loc[0, 'bar'],
                         obj_copy.positional_metadata.loc[0, 'bar'])

        obj_copy.positional_metadata.loc[0, 'bar'].append(1)
        obj_copy.positional_metadata.loc[0, 'baz'] = 43

        assert_data_frame_almost_equal(
            obj_copy.positional_metadata,
            pd.DataFrame({'bar': [[1], [], [], []],
                          'baz': [43, 42, 42, 42]}))
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'bar': [[], [], [], []],
                          'baz': [42, 42, 42, 42]}))

    def test_deepcopy_memo_is_respected(self):
        # Basic test to ensure deepcopy's memo is passed through to recursive
        # deepcopy calls.
        obj = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})
        memo = {}
        copy.deepcopy(obj, memo)
        self.assertGreater(len(memo), 2)

    def test_positional_metadata_getter(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [22, 22, 0]})

        self.assertIsInstance(obj.positional_metadata, pd.DataFrame)
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [22, 22, 0]}))

        # Update existing column.
        obj.positional_metadata['foo'] = [42, 42, 43]
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [42, 42, 43]}))

        # Add new column.
        obj.positional_metadata['foo2'] = [True, False, True]
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [42, 42, 43],
                          'foo2': [True, False, True]}))

    def test_positional_metadata_getter_no_positional_metadata(self):
        obj = self._positional_metadata_constructor_(4)

        self.assertIsInstance(obj.positional_metadata, pd.DataFrame)
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame(index=np.arange(4)))

    def test_positional_metadata_getter_set_column_series(self):
        length = 8
        obj = self._positional_metadata_constructor_(
            length, positional_metadata={'foo': range(length)})

        obj.positional_metadata['bar'] = pd.Series(range(length-3))
        # pandas.Series will be padded with NaN if too short.
        npt.assert_equal(obj.positional_metadata['bar'],
                         np.array(list(range(length-3)) + [np.nan]*3))

        obj.positional_metadata['baz'] = pd.Series(range(length+3))
        # pandas.Series will be truncated if too long.
        npt.assert_equal(obj.positional_metadata['baz'],
                         np.array(range(length)))

    def test_positional_metadata_getter_set_column_array(self):
        length = 8
        obj = self._positional_metadata_constructor_(
            length, positional_metadata={'foo': range(length)})

        # array-like objects will fail if wrong size.
        for array_like in (np.array(range(length-1)), range(length-1),
                           np.array(range(length+1)), range(length+1)):
            with self.assertRaisesRegex(ValueError,
                                        "Length of values does not match "
                                        "length of index"):
                obj.positional_metadata['bar'] = array_like

    def test_positional_metadata_setter_pandas_consumable(self):
        obj = self._positional_metadata_constructor_(3)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))

        obj.positional_metadata = {'foo': [3, 2, 1]}
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [3, 2, 1]}))

        obj.positional_metadata = pd.DataFrame(index=np.arange(3))
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=np.arange(3)))

    def test_positional_metadata_setter_data_frame(self):
        obj = self._positional_metadata_constructor_(3)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))

        obj.positional_metadata = pd.DataFrame({'foo': [3, 2, 1]},
                                               index=['a', 'b', 'c'])
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [3, 2, 1]}))

        obj.positional_metadata = pd.DataFrame(index=np.arange(3))
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=np.arange(3)))

    def test_positional_metadata_setter_none(self):
        obj = self._positional_metadata_constructor_(
            0, positional_metadata={'foo': []})

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': []}))

        # `None` behavior differs from constructor.
        obj.positional_metadata = None

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=np.arange(0)))

    def test_positional_metadata_setter_makes_shallow_copy(self):
        obj = self._positional_metadata_constructor_(3)

        df = pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                          index=['a', 'b', 'c'])
        obj.positional_metadata = df

        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))
        self.assertIsNot(obj.positional_metadata, df)

        # Original df is not mutated.
        orig_df = pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                               index=['a', 'b', 'c'])
        assert_data_frame_almost_equal(df, orig_df)

        # Change values of column (using same dtype).
        df['foo'] = [42, 42, 42]
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))

        # Change single value of underlying data.
        df.values[0][0] = 10
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[], [], []]},
                         index=np.arange(3)))

        # Mutate list (not a deep copy).
        df['bar'][0].append(42)
        assert_data_frame_almost_equal(
            obj.positional_metadata,
            pd.DataFrame({'foo': [22, 22, 0], 'bar': [[42], [], []]},
                         index=np.arange(3)))

    def test_positional_metadata_setter_invalid_type(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 42]})

        with self.assertRaisesRegex(TypeError,
                                    'Invalid positional metadata. Must be '
                                    'consumable by `pd.DataFrame` constructor.'
                                    ' Original pandas error message: '):
            obj.positional_metadata = 2

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [1, 2, 42]}))

    def test_positional_metadata_setter_len_mismatch(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 42]})

        # `None` behavior differs from constructor.
        with self.assertRaisesRegex(ValueError, '\(0\).*\(3\)'):
            obj.positional_metadata = None

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [1, 2, 42]}))

        with self.assertRaisesRegex(ValueError, '\(4\).*\(3\)'):
            obj.positional_metadata = [1, 2, 3, 4]

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [1, 2, 42]}))

    def test_positional_metadata_deleter(self):
        obj = self._positional_metadata_constructor_(
            3, positional_metadata={'foo': [1, 2, 3]})

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame({'foo': [1, 2, 3]}))

        del obj.positional_metadata
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))

        # Delete again.
        del obj.positional_metadata
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))

        obj = self._positional_metadata_constructor_(3)

        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))
        del obj.positional_metadata
        assert_data_frame_almost_equal(obj.positional_metadata,
                                       pd.DataFrame(index=range(3)))

    def test_has_positional_metadata(self):
        obj = self._positional_metadata_constructor_(4)
        self.assertFalse(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(0, positional_metadata={})
        self.assertFalse(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(
            4, positional_metadata=pd.DataFrame(index=np.arange(4)))
        self.assertFalse(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(
            4, positional_metadata=pd.DataFrame(index=['a', 'b', 'c', 'd']))
        self.assertFalse(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(
            0, positional_metadata={'foo': []})
        self.assertTrue(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(
            4, positional_metadata={'foo': [1, 2, 3, 4]})
        self.assertTrue(obj.has_positional_metadata())

        obj = self._positional_metadata_constructor_(
            2, positional_metadata={'foo': [1, 2], 'bar': ['abc', 'def']})
        self.assertTrue(obj.has_positional_metadata())

max_stars_repo_name: squirrelo/scikit-bio

max_issues_repo_name: squirrelo/scikit-bio

max_forks_repo_name: squirrelo/scikit-bio


text:
#!/usr/bin/env python
import sys
sys.path.insert(0, "../../python/")
import mxnet as mx
import numpy as np

keys = [3, 5, 7]
# let the last shape exceed MXNET_KVSTORE_BIGARRAY_BOUND
shapes = [(4, 4), (100, 100), (2000, 2000)];

lr = .1
nworker = 4
nrepeat = 10

## generate data
data = [[[np.random.random(s)*2-1 for i in range(nworker)] for s in shapes] for j in range(nrepeat)]

## individual key interface
def test_kvstore(kv_type):
    print kv_type
    kv = mx.kv.create(kv_type)
    kv.set_optimizer(mx.optimizer.create('test', lr))
    for k, s in zip(keys, shapes):
        kv.init(k, mx.nd.zeros(s))

    res = [np.zeros(s) for s in shapes]
    for i in range(nrepeat):
        for j in range(len(keys)):
            kv.push(keys[j], [mx.nd.array(
                data[i][j][g], mx.gpu(g)) for g in range(nworker)])

        res = [a + b * lr for a, b in zip(res, [sum(d) for d in data[i]])]
        for j in range(len(keys)):
            out = [mx.nd.zeros(shapes[j], mx.gpu(g)) for g in range(nworker)]
            kv.pull(keys[j], out=out)
            err = [np.sum(np.abs(o.asnumpy() - res[j])) for o in out]
            err = sum(err) / np.sum(np.abs(res[j]))
            assert(err < 1e-6), (err, shapes[j])

test_kvstore('local_update_cpu')
test_kvstore('local_allreduce_cpu')
test_kvstore('local_allreduce_device')

## group keys interface
def test_group_kvstore(kv_type):
    print kv_type
    kv = mx.kv.create(kv_type)
    kv.set_optimizer(mx.optimizer.create('test', lr))
    kv.init(keys, [mx.nd.zeros(s) for s in shapes])
    res = [np.zeros(s) for s in shapes]
    out = [[mx.nd.zeros(s, mx.gpu(g)) for g in range(nworker)] for s in shapes]
    for i in range(nrepeat):
        kv.push(keys, [[
            mx.nd.array(data[i][j][g], mx.gpu(g)) for g in range(nworker)]
                       for j in range(len(keys))])

        kv.pull(keys, out=out)
        res = [a + b * lr for a, b in zip(res, [sum(d) for d in data[i]])]
        for a, b in zip(res, out):
            err = [np.sum(np.abs(o.asnumpy() - a)) for o in b]
            err = sum(err) / np.sum(np.abs(a))
            assert(err < 1e-6), (err, a.shape)

test_group_kvstore('local_update_cpu')
test_group_kvstore('local_allreduce_cpu')
test_group_kvstore('local_allreduce_device')

max_stars_repo_name: ranxian/mxnet-gpups

max_issues_repo_name: ranxian/mxnet-gpups

max_forks_repo_name: ranxian/mxnet-gpups


text:
import numpy as np
import matplotlib.pyplot as plt
import cv2
from skimage import data
from skimage.feature import register_translation
from skimage.feature.register_translation import _upsampled_dft
from scipy.ndimage import fourier_shift
from skimage import transform as tf
import time
import os
import argparse
import video

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser(description='crack detection video')
    parser.add_argument('-v','--video',dest='video',help='video file',required=True)
    parser.add_argument('-o','--output',dest='output',help='Output file', default = 'output.png')
    parser.add_argument('-f','--frame',dest='frame',help='Frame file', default = 'frame.png')
    parser.add_argument('-b','--output_bin',dest='output_bin',help='Binary file', default = 'output_bin.png')
    parser.add_argument('-s','--output_stiching',dest='output_stiching',help='Stiching file', default = 'output_stiching.png')
    parser.add_argument('-t','--type',dest='type' ,default = '2')
    parser.add_argument('-w','--width',dest='width',default= '30')
    args = parser.parse_args()

    return args

def blend(imageA,imageB):

    if len(np.shape(imageA))==3:
          maskA=imageA[:,:,0]>0
          maskB=imageB[:,:,0]>0
          mask_diff=(1-maskB)*maskA
          result=imageB
          result[:,:,0]=imageB[:,:,0]+mask_diff*imageA[:,:,0]
          result[:,:,1]=imageB[:,:,1]+mask_diff*imageA[:,:,1]
          result[:,:,2]=imageB[:,:,2]+mask_diff*imageA[:,:,2]
          ra,ca=np.where(maskA>0)
          rb,cb=np.where(maskB>0)
          r1a=min(ra)
          r2a=max(ra)
          c1a=min(ca)
          c2a=max(ca)
          r1b=min(rb)
          r2b=max(rb)
          c1b=min(cb)
          c2b=max(cb)
          r1=min(r1a,r1b)
          r2=min(r2a,r2b)
          c1=min(c1a,c1b)
          c2=min(c2a,c2b)
          k=10;
          result=result[r1+k:r2-k,c1+k:c2-k,:]
    else:
          maskA=imageA>0
          maskB=imageB>0
          mask_diff=(1-maskB)*maskA
          result=imageB
          result=imageB+mask_diff*imageA
          ra,ca=np.where(maskA>0)
          rb,cb=np.where(maskB>0)
          r1a=min(ra)
          r2a=max(ra)
          c1a=min(ca)
          c2a=max(ca)
          r1b=min(rb)
          r2b=max(rb)
          c1b=min(cb)
          c2b=max(cb)
          r1=min(r1a,r1b)
          r2=min(r2a,r2b)
          c1=min(c1a,c1b)
          c2=min(c2a,c2b)
          k=1;
         # result=result[r1+k:r2-k,c1+k:c2-k]

    #mask_sum=maskA+maskB;

    return result

def image_remove_pad(image):
     if len(np.shape(image))==3:
          mask=image[:,:,0]>0
          ra,ca=np.where(mask>0)
          r1=min(ra)
          r2=max(ra)
          c1=min(ca)
          c2=max(ca)
          k=1
          image=image[r1+k:r2-k,c1+k:c2-k,:]
     else:
          mask=image>0
          ra,ca=np.where(mask>0)
          r1=min(ra)
          r2=max(ra)
          c1=min(ca)
          c2=max(ca)
          k=1
          image=image[r1+k:r2-k,c1+k:c2-k]
     return image
def image_pad(image,offr,offc):
    rows=np.shape(image)[0]
    cols=np.shape(image)[1]
    size = rows+offr, cols+offc
    image_BIG = np.zeros(size, dtype=np.uint8)
    image_BIG[int(offr/2) :int(offr/2)+rows,int(offc/2) :int(offc/2) +cols]=image
    return image_BIG

def find_bb_scale(image,scale):
        image =  cv2.resize(image,None,fx=scale, fy=scale, interpolation = cv2.INTER_NEAREST)
        ra,ca=np.where(image>0)
        ra=ra/scale
        ca=ca/scale
        r1=int(min(ra))
        r2=int(max(ra))
        c1=int(min(ca))
        c2=int(max(ca))
        return r1,c1,r2,c2
########################################
def image_remove_pad_corner(image,corner_r,corner_c):
     if len(np.shape(image))==3:
          mask=image[:,:,0]>0
          ra,ca=np.where(mask>0)
          r1=min(ra)
          r2=max(ra)
          c1=min(ca)
          c2=max(ca)
          corner_r=corner_r-r1
          corner_c=corner_c-c1
          k=0
          image=image[r1+k:r2-k,c1+k:c2-k,:]
     else:
          #mask=image>0
          r1,c1,r2,c2=find_bb_scale(image,0.4)
          k=2
          r1=r1-k
          r2=r2+k
          c1=c1-k
          c2=c2+k
          #ra,ca=np.where(image>0)
          #k=2
          #r1=min(ra)-k
          #r2=max(ra)+k
          #c1=min(ca)-k
          #c2=max(ca)+k
          corner_r=corner_r-r1
          corner_c=corner_c-c1
          k=0
          image=image[r1:r2,c1:c2]
     return image,corner_r,corner_c

def image_pad_corner(image,offr,offc,corner_r,corner_c):
    rows=np.shape(image)[0]
    cols=np.shape(image)[1]
    size = rows+offr, cols+offc
    image_BIG = np.zeros(size, dtype=np.uint8)
    image_BIG[int(offr/2) :int(offr/2)+rows,int(offc/2) :int(offc/2) +cols]=image
    corner_r=corner_r+int(offr/2)
    corner_c=corner_c+int(offc/2)
    return image_BIG,corner_r,corner_c

def blend_corner(imageP,image_new,shift,corner_r,corner_c):
    imageP,corner_r_new,corner_c_new=image_pad_corner(imageP,500,500,corner_r,corner_c)
    rs=corner_r_new-shift[0]
    cs=corner_c_new-shift[1]
    if len(np.shape(imageP))==3:
        imageP[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1],:]=image_new
    else:
        imageP[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1]]=image_new

    start_time = time.time()

    imageP,corner_r_new,corner_c_new= image_remove_pad_corner(imageP,rs,cs)
    print("%s seconds ---" % (time.time() - start_time))
    #corner_r_new=rs
    #corner_c_new=cs
    return imageP,corner_r_new,corner_c_new

def blend_corner2(imageP,image_new,shift,corner_r,corner_c,black):

    rs=corner_r-shift[0]
    cs=corner_c-shift[1]
    #print (np.shape(image_new)[0])

    rs2=int(rs)+np.shape(image_new)[0]
    cs2=int(cs)+np.shape(image_new)[1]
    W=np.shape(imageP)[1]
    H=np.shape(imageP)[0]
    Hn=H
    Wn=W

    origin_r=0
    origin_c=0
    if rs<0:
        Hn=H+abs(rs)
        origin_r=abs(rs)
        rs=0

    if cs<0:
        Wn=W+abs(cs)
        origin_c=abs(cs)
        cs=0
    if rs2>H:
        Hn=rs2
    if cs2>W:
        Wn=cs2

    if len(np.shape(imageP))==3:
        imagePn=np.zeros((Hn,Wn,3))
        imagePn[origin_r:origin_r+H,origin_c:origin_c+W]=imageP
        imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1],:]=image_new
    else:
        imagePn=np.zeros((int(Hn),int(Wn)))
        imagePn[int(origin_r):int(origin_r+H),int(origin_c):int(origin_c+W)]=imageP
        if black==False:
            imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1]]=image_new

    return imagePn,int(rs),int(cs)

def check_overlap(imageP,image_new,shift,corner_r,corner_c):
    rs=corner_r-shift[0]
    cs=corner_c-shift[1]
    rs2=int(rs)+np.shape(image_new)[0]
    cs2=int(cs)+np.shape(image_new)[1]
    W=np.shape(imageP)[1]
    H=np.shape(imageP)[0]
    Hn=H
    Wn=W

    origin_r=0
    origin_c=0
    if rs<0:
        Hn=H+abs(rs)
        origin_r=abs(rs)
        rs=0

    if cs<0:
        Wn=W+abs(cs)
        origin_c=abs(cs)
        cs=0
    if rs2>H:
        Hn=rs2
    if cs2>W:
        Wn=cs2

    imagePn=np.zeros((int(Hn),int(Wn)))
    imagePn[int(origin_r):int(origin_r+H),int(origin_c):int(origin_c+W)]=imageP
    noverlap= np.sum(imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1]])
    overlap=noverlap/(np.shape(image_new)[0]*np.shape(image_new)[1])
    return overlap

def blend_corner3(imageP,image_new,shift,corner_r,corner_c,black):

    rs=corner_r-shift[0]
    cs=corner_c-shift[1]
    rs2=int(rs)+np.shape(image_new)[0]
    cs2=int(cs)+np.shape(image_new)[1]
    W=np.shape(imageP)[1]
    H=np.shape(imageP)[0]
    Hn=H
    Wn=W

    origin_r=0
    origin_c=0
    if rs<0:
        Hn=H+abs(rs)
        origin_r=abs(rs)
        rs=0

    if cs<0:
        Wn=W+abs(cs)
        origin_c=abs(cs)
        cs=0
    if rs2>H:
        Hn=rs2
    if cs2>W:
        Wn=cs2

    if len(np.shape(imageP))==3:
        imagePn=np.zeros((Hn,Wn,3))
        imagePn[origin_r:origin_r+H,origin_c:origin_c+W]=imageP
        imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1],:]=image_new
    else:

        imagePn=np.zeros((int(Hn),int(Wn)))
        imagePn[int(origin_r):int(origin_r+H),int(origin_c):int(origin_c+W)]=imageP
        noverlap= np.sum(imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1]])
        if black==False or noverlap==0:
            imagePn[int(rs):int(rs)+np.shape(image_new)[0], int(cs):int(cs)+np.shape(image_new)[1]]=image_new

    return imagePn,int(rs),int(cs)

def register_translation_scale(offset_image,image,scale):
    image =  cv2.resize(image,None,fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
    offset_image =  cv2.resize(offset_image,None,fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
    shift, error, diffphase = register_translation( offset_image,image)
    shift=shift/scale
    return shift, error, diffphase

if __name__ == "__main__":
    args = parse_args()
    video_src=args.video
    cam = video.create_capture(video_src)

    ret, frame = cam.read()
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ret, frame = cam.read()
    offset_image_color=frame
    offset_image =cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    width_cm=int(args.width)
    scale_reg=0.5
    ################################

    #print(np.shape(image)[0])
    #print(np.shape(image)[1])
    imageP=image
    imageP_bin=np.zeros((np.shape(image)[0],np.shape(image)[1]),'uint8')
    imageP_mask=np.ones((np.shape(image)[0],np.shape(image)[1]),'uint8')
    mask_frame=np.ones((np.shape(image)[0],np.shape(image)[1]),'uint8')
    corner_r=0
    corner_c=0
    ################################
    image=imageP[int(corner_r):int(corner_r)+np.shape(offset_image)[0],int(corner_c):int(corner_c)+np.shape(offset_image)[1] ]
    shift, error, diffphase = register_translation_scale( offset_image,image,scale_reg)
    corner_r_pre=corner_r
    corner_c_pre=corner_c
    imageP,corner_r,corner_c=blend_corner2(imageP,offset_image,shift,corner_r,corner_c,False)
    imageP_mask,tmp1,tmp2=blend_corner2(imageP_mask,mask_frame,shift,corner_r_pre,corner_c_pre,False)
    overlap=check_overlap(imageP_mask,mask_frame,shift,corner_r_pre,corner_c_pre)
    cv2.imwrite(args.frame,offset_image_color)
    #print(width_cm)
    if int(args.type)==2:
        os.system('/usr/local/bin/python3 scripts/video/crack_detection2_fast.py '+args.frame +' '+str(width_cm)+' '+args.output+' '+args.output_bin)
    else:
        os.system('/usr/local/bin/python3 scripts/video/crack_detection_fast.py '+args.frame)

    print (args.output_bin)
    crack=cv2.imread(args.output_bin,0)
    imageP_bin,tmp1,tmp2=blend_corner2(imageP_bin,crack,shift,corner_r_pre,corner_c_pre,False)

    #cv2.imwrite('imageP.png',imageP)
    #cv2.imwrite('imageP_bin.png',imageP_bin)
    #cv2.imwrite('imageP_mask.png',imageP_mask*255)
    ##############################
    step=1
    step_crackdetection=100#70
    th_overlap=0.3

    count_frame=1
    while ret==True:
        image=offset_image
        print('Frame : ' + str(count_frame))
        count_frame=count_frame+1
        ret, frame = cam.read()
        if ret==True:
            offset_image_color=frame
            offset_image =cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            shift, error, diffphase = register_translation_scale( offset_image,image,scale_reg)
            overlap=check_overlap(imageP_mask,mask_frame,shift,corner_r,corner_c)
            #print(overlap)
            if  overlap<th_overlap:
                cv2.imwrite(args.frame,offset_image_color)

                if int(args.type)==2:
                     os.system('/usr/local/bin/python3 scripts/video/crack_detection2_fast.py '+args.frame+' '+str(width_cm)+' '+args.output+' '+args.output_bin)
                else:
                    os.system('/usr/local/bin/python3 scripts/video/crack_detection_fast.py '+args.frame)

                crack=cv2.imread(args.output_bin,0)

                corner_r_pre=corner_r
                corner_c_pre=corner_c

                imageP,corner_r,corner_c=blend_corner2(imageP,offset_image,shift,corner_r,corner_c,False)
                imageP_bin,tmp1,tmp2=blend_corner2(imageP_bin,crack,shift,corner_r_pre,corner_c_pre,False)
                imageP_mask,tmp1,tmp2=blend_corner2(imageP_mask,mask_frame,shift,corner_r_pre,corner_c_pre,False)
                #cv2.imwrite('imageP.png',imageP)#debug
                #cv2.imwrite('imageP_bin.png',imageP_bin)#debug
                #cv2.imwrite('imageP_mask.png',imageP_mask*255)#debug
            else:
                corner_r_pre=corner_r
                corner_c_pre=corner_c
                imageP,corner_r,corner_c=blend_corner2(imageP,offset_image,shift,corner_r,corner_c,True)
                imageP_bin,tmp1,tmp2=blend_corner2(imageP_bin,offset_image,shift,corner_r_pre,corner_c_pre,True)
                imageP_mask,tmp1,tmp2=blend_corner2(imageP_mask,mask_frame,shift,corner_r_pre,corner_c_pre,True)

    ##############################
    cv2.imwrite(args.frame,offset_image_color)

    if int(args.type)==2:
         os.system('/usr/local/bin/python3 scripts/video/crack_detection2_fast.py '+args.frame+' '+str(width_cm)+' '+args.output+' '+args.output_bin)
    else:
         os.system('/usr/local/bin/python3 scripts/video/crack_detection_fast.py '+args.frame)

    crack=cv2.imread(args.output_bin,0)

    corner_r_pre=corner_r
    corner_c_pre=corner_c
    imageP,corner_r,corner_c=blend_corner2(imageP,offset_image,shift,corner_r,corner_c,False)
    imageP_bin,tmp1,tmp2=blend_corner2(imageP_bin,crack,shift,corner_r_pre,corner_c_pre,False)
    imageP_mask,tmp1,tmp2=blend_corner2(imageP_mask,mask_frame,shift,corner_r_pre,corner_c_pre,False)

    #############################

    #############################
    cv2.imwrite(args.output_bin,imageP_bin*255)

    img_out=np.zeros((np.shape(imageP)[0],np.shape(imageP)[1],3),'uint8')
    img_out[:,:,0]=imageP
    img_out[:,:,1]=imageP
    img_out[:,:,2]=imageP

    imgr=img_out[:,:,2];
    imgr[imageP_bin>0]=255;
    img_out[:,:,2]=imgr;
    cv2.imwrite(args.output_stiching,imageP)
    cv2.imwrite(args.output,img_out)
    print('done ')

max_stars_repo_name: ficusoftdeveloper/bluedome

max_issues_repo_name: ficusoftdeveloper/bluedome

max_forks_repo_name: ficusoftdeveloper/bluedome

reason: import numpy,from scipy


text:
#-------------------------------------------------------------------------------------------------------------------
# Packages & Settings
#-------------------------------------------------------------------------------------------------------------------

# General packages
import time
import sys
import os
import datetime
from glob import glob

# Math and data structure packages
import numpy as np
from scipy import stats
import math
import random

# Plots, Fits, etc.
import matplotlib
import matplotlib.pyplot as plt

# Writing Output
import pickle

text_folder = '/home/rettenls/data/texts/wiki/'
eval_folder = '/home/rettenls/data/evaluation/analogy/'
exp_folder = '/home/rettenls/data/experiments/wiki/'
dist_folder = '/home/rettenls/data/experiments/wiki/analysis/distribution/'

coordination_file = exp_folder + 'coordination/coordinate.txt'

date_format = '%Y-%m-%d_%H:%M:%S'

#-------------------------------------------------------------------------------------------------------------------
# Loading own Modules
#-------------------------------------------------------------------------------------------------------------------

sys.path.append('/home/rettenls/code')
from lib.model 			import Model
from lib.trafo 			import Transformation
from lib.eval 			import print_nn_word, get_nn_list, get_cosine_similarity, get_pip_norm, get_word_relatedness
from lib.score 			import evaluate_analogy
from lib.operations 	import align, avg, join, align_list, get_common_vocab
from lib.util			import get_filename

#-------------------------------------------------------------------------------------------------------------------
# Experiments
#-------------------------------------------------------------------------------------------------------------------

language = 'en'
model = 'word2vec'
model_type = 'skipgram'
data_type = 'shuffle'

max_run_num = 2

if model == 'glove':
		folder = exp_folder + language + '/' + model + '/' + data_type
else:
	folder = exp_folder + language + '/' + model + '/' + model_type + '/' + data_type

for run in range(max_run_num):

	m1 = Model(model)
	m1.load(folder + '/merge_nnz_0032_run_{:04d}'.format(run))
	m1.normalize()
	m1 = m1.reduce(200000)

	print_nn_word(m1, "momentum", 30)

max_stars_repo_path: thesis/8 - Averaging/7 - Most Similar Words.py


text:
# Implementation of a rotating buffer on the GPU of size 2.
import threading
import tensorflow as tf
from tensorflow.python.client import timeline
import numpy as np
import time

params = {
    'batch_size': 128,
    'seg_len': 4000,
}

kernel_shape = [64, 8, 100]

graph = tf.Graph()
sess = tf.Session(config=tf.ConfigProto(log_device_placement=False), graph=graph)
verbose = False

target_dev = '/gpu:0'

with graph.as_default(), tf.device('/cpu:0'):

    # Variables
    with tf.device(target_dev):
        var_buffer_out = tf.Variable([[0]], validate_shape=False, dtype=tf.float32, trainable=False)
        var_buffer_in = tf.Variable([0], validate_shape=False, dtype=tf.float32, trainable=False)

    data_in = tf.placeholder(dtype=tf.float32, shape=[None, params['seg_len'], kernel_shape[1]])

    queue = tf.FIFOQueue(shapes=[[params['seg_len'], kernel_shape[1]]],
        dtypes=[tf.float32],
        capacity=2*params['batch_size'],
    )

    enqueue_op = queue.enqueue_many([data_in])
    dequeued_data_in = queue.dequeue_many(params['batch_size'])

    with tf.device(target_dev):
        move_buffer = tf.assign(var_buffer_out, var_buffer_in, validate_shape=False)

        put_in_buffer = tf.assign(var_buffer_in, dequeued_data_in, validate_shape=False)

        get_from_buffer = tf.identity(var_buffer_out)

        with tf.variable_scope("conv1"):
            Wz = tf.get_variable("Wz", initializer=np.zeros(kernel_shape, dtype=np.float32)+0.00001)
            Z = tf.nn.conv1d(get_from_buffer, Wz, 1, 'SAME')
            test_out = (tf.reduce_mean(Z) * 0) + tf.reduce_mean(get_from_buffer)
            if verbose:
                with tf.device('/cpu:0'):
                    test_out = tf.Print(test_out, [put_in_buffer, move_buffer, test_out], '[BUF_IN][BUF_OUT][RESULT]:')

    with tf.control_dependencies([move_buffer]):
        pull = tf.group(put_in_buffer, test_out)

    def runThread(sess, coord):
        i=0
        while not coord.should_stop():
            sess.run(enqueue_op, feed_dict={
                data_in: np.zeros([params['batch_size'], params['seg_len'], kernel_shape[1]], dtype=np.float32)+i
            })
            i += 1

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    threads = []
    for n in range(1):
        t = threading.Thread(target=runThread, args=(sess, coord))
        t.daemon = True
        t.start()
        threads.append(t)

    # Put something in the input-buffer on the GPU.
    sess.run([put_in_buffer])
    # Move to the output-side of the buffer on the GPU.
    sess.run([move_buffer])

    # Now pull th
    for i in range(20):
        sess.run([pull])

    print('Running a trace..')
    time.sleep(5)  # Give the queue fetcher some time
    run_metadata = tf.RunMetadata()
    sess.run([pull],
        options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE),
        run_metadata=run_metadata
    )
    trace = timeline.Timeline(step_stats=run_metadata.step_stats)
    trace_file = open('timeline.ctf.json', 'w')
    trace_file.write(trace.generate_chrome_trace_format())
    exit() # Trace seems clog thing up?
    # Wrap up
    coord.request_stop()
    coord.join(threads)
    sess.close()

max_stars_repo_name: jjhenkel/dockerizeme

max_issues_repo_name: jjhenkel/dockerizeme

max_forks_repo_name: jjhenkel/dockerizeme


text:
import torch
from rkn_cell.kalman_ops.update import Update
from util.ConfigDict import ConfigDict
from rkn_cell.kalman_ops.ac_predict import AcPredict
nn = torch.nn

class AcRKNLayerInv(nn.Module):
    @staticmethod
    def get_default_config() -> ConfigDict:
        config = ConfigDict(
            num_basis=15,
            bandwidth=3,
            trans_net_hidden_units=[],
            control_net_hidden_units=[60],
            trans_net_hidden_activation="Tanh",
            control_net_hidden_activation='ReLU',
            learn_trans_covar=True,
            trans_covar=1,
            learn_initial_state_covar=True,
            initial_state_covar=1,
            learning_rate=7e-3,
            enc_out_norm='post',
            clip_gradients=True,
            never_invalid=True
        )
        config.finalize_adding()
        return config

    def __init__(self, latent_obs_dim: int, act_dim: int, cell_config: ConfigDict = None, dtype=torch.float32):
        super().__init__()
        if cell_config == None:
            self.c = self.get_default_config()
        else:
            self.c = cell_config
        self._lad = act_dim
        self._lod = latent_obs_dim
        self._lsd = 2 * latent_obs_dim
        self._update = Update(self._lod, self.c)
        self._predict = AcPredict(self._lod, self._lad, self.c)

    def forward(self, latent_obs, obs_vars, action, initial_mean, initial_cov, obs_valid=None):
        """
        This currently only returns the posteriors. If you also need the priors uncomment the corresponding parts

        :param latent_obs: latent observations
        :param obs_vars: uncertainty estimate in latent observations
        :param action: control signals
        :param initial_mean: mean of initial belief
        :param initial_cov: covariance of initial belief (as 3 vectors)
        :param obs_valid: flags indicating which observations are valid, which are not
        """

        # tif you need a version that also returns the prior uncomment the respective parts below
        # prepare list for return

        prior_mean_list = []
        prior_cov_list = [[], [], []]

        post_mean_list = []
        post_cov_list = [[], [], []]

        # initialize prior
        prior_mean, prior_cov = initial_mean, initial_cov

        # actual computation
        for i in range(latent_obs.shape[1]):

            cur_obs_valid = obs_valid[:, i] if obs_valid is not None else None

            # update belief with updateLayer
            post_mean, post_cov = \
                self._update(prior_mean, prior_cov, latent_obs[:, i], obs_vars[:, i], cur_obs_valid)

            post_mean_list.append(post_mean)
            [post_cov_list[i].append(post_cov[i]) for i in range(3)]
            import numpy as np

            # predict next belief state ahead in time
            next_prior_mean, next_prior_cov = self._predict(post_mean, post_cov, action[:, i])

            prior_mean_list.append(next_prior_mean)
            [prior_cov_list[i].append(next_prior_cov[i]) for i in range(3)]

            prior_mean = next_prior_mean
            prior_cov = next_prior_cov

        # stack results
        prior_means = torch.stack(prior_mean_list, 1)
        prior_covs = [torch.stack(x, 1) for x in prior_cov_list]

        post_means = torch.stack(post_mean_list, 1)
        post_covs = [torch.stack(x, 1) for x in post_cov_list]

        return post_means, post_covs, prior_means, prior_covs


text:
# ---
# jupyter:
#   jupytext:
#     metadata_filter:
#       cells:
#         additional: all
#       notebook:
#         additional: all
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.2'
#       jupytext_version: 0.8.6
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
#   language_info:
#     codemirror_mode:
#       name: ipython
#       version: 3
#     file_extension: .py
#     mimetype: text/x-python
#     name: python
#     nbconvert_exporter: python
#     pygments_lexer: ipython3
#     version: 3.7.2
# ---

# %% [markdown]
# This is a `bqplot` recreation of Mike Bostock's [Wealth of Nations](https://bost.ocks.org/mike/nations/). This was also done by [Gapminder](http://www.gapminder.org/world/#$majorMode=chart$is;shi=t;ly=2003;lb=f;il=t;fs=11;al=30;stl=t;st=t;nsl=t;se=t$wst;tts=C$ts;sp=5.59290322580644;ti=2013$zpv;v=0$inc_x;mmid=XCOORDS;iid=phAwcNAVuyj1jiMAkmq1iMg;by=ind$inc_y;mmid=YCOORDS;iid=phAwcNAVuyj2tPLxKvvnNPA;by=ind$inc_s;uniValue=8.21;iid=phAwcNAVuyj0XOoBL_n5tAQ;by=ind$inc_c;uniValue=255;gid=CATID0;by=grp$map_x;scale=log;dataMin=194;dataMax=96846$map_y;scale=lin;dataMin=23;dataMax=86$map_s;sma=49;smi=2.65$cd;bd=0$inds=;modified=60). It is originally based on a TED Talk by [Hans Rosling](http://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen).

# %% {"collapsed": true}
# %reset -f
# %matplotlib inline
# %config InlineBackend.figure_format = 'retina' # High-res graphs
# %config InlineBackend.print_figure_kwargs = {'bbox_inches':'tight'} # No extra white space
# %config InlineBackend.figure_format = 'svg' # 'png' is default

import pandas as pd
import numpy as np
import os

from bqplot import (
    LogScale, LinearScale, OrdinalColorScale, ColorAxis,
    Axis, Scatter, Lines, CATEGORY10, Label, Figure, Tooltip
)

from ipywidgets import HBox, VBox, IntSlider, Play, jslink

# %% {"collapsed": true}
initial_year = 1800

# %% [markdown]
# #### Cleaning and Formatting JSON Data

# %% {"collapsed": true, "scrolled": false}
data = pd.read_json(os.path.abspath("data/nations.json"))

# %% {"collapsed": true}
def clean_data(data):
    for column in ['income', 'lifeExpectancy', 'population']:
        data = data.drop(data[data[column].apply(len) <= 4].index)
    return data

def extrap_interp(data):
    data = np.array(data)
    x_range = np.arange(1800, 2009, 1.)
    y_range = np.interp(x_range, data[:, 0], data[:, 1])
    return y_range

def extrap_data(data):
    for column in ['income', 'lifeExpectancy', 'population']:
        data[column] = data[column].apply(extrap_interp)
    return data

# %% {"collapsed": true}
data = clean_data(data)
data = extrap_data(data)

# %% {"collapsed": true, "scrolled": true}
income_min, income_max = np.min(data['income'].apply(np.min)), np.max(data['income'].apply(np.max))
life_exp_min, life_exp_max = np.min(data['lifeExpectancy'].apply(np.min)), np.max(data['lifeExpectancy'].apply(np.max))
pop_min, pop_max = np.min(data['population'].apply(np.min)), np.max(data['population'].apply(np.max))

# %% {"collapsed": true}
def get_data(year):
    year_index = year - 1800
    income = data['income'].apply(lambda x: x[year_index])
    life_exp = data['lifeExpectancy'].apply(lambda x: x[year_index])
    pop =  data['population'].apply(lambda x: x[year_index])
    return income, life_exp, pop

# %% [markdown]
# #### Creating the Tooltip to display the required fields
#
# `bqplot`'s native `Tooltip` allows us to simply display the data fields we require on a mouse-interaction.

# %% {"collapsed": true}
tt = Tooltip(fields=['name', 'x', 'y'], labels=['Country Name', 'Income per Capita', 'Life Expectancy'])

# %% [markdown]
# #### Creating the Label to display the year
#
# Staying true to the `d3` recreation of the talk, we place a `Label` widget in the bottom-right of the `Figure` (it inherits the `Figure` co-ordinates when no scale is passed to it). With `enable_move` set to `True`, the `Label` can be dragged around.

# %% {"collapsed": true}
year_label = Label(x=[0.75], y=[0.10], default_size=46, font_weight='bolder', colors=['orange'],
                   text=[str(initial_year)], enable_move=True)

# %% [markdown]
# #### Defining Axes and Scales
#
# The inherent skewness of the income data favors the use of a `LogScale`. Also, since the color coding by regions does not follow an ordering, we use the `OrdinalColorScale`.

# %% {"collapsed": true}
x_sc = LogScale(min=income_min, max=income_max)
y_sc = LinearScale(min=life_exp_min, max=life_exp_max)
c_sc = OrdinalColorScale(domain=data['region'].unique().tolist(), colors=CATEGORY10[:6])
size_sc = LinearScale(min=pop_min, max=pop_max)

# %% {"collapsed": true}
ax_y = Axis(label='Life Expectancy', scale=y_sc, orientation='vertical', side='left', grid_lines='solid')
ax_x = Axis(label='Income per Capita', scale=x_sc, grid_lines='solid')

# %% [markdown]
# #### Creating the Scatter Mark with the appropriate size and color parameters passed
#
# To generate the appropriate graph, we need to pass the population of the country to the `size` attribute and its region to the `color` attribute.

# %% {"collapsed": true}
# Start with the first year's data
cap_income, life_exp, pop = get_data(initial_year)

# %% {"collapsed": true}
wealth_scat = Scatter(x=cap_income, y=life_exp, color=data['region'], size=pop,
                      names=data['name'], display_names=False,
                      scales={'x': x_sc, 'y': y_sc, 'color': c_sc, 'size': size_sc},
                      default_size=4112, tooltip=tt, animate=True, stroke='Black',
                      unhovered_style={'opacity': 0.5})

# %% {"collapsed": true}
nation_line = Lines(x=data['income'][0], y=data['lifeExpectancy'][0], colors=['Gray'],
                       scales={'x': x_sc, 'y': y_sc}, visible=False)

# %% [markdown]
# #### Creating the Figure

# %% {"collapsed": true}
time_interval = 10

# %% {"collapsed": true}
fig = Figure(marks=[wealth_scat, year_label, nation_line], axes=[ax_x, ax_y],
             title='Health and Wealth of Nations', animation_duration=time_interval)

# %% [markdown]
# #### Using a Slider to allow the user to change the year and a button for animation
#
# Here we see how we can seamlessly integrate `bqplot` into the jupyter widget infrastructure.

# %% {"collapsed": true}
year_slider = IntSlider(min=1800, max=2008, step=1, description='Year', value=initial_year)

# %% [markdown]
# When the `hovered_point` of the `Scatter` plot is changed (i.e. when the user hovers over a different element), the entire path of that country is displayed by making the `Lines` object visible and setting it's `x` and `y` attributes.

# %% {"collapsed": true}
def hover_changed(change):
    if change.new is not None:
        nation_line.x = data[data['name'] == wealth_scat.names[change.new]]['income'].values[0]
        nation_line.y = data[data['name'] == wealth_scat.names[change.new]]['lifeExpectancy'].values[0]
        nation_line.visible = True
    else:
        nation_line.visible = False

wealth_scat.observe(hover_changed, 'hovered_point')

# %% [markdown]
# On the slider value `callback` (a function that is triggered everytime the `value` of the slider is changed) we change the `x`, `y` and `size` co-ordinates of the `Scatter`. We also update the `text` of the `Label` to reflect the current year.

# %% {"collapsed": true}
def year_changed(change):
    wealth_scat.x, wealth_scat.y, wealth_scat.size = get_data(year_slider.value)
    year_label.text = [str(year_slider.value)]

year_slider.observe(year_changed, 'value')

# %% [markdown]
# #### Add an animation button

# %% {"collapsed": true}
play_button = Play(min=1800, max=2008, interval=time_interval)
jslink((play_button, 'value'), (year_slider, 'value'))

# %% [markdown]
# #### Displaying the GUI

# %% {"scrolled": false}
VBox([HBox([play_button, year_slider]), fig])