import re
from datetime import datetime
from .driver import Driver
from .settings import Settings
from .user import User
from PyInquirer import prompt
from PyInquirer import Validator, ValidationError
##
from .validators import AmountValidator, MonthValidator, LimitValidator, PriceValidator, NumberValidator, TimeValidator, DateValidator, DurationValidator, PromoDurationValidator, ExpirationValidator, ListValidator
from . import remote as Remote
from .file import File, Folder, Google_File, Google_Folder

class Discount:
    """OnlyFans discount class"""

    def __init__(self):
        """OnlyFans discount object"""

        # amount in percent
        self.amount = None
        # number of months (1-12)
        self.months = None
        # the recipient username
        self.username = None
        # prevents double prompts
        self.gotten = False

    def apply(self):
        """
        Applies the discounted amount to the recipient username via Driver.discount_user

        If the targeted username is one of the matching keywords then all of the
        matching recipients will be discounted. Values are determined by runtime args or prompted
        for.

        """

        # ensure the discount has non default values
        self.get()
        if not self.gotten:
            Settings.err_print("Unable to apply discount")
            return
        # skip prompt if disabled
        if Settings.is_prompt():
            if not Settings.prompt("Discount"): return
        Settings.maybe_print("discounting: {}".format(self.username))
        # create new or find default browser
        driver = Driver.get_driver()
        if self.username.lower() == "all":
            users = User.get_all_users(driver=driver)
        elif self.username.lower() == "recent":
            users = User.get_recent_users(driver=driver)
        elif self.username.lower() == "favorite":
            users = User.get_favorite_users(driver=driver)
        elif self.username.lower() == "new":
            users = User.get_new_users(driver=driver)
        else: users = [self]
        for user in users:
            self.username = user.username
            driver.discount_user(discount=self)

    @staticmethod
    def create():
        """Create and apply a discount from args or prompts"""

        discount = Discount()
        discount.apply()

    def get(self):
        """Update the discount object's default values"""

        if self.gotten: return
        gotten = self.get_username()
        gotten = self.get_amount()
        gotten = self.get_months()
        self.gotten = True

    def get_amount(self):
        """
        Populate and get the amount value

        If not found in args and prompt is enabled, ask for value.

        Returns
        -------
        int
            the discounted amount to apply

        """

        if self.amount: return self.amount
        # retrieve from args and return if exists
        amount = Settings.get_amount() or None
        if amount:
            self.amount = amount
            return amount
        # prompt skip
        if not Settings.prompt("amount"): return None
        question = {
            'type': 'input',
            'name': 'amount',
            'message': 'Amount:',
            'validate': AmountValidator,
            'filter': lambda val: int(myround(int(val)))
        }
        amount = prompt(question)["amount"]
        if not Settings.confirm(amount): return self.get_amount()
        self.amount = amount
        return self.amount

    def get_months(self):
        """
        Populate and get the months value

        If not found in args and prompt is enabled, ask for value.

        Returns
        -------
        int
            the number of months to discount for

        """

        if self.months: return self.months
        # retrieve from args and return if exists
        months = Settings.get_months() or None
        if months:
            self.months = months
            return months
        # prompt skip
        if not Settings.prompt("months"): return None
        question = {
            'type': 'input',
            'name': 'months',
            'message': 'Months:',
            'validate': MonthValidator,
            'filter': lambda val: int(val)
        }
        months = prompt(question)["months"]
        if not Settings.confirm(months): return self.get_months()
        self.months = months
        return self.months

    def get_username(self):
        """
        Populate and get the username value

        If not found in args and prompt is enabled, ask for value.

        Returns
        -------
        str
            the username to discount

        """

        if self.username: return self.username
        self.username = User.select_user()
        return self.username

    def grandfatherer(self, users=[]):
        """
        Executes the 'Grandfather' discount model

        If users is empty it is populated with users from the 'Grandfather' OnlyFans list in
        the account. All 'Grandfather'ed users are provided with the max discount for the max months.

        Parameters
        ----------
        users : list
            list of users to 'Grandfather'

        """

        from .driver import Driver
        if len(users) == 0:
            users = User.get_users_by_list(name="grandfathered", driver=Driver.get_driver())
        print("Discount - Grandfathering: {} users".format(len(users)))
        from .validators import DISCOUNT_MAX_MONTHS, DISCOUNT_MAX_AMOUNT
        self.months = DISCOUNT_MAX_MONTHS
        self.amount = DISCOUNT_MAX_AMOUNT
        # apply discount to all users
        for user in users:
            self.username = user.username
            print("Grandfathering: {}".format(self.username))
            try:
                Driver.get_driver().discount_user(discount=self)
            except Exception as e:
                print(e)


from flask import Flask, render_template, request
import sklearn
import pandas as pd
from datetime import datetime
import pickle
import numpy as np

filename = 'fare_price_model.pkl'
regressor = pickle.load(open(filename, 'rb'))

app = Flask(__name__)

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

@app.route('/predict',methods=["GET", "POST"])
def predict():
    temp=list()
    if request.method == 'POST':

        #Total Stop
        Stopage=int(request.form["Stopage"])
        temp=temp+[Stopage]

        #Airline
        Airline=request.form["Airline"]
        if Airline == 'Air India':
            temp = temp + [1,0,0,0,0,0,0,0,0,0,0]
        elif Airline == 'Vistara Premium economy':
            temp = temp + [0,1,0,0,0,0,0,0,0,0,0]
        elif Airline == 'Trujet':
            temp = temp + [0,0,1,0,0,0,0,0,0,0,0]
        elif Airline == 'GoAir':
            temp = temp + [0,0,0,1,0,0,0,0,0,0,0]
        elif Airline == 'IndiGo':
            temp = temp + [0,0,0,0,1,0,0,0,0,0,0]
        elif Airline == 'Jet Airways':
            temp = temp + [0,0,0,0,0,1,0,0,0,0,0]
        elif Airline == 'Jet Airways Business':
            temp = temp + [0,0,0,0,0,0,1,0,0,0,0]
        elif Airline == 'Multiple carriers':
            temp = temp + [0,0,0,0,0,0,0,1,0,0,0]
        elif Airline == 'Multiple carriers Premium economy':
            temp = temp + [0,0,0,0,0,0,0,0,1,0,0]
        elif Airline == 'SpiceJet':
            temp = temp + [0,0,0,0,0,0,0,0,0,1,0]
        elif Airline == 'Vistara':
            temp = temp + [0,0,0,0,0,0,0,0,0,0,1]
        else:
            temp = temp + [0,0,0,0,0,0,0,0,0,0,0]

            #Source
        Source=request.form["Source"]
        if Source=="Chennai":
            temp=temp+[1,0,0,0]
        elif Source=="Delhi":
            temp=temp+[0,1,0,0]
        elif Source=="Kolkata":
            temp=temp+[0,0,1,0]
        elif Source=="Mumbai":
            temp=temp+[0,0,0,1]
        else:
            temp=temp+[0,0,0,0]

            #Destination
        Destination=request.form["Destination"]
        if Destination=="Cochin":
            temp=temp+[1,0,0,0,0]
        elif Destination=="Delhi":
            temp=temp+[0,1,0,0,0]
        elif Destination=="Hyderabad":
            temp=temp+[0,0,1,0,0]
        elif Destination=="Kolkata":
            temp=temp+[0,0,0,1,0]
        elif Destination=="New Delhi":
            temp=temp+[0,0,0,0,1]
        else:
            temp=temp+[0,0,0,0,0]

        #departure
        checkin=request.form['check-in']
        Journey_day = int(pd.to_datetime(checkin, format="%Y-%m-%dT%H:%M").day)
        Journey_month = int(pd.to_datetime(checkin, format="%Y-%m-%dT%H:%M").month)
        Journey_year = int(pd.to_datetime(checkin, format="%Y-%m-%dT%H:%M").year)
        Dep_hour = int(pd.to_datetime(checkin, format="%Y-%m-%dT%H:%M").hour)
        Dep_min = int(pd.to_datetime(checkin, format="%Y-%m-%dT%H:%M").minute)

        temp=temp+[Journey_day,Journey_month,Dep_hour,Dep_min]

        #Arrival
        checkout=request.form['check-out']
        Arrival_day = int(pd.to_datetime(checkout, format="%Y-%m-%dT%H:%M").day)
        Arrival_month = int(pd.to_datetime(checkout, format="%Y-%m-%dT%H:%M").month)
        Arrival_year = int(pd.to_datetime(checkout, format="%Y-%m-%dT%H:%M").year)
        Arrival_hour = int(pd.to_datetime(checkout, format="%Y-%m-%dT%H:%M").hour)
        Arrival_min = int(pd.to_datetime(checkout, format="%Y-%m-%dT%H:%M").minute)

        #Duration
        val1=datetime(Journey_year,Journey_month,Journey_day,Dep_hour,Dep_min,0)
        val2=datetime(Arrival_year,Arrival_month,Arrival_day,Arrival_hour,Arrival_min,0)

        Duration_hours=int(((val2-val1).total_seconds())/60**2)
        Duration_mins=int((((val2-val1).total_seconds())/60)%60)

        temp=temp+[Arrival_hour,Arrival_min,Duration_hours,Duration_mins]

        data = np.array([temp])
        my_prediction = int(regressor.predict(data)[0])
    return render_template("index.html",output=f'Predicted price is Rs. {my_prediction} ')

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


import base64
from datetime import datetime
import time
import json
import hashlib
import winreg
import codecs
from urllib.request import urlopen
import sqlite3
import os
import sys
hash_length = 32

if 'back_end' not in sys.modules:
    print('Not running in client')
    with open('server_cfg.json',"r+") as conf_file:
        config = json.loads(conf_file.read())
        database = config['database']
        if os.path.isfile(database):
            conn = sqlite3.connect(database, check_same_thread=False)
            db = conn.cursor()
        else:
            conn = sqlite3.connect(database)
            db = conn.cursor()
            db.execute("CREATE TABLE posts (thread,author_name,id,timestamp,subject,comment,image)")
            db.execute("CREATE TABLE users (username,id,ip_addr,reg_date)")
            db.execute("CREATE TABLE sessions (bid,sid,time,invalid)")
else:
    config = open("config.json","r")
    config = json.load(config)
    database = config['database']
    hash_length = int(config['hash_len'])
    if os.path.isfile(database):
        conn = sqlite3.connect(database)
        db = conn.cursor()
    else:
        conn = sqlite3.connect(database)
        db = conn.cursor()
        db.execute("CREATE TABLE posts (thread,author_name,id,timestamp,subject,comment,image)")

def regkey_value(path, name="", start_key = None):
    if isinstance(path, str):
        path = path.split("\\")
    if start_key is None:
        start_key = getattr(winreg, path[0])
        return regkey_value(path[1:], name, start_key)
    else:
        subkey = path.pop(0)
    with winreg.OpenKey(start_key, subkey) as handle:
        assert handle
        if path:
            return regkey_value(path, name, handle)
        else:
            desc, i = None, 0
            while not desc or desc[0] != name:
                desc = winreg.EnumValue(handle, i)
                i += 1
            return desc[1]

def make_hash(*args,length=hash_length):
    tobehashed = ''
    for arg in args:
        tobehashed = tobehashed+str(arg)
    tobehashed = tobehashed.encode("utf-8")
    hash_object = hashlib.sha256(tobehashed)
    return hash_object.hexdigest()[:length]

def get_timestamp():
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

def get_epoch():
    return int(time.time())

def get_user_id():
    regkey = regkey_value("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion","ProductId")
    ip = json.loads(urlopen('https://api.ipify.org?format=json').read().decode("utf-8"))['ip']
    return make_hash(regkey_value,ip)

class Post():
    id = ""
    author_name = ""
    timestamp = ""
    subject = ""
    comment = ""
    image = ""
    thread = ""

    def save_post(self):
        id = self.id
        author_name = self.author_name
        subject = self.subject
        comment = self.comment
        image = self.image
        timestamp = self.timestamp
        if db.execute("INSERT INTO posts (thread,id,author_name,timestamp,subject,comment,image) VALUES(0,?,?,?,?,?,?)",(id,author_name,timestamp,subject,comment,image)):
            conn.commit()
            return 1
        else:
            return 0

    def add_comment(self,comm):
        author_name = comm.author_name
        timestamp = comm.timestamp
        subject = comm.subject
        image = comm.image
        comment = comm.comment
        post_id = self.id
        id = make_hash(timestamp,post_id)
        if db.execute("INSERT INTO posts(thread,author_name,id,timestamp,subject,comment,image) values(?,?,?,?,?,?,?)",(post_id,author_name,id,timestamp,subject,comment,image)):
            conn.commit()
            return 1
        else:
            return 0

    def get_comment_count(self):
        db.execute("SELECT count(*) FROM posts WHERE thread=?",(self.id,))
        return db.fetchone()[0]

    def get_comments(self):
        comments = []
        db.execute("SELECT id,timestamp,subject,comment,image,author_name FROM posts WHERE thread=?",(self.id,))
        for res in db.fetchall():
            comm = Post()
            comm.thread = self.id
            comm.id = res[0]
            comm.timestamp = res[1]
            comm.subject = res[2]
            comm.comment = res[3]
            comm.image = res[4]
            comm.author_name = res[5]
            comm.replies = []
            db.execute("SELECT id,timestamp,subject,comment,image FROM posts WHERE thread=?",(comm.id,))
            for res2 in db.fetchall():
                reply = Post()
                reply.thread = self.id
                reply.id = res[0]
                reply.timestamp = res[1]
                reply.subject = res[2]
                reply.comment = res[3]
                reply.image = res[4]
                reply.author_name = res[5]
                comm.replies.append(reply)
            comments.append(comm)
        return comments

    def pack_json(self):
        post = {
        "post_id" : self.id,
        "timestamp" : self.timestamp,
        "comment" : self.comment,
        "subject" : self.subject,
        "image" : self.image
        }
        return json.dumps(post)

class blockchan():

    def encode_image(path):
        with open(path,"rb") as image:
            image = base64.b64encode(image.read())
        image = image.decode("utf-8")
        return image

    def make_post(name,subject,comment,file,user_id,post_id=0,thread=0):
        if file != '':
            image = blockchan.encode_image(file)
        else:
            image = 0
        timestamp = str(get_timestamp())
        author_id = user_id
        if post_id == 0:
            post_id = make_hash(timestamp,author_id)
        post = Post()
        post.id = post_id
        post.author_name = name
        post.timestamp = timestamp
        post.subject = subject
        post.comment = comment
        post.image = image
        post.thread = thread
        return post

    def get_all_posts():
        db.execute("SELECT author_name,timestamp,subject,comment,image,id FROM posts WHERE thread=0 ORDER BY timestamp DESC")
        posts = []
        for res in db.fetchall():
            post = Post()
            post.author_name = res[0]
            post.timestamp = res[1]
            post.subject = res[2]
            post.comment = res[3]
            post.image = res[4]
            post.id = res[5]
            posts.append(post)
        return posts

    def post_exists(id):
        db.execute("SELECT id FROM posts WHERE id=?",(id,))
        if db.fetchone():
            return 1

    def get_post(id):
        db.execute("SELECT id,author_name,timestamp,subject,comment,image,thread FROM posts WHERE id=?",(id,))
        res = db.fetchone()
        post = Post()
        post.id = res[0]
        post.author_name = res[1]
        post.timestamp = res[2]
        post.subject = res[3]
        post.comment = res[4]
        post.image = res[5]
        post.thread = res[6]
        return post

    def get_post_ids(sort='desc'):
        if sort == 'desc':
            db.execute("SELECT id FROM posts WHERE thread=0 ORDER BY timestamp DESC")
        elif sort == 'asc':
            db.execute("SELECT id FROM posts WHERE thread=0 ORDER BY timestamp ASC")
        else:
            return 0
        result = []
        for res in db.fetchall():
            result.append(res[0])
        return result


import random
import timeit
from copy import deepcopy
from math import sqrt, log
import numpy as np
from envs.env import Env
from agents.non_rl.rule_based_agent import DouDizhuRuleAgentV1
from envs.mydoudizhu import DoudizhuEnv
from utils_global import eval_tournament

class MCTSNode(object):

    def __init__(self, player_num, action, legal_actions, parent=None):
        #self.ucb_const = ucb_const
        self.action = action
        # list
        self.legal_actions = legal_actions
        self.parent = parent
        self.player_num = player_num
        # total rewards
        self.Q = [0.0 for i in range(player_num)]
        # visit sum
        self.N = [0.0 for i in range(player_num)]
        # a map from action to Node {key: action_id, value: MCTSTreeNode - child_node}
        self.children = {}

    def is_leaf(self):
        # check if this node is leaf(i.e. no nodes below this have been expanded).
        return self.children == {}

    def is_root(self):
        # check is this node is root(i.e. node has no parent).
        return self.parent is None

    def fully_expanded(self, player_id):
        # check whether all child nodes have been expanded.

        # for legal_action in self.legal_actions[player_id]:
        # if self.children.__contains__(legal_action):
        # return True
        if len(self.children) == len(self.legal_actions[player_id]):
            return True
        else:
            return False

    def add_child(self, child):
        self.children[child.action] = child

    def get_ucb_value(self, player_id, ucb_const):
        # Calculate and return the ucb1 value for this node.
        if self.N[player_id] == 0:
            return float('inf')
        else:
            # ucb1 formula = Q(v')/N(v') + C*(sqrt(2*ln(N(v)/N(v'))))
            # Q(v')/N(v') - value estimation, exploitation
            # C*(sqrt(2*ln(N(v)/N(v')))) - exploration
            ucb_value = self.Q[player_id] / self.N[player_id] + \
                        ucb_const * sqrt(2.0 * log(self.parent.N[player_id]) / self.N[player_id])
            return ucb_value

    def get_best_child(self, player_id, ucb_const):
        # best child = argmax(v'ϵ chilidren of v) (ucb_value)
        value_list = {action: child.get_ucb_value(player_id, ucb_const) for action, child in self.children.items()}
        action = max(value_list, key=value_list.get)
        child = self.children[action]
        return value_list, child

    def update(self, reward, player_id):
        # Update node values and visits.
        # reward: the payoff of subtree evaluation from the current player's perspective.
        self.Q[player_id] += reward
        self.N[player_id] += 1

    def __repr__(self):
        str = f'Node: {self.action}; children: {self.children}; visits: {self.N}; reward: {self.Q}'
        return str

class MCTSAgent:
    def __init__(self, env: Env, num_simulation):
        self.env = env
        self.use_raw = False
        self.num_simulations = num_simulation

    def step(self, state):

        legal_actions = state['legal_actions']
        current_player = self.env.get_player_id()
        all_legal_actions = [[] for i in range(self.env.player_num)]
        all_legal_actions[current_player] = legal_actions

        # initialize a root node from current state
        self.root = MCTSNode(player_num=self.env.player_num,
                             legal_actions=all_legal_actions, parent=None, action=0)

        temp_timestep = self.env.timestep
        for i in range(self.num_simulations):
            # TODO: reshuffle other players' hand before every simulation, so that the agent will not remember the cards
            # Run a single random rollout from the root to the leaf,
            # get payoff at the leaf and propagate it back through its parents.
            # State is modified in-place, so a copy of environment must be provided.

            env_cp = deepcopy(self.env)
            selected_node = self.select(self.root, self.env)
            rewards = self.random_rollout(selected_node, self.env)
            self.back_prop(selected_node, rewards)

            for j in range(self.env.timestep - temp_timestep):
                self.env.step_back()
            self.env.timestep = temp_timestep
        # print(self.root)

        # get the value list and best child for the current root node(ucg_const=0), the child with best average score
        list, best_child = self.root.get_best_child(self.env.get_player_id(), 0)

        # return the action needed to reach the best child from the current root as action to take for the agent.
        action = best_child.action

        current_hand = state['raw_obs']['current_hand']
        print(f'current player: {current_player}, '
              f'current hand: {current_hand}, '
              f'action: {self.env._ACTION_LIST[action]}')

        return action

    def eval_step(self, state):
        probs = [0 for _ in range(self.env.action_num)]
        for i in state['legal_actions']:
            probs[i] = 1 / len(state['legal_actions'])
        return self.step(state), probs

    def select(self, root, env):
        player_id = env.get_player_id()
        # initialize a list of  for all the players
        unvisited_actions = [[] for i in range(env.player_num)]
        if not root.fully_expanded(player_id):
            unvisited_actions[player_id] = root.legal_actions[player_id]

        if len(unvisited_actions[player_id]) != 0:
            random.shuffle(unvisited_actions[player_id])
            untried_action = unvisited_actions[player_id].pop()
            node = self.expand(env, root, untried_action)

        else:
            _, node = root.get_best_child(player_id, 0.7)
            next_state, next_player_id = env.step(node.action)
            node.legal_actions[next_player_id] = next_state['legal_actions']

        return node

    def expand(self, env, node, action):
        # Instantiate one of the unexpanded children and return it.
        next_state, next_player_id = env.step(action)

        if node.children.__contains__(action):
            new_node = node.children[action]
            new_node.legal_actions[next_player_id] = next_state['legal_actions']
        else:
            legal_actions = [[] for i in range(env.player_num)]
            legal_actions[next_player_id] = next_state['legal_actions']
            new_node = MCTSNode(player_num=env.player_num, action=action, legal_actions=legal_actions, parent=node)

            node.add_child(new_node)

        return new_node

    def random_rollout(self, node, env):
        """
        random rollout policy: randomly selects action from legal_actions until game ends
        return:(list)  final reward
        """

        if env.is_over():
            # if game is over, return the playoff
            return env.get_payoffs()
        player_id = env.get_player_id()

        # randomly pick an action from current node's legal_actions
        action = np.random.choice(node.legal_actions[player_id])

        while not env.is_over():
            # step forward in the environment until game is over
            # execute the chosen action in env, get the next state and player id for next steps
            next_state, next_player_id = env.step(action)
            if not env.is_over():
                # if game is not over, next_action is randomly selected from legal_actions in next_state
                action = np.random.choice(next_state['legal_actions'])
        reward = env.get_payoffs()

        return reward

    def back_prop(self, node, rewards):

        while node is not None:

            for i in range(self.env.player_num):
                node.update(rewards[i], i)

            node = node.parent

def main():
    config = {
        'seed': None,
        'use_conv': False,
        'allow_step_back': True,
        'allow_raw_data': True,
        'record_action': True,
        'single_agent_mode': False,
        'active_player': None,
    }

    start = timeit.default_timer()

    # Make environment
    eval_env = DoudizhuEnv(config, state_shape=[5, 4, 15])
    evaluate_num = 1000

    # Set up the agents
    agent = MCTSAgent(env=eval_env, num_simulation=100)
    rule_agent = DouDizhuRuleAgentV1(action_num=eval_env.action_num)

    eval_env.set_agents([agent, rule_agent, rule_agent])

    payoff = eval_tournament(eval_env, evaluate_num)[0]
    print(payoff)
    print(f'time: {timeit.default_timer() - start} for {evaluate_num} episodes')

if __name__ == "__main__":
    main()


__author__ = 'pablogsal'

import numpy as np
from tools import stype
import logging

#Create logger for module
module_logger = logging.getLogger('HADES.geometry')

# This class has the information and contents of the observer map.
# When instantiating the class you must provide an instantiation of
# the input parameters class to create correctly the observer map.
#
# There are two methods that provide print_to_screen and print_to_file
# functionalities.

class image(object):

    def __init__(self,input_par):
        #Get class logger

        self.logger = logging.getLogger('HADES.geometry.image')
        self.logger.info('Creating an instance of image')

        #Renaming things from input_par to easy reading

        # Notice that for the RMHD plane we are using the variable names:
        #
        # x= rho
        # y= z

        dim_x=input_par.x_grid_dim*input_par.scalex
        dim_y=input_par.y_grid_dim*input_par.scaley
        view_angle=input_par.viewing_angle
        beam_radius=input_par.beam_radius
        beam_cells=input_par.beam_radius_cells
        lum_dis= input_par.luminosity_distance
        redshift=input_par.redshift
        self.out_file=input_par.result_file

        #Angle theta

        self.theta=(90.e0-view_angle)*np.pi/180.e0

        # Limits in the z-axis -> Easy as the actual rotation is around the z axis

        self.z_min=-dim_x
        self.z_max=dim_x

        # Limits in the y-axis; y_mi always < y_ma
        # These are the rotation of the points (dimx,0) and (-dimx,dimy)
        # around the z-axis. The transformation is from the lab-frame-basis
        # to the observer-frame-axis.

        self.y_min=-np.sin(self.theta)*dim_x

                #Start of evil hacking
                # We must take a decision based on the round of the y_min parameter
                # ASK JOSE LUIS ABOUT THIS BECAUSE IT IS A MESS

        # if (np.abs(np.round(np.abs(self.y_min))+0.51e0-np.abs(self.y_min)) > 1.e-14):
        #     self.y_min==np.round(self.y_min)+0.51e0

                #End of evil hacking

        self.y_max=np.round(  np.sin(self.theta)*dim_x+np.cos(self.theta)*dim_y)

        #Total number of pixel in the observer's map

        self.total_pixels=((self.z_max-self.z_min)+1)*((self.y_max-self.y_min)+1)

        # Size of the cells in cm

        self.cell_size=beam_radius/beam_cells*3.0856e18

        # Angular distance (in cm)

        self.angular_distance=lum_dis/(1.e0+redshift)/(1.e0+redshift)

        #Size of cells (or pixels) in mas

        self.cell_area = self.cell_size/self.angular_distance*180.e0/np.pi*60.e0*60.e0*1.e3

        # Create the array with indexes

        self.mesh = np.meshgrid( np.linspace(np.int(self.y_min), np.int(self.y_max), np.int(
            self.y_max)-np.int(self.y_min)),np.linspace(np.int(self.z_min), np.int(self.z_max),
            np.int(self.z_max)- np.int(self.z_min)))

        self.mesh= np.array(self.mesh).astype(int)

        # Create an empty array

        self.test = np.zeros([np.round(self.y_max-self.y_min), np.round(
                self.z_max-self.z_min)], dtype=np.float64)

    def print_to_file(self):
            self.logger.info('Requested print value table to file '+self.out_file+'.')

            outfile=open(self.out_file, 'a')
            outfile.write('\n'+"-----Size map properties--------"+'\n')

            outfile.write('Pixels in the map: '+str( np.round(self.y_max-self.y_min))+' x '+ str( np.round(
                self.z_max-self.z_min))+'  (horizontal x vertical)'+'\n')
            outfile.write('Size of pixels in mas: '+str(self.cell_area)+'\n')

            outfile.write ("-----End Size map properties----"+'\n')

            self.logger.info('Value table correctly written to file '+self.out_file+'.')

    def print_to_screen(self):

            stype('\n'+'Observer map properties:')

            stype('Pixels in the map: '+str( np.round(self.y_max-self.y_min))+' x '+ str( np.round(
                self.z_max-self.z_min))+'  (horizontal x vertical)')
            stype('Size of pixels in mas: '+str(self.cell_area)+'\n')

def tracer_limits(tracer,limit):

        #Get class logger

        logger = logging.getLogger('HADES.geometry.tracer_limits')
        logger.info('Requested a calculation of jet limits along the z axis')

        #Redefinitions to easier access to dimensions and data

        dim_x=tracer.shape[0]
        dim_y=tracer.shape[1]

        # Get the values along the z axis (of the Jet, in the rho-z plane) when the jet starts.
        # These values come from the criterion of where the tracer is greater than some value.

        logger.info('Limits along the z axis correctly calculated')
        return np.array([np.where(x>limit)[0][-1] for x in tracer.transpose()])

def tracerQ(jet_limits,point):

    return jet_limits[point[1]] > point[0]

# This function is to tranform from the x_cell and y_cell in the observer map to the
# x_cell (rho) and y_cell (z) in the RMHD code. It is only a cartesian to polar transformation
# taking into account the rotation between the frames and that the RMHD plane is in a cylinder.
#
# def obs_to_rmhd(input_par,x,y,z):
#
#         # Renaming things from input_par to easy reading
#     # Notice that for the RMHD plane we are using the variable names:
#     #
#     # x= rho
#     # y= z
#
#     dim_x=input_par.x_grid_dim
#     dim_y=input_par.y_grid_dim
#     view_angle=input_par.viewing_angle
#     theta=(90.e0-view_angle)*np.pi/180.e0 #Angle theta
#
#
#     # The x,y,z in lab frame (a rotation of x,y,z)
#
#     x_lab= np.cos(theta) * x - np.sin(theta) * y
#     y_lab= np.cos(theta) * y + np.sin(theta) * x
#     z_lab = z
#
#     #The rho and z coordinates
#
#     rho=np.sqrt(x_lab * x_lab + z_lab * z_lab )
#     z= y_lab
#
#     # Return the rounded values of the variables
#
#     return ( np.int(rho) +1, np.int(z) +1 )

# def x_limit(input_par,y_cell,z_cell):
#
#
#     # Renaming things from input_par to easy reading
#     # Notice that for the RMHD plane we are using the variable names:
#     #
#     # x= rho
#     # y= z
#
#     dim_x=input_par.x_grid_dim
#     dim_y=input_par.y_grid_dim
#     view_angle=input_par.viewing_angle
#     theta=(90.e0-view_angle)*np.pi/180.e0 #Angle theta
#     # As when we revolve the RMHD rho-z plane around the z axis we
#     # are constructing a cylinder, the limits in the x coordinate
#     # (perpendicular to the observer plane) change as we change the z
#     # coordinate because we are slicing the cylinder at different heights.
#     #
#     # To take account of these we must calculate the limits in the RMHD slice
#     # when doing the cylinder thing. This is easy when using the cylinder eq:
#     #
#     # X_max^2 = x^2 + z^2   (x,y,z is the LAB frame)
#     #
#     # So x_lim= Sqrt( x_lim^2-z^2)
#
#     x_lim=np.sqrt(dim_x*dim_x-z_cell*z_cell)
#
#     # Now, the limits for the y variable in the observer map change as the x_lim change. We must recalculate them
#     # as we did in the image class.
#
#     y_min=-np.sin(theta)*x_lim
#     y_max= np.sin(theta)*x_lim+np.cos(theta)*dim_y
#
#
#     if y_cell>y_min and y_cell < y_max:
#
#
#         # Now we can calculate the x limits. These are a little tricky. There are two easy cases: theta=90,0. We
#         # take account of these fist.
#
#         if theta==np.pi/2:
#             x_min=0+0.51
#             x_max=dim_y-0.51
#
#         if theta==0.0:
#             x_min=-int(x_lim)+0.51
#             x_max= int(x_lim)-0.51
#
#         # Now for the serious business. The x lims are the point where the line y=y_cell cuts the RMHD rectangle.
#         # These limits are a little tricky because it depend if the first crossing point is in the z-axis (parallel to) of the RMHD
#         # rectangle of in the rho-axis (parallel to). So first we must calculate if the crossing point is in the z-axis (long one) or
#         # in the rho axis (short) one. If the crossing is in the z axis (parallel to), we must use trigonometry to
#         # calculate these
#         # crossing points. The easiest way is to start with the line in the observer frame -> l=(t,y0), then using the
#         # rotation matrix calculate this line in the LAB frame as l' = R.transpose * (t,y0) and now calculate the
#         # crossing point of l' with the line (x_lim,t)->z axis or the line (t,dim_y) -> rho-axis. Then rotate the
#         # crossing point to the observer frame and you are done.
#         #
#         # In this program we first calculate the crossing point with the z axis and then we check if we must calculate
#         # the other one.
#
#
#
#         if theta != 0.0 and theta != np.pi/2:
#
#             # X_max calculation
#
#             z_cross=  1/(np.cos(theta)) * ( y_cell + x_lim * np.sin(theta))# Cross with z axis in lab-frame
#
#             if 0 < z_cross < dim_y: #This checks if the cross is long away the RMHD (x_lim,dim_y) coordinate.
#                 x_max = 1/(np.cos(theta)) * (x_lim + y_cell * np.sin(theta)) # Cross with z axis in obs-frame
#             else:
#                 x_max= ( dim_y - y_cell * np.cos(theta) ) / np.sin(theta) # Cross with rho axis in OBS-frame
#
#
#
#
#
#
#
#
#             # X_min calculation
#
#             z_cross= y_cell /np.cos(theta) - x_lim * np.tan(theta) # Cross with z axis in lab-frame
#
#             if 0 < z_cross < dim_y: #This checks if the cross is long away the RMHD (x_lim,dim_y) coordinate.
#                 x_min =  - x_lim /np.cos(theta) + y_cell * np.tan(theta)     # Cross with z axis in obs-frame
#             else:
#                 x_min= -y_cell / np.tan(theta)# Cross with rho axis in OBS-frame
#
#
#
#
#
#
#
#         #Hurray! Now we have to round the calculations.
#
#
#                     #Start of evil hacking
#                     # We must round the calculations.
#                     # ASK JOSE LUIS ABOUT THIS BECAUSE IT IS A MESS
#
#
#             if (np.abs(np.round(np.abs(x_min))+0.51e0-np.abs(x_min)) < 1.e-14):
#                 x_min==np.round(x_min)
#             else:
#                 x_min==np.round(x_min)+0.51e0
#
#
#                     #End of evil hacking.
#
#
#             x_max=np.round(x_max)-0.51
#
#
#
#             #Now we check if x_min < x_max and if this is the case, we equal the values so the integration column is 0
#
#             if x_min > x_max:
#
#                 x_min= x_max
#
#     else:
#
#         x_min=1
#         x_max=-1
#
#
#     return (np.round(x_min),np.round(x_max))


from django.db import models
from . import fields
from operator import iand, ior

class Offer(models.Model):
    coupon_code = models.CharField(max_length=30)
    slug = models.SlugField(max_length=30, unique=True)
    short_description=models.CharField(max_length=50)

    popularity=models.IntegerField()
    description=models.CharField(max_length=500)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    valid_from=models.DateTimeField()
    expire_at=models.DateTimeField()

    class Meta:
        ordering = ('popularity', )
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('offers:offer_detail', args=[self.id, self.slug])

class Discount(models.Model):
    """
    Abstract model representing one of several types of monetary
    reductions, as well as a date range they're applicable for, and
    the products and products in categories that the reduction is
    applicable for.
    """

    title = models.CharField(max_length=100)
    active = models.BooleanField(_("Active"), default=False)
    popularity=models.IntegerField()

    products = models.ManyToManyField("Product", blank=True,
                                      verbose_name=_("Products"))
    categories = models.ManyToManyField("Category", blank=True,
                                        related_name="%(class)s_related",
                                        verbose_name=_("Categories"))
    discount_deduct = fields.MoneyField(_("Reduce by amount"))
    discount_percent = fields.PercentageField(_("Reduce by percent"),
                                           max_digits=5, decimal_places=2,
                                           blank=True, null=True)
    max_user_usage=models.IntegerField()

    discount_exact = fields.MoneyField(_("Reduce to amount"))
    valid_from = models.DateTimeField(_("Valid from"), blank=True, null=True)
    valid_to = models.DateTimeField(_("Valid to"), blank=True, null=True)

    class Meta:
        abstract = True

    def __str__(self):
        return self.title

    def all_products(self):
        """
        Return the selected products as well as the products in the
        selected categories.
        """
        filters = [category.filters() for category in self.categories.all()]
        filters = reduce(ior, filters + [Q(id__in=self.products.only("id"))])
        return Product.objects.filter(filters).distinct()


import numpy as np

import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F

from agents.BaseAgent import BaseAgent
from networks.networks import ActorCriticAtari
from utils.RolloutStorage import RolloutStorage

from timeit import default_timer as timer

class Model(BaseAgent):
    def __init__(self, static_policy=False, env=None, config=None, log_dir='/tmp/gym'):
        super(Model, self).__init__(config=config, env=env, log_dir=log_dir)
        self.config = config
        self.static_policy = static_policy
        self.num_feats = env.observation_space.shape
        self.num_actions = env.action_space.n
        self.env = env

        self.declare_networks()

        if not self.config.recurrent_policy_grad:
            self.optimizer = optim.RMSprop(self.model.parameters(), lr=self.config.LR, alpha=0.99, eps=1e-5)
        else:
            self.optimizer = optim.Adam(self.model.parameters(), lr=self.config.LR, amsgrad=True)

        #move to correct device
        self.model = self.model.to(self.config.device)

        if self.static_policy:
            self.model.eval()
        else:
            self.model.train()

        self.config.rollouts = RolloutStorage(self.config.rollout, self.config.num_agents,
            self.num_feats, self.env.action_space, self.model.state_size,
            self.config.device, config.USE_GAE, config.gae_tau)

        self.value_losses = []
        self.entropy_losses = []
        self.policy_losses = []

    def declare_networks(self):
        self.model = ActorCriticAtari(self.num_feats, self.num_actions, self.config.recurrent_policy_grad, self.config.gru_size)

    def get_action(self, s, states, masks, deterministic=False):
        logits, values, states = self.model(s, states, masks)
        dist = torch.distributions.Categorical(logits=logits)

        if deterministic:
            actions = dist.probs.argmax(dim=1, keepdim=True)
        else:
            actions = dist.sample().view(-1, 1)

        log_probs = F.log_softmax(logits, dim=1)
        action_log_probs = log_probs.gather(1, actions)

        return values, actions, action_log_probs, states

    def evaluate_actions(self, s, actions, states, masks):
        logits, values, states = self.model(s, states, masks)

        dist = torch.distributions.Categorical(logits=logits)

        log_probs = F.log_softmax(logits, dim=1)
        action_log_probs = log_probs.gather(1, actions)

        dist_entropy = dist.entropy().mean()

        return values, action_log_probs, dist_entropy, states

    def get_values(self, s, states, masks):
        _, values, _ = self.model(s, states, masks)

        return values

    def compute_loss(self, rollouts, next_value):
        obs_shape = rollouts.observations.size()[2:]
        action_shape = rollouts.actions.size()[-1]
        num_steps, num_processes, _ = rollouts.rewards.size()

        rollouts.compute_returns(next_value, self.config.GAMMA)

        values, action_log_probs, dist_entropy, states = self.evaluate_actions(
            rollouts.observations[:-1].view(-1, *obs_shape),
            rollouts.actions.view(-1, 1),
            rollouts.states[0].view(-1, self.model.state_size),
            rollouts.masks[:-1].view(-1, 1))

        values = values.view(num_steps, num_processes, 1)
        action_log_probs = action_log_probs.view(num_steps, num_processes, 1)

        advantages = rollouts.returns[:-1] - values
        value_loss = advantages.pow(2).mean()

        action_loss = -(advantages.detach() * action_log_probs).mean()

        loss = action_loss + self.config.value_loss_weight * value_loss
        loss -= self.config.entropy_loss_weight * dist_entropy

        return loss, action_loss, value_loss, dist_entropy

    def update(self, rollout, next_value):
        loss, action_loss, value_loss, dist_entropy = self.compute_loss(rollout, next_value)

        self.optimizer.zero_grad()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.config.grad_norm_max)
        self.optimizer.step()

        #self.save_loss(loss.item(), action_loss.item(), value_loss.item(), dist_entropy.item())

        return value_loss.item(), action_loss.item(), dist_entropy.item()

    def save_distance(self, max_dist, tstep):
        pass

    '''def save_loss(self, loss, policy_loss, value_loss, entropy_loss):
        super(Model, self).save_loss(loss)
        self.policy_losses.append(policy_loss)
        self.value_losses.append(value_loss)
        self.entropy_losses.append(entropy_loss)'''


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 22:03:24 2018

@author: ly
"""
import pandas as pd
import numpy as np
import networkx as nx
import plotly.plotly as py
import plotly.graph_objs as go
import math

def processDF():
    # Read data frame
    df = pd.read_csv('cleaned_data.csv', sep=',',encoding='utf-8-sig',
                     usecols = ['Movie_genre','Movie_name'])
    # Drop duplicated movie name
    df = df.drop_duplicates(subset = 'Movie_name')
    # Split genre column to calculate the number of genres a movie has
    genre = df['Movie_genre']
    genre_list = [i.split(',') for i in genre.tolist()]
    # Add a new column for the nubmer of genres a movie belongs to
    length = [len(i) for i in genre_list]
    df['length'] = length
    # Create e new data frame, repeat movie name for certain times
    # depending on the numbe of genres
    df_temp = pd.DataFrame({'movie':np.repeat(df['Movie_name'], length)})

    # split genre again but into separate columns
    genre_split = genre.str.split(',',expand = True)
    # Transpose the genre columns to add to the new data frame
    row_temp = pd.Series()
    for index, row in genre_split.iterrows():
        row_temp = pd.concat([row_temp,row], ignore_index=True)
    row_temp = row_temp.dropna()
    # Add the new genre column for the temporary data frame
    df_temp['genre'] = row_temp.tolist()

    return df_temp

# The code below is adapted from two example from plotly tutorials
# Reference:
# https://plot.ly/python/network-graphs/
# https://plot.ly/python/custom-buttons/

def plotNetwork(df):
    # Use networkx to create a graph directly from the tailored data frame
    G = nx.from_pandas_edgelist(df, source='movie', target='genre')

    # Set node positions

    pos = nx.kamada_kawai_layout(G)
    for node in G.nodes():
        G.node[node]['pos']= pos[node]

    # Create node trace:
    node_trace = go.Scatter(
                            x=[],
                            y=[],
                            name='Node Trace',
                            text=[],
                            mode='markers',
                            hoverinfo='text',
                            marker=dict(
                                showscale=True,
                                # colorscale options
                                #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
                                #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
                                #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
                                colorscale='Jet',
                                reversescale= True,
                                color=[],
                                size= [],
                                colorbar=dict(
                                    thickness=15,
                                    title='Node Connections',
                                    xanchor='left',
                                    titleside='right'
                                ),
                                line=dict(width=2)))

    for node in G.nodes():
        x, y = G.node[node]['pos']
        node_trace['x'] += tuple([x])
        node_trace['y'] += tuple([y])

    # Create edge trace:
    edge_trace = go.Scatter(
                            x=[],
                            y=[],
                            name='Edge Trace',
                            line=dict(width=0.5,color='#888'),
                            hoverinfo='none',
                            mode='lines')

    for edge in G.edges():
        x0, y0 = G.node[edge[0]]['pos']
        x1, y1 = G.node[edge[1]]['pos']
        edge_trace['x'] += tuple([x0, x1, None])
        edge_trace['y'] += tuple([y0, y1, None])

    # Color and resize nodes by degree
    for node, adjacencies in enumerate(G.adjacency()):
        node_trace['marker']['color']+=tuple([len(adjacencies[1])])
        node_info = str(adjacencies[0]) + '
'+'# of connections: '+str(len(adjacencies[1]))
        node_trace['text']+=tuple([node_info])
        # Use log transformation to alleviate extremely large values
        # Multiply by 3 so that small nodes are visible
        node_trace['marker']['size']+=tuple([math.log1p(len(adjacencies[1]))*3])
        # math.log1p(x)
        # Return the natural logarithm of 1+x (base e).
        # The result is calculated in a way which is accurate for x near zero.

    # Data is ready!
    data = [edge_trace, node_trace]

    # Set the layout
    layout = go.Layout(
                     title = '
Movies and Genres Network',
                     titlefont = dict(size = 18),
                     showlegend = True,
                     legend=dict(
                        x = 0.8,
                        y = 1.065,
                        font=dict(size=12)
                     ),
                     margin = dict(b = 20, l = 10, r = 5, t = 40),
                     xaxis = dict(showgrid = False,
                                   zeroline = False,
                                   showticklabels = False),
                     yaxis = dict(showgrid = False,
                                   zeroline = False,
                                   showticklabels = False))
    # Create update menus
    updatemenus=list([
            dict(
            # Create buttons for changing node color scale
            buttons=list([
                dict(
                    args=['marker.colorscale','Jet'],
                    label='Jet',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','YlOrRd'],
                    label='YlOrRd',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Reds'],
                    label='Reds',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Picnic'],
                    label='Picnic',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Electric'],
                    label='Electric',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Viridis'],
                    label='Viridis',
                    method='restyle'
                ),
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.06,
            xanchor = 'left',
            y = 1.065,
            yanchor = 'top'
        ),
        # Create reverse color scale buttons
        dict(
            buttons=list([
                dict(
                    args=['marker.reversescale', True],
                    label='Reverse',
                    method='restyle'
                ),
                dict(
                    args=['marker.reversescale', False],
                    label='Undo',
                    method='restyle'
                )
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.32,
            xanchor = 'left',
            y = 1.13,
            yanchor = 'top'
        ),
        # Create buttons for changing edge color
        dict(
            buttons=list([
                dict(
                    args=['line.color', 'rgba(100,90,80,0.5)'],
                    label='Gray',
                    method='restyle'
                ),
                dict(
                    args=['line.color', 'rgba(0,176,246,0.5)'],
                    label='Blue',
                    method='restyle'
                ),
                dict(
                    args=['line.color', 'rgba(231,107,243,0.5)'],
                    label='Pink',
                    method='restyle'
                )
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.06,
            xanchor = 'left',
            y = 1.13,
            yanchor = 'top'
        )
    ])

    # Add annotations
    annotations = list([
        dict(text='Node Color
Scale', x=0, y=1.05,xref = 'paper', yref='paper', align='left', showarrow=False),
        dict(text='Node Reverse', x=0.24, y=1.11,xref = 'paper',  yref='paper', showarrow=False),
        dict(text='Edge Color', x=0, y= 1.11, xref = 'paper', yref='paper', showarrow=False),
    ])

    layout['updatemenus'] = updatemenus
    layout['annotations'] = annotations
    # Layout is ready!

    # Figure is ready!
    fig = go.Figure(data = data,layout = layout)
    # Plot it!
    py.iplot(fig, filename = 'networkx')

def main():

    df = processDF()
    plotNetwork(df)

if __name__ == "__main__":
    main()

Movie Soundtracks Analysis/Interactive Chart/movie genre network - interactive.py


# Random Forest Classification

# Importing the libraries
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('train.csv')
X_train = dataset.iloc[:, [0,2, 4,5,6,7]].values
y_train = dataset.iloc[:, 1].values

test_dataset = pd.read_csv('test.csv')
X_test= test_dataset.iloc[:, [0,1,3,4,5,6]].values

# taking care of missing values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X_train[:, 3:4])
X_train[:, 3:4] = imputer.transform(X_train[:, 3:4])
imputer_test = imputer.fit(X_test[:, 3:4])
X_test[:, 3:4] = imputer_test.transform(X_test[:, 3:4])

# One hot encoder
from sklearn.preprocessing import LabelEncoder , OneHotEncoder
labelEncoder_X = LabelEncoder()
X_train[:, 2] = labelEncoder_X.fit_transform(X_train[:, 2])
oneHotEncoder= OneHotEncoder(categorical_features = [2])
X_train = oneHotEncoder.fit_transform(X_train).toarray()

X_test[:, 2] = labelEncoder_X.fit_transform(X_test[:, 2])
oneHotEncoder= OneHotEncoder(categorical_features = [2])
X_test = oneHotEncoder.fit_transform(X_test).toarray()

# Splitting the dataset into the Training set and Test set
'''from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)'''

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting Random Forest Classification to the Training set
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)


from scipy.misc import imread
from scipy.io import loadmat
import numpy as np
import sys
import os
import xml.etree.ElementTree as ET
from matplotlib import pyplot as plt
import skimage.io as sio
from scipy.ndimage import zoom
import torch
import torch.utils.data as data

VOC_CLASSES = [
    'aeroplane', 'bicycle', 'bird', 'boat',
    'bottle', 'bus', 'car', 'cat', 'chair',
    'cow', 'diningtable', 'dog', 'horse',
    'motorbike', 'person', 'pottedplant',
    'sheep', 'sofa', 'train', 'tvmonitor']

def sample_img_pairs(root='/home/seungkwan/data/', name='sbd_val'):
    items = []
    name_to_index = dict(zip(VOC_CLASSES, range(len(VOC_CLASSES))))

    rootpath = os.path.join(root, 'VOCdevkit2007', 'VOC2012')
    for line in open(os.path.join(rootpath, 'ImageSets', 'Segmentation', name + '.txt')):
        data = {}
        id = line.strip()
        target = ET.parse(os.path.join(rootpath, 'Annotations', id + '.xml'))

        category_set = np.zeros(21, dtype=np.long)
        easy_category_set = np.zeros(21, dtype=np.long)

        for obj in target.iter('object'):
            cls_name = obj.find('name').text.strip().lower()
            truncatged = int(obj.find('truncated').text.strip())
            difficult = int(obj.find('difficult').text.strip())
            category = name_to_index[cls_name] + 1

            category_set[category] = 1

            if truncatged == 0 and difficult == 0:
                easy_category_set[category] = 1

        if np.sum(easy_category_set) == 0:
            continue
        data['id'] = id
        data['category_set'] = category_set
        data['easy_category_set'] = easy_category_set
        data['img_path'] = os.path.join(rootpath, 'JPEGImages', line.strip() + '.jpg')
        data['seg_path'] = os.path.join(rootpath, 'SegmentationClassAug/SegmentationClassAug', line.strip() + '.png')
        category_area = np.zeros(21)
        seg_map = imread(data['seg_path'])

        total_area = seg_map.shape[0] * seg_map.shape[1]
        for cls in range(21):
            category_area[cls] = np.sum(seg_map == cls) / total_area

        data['category_area'] = category_area
        items.append(data)

    print('num_images: %d' % len(items))

    img_pairs = []
    co_category_cnt = np.zeros(21, dtype=np.long)

    cls_including_img = [[] for i in range(21)]
    for i in range(len(items)):
        cls_set = np.where(items[i]['easy_category_set'])[0]
        for cls in cls_set:
            cls_including_img[cls].append(i)

    for i in range(len(items)):
        i_cls_set = np.where(items[i]['easy_category_set'])[0]
        for cls in i_cls_set:
            cnt = 0
            for j in cls_including_img[cls]:
                if cnt == 20:
                    break
                if j <= i:
                    continue
                co_cls = np.where(items[i]['easy_category_set'] * items[j]['easy_category_set'])[0]
                co_area_i = np.sum(items[i]['category_area'][co_cls])
                co_area_j = np.sum(items[j]['category_area'][co_cls])

                th = 0.1
                if co_area_i > th and co_area_j > th:
                    img_pairs.append([items[i]['id'], items[j]['id']])
                    co_category_cnt = co_category_cnt + (items[i]['category_set'] * items[j]['category_set'])
                    cnt += 1
    # for i in range(len(items)):
    #     for j in range(i + 1, len(items)):
    #         co_cls = np.where(items[i]['easy_category_set'] * items[j]['easy_category_set'])[0]
    #         if len(co_cls) == 0:
    #             continue
    #         if np.sum(items[i]['category_set']) + np.sum(items[j]['category_set']) <= 2:
    #             continue
    #
    #         co_category_cnt = co_category_cnt + (items[i]['category_set'] * items[j]['category_set'])
    #         co_area_i = np.sum(items[i]['category_area'][co_cls])
    #         co_area_j = np.sum(items[j]['category_area'][co_cls])
    #
    #         th = 0.1
    #         if co_area_i > th and co_area_j > th:
    #             img_pairs.append([items[i]['id'], items[j]['id']])

    print(co_category_cnt, np.sum(co_category_cnt))
        # print(i)
    print('num_pairs: %d' % len(img_pairs))
    # img_pairs = np.array(img_pairs)
    # img_pairs = img_pairs[np.random.choice(len(img_pairs), 40000, replace=False)]
    np.save('/home/seungkwan/data/docs_pairs_val.npy', img_pairs)

class DOCSDatasetCOCO(data.Dataset):
    def __init__(self, root='/home/seungkwan/data/', name='train'):
        self.name_to_index = dict(zip(VOC_CLASSES, range(len(VOC_CLASSES))))
        self.rootpath = os.path.join(root, 'VOCdevkit2007', 'VOC2012')
        self.pair_list = np.load('/home/seungkwan/data/docs_pairs_%s.npy' % name)
        self.rgb_means = [122.67892, 116.66877, 104.00699]
        self.img_size = 512

    def __len__(self):
        return len(self.pair_list)

    def _load_img(self, im_id):
        img_path = os.path.join(self.rootpath, 'JPEGImages', im_id + '.jpg')
        seg_path = os.path.join(self.rootpath, 'SegmentationClassAug/SegmentationClassAug', im_id + '.png')
        anno = ET.parse(os.path.join(self.rootpath, 'Annotations', im_id + '.xml'))

        category_set = np.zeros(21, dtype=np.long)

        for obj in anno.iter('object'):
            cls_name = obj.find('name').text.strip().lower()
            category = self.name_to_index[cls_name] + 1
            category_set[category] = 1

        im = imread(img_path)
        seg = imread(seg_path)

        h, w = im.shape[:2]
        if h >= w and h > self.img_size:
            im = zoom(im, (self.img_size / h, self.img_size / h, 1))
            h, w = im.shape[:2]
        elif w >= h and w > self.img_size:
            im = zoom(im, (self.img_size / w, self.img_size / w, 1))
            h, w = im.shape[:2]

        pad_top = (self.img_size - h) // 2
        pad_lef = (self.img_size - w) // 2
        pad_bottom = self.img_size - h - pad_top
        pad_right = self.img_size - w - pad_lef
        pad = ((pad_top, pad_bottom), (pad_lef, pad_right), (0, 0))
        im_padded = np.pad(im, pad, 'constant', constant_values=0)
        seg_padded = np.pad(seg, (pad[0], pad[1]), 'constant', constant_values=0)
        im_padded = im_padded.astype(np.float32)
        im_padded -= self.rgb_means

        return im_padded, seg_padded, category_set

    def __getitem__(self, idx):
        a_id = self.pair_list[idx, 0]
        b_id = self.pair_list[idx, 1]

        a_img, a_seg_amp, a_category = self._load_img(a_id)
        b_img, b_seg_amp, b_category = self._load_img(b_id)

        co_category = a_category * b_category
        a_coseg_map = np.zeros(a_seg_amp.shape, dtype=np.long)
        b_coseg_map = np.zeros(b_seg_amp.shape, dtype=np.long)

        for cls in range(1, 21):
            if co_category[cls] == 0:
                continue
            a_coseg_map = a_coseg_map + (a_seg_amp == cls)
            b_coseg_map = b_coseg_map + (b_seg_amp == cls)

        a_img = torch.from_numpy(a_img).permute(2, 0, 1)
        b_img = torch.from_numpy(b_img).permute(2, 0, 1)
        a_coseg_map = torch.from_numpy(a_coseg_map)
        b_coseg_map = torch.from_numpy(b_coseg_map)
        return a_img, b_img, a_coseg_map, b_coseg_map

if __name__ == '__main__':
    # dataset = DOCSDataset()
    # for i in range(0, len(dataset), 30):
    #     a_img, b_img, a_coseg, b_coseg, co_category = dataset[i]
    #     plt.imshow(a_img)
    #     plt.show()
    #     plt.imshow(a_coseg)
    #     plt.show()
    #
    #     plt.imshow(b_img)
    #     plt.show()
    #     plt.imshow(b_coseg)
    #     plt.show()

    sample_img_pairs()


import time, psutil, asyncio
import globals, emformat
from discord import __version__ as discordversion
from discord.ext import commands,tasks

# ['stats']=['stats']

class Stats(commands.Cog):
	def __init__(self,bot):
		self.bot=bot

		self.status='online'
		self.exposure="populating..."
		self.responses="0/0 messages responded to since last reboot."
		self.sentcount=0
		self.recievedcount=0
		self.starttime=time.time()
		self.uptime="calculating..."
		self.d=self.h=self.m=self.s=0
		self.modules='populating...'
		self.library="discord.py v"+discordversion
		self.core="merelybot v"+globals.ver
		self.memes="unavailable"
		self.cpu_usage='0%'
		self.ram_usage='0MB/0MB (0%)'
		self.hardware='merely is running on a custom windows server.'
		self.gentime='please wait...'
		self.lastuptime=0

		self.inituptime()
		self.runstats.start()

	def cog_unload(self):
		self.runstats.cancel()

	@commands.command(pass_context=True, no_pm=False, aliases=['status','ver','version'])
	async def stats(self,ctx):
		if globals.verbose:print('stats command')
		await emformat.make_embed(ctx.message.channel,'for constantly updating stats, go to '+globals.apiurl+'stats.html',
		'merely stats','',color=0x2C5ECA,thumbnail=globals.emurl+'greet.gif',
		fields={
			'exposure': self.exposure,
			'responses': self.responses,
			'uptime': self.uptime,
			'modules': self.modules,
			'library': self.library,
			'core': self.core,
			#'memes': self.memes,
			'cpu usage': self.cpu_usage,
			'ram usage': self.ram_usage,
			'hardware': self.hardware,
			'generated time': self.gentime
		},
		icon=globals.iconurl,footer="merely v"+globals.ver+" - created by Yiays#5930",link=globals.apiurl)

	def inituptime(self):
		with open(globals.store+'uptime.txt','r') as f:
			line=f.readline() #get starting point
			savelines=[]
			self.upmins=0
			self.downmins=0
			i=0
			sprint=0
			for line in f.readlines():
				ii=[int(x) for x in line.split('-')]
				if ii[0]>i+2:
					self.upmins+=i-sprint
					self.downmins+=ii[0]-i
					savelines.append(str(sprint)+'-'+str(i))
					sprint=ii[0]
				i=ii[-1]
			savelines.append(str(sprint)+'-'+str(i))
		with open(globals.store+'uptime.txt','w') as f:
			for line in savelines:
				f.write(line+'\n')

	@tasks.loop(seconds=1.0)
	async def runstats(self):
		try:
			if round(time.time())%60==0 and round(time.time()/60)!=self.lastuptime:
				self.lastuptime=round(time.time()/60)
				with open(globals.store+'uptime.txt','a') as f:
					f.write(str(round(time.time()/60)-26394323)+'\n')
				self.upmins+=1

			self.m, self.s = divmod(time.time()-self.starttime, 60)
			self.h, self.m = divmod(self.m, 60)
			self.d, self.h = divmod(self.h, 24)

			self.exposure="**"+str(len(self.bot.guilds))+"** servers with **"+str(sum([len(s.members) for s in self.bot.guilds]))+"** members."
			self.responses=str(self.sentcount)+"/"+str(self.recievedcount)+" messages responded to since last reboot."
			self.modules=', '.join(list(globals.modules.keys()))
			self.uptime=str(round((self.upmins*100)/max(self.upmins+self.downmins,1),2))+'%'
			self.cpu_usage=str(psutil.cpu_percent())+'%'
			self.ram_usage=str(round(psutil.virtual_memory().used/1000000))+'MB ('+str(round((psutil.virtual_memory().used*100)/psutil.virtual_memory().total))+'%)'
			self.gentime=time.asctime(time.localtime())+'+1300'
		except Exception as e:
			print(repr(e))


from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import openpyxl
import time

from tkinter import *

jntua = webdriver.Chrome(executable_path="webdrivers/chromedriver.exe")

def fetch():
    xl = openpyxl.Workbook()

    sheet = xl.active

    # ----- year -------

    b_year = year.get()
    # -----------------

    # ------ sem -------

    b_sem = sem.get()

    # -----------------

    # ----- branch -----

    b_branch = branch.get()
    # -----------------

    # ------------ Subjects -----------------

    if b_branch == "CSE":
        if b_year == 1 and b_sem == 1:
            cse = ['CP LAB', 'ELCS LAB', 'ED', 'PHYSICS', 'CP', 'M-1', 'ENGLISH']
        elif b_year == 1 and b_sem == 2:
            cse = ['ENG & IT WORKSHOP', 'CHEMISTRY LAB', 'DS LAB', 'ES', 'CHEMISTRY', 'DS', 'M-2', 'ENGLISH']
        elif b_year == 2 and b_sem == 1:
            cse = ['BEEE LAB', 'BEEE', 'M-3', 'MEFA', 'DBMS LAB', 'DM', 'DBMS', 'DLD']
        elif b_year == 2 and b_sem == 2:
            cse = ['P AND S', 'ONLINE TEST 1', 'JAVA LAB', 'FLAT', 'OOPJ', 'CO', 'SE', 'MPI LAB', 'MPI']
        elif b_year == 3 and b_sem == 1:
            cse = ['SOCIAL VALUES', 'OS LAB', 'OOAD LAB', 'BD', 'ST', 'PPL', 'OOAD', 'CN', 'OS']
        elif b_year == 3 and b_sem == 2:
            cse = ['ENGLISH LAB', 'ONLINE TEST 2', 'DWDM LAB', 'WIT LAB', 'WIT', 'DAA', 'DP', 'DWDM', 'CD',
                   'IPR']  # Subject Names
        elif b_year == 4 and b_sem == 1:
            cse = ['MS', 'MAD LAB', 'GCC LAB', 'RTS', 'SA', 'MAD', 'IS', 'GCC']
        else:
            cse = ['PROJECT WORK', 'TECHNICAL SEMINAR', 'VIVA VOCE', 'CS', 'MC']

        subject=cse.copy()

    # ---------------------------------------

    link = url_link.get()

    rline = 3

    start_hall = hall_ticket = start_hallticket.get()
    end_hall = end_hallticket.get()
    start_limit = int(start_hall[8:])
    end_limit = int(end_hall[8:])
    limit = end_limit - start_limit + 1
    code = int(hall_ticket[7:])

    rows=len(subject)
    column=subject*3

    for i in range(1, rows * 3, 3):
        sheet.cell(row=2, column=i + 1).value = 'MID'
        sheet.cell(row=2, column=i + 2).value = 'SEM'  # sub heading of subjects
        sheet.cell(row=2, column=i + 3).value = 'P/F'

    for i in range(1, rows * 3, 3):
        sheet.cell(row=1, column=i + 2).value = subject.pop()  # Subjects names Entering into Excel file

    for i in range(limit):

        hall_ticket = hall_ticket[:7] + str(code)

        jntua.get(link)
        jntua.find_element_by_class_name('txt').send_keys(hall_ticket)
        jntua.find_element_by_class_name('ci').click()

        code += 1
        try:
            jntua.implicitly_wait(10)
            jntua.find_element_by_xpath('/html/body/div/div[1]/div/div/center/div[1]/table')

        except NoSuchElementException:
            continue

        sheet.cell(row=rline, column=1).value = hall_ticket

        rows = len(jntua.find_elements_by_xpath(
            '//*[@id="rs"]/table/tbody/tr'))  # len of rows in tables also known as Subject length if "rows-2"
        cols = len(jntua.find_elements_by_xpath('//*[@id="rs"]/table/tbody/tr[1]/th'))  # len of the columns

        cline = 2

        for r in range(2, rows):
            for c in range(3, 7):
                if c == 5:
                    continue
                data = jntua.find_element_by_xpath(
                    "//*[@id='rs']/table/tbody/tr[" + str(r) + "]/td[" + str(c) + "]").text
                sheet.cell(row=rline, column=cline).value = data  # storing Marks into Excel file
                cline = cline + 1

        rline = rline + 1

    txt.delete(0.0, 'end')
    txt.insert(0.0, "\nData Entered Successfully....\n")
    txt.insert(0.0, '============ Log =============\n')

    file = "results/" + str(b_year)
    xl.save(file + " Year " + str(b_sem) + " Sem " + b_branch + " Results.xlsx")

def jntuaresults():
    jntua.get("https://jntuaresults.ac.in/")

gui = Tk()

gui.geometry('1000x500+430+250')

gui.title("SVIT JNTUA SEMESTER RESULTS AUTOMATION TOOL")
Label(text="Sri Venkateswara Institute Of Technology (SVIT)", fg="black", font=('Comic Sans MS', 15)).place(x=300, y=1)

# -------- Year ------

Label(text="Year --->  ", font=13).place(x=100, y=100)
year = IntVar()
Radiobutton(gui, text="1", variable=year, value=1, font=10).place(x=220, y=100)
Radiobutton(gui, text="2", variable=year, value=2, font=10).place(x=300, y=100)
Radiobutton(gui, text="3", variable=year, value=3, font=10).place(x=380, y=100)
Radiobutton(gui, text="4", variable=year, value=4, font=10).place(x=460, y=100)
# --------------------

# -------- Sem -------

Label(text="Sem --->  ", font=13).place(x=100, y=150)
sem = IntVar()
Radiobutton(gui, text="1", variable=sem, value=1, font=10).place(x=220, y=150)
Radiobutton(gui, text="2", variable=sem, value=2, font=10).place(x=300, y=150)

# -------------------

# ----------- Branch -------------

branch = StringVar()
branch.set(" ")
Label(text="Branch --->  ", font=13).place(x=100, y=200)
Radiobutton(gui, text="CSE", variable=branch, value="CSE", font=10).place(x=220, y=200)
Radiobutton(gui, text="ECE", variable=branch, value="ECE", font=10).place(x=300, y=200)
Radiobutton(gui, text="EEE", variable=branch, value="EEE", font=10).place(x=380, y=200)
Radiobutton(gui, text="Civil", variable=branch, value="Civil", font=10).place(x=460, y=200)
Radiobutton(gui, text="Mech", variable=branch, value="Mech", font=10).place(x=540, y=200)

# --------------------------------

# -------- Hallticket no. ---------

start_hallticket = StringVar()
end_hallticket = StringVar()

Label(text="Hallticket No. --->", font=13).place(x=100, y=250)

Label(text="Start : ", font=10).place(x=300, y=250)
Entry(gui, justify="center", textvariable=start_hallticket).place(x=380, y=255)

Label(text="End : ", font=10).place(x=520, y=250)
Entry(gui, justify="center", textvariable=end_hallticket).place(x=600, y=255)
# ---------------------------------

# ------- External Entry ---------

ext_entry = StringVar()
Label(text="External Entry (optional) --->", font=13).place(x=100, y=300)
Entry(gui, justify="center", textvariable=ext_entry).place(x=380, y=305)

Button(gui, text="ADD", command=fetch).place(x=550, y=300)
# ---------------------------------

# ---------- Get URl -------------

url_link = StringVar()

Label(text="URL --->", font=13).place(x=100, y=350)
Entry(gui, justify="center", width=50, textvariable=url_link).place(x=200, y=355)

Button(gui, text="Get Link", command=jntuaresults).place(x=550, y=350)

# -------------------------------

# -------- Fetch Button ---------

Button(gui, text="Fetch It", command=fetch).place(x=400, y=400)
# -------------------------------
# ---------- Log ----------------

txt = Text(gui, width=30, height=5)
txt.place(x=740, y=60)
txt.insert(0.0, '============ Log =============')

# -------------------------------

mainloop()
# -------------------------------------------------------------------------------------------------------------------


#!/usr/bin/env python

'''
script.py: part of singularity command line tool
Runtime executable, "shub"

'''

from singularity.package import package, docker2singularity
from singularity.runscript import get_runscript_template
from singularity.utils import check_install
from singularity.app import make_tree
from glob import glob
import argparse
import sys
import os

def main():
    parser = argparse.ArgumentParser(
    description="package Singularity containers for singularity hub.")
    parser.add_argument("--image", dest='image', help="full path to singularity image (for use with --package)", type=str, default=None)
    parser.add_argument("--docker2singularity", dest='docker', help="name of Docker image to export to Singularity (does not include runscript cmd)", type=str, default=None)
    parser.add_argument("--outfolder", dest='outfolder', help="full path to folder for output, if not specified, will go to pwd", type=str, default=None)
    parser.add_argument("--runscript", dest='runscript', help="specify extension to generate a runscript template in the PWD, or include --outfolder to change output directory. Currently supported types are py (python).", type=str, default=None)
    parser.add_argument('--package', help="package a singularity container for singularity hub", dest='package', default=False, action='store_true')
    parser.add_argument('--tree', help="view the guts of an image or package.", dest='tree', default=False, action='store_true')
    try:
        args = parser.parse_args()
    except:
        parser.print_help()
        sys.exit(0)

    # Output folder will be pwd if not specified
    if args.outfolder == None:
        output_folder = os.getcwd()
    else:
        output_folder = args.outfolder

    # If the user wants a runscript template.
    if args.runscript != None:
        get_runscript_template(output_folder=output_folder,
                               script_name="singularity",
                               language=args.runscript)

    # If the user wants to export docker2singularity!
    if args.docker != None:
        docker2singularity(output_folder=output_folder,
                           docker_image=args.docker)

    # We can only continue if singularity is installed
    if check_install() == True:

       # We also must have an image to work with
       if args.image !=None:
           image = os.path.abspath(args.image)

           # Make sure the image exists!
           if os.path.exists(image):

               # the user wants to make a tree for an image
               if args.tree == True:
                   make_tree(image)

               # The user wants to package the image
               elif args.package == True:
                   package(image_path=image,
                           output_folder=output_folder,
                           runscript=True,
                           software=True)

       else:
          print("Please specify a singularity image with --image.")

if __name__ == '__main__':
    main()


import pandas
import config
from sklearn.utils import resample
import MachineLearning

def binary_to_char(value):
    return 'N' if value == 0 else 'Y'

ml = MachineLearning.MachineLearning()

data = pandas.read_csv(config.DATA_PATH + 'ortopedia.csv', sep=';')

# Convertendo coluna de classificação para caractere
data.Fusao_de_Vertebras = data.Fusao_de_Vertebras.map(binary_to_char)

# Verificando balanceamento de classes
print(data.Fusao_de_Vertebras.value_counts())

# Separando dados das classes
minor = data[data['Fusao_de_Vertebras'] == 'Y']
major = data[data['Fusao_de_Vertebras'] == 'N']

# Rebalanceando a classe com menor número de registros
minor_up_sample = resample(minor, replace=True, n_samples=7900, random_state=None)

# Criando novo dataframe com os dados balanceados
balanced_data = pandas.concat([major, minor_up_sample])

# Separando as colunas de dados da coluna de classificação
attributes = balanced_data.drop(['#', 'Fusao_de_Vertebras'], axis=1)
classes = balanced_data['Fusao_de_Vertebras']

data = minor = major = minor_up_sample = balanced_data = None

attributes = pandas.get_dummies(attributes)

# LOGISTIC REGRESSION
filename = 'orthopedics_lr'

x_training, x_testing, y_training, y_testing = ml.train_test_split(attributes, classes)

# Módulo indutor
print("\nGerando modelo de Regressão Logística")
response, y_predict = ml.generate_logistic_regression(
    x_training, x_testing, y_training, filename
)

print(response)

matrix = MachineLearning.conf_matrix(y_testing, y_predict)

MachineLearning.report(y_testing, y_predict)

print("\nAcurácia")
acc = MachineLearning.accuracy(matrix)
print(acc)

print("\nSensibilidade")
sen = MachineLearning.sensitivity(matrix)
print(sen)

print("\nEspecificidade")
spe = MachineLearning.specificity(matrix)
print(spe)

print("\nValidando modelo")
validate = MachineLearning.validate_model(filename, x_testing, y_testing)
print(validate)


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import re
import os
import csv
from configparser import ConfigParser

parser = ConfigParser()
parser.read(os.path.join(os.path.dirname(__file__),
                         '..', 'config.cfg'), encoding='utf-8')
driverLocation = parser.get('linkscraper settings', 'driverLocation')
startDate = parser.get('linkscraper settings', 'startDate')
endDate = parser.get('linkscraper settings', 'endDate')
county = parser.get('linkscraper settings', 'county')
countyKey = parser.get('linkscraper settings', 'countyKey')

outputLocation = os.path.join(os.path.dirname(__file__),
                              '..',
                              'input',
                              'URLs.csv')

class driver:
    """Driver instance"""

    def __init__(self, chromedriverPath,
                 startDate='',
                 endDate='',
                 county='',
                 countyKey='',
                 headless=False):
        """
        Initialize driver instance
        """
        self._default = (
            'http://www.landchina.com/default.aspx?tabid=263&ComName=default'
        )
        self._startDate = startDate
        self._endDate = endDate
        self._county = county
        self._countyKey = countyKey
        options = Options()
        if headless:
            options.headless = True
        self._driver = webdriver.Chrome(chromedriverPath, options=options)
        self._URLList = []

    # Getters and Setters
    @property
    def startDate(self):
        return self._startDate

    @startDate.setter
    def startDate(self, newStartDate):
        self._startDate = newStartDate

    @property
    def endDate(self):
        return self._endDate

    @endDate.setter
    def endDate(self, newEndDate):
        self._endDate = newEndDate

    @property
    def county(self):
        return self._county

    @county.setter
    def county(self, newCounty):
        self._county = newCounty

    @property
    def countyKey(self):
        return self._countyKey

    @countyKey.setter
    def countyKey(self, newCountyKey):
        self._countyKey = newCountyKey

    @property
    def URLList(self):
        return self._URLList

    @URLList.setter
    def URLList(self, newURLList):
        self._URLList = newURLList

    def _goToLocation(self):
        self._driver.get(self._default)

    def _enterSearchParams(self):
        """Enter search filter parameters"""
        # Enter parameters
        WebDriverWait(self._driver, 20).until(
            EC.element_to_be_clickable(
                (By.ID, 'TAB_QueryConditionItem270')
            )
        ).click()
        self._driver.find_element_by_id('TAB_queryDateItem_270_1').clear()
        self._driver.find_element_by_id(
            'TAB_queryDateItem_270_1').send_keys(self._startDate)
        self._driver.find_element_by_id('TAB_queryDateItem_270_2').clear()
        self._driver.find_element_by_id(
            'TAB_queryDateItem_270_2').send_keys(self._endDate)
        self._driver.find_element_by_id('TAB_queryTblEnumItem_256').clear()
        self._driver.find_element_by_id(
            'TAB_queryTblEnumItem_256').send_keys(self._county)
        self._driver.find_element_by_id('TAB_QueryConditionItem256').click()
        self._driver.execute_script(
            ("document.getElementById('TAB_queryTblEnumItem_256_v')"
             ".setAttribute('type', 'text');")
        )
        self._driver.find_element_by_id('TAB_queryTblEnumItem_256_v').clear()
        self._driver.find_element_by_id(
            'TAB_queryTblEnumItem_256_v').send_keys(self._countyKey)
        self._driver.find_element_by_id('TAB_QueryButtonControl').click()

    def search(self):
        self._goToLocation()
        self._enterSearchParams()

    def _getPages(self):
        """Get current page and total pages"""
        p = self._driver.find_elements_by_css_selector('.pager')
        # If list is not empty
        if p:
            text = p[1].text
            reg = re.findall(r'\d+', text)
            totalPages = int(reg[0])
            pageSelector = self._driver.find_elements_by_css_selector(
                ".pager>input")
            currentPage = int(pageSelector[0].get_attribute('value'))
        # If list is empty there is only one page
        else:
            totalPages = 1
            currentPage = 1
            pageSelector = None
        return currentPage, totalPages, pageSelector

    def _getLinksFromTable(self):
        html = self._driver.find_element_by_id(
            'TAB_contentTable').get_attribute('innerHTML')
        soup = BeautifulSoup(html, 'html.parser')
        href = soup.select('.queryCellBordy a')
        for line in href:
            url = 'http://www.landchina.com/' + line['href']
            self._URLList.append(url)
            print('Added URL: ', url, '. Total urls')

    def scrapeLinks(self):
        """Recursive function to cycle through pages"""
        currentPage, totalPages, pageSelector = self._getPages()
        print('On page ', currentPage, ' of ', totalPages, 'total pages')
        self._getLinksFromTable()
        if currentPage < totalPages:
            pageSelector[0].clear()
            pageSelector[0].send_keys(currentPage + 1)
            pageSelector[1].click()
            self.scrapeLinks()
        else:
            print('Done!')

    def getCSV(self, outputLocation):
        """Export CSV file with links from URLList"""
        with open(outputLocation, 'w', newline='') as csvResults:
            writer = csv.writer(csvResults, dialect='excel')
            for u in self._URLList:
                writer.writerow([u])
        print('Exported CSV to ', outputLocation)

    def quitDriver(self):
        """Quit selenium driver"""
        self._driver.quit()

if __name__ == "__main__":
    driver = driver(chromedriverPath=driverLocation,
                    startDate=startDate,
                    endDate=endDate,
                    county=county,
                    countyKey=countyKey,
                    headless=True)
    driver.search()
    driver.scrapeLinks()
    driver.getCSV(outputLocation)
    driver.quitDriver()