import numpy as np
import pandas as pd
import ast
from collections import defaultdict
import os
from scipy import stats
import datetime as dt
# from catboost import CatBoostRegressor
# from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error

train = pd.read_csv('./input/train.csv')
test = pd.read_csv('./input/test.csv')

# Fixing values that are missing/incorrect for the train and test sets
# The data was taken from the discussions

train.loc[train['id'] == 16,'revenue'] = 192864
train.loc[train['id'] == 90,'budget'] = 30000000
train.loc[train['id'] == 118,'budget'] = 60000000
train.loc[train['id'] == 149,'budget'] = 18000000
train.loc[train['id'] == 313,'revenue'] = 12000000
train.loc[train['id'] == 451,'revenue'] = 12000000
train.loc[train['id'] == 464,'budget'] = 20000000
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 930000
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 819,'budget'] = 90000000
train.loc[train['id'] == 850,'budget'] = 90000000
train.loc[train['id'] == 1007,'budget'] = 2
train.loc[train['id'] == 1112,'budget'] = 7500000
train.loc[train['id'] == 1131,'budget'] = 4300000
train.loc[train['id'] == 1359,'budget'] = 10000000
train.loc[train['id'] == 1542,'budget'] = 1
train.loc[train['id'] == 1570,'budget'] = 15800000
train.loc[train['id'] == 1571,'budget'] = 4000000
train.loc[train['id'] == 1714,'budget'] = 46000000
train.loc[train['id'] == 1721,'budget'] = 17500000
train.loc[train['id'] == 1865,'revenue'] = 25000000
train.loc[train['id'] == 1885,'budget'] = 12
train.loc[train['id'] == 2091,'budget'] = 10
train.loc[train['id'] == 2268,'budget'] = 17500000
train.loc[train['id'] == 2491,'budget'] = 6
train.loc[train['id'] == 2602,'budget'] = 31000000
train.loc[train['id'] == 2612,'budget'] = 15000000
train.loc[train['id'] == 2696,'budget'] = 10000000
train.loc[train['id'] == 2801,'budget'] = 10000000
train.loc[train['id'] == 335,'budget'] = 2
train.loc[train['id'] == 348,'budget'] = 12
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 1100000
train.loc[train['id'] == 640,'budget'] = 6
train.loc[train['id'] == 696,'budget'] = 1
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 850,'budget'] = 1500000
train.loc[train['id'] == 1199,'budget'] = 5
train.loc[train['id'] == 1282,'budget'] = 9
train.loc[train['id'] == 1347,'budget'] = 1
train.loc[train['id'] == 1755,'budget'] = 2
train.loc[train['id'] == 1801,'budget'] = 5
train.loc[train['id'] == 1918,'budget'] = 592
train.loc[train['id'] == 2033,'budget'] = 4
train.loc[train['id'] == 2118,'budget'] = 344
train.loc[train['id'] == 2252,'budget'] = 130
train.loc[train['id'] == 2256,'budget'] = 1
train.loc[train['id'] == 2696,'budget'] = 10000000

test.loc[test['id'] == 6733,'budget'] = 5000000
test.loc[test['id'] == 3889,'budget'] = 15000000
test.loc[test['id'] == 6683,'budget'] = 50000000
test.loc[test['id'] == 5704,'budget'] = 4300000
test.loc[test['id'] == 6109,'budget'] = 281756
test.loc[test['id'] == 7242,'budget'] = 10000000
test.loc[test['id'] == 7021,'budget'] = 17540562
test.loc[test['id'] == 5591,'budget'] = 4000000
test.loc[test['id'] == 4282,'budget'] = 20000000
test.loc[test['id'] == 3033,'budget'] = 250
test.loc[test['id'] == 3051,'budget'] = 50
test.loc[test['id'] == 3084,'budget'] = 337
test.loc[test['id'] == 3224,'budget'] = 4
test.loc[test['id'] == 3594,'budget'] = 25
test.loc[test['id'] == 3619,'budget'] = 500
test.loc[test['id'] == 3831,'budget'] = 3
test.loc[test['id'] == 3935,'budget'] = 500
test.loc[test['id'] == 4049,'budget'] = 995946
test.loc[test['id'] == 4424,'budget'] = 3
test.loc[test['id'] == 4460,'budget'] = 8
test.loc[test['id'] == 4555,'budget'] = 1200000
test.loc[test['id'] == 4624,'budget'] = 30
test.loc[test['id'] == 4645,'budget'] = 500
test.loc[test['id'] == 4709,'budget'] = 450
test.loc[test['id'] == 4839,'budget'] = 7
test.loc[test['id'] == 3125,'budget'] = 25
test.loc[test['id'] == 3142,'budget'] = 1
test.loc[test['id'] == 3201,'budget'] = 450
test.loc[test['id'] == 3222,'budget'] = 6
test.loc[test['id'] == 3545,'budget'] = 38
test.loc[test['id'] == 3670,'budget'] = 18
test.loc[test['id'] == 3792,'budget'] = 19
test.loc[test['id'] == 3881,'budget'] = 7
test.loc[test['id'] == 3969,'budget'] = 400
test.loc[test['id'] == 4196,'budget'] = 6
test.loc[test['id'] == 4221,'budget'] = 11
test.loc[test['id'] == 4222,'budget'] = 500
test.loc[test['id'] == 4285,'budget'] = 11
test.loc[test['id'] == 4319,'budget'] = 1
test.loc[test['id'] == 4639,'budget'] = 10
test.loc[test['id'] == 4719,'budget'] = 45
test.loc[test['id'] == 4822,'budget'] = 22
test.loc[test['id'] == 4829,'budget'] = 20
test.loc[test['id'] == 4969,'budget'] = 20
test.loc[test['id'] == 5021,'budget'] = 40
test.loc[test['id'] == 5035,'budget'] = 1
test.loc[test['id'] == 5063,'budget'] = 14
test.loc[test['id'] == 5119,'budget'] = 2
test.loc[test['id'] == 5214,'budget'] = 30
test.loc[test['id'] == 5221,'budget'] = 50
test.loc[test['id'] == 4903,'budget'] = 15
test.loc[test['id'] == 4983,'budget'] = 3
test.loc[test['id'] == 5102,'budget'] = 28
test.loc[test['id'] == 5217,'budget'] = 75
test.loc[test['id'] == 5224,'budget'] = 3
test.loc[test['id'] == 5469,'budget'] = 20
test.loc[test['id'] == 5840,'budget'] = 1
test.loc[test['id'] == 5960,'budget'] = 30
test.loc[test['id'] == 6506,'budget'] = 11
test.loc[test['id'] == 6553,'budget'] = 280
test.loc[test['id'] == 6561,'budget'] = 7
test.loc[test['id'] == 6582,'budget'] = 218
test.loc[test['id'] == 6638,'budget'] = 5
test.loc[test['id'] == 6749,'budget'] = 8
test.loc[test['id'] == 6759,'budget'] = 50
test.loc[test['id'] == 6856,'budget'] = 10
test.loc[test['id'] == 6858,'budget'] =  100
test.loc[test['id'] == 6876,'budget'] =  250
test.loc[test['id'] == 6972,'budget'] = 1
test.loc[test['id'] == 7079,'budget'] = 8000000
test.loc[test['id'] == 7150,'budget'] = 118
test.loc[test['id'] == 6506,'budget'] = 118
test.loc[test['id'] == 7225,'budget'] = 6
test.loc[test['id'] == 7231,'budget'] = 85
test.loc[test['id'] == 5222,'budget'] = 5
test.loc[test['id'] == 5322,'budget'] = 90
test.loc[test['id'] == 5350,'budget'] = 70
test.loc[test['id'] == 5378,'budget'] = 10
test.loc[test['id'] == 5545,'budget'] = 80
test.loc[test['id'] == 5810,'budget'] = 8
test.loc[test['id'] == 5926,'budget'] = 300
test.loc[test['id'] == 5927,'budget'] = 4
test.loc[test['id'] == 5986,'budget'] = 1
test.loc[test['id'] == 6053,'budget'] = 20
test.loc[test['id'] == 6104,'budget'] = 1
test.loc[test['id'] == 6130,'budget'] = 30
test.loc[test['id'] == 6301,'budget'] = 150
test.loc[test['id'] == 6276,'budget'] = 100
test.loc[test['id'] == 6473,'budget'] = 100
test.loc[test['id'] == 6842,'budget'] = 30

train.head()
test.head()

# Some columns have issues with dictionaries, so those need to be adjusted
issue_cols = ['belongs_to_collection', 'genres','production_companies','production_countries',
              'spoken_languages','Keywords','cast','crew']
def fix_cols(df, cols):
    for col in cols:
        df[col] = df[col].apply(lambda x: {} if pd.isna(x) else ast.literal_eval(x))
    return df

# Applying
train = fix_cols(train, issue_cols)
test = fix_cols(test, issue_cols)

# Adjusting the collection columns
train['collection_name'] = train['belongs_to_collection'].apply(lambda x: x[0]['name'] if x!={} else '')
test['collection_name'] = test['belongs_to_collection'].apply(lambda x: x[0]['name'] if x!={} else '')
train['has_collection'] = train['collection_name'].apply(lambda x: 0 if '' else 1)
test['has_collection'] = test['collection_name'].apply(lambda x: 0 if '' else 1)

# Applying
train = train.drop(['belongs_to_collection'], axis=1)
test = test.drop(['belongs_to_collection'], axis=1)

# Fixing columns
def fixing_columns(train, test, original_col, count_col, num_to_return):
    train[original_col] = train[original_col].apply(lambda x: sorted(i['name'] for i in x) if x != {} else '')
    train[count_col] = train[original_col].apply(lambda x: len(x))

    col_list = list(train[original_col].apply(lambda x: [i for i in x]))
    col_counts = defaultdict(int)
    for row in col_list:
        for i in row:
            col_counts[i] += 1

    top = [i[0] for i in sorted(col_counts.items(), key=lambda kv: kv[1], reverse=True)[:num_to_return]]

    # One hot encoding if the production company is in the top 20 and exists in the record
    for top_var in list(top):
        train[top_var] = train[original_col].apply(lambda x: 1 if top_var in x else 0)

    # For test. Fixing the production_companies column and adding a pc count column
    test[original_col] = test[original_col].apply(lambda x: sorted(i['name'] for i in x) if x != {} else '')
    test[count_col] = test[original_col].apply(lambda x: len(x))

    # One hot encoding if the production company is in the top 20 and exists in the record
    for top_var in list(top):
        test[top_var] = test[original_col].apply(lambda x: 1 if top_var in x else 0)

number_to_keep = [20, 20, 5, 3, 10, 10, 10]
issue_cols = ['genres', 'production_companies', 'production_countries',
              'spoken_languages', 'Keywords', 'cast', 'crew']
for i in range(len(issue_cols)):
    fixing_columns(train, test, issue_cols[i], 'num_' + issue_cols[i], number_to_keep[i])
# Dropping the columns that we are not considering at this moment
drop_columns = ['genres', 'homepage', 'imdb_id', 'original_title', 'overview', 'poster_path',
                'production_companies', 'production_countries', 'spoken_languages',
                'status', 'tagline', 'title', 'Keywords', 'cast', 'crew']

# Applying
train = train.drop(drop_columns, axis=1)
test = test.drop(drop_columns, axis=1)

# Removing the N/A values and just replacing them with 0
print("Test NA: ", test.columns[test.isna().sum() > 0].tolist())
print("\nTrain NA: ", train.columns[train.isna().sum() > 0].tolist())

train[train.isna().any(axis=1)]
train[train.isna().any(axis=1)]

# Using 0 to fill the na columns
train.fillna(0, inplace=True)
test.fillna(0, inplace=True)

# Adjusting the date values
def date_adjust(df):
    # Converting release date into proper format
    df[['release_month', 'release_day', 'release_year']] = df['release_date'].str.split('/', expand=True).replace(
        np.nan, 0).astype(int)

    releaseDate = pd.to_datetime(df['release_date'])
    df['release_week'] = releaseDate.dt.week
    df['release_dayofweek'] = releaseDate.dt.dayofweek
    df['release_quarter'] = releaseDate.dt.quarter
    df['original_english'] = df['original_language'].apply(lambda x: 1 if x == 'en' else 0)
    df = df.drop(['collection_name', 'original_language', 'release_date'], axis=1)

# Applying
date_adjust(train)
date_adjust(test)

train = train.drop(['collection_name', 'original_language', 'release_date', 'id'], axis=1)
test = test.drop(['collection_name', 'original_language', 'release_date', 'id'], axis=1)

# Creating some new features based on combinations of high performing movies

def feature_transform(df):
    df['budget_runtime'] = (df['budget'] + 1) / (df['runtime'] + 1)
    df['poularity_year'] = (df['popularity'] + 1) / (df['release_year'] + 1)
    df['budget_popularity'] = (df['budget'] + 1) / (df['popularity'] + 1)
    df['budget_year'] = (df['budget'] + 1) / (df['release_year'] + 1)
    df['runtime_year'] = (df['runtime'] + 1) / (df['release_year'] + 1)

feature_transform(train)
feature_transform(test)
train['budget'] = train['budget'].apply(np.log1p)
test['budget'] = test['budget'].apply(np.log1p)
X = np.asarray(train.drop(['revenue'], axis=1))
y = np.asarray(train.revenue.apply(np.log1p))

# Training and Running an xgboost model

# Setting Parameters

params = {'objective': 'reg:linear',
          'eta': 0.01,
          'max_depth': 6,
          'min_child_weight': 3,
          'subsample': 0.8,
          'colsample_bytree': 0.8,
          'colsample_bylevel': .5,
          'gamma': 1.45,
          'eval_metric': 'rmse',
          'seed': 1,
          'silent': True}

# CatBoostRegressor
# params = {'random_seed': 0,
#          'early_stopping_rounds': 500,
#          'eval_metric': 'RMSE',
#          'logging_level': 'Silent',
#          'n_estimators': 1000,
#          'depth': hp.choice('depth', np.arange(3, 15, dtype=int)),
#          'learning_rate': hp.uniform('learning_rate', 0, 0.75),
#          'colsample_bylevel': hp.uniform('colsample_bylevel', 0.5, 1)}

# LGBMRegressor
#params = {'seed': 0,
#          'tree_learner_type': hp.choice('tree_learner_type', ['serial', 'feature', 'data', 'voting']),
#          'n_estimators': hp.choice('n_estimators', [100, 250, 500, 750, 1000]),
#          'num_leaves': hp.choice('num_leaves', [50, 100, 150, 200, 250, 500, 750, 1000]),
#          'min_data_in_leaf' : hp.choice('min_data_in_leaf', [10, 20, 30, 40, 50, 100]),
#          'max_depth': hp.choice('max_depth', np.arange(3, 15, dtype=int)),
#          'learning_rate': hp.uniform('learning_rate', 0, 0.75),}

def train_xgb(X_train, y_train, X_test, y_test):
    xgb_data = [(xgb.DMatrix(X_train, y_train), 'train'), (xgb.DMatrix(X_test, y_test), 'valid')]
    xgb_model = xgb.train(params, xgb.DMatrix(X_train, y_train),
                          5000, xgb_data,
                          verbose_eval=100,
                          early_stopping_rounds=500)
    return xgb_model

error = []
kf = list(KFold(n_splits=10, random_state=1, shuffle=True).split(X))
for i, (train_index, test_index) in enumerate(kf):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    # cat_model = CatBoostRegressor(**args)
    # lgb_model = LGBMRegressor(**args)
    xgb_model = train_xgb(X_train, y_train, X_test, y_test)
    error.append(xgb_model.best_score)

print("Error: {} +/- {}".format(np.mean(error), np.std(error)))

# Plotting the feature importance of Last fold
xgb_model.feature_names = train.columns.tolist()
fig, ax = plt.subplots(figsize=(20,10))
xgb.plot_importance(xgb_model, max_num_features=30,ax=ax, height=.9)
plt.show()

# Fitting on the full training set
xgb_full_train = xgb.XGBRegressor(objective = 'reg:linear',
                                  eta = 0.01,
                                  max_depth = 6,
                                  min_child_weight = 3,
                                  subsample = 0.8,
                                  colsample_bytree = 0.7,
                                  eval_metric = 'rmse',
                                  seed = 1,
                                  n_estimators = 2800)

# lgb_full_train = LGBMRegressor(n_estimators=500,
#                      min_data_in_leaf=10,
#                      learning_rate=0.0100,
#                      bagging_fraction=0.427,
#                      max_depth=7,
#                      num_leaves=250)

# cat_full_train = CatBoostRegressor(iterations=1000,
#                          eval_metric='RMSE',
#                          early_stopping_rounds=200,
#                          metric_period=None,
#                          depth=8,
#                          learning_rate=0.043,
#                          colsample_bylevel=0.153,
#                          bagging_temperature=0.063)

xgb_full_train.fit(X, y)

# lgb_full_train.fit(X, y)
# cat_full_train.fit(X, y)

y_predictions = xgb_full_train.predict(test.values)

# y_predictions = lgb_full_train.predict(test.values)
# y_predictions = cat_full_train.predict(test.values)

#train = pd.read_csv('./input/train.csv')
submission = pd.read_csv('./input/sample_submission.csv')
submission['revenue'] = np.exp(y_predictions)

submission.to_csv('submission13.csv',index=False)


# encoding:utf-8
import json
import requests
from requests.exceptions import RequestException
import re
import time
def get_one_page(url):
    # 添加头信息
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3964.2 Safari/537.36'}
    html = requests.get(url, headers=headers)
    if html.status_code == 200:
        return html.text
    return None
def parse_one_page(html):
    pattern = re.compile('
.*?num .*?>(.*?)	.*?colorgray3.*?>(.*?).*?linka.*?>(.*?).*?colorgray9">(.*?)	.*?colorgray9">(.*?)	.*?fnt600">(.*?)	.*?colorgray9">(.*?)	.*?colorgray9">(.*?)	.*?
', re.S)
    items = re.findall(pattern, html)
    for item in items:
        yield {
            '排名': item[0],
            '姓名': item[1],
            '队名': item[2],
            '位置': item[3],
            '出场次数': item[4],
            '进球数': item[5],
            '点球数': item[6],
            '平均进球时间': item[7]
        }
def write_to_file(content):
    with open('result.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')
def main(dataPage):
    url = 'https://data.huanhuba.com/ucl/goals-410-1-37521-p' + str(dataPage) + '/'
    html = get_one_page(url)
    for item in parse_one_page(html):
        print(item)
        write_to_file(item)
if __name__ == '__main__':
    for dataPage in range(1,17):
        main(dataPage)
        time.sleep(1)


# -*- coding: utf-8 -*-
import urllib, os, requests, random#, logging
from multiprocessing.dummy import Pool as ThreadPool
#logging.basicConfig(level=logging.DEBUG)

DIR = os.path.abspath(os.curdir) + '/'
pool_number = 10

#get random proxy from proxy.txt
def get_random_proxy():
	try:
		return random.choice(open('proxy.txt', 'r').readlines())
	except:
		print('get_random_proxy error')
		return None

#generate url by parameters
def create_api_url(method, parameters={}):
	try:
		return 'https://api.vk.com/method/' + method + '?' + urllib.urlencode(parameters)
	except:
		print('create_api_url error')
		return None

#create request by url
def get_request(url):
	try:
		random_proxy = get_random_proxy()
		#print(random_proxy)
		return requests.get(url, proxies={'http':random_proxy}).json()
	except:
		print('get_request error')
		return None

#create vkapi request by method_name and parameters
def vkapi_method(method, parameters={}):
	try:
		url = create_api_url(method, parameters)
		#print(url)
		return get_request(url)['response']
	except:
		print('vkapi_method error')
		return None

#https://vk.com/dev/groups.getMembers
def groups_getMembers(groupid='', offset=0, sort='', count=1000, fields='', filter_=''):
	try:
		print(offset)
		f = open(DIR + groupid + '_usersdata.txt', 'a')
		res = vkapi_method('groups.getMembers', {'group_id':groupid, 'offset': offset, 'count':count, 'sort':sort, 'fields':fields, 'filter':filter_})
		for i in res['users']:
			f.write(str(i) + '\n')
		f.close()
		return res['count']
	except:
		print('groups_getMembers error')
		return None

#https://vk.com/dev/groups.search
def groups_search(q, access_token, filename='', type_='', country_id='', city_id='', future='', market='', offset=0, sort=0, count=1000):
	try:
		if filename == '':
			fname = q
		else:
			fname = filename
		f = open(DIR + fname + '.txt', 'a')
		for i in vkapi_method('groups.search', {'access_token':access_token, 'q':q, 'type':type_, 'country_id':country_id, 'ciy_id':city_id, 'future':future, 'market':market, 'sort':sort, 'count':count, 'offset':offset})[1:]:
			#print(i['gid'])
			f.write(str(i['gid']) + '\n')
		f.close()
		return 'susccess'
	except:
		print('groups_search error')
		return None

#https://vk.com/dev/wall.get
def wall_get(access_token='', owner_id='', domain='', count=1000, extended='', fields='', offset=0, filter_='owner', likes=None, reposts=None, comments=None):
	try:
		print(offset)
		filename = max(owner_id, domain)
		f = open(DIR + filename + '_posts.txt', 'a')
		res = vkapi_method('wall.get', {'owner_id':owner_id, 'domain':domain, 'count':count, 'extended':extended, 'fields':fields, 'offset':offset, 'filter':filter_, 'access_token':access_token})
		for i in res[1:]:
			if i['likes']['count'] >= likes and i['reposts']['count'] >= reposts and i['comments']['count'] >= comments:
				f.write(str(i['likes']['count']) + ',' + str(i['id']) + ',' + str(i['date']) + '\n')
		f.close()
		return res[0]
	except:
		print('wall_get error')
		return None

#===========================================================================================================MAIN FUNCTIONS
#get full users list by group_id
def get_users_list_by_groupid(groupid):
	try:
		#in first step get 1000 users id and count of group (border)
		open(DIR + groupid + '_usersdata.txt', 'w')
		border = groups_getMembers(groupid)
		print('Group count:' + str(border))
		#ThreadPool model
		offsets = [(groupid, x) for x in range(1000, border+1, 1000)]
		def groups_getMembers_local(groupid_offset):
			return groups_getMembers(*groupid_offset)
		pool = ThreadPool(pool_number)
		pool.map(groups_getMembers_local, offsets)
		return 'susccess'
	except:
		print('get_users_list_by_groupid error')
		return None

#get groups list by substrings and parameters
def get_groups_list_by_substrings(q, access_token, filename='', type_='', country_id='', city_id='', future='', market='', offset=0, sort=0, count=''):
	try:
		fname = q[:50]
		open(DIR + fname + '.txt', 'w')
		substrings_list = q.split(',')
		for string in substrings_list:
			print(string)
			groups_search(string, access_token, fname, type_, country_id, city_id, future, market, offset, sort, count)
		return 'susccess'
	except:
		print('get_groups_list error')
		return None

#get posts list by group and parameters
def get_page_posts(access_token='', owner_id='', domain='', count='', extended='', fields='', offset=0, filter_='owner', likes=None, reposts=None, comments=None):
	try:
		filename = max(owner_id, domain)
		open(DIR + filename + '_posts.txt', 'w')
		border = wall_get(access_token, owner_id, domain, count, extended, fields, offset, filter_, likes, reposts, comments)
		print(border)
		#ThreadPool model
		offsets = [(access_token, owner_id, domain, count, extended, fields, offset, filter_, likes, reposts, comments) for offset in range(100, border+1, 100)]
		def wall_get_local(parameters):
				return wall_get(*parameters)
		pool = ThreadPool(pool_number)
		pool.map(wall_get_local, offsets)
		return 'susccess', border
	except:
		print('get_page_posts error')
		return None


from django.conf.urls import include, url
from django.views.generic import TemplateView

from . import views

urlpatterns = [
    url(r'^$', views.Home.as_view(), name='home'),
    url(r'^appt$', views.Appt.as_view(), name='appt'),
    url(r'^p_login/(\d+)/(\d+)$', views.PatientCheckin.as_view(), name='p_login'),
    url(r'^demographic/(\d+)/(\d+)$', views.Demographics.as_view(), name='demog'),
    url(r'^checkin/(\d+)$', views.Checkin.as_view(), name='checkin'),
    url(r'^d_waitlist$', views.Waitlist.as_view(), name='d_waitlist'),
    url(r'^begin$', TemplateView.as_view(template_name='begin.html'), name='begin'),
    url(r'^d_login$', TemplateView.as_view(template_name='doctor_login.html'), name='d_login'),
    url(r'^logout$', views.Logout.as_view(), name='logout'),

    url(r'', include('social.apps.django_app.urls', namespace='social')),

    # included to provide easy access to checkin error page
    # url(r'^checkin_err$', TemplateView.as_view(template_name='checkin_error.html'), name='checkin_err'),
]


from typing import List

class Solution:
    def exist(self, board: List[List[int]], word: str) -> bool:
        rows = len(board)
        if rows == 0:
            return False
        cols = len(board[0])
        is_come = []
        for _ in range(rows):
            is_come.append([False] * cols)
        for i in range(rows):
            for j in range(cols):
                if self.check(board, word, is_come, i, j, 0, len(word)):
                    return True
        return False

    def check(self, board: List[List[int]], word: str, is_come: List[List[bool]], i: int, j: int, flag: int, max_flag: int) -> bool:
        print("check", i, j)
        if flag == max_flag:
            return True
        rows = len(board)
        if rows == 0:
            return False
        cols = len(board[0])
        is_next_in = False
        if i < 0 or j < 0 or i >= rows or j >= cols:
            return is_next_in
        if board[i][j] == word[flag] and not is_come[i][j]:
            is_come[i][j] = True
            flag += 1
            is_next_in = self.check(board, word, is_come, i-1, j, flag, max_flag) or\
                self.check(board, word, is_come, i, j-1, flag, max_flag) or\
                self.check(board, word, is_come, i+1, j, flag, max_flag) or\
                self.check(board, word, is_come, i, j+1, flag, max_flag)

            if not is_next_in:
                flag -= 1
                is_come[i][j] = False
        return is_next_in

if __name__ == "__main__":
    s = Solution()
    board = [["A", "B", "C", "E"], ["S", "F", "E", "S"], ["A", "D", "E", "E"]]
    word = "ABCESEEEFS"
    if s.exist(board, word):
        print("exists")
    else:
        print("not exist")


import sys
import logging
import numpy as np
import pandas as pd
import constants as const
from src.yolo_metrics_img import YoloMetricImg

logging.basicConfig(format=const.LOG_FORMAT,
                    level=const.LOG_LEVEL,
                    stream=sys.stdout)

class YoloMetricsImgs:
    def __init__(self,
                 df_pred,
                 df_origin,
                 test_set,
                 classes,
                 model,
                 iou_thr=0.5):
        self._origin = df_origin
        self._pred = df_pred
        self._test_set = test_set
        self._iou_thr = iou_thr
        self._classes = classes
        self._model = model

    def get_metrics_img(self, by_class=None):
        origin = self._origin
        pred = self._pred

        if by_class is not None:
            origin = origin[origin.type_disk == by_class]
            pred = pred[pred.type_disk == by_class]
        metric_verbose_list = []
        metric_list = []
        for img in self._test_set:
            origin_img = origin[origin.file == img]
            pred_img = pred[pred.file == img]

            if origin_img.shape[0] == 0 & \
                    pred_img.shape[0] == 0:
                continue

            metrics_obj = YoloMetricImg(df_img_origin=origin_img,
                                        df_img_pred=pred_img,
                                        iou_thr=self._iou_thr)

            metric_verbose_img, metric_img = metrics_obj.get_metrics()

            metric_verbose_list.append(metric_verbose_img)
            metric_list.append(metric_img)

        metric_verbose = pd.concat(metric_verbose_list,
                                   axis=0,
                                   sort=True)
        metric = pd.concat(metric_list,
                           axis=0,
                           sort=True)

        metric_cum = self.get_prec_recall_cumulative(metric_verbose)

        ap = self.calc_average_precisions(rec=metric_cum.recall,
                                          prec=metric_cum.precision)
        metric['ap'] = ap
        return metric_cum, metric

    def get_metrics_by_class(self, summary=True):
        metrics_verbose_list = []
        metrics_list = []
        summary_df = None

        logging.info('Metrics for model = {}'.format(self._model))

        for class_id in self._classes:
            metric_verbose_class, metrics_class = self.get_metrics_img(by_class=class_id)

            metric_verbose_class['type_disk'] = class_id
            metrics_class['type_disk'] = class_id

            metrics_verbose_list.append(metric_verbose_class)
            metrics_list.append(metrics_class)

        metric_verbose = pd.concat(metrics_verbose_list,
                                   axis=0,
                                   sort=True)
        metric = pd.concat(metrics_list,
                           axis=0,
                           sort=True)

        metric_verbose['model'] = self._model

        if summary:
            logging.info('Create summary')
            summary_df = self.get_summarize(metric=metric)

        return metric_verbose, metric, summary_df

    def get_summarize(self, metric):

        metric_gr = metric.groupby(by='type_disk').agg({'pb': 'sum',
                                                        'ab': 'sum',
                                                        'tp': 'sum',
                                                        'ap': 'mean'}).reset_index()
        metric_all = metric[['pb', 'tp', 'ab']].sum()
        metric_all['ap'] = metric_gr['ap'].mean()
        metric_all['type_disk'] = 'all'
        metric_all = pd.DataFrame(metric_all).transpose()

        metric_gen = pd.concat([metric_gr, metric_all],
                               axis=0,
                               sort=False)

        metric_gen['precision'] = metric_gen.tp / metric_gen.pb
        metric_gen['recall'] = metric_gen.tp / metric_gen.ab
        metric_gen['f1'] = 2 * metric_gen.precision * metric_gen.recall / \
                           (metric_gen.precision + metric_gen.recall)
        metric_gen['model'] = self._model

        return metric_gen

    @staticmethod
    def get_prec_recall_cumulative(metric):
        metric = metric.sort_values(by='iou',
                                    ascending=False)
        metric['acc_tp'] = metric.tp.cumsum(axis=0)
        metric = metric.reset_index()

        metric['ind'] = metric.index + 1

        cnt = metric.shape[0]

        metric['precision'] = metric.acc_tp / metric.ind
        metric['recall'] = metric.acc_tp / cnt

        return metric

    @staticmethod
    def calc_average_precisions(rec, prec):
        mrec = []
        mrec.append(0)
        [mrec.append(e) for e in rec]
        mrec.append(1)
        mpre = []
        mpre.append(0)
        [mpre.append(e) for e in prec]
        mpre.append(0)
        for i in range(len(mpre) - 1, 0, -1):
            mpre[i - 1] = max(mpre[i - 1], mpre[i])
        ii = []
        for i in range(len(mrec) - 1):
            if mrec[1:][i] != mrec[0:-1][i]:
                ii.append(i + 1)
        ap = 0
        for i in ii:
            ap = ap + np.sum((mrec[i] - mrec[i - 1]) * mpre[i])
        # return [ap, mpre[0:len(mpre) - 1], mrec[0:len(mpre) - 1], ii]
        return ap


from sklearn.linear_model import LogisticRegression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score

data = pd.read_csv('iris-data.csv')
data.head()
data.describe()
data.info()

# drop na values
data = data.dropna()
data.info()

# make classes consistent
data['class'].value_counts()
data['class'].replace(["Iris-setossa", "versicolor"],
                      ["Iris-setosa", "Iris-versicolor"], inplace=True)
data['class'].value_counts()

# select 2 categories only
final_data = data[data['class'] != 'Iris-virginica']
final_data['class'].value_counts()

# sns.pairplot(final_data, hue='class', height=2.5)
# plt.show()

final_data.hist(column='sepal_length_cm', bins=20, figsize=(10, 5))
# remove outliests
final_data.loc[final_data.sepal_length_cm < 1, [
    'sepal_length_cm']] = final_data['sepal_length_cm']*100
final_data.hist(column='sepal_length_cm', bins=20, figsize=(10, 5))
final_data = final_data.drop(final_data[(
    final_data['class'] == "Iris-setosa") & (final_data['sepal_width_cm'] < 2.5)].index)

# sns.pairplot(final_data, hue='class', size=2.5)
# plt.show()

# replace class names with values
final_data['class'].replace(
    ["Iris-setosa", "Iris-versicolor"], [1, 0], inplace=True)
final_data.head()

# model construction
# get values for matrix
inp_data = final_data.iloc[:, :-1].values
out_data = final_data.iloc[:, -1].values

# scale
scalar = StandardScaler()
inp_data = scalar.fit_transform(inp_data)

# split into test and train
X_train, X_test, y_train, y_test = train_test_split(
    inp_data, out_data, test_size=0.2, random_state=42)

# X_tr_arr = X_train
# X_ts_arr = X_test
# y_tr_arr = y_train.as_matrix()
# y_ts_arr = y_test.as_matrix()
print('Input Shape', (X_train.shape))
print('Output Shape', X_test.shape)

def weight_init(n_features):
    w = np.zeros((1, n_features))
    b = 0
    return w, b

def sigmoid_activation(result):
    return 1/(1+np.exp(-result))

def model_optimize(w, b, X, Y):
    m = X.shape[0]

    # Prediction
    final_result = sigmoid_activation(np.dot(w, X.T)+b)
    Y_T = Y.T
    cost = (-1/m)*(np.sum((Y_T*np.log(final_result)) +
                          ((1-Y_T)*(np.log(1-final_result)))))

    # Gradient calculation
    dw = (1/m)*(np.dot(X.T, (final_result-Y.T).T))
    db = (1/m)*(np.sum(final_result-Y.T))

    grads = {"dw": dw, "db": db}

    return grads, cost

def model_predict(w, b, X, Y, learning_rate, no_iterations):
    costs = []
    for i in range(no_iterations):
        #
        grads, cost = model_optimize(w, b, X, Y)
        #
        dw = grads["dw"]
        db = grads["db"]
        # weight update
        w = w - (learning_rate * (dw.T))
        b = b - (learning_rate * db)
        #

        if (i % 100 == 0):
            costs.append(cost)
            print("Cost after %i iteration is %f" % (i, cost))

    # final parameters
    coeff = {"w": w, "b": b}
    gradient = {"dw": dw, "db": db}

    return coeff, gradient, costs

def predict(final_pred, m):
    y_pred = np.zeros((1, m))
    for i in range(final_pred.shape[1]):
        if final_pred[0][i] > 0.5:
            y_pred[0][i] = 1
    return y_pred

# Get number of features
n_features = X_train.shape[1]
print('Number of Features', n_features)
w, b = weight_init(n_features)
# Gradient Descent
coeff, gradient, costs = model_predict(
    w, b, X_train, y_train, learning_rate=0.0001, no_iterations=4500)
# Final prediction
w = coeff["w"]
b = coeff["b"]
print('Optimized weights', w)
print('Optimized intercept', b)
#
final_train_pred = sigmoid_activation(np.dot(w, X_train.T)+b)
final_test_pred = sigmoid_activation(np.dot(w, X_test.T)+b)
#
m_tr = X_train.shape[0]
m_ts = X_test.shape[0]
#
y_tr_pred = predict(final_train_pred, m_tr)
print('Training Accuracy', accuracy_score(y_tr_pred.T, y_train))
#
y_ts_pred = predict(final_test_pred, m_ts)
print('Test Accuracy', accuracy_score(y_ts_pred.T, y_test))

plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title('Cost reduction over time')
plt.show()

# Sklearn
clf = LogisticRegression()
clf.fit(X_tr_arr, y_tr_arr)
print(clf.intercept_, clf.coef_)
pred = clf.predict(X_ts_arr)
print('Accuracy from sk-learn: {0}'.format(clf.score(X_ts_arr, y_ts_arr)))


from __future__ import unicode_literals

from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpRequest
from django.template import RequestContext, Template
from djblets.avatars.services.gravatar import GravatarService

from reviewboard.testing import TestCase
from reviewboard.avatars import avatar_services
from reviewboard.avatars.testcase import AvatarServicesTestMixin
from reviewboard.avatars.tests import DummyAvatarService

class TemplateTagTests(AvatarServicesTestMixin, TestCase):
    """Tests for reviewboard.avatars.templatetags."""

    fixtures = ['test_users']

    @classmethod
    def setUpClass(cls):
        super(TemplateTagTests, cls).setUpClass()
        avatar_services.enable_service(GravatarService.avatar_service_id,
                                       save=False)

    def setUp(self):
        super(TemplateTagTests, self).setUp()

        gravatar_service = avatar_services.get_avatar_service(
            GravatarService.avatar_service_id)
        avatar_services.enable_service(gravatar_service.avatar_service_id,
                                       save=False)
        avatar_services.set_default_service(gravatar_service, save=False)

        self.user = User.objects.get(username='doc')
        self.request = HttpRequest()
        self.request.user = AnonymousUser()

    def test_avatar_urls(self):
        """Testing {% avatar_urls %} template tag"""
        service = avatar_services.default_service
        self.assertIsNotNone(service)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': service.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"3x": "%(3x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )

    def test_avatar_urls_with_service(self):
        """Testing {% avatar_urls %} template tag with avatar_service_id"""
        service = DummyAvatarService(use_2x=True)
        avatar_services.register(service)
        avatar_services.enable_service(service.avatar_service_id)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': service.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )

    def test_avatar_urls_no_service(self):
        """Testing {% avatar_urls %} template tag with no available services"""
        services = list(avatar_services)

        for service in services:
            avatar_services.unregister(service)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
            })),
            '{}')

    def test_avatar_urls_service_not_found(self):
        """Testing {% avatar_urls %} template tag with an invalid service"""
        service = avatar_services.default_service

        self.assertIsNotNone(service)
        self.assertIsNone(avatar_services.get_avatar_service(
            DummyAvatarService.avatar_service_id))

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': DummyAvatarService.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"3x": "%(3x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )


"""
    File name: main.py
    Author: Timothy Clark
    Date created: 6/27/2021
    Date last modified: 6/27/2021
    Python Version: 3.8

    Description: this file contains all utility methods used when connecting to the Alpaca API
"""

import alpaca_trade_api as tradeapi
import constants
import datetime
import pandas as pd

api = tradeapi.REST(
    constants.API_KEY,
    constants.API_SECRET_KEY,
    constants.MARKET
)

def isTomorrowOpen():
    result = False
    date = datetime.datetime.now() + datetime.timedelta(1)
    tomorrow = date.strftime("%Y-%m-%d %H:%M:%S")
    calendar = api.get_calendar(tomorrow,tomorrow)[0]

    if str(calendar.date)[:10] == tomorrow[:10]:
        result = True

    print(result)
    return result

def isTodayOpen():
    result = False
    date = datetime.datetime.now()
    today = date.strftime("%Y-%m-%d %H:%M:%S")
    calendar = api.get_calendar(today, today)[0]

    if str(calendar.date)[:10] == today[:10]:
        result = True

    print(result)
    return result

def createTransaction(symbol, qty, side, type, tif):
    api.submit_order(symbol,qty,side,type,tif)

def getHistory():
    history= api.get_portfolio_history(period='1W', timeframe='1H',).df
    return history.drop(columns=['profit_loss', 'profit_loss_pct'])

def getEquity():
    account = api.get_account()

    equity = account.equity

    return "$"+str(equity)


from reportlab.platypus import Paragraph
from reportlab.platypus import Table
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus import Spacer

from reportlab.lib.styles import getSampleStyleSheet

from reportlab.lib.pagesizes import A4

from reportlab.lib import colors
import Basedd

class AlbaranVentas:

    def __init__(self, referencia, dni, fecha):
        """Genera el albaran de la venta.

        :param referencia: referencia de producto para buscar el precio
        :param dni: parametro de dni para filtrar al consulta por parametro en la bdd
        :param fecha: fecha de la venta

        """
        self.referencia = referencia
        self.fecha = fecha

        estiloHoja = getSampleStyleSheet()

        story = []

        Basedd.Basedd.conectar(self)

        self.ventas = Basedd.Basedd.get_venta(self, dni, self.fecha)

        print([self.ventas[0], self.ventas[1], self.ventas[2], self.ventas[3], self.ventas[4], self.ventas[5]])

        filaVenta = [self.ventas[0], self.ventas[1], self.ventas[2], self.ventas[3], self.ventas[4], self.ventas[5]]

        Basedd.Basedd.cerrar(self)

        fila1 = ['Refcia.', 'DNI', 'Nombre', 'Fecha', 'Unidades', 'Total euros']

        tabla = Table((fila1, filaVenta))

        tabla.setStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)])

        estiloHoja = getSampleStyleSheet()
        cabecera = estiloHoja['Heading4']

        parrafo = Paragraph("Factura simplificada:", cabecera)
        story.append(parrafo)

        story.append(tabla)

        story.append(Spacer(0, 20))

        doc = SimpleDocTemplate("AlvaranVentas.pdf", pagesize=A4, showBoundary=1)

        doc.build(story)


# Geodesic Regression on Sphere Manifold
# Manifolds
import manifolds
import numpy as np
import StatsModel as sm

# Visualization
import vtk

# Time
import time

# Parameters
nDimManifold = 3

# Ground Truth
p_interp = manifolds.sphere( nDimManifold )
v_slope = manifolds.sphere_tVec( nDimManifold )

p_interp.SetPoint( [ 0.0, 0.0, 1.0 ] )
v_slope.SetTangentVector( [ 0, np.pi * 0.25, 0 ] )

# Generating sphere manifolds. distributed over time perturbed by Gaussian random
# Time
t0 = 0
t1 = 2

# Generate a random point on the manifold
nData = 1000
dim = nDimManifold
sigma = 0.15

pt_list = []
t_list = []

for n in range( nData ):
	# time_pt = np.random.uniform( t0, t1 )
	time_pt = ( t1 - t0 ) * n / nData + t0

	# Generate a random Gaussians with polar Box-Muller Method
	rand_pt = np.zeros( nDimManifold ).tolist()

	for i in range( dim ):
		r2 = 0
		x = 0
		y = 0

		while( r2 > 1.0 or r2 == 0 ):
			x = ( 2.0 * np.random.rand() - 1.0 )
			y = ( 2.0 * np.random.rand() - 1.0 )
			r2 = x * x + y * y

		gen_rand_no = sigma * y * np.sqrt( -2.0 * np.log( r2 ) / r2 )
		rand_pt[ i ] = gen_rand_no
	# print( rand_pt )

	# Set Random Vector to Tangent Vector - ListToTangent
	rand_tVec = manifolds.sphere_tVec( nDimManifold )
	rand_tVec.SetTangentVector( rand_pt )

	v_t = manifolds.sphere_tVec( nDimManifold )

	for i in range( nDimManifold ):
		v_t.tVector[ i ] = v_slope.tVector[ i ] * time_pt

	mean = p_interp.ExponentialMap( v_t )

	# print( "Mean At Time : " + str( time_pt ) )
	# print( mean.pt )

	# Projected Tangent to Mean Point
	rand_tVec_projected = mean.ProjectTangent( mean, rand_tVec )

	# print( "Random Tangent" )
	# print( rand_tVec.tVector )

	# print( "Projected Random Tangent" )
	# print( rand_tVec_projected.tVector )

	# Perturbed point at time_pt
	pt_perturbed = mean.ExponentialMap( rand_tVec_projected )

	# print( "Perturbed pt At Time : " + str( time_pt ) )
	# print( pt_perturbed.pt )

	pt_list.append( pt_perturbed )
	t_list.append( time_pt )

#######################
# Geodesic Regression #
#######################

# Gradient Descent Parameters
step_size = 0.1
max_iter = 200
step_tol = 1e-4

start_greg = time.time()
base_GReg, tangent_GReg = sm.GeodesicRegression( t_list, pt_list, max_iter, step_size, step_tol, False )
end_greg = time.time()

#######################
# Linearized Geodesic Regression #
#######################

step_size = 0.01
max_iter = 500
step_tol = 1e-8

base_lReg1, tangent_lReg1 = sm.LinearizedGeodesicRegression( t_list, pt_list, 1, step_size, step_tol, False, True )

base_lReg2, tangent_lReg2 = sm.LinearizedGeodesicRegression( t_list, pt_list, 2, step_size, step_tol, False, True )

base_lReg3, tangent_lReg3 = sm.LinearizedGeodesicRegression( t_list, pt_list, 3, step_size, step_tol, False, True )

base_lReg4, tangent_lReg4 = sm.LinearizedGeodesicRegression( t_list, pt_list, 4, step_size, step_tol, False, True )

start_egreg = time.time()
base_lReg, tangent_lReg = sm.LinearizedGeodesicRegression( t_list, pt_list, max_iter, step_size, step_tol, False, False )
end_egreg = time.time()

print( "True P" )
print( p_interp.pt )
print( "True V" )
print( v_slope.tVector )

# Linearized Regression Results
print( "=================================================" )
print( "    Resulsts : Linearized Geodesic Regression    " )
print( "=================================================" )

print( "Estimated P" )
print( base_lReg.pt )
print( "Estimated V" )
print( tangent_lReg.tVector )

R2 = sm.R2Statistics( t_list, pt_list, base_lReg, tangent_lReg )

print( "R2 Statistics" )
print( R2 )

RMSE = sm.RootMeanSquaredError( t_list, pt_list, base_lReg, tangent_lReg )

print( "RMSE" )
print( RMSE )

# Linearized Regression Results
print( "=================================================" )
print( "    Resulsts : Geodesic Regression    " )
print( "=================================================" )

print( "Estimated P" )
print( base_GReg.pt )
print( "Estimated V" )
print( tangent_GReg.tVector )

R2_GReg = sm.R2Statistics( t_list, pt_list, base_GReg, tangent_GReg )

print( "R2 Statistics" )
print( R2_GReg )

RMSE_GReg = sm.RootMeanSquaredError( t_list, pt_list, base_GReg, tangent_GReg )

print( "RMSE" )
print( RMSE_GReg )

# # Permutation Test
# nTrial = 10000

# print( "======================================" )
# print( "Random Permutation Testing........    " )
# print( ( "# of Trials : %d", nTrial )  )
# print( "======================================" )

# P_val = sm.NullHypothesisTestingPermutationTest( t_list, pt_list, base, tangent, nTrial, max_iter, step_size, step_tol )

# print( "P-Value" )
# print( P_val )

########################################
#####        Visualization        ######
########################################
# Visualize a sphere coordinate
sphere = vtk.vtkSphereSource()
sphere.SetThetaResolution( 30 )
sphere.SetPhiResolution( 30 )
sphere.SetRadius( 1.0 )
sphere.SetCenter( 0.0, 0.0, 0.0 )
sphere.SetLatLongTessellation( True )
sphere.Update()

conMapper = vtk.vtkPolyDataMapper()
conMapper.SetInputData( sphere.GetOutput() )
conMapper.ScalarVisibilityOff()
conMapper.Update()

conActor = vtk.vtkActor()
conActor.SetMapper( conMapper )
conActor.GetProperty().SetOpacity( 1 )
conActor.GetProperty().SetColor( 0.9, 0.9, 0.9 )
conActor.GetProperty().SetEdgeColor( 0.4, 0.4, 0.7 )
conActor.GetProperty().EdgeVisibilityOn()
conActor.GetProperty().SetAmbient(0.3)
conActor.GetProperty().SetDiffuse(0.375)
conActor.GetProperty().SetSpecular(0.0)

# Visualize spherical points - Red
points = vtk.vtkPoints()

for i in range( len( pt_list ) ):
	points.InsertNextPoint( pt_list[ i ].pt[0], pt_list[ i ].pt[1], pt_list[ i ].pt[2] )

ptsPolyData = vtk.vtkPolyData()
ptsPolyData.SetPoints( points )

vertFilter = vtk.vtkVertexGlyphFilter()
vertFilter.SetInputData( ptsPolyData )
vertFilter.Update()

ptsMapper = vtk.vtkPolyDataMapper()
ptsMapper.SetInputData( vertFilter.GetOutput() )

ptsActor = vtk.vtkActor()
ptsActor.SetMapper( ptsMapper )
ptsActor.GetProperty().SetPointSize( 8 )
ptsActor.GetProperty().SetColor( 0, 0, 1 )
ptsActor.GetProperty().SetOpacity( 1.0 )
ptsActor.GetProperty().SetRenderPointsAsSpheres( 1 )

# Visualize an initial anchor point - Magenta
points_init = vtk.vtkPoints()

points_init.InsertNextPoint( pt_list[ 0 ].pt[0], pt_list[ 0 ].pt[1], pt_list[ 0 ].pt[2] )

ptInitPolyData = vtk.vtkPolyData()
ptInitPolyData.SetPoints( points_init )

init_vertFilter = vtk.vtkVertexGlyphFilter()
init_vertFilter.SetInputData( ptInitPolyData )
init_vertFilter.Update()

ptInitMapper = vtk.vtkPolyDataMapper()
ptInitMapper.SetInputData( init_vertFilter.GetOutput() )

ptInitActor = vtk.vtkActor()
ptInitActor.SetMapper( ptInitMapper )
ptInitActor.GetProperty().SetPointSize( 30 )
ptInitActor.GetProperty().SetColor( 1, 0, 0 )
ptInitActor.GetProperty().SetOpacity( 1.0 )
ptInitActor.GetProperty().SetRenderPointsAsSpheres( 1 )

# Visualize a ground truth trend line - yellow
trend_pt_list = []
trend_t_list = []
nTimePt = 100

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ v_slope.tVector[0] * time_pt, v_slope.tVector[1] * time_pt, v_slope.tVector[2] * time_pt ]  )
	mean = p_interp.ExponentialMap( v_t )

	trend_pt_list.append( mean )
	trend_t_list.append( time_pt )

linePolyData = vtk.vtkPolyData()
linePts = vtk.vtkPoints()

for i in range( len( trend_pt_list ) ):
	linePts.InsertNextPoint( trend_pt_list[ i ].pt[0], trend_pt_list[ i ].pt[1], trend_pt_list[ i ].pt[2] )

linePolyData.SetPoints( linePts )

lines = vtk.vtkCellArray()

for i in range( len( trend_pt_list ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	lines.InsertNextCell( line_i )

linePolyData.SetLines( lines )

lineMapper = vtk.vtkPolyDataMapper()
lineMapper.SetInputData( linePolyData )

lineActor = vtk.vtkActor()
lineActor.SetMapper( lineMapper )
lineActor.GetProperty().SetColor( 1, 1, 0 )
lineActor.GetProperty().SetOpacity( 1.0 )
lineActor.GetProperty().SetLineWidth( 10 )
lineActor.GetProperty().SetRenderLinesAsTubes( 1 )

# Visualize an estimated trend - Geodesic Regression : Carolina Blue
est_trend_pt_list = []
est_trend_t_list = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_GReg.tVector[0] * time_pt, tangent_GReg.tVector[1] * time_pt, tangent_GReg.tVector[2] * time_pt ]  )
	mean = base_GReg.ExponentialMap( v_t )

	est_trend_pt_list.append( mean )
	est_trend_t_list.append( time_pt )

estLinePolyData = vtk.vtkPolyData()
estLinePts = vtk.vtkPoints()

for i in range( len( est_trend_pt_list ) ):
	estLinePts.InsertNextPoint( est_trend_pt_list[ i ].pt[0], est_trend_pt_list[ i ].pt[1], est_trend_pt_list[ i ].pt[2] )

estLinePolyData.SetPoints( estLinePts )

estLines = vtk.vtkCellArray()

for i in range( len( est_trend_pt_list ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines.InsertNextCell( line_i )

estLinePolyData.SetLines( estLines )

estLineMapper = vtk.vtkPolyDataMapper()
estLineMapper.SetInputData( estLinePolyData )

estLineActor = vtk.vtkActor()
estLineActor.SetMapper( estLineMapper )
estLineActor.GetProperty().SetColor( 0.6, 0.729, 0.867 )
estLineActor.GetProperty().SetOpacity( 0.5 )
estLineActor.GetProperty().SetLineWidth( 20 )
estLineActor.GetProperty().SetRenderLinesAsTubes( 0 )

# Visualize an estimated trend of Linearized Geodesic Regression Model - NYU Violet
est_trend_LReg_pt_list = []
est_trend_LReg_t_list = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_lReg.tVector[0] * time_pt, tangent_lReg.tVector[1] * time_pt, tangent_lReg.tVector[2] * time_pt ]  )
	mean = base_lReg.ExponentialMap( v_t )

	est_trend_LReg_pt_list.append( mean )
	est_trend_LReg_t_list.append( time_pt )

estLinePolyData_LReg = vtk.vtkPolyData()
estLinePts_LReg = vtk.vtkPoints()

for i in range( len( est_trend_LReg_pt_list ) ):
	estLinePts_LReg.InsertNextPoint( est_trend_LReg_pt_list[ i ].pt[0], est_trend_LReg_pt_list[ i ].pt[1], est_trend_LReg_pt_list[ i ].pt[2] )

estLinePolyData_LReg.SetPoints( estLinePts_LReg )
estLines_LReg = vtk.vtkCellArray()

for i in range( len( est_trend_LReg_pt_list ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines_LReg.InsertNextCell( line_i )

estLinePolyData_LReg.SetLines( estLines_LReg )

estLineMapper_LReg = vtk.vtkPolyDataMapper()
estLineMapper_LReg.SetInputData( estLinePolyData_LReg )

estLineActor_LReg = vtk.vtkActor()
estLineActor_LReg.SetMapper( estLineMapper_LReg )
estLineActor_LReg.GetProperty().SetColor( 0.980, 0.447,0.408 )
estLineActor_LReg.GetProperty().SetOpacity( 0.7 )
estLineActor_LReg.GetProperty().SetLineWidth( 30 )
estLineActor_LReg.GetProperty().SetRenderLinesAsTubes( 1 )

points_final = vtk.vtkPoints()

points_final.InsertNextPoint( base_lReg3.pt[0], base_lReg3.pt[1], base_lReg3.pt[2] )

ptFinalPolyData = vtk.vtkPolyData()
ptFinalPolyData.SetPoints( points_final )

final_vertFilter = vtk.vtkVertexGlyphFilter()
final_vertFilter.SetInputData( ptFinalPolyData )
final_vertFilter.Update()

ptFinalMapper = vtk.vtkPolyDataMapper()
ptFinalMapper.SetInputData( final_vertFilter.GetOutput() )

ptFinalActor = vtk.vtkActor()
ptFinalActor.SetMapper( ptFinalMapper )
ptFinalActor.GetProperty().SetPointSize( 30 )
ptFinalActor.GetProperty().SetColor( 1, 0, 0 )
ptFinalActor.GetProperty().SetOpacity( 1.0 )
ptFinalActor.GetProperty().SetRenderPointsAsSpheres( 1 )

# Visualize an estimated trend of Linearized Geodesic Regression Model - Iteration 0
est_trend_LReg_pt_list1 = []
est_trend_LReg_t_list1 = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_lReg1.tVector[0] * time_pt, tangent_lReg1.tVector[1] * time_pt, tangent_lReg1.tVector[2] * time_pt ]  )
	mean = base_lReg1.ExponentialMap( v_t )

	est_trend_LReg_pt_list1.append( mean )
	est_trend_LReg_t_list1.append( time_pt )

estLinePolyData_LReg1 = vtk.vtkPolyData()
estLinePts_LReg1 = vtk.vtkPoints()

for i in range( len( est_trend_LReg_pt_list1 ) ):
	estLinePts_LReg1.InsertNextPoint( est_trend_LReg_pt_list1[ i ].pt[0], est_trend_LReg_pt_list1[ i ].pt[1], est_trend_LReg_pt_list1[ i ].pt[2] )

estLinePolyData_LReg1.SetPoints( estLinePts_LReg1 )
estLines_LReg1 = vtk.vtkCellArray()

for i in range( len( est_trend_LReg_pt_list1 ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines_LReg1.InsertNextCell( line_i )

estLinePolyData_LReg1.SetLines( estLines_LReg1 )

estLineMapper_LReg1 = vtk.vtkPolyDataMapper()
estLineMapper_LReg1.SetInputData( estLinePolyData_LReg1 )

estLineActor_LReg1 = vtk.vtkActor()
estLineActor_LReg1.SetMapper( estLineMapper_LReg1 )
estLineActor_LReg1.GetProperty().SetColor( 0.980, 0.447,0.408 )
estLineActor_LReg1.GetProperty().SetOpacity( 0.7 )
estLineActor_LReg1.GetProperty().SetLineWidth( 30 )
estLineActor_LReg1.GetProperty().SetRenderLinesAsTubes( 1 )

# Visualize an estimated trend of Linearized Geodesic Regression Model - Iteration 1
est_trend_LReg_pt_list2 = []
est_trend_LReg_t_list2 = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_lReg2.tVector[0] * time_pt, tangent_lReg2.tVector[1] * time_pt, tangent_lReg2.tVector[2] * time_pt ]  )
	mean = base_lReg2.ExponentialMap( v_t )

	est_trend_LReg_pt_list2.append( mean )
	est_trend_LReg_t_list2.append( time_pt )

estLinePolyData_LReg2 = vtk.vtkPolyData()
estLinePts_LReg2 = vtk.vtkPoints()

for i in range( len( est_trend_LReg_pt_list2 ) ):
	estLinePts_LReg2.InsertNextPoint( est_trend_LReg_pt_list2[ i ].pt[0], est_trend_LReg_pt_list2[ i ].pt[1], est_trend_LReg_pt_list2[ i ].pt[2] )

estLinePolyData_LReg2.SetPoints( estLinePts_LReg2 )
estLines_LReg2 = vtk.vtkCellArray()

for i in range( len( est_trend_LReg_pt_list2 ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines_LReg2.InsertNextCell( line_i )

estLinePolyData_LReg2.SetLines( estLines_LReg2 )

estLineMapper_LReg2 = vtk.vtkPolyDataMapper()
estLineMapper_LReg2.SetInputData( estLinePolyData_LReg2 )

estLineActor_LReg2 = vtk.vtkActor()
estLineActor_LReg2.SetMapper( estLineMapper_LReg2 )
estLineActor_LReg2.GetProperty().SetColor( 0.980, 0.447,0.408 )
estLineActor_LReg2.GetProperty().SetOpacity( 0.7 )
estLineActor_LReg2.GetProperty().SetLineWidth( 30 )
estLineActor_LReg2.GetProperty().SetRenderLinesAsTubes( 1 )

points_2 = vtk.vtkPoints()

points_2.InsertNextPoint( base_lReg1.pt[0], base_lReg1.pt[1], base_lReg1.pt[2] )

ptPolyData2 = vtk.vtkPolyData()
ptPolyData2.SetPoints( points_2 )

vertFilter2 = vtk.vtkVertexGlyphFilter()
vertFilter2.SetInputData( ptPolyData2 )
vertFilter2.Update()

ptMapper2 = vtk.vtkPolyDataMapper()
ptMapper2.SetInputData( vertFilter2.GetOutput() )

ptActor2 = vtk.vtkActor()
ptActor2.SetMapper( ptMapper2 )
ptActor2.GetProperty().SetPointSize( 30 )
ptActor2.GetProperty().SetColor( 1, 0, 0 )
ptActor2.GetProperty().SetOpacity( 1.0 )
ptActor2.GetProperty().SetRenderPointsAsSpheres( 1 )

# Visualize an estimated trend of Linearized Geodesic Regression Model - Iteration 3
est_trend_LReg_pt_list3 = []
est_trend_LReg_t_list3 = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_lReg3.tVector[0] * time_pt, tangent_lReg3.tVector[1] * time_pt, tangent_lReg3.tVector[2] * time_pt ]  )
	mean = base_lReg3.ExponentialMap( v_t )

	est_trend_LReg_pt_list3.append( mean )
	est_trend_LReg_t_list3.append( time_pt )

estLinePolyData_LReg3 = vtk.vtkPolyData()
estLinePts_LReg3 = vtk.vtkPoints()

for i in range( len( est_trend_LReg_pt_list3 ) ):
	estLinePts_LReg3.InsertNextPoint( est_trend_LReg_pt_list3[ i ].pt[0], est_trend_LReg_pt_list3[ i ].pt[1], est_trend_LReg_pt_list3[ i ].pt[2] )

estLinePolyData_LReg3.SetPoints( estLinePts_LReg3 )
estLines_LReg3 = vtk.vtkCellArray()

for i in range( len( est_trend_LReg_pt_list3 ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines_LReg3.InsertNextCell( line_i )

estLinePolyData_LReg3.SetLines( estLines_LReg3 )

estLineMapper_LReg3 = vtk.vtkPolyDataMapper()
estLineMapper_LReg3.SetInputData( estLinePolyData_LReg3 )

estLineActor_LReg3 = vtk.vtkActor()
estLineActor_LReg3.SetMapper( estLineMapper_LReg3 )
estLineActor_LReg3.GetProperty().SetColor( 0.980, 0.447,0.408 )
estLineActor_LReg3.GetProperty().SetOpacity( 0.7 )
estLineActor_LReg3.GetProperty().SetLineWidth( 30 )
estLineActor_LReg3.GetProperty().SetRenderLinesAsTubes( 1 )

# Visualize an estimated trend of Linearized Geodesic Regression Model - Iteration 3
est_trend_LReg_pt_list4 = []
est_trend_LReg_t_list4 = []

for n in range( nTimePt ):
	time_pt = ( t1 - t0 ) * n / ( nTimePt - 1 ) + t0

	# Generate a random Gaussians with polar Box-Muller Method
	v_t = manifolds.sphere_tVec( nDimManifold )
	v_t.SetTangentVector( [ tangent_lReg4.tVector[0] * time_pt, tangent_lReg4.tVector[1] * time_pt, tangent_lReg4.tVector[2] * time_pt ]  )
	mean = base_lReg4.ExponentialMap( v_t )

	est_trend_LReg_pt_list4.append( mean )
	est_trend_LReg_t_list4.append( time_pt )

estLinePolyData_LReg4 = vtk.vtkPolyData()
estLinePts_LReg4 = vtk.vtkPoints()

for i in range( len( est_trend_LReg_pt_list4 ) ):
	estLinePts_LReg4.InsertNextPoint( est_trend_LReg_pt_list4[ i ].pt[0], est_trend_LReg_pt_list4[ i ].pt[1], est_trend_LReg_pt_list4[ i ].pt[2] )

estLinePolyData_LReg4.SetPoints( estLinePts_LReg4 )
estLines_LReg4 = vtk.vtkCellArray()

for i in range( len( est_trend_LReg_pt_list4 ) - 1 ):
	line_i = vtk.vtkLine()
	line_i.GetPointIds().SetId( 0, i )
	line_i.GetPointIds().SetId( 1, i + 1 )
	estLines_LReg4.InsertNextCell( line_i )

estLinePolyData_LReg4.SetLines( estLines_LReg4 )

estLineMapper_LReg4 = vtk.vtkPolyDataMapper()
estLineMapper_LReg4.SetInputData( estLinePolyData_LReg4 )

estLineActor_LReg4 = vtk.vtkActor()
estLineActor_LReg4.SetMapper( estLineMapper_LReg4 )
estLineActor_LReg4.GetProperty().SetColor( 0.980, 0.447,0.408 )
estLineActor_LReg4.GetProperty().SetOpacity( 0.7 )
estLineActor_LReg4.GetProperty().SetLineWidth( 30 )
estLineActor_LReg4.GetProperty().SetRenderLinesAsTubes( 1 )

# Renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer( ren )

renIn = vtk.vtkRenderWindowInteractor()
renIn.SetRenderWindow( renWin )

# Iteration 0
ren.AddActor( conActor )
ren.AddActor( ptsActor )
ren.AddActor( lineActor )
# ren.AddActor( estLineActor )
ren.AddActor( estLineActor_LReg1 )
ren.AddActor( ptInitActor )

light = vtk.vtkLight()
light.SetFocalPoint(1.875,0.6125,0)
light.SetPosition(0.875,1.6125,1)

ren.AddLight( light )
ren.SetBackground( 1.0, 1.0, 1.0 )

renWin.Render()
renIn.Start()

# Iteration 2
ren.RemoveActor( ptInitActor )
ren.RemoveActor( estLineActor_LReg1 )
ren.AddActor( estLineActor_LReg2 )
ren.AddActor( ptActor2 )

renWin.Render()
renIn.Start()

# # Iteration 3
# ren.RemoveActor( estLineActor_LReg2 )
# ren.AddActor( estLineActor_LReg3 )

# renWin.Render()
# renIn.Start()

# # Iteration 4
# ren.RemoveActor( estLineActor_LReg3 )
# ren.AddActor( estLineActor_LReg4 )

# renWin.Render()
# renIn.Start()

# Final Estimation
ren.RemoveActor( estLineActor_LReg2 )
ren.RemoveActor( ptActor2 )
ren.AddActor( estLineActor_LReg )
# ren.AddActor( estLineActor )
ren.AddActor( ptFinalActor )

renWin.Render()
renIn.Start()

print( "GReg time ")
print( end_greg - start_greg )

print( "EGReg time" )
print( end_egreg - start_egreg )


import pickle
import re
import nltk
pos=negat=neu=0
def get_words_in_tweet(tweet):
   wordList = re.sub("[^\w]", " ",tweet).split()
  # print wordList
   return wordList
def get_word_features(wordlist):
    wordlist = nltk.FreqDist(wordlist)
    word_features = wordlist.keys()
    return word_features

def extract_features(document):
    document_words = set(document)
    features = {}
    for word in word_features:
        features['contains(%s)' % word] = (word in document_words)
    return features

f = open('my_classifier.pickle0089607', 'rb')
classifier = pickle.load(f)
open('classified.txt', 'w').close()
open('pre_processing.txt', 'w').close()
execfile('pre_processing.py')
File=open("twitDB46.txt")

File1=open("pre_processing.txt")
N=50

for i in range(N):
    original_line=File.next().strip()
    original_tweet=original_line
    processed_line=File1.next().strip()
    processed_tweet=processed_line
    word_features = get_word_features(get_words_in_tweet(processed_tweet))
    classified_score=classifier.classify(extract_features(processed_tweet.split()))
    score=classified_score
    if(score=='4'):
       pos=pos+1
       classified_score='POSITIVE'

    if(score=='0'):
       negat=negat+1
       classified_score='NEGATIVE'

    if(score=='2'):
       neu=neu+1
       classified_score='NEUTRAL '

    classified_Tweet= classified_score+ ":  "+original_tweet
    saveFile=open('classified.txt','a')
    saveFile.write(classified_Tweet)
    saveFile.write('\n')

posfile=open('poscount.txt','w')
posfile.write(str(pos))
posfile.close()
negfile=open('negcount.txt','w')
negfile.write(str(negat))
negfile.close()
neutralfile=open('neutralcount.txt','w')
neutralfile.write(str(neu))
neutralfile.close()
f.close();
File.close();
File1.close();
saveFile.close()


import os, itertools, time, pickle
import subprocess
from xml.dom import minidom
from collections import Counter, OrderedDict
from operator import itemgetter
from scipy import spatial
from sklearn.metrics import precision_score, accuracy_score, recall_score, f1_score
from sklearn.feature_extraction.text import TfidfVectorizer
import re, sys
import numpy as np
import scipy.sparse as sp
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from math import ceil, exp
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence

f = open("data.pkl", "rb")
data, emb_indexer, emb_indexer_inv, emb_vals, gt_mappings  = pickle.load(f)

ontologies_in_alignment = [l.split(".")[0].split("-") for l in os.listdir("reference-alignment/")]
flatten = lambda l: [item for sublist in l for item in sublist]

ind_test, inp_test1, inp_test2 = None, None, None

def write(statement):
    op_file = open("Logs", "a+")
    op_file.write("\n" + str(statement) + "\n")
    op_file.close()

class Ontology():
    def __init__(self, ontology):
        self.ontology = ontology
        self.ontology_obj = minidom.parse(ontology)
        self.root = self.ontology_obj.documentElement
        self.subclasses = self.parse_subclasses()
        self.object_properties = self.parse_object_properties()
        self.data_properties = self.parse_data_properties()
        self.triples = self.parse_triples()
        self.classes = self.parse_classes()

    def get_child_node(self, element, tag):
        return [e for e in element._get_childNodes() if type(e)==minidom.Element and e._get_tagName() == tag]

    def has_attribute_value(self, element, attribute, value):
        return True if element.getAttribute(attribute).split("#")[-1] == value else False

    def get_subclass_triples(self):
        return [(b,a,"subclass_of") for (a,b) in self.get_subclasses()]

    def parse_triples(self, union_flag=0, subclass_of=True):
        obj_props = self.object_properties
        data_props = self.data_properties
        props = obj_props + data_props
        all_triples = []
        for prop in props:
            domain_children = self.get_child_node(prop, "rdfs:domain")
            range_children = self.get_child_node(prop, "rdfs:range")
            domain_prop = self.filter_null([self.extract_ID(el) for el in domain_children])
            range_prop = self.filter_null([self.extract_ID(el) for el in range_children])
            if not domain_children or not range_children:
                continue
            if not domain_prop:
                domain_prop = self.filter_null([self.extract_ID(el) for el in domain_children[0].getElementsByTagName("owl:Class")])
            if not range_prop:
                range_prop = self.filter_null([self.extract_ID(el) for el in range_children[0].getElementsByTagName("owl:Class")])
            if domain_prop and range_prop:
                if union_flag == 0:
                    all_triples.extend([(el[0], el[1], self.extract_ID(prop)) for el in list(itertools.product(domain_prop, range_prop))])
                else:
                    all_triples.append(("###".join(domain_prop), "###".join(range_prop), self.extract_ID(prop)))
        if subclass_of:
            all_triples.extend(self.get_subclass_triples())
        return list(set(all_triples))

    def get_triples(self, union_flag=0, subclass_of=True, include_inv=True):
        return self.parse_triples(union_flag, subclass_of)

    def parse_subclasses(self, union_flag=0):
        subclasses = self.root.getElementsByTagName("rdfs:subClassOf")
        subclass_pairs = []
        for el in subclasses:
            inline_subclasses = self.extract_ID(el)
            if inline_subclasses:
                subclass_pairs.append((el, el.parentNode))
            else:
                level1_class = self.get_child_node(el, "owl:Class")
                if not level1_class:
                    continue
                if self.extract_ID(level1_class[0]):
                    subclass_pairs.append((level1_class[0], el.parentNode))
                else:
                    level2classes = level1_class[0].getElementsByTagName("owl:Class")

                    subclass_pairs.extend([(elem, el.parentNode) for elem in level2classes if self.extract_ID(elem)])
        return subclass_pairs

    def get_subclasses(self):
        return [(self.extract_ID(a), self.extract_ID(b)) for (a,b) in self.subclasses]

    def filter_null(self, data):
        return [el for el in data if el]

    def extract_ID(self, element):
        element_id = element.getAttribute("rdf:ID") or element.getAttribute("rdf:resource") or element.getAttribute("rdf:about")
        return element_id.split("#")[-1]

    def parse_classes(self):
        class_elems = [self.extract_ID(el) for el in self.root.getElementsByTagName("owl:Class")]
        subclass_classes = list(set(flatten([el[:-1] for el in self.triples])))
        return list(set(self.filter_null(class_elems + subclass_classes)))

    def get_classes(self):
        return self.classes

    def get_entities(self):
        entities = [self.extract_ID(el) for el in self.root.getElementsByTagName("owl:Class")]
        return list(set(self.filter_null(entities)))

    def parse_data_properties(self):
        data_properties = [el for el in self.get_child_node(self.root, 'owl:DatatypeProperty')]
        fn_data_properties = [el for el in self.get_child_node(self.root, 'owl:FunctionalProperty') if el]
        fn_data_properties = [el for el in fn_data_properties if type(el)==minidom.Element and
            [el for el in self.get_child_node(el, "rdf:type") if
             self.has_attribute_value(el, "rdf:resource", "DatatypeProperty")]]
        inv_fn_data_properties = [el for el in self.get_child_node(self.root, 'owl:InverseFunctionalProperty') if el]
        inv_fn_data_properties = [el for el in inv_fn_data_properties if type(el)==minidom.Element and
            [el for el in self.get_child_node(el, "rdf:type") if
             self.has_attribute_value(el, "rdf:resource", "DatatypeProperty")]]
        return data_properties + fn_data_properties + inv_fn_data_properties

    def parse_object_properties(self):
        obj_properties = [el for el in self.get_child_node(self.root, 'owl:ObjectProperty')]
        fn_obj_properties = [el for el in self.get_child_node(self.root, 'owl:FunctionalProperty') if el]
        fn_obj_properties = [el for el in fn_obj_properties if type(el)==minidom.Element and
            [el for el in self.get_child_node(el, "rdf:type") if
             self.has_attribute_value(el, "rdf:resource", "ObjectProperty")]]
        inv_fn_obj_properties = [el for el in self.get_child_node(self.root, 'owl:InverseFunctionalProperty') if el]
        inv_fn_obj_properties = [el for el in inv_fn_obj_properties if type(el)==minidom.Element and
            [el for el in self.get_child_node(el, "rdf:type") if
             self.has_attribute_value(el, "rdf:resource", "ObjectProperty")]]
        return obj_properties + fn_obj_properties + inv_fn_obj_properties

    def get_object_properties(self):
        obj_props = [self.extract_ID(el) for el in self.object_properties]
        return list(set(self.filter_null(obj_props)))

    def get_data_properties(self):
        data_props = [self.extract_ID(el) for el in self.data_properties]
        return list(set(self.filter_null(data_props)))

def greedy_matching():
    global batch_size, test_data_t, test_data_f, model, optimizer, emb_indexer_inv, gt_mappings, all_metrics
    all_results = OrderedDict()
    with torch.no_grad():
        all_pred = []
        batch_size = min(batch_size, len(test_data_t))
        num_batches = int(ceil(len(test_data_t)/batch_size))
        batch_size_f = int(ceil(len(test_data_f)/num_batches))

        np.random.shuffle(test_data_t)
        np.random.shuffle(test_data_f)

        for batch_idx in range(num_batches):
            batch_start = batch_idx * batch_size
            batch_end = (batch_idx+1) * batch_size

            batch_start_f = batch_idx * batch_size_f
            batch_end_f = (batch_idx+1) * batch_size_f

            pos_elems = np.array(test_data_t)[batch_start:batch_end]
            neg_elems = np.array(test_data_f)[batch_start_f:batch_end_f]
            optimizer.zero_grad()

            inputs = np.array([generate_data(elem) for elem in list(pos_elems) + list(neg_elems)])
            targets = np.array([1 for i in range(len(pos_elems))] + [0 for i in range(len(neg_elems))])

            indices = np.random.permutation(inputs.shape[0])
            inputs, targets = inputs[indices].transpose(1,0,2), targets[indices]
            inputs_elem = inputs.copy()

            nonzero_elems = np.count_nonzero(inputs, axis=-1)
            indices = np.flip(np.argsort(nonzero_elems, axis=-1), axis=-1)
            seq_lens = np.flip(np.sort(nonzero_elems, axis=-1), axis=-1)
            inputs = np.stack((inputs[0][[indices[0]]], inputs[1][[indices[1]]]), axis=0)

            d1 = {elem:i for i,elem in enumerate(indices[0])}
            d2 = {elem:i for i,elem in enumerate(indices[1])}
            rev_indices = np.stack(([d1[k] for k in range(inputs_elem.shape[1])], [d2[k] for k in range(inputs_elem.shape[1])]))

            rev_indices = torch.LongTensor(rev_indices.T).to(device)
            inputs = torch.LongTensor(inputs.transpose(1,0,2)).to(device)
            seq_lens = torch.LongTensor(seq_lens.copy().T).to(device)
            targets = torch.DoubleTensor(targets)

            outputs = model(inputs, seq_lens, rev_indices)
            outputs = [el.item() for el in outputs]
            #outputs /= torch.sum(outputs, dim=1).view(-1, 1)
            #outputs = [(1-el[1].item()) for el in outputs]

            targets = [True if el.item() else False for el in targets]
#             print (inputs)
            for idx, pred_elem in enumerate(outputs):
                ent1 = emb_indexer_inv[inputs_elem[0][idx][0]]
                ent2 = emb_indexer_inv[inputs_elem[1][idx][0]]
                if (ent1, ent2) in all_results:
                    print ("Error: ", ent1, ent2, "already present")
                all_results[(ent1, ent2)] = (pred_elem, targets[idx])

        #all_results = OrderedDict(sorted(all_results.items(), key=lambda x: x[0], reverse=True))
        #filtered_results = dict()

        #entities_to_assign = set([el[0] for el in list(all_results.keys())])
        #for pair in all_results:
        #    if pair[0] in entities_to_assign:
        #        filtered_results[pair] = all_results[pair]
        #        entities_to_assign.remove(pair[0])

        #entities_to_assign = set([el[1] for el in list(all_results.keys())])
        #for pair in all_results:
        #    if pair[1] in entities_to_assign:
        #        filtered_results[pair] = all_results[pair]
        #        entities_to_assign.remove(pair[1])

        #filtered_results = OrderedDict(sorted(filtered_results.items(), key=lambda x: x[1][0], reverse=True))

        optimum_metrics, opt_threshold = [-1000 for i in range(5)], -1000
        low_threshold = np.min([el[0] for el in all_results.values()]) - 0.02
        high_threshold = np.max([el[0] for el in all_results.values()]) + 0.02
        #low_threshold, high_threshold = 0.9, 1.02
        threshold = low_threshold
        step = 0.01
        while threshold < high_threshold:
            res = []
            for i,key in enumerate(all_results):
                if all_results[key][0] > threshold:
                    res.append(key)
            fn_list = [key for key in gt_mappings if key not in set(res) and not is_valid(test_onto, key)]
            fp_list = [elem for elem in res if not all_results[elem][1]]
            tp_list = [elem for elem in res if all_results[elem][1]]

            tp, fn, fp = len(tp_list), len(fn_list), len(fp_list)
            exception = False

            try:
                precision = tp/(tp+fp)
                recall = tp/(tp+fn)
                f1score = 2 * precision * recall / (precision + recall)
                f2score = 5 * precision * recall / (4 * precision + recall)
                f0_5score = 1.25 * precision * recall / (0.25 * precision + recall)
            except Exception as e:
                print (e)
                exception = True
                step = 0.01
                threshold += step
                continue
            print ("Threshold: ", threshold, precision, recall, f1score, f2score, f0_5score)

            if f1score > optimum_metrics[2]:
                optimum_metrics = [precision, recall, f1score, f2score, f0_5score]
                opt_threshold = threshold

            if threshold > 0.98 and not exception:
                step = 0.0001
            else:
                step = 0.01
            print (step, threshold, exception)
            threshold += step
        print ("Precision: {} Recall: {} F1-Score: {} F2-Score: {} F0.5-Score: {}".format(*optimum_metrics))
        if optimum_metrics[2] != -1000:
            all_metrics.append((opt_threshold, optimum_metrics))
    return all_results

def write(elem):
    f = open("Logs", "a+")
    if type(elem) == list or type(elem) == tuple:
        string = str("\n".join([str(s) for s in elem]))
    else:
        string = str(elem)
    f.write("\n"+string)
    f.close()

inputs3, results3 = None, None

class SiameseNetwork(nn.Module):
    def __init__(self, embedding_dim, hidden_dim, num_layers):
        super().__init__()

        self.embedding_dim = embedding_dim
        self.hidden_dim = hidden_dim
        self.num_layers = num_layers
        self.num_directions = 2

        self.name_embedding = nn.Embedding(len(emb_vals), self.embedding_dim)
        self.name_embedding.load_state_dict({'weight': torch.from_numpy(np.array(emb_vals))})
        self.name_embedding.weight.requires_grad = False

        self.dropout = dropout

        self.lstm = nn.LSTM(self.embedding_dim, self.hidden_dim, self.num_layers, bidirectional=True, batch_first=True)
        self.cosine_sim_layer = nn.CosineSimilarity(dim=1)
        self.bilinear = nn.Bilinear(self.hidden_dim, self.hidden_dim, 1)

    def forward(self, inputs, seq_lens, rev_indices):
        results = []
        inputs = inputs.permute(1,0,2)
        seq_lens, rev_indices = seq_lens.T, rev_indices.T
        #print ("input len: {} seq len: {}, rev len: {}".format(inputs.shape, seq_lens.shape, rev_indices.shape))
        for i in range(2):
            x = self.name_embedding(inputs[i])
            #self.lstm.flatten_parameters()
            packed_inp = pack_padded_sequence(x, seq_lens[i].cpu().numpy(), batch_first=True)
            op, (ht, ct) = self.lstm(x)
            x = ht[2*(self.num_layers-1):].permute(1,0,2)
            x = x[rev_indices[i],:,:]
            results.append(x.reshape(-1, self.num_directions*self.hidden_dim))
        #global inputs3, results3
        #results3 = results
        #inputs3 = inputs
        #x = self.layer1(results[0], results[1])
        #x = F.log_softmax(x)
        x = self.cosine_sim_layer(results[0], results[1])
        return x

def get_one_hop_neighbours(ont, K=1):
    ont_obj = Ontology("conference_ontologies/" + ont + ".owl")
    triples = ont_obj.get_triples()
    entities = [(a,b) for (a,b,c) in triples]
    neighbours_dict = {elem: [elem] for elem in list(set(flatten(entities)))}
    for e1, e2 in entities:
        neighbours_dict[e1].append(e2)
        neighbours_dict[e2].append(e1)

    prop_triples = ont_obj.get_triples(subclass_of=False)
    neighbours_dict_props = {c: [c] for a,b,c in prop_triples}
    for e1, e2, p in prop_triples:
        neighbours_dict_props[p].extend([e1, e2])

    neighbours_dict = {**neighbours_dict, **neighbours_dict_props}

    for elem in ont_obj.get_entities() + ont_obj.get_object_properties() + ont_obj.get_data_properties():
        if elem not in neighbours_dict:
            neighbours_dict[elem] = [elem]

    neighbours_dict = {el: neighbours_dict[el][:1] + sorted(list(set(neighbours_dict[el][1:])))
                       for el in neighbours_dict}
    neighbours_dict = {el: neighbours_dict[el][:int(sys.argv[1])] for el in neighbours_dict}
    neighbours_dict = {ont + "#" + el: [ont + "#" + e for e in neighbours_dict[el]] for el in neighbours_dict}
    return neighbours_dict

def is_valid(test_onto, key):
    return tuple([el.split("#")[0] for el in key]) not in test_onto

def generate_data(elem_tuple):
    op = np.array([[emb_indexer[el] for el in neighbours_dicts[elem.split("#")[0]][elem]] for elem in elem_tuple])
    return op

def generate_input(elems, target):
    inputs = np.array([generate_data(elem) for elem in list(elems)])
    targets = np.array([target for i in range(len(elems))])
    return inputs, targets

neighbours_dicts = {ont: get_one_hop_neighbours(ont) for ont in list(set(flatten(ontologies_in_alignment)))}
max_neighbours = np.max(flatten([[len(el[e]) for e in el] for el in neighbours_dicts.values()]))
neighbours_lens = {ont: {key: len(neighbours_dicts[ont][key]) for key in neighbours_dicts[ont]}
                   for ont in neighbours_dicts}
neighbours_dicts = {ont: {key: neighbours_dicts[ont][key] + ["<UNK>" for i in range(max_neighbours -len(neighbours_dicts[ont][key]))]
              for key in neighbours_dicts[ont]} for ont in neighbours_dicts}

print("Number of neighbours: " + sys.argv[1])

data_items = data.items()
np.random.shuffle(list(data_items))
data = OrderedDict(data_items)

print ("Number of entities:", len(data))
all_ont_pairs = list(set([tuple([el.split("#")[0] for el in l]) for l in data.keys()]))

all_metrics = []

for i in list(range(0, len(all_ont_pairs), 3)):

    test_onto = all_ont_pairs[i:i+3]

    train_data = {elem: data[elem] for elem in data if tuple([el.split("#")[0] for el in elem]) not in test_onto}
    test_data = {elem: data[elem] for elem in data if tuple([el.split("#")[0] for el in elem]) in test_onto}

    torch.set_default_dtype(torch.float64)

    train_test_split = 0.9

    train_data_t = [key for key in train_data if data[key]]
    train_data_f = [key for key in train_data if not data[key]]
    #train_data_f = train_data_f[:int(len(train_data_t))]
#     [:int(0.1*(len(train_data) - len(train_data_t)) )]
#     np.random.shuffle(train_data_f)

    lr = 0.001
    num_epochs = 50
    weight_decay = 0.001
    batch_size = 8
    dropout = 0.3
    batch_size = min(batch_size, len(train_data_t))
    num_batches = int(ceil(len(train_data_t)/batch_size))
    batch_size_f = int(ceil(len(train_data_f)/num_batches))
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    model = SiameseNetwork(512, 250, 1).to(device)

    optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)

    for epoch in range(num_epochs):
        inputs_pos, targets_pos = generate_input(train_data_t, 1)
        inputs_neg, targets_neg = generate_input(train_data_f, 0)
        indices_pos = np.random.permutation(len(inputs_pos))
        indices_neg = np.random.permutation(len(inputs_neg))

        inputs_pos, targets_pos = inputs_pos[indices_pos], targets_pos[indices_pos]
        inputs_neg, targets_neg = inputs_neg[indices_neg], targets_neg[indices_neg]

#        indices = np.random.permutation(len(inputs_pos) + len(inputs_neg))

#        inputs = np.array(list(inputs_pos) + list(inputs_neg))[indices]
#        targets = np.array(list(targets_pos) + list(targets_neg))[indices]

#         inputs = np.array(list(inputs_pos) + list(inputs_neg))
#         targets = np.array(list(targets_pos) + list(targets_neg))

        for batch_idx in range(num_batches):
            batch_start = batch_idx * batch_size
            batch_end = (batch_idx+1) * batch_size

            batch_start_f = batch_idx * batch_size_f
            batch_end_f = (batch_idx+1) * batch_size_f

            inputs = np.concatenate((inputs_pos[batch_start: batch_end], inputs_neg[batch_start_f: batch_end_f]))
            targets = np.concatenate((targets_pos[batch_start: batch_end], targets_neg[batch_start_f: batch_end_f]))

            inp = inputs.transpose(1,0,2)
            nonzero_elems = np.count_nonzero(inp, axis=-1)
            indices = np.flip(np.argsort(nonzero_elems, axis=-1), axis=-1)
            seq_lens = np.flip(np.sort(nonzero_elems, axis=-1), axis=-1)
            inp_elems = np.stack((inp[0][[indices[0]]], inp[1][[indices[1]]]), axis=0).transpose(1,0,2)

            inp_elems = torch.LongTensor(inp_elems).to(device)
            targ_elems = torch.DoubleTensor(targets).to(device)
            optimizer.zero_grad()

            d1 = {elem:i for i,elem in enumerate(indices[0])}
            d2 = {elem:i for i,elem in enumerate(indices[1])}
            rev_indices = np.stack(([d1[k] for k in range(inp.shape[1])],
                                    [d2[k] for k in range(inp.shape[1])]))

            rev_indices = torch.LongTensor(rev_indices.T).to(device)
            seq_lens = torch.LongTensor(seq_lens.copy().T).to(device)
            outputs = model(inp_elems, seq_lens, rev_indices)
            loss = F.mse_loss(outputs, targ_elems)
            loss.backward()
#             break
            optimizer.step()

            if batch_idx%10 == 0:
                print ("Epoch: {} Idx: {} Loss: {}".format(epoch, batch_idx, loss.item()))

    model.eval()

    test_data_t = [key for key in test_data if data[key]]
    test_data_f = [key for key in test_data if not data[key]]

    res = greedy_matching()

print ("Final Results: " + str(np.mean([el[1] for el in all_metrics], axis=0)))
print ("Best threshold: " + str(all_metrics[np.argmax([el[1][2] for el in all_metrics])][0]))


# Import libraries

from flask import Flask, request, jsonify, render_template, url_for
import numpy as np
import pickle
import pickle as p
import pandas as pd

app = Flask(__name__)
# app = Flask(__name__, template_folder='app')

# Load the model
# model = pickle.load(open('Model- sav/logisticregression.pickle','rb'))
# print(model)

# Load the model
model = p.load(open('Model- sav/logisticregression.sav','rb'))
print(model)

@app.route("/")
def home():
    return render_template("index.html")

# @app.route("/api")
# def api_list():
#     """List all available api routes."""
#     return (
#         f"Available Routes:<br>"
#         f"Here goes some api"
    # )

@app.route("/model")
def model_results():
    return render_template('model.html')

@app.route("/stats")
def stats():
    return render_template("stats.html")

@app.route('/results/<crime>/<location>/<season>/<hour>/<lat>/<lon>/<domestic>')
def results(crime,location,season,hour,lat,lon,domestic):
    in_data = {
                'Domestic': 0.0,
                'Latitude': 0.0,
                'Longitude': 0.0,
                'Location Description_AIRPORT': 0.0,
                'Location Description_CHURCH': 0.0,
                'Location Description_COMMERCIAL BUILDING': 0.0,
                'Location Description_CTA': 0.0,
                'Location Description_EDUCATIONAL BUILDING': 0.0,
                'Location Description_FEDERAL PROPERTY': 0.0,
                'Location Description_HOSPITAL': 0.0,
                'Location Description_HOTEL': 0.0,
                'Location Description_OTHER': 0.0,
                'Location Description_PUBLIC ENTERTAINMENT': 0.0,
                'Location Description_PUBLIC OPEN SPACE': 0.0,
                'Location Description_RESIDENCE': 0.0,
                'Location Description_VEHICLE': 0.0,
                'Primary Type_ARSON': 0.0,
                'Primary Type_ASSAULT': 0.0,
                'Primary Type_BATTERY': 0.0,
                'Primary Type_BURGLARY': 0.0,
                'Primary Type_CRIM SEXUAL ASSAULT': 0.0,
                'Primary Type_CRIMINAL DAMAGE': 0.0,
                'Primary Type_CRIMINAL SEXUAL ASSAULT': 0.0,
                'Primary Type_CRIMINAL TRESPASS': 0.0,
                'Primary Type_DECEPTIVE PRACTICE': 0.0,
                'Primary Type_GAMBLING': 0.0,
                'Primary Type_HOMICIDE': 0.0,
                'Primary Type_INTERFERENCE WITH PUBLIC OFFICER': 0.0,
                'Primary Type_INTIMIDATION': 0.0,
                'Primary Type_KIDNAPPING': 0.0,
                'Primary Type_LIQUOR LAW VIOLATION': 0.0,
                'Primary Type_MOTOR VEHICLE THEFT': 0.0,
                'Primary Type_NARCOTICS': 0.0,
                'Primary Type_OFFENSE INVOLVING CHILDREN': 0.0,
                'Primary Type_OTHER': 0.0,
                'Primary Type_OTHER OFFENSE': 0.0,
                'Primary Type_PROSTITUTION': 0.0,
                'Primary Type_PUBLIC PEACE VIOLATION': 0.0,
                'Primary Type_ROBBERY': 0.0,
                'Primary Type_SEX OFFENSE': 0.0,
                'Primary Type_STALKING': 0.0,
                'Primary Type_THEFT': 0.0,
                'Primary Type_WEAPONS VIOLATION': 0.0,
                'HOUR_Afternoon': 0.0,
                'HOUR_Evening': 0.0,
                'HOUR_Morning': 0.0,
                'HOUR_Night': 0.0,
                'SEASON_Autumn': 0.0,
                'SEASON_Spring': 0.0,
                'SEASON_Summer': 0.0,
                'SEASON_Winter': 0.0}
    # print(model)
    crime = "Primary Type_"+crime.upper()
    location = "Location Description_"+location.upper()
    season = "SEASON_"+season.title()
    hour = "HOUR_"+hour.title()
    lat = float(lat)
    lon = float(lon)
    domestic=bool(domestic)

    in_data[crime] = 1.0
    in_data[location] = 1.0
    in_data[season] = 1.0
    in_data[hour] = 1.0
    in_data['Domestic'] = domestic
    in_data['Longitude'] = lon
    in_data['Latitude'] = lat

    data = np.array([x for x in in_data.values()]).reshape(1,-1)

    print(data)

    # data = np.array([crime,location,season,hour,lat,lon,domestic]).reshape(1,-1)
    output = model.predict(data)
    prob = model.predict_proba(data)
    print(prob)

    return jsonify({'result': output.tolist(), 'prob': prob.tolist()})

if __name__ == "__main__":
    app.run(debug=True)


import twitter
from twit_creds import *

# Credentials used to authenticate twitter access
api = twitter.Api(consumer_key=api_key,
                  consumer_secret=secret_key,
                  access_token_key=access_token,
                  access_token_secret=at_secret)

def twitterSearch():
    # gets 100 (the max) english tweets that contain the given term
    # and returns them in a .json form.
    # Slices the .json to grab just the text from the tweet body.
    search = api.GetSearch(term="searchTerm", count=100, lang="en", return_json=True)
    search_words = search['statuses'][0]['text'].split(' ')

    return search_words

def tickerFinder(string):
    # Takes a string of text and locates any stock tickers inside

    possible_stocks = []
    for i in string:
        if len(i) > 0 and i[0] == '$' and len(i) < 6:
            possible_stocks.append(i)

    # List comprehension that makes a list from possible_stocks that
    # only includes stock tickers by getting rid of any terms that have
    # numbers after the first character (a dollar sign)
    stock_list = [i for i in possible_stocks if i[1:].isalpha() == True]

    print("List Of Stock Tickers:")
    print(stock_list)

# ACTIVATION
if __name__ == '__main__':
    tickerFinder(twitterSearch())


import tkinter as tk
from tkinter import messagebox
import requests
import re

class UpdateGymLeaderPopup(tk.Frame):
    """ Popup Frame to Add Trainer """

    def __init__(self, parent, close_callback):
        """ constructor """

        tk.Frame.__init__(self, parent)
        self._close_cb = close_callback
        self.grid(rowspan=2, columnspan=2)

        tk.Label(self, text="Trainer Id").grid(row=1, column=1)
        self._trainer_id = tk.Entry(self)
        self._trainer_id.grid(row=1, column=2)
        tk.Label(self, text="Gym Leader Name").grid(row=2, column=1)
        self._name = tk.Entry(self)
        self._name.grid(row=2, column=2)
        tk.Label(
            self, text="Pokemon #1 - Required - Format: Pokemon, Level").grid(row=3, column=1)
        self._pokemon1 = tk.Entry(self)
        self._pokemon1.grid(row=3, column=2)
        self._pokemon1_level = tk.Entry(self)
        self._pokemon1_level.grid(row=3, column=3)
        tk.Label(
            self, text="Pokemon #2 - Optional - Format: Pokemon, Level").grid(row=4, column=1)
        self._pokemon2 = tk.Entry(self)
        self._pokemon2.grid(row=4, column=2)
        self._pokemon2_level = tk.Entry(self)
        self._pokemon2_level.grid(row=4, column=3)
        tk.Label(
            self, text="Pokemon #3 - Optional - Format: Pokemon, Level").grid(row=5, column=1)
        self._pokemon3 = tk.Entry(self)
        self._pokemon3.grid(row=5, column=2)
        self._pokemon3_level = tk.Entry(self)
        self._pokemon3_level.grid(row=5, column=3)
        tk.Label(
            self, text="Pokemon #4 - Optional - Format: Pokemon, Level").grid(row=6, column=1)
        self._pokemon4 = tk.Entry(self)
        self._pokemon4.grid(row=6, column=2)
        self._pokemon4_level = tk.Entry(self)
        self._pokemon4_level.grid(row=6, column=3)
        tk.Label(
            self, text="Pokemon #5 - Optional - Format: Pokemon, Level").grid(row=7, column=1)
        self._pokemon5 = tk.Entry(self)
        self._pokemon5.grid(row=7, column=2)
        self._pokemon5_level = tk.Entry(self)
        self._pokemon5_level.grid(row=7, column=3)
        tk.Label(
            self, text="Pokemon #6 - Optional - Format: Pokemon, Level").grid(row=8, column=1)
        self._pokemon6 = tk.Entry(self)
        self._pokemon6.grid(row=8, column=2)
        self._pokemon6_level = tk.Entry(self)
        self._pokemon6_level.grid(row=8, column=3)
        tk.Label(self, text="Pokecoins").grid(row=9, column=1)
        self._pokecoins = tk.Entry(self)
        self._pokecoins.grid(row=9, column=2)
        tk.Label(self, text="Location").grid(row=10, column=1)
        self._location = tk.Entry(self)
        self._location.grid(row=10, column=2)
        tk.Label(self, text="Badge").grid(row=11, column=1)
        self._badge = tk.Entry(self)
        self._badge.grid(row=11, column=2)
        tk.Label(self, text="Element").grid(row=12, column=1)
        self._element = tk.Entry(self)
        self._element.grid(row=12, column=2)
        tk.Label(self, text="Prize").grid(row=13, column=1)
        self._prize = tk.Entry(self)
        self._prize.grid(row=13, column=2)
        tk.Button(self, text="Submit", command=self._submit_cb).grid(
            row=15, column=2, pady=(10, 0))
        tk.Button(self, text="Close", command=self._close_cb).grid(
            row=15, column=3, pady=(10, 0))

    def _submit_cb(self):
        """ Submit the Update """

        if re.match("^\d+$", self._pokemon1_level.get()) is None:
            messagebox.showerror(
                "Error", "Pokemon level must be a valid integer")
            return

        if re.match("^\d+$", self._pokecoins.get()) is None:
            messagebox.showerror(
                "Error", "Pokecoins amount must be a valid integer")
            return

        if re.match("^\d+$", self._trainer_id.get()) is None:
            messagebox.showerror(
                "Error", "Trainer id amount must be a valid integer")
            return

        trainer_id = self._trainer_id.get()

        # declaring a temp variable to store pokemon team
        pokemon_team = {}

        # putting the trainer pokemon into a dict
        if self._pokemon1.get() != "":
            try:
                pokemon = self._pokemon1.get()
                level = int(self._pokemon1_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #1 - level must be an integer")
                return
        else:
            messagebox.showerror(
                "Error", "Trainer must have at least one pokemon")
            return

        if self._pokemon2.get() != "":
            try:
                pokemon = self._pokemon2.get()
                level = int(self._pokemon2_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #2 - level must be an integer")
                return

        if self._pokemon3.get() != "":
            try:
                pokemon = self._pokemon3.get()
                level = int(self._pokemon3_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #3 - level must be an integer")
                return

        if self._pokemon4.get() != "":
            try:
                pokemon = self._pokemon4.get()
                level = int(self._pokemon4_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #4 - level must be an integer")
                return

        if self._pokemon5.get() != "":
            try:
                pokemon = self._pokemon5.get()
                level = int(self._pokemon5_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #5 - level must be an integer")
                return

        if self._pokemon6.get() != "":
            try:
                pokemon = self._pokemon6.get()
                level = int(self._pokemon6_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #6 - level must be an integer")
                return

        if pokemon_team == {}:
            messagebox.showerror(
                "Error", "Trainer must have at least one pokemon")
            return

        # Create the dictionary for the JSON request body
        data = {}
        data['name'] = self._name.get()
        data['pokemon_team'] = pokemon_team
        data['trainer_class'] = "Gym Leader"
        data['pokecoins'] = int(self._pokecoins.get())
        data['location'] = self._location.get()
        data['badge'] = self._badge.get()
        data['element'] = self._element.get()
        data['prize'] = self._prize.get()
        data['type'] = "Gym Leader"

        headers = {"content-type": "application/json"}
        response = requests.put("http://127.0.0.1:5000/trainermanager/trainers/" + trainer_id,
                                json=data, headers=headers)

        print(data)

        if response.status_code == 200:
            messagebox.showinfo("Success", "Gym Leader has been updated")
            self._close_cb()
        else:
            messagebox.showwarning(
                "Error", "Gym Leader could not be updated because of invalid data or an incorrect trainer id number")


from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

# Create your models here.

class Client(models.Model):
    user = models.OneToOneField(User)
    street_name = models.CharField(max_length=40)
    street_number = models.IntegerField()
    postal_code = models.CharField(max_length=6)
    city = models.CharField(max_length=40)

    def __unicode__(self):
        return self.user.username

class ClientAdmin(admin.ModelAdmin):
    list_display = ('user', 'street_name', 'street_number', 'city')
    fields = ['street_name', 'street_number', 'postal_code', 'city']

admin.site.register(Client, ClientAdmin)