import math
import time as timelib
from typing import List

import numpy as np

from .constants import tangental_velocity_earth, radius_earth, standard_pressure, speed_of_sound
from .params import acceleration_limit, dt, num_timesteps, get_downranges, get_initial_conditions, get_stage_time_intervals
from . import rockets
from .rkt_types import Control, Coords, Stage, Telemetry
from .utils import barometric_density, cart2pol, force_drag, force_gravity, pol2cart, magnitude

def get_forces(mass_stages: float, mass_prop_remaining: float,
               i: int, time: float,
               stage: Stage, controls: List[Control],
               position: np.ndarray, velocity: np.ndarray) -> List[float]:

    mass_curr = mass_stages + mass_prop_remaining
    (pos_rho, pos_phi) = cart2pol(position[0], position[1])
    radius = magnitude(position[0], position[1])

    # The velocity of the atmosphere points
    # in the direction of the derivative of position, i.e. + math.pi / 2
    phi = pos_phi + math.pi / 2.0
    (x_comp, y_comp) = pol2cart(1.0, phi)
    atmosphere_vel_phi = tangental_velocity_earth*(pos_rho/radius_earth)
    atm_vel_x = x_comp * atmosphere_vel_phi
    atm_vel_y = y_comp * atmosphere_vel_phi
    # NOTE: The speed used to calculate force_drag should be the NET
    # speed of the stage w.r.t. the atmosphere, NOT just the stage itself!
    # speed_stage = magnitude(velocity[0], velocity[1])
    net_vel_x = velocity[0] - atm_vel_x
    net_vel_y = velocity[1] - atm_vel_y
    speed = magnitude(net_vel_x, net_vel_y)

    altitude = radius - radius_earth

    # Determine forces

    # Force of gravity always points 'down',
    # in the opposite direction to position (in polar coordinates), i.e. - math.pi
    phi = pos_phi - math.pi
    (x_comp, y_comp) = pol2cart(1.0, phi)
    force_gravity_mag = force_gravity(mass_curr, altitude)
    force_gravity_x = x_comp * force_gravity_mag
    force_gravity_y = y_comp * force_gravity_mag

    # Centripetal force always points 'up',
    # in the same direction to position (in polar coordinates)
    # Necessary in cartesian?

    # Force of drag always points
    # in the opposite direction to velocity, i.e. (-1) *
    density = barometric_density(altitude)
    drag_coef = 0.05 if speed < speed_of_sound else 0.15
    force_drag_mag = force_drag(density, speed, drag_coef, rockets.cross_sectional_area)
    #(vel_rho, vel_phi) = cart2pol(velocity[0], velocity[1])
    #force_drag_rho = force_drag_mag * vel_rho / speed
    #force_drag_phi = force_drag_mag * vel_phi / speed
    force_drag_x = 0.0 if speed == 0.0 else (-1.0) * force_drag_mag * net_vel_x / speed
    force_drag_y = 0.0 if speed == 0.0 else (-1.0) * force_drag_mag * net_vel_y / speed

    #forces_sum_rho = thrust_rho - force_gravity(mass_curr, altitude) - force_drag_rho
    #forces_sum_phi = thrust_phi - force_drag_phi
    #(forces_sum_x, forces_sum_y) = pol2cart(forces_sum_rho, forces_sum_phi)
    forces_sum_x = force_gravity_x + force_drag_x
    forces_sum_y = force_gravity_y + force_drag_y

    excess_thrust_factor = 1.0
    if mass_prop_remaining > 0.0:
        # Update the control for the current stage, if necessary
        if not controls[i].t1 <= time < controls[i].t2:
            for c in stage.controls:
                if c.t1 <= time < c.t2:
                    controls[i] = c
                    #print(f'stage {i+1} updating', controls[i])
                    break
                # TODO: Check that there is always a valid control.

        thrust_mag = sum([e.dmdt * e.velocity_exhaust for e in stage.engines])
        thrust_mag = thrust_mag * controls[i].force_mag
        # To find the thrust vector, add the position and control angles
        phi = pos_phi + controls[i].force_phi
        (x_comp, y_comp) = pol2cart(1.0, phi)
        thrust_x = x_comp * thrust_mag
        thrust_y = y_comp * thrust_mag

        #forces_sum_mag = magnitude(forces_sum_rho, forces_sum_phi)
        # Include all forces, or just thrust? Let's just use thrust.
        # For the purpose of limiting structural loads we can ignore gravity,
        # and the acceleration near maxQ is nowhere near this limit.
        acceleration_mag = thrust_mag / mass_curr

        excess_thrust_factor = 1.0
        if acceleration_mag > acceleration_limit:
            excess_thrust = (acceleration_mag - acceleration_limit) * mass_curr
            excess_thrust_factor = (thrust_mag - excess_thrust) / thrust_mag
            thrust_x = thrust_x * acceleration_limit / acceleration_mag
            thrust_y = thrust_y * acceleration_limit / acceleration_mag

        forces_sum_x += thrust_x
        forces_sum_y += thrust_y

    return [forces_sum_x, forces_sum_y, force_drag_mag, excess_thrust_factor]

def run(stages: List[Stage]) -> List[Telemetry]:
    time_initial = timelib.time()

    # Initialization
    stage_time_intervals = get_stage_time_intervals(stages)
    controls = [stage.controls[0] for stage in stages]
    telemetries = get_initial_conditions(stages)

    for timestep in range(num_timesteps - 1):
        time = timestep * dt
        for i, stage in enumerate(stages):
            (t1, t2) = stage_time_intervals[i]
            # Before stage separation i, stages >= i move together, so only
            # calculate forces and accelerations for the combined stages once
            # and overwrite the telemetries for stages >= i identically below.
            if time < t1:
                continue

            telemetry = telemetries[i]
            if not telemetry.update:
                continue

            # copy so we can mutably update @ half-step
            pos0 = np.copy(telemetry.positions[timestep])
            vel0 = np.copy(telemetry.velocities[timestep])

            #print('timestep, stage', timestep, i)
            # This is the total mass of all the (remaining) stages,
            # excluding the remaining prop mass of the current stage.
            mass_stages = stage.mass_dry + stage.mass_payload
            if time < t2:
                mass_stages += sum([s.mass_dry + s.mass_payload + s.mass_prop for s in stages[(i+1):]])
            mass_curr = mass_stages + telemetry.mass_prop_remaining

            forces = get_forces(mass_stages, telemetry.mass_prop_remaining, i, time, stage, controls, pos0, vel0)
            forces_sum_x, forces_sum_y, force_drag_mag, excess_thrust_factor = forces

            prop_used = sum([e.dmdt for e in stage.engines]) * dt * excess_thrust_factor

            # Determine acceleration
            a_x = forces_sum_x / mass_curr
            a_y = forces_sum_y / mass_curr
            acc1 = Coords(a_x, a_y) # using pos0, vel0

            position = np.copy(telemetry.positions[timestep])
            velocity = np.copy(telemetry.velocities[timestep])
            h = dt
            position[0] += h * velocity[0]
            position[1] += h * velocity[1]

            velocity[0] += h * acc1[0]
            velocity[1] += h * acc1[1]

            acceleration = acc1

            midpoint = True
            if midpoint:
                # See https://en.wikipedia.org/wiki/Midpoint_method
                # Now calculate forces again using updated
                # position, velocity, and mass_prop args (and time, if it matters)

                # copy so we can mutably update @ half-step
                pos1 = np.copy(telemetry.positions[timestep])
                vel1 = np.copy(telemetry.velocities[timestep])
                h = dt / 2.0
                pos1[0] += h * vel1[0]
                pos1[1] += h * vel1[1]

                vel1[0] += h * acc1[0]
                vel1[1] += h * acc1[1]

                mass_curr = mass_stages + telemetry.mass_prop_remaining - prop_used / 2.0
                forces = get_forces(mass_stages, telemetry.mass_prop_remaining - prop_used / 2.0,
                                    i, time + h, stage, controls, pos1, vel1)
                forces_sum_x, forces_sum_y, force_drag_mag_, excess_thrust_factor_ = forces

                # Determine acceleration
                a_x = forces_sum_x / mass_curr
                a_y = forces_sum_y / mass_curr
                acc2 = Coords(a_x, a_y) # using pos1, vel1

                # There are a few timesteps where the two acceleration
                # estimates drastically diverge. This appears to be around
                # stage separation events. For now, simply check for errors
                # and fallback to euler integration.
                acc_mag_ratio = magnitude(acceleration[0], acceleration[1]) / magnitude(acc2[0], acc2[1])
                if abs(acc_mag_ratio - 1.0) < 0.02: # 2% tolerance
                    position = np.copy(telemetry.positions[timestep])
                    velocity = np.copy(telemetry.velocities[timestep])

                    h = dt
                    position[0] += h * velocity[0]
                    position[1] += h * velocity[1]

                    velocity[0] += h * acc2[0]
                    velocity[1] += h * acc2[1]

                    acceleration = acc2
                else:
                    pass #print(i + 1, timestep, round(time, 4), round(acc_mag_ratio, 6))

            runge_kutta_4 = True
            if runge_kutta_4:
                # See https://en.wikipedia.org/wiki/Runge–Kutta_methods
                # Now calculate forces again using updated
                # position, velocity, and mass_prop args (and time, if it matters)

                # copy so we can mutably update @ half-step
                pos2 = np.copy(telemetry.positions[timestep])
                vel2 = np.copy(telemetry.velocities[timestep])

                # Estimate half-timestep velocities, positions using the Euler method.
                h = dt / 2.0
                pos2[0] += h * vel2[0]
                pos2[1] += h * vel2[1]

                vel2[0] += h * acc2[0]
                vel2[1] += h * acc2[1]

                mass_curr = mass_stages + telemetry.mass_prop_remaining - prop_used / 2.0
                forces = get_forces(mass_stages, telemetry.mass_prop_remaining - prop_used / 2.0,
                                    i, time + h, stage, controls, pos2, vel2)
                forces_sum_x, forces_sum_y, force_drag_mag_, excess_thrust_factor_ = forces

                # Determine acceleration
                a_x = forces_sum_x / mass_curr
                a_y = forces_sum_y / mass_curr
                acc3 = Coords(a_x, a_y) # using pos2, vel2

                # copy so we can mutably update @ next step
                pos3 = np.copy(telemetry.positions[timestep])
                vel3 = np.copy(telemetry.velocities[timestep])

                # Estimate next timestep velocities, positions using the Euler method.
                h = dt
                pos3[0] += h * vel3[0]
                pos3[1] += h * vel3[1]

                vel3[0] += h * acc3[0]
                vel3[1] += h * acc3[1]

                mass_curr = mass_stages + telemetry.mass_prop_remaining - prop_used
                forces = get_forces(mass_stages, telemetry.mass_prop_remaining - prop_used,
                                    i, time + h, stage, controls, pos3, vel3)
                forces_sum_x, forces_sum_y, force_drag_mag_, excess_thrust_factor_ = forces

                # Determine acceleration
                a_x = forces_sum_x / mass_curr
                a_y = forces_sum_y / mass_curr
                acc4 = Coords(a_x, a_y) # using pos3, vel3

                # Finally, perform a weighted average of the four estimates.
                # This is essentially Simpson's rule.
                vel_rk4_x = (vel0[0] + 2.0*vel1[0] + 2.0*vel2[0] + vel3[0]) / 6.0
                vel_rk4_y = (vel0[1] + 2.0*vel1[1] + 2.0*vel2[1] + vel3[1]) / 6.0
                pos_rk4 = Coords(pos0[0] + dt * vel_rk4_x, pos0[1] + dt * vel_rk4_y)
                acc_rk4_x = (acc1[0] + 2.0*acc2[0] + 2.0*acc3[0] + acc4[0]) / 6.0
                acc_rk4_y = (acc1[1] + 2.0*acc2[1] + 2.0*acc3[1] + acc4[1]) / 6.0
                vel_rk4 = Coords(vel0[0] + dt * acc_rk4_x, vel0[1] + dt * acc_rk4_y)
                acc_rk4 = Coords(acc_rk4_x, acc_rk4_y)

                # There are a few timesteps where the two acceleration
                # estimates drastically diverge. This appears to be around
                # stage separation events. For now, simply check for errors
                # and fallback to euler integration.
                acc_mag_ratio = magnitude(acceleration[0], acceleration[1]) / magnitude(acc_rk4[0], acc_rk4[1])
                if abs(acc_mag_ratio - 1.0) < 0.02: # 2% tolerance
                    position[0] = pos_rk4[0]
                    position[1] = pos_rk4[1]
                    velocity[0] = vel_rk4[0]
                    velocity[1] = vel_rk4[1]
                    acceleration = acc_rk4
                else:
                    pass #print(i + 1, timestep, round(time, 4), round(acc_mag_ratio, 6))

            # NOW mutably update prop after half-timestep
            telemetry.mass_prop_remaining -= prop_used

            telems = [telemetries[i]]
            # Before stage separation i, stages >= i move together, so
            # overwrite the telemetries for stages >= i identically here.
            if time < t2:
                telems = telemetries[i:]

            for telem in telems:
                radius = magnitude(position[0], position[1])
                altitude = radius - radius_earth
                if not telem.update:
                    pass
                elif altitude < 0.0:
                    # We either landed or crashed into the dirt.
                    telem.accelerations = telem.accelerations[:timestep]
                    telem.velocities = telem.velocities[:timestep]
                    telem.positions = telem.positions[:timestep]
                    telem.barometric_densities = telem.barometric_densities[:timestep]
                    telem.dynamic_pressures = telem.dynamic_pressures[:timestep]
                    telem.update = False
                else:
                    # Integrate trajectory / update telemetry
                    telem.accelerations[timestep][0] = acceleration[0]
                    telem.accelerations[timestep][1] = acceleration[1]

                    telem.velocities[timestep+1][0] = velocity[0]
                    telem.velocities[timestep+1][1] = velocity[1]

                    telem.positions[timestep+1][0] = position[0]
                    telem.positions[timestep+1][1] = position[1]

                    density = barometric_density(altitude)
                    telem.barometric_densities[timestep+1] = density
                     # convert to pressure in units of bar
                    force_tot_bar = (rockets.cross_sectional_area * standard_pressure)
                    telem.dynamic_pressures[timestep+1] = force_drag_mag / force_tot_bar

    time_final = timelib.time()
    print('simulation time', time_final - time_initial)
    return telemetries

def score_telemetries(orbit: bool, telemetries: List[Telemetry]) -> float:
    # Only care about final stage for orbit check and delta_velocity
    telemetry = telemetries[-1]

    def phis_pos(phi: float) -> float:
        return phi if phi >= 0 else phi + math.pi

    downranges = []
    for telemetry in telemetries:
        phis = [phis_pos(cart2pol(pos[0], pos[1])[1]) for pos in telemetry.positions]
        downranges.append(get_downranges(phis)[-1])

    # NOTE: The various score components will in general have different units.
    # This is okay!
    # NOTE: Use absolute value here instead of squaring the residual; otherwise,
    # the return-to-launch-site penalty can outweigh the dV to orbit!
    # Only care about initial stages for rtls penalty
    rtls_penalty = sum([abs(d) for d in downranges[:-1]])

    # Check if we failed to reach orbit
    if telemetry.positions.size < num_timesteps:
        # If we have not reached orbit yet,
        # we do NOT want discontinuities in the objective function, because
        # that will cause an 'activity cliff' and prevent the algorithm from
        # improving failing solutions. Instead, return something that will
        # monotonically increase as we get closer to reaching orbit, such as
        # the downrange distance of the last stage.
        if orbit:
            return -math.inf
        else:
            # Divide by 100 so this is numerically smaller than the dV
            # required to achieve orbit.
            return downranges[-1] / 100.0

    velocities = [magnitude(vx, vy) for vx, vy in telemetry.velocities]
    delta_velocity = max(velocities)

    score = delta_velocity - rtls_penalty
    return score


#!/usr/bin/env python3
import os
import sys
import time
import re
import struct
import json
import glob
import yaml
import datetime
import hashlib
import shutil

cwd = os.path.dirname(os.path.abspath(__file__))
srcdir = cwd + "/../articles/"
distdir = cwd + "/../dist/"
shutil.rmtree(distdir, ignore_errors=True)
shutil.copytree(srcdir, distdir)
category_meta = yaml.load(open(srcdir + "categories.yaml", "r", encoding="utf-8"))
srcfiles = glob.glob(srcdir + "**/**.md", recursive=True)
metalines = slice(1, 4)

categories = []
artitles = {}
artitles_list = {}

for filepath in srcfiles:
    filename = filepath[len(srcdir):]
    mtime = os.stat(filepath).st_mtime
    category = filename.split('/')[0]
    if not category in categories:
        categories.append(category)
        artitles[category] = []

    file = open(filepath, "r", encoding="utf-8")
    mdcontent = file.read().split('\n')[metalines]
    metayml = "\n".join(mdcontent)
    meta = yaml.load(metayml)

    md5id = hashlib.md5(filepath.encode('utf-8')).hexdigest()[:16]
    #meta["id"] = md5id
    meta["filepath"] = filename
    meta["edittime"] = datetime.datetime.fromtimestamp(int(mtime))

    artitles[category].append(md5id)
    artitles_list[md5id] = meta

with open(distdir + "meta.json", "wb") as of:
    string = json.dumps({
        "categories": category_meta,
        "artitles" : artitles,
        "artitles_list": artitles_list
    }, ensure_ascii=False, indent=2, default=str)
    of.write(string.encode('utf-8'))


# --- Day 10: Syntax Scoring ---
#
# https://adventofcode.com/2021/day/10

import requests

openers = {'(', '{', '<', '['}
closers = {')', '}', '>', ']'}

o2c = {'(': ')',
       '{': '}',
       '<': '>',
       '[': ']'}
c2o = {')': '(',
       '}': '{',
       '>': '<',
       ']': '['}

def syntax_error_score_v1(syntax_lines):
    score = 0
    for line in syntax_lines:
        score = score + __get_corrupted_score(__get_corrupted_bracket(line))
    return score

def syntax_error_score_v2(syntax_lines):
    scores = list()
    for line in syntax_lines:
        if __get_corrupted_bracket(line) is None:
            scores.append(__get_autocomplete_score(__complete_line(line)))
    scores.sort()
    return scores[int(len(scores) / 2)]

def __complete_line(line):
    stack = []
    for bracket in line:
        if c2o.get(peek_stack(stack)) == o2c.get(bracket):
            stack.pop()
        else:
            stack.append(bracket)
    completion_chunk = ""
    while stack:
        completion_chunk = completion_chunk + o2c.get(peek_stack(stack))
        stack.pop()
    return completion_chunk

def __get_autocomplete_score(completion_chunk):
    points = {')': 1, '}': 3, '>': 4, ']': 2}
    score = 0
    for bracket in completion_chunk:
        score = score * 5 + points.get(bracket)

    return score

def __get_corrupted_score(bracket):
    points = {')': 3, '}': 1197, '>': 25137, ']': 57}
    if bracket:
        return points[bracket]
    else:
        return 0

def __get_corrupted_bracket(line):
    stack = []
    for bracket in line:
        if bracket in openers:
            stack.append(bracket)
        else:
            if peek_stack(stack) == c2o.get(bracket):
                stack.pop()
            else:
                return bracket
    return None

def peek_stack(stack):
    if stack:
        return stack[-1]
    else:
        return None

if __name__ == '__main__':
    m_headers = {
        'cookie': '<TOKEN HERE>'}
    url_input = requests.get("https://adventofcode.com/2021/day/10/input", headers=m_headers).text.splitlines()
    file_input = open('syntax.txt', 'r').read().splitlines()
    print("Part: 1 = " + str(syntax_error_score_v1(url_input)))
    print("Part: 2 = " + str(syntax_error_score_v2(url_input)))


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

def hidden_init(layer):
    fan_in = layer.weight.data.size()[0]
    lim = 1. / np.sqrt(fan_in)
    return (-lim, lim)

class Actor(nn.Module):

    def __init__(self, state_size, action_size, layer_sizes = [200, 200, 200], seed = 0):
        super(Actor, self).__init__()
        self.seed = torch.manual_seed(seed)
        self.state_size = state_size
        self.action_size = action_size
        self.fc1 = nn.Linear(state_size, layer_sizes[0])
        self.fc2 = nn.Linear(layer_sizes[0], layer_sizes[1])
        self.fc3 = nn.Linear(layer_sizes[1], layer_sizes[2])
        self.fc4 = nn.Linear(layer_sizes[2], action_size)
        self.initialize_weights()

    def initialize_weights(self):
        self.fc1.weight.data.uniform_(*hidden_init(self.fc1))
        self.fc2.weight.data.uniform_(*hidden_init(self.fc2))
        self.fc3.weight.data.uniform_(*hidden_init(self.fc3))
        self.fc4.weight.data.uniform_(-3e-3, 3e-3)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)
        out = torch.tanh(x)
        return out

class Critic(nn.Module):

    def __init__(self, state_size, action_size, layer_sizes = [200, 200, 200], seed = 0):
        super(Critic, self).__init__()
        self.seed = torch.manual_seed(seed)
        self.state_size = state_size
        self.action_size = action_size
        self.fc1a = nn.Linear(state_size, layer_sizes[0])
#         self.fc1b = nn.Linear(state_size, layer_sizes[0])
        self.fc2 = nn.Linear(layer_sizes[0] + action_size, layer_sizes[1])
        self.fc3 = nn.Linear(layer_sizes[1], layer_sizes[2])
        self.fc4 = nn.Linear(layer_sizes[2], 1)
        self.initialize_weights()

    def initialize_weights(self):
        self.fc1a.weight.data.uniform_(*hidden_init(self.fc1a))
#         self.fc1b.weight.data.uniform_(*hidden_init(self.fc1b))
        self.fc2.weight.data.uniform_(*hidden_init(self.fc2))
        self.fc3.weight.data.uniform_(*hidden_init(self.fc3))
        self.fc4.weight.data.uniform_(-3e-3, 3e-3)

    def forward(self, state, action):
        state = state.view(-1, 48)
        action = action.view(-1, 4)

        xa = F.leaky_relu(self.fc1a(state))
#         xb = F.relu(self.fc1b(state))
        x2 = F.leaky_relu(torch.cat([xa, action], dim=1))
        x3 = F.leaky_relu(self.fc2(x2))
        x4 = F.leaky_relu(self.fc3(x3))
        return self.fc4(x4)


import streamlit as st
import SessionState
from prompts import PROMPT_LIST
from wit_index import WitIndex
import random
import time

# st.set_page_config(page_title="Image Search")

# vector_length = 128
wit_index_path = f"./models/wit_faiss.idx"
model_name = f"./models/distilbert-base-wit"
wit_dataset_path = "./models/wit_dataset.pkl"

@st.cache(suppress_st_warning=True, allow_output_mutation=True)
def get_wit_index():
    st.write("Loading the WIT index, dataset and the DistillBERT model..")
    wit_index = WitIndex(wit_index_path, model_name, wit_dataset_path, gpu=False)
    return wit_index

# st.cache is disabled temporarily because the inference could take forever using newer streamlit version.
# @st.cache(suppress_st_warning=True)
def process(text: str, top_k: int = 10):
    # st.write("Cache miss: process")
    distance, index, image_info = wit_index.search(text, top_k=top_k)
    return distance, index, image_info

st.title("Image Search")

st.markdown(
    """
    This application is a demo for sentence-based image search using
    [WIT dataset](https://github.com/google-research-datasets/wit). We use DistillBert to encode the sentences
    and Facebook's Faiss to search the vector embeddings.
    """
)
session_state = SessionState.get(prompt=None, prompt_box=None, text=None)
ALL_PROMPTS = list(PROMPT_LIST.keys())+["Custom"]
prompt = st.selectbox('Prompt', ALL_PROMPTS, index=len(ALL_PROMPTS)-1)
# Update prompt
if session_state.prompt is None:
    session_state.prompt = prompt
elif session_state.prompt is not None and (prompt != session_state.prompt):
    session_state.prompt = prompt
    session_state.prompt_box = None
    session_state.text = None
else:
    session_state.prompt = prompt

# Update prompt box
if session_state.prompt == "Custom":
    session_state.prompt_box = "Enter your text here"
else:
    if session_state.prompt is not None and session_state.prompt_box is None:
        session_state.prompt_box = random.choice(PROMPT_LIST[session_state.prompt])

session_state.text = st.text_area("Enter text", session_state.prompt_box)

top_k = st.sidebar.number_input(
    "Top k",
    value=6,
    min_value=1,
    max_value=10
)

wit_index = get_wit_index()
if st.button("Run"):
    with st.spinner(text="Getting results..."):
        st.subheader("Result")
        time_start = time.time()
        distances, index, image_info = process(text=session_state.text, top_k=int(top_k))
        time_end = time.time()
        time_diff = time_end-time_start
        print(f"Search in {time_diff} seconds")
        st.markdown(f"*Search in {time_diff:.5f} seconds*")
        for i, distance in enumerate(distances):
            try:
                st.image(image_info[i][0].replace("http:", "https:"), width=400)
            except FileNotFoundError:
                st.write(f"{image_info[i][0]} can't be displayed")
            st.write(f"{image_info[i][1]}. (D: {distance:.2f})")

        # Reset state
        session_state.prompt = None
        session_state.prompt_box = None
        session_state.text = None


import configparser
import psycopg2
from sql_queries import create_table_queries, drop_table_queries

""" This function will drop all the tables if exists any """
def drop_tables(cur, conn):
    for query in drop_table_queries:
        cur.execute(query)
        conn.commit()

""" This function will create respective tables for our project"""
def create_tables(cur, conn):
    for query in create_table_queries:
        cur.execute(query)
        conn.commit()

def main():
    """ Read configurations from dwh.cfg """
    config = configparser.ConfigParser()
    print("Read configurations from the configuration file")
    config.read('dwh.cfg')

    """ Reading hostname, databasename, user and password to connect to database """
    conn = psycopg2.connect("host={} dbname={} user={} password={} port={}".\
                            format(*config['CLUSTER'].values()))
    cur = conn.cursor()
    print("Connected to Redshift ")

    """ Executing function drop_tables to drop tables before creating """
    drop_tables(cur, conn)
    print("Droped existing tables from sparkifydb")

    """ Executing function create_tables to create tables as per star schema """
    create_tables(cur, conn)
    print("Created new tables into sparkifydb")

    """ Closing connection """
    conn.close()

if __name__ == "__main__":
    main()


import datetime
from discord.ext import commands, tasks

from src.modules.frog import FrogMessage
from src.config import read_config, update_guild_settings, default_config

daily_message_time = datetime.time(hour=16, tzinfo=datetime.timezone.utc)

class DailyCommands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.guild_id_list = read_config()
        self.daily_frog_fact.start()

    @tasks.loop(time=daily_message_time)
    async def daily_frog_fact(self):
        '''Function to send a random frog fact daily'''
        frog_class = FrogMessage()

        # Gets a random fact
        message = frog_class.get_frog_fact(daily=True)

        # Updates variable
        self.guild_id_list = read_config()

        for guild_id in self.guild_id_list:
            # Gets channel_id and sends daily message
            try:
                if self.guild_id_list[guild_id]["subscribe.daily"]["enabled"]:
                    channel_id = self.guild_id_list[guild_id]["subscribe.daily"]["channel.id"]
                    channel = self.bot.get_channel(int(channel_id))

                    await channel.send(f"Daily Frog Fact:\n{message}")
                    continue

            except:
                if channel_id:
                    print(f"{channel_id} was not found, skipping...")

                else:
                    print("No channel was found, skipping...")

                continue
    #

    @commands.command(name="subscribe_channel")
    @commands.has_any_role("Administrator", "Admin", "Moderator", "Mod")
    async def subscribe_to_daily(self, ctx):
        guild_id = str(ctx.guild.id)

        # Checks if guild already has a daily message channel
        # If it does, checks if channel.id matches ctx channel
        # If it doesn't, it writes over the old channel.id

        if guild_id not in str(self.guild_id_list.keys()):
            # Gets template
            guild_config = default_config()

            self.guild_id_list[guild_id] = guild_config
            self.guild_id_list[guild_id]["subscribe.daily"]["enabled"] = True

        for guild in self.guild_id_list:
            if guild == guild_id:
                # Updates subscribed channel
                self.guild_id_list[guild_id]["subscribe.daily"]["enabled"] = True
                self.guild_id_list[guild_id]["subscribe.daily"]["channel.id"] = ctx.channel.id

                update_guild_settings(self.guild_id_list)

                await ctx.send("This channel has successfully enabled daily frog facts; they will be sent daily at 12pm EST.")
                break

            else:
                continue

    @commands.command(name="unsubscribe_channel")
    @commands.has_any_role("Administrator", "Admin", "Moderator", "Mod")
    async def unsubscribe_to_daily(self, ctx):
        '''Marks server as having the daily fact disabled'''
        guild_id = str(ctx.guild.id)

        if guild_id not in self.guild_id_list.keys():
            await ctx.reply("Daily frog facts were never enabled.", mention_author=False)
        else:
            for guild in self.guild_id_list:
                if guild == guild_id:
                    self.guild_id_list[guild_id]["subscribe.daily"]["enabled"] = False
                    self.guild_id_list[guild_id]["subscribe.daily"]["channel.id"] = None
                    update_guild_settings(self.guild_id_list)

                    await ctx.send("All daily frog facts have been disabled successfully.")
                    break

async def setup(bot):
    await bot.add_cog(DailyCommands(bot))


import spacy
import time
from django.db import transaction
import bson
import django
import re
from django.utils import timezone  # Import Django's timezone module
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '../openCore'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "openCore.settings")
django.setup()
from news.models import News, WordIndex, ImportanceScore

def process_text(input_text):
    # Convert to lowercase
    lowercase_text = input_text.lower()

    # Remover acento
    no_accent_text = lowercase_text.replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace(
        'ú', 'u')

    # Remove punctuation
    no_punctuation_text = re.sub(r'[^\w\s]', '', no_accent_text)

    # Convert ñ to n
    final_text = re.sub(r'ñ', 'n', no_punctuation_text)

    return final_text

def indexer(historical_json = None):
    #regex to convert to lowercase, remove punctuation and convert ñ to n
    print('current time: ', timezone.now())
    #carga el modelo de lenguaje español
    nlp = spacy.load("es_core_news_sm")

    #obtiene el id del último artículo indexado
    last_indexed_article_id = get_last_indexed_article_id()

    # obtiene los artículos de noticias nuevos publicados después del último artículo indexado
    new_news_articles = News.objects.filter(id__gt=last_indexed_article_id)
    print (new_news_articles.count())

    #cargado de stop words
    stop_words = spacy.lang.es.stop_words.STOP_WORDS

    #word list to save will be an empty array if no historical json is passed
    #if historical json is passed, it will hold the json data
    if historical_json is None:
        word_list_to_save = []
    else:
        word_list_to_save = historical_json

    articles_word_count = {}
    for news_article in new_news_articles:
        #tokeniza, elimina las palabras vacías y pone en minúsculas las palabras en los documentos
        content = news_article.content
        doc = nlp(content)

        #lista de palabras filtradas(sin stop words)
        words = [token.text.lower() for token in doc if token.text.lower() not in stop_words and token.is_alpha]
        words = list(set(words)) #elimina duplicados
        articles_word_count[news_article.id] = len(words)

        db_words_list = set(WordIndex.objects.values_list('word', flat=True)) #lista de palabras ya indexadas en la base de datos
        # indexado de palabras en el articulo
        for word in words:
            word_normalized = process_text(word)
            frequency = content.lower().count(word)
            #si la palabra no esta en la base de datos ni en la lista de palabras a guardar, se agrega a la lista de palabras a guardar
            if word_normalized not in db_words_list and word_normalized not in [word["word"] for word in word_list_to_save]:
                word_list_to_save.append({
                    "word": word_normalized,
                    "frequency_global": frequency,
                    "importance_scores": [
                        {
                            "article_info": {
                                "article_id": news_article.id,
                                "word_count": articles_word_count[news_article.id],
                            },
                            "frequency": frequency,
                            "tf_idf": 0.0
                        }
                    ],
                })
            elif word_normalized in [word["word"] for word in word_list_to_save]:
                #if word is already in the list, append a dict to the importance_scores list with the id_noticia and frequency
                for word_dict in word_list_to_save:
                    if word_dict["word"] == word_normalized:
                        word_dict["frequency_global"] += frequency
                        word_dict["importance_scores"].append({
                            "article_info": {
                                "article_id": news_article.id,
                                "word_count": articles_word_count[news_article.id],
                            },
                            "frequency": frequency,
                            "tf_idf": 0.0
                        })
        # Update the record of the last indexed article to the most recent one in the batch
        update_last_indexed_article(news_article)
        print("indexed article: ", news_article.id)
    return word_list_to_save
    print ('time after query: ', timezone.now())

def get_last_indexed_article_id():
    # Implement a method to fetch the ID of the last indexed article from a configuration file or database
    # Return a default value (e.g., 0) if no previous indexing has been done
    try:
        last_indexed_article = News.objects.latest('indexed_on')
        if last_indexed_article.indexed_on is None:
            return 0
        return last_indexed_article.id
    except News.DoesNotExist:
        return 0

def update_last_indexed_article(news_article):
    # Implement a method to update the 'indexed_on' field of the last indexed article
    # You can set 'indexed_on' to the current date and time using Django's timezone module
    news_article.indexed_on = timezone.now()
    news_article.save()


import os
import argparse
import time
import pandas as pd

from simple_slurm import Slurm
import visual_behavior_glm.PSTH as psth

parser = argparse.ArgumentParser(description='deploy glm fits to cluster')
parser.add_argument('--env-path', type=str, default='visual_behavior', metavar='path to conda environment to use')

def already_fit(row):
    filename1 = psth.get_hierarchy_filename(
        row.cell_type,
        row.response,
        row['data'],
        'all',
        row.nboots,
        ['visual_strategy_session'],
        'running_engaged_{}_{}'.format('visual',row.bin_num),
        meso=True,
        first=False,
        second=False)
    filename2 = psth.get_hierarchy_filename(
        row.cell_type,
        row.response,
        row['data'],
        'all',
        row.nboots,
        ['visual_strategy_session'],
        'running_engaged_{}_{}'.format('timing',row.bin_num),
        meso=True,
        first=False,
        second=False)
    return os.path.exists(filename1) & os.path.exists(filename2)
    #filename = psth.get_hierarchy_filename(
    #    row.cell_type,
    #    row.response,
    #    row['data'],
    #    'all',
    #    row.nboots,
    #    ['visual_strategy_session'],
    #    'running_engaged_{}_{}'.format('timing',row.bin_num),
    #    meso=True,
    #    first=False,
    #    second=True)
    #return os.path.exists(filename)

def get_bootstrap_jobs():
    nboots=10000
    base_jobs = [
        #{'cell_type':'exc','response':'image','data':'events','nboots':nboots},
        #{'cell_type':'sst','response':'image','data':'events','nboots':nboots},
        #{'cell_type':'vip','response':'image','data':'events','nboots':nboots},
        #{'cell_type':'exc','response':'omission','data':'events','nboots':nboots},
        #{'cell_type':'sst','response':'omission','data':'events','nboots':nboots},
        {'cell_type':'vip','response':'omission','data':'events','nboots':nboots}
        ]
    jobs = []
    for b in range(-5,21):
        for j in base_jobs:
            temp = j.copy()
            temp['bin_num'] = b
            jobs.append(temp)
    jobs = pd.DataFrame(jobs)
    return jobs

def make_job_string(row):
    arg_string = ''
    arg_string += ' --cell_type {}'.format(row.cell_type)
    arg_string += ' --response {}'.format(row.response)
    arg_string += ' --data {}'.format(row['data'])
    arg_string += ' --bin_num {}'.format(row.bin_num)
    arg_string += ' --nboots {}'.format(row.nboots)
    return arg_string

if __name__ == "__main__":
    args = parser.parse_args()
    python_executable = "{}/bin/python".format(args.env_path)
    print('python executable = {}'.format(python_executable))
    python_file = "/home/alex.piet/codebase/GLM/visual_behavior_glm/scripts/running_bootstrap.py"
    stdout_basedir = "/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm"
    stdout_location = os.path.join(stdout_basedir, 'job_records_running_bootstraps')
    if not os.path.exists(stdout_location):
        print('making folder {}'.format(stdout_location))
        os.mkdir(stdout_location)
    print('stdout files will be at {}'.format(stdout_location))

    job_count = 0
    jobs = get_bootstrap_jobs()

    for index, row in jobs.iterrows():
        if not already_fit(row):
            job_count += 1
            args_string = make_job_string(row)
            print('starting cluster job. job count = {}'.format(job_count))
            print('   ' + args_string)
            job_title = 'bootstraps'
            walltime = '5:00:00'
            mem = '50gb'
            job_id = Slurm.JOB_ARRAY_ID
            job_array_id = Slurm.JOB_ARRAY_MASTER_ID
            output = stdout_location+"/"+str(job_array_id)+"_"+str(job_id)+"_bootstrap.out"

            # instantiate a SLURM object
            slurm = Slurm(
                cpus_per_task=1,
                job_name=job_title,
                time=walltime,
                mem=mem,
                output= output,
                partition="braintv"
            )

            slurm.sbatch('{} {} {}'.format(
                    python_executable,
                    python_file,
                    args_string,
                )
            )
            time.sleep(0.001)


from flask import Blueprint, jsonify, abort
from flask.views import MethodView
from flask_login import login_required, current_user

from game_shop_api.extensions import admin_required
from game_shop_api.models import db, Genre, Game, User
from game_shop_api.schemas import GenreSchema, GameSchema, UserSchema, BalanceSchema

bp = Blueprint('api', __name__)

class GenreCollection(MethodView):
    def get(self):
        return jsonify(
            GenreSchema(many=True).dump(Genre.query.all())
        )

    @admin_required
    def post(self):
        schema = GenreSchema()
        data = schema.load_from_request()

        genre = Genre(**data)
        db.session.add(genre)
        db.session.commit()

        return schema.dump(genre), 201

class GenreItem(MethodView):
    def get(self, genre_id):
        return GenreSchema().dump(
            Genre.query.get_or_404(genre_id)
        )

    @admin_required
    def put(self, genre_id):
        genre = Genre.query.get_or_404(genre_id)
        schema = GenreSchema()
        data = schema.load_from_request()
        schema.populate_obj(data, genre)
        db.session.commit()
        return schema.dump(genre)

    @admin_required
    def delete(self, genre_id):
        genre = Genre.query.get_or_404(genre_id)
        db.session.delete(genre)
        db.session.commit()
        return '', 204

class GamesCollection(MethodView):
    def get(self):
        return jsonify(
            GameSchema(many=True).dump(Game.query.all())
        )

    @admin_required
    def post(self):
        schema = GameSchema()
        data = schema.load_from_request()

        game = Game(**data)
        db.session.add(game)
        db.session.commit()

        return schema.dump(game), 201

class GamesItem(MethodView):
    def get(self, game_id):
        return GameSchema().dump(
            Game.query.get_or_404(game_id)
        )

    @admin_required
    def put(self, game_id):
        game = Game.query.get_or_404(game_id)
        schema = GameSchema()
        data = schema.load_from_request()
        schema.populate_obj(data, game)
        db.session.commit()
        return schema.dump(game)

    @admin_required
    def delete(self, game_id):
        game = Game.query.get_or_404(game_id)
        db.session.delete(game)
        db.session.commit()
        return '', 204

class UsersCollection(MethodView):
    def post(self):
        schema = UserSchema()
        data = schema.load_from_request()

        user = User(**data)
        db.session.add(user)
        db.session.commit()

        return schema.dump(user), 201

class UserGames(MethodView):
    decorators = [login_required]

    def get(self):
        return jsonify(GameSchema(many=True).dump(current_user.games))

class Purchase(MethodView):
    decoratos = [login_required]

    def put(self, game_id):
        game = Game.query.get_or_404(game_id)

        if current_user.is_purchased(game):
            abort(409, 'You have already bought this game.')

        try:
            current_user.buy_game(game)
            db.session.commit()
        except RuntimeError as err:
            abort(412, err)

        return '', 201

class Balance(MethodView):

    decorators = [login_required]

    def get(self):
        return BalanceSchema().dump({'balance': current_user.balance})

    def post(self):
        schema = BalanceSchema()
        data = schema.load_from_request()
        current_user.balance += data['balance']
        db.session.commit()
        return schema.dump({'balance': current_user.balance})

class Profile(MethodView):

    decorators = [login_required]

    def get(self):
        return UserSchema().dump(current_user)

    def put(self):
        schema = UserSchema()
        schema.context['user'] = current_user
        data = schema.load_from_request()
        schema.populate_obj(data, current_user)
        db.session.commit()

        return schema.dump(current_user)

genres_api = GenreCollection.as_view('genres_api')
genres_item_api = GenreItem.as_view('genres_item_api')
games_api = GamesCollection.as_view('games_api')
games_item_api = GamesItem.as_view('games_item_api')
users_api = UsersCollection.as_view('users_api')
users_games = UserGames.as_view('user_games')
purchase_api = Purchase.as_view('purchase_api')
balance_api = Balance.as_view('balance_api')
profile_api = Profile.as_view('profile_api')

bp.add_url_rule('/genres/', view_func=genres_api)
bp.add_url_rule('/genres/<int:genre_id>', view_func=genres_item_api)
bp.add_url_rule('/games/', view_func=games_api)
bp.add_url_rule('/games/<int:game_id>', view_func=games_item_api)
bp.add_url_rule('/users/', view_func=users_api)
bp.add_url_rule('/user/games/', view_func=users_games)
bp.add_url_rule('/user/games/<int:game_id>', view_func=purchase_api)
bp.add_url_rule('/user/balance', view_func=balance_api)
bp.add_url_rule('/user', view_func=profile_api)


import argparse
import json
import os

full_path_to_file = None
path_to_preserved_file = None
label_titles = ['Heading', 'Education', 'Experience']

def parse_cli_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('tex_file')
    args = parser.parse_args()

    if args.tex_file:
        return args.tex_file

def get_all_lines_from_file():
    all_lines = list()
    with open(full_path_to_file, 'r') as tex_file:
        for line in tex_file:
            all_lines.append(line)
    return all_lines

def get_label_lines(label):
    label = '\label{{{}}}'.format(label)
    found_label = False
    label_lines = dict()

    with open(full_path_to_file, 'r') as tex_file:
        line_counter = 0
        for line in tex_file:
            if '\label' in line:
                if line.split()[0] == label:
                    found_label = True
                else:
                    found_label = False
            if found_label:
                label_lines[line_counter] = line
            line_counter += 1

    return label_lines

def get_proper_values(label_title):
    full_path_to_proper_values_file = '{}\{}'.format(dir_path, 'proper_values.json')
    with open(full_path_to_proper_values_file, 'r') as proper_values_file:
        full_proper_values = json.load(proper_values_file)
    proper_values = full_proper_values[label_title]

    proper_value_dictionary = dict()
    for feature_name, feature_value in proper_values.items():
        proper_value_dictionary[feature_name] = feature_value

    return proper_value_dictionary

def search_and_replace_improper_values(label_lines, proper_values):
    for line_number, line_content in label_lines.items():
        for phony_value, real_value in proper_values.items():
            if phony_value in line_content:
                line_content = line_content.replace(phony_value, real_value)
                label_lines[line_number] = line_content

def log_back_proper_values(all_lines, label_lines):
    for line_number in range(len(all_lines)):
        if label_lines.get(line_number):
            all_lines[line_number] = label_lines[line_number]

def write_values_to_file(lines, path):
    with open(path, 'w') as tex_file:
        for i in range(len(lines)):
            line = lines[i]
            tex_file.write(line)

if __name__ == "__main__":
    file_name = parse_cli_args()

    if file_name:
        dir_path = os.path.dirname(os.path.realpath(__file__))
        full_path_to_file = '{}\{}'.format(dir_path, file_name)
        path_to_preserved_file = '{}\{}'.format(dir_path, file_name.replace('.', '-old.'))
        all_lines = get_all_lines_from_file()
        preserved_lines = all_lines.copy()

        for label_title in label_titles:
            # Get line number of values.
            heading_label = '\label{{{}}}'.format(label_title)
            heading_lines = get_label_lines(heading_label)

            # Get a dictionary of 'improper values' mapped to their proper values, as defined by the user.
            proper_values = get_proper_values(label_title)

            if proper_values:
                # We have a dictionary of lines mapping the label to values to be replaced. Complete the replacements.
                search_and_replace_improper_values(heading_lines, proper_values)

                # Log back the real values to the list representation of the .tex document.
                log_back_proper_values(all_lines, heading_lines)

        # Write the proper values back to the tex file and the old values to another tex file.
        if all_lines:
            write_values_to_file(all_lines, full_path_to_file)
            write_values_to_file(preserved_lines, path_to_preserved_file)


import os
import sys
import json
import hashlib
import imp
import types
import warnings

warnings.filterwarnings("ignore")

try:
    import zsh
    eval = zsh.eval
except ImportError:
    def eval(command):
        print(command)

dirrc = sys.modules['dirrc'] = types.ModuleType('dirrc')

KNOWN_FILE_PATH = os.path.expanduser('~/.dirrc_known_files')

def query_yes_no(question, default="yes"):
    """Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is True for "yes" or False for "no".
    """
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")

try:
    with open(KNOWN_FILE_PATH) as file:
        known_files = json.loads(file.read())
except:
    known_files = {}

def check_rc(rc_path, hash):
    key = '{}:{}'.format(rc_path, hash)
    if key not in known_files:
        known_files[key] = query_yes_no(
            'Do you want to run an unknown file({})?'.format(key)
        )
        with open(KNOWN_FILE_PATH, 'w+') as file:
            file.write(json.dumps(known_files))
    return known_files[key]

def init_rc(rc_path):
    if not check_rc(rc_path, filehash(rc_path)):
        return
    print('init-{}'.format(rc_path))
    rc = imp.load_source(rc_path, rc_path)
    if 'init' in dir(rc):
        rc.init()

def exit_rc(rc_path):
    if not check_rc(rc_path, filehash(rc_path)):
        return
    print('exit-{}'.format(rc_path))
    rc = imp.load_source(rc_path, rc_path)
    if 'exit' in dir(rc):
        rc.exit()

def setenv(name, value):
    if isinstance(name, str):
        name = json.dumps(name)
    else:
        raise Error
    if isinstance(value, str):
        value = json.dumps(value)
    elif value is None:
        pass
    else:
        raise Error
    if value is None:
        eval('unset {}'.format(name))
    else:
        eval('export {}={}'.format(name, value))

dirrc.setenv = setenv
dirrc.system = eval

def filehash(path):
    return hashlib.md5(open(path, 'rb').read()).hexdigest()

# root 권한으로 실행하면 보안문제 발생할 가능성이 있음
if os.getuid() == 0 or os.geteuid == 0:
    exit()

# TODO: known file list를 로드

if len(sys.argv) < 2:
    path = os.getcwd()
else:
    path = os.path.abspath(sys.argv[1])

rcfiles = []
while path != '/':
    filepath = os.path.join(path, '.dirrc')
    if os.path.isfile(filepath):
        rcfiles.append([filepath, filehash(filepath)])
    path = os.path.abspath(os.path.join(path, os.pardir))
rcfiles.reverse()

current_rcfiles = json.loads(os.environ.get('DIRRC_LIST', '[]'))

while rcfiles[:len(current_rcfiles)] != current_rcfiles:
    path, hash = current_rcfiles[-1]
    exit_rc(path)
    current_rcfiles = current_rcfiles[:-1]

while rcfiles != current_rcfiles:
    path, hash = rcfiles[len(current_rcfiles)]
    init_rc(path)
    current_rcfiles.append([path, hash])

os.environ['DIRRC_LIST'] = json.dumps(current_rcfiles)


import cv2
import numpy as np
import math

objectWidth = 8.5
objectHeight = 11

print("Distance to object")
distance = input()
print("Distance: " + distance + " in")

#Define path
path = "OpenCV/resources/Logi.jpg"

def getRectangle(path):
    image = cv2.imread(path)
    imageHSV = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray,(5,5),0)
    canny = cv2.Canny(gray,30,150)

    frameMask = cv2.inRange(imageHSV,(150,0,0),(255,150,250))

    ret,binary = cv2.threshold(frameMask,100,255,cv2.THRESH_BINARY)

    contours, hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    cv2.imshow("Canny", binary)
    color = (255,0,0)

    if len(contours)>1:
        bestContour = max(contours, key = cv2.contourArea)

        minRect = cv2.minAreaRect(bestContour)
        box = cv2.boxPoints(minRect)
        box = np.int0(box)
        cv2.drawContours(image, [box], 0, color)

        point1 = box[0]
        point2 = box[1]
        point3 = box[2]

        print("We got 'em")
        cv2.imshow("Image",image)

    else: print("Hmmm")

    return(point1[0],point1[1],point2[0],point2[1],point3[0],point3[1])

def getPixelsToDistance(objectWidth,objectHeight,x1,x2,y1,y2,x3,y3,distance,image):

    #Pixels to inches
    wPix = math.sqrt(math.pow(x2-x1,2) + math.pow(y2-y1,2))
    hPix = math.sqrt(math.pow(x3-x2,2) + math.pow(y3-y2,2))
    multiplierW = image.shape[1] / wPix
    multiplierH = image.shape[0] / hPix
    inchesW = multiplierW * objectWidth
    inchesH = multiplierH * objectHeight
    horFOV = math.degrees( math.atan2( ( .5*inchesW ), int(distance) ) ) * 2
    verFOV = math.degrees( math.atan2( ( .5*inchesH ), int(distance) ) ) * 2

    return horFOV,verFOV

image = cv2.imread(path)

x1,x2,y1,y2,x3,y3 = getRectangle(path)

horizontalFOV,verticalFOV = getPixelsToDistance(objectWidth,objectHeight,x1,x2,y1,y2,x3,y3,distance,image)
print(horizontalFOV)
print(verticalFOV)

cv2.waitKey(0)


import json
import logging
import os

from birder.core.check import BaseCheck
from birder.core.queue import send
from birder.exceptions import ValidationError

from .redis import client

logger = logging.getLogger(__name__)

class Registry:
    def __init__(self, ctx=os.environ):
        self.context = ctx

    def initialize(self):
        pass

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

    def __iter__(self):
        checks = self._checks()
        if not self.order:
            self.sort_by()
        return iter([checks[v] for v in self.order if v in checks.keys() and checks[v].enabled])

    def disabled(self):
        checks = self._checks()
        if not self.order:
            self.sort_by()
        return iter([checks[v] for v in self.order if v in checks.keys() and not checks[v].enabled])

    def __getitem__(self, item):
        checks = self._checks()
        return checks[item]

    def _checks(self) -> [BaseCheck]:
        from birder.checks import Factory

        checks = {}
        names = sorted([k for k, v in self.context.items() if k.startswith('MONITOR_')])
        for varname in names:
            try:
                conn = self.context[varname]
                check = Factory.from_conn_string(*conn.split('|', 1), system=True)
                check.pk = varname
                for k, v in self.overriden(varname).items():
                    setattr(check, k, v)
                checks[varname] = check
            except Exception as e:
                logger.exception(e)

        dynamics = self.get_dynamic()
        for label, init_string in dynamics.items():
            try:
                check = Factory.from_conn_string(label.decode(), init_string.decode())
                check.pk = check.name
                override = client.get(f"override:{check.pk}")
                if override:
                    for k, v in json.loads(override).items():
                        setattr(check, k, v)
                checks[check.pk] = check
            except Exception as e:
                logger.exception(e)
        return checks

    @property
    def order(self):
        return [c.decode() for c in client.lrange("order", 0, client.llen("order"))]

    def sort_by(self, sequence=None):
        ordered = list(sequence or [])
        ordered.extend([c.pk for c in self._checks().values() if c.pk not in ordered])
        ordered.reverse()
        p = client.pipeline()
        p.delete("order")
        p.lpush("order", *ordered)
        p.execute()

    def override(self, hkey, **kwargs):
        client.set(f"override:{hkey}", json.dumps(kwargs))

    def overriden(self, hkey):
        return json.loads(client.get(f"override:{hkey}") or "{}")

    def get_dynamic(self):
        return client.hgetall('dynamic')

    def add_dynamic(self, label, init_string):
        data = client.hgetall('dynamic')
        if label in data:
            raise ValueError('--')
        data[label.upper()] = init_string
        client.hmset('dynamic', data)
        self.sort_by()
        send(label)

    def enable(self, hkey):
        kwargs = self.overriden(hkey)
        kwargs['enabled'] = True
        self.override(hkey, **kwargs)
        send(hkey)

    def remove(self, hkey):
        check = self[hkey]
        if check.system:
            kwargs = self.overriden(hkey)
            kwargs['enabled'] = False
            self.override(hkey, **kwargs)
        else:
            client.hdel('dynamic', hkey.encode())
            self.sort_by()

        send(hkey)

registry = Registry()


import sys
import math
import pandas as pd
import matplotlib.pyplot as plt

def rrw_error_adj_formula(e, d, s):
    return (e * d) / s - d * (e - 1) + 1

def a6519_helper(n):
    return n & (-n)

def a6519_sequence(n):
    sequence = []
    for i in range(1,n+1):
        sequence.append(a6519_helper(i))
    return sequence

def luby_sequence(n):
    sequence = [1]
    counts = {1: 1}

    for i in range(1, n):
        last_term = sequence[-1]

        if counts[last_term] % 2 == 0:
            next_term = 2 * last_term
        else:
            next_term = 1

        sequence.append(next_term)

        if next_term in counts:
            counts[next_term] += 1
        else:
            counts[next_term] = 1

    return sequence

def expected_num_luby_rws(d, g, precision, b, scale, seq):
  if g >= b**d:
    prob_succ = 1
    prob_fail = 0
  else:
    prob_succ = g / (b**d)
    prob_fail = ((b**d) - g) / b**d

  expected_val = 0

  exp_count = 0
  succ_count = 0

  for walk in range(0, precision):
    current_walk = seq[walk]
    if scale == 'none':
        current_walk = current_walk
    if scale == 'd':
        current_walk = d * current_walk
    if scale == 'd/2':
        current_walk = (d/2) * current_walk
    if scale == '2d':
        current_walk = (2 * d) * current_walk
    if scale == 'luby*2':
        current_walk = 2 * current_walk
    if scale == 'luby*4':
        current_walk = 4 * current_walk
    if scale == 'luby*8':
        current_walk = 8 * current_walk
    if scale == 'luby*16':
        current_walk = 16 * current_walk

    if current_walk < d:
      exp_count += current_walk

    else:
      succ_count += 1
      expected_val +=(exp_count + d) *  prob_succ * prob_fail**(succ_count-1)
      # print("debug", exp_count,d,prob_succ, prob_fail**(succ_count-1), succ_count)
      exp_count += current_walk

  return expected_val + 1

def make_rws_table(depth, num_goals, precision, b, scale):
  data = []
  for g in range(1,num_goals+1):

    data_to_append = []
    for d in range(1,depth+1):
        expected_rws = 1
        if g <= b**d: expected_rws = expected_num_luby_rws(d, g, precision, b, scale)
        expected_rws = round(expected_rws,3)
        data_to_append.append(expected_rws)
        # else: data_to_append.append(1)
    data.append(data_to_append)
      # print(f"expected  of walks: {expected_rws}, depth: {d}, num_g: {g}")

  df = pd.DataFrame(data).transpose()
  # print(df)
  df.columns = [f"Num_goals={g}" for g in range(1, num_goals+1)]
  df.index = [f"Depth={d}" for d in range(1, depth+1)]
  # df = df.astype(int)  # Specify integer data type
  return df

def make_dataframe(depth, precision, branch, scale, formula, type, seq):
    to_csv = []
    for b in range(2,3):
        for d in range(2, depth+1):
            g = 0
            bfs_min = float(((b**d) - 1)/(b - 1)) + 1

            rws_expected = float('inf')
            # print(bfs_min, rws_expected)
            while rws_expected - 0.000001 >= bfs_min:
                g += 1
                s = g / (b ** d)
                if type == 'luby' or type == 'a6519':
                    rws_expected = formula(d=d, g=g, precision=precision, b=b, scale=scale, seq=seq)
                if type =='rrw_e':
                    rws_expected = formula(e=1, d=d, s=s)
                # print(rws_expected)
                # print(f"d ={d}, g={g}, rws={rws_expected}")
                print(f'd={d}, {rws_expected}, {bfs_min}, g={g}')
                if rws_expected <= bfs_min:
                    to_csv.append([b, d, g])
                    break

    df = pd.DataFrame(to_csv)
    df.columns = ["b", "d", "cutoff"]

    return df

if __name__ == '__main__':
    b = 2
    d = 15
    g = 12
    precision = 30000
    luby = luby_sequence(precision)
    a6519 = a6519_sequence(precision)
    print('hi')

    # print(expected_num_luby_rws(d=11, g=1, precision=precision, b=b, scale='none'))

    df_plot1 = make_dataframe(depth=d, precision=precision, branch=b, scale='none', formula=expected_num_luby_rws, type='luby', seq=luby)
    # df_plot2 = make_dataframe(depth=d, precision=precision, branch=b, scale='d', formula=expected_num_luby_rws, type='luby', seq=luby)
    # df_plot3 = make_dataframe(depth=d, precision=precision, branch=b, scale='d/2', formula=expected_num_luby_rws, type='luby', seq=luby)
    # df_plot4 = make_dataframe(depth=d, precision=precision, branch=b, scale='none', formula=rrw_error_adj_formula, type='rrw_e', seq=luby)
    # df_plot5 = make_dataframe(depth=d, precision=precision, branch=b, scale='2d', formula=expected_num_luby_rws, type='luby', seq=luby)
    df_plot6 = make_dataframe(depth=d, precision=precision, branch=b, scale='luby*2', formula=expected_num_luby_rws, type='luby', seq=luby)
    df_plot9 = make_dataframe(depth=d, precision=precision, branch=b, scale='luby*4', formula=expected_num_luby_rws, type='luby', seq=luby)
    df_plot10 = make_dataframe(depth=d, precision=precision, branch=b, scale='luby*8', formula=expected_num_luby_rws, type='luby', seq=luby)
    df_plot11 = make_dataframe(depth=d, precision=precision, branch=b, scale='luby*16', formula=expected_num_luby_rws, type='luby', seq=luby)

    # print(df_plot1, df_plot2, df_plot3, df_plot4)

    fig, ax = plt.subplots(figsize=(10, 6))

    # # Plot each DataFrame on the same axis
    ax.plot(df_plot1['d'], df_plot1['cutoff'], label='luby')
    # ax.plot(df_plot2['d'], df_plot2['cutoff'], label='luby_d')
    # ax.plot(df_plot3['d'], df_plot3['cutoff'], label='luby_d/2')
    # ax.plot(df_plot4['d'], df_plot4['cutoff'], label='RRWe_1')
    # ax.plot(df_plot5['d'], df_plot5['cutoff'], label='luby_2d')
    ax.plot(df_plot6['d'], df_plot6['cutoff'], label='luby_*2')
    # ax.plot(df_plot7['d'], df_plot7['cutoff'], label='a6519')
    # ax.plot(df_plot8['d'], df_plot8['cutoff'], label='a6519_d')
    ax.plot(df_plot6['d'], df_plot9['cutoff'], label='luby_*4')
    ax.plot(df_plot6['d'], df_plot10['cutoff'], label='luby_*8')
    ax.plot(df_plot6['d'], df_plot11['cutoff'], label='luby_*16')

    # Set labels and legend
    ax.set_xlabel('Depth')
    ax.set_ylabel('Goal Cutoff')
    ax.set_title(f'b = {b}')
    ax.legend()

    # Show the plot
    plt.show()

    # print(make_rws_table(depth=d, num_goals=g, precision=precision, b=b, scale='d/2'))


#!/usr/bin/python

import glob
import argparse
import os
import sys
from psnr_analyzer import PsnrAnalyzer

from report_manager import ReportData, ReportManager

from transcoder import Transcoder
from configuration import prepare_config

def prepare_output_dir(output_dir):
        if os.path.exists(output_dir):
            print("Clear output folder")
            filelist = glob.glob(os.path.join(output_dir, "*"))
            for file in filelist:
                print("Remove: " + file)
                os.remove(file)
        else:
            print("Create output folder")
            os.mkdir(output_dir)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Ping script")

    parser.add_argument("-i", dest="input", help="Origin input file", required=True)
    parser.add_argument("-c", dest="config", help="Config file", default="config.json", required=False)

    args = parser.parse_args()
    if not vars(args):
        parser.print_usage()
        sys.exit()

    original_file_size = os.path.getsize(args.input)
    config = prepare_config(args)

    prepare_output_dir(config.output_dir)

    analyzers = { "PSNR": PsnrAnalyzer(config.ffmpeg_tools, config.hwaccel, config.output_dir) }
    transcoder = Transcoder(config.ffmpeg_tools, config.hwaccel, config.output_dir)
    report_manager = ReportManager(config.output_dir)

    row_id = 1
    report_datas = []

    for profile in config.profiles:
        transcode_file_path = transcoder.transcode(args.input, profile)
        transcode_file_size = os.path.getsize(transcode_file_path)
        difference_in_percent = 100 - (transcode_file_size / original_file_size) * 100
        report_data = ReportData(row_id, transcode_file_path, profile.build_output_file_name(), transcode_file_size, difference_in_percent)
        report_datas.append(report_data)
        row_id += 1

    for report_data in report_datas:
        for item in config.metrics:
            log_file = analyzers[item].analyze(report_data.transcode_file_path, args.input, report_data.transcode_file_name)
            report_data.add_report(item, log_file)

    for report_data in report_datas:
        report_manager.add_report(report_data)

    report_manager.close()


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 22 23:57:46 2018

@author: zhengjt
"""

import json
import urllib3

#利用urllib2获取网络数据
http = urllib3.PoolManager()
#url ="http://f.apiplus.net/ssq.json"
url ="http://caipiaojieguo.com/api/lottrey?biaoshi=ssq&format=json&rows=10"
r = http.request('GET', url)
value=json.loads(r.data)
rootlist=value.keys()
for rootkey in rootlist:
    print(rootkey)
datas=value['data']

num=len(datas)
i=0
#逐个号码解析出来

#while(i<num):
#    str_code=datas[i]['opencode']
#    blue_ball=str_code[-2:]
#    print('blue ball: ',blue_ball)
#    str_code_split=str_code[:-3].split(',')
#    j=0
#    while(j<len(str_code_split)):
#        print(datas[i]['expect'],str_code_split[j])
#        j=j+1
#    i=i+1

#检索是否中奖
targe_red_ball=['13', '03', '14', '16', '25', '28']
while(i<num):
#    from彩票结果网
    str_code=datas[i]['result']
    blue_ball=str_code[-2:]
    print('blue ball: ',blue_ball)
    red_ball_list=str_code[:-3].split(',')
#    开彩网
#    str_code=datas[i]['opencode']
#    blue_ball=str_code[-2:]
#    print('blue ball: ',blue_ball)
#    red_ball_list=str_code[:-3].split(',')
#    向字典加入内容项
    datas[i]['blue_ball']=blue_ball
    datas[i]['red_ball']=red_ball_list
#    print(datas[i]['expect']+':',red_ball_list)
    print(datas[i]['qishu']+':',red_ball_list)
    j=0
    result=[0,0,0,0,0,0]
    while(j<len(red_ball_list)):
        result[j]=red_ball_list.count(targe_red_ball[j])
        j+=1
    print('中奖结果：',result)
    i+=1

#把字典写到文件
jsObj = json.dumps(datas)

fileObject = open('double_color_ball.json', 'w')
fileObject.write(jsObj)
fileObject.close()


from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..utils import str_to_int

class NineGagIE(InfoExtractor):
    IE_NAME = '9gag'
    _VALID_URL = r'https?://(?:www\.)?9gag(?:\.com/tv|\.tv)/(?:p|embed)/(?P<id>[a-zA-Z0-9]+)(?:/(?P<display_id>[^?#/]+))?'

    _TESTS = [{
        'url': 'http://9gag.com/tv/p/Kk2X5/people-are-awesome-2013-is-absolutely-awesome',
        'info_dict': {
            'id': 'kXzwOKyGlSA',
            'ext': 'mp4',
            'description': 'This 3-minute video will make you smile and then make you feel untalented and insignificant. Anyway, you should share this awesomeness. (Thanks, Dino!)',
            'title': '\"People Are Awesome 2013\" Is Absolutely Awesome',
            'uploader_id': 'UCdEH6EjDKwtTe-sO2f0_1XA',
            'uploader': 'CompilationChannel',
            'upload_date': '20131110',
            'view_count': int,
        },
        'add_ie': ['Youtube'],
    }, {
        'url': 'http://9gag.com/tv/p/aKolP3',
        'info_dict': {
            'id': 'aKolP3',
            'ext': 'mp4',
            'title': 'This Guy Travelled 11 countries In 44 days Just To Make This Amazing Video',
            'description': "I just saw more in 1 minute than I've seen in 1 year. This guy's video is epic!!",
            'uploader_id': 'rickmereki',
            'uploader': 'Rick Mereki',
            'upload_date': '20110803',
            'view_count': int,
        },
        'add_ie': ['Vimeo'],
    }, {
        'url': 'http://9gag.com/tv/p/KklwM',
        'only_matching': True,
    }, {
        'url': 'http://9gag.tv/p/Kk2X5',
        'only_matching': True,
    }, {
        'url': 'http://9gag.com/tv/embed/a5Dmvl',
        'only_matching': True,
    }]

    _EXTERNAL_VIDEO_PROVIDER = {
        '1': {
            'url': '%s',
            'ie_key': 'Youtube',
        },
        '2': {
            'url': 'http://player.vimeo.com/video/%s',
            'ie_key': 'Vimeo',
        },
        '3': {
            'url': 'http://instagram.com/p/%s',
            'ie_key': 'Instagram',
        },
        '4': {
            'url': 'http://vine.co/v/%s',
            'ie_key': 'Vine',
        },
    }

    def _real_extract(self, url):
        mobj = re.match(self._VALID_URL, url)
        video_id = mobj.group('id')
        display_id = mobj.group('display_id') or video_id

        webpage = self._download_webpage(url, display_id)

        post_view = self._parse_json(
            self._search_regex(
                r'var\s+postView\s*=\s*new\s+app\.PostView\({\s*post:\s*({.+?})\s*,\s*posts:\s*prefetchedCurrentPost',
                webpage, 'post view'),
            display_id)

        ie_key = None
        source_url = post_view.get('sourceUrl')
        if not source_url:
            external_video_id = post_view['videoExternalId']
            external_video_provider = post_view['videoExternalProvider']
            source_url = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['url'] % external_video_id
            ie_key = self._EXTERNAL_VIDEO_PROVIDER[external_video_provider]['ie_key']
        title = post_view['title']
        description = post_view.get('description')
        view_count = str_to_int(post_view.get('externalView'))
        thumbnail = post_view.get('thumbnail_700w') or post_view.get('ogImageUrl') or post_view.get('thumbnail_300w')

        return {
            '_type': 'url_transparent',
            'url': source_url,
            'ie_key': ie_key,
            'id': video_id,
            'display_id': display_id,
            'title': title,
            'description': description,
            'view_count': view_count,
            'thumbnail': thumbnail,
        }


# coding=utf-8
import os
import glob
from biocluster.agent import Agent
from biocluster.tool import Tool
from biocluster.core.exceptions import OptionError
import unittest
import psutil
import time
import threading

__author__ = 'liubinxu'

class Trinity2DistributeAgent(Agent):
    """
    Trinity
    """
    def __init__(self, parent):
        super(Trinity2DistributeAgent, self).__init__(parent)
        options = [
            {'type': 'infile', 'name': 'distribute_cmd', 'format': 'denovo_rna_v2.common'},
            {'default': 2, 'type': 'int', 'name': 'cpu'}, # cpu不要太大否则Java容易报错
            {'default': 0, 'type': 'float', 'name': 'mis_rate'},
            {"name": "trinity_version", "type": "string", "default": "2.8.5"},
        ]
        self.add_option(options)

    def check_options(self):
        if not self.option("distribute_cmd").is_set:
            raise OptionError("必须设置参数distribute_cmd", code = "32006201")
        if self.option("cpu")>5:
            raise OptionError("cpu设置过大，不要大于 5", code = "32006202")

    def set_resource(self):
        self._cpu = self.option('cpu')
        self._memory = "{}G".format(self.option('cpu')  * 20)

    def end(self):
        result_dir = self.add_upload_dir(self.output_dir)
        result_dir.add_relpath_rules([
            [".", "", ""]
            ])
        super(Trinity2DistributeAgent, self).end()

def parafly_process_control(tool_pid, para_cmd):
    """
    通过tool的process id 监控子线程中的butterfly
    """
    with open(para_cmd, 'r') as para_f:
        para_num = len(para_f.readlines())
    no_bfly = 0
    time_start = time.time()

    with open("para_control.txt", 'a+') as control_log:
        while 1:
            c_thread_pid = os.getpid()
            time_step = 10
            p = psutil.Process(tool_pid)
            p_children = p.children(recursive=True)
            bfly_process_num = 0
            # 监控子进程任务数, butterfly有无超时
            is_run_para = False
            for p_child in p_children:
                try:
                    cmd_name = str(p_child.cmdline())
                except:
                    control_log.write("进程不存在 {}\n".format(p_child.pid))
                    cmd_name = ""
                try:
                    cmd_time = float(p_child.cpu_times().user)
                except:
                    cmd_time = 0

                if 'ParaFly' in cmd_name:
                    is_run_para = True
                if "Butterfly.jar" in cmd_name and cmd_time > 300:
                    control_log.write("进程{} {}使用超时{}s，自动结束\n".format(str(p_child.pid), cmd_name, cmd_time))
                    p_child.terminate()
                else:
                    control_log.write("进程{} {} 运行时间{}s\n".format(str(p_child.pid), cmd_name, cmd_time))
                    if "Butterfly.jar" in cmd_name:
                        bfly_process_num += 1
            # 计算parafly完成数
            finish_num = 0
            if os.path.exists(para_cmd + ".completed"):
                with open(para_cmd + ".completed", 'r') as para_finish_f:
                    finish_num = len(para_finish_f.readlines())

            if bfly_process_num == 0:
                no_bfly += time_step
            else:
                no_bfly = 0

            time_now = time.time()
            time_str = time.localtime()

            # control_log.write("time is {} thread id is {}\n".format(time_str, c_thread_pid))
            '''
            if bfly_process_num == 0 and float(finish_num)/float(para_num) > 0.8:
                control_log.write("程序完成,监控结束\n")
                break
            elif time_now - time_start > 3000 and no_bfly > 1000:
                control_log.write("程序已运行超过3000秒 且 连续1000秒没有butterfly脚本运行,监控结束\n")
                break
            '''
            if not is_run_para:
                control_log.write("程序完成,监控结束\n")
                break
            elif time_now - time_start > 3000 and no_bfly > 1000:
                control_log.write("程序已运行超过3000秒 且 连续1000秒没有butterfly脚本运行,监控结束\n")
                break
            else:
                time.sleep(time_step)
        control_log.write("循环结束")
    return

class Trinity2DistributeTool(Tool):
    """
    Trinity
    """
    def __init__(self, config):
        super(Trinity2DistributeTool, self).__init__(config)
        software_dir = self.config.SOFTWARE_DIR
        if self.option("trinity_version") == "2.5.0":
            self.parafly = '/bioinfo/denovo_rna_v2/trinityrnaseq-Trinity-v2.5.0/trinity-plugins/ParaFly-0.1.0/bin/ParaFly'
        elif self.option("trinity_version") == "2.8.5":
            self.parafly = '/bioinfo/denovo_rna_v2/trinityrnaseq-2.8.5/trinity-plugins/ParaFly-0.1.0/bin/ParaFly'

        else:
            self.set_error("trinity version error", code="32006205")

        self.perl = self.config.SOFTWARE_DIR + '/program/perl-5.24.0/bin/'
        self.bowtie = self.config.SOFTWARE_DIR + '/bioinfo/denovo_rna_v2/bowtie2-2.3.3.1-linux-x86_64/'
        self.java = self.config.SOFTWARE_DIR + '/program/sun_jdk1.8.0/bin'
        python_path = self.config.SOFTWARE_DIR + '/miniconda2/bin/'
        self.set_environ(PATH=python_path)
        self.set_environ(PATH=self.perl)
        self.set_environ(PATH=self.bowtie)
        self.set_environ(PATH=self.java)

        if self.option("trinity_version") == "2.8.5":
            self.salmon = self.config.SOFTWARE_DIR + '/bioinfo/rna/salmon-0.14.1/bin/'
            self.jellyfish = self.config.SOFTWARE_DIR + '/bioinfo/denovo_rna_v2/jellyfish-2.3.0/bin/'
            self.set_environ(PATH=self.salmon)
            self.set_environ(PATH=self.jellyfish)
            self.gcc = software_dir + '/gcc/5.1.0/bin'
            self.gcc_lib = software_dir + '/gcc/5.1.0/lib64'
            self.set_environ(PATH=self.gcc, LD_LIBRARY_PATH=self.gcc_lib)

    def run_parafly(self):
        cmd = '{} '.format(self.parafly)
        cmd += '-{} {} '.format("c", self.option("distribute_cmd").prop['path'])
        cmd += '-{} {} '.format("CPU", self.option("cpu"))
        cmd += '-v -shuffle'
        cmd_name = 'trinity2distribute'
        command = self.add_command(cmd_name, cmd, ignore_error=True)

        # 启动监控
        tool_pid = os.getpid()
        command.run()
        try:
            control_t = threading.Thread(target=parafly_process_control, args=(tool_pid, self.option("distribute_cmd").prop['path']))
            control_t.start()
            self.logger.info("启动监控线程")
        except:
            self.set_error("无法启动监控线程", code="32006206")

        self.wait()
        control_t.join()

        self.logger.info("启动监控线程结束")
        if command.return_code == 0:
            self.logger.info("{} Finished successfully".format(cmd_name))
        elif os.path.exists(os.path.join(self.work_dir, 'FailedCommands')):
            fail_file = open(os.path.join(self.work_dir, 'FailedCommands'))
            fail_num = len(fail_file.readlines())
            if fail_num < 50:
                self.logger.info("unfinished command little than 50 will jumped")
            else:
                self.logger.info("unfinished command more than 50 will rerun")
                command.rerun()
                self.wait()
                # self.set_error("{} Failed. >>>{}".format(cmd_name, cmd))
        elif command.return_code is None:
            self.logger.warn("{} Failed and returned None, we will try it again.".format(cmd_name))
            command.rerun()
            self.wait()
            if command.return_code is 0:
                self.logger.info("{} Finished successfully".format(cmd_name))
            else:
                self.set_error("%s Failed. >>>%s", variables = (cmd_name, cmd), code = "32006203")
        else:
            self.set_error("%s Failed. >>>%s", variables = (cmd_name, cmd), code = "32006204")

    def set_output(self):
        pass

    def run(self):
        super(Trinity2DistributeTool, self).run()
        self.run_parafly()
        self.set_output()
        self.end()

class TestFunction(unittest.TestCase):
    """
    This is test for the tool. Just run this script to do test.
    """
    def test(self):
        import random
        from mbio.workflows.single import SingleWorkflow
        from biocluster.wsheet import Sheet
        test_dir='/mnt/ilustre/users/sanger-dev/workspace/20180102/Single_de_tr_data2.2'
        data = {
            "id": "Trinity2Distribute" + str(random.randint(1, 10000)),
            "type": "tool",
            "name": "denovo_rna_v2.trinity2_distribute",
            "instant": False,
            "options": dict(
                distribute_cmd=test_dir + "/" + "trinity_cmd_distribute09",
                cpu="4",
                mis_rate="0",
            )
           }
        wsheet = Sheet(data=data)
        wf = SingleWorkflow(wsheet)
        wf.run()

if __name__ == '__main__':
    unittest.main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sat Jul  8 13:52:25 2017

@author: Gaspar
"""
import pygame
from pygame.locals import *
from sys import exit
import GameTools
from GameTools import *
import random
import math

#Iniciamos Pygame
pygame.init()
screen = pygame.display.set_mode(GameTools.SCREEN_SIZE)
pygame.display.set_caption("THE MONKEY GAME")

#Crea el monkey y las bananas
monkey = Player(screen, SCREEN_WIDTH/2, SCREEN_HEIGHT - 20) #Creamos el Monkey al centro de la pantalla
#num_bananas = 20
#lista_bananas = []
#lista_bananas = createListaBananas(screen, num_bananas)

lista_frutas = []

#Objeto para registro del tiempo
clock = pygame.time.Clock()

#Ciclo infinito para dibujar
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() #necesario para cerrar la ventana al presionar X sino se usa se traba el juego
            running = False
            exit()

    #Revisa que tecla fue presionada
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        if checkLimitX(monkey, 'LEFT'):
            monkey.mover_left(PLAYER_SPEED)

    elif keys[K_RIGHT]:
        if checkLimitX(monkey, 'RIGHT'):
            monkey.mover_right(PLAYER_SPEED)

    screen.fill((0, 0, 0))

    monkey.dibuja() #Dibuja el monkey

    un_coco = crearFruta(screen, WHITE) #Crea un coco
    una_banana = crearFruta(screen, BLUE) #Crea una banana
    lista_frutas.append(un_coco)
    lista_frutas.append(una_banana)

    for f in lista_frutas:     #Dibuja y mueve las bananas
        if checkLimitY(f):     #Revisa si la fruta ya alcanzo el piso de la pantalla
            lista_frutas.remove(f)
        else:
            #revisa si hubo colision, actualiza el contador y la bandera de paro
            (col, record) = colision(f, monkey)
            if col:
                lista_frutas.remove(f)

        record += record
        f.dibuja()
        f.mover()

    #Refresca la pantalla
    pygame.display.update()


import tkinter
import customtkinter

customtkinter.set_appearance_mode("Dark")
customtkinter.set_default_color_theme("dark-blue")

class App(customtkinter.CTk):
	def __init__(self):
		super ().__init__()

		# configue the window
		self.geometry(f"{1280}x{720}")
		self.title("Image Editing Screen")
		self.resizable(0, 0)

		# configure frame on right side for edit buttons
		self.edit_frame = customtkinter.CTkFrame(master=self, width=240, height=704)
		self.edit_frame.pack(padx=8, pady=8, side="right", fill="both", expand=True)

		# configure frame on left side for displaying/handling image
		self.image_frame = customtkinter.CTkFrame(master=self, width=960, height=704)
		self.image_frame.pack(padx=8, pady=8, side="left", fill ="both", expand=True)

	def create_buttons(self):
		# configure edit buttons that will be in the edit button frame
		self.colorselect_optionmenu = customtkinter.CTkOptionMenu(master=self.edit_frame, height=50, width=200, values=["Default", "other"], anchor=tkinter.CENTER, command = self.color_select)
		self.colorselect_optionmenu.place(relx=0.5, rely=0.05, anchor=tkinter.CENTER)

		self.design_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Select Design", border_width=2, command=self.design_select)
		self.design_button.place(relx=0.5, rely=0.17, anchor=tkinter.CENTER)

		self.zoom_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Zoom In/Zoom Out", border_width=2, command=self.zoom)
		self.zoom_button.place(relx=0.5, rely=0.29, anchor=tkinter.CENTER)
		self.zoom_slider = customtkinter.CTkSlider(master=self.edit_frame, from_=25, to=400, number_of_steps=75, command=self.zoom_slider)
		self.zoom_slider.place(relx=0.5, rely=0.34, anchor=tkinter.CENTER)

		self.undo_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Undo Last Change", border_width=2, command=self.undo)
		self.undo_button.place(relx=0.5, rely=0.41, anchor=tkinter.CENTER)

		self.reset_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Reset to Original", border_width=2, command=self.reset)
		self.reset_button.place(relx=0.5, rely=0.53, anchor=tkinter.CENTER)

		self.back_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Back to Home", border_width=2, command=self.back)
		self.back_button.place(relx=0.5, rely=0.87, anchor=tkinter.CENTER)

		self.saveas_button = customtkinter.CTkButton(master=self.edit_frame, height=50, width=200, text="Save Image As", border_width=2, command=self.save_as)
		self.saveas_button.place(relx=0.5, rely=0.95, anchor=tkinter.CENTER)

	# method will handle the changing of the color after the user has selected and option from the dropdown
	def color_select(self, choice):
		print(f"Color select menu clicked, color selected: {choice}")

	# method will allow the user to choose an image file that is a design to add onto the clothing
	def design_select(self):
		print("Design selection button has been pressed")

	# method will handle the zooming in and out of the selected image to edit
	def zoom(self):
		print("Zoom button has been pressed")

	def zoom_slider(self, value):
		print(f"Zoom slider has been adjusted, new value: {value}")

	# un does the last change the user made to the image
	def undo(self):
		print("Undo button has been pressed")

	# resets the selected image to its default when it was first selected
	def reset(self):
		print("Reset button has been pressed")

	# takes the user back to the home screen
	def back(self):
		print("Back button has been pressed")

	# prompts the user to save the newly edited image to their computer
	def save_as(self):
		print("Save As button has been pressed")

if __name__ == "__main__":
	app = App()
	app.create_buttons()
	app.mainloop()


import sys
import os
import unittest
import pytest
import numpy as np
from itertools import product
from pclam import make_lineload, Config
from pclam.util import call_write_lineloads
from pclam.io import write_lineload

import geometry.create_test_surface as cts

#probs a better way to do this but oh well
if '--save' in sys.argv:
    save_exact = True
else:
    save_exact =False
rocket_grids = ['smooth', 'bumpy']
rocket_fields = ['smooth', 'bumpy']
rocket_orientation = ['xy']

flat_grids = ['smooth', 'cubic', 'step']
flat_fields= ['step', 'wavy']
flat_orientation = ['xy', 'xz', 'yz']
rocket_node_to_elem = {}
rocket_data_by_node = {}
flat_node_to_elem = {}
flat_data_by_node = {}

for grid, field, orientation in product(rocket_grids, rocket_fields, rocket_orientation):
    case = (grid, field, orientation)
    node_to_elem, data_by_node = cts.create_sample_surfaces('rocket', grid, field, orientation)
    rocket_node_to_elem[case] = node_to_elem
    rocket_data_by_node[case] = data_by_node

for grid, field, orientation in product(flat_grids, flat_fields, flat_orientation):
    case = (grid, field, orientation)
    node_to_elem, data_by_node = cts.create_sample_surfaces('flat', grid, field, orientation, save_exact)
    flat_node_to_elem[case] = node_to_elem
    flat_data_by_node[case] = data_by_node

@pytest.mark.parametrize("grid", flat_grids)
@pytest.mark.parametrize("field", flat_fields)
@pytest.mark.parametrize("orientation", flat_orientation)
@pytest.mark.parametrize("nll", (33, 67))
@pytest.mark.parametrize("bin_edge",(True, False))
def test_flat_plate(grid, field, orientation, nll, bin_edge, save):
    config = Config({})
    case = (grid, field, orientation)
    for axis in orientation:
        config.base_name = '_'.join(('flat',) + case + (str(nll),))
        config.axis = axis
        config.nll_points = nll
        config.bin_edges_on_minmax = bin_edge
        data_by_node = flat_data_by_node[case]
        node_to_elem = flat_node_to_elem[case]
        ll = make_lineload(config, data_by_node, node_to_elem)
        if save:
            if bin_edge:
                edge = 'b1'
            else:
                edge = 'b0'
            output_dir = os.path.join('lineloads', edge, axis+'_axis')
            os.makedirs(output_dir, exist_ok=True)
            config.output_dir = output_dir
            config.variables_saved = ['all']
            call_write_lineloads(ll, config)

def test_large_bin_number(save):
    config = Config({})
    case = ('smooth', 'wavy', 'xy')
    config.base_name = 'large_bin_number'
    config.nll_points = 1000
    config.output_dir = os.path.join('lineloads', 'x_axis')
    data_by_node = flat_data_by_node[case]
    node_to_elem = flat_node_to_elem[case]
    ll = make_lineload(config, data_by_node, node_to_elem)
    if save:
        output_dir = os.path.join('lineloads', 'other')
        os.makedirs(output_dir, exist_ok=True)
        out_fn = config.base_name + '.dat'
        write_lineload(ll[:,:,4], os.path.join(output_dir, out_fn), config.axis, config.profile_axis)

def test_small_bin_number(save):
    config = Config({})
    case = ('smooth', 'wavy', 'xy')
    config.base_name = 'small_bin_number'
    config.nll_points = 20
    config.output_dir = os.path.join('lineloads', 'x_axis')
    data_by_node = flat_data_by_node[case]
    node_to_elem = flat_node_to_elem[case]
    ll = make_lineload(config, data_by_node, node_to_elem)
    if save:
        output_dir = os.path.join('lineloads', 'other')
        os.makedirs(output_dir, exist_ok=True)
        out_fn = config.base_name + '.dat'
        write_lineload(ll[:,:,4], os.path.join(output_dir, out_fn), config.axis, config.profile_axis)

@pytest.mark.parametrize("grid",rocket_grids)
@pytest.mark.parametrize("field", rocket_fields)
@pytest.mark.parametrize("orientation", rocket_orientation)
@pytest.mark.parametrize("bin_edge",(True, False))
@pytest.mark.parametrize("nll",(33, 67))
def test_rocket(grid, field, orientation, bin_edge, nll, save):
    #something up with this grid definition which causes some divide by 0 warnings
    config = Config({})
    config.nll_points = nll
    case = (grid, field, orientation)
    for axis in orientation:
        config.base_name = '_'.join(('rocket',) + case + (str(nll),))
        config.axis = axis
        config.output_dir = os.path.join('lineloads', axis+'_axis')
        data_by_node = rocket_data_by_node[case]
        node_to_elem = rocket_node_to_elem[case]
        ll = make_lineload(config, data_by_node, node_to_elem)

        if save:
            if bin_edge:
                edge = 'b1'
            else:
                edge = 'b0'
            output_dir = os.path.join('lineloads', edge, axis+'_axis')
            os.makedirs(output_dir, exist_ok=True)
            config.output_dir = output_dir
            config.variables_saved = ['cp', 'cfx', 'all']
            call_write_lineloads(ll, config)

if __name__ == '__main__':
    unittest.main()


from bottle import get, post, run, request, response
from json import dumps

customers = {}

@get('/customers')
def all_customers():
    response.content_type = 'application/json'
    return dumps(customers.values())

@get('/customers/<name>')
def get_customer(name=None):
    customer = customers.get(name, None)
    if customer:
        return customer
    else:
        response.status = 404
        return 'Customer {} not found.'.format(name)

@post('/customers')
def create_customer():
    customer = request.json
    name = customer['name']
    customers[name] = customer
    return {'status': 'ok'}

customers['test1'] = {'name': 'test1', 'born': 1815}

run(host='0.0.0.0', port=8888, debug=True)


from discord.ext.commands import bot

async def eat(client: bot, ctx, users, resources, food_type, amount, icons):

    # User
    user = ctx.message.author
    nourishment_needed = 100 - users[user.id]["nourishment"]

    # Check Input Food
    if food_type is None:
        await client.say("{}, here's a list of food items you can eat:\n"
                         "{}Berries (1 Nourishment)\n"
                         "{}Apples (5 Nourishment)\n"
                         "...WIP...".format(user.mention, icons["icon"]["berries"], icons["icon"]["apples"]))
    else:
        # Possible Food Items
        if food_type == "berries":
            ufood_amount = resources[user.id]["berries"]
            ufood_nourishment = 1
        elif food_type == "apples":
            ufood_amount = resources[user.id]["apple"]
            ufood_nourishment = 5
        else:
            ufood_amount = 0
            ufood_nourishment = 0
            food_type = "unknown"
        ufood_amount = int(ufood_amount)

        # Check Input Amount
        amount_until_full = nourishment_needed / ufood_nourishment
        if amount_until_full >= ufood_amount:
            amount_until_full = ufood_amount
        if amount == "all":
            amount = amount_until_full
        elif amount == "a":
            amount = amount_until_full
        elif amount is None:
            amount = amount_until_full
        try:
            amount = int(amount)
        except ValueError:
            amount = 1
        amount = int(amount)

        # Food Required
        if users[user.id]["nourishment"] == 100:
            await client.say("{}, you're already full!".format(user.mention))
        elif food_type == "unknown":
            await client.say("{}, this is an unknown type of food, check your spelling!".format(user.mention))
        elif ufood_amount == 0:
            await client.say("{}, you don't have enough of that type of food!".format(user.mention))
        else:
            resources[user.id][food_type] -= amount
            replenished_nourishment = ufood_nourishment * amount
            users[user.id]["nourishment"] += replenished_nourishment
            await client.say("{}, you ate {} food items and replenished {} nourishment!"
                             .format(user.mention, amount, replenished_nourishment))

            # Reset to 100 if to high
            if users[user.id]["nourishment"] > 100:
                users[user.id]["nourishment"] = 100


import sys
import unittest
from sdl2 import SDL_Init, SDL_Quit, SDL_INIT_EVERYTHING
from sdl2 import clipboard
from sdl2.stdinc import SDL_TRUE

def is_win_or_mac():
    return sys.platform in ("win32", "cygwin", "darwin")

class SDLClipboardTest(unittest.TestCase):
    __tags__ = ["sdl"]

    @classmethod
    def setUpClass(cls):
        SDL_Init(SDL_INIT_EVERYTHING)

    @classmethod
    def tearDownClass(cls):
        SDL_Quit()

    # @unittest.skipIf(not is_win_or_mac(), "we would need a SDL window")
    # @interactive()
    # def test_SDL_HasClipboardText(self):
    #     doprint("Please put some text on the clipboard")
    #     self.assertEqual(clipboard.SDL_HasClipboardText(), SDL_TRUE)

    # @unittest.skipIf(not is_win_or_mac(), "we would need a SDL window")
    # @interactive("Does the shown value match the clipboard content?")
    # def test_SDL_GetClipboardText(self):
    #     doprint("Please put some text on the clipboard")
    #     retval = clipboard.SDL_GetClipboardText()
    #     doprint("Clipboard content: '%s'" % retval)

    # @unittest.skipIf(not is_win_or_mac(), "we would need a SDL window")
    # def test_SDL_SetClipboardText(self):
    #     self.assertEquals(clipboard.SDL_SetClipboardText(b"Test content"), 0)
    #     retval = clipboard.SDL_GetClipboardText()
    #     self.assertEqual(retval, b"Test content")

    #     self.assertEquals(clipboard.SDL_SetClipboardText(b""), 0)
    #     retval = clipboard.SDL_GetClipboardText()
    #     self.assertEqual(retval, b"")

    #     self.assertEquals(clipboard.SDL_SetClipboardText(b"Test content"), 0)
    #     retval = clipboard.SDL_GetClipboardText()
    #     self.assertEqual(retval, b"Test content")

    #     self.assertEquals(clipboard.SDL_SetClipboardText(None), 0)
    #     retval = clipboard.SDL_GetClipboardText()
    #     self.assertEqual(retval, b"")

if __name__ == '__main__':
    sys.exit(unittest.main())


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import argparse
import json
import sys
import configparser
import os
from io import open
import requests
from inscrawler import InsCrawler
from inscrawler.settings import override_settings
from inscrawler.settings import prepare_override_settings

def usage():
    return """
        python crawler.py posts -u cal_foodie -n 100 -o ./output
        python crawler.py posts_full -u cal_foodie -n 100 -o ./output
        python crawler.py profile -u cal_foodie -o ./output
        python crawler.py profile_script -u cal_foodie -o ./output
        python crawler.py hashtag -t taiwan -o ./output

        The default number for fetching posts via hashtag is 100.
    """

def get_posts_by_user(username, number, detail, debug):
    ins_crawler = InsCrawler(has_screen=debug)
    return ins_crawler.get_user_posts(username, number, detail)

def get_profile(username):
    ins_crawler = InsCrawler()
    return ins_crawler.get_user_profile(username)

def get_profile_from_script(username):
    ins_cralwer = InsCrawler()
    return ins_cralwer.get_user_profile_from_script_shared_data(username)

def get_posts_by_hashtags(tag, number, debug):
    ins_crawler = InsCrawler(has_screen=debug)

    ret=ins_crawler.get_latest_posts_by_tag(tag, number)

    return ins_crawler.get_latest_posts_by_tag(tag, number)

def arg_required(args, fields=[]):
    for field in fields:
        if not getattr(args, field):
            parser.print_help()
            sys.exit()

def output(data, filepath):
    out = json.dumps(data, ensure_ascii=False)
    if filepath:
        with open(filepath, "w", encoding="utf8") as f:
            f.write(out)
    else:
        print(out)

if __name__ == "__main__":

    output_folder='./output_folder/'

    filenames=os.listdir(output_folder)

    if os.path.isdir('./img_folder/'):
        pass
    else:
        os.mkdir('./img_folder/')

    for filename in filenames:
        print(filename)
        count=0
        with open('{}/{}'.format(output_folder,filename), 'r', encoding="utf8") as read_file:
            dict_data = json.load(read_file)
            hashtag=filename.split('.')[0]

            if os.path.isdir('./img_folder/{}'.format(hashtag)):
                pass
            else:
                os.mkdir('./img_folder/{}'.format(hashtag))

            #print(dict_data)
            for k in dict_data:
                print(k['img_url'])

                filename=k['img_url'].split('/')[-1].split('.')[0]
                if os.path.isfile("./img_folder/{}/{}.png".format(hashtag,filename)):
                    print("./img_folder/{}/{}.png".format(hashtag,filename),'exist')
                    continue
                response = requests.get(k['img_url'])
                file = open("./img_folder/{}/{}.png".format(hashtag,filename), "wb")
                file.write(response.content)
                file.close()
                count+=1
                #break