# -*- coding: utf-8 -*-
# @Author: Carter.Blum, Suhail.Alnahari
# @Date:   2020-12-05 16:42:44
# @Last Modified by:   Suhail.Alnahari
# @Last Modified time: 2020-12-06 15:36:25

from enum import Enum, unique, auto
from dataclasses import dataclass
from typing import Optional, Union, Tuple, List, Callable, Dict, Iterable, Generic
import numpy as np

from VIAYN.project_types import Agent, VoteBoundGetter, A, S
from VIAYN.samples.agents import (
    VotingMechanism,
    LookupBasedVotingMechanism,
    LookupBasedBetSelectionMech,
    LookupBasedPredSelectionMech,
    BetSelectionMechanism,
    PredictionSelectionMechanism,
    VotingMechanism,
    CompositeBettingMechanism,
    CompositeAgent,
    StaticVotingMechanism,
    StaticBetSelectionMech,
    StaticPredSelectionMech,
    RNGUniforPredSelectionMech,
    MorphicAgent
)
from VIAYN.utils import is_numeric, repeat_if_float

@unique
class AgentsEnum(Enum):
    # constant agent returns the same votes, bets and predictions that are specified
    constant = auto()
    # random agent returns random predictions that are specified with length N
    random = auto()
    composite = auto()
    # agent with voting, bet selection and prediction selection specified separately

@dataclass(frozen=True)
class AgentFactorySpec(Generic[A, S]):
    """
    Typed Spec to input to [AgentFactory.create] to create an [Agent]

    Note: Typed spec was used instead of Dict for the factory to provide
        more information to users.

    Parameters
    ----------
    agentType: AgentsEnum
        which agent to create possible types are (constant/random) currently
    vote: float
        what value should the agent's happiness be. Agent's happiness is constant
        for now
    totalVotesBound: Optional[Tuple[VoteBoundGetter, VoteBoundGetter]] = None
        What predictions values can be predicted by the agent (min, max)
    seed: Optional[int]
        Random seed used by random processes of agents
    prediction: Optional[Union[float, List[float]]] = None
        what agents predict the total happiness of all agents at each timestep.
        If prediction is a List of floats, you would be predicting the happiness
        for later in the future.
        e.g. [5.5,10.7,30.2] at time-step 4 corresponds to the total happiness of the
        system is 10.7 for all agents.
        money wil be bet at time-step 5.
    bet: Optional[Union[float, List[float]]] = None
        what percent of the money an agent predicts at each timestep.
        If bet is a List of floats, you would be casting a bet for later in the
        future. e.g. [0,0.5,0.2] at time-step 4 corresponds to 0.5 of an agents
        money wil be bet at time-step 5.
    N: Optional[int] = None
        length of bets as specified by requirements
    """
    agentType: AgentsEnum
    vote: float
    totalVotesBound: Optional[Tuple[VoteBoundGetter, VoteBoundGetter]] = None
    seed: Optional[int] = None
    prediction: Optional[Union[float, List[float]]] = None
    bet: Optional[Union[float, List[float]]] = None
    N: Optional[int] = None
    vote_lookup: Optional[Dict[S, Union[VotingMechanism[S], float]]] = None
    bet_lookup: Optional[Dict[
        Tuple[Optional[S], Optional[A], Optional[float]],
        Union[BetSelectionMechanism[A, S], List[float], float]]] = None
    prediction_lookup: Optional[Dict[
        Tuple[Optional[S], Optional[A], Optional[float]],
        Union[PredictionSelectionMechanism[A, S], List[float], float]]] = None

    def __post_init__(self):
        # constant agent uses these params in addition to vote at least
        if self.agentType == AgentsEnum.constant:
            assert(self.bet is not None)
            assert(self.prediction is not None)
        # random agent uses these params in addition to vote at least
        elif self.agentType == AgentsEnum.random:
            assert(self.N > 0)
            assert(self.totalVotesBound is not None)
            assert(self.bet is not None)
        elif self.agentType == AgentsEnum.composite:
            assert self.vote_lookup is not None
            assert self.bet_lookup is not None
            assert self.prediction_lookup is not None
            # there should probably be some more checks that we do here
            assert self.N > 0
        else:
            raise TypeError(self.agentType)

class AgentFactory:
    """
    Creates different types of Agents Based on spec

    Parameters
    ----------
    spec: AgentFactorySpec
        Specifications to create agent with, see above

    Returns
    -------
    Agent
        created agent based on spec
    """
    @staticmethod
    def create(spec: AgentFactorySpec) -> Agent:
        assert spec.agentType in AgentFactory._creators_
        return AgentFactory._creators_[spec.agentType](spec)

    @staticmethod
    def _create_static_agent_(
            spec: AgentFactorySpec) -> Agent:
        return CompositeAgent(
            voting_mechanism=AgentFactory._create_static_vote_selection_(spec),
            betting_mechanism=CompositeBettingMechanism(
                prediction_selection=AgentFactory._create_static_prediction_selection_(spec),
                bet_selection=AgentFactory._create_static_bet_selection_(spec)))

    @staticmethod
    def _create_static_bet_selection_(
            spec: AgentFactorySpec) -> BetSelectionMechanism:
        assert spec.bet is not None
        return StaticBetSelectionMech(repeat_if_float(spec.bet, spec.N))

    @staticmethod
    def _create_static_vote_selection_(spec: AgentFactorySpec) -> VotingMechanism:
        return StaticVotingMechanism(spec.vote)

    @staticmethod
    def _create_static_prediction_selection_(spec: AgentFactorySpec) -> PredictionSelectionMechanism:
        assert spec.prediction is not None
        return StaticPredSelectionMech(repeat_if_float(spec.prediction, spec.N))

    @staticmethod
    def _create_rng_uniform_prediction_selection_(spec: AgentFactorySpec) -> PredictionSelectionMechanism:
        assert spec.totalVotesBound is not None
        assert spec.seed is not None
        assert spec.N is not None
        return RNGUniforPredSelectionMech(
            tsteps_per_prediction=spec.N,
            min_possible_prediction=spec.totalVotesBound[0],
            max_possible_prediction=spec.totalVotesBound[1],
            random_seed=spec.seed)

    @staticmethod
    def _create_random_agent_(
            spec: AgentFactorySpec) -> Agent:
        return CompositeAgent(
            voting_mechanism=AgentFactory._create_static_vote_selection_(spec),
            betting_mechanism=CompositeBettingMechanism(
                prediction_selection=AgentFactory._create_rng_uniform_prediction_selection_(spec),
                bet_selection=AgentFactory._create_static_bet_selection_(spec)))

    @staticmethod
    def _create_lookup_vote_selection_(spec: AgentFactorySpec) -> VotingMechanism:
        assert spec.vote_lookup is not None
        return LookupBasedVotingMechanism(spec.vote_lookup)

    @staticmethod
    def _create_lookup_pred_selection_(spec: AgentFactorySpec) -> PredictionSelectionMechanism:
        assert spec.prediction_lookup is not None
        return LookupBasedPredSelectionMech({
            key: value if not is_numeric(value)
            else repeat_if_float(value, spec.N, normalize=False)
            for key, value in spec.prediction_lookup.items()
        })

    @staticmethod
    def _create_lookup_bet_selection_(spec: AgentFactorySpec) -> BetSelectionMechanism:
        assert spec.bet_lookup is not None
        return LookupBasedBetSelectionMech({
            key: value if not is_numeric(value)
            else repeat_if_float(value, spec.N, normalize=True)
            for key, value in spec.bet_lookup.items()
        })

    @staticmethod
    def _create_composite_agent_(
            spec: AgentFactorySpec) -> Agent:
        return CompositeAgent(
            voting_mechanism=AgentFactory._create_lookup_vote_selection_(spec),
            betting_mechanism=CompositeBettingMechanism(
                prediction_selection=AgentFactory._create_lookup_pred_selection_(spec),
                bet_selection=AgentFactory._create_lookup_bet_selection_(spec)
            )
        )

    @staticmethod
    def sequentialize(agents: List[Agent[A,S]], switch_at: List[int]) -> Agent[A,S]:
        """
        Method that creates an agent Manager that acts as an agent in [agents] for a certain
        number of time-steps

        Parameters
        ----------
        agents: List[Agent[A,S]]
            agents of the same type that will act in the order given in the list
            for [switch_at] time-steps each
        switch_at: List[int]
            each element in the list corresponds to the amount of time-steps
            the agent at the corresponding position in [agents] will be acting for

        Returns
        -------
        Agent
            agent manager that plugs agents in and out sequentially based on
            the amount of time specified in [switch_at]
        """
        # assert len(np.unique(switch_at)) == len(switch_at)
        return MorphicAgent(agents,switch_at)

    _creators_: Dict[AgentsEnum, Callable[[AgentFactorySpec], Agent]] = {
        AgentsEnum.random: lambda x: AgentFactory._create_random_agent_(x),
        AgentsEnum.constant: lambda x: AgentFactory._create_static_agent_(x),
        AgentsEnum.composite: lambda x: AgentFactory._create_composite_agent_(x)
    }


from modules.utils import *
from modules.geometry import *
from modules.gui import *

shapes_lst = ["lungslidingpresent", "lungslidingabsent", "aline", "bline"]

if __name__ == "__main__":
    ctk.set_appearance_mode("System")  # Modes: system (default), light, dark
    # Themes: blue (default), dark-blue, green
    ctk.set_default_color_theme("dark-blue")
    root = ctk.CTk()
    window = MainGUI(root)
    root.mainloop()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 双指针的解法
# 执行用时：48 ms, 在所有 Python3 提交中击败了76.44%的用户
# 内存消耗：14.7 MB, 在所有 Python3 提交中击败了54.00%的用户
# class Solution:
#     def removeDuplicates(self, nums) -> int:
#         slow = 0
#         fast = 0
#         if not nums:
#             return 0
#         while fast<len(nums)-1:
#             fast +=1
#             if nums[slow] != nums[fast]:
#                 slow +=1
#                 nums[slow]=nums[fast]
#                 continue
#         for _ in range(fast-slow):
#             nums.pop()
#
#         print(nums)
#         return slow+1

#比较优雅一点,for直接代替了fast指针, 但是缺乏改数组的操作
class Solution:
    def removeDuplicates(self, nums) -> int:
        if not nums:
            return 0
        i = 0
        for j in range(1, len(nums)):
            if nums[i] != nums[j]:
                i += 1
                nums[i] = nums[j]
        print(nums)
        return i + 1

# 二刷
class Solution:
    def removeDuplicates(self, nums) -> int:
        fast = 0
        slow = 0
        nums.sort()
        while fast<len(nums)-1:
            fast+=1
            if nums[fast]!=nums[slow]:
                nums[slow+1] = nums[fast]
                slow+=1
        # return len(nums[:slow+1])
        if not nums:return 0
        return slow+1
a=Solution().removeDuplicates([])
print(a)

26. 删除排序数组中的重复项.py


import math
import boto3
import random
from typing import List
from datetime import datetime, timedelta
from elasticsearch import Elasticsearch, RequestsHttpConnection, helpers
from elasticsearch_dsl import Search, A
from requests_aws4auth import AWS4Auth
from decimal import Decimal
from ..mpc.product_types import ProductType
from ..mpc.product_visit_logs import ProductVisitLog
from ....settings import settings
from .orders import OrderAggregation
from ..mpc.Cms.UserQuestions import UserQuestionEntity as Question
from .product_entry import ProductEntry

class Product(object):
    ES_HOST = settings.AWS_ELASTICSEARCH_HOST
    ES_PORT = settings.AWS_ELASTICSEARCH_PORT
    ES_REGION = settings.AWS_ELASTICSEARCH_PRODUCTS_REGION
    INDEX_NAME = settings.AWS_ELASTICSEARCH_PRODUCTS
    DOC_TYPE = settings.AWS_ELASTICSEARCH_PRODUCTS

    def __init__(self, **kwargs):
        service = 'es'
        credentials = boto3.Session(region_name=self.ES_REGION).get_credentials()
        awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, self.ES_REGION, service)

        self.__es = Elasticsearch(
            hosts=[{'host': self.ES_HOST, 'port': self.ES_PORT}],
            # http_auth = awsauth,
            use_ssl=True,
            verify_certs=True,
            connection_class=RequestsHttpConnection
        )

    @property
    def elasticsearch(self):
        return self.__es

    def create_index(self, index, body, recreate=False, **kwargs):
        if not settings.DEBUG:
            raise Exception("Not permitted in production mode.")

        es = self.elasticsearch
        if recreate:  # Delete if recreate required.
            try:
                from warnings import warn
                warn("Are you sure that you want to remove this index?\n"\
                    "Double Check and check the next line and enable!")
                # res = es.indices.delete(index)
                # print(res)
            except Exception as e:
                print(str(e))
        return es.indices.create(index, body)

    def bulk_insert(self, index, doc_type, items, recreate=False, random_date=False, **kwargs):
        es = self.elasticsearch
        if recreate:  # Delete if recreate required.
            res = es.indices.delete(index)
        actions = []
        for item in items:
            if random_date:
                MAX_DAY = 10
                new_datetime = (datetime.now() - timedelta(days=random.randint(0, MAX_DAY))).strftime("%Y-%m-%d %H:%M:%S")
                item.update({
                    "created_at": new_datetime,
                    "updated_at": new_datetime
                })
            item.update({
                'brand_code': item.get('manufacturer', '').lower()
            })
            actions.append({
                '_index': index,
                '_type': doc_type,
                '_id': item['rs_sku'],
                '_source': item
            })
        return helpers.bulk(es, actions)

    def insert(self, index, doc_type, item, **kwargs):
        es = self.elasticsearch
        item.update({
            'brand_code': item.get('manufacturer', '').lower()
        })
        return helpers.create(es, index, item.get('rs_sku'), item, doc_type)

    def convert_item(self, item, tier: dict=None) -> dict:
        from_date = datetime.now() - timedelta(days=settings.NEW_PRODUCT_THRESHOLD)

        # @todo : refactoring
        original_price = float(item['rs_selling_price']) if item['rs_selling_price'] else 0
        discount = float(item['discount']) if item['discount'] else 0
        current_price = original_price - original_price * discount / 100

        item = {
            'id': item['portal_config_id'],
            'sku': item['rs_sku'],
            'title': item['product_name'],
            'subtitle': item['product_description'],
            'price': Decimal(0 if item['rs_selling_price'] is None else item['rs_selling_price']),
            'badge': 'NEW IN' if datetime.strptime(item['created_at'], "%Y-%m-%d %H:%M:%S") > from_date else None,
            'favorite': random.choice([True, False]),
            'product_type': item['product_size_attribute'],
            'product_sub_type': item['rs_product_sub_type'],
            'gender': item['gender'],
            'brand': item['manufacturer'],
            'sizes': [{
                'size': size['size'],
                'qty': size['qty'],
                'rs_simple_sku': size['rs_simple_sku']
            } for size in item['sizes']],
            'image': {
                'src': item['images'][0]['s3_filepath'] if len(item['images']) > 0 else 'https://placeimg.com/155/140/arch',
                'title': item['product_size_attribute'],
            },
            'original_price': original_price,
            'current_price': current_price,
        }

        if tier is not None and type(tier) == dict:
            item['fbucks'] = math.ceil(float(item['price']) * float(tier.get('discount_rate')) / 100)
        return item

    def get(self, id, **kwargs):
        return self.elasticsearch.get(self.INDEX_NAME, id, doc_type=self.DOC_TYPE)['_source']

    def convert(
            self, products, personalize=False, customer_id='BLANK',
            tier: dict=None, **kwargs) -> List[dict]:
        return [self.convert_item(item, tier=tier) for item in products]

    def get_bestsellers(self, product_type=None, gender=None, page=1, size=20, **kwargs):
        offset = (page - 1) * size
        end_date = (datetime.now() - timedelta(
            days=settings.LAST_CHANCE_END_DATE_THRESHOLD)).strftime("%Y-%m-%d %H:%M:%S")

        if product_type is None:
            query = {
                "bool": {
                    "should": [
                        {"range": {"created_at": {"lt": end_date}}},
                        {"range": {"sizes.qty": {"lte": settings.LAST_CHANCE_STOCK_THRESHOLD, "gt": 0}}}
                    ]
                }
            }
        else:
            query = {
                "bool": {
                    "should": [
                        {"range": {"created_at": {"lt": end_date}}},
                        {"range": {"sizes.qty": {"lte": settings.LAST_CHANCE_STOCK_THRESHOLD, "gt": 0}}}
                    ],
                    "must": [
                        {"term": {"product_size_attribute": product_type}}
                    ]
                }
            }

        if gender is not None and gender.lower() != 'unisex':
            if query['bool'].get('must') is None:
                query['bool']['must'] = list()
            query['bool']['must'].appen({
                "term": {"gender": gender}
            })

        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)\
            .query(query)[offset:offset + size]
        response = s.execute()
        return self.convert([item['_source'] for item in response.hits.hits])

    def get_by_price(self, max_price, min_price=0, page=1, size=20, **kwargs):
        offset = (page - 1) * size
        query = {
            "bool": {
                "must": [
                    {"range": {"rs_selling_price": {
                        "gt": min_price,
                        "lte": max_price
                    }}}
                ]
            }
        }
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)\
            .query(query)[offset:offset + size]
        response = s.execute()

        return self.convert([item['_source'] for item in response.hits.hits], personalize=True)

    def find_by_id(self, id, log=False, session_id=None, user_id=None, tier=None, **kwargs):
        es = self.elasticsearch
        item = es.get(index=self.INDEX_NAME, doc_type=self.DOC_TYPE, id=id, ignore=[404]).get('_source')
        if log and item is not None:
            # TODO: Track logs
            log_model = ProductVisitLog(session_id, customer_id=user_id)
            log_model.insert(self.convert_item(item))
        return self.convert_item(item, tier=tier)

    def get_smiliar_styles(
            self, id, page=1, size=20, customer_id='BLANK',
            tier=None, **kwargs):
        offset = (page - 1) * size
        item = self.find_by_id(id)
        if item is None:
            return []
        product_type = item.get('product_type')
        sub_type = item.get('product_sub_type')
        gender = item.get('gender')
        brand = item.get('brand')
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)\
            .query({
                "bool": {
                    "must": [
                        {
                            "range": {"sizes.qty": {"gt": 0}}
                        },
                        {
                            "term": {"gender": gender}
                        },
                        {
                            "term": {"product_size_attribute": product_type}
                        },
                        {
                            "term": {"rs_product_sub_type": sub_type}
                        },
                        {
                            "match": {"manufacturer": brand}
                        }
                    ],
                    "must_not": [
                        {
                            "match": {
                                "rs_sku": id
                            }
                        }
                    ]
                }
            }).query({
                "function_score" : {
                "query" : { "match_all": {} },
                "random_score" : {}
            }
            })[offset:offset + size]
        response = s.execute()

        return self.convert(
            [item['_source'] for item in response.hits.hits],
            tier=tier)

    def get_also_availables(self, id, tier=None, **kwargs):
        item = self.find_by_id(id)
        if item is None:
            return []

        base_sku = '_'.join(item.get('sku').split('_')[:-1])
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)\
            .query({
                "bool": {
                    "must": [
                        {
                            "range": {"sizes.qty": {"gt": 0}}
                        },
                        {
                            "prefix": {"rs_sku": {"value": base_sku}}
                        }
                    ],
                    "must_not": [
                        {
                            "match": {
                                "rs_sku": id
                            }
                        }
                    ]
                }
            }).query({
                "function_score" : {
                    "query" : { "match_all": {} },
                    "random_score" : {}
                }
            })
        response = s.execute()
        return self.convert([item['_source'] for item in response.hits.hits], tier=tier)

    def get_recently_viewed(
            self, session_id, customer_id=None,
            product_id=None, tier: dict = None, **kwargs):
        log_model = ProductVisitLog(session_id, customer_id=customer_id)
        logs = log_model.get_logs(omit=product_id)

        # for log in logs:
        #     discount = float(log.get('discount'))
        #     if log.get('original_price') is None:
        #         log['original_price'] = float(log.get('rs_selling_price'))

        #     original_price = float(log.get('original_price') or 0)
        #     current_price = original_price - original_price * discount / 100
        #     log['current_price'] = current_price
        #     if tier is not None and type(tier) == dict:
        #         log['fbucks'] = math.ceil(log['current_price'] * tier.get('discount_rate') / 100)

        # To implement discount
        skus: List[str] = [log['sku'] for log in logs]
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)\
            .query({
                "terms": {"rs_sku": skus}
            })
        response = s.execute()
        products = self.convert([item['_source'] for item in response.hits.hits], tier=tier)
        return sorted(
            products, key=lambda x: skus.index(x['sku']))

    def get_brands(
            self,
            keyword: str=None,
            prefix: List[str]=[], exclude: List[str]=[],
            page=1, size=1000, **kwargs) -> List[dict]:

        prefix = [item.lower().strip() for item in prefix]
        exclude = [item.lower().strip() for item in exclude]
        offset = (page - 1) * size
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME)
        s = s.update_from_dict({
            "query": {
                "bool": {
                    "must_not": [
                        {
                            "terms": {
                                "brand_code": [item.lower() for item in exclude if item]
                            }
                        }
                    ],
                    "must": [
                        {
                            "range": {
                                "sizes.qty": {
                                    "gte": 0
                                }
                            }
                        },
                        {
                            "bool": {
                                "should": [
                                    {
                                        "prefix": {
                                            "brand_code": item.lower()
                                        }
                                    } for item in prefix if item
                                ]
                            }
                        }
                    ]
                }
            },
            "aggs": {
                "brands": {
                    "terms": {
                        "field": "manufacturer",
                        "order": {
                            "_term": "asc"
                        },
                        "size": 1000
                    }
                }
            },
            "size": 10
        })

        response = s.execute()
        buckets = response.aggregations.brands.buckets
        return [item['key'] for item in buckets]

    def get_products_by_category(self, product_type, sub_types, gender, page=1, size=18):
        """Get products for a specific category
        """
        offset = (page - 1) * size
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME).query(
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "product_size_attribute": {
                                    "value": product_type
                                }
                            }
                        },
                        {
                            "term": {
                                "gender": {
                                    "value": gender
                                }
                            }
                        },
                        {
                            "terms": {
                                "rs_product_sub_type": sub_types + ['BLANK']
                            }
                        },
                        {
                            "range": {
                                "sizes.qty" : {
                                    "gt": 0
                                }
                            }
                        }
                    ]
                }
            })[offset: offset + size]

        response = s.execute()
        return self.convert([item['_source'] for item in response.hits.hits])

    def filter_subtypes_in_stock(self, subtypes, gender=None, **kwargs):
        subtype_names = [item['product_type_name'] for item in subtypes]
        if gender is None or gender.lower() == 'unisex':
            query = {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "rs_product_sub_type": subtype_names
                            }
                        },
                        {
                            "range": {
                                "sizes.qty" : {
                                    "gt": 0
                                }
                            }
                        }
                    ]
                }
            }
        else:
            query = {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "gender": {
                                    "value": gender.upper()
                                }
                            }
                        },
                        {
                            "terms": {
                                "rs_product_sub_type": subtype_names
                            }
                        },
                        {
                            "range": {
                                "sizes.qty" : {
                                    "gt": 0
                                }
                            }
                        }
                    ]
                }
            }
        s = Search(using=self.elasticsearch, index=self.INDEX_NAME).query(query)

        s.aggs.bucket('subtypes_in_stock', "terms", field='rs_product_sub_type')
        response = s.execute()
        buckets = response.aggregations.subtypes_in_stock.buckets
        buffer = {}
        for bucket in buckets:
            buffer[bucket['key']] = True

        return [item for item in subtypes if buffer.get(item['product_type_name'])]

    def find_by_simple_sku(self, simple_sku, **kwargs):
        config_sku = simple_sku.split('-')[0]
        item = self.get(config_sku)
        for size in item.get('sizes', []):
            if size['rs_simple_sku'] == simple_sku:
                if int(size['qty']) > 0:
                    item.update(size)
                break
        del item['sizes']
        return item


import json
from flask import Flask
from flask import make_response, jsonify

from optionPrice import *

def jsonify(status=200, indent=4, sort_keys=True, **kwargs):
    response = make_response(json.dumps(dict(**kwargs), indent=indent, sort_keys=sort_keys))
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    response.headers['mimetype'] = 'application/json'
    response.status_code = status
    return response

app = Flask(__name__)
@app.route('/<symbol>/<key>')
def index(symbol, key):
    dic = optionPrice(symbol, key).record()
    return jsonify(indent=2, sort_keys=False, flask_response=dic)

if __name__ == '__main__':
    app.run(host='0.0.0.0')


import socket
import os
import os.path

import crypto2

# creating a socket object
s_obj = socket.socket()
print ("Socket created successfully")

# allocate a port
port = 12345

# bind to the port
s_obj.bind(('10.7.55.35', port))
print ("socket binded to %s" %(port))

# put the socket into listening mode
s_obj.listen(5)
print ("socket is listening")

# accept the connection request from the client
c, address = s_obj.accept()
print ('Got connection from', address )

current_directory = os.getcwd()

while (True):
    cmd = c.recv(1024).decode()

    # command (1): CWD
    if (cmd == 'CWD'):
        c.send(current_directory.encode())

    # command (2): LS
    elif (cmd == 'LS'):
        str = ""
        directory_list = os.listdir(current_directory)

        for file in directory_list:
            str += file
            str += "$"

        str = str[:-1]
        c.send(str.encode())

    # command (3): CD
    elif (cmd == 'CD'):
        # get path
        getpath = c.recv(1024).decode()
        # try and except
        try:
            os.chdir(getpath)
            current_directory = os.getcwd()
            c.send('OK'.encode())
        except:
            c.send('NOK'.encode())

    # command (4): DWD
    elif (cmd == "DWD"):
        request_file = c.recv(1024).decode()
        does_file_exist = os.path.exists(request_file) # bool

        if (does_file_exist == False):
            c.send('NOK'.encode())
            continue
        else:
            c.send('OK'. encode())

        # encode
        crypto2.substitute_encode(request_file)

        with open(request_file, 'rb') as file_to_send:
            for data in file_to_send: c.sendall(data)

        crypto2.substitute_decode(request_file)

    # command (5): UPD
    elif (cmd == "UPD"):
        # cuurent directory is the upload directory
        download_directory = os.getcwd()

        file_name = c.recv(1024).decode()

        with open(os.path.join(download_directory, file_name), 'wb') as file_to_write:
            msg = c.recv(1024)
            if not msg:
                break
            file_to_write.write(msg)
        file_to_write.close()

        # decode
        crypto2.substitute_decode(file_name)

    # if invalid command, close
    else:
        # diplay error
        print("Invalid command\n")
        c.send('error'.encode())
        c.close()

s_obj.close()


import urllib
import os

import stringrnautils

################################################################################
# globals
################################################################################
organisms = ['9606', '10090', '10116', '4932']
UNIPROT_QUERY = "http://www.uniprot.org/uniprot/?query=organism:{0}&columns=id,go-id&format=tab"
# UNIPROT_QUERY = "http://www.uniprot.org/uniprot/?query=organism:{0}&columns=id,go-id,go(cellular component)&format=tab"

DATA_DIR = os.path.join(os.path.dirname(__file__), '../data')
goterm_to_urna = {

    ########################################
    # Biological Process (3'-]end processing
    'GO:0034473': ['U1'],
    'GO:0034474': ['U2'],
    'GO:0034475': ['U4'],
    'GO:0034476': ['U5'],
    'GO:0034477': ['U6'],

    ########################################
    # molecular function (binding)

    # uncommented because it does not discriminate with the U rna and the protein
    #'GO:1990446' : 'U1 snRNP',
    #'GO:1990447' : 'U2 snRNP',

    'GO:0030619': ['U1'],
    'GO:0030620': ['U2'],
    'GO:0030621': ['U4'],
    'GO:0030623': ['U5'],
    'GO:0017070': ['U6'],
    'GO:0071209': ['U7'],
    'GO:0030622': ['U4atac'],
    'GO:0030624': ['U6atac'],
    'GO:0030625': ['U11'],
    'GO:0030626': ['U12'],

    ########################################
    # Cellular Component (in complex)
    'GO:0005685': ['U1'],
    'GO:0005686': ['U2'],

    'GO:0005687': ['U4'],
    'GO:0005682': ['U5'],
    'GO:0005688': ['U6'],
    'GO:0005683': ['U7'],

    'GO:0005692': ['U11'],
    'GO:0005693': ['U12'],
    'GO:0005690': ['U4atac'],
    'GO:0005691': ['U6atac'],

    'GO:0071001': ['U4', 'U6'],
    'GO:0034693': ['U11', 'U12'],
    'GO:0071002': ['U4atac', 'U6atac'],
    ########################################
    # 'GO:0071024' : 'SL snRNP',
}

################################################################################
# Functions
################################################################################
def get_u_snrna_rna_rna_interactions():
    interactions = []

    # Core component of the spliceosomal U1, U2, U4 and U5 small nuclear ribonucleoproteins (snRNPs),
    # the building blocks of the spliceosome
    core = ('U1', 'U2', 'U4', 'U5')
    for i, rna1 in enumerate(core, 1):
        for rna2 in core[i:]:
            # a better refrence may be: Maniatis, T. and Reed, R. (1987). Nature 325, 673-678.
            # but I do not have journal acces at my new job and have therefore not read it -jcr
            url = "http://bioscience.jbpub.com/cells/MBIO5245.aspx"
            interactions.append((rna1, rna2, url))

    # splisosomal tri complex GO:0097526
    url = "http://www.ebi.ac.uk/QuickGO/GTerm?id={}"
    interactions.append(('U4', 'U5', url.format('GO:0046540')))
    interactions.append(('U4', 'U6', ','.join((url.format('GO:0046540'), url.format('GO:0071001')))))
    interactions.append(('U5', 'U6', url.format('GO:0046540')))
    interactions.append(('U11', 'U12', url.format('GO:0034693')))
    interactions.append(('U4atac', 'U6atac', url.format('GO:0071002')))
    return interactions

def download():
    for organism in organisms:
        url = UNIPROT_QUERY.format(organism).replace(' ', '%20')
        file_name = os.path.join(DATA_DIR, '{}_celluar_component.tsv'.format(organism))
        print (url)
        print (file_name)
        urllib.urlretrieve(url, file_name)

def run():
    # TODO: UNCOMMENT BEFORE YOU COMMIT!!!
    # download()

    master_file = open ('master_files/database_spliceosome.tsv', 'w')

    # TODO: map RNAs as well!!!
    for organism in organisms:
        print (' - generating interactions for {}'.format(organism))
        interactions = get_u_snrna_rna_rna_interactions()
        for ent1, ent2, url in interactions:
            _str = "\t".join((organism, ent1, ent2, "0", "DATABASE", "0.9", url, ''))
            master_file.write("{}\n".format(_str))

        uniprot_to_ensp = stringrnautils.get_alias_to_string_mapper(organism, '', '', 10)[organism]
        with open('data/{}_celluar_component.tsv'.format(organism)) as go_terms_file:
            go_terms_file.readline()  # skip header
            for line in go_terms_file:
                try:
                    uniprot_acc, goterms = line.rstrip().split('\t')
                except ValueError:
                    # there are no go terms for this protein
                    continue
                url = 'http://www.uniprot.org/uniprot/{}'.format(uniprot_acc)
                for go_term in goterms.split('; '):
                    ensp = uniprot_to_ensp.get(uniprot_acc, None)
                    if ensp:
                        for u_rna in goterm_to_urna.get(go_term, []):
                            _line = "\t".join((organism, u_rna, ensp, "0", "DATABASE", "0.9", url, ''))
                            master_file.write("{}\n".format(_line))

if __name__ == '__main__':
    run()


import hashlib
import string

SUCCESSFUL = [200, 201, 202, 203, 204, 205, 206]

SERVICE_NAME = "OIC"
CLIENT_CONFIG = {}

DEFAULT_OIDC_SERVICES = {
    "discovery": {"class": "idpyoidc.client.oidc.provider_info_discovery.ProviderInfoDiscovery"},
    "registration": {"class": "idpyoidc.client.oidc.registration.Registration"},
    "authorization": {"class": "idpyoidc.client.oidc.authorization.Authorization"},
    "access_token": {"class": "idpyoidc.client.oidc.access_token.AccessToken"},
    "refresh_access_token": {
        "class": "idpyoidc.client.oidc.refresh_access_token.RefreshAccessToken"
    },
    "userinfo": {"class": "idpyoidc.client.oidc.userinfo.UserInfo"},
}

DEFAULT_OAUTH2_SERVICES = {
    "discovery": {"class": "idpyoidc.client.oauth2.server_metadata.ServerMetadata"},
    "authorization": {"class": "idpyoidc.client.oauth2.authorization.Authorization"},
    "access_token": {"class": "idpyoidc.client.oauth2.access_token.AccessToken"},
    "refresh_access_token": {
        "class": "idpyoidc.client.oauth2.refresh_access_token.RefreshAccessToken"
    },
}

DEFAULT_CLIENT_PREFERENCES = {
    "application_type": "web",
    "response_types": [
        "code",
        "id_token",
        "code id_token",
    ],
    "token_endpoint_auth_method": "client_secret_basic",
    "scopes_supported": ["openid"],
}

DEFAULT_USAGE = {
    "jwks_uri": True,
    "scope": ["openid"],
}

# Using PKCE is default
DEFAULT_CLIENT_CONFIGS = {
    "": {
        "client_type": "oidc",
        "preference": DEFAULT_CLIENT_PREFERENCES,
        "add_ons": {
            "pkce": {
                "function": "idpyoidc.client.oauth2.add_on.pkce.add_support",
                "kwargs": {"code_challenge_length": 64, "code_challenge_method": "S256"},
            }
        },
    }
}

DEFAULT_KEY_DEFS = [
    {"type": "RSA", "use": ["sig"]},
    {"type": "EC", "crv": "P-256", "use": ["sig"]},
]

DEFAULT_RP_KEY_DEFS = {
    "private_path": "private/jwks.json",
    "key_defs": DEFAULT_KEY_DEFS,
    "public_path": "static/jwks.json",
    "read_only": False,
}

OIDCONF_PATTERN = "{}/.well-known/openid-configuration"
OAUTH2_SERVER_METADATA_URL = "{}/.well-known/oauth-authorization-server"

CC_METHOD = {
    "S256": hashlib.sha256,
    "S384": hashlib.sha384,
    "S512": hashlib.sha512,
}

# Map the signing context to a signing algorithm
DEF_SIGN_ALG = {
    "id_token": "RS256",
    "userinfo": "RS256",
    "request_object": "RS256",
    "client_secret_jwt": "HS256",
    "private_key_jwt": "RS256",
}

HTTP_ARGS = ["headers", "redirections", "connection_type"]

JWT_BEARER = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
SAML2_BEARER_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:saml2-bearer"

BASECHR = string.ascii_letters + string.digits

DEFAULT_RESPONSE_MODE = {
    "code": "query",
    "id_token": "fragment",
    "token": "fragment",
    "code token": "fragment",
    "code id_token": "fragment",
    "id_token token": "fragment",
    "code id_token token": "fragment",
}


import sys
import re

path = sys.argv[1]
with open(path,'r') as f:
    new_list = []
    lines = f.readlines()
    for line in lines:
        if not line.startswith('-'):
            new_list.append(line)
    with open(path, "w") as f:
        for line in new_list:
            f.write(line)


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
from datetime import time
import os
import matplotlib
from celluloid import Camera

def to_polar(X,Y,Z):
    a = 6378137.000
    b = 6356752.3141
    c = a**2 / b
    e1 = np.sqrt((a**2 - b**2) / a**2)
    e2 = np.sqrt((a**2 - b**2) / b**2)
    p = np.sqrt(X**2 + Y**2)
    lam = np.arctan2(Y , X)
    beta = np.arctan2(a * Z , (b*p))
    fi = np.arctan2((Z+e2**2 * b *np.sin(beta)**3),(p-e1**2 * a * np.cos(beta)**3))
    N = c / np.sqrt(1 + e2**2 * np.cos(fi)**2)
    h = p / np.cos(fi) - N
    return fi,lam,h

def to_SagYukarı(fi,lam):
    a = 6378137.000
    b = 6356752.3141
    c = a**2 / b
    e1 = np.sqrt((a**2 - b**2) / a**2)
    e2 = np.sqrt((a**2 - b**2) / b**2)
    N = c / np.sqrt(1 + e2**2 * np.cos(fi)**2)
    der = 27 *np.pi/ 180
    A0 = 1- e1**2 /4 - 3*e1**4 /64 -5*e1**6 /256 -175*e1**8 /16384
    A2 = 3/8 *(e1**2 +e1**4 /4+ 15*e1**6 /128- 455*e1**8 /4096)
    A4 = 15/256*(e1**4 +3*e1**6 /4 - 77*e1**8/128)
    A6 = 35/3072*(e1**6 -41*e1**8 /32)
    A8 = (-315)/131072 *e1**8
    Sfi = a*(A0*fi-A2*np.sin(2*fi) + A4* np.sin(4*fi) - A6 *np.sin(6*fi) + A8 *np.sin(8*fi))
    lam = lam - der
    u = lam*np.cos(fi)
    t = np.tan(fi)
    n_squared = (e2**2)*np.cos(fi)**2
    x = N*(u+u**3 /6*(1-t**2 +n_squared)+ u**5 /120 *(5- 18*t**2 + t**4 +14*n_squared - 58*t**2
           * n_squared+ 13*n_squared**2 +4 *n_squared**3 -64 *n_squared**2 *t**2 -24*n_squared**3
           *t**2)+ u**7 /5040 *(61- 479*t**2 +179*t**4 -t**6))
    y = N*(Sfi/N+lam**2 *np.sin(fi)* np.cos(fi)/ 2+ lam**4 *np.sin(fi)* np.cos(fi)**3 / 24 *
           (5-t**2 +9*n_squared +4*n_squared**2)+lam**6 *np.sin(fi)* np.cos(fi)**5 /720 *(61-
           58 *t**2 +t**4 +270*n_squared +445*n_squared**2 +324*n_squared**3- 680*n_squared**2 *
           t**2 +88*n_squared**4 -600*n_squared**4 -600*n_squared**3 *t**2 -192*n_squared**4 *t**2)+
           lam**8 *np.sin(fi)* np.cos(fi)**7 /40320 *(1385- 311*t**2 +543*t**4 -t**6))

    conv = lam*np.sin(fi)*(1+ lam**2 *np.cos(fi)**2 /3 *(1+3*n_squared+2*n_squared**2)+lam**4 * np.cos(fi)**4 /15* (2-t**2))

    yukari = y*0.9996
    saga = x*0.9996 + 500000
    return saga,yukari
def grafnav_read(fname):
    f = open(fname,"r")
    data = f.readlines()
    f.close()
    for i in range(len(data)):
        if len(data[i].split()) != 0  and data[i].split()[0] == "(HMS)":
            break
        else:
            continue

    del data[:i+1]
    easting = []
    northing = []
    height = []
    epoch = []

    for i in range(len(data)):
        if len(data[i].split()) == 0:
            break
        else:
            epoch.append(datetime.strptime(("2021-10-26"+" "+str(data[i].split()[0])),"%Y-%m-%d %H:%M:%S.%f"))
            height.append(float(data[i].split()[3]))
            easting.append(float(data[i].split()[1]))
            northing.append(float(data[i].split()[2]))
    return epoch, easting, northing, height

def output_read(fname):
    # PPP-WIZARD output reader (verbose on)
    f = open(fname,"r")
    data = f.readlines()
    f.close()

    easting = []
    northing = []
    height = []
    X_list = []
    Y_list = []
    Z_list = []
    XS_list = []
    YS_list = []
    ZS_list = []
    epoch = []

    for i in range(len(data)):
        epoch.append(datetime.strptime(("20"+str(data[i].split()[0])+" "+str(data[i].split()[1])),"%Y-%m-%d %H:%M:%S.%f"))
        X = float(data[i].split()[12])
        Y = float(data[i].split()[15])
        Z = float(data[i].split()[18])
        X_list.append(X)
        Y_list.append(Y)
        Z_list.append(Z)
        XS_list.append(float(data[i].split()[14]))
        YS_list.append(float(data[i].split()[17]))
        ZS_list.append(float(data[i].split()[20]))
        fi,lam,h = to_polar(X,Y,Z)
        S,Y = to_SagYukarı(fi,lam)
        height.append(h)
        easting.append(S)
        northing.append(Y)
    return epoch, easting, northing, height, X_list, Y_list, Z_list,XS_list,YS_list,ZS_list

def Sdisp2vel(data,dt = 1):
    vel = []
    i = 1
    while i < (len(data)):
        vel.append((data[i-1]**2 + data[i]**2)**0.5 /dt)
        i+=1
    return vel

def disp2vel(data,dt = 1):
    vel = []
    i = 1
    while i < (len(data)):
        vel.append((data[i] - data[i-1])/dt)
        i+=1
    return vel

def vel2acc(data,dt = 1):
    acc = []
    i = 1
    while i < (len(data)):
        acc.append((data[i]-data[i-1])/dt)
        i+=1
    return acc
def graph_E_N2(var1,var2,Svar1,Svar2,Tvar1,Tvar2,fname):
    var1 = np.array(var1)
    var2 = np.array(var2)
    Svar1 = np.array(Svar1)
    Svar2 = np.array(Svar2)
    Tvar1 = np.array(Tvar1)
    Tvar2 = np.array(Tvar2)

    #matplotlib.use('Agg')
    fig = plt.figure(figsize=(10,7))
    plt.rcParams.update({'font.size': 9})

    plt.grid(True, linestyle='--',linewidth=0.2)
    plt.title("2D Scatter Plot",  fontsize=10)
    plt.ylabel("Northing (\u0394X) (cm)",  fontsize=9)
    plt.xlabel("Easting (\u0394Y) (cm)",  fontsize=9)

    xax = plt.gca().axes.get_xaxis()
    xax = xax.set_visible(False)

    # set visibility of y-axis as False
    yax = plt.gca().axes.get_yaxis()
    yax = yax.set_visible(False)
    for i in range(len(var1)):
        plt.scatter(var2[:i],var1[:i],c='b',marker="o",s = 2,label="rover")
        plt.scatter(Svar2[:i],Svar1[:i],c='r',marker="o", s = 4,label="filtered rover")
        plt.scatter(Tvar2[:i],Tvar1[:i],c='g',marker="o", s = 2,label="Predicted rover")

        plt.pause(0.001)
    plt.show()

def graph_E_N(var1,var2,Svar1,Svar2,Tvar1,Tvar2,Fvar1,Fvar2,fname):
    var1 = np.array(var1)
    var2 = np.array(var2)
    Svar1 = np.array(Svar1)
    Svar2 = np.array(Svar2)
    Tvar1 = np.array(Tvar1)
    Tvar2 = np.array(Tvar2)
    Fvar1 = np.array(Fvar1)
    Fvar2 = np.array(Fvar2)

    fig = plt.figure(figsize=(20,15))
    plt.plot(var2,var1,color='b',marker="o", linestyle="None",markersize = 2,label="rover")
    plt.plot(Svar2,Svar1,color='r',marker="o", linestyle="None", markersize = 4,label="filtered rover")
    plt.plot(Tvar2,Tvar1,color='g',marker="o", linestyle="None",markersize = 2,label="Predicted rover")
    plt.plot(Fvar2,Fvar1,color='black',marker="o", linestyle="None",markersize = 2,label="Fixed rover")
    plt.grid(True, linestyle='--',linewidth=0.2)
    plt.legend(loc=1, handlelength=1,  fontsize=12,markerscale=1)
    plt.title("2D Scatter Plot",  fontsize=12)
    plt.ylabel("Northing (\u0394X) (cm)",  fontsize=12)
    plt.xlabel("Easting (\u0394Y) (cm)",  fontsize=12)
    xax = plt.gca().axes.get_xaxis()
    xax = xax.set_visible(False)
    yax = plt.gca().axes.get_yaxis()
    yax = yax.set_visible(False)
    plt.tight_layout()
    plt.savefig("kalman2d.png")

def kalman(Xmatrx, obs_err, init, pro_err,aX):
    i           = int(np.size(Xmatrx)/len(Xmatrx))

    A           = np.identity(i)
    pc = 0
    while pc < i:
        A[pc][pc+1] = init[1]
        pc += 2

    B           = np.identity(i)
    pc = 0
    while pc < i:
        B[pc][pc] = 0.5 * init[1]**2
        B[pc+1][pc+1] = init[1]
        pc += 2

    H           =  np.identity(i)
    for pc in range(i):
        if pc % 2 != 0:
            H[pc][pc] = 0
    C           = np.identity(i)
    I           = np.identity(i)

    X_kp        = np.ndarray((i,1), dtype = float)
    X_k_1       = np.array(Xmatrx[0]).reshape((-1,1))
    P_k_1       = np.ndarray((i,i), dtype = float)
    for k in range(i):
        P_k_1[k][k] = pro_err[k]**2

    R           = np.ndarray((i,i), dtype = float)

    x_k         = np.ndarray((len(Xmatrx)-1,3), dtype = float)
    x_p         = np.ndarray((len(Xmatrx)-1,3), dtype = float)
    x_m         = np.ndarray((len(Xmatrx)-1,3), dtype = float)

    k, j        = 0, 1
    while j < len(Xmatrx):
        for pc in range(i):
            R[pc][pc]           = obs_err[k][pc]**2
        X_kp            = np.dot(A, X_k_1) + np.dot(B, np.array([aX[k]]).T)
        P_kp            = (np.dot(np.dot(A, P_k_1), np.transpose(A)))
        for pc in range(i):
            for pk in range(i):
                if pc !=pk:
                    P_kp[pc][pk] = 0

        KG          = np.divide(np.dot(P_kp, H), (np.dot(np.dot(H, P_kp), np.transpose(H)) + R))
        for pc in range(i):
            for pk in range(i):
                if pc !=pk:
                    KG[pc][pk] = 0

        Y_k         = np.dot(H, Xmatrx[j].reshape((-1, 1)))
        X_k         = X_kp + np.dot(KG, (Y_k - np.dot(H, X_kp)))
        P_k         = np.dot((I - np.dot(KG, H)), P_kp)

        X_k_1       = X_k
        P_k_1       = P_k
        x_k[k]      = X_k[0][0],X_k[2][0],X_k[4][0]
        x_p[k]      = X_kp[0][0],X_kp[2][0],X_kp[4][0]
        x_m[k]      = Xmatrx[j][0],Xmatrx[j][2],Xmatrx[j][4]

        j += 1
        k += 1
    return x_m, x_k, x_p
epoch, easting, northing, height, X_list, Y_list, Z_list,XS_list,YS_list,ZS_list = output_read("output_lowlevel_itu_kampus_RAIM")

np.seterr(divide = 'ignore', invalid = 'ignore')

vX = disp2vel(X_list)
aX = disp2vel(vX)
SvX = Sdisp2vel(XS_list)
SaX = Sdisp2vel(SvX)
SaX.append(0)
aX.append(0)
del X_list[-1]
del XS_list[-1]

vY = disp2vel(Y_list)
aY = disp2vel(vY)
SvY = Sdisp2vel(YS_list)
SaY = Sdisp2vel(SvY)
SaY.append(0)
aY.append(0)
del Y_list[-1]
del YS_list[-1]

vZ = disp2vel(Z_list)
aZ = disp2vel(vZ)
SvZ = Sdisp2vel(ZS_list)
SaZ = Sdisp2vel(SvZ)
SaZ.append(0)
aZ.append(0)
del Z_list[-1]
del ZS_list[-1]

X               = np.array((list(zip(X_list, vX, Y_list, vY ,Z_list, vZ))))
obs_err         = np.array((list(zip(XS_list, SvX,YS_list, SvY, ZS_list, SvZ))), dtype = float)
pro_err         = np.array(([np.mean(XS_list), np.mean(SvX), np.mean(YS_list), np.mean(SvY), np.mean(ZS_list), np.mean(SvZ)]), dtype = float)
init            = np.array(([10],[1],[0]), dtype = float)
A               = np.array((list(zip(aX, aX, aY, aY ,aZ, aZ))))
x_m, x_k, x_p   = kalman(X, obs_err, init, pro_err,A)

height = []
northing = []
easting = []
height2 = []
northing2 = []
easting2 = []
height3 = []
northing3 = []
easting3 = []

for i in range(len(x_m)):

    fi,lam,h = to_polar(x_m[i][0],x_m[i][1],x_m[i][2])
    S,Y = to_SagYukarı(fi,lam)
    height.append(h)
    easting.append(S)
    northing.append(Y)

for i in range(len(x_k)):

    fi,lam,h = to_polar(x_k[i][0],x_k[i][1],x_k[i][2])
    S,Y = to_SagYukarı(fi,lam)
    height2.append(h)
    easting2.append(S)
    northing2.append(Y)

for i in range(len(x_p)):
    fi,lam,h = to_polar(x_p[i][0],x_p[i][1],x_p[i][2])
    S,Y = to_SagYukarı(fi,lam)
    height3.append(h)
    easting3.append(S)
    northing3.append(Y)

epoch4, easting4, northing4, height4 = grafnav_read("grafnav-brodcastorbitilePPK-chc-rt-ppp-rinex304v2.txt")

graph_E_N(northing,easting,northing2,easting2,northing3,easting3,northing4,easting4,"kalman")
fig = plt.figure()
ax = plt.axes(projection ='3d')

ax.plot3D(northing3,easting3, height3, 'green')
plt.show()
ax.plot3D(northing4,easting4, height4, 'green')
plt.show()


from functools import reduce
from typing import List
from typing import Optional

class Solution:

    # xor of same ints will eliminate each other
    # e.g.,
    # 0001 xor 1111 = 1110
    # 1110 xor 1111 = 0001

    def singleNumber(self, nums: List[int]) -> int:
        res = 0
        for i in range(len(nums)):
            # Pairs of numbers will be eliminated with xor
            # Then the single number will be remained
            res ^= nums[i]
        return res

def test():
    solution = Solution()
    # test method
    print(solution.singleNumber([2, 2, 1]))
    print(solution.singleNumber([4, 1, 2, 1, 2]))

    nums = [5, 4, 2, 3, 5]
    # Python reduce/aggregation function
    result = reduce(lambda value, element: value + element, nums)

test()


from flask import Blueprint, request
from firebase_admin import auth
from os import getenv
from ..helper import get_bucket_storage
from ..decorator import authenticated_only

profiles = Blueprint("profiles", __name__)

@profiles.route("/profile-pictures", methods=["PUT"])
@authenticated_only
def update_profile_picture():
    bucket = get_bucket_storage(getenv("BUCKET_NAME"))
    user_id = request.user.get("uid")

    file = request.files.get("photo")
    if not (file and file.content_type.startswith("image/")):
        return {"message": "File is not a valid image"}, 400

    blob = bucket.blob(f"profiles/photo-{user_id}.{file.content_type[6:]}")
    blob.upload_from_file(file)
    blob.make_public()

    user = auth.update_user(uid=user_id, photo_url=blob.public_url)

    return {"data": user.photo_url}, 200


'''
    Data Structures - int, bool, list, tuple, dict, set

    if/else - conditional block

    for, while - looping
'''

'''
    Input your name, age ->
        based upon the age it will display

    For entering into bar, min age required is 18
    Mohan, 14 ->  Sorry Mohan!!! You have to grown up
    Krish, 24 -> Welcome Krish to the club
'''
'''
name = input("Enter your name: ")
age = int(input("Enter your age: "))

if age >= 18:
    print(f"Welcome {name} to the club")
else:
    print(f"Sorry {name} !!! You have to grown up")
'''
# Practice - Print the years that person has to come after that

'''
    Multiple if, else statement blocks

    IT Slabs -
        If salary is lesser than or equal to 10K, person belongs to "Group A"
        If salary is greater than 10K and lesser than 50K, person belongs to "Group B"
        If salary is greater than 50K, person belongs to "Group C"
'''

'''
name = input("Enter your name: ")
salary = int(input("Enter your salary: "))

person_cat = ""

if salary <= 10000:
    person_cat = "Group A"
elif salary > 10000 and salary < 50000:
    person_cat = "Group B"
else:
    person_cat = "Group C"

print(f"Hi {name}, Your category is {person_cat}")
'''

'''
    Collect the name of fruits from the users - List
    End button - to close down the capture
        By typing end/break in the fruit name
'''

# List - Data structure, collection of items/elements

from importlib.abc import ExecutionLoader
from re import M
from tokenize import Name
from typing import Dict

fruits = []
veggies = []

# lower - lower case all characters in string
# upper - Upper case all characters in string

    # Categorization logic - which will input from the user to specific

#    if :
#        veggies.append(inp_name)
#    else:

#print(fruits)
#print(veggies)

'''
    Unique - set
'''

#print(type(fruits))

uq_fruits = set(fruits)

#print(f"Unique fruit name: {uq_fruits}")

'''
    Agenda:

        Dict from input - Columns

        Pythonic ways to write up
            List comprehension
            Dict comprehension

        Functions
            What & why
            Arguments - Positional, Keyword, Optional, Default
                        Args, Keyword-based arguments

'''

# List/Tuple - Collection of items/elements
    # Acces - index (indices)

# Dict - Key-value based collection of items
    # Access - Key

'''
Spreadsheet -
    Rows & Columns

Columns - Keys of Dict
Row - Element in a dict

Rows -
    row1
    row2
    .
    .
    .
    rown

List - collection of item/element - dict

Name    Age     Gender
Nitish  23      Male                -> Element 1
Ramesh  34      Male                -> Element 2
Sophia  56      Female              -> Element 3

'''

row1 = {"Name": "Nitish", "Age": 23, "Gender": "Male", "NewKey": "val"}
row2 = {"Name": "Ramesh", "Age": 34, "Gender": "Male"}
row3 = {"Name": "Sophia", "Age": 56, "Gender": "Female"}

#print(f"Age - {row1['Age']}")
#print(f"Name - {row2['Name']}")
#print(f"Gender - {row3['Gender']}")

# Keys and values access from dict
#print(f"Keys: {row1.keys()}, Values - {row1.values()}")

'''
    Spread-sheet, List of dict
'''

spread_sheet = []

while True:

    name = input("Enter the person name: ")

    if name == "eol":
        break

    age = int(input("Enter the age: "))
    gender = input("Enter the gender: ")

    row = {"Name": name, "Age": age, "Gender": gender}

    spread_sheet.append(row)

print(spread_sheet)

# enumerate - Iterate over the list & dict:
# list - index
# dict - key

'''
keys() - Name, age & gender
    Name    age     gender          -    Keys in dict element - printed only for 1st record
    Nitish  23      M
    Ramesh  45      M

'''

for idx, row in enumerate(spread_sheet):

    # Print the header - keys() - header - ['Name', 'Age', 'Gender']
    if idx == 0:
        header = list(row.keys())
        print(f"{header[0]} :: {header[1]} :: {header[2]}")

    print(f"{row['Name']} :: {row['Age']} :: {row['Gender']}")

'''
    List comprehension - pythonic way to generate/filter the list

    Filter out a list of numbers  - odd, even
'''

#[1,2,3,4,5, ... 99]
# odd number and even number
# odd = [1,3,5,7]
# even = [2,4,6]

odd = []
even = []

for i in range(1, 100):
    if i %2 == 0:
        even.append(i)
    else:
        odd.append(i)

print(f"Odd - {odd}")
print(f"Even - {even}")

#odd = [ <Iteraable> {<if_condition>} ]

odd2 = [i  for i in range(1, 100) if i % 2 != 0]
even2 = [elem for elem in range(1,100) if elem %2 == 0]

print(f"Odd - {odd2}")
print(f"Even - {even2}")

# Dict Comprehension
# Dict {1:1, 2: 4, 3: 9}
print("*"*90)
sq_nums = {elem: elem*elem for elem in range(1,10)}
print(list(sq_nums.values()))
print(sq_nums)

sq_nums_ls = [elem*elem for elem in range(1, 10) ]
print("*"*90)
print(sq_nums_ls)


import bisect
class Solution:
    #Function to find length of longest increasing subsequence.
    def longestSubsequence(self,a,n):
        lis=[]
        for i in a:
            if len(lis)==0:
                lis.append(i)
            elif i>lis[-1]:
                lis.append(i)
            else:
                lis[bisect.bisect_left(lis,i)]=i
        return len(lis)


import RPi.GPIO as GPIO;
import time;

class Motor():
    """ Defines motor connection for the CI L298.
        The IN1 and IN2 pins control the direction of rotation.
        This class considers the enabled pins as always on.
    """
    def __init__(self, IN1, IN2):
        # Define the pins as output:
        self.IN1 = IN1;
        self.IN2 = IN2;
        GPIO.setmode(GPIO.BCM);
        GPIO.setup(IN1, GPIO.OUT);
        GPIO.setup(IN2, GPIO.OUT);

        # Set all pins to low:
        self.stop();

    def forward(self):
        GPIO.output(self.IN1, True);
        GPIO.output(self.IN2, False);

    def reverse(self):
        GPIO.output(self.IN1, False);
        GPIO.output(self.IN2, True);

    def stop(self):
        GPIO.output(self.IN1, False);
        GPIO.output(self.IN2, False);

if __name__ == "__main__":
    # Test individual motors:
    try:
        # Top Left Motor:
        top_left = Motor(11, 5);
        top_left.forward();
        time.sleep(1);
        top_left.reverse();
        time.sleep(1);

        # Bottom Left Motor:
        bottom_left = Motor(22, 27);
        bottom_left.forward();
        time.sleep(1);
        bottom_left.reverse();
        time.sleep(1);

        # Top Right Motor:
        top_right = Motor(26, 19);
        time.sleep(1);
        top_right.forward();
        time.sleep(1);
        top_right.reverse();

        # Bottom Right Motor:
        bottom_right = Motor(6, 13);
        bottom_right.forward();
        time.sleep(1);
        bottom_right.reverse();
        time.sleep(1);

        time.sleep(1);
        GPIO.cleanup();
    except:
        GPIO.cleanup();


import numpy as np
import pandas as pd

if __name__ == '__main__':
    df = pd.read_csv('gender2.csv',encoding="EUC-KR",index_col=0,header=0)
    df = df[['2022년_계_총인구수', '2022년_남_총인구수','2022년_여_총인구수']]

    df.rename(columns={'2022년_계_총인구수': 'Total','2022년_남_총인구수':'Male','2022년_여_총인구수':'Female'}, inplace=True)
    print(df)


#
# @lc app=leetcode.cn id=2134 lang=python3
#
# [2134] 最少交换次数来组合所有的 1 II
#
# https://leetcode-cn.com/problems/minimum-swaps-to-group-all-1s-together-ii/description/
#
# algorithms
# Medium (45.71%)
# Likes:    26
# Dislikes: 0
# Total Accepted:    4.6K
# Total Submissions: 10K
# Testcase Example:  '[0,1,0,1,1,0,0]'
#
# 交换 定义为选中一个数组中的两个 互不相同 的位置并交换二者的值。
#
# 环形 数组是一个数组，可以认为 第一个 元素和 最后一个 元素 相邻 。
#
# 给你一个 二进制环形 数组 nums ，返回在 任意位置 将数组中的所有 1 聚集在一起需要的最少交换次数。
#
#
#
# 示例 1：
#
# 输入：nums = [0,1,0,1,1,0,0]
# 输出：1
# 解释：这里列出一些能够将所有 1 聚集在一起的方案：
# [0,0,1,1,1,0,0] 交换 1 次。
# [0,1,1,1,0,0,0] 交换 1 次。
# [1,1,0,0,0,0,1] 交换 2 次（利用数组的环形特性）。
# 无法在交换 0 次的情况下将数组中的所有 1 聚集在一起。
# 因此，需要的最少交换次数为 1 。
#
#
# 示例 2：
#
# 输入：nums = [0,1,1,1,0,0,1,1,0]
# 输出：2
# 解释：这里列出一些能够将所有 1 聚集在一起的方案：
# [1,1,1,0,0,0,0,1,1] 交换 2 次（利用数组的环形特性）。
# [1,1,1,1,1,0,0,0,0] 交换 2 次。
# 无法在交换 0 次或 1 次的情况下将数组中的所有 1 聚集在一起。
# 因此，需要的最少交换次数为 2 。
#
#
# 示例 3：
#
# 输入：nums = [1,1,0,0,1]
# 输出：0
# 解释：得益于数组的环形特性，所有的 1 已经聚集在一起。
# 因此，需要的最少交换次数为 0 。
#
#
#
# 提示：
#
#
# 1 <= nums.length <= 10^5
# nums[i] 为 0 或者 1
#
#
#

# @lc code=start
class Solution:
    def minSwaps(self, nums: List[int]) -> int:
        n = len(nums)
        windowLen = sum(nums)
        cnt = sum(nums[:windowLen])
        ans = cnt
        for i in range(windowLen, n):
            cnt += nums[i]
            cnt -= nums[i-windowLen]
            ans = max(ans,cnt)
            # print(ans)
        for i in range(windowLen):
            cnt -= nums[-windowLen+i]
            cnt += nums[i]
            # print(nums[-windowLen+i], nums[i])
            ans = max(ans,cnt)
            # print(ans)
        return windowLen-ans
# @lc code=end


"""
This module holds the keypress control system class, Fovea setup, and linalg methods
used to transform disc data for the PCA user story.
"""

from __future__ import absolute_import, print_function

from PyDSTool.Toolbox import data_analysis as da

import numpy as np
import matplotlib.pyplot as plt

from PyDSTool import *
import retina.core.axes
import random

def translate(X, axis, amount):
    A = np.zeros((len(X), len(X[0])))
    A[:,axis] = np.ones(len(X))*amount
    Y = X + A
    return Y

def rotate_x(X, theta):
    R = np.array([[1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def rotate_y(X, theta):
    R = np.array([[np.cos(theta), 0, -np.sin(theta)], [0, 1, 0], [np.sin(theta), 0, np.cos(theta)]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def rotate_z(X, theta):
    R = np.array([[np.cos(theta), np.sin(theta), 0], [-np.sin(theta), np.cos(theta), 0], [0, 0, 1]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def stretch(X, axis, amount):
    X[:,axis] = X[:,axis]*amount
    return X

def noise(X, axis, percent, loc, scale):
    for i in range(0, int(round(len(X)*percent))):
        X[i][axis] = np.random.normal(loc, scale, 1)
    return X

def ortho_proj_mat(n, m):
    Z = npy.random.rand(n, m)
    Q, R = npy.linalg.qr(Z)
    return Q

def compute(X, new_dim, layer, style, before, after, variance, proj_vecsLO = None, proj_vecsHI = None):

    data_dict = {}
    data_dict['X'] = X

    p = da.doPCA(X, len(X[0]), len(X[0])) #Creates a pcaNode object.

    pcMat = p.get_projmatrix()

    pcPts = np.concatenate(([pcMat.transpose()[0,:]*15],
                            [-pcMat.transpose()[0,:]*15]), axis=0)

    for j in range(1, new_dim):
        pcPts = np.concatenate((pcPts, [pcMat.transpose()[j,:]*15],
                                [-pcMat.transpose()[j,:]*15]), axis=0)

    Y = p._execute(X, new_dim) #Get dimensionality reduced data.
    data_dict['Y'] = Y

    #Produce proj vecs if none supplied
    if proj_vecsHI is None:
        proj_vecsHI = ortho_proj_mat(len(X[0]), 3)

    if proj_vecsLO is None:
        proj_vecsLO = ortho_proj_mat(len(Y[0]), 2)

    #If data are high-dimensional, use the projection vectors.
    if len(X[0]) > 3:
        X = npy.dot(X, proj_vecsHI)
        pcPts = npy.dot(pcPts, proj_vecsHI)
        data_dict['X_projected'] = X

    if len(Y[0]) > 2:
        proj_vecsLO = proj_vecsLO[0:new_dim, :] #Scrape the needed rows from projection matrix, so we can page between dimensionalities.
        Y = npy.dot(Y, proj_vecsLO)
        data_dict['Y_projected'] = Y

    #Create line plot for variance explained by each component.
    layer_obj = variance.get_layer(layer)
    layer_obj.add_data(range(1, len(p.d)+1), p.d/sum(p.d))
    layer_obj.set_style(style + "-o")

    #Create plot of high-dimensional data and its PC's.
    layer_obj = before.get_layer(layer)
    layer_obj.add_data(X[:,0], X[:,1], X[:,2])
    layer_obj.set_style(style + '.')

    for j in range(0, len(pcPts), 2):
        before.add_layer(layer + " PC")
        layer_obj = before.get_layer(layer + " PC")
        layer_obj.add_data(pcPts[j+0:j+2,0], pcPts[j+0:j+2,1], pcPts[j+0:j+2,2])
        layer_obj.add_data(pcPts[j+2:j+4,0], pcPts[j+2:j+4,1], pcPts[j+2:j+4,2])
        layer_obj.set_style(style)

    #Create plot of low-dimensional data.

    layer_obj = after.get_layer(layer)
    layer_obj.add_data(Y[:,0], Y[:,1])
    layer_obj.set_style(style + ".")

    print("Variance Explained in", layer,"with first",new_dim,"components:")
    print(sum(p.d[0:new_dim])/sum(p.d))

    before.build_layers()
    after.build_layers()
    variance.build_layers()

    return data_dict

def setupDisplay(clus_layers, clus_styles, DOI):
    fig = plt.figure(figsize=(14,6))
    fig.suptitle('PCA Disc')
    fig.canvas.set_window_title('Master')

    before = plt.subplot('131', projection='Fovea3D')
    before.set_title('Before')
    after = plt.subplot('132', projection='Fovea2D')
    after.set_title('After')
    variance = plt.subplot('133', projection='Fovea2D')
    variance.set_title('Variance by Components')
    subplots = [before, after, variance]

    #Setup all layers
    before.add_layer('orig_data')
    before.add_layer('meta_data', kind='text')

    for clus in clus_layers:
        before.add_layer(clus)
        after.add_layer(clus)
        variance.add_layer(clus)

    for subplot in subplots:
        subplot.build_layers()

    return fig, subplots

proj_thresh = 20

class ControlSys(object):
    def __init__(self, fig, data, clus_layers, clus_styles, d, before, after, variance, proj_vecsLO=None, proj_vecsHI=None):
        self.fig = fig
        self.before = before
        self.after = after
        self.variance = variance
        self.data = data
        self.data_dict = None
        self.proj_thresh = 4
        self.clus_layers = clus_layers
        self.clus_styles = clus_styles
        self.proj_vecsLO = proj_vecsLO
        self.proj_vecsHI = proj_vecsHI
        self.d = d
        self.c = 0
        self.m = False
        self.fig.canvas.mpl_connect('key_press_event', self.keypress)

        for i in range(len(clus_layers)):
            self.data_dict = compute(data[i], d, clus_layers[i], clus_styles[i], self.before, self.after, self.variance, proj_vecsLO, proj_vecsHI)

        self.highlight_eigens(self.variance)

        #Initialize Bombardier callbacks on 2D subplot.
        #gui.current_domain_handler.assign_criterion_func(self.get_projection_distance)
        #gui.assign_user_func(self.get_displacements)

        #User tips
        print("Press left or right arrow keys to view different rotations of Hi-D data and their PC's.")
        print("Press m to display or hide all layers.")
        print("Press h to show or hide original data.")

    def highlight_eigens(self, variance):
        for i in range(len(self.clus_layers)):
            for j in range(1, self.d+1):
                layer_obj = variance.get_layer(self.clus_layers[i])
                layer_obj.add_vline(j)

    def get_projection_distance(self, pt_array, fsign=None):
        #Judge by nearest point rather than points inside. Otherwise, I would need to access the whole polygon,
        #which would be difficult.
        nearest_pt = []
        index = []

        try:
            pts = self.data_dict['Y_projected']
        except KeyError:
            pts = self.data_dict['Y']

        #Find closest projected point.
        dist = 10000000
        for i, row in enumerate(pts):
            if np.linalg.norm(pt_array - row) < dist:
                dist = np.linalg.norm(pt_array - row)
                nearest_pt = row
                index = i

        #Need more formal way to do this. Standard deviation? This doesn't seem to accomplish anything right now anyways.
        if dist > 2 and fsign is not None:
            return -fsign

        #Calculate distance between projected point and loD point.
        orig_pt = self.data_dict['Y'][i]
        proj_pt = np.append(nearest_pt, np.zeros(len(orig_pt)-len(nearest_pt)))
        if np.linalg.norm(orig_pt - proj_pt) > self.proj_thresh:
            return -1
        else:
            return 1

    def keypress(self, event):

        if event.key == 'left' or event.key == 'right':
            if event.key == 'left':
                self.c += 1
            else:
                self.c -= 1

            layer_obj = self.before.get_layer(self.clus_layers[self.c%len(self.clus_layers)])
            layer_obj.toggle_display()

        if event.key == 'm':
            self.m = not self.m
            for clus in self.clus_layers:
                layer_obj = self.before.get_layer(clus)
                if self.m:
                    layer_obj.show()
                else:
                    layer_obj.hide()

        if event.key == 'h':
            layer_obj = self.before.get_layer('orig_data')
            layer_obj.toggle_display()

        if self.proj_vecsHI is not None:
            if (event.key == 'up' and self.d is not len(self.proj_vecsHI)) or (event.key == 'down' and self.d is not 2):
                if event.key == 'up':
                    self.d += 1
                elif event.key == 'down':
                    self.d -= 1

                print("Attempting to display", self.d,"-dimensional data...")

                for i in range(len(self.clus_layers)):
                    self.data_dict = compute(self.data[i], self.d, self.clus_layers[i], self.clus_styles[i], self.before, self.after, self.variance, self.proj_vecsLO, self.proj_vecsHI)

                self.highlight_eigens(self.variance)

    def get_displacements(self, x, y):
        print("Last output = (mag dict, vector dict)")
        Fxs = []
        Fys = []
        Fs = []

        try:
            pts = self.data_dict['Y_projected']
        except KeyError:
            pts = self.data_dict['Y']

        ixs = range(len(pts))
        for i in ixs:
            Fx = x - pts[i][0]
            Fy = y - pts[i][1]
            Fxs.append(Fx)
            Fys.append(Fy)
            Fs.append(sqrt(Fx*Fx+Fy*Fy))
        return dict(zip(ixs, Fs)), dict(zip(ixs, zip(Fxs, Fys)))


def d(n):
    sum = 0
    for number in range(1,n):
        if n % number == 0:
            sum += number
    return sum

def areAmicable(a,b):
    return d(a) == b and d(b) == a and a != b

def main():
    amicables = []
    res = 0
    n = 10001
    for x in range(n):
        for y in range(x,n):
            if areAmicable(x,y):
                if (not x in amicables and not y in amicables):
                    amicables.append(x)
                    amicables.append(y)
                    res += x + y
                    print(f'Amicable [{x},{y}]')
    print(f'All amicables: {amicables}\nSum of all the amicable numbers under {n} : {res}')
if __name__ == "__main__":
    main()


import pandas as pd
import numpy as np
import random
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier, RandomForestClassifier, GradientBoostingClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split

random.seed(42)

path = './_data/project/'
save_path = '/_save/project/'

# Load Data
dt_eng = pd.read_csv(path + 'spam_ham_dataset.csv')
dt_kor = pd.read_csv(path + 'kor_spam_ham_dataset3.csv')

# Preprocess the data
dt_eng.drop('Unnamed: 0', axis=1, inplace= True)
dt_eng.columns = ['label', 'text', 'class']
dt_eng.head()
#Eng_Text processing
#Remove stopwords from the data
stopwords = set(stopwords.words('english'))
dt_eng['text'] = dt_eng['text'].apply(lambda x: ' '.join([ word for word in word_tokenize(x)
                                                          if not word in stopwords]))

# 먼저 train 데이터와 test 데이터 인덱스 없이 배열로 만들기
kor_x = np.array([x for x in dt_kor['text']])
# print(kor_x)
kor_y = dt_kor.loc[:,'class']

eng_x = dt_eng.loc[:,'text']
eng_y = dt_eng.loc[:,'class']

y=np.array(dt_kor['class'])
_, y_indices = np.unique(y, return_inverse=True)
print(np.bincount(y_indices))
# Split the Korean data into training and testing sets
X_korean_train, X_korean_test, y_korean_train, y_korean_test = train_test_split(
    dt_kor['text'], y, test_size=0.3, random_state=42,stratify=y)

# Split the English data into training and testing sets
X_english_train, X_english_test, y_english_train, y_english_test = train_test_split(
    dt_eng['text'], dt_eng['class'], test_size=0.3, random_state=42,stratify=dt_eng['class'])

# Feature extraction
vectorizer = CountVectorizer()
# vectorizer = TfidfVectorizer()
X_korean_train_features = vectorizer.fit_transform(X_korean_train).toarray()
print(X_korean_train_features)
X_korean_test_features = vectorizer.transform(X_korean_test).toarray()
X_english_train_features = vectorizer.fit_transform(X_english_train).toarray()
X_english_test_features = vectorizer.transform(X_english_test).toarray()
print(X_korean_train_features.shape[0], X_korean_train_features.shape[1])
print(X_english_train_features.shape[0], X_english_train_features.shape[1])

# 변환된 시퀀스 번호를 이용해 단어 임베딩 벡터 생성
max_length = 230
padding_type='pre'
train_korx = pad_sequences(X_korean_train_features, padding='pre', maxlen=max_length)
test_korx = pad_sequences(X_korean_test_features, padding=padding_type, maxlen=max_length)

print(train_korx.shape, test_korx.shape) #(90, 1000) (39, 1000)
# print(train_korx)

train_engx = pad_sequences(X_english_train_features, padding='pre', maxlen=max_length)
test_engx = pad_sequences(X_english_test_features, padding=padding_type, maxlen=max_length)
print(train_engx.shape, test_engx.shape) #(3619, 1000) (1552, 1000)

# Korean model1
korean_clf = LogisticRegression(random_state=42).fit(train_korx, y_korean_train)

# English model2
english_clf = LogisticRegression(random_state=42).fit(train_engx, y_english_train)

# classifier with the two models
E_clf = RandomForestClassifier()
# E_clf = GradientBoostingClassifier()

# Fit the voting classifier on the training data
E_clf.fit(train_korx, y_korean_train)

# korean_clf.summary() # 머신러닝 모델은  summary 제공x
#훈련된 모델의 요약을 찾고 있다면 모델 coef_의 속성/ 훈련된 모델의 요약을 찾고 있다면 모델 coef_의 속성
print("Korean Model Coefficients:")
print(korean_clf.coef_)

print("English Model Coefficients:")
print(english_clf.coef_)
#훈련된 모델에서 각 기능의 중요도
print("Feature Importances of the Random Forest Classifier:")
print(E_clf.feature_importances_)

#total params전체 매개변수
#총 매개변수 수는 입력 데이터의 기능 수에 따라 결정됩니다.
# 특히 매개변수의 수는 특성 수에 1(편향 항)을 더한 것과 같습니다./ 각 입력 데이터 세트의 기능 수가 인쇄
print("Number of features in train_korx:", train_korx.shape[1])
print("Number of features in train_engx:", train_engx.shape[1])
#그런 다음 각 로지스틱 회귀 모델에 대한 총 매개변수 수를 계산하기 위해 기능 수에 1을 더할 수 있습니다.
kor_params = train_korx.shape[1] + 1
eng_params = train_engx.shape[1] + 1

print("Total parameters in Korean logistic regression model:", kor_params)
print("Total parameters in English logistic regression model:", eng_params)

#랜덤 포레스트 및 그래디언트 부스팅 분류기의 경우 매개변수의 총 수는 트리 수 또는 추정기 수와 같이 모델에 대해 선택한 하이퍼 매개변수에 따라 달라집니다.
#이렇게 하면 모델의 트리 수와 입력 데이터의 기능 수를 기반으로 랜덤 포레스트 분류기의 총 매개변수 수가 인쇄
print("Total parameters in Random Forest Classifier:", E_clf.n_estimators * (train_korx.shape[1] + 1))
#결정트리의 개수 : n_estimators

# Test the voting classifier on the testing data
y_pred = E_clf.predict(test_korx)

#lr
# Evaluate the performance of the model
accuracy = accuracy_score(y_korean_test, y_pred)
precision = precision_score(y_korean_test, y_pred)
recall = recall_score(y_korean_test, y_pred)
f1 = f1_score(y_korean_test, y_pred)

print('Accuracy:', accuracy)
print('Precision:', precision)
print('Recall:', recall)
print('F1-score:', f1)

'''
#data2
vectorizer = TfidfVectorizer()
Accuracy: 0.7692307692307693
Precision: 0.7692307692307693
Recall: 1.0
F1-score: 0.8695652173913044
'''

'''
#data3
vectorizer = TfidfVectorizer()
#max_len : 1000(임의값)
Accuracy: 0.9174311926605505
Precision: 0.9174311926605505
Recall: 1.0
F1-score: 0.9569377990430622

#max_len : 230 / 8862
Accuracy: 0.9174311926605505
Precision: 0.9174311926605505
Recall: 1.0
F1-score: 0.9569377990430622
======================================
CountVectorizer()
#max_len :  8862
Accuracy: 0.9357798165137615
Precision: 0.9345794392523364
Recall: 1.0
F1-score: 0.966183574879227

#max_len : 230
Accuracy: 0.9174311926605505
Precision: 0.9174311926605505
Recall: 1.0
F1-score: 0.9569377990430622
'''

'''
#E_clf = GradientBoostingClassifier()
Accuracy: 0.926605504587156
Precision: 0.9259259259259259
Recall: 1.0
F1-score: 0.9615384615384615
'''

# # Predict the label of a new email in English
# new_email_english = ['Buy cheap Viagra now!']
# new_email_english_features = vectorizer.transform(new_email_english)
# new_email_english_pred = voting_clf.predict(new_email_english_features)
# print('Prediction:', new_email_english_pred)

# #[실습]# 긍정인지 부정인지 맞추기
# x_predict = ['나는 성호가 정말 재미없다 너무 정말']
# token.fit_on_texts(x_predict)
# x_predict = token.texts_to_sequences(x_predict)
# x_predict = np.array(x_predict)
# x_predict = x_predict.reshape(-1,6,1)
# predict = model.predict([x_predict])
# print("긍정/부정", predict)

##############################
# #Tokenizer
# vocab_size = 2000
# tokenizer = Tokenizer(num_words = vocab_size)
# tokenizer.fit_on_texts(X_korean_train)
# X_korean_train_features = tokenizer.texts_to_sequences(X_korean_train)
# print(X_korean_train_features)
# print(X_korean_train_features.__class__)
# print(X_korean_train_features[0].__class__)
# X_korean_test_features = tokenizer.texts_to_sequences(X_korean_test)
# print(len(X_korean_train_features[0]), len(X_korean_test_features)) #69 30

# word_index = tokenizer.word_index
# max_length = 50
# padding_type='pre'
# train_korx = pad_sequences(X_korean_train_features, padding='pre', maxlen=max_length)
# test_korx = pad_sequences(X_korean_test_features, padding=padding_type, maxlen=max_length)

# print(train_korx)
# train_korx = test_korx[0]
# print(train_korx)


# Creates a pickled file to read vectorized sentence data for "distance_assessment.py"
# Input: "./data/tasks/sentence_correction/task_data.csv"

from sent2vec.vectorizer import Vectorizer
import pandas as pd
import time

# setup
sentence_data = pd.read_csv("./data/tasks/sentence_correction/task_data.csv")
whole_sentences = []

if __debug__:
    print(sentence_data.columns)
    start_time = time.time()

# each "row" contains its "values" as list item
# save corrected sentences to "whole_sentences"
for row, values in sentence_data.iterrows():
    whole_sentences.append(values[2].format(values[3].strip("{}")))

sentence_data["sentence_corpus"] = whole_sentences

# create vectorized items and save them as list
vectorizer = Vectorizer()
vectorizer.bert(sentence_data["sentence_corpus"])
sentence_data["sentence_vectors"] = vectorizer.vectors.tolist()

if __debug__:
    print(sentence_data.index)
    end_time = time.time() - start_time
    print(end_time)

sentence_data.to_pickle("pickled_sentences")


options = [1, 2, 3, 4]

menu = '''Please select an option:

1. Option 1
2. Option 2
3. Option 3
4. Option 4
5. Exit Menu'''

choice = -1

while choice not in options:
    choice = int(input("What do you want to do? "))

    if choice == 5:
        print("Exited Menu")
        break
    elif choice in options:
        print(choice)
    else:
        print("Invalid Input")
        print()
        print(menu)


from django.conf.urls import url
from django.contrib import admin
from upload_app import views
from django.conf import settings
from django.conf.urls.static import static

app_name = 'upload_app'

urlpatterns = [
	url(r'^$', views.index, name='index'),
	url(r'^intro/', views.intro, name='intro'),
	url(r'^register/', views.register, name='register'),
	url(r'^login/', views.user_login, name='user_login'),
	url(r'^logout/', views.user_logout, name='user_logout'),
	url(r'^special/', views.special, name='special'),
	url(r'^upload/', views.upload, name='upload'),
	url(r'^iris gallery/', views.iris_gallery, name='iris_gallery'),
	url(r'^upload_face/', views.upload_face, name='upload_face'),
	url(r'^face_gallery/', views.face_gallery, name='face_gallery'),
    # url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


# -*- coding: utf-8 -*-
# @Time    : 18-7-3 上午10:06
# @Author  : zhoujun
import pathlib
import tqdm
import shutil
import os

def check():
    img_path = '/data1/zj/data/add_bg_img'
    img_path = pathlib.Path(img_path)

    img_list = list(img_path.rglob('*.jpg'))
    print('start')
    pbar = tqdm.tqdm(total=len(img_list))
    for img in img_list:
        npy_path = pathlib.Path(str(img) + '.npy')
        if not img.exists() or img.stat().st_size <=0 or not npy_path.exists() or npy_path.stat().st_size <=0:
            if img.exists():
                os.remove(str(img))
            if npy_path.exists():
                os.remove(str(npy_path))
            print('img or npy is bad:{0},{1}'.format(img,npy_path))
        pbar.update(1)
    pbar.close()

def main():
    check()

if __name__ == '__main__':

    main()


def channel_shift_multi(x, intensity, is_random=False, channel_index=2):
    """Shift the channels of images with the same arguments, randomly or non-randomly, see `numpy.rollaxis <https://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html>`__.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.channel_shift``.

    Returns
    -------
    numpy.array
        A list of processed images.

    """
    if is_random:
        factor = np.random.uniform(-intensity, intensity)
    else:
        factor = intensity

    results = []
    for data in x:
        data = np.rollaxis(data, channel_index, 0)
        min_x, max_x = np.min(data), np.max(data)
        channel_images = [np.clip(x_channel + factor, min_x, max_x) for x_channel in x]
        data = np.stack(channel_images, axis=0)
        data = np.rollaxis(x, 0, channel_index + 1)
        results.append(data)
    return np.asarray(results)


from discord.ext import commands
import sympy as sym
from sympy import *
import math

# コグとして用いるクラスを定義。
class MathCog(commands.Cog):

    # TestCogクラスのコンストラクタ。Botを受取り、インスタンス変数として保持。
    def __init__(self, bot):
        self.bot = bot

    # コマンドの作成。コマンドはcommandデコレータで必ず修飾する。
    @commands.command()
    async def circle(self, ctx, radius='0'): # 関数名=コマンド名
        '''半径から円の周と面積を求める''' # helpコマンドに載せる説明を書く
        try:
            radius = float(radius)
        except ValueError:
            await ctx.send('数字を入力して下さい')
        else:
            await ctx.send(f'円周：{2.0 * math.pi * radius}\n面積：{math.pi * radius * radius}')

    @commands.command()
    async def sphere(self, ctx, radius='0'):
        '''半径から球の表面積と体積を求める'''
        try:
            radius = float(radius)
        except ValueError:
            await ctx.send('数字を入力して下さい')
        else:
            await ctx.send(f'表面積：{4.0 * math.pi * radius * radius}\n体積　：{4.0 / 3.0 * math.pi * radius * radius * radius}')

    @commands.command()
    async def diff(self, ctx, y='0', n='1'):
        '''関数をxで微分する'''
        x = sym.symbols('x')
        try:
            y = eval(y)
            n = abs(int(n))
        except:
            await ctx.send('Pythonのフォーマットに対応した変数はxのみの数式を入力してください')
        else:
            y = sym.diff(y, x, n)
            prime = ['\'' for i in range(n)]
            prime = ''.join(prime)
            await ctx.send(f'y{prime} = {y}')

    @commands.command()
    async def intg(self, ctx, y='0', a='0', b='0'):
        '''関数をxで積分する'''
        x = sym.symbols('x')
        try:
            y = eval(y)
            a = int(a)
            b = int(b)
        except:
            await ctx.send('Pythonのフォーマットに対応した変数はxのみの数式を入力してください')
        else:
            if a == b:
                y = sym.integrate(y, x)
                await ctx.send(f'∫ydx = {y}')
            else:
                y = sym.integrate(y, (x, a, b))
                await ctx.send(f'∫[{a}:{b}]ydx = {y}')

# Bot本体側からコグを読み込む際に呼び出される関数。
def setup(bot):
    bot.add_cog(MathCog(bot))


from engine.environment.cell_package.cell import Cell
from engine.environment.part_package.part import FieldPart
from engine.ship_package.ship import Ship

DELTA = 20

class Field(object):
    def __init__(self, size):
        self.size = size
        self.map = [[Cell.empty_cell for _ in range(size)] for _ in range(size)]
        self.radar = [[Cell.empty_cell for _ in range(size)] for _ in range(size)]
        self.weight = [[1 for _ in range(size)] for _ in range(size)]

    def get_field_part(self, element):
        if element == FieldPart.main:
            return self.map
        if element == FieldPart.radar:
            return self.radar
        if element == FieldPart.weight:
            return self.weight

    def draw_field(self, element):
        field = self.get_field_part(element)
        weights = self.get_max_weights()

        if element == FieldPart.weight:
            for x in range(self.size):
                for y in range(self.size):
                    if (x, y) in weights:
                        print('\033[1;32m', end='')
                    if field[x][y] < self.size:
                        print(" ", end='')
                    if field[x][y] == 0:
                        print(str("" + ". " + ""), end='')
                    else:
                        print(str("" + str(field[x][y]) + " "), end='')
                    print('\033[0;0m', end='')
                print()

        else:
            for x in range(-1, self.size):
                for y in range(-1, self.size):
                    if x == -1 and y == -1:
                        print("  ", end="")
                        continue
                    if x == -1 and y >= 0:
                        print(y + 1, end=" ")
                        continue
                    if x >= 0 and y == -1:
                        from engine.game_package.game import Game
                        print(Game.letters[x], end='')
                        continue
                    print(" " + str(field[x][y]), end='')
                print("")
        print("")

    def check_ship_fits(self, ship, element):
        field = self.get_field_part(element)

        if ship.x + ship.height - 1 >= self.size or ship.x < 0 or \
                ship.y + ship.width - 1 >= self.size or ship.y < 0:
            return False

        x = ship.x
        y = ship.y
        width = ship.width
        height = ship.height

        for p_x in range(x, x + height):
            for p_y in range(y, y + width):
                if str(field[p_x][p_y]) == Cell.miss_cell:
                    return False

        for p_x in range(x - 1, x + height + 1):
            for p_y in range(y - 1, y + width + 1):
                if p_x < 0 or p_x >= len(field) or p_y < 0 or p_y >= len(field):
                    continue
                if str(field[p_x][p_y]) in (Cell.ship_cell, Cell.destroyed_ship):
                    return False

        return True

    def mark_destroyed_ship(self, ship, element):
        field = self.get_field_part(element)

        x, y = ship.x, ship.y
        width, height = ship.width, ship.height

        for p_x in range(x - 1, x + height + 1):
            for p_y in range(y - 1, y + width + 1):
                if p_x < 0 or p_x >= len(field) or p_y < 0 or p_y >= len(field):
                    continue
                field[p_x][p_y] = Cell.miss_cell

        for p_x in range(x, x + height):
            for p_y in range(y, y + width):
                field[p_x][p_y] = Cell.destroyed_ship

    def add_ship_to_field(self, ship, element):
        field = self.get_field_part(element)

        x, y = ship.x, ship.y
        width, height = ship.width, ship.height

        for p_x in range(x, x + height):
            for p_y in range(y, y + width):
                field[p_x][p_y] = ship

    def get_max_weights(self):
        weights = {}
        max_weight = 0
        for x in range(self.size):
            for y in range(self.size):
                if self.weight[x][y] > max_weight:
                    max_weight = self.weight[x][y]
                weights.setdefault(self.weight[x][y], []).append((x, y))

        return weights[max_weight]

    def recalculate_weights(self, available_ships):
        self.weight = [[1 for _ in range(self.size)] for _ in range(self.size)]

        for x in range(self.size):
            for y in range(self.size):
                if self.radar[x][y] == Cell.damaged_ship:
                    self.weight[x][y] = 0

                    if x - 1 >= 0:
                        if y - 1 >= 0:
                            self.weight[x - 1][y - 1] = 0
                        self.weight[x - 1][y] *= DELTA
                        if y + 1 < self.size:
                            self.weight[x - 1][y + 1] = 0

                    if y - 1 >= 0:
                        self.weight[x][y - 1] *= DELTA
                    if y + 1 < self.size:
                        self.weight[x][y + 1] *= DELTA

                    if x + 1 < self.size:
                        if y - 1 >= 0:
                            self.weight[x + 1][y - 1] = 0
                        self.weight[x + 1][y] *= DELTA
                        if y + 1 < self.size:
                            self.weight[x + 1][y + 1] = 0

        for ship_size in available_ships:
            ship = Ship(ship_size, 1, 1, 0)
            for x in range(self.size):
                for y in range(self.size):
                    if self.radar[x][y] in (Cell.destroyed_ship, Cell.damaged_ship, Cell.miss_cell) \
                            or self.weight[x][y] == 0:
                        self.weight[x][y] = 0
                        continue
                    for rotation in range(0, 4):
                        ship.set_position(x, y, rotation)
                        if self.check_ship_fits(ship, FieldPart.radar):
                            self.weight[x][y] += 1