import os
import json
import csv
import formatTickerIntoCSV as ticker_converter

def create_test_json_file(file_path):
    test_data = {
        "US": [
            {
                "ticker": {
                    "Code": "A",
                    "Name": "Agilent Technologies Inc",
                    "Country": "USA",
                    "Exchange": "NYSE",
                    "Currency": "USD",
                    "Type": "Common Stock",
                    "Isin": "US00846U1016"
                },
                "delisted": False
            }
        ],
        "LSE": [
            {
                "ticker": {
                    "Code": "0A00",
                    "Name": "Akzo Nobel N.V.",
                    "Country": "UK",
                    "Exchange": "LSE",
                    "Currency": "EUR",
                    "Type": "Common Stock",
                    "Isin": ""
                },
                "delisted": False
            }
        ]
    }
    with open(file_path, 'w') as f:
        json.dump(test_data, f)

def read_csv_file(file_path):
    with open(file_path, 'r', newline='', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        data = [row for row in reader]
    return data
def test_formatTickerIntoCSV():
    test_input_file = 'Data/test_input.json'
    test_output_file = 'Data/test_output.csv'
    ticker_converter.input_file = test_input_file
    ticker_converter.output_file = test_output_file

    create_test_json_file(test_input_file)
    ticker_converter.main()
    csv_data = read_csv_file(test_output_file)

    with open(test_input_file, 'r') as f:
        json_data = json.load(f)

    total_json_items = sum(len(ticker_list) for ticker_list in json_data.values())

    assert len(csv_data) == total_json_items, f"Expected {total_json_items} items in CSV, but got {len(csv_data)} items"

    expected_data = [
        {
            'Code': 'A',
            'Name': 'Agilent Technologies Inc',
            'Country': 'USA',
            'Exchange': 'NYSE',
            'Currency': 'USD',
            'Type': 'Common Stock',
            'Isin': 'US00846U1016',
            'Delisted': 'False'
        },
        {
            'Code': '0A00',
            'Name': 'Akzo Nobel N.V.',
            'Country': 'UK',
            'Exchange': 'LSE',
            'Currency': 'EUR',
            'Type': 'Common Stock',
            'Isin': '',
            'Delisted': 'False'
        }
    ]

    assert csv_data == expected_data, f"Expected {expected_data}, but got {csv_data}"

    os.remove(test_input_file)
    os.remove(test_output_file)

if __name__ == '__main__':
    test_formatTickerIntoCSV()


from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Union
import os

import fsspec

from dreamsboard.engine.constants import (
    DOC_STORE_KEY,
    INDEX_STORE_KEY,
)
from dreamsboard.engine.storage.template_store.simple_template_store import SimpleTemplateStore
from dreamsboard.engine.storage.template_store.types import DEFAULT_PERSIST_FNAME as TEMPLATE_STORE_FNAME
from dreamsboard.engine.storage.template_store.types import BaseTemplateStore
from dreamsboard.engine.storage.index_store.simple_index_store import SimpleIndexStore
from dreamsboard.engine.storage.index_store.types import (
    DEFAULT_PERSIST_FNAME as INDEX_STORE_FNAME,
)
from dreamsboard.engine.storage.index_store.types import BaseIndexStore
from dreamsboard.engine.utils import concat_dirs

DEFAULT_PERSIST_DIR = "./storage"

@dataclass
class StorageContext:
    """Storage context.

    The storage context container is a utility container for storing nodes,
    indices, and vectors. It contains the following:
    - template_store: BaseTemplateStore
    - index_store: BaseIndexStore

    """

    template_store: BaseTemplateStore
    index_store: BaseIndexStore

    @classmethod
    def from_defaults(
            cls,
            template_store: Optional[BaseTemplateStore] = None,
            index_store: Optional[BaseIndexStore] = None,
            persist_dir: Optional[str] = None,
            fs: Optional[fsspec.AbstractFileSystem] = None,
    ) -> "StorageContext":
        """Create a StorageContext from defaults.

        Args:
            template_store (Optional[BaseTemplateStore]): document store
            index_store (Optional[BaseIndexStore]): index store
            :param template_store:
            :param index_store:
            :param fs:
            :param persist_dir:

        """
        if persist_dir is None:
            template_store = template_store or SimpleTemplateStore()
            index_store = index_store or SimpleIndexStore()
        else:
            template_store = template_store or SimpleTemplateStore.from_persist_dir(
                persist_dir, fs=fs
            )
            index_store = index_store or SimpleIndexStore.from_persist_dir(
                persist_dir, fs=fs
            )

        return cls(template_store, index_store)

    def persist(
            self,
            persist_dir: Union[str, os.PathLike] = DEFAULT_PERSIST_DIR,
            template_store_fname: str = TEMPLATE_STORE_FNAME,
            index_store_fname: str = INDEX_STORE_FNAME,
            fs: Optional[fsspec.AbstractFileSystem] = None,
    ) -> None:
        """Persist the storage context.

        Args:
            persist_dir (str): directory to persist the storage context
            :param fs:
            :param persist_dir:
            :param index_store_fname:
            :param template_store_fname:
        """
        if fs is not None:
            persist_dir = str(persist_dir)  # NOTE: doesn't support Windows here
            template_store_path = concat_dirs(persist_dir, template_store_fname)
            index_store_path = concat_dirs(persist_dir, index_store_fname)
        else:
            persist_dir = Path(persist_dir)
            template_store_path = str(persist_dir / template_store_fname)
            index_store_path = str(persist_dir / index_store_fname)

        self.template_store.persist(persist_path=template_store_path, fs=fs)
        self.index_store.persist(persist_path=index_store_path, fs=fs)

    def to_dict(self) -> dict:
        all_simple = (
                isinstance(self.template_store, SimpleTemplateStore)
                and isinstance(self.index_store, SimpleIndexStore)
        )
        if not all_simple:
            raise ValueError(
                "to_dict only available when using simple doc/index/vector stores"
            )

        assert isinstance(self.template_store, SimpleTemplateStore)
        assert isinstance(self.index_store, SimpleIndexStore)

        return {
            DOC_STORE_KEY: self.template_store.to_dict(),
            INDEX_STORE_KEY: self.index_store.to_dict(),
        }

    @classmethod
    def from_dict(cls, save_dict: dict) -> "StorageContext":
        """Create a StorageContext from dict."""
        template_store = SimpleTemplateStore.from_dict(save_dict[DOC_STORE_KEY])
        index_store = SimpleIndexStore.from_dict(save_dict[INDEX_STORE_KEY])
        return cls(
            template_store=template_store,
            index_store=index_store,
        )


import cv2
import numpy as np
import os

IMG_SIZE=96

tp, rg, bt, lf = 100, 150, 400, 450

esc='**'

a=''

dataset_folder=input('Enter Folder Name : ')

try:
    os.mkdir(dataset_folder)
except:
    print('Containing folder is having same name ')

cam = cv2.VideoCapture(0)

while(True):

    a=input(' enter name of label or ** to exit : ')

    if a==esc:
        break

    label_dir=str(dataset_folder)+'/'+str(a)
    print(label_dir)

    try:
        os.mkdir(label_dir)
    except Exception as e:
        print('Exception Occoured',e)

    i=0

    while(True):
        (t, frame) = cam.read()

        frame = cv2.flip(frame, 1)

        roi = frame[tp:bt, rg:lf]
        gray_scale = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
        gray_scale = cv2.GaussianBlur(gray_scale, (7, 7), 0)

        gray_scale = cv2.resize(gray_scale, (IMG_SIZE,IMG_SIZE))

        cv2.imwrite("%s/%s/%d.jpg"%(dataset_folder,a,i),gray_scale)
        i+=1

        print(i)
        if i>300:
            break

        cv2.rectangle(frame, (lf, tp), (rg, bt), (0,255,0), 2)

        cv2.imshow("Video Feed 1", gray_scale)

        cv2.imshow("Video Feed", frame)
        # observe the keyprss by the user
        keyprss = cv2.waitKey(1)

        if keyprss == 27:
            break

cam.release()
cv2.destroyAllWindows()


import random
import typing as t

from toolbox.core.consts import PromptConstants
from toolbox.core.models import Episode, Turn
from toolbox.datasets.rallio67_instruct import Rallio67InstructDataset
from toolbox.modules import BaseModule

class Rallio67InstructPDM(BaseModule):
    '''Persona Dialogue Module based on instruction following data.'''

    SYNTHETIC_PERSONAS = [
        "An artificial intelligence that can generate meaningful responses in plain English",
        "A helpful AI assistant", "Very intelligent",
        "A large language model (LLM) capable of answering user questions",
        "Highly knowledgeable and intelligent AI assistant",
        "Does their best to answer the user's questions",
        "Knows a lot, and always attempts to tell the truth"
    ]

    def generator(self) -> t.Generator[Episode, None, None]:
        for episode in Rallio67InstructDataset():
            turns: list[Turn] = [
                Turn(utterance=episode.prompt,
                     speaker=PromptConstants.USER_PREFIX,
                     human_speaker=True),
                Turn(utterance=episode.response,
                     speaker=PromptConstants.BOT_TOKEN,
                     human_speaker=False),
            ]

            persona = {
                PromptConstants.BOT_TOKEN:
                    self._generate_synthetic_persona_string()
            }

            yield Episode(turns=turns, participant_personas=persona)

    def _generate_synthetic_persona_string(self) -> str:
        selected_personas = random.sample(self.SYNTHETIC_PERSONAS, 5)
        random.shuffle(selected_personas)
        return ". ".join(selected_personas) + "."


"""Tests for Domain Txn Handling Utils."""
import base58
import json

from copy import deepcopy

from unittest import TestCase

from ..domain_txn_handler import (
    _extract_attr_typed_value,
    parse_attr_txn,
    decode_state_value,
    extract_params_write_request,
    hash_of,
    make_state_path_for_attr,
    prepare_attr_for_state,
    prepare_nym_for_state,
    prepare_revoc_reg_entry_for_state,
    prepare_schema_for_state,
    prepare_get_claim_def_for_state,
    prepare_claim_def_for_state,
    prepare_revoc_def_for_state,
    prepare_get_revoc_reg_entry_for_state,
    prepare_revoc_reg_entry_accum_for_state,
)

CLAIM_DEF_TXN = {
    "result": {
        "txn": {
            "data": {
                "ver": 1,
                "signature_type": "CL",
                "ref": 10,
                "tag": "some_tag",
                "data": {"primary": "....", "revocation": "...."},
            },
            "metadata": {
                "reqId": 1514280215504647,
                "from": "L5AD5g65TDQr1PPHHRoiGf",
                "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
            },
        },
        "txnMetadata": {
            "txnTime": 1513945121,
            "seqNo": 10,
            "txnId": "HHAD5g65TDQr1PPHHRoiGf2L5AD5g65TDQr1PPHHRoiGf1|Degree1|CL|key1",
        },
    }
}

REVOC_REG_ENTRY_TXN = {
    "result": {
        "txn": {
            "data": {
                "ver": 1,
                "revocRegDefId": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                "revocDefType": "CL_ACCUM",
                "value": {
                    "accum": "accum_value",
                    "prevAccum": "prev_acuum_value",
                    "issued": [],
                    "revoked": [10, 36, 3478],
                },
            },
        },
        "txnMetadata": {
            "txnTime": 1513945121,
            "seqNo": 10,
            "txnId": "5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
        },
    }
}

class TestDomainTxnHandler(TestCase):
    """Domain Txn Handler Tests"""

    def test_extract_attr_typed_value(self):
        test_txn_data = {"test": {...}}
        with self.assertRaises(ValueError) as cm:
            _extract_attr_typed_value(test_txn_data)
            assert "ATTR should have one" in cm
        test_txn_data = {
            "raw": {...},
            "enc": {...},
            "hash": {...},
        }
        with self.assertRaises(ValueError) as cm:
            _extract_attr_typed_value(test_txn_data)
            assert "ATTR should have only one" in cm

    def test_parse_attr_txn(self):
        test_txn_data = {"raw": '{"name": "Alice"}'}
        assert parse_attr_txn(test_txn_data) == ("raw", "name", '{"name": "Alice"}')
        test_txn_data = {"enc": "test"}
        assert parse_attr_txn(test_txn_data) == ("enc", "test", "test")
        test_txn_data = {"hash": "test"}
        assert parse_attr_txn(test_txn_data) == ("hash", "test", None)

    def test_decode_state_value(self):
        test_value = "test_value"
        test_lsn = "100"
        test_lut = "test_lut"
        test_encoded = {"val": test_value, "lsn": test_lsn, "lut": test_lut}
        assert decode_state_value(json.dumps(test_encoded)) == (
            test_value,
            test_lsn,
            test_lut,
        )

    def test_hash_of(self):
        test = {"test": "test"}
        assert hash_of(test)
        test = "123"
        assert hash_of(test)
        test = b"234"
        assert hash_of(test)

    def test_make_state_path_for_attr(self):
        assert b"did1:1:attrName1" == make_state_path_for_attr(
            "did1", "attrName1", attr_is_hash=True
        )
        assert (
            b"did1:1:677a81e8649df8f1a1e8af7709a5ece1d965cb684b2c185272114c5cc3b7ec49"
            == make_state_path_for_attr("did1", "attrName1", attr_is_hash=False)
        )
        assert (
            b"did1:1:677a81e8649df8f1a1e8af7709a5ece1d965cb684b2c185272114c5cc3b7ec49"
            == make_state_path_for_attr("did1", "attrName1")
        )

    def test_prepare_attr_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "data": {
                        "ver": 1,
                        "dest": "N22KY2Dyvmuu2PyyqSFKue",
                        "raw": '{"name":"Alice"}',
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "N22KY2Dyvmuu2PyyqSFKue|02",
                },
            }
        }
        txn = txn.get("result")
        path, value_bytes = prepare_attr_for_state(txn)
        assert (
            path
            == b"N22KY2Dyvmuu2PyyqSFKue:1:82a3537ff0dbce7eec35d69edc3a189ee6f17d82f353a553f9aa96cb0be3ce89"
        )
        assert (
            value_bytes
            == b'{"lsn": 10, "lut": 1513945121, "val": "6d4a333838d0ef96756cccc680af2531075c512502fb68c5503c63d93de859b3"}'
        )
        path = prepare_attr_for_state(txn, path_only=True)
        assert (
            path
            == b"N22KY2Dyvmuu2PyyqSFKue:1:82a3537ff0dbce7eec35d69edc3a189ee6f17d82f353a553f9aa96cb0be3ce89"
        )

    def test_prepare_nym_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "data": {
                        "dest": "N22KY2Dyvmuu2PyyqSFKue",
                    },
                },
            }
        }
        txn = txn.get("result")
        assert prepare_nym_for_state(txn)

    def test_prepare_schema_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "type": "101",
                    "protocolVersion": 2,
                    "data": {
                        "ver": 1,
                        "data": {
                            "name": "Degree",
                            "version": "1.0",
                            "attr_names": [
                                "undergrad",
                                "last_name",
                                "first_name",
                                "birth_date",
                                "postgrad",
                                "expiry_date",
                            ],
                        },
                    },
                    "metadata": {
                        "reqId": 1514280215504647,
                        "from": "L5AD5g65TDQr1PPHHRoiGf",
                        "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                        "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                        "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "L5AD5g65TDQr1PPHHRoiGf1|Degree|1.0",
                },
            }
        }
        txn = txn.get("result")
        path, value_bytes = prepare_schema_for_state(txn)
        assert path == b"L5AD5g65TDQr1PPHHRoiGf:2:Degree:1.0"
        assert (
            prepare_schema_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:2:Degree:1.0"
        )

    def test_prepare_get_claim_def_for_state(self):
        txn = deepcopy(CLAIM_DEF_TXN)
        txn.get("result").get("txn").get("data").pop("ref")
        with self.assertRaises(ValueError) as cm:
            prepare_get_claim_def_for_state(txn)
            assert "ref field is absent, but it must contain schema seq no" in cm

    def test_prepare_claim_def_for_state(self):
        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        txn.get("txn").get("data").pop("ref")
        with self.assertRaises(ValueError) as cm:
            prepare_claim_def_for_state(txn)
            assert "ref field is absent, but it must contain schema seq no" in cm

        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        txn.get("txn").get("data").pop("data")
        with self.assertRaises(ValueError) as cm:
            prepare_claim_def_for_state(txn)
            assert "data field is absent, but it must contain components of keys" in cm

        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        path, value_bytes = prepare_claim_def_for_state(txn)
        assert path == b"L5AD5g65TDQr1PPHHRoiGf:3:CL:10:some_tag"
        assert (
            prepare_claim_def_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:3:CL:10:some_tag"
        )

    def test_prepare_revoc_def_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "type": "113",
                    "protocolVersion": 2,
                    "data": {
                        "ver": 1,
                        "id": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                        "credDefId": "FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag",
                        "revocDefType": "CL_ACCUM",
                        "tag": "tag1",
                        "value": {
                            "maxCredNum": 1000000,
                            "tailsHash": "6619ad3cf7e02fc29931a5cdc7bb70ba4b9283bda3badae297",
                            "tailsLocation": "http://tails.location.com",
                            "issuanceType": "ISSUANCE_BY_DEFAULT",
                            "publicKeys": {},
                        },
                    },
                    "metadata": {
                        "reqId": 1514280215504647,
                        "from": "L5AD5g65TDQr1PPHHRoiGf",
                        "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                        "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                        "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                },
            },
        }
        txn = txn.get("result")
        path, value_bytes = prepare_revoc_def_for_state(txn)
        assert (
            path
            == b"L5AD5g65TDQr1PPHHRoiGf:4:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        assert (
            prepare_revoc_def_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:4:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_prepare_get_revoc_reg_entry_for_state(self):
        assert prepare_get_revoc_reg_entry_for_state(REVOC_REG_ENTRY_TXN)
        assert prepare_get_revoc_reg_entry_for_state(REVOC_REG_ENTRY_TXN.get("result"))

    def test_prepare_revoc_reg_entry_for_state(self):
        path, value_bytes = prepare_revoc_reg_entry_for_state(
            REVOC_REG_ENTRY_TXN.get("result")
        )
        assert (
            path
            == b"5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        assert (
            prepare_revoc_reg_entry_for_state(
                REVOC_REG_ENTRY_TXN.get("result"), path_only=True
            )
            == b"5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_prepare_revoc_reg_entry_accum_for_state(self):
        path, value_bytes = prepare_revoc_reg_entry_accum_for_state(REVOC_REG_ENTRY_TXN)
        assert (
            path
            == b"6:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        path, value_bytes = prepare_revoc_reg_entry_accum_for_state(
            REVOC_REG_ENTRY_TXN.get("result")
        )
        assert (
            path
            == b"6:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_extract_params_write_request(self):
        write_request = {
            "result": {
                "txn": {
                    "data": {
                        "data": {
                            "attr_names": ["axuall_proof_id"],
                            "name": "manual_issuance",
                            "version": "4.1.0",
                        }
                    },
                    "metadata": {
                        "digest": "f440da62ab1c38601e41b5e148370b64af3183a2428f327cfb8ac83ca4cbc698",
                        "from": "F72i3Y3Q4i466efjYJYCHM",
                        "payloadDigest": "0ecfd475ed34635b6623bcbcda422ff90dee53f66de20a7441bca04ba0632670",
                        "reqId": 1634854863739554300,
                        "taaAcceptance": {
                            "mechanism": "for_session",
                            "taaDigest": "8cee5d7a573e4893b08ff53a0761a22a1607df3b3fcd7e75b98696c92879641f",
                            "time": 1600300800,
                        },
                    },
                    "protocolVersion": 2,
                    "type": "101",
                },
                "txnMetadata": {
                    "seqNo": 75335,
                    "txnId": "F72i3Y3Q4i466efjYJYCHM:2:manual_issuance:4.1.0",
                    "txnTime": 1634854863,
                },
                "ver": "1",
                "auditPath": [
                    "DhzuS5ZkyxUrQbRzbCfq3NDcTnTzFcJHYJYyTAqgMymT",
                    "AtYz5H13m1ZyFiGVVLwRL7au8muhCMwikeQrMwJBJ5d9",
                    "3GpqpZw4jymXDFm8Mf5gSMHSJ8r1wXutiZ1x26kxwWBE",
                    "EQyUpv9z64prPmDqkb8n2SzBtor3tGaNw4qZP2psG2wD",
                    "9fbpJjWbDuNbCrC7GQxjVxDGJ48k7rdBDf5ATu7PXVx3",
                    "CRbATGbi9DbN1pphXMcREVZTUUqsQbD9mN5ikKm1FkSc",
                    "E19R3Cty6iLtWBoxSqHLrxTSJSKpBtj3wJEdD98pS9g8",
                ],
                "ledgerSize": 75335,
                "reqSignature": {
                    "type": "ED25519",
                    "values": [
                        {
                            "from": "F72i3Y3Q4i466efjYJYCHM",
                            "value": "29sKMwQjm6r2BjpbJdFi1TGvowaxbi9nnAPP9LwPHhuBzwtfWwqJFg4Ur2xAWBLYfPLnMNNSgwwNesRSb1C1L72d",
                        }
                    ],
                },
                "rootHash": "4aM6yCpamk82Uqb414mNRpEdYkwdSMhi3HkXgN7YvRaX",
            }
        }
        txn = deepcopy(write_request)
        (
            tree_size,
            leaf_index,
            decoded_audit_path,
            expected_root_hash,
        ) = extract_params_write_request(txn)

        given_audit_path = [
            "DhzuS5ZkyxUrQbRzbCfq3NDcTnTzFcJHYJYyTAqgMymT",
            "AtYz5H13m1ZyFiGVVLwRL7au8muhCMwikeQrMwJBJ5d9",
            "3GpqpZw4jymXDFm8Mf5gSMHSJ8r1wXutiZ1x26kxwWBE",
            "EQyUpv9z64prPmDqkb8n2SzBtor3tGaNw4qZP2psG2wD",
            "9fbpJjWbDuNbCrC7GQxjVxDGJ48k7rdBDf5ATu7PXVx3",
            "CRbATGbi9DbN1pphXMcREVZTUUqsQbD9mN5ikKm1FkSc",
            "E19R3Cty6iLtWBoxSqHLrxTSJSKpBtj3wJEdD98pS9g8",
        ]
        expected_audit_path = [
            base58.b58decode(hash_str.encode("utf-8")) for hash_str in given_audit_path
        ]
        expected_hash = base58.b58decode(
            "4aM6yCpamk82Uqb414mNRpEdYkwdSMhi3HkXgN7YvRaX".encode("utf-8")
        )
        assert tree_size == 75335
        assert leaf_index == 75334
        assert expected_root_hash == expected_hash
        assert decoded_audit_path == expected_audit_path

        txn = deepcopy(write_request)
        txn = txn.get("result")
        (
            tree_size,
            leaf_index,
            decoded_audit_path,
            expected_root_hash,
        ) = extract_params_write_request(txn)

        assert tree_size == 75335
        assert leaf_index == 75334
        assert expected_root_hash == expected_hash
        assert decoded_audit_path == expected_audit_path

        txn = deepcopy(write_request)
        txn["result"]["txnMetadata"]["seqNo"] = 1000
        with self.assertRaises(Exception) as cm:
            extract_params_write_request(txn)
            assert "auditPath length does not match with given seqNo" in cm


from flask import request
from flask_restful import Resource
from employeeRepository import EmployeeRepository

class Employee(Resource):

    def get(self, employee_id=None):
        employeeRepository = EmployeeRepository()
        if employee_id is None:
            return employeeRepository.find_all()
        else:
            return employeeRepository.find(str(employee_id))

    def post(self):
        employee = request.get_json()
        employeeRepository = EmployeeRepository()
        return employeeRepository.insert_new_employee(str(employee['id']), employee['name'], employee['address'])


#!/usr/bin/env python

"""
web
----

Word Embeddings Benchmarks
"""

from __future__ import print_function

import os.path as op
import io
import sys

from   setuptools import setup, find_packages
from   setuptools.command.test import test as TestCommand

# long description
def read(*filenames, **kwargs):
    encoding = kwargs.get('encoding', 'utf-8')
    sep = kwargs.get('sep', '\n')
    buf = []
    for filename in filenames:
        with io.open(filename, encoding=encoding) as f:
            buf.append(f.read())
    return sep.join(buf)

# Get version without importing, which avoids dependency issues
MODULE_NAME = find_packages(exclude=['tests'])[0]
VERSION_PYFILE = op.join(MODULE_NAME, 'version.py')
# set __version__ variable
exec (compile(read(VERSION_PYFILE), VERSION_PYFILE, 'exec'))

# INSTALL_REQUIRES = list(parse_requirements('requirements.txt'))
# req_files = ['requirements.txt', 'pip_requirements.txt']

LICENSE = 'New BSD'

setup_dict = dict(
    name=MODULE_NAME,
    version=__version__,
    description='Word Embedding Benchmarks',

    license='New BSD',
    author='Stanislaw Jastrzebski',
    author_email='grimghil@gmail.com',
    maintainer='Stanislaw Jastrzebski',
    maintainer_email='grimghil@gmail.com',

    packages=find_packages(),

    install_requires=['numpy',
                      'scipy',
                      'scikit-learn'],

    extra_files=['CHANGES.rst', 'COPYING', 'README.rst'],

    scripts=[],

    long_description=read('README.rst', 'CHANGES.rst'),

    platforms='Linux/MacOSX',

    # https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        'Programming Language :: Python',
        'Development Status :: 1 - Alpha',
        'Natural Language :: English',
        'Environment :: Console',
        'Intended Audience ::  Machine Learning Research',
        'License :: OSI Approved ::' + LICENSE,
        'Operating System :: OS Independent',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Topic :: Scientific/Engineering :: Machine Learning',
        'Topic :: Scientific/Engineering :: Information Analysis',
        'Operating System :: POSIX',
        'Operating System :: Unix',
        'Operating System :: MacOS',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
    ],

    extras_require={
        'testing': ['pytest', 'pytest-cov'],
    }
)

# Python3 support keywords
if sys.version_info >= (3,):
    setup_dict['use_2to3'] = False
    setup_dict['convert_2to3_doctests'] = ['']
    setup_dict['use_2to3_fixers'] = ['']

class PyTest(TestCommand):
    user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = []

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        # import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.pytest_args)
        sys.exit(errno)

setup_dict.update(dict(tests_require=['pytest'],
                       cmdclass={'test': PyTest}))

if __name__ == '__main__':
    setup(**setup_dict)


import torch
import torch.nn as nn

from math import sqrt

"""VDSR Model

Very Deep Super Resolution (VDSR)

Site:
  "Accurate Image Super-Resolution Using Very Deep Convolutional Networks (2015)" (https://arxiv.org/abs/1511.04587)
"""

class VDSR(nn.Module):
  def __init__(self, c: int = 3, f: int = 3, n: int = 64, d: int = 20) -> None:
    """VDSR's Constructor

    Args:
      c (int): number of channel the input/output image.
      f (int): spatial size of region.
      n (int): number of feature map.
      d (int): number of layers. d - 1 residual layers and one output layer.

    Examples:
      >>> VSDR() # typical VDSR parameters
    """
    super().__init__()
    # Input Layer
    self.input = nn.Sequential(
      nn.Conv2d(in_channels=c, out_channels=n, kernel_size=f, padding=f//2, bias=False),
      nn.ReLU(inplace=True)
    )
    # Residual Layer
    self.residual = nn.Sequential(
      *sum([[
        nn.Conv2d(in_channels=n, out_channels=n, kernel_size=f, padding=f//2, bias=False),
        nn.ReLU(inplace=True)
      ] for _ in range(max(0, d - 1))], []),
    )
    # Output Layer
    self.output = nn.Conv2d(in_channels=n, out_channels=c, kernel_size=f, padding=f//2, bias=False)

    for m in self.modules():
      if isinstance(m, nn.Conv2d):
        m.weight.data.normal_(0, sqrt(2. / (m.kernel_size[0] * m.kernel_size[1] * m.out_channels)))
        if m.bias is not None: m.bias.data.zero_()

  def forward(self, x):
    input = x
    x = self.input(x)
    x = self.residual(x)
    x = self.output(x)
    x = torch.add(x, input)
    return x


# ----------------------------------------------------------------------------------------------------------------------
#  Imports
# ----------------------------------------------------------------------------------------------------------------------

import pyodbc
from typing import List
from DICOMTransit.redcap.constants import *
from DICOMTransit.redcap.enums import Database, Field, DataType
from DICOMTransit.redcap.transaction import RedcapTransaction

# ----------------------------------------------------------------------------------------------------------------------
#  Local ODBC
# ----------------------------------------------------------------------------------------------------------------------

def get_database_column_names(
    table_info, transaction: RedcapTransaction
) -> List[Field]:
    """
    Returns a list of fields contained within a database table.
    :param table_info: Table Information
    :param transaction: RedcapTransaction
    :return: List of fields or None if table not found.
    """
    if table_info is None:
        return None

    # If this is the first time the column names are requested for this table
    if table_info[DATABASE_TABLE_NAME].lower() not in transaction.database_column_names:
        # Get column names from database.

        conn = pyodbc.connect(get_connection_string(table_info[DATABASE]))
        odbc_cursor = conn.cursor()

        result = odbc_cursor.execute(
            "SELECT * FROM [" + table_info[DATABASE_TABLE_NAME].lower() + "] WHERE 1=0"
        )
        database_columns = [tuple[0] for tuple in result.description]

        odbc_cursor.close()
        conn.close

        # Store a copy of column names for this table in local memory.
        transaction.database_column_names[
            table_info[DATABASE_TABLE_NAME].lower()
        ] = database_columns

    else:
        # Get column names from cache.
        database_columns = transaction.database_column_names[
            table_info[DATABASE_TABLE_NAME].lower()
        ]

    return database_columns

def get_case_ids(transaction: RedcapTransaction) -> List[int]:
    """
    Get Case Ids
    :param transaction: RedcapTransaction
    :return: List of all the case ids related to the current hospital record number or None if table not found.
    """

    case_ids = []

    primary_key_filter_name = Field.HospitalRecordNumber.name
    primary_key_filter_value = str(
        transaction.get_primary_key_value(Field.HospitalRecordNumber.value)
    )

    if (
        primary_key_filter_name == ""
        or primary_key_filter_value == ""
        or primary_key_filter_value == "None"
    ):
        return None

    select_statement = (
        "SELECT [CaseId] FROM [Admission] WHERE ["
        + primary_key_filter_name
        + "] = '"
        + primary_key_filter_value
        + "'"
    )

    conn = pyodbc.connect(get_connection_string(1))
    odbc_cursor = conn.cursor()

    odbc_cursor.execute(select_statement)

    data = odbc_cursor.fetchall()

    for index_case_id in range(len(data)):

        # Add Case Id to list of Case Ids.
        case_ids.append(data[index_case_id][0])

    odbc_cursor.close()
    conn.close

    return case_ids

def get_cnfunid_by_cnnid(cnnid: int) -> int:
    """
    Get cnfun id using the cnn id ( cnn - case id )
    :param cnnid: int (cnnid (case id))
    :return: int cnfun patient id (Return -1 if no match found)
    """

    # Preparing CNN database - SQL request.
    select_statement = (
        "SELECT baby.PatientUI FROM Admission admission, Baby baby WHERE admission.CaseId = '"
        + cnnid
        + "' AND admission.BabyId=baby.BabyId"
    )

    # Connecting to CNN database.
    conn = pyodbc.connect(get_connection_string(1))
    odbc_cursor = conn.cursor()

    # Executing SQL request.
    odbc_cursor.execute(select_statement)
    data = odbc_cursor.fetchone()

    # Closing connection.
    odbc_cursor.close()
    conn.close

    # If not found, we return -1.
    if data == None:
        return -1

    # Preparing CNFUN database - SQL request.
    patientui = data[0]
    select_statement = (
        "SELECT PatientId FROM Patients WHERE CNNPatientUI = '" + patientui + "'"
    )

    # Connecting to CNFUN database.
    conn = pyodbc.connect(get_connection_string(2))
    odbc_cursor = conn.cursor()

    # Executing SQL request.
    odbc_cursor.execute(select_statement)
    data = odbc_cursor.fetchone()

    # Closing connection.
    odbc_cursor.close()
    conn.close

    # If not found, we return -1.
    if data == None:
        return -1

    # Return CNFUN - patient id.
    patientid = data[0]
    return patientid

def get_cnfunid_by_mrn(mrn: int) -> int:
    """
    Get cnfun id using the mrn id ( Medical record number )
    :param mrn: int (mrn - Medical record number)
    :return: int cnfun patient id (Return -1 if no match found)
    """

    # Preparing CNN database - SQL request.
    select_statement = (
        "SELECT baby.PatientUI FROM Admission admission, Baby baby WHERE admission.HospitalRecordNumber = '"
        + mrn
        + "' AND admission.BabyId=baby.BabyId"
    )

    # Connecting to CNN database.
    conn = pyodbc.connect(get_connection_string(1))
    odbc_cursor = conn.cursor()

    # Executing SQL request.
    odbc_cursor.execute(select_statement)
    data = odbc_cursor.fetchone()

    # Closing connection.
    odbc_cursor.close()
    conn.close

    # If not found, we return -1.
    if data == None:
        return -1

    # Preparing CNFUN database - SQL request.
    patientui = data[0]
    select_statement = (
        "SELECT PatientId FROM Patients WHERE CNNPatientUI = '" + patientui + "'"
    )

    # Connecting to CNFUN database.
    conn = pyodbc.connect(get_connection_string(2))
    odbc_cursor = conn.cursor()

    # Executing SQL request.
    odbc_cursor.execute(select_statement)
    data = odbc_cursor.fetchone()

    # Closing connection.
    odbc_cursor.close()
    conn.close

    # If not found, we return -1.
    if data == None:
        return -1

    # Return CNFUN - patient id.
    patientid = data[0]
    return patientid

def get_data_rows_for_patient_table(table_info, transaction: RedcapTransaction) -> list:
    """
    Gets all rows of data for a specific patient table.
    :param table_info: Table Information
    :param transaction: RedcapTransaction
    :return: List of all the rows obtained from the query or None if table not found.
    """
    if table_info is None:
        return None

    if table_info[DATABASE_TABLE_NAME] == "":
        return None

    primary_key_filter_name = str(get_primary_key_name(table_info[PRIMARY_KEY_NAME]))
    primary_key_filter_value = str(
        transaction.get_primary_key_value(table_info[PRIMARY_KEY_VALUE])
    )
    primary_key_data_type = get_primary_key_data_type(table_info[PRIMARY_KEY_VALUE])

    if (
        primary_key_filter_name == ""
        or primary_key_filter_value == ""
        or primary_key_filter_value == "None"
    ):
        return None

    if primary_key_data_type == DataType.String.value:
        select_statement = (
            "SELECT * FROM ["
            + table_info[DATABASE_TABLE_NAME]
            + "] WHERE ["
            + primary_key_filter_name
            + "] = '"
            + primary_key_filter_value
            + "'"
        )
    elif primary_key_data_type == DataType.Integer.value:
        select_statement = (
            "SELECT * FROM ["
            + table_info[DATABASE_TABLE_NAME]
            + "] WHERE ["
            + primary_key_filter_name
            + "] = "
            + primary_key_filter_value
        )
    else:
        select_statement = ""

    if select_statement != "":

        conn = pyodbc.connect(get_connection_string(table_info[DATABASE]))
        odbc_cursor = conn.cursor()

        odbc_cursor.execute(select_statement)

        data = odbc_cursor.fetchall()

        odbc_cursor.close()
        conn.close

        return data

    else:
        return None

def get_data_rows_for_reference_table(table_info) -> list:
    """
    Gets all rows of data for a specific reference table.
    :param table_info: Table Information
    :return: List of all the rows obtained from the query or None if table not found.
    """
    if table_info is not None:
        if table_info[DATABASE_TABLE_NAME] != "":

            select_statement = "SELECT * FROM [" + table_info[DATABASE_TABLE_NAME] + "]"

            conn = pyodbc.connect(get_connection_string(table_info[DATABASE]))
            odbc_cursor = conn.cursor()

            odbc_cursor.execute(select_statement)

            data = odbc_cursor.fetchall()

            odbc_cursor.close()
            conn.close

            return data

        else:
            return None
    else:
        return None

def get_connection_string(database) -> str:
    """
    Get Connection String
    :param database: Database Configuration Number
    :return: Connection String
    """
    if database == Database.CNN.value:
        return cnn_connection_string
    elif database == Database.CNFUN.value:
        return cnfun_connection_string
    else:
        return ""

def get_primary_key_name(primary_key) -> str:
    """
    Get Primary Key Name
    :param primary_key: Primary Key Configuration Number
    :return: String - Primary Key Name
    """
    if primary_key == Field.BabyId.value:
        return Field.BabyId.name
    elif primary_key == Field.CaseId.value:
        return Field.CaseId.name
    elif primary_key == Field.CNNPatientUI.value:
        return Field.CNNPatientUI.name
    elif primary_key == Field.HospitalRecordNumber.value:
        return Field.HospitalRecordNumber.name
    elif primary_key == Field.MotherId.value:
        return Field.MotherId.name
    elif primary_key == Field.PatientId.value:
        return Field.PatientId.name
    elif primary_key == Field.PatientUI.value:
        return Field.PatientUI.name
    elif primary_key == Field.MasterId.value:
        return Field.MasterId.name
    else:
        return Field.Unknown.name

def get_primary_key_data_type(primary_key) -> str:
    """
    Get Primary Key Data Type
    :param primary_key: Primary Key Configuration Number
    :return: String - Data Type Configuration Value
    """
    if primary_key == Field.BabyId.value:
        return DataType.Integer.value
    elif primary_key == Field.CaseId.value:
        return DataType.String.value
    elif primary_key == Field.CNNPatientUI.value:
        return DataType.String.value
    elif primary_key == Field.HospitalRecordNumber.value:
        return DataType.String.value
    elif primary_key == Field.MotherId.value:
        return DataType.Integer.value
    elif primary_key == Field.PatientId.value:
        return DataType.Integer.value
    elif primary_key == Field.PatientUI.value:
        return DataType.String.value
    elif primary_key == Field.MasterId.value:
        return DataType.Integer.value
    else:
        return DataType.Unknown.value


# -*- coding: utf-8 -*-
"""
Created on Tue Dec  1 22:54:14 2020

@author: Rodrigo
"""

# -*- coding: utf-8 -*-

from texttable import Texttable
import qprompt
from tabulate import tabulate
import time
start_time = time.time()

def crearGrafo():
    g = Grafo()
    while 1:

        menu = qprompt.Menu()
        menu.add("1", "Agregar nodo")
        menu.add("2", "Agregar arista")
        menu.add("3", "Bellman-Ford")
        menu.add("4", "Mostrar")
        menu.add("5", "Salir")
        choice = menu.show()

        if choice == "1":
            valor = input("Ingrese el numero del nodo: ")
            if valor not in g:
                g.agregarNodo(valor)
            else:
                print('El nodo ya existe.')
        elif choice == '2':
            origen = input("Ingrese el nodo origen: ")
            destino = input("Ingrese el nodo destino: ")
            peso = input("Ingrese el peso: ")
            if origen not in g.nodos:
                print('El nodo {} no existe'.format(origen))
            elif destino not in g.nodos:
                print('El nodo {} no existe.'.format(destino))
            else:
                if not g.existe(origen, destino):
                    g.agregarArista(origen, destino, peso)
                else:
                    print('La arista ya existe.')

        elif choice == '3':
            valor = input("Ingrese el nodo: ")
            origen = g.getNodo(valor)
            distancia = bellman_ford(g, origen)

            if distancia != 1:

                nodos = []
                for nodo in distancia:
                    nodos.append((nodo.get_key(), distancia[nodo]))
                nodos =sorted(nodos, key=lambda tup: tup[0])

                tabla = Texttable()
                row = ["Nodo"]
                row2 = ["Distancia"]
                for nodo in nodos:
                    row.append(nodo[0])
                    row2.append(nodo[1])
                tabla.add_row(row)
                tabla.add_row(row2)
                print(tabla.draw())
            else:
                print("Contiene pesos negativos")
        elif choice == "4":
            print('Nodos: ')
            for x in g:
                print(x.get_key())
            print()

            print('Aristas: ')
            for x in g:
                for destino in x.getNodoAdyacentes():
                    w = x.peso(destino)
                    print('(Origen=: {}, Destino: {}, peso: {}) '.format(x.get_key(),
                                                                 destino.get_key(), w))
            print()

        elif choice == '5':
            break

class Grafo:
    def __init__(self):
        self.nodos = {}

    def agregarNodo(self, numero):
        nodo = Nodo(numero)
        self.nodos[numero] = nodo

    def agregarArista(self, origen, destino, peso=1):
        self.nodos[origen].agregarAdyacente(self.nodos[destino], peso)

    def getNodo(self, numero):
        return self.nodos[numero]

    def __iter__(self):
        return iter(self.nodos.values())

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

    def existe(self, origen, destino):
        return self.nodos[origen].adyacente(self.nodos[destino])

class Nodo:
    def __init__(self, nro_nodo):
        self.nro_nodo = nro_nodo
        self.nodos_adyacentes = {}

    def get_key(self):
        return self.nro_nodo

    def agregarAdyacente(self, dest, peso):
        self.nodos_adyacentes[dest] = peso

    def getNodoAdyacentes(self):
        return self.nodos_adyacentes.keys()

    def adyacente(self, dest):
        if dest in self.nodos_adyacentes:
            return True
        else:
            return False

    def peso(self, dest):
        return self.nodos_adyacentes[dest]

def bellman_ford(grafo, origen):
    distancia = dict.fromkeys(grafo, float('inf'))
    distancia[origen] = 0

    for _ in range(len(grafo) - 1):
        for nodo in grafo:
            for adyacente in nodo.getNodoAdyacentes():
                if int(nodo.peso(adyacente)) < 0 :
                    return 1
                distancia[adyacente] = min(distancia[adyacente], distancia[nodo] + int(nodo.peso(adyacente)))

    return distancia

def main():
    crearGrafo()
    print("--- %s seconds ---" % (time.time() - start_time))

if __name__ == "__main__": main()


import logging
from flask import Flask, render_template, request, session, flash
from pydantic import BaseModel, validator, ValidationError
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
from flask_migrate import Migrate
from flask_wtf import CSRFProtect
from flask_login import LoginManager

################################
# Configuration
################################
# Create a naming convention for the database tables
convention = {
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
}
metadata = MetaData(naming_convention=convention)
database = SQLAlchemy(metadata=metadata)
#This is not attached to the Flask application yet
db_migration = Migrate()
csrf_protection = CSRFProtect()
login = LoginManager()
login.login_view = 'users.login' #Specify the view on which login performed

################################
# Helper Functions
################################

def initialize_extensions(app):
    """
    Since the application instance is now created, pass it to each Flask
    extension instance to bind it to the Flask application instance (app)
    """
    #All Flask extensions need to be initialized here
    database.init_app(app)
    db_migration.init_app(app, database, render_as_batch=True)
    csrf_protection.init_app(app)
    login.init_app(app)

    from src.models import User

    @login.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))

def configure_logging(app):
    """
    Define logging format and level in app
    """
    file_handler = logging.FileHandler('flask_recipe_app.log')
    file_formatter = logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(filename)s:%(lineno)d]')  # NEW!
    file_handler.setFormatter(file_formatter)
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)
    app.logger.info('Recipe App is Starting...')

def register_blueprints(app):
    """
    Register blueprints app
    """
    from src.ingredients import ingredients_blueprint
    from src.users import users_blueprint
    from src.recipes import recipes_blueprint
    from src.basket import baskets_blueprint

    app.register_blueprint(ingredients_blueprint)
    app.register_blueprint(users_blueprint)
    app.register_blueprint(recipes_blueprint)
    app.register_blueprint(baskets_blueprint)

def register_error_pages(app):
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('404.html'), 404

def create_app(config=None):
    """
    Creates an instance of a Flask Application
    args:
        config: str - Specify which config to use for applicatin. Dev, Prod, Test. Default: DevConfig
    """
    app = Flask(__name__, template_folder='templates')
    if not config:
        config = 'config.DevConfig'
    app.config.from_object(config)

    initialize_extensions(app)
    configure_logging(app)
    register_blueprints(app)
    register_error_pages(app)
    return app


#!/usr/bin/env python
import glob
import argparse
import logging
import re
import os
import pandas as pd
import matplotlib.pyplot as plt
import json

global data
data = {"names":[], "total_reads":[], "total_bases":[], "q30_rate":[], "duplication":[]}
def main():
    parser = argparse.ArgumentParser(prog=__file__, formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=__doc__)
    parser.add_argument('-fastp',  "--fastp", required=True, help="folder of fastp")
    parser.add_argument('-output',  "--output", required=True, help="folder for fastp stats statistics file")
    parser.add_argument('-v', '--verbose', action='count', default=0, help="Increase verbosity")
    parser.add_argument('-q', '--quiet', action='count', default=0, help="Decrease verbosity")
    args = parser.parse_args()

    logging_level = logging.WARN + 10*args.quiet - 10*args.verbose
    logging.basicConfig(level=logging_level)
    logger = logging.getLogger(__name__)
    logger.debug('args verbose {}:'.format(args.verbose))
    logger.debug('args quiet {}:'.format(args.quiet))
    logger.debug('logging_level {}:'.format(logging_level))
    fns = getfiles(args.fastp)
    [proc_fastp(fn) for fn in fns]
    df = pd.DataFrame.from_dict(data)

    if not os.path.exists(args.output):
         os.makedirs(args.output, exist_ok=True)

    df.to_csv(args.output + "/fastp.stats.csv", index=False)
    #print(df)
    #plot(df)

def getfiles(d):
    fns = glob.glob(d + "/*.fastp.json")
    return fns

def proc_fastp(fn):
    basename = os.path.basename(fn)
    reResult = re.search("(.*).fastp.json", basename)
    if reResult:
        sample_name = reResult.group(1)
        data['names'].append(sample_name)

    with open(fn, newline='') as jsonfile:
        myjson = json.load(jsonfile)
        total_reads = myjson['summary']['before_filtering']['total_reads']
        data['total_reads'].append(total_reads)
        total_bases = myjson['summary']['before_filtering']['total_bases']
        data['total_bases'].append(total_bases)
        q30_rate = myjson['summary']['before_filtering']['q30_rate']
        data['q30_rate'].append(q30_rate)
        duplication = myjson['duplication']['rate']
        data['duplication'].append(duplication)

def plot(data):
    df = data[['>0x', '>=5x', '>=10x', '>=20x', '>=30x', '>=40x', '>=50x', '>=60x', '>=70x', '>=80x', '>=90x', '>=100x']]
    df.index = data['names']
    df = df.T
    #print(df)
    line = df.plot.line()
    plt.show()

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan  8 16:18:41 2021
Sankey did not work,
I made this kind of a transition plot instead
@author: leonid
"""

import pandas as pd, os, matplotlib.pyplot as plt
#%%
os.chdir("/Users/Art/Drive/PhD/Experiments/Aging Signature/000_FINAL/12_machine_learning/selected_AS/")
os.getcwd()
df1=pd.read_csv("Best_ADAboost.tsv", index_col=0, sep="\t")
df2=pd.read_csv("Best_RNDforest.tsv", index_col=0, sep="\t")
print("index file 1", df1.index,"\nIndex file2", df2.index)
lim=50
words1=df1.index[:lim]
words2=df2.index[:lim]
#%% generate y coords for lines and arrows
Y=[]
set1=[-1]*max(len(words1),len(words2))
set2=[-1]*max(len(words1),len(words2))
for i in range(len(words1)):
    for j in range(len(words2)):
        if words1[i]==words2[j]:
            Y.append([i,j])

#%%
#generate start and stop positions
plt.rcParams['font.sans-serif'] = "Arial"
plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['svg.fonttype'] = 'none'
for i in range(len(Y)):
    if min(Y[i])>=0:
        y=Y[i]
        x=[1,2]
        colr="gray"
        if y[0]>y[1]:
            colr="red"
        if y[0]<y[1]:
            colr="blue"
        plt.plot(x,y, c=colr, linewidth=2)
     #   plt.arrow(x[0],y[0],x[1]-x[0],y[1]-y[0],
      #            head_width=0.1, color=colr, linewidth=3)
     #   plt.text(x[0]-0.4,y[0],words[i], fontsize=15, color=colr)
      #  plt.text(x[1]+0.3,y[1], words[i], fontsize=15, color=colr)
plt.gca().invert_yaxis()
# words will be positioned according to their indexes
for i in range(len(words1)):
    plt.text(0.8,i,words1[i])
for i in range(len(words2)):
    plt.text(2,i,words2[i])
#plt.arrow(1,1,1,1, head_width=1)
plt.xticks([],[])
plt.yticks([],[])
plt.xlim(0,3)
plt.ylim(lim,-1)
plt.title("Best ADAboost vs RNDforest")
plt.show()
#plt.savefig("test.svg", format="svg")
# %%


import xlwt
import xlrd
from xlutils.copy import copy

excel_path="D:\PythonProject\data_excel\五中心.xls"
row_i=3
col_j=13
text="你好我的中国"
def write_excel(excel_path,row_i,col_j,text):
    # 打开想要更改的excel文件
    old_excel=xlrd.open_workbook(excel_path,formatting_info=True)
    # 将操作文件对象拷贝，变成可写的workbook对象
    new_excel=copy(old_excel)
    # 获得第一个sheet的对象
    new_sheet=new_excel.get_sheet(0)

    #预定一个字体相关的格式对象style
    style=xlwt.XFStyle()
    #字体参数相关配置
    font=xlwt.Font()
    font.name="Microsoft YaHei UI"
    font.bold = False #是否加粗，True为加粗
    font.height = 250 #  如果是200对应的是10号字体
    style.font = font
    #边框
    borders=xlwt.Borders()
    borders.top=xlwt.Borders.THIN
    borders.bottom=xlwt.Borders.THIN
    borders.left=xlwt.Borders.THIN
    borders.right=xlwt.Borders.THIN
    style.borders=borders
    #写入数据
    new_sheet.write(row_i-1, col_j-1,text,style)
    #另存为excel文件，并将文件命名如果与原来文件重名则修改原文件
    new_excel.save(excel_path)

write_excel(excel_path,row_i,col_j,text)

# import urllib.request
# import urllib.error
# import time
# #url="http://10.45.138.39:8380/bac-ia-account-microservice/account-relation/queryGroupAccountingRelationInfo"
# url="https://www.cnblogs.com/mcq1999/p/python_Crawler_1.html"
# class PortOperation:
#     def __init__(self):
#         print("----init----")
#     def __del__(self):
#         print("----del-----")
#     #捕获请求异常有正确返回后，再进行后续操作
#     def request_error(self,url):
#         request_status=""
#         for i in range(3):
#             try:
#                 #urllib.request.urlopen("https://www.cnblogs.com/mcq1999/p/python_Crawler_1.html")
#                 urllib.request.urlopen(url)
#                 request_status="OK"
#                 time.sleep(3)
#             except urllib.error.URLError as e:
#                 if hasattr(e, "code"):
#                     print(e.code)
#                 if hasattr(e, "reason"):
#                     print(e.reason)
#                 request_status="NO"
#         return request_status
#
#     def port_get(self,url):
#         if self.request_error(url)=="OK":
#             #发起get请求
#             print("发起get请求")
#
# PO=PortOperation()
# PO.port_get(url)


import discord, motor, asyncio, Core.utils as util, Core.confirm as cop_is_gae, typing
from discord.ext import commands
from typing import List
import discord.ui
from Core import utils as util
from datetime import datetime
from Core.utils import get_theme

class autoResponsEEvent(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.db = self.bot.db['autoResponder']
        self._cd = commands.CooldownMapping.from_cooldown(1, 6.0, commands.BucketType.member)

    def get_ratelimit(self, message: discord.Message) -> typing.Optional[int]:
        """Returns the ratelimit left"""
        bucket = self._cd.get_bucket(message)
        return bucket.update_rate_limit()

    @commands.Cog.listener()
    async def on_message(self, message):
        try:
            if message.guild is None:
                return
            if message.author == self.bot.user:
                return
            if message.author.bot:
                return
            ctx=await self.bot.get_context(message)
            check = await self.db.count_documents({ "guild_id": message.guild.id })
            mention=f"<@!{self.bot.user.id}>"
            mention2=f"<@{self.bot.user.id}>"
            if message.content == mention or message.content == mention2:
                ratelimit = self.get_ratelimit(message)
                if ratelimit is None:
                    prefix=await util.get_prefix(self=ctx, bot=self.bot, ctx=ctx)
                    try:
                        return await message.channel.send(embed=discord.Embed(color=int(await get_theme(self, bot=self.bot, guild=ctx.guild.id), 16), description=f":mag: Prefix = **{prefix}**"))
                    except:
                        pass
                else:
                    return
            if check:
                check_existing = self.db.find({ "guild_id": message.guild.id })
                for trigger in await check_existing.to_list(length=20):
                    found = trigger["trigger"]
                    if found in message.content.lower():
                        ratelimit = self.get_ratelimit(message)
                        if ratelimit is None:
                            response = trigger['response']
                            channel = message.channel
                            try:
                                return await channel.send(response)
                            except Exception as e:
                                print (e)
                        else:
                            return

            else:
                return
        except:
            pass; return

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


# -*- coding: utf-8 -*-
import pandas as pd
import neurokit2 as nk
import json
import urllib.request
import datetime

def github_stars(repo="user/repo", access_token="access_token"):
    """Access the stars history from GitHub

    You need your access token that you can get by following these instructions:
    https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

    Examples
    ---------
    >>> import popularipy
    >>> data = popularipy.github_stars("neuropsychology/neurokit", "myaccesstoken")
    >>> data.plot(x="Date")
    """
    page_number = 0
    stars_remaining = True
    stars_times = []
    stars_users = []
    while stars_remaining:
        query_url = "https://api.github.com/repos/%s/stargazers?page=%s&access_token=%s" % (repo, page_number, access_token)

        req = urllib.request.Request(query_url)
        req.add_header('Accept', 'application/vnd.github.v3.star+json')
        try:
            response = urllib.request.urlopen(req)
        except:
            pass
        data = json.loads(response.read())

        for user in data:
            username = user['user']['login']

            star_time = datetime.datetime.strptime(user['starred_at'],'%Y-%m-%dT%H:%M:%SZ')
            star_time = star_time + datetime.timedelta(hours=-5) # EST
            star_time = star_time.strftime('%Y-%m-%d %H:%M:%S')
            stars_times.append(star_time)

            stars_users.append(username)

        if len(data) < 25:
            stars_remaining = False

        page_number += 1

    data = pd.DataFrame({"Date": stars_times,
                         "User": stars_users})
    data = data.drop_duplicates(subset ="User")
    data["Stars"] = 1
    data["Stars"] = data["Stars"].cumsum()
    data = data.append({'Date' : datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S') ,
                        'User' : "MrBrownstone",
                        "Stars": data["Stars"].values[-1]} , ignore_index=True)
    data = data.drop_duplicates(subset ="Date")
    data.index = pd.DatetimeIndex(data["Date"])

    data = data.resample('D').fillna("backfill")
    data = data.drop(["Date", "User"], axis=1)
    data = data.reset_index()
    data["Date"] = pd.to_datetime(data["Date"]).dt.strftime('%d %b %Y')

    return data


from flask import Flask, request, render_template
import poker as p
# jsonify 回傳json

app = Flask(__name__)

@app.route('/poker', methods=['GET', 'POST'])
def poker():
    request_method = request.method
    if request.method == 'GET':
        return render_template('pokercode.html')
    elif request.method == 'POST':
        players = request.form.get('players')
        cards = p.poker(int(players))
        return render_template('pokercode.html',
                               request_method=request_method,
                               players=players,
                               cards=cards)
        # 印 flask 一定要回傳string, 將 json 轉換為 string

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


from elasticsearch import Elasticsearch
import json

class Indexer(object):
    def __init__(self, index_name, doc_type):

        # Load configuration from file
        with open("../config.json", "r") as fig:
            config = json.load(fig)

        host = config['host']
        port = int(config['port'])

        # create an Elasticsearch index
        es = Elasticsearch([{'host': host, 'port': port}])

        self.index_name = index_name
        self.es = es
        self.doc_type = doc_type

    def index(self, doc_id=None, doc_body=None):
        self.es.index(
            index=self.index_name,
            doc_type=self.doc_type,
            id=doc_id,
            body=doc_body)


"""
Various helper utilities used internally by ncluster project, that are not explicitly tied to AWS
"""

import os
import random
import string
import sys
import time
from collections import Iterable
import shlex

from typing import Optional, Tuple

import portalocker
import paramiko

# starting value for now_micros (Aug 31, 2018), using this to make various timestamped names shorter
EPOCH_MICROS = 1535753974788163

# Whitelist of temporary settings customizable through env vars. These are work-arounds for issues that are
# don't have permanent solutions yet. Keep this small to avoid many moving parts.
env_settings = {
  'NCLUSTER_AUTHORIZED_KEYS',         # public keys used to authorize ssh access on all instances
  'NCLUSTER_AWS_FAST_ROOTDISK',       # request $1/hour high performance AWS disk
  'NCLUSTER_AWS_PLACEMENT_GROUP',     # name of placement group to use, use when adding machines to a previous launched job
  'NCLUSTER_DISABLE_PDB_HANDLER',     # don't intercept pdb exception by default
  'NCLUSTER_RUNNING_UNDER_CIRCLECI',  # special settings for non-interactive CircleCI integration test env
  #  'NCLUSTER_IMAGE',
  'NCLUSTER_SSH_USERNAME',            # used as workaround when Amazon Linux detection fails
  'NCLUSTER_ZONE',                    # zone spec for when automatic zone fails (p3dn's + spot instances)
  'NCLUSTER_AWS_FORCE_CREATE_RESOURCES',  # AWS resources are created ignoring automatic existence checks
}

# keep this here instead of aws_backend because it's used by its dependency aws_util
VALID_REGIONS = ['us-east-2',
                 'us-east-1',
                 'us-west-1',       # An error occurred (Unsupported) when calling the RunInstances operation
                 'us-west-2',
                 'ap-east-1',       # doesn't have ec2
                 'ap-south-1',      # no EFS
                 'ap-northeast-3',  # An error occurred (OptInRequired) when calling the DescribeVpcs operation
                 'ap-northeast-2',
                 'ap-southeast-1',
                 'ap-southeast-2',
                 'ap-northeast-1',
                 'ca-central-1',
                 'cn-north-1',      # account number
                 'cn-northwest-1',  # account number
                 'eu-central-1',
                 'eu-west-1',
                 'eu-west-2',
                 'eu-west-3',       # no EFS
                 'eu-north-1',      # no EFS
                 'sa-east-1',       # no EFS
                 'us-gov-east-1',   # not authorized
                 'us-gov-west-1',   # not authorized
                 ]

# print/validate custom settings
for v in os.environ:
  if v.startswith('NCLUSTER'):
    assert v in env_settings, f"Custom setting '{v}'='{os.environ[v]}' not in settings whitelist, if you" \
      f"are sure you need this setting, add it to the env_settings in {os.path.basename(__file__)}, otherwise 'unset {v}'"
    # the following are often set by default, so don't print them
    if v in {'NCLUSTER_AUTHORIZED_KEYS', 'NCLUSTER_ZONE'}:
      continue

    sys.stderr.write(f"ncluster env setting {v}={os.environ[v]}\n")

def is_iterable(k):
  return isinstance(k, Iterable)

def now_micros(absolute=False) -> int:
  """Return current micros since epoch as integer."""
  micros = int(time.time() * 1e6)
  if absolute:
    return micros
  return micros - EPOCH_MICROS

def now_millis(absolute=False) -> int:
  """Return current millis since epoch as integer."""
  millis = int(time.time() * 1e3)
  if absolute:
    return millis
  return millis - EPOCH_MICROS // 1000

def current_timestamp() -> str:
  # timestamp format from https://github.com/tensorflow/tensorflow/blob/155b45698a40a12d4fef4701275ecce07c3bb01a/tensorflow/core/platform/default/logging.cc#L80
  current_seconds = time.time()
  remainder_micros = int(1e6 * (current_seconds - int(current_seconds)))
  time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_seconds))
  full_time_str = "%s.%06d" % (time_str, remainder_micros)
  return full_time_str

def log_error(*args, **kwargs):
  print(f"Error encountered {args} {kwargs}")

def log(*args, **kwargs):
  print(f"{args} {kwargs}")

def install_pdb_handler():
  """Automatically start pdb:
      1. CTRL+\\ breaks into pdb.
      2. pdb gets launched on exception.
  """

  import signal
  import pdb

  def handler(_signum, _frame):
    pdb.set_trace()
  signal.signal(signal.SIGQUIT, handler)

  # Drop into PDB on exception
  # from https://stackoverflow.com/questions/13174412
  def info(type_, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type_, value, tb)
   else:
      import traceback
      import pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type_, value, tb)
      print()
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

  sys.excepthook = info

def shell_add_echo(script):
  """Goes over each line script, adds "echo cmd" in front of each cmd.

  ls a

  becomes

  echo * ls a
  ls a
  """
  new_script = ""
  for cmd in script.split('\n'):
    cmd = cmd.strip()
    if not cmd:
      continue
    new_script += "echo \\* " + shlex.quote(cmd) + "\n"
    new_script += cmd + "\n"
  return new_script

def shell_strip_comment(cmd):
  """ hi # testing => hi"""
  if '#' in cmd:
    return cmd.split('#', 1)[0]
  else:
    return cmd

def random_id(k=5):
  """Random id to use for AWS identifiers."""
  #  https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python
  return ''.join(random.choices(string.ascii_lowercase + string.digits, k=k))

def alphanumeric_hash(s: str, size=5):
  """Short alphanumeric string derived from hash of given string"""
  import hashlib
  import base64
  hash_object = hashlib.md5(s.encode('ascii'))
  s = base64.b32encode(hash_object.digest())
  result = s[:size].decode('ascii').lower()
  return result

def reverse_taskname(name: str) -> str:
  """
  Reverses components in the name of task. Reversed convention is used for filenames since
  it groups log/scratch files of related tasks together

  0.somejob.somerun -> somerun.somejob.0
  0.somejob -> somejob.0
  somename -> somename

  Args:
    name: name of task

  """
  components = name.split('.')
  assert len(components) <= 3
  return '.'.join(components[::-1])

def is_bash_builtin(cmd):
  """Return true if command is invoking bash built-in
  """
  # from compgen -b
  bash_builtins = ['alias', 'bg', 'bind', 'alias', 'bg', 'bind', 'break',
                   'builtin', 'caller', 'cd', 'command', 'compgen', 'complete',
                   'compopt', 'continue', 'declare', 'dirs', 'disown', 'echo',
                   'enable', 'eval', 'exec', 'exit', 'export', 'false', 'fc',
                   'fg', 'getopts', 'hash', 'help', 'history', 'jobs', 'kill',
                   'let', 'local', 'logout', 'mapfile', 'popd', 'printf',
                   'pushd', 'pwd', 'read', 'readarray', 'readonly', 'return',
                   'set', 'shift', 'shopt', 'source', 'suspend', 'test',
                   'times', 'trap', 'true', 'type', 'typeset', 'ulimit',
                   'umask', 'unalias', 'unset', 'wait']
  toks = cmd.split()
  if toks and toks[0] in bash_builtins:
    return True
  return False

def is_set(name: str) -> bool:
  """Helper method to check if given property is set"""
  assert name in env_settings

  val = os.environ.get(name, '0')
  return not (val == '0')

def get_env(name: str) -> Optional[str]:
  """Helper method to retrieve custom env setting, returns None if not set"""
  assert name in env_settings
  return os.environ.get(name, None)

def set_env(name: str, value: str) -> None:
  """Helper method to set custom env setting"""
  assert name in env_settings
  os.environ[name] = value

def assert_script_in_current_directory():
  """Assert fail if current directory is different from location of the script"""

  script = sys.argv[0]
  assert os.path.abspath(os.path.dirname(script)) == os.path.abspath(
    '.'), f"Change into directory of script {script} and run again."

def validate_ncluster_job_name(name):
  assert name.count(
    '.') <= 1, "Job name has too many .'s (see ncluster design: Run/Job/Task hierarchy for  convention)"

def toseconds(dt) -> float:
  """Converts datetime object to seconds."""
  return time.mktime(dt.utctimetuple())

def wait_for_file(fn: str, max_wait_sec: int = 60,
                  check_interval: float = 1) -> bool:
    """
    Waits for file maximum of max_wait_sec. Returns True if file was detected within specified max_wait_sec
    Args:
      fn: filename
      max_wait_sec: how long to wait in seconds
      check_interval: how often to check in seconds
    Returns:
      False if waiting was was cut short by max_wait_sec limit, True otherwise
    """
    log("Waiting for file", fn)
    start_time = time.time()
    while True:
      if time.time() - start_time > max_wait_sec:
        log(f"Timeout exceeded ({max_wait_sec} sec) for {fn}")
        return False
      if not os.path.exists(fn):
        time.sleep(check_interval)
        continue
      else:
        break
    return True

# locations of default keypair
ID_RSA = os.environ['HOME'] + '/.ssh/id_rsa'
ID_RSA_PUB = ID_RSA + '.pub'

def setup_local_ssh_keys() -> str:
  """Sanity checks on local ssh keypair and regenerate it if necessary. Returns location of public keypair file"""

  if os.path.exists(ID_RSA_PUB):
    assert os.path.exists(ID_RSA), f"Public key {ID_RSA_PUB} exists but private key {ID_RSA} not found, delete {ID_RSA_PUB} and run again to regenerate pair"
    log(f"Found local keypair {ID_RSA}")
  elif os.path.exists(ID_RSA):
    if not os.path.exists(ID_RSA_PUB):
      if is_set('NCLUSTER_RUNNING_UNDER_CIRCLECI'):
        pass
      else:
        assert os.path.exists(ID_RSA_PUB), f"Private key {ID_RSA} exists but public key {ID_RSA_PUB} not found, delete {ID_RSA} and run again to regenerate pair"
    log(f"Found local keypair {ID_RSA}")
  else:
    log(f"Generating keypair {ID_RSA}")
    with portalocker.Lock(ID_RSA+'.lock', timeout=5) as _:
      os.system(f"ssh-keygen -t rsa -f {ID_RSA} -N ''")
    os.system(f'rm {ID_RSA}.lock')

  return ID_RSA_PUB

def get_authorized_keys() -> str:
  """Appends local public key to NCLUSTER_AUTHORIZED_KEYS and returns in format key1;key2;key3;
  The result can be assigned back to NCLUSTER_AUTHORIZED_KEYS env var"""

  assert os.path.exists(ID_RSA_PUB), f"{ID_RSA_PUB} not found, make sure to run 'ncluster keys'"

  current_key = open(ID_RSA_PUB).read().strip()
  auth_keys = os.environ.get('NCLUSTER_AUTHORIZED_KEYS', '')
  return auth_keys+';'+current_key+';'

def get_public_key() -> str:
  """Returns public key, creating it if needed"""

  if not os.path.exists(ID_RSA_PUB):
    print(f"{ID_RSA_PUB} not found, running sure to run setup_local_ssh_keys()")
    setup_local_ssh_keys()
    assert os.path.exists(ID_RSA_PUB)

  return open(ID_RSA_PUB).read().strip()

def exec_command(ssh: paramiko.SSHClient, command: str, bufsize=-1, timeout=None, get_pty=False, environment=None) -> Tuple[paramiko.ChannelFile, paramiko.ChannelFile, paramiko.ChannelFile, paramiko.Channel]:
    """Copy of paramiko's exec_command which also returns the channel."""

    transport: paramiko.Transport = ssh.get_transport()
    chan: paramiko.Channel = transport.open_session(timeout=timeout)
    if get_pty:
      chan.get_pty()
    chan.settimeout(timeout)
    if environment:
      chan.update_environment(environment)
    chan.exec_command(command)
    stdin: paramiko.ChannelFile = chan.makefile("wb", bufsize)
    stdout: paramiko.ChannelFile = chan.makefile("r", bufsize)
    stderr: paramiko.ChannelFile = chan.makefile_stderr("r", bufsize)
    return stdin, stdout, stderr, chan

class timeit:
  """Decorator to measure length of time spent in the block in millis and log
  it to TensorBoard."""

  def __init__(self, tag=""):
    self.tag = tag

  def __enter__(self):
    self.start = time.perf_counter()
    return self

  def __exit__(self, *args):
    self.end = time.perf_counter()
    interval_ms = 1000 * (self.end - self.start)
    print(f'timeit({self.tag}): {interval_ms})')

# no_op method/object that accept every signature
class NoOp:
  def __getattr__(self, *args):
    def no_op(*_args, **_kwargs): pass
    return no_op


import matplotlib.pyplot as plt
import os
import matplotlib.image as mpimg
import numpy as np
import cv2 as cv
import time

os.chdir("C:/Users/fseignat/Desktop/stega Seignat/code_images")

from quantiles import quantiles_theoriques,quantiles_TCL
from lecture_video import tableau,cv_plt,distance,recup_Cover,modif_video, supp_val_extrem
from test_wave import decomposition_image, concatene
from estimation_parametres import sigma_chap, beta_chap,g_prime,val_dep_beta
from scipy.stats import gennorm
from etude_coefs import recup_image

start_time = time.time()

##Recuperation des fichiers

#KB = np.load('pixel_video_KB.npy')
#TR = np.load('pixel_video_TR.npy')
#TR2 = np.load('pixel_video_TR(2).npy')
#SC=np.load("Scarface.npy")
#AZ=np.load("Azur et Asmar.npy")
BA_TR=np.load("BA_TR.npy")

#Vidéo non modifiée
file=BA_TR
File=[file]

Nom_File=["KB","TR","TR2","SC","AZ"]
#File=[KB,TR,TR2,SC,AZ]

nb_echelle=3

# #NB=len(file)

##♥ Analyse d'une vidéo entière

# Count_Ste=[]

# f=0
# for file in File:
#     c=0
#     Rapp_sigma=[]
#     Rapp_beta=[]
#     NB=len(file)
#     for ind in range(1,NB-1):
#         Signe="Cover"
#         for e in range(1):

#             img=file[ind]

#             ind_Cover=recup_Cover(file,ind)
#             img_Cover=file[ind_Cover]

#             WR=concatene(img,nb_echelle,e+1)
#             WR_C=concatene(img_Cover,nb_echelle,e+1)

#             WR=supp_val_extrem(WR,0.002)
#             WR_C=supp_val_extrem(WR_C,0.002)

#             #Image à tester
#             val_dep=val_dep_beta(WR)
#             beta_estim=beta_chap(WR,val_dep,10**(-4))
#             sigma_estim=sigma_chap(WR,beta_estim)

#             #Image "Cover"
#             val_dep_C=val_dep_beta(WR_C)
#             beta_estim_C=beta_chap(WR_C,val_dep_C,10**(-4))
#             sigma_estim_C=sigma_chap(WR_C,beta_estim_C)

#             if beta_estim!="Pas de solution":
#                     num=np.sum(np.abs(WR)**beta_estim)
#                     den=np.sum(np.abs(WR_C)**beta_estim)
#                     rapport=num/den

#                     #Quantiles théoriques
#                     Q_th=quantiles_theoriques(np.size(WR_C),beta_estim,0.05)

#                     #Quantiles TCL
#                     #Q_TCL=quantiles_TCL(np.size(WR_Cov),beta_estim,0.05)

#                     if rapport>Q_th:
#                         Signe="Stego"

#             #print("ind = ", ind,"beta_Cov = ",beta_estim_C,"beta_test =",beta_estim)
#             #print("\n")
#             if Signe=="Stego":
#                     c+=1
#         if beta_estim!='Pas de solution' and beta_estim_C !='Pas de solution':
#             Rapp_sigma.append(sigma_estim/sigma_estim_C)
#             Rapp_beta.append(beta_estim/beta_estim_C)
#         print("F= ",f,"ind =",ind,Signe)
#         print("\n")

#     print("\n")
#     Count_Ste.append(c)

#     B=[i for i in range(len(Rapp_beta))]

#     plt.figure()
#     plt.plot(B,Rapp_beta)
#     plt.title(Nom_File[f]+"_Rapport des beta"+"_2")      #entre l'image test et l'image désignée Cover (par la norme L²)

#     plt.figure()
#     plt.plot(B,Rapp_sigma)
#     plt.title(Nom_File[f]+"_Rapport des sigma"+"_2")
#     f+=1

# print("--- %s seconds ---" % (time.time() - start_time))

## Analyse d'une trame (pour comprendre le comportement d'une image spécifique)

img=file[187]              #154

ind_Cover=recup_Cover(file,187)
img_Cover=file[ind_Cover]

#img=recup_image(7841,"C:/Users/fseignat/Desktop/stega Seignat/code_images/ALASKA/Cover")
WR=concatene(img,2,2)
WR_C=concatene(img_Cover,2,2)

#Seuillage
WR=supp_val_extrem(WR,0.002)
WR_C=supp_val_extrem(WR_C,0.002)

#WR=decomposition_image(img,3,2,1)
val_dep=val_dep_beta(WR)
beta_estim=beta_chap(WR,val_dep,10**(-4))
sigma_estim=sigma_chap(WR,beta_estim)
print("beta = ",beta_estim,"sigma =",sigma_estim,"val =",val_dep)

val_dep_C=val_dep_beta(WR_C)
beta_estim_C=beta_chap(WR_C,val_dep_C,10**(-4))
sigma_estim_C=sigma_chap(WR_C,beta_estim_C)
print("beta = ",beta_estim_C,"sigma =",sigma_estim_C,"val =",val_dep_C)

plt.figure()
n, b, patches=plt.hist(np.reshape(WR,np.size(WR)), bins='sqrt',color='blue',density=True, alpha=0.3, rwidth=0.85)
x = np.linspace(np.min(WR),np.max(WR),150)
F=gennorm.pdf(x,beta_estim,scale=sigma_estim)
plt.plot(x,F,'r-', lw=1, alpha=0.6, label='gennorm pdf')
plt.ylim([0,np.max(F)])
plt.xlim([0,n.max()])

plt.figure()
plt.hist(np.reshape(WR_C,np.size(WR_C)), bins='sqrt',color='blue',density=True, alpha=0.3, rwidth=0.85)
x_C = np.linspace(np.min(WR_C),np.max(WR_C),150)
F_C=gennorm.pdf(x_C,beta_estim_C,scale=sigma_estim_C)
plt.plot(x_C,F_C,'r-', lw=1, alpha=0.6, label='gennorm pdf')
plt.ylim([0,np.max(F_C)])

# ##Dessins animés

# # AZ=np.load("Azur et Asmar.npy")

# # for i in range(10):
# #     img=AZ[i]
# #     for e in range(nb_echelle):
# #         WR=concatene(img,nb_echelle,e+1)
# #         val_dep=val_dep_beta(WR)
# #         beta_estim=beta_chap(WR,val_dep,10**(-4))
# #         sigma_estim=sigma_chap(WR,beta_estim)
# #         print("beta =", beta_estim, "sigma =",sigma_estim)
# #         plt.figure()
# #         plt.hist(np.reshape(WR,WR.shape[0]*WR.shape[1]), bins='auto',color='blue',density=True, alpha=0.5, rwidth=0.85)
# #         x = np.linspace(gennorm.ppf(0.001,beta_estim,scale=sigma_estim),gennorm.ppf(0.999, beta_estim,scale=sigma_estim), 100)
# #         F=gennorm.pdf(x,beta_estim,scale=sigma_estim)
# #         plt.plot(x, F,'r-', lw=1, alpha=0.5,color='red', label='gennorm pdf')
# #         plt.ylim([0,np.max(F)])


import random
import numpy as np
import neuralNetwork as nn
import pygame
import math

#--------------------Colors-----------------------
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
green = (0,255,0)

#---------------------sizes--------------------

display_width = 1600
display_height = 1200
wall_width= display_width/50
wall_height=display_height/6
x =  (display_width * 0.2)
y = (display_height * 0.5)

def mutation(x):
	if random.randrange(0,1)<0.1:
		offset = np.random.normal()*0.5
		newx = x + offset
		return newx
	else:
		return x

class Copter():
	def __init__(self, x, y, brain,speed =0,velocity =0, gravity=1, acceleration = 2):
		self.x = x
		self.y = y
		self.speed = speed
		self.gravity = gravity
		self.acceleration = acceleration
		self.velocity = velocity
		self.copterImg = pygame.image.load('copter.png')
		if(isinstance(brain, nn.NeuralNetwork)):
			self.brain = brain.copy()
			self.brain.mutate(mutation)
		else:
			self.brain = nn.NeuralNetwork(5,8,2)
		self.score = 0
		self.fitness = 0

	def draw(self, gameDisplay):
		gameDisplay.blit(self.copterImg, (self.x,self.y))
		#pygame.draw.rect(gameDisplay, white, [self.x, self.y, 20, 20])

	def update(self,gameDisplay, draw = False):
		self.score+=1
		self.velocity +=self.gravity
		self.y +=self.velocity
		# if draw:
		# 	self.draw(gameDisplay)

	def copy(self):
		return Copter(x,y,self.brain)

	def forward(self):
		self.x += self.speed

	def up(self):
		self.y += self.gravity

	def down(self):
		self.y -= gravity

	def think(self,wall):
		check_wall_up = wall.upper_walls[40]
		check_wall_down = wall.lower_walls[40]

		inputs = []
		inputs.append(check_wall_down.x)
		inputs.append(check_wall_up.y)
		inputs.append(check_wall_down.y)
		inputs.append(self.y)
		inputs.append(self.velocity)

		action = self.brain.predict(inputs)

		if action[1] > action[0]:
			self.velocity -= self.acceleration

class Wall_element:

	def __init__(self, x,y, width, height, color):
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.color = color

	def draw(self,gameDisplay, xmod =0, ymod =0, wmod =0, hmod =0):
		if self.color==green:
			#pygame.draw.rect(gameDisplay, black, [self.x, 0, self.width, ],1)
			pygame.draw.rect(gameDisplay, self.color, [self.x, 0, self.width, self.y+self.height])
		else:
			#	pygame.draw.rect(gameDisplay, black, [self.x, self.y, self.width, self.height],1)
			pygame.draw.rect(gameDisplay, self.color, [self.x, self.y, self.width, display_height-self.y])

class Wall():
	def __init__(self):
		self.prev_height_top = 0
		self.prev_x = -5
		self.prev_height_bottom = display_height-wall_height
		self.upper_walls = []
		self.lower_walls = []
		self.direction = [0,0]

		for i in range(0,55):
			self.upper_walls.append(Wall_element(-wall_width+i*wall_width,0,wall_width,wall_height, green))
			self.lower_walls.append(Wall_element(-wall_width+i*wall_width,self.prev_height_bottom,wall_width,wall_height,blue))

	def update_wallgroup(self,gameDisplay,walls, draw):
		global direction
		for index in range(0,len(walls)):
			if index < len(walls)-1:
				walls[index].y = walls[index+1].y
			else:
				if self.direction[0]==1:
					walls[index].y= walls[index].y - (self.direction[1]+1)*2
					if (walls[index].color==green and walls[index].y<-150):
						self.direction = [0,0]
					self.direction[1]+=1
				else:
					walls[index].y= walls[index].y + (self.direction[1]+1)*2
					if (walls[index].color==blue and walls[index].y>display_height-20):
						self.direction = [1,0]
					self.direction[1]+=1
			if draw:
				walls[index].draw(gameDisplay)
			# if copter_x>=walls[index].x and copter_x <= (walls[index].x+walls[index].width):
			# 	if copter_y >= walls[index].y and copter_y <= (walls[index].y+walls[index].height):
			# 		return True
		return False;

	def update(self,gameDisplay,drawing=True):
		#Only update the lower walls if the upper walls did not lead to a crash
		next_direction = random.randrange(0,10)
		if next_direction <= self.direction[1]:
			self.direction = [math.floor((self.direction[0]+1)/2),0]
		self.update_wallgroup(gameDisplay, self.upper_walls, drawing)
		self.update_wallgroup(gameDisplay,self.lower_walls, drawing)

	def draw(self,gameDisplay):
		for wall in self.upper_walls:
			wall.draw(gameDisplay)
		for wall in self.lower_walls:
			wall.draw(gameDisplay)

	def crashCheck(self, copter_x,copter_y):
		for wall in self.upper_walls:
			if copter_x>=wall.x and copter_x <= (wall.x+wall.width):
				if copter_y<=wall.y+wall.height:
					return True
		for l_wall in self.lower_walls:
			if copter_x>=l_wall.x and copter_x <= (l_wall.x+l_wall.width):
				if copter_y >= l_wall.y:
					return True
		return False

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',50)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

def crash():
    message_display('You Crashed')

def display_score(score, gameDisplay):

    font = pygame.font.SysFont(None, 25)
    text = font.render("Score: "+str(score), True, black)
    gameDisplay.blit(text,(20,200))


import gym
import numpy as np
import matplotlib.pyplot as plt

from keras.models import Sequential,Model
from keras.layers import Dense, Reshape, Flatten, Activation,Input,merge

from keras.optimizers import Adam
from keras.layers.convolutional import Convolution2D

import networkx as nx
import pylab as plt
import numpy as np
import logging.config

import random

class PGAgent:
	def __init__(self, state_size, action_size):
		self.state_size = state_size
		self.action_size = action_size
		self.gamma = 0.99
		self.learning_rate = 0.001
		self.states = []
		self.gradients = []
		self.rewards = []
		self.probs = []
		self.model = self._build_model()
		self.model.summary()

	def _build_model(self):

		model = Sequential()
		model.add(Reshape((1, 2, 64), input_shape=(self.state_size,)))
		model.add(Convolution2D(2, 2, 4, subsample=(2, 2), border_mode='same',activation='relu', init='he_uniform'))
		model.add(Flatten())
		model.add(Dense(16, activation='relu', init='he_uniform'))
		model.add(Dense(8, activation='relu', init='he_uniform'))
		model.add(Dense(self.action_size, activation='softmax'))
		opt = Adam(lr=self.learning_rate)
		model.compile(loss='categorical_crossentropy', optimizer=opt)

		return model

	def remember(self, state, action, prob, reward):
		y = np.zeros([self.action_size])
		y[action] = 1
		self.gradients.append(np.array(y).astype('float32') - prob)
		self.states.append(state)
		self.rewards.append(reward)

	def act(self, state):

		state = state.reshape([1, state.shape[0]])

		#print("STATETOMODEL::",state)

		aprob = self.model.predict(state, batch_size=3).flatten()

		self.probs.append(aprob)
		#prob = aprob / np.sum(aprob)
		action = np.random.choice(self.action_size, 1, p=aprob)[0] #exploration and exploitation
		#[]

		#print('np.sum(aprob):', np.sum(aprob))
		#print("aprob::::", aprob)

		#print("ACTION:", action)

		return action, aprob

	def discount_rewards(self, rewards):
		discounted_rewards = np.zeros_like(rewards)
		running_add = 0
		for t in reversed(range(0, rewards.size)):
			if rewards[t] != 0:
				running_add = 0
			running_add = running_add * self.gamma + rewards[t]
			discounted_rewards[t] = running_add
		return discounted_rewards

	def train(self):
		gradients = np.vstack(self.gradients)
		rewards = np.vstack(self.rewards)
		rewards = self.discount_rewards(rewards)
		rewards = (rewards - np.mean(rewards)) / (np.std(rewards) - np.mean(rewards))
		 #normalize

		#print('rewards::', rewards)

		gradients *= rewards
		X = np.squeeze(np.vstack([self.states]))
		Y = self.probs + self.learning_rate * np.squeeze(np.vstack([gradients]))

		self.model.train_on_batch(X, Y)
		self.states, self.probs, self.gradients, self.rewards = [], [], [], []

	def load(self, name):
		self.model.load_weights(name)

	def save(self, name):
		self.model.save_weights(name)

class Environment:

	def __init__(self):
		self.__version__ = "0.1.0"
		logging.info("BananaEnv - Version {}".format(self.__version__))

		self.points_list=[(100,0),(0,1),(0,2),(1,3),(1,4),(2,5),(2,6),(3,7),(3,8),(4,9),
                (4,10),(5,11),(5,12),(6,13),(6,14),(7,15),(7,16), (8,17),
                (8,18),(9,19),(9,20),(10,21),(10,22),(11,23),(11,24),(12,25),
                (12,26), (13,27),(13,28),(14,29),(14,30),(15,31),(15,32),(16,33),
                (16,34),(17,35),(17,36),(18,37),(18,39),(19,40),(19,41),(20,42),(20,43),
                (21,44),(21,45),(22,46),(22,47),(23,48),(23,49),(24,50),(24,51),(25,52),(25,53),
                (26,54),(26,55),(27,56),(27,57),(28,58),(28,59),(29,60),(29,61),(30,62),(30,63)]

		self.attrs = { # all nodes have [1,0] label

        0: {'attr': [[0,1]]}, 1: {'attr': [[0,1]]},
        2: {'attr': [[0,1]]}, 5: {'attr': [[0,1]]},  12: {'attr': [[0,1]]},25: {'attr': [[0,1]]},
        52: {'attr': [[0,1]]}, 6: {'attr': [[0,1]]}, 14: {'attr': [[0,1]]},  8: {'attr': [[0,1]]}, 27: {'attr': [[0,1]]}
        }

		self.G=nx.DiGraph()
		self.G.add_edges_from(self.points_list)

		self.labels = []
		nx.set_node_attributes(self.G, self.labels, 'attr')
		self.labels.append([1,0])

		nx.set_node_attributes(self.G, self.attrs)

		self.state = 100

		self.done = self.is_over() # True or False

		self.curr_episode = -1

		self.action_episode_memory = []
		self.reward_episode_memory = []

	def step(self, action): #action is 0 for left 1 for right

		self.do_action(action)#the state is updated
		ob = self._get_state()
		reward = self._get_reward()
		done = self.is_over()

		return ob,reward,done

	def _neighbors(self):
		tmp = []
		for i in self.G.neighbors(self.state):
			tmp.append(i)
		return tmp

	def do_action(self,action): #0

		self.action_episode_memory[self.curr_episode].append(action)

		neighbors = self._neighbors() # [7,8]
		if len(neighbors) ==1:
			#print("Len neighbors: ", len(neighbors))
			self.state=0

		else:
			#print("Len neighbors: ", len(neighbors))
			self.state = neighbors[action] #neighbors[0] = 7

	def _get_reward(self):
		if self.G.nodes[self.state]['attr'][0] == [0,1]:
			self.reward_episode_memory[self.curr_episode].append(0.2)
			return 0.2
		else:

			if self.G.nodes[self.state]['attr'][0] == [1,0]:
				self.reward_episode_memory[self.curr_episode].append(0.0)
				return 0
			else:
				raise Exception("Go to get reward something wrong with node label")

	def reset(self):
		self.state = 100
		self.curr_episode += 1 #-1 ==>0 for the first time
		self.action_episode_memory.append([])
		self.reward_episode_memory.append([])
		return self.state

	def is_over(self):
		neighbors = self._neighbors()
		if len(neighbors) == 0:
			return True
		else:
			return False

	def _get_state(self):
		return self.state #just returns the state

if __name__ == "__main__":

	env = Environment()
	state = env.reset()
	score = 0
	episode = 0
	path = []
	x_data = []
	y_data = []

	state_size = 64 * 2 #number of nodes in graph
	action_size = 2

	agent = PGAgent(state_size, action_size)

	state, reward, done  = env.step(0)
	i = 0
	while i<10000:

		base = np.zeros((64,2))
		base[state][0] = 1

		#print("base::",base)

		action, prob = agent.act(base.astype(np.float).ravel())

		state, reward, done  = env.step(action)

		path.append(state)
		score += reward
		#print(done)

		base = np.zeros((64,2))
		base[state][0] = 1 # = [0,0,0,0,0,1,0,0,0,...]

		agent.remember(base.astype(np.float).ravel(), action, prob, reward)

		if done==True:
			i+=1
			episode += 1
			agent.train()
			print("path:", path)
			path = []
			print('Episode: %d - Score: %f.' % (episode, score))
			if i%10==0:
				x_data.append(score)
				y_data.append(episode)
			score = 0
			state = env.reset()
			state, reward, done  = env.step(0)

# Plot the data
	plt.plot(y_data, x_data, label='linear')
# Add a legend
	plt.legend()
# Show the plot
	plt.show()


from sqlalchemy import create_engine,MetaData,Table,ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,Float,DateTime
import csv

from sqlalchemy.orm import sessionmaker
from datetime import datetime,date
from dateutil.parser import parse

engine = create_engine('sqlite:///etl.db', echo = True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
year=input("Enter a year to load:")
path='../../input/'+year+'/Network.csv'

try:
    reader_file = csv.DictReader(open(path,'r',encoding='UTF-8'))# utf-8 for 2014 and 2015 and #cp 1252 for 2016
    field=reader_file.fieldnames

    tablename="Network"+"_"+year
    attr_dict = {'__tablename__': tablename}
    attr_dict["record_id"]=Column(Integer, primary_key=True, autoincrement=True)
    for i in field:
        if i =="ImportDate":
            attr_dict[i] = Column(DateTime)
        else:
            attr_dict[i] = Column(String)

    MyTableClass = type('MyTableClass', (Base,), attr_dict)
    Base.metadata.create_all(engine)

    class LogTable(Base):
        __tablename__ = 'LogTable'
        transaction_id = Column(Integer, primary_key=True,autoincrement=True)
        tablename=Column(String)
        year=Column(Integer,ForeignKey(f'Business_Rules_{year}.BusinessYear'))
        start_time=Column(DateTime)
        end_time = Column(DateTime)
        no_of_records=Column(Integer)
        transaction_status= Column(String)

    Base.metadata.create_all(engine)

    confirm=input("Do you want to transform load:{y/n)\n")
    if confirm == "y":

        start = datetime.now()

        dat_check = session.query(MyTableClass).filter_by(BusinessYear=year).first()
        if dat_check is not None:
            MyTableClass.__table__.drop(engine)
            print("Old Records Deleted")
        flag = 0
        checkpoint = 0
        total_records = 0

        try:
            for row in reader_file:
                #Cleansing ImportDate column
                #fdate=row['ImportDate'].split(" ")[0].split("-")
                #print(fdate[0],fdate[1],fdate[2])
                #dat=date(int(fdate[0]),int(fdate[1]),int(fdate[2]))
                date= parse(row['ImportDate'])
                row['ImportDate']=date
                try:
                    record = MyTableClass(**row)
                    session.add(record)
                    print(flag, "th record")
                    if flag >= 500000:
                        try:
                            session.commit()
                        except:
                            session.rollback()
                            MyTableClass.__table__.drop(engine)
                            print("Invalid Data format")
                            break
                        total_records+=flag
                        checkpoint+=1
                        print("5L records inserted: checkpoint")
                        flag = 0
                        continue
                    flag += 1
                except KeyboardInterrupt as k:
                    MyTableClass.__table__.drop(engine)
                    print("Process aborted")
                    break
                except MemoryError as e:
                    session.rollback()
                    MyTableClass.__table__.drop(engine)
                    session.add(LogTable(tablename=tablename, year=year, start_time=start, end_time=datetime.now(),
                                         no_of_records=total_records,transaction_status="Failure"))
                    session.commit()
                    print("Transaction roll back")
                    break
            else:
                total_records += flag
                session.commit()
                session.add(LogTable(tablename=tablename, year=year, start_time=start, end_time=datetime.now(),
                                     no_of_records=total_records,transaction_status="Success"))
                session.commit()
                print("All records inserted: Transaction Completed")

        except UnicodeDecodeError as e:

            MyTableClass.__table__.drop(engine)
            print("Check encoding")

except FileNotFoundError as e:
    print("Dataset for Year doesnt exist")