import torch
import torch.nn as nn
import torch.nn.functional as F

import math

# Block with stage means that the block is a down-sample block, where hierarchical connections are removed. There are only 3 blocks in a net that is stage block.

# depreacated 当Bottle2neck_w 的 base_width = 16 时， 等价于Bottle2neck
class Bottle2neck(nn.Module):
    expansion = 4
    def __init__(self, inplanes, planes, stride=1, downsample=None, scale=4, stype='normal'):
        """
        inplanes： 前面 1x1 卷积的输入的通道数
        planes： bottleneck 3x3 卷积的输入输出通道数
        后面1x1卷积的输出通道数： planes * self.expansion

        n: planes
        s: scale
        w: w * s = n, width = planes / scale

        """
        super(Bottle2neck, self).__init__()

        width = int(math.floor(planes / scale))
        # print(width)

        self.conv1 = nn.Conv2d(inplanes, width * scale, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(width * scale)

        if stype == "stage":
            self.pool = nn.AvgPool2d(kernel_size=3, stride = stride, padding = 1)

        convs = []
        bns = []
        for i in range(scale-1):
            convs.append(nn.Conv2d(width, width, kernel_size=3, stride=stride, padding=1, bias=False))
            bns.append(nn.BatchNorm2d(width))
        self.convs = nn.ModuleList(convs)
        self.bns = nn.ModuleList(bns)

        self.conv3 = nn.Conv2d(width * scale, planes * self.expansion, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * self.expansion)

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stype = stype
        self.scale = scale
        self.width = width

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        # sp 指 split， 将out在第一个维度按照width大小拆分
        spx = torch.split(out, self.width, 1)

        # 如果scale=4， 那么i = 0, 1, 2
        for i in range(self.scale-1):
            # 如果是stage模式， scale的connection就没了
            if i==0 or self.stype == 'stage':
                sp = spx[i]
                # print(sp.shape)
            else:
                sp = sp + spx[i]
            sp = self.convs[i](sp)
            sp = self.relu(self.bns[i](sp))

            if i ==0:
                out = sp
            else:
                out = torch.cat((out, sp), 1)

        if self.scale != 1 and self.stype=='normal':
            out = torch.cat((out, spx[self.scale-1]), 1)
        elif self.scale != 1 and self.stype == 'stage':
            # b = self.pool(spx[self.scale-1])
            # print(spx[self.scale-1].shape)
            # print("b_shape: ", b.shape)
            out = torch.cat((out, self.pool(spx[self.scale-1])), 1)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Bottle2neck_w(nn.Module):
    expansion = 4
    def __init__(self, inplanes, planes, stride=1, downsample=None, base_width = 26, scale=4, stype='normal'):
        """
        inplanes： 前面 1x1 卷积的输入的通道数
        planes： bottleneck 3x3 卷积的输入输出通道数
        后面1x1卷积的输出通道数： planes * self.expansion

        n: planes
        s: scale
        w: w * s = n, width = planes / scale

        """
        super(Bottle2neck_w, self).__init__()

        # As long as the model structure and the channel number of each split is the same, there is no difference between different code style, and there should be no difference in performance.
        # Width/baseWidth is just used to control the channel number in each split. We just follow the previous works such as Res2NeXt to use this code style.
        width = int(math.floor((planes * base_width/64)))
        # print(width)

        self.conv1 = nn.Conv2d(inplanes, width * scale, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(width * scale)

        if stype == "stage":
            self.pool = nn.AvgPool2d(kernel_size=3, stride = stride, padding = 1)

        convs = []
        bns = []
        for i in range(scale-1):
            convs.append(nn.Conv2d(width, width, kernel_size=3, stride=stride, padding=1, bias=False))
            bns.append(nn.BatchNorm2d(width))
        self.convs = nn.ModuleList(convs)
        self.bns = nn.ModuleList(bns)

        self.conv3 = nn.Conv2d(width * scale, planes * self.expansion, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * self.expansion)

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stype = stype
        self.scale = scale
        self.width = width

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        # sp 指 split， 将out在第一个维度按照width大小拆分
        spx = torch.split(out, self.width, 1)

        # 如果scale=4， 那么i = 0, 1, 2
        for i in range(self.scale-1):
            # 如果是stage模式， scale的connection就没了
            if i==0 or self.stype == 'stage':
                sp = spx[i]
                # print(sp.shape)
            else:
                sp = sp + spx[i]
            sp = self.convs[i](sp)
            sp = self.relu(self.bns[i](sp))

            if i ==0:
                out = sp
            else:
                out = torch.cat((out, sp), 1)

        if self.scale != 1 and self.stype=='normal':
            out = torch.cat((out, spx[self.scale-1]), 1)
        elif self.scale != 1 and self.stype == 'stage':
            # b = self.pool(spx[self.scale-1])
            # print(spx[self.scale-1].shape)
            # print("b_shape: ", b.shape)
            out = torch.cat((out, self.pool(spx[self.scale-1])), 1)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Res2Net(nn.Module):

    def __init__(self, block, layers,  base_width = 26, scale = 4, num_classes = 1000):
        self.inplanes = 64
        super(Res2Net, self).__init__()
        self.base_width = base_width
        self.scale = scale

        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)

        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    # blocks -> layers
    # stride 只在 residual 的 downsample 1x1 卷积 和 bottleneck 的 3x3 卷积中起作用
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion)
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample=downsample, stype='stage', base_width = self.base_width, scale=self.scale))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, base_width = self.base_width,scale=self.scale))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x

def res2net50(num_classes=1000):
    model = Res2Net(Bottle2neck, [3, 4, 6, 3], scale = 4, num_classes = num_classes)
    return model

def res2net50_w(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], scale = 4, num_classes = num_classes)
    return model

def res2net50_26w_6s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 26, scale = 6, num_classes = num_classes)
    return model

def res2net50_26w_8s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 26, scale = 8, num_classes = num_classes)
    return model

def res2net50_48w_2s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 48, scale = 2, num_classes = num_classes)
    return model

def res2net50_26w_4s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 26, scale = 4, num_classes = num_classes)
    return model

def res2net50_14w_8s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 14, scale = 8, num_classes = num_classes)
    return model

def res2net50_16w_4s(num_classes=1000):
    model = Res2Net(Bottle2neck_w, [3, 4, 6, 3], base_width = 16, scale = 4, num_classes = num_classes)
    return model

def get_model(model_name, num_classes=1000):
    if model_name == "res2net50":
        return res2net50(num_classes)

    if model_name == "res2net50_w":
        return res2net50_w(num_classes)

    if model_name == "res2net50_26w_6s":
        return res2net50_26w_6s(num_classes)

    if model_name == "res2net50_26w_6s_dev":
        return res2net50_26w_6s(num_classes)

    if model_name == "res2net50_26w_8s":
        return res2net50_26w_8s(num_classes)

    if model_name == "res2net50_26w_8s_dev":
        return res2net50_26w_8s(num_classes)

    if model_name == "res2net50_48w_2s":
        return res2net50_48w_2s(num_classes)

    if model_name == "res2net50_26w_4s":
        return res2net50_26w_4s(num_classes)

    if model_name == "res2net50_14w_8s":
        return res2net50_14w_8s(num_classes)

if __name__ == "__main__":
    downsample = nn.Sequential(
                nn.Conv2d(64, 128,
                          kernel_size=1, stride=2, bias=False),
                nn.BatchNorm2d(128),
            )
    block = Bottle2neck_w(64, 64, stride=1, downsample=downsample, scale=4, stype='stage')
    a = torch.ones((1, 64, 32, 32))
    block(a)


import pytest
from process_reports import RPCArgParse
import os
from argparse import ArgumentTypeError
import datetime

bucket_fn_data = [
    (os.path.join('Sample_Reports', '2_1_18Bucket_Scrubbed.xls'), True),
    (os.path.join('Sample_Reports', '2_1_18Bucket_BAD.xls'), False),
]

rpc_fn_data = [
    (os.path.join('Sample_Reports', 'ALL_RPC-2-1-2018_Scrubbed.xlsx'), True),
    (os.path.join('Sample_Reports', 'ALL_RPC-2-1-2018_ScrubbedBAD.xlsx'), False),
]

@pytest.fixture
def rpcargparse():
    '''Returns a typical RPCArgParse'''
    return RPCArgParse()

@pytest.fixture
def descrip_RPCArgParse():
    '''Returns a typical RPCArgParse'''
    return RPCArgParse(description='Alternate descrip')

@pytest.fixture
def typical_args():
    return ['-r', rpc_fn_data[0][0], '-b', bucket_fn_data[0][0], '-d' , 'Sample_Reports']

def test_RPCArgParse_descrip1(rpcargparse):
    assert rpcargparse.description == 'Read in the main files to create RPC summary'

def test_RPCArgParse_descrip2(descrip_RPCArgParse):
    assert descrip_RPCArgParse.description == 'Alternate descrip'

@pytest.mark.parametrize("fn,is_file", bucket_fn_data)
def test_is_valid_file(fn, is_file, rpcargparse):
    '''
    Test the is_valid_file with the paraemtrized inputs.
    Should raise an error for a bad file and nothing for a good file.
    '''
    if not is_file:
        with pytest.raises(ArgumentTypeError):
            rpcargparse.is_valid_file(fn)

@pytest.mark.xpass(strict=True)
def test_good_bucket(rpcargparse):
    '''
    We expect this to pass using xpass with a good filename
    https://docs.pytest.org/en/documentation-restructure/how-to/skipping.html
    '''
    rpcargparse.parse_args(['-r', rpc_fn_data[0][0], '-b', bucket_fn_data[0][0], '-d', 'tests'])

@pytest.mark.parametrize('parse', [
    ['-b', bucket_fn_data[1][0]],
    ['-o'],
    ['-r', rpc_fn_data[0][0], '-b', bucket_fn_data[0][0], '-d', 'stupid_bad']
    ])
def test_bad_arg(parse, rpcargparse):
    '''
    These are tests we expect to fail, bad file, bad syntax etc.
    https://medium.com/python-pandemonium/testing-sys-exit-with-pytest-10c6e5f7726f
    '''
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        rpcargparse.parse_args(parse)

    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 2

@pytest.mark.parametrize("parse,key,expected", [
    (None, 'output_fn', '%s' % datetime.date.today().strftime('%Y_%m_%d')),
    (['-o', 'cool'], 'output_fn', 'cool'),
    (['-o', ''], 'output_fn', ''),
])
def test_output_default_val(parse, key, expected, rpcargparse, typical_args):
    if parse:
        args = rpcargparse.parse_args(typical_args + parse)
    else:
        args = rpcargparse.parse_args(typical_args)
    assert vars(args).get(key) == expected


from cx_Freeze import setup, Executable

base = None

executables = [Executable("MasterLauncher.py", base=base)]

packages = ["idna", "os", "PyQt5", "win32gui", "time", "atexit", "webbrowser",
            "atexit", "pycurl", "sys", "win32process", "urllib"]

incl_files = ["happy.png", "whale.jpg"]

options = {
    'build_exe': {
        'packages': packages,
        'include_files': incl_files
    },
}

setup(
    name="Focus Task",
    options=options,
    version="0.1",
    description='A productivity application that works by limiting multitasking to reduce switching costs',
    executables=executables
)


# 434. 字符串中的单词数

class Solution(object):
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        if s is None or len(s) == 0:
            return 0

        s = s.strip()

        words = s.split()

        return len(words)


self.description = "Install a set of three packages"

p1 = pmpkg("pkg1")
p1.files = ["bin/pkg1",
            "usr/man/man1/pkg1.1"]

p2 = pmpkg("pkg2", "2.0-1")
p2.files = ["bin/pkg2",
            "usr/man/man1/pkg2.1"]

p3 = pmpkg("pkg3", "3.0-1")
p3.files = ["bin/pkg3", "usr/man/man1/pkg3.1"]

for p in p1, p2, p3:
	self.addpkg(p)

self.args = "-U %s" % " ".join([p.filename() for p in (p1, p2, p3)])

self.addrule("PACMAN_RETCODE=0")
for p in p1, p2, p3:
	self.addrule("PKG_EXIST=%s" % p.name)
	for f in p.files:
		self.addrule("FILE_EXIST=%s" % f)


import sys
input = sys.stdin.readline

TEST_CASE = 1 # 테스트 케이스 수

def solution():
    N = int(input().rstrip())
    weights = list(map(int, input().split()))
    weights.sort()
    answer = 0
    for weight in weights:
        if answer+1 >= weight:
            answer += weight
        else:
            break
    print(answer+1)

for _ in range(TEST_CASE):
    solution()


# Consider an array of non-negative integers.  A second array is formed by shuffling the elements of the first and
# deleting a random element. Give these two arrays, find which element is missing in the second array.

# Ex. input:  finder([1,2,3,4,5,6,7], [3,7,2,1,4,6])
              # 5 is missing.

def finder(array1, array2):
    # if len(array1) - len(array2) != 1:
    #     return -1

    tracker = {}

    for num in array1:
        if num in tracker:
            tracker[num] += 1
        else:
            tracker[num] = 1

    for num in array2:
        if num in tracker:
            tracker[num] -= 1
        else:
            return num

    for key in tracker:
        if tracker[key] > 0:
            return key

# nlogn
def finder2(arr1, arr2):
    arr1.sort()
    arr2.sort()

    #zip->   zip([1,2,3], [4,5,6])  -> [(1,4), (2,5), (3,6)]
    for num1, num2 in zip(arr1, arr2):

        if num1 != num2:
            return num1

    return arr1[-1]

print(finder([1,2,3,3,5,6,7, 4], [3,5,7,2,1,4,6]))


import django_filters
from backend.apps.film.models import Film, Genre ,Celebrity
from django import forms

class FilmFilter(django_filters.FilterSet):
    genres = django_filters.ModelChoiceFilter(
        field_name="genres",
        queryset=Genre.objects.all(),
        widget=forms.Select()
    )
    date_relise = django_filters.NumberFilter(
        field_name='date_relise', lookup_expr='year'
    )

    class Meta:
        model = Film
        fields = [
            'genres',
            'date_relise',
            ]

class CelebrityFilter(django_filters.FilterSet):
    birthday = django_filters.NumberFilter(
        field_name='birthday', lookup_expr='year'
    )

    class Meta:
        model = Celebrity
        fields = [
            'birthday',
            ]


# Databricks notebook source
# MAGIC %md
# MAGIC
# MAGIC ## Analyzing and Cleaning large Datasets Using Apache Spark
# MAGIC -Data Analysis
# MAGIC
# MAGIC -Data Cleaning
# MAGIC
# MAGIC -Spark dataframes funcions
# MAGIC
# MAGIC -Spark SQL
# MAGIC
# MAGIC -Descriptive statistics

# COMMAND ----------

# MAGIC
# MAGIC %sql
# MAGIC
# MAGIC select * from lc_loan_dt

# COMMAND ----------

df_sel = spark.table('lc_loan_dt')
display(df_sel)

# COMMAND ----------

# MAGIC %sql
# MAGIC select distinct emp_length from lc_loan_dt limit 50

# COMMAND ----------

from pyspark.sql.functions import regexp_replace, regexp_extract
from pyspark.sql.functions import col

regex_string='years|year|\\+|\\<'
df_sel.select(regexp_replace(col("emp_length"), regex_string, "").alias("emplength_cleaned"),col("emp_length")).show(10)

# COMMAND ----------

df_sel=df_sel.withColumn("term_cleaned",regexp_replace(col("term"), "months", "")).withColumn("emplen_cleaned",regexp_extract(col("emp_length"),"\\d+", 0))

# COMMAND ----------

df_sel.select('term','term_cleaned','emp_length','emplen_cleaned').show(15)

# COMMAND ----------

df_sel.printSchema()

# COMMAND ----------

table_name="loanstatus_sel"

df_sel.createOrReplaceTempView(table_name)

# COMMAND ----------

# MAGIC %sql
# MAGIC select * from loanstatus_sel

# COMMAND ----------

#Covariance & Correlation

df_sel.stat.cov('annual_inc', 'loan_amnt')

# COMMAND ----------

df_sel.stat.corr('annual_inc', 'loan_amnt')

# COMMAND ----------

#Crosstab

df_sel.stat.crosstab('loan_status','grade').show()

# COMMAND ----------

#frequency

freq=df_sel.stat.freqItems(['purpose','grade'],0.3)

# COMMAND ----------

freq.collect()

# COMMAND ----------

df_sel.groupby('purpose').count().show()

# COMMAND ----------

df_sel.groupby('purpose').count().orderBy(col('count').desc()).show()

# COMMAND ----------

quantileProbs = [0.25, 0.5, 0.75, 0.9]
relError = 0.05
df_sel.stat.approxQuantile("loan_amnt", quantileProbs, relError)

# COMMAND ----------

quantileProbs = [0.25, 0.5, 0.75, 0.9]
relError = 0
df_sel.stat.approxQuantile("loan_amnt", quantileProbs, relError)

# COMMAND ----------

#***************************************************************
from pyspark.sql.functions import isnan, when, count, col
df_sel.select([count(when(isnan(c) | col(c).isNull(), c)).alias(c) for c in df_sel.columns]).show()

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC select loan_status, count(*) from loanstatus_sel group by loan_status order by 2 desc

# COMMAND ----------

df_sel=df_sel.na.drop("all", subset=["loan_status"])

# COMMAND ----------

df_sel.select([count(when(isnan(c) | col(c).isNull(), c)).alias(c) for c in df_sel.columns]).show()

# COMMAND ----------

df_sel.count()

# COMMAND ----------

df_sel.describe("dti","revol_util").show()

# COMMAND ----------

# MAGIC %sql
# MAGIC select ceil(REGEXP_REPLACE(revol_util,"\%","")), count(*) from loanstatus_sel group by ceil(REGEXP_REPLACE(revol_util,"\%",""))

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC select * from loanstatus_sel where revol_util is null

# COMMAND ----------

df_sel=df_sel.withColumn("revolutil_cleaned", regexp_extract(col("revol_util"), "\\d+", 0))

# COMMAND ----------

df_sel.describe('revol_util','revolutil_cleaned').show()

# COMMAND ----------

from pyspark.sql.functions import *
from pyspark.sql.types import *
def fill_avg(df, colname):
  return df.select(colname).agg(avg(colname))

# COMMAND ----------

rev_avg=fill_avg(df_sel,'revolutil_cleaned')

# COMMAND ----------

from pyspark.sql.functions import lit

rev_avg=fill_avg(df_sel,'revolutil_cleaned').first()[0]
df_sel=df_sel.withColumn('rev_avg',lit(rev_avg))

# COMMAND ----------

from pyspark.sql.functions import coalesce
df_sel=df_sel.withColumn('revolutil_cleaned',coalesce(col('revolutil_cleaned'),col('rev_avg')))

# COMMAND ----------

df_sel.describe('revol_util','revolutil_cleaned').show()

# COMMAND ----------

df_sel=df_sel.withColumn("revolutil_cleaned",df_sel["revolutil_cleaned"].cast("double"))

# COMMAND ----------

df_sel.describe('revol_util','revolutil_cleaned').show()

# COMMAND ----------

df_sel.select([count(when(isnan(c) | col(c).isNull(), c)).alias(c) for c in df_sel.columns]).show()

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC select * from loanstatus_sel where dti is null

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC select application_type, dti, dti_joint from loanstatus_sel where dti is null

# COMMAND ----------

df_sel=df_sel.withColumn("dti_cleaned",coalesce(col("dti"),col("dti_joint")))

# COMMAND ----------

df_sel.select([count(when(isnan(c) | col(c).isNull(), c)).alias(c) for c in df_sel.columns]).show()

# COMMAND ----------

df_sel.groupby('loan_status').count().show()

# COMMAND ----------

df_sel.where(df_sel.loan_status.isin(["Late (31-120 days)", "Charged Off", "In Grace Period", "Late(16-30 days)"])).show()

# COMMAND ----------

df_sel=df_sel.withColumn("bad_loan", when(df_sel.loan_status.isin(["Late (31-120 days)", "Charged Off", "In Grace Period", "Late (16-30 days)"]), 'yes').otherwise('No'))

# COMMAND ----------

df_sel.groupBy('bad_loan').count().show()

# COMMAND ----------

df_sel.filter(df_sel.bad_loan == 'yes').show()

# COMMAND ----------

df_sel.printSchema()

# COMMAND ----------

df_sel_final=df_sel.drop('revol_util','dti','dti_joint')

# COMMAND ----------

df_sel_final.printSchema()

# COMMAND ----------

df_sel.stat.crosstab('bad_loan','grade').show()

# COMMAND ----------

df_sel.describe('dti_cleaned').show()

# COMMAND ----------

permanent_table_name = "lc_loan_datafinal"
df_sel.write.format("parquet").saveAsTable(permanent_table_name)

# COMMAND ----------

# MAGIC %sql
# MAGIC
# MAGIC select * from lc_loan_data3

# COMMAND ----------

df_sel.filter(df_sel.dti_cleaned > 100).show()

# COMMAND ----------

dbutils.fs.unmount("/mnt/landing")

# COMMAND ----------

dbutils.fs.mount(
  source = "wasbs://landing@autosparkk.blob.core.windows.net",
  mount_point = "/mnt/landing",
  extra_configs = {"fs.azure.account.key.autosparkk.blob.core.windows.net":dbutils.secrets.get(scope = "dbscope", key = "keyfinal")})

# COMMAND ----------

#data = df_sel

# COMMAND ----------

# MAGIC %scala
# MAGIC
# MAGIC // Write the file back to Azure Blob Storage
# MAGIC val df = df_sel
# MAGIC //.option("header","true")
# MAGIC //.option("inferSchema", "true")
# MAGIC //.csv("/mnt/landing/")
# MAGIC
# MAGIC spark.conf.set("fs.azure.account.key.autosparkk.blob.core.windows.net",dbutils.secrets.get(scope = "dbscope", key = "keyfinal"))
# MAGIC
# MAGIC // Save to the source container
# MAGIC df.write.mode(SaveMode.Append).json("wasbs://landing@autosparkk.blob.core.windows.net/source/")
# MAGIC
# MAGIC // Display the output in a table
# MAGIC display(df)

# COMMAND ----------

notebooks/Users/shakani@emea.corpds.net/##(databricks pipeline integration) Data cleaning and Analysis using Apache Spark.py


from core import Errors

class CythanMachine():

  def __init__(self, data, negdata=[-1]*100):

    self.data = data
    self.negdata = negdata

  def turn(self, nb):

    def combine(x,y):
      if x[0] == -1:r1=y[0]
      else:r1=x[0]
      if x[1] == -1:r2=y[1]
      else:r2=x[1]
      return [r1,r2]

    for _ in range(nb):
      try:PP = self.data[0][0]
      except:raise Errors.DataUnreachable("Data[0][0] is not reachable")
      try:PPP = self.data[PP]
      except:raise Errors.DataUnreachable("PPP: Data["+str(PP)+"] is not reachable")
      try:
        if PPP[0] == -1: dataToSet = [-1,-1]
        elif PPP[1] == -1:raise Errors.EndPoint("Machine Ended with code "+str(self.data[PPP[0]]))
      except Errors.EndPoint:raise
      except IndexError:
        raise Errors.EndPoint("Machine Ended with code from unreadable data positionned at cell "+str(PPP[0]))
      except BaseException:
        raise Errors.MinusOneRuleError("HARDWARE got a -1 rule problem.")

      try:
        if PPP[0] >=0:dataToSet = self.data[PPP[0]]
        else: dataToSet = [self.negdata[PPP[0]*-1],self.negdata[PPP[0]*-1-1]]
      except:raise Errors.DataUnreachable("DataToSet: Data["+str(PPP[0])+"] is not reachable")

      try:
        if PPP[1] >=0:dataToEarse = self.data[PPP[1]]
        else:dataToEarse= [self.negdata[PPP[1]*-1],self.negdata[PPP[1]*-1-1]]
      except:raise Errors.DataUnreachable("DataToOverWrite: Data["+str(PPP[1])+"][0] is not reachable")

      try: finaldata = combine(dataToSet, dataToEarse)
      except:raise Errors.DataCombine("HARWARE: Combine has failed.")

      try:
        self.data[0][0] += self.data[0][1]
      except:raise Errors.DataPointer("Pointer could not be add.")

      try:
        if PPP[1] >=0: self.data[PPP[1]] = finaldata
        else: self.negdata[-1*PPP[1]] = finaldata[0];self.negdata[-1*PPP[1]-1] = finaldata[1]
      except: raise Errors.DataCombine("finaldata: module "+str(PPP)+" & "+str(finaldata)+" have failed to be set.")


from hypernets.tabular import sklearn_ex as skex, get_tool_box
from hypernets.tabular.cache import cache, clear, CacheCallback
from hypernets.tabular.datasets import dsutils
from hypernets.utils import Counter

class CacheCounter(CacheCallback):
    def __init__(self):
        super(CacheCounter, self).__init__()

        self.enter_counter = Counter()
        self.apply_counter = Counter()
        self.store_counter = Counter()

    def on_enter(self, fn, *args, **kwargs):
        self.enter_counter()

    def on_apply(self, fn, cached_data, *args, **kwargs):
        self.apply_counter()

    def on_store(self, fn, cached_data, *args, **kwargs):
        self.store_counter()

    def reset(self):
        self.enter_counter.reset()
        self.apply_counter.reset()
        self.store_counter.reset()

class CachedMultiLabelEncoder(skex.MultiLabelEncoder):
    @cache(attr_keys='columns', attrs_to_restore='columns,encoders')
    def fit_transform(self, X, *args):
        return super().fit_transform(X, *args)

    @cache(attr_keys='columns', attrs_to_restore='columns,encoders')
    def fit_transform_as_tuple_result(self, X, *args):
        Xt = super().fit_transform(X.copy(), *args)
        return X, Xt

def test_cache():
    clear()

    df = dsutils.load_bank()
    t = skex.MultiLabelEncoder()
    X = t.fit_transform(df.copy())

    t1 = CachedMultiLabelEncoder()
    X1 = t1.fit_transform(df.copy())
    t2 = CachedMultiLabelEncoder()
    X2 = t2.fit_transform(df.copy())

    hasher = get_tool_box(df).data_hasher()
    assert hasher(X) == hasher(X1) == hasher(X2)

    t3 = CachedMultiLabelEncoder()
    X3 = t3.fit_transform_as_tuple_result(df.copy())
    t4 = CachedMultiLabelEncoder()
    X4 = t4.fit_transform_as_tuple_result(df.copy())
    assert isinstance(X3, (tuple, list))
    assert isinstance(X4, (tuple, list))
    assert hasher(X3[1]) == hasher(X4[1])


import cv2
from keras.models import load_model
import numpy as np
import time
import random

class CountdownTimer:
    def __init__(self, duration):
        self.duration = duration
        self.remaining_time = None

    def countdown_timer(self, func):
        def wrapper(*args, **kwargs):
            start_time = time.time()
            prev_int_remaining_time = int(self.duration)
            self.remaining_time = None  # Initialize within the scope

            while True:
                elapsed_time = time.time() - start_time
                self.remaining_time = max(0, self.duration - elapsed_time)
                int_remaining_time = int(self.remaining_time)

                if int_remaining_time != prev_int_remaining_time:
                    print(f"Time remaining: {int_remaining_time} seconds")
                    prev_int_remaining_time = int_remaining_time

                if self.remaining_time <= 0:
                    break

                # Get and show the camera frame
                ret, frame = args[0].read()
                cv2.imshow('frame', frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            return func(*args, **kwargs)  # Return the result of the wrapped function

        return wrapper

def get_computer_choice():
    computer_choice = random.choice(['rock', 'paper', 'scissors'])
    print("Computer chose:", computer_choice)
    return computer_choice

def get_prediction(frame, model):
    resized_frame = cv2.resize(frame, (224, 224), interpolation=cv2.INTER_AREA)
    image_np = np.array(resized_frame)
    normalized_image = (image_np.astype(np.float32) / 127.0) - 1
    data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
    data[0] = normalized_image
    prediction = model.predict(data)
    return prediction[0]

def get_winner(user_input, computer_choice):
    if user_input == computer_choice:
        print("It is a tie!")
    elif user_input == "rock" and computer_choice == "scissors":
        print("You win!")
    elif user_input == "paper" and computer_choice == "rock":
        print("You win!")
    elif user_input == "scissors" and computer_choice == "paper":
        print("You win!")
    else:
        print("Computer wins!")

def main():
    model = load_model('keras_model.h5')
    cap = cv2.VideoCapture(0)

    # Set a larger window size for the camera feed
    cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('frame', 800, 600)  # Set the desired width and height

    timer = CountdownTimer(duration=3)

    @timer.countdown_timer
    def get_prediction_timed(cap, model):
        while True:
            ret, frame = cap.read()
            prediction = get_prediction(frame, model)
            predicted_class = np.argmax(prediction)
            predicted_class_name = {0: "rock", 1: "paper", 2: "scissors", 3: "nothing"}
            user_choice = predicted_class_name.get(predicted_class)

            print(f"You chose {user_choice}!")
            return user_choice  # Return the user choice from here

    user_input = get_prediction_timed(cap, model)  # Run the prediction function with countdown
    print(user_input)
    computer_choice = get_computer_choice()
    get_winner(user_input, computer_choice)

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()


## Controller Python Script "setFavori"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##

authMember = context.portal_membership.getAuthenticatedMember().getId()
context.setFavoris(authMember)

context.REQUEST.RESPONSE.redirect("%s/cours/%s/" % (context.portal_url.getPortalObject().absolute_url(), authMember))


import time

num = 10000
multiples = []

t0 = time.time()
for i in range(num):
	if (i%3 == 0 or i%5== 0):
		multiples.append(i)

print("sum of multiples of numbers under %d is %s, time:%f"%(num, sum(multiples),time.time()-t0))


import pickle
import cv2
import numpy as np
from shapely.geometry import Polygon as shapely_poly
from b_Model import model

class c_Detector_1():
    def __init__(self, model):
        self.model = model

    def getModel(self):
        return self.model

    def setModel(self, model):
        self.model = model

    def get_car_boxes(self, boxes, class_ids):
        car_boxes = []

        for i, box in enumerate(boxes):
            #12, 68
            #bicycle, car, motorcycle, airplane, bus, train, truck, boat
            if class_ids[i] in [3, 4, 5, 6, 7, 8, 9, 10, 13, 68]:
                car_boxes.append(box)
        return np.array(car_boxes)

    def compute_overlaps(self, parked_car_boxes, car_boxes):
        new_car_boxes = []
        for box in car_boxes:
            y1 = box[0]
            x1 = box[1]
            y2 = box[2]
            x2 = box[3]

            p1 = (x1, y1)
            p2 = (x2, y1)
            p3 = (x2, y2)
            p4 = (x1, y2)
            new_car_boxes.append([p1, p2, p3, p4])

        overlaps = np.zeros((len(parked_car_boxes), len(new_car_boxes)))
        for i in range(len(parked_car_boxes)):
            for j in range(car_boxes.shape[0]):
                pol1_xy = parked_car_boxes[i]
                pol2_xy = new_car_boxes[j]
                polygon1_shape = shapely_poly(pol1_xy)
                polygon2_shape = shapely_poly(pol2_xy)

                polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
                polygon_union = polygon1_shape.union(polygon2_shape).area
                IOU = polygon_intersection / polygon_union
                overlaps[i][j] = IOU
        return overlaps

    def countAvailableSpots(self, frame, regions, saved):
        limit = 0.15
        alpha = 0.5
        print(" ------------ countAvailableSpots ----------------")
        cv2.addWeighted(frame, alpha, frame, 1 - alpha, 0, frame)
        overlay = frame.copy()
        nFreeSpots = 0
        #------------- modelo --------------
        with open(regions, 'rb') as f:
            parked_car_boxes = pickle.load(f)

        coco = np.zeros(len(parked_car_boxes)).tolist()
        Spots = np.array([parked_car_boxes, coco])
        nSpotsTotal = len(Spots[0])
        print('Total Parking Spots: ', nSpotsTotal)

        # Convert the image from BGR color (which OpenCV uses) to RGB color
        rgb_image = frame[:, :, ::-1]
        results = self.getModel().detect([rgb_image], verbose=0)

        car_boxes = self.get_car_boxes(results[0]['rois'], results[0]['class_ids'])
        overlaps = self.compute_overlaps(parked_car_boxes, car_boxes)

        for parking_area, overlap_areas in zip(parked_car_boxes, overlaps):
            max_IoU_overlap = np.max(overlap_areas)
            if max_IoU_overlap < limit:
                cv2.fillPoly(overlay, [np.array(parking_area)], (71, 27, 92))
                nFreeSpots += 1
            else:
                cv2.fillPoly(overlay, [np.array(parking_area)], (56, 224, 120))

        cv2.imwrite(saved, overlay)
        video_scale = 70
        width = int(overlay.shape[1] * video_scale / 100)
        height = int(overlay.shape[0] * video_scale / 100)
        dim = (width, height)
        resized = cv2.resize(overlay, dim, interpolation=cv2.INTER_AREA)
        cv2.imshow('frame', resized)
        cv2.waitKey(0)
        print('Free Parking Spots: ', nFreeSpots)
        return nFreeSpots


#%% [markdown]
# ## Python Script that the application will call to take a voice clip and manipulate it for TensorFlowLite

# %%
# Test a single clip with tflite model

# tflitePath = 'R:\School\Masters\Thesis\\automated-spoken-language-detection\source\model.tflite'
# filePath = 'R:\School\Masters\Thesis\\automated-spoken-language-detection\database\cv-corpus-6.0-2020-12-11\oneTimeTestDatabase\s19.wav'
def pythonClassify(filePath, tflitePath):
    import numpy as np
    from tensorflow.keras.preprocessing.image import img_to_array
    import tensorflow as tf
    import librosa
    from os.path import dirname, join

    langDBs = ["English", "French", "Arabic", "German", "Persian", "Russian", "Chinese (China)"]

    #tflitePath = join(dirname(__file__), "model/model.tflite")

    interpreter = tf.lite.Interpreter(model_path=tflitePath)
    interpreter.allocate_tensors()

    loadedClip = librosa.load(filePath, 16000)
    # Fix Duration to 10 seconds
    fixedClip = np.concatenate([loadedClip[0]]*9, axis=0)
    fixedClip = fixedClip[0:(16000*10)]
    # Make Spectogram
    hoplength = fixedClip.shape[0] // 500
    spec = librosa.feature.melspectrogram(fixedClip, n_mels=128, hop_length=int(hoplength))
    image = librosa.core.power_to_db(spec)
    image_np = np.asmatrix(image)
    image_np_scaled_temp = (image_np - np.min(image_np))
    image_np_scaled = image_np_scaled_temp / np.max(image_np_scaled_temp)
    spectro = image_np_scaled[:, 0:500]

    input_array = img_to_array((spectro * 255.).astype(np.uint8))
    input_array = np.array([input_array])
    input_array = input_array.astype('float32') / 255.

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]['index'], input_array)
    interpreter.invoke()
    result = interpreter.get_tensor(output_details[0]['index'])

    classification = np.max(result[0])
    classificationOfLanguage = langDBs[np.where(result[0] == classification)[0][0]]
    returnStatement = "I am  " + str(round(classification*100, 2)) + "% sure that this is " +  classificationOfLanguage
    return returnStatement

def pythonWarmUpModel(tflitePath):
    import tensorflow as tf

    interpreter = tf.lite.Interpreter(model_path=tflitePath)
    interpreter.allocate_tensors()

# %%


from itertools import product

def solution(users, emoticons):
    discount = [10, 20, 30, 40]
    purpose_list = []
    for combs in product(discount, repeat=len(emoticons)):

        discounted_price_list = []
        for i in range(len(emoticons)):
            discounted_price_list.append(round(emoticons[i] * (100 - combs[i]) / 100))

        money_list = []
        register = 0
        for user in users:
            money = 0
            for i in range(len(emoticons)):
                if combs[i] >= user[0]:
                    money += discounted_price_list[i]

            if money >= user[1]:
                register += 1
                money = 0

            money_list.append(money)
        purpose = [register, sum(money_list)]
        purpose_list.append(purpose)

    purpose_list.sort(key=lambda x: (x[0], x[1]))
    answer = purpose_list[-1]
    return answer

users = [[40, 2900], [23, 10000], [11, 5200], [5, 5900], [40, 3100], [27, 9200], [32, 6900]]
emoticons = [1300, 1500, 1600, 4900]
discount = [10, 20, 30, 40]

print(solution(users, emoticons))


from pygame import mixer
import os
from datetime import datetime
import time

os.system("CLS")
print("Welcome to the Healthy Programmer Application!\n")
print("This application starts at 9AM and lasts till 5PM\n")
print("During this time it reminds you the following:")
print("1) Drink water after every 45mins!")
print("2) Remove eyes from the screen after every 25mins!")
print("3) Do a physical activity after every 1hour!\n")

mixer.init()
while(1):
    time.sleep(1)
    now = datetime.now()
    current_time = int(now.strftime("%S"))

    if(current_time == 0):
        current_time = 60

    print("Current Time =", current_time)

    if(current_time % 25 == 0):
        mixer.music.load("Healthy Programmer Exercise\eye.mp3")
        mixer.music.set_volume(1.0)
        mixer.music.play()
    elif(current_time % 45 == 0):
        mixer.music.load("Healthy Programmer Exercise\water.mp3")
        mixer.music.set_volume(1.0)
        mixer.music.play()
    elif(current_time % 60 == 0):
        mixer.music.load("Healthy Programmer Exercise\physical.mp3")
        mixer.music.set_volume(1.0)
        mixer.music.play()


import copy
import json
from datetime import datetime

from perceptrons.single_perceptron import perceptron
from simple_perceptron import *
from animation import animate_lines
from perceptron_average import average_accuracy

def export_metrics(metrics):
    now = datetime.now().strftime("%d-%m-%Y_%H%M%S")
    with open(f"./results/results_{now}.json", mode="w+") as file:
        file.write(json.dumps(metrics, indent=4))

def main():
    with open("config.json") as file:
        config = json.load(file)

    input_data = config["input_data"]

    if config["and"]["run_and"]:
        expected_output_and = config["and"]["expected_output_and"]

        print("Running \'AND\' data set")
        w_min, metrics_and = perceptron(copy.deepcopy(input_data),
                                        expected_output_and,
                                        config["and"]["learning_constant"],
                                        0,
                                        update_weights_simple,
                                        module_error_simple,
                                        theta_simple,
                                        collect_metrics,
                                        config["and"]["limit"])
        if config["and"]["print_wmin"]:
            print(f"\'AND\' w min: {w_min}")

        if config["and"]["generate_gif"]:
            export_metrics(metrics_and)
            animate_lines(input_data, "and_gif")

    if config["xor"]["run_xor"]:
        expected_output_xor = config["xor"]["expected_output_xor"]

        print("Running \'XOR\' data set")
        w_min_xor, metrics_xor = perceptron(copy.deepcopy(input_data),
                                            expected_output_xor,
                                            config["xor"]["learning_constant"],
                                            0,
                                            update_weights_simple,
                                            module_error_simple,
                                            theta_simple,
                                            collect_metrics,
                                            config["xor"]["limit"])

        if config["xor"]["print_wmin"]:
            print(f"\'XOR\' w min: {w_min_xor}")

        if config["xor"]["generate_gif"]:
            export_metrics(metrics_xor)
            animate_lines(input_data, "xor_gif")

    if config["average"]["run_average"]:
        average_accuracy(config["average"]["limit"],
                         config["average"]["number_of_runs"],
                         config["average"]["run_and_data_set"],
                         config["average"]["learning_constant"])

main()


import pickle
import sklearn.linear_model
from sklearn.ensemble import RandomForestClassifier
import numpy as np

class Baseline(object):
	def __init__(self, params):
		self.params = params
		with open(params['data_x'], 'rb') as f:
			self.x = np.array(pickle.load(f))[:, params['access_day'][0]:params['access_day'][1], params['access_feat'][0]:params['access_feat'][1]]
		with open(params['data_y'], 'rb') as f:
			self.y = np.array(pickle.load(f))
		self.x = self.x.reshape(-1, (params['access_day'][1]-params['access_day'][0])*(params['access_feat'][1]-params['access_feat'][0]))
		self.n = self.x.shape[0]
		seed = 1111
		split_train = int(np.floor(self.params['split_ratio']*self.n))
		ind = np.arange(self.n)
		np.random.seed(seed)
		np.random.shuffle(ind)
		self.ind_train, self.ind_test = ind[:split_train], ind[split_train:]

	def logistic(self):
		model = sklearn.linear_model.LogisticRegression()
		model.fit(self.x[self.ind_train,:], self.y[self.ind_train])
		pred = model.predict(self.x[self.ind_test,:])
		true = self.y[self.ind_test]
		acc = (pred==true).sum()*1.0/len(pred)
		tp = map(lambda i: pred[i] == 1 and true[i] == 1, range(len(pred))).count(True)
		fp = map(lambda i: pred[i] == 1 and true[i] == 0, range(len(pred))).count(True)
		fn = map(lambda i: pred[i] == 0 and true[i] == 1, range(len(pred))).count(True)
		prec = tp*1.0/(tp+fp)
		rec = tp*1.0/(tp+fn)
		print(acc, prec, rec)

	def forest(self):
		model = RandomForestClassifier(max_depth=4, random_state=0)
		model.fit(self.x[self.ind_train,:], self.y[self.ind_train])
		pred = model.predict(self.x[self.ind_test,:])
		true = self.y[self.ind_test]
		acc = (pred==true).sum()*1.0/len(pred)
		tp = map(lambda i: pred[i] == 1 and true[i] == 1, range(len(pred))).count(True)
		fp = map(lambda i: pred[i] == 1 and true[i] == 0, range(len(pred))).count(True)
		fn = map(lambda i: pred[i] == 0 and true[i] == 1, range(len(pred))).count(True)
		prec = tp*1.0/(tp+fp)
		rec = tp*1.0/(tp+fn)
		print(acc, prec, rec)

if __name__ == '__main__':
	params = {}
	params['data_x'] = '../data/act_data.pkl'
	params['data_y'] = '../data/churns.pkl'
	params['access_day'] = (0, 7)
	params['access_feat'] = (0, 12)
	params['split_ratio'] =0.8

	predictor = Baseline(params)
	predictor.forest()


import os
import discord
from discord.ext import tasks
from dotenv import load_dotenv
from acciones import conectar, conectar_debug, acciones_programadas, recibir_mensaje_discord, recibir_mensaje_telegram
from fechayhora import justo_ahora
import tg

INICIADO = False

def main():
    load_dotenv()
    DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
    DEBUG = os.getenv('DEBUG')=="YES"
    if (not os.path.isdir('tmp')):
        os.makedirs('tmp')

    @tasks.loop(minutes=1)
    async def una_vez_por_minuto():
        # Recibir mensajes de TG:
        for mensaje in tg.recibir_mensajes():
            recibir_mensaje_telegram(mensaje)
        await acciones_programadas()

    if (DEBUG):
        conectar_debug()
    else:
        client = discord.Client()
        tg.inicializar()

        @client.event
        async def on_message(message):
            if message.author == client.user:
                return
            await recibir_mensaje_discord(message)

        @client.event
        async def on_ready():
            global INICIADO
            await conectar(client)
            if not INICIADO:
                una_vez_por_minuto.start()
                INICIADO = True
            print(str(justo_ahora()) + " READY")

        client.run(DISCORD_TOKEN)

if __name__ == "__main__":
    main()


import sys

def LCS(str_a, str_b):
	# 두 문자열을 한글자씩 비교
	for i in range(1, len(str_a) + 1):
		for j in range(1, len(str_b) + 1):
			# 두 문자가 같다면 dp[i - 1][j - 1]값을 찾아 1증가
			if str_a[i - 1] == str_b[j - 1]:
				dp[i][j] = dp[i - 1][j - 1] + 1
			# 두 문자가 다르다면 dp[i - 1][j]와 dp[i][j - 1]중에서 큰값을 표시
			else:
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
	# dp테이블의 마지막 값 출력
	return dp[-1][-1]

if __name__ == "__main__":
	sys.stdin = open("input.txt","rt")
	# 두 문자열 입력
	str_a = sys.stdin.readline().strip()
	str_b = sys.stdin.readline().strip()
	# DP테이블 생성 : i, j가 1이상일 때부터 검사할 수 있도록 편의상 i, j가 0일때는 모두 0을 넣어 마진값 설정
	dp = [[0] * (len(str_b) + 1) for _ in range(len(str_a) + 1)]
	# 두 문자열에 대한 최장 공통 부분수열 출력
	print(LCS(str_a, str_b))


#W3PA2

L = [3, 5, 7, 1, 10, 11, 22, 4, 67, 54, 2, 99]
n = 10
#L = []
#n

def rev(L, n):
    L.sort(reverse = True)
    #print(L)
    print(L[0:n], end = "") # prints index zero to n-1 (ie. 9) (Prints index 0 to index 9)

rev(L, n)

# Alternative, sort funtion:

    # sorted(L, reverse = True)

'''
#Write a function rev which takes a list L and integer n and print the
# first n largest numbers of the list.

#Input is managed for you, please write the required function only.

#Input:

#A list L and an integer n.

#Output:

#First n largest numbers

'''


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.figure as figure
import pylab
import matplotlib.patches as patches
import time
import copy
import puyo_func as puyo
pylab.figure(figsize=(6,12))
ax=plt.gca()
plt.xlim(xmin=0)
plt.xlim(xmax=6)
plt.ylim(ymin=0)
plt.ylim(ymax=12)
haichi=[[0 for i in range(6)] for j in range(13)]
n13=raw_input('[13] ')
n12=raw_input('[12] ')
n11=raw_input('[11] ')
n10=raw_input('[10] ')
n9=raw_input('[9]  ')
n8=raw_input('[8]  ')
n7=raw_input('[7]  ')
n6=raw_input('[6]  ')
n5=raw_input('[5]  ')
n4=raw_input('[4]  ')
n3=raw_input('[3]  ')
n2=raw_input('[2]  ')
n1=raw_input('[1]  ')
num1=[int(x) for x in list(str(n1))]
num2=[int(x) for x in list(str(n2))]
num3=[int(x) for x in list(str(n3))]
num4=[int(x) for x in list(str(n4))]
num5=[int(x) for x in list(str(n5))]
num6=[int(x) for x in list(str(n6))]
num7=[int(x) for x in list(str(n7))]
num8=[int(x) for x in list(str(n8))]
num9=[int(x) for x in list(str(n9))]
num10=[int(x) for x in list(str(n10))]
num11=[int(x) for x in list(str(n11))]
num12=[int(x) for x in list(str(n12))]
num13=[int(x) for x in list(str(n13))]
haichi[0][0]=num1[0]
haichi[0][1]=num1[1]
haichi[0][2]=num1[2]
haichi[0][3]=num1[3]
haichi[0][4]=num1[4]
haichi[0][5]=num1[5]
haichi[1][0]=num2[0]
haichi[1][1]=num2[1]
haichi[1][2]=num2[2]
haichi[1][3]=num2[3]
haichi[1][4]=num2[4]
haichi[1][5]=num2[5]
haichi[2][0]=num3[0]
haichi[2][1]=num3[1]
haichi[2][2]=num3[2]
haichi[2][3]=num3[3]
haichi[2][4]=num3[4]
haichi[2][5]=num3[5]
haichi[3][0]=num4[0]
haichi[3][1]=num4[1]
haichi[3][2]=num4[2]
haichi[3][3]=num4[3]
haichi[3][4]=num4[4]
haichi[3][5]=num4[5]
haichi[4][0]=num5[0]
haichi[4][1]=num5[1]
haichi[4][2]=num5[2]
haichi[4][3]=num5[3]
haichi[4][4]=num5[4]
haichi[4][5]=num5[5]
haichi[5][0]=num6[0]
haichi[5][1]=num6[1]
haichi[5][2]=num6[2]
haichi[5][3]=num6[3]
haichi[5][4]=num6[4]
haichi[5][5]=num6[5]
haichi[6][0]=num7[0]
haichi[6][1]=num7[1]
haichi[6][2]=num7[2]
haichi[6][3]=num7[3]
haichi[6][4]=num7[4]
haichi[6][5]=num7[5]
haichi[7][0]=num8[0]
haichi[7][1]=num8[1]
haichi[7][2]=num8[2]
haichi[7][3]=num8[3]
haichi[7][4]=num8[4]
haichi[7][5]=num8[5]
haichi[8][0]=num9[0]
haichi[8][1]=num9[1]
haichi[8][2]=num9[2]
haichi[8][3]=num9[3]
haichi[8][4]=num9[4]
haichi[8][5]=num9[5]
haichi[9][0]=num10[0]
haichi[9][1]=num10[1]
haichi[9][2]=num10[2]
haichi[9][3]=num10[3]
haichi[9][4]=num10[4]
haichi[9][5]=num10[5]
haichi[10][0]=num11[0]
haichi[10][1]=num11[1]
haichi[10][2]=num11[2]
haichi[10][3]=num11[3]
haichi[10][4]=num11[4]
haichi[10][5]=num11[5]
haichi[11][0]=num12[0]
haichi[11][1]=num12[1]
haichi[11][2]=num12[2]
haichi[11][3]=num12[3]
haichi[11][4]=num12[4]
haichi[11][5]=num12[5]
haichi[12][0]=num13[0]
haichi[12][1]=num13[1]
haichi[12][2]=num13[2]
haichi[12][3]=num13[3]
haichi[12][4]=num13[4]
haichi[12][5]=num13[5]
puyo.hyouji(haichi)
plt.savefig('/home/harada/python2/result_puyo.png')
time.sleep(1)
plt.close()
puyo.rakka(haichi)
puyo.hyouji(haichi)
puyo.rensa(haichi)
puyo.hyouji(haichi)

plt.savefig('/home/harada/python2/result_puyo.png')


import threading
import time

import pytest
import requests

from ocean_lib.assets.ddo import DDO
from ocean_lib.data_provider.data_encryptor import DataEncryptor
from ocean_lib.data_provider.data_service_provider import DataServiceProvider
from ocean_lib.example_config import DEFAULT_PROVIDER_URL
from ocean_lib.ocean.ocean import Ocean

exception_flag = 0

def test_with_wrong_provider(config, caplog):
    """Tests encrypt with a good provider URL and then switch to a bad one."""

    config["PROVIDER_URL"] = DEFAULT_PROVIDER_URL
    updating_thread = threading.Thread(
        target=_update_with_wrong_component,
        args=(config,),
    )
    updating_thread.start()
    _iterative_encrypt(config)
    updating_thread.join()

    assert "Asset urls encrypted successfully" in caplog.text
    assert exception_flag == 1

def test_with_wrong_aquarius(publisher_wallet, caplog, monkeypatch, config):
    """Tests DDO creation with a good config.ini and then switch to a bad one."""
    config["METADATA_CACHE_URI"] = "http:://not-valid-aqua.com"

    with pytest.raises(Exception, match="Invalid or unresponsive aquarius url"):
        ocean = Ocean(config, DataServiceProvider)

    config["METADATA_CACHE_URI"] = "http://172.15.0.5:5000"
    ocean = Ocean(config, DataServiceProvider)

    # force a bad URL, assuming initial Ocean and Aquarius objects were created successfully
    ocean.assets._aquarius.base_url = "http://not-valid-aqua.com"
    with pytest.raises(Exception):
        ocean.assets._aquarius.validate_ddo(DDO())

def _create_ddo(ocean, publisher):
    global exception_flag
    time.sleep(5)
    try:
        ocean.assets.create_url_asset(
            "Sample asset", "https://foo.txt", {"from": publisher}
        )
    except requests.exceptions.InvalidURL as err:
        exception_flag = 1
        assert err.args[0] == "InvalidURL http://foourl.com."
    except requests.exceptions.ConnectionError as e:
        exception_flag = 2
        assert (
            e.args[0]
            .args[0]
            .startswith("HTTPConnectionPool(host='fooaqua.com', port=80)")
        )

def _iterative_create_ddo(mock_ocean, publisher):
    time.sleep(10)
    _create_ddo(mock_ocean.return_value, publisher)

def _iterative_encrypt(mock):
    global exception_flag
    for _ in range(5):
        try:
            DataEncryptor.encrypt({}, mock["PROVIDER_URL"], 8996)
        except requests.exceptions.InvalidURL as err:
            exception_flag = 1
            assert err.args[0] == "InvalidURL http://foourl.com."
        time.sleep(1)

def _update_with_wrong_component(mock):
    time.sleep(2)
    mock["PROVIDER_URL"] = "http://foourl.com"


import importlib
import time
import traceback

from selenium import webdriver
from IPython.core.debugger import Tracer

import webdriver.page_objects as page_objects
import webdriver.test_connect_page as test_connect_page
import webdriver.test_overview_page as test_overview_page

# useful inside the IPython debugger
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# BASE_URL = 'http://10.0.2.2:8080/hawtio'
# CONSOLE_IP = '10.0.2.2'

BASE_URL = 'http://127.0.0.1:8080/hawtio'
CONSOLE_IP = '127.0.0.1'

def initialize_local_chrome():
    options = webdriver.ChromeOptions()
    options.binary_location = '/home/jdanek/.nix-profile/bin/google-chrome-stable'
    driver = webdriver.Chrome(chrome_options=options)
    return driver

def initialize_remote_selenium():

    selenium_url = 'http://192.168.33.10:4444/wd/hub'
    capabilities = {'browserName': 'chrome'}

    # selenium_url = 'http://127.0.0.1:4444/wd/hub'
    # capabilities = {
    #     'browserName': 'jbrowserdriver',
    #     'version': '1',
    #     'platform': 'ANY',
    # }

    driver = webdriver.Remote(selenium_url, desired_capabilities=capabilities)
    driver.implicitly_wait(10)
    return driver

def deinitialize_selenium(driver: webdriver.Remote):
    driver.close()

def reload_test(selenium):
    """Reloads modules and instantiates a test.

    Edit this method to choose which test you want to be executed
    """
    base_url = BASE_URL
    console_ip = CONSOLE_IP

    importlib.reload(page_objects)
    importlib.reload(test_connect_page)
    importlib.reload(test_overview_page)

    suite = test_connect_page.TestConnectPage().setup(base_url, console_ip, selenium)
    test = suite.test_submit_form_with_enter_key
    return test

def main():

    selenium = initialize_local_chrome()
    # selenium = initialize_remote_selenium()
    try:
        while True:
            thence = time.time()
            print('Beginning a new run')
            test = reload_test(selenium)

            try:
                test()
            except:
                traceback.print_exc()

            print('Run finished. Took {}. Continue?'.format(time.time() - thence))
            Tracer(colors='NoColor')()

    finally:
        deinitialize_selenium(selenium)

# Tip: select everything from here up and Execute Selection in Console (Alt+Shift+E in Intellij)
if __name__ == '__main__':
    main()


import simplejson as json
import os

new_file = open("newFile.txt", "w+")
string = "Content to be written in file."
new_file.write(string)

if os.path.isfile("./ages.json") and os.stat("./ages.json").st_size != 0:  # Check if the file exists and has some data
	old_file = open("./ages.json", "r+")
	data = json.loads(old_file.read())
	print("Current age is", data["age"], "-- adding a year.")
	data["age"] = data["age"] + 1
	print("New age is", data["age"])
else:
	old_file = open("./ages.json", "w+")
	data = {"name": "Mark", "age": 24}
	print("No file found, set as default age to:", data["age"])

old_file.seek(0)
old_file.write(json.dumps(data))


#!/usr/local/bin/python3

# avenir-python: Machine Learning
# Author: Pranab Ghosh
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.

import os
import sys
from random import randint
import time
from array import *
import statistics
import numpy as np
from sklearn.neighbors import KDTree
from matumizi.mlutil import *
from matumizi.util import *
from matumizi.sampler import *
from torvik.tnn import *
from matumizi.daexp import *

NFEAT = 11
NFEAT_EXT = 14

class LoanApprove:
	def __init__(self, numLoans=None):
		self.numLoans = numLoans
		self.marStatus = ["married", "single", "divorced"]
		self.loanTerm = ["7", "15", "30"]
		self.addExtra = False

	def initOne(self):
		"""
		initialize samplers
		"""
		self.threshold = 118
		self.margin = 5

		# distributions
		self.marriedDistr = CategoricalRejectSampler(("married", 80), ("single", 100), ("divorced", 30))
		self.numChildDistr = CategoricalRejectSampler(("1", 80), ("2", 100), ("2", 40))
		self.eduDistr = CategoricalRejectSampler(("1", 60), ("2", 100), ("3", 30))
		self.selfEmployedDistr = CategoricalRejectSampler(("1", 30), ("0", 100))
		self.incomeDistr = GaussianRejectSampler(100,20)
		self.numYearsExpDistr = GaussianRejectSampler(10,3)
		self.outstandingLoanDistr = GaussianRejectSampler(20,5)
		self.loanAmDistr = GaussianRejectSampler(300,70)
		self.loanTermDistr = CategoricalRejectSampler(("10", 40), ("15", 60), ("30", 100))
		self.credScoreDistr = GaussianRejectSampler(700,50)
		zipClusterDistr = [("high", 30), ("average", 100), ("low", 60)]
		zipClusters = {\
		"high" : ["95061", "95062", "95064", "95065", "95067"], \
		"average" : ["95103", "95104", "95106", "95107", "95109", "95113", "95115", "95118", "95121" ], \
		"low" : ["95376", "95377", "95378", "95353", "95354", "95356"]}
		self.zipDistr = ClusterSampler(zipClusters, ("high", 30), ("average", 100), ("low", 60))

		# scores
		self.marriedScore = {"married" : 16, "single" : 10, "divorced" : 6}
		self.numChildScore = {1 : 12, 2 : 9 , 3 : 4}
		self.eduScore = {1 : 7 , 2 : 12, 3 : 15}
		self.selfEmployedScore = {"0" : 15, "1" : 11}
		self.incomeScore = StepFunction((50, 70, 2), (70, 90, 5), (90, 100, 8), (100, 110, 12),\
		(110, 130, 14), (130, 150, 18))
		self.numYearsExpScore = StepFunction((6, 10, 4), (10, 14, 9), (14, 20, 13))
		self.outstandingLoanScore = StepFunction((2, 4, 16), (4, 8, 13), (8, 14, 10), (14, 22, 8),\
		(22, 32, 6), (32, 44, 2))
		self.loanAmScore = StepFunction((200, 250, 22), (250, 300, 20), (300, 350, 16), (350, 400, 10),\
		(400, 450, 5), (450, 500, 2))
		self.loanTermScore = {10 : 15, 15 : 18 , 30 : 23}
		self.credScoreScore = StepFunction((600, 650, 8), (650, 700, 12), (700, 750, 17), (750, 800, 23),\
		(800, 850, 31))
		self.zipRateScore = {"high" : 17, "average" : 15, "low" : 11}

	def generateOne(self):
		"""
		sample
		"""
		self.initOne()
		posCount = 0
		for i in range(self.numLoans):
			id = genID(10)
			married = self.marriedDistr.sample()
			numChild = int(self.numChildDistr.sample())
			edu = int(self.eduDistr.sample())
			selfEmployed = self.selfEmployedDistr.sample()
			income = int(self.incomeDistr.sample())
			income = rangeSample(income, 50, 160)
			numYearsExp = int(self.numYearsExpDistr.sample())
			numYearsExp = rangeSample(numYearsExp, 6, 20)
			outstandingLoan = int(self.outstandingLoanDistr.sample())
			loanAm = int(self.loanAmDistr.sample())
			loanAm = rangeSample(loanAm, 200, 500)
			loanTerm  = int(self.loanTermDistr.sample())
			credScore = int(self.credScoreDistr.sample())
			credScore = rangeSample(credScore, 600, 850)
			(zipRate, zipCode)  = self.zipDistr.sample()

			# score for each score
			score = 0
			score +=  self.marriedScore[married]
			score +=  self.numChildScore[numChild]
			score +=  self.eduScore[edu]
			score +=  self.selfEmployedScore[selfEmployed]
			score +=  self.incomeScore.find(income)
			score +=  self.numYearsExpScore.find(numYearsExp)
			score +=  self.outstandingLoanScore.find(outstandingLoan)
			score +=  self.loanTermScore[loanTerm]
			score +=  self.credScoreScore.find(credScore)
			score +=  self.zipRateScore[zipRate]

			# feature coupling
			if (income > 140 and loanAm < 300):
				score += 10
			if (income < 80 and loanAm > 280):
				score -= 12

			if (credScore > 760 and loanAm < 320):
				score += 12
			if (credScore < 700 and loanAm > 260):
				score -= 14

			if (numChild == 3 and income < 100):
				score -= 8

			# outcome
			if score > (self.threshold + self.margin):
				approved = 1
			elif score < (self.threshold - self.margin):
				approved = 0
			else:
				if randint(0, 100) < 50:
					approved = 1
				else:
					approved = 0

			if approved == 1:
				posCount += 1

			print ("{},{},{},{},{},{},{},{},{},{},{},{},{}".format(id, married, numChild, edu, selfEmployed, income,\
			numYearsExp, outstandingLoan, loanAm, loanTerm, credScore, zipCode, approved))

		#print "positive count " + str(posCount)

	def initTwo(self):
		"""
		initialize samplers
		"""
		self.approvDistr = CategoricalRejectSampler(("1", 60), ("0", 40))
		self.featCondDister = {}

		#marital status
		key = ("1", 0)
		distr = CategoricalRejectSampler(("married", 100), ("single", 60), ("divorced", 40))
		self.featCondDister[key] = distr
		key = ("0", 0)
		distr = CategoricalRejectSampler(("married", 40), ("single", 100), ("divorced", 40))
		self.featCondDister[key] = distr

		# num of children
		key = ("1", 1)
		distr = CategoricalRejectSampler(("1", 100), ("2", 90), ("3", 40))
		self.featCondDister[key] = distr
		key = ("0", 1)
		distr = CategoricalRejectSampler(("1", 50), ("2", 70), ("3", 100))
		self.featCondDister[key] = distr

		# education
		key = ("1", 2)
		distr = CategoricalRejectSampler(("1", 30), ("2", 80), ("3", 100))
		self.featCondDister[key] = distr
		key = ("0", 2)
		distr = CategoricalRejectSampler(("1", 100), ("2", 40), ("3", 30))
		self.featCondDister[key] = distr

		#self employed
		key = ("1", 3)
		distr = CategoricalRejectSampler(("1", 40), ("0", 100))
		self.featCondDister[key] = distr
		key = ("0", 3)
		distr = CategoricalRejectSampler(("1", 100), ("0", 30))
		self.featCondDister[key] = distr

		# income
		key = ("1", 4)
		distr = GaussianRejectSampler(120,15)
		self.featCondDister[key] = distr
		key = ("0", 4)
		distr = GaussianRejectSampler(50,10)
		self.featCondDister[key] = distr

		# years of experience
		key = ("1", 5)
		distr = GaussianRejectSampler(15,3)
		self.featCondDister[key] = distr
		key = ("0", 5)
		distr = GaussianRejectSampler(5,1)
		self.featCondDister[key] = distr

		# number of years in current job
		key = ("1", 6)
		distr = GaussianRejectSampler(3,.5)
		self.featCondDister[key] = distr
		key = ("0", 6)
		distr = GaussianRejectSampler(1,.2)
		self.featCondDister[key] = distr

		# outstanding debt
		key = ("1", 7)
		distr = GaussianRejectSampler(20,5)
		self.featCondDister[key] = distr
		key = ("0", 7)
		distr = GaussianRejectSampler(60,10)
		self.featCondDister[key] = distr

		# loan amount
		key = ("1", 8)
		distr = GaussianRejectSampler(300,50)
		self.featCondDister[key] = distr
		key = ("0", 8)
		distr = GaussianRejectSampler(600,50)
		self.featCondDister[key] = distr

		# loan term
		key = ("1", 9)
		distr = CategoricalRejectSampler(("7", 100), ("15", 40), ("30", 60))
		self.featCondDister[key] = distr
		key = ("0", 9)
		distr = CategoricalRejectSampler(("7", 30), ("15", 100), ("30", 60))
		self.featCondDister[key] = distr

		# credit score
		key = ("1", 10)
		distr = GaussianRejectSampler(700,20)
		self.featCondDister[key] = distr
		key = ("0", 10)
		distr = GaussianRejectSampler(500,50)
		self.featCondDister[key] = distr

		if self.addExtra:
			# saving
			key = ("1", 11)
			distr = NormalSampler(80,10)
			self.featCondDister[key] = distr
			key = ("0", 11)
			distr = NormalSampler(60,8)
			self.featCondDister[key] = distr

			# retirement
			zDistr = NormalSampler(0, 0)
			key = ("1", 12)
			sDistr = DiscreteRejectSampler(0,1,1,20,80)
			nzDistr = NormalSampler(100,20)
			distr = DistrMixtureSampler(sDistr, zDistr, nzDistr)
			self.featCondDister[key] = distr
			key = ("0", 12)
			sDistr = DiscreteRejectSampler(0,1,1,50,50)
			nzDistr = NormalSampler(40,10)
			distr = DistrMixtureSampler(sDistr, zDistr, nzDistr)
			self.featCondDister[key] = distr

			#num of prior mortgae loans
			key = ("1", 13)
			distr = DiscreteRejectSampler(0,3,1,20,60,40,15)
			self.featCondDister[key] = distr
			key = ("0", 13)
			distr = DiscreteRejectSampler(0,1,1,70,30)
			self.featCondDister[key] = distr

	def generateTwo(self, noise, keyLen, addExtra):
		"""
		ancestral sampling
		"""
		self.addExtra = addExtra
		self.initTwo()

		#error
		erDistr = GaussianRejectSampler(0, noise)

		#sampler
		numChildren = NFEAT_EXT if self.addExtra else NFEAT
		sampler = AncestralSampler(self.approvDistr, self.featCondDister, numChildren)

		for i in range(self.numLoans):
			(claz, features) = sampler.sample()

			# add noise
			features[4] = int(features[4])
			features[7] = int(features[7])
			features[8] = int(features[8])
			features[10] = int(features[10])
			if self.addExtra:
				features[11] = int(features[11])
				features[12] = int(features[12])

			claz = addNoiseCat(claz, ["0", "1"], noise)

			strFeatures = list(map(lambda f: toStr(f, 2), features))
			rec =  genID(keyLen) + "," + ",".join(strFeatures) + "," + claz
			print (rec)

	def encodeDummy(self, fileName, extra):
		"""
		dummy var encoding
		"""
		catVars = {}
		catVars[1] = self.marStatus
		catVars[10] = self.loanTerm
		rSize = NFEAT_EXT if extra else NFEAT
		rSize += 2
		dummyVarGen = DummyVarGenerator(rSize, catVars, "1", "0", ",")
		for row in fileRecGen(fileName, None):
			newRow = dummyVarGen.processRow(row)
			print (newRow)

	def encodeLabel(self, fileName):
		"""
		label encoding
		"""
		catVars = {}
		catVars[1] = self.marStatus
		catVars[10] = self.loanTerm
		encoder = CatLabelGenerator(catVars, ",")
		for row in fileRecGen(fileName, None):
			newRow = encoder.processRow(row)
			print (newRow)

##########################################################################################
if __name__ == "__main__":
	op = sys.argv[1]

	if op == "generate" or op == "genOne" :
		"""  generate data """
		numLoans = int(sys.argv[2])
		loan = LoanApprove(numLoans)
		loan.generateOne()

	elif op == "genTwo":
		"""  generate data """
		numLoans = int(sys.argv[2])
		loan = LoanApprove(numLoans)
		noise = float(sys.argv[3])
		keyLen = int(sys.argv[4])
		addExtra = True if len(sys.argv) == 6 and sys.argv[5] == "extra" else False
		loan.generateTwo(noise, keyLen, addExtra)

	elif op == "encDummy":
		""" encode binary """
		fileName = sys.argv[2]
		extra = True if len(sys.argv) == 4 and sys.argv[3] == "extra" else False
		loan = LoanApprove()
		loan.encodeDummy(fileName, extra)

	elif op == "encLabel":
		""" encode label """
		fileName = sys.argv[2]
		loan = LoanApprove(numLoans)
		loan.encodeLabel(fileName)

	elif op == "nnTrain":
		""" tran neural network model """
		prFile = sys.argv[2]
		loModel = FeedForwardNetwork(prFile)
		loModel.buildModel()
		FeedForwardNetwork.batchTrain(loModel)

	elif op == "nnVal":
		""" tran neural network model """
		prFile = sys.argv[2]
		loModel = FeedForwardNetwork(prFile)
		loModel.buildModel()
		FeedForwardNetwork.validateModel(loModel)

	elif op == "nnPred":
		""" tran neural network model """
		prFile = sys.argv[2]
		loModel = FeedForwardNetwork(prFile)
		loModel.buildModel()
		yp = FeedForwardNetwork.modelPredict(loModel)
		print(yp[:4])

	elif op == "predOne":
		""" tran neural network model """
		prFile = sys.argv[2]
		rec = sys.argv[3]
		typeSpec = "0:str,1:cat,2:int,3:int,4:int,5:int,6:float,7:float,8:int,9:int,10:cat,11:int,12:int,13:int,14:int"
		lt = len(typeSpec.split(","))
		lr = len(rec.split(","))
		#print (lr, lt)
		rec = getRecAsTypedRecord(rec, typeSpec, ",")

		catVars = {}
		marStatus = ["married", "single", "divorced"]
		loanTerm = ["7", "15", "30"]
		catVars[1] = marStatus
		catVars[10] = loanTerm
		dummyVarGen = DummyVarGenerator(lr, catVars, 1, 0)
		rec = dummyVarGen.processRow(rec)

		loModel = FeedForwardNetwork(prFile)
		loModel.buildModel()
		yp = FeedForwardNetwork.modelPredict(loModel, [rec])
		print(yp)

	else:
		exitWithMsg("unknow operation")


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

# In[ ]:

import pandas as pd
# note: we need fips as str with prepended 0 for mapping to geojson in plotly
df_nyt_counties = pd.read_csv("../nyt_repo/us-counties.csv",
                              index_col="date",
                              parse_dates=True
                             )
df_state_fips_codes = pd.read_csv("../other_data/state_fips.tsv",
                                  index_col=None, sep='\t'
                                 )

df_nyt_counties = df_nyt_counties.rename(columns={"fips": "county_fips_idx"})
df_state_fips_codes = df_state_fips_codes.rename(columns={"fips": "state_fips_idx"})

# In[ ]:

# merge data
df_merged = df_nyt_counties.copy()
df_merged = df_merged.reset_index().merge(df_state_fips_codes, on="state")

# In[ ]:

# remap "unknown" counties and NYC, KC to eliminate nan's
def remap_county_fips(row):
    if row["county"] == "Unknown":
        if row["state_fips_idx"] > 56:
            # map territories (e.g. virgin islands) state and county fips code to their code
            return row["state_fips_idx"]
        else:
            # map us states with unknown locations to the state fips
            return row["state_fips_idx"] * -1000

    elif row["county"] == "New York City":
        return -100
    elif row["county"] == "Kansas City":
        return -200

    else:
        return row["county_fips_idx"]

df_merged["county_fips_idx"] = df_merged.apply(remap_county_fips, axis=1)
df_merged = df_merged.astype({"county_fips_idx": int, "state_fips_idx": int})

# In[ ]:

# make string version of fips for plotly to map to geo json
df_merged["county_fips_str"] = df_merged["county_fips_idx"].apply(lambda x: str(x))
df_merged["state_fips_str"] = df_merged["state_fips_idx"].apply(lambda x: str(x))

# In[ ]:

df_merged["county_fips_str"] = df_merged["county_fips_str"].apply(
    lambda x: "0" + x if (len(x) < 5 and int(x) > 0) else x
)

df_merged["state_fips_str"] = df_merged["state_fips_str"].apply(
    lambda x: "0" + x if (len(x) < 2) else x
)

# set index
df_merged = df_merged.set_index(["date", "state_fips_idx", "county_fips_idx"], drop=True)

# In[ ]:

# calculate new cases and deaths
df_merged[["new_cases", "new_deaths"]] = df_merged.groupby(level=2)[["cases", "deaths"]].diff()

def remap_first_cases(row):
    case_val = row["new_cases"]
    if pd.isnull(case_val):
        return row["cases"]
    else:
        return case_val

def remap_first_deaths(row):
    case_val = row["new_deaths"]
    if pd.isnull(case_val):
        return row["deaths"]
    else:
        return case_val

df_merged["new_cases"] = df_merged.apply(remap_first_cases, axis=1)
df_merged["new_deaths"] = df_merged.apply(remap_first_deaths, axis=1)

df_merged = df_merged.astype({"cases": int, "new_cases": int, "deaths": int, "new_deaths": int})

# reorder columns
df_merged = df_merged[["post_code", "state", "county",
                       "county_fips_str", "state_fips_str",
                       "cases", "new_cases",
                       "deaths", "new_deaths"]]

# sort index by date
df_merged = df_merged.sort_index(level=["date"], sort_remaining=True)

df_merged.to_csv("../model_data/nyt_county_data_cleaned.tsv", sep="\t", na_rep="NA")
df_merged.to_pickle("../model_data/nyt_county_data_cleaned.pkl")
# ## Create a dataframe for each unique county with "empty" data

# In[ ]:

df_fips_unique = df_merged.copy().reset_index()

# In[ ]:

fake_date = pd.Timestamp("2000-01-01 00:00:00")

# In[ ]:

df_fips_unique["cases"] = 0
df_fips_unique["deaths"] = 0
df_fips_unique["new_cases"] = 0
df_fips_unique["new_deaths"] = 0
df_fips_unique["date"] = fake_date
df_fips_unique = df_fips_unique.drop_duplicates().copy()

# ### Bring in counties from adj list not in nyt data

# In[ ]:

adj_code_map = pd.read_pickle("../model_data/county_adj_codes_map.pkl")
adj_code_map = adj_code_map.rename(columns={"county_fips": "county_fips_str"})
adj_code_map = adj_code_map.merge(df_state_fips_codes, on="post_code")

# In[ ]:

adj_code_map["county_fips_idx"] = adj_code_map["county_fips_str"].apply(lambda x: int(x))
adj_code_map["state_fips_str"] = adj_code_map["state_fips_idx"].apply(lambda x: str(x))
adj_code_map["state_fips_str"] = adj_code_map["state_fips_str"].apply(
    lambda x: "0" + x if (len(x) < 2) else x
)

# In[ ]:

adj_code_map["cases"] = 0
adj_code_map["deaths"] = 0
adj_code_map["new_cases"] = 0
adj_code_map["new_deaths"] = 0
adj_code_map["date"] = fake_date
adj_code_map = adj_code_map.drop_duplicates().copy()

# In[ ]:

nyt_fips_list = df_fips_unique.county_fips_idx.to_list()
new_counties = adj_code_map[~adj_code_map["county_fips_idx"].isin(nyt_fips_list)].reset_index(drop=True)

# ### Concatenate

# In[ ]:

df_fips_unique = pd.concat([df_fips_unique, new_counties])

# In[ ]:

df_fips_unique = df_fips_unique.set_index(["date", "state_fips_idx", "county_fips_idx"])
df_fips_unique = df_fips_unique.sort_index(level=0)

# In[ ]:

df_fips_unique.head()

# In[ ]:

assert (df_fips_unique.isna().all() == False).all() == True, "ERROR: nan in unique empty, did data format change?"

# In[ ]:

assert df_fips_unique.duplicated().sum() == 0, "ERROR: some rows in final unique empty are duplicated, what happened?"

# In[ ]:

df_fips_unique.to_csv("../model_data/nyt_empty_counties_frame_slice.tsv", sep='\t', na_rep="NA")
df_fips_unique.to_pickle("../model_data/nyt_empty_counties_frame_slice.pkl")

# ## Create a dataframe for mapping identifiers

# In[ ]:

df_fips_list_frame = df_fips_unique.copy().reset_index(drop=True)

# In[ ]:

df_fips_list_frame = df_fips_list_frame.drop(columns=["cases", "new_cases", "deaths", "new_deaths"])

# In[ ]:

df_fips_list_frame.to_csv("../model_data/nyt_counties_list_slice.tsv", sep='\t', na_rep="NA", index=False)
df_fips_list_frame.to_pickle("../model_data/nyt_counties_list_slice.pkl")

# ## Create a dataframe for a simple list of fips, as str (to include pre-fixed 0)

# In[ ]:

df_fips_list = df_fips_list_frame["county_fips_str"]

# In[ ]:

df_fips_list.to_csv("../model_data/model_counties_list.lst", sep='\t', index=False)


import sys

sys.stdin = open("input.txt", "r")

num = int(input())

for n in range(num):
    case = list(map(int, input().split()))
    stops = list(map(int, input().split()))
    bus = 0
    count = 0
    while bus < case[1]:
        wrong = case[0]
        for i in range(bus + case[0], bus, -1):
            bus = i
            if bus >= case[1]:
                break
            elif bus in stops:
                count += 1
                break
            wrong -= 1
            if wrong == 0:
                count = 0
                bus = case[1]
                break
    print('#%d %d' %(n+1, count))


"""
Test AtomicSpecification using API
"""

import json
from rest_framework.test import APITestCase
from rest_framework import status

from modular_assembly.atomic.models import AtomicComponent, AtomicPrerequisite
from modular_assembly.assembly.models import Blueprint

class AtomicSpecificationTestCase(APITestCase):

    def setUp(self):

        self.URL_VERSION = '/api/v1'

        self.rod = AtomicComponent.objects.create(
            sku="rod",
            description="rod of a gas spring",
            warehouse_location="2001",
            material="steel",
            weight=80,
            image="",
            availability=2,
        )
        self.tube = AtomicComponent.objects.create(
            sku="tube",
            description="tube of a gas spring",
            warehouse_location="2003",
            material="steel",
            weight=90,
            image="",
            availability=6,
        )
        self.seal = AtomicComponent.objects.create(
            sku="seal",
            description="seal of a gas spring",
            warehouse_location="1000",
            material="rubber",
            weight=4,
            image="",
            availability=8000,
        )
        self.endfitting1 = AtomicComponent.objects.create(
            sku="endfitting1",
            description="endfitting of a gas spring",
            warehouse_location="30",
            material="steal",
            weight=20,
            image="",
            availability=50,
        )
        self.endfitting2 = AtomicComponent.objects.create(
            sku="endfitting2",
            description="endfitting of a gas spring",
            warehouse_location="31",
            material="steal",
            weight=20,
            image="",
            availability=50,
        )

        url = self.URL_VERSION + '/assembly/blueprint/create/'
        payload = {
            "name": "gas spring",
            "atomic_prerequisites": [
                {
                    "atomic_component": self.rod.id,
                    "min_quantity": 100000,  # 100mm
                    "max_quantity": 800000,  # 800mm
                },
                {
                    "atomic_component": self.tube.id,
                    "min_quantity": 100000,  # 100mm
                    "max_quantity": 800000,  # 800mm
                },
                {
                    "atomic_component": self.seal.id,
                    "min_quantity": 1,
                    "max_quantity": 1,
                },
                {
                    "atomic_component": self.endfitting1.id,
                    "min_quantity": 0,
                    "max_quantity": 1,
                },
                {
                    "atomic_component": self.endfitting2.id,
                    "min_quantity": 0,
                    "max_quantity": 1,
                }
            ],
            "product_prerequisites": []
        }
        data = json.dumps(payload)
        self.client.post(url, data, content_type='application/json')

    def test_baseline(self):
        """
        Specify a product without choosing options
        """
        url = self.URL_VERSION + '/assembly/product/create/'
        payload = {
            "name": "gas spring",
            "sku": "GSP",
            "availability": 20,
            "blueprint": Blueprint.objects.filter(name="gas spring").first().id,
            "atomic_specifications": [
                {
                    "selected_component": self.rod.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.rod).first().id,
                    "quantity": 200000,  # 100mm
                },
                {
                    "selected_component": self.tube.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.tube).first().id,
                    "quantity": 200000  # 100mm
                },
                {
                    "selected_component": self.seal.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.seal).first().id,
                    "quantity": 1
                }
            ],
            "product_specifications": []
        }
        data = json.dumps(payload)
        response = self.client.post(url, data, content_type='application/json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_create_with_options(self):
        """
        Specify Gas Sprint with optional endfitting
        """
        url = self.URL_VERSION + '/assembly/product/create/'
        payload = {
            "name": "gas spring",
            "sku": "GSP",
            "availability": 20,
            "blueprint": Blueprint.objects.filter(name="gas spring").first().id,
            "atomic_specifications": [
                {
                    "selected_component": self.rod.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.rod).first().id,
                    "quantity": 200000,  # 100mm
                },
                {
                    "selected_component": self.tube.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.tube).first().id,
                    "quantity": 200000  # 100mm
                },
                {
                    "selected_component": self.seal.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.seal).first().id,
                    "quantity": 1
                },
                {
                    "selected_component": self.endfitting1.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.endfitting1).first().id,
                    "quantity": 0
                },
                {
                    "selected_component": self.endfitting2.id,
                    "prerequisite": AtomicPrerequisite.objects.filter(atomic_component=self.endfitting2).first().id,
                    "quantity": 1
                }
            ],
            "product_specifications": []
        }
        data = json.dumps(payload)
        response = self.client.post(url, data, content_type='application/json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)


from os import path, getcwd, listdir
import pandas as pd
import datetime
from pprint import pprint

class Prepare:

    def __init__(self):
        self.path = path.abspath(getcwd())
        self.datasetPath = path.join(self.path, 'data', 'dataset')
        self.datasetStaticPath = path.join(self.datasetPath, 'static')
        self.datasetPedestrianPath = path.join(self.datasetPath, 'pedestrian')
        self.modifiedPath = path.join(self.path, 'modified')
        self.peak = None

    def readCSV(self):
        print(f'[*] Current dataset Path: {self.datasetPath}')
        print(f'[*] Current dataset Static Path: {self.datasetStaticPath}')
        print(f'[*] Current dataset Pedestrian Path: {self.datasetPedestrianPath}')
        print('--------------------------------------------------------------------')
        csvFiles = listdir(self.datasetStaticPath)
        for csvFile in csvFiles:
            filepath = path.join(self.datasetStaticPath, csvFile)
            col_names = ['Timestamp', 'CellID', 'DL_bitrate', 'UL_bitrate', 'State']
            dataset = pd.read_csv(filepath, usecols=col_names)
            #test = dataset.head(10)
            for i, row in dataset.iterrows():
                ts = row['Timestamp']
                date_time_obj = datetime.datetime.strptime(ts, '%Y.%m.%d_%H.%M.%S')
                date = date_time_obj.date()
                time = date_time_obj.time()
                day_time = ''
                if time >= datetime.time(0) and time <= datetime.time(5,59):
                    day_time = 'Midnight'
                    #print('Midnight')

                if time >= datetime.time(6) and time <= datetime.time(11,59):
                    day_time = 'Morning'
                    #print('Morning')

                if time >= datetime.time(12) and time <= datetime.time(17,59):
                    day_time = 'Afternoon'
                    #print('Afternoon')

                if time >= datetime.time(18) and time <= datetime.time(23,59):
                    day_time = 'Night'
                    #print('Night')

                weekDays = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
                day = date.weekday()
                week_day = weekDays[day]
                dataset.loc[i, "WeekDay"] = week_day
                dataset.loc[i, "DayPeriod"] = day_time
                dataset.loc[i, "DL_UL"] = row['DL_bitrate'] + row['UL_bitrate']

            print(dataset.head(100))
            print(dataset.groupby('CellID').size())
            break
p = Prepare()

p.readCSV()
'''
            self.peak = dataset['DL_UL'].max()

            for i, row in dataset.iterrows():
                dataset.loc[i, "BW_RANK"] = self.BWRank(row['DL_UL'])

            print(dataset.head(100))
                #week_day = date_time_obj.date().weekday()
                #print(week_day)
                #print('Date:', date_time_obj.date())
                #print('Time:', date_time_obj.time())
                #print('Date-time:', date_time_obj)
            break

    def BWRank(self, tp, noise):

        percent_20 = (self.peak * 20)/100
        percent_21 = (self.peak * 21)/100
        percent_40 = (self.peak * 40)/100
        percent_41 = (self.peak * 41)/100
        percent_60 = (self.peak * 60)/100
        percent_61 = (self.peak * 61)/100
        percent_80 = (self.peak * 80)/100
        percent_81 = (self.peak * 81)/100

        if tp > 100 and tp <= percent_20:
            return '2'

        if tp >= percent_21 and tp <= percent_40:
            return '4'

        if tp >= percent_41 and tp <= percent_60:
            return '6'

        if tp >= percent_61 and tp <= percent_80:
            return '8'

        if tp >= percent_81:
            return '10'

        return '0'
        #if tp > 100 and tp <= ((self.peak * 20)/100)

 '''