#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun  3 13:45:43 2021
By: Guido Meijer
"""

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn.objects as so
from scipy.stats import pearsonr
from os.path import join
from serotonin_functions import paths, figure_style, load_subjects, combine_regions

# Settings
MIN_NEURONS = 10

# Paths
fig_path, save_path = paths(dropbox=True)
fig_path = join(fig_path, 'PaperPassive', 'figure3')

# Load in results
light_neurons = pd.read_csv(join(save_path, 'light_modulated_neurons.csv'), index_col=0)
neuron_type = pd.read_csv(join(save_path, 'neuron_type.csv'))
neuron_type = neuron_type[neuron_type['type'] != 'Und.']
neuron_type['neuron_id'] = neuron_type['cluster_id']
merged_df = pd.merge(light_neurons, neuron_type, on=['neuron_id', 'pid', 'eid', 'probe'])
merged_df['full_region'] = combine_regions(merged_df['region'], abbreviate=True)

# Add expression
subjects = load_subjects()
for i, nickname in enumerate(np.unique(subjects['subject'])):
    merged_df.loc[merged_df['subject'] == nickname, 'sert-cre'] = subjects.loc[subjects['subject'] == nickname, 'sert-cre'].values[0]

# Get percentage modulated per region
per_region_df = merged_df.groupby('full_region').mean(numeric_only=True)['mod_index_late'].to_frame()
per_region_df['n_neurons'] = merged_df[merged_df['type'] == 'NS'].groupby(['full_region']).size()
per_region_df['perc_mod_NS'] = (merged_df[merged_df['type'] == 'NS'].groupby(['full_region']).sum()['modulated']
                                / merged_df[merged_df['type'] == 'NS'].groupby(['full_region']).size()) * 100
per_region_df = per_region_df.reset_index()
per_region_df = per_region_df[per_region_df['full_region'] != 'root']
per_region_df = per_region_df[per_region_df['full_region'] != 'ZI']
per_region_df = per_region_df[per_region_df['n_neurons'] >= MIN_NEURONS]

# %%
colors, dpi = figure_style()
f, ax1 = plt.subplots(1, 1, figsize=(1.75, 1.75), dpi=dpi)
per_region_df['color'] = [colors[i] for i in per_region_df['full_region']]

(
     so.Plot(per_region_df, x='mod_index_late', y='perc_mod_NS')
     .add(so.Dot(pointsize=0))
     .add(so.Line(color='grey', linewidth=1), so.PolyFit(order=1))
     .on(ax1)
     .plot()
)
for i in per_region_df.index:
    ax1.text(per_region_df.loc[i, 'mod_index_late'] ,
             per_region_df.loc[i, 'perc_mod_NS'],
             per_region_df.loc[i, 'full_region'],
             ha='center', va='center',
             color=per_region_df.loc[i, 'color'], fontsize=4.5, fontweight='bold')
ax1.set(yticks=[0, 25, 50], xticks=[-0.06, -0.03, 0, 0.03],
        ylabel='Modulated inhibitory neurons (%)', xlabel='Modulation index')
r, p = pearsonr(per_region_df['mod_index_late'], per_region_df['perc_mod_NS'])
#ax1.text(0.1, 100, f'r = {r:.2f}', fontsize=6)
#ax1.text(-0.1, 520, '***', fontsize=10, ha='center')

sns.despine(offset=2, trim=True)
plt.tight_layout()
plt.savefig(join(fig_path, 'modulation_vs_perc_mod_NS.pdf'))


class _DoublyLinkedBase:

    ''' maintains header and trailer nodes which will always be empty
        This is important to make removing and adding new elements on respectively size of one element and zero size easier, without any if conditions
        Any element that is added or removed will be having its previous and next because of these special empty nodes we construct
    '''

    class _Node:
        __slots__='_element', '_prev', '_next' #streamline memory

        def __init__(self,element,prev,next):
            self._element = element
            self._prev = prev
            self._next = next

    def __init__(self):
        self._size = 0
        self._header = self._Node(None,None,None)
        self._trailer = self._Node(None,None,None)
        self._header._next = self._trailer
        self._trailer._prev = self._header

    def __len__(self):
        return self._size

    def is_empty(self):
        return self._size == 0

    def _insert_between(self, e, predecessor, successor):
        newest = self._Node(e,predecessor, successor)
        predecessor._next = newest
        successor._prev = newest
        self._size+=1
        return newest

    def _delete_node(self, node):
        predecessor = node._prev
        successor = node._next
        predecessor._next = successor
        successor._prev = predecessor
        self._size-=1
        element = node._element
        node._prev = node._next = node._element = None # help gc and mark as deprecated
        return element

    #C-7.33
    def _reverse(self):
        walk = self._trailer._prev
        while walk != self._header:
            nxt = walk._next
            prev = walk._prev
            walk._next = prev
            walk._prev = nxt
            walk = prev
        self._trailer._next = self._trailer._prev
        self._trailer._prev = None
        self._header._prev = self._header._next
        self._header._next = None
        old_head = self._header
        self._header = self._trailer
        self._trailer = old_head

    def _swap(self,n1,n2):
        n1._next = n2._next
        n2._next._prev = n2._prev
        n2._prev = n1._prev
        n1._prev._next = n2
        n1._prev = n2
        n2._next = n1

class LinkedDeque(_DoublyLinkedBase):

    def first(self):
        if self.is_empty():
            raise ValueError('empty')
        return self._header._next._element

    def last(self):
        if self.is_empty():
            raise ValueError('empty')
        return self._trailer._prev._element

    def insert_first(self, e):
        self._insert_between(e, self._header, self._header._next)

    def insert_last(self, e):
        self._insert_between(e, self._trailer._prev, self._trailer)

    def delete_first(self):
        if self.is_empty():
            raise ValueError('empty')
        return self._delete_node(self._header._next)

    def delete_last(self):
        if self.is_empty():
            raise ValueError('empty')
        return self._delete_node(self._trailer._prev)

    def reverse(self):
        self._reverse()

class PositionalList(_DoublyLinkedBase):

    class Position:
        ''' an abstraction representing the location of single element '''
        def __init__(self, container, node):
            self._container = container # container is reference to the structure which node belongs to
            self._node = node

        def element(self):
            ''' returns element under this position '''
            return self._node._element

        def __eq__(self, other):
            ''' equals method '''
            return type(other) is type(self) and other._node is self._node

        def __ne__(self, other):
            ''' not equals '''
            return not (self == other)

    def _validate(self,p):
        ''' private function to unwrap position and determine if argument is position which references valid node from the positional list object'''
        if not isinstance(p, self.Position):
            raise TypeError('p must be proper Position type')
        if p._container is not self:
            raise ValueError('p does not belong to this container')
        if p._node._next is None and p._node._prev is None: # convetion for deprecated nodes
            raise ValueError('p is no longer valid')
        return p._node

    def _make_position(self, node):
        ''' private function to wrap node into position if the node is not sentinel node ( header or trailer )'''
        if node is self._header or node is self._trailer:
            return None
        return self.Position(self, node)

    def first(self):
        ''' inspects first element
            returns position
        '''
        return self._make_position(self._header._next)

    def last(self):
        ''' inspects last element
            returns position
        '''
        return self._make_position(self._trailer._prev)

    def before(self, p):
        ''' returns position before argument position
            If argument position is first then returns None
        '''
        node = self._validate(p)
        return self._make_position(node._prev)

    def after(self,p):
        ''' returns position after argument position
            If arg pos is last then returns None
        '''
        node = self._validate(p)
        return self._make_position(node._next)

    def __iter__(self):
        ''' iterator '''
        cursor = self.first()
        while cursor is not None:
            yield cursor.element()
            cursor = self.after(cursor)

    #R-7.15
    def __reversed__(self):
        ''' reversed iterator '''
        cursor = self.last()
        while cursor is not None:
            yield cursor.element()
            cursor = self.before(cursor)

    def _insert_between(self, e, predecessor, successor):
        ''' overriden function. Instead of node returns position '''
        node = super()._insert_between(e, predecessor, successor)
        return self._make_position(node)

    def add_first(self, e):
        ''' insert element at the front of the list and return position '''
        return self._insert_between(e, self._header, self._header._next)

    def add_last(self, e):
        ''' insert element at the end of the list and return position '''
        return self._insert_between(e, self._trailer._prev, self._trailer)

    def add_before(self, e, p):
        ''' inserts element before arg position and returns position of the element'''
        node = self._validate(p)
        return self._insert_between(e, node._prev, node)

    def add_after(self, e, p):
        ''' inserts element after arg position and returns position of the element '''
        node = self._validate(p)
        return self._insert_between(e, node, node._next)

    def delete(self, p):
        ''' deletes element at the given position in the list '''
        node = self._validate(p)
        return self._delete_node(node)

    def replace(self, p, e):
        ''' Replace element at position p with element e

            Returns the element formerly at position p
        '''
        node = self._validate(p)
        old_value = node._element
        node._element = e
        return old_value

    #C-7.34
    def swap(self,p,q):
        ''' swaps underlying nodes on position p and position q '''
        n1 = self._validate(p)
        n2 = self._validate(q)
        self._swap(n1, n2)

    def __str__(self):
        return ''.join(self)

class FavoritesList:
    ''' List of elements ordered from most frequently accessed to least '''

    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self,e):
            self._value = e
            self._count = 0

    def __init__(self):
        self._data = PositionalList()

    def __len__(self):
        return len(self._data)

    def is_empty(self):
        return len(self._data) == 0

    def _find_position(self, e):
        ''' search for element and return its position or None if not found '''
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        ''' Move item at Position p earlier in the list based on access count '''
        if p != self._data.first():
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count:
                while (walk != self._data.first() and
                       cnt > self._data.before(walk).element()._count):
                    walk = self._data.before(walk)
                item = self._data.delete(p)
                self._data.add_before(item, walk)
                #self._data.add_before(walk, self._data.delete(p))

    def access(self, e):
        p = self._find_position(e)
        if p is None:
            p = self._data.add_last(self._Item(e))
        p.element()._count+=1
        self._move_up(p)

    def remove(self, e):
        p = self._find_position(e)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        if not 1 <= k <= len(self):
            raise ValueError('illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)

class FavoritestListMTF(FavoritesList):
    ''' list of elements ordered with move-to-front heuristic '''

    def _move_up(self, p): #override move up to implement move to front instead of shifting to proper index based on cnt
        if p != self._data.first():
            self._data.add_first(self._data.delete(p))

    def top(self, k): # override top because list is no more sorted
        if not 1 <= k and k <= len(self):
            raise ValueError('illegal value for k')

        temp = PositionalList()
        for item in self._data:
            temp.add_last(item)

        for j in range(k):
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)
            item = temp.delete(highPos)
            yield item._value

class PositionalListUtils:

    @staticmethod
    def insertion_sort(L):
        if len(L) > 1:
            marker = L.first()
            while marker != L.last():
                pivot = L.after(marker)
                value = pivot.element()
                if value > marker.element():
                    marker = pivot
                else:
                    walk = marker
                    while walk != L.first() and L.before(walk).element() > value:
                        walk = L.before(walk)
                    L.delete(pivot)
                    L.add_before(walk, value)

    #R-7.11, R-7.14
    @staticmethod
    def max(L, recursive=False):
        def _max_recursive(current_ans, walk):
            if walk is None:
                return current_ans
            if walk.element() > current_ans:
                current_ans = walk.element()
            walk = L.after(walk)
            return _max_recursive(current_ans, walk)

        if recursive:
            return _max_recursive(L.first().element(), L.after(L.first()))
        else:
            ans = L.first().element()
            for e in L:
                if e > ans:
                    ans = e
            return ans

if __name__ == '__main__':
    fl = FavoritesList()
    fl.access('matija')
    fl.access('igor')
    fl.access('mix')

    fl.access('matija')
    fl.access('mix')

    for e in fl.top(2):
        print(e)

    pl = PositionalList()
    pl.add_first(12)
    pl.add_last(3)
    pl.add_last(4)
    pl.add_last(10)

    first = pl.first()
    second = pl.after(first)
    print(first.element())
    print(second.element())
    pl.swap(first, second)

    first = pl.first()
    second = pl.after(first)
    print(first.element())
    print(second.element())
    #print(PositionalListUtils.max(pl, recursive=True))

    deque = LinkedDeque()
    for i in range(4):
        deque.insert_last(i+1)

    deque.reverse()
    print(deque)


import scrapy
from scrapy.linkextractors import LinkExtractor

class getDrugsInfoSpider(scrapy.Spider):
    name = 'getDrugsInfo'

    start_urls = ['https://medlineplus.gov/spanish/druginfo/drug_Aa.html']

    # def parse(self, response):
    #    extactor = LinkExtractor(allow_domains = 'medlineplus.gov')
    #    links = extactor.extract_links(response)
    #    for link in links:
    #        print(link.url)

    def parse(self, response):
        iniciales = response.xpath('//ul[contains(@class, "alpha-links")]/li')

        pagination = []
        for letra in iniciales[1:]:
            pagination.append(f'{"https://medlineplus.gov/spanish/druginfo/"}{letra.xpath("./a").attrib["href"]}')

        yield {

            'link': pagination
        }


class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class SinglyLinkedList: #chain of nodes
    def __init__(self):
        self.head = None

    def traverse(self):
        if self.head is None:
            print('No list')
        else:
            value = self.head
            while value is not None:
                print(value.data)
                value = value.next

v1 = SinglyLinkedList()
v1.head= Node('eggs')
v2 = Node('ham')
v3 = Node('burger')

v1.head.next = v2
v2.next = v3
v1.traverse()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.admin.widgets import AdminFileWidget
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.conf import settings
from PIL import Image
import os

try:
    from easy_thumbnails.files import get_thumbnailer
    def thumbnail(image_path):
        thumbnailer = get_thumbnailer(image_path)
        thumbnail_options = {'crop': True, 'size': (120, 120), 'detail': True, 'upscale':True }
        t=thumbnailer.get_thumbnail(thumbnail_options)
        media_url = settings.MEDIA_URL
        return u'' % (media_url, t, image_path)
except ImportError:
    def thumbnail(image_path):
        absolute_url = os.path.join(settings.MEDIA_ROOT, image_path)
        return u'' % (absolute_url, image_path)

class AdminImageWidget(AdminFileWidget):
    """
    A FileField Widget that displays an image instead of a file path
    if the current file is an image.
    """
    def render(self, name, value, attrs=None):
        output = []
        file_name = str(value)
        if file_name:
            file_path = '%s%s' % (settings.MEDIA_URL, file_name)
            try:            # is image
                Image.open(os.path.join(settings.MEDIA_ROOT, file_name))
                output.append('%s
%s ' % \
                    (file_path, thumbnail(file_name), _('Change:')))
            except IOError: # not image
                output.append('%s %s
%s ' % \
                    (_('Currently:'), file_path, file_name, _('Change:')))

        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))


import math

def ell2utm(lat, lon, a, e2, lcm):

#% ELL2UTM  Converts ellipsoidal coordinates to UTM.
#%   UTM northing and easting coordinates in a 6 degree
#%   system.  Zones begin with zone 1 at longitude 180E
#%   to 186E and increase eastward.  Formulae from E.J.
#%   Krakiwsky, "Conformal Map Projections in Geodesy",
#%   Dept. Surveying Engineering Lecture Notes No. 37,
#%   University of New Brunswick, Fredericton, N.B.
#%   Vectorized.
#% Version: 2011-02-19
#% Useage:  [N,E,Zone]=ell2utm(lat,lon,a,e2,lcm)
#%          [N,E,Zone]=ell2utm(lat,lon,a,e2)
#%          [N,E,Zone]=ell2utm(lat,lon,lcm)
#%          [N,E,Zone]=ell2utm(lat,lon)
#% Input:   lat - vector of latitudes (rad)
#%          lon - vector of longitudes (rad)
#%          a   - ref. ellipsoid major semi-axis (m) default GRS80
#%          e2  - ref. ellipsoid eccentricity squared default GRS80
#%          lcm - central meridian default = standard UTM def'n
#% Output:  N   - vector of UTM northings (m)
#%          E   - vector of UTM eastings (m)
#%          Zone- vector of UTM zones
#
#% Copyright (c) 2011, Michael R. Craymer
#% All rights reserved.
#% Email: mike@craymer.com

    Zone = 0

    #Zone=floor((rad2deg(lon)-180)/6)+1
    #Zone=Zone+(Zone<0)*60-(Zone>60)*60
    #lcm=deg2rad(Zone*6-183)

    ko = 0.9996         # Scale factor
    No = 0              # False northing (north)
    if lat < 0:
        No = 1e7        # False northing (south)
    Eo = 500000         # False easting

    lam = lon-lcm
    lam = lam-(lam >= math.pi)*(2*math.pi)

    #print('\nZones\n')
    #print('%3d\n',Zone')
    #print('\nCentral Meridians\n')
    #print('%3d %2d %9.6f\n',rad2dms(lcm)')
    #print('\nLongitudes wrt Central Meridian\n')
    #print('%3d %2d %9.6f\n',rad2dms(lam)')

    f = 1-math.sqrt(1-e2)
    RN = a/(1-e2*math.sin(lat)**2)**0.5
    #RM = a*(1-e2)/(1-e2*math.sin(lat)**2)**1.5
    t = math.tan(lat)
    h = math.sqrt(e2*math.cos(lat)**2/(1-e2))
    n = f/(2-f)

    a0 = 1+n^2/4+n^4/64
    a2 = 1.5*(n-n^3/8)
    a4 = 15/16*(n^2-n^4/4)
    a6 = 35/48*n^3
    a8 = 315/512*n^4

    s = a/(1+n)*(a0*lat-a2*math.sin(2*lat)+a4*math.sin(4*lat)-a6*math.sin(6*lat)+a8*math.sin(8*lat))

    E1 = lam*math.cos(lat)
    E2 = lam**3*math.cos(lat)**3/6*(1-t**2+h**2)
    E3 = lam**5*math.cos(lat)**5/120*(5-18*t**2+t**4+14*h**2-58*t**2*h**2+13*h**4+4*h**6-64*t**2*h**4-24*t**2*h**6)
    E4 = lam**7*math.cos(lat)**7/5040*(61-479*t**2+179*t**4-t**6)
    E = Eo + ko*RN*(E1 + E2 + E3 + E4)

    N1 = lam**2/2 * math.sin(lat) * math.cos(lat)
    N2 = lam**4/24 * math.sin(lat) * math.cos(lat)**3 * (5-t**2+9*h**2+4*h**4)
    N3 = (lam**6/720 * math.sin(lat) * math.cos(lat)**5 *
          (61-58*t**2+t**4+270*h**2- 330*t**2 *h**2+445*h**4+ 324*h**6-680*t**2 *h**4+ 88*h**8-600*t**2 *h**6- 192*t**2 *h**8))
    N4 = lam**8/40320 * math.sin(lat) * math.cos(lat)**7 * (1385-311*t**2+543*t**4-t**6)
    N = No + ko*RN*(s/RN + N1 + N2 + N3 + N4)

    return (N, E, Zone)


"""
MyLogging Test
"""

import time
import logging
import demo3_my_logging  # 导入自定义的logging配置

logger = logging.getLogger(__name__)  # 生成logger实例

def demo():
    logger.debug("start range... time:{}".format(time.time()))
    logger.info("中文测试开始。。。")
    for i in range(10):
        logger.debug("i:{}".format(i))
        time.sleep(0.2)
    else:
        logger.debug("over range... time:{}".format(time.time()))
    logger.info("中文测试结束。。。")

if __name__ == "__main__":
    demo3_my_logging.load_my_logging_cfg()  # 在你程序文件的入口加载自定义logging配置
    demo()


import sqlite3
import csv
import urllib2

conn = sqlite3.connect('listingsALL.db')
conn.isolation_level = None

url = "http://data.insideairbnb.com/united-states/ny/new-york-city/2016-12-03/visualisations/listings.csv"
response = urllib2.urlopen(url)
cr = csv.DictReader(response)

def create_table():
	c.execute("CREATE TABLE IF NOT EXISTS new_york(lat REAL, long REAL, price REAL)")

def data_entry(latitude, longitude, price):
	c.execute("INSERT INTO new_york (lat, long, price) VALUES (?, ?, ?)", (latitude, longitude, price))

try:
	c = conn.cursor()
	c.execute("begin")

	create_table()
	for row in cr:
		data_entry(row['latitude'], row['longitude'], row['price'])

	c.execute("commit")
	c.close()
	conn.close()
except:
	c.execute("rollback")


# 161 - Traffic Lights

file = open('in', 'r')

INF = 99999999

for i in range(3):
  # Input the durations of the traffic lights
  A = []
  intdelay = -1
  line = ['start']
  while intdelay != 0:
    line = file.readline().split()
    print(line)
    for delay in line:
      print(int(delay))
      intdelay = delay
      A.append(int(delay))
      if int(delay) == 0:
        A.pop()
        break

    # print('iteration')
    break

  # A = list(map(int, file.readline().split()))
  print(A)
  # A = list(map(int, input().split()))

  # Check for the end of input
  # if A[0] == 0:
  #   break

  n = len(A)
  time = [0] * n

  s = INF

  for i in range(n):
    if A[i] < s:
      s = A[i]

  # if (s % 10 == 0):
  #   print(s)

  # Iterate over the time until the lights synchronize
  while s <= 18000:
    sync = True

    # Check if all lights are synchronized at time 's'
    for i in range(n):
      while time[i] + A[i] - 5 <= s:
        time[i] += 2 * A[i]

      if not (time[i] <= s < time[i] + A[i] - 5):
        s = time[i] - 1
        yes = False
        break

    # If all lights are synchronized, break out of the loop
    if sync:
      break

    s += 1

  if s > 18000:
    print("Signals fail to synchronise in 5 hours")
  else:
    # Print the synchronized time in HH:MM:SS format
    print(f"{s // 3600:02d}:{(s % 3600) // 60:02d}:{s % 60:02d}")


#!/usr/bin/env python3

import os
import subprocess

def get_all_subdirectories(path: str) -> list:
    return list(filter(os.path.isdir,
                map(lambda x: path + x, os.listdir(path))))

def get_git_status(path: str) -> str:
    wd = os.getcwd()
    os.chdir(path)
    op = subprocess.Popen(["git", "status"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    os.chdir(wd)

    out, err = op.communicate()
    out = out.decode()
    err = err.decode().split("\n")

    if err[0].startswith('fatal'):
        return "no repo"
    return out

def get_all_subdir_status():
    for directory in get_all_subdirectories('./'):
        status = get_git_status(directory)
        d = " ".join(["===",directory,"==="])
        print((": ").join([d, status]))

if __name__ == '__main__':
    get_all_subdir_status()


import cv2
import matplotlib.pyplot as plt
import numpy as np

# import image
img = cv2.imread("image_name.png")
img_vis = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.figure(),plt.imshow(img_vis),plt.axis("off"),plt.title("orginal"),plt.show()

print(img.shape)

img_hist = cv2.calcHist([img],channels=[0],mask=None,histSize= [256],ranges=[0,256])
print(img_hist.shape)
plt.figure(), plt.plot(img_hist)

color = ("b","g","r")
plt.figure()
for i,c in enumerate(color):
    hist = img_hist = cv2.calcHist([img],channels=[i],mask=None,histSize= [256],ranges=[0,256])
    plt.plot(hist,color=c)

#cat image
cat_img = cv2.imread("image_name.jpg")
cat_img_vis = cv2.cvtColor(cat_img, cv2.COLOR_BGR2RGB)
plt.figure(),plt.imshow(cat_img_vis),plt.show()

print(cat_img.shape)

mask = np.zeros(cat_img.shape[:2],np.uint8)
plt.figure(),plt.imshow(mask,cmap="gray"),plt.show()

mask[100:150,120:150] = 255
plt.figure(),plt.imshow(mask,cmap="gray"),plt.show()

mask_cat_img = cv2.bitwise_and(cat_img_vis,cat_img_vis,mask= mask)
plt.figure(),plt.imshow(mask_cat_img,cmap="gray"),plt.show()

mask_cat_img = cv2.bitwise_and(cat_img,cat_img,mask= mask)
masked_cat_img = cv2.calcHist([mask_cat_img],channels=[0],mask=mask,histSize= [256],ranges=[0,256])
plt.figure(),plt.plot(masked_cat_img),plt.show()

# histogram eşitleme
# kontrast artırma
img = cv2.imread("Lenna.png",0)
plt.figure(),plt.imshow(img,cmap="gray"),plt.show()

img_hist = cv2.calcHist([img],channels=[0],mask=None,histSize= [256],ranges=[0,256])
plt.figure(),plt.plot(img_hist),plt.show()

eq_hist = cv2.equalizeHist(img)
plt.figure(),plt.imshow(eq_hist,cmap="gray")

eq_img_hist = cv2.calcHist([eq_hist],channels=[0],mask=None,histSize= [256],ranges=[0,256])
plt.figure(),plt.plot(img_hist),plt.show()

Computer Vision with Opencv and Python/2. image processing with opencv/13_Histogram.py


import os
import pandas as pd
import urllib
import wget
import subprocess
import mojimoji
import re
import numpy as np

# 圧縮ファイルをウェブからダウンロードし解凍 >> テキストファイルを保存
from lhafile import LhaFile

def download_file(obj, date):
    """
    obj (str): 'racelists' or 'results'
    """
    date = str(pd.to_datetime(date).date())
    ymd = date.replace('-', '')
    S, s = ('K', 'k') if obj == 'results' else ('B', 'b')
    if os.path.exists(f'downloads/{obj}/{ymd}.txt'):
        return
    else:
        os.makedirs(f'downloads/{obj}', exist_ok=True)
        try:
            url_t = f'http://www1.mbrace.or.jp/od2/{S}/'
            url_b = f'{ymd[:-2]}/{s}{ymd[2:]}.lzh'
            wget.download(url_t + url_b, f'downloads/{obj}/{ymd}.lzh')
            archive = LhaFile(f'downloads/{obj}/{ymd}.lzh')
            d = archive.read(archive.infolist()[0].filename)
            open(f'downloads/{obj}/{ymd}.txt', 'wb').write(d)
            subprocess.run(['rm', f'downloads/{obj}/{ymd}.lzh'])
        except urllib.request.HTTPError:
            print(f'There are no data for {date}')

# テキストファイルを読み込み、会場ごとのデータにテキストを区切って出力
def read_file(obj, date):
    """
    obj (str): 'racelists' or 'results'
    """
    date = str(pd.to_datetime(date).date())
    ymd = date.replace('-', '')
    f = open(f'downloads/{obj}/{ymd}.txt', 'r', encoding='shift-jis')
    Lines = [l.strip().replace('\u3000', '') for l in f]
    Lines = [mojimoji.zen_to_han(l, kana=False) for l in Lines][1:-1]
    lines_by_plc = {}
    for l in Lines:
        if 'BGN' in l:
            place_cd = int(l[:-4])
            lines = []
        elif 'END' in l:
            lines_by_plc[place_cd] = lines
        else:
            lines.append(l)
    return lines_by_plc

# 出走表ファイルのフォーマットを解析し、いい感じにテーブルに変形して出力
place_mapper = {
    1: '桐生', 2: '戸田', 3: '江戸川', 4: '平和島', 5: '多摩川',
    6: '浜名湖', 7: '蒲郡', 8: '常滑', 9: '津', 10: '三国',
    11: '琵琶湖', 12: '住之江', 13: '尼崎', 14: '鳴門', 15: '丸亀',
    16: '児島', 17: '宮島', 18: '徳山', 19: '下関', 20: '若松',
    21: '芦屋', 22: '福岡', 23: '唐津', 24: '大村'
}

def get_racelists(date):
    info_cols = ['title', 'day', 'date', 'place_cd', 'place']
    race_cols = ['race_no', 'race_type', 'distance', 'deadline']
    keys = ['toban', 'name', 'area', 'class', 'age', 'weight',
            'glob_win', 'glob_in2', 'loc_win', 'loc_in2',
            'moter_no', 'moter_in2', 'boat_no', 'boat_in2']
    racer_cols = [f'{k}_{i}' for k in keys for i in range(1, 7)]
    cols = info_cols + race_cols + racer_cols

    stack = []
    date = str(pd.to_datetime(date).date())
    for place_cd, lines in read_file('racelists', date).items():
        min_lines = 11
        if len(lines) < min_lines:
            continue
        title = lines[4]
        day = int(re.findall('第(\d)日', lines[6].replace(' ', ''))[0])
        place = place_mapper[place_cd]
        info = {k: v for k, v in zip(
            info_cols, [title, day, date, place_cd, place])}

        head_list = []
        race_no = 1
        for i, l in enumerate(lines[min_lines:]):
            if f'{race_no}R' in l:
                head_list.append(min_lines + i)
                race_no += 1
        for race_no, head in enumerate(head_list, 1):
            try:
                race_type = lines[head].split()[1]
                distance = int(re.findall('H(\d*)m', lines[head])[0])
                deadline = re.findall('電話投票締切予定(\d*:\d*)', lines[head])[0]
                arr = []
                for l in lines[head + 5: head + 11]:
                    split = re.findall('\d \d{4}.*\d\d\.\\d\d', l)[0].split()
                    bno = [0]
                    name, area, cls1 = [e for e in re.findall(
                        '[^\d]*', split[1]) if e != '']
                    toban, age, wght, cls2 = [e for e in re.findall(
                        '[\d]*', split[1]) if e != '']
                    tmp = [toban, name, area, cls1 + cls2, age, wght] + split[2:10]
                    if len(tmp) == 14:
                        arr.append(tmp)
                    else:
                        continue
                if len(arr) == 6:
                    dic = info.copy()
                    dic.update(zip(race_cols, [race_no, race_type, distance, deadline]))
                    dic.update(dict(zip(racer_cols, np.array(arr).T.reshape(-1))))
                    stack.append(dic)
            except IndexError:
                continue
            except ValueError:
                continue
    if len(stack) > 0:
        df = pd.DataFrame(stack)[cols].dropna()
        return df
    else:
        return None

# 結果ファイルのフォーマットを解析し、いい感じにテーブルに変形して出力
def get_results(date):
    conv_racetime = lambda x: np.nan if x == '.' else\
        sum([w * float(v) for w, v in zip((60, 1, 1/10), x.split('.'))])
    info_cols = ['title', 'day', 'date', 'place_cd', 'place']
    race_cols = ['race_no', 'race_type', 'distance']
    keys = ['toban', 'name', 'moter_no', 'boat_no',
            'ET', 'SC', 'ST', 'RT', 'position']
    racer_cols = [f'{k}_{i}' for k in keys for i in range(1, 7)]
    res_cols = []
    for k in ('tkt', 'odds', 'poprank'):
        for type_ in ('1t', '1f1', '1f2', '2t', '2f',
                      'w1', 'w2', 'w3', '3t', '3f'):
            if (k == 'poprank') & (type_ in ('1t', '1f1', '1f2')):
                pass
            else:
                res_cols.append(f'{k}_{type_}')
    res_cols.append('win_method')
    cols = info_cols + race_cols + racer_cols + res_cols

    stack = []
    date = str(pd.to_datetime(date).date())
    for place_cd, lines in read_file('results', date).items():
        min_lines = 26
        if len(lines) < min_lines:
            continue
        title = lines[4]
        day = int(re.findall('第(\d)日', lines[6].replace(' ', ''))[0])
        place = place_mapper[place_cd]
        info = {k: v for k, v in zip(
            info_cols, [title, day, date, place_cd, place])}

        head_list = []
        race_no = 1
        for i, l in enumerate(lines[min_lines:]):
            if f'{race_no}R' in l:
                head_list.append(min_lines + i)
                race_no += 1
        for race_no, head in enumerate(head_list, 1):
            try:
                race_type = lines[head].split()[1]
                distance = int(re.findall('H(\d*)m', lines[head])[0])
                win_method = lines[head + 1].split()[-1]
                _, tkt_1t, pb_1t = lines[head + 10].split()
                _, tkt_1f1, pb_1f1, tkt_1f2, pb_1f2 = lines[head + 11].split()
                _, tkt_2t, pb_2t, _, pr_2t = lines[head + 12].split()
                _, tkt_2f, pb_2f, _, pr_2f = lines[head + 13].split()
                _, tkt_w1, pb_w1, _, pr_w1 = lines[head + 14].split()
                tkt_w2, pb_w2, _, pr_w2 = lines[head + 15].split()
                tkt_w3, pb_w3, _, pr_w3 = lines[head + 16].split()
                _, tkt_3t, pb_3t, _, pr_3t = lines[head + 17].split()
                _, tkt_3f, pb_3f, _, pr_3f = lines[head + 18].split()
                race_vals = [race_no, race_type, distance]
                res_vals = [
                    tkt_1t, tkt_1f1, tkt_1f2, tkt_2t, tkt_2f,
                    tkt_w1, tkt_w2, tkt_w3, tkt_3t, tkt_3f,
                    pb_1t, pb_1f1, pb_1f2, pb_2t, pb_2f,
                    pb_w1, pb_w2, pb_w3, pb_3t, pb_3f,
                    pr_2t, pr_2f, pr_w1, pr_w2, pr_w3,
                    pr_3t, pr_3f, win_method
                ]
                dic = info.copy()
                dic.update(dict(zip(race_cols, race_vals)))
                dic.update(dict(zip(res_cols, res_vals)))
                dic = {k: float(v) / 100 if 'odds' in k else v
                       for k, v in dic.items()}
                for i in range(6):
                    bno, *vals = lines[head + 3 + i].split()[1:10]
                    vals.append(i + 1)
                    keys = ['toban', 'name', 'moter_no', 'boat_no',
                            'ET', 'SC', 'ST', 'RT', 'position']
                    dic.update(zip([f'{k}_{bno}' for k in keys], vals))
                stack.append(dic)
            except IndexError:
                continue
            except ValueError:
                continue
    if len(stack) > 0:
        df = pd.DataFrame(stack)[cols].dropna(how='all')
        repl_mapper = {'K': np.nan, '.': np.nan}
        for i in range(1, 7):
            df[f'ET_{i}'] = df[f'ET_{i}'].replace(repl_mapper)
            df[f'ST_{i}'] = df[f'ST_{i}'].replace(repl_mapper)\
                .str.replace('F', '-').str.replace('L', '1')
            df[f'RT_{i}'] = df[f'RT_{i}'].map(conv_racetime)
        waku = np.array([('{}'*6).format(*v) for v in df[
            [f'SC_{i}' for i in range(1, 7)]].values])
        df['wakunari'] = np.where(waku == '123456', 1, 0)
        df = df.replace({'K': np.nan})
        return df
    else:
        return None


# import keras
import glob
import pickle
import random
import time

# import matplotlib.pyplot as plt
import numpy as np

# %%
fileList = glob.glob('/data2T/mariotti_data_2/interp_from_root/MC_channel_last/*.pkl')
random.seed(42)
random.shuffle(fileList)

# %%
befbef = time.time()
times = []
full_energy = np.zeros(269828)
full_interp_M1 = np.zeros((269828, 67, 68, 2))
full_interp_M2 = np.zeros((269828, 67, 68, 2))

# %%
num_items = 0
old = 0
tot = 0
print(f'number of files: {len(fileList)}')
print('start loading...')
for i, file in enumerate(fileList[2250:]):
    print(str(i * 100 / len(fileList[2250:])) + '%')
    bef = time.time()
    with open(file, 'rb') as f:
        data = pickle.load(f)
        num_items = len(data['energy'])
        tot += num_items
        full_energy[old:old + num_items] = data['energy']
        # full_interp_M1[old:old + num_items, :, :, :] = data['M1_interp']
        # full_interp_M2[old:old + num_items, :, :, :] = data['M2_interp']
        old = old + num_items
    now = time.time()
    times.append(now - bef)
nownow = time.time()

print('Number of items: ' + str(tot))
print(f'Time for loading all the files: {nownow-befbef}')
print(f'Average time for loading one dict: {np.mean(np.array(times))}')

# %% Split train/validation/test
import random

idxs = [i for i in range(269828)]
random.shuffle(idxs)
idx_tr = idxs[:int(len(idxs) * 0.5)]
idx_va = idxs[int(len(idxs) * 0.5):int(len(idxs) * 0.75)]
idx_te = idxs[int(len(idxs) * 0.75):]

# %% Dump M1
print('Saving M1...')

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m1_train.pkl',
          'wb') as f:
    pickle.dump(full_interp_M1[idx_tr, :, :, :], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m1_val.pkl',
          'wb') as f:
    pickle.dump(full_interp_M1[idx_va, :, :, :], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m1_test.pkl',
          'wb') as f:
    pickle.dump(full_interp_M1[idx_te, :, :, :], f, protocol=4)

# %% Dump M2
print('Saving M2...')

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m2_train.pkl',
          'wb') as f:
    pickle.dump(full_interp_M2[idx_tr, :, :, :], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m2_val.pkl',
          'wb') as f:
    pickle.dump(full_interp_M2[idx_va, :, :, :], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/m2_test.pkl',
          'wb') as f:
    pickle.dump(full_interp_M2[idx_te, :, :, :], f, protocol=4)

# %% Dump Energy
print('Saving Energy...')

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/energy_train.pkl',
          'wb') as f:
    pickle.dump(full_energy[idx_tr], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/energy_val.pkl',
          'wb') as f:
    pickle.dump(full_energy[idx_va], f, protocol=4)

with open('/data2T/mariotti_data_2/interp_from_root/MC_whole_channel_last/energy_test.pkl',
          'wb') as f:
    pickle.dump(full_energy[idx_te], f, protocol=4)

# %%
print('All done.')


import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer

tokenizer = Tokenizer()
data = '''
In the town of Athy one Jeremy Lanigan
battered away till he hadn't a pound
his father he died and made him a man again
left a farm with ten acres of ground
he gave a grand party for friends a relations
who did not forget him when come to the will
and if you'll but listen I'll make you're eyes glisten
of rows and ructions at Lanigan's Ball

CHORUS
six long months I spent in Dub-i-lin
six long months doing nothin' at all

I stepped out I stepped in again
learning to dance for Lanigan's Ball

Myself to be sure got free invitaions
for all the nice boys and girls I did ask
in less than 10 minutes the friends and relations
were dancing as merry as bee 'round a cask
There was lashing of punch and wine for the ladies
potatoes and cakes there was bacon a tay
there were the O'Shaughnessys, Murphys, Walshes, O'Gradys
courtin' the girls and dancing away

they were doing all kinds of nonsensical polkas
all 'round the room in a whirly gig
but Julia and I soon banished their nonsense
and tipped them a twist of a real Irish jig
Oh how that girl got mad on me
and danced till you'd think the ceilings would fall
for I spent three weeks at Brook's academy
learning to dance for Lanigan's Ball CHORUS

The boys were all merry the girls were all hearty
dancing away in couples and groups
till an accident happened young Terrance McCarthy
put his right leg through Miss Finerty's hoops
The creature she fainted and cried 'melia murder'
cried for her brothers and gathered them all
Carmody swore that he'd go no further
till he'd have satisfaction at Lanigan's Ball

In the midst of the row Miss Kerrigan fainted
her cheeks at the same time as red as a rose
some of the boys decreed she was painted
she took a wee drop too much I suppose
Her sweetheart Ned Morgan all powerful and able
when he saw his fair colleen stretched out by the wall
he tore the left leg from under the table
and smashed all the dishes at Lanigan's Ball CHORUS

Boy oh Boys tis then there was ructions
myself got a kick from big Phelam McHugh
but soon I replied to this kind introduction
and kicked up a terrible hullaballoo
old Casey the piper was near being strangled
they squeezed up his pipes bellows chanters and all
the girls in their ribbons they all got entangled
and that put an end to Lanigan's Ball CHORUS
'''
corpus = data.lower().split('\n')

tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1

# 获取每句话的不同长度的枚举
input_sequences = []
for line in corpus:
    token_list = tokenizer.texts_to_sequences([line])[0]
    # 由于range的特性，这里不用特殊处理(1,0)、(1,1)的情况
    for i in range(1, len(token_list)):
        n_gram_sequences = token_list[:i + 1]
        input_sequences.append(n_gram_sequences)

max_sequences_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences))

# 划分数据集
xs, labels = input_sequences[:, :-1], input_sequences[:, -1]
# ys似one_hot编码
ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)

model = Sequential()
# -1是因为输入为xs
model.add(Embedding(total_words, 64, input_length=max_sequences_len - 1))
model.add(Bidirectional(LSTM(20)))
# total_words决定了要用ys而不是labels
model.add(Dense(total_words, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# 同上，ys是因为最后Dense为多分类输出
num_epochs = 500
history = model.fit(xs, ys, epochs=num_epochs, verbose=0)

def plot_graphs(history, string):
    plt.plot(history.history[string])
    # plt.plot(history.history['val_' + string])
    plt.xlabel("Epochs")
    plt.ylabel(string)
    # plt.legend([string, 'val_' + string])
    plt.show()

plot_graphs(history, "accuracy")
plot_graphs(history, "loss")

seed_text = "Laurence went to dublin"
next_word = 100
for i in range(next_word):
    token_list = tokenizer.texts_to_sequences([seed_text])[0]
    # pad_sequences必须是二维，-1仍然是只作为x
    token_list = pad_sequences([token_list], maxlen=max_sequences_len - 1)
    # predict返回各种答案的概率
    predicted = np.argmax(model.predict(token_list), axis=-1)
    output_word = tokenizer.index_word[predicted[0]]
    seed_text += " " + output_word

print(seed_text)


from w3af.plugins.attack.payloads.base_payload import Payload
from w3af.core.ui.console.tables import table

class hosts(Payload):
    """
    This payload shows the hosts allow and deny files.
    """
    def api_read(self):
        result = {}
        hosts = []

        hosts.append('/etc/hosts')
        hosts.append('/etc/hosts.allow')
        hosts.append('/etc/hosts.deny')

        for file in hosts:
            content = self.shell.read(file)
            if content:
                result[file] = content
        return result

    def run_read(self):
        api_result = self.api_read()

        if not api_result:
            return 'Hosts files not found.'
        else:
            rows = []
            rows.append(['Host file', 'Content'])
            rows.append([])
            for file in api_result:
                rows.append([file, api_result[file]])
                rows.append([])

            result_table = table(rows[:-1])
            result_table.draw(160)
            return rows


import pytest
from appium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait

class Test_ST:
    def setup_class(self):
        # server 启动参数
        desired_caps = {}
        # 设备信息
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.1.1'
        desired_caps['deviceName'] = '127.0.0.1:5555 devices'
        # app信息
        desired_caps['appPackage'] = 'com.android.settings'
        desired_caps['appActivity'] = 'com.android.settings.Settings'
        desired_caps['unicodeKeyboard'] = True
        desired_caps['resetKeyboard'] = True
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def teardown_class(self):
        print(">>>>teardown")

    @pytest.mark.run(order=2)
    def test_001(self):
        # self.driver.find_element_by_xpath("//*[contains(@text, '显示')]").click()
        WebDriverWait(self.driver, 5).until(lambda x: x.find_element_by_xpath("//*[contains(@text, 'daf')]")).click()

        self.driver.find_element_by_xpath("//*[contains(@text, '字体大小')]").click()
        time.sleep(1)
        self.driver.find_elements_by_id("android:id/text1")[0].click()
        summary_list = self.driver.find_elements_by_id("android:id/summary")
        summary_text_list = list()
        for i in summary_list:
            summary_text_list.append(i.text)
        assert "小" in summary_text_list, "失败了~"

    @pytest.mark.run(order=1)
    def test_002(self):
        print(">>>>test_002")


'''
BSD 3-Clause License

Copyright (c) 2021, Logan Bier.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

from direct.showbase.ShowBase import ShowBase
from direct.stdpy import threading2
from panda3d.core import load_prc_file_data, BitMask32, TransformState, ConfigVariableManager
from panda3d.core import FrameBufferProperties, AntialiasAttrib, InputDevice, Texture
import sys
import random
import time
from panda3d.core import LPoint3f, Point3, Vec3, Vec4, LVecBase3f, VBase4, LPoint2f
from panda3d.core import WindowProperties
from direct.showbase.DirectObject import DirectObject
from direct.interval.IntervalGlobal import *
# gui imports
from direct.gui.DirectGui import *
from panda3d.core import TextNode
# new pbr imports
import complexpbr
# local imports
import actor_data
import arena_lighting

class app(ShowBase):
    def __init__(self):
        load_prc_file_data("", """
            win-size 1920 1080
            window-title complexpbr Demo
            framebuffer-multisample 1
            multisamples 4
            hardware-animated-vertices #t
            cursor-hidden #t
        """)

        # initialize the showbase
        super().__init__()

        # lighting
        arena_lighting.lighting()
        arena_lighting.init_flashlight()
        self.accept('f', arena_lighting.toggle_flashlight)
        self.accept("gamepad-face_x", arena_lighting.toggle_flashlight)

        # complexpbr
        complexpbr.apply_shader(self.render)

        def quality_mode():
            complexpbr.screenspace_init()

            base.screen_quad.set_shader_input("bloom_intensity", 0.3)
            base.screen_quad.set_shader_input("bloom_threshold", 0.7)
            base.screen_quad.set_shader_input("bloom_blur_width", 10)
            base.screen_quad.set_shader_input("bloom_samples", 6)
            base.screen_quad.set_shader_input('ssr_samples', 0)
            base.screen_quad.set_shader_input('ssao_samples', 6)
            base.screen_quad.set_shader_input('hsv_r', 1.0)
            base.screen_quad.set_shader_input('hsv_g', 1.1)
            base.screen_quad.set_shader_input('hsv_b', 1.0)

            text_1.set_text("Quality Mode: On")

        self.accept_once('m', quality_mode)
        self.accept_once("gamepad-face_y", quality_mode)

        def save_screen():
            base.screenshot('arena_screen')

        self.accept('o', save_screen)

        # window props
        props = WindowProperties()
        props.set_mouse_mode(WindowProperties.M_relative)
        base.win.request_properties(props)
        base.set_background_color(0.5, 0.5, 0.8)

        self.camLens.set_fov(90)
        self.camLens.set_near_far(0.1, 5000)
        # ConfigVariableManager.getGlobalPtr().listVariables()

        self.accept("f3", self.toggle_wireframe)
        self.accept("escape", sys.exit, [0])

        self.game_start = 0

        # gamepad initialization
        self.gamepad = None
        devices = self.devices.get_devices(InputDevice.DeviceClass.gamepad)

        if int(str(devices)[0]) > 0:
            self.gamepad = devices[0]

        def do_nothing():
            print('Something should happen but I cannot effect it.')

        def gp_exit():
            sys.exit()[0]

        self.accept("gamepad-back", gp_exit)
        self.accept("gamepad-start", do_nothing)
        # self.accept("gamepad-face_x", do_nothing)
        # self.accept("gamepad-face_x-up", do_nothing)
        # self.accept("gamepad-face_a", do_nothing)
        # self.accept("gamepad-face_a-up", do_nothing)
        self.accept("gamepad-face_b", do_nothing)
        self.accept("gamepad-face_b-up", do_nothing)
        # self.accept("gamepad-face_y", do_nothing)
        # self.accept("gamepad-face_y-up", do_nothing)

        if int(str(devices)[0]) > 0:
            base.attach_input_device(self.gamepad, prefix="gamepad")

        self.right_trigger_val = 0.0
        self.left_trigger_val = 0.0
        # end gamepad initialization

        # begin physics environment
        from panda3d.bullet import BulletWorld
        from panda3d.bullet import BulletCharacterControllerNode
        from panda3d.bullet import ZUp
        from panda3d.bullet import BulletCapsuleShape
        from panda3d.bullet import BulletTriangleMesh
        from panda3d.bullet import BulletTriangleMeshShape
        from panda3d.bullet import BulletBoxShape, BulletSphereShape
        from panda3d.bullet import BulletGhostNode
        from panda3d.bullet import BulletRigidBodyNode
        from panda3d.bullet import BulletPlaneShape

        self.world = BulletWorld()
        self.world.set_gravity(Vec3(0, 0, -9.81))

        arena_1 = self.loader.load_model('models/arena_1.gltf')
        arena_1.reparent_to(self.render)
        arena_1.set_pos(0, 0, 0)

        def make_collision_from_model(input_model, node_number, mass, world, target_pos):
            # tristrip generation from static models
            # generic tri-strip collision generator begins
            geom_nodes = input_model.find_all_matches('**/+GeomNode')
            geom_nodes = geom_nodes.get_path(node_number).node()
            # print(geom_nodes)
            geom_target = geom_nodes.get_geom(0)
            # print(geom_target)
            output_bullet_mesh = BulletTriangleMesh()
            output_bullet_mesh.add_geom(geom_target)
            tri_shape = BulletTriangleMeshShape(output_bullet_mesh, dynamic=False)
            print(output_bullet_mesh)

            body = BulletRigidBodyNode('input_model_tri_mesh')
            np = self.render.attach_new_node(body)
            np.node().add_shape(tri_shape)
            np.node().set_mass(mass)
            np.node().set_friction(0.01)
            np.set_pos(target_pos)
            np.set_scale(1)
            # np.set_h(180)
            # np.set_p(180)
            # np.set_r(180)
            np.set_collide_mask(BitMask32.allOn())
            world.attach_rigid_body(np.node())

        make_collision_from_model(arena_1, 0, 0, self.world, (arena_1.get_pos()))

        # initialize player character physics the Bullet way
        shape_1 = BulletCapsuleShape(0.75, 0.5, ZUp)
        player_node = BulletCharacterControllerNode(shape_1, 0.1, 'Player')  # (shape, mass, player name)
        player_np = self.render.attach_new_node(player_node)
        player_np.set_pos(-20, -10, 30)
        player_np.set_collide_mask(BitMask32.allOn())
        self.world.attach_character(player_np.node())
        # cast player_np to self.player
        self.player = player_np

        # reparent player character to render node
        fp_character = actor_data.player_character
        fp_character.reparent_to(self.render)
        fp_character.set_scale(1)
        # set the complexpbr actor skinning
        complexpbr.skin(fp_character)

        self.camera.reparent_to(self.player)
        # reparent character to FPS cam
        fp_character.reparent_to(self.player)
        fp_character.set_pos(0, 0, -0.95)
        # self.camera.set_x(self.player, 1)
        self.camera.set_y(self.player, 0.03)
        self.camera.set_z(self.player, 0.5)

        # player gun begins
        self.player_gun = actor_data.arm_handgun
        self.player_gun.reparent_to(self.render)
        self.player_gun.reparent_to(self.camera)
        # set the complexpbr actor skinning
        complexpbr.skin(self.player_gun)
        self.player_gun.set_light_off(base.render.find('**/slight_1'))
        self.player_gun.set_x(self.camera, 0.1)
        self.player_gun.set_y(self.camera, 0.4)
        self.player_gun.set_z(self.camera, -0.1)

        # directly make a text node to display text
        text_1 = TextNode('text_1_node')
        text_1.set_text("Quality Mode: Off  (Press 'm' to turn on.)")
        if int(str(devices)[0]) > 0:
            text_1.set_text("Quality Mode: Off  (Press 'Y' to turn on.)")
        # text_1.set_text("Quality Mode: Off  (Press 'm' to turn on.)")
        text_1_node = self.aspect2d.attach_new_node(text_1)
        text_1_node.set_scale(0.04)
        text_1_node.set_pos(-1.4, 0, 0.92)
        # import font and set pixels per unit font quality
        nunito_font = loader.load_font('fonts/Nunito/Nunito-Light.ttf')
        nunito_font.set_pixels_per_unit(100)
        nunito_font.set_page_size(512, 512)
        # apply font
        text_1.set_font(nunito_font)
        text_1.set_shadow(0.1)

        # on-screen target dot for aiming
        target_dot = TextNode('target_dot_node')
        target_dot.set_text(".")
        target_dot_node = self.aspect2d.attach_new_node(target_dot)
        target_dot_node.set_scale(0.075)
        target_dot_node.set_pos(0, 0, 0)
        # target_dot_node.hide()
        # apply font
        target_dot.set_font(nunito_font)
        target_dot.set_align(TextNode.ACenter)
        # see the Task section for relevant dot update logic

        # directly make a text node to display text
        text_2 = TextNode('text_2_node')
        text_2.set_text("Neutralize the NPC by shooting the head." + '\n' +
                        "Press 'f' to toggle the flashlight." + '\n' + "Right-click to jump.")
        if int(str(devices)[0]) > 0:
            text_2.set_text("Neutralize the NPC by shooting the head." + '\n' +
                            "Press 'X' to toggle the flashlight." + '\n' + "Press 'A' to jump.")
        text_2_node = self.aspect2d.attach_new_node(text_2)
        text_2_node.set_scale(0.04)
        text_2_node.set_pos(-1.4, 0, 0.8)
        # import font and set pixels per unit font quality
        nunito_font = self.loader.load_font('fonts/Nunito/Nunito-Light.ttf')
        nunito_font.set_pixels_per_unit(100)
        nunito_font.set_page_size(512, 512)
        # apply font
        text_2.set_font(nunito_font)
        text_2.set_text_color(0.9, 0.9, 0.9, 1)
        text_2.set_shadow(0.1)

        # print player position on mouse click
        def print_player_pos():
            print(self.player.get_pos())
            self.player.node().do_jump()

        self.accept('mouse3', print_player_pos)
        self.accept("gamepad-face_a", print_player_pos)

        # add a few random physics spheres
        for x in range(0, 30):
            # dynamic collision
            random_vec = Vec3(1, 1, 1)
            # special_shape = BulletBoxShape(random_vec)
            special_shape = BulletSphereShape(random_vec[0])
            # rigidbody
            body = BulletRigidBodyNode('random_prisms')
            d_coll = self.render.attach_new_node(body)
            d_coll.node().add_shape(special_shape)
            d_coll.node().set_mass(15)
            d_coll.node().set_friction(50)
            d_coll.set_collide_mask(BitMask32.allOn())
            # turn on Continuous Collision Detection
            d_coll.node().set_ccd_motion_threshold(0.000000007)
            d_coll.node().set_ccd_swept_sphere_radius(0.30)
            d_coll.node().set_deactivation_enabled(False)  # prevents stopping the physics simulation
            d_coll.set_pos(random.uniform(-60, -20), random.uniform(-60, -20), random.uniform(50, 800))
            sphere_choices = ['1m_sphere_black_marble','1m_sphere_purple_metal','1m_sphere_concrete_1','1m_sphere_bright_1']
            sphere_choice = random.choice(sphere_choices)
            box_model = self.loader.load_model('models/' + sphere_choice + '.gltf')
            box_model.reparent_to(self.render)
            box_model.reparent_to(d_coll)
            # box_model.set_pos(0,0,-1)

            if sphere_choice == '1m_sphere_concrete_1':
                dis_tex = Texture()
                dis_tex.read('textures/get_file_Concrete017_2K-PNG/Concrete017_2K_Displacement.png')
                box_model.set_shader_input('displacement_map', dis_tex)
                box_model.set_shader_input('displacement_scale', 0.03)

            self.world.attach_rigid_body(d_coll.node())

        # NPC_1 load-in
        comp_shape_1 = BulletCapsuleShape(0.05, 0.01, ZUp)
        npc_1_node = BulletCharacterControllerNode(comp_shape_1, 0.2, 'NPC_A_node')  # (shape, mass, character name)
        np = self.render.attach_new_node(npc_1_node)
        np.set_pos(-40, -40, 5)
        np.set_collide_mask(BitMask32.allOn())
        self.world.attach_character(np.node())
        np.set_h(random.randint(0, 180))
        npc_model_1 = actor_data.NPC_1
        npc_model_1.reparent_to(np)
        # set the complexpbr actor skinning
        complexpbr.skin(npc_model_1)
        # get the separate head model
        npc_1_head = self.loader.load_model('models/npc_1_head.gltf')
        npc_1_head.reparent_to(actor_data.NPC_1.get_parent())

        # npc base animation loop
        npc_1_control = actor_data.NPC_1.get_anim_control('walking')
        if not npc_1_control.is_playing():
            actor_data.NPC_1.stop()
            actor_data.NPC_1.loop("walking", fromFrame = 60, toFrame = 180)
            actor_data.NPC_1.set_play_rate(6.0, 'walking')

        # create special hit areas
        # use Task section for npc collision movement logic
        # special head node size
        special_shape = BulletBoxShape(Vec3(0.1, 0.1, 0.1))
        # ghost npc node
        body = BulletGhostNode('special_node_A')
        special_node = self.render.attach_new_node(body)
        special_node.node().add_shape(special_shape, TransformState.makePos(Point3(0, 0, 0.4)))
        # special_node.node().set_mass(0)
        # special_node.node().set_friction(0.5)
        special_node.set_collide_mask(BitMask32(0x0f))
        # turn on Continuous Collision Detection
        special_node.node().set_deactivation_enabled(False)
        special_node.node().set_ccd_motion_threshold(0.000000007)
        special_node.node().set_ccd_swept_sphere_radius(0.30)
        self.world.attach_ghost(special_node.node())

        # dynamic collision
        special_shape = BulletBoxShape(Vec3(0.3, 0.15, 0.6))
        # rigidbody npc node
        body = BulletRigidBodyNode('d_coll_A')
        d_coll = self.render.attach_new_node(body)
        d_coll.node().add_shape(special_shape, TransformState.makePos(Point3(0, 0, 0.7)))
        # d_coll.node().set_mass(0)
        d_coll.node().set_friction(0.5)
        d_coll.set_collide_mask(BitMask32.allOn())
        # turn on Continuous Collision Detection
        d_coll.node().set_deactivation_enabled(False)
        d_coll.node().set_ccd_motion_threshold(0.000000007)
        d_coll.node().set_ccd_swept_sphere_radius(0.30)
        self.world.attach_rigid_body(d_coll.node())

        # npc state variables
        self.npc_1_is_dead = False
        self.npc_1_move_increment = Vec3(0, 0, 0)
        self.gun_anim_is_playing = False

        # npc movement timer
        def npc_1_move_gen():
            while not self.npc_1_is_dead:
                m_incs = []
                for x in range(0, 2):
                    m_incs.append(random.uniform(2, 5))

                # print('NPC_1 movement increments this cycle: ' + str(m_incs))
                self.npc_1_move_increment[0] = m_incs[0]
                self.npc_1_move_increment[1] = m_incs[1]
                time.sleep(3)
                self.npc_1_move_increment[0] = m_incs[0]
                self.npc_1_move_increment[1] = m_incs[1]
                time.sleep(3)
                self.npc_1_move_increment[0] = (-1 * m_incs[0]) * 2
                self.npc_1_move_increment[1] = (-1 * m_incs[1]) * 2
                time.sleep(3)
                self.npc_1_move_increment[0] = 0
                self.npc_1_move_increment[1] = 0

        # activate the movement timer in a dedicated thread to prevent lockup with .sleep()
        threading2._start_new_thread(npc_1_move_gen, ())

        def is_npc_1_shot_seq():
            def gun_anim():
                gun_ctrl = actor_data.arm_handgun.get_anim_control('shoot')
                if not gun_ctrl.is_playing():
                    actor_data.arm_handgun.stop()
                    actor_data.arm_handgun.play("shoot")
                    actor_data.arm_handgun.set_play_rate(12.0, 'shoot')

            seq = Sequence()
            seq.append(Func(gun_anim))
            seq.append(Wait(0.2))

            # target dot ray test
            # get mouse data
            mouse_watch = base.mouseWatcherNode
            if mouse_watch.has_mouse():
                posMouse = base.mouseWatcherNode.get_mouse()
                posFrom = Point3()
                posTo = Point3()
                base.camLens.extrude(posMouse, posFrom, posTo)
                posFrom = self.render.get_relative_point(base.cam, posFrom)
                posTo = self.render.get_relative_point(base.cam, posTo)
                rayTest = self.world.ray_test_closest(posFrom, posTo)
                target = rayTest.get_node()
                target_dot = self.aspect2d.find_all_matches("**/target_dot_node")

                if 'special_node_A' in str(target):
                    def npc_cleanup():
                        # the head is hit, the npc is dead
                        self.npc_1_is_dead = True
                        text_2.set_text('Congrats, you have won!')
                        npc_1_control = actor_data.NPC_1.get_anim_control('walking')
                        if npc_1_control.is_playing():
                            actor_data.NPC_1.stop()
                        npc_1_control = actor_data.NPC_1.get_anim_control('death')
                        if not npc_1_control.is_playing():
                            actor_data.NPC_1.play('death')

                        # Bullet node removals
                        self.world.remove(target)
                        rigid_target = self.render.find('**/d_coll_A')
                        self.world.remove(rigid_target.node())

                    seq.append(Func(npc_cleanup))
                    seq.start()

                else:
                    seq.start()

        self.accept('mouse1', is_npc_1_shot_seq)

        self.gamepad_npc_cleanup_bool = False

        def gamepad_trigger_shoot_seq():
            def gun_anim():
                gun_ctrl = actor_data.arm_handgun.get_anim_control('shoot')
                if not gun_ctrl.is_playing():
                    actor_data.arm_handgun.stop()
                    actor_data.arm_handgun.play("shoot")
                    actor_data.arm_handgun.set_play_rate(12.0, 'shoot')

            seq = Sequence()
            seq.append(Func(gun_anim))
            seq.append(Wait(0.2))

            # target dot ray test
            # get mouse data
            def gamepad_npc_test_cleanup():
                posMouse = LPoint2f(0, 0)
                posFrom = Point3()
                posTo = Point3()
                base.camLens.extrude(posMouse, posFrom, posTo)
                posFrom = self.render.get_relative_point(base.cam, posFrom)
                posTo = self.render.get_relative_point(base.cam, posTo)
                rayTest = self.world.ray_test_closest(posFrom, posTo)
                target = rayTest.get_node()
                target_dot = self.aspect2d.find_all_matches("**/target_dot_node")

                if 'special_node_A' in str(target):
                    def npc_cleanup():
                        # the head is hit, the npc is dead
                        self.npc_1_is_dead = True
                        text_2.set_text('Congrats, you have won!')
                        npc_1_control = actor_data.NPC_1.get_anim_control('walking')
                        if npc_1_control.is_playing():
                            actor_data.NPC_1.stop()
                        npc_1_control = actor_data.NPC_1.get_anim_control('death')
                        if not npc_1_control.is_playing():
                            actor_data.NPC_1.play('death')

                        # Bullet node removals
                        self.world.remove(target)
                        rigid_target = self.render.find('**/d_coll_A')
                        self.world.remove(rigid_target.node())

                    seq.append(Func(npc_cleanup))
                    seq.start()

                else:
                    seq.start()

            if not self.gamepad_npc_cleanup_bool:
                gamepad_npc_test_cleanup()

        # 3D player movement system begins
        self.keyMap = {"left": 0, "right": 0, "forward": 0, "backward": 0, "run": 0, "jump": 0}

        def setKey(key, value):
            self.keyMap[key] = value

        # define button map
        self.accept("a", setKey, ["left", 1])
        self.accept("a-up", setKey, ["left", 0])
        self.accept("d", setKey, ["right", 1])
        self.accept("d-up", setKey, ["right", 0])
        self.accept("w", setKey, ["forward", 1])
        self.accept("w-up", setKey, ["forward", 0])
        self.accept("s", setKey, ["backward", 1])
        self.accept("s-up", setKey, ["backward", 0])
        self.accept("shift", setKey, ["run", 1])
        self.accept("shift-up", setKey, ["run", 0])
        self.accept("space", setKey, ["jump", 1])
        self.accept("space-up", setKey, ["jump", 0])
        # disable mouse
        self.disable_mouse()

        # the player movement speed
        self.movementSpeedForward = 5
        self.movementSpeedBackward = 5
        self.dropSpeed = -0.2
        self.striveSpeed = 6
        self.static_pos_bool = False
        self.static_pos = Vec3()

        def animate_player():
            myAnimControl = actor_data.player_character.get_anim_control('walking')
            if not myAnimControl.isPlaying():
                actor_data.player_character.play("walking")
                actor_data.player_character.set_play_rate(4.0, 'walking')

        def move_npc(Task):
            if self.game_start > 0:
                if not self.npc_1_is_dead:
                    npc_pos_1 = actor_data.NPC_1.get_parent().get_pos()
                    # place head hit box
                    special_node.set_pos(npc_pos_1[0], npc_pos_1[1], npc_pos_1[2] + 1)
                    special_node.set_h(actor_data.NPC_1.get_h())
                    # dynamic collision node
                    d_coll.set_pos(npc_pos_1[0], npc_pos_1[1], npc_pos_1[2])
                    d_coll.set_h(actor_data.NPC_1.get_h())
                    # make the npc look at the player continuously
                    actor_data.NPC_1.look_at(self.player)
                    npc_1_head.look_at(self.player)

                    if actor_data.NPC_1.get_p() > 3:
                        actor_data.NPC_1.set_p(3)

                    if npc_1_head.get_p() > 3:
                        npc_1_head.set_p(3)

                    m_inst = self.npc_1_move_increment
                    t_inst = globalClock.get_dt()
                    actor_data.NPC_1.get_parent().set_pos(npc_pos_1[0] + (m_inst[0] * t_inst), npc_pos_1[1] + (m_inst[1] * t_inst), npc_pos_1[2])

                if self.npc_1_is_dead:
                    npc_1_head.hide()
                    inst_h = actor_data.NPC_1.get_h()
                    inst_p = actor_data.NPC_1.get_p()
                    actor_data.NPC_1.set_hpr(inst_h, inst_p, 0)

            return Task.cont

        def move(Task):
            if self.game_start > 0:
                # target dot ray test
                # turns the target dot red
                # get mouse data
                mouse_watch = base.mouseWatcherNode
                if mouse_watch.has_mouse():
                    posMouse = base.mouseWatcherNode.get_mouse()
                    # print(posMouse)
                    posFrom = Point3()
                    posTo = Point3()
                    base.camLens.extrude(posMouse, posFrom, posTo)
                    posFrom = self.render.get_relative_point(base.cam, posFrom)
                    posTo = self.render.get_relative_point(base.cam, posTo)
                    rayTest = self.world.ray_test_closest(posFrom, posTo)
                    target = rayTest.get_node()
                    target_dot = self.aspect2d.find_all_matches("**/target_dot_node")

                    if 'special_node_A' in str(target):
                        # the npc is recognized, make the dot red
                        for dot in target_dot:
                            dot.node().set_text_color(0.9, 0.1, 0.1, 1)

                    if 'd_coll_A' in str(target):
                        # the npc is recognized, make the dot red
                        for dot in target_dot:
                            dot.node().set_text_color(0.9, 0.1, 0.1, 1)

                    if 'special_node_A' not in str(target):
                        # no npc recognized, make the dot white
                        if 'd_coll_A' not in str(target):
                            for dot in target_dot:
                                dot.node().set_text_color(1, 1, 1, 1)

                # get mouse data
                mouse_watch = base.mouseWatcherNode
                if mouse_watch.has_mouse():
                    pointer = base.win.get_pointer(0)
                    mouseX = pointer.get_x()
                    mouseY = pointer.get_y()

                # screen sizes
                window_Xcoord_halved = base.win.get_x_size() // 2
                window_Ycoord_halved = base.win.get_y_size() // 2
                # mouse speed
                mouseSpeedX = 0.2
                mouseSpeedY = 0.2
                # maximum and minimum pitch
                maxPitch = 90
                minPitch = -50
                # cam view target initialization
                camViewTarget = LVecBase3f()

                if base.win.movePointer(0, window_Xcoord_halved, window_Ycoord_halved):
                    p = 0

                    if mouse_watch.has_mouse():
                        # calculate the pitch of camera
                        p = self.camera.get_p() - (mouseY - window_Ycoord_halved) * mouseSpeedY

                    # sanity checking
                    if p < minPitch:
                        p = minPitch
                    elif p > maxPitch:
                        p = maxPitch

                    if mouse_watch.has_mouse():
                        # directly set the camera pitch
                        self.camera.set_p(p)
                        camViewTarget.set_y(p)

                    # rotate the self.player's heading according to the mouse x-axis movement
                    if mouse_watch.has_mouse():
                        h = self.player.get_h() - (mouseX - window_Xcoord_halved) * mouseSpeedX

                    if mouse_watch.has_mouse():
                        # sanity checking
                        if h < -360:
                            h += 360

                        elif h > 360:
                            h -= 360

                        self.player.set_h(h)
                        camViewTarget.set_x(h)

                    # hide the gun if looking straight down
                    if p < -30:
                        self.player_gun.hide()
                    if p > -30:
                        self.player_gun.show()

                if self.keyMap["left"]:
                    if self.static_pos_bool:
                        self.static_pos_bool = False

                    self.player.set_x(self.player, -self.striveSpeed * globalClock.get_dt())

                    animate_player()

                if not self.keyMap["left"]:
                    if not self.static_pos_bool:
                        self.static_pos_bool = True
                        self.static_pos = self.player.get_pos()

                    self.player.set_x(self.static_pos[0])
                    self.player.set_y(self.static_pos[1])
                    # self.player.set_z(self.player, self.dropSpeed * globalClock.get_dt())

                if self.keyMap["right"]:
                    if self.static_pos_bool:
                        self.static_pos_bool = False

                    self.player.set_x(self.player, self.striveSpeed * globalClock.get_dt())

                    animate_player()

                if not self.keyMap["right"]:
                    if not self.static_pos_bool:
                        self.static_pos_bool = True
                        self.static_pos = self.player.get_pos()

                    self.player.set_x(self.static_pos[0])
                    self.player.set_y(self.static_pos[1])
                    # self.player.set_z(self.player, self.dropSpeed * globalClock.get_dt())

                if self.keyMap["forward"]:
                    if self.static_pos_bool:
                        self.static_pos_bool = False

                    self.player.set_y(self.player, self.movementSpeedForward * globalClock.get_dt())

                    animate_player()

                if self.keyMap["forward"] != 1:
                    if not self.static_pos_bool:
                        self.static_pos_bool = True
                        self.static_pos = self.player.get_pos()

                    self.player.set_x(self.static_pos[0])
                    self.player.set_y(self.static_pos[1])
                    # self.player.set_z(self.player, self.dropSpeed * globalClock.get_dt())

                if self.keyMap["backward"]:
                    if self.static_pos_bool:
                        self.static_pos_bool = False

                    self.player.set_y(self.player, -self.movementSpeedBackward * globalClock.get_dt())

                    animate_player()

            return Task.cont

        def gp_move(Task):
            if self.game_start > 0:
                def gamepad_mouse_test():
                    posMouse = LPoint2f(0, 0)
                    posFrom = Point3()
                    posTo = Point3()
                    base.camLens.extrude(posMouse, posFrom, posTo)
                    posFrom = self.render.get_relative_point(base.cam, posFrom)
                    posTo = self.render.get_relative_point(base.cam, posTo)
                    rayTest = self.world.ray_test_closest(posFrom, posTo)
                    target = rayTest.get_node()
                    target_dot = self.aspect2d.find_all_matches("**/target_dot_node")

                    if 'special_node_A' in str(target):
                        # the npc is recognized, make the dot red
                        for dot in target_dot:
                            dot.node().set_text_color(0.9, 0.1, 0.1, 1)

                    if 'd_coll_A' in str(target):
                        # the npc is recognized, make the dot red
                        for dot in target_dot:
                            dot.node().set_text_color(0.9, 0.1, 0.1, 1)

                    if 'special_node_A' not in str(target):
                        # no npc recognized, make the dot white
                        if 'd_coll_A' not in str(target):
                            for dot in target_dot:
                                dot.node().set_text_color(1, 1, 1, 1)

                gamepad_mouse_test()

            dt = globalClock.get_dt()

            right_trigger = self.gamepad.find_axis(InputDevice.Axis.right_trigger)
            left_trigger = self.gamepad.find_axis(InputDevice.Axis.left_trigger)
            self.right_trigger_val = right_trigger.value
            self.left_trigger_val = left_trigger.value

            if self.right_trigger_val > 0.2:
                gamepad_trigger_shoot_seq()

            xy_speed = 7
            p_speed = 60
            rotate_speed = 100

            r_stick_right_axis = self.gamepad.find_axis(InputDevice.Axis.left_y)
            r_stick_left_axis = self.gamepad.find_axis(InputDevice.Axis.left_x)
            l_stick_right_axis = self.gamepad.find_axis(InputDevice.Axis.right_y)
            l_stick_left_axis = self.gamepad.find_axis(InputDevice.Axis.right_x)

            if abs(r_stick_right_axis.value) >= 0.15 or abs(r_stick_left_axis.value) >= 0.15:
                if self.static_pos_bool:
                    self.static_pos_bool = False

                self.player.set_h(self.player, rotate_speed * dt * -r_stick_left_axis.value)
                self.player.set_y(self.player, xy_speed * dt * r_stick_right_axis.value)

                animate_player()

            if abs(r_stick_right_axis.value) < 0.15 or abs(r_stick_left_axis.value) < 0.15:
                if not self.static_pos_bool:
                    self.static_pos_bool = True
                    self.static_pos = self.player.get_pos()

                self.player.set_y(self.static_pos[1])

            # reset camera roll
            self.camera.set_r(0)
            self.player.set_r(0)

            min_p = -49
            max_p = 80

            if abs(l_stick_right_axis.value) >= 0.15 or abs(l_stick_left_axis.value) >= 0.15:
                if self.static_pos_bool:
                    self.static_pos_bool = False

                if self.camera.get_p() < max_p:
                    if self.camera.get_p() > min_p:
                        self.camera.set_p(self.camera, p_speed * dt * l_stick_right_axis.value)
                    if self.camera.get_p() < min_p:
                        self.camera.set_p(self.camera, p_speed * dt * -l_stick_right_axis.value)

                if self.camera.get_p() >= max_p:
                    self.camera.set_p(79)

                self.player.set_x(self.player, xy_speed * dt * l_stick_left_axis.value)

                animate_player()

            if abs(l_stick_right_axis.value) < 0.15 or abs(l_stick_left_axis.value) < 0.15:
                if not self.static_pos_bool:
                    self.static_pos_bool = True
                    self.static_pos = self.player.get_pos()

                self.player.set_x(self.static_pos[0])

            # hide the gun if looking straight down
            if self.camera.get_p() < -30:
                self.player_gun.hide()
            if self.camera.get_p() > -30:
                self.player_gun.show()

            return Task.cont

        # infinite ground plane
        # the effective world-Z limit
        ground_plane = BulletPlaneShape(Vec3(0, 0, 1), 0)
        node = BulletRigidBodyNode('ground')
        node.add_shape(ground_plane)
        node.set_friction(0.1)
        np = self.render.attach_new_node(node)
        np.set_pos(0, 0, -1)
        self.world.attach_rigid_body(node)

        # Bullet debugger
        from panda3d.bullet import BulletDebugNode
        debugNode = BulletDebugNode('Debug')
        debugNode.show_wireframe(True)
        debugNode.show_constraints(True)
        debugNode.show_bounding_boxes(False)
        debugNode.show_normals(False)
        debugNP = self.render.attach_new_node(debugNode)
        self.world.set_debug_node(debugNP.node())

        # debug toggle function
        def toggle_debug():
            if debugNP.is_hidden():
                debugNP.show()
            else:
                debugNP.hide()

        self.accept('f1', toggle_debug)

        def update(Task):
            if self.game_start < 1:
                self.game_start = 1
            return Task.cont

        def physics_update(Task):
            dt = globalClock.get_dt()
            self.world.do_physics(dt)
            return Task.cont

        if self.gamepad is None:
            self.task_mgr.add(move)

        if int(str(devices)[0]) > 0:
            self.task_mgr.add(gp_move)

        self.task_mgr.add(update)
        self.task_mgr.add(move_npc)
        self.task_mgr.add(physics_update)

app().run()


import sys

def histogram(words):
    """
        Takes an argument(corpus) and returns the times each word is used
    """
    wordlists = []
    for word in words:
        if word not in wordlists:
            wordlists.append([word, 1])
        for item in wordlists:
            if item[0] == word:
                item[1] += 1
    return wordlists

if __name__ == '__main__':
    words = open(sys.argv[1], "r").read().split()
    print(histogram(words))


# %%
import os
import random
import numpy as np
from numpy.lib.shape_base import expand_dims
from tqdm import tqdm
from PIL import Image

# %%
class_indices_S2_rev = {
    'Waterbuck': 1,
    'Baboon': 2,
    'Warthog': 3,
    'Bushbuck': 4,
    'Impala': 5,
    'Oribi': 6,
    'Elephant': 7,
    'Genet': 8,
    'Nyala': 9,
    'Setup': 10,
    'Bushpig': 11,
    'Porcupine': 12,
    'Civet': 13,
    'Vervet': 14,
    'Reedbuck': 15,
    'Kudu': 16,
    'Buffalo': 17,
    'Sable_antelope': 18,
    'Duiker_red': 19,
    'Hartebeest': 20,
    'Wildebeest': 21,
    'Guineafowl_helmeted': 22,
    'Hare': 23,
    'Duiker_common': 24,
    'Fire': 25,
    'Mongoose_marsh': 26,
    'Aardvark': 27,
    'Honey_badger': 28,
    'Hornbill_ground': 29,
    'Mongoose_slender': 30,
    'Mongoose_bushy_tailed': 31,
    'Samango': 32,
    'Mongoose_white_tailed': 33,
    'Mongoose_banded': 34,
    'Mongoose_large_grey': 35,
    'Bushbaby': 36,
    'Guineafowl_crested': 37,
    'Eland': 38,
    'Lion': 39,
    'Serval': 40
}

class_indices_S2 = {class_indices_S2_rev[k]: k for k in class_indices_S2_rev}

# %%
image_root = '/home/zhmiao/datasets/ecology/Mozambique/Mozambique_season_3'
confident_path = '/home/zhmiao/repos/AnimalActiveLearning/weights/SemiStage2OLTR_Energy/111620_MOZ_PSLABEL_OLTR_Energy_0_preds_conf.txt'
unconfident_path = '/home/zhmiao/repos/AnimalActiveLearning/weights/SemiStage2OLTR_Energy/111620_MOZ_PSLABEL_OLTR_Energy_0_preds_unconf.txt'
validation_path = '/home/zhmiao/repos/AnimalActiveLearning/weights/SemiStage2OLTR_Energy/111620_MOZ_PSLABEL_OLTR_Energy_0_preds_eval.txt'

# %%
def load_list(path):
    with open(path, 'r') as f:
        file_id_list = []
        cat_list = []
        for line in tqdm(f):
            line_sp = line.replace('\n', '').rsplit(' ', 1)
            file_id = line_sp[0]
            cat = class_indices_S2[int(line_sp[1])]
            file_id_list.append(file_id)
            cat_list.append(cat)
    return (np.array(file_id_list), np.array(cat_list))

# %%
file_id_conf, cat_conf = load_list(confident_path)
file_id_unconf, cat_unconf = load_list(unconfident_path)

# %%
seed = random.choice(list(range(0, 50000)))
random.seed(seed)
target_spp = random.choice(list(class_indices_S2.values()))
conf_id = random.choice(file_id_conf[cat_conf == target_spp])
unconf_id = random.choice(file_id_unconf[cat_unconf == target_spp])
conf = Image.open(os.path.join(image_root, conf_id))
unconf = Image.open(os.path.join(image_root, unconf_id))

# %%
target_spp
# %%
conf
# %%
unconf
# %%
conf.save('./Figs/conf_unconf/{}_{}_{}_{}'.format(seed, 'conf', target_spp, conf_id.replace('/', ':::').replace('.JPG', '.PNG')))
unconf.save('./Figs/conf_unconf/{}_{}_{}_{}'.format(seed, 'unconf', target_spp, unconf_id.replace('/', ':::').replace('.JPG', '.PNG')))

# %%
class_indices_S2[0] = 'Ghost'
def load_list_eval(path):
    with open(path, 'r') as f:
        file_id_list = []
        pred_list = []
        cat_list = []
        for line in tqdm(f):
            line_sp = line.replace('\n', '').rsplit(' ', 2)
            file_id = line_sp[0]
            pred = -1 if int(line_sp[1]) == -1 else class_indices_S2[int(line_sp[1])]
            cat = -1 if int(line_sp[2]) == -1 else class_indices_S2[int(line_sp[2])]
            file_id_list.append(file_id)
            pred_list.append(pred)
            cat_list.append(cat)
    return (np.array(file_id_list), np.array(cat_list), np.array(pred_list))
# %%
file_id, preds, cats = load_list_eval(validation_path)

# %%
image_root = '/home/zhmiao/datasets/ecology/Mozambique'
wrong_ids = file_id[preds != cats]
wrong_preds = preds[preds != cats]
wrong_cats = cats[preds != cats]
correct_ids = file_id[preds == cats]
correct_preds = preds[preds == cats]

# %%
seed = random.choice(list(range(0, 50000)))
# seed = 5742
random.seed(seed)
wrong_index = random.choice(list(range(len(wrong_ids))))
correct_index = random.choice(list(range(len(correct_ids))))

wrong = Image.open(os.path.join(image_root, wrong_ids[wrong_index]))
wrong_p = wrong_preds[wrong_index]
wrong_cat = wrong_cats[wrong_index]
correct = Image.open(os.path.join(image_root, correct_ids[correct_index]))
correct_p = correct_preds[correct_index]

# %%
print(wrong_p)
print(wrong_cat)
wrong

# %%
print(correct_p)
correct

# %%
wrong.save('./Figs/wrong_correct/{}_{}_{}_{}_{}'
           .format(seed, 'wrong', wrong_p, wrong_cat,
                   wrong_ids[wrong_index].replace('/', ':::').replace('.JPG', '.PNG')))

# %%
correct.save('./Figs/wrong_correct/{}_{}_{}'
             .format(seed, 'correct', correct_p,
                     correct_ids[correct_index].replace('/', ':::').replace('.JPG', '.PNG')))


from flask import Flask, render_template, jsonify
import configs.main_config
from controllers import book_controller

app = Flask(__name__, template_folder=configs.main_config.APP.get("template_folder"))

app.register_blueprint(book_controller.books_blueprint, url_prefix='/api/books')

# www.localhost:8080 - vrati nam return z def home, pocuva len pre metody GET
@app.route('/', methods=['GET'])
def home():
    return render_template(
        '/home/index.html',
        title="Flask-Login Tutorial",
        body="You are logged in."
    )

@app.route('/score/<int:score>', methods=['GET'])
def score(score):
    # request.get()
    result = { "msg" : "", "your_score" : score}
    if score < 50:
        result["msg"] = "Zyour result is fail"
        return jsonify(result), 200
    return jsonify()

if __name__ == '__main__':
    app.run(configs.main_config.APP.get("host"), configs.main_config.APP.get("port"), configs.main_config.APP.get("debug"))


from django.http import HttpResponse
from django.urls import reverse_lazy, reverse
from django.shortcuts import render, redirect
from django.views.generic import FormView, ListView, DetailView, TemplateView, UpdateView
from django.views.generic.edit import DeleteView
from django.contrib import messages
from .forms import ReservaModelFrom
from .models import Reserva
from stands.models import Stand

class HomePage(TemplateView):
    template_name ="home.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['num_reservas'] = Reserva.objects.count()
        context['num_stands'] = Stand.objects.count()
        return context

class ReservaTemplateView(TemplateView):
    template_name = "reservas_home.html"

class ReservaFormView(FormView):
    template_name = "reservaform.html"
    form_class = ReservaModelFrom
    success_url = "cria_reserva"

    def form_valid(self, form):
        form.save(commit=True)
        messages.success(self.request, "Reserva salva com sucesso! :)")
        return super().form_valid(form)

    def form_invalid(self, form):
        messages.error(self.request, "Falha ao salvar a reserva! :(")
        return super().form_invalid(form)

class ReservaListView(ListView):
    model = Reserva
    paginate_by = 6
    template_name = "reservalist.html"

class ReservaDetailView(DetailView):
    model = Reserva
    template_name = "detail.html"

class ReservaDeleteView(DeleteView):
    model = Reserva
    template_name = 'confirm_delete.html'
    context_object_name = "object"
    success_url = reverse_lazy("reservas")
    success_message = "Reserva deletada com sucesso! :)"

    def delete(self, request, *args, **kwargs):
        messages.success(self.request, self.success_message)
        return super(ReservaDeleteView, self).delete(request, *args, **kwargs)

class ReservaUpdateView(UpdateView):
    model = Reserva
    fields = ["cnpj", "nome_empresa", "categoria_empresa", "quitado", "stand"]
    template_name = "reserva_update_form.html"
    template_name_suffix = "_update_form"
    context_object_name = "object"

    def get_success_url(self):
        messages.success(self.request, "Reserva atualizada com sucesso! :)")
        return reverse('detail', kwargs={'pk': self.object.pk})


import json
import os
import boto3
import logging
import json
import botocore.exceptions
import base64
import urllib
import time
from xml.dom import minidom

try:
    logger = logging.getLogger(__name__)
    logging.root.setLevel(os.environ.get("LOG_LEVEL", "INFO"))
    sm = boto3.client("sagemaker")
except Exception as e:
    print(f"Exception in initializing block: {e}")

try:
    user_profile_metadata = json.loads(os.environ.get("USER_PROFILE_METADATA", "{}"))
except Exception as e:
    print(f"Exception in loading user profile metadata: {e}")

HTTP_REDIRECT = 302
HTTP_EXCEPTION = 400
KEY_NAME_USER_ID = os.environ.get("KEY_NAME_USER_ID", "ssouserid")
KEY_NAME_TEAM_ID = os.environ.get("KEY_NAME_TEAM_ID", "teamid")

def get_xml(body):
    saml_response=urllib.parse.unquote(body.split("&")[0].split("=")[1])
    return base64.b64decode(saml_response)

def get_saml_attributes(saml_response_xml):
    return {
        e.attributes["Name"].value:e.getElementsByTagName("saml2:AttributeValue")[0].childNodes[0].nodeValue
        for e in minidom.parseString(saml_response_xml).getElementsByTagName("saml2:Attribute")
        }

def get_user_profile_name(user_id, team_id):
    return f"{user_id}-{team_id}"

def get_user_profile_metadata(team):
    """
    This function can be implemented as a microservice to return user and team metadata
    """
    return user_profile_metadata.get(team)

def create_presigned_domain_url(user_profile_name, metadata, expires_in_seconds=5):
    """
    This function can be implemented as a microservice to manage studio user profiles
    """
    user_profiles = sm.list_user_profiles(
        DomainIdEquals=metadata["DomainId"],
        UserProfileNameContains=user_profile_name)["UserProfiles"]

    if len(user_profiles) > 1:
        raise Exception(f"{user_profile_name} contains in more than one user profile for domain {metadata['DomainId']}")

    if not len(user_profiles):
        r = sm.create_user_profile(
            DomainId=metadata["DomainId"],
            UserProfileName=user_profile_name,
            Tags=metadata["Tags"],
            UserSettings=metadata["UserSettings"]
        )

        while sm.describe_user_profile(
            DomainId=metadata["DomainId"],
            UserProfileName=user_profile_name)["Status"] != "InService": time.sleep(3)

    return sm.create_presigned_domain_url(
                DomainId=metadata["DomainId"],
                UserProfileName=user_profile_name,
                SessionExpirationDurationInSeconds=int(metadata["SessionExpiration"]),
                ExpiresInSeconds=expires_in_seconds
            )["AuthorizedUrl"]

def lambda_handler(event, context):
    try:
        logger.info(json.dumps(event))

        body = event.get("body")
        if not body:
            raise Exception("No body key in the request")

        attr_dict = get_saml_attributes(get_xml(body))
        user_profile_name = get_user_profile_name(
            attr_dict[KEY_NAME_USER_ID],
            attr_dict[KEY_NAME_TEAM_ID]
            )

        logger.info(f"Got team {attr_dict[KEY_NAME_TEAM_ID]} and constructed user profile name={user_profile_name}")

        try:
            metadata = get_user_profile_metadata(attr_dict[KEY_NAME_TEAM_ID])
            if not metadata:
                raise Exception(f"no user profile metadata found for team {attr_dict[KEY_NAME_TEAM_ID]}")

            response = {
                "statusCode": HTTP_REDIRECT,
                "headers": {
                    "Location": create_presigned_domain_url(
                        user_profile_name,
                        metadata,
                        int(os.environ.get("PRESIGNED_URL_EXPIRATION", 5))
                    )
                },
                "isBase64Encoded": False
            }

        except botocore.exceptions.ClientError as ce:
            logger.error(f"ClientError exception in CreatePresignedDomainUrl: {ce}")
            response = {
                "statusCode": ce.response["ResponseMetadata"]["HTTPStatusCode"],
                "headers": {},
                "body":  ce.response["Error"]["Message"],
                "isBase64Encoded": False
            }
        except Exception as e:
            logger.error(f"Exception in CreatePresignedDomainUrl: {e}")
            response = {
                "statusCode": HTTP_EXCEPTION,
                "headers": {},
                "body": json.dumps(str(e)),
                "isBase64Encoded": False
            }

        return response

    except Exception as e:
        logger.error(f"Exception in function body: {e}")
        raise e


import tensorflow as tf
import numpy as np
import cv2
from scipy import misc
import random
import glob

# using pre-defined mask images
class DataLoader4_ldr_celebahq:
    def __init__(self, filename, im_size, batch_size, exact_decay, decay_steps, more_turns_to_more_masks,
                 specific_mask_number, mask_range, minimum_mask_number, curriculum_range, out2in):
        self.filename = filename
        self.filelist = open(filename, 'rt').read().splitlines()

        if not self.filelist:
            print(len(self.filelist))
            exit('\nError: file list is empty\n')

        self.len_files = len(self.filelist)
        self.ldr_shape = (256, 256, 3)
        self.im_size = im_size
        self.batch_size = batch_size
        self.data_queue = None

        self.masks_16 = [cv2.imread(m, cv2.IMREAD_GRAYSCALE) for m in glob.glob("./mask_images_16/mask16_*.png")]

        if exact_decay:
            self.number_of_masks_decay_steps = self.len_files
        else:
            self.number_of_masks_decay_steps = decay_steps

        self.more_turns_to_more_masks = more_turns_to_more_masks
        self.term_index = 0

        self.specific_mask_number = specific_mask_number
        self.mask_range = mask_range

        self.minimum_mask_number = minimum_mask_number

        self.curriculum_range = curriculum_range

        self.out2in = out2in

        # self.masklist = [n.replace("/data", "/mask") for n in self.filelist]
        self.masklist = []
        for n in self.filelist:
            if n.count("/data") > 1:
                print("please check data.py")
                exit(-1)
            self.masklist.append(n.replace("/data", "/mask").replace('.jpg', '.png'))

    def next(self, global_step, mask_curriculum):
        with tf.variable_scope('feed'):
            filelist_tensor = tf.convert_to_tensor(self.filelist, dtype=tf.string)
            masklist_tensor = tf.convert_to_tensor(self.masklist, dtype=tf.string)

            self.data_queue = tf.train.slice_input_producer([filelist_tensor, masklist_tensor], shuffle=True)

            im_gt = tf.image.decode_image(tf.read_file(self.data_queue[0]), channels=3)
            im_gt = tf.cast(im_gt, tf.float32)
            im_gt.set_shape(self.ldr_shape)

            celebahq_mask_tensor = tf.image.decode_image(tf.read_file(self.data_queue[1]), channels=1)
            celebahq_mask_tensor = tf.cast(celebahq_mask_tensor, tf.float32)
            celebahq_mask_tensor.set_shape(list(self.ldr_shape[:-1]) + [1])

            batch_celebahq = tf.train.batch([im_gt, celebahq_mask_tensor], batch_size=self.batch_size, num_threads=4)

            def get_mask_tensor(global_step):
                if len(self.mask_range) == 2:
                    number_of_masks = random.randrange(int(self.mask_range[0]), int(self.mask_range[1]) + 1)
                elif self.specific_mask_number > 0:
                    number_of_masks = self.specific_mask_number
                elif mask_curriculum:
                    if self.more_turns_to_more_masks:
                        if global_step > 100 * (self.term_index + 1) * (self.term_index + 1) + 900 * (
                                self.term_index + 1):
                            self.term_index = self.term_index + 1
                        number_of_masks = max(15 - self.term_index, self.minimum_mask_number)
                    else:
                        if self.out2in:
                            number_of_masks = min(self.minimum_mask_number + global_step // self.number_of_masks_decay_steps, 15)
                        else:
                            number_of_masks = max(15 - global_step // self.number_of_masks_decay_steps, self.minimum_mask_number)

                    if self.curriculum_range:
                        number_of_masks = random.randrange(number_of_masks, 16)
                else:
                    number_of_masks = 1
                random_regions = random.sample(self.masks_16, number_of_masks)
                # mask들 미리 만들면 다음 코드들이 필요 없어짐
                random_region = np.maximum.reduce(random_regions)
                random_region = random_region[None, :, :, None]
                random_region = random_region.astype(np.float32)
                return random_region

            mask_tensor = tf.py_func(get_mask_tensor, [global_step], tf.float32)
            mask_tensor.set_shape([1, self.im_size[0], self.im_size[1], 1])

        return batch_celebahq, mask_tensor