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

# ## Simple Graph:

# In[2]:

x = [1, 2, 3, 4, 5, 6, 7]
y = [50, 51 ,52, 38, 47, 49, 46]

# In[1]:

import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

# In[15]:

plt.xlabel("Day")
plt.ylabel("Temperature")
plt.title("Weather")
plt.plot(x,y, color="Green", linewidth=2) #, linestyle='dotted')
plt.grid()

# ## Legends and grids:

# In[16]:

days = x
max_t = [50, 51, 52, 48, 47, 49, 46]
min_t = [43, 42, 40, 44, 33, 35, 37]
avg_t = [45, 48, 48, 46, 40, 42, 41]
plt.title("Weather")
plt.xlabel("Days")
plt.ylabel("Temperature")
plt.plot(days, max_t, label="Max Temp")
plt.plot(days, min_t, label="Min Temp")
plt.plot(days, avg_t, label="Avg Temp")
plt.legend(shadow="True")
plt.grid()

# ## Vertical Bars:

# In[28]:

import numpy as np
company = ['GOOGL','AMZN','MSFT','FB']
revenue = [90, 136, 89, 27]
profit = [40, 2, 34, 12]
xpos = np.arange(len(company))
xpos

# In[35]:

plt.title("US Tech Stocks")
plt.xlabel("Companies")
plt.ylabel("Revenues")
plt.xticks(xpos, company)  # set the xticks labels (company names)
plt.bar(xpos-0.2, revenue, width=0.4, label="Revenue")
plt.bar(xpos+0.2, profit, width=0.4, label="Profit")
plt.legend()

# ## Horizontal Bars:

# In[38]:

plt.title("US Tech Stocks")
plt.ylabel("Companies")
plt.xlabel("Revenues")
plt.yticks(xpos, company)  # set the xticks labels (company names)
plt.barh(xpos-0.2, revenue, height=0.4, label="Revenue")
plt.barh(xpos+0.2, profit, height=0.4, label="Profit")
plt.legend()

# ## Histograms:

# In[57]:

blood_sugar_men = [113, 85, 90, 150, 149, 88, 93, 115, 135, 80, 77 ,82, 129]
blood_sugar_women = [67, 98, 89, 120, 133, 150, 84, 69, 89, 79, 120, 112, 100]

plt.xlabel('Sugar Range')
plt.ylabel('Total No of Patients')
plt.title('Blood Sugar Analysis')
plt.hist([blood_sugar_men, blood_sugar_women],
         bins=[80, 100, 125, 150],
         rwidth=0.95,
         color=['green', 'orange'],
         label=['Men', 'Women']),
        #orientation='horizontal')  # print results as buckets
plt.legend()
plt.show()

# ## Pie Chart:

# In[71]:

exp_vals = [1400, 600, 300, 410, 250]
exp_labels = ["Home Rent", "Food", "Phone/Internet Bill", "Car", "Other Utilities"]

plt.pie(exp_vals,
        labels=exp_labels,
        radius=1.5,
        autopct="%0.2f%%",
        shadow="True",
       explode=[0, 0.1, 0.1, 0, 0],
       startangle=45)

# ## Saving Graph to File:

# In[69]:

plt.savefig('piechart.png',
            bbox_inches="tight",
           pad_inches=2,
           transparent=True)  # must save to file before plt.show()!

plt.show()  # remove the matplotlib text prints


import traceback
import sys
import bredis
import utils
from config import creator

def creator_only(func):
    def isCreator(bot, update, *args, **kwargs):
        if update.message.from_user.id in creator:
            func(bot, update, *args, **kwargs)
        else:
            update.message.reply_text('who are you? my lordy told me never to talk to strangers... *runs away*', quote=False)
    return isCreator

###Creator Only
@creator_only
def add(bot, update, args):
    bredis.superadmin.add(args)
    chat_id = update.message.chat.id
    user_id = args[0]
    try:
        member = bot.get_chat_member(chat_id, user_id)
        name = member.user.first_name
        msg = "*shyly peeks out from behind lord* hi {} youre no longer a stranger"
    except:
        name = args[0]
        msg = "{} is now a superadmin"
    update.message.reply_text(msg.format(name), quote=False)

@creator_only
def adminsend(bot, update, args):
    for uid in bredis.superadmin.get():
        bot.sendMessage(uid, ' '.join(args).format(u=update.message.from_user.id))

@creator_only
def list_admins(bot, update, args):
    resp = []
    for uid in bredis.superadmin.get():
        name = bredis.user.name(uid)
        resp.append(str(name)+' '+str(uid))
    update.message.reply_text('\n'.join(resp))

def sAdmin_only(func):
    def isAdmin(bot, update, *args, **kwargs):
        adminId = update.message.from_user.id
        isAdmin = bredis.superadmin.check(adminId)
        args = []
        if isAdmin is True:
            func(bot, update, *args, **kwargs)
        else:
            update.message.reply_text('who are you? my lordy told me never to talk to strangers... *runs away*', quote=False)
    return isAdmin

###SuperAdmin Only
@sAdmin_only
def block_user(bot, update, args):
    blockedId = args[0]
    bredis.blocked.add(str(blockedId))
    update.message.reply_text('`{}` has been blocked from setting their own welcome'.format(blockedId), quote=False, parse_mode='Markdown')

@sAdmin_only
def list_blocked_users(bot, update):
    blockedUsers = bredis.blocked.get()
    resp = []
    for i  in blockedUsers:
        resp.append(str(i))
    update.message.reply_text('\n'.join(resp), quote=False, parse_mode='Markdown')

@sAdmin_only
def unblock_user(bot, update, args):
    blockedId = args[0]
    bredis.blocked.rem(str(blockedId))
    update.message.reply_text('`{}` has been unblocked'.format(blockedId), quote=False, parse_mode='Markdown')

@sAdmin_only
def send(bot, update, args):
    gid = args[0]
    del args[0]
    msg = ' '.join(args)
    try:
        bot.sendMessage(gid, msg, parse_mode='Markdown')
#    except:
    except Exception as e:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        msg = traceback.format_exception(exc_type, exc_value, exc_traceback)[-1]
        update.message.reply_text(msg, quote=False)


"""
- O(n) worst case time complexity

- Based on insertion sorted

- Efficient for medium size datasets
"""

def shell_sort(arr):

    inc = len(arr)//2

    while inc:
        for i, el in enumerate(arr):
            while i >= inc and arr[i - inc] > el:
                arr[i] = arr[i - inc]
                i -= inc
            arr[i] = el
        inc = 1 if inc == 2 else int(inc * 5.0 / 11)

A = [35, 33, 42, 10, 14, 19, 27, 44, 3, 4]
shell_sort(A)
print(A)


from django.shortcuts import redirect, render
from company.models import Company
from django.http import  HttpResponseRedirect, JsonResponse
from company.forms import CompanyCreationForm,DashboardForm
# Create your views here.

def create_company(request):
    if request.method == 'POST':
        form = CompanyCreationForm(request.POST or None,)
        if form.is_valid():
            obj=form.save(commit=False)
            obj.user = request.user
            obj.save()
            return redirect('dashboard', obj.identifier)
        else:
            form = CompanyCreationForm()
            context={
                'form':form
            }
            return render(request, 'company/create-company.html',context)
    else:
            form = CompanyCreationForm()
            context={
                'form':form
            }
            return render(request, 'company/create-company.html',context)

def dashboard(request,identifier):
    company= Company.objects.get(identifier = identifier)
    if request.method == 'POST':
        form = DashboardForm(request.POST or None, request.FILES or None ,instance=company)
        if form.is_valid():
            form.save()
            return redirect('companyprofile', company.identifier, company.name)
        else:
            company= Company.objects.get(identifier = identifier)
            form = DashboardForm(instance=company)
            return render(request, 'company/dashboard.html',{'profile':form,'company':company})
    else:
            company= Company.objects.get(identifier = identifier)
            form = DashboardForm(instance=company)
            context= {'profile':form,'company':company}
            return render(request, 'company/dashboard.html', context)

def company_profile(request,slug,name):
   company = Company.objects.get(identifier = slug)
   products= company.companyproduct.all()
   companysuggestions = Company.objects.exclude(identifier=slug)
   return render(request,'company/companyprofile.html', {'profile':company,'products':products,'companysuggestions':companysuggestions})

def follow_page(request,slug):
   company = Company.objects.get(identifier=slug)
   if request.user in company.pagefollowers.all():
        company.pagefollowers.remove(request.user)
        return JsonResponse({'success':'follow' })
   else:
        company.pagefollowers.add(request.user)
        return JsonResponse({'success':'unfollow' })


import os, sys, random
import numpy as np

from toolz import *
from toolz.curried import *

import torch
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
import torchvision.transforms as transforms

from .dataGen import dataGen

def makeDataLoader(config, flag):

    return DataLoader(dataGen(config, augument),
                      batch_size  = config.batchSize,
                      shuffle     = True if flag == 'train' else False,
                      num_workers = config.numWorkers,
                      collate_fn  = collateFn)

@curry
def collateFn(batch):

    """
    this function is added as an argument to the makeDataLoader function.
    this function handles how the datapoints for every batch are merged.
    this is required for our case becuase input has variable sizes.
    A remedy to this is to padd zero to short ones.

    batch :: [ imgs, diags, y ]
    imgs  :: [np.array(H,W)]
    diags :: [np.array([int])]
    y     :: int
    """

    batchSize = len(batch)

    N     = max( map(compose(len, first) )( batch) )
    C,H,W = batch[0][0][0].shape
    nDiag = len(batch[0][1][1])

    imgss   = torch.zeros(batchSize,N,C,H,W)
    diagss  = torch.zeros(batchSize,N,nDiag)
    ys      = torch.zeros(batchSize)

    for b, (imgs, diags, y) in enumerate(batch):

        for i, (img, diag) in enumerate(zip(imgs,diags)):
            imgss[b,i,...] = img
            diagss[b,i,...] = torch.tensor(diag)

        ys[b] = y

    return torch.tensor(imgss), torch.tensor(diagss), torch.tensor(ys)

@curry
def augument (flag, images) :

    """
    flag   : one of ["train","test","validate"]
    images : [np.array(H,W)]

    it is a simple augmentation fucntion that ,I admit, is imperfect.
    One key major drawback is that I resize the image to a much smaller one.
    However, following the works done by google. this does not give a bad performance.
    https://github.com/GoogleCloudPlatform/healthcare/tree/master/datathon/datathon_etl_pipelines
    """

    if flag == "train":
        return \
            transforms.Compose(
                [transforms.ToPILImage(),
                 transforms.ToTensor(),
                 transforms.Resize((320,320)),
                 transforms.Normalize((0), (1)),
                 transforms.RandomRotation((-5,5)),
                ])(images)

    else: # ["test","validate"]
        return \
            transforms.Compose(
                [transforms.ToPILImage(),
                 transforms.ToTensor(),
                 transforms.Resize((320,320)),
                 transforms.Normalize((0,0), (1,1)),
                ])(images)

if __name__ == "__main__":

    from easydict import EasyDict

    config = EasyDict()
    config.pklPath = "./joined.pkl"

    config.L = 72 # hours
    config.W = 24 # hours
    config.dataPath = "." #"./data"

    config.batchSize  = 4
    config.numWorkers = 16

    gen = dataGen(config, augument)

    batch = [gen.__getitem__(i) for i in range(6)]

    for x in makeDataLoader(config, "train"):
        print(x)


SQUARES_NAMES = (
    ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10'),
    ('B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10'),
    ('C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10'),
    ('D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10'),
    ('E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'E10'),
    ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'),
    ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10'),
    ('H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10'),
    ('I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9', 'I10'),
    ('J1', 'J2', 'J3', 'J4', 'J5', 'J6', 'J7', 'J8', 'J9', 'J10'),
)
EMPTY = 0
BLACK = 1
WHITE = 2

LETTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

SQUARES_CORD = {}
for i in range(len(SQUARES_NAMES)):
    for j in range(len(SQUARES_NAMES[i])):
        SQUARES_CORD[SQUARES_NAMES[i][j]] = (j, i)

class GameBoard:
    def __init__(self):
        self.board = {}
        self.empty_squares = 100
        self.last_move = []
        self.history_of_moves = ''
        self.next_color = WHITE
        self.possible_next_movies = []
        for y in range(10):
            for x in range(10):
                self.board[SQUARES_NAMES[y][x]] = EMPTY
                self.possible_next_movies.append(SQUARES_NAMES[y][x])

    def is_it_possible_move(self, color, square):
        # Is it this color turn?
        if self.next_color == color:
            # Is square empty?
            if self.board[square] == EMPTY:
                # Is it possible move?
                if square in self.possible_next_movies:
                    return True
                else:
                    # Is it freedom?
                    if len(self.possible_next_movies) == 0:
                        return True
                    else:
                        return False
            else:
                return False
        else:
            return False

    def put_piece(self, color, next_color, square_name):
        if self.is_it_possible_move(color, square_name):
            self.board[square_name] = color
            self.next_color = next_color
            self.possible_next_movies = []
            y = square_name[0]
            y_val = -1
            for iter_letters in range(len(LETTERS)):
                if LETTERS[iter_letters] == y:
                    y_val = iter_letters + 1
                    break
            x_val = int(square_name[1:])
            for x in range(x_val - 1, x_val - 1 + 3):
                for y in range(y_val - 1, y_val - 1 + 3):
                    if x in range(1, 11) and y in range(1, 11):
                        if self.board[SQUARES_NAMES[y - 1][x - 1]] == EMPTY:
                            self.possible_next_movies.append(SQUARES_NAMES[y - 1][x - 1])
            if color == BLACK:
                self.last_move = [square_name, 'black']
            elif color == WHITE:
                self.last_move = [square_name, 'white']
            self.history_of_moves += square_name + ' '
            return 'OK'
        else:
            return 'E1'

    def count_current_result(self, color):
        # Every square is counted in 4 ways: across, down and crosswise two times (| -- / \)
        points = 0
        for square in self.board:
            if self.board[square] == color:
                x = -1
                for iterator in range(10):
                    if square[0] == LETTERS[iterator]:
                        y = iterator
                x = int(square[1:])-1
                # across
                in_one_line = 1
                x_iter, y_iter = x+1, y  # x+1 because there is checked square in right side
                while x_iter < 10 and y_iter < 10:
                    # Reject when it is consecutive this color square
                    if 0 <= x - 1 < 10 and 0 <= y < 10:
                        if self.board[SQUARES_NAMES[y][x - 1]] == color:
                            break
                    if self.board[SQUARES_NAMES[y_iter][x_iter]] == color:
                        in_one_line += 1
                        x_iter += 1
                    else:
                        break
                if in_one_line == 4:
                    #print(square, '->', SQUARES_NAMES[y_iter][x_iter-1])
                    points += 1

                # down
                in_one_line = 1
                x_iter, y_iter = x, y+1  # y+1 because there is checked square in down side
                while x_iter < 10 and y_iter < 10:
                    # Reject when it is consecutive this color square
                    if 0 <= x < 10 and 0 <= y - 1 < 10:
                        if self.board[SQUARES_NAMES[y-1][x]] == color:
                            break
                    if self.board[SQUARES_NAMES[y_iter][x_iter]] == color:
                        in_one_line += 1
                        y_iter += 1
                    else:
                        break
                if in_one_line == 4:
                    #print(square, '->', SQUARES_NAMES[y_iter-1][x_iter])
                    points += 1

                # crosswise \
                in_one_line = 1
                x_iter, y_iter = x+1, y+1  # y+1 and x+1 because there is checked square in right-down side
                while x_iter < 10 and y_iter < 10:
                    # Reject when it is consecutive this color square
                    if 0 <= x - 1 < 10 and 0 <= y - 1 < 10:
                        if self.board[SQUARES_NAMES[y-1][x-1]] == color:
                            break
                    if self.board[SQUARES_NAMES[y_iter][x_iter]] == color:
                        in_one_line += 1
                        y_iter += 1
                        x_iter += 1
                    else:
                        break
                if in_one_line == 4:
                    #print(square, '->', SQUARES_NAMES[y_iter-1][x_iter-1])
                    points += 1

                # crosswise /
                in_one_line = 1
                x_iter, y_iter = x - 1, y + 1  # y+1 and x-1 because there is checked square in left-down side
                while 0 <= x_iter < 10 and y_iter < 10:
                    # Reject when it is consecutive this color square
                    if 0 <= x + 1 < 10 and 0 <= y - 1 < 10:
                        if self.board[SQUARES_NAMES[y - 1][x + 1]] == color:
                            break
                    if self.board[SQUARES_NAMES[y_iter][x_iter]] == color:
                        in_one_line += 1
                        y_iter += 1
                        x_iter -= 1
                    else:
                        break
                if in_one_line == 4:
                    #print(square, '->', SQUARES_NAMES[y_iter - 1][x_iter + 1])
                    points += 1

        return str(points)

    def reset(self):
        for square in self.board:
            self.board[square] = EMPTY

class GameHandler:
    game_board = GameBoard()

    @staticmethod
    def put_white(square_name):
        return GameHandler.game_board.put_piece(WHITE, BLACK, square_name)

    @staticmethod
    def put_black(square_name):
        return GameHandler.game_board.put_piece(BLACK, WHITE, square_name)

    @staticmethod
    def get_board():
        return GameHandler.game_board.board

    @staticmethod
    def get_possible_movies():
        return GameHandler.game_board.possible_next_movies

    @staticmethod
    def get_black_points():
        return GameHandler.game_board.count_current_result(BLACK)

    @staticmethod
    def get_white_points():
        return GameHandler.game_board.count_current_result(WHITE)


import numpy as np
import enum
import gym

from safety_gym.envs.engine import *
from mujoco_py import MjViewer, MujocoException, const, MjRenderContextOffscreen

DEFAULT_WIDTH = 100
DEFAULT_HEIGHT = 100

GROUP_ZONE = 7

class zone(enum.Enum):
    JetBlack = 0
    White    = 1
    Blue     = 2
    Green    = 3
    Red      = 4
    Yellow   = 5
    Cyan     = 6
    Magenta  = 7

    def __lt__(self, sth):
        return self.value < sth.value

    def __str__(self):
        return self.name[0]

    def __repr__(self):
        return self.name

class ZoneEnvBase(Engine):
    """
    This environment is a modification of the Safety-Gym's environment.
    There is no "goal circle" but rather a collection of zones that the
    agent has to visit or to avoid in order to finish the task.
    """
    def __init__(self, zones:list, config:dict={}):
        walled = config.pop("walled")
        world_extent = 3
        self.DEFAULT.update({
            'floor_display_mode': True,
            'observe_sensors': False,
            'observe_xy': True,
            'observe_zones': False,
            'zones_num': 0,  # Number of hazards in an environment
            'zones_placements': None,  # Placements list for hazards (defaults to full extents)
            'zones_locations': [],  # Fixed locations to override placements
            'zones_keepout': 0.55,  # Radius of hazard keepout for placement
            'zones_size': 0.2,  # Radius of hazards
            'placements_extents': [-world_extent, -world_extent, world_extent, world_extent]
        })

        if (walled):
            walls = [(i/10, j) for i in range(int(-world_extent * 10),int(world_extent * 10 + 1),1) for j in [-world_extent, world_extent]]
            walls += [(i, j/10) for i in [-world_extent, world_extent] for j in range(int(-world_extent * 10), int(world_extent * 10 + 1),1)]
            self.DEFAULT.update({
                'walls_num': len(walls),  # Number of walls
                'walls_locations': walls,  # This should be used and length == walls_num
                'walls_size': 0.1,  # Should be fixed at fundamental size of the world
            })

        self.zones = zones
        if not self.zone_types:
            self.zone_types = list(set(zones))
        self.zone_types.sort()
        self._rgb = {
            zone.JetBlack: [0, 0, 0, 0.25],
            zone.Blue    : [0, 0, 1, 0.25],
            zone.Green   : [0, 1, 0, 0.25],
            zone.Cyan    : [0, 1, 1, 0.25],
            zone.Red     : [1, 0, 0, 0.25],
            zone.Magenta : [1, 0, 1, 0.25],
            zone.Yellow  : [1, 1, 0, 0.25],
            zone.White   : [1, 1, 1, 0.25]
        }
        self.zone_rgbs = np.array([self._rgb[haz] for haz in self.zones])

        parent_config = {
            'robot_base': 'xmls/car.xml',
            'task': 'none',
            'lidar_num_bins': 16,
            'observe_zones': True,
            'zones_num': len(zones),
            'num_steps': 1000
        }
        parent_config.update(config)
        super().__init__(parent_config)

    @property
    def zones_pos(self):
        ''' Helper to get the zones positions from layout '''
        return [self.data.get_body_xpos(f'zone{i}').copy() for i in range(self.zones_num)]

    def build_observation_space(self):
        super().build_observation_space()

        if self.observe_zones:
            self.build_zone_observation_space()

        if self.observe_xy:
            self.obs_space_dict.update({'robot_pos': gym.spaces.Box(-np.inf, np.inf, (2,), dtype=np.float32)})
            self.obs_space_dict.update({'robot_dir': gym.spaces.Box(-np.inf, np.inf, (2,), dtype=np.float32)})
            self.obs_space_dict.update({'robot_velp': gym.spaces.Box(-np.inf, np.inf, (2,), dtype=np.float32)})
            self.obs_space_dict.update({'robot_velr': gym.spaces.Box(-np.inf, np.inf, (1,), dtype=np.float32)})

        if self.observation_flatten:
            self.obs_flat_size = sum([np.prod(i.shape) for i in self.obs_space_dict.values()])
            self.observation_space = gym.spaces.Box(-np.inf, np.inf, (self.obs_flat_size,), dtype=np.float32)
        else:
            self.observation_space = gym.spaces.Dict(self.obs_space_dict)

    def build_zone_observation_space(self):
        for zone_type in self.zone_types:
            self.obs_space_dict.update({f'zones_lidar_{zone_type}': gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32)})

    def build_placements_dict(self):
        super().build_placements_dict()

        if self.zones_num: #self.constrain_hazards:
            self.placements.update(self.placements_dict_from_object('zone'))

    def build_world_config(self):
        world_config = super().build_world_config()

        for i in range(self.zones_num):
            name = f'zone{i}'
            geom = {'name': name,
                    'size': [self.zones_size, 1e-2],#self.zones_size / 2],
                    'pos': np.r_[self.layout[name], 2e-2],#self.zones_size / 2 + 1e-2],
                    'rot': self.random_rot(),
                    'type': 'cylinder',
                    'contype': 0,
                    'conaffinity': 0,
                    'group': GROUP_ZONE,
                    'rgba': self.zone_rgbs[i] * [1, 1, 1, 1.]} #0.1]}  # transparent
            world_config['geoms'][name] = geom

        return world_config

    def obs(self):
        ''' Return the observation of our agent '''
        self.sim.forward()  # Needed to get sensordata correct
        obs = {}

        if self.observe_goal_dist:
            obs['goal_dist'] = np.array([np.exp(-self.dist_goal())])
        if self.observe_goal_comp:
            obs['goal_compass'] = self.obs_compass(self.goal_pos)
        if self.observe_goal_lidar:
            obs['goal_lidar'] = self.obs_lidar([self.goal_pos], GROUP_GOAL)
        if self.task == 'push':
            box_pos = self.box_pos
            if self.observe_box_comp:
                obs['box_compass'] = self.obs_compass(box_pos)
            if self.observe_box_lidar:
                obs['box_lidar'] = self.obs_lidar([box_pos], GROUP_BOX)
        if self.task == 'circle' and self.observe_circle:
            obs['circle_lidar'] = self.obs_lidar([self.goal_pos], GROUP_CIRCLE)
        if self.observe_freejoint:
            joint_id = self.model.joint_name2id('robot')
            joint_qposadr = self.model.jnt_qposadr[joint_id]
            assert joint_qposadr == 0  # Needs to be the first entry in qpos
            obs['freejoint'] = self.data.qpos[:7]
        if self.observe_com:
            obs['com'] = self.world.robot_com()
        if self.observe_sensors:
            # Sensors which can be read directly, without processing
            for sensor in self.sensors_obs:  # Explicitly listed sensors
                obs[sensor] = self.world.get_sensor(sensor)
            for sensor in self.robot.hinge_vel_names:
                obs[sensor] = self.world.get_sensor(sensor)
            for sensor in self.robot.ballangvel_names:
                obs[sensor] = self.world.get_sensor(sensor)
            # Process angular position sensors
            if self.sensors_angle_components:
                for sensor in self.robot.hinge_pos_names:
                    theta = float(self.world.get_sensor(sensor))  # Ensure not 1D, 1-element array
                    obs[sensor] = np.array([np.sin(theta), np.cos(theta)])
                for sensor in self.robot.ballquat_names:
                    quat = self.world.get_sensor(sensor)
                    obs[sensor] = quat2mat(quat)
            else:  # Otherwise read sensors directly
                for sensor in self.robot.hinge_pos_names:
                    obs[sensor] = self.world.get_sensor(sensor)
                for sensor in self.robot.ballquat_names:
                    obs[sensor] = self.world.get_sensor(sensor)
        if self.observe_remaining:
            obs['remaining'] = np.array([1. - self.steps / self.num_steps])
            assert 0.0 <= obs['remaining'][0] <= 1.0, 'bad remaining {}'.format(obs['remaining'])
        if self.walls_num and self.observe_walls:
            obs['walls_lidar'] = self.obs_lidar(self.walls_pos, GROUP_WALL)
        if self.observe_hazards:
            obs['hazards_lidar'] = self.obs_lidar(self.hazards_pos, GROUP_HAZARD)
        if self.observe_vases:
            obs['vases_lidar'] = self.obs_lidar(self.vases_pos, GROUP_VASE)
        if self.gremlins_num and self.observe_gremlins:
            obs['gremlins_lidar'] = self.obs_lidar(self.gremlins_obj_pos, GROUP_GREMLIN)
        if self.pillars_num and self.observe_pillars:
            obs['pillars_lidar'] = self.obs_lidar(self.pillars_pos, GROUP_PILLAR)
        if self.buttons_num and self.observe_buttons:
            # Buttons observation is zero while buttons are resetting
            if self.buttons_timer == 0:
                obs['buttons_lidar'] = self.obs_lidar(self.buttons_pos, GROUP_BUTTON)
            else:
                obs['buttons_lidar'] = np.zeros(self.lidar_num_bins)
        if self.observe_qpos:
            obs['qpos'] = self.data.qpos.copy()
        if self.observe_qvel:
            obs['qvel'] = self.data.qvel.copy()
        if self.observe_ctrl:
            obs['ctrl'] = self.data.ctrl.copy()
        if self.observe_vision:
            obs['vision'] = self.obs_vision()
        if self.observe_zones:
            self.obs_zones(obs)
        if self.observe_xy:
            obs['robot_pos'] = self.world.robot_pos()[:2] / 3.
            robot_quat = self.data.get_body_xquat('robot').astype(np.float32)
            obs['robot_dir'] = np.array([robot_quat[0]**2 - robot_quat[3]**2, 2 * robot_quat[0] * robot_quat[3]])
            obs['robot_velp'] = self.world.robot_vel()[:2] / 1.5
            obs['robot_velr'] = np.array([self.data.get_body_xvelr('robot').copy()[2]]) / 3.
        if self.observation_flatten:
            flat_obs = np.zeros(self.obs_flat_size)
            offset = 0
            for k in sorted(self.obs_space_dict.keys()):
                k_size = np.prod(obs[k].shape)
                flat_obs[offset:offset + k_size] = obs[k].flat
                offset += k_size
            obs = flat_obs

        assert self.observation_space.contains(obs), f'Bad obs {obs} {self.observation_space}'
        return obs

    def obs_zones(self, obs):
        for zone_type in self.zone_types:
            ind = [i for i, z in enumerate(self.zones) if (self.zones[i] == zone_type)]
            pos_in_type = list(np.array(self.zones_pos)[ind])
            obs[f'zones_lidar_{zone_type}'] = self.obs_lidar(pos_in_type, GROUP_ZONE)

    def render(self,
               mode='human',
               camera_id=None,
               width=DEFAULT_WIDTH,
               height=DEFAULT_HEIGHT
               ):
        ''' Render the environment to the screen '''

        if self.viewer is None or mode!=self._old_render_mode:
            # Set camera if specified
            if mode == 'human':
                self.viewer = MjViewer(self.sim)
                self.viewer.cam.fixedcamid = -1
                self.viewer.cam.type = const.CAMERA_FREE
            else:
                self.viewer = MjRenderContextOffscreen(self.sim)
                self.viewer._hide_overlay = True
                self.viewer.cam.fixedcamid = camera_id #self.model.camera_name2id(mode)
                self.viewer.cam.type = const.CAMERA_FIXED
            self.viewer.render_swap_callback = self.render_swap_callback
            # Turn all the geom groups on
            self.viewer.vopt.geomgroup[:] = 1
            self._old_render_mode = mode
        self.viewer.update_sim(self.sim)

        if camera_id is not None:
            # Update camera if desired
            self.viewer.cam.fixedcamid = camera_id

        # Lidar markers
        if self.render_lidar_markers:
            offset = self.render_lidar_offset_init  # Height offset for successive lidar indicators
            if 'box_lidar' in self.obs_space_dict or 'box_compass' in self.obs_space_dict:
                if 'box_lidar' in self.obs_space_dict:
                    self.render_lidar([self.box_pos], COLOR_BOX, offset, GROUP_BOX)
                if 'box_compass' in self.obs_space_dict:
                    self.render_compass(self.box_pos, COLOR_BOX, offset)
                offset += self.render_lidar_offset_delta
            if 'goal_lidar' in self.obs_space_dict or 'goal_compass' in self.obs_space_dict:
                if 'goal_lidar' in self.obs_space_dict:
                    self.render_lidar([self.goal_pos], COLOR_GOAL, offset, GROUP_GOAL)
                if 'goal_compass' in self.obs_space_dict:
                    self.render_compass(self.goal_pos, COLOR_GOAL, offset)
                offset += self.render_lidar_offset_delta
            if 'buttons_lidar' in self.obs_space_dict:
                self.render_lidar(self.buttons_pos, COLOR_BUTTON, offset, GROUP_BUTTON)
                offset += self.render_lidar_offset_delta
            if 'circle_lidar' in self.obs_space_dict:
                self.render_lidar([ORIGIN_COORDINATES], COLOR_CIRCLE, offset, GROUP_CIRCLE)
                offset += self.render_lidar_offset_delta
            if 'walls_lidar' in self.obs_space_dict:
                self.render_lidar(self.walls_pos, COLOR_WALL, offset, GROUP_WALL)
                offset += self.render_lidar_offset_delta
            if 'hazards_lidar' in self.obs_space_dict:
                self.render_lidar(self.hazards_pos, COLOR_HAZARD, offset, GROUP_HAZARD)
                offset += self.render_lidar_offset_delta
            if 'pillars_lidar' in self.obs_space_dict:
                self.render_lidar(self.pillars_pos, COLOR_PILLAR, offset, GROUP_PILLAR)
                offset += self.render_lidar_offset_delta
            if 'gremlins_lidar' in self.obs_space_dict:
                self.render_lidar(self.gremlins_obj_pos, COLOR_GREMLIN, offset, GROUP_GREMLIN)
                offset += self.render_lidar_offset_delta
            if 'vases_lidar' in self.obs_space_dict:
                self.render_lidar(self.vases_pos, COLOR_VASE, offset, GROUP_VASE)
                offset += self.render_lidar_offset_delta

            for zone_type in self.zone_types:
                if f'zones_lidar_{zone_type}' in self.obs_space_dict:
                    ind = [i for i, z in enumerate(self.zones) if (self.zones[i] == zone_type)]
                    pos_in_type = list(np.array(self.zones_pos)[ind])

                    self.render_lidar(pos_in_type, np.array([self._rgb[zone_type]]), offset, GROUP_ZONE)
                    offset += self.render_lidar_offset_delta

        # Add goal marker
        if self.task == 'button':
            self.render_area(self.goal_pos, self.buttons_size * 2, COLOR_BUTTON, 'goal', alpha=0.1)

        # Add indicator for nonzero cost
        if self._cost.get('cost', 0) > 0:
            self.render_sphere(self.world.robot_pos(), 0.25, COLOR_RED, alpha=.5)

        # Draw vision pixels
        if self.observe_vision and self.vision_render:
            vision = self.obs_vision()
            vision = np.array(vision * 255, dtype='uint8')
            vision = Image.fromarray(vision).resize(self.vision_render_size)
            vision = np.array(vision, dtype='uint8')
            self.save_obs_vision = vision

        if mode=='human':
            self.viewer.render()
        elif mode=='rgb_array':
            self.viewer.render(width, height)
            data = self.viewer.read_pixels(width, height, depth=False)
            self.viewer._markers[:] = []
            self.viewer._overlay.clear()
            return data[::-1, :, :]


"""CFFI build script for lightvolume library."""
from __future__ import print_function
import sys
import os.path
from cffi import FFI
ffibuilder = FFI()

root = os.path.abspath(os.path.dirname(__file__))
visibility = os.path.join(root, 'vendor', 'visibility')

# Use GLEW to load OpenGL extensions on Windows only for now
# Set the USE_GLEW=1 environment variable to force
USE_GLEW = (
    sys.platform == 'win32' or
    os.environ.get('USE_GLEW')
)

include_dirs = [root, visibility]
library_dirs = []

if sys.platform == 'win32':
    IS_64BIT = sys.maxsize > 2 ** 32

    extra_compile_args = ['/std:c++14']
    libraries = ['opengl32', 'glu32']
    if USE_GLEW:
        extra_compile_args += ['/DUSE_GLEW']
        libraries += ['glew32']

        glew_path = os.path.join(root, 'vendor', 'glew-2.1.0')
        include_dirs += [
            os.path.join(glew_path, 'include')
        ]
        library_dirs += [
            os.path.join(
                glew_path, 'lib', 'Release',
                'x64' if IS_64BIT else 'Win32'
            )
        ]
else:
    extra_compile_args = ['-std=c++14']
    libraries = ['GL', 'GLU']
    if USE_GLEW:
        extra_compile_args += ['-DUSE_GLEW']
        libraries += ['GLEW']

ffibuilder.set_source(
    "_lightvolume",
    r"""
    #include "lightvolume.h"
    """,
    libraries=libraries,
    sources=['lightvolume.cpp'],
    include_dirs=include_dirs,
    library_dirs=library_dirs,
    extra_compile_args=extra_compile_args,
)

with open('lightvolume.h', 'r') as f:
    hdr = f.read()
ffibuilder.cdef(hdr)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)


#!/usr/bin/env python3
import codecs
import os
import re
from itertools import chain

from setuptools import setup, find_packages

__NAME__ = "nldcsc"

# -*- Distribution Meta -*-

re_meta = re.compile(r"__(\w+?)__\s*=\s*(.*)")
re_doc = re.compile(r'^"""(.+?)"""')

def _add_default(m):
    attr_name, attr_value = m.groups()
    return ((attr_name, attr_value.strip("\"'")),)

def _add_doc(m):
    return (("doc", m.groups()[0]),)

def parse_dist_meta():
    """Extract metadata information from ``$dist/__init__.py``."""
    pats = {re_meta: _add_default, re_doc: _add_doc}
    here = os.path.abspath(os.path.dirname(__file__))
    with open(os.path.join(here, __NAME__, "__init__.py")) as meta_fh:
        distmeta = {}
        for line in meta_fh:
            if line.strip() == "# -eof meta-":
                break
            for pattern, handler in pats.items():
                m = pattern.match(line.strip())
                if m:
                    distmeta.update(handler(m))
        return distmeta

# -*- Extras -*-

MODULES = {
    "auth",
    "datatables",
    "flask_managers",
    "flask_middleware",
    "flask_plugins",
    "http_apis",
    "loggers",
    "sql_migrations",
}

# -*- Requirements -*-

def _strip_comments(l):
    return l.split("#", 1)[0].strip()

def _pip_requirement(req):
    if req.startswith("-r "):
        _, path = req.split()
        return reqs(*path.split("/"))
    return [req]

def _reqs(*f):
    if len(f) == 2:
        if os.getcwd().endswith("modules") and f[0] == "modules":
            f = [f[1]]
        if not os.getcwd().endswith("modules") and f[0] == ".":
            f = ("modules", f[1])
    return [
        _pip_requirement(r)
        for r in (
            _strip_comments(l)
            for l in open(os.path.join(os.getcwd(), "requirements", *f)).readlines()
        )
        if r
    ]

def reqs(*f):
    """Parse requirement file.

    Example:
        reqs('default.txt')          # requirements/default.txt
        reqs('modules', 'redis.txt')  # requirements/modules/redis.txt
        reqs('.', 'loggers.txt')  # requirements/modules/loggers.txt -> this is a reference in a requirements file
        like -r ./loggers.txt
    Returns:
        List[str]: list of requirements specified in the file.
    """
    return [req for subreq in _reqs(*f) for req in subreq]

def extras(*p):
    """Parse requirement in the requirements/modules/ directory."""
    return reqs("modules", *p)

def install_requires():
    """Get list of requirements required for installation."""
    return reqs("default.txt")

def extras_require():
    """Get map of all extra requirements."""
    module_requirements = {x: extras(x + ".txt") for x in MODULES}

    # add an 'all' value to install all requirements for all modules
    module_requirements["all"] = list(set(chain(*module_requirements.values())))

    return module_requirements

# -*- Long Description -*-

def long_description():
    try:
        return codecs.open("README.md", "r", "utf-8").read()
    except OSError:
        return "Long description error: Missing README.md file"

meta = parse_dist_meta()

setup(
    name=__NAME__,
    packages=find_packages(exclude=["tests", "test_data"]),
    version=meta["version"],
    description="Package with general devops code",
    long_description=long_description(),
    long_description_content_type="text/markdown",
    author=meta["author"],
    author_email="NLDCSC@invalid.com",
    url="https://github.com/NLDCSC/nldcsc",
    license="GNU General Public License v3.0",
    platforms=["any"],
    install_requires=install_requires(),
    extras_require=extras_require(),
    python_requires=">=3.10",
    include_package_data=True,
    project_urls={
        "Code": "https://github.com/NLDCSC/nldcsc",
    },
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3 :: Only",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Operating System :: OS Independent",
    ],
)


import torch

from torch.autograd import Variable

class Train:
  def __init__(self, model, dataloader, optimizer, criterion, schedular=None, l1_lambda=0):
    self.model = model
    self.dataloader = dataloader
    self.optimizer = optimizer
    self.schedular = schedular
    self.l1_lambda = l1_lambda
    self.criterion = criterion

  def run(self):
    self.model.train()
    train_acc = 0.0
    train_loss = 0.0

    for i, (images, labels) in enumerate(self.dataloader):
        cuda_avail = torch.cuda.is_available()
        if cuda_avail:
            images = Variable(images.cuda())
            labels = Variable(labels.cuda())

        # Clear all accumulated gradients
        self.optimizer.zero_grad()
        # Predict classes using images from the test set
        outputs = self.model(images)
        # Compute the loss based on the predictions and actual labels
        loss = self.criterion(outputs, labels)

        if self.l1_lambda > 0:
            reg_loss = 0.0
            for param in self.model.parameters():
                reg_loss += torch.sum(param.abs())
            loss += self.l1_lambda * reg_loss
        # Backpropagate the loss
        loss.backward()

        # Adjust parameters according to the computed gradients
        self.optimizer.step()

        _, prediction = torch.max(outputs.data, 1)

        train_acc += torch.sum(prediction == labels.data)

    train_acc = train_acc / 50000

    return train_acc

class Test:
  def __init__(self, model, dataloader):
    self.model = model
    self.dataloader = dataloader

  def run(self):
    self.model.eval()
    test_acc = 0.0
    with torch.no_grad():
      for i, (images, labels) in enumerate(self.dataloader):
        cuda_avail = torch.cuda.is_available()
        if cuda_avail:
          images = Variable(images.cuda())
          labels = Variable(labels.cuda())

            # Predict classes using images from the test set
        outputs = self.model(images)
        _, prediction = torch.max(outputs.data, 1)

        test_acc += torch.sum(prediction == labels.data)

    #Compute the average acc and loss over all 10000 test images
    test_acc = test_acc / 10000

    return test_acc

class ModelTrainer:
  def __init__(self, model, optimizer, train_loader, test_loader, criterion, schedular=None, batch_schedular=False, l1_lambda = 0):
    self.model = model
    self.schedular = schedular
    self.batch_schedular = batch_schedular
    self.optimizer = optimizer
    self.criterion = criterion
    self.train = Train(model, train_loader, optimizer, self.criterion, self.schedular if self.schedular and self.batch_schedular else None, l1_lambda)
    self.test = Test(model, test_loader)

  def run(self, epochs=10):
    for epoch in range(epochs):
        train_acc = self.train.run()
        test_acc = self.test.run()
        if self.schedular and not self.batch_schedular:
            self.schedular.step()
        print("Epoch {}, Train Accuracy: {} , Test Accuracy: {}".format(epoch, train_acc, test_acc))


#pg 222 guttag program
def int_to_str (i):
    digits = '0123456789'
    if i == 0:
        return '0'
    result = ''
    while i > 0:
        result = digits[i%10]+result
        i = i // 10
    return result
number = 56
value = int_to_str(number)
print(f"The string representation of this number is " + value)

MIT Intro to CS+Python/MyOwn/LogSearch.py


'''
    Crawling Exchange Rate from BOT and write into JSON format

    http://rate.bot.com.tw/xrt/quote/l6m/USD
    http://rate.bot.com.tw/xrt/quote/yyyy-mm-dd/USD/spot（for Intraday）

    @author: Double
'''
import requests
import time
from bs4 import BeautifulSoup
import os
import json

def get_web_page(url):
    time.sleep(2) # sleep 2s first
    try:
        resp = requests.get(url=url,)
        if resp.status_code != 200:
            print('Invalid url:', resp.url)
            return None
        else:
            return resp.text
    except requests.exceptions.ConnectionError:
        requests.status_codes = "Connection refused"
        print(requests.status_codes)

def get_contents(dom):
    soup = BeautifulSoup(dom, 'html.parser')
    outputs = []

    # Get period, currency, and title information
    infos = soup.find_all('div', 'chart-key-value')
    period = infos[0].find('div', 'value').string
    currency = infos[1].find('div', 'value').text.strip()
    title = infos[2].find('div', 'value').string.replace("本行", "臺灣銀行近半年")   # replace original wording

    # Get table title
    FXtable = soup.find('table', 'table table-striped table-bordered table-condensed table-hover')
    FXtableTrinTH = FXtable.find('thead').find_all('tr')
    date = FXtableTrinTH[0].find('th', 'noscript').string
    cash_Bid = '現金'+FXtableTrinTH[1].find_all('th')[0].string.lstrip('本行')
    cash_Ask = '現金'+FXtableTrinTH[1].find_all('th')[1].string.lstrip('本行')
    spot_Bid = '即期'+FXtableTrinTH[1].find_all('th')[2].string.lstrip('本行')
    spot_Ask = '即期'+FXtableTrinTH[1].find_all('th')[3].string.lstrip('本行')

    # Get quote information
    FXdata = []
    FXtableTrinTB = FXtable.find('tbody').find_all('tr')
    for data in FXtableTrinTB:
        QuoteDate = data.find_all('td')[0].string
        QuoteCash_Bid = data.find_all('td')[2].string
        QuoteCash_Ask = data.find_all('td')[3].string
        QuoteSpot_Bid = data.find_all('td')[4].string
        QuoteSpot_Ask = data.find_all('td')[5].string
        FXdata.append([QuoteDate, QuoteCash_Bid, QuoteCash_Ask, QuoteSpot_Bid, QuoteSpot_Ask])

    # Summary
    outputs.append({
        'title' : title,
        'currency' : currency,
        'period' : period,
        'fields' : [date, cash_Bid, cash_Ask, spot_Bid, spot_Ask],
        'data' : FXdata
        })
    return outputs

# Create a directory if that doesn't exist
def makedir(directory):
    if not os.path.isdir(directory):
        os.makedirs (directory)

print('--------------------------START Crawling Exchange Rate--------------------------')
# Set Currency would query
URL_Curs = ['USD', 'JPY', 'CNY']

# Crawl Beginning
for URL_Cur in URL_Curs:
    URL_6mFX = 'http://rate.bot.com.tw/xrt/quote/l6m/'
    FX_6m = get_web_page(URL_6mFX + URL_Cur)
    if FX_6m:
        result = get_contents(FX_6m)

        # Set directory and filename
        dir_FX = 'ExchangeRate'
        fname_FX = '/'+URL_Cur+'6m.json'
        makedir(dir_FX)

        # Write into JSON
        with open(dir_FX + fname_FX, 'w', encoding='utf-8') as res:
            json.dump(result, res, indent=2, sort_keys=False, ensure_ascii=False)
print('--------------------------END--------------------------')


from itertools import cycle
from typing import Tuple

from gemini_python import CqlDto, QueryMode, Operation
from gemini_python.history_store import HistoryStore
from gemini_python.query import (
    InsertQueryGenerator,
    SelectQueryGenerator,
    QueryGenerator,
)
from gemini_python.schema import Schema

class LoadGenerator:
    """Query generator selector according to schema and mode."""

    def __init__(
        self,
        schema: Schema,
        partitions: list[list[tuple]],
        history_store: HistoryStore,
        mode: QueryMode = QueryMode.WRITE,
    ):

        generators: list[QueryGenerator] = []
        assert len(schema.tables) == len(
            partitions
        ), "partitions were not generated for all tables. Should not happen."
        for table, partition_list in zip(schema.tables, partitions):
            if mode == QueryMode.WRITE:
                generators.append(InsertQueryGenerator(table=table, partitions=partition_list))
            elif mode == QueryMode.READ:
                generators.append(
                    SelectQueryGenerator(
                        table=table, partitions=partition_list, history_store=history_store
                    )
                )
            elif mode == QueryMode.MIXED:
                generators.append(InsertQueryGenerator(table=table, partitions=partition_list))
                generators.append(
                    SelectQueryGenerator(
                        table=table, partitions=partition_list, history_store=history_store
                    )
                )
            else:
                raise ValueError(f"Unsupported query mode: {mode}")
        self._query_generator = cycle(generators)

    def get_query(self) -> Tuple[Operation, CqlDto]:
        return next(next(self._query_generator))


#パーセンタイル値はs+nを作成する際に用いるnから算出する

import argparse
import numpy as np
from scipy.io.wavfile import read
from PyOctaveBand import octavefilter
import os
cwd=os.getcwd()

def get_args():
    parser=argparse.ArgumentParser()
    parser.add_argument('cleanfile',type=str)
    parser.add_argument('number_of_simulation',type=int,help='シミュレーション回数')
    parser.add_argument('percentile',type=float)
    args=parser.parse_args()
    return args

#rms:root-mean-square:二乗平均平方根　音圧平均を求める
def cal_rms(amp):
    return np.sqrt(np.mean(np.square(amp),axis=-1))

#任意のSN比となるように音源の音圧平均から騒音の音圧平均を求める
def cal_adjusted_noise_rms(clean_rms,snr):
    anr=clean_rms/(10**(float(snr)/20))
    return anr

def generate_pink_noise(noise_length):
    wn = np.random.normal(size=noise_length)
    wn_f = np.fft.rfft(wn) #ホワイトノイズのフーリエスペクトル
    amp = np.abs(wn_f) #振幅スペクトル（-3dB/Octにする）
    phase = np.angle(wn_f) #位相スペクトル（保持する）
    for f in range(amp.size):
        if f > 0 : # f = 0は対数を取れないのでスキップ
            amp[f] *= 10 ** ( ( -3 * np.log2( f / 2 ) ) / 20 ) #振幅スペクトルを-3dB/Oct
    pn_f = amp * np.exp( 1j * phase ) #振幅スペクトルと位相スペクトルからフーリエスペクトルを作成
    return np.fft.irfft( pn_f ) #時間軸に戻す

def apply_Aweighting(wave, fs):
    def calc_r(f):
        r = 12194**2 * f**4 / ( \
        (f**2 + 20.6**2) * \
        np.sqrt(f**2 + 107.7**2) * \
        np.sqrt(f**2 + 737.9**2) * \
        (f**2 + 12194**2) )
        return r
    #rfftした時の周波数軸を作る
    freqs = np.arange(4097) * fs / (4096 * 2)
    #4097個分の振幅を入れる箱を作っておく
    r_amp = np.zeros(4097)
    #周波数一つ一つを見ていく
    for i, f in enumerate(freqs):
        #f=0の時は除外する
        if f > 0:
            #i番目の振幅変化量＝周波数fにおけるcalc_r(f)
            r_amp[i] = calc_r(f)
    #周波数が1000の時のrを基準とする
    r_amp /= calc_r(1000)
    #r_ampをirfft
    fil_a = np.fft.irfft(r_amp)
    #irfft結果の前半と後半を入れ替える
    fil_a = np.append(fil_a[4096:8192],fil_a[0:4096])
    wave_a = np.convolve(wave, fil_a)
    return wave_a[4096 : 4096 + wave.size]

def Leq(t_list,data,wleng):
    #窓関数の数
    loop_size=int(np.floor(len(data)/wleng))
    #Leqを代入する箱
    leq_array=np.zeros([len(t_list),loop_size])
    idx=0
    for i in t_list:
        for j in range(loop_size):
            sample=i[j*wleng:j*wleng+wleng]
            leq=10*np.log10(np.mean(sample*sample))
            leq_array[idx,j]=leq
        idx+=1
    return leq_array

def Gradient(leq_array,data,wleng,sr):
    loop_size=int(np.floor(len(data)/wleng)) #窓関数の数
    gradient_array=np.zeros([len(leq_array),loop_size-4]) #代入用零行列の生成
    x=np.array(range(5))/(wleng/sr)
    x_mean=np.mean(x)
    x_var=np.sum((x-x_mean)**2)/5 #xの分散
    idx=0
    for i in leq_array:
        for j in range(loop_size-4):
            y=i[j:j+5]
            y_mean=np.mean(y)
            xy_var=np.sum((x-x_mean)*(y-y_mean))/5 #xyの共分散
            gradient=np.abs(xy_var/x_var) #傾きは絶対値を取る
            gradient_array[idx,j]=gradient
        idx+=1
    return gradient_array

if __name__=='__main__':

    #引数を解決
    args=get_args()
    cleanfile=args.cleanfile
    percentile=args.percentile
    number_of_simulation=args.number_of_simulation

    #読み込み
    sr,s=read(cleanfile)
    if (s.ndim>1):
        s=s[:,1]
    if (s.size % 2 !=0):
        s=np.append(s,[0])

    #sにA特性で重みづけし、音圧二乗平均を求める
    sA=apply_Aweighting(s,sr)
    sA_rms=cal_rms(sA)

    '''
    A特性について整理
    まず、もと音源のSとNがそれぞれある。
    その次に、SとNそれぞれにA特性をかける（これが普段聞いている音と似た音になる）。
    A特性がかかったSとNの音圧の2乗の平均の比に対して対数を取って10を掛ける事でSN比（㏈）が求まる。

    '''
    #1つのSN比での音声識別可能時間率の平均値と標準偏差を記録する箱
    total_time_ratio_mean=np.zeros(6)
    total_time_ratio_sd=np.zeros(6)
    SNR_box=np.zeros(6)

    #Rcmdr用のファイル
    f=open(f"{cwd}\\recordファイル\\record_gradient_1_p={str(percentile)}.txt","a",encoding="UTF-8")
    f.write("total_time_ratio SNR\n")

    #SN比の指定
    for a in range(6):
        SNR=(-5)*(a+1)
        SNR_box[a]=SNR

        #number_of_simulationの数だけ時間率を計算
        total_time_ratio_record=np.zeros(number_of_simulation)
        for nth_time in range(number_of_simulation):

            #同じサンプル数のノイズを作る
            n1=generate_pink_noise(len(s))

            #n1にA特性を掛ける
            n1A=apply_Aweighting(n1,sr)

            #n1Aの音圧2乗平均を求める
            n1A_rms=cal_rms(n1A)

            #SNRから騒音の音圧2乗平均を求める
            nA_rms=cal_adjusted_noise_rms(sA_rms,SNR)

            #SNRに合う騒音の作成
            n=n1*(nA_rms/n1A_rms)

            #騒音付加音声
            mix=s+n

            #正規化
            nmax=np.max(np.abs(n))
            mixmax=np.max(np.abs(mix))
            n=32767*n/np.max([nmax,mixmax])
            mix=32767*mix/np.max([nmax,mixmax])

            #基準化した音源をバンドパスフィルタに通す
            f_n_list,cf,t_n_list=octavefilter(x=n,fs=sr,fraction=3,order=6,limits=[90,11000],show=False,sigbands=True)
            f_mix_list,cf,t_mix_list=octavefilter(x=mix,fs=sr,fraction=3,order=6,limits=[90,11000],show=False,sigbands=True)

            #窓関数1つあたりのデータ数
            wleng=int(0.025*sr)

            #Leqの計算
            n_leq=Leq(t_n_list,n,wleng)
            mix_leq=Leq(t_mix_list,mix,wleng)

            #回帰直線の計算
            n_gradient=Gradient(n_leq,n,wleng,sr)
            mix_gradient=Gradient(mix_leq,mix,wleng,sr)

            #閾値の計算
            n_threshold=np.zeros(len(n_gradient))
            idx=0
            for one_array in n_gradient:
                n_threshold[idx]=np.percentile(one_array,percentile)  #バンドごとの閾値の設定
                idx+=1

            #各帯域で閾値とmix_gradientの比較,点数加算
            time_ratio_box=np.zeros(len(mix_gradient))
            idx=0
            for one_array in mix_gradient: #バンドごとで比較
                score=0
                for one_gradient in one_array: #mixの回帰直線の傾きとnの傾きの閾値を比較
                    if one_gradient>n_threshold[idx]:
                        score+=1 #傾きが大きければ加点
                time_ratio_box[idx]=score/len(mix_gradient[0]) #合計得点を計算回数で割った時間率を記録
                idx+=1

            #各バンドの時間率の平均を算出（音声識別可能時間率を算出）
            total_time_ratio=np.mean(time_ratio_box)
            #音声識別可能時間率をひとつずつ書き出し
            f.write(f"{str(total_time_ratio)} {str(SNR)}\n")
            #1つのSN比で算出した音声識別可能時間率を全て記録
            total_time_ratio_record[nth_time]=total_time_ratio

        #1つのSN比での音声識別可能時間率の平均値、標準偏差を計算し記録　図の作成に用いる
        total_time_ratio_mean[a]=np.mean(total_time_ratio_record)
        total_time_ratio_sd[a]=np.std(total_time_ratio_record)
        f.close

    #グラフ用ファイルの作成
    g=open(f"{cwd}\\forgraph保存\\forgraph_gradient_1_p={percentile}.txt","a",encoding="UTF-8")
    for SNR in SNR_box:
        g.write(f"{str(SNR)} ")
    g.write("\n")
    for mean in total_time_ratio_mean:
        g.write(f"{str(mean)} ")
    g.write("\n")
    for sd in total_time_ratio_sd:
        g.write(f"{str(sd)} ")
    g.close()


"""
Datasets for cropnet
"""

import argparse
import csv
import cv2
import logging
import numpy as np
import os
import platform
import torch
import torchvision as tv

from collections import OrderedDict
from PIL import Image
from skimage.transform import resize

# pytorch imports
from torch.utils.data import Dataset
from torch.utils.data import DataLoader

# ml_utils imports

# local imports
from utils import get_chip_bbox, load_tb_chips, make_clut, transform_cdl

logging.getLogger("PIL").setLevel(logging.WARNING)
logging.getLogger("Image").setLevel(logging.WARNING)
logging.getLogger("PIL.Image").setLevel(logging.WARNING)

pe = os.path.exists
pj = os.path.join
HOME = os.path.expanduser("~")
if platform.node() == "matt-XPS-8900":
    DATA = HOME
else:
    DATA = "/media/data"

# 224x224 patches to feed into pre-trained ResNet, classifying whole patch
class RGBPatches(Dataset):
    def __init__(self, data_dir_or_file, labels_dir_or_file, cats_dict={},
            ingest_patch_size=224, mode="test"):
        self._cats_dict = cats_dict
        self._cats = [3, 4, 5, 190] # TODO--just estimated from a single chip
        self._data_dir = None
        self._data_img = None
        self._img_ht = None
        self._img_wd = None
        self._ingest_patch_size = ingest_patch_size
        self._label_mode = None
        self._labels_dir = None
        self._labels_img = None
        self._mode = mode

        if os.path.isdir(data_dir_or_file):
            self._data_dir = data_dir_or_file
            raise NotImplementedError()
        else:
            self._load_rgb(data_dir_or_file)

        if os.path.isdir(labels_dir_or_file):
            self._data_dir = labels_dir_or_file
            raise NotImplementedError()
        else:
            self._load_labels(labels_dir_or_file)

    def __getitem__(self, index):
        i = index % (self._img_ht // self._ingest_patch_size)
        j = index // (self._img_ht // self._ingest_patch_size)

        sz = self._ingest_patch_size
        if self._mode=="train":
            io,jo = (torch.rand(2).data - 0.5) / 2.0
            i_beg = i * sz + io*sz
            j_beg = j * sz + jo*sz
            i_beg = int( i_beg.clamp(0.0, self._img_ht - sz) )
            j_beg = int( j_beg.clamp(0.0, self._img_wd - sz) )
        else:
            i_beg = i * sz
            j_beg = j * sz
        i_end = i_beg + sz
        j_end = j_beg + sz
        patch = self._data_img[i_beg:i_end, j_beg:j_end]
        patch = np.copy(patch)
        for k in range(patch.shape[2]):
            c = patch[:,:,k]
            patch[:,:,k] = (c - np.min(c)) / (np.max(c) - np.min(c))
        raw_label = self._labels_img[i_beg:i_end, j_beg:j_end]
        label = np.copy(raw_label)

        bgnd_mask = np.ones_like(label) > 0
        ct = 1
#        for k,v in self._cats_dict.items():
        for k in self._cats:
            mask_k = raw_label==k
            label[mask_k] = ct
            ct += 1
            bgnd_mask[mask_k] = False
        label[bgnd_mask] = 0

        label = torch.LongTensor([label]).squeeze()
        patch = Image.fromarray( np.uint8(patch*255) )
        transform = tv.transforms.ToTensor()
        return transform(patch),label

    def __len__(self):
        return (self._img_ht // self._ingest_patch_size) \
                    * (self._img_wd // self._ingest_patch_size)

    def get_cats_dict(self):
        return self._cats_dict

    def get_num_cats(self):
        return len(self._cats) + 1

    def _load_labels(self, data_npy_file):
        print("Loading labels file %s ..." % (data_npy_file))
        self._labels_img = np.load(data_npy_file)
        if self._labels_img is None:
            raise RuntimeError("Image file %s does not exist or is not a valid"\
                    " image")
        print("...Done.  Shape is %s" % repr(self._labels_img.shape))

    def _load_rgb(self, data_npy_file):
        print("Loading rgb file %s ..." % (data_npy_file))
        self._data_img = np.load(data_npy_file)
        if self._data_img is None:
            raise RuntimeError("Image file %s does not exist or is not a valid"\
                    " image")
        self._img_ht,self._img_wd = self._data_img.shape[:2]
        print("...Done.  Shape is %s" % repr(self._data_img.shape))

# 224x224 patches to feed into pre-trained ResNet, classifying center pixel
class RGBPatchesCenter(Dataset):
    def __init__(self, data_dir_or_file, labels_dir_or_file, cats_dict={},
            upsample=2, ingest_patch_size=224):
        self._cats_dict = cats_dict
        self._data_dir = None
        self._data_img = None
        self._img_ht = None
        self._img_wd = None
        self._ingest_patch_size = ingest_patch_size
        self._labels_dir = None
        self._labels_img = None
        self._pad = None
        self._upsample = upsample

        self._pad = self._ingest_patch_size // self._upsample

        if os.path.isdir(data_dir_or_file):
            self._data_dir = data_dir_or_file
            raise NotImplementedError()
        else:
            self._load_rgb(data_dir_or_file)

        if os.path.isdir(labels_dir_or_file):
            self._data_dir = labels_dir_or_file
            raise NotImplementedError()
        else:
            self._load_labels(labels_dir_or_file)

        self._check_dims()

    def __getitem__(self, index):
        i = self._pad + index % self._img_ht  # Column major
        j = self._pad + index // self._img_ht
        i_beg = i - self._pad
        i_end = i + self._pad
        j_beg = j - self._pad
        j_end = j + self._pad
        raw_patch = self._data_img[i_beg:i_end, j_beg:j_end]
        resize_dims = (self._ingest_patch_size, self._ingest_patch_size)
        patch = cv2.resize(raw_patch,resize_dims,interpolation=cv2.INTER_CUBIC)
        patch = np.transpose(patch, (2,0,1))
        label = self._labels_img[i_beg][j_beg]
        label = 0 if label not in self._cats_dict \
                else self._cats_dict[label]
        label = torch.LongTensor([label]).squeeze()
        return patch,label

    def __len__(self):
        return self._img_ht * self._img_wd

    def get_cats_dict(self):
        return self._cats_dict

    def _check_dims(self):
        label_h,label_w = self._labels_img.shape[:2]
        h,w = self._data_img.shape[:2]
        if label_h+2*self._pad != h or label_w+2*self._pad != w:
            raise RuntimeError("Inconsistent data, label matrices; (%d, %d) " \
                    "with total padding=%d vs. (%d, %d)." \
                    % (h, w, 2*self._pad, label_h, label_w))

    def _load_labels(self, data_npy_file):
        self._labels_img = np.load(data_npy_file)
        if self._labels_img is None:
            raise RuntimeError("Image file %s does not exist or is not a valid"\
                    " image")

    def _load_rgb(self, data_npy_file):
        self._data_img = np.load(data_npy_file)
        if self._data_img is None:
            raise RuntimeError("Image file %s does not exist or is not a valid"\
                    " image")
        self._img_ht,self._img_wd = self._data_img.shape[:2]
        pad_shape = ((self._pad, self._pad), (self._pad, self._pad), (0,0))
        self._data_img = np.pad(self._data_img/256, pad_shape, mode="reflect")

# Time-Band Chips, where Band is spectral band
class TBChips(Dataset):
    def __init__(self, data_dir_or_file, src_image_x=0, src_image_y=0,
            src_image_size=500):
        self._data_dir = None
        self._data_npy_file = None
        self._N = None
        self._src_image_x = src_image_x
        self._src_image_y = src_image_y
        self._src_image_size = src_image_size
        self._tb_chips = None # We're going to hold these in memory for now

        if os.path.isdir(data_dir_or_file):
            self._data_dir = data_dir_or_file
            self._get_tb_chips()
        else:
            self._load_tb_chips_from_npy(data_dir_or_file)

    def __getitem__(self, index):
        i = index // self._src_image_size
        j = index % self._src_image_size
        tb_chip = np.squeeze( self._tb_chips[:,:,i,j] )
        tb_chip = torch.FloatTensor(tb_chip).unsqueeze(0)
        return tb_chip,tb_chip

    def __len__(self):
        return self._N

    def get_data(self):
        return self._tb_chips

    def get_image_bbox(self):
        x0,y0 = self._src_image_x,self._src_image_y
        x1,y1 = x0+self._src_image_size,y0+self._src_image_size
        return (x0,y0,x1,y1)

    def _get_tb_chips(self):
        src_bbox = get_chip_bbox(self._src_image_x, self._src_image_y,
                self._src_image_size)
        self._tb_chips = load_tb_chips(self._data_dir, src_bbox)
        # TODO load_tb_chips does virtually exactly what the version here does,
        # just loads the npy file directly.  Doesn't create it
        self._N = self._tb_chips.shape[2] * self._tb_chips.shape[3]

    def _load_tb_chips_from_npy(self, data_file):
        full_tb_chips = np.load(data_file)
        x0,y0,x1,y1 = self.get_image_bbox()
        self._tb_chips = full_tb_chips[:, :, y0:y1, x0:x1]
        self._N = self._tb_chips.shape[2] * self._tb_chips.shape[3]

def _test_normalize_feats(features, do_copy=False):
    if len(features.shape) != 3:
        raise RuntimeError("Expecting features input to have 3 dimensions")
    if do_copy:
        features = np.copy(features)
    for k in range(features.shape[2]):
        c = features[:,:,k]
        features[:,:,k] = (c - np.min(c)) / (np.max(c) - np.min(c))
    return features

def _test_main(args):
    if not pe(args.output_supdir):
        os.makedirs(args.output_supdir)
    output_dir = pj(args.output_supdir, args.test)
    if not pe(output_dir):
        os.makedirs(output_dir)

    if args.test == "RGBPatches":
        print("Testing RGBPatches...")
        cats_dict = make_clut()
        dataset = RGBPatches(args.data_dir_or_file, args.labels_dir_or_file,
                mode="train", cats_dict=cats_dict)
        num_cats = dataset.get_num_cats()
        inc = 256 // num_cats
        print("The length of %s is %d." % (args.test, len(dataset)))
        N = len(dataset) if args.num_outputs<0 else args.num_outputs
        for i in range(N):
            patch,label = dataset[i % len(dataset)]
            patch = np.transpose( patch.cpu().data.numpy(), (1,2,0) )
            label = label.cpu().data.numpy() * inc
            print("Patch %i range, (%f, %f); label %i range, (%i, %i)" \
                    % (i, np.min(patch), np.max(patch), i, np.min(label),
                        np.max(label)))
            print("\tShape: %s" % repr(patch.shape))
            patch = _test_normalize_feats(patch, do_copy=True)
            patch = (patch * 255.0).astype(np.uint8)
            cv2.imwrite(pj(output_dir, "patch_%03d.png" % (i)), patch)
            cv2.imwrite(pj(output_dir, "label_%03d.png" % (i)), label)
        print("...Done")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--test", type=str, default="RGBPatches",
            choices=["RGBPatches"])
    parser.add_argument("-d", "--data-dir-or-file", type=str,
            default=pj(HOME, "Training/cropnet/sessions/session_07/feats.npy"))
    parser.add_argument("-l", "--labels-dir-or-file", type=str,
            default=pj(DATA, "Datasets/HLS/test_imgs/cdl/" \
                    "cdl_2016_neAR_0_0_500_500.npy"))
    parser.add_argument("-o", "--output-supdir", type=str,
            default=pj(HOME, "Training/cropnet/test_out"))
    parser.add_argument("-n", "--num-outputs", type=int, default=20)
    args = parser.parse_args()
    _test_main(args)


from rest_framework import serializers
from .models import WishList, WishListItem
from products.serializers import ProductSerializer

class WishListItemSerializer(serializers.ModelSerializer):

  class Meta:
        model = WishListItem
        fields = [
            "id",
            "product",
            "created_at",
            "updated_at",
        ]

class WishListSerializer(serializers.ModelSerializer):
    items = WishListItemSerializer(many=True)
    class Meta:
        model = WishList
        fields = [
            "id",
            "owner",
            "items",
            "created_at",
            "updated_at",
        ]


from decimal import Decimal
from django.db.models import Q, Sum
import finance_core.models

def build_table_annotate(line_items, primary_field, secondary_field, primary_axis, secondary_axis, ):
    # Setup
    arcprimary = {}
    arcsecondary = {}
    table = []
    zero = Decimal('0.00')
    for num, (pk, label, qobj, ) in enumerate(primary_axis):
        arcprimary[pk] = num
        table.append([zero]*len(secondary_axis))
    for num, (pk, label, qobj, ) in enumerate(secondary_axis):
        arcsecondary[pk] = num

    def lineitem_total(obj):
        if obj['amount__sum'] is None: return zero
        else: return obj['amount__sum']

    # Do the real work
    results = line_items.values(primary_field, secondary_field,).annotate(Sum('amount'))
    for result in results:
        pkey = arcprimary[result[primary_field]]
        skey = arcsecondary[result[secondary_field]]
        assert table[pkey][skey] == zero
        table[pkey][skey] = lineitem_total(result)

    return table

def build_table_aggregate(line_items, primary_field, secondary_field, primary_axis, secondary_axis):
    # This uses a simpler but probably slower method
    zero = Decimal('0.00')
    def total_amount(queryset):
        amount = queryset.aggregate(Sum('amount'))['amount__sum']
        if amount is None: return zero
        else: return amount
    table = [ # Primary axis
            [ # Secondary axis
                total_amount(line_items.filter(primary[2], secondary[2]))
            for secondary in secondary_axis]
        for primary in primary_axis]

    return table

build_table = build_table_annotate

def get_primary_axis(slug, base_area, term, ):
    if slug in axes and axes[slug][3]:
        return axes[slug][0:2] + (axes[slug][2](base_area, term, ), )
    else:
        raise NotImplementedError

def get_secondary_axis(slug, base_area, term, ):
    if slug in axes and axes[slug][4]:
        return axes[slug][0:2] + (axes[slug][2](base_area, term, ), )
    else:
        raise NotImplementedError

def get_budget_areas(base_area, term, ):
    base_area_depth = base_area.depth
    areas = base_area.get_descendants()
    if term:
        areas = areas.filter(Q(always=True) | Q(budget_term=term))
    axis = [
        (
            area.pk,
            area.indented_name(base_area_depth),
            Q(budget_area=area),
        )
        for area in areas
    ]
    return axis

def get_budget_terms(base_area, term, ):
    if term:
        terms = finance_core.models.BudgetTerm.objects.filter(pk=term.pk)
    else:
        terms = finance_core.models.BudgetTerm.objects.all()
    axis = [
        (
            term.pk,
            term.name,
            Q(budget_term=term),
        )
        for term in terms
    ]
    return axis

def get_layers(base_area, term, ):
    axis = [
        (
            finance_core.models.layer_num(layer),
            finance_core.models.layer_name(layer),
            Q(layer=finance_core.models.layer_num(layer)),
        )
        for layer in finance_core.models.layers
    ]
    return axis

axes = {
    'budget-areas': ('Budget Areas', 'budget_area', get_budget_areas, True,  True,  ),
    'budget-terms': ('Budget Terms', 'budget_term', get_budget_terms, True,  True,  ),
    'layers':       ('Layers',       'layer',       get_layers,       True,  True,  ),
}

def append_totals(table):
    # Row totals
    for row in table:
        row.append(sum(row))
    # Column totals
    if len(table) > 0:
        totalrow = [None]*len(table[0])
        for col in xrange(len(table[0])):
            totalrow[col] = sum([row[col] for row in table])
        table.append(totalrow)


from flask import request, g, Blueprint, json, Response
from ..shared.Authentication import Auth
from ..models.ClusterModel import ClusterModel, ClusterSchema

cluster_api = Blueprint('cluster_api', __name__)
cluster_schema = ClusterSchema()

@cluster_api.route('/', methods=['POST'])
@Auth.auth_required
def create():
  """
  Create Cluster Function
  """
  req_data = request.get_json()
  data, error = cluster_schema.load(req_data)
  if error:
    return custom_response(error, 400)
  cluster = ClusterModel(data)
  cluster.save()
  data = cluster_schema.dump(cluster).data
  return custom_response(data, 201)

"""
@cluster_api.route('/<int:cluster_id>', methods=['DELETE'])
@Auth.auth_required
def delete(cluster_id):
  post = ClusterModel.get_one_article(Cluster_id)
  if not post:
    return custom_response({'error': ' not found'}, 404)
  data = cluster_schema.dump(post).data
  if data.get('owner_id') != g.user.get('id'):
    return custom_response({'error': 'permission denied'}, 400)

  post.delete()
  return custom_response({'message': 'deleted'}, 204)
"""

def custom_response(res, status_code):
  """
  Custom Response Function
  """
  return Response(
    mimetype="application/json",
    response=json.dumps(res),
    status=status_code
  )


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 27 12:28:13 2017

@author: Yorick.Boheemen
"""

from PIL import Image
from pylab import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import filters
from scipy.ndimage import measurements,morphology
from scipy import signal
import scipy.misc
from sklearn.cluster import KMeans
from os import listdir
from os.path import isfile, join

# custom imports:
import imtools
from imtools import denoise
from imtools import pca
#import cv2

def sobelFilter(img):
#  imgsobel = filters.sobel(img)
#  return imgsobel
  imgray = img # maybe convert to grayscale first?
  imx = zeros(imgray.shape)
  filters.sobel(imgray,1,imx)
  imy = zeros(imgray.shape)
  filters.sobel(imgray,0,imy)
  magnitude = sqrt(imx**2+imy**2)
  return magnitude.astype(int)
# enddef

# attempt to detect corners
# so far, it fails
def sobelCorners(img):

  imgray = img # maybe convert to grayscale first?
  imx = zeros(imgray.shape)
  filters.sobel(imgray,1,imx)
  imy = zeros(imgray.shape)
  filters.sobel(imgray,0,imy)
  prodsobel = np.multiply(imx,imy)
  prodsobel = np.round((prodsobel-np.min(prodsobel))/np.max(prodsobel)*255)
  return prodsobel
# enddef

def gaussianBlur(img,sigma=3):
  imgfiltered = filters.gaussian_filter(img, sigma)
  return imgfiltered
# enddef

def gaussDer(img,sigma=3,k=sqrt(2)):
  img_gsigma = filters.gaussian_filter(img,sigma)
  img_gksigma = filters.gaussian_filter(img,k*sigma)
  #img_gk2sigma = filters.gaussian_filter(imgray,k*k*sigma)
  return img_gksigma-img_gsigma
# enddef

# 2 level segmentation, find ideal threshold
def binarySegmentation(img):
  imgflat = img.flatten()
  T = 100
  lastT = 0
  while (abs(T - lastT) > 1):
      lastT = T
      mf = mean(imgflat[np.where(imgflat>T)])
      mb = mean(imgflat[np.where(imgflat<=T)])
      T = 0.5*(mb+mf)
  #endwhile
  imgsegm = 1*(img<T)
  return imgsegm
# enddef

#grayscale
def kMeansSegmentationGray(img,k=5):
  rows,cols = shape(img)

  R = img.reshape(cols*rows,1)

  kmeans = KMeans(n_clusters=k, random_state=0).fit(R)

  imsegmented = kmeans.labels_.reshape(rows,cols)
  #add colours:
  labels = kmeans.labels_
  levels = kmeans.cluster_centers_
  imsegmented = (levels[np.array(labels)]).reshape(rows,cols).astype(uint8)
  return imsegmented
# enddef

# color
def kMeansSegmentation(img,k=5):
  rows,cols, planes = shape(img)

  R = img.reshape(cols*rows,planes)

  kmeans = KMeans(n_clusters=k, random_state=0).fit(R)

  imsegmented = kmeans.labels_.reshape(rows,cols)
  #add colours:
  labels = kmeans.labels_
  levels = kmeans.cluster_centers_
  imsegmented = (levels[np.array(labels)]).reshape(rows,cols,planes).astype(uint8)
  return imsegmented
# enddef

# denoise: in imtools
def denoise(imgray):
  return imtools.denoise(imgray,imgray)
# enddef


def count_inversions(arr):
    """
    Count the number of inversions in an array.
    An inversion is a pair (i, j) such that i < j and A[i] > A[j].
    This can be thought of as a discrete measure of how close an array is to being sorted. (n_inversions = 0 ==> array is sorted)
    """
    return count_inversions_sort(arr)[0]

def count_inversions_sort(arr):
    size = len(arr)

    # Base case
    if size <= 1:
        return 0, arr

    # Recursive case
    mid = size // 2
    left_inversions, sorted_A = count_inversions_sort(arr[:mid])
    right_inversions, sorted_B = count_inversions_sort(arr[mid:])

    # Combine (and also count split inversions)
    a = 0
    b = 0
    split_inversions = 0
    sorted_arr = []

    for i in range(size):
        if a == len(sorted_A):
            sorted_arr.append(sorted_B[b])
            b += 1
        elif b == len(sorted_B):
            sorted_arr.append(sorted_A[a])
            a += 1
        elif sorted_A[a] < sorted_B[b]:
            sorted_arr.append(sorted_A[a])
            a += 1
        else:
            sorted_arr.append(sorted_B[b])
            b += 1

            # If this is reached, then the current element in A is LARGER than the current element in B.
            # Moreover, since A and B are both sorted (ascending), we know that all elements in A after the current element are also greater than the current element in B.
            # Thus, the number of inversions involving the current element in B is the number of elements remaining in A
            split_inversions += len(sorted_A) - a

    return (left_inversions + right_inversions + split_inversions), sorted_arr

def __count_inversions_test(arr, expected_inversions):
    inversions = count_inversions(arr)
    print("{} --> {} (passed? {})".format(arr, inversions, "yes" if inversions == expected_inversions else "no"))

if __name__ == "__main__":
    __count_inversions_test([1, 3, 5, 2, 4, 6], 3)
    __count_inversions_test([6, 5, 4, 3, 2, 1], 15) # If array is sorted descending: n_inversions = (n-1)+(n-2)+(n-3)+...+2+1 = n(n-1)/2


# import random

# def busqueda_lineal(lista,objetivo):
#     match=False

#     for elemento in lista:

#         if elemento==objetivo:
#             match=True

#     return match

# if __name__ == '__main__':

#     tamaño_lista=int(input("Tamaño de la lista"))
#     objetivo=int(input("Numero a buscar"))

#     lista=[random.randint(0,100) for i in range(tamaño_lista)]
#     encontrado= busqueda_lineal(lista,objetivo)
#     print(lista)
#     print(f'El elemento {objetivo} {"esta" if encontrado else "no esta"} en la lista')
#     #Operador Ternario

import random
lista=[random.randint(0,100) for i in range (10)]
print (lista)

len(lista)
lista[9]


import os
# This script modifies `WarpX-test.ini` (which is used for nightly builds)
# and creates the file `ci-test.ini` (which is used for continous
# integration)
# The subtests that are selected are controlled by WARPX_TEST_DIM
# The architecture (CPU/GPU) is selected by WARPX_TEST_ARCH
import re

# Get relevant environment variables
arch = os.environ.get('WARPX_TEST_ARCH', 'CPU')

ci_regular_cartesian_1d = os.environ.get('WARPX_CI_REGULAR_CARTESIAN_1D') == 'TRUE'
ci_regular_cartesian_2d = os.environ.get('WARPX_CI_REGULAR_CARTESIAN_2D') == 'TRUE'
ci_regular_cartesian_3d = os.environ.get('WARPX_CI_REGULAR_CARTESIAN_3D') == 'TRUE'
ci_psatd = os.environ.get('WARPX_CI_PSATD', 'TRUE') == 'TRUE'
ci_single_precision = os.environ.get('WARPX_CI_SINGLE_PRECISION') == 'TRUE'
ci_rz_or_nompi = os.environ.get('WARPX_CI_RZ_OR_NOMPI') == 'TRUE'
ci_qed = os.environ.get('WARPX_CI_QED') == 'TRUE'
ci_eb = os.environ.get('WARPX_CI_EB') == 'TRUE'
ci_openpmd = os.environ.get('WARPX_CI_OPENPMD') == 'TRUE'
ci_ccache = os.environ.get('WARPX_CI_CCACHE') == 'TRUE'
ci_num_make_jobs = os.environ.get('WARPX_CI_NUM_MAKE_JOBS', None)

# Find the directory in which the tests should be run
current_dir = os.getcwd()
test_dir = re.sub('warpx/Regression', '', current_dir )

with open('WarpX-tests.ini') as f:
    text = f.read()

# Replace default folder name
text = re.sub('/home/regtester/AMReX_RegTesting', test_dir, text)
# Remove the web directory
text = re.sub('[\w\-\/]*/web', '', text)

# Add doComparison = 0 for each test
text = re.sub( '\[(?P<name>.*)\]\nbuildDir = ',
               '[\g<name>]\ndoComparison = 0\nbuildDir = ', text )

# Change compile options when running on GPU
if arch == 'GPU':
    text = re.sub( 'addToCompileString =',
                   'addToCompileString = USE_GPU=TRUE USE_OMP=FALSE ', text)
print('Compiling for %s' %arch)

# Extra dependencies
if ci_openpmd:
    text = re.sub('addToCompileString =',
                  'addToCompileString = USE_OPENPMD=TRUE ', text)

# always build with PSATD support (runtime controlled if used)
if ci_psatd:
    text = re.sub('addToCompileString =',
                  'addToCompileString = USE_PSATD=TRUE ', text)
    text = re.sub('USE_PSATD=FALSE',
                  '', text)

# Ccache
if ci_ccache:
    text = re.sub('addToCompileString =',
                  'addToCompileString = USE_CCACHE=TRUE ', text)

# Add runtime options:
# > crash for unused variables
# > trap NaNs, divisions by zero, and overflows
# > abort upon any warning message by default
text = re.sub('runtime_params =',
              'runtime_params = amrex.abort_on_unused_inputs=1 '+
              'amrex.fpe_trap_invalid=1 amrex.fpe_trap_zero=1 amrex.fpe_trap_overflow=1 '+
              'warpx.always_warn_immediately=1 warpx.abort_on_warning_threshold=low',
              text)

# Use less/more cores for compiling, e.g. public CI only provides 2 cores
if ci_num_make_jobs is not None:
    text = re.sub( 'numMakeJobs = \d+', 'numMakeJobs = {}'.format(ci_num_make_jobs), text )

# Prevent emails from being sent
text = re.sub( 'sendEmailWhenFail = 1', 'sendEmailWhenFail = 0', text )

# Select the tests to be run
# --------------------------

# - Extract test blocks (they are identified by the fact that they contain "inputFile")
select_test_regex = r'(\[(.+\n)*inputFile(.+\n)*)'
test_blocks =  [ match[0] for match in re.findall(select_test_regex, text) ]
# - Remove the test blocks from `text` (only the selected ones will be added back)
text = re.sub( select_test_regex, '', text )

def select_tests(blocks, match_string_list, do_test):
    """Remove or keep tests from list in WarpX-tests.ini according to do_test variable"""
    if do_test not in [True, False]:
        raise ValueError("do_test must be True or False")
    if (do_test == False):
        for match_string in match_string_list:
            print('Selecting tests without ' + match_string)
            blocks = [ block for block in blocks if not match_string in block ]
    else:
        for match_string in match_string_list:
            print('Selecting tests with ' + match_string)
            blocks = [ block for block in blocks if match_string in block ]
    return blocks

if ci_regular_cartesian_1d:
    test_blocks = select_tests(test_blocks, ['dim = 1'], True)
    test_blocks = select_tests(test_blocks, ['USE_RZ=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['PRECISION=FLOAT', 'USE_SINGLE_PRECISION_PARTICLES=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['useMPI = 0'], False)
    test_blocks = select_tests(test_blocks, ['QED=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['USE_EB=TRUE'], False)

if ci_regular_cartesian_2d:
    test_blocks = select_tests(test_blocks, ['dim = 2'], True)
    test_blocks = select_tests(test_blocks, ['USE_RZ=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['PRECISION=FLOAT', 'USE_SINGLE_PRECISION_PARTICLES=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['useMPI = 0'], False)
    test_blocks = select_tests(test_blocks, ['QED=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['USE_EB=TRUE'], False)

if ci_regular_cartesian_3d:
    test_blocks = select_tests(test_blocks, ['dim = 3'], True)
    test_blocks = select_tests(test_blocks, ['PRECISION=FLOAT', 'USE_SINGLE_PRECISION_PARTICLES=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['useMPI = 0'], False)
    test_blocks = select_tests(test_blocks, ['QED=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['USE_EB=TRUE'], False)

if ci_single_precision:
    test_blocks = select_tests(test_blocks, ['PRECISION=FLOAT', 'USE_SINGLE_PRECISION_PARTICLES=TRUE'], True)

if ci_rz_or_nompi:
    block1 = select_tests(test_blocks, ['USE_RZ=TRUE'], True)
    block2 = select_tests(test_blocks, ['useMPI = 0'], True)
    test_blocks = block1 + block2

if ci_qed:
    test_blocks = select_tests(test_blocks, ['QED=TRUE'], True)

if ci_eb:
    test_blocks = select_tests(test_blocks, ['USE_RZ=TRUE'], False)
    test_blocks = select_tests(test_blocks, ['USE_EB=TRUE'], True)

# - Add the selected test blocks to the text
text = text + '\n' + '\n'.join(test_blocks)

with open('ci-tests.ini', 'w') as f:
    f.write(text)


print("""

FEM simulation using getfem++ and siconos.

""")

import siconos.kernel as kernel
import getfemtosiconos as gts
import numpy as np
import getfem as gf
from matplotlib.pyplot import *

t0 = 0.0      # start time
T = 10.0    # end time
h = 0.005   # time step
g = 9.81    # gravity
e = 0.1    # restitution coeficient
mu=0.3 # Friction coefficient
theta = 0.5 # theta scheme

with_friction = False

sico = gts.SiconosFem()
fem_model = gts.import_fem(sico)

# =======================================
# Create the siconos Dynamical System
# =======================================
# Initial position and velocity
v0 = np.zeros(sico.nbdof)

block = kernel.LagrangianLinearTIDS(sico.q0,v0,sico.Mass.full())
F = sico.RHS
block.setFExtPtr(F)
block.setKPtr(sico.Stiff.full())

position_init = sico.pos

# =======================================
# The interaction
# =======================================

dist = 0.5
diminter = 1
b = np.repeat([dist], diminter)
nslaw = kernel.NewtonImpactNSL(e)
k=0

pos0 = np.dot(sico.H,sico.pos)

dimH = sico.H.shape[0]
relation=[]
inter=[]
hh = np.zeros((diminter,sico.nbdof))
for i in range(0,dimH,3):
    hh[0,:] = sico.H[i,:]
    b2 = b + pos0[i]
    relation.append(kernel.LagrangianLinearTIR(hh,b2))
    inter.append(kernel.Interaction(diminter, nslaw, relation[k]))
    k+=1

nbInter=len(inter)

# =======================================
# The Model
# =======================================
blockModel = kernel.Model(t0,T)

# add the dynamical system to the non smooth dynamical system
blockModel.nonSmoothDynamicalSystem().insertDynamicalSystem(block)

# link the interaction and the dynamical system
for i in range(nbInter):
    blockModel.nonSmoothDynamicalSystem().link(inter[i],block);

# =======================================
# The Simulation
# =======================================

# (1) OneStepIntegrators
OSI = kernel.Moreau(theta)

# (2) Time discretisation --
t = kernel.TimeDiscretisation(t0,h)

# (3) one step non smooth problem
if(with_friction):
    osnspb = kernel.FrictionContact(3)
    osnspb.numericsSolverOptions().iparam[0]=100
    osnspb.numericsSolverOptions().iparam[1]=20
    osnspb.numericsSolverOptions().iparam[4]=2
    osnspb.numericsSolverOptions().dparam[0]=1e-6
    osnspb.numericsSolverOptions().dparam[2]=1e-8
    osnspb.setMaxSize(1000)
    osnspb.setMStorageType(1)
    osnspb.setNumericsVerboseMode(0)
    osnspb.setKeepLambdaAndYState(true)
else:
    osnspb = kernel.LCP()

# (4) Simulation setup with (1) (2) (3)
s = kernel.TimeStepping(t)
s.insertIntegrator(OSI)
s.insertNonSmoothProblem(osnspb)

# simulation initialization
blockModel.initialize()

# the number of time steps
N = (T-t0)/h

# Get the values to be plotted
# ->saved in a matrix dataPlot

dataPlot = np.empty((N+1,9))

dataPlot[0, 0] = t0
dataPlot[0, 1] = block.q()[2]
#dataPlot[0, 2] = block.velocity()[2]
dataPlot[0, 2] = block.q()[5]
dataPlot[0, 3] = block.q()[8]
dataPlot[0, 4] = block.q()[11]
dataPlot[0, 5] = block.q()[14]
dataPlot[0, 6] = block.q()[17]
dataPlot[0, 7] = block.q()[20]
dataPlot[0, 8] = block.q()[23]

nbNodes = sico.mesh.pts().shape[1]

k = 1
# time loop
while(s.hasNextEvent()):
    s.computeOneStep()
    name = 'titi'+str(k)+'.vtk'
    dataPlot[k,0]=s.nextTime()
    #dataPlot[k,1]=block.q()[2]
    #dataPlot[k,2]=block.velocity()[2]
    #   dataPlot[k, 2] = block.q()[5]
    #dataPlot[k, 3] = block.q()[8]
    #dataPlot[k, 4] = block.q()[11]
    #dataPlot[k, 5] = block.q()[14]
    #dataPlot[k, 6] = block.q()[17]
    #dataPlot[k, 7] = block.q()[20]
    #dataPlot[k, 8] = block.q()[23]
    siz = sico.pos.size
    reference_pos = sico.pos
    current_pos = block.q() +  reference_pos
    dataPlot[k,1]=current_pos[2]
    #dataPlot[k,2]=block.velocity()[2]
    dataPlot[k, 2] = current_pos[5]
    dataPlot[k, 3] = current_pos[8]
    dataPlot[k, 4] = current_pos[11]
    dataPlot[k, 5] = current_pos[14]
    dataPlot[k, 6] = current_pos[17]
    dataPlot[k, 7] = current_pos[20]
    dataPlot[k, 8] = current_pos[23]

    #bottom_pos = current_pos[0:12]
    #new_ref = reference_pos
    #translatX = reference_pos[0] - current_pos[0]
    #translatY = reference_pos[1] - current_pos[1]
    #translatZ = reference_pos[2] - current_pos[2]
    #trans = np.repeat([translatX],nbNodes)
    #new_ref[0:siz:3] = reference_pos[0:siz:3] - trans
    #trans = np.repeat([translatY],nbNodes)
    #new_ref[1:siz:3] = reference_pos[1:siz:3] - trans
    #trans = np.repeat([translatZ],nbNodes)
    #new_ref[2:siz:3] = reference_pos[2:siz:3] - trans

    #depl = current_pos - new_ref
    #block.setQPtr(depl)
    #correction = np.dot(sico.H,new_ref)
    #bnew = correction[0:dimH:3]
    #for i in range(nbInter):
    #    bb = b + bnew[i]
    #    relation[i].setEPtr(bb)

    #print "RESUME"
    #relation[0].computeOutput(s.nextTime(),0)
    #inter[0].y(0).display()
    #print current_pos[0:3]
    #print new_ref[0:3]
    #print depl[0:3]

    fem_model.to_variables(block.q())
    U = fem_model.variable('u')
    #POS = current_pos
    sl = gf.Slice(('boundary',),sico.mfu,1)
    #sl.export_to_vtk(name, sico.mfu, U,'Displacement', sico.mfu, POS, 'Position')
    sl.export_to_vtk(name, sico.mfu, U,'Displacement')
    #print s.nextTime()
    k += 1
    s.nextStep()

subplot(211)
title('position')
plot(dataPlot[:,0], dataPlot[:,1])
plot(dataPlot[:,0], dataPlot[:,2])
plot(dataPlot[:,0], dataPlot[:,3])
plot(dataPlot[:,0], dataPlot[:,4])
plot(dataPlot[:,0], dataPlot[:,5])
plot(dataPlot[:,0], dataPlot[:,6])
plot(dataPlot[:,0], dataPlot[:,7])
plot(dataPlot[:,0], dataPlot[:,8])

grid()
subplot(212)
title('velocity')
plot(dataPlot[:,0], dataPlot[:,2])
grid()
show()


import time
import sys
from list_files import InventoryList as InventList

################################################################
# Movie Inventory List
#
# Driver that utilizes the Inventory List file to create a
# list of movies and their genres for the user. Currently only
# # supports unique movie titles.
#
# Movie  - key
# Genre  - value
#
# Imports:
#   time            allows the program to pause for ease of use
#   sys             allows the user to exit the program on command
#   inventory_list  file that contains this list's data functions
################################################################

######################################
# Movie Driver
######################################
def movie_driver(user_list):
    # find out what the user wants to do with their list
    request = input("\nWhat would you like to do?\n" +
                    "1 - Print your movie list\n" +
                    "2 - Add new movie\n" +
                    "3 - Remove an existing movie\n" +
                    "4 - Change a movie's genre\n" +
                    "5 - Show all movies of a genre\n" +
                    "6 - Delete your movie list\n" +
                    "7 - Make a new movie list\n" +
                    "q - I'm done!\n")

    ######################################
    # User requests will be directed here:
    ######################################
    # Print list
    if request == "1":
        print_movielist(user_list)
    # Add new movie
    elif request == "2":
        add_movies(user_list)
    # Remove an existing movie
    elif request == "3":
        remove_movie(user_list)
    # Change movie's genre
    elif request == "4":
        set_genre(user_list)
    # Show all movies of genre
    elif request == "5":
        movies_by_genre(user_list)
    # Delete list
    elif request == "6":
        delete_movielist(user_list)
    # Make new list
    elif request == "7":
        confirm = input("\nThis will overwrite your current movie list. Proceed?\n" +
                        "1 - Go ahead\n" +
                        "2 - No please!\n")
        if confirm == "1":
            create_movielist()
    elif request == "q" or request == "quit":
        sys.exit()
    else:  # user put in a num not on the list
        print("\nSorry, that number isn't on the list. Try again?\n")
        movie_driver(user_list)

    movie_driver(user_list)

######################################
# Movie List Constructor
######################################
def create_movielist():
    print("\nLet's create a Movie List!\n")
    user_list = InventList.InventoryList()
    add_movies(user_list)

######################################
# Movie List Printer
######################################
def print_movielist(user_list):
    print("\n")
    user_list.print_list()
    print("\n")
    time.sleep(0.5)

######################################
# Movie Deleter
######################################
def remove_movie(user_list):
    print("\n")
    print_movielist(user_list)
    print("\nWhich movie would you like to remove?")
    movie = input()
    user_list.remove_item(movie)
    time.sleep(0.5)
    print("\nRemoved " + movie + " from your movie list.")

    # return the updated list to the Driver
    movie_driver(user_list)

######################################
# Movie List Deleter
######################################
def delete_movielist(user_list):
    confirm = input("\nAre you sure you want to delete your entire list?\n" +
                    "1 - Yes! I'm done with it.\n" +
                    "2 - No! Please take me back.\n")

    if confirm == "1":
        user_list.delete_list()
        print("\nYour movie list has been deleted.")
        time.sleep(0.5)
    elif confirm == "2":
        print("Ok!")
        time.sleep(0.5)
    else:  # allows the user to try again
        print("That isn't an option, try again?\n")
        time.sleep(0.5)
        delete_movielist(user_list)

######################################
# Movie Adder
######################################
def add_movies(user_list):
    print("\nType each movie you would like to add with its genre separated by a comma.\n" +
          "For example: Hereditary, horror\n" +
          "Hit Enter after each movie and its genre and type done when you're finished.")

    # take in user's input in format: "key, value"
    key_val = input()

    while key_val != "done":
        if key_val == "q" or key_val == "quit":
            sys.exit()
        else:
            user_list.add_item(key_val)
            key_val = input()

    # return the updated list to the Driver
    movie_driver(user_list)

######################################
# Genre Setter
######################################
def set_genre(user_list):
    print_movielist(user_list)
    print("Which movie's genre would you like to change?")
    movie = input()
    print("What is the new genre you'd like it to have?")
    genre = input()

    user_list.set_value(movie, genre)
    print("\n" + movie + " is now in the " + genre + " genre.")

    # return the updated list to the Driver
    movie_driver(user_list)

######################################
# Print Movies by Genre
######################################
def movies_by_genre(user_list):
    print("\nWhich genre would you like to see?")
    genre = input()

    if genre == "q" or genre == "quit":
        sys.exit()
    else:
        print("\nMovies in the " + genre + " genre:")
        # list with these movies
        movies_in_genre = user_list.keys_by_value(genre)
        movies_in_genre.sort()
        for movie in movies_in_genre:
            print(movie)