import argparse
import collections
import cv2
import numpy as np
import os
import pdb
import random
import time

import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from torch.cuda.amp import GradScaler
torch.backends.cudnn.benchmark=True
autocast = torch.cuda.amp.autocast

from utils.flowlib import flow_to_image
from utils.io import add_image
from models import *
from utils.multiscaleloss import realEPE

parser = argparse.ArgumentParser(description='VCNPlus')
parser.add_argument('--maxdisp', type=int ,default=256,
                    help='maxium disparity, out of range pixels will be masked out. Only affect the coarsest cost volume size (default 256)')
parser.add_argument('--fac', type=float ,default=1,
                    help='controls the shape of search grid. Only affect the coarsest cost volume size (default 1)')
parser.add_argument('--logname', default='exp-1',
                    help='name of the log file (default exp-1)')
parser.add_argument('--database',
                    help='path to the database (required)')
parser.add_argument('--loadmodel', default=None,
                    help='path of the pre-trained model (default None)')
parser.add_argument('--loadflow', default=None,
                    help='path of the pre-trained flow model (default None)')
parser.add_argument('--savemodel',
                    help='path to save the model (required)')
parser.add_argument('--retrain', default='true',
                    help='whether to reset moving mean / other hyperparameters (default true)')
parser.add_argument('--stage', default='expansion',
                    help='one of {chairs, things, 2015train, 2015trainval, sinteltrain, sinteltrainval, expansion, expansion2015train, expansion2015tv} (deafult expansion)')
parser.add_argument('--nproc', type=int, default=1,
                    help='number of process to use (default 1)')
parser.add_argument('--ngpus', type=int, default=1,
                    help='(deprecated) number of gpus to use before ddp (default 1)')
parser.add_argument('--itersave', default='./',
                    help='a dir to save iteration counts (default ./)')
parser.add_argument('--niter', type=int ,default=300000,
                    help='maximum iteration (default 300k)')
parser.add_argument('--local_rank', type=int, default=0)
args = parser.parse_args()

# distributed dataparallel
torch.cuda.set_device(args.local_rank)
world_size = args.nproc
torch.distributed.init_process_group(
    'nccl',
    init_method='env://',
    world_size=world_size,
    rank=args.local_rank,
)

# fix random seed
torch.manual_seed(1)
def _init_fn(worker_id):
    np.random.seed()
    random.seed()
torch.manual_seed(8)  # do it again
torch.cuda.manual_seed(1)

## set hyperparameters for training
ngpus = args.ngpus
worker_mul = int(1)

if 'expansion' in args.stage:
    datashape = [320,640]
    batch_size = 6*ngpus
elif 'seg' in args.stage:
    datashape = [320,640]
    batch_size = 6*ngpus
elif args.stage == 'chairs' or args.stage == 'things':
    datashape = [320,448]
    batch_size = 4*ngpus
elif '2015' in args.stage:
    datashape = [256,768]
    batch_size = 4*ngpus
elif 'sintel' in args.stage:
    datashape = [320,576]
    batch_size = 4*ngpus
elif args.stage == 'rob':
    datashape = [320,640]
    batch_size = 3*ngpus
else:
    print('error')
    exit(0)

## dataloader
## expansion datasets
if 'expansion' in args.stage:
    from dataloader import exploader as dd
    if '2015' in args.stage:
        if 'train' in args.stage:
            from dataloader import kitti15list_train as lk15
        elif 'tv' in args.stage:
            from dataloader import kitti15list as lk15
        iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
        disp0 = [i.replace('flow_occ','disp_occ_0') for i in flowl0]
        disp1 = [i.replace('flow_occ','disp_occ_1') for i in flowl0]
        calib = [i.replace('flow_occ','calib')[:-7]+'.txt' for i in flowl0]
        loader_kitti15_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0,prob=0.5,disp0=disp0, disp1=disp1, calib=calib)
    else:
        from dataloader import sceneflowlist as lsf
        iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Driving/'%args.database, level=6)
        loader_driving_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
        iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Monkaa/'%args.database, level=4)
        loader_monkaa_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
        iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('/ssd1/gengshay/FlyingThings3D/', level=6)
        loader_things_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
        # kitti
        from dataloader import kitti15list_train as lk15
        iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
        disp0 = [i.replace('flow_occ','disp_occ_0') for i in flowl0]
        disp1 = [i.replace('flow_occ','disp_occ_1') for i in flowl0]
        calib = [i.replace('flow_occ','calib')[:-7]+'.txt' for i in flowl0]
        loader_kitti15_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0,prob=0.5,disp0=disp0, disp1=disp1, calib=calib)
        # sintel
        from dataloader import sintellist_train as ls
        iml0, iml1, flowl0 = ls.dataloader('%s/rob_flow/training/'%args.database)
        disp0 = []; disp1 = []; calib = []
        for impath in iml0:
            passname = impath.split('/')[-1].split('_')[-4]
            seqname1 = impath.split('/')[-1].split('_')[-3]
            seqname2 = impath.split('/')[-1].split('_')[-2]
            framename = int(impath.split('/')[-1].split('_')[-1].split('.')[0])
            disp0.append('%s/Sintel/disparities/%s_%s/frame_%04d.png'%(impath.rsplit('/',2)[0], seqname1, seqname2,framename+1))
            disp1.append('%s/Sintel/disparities/%s_%s/frame_%04d.png'%(impath.rsplit('/',2)[0], seqname1, seqname2,framename+2))
            calib.append('%s/Sintel/camdata_left/%s_%s/frame_%04d.cam'%(impath.rsplit('/',2)[0], seqname1,seqname2,framename+1))
        loader_sintel_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1, noise=0, disp0=disp0, disp1=disp1, calib=calib)
elif 'seg' in args.stage:
    from dataloader import exploader as dd
    from dataloader import sceneflowlist as lsf
    iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Driving/'%args.database, level=6)
    loader_driving_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
    iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Monkaa/'%args.database, level=4)
    loader_monkaa_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
    iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/FlyingThings3D/', level=6)
    loader_things_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, disp0=disp0,disp1=dispc,calib=calib)
    # kitti
    from dataloader import kitti15list as lk15
    iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
    disp0 = [i.replace('flow_occ','disp_occ_0') for i in flowl0]
    disp1 = [i.replace('flow_occ','disp_occ_1') for i in flowl0]
    calib = [i.replace('flow_occ','calib')[:-7]+'.txt' for i in flowl0]
    # dense disp
    disp0 = [i.replace('disp_occ_0','disp_occ_0_ganet') for i in disp0]
    loader_kitti15_sc = dd.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0,prob=0.5,disp0=disp0, disp1=disp1, calib=calib)
else: # flow
    from dataloader import robloader as dr
    if args.stage == 'chairs' or 'sintel' in args.stage or args.stage=='rob':
        # flying chairs
        from dataloader import chairslist as lc
        iml0, iml1, flowl0 = lc.dataloader('%s/FlyingChairs_release/data/'%args.database)
        with open('misc/order.txt','r') as f:
            order = [int(i) for i in f.readline().split(' ')]
        with open('misc/FlyingChairs_train_val.txt', 'r') as f:
            split = [int(i) for i in f.readlines()]
        iml0 = [iml0[i] for i in order     if split[i]==1]
        iml1 = [iml1[i] for i in order     if split[i]==1]
        flowl0 = [flowl0[i] for i in order if split[i]==1]
        loader_chairs = dr.myImageFloder(iml0,iml1,flowl0, shape = datashape)
    if args.stage == 'things' or 'sintel' in args.stage or args.stage=='rob':
        # flything things
        from dataloader import thingslist as lt
        iml0, iml1, flowl0 = lt.dataloader('/ssd0/gengshay/FlyingThings3D_subset/train/')
        loader_things = dr.myImageFloder(iml0,iml1,flowl0,shape = datashape,scale=1, order=1)

    # fine-tuning datasets
    if args.stage == '2015train' or args.stage=='rob':
        from dataloader import kitti15list_train as lk15
    else:
        from dataloader import kitti15list as lk15
    if args.stage == 'sinteltrain' or args.stage=='rob':
        from dataloader import sintellist_train as ls
    else:
        from dataloader import sintellist as ls
    from dataloader import kitti12list as lk12
    from dataloader import hd1klist_train as lh

    if 'sintel' in args.stage:
        iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
        loader_kitti15 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, noise=0)  # SINTEL
        iml0, iml1, flowl0 = lh.dataloader('%s/rob_flow/training/'%args.database)
        loader_hd1k = dr.myImageFloder(iml0,iml1,flowl0,shape=datashape, scale=0.5,order=0, noise=0)
        iml0, iml1, flowl0 = ls.dataloader('%s/rob_flow/training/'%args.database)
        loader_sintel = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1, noise=0)
        #loader_sintel = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1, noise=0, scale_aug=[0.2,0.])
    if '2015' in args.stage:
        iml0, iml1, flowl0 = lk12.dataloader('%s/data_stereo_flow/training/'%args.database)
        #loader_kitti12 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5, scale_aug=[0.2,0.])
        loader_kitti12 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5)
        iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
        #loader_kitti15 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5, scale_aug=[0.2,0.])  # KITTI
        loader_kitti15 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5)  # KITTI
    if args.stage=='rob':
        #from dataloader import kitti15list as lk15
        #from dataloader import sintellist as ls
        #from dataloader import viperlist as lv
        from dataloader import kitti15list_train as lk15
        from dataloader import sintellist_train as ls
        from dataloader import viperlist_train as lv
        from dataloader import hd1klist as lh
        iml0, iml1, flowl0 = lk12.dataloader('%s/data_stereo_flow/training/'%args.database)
        loader_kitti12 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5)
        iml0, iml1, flowl0 = lk15.dataloader('%s/kitti_scene/training/'%args.database)
        loader_kitti15 = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=0, prob=0.5)  # KITTI
        iml0, iml1, flowl0 = lh.dataloader('%s/rob_flow/training/'%args.database)
        loader_hd1k = dr.myImageFloder(iml0,iml1,flowl0,shape=datashape, scale=0.5,order=1, noise=0)
        iml0, iml1, flowl0 = ls.dataloader('%s/rob_flow/training/'%args.database)
        loader_sintel = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1, noise=0)
        from dataloader import sceneflowlist as lsf
        iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Driving/'%args.database, level=6)
        loader_driving = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1)
        iml0, iml1, flowl0, disp0, dispc, calib = lsf.dataloader('%s/Monkaa/'%args.database, level=4)
        loader_monkaa = dr.myImageFloder(iml0,iml1,flowl0, shape=datashape, scale=1, order=1)
        iml0, iml1, flowl0 = lv.dataloader('%s/rob_flow/training/'%args.database)
        loader_viper = dr.myImageFloder(iml0,iml1,flowl0, shape = datashape, scale=1, order=1, scale_aug=[0.8,-0.2])

## aggregate datasets
if 'expansion' in args.stage:
    if '2015' in args.stage:
        data_inuse = torch.utils.data.ConcatDataset([loader_kitti15_sc]*10000)
    else:
        #data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_monkaa_sc]*100+[loader_things_sc]*40 + [loader_kitti15_sc]*22000 + [loader_sintel_sc]*2200)
        #data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_monkaa_sc]*100+[loader_things_sc]*40 + [loader_gtav_sc]*700)  # no kitti sintel
        data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_monkaa_sc]*100+[loader_things_sc]*40)  # no kitti sintel
    for i in data_inuse.datasets:
        i.black = False
        i.cover = True
    baselr = 1e-3
    num_steps = 7e4
elif 'seg' in args.stage:
    if args.stage=='segsf':
        #data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_things_sc]*40)
        data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_monkaa_sc]*100+[loader_things_sc]*40)
    elif args.stage=='segkitti':
        #data_inuse = torch.utils.data.ConcatDataset([loader_kitti15_sc]*22000)
        #data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_things_sc]*40 + [loader_kitti15_sc]*22000)
        data_inuse = torch.utils.data.ConcatDataset([loader_driving_sc]*200+[loader_monkaa_sc]*100+[loader_things_sc]*40 + [loader_kitti15_sc]*22000)
    for i in data_inuse.datasets:
        i.black = False
        i.cover = True
    baselr=5e-4
    num_steps = 7e4
elif args.stage=='chairs':
    data_inuse = torch.utils.data.ConcatDataset([loader_chairs]*100)
    baselr = 1e-3
    num_steps = 7e4
elif args.stage=='things':
    data_inuse = torch.utils.data.ConcatDataset([loader_things]*100)
    baselr = 1e-3
    num_steps = 7e4
elif '2015' in args.stage:
    data_inuse = torch.utils.data.ConcatDataset([loader_kitti15]*50+[loader_kitti12]*50)
    for i in data_inuse.datasets:
        i.black = True
        i.cover = True
elif 'sintel' in args.stage:
    data_inuse = torch.utils.data.ConcatDataset([loader_kitti15]*200*6+[loader_hd1k]*40*6 + [loader_sintel]*150*6 + [loader_chairs]*2*6 + [loader_things]*6)
    for i in data_inuse.datasets:
        i.black = True
        i.cover = True
    baselr = 1e-4
elif args.stage=='rob':
    data_inuse = torch.utils.data.ConcatDataset([loader_kitti12]*2700+[loader_kitti15]*2700 + [loader_sintel]*600 + [loader_chairs]*12 + [loader_things]*6 + [loader_hd1k]*900 + [loader_driving]*50 + [loader_monkaa]*25+[loader_viper]*70)
    #data_inuse = torch.utils.data.ConcatDataset([loader_chairs]*12 + [loader_things]*6 + [loader_driving]*50 + [loader_monkaa]*25+ [loader_viper]*70)  # noks
    for i in data_inuse.datasets:
        i.black = True
        i.cover = True
    baselr = 1e-3
    num_steps = 7e4
else:
    print('error')
    exit(0)

print('Total iterations: %d'%(len(data_inuse)//batch_size))
print('Max iterations: %d'  %(args.niter))

from models.VCNplus  import VCN
model = VCN([batch_size//ngpus]+data_inuse.datasets[0].shape[::-1],
md=[int(4*(args.maxdisp/256)), 4,4,4,4], fac=args.fac, exp_unc= args.loadmodel is None or not ('kitti' in args.loadmodel))

# sync bn and dataparallel
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
device = torch.device('cuda:{}'.format(args.local_rank))
model = model.to(device)
model = torch.nn.parallel.DistributedDataParallel(
    model,
    device_ids=[args.local_rank],
    output_device=args.local_rank,
    find_unused_parameters=True
)

total_iters = 0
mean_L=[[0.33,0.33,0.33]]
mean_R=[[0.33,0.33,0.33]]
if args.loadmodel is not None:
    pretrained_dict = torch.load(args.loadmodel,map_location='cpu')
    pretrained_dict['state_dict'] =  {k:v for k,v in pretrained_dict['state_dict'].items()}
    #pretrained_dict['state_dict'] =  {k:v for k,v in pretrained_dict['state_dict'].items() if 'fgnet' not in k and 'det' not in k}
    model.load_state_dict(pretrained_dict['state_dict'],strict=False)

    if args.retrain == 'true':
        print('re-training')
        if 'expansion' in args.stage or 'depth' in args.stage or 'seg' in args.stage:
            print('resuming mean from %d'%total_iters)
            mean_L=pretrained_dict['mean_L']
            mean_R=pretrained_dict['mean_R']
    else:
        with open('%s/iter_counts-%d.txt'%(args.itersave, int(args.logname.split('-')[-1])), 'r') as f:
            total_iters = int(f.readline())
        print('resuming from %d'%total_iters)
        mean_L=pretrained_dict['mean_L']
        mean_R=pretrained_dict['mean_R']

if args.loadflow is not None:
    pretrained_dict = torch.load(args.loadflow,map_location='cpu')
    pretrained_dict['state_dict'] =  {k:v for k,v in pretrained_dict['state_dict'].items() if 'f_modules' in k or 'p_modules' in k or 'oor_modules' in k or 'fuse_modules' in k}
    model.load_state_dict(pretrained_dict['state_dict'],strict=False)

mix_precision = False
print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()])))
optimizer = optim.AdamW(model.parameters(), lr=baselr, weight_decay=0.0001, eps=1e-8)
scheduler = optim.lr_scheduler.OneCycleLR(optimizer, baselr, int(num_steps+100),
        pct_start=0.05, cycle_momentum=False, anneal_strategy='linear')
if args.local_rank==0: log = SummaryWriter('%s/%s'%(args.savemodel,args.logname), comment = args.logname)
scaler = GradScaler(enabled=mix_precision)

def train(imgL,imgR,flowl0,imgAux,intr, imgoL, imgoR, occp, RT01):
        model.train()
        imgL   = Variable(torch.FloatTensor(imgL))
        imgR   = Variable(torch.FloatTensor(imgR))
        flowl0 = Variable(torch.FloatTensor(flowl0))

        imgL, imgR, flowl0 = imgL.cuda(device), imgR.cuda(device), flowl0.cuda(device)
        # mask: valid flow GT & within pre-defined range
        mask = (flowl0[:,:,:,2] == 1) & (flowl0[:,:,:,0].abs() < args.maxdisp) & (flowl0[:,:,:,1].abs() < (args.maxdisp//args.fac))
        if not imgAux is None:
            imgAux = imgAux.cuda(device)
            imgoL, imgoR = imgoL.float().cuda(device), imgoR.float().cuda(device)
            # mask: + 0.01<depth<100, imgAux: depth, d1,d2,d2,flow3d
            mask = mask & (imgAux[:,:,:,0] < 100) & (imgAux[:,:,:,0] > 0.01)
            exp_flag = True
        else:
            exp_flag = False
        if 'expansion' in args.stage:
            exp_flag = 1 # expanson
        elif 'seg' in args.stage:
            exp_flag = 2 # segmentation
        else:
            exp_flag = 0 # flow
        mask.detach_();

        # rearrange inputs
        groups = []
        for i in range(ngpus):
            groups.append(imgL[i*batch_size//ngpus:(i+1)*batch_size//ngpus])
            groups.append(imgR[i*batch_size//ngpus:(i+1)*batch_size//ngpus])

        # forward-backward
        optimizer.zero_grad()
        disp_input = None
        #disp_input = 1./torch.clamp(imgAux[:,:,:,0],1,100)[:,np.newaxis]
        with autocast(enabled=mix_precision):
            output = model(torch.cat(groups,0), [flowl0,mask,imgAux,intr, imgoL, imgoR, occp, RT01, exp_flag],disp_input=disp_input)
            loss = output[-3].mean()

        scaler.scale(loss).backward()
        scaler.unscale_(optimizer)
        grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 100.)
        scaler.step(optimizer)
        scheduler.step()
#        for param_group in optimizer.param_groups:
#            print(param_group['lr'])
        scaler.update()

#        loss.backward()
#        #torch.nn.utils.clip_grad_norm_(model.parameters(), 100.0)
#        optimizer.step()

        # for debugging
        if np.isnan(np.asarray(model.module.dc2_conv7.weight.max().detach().cpu())):
            pdb.set_trace()
            pass

        vis = {}
        vis['output2'] = output[0].detach().cpu().numpy()
        vis['output3'] = output[1].detach().cpu().numpy()
        vis['output4'] = output[2].detach().cpu().numpy()
        vis['output5'] = output[3].detach().cpu().numpy()
        vis['output6'] = output[4].detach().cpu().numpy()
        if 'expansion' in args.stage:
            vis['mid'] = output[6][0].detach().cpu().numpy()
            vis['exp'] = output[7][0].detach().cpu().numpy()
        elif 'seg' in args.stage:
            vis['fg'] = output[6][0].detach().cpu().numpy()
            vis['fg_gt'] = output[7][0].detach().cpu().numpy()
        vis['gt'] = flowl0[:,:,:,:].detach().cpu().numpy()
        if mask.sum():
            vis['AEPE'] = realEPE(output[0].detach(), flowl0.permute(0,3,1,2).detach(),mask,sparse=False)
        vis['mask'] = mask
        vis['grad_norm'] = grad_norm
        return loss.data,vis

# get global counts
with open('%s/iter_counts-%d.txt'%(args.itersave, int(args.logname.split('-')[-1])), 'w') as f:
    f.write('%d'%total_iters)

def main():
    sampler = torch.utils.data.distributed.DistributedSampler(
        data_inuse,
        num_replicas=args.nproc,
        rank=args.local_rank,
    )
    TrainImgLoader = torch.utils.data.DataLoader(
         data_inuse,
         batch_size= batch_size, num_workers=int(worker_mul*batch_size), drop_last=True, worker_init_fn=_init_fn, pin_memory=True,sampler=sampler)
    start_full_time = time.time()
    global total_iters

    # training loop
    for batch_idx, databatch in enumerate(TrainImgLoader):
        if 'expansion' in args.stage or 'seg' in args.stage:
            imgL_crop, imgR_crop, flowl0,imgAux,intr, imgoL, imgoR, occp, RT01  = databatch
            intr = [t.float() for t in intr]
        else:
            imgL_crop, imgR_crop, flowl0 = databatch
            imgAux,intr, imgoL, imgoR, occp, RT01 = None,None,None,None,None,None
        if total_iters < 1000 and not ('expansion' in args.stage or 'seg' in args.stage):
            # subtract mean
            mean_L.append( np.asarray(imgL_crop.mean(0).mean(1).mean(1)) )
            mean_R.append( np.asarray(imgR_crop.mean(0).mean(1).mean(1)) )
        imgL_crop -= torch.from_numpy(np.asarray(mean_L).mean(0)[np.newaxis,:,np.newaxis, np.newaxis]).float()
        imgR_crop -= torch.from_numpy(np.asarray(mean_R).mean(0)[np.newaxis,:,np.newaxis, np.newaxis]).float()

        start_time = time.time()
        loss,vis = train(imgL_crop,imgR_crop, flowl0, imgAux,intr, imgoL, imgoR, occp, RT01)

        if args.local_rank==0:
            print('Iter %d training loss = %.3f , time = %.2f' %(batch_idx, loss, time.time() - start_time))
            if total_iters %10 == 0:
                log.add_scalar('train/loss_batch',loss, total_iters)
                log.add_scalar('train/aepe_batch',vis['AEPE'], total_iters)
                log.add_scalar('train/grad_norm',vis['grad_norm'], total_iters)
            if total_iters %100 == 0:
                #torch.cuda.empty_cache()
                add_image(log,'train/left', imgL_crop[0:1],total_iters)
                add_image(log,'train/right',imgR_crop[0:1],total_iters)
                if len(np.asarray(vis['gt']))>0:
                    log.add_histogram('train/gt_hist',np.asarray(vis['gt']).reshape(-1,3)[np.asarray(vis['gt'])[:,:,:,-1].flatten().astype(bool)][:,:2], total_iters)
                gu = vis['gt'][0,:,:,0]; gv = vis['gt'][0,:,:,1]
                gu = gu*np.asarray(vis['mask'][0].float().cpu());  gv = gv*np.asarray(vis['mask'][0].float().cpu())
                mask = vis['mask'][0].float().cpu()
                add_image(log,'train/gt0', flow_to_image(np.concatenate((gu[:,:,np.newaxis],gv[:,:,np.newaxis],mask[:,:,np.newaxis]),-1))[np.newaxis],total_iters)
                add_image(log,'train/output2',flow_to_image(vis['output2'][0].transpose((1,2,0)))[np.newaxis],total_iters)
                add_image(log,'train/output3',flow_to_image(vis['output3'][0].transpose((1,2,0)))[np.newaxis],total_iters)
                add_image(log,'train/output4',flow_to_image(vis['output4'][0].transpose((1,2,0)))[np.newaxis],total_iters)
                add_image(log,'train/output5',flow_to_image(vis['output5'][0].transpose((1,2,0)))[np.newaxis],total_iters)
                add_image(log,'train/output6',flow_to_image(vis['output6'][0].transpose((1,2,0)))[np.newaxis],total_iters)
                if 'expansion' in args.stage:
                    add_image(log,'train/mid_gt',(1+imgAux[:1,:,:,6]/imgAux[:1,:,:,0]).log() ,total_iters)
                    add_image(log,'train/mid',vis['mid'][np.newaxis],total_iters)
                    add_image(log,'train/exp',vis['exp'][np.newaxis],total_iters)
                if 'seg' in args.stage:
                    add_image(log,'train/fg_gt',vis['fg_gt'][np.newaxis],total_iters)
                    add_image(log,'train/fg_pred',vis['fg'][np.newaxis],total_iters)
        total_iters += 1
        # get global counts
        with open('%s/iter_counts-%d.txt'%(args.itersave,int(args.logname.split('-')[-1])), 'w') as f:
            f.write('%d'%total_iters)
#        torch.cuda.empty_cache()

        if (total_iters + 1)%2000==0:
            if args.local_rank==0:
                #SAVE
                savefilename = args.savemodel+'/'+args.logname+'/finetune_'+str(total_iters)+'.pth'
                save_dict = model.state_dict()
                save_dict = collections.OrderedDict({k:v for k,v in save_dict.items() if ('reg_modules' not in k or 'conv1' in k) and ('grid' not in k) and ('flow_reg' not in k) and ('midas' not in k)  })
                torch.save({
                    'iters': total_iters,
                    'state_dict': save_dict,
                    'mean_L': mean_L,
                    'mean_R': mean_R,
                }, savefilename)

    print('full finetune time = %.2f HR' %((time.time() - start_full_time)/3600))

if __name__ == '__main__':
    main()


# import libraries here
import pygame

# global constant
N = 9

# functions below

# a function to print the grid

def print_grid(grid):
    for i in range(len(grid)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(grid[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(grid[i][j])
            else:
                print(str(grid[i][j]) + " ", end="")

# a function to check if it is ok to assign a number in that cell

def is_okay(grid, row, col, num):

    # check if it finds the same number
    # anywhere else in the row
    # return false if so
    for x in range(9):
        if grid[row][x] == num:
            return False

    # check if it find the same number
    # anywhere else in the column
    # return False if so
    for x in range(9):
        if grid[x][col] == num:
            return False

    # check if it finds the same num in the 3 by 3 box
    # return False if so
    row_start = row - row % 3
    col_start = col - col % 3
    for i in range(3):
        for j in range(3):
            if grid[i + row_start][j + col_start] == num:
                return False

    return True

# a function to solve the sudoku entirely
def solve_sudoku(grid, row, col):

    # check if the last cell has been reached
    # this would be the 8th row and 9th column
    # this means that the sudoku has been solved
    # therefore, return True to end the backtracking
    if (row == N - 1 and col == N):
        return True

    # check if it column is 9
    # then move to the next row with column at 0
    if col == N:
        row += 1
        col = 0

    # check if the current position on the grid
    # already has a value greater than 0,
    # then iterate next column
    if grid[row][col] > 0:
        return solve_sudoku(grid, row, col + 1)

    for num in range(1, N + 1, 1):

        # check if it is okay to place in the current cell
        # then move to the next column
        if is_okay(grid, row, col, num):

            # assigning the num in the current position
            grid[row][col] = num

            # check for next column
            if solve_sudoku(grid, row, col + 1):
                return True

        # use a different num value
        # as the previous num did not work
        grid[row][col] = 0

    return False

# Driver code below

# 0 means empty, unassigned cells
grid = [[3, 0, 6, 5, 0, 8, 4, 0, 0],
        [5, 2, 0, 0, 0, 0, 0, 0, 0],
        [0, 8, 7, 0, 0, 0, 0, 3, 1],
        [0, 0, 3, 0, 1, 0, 0, 8, 0],
        [9, 0, 0, 8, 6, 3, 0, 0, 5],
        [0, 5, 0, 0, 9, 0, 6, 0, 0],
        [1, 3, 0, 0, 0, 0, 2, 5, 0],
        [0, 0, 0, 0, 0, 0, 0, 7, 4],
        [0, 0, 5, 2, 0, 6, 3, 0, 0]]

print("\nThis is the grid we will be solving today\n")
print_grid(grid)
print('\n')
print("*****************")
print('\n')

if (solve_sudoku(grid, 0, 0)):
    print_grid(grid)
    print("\nThis Sudoku Puzzle has been solved :)\n")

else:
    print("No Solution Exists :(")


from src.reference import JAVA_CODE_PATH

def get_code(path: str):
    with open(path, 'r') as java_file:
        with open(JAVA_CODE_PATH, 'w') as code:
            code.writelines(list(java_file))

def augment_code():
    with open(JAVA_CODE_PATH, 'r') as code:
        lines = code.readlines()

        for i in range(len(lines)):
            if lines[i].startswith('//'):
                lines[i] = ''

            if 'int n = 5;' in lines[i]:
                lines[i] = '      int n = Integer.parseInt(args[0]);\n'

    with open(JAVA_CODE_PATH, 'w') as code:
        code.writelines(lines)


from collections import defaultdict
import heapq

#ADJの方が圧倒的に早い感
#(matの受け渡しが辛い?)

class Dijkstra:
    """
    dijkstraの最短経路問題を解く
    input:
        S: start node
        mat : mat[a][b] = edge_weight(a,b)
    """

    def __init__(self, start, num_node, mat):
        """
        mat: mat[a][b] = edge_weight(a,b)
        num_node: num of nodes
        dist: dist from start_node
        prev: 最短経路において、一つ前のノードを記憶。経路復元に使う?
        """
        self.start_node = start
        self.mat = mat
        self.num_node = num_node
        self.dist = [float('inf')]*num_node
        self.prev = defaultdict(lambda: None)

        # dist form S to S = 0
        self.dist[start] = 0

    def calc_dist(self):
        """
        最短経路 distを計算する
        Q: content (dist_from_start,node)
        Qはheapqとして使う。dist_from_startの小さい順になる
        """
        Q = []
        heapq.heappush(Q, (0, self.start_node))
        while Q:
            dist_to_node, node = heapq.heappop(Q)
            # 現在の値よりも大きくて、更新にならない場合
            if self.dist[node] < dist_to_node:
                continue

            # 次のホップを計算する
            for nex in range(self.num_node):
                weight = self.mat[node][nex]
                cand = dist_to_node + weight

                if self.dist[nex] > cand:  # update
                    self.dist[nex] = cand
                    self.prev[nex] = node
                    heapq.heappush(Q, (cand, nex))

    def get_dist(self, d):
        """
            startからdまでの距離を返す
            これの前にcalc()を実行しておく
            たどり着けない場合は、float('inf')がかえる
        """
        return self.dist[d]
    def get_path(self,dst):
        """
        startからdstまでのpathをlistでreturn
        valifyしてないので怪しい
        """
        cur=dst
        ret=[cur]
        while cur!=self.start_node:
            cur=self.prev[cur]
            ret.append(cur)
        return list(reversed(ret))


#!/usr/bin/env python
# -*- coding: utf -*-
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

def main():
    if len(sys.argv) != 2:
        print('Usage: {} <log file>'.format(sys.argv[0]))
        sys.exit(1)
    throughputs = {}
    delays = {}
    with open(sys.argv[1]) as f:
        for l in f:
            if 'Window size' in l:
                current_size = int(l.split()[2])
                throughputs.setdefault(current_size, [])
                delays.setdefault(current_size, [])
            if 'throughput' in l:
                throughputs[current_size].append(float(l.split()[2]))
            elif 'signal delay' in l:
                delays[current_size].append(float(l.split()[4]))
    for window_size in throughputs:
        plt.scatter(
            delays[window_size],
            throughputs[window_size],
            marker='x',
            label=window_size)
    plt.gca().invert_xaxis()
    plt.ylabel('Throughput (Mbits/s)')
    plt.xlabel('Delay (ms)')
    plt.xscale('log')
    plt.title('Fixed window sizes')
    plt.legend()
    plt.savefig('exercise_a.png', bbox_inches='tight')

if __name__ == '__main__':
    main()


# Import modul dan lib yang diperlukan
from flask import Flask, render_template, request, url_for, redirect, flash
from sklearn.cluster import MiniBatchKMeans
from matplotlib.colors import rgb2hex
from collections import Counter
from PIL import Image
import numpy as np
import os

# Setup flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'klasifikasi-warna'
app.config['UPLOAD_PATH'] = "static"
app.config['SESSION_TYPE'] = 'filesystem'

# Set laman berada
@app.route("/", methods=["GET", "POST"])
def home():

    if request.method == "GET":
        return render_template("index.html")

    elif request.method == "POST":
        img = request.files["image_file"]
        if img.filename != "":
            img.save( os.path.join(app.config['UPLOAD_PATH'], img.filename) )

            # Buka citra yang akan diklasififikasi
            img_object = Image.open(fp=f"static/{img.filename}", mode="r")

            # Konversi citra ke 2d array
            img_array = np.array(img_object)

            # Gunakan MiniBatchKMeans algoritma dengan mengatur cluster n sebanyak 7
            k_warna = MiniBatchKMeans(n_clusters=7)
            k_warna.fit(img_array.reshape(-1, 3))

            # Hitung berapa banyak pixel per cluster
            n_pixels = len(k_warna.labels_)
            counter = Counter(k_warna.labels_)

            # 2D array yang berisi nilai pixel RGB sejumlah n cluster = 7
            rgb_int = k_warna.cluster_centers_

            # Konversi ke float 0-1
            rgb_float = np.array(rgb_int / 255, dtype=float)

            # Konversi nilai RGB ke nilai HEXA dan simpan ke dalam list
            hex_values = [rgb2hex(rgb_float[i, :]) for i in range(rgb_float.shape[0])]

            prop_warna = {}

            # Kalkulasi presentase tiap pixel warna
            for i in counter:
                prop_warna[i] = np.round(counter[i] / n_pixels, 4)

            # Konversi dictionary ke dalam list
            prop_warna = dict(sorted(prop_warna.items()))
            props_list = [value for (key, value) in prop_warna.items()]

            def to_dictionary(key, value):
                return dict(zip(key, value))

            # Merge 2 list ke dalam sebuah dictionary
            dict_warna = to_dictionary(props_list, hex_values)

            # Sort/urutkan dict secara descending
            sorted_dict = dict(sorted(dict_warna.items(), reverse=True))

            return render_template("index.html", image=img.filename, colors=sorted_dict)
        else:
            # Tampilkan error ketika belum mengunggah citra
            flash("Kamu belum mengunggah citra!")
            return redirect(url_for("home"))

# Jalankan flask app
if __name__ == "__main__":
    app.run(host='0.0.0.0', port='5000', debug=True)


# -*- coding: utf-8 -*-
"""
Created on Sat Aug  5 19:14:50 2023

@author: Nabanita Paul
"""

class car:
    pass

car1 = car()
car1.brand ='Audi'
car1.color='Black'
print(car1.brand)
print(car1.color)

class car:
    def __init__(self,brand, color):
        self.brand=brand
        self.color =color

car1 =car('Audi','Black')
car1.color

class car:
    def __init__(self,brand, color):
        self.brand=brand
        self.color =color
    def car_details(self):
        return "The brand of the car is {} and the color of the car is {}".format(self.brand,self.color)

car1 =car('Audi','Black')
car1.car_details()

## Inheritence
car1.windows=4
print(car1.windows)
class audi(car):
    def __init__(self,brand, color,windows):
        super().__init__(brand, color)
        self.windows =windows
    def  enable_ai(self):
        return "AI is enabled for Audi"

audi1=audi('011','Golden','4')
audi1.windows
audi1.enable_ai()
audi1.car_details()
audi1
audi

## Multiple Inheritence

class A:
    def method1(self):
        print("Class A method is called")

class B(A):
    def method1(self):
        print("Class B method is called")
    def method2(self):
        print("Class B method2 is called")
class C(A):
    def method1(self):
        print("Class C method is called")
class D(B,C):
    def method1(self):
        print("Class D method is called")

d =D()
d.method1()
d.method2()

A.method1(D)
B.method1(D)
C.method1(D)
D.method1(D)
class D(B,C):
    def method1(self):
        print("Class D method is called")
        A.method1(self)
        B.method1(self)
        C.method1(self)
d =D()
d.method1()


import matplotlib.pyplot as plt
import sys
import tensorflow as tf
import os

if len(sys.argv) < 2:
    print("Usage: python main.py <path to log file folder>")
    exit()
logdir = sys.argv[1]

losses = []
directory = os.fsencode(logdir)
for logfile in os.listdir(directory):
    filename = os.fsdecode(logfile)
    file_path = os.path.join(logdir, filename)
    for e in tf.train.summary_iterator(file_path):
        for v in e.summary.value:
            if v.tag == 'loss':
                losses.append(v.simple_value)

plt.plot(losses)
plt.ylabel('Loss')
plt.savefig('loss.png')


from flask import Flask , render_template, request
import pickle
import numpy as np
import pandas as pd
import joblib
import warnings
import sys
if not sys.warnoptions:
    warnings.simplefilter("ignore")
warnings.filterwarnings("ignore",category=DeprecationWarning)

# model=pickle.load(open('first.h5','rb'))

model=joblib.load('loan_prediction')

app = Flask(__name__,template_folder='template')

@app.route('/')
def main():
    return render_template('home.html')

@app.route('/predict',methods=['POST'])
def home():
    name = request.form['NAME']
    address = request.form['ADDRESS']
    email = request.form['EMAIL']
    mobile1 = request.form['MOBILE']

# form
    gender1 = request.form['GENDER']
    married1 = request.form['MARRIED']
    dependents1 = request.form['DEPENDENTS']
    education1 = request.form['EDUCATION']
    self_emp1 = request.form['SELF_EMP']
    app_income1 = request.form['APP_INCOME']
    coapp_income1 = request.form['COAPP_INCOME']
    loan_amount1 = request.form['LOAN_AMOUNT']
    la_term1 = request.form['LA_TERM']
    cr_history1 = request.form['CR_HISTORY']
    property1 = request.form['PROPERTY']

# valeurs saisies de string vers int / float
    mobile=int(mobile1)
    app_income=int(app_income1)
    coapp_income=float(coapp_income1)
    loan_amount=float(loan_amount1)
    la_term=float(la_term1)

    if(cr_history1=='Clear'):
        cr_history=1.0
    else:
        cr_history=0.0
    if(married1=='YES'):
        married=1
    else:
        married=0
    if(gender1=='MALE'):
        gender=1
    else:
        gender=0

    if(dependents1=='zero'):
        dependents='0'
    if(dependents1=='one'):
        dependents='1'
    if(dependents1=='two'):
        dependents='2'
    if(dependents1=='three_plus'):
        dependents='3+'

    if(education1=='Graduate'):
        education=1
    else:
        education=0
    if(self_emp1=='yes'):
        self_emp=1
    else:
        self_emp=0

    p_rural=0
    p_semi_urban=0
    p_urban=0
    if(property1=="rural"):
        p_a=0
    if(property1=="semi-urban"):
       p_a=2
    if(property1=="urban"):
        p_a=1

    arr=np.array([gender,married,dependents,education,self_emp,app_income,coapp_income,loan_amount,la_term,cr_history,p_a])
    # df=pd.DataFrame(arr)
    pred= model.predict(arr.reshape(1,-1))
    return render_template('prediction_page.html', data=pred,name=name)

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


"""
read specific line(s) #

function signature
"""

def gertline(start_no, end_no):
    pass
def getline(line_no):
    if line_no < 1:
        return "No such line"
    # line_no = 2
    with open("filemode_3.txt") as file:
        data = file.readlines()
        if line_no > line_no:

            return "No such line"
        content = data[line_no-1]
        return content

#  main program
try:
    lineno = int(input("Please enter a line:"))
    print(getline(lineno), end="")

except ValueError as e:
    print(e)
print(getline(1), end="")
print(getline(2), end="")
print(getline(3), end="")
print(getline(4), end="")
print(getline(5), end="")


import torch.optim as optim
import torch
import math
import os
import sys
from utils.datasets import get_train_data, get_test_query, get_test_db
from utils.model import MyAlexNet, pretrain_alexnet, SGDWoReplace, AttnNet, copy_mask_MyAlexNet
from utils.misc import load_dict
from utils.train_utils import copy_params, reset_model, schedule_dict
from utils.test_utils import run_test
from utils.hashloss import sim_log_loss_double

sys.path.append(os.path.dirname(__file__))

def train(config):
    autocuda = config["autocuda"]

    test_period = config["test_period"]

    init_scale = 1.0
    scale = init_scale
    scale_step_size = 200
    scale_iter_num = 0
    scale_gamma = 0.005
    scale_power = 0.5

    train_loader_1 = get_train_data(config["data"]["train_set"])
    train_loader_2 = get_train_data(config["data"]["train_set"], seed=3571)

    query_loader = get_test_query(config["data"]["query_set"], config["test_10crop"])
    db_loader = get_test_db(config["data"]["db_set"], config["test_10crop"])

    net = pretrain_alexnet(autocuda(MyAlexNet(num_bits=config["num_bits"])))
    net.train(True)
    net_shadow = autocuda(MyAlexNet(num_bits=config["num_bits"]))
    copy_params(net, net_shadow)
    attn_net = autocuda(AttnNet(hidden_size=100))
    attnnet_optimizer = optim.Adam(attn_net.parameters(), lr=config["attn_lr"])

    # torch.save(net.hash_layer.state_dict(), os.path.join("./saved_model/init/", "hash_layer64".format("model")))
    # torch.save(net.hash_layer.state_dict(), os.path.join("./saved_model/init/", "hash_layer64".format("model")))
    # torch.save(attn_net.state_dict(), os.path.join("./saved_model/init/", "attn".format("model")))

    hash_layer_dict = torch.load(os.path.join("./saved_model/init/", ("hash_layer"
                                                                       + str(config["num_bits"])).format("model"))
                                 , map_location=lambda storage, loc: storage)
    attn_net_dict = torch.load(os.path.join("./saved_model/init/", "attn".format("model")),
                               map_location=lambda storage, loc: storage)

    load_dict(net.hash_layer, hash_layer_dict)
    load_dict(attn_net, attn_net_dict)

    optimizer_config = config["optimizer"]
    parameter_list = [{"params": net_shadow.feature_layers.parameters(), "lr": 1},
                      {"params": net_shadow.hash_layer.parameters(), "lr": 10}]
    optimizer = SGDWoReplace(parameter_list, **(optimizer_config["optim_params"]))

    param_lr = []
    for param_group in optimizer.param_groups:
        param_lr.append(param_group["lr"])

    schedule_param = optimizer_config["lr_param"]
    lr_scheduler = schedule_dict[optimizer_config["lr_type"]]
    j = 0
    running_loss = 0.0
    running_attn_loss = 0.0

    for epoch in range(config["epoch"]):  # loop over the dataset multiple times

        for i, ((data1, labels1), (data2, labels2)) in enumerate(zip(train_loader_1, train_loader_2), 0):

            net.train()
            net_shadow.train()

            data1, labels1 = autocuda(data1), autocuda(labels1)
            data2, labels2 = autocuda(data2), autocuda(labels2)

            # zero the parameter gradients
            optimizer = lr_scheduler(param_lr, optimizer, j + i, **schedule_param)
            net.zero_grad()
            net.clear_dropout_mask()
            reset_model(net_shadow)
            attnnet_optimizer.zero_grad()

            scale_iter_num += 1
            if scale_iter_num % scale_step_size == 0:
                scale = init_scale * math.pow((1. + scale_gamma * scale_iter_num), scale_power)

            # forward + backward + optimize
            batch_size = data1.size(0)
            data = torch.torch.cat((data1, data2), dim=0)
            feat = net(data)
            feats1, feats2 = torch.split(feat, batch_size, dim=0)
            outputs1 = torch.tanh(scale * feats1)
            outputs2 = torch.tanh(scale * feats2)

            attn_net.get_cont_code_1(outputs1)
            attn_net.get_cont_code_2(outputs2)
            h1 = outputs1.register_hook(attn_net.set_code_grad_1)
            h2 = outputs2.register_hook(attn_net.set_code_grad_2)
            loss = sim_log_loss_double(outputs1, labels1, outputs2, labels2,
                                       positive_weight=config["loss"]["positive_weight"],
                                       sigmoid_param=config["loss"]["sigmoid_param"],
                                       hook=attn_net.get_sim_grad)

            loss.backward(create_graph=True)
            optimizer.step(net, net_shadow)

            copy_params(net_shadow, net)

            h1.remove()
            h2.remove()

            if epoch <= 1000:
                copy_mask_MyAlexNet(net, net_shadow)
                feat = net_shadow(data)
                feats1, feats2 = torch.split(feat, batch_size, dim=0)

                outputs3 = torch.tanh(scale * feats1)
                outputs4 = torch.tanh(scale * feats2)

                attn_loss = sim_log_loss_double(outputs3, labels1, outputs4, labels2,
                                                sigmoid_param=config["loss"]["sigmoid_param"],
                                                positive_weight=config["loss"][
                                                    "positive_weight"])

                attn_loss.backward()
                attnnet_optimizer.step()

            # print statistics
            running_loss += loss.item()
            running_attn_loss += attn_loss.item()
            if (i + j) % test_period == (test_period - 1):

                # quickly test the mAP by using only 1000 query instances. For evaluation, num_query should set as None.
                m_a_p= run_test(query_loader, db_loader, net.eval(),autocuda, test_10crop=config["test_10crop"],
                                                                          num_query=1000,
                                                                          num_top_return=config["test"]["top_return"])

                print('[%d, %5d] loss: %.3f; loss: %.3f; MAP: %.4f' %
                      (epoch + 1, i + 1, running_loss / test_period, running_attn_loss / test_period, m_a_p))

                if config["save_model"]:
                    torch.save(net.state_dict(), os.path.join(config["output_path"], "alexnet".format("model")))

                running_loss = 0.0
                running_attn_loss = 0.0
                net.train(True)
        j = j + i + 1

    print('Training is complete')


import asyncio
import nest_asyncio # must be before any imports
nest_asyncio.apply()

import json
from functools import partial
import http
import http.server
import socketserver
import os
import sys

import discord

current_dir = os.path.dirname(__file__)

DISCORD_CLIENT_TOKEN_KEY = "discordClientToken"
DISCORD_CHANNEL_ID_KEY = "discordChannelId"
POSSIBLE_PORTS_KEY = "possiblePorts"

TEMP_VARIABLE_L_SP_PAREN = "<<<<<ae05d0454a05221d2705b1e8b3df7997"
TEMP_VARIABLE_R_SP_PAREN = ">>>>>b61d275e529583db4f8f94f82ef96851"

TEMP_VARIABLE_L_PAREN = "<<<<<1c67bbc1e3d67fcb0f99cbe596ddc6bf"
TEMP_VARIABLE_R_PAREN = ">>>>>5adc29d9c262b3b393f1effce07f15d5"

POST_SEPERATOR = "<<|>>"

def quit(message=None):
	if message is None:
		sys.exit()
	else:
		sys.exit(message)

def fill_template(templ_string, fill_dict):
	temp_main_template = templ_string.replace("{%", TEMP_VARIABLE_L_SP_PAREN)
	temp_main_template = temp_main_template.replace("%}", TEMP_VARIABLE_R_SP_PAREN)

	temp_main_template = temp_main_template.replace("{", TEMP_VARIABLE_L_PAREN)
	temp_main_template = temp_main_template.replace("}", TEMP_VARIABLE_R_PAREN)

	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_L_SP_PAREN, "{")
	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_R_SP_PAREN, "}")

	temp_main_template = temp_main_template.format(**fill_dict)

	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_L_PAREN, "{")
	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_R_PAREN, "}")

	return temp_main_template

def filenames_in_directory(direc):
	for (_, _, filenames) in os.walk(direc):
			return filenames

def build_player_data_json():
	out_arr = []
	for file in filenames_in_directory(os.path.join(current_dir, "saved")):
		if (file != '.gitignore'):
			with open(os.path.join(current_dir, f"saved/{file}"), "r") as f:
				out_arr.append(json.load(f))
	return json.dumps(out_arr)

def get_html():
	with open(os.path.join(current_dir, "templates/main.tmpl"), "r") as f:
		main_template = f.read()

	with open(os.path.join(current_dir, "templates/style.css"), "r") as f:
		style = f.read()

	with open(os.path.join(current_dir, "default.json"), "r") as f:
		default_info = json.load(f)

	player_data = build_player_data_json()

	fill_dict = {
		"style_info":style,
		"default_player_data":json.dumps(default_info),
		"player_data":player_data,
		"post_seperator":POST_SEPERATOR
	}

	return fill_template(main_template, fill_dict)

def create_roll_command(data):
	command = []

	if data["greens"] > 0:
		command.append(f"{data['greens']}g")

	if data["yellows"] > 0:
		command.append(f"{data['yellows']}y")

	if data["blues"] > 0:
		command.append(f"{data['blues']}b")

	if data["purples"] > 0:
		command.append(f"{data['purples']}p")

	if data["blacks"] > 0:
		command.append(f"{data['blacks']}k")

	if data["reds"] > 0:
		command.append(f"{data['reds']}r")

	if len(command) > 0:
		return "!r " + " ".join(command)
	else:
		return None

def save_json_to_file(data):
	with open(os.path.join(current_dir, f"saved/{data['base']['name']}.json"), "w") as f:
		json.dump(data, f)

def delete_json_file(data):
	os.remove(f"saved/{data['base']['name']}.json")

class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
	def __init__(self, discord_client, discord_channel_id, *args, **kwargs):
		self.discord_client = discord_client
		self.discord_channel = discord_client.get_channel(discord_channel_id)
		super().__init__(*args, **kwargs)

	def do_GET(self):
		self.send_response(200)
		self.send_header("Content-type", "text/html")
		self.end_headers()

		html = get_html()
		self.wfile.write(bytes(html, "utf8"))

	def do_POST(self):
		content_length = int(self.headers['Content-Length'])
		post_data = self.rfile.read(content_length).decode("utf-8")
		if post_data == "shutdown":
			print("Shutting Down")
			quit()
		elif post_data == "destiny":
			self.send_message("!destiny roll")
		else:
			command, data = post_data.split(POST_SEPERATOR)
			json_post_data = json.loads(data)
			if command == "roll":
				roll_command = create_roll_command(json_post_data)
				if roll_command is not None:
					self.send_message(roll_command)
			if command == "save":
				save_json_to_file(json_post_data)

		self.send_response(200)
		self.send_header('Content-Type', 'application/json')
		self.end_headers()

	def send_message(self, message):
		self.discord_client.loop.run_until_complete(self.discord_channel.send(message))

async def serve(discord_client, discord_channel_id, possible_ports):
	handler = partial(HttpRequestHandler, discord_client, discord_channel_id)
	found_port = False
	for p in possible_ports:
		try:
			with socketserver.TCPServer(("", p), handler) as my_server:
				found_port = True
				print(f"starting server on {my_server.server_address[0]}:{my_server.server_address[1]}")
				await discord_client.wait_until_ready()
				await my_server.serve_forever()
				my_server.server_close()
				break
		except OSError:
			print(f"port {p} already in use moving on...")
			continue

	if not found_port:
		print("could not find open port in range")
		quit()

class DiscordClient(discord.Client):
		async def on_ready(self):
			print('Logged on as', self.user)

def main():
	try:
		with open(os.path.join(current_dir, "config.json"), "r") as f:
			config = json.load(f)
	except json.decoder.JSONDecodeError:
		quit("'config.json' could not be read, or is not filled in correctly.")

	if all(config[x] is not None for x in [DISCORD_CLIENT_TOKEN_KEY, DISCORD_CHANNEL_ID_KEY]):
		client = DiscordClient()
		client.loop.create_task(serve(client, config[DISCORD_CHANNEL_ID_KEY], config[POSSIBLE_PORTS_KEY]))
		client.run(config[DISCORD_CLIENT_TOKEN_KEY], bot=False)
	else:
		print(f"Could not find {DISCORD_CLIENT_TOKEN_KEY} or {DISCORD_CHANNEL_ID_KEY} in config.json.")
		print("Shutting Down.")
		quit()

if __name__ == "__main__":
	main()


import lexicon
import synonyms
import game

##import mobs

synonyms = synonyms.synonym_list
tokens = lexicon.tokens

Player = game.Player('yourself',50,[],'house')

def player_move(direction):
    print(Player.move(direction))

def check_commands(s):
    try:
        if s[0] == 'GO' and s[1] in tokens['DIRECTIONS']:
            player_move(s[1])
        elif s[0] in tokens['DIRECTIONS']:
            player_move(s[0])
        elif s[0] == 'LOOK' and 'L':
            print(Player.look())
        elif s[0] == 'QUIT':
            print("Goodbye!")
            exit()

        else:
            print("Can't do that!")
    except IndexError:
        print("Index Error")

def synonymize(s):
    if isinstance(s,str):
        if s in synonyms.keys():
            return synonyms[s]
    if isinstance(s,list):
        for num in range(0,len(s)):
            if s[num] in synonyms.keys():
                s[num] = synonyms[s[num]]
        return s
    else:
        return None

def sanitize_sentence(sentence):
    try:
        while 1:
            if 'xxx' in sentence:
                sentence.remove('xxx')
            else:
                break
        return sentence
    except ValueError:
        return sentence

def parse(words):
    sentence = ['xxx','xxx','xxx']
    for word in words:
        if word.upper() in tokens['DIRECTIONS']:
            if sentence[1] == 'xxx':
                sentence[1] = word.upper()
        if word.upper() in tokens['VERBS']:
            if sentence[0] == 'xxx':
                sentence[0] = word.upper()
        if word.upper() in tokens['NOUNS']:
            if sentence[2] == 'xxx':
                sentence[2] = word.upper()
    return(sentence)

def debug_parse(words):
    sentence = ['xxx','xxx','xxx']
    for word in words:
        if word.upper() in tokens[0][1]:
            print(word,' is a direction')
            print('putting it in the sentence')
            if sentence[1] == 'xxx':
                sentence[1] = word.upper()
                print(sentence)
        if word.upper() in tokens[1][1]:
            print(word,' is a verb')
            print('putting it in the sentence')
            if sentence[0] == 'xxx':
                sentence[0] = word.upper()
                print(sentence)
        if word.upper() in tokens[2][1]:
            print(word,' is a noun')
            print('putting it in the sentence')
            if sentence[2] == 'xxx':
                sentence[2] = word.upper()
                print(sentence)
    return(sentence)


from multiprocessing.sharedctypes import SynchronizedString, Synchronized
import time

from utils.util import BUFF_SIZE

def write_to_shared_memory(
    data_shm: SynchronizedString, stat_shm: Synchronized, serd_num_seq: str
) -> int or None:

    # write to shared memory
    while stat_shm.value != 0:
        time.sleep(0.01)
    serd_num_seq_bytes = serd_num_seq.encode()
    l = len(serd_num_seq_bytes)
    c = 0

    while c < l:
        h = min(l, c + BUFF_SIZE)
        while stat_shm.value not in (0, 2):
            time.sleep(0.01)
        data_shm.value = serd_num_seq_bytes[c:h]
        stat_shm.value = 1
        c = h
    while stat_shm.value != 2:
        time.sleep(0.01)
    stat_shm.value = -1

    if serd_num_seq == '"quit"':
        return -1


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 12 10:36:28 2019

@author: pageot
"""

import os
import otbApplication
import argparse

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='SAR_fusion_impose_programme. this programme allows to fusion and cut image')
    parser.add_argument('-out', dest='out')
    parser.add_argument('-inp', dest='path',nargs='+',required = True)
    args = parser.parse_args()
    print (args.path)
    print (args.out)

    list_vv=[]
    list_vh=[]
    for i in  os.listdir("{}".format(str(args.path).strip("['']"))):
        #print (i)
        polar=i[10:12]
        orbite=i[13:16]
        if (polar == "vv" and "s1b_31TCJ_vv" and ".tif" in i and "BorderMask" not in i):
            list_vv.append("{}".format(str(args.path).strip("['']"))+i)
        if (polar == "vh" and "s1b_31TCJ_vv" and ".tif" in i and "BorderMask" not in i):
            list_vh.append("{}".format(str(args.path).strip("['']"))+i)
    print(list_vv)
    list_vv_sorted=[]
    for x in sorted(list_vv):
        list_vv_sorted.append(x)
    list_vh_sorted=[]
    for h in sorted(list_vh):
        list_vh_sorted.append(h)

    for p in ["vv","vh"]:
        if (p == "vv"):
            App2 = otbApplication.Registry.CreateApplication("ConcatenateImages")
            App2.SetParameterStringList("il",list_vv_sorted)
            App2.SetParameterString("out","%sSAR_%s%s.tif"%("{}".format(str(args.out).strip("['']")),'vv',orbite))
            App2.ExecuteAndWriteOutput()
        else:
            App2 = otbApplication.Registry.CreateApplication("ConcatenateImages")
            App2.SetParameterStringList("il",list_vh_sorted)
            App2.SetParameterString("out","%sSAR_%s%s.tif"%("{}".format(str(args.out).strip("['']")),'vh',orbite))
            App2.ExecuteAndWriteOutput()


import serdespy as sdp
import numpy as np
import matplotlib.pyplot as plt
import skrf as rf
import scipy as sp

h = np.load("./data/h_thru.npy")
h_ctle = np.load("./data/h_ctle.npy")
ffe_tap_weights = np.load("./data/rx_ffe_tap_weights.npy")
dfe_tap_weights = np.load("./data/rx_dfe_tap_weights.npy")
tx_fir_tap_weights = np.load("./data/tx_fir_tap_weights.npy")

nyquist_f = 26.56e9

samples_per_symbol = 64

voltage_levels = np.array([-3,-1,1,3])

data = sdp.prqs10(1)

TX = sdp.Transmitter(data, voltage_levels, nyquist_f)

TX.FIR(tx_fir_tap_weights)

TX.oversample(samples_per_symbol)

TX.gaussian_jitter(stdev_div_UI=0.025)

TX.tx_bandwidth(freq_bw=120e9)

sdp.simple_eye(TX.signal, samples_per_symbol*3, 1000, TX.UI/TX.samples_per_symbol, "TX Bandwidth-Limited Eye Diagram (-3dB frequency at 100GHz)")
#%%
signal_out = sp.signal.fftconvolve(TX.signal, h, mode = "same")

#xtalk = np.load("./data/xt_response.npy")[:384000]

#signal_out_ctle = sp.signal.fftconvolve(signal+xtalk, h_ctle, mode = "same")
signal_out_ctle = sp.signal.fftconvolve(signal_out, h_ctle, mode = "same")
#%%
sdp.simple_eye(signal_out_ctle, samples_per_symbol*3, 1000, TX.UI/TX.samples_per_symbol, "Eye Diagram with CTLE")

RX = sdp.Receiver(signal_out_ctle, samples_per_symbol, nyquist_f, voltage_levels, shift = True, main_cursor = 0.5006377086200736)

RX.slice_signal()
#%%
RX.signal_BR =  RX.signal_BR + np.random.normal(scale=0.1, size = RX.signal_BR.size)

sdp.simple_eye(RX.signal_BR, 3, 1000, TX.UI, "Eye Diagram with CTLE and noise BR")
#%%
RX.FFE_BR(ffe_tap_weights, 3)

sdp.simple_eye(RX.signal_BR, 3, 800, TX.UI/TX.samples_per_symbol, "Eye Diagram after FFE")
#%%
RX.pam4_DFE_BR(dfe_tap_weights)

sdp.simple_eye(RX.signal_BR, 3, 800, TX.UI/TX.samples_per_symbol, "Eye Diagram after DFE")

#%%
err = sdp.prqs_checker(10, data, RX.symbols_out[10:-10])

print("Bits Transmitted =", RX.symbols_out[10:-10].size*2, 'Bit Errors =', err[0])

print("Bit Error Ratio = ", err[0]/(RX.symbols_out[10:-10].size*2))


import unittest
from utils import *

class TestUtils(unittest.TestCase):
    def test_calories(self):
        cal = get_calories(88, 183, 29, "male", "very_active", "loose_weight")
        self.assertAlmostEqual(cal, 2599.575)

    def test_macros(self):
        prot, fat, carbs = get_macros(88, 183, 29, "male", "very_active", "maintain_weight")
        self.assertAlmostEqual(prot, 193.6)
        self.assertAlmostEqual(fat, 79.43, 2)
        self.assertAlmostEqual(carbs, 440.046, 2)

    def test_clean(self):
        cleaned_ingre1 = clean_ingredient("1/2 stick (3/4 Cup) Cold Butter")
        cleaned_ingre2 = clean_ingredient("1 pound Breakfast Sausage")
        cleaned_ingre3 = clean_ingredient("1/4 cup Mayonnaise")
        self.assertEqual(cleaned_ingre1, "butter")
        self.assertEqual(cleaned_ingre2, "breakfast sausage")
        self.assertEqual(cleaned_ingre3, "mayonnaise")

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


from pathlib import Path
import pandas as pd
import pytest

@pytest.fixture(scope="function")
def imgfile():
    dirpath = Path("input")
    dirpath.mkdir(exist_ok=True)
    filepath = dirpath.joinpath("test.jpg")
    yield str(filepath)
    filepath.unlink(missing_ok=True)

@pytest.fixture(scope="session")
def c_df():
    GITHUB_URL = "https://raw.githubusercontent.com"
    URL_C = f"{GITHUB_URL}/lisphilar/covid19-sir/master/data/japan/covid_jpn_total.csv"
    path = Path("data", "japan", "covid_jpn_total.csv")
    filepath = path if path.exists() else URL_C
    df = pd.read_csv(filepath, dayfirst=False).rename(columns={"Date": "date"})
    df = df[["date", "Positive", "Tested", "Discharged", "Fatal"]].groupby("date", as_index=False).sum()
    df.insert(0, "Country", "Japan")
    return df

@pytest.fixture(scope="session")
def p_df():
    GITHUB_URL = "https://raw.githubusercontent.com"
    URL_P = f"{GITHUB_URL}/lisphilar/covid19-sir/master/data/japan/covid_jpn_prefecture.csv"
    path = Path("data", "japan", "covid_jpn_prefecture.csv")
    filepath = path if path.exists() else URL_P
    df = pd.read_csv(filepath, dayfirst=False).rename(columns={"Date": "date"})
    df.insert(0, "Country", "Japan")
    return df[["Country", "Prefecture", "date", "Positive", "Tested", "Discharged", "Fatal"]]

@pytest.fixture(scope="session")
def japan_df(c_df, p_df):
    df = pd.concat([c_df, p_df], axis=0)
    assert set(df.columns) == {"Country", "Prefecture", "date", "Positive", "Tested", "Discharged", "Fatal"}
    return df


import sys
input = sys.stdin.readline

n, m = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
k = int(input())

d = [[0] * (m + 1) for _ in range(n + 1)]

# 누적합 구하기
for i in range(1, n + 1):
    for j in range(1, m + 1):
        d[i][j] = board[i - 1][j - 1] + d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]

# 해당 범위의 총 사람 수 구하기
for _ in range(k):
    x1, y1, x2, y2 = map(int, input().split())
    # 마지막 좌표까지의 누적합에서 한 번 겹치는 부분을 빼고 두 번 겹치는 부분은 더함
    # (1, 1) ~ (2, 3)까지의 범위를 구한다고 하면 시작 행과 열에서 -1을 해준 행과 열을 빼주고 겹치는 부분은 더해주어야 한다.
    # Why? (0, 0) ~ (n - 1, m - 1) 범위까지 이어져오면서 누적합을 구하기 때문에 위 연산을 하지 않는 경우 두 번 더해지는 경우가 생기기 때문에
    answer = d[x2][y2] - d[x1 - 1][y2] - d[x2][y1 - 1] + d[x1 - 1][y1 - 1]
    print(answer)


N = int(input())  # 과목의 개수
grade_lst = list(map(int, input().split()))  # 과목들의 성적을 저장하는 리스트

aver = 0  # 성적의 평균
for i in range(N):
    aver += grade_lst[i]
aver = aver / N

new_aver = aver / max(grade_lst) * 100  # 새로운 평균
print(new_aver)

[백준] 단계별로풀어보기/04 1차원 배열/1546.py


from . import InstructionSet, byte, long, float, signedLong

exodeInstructionSet = InstructionSet(name="Exode")

instructions = [
    [0,  "pinMode",                byte*3],
    [1,  "digitalWrite",           byte*2],
    [2,  "digitalRead",            byte*2],
    [3,  "digitalSwitch",          byte],
    [4,  "analogWrite",            byte*2],
    [5,  "analogRead",             byte*2],
    [6,  "addServo",               byte*2 + long],
    [7,  "removeServo",            byte],
    [8,  "writeServo",             byte + long],
    [9,  "pulse",                  byte + long],
    [10, "pulseIn",                byte*2],
    [14, "checkExode",             []],
    [15, "addStepper",             byte*6],
    [16, "setStepperAcceleration", byte + float],
    [17, "setStepperSpeed",        byte + float],
    [18, "moveStepper",            byte + signedLong]
]

for inst in instructions:
    id = inst[0]
    name = inst[1]
    types = inst[2]
    exodeInstructionSet.setInstruction(id, name, types)


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

import sys
import string

def main(lista, length):
    wordsList = lista.translate(
        str.maketrans('', '', string.punctuation)
        ).split()
    max = None
    if length.isdigit():
        max = int(length)
    if len(wordsList) > 0 and max is not None:
        newList = [word for word in wordsList if len(word) > max]
        print(newList)
    else:
        print('\033[1;31mError\033[0;0m')

if __name__ == "__main__":
    if len(sys.argv) == 3 and not sys.argv[1].isdigit():
        main(sys.argv[1], sys.argv[2])
    else:
        print('\033[1;31mError\033[0;0m')


from malduck import *

data = open("2.bin", "rb").read()
packets = data.split(b"PA30")[1:]

for i in range(len(packets)):
    p = b"PA30" + packets[i].split(b"ME0W")[0]
    if i % 2 == 0:
        name = str(i) + "_client"
    else:
        name = str(i) + "_server"
    open("./2/patches/" + name, "wb").write(p)


import pymongo
import redis

class RedisBackend:

	__newUrlKey = 'newUrl'
	__crawledUrlKey = 'CrawledUrl'

	__port = 6379
	__hostName = '54.183.40.123'

	redisOperator = redis.Redis(host=__hostName,port=__port)
	pub = redisOperator.pubsub()

	def __init__(self):
		pass

	# this function only serve for spider
	def pushUrls(self,urls):
		# urls: list of url
		for url in urls:
			# self.redisOperator.sadd(self.__newUrlKey,url)
			self.redisOperator.publish(self.__newUrlKey,url)

	# this function only seve for master
	def pullUrls(self):

		# pipeline as multi, transaction = false
		pipe = self.redisOperator.pipeline(transaction=False)
		pipe.smembers(self.__newUrlKey)
		pipe.delete(self.__newUrlKey)
		urls = pipe.execute()[0]
		urls = list(map(lambda u: u.decode('utf-8'),urls))
		return urls

class MongoBackend:

	def __init__(self):
		self.myClient = pymongo.MongoClient('mongodb://54.193.51.217')

	# insert one json object in USC collection
	def insertUSC(self, content):
		crawlerDB = self.myClient['crawler']
		usc = crawlerDB['USC']
		usc.insert_one(content)


#-------------------------------------------------------------------------
# AUTHOR: Christopher Tomes
# FILENAME: decision_tree.py
# SPECIFICATION: This code implements a decision tree classifier using scikit-learn's decision tree classifier on "contact_lens.csv". The categorical data in the file is mapped to integers, fit into the model, then plotted using matplotlib.
# FOR: CS 4210- Assignment #1
# TIME SPENT: the coding portion for this assignment was around 1 hour.
#-----------------------------------------------------------*/

#IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to work here only with standard
# dictionaries, lists, and arrays

#importing some Python libraries
from sklearn import tree
import matplotlib.pyplot as plt
import csv

#MacThings
import os
import platform
db = []
X = []
Y = []

#MacThings:
##################################################
#small change to alow me to have it work on my Mac
#also replaced name of csv with the var string_path
if platform.system() == 'Darwin':
  string_path = os.getcwd() + '/contact_lens.csv'
else:
  string_path = 'contact_lens.csv'
###################################################

#reading the data in a csv file
with open(string_path, 'r') as csvfile:
  reader = csv.reader(csvfile)
  for i, row in enumerate(reader):
      if i > 0: #skipping the header
         db.append (row)
         #print(row)

#transform the original categorical training features into numbers and add to the 4D array X. For instance Young = 1, Prepresbyopic = 2, Presbyopic = 3
# so X = [[1, 1, 1, 1], [2, 2, 2, 2], ...]]
#--> add your Python code here

# Create a dictionary to store map from unique elements to integers
element_to_int = {}
int_to_element = {}

# Iterate over the columns
for column in zip(*db):
    # Get the unique elements
    unique_elements = set(column)

    # Iterate over the unique elements
    for element in unique_elements:
        if element not in element_to_int:
            element_to_int[element] = len(element_to_int)
            int_to_element[len(int_to_element)] = element

# Create a new 2D array to store the mapped values
mapped_arr = []

# Iterate over the rows of to convert via new vals
for row in db:
    mapped_row = []

    # Iterate over the elements in a row
    for element in row:
        # Replace each element with new corresponding integer value we got earlier
        mapped_element = element_to_int[element]
        mapped_row.append(mapped_element)

    # Add the mapped row to the mapped array
    mapped_arr.append(mapped_row)

#print(mapped_array)

# X =
#transform the original categorical training classes into numbers and add to the vector Y. For instance Yes = 1, No = 2, so Y = [1, 1, 2, 2, ...]
#--> add your Python code here
# Y =

#combined steps

# Iterate over the rows in the mapped array to assign into your defined data structues X/Y
for row in mapped_arr:
    # Extract the vals
    values = row[:-1]
    last_value = row[-1]
    # Add the vals to the first n-1 columns to
    X.append(values)
    # Add last column to Y
    Y.append(last_value)

#print(X)
#print(Y)

#fitting the decision tree to the data
clf = tree.DecisionTreeClassifier(criterion = 'entropy')
clf = clf.fit(X, Y)

#plotting the decision tree
tree.plot_tree(clf, feature_names=['Age', 'Spectacle', 'Astigmatism', 'Tear'], class_names=['Yes','No'], filled=True, rounded=True)
plt.show()


#Q1

import numpy as np
np.lcm(4,5)

def test1(a,b):
    big=max(a,b)
    while True:
        if big%a==0 and big%b==0:
            return big
        else:
            big+=1

test1(10,20)

#Q2

np.gcd(4,5)

def test2(a,b):
    small=min(a,b)
    for i in range(1,small+1):
        if a%i==0 and b%i==0:
            hcf=i
    return hcf

test2(10,20)

#Q3

def test3(num):
    b=bin(num)
    h=hex(num)
    o=oct(num)
    return(b,h,o)

test3(17)

#Q4

def test4(c):
    return ord(c)

test4('|')

#Q5

def cal(a,b,op):
    if op=='+':
        return a+b
    elif op=='-':
        return a-b
    elif op=='*':
        return a*b
    elif op=='/':
        return a/b
    else:
        return "Invalid operation"

while True:
    a=int(input("Enter 2 numbers followed by operation\nGive operation . to exit\n"))
    b=int(input())
    c=input()
    if c=='.':
        print("Thank You...")
        break
    print("{} {} {} = {}".format(a,c,b,cal(a,b,c)))


import numpy as np
import sklearn.svm

def dataset3Params(X, y, Xval, yval):
    """returns your choice of C and sigma. You should complete
    this function to return the optimal C and sigma based on a
    cross-validation set.
    """

# You need to return the following variables correctly.
    C = 1
    sigma = 0.3

# ====================== YOUR CODE HERE ======================
# Instructions: Fill in this function to return the optimal C and sigma
#               learning parameters found using the cross validation set.
#               You can use svmPredict to predict the labels on the cross
#               validation set. For example,
#                   predictions = svmPredict(model, Xval)
#               will return the predictions on the cross validation set.
#
#  Note: You can compute the prediction error using
#        mean(double(predictions ~= yval))
#
    C_vec = np.array([0.01, 0.03, 0.1, 0.3, 1.0, 3.0, 10.0, 30.0])
    sig_vec = np.array([0.01, 0.03, 0.1, 0.3, 1.0, 3.0, 10.0, 30.0])
    lenth = len(C_vec)
    pred_mat = np.ones((lenth,lenth))
    for i in range(lenth):
        for j in range(lenth):
            gamij = 1.0 / (2.0 * sig_vec[j] ** 2)
            clf = sklearn.svm.SVC(C=C_vec[i], kernel='rbf', tol=1e-3, max_iter=200, gamma=gamij)
            model = clf.fit(X,y)
            predictions = model.predict(Xval)
            pred_mat[i,j] = np.mean(np.double(predictions != yval))
    min_mat = np.argwhere(pred_mat == np.min(pred_mat))
    C_i = min_mat[0,0]
    sig_j = min_mat[0,1]
    C = C_vec[C_i]
    sigma = sig_vec[sig_j]

# =========================================================================
    return C, sigma


#!/usr/bin/python3

from . import icons, base
import os
import logging
import math

_here = os.path.dirname(__file__)

_cond_icons = icons.IconSet(os.path.join(_here,"weather-conditions.ppm"))
_cond_names = {
	"moon_night":0,
	"sun":1,
	"partial_cloud_night":2,
	"partial_cloud":3,
	"dark_night":4,
	"mist":5,
	"fog":6,
	"cloud":7,
	"dark_cloud":8,
	"rain_1":9,
	"showers":10,
	"drizzle":11,
	"rain":12,
	"heavy_rain":13,
	"heavy_showers":14,
	"heavy_rain_1":15,
	"sleet":16,
	"sleet_showers":17,
	"sleet_1":18,
	"hail":19,
	"hail_1":20,
	"hail_2":21,
	"snow":22,
	"snow_showers":23,
	"snow_1":24,
	"heavy_snow":25,
	"heavy_snow_showers":26,
	"heavy_snow_1":27,
	"lightning":28,
	"lightning_1":29,
	"lightning_2":30,
	"unknown":31
}

temp_banner = _cond_icons.get(7, 3, 8)

def temperature(t, cold=0, warm=15, hot=35):
    # "warm" means what will get a green number.
    # There's more perceptual distance between red and green
    # than green and blue, so "warm" should be on the cold side

    if t is None:
        return base.error("weather.temperature: expected number, got None")

    try:
        if t > warm:
            return base.number(t, icons.GREEN, warm, icons.RED, hot)
        else:
            return base.number(t, icons.BLUE, cold, icons.GREEN, warm)
    except ValueError as e: # not a number?
        return base.error("weather.temperature: %s" % e)

def conditions(c):
    if isinstance(c, str):
        if c in _cond_names:
            c = _cond_names[c]
        else:
            return base.error("weather.conditions: no icon for '%s'" % c)

    try:
        c = math.floor(c)
    except Exception as e:
        return base.error("weather.conditions: %s" % e)

    if c is None or c < 0 or c > 30:
        return base.error("weather.conditions: %d out of range" % c)
    else:
        return _cond_icons.get(c%8, c//8, 8)


import ConfigParser
import os

import music21

def time_signature_equality(a, b):
    return a.numerator == b.numerator and a.denominator == b.denominator

def rhythms(song_path, sig):
    """Traverse the file whose handle is supplied and return a collection of
    usable rhythms.

    This method will look at all the rhythms that are in the piece, so not just
    the guitar voice. It will discard ghost notes. The result
    may contain duplicates, which will need to be filtered out later on."""
    if sig == '128':
        sig = music21.meter.TimeSignature('12/8')
    elif sig == 'common':
        sig = music21.meter.TimeSignature('4/4')
    else:
        sig = None
    assert sig is not None
    discovered_rhythms = []
    song_score = music21.converter.parse(song_path)
    for part in song_score.getElementsByClass(music21.stream.Part):
        for measure in part.getElementsByClass(music21.stream.Measure):
            measure_replacement = music21.stream.Measure()
            measure_replacement.timeSignature = sig
            for index in range(0, len(measure)):
                if(isinstance(measure[index], music21.note.Note) or
                   isinstance(measure[index], music21.chord.Chord)):
                    sound_replacement = music21.note.Note('a4')
                    sound_replacement.duration = measure[index].duration
                    measure_replacement.append(sound_replacement)
                    measure_contains_notes = True
                else:
                    measure_replacement.append(measure[index])
            measure_replacement.offset = 0
            discovered_rhythms.append(measure_replacement)
    return discovered_rhythms

def time_signature(path):
    """Return the time signature of a piece.

    The known types of time signature are "common", "128" and "unknown".
    If a piece contains measures with different signatures, the result is
    "unknown"."""
    song_score = music21.converter.parse(path)
    # note: __equals__ does not behave as expected for TimeSignature!
    # use time_signature_equality instead.
    common_time = music21.meter.TimeSignature('4/4')
    twelve_eighths = music21.meter.TimeSignature('12/8')
    guess = None
    part = song_score.getElementsByClass(music21.stream.Part)[0]
    for measure in part.getElementsByClass(music21.stream.Measure):
        if measure.timeSignature:
            if time_signature_equality(measure.timeSignature, common_time):
                if(guess is None or time_signature_equality(measure.timeSignature, guess)):
                    guess = common_time
                else:
                    return 'unknown'
            elif time_signature_equality(measure.timeSignature, twelve_eighths):
                if(guess is None or time_signature_equality(measure.timeSignature, guess)):
                    guess = twelve_eighths
                else:
                    return 'unknown'
        else:
            continue # if the signature is unchanged, no problem
    if guess is common_time:
        return 'common'
    elif guess is twelve_eighths:
        return '128'
    else:
        return 'unknown'

def create_measure_files(in_folder, out_folder_root):
    r"""
    Given the path the source MusicXML files as `in_folder` and the desired
    output location for subfolders containing extracted rhythms organized
    per time signature as `out_folder_root`, create a MusicXML file for each
    meaningful rhythm.

    **This function does not eliminate duplicate rhythms and it does not leave
    out rhythms played by non-guitar instruments, nor does it ignore
    whole-note rests.** Such postprocessing is handled by consumer functions.
    """
    for file_entry in enumerate(os.listdir(in_folder)):
        source_path = in_folder + os.sep + file_entry[1]
        sig = time_signature(source_path) # common, 128 or unknown
        if sig == 'unknown':
            continue
        out_folder = out_folder_root + os.sep + sig
        sep = os.sep
        file_num = file_entry[0]
        for entry in enumerate(rhythms(source_path, sig)):
            rhythm_num = entry[0]
            out_path = '{out_folder}{sep}{file_num}_{rhythm_num}.xml'
            entry[1].write(fmt='musicxml', fp=out_path.format(**locals()))

if __name__ == '__main__':
    config = ConfigParser.ConfigParser()
    here = os.path.dirname(os.path.abspath(__file__))
    param_path = os.sep.join((here, 'params.ini'))
    with open(param_path) as param_fh:
        config.readfp(param_fh)
    musicxml_dir = config.get('Analysis', 'musicxml_dir')
    rhythm_measures_dir = config.get('Analysis',
                                     'intermediate_rhythm_measures_dir')
    create_measure_files(musicxml_dir, rhythm_measures_dir)


import re
from dataclasses import dataclass, field
from typing import List

@dataclass
class Region:
    x_min: int
    x_max: int
    y_min: int
    y_max: int
    z_min: int
    z_max: int
    holes: List['Region'] = field(default_factory=list)
    on: bool = False

    def count_on(self):
        if self.on:
            volume = (self.x_max + 1 - self.x_min) * (self.y_max + 1 - self.y_min) * (self.z_max + 1 - self.z_min)
            for region in self.holes:
                if not region.on:
                    volume -= region.count_on()
            return volume
        volume = 0
        for region in self.holes:
            if region.on:
                volume += region.count_on()
        return volume

    def is_within_region(self, other: 'Region'):
        within = (
            other.x_min <= self.x_min <= self.x_max <= other.x_max
        and other.y_min <= self.y_min <= self.y_max <= other.y_max
        and other.z_min <= self.z_min <= self.z_max <= other.z_max
        )
        if within:
            return True
        return False

    def get_common_region(self, other: 'Region'):
        if self.x_max < other.x_min or self.x_min > other.x_max:
            return None
        if self.y_max < other.y_min or self.y_min > other.y_max:
            return None
        if self.z_max < other.z_min or self.z_min > other.z_max:
            return None

        x_min = max(self.x_min, other.x_min)
        x_max = min(self.x_max, other.x_max)
        y_min = max(self.y_min, other.y_min)
        y_max = min(self.y_max, other.y_max)
        z_min = max(self.z_min, other.z_min)
        z_max = min(self.z_max, other.z_max)
        return Region(
            x_min=x_min,
            x_max=x_max,
            y_min=y_min,
            y_max=y_max,
            z_min=z_min,
            z_max=z_max,
            on=other.on
        )

@dataclass
class Command:
    action: str
    region: Region

def get_commands(lines):
    command_pattern = r"(on|off) x=(-?[0-9]+)..(-?[0-9]+),y=(-?[0-9]+)..(-?[0-9]+),z=(-?[0-9]+)..(-?[0-9]+)"
    commands = []
    for line in lines:
        command_search = re.search(command_pattern, line)
        if not command_search:
            raise ValueError(f"Command {line} is not a valid command.")
        action = command_search.group(1)
        x_min = int(command_search.group(2))
        x_max = int(command_search.group(3))
        y_min = int(command_search.group(4))
        y_max = int(command_search.group(5))
        z_min = int(command_search.group(6))
        z_max = int(command_search.group(7))
        region = Region(
            x_min=x_min,
            x_max=x_max,
            y_min=y_min,
            y_max=y_max,
            z_min=z_min,
            z_max=z_max,
            on = True if action == "on" else False
        )
        command = Command(
            action=action,
            region=region
        )
        commands.append(command)
    return commands

def execute_commands(commands, initial_region: Region):
    regions = [initial_region]
    for command in commands:
        new_regions = []
        for region in regions:
            if command.region.is_within_region(region) and command.region.on == region.on:
                continue  # skip steps which won't change the status of any cubes
            if any(command.region.is_within_region(hole) for hole in region.holes):
                continue
            new_region = region.get_common_region(command.region)
            if not new_region:
                continue
            region.holes.append(new_region)
            new_regions.append(new_region)
        regions.extend(new_regions)
    return regions

def main():
    with open("test.txt") as f:
        lines = [l.replace("\n", "") for l in f.readlines()]
    commands = get_commands(lines)
    maximum_area = 50
    initial_region = Region(
        x_min=-maximum_area,
        x_max=maximum_area,
        y_min=-maximum_area,
        y_max=maximum_area,
        z_min=-maximum_area,
        z_max=maximum_area,
        on=False
    )
    regions = execute_commands(commands, initial_region)
    print(initial_region.count_on())

if __name__ == "__main__":
    main()


from django.urls import path
from .import views

urlpatterns = [
    path('', views.home, name='home'),
    path('trending/', views.trending, name='trending'),
    path('search/', views.search, name='search'),
    path('product_details/<slug>', views.details, name='product_details'),
    path('about/', views.about, name='about')
    # path('productBycat/', views.productBycat, name='productBycat'),
]


from django import forms
from django.contrib.auth.models import User
from profiles.models import *
from projects.models import Project, Upload

class ProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        self.fields['pic'].label = 'Picture'

    class Meta:
        model = Academic
        fields = ('user','affiliation', 'pic', 'bio', 'url', 'linkedIn', 'twitter')

class ProjectForm(forms.ModelForm):

    class Meta:
        model = Project
        fields = ('title', 'description', 'supervisor1', 'supervisor2')

class AddPortfolioForm(forms.ModelForm):

    class Meta:
        model = Upload
        fields = ('type', 'label', 'upload', 'URL', 'public')

class EditPortfolioForm(forms.ModelForm):

    class Meta:
        model = Upload
        fields = ('type', 'label', 'upload', 'URL', 'public')