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

from __future__ import print_function

import importlib
import os
import time
import zipfile
import requests

from .._protos.public.common import CommonService_pb2 as _CommonCommonService
from .._protos.public.modeldb import CommonService_pb2 as _CommonService

from ..external import six

from .._internal_utils import (
    _artifact_utils,
    _git_utils,
    _utils,
)

_OSS_DEFAULT_WORKSPACE = "personal"
_MODEL_ARTIFACTS_ATTR_KEY = "verta_model_artifacts"

class _ModelDBEntity(object):
    def __init__(self, conn, conf, service_module, service_url_component, msg):
        self._conn = conn
        self._conf = conf

        # TODO: remove these b/c they're barely used, and don't even work with registry
        self._service = service_module
        self._request_url = "{}://{}/api/v1/modeldb/{}/{}".format(self._conn.scheme,
                                                      self._conn.socket,
                                                      service_url_component,
                                                      '{}')  # endpoint placeholder

        self.id = msg.id
        self._msg = msg
        self._cache_time = time.time()
        self._update_cache()

    def _clear_cache(self):
        self._msg = None
        self._cache_time = None

    def _refresh_cache(self):
        now = time.time()
        if self._cache_time is None:
            self._msg = None

        # Cache for 5 seconds
        if self._cache_time is not None and now - self._cache_time > 5:
            self._msg = None

        if self._msg is None:
            self._msg = self._get_proto_by_id(self._conn, self.id)
            self._cache_time = now
            self._update_cache()

    def _fetch_with_no_cache(self):
        self._msg = self._get_proto_by_id(self._conn, self.id)
        self._cache_time = None
        self._update_cache()

    def _update_cache(self):
        pass

    def __getstate__(self):
        state = self.__dict__.copy()

        state['_service_module_name'] = state['_service'].__name__
        del state['_service']

        # This is done because we can't pickle protobuf objects
        # TODO: use json conversion instead to avoid the call
        del state['_msg']

        return state

    def __setstate__(self, state):
        state['_service'] = importlib.import_module(state['_service_module_name'])
        del state['_service_module_name']

        self.__dict__.update(state)

    @classmethod
    def _generate_default_name(cls):
        raise NotImplementedError

    @classmethod
    def _get_by_id(cls, conn, conf, id):
        msg = cls._get_proto_by_id(conn, id)
        if msg:
            print("got existing {}: {}".format(cls.__name__, msg.id))
            # pylint: disable=no-value-for-parameter
            # this is only called on subclasses, so 3 params to cls() is correct
            return cls(conn, conf, msg)
        else:
            raise ValueError("{} with ID {} not found".format(cls.__name__, id))

    @classmethod
    def _get_proto_by_id(cls, conn, id):
        raise NotImplementedError

    # TODO: add prints about status
    @classmethod
    def _get_or_create_by_name(cls, conn, name, getter, creator, checker):
        if name is None:
            name = cls._generate_default_name()

        obj = getter(name)
        if obj is None:
            obj = creator(name)
        else:
            print("got existing {}: {}".format(cls.__name__, name))
            checker()
        return obj

    @classmethod
    def _get_by_name(cls, conn, conf, name, parent):
        msg = cls._get_proto_by_name(conn, name, parent)
        if msg:
            # pylint: disable=no-value-for-parameter
            # this is only called on subclasses, so 3 params to cls() is correct
            return cls(conn, conf, msg)
        else:
            return None

    @classmethod
    def _get_proto_by_name(cls, conn, name, parent):
        raise NotImplementedError

    @classmethod
    def _create(cls, conn, conf, *args, **kwargs):
        if 'name' in kwargs and kwargs['name'] is None:
            kwargs['name'] = cls._generate_default_name()

        msg = cls._create_proto(conn, *args, **kwargs)
        if msg:
            # pylint: disable=no-value-for-parameter
            # this is only called on subclasses, so 3 params to cls() is correct
            return cls(conn, conf, msg)
        else:
            return None

    @classmethod
    def _create_proto(cls, conn, *args, **kwargs):
        tags = kwargs.pop('tags', None)
        if tags is not None:
            kwargs['tags'] = _utils.as_list_of_str(tags)

        attrs = kwargs.pop('attrs', None)
        if attrs is not None:
            kwargs['attrs'] = [
                _CommonCommonService.KeyValue(key=key, value=_utils.python_to_val_proto(value, allow_collection=True))
                for key, value in six.viewitems(attrs)
            ]

        return cls._create_proto_internal(conn, *args, **kwargs)

    @classmethod
    def _create_proto_internal(cls, conn, ctx, name, desc=None, tags=None, attrs=None, date_created=None, **kwargs):  # recommended params
        raise NotImplementedError

    def log_code(self, exec_path=None, repo_url=None, commit_hash=None, overwrite=False):
        """
        Logs the code version.

        A code version is either information about a Git snapshot or a bundle of Python source code files.

        `repo_url` and `commit_hash` can only be set if `use_git` was set to ``True`` in the Client.

        Parameters
        ----------
        exec_path : str, optional
            Filepath to the executable Python script or Jupyter notebook. If no filepath is provided,
            the Client will make its best effort to find the currently running script/notebook file.
        repo_url : str, optional
            URL for a remote Git repository containing `commit_hash`. If no URL is provided, the Client
            will make its best effort to find it.
        commit_hash : str, optional
            Git commit hash associated with this code version. If no hash is provided, the Client will
            make its best effort to find it.
        overwrite : bool, default False
            Whether to allow overwriting a code version.

        Examples
        --------
        With ``Client(use_git=True)`` (default):

            Log Git snapshot information, plus the location of the currently executing notebook/script
            relative to the repository root:

            .. code-block:: python

                run.log_code()
                run.get_code()
                # {'exec_path': 'comparison/outcomes/classification.ipynb',
                #  'repo_url': 'git@github.com:VertaAI/experiments.git',
                #  'commit_hash': 'f99abcfae6c3ce6d22597f95ad6ef260d31527a6',
                #  'is_dirty': False}

            Log Git snapshot information, plus the location of a specific source code file relative
            to the repository root:

            .. code-block:: python

                run.log_code("../trainer/training_pipeline.py")
                run.get_code()
                # {'exec_path': 'comparison/trainer/training_pipeline.py',
                #  'repo_url': 'git@github.com:VertaAI/experiments.git',
                #  'commit_hash': 'f99abcfae6c3ce6d22597f95ad6ef260d31527a6',
                #  'is_dirty': False}

        With ``Client(use_git=False)``:

            Find and upload the currently executing notebook/script:

            .. code-block:: python

                run.log_code()
                zip_file = run.get_code()
                zip_file.printdir()
                # File Name                          Modified             Size
                # classification.ipynb        2019-07-10 17:18:24        10287

            Upload a specific source code file:

            .. code-block:: python

                run.log_code("../trainer/training_pipeline.py")
                zip_file = run.get_code()
                zip_file.printdir()
                # File Name                          Modified             Size
                # training_pipeline.py        2019-05-31 10:34:44          964

        """
        if self._conf.use_git:
            # verify Git
            try:
                repo_root_dir = _git_utils.get_git_repo_root_dir()
            except OSError:
                # don't halt execution
                print("unable to locate git repository; you may be in an unsupported environment")
                return
                # six.raise_from(OSError("failed to locate git repository; please check your working directory"),
                #                None)
            print("Git repository successfully located at {}".format(repo_root_dir))
        elif repo_url is not None or commit_hash is not None:
            raise ValueError("`repo_url` and `commit_hash` can only be set if `use_git` was set to True in the Client")

        if exec_path is None:
            # find dynamically
            try:
                exec_path = _utils.get_notebook_filepath()
            except (ImportError, OSError):  # notebook not found
                try:
                    exec_path = _utils.get_script_filepath()
                except OSError:  # script not found
                    print("unable to find code file; skipping")
        else:
            exec_path = os.path.expanduser(exec_path)
            if not os.path.isfile(exec_path):
                raise ValueError("`exec_path` \"{}\" must be a valid filepath".format(exec_path))

        # TODO: remove this circular dependency
        from .project import Project
        from .experiment import Experiment
        from .experimentrun import ExperimentRun
        if isinstance(self, Project):  # TODO: not this
            Message = self._service.LogProjectCodeVersion
            endpoint = "logProjectCodeVersion"
        elif isinstance(self, Experiment):
            Message = self._service.LogExperimentCodeVersion
            endpoint = "logExperimentCodeVersion"
        elif isinstance(self, ExperimentRun):
            Message = self._service.LogExperimentRunCodeVersion
            endpoint = "logExperimentRunCodeVersion"
        msg = Message(id=self.id)

        if overwrite:
            if isinstance(self, ExperimentRun):
                msg.overwrite = True
            else:
                raise ValueError("`overwrite=True` is currently only supported for ExperimentRun")

        if self._conf.use_git:
            try:
                # adjust `exec_path` to be relative to repo root
                exec_path = os.path.relpath(exec_path, _git_utils.get_git_repo_root_dir())
            except OSError as e:
                print("{}; logging absolute path to file instead")
                exec_path = os.path.abspath(exec_path)
            msg.code_version.git_snapshot.filepaths.append(exec_path)

            try:
                msg.code_version.git_snapshot.repo = repo_url or _git_utils.get_git_remote_url()
            except OSError as e:
                print("{}; skipping".format(e))

            try:
                msg.code_version.git_snapshot.hash = commit_hash or _git_utils.get_git_commit_hash()
            except OSError as e:
                print("{}; skipping".format(e))

            try:
                is_dirty = _git_utils.get_git_commit_dirtiness(commit_hash)
            except OSError as e:
                print("{}; skipping".format(e))
            else:
                if is_dirty:
                    msg.code_version.git_snapshot.is_dirty = _CommonCommonService.TernaryEnum.TRUE
                else:
                    msg.code_version.git_snapshot.is_dirty = _CommonCommonService.TernaryEnum.FALSE
        else:  # log code as Artifact
            if exec_path is None:
                # don't halt execution
                print("unable to find code file; you may be in an unsupported environment")
                return
                # raise RuntimeError("unable to find code file; you may be in an unsupported environment")

            # write ZIP archive
            zipstream = six.BytesIO()
            with zipfile.ZipFile(zipstream, 'w') as zipf:
                filename = os.path.basename(exec_path)
                if exec_path.endswith(".ipynb"):
                    try:
                        saved_notebook = _utils.save_notebook(exec_path)
                    except:  # failed to save
                        print("unable to automatically save Notebook;"
                              " logging latest checkpoint from disk")
                        zipf.write(exec_path, filename)
                    else:
                        zipf.writestr(filename, six.ensure_binary(saved_notebook.read()))
                else:
                    zipf.write(exec_path, filename)
            zipstream.seek(0)

            key = 'code'
            extension = 'zip'

            artifact_hash = _artifact_utils.calc_sha256(zipstream)
            zipstream.seek(0)
            basename = key + os.extsep + extension
            artifact_path = os.path.join(artifact_hash, basename)

            msg.code_version.code_archive.path = artifact_path
            msg.code_version.code_archive.path_only = False
            msg.code_version.code_archive.artifact_type = _CommonCommonService.ArtifactTypeEnum.CODE
            msg.code_version.code_archive.filename_extension = extension
        # TODO: check if we actually have any loggable information
        msg.code_version.date_logged = _utils.now()

        data = _utils.proto_to_json(msg)
        response = _utils.make_request("POST",
                                       self._request_url.format(endpoint),
                                       self._conn, json=data)
        if not response.ok:
            if response.status_code == 409:
                raise ValueError("a code version has already been logged")
            else:
                _utils.raise_for_http_error(response)

        if msg.code_version.WhichOneof("code") == 'code_archive':
            # upload artifact to artifact store
            # pylint: disable=no-member
            # this method should only be called on ExperimentRun, which does have _get_url_for_artifact()
            url = self._get_url_for_artifact("verta_code_archive", "PUT", msg.code_version.code_archive.artifact_type).url

            response = _utils.make_request("PUT", url, self._conn, data=zipstream)
            _utils.raise_for_http_error(response)

    def get_code(self):
        """
        Gets the code version.

        Returns
        -------
        dict or zipfile.ZipFile
            Either:
                - a dictionary containing Git snapshot information with at most the following items:
                    - **filepaths** (*list of str*)
                    - **repo** (*str*) – Remote repository URL
                    - **hash** (*str*) – Commit hash
                    - **is_dirty** (*bool*)
                - a `ZipFile <https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile>`_
                  containing Python source code files

        """
        # TODO: remove this circular dependency
        from .project import Project
        from .experiment import Experiment
        from .experimentrun import ExperimentRun
        if isinstance(self, Project):  # TODO: not this
            Message = self._service.GetProjectCodeVersion
            endpoint = "getProjectCodeVersion"
        elif isinstance(self, Experiment):
            Message = self._service.GetExperimentCodeVersion
            endpoint = "getExperimentCodeVersion"
        elif isinstance(self, ExperimentRun):
            Message = self._service.GetExperimentRunCodeVersion
            endpoint = "getExperimentRunCodeVersion"
        msg = Message(id=self.id)
        data = _utils.proto_to_json(msg)
        response = _utils.make_request("GET",
                                       self._request_url.format(endpoint),
                                       self._conn, params=data)
        _utils.raise_for_http_error(response)

        response_msg = _utils.json_to_proto(_utils.body_to_json(response), Message.Response)
        code_ver_msg = response_msg.code_version
        which_code = code_ver_msg.WhichOneof('code')
        if which_code == 'git_snapshot':
            git_snapshot_msg = code_ver_msg.git_snapshot
            git_snapshot = {}
            if git_snapshot_msg.filepaths:
                git_snapshot['filepaths'] = git_snapshot_msg.filepaths
            if git_snapshot_msg.repo:
                git_snapshot['repo_url'] = git_snapshot_msg.repo
            if git_snapshot_msg.hash:
                git_snapshot['commit_hash'] = git_snapshot_msg.hash
                if git_snapshot_msg.is_dirty != _CommonCommonService.TernaryEnum.UNKNOWN:
                    git_snapshot['is_dirty'] = git_snapshot_msg.is_dirty == _CommonCommonService.TernaryEnum.TRUE
            return git_snapshot
        elif which_code == 'code_archive':
            # download artifact from artifact store
            # pylint: disable=no-member
            # this method should only be called on ExperimentRun, which does have _get_url_for_artifact()
            url = self._get_url_for_artifact("verta_code_archive", "GET", code_ver_msg.code_archive.artifact_type).url

            response = _utils.make_request("GET", url, self._conn)
            _utils.raise_for_http_error(response)

            code_archive = six.BytesIO(response.content)
            return zipfile.ZipFile(code_archive, 'r')  # TODO: return a util class instead, maybe
        else:
            raise RuntimeError("unable find code in response")

    def _get_workspace_name_by_id(self, workspace_id):
        # try getting organization
        response = _utils.make_request(
            "GET",
            "{}://{}/api/v1/uac-proxy/organization/getOrganizationById".format(self._conn.scheme, self._conn.socket),
            self._conn, params={'org_id': workspace_id},
        )
        try:
            _utils.raise_for_http_error(response)
        except requests.HTTPError:
            # try getting user
            response = _utils.make_request(
                "GET",
                "{}://{}/api/v1/uac-proxy/uac/getUser".format(self._conn.scheme, self._conn.socket),
                self._conn, params={'user_id': workspace_id},
            )
            _utils.raise_for_http_error(response)

            # workspace is user
            return _utils.body_to_json(response)['verta_info']['username']
        else:
            # workspace is organization
            return _utils.body_to_json(response)['organization']['name']


'''
Buffet: A buffet-style restaurant offers only five basic foods. Think of five simple foods, and store them in a tuple.
• Use a for loop to print each food the restaurant offers.
• Try to modify one of the items, and make sure that Python rejects the change.
• The restaurant changes its menu, replacing two of the items with different foods. Add a line that rewrites the tuple, and then use a for loop to print each of the items on the revised menu.
'''
BuffetFoods = ('Egusi', 'Draw Soup', 'Fried Rice', 'White Rice and Stew')
for AvailableFoods in BuffetFoods:
    print(AvailableFoods)

    '''
• Try to modify one of the items, and make sure that Python rejects the change.

    '''
# AvailableFoods[0] = 'Jollof Rice'
# print(AvailableFoods)

'''
• The restaurant changes its menu, replacing two of the items with different foods. Add a line that rewrites the tuple, and then use a for loop to print each of the items on the revised menu.

'''
BuffetFoods = ('yello Soup', 'Draw Soup', 'Rice',
               'White Rice and Stew', 'Bango soup')
for AvailableFoods in BuffetFoods:
    print(AvailableFoods)


import sys

input = sys.stdin.readline

N, M = map(int, input().split())
r, c, d = map(int, input().split())
room = [list(map(int, input().split())) for _ in range(N)]

dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]
answer = 0

while True:
    if room[r][c] == 0:
        room[r][c] = 2
        answer += 1

    uncleaned_exist = False
    for i in range(1, 5):
        nd = (d - i) % 4
        nr, nc = r + dr[nd], c + dc[nd]
        if room[nr][nc] == 0:
            uncleaned_exist = True
            r, c = nr, nc
            d = nd
            break
    if uncleaned_exist:
        continue

    nr, nc = r - dr[d], c - dc[d]
    if room[nr][nc] != 1:
        r, c = nr, nc
    else:
        break

print(answer)


import sys

def dfs(adj, start_node):
    visit = list()
    stack = list()

    stack.append(start_node)
    first = True
    while stack:
        node = stack.pop()
        if node not in visit and first ==False:
            visit.append(node)
            stack.extend(adj[node])
        elif node not in visit and first:
            stack.extend(adj[node])
            first = False

    for i in range(len(visit)):
        result[start_node][visit[i]] = 1

N = int(sys.stdin.readline())
adj = dict()
result = [[0]*N for _ in range(N)]
for i in range(N):
    line = list(map(int,sys.stdin.readline().split()))
    value = []
    for j in range(N):
        if(line[j] == 1):
            value.append(j)
            result[i][j]= 1
    adj[i] = value

for i in range(N):
    dfs(adj,i)

for i in range(N):
    line = result[i]
    for j in range(N):
        print(line[j],end =' ')
    print()


"""This script primes the Scraper and reruns it once a month.

This script runs the Scraper and sends a concluding PUT REQUEST
to the HotspotHousing API once the Scraper is finished. As soon
as HotspotHousing receives the PUT REQUEST, the flat data shown
on the maps will be updated. Hereafter this script will sleep
for one month until the same thing happens again.
"""
# Standard library
from time import sleep
import subprocess
import requests

# Timeout is measured in seconds
TIMEOUT = 60

while True:
    sleep(TIMEOUT * 20160)  # sleep for 2 weeks
    try:
        requests.post('http://HotspotHousing:8080/api/before/')
    except Exception as error:
        print(error)
    else:
        command = 'scrapy crawl ImmobilienScout'
        subprocess.run(command, shell=True)
        try:
            requests.post('http://HotspotHousing:8080/api/after/')
        except Exception as error:
            print(error)


import requests

url = "https://api.removal.ai/2.0/remove/"

payload = "{\r\n    \"high_resolution\" : 1,\r\n    \"demo\" : 0,\r\n    \"file\" : \"your_image_url or your image base64\",\r\n    \"get_base64\" : 0\r\n}"
headers = {
  'Rm-Token': 'your token',
  'Content-Type': 'application/json',
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))


# import library
import requests
from bs4 import BeautifulSoup
import re
import time
import os
import psutil

# get url from user and separate website by languages (English, Chinese, Japanese)
def get_info():
    '''
    Get user's information of website and language.
    Args: NA
    Returns:
        (str) url - user's input of url
        (str) lang - user's input of language
    '''

    while True:
            try:
                url = input('\nPlease enter the website:\n')
                # request for a webpage
                res = requests.get(url)
                #check requests ststus
                if res.status_code == 200:
                    break
                else:
                    print('Please try another website.')
            except:
                print('Please enter a valid website.')
                continue

    while True:
        try:
            lang = input('\nPlease select the language of result (English, Chinese or Japanese):\n').lower()
            if lang in ['english', 'chinese', 'japanese']:
                break
            else:
                print('Please enter a valid language.')
        except:
            continue
    return url, lang

def get_html_text(url):
    '''
    Get html from website and get rid of html tag, and then convert to beautifulsoup object.
    Args:
        (str) url - website url
    Returns:
        (str) text - text without html tags
    '''

    res = requests.get(url)
    # get html from url
    html_text = res.text
    # change to beautifulsoup object
    soup = BeautifulSoup(html_text, 'html.parser')
    # get rid of extra spaces and line breaks and change to string separete by space
    text = ' '.join(soup.stripped_strings)
    return text

def text_process_en(text):
    '''
    Function for process if the selected language is English.
    Args:
        (str) text - text without html tags
    Returns:
        (list) list - a list containing each word
    '''

    # get rid of punctuations and special characters
    clean_text = re.sub(r"[^a-zA-Z0-9]+", ' ', text)
    # get rid of numbers
    clean_text = re.sub(r'\d+', '', clean_text)

    # change text to a list of words
    list =  clean_text.split()
    return list

def text_process_jp(text):
    '''
    Function for process if the selected language is Japanese.
    Args:
        (str) text - text without html tags
    Returns:
        (list) list - a list containing each hanzi
    '''

    # get rid of punctuations and special characters
    punctuation_jp = '[!"#$%&()*+,-./:;<=>?@[\\]：^_`{|}~「」〔〕“”〈〉『』【】＆＊・（）＄＃＠。、？！｀＋￥％]'
    clean_text = re.sub(punctuation_jp, "", text)
    # get rid of English characters and numbers
    clean_text = re.sub("[A-Za-z0-9]", '',clean_text)
    # get rid of spaces
    clean_text = clean_text.replace(' ', '')

    # change text to a list of hanzi
    list = []
    for i in clean_text:
        list.append(i)
    return list

def text_process_ch(text):
    '''
    Function for process if the selected language is Chinese.
    Args:
        (str) text - text without html tags
    Returns:
        (list) list - a list containing each hanzi
    '''

    # get rid of punctuations and special characters
    punctuation_ch = '[！？。＂＃＄％＆＇/（）＊＋，－／|():：；＜＝＞＠［＼］＾＿｀｛｜｝～｟｠｢｣､、·〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏.]'
    clean_text = re.sub(punctuation_ch, '',text)
    # get rid of English characters and numbers
    clean_text = re.sub("[A-Za-z0-9]", '',clean_text)
    # get rid of spaces
    clean_text = clean_text.replace(' ', '')

    # change text to a list of hanzi
    list = []
    for i in clean_text:
        list.append(i)
    return list

def sorted_result(list):
    '''
    Function to sort the words by frequency.
    Args:
        (list) list - a list containing each word (English) or hanzi (Chinese, Japanese)
    Returns:
        (dic) result - a dictionary containing each word (English) or hanzi (Chinese, Japanese) and it's frequency
    '''

    # create a dictionary to store words and it's frenquency
    dic = {}
    for word in list:
        if word in dic:
            dic[word] += 1
        else:
            dic[word] = 1

    # list containing sorted the words by frenquency
    list_sort = sorted(dic, key = dic.get, reverse = True)
    # create a dictionary to show sorted results
    result = {}
    for i in list_sort:
        result[i] = dic[i]
    return result

def export_csv(result_dic):
    '''
    Function to export the result to a csv file.
    Args:
        (dic) result_dic - a dictionary containing each word (English) or hanzi (Chinese, Japanese) and it's frequency
    Returns:
        NA
    '''

    with open('data.csv', 'w', encoding = 'utf-8') as f:
        for key in result_dic:
            f.write('%s, %i\n' % (key, result_dic[key]))

# def of memory and time?

def main():
    while True:
        url, lang = get_info()
        # check start time
        start = time.time()
        # check start memory
        pid = os.getpid()
        p = psutil.Process(pid)
        info_start = p.memory_full_info().uss/1024

        soup = get_html_text(url)
        if lang == 'english':
            list_en = text_process_en(soup)
            result = sorted_result(list_en)
            export_csv(result)
        elif lang == 'japanese':
            list_jp = text_process_jp(soup)
            result = sorted_result(list_jp)
            export_csv(result)
        else:
            list_ch = text_process_ch(soup)
            result = sorted_result(list_ch)
            export_csv(result)

        print('The result file was generated.')
        # check end time and calculate running time
        end = time.time()
        print('running time: %s seconds' % str(end - start))
        # check end memory
        info_end = p.memory_full_info().uss/1024
        print('running memory: %s KB' % str(info_end - info_start))

        while True:
            try:
                restart = input('\nWould you like to restart? Enter yes or no (Please change the saved file name if you want to restart).\n').lower()
                if restart in ['yes', 'no']:
                    break
                else:
                    print('Please enter a valid input.')
            except:
                continue

        if restart != 'yes':
            break

if __name__ == "__main__":
    main()


import argparse
import json
import logging
import time
import signal
from os import getpid
import http1

import utils
import notifications

### GLOBAL VARIABLES ###
running = True

### LOGGING CONF ###
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s: %(message)s'
)
logger = logging.getLogger('imapmanager')

def signal_handler(_signal, _frame):
    global running
    running = False
    logger.info('Ending signal handled, ending the script...')

def main():
    logger.info('--------------------')

    # Check python3 is used
    utils.check_python_version()

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--conf', '-c', dest='config_path')
    args = parser.parse_args()

    # Open conf and load parameters
    config = utils.open_and_load_config(args)

    level = logging.getLevelName(config.get(utils.SECTION_DEFAULT_NAME, utils.LOG_LEVEL_NAME))
    logger.setLevel(level)

    api_url = config.get(utils.SECTION_DEFAULT_NAME, utils.API_URL_NAME)
    id_servers = config.get(utils.SECTION_DEFAULT_NAME, utils.ID_SERVERS_NAME).split(',')
    polling_interval = config.get(utils.SECTION_DEFAULT_NAME, utils.POLLING_INTERVAL_NAME)
    zones_desired = set()
    for zone in set(config.items(utils.SECTION_ZONES_NAME)):
        zones_desired.add(zone[1])

    last_status = False
    logger.info('Calling kimsufi API on url=%s with PID=%d', api_url, getpid())
    while running:
        server_found = False
        try:
            response = http1.get(api_url)
            if response.status == 200:
                struct = json.loads(response.body)
                for item in struct:
                    zones = [z['datacenter'] for z in item['datacenters'] if z['availability'] not in ('unavailable', 'unknown')]
                    if set(zones).intersection(zones_desired) and item['hardware'] in id_servers:
                        server_found = True
                        if not last_status:
                            logger.info('Found available server, sending notifications...')
                            notifications.send_notifications(config, True)
                            last_status = True
                        else:
                            logger.debug('Notification already sent, passing...')
                        # Do not iterate on other items as we already found an available server
                        break
                if not server_found:
                    logger.debug('No server available')
                    if last_status:
                        logger.info('Server not available anymore')
                        notifications.send_notifications(config, False)
                        last_status = False
            else:
                logger.error('Calling API: status=%d msg=%s', response.status, response.message)
        except Exception as exc:
            logger.error('Calling API: err=%s', str(exc))
        finally:
            # If signal occurs during process, there is no need to sleep
            if running:
                time.sleep(float(polling_interval))

    logger.info('kimsufi script ended.')

if __name__ == '__main__':
    main()


import os
import requests
import json

fname = "dataToLoad.txt"
t_file = open(fname, 'r')
righe = t_file.readlines()
if len(righe) > 0:
	for line in righe:
		try:
			data = json.loads(line)
			res = requests.post('http://localhost:5000/api/measures', json=data)
			righe.remove(line)
		except requests.exceptions.RequestException as e:  # This is the correct syntax
			print("No Connection to server.")
else:
	print ("No more lines to import")

open(fname, 'w').writelines(righe)


#!/usr/bin/env python3

# Created by Amir Mersad
# Created on September 2019
# This program adds positive numbers

def main():
    # This function adds positive numbers
    counter = 0
    zero = 0
    result = 0

    # Input, Process and Output
    try:
        counter = input("how many numbers do you want to add: ")
        counter = int(counter)
        for zero in range(1, counter + 1):
            usernumber = input("enter a number to add: ")
            usernumber = int(usernumber)
            if usernumber <= 0:
                continue
            else:
                result = usernumber + result
        print(result)
    except(Exception):
        print("Wrong input!!!")

if __name__ == "__main__":
    main()
a


# -*- coding: utf-8 -*-
"""
    Created by Huang
    Date: 2018/8/12
"""
from flask import current_app
from flask.json import jsonify

from app.libs.enums import ClientTypeEnum
from app.libs.redprint import Redprint
from app.models.user import User
from app.validators.forms import ClientForm
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

api = Redprint('token')

@api.route('', methods=['POST'])
def get_token():
    """
    登录
    :return:
    """
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: User.verify,
    }
    identity = promise[form.type.data](
        form.account.data,
        form.secret.data
    )
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(identity['uid'],
                                form.type.data,
                                identity['scope'],
                                expiration)
    # Token
    t = {
        'token': token.decode('ascii')
    }
    return jsonify(t), 201

def generate_auth_token(uid, ac_type, is_admin=None, expiration=7200):
    """
    生成令牌
    :param uid: 客户id
    :param ac_type: 客户端种类
    :param scope: 权限作用域
    :param expiration: 过期时间
    :return:
    """
    s = Serializer(current_app.config['SECRET_KEY'], expires_in=expiration)

    return s.dumps({
        'uid': uid,
        'type': ac_type.value,
        'scope': is_admin
    })


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec  1 21:59:44 2020

TwoSum Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

    2 <= nums.length <= 103
    -109 <= nums[i] <= 109
    -109 <= target <= 109
    Only one valid answer exists

@author: gauravanand
"""

def twoSum(ArithmeticErrornums, target):
    complement_table = dict()
    index = 0
    for element in nums:
        if element not in complement_table:
            complement_table[target-element] = index
            index += 1
        else:
            return complement_table[element],index

if __name__ == "__main__":
    nums = [2,7,11,15]
    print(twoSum(nums,9))


total = 0
def increment ():
    """a simple fuction printing 1 to 10"""
    global total
while total <= 9:
        total += 1
        print(total)

increment()
print(increment.__doc__)

"""""""""""
instructor ="colt"
def sayhello():

    a=f"hello {instructor}"
    print (a)
sayhello()

"""""""""""


from datetime import datetime
from elasticsearch import Elasticsearch
import json
from pprint import pprint

es = Elasticsearch()

def get_graphs(search_terms):
    graphs = {"graph_data":[]}
    graphs["graph_data"].append(get_types())
    return graphs

def get_types():
    query = {
              "size": 0,
              "aggregations": {
                "movie_type": {
                  "terms": {
                    "field": "kind.keyword"
                  }
                }
              }
            }
    res = es.search(index="movies", body=query)
    result = {"graph":"pie", "name": "Film Type", "data":[]}
    data = [
        {
            "name": "Germany",
            "value": 8940000
        },
        {
            "name": "USA",
            "value": 5000000
        },
        {
            "name": "France",
            "value": 7200000
        },
        {
            "name": "UK",
            "value": 6200000
        }
    ]
    for item in res["aggregations"]["movie_type"]["buckets"]:
        result["data"].append({"name":item["key"], "value":item["doc_count"]})

    return result


import requests
import json
from labels import labels
import cv2

def get_label(img):
	lbls = [l.lower() for l in labels]

	lbls = set(lbls)

	url = "https://eastus.api.cognitive.microsoft.com/vision/v3.0/tag?language=en"

	payload = {}
	files = [
	  ('', cv2.imencode('.jpg', img)[1])
	]
	headers = {
	  'Ocp-Apim-Subscription-Key': '[API KEY]'
	}

	response = requests.request("POST", url, headers=headers, data = payload, files = files)

	res = json.loads(response.text)

	print(res)

	for t in res['tags']:
		if t['name'] in lbls:
			return t['name']

if __name__ == '__main__':
	get_label(cv2.imread('test.png'))


import EM_algorithm as em
import math
import random
from copy import deepcopy
import numpy as np
from copy import deepcopy
from scipy import stats

def step_zero(estim_c,c_max,n):
    s=np.zeros(c_max)
    for i in range(estim_c):
        s[i]=random.randint(1,n)
    random.shuffle(s)
    return s

def neighboorhood(s,i,n):
    Neighbor=list()
    if s[i]==0:
        Neighbor.append(s)
        for j in range(1,n+1):
            if j not in s:
                s2= deepcopy(s)
                s2[i]=j
                Neighbor.append(s2)
    else:
        Neighbor.append(s)
        s2 = deepcopy(s)
        s2[i] = 0
        Neighbor.append(s2)
        for j in range(1, n + 1):
            if j not in s:
                s3 = deepcopy(s)
                s3[i] = j
                Neighbor.append(s3)
    return Neighbor

def size_collusion(s):
    size = 0
    for colluder in s:
        if colluder > 0:
            size += 1
    return size

def proba_s(c_max,size_s,n):
    n_fac=math.factorial(n)
    return (1/c_max) *n_fac/(math.factorial(size_s)*math.factorial(n-size_s))

def post_proba1(z,secret_fingerprint,s, m, mu_list, sigma):
    p = 1
    size_s=size_collusion(s)
    for i in range(m):
        ki = 0
        for colluder in s:
            if colluder > 0:
                ki += secret_fingerprint[int(colluder)-1][i]
        p *= stats.norm.pdf(z[i], loc=mu_list[size_s-1][ki], scale=sigma)
    return p

def distrib(p):
    cumul_p=[p[0]]
    for i in range(1,len(p)):
        cumul_p.append(p[i]+cumul_p[i-1])
    r=random.random()
    index_tirage=0
    while(r>cumul_p[index_tirage]):
        index_tirage+=1
    print("index", index_tirage)
    return index_tirage

def gibbs_sampler(z,secret_fingerprint,c_max,s,n,mu_list,sigma,m):
    i=random.randint(1,c_max-1)
    Neighbors=neighboorhood(s,i,n)
    p=list()
    for s_tilde in Neighbors:
        proba_s(c_max,size_collusion(s_tilde),n)
        p.append(proba_s(c_max,size_collusion(s_tilde),n)*post_proba1(z,secret_fingerprint,s_tilde,m,mu_list,sigma))
    p_norm=sum(p)
    p=np.array(p)/p_norm
    print(len(p))

    tirage=Neighbors[distrib(p)]
    return tirage

def algo(z,secret_fingerprint,c_max,n,mu_list,sigma,m,estim_c, T,K):
    s0=step_zero(estim_c,c_max,n)
    tirage = gibbs_sampler(z,secret_fingerprint,c_max,s0,n,mu_list,sigma,m)
    for t in range(T):
        tirage=gibbs_sampler(z,secret_fingerprint,c_max,tirage,n,mu_list,sigma,m)
        print("t",t,"tirage",tirage)
    markov_tirages=list()
    for t in range(K):
        tirage=gibbs_sampler(z,secret_fingerprint,c_max,tirage,n,mu_list,sigma,m)
        markov_tirages.append(tirage)
        print("t", t, "tirage", tirage)
    return markov_tirages

def margins(tirages,K,n):
    margin=list()
    for j in range(1,n+1):
        sum=0
        for k in range(K):
            if j in tirages[k]:
                sum+=1
        margin.append(sum/K)
    return margin


from rest_framework import novaUtils
from rest_framework import quantumUtils
from rest_framework import cinderUtils
from rest_framework import svt_tester_base
from rest_framework import svt_tester_base
from rest_framework import configUtils
import pexpect
import subprocess
import paramiko
import re
import time
import os
import sys
from subprocess import Popen, PIPE, STDOUT

volume_name_list = 'volume_name_list'
vol_onboard_count = 'vol_onboard_count'
#Image_name = 'Image_name'
storageName = 'svc_display_name_list'

class SvtonboardVolume(svt_tester_base.SvtTesterBase):
    """

    [test_1020_onboardvolumes]
    volume_name_list = ['DND_Ub']
    svc_display_name_list = ['svc_61']
    vol_onboard_count = 9

    """
    required_options = [volume_name_list, vol_onboard_count]

    def onBoardVolume(self, storageName, volume_name_list, vol_onboard_count, ressto, cinderUrl, token):

        for s in range(len(storageName)):
            storagedisplayname = storageName[s]
            #volume_name = volume_name_list[s]
            argumNo = str(s)
            print("Validating for " + argumNo +  " storage " + storagedisplayname + " argument")
            print("Storage name given in config file =>", storagedisplayname)
            for j in range(len(ressto[1]['storage_providers'])):
                host_name = ressto[1]['storage_providers'][j]['storage_hostname']
                servicelist = ressto[1]['storage_providers'] [j]['service']
                storagebasename = servicelist['host_display_name']
                #print servicelist
                print("storage base name=>", storagebasename)
                print("storage host name=>", host_name)
                # Check if svc display name matches in pvc
                if storagebasename in storagedisplayname:
                    print("Storgae found in powervc")
                    print("***********************")
                    for n in range(len(volume_name_list)):
                        onboardcount = 0;
                        #storagedisplayname = storageName[s]
                        volume_name = volume_name_list[n]
                        print("Check volume existence:", volume_name)
                        # Get list of all volumes from that storgae
                        _, allvolumes = cinderUtils.listExistingVolumes(cinderUrl, token, host_name)
                        #print "all volumes", allvolumes
                        allvol = allvolumes['volumes']
                        for volume in allvol:
                            #print 'name=', volume['name'], 'volumeid in storage=', volume['id']
                            # Check if volume name from config file exist in storage
                            if volume_name in volume['name'] :
                                print("volume managed=>", volume['managed'])
                                #check if volume is managed by other powervc, if its false, then onboard volume.
                                if volume['managed'] == False:
                                    print("Before onboard count is", + onboardcount)
                                    if vol_onboard_count > onboardcount:
                                        configUtils.onboard_vol(cinderUrl, token, volume['id'], host_name)
                                        onboardcount += 1
                                        print("count is", + onboardcount)

                                        time.sleep(10)
                                        print("onboarding volume", volume['id'] + "in", host_name)
                                        #volumestoragelist.append(volume['id'])
                                    else:
                                        print("User input threshold is reached. No need to onboard additional volumes from this storage")

                else:
                    print("Storage not found in ", storagebasename)

    def test_1020_onboardvolumes(self):

        options_missing = False
        for option in self.required_options:
            if not self.config.has_option(self.config_section, option):
                print('option=', option, 'not found in configuration file')
                options_missing = True
        if options_missing:
            print('Provide missing options to the configuration file.')
            os._exit(1)
        volume_list = []
        volumestoragelist = []
        #novaUrl = self.getServiceUrl('compute')
        token = self.authent_id
        cinderUrl = self.getServiceUrl('volume')
        print(cinderUrl)
        ressto = cinderUtils.showAllStorageProvidersDetail(cinderUrl, token)
        print(ressto[1]['storage_providers'])
        storageName = self.config_get('svc_display_name_list')
        volume_name_list = self.config_get('volume_name_list')
        vol_onboard_count = int(self.config_get('vol_onboard_count'))
        self.onBoardVolume(storageName, volume_name_list, vol_onboard_count, ressto, cinderUrl, token)

    def test_1020_onboardvolumes2(self):

        options_missing = False
        for option in self.required_options:
            if not self.config.has_option(self.config_section, option):
                print('option=', option, 'not found in configuration file')
                options_missing = True
        if options_missing:
            print('Provide missing options to the configuration file.')
            os._exit(1)
        volume_list = []
        volumestoragelist = []
        #novaUrl = self.getServiceUrl('compute')
        token = self.authent_id
        cinderUrl = self.getServiceUrl('volume')
        print(cinderUrl)
        ressto = cinderUtils.showAllStorageProvidersDetail(cinderUrl, token)
        print(ressto[1]['storage_providers'])
        storageName = self.config_get('svc_display_name_list')
        volume_name_list = self.config_get('volume_name_list')
        vol_onboard_count = int(self.config_get('vol_onboard_count'))
        self.onBoardVolume(storageName, volume_name_list, vol_onboard_count, ressto, cinderUrl, token)

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


# Требуется вывести все целые степени двойки (т.е. числа вида 2k), не превосходящие числа N.

import os

os.system('cls')

number = int(input('Enter number: '))
degree = 1
while number >= degree:
    print(degree, end=' ')
    degree = degree * 2


import numpy as np
import matplotlib.pyplot as plt
import scipy
import xarray as xr

Rz=6.378e6

def direction_to_px(d, observer):
    """
    Calculates direction from the observer to all pixels in the grid
    :param d: DataArray with altitudes as a function of lat and lon
    :param observer: tuple of observer coordinates (lat,lon)
    :return: DataArray with direction vectors as a function of coordinates
    """
    latp = np.radians(d.lat)
    lonp = np.radians(d.lon)
    la, lo = np.meshgrid(latp, lonp)
    lato = np.radians(observer[0])
    lono = np.radians(observer[1])
    d = np.dstack((la.T - lato, lo.T - lono))
    dire = d / np.dstack([np.linalg.norm(d, axis=2)] * 2)
    directions = xr.DataArray(dire, coords={'lat': latp, 'lon': lonp, 'comp': [1, 2]}, dims=('lat', 'lon', 'comp'))
    print(np.shape(d), np.shape(dire), np.shape(d))
    return directions

def solar_shades(d, sunphi, sunelev):
    """
    WIP: Sinmplified calculation of illuminated mountain faces - projection of surface graidient to the direction of sunrayss

    :param d: DataArray with altitudes as a function of lat and lon
    :param sunphi: azimuth of the sun 0 is the local meridian
    :param sunelev: elevation angle of the sun
    :return:
    """""
    u, v = np.gradient(d.data)
    grad = np.sqrt(u ** 2 + v ** 2)
    gradx = u / np.nanmax(grad)
    grady = v / np.nanmax(grad)
    sunx = gradx * np.sin(sunelev) * np.cos(sunphi)  # is the sun elevation correctly implemented?
    # - scalar product would be better
    suny = grady * np.sin(sunelev) * np.sin(sunphi)
    sun = d.copy()
    sun.data = sunx + suny
    return sun

def gradient_vision_direction(d, observer):
    """
    Calculatiats the projection of the surface gradients to the direction of the vision of the observer
    flat Earth aprox ...

    :param d: DataArray with altitudes as a function of lat and lon
    :param observer: tuple of observer coordinates (lat,lon)
    :return: DataArray with gradiant value in the direction
    """

    u, v = np.gradient(d.data)
    grad = np.sqrt(u ** 2 + v ** 2)
    gradx = u / np.nanmax(grad)
    grady = v / np.nanmax(grad)
    dirangle_xa = direction_to_px(d, observer)
    print(dirangle_xa)
    dirangle = dirangle_xa.data
    visgrad = d.copy()
    df = gradx * dirangle[:, :, 0] + grady * dirangle[:, :, 1]
    visgrad.data = df
    # plt.imshow(df)
    return visgrad

# test calls
# d = load_grt('CR_SR.grd')
# visgrad = gradient_vision_direction(d, (49.1, 20))
# limits = [d.lon[0], d.lon[-1], d.lat[0], d.lat[-1]]
# phis = np.linspace(-np.pi / 2.0, np.pi / 2.0, 2)
#
# fig2, ax2 = plt.subplots()
# ax2.imshow(visgrad.data, origin='bottom', vmin=0, vmax=np.nanmax(visgrad.data), extent=limits, aspect='equal')
# ax2.scatter([20], [49.1], s=10, color='r')
#
# i = 0
# for phi in phis:
#     i = i + 1
#     limits = [19, 21, 48.5, 49.8]
#     sun = solar_shades(d.loc[48.5:49.8, 19:21], phi, np.cos(phi))
#     figs, ax = plt.subplots(figsize=(15, 10))
#     ax.imshow(sun.data, origin='bottom', vmin=0, vmax=np.nanmax(sun.data), extent=limits, cmap=plt.get_cmap('jet'),
#               aspect='equal')  # ,alpha=0.6)
#     plt.tight_layout()
#     plt.savefig('Movie/JET_sun_phi_{}_Tatry.png'.format(i))


def find_velocity(x, y, xvel, yvel, xmin, xmax, ymin, ymax, orxvel, oryvel, top):
    x += xvel
    y += yvel
    if (xvel > 0):
        xvel -= 1
    elif xvel < 0:
        xvel += 1
    yvel -= 1
    if y > top[0]:
        top[0] = y
    if x > xmax or y < ymax:
        return
    if x >= xmin and x <= xmax and y <= ymin and y >= ymax:
        print("possible! x: ", orxvel, " y: ", oryvel, "top: ", top[0])
        return
    find_velocity(x, y, xvel, yvel, xmin, xmax, ymin, ymax, orxvel, oryvel, top)

f = open("input.txt", "r").read().splitlines()

xmin = int(f[0])
xmax = int(f[1])
ymin = int(f[2])
ymax = int(f[3])

top = [1]
top[0] = 0
for j in range (-200, 200):
    for i in range (-20, 200):
        find_velocity(0, 0, i, j, xmin, xmax, ymin, ymax, i, j, top)


import positioning

class GPS(object):

    def __init__(self):
        self.seekingSatellites = False
        self.active = False
        self.heading = None
        self.position = None
        self._observers = []

    def addObserver(self, observer):
        self._observers.append(observer)

    def start(self):
        self.seekingSatellites = True
        positioning.select_module(positioning.default_module())
        positioning.set_requestors([{"type":"service", "format":"application", "data":"test_app"}])
        positioning.position(course=1,satellites=1, callback=self._dataReceived, interval=1000000, partial=0)

    def stop(self):
        self.active = False
        self.seekingSatellites = False
        positioning.stop_position()

    def _dataReceived(self, event):
        self.seekingSatellites = False
        self.active = True
        pos = event['position']
        lat = pos['latitude']
        lon = pos['longitude']
        if str(lat) != 'nan' and str(lon) != 'nan':
            self.position = (lat,lon)

        heading = event['course']['heading']
        if str(heading) != 'nan':
            self.heading = heading
        self._callObservers()

    def _callObservers(self):
        for obs in self._observers:
            obs()


from __future__ import division

import torch
import torch.nn as nn

from mmdet.core import (bbox2result, bbox2roi, build_assigner, build_sampler,
                        merge_aug_masks)
from .. import builder
from ..registry import DETECTORS
from .base import BaseDetector
from .test_mixins import RPNTestMixin, BBoxTestMixin
from mmdet.models.utils import Scale, Scale_channel
from icecream import ic
import mmcv
@DETECTORS.register_module
class CascadeRCNN_pair(BaseDetector, RPNTestMixin, BBoxTestMixin):

    def __init__(self,
                 num_stages,
                 backbone,
                 neck=None,
                 shared_head=None,
                 rpn_head=None,
                 bbox_roi_extractor=None,
                 bbox_head=None,
                 mask_roi_extractor=None,
                 mask_head=None,
                 train_cfg=None,
                 test_cfg=None,
                 pretrained=None,
                 pair_train=False,
                 normal_train=False,
                 style=None):
        assert bbox_roi_extractor is not None
        assert bbox_head is not None
        super(CascadeRCNN_pair, self).__init__()

        self.num_stages = num_stages
        self.backbone = builder.build_backbone(backbone)

        if neck is not None:
            self.neck = builder.build_neck(neck)

        if rpn_head is not None:
            self.rpn_head = builder.build_head(rpn_head)

        if shared_head is not None:
            self.shared_head = builder.build_shared_head(shared_head)

        if bbox_head is not None:
            self.bbox_roi_extractor = nn.ModuleList()
            self.bbox_head = nn.ModuleList()
            if not isinstance(bbox_roi_extractor, list):
                bbox_roi_extractor = [
                    bbox_roi_extractor for _ in range(num_stages)
                ]
            if not isinstance(bbox_head, list):
                bbox_head = [bbox_head for _ in range(num_stages)]
            assert len(bbox_roi_extractor) == len(bbox_head) == self.num_stages
            for roi_extractor, head in zip(bbox_roi_extractor, bbox_head):
                self.bbox_roi_extractor.append(
                    builder.build_roi_extractor(roi_extractor))
                self.bbox_head.append(builder.build_head(head))

        if mask_head is not None:
            self.mask_head = nn.ModuleList()
            if not isinstance(mask_head, list):
                mask_head = [mask_head for _ in range(num_stages)]
            assert len(mask_head) == self.num_stages
            for head in mask_head:
                self.mask_head.append(builder.build_head(head))
            if mask_roi_extractor is not None:
                self.share_roi_extractor = False
                self.mask_roi_extractor = nn.ModuleList()
                if not isinstance(mask_roi_extractor, list):
                    mask_roi_extractor = [
                        mask_roi_extractor for _ in range(num_stages)
                    ]
                assert len(mask_roi_extractor) == self.num_stages
                for roi_extractor in mask_roi_extractor:
                    self.mask_roi_extractor.append(
                        builder.build_roi_extractor(roi_extractor))
            else:
                self.share_roi_extractor = True
                self.mask_roi_extractor = self.bbox_roi_extractor

        self.train_cfg = train_cfg
        self.test_cfg = test_cfg

        self.init_weights(pretrained=pretrained)

        self.pair_train = pair_train
        self.normal_train = normal_train
        self.style = style
        if self.style == 'sub_feat':
            self.scale_a = Scale(0.5)
            self.scale_b = Scale(0.5)
        elif self.style == 'vector_add_feat':
            self.scale_a = Scale_channel(1, 256)
            self.scale_b = Scale_channel(1, 256)
    @property
    def with_rpn(self):
        return hasattr(self, 'rpn_head') and self.rpn_head is not None

    def init_weights(self, pretrained=None):
        super(CascadeRCNN_pair, self).init_weights(pretrained)
        self.backbone.init_weights(pretrained=pretrained)
        if self.with_neck:
            if isinstance(self.neck, nn.Sequential):
                for m in self.neck:
                    m.init_weights()
            else:
                self.neck.init_weights()
        if self.with_rpn:
            self.rpn_head.init_weights()
        if self.with_shared_head:
            self.shared_head.init_weights(pretrained=pretrained)
        for i in range(self.num_stages):
            if self.with_bbox:
                self.bbox_roi_extractor[i].init_weights()
                self.bbox_head[i].init_weights()
            if self.with_mask:
                if not self.share_roi_extractor:
                    self.mask_roi_extractor[i].init_weights()
                self.mask_head[i].init_weights()

    def extract_feat(self, img):
        x = self.backbone(img)
        if self.with_neck:
            x = self.neck(x)
        return x

    def extract_feat_pair(self, img):
        if self.pair_train is True and self.style is not None:

            b, c, h, w = img.shape
            img = img.reshape(-1, c // 2, h, w)
            if self.style == 'sub_img':
                img = img[0::2, :, :, :] - img[1::2, :, :, :]
                x = self.extract_feat(img)
            elif self.style == 'add_img':
                img = img[0::2, :, :, :] + img[1::2, :, :, :]
                x = self.extract_feat(img)
            elif self.style == 'sub_feat':
                x = self.extract_feat(img)
                x_ = []

                for i, lvl_feat in enumerate(x):
                    x_.append(self.scale_a(lvl_feat[0::2, :, :, :]) + self.scale_b(lvl_feat[1::2, :, :, :]))
                x = tuple(x_)
            elif self.style == 'add_feat':
                x = self.extract_feat(img)
                x_ = []
                for lvl_feat in x:
                    x_.append(lvl_feat[0::2, :, :, :] + lvl_feat[1::2, :, :, :])
                x = tuple(x_)
            elif self.style == 'vector_add_feat':
                x = self.extract_feat(img)
                x_ = []
                for i, lvl_feat in enumerate(x):
                    x_.append(self.scale_a(lvl_feat[0::2, :, :, :]) + self.scale_b(lvl_feat[1::2, :, :, :]))
                x = tuple(x_)
        else:
            x = self.extract_feat(img)

        return x

    def extract_feats_pair(self, imgs):
        assert isinstance(imgs, list)
        for img in imgs:
            yield self.extract_feat_pair(img)

    def extract_feat_pair_test(self, img, temp_feat=None):
        if self.pair_train is True and self.style is not None:
            if temp_feat is None:
                b, c, h, w = img.shape
                img = img.reshape(-1, c // 2, h, w)
                if self.style == 'sub_feat' or self.style == 'vector_add_feat':
                    x = self.extract_feat(img)
                    x_ = []
                    y = []
                    for i, lvl_feat in enumerate(x):
                        x_.append(self.scale_a(lvl_feat[0::2, :, :, :]) + self.scale_b(lvl_feat[1::2, :, :, :]))
                        y.append(self.scale_b(lvl_feat[1::2, :, :, :]))
                    x = tuple(x_)
                else:
                    raise ValueError('unvalid style')
                return x, y
            else:
                if self.style == 'sub_feat' or self.style == 'vector_add_feat':
                    x = self.extract_feat(img)
                    x_ = []
                    y_ = []
                    for i, (lvl_feat, lvl_temp_feat) in enumerate(zip(x, temp_feat)):
                        x_.append(self.scale_a(lvl_feat) + lvl_temp_feat)

                    x = tuple(x_)
                else:
                    raise ValueError('unvalid style')
                return x
        else:
            x = self.extract_feat(img)
            return x

    def extract_feats_pair_test(self, imgs, temp_feats=None):
        assert isinstance(imgs, list)
        x = []
        if temp_feats is None:
            y = []
            for img in imgs:
                x.append(self.extract_feat_pair_test(img, temp_feat=None)[0])
                y.append(self.extract_feat_pair_test(img, temp_feat=None)[1])
            return x, y
        else:
            for img, temp_feat in zip(imgs, temp_feats):
                x.append(self.extract_feat_pair_test(img, temp_feat=temp_feat))
            return x

    def forward_dummy(self, img):
        outs = ()
        # backbone
        x = self.extract_feat(img)
        # rpn
        if self.with_rpn:
            rpn_outs = self.rpn_head(x)
            outs = outs + (rpn_outs, )
        proposals = torch.randn(1000, 4).cuda()
        # bbox heads
        rois = bbox2roi([proposals])
        if self.with_bbox:
            for i in range(self.num_stages):
                bbox_feats = self.bbox_roi_extractor[i](
                    x[:self.bbox_roi_extractor[i].num_inputs], rois)
                if self.with_shared_head:
                    bbox_feats = self.shared_head(bbox_feats)
                cls_score, bbox_pred = self.bbox_head[i](bbox_feats)
                outs = outs + (cls_score, bbox_pred)
        # mask heads
        if self.with_mask:
            mask_rois = rois[:100]
            for i in range(self.num_stages):
                mask_feats = self.mask_roi_extractor[i](
                    x[:self.mask_roi_extractor[i].num_inputs], mask_rois)
                if self.with_shared_head:
                    mask_feats = self.shared_head(mask_feats)
                mask_pred = self.mask_head[i](mask_feats)
                outs = outs + (mask_pred, )
        return outs

    def forward_train_single(self,
                      img,
                      img_meta,
                      gt_bboxes,
                      gt_labels,
                      gt_bboxes_ignore=None,
                      gt_masks=None,
                      proposals=None):

        x = self.extract_feat_pair(img)
        losses = dict()

        if self.with_rpn:
            rpn_outs = self.rpn_head(x)
            rpn_loss_inputs = rpn_outs + (gt_bboxes, img_meta,
                                          self.train_cfg.rpn)
            rpn_losses = self.rpn_head.loss(
                *rpn_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore)
            losses.update(rpn_losses)

            proposal_cfg = self.train_cfg.get('rpn_proposal',
                                              self.test_cfg.rpn)
            proposal_inputs = rpn_outs + (img_meta, proposal_cfg)

            proposal_list = self.rpn_head.get_bboxes(*proposal_inputs)
        else:
            proposal_list = proposals

        for i in range(self.num_stages):
            self.current_stage = i
            rcnn_train_cfg = self.train_cfg.rcnn[i]
            lw = self.train_cfg.stage_loss_weights[i]

            # assign gts and sample proposals
            sampling_results = []
            if self.with_bbox or self.with_mask:
                bbox_assigner = build_assigner(rcnn_train_cfg.assigner)
                bbox_sampler = build_sampler(
                    rcnn_train_cfg.sampler, context=self)
                num_imgs = img.size(0)
                if gt_bboxes_ignore is None:
                    gt_bboxes_ignore = [None for _ in range(num_imgs)]

                for j in range(num_imgs):
                    assign_result = bbox_assigner.assign(
                        proposal_list[j], gt_bboxes[j], gt_bboxes_ignore[j],
                        gt_labels[j])
                    sampling_result = bbox_sampler.sample(
                        assign_result,
                        proposal_list[j],
                        gt_bboxes[j],
                        gt_labels[j],
                        feats=[lvl_feat[j][None] for lvl_feat in x])
                    sampling_results.append(sampling_result)

            # bbox head forward and loss
            bbox_roi_extractor = self.bbox_roi_extractor[i]
            bbox_head = self.bbox_head[i]

            rois = bbox2roi([res.bboxes for res in sampling_results])
            bbox_feats = bbox_roi_extractor(x[:bbox_roi_extractor.num_inputs],
                                            rois)
            if self.with_shared_head:
                bbox_feats = self.shared_head(bbox_feats)
            cls_score, bbox_pred = bbox_head(bbox_feats)

            bbox_targets = bbox_head.get_target(sampling_results, gt_bboxes,
                                                gt_labels, rcnn_train_cfg)
            loss_bbox = bbox_head.loss(cls_score, bbox_pred, *bbox_targets)
            for name, value in loss_bbox.items():
                losses['s{}.{}'.format(i, name)] = (
                    value * lw if 'loss' in name else value)

            # mask head forward and loss
            if self.with_mask:
                if not self.share_roi_extractor:
                    mask_roi_extractor = self.mask_roi_extractor[i]
                    pos_rois = bbox2roi(
                        [res.pos_bboxes for res in sampling_results])
                    mask_feats = mask_roi_extractor(
                        x[:mask_roi_extractor.num_inputs], pos_rois)
                    if self.with_shared_head:
                        mask_feats = self.shared_head(mask_feats)
                else:
                    # reuse positive bbox feats
                    pos_inds = []
                    device = bbox_feats.device
                    for res in sampling_results:
                        pos_inds.append(
                            torch.ones(
                                res.pos_bboxes.shape[0],
                                device=device,
                                dtype=torch.uint8))
                        pos_inds.append(
                            torch.zeros(
                                res.neg_bboxes.shape[0],
                                device=device,
                                dtype=torch.uint8))
                    pos_inds = torch.cat(pos_inds)
                    mask_feats = bbox_feats[pos_inds]
                mask_head = self.mask_head[i]
                mask_pred = mask_head(mask_feats)
                mask_targets = mask_head.get_target(sampling_results, gt_masks,
                                                    rcnn_train_cfg)
                pos_labels = torch.cat(
                    [res.pos_gt_labels for res in sampling_results])
                loss_mask = mask_head.loss(mask_pred, mask_targets, pos_labels)
                for name, value in loss_mask.items():
                    losses['s{}.{}'.format(i, name)] = (
                        value * lw if 'loss' in name else value)

            # refine bboxes
            if i < self.num_stages - 1:
                pos_is_gts = [res.pos_is_gt for res in sampling_results]
                roi_labels = bbox_targets[0]  # bbox_targets is a tuple
                with torch.no_grad():
                    proposal_list = bbox_head.refine_bboxes(
                        rois, roi_labels, bbox_pred, pos_is_gts, img_meta)
        return losses

    def forward_train_pair(self,
                      img,
                      img_meta,
                      gt_bboxes,
                      gt_labels,
                      gt_bboxes_ignore=None,
                      gt_masks=None,
                      proposals=None):
        # img: b, 2*c = 6, h, w
        b, c, h, w = img.shape
        img = img.reshape(-1, c // 2, h, w)
        # img: 2*b, c, h, w [0,1] , [2,3] , ......
        x = self.extract_feat(img)
        # x : tuple,5 layer
        losses = dict()

        if self.with_rpn:
            rpn_outs = self.rpn_head(x)
            rpn_outs_half = []
            for outs_0 in rpn_outs:
                tmp = []
                for outs_1 in outs_0:
                    tmp.append(outs_1[::2, :, :, :])
                rpn_outs_half.append(tmp)

            rpn_loss_inputs = tuple(rpn_outs_half) + (gt_bboxes, img_meta,
                                                      self.train_cfg.rpn)
            rpn_losses = self.rpn_head.loss(
                *rpn_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore)
            losses.update(rpn_losses)

            proposal_cfg = self.train_cfg.get('rpn_proposal',
                                              self.test_cfg.rpn)

            # copy train img_meta to pair. 1,2,3->1,1,2,2,3,3
            img_meta = [img_meta[i // 2] for i in range(2 * len(img_meta))]

            proposal_inputs = rpn_outs + (img_meta, proposal_cfg)
            proposal_list = self.rpn_head.get_bboxes(*proposal_inputs)
        else:
            proposal_list = proposals

        # generate blank gt for normal img
        gt_bboxes_ = []
        gt_labels_ = []
        for i in range(len(gt_bboxes)):
            gt_bboxes_.append(gt_bboxes[i])
            gt_bboxes_.append(torch.Tensor([[1, 1, 1, 1]]).to(gt_bboxes[i].device))
            gt_labels_.append(gt_labels[i])
            gt_labels_.append(torch.Tensor([[0]]).to(gt_labels[i].device))

        for i in range(self.num_stages):
            self.current_stage = i
            rcnn_train_cfg = self.train_cfg.rcnn[i]
            lw = self.train_cfg.stage_loss_weights[i]

            # assign gts and sample proposals
            sampling_results = []

            if self.with_bbox or self.with_mask:
                bbox_assigner = build_assigner(rcnn_train_cfg.assigner)
                bbox_sampler = build_sampler(
                    rcnn_train_cfg.sampler, context=self)
                assert img.size(0) % 2 == 0
                num_pairs = img.size(0) // 2
                num_imgs = img.size(0)

                if gt_bboxes_ignore is None:
                    gt_bboxes_ignore = [None for _ in range(num_imgs)]
                sampling_results = []

                for j in range(num_pairs):
                    i_train = 2 * j
                    i_normal = i_train + 1

                    assign_result_train = bbox_assigner.assign(proposal_list[i_train],
                                                               gt_bboxes_[i_train],
                                                               gt_bboxes_ignore[i_train],
                                                               gt_labels_[i_train])

                    assign_result_normal = bbox_assigner.assign(proposal_list[i_normal],
                                                                gt_bboxes_[i_normal],
                                                                gt_bboxes_ignore[i_normal],
                                                                gt_labels_[i_normal])

                    sampling_result_train, sampling_results_normal = bbox_sampler.pair_sample(
                        assign_result_train,
                        assign_result_normal,
                        proposal_list[i_train],
                        proposal_list[i_normal],
                        gt_bboxes_[i_train],
                        gt_labels_[i_train],
                        feats_train=[lvl_feat[i_train][None] for lvl_feat in x],
                        feats_normal=[lvl_feat[i_normal][None] for lvl_feat in x])
                    sampling_results.append(sampling_result_train)
                    sampling_results.append(sampling_results_normal)

            # bbox head forward and loss
            bbox_roi_extractor = self.bbox_roi_extractor[i]
            bbox_head = self.bbox_head[i]

            rois = bbox2roi([res.bboxes for res in sampling_results])
            bbox_feats = bbox_roi_extractor(x[:bbox_roi_extractor.num_inputs],
                                            rois)
            if self.with_shared_head:
                bbox_feats = self.shared_head(bbox_feats)
            cls_score, bbox_pred = bbox_head(bbox_feats)

            bbox_targets = bbox_head.get_target(sampling_results, gt_bboxes,
                                                gt_labels, rcnn_train_cfg)
            loss_bbox = bbox_head.loss(cls_score, bbox_pred, *bbox_targets)
            for name, value in loss_bbox.items():
                losses['s{}.{}'.format(i, name)] = (
                    value * lw if 'loss' in name else value)

            # mask head forward and loss
            if self.with_mask:
                if not self.share_roi_extractor:
                    mask_roi_extractor = self.mask_roi_extractor[i]
                    pos_rois = bbox2roi(
                        [res.pos_bboxes for res in sampling_results])
                    mask_feats = mask_roi_extractor(
                        x[:mask_roi_extractor.num_inputs], pos_rois)
                    if self.with_shared_head:
                        mask_feats = self.shared_head(mask_feats)
                else:
                    # reuse positive bbox feats
                    pos_inds = []
                    device = bbox_feats.device
                    for res in sampling_results:
                        pos_inds.append(
                            torch.ones(
                                res.pos_bboxes.shape[0],
                                device=device,
                                dtype=torch.uint8))
                        pos_inds.append(
                            torch.zeros(
                                res.neg_bboxes.shape[0],
                                device=device,
                                dtype=torch.uint8))
                    pos_inds = torch.cat(pos_inds)
                    mask_feats = bbox_feats[pos_inds]
                mask_head = self.mask_head[i]
                mask_pred = mask_head(mask_feats)
                mask_targets = mask_head.get_target(sampling_results, gt_masks,
                                                    rcnn_train_cfg)
                pos_labels = torch.cat(
                    [res.pos_gt_labels for res in sampling_results])
                loss_mask = mask_head.loss(mask_pred, mask_targets, pos_labels)
                for name, value in loss_mask.items():
                    losses['s{}.{}'.format(i, name)] = (
                        value * lw if 'loss' in name else value)

            # refine bboxes
            if i < self.num_stages - 1:
                pos_is_gts = [res.pos_is_gt for res in sampling_results]
                roi_labels = bbox_targets[0]  # bbox_targets is a tuple
                # print('stage:{}'.format(i))
                # ic(proposal_list[1])
                with torch.no_grad():
                    proposal_list = bbox_head.refine_bboxes(
                        rois, roi_labels, bbox_pred, pos_is_gts, img_meta)
                # ic(proposal_list[1])
        # exit(0)

        return losses

    def forward_train(self,
                      img,
                      img_meta,
                      gt_bboxes,
                      gt_labels,
                      gt_bboxes_ignore=None,
                      gt_masks=None,
                      proposals=None):
        if self.normal_train is False:
            return self.forward_train_single(img,
                                             img_meta,
                                             gt_bboxes,
                                             gt_labels,
                                             gt_bboxes_ignore=gt_bboxes_ignore,
                                             gt_masks=gt_masks,
                                             proposals=proposals)
        else:
            return self.forward_train_pair(img,
                                             img_meta,
                                             gt_bboxes,
                                             gt_labels,
                                             gt_bboxes_ignore=gt_bboxes_ignore,
                                             gt_masks=gt_masks,
                                             proposals=proposals)

    def simple_test(self, img, img_meta, proposals=None, rescale=False):
        x = self.extract_feat_pair(img)
        proposal_list = self.simple_test_rpn(
            x, img_meta, self.test_cfg.rpn) if proposals is None else proposals

        img_shape = img_meta[0]['img_shape']
        ori_shape = img_meta[0]['ori_shape']
        scale_factor = img_meta[0]['scale_factor']

        # "ms" in variable names means multi-stage
        ms_bbox_result = {}
        ms_segm_result = {}
        ms_scores = []
        rcnn_test_cfg = self.test_cfg.rcnn

        rois = bbox2roi(proposal_list)
        for i in range(self.num_stages):
            bbox_roi_extractor = self.bbox_roi_extractor[i]
            bbox_head = self.bbox_head[i]

            bbox_feats = bbox_roi_extractor(
                x[:len(bbox_roi_extractor.featmap_strides)], rois)
            if self.with_shared_head:
                bbox_feats = self.shared_head(bbox_feats)

            cls_score, bbox_pred = bbox_head(bbox_feats)
            ms_scores.append(cls_score)

            if self.test_cfg.keep_all_stages:
                det_bboxes, det_labels = bbox_head.get_det_bboxes(
                    rois,
                    cls_score,
                    bbox_pred,
                    img_shape,
                    scale_factor,
                    rescale=rescale,
                    cfg=rcnn_test_cfg)
                bbox_result = bbox2result(det_bboxes, det_labels,
                                          bbox_head.num_classes)
                ms_bbox_result['stage{}'.format(i)] = bbox_result

                if self.with_mask:
                    mask_roi_extractor = self.mask_roi_extractor[i]
                    mask_head = self.mask_head[i]
                    if det_bboxes.shape[0] == 0:
                        mask_classes = mask_head.num_classes - 1
                        segm_result = [[] for _ in range(mask_classes)]
                    else:
                        _bboxes = (
                            det_bboxes[:, :4] *
                            scale_factor if rescale else det_bboxes)
                        mask_rois = bbox2roi([_bboxes])
                        mask_feats = mask_roi_extractor(
                            x[:len(mask_roi_extractor.featmap_strides)],
                            mask_rois)
                        if self.with_shared_head:
                            mask_feats = self.shared_head(mask_feats, i)
                        mask_pred = mask_head(mask_feats)
                        segm_result = mask_head.get_seg_masks(
                            mask_pred, _bboxes, det_labels, rcnn_test_cfg,
                            ori_shape, scale_factor, rescale)
                    ms_segm_result['stage{}'.format(i)] = segm_result

            if i < self.num_stages - 1:
                bbox_label = cls_score.argmax(dim=1)
                rois = bbox_head.regress_by_class(rois, bbox_label, bbox_pred,
                                                  img_meta[0])

        cls_score = sum(ms_scores) / self.num_stages
        det_bboxes, det_labels = self.bbox_head[-1].get_det_bboxes(
            rois,
            cls_score,
            bbox_pred,
            img_shape,
            scale_factor,
            rescale=rescale,
            cfg=rcnn_test_cfg)
        bbox_result = bbox2result(det_bboxes, det_labels,
                                  self.bbox_head[-1].num_classes)
        ms_bbox_result['ensemble'] = bbox_result

        if self.with_mask:
            if det_bboxes.shape[0] == 0:
                mask_classes = self.mask_head[-1].num_classes - 1
                segm_result = [[] for _ in range(mask_classes)]
            else:
                if isinstance(scale_factor, float):  # aspect ratio fixed
                    _bboxes = (
                        det_bboxes[:, :4] *
                        scale_factor if rescale else det_bboxes)
                else:
                    _bboxes = (
                        det_bboxes[:, :4] *
                        torch.from_numpy(scale_factor).to(det_bboxes.device)
                        if rescale else det_bboxes)

                mask_rois = bbox2roi([_bboxes])
                aug_masks = []
                for i in range(self.num_stages):
                    mask_roi_extractor = self.mask_roi_extractor[i]
                    mask_feats = mask_roi_extractor(
                        x[:len(mask_roi_extractor.featmap_strides)], mask_rois)
                    if self.with_shared_head:
                        mask_feats = self.shared_head(mask_feats)
                    mask_pred = self.mask_head[i](mask_feats)
                    aug_masks.append(mask_pred.sigmoid().cpu().numpy())
                merged_masks = merge_aug_masks(aug_masks,
                                               [img_meta] * self.num_stages,
                                               self.test_cfg.rcnn)
                segm_result = self.mask_head[-1].get_seg_masks(
                    merged_masks, _bboxes, det_labels, rcnn_test_cfg,
                    ori_shape, scale_factor, rescale)
            ms_segm_result['ensemble'] = segm_result

        if not self.test_cfg.keep_all_stages:
            if self.with_mask:
                results = (ms_bbox_result['ensemble'],
                           ms_segm_result['ensemble'])
            else:
                results = ms_bbox_result['ensemble']
        else:
            if self.with_mask:
                results = {
                    stage: (ms_bbox_result[stage], ms_segm_result[stage])
                    for stage in ms_bbox_result
                }
            else:
                results = ms_bbox_result

        return results

    # def aug_test(self, img, img_meta, proposals=None, rescale=False):
    #     raise NotImplementedError

    def aug_test_(self, imgs, img_metas, proposals=None, rescale=False):
        """Test with augmentations.

        If rescale is False, then returned bboxes and masks will fit the scale
        of imgs[0].
        """
        # recompute feats to save memory

        proposal_list = self.aug_test_rpn(
            self.extract_feats_pair(imgs), img_metas, self.test_cfg.rpn)
        det_bboxes, det_labels = self.multi_bboxes_test(self.extract_feats_pair(imgs), img_metas, proposal_list,
                                                  self.test_cfg.rcnn, rescale)
        bbox_results = bbox2result(det_bboxes, det_labels,
                                   self.bbox_head[-1].num_classes)

        # proposal_list = self.aug_test_rpn(
        #     self.extract_feats(imgs), img_metas, self.test_cfg.rpn)
        # det_bboxes, det_labels = self.multi_bboxes_test(self.extract_feats(imgs), img_metas, proposal_list,
        #                                           self.test_cfg.rcnn, rescale)
        # bbox_results = bbox2result(det_bboxes, det_labels,
        #                            self.bbox_head[-1].num_classes)

        return bbox_results

    def aug_test__(self, imgs, img_metas, proposals=None, rescale=False, temp_feats=None):
        """Test with augmentations.

        If rescale is False, then returned bboxes and masks will fit the scale
        of imgs[0].
        """
        # save feats to speed up
        if temp_feats is None:
            x, temp_feats = self.extract_feats_pair_test(imgs)
            proposal_list = self.aug_test_rpn(
                x, img_metas, self.test_cfg.rpn)

            det_bboxes, det_labels = self.multi_bboxes_test(x, img_metas, proposal_list,
                                                      self.test_cfg.rcnn, rescale)
            bbox_results = bbox2result(det_bboxes, det_labels,
                                       self.bbox_head[-1].num_classes)
            return bbox_results, temp_feats

        else:
            x = self.extract_feats_pair_test(imgs, temp_feats)
            proposal_list = self.aug_test_rpn(
                x, img_metas, self.test_cfg.rpn)

            det_bboxes, det_labels = self.multi_bboxes_test(x, img_metas, proposal_list,
                                                            self.test_cfg.rcnn, rescale)
            bbox_results = bbox2result(det_bboxes, det_labels,
                                       self.bbox_head[-1].num_classes)
            return bbox_results, temp_feats

    def aug_test(self, imgs, img_metas, is_new, proposals=None, rescale=False, temp_feats=None):
        """Test with augmentations.

        If rescale is False, then returned bboxes and masks will fit the scale
        of imgs[0].
        """
        # save feats to speed up
        if is_new[0] == 1:
            x, temp_feats = self.extract_feats_pair_test(imgs)
            proposal_list = self.aug_test_rpn(
                x, img_metas, self.test_cfg.rpn)

            det_bboxes, det_labels = self.multi_bboxes_test(x, img_metas, proposal_list,
                                                      self.test_cfg.rcnn, rescale)
            bbox_results = bbox2result(det_bboxes, det_labels,
                                       self.bbox_head[-1].num_classes)
            return bbox_results, temp_feats

        else:
            x = self.extract_feats_pair_test(imgs, temp_feats)
            proposal_list = self.aug_test_rpn(
                x, img_metas, self.test_cfg.rpn)

            det_bboxes, det_labels = self.multi_bboxes_test(x, img_metas, proposal_list,
                                                            self.test_cfg.rcnn, rescale)
            bbox_results = bbox2result(det_bboxes, det_labels,
                                       self.bbox_head[-1].num_classes)
            return bbox_results, temp_feats

        # return bbox_results

    def show_result(self, data, result, **kwargs):
        if self.with_mask:
            ms_bbox_result, ms_segm_result = result
            if isinstance(ms_bbox_result, dict):
                result = (ms_bbox_result['ensemble'],
                          ms_segm_result['ensemble'])
        else:
            if isinstance(result, dict):
                result = result['ensemble']
        super(CascadeRCNN_pair, self).show_result(data, result, **kwargs)


# runs currency with 3 menus of option

def currencies_to_dollars(pesos, dollar_price):
    local_currency = int(input("How many $ " + pesos + " do you have: "))
    local_currency = float(local_currency)
    dollars = str(round(local_currency/dollar_price, 2))
    print("You have $" + dollars + " dollars")
    print("Thanks ðŸ˜€ ")

def currencies_to_dollars_menu():
    menu_currencies = int(input("""
    Mexican pesos       (1)
    Colombian pesos     (2)
    Argentine pesos     (3)

    Please! Choose an option: """))

    if menu_currencies == 1:
        currencies_to_dollars("mexican pesos", 21.5)
    elif menu_currencies == 2:
        currencies_to_dollars("colombian pesos", 4500.1)
    elif menu_currencies == 3:
        currencies_to_dollars("argentine pesos", 102)
    else:
        print("Please! Select a correct option")

def dollars_to_currencies(dollar_price,pesos):
    dollars_amount = int(input("How many dollars ðŸ’° do you have?: "))
    dollars_amount = float(dollars_amount)
    dollars_to_local_currency = str(round(dollars_amount * dollar_price,2))
    print("You have: $" + dollars_to_local_currency +  " " + pesos)
    print("Thanks ðŸ˜€ ")

def dollars_to_currencies_menu():
    menu_dollars = int(input("""
    Dollars to mexican pesos       (1)
    Dollars to colombian pesos     (2)
    Dollars to argentine pesos     (3)

    Please! Choose an option: """))

    if menu_dollars == 1:
        dollars_to_currencies(21.5,"mexican pesos")
    elif menu_dollars == 2:
        dollars_to_currencies(4500,"colombian pesos")
    elif menu_dollars == 3:
        dollars_to_currencies(102,"argentine pesos")
    else:
        print("Please! Select a correct option")

def main_mennu():
    main_menu = int(input("""
    Dollars to other currency   (1)
    Currencies to dollars        (2)

    Please! Choose one option: """))

    if main_menu == 1:
        print(dollars_to_currencies_menu())
    elif main_menu == 2:
        print(currencies_to_dollars_menu())
    else:
        print("Please! Select a correct option")

if __name__ == "__main__":
    main_mennu()

# pesos = int(input("How many colombian pesos ðŸ’° do you have?: "))
# pesos = float(pesos)
# dollar_price = 4011.65
# dollars = round(pesos/dollar_price,2)
# dollars = str(dollars)

# print("You have $" + dollars + " dollars")
# print("Thanks ðŸ˜€ ")

# ## convert from mexican pesos to dollars

# pesos = int(input("How many mexican pesos ðŸ’° do you have?: "))
# pesos = float(pesos)
# dollar_price = 21.4
# dollars = round(pesos/dollar_price,2)
# dollars = str(dollars)

# print("You have $" + dollars + " dollars")
# print("Thanks ðŸ˜€ ")

# ## convert from dollars to mexican pesos

# dollars = int(input("How many dollars ðŸ’° do you have?: "))
# dollars = float(dollars)
# dollar_price = 21.4
# pesos = round(dollars * dollar_price,2)
# pesos = str(pesos)

# print("You have $" + pesos + " mexican pesos")
# print("Thanks ðŸ˜€ ")

# 1 crear menu de eleccion
# 2 crear las condiciones repetitivas
# 3 parametros y argumentos


# -*- coding: utf-8 -*-
"""
This is an implementation of the Kalman filter equations that are derived and explained here

https://aleksandarhaber.com/kalman-filter-complete-derivation-from-scratch/

Author: Aleksandar Haber
Last Revision: November 19, 2022

"""
class KalmanFilter(object):

    # x0 - initial guess of the state vector
    # P0 - initial guess of the covariance matrix of the state estimation error
    # A,B,C - system matrices describing the system model
    # Q - covariance matrix of the process noise
    # R - covariance matrix of the measurement noise

    def __init__(self,x0,P0,A,B,C,Q,R):

        # initialize vectors and matrices
        self.x0=x0
        self.P0=P0
        self.A=A
        self.B=B
        self.C=C
        self.Q=Q
        self.R=R

        # this variable is used to track the current time step k of the estimator
        # after every measurement arrives, this variables is incremented for +1
        self.currentTimeStep=0

        # this list is used to store the a posteriori estimates xk^{+} starting from the initial estimate
        # note: list starts from x0^{+}=x0 - where x0 is an initial guess of the estimate
        self.estimates_aposteriori=[]
        self.estimates_aposteriori.append(x0)

        # this list is used to store the a apriori estimates xk^{-} starting from x1^{-}
        # note: x0^{-} does not exist, that is, the list starts from x1^{-}
        self.estimates_apriori=[]

        # this list is used to store the a posteriori estimation error covariance matrices Pk^{+}
        # note: list starts from P0^{+}=P0, where P0 is the initial guess of the covariance
        self.estimationErrorCovarianceMatricesAposteriori=[]
        self.estimationErrorCovarianceMatricesAposteriori.append(P0)

        # this list is used to store the a priori estimation error covariance matrices Pk^{-}
        # note: list starts from P1^{-}, that is, P0^{-} does not exist
        self.estimationErrorCovarianceMatricesApriori=[]

        # this list is used to store the gain matrices Kk
        self.gainMatrices=[]

        # this list is used to store prediction errors error_k=y_k-C*xk^{-}
        self.errors=[]

    # this function propagates x_{k-1}^{+} through the model to compute x_{k}^{-}
    # this function also propagates P_{k-1}^{+} through the covariance model to compute P_{k}^{-}
    # at the end this function increments the time index currentTimeStep for +1
    def propagateDynamics(self,inputValue):

        xk_minus=self.A*self.estimates_aposteriori[self.currentTimeStep]+self.B*inputValue
        Pk_minus=self.A*self.estimationErrorCovarianceMatricesAposteriori[self.currentTimeStep]*(self.A.T)+self.Q

        self.estimates_apriori.append(xk_minus)
        self.estimationErrorCovarianceMatricesApriori.append(Pk_minus)

        self.currentTimeStep=self.currentTimeStep+1

    # this function should be called after propagateDynamics() because the time step should be increased and states and covariances should be propagated
    def computeAposterioriEstimate(self,currentMeasurement):
        import numpy as np
        # gain matrix
        Kk=self.estimationErrorCovarianceMatricesApriori[self.currentTimeStep-1]*(self.C.T)*np.linalg.inv(self.R+self.C*self.estimationErrorCovarianceMatricesApriori[self.currentTimeStep-1]*(self.C.T))

        # prediction error
        error_k=currentMeasurement-self.C*self.estimates_apriori[self.currentTimeStep-1]
        # a posteriori estimate
        xk_plus=self.estimates_apriori[self.currentTimeStep-1]+Kk*error_k

        # a posteriori matrix update
        IminusKkC=np.matrix(np.eye(self.x0.shape[0]))-Kk*self.C
        Pk_plus=IminusKkC*self.estimationErrorCovarianceMatricesApriori[self.currentTimeStep-1]*(IminusKkC.T)+Kk*(self.R)*(Kk.T)

        # update the lists that store the vectors and matrices
        self.gainMatrices.append(Kk)
        self.errors.append(error_k)
        self.estimates_aposteriori.append(xk_plus)
        self.estimationErrorCovarianceMatricesAposteriori.append(Pk_plus)


import random
digit=5
name=input("enter your name** ")
print("Hello***",name,"***WELCOME TO COWS AND BULLS GAME")
print("********************************************")

def getSecretNum():
    number=list(range(10))
    random.shuffle(number)
    return number
def getclues():
    numbers=getSecretNum()
    secret_num=[]
    for i in range(digit):
        secret_num.append(numbers[i])
    return secret_num
def check_guess():
    bull=[]
    cow=[]
    num=getclues()
    print(num)
    user_can_guess=10
    while user_can_guess>0:
        guess=int(input("enter your guess** "))
        position=int(input("enter the position of your number** "))
        print("----------------------------")
        if guess not in num:
            print("This number is not in the list")
        print("----------------------------")
        if  guess in num:
            index = num.index(guess)
            if index== position:
                if guess not in bull:
                    bull.insert(index,guess)
                    print("Bull ",bull)
                    print("----------------------------")
                else:
                    print("You Already Used This digit Try  any Different digit")
                    print("----------------------------")
            else:
                if guess not in cow:
                    cow.append(guess)
                    print("This number is in list you can reuse it")
                    print("----------------------------")
                    print("Cow ",cow)
                    print("----------------------------")
                else:
                    print("You Already Used This digit Try  any Different digit")
                    print("----------------------------")
        if bull==num:
            print(name,"Congractulations you win the game ")
            print("----------------------------")
            break
        user_can_guess-=1
        print(user_can_guess,"Turns are left")
    else:
        print("You ran out your tries, You Loose The Game")
        print("----------------------------")
    return bull
check_guess()
def play_again():
    while True:
        again=input("If you Want to play again press y for yes or n of No*** ")
        if again=="y":
            check_guess()
        else:
            break
play_again()


import cv2
import time
from google.auth.credentials import Credentials
import io
import os

# Imports the Google Cloud client library
from google.cloud import vision

import json
from google.oauth2 import service_account

def visionMain(pipe):
    keyfile_dict = json.loads(open('baraboo.json').read())
    credentials = service_account.Credentials.from_service_account_info(keyfile_dict)

    # Instantiates a client
    client = vision.ImageAnnotatorClient(credentials=credentials)

    alpha = 1.2  # Contrast control (1 - 3)
    beta = 10  # Brightness control (0 - 100)
    save = True # display the camera view

    camera = cv2.VideoCapture(0)
    if not camera.isOpened():
        print("Vision: Error opening video capture device")
        quit()
    count = 0

    while True:
        ret, frame = camera.read()
        frame = cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
        cv2.imshow("Camera View", frame)
        key = cv2.waitKey(1)
        if pipe.poll():
            cmd = pipe.recv()
        else:
            cmd = None

        if cmd == "Capture":  # "return" takes a snapshot
            print(f"Vision: Taking snapshot")
            if save:
                filename = f"snapshot_{count}.jpg"
                cv2.imwrite(filename, frame) # save the snapshot to file
            count += 1 # keep count of snapshots
            time.sleep(1) # pause the camera view for 1 second

            # Loads the image into memory
            with io.open(filename, 'rb') as image_file:
                content = image_file.read()

            image = vision.Image(content=content)

            # Performs label detection on the image file
            response = client.object_localization(image=image)
            labels = response.localized_object_annotations

            pipe.send([label.name for label in labels])

        elif cmd == "Exit":
            print("Vision: Exiting vision process")
            return

    camera.release()
    cv2.destroyAllWindows()


import numpy as np
from fuzzywuzzy import fuzz
from sklearn.ensemble import IsolationForest as isolate

def duplicates(vector, data_type, fuzzy=False, fuzz_ratio_threshold=70):
    error = np.zeros_like(vector)

    if data_type.data_type != data_type.STRING or not data_type.is_consistent:
        return error

    for i, x in enumerate(vector):
        for y in vector[i + 1:]:
            error[i] = fuzz.ratio(x, y) > fuzz_ratio_threshold if fuzzy else x == y

    return error

def wrong_types(vector, data_type):
    numbers = np.zeros_like(vector)
    strings = np.zeros_like(vector)

    for i, item in enumerate(vector):
        try:
            float(item)
            numbers[i] = True
        except ValueError:
            strings[i] = True

    is_numbers = numbers.sum() > strings.sum()
    return strings if is_numbers else numbers

def outliers(vector, data_type):
    error = np.zeros_like(vector)

    if data_type.data_type != data_type.NUMBER or not data_type.is_consistent:
        return error

    vector = vector.reshape(-1, 1)
    forest = isolate()

    forest.fit(vector)
    forest_outliers = forest.predict(vector)
    error = np.asarray(forest_outliers == -1)

    return error