import os
import sys
import time

import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as functional
import torch.optim as optim

from datautils import PreTrainedResnet, Pixel

from dataloader import get_train_data

import torchvision
import torchvision.models as models

class CNN(nn.Module):

	def __init__(self):
		super(CNN, self).__init__()

		self.pretrained_model = models.resnet18(pretrained=True)
		if torch.cuda.is_available():
			self.pretrained_model = self.pretrained_model.cuda()
		for param in self.pretrained_model.parameters():
			param.requires_grad = True

		self.linear = nn.Linear(1000, 500)

	def forward(self, x):
		for name, module in self.pretrained_model._modules.items():
			if name=='fc':
				x = x.squeeze().contiguous()
			x = module(x)
			if name=='fc':
				lastlayer = x

		features = self.linear(lastlayer)
		return features

class SiemeseCNN(nn.Module):

	def __init__(self):
		super(SiemeseCNN, self).__init__()
		self.cnn = CNN()
		self.linear1 = nn.Linear(1000, 500)
		self.linear2 = nn.Linear(500, 2)

	def forward(self, x1, x2):
		feat1 = self.cnn(x1)
		feat2 = self.cnn(x2)
		return self.linear2(functional.ReLU(self.linear1(torch.cat((feat1, feat2), dim=-1))))

def train_cnn():

	modeltype = 'cnn'
	train_batch_size = 2
	sequence_length = 1
	directory_type = 'DPM'

	pixel = Pixel('pixel.pkl')

	train_dataloader, train_data_size, val_dataloader, val_data_size = \
		get_train_data(train_batch_size, sequence_length, directory_type, pixel)

	cnnmodel = CNN()
	if torch.cuda.is_available(): cnnmodel = cnnmodel.cuda()

	for i, batch in enumerate(train_dataloader):
		frame1 = Variable(torch.stack(batch[0])).squeeze(1)
		frame2 = Variable(torch.stack(batch[1]))
		labels = Variable(torch.LongTensor(batch[2]))

		print(frame1.size())
		print(frame2.size())
		print(labels)

		print(cnnmodel(frame1).size())
		break

if __name__=='__main__':
	train_cnn()


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

# Data Analysis where i import the libraries and the datas

# In[98]:

import pandas as pd
import numpy as np
import plotly.express as px
import requests
import io
from datetime import date

# In[6]:

url = 'https://raw.githubusercontent.com/Marcellopp/Desarollo-de-applicaciones-mobiles/main/healthy_lifestyle_city_2021.csv?token=GHSAT0AAAAAABZYTLOLF5NFJYHJFPDWOMTUY3UWNFA'
download = requests.get(url).content

# Here i create a dataframe with the data captured

# In[78]:

df = pd.read_csv(io.StringIO(download.decode('utf-8')))
df.head()

# In[ ]:

# In[ ]:

# It's know that the more sunshine has a city the happier it is. So what is the Happiest city?

# In[70]:

df.iloc[df['Sunshine hours(City)'].argsort()[::-1][:1]]

# But is it true? which is the happiest city based on the data present in the database?

# In[82]:

df.iloc[df['Happiness levels(Country)'].argsort()[::-1][:1]]

# No, those are not the same cities, CAIRO has a Happiness value of 4.15 compared to HELSINKI that has a 7.8

# In[ ]:

# In[ ]:

# What are the 3 bigger cities based on number of takeouts

# In[68]:

df.iloc[df['Number of take out places(City)'].argsort()[::-1][:3]]

# I need to convert the type of the data to be able to use those in an eventual comparison so i also remove symbols

# In[79]:

df['Cost of a bottle of water(City)'] = df['Cost of a bottle of water(City)'].str.lstrip('£')
df['Obesity levels(Country)'] = df['Obesity levels(Country)'].str.rstrip('%')
df['Cost of a monthly gym membership(City)'] = df['Cost of a monthly gym membership(City)'].str.lstrip('£')

# In[80]:

df['Sunshine hours(City)'] = pd.to_numeric(df['Sunshine hours(City)'], errors = 'coerce')
df['Cost of a bottle of water(City)'] = pd.to_numeric(df['Cost of a bottle of water(City)'], errors = 'coerce')
df['Obesity levels(Country)'] = pd.to_numeric(df['Obesity levels(Country)'], errors = 'coerce')
df['Pollution(Index score) (City)'] = pd.to_numeric(df['Pollution(Index score) (City)'], errors = 'coerce')
df['Annual avg. hours worked'] = pd.to_numeric(df['Annual avg. hours worked'], errors = 'coerce')
df['Cost of a monthly gym membership(City)'] = pd.to_numeric(df['Cost of a monthly gym membership(City)'], errors = 'coerce')
print(df.dtypes)

# What city is more Healhy? Comparison between Pollution and Cost of gym membership

# In[83]:

df_new = df[['City','Pollution(Index score) (City)', 'Cost of a monthly gym membership(City)']]
df_new

# In[89]:

df_new.sort_values('Pollution(Index score) (City)')

# In[90]:

df_new.sort_values('Cost of a monthly gym membership(City)')

# We can see that the aren't strong relationships between the pollution and the cost of a gym membership also
# because this last one depends a lot on the economy of the city itself but we can notice that the most polluted
# countriees are also beneth the poorest and so the cost of gym membership is noticable lower...

# In[ ]:

# In[ ]:

# Relation between obesity and Life expectancy(years).

#

# In[95]:

df_nonan = df.dropna()

# In[101]:

df_nonan.sort_values('Obesity levels(Country)')
fig = px.pie(df_nonan, values='Obesity levels(Country)', names='City', title='Obesity percentage in cities')
fig.show()

# In[ ]:

# In[103]:

df_nonan.sort_values('Life expectancy(years) (Country)')
fig = px.pie(df_nonan, values='Life expectancy(years) (Country)', names='City', title='Life expectancy(years) (Country)')
fig.show()

# In[115]:

df_ob=df_nonan.iloc[df_nonan['Obesity levels(Country)'].argsort()[:10]]
df_ob

# In[ ]:

# In[113]:

df_life=df_nonan.iloc[df_nonan['Life expectancy(years) (Country)'].argsort()[::-1][:10]]
df_life

# We notice that there is a clear relation between the obesity level anda the life expectancy, for example we can see how 5 out of 10 of the cities with lover % of obesity are present in the top 10 cities with the higher life expectancy.

# In[ ]:

# In[ ]:

# In[116]:

df_sport = df[['City','Outdoor activities(City)', 'Cost of a monthly gym membership(City)']]
df_sport

# In[117]:

df_sport.sort_values('Cost of a monthly gym membership(City)')


import math
import glob
import os.path
import json
#Consumer Amount and Factory Amount
print("=== INPUT SELECTION ===")
print("1 for population")
print("2 for umits")
print("3 for most recent country file")
inputtype = input("Selection: ")

# Determine value input method

# Calculate value from population
if inputtype == "1":
    population = int(input("Input population (to nearest million)"))
    consumeamt = population

# Use manual value from user
elif inputtype == "2":
    consumeamt = float(input("How many Consumer Goods do you need?"))
    population = consumeamt

# Use dictionary element from other python scraper
elif inputtype == "3":
    # Find Nation JSON File
    folder_path = r'C:\Users\Cameron Labes\Documents\Programming\fandom_scraper-master\data'
    file_type = r'\*json'
    files = glob.glob(folder_path + file_type)
    natfile = max(files, key=os.path.getctime)
    print(natfile)

    # Extract Dictionary from JSON file
    with open(natfile, "r") as file:
        natdata = json.load(file)
    print(natdata)
    consumeamt = natdata['Consumers Required to Upkeep']

    # Conversion for calculation
    consumeamt = float(consumeamt)
    population = consumeamt

# Round variable to account for decimals and add overhead
if consumeamt < 40:
 consumeamt = 40
consumefac = float(round(consumeamt / 40))

#Ingrediant Amount and Factory Amount
fertamt = consumefac * 2.5
motoramt = consumefac * 2.5
elecamt = consumefac * 3
fertfac = math.ceil(fertamt / 12)
motorfac = math.ceil(motoramt / 6)
elecfac = math.ceil(elecamt / 16)
steelamt = motorfac
steelfac = math.ceil(steelamt / 3)

#Resource Amount
phosphateamt = math.ceil(fertfac * 3.5)
titaniumamt = math.ceil(steelfac * 0.2)
ironamt = math.ceil(steelfac * 4)
tugstenamt = math.ceil(motorfac * 2)
goldamt = math.ceil(elecfac * 2)
copperamt = math.ceil(elecfac * 2)

# Print result
print("For %d Million Population, you will need %d Consumer Goods which requires:" % (population, population))
print("=====================Factories=====================")
print("%d consumer factories." % (consumefac))
print("%d ferterlizer factories." % (fertfac))
print("%d motor factories." % (motorfac))
print("%d steel factories." % (steelfac))
print("%d electronic factories." % (elecfac))
print("===================Raw Resources===================")
print("%d units of phosphate." % (phosphateamt))
print("%d units of titanium." % (titaniumamt))
print("%d units of iron." % (ironamt))
print("%d units of tugsten." % (tugstenamt))
print("%d units of gold." % (goldamt))
print("%d units of copper." % (copperamt))
input('Press ENTER to exit')


#!/usr/bin/env python

import os
import setuptools

VERSION = '0.0.1.dev100'

INSTALL_REQUIRES = [
    'scikit-learn',
    ]

TEST_REQUIRES = [
    'coverage',
    ]

setuptools.setup(
    name = 'sklc',
    version = VERSION,
    description = 'sklearn for error correction',
    packages = ['sklc'],
    install_requires = INSTALL_REQUIRES,
    #extras_require = {
    #    'tests': TEST_REQUIRES,
    #    },
    #test_suite = 'tests',
    #test_loader = 'unittest:TestLoader'
    )


import unittest
import sqlite3
import os
import time
from database import Database

class DataaseCase(unittest.TestCase):
    filename = 'test_datebase.db'

    def setUp(self):
        try:
            os.remove(self.filename)
        except:
            pass
        db = Database(self.filename, new=True)

    def tearDown(self):
        os.remove(self.filename)

    def test_create(self):
        con = sqlite3.connect(self.filename)
        cur = con.execute("SELECT * from Word;")
        col_names = [description[0] for description in cur.description]
        self.assertEqual(col_names, ['word', 'rank', 'known_earlier', 'known_now', 'mod_time'])

        rows = cur.fetchall()
        self.assertEqual(rows, [])

    def test_modify_word(self):
        con = sqlite3.connect(self.filename)

        cur = con.execute("SELECT * from Word;")
        rows = cur.fetchall()
        self.assertEqual(rows, [])

        db = Database(self.filename)
        db.modify_word('x', 0, True, False)
        cur = con.execute("SELECT * from Word;")
        rows = [(a, b, c, d) for (a, b, c, d, _) in cur.fetchall()]
        self.assertEqual(rows, [('x', 0, True, False)])

        cur = con.execute("SELECT mod_time from Word WHERE word = 'x';")
        mod_time1 = cur.fetchall()[0][0]

        time.sleep(1)

        db.modify_word('x', 1, True, True)
        cur = con.execute("SELECT * from Word;")
        rows = [(a, b, c, d) for (a, b, c, d, _) in cur.fetchall()]
        self.assertEqual(rows, [('x', 1, True, True)])

        cur = con.execute("SELECT mod_time from Word WHERE word = 'x';")
        mod_time2 = cur.fetchall()[0][0]

        self.assertNotEqual(mod_time1, mod_time2)

    def test_known_earlier(self):
        db = Database(self.filename)
        db.modify_word('a', 0, known_earlier=True, known_now=False)
        db.modify_word('b', 0, known_earlier=False, known_now=False)
        self.assertEqual(db.known_earlier('a'), True)
        self.assertEqual(db.known_earlier('b'), False)
        self.assertEqual(db.known_earlier('c'), False)

    def test_known_now(self):
        db = Database(self.filename)
        db.modify_word('k', 0, known_earlier=True, known_now=False)
        db.modify_word('l', 0, known_earlier=False, known_now=True)
        self.assertEqual(db.known_now('k'), False)
        self.assertEqual(db.known_now('l'), True)
        self.assertEqual(db.known_now('m'), False)

    def test_known_words(self):
        db = Database(self.filename)
        db.modify_word('q', 1000, known_earlier=True, known_now=False)
        db.modify_word('w', 1001, known_earlier=False, known_now=True)
        db.modify_word('e', 1002, known_earlier=True, known_now=False)
        db.modify_word('r', 1003, known_earlier=False, known_now=True)
        db.modify_word('t', 1004, known_earlier=False, known_now=True)
        self.assertEqual(db.known_words(1000, 1000), None)
        self.assertEqual(db.known_words(1005, 1009), None)
        self.assertEqual(db.known_words(1000, 1001), 1/1)
        self.assertEqual(db.known_words(1000, 1002), 1/2)
        self.assertEqual(db.known_words(1000, 1005), 2/5)

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


import requests
from pydojah.endpoints import EndPoints

class PyDojah:

    def __init__(self, app_id, secret_key, sandbox=False):
        self.app_id = app_id
        self.secret_key = secret_key
        self.sandbox = sandbox
        self.endpoint = EndPoints(sandbox=self.sandbox)

    '''Private Methods'''
    # Private method to create headers dictionary
    def __authentication_headers(self):
        headers = {'Authorization': self.secret_key, 'AppId': self.app_id}
        return headers

    # Private method to send a POST request
    def __post_data(self, url, data):
        headers = self.__authentication_headers()
        response = requests.post(url, headers=headers, data=data)
        return response.json()

    # Private method to send a GET request
    def __get_data(self, url, params=None):
        headers = self.__authentication_headers()

        response = requests.get(url, headers=headers, params=params)
        return response.json()

    '''Dojah Wallet Function'''
    # method to get Dojah wallet balance
    def get_balance(self):
        return self.__get_data(self.endpoint.wallet_balance_endpoint())

    '''Crypto Functions'''
    # method to get crypto wallet details
    def crypto_wallet_details(self, wallet_id):

        payload = {
            "wallet_id": wallet_id
        }
        return self.__get_data(self.endpoint.get_crypto_wallet_endpoint(), params=payload)

    # method to create crypto wallet
    def create_crypto_wallet(self, wallet_type):
        data = {"wallet_type": wallet_type}
        return self.__post_data(self.endpoint.crypto_wallet_endpoint(), data)

    # method for crypto transaction details
    def crypto_transaction_detail(self, transaction_id):
        payload = {
            "transaction_id": transaction_id
        }
        return self.__get_data(self.endpoint.transaction_details_endpoint(), params=payload)

    # method for sending Crypto to another Address of the same currency(Tested)
    def send_crypto(self, sender_wallet_id, amount, recipient_address):
        if amount < 0:
            raise ValueError('Amount cannot be a negative value')
        data = {
            "sender_wallet_id": sender_wallet_id,
            "amount": amount,
            "recipient_address": recipient_address
        }
        return self.__post_data(self.endpoint.send_crypto_endpoint(), data)

    def send_crypto_to_your_wallet(self, amount, recipient_wallet_id, sender_wallet_id):

        if amount < 0:
            raise ValueError('Amount cannot be a negative value')
        data = {
            "amount": amount,
            "recipient_wallet_id": recipient_wallet_id,
            "sender_wallet_id": sender_wallet_id
        }
        return self.__post_data(self.endpoint.send_crypto_internal_endpoint(), data)

    '''Data and Airtime functions'''
    # method to buy Airtime
    # To check for negative amount
    def airtime(self, amount, destination):

        if amount < 0:
            raise ValueError('Amount cannot be a negative value')
        data = {
            "amount": amount,
            "destination": destination
        }
        return self.__post_data(self.endpoint.airtime_endpoint(), data)

    # method to buy data
    def data(self, plan, destination):

        data = {
            "plan": plan,
            "destination": destination
        }

        return self.__post_data(self.endpoint.buy_data_endpoint(), data)

    # method to fetch all data plans available
    def data_plan(self):
        return self.__get_data(self.endpoint.data_plans_endpoint())

    '''OTP and Messaging'''

    def send_otp(self, sender_id, destination, channels, priority=True, expiry=None, length=None):
        data = {
            "expiry": expiry,
            "length": length,
            "sender_id": sender_id,
            "destination": destination,
            "channel": channels
        }
        return self.__post_data(self.endpoint.otp_endpoint(), data)

    # method for validating OTP
    def check_otp(self, code, reference_id):
        payload = {
            "code": code,
            "reference_id": reference_id
        }

        return self.__get_data(self.endpoint.validate_otp_endpoint(), payload)

    # method for sending whatsapp message or sms
    def send_sms_or_whatsapp(self, channel, message, destination, sender_id):
        data = {
            "channel": channel,
            "message": message,
            "destination": destination,
            "sender_id": sender_id
        }
        return self.__post_data(self.endpoint.messaging_endpoint(), data)

    # Method to confirm message status
    def get_message_status(self, message_id):
        payload = {
            "message_id": message_id
        }

        return self.__get_data(self.endpoint.message_status_endpoint(), payload)

    # method to request for sender id
    def request_for_sender_id(self, sender_id):
        data = {
            "sender_id": sender_id
        }
        return self.__post_data(self.endpoint.request_sender_id_endpoint(), data)

    # method for fetching the list of all your sender ID
    def get_sender_id(self):
        return self.__get_data(self.endpoint.fetch_sender_ids_endpoint())


from django.core.management.base import BaseCommand
from stores.models import Store
from django.db.utils import IntegrityError
from olmonopolet.vmp_api import stores as vmp_api_stores
import httpx

class Command(BaseCommand):
    help = 'Update Vinmonopolet stores'

    def handle(self, *args, **options):
        # Used for logging
        new_stores = 0

        # Get list of all stores in database
        db_stores = list(Store.objects.values_list('store_id', flat=True))

        # Instantiate httpx Client
        with httpx.Client() as client:
            # Retrieve all Vinmonopolet stores
            all_stores = vmp_api_stores.get_all_stores(client)

        for store in all_stores:

            # Only add the store if it does not exist in the database
            if int(store["storeId"]) in db_stores:
                pass
            else:

                try:
                    new_obj = Store.objects.create(
                        store_id = store["storeId"],
                        name = store["storeName"],
                        category = store["category"],
                        city = store["address"]["city"],
                        street_address = store["address"]["street"],
                        postalcode = store["address"]["postalCode"],
                        latitude = store["address"]["gpsCoord"].split(';')[0],
                        longitude = store["address"]["gpsCoord"].split(';')[1],
                    )

                    new_stores += 1
                except Exception as err:
                    self.stdout.write(f"Could not insert store: {store['storeId']}")

        # Log how many new products were added to the database
        self.stdout.write(f"Completed and inserted {new_stores} new stores.")

        return


import logging
import time

import aioboto3

from .retriable_operations import RetriableKinesisProducer

log = logging.getLogger(__name__.split('.')[-2])

# Following constants are originating from here:
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/kinesis.html#Kinesis.Client.put_records
MAX_RECORDS_IN_BATCH = 500
MAX_RECORD_SIZE = 1024 * 1024           # 1 Mb
MAX_BATCH_SIZE = 5 * MAX_RECORD_SIZE    # 5 Mb

def _get_default_partition_key():
    return '{0}{1}'.format(time.process_time(), time.time())

class AsyncKinesisProducer:

    def __init__(self, stream_name, ordered=True, custom_kinesis_client=None):

        self.stream_name = stream_name
        self.ordered = ordered

        self.seq = '0'

        self.record_buf = []
        self.buf_size = 0

        # Allow a custom kinesis client to be passed in. This allows for setting of any additional parameters in
        # the client without needing to track them in this library.
        if custom_kinesis_client is not None:
            client = custom_kinesis_client
        else:
            client = aioboto3.client('kinesis')

        self.kinesis_client = RetriableKinesisProducer(client=client)
        log.debug("Configured kinesis producer for stream '%s'; ordered=%s",
                  stream_name, ordered)

    async def put_record(self, record, partition_key=None, explicit_hash_key=None):
        """
        Put single record into Kinesis stream
        :param record:              record to put, bytes
        :param partition_key:       partition key to determine shard; if none, time-based key is used
        :param explicit_hash_key:   hash value used to determine the shard explicitly, overriding partition key
        :return:                    response from kinesis client, see boto3 doc
        """

        if partition_key is None:
            partition_key = _get_default_partition_key()

        kwargs = {
            'StreamName': self.stream_name,
            'Data': record,
            'PartitionKey': partition_key,
        }

        if self.ordered:
            kwargs['SequenceNumberForOrdering'] = self.seq

        kwargs['PartitionKey'] = partition_key or _get_default_partition_key()
        if explicit_hash_key:
            kwargs['ExplicitHashKey'] = explicit_hash_key

        resp = await self.kinesis_client.put_record(**kwargs)
        if self.ordered:
            self.seq = resp.get('SequenceNumber')
        return resp

    async def put_records(self, records):
        """
        Put list of records into Kinesis stream
        This call is buffered until it outgrow maximum allowed sizes (500 records or 5 Mb of data including partition
        keys) or until explicitly flushed (see flush() below)

        :param records:             iterable with records to put; has following structure:
                                    records=[
                                        {
                                            'Data': b'bytes',
                                            'ExplicitHashKey': 'string',
                                            'PartitionKey': 'string'
                                        },
                                    ],
                                    If no 'PartitionKey' given, default time-based key will be used
                                    See https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/kinesis.html#Kinesis.Client.put_records
                                    for details
        :return:                    Empty list if no records were flushed, list of responses from kinesis client
                                    otherwise

                                    Raises ValueError if single record exceeds 1 Mb
                                    Currently application should check for ProvisionedThroughputExceededException
                                    in response structure itself.
        """
        resp = []
        n = 1
        for datum in records:

            if len(self.record_buf) == MAX_RECORDS_IN_BATCH:
                resp.append(await self.flush())

            data = datum.get('Data')
            if not (isinstance(data, bytes) or isinstance(data, bytearray)):
                raise TypeError('Record # {} is of type {}; accepted types are "bytes" and "bytearray"'.format(
                    n, type(data)
                ))
            record_size = len(data)

            # boto3 docs say that combined size of record and partition key should not exceed 1 MB,
            # while in reality, at least with boto3==1.9.49 and botocore==1.12.49, the key size
            # is not taken into account
            if record_size > MAX_RECORD_SIZE:
                raise ValueError('Record # {} exceeded max record size of {}; size={}; record={}'.format(
                    n, MAX_RECORD_SIZE, record_size, datum))

            if datum.get('PartitionKey') is None:
                datum['PartitionKey'] = _get_default_partition_key()

            # The same applies to batch size - only record size is counted
            if self.buf_size + record_size > MAX_BATCH_SIZE:
                resp.append(await self.flush())

            self.record_buf.append(datum)
            self.buf_size += record_size
            n += 1

        return resp

    async def flush(self):

        if len(self.record_buf) == 0:
            return

        resp = await self.kinesis_client.put_records(
            Records=self.record_buf,
            StreamName=self.stream_name
        )
        self.record_buf = []
        self.buf_size = 0
        return resp


import sqlite3
import pandas as pd
conn = sqlite3.connect('my_database.sqlite')
cursor = conn.cursor()
sales = pd.read_csv(r"/Users/aravind/Downloads/100 Sales Records.csv")
print(sales)

# cursor.execute('''CREATE TABLE SCHOOL
#          (ID INT PRIMARY KEY     NOT NULL,
#          NAME           TEXT    NOT NULL,
#          AGE            INT     NOT NULL,
#          ADDRESS        CHAR(50),
#          MARKS          INT);''')
#
# cursor.execute("INSERT INTO SCHOOL (ID,NAME,AGE,ADDRESS,MARKS) \
#       VALUES (1, 'Rohan', 14, 'Delhi', 200)");
# cursor.execute("INSERT INTO SCHOOL (ID,NAME,AGE,ADDRESS,MARKS) \
#       VALUES (2, 'Allen', 14, 'Bangalore', 150 )");
# cursor.execute("INSERT INTO SCHOOL (ID,NAME,AGE,ADDRESS,MARKS) \
#       VALUES (3, 'Martha', 15, 'Hyderabad', 200 )");
# cursor.execute("INSERT INTO SCHOOL (ID,NAME,AGE,ADDRESS,MARKS) \
#       VALUES (4, 'Palak', 15, 'Kolkata', 650)");
#
# for row in cursor.execute("SELECT id, name, marks from SCHOOL"):
#     print("ID = ", row[0])
#     print("NAME = ", row[1])
#     print("MARKS = ", row[2], "\n")

print("Opened database successfully")


"""
Shows how to use urllib.request to do GET and POST requests
"""
import json
import sys

from http.client import HTTPResponse
from typing import Dict
from urllib import request

def create_request(url: str, headers: Dict[str, str]) -> request.Request:
    req = request.Request(url)
    for key, value in headers.items():
        req.add_header(key, value)

    return req

def http_get(url: str, headers: Dict[str, str]) -> HTTPResponse:
    req = create_request(url, headers)
    return request.urlopen(req)

def http_post(url: str, headers: Dict[str, str], payload: Dict[str, str]) -> HTTPResponse:
    req = create_request(url, headers)
    json_payload = json.dumps(payload).encode("utf-8")
    return request.urlopen(req, json_payload)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        sys.exit(f"Usage: `urlopen.py GET` or `urlopen.py POST [<key1>=<value>...]`")
    method = sys.argv[1]
    headers = {"User-Agent": "urlopen.py"}
    if method == "GET":
        response = http_get("https://httpbin.org/get", headers)
    elif method == "POST":
        payload = dict(x.split("=", 1) for x in sys.argv[2:])
        headers["Content-Type"] = "application/json"
        response = http_post("https://httpbin.org/post", headers, payload)
    else:
        sys.exit(f"Invalid method {method}")

    print(f"{response.status} {response.reason}")
    print()
    print("Headers:")
    for key, value in response.getheaders():
        print(f"- {key}: {value}")
    print()
    print("Content:")
    content = response.read().decode("utf-8", errors="backslashreplace")
    print(content)


# coding: utf-8

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from nose.tools import assert_equal

import leancloud
from leancloud import operation  # type:ignore

__author__ = "asaka <lan@leancloud.rocks>"

def test_set():  # type: () -> None
    s = operation.Set(10)
    assert s.value == 10

def test_unset():  # type: () -> None
    s = operation.Unset()
    assert s._apply(operation.Set(10)) == operation._UNSET

def test_increment():  # type: () -> None
    s = operation.Increment(1)
    assert s.amount == 1
    previous = operation.Increment(2)
    new = s._merge(previous)
    assert isinstance(new, operation.Increment)
    assert new.amount == 3

def test_apply_relation_op():  # type: () -> None
    album = leancloud.Object.create("Album", objectId="abc001", title="variety")
    band1 = leancloud.Object.create("Band", objectId="abc101", name="xxx")
    band2 = leancloud.Object.create("Band", objectId="abc102", name="ooo")

    relation = album.relation("band")

    op = operation.Relation([band1], [band2])
    val = op._apply(None, album, "band")
    assert isinstance(val, leancloud.Relation)
    val._ensure_parent_and_key(album, "band")

    val = op._apply(relation)
    assert isinstance(val, leancloud.Relation)
    val._ensure_parent_and_key(album, "band")

def test_bit_op():  # type: () -> None
    unset = operation.Unset()

    add_ = operation.BitAnd(123)
    assert_equal(add_._merge(unset).value, 0)

    or_ = operation.BitOr(321)
    assert_equal(or_._merge(unset).value, 321)

    xor = operation.BitOr(321)
    assert_equal(xor._merge(unset).value, 321)


import requests
from lxml import etree

def get_page(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
    }
    res = requests.get(url, headers=headers)
    res.encoding = res.apparent_encoding
    data = res.text
    # print(data)
    tree = etree.HTML(data)
    # text = tree.xpath('//ul[@class="list-con"]/li/span/text()')
    li_list = tree.xpath('//ul[@class="list-con"]/li')
    # print(text)
    for li in li_list:
        print(li.xpath('./span/text()'))

'''
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index.html
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_1.html
http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_2.html
'''
for i in range(3):
    url = f'http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index_{i}.html'
    if i == 0:
        url = 'http://www.cs.ecitic.com/newsite/cpzx/jrcpxxgs/zgcp/index.html'
    get_page(url)


import argparse

parser = argparse.ArgumentParser()
parser.add_argument("infiles", nargs="+", type=argparse.FileType("r"))
parser.add_argument("outfile", type=argparse.FileType("w"))
args = parser.parse_args()

content = "\n".join(f.read() for f in args.infiles)
args.outfile.write(content)


import sys
import logging
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from genotypes import PRIMITIVES, Genotype
from operations import *
from utils import process_step_vector, process_step_matrix, prune
from torch.distributions.dirichlet import Dirichlet
from torch.distributions.kl import kl_divergence

def channel_shuffle(x, groups):
    batchsize, num_channels, height, width = x.data.size()
    channels_per_group = num_channels // groups
    # reshape
    x = x.view(batchsize, groups,
               channels_per_group, height, width)
    x = torch.transpose(x, 1, 2).contiguous()
    # flatten
    x = x.view(batchsize, -1, height, width)
    return x

class MixedOp(nn.Module):

  def __init__(self, C, stride, k):
    super(MixedOp, self).__init__()
    self.k = k
    self.C = C
    self._ops = nn.ModuleList()
    self.mp = nn.MaxPool2d(2,2)

    for primitive in PRIMITIVES:
      op = OPS[primitive](C //self.k, stride, False)
      # if 'pool' in primitive:
      #   op = nn.Sequential(op, nn.BatchNorm2d(C //self.k, affine=False))
      self._ops.append(op)

  def forward(self, x, weights):
    dim_2 = x.shape[1]
    xtemp = x[ : , :  dim_2//self.k, :, :]
    xtemp2 = x[ : ,  dim_2//self.k:, :, :]
    temp1 = sum(w * op(xtemp) for w, op in zip(weights, self._ops) if not w == 0)
    if self.k == 1:
      return temp1
    #reduction cell needs pooling before concat
    if temp1.shape[2] == x.shape[2]:
      ans = torch.cat([temp1,xtemp2],dim=1)
    else:
      ans = torch.cat([temp1,self.mp(xtemp2)], dim=1)
    ans = channel_shuffle(ans,self.k)
    #ans = torch.cat([ans[ : ,  dim_2//4:, :, :],ans[ : , :  dim_2//4, :, :]],dim=1)
    #except channe shuffle, channel shift also works
    return ans

  def wider(self, k):
    self.k = k
    for op in self._ops:
      op.wider(self.C//k, self.C//k)

class Cell(nn.Module):

  def __init__(self, steps, multiplier, C_prev_prev, C_prev, C, reduction, reduction_prev, k):
    super(Cell, self).__init__()
    self.reduction = reduction
    self.k = k

    if reduction_prev:
      self.preprocess0 = FactorizedReduce(C_prev_prev, C, affine=False)
    else:
      self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1, 1, 0, affine=False)
    self.preprocess1 = ReLUConvBN(C_prev, C, 1, 1, 0, affine=False)
    self._steps = steps
    self._multiplier = multiplier

    self._ops = nn.ModuleList()
    self._bns = nn.ModuleList()
    for i in range(self._steps):
      for j in range(2+i):
        stride = 2 if reduction and j < 2 else 1
        op = MixedOp(C, stride, self.k)
        self._ops.append(op)

  def forward(self, s0, s1, weights):
    s0 = self.preprocess0(s0)
    s1 = self.preprocess1(s1)

    states = [s0, s1]
    offset = 0
    for i in range(self._steps):
      s = sum(self._ops[offset+j](h, weights[offset+j]) for j, h in enumerate(states))
      offset += len(states)
      states.append(s)

    return torch.cat(states[-self._multiplier:], dim=1)

  def wider(self, k):
    self.k = k
    for op in self._ops:
      op.wider(k)

class Network(nn.Module):

  def __init__(self, C, num_classes, layers, criterion, steps=4, multiplier=4, stem_multiplier=3, k=4,
               reg_type='l2', reg_scale=1e-3):
    super(Network, self).__init__()
    self._C = C
    self._num_classes = num_classes
    self._layers = layers
    self._criterion = criterion
    self._steps = steps
    self._multiplier = multiplier
    self.k = k

    C_curr = stem_multiplier*C
    self.stem = nn.Sequential(
      nn.Conv2d(3, C_curr, 3, padding=1, bias=False),
      nn.BatchNorm2d(C_curr)
    )

    C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
    self.cells = nn.ModuleList()
    reduction_prev = False
    for i in range(layers):
      if i in [layers//3, 2*layers//3]:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      cell = Cell(steps, multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, k)
      reduction_prev = reduction
      self.cells += [cell]
      C_prev_prev, C_prev = C_prev, multiplier*C_curr

    self.global_pooling = nn.AdaptiveAvgPool2d(1)
    self.classifier = nn.Linear(C_prev, num_classes)

    self._initialize_alphas()

    #### reg
    self.reg_type = reg_type
    self.reg_scale = reg_scale
    self.anchor_normal = Dirichlet(torch.ones_like(self.alphas_normal).cuda())
    self.anchor_reduce = Dirichlet(torch.ones_like(self.alphas_reduce).cuda())

  def new(self):
    model_new = Network(self._C, self._num_classes, self._layers, self._criterion).cuda()
    for x, y in zip(model_new.arch_parameters(), self.arch_parameters()):
        x.data.copy_(y.data)
    return model_new

  def show_arch_parameters(self):
    with torch.no_grad():
      logging.info('alphas normal :\n{:}'.format(process_step_matrix(self.alphas_normal, 'softmax', self.mask_normal).cpu()))
      logging.info('alphas reduce :\n{:}'.format(process_step_matrix(self.alphas_reduce, 'softmax', self.mask_reduce).cpu()))
      logging.info('concentration normal:\n{:}'.format((F.elu(self.alphas_normal) + 1).cpu()))
      logging.info('concentration reduce:\n{:}'.format((F.elu(self.alphas_reduce) + 1).cpu()))

  def pruning(self, num_keep):
    with torch.no_grad():
      self.mask_normal = prune(self.alphas_normal, num_keep, self.mask_normal)
      self.mask_reduce = prune(self.alphas_reduce, num_keep, self.mask_reduce)

  def wider(self, k):
    self.k = k
    for cell in self.cells:
      cell.wider(k)

  def forward(self, input):
    s0 = s1 = self.stem(input)

    weights_normal = process_step_matrix(self.alphas_normal, 'dirichlet', self.mask_normal)
    weights_reduce = process_step_matrix(self.alphas_reduce, 'dirichlet', self.mask_reduce)
    if not self.mask_normal is None:
      assert (weights_normal[~self.mask_normal] == 0.0).all()
    if not self.mask_reduce is None:
      assert (weights_reduce[~self.mask_reduce] == 0.0).all()

    for i, cell in enumerate(self.cells):
      if cell.reduction:
        weights = weights_reduce
      else:
        weights = weights_normal
      s0, s1 = s1, cell(s0, s1, weights)
    out = self.global_pooling(s1)
    logits = self.classifier(out.view(out.size(0),-1))
    return logits

  def _loss(self, input, target):
    logits = self(input)
    loss = self._criterion(logits, target)
    if self.reg_type == 'kl':
      loss += self._get_kl_reg()
    return loss

  def _get_kl_reg(self):
    cons_normal = (F.elu(self.alphas_normal) + 1)
    cons_reduce = (F.elu(self.alphas_reduce) + 1)
    q_normal = Dirichlet(cons_normal)
    q_reduce = Dirichlet(cons_reduce)
    p_normal = self.anchor_normal
    p_reduce = self.anchor_reduce
    kl_reg = self.reg_scale * (torch.sum(kl_divergence(q_reduce, p_reduce)) + \
                               torch.sum(kl_divergence(q_normal, p_normal)))
    return kl_reg

  def _initialize_alphas(self):
    k = sum(1 for i in range(self._steps) for n in range(2+i))
    num_ops = len(PRIMITIVES)

    self.alphas_normal = Variable(1e-3*torch.randn(k, num_ops).cuda(), requires_grad=True)
    self.alphas_reduce = Variable(1e-3*torch.randn(k, num_ops).cuda(), requires_grad=True)
    self._arch_parameters = [
      self.alphas_normal,
      self.alphas_reduce,
    ]
    self.mask_normal = None
    self.mask_reduce = None

  def arch_parameters(self):
    return self._arch_parameters

  def genotype(self):

    def _parse(weights):
      gene = []
      n = 2
      start = 0
      for i in range(self._steps):
        end = start + n
        W = weights[start:end].copy()
        # edges = sorted(range(i + 2), key=lambda x: -max(W[x][k] for k in range(len(W[x])) if k != PRIMITIVES.index('none')))[:2]
        edges = sorted(range(i + 2), key=lambda x: -max(W[x][k] for k in range(len(W[x]))))[:2]
        for j in edges:
          k_best = None
          for k in range(len(W[j])):
            # if k != PRIMITIVES.index('none'):
            if k_best is None or W[j][k] > W[j][k_best]:
              k_best = k
          gene.append((PRIMITIVES[k_best], j))
        start = end
        n += 1
      return gene

    # gene_normal = _parse(F.softmax(self.alphas_normal, dim=-1).data.cpu().numpy())
    # gene_reduce = _parse(F.softmax(self.alphas_reduce, dim=-1).data.cpu().numpy())
    gene_normal = _parse(process_step_matrix(self.alphas_normal, 'softmax',
                                             self.mask_normal).data.cpu().numpy())
    gene_reduce = _parse(process_step_matrix(self.alphas_reduce, 'softmax',
                                             self.mask_reduce).data.cpu().numpy())

    concat = range(2+self._steps-self._multiplier, self._steps+2)
    genotype = Genotype(
      normal=gene_normal, normal_concat=concat,
      reduce=gene_reduce, reduce_concat=concat
    )
    return genotype


import bpy
from bpy.props import *
from .. utils.layout import writeText
from .. base_types import AnimationNodeSocket
from .. preferences import getDeveloperSettings, debuggingIsEnabled

def draw(self, context):
    node = getattr(bpy.context, "active_node", None)
    if not getattr(node, "isAnimationNode", False): return
    layout = self.layout
    layout.separator()
    drawNodeSettings(layout, node)

def drawNodeSettings(layout, node):
    drawSocketLists(layout, node)

    if debuggingIsEnabled():
        layout.separator()
        drawGenericNodeProperties(layout, node)
        layout.label(text = "Identifier: " + node.identifier)

def drawGenericNodeProperties(layout, node):
    layout.prop(node, "width", text = "Width")

    col = layout.column(align = True)
    col.prop(node, "location", text = "X", index = 0)
    col.prop(node, "location", text = "Y", index = 1)

def drawSocketLists(layout, node):
    row = layout.row(align = True)

    size = max(len(node.inputs), len(node.outputs), 1)

    if len(node.inputs) > 0:
        col = row.column()
        subrow = col.row(align = True)
        subrow.label(text = "Inputs")
        subrow.operator("an.move_input", text = "", icon = "TRIA_UP").moveUp = True
        subrow.operator("an.move_input", text = "", icon = "TRIA_DOWN").moveUp = False
        col.template_list("AN_UL_SocketUiList_Normal", "", node, "inputs", node, "activeInputIndex", rows = size, maxrows = size)

    if len(node.outputs) > 0:
        col = row.column()
        subrow = col.row(align = True)
        subrow.label(text = "Outputs")
        subrow.operator("an.move_output", text = "", icon = "TRIA_UP").moveUp = True
        subrow.operator("an.move_output", text = "", icon = "TRIA_DOWN").moveUp = False
        col.template_list("AN_UL_SocketUiList_Normal", "", node, "outputs", node, "activeOutputIndex", rows = size, maxrows = size)

class SocketUiList_Normal(bpy.types.UIList):
    bl_idname = "AN_UL_SocketUiList_Normal"

    def draw_item(self, context, layout, node, socket, icon, activeData, activePropname):
        if not isinstance(socket, AnimationNodeSocket):
            layout.label(text = "No Animation Node Socket")
            return
        if socket.textProps.editable:
            layout.prop(socket, "text", emboss = False, text = "")
        else: layout.label(text = socket.getDisplayedName())

        if socket.removeable:
            socket.invokeFunction(layout, node, "remove", icon = "X", emboss = False)

        icon = "RESTRICT_VIEW_ON" if socket.hide else "RESTRICT_VIEW_OFF"
        layout.prop(socket, "hide", text = "", icon_only = True, icon = icon, emboss = False)

class MoveInputSocket(bpy.types.Operator):
    bl_idname = "an.move_input"
    bl_label = "Move Input"

    moveUp: BoolProperty()

    @classmethod
    def poll(cls, context):
        socket = getActiveSocket(isOutput = False)
        return getattr(socket, "moveable", False)

    def execute(self, context):
        return moveSocket(isOutput = False, moveUp = self.moveUp)

class MoveOutputSocket(bpy.types.Operator):
    bl_idname = "an.move_output"
    bl_label = "Move Output"

    moveUp: BoolProperty()

    @classmethod
    def poll(cls, context):
        socket = getActiveSocket(isOutput = True)
        return getattr(socket, "moveable", False)

    def execute(self, context):
        return moveSocket(isOutput = True, moveUp = self.moveUp)

def moveSocket(isOutput, moveUp):
    socket = getActiveSocket(isOutput)
    socket.moveInGroup(moveUp)

    node = socket.node
    if isOutput: node.activeOutputIndex = list(node.outputs).index(socket)
    else: node.activeInputIndex = list(node.inputs).index(socket)
    return {"FINISHED"}

def getActiveSocket(isOutput):
    node = getattr(bpy.context, "active_node", None)
    if node is None: return
    if isOutput: return node.activeOutputSocket
    else: return node.activeInputSocket

# Register
##################################

def register():
    bpy.types.NODE_PT_active_node_generic.append(draw)

def unregister():
    bpy.types.NODE_PT_active_node_generic.remove(draw)


from pathlib import Path
import numpy as np
import glob

path = Path(r"E:\NN_Research\Example1_Hyperelasticity")
file = path / "E_gl_M_data_and_PK2_M_Data_odb.npz"
arrays = np.load(file)
E_gl_M_data = arrays["E_gl_M_data"]
S_M_data = arrays["PK2__M_Data"]
Eyoung = 200e9
xnu = 0.3
Gshear = Eyoung / (2 * (1 + xnu))
Kbulk = Eyoung / (3 * (1 - 2 * xnu))
lmbda = Kbulk - (2 / 3) * Gshear

file = []
I = np.diag([1, 1])
i = 0
for E_gl, S in zip(E_gl_M_data, S_M_data):
    S = S.reshape(3, 3)
    E_gl = E_gl.reshape(2, 2)
    PK2_stV = lmbda * np.trace(E_gl) * I + 2 * Gshear * E_gl
    check = np.isclose(PK2_stV, S[:2, :2], atol=10 ** 5)

    if check.sum() != 4:
        print("Check failed")
        print("iteration", i)
        print(PK2_stV, S)
    i = i + 1
    # print("Iteration:", i)


"""
Utilities for RL training and eval.

Note: this code is for research i.e. quick experimentation; it has minimal
comments for now, but if we see further interest from the community -- we will
add further comments, unify the style, improve efficiency and add unittests.

@contactrika

"""
import os
import pickle

import cv2
import torch

from stable_baselines3.common.callbacks import BaseCallback
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.logger import Video

from dedo.utils.train_utils import object_to_str

def play(env, num_episodes, rl_agent, debug=False, logdir=None,
         cam_resolution=None, filename="play"):
    vidwriter = None
    if logdir is not None:
        if not os.path.exists(logdir):
            os.mkdir(logdir)
        vidwriter = cv2.VideoWriter(
            os.path.join(logdir, f'{filename}.mp4'), cv2.VideoWriter_fourcc(*'mp4v'),
            24, (cam_resolution, cam_resolution))
    for epsd in range(num_episodes):
        episode_rwd = 0.0
        if debug:
            print('------------ Play episode ', epsd, '------------------')
        obs = env.reset()
        if vidwriter is not None:
            img = env.render(mode='rgb_array', width=cam_resolution,
                             height=cam_resolution)
            vidwriter.write(img[..., ::-1])
        step = 0
        while True:
            # rl_agent.predict() to get acts, not forcing deterministic.
            act, _states = rl_agent.predict(obs)
            next_obs, rwd, done, info = env.step(act)
            episode_rwd += rwd
            if vidwriter is not None:
                img = env.render(mode='rgb_array', width=cam_resolution,
                                 height=cam_resolution)
                vidwriter.write(img[..., ::-1])
            if done:
                if debug:
                    print(f'episode reward {episode_rwd:0.2f}')
                break
            obs = next_obs
            step += 1
        # input('Episode ended; press enter to go on')
    if vidwriter is not None:
        vidwriter.release()

class CustomCallback(BaseCallback):
    """
    A custom callback that runs eval and adds videos to Tensorboard.
    """

    def __init__(self, eval_env, logdir, num_train_envs, args,
                 num_steps_between_save=10000, viz=False, debug=False):
        super(CustomCallback, self).__init__(debug)
        # Those variables will be accessible in the callback
        # (they are defined in the base class)
        # The RL model
        # self.model = None  # type: BaseAlgorithm
        # An alias for self.model.get_env(), the environment used for training
        # self.training_env = None  # type: Union[gym.Env, VecEnv, None]
        # Number of time the callback was called
        # self.n_calls = 0  # type: int
        # self.num_timesteps = 0  # type: int
        # local and global variables
        # self.locals = None  # type: Dict[str, Any]
        # self.globals = None  # type: Dict[str, Any]
        # The logger object, used to report things in the terminal
        # self.logger = None  # stable_baselines3.common.logger
        # # Sometimes, for event callback, it is useful
        # # to have access to the parent object
        # self.parent = None  # type: Optional[BaseCallback]
        self._eval_env = eval_env
        self._logdir = logdir
        self._num_train_envs = num_train_envs
        self._my_args = args
        self._num_steps_between_save = num_steps_between_save
        self._viz = viz
        self._debug = debug
        self._steps_since_save = num_steps_between_save  # save right away

    def _on_training_start(self) -> None:
        """
        This method is called before the first rollout starts.
        """
        # Log args to tensorboard.
        self.logger.record('args', object_to_str(self._my_args))

    def _on_rollout_start(self) -> None:
        """
        A rollout is the collection of environment interaction
        using the current policy.
        This event is triggered before collecting new samples.
        """
        pass

    def _on_step(self) -> bool:
        """
        This method will be called by the model after each call to `env.step()`.

        For child callback (of an `EventCallback`), this will be called
        when the event is triggered.

        :return: (bool) If the callback returns False, training is aborted early.
        """
        self._steps_since_save += self._num_train_envs
        if self._steps_since_save >= self._num_steps_between_save:
            # Save checkpoint.
            if self._logdir is not None:
                self.model.save(os.path.join(self._logdir, 'agent'))
                pickle.dump(self._my_args,
                            open(os.path.join(self._logdir, 'args.pkl'), 'wb'),
                            protocol=pickle.HIGHEST_PROTOCOL)
            self._steps_since_save = 0
            # Record video.
            if not self._my_args.disable_logging_video:
                screens = []

                def grab_screens(_locals, _globals):
                    screen = self._eval_env.render(
                        mode='rgb_array', width=300, height=300)
                    # PyTorch uses CxHxW vs HxWxC gym (and TF) images
                    screens.append(screen.transpose(2, 0, 1))

                evaluate_policy(
                    self.model, self._eval_env, callback=grab_screens,
                    n_eval_episodes=1, deterministic=False)
                self.logger.record(
                    'trajectory/video',
                    Video(torch.ByteTensor([screens]), fps=50),
                    exclude=('stdout', 'log', 'json', 'csv'))

        return True

    def _on_rollout_end(self) -> None:
        """
        This event is triggered before updating the policy.
        """
        pass

    def _on_training_end(self) -> None:
        """
        This event is triggered before exiting the `learn()` method.
        """
        pass


#409. Longest Palindrome

import collections

def longestPalindrome(self, s):
    if len(s) <= 1:
        return len(s)
    s_c = collections.Counter(s)
    count, odds = 0, 0
    for ch, freq in s_c.items():
        if freq % 2 != 0:
            odds += 1
        count += freq

    return count-odds+1 if odds != 0 else count

s = "abccccdd"
print(longestPalindrome(0, s))


from PIL import Image, ImageOps

# resize the image
def resizeImage(img):
    resize_factor = 130
    size = [int(img.size[0] / resize_factor), int(img.size[1] / resize_factor)]
    print('Image size after resizing: ', size)
    return img.resize(size)

# function to remap the image (turn it to greyscale and map between 0-10)
def remapImage(img):
    img = ImageOps.grayscale(img) # to grayscale

    # transforms pixels to fit on a scale between 0-10
    pixels_for_pi = [int(value / 25.5) for value in list(img.getdata())]

    # transforms pixels back to a scale of 255 for viewing purposes
    pixels = [value * 25.5 for value in pixels_for_pi]

    # shows new rempaed image
    img = Image.new(img.mode, img.size)
    img.putdata(pixels)
    # img.show()

    return pixels_for_pi

def piLoop(pixels):
    # opens file and saves to ...
    with open('data/pi data/pi.txt') as f:
        for line in f:
            data = line[2:]
    print('loaded')

    for x in range(len(data) - len(pixels)):
        if data[x:len(pixels)+x] == pixels:
            print('success!!!!!!!!!!!!!!!!')

if __name__ == '__main__':

    img_path = 'data/img data/face.jpg'
    img = Image.open(img_path)

    img = resizeImage(img)
    pixels = remapImage(img)
    pixels = ''.join([str(x) for x in pixels])
    print(pixels)

    piLoop(pixels)


import numpy as np
import re
from sklearn.datasets import fetch_20newsgroups
from sklearn.datasets import load_files

def clean_str(string):
    """
    Tokenization/string cleaning for all datasets except for SST.
    Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
    """
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip().lower()

def batch_iter(data, batch_size, num_epochs, shuffle=False):
    """
    Generates a batch iterator for a dataset.
    """
    data = np.array(data)
    data_size = len(data)
    num_batches_per_epoch = int((len(data)-1)/batch_size) + 1
    for epoch in range(num_epochs):
        # Shuffle the data at each epoch
        if shuffle:
            shuffle_indices = np.random.permutation(np.arange(data_size))
            shuffled_data = data[shuffle_indices]
        else:
            shuffled_data = data
        for batch_num in range(num_batches_per_epoch):
            start_index = batch_num * batch_size
            end_index = min((batch_num + 1) * batch_size, data_size)
            yield shuffled_data[start_index:end_index]

def get_datasets_20newsgroup(subset='train', categories=None, shuffle=True, random_state=42):
    """
        Retrieve data from 20 newsgroups
        :param subset: train, test or all
        :param categories: List of newsgroup name
        :param shuffle: shuffle the list or not
        :param random_state: seed integer to shuffle the dataset
        :return: data and labels of the newsgroup
        """
    datasets = fetch_20newsgroups(subset=subset, categories=categories, shuffle=shuffle, random_state=random_state)
    #print(datasets)
    return datasets

def get_datasets_abstract(dir):
    print('+++++++++abstract+++++++++')
    #dir = 'datatrain/'
    task_examples = list(open(dir + 'task_data_file', "r").readlines())
    task_examples = [s.strip() for s in task_examples]
    model_examples = list(open(dir + 'model_data_file', "r").readlines())
    model_examples = [s.strip() for s in model_examples]
    dataset_examples = list(open(dir + 'dataset_data_file', "r").readlines())
    dataset_examples = [s.strip() for s in dataset_examples]
    result_examples = list(open(dir + 'result_data_file', "r").readlines())
    result_examples = [s.strip() for s in result_examples]
    other_examples = list(open(dir + 'other_data_file', "r").readlines())
    other_examples = [s.strip() for s in other_examples]
    task_model_examples = list(open(dir + 'task_model_file', "r").readlines())
    task_model_examples = [s.strip() for s in task_model_examples]
    data_result_examples = list(open(dir + 'data_result_file', "r").readlines())
    data_result_examples = [s.strip() for s in data_result_examples]
    all_examples = list(open(dir + 'all_file', "r").readlines())
    all_examples = [s.strip() for s in all_examples]

    data_train = dict()
    data_train['data'] = all_examples
    target = [0 for x in all_examples]
    data_train['target'] = target
    data_train['target_names'] = ['all_examples']
    return data_train

def get_datasets_intents(dir):

    expert_examples = list(open(dir + 'expert.txt', "r").readlines())
    expert_examples = [s.strip() for s in expert_examples]
    paper_examples = list(open(dir + 'paper.txt', "r").readlines())
    paper_examples = [s.strip() for s in paper_examples]
    conference_examples = list(open(dir + 'conference.txt', "r").readlines())
    conference_examples = [s.strip() for s in conference_examples]

    data_train = dict()
    data_train['data'] = expert_examples + paper_examples + conference_examples
    target = [0 for x in expert_examples] + [1 for x in paper_examples] + [2 for x in conference_examples]
    data_train['target'] = target
    data_train['target_names'] = ['expert', 'paper', 'conference']

    return data_train

def get_datasets_intentst(dir):

    expert_examples = list(open(dir + 'expertt.txt', "r").readlines())
    expert_examples = [s.strip() for s in expert_examples]
    paper_examples = list(open(dir + 'papert.txt', "r").readlines())
    paper_examples = [s.strip() for s in paper_examples]
    conference_examples = list(open(dir + 'conferencet.txt', "r").readlines())
    conference_examples = [s.strip() for s in conference_examples]

    data_train = dict()
    data_train['data'] = expert_examples + paper_examples + conference_examples
    target = [0 for x in expert_examples] + [1 for x in paper_examples] + [2 for x in conference_examples]
    data_train['target'] = target
    data_train['target_names'] = ['expert', 'paper', 'conference']

    return data_train

def get_datasets_mrpolarity(positive_data_file, negative_data_file):
    """
    Loads MR polarity data from files, splits the data into words and generates labels.
    Returns split sentences and labels.
    """
    # Load data from files
    positive_examples = list(open(positive_data_file, "r").readlines())
    positive_examples = [s.strip() for s in positive_examples]
    negative_examples = list(open(negative_data_file, "r").readlines())
    negative_examples = [s.strip() for s in negative_examples]

    datasets = dict()
    datasets['data'] = positive_examples + negative_examples
    target = [0 for x in positive_examples] + [1 for x in negative_examples]
    datasets['target'] = target
    datasets['target_names'] = ['positive_examples', 'negative_examples']
    return datasets

def get_datasets_localdata(container_path=None, categories=None, load_content=True,
                       encoding='utf-8', shuffle=True, random_state=42):
    """
    Load text files with categories as subfolder names.
    Individual samples are assumed to be files stored a two levels folder structure.
    :param container_path: The path of the container
    :param categories: List of classes to choose, all classes are chosen by default (if empty or omitted)
    :param shuffle: shuffle the list or not
    :param random_state: seed integer to shuffle the dataset
    :return: data and labels of the dataset
    """
    datasets = load_files(container_path=container_path, categories=categories,
                          load_content=load_content, shuffle=shuffle, encoding=encoding,
                          random_state=random_state)
    return datasets

def load_data_labels(datasets):
    """
    Load data and labels
    :param datasets:
    :return:
    """
    # Split by words
    x_text = datasets['data']
    x_text = [clean_str(sent) for sent in x_text]
    # Generate labels
    labels = []
    for i in range(len(x_text)):
        label = [0 for j in datasets['target_names']]
        label[datasets['target'][i]] = 1
        labels.append(label)
    y = np.array(labels)
    return [x_text, y]

def load_embedding_vectors_word2vec(vocabulary, filename, binary):
    # load embedding_vectors from the word2vec
    encoding = 'utf-8'
    with open(filename, "rb") as f:
        header = f.readline()
        vocab_size, vector_size = map(int, header.split())
        # initial matrix with random uniform
        embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), vector_size))
        if binary:
            binary_len = np.dtype('float32').itemsize * vector_size
            for line_no in range(vocab_size):
                word = []
                while True:
                    ch = f.read(1)
                    if ch == b' ':
                        break
                    if ch == b'':
                        raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
                    if ch != b'\n':
                        word.append(ch)
                word = str(b''.join(word), encoding=encoding, errors='strict')
                idx = vocabulary.get(word)
                if idx != 0:
                    embedding_vectors[idx] = np.fromstring(f.read(binary_len), dtype='float32')
                else:
                    f.seek(binary_len, 1)
        else:
            for line_no in range(vocab_size):
                line = f.readline()
                if line == b'':
                    raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
                parts = str(line.rstrip(), encoding=encoding, errors='strict').split(" ")
                if len(parts) != vector_size + 1:
                    raise ValueError("invalid vector on line %s (is this really the text format?)" % (line_no))
                word, vector = parts[0], list(map('float32', parts[1:]))
                idx = vocabulary.get(word)
                if idx != 0:
                    embedding_vectors[idx] = vector
        f.close()
        return embedding_vectors

def load_embedding_vectors_glove(vocabulary, filename, vector_size):
    # load embedding_vectors from the glove
    # initial matrix with random uniform
    embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), vector_size))
    f = open(filename)
    for line in f:
        values = line.split()
        word = values[0]
        vector = np.asarray(values[1:], dtype="float32")
        idx = vocabulary.get(word)
        if idx != 0:
            embedding_vectors[idx] = vector
    f.close()
    return embedding_vectors

def load_char_embedding(vocabulary):
    alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}"
    one_hot = np.eye(26, dtype='int64')
    embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), 26))
    zero = np.zeros(26)
    for i in range(len(alphabet)):
        idx = vocabulary.get(alphabet[i])
        if i < 26:
            embedding_vectors[idx] = one_hot[i]
        else:
            embedding_vectors[idx] = zero
    return embedding_vectors


from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder

from .callback_data_models import MainAdMenuOption, ParamOnEdit, WatchAllAdsMenuOption

main_menu = InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text='Добавить обьявление', callback_data=MainAdMenuOption(action="create_new_ad").pack()),
                InlineKeyboardButton(text='Все объявления', callback_data=MainAdMenuOption(action="watch_all_ads").pack()),
            ],
            [
                InlineKeyboardButton(text='Мои обьявления', callback_data=MainAdMenuOption(action="watch_own_ads").pack())
            ]
        ]
    )

ad_params_on_edit = InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text='Фото', callback_data=ParamOnEdit(param_name="photo").pack()),
                InlineKeyboardButton(text='Название', callback_data=ParamOnEdit(param_name="title").pack()),
                InlineKeyboardButton(text='Описание', callback_data=ParamOnEdit(param_name="description").pack()),
                InlineKeyboardButton(text='Цену', callback_data=ParamOnEdit(param_name="price").pack())
            ],

            [
                InlineKeyboardButton(text='Назад', callback_data='back_to_watching_ads_before_edit')
            ]
        ],
    )

ad_creating_keyboard = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_creating')
        ]
    ]
)

ad_creating_keyboard_on_photo = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Пропустить', callback_data='skip_adding_photo')
        ],
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_creating')
        ]
    ]
)

break_ad_editing_keyb = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Отмена', callback_data='break_ad_editing')
        ]
    ]
)

back_to_main_menu = InlineKeyboardMarkup(
    inline_keyboard=
    [
        [
            InlineKeyboardButton(text='Вернуться в главное меню', callback_data='back_to_ad_menu')
        ]
    ]
)

kb_after_edit = InlineKeyboardMarkup(
    inline_keyboard=
    [
        [
            InlineKeyboardButton(text='К просмотру объявлений', callback_data='back_to_watching_ads_after_edit'),
            InlineKeyboardButton(text='В главное меню', callback_data='back-to_advertisement_menu'),
        ]
    ]
)

def kb_on_user_ad_watching(all_ads_len, current_num):
    builder = InlineKeyboardBuilder()
    two_btns_required = (0 < current_num < all_ads_len - 1)

    if current_num == 0 and all_ads_len != 1:
        builder.button(text='➡', callback_data='usr_ad_watch:next')
    elif (current_num == all_ads_len - 1) and all_ads_len != 1:
        builder.button(text='⬅', callback_data='usr_ad_watch:prev')
    elif two_btns_required:
        builder.button(text='⬅', callback_data='usr_ad_watch:prev')
        builder.button(text='➡', callback_data='usr_ad_watch:next')

    builder.button(text='🖋Изменить', callback_data='edit_ad')
    builder.button(text='🗑Удалить', callback_data='delete_ad')

    builder.button(text='В главное меню', callback_data='back-to_advertisement_menu')
    if all_ads_len == 1:
        builder.adjust(2, 1)
    else:
        builder.adjust(1 + two_btns_required, 2, 1)
    return builder.as_markup()

watch_all_ads_options = InlineKeyboardMarkup(
    inline_keyboard=[
        [
            InlineKeyboardButton(text='Поиск по названию', callback_data=WatchAllAdsMenuOption(action="search").pack()),
            InlineKeyboardButton(text='Смотреть все', callback_data=WatchAllAdsMenuOption(action="watch_all").pack())
        ],
        [
            InlineKeyboardButton(text='Вернуться в главное меню', callback_data=WatchAllAdsMenuOption(action="back_to_ad_menu").pack())
        ]
    ]
)

def kb_on_others_ad_watching(all_ads_len, current_num):
    builder = InlineKeyboardBuilder()
    two_btns_required = (0 < current_num < all_ads_len - 1)

    if not current_num and all_ads_len != 1:
        builder.button(text='➡', callback_data='scroll_ad:next')
    elif (current_num == all_ads_len - 1) and all_ads_len != 1:
        builder.button(text='⬅', callback_data='scroll_ad:prev')
    elif two_btns_required:
        builder.button(text='⬅', callback_data='scroll_ad:prev')
        builder.button(text='➡', callback_data='scroll_ad:next')

    builder.button(text="Назад", callback_data='back_to_watch_others_menu')
    builder.button(text='Главное меню', callback_data='back_to_ad_menu')
    if all_ads_len == 1:
        builder.adjust(2, 1)
    else:
        builder.adjust(1 + two_btns_required, 1)
    return builder.as_markup()


"""
1、学习目标
    掌握name元素定位方法
2、操作步骤（语法）
    driver.find_element_by_name("name的属性值")
"""

from selenium import webdriver
import time

driver = webdriver.Chrome()

url = "http://192.168.0.90/zfcapp/index.html#login"
driver.get(url)

time.sleep(2)

companyID = driver.find_element_by_name("企业ID")

print(companyID.get_attribute("outerHTML"))

companyID.send_keys("2")

time.sleep(3)

driver.quit()


from flask import Blueprint, json
from flask_restful import Api
from werkzeug.exceptions import HTTPException
from api.tracks.controllers import Tracks, Coordinates, TrackInfo, Sensors

api_tracks_bp = Blueprint("api", __name__)
api_tracks = Api(api_tracks_bp)

# Add resources
api_tracks.add_resource(Tracks, "/tracks")
api_tracks.add_resource(TrackInfo, "/tracks/<uuid:track_uuid>",
                        endpoint="track_info")
api_tracks.add_resource(Coordinates, "/tracks/<uuid:track_uuid>/coordinates")
api_tracks.add_resource(Sensors, "/tracks/<uuid:track_uuid>/sensors")

# JSON format for error
@api_tracks_bp.errorhandler(HTTPException)
def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    response = e.get_response()
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response


################################################################################
# GitKraken Glo のカードの一覧を CSV に出力します。
################################################################################

import csv
import json
import os
import py_glo_boards_api

# 作業ディレクトリのパス
WORKING_DIRECTORY_PATH = os.path.dirname(os.path.abspath(__file__))

# 設定をロード
config = json.load(open(f"{WORKING_DIRECTORY_PATH}/config.json", "rt"))

# 各ボード、カードから取得するフィールド
board_fields = ["name", "columns"]
card_fields = ["name", "description", "column_id", "labels"]

# 出力先のディレクトリが無ければ作成
dest_dir_path = f"{WORKING_DIRECTORY_PATH}/dest"
if not os.path.exists(dest_dir_path):
    os.makedirs(dest_dir_path)

# GloBoard オブジェクトを取得
globoard = py_glo_boards_api.GloBoard(config["personal_access_token"])

# 各ボードに対して処理を行う
for board in globoard.get_boards(board_fields):
    # カラムの ID がキー、名前が値の辞書を作成
    columns = {}
    for column in board.columns:
        columns[column.id] = column.name

    # 全カードのリストを作成
    cards = []
    for card in globoard.get_cards(board.id, card_fields, per_page=300):
        # カードデータ保存用の辞書を用意
        row = {}

        # 名前
        row["name"] = card.name

        # 説明
        # 改行は "\\n" に置換する
        if card.description is not None:
            row["description"] = card.description["text"].replace("\n", "\\n")

        # カラム
        row["column"] = columns[card.column_id]

        # ラベル
        # 複数ある場合は "|" で区切る
        row["label"] = ""
        if card.labels is not None:
            for label in card.labels:
                row["label"] += label.name + "|"
        row["label"] = row["label"].rstrip("|")

        # カードデータをリストに追加
        cards.append(row)

    # 全カードのリストを CSV に出力
    with open(
        f"{dest_dir_path}/{board.name}.csv", "wt", encoding="utf_8", newline=""
    ) as file:
        writer = csv.DictWriter(file, ["name", "description", "column", "label"])
        writer.writeheader()
        writer.writerows(cards)


#encoding: utf-8

import pandas as pd
import time
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import db.db as db
import redis
import sys

input_dim = 100
hidden_encoder_dim_1 = 133
hidden_encoder_dim_2 = 53
hidden_decoder_dim = 53
latent_dim = 33  # （latent Variable）
lam = 0

# 增量更新
add_ids = 500

# 阈值
ratio = 1.5
cpu_ratio = ratio
memory_ratio = ratio
disk_ratio = ratio
net_ratio = ratio

db_obj = db.database(dict=True)
r = redis.StrictRedis(host='127.0.0.1', port=6379, decode_responses=True)
r_key = 'ai_network_id'
nid = r.get('r_key') if r.get('r_key') else 0
nid = int(nid if int(nid) >= 100 else 100)
print("nid: "+str(nid))

# r.set(r_key,0)
# print(r.get(r_key))
# sys.exit(0)
def weight_variable(shape):
    # initial = tf.random_normal(shape)
    initial = tf.truncated_normal(shape, stddev=0.001)

    # 取正态分布的2σ部分
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0., shape=shape)
    return tf.Variable(initial)

x = tf.placeholder("float", shape=[None, input_dim])  # input x [,784]
l2_loss = tf.constant(0.0)

# encoder1 W b
W_encoder_input_hidden_1 = weight_variable(
    [input_dim, hidden_encoder_dim_1])  # 784*1000
b_encoder_input_hidden_1 = bias_variable([hidden_encoder_dim_1])  # 1000
l2_loss += tf.nn.l2_loss(W_encoder_input_hidden_1)

# Hidden layer1 encoder
hidden_encoder_1 = tf.nn.relu(
    tf.matmul(x, W_encoder_input_hidden_1) + b_encoder_input_hidden_1)  # w*x+b

# encoder2 W b
W_encoder_input_hidden_2 = weight_variable(
    [hidden_encoder_dim_1, hidden_encoder_dim_2])  # 1000*400
b_encoder_input_hidden_2 = bias_variable([hidden_encoder_dim_2])  # 400
l2_loss += tf.nn.l2_loss(W_encoder_input_hidden_2)

# Hidden layer2 encoder
hidden_encoder_2 = tf.nn.relu(tf.matmul(
    hidden_encoder_1, W_encoder_input_hidden_2) + b_encoder_input_hidden_2)  # w*x+b

W_encoder_hidden_mu = weight_variable(
    [hidden_encoder_dim_2, latent_dim])  # 400*20
b_encoder_hidden_mu = bias_variable([latent_dim])  # 20
l2_loss += tf.nn.l2_loss(W_encoder_hidden_mu)

# Mu encoder=+
mu_encoder = tf.matmul(hidden_encoder_2, W_encoder_hidden_mu) + \
             b_encoder_hidden_mu  # mu_encoder:1*20(1*400 400*20)

W_encoder_hidden_logvar = weight_variable(
    [hidden_encoder_dim_2, latent_dim])  # W_encoder_hidden_logvar:400*20
b_encoder_hidden_logvar = bias_variable([latent_dim])  # 20
l2_loss += tf.nn.l2_loss(W_encoder_hidden_logvar)

# Sigma encoder
logvar_encoder = tf.matmul(hidden_encoder_2, W_encoder_hidden_logvar) + \
                 b_encoder_hidden_logvar  # logvar_encoder:1*20(1*400 400*20)

# Sample epsilon
epsilon = tf.random_normal(tf.shape(logvar_encoder), name='epsilon')

# Sample latent variable
std_encoder = tf.exp(0.5 * logvar_encoder)
# z_mu+epsilon*z_std=z,as decoder's input;z:1*20
z = mu_encoder + tf.multiply(std_encoder, epsilon)

W_decoder_z_hidden = weight_variable(
    [latent_dim, hidden_decoder_dim])  # W_decoder_z_hidden:20*400
b_decoder_z_hidden = bias_variable([hidden_decoder_dim])  # 400
l2_loss += tf.nn.l2_loss(W_decoder_z_hidden)

# Hidden layer decoder
# hidden_decoder:1*400(1*20 20*400)
hidden_decoder = tf.nn.relu(
    tf.matmul(z, W_decoder_z_hidden) + b_decoder_z_hidden)

W_decoder_hidden_reconstruction = weight_variable(
    [hidden_decoder_dim, input_dim])  # 400*784
b_decoder_hidden_reconstruction = bias_variable([input_dim])
l2_loss += tf.nn.l2_loss(W_decoder_hidden_reconstruction)

KLD = -0.5 * tf.reduce_sum(1 + logvar_encoder - tf.pow(mu_encoder, 2) -
                           tf.exp(logvar_encoder), reduction_indices=1)  # KLD

x_hat = tf.matmul(hidden_decoder, W_decoder_hidden_reconstruction) + \
        b_decoder_hidden_reconstruction  # x_hat:1*784(reconstruction x)

BCE = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(
    logits=x_hat, labels=x), reduction_indices=1)  # sum cross_entropy

loss = tf.reduce_mean(BCE + KLD)  # average value

regularized_loss = loss + lam * l2_loss

# Record the stored value of loss
loss_summ = tf.summary.scalar("lowerbound", loss)
train_step = tf.train.AdamOptimizer(0.001).minimize(
    regularized_loss)  # Optimization Strategy

# add op for merging summary
summary_op = tf.summary.merge_all()

# add Saver ops
saver = tf.train.Saver()

n_steps = int(1e5 + 1)  # step:1000000
batch_size = 100

def data_get_time(timestamp):
    input_data = time.localtime(timestamp)
    return time.strftime("%Y-%m-%d %H:%M:%S", input_data)

def prediction(input_data):
    # data = pd.read_csv("../data/machine_usage.csv", header=None,names=["machine_id", "time_stamp", "cpu_util_percent", "mem_util_percent", "mem_gps", "mkpi","net_in", "net_out", "disk_io_percent"], nrows=1000000)
    # data["time_stamp"] = data["time_stamp"].apply(data_get_time)
    # id = 'm_2043'
    # input_data = data[data['machine_id'] == id]
    # input_data.drop(['mem_gps', 'mkpi', 'net_in'], axis=1, inplace=True)

    data = np.array(input_data['cpu_util_percent']).reshape([-1, 1])

    flag = False
    output = []

    # cpu part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/cpu/model_cpu.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]

        result = scal.fit_transform(result.reshape([-1, 1]))
        # result = result.reshape([-1, 100])
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > cpu_ratio:
            flag = True
            output.append('2')

    data = np.array(input_data['mem_util_percent']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/mem/model_mem.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > memory_ratio:
            flag = True
            output.append('3')

    data = np.array(input_data['net_out']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/net/model_net.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > net_ratio:
            flag = True
            output.append('1')

    data = np.array(input_data['disk_io_percent']).reshape([-1, 1])
    # mem part
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        scal = MinMaxScaler(feature_range=(0, 1))
        data = scal.fit_transform(data)
        saver.restore(sess, "./model/io/model_io.ckpt")
        feed_dict = {x: data.reshape([-1, 100])}
        result = sess.run([x_hat], feed_dict=feed_dict)[0]
        result = scal.fit_transform(result.reshape([-1, 1]))
        diff = (data - result) + 1
        mean = np.mean(diff[89:99])
        if diff[99] > mean:
            ratio = diff[99] / mean
        else:
            ratio = mean / diff[99]
        if ratio > disk_ratio:
            flag = True
            output.append('4')

    output.sort()
    cur_id = r.get('r_key')
    print("cur_id: "+str(cur_id))
    if not output:
        out_str = '0'
        print('No anomoly')
    else:
        out_str = ','.join(output)
        print('Anomoly_type: '+out_str)
    sql = "update data set anomoly_type = '"+out_str+"' where id = "+str(int(cur_id)+100)
    result = db_obj.save(sql)
    if result is True:
        print("Save success:")
        r.set('r_key', str(int(cur_id)+1))
    else:
        print("Save error:")
    print('over')

def read_data_from_db(add_ids, nid):
    sql = "select * from data where anomoly_type = '-1' and id >= "+str(nid-100)+" limit "+str(100+add_ids)
    result = db_obj.fetch_all(sql)
    # print(result)
    return list(result)

def prediction_all(dataset):
    # print(len(dataset))
    for i in range(100, len(dataset)):
        # print(dataset.iloc[i-100:i])
        prediction(dataset.iloc[i-100:i])

data = read_data_from_db(add_ids, nid)
data = pd.DataFrame(data)
# print(data[:3])
# print(data)
# print(type(data))
prediction_all(data)