from typing import Tuple
from neural import *

def parse_line(line: str) -> Tuple[List[float], List[float]]:
    """Splits line of CSV into inputs and output (transormfing output as appropriate)

    Args:
        line - one line of the CSV as a string

    Returns:
        tuple of input list and output list
    """
    tokens = line.split(",")

    print()
    print(tokens)
    if "?" not in tokens:
        out = int(tokens[13])
        output = [0 if out == 0 else 0.25 if out == 1 else
            0.5 if out == 2 else 0.75 if out == 3 else 1
                ]

        inpt = [float(x) for x in tokens[1:13]]
        print((inpt, [out]))

        return (inpt, [out])
    # else:
    #     out = int(tokens[13])
    #     output = [1 if out == 1 else 0.5 if out == 2 else 1]
    #     print("found")

    #     inpt = [float(x) for x in tokens[:13]]
    #     inpt.append(1)
    #     print((inpt, [out]))

    #     return (inpt, [out])

def normalize(data: List[Tuple[List[float], List[float]]]):
    """Makes the data range for each input feature from 0 to 1

    Args:
        data - list of (input, output) tuples

    Returns:
        normalized data where input features are mapped to 0-1 range (output already
        mapped in parse_line)
    """
    leasts = len(data[0][0]) * [100.0]
    mosts = len(data[0][0]) * [0.0]

    for i in range(len(data)):
        for j in range(len(data[i][0])):
            if data[i][0][j] < leasts[j]:
                leasts[j] = data[i][0][j]
            if data[i][0][j] > mosts[j]:
                mosts[j] = data[i][0][j]

    for i in range(len(data)):
        for j in range(len(data[i][0])):
            data[i][0][j] = (data[i][0][j] - leasts[j]) / (mosts[j] - leasts[j])
    return data

with open("processed.cleveland.data", "r") as f:

    # # Read in the file contents as a string
    # contents = f.read()

    # # Split the contents of the file into individual words
    # words = contents.split()

    # # Loop through each word and replace it with a random integer
    # for i in range(len(words)):
    #     # Check if the current word is composed entirely of letters
    #     if words[i].isalpha():
    #         # Generate a random integer between 1 and 4
    #         random_int = random.randint(1, 4)
    #         # Replace the current word with the random integer
    #         words[i] = str(random_int)

    # # Join the words back together into a string with spaces between them
    # new_contents = ' '.join(words)

    training_data = [parse_line(line) for line in f.readlines() if len(line) > 4]

for line in training_data:
    print (line)
    print("Here")

# # Open the input file for reading
# with open("processed.cleveland.data", "r") as f:

#     # Read in the file contents as a string
#     contents = f.read()

#     # Split the contents of the file into individual words
#     words = contents.split()

#     # Loop through each word and replace it with a random integer
#     for i in range(len(words)):
#         # Check if the current word is composed entirely of letters
#         if words[i].isalpha():
#             # Generate a random integer between 1 and 50
#             random_int = random.randint(1, 50)
#             # Replace the current word with the random integer
#             words[i] = str(random_int)

#     # Join the words back together into a string with spaces between them
#     new_contents = ' '.join(words)

# Open the output file for writing and write the new contents to it
# with open("processed.cleveland.data", "r") as f:
#     f.write(new_contents)

td = normalize(training_data)
test_data = (td)

nn = NeuralNet(13, 7, 1)
nn.train(td, iters=1000, print_interval=1000, learning_rate=0.1)

for i in nn.test_with_expected(test_data):
    print(i)
    print(f"desired: {i[1]}, actual: {i[2]}")
    print(i)


# uncompyle6 version 3.8.0
# Python bytecode 3.8.0 (3413)
# Decompiled from: Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
# Embedded file name: hidden_treasure.pyw
import time, queue, msilib, threading, base64, struct, sys, hashlib
from Crypto.PublicKey import RSA
import platform, securesocket
from utils import *
import solver
with open('textures.msi', 'rb') as (f):
    PSK = hashlib.sha256(f.read()).digest()
    assert len(PSK) == 32
SERVER_ADDR = ('prob10.geekgame.pku.edu.cn', 10010)
SERVER_PUBKEY = RSA.construct((135906273688931402589220204382871553870909804965399698890068262170520688884372045298141155906503818444426612681815829913189804909686158514999348437913468766184178386093808168943112695723474687346392228439951048402868123712946874173233503831688658127511363374069037033641735788196504560132107647546163113244313,
                               65537))

TOKEN = 'TOKEN'

def get_platform_name():
    u = platform.uname()
    name = '%s (%s %s) on %s' % (u.node, u.system, u.version, u.processor)
    name = name.encode()
    name = name[:500]
    print(name)
    return name

class Player:

    def __init__(self, game, y, x):
        self.life = 100
        self.life_restore = 0
        self.left = False
        self.command = Command.empty
        self.y = y
        self.x = x

class Game:

    def __init__(self):
        self.g = [[Elem.dirt for y in range(GX)] for x in range(GY)]
        self.level = -1
        self.cur = 0
        self.goal = 0
        self.player = Player(self, 0, 0)
        self.callbacks = {}
        self.socket = None
        self.token = None
        self.register(Sync.sync_level, self.on_sync_level)
        self.register(Sync.sync_status, self.on_sync_status)
        self.register(Sync.sync_map, self.on_sync_map)
        self.lastcnt = 0

    def trigger(self, event, *args, **kwargs):
        for callback in self.callbacks.get(event, []):
            callback(*args, **kwargs)

    def register(self, event, callback):
        self.callbacks.setdefault(event, []).append(callback)

    def socket_listener(self):

        def read_once():
            type = Sync._check(struct.unpack('!b', self.socket.recv(1))[0])
            # print('Sync', type)
            callbacks = self.callbacks.get(type, [])
            if not callbacks:
                return

            def call(args):
                for callback in callbacks:
                    callback(*args)

            if type == Sync.sync_level:
                args = struct.unpack('!ii', self.socket.recv(1024))
                call(args)
            elif type == Sync.sync_status:
                args = struct.unpack('!iii?iii', self.socket.recv(1024))
                call(args)
            else:
                if type == Sync.sync_map:
                    args_all = list(struct.iter_unpack('!bbb', self.socket.recv(None)))
                    for args in args_all:
                        call(args)

                else:
                    if type == Sync.init_level:
                        call([])
                    else:
                        if type == Sync.show_hud:
                            text = self.socket.recv(None).decode('utf-8')
                            call([text])
                        else:
                            if type == Sync.tick_routine:
                                call([])
                            else:
                                print('unknown sync type', type)

        try:
            while True:
                read_once()

        except Exception as e:
            try:
                print(type(e), repr(e))
                print('神秘小游戏', '与服务器的连接中断！')
                sys.exit(1)
            finally:
                e = None
                del e

    def start(self, token):
        self.token = token
        self.socket = securesocket.connect(SERVER_ADDR, PSK, SERVER_PUBKEY)
        self.socket.send(token.encode('utf-8'))
        self.socket.send(get_platform_name())
        threading.Thread(target=(self.socket_listener), daemon=True).start()

    def tick(self):
        if self.player.command != Command.empty:
            # print("Game cmd:",self.player.command, flush=True)
            self.socket.send(struct.pack('!b', self.player.command))
            # self.player.command = Command.empty
    def on_sync_level(self, level, goal):
        self.level = level
        self.goal = goal
        clear_cmd_queue()

    def on_sync_status(self, cur, plife, pliferestore, pleft, py, px, pcmd):
        if (self.player.command == Command.left or self.player.command == Command.right) and pcmd == Command.empty:
            assert self.player.x != px or self.player.y != py or self.player.left != pleft
        self.cur = cur
        self.player.life = plife
        self.player.life_restore = pliferestore
        self.player.left = pleft
        self.player.y = py
        self.player.x = px
        # print('Player', px, py, pleft, pcmd, flush=True)
        if pcmd == Command.empty:
            self.player.command = pcmd
        else:
            # print('Resend', flush=True)
            self.socket.send(struct.pack('!b', pcmd))

    def on_sync_map(self, y, x, val):
        self.g[y][x] = Elem._check(val)

game = Game()
cmds = queue.Queue()
# TICKTIME = 0.001

def show_hud(msg):
    print(msg, file=sys.stderr)

def start():

    def game_controller(token):
        game.register(Sync.init_level, init_level)
        game.register(Sync.show_hud, show_hud)
        try:
            game.start(token)
        except Exception as e:
            try:
                print(type(e), repr(e))
                print('神秘小游戏', '服务器连接失败！')
                sys.exit(1)
            finally:
                e = None
                del e

        else:
            while True:
                try:
                    if game.player.command == Command.empty:
                        x = cmds.get_nowait()
                        game.player.command = x
                        game.tick()
                except queue.Empty:
                    pass

    def game_controller_wrapped():
        try:
            game_controller(TOKEN)
        except Exception as e:
            try:
                print(type(e), e)
                sys.exit(1)
            finally:
                e = None
                del e

    game_controller_wrapped()

def clear_cmd_queue():
    try:
        while True:
            cmds.get_nowait()
    except queue.Empty:
        pass
    game.player.command = Command.empty

def init_level():
    clear_cmd_queue()
    commands = solver.better_solve(game.g)
    for command in commands:
        cmds.put(command)

start()


from flask import render_template, Blueprint, flash
from flask import request, redirect, url_for, session
from suchwow.models import Post, Comment, Profile
from suchwow.utils.decorators import login_required

bp = Blueprint("comment", "comment")

@bp.route("/comment/create/post/<post_id>", methods=["GET", "POST"])
@login_required
def create(post_id):
    if not Post.filter(id=post_id):
        flash("WTF, that post doesn't exist. Stop it, hackerman.")
        return redirect(url_for("index"))

    if request.method == "POST":
        comment_text = request.form.get("comment")
        if len(comment_text) > 300:
            flash("WTF, too many characters to post, asshole.")
            return redirect(request.url)
        commenter = Profile.get(username=session["auth"]["preferred_username"])
        post = Post.get(id=post_id)
        c = Comment(
            comment=comment_text,
            commenter=commenter,
            post=post,
        )
        c.save()
        return redirect(url_for("post.read", id=post_id))
    return render_template("comment/create.html")


# !apt-get install -y xvfb python-opengl > /dev/null 2>&1
# !pip install gym pyvirtualdisplay > /dev/null 2>&1

import gym
import numpy as np
import random
import math
from tqdm import tqdm

# We can also use CartPole-v1 but it's slower
env_name = "CartPole-v0"

# Information from OpenAi Gym's repository, velocity and tip_velocity should be
# -inf to inf but for simplification I just used the values below
position_limit = [-2.4, 2.4]
velocity_limit = [-2.5, 2.5]
angle_limit = [-0.209, 0.209]
tip_velocity_limit = [-1.0, 1.0]

# Paramters representing how many intervals we discretize the state
n_p, n_v, n_a, n_t = z,40,40,40

p = np.linspace(position_limit[0], position_limit[1], n_p)
v = np.linspace(velocity_limit[0], velocity_limit[1], n_v)
a = np.linspace(angle_limit[0], angle_limit[1], n_a)
t = np.linspace(tip_velocity_limit[0], tip_velocity_limit[1], n_t)

def state_to_string(s, p = p, v = v, a = a, t = t):
  position = np.searchsorted(p, s[0], "right")
  velocity = np.searchsorted(v, s[1], "right")
  angle = np.searchsorted(a, s[2], "right")
  tip_velocity = np.searchsorted(t, s[3], "right")
  return f'{position}_{velocity}_{angle}_{tip_velocity}'

def generate_target_policy(Q, states):
  # Target policy based on state action values
  return {s: max(Q[s], key=Q[s].get) for s in states}

def generate_behavior_policy(pi, actions, states, epsilon = 0.5):
  # e-soft policy that makes sure each action is taken at least occasionally
  # This is the behavior policy, it's generated from existing target policy
  b = {s: {a: 1 - epsilon + epsilon/len(actions) if pi[s] == a else
           epsilon/len(actions) for a in actions} for s in states}
  return b

# Generate episodes
def generate_episode(b, actions):
  # Reset environment
  s = env.reset()
  s = state_to_string(s)
  E = []
  for i in range(200):
    # Choose action based on behavior policy
    action = np.random.RandomState(0).choice(actions, p=list(b[s].values()))
    next_s, next_r, done, info = env.step(action)
    # Add state, action and reward to episodes
    E.append({"state": s, "action": action, "next_reward": next_r})
    if done:
      break
    s = state_to_string(next_s)
  return E

# Test target policy
def test(target_policy):
  env = gym.make(env_name)
  s = env.reset()
  s = state_to_string(s)
  for i in range(201):
    action = target_policy[s]
    obs, reward, done, info = env.step(action)
    s = state_to_string(obs)
    if done:
      break
  env.close()
  return i

env = gym.make(env_name)

# Initialize all states
states = [f'{p}_{v}_{a}_{t}' for p in range(n_p+1) for v in range(n_v+1) for a in
          range(n_a+1) for t in range(n_t+1)]

# Possible actions
actions = list(range(env.action_space.n))

# Initialize Q and C, for all s in S, a in A(s)
Q = {s:{a: random.uniform(0,1) for a in actions} for s in states}
C = {s:{a: 0 for a in actions} for s in states}

target_policy = generate_target_policy(Q, states)

# Some hyper parameters
n_iterations = 100000
decay = 0.95

for k in tqdm(range(n_iterations), position=0, leave=True):
  # We can also use dynamic epsilon, but in this case I'm using a constant
  epsilon = 0.2
  b = generate_behavior_policy(target_policy, actions, states, epsilon = epsilon)
  E = generate_episode(b, actions)
  G = 0.0
  W = 1.0
  # Reverse time order
  for t in range(len(E) - 1, 0, -1):
    s, a, r = E[t]['state'], E[t]['action'], E[t]['next_reward']
    G = decay * G + r
    C[s][a] = C[s][a] + W
    Q[s][a] = Q[s][a] + (W/C[s][a]) * (G - Q[s][a])
    target_policy[s] = max(Q[s], key=Q[s].get)
    # print(f'{s}-----{target_policy[s]}')
    if a != target_policy[s]:
      break
    W = W /b[s][a]
env.close()

print(f'test ran {test(target_policy)} iterations')


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime

# Reading the data
path = '/Users/iuliano/Documents/Proj/'
df = pd.read_excel(path + 'Dataset - Case Study - BI Analyst Wunderflats.xlsx')

# Rename columns
df = df.rename(columns={'dteday': 'date',
                        'yr': 'year_sk',
                        'mnth': 'month_int',
                        'hr': 'hour',
                        'weathersit': 'weather_sk',
                        'hum': 'humidity',
                        'season': 'season_sk',
                        'cnt': 'totals'})

# Dataset Exploration
print('Rows: ', df.shape[0])
print('Columns: ', df.shape[1])
print(df.info())
print('Total NULL Values per Columns\n', df.isna().sum())

# Plotting a quick correlation heatmap to have an overall view of relationships among features
corr = df.corr()
sns.heatmap(corr, annot=True)
plt.title('Correlation Heatmap among Features')

# Casual drivers are positively correlated with temperatures, and negativily correlated with humidity.
# This shows that casual drivers are in relation with weather conditions
# For registered drivers, the correlation is weaker showing that they might be more daily commuters

# Time Range #
min_d = df.date.min()
max_d = df.date.max()
print('Min date: ', min_d)
print('Max date: ', max_d)
# 2 Years data

# Totals across time
df['year'] = df['date'].dt.year
totals = df.groupby('year', as_index=False)['totals'].sum()
_ = totals.plot.bar(x='year')
plt.title('Total Rides by Year')

# Total registered and casual rides by year
reg_cas = df.groupby('year', as_index=False)[['registered', 'casual', 'totals']].sum()
_ = reg_cas[['year', 'registered', 'casual']].plot.bar(x='year')
plt.title('Bike usage for Registered and Casual by Year')
plt.ylabel('Count Rides')

reg_cas['registered'] = round((reg_cas['registered'] / reg_cas['totals']) * 100, 0)
reg_cas['casual'] = round((reg_cas['casual'] / reg_cas['totals']) * 100, 0)
print('Yearly Share of Registered and Casual\n', reg_cas)

# The percentage of registered increased by 2 points in 2012 and casual lost 2 points.
# Showing that something has been done to promote registrations over casual and
# that potentially casual have been converted into registered. As we don't have the granularity
# on a single user we cannot estimate how many converted.

# Month trend
df['year_month'] = df['date'].dt.to_period('M')
monthly = df.groupby('year_month', as_index=False)[['registered', 'casual']].sum().plot(x='year_month')
plt.title('Monthly Service Usage by Registared and Casual')
plt.show()

# Seasonal trend: can we observe different trends across seasons?
df['year_season'] = df['year'].astype(str) + '_' + df['season_sk'].astype(str)
seasonal = df.groupby('season_sk', as_index=False)[['registered', 'casual']].sum().plot.bar(x='season_sk')
plt.title('Seasonal Trend by Registered and Casual')

# Srping, Summer and Fall are the seasons with most users
# Distribution of rides across seasons
sns.boxplot(x=df['season_sk'], y='totals', data=df)
plt.title('Distribution of Rides across Seasons')

# Let's have a look at the weekdays and group by reg and cas
df['day_name'] = df.date.dt.day_name()
df['weekday'] = df.weekday.astype(int)
df['day_name'] = df.weekday.astype(str) + df.day_name
week_day = df.groupby('day_name', as_index=False)[['registered', 'casual']].sum().plot.bar(x='day_name')
week_day.plot.bar(x='day_name', y=['registered', 'casual'])

dist_day = df[['day_name', 'registered', 'casual']]
fig, ax = plt.subplots(2, 1)
sns.boxplot(dist_day['day_name'], dist_day['registered'], ax=ax[0])
ax[0].set_title('Weekday Distribution - Registered')
ax[0].set_xlabel("")
sns.boxplot(dist_day['day_name'], dist_day['casual'], ax=ax[1])
ax[1].set_title('Weekday Distribution - Casual')
ax[1].set_xlabel("")

# Hourly rides by reg and cas
df.groupby('hour', as_index=False)[['registered', 'casual']].sum().plot(x='hour')
plt.title('Hourly Trend')
plt.show()

# Hourly trend over the weekend and workingdays
fig, ax = plt.subplots(2, 1)
working_day = df[df.weekday == 1].groupby('hour', as_index=False)[['registered', 'casual']].sum()
weekends = df[df.weekday == 0].groupby('hour', as_index=False)[['registered', 'casual']].sum()
ax[0].plot(working_day['hour'], working_day[['registered', 'casual']], marker='v', linestyle='--')
ax[0].set_title('Hourly Working Days Rides')
ax[1].plot(weekends['hour'], weekends[['registered', 'casual']], marker='v', linestyle='--')
ax[1].set_title('Hourly Weekends Rides')
# Weekdays and weekends have opposite trends. But the increase of casual is remarkable during the weekend

# What's the relationship with the weather?
sns.heatmap(df[['temp', 'windspeed', 'humidity', 'registered', 'casual']].corr(), annot=True)
plt.title('Heatmap of Rides in Correlation with Weather')
# The casual riders seem to be more sensitive to temperature. As it increases, also casual riders are more around.

# Creating 2 dimensional table and joining to use labels instead of sk.

# Create Dim Tables to merge #
dim_season = pd.DataFrame({'season_sk': [1, 2, 3, 4],
                           'season': ['winter', 'spring', 'summer', 'fall']})

dim_weather = pd.DataFrame({'weather_sk': [1, 2, 3, 4],
                            'weather': ['Few clouds, Partly cloudy, Partly cloudy',
                                        'Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist',
                                        'Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds',
                                        'Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog']})

# Join the 2 tables on sk
data = df.merge(dim_season, how='inner', on='season_sk').merge(dim_weather, how='inner', on='weather_sk')

bike_share = data[
    ['instant', 'date', 'year', 'year_season', 'month_int', 'hour', 'holiday', 'weekday', 'workingday', 'season',
     'weather', 'temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered', 'totals']]

# Export cleaned data set as csv file and plot it into Tableau
wf = bike_share.to_csv(path + 'bike_sharing_wf.csv',
                       header=True,
                       index=False)


from functools import partial
import os
from argparse import ArgumentParser
import numpy as np
import open3d as o3d

from utils.dyn_feasibility_check import check_dyn_feasible, check_dyn_feasible_parallel, simple_check_dyn_feasible
from utils.kin_feasibility_check import check_kin_feasible, check_kin_feasible_parallel
import model.manipulation_obj_creator as creator

DIFFUSION_RADIUS = 0.03
NUM_STEPS = 10
NUM_NEIGHBOR_SUPPORT=1

def load_seeds(seed_folder, use_scale=False):
    """
    seeds: grasp seed have only finger tip pose, no normal vector
    """
    files = os.listdir(f"data/{seed_folder}/pointclouds")
    files.sort(key=lambda x: int(x[5:7]))
    pcds = []
    for file in files:
        pcd = o3d.io.read_point_cloud(f"data/{seed_folder}/pointclouds/{file}")
        assert(pcd.has_normals())
        pcds.append(pcd)

    files = os.listdir(f"data/{seed_folder}/grasps")
    files.sort(key=lambda x: int(x[5:7]))
    all_grasps = []
    for file in files:
        grasps = np.load(f"data/{seed_folder}/grasps/{file}")
        if len(grasps.shape) == 2:
            grasps = grasps.reshape(1,grasps.shape[0],grasps.shape[1])
        all_grasps.append(grasps[:,:,:3])
    if use_scale:
        files = os.listdir(f"data/{seed_folder}/scales")
        files.sort(key=lambda x: int(x[5:7]))
        all_scales = []
        for file in files:
            scale = np.load(f"data/{seed_folder}/scales/{file}")
            all_scales.append(scale)
        return pcds, all_grasps, all_scales
    else:
        return pcds, all_grasps

def randomize_sample_trajectory(points, normals, kd_tree, seed, depth=8):
    """
    seed: only finger tip pose no normal vector
    """
    init_grasp = np.zeros((seed.shape[0], seed.shape[1] + 3))
    for i, point in enumerate(seed):
        init_idx = kd_tree.search_knn_vector_3d(point, 1)[1]
        init_grasp[i,:3] = points[init_idx]
        init_grasp[i,3:] = normals[init_idx]

    grasps_traj = [init_grasp]
    for i in range(depth):
        new_grasp = np.zeros_like(grasps_traj[-1])

        for i, point in enumerate(grasps_traj[-1]):
            indices = np.array(list(kd_tree.search_radius_vector_3d(point[:3], DIFFUSION_RADIUS)[1]))
            choice = np.random.choice(indices,1)
            new_grasp[i,:3] = points[choice]
            new_grasp[i,3:] = normals[choice]
        grasps_traj.append(new_grasp)
    return grasps_traj

def project_to_pcd(points, normals, grasp, kd_tree, num_neighbors):
    """
    grasp: finger tip pose and normal vector
    """
    new_poses=[]
    for i in range(len(grasp)):
        idx = kd_tree.search_knn_vector_3d(grasp[i,:3],num_neighbors)[1]
        new_poses.append(np.hstack([points[idx], normals[idx]]))
    if len(grasp) < 4:
        new_poses.append(np.array([[100, 100, 100, 0, 0, 0]] * num_neighbors)) # Ring finger
    return np.vstack(new_poses)

def check_traj_feasibility(grasps_traj, points, normals, kd_tree, num_neighbors=3, hand_model="allegro", env="laptop",scale=np.array([1,1,1])):
    """
    Assume grasps_traj have finger tip pose and normal vector
    For any small box object, use env == "laptop"
    """
    feasible_grasps = []
    infeasible_grasps = []
    for grasp in grasps_traj:
        tip_poses = project_to_pcd(points, normals, grasp, kd_tree, num_neighbors)
        flag = simple_check_dyn_feasible(tip_poses[:,:3], tip_poses[:,3:])
        print("Dyn:",flag)
        obj_creator = partial(creator.object_creators[env], scale=scale)
        if flag == False:
            infeasible_grasps.append(grasp)
        elif not check_kin_feasible_parallel(np.vstack([grasp[:,:3], np.array([100,100,100])]),
                                             np.vstack([grasp[:,3:], np.array([0,0,0])]), hand_model=hand_model,object_creator=obj_creator)[0]:
            infeasible_grasps.append(grasp)
        else:
            feasible_grasps.append(grasp)
    return feasible_grasps, infeasible_grasps

def main(args):
    if args.has_scale:
        pcds, all_grasps, all_scales = load_seeds(args.seed_folder, args.has_scale)
    else:
        pcds, all_grasps = load_seeds(args.seed_folder, args.has_scale)
    print(f"Seed Loaded: {len(pcds)} point clouds")
    for i, (pcd, grasps) in enumerate(zip(pcds, all_grasps)):
        if i < args.start_idx  or (args.use_idx is not None and str(i) not in args.use_idx):
            continue
        print(f"Seed {i} Loaded: {len(grasps)} grasps")
        kd_tree = o3d.geometry.KDTreeFlann(pcd)
        points = np.asarray(pcd.points)
        normals = np.asarray(pcd.normals)
        feasible_grasps_this_pose = []
        infeasible_grasps_this_pose = []
        for seed_grasp in grasps:
            grasps_traj = randomize_sample_trajectory(points, normals, kd_tree, seed_grasp, depth=NUM_STEPS)
            feasible_grasps, infeasible_grasps = check_traj_feasibility(grasps_traj, points, normals, kd_tree, num_neighbors=NUM_NEIGHBOR_SUPPORT, hand_model=args.hand_model,
                                                                        env=args.env, scale=all_scales[i] if args.has_scale else np.array([1,1,1]))
            feasible_grasps_this_pose += feasible_grasps
            infeasible_grasps_this_pose += infeasible_grasps
        print(f"Finish {i} pcd, total feasible grasps: {len(feasible_grasps_this_pose)} infeasible grasps {len(infeasible_grasps_this_pose)}")
        if len(feasible_grasps_this_pose) != 0:
            np.save(f"data/{args.seed_folder}/processed_data/pose_{i:02}.npy", np.asarray(feasible_grasps_this_pose))
        np.save(f"data/{args.seed_folder}/processed_data/infeasible_pose_{i:02}.npy", np.asarray(infeasible_grasps_this_pose))
    print("Processed data has been saved")

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--exp_name", type=str, default="default")
    parser.add_argument("--hand_model", type=str, default="shadow")
    parser.add_argument("--seed_folder", type=str, default="seeds")
    parser.add_argument("--env",type=str,default="laptop")
    parser.add_argument("--has_scale",action="store_true", default=False)
    parser.add_argument("--start_idx", type=int, default=0)
    parser.add_argument('--use_idx','--list', action='append')
    args = parser.parse_args()
    main(args)


import os
import random
from collections import defaultdict
import nltk
from nltk.util import ngrams

# Step 1: Import necessary libraries
nltk.download('punkt')

# Step 2: Read and preprocess the text from the files
def read_text_files(folder_path):
    text_corpus = []
    for filename in os.listdir(folder_path):
        if filename.endswith('.txt'):
            with open(os.path.join(folder_path, filename), 'r', encoding='utf-8') as file:
                text = file.read()
                text = text.lower()  # Convert to lowercase
                text_corpus.extend(nltk.sent_tokenize(text))
    return text_corpus

# Step 3: Create bigram and trigram models
def create_ngram_models(text_corpus, n):
    ngram_model = defaultdict(list)

    for sentence in text_corpus:
        words = nltk.word_tokenize(sentence)
        ngrams_list = list(ngrams(words, n, pad_left=True, pad_right=True))

        for ngram in ngrams_list:
            ngram_model[ngram[:-1]].append(ngram[-1])

    return ngram_model

folder_path = 'C:/Users/Lenovo/OneDrive/Desktop/IR/dhruv'  # Update with your folder path
text_corpus = read_text_files(folder_path)
bigram_model = create_ngram_models(text_corpus, 2)
trigram_model = create_ngram_models(text_corpus, 3)

# Step 4: Generate random text using the models
def generate_random_text(model, n, num_sentences=5, max_length=100):
    generated_text = []

    for _ in range(num_sentences):
        sentence = []
        current_ngram = [None] * (n - 1)

        while True:
            next_word = random.choice(model[tuple(current_ngram)])
            if next_word is None:
                break
            sentence.append(next_word)
            current_ngram = current_ngram[1:] + [next_word]
            if len(sentence) >= max_length:
                break

        generated_text.append(' '.join(sentence))

    return '\n'.join(generated_text)

# Generate random bigram text
random_bigram_text = generate_random_text(bigram_model, 2)

# Generate random trigram text
random_trigram_text = generate_random_text(trigram_model, 3)

print("Random Bigram Text:")
print(random_bigram_text)

print("\nRandom Trigram Text:")
print(random_trigram_text)


# -*- coding: utf-8 -*-
"""
Created on Fri May 19 07:24:06 2017

@author: manuj
"""
import datetime
from django.utils import timezone
from dateutil.parser import parse
import datefinder
import difflib

import time
import numpy as np
import pandas as pd

from MovieScraper import MovieScraper
from MovieListCleaner import MovieListCleaner
from .models import Movie
from django.db.models import Max
from django.db.models import Min
from django.utils import timezone
import imdb
import re
from imdb._exceptions import IMDbDataAccessError, IMDbParserError

class Utils:

    MAX_SCRAP_PAGES = 100
    INIT_SCRAP_TIME = 30 #days

    @classmethod
    def convert_timezone_aware(self,x):
        if isinstance(x,datetime.datetime):
            return timezone.make_aware(x)
        else:
            return x

    # @transaction.atomic
    @classmethod
    def scrape_and_add_movies(self):
        cut_off_year = 2015
        #Actions to be done here
        if Movie.objects.all().count()>0:
            post_max_date = timezone.make_naive(Movie.objects.latest('post_date').post_date)
        else:
            post_max_date = datetime.datetime.now() - datetime.timedelta(self.INIT_SCRAP_TIME)
        movie_scraped = MovieScraper()
        movie_scraped.scrape_site(self.MAX_SCRAP_PAGES, post_max_date)
        if len(movie_scraped.movieScraped.index) >0 :
            movie_clean = MovieListCleaner(movie_scraped.movieScraped)
            movie_clean.clean_movie()
            movie_df = movie_clean.cleanMovieList
            print("scrape complete")
            #rename dataframe to match model
            cols = [ 'name','year', 'genre', 'imdb_rating', 'imdb_votes','rt_critics','plot', 'starring','director',
                'imdb_link','rt_link', 'post_link','release_name', 'release_type', 'release_date','thumbnail_link',
                'date_time','trailer_link', 'tomatometer','rt_rating','post_date']
            movie_df.columns = cols
            #print(movie_df.name)
            movie_df.replace(r'^\s+$', np.nan, regex=True, inplace=True)
            #Making Date-time Timezone aware
            movie_df['release_date'] = movie_df['release_date'].map(self.convert_timezone_aware, na_action = 'ignore')
            movie_df['date_time'] = movie_df['date_time'].map(self.convert_timezone_aware, na_action = 'ignore')
            movie_df['post_date'] = movie_df['post_date'].map(self.convert_timezone_aware, na_action = 'ignore')
            #Make Key
            movie_df['key'] = movie_df[['name','year', 'genre', 'imdb_rating', 'imdb_votes','rt_critics','plot', 'starring','director',
                'imdb_link','rt_link', 'post_link','release_name', 'release_type', 'release_date','thumbnail_link',
                'trailer_link', 'tomatometer','rt_rating','post_date']].apply(lambda row: ','.join(map(str, row)), axis=1)
            movie_df['year'] = pd.to_numeric(movie_df['year'], errors='coerce')
            movie_df.dropna(subset='year',inplace=True)
            movie_df = movie_df.loc[movie_df.year>=cut_off_year]
            #print(movie_df.name)
            # print(movie_df.key.count())
            # print(len(movie_df.key.unique()))
        else:
            movie_df = pd.Dataframe()
        movie_dict = movie_df.to_dict('records')
        #replacing empty strings with None
        counter = 0
        for movie in movie_dict:
            # print(timezone.make_naive(movie['post_date']))
            # print(timezone.make_naive(movie['post_date']) <= post_max_date)
            if isinstance(movie['post_date'], datetime.datetime):
                if timezone.make_naive(movie['post_date']) <= post_max_date:
                    # print("Max post date")
                    if not(Movie.objects.filter(key=movie['key']).exists()):
                        # print("new-saving")
                        m = Movie(**movie)
                        m.save()
                        counter = counter+1
                else:
                    m = Movie(**movie)
                    m.save()
                    counter = counter+1

        return counter

    # def update_all_ratings():
    #     #for debug only
    #     movies = Movie.objects.filter(movie_read = False)
    #     # Create the object that will be used to access the IMDb's database.
    #     ia = imdb.IMDb() # by default access the web.
    #     counter = 0
    #     for movie in movies:
    #         print("movie name: {}".format(movie.name))
    #         print("movie link: {}".format(movie.imdb_link))
    #         movie_info, imdb_link_present = get_imdb_info(movie.imdb_link,movie.name,ia)
    #         if movie_info:
    #             if 'rating' in movie_info.keys():
    #                 print ("new rating: {} <--- old rating: {}".format(movie_info['rating'],movie.imdb_rating))
    #             else:
    #                 print("no ratings")
    #             if 'votes' in movie_info.keys():
    #                 print ("new votes: {} <--- old votes: {}".format(movie_info['votes'],movie.imdb_votes))
    #             else:
    #                 print ("no votes")
    #             counter = counter +1
    #             #For Debug only
    #             # movie.movie_read = True
    #             # movie.save()
    #     return counter

    # @transaction.atomic
    @classmethod
    def update_all_ratings(self):
        movies = Movie.objects.filter(movie_read = False)
        print("in update: count of movie {}".format(movies.count()))
        # Create the object that will be used to access the IMDb's database.
        ia = imdb.IMDb() # by default access the web.
        counter = 0
        for movie in movies:
            print("movie name: {}".format(u''.join(movie.name).encode('utf-8').strip()))
            print("movie link: {}".format(u''.join(movie.imdb_link).encode('utf-8').strip()))
            movie_info, imdb_link_present = self.get_imdb_info(movie.imdb_link,movie.name,ia)
            if movie_info:
                if 'rating' in movie_info.keys():
                    movie.imdb_rating = movie_info['rating']
                if 'votes' in movie_info.keys():
                    movie.imdb_votes = movie_info['votes']
                if 'plot' in movie_info.keys():
                    movie.plot = movie_info['plot']
                if not imdb_link_present:
                    movie.imdb_link = ia.get_imdbURL(movie_info)
                movie.datetime = timezone.make_aware(datetime.datetime.now())
                movie.save()
                print ("movie rating: {}".format(movie.imdb_rating))
                print ("movie votes: {}".format(movie.imdb_votes))
                counter = counter +1
        return counter

    @classmethod
    def get_imdb_info(self,imdb_link,imdb_name,ia):
        match = re.search(r'^https?://[^\s]+/[^\s]+/tt([^\s]+)$', imdb_link.strip('/'))
        if match is not None:
            id = match.group(1)
            try:
                movie = ia.get_movie(id)
                return movie, True
            except IMDbParserError as e:
                result_set = ia.search_movie(imdb_name)
                if len(result_set)>0:
                    return ia.search_movie(imdb_name)[0], False
                else:
                    return False, False
        else:
            result_set = ia.search_movie(imdb_name)
            if len(result_set)>0:
                return ia.search_movie(imdb_name)[0], False
            else:
                return False, False

    @classmethod
    def remove_duplicates_in_db(self):
        df = pd.DataFrame(list(Movie.objects.all().values()))
        print ("Total Number of Movies: {}".format(len(df)))
        movie_cleaner = MovieListCleaner(df)
        df = movie_cleaner.remove_duplicates_model(df)
        id_list = df.id.tolist()
        duplicates = Movie.objects.exclude(id__in = id_list).delete()
        #Movie query to delete all ids which are not in this list
        # print(df.id)
        return duplicates[0]

    # @commit_on_success
    @classmethod
    def update_release_type(self):
        movies = Movie.objects.filter(release_type="")
        movie_scraper = MovieScraper()
        for movie in movies:
            movie.release_type = movie_scraper.find_release_type_from_name(movie.release_name)
            movie.save()
        return movies.count()


'''
An efficient program to solve loopover
'''
from collections import deque
def SOLVED(scrambled, solved, INVERSE):
    scrambled = list(map(lambda x: list(map(lambda y:INVERSE[tuple(y)], x)), scrambled))
    return scrambled == solved
def parity(board):
    Q = [[1 if board[i][j]!=[i, j] else 0 for j in range(len(board[0]))] for i in range(len(board))]
    if sum(sum(i) for i in Q) != 2:
        return "Fucked up"
    if Q[-1][-1] == 1:
        if Q[-2][-1] == 1:
            return 'V'
        if Q[-1][-2] == 1:
            return 'H'
    return "O"
def loopover(scrambled, solved):
    print(scrambled, solved)
    MAP = {solved[i][j]:[i, j] for j in range(len(solved[0])) for i in range(len(solved))}
    INVERSE = {tuple(j): i for i, j in MAP.items()}
    scrambled = list(map(lambda x: list(map(lambda y:MAP[y], x)), scrambled))
    to_find = [0, 0]
    current = [0, 0]
    H, W = len(scrambled), len(scrambled[0])

    RS = []
    if SOLVED(scrambled, solved, INVERSE):
        return RS
    #Perform up/down movement in matrix
    #c is number of column in which the movement is required
    #n is number of moves required
    def ud(matrix, c, n):
        nonlocal RS
        if n < 0:
            RS += [f'{"U"}{c}']*abs(n)
        elif n > 0:
            RS += [f'{"D"}{c}']*abs(n)
        column = deque([i[c] for i in matrix])
        column.rotate(n)
        for i in range(len(matrix)):
            matrix[i][c] = column.popleft()
        return matrix

    #Performs left/right movement in matrix
    #r is the number of row in which the movement is required
    #n is number of moves required, which can be positive or negative
    def lr(matrix, r, n):
        nonlocal RS
        if n > 0:
            RS += [f'{"R"}{r}']*abs(n)
        elif n < 0:
            RS += [f'{"L"}{r}']*abs(n)
        row = deque(matrix[r])
        row.rotate(n)
        matrix[r] = list(row)
        return matrix

    def finder(to_find, mode = 'H'):
        for i in range(H):
            for j in range(W):
                if scrambled[i][j] == to_find:
                    if mode == 'H':
                        if W - 1 == to_find[1]:
                            to_find[0], to_find[1] = to_find[0] + 1, 0
                        else:
                            to_find[1] = to_find[1] + 1
                    else:
                        to_find[0] += 1
                    return [i, j]

    while to_find[1] < W - 1:
        location = finder(to_find)
        if location[0] == current[0]:
            location[0] += 1
            scrambled = ud(scrambled, location[1], 1)

        scrambled = lr(scrambled, location[0], current[1] - location[1])

        location[1] = current[1]
        scrambled = ud(scrambled, location[1], current[0] - location[0])

        current = to_find[::]

    to_find = [1, 0]
    current = [1, 0]

    while to_find[0] < H - 1:
        location = finder(to_find)
        if location[0] == current[0]:
            temp = W - 1 - location[1]
            scrambled = lr(scrambled, location[0], temp)
            location[1] = W - 1
            scrambled = ud(scrambled, W - 1, 1)
            location[0] += 1
            scrambled = lr(scrambled, current[0], -temp)

        if location[0] != current[0] + 1 and location[1] == W - 1:
            scrambled = ud(scrambled, location[1], current[0] + 1 - location[0])
            location[0] = current[0] + 1

        scrambled = lr(scrambled, location[0], current[1] - location[1])

        for each_row in range(current[0]):
            scrambled = lr(scrambled, each_row, current[1] - W + 1)
        scrambled = ud(scrambled, current[1], current[0] - location[0])
        for each_row in range(current[0]):
            scrambled = lr(scrambled, each_row, -(current[1] - W + 1))

        current = to_find[::]

    if SOLVED(scrambled, solved, INVERSE):
        return RS

    to_find = [0, W - 1]
    current = [0, W - 1]

    while to_find[0] < H - 1:
        location = finder(to_find, 'W')
        if location[0] == H - 1:
            scrambled = lr(scrambled, H - 1, W - 1 - location[1])
            location = [H - 1, W - 1]
        saved = -location[0] - 1
        scrambled = ud(scrambled, W - 1, saved)
        location = [H - 1, W - 1]
        scrambled = lr(scrambled, H - 1, -1)
        location = [H - 1, W - 2]
        scrambled = ud(scrambled, W - 1, -saved)
        scrambled = lr(scrambled, H - 1, 1)
        scrambled = ud(scrambled, W - 1,-1)

    scrambled = lr(scrambled, H - 1, -finder([H - 1, 0])[1])
    if SOLVED(scrambled, solved, INVERSE):
        return RS

    #Assuming that now the whole board except the last row is solved
    temp = [H - 1, 0]
    location = finder(temp)
    scrambled = lr(scrambled, H - 1, -location[1])
    scrambled = ud(scrambled, W - 1, 1)
    scrambled = lr(scrambled, H - 1, 1)
    scrambled = ud(scrambled, W - 1, -1)
    #^^ This is setup part for solving the last row(I think(but I can be wrong))
    K = [H - 1, 1]
    for i in range(W-2):
        K_D = K[::]
        location = finder(K)
        if location == [H - 2, W - 1]:
            last = [K_D[0], K_D[1] - 1]
            location_last = finder(last[::])
            scrambled = lr(scrambled, H - 1, W - 1 - location_last[1] - 1)
            scrambled = ud(scrambled, W - 1, 1)
            if K_D != [H - 1, W - 2]:
                scrambled = lr(scrambled, H - 1, - 1)
            else:
                scrambled = lr(scrambled, H - 1, W - 2 - finder(K_D)[1])
            scrambled = ud(scrambled, W - 1, -1)
        elif location[0] == H - 1:
            last = [K_D[0], K_D[1] - 1]
            scrambled = lr(scrambled, H - 1, W - 1 - location[1])
            scrambled = ud(scrambled, W - 1, 1)
            location_last = finder(last[::])
            scrambled = lr(scrambled, H - 1, W - 1 - location_last[1] - 1)
            scrambled = ud(scrambled, W - 1, -1)
            scrambled = lr(scrambled, H - 1, -1)

    scrambled = lr(scrambled, H - 1, -finder([H - 1, 0])[1])
    if SOLVED(scrambled, solved, INVERSE):
        return RS
    P = parity(scrambled)

    if P == 'V' and len(scrambled) % 2 == 1:
        scrambled = lr(scrambled, H - 1,  1)
        scrambled = ud(scrambled, W - 1,  1)
        scrambled = lr(scrambled, H - 1, -1)
        scrambled = ud(scrambled, W - 1, -1)
    P = parity(scrambled)
    if P == 'V':
        scrambled = lr(scrambled, H - 1, -1)
        scrambled = ud(scrambled, W - 1,  1)
        scrambled = lr(scrambled, H - 1,  1)
        scrambled = ud(scrambled, W - 1, -1)
        scrambled = lr(scrambled, H - 1, -1)
        scrambled = lr(scrambled, H - 1,  1)
        scrambled = ud(scrambled, W - 1, -1)
        to_find = [H - 1, W - 1]
        while 1:
            location = finder(to_find, 'W')
            if location[1] == W - 1:
                scrambled = ud(scrambled, W - 1, -location[0] - 1)
                scrambled = lr(scrambled, H - 1, -1)
                scrambled = ud(scrambled, W - 1, location[0] + 1)
                scrambled = lr(scrambled, H - 1, 1)
                scrambled = ud(scrambled, W - 1, -1)
            elif location[0] == H - 1:
                scrambled = lr(scrambled, H - 1, W - 1 -location[1])
                scrambled = ud(scrambled, W - 1, -1)
            if to_find[0] == H:
                to_find = [0, W - 1]
            if to_find[0] == H - 1:
                break
    elif P == 'H':
        scrambled = ud(scrambled, W - 1, -1)
        scrambled = lr(scrambled, H - 1,  1)
        scrambled = ud(scrambled, W - 1,  1)
        scrambled = lr(scrambled, H - 1, -1)
        scrambled = ud(scrambled, W - 1, -1)
        scrambled = ud(scrambled, W - 1,  1)
        scrambled = lr(scrambled, H - 1, -1)
        to_find = [H - 1, W - 1]
        while 1:
            print(to_find)
            location = finder(to_find)
            if location[0] == H - 1:
                scrambled = lr(scrambled, H - 1, -location[1] - 1)
                scrambled = ud(scrambled, W - 1, -1)
                scrambled = lr(scrambled, H - 1, location[1] + 1)
                scrambled = ud(scrambled, W - 1, 1)
                scrambled = lr(scrambled, H - 1, -1)
            elif location[1] == W - 1:
                scrambled = ud(scrambled, W - 1, H - 1 -location[0])
                scrambled = lr(scrambled, H - 1, -1)
            if to_find[0] == H:
                to_find = [H - 1, 0]
            if to_find[1] == W - 1:
                break

        for i in scrambled:
            print(i)

    if SOLVED(scrambled, solved, INVERSE):
        return RS
    return


import matplotlib.pyplot as plt
import visuals
import switch_functions as switch
import looper
import malus_calc
import numpy as np

"""
These are the used functions. For instructions on how to use them navigate to the README.md

solution = switch.single_loop():

switch.activity_switch_emptyslot(seconds,timeout_amount,change_amount, solution)

switch.activity_switch(seconds,timeout_amount,change_amount, solution)

switch.student_switch_emptyslot(seconds,timeout_amount,change_amount, solution)

switch.student_switch(seconds,timeout_amount,change_amount, solution)

looper.loop_tuner(ac_ac,ac_emp,stud_stud,stud_emp, run_time)
"""

# This is the looper function, comment out if using a single switch
# change variables to your liking
seconds = 60
ac_ac = 25
ac_emp = 25
stud_stud = 75
stud_emp = 75
week_data,student_data,activity_list,total_changes,total_attempts, malus_list_changes, malus_list_tries = looper.loop_tuner(ac_ac,ac_emp,stud_stud,stud_emp, seconds)

# this is the single switch, comment out if using the looper
# Change the function with any of the single switch functions found at the top of this file
# change variables to your liking
"""
seconds = 30
timeout_amount = -1
change_amount = -1
solution = switch.single_loop()
malus_list_2,change_tries,succesfull_changes,malus_list = switch.student_switch(seconds,timeout_amount,change_amount, solution)
"""

# This will print the final maluscount of our solution aqquired from the looper of switch function.
print("final malus count: {}".format(malus_calc.malus_calc(student_data,activity_list)))

# this will print our schedule
visuals.plotter(week_data)

# this will plot the total changes and the total change attempts on the x axis and the malus points on the y
# this is a result of the looper function, comment this out if you are not using the looper function.
plt.plot(np.arange(total_changes), malus_list_changes)
plt.plot(np.arange(total_attempts), malus_list_tries)
plt.title(f"{seconds} seconds of the combined switches")
plt.xlabel("Amount of total switch attempts")
plt.ylabel("Maluspoints")
plt.show()

# this will plot the total changes and the total change attempts on the x axis and the malus points on the y
# this is a result of the single switich functions, comment this out if you are using the looper function.
"""
plt.plot(np.arange(succesfull_changes), malus_list)
plt.plot(np.arange(change_tries), malus_list_2)
plt.title(f"{seconds} seconds of the single switches")
plt.xlabel("Amount of total switch attempts")
plt.ylabel("Maluspoints")
plt.show()
"""


#Giải mã base64 --> ảnh
from base64 import b64decode

#print(b64decode('AQID'))
f = open('imagebase64.txt')
b64data = f.read()
f.close()

data = b64decode(b64data)
print(len(data))

f = open('out.png', 'wb')
f.write(data)
f.close()


from os import path
from setuptools import setup, find_packages

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.md'), encoding='utf-8') as readme_file:
    readme = readme_file.read()

requirements = [
    'click'
]

test_requirements = [
]

setup(
    name='intercom',
    version='0.9',
    description="Distance finder for intercom",
    long_description=readme,
    author="Batuhan Ceylan",
    author_email='batuhan@batuhanceylan.com',
    packages=find_packages(exclude=['tests']),
    entry_points={
        'console_scripts': [
            'intercom=intercom.intercom:intercom',
            ],
        },
    include_package_data=True,
    install_requires=requirements,
    license="MIT",
    classifiers=[
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.8',
    ],
    test_suite='tests',
    tests_require=test_requirements,
)


'''
Created on 2019年4月8日

@author: bkd
'''
from traceback import format_exception
import sys
from PyQt5.QtWidgets import QMessageBox

class global_exception_hander:
    def new_except_hook(self,etype, evalue, tb):
        print(''.join(format_exception(etype, evalue, tb)))
        QMessageBox.information(None,
                                      str('error'),
                                      ''.join(format_exception(etype, evalue, tb)))
        sys.exit()

#     注册全局异常处理类
    def patch_excepthook(self):
        sys.excepthook = self.new_except_hook


from sqlalchemy import Column, MetaData, Table, Integer, String, DateTime
from sqlalchemy.sql import func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import JSON

Base = declarative_base()

class VcmsFeaturePklFile(Base):
    __tablename__ = 'feature_pkl_files'

    sn = Column(Integer, primary_key=True)
    company_sn = Column(Integer, default=0)
    service_sn = Column(Integer, default=0)
    branch_sn = Column(Integer, default=0)
    pkl_key = Column(String)
    pkl_update_send_mark = Column(Integer, default=0)
    pkl_update_send_time = Column(DateTime, nullable=True)
    pkl_update_finish_mark = Column(Integer, default=0)
    pkl_update_finish_time = Column(DateTime, nullable=True)
    output_pkl_mark = Column(Integer, default=0)
    output_pkl_time = Column(DateTime, nullable=True)
    ct_user_sn = Column(Integer, default=0)
    ut_user_sn = Column(Integer, default=0)
    enabled = Column(Integer, default=1)
    ct = Column(DateTime, default=func.now())
    ut = Column(DateTime, nullable=True, onupdate=func.now())
    deprecated = Column(Integer, default=0)

    def __init__(self,company_sn = 0,
                      service_sn = 0,
                      branch_sn = 0,
                      pkl_key = None,
                      pkl_update_send_mark = 0,
                      pkl_update_send_time = None,
                      pkl_update_finish_mark = 0,
                      pkl_update_finish_time = None,
                      output_pkl_mark = 0,
                      output_pkl_time = None,
                      ct_user_sn = 0,
                      ut_user_sn = 0,
                      enabled = 1,
                      ct = None,
                      ut = None,
                      deprecated = 0) :
        self.company_sn = company_sn
        self.service_sn = service_sn
        self.branch_sn = branch_sn
        self.pkl_key = pkl_key
        self.pkl_update_send_mark = pkl_update_send_mark
        self.pkl_update_send_time = pkl_update_send_time
        self.pkl_update_finish_mark = pkl_update_finish_mark
        self.pkl_update_finish_time = pkl_update_finish_time
        self.ct_user_sn = ct_user_sn
        self.ut_user_sn = ut_user_sn
        self.enabled = enabled
        self.ct = ct
        self.ut = ut
        self.deprecated = deprecated


from asyncio import tasks
from airflow import DAG
from datetime import datetime, timedelta
from airflow.operators.bash_operator import BashOperator

default_args = {
    'owner': 'airflow',
    'retries': 2,
    'retry_delay': timedelta(minutes=2)
}

with DAG(
    dag_id='my_first_dag',
    default_args=default_args,
    description='My first DAG is in testing mode',
    start_date=datetime(2022, 8, 3, 2),
    schedule_interval='@daily'

) as dag:
    task1 = BashOperator(
        task_id='task_first',
        bash_command="echo Hello World"
    )

    task2 = BashOperator(
        task_id='task_second',
        bash_command="echo Hello People"
    )

    task3 = BashOperator(
        task_id='task_three',
        bash_command="echo I am partner of task_first"
    )

    task4 = BashOperator(
        task_id='task_four',
        bash_command="echo I am partner of task_second"
    )

    task5 = BashOperator(
        task_id='task_five',
        bash_command="python /opt/airflow/landings/hello_landing.py"

    )

    # Task dependencies method 1
    # task1.set_downstream(task2)
    # task1.set_downstream(task3)
    # task2.set_upstream(task4)

    # Task dependencies method 2
    task1 >> task2 >> task3
    task2 >> task4
    task1 >> task5

    # Task dependencies method 3
    # task1 >> [task2, task3]
    # task2 >> [task4]


# Importing necessary files
import os
import discord
from dotenv import load_dotenv
import logging.handlers

# .env
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('GUILD_ID')

# Set up logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
logging.getLogger('discord.http').setLevel(logging.INFO)

handler = logging.handlers.RotatingFileHandler(
    filename='discord.log',
    encoding='utf-8',
    maxBytes=32 * 1024 * 1024,  # 32 MiB
    backupCount=5,  # Rotate through 5 files
)
dt_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', dt_fmt, style='{')
handler.setFormatter(formatter)
logger.addHandler(handler)

# Intents are like permissions; groups of events
intents = discord.Intents.default() # use the default set of intents
intents.message_content = True # allow the message_content events into our app

# load a new discord client with the intents we specified above
client = discord.Client(intents=intents)

# when the on_ready event is triggered,
@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'BOT({client.user}) Successfully Connected To Guild: {guild.name} (id: {guild.id})'
    )

# when the on_message event is triggered,
@client.event
async def on_message(message):
    if message.author == client.user: # stop the bot from messaging itself
        return

    if message.content.startswith('$hello'): # basic $hello command
        await message.channel.send('Hello!') # returns "Hello!"

client.run(TOKEN, log_handler=None) # run the discord client/bot using our client token (on dev portal)


import matplotlib.pylab as plt
import numpy as np
import scipy
from scipy import interpolate
import sys
import os

from linetools.isgm.abscomponent import AbsComponent
import json
from pyigm.guis import igmguesses
from linetools.spectra.io import readspec

import spectrum_analysis_tools as spa

# inwave = REST wavelength for rest-frame EW; observed wavelength for observed EW
# inspec = flux
# inerror = error
# vrange = [vmin, vmax] velocity range for integration
# w0 = rest wavelength of transition
# f0 = fval of transition
# limit = how many sigma limit do we want, for limits?
# norm = the flux value to normalise the spectrum by
# nstapix -- the number of pixels that need to be below the saturation limit or error before we trigger the saturation flag.
#
# Keywords
# ctnorm -- normalise the spectra by the median flux in +/-600km/s (excluding the line region)
#
# OUTPUTS
# EW -- 2-element array of eqw and eqwerror.
# colm -- 2-element array of linear column density and error
#
# Note -- Return 2-element arrays with error included, since 2 keywords eqw and eqwerr are
#         ambiguous to IDL and so not allowed.
#inspec
# REVISION HISTORY
#
def deriv(x):
#	return scipy.misc.derivative(x)
	return np.diff(np.append(x, x[-1]))

def ct_ew2n(ew, wrest, fval):
	# assume optically thin case, and compute column density from EW. See, e.g. Savage & Sembach (1996) eqn 3
	# EW in mA
	# wrest in A
	# fval is from morton
#return 1.13d17*ew/(fval * wrest**2)
	return 1.13e17*ew/(fval * wrest**2)

def eqwrange(ion, wave, spec, error, vrange, w0, f0, \
              limit = 1., norm = 1.0, ctnorm = 1, nsatpix = 3, \
              silent = 0, plots = 0, sig_limit = 3, sat_limit = 0.1, \
	      plot_dir = '.'):

	### DEFAULT SETTING FOR FLAG. 1 = ok.
	flag = 1

	if norm:
  		spec = spec / norm
  		error = error / norm

	if ctnorm:
		#grab the median flux value in the +/-600km/s range, outside the line area
		vel = (wave-w0)/w0*2.9979e5
		qq = ((vel > -1200) & (vel < vrange[0])) | ((vel > vrange[1]) & (vel < 1200))
		med = np.median(spec[qq])
		spec  /= med
		error /= med
	else:
		print('warning - assumes data and errors are already continuum normalized')

	# convert wavelengths to velocty
	vv = (wave-w0) / w0 * 2.9979e5
	# find points in range

	# get rid of infinity values down the line
	# note: results sensitive to the value we set spec to
	# sort of converges but not really
	spec[spec <= 0] = 0.01
	iv = (vv > vrange[0]) & (vv < vrange[1])
	if len(vv[iv]) == 0:
		# Something is wrong. We were given a spectrum with no points in the velocity range. Pull out!!
		print("Spectrum has no points in the velocity range")
		ew = [-999.9, -999.9]
		colm = [-999.9, -999.9]
		return

	sat = (spec[iv] < sat_limit) | (spec[iv] < error[iv])
	if len(spec[iv][sat]) > 0:
		spec[iv][sat] = error[iv][sat]
		if len(spec[iv][sat]) > nsatpix:
			flag += 8  #If there are more pixels than we set, then trip the saturated flag

	# now that we've calculated the saturation flag, we can filter spec[iv]
        # down even more to get more accurate column density estimates
#	iv =  iv & (spec > sat_limit)

	# plotting the flux vs velocities for all pixels and those used in fit
	if plots:
		fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize=(10, 5), sharex = False, sharey = False)
		ax[0].errorbar(vv, spec, yerr = error)
		ax[0].errorbar(vv[iv], spec[iv], yerr = error[iv])
		ax[0].axhline(y = 0, color = 'red', linestyle = 'dashed')
		ax[0].set_xlim(vrange[0], vrange[1])
		ax[0].set_ylim(-0.1, 1.5)
		ax[0].set_xlabel('Relative Velocity (km/s)')
		ax[0].set_ylabel('Normalized Flux')

	dwave = deriv(wave[iv])
	d_eqw = dwave * (1. - spec[iv])  # * 1000.  # why 1000?

	eqw = np.sum(d_eqw)

 	# dtermine centroid of equivalent width
	f_eqw = np.cumsum(d_eqw) / np.max(np.cumsum(d_eqw)) #cumulative eqw fraction
	# interpolate for velocity at which fraction of eqw = 0.5
	# we'll take this as the velocity centroid
	# trying something new:
	vcent = np.interp(0.5, f_eqw, vv[iv])
	#print(vcent)
#	plt.plot(vv[iv], f_eqw)
#	plt.show()
	# now ready to calculate vwidth
	# Got rid of * 1000
#        eqwerr = np.sqrt(np.sum((deriv(wave[iv])*1000)**2 \
 #                                               * (error[iv])**2 ))

#	eqwerr = np.sqrt(np.sum((deriv(wave[iv])**2  * error[iv])**2 )
	eqwerr = np.sqrt(np.sum((deriv(wave[iv])**2  * error[iv])**2) )

	EW = [eqw, eqwerr]

  	# Upper limit flag
	if eqw < sig_limit*eqwerr:
  		flag += 4

  	# Convert to strings
  	#  eqw = string(eqw, format='(F8.1)')
  	#eqwerr = string(eqwerr, format='(F8.1)')

  	#now convert data points to tau0 -> column density using input line data
	tau = -1. * np.log(spec)
	f_tau = np.cumsum(tau[iv]) / max(np.cumsum(tau[iv])) # cumulative opticaldepth fraction
	tau_cent = np.interp(0.5, f_tau, vv[iv])

	# calculate velocity width as moment of tau dist
	top = np.sum((vv[iv] - tau_cent)**2 * tau[iv] * deriv(vv[iv])) # integral of (v-vbar)^2 * tau_a(v) * dv
	bottom = np.sum(tau[iv] * deriv(vv[iv])) # integral of tau_a(v) * dv
	sigma_v = (top / bottom)**0.5 # this is sigma as defined on pg 692 of heckman et al. 2002
	velcent = tau_cent
	velwidth = np.sqrt(2.) * sigma_v

	nv = tau / 2.654e-15 / f0 / w0 # in units cm^-2 / (km s^-1), SS91
                                        # column density per unit velocity
	n = nv * deriv(vv) # column density per bin obtained by multiplying differential Nv by bin width
#	when spec[iv] == 0, tau and therefore n and col = inf
# fixed this by specifying the [iv] mask to have spec > 0
	for ii in range(len(n[iv])):
		if np.isinf(n[iv][ii]):
			print(nv[iv][ii], spec[iv][ii])

	tauerr = error / spec
	nerr = tauerr / 2.654e-15 / f0 / w0 * deriv(vv)
	if plots:
		ax[1].errorbar(vv, n,  yerr = nerr)#, drawstyle = 'steps-mid-')
		ax[1].errorbar(vv[iv], n[iv], yerr = nerr[iv])#, drawstyle = 'steps-mid-')
		ax[1].set_yscale('log')
		ax[1].set_ylim(1e10, 1e15)
		ax[1].set_xlim(-250, 250)
		ax[1].set_xlabel('Relative Velocity (km/s)')
		ax[1].set_ylabel('Column Density (cm$^{-2}$)')
		fig.tight_layout()
		plt.savefig('%s/%s_%.1f_flag_%i.png'%(plot_dir, ion, w0, flag))

	col = np.sum(n[iv])
	colerr = np.sqrt(np.sum(nerr[iv]**2))
	colm = [col, colerr]

	if not silent:
		if not limit:
			print('Direct N = %e +/- %e'%(col, colerr))
			print('Direct N = %e, +/- %e'%(np.log10(col), np.alog10(col+colerr) - alog10(col)))
			print('W_lambda = %e +/- %e mA over %s km/s'%(eqw, eqwerr, str(vrange)))
		else:
			print('Limit N = (%e) +/- %e'%(col, colerr*limit))
			print('Limit N = (%e) +/- %e'%(np.log10(np.abs(col)), np.log10(colerr * limit)))
			print('W_lambda = (%e) +/- %e mA over %s km/s'%(eqw, eqwerr*limit, str(vrange)))
	return eqw, eqwerr, np.log10(np.abs(col)), np.log10(col+colerr)-np.log10(col), flag, velcent, velwidth

def find_ion_limits(ion, fn, restwave = 0, redshift = 0, \
			    vrange = (-200, 200), silent = 0, plots = 0, plot_dir = '.', sat_limit = 0.1):
    ion_names = np.loadtxt('../../dla.lst', unpack=True, skiprows = 1, usecols=(1), dtype = 'str')
    ion_wls, ion_fs = np.loadtxt('../../dla.lst', unpack=True, skiprows = 1,usecols=(0, 3))
    ion_wls_int = ion_wls.astype(int)

    wl, flux, ferr = spa.load_spec_from_fits(fn)

    eqw_list = []
    eqwerr_list = []
    col_list = []
    colerr_list = []
    flag_list = []
    velcent_list = []
    velwidth_list = []

    ion = ion.replace(" ", "")
    if restwave == 0:
	    w0 = spa.restwave(ion, z = 0) # REST wavelength for rest-frame EW; observed wavelength for observed EW
	    w_obs = spa.restwave(ion, z = redshift)
    else:
	    w0 = restwave
	    w_obs = restwave * (redshift + 1)
    ion_mask  = (ion_names == ion)
    wl_mask =  (ion_wls_int[ion_mask] == int(w0))
    f0 = ion_fs[ion_mask][wl_mask][0]

    eqw, eqwerr, col, colerr, flag, velcent, velwidth = eqwrange(ion, wl, flux, ferr, vrange, w_obs, f0, silent=silent, \
									   plots = plots, plot_dir = plot_dir)
    eqw_list.append(eqw)
    eqwerr_list.append(eqwerr)
    col_list.append(col)
    colerr_list.append(colerr)
    flag_list.append(flag)
    velcent_list.append(velcent)
    velwidth_list.append(velwidth)

    return eqw_list, eqwerr_list, col_list, colerr_list, flag_list, velcent_list, velwidth_list

def json_eqw(json_file, fits_file, outfile, overwrite = False):
	ion_name = []; restwave = []; col = []; col_err = []; flag = []; zcomp = [];
	if not os.path.isfile(json_file):
		return np.array(ion_name), np.array(restwave), np.array(col), \
		    np.array(col_err), np.array(flag), np.array(zcomp)

	if not os.path.isfile(outfile) or overwrite == True:
		out =  open(outfile, 'w')
		out.write('#ion_name, restwave, logN, logN_err, flag_N, zcomp\n')

		with open(json_file) as data_file:
			igmg_dict = json.load(data_file)

		comp_list = []
		for ii, key in enumerate(igmg_dict['cmps'].keys()):
			comp = AbsComponent.from_dict(igmg_dict['cmps'][key], chk_sep=False, chk_data=False, chk_vel=False)
			comp_list += [comp]

		spec = readspec(fits_file)
		for i, cc in enumerate(comp_list):
			for j, al in enumerate(cc._abslines):
				al.analy['spec']=spec
				al.measure_ew()
				al.measure_aodm()

				ion_name.append(al.ion_name)
				restwave.append(al.wrest.value)
				col.append(al.attrib['logN'])
				sig_logN = al.attrib['sig_logN']
				if np.isnan(sig_logN):
					sig_logN = 0.0
				col_err.append(sig_logN)
				flag.append(al.attrib['flag_N'])
				zcomp.append(al.z)

				out.write('%s\t%f\t%e\t%e\t%d\t%0.6f\n'%(al.ion_name, al.wrest.value, \
						   al.attrib['logN'], sig_logN, al.attrib['flag_N'], al.z))
		out.close()
	if os.path.isfile(outfile) and len(open(outfile).readlines()) > 1:
		ion_name = np.loadtxt(outfile, unpack = True, skiprows = 1, usecols = 0, dtype = 'str')
		restwave, col, col_err, flag, zcomp = \
		    np.loadtxt(outfile, unpack = True, skiprows = 1, usecols =  (1, 2, 3, 4, 5))

	return np.array(ion_name), np.array(restwave), np.array(col), np.array(col_err), np.array(flag), np.array(zcomp)

def load_veeper_fit(veeper_fn):
	if os.path.isfile(veeper_fn):
		restwaves, cols, sigcols, bvals, sigbvals, vels, sigvels, zcomps = \
		    np.loadtxt(veeper_fn, unpack=True, skiprows = 1, usecols = (1,3,4,5,6,7,8, 18), delimiter = '|')
		veeper_ions, labels = np.loadtxt(veeper_fn, unpack=True, skiprows = 1, \
						 usecols = (19, 21), dtype = 'str', delimiter = '|')

		if type(veeper_ions) == np.str_:
			veeper_ions = np.array([veeper_ions]); restwaves =  np.array([restwaves]);
			cols =  np.array([cols]); sigcols =  np.array([sigcols]);
			bvals =  np.array([bvals]); sigbvals =  np.array([sigbvals]);
			vels =  np.array([vels]); sigvels =  np.array([sigvels]);
			labels =  np.array([labels]); zcomps = np.array([zcomps]);

		for i in range(len(veeper_ions)):
			temp    = veeper_ions[i]
			veeper_ions[i] = temp.replace(" ", "")

		if sigcols.size > 1:
			mask = sigcols > 0
			veeper_ions = veeper_ions[mask]
			restwaves = restwaves[mask]
			cols = cols[mask]
			sigcols = sigcols[mask]
			bvals = bvals[mask]
			sigbvals = sigbvals[mask]
			vels = vels[mask]
			sigvels = sigvels[mask]
			labels = labels[mask]
			zcomps = zcomps[mask]
	else:
                restwaves = []; cols = []; sigcols = []; bvals = []; sigbvals = [];
                vels      = []; sigvels = []; flags = []; veeper_ions = []; labels = []; zcomps = [];
	return veeper_ions, restwaves, cols, sigcols, bvals, sigbvals, vels, sigvels, labels, zcomps


import tensorflow.compat.v2 as tf
from scipy.optimize import minimize
import numpy as np
import tensorflow_probability as tfp

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

np.sum(np.array([a for a in range(1, 10,1)]))
x_ = np.arange(start = -1, stop = 0, step = 0.1)
np.sum(x_[x_.__len__() // 4 :x_.__len__()//2])
x_range_ = np.arange(4)
x_range_[:-1] # по последнему не смотрят
#x_range_[1:] # по первому игнор

@tf.function
def input_tens(tens):
    tf.print(tens)

def rosen(x):
    """The Rosenbrock function"""
    return np.sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0, axis=0)

x_another = [x_range_,-x_range_]
print(x_another)
np.sum(x_another)

x = tf.convert_to_tensor(np.full((2,1), 1))

@tf.function
def input_(x_tens):
    tf.print(x_tens)

input_(x)

x_ = np.full((3,), 2)
rosen(x_)

tens_ = tf.convert_to_tensor(rosen(x_))
input_(tens_)
tens_.__repr__()

def rosen_tf(x_tens):
    return tf.reduce_sum(100.0 * (x_tens[1:] - x_tens[:-1] ** 2) ** 2 + (1 - x_tens[:-1]) ** 2)

def diff_(x_tens):
    return tf.reduce_sum(x_tens) * 3.0

class function(tf.Module):

    def compute_gradient_optional(self, functional_obj):
        with tf.GradientTape() as g:
            g.watch(self._tensor)
            res = functional_obj(self._tensor)
            grad_ = g.gradient(res, self._tensor)

        return grad_

    def __init__(self, x_numpy):

        assert( (x_numpy.shape.__len__() == 1), \
               ('We tae a x_numpy shape not (ndims, ) ! \n shape tensor is : {!s}'.format(x_numpy.shape.__len__())))

        if(x_numpy.shape.__len__() != 1):
            assert (1 != 1) ('shape is not signature : (n, )!')
        else:
            self._tensor = tf.Variable(x_numpy)
            self._dims = x_numpy.shape[0]

    def _call_f(self, function = rosen):
       return tf.converct_to_tensor(function(self._tensor)) ## При условии что ее вообще можно преобразовать в тензор

    def __call__(self, some_parameter = 0): # define a default parameter rosen function
        self._another = tf.convert_to_tensor(rosen(self._tensor.numpy()))
        return self._another  ## we can define call for another functional pbject

    def __repr__(self):
        return (self._tensor.__repr__(), self._another.__repr__())

    def __str__(self):

        return "Dimensional is {!s} \n Vector x is : {!s} \n Value of functional is: {!s} \n".format(
                                      self._dims, self._tensor.numpy(), self.__call__(some_parameter = 0).numpy())

    def _change(self, new_vector):
        @tf.function
        def assign_(x_new):
            x_new_tensor = tf.convert_to_tensor(x_new)
            if(x_new_tensor.shape != self._tensor.shape):
                assert (1 != 1) ("shape of tensor is not a equal!")
            else:
                self._tensor.assign(x_new_tensor)

        assign_(new_vector)

    def _wrapper(self):
        def _inner(vars_x) -> np.float32:
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x) # we change aor vector of x
                return (self.__call__(some_parameter = 0)).numpy()

        return _inner

    def compute_gradient(self):
                with tf.GradientTape() as g:
                    g.watch((self._tensor))
                    f_a = tf.reduce_sum(100.0 * (self._tensor[1:] - self._tensor[:-1] ** 2) ** 2 + (1 - self._tensor[:-1]) ** 2)
                    grad_ = g.gradient(f_a, self._tensor)

                return grad_

    def wrapper_grad(self):
        def inner_grad(vars_x, *args) -> float: # should be a vector of shape np.float
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x)
                return self.compute_gradient().numpy()

        return inner_grad

    def compute_hessian(self):
        with tf.GradientTape(persistent = True) as g:
            g.watch(self._tensor)
            f_a = tf.reduce_sum(100.0 * (self._tensor[1:] - self._tensor[:-1] ** 2) ** 2 + (1 - self._tensor[:-1]) ** 2)
            grad_ = g.gradient(f_a, self._tensor)
            list_ = []
            for iter_ in range(self._tensor.shape[0]):
                grad_grad = g.gradient(grad_[iter_], self._tensor)
                list_.append(grad_grad)

            matrix = tf.convert_to_tensor(list_)
        return matrix

    def wrapper_hessian(self):
        def inner_hessian(vars_x, *args) -> float:
            if(vars_x.shape[0] != self._tensor.shape[0]):
                assert (1 != 1) ('Error with different size!: ')
                return None
            else:
                self._change(vars_x)
                return self.compute_hessian().numpy()

        return inner_hessian

x_tens_ = tf.constant(np.full((5,), 2.))
x_another = tf.constant(np.full((5,), 1.5))

foo_ = function(x_tens_)
print("proba: ", foo_.compute_gradient_optional(diff_))
param_ = 3
input_(foo_.__call__(param_))

foo_._change(np.full((5, ), 2.))
function_ = foo_._wrapper()
funct_grad = foo_.wrapper_grad()

input_tens(foo_.compute_gradient())

funct_hessian = foo_.wrapper_hessian()

#print(funct_grad(np.full((5,), 0.0)))

from scipy.optimize import minimize
x_0 = np.array([1.3 , 0.7 , 0.8, 3.0, 2.0])
res_ = minimize(function_, x_0, method = 'nelder-mead', options = {'xtol': 1e-8, 'disp': True})
print(res_.x)
res_an_ = minimize(function_, x_0, method = 'BFGS', jac = funct_grad, options = {'disp': False})

print(res_an_)

res_another = minimize(function_, x_0, method = 'Newton-CG', jac = funct_grad,hess = funct_hessian,options = {'xtol' : 1e-8, 'disp': True})
print(res_another)


#!/usr/bin/env python
# -*- coding: utf-8-*-
import xml.etree.ElementTree as ET
import tarfile
import zipfile
import re
from pathlib import Path
import shutil
import os
import pymysql
import sys
import traceback
import subprocess
import getpass
import Archive as archive

if __name__ in '__main__':

    print('処理対象の年度:範囲指定2012-2015')
    in_year = input('>> ')
    target_years = []
    if re.match(r'\d{4}-\d{4}',in_year):
        temp = in_year.split('-')
        target_years = range(int(temp[0]),int(temp[1])+1)
    elif re.match(r'\d{4}', in_year):
        target_years = [in_year]
    else:
        print('入力が不正です')
        sys.exit()

    print('公報種類を入力してください(A,A5,S,S5,T,T5)')
    in_filetype = input('>> ')
    if in_filetype not in ['A','A5','S','S5','T','T5']:
        print('入力が不正です')
        sys.exit()

    for y in target_years:
        target_year = str(y)
        root_dir = "/mnt/Drobo/XML_files/" + in_filetype + "/" +  target_year
        files = os.listdir(root_dir)
        file_list = files

        str_f_list = sorted(file_list, reverse=True)

        for f_name in str_f_list:
            print(f_name)
            tar_path = Path(root_dir + "/" + f_name)
            path_list = list(tar_path.glob("**/*.xml"))

            for pl in path_list:

                try:
                    xml_path = str(pl)
                    ret = archive.xml_elements(xml_path)
                    pub_nr = ret['publn_nr']
                    print(pub_nr)
                    print(xml_path)

                    #DB登録
                    sql_element = archive.generate_insert_sql(ret)
                    archive.register_xml_elements(sql_element)
                except (pymysql.IntegrityError, pymysql.err.IntegrityError):
                    print('already exist')
                    continue
                except (pymysql.OperationalError, pymysql.ProgrammingError, pymysql.InternalError) as error:
                    # データベースエラー　エラーログファイル生成、終了　処理は継続
                    except_str = traceback.format_exc()
                    print(except_str)
                    message = "--------------------------------------------------\n"
                    message += "XML_PATH:" + xml_path + "\n"
                    message += "ERROR_MSG:" + except_str + "\n"
                    message += "--------------------------------------------------\n"
                    f = open('publn_error_log.txt', 'a')
                    f.write(message)
                    f.close()
                except:
                    # データベースエラー　エラーログファイル生成、終了　処理は継続
                    except_str = traceback.format_exc()
                    print(except_str)
                    message = "--------------------------------------------------\n"
                    message += "XML_PATH:" + xml_path + "\n"
                    message += "ERROR_MSG:" + except_str + "\n"
                    message += "--------------------------------------------------\n"
                    f = open('publn_error_log.txt', 'a')
                    f.write(message)
                    f.close()
                    sys.exit()


from django.db import models
from django.utils import timezone
from datetime import datetime, timedelta
from django import forms

#===========Project_Information=============================

class ProjectInformation(models.Model):
    class Meta:
        verbose_name = 'Project Information Record'

    RESPONSIBLE_PARTY = (
        ('Stanley Njoku', 'Stanley Njoku'),
        ('Beverly Everhart', 'Beverly Everhart'),
        ('Lenka Barrettova', 'Lenka Barrettova'),
        ('Chris Bradshaw', 'Chris Bradshaw'),
        ('Connie Gordon', 'Connie Gordon'),
        ('Xonie Lloyd', 'Xonie Lloyd'),
        ('Jeffrey Hamilton', 'Jeffrey Hamilton'),
    )
    person_Responsible = models.CharField(max_length=150, choices= RESPONSIBLE_PARTY)

# Project Category Type Dropdown Selection

    PROJECT_CATEGORY =(
        ('Safety','Safety'),
        ('Environment','Environment'),
        ('Fee',' Fee',),
        ('Regulatory', 'Regulatory'),
        ('Report','Report')
    )
    category = models.CharField(max_length=150, choices = PROJECT_CATEGORY)

# Project Type Dropdown Selection
    PROJECT_TYPE = (
        ('Hazardous Waste Management', 'Hazardous Waste Management'),
        ('Confined Space', 'Confined Space'),
        ('Emergency Response Management', ' Emergency Response Management',),
        ('Storm Water', 'Storm Water'),
        ('Hazardous Management Business Plan', 'Hazardous Management Business Plan'),
        ('Respiratory Fit Testing', 'Respiratory Fit Testing'),
        ('Worker Compensation', 'Worker Compensation'),
        ('Ergonomics', 'Ergonomics'),
        ('Respiratory Fit Testing', 'Respiratory Fit Testing'),
        ('Job Hazard Analysis', 'Job Hazard Analysis'),
        ('Lockout/tagout Procedures', 'Lockout/tagout Procedures'),
        ('Machine Safety', 'Machine Safety'),
        ('Chemical Hazard Communication', 'Chemical Hazard Communication'),
        ('Accident Prevention Programs', 'Accident Prevention Programs'),

    )
    Type= models.CharField(max_length =150, choices=PROJECT_TYPE)

    # RADIO BUTTON

    PROJECT_STATUS = (
        ('Not Started', 'Not Started'),
        ('In Progress', 'In Progress'),
        ('Pending Review', 'Pending Review',),
        ('On Hold', 'On Hold'),
        ('Approved', 'Approved'),
    )
    status = models.CharField(max_length=150, choices=PROJECT_STATUS)

    #Add Additional Field Lines
    date_added = models.DateTimeField(default=datetime.now)
    estimated_completion = models.DateField()
    comment = models.TextField()

#============Modifying Table to Read information ======================
    def __str__(self):
       return'person_Responsible:{}, Category: {}, Type:{}, Status:{}'.format(self.person_Responsible, self.category, self.Type, self.status)

#=================Manifest Class==============================================

class Manifest(models.Model):
    Name= models.CharField(max_length=200)
    Manifest_Tracking_Number = models.CharField(max_length=200,)
    EPA_ID_Location = models.CharField(max_length=200)
    Building_Address = models.CharField(max_length=200)

    VENDOR_NAMES = (
        ('Clean Harbors', 'Clean Harbors'),
        ('Ingenium', 'Ingenium'),
        ('Veolia', 'Veolia'),

     )
    Vendor_Name = models.CharField(max_length=150, choices= VENDOR_NAMES)

    LOCATION_EPAID = (
         ('B2500 - CAL000342024', 'B2500 - CAL000342024'),
         ('B6000 - CAD983606872', 'B6000 - CAD983606872'),
         ('4000  - CAD981991276', '4000  - CAD981991276'),
         ('B2000 - CAD983606872', 'B2000 - CAD983606872'),
         ('B925  - CAR000158816', 'B925  - CAR000158816'),
         ('B3110 - CAD981453871', 'B3110 - CAD981453871'),
         ('B487 - CAL000248918', 'B487 - CAL000248918'),
         ('B5731 - CAL000346207', 'B5731 - CAL000346207'),
         ('B5500 - CAL000095938', 'B5500 - CAL000095938'),
         ('Select One', 'Select One'),
     )
    EPA_ID_Location = models.CharField(max_length=100, choices=LOCATION_EPAID, default='Select One')

    SHIPPING_TYPE = (
        ('Weekly Shipment', 'Weekly Shipment'),
        ('Monthly Shipment', 'Monthly Shipment'),
        ('90-day Shipment', '90-day Shipment',),
        ('180-day/270-day Shipment', '180-day/270-day Shipment'),
        ('Urgent Shipment', 'Urgent Shipment'),

    )
    Type= models.CharField(max_length =150, choices=SHIPPING_TYPE)

    CATEGORIES= (
         ('Drum Shipment', 'Drum Shipment'),
         ('Labpack', 'Labpack'),
         ('Bulk Shipment','Bulk Shipment'),

     )
    category = models.CharField(max_length=150, choices=CATEGORIES)

    Weight_Shipped = models.CharField(max_length=150)

    WEIGHT_UNITS= (
         ('Gallons (Liquid Only)', 'Gallons (Liquid Only)'),
         ('Kilograms', 'Kilograms'),
         ('Liters Liquid Only','Liters Liquid Only'),
         ('Metric Tons', 'Metric Tons'),
         ('Cubic Meter', 'Cubic Meter'),
         ('Pounds', 'Pounds'),
         ('Tons', 'Tons'),
         ('Cubic Yards', 'Cubic Yards'),
         ('Select One', 'Select One'),
     )
    weight_Unit = models.CharField(max_length=150, choices=WEIGHT_UNITS, default='Select One')

    def due_Date():
        return datetime.today() + timedelta(days=30)
    Date_Generator_Sgned = models.DateField(default=due_Date)

    Date_to_DTSC = models.DateField(default=due_Date)

    # def DateField():
     # date_added = DateField.DateField.strptime(start_Date,"%m/%d/%y")
     # estimated_completion = date_added+DateField.timedelta(days=30)

    MANIFEST_VENDOR = (
        ('Yes', 'Yes'),
        ('No', 'No'),
    )
    Manifest_Return_from_Vendor = models.CharField(max_length=150, choices=MANIFEST_VENDOR)

    MANIFEST_SIGNATURE = (
        ('Stanley Njoku', 'Stanley Njoku'),
        ('Beverly Everhart', 'Beverly Everhart'),
        ('Lenka Barrettova', 'Lenka Barrettova'),
        ('Chris Bradshaw', 'Chris Bradshaw'),
        ('Connie Gordon', 'Connie Gordon'),
        ('Kevin Tennyson', 'Kevin Tennyson'),
    )
    Manifest_Signatory = models.CharField(max_length=150, choices= MANIFEST_SIGNATURE)

    MANIFEST_FILING_STATUS = (
        ('Not Started', 'Not Started'),
        ('In Progress', 'In Progress'),
        ('Pending Review', 'Pending Review',),
        ('On Hold', 'On Hold'),
        ('Approved', 'Approved'),
    )
    status = models.CharField(max_length=150, choices=MANIFEST_FILING_STATUS)

    comment = models.TextField()

# ==========Read Manifest======================================================

    def __str__(self):
       return'Manifest_Tracking_Number:{}, EPA_ID_Location:{}, date_to_DTSC :{}, Manifest_Return_from_Vendor:{}'.format(self.Manifest_Tracking_Number, self.EPA_ID_Location, self.Date_to_DTSC, self.Manifest_Return_from_Vendor)

# ==========Read Manifest ENDS HERE=====================================================

#=================Compliance Class==============================================

class Compliance(models.Model):
    RESPONSIBLE_INDIVIDUAL = (
        ('Stanley Njoku', 'Stanley Njoku'),
        ('Beverly Everhart', 'Beverly Everhart'),
        ('Lenka Barrettova', 'Lenka Barrettova'),
        ('Chris Bradshaw', 'Chris Bradshaw'),
        ('Connie Gordon', 'Connie Gordon'),
        ('Kevin Tennyson', 'Kevin Tennyson'),
    )
    Individual_Responsible = models.CharField(max_length=150, choices= RESPONSIBLE_INDIVIDUAL)

    agency = models.CharField(max_length=200)

    description = models.CharField(max_length=30)

    COMPLIANCE_CATEGORY =(
        ('Safety','Safety'),
        ('Environment','Environment'),
        ('Fee',' Fee',),
        ('Regulatory', 'Regulatory'),
        ('Report','Report')
    )
    category = models.CharField(max_length=150, choices = COMPLIANCE_CATEGORY)

    AGENCY_TYPE = (
        ('County', 'County'),
        ('Local', 'Local'),
        ('State', 'State',),
        ('Federal', 'Federal'),
        ('EPA', 'EPA'),
        ('OSHA', 'OSHA'),
    )
    Type = models.CharField(max_length=150, choices=AGENCY_TYPE)

    def due_Date():
       return datetime.today() + timedelta(days=30)

    start_Date = models.DateField(default=datetime.today)
    due_Date = models.DateField(default=due_Date)
    comment = models.TextField()

    COMPLIANCE_STATUS = (
        ('Not Started', 'Not Started'),
        ('In Progress', 'In Progress'),
        ('Pending Review', 'Pending Review',),
        ('On Hold', 'On Hold'),
        ('Approved', 'Approved'),
    )
    status = models.CharField(max_length=150, choices=COMPLIANCE_STATUS)

# Modifying Table to Read information ======================

    def __str__(self):
       return'Agency: {}, Description: {}, Type: {}, Start_Date:{}, Due_Date:{}'.format(self.agency, self.description, self.Type, self.start_Date, self.due_Date)

#=================Dashbaord==============================================

def index(request):
    return render(request,'management/index.html')
    num_compliance=Compliance.objects.all().count()
    num_manifest=Manifest.objects.all().count()

    return render(
        request,
        'management/index.html',
        context={'num_manifest':num_manifest,'num_compliance':num_compliance},

      )

    #=================EPAS===============================================================


import logging
from urllib.parse import unquote
from datetime import datetime

from biocontainers.common.models import MongoToolVersion, ContainerImage
from biocontainers.common.utils import call_api
from bs4 import BeautifulSoup

logger = logging.getLogger('biocontainers.singularity.models')

class SingularityReader:
    """
    This class contains the services to retrieve the containers from Quay.io
    """

    def __init__(self, containers_list_url):
        self.containers_list_url = containers_list_url
        self.containers_list = None

    def get_containers(self):
        """
        This method returns the list of small/short containers descriptions for
        all Singularity containers.
        :return: list of container minimum metadata
        """
        container_list = {}
        response = call_api(self.containers_list_url)
        if response.status_code == 200:
            html = response.text
            for line in html.splitlines():
                if line.startswith("<a href="):
                    entry = BeautifulSoup(line, 'html.parser').find('a')
                    image_name = unquote(entry['href'])
                    if image_name.endswith("/"):
                        continue
                    sibling = entry.next_sibling
                    line_splits = sibling.split()
                    last_updated = line_splits[0] + " " + line_splits[1]
                    size = line_splits[2]
                    # print(image_name + "\t" + last_updated + "\t" +size)
                    image_name_split = image_name.split(':')
                    tool_version = image_name_split[0]+"-"+image_name_split[1].split('--')[0]
                    container = ContainerImage()
                    container.container_type = "SINGULARITY"
                    container.tag = image_name_split[1]
                    container.full_tag = self.containers_list_url + "/" + image_name
                    container.size = size
                    container.last_updated = datetime.strptime(last_updated, '%d-%b-%Y %H:%M')
                    if tool_version not in container_list:
                        container_list[tool_version] = []

                    container_list[tool_version].append(container)

        return container_list


from datetime import date
data_atual = date.today()
data_em_texto = "0{}/0{}/{}".format(data_atual.day, data_atual.month,data_atual.year)

listaNomes      = ['Andre','Joao','Luccas']
listaIdades     = [23,21,25]
listaNascimento = ['23/11/1998','05/07/2000','18/01/1996']
listaMesMenor = ['04', '06', '09', '11']

def menu():
    m = ''' \n
            0-  Finalizar
            1-  Cadastrar dados nas listas
            2-  Listar somente os nomes
            3-  Listar os dados completos das pessoas
            4-  Localizar o nome de alguém e mostrar todos seus dados
            5-  Alterar a idade ou a data de nascimento
            6-  Excluir alguém do cadastro
            Escolha: '''
    return input(m)

def verificarIdade(idade):
    try:
        if int(idade) > 0 and int(idade) < 115:
            return idade
        else:
            print("Digite uma maior que zero")
    except:
        print("Digite uma idade válida")

    return verificarIdade(input("Digite a idade: "))

def verificarDataNasc(data):
    try:
        if data[2:3] == '/' and data[5:6] == '/':
            if int(data[:2]) > 00 and int(data[:2]) <= 31:
                if int(data[3:5]) > 00 and int(data[3:5]) < 12:
                    if data[3:5] in listaMesMenor and int(data[:2]) <= 30:
                        pass
                    elif data[3:5] == '02' and int(data[:2]) <= 28:
                        pass
                    elif data[3:5] not in listaMesMenor:
                        pass
            if data[6:] <= data_em_texto[6:]:
                listaNascimento.append(data)
        else:
            print("Digite uma data valida")
            verificarDataNasc(input("Digite a data de nascimento no formato dd/mm/aaaa: : "))
    except:
        print("Digite uma data valida")
        verificarDataNasc(input("Digite a data de nascimento no formato dd/mm/aaaa: : "))

def cadastro():
    listaNomes.append(input("Digite o nome: "))
    listaIdades.append(verificarIdade(input("Digite a idade: ")))
    listaNascimento.append(verificarDataNasc(input("Digite a data de nascimento no formato dd/mm/aaaa: : ")))

def listarNomes():
    for nome in range(len(listaNomes)):
        print("Nome: %s"%listaNomes[nome])

def listarDados():
    for i in range(len(listaNomes)):
        printar(i)

def localizarDados():
    while True:
        nome = input("Digite o nome que deseja buscar ou 0 para sair: ")
        if nome in listaNomes:
            i = listaNomes.index(nome)
            printar(i)
        elif nome == '0':
            break
        print("Nome não encontrado")

def attDado():
    nome = input("Digite o nome de quem deseja alterar: ")
    if nome in listaNomes:
        i = listaNomes.index(nome)
        while True:
            n = input("Digite 1 para alterar a idade ou 2 para data de nascimento ou 0 para sair: ")
            if n == '1':
                novaIdade = verificarIdade(input("Digite a nova idade: "))
                listaIdades[i] = novaIdade
                break
            elif n == '2':
                novaData = verificarDataNasc(input("Digite a data de nascimento no formato dd/mm/aaaa: : "))
                listaNascimento[i] = novaData
                break
            elif n == '0':
                break
            else:
                print("Digite 1 para alterar a idade ou 2 para data de nascimento ou 0 para sair: ")

def excluir():
    nome = input("Digite o nome de quem deseja excluir: ")
    if nome in listaNomes:

        i = listaNomes.index(nome)
        printar(i),
        print("\n     ---------Excluido com sucesso---------\n")
        listaNomes.pop(i)
        listaIdades.pop(i)
        listaNascimento.pop(i)
    else:
        print("Nome nao localizado")

#Funçao para printar dados completos
def printar(i):
    print(f"Nome: {listaNomes[i]}, Idade: {listaIdades[i]}, Data de nascimento: {listaNascimento[i]}\n", end="")

#PROGRAMA PRINCIPAL

while True:
    escolha = menu()
    if escolha == '0':
        break
    elif escolha == '1':
        cadastro()
    elif escolha == '2':
        listarNomes()
    elif escolha == '3':
        listarDados()
    elif escolha == '4':
        localizarDados()
    elif escolha == '5':
        attDado()
    elif escolha == '6':
        excluir()


import pandas as pd     #pip install pandas
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr     #pip install SpeechRecognition

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)
        print("Say that again please...")
        return "None"
    return query

sheet_id='1nFE-K6TH_qzybFgPFK-idvUUISaT0nZ6gnHHdIXklbo'

df= pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
a=df.set_index('Organization name').T.to_dict('list')  #converting pandas dataframe to a python dictionary

print(type(a.values()))
print(a.keys())
query = takeCommand().lower()

for key,value in a.items():
    if key == query:
        print(key)
        speak(value)