# -*- coding: utf-8 -*-
"""
Created on Wed Dec 12 00:18:21 2018
@author: shreyasjothish

Description: This file is used to interact with Twitter API
"""

import tweepy
import pandas as pd
import credentials

access_token = credentials.ACCESS_TOKEN
access_token_secret = credentials.ACCESS_TOKEN_SECRET
consumer_key = credentials.CONSUMER_KEY
consumer_secret = credentials.CONSUMER_SECRET

def authenticate():
    """
    Authenticate with Twitter API based on
    consumer_key
    consumer_secret
    access_token
    access_token_secret

    Input:
    None

    Output:
    tweepy api object
    """
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)

    return api

def fetchrelatedtweets(api, tweetid_str, itemcount=100):
    """
    Fetch related tweets based on original tweet id.

    Input:
    api - Twitter authentication object
    tweetid_str - Original tweet id string
    itemcount - Count of number of related tweets to be fetched
    There is an limit on number of tweets which can be fetched based on
    twitter account type

    Output:
    DataFrame of related tweets with key informations listed below.
    1. created_at
    2. id
    3. id_str
    4. text
    5. text_range
    6. lang
    7. user_id
    8. user_id_str
    9. user_name
    10. user_screen_name
    11. retweeted - Set based on retweeted_status
    12. quoted - Set based on quoted_status
    13. polarity - Placeholder for sentiment polarity
    14. subjectivity - Placeholder for sentiment subjectivity

    For more details on the fields refer:
    https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object
    """
    related_tweets = []

    for status in tweepy.Cursor(api.search,
                                q=tweetid_str,
                                tweet_mode='extended').items(itemcount):
        related_tweets.append(status)

    tweetdf = pd.DataFrame()

    # Build the dataframe with key information
    tweetdf['created_at'] = list(map(lambda tweet: tweet.created_at,
    					related_tweets))
    tweetdf['id'] = list(map(lambda tweet: tweet.id, related_tweets))
    tweetdf['id_str'] = list(map(lambda tweet: tweet.id_str, related_tweets))
    tweetdf['text'] = list(map(lambda tweet: tweet.full_text, related_tweets))
    tweetdf['text_range'] = list(map(lambda tweet: tweet.display_text_range,
    					related_tweets))
    tweetdf['lang'] = list(map(lambda tweet: tweet.lang,
    			related_tweets))
    tweetdf['user_id'] = list(map(lambda tweet: tweet.user.id,
    			related_tweets))
    tweetdf['user_id_str'] = list(map(lambda tweet: tweet.user.id_str,
    					related_tweets))
    tweetdf['user_name'] = list(map(lambda tweet: tweet.user.name,
    					related_tweets))
    tweetdf['user_screen_name'] = list(map(
            lambda tweet: tweet.user.screen_name, related_tweets))
    tweetdf['retweeted'] = list(map(
            lambda tweet: int(hasattr(tweet,'retweeted_status')),
            related_tweets))
    tweetdf['quoted'] = list(map(
            lambda tweet: int(hasattr(tweet,'quoted_status')), related_tweets))
    tweetdf['polarity'] = list(map(lambda tweet: 0.0,
    					related_tweets))
    tweetdf['subjectivity'] = list(map(lambda tweet: 0.0,
    					related_tweets))

    return tweetdf


# encoding: utf-8

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from xmuda.models.LMSCNet import SegmentationHead
from xmuda.models.context_prior import ContextPrior3D
from xmuda.models.context_prior_v2 import ContextPrior3Dv2
from xmuda.models.CP_baseline import CPBaseline
from xmuda.models.CP_implicit import CPImplicit
from xmuda.models.CP_v5 import CPMegaVoxels
from xmuda.models.DDR import Bottleneck3D
from functools import partial
from collections import OrderedDict
from xmuda.models.modules import Process, Upsample, Downsample

class Decoder3D(nn.Module):
    def __init__(self, class_num, norm_layer,
                 scene_sizes,
                 features,
                 agg_k=None,
                 n_relations=4,
                 project_res=[],
                 context_prior=None,
                 bn_momentum=0.1):
        super(Decoder3D, self).__init__()
        self.business_layer = []
        self.project_res = project_res

        self.feature_1_4 = features[0]
        self.feature_1_8 = features[1]

        self.feature_1_8_dec = self.feature_1_8
        self.feature_1_4_dec = self.feature_1_4
        self.size_1_4 = scene_sizes[0]
        self.size_1_8 = scene_sizes[1]

#        self.completion_head = CompletionHead(norm_layer, bn_momentum)

        self.process_1_4 = nn.Sequential(
            Process(self.feature_1_4, norm_layer, bn_momentum, dilations=[1, 2, 3]),
        )
        self.process_1_8 = nn.Sequential(
            Process(self.feature_1_8, norm_layer, bn_momentum, dilations=[1, 2, 3]),
        )

        self.down_4_8 = Downsample(self.feature_1_4, norm_layer, bn_momentum)

        self.up_1_8_1_4 = Upsample(self.feature_1_8_dec, self.feature_1_4_dec, norm_layer, bn_momentum)

        self.ssc_head_1_4 = SegmentationHead(self.feature_1_4_dec, self.feature_1_4_dec, class_num, [1, 2, 3])

        self.context_prior = context_prior
        print("context_prior", self.context_prior)
        if context_prior == "CRCP":
            self.CP_mega_voxels = CPMegaVoxels(self.feature_1_8,
                                               self.feature_1_8,
                                               self.size_1_8,
                                               n_relations=n_relations,
                                               bn_momentum=bn_momentum)
#
    def forward(self, input_dict):
        res = {}

        x3d_1_4 = input_dict['x3d']

        x3d_1_4 = self.process_1_4(x3d_1_4)
        x3d_1_4_down_1_8 = self.down_4_8(x3d_1_4)

        x3d_1_8 = x3d_1_4_down_1_8
        x3d_1_8 = self.process_1_8(x3d_1_8)

        if self.context_prior == "CRCP":
            ret = self.CP_mega_voxels(x3d_1_8)
            x3d_1_8_CP = ret['x']
            for k in ret.keys():
                res[k] = ret[k]
        else:
            x3d_1_8_CP = x3d_1_8

#        x3d_up_1_16 = self.process_1_16_dec(x3d_1_16_CP)

#        x3d_up_1_8 = self.up_1_16_1_8(x3d_up_1_16) + x3d_1_8

        x3d_up_1_4 = self.up_1_8_1_4(x3d_1_8_CP) + x3d_1_4

        ssc_logit_1_4 = self.ssc_head_1_4(x3d_up_1_4)

        res['ssc'] = ssc_logit_1_4

        return res

if __name__ == '__main__':
    model = Network(class_num=12, norm_layer=nn.BatchNorm3d, feature=128, eval=True)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = model.to(device)
    model.eval()

    left = torch.rand(1, 3, 480, 640).cuda()
    right = torch.rand(1, 3, 480, 640).cuda()
    depth_mapping_3d = torch.from_numpy(np.ones((1, 129600)).astype(np.int64)).long().cuda()
    tsdf = torch.rand(1, 1, 60, 36, 60).cuda()

    out = model(left, depth_mapping_3d, tsdf, None)


import json
import boto3

#Using client for AWS service access
s3 = boto3.client('s3')

#Method to read object from s3 bucket
def lambda_handler(event, context):
       print('Started Execution of lanmba_handler for reading object and downloading file from s3 bucket')

       bucket = 'sample_bucket'
       new_bucket= 'new_sample'
       key = 'content/file1.json'

       #Downloading file/image from s3 bucket to local machine
       s3.download_file('sample_bucket', 'sea.png', 'Users/RR/Desktop/sea.png')

       #uploading downloaded file/image to another bucket
       response = s3.upload_file('Users/RR/Desktop/sea.png', new_bucket, 'saved_seaImage.png' )

       #Placing code within try block to handle run time exceptions
       try:
           #get_object method to return dictionary
           data = s3.get_object(Bucket=bucket, Key=key)
           #Store Body which holds streamingBody in output variable
           output = data['Body'].read()

           #return output values from lamba_handler
           return output
       #Handling exception and printing exception details

       except Exception as e:
           print(e)
           raise e


from modelLib import *
from trainUtils import *
import customLoss
import os
import gc
import pickle
from optparse import OptionParser

from keras.models import load_model
from keras.optimizers import Adam, RMSprop
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, CSVLogger, ReduceLROnPlateau

from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import GroupKFold

# suprresing tensorflow messages
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
configTF = tf.ConfigProto()
configTF.gpu_options.allow_growth = True
sess = tf.Session(config=configTF)

tf.set_random_seed(1306)
np.random.seed(1306)

def main():

	parser = OptionParser()

	parser.add_option("-l", "--load_model", dest="loadModel", default=False)
	parser.add_option("-c", "--continue", dest="continueTraining",action='store_true', default=False)

	parser.add_option("--vb", dest="verbose", action='store_true', default=False)
	parser.add_option("--nw", "--numWorkers", dest="numWorkers", default=4)
	parser.add_option("-e", "--epochs", dest="epochs", default=80)
	parser.add_option("--bs", "--batchSize", dest="batchSize", default=20)
	parser.add_option("--ed", "--extendDim", dest="extendDim", action='store_true', default=False)
	parser.add_option("--lr", "--learning_rate",dest="learningRate", default=0.001)
	parser.add_option("-p", "--patience", dest="patience", default=50)

	(options, args) = parser.parse_args()

	# database to use
	# dbPath = '../dbHdf5/dataset1_2d_onlyTumor_cropped_x-75-425_y-75-425_aug-R90-R180-R270-Hf-Vf-Et-Compounded.hdf5'
	dbPath = '/media/shahruk/Pluto/dataset1_3d_n-1_mask-1_onlyTumor_cropped_x-75-425_y-75-425_aug-R90-R180-R270-Hf-Vf-Et_clipped-600.hdf5'

	modelDir = '../models'              # where models are saved
	modelArch = 'maskNet003'            # model architecture to use from modelLib.py
	modelName = 'maskNet003_001'        # name to save model with

	epochs = int(options.epochs)
	batchSize = int(options.batchSize)
	numWorkers = int(options.numWorkers)

	modelFolder = os.path.join(modelDir, modelName)
	weightsFolder = os.path.join(modelFolder, "weights")
	ensureDir(weightsFolder)

	notes = "Model trained on 3d augmented data, cropped, resampled, clipped to 600 and rescaled to between 0 and 1, rot 90 180 270, hor flip, \
			 ver flip and elastic - NOT compounded).Using Dice Loss."

	with open(os.path.join(modelFolder, "trainingData.txt"), "w") as df:
		df.write("Dataset\t%s\n" % dbPath)
		df.write("Architecture\t%s\n" % modelArch)
		df.write("Batch Size\t%s\n" % batchSize)
		df.write("Notes\t%s\n" % notes)

	db = h5py.File(dbPath, 'r')
	cases = db['case'][...]
	db.close()

	x_index = np.arange(0,len(cases))
	y_index = np.arange(0,len(cases))

	group_kfold = GroupKFold(n_splits=5)
	group_kfold.get_n_splits(x_index, y_index, cases)

	kdx = 0
	for train_index, test_index in group_kfold.split(x_index, y_index, cases):
		kdx += 1
		# if kdx <=3:
		# 	continue
		with open(os.path.join(modelFolder, "trainingData.txt"), "a") as df:
			df.write("\nTraining Cases for CV-%d (%d)\t" % (kdx, len(train_index)))
			df.write("\t".join(np.unique(cases[train_index])))
			df.write("\n")
			df.write("Test Cases for CV-%d (%d)\t" % (kdx, len(test_index)))
			df.write("\t".join(np.unique(cases[test_index])))
			df.write("\n")

		bestModelPath = os.path.join(weightsFolder, "best_fold_%02d.hdf5" % kdx)
		ensureDir(bestModelPath)

		# creating model
		model = makeModel(modelArch, verbose=options.verbose)
		model.save(os.path.join(modelFolder, modelName + '.h5'))

		adam = Adam(lr=float(options.learningRate), beta_1=0.9,beta_2=0.999, epsilon=1e-06, decay=0.00001)

		model.compile(loss=[customLoss.dice_coef_loss], optimizer=adam)

		# loading model
		if options.loadModel:
			print("\n\nLoading Model Weights:\t %s" % modelName)
			model = load_model(bestModelPath)
			log = np.genfromtxt(os.path.join(
				modelFolder,  modelName + '_trainingLog.csv'), delimiter=',', dtype=str)[1:, 0]
			epochStart = len(log)
		else:
			epochStart = 0

		print("\nCross Validation Fold : %02d \n" % kdx)

		trainGen = dataGenerator(dbPath,'slice','mask',batchSize, dataIndex=train_index, extendDim=options.extendDim,numWorkers=numWorkers)
		testGen = dataGenerator(dbPath,'slice','mask',batchSize, dataIndex=test_index, extendDim=options.extendDim,numWorkers=numWorkers)

		# callbacks
		check1 = ModelCheckpoint(os.path.join(weightsFolder, modelName + "_fold_%02d" % kdx +"_{epoch:02d}-loss-{val_loss:.3f}.hdf5"), monitor='val_loss', save_best_only=True, mode='auto')
		check2 = ModelCheckpoint(bestModelPath, monitor='val_loss', save_best_only=True, mode='auto')
		check3 = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=int(options.patience), verbose=0, mode='auto')
		check4 = CSVLogger(os.path.join(modelFolder, modelName +'_trainingLog.csv'), separator=',', append=True)
		check5 = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=int(options.patience)//1.5, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=1e-10)

		print("\nInitiating Training:\n")
		trained_model = model.fit_generator(trainGen, steps_per_epoch=(len(train_index) // batchSize), epochs=epochs, initial_epoch=epochStart,
											validation_data= testGen, validation_steps=(len(test_index) // batchSize), callbacks=[check1,check2,check3,check4,check5],
											verbose=1)
		trainGen.close()
		testGen.close()
		gc.collect()

		# closing hdf5 db in case it was left open due to unexpected shutdown
		for obj in gc.get_objects():
			try:
				if isinstance(obj, h5py.File):
					try:
						obj.close()
					except:
						pass # Was already closed
			except:
				pass

if __name__ == "__main__":
	main()


from app.main.models import ItemsProcessed
from django.contrib import admin

@admin.register(ItemsProcessed)
class ItemsProcessedAdmin(admin.ModelAdmin):
    """Admin for article catalog."""

    list_display = (
        "items",
        "result",
        "is_active",
        "updated_at",
    )

    search_fields = ("items", "result")

    ordering = ("-updated_at",)


# [SublimeLinter @python:3]

import sys
from pathlib import Path
import shutil

def main(argv=sys.argv):
    for x in Path().glob("**/__pycache__"):
        if x.is_dir():
            shutil.rmtree(x)

    for x in Path().glob("**/*.pyc"):
        if x.is_file():
            x.unlink()

    if Path("build").exists():
        shutil.rmtree("build")

    if Path("build_exe").exists():
        shutil.rmtree("build_exe")

    for mask in ("*.c", "*.pyd", "*.pyx"):
        for file in Path("dufi/commands").glob(mask):
            file.unlink()

    for d in Path().glob("dufi-*"):
        if d.is_dir():
            shutil.rmtree(d)

    if Path("config.bat").exists():
        Path("config.bat").unlink()

    print("clean.py: done!")

if __name__ == "__main__":
    sys.exit(main())


from plumbum import (cli, local)
from datetime import datetime
from collections import OrderedDict
import pandas as pd
import yaml
import os
import sys
sys.path.append(os.path.join(local.path(__file__).dirname, 'db'))

from db.db_service import DbService
class Collaborator(cli.Application):
    PROGNAME = "Collaborator"
    VERSION = "1.0"
    DESCRIPTION = "This is a command line tool to store and query professional social networking information."
    # ********************************* read interface ********************************************

    # Types of acceptable files at command line (friend not implemented)
    _valid_file_types = ('skill', 'project', 'organization', 'interest', 'distance', 'user', 'friend')
    # Structure of csv columns
    _valid_file_structures = {
            _valid_file_types[0] : OrderedDict({'userid': int, 'skill': str, 'skilllevel': int}),
            _valid_file_types[1] : OrderedDict({'userid': int, 'project': str}),
            _valid_file_types[2] : OrderedDict({'userid': int, 'organization': str, 'organizationtype': str}),
            _valid_file_types[3] : OrderedDict({'userid': int, 'interest': str, 'interestlevel': int}),
            _valid_file_types[4] : OrderedDict({"organization1": str, "organization2": str, "distance": int}),
            _valid_file_types[5] : OrderedDict({'userid': int, 'firstname': str, 'lastname': str}),
            # Not implemented
            _valid_file_types[6] : OrderedDict({'userid1': int, 'userid2':int})
    }

    _yaml_option = cli.SwitchAttr(
        ['-y', '--yaml-file'], mandatory=False, default='load_all_data',
        help='In the yaml file specified at path from --input-many switch, can specify which attribue of yaml file you want to download. load_all_data is used by default. Must be used before the --input-many switch to work.'
    )

    _db_service = None

    def connect_to_db(self):
        if not self._db_service:
            print('Connecting to databases at ... {0}\n'.format(datetime.now()))
            self._db_service = DbService()

    @cli.switch(['-m', '--input-many'], str)
    def input_lots_of_data(self, file_path_to_yaml):
        """ Input a list of csv files to input into input_data """
        self.connect_to_db()
        yaml_file = None
        with open(file_path_to_yaml) as raw_config_file:
            try:
                yaml_file  = yaml.load(raw_config_file, yaml.FullLoader)
            except yaml.YAMLError as err:
                raise err
        for i in yaml_file[self._yaml_option]:
            self.input_data(i)
        print('All data loaded at ... {0}'.format(datetime.now()))

    # will input a file named file_name that is input data of file_type

    @cli.switch(['-o', '--input-one'], str)
    def input_data(self, file_path_and_type):
        """
        Input data, enter a string of form file_type,file_path
        """
        self.connect_to_db()
        file_path, file_type = file_path_and_type.split(',')
        print('Reading file of type {0} from {1} at ... {2}'.format(file_type, file_path, datetime.now()))
        print('Check if input is valid at ... {0}'.format(datetime.now()))
        # Error validation
        self.is_file(file_path)
        self.is_csv(file_path)
        self.is_valid_file_type(file_type, self._valid_file_types, self._valid_file_structures, file_path)
        print('Input validated at ... {0}\n'.format(datetime.now()))
        print('Attempting to read data from {0} at ... {1}'.format(file_path, datetime.now()))
        input_data = self.read_data(file_path, file_type)
        print('Data read and cleaned at ... {0}\n'.format(datetime.now()))
        if file_type == self._valid_file_types[0]:
            # Skill switch
            print("Loading {0} data at ... {1}\n\n".format(self._valid_file_types[0].upper(), datetime.now()))
            self._db_service.add_many_new_skill_nodes(input_data)
        elif file_type == self._valid_file_types[1]:
            # Project switch
            print("Loading {0} data at ... {1}\n\n".format(self._valid_file_types[1].upper(), datetime.now()))
            self._db_service.add_many_new_project_nodes(input_data)
        elif file_type == self._valid_file_types[2]:
            # Organization switch
            print("Loading {0} data at ... {1}\n\n".format(self._valid_file_types[2].upper(), datetime.now()))
            self._db_service.add_many_new_organization_nodes(input_data)
        elif file_type == self._valid_file_types[3]:
            # Interest switch
            print("Loading {0} data at ... {1}\n\n".format(self._valid_file_types[3].upper(), datetime.now()))
            self._db_service.add_many_new_interest_nodes(input_data)
        elif file_type == self._valid_file_types[4]:
            # Distance switch
            print("Loading {0} data at ... {1}\n\n".format(self._valid_file_types[4].upper(), datetime.now()))
            self._db_service.add_many_new_distance_rels(input_data)
        elif file_type == self._valid_file_types[5]:
            # Skill associated with user switch
            print('Loading {0} data at  ... {1}\n\n'.format(self._valid_file_types[5].upper(), datetime.now()))
            self._db_service.add_many_new_user_nodes(input_data)

    def read_data(self, file_path, file_type):
        data_to_load = pd.read_csv(file_path)
        data_to_load = self.normalize_headers(data_to_load)
        types_to_convert = []
        for (key, value) in self._valid_file_structures[file_type].items():
            if value ==  int:
                types_to_convert.append(key)
        for data_type in types_to_convert:
            data_to_load[data_type] = pd.to_numeric(data_to_load[data_type], errors='coerce')
            data_to_load = data_to_load.dropna()
            data_to_load[data_type] = data_to_load[data_type].astype(int)
        return data_to_load

    def normalize_headers(self, data_frame):
        data_frame.columns = list(map(lambda x : x.replace('_','').replace(' ','').lower(), list(data_frame.columns.values)))
        return data_frame

    def is_file(self, file_path):
        if local.path(file_path).is_file():
            pass
        else:
            print("Failed to find file path at ... {0}".format(datetime.now()))
            err_message = "{0} is not a file, exiting".format(file_path)
            raise TypeError(err_message)

    def is_csv(self, file_path):
        if local.path(file_path).suffix != '.csv':
            print("Failure: not a csv file at ... {0}".format(datetime.now()))
            err_message = 'Invalid file format for {0}, must be a csv file, exiting'.format(file_path)
            raise TypeError(err_message)

    def is_valid_file_type(self, file_type, file_types, file_structure, file_path):
        if file_type not in file_types:
            print("Failed to match valid data input types at ... {0}".format(datetime.now()))
            err_message = 'Invalid file input type "{0}", exiting'.format(file_type)
            raise TypeError(err_message)
        data_to_load = self.normalize_headers(pd.read_csv(file_path))
        headers = list(data_to_load.columns)
        if list(file_structure[file_type].keys()) != headers:
            print("Failed to match valid proper csv data structure  at ... {0}".format(datetime.now()))
            err_message = 'Invalid file structure at "{0}", exiting'.format(file_path)
            raise TypeError(err_message)

    @cli.switch(['-d', '--delete-all-data'])
    def delete_all_data(self):
        """
        deletes all data in both databases. Warning provided before deletion.
        """
        self.connect_to_db()
        self._db_service.delete_all_data()

    @cli.switch(['-u','--update-specific-entity' ], str)
    def update_entity(self, raw_info_string):
        """
        Updates documents based on project, user, skill, interest, or organization.
        Can store any type of information, but it cannot modify read only properties.
        Read only properties are the ones that were read in on entity creation from the
        dataset provided. Input is of form entity-type,key
        """
        self.connect_to_db()
        label, key = raw_info_string.split(',')
        if label not in ('user', 'project', 'skill', 'interest', 'organization'):
            print('Label {0} not found. Exiting'.format(label))
            sys.exit(1)
        if label == 'user':
            try:
                key = int(key)
            except ValueError as err:
                raise err
        self._db_service.update_specific_info(label, key)

    #******************* read interface: *********************************
    @cli.switch(["--about"])
    def about(self):
        """
        A little bit of information about collaborator
        """
        print()
        print("Welcome to collaborator. A command line tool to store and query professional")
        print("social networking information. Please read the README.md document attached for details")
        print("on how to use the application.")
        print()

    @cli.switch(['-a', '--get-all'], str)
    def get_all_of_node_type(self, node_type):
        """
        Flag to retrieve nodes of a particular type. Valid input types are interest,
        skill, project, user, and organization.
        Output will be all nodes of that type and there data outputted to terminal.
        """
        self.connect_to_db()
        print('Searching for data of type {0} at ... {1}\n'.format(node_type, datetime.now()))
        valid_node_types = self._valid_file_types[:4] + (self._valid_file_types[5],)
        if node_type in (valid_node_types):
            self._db_service.get_all_of_node_type(node_type)
        else:
            print('Node type {0} is not a valid type.\nExiting at {1}'.format(node_type, datetime.now()))

    @cli.switch(['-s', '--get-specific-information'], str)
    def get_specific_information(self, raw_info_string):
        """
        This switch will allow retrieval of specific information based on the key of a node type.
        The input options are: (user,some_user_id_integer), (project,some_project_str),
        (organization,some_org, (skill,some_skill_str) , (interest,some_interest_str).
        """
        self.connect_to_db()
        label, key = raw_info_string.split(',')
        if label not in ('user', 'project', 'skill', 'interest', 'organization'):
            print('Label {0} not found. Exiting'.format(label))
            sys.exit(1)
        if label == 'user':
            try:
                key = int(key)
            except ValueError as err:
                raise err
        print('Retrieving information about {0} with key {1} at ... {2}\n'.format(label, key, datetime.now()))
        info_list = self._db_service.get_specific_info(label, key)
        if info_list:
            print('Information found about {0} with key {1} at ... {2}\n'.format(label, key, datetime.now()))
            for node in info_list:
                print_value = ''
                for (key, value) in node.items():
                    if (key != '_id'):
                        print("\t" + key + ": " + str(value) + ";")
            print('\nInformation Retrieval finished at ... {0}\n'.format(datetime.now()))
        else:
            print('No information found about {0} with key {1}. at ... {2}'.format(label, key, datetime.now()))

    @cli.switch(['-t','--trusted-collegues'], int)
    def trusted_collegues(self, user_id):
        """
        Based off of network, this shows people who are trusted collegues of your collegues.
        These include people who have similar interests and have worked on the same projects
        """
        self.connect_to_db()
        raw_input_string = input('Please enter if you would like to match by skill or interest (s/i): ')
        while raw_input_string not in ('i', 's'):
            raw_input_string = input('Incorrect input. Please enter if you would like to match by skill or interest (s/i) : ')
        print()
        is_skill = False
        if raw_input_string == 's':
            is_skill = True
        print('Fetching trusted collegues of collegues at ... {0}'.format(datetime.now()))
        rec = list(self._db_service.get_trusted_c_of_c(user_id, is_skill))
        if not rec:
            print('Fetch retrieved no results at ... {0}.\nExiting'.format(datetime.now()))
            sys.exit(0)
        rec = [dict(i) for i in rec]
        print('Fetch retrieved at ... {0}\n'.format(datetime.now()))
        for result in rec:
            #main_user, other_user, role, project, level, interest = result
            main_user = dict(result['main_user'])
            other_user = dict(result['other_users'])
            role = dict(result['role'])
            project = dict(result['projects'])
            level = dict(result['level'])
            i_or_s = None
            if is_skill:
                i_or_s = dict(result['skills'])
            else:
                i_or_s = dict(result['interests'])
            print('\tUser {0} with id {1} is recommended to trust {2} with id {3}'
                    .format(' '.join([main_user['first_name'], main_user['last_name']]),
                        main_user['user_id'],
                        ' '.join([other_user['first_name'], other_user['last_name']]),
                        other_user['user_id']))
            print('\tThis is based on both working on project {0} and having similar',
                  '{1} {2}'.format(project['project_name'], 'skill' if is_skill else 'interest',
                  i_or_s.get('skill_name') if is_skill else i_or_s.get('interest_name')))
            print()
        print('Retrieved and outputted at ... {0}.\nExiting'.format(datetime.now()))

    @cli.switch(['-r', '--rec-to-meet'], int)
    def people_on_path_of(self, user_id):
        """
        Based off of network, recommends people you may have met or that you may want to meet.
        It bases it off of similar interest or skills, and people who either work at the same
        company or work at a company within a close distance (under 10 miles)
        """
        self.connect_to_db()
        print('Looking for recommendations for user {0} at ... {1}'.format(user_id, datetime.now()))
        #retrieve_recommendations
        rec_list = list(self._db_service.retrieve_recommendations(user_id))
        print('Fetch finished at ... {0}'.format(datetime.now()))
        print('Sorting recommendation at .. {0}'.format(datetime.now()))
        sorted_rec_dict = OrderedDict()
        for rec in rec_list:
            items = list(rec)
            for i in range(0, len(items), 1):
                if items[i]:
                    items[i] = dict(items[i])
                else:
                    items[i] = None
            main_user, interest_skill, other_user, organization_of_main, dist, organization_of_other, i_lvl_1, i_lvl_2  = items
            main_user_info = None
            if not sorted_rec_dict.get('main_user'):
                 main_user_info = (main_user['first_name'] + ' ' + main_user['last_name'], main_user['user_id'])
            if not dist:
                if not organization_of_main['organization_name'] == organization_of_other['organization_name']:
                    continue
            skill_or_interest_prop = None
            if i_lvl_1.get('skill_level'):
                skill_or_interest_prop = 'skill_level'
            else:
                skill_or_interest_prop = 'interest_level'
            if sorted_rec_dict.get(other_user['user_id']):
                sorted_rec_dict[other_user['user_id']][0] += i_lvl_1[skill_or_interest_prop] * i_lvl_2[skill_or_interest_prop]
                sorted_rec_dict[other_user['user_id']][5].append(interest_skill[skill_or_interest_prop.replace('level', 'name')])
            else:
                sorted_rec_dict[other_user['user_id']] = [
                                                      i_lvl_1[skill_or_interest_prop] * i_lvl_2[skill_or_interest_prop],
                                                      (other_user['first_name'] + ' ' + other_user['last_name'], other_user['user_id']),
                                                      organization_of_main['organization_name'],
                                                      organization_of_other['organization_name'],
                                                      dist,
                                                      [interest_skill[skill_or_interest_prop.replace('level', 'name')]]
                                                      ]

        sorted_rec_dict = OrderedDict(sorted(sorted_rec_dict.items(), key= lambda x: x[1][0], reverse=True))
        print('Finished sorting at ... {0}\n'.format(datetime.now()))
        print('Displaying results at ... {0}\n'.format(datetime.now()))
        for final_rec in sorted_rec_dict.values():
            print('\tWe recommend user {0} with id {1} to meet user {2} with id {3}'
                    .format(main_user_info[0], main_user_info[1],
                            final_rec[1][0], final_rec[1][1]))
            s_i_key = ('interest', 'interest_name')
            if 'skill_name' in interest_skill:
                s_i_key = ('skill', 'skill_name')
            print("\tThis is based on shared interest or skills {0} with collective weight {1}".format(', '.join(final_rec[5]), final_rec[0]))
            if final_rec[4]:
                print("\tThis is based on organization {0} and {1} being only {2} miles apart"
                        .format(final_rec[2], final_rec[3], final_rec[4]['distance']))
            else:
                print("\tThis is based on both people working at {0}".format(final_rec[2]))
            print()
        print('Recommendation finished at ... {0}'.format(datetime.now()))

    def main(self):
        pass

if __name__=='__main__':
    Collaborator.run()


# -- Format transcriptions + translation into English (if necessary)
from   googletrans import Translator
import pandas as pd
import itertools
import spacy
import json
import re
import os

BASE_DIR  = '/home/runner/work/Castena/Castena'
TRANSLATE = False

list_original_transcriptions   = [file for file in os.listdir(BASE_DIR + '/data/original_spanish_transcriptions/') if '.txt' in file]
list_translated_transcriptions = [file for file in os.listdir(BASE_DIR + '/data/translated_transcriptions/') if '.txt' in file]

translator = Translator(service_urls=['translate.googleapis.com'])
nlp = spacy.load('es_core_news_sm')

# -- Define auxiliar functions
def translate_text(text, target_lang='en'):
    # Tokenize text and find proper nouns (omit translation in these cases)
    doc = nlp(text)
    named_entities = [ent.text for ent in doc if ent.pos_ == 'PROPN' and ent.dep_ in ['NNP', 'NN']]
    named_entities_list = []
    # Replace proper nouns for temporal __identifier__
    for entity in named_entities:
        text = text.replace(entity, f'__{entity}__')
        named_entities_list.append(entity)

    translated_text = translator.translate(text, dest=target_lang).text
    final_translated_text = []

    i = 0
    for text in translated_text.split(' '):
      if '__' in text and len(named_entities_list):
        final_translated_text.append(named_entities_list[i])
        i+=1
      else:
        final_translated_text.append(text)
    return ' '.join(final_translated_text)

def capitalize_proper_nouns(text):
    # Text analysis with SpaCy
    doc = nlp(text)

    modified_text = []

    for token in doc:
        if token.pos_ not in ["PROPN", "NOUN"]:
            # Si es un nombre propio y ya está en mayúscula, conservarlo
            modified_text.append(token.text)
        else:
            # De lo contrario, capitalizar la primera letra
            modified_text.append(token.text.capitalize())

    modified_text = " ".join(modified_text)

    # Correct punctuation signs
    modified_text = modified_text.replace(" .. ", "...")\
                                 .replace(" , ", ", ")\
                                 .replace(" . ", ". ")\
                                 .replace(" .... ", "...")\
                                 .replace(" ... ", "...")\
                                 .replace(",¿", " ¿")\
                                 .replace(",.", ",")\
                                 .replace("?.", "? ")
    return modified_text

# -- For loop to format every original transcription (if not already processed)
for original_transcription in list_original_transcriptions:
	if original_transcription not in list_translated_transcriptions:
		transcription_df = pd.read_table(BASE_DIR + '/data/original_spanish_transcriptions/' + original_transcription, sep='|', header=None)
		transcription_df.rename(columns={0: 'time', 1: 'speaker', 2: 'transcript'}, inplace=True)

		transcription_df['time'] = pd.to_timedelta(transcription_df['time'])
		transcription_df['speaker_change'] = transcription_df['speaker'] != transcription_df['speaker'].shift()

		result = transcription_df.groupby(['speaker', transcription_df['speaker_change'].cumsum()]).agg({\
	                                                                               'time': ['min', 'max'],
	                                                                               'transcript': lambda x: '.'.join(x)
	                                                                               })
		result.columns = result.columns.droplevel()
		result.columns = ['min_time', 'max_time', 'transcript']
		result.reset_index(inplace=True)
		result['min_time'] = result['min_time'].apply(lambda x: str(x).replace('0 days ', ''))
		result['max_time'] = result['max_time'].apply(lambda x: str(x).replace('0 days ', ''))

		# -- Preprocess transcript
		result['transcript'] = result['transcript'].apply(capitalize_proper_nouns)

		result['literal_transcript'] = 'Desde el instante ' + result['min_time'] + ' hasta ' + result['max_time'] +\
						' ' + result['speaker'] + ' dice: \"' + result['transcript'] + '\"'
		if TRANSLATE:
			result['literal_transcript'] = result['literal_transcript'].apply(translate_text)

		result = result.sort_values('min_time')
		result_text = '\n\n'.join(result['literal_transcript'])

		# -- Save text as .txt file
		with open(BASE_DIR + '/data/translated_transcriptions/' + original_transcription, 'w') as f:
			f.write(result_text)

print("Processing finished successfully!")


import numpy as np
import pandas as pd
from climate_model.functions import q2
from scipy.interpolate import interp1d
from params_control import PARAMS_CONTROL
from params import minutos

data      = pd.read_csv('Inputs_Bleiswijk.csv')
KP4       = PARAMS_CONTROL['KP4']
KI4       = PARAMS_CONTROL['KI4']
KD4       = PARAMS_CONTROL['KD4']

KP10      = PARAMS_CONTROL['KP10']
KI10      = PARAMS_CONTROL['KI10']
KD10      = PARAMS_CONTROL['KD10']

VENTSET1 = 25
VENTSET2 = 25

NIGHT_PAR = 5

KP8       = PARAMS_CONTROL['KP8']
KI8       = PARAMS_CONTROL['KI8']
KD8       = PARAMS_CONTROL['KD8']

VENTSET1  = PARAMS_CONTROL['VENTSET1']
VENTSET2  = PARAMS_CONTROL['VENTSET2']

NIGHT_PAR = PARAMS_CONTROL['NIGHT_PAR']
OTHER_PAR = PARAMS_CONTROL['OTHER_PAR']

PBAND1    = PARAMS_CONTROL['PBAND1']
PBAND2    = PARAMS_CONTROL['PBAND2']

T_OUT1    = PARAMS_CONTROL['T_OUT1']
T_OUT2    = PARAMS_CONTROL['T_OUT2']

VPD_SET1  = PARAMS_CONTROL['VPD_SET1']
VPD_SET2  = PARAMS_CONTROL['VPD_SET2']

HEATSET1  = PARAMS_CONTROL['HEATSET1']
HEATSET2  = PARAMS_CONTROL['HEATSET2']

CO2SET1   = PARAMS_CONTROL['CO2SET1']
CO2SET2   = PARAMS_CONTROL['CO2SET2']

V         = PARAMS_CONTROL['V']

def set_varset(par):
    vpd_set  = VPD_SET1 if par < NIGHT_PAR else VPD_SET2
    vent_set = VENTSET1 if par < NIGHT_PAR else VENTSET2
    heat_set = HEATSET1 if par < NIGHT_PAR else HEATSET2
    co2_set  = CO2SET1  if par < NIGHT_PAR else CO2SET2
    return vpd_set, vent_set, heat_set, co2_set

def set_pband(T_out, Vel):
    Pband = 0
    if T_out < T_OUT1:
        Pband = PBAND1
    elif T_out > T_OUT2:
        Pband =  PBAND2
    else:
        m = (PBAND2 - PBAND1) / (T_OUT2 - T_OUT1)
        Pband = PBAND2 - m * (T_out - T_OUT1)

    Pband_corr = 0.5 * (Vel - V) if Vel >= V else 0
    return Pband - Pband_corr

def pid(error, kp, ki, kd, I, d):
    return kp * error + ki * I + kd * d

def integral(X):
    return np.mean(X)

def control(error, kp, ki, kd, I, d):
    PID = pid(error, kp, ki, kd, I, d)
    return 0.01 * max(min(PID, 100), 0)

def control_u1(par, T_out):
    '''Control de la pantalla termica'''
    if NIGHT_PAR < par < OTHER_PAR and T_out < 10:
        return 0.5
    elif par < NIGHT_PAR:
        return 1
    else:
        return 0

def control_u4(x,set_):
    '''Control de la temperatura del aire (T2)'''
    I = integral(x-set_)
    try:
        error = x[-1] - set_
    except:
        breakpoint()
    d = 0
    return control(error, KP4, KI4, KD4, I, d)

def control_u10(x, set_):
    '''Control del CO2 (C1)'''
    I = integral(x - set_)
    error = x[-1] - set_
    if control_u10.old_error is None:
        control_u10.old_error = error
    d = error - control_u10.old_error
    control_u10.old_error = error
    breakpoint()
    return control(error, KP10, KI10, KD10, I, d)

def control_u9(T_out, vent_set, Pband ):
    '''Control del sistema de niebla'''
    if T_out < vent_set:
        return 0.0
    elif T_out > vent_set + Pband:
        return 1.0
    else:
        return (T_out - vent_set)/Pband

def control_u8(vpd, vpd_set):
    '''Control de los respiraderos en el techo'''
    I = integral(vpd - vpd_set)
    error = vpd[-1] - vpd_set
    if control_u8.old_error is None:
        control_u8.old_error = error
    d = error - control_u8.old_error
    control_u8.old_error = error
    control = pid(error, KP8, KI8, KD8, I, d)
    U_min = 0.02 if vpd[-1] < 0.5 else 0
    return min(max(0, control + U_min), 1)

class agent_baseline():
    def __init__(self,agent = None):
        self.i       = 0
        self.reset()
        self.agent = agent

    def get_action(self, i, env, update):
        '''
        i sirve para saber con que puntos crear una inteprolacion lineal
        update es una bandera que avisa que hay que actualizar los setpoints
        '''
        par   = self.get_straight_line('I2',i)
        T_out = self.get_straight_line('I5',i)
        Vel   = self.get_straight_line('I8',i)
        U     = np.zeros(11)
        T1_list = np.array([float(y) for y in env.daily_T1[-5:]])
        T2_list = np.array([float(y) for y in env.daily_T2[-5:]])
        C1_list = np.array([float(y) for y in env.daily_C1[-5:]])
        V1_list = np.array([float(y) for y in env.daily_V1[-5:]])

        if self.agent == None:
            if update:
                pband = set_pband(T_out[-1], Vel[-1])
                vpd_set, vent_set, heat_set, co2_set = set_varset(par[-1])
            breakpoint()
            VPD = np.array(list(map(lambda t1,v1:q2(t1) - v1, T1_list, V1_list)))
            U[0] = control_u1(par[-1], T_out[-1])         # U1(pantalla termica)
            U[7] = control_u8(VPD, vpd_set)               # U8(Respiraderos del techo)
            U[8] = control_u9(T_out[-1], vent_set, pband) # U9(Sistema de Niebla) PID
            U[10] = control_u4(T2_list, heat_set)          # U11(Calentador de Aire) Ultimos 5 min PID
            #Hace falta parar el CO2 mg*m^3 a ppm

            C1_list = np.array([(24.45* x)/44.01 for x in list(C1_list)])
            U[9] = control_u10(C1_list, co2_set)          # U10(Fuente de CO2)  Ultimos 5 min PID
            U[9] = 0
        else:
            pass
            ### Aqui van las acciones(set_points) que vienen de la red
        return U

    def get_straight_line(self, key, k):
        y1 = data[key][k]
        y2 = data[key][k + 1]
        y = interp1d([0, 1], [y1, y2], kind='linear')
        x = np.linspace(0, 1, 60)
        start = (self.i*minutos)%60
        end = -1 if ((self.i+ 1) * (minutos))%60 == 0 else ((self.i+ 1) * (minutos))%60
        x = x[start:end]
        if len(x)== 0:
            breakpoint()
        self.i += 1
        return np.array(list(map(lambda t: float(y(t)), x)))

    def set_point_co2(self,state):
        if state[3] > 5:
            self.co2 = 900
        else:
            self.co2 = 400

    def reset(self):
        control_u10.old_error = None
        control_u8.old_error = None


from time import sleep
from onvif import ONVIFCamera
import zeep

class MoverCamSimple:

    def __init__(self):
        self.XMAX = 1
        self.XMIN = -1
        self.YMAX = 1
        self.YMIN = -1
        self.IP = '192.168.78.90'
        self.USER = 'admin'
        self.PASS = 'Admin321'
        self.PORT_PROTOCOL = 554

    def zeep_pythonvalue(self, xmlvalue):
        return xmlvalue

    def moves(self):
        mycam = ONVIFCamera(self.IP, 80, self.USER, self.PASS)
        media = mycam.create_media_service()
        # Create ptz service object
        self.ptz = mycam.create_ptz_service()

        # Get target profile
        zeep.xsd.simple.AnySimpleType.pythonvalue = self.zeep_pythonvalue
        media_profile = media.GetProfiles()[0]

        # Get PTZ configuration options for getting continuous move range
        self.request = self.ptz.create_type('GetConfigurationOptions')
        self.request.ConfigurationToken = media_profile.PTZConfiguration.token
        ptz_configuration_options = self.ptz.GetConfigurationOptions(self.request)

        self.request = self.ptz.create_type('ContinuousMove')
        self.request.ProfileToken = media_profile.token
        self.ptz.Stop({'ProfileToken': media_profile.token})

        if self.request.Velocity is None:
            self.request.Velocity = self.ptz.GetStatus({'ProfileToken': media_profile.token}).Position
            self.request.Velocity = self.ptz.GetStatus({'ProfileToken': media_profile.token}).Position
            self.request.Velocity.PanTilt.space = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].URI
            self.request.Velocity.Zoom.space = ptz_configuration_options.Spaces.ContinuousZoomVelocitySpace[0].URI

        # Get range of pan and tilt
        # NOTE: X and Y are velocity vector
        #global XMAX, XMIN, YMAX, YMIN
        self.XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max
        self.XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min
        self.YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max
        self.YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min

    def perform_move(self,timeout):
        # Start continuous move
        self.ptz.ContinuousMove(self.request)
        # Wait a certain time
        sleep(timeout)
        # Stop continuous move
        self.ptz.Stop({'ProfileToken': self.request.ProfileToken})

    def move_up(self,timeout=1):
        print('move up...')
        self.request.Velocity.PanTilt.x = 0
        self.request.Velocity.PanTilt.y = self.YMAX
        self.perform_move(self.ptz, self.request,timeout)

    def move_down(self,timeout=1):
        print('move down...')
        self.request.Velocity.PanTilt.x = 0
        self.request.Velocity.PanTilt.y = self.YMIN
        self.perform_move(self.ptz, self.request,timeout)

    def move_right(self,timeout=1):
        print('move right...')
        self.request.Velocity.PanTilt.x = self.XMAX
        self.request.Velocity.PanTilt.y = 0
        self.perform_move(self.ptz, self.request,timeout)

    def move_left(self,timeout=1):
        print('move left...')
        self.request.Velocity.PanTilt.x = self.XMIN
        self.request.Velocity.PanTilt.y = 0
        self.perform_move(self.ptz, self.request,timeout)

    def zoom_up(self,timeout=1):
        print('zoom up')
        self.request.Velocity.Zoom.x = 1
        self.request.Velocity.PanTilt.x = 0
        self.request.Velocity.PanTilt.y = 0
        self.perform_move(self.ptz, self.request,timeout)

    def zoom_down(self,timeout=1):
        print('zoom down')
        self.request.Velocity.Zoom.x = -1
        self.request.Velocity.PanTilt.x = 0
        self.request.Velocity.PanTilt.y = 0
        self.perform_move(self.ptz, self.request,timeout)

    # VER MOVIMIENTO

    # Create media service object

if __name__=='__main__':
    ob = MoverCamSimple()
    ob.moves()
    ob.move_up(5)


import sys
import glob

import cv2
import numpy as np

from keras.applications.resnet50 import decode_predictions

import tensorflow as tf

# target model
output_model = "./models/output.tflite"
output_quant_model = "./models/output_quant.tflite"

# load validation set
image_folder = "../calibration"
img_path = glob.glob(image_folder+"/*")
if len(img_path)==0:
    print("image not found")
    sys.exit(1)

validation_data_set=[]
for file_name in img_path:
    img = cv2.imread(file_name) #BGR
    img = cv2.resize(img,(224, 224))
    ary = np.asarray(img, dtype=np.float32)
    ary = np.expand_dims(ary, axis=0)
    ary = ary/255.0
    validation_data_set.append(ary)

#verify Tensorflow lite
interpreter = tf.lite.Interpreter(model_path=output_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

print("tf lite float")

for i in range(len(validation_data_set)):
    interpreter.set_tensor(input_details[0]['index'], validation_data_set[i])
    interpreter.invoke()
    preds_tf_lite = interpreter.get_tensor(output_details[0]['index'])
    print('Predicted:', decode_predictions(preds_tf_lite, top=3)[0])

#verify quantized tensorflow lite
interpreter = tf.lite.Interpreter(model_path=output_quant_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

print("tf lite int8")

for i in range(len(validation_data_set)):
    data = validation_data_set[i]
    data = data*255
    data = data.astype(np.uint8)
    interpreter.set_tensor(input_details[0]['index'], data)
    interpreter.invoke()
    preds_tf_lite = interpreter.get_tensor(output_details[0]['index'])
    print('Predicted:', decode_predictions(preds_tf_lite, top=3)[0])


from fastapi import APIRouter, Depends, status

from src.core.cottages.exceptions import NoCottageWithThisId, NoPermission
from src.core.cottages.schemas import CottageCreate, CottageRead, CottageUpdate
from src.core.cottages.services import CottageService, get_cottage_service
from src.core.users.dependencies import current_user
from src.core.users.models import User
from src.core.utils.models import ErrorModel

def get_create_list_router() -> APIRouter:
    router = APIRouter(prefix="/cottages")

    @router.post(
        "",
        status_code=status.HTTP_201_CREATED,
        response_model=CottageCreate,
        responses={
            status.HTTP_401_UNAUTHORIZED: {
                "content": {
                    "application/json": {
                        "examples": {
                            "Unauthorized": {
                                "summary": "No token",
                                "value": {
                                    "detail": "Unauthorized"
                                }
                            }
                        }
                    }
                }
            },
        },
    )
    async def create_cottage(
            cottage_create: CottageCreate,
            cottage_service: CottageService = Depends(get_cottage_service),
            user: User = Depends(current_user)
    ):
        cottage = await cottage_service.create(user.id, cottage_create)
        return cottage

    @router.get(
        "",
        status_code=status.HTTP_200_OK,
        response_model=list[CottageRead]
    )
    async def get_cottages(
            cottage_service: CottageService = Depends(get_cottage_service),
    ):
        cottages = await cottage_service.get_all()
        return cottages

    @router.get(
        "/{cottage_id}",
        status_code=status.HTTP_200_OK,
        response_model=CottageRead,
        responses={
            status.HTTP_400_BAD_REQUEST: {
                "content": {
                    "application/json": {
                        "examples": {
                            NoCottageWithThisId.detail: {
                                "summary": "No cottage with this id",
                                "value": {
                                    "detail": NoCottageWithThisId.detail
                                }
                            }
                        }
                    }
                }
            },
        }
    )
    async def get_cottage_by_id(
            cottage_id: int,
            cottage_service: CottageService = Depends(get_cottage_service)
    ):
        cottage = await cottage_service.get_by_id(cottage_id)
        if cottage is None:
            raise NoCottageWithThisId
        return cottage

    @router.put(
        "/{cottage_id}",
        response_model=CottageCreate,
        responses={
            status.HTTP_400_BAD_REQUEST: {
                "content": {
                    "application/json": {
                        "examples": {
                            NoCottageWithThisId.detail: {
                                "summary": "No cottage with this id",
                                "value": {
                                    "detail": NoCottageWithThisId.detail
                                }
                            }
                        }
                    }
                }
            },
            status.HTTP_401_UNAUTHORIZED: {
                "content": {
                    "application/json": {
                        "examples": {
                            "Unauthorized": {
                                "summary": "No token",
                                "value": {
                                    "detail": "Unauthorized"
                                }
                            }
                        }
                    }
                }
            },
            status.HTTP_403_FORBIDDEN: {
                "content": {
                    "application/json": {
                        "examples": {
                            NoPermission.detail: {
                                "summary": "You don’t have permission to access.",
                                "value": {
                                    "detail": NoPermission.detail
                                }
                            }
                        }
                    }
                }
            }
        }
    )
    async def update_cottage(
            cottage_id: int,
            cottage_update: CottageUpdate,
            cottage_service: CottageService = Depends(get_cottage_service),
            user: User = Depends(current_user)
    ):
        current_cottage = await cottage_service.get_by_id(cottage_id)
        if current_cottage is None:
            raise NoCottageWithThisId
        elif current_cottage.owner != user.id:
            raise NoPermission
        cottage = await cottage_service.update(user.id, cottage_id, cottage_update)
        return cottage

    @router.delete(
        "/{cottage_id}",
        response_model=str,
        responses={
            status.HTTP_400_BAD_REQUEST: {
                "model": ErrorModel,
                "content": {
                    "application/json": {
                        "examples": {
                            NoCottageWithThisId.detail: {
                                "summary": "No cottage with this id",
                                "value": {
                                    "detail": NoCottageWithThisId.detail,
                                }
                            }
                        }
                    }
                }
            },
            status.HTTP_403_FORBIDDEN: {
                "model": ErrorModel,
                "content": {
                    "application/json": {
                        "examples": {
                            NoPermission.detail: {
                                "summary": "You don't have permission to access",
                                "value": {
                                    "detail": NoPermission.detail,
                                }
                            }
                        }
                    }
                }
            }
        }
    )
    async def delete_cottage(
            cottage_id: int,
            cottage_service: CottageService = Depends(get_cottage_service),
            user: User = Depends(current_user)
    ):
        current_cottage = await cottage_service.get_by_id(cottage_id)
        if current_cottage is None:
            raise NoCottageWithThisId
        elif current_cottage.owner != user.id:
            raise NoPermission
        result = await cottage_service.delete(user.id, cottage_id)
        return result

    return router


import re
import pymysql

f = open("member.html", "r", encoding='utf')
test = re.sub('\s+', " ", f.read())

pattern = re.compile('(.+?) 	 .+?(.+?).+?	 (.+?)	 (.+?)	')
data1 = re.findall(pattern, test)
# print(data1)

db = pymysql.connect("localhost", "root", "123456", "shenzy", charset='utf8mb4')
print("成功连接数据库")

cursor = db.cursor()
sql = "insert into qqgroup(nickname, name, qq, sex) values (%s, %s, %s, %s)"
try:
    cursor.executemany(sql, data1)
    db.commit()
    print("insert success")
except Exception as e:
    print("Error", str(e), sql)

db.close()


import torch.nn as nn

class DiceLoss(nn.Module):
    def __init__(self, eps=0.):
        super().__init__()
        self.eps = eps

    def forward(self, input, target):
        return dice_loss(input, target, self.eps)

def dice_loss(input, target, eps=0.):
    input = input.view(-1)
    target = target.view(-1)

    prod = (input * target).sum()
    in_sq = (input * input).sum()
    tgt_sq = (target * target).sum()

    dice = (2 * prod + eps) / (in_sq + tgt_sq + eps)

    return 1 - dice


#!/usr/bin/env python

import csv
import math
import re
import sys

import lxml.etree
import requests

HIERARCHY = '2'
LONDON = '276706'

def areas_csv(inpath=None, outpath=None):
    """
    Given a csv with station ids, names, postcodes, eastings and northings,
    write a csv of station ids, names, and ONS mid-layer super output area
    names.
    """
    get_name = get_msoa_name()

    if inpath:
        infile = open(inpath, 'rb')
    else:
        infile = sys.stdin
    if outpath:
        outfile = open(outpath, 'wb')
    else:
        outfile = sys.stdout

    try:
        reader = csv.reader(infile)
        writer = csv.writer(outfile)
        for id, name, postcode, x, y in reader:
            area = get_name(postcode, int(x), int(y))
            writer.writerow([id, name, area])
    finally:
        infile.close()
        outfile.close()

def get_msoa_name():
    """
    Return a function that takes a postcode, easting and northing and returns
    the matching MSOA name.
    """
    url = 'http://neighbourhood.statistics.gov.uk/NDE2/Disco/'

    def get_children(area_id):
        resp = requests.get(url + 'GetAreaChildren', params={
            'AreaId': area_id
        })
        resp.raise_for_status()
        tree = lxml.etree.fromstring(resp.content)
        for child in tree.iterfind('.//{*}Area'):
            child_id = child.find('{*}AreaId').text.strip()
            child_name = child.find('{*}Name').text.strip()
            sys.stderr.write('Fetching details for %s\n' % child_name)
            resp = requests.get(url + 'GetAreaDetail', params={
                'AreaId': child_id
            })
            resp.raise_for_status()
            tree = lxml.etree.fromstring(resp.content)
            envelope = tree.find('.//{*}AreaDetail/{*}Envelope').text.strip()
            x1, y1, x2, y2 = map(int, envelope.split(':'))
            midx = x1 + ((x2 - x1) / 2)
            midy = y1 + ((y2 - y1) / 2)
            yield (child_id, child_name, midx, midy)

    def get_closest(x, y, points):
        closest = None
        selected = None
        for idx, (xi, yi) in enumerate(points):
            distance = math.sqrt(((x - xi) ** 2) + ((y - yi) ** 2))
            if closest is None or distance < closest:
                closest = distance
                selected = idx
        return selected

    boroughs = tuple((borough, tuple(get_children(borough[0])))
                      for borough in get_children(LONDON))

    def inner(postcode, x, y):
        """
        Calls the neighbourhood statistics api to fetch the 2001 MSOA for the
        given postcode. If that fails (the api doesn't seem to have a full
        dataset for 2001 MSOAs), try to work out which MSOA we want based on
        the given easting and northing.
        """
        resp = requests.get(url + 'FindAreas', params={
            'HierarchyId': HIERARCHY,
            'Postcode': re.sub(r'\s+', '', postcode)
        })
        resp.raise_for_status()
        tree = lxml.etree.fromstring(resp.content)
        borough = None
        for area in tree.iterfind('.//{*}Area'):
            hierarchy = area.find('{*}HierarchyId').text.strip()
            level = area.find('{*}LevelTypeId').text.strip()
            if hierarchy == HIERARCHY and level == '140':
                return area.find('{*}Name').text.strip()

        sys.stderr.write('Could not find MSOA with postcode %s, trying all '
                         'areas in borough\n' % postcode)
        borough, areas = boroughs[get_closest(x, y, (b[0][2:4] for b in boroughs))]
        return areas[get_closest(x, y, (a[2:4] for a in areas))][1]

    return inner

if __name__ == '__main__':
    try:
        inpath = sys.argv[1]
    except IndexError:
        inpath = outpath = None
    else:
        try:
            outpath = sys.argv[2]
        except IndexError:
            outpath = None
    areas_csv(inpath, outpath)


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

""" Module summary description.

More detailed description.
"""
import pandas as pd
from matplotlib import pyplot as plt

from _scripts.plot_pandas_series import pandas_plot

__author__ = 'Benjamin Pillot'
__copyright__ = 'Copyright 2019, Benjamin Pillot'
__email__ = 'benjaminpillot@riseup.net'

ratio = 1/(1000 * 6.00372)
fontsize = 24
df = pd.read_csv("/home/benjamin/ownCloud/Post-doc Guyane/GREECE model/Results/Solar PV/"
                 "output_ac_power_permissive_contour.csv", index_col=0, sep=",")

# new_df = pd.DataFrame(index=df.index, columns=["C10", "C20", "C30", "C35", "C40", "C45", "C50", "C55", "C60", "C65"])
new_df = pd.DataFrame(index=df.index)
# new_df["P4, C65"] = df["polygon 6"] * 29.34 * ratio
new_df["P4, C65"] = df["polygon 6"] * ratio * 100
# new_df["P4, C45"] = df["polygon 6"] * 28.04 * ratio
# new_df["P3, C40"] = df["polygon 2"] * 24.78 * ratio
new_df["P3, C40"] = df["polygon 2"] * ratio * 100
# new_df["P2, C30"] = df["polygon 5"] * 17.29 * ratio
new_df["P2, C30"] = df["polygon 5"] * ratio * 100
# new_df["P1, C10"] = df["polygon 71"] * 4.66 * ratio
new_df["P1, C10"] = df["polygon 71"] * ratio * 100
# new_df["C20"] = df["polygon 5"] * 9.76 * ratio
# new_df["C30"] = df["polygon 5"] * 17.29 * ratio
# new_df["C35"] = df["polygon 2"] * 20.96 * ratio
# new_df["C40"] = df["polygon 2"] * 24.78 * ratio
# new_df["C45"] = df["polygon 6"] * 28.04 * ratio
# new_df["C50"] = df["polygon 6"] * 29.34 * ratio + df["polygon 71"] * 1.16 * ratio
# new_df["C55"] = df["polygon 6"] * 29.34 * ratio + df["polygon 71"] * 3.77 * ratio
# new_df["C60"] = df["polygon 5"] * 6.26 * ratio + df["polygon 6"] * 29.34 * ratio
# new_df["C65"] = df["polygon 5"] * 8.87 * ratio + df["polygon 6"] * 29.34 * ratio

new_df.index = pd.DatetimeIndex(new_df.index)
new_df = new_df[new_df.index.month == 2]

ax = pandas_plot(new_df, fontsize=fontsize, day_interval=2)
ax.set_ylabel("Power (% of $P_{max}$)", fontsize=fontsize)
ax.set_ylim(0, 100)
ax.legend(new_df.columns, fontsize=fontsize)
plt.show()


import requests
import base64
import wave
import json
import soundfile as sf
import json

# Chemin vers le fichier audio WAV
audio_file_path = 'test.wav'

file = wave.open(audio_file_path, 'rb')
params = base64.b64encode(json.dumps(file.getparams()).encode())
data = base64.b64encode(file.readframes(file.getnframes()))

# Encode the audio parameters as JSON and then in base64
#encoded_audio_params = base64.b64encode(json.dumps(audio_params_dict).encode())

# URL of your FastAPI endpoint
url = 'http://10.126.7.78:8000/speech_to_text/'

print(url)
# Create the request payload
data = {
    "data": data,
    "params": params
}

# Send the POST request

headers = {"Content-Type": "application/json"}
response = requests.post(url, data=data, headers=headers)
# Encode the data as J

# Display the response
if response.status_code == 200:
    print("Request was successful")
    print(response.json())
else:
    print(f"Request failed with status code: {response.status_code}")


import toml
from pathlib import Path
from typing import Optional, Union, Iterable
import blobconverter
import shutil

from tensorflow.keras.applications.resnet import (
    ResNet50,
    ResNet101,
    ResNet152,
)

from tensorflow.keras.applications.resnet_v2 import (
    ResNet50V2,
    ResNet101V2,
    ResNet152V2,
)

from tensorflow.keras.applications.mobilenet import MobileNet

from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.metrics import (
    BinaryAccuracy,
    CategoricalAccuracy,
    Precision,
    Recall,
)
from tensorflow.keras.backend import epsilon
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.callbacks import EarlyStopping

from tensorflow.keras.models import load_model
import typer

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import (
    convert_variables_to_constants_v2,
)

import tensorflow as tf

from tensorflow.python.keras import backend as K

config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.InteractiveSession(config=config)
K.set_session(tf.compat.v1.Session(config=config))

tf.config.run_functions_eagerly(True)

import typer

MODEL_TYPES = {
    "ResNet50": ResNet50,
    "ResNet101": ResNet101,
    "ResNet152": ResNet152,
    "ResNet50V2": ResNet50V2,
    "ResNet101V2": ResNet101V2,
    "ResNet152V2": ResNet152V2,
    "MobileNet": MobileNet,
}

def F1(y_true, y_pred):
    prec_fun = Precision()
    rec_fun = Recall()

    precision = prec_fun(y_true, y_pred)
    recall = rec_fun(y_true, y_pred)

    return 2 * ((precision * recall) / (precision + recall + epsilon()))

class CNN:
    def __init__(
        self,
        model_path: Optional[Union[str, Path]] = None,
        base_model: Optional[str] = None,
    ):
        """Initialize a new CNN model.

        Parameters
        ----------
        model_path : Optional[Union[str, Path]]
            Path to model, by default None
            If it is `None`, a `ResNet50v2` with weights of `imagenet` is loaded.
        base_model : Optional[str], optional
            Base model to load weights on it, by default None.
            If it is `None`, a local saved model will be loaded using `model_path`.
        """
        if model_path is not None:
            self.load(model_path, base_model)
        else:
            self.load()

    def train(
        self,
        train_images_folder: Union[Path, str],
        val_images_folder: Union[Path, str],
        labels_names: Union[str, Iterable[str]] = "inferred",
        n_labels: Optional[int] = None,
        color_mode: str = "rgb",
        image_size: Iterable[int] = (256, 256),
        shuffle: bool = True,
        batch_size: int = 32,
        optimizer: str = "adam",
        epochs: int = 10,
        metrics: Iterable[str] = ("F1", "binary_accuracy", "precision", "recall"),
        early_stopping_metric: str = "val_F1",
        early_stopping_patience: str = 5,
        early_stopping_mode: str = "max",
    ):
        if n_labels is None:
            assert (
                not labels_names == "inferred"
            ), f"If labels are inferred `n_labels` must be passed."
            n_labels = len(labels_names)

        if n_labels > 2:
            label_mode = "int"
            loss_function = "sparse_categorical_crossentropy"
            self.MODEL.add(Dense(n_labels, activation="softmax"))
        else:
            label_mode = "binary"
            loss_function = "binary_crossentropy"
            self.MODEL.add(Dense(1, activation="sigmoid"))

        train_dataset = image_dataset_from_directory(
            train_images_folder,
            labels=labels_names,
            batch_size=batch_size,
            color_mode=color_mode,
            image_size=image_size,
            shuffle=shuffle,
            label_mode=label_mode,
        )

        validation_dataset = image_dataset_from_directory(
            val_images_folder,
            labels=labels_names,
            batch_size=batch_size,
            color_mode=color_mode,
            image_size=image_size,
            shuffle=shuffle,
            label_mode=label_mode,
        )

        metrics_list = []

        for m in metrics:
            if m == "binary_accuracy":
                if label_mode == "binary":
                    metrics_list.append(BinaryAccuracy())
                else:
                    metrics_list.append(CategoricalAccuracy())
            elif m == "precision":
                metrics_list.append(Precision())
            elif m == "recall":
                metrics_list.append(Recall())
            elif m == "F1":
                metrics_list.append(F1)

        self.MODEL.compile(
            optimizer=optimizer, loss=loss_function, metrics=metrics_list
        )

        early_stopping_monitor = EarlyStopping(
            monitor=early_stopping_metric,
            patience=early_stopping_patience,
            mode=early_stopping_mode,
            restore_best_weights=True,
        )

        self.MODEL.fit(
            train_dataset,
            validation_data=validation_dataset,
            epochs=epochs,
            callbacks=[early_stopping_monitor],
        )

    def save(self, model_path: Union[str, Path]):
        """Save the architecture and the weights of the model.

        Parameters
        ----------
        model_path : Union[str, Path]
            Path to the folder where the model will be saved.
        """

        self.MODEL.save(model_path)

    def load(
        self,
        model_path: Union[str, Path] = "imagenet",
        base_model: Optional[str] = "ResNet50v2",
    ):
        """Load the architecture and the weights of a model.

        Parameters
        ----------
        model_path : Union[str, Path]
            Path to the model saved.
        base_model : Optional[str], optional
            Base model to load weights on it, by default None.
            If it is `None`, a local saved model will be loaded using `model_path`.
        """

        if base_model is None:
            self.MODEL = load_model(model_path, custom_objects={"F1": F1})

        else:
            self.MODEL = Sequential()
            model = MODEL_TYPES[base_model](weights=model_path)
            self.MODEL.add(model)

    def to_pb(self, path_out_folder):
        # path of the directory where you want to save your model

        frozen_graph_filename = "frozen_graph"
        model = self.MODEL
        full_model = tf.function(lambda x: model(x))
        full_model = full_model.get_concrete_function(
            tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype)
        )  # Get frozen ConcreteFunction
        frozen_func = convert_variables_to_constants_v2(full_model)
        frozen_func.graph.as_graph_def()
        layers = [op.name for op in frozen_func.graph.get_operations()]
        print("-" * 60)
        print("Frozen model layers: ")

        for layer in layers:
            print(layer)

        print("-" * 60)
        print("Frozen model inputs: ")
        print(frozen_func.inputs)
        print("Frozen model outputs: ")
        print(frozen_func.outputs)  # Save frozen graph to disk
        tf.io.write_graph(
            graph_or_graph_def=frozen_func.graph,
            logdir=path_out_folder,
            name=f"{frozen_graph_filename}.pb",
            as_text=False,
        )

    def to_blob(self, path_out_folder):
        self.to_pb(path_out_folder)

        blob_path = blobconverter.from_tf(
            frozen_pb=f"{path_out_folder}/frozen_graph.pb",
            data_type="FP16",
            shaves=5,
            version = blobconverter.Versions.v2020_1,
            optimizer_params=[
                "--reverse_input_channels",
                "--input_shape=[1,224,224,3]",
            ],
        )

        shutil.move(blob_path, f"{path_out_folder}/depthai_model.blob")

def main(config_path: Optional[str] = "./CNN_stress/config.toml"):

    config_dict = toml.load(config_path)

    model = CNN(**config_dict["load"])
    model.train(**config_dict["training"])
    model.save(**config_dict["save"])
    model.load(config_dict["save"]["model_path"], base_model=None)

if __name__ == "__main__":
    typer.run(main)


import glob
import json
import requests

def parse_taxonomy_files(source_dir):
    """

    :param source_dir: Directory where the JSON files are stored
    :return:
    """
    r = requests.get("https://alpha.nationalarchives.gov.uk/staticdata/taxonomy_keys.json")
    if r.status_code == requests.codes.ok:
        taxonomy_lookup = r.json()
    else:
        return
    master = {}
    files = glob.glob(f"{source_dir}/*")
    for i, file in enumerate(files):
        print(f"Working on {i} of {len(files)}")
        with open(file, "r") as f:
            j = json.load(f)
            try:
                for x in j["hits"]["hits"]:
                    cat_ref = x["_source"].get("CATALOGUE_REFERENCE")
                    if cat_ref:
                        shard = "".join([x for x in cat_ref[0:2] if x.isalpha()]).lower()
                        d = {
                            "iaid": x["_id"],
                            "taxonomy_ids": [
                                taxonomy_lookup.get(y) for y in x["_source"]["TAXONOMY_ID"]
                            ],
                        }
                        if not master.get(shard):
                            master[shard] = {}
                        master[shard][cat_ref] = d
            except KeyError:
                print(f"File with error: {file}")
    for k, v in master.items():
        with open(f"taxonomy_{k}.json".lower(), "w") as wf:
            json.dump(v, wf, indent=2)

def get_taxonomy(catalogue_reference):
    """
    return the taxonomy data and the information asset ID for a given catalogue reference
    :param catalogue_reference:
    :return:
    """
    if catalogue_reference:
        pass
    return None, None

def make_taxonomy_lookup():
    taxo = {}
    r = requests.get("https://alpha.nationalarchives.gov.uk/staticdata/taxonomy.json")
    if r.status_code == requests.codes.ok:
        j = r.json()
    for list_item in j:
        taxo[list_item["code"]] = list_item
    with open("taxonomy_keys.json", "w") as wf:
        json.dump(taxo, wf, indent=2)

if __name__ == "__main__":
    parse_taxonomy_files(
        source_dir="/home/ec2-user/Python-3.8.1/ds-alpha-code/ildbweb/Graph/tmp/TAXONOMY/"
    )
    # make_taxonomy_lookup()


import matplotlib.pyplot as plt
import numpy as np

def fungsi(x):
    return 4*(x**2) + x + 6

def kartesian():
    x = np.linspace(-3,3,100)
    y = 4*(x**2) + x + 6

    plt.plot(x,y)
    plt.show()

def trapezcomp(fx, bwh, ats, klm):
    lbr_klm = (ats - bwh) / klm
    x = bwh

    integrasi = fx(bwh)
    for k in range(1, klm):
        x = x + lbr_klm
        integrasi += 2 * fx(x)

    return (integrasi + fx(ats)) * lbr_klm * 0.5

def romberg(fx, bwh, ats, klm):
    kartesian()
    iterasi = np.zeros((klm, klm))

    for k in range(0, klm):
        iterasi[k, 0] = trapezcomp(fx, bwh, ats, 2**k)

        for j in range(0, k):
            iterasi[k, j+1] = (4**(j+1) * iterasi[k, j] - iterasi[k-1, j]) / (4**(j+1) - 1)

        print(iterasi[k, 0:k+1])

    return iterasi

bawah = int(input("Batas bawah: "))
atas = int(input("Batas atas: "))
kolom = int(input("Jumlah kolom: "))

iterasi = romberg(fungsi, bawah, atas, kolom)
hasil = iterasi[kolom-1, kolom-1]
print(hasil)


from replit import clear
#HINT: You can call clear() to clear the output in the console.

from art import logo

print(logo)
print("Welcome to the silent auction.")
input("The bid is about to start enter any key to continue: ")

continue_bid = True
bid_dict = {}

while continue_bid:

    clear()

    name = input("What is your name?: ")
    bid = input("What is your bid? £ : ")
    bid_dict[name] = float(bid)
    next_bidder = input("Are there any other bidders? Type 'yes' or 'no': ")

    if next_bidder == "yes":
        continue_bid = True
    else:
        continue_bid = False

max_bid = max(bid_dict, key=bid_dict.get)

print(f"{max_bid} won with a bid of £{bid_dict[max_bid]}!")

#Challenge: is to create a dictionary, where each person's name is the key and their bid will be the value

#after each bid is inputted, the user should be able to type 'yes' if there is someone else yet to bid or 'no' if they are the last bidder.

#if the user clicks 'yes' the console should clear() so their bid remains a secret and if they answer 'no' the highest bidder and their bid can be printted.

# the last step will require the program to loop through the dictionary to find the highest value/bid


from collections import Counter

def solution(want, number, discount):
    counter = Counter()
    for i in range(len(want)):
        counter[want[i]] = number[i]

    cnt = 0
    for i in range(len(discount) - 10 + 1):
        if counter == Counter(discount[i: 10 + i]):
            cnt += 1
    return cnt


#!/usr/bin/python3.4
# coding: utf8

from PIL import Image
import os,sys

inputPath=sys.argv[1]
outputPath=sys.argv[2]
img = Image.open(inputPath)
print("actual size : ",img.size[0],"x",img.size[1]," pixels.")
newSizeX=int(input("enter new size x : "))
newSizeY=int(input("enter new size y : "))
img = img.resize((newSizeX,newSizeY))
img.save(outputPath)


import requests
from bs4 import BeautifulSoup

def searchWeather(city):
    try:
        # creating url and requests instance
        url = "https://www.google.com/search?q="+"weather"+city
        html = requests.get(url).content

        # getting raw data
        soup = BeautifulSoup(html, 'html.parser')
        temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
        str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text

        # formatting data
        data = str.split('\n')
        time = data[0]
        sky_desc = data[1]

        # printing all data
        city = city
        temp = temp
        time = time
        desc = sky_desc

        #print(other_data)
        weather_info = f'\nToday is: {time}\n\nCity: {city.title()}\nTemperature: {temp}\nWeather Condition: {desc}'

    except Exception:
        weather_info = f'\nThere was problem retrieving that value!'

    return weather_info


# myproject/ex_urls/views.py
# -*- coding: UTF-8 -*-

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def articles(request):
    return render(request,
                  'ex_urls/index.html',
                  {'year_list': (2003, 2004, 2005,),}
                  )

def redirect_to_year(request):
    year = 2006
    return HttpResponseRedirect(reverse('urls:news-year-archive', args=(year,)))

def special_case_2003(request):
    html = "It is now only 2003"
    return HttpResponse(html)

def year_archive(request, *args, **kwargs):
    try:
        html = "It is key Year: %s" %\
                (kwargs['year'],)
    except KeyError:
        html = "It is Year: %s" %\
                (args[0],)
    return HttpResponse(html)

def month_archive(request, *args, **kwargs):

    try:
        html = "It is now key Year: %s Month: %s" %\
         (kwargs['year'], kwargs['month'])
    except KeyError:
        html = "It is now Year: %s Month: %s" %\
         (args[0], args[1])
    return HttpResponse(html)

def day_archive(request, *args, **kwargs):
    try:
        html = "It is now key"+\
        " Year: %s Month: %s Day: %s and arg3: %s" %\
        (kwargs['year'], kwargs['month'], kwargs['day'], kwargs['extra'])
    except KeyError:
        html = "It is now Year: %s Month: %s Day: %s" %\
        (args[0], args[1], args[2])
    return HttpResponse(html)


import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import preProcessData

#Read the csv file
dfRaw = preProcessData.dfDaily #pd.read_csv("Gemini_BTCUSD_1h.csv", header=1)

#to view all columns
pd.set_option("display.max.columns", None)

print(dfRaw.head())
print(dfRaw.tail())
print(dfRaw.describe())
print(dfRaw.info())
print(dfRaw.std(numeric_only=None))

#plots
#sns.boxplot(x='BullishBearish', y='rsi', data=dfRaw)
#sns.boxplot(dfRaw['close'])
#sns.lineplot(dfRaw[['open', 'close']])
#plt.show()

#heatmap correlation
corr_matrix = dfRaw.corr(method='spearman')
f, ax = plt.subplots(figsize=(16,8))
sns.heatmap(corr_matrix, annot=True, fmt='.2f', linewidth=0.4,
            annot_kws={"size": 10}, cmap='coolwarm', ax=ax)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.show()


import cv2
# load the image and show it
image = cv2.imread("../../images/jurassic-world.jpg")
# cv2.imshow("original", image)
# cv2.waitKey(0)

cropped = image[70:170, 440:540]
# cv2.imshow("cropped", cropped)
# cv2.waitKey(0)

cv2.imwrite("../../images/output/crop.jpg", cropped)