from typing import Dict, Union, Optional

class Computer:
    def __init__(
        self,
        description: str,
        processor_type: str,
        hard_drive_capacity: int,
        memory: int,
        os: str,
        made_in_year: int,
        price: int
    ):
        self.description = description
        self.processor_type = processor_type
        self.hard_drive_capacity = hard_drive_capacity
        self.memory = memory
        self.os = os
        self.made_in_year = made_in_year
        self.price = price

class ComputerShop:
    def __init__(self):
        self.shop_inventory: Dict[int, Computer] = {}
        self.item_id_generator: int = 0

    def buy(self, computer: Computer) -> int:
        self.item_id_generator = self.item_id_generator + 1
        self.shop_inventory[self.item_id_generator] = computer
        return self.item_id_generator

    def update_price(self, item_id: int, new_price: int) -> None :
        if item_id in self.shop_inventory:
            self.shop_inventory[item_id].price = new_price
        else:
            print('Cannot update price for an item that does not exist')

    def sell(self, item_id: int) -> None:
        del self.shop_inventory[item_id]

    def print_item(self, item_id: int, shop_inventory: dict) -> None:
        if item_id in self.shop_inventory:
            print(f'Item with id {item_id} : {self.shop_inventory[item_id]}')
        else:
            print(f'Item with id {item_id} does not exist')

class RebuyShop(ComputerShop):
    shop_inventory: Dict[int, Dict[str, Union[str, int, bool]]] = {}
    item_id: int = 0

    def refurbish(self, item_id: int, shop_inventory: dict, new_os: Optional[str] = None) -> None:
        if item_id in shop_inventory:

            computer = shop_inventory[item_id]

            if int(computer['made_in_year']) < 2000:
                computer['price'] = 0
            elif int(computer['made_in_year']) < 2010:
                computer['price'] = 250
            elif int(computer['made_in_year']) < 2018:
                computer['price'] = 650
            else:
                computer['price'] = 1000

            if new_os is not None:
                computer['os'] = new_os
        else:
            print('Cannot refurbish an item that does not exist')

def create_computer(
                    description: str,
                    processor_type: str,
                    hard_drive_capacity: int,
                    memory: int,
                    os: str,
                    made_in_year: int,
                    price: int
                    ) -> Dict[str, Union[str, int, bool]]:
    return {
            'description': description,
            'processor_type': processor_type,
            'hard_drive_capacity': hard_drive_capacity,
            'memory': memory,
            'os': os,
            'made_in_year': made_in_year,
            'price': price
        }

if __name__ == "main":
    # Computer Shop
    print('CREATE COMPUTER')
    computer_shop = ComputerShop()

    computer = create_computer(
        "Triston XPT 3352",
        "1.4 GHz Quad‑Core Intel Core i5",
        256, 16,
        "Unix", 2016, 1400)

    print(computer)

    print('\nCOMPUTER SHOP')
    print('#' * 20)
    cid: int = computer_shop.buy(computer)

    print('UPDATE PRICE')
    computer_shop.update_price(cid, 1450)
    computer_shop.print_item(cid)

    print('SELL')
    computer_shop.sell(cid)

    print('PRINT AFTER SELLING')
    computer_shop.print_item(cid)

# Rebuy Shop
    rebuy_shop = RebuyShop()
    rcid: int = rebuy_shop.buy(computer)
    print('\nREBUY SHOP')
    print('#' * 20)

    print('REFURBISH')
    rebuy_shop.refurbish(rcid, "Linux Mint")
    rebuy_shop.print_item(rcid)

    print('R SELL')
    rebuy_shop.sell(rcid)

    print('PRINT AFTER R SELLING')
    rebuy_shop.print_item(rcid)


import torch
import torch.nn as nn
import numpy as np
import logging
import sys
import os
import config
from config import args
import constants

from smpl_family.create_smpl_models import create_model
from utils.projection import vertices_kp3d_projection_withfov, vertices_kp3d_projection
from utils.rot_6D import rot6D_to_angular
from maps_utils.relative_parser import parse_age_cls_results
from maps_utils.debug_utils import print_dict

import torch.nn.functional as F

class SMPLWrapper(nn.Module):
    def __init__(self):
        super(SMPLWrapper, self).__init__()
        logging.info('Building SMPL family for relative learning in temporal!')

        self.smpl_model = create_model(args().smpl_model_type)
        #self.part_name = ['cam_trans', 'global_orient', 'body_pose', 'smpl_betas']
        #self.part_idx = [3, 3,  (args().smpl_joint_num-1) * 3, 11 if not args().separate_smil_betas else 21]
        self.part_name = ['cam', 'global_orient', 'body_pose', 'smpl_betas']
        self.part_idx = [3, 6,  (args().smpl_joint_num-1) * 6, 11 if not args().separate_smil_betas else 21]
        self.params_num = np.array(self.part_idx).sum()

    def parse_params_pred(self, params_pred, without_cam=False):
        params_dict = self.pack_params_dict(
            params_pred, without_cam=without_cam)
        params_dict['smpl_betas'], cls_dict = self.process_betas(
            params_dict['smpl_betas'])
        vertices, joints44_17 = self.smpl_model(betas=params_dict['smpl_betas'], \
                            poses=params_dict['smpl_thetas'], separate_smil_betas=args().separate_smil_betas)
        return vertices, joints44_17, params_dict, cls_dict

    def forward(self, outputs, meta_data, calc_pj2d_org=True):
        vertices, joints44_17, params_dict, cls_dict = self.parse_params_pred(outputs['params_pred'])

        if 'world_global_rots' in outputs:
            world_vertices, world_joints44_17 = self.smpl_model(betas=params_dict['smpl_betas'].detach(), \
                            poses=torch.cat([outputs['world_global_rots'], params_dict['smpl_thetas'][:,3:].detach()],1), separate_smil_betas=args().separate_smil_betas)
            outputs.update({'world_verts': world_vertices, 'world_j3d': world_joints44_17[:, :args().joint_num], 'world_joints_h36m17': world_joints44_17[:, args().joint_num:]})

        outputs.update(
            {'verts': vertices, 'j3d': joints44_17[:, :args().joint_num], 'joints_h36m17': joints44_17[:, args().joint_num:], **params_dict, **cls_dict})

        outputs.update(vertices_kp3d_projection(outputs['j3d'], outputs['joints_h36m17'], outputs['cam'], \
                input2orgimg_offsets=meta_data['offsets'] if calc_pj2d_org else None, presp=args().perspective_proj, vertices=outputs['verts'] if args().compute_verts_org else None))
        if args().dynamic_augment:
            dyna_pouts = vertices_kp3d_projection(outputs['world_j3d'].detach(), outputs['joints_h36m17'].detach(), outputs['world_cams'], \
                input2orgimg_offsets=meta_data['offsets'] if calc_pj2d_org else None, presp=args().perspective_proj, vertices=outputs['verts'] if args().compute_verts_org else None)
            outputs.update({'world_pj2d': dyna_pouts['pj2d'], 'world_trans': dyna_pouts['cam_trans'], 'world_joints_h36m17': dyna_pouts['pj2d_h36m17']})
            if args().compute_verts_org:
                outputs.update({'world_verts_camed_org': dyna_pouts['verts_camed_org']})

        return outputs

    def pack_params_dict(self, params_pred, without_cam=False):
        idx_list, params_dict = [0], {}
        for i, (idx, name) in enumerate(zip(self.part_idx, self.part_name)):
            if without_cam and i == 0:
                idx_list.append(0)
                continue
            idx_list.append(idx_list[i] + idx)
            params_dict[name] = params_pred[:, idx_list[i]: idx_list[i+1]].contiguous()
        #print_dict(params_dict)
        if params_dict['global_orient'].shape[-1] == 6:
            params_dict['body_pose'] = rot6D_to_angular(params_dict['body_pose'])
            params_dict['global_orient'] = rot6D_to_angular(params_dict['global_orient'])
        N = params_dict['body_pose'].shape[0]
        params_dict['body_pose'] = torch.cat([params_dict['body_pose'], torch.zeros(
            N, 6).to(params_dict['body_pose'].device)], 1)
        params_dict['smpl_thetas'] = torch.cat(
            [params_dict['global_orient'], params_dict['body_pose']], 1)
        return params_dict

    def process_betas(self, betas_pred):
        # TODO: don't have video training data with kid offset
        if not args().learn_relative_age:
            betas_pred[:, 10] = 0

        # the obvious variance of SMPL body shape is in -8~8.
        #smpl_betas = betas_pred[:, :10]
        #smpl_betas[:,:2] = smpl_betas[:,:2]* 6.
        kid_offsets = betas_pred[:, 10]
        #genders_pred = self.softmax(betas_pred[:,11:])
        #age_genders = torch.cat([kid_offsets.unsqueeze(1), genders_pred],1).contiguous()
        Age_preds = parse_age_cls_results(kid_offsets)
        #Gender_cls_preds = Age_Gender_cls_preds[:,1]
        #smpl_gender = torch.index_select(torch.tensor([[1,0],[0,1],[0,0]],device=Gender_cls_preds.device),0,Gender_cls_preds).float().to(smpl_betas.device)

        # whether to detach the learning of kid_offsets in vertex
        #betas_pred = torch.cat([smpl_betas, kid_offsets.unsqueeze(1).detach()],1).contiguous()
        betas_pred = betas_pred[:, :10]

        cls_dict = {'Age_preds': Age_preds, 'kid_offsets_pred': kid_offsets}
        return betas_pred, cls_dict

def merge_smpl_outputs(results_list):
    if len(results_list) == 1:
        return results_list[0][0]
    results = {k: None for k in results_list[0][0].keys()}
    map_inds = torch.cat([torch.where(mask)[0] for _, mask in results_list], 0)
    for key in list(results.keys()):
        results[key] = torch.cat([result[key]
                                  for result, _ in results_list], 0)[map_inds]
    return results

"""
            outputs.update(vertices_kp3d_projection_withfov(outputs['j3d'], outputs['joints_h36m17'], \
                outputs['cam_trans'], outputs['pred_fovs'],
                input2orgimg_offsets=meta_data['offsets'] if calc_pj2d_org else None, vertices=outputs['verts'] if args().inference_video else None))
            if args().dynamic_augment:
                dyna_pouts = vertices_kp3d_projection_withfov(outputs['j3d'].detach(), outputs['joints_h36m17'].detach(), \
                    outputs['world_trans'], outputs['pred_fovs'], input2orgimg_offsets=meta_data['offsets'] if calc_pj2d_org else None, vertices=outputs['verts'] if args().inference_video else None)
                outputs.update({'world_pj2d': dyna_pouts['pj2d'], 'world_pj2d_h36m17': dyna_pouts['pj2d_h36m17']})
"""


import matplotlib.pyplot as plt
import numpy as np

def ploting(datax, datay, model,model_2,num_examples=6):
    fig, ax = plt.subplots(nrows=num_examples, ncols=4, figsize=(18,4*num_examples))
    m = datax.shape[0]
    for row_num in range(num_examples):
        image_indx = np.random.randint(m)
        image_arr = model(datax[image_indx:image_indx+1]).squeeze(0).detach().cpu().numpy()
        image_arr_nested = model_2(datax[image_indx:image_indx+1]).squeeze(0).detach().cpu().numpy()
        ax[row_num][0].imshow(np.transpose(datax[image_indx].cpu().numpy(), (1,2,0))[:,:,0])
        ax[row_num][0].set_title("Orignal Image")
        ax[row_num][1].imshow(np.squeeze((image_arr > 0.40)[0,:,:].astype(int)))
        ax[row_num][1].set_title("Segmented Image localization, Unet")
        ax[row_num][2].imshow(np.squeeze((image_arr_nested > 0.40)[0,:,:].astype(int)))
        ax[row_num][2].set_title("Segmented Image localization, Unet++")
        ax[row_num][3].imshow(np.transpose(datay[image_indx].cpu().numpy(), (1,2,0))[:,:,0])
        ax[row_num][3].set_title("Target image")
    plt.show()


from PIL import Image, ImageDraw, ImageFont, ImageFilter

from src.Resource import resource, conf

RGB_TEXT = (255, 255, 255)

class AlbumCoverGenerator:
    """
    专辑图片生成
    """
    def __init__(self):
        self.resource = resource()
        self.img = Image.open(self.resource.image_file_path) # 将打开的图片缩放尺寸到1280*1280
        self.img = self.img.resize((1280, 1280), Image.ANTIALIAS)

    def draw_text(self, text, position, font, color, outline_color, outline_width):
        draw = ImageDraw.Draw(self.img)
        x, y = position
        # 绘制文本的阴影
        draw.text((x - 1, y - 1), text, fill=color, font=font, stroke_width=outline_width, stroke_fill=outline_color)
        draw.text((x + 1, y + 1), text, fill=color, font=font, stroke_width=outline_width, stroke_fill=outline_color)
        # 绘制文本
        draw.text(position, text, fill=color, font=font)

    def add_image_surface(self):
        background = Image.new('RGB', (700, 700), (255, 255, 255))
        self.img.paste(background, (334, 190))
        surface = Image.open(self.resource.image_file_path)
        surface = surface.resize((668, 668))
        self.img.paste(surface, (350, 206))

    def generate_album_cover(self):

        width, height = self.img.size

        # 计算放大比例
        if width < 1080:
            scale = 1080 / width
        else:
            scale = 1
        while width * scale < 2440 or height * scale < 1080:
            scale += 0.1

        # 缩放图片
        new_width = int(width * scale)
        new_height = int(height * scale)
        self.img = self.img.resize((new_width, new_height))

        # 裁剪图片
        left = (new_width - 2440) / 2
        top = (new_height - 1080) / 2
        right = left + 2440
        bottom = top + 1080
        self.img = self.img.crop((left, top, right, bottom))
        self.img = self.img.filter(ImageFilter.GaussianBlur(radius=42))

        # 添加你的专辑封面
        self.add_image_surface()

        # 添加文本
        font_large = ImageFont.truetype(self.resource.font, conf['AlbumCover']['FONT_SIZE_LARGE'])
        font_small = ImageFont.truetype(self.resource.font, conf['AlbumCover']['FONT_SIZE_SMALL'])
        outline_color = (0, 0, 0)
        outline_width = 3
        assert type(self.resource.title) is str, "专辑标题异常，请手动填写专辑信息"
        assert type(self.resource.artist) is str, "专辑艺术家异常，请手动填写专辑信息"
        assert type(self.resource.album) is str, "专辑名称异常，请手动填写专辑信息"
        self.draw_text(self.resource.title, (1152, 240), font_large, RGB_TEXT, outline_color, outline_width)
        self.draw_text(self.resource.artist, (1152, 450), font_small, RGB_TEXT, outline_color, outline_width)
        self.draw_text(self.resource.album, (1152, 540), font_small, RGB_TEXT, outline_color, outline_width)

        # 保存图像
        self.img.save(self.resource.pic_savepath, "PNG")
        # 显示图像
        self.img.show()


import io
import sys
import os
from common import *
from dotenv import load_dotenv

def parse_alipay(filename):
    account = "Liabilities:CreditCard:CITIC"
    account_alipay = "Assets:VirtualCard:Alipay:ZZS"
# 交易号 ,商家订单号 ,交易创建时间 ,付款时间 ,最近修改时间 ,交易来源地 ,类型 ,交易对方 ,商品名称 ,金额（元） ,收/支 ,交易状态 ,服务费（元） ,成功退款（元） ,备注,资金状态 ,

    with open(filename, 'r', encoding="gbk") as f:
        for line in f.readlines()[5:-8]:
            line = line.strip()
            data = line.split(',')

            date = data[2].split(' ')[0] # 付款时间
            counterpart = data[7].strip() # 交易对方
            description = data[8].strip() # 商品名称
            currency = "CNY"
            money_status = data[15].strip()
            amount = float(data[9])
            if money_status == "资金转移": # 资金状态
                if data[11] == "还款成功":
                    print("还款成功")
                    print(line)
                elif data[11] == "交易成功" and description == "余额宝-自动转入":
                    continue
                else:
                    print(line)
            elif money_status == "已支出":
                print(get_notsure(date, counterpart, description, amount, currency, account))
            elif money_status == "已收入":
                if "余额宝" in description:
                    print(get_yuebao(date, description, amount, currency, account_alipay))
                elif data[11].strip() == "退款成功": # 退款
                    print(get_notsure(date, counterpart, description, -1 * amount, currency, account))
                else: # 二手
                    print(sell_something(date, description, amount, currency, account_alipay))
                    print(line)
            elif data[11].strip() == "交易关闭": # 交易状态
                continue
            else:
                print(line)

if __name__ == '__main__':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
    load_dotenv()
    alipay_path = os.getenv("ALIPAYPATH")
    parse_alipay(alipay_path)


from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation
import sys
# https://en.wikipedia.org/wiki/Two-body_problem
# https://towardsdatascience.com/modelling-the-three-body-problem-in-classical-mechanics-using-python-9dc270ad7767
# https://evgenii.com/blog/two-body-problem-simulator/

x1 = []
x2 = []
y1 = []
y2 = []
z1 = []
z2 = []
t = []
R = []
r = []

# default parameters, overwritten by config.ini
PARAMETER_m1 = 1
PARAMETER_m2 = 0.5
PARAMETER_step = 0.1
PARAMETER_points = 1000

G = 6.67408e-11 # N-m2/kg2
m_nd = 1.989e+30 #kg #mass of the sun
r_nd = 5.326e+12 #m #distance between stars in Alpha Centauri
v_nd = 30000 #m/s #relative velocity of earth around the sun
t_nd = 79.91*365*24*3600*0.51 #s #orbital period of Alpha Centauri#Net constants

FACTOR_K1 = 1#G*t_nd*m_nd/(r_nd**2*v_nd)     uncomment for ex2/sci
FACTOR_K2 = 1#v_nd*t_nd/r_nd                 uncomment for ex2/sci
ZERO_R = True                              #R is zeroed

def init(pnumberofvalues):
    global x1,x2,y1,y2,z1,z2,t,R,r
    t = np.zeros(pnumberofvalues)
    x1 = np.zeros([pnumberofvalues,2])
    x2 = np.zeros([pnumberofvalues,2])
    y1 = np.zeros([pnumberofvalues,2])
    y2 = np.zeros([pnumberofvalues,2])
    z1 = np.zeros([pnumberofvalues,2])
    z2 = np.zeros([pnumberofvalues,2])

    R = np.zeros([pnumberofvalues,3])
    r = np.zeros([pnumberofvalues,3])

# Finds value of y for a given t using step size h
# and initial value y0 at x0.
'''
k1 = h * dydt(t0, y)
k2 = h * dydt(t0 + 0.5 * h, y + 0.5 * k1)
k3 = h * dydt(t0 + 0.5 * h, y + 0.5 * k2)
k4 = h * dydt(t0 + h, y + k3)

y[i] = y[i-1] + (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4)
'''
def twobodiesExample2(pnumberofpoints, h, K1, K2, m1, m2):
    global x1, x2, y1, y2, z1, z2
    import scipy as sci#Import matplotlib and associated modules for 3D and animations
    # Count number of iterations using step size or
    # step height h
    #Define masses
    t=[]
    r=[]
    R=[]
    r1=[-0.5,0,0] #m
    r2=[0.5,0,0] #m#Convert pos vectors to arrays
    r1=np.array(r1,dtype="float64")
    r2=np.array(r2,dtype="float64")#Find Centre of Mass
    r_com=(m1*r1+m2*r2)/(m1+m2)#Define initial velocities
    v1=[0.01,0.01,0] #m/s
    v2=[-0.05,0,-0.1] #m/s#Convert velocity vectors to arrays
    v1=np.array(v1,dtype="float64")
    v2=np.array(v2,dtype="float64")#Find velocity of COM
    v_com=(m1*v1+m2*v2)/(m1+m2)

    #Package initial parameters
    init_params=np.array([r1,r2,v1,v2]) #create array of initial params
    init_params=init_params.flatten() #flatten array to make it 1D
    time_span=np.linspace(0,1,pnumberofpoints) #8 orbital periods and 500 points#Run the ODE solver
    import scipy.integrate

    #A function defining the equations of motion
    def TwoBodyEquations(w,t,G,m1,m2,K1,K2):
        r1=w[:3]
        r2=w[3:6]
        v1=w[6:9]
        v2=w[9:12]
        r=sci.linalg.norm(r2-r1) #Calculate magnitude or norm of vector
        dv1bydt=K1*m2*(r2-r1)/r**3
        dv2bydt=K1*m1*(r1-r2)/r**3
        dr1bydt=K2*v1
        dr2bydt=K2*v2
        r_derivs=np.concatenate((dr1bydt,dr2bydt))
        derivs=np.concatenate((r_derivs,dv1bydt,dv2bydt))
        return derivs

    two_body_sol=sci.integrate.odeint(TwoBodyEquations,init_params,time_span,args=(G,m1,m2,K1,K2))

    r1_sol=two_body_sol[:,:3]
    r2_sol=two_body_sol[:,3:6]

    init(pnumberofpoints)

    for idx in range(len(x1)):
        x1[idx][0] = r1_sol[idx,0]
        y1[idx][0] = r1_sol[idx,1]
        z1[idx][0] = r1_sol[idx,2]
        x2[idx][0] = r2_sol[idx,0]
        y2[idx][0] = r2_sol[idx,1]
        z2[idx][0] = r2_sol[idx,2]
    '''
    fig=plt.figure(figsize=(15,15))#Create 3D axes
    ax=fig.add_subplot(111,projection="3d")#Plot the orbits
    ax.plot(r1_sol[:,0],r1_sol[:,1],r1_sol[:,2],color="darkblue")
    ax.plot(r2_sol[:,0],r2_sol[:,1],r2_sol[:,2],color="tab:red")#Plot the final positions of the stars
    ax.scatter(r1_sol[-1,0],r1_sol[-1,1],r1_sol[-1,2],color="darkblue",marker="o",s=100,label="Alpha Centauri A")
    ax.scatter(r2_sol[-1,0],r2_sol[-1,1],r2_sol[-1,2],color="tab:red",marker="o",s=100,label="Alpha Centauri B")#Add a few more bells and whistles
    ax.set_xlabel("x-coordinate",fontsize=14)
    ax.set_ylabel("y-coordinate",fontsize=14)
    ax.set_zlabel("z-coordinate",fontsize=14)
    ax.set_title("Visualization of orbits of stars in a two-body system\n",fontsize=14)
    ax.legend(loc="upper left",fontsize=14)
    plt.show()
    '''

def twobodiesExample1(pnumberofpoints, h, K1, K2, m1, m2):
    global r,R,x1,x2,y1,y2,z1,z2,t
    # Count number of iterations using step size or
    # step height h
    t0 = 0

    init(pnumberofpoints)

    x1[0][0] = 1
    y1[0][0] = 0
    z1[0][0] = 0
    x2[0][0] = 0
    y2[0][0] = 0
    z2[0][0] = 0

    x1[0][1] = 0
    y1[0][1] = 0
    z1[0][1] = 0
    x2[0][1] = 0
    y2[0][1] = 1.59687
    z2[0][1] = 0

    r[0][0] = (x2[0][0]-x1[0][0])
    r[0][1] = (y2[0][0]-y1[0][0])
    r[0][2] = (z2[0][0]-z1[0][0])

    R[0][0]=(m1*x1[0][0]+m2*x2[0][0])/(m1+m2)   # TODO
    R[0][1]=(m1*y1[0][0]+m2*y2[0][0])/(m1+m2)   # TODO
    R[0][2]=(m1*z1[0][0]+m2*z2[0][0])/(m1+m2)   # TODO

    a = [h/2.0, h/2.0, h, 0]
    b = [h/6.0, h/3.0, h/3.0, h/6.0]

    def deriveVelocityAndAcceleration(px1,py1,pz1,px2,py2,pz2,m1,m2):
        global K1, K2
        rnormed = math.sqrt((px1[0]-px2[0])**2 + (py1[0]-py2[0])**2 + (pz1[0]-pz2[0])**2)
        dx1 = [0,0]
        dy1 = [0,0]
        dz1 = [0,0]

        dx2 = [0,0]
        dy2 = [0,0]
        dz2 = [0,0]

        dx1[0] = px1[1]
        dy1[0] = py1[1]
        dz1[0] = pz1[1]

        dx2[0] = px2[1]
        dy2[0] = py2[1]
        dz2[0] = pz2[1]

        dx1[1] = (m2)  * (px2[0]-px1[0])/ (rnormed**3)
        dy1[1] = (m2) * (py2[0]-py1[0])/ (rnormed**3)
        dz1[1] = (m1) * (pz2[0]-pz1[0]) / (rnormed**3)

        dx2[1] = (m1) * (px1[0]-px2[0]) / (rnormed**3)
        dy2[1] = (m1) * (py1[0]-py2[0]) / (rnormed**3)
        dz2[1] = (m2) * (pz1[0]-pz2[0]) / (rnormed**3)
        return dx1,dy1,dz1,dx2,dy2,dz2

    for i in range(1, len(t)):
        xn1 = np.zeros(2)     # Position and velocity
        xn2 = np.zeros(2)
        yn1 = np.zeros(2)
        yn2 = np.zeros(2)
        zn1 = np.zeros(2)
        zn2 = np.zeros(2)

        # Runge-Kutta
        xn1[0] = x1[i-1][0]
        xn1[1] = x1[i-1][1]

        xn2[0] = x2[i-1][0]
        xn2[1] = x2[i-1][1]

        yn1[0] = y1[i-1][0]
        yn1[1] = y1[i-1][1]

        yn2[0] = y2[i-1][0]
        yn2[1] = y2[i-1][1]

        zn1[0] = z1[i-1][0]
        zn1[1] = z1[i-1][1]

        zn2[0] = z2[i-1][0]
        zn2[1] = z2[i-1][1]

        #vars
        xvar1 = [0,0]
        yvar1 = [0,0]
        zvar1 = [0,0]

        xvar2 = [0,0]
        yvar2 = [0,0]
        zvar2 = [0,0]

        xvar1[0] = xn1[0]
        yvar1[0] = yn1[0]
        zvar1[0] = zn1[0]

        xvar1[1] = xn1[1]
        yvar1[1] = yn1[1]
        zvar1[1] = zn1[1]

        xvar2[0] = xn2[0]
        yvar2[0] = yn2[0]
        zvar2[0] = zn2[0]

        xvar2[1] = xn2[1]
        yvar2[1] = yn2[1]
        zvar2[1] = zn2[1]

        #k
        kx1 = [0,0]
        kx2 = [0,0]
        ky1 = [0,0]
        ky2 = [0,0]
        kz1 = [0,0]
        kz2 = [0,0]

        for idx in range(4):

            dxvar1,dyvar1,dzvar1, dxvar2,dyvar2,dzvar2 = deriveVelocityAndAcceleration(xvar1,yvar1,zvar1,xvar2,yvar2,zvar2,m1, m2)

            dxvar1[0] = K2*dxvar1[0]
            dyvar1[0] = K2*dyvar1[0]
            dzvar1[0] = K2*dzvar1[0]

            dxvar1[1] = K1*dxvar1[1]
            dyvar1[1] = K1*dyvar1[1]
            dzvar1[1] = K1*dzvar1[1]

            kx1[0] = kx1[0] + dxvar1[0]*b[idx]
            ky1[0] = ky1[0] + dyvar1[0]*b[idx]
            kz1[0] = kz1[0] + dzvar1[0]*b[idx]
            kx1[1] = kx1[1] + dxvar1[1]*b[idx]
            ky1[1] = ky1[1] + dyvar1[1]*b[idx]
            kz1[1] = kz1[1] + dzvar1[1]*b[idx]

            kx2[0] = kx2[0] + dxvar2[0]*b[idx]
            ky2[0] = ky2[0] + dyvar2[0]*b[idx]
            kz2[0] = kz2[0] + dzvar2[0]*b[idx]
            kx2[1] = kx2[1] + dxvar2[1]*b[idx]
            ky2[1] = ky2[1] + dyvar2[1]*b[idx]
            kz2[1] = kz2[1] + dzvar2[1]*b[idx]

            # next step
            xvar1[0] = xn1[0]+(dxvar1[0]* a[idx])
            xvar1[1] = xn1[1]+(dxvar1[1] * a[idx])

            yvar1[0] = yn1[0]+(dyvar1[0]* a[idx])
            yvar1[1] = yn1[1]+(dyvar1[1] * a[idx])

            zvar1[0] = zn1[0]+(dzvar1[0]  * a[idx])
            zvar1[1] = zn1[1]+(dzvar1[1] * a[idx])

            xvar2[0] = xn2[0]+(dxvar2[0]* a[idx])
            xvar2[1] = xn2[1]+(dxvar2[1] * a[idx])

            yvar2[0] = yn2[0]+(dyvar2[0]* a[idx])
            yvar2[1] = yn2[1]+(dyvar2[1] * a[idx])

            zvar2[0] = zn2[0]+(dzvar2[0]  * a[idx])
            zvar2[1] = zn2[1]+(dzvar2[1] * a[idx])
            #print(idx)
            #print("-------->  ku::("+str(kx1))
            #print("-------->  ku::("+str(ky1))
            #print("-------->  ku::("+str(kz1))
            #print("-------->  ku::("+str(kx2))
            #print("-------->  ku::("+str(ky2))
            #print("-------->  ku::("+str(kz2))
            #print(str(idx)+")x ---> "+str(xvar1[0] )+" "+str(dxvar1[0]) )

        x1[i][0]  = xn1[0]  + kx1[0]
        y1[i][0]  = yn1[0]  + ky1[0]
        z1[i][0]  = zn1[0]  + kz1[0]

        x2[i][0]  = xn2[0]  + kx2[0]
        y2[i][0]  = yn2[0]  + ky2[0]
        z2[i][0]  = zn2[0]  + kz2[0]

        x1[i][1]  = xn1[1]  + kx1[1]
        y1[i][1]  = yn1[1]  + ky1[1]
        z1[i][1]  = zn1[1]  + kz1[1]

        x2[i][1]  = xn2[1]  + kx2[1]
        y2[i][1]  = yn2[1]  + ky2[1]
        z2[i][1]  = zn2[1]  + kz2[1]

        print(str(i)+" -----------------------------")
        print("u::"+str(x1[i]))
        print("u::"+str(y1[i]))
        print("u::"+str(z1[i]))
        print("u::"+str(x2[i]))
        print("u::"+str(y2[i]))
        print("u::"+str(z2[i]))
        print("----------------------------------")

        R[i][0]=(m1*x1[i][0]+m2*x2[i][0])/(m1+m2)   # TODO
        R[i][1]=(m1*y1[i][0]+m2*y2[i][0])/(m1+m2)   # TODO
        R[i][2]=(m1*z1[i][0]+m2*z2[i][0])/(m1+m2)   # TODO

        t0 = t0 + h
        t[i] = t0

        r[i][0] = (x2[i][0]-x1[i][0])
        r[i][1] = (y2[i][0]-y1[i][0])
        r[i][2] = (z2[i][0]-z1[i][0]) #

def twobodiesExample0(pnumberofpoints, h, K1, K2, m1, m2):
    global r, t, R
    # Count number of iterations using step size or
    # step height h

    init(pnumberofpoints)
    def deriveVelocityAndAcceleration(x1,y1,z1,m1, m2):
        rr = math.sqrt(x1[0]**2 + y1[0]**2)# + z1[0]**2)
        dx1 = [0,0]
        dy1 = [0,0]
        dz1 = [0,0]

        dx1[0] = x1[1]
        dy1[0] = y1[1]
        dz1[0] = z1[1]

        dx1[1] = (-(1+m2)* x1[0]) / (rr**3)
        dy1[1] = (-(1+m2) * y1[0]) / (rr**3)
        dz1[1] = (-(1+m2) * z1[0]) / (rr**3)
        return dx1,dy1,dz1

    t0 = 0
    rx1 = [1,0]
    ry1 = [0, 1.59687]
    rz1 = [0,0]

    r[0][0] = rx1[0]
    r[0][1] = ry1[0]
    r[0][2] = rz1[0]

    a = [h/2.0, h/2.0, h, 0.0]
    b = [h/6.0, h/3.0, h/3.0, h/6.0]
    for i in range(1,len(t)):
        # Runge-Kutta
        xn1 = [0,0]
        yn1 = [0,0]
        zn1 = [0,0]

        xn1[0] = rx1[0]
        xn1[1] = rx1[1]

        yn1[0] = ry1[0]
        yn1[1] = ry1[1]

        zn1[0] = rz1[0]
        zn1[1] = rz1[1]

        #print("####"+str(i)+" x:"+str(xn1)+" y:"+str(yn1))
        kx1 = [0,0]
        ky1 = [0,0]
        kz1 = [0,0]

        xvar1 = xn1[:]
        yvar1 = yn1[:]
        zvar1 = zn1[:]

        for idx in range(4):

            #print(str(xn1)+" "+str(yn1))
            dx1,dy1,dz1 = deriveVelocityAndAcceleration(xvar1,yvar1,zvar1,m1, m2)

            kx1[0] = kx1[0] + (K2*dx1[0]*b[idx])
            ky1[0] = ky1[0] + (K2*dy1[0]*b[idx])
            kz1[0] = kz1[0] + (K2*dz1[0]*b[idx])

            kx1[1] = kx1[1] + (K1*dx1[1]*b[idx])
            ky1[1] = ky1[1] + (K1*dy1[1]*b[idx])
            kz1[1] = kz1[1] + (K1*dz1[1]*b[idx])

            # next step
            xvar1[0] = xn1[0]+(K2*dx1[0]  * a[idx])
            yvar1[0] = yn1[0]+(K2*dy1[0]  * a[idx])
            zvar1[0] = zn1[0]+(K2*dz1[0]  * a[idx])

            xvar1[1] = xn1[1]+(K1*dx1[1]  * a[idx])
            yvar1[1] = yn1[1]+(K1*dy1[1]  * a[idx])
            zvar1[1] = zn1[1]+(K1*dz1[1]  * a[idx])

            #print("-------->("+str(idx)+")")
            #print("-------->  u ::("+str(xn1))
            #print("-------->  u ::("+str(xvar1[0])+"\t"+ str(yvar1[0])+"\t"+str(xvar1[1])+"\t"+str(yvar1[1]))
            #print("-------->  du::("+str(dx1[0])+"\t"+ str(dy1[0])+"\t"+str(dx1[1])+"\t"+str(dy1[1]))
            #print("-------->  ut::("+str(kx1[0])+"\t"+ str(ky1[0])+"\t"+str(kx1[1])+"\t"+str(ky1[1]))

        #print("->kx:"+str(kx1))
        #print("->ky:"+str(kx1))
        rx1[0]  = xn1[0]  + kx1[0]
        ry1[0]  = yn1[0]  + ky1[0]
        rz1[0]  = zn1[0]  + kz1[0]

        rx1[1]  = xn1[1]  + kx1[1]
        ry1[1]  = yn1[1]  + ky1[1]
        rz1[1]  = zn1[1]  + kz1[1]

        r[i][0] = rx1[0]
        r[i][1] = ry1[0]
        r[i][2] = rz1[0]

        t0 = t0 + h
        t[i] = t0

def writeData(path, typeofdata):
    global ZERO_R, PARAMETER_m1,PARAMETER_m2, t, r, R
    print("## %d values generated"%(len(t)))
    f = open(path+"data.out","w+")

    tstring = "t ="

    f.write("m1 = %s\n"%(PARAMETER_m1))
    f.write("m2 = %s\n"%(PARAMETER_m2))

    if typeofdata == 0:
        x1string = "x1 ="
        y1string = "y1 ="
        z1string = "z1 ="

        x2string = "x2 ="
        y2string = "y2 ="
        z2string = "z2 ="

        for i in range(len(t)):
            tstring =  tstring+" "+str(t[i])
            x1string = x1string+" "+str(x1[i][0])
            y1string = y1string+" "+str(y1[i][0])
            z1string = z1string+" "+str(z1[i][0])
            x2string = x2string+" "+str(x2[i][0])
            y2string = y2string+" "+str(y2[i][0])
            z2string = z2string+" "+str(z2[i][0])
        f.write(tstring+"\n")
        f.write (x1string+"\n")
        f.write (y1string+"\n")
        f.write (z1string+"\n")
        f.write (x2string+"\n")
        f.write (y2string+"\n")
        f.write (z2string+"\n")
    elif typeofdata == 1:
        rxstring = "rx ="
        rystring = "ry ="
        rzstring = "rz ="
        Rxstring = "Rx ="
        Rystring = "Ry ="
        Rzstring = "Rz ="
        for i in range(len(t)):
            tstring =  tstring+" "+str(t[i])
            rxstring = rxstring+" "+str(r[i][0])
            rystring = rystring+" "+str(r[i][1])
            rzstring = rzstring+" "+str(r[i][2])

            if ZERO_R:
                Rxstring = Rxstring+" 0"
                Rystring = Rystring+" 0"
                Rzstring = Rzstring+" 0"
            else:
                Rxstring = Rxstring+" "+str(R[i][0])
                Rystring = Rystring+" "+str(R[i][1])
                Rzstring = Rzstring+" "+str(R[i][2])

        f.write (tstring+"\n")
        f.write (rxstring+"\n")
        f.write (rystring+"\n")
        f.write (rzstring+"\n")

        f.write (Rxstring+"\n")
        f.write (Rystring+"\n")
        f.write (Rzstring+"\n")

    f.close()

def readInputData(path):
    global PARAMETER_m1, PARAMETER_m2, PARAMETER_step, PARAMETER_points

    try:
        f = open(path+"config.ini","r")
        data = f.readlines()
        f.close()
        for line in data:
            if line[0] == "#":
                continue

            idx = line.index(":")
            param = line[0:idx]

            if idx != -1:
                if param == "m1":
                    PARAMETER_m1 = float(line[idx+1:].strip())
                elif param == "m2":
                    PARAMETER_m2 = float(line[idx+1:].strip())
                elif param == "step":
                    PARAMETER_step = float(line[idx+1:].strip())
                elif param == "points":
                    PARAMETER_points = int(line[idx+1:].strip())
    except:
        print("config.ini couldn't be interpreted, using default parameters")

if __name__ == "__main__":
    typer = 1
    typex = 0
    path = ""

    if len(sys.argv) > 1:

        if len(sys.argv) == 3:
            path = sys.argv[2]+"/"

        readInputData(path)
        if sys.argv[1] == "-ex0":
            twobodiesExample0(PARAMETER_points, PARAMETER_step, FACTOR_K1, FACTOR_K2, PARAMETER_m1, PARAMETER_m2)
            writeData(path,typer)
        elif sys.argv[1] == "-ex1":
            twobodiesExample1(PARAMETER_points, PARAMETER_step, FACTOR_K1, FACTOR_K2, PARAMETER_m1, PARAMETER_m2)
            writeData(path,typer)
        elif sys.argv[1] == "-ex2":
            twobodiesExample2(PARAMETER_points, PARAMETER_step, FACTOR_K1, FACTOR_K2, PARAMETER_m1, PARAMETER_m2)
            writeData(path, typex)
    else:
        print("# no action")


"""
flask app handle bitcoin wallet
can add remove addresses, and sync data for them(transactions, balance)
"""

import os

from dotenv import load_dotenv
from flask import Flask, request

from db_methods.init_tables import init_method
from db_methods.queries_methods import add_address_to_db, remove_address_from_db, sync_addresses_in_db

load_dotenv()
app = Flask(__name__)

@app.route('/api/v1/<address>', methods=["POST"])
def add_address(address):
    """
    add bitcoin address to wallet
    @param address: bitcoin address
    @type address: str
    @rtype: str
    """
    if request.method == 'POST':
        add_address_to_db(address)
        return "success"

    return "nothing saved to db"

@app.route('/api/v1/<address>/remove', methods=["POST"])
def remove_address(address):
    """
    remove bitcoin address from wallet
    @param address: bitcoin address
    @type address: str
    @rtype: str
    """
    if request.method == 'POST':
        remove_address_from_db(address)
        return "success"

    return "nothing saved to db"

@app.route('/api/v1/sync', methods=["POST"])
def sync_addresses():
    """
    sync addresses last transaction and balance
    @rtype: str
    """
    if request.method == 'POST':
        sync_addresses_in_db()

    return "success"

if __name__ == "__main__":
    port = int(os.getenv('FLASK_PORT', 5000))
    init_method()
    app.run(debug=True, host="0.0.0.0", port=port)


import random
import networkx as nx
import matplotlib.pyplot as plt
from reachable import reachable_nodes

def cascade(G, A, p):

    nodes = list(G)
    edges = G.edges

    GX = nx.DiGraph()
    GX.add_nodes_from(nodes)

    liveArcs = []
    for edge in edges:
        edgeIsActive = random.random() < p
        if edgeIsActive == True:
            liveArcs.append(edge)

    GX.add_edges_from(liveArcs)

    num_nodes_reached = 0
    for n in A:
        num_nodes_reached = num_nodes_reached + len(reachable_nodes(GX,n))

    return num_nodes_reached, GX


#!/usr/bin/env python3
# statistic.py [file_path]

import sys
from collections import defaultdict

file_path = sys.argv[1]
with open(file_path) as f:
    hist = defaultdict(int)
    sum_ = 0
    line_ = 0
    max_ = 0
    for l in f:
        n = int(l.strip())
        hist[n] += 1
        sum_ += n
        line_ += 1
        if max_ < n:
            max_ = n
    average = sum_ / line_
    print('avarage =', average)
    print('sum = ', sum_)
    print('line = ', line_)
    print('max = ', max_)
    print('hist = ', hist)


#!/usr/bin/python3
# ############################################################################################
# AUTHOR: Michael Gallagher (CIRES/NOAA)
# EMAIL:  michael.r.gallagher@noaa.gov
#
# PURPOSE:
#
# A quick example on pulling in ATMOS flux team data for analysis
#
# ############################################################################################
from   datetime import datetime, timedelta
import os, inspect, argparse

import numpy  as np
import pandas as pd
import xarray as xr
import netCDF4

station = 'asfs30'
site = 'L2'
data_dir = './processed_data/level2/{}/'.format(station) # point this program to your data
head_str = 'level2_preliminary_1min_{}.{}.'.format(station,site)        # plot 10min data
tail_str = '.nc'           # leading data file string

parser = argparse.ArgumentParser()
parser.add_argument('-s', '--start_time', metavar='str',   help='beginning of processing period, Ymd syntax')
parser.add_argument('-e', '--end_time',   metavar='str',   help='end  of processing period, Ymd syntax')

# start at first day of available data if a start isn't provided, end at the end of leg 2
args         = parser.parse_args()
start_time = datetime(2020,2,28,0,0) # the first day of MOSAiC tower data
#start_time = datetime(2019,10,15,0,0) # the first day of MOSAiC tower data

if args.start_time:
    start_time = datetime.strptime(args.start_time, '%Y%m%d')

if args.end_time:
    end_time = datetime.strptime(args.end_time, '%Y%m%d')
else:
    end_time = datetime(2020,3,1,0,0) # end of leg 2

print('---------------------------------------------------------------------------------------')
print('')
print('Looking at data days between {} -----> {}'.format(start_time,end_time))
print('')
print('---------------------------------------------------------------------------------------')

# ###############################################################################################
# pull in the data from every daily file in the time range asked for and concatenate it into
# a single data frame. this means you can look at data across the full range without array syntax
# ###############################################################################################
print(' ... pulling in data from the following files:')
day_series = pd.date_range(start_time, end_time) # we're going to get data for these days between start->end
file_list  = []
for today in day_series: # loop over the days in the processing range and get a list of files
    date_str  = today.strftime('%Y%m%d') # get date string for file name
    curr_file = data_dir+head_str+'{}'.format(date_str)+tail_str # name of file from today
    if os.path.isfile(curr_file):
        file_list.append(curr_file)
    else:
        print(' !!! file {} not found !!!'.format(curr_file))
        continue
#for name in file_list: print('    {}'.format(name)); print('') # uncomment to see every file used

# do it the hard way??
manual_labor_is_my_style = False

if manual_labor_is_my_style:
    # open the entire list of data files from these days as one netcdf dataset
    dataset = netCDF4.MFDataset(files=file_list, aggdim='time')

    # now put that dataset into a dataframe because dataframes are so much more useful, use timestamps as index
    print(' ... making dataframe')
    time_data     = dataset.variables['time']
    time_to_dates = netCDF4.num2date(time_data[:], time_data.units, \
                                     only_use_cftime_datetimes=False,only_use_python_datetimes=True)
    # only_use declarations might only be required/available in the newest netcdf version...??

    df = pd.DataFrame(index=time_to_dates) # put netcdf dataset into a pandas dataframe to be easily plotted

    # loop over all variables
    for var in dataset.variables.keys():
        values = dataset.variables[var][:]
        df[var] = values

        try: fill_val = np.float(values.ncattrs()['fill_value'])
        except: print("no fill value for {}".format(var))

    df['time'] = time_to_dates # duplicates index... but it can be convenient
    df = df.sort_index()       # or (inplace=True)

    # there it is! you can make plots, compare, etc etc etc
    print('\n---------------------------------------------------------------------------------------\n')
    print('netcdf file attributes:')
    for name in dataset.ncattrs():
        print("{} = {}".format(name, getattr(dataset, name)))

else: # or let someone else write the code for me.
    xarr_ds = xr.open_mfdataset(file_list)
    df = xarr_ds.to_dataframe() # pandas dataframes are really useful

print('\n---------------------------------------------------------------------------------------\n')
print('dataframe header and footer:')
print(df.info(verbose=True))
print('\n---------------------------------------------------------------------------------------\n')
print(df['radiation_SWu'])
print(df['radiation_net'])


from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from airflow.contrib.hooks.aws_hook import AwsHook

class StageToRedshiftOperator(BaseOperator):
    ui_color = '#358140'

    template_fields = ("s3_key",)
    copy_sql = """
        COPY {}
        FROM '{}'
        ACCESS_KEY_ID '{}'
        SECRET_ACCESS_KEY '{}'
        REGION '{}'
        {}
        {}
        {}
        {}
        {}
    """

    @apply_defaults
    def __init__(self,
                 redshift_conn_id="",
                 aws_credentials_id="",
                 table="",
                 s3_bucket="",
                 s3_key="",
                 region="us-west-2",
                 data_format="",
                 data_format_args=None,
                 data_format_kwargs=None,
                 file_compression="",
                 data_conversion_args=None,
                 data_conversion_kwargs=None,
                 data_load_args=None,
                 data_load_kwargs=None,
                 del_existing=True,
                 *args, **kwargs):

        super(StageToRedshiftOperator, self).__init__(*args, **kwargs)
        self.redshift_conn_id = redshift_conn_id
        self.aws_credentials_id = aws_credentials_id
        self.table = table
        self.s3_bucket = s3_bucket
        self.s3_key = s3_key
        self.region = region
        self.data_format = data_format
        self.data_format_args = data_format_args or []
        self.data_format_kwargs = data_format_kwargs or {}
        self.file_compression = file_compression
        self.data_conversion_args = data_conversion_args or []
        self.data_conversion_kwargs = data_conversion_kwargs or {}
        self.data_load_args = data_load_args or []
        self.data_load_kwargs = data_load_kwargs or {}
        self.del_existing = del_existing

    def execute(self, context):
        self.log.info(f'StageToRedshiftOperator table: {self.table} {self.s3_bucket}/{self.s3_key}')
        aws_hook = AwsHook(self.aws_credentials_id)
        credentials = aws_hook.get_credentials()
        redshift = PostgresHook(postgres_conn_id=self.redshift_conn_id)

        if self.del_existing:
            self.log.info("Clearing data from destination Redshift table")
            redshift.run("DELETE FROM {}".format(self.table))

        self.log.info("Copying data from S3 to Redshift")
        rendered_key = self.s3_key.format(**context)
        s3_path = "s3://{}/{}".format(self.s3_bucket, rendered_key)
        data_format_options = ''
        data_conversion = ''
        data_load_ops = ''
        if self.data_format_args != None:
            if type(self.data_format_args) is str:
                data_format_options += self.data_format_args
            else:
                data_format_options += ' '.join(self.data_format_args)
            data_format_options += ' '
        if self.data_format_kwargs != None:
            data_format_options += ' '.join([k + ' ' + str(v) for k,v in self.data_format_kwargs.items()])
        if self.data_conversion_args != None:
            if type(self.data_conversion_args) is str:
                data_conversion += self.data_conversion_args
            else:
                data_conversion += ' '.join(self.data_conversion_args)
            data_conversion += ' '
        if self.data_conversion_kwargs != None:
            data_conversion += ' '.join([k + ' ' + str(v) for k,v in self.data_conversion_kwargs.items()])
        if self.data_load_args != None:
            if type(self.data_load_args) is str:
                data_load_ops += self.data_load_args
            else:
                data_load_ops += ' '.join(self.data_load_args)
            data_load_ops += ' '
        if self.data_load_kwargs != None:
            data_load_ops += ' '.join([k + ' ' + str(v) for k,v in self.data_load_kwargs.items()])

        formatted_sql = StageToRedshiftOperator.copy_sql.format(
            self.table,
            s3_path,
            credentials.access_key,
            credentials.secret_key,
            self.region,
            self.data_format,
            data_format_options,
            self.file_compression,
            data_conversion,
            data_load_ops
        )
        redshift.run(formatted_sql)


from flask import Flask
from flask import render_template
from flask import *
import keras
import numpy as np
import cv2
import os
from keras.preprocessing import image

model = keras.models.load_model("model/model.h5")
app = Flask(__name__)

folder = "uploads"

def predict():
    try:
        test_img = image.load_img('uploads/1.jpg',target_size = (150,150))
        test_img = image.img_to_array(test_img)
        test_img = np.expand_dims(test_img,axis = 0)
        # return 100
        ans = model.predict(test_img)
        ans = np.argmax(ans)
        if ans == 0:
            return "Actinic keratoses"

        elif ans == 1:
            return "Basal cell carcinoma"

        elif ans == 2:
            return "Benign keratosis-like lesions "

        elif ans == 3:
            return "Melanocytic nevi"

        elif ans == 4:
            return "Melanoma"

        elif ans == 5:
            return "Dermatofibroma"

        elif ans == 6:
            return "Vascular lesions"

        return "normal"
    except:
        return "Please choose jpg or png file only"
@app.route("/",methods = ["GET","POST"])
def upload_predict():
    if request.method == "POST":
        image_file = request.files["image"]
        if image_file:
            location = os.path.join(
                folder,
                "1.jpg"
            )
            image_file.save(location)
            ans = predict()
            return render_template("index.html",prediction=ans)
    return render_template("index.html",prediction="None")

if __name__ == "__main__":
    app.run(port = 12000,debug = True)


import random
import torch
import os

from dataloaders.datasets import CaptionDataset
from models.model import BiLSTM_withMaxPooling
import argparse
import json

import torchvision.transforms as transforms

parser = argparse.ArgumentParser()
parser.add_argument('--run_local', default=False, action='store_true')
parser.add_argument('--batch_size', default=32, type=int)
parser.add_argument('--neg_type', default='standard', type=str)
parser.add_argument('--runname', default='standard', type=str)
parser.add_argument('--cuda', default=0, type=int)
parser.add_argument('--ex_dup', default=10, type=int)
parser.add_argument('--debug', default=False, action='store_true')
parser.add_argument('--embedding_size', default=512, type=int)
args = parser.parse_args()

device = torch.device("cuda:{}".format(args.cuda) if torch.cuda.is_available() else "cpu")

net = BiLSTM_withMaxPooling(args.embedding_size, args.embedding_size, device)
model_path = '../trained_models/train_standard_nli_batch_size_32/checkpoint_IC_NLI.pth.tar'
stat_dict = torch.load(model_path, map_location=device)
net = stat_dict['net']
epoch = stat_dict['epoch']
epochs_since_improvement = stat_dict['epochs_since_improvement']
optimizer = stat_dict['optimizer']

data_normalization = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

if args.run_local:
    net.device = 'cpu'
net.eval()

data_folder = '/home/gal/Desktop/Pycharm_projects/image_captioning/output_folder'
data_name = 'coco_5_cap_per_img_5_min_word_freq'  # base name shared by data files

if not args.run_local:
    data_f = '/yoav_stg/gshalev/image_captioning/output_folder'
else:
    data_f = data_folder

# section: word map
word_map_file = os.path.join(data_f, 'WORDMAP_' + data_name + '.json')
print('word_map_file: {}'.format(word_map_file))
print('loading word map from path: {}'.format(word_map_file))
with open(word_map_file, 'r') as j:
    word_map = json.load(j)
print('load word map COMPLETED')
rev_word_map = {v: k for k, v in word_map.items()}

val_loader = torch.utils.data.DataLoader(
    CaptionDataset(data_f, data_name, 'VAL', transform=transforms.Compose([data_normalization])),
    batch_size=1, shuffle=True, num_workers=1, pin_memory=True)

val_caps_lst = torch.load(
        '/yoav_stg/gshalev/image_captioning/output_folder/val_caps_lst' if not args.run_local else os.path.join(
            data_folder, 'val_caps_lst'))

for val_batch_i, (imgs, caps, caplens, all_captions) in enumerate(val_loader):

        # subsection: pos example
    pos_output = net((caps, caplens[0][0]), (all_captions[0][1], caplens[0][1]))
    pred = torch.argmax(pos_output).item()
    sen1 = ' '.join([rev_word_map[x] for x in caps[0].cpu().detach().numpy()[:list(caps[0]).index(0)]][1:-1])
    sen2 = ' '.join([rev_word_map[x] for x in all_captions[0][1].cpu().detach().numpy()[:list(all_captions[0][1]).index(0)]][1:-1])
    print('-------------------')
    print('-------------------')
    print('sen1: {}\nsen2: {}\ngt: {}\npred: {}'.format(sen1, sen2, True, pred))
    print('-------------------')

    fake_caps_and_lens_lst = random.sample(list(val_caps_lst), 1)
    fake_caps_lst = [x[0][0] for x in fake_caps_and_lens_lst]
    fake_caps_lens_lst = [x[1][0] for x in fake_caps_and_lens_lst]
    neg_output = net((caps, caplens[0]), (torch.stack(fake_caps_lst), torch.tensor(fake_caps_lens_lst)))
    pred2 = torch.argmax(neg_output).item()

    sen1 = ' '.join([rev_word_map[x] for x in caps[0].cpu().detach().numpy()[:list(caps[0]).index(0)]][1:-1])
    sen2 = ' '.join([rev_word_map[x] for x in torch.stack(fake_caps_lst)[0].cpu().detach().numpy()[:list(torch.stack(fake_caps_lst)[0]).index(0)]][1:-1])
    print('sen1: {}\nsen2: {}\ngt: {}\npred: {}'.format(sen1, sen2, False, pred2))

    if pred == 1:
        g=0
    if pred2 == 0:
        g=0

l = 0


from flask import Flask, jsonify
import sqlite3

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['DEBUG'] = True

def db_connect(query):
    connection = sqlite3.connect('netflix.db')
    cursor = connection.cursor()
    cursor.execute(query)
    result = cursor.fetchall()
    connection.close()
    return result

@app.route('/movie/<title>')
def search_by_title(title):
    query = f"""
        SELECT
            title
            , country
            , release_year
            , listed_in AS genre
            , description
        FROM netflix
        WHERE title = '{title}'
        ORDER BY release_year DESC
        LIMIT 1
    """
    response = db_connect(query)[0]
    response_json = {
        'title': response[0],
        'country': response[1],
        'release_year': response[2],
        'genre': response[3],
        'description': response[4].strip(),
    }
    return jsonify(response_json)

@app.route('/movie/<int:start>/to/<int:end>')
def search_by_period(start, end):
    query = f"""
        SELECT
            title
            , release_year
        FROM netflix
        WHERE release_year BETWEEN {start} AND {end}
        ORDER BY release_year
        LIMIT 100
    """
    response = db_connect(query)
    response_json = []
    for film in response:
        response_json.append({
            'title': film[0],
            'release_year': film[1],
        })
    return jsonify(response_json)

@app.route('/rating/<group>')
def search_by_rating(group):
    levels = {
        'children': ['G'],
        'family': ['G', 'GB', 'PG-13'],
        'adult': ['R', 'NC-17'],
    }

    if group in levels:
        level = '\", \"'.join(levels[group])
        level = f'\"{level}\"'
    else:
        return jsonify([])

    query = f"""
            SELECT
                title
                , rating
                , description
            FROM netflix
            WHERE rating IN ({level})
        """
    print(query)

    response = db_connect(query)
    response_json = []
    for film in response:
        response_json.append({
            'title': film[0],
            'rating': film[1],
            'description': film[2].strip(),
        })
    return jsonify(response_json)

@app.route('/genre/<genre>')
def search_by_genre(genre):
    query = f"""
            SELECT
                title
                , description
            FROM netflix
            WHERE listed_in LIKE '%{genre}%'
            ORDER BY release_year DESC
            LIMIT 10
        """

    response = db_connect(query)
    response_json = []
    for film in response:
        response_json.append({
            'title': film[0],
            'description': film[1],
        })
    return jsonify(response_json)

app.run()


# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.tools.translate import _
from openerp.exceptions import except_orm, ValidationError
import time, datetime
import openerp.addons.decimal_precision as dp
from docutils.nodes import line

class wiz_intervention_quotation(models.TransientModel):
    """
        Wiz intervention quotation
    """
    _name = 'wiz.intervention.quotation'
    _description = 'Wiz intervention quotation'

    @api.model
    def default_get(self, fields_list):
        res = super(wiz_intervention_quotation, self).default_get(fields_list=fields_list)
        res['intervention_id'] = self._context.get('active_id')
        return res

    @api.one
    @api.depends('park_id')
    def _all_operation_compute(self):
        """
            All resource pour le domaine
        """
        self.all_operation_ids = [x.maintenance_operation_id.id for x in self.park_id.maintenance_operation_ids]

    #===========================================================================
    # COLUMNS
    #===========================================================================
    intervention_id = fields.Many2one('intervention', string='Intervention', required=False, ondelete='cascade')
    customer_id = fields.Many2one('res.partner', string='Customer', required=False, ondelete='cascade')
    line_ids = fields.One2many('wiz.intervention.quotation.line', 'intervention_quotation_id',  string='Lines')
    date = fields.Date(string='Date', default=lambda self: fields.Date.today(), required=False)
    no_order_line_ids = fields.One2many('wiz.intervention.quotation.noorder', 'intervention_quotation_id',  string='Lines')

    #===========================================================================
    # Button
    #===========================================================================
    @api.multi
    def action_validation(self):
        sol_obj = self.env['sale.order.line']
        so_obj = self.env['sale.order']
        product_obj = self.env['product.product']
        for wiz in self:
            if not wiz.line_ids:
                raise except_orm(_('Error'), _('There are not lines.'))

            if wiz.intervention_id.sale_id:
                sale_rcs = wiz.intervention_id.sale_id
                if sale_rcs.state != 'draft':
                    raise except_orm(_('Error'), _('The sale has to be in the state draft.'))

            else:
                sale_rcs = so_obj.create_sale(customer=wiz.customer_id.id, so_line=None, date=wiz.date, other_data={'requested_date': wiz.date, 'type': 'sav'}, forced_qty=False)
#                                                                                'sale_account_invoice_trigger': 'manual',
#                                                                                'sale_account_invoiced_on': 'order'
                wiz.intervention_id.write({'sale_id': sale_rcs.id})

            currency_rcs = sale_rcs.currency_id
#             sale_rcs.write({'sale_account_invoice_trigger': 'manual', 'sale_account_invoiced_on': 'order'})
            for line in wiz.line_ids:
                if currency_rcs.id != line.currency_id.id:
                    price = product_obj._calcul_price_rate_devise(currency_rcs, line.price, line.currency_id)
                else:
                    price = line.price

                values = {'name': line.description,
                          'uom_id': line.uom_id.id,
                          'sec_uom_id': line.uom_id.id,
                          'uoi_id': line.uom_id.id,
                          'uom_qty': line.qty,
                          'sec_uom_qty': line.qty,
                          'uoi_qty': line.qty,
                          'requested_date': wiz.date,
                          'intervention_id': wiz.intervention_id.id,
                        }
                if (line.pmi_id and not line.pmi_id.sale_line_id or not line.pmi_id) and (line.qi_id and not line.qi_id.sale_line_id or not line.qi_id):
                    sol_rc = sol_obj.create_sale_order_line(sale=sale_rcs, product=line.product_id, values=values, forced_qty=True, forced_price_unit=price)
                    if line.pmi_id:
                        line.pmi_id.write({'sale_line_id': sol_rc.id})

                    if line.qi_id:
                        line.qi_id.write({'sale_line_id': sol_rc.id})

                elif line.pmi_id.sale_line_id:
                    values['price_unit'] = price
                    line.pmi_id.sale_line_id.write(values)
                elif line.qi_id.sale_line_id:
                    values['price_unit'] = price
                    line.pmi_id.sale_line_id.write(values)

            wiz.intervention_id.write({'is_create_quotation': True})
            return {'name': _('Sale order'),
                    'view_type': 'form',
                    'view_mode': 'form',
                    'res_model': 'sale.order',
                    'type': 'ir.actions.act_window',
                    'target': 'current',
                    'res_id': sale_rcs.id,
                    'nodestroy': True,
                    }

        return {'type': 'ir.actions.act_window_close'}

class wiz_intervention_quotation_line(models.TransientModel):
    """
        Wiz intervention quotation line
    """
    _name = 'wiz.intervention.quotation.line'
    _description = 'Wiz intervention quotation line'
    _rec_name = 'product_id'

    @api.one
    @api.depends('product_id')
    def _uom_category_compute(self):
        """
            Category UoM
        """
        uom_category_id = False
        if self.product_id:
            uom_category_id = self.uom_id.category_id.id

        self.uom_category_id = uom_category_id

    #===========================================================================
    # COLUMNS
    #===========================================================================
    intervention_quotation_id = fields.Many2one('wiz.intervention.quotation', string='Intervention quotation', required=False, ondelete='cascade')
    description = fields.Text(string='Description')
    product_id = fields.Many2one('product.product', string='Product', required=False, ondelete='cascade')
    qty = fields.Float(string='Qty', default=0.0, required=True, digits=dp.get_precision('Product quantity'))
    price = fields.Float(string='Invoice price', default=0.0, required=True, digits=dp.get_precision('Product price'))
    uom_id = fields.Many2one('product.uom', string='UoM', required=True, ondelete='cascade')
    currency_id = fields.Many2one('res.currency', string='Currency', required=True, ondelete='cascade', default=lambda self: self.env.user.company_id.currency_id)
    uom_category_id = fields.Many2one('product.uom.category', compute='_uom_category_compute', string="UOM category", readonly=True)
    pmi_id = fields.Many2one('piece.maintenance.intervention', string='PMI', required=False, ondelete='cascade')
    qi_id = fields.Many2one('quotation.intervention', string='QI', required=False, ondelete='cascade')

    #===========================================================================
    # Onchange
    #===========================================================================
    @api.onchange('product_id')
    def _onchange_product_id(self):
        """
            Onchange du produit
        """
        uom_id = False
        category_id = False
        if self.product_id:
            uom_id = self.product_id.uom_id.id
            category_id = self.product_id.uom_id.category_id.id

        self.uom_id = uom_id
        self.uom_category_id = category_id

class wiz_intervention_quotation_noorder(models.TransientModel):
    """
        Wiz intervention quotation noorder
    """
    _name = 'wiz.intervention.quotation.noorder'
    _description = 'Wiz intervention quotation noorder'
    _rec_name = 'product_id'

    #===========================================================================
    # COLUMNS
    #===========================================================================
    intervention_quotation_id = fields.Many2one('wiz.intervention.quotation', string='Intervention quotation', required=False, ondelete='cascade')
    description = fields.Text(string='Description')
    product_id = fields.Many2one('product.product', string='Product', required=False, ondelete='cascade')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  5 08:59:37 2022
## Examples:

    print(convert(value='10:08:06:12',framerate=25,start='10:00:00:00').timecode_to_frames())

    print(convert(value=600,framerate=25,start=500).frames_to_timecode())

    print(convert(value='00:08:06:12',framerate=25,start='00:00:00:00')())

based on the work found in https://stackoverflow.com/a/34607115

#### TODO:
    - consider usiong the meemoo logger

@author: tina
"""
import argparse
import logging
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
logger = logging.getLogger(__name__)  # root logger
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='exporter.log',
                    filemode='a')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('__main__').addHandler(console)

def _timecode(seconds, framerate):
    return '{h:02d}:{m:02d}:{s:02d}:{f:02d}' \
            .format(h=int(seconds/3600),
                    m=int(seconds/60%60),
                    s=int(seconds%60),
                    f=round((seconds-int(seconds))* framerate))

def _frames(seconds, framerate):
    return seconds * framerate

def _seconds(value,framerate):
    if isinstance(value, str):
        _zip_ft = zip((3600, 60, 1, 1/framerate), value.split(':'))
        return sum(f * float(t) for f,t in _zip_ft)
    elif isinstance(value, (int, float)):  # frames
        return value / framerate
    else:
        return 0

class convert(object):
    def __init__(self, value, framerate,start=0):
        self.framerate = framerate
        self.value = value
        self.start = start

    def __call__(self):
        if isinstance(self.value, str):
            timecode = self.value
            return _frames(_seconds(timecode,
                                    self.framerate) - _seconds(self.start,
                                                               self.framerate),
                                                               self.framerate)
        elif isinstance(self.value, (int, float)):
            frames = self.value
            return _timecode(_seconds(frames,
                                      self.framerate) + _seconds(self.start,
                                                                 self.framerate),
                                                                 self.framerate)
    def timecode_to_frames(self,start=None):
        timecode = self.value
        return _frames(_seconds(timecode,
                                self.framerate) - _seconds(self.start,
                                                           self.framerate),
                                                           self.framerate)
    def frames_to_timecode(self,start=None):
        frames = self.value
        return _timecode(_seconds(frames,
                                  self.framerate) + _seconds(self.start,
                                                             self.framerate),
                                                             self.framerate)

def main():
    """## Description:

        - convert a timecode string to frames or frames to timecode

    Args:
         value: string or int

        start: ofset start time eg 10:00:00:00

        framerate: nr of frames / second

    """
    parser = argparse.ArgumentParser(description="export mediahaven partial from browse")
    parser.add_argument(
      "-v",
      "--value",
      help="10:00:00:00 or int 600",
      required=True,
      )
    parser.add_argument(
      "-s",
      "--start",
      help="start frame",
      required=True,
      )
    parser.add_argument(
      "-r",
      "--framerate",
      help="nr of frames / seconds",
      required=True,
      )
    args = parser.parse_args()
    logger.info(str(args))
    o = convert(value=args.value,framerate=int(args.framerate),start=args.start)()
    o = int(round(o))
    print(str(o))
    logger.info(str(o))
    return o

if __name__ == '__main__':
    main()


import re
from datetime import datetime, time

from city_scrapers_core.constants import COMMISSION
from city_scrapers_core.items import Meeting
from city_scrapers_core.spiders import CityScrapersSpider

class CookLocalRecordsSpider(CityScrapersSpider):
    name = "cook_local_records"
    agency = "Cook County Local Records Commission"
    timezone = "America/Chicago"
    start_urls = [
        "https://cyberdriveillinois.com/departments/archives/records_management/lrc_cook_county_meeting_schedule.html"  # noqa
    ]
    location = {
        "name": "James R Thompson Center",
        "address": "100 W Randolph St, Room 9-035 Chicago, IL 60601",
    }

    def parse(self, response):
        """
        `parse` should always `yield` Meeting items.

        Change the `_parse_title`, `_parse_start`, etc methods to fit your scraping
        needs.
        """
        description = response.css(".content > p::text").extract_first()
        if not re.search(r"100 W(est)? Randolph", description):
            raise ValueError("Meeting location has changed")
        for item in response.css(".content > ul li"):
            meeting = Meeting(
                title="Local Records Commission",
                description="",
                classification=COMMISSION,
                start=self._parse_start(item),
                end=None,
                all_day=False,
                time_notes="",
                location=self.location,
                links=self._parse_links(item, response),
                source=response.url,
            )

            meeting["status"] = self._get_status(
                meeting, text=" ".join(item.css("*::text").extract())
            )
            meeting["id"] = self._get_id(meeting)

            yield meeting

    def _parse_start(self, item):
        """Parse start datetime as a naive datetime object."""
        day_str = item.xpath("./text()").extract_first()
        date_str = re.search(r"\w+\s+\d{1,2},\s+\d{4}", day_str).group()
        date_obj = datetime.strptime(date_str, "%B %d, %Y")
        return datetime.combine(date_obj.date(), time(11))

    def _parse_links(self, item, response):
        """Parse or generate links."""
        links = []
        for link in item.css("a"):
            links.append(
                {
                    "title": link.xpath("./text()").extract_first().strip(),
                    "href": response.urljoin(link.xpath("@href").extract_first()),
                }
            )
        return links


import argparse
import pandas as pd
import itertools
import os

from ast import literal_eval

from helper_functions import comorbidity_indicator

# Computes basic stats for a data segment; takes in a Pandas Dataframe with
# appropriate fields (sum_costs and num_comorbidities).
def segment_stats(data):
  row_dict = {}
  row_dict['avg_cost'] = data.sum_costs.mean()
  row_dict['std_cost'] = data.sum_costs.std()
  row_dict['num_person_years'] = data.shape[0]
  row_dict['avg_num_com'] = data.no_comorbidities.mean()
  row_dict['std_num_com'] = data.no_comorbidities.std()

  return row_dict

def main(args):
  df_merged = pd.read_csv(args.input_file)
  df_merged.classes = df_merged.classes.apply(lambda x: literal_eval(x))
  df_merged.agg_indices = \
    df_merged.agg_indices.fillna('None').apply(lambda x: literal_eval(x))

  df_merged.sex.fillna('Not provided')
  pop_N = df_merged.shape[0] # grab total number of person-years

  # IMPORT DESCRIPTION INDEX
  desc_dict = pd.read_excel(args.disease_dict).long.to_dict()

  # We will be grouping by pairs of comorbid diseases.
  disease_dyads = [i for i in itertools.combinations(range(len(desc_dict)), 2)]
  # Ancillary function to convert pairs of disease indices to pairs of
  # disease descriptions.
  def map_dyad(d_dyad):
    return [desc_dict[d_dyad[0]], desc_dict[d_dyad[1]]]

  bins = [0,18,35,50,65,150]
  age_labels = ['0-18','18-35','35-50','50-65','65+']
  df_merged['age_bin'] = pd.cut(df_merged.age, bins=bins, labels=age_labels)

  sexes = ['F','M']

  # convert comorbidity list to boolean columns
  ## comorbidity_indicator()
  for i in range(len(desc_dict)):
    df_merged[desc_dict[i]] = comorbidity_indicator(df_merged,[str(i)])

  # disease stats
  diseases_df = pd.DataFrame()
  for i in range(len(desc_dict)):
    temp = df_merged[df_merged[desc_dict[i]]]
    diseases_df = diseases_df.append(pd.Series(segment_stats(temp), name=i))
  diseases_df['frequency'] = diseases_df.num_person_years / pop_N
#  diseases_df.to_csv(os.path.join(args.output_path, 'diseases_all_pop.csv'))

  # comorbid disease dyad stats
  d_dyad_df = pd.DataFrame()

  for i in disease_dyads:
    d_dyad = map_dyad(i)
    d_dyad_segment = df_merged[df_merged[d_dyad[0]] & df_merged[d_dyad[1]]]
    d_dyad_df = d_dyad_df.append(
      pd.Series(segment_stats(d_dyad_segment), name=i)
    )

  expected_df = pd.DataFrame()

  for i in d_dyad_df.index.tolist():
    expected_df = expected_df.append(pd.Series(
      {'expected': (diseases_df.iloc[i[0]].frequency *\
                    diseases_df.iloc[i[1]].frequency) },
      name = i
    ))

  d_dyad_df = pd.concat([d_dyad_df, expected_df], axis = 1)
  d_dyad_df['observed'] = d_dyad_df.num_person_years / pop_N
  d_dyad_df['O/E'] = d_dyad_df.observed / d_dyad_df.expected
  d_dyad_df['diseases'] = d_dyad_df.index.map(map_dyad)
  d_dyad_df['total_cost'] = d_dyad_df.avg_cost * d_dyad_df.num_person_years

  # Filter by minimum number of person-years
  d_dyad_df = d_dyad_df[d_dyad_df.num_person_years >= args.py_filter]

  d_dyad_df.sort_values(by=args.sort_type, ascending=False).to_csv(
    os.path.join(args.output_path, 'disease_dyads_all_pop.csv'),
    index=False, float_format='%.2f'
  )

  if not (args.age_bucket or args.gender_bucket): return
  # diseases + population segments
  buckets = [list(map(lambda x: map_dyad(x), disease_dyads))]
  if args.age_bucket:
    buckets.append(age_labels)
  if args.gender_bucket:
    buckets.append(sexes)
  segments = list(itertools.product(*buckets))

  segments_df = pd.DataFrame()

  for idx, segment in enumerate(segments):
    segment_df = df_merged[df_merged[segment[0][0]] & df_merged[segment[0][1]]]
    if args.age_bucket:
      segment_df = segment_df[segment_df.age_bin == segment[1]]
    if args.gender_bucket:
      segment_df = segment_df[segment_df.sex == segment[-1]]
    stats = segment_stats(segment_df)
    stats['segment'] = segment
    segments_df = segments_df.append(pd.Series(stats, name=idx))

  # Filter by minimum number of person-years
  segments_df = segments_df[segments_df.num_person_years >= args.py_filter]
  segments_df['total_cost'] = segments_df.avg_cost * segments_df.num_person_years

  segments_df.sort_values(by=args.sort_type, ascending=False).to_csv(
    os.path.join(args.output_path, 'disease_dyads_segmented.csv'),
    index=False, float_format='%.2f'
  )

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--disease_dict', default='data/disease_dictionary.xls',
                      help='Dictionary of diseases/conditions to segment by')
  parser.add_argument('--py_filter', default=1, type=int,
                      help='Minimum number of person-years per population ' +
                      'segment to include in results')
  parser.add_argument('--gender_bucket', action='store_true',
                      help='Bucket by gender')
  parser.add_argument('--age_bucket', action='store_true',
                      help='Bucket by age group')
  parser.add_argument('--sort_type', default='total_cost',
                      choices=['total_cost', 'avg_cost', 'num_person_years'],
                      help='Sort results by: total_cost, avg_cost, '
                      'or num_person_years]')
  parser.add_argument('--input_file', type=str, required=True,
                      help='Location of input CSV file containing claims by ' +
                      'person-year')
  parser.add_argument('--output_path', default='output',
                      help='Directory to put output files in')

  args = parser.parse_args()

  if not os.path.exists(args.output_path):
    os.mkdir(args.output_path)

  main(args)


#!/usr/bin/env python3

""" A script to convert a video downloaded with yt-dlp to an audio format"""

import argparse
import os
import subprocess
import sys
from shutil import which

parser = argparse.ArgumentParser(description="Convert audio files to mp3")
parser.add_argument("audio_file", help="audio file to convert")
args = parser.parse_args()

# Checking if ffmpeg is installed
if not which("ffmpeg"):
    print("ffmpeg is not installed")
    sys.exit(1)

formats = [".m4a", ".webm", ".opus", ".mkv", ".ogg", ".wav", ".flac"]
# auto-detect file extension
# formats = []
# for format in subprocess.run(["ffmpeg", "-formats"], capture_output=True).stdout.decode().splitlines():
#     if format.startswith(" D "):
#         formats.append(format.split()[1])

file_path = os.path.dirname(args.audio_file)

def mp3_convert(audio_file):
    """Convert file to mp3. If file is already mp3, do nothing"""

    if not os.path.isfile(audio_file):
        print("::: File not specified.")
        sys.exit(1)

    if audio_file.endswith(".mp3"):
        print("::: File is already mp3. Exiting...")
        sys.exit(0)
    else:
        file_name = os.path.splitext(audio_file)[0]
        file_ext = os.path.splitext(audio_file)[1]
        if file_ext in formats:
            new_file = file_name + ".mp3"
            subprocess.run(
                [
                    "ffmpeg",
                    "-i",
                    audio_file,
                    "-c:a",
                    "libmp3lame",
                    "-b:a",
                    "320k",
                    new_file,
                ],
                check=True,
            )
            os.remove(audio_file)
        else:
            print("\nFile doesn't match any of the supported formats:")
            # print formats as bullet points
            for f in formats:
                print(f"* {f}")
            sys.exit(1)

if __name__ == "__main__":
    mp3_convert(args.audio_file)


import keras
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.convolutional import Deconvolution2D
from keras.layers.normalization import BatchNormalization

from keras.layers.convolutional import UpSampling2D
from keras.layers import Input, Concatenate, Conv2D
from keras.models import Model, model_from_json
from keras.optimizers import *
import numpy as np

class DispNet:
    def __init__(self):
        super().__init__()
        self.model = None

    # Initialize the model
    def init_model(self):
        Input_2 = Input(shape=(128, 256, 2), name='Input_2')  # 6,768,384

        Convolution2D_1 = Conv2D(name='Convolution2D_1', nb_col=7, nb_filter=64, border_mode='same',
                                 activation='relu', nb_row=7)(Input_2)

        MaxPooling2D_1 = MaxPooling2D(name='MaxPooling2D_1')(Convolution2D_1)
        Convolution2D_3 = Convolution2D(name='Convolution2D_3', nb_col=5, nb_filter=128, border_mode='same',
                                        activation='relu', nb_row=5)(MaxPooling2D_1)
        MaxPooling2D_3 = MaxPooling2D(name='MaxPooling2D_3')(Convolution2D_3)
        Convolution2D_17 = Convolution2D(name='Convolution2D_17', nb_col=5, nb_filter=256, border_mode='same',
                                         activation='relu', nb_row=5)(MaxPooling2D_3)
        MaxPooling2D_8 = MaxPooling2D(name='MaxPooling2D_8')(Convolution2D_17)
        Convolution2D_4 = Convolution2D(name='Convolution2D_4', nb_col=3, nb_filter=256, border_mode='same',
                                        activation='relu', nb_row=3)(MaxPooling2D_8)
        Convolution2D_9 = Convolution2D(name='Convolution2D_9', nb_col=3, nb_filter=512, border_mode='same',
                                        activation='relu', nb_row=3)(Convolution2D_4)
        MaxPooling2D_5 = MaxPooling2D(name='MaxPooling2D_5')(Convolution2D_9)
        Convolution2D_10 = Convolution2D(name='Convolution2D_10', nb_col=3, nb_filter=512, border_mode='same',
                                         activation='relu', nb_row=3)(MaxPooling2D_5)
        Convolution2D_11 = Convolution2D(name='Convolution2D_11', nb_col=3, nb_filter=512, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_10)
        MaxPooling2D_6 = MaxPooling2D(name='MaxPooling2D_6')(Convolution2D_11)
        Convolution2D_12 = Convolution2D(name='Convolution2D_12', nb_col=3, nb_filter=512, border_mode='same',
                                         activation='relu', nb_row=3)(MaxPooling2D_6)
        Convolution2D_13 = Convolution2D(name='Convolution2D_13', nb_col=3, nb_filter=1024, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_12)
        MaxPooling2D_7 = MaxPooling2D(name='MaxPooling2D_7')(Convolution2D_13)
        Convolution2D_14 = Convolution2D(name='Convolution2D_14', nb_col=3, nb_filter=1024, border_mode='same',
                                         activation='relu', nb_row=3)(MaxPooling2D_7)
        Convolution2D_18 = Convolution2D(name='Convolution2D_18', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_14)
        UpSampling2D_1 = UpSampling2D(name='UpSampling2D_1')(Convolution2D_18)
        Deconvolution2D_4 = Deconvolution2D(name='Deconvolution2D_4', nb_col=4, border_mode='same', strides=(2, 2),
                                            nb_filter=512, activation='relu', nb_row=4)(
            Convolution2D_14)  # output_shape=(None, 512, 24, 12),
        BatchNormalization_1 = BatchNormalization(name='BatchNormalization_1')(Deconvolution2D_4)
        merge_2 = Concatenate(axis=3)([Convolution2D_12, BatchNormalization_1, UpSampling2D_1, ])
        Convolution2D_19 = Convolution2D(name='Convolution2D_19', nb_col=3, nb_filter=512, border_mode='same',
                                         activation='relu', nb_row=3)(merge_2)

        Convolution2D_20 = Convolution2D(name='Convolution2D_20', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_19)
        UpSampling2D_2 = UpSampling2D(name='UpSampling2D_2')(Convolution2D_20)
        Deconvolution2D_5 = Deconvolution2D(name='Deconvolution2D_5', nb_col=4, nb_row=4, strides=(2, 2),
                                            padding='same',
                                            activation='relu', nb_filter=256)(Convolution2D_19)
        BatchNormalization_2 = BatchNormalization(name='BatchNormalization_2')(Deconvolution2D_5)
        merge_3 = Concatenate(axis=3)([Convolution2D_10, BatchNormalization_2, UpSampling2D_2])
        Convolution2D_21 = Convolution2D(name='Convolution2D_21', nb_col=3, nb_filter=256, border_mode='same',
                                         activation='relu', nb_row=3)(merge_3)
        Deconvolution2D_24 = Deconvolution2D(name='Deconvolution2D_24', nb_col=4, border_mode='same',
                                             strides=(2, 2), nb_filter=128, activation='relu', nb_row=4)(
            Convolution2D_21)  # output_shape=(None, 128, 96, 48)
        BatchNormalization_3 = BatchNormalization(name='BatchNormalization_3')(Deconvolution2D_24)
        Convolution2D_22 = Convolution2D(name='Convolution2D_22', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_21)
        UpSampling2D_3 = UpSampling2D(name='UpSampling2D_3')(Convolution2D_22)
        merge_4 = Concatenate(axis=3)([Convolution2D_4, BatchNormalization_3, UpSampling2D_3, ])
        Convolution2D_23 = Convolution2D(name='Convolution2D_23', nb_col=3, nb_filter=128, border_mode='same',
                                         activation='relu', nb_row=3)(merge_4)
        Convolution2D_24 = Convolution2D(name='Convolution2D_24', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_23)
        UpSampling2D_4 = UpSampling2D(name='UpSampling2D_4')(Convolution2D_24)
        Deconvolution2D_7 = Deconvolution2D(name='Deconvolution2D_7', nb_col=4, border_mode='same',
                                            strides=(2, 2), nb_filter=64, activation='relu', nb_row=4)(
            Convolution2D_23)  # output_shape=(None, 64, 192, 96)
        BatchNormalization_4 = BatchNormalization(name='BatchNormalization_4')(Deconvolution2D_7)
        merge_5 = Concatenate(axis=3)([MaxPooling2D_3, BatchNormalization_4, UpSampling2D_4])
        Convolution2D_25 = Convolution2D(name='Convolution2D_25', nb_col=3, nb_filter=64, border_mode='same',
                                         activation='relu', nb_row=3)(merge_5)
        Convolution2D_26 = Convolution2D(name='Convolution2D_26', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_25)
        UpSampling2D_5 = UpSampling2D(name='UpSampling2D_5')(Convolution2D_26)
        Deconvolution2D_8 = Deconvolution2D(name='Deconvolution2D_8', nb_col=4, nb_row=4, strides=(2, 2),
                                            padding='same',
                                            activation='relu', nb_filter=32)(
            Convolution2D_25)  # output_shape=(None, 64, 384, 192)
        BatchNormalization_5 = BatchNormalization(name='BatchNormalization_5')(Deconvolution2D_8)
        merge_6 = Concatenate(axis=3)([MaxPooling2D_1, BatchNormalization_5, UpSampling2D_5])
        Convolution2D_27 = Convolution2D(name='Convolution2D_27', nb_col=3, nb_filter=32, border_mode='same',
                                         activation='relu', nb_row=3)(merge_6)
        Convolution2D_28 = Convolution2D(name='Convolution2D_28', nb_col=3, nb_filter=1, border_mode='same',
                                         activation='relu', nb_row=3)(Convolution2D_27)

        model = Model([Input_2], [Convolution2D_28])

        print(model.summary())
        self.model = model
        return model

    # Return the model optimizer
    def get_optimizer(self):
        return Adam()

    # Return the model Loss function
    def get_loss_function(self):
        return 'mean_squared_error'

    # Return the Batch size
    def get_batch_size(self):
        return 32

    # Return the default number of epochs
    def get_num_epoch(self):
        return 100

    # Load model and weights from disk
    def load_model_and_weight(self, model_name):
        # load model
        json_file = open(model_name + '.json', 'r')
        model = json_file.read()
        json_file.close()
        model = model_from_json(model)
        # load weights into model
        model.load_weights(model_name + ".h5")
        print("Loaded model from disk")
        self.model = model

    # Save model and weights into model directory
    def save_model_and_weight(self, model_name):
        # serialize model to JSON
        model_json = self.model.to_json()
        with open(model_name + '.json', "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights(model_name + '.h5')
        print("Saved model to disk")

    # Compile the model
    def compile(self):
        self.model.compile(optimizer=self.get_optimizer(), loss=self.get_loss_function(), metrics=['accuracy'])
        return self.model

    # Train the model
    def train(self, x, y, x_vl, y_vl, n_epoch=20, batch_size=1):
        self.model.fit([x], y, epochs=n_epoch, batch_size=batch_size, verbose=1,validation_split = 0.2)

    # Check the error rate on its input test data (x_test & y_test) and print the result in consule
    def get_error_rate(self, x_ts, y_ts):
        p = self.model.predict(x_ts, batch_size=self.get_batch_size(), verbose=0)
        mse = np.mean(np.square(y_ts - p))
        print("Error rate is " + str(mse))
        return mse


import json
import requests
import query as ep

class server(ep):

  global endpoint_url
  endpoint_url = f"https://api.spotify.com/v1/users/{ep.user_id}/playlists"

  def request_generator(self,name = "unnamed", desc = "unnamed", is_public = False):

    request_body = json.dumps({
      "name": name,
      "description": desc,
      "public": is_public
      })

    return request_body

  def playlist_id_generator(self, request_body):

    response = requests.post(url = server.endpoint_url, data = request_body, headers={
      "Content-Type":"application/json",
      "Authorization":f"Bearer {ep.token}"})

    if response.status_code != 201:
      return "HTTP Error"

    url = response.json()['external_urls']['spotify']

    return (response.json()['id'], url)

  def playlist_run(self):
    req  = self.request_generator("demop_playlist","playlist created with python",False)
    playlist_id, playlist_url = self.playlist_id_generator(req)
    playist_endpoint_url = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"

    request_body = json.dumps({
          "uris" : ep.uris
        })
    response = requests.post(url = endpoint_url, data = request_body, headers={"Content-Type":"application/json",
                        "Authorization":f"Bearer {ep.token}"})

    if response.status_code != 201:
          print("HTTP Error")
    return playlist_url


from __future__ import unicode_literals
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin
from django.core.validators import (
    RegexValidator,
    )
from django.db import models
from django.urls import reverse
from django.conf import settings
from .managers import UserManager
from django.utils.translation import ugettext_lazy as _
from django_resized import ResizedImageField

NAME_REGEX = '^[a-zA-Z ]*$'

GENDER_CHOICE = (
    ("Male", "Male"),
    ("Female", "Female"),
    ("Prefer not to say", "Prefer not to say"),
    )

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True)
    first_name = models.CharField(
        max_length=256,
        blank=False,
        validators=[
                RegexValidator(
                    regex=NAME_REGEX,
                    message='Name must be Alphabetic',
                    code='invalid_first_name'
                    )
                ]
        )
    last_name = models.CharField(
        max_length=256,
        blank=False,
        validators=[
                RegexValidator(
                    regex=NAME_REGEX,
                    message='Name must be Alphabetic',
                    code='invalid_last_name'
                    )
                ]
        )

    gender = models.CharField(max_length=25, choices=GENDER_CHOICE,help_text="Select gender")
    date_joined = models.DateTimeField(auto_now_add=True)
    balance = models.DecimalField(
        default=0,
        max_digits=12,
        decimal_places=2
        )

    bonus = models.DecimalField(
        default=0,
        max_digits=12,
        decimal_places=2
        )

    picture = ResizedImageField(
        size=[128, 128],
        crop=['top','left'],
        quality=80,
        upload_to="media-root/",
        null=True,
        blank=False,
        )

    is_staff = models.BooleanField(_("is staff"), default=False)
    is_active = models.IntegerField(default = 1,
                                   blank = True,
                                    null = True,
                                    help_text ='1->Active, 0->Inactive',
                                    choices =(
                                    (1, 'Active'), (0, 'Inactive')
                                    ))
    email_confirmed = models.BooleanField(default=False)

    objects = UserManager()

    # USERNAME_FIELD = 'email'  # use email to log in
    USERNAME_FIELD = 'email'  # use email to log in
    REQUIRED_FIELDS = []  # required when user is created

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        fullname = '%s %s' %(self.first_name, self.last_name)
        return fullname.strip()

    def get_short_name(self):
        return self.first_name

    def Save(self, *args, **kwargs):
        super(User, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.last_name+' '+self.first_name)