from flask import Flask
from flask.json import jsonify
from flask import request
import pandas as pd
import os
import socket
import pickle

app = Flask(__name__)

count_vect = pickle.load(open('pkls/count_vectorizer.pkl', 'rb'))
tf_transformer = pickle.load(open('pkls/tf_transformer.pkl', 'rb'))
model = pickle.load(open('pkls/model.pkl', 'rb'))

@app.route("/", methods=["POST"])
def main():
    if request.json:
        text = request.json
        if isinstance(text, dict):
            if 'text_review' in text.keys():
                text = text['text_review'].lower().replace('[\.,&;\(\)]', '')
                counts = count_vect.transform(pd.Series(text))
                transformed = tf_transformer.transform(counts)
                sentiment = 'Positive' if model.predict(transformed)[0] else 'Negative'
                return jsonify({'sentiment': sentiment}), 200
    return '', 400

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=805)


#!/usr/bin/python3
# -*- coding: utf-8 -*-

from wtforms.form import Form
from wtforms.fields import SelectField, SelectMultipleField, StringField, SubmitField
from wtforms import widgets
from bottle import get, post, redirect, run, template, request, static_file
from makeReport import ZabbixReportAPI
from tempfile import TemporaryDirectory
from tqdm import tqdm
from datetime import datetime
import zipfile, re, sys

class MultiCheckBoxField(SelectMultipleField):
    widget = widgets.ListWidget(prefix_label=False)
    option_widget = widgets.CheckboxInput()

class ItemHistoryForm(Form):
    host_name = SelectField('Host Name')
    item = SelectField('Item', coerce = str)
    time_from = StringField('Time from')
    time_till = StringField('Time till')
    submit = SubmitField('Download CSV')

class ReportForm(Form):
    screen = MultiCheckBoxField('Screens')
    time_from = StringField('Time from')
    time_till = StringField('Time till')
    submit = SubmitField('Download ZIP')

if sys.argv[1:]:
    zabbix_ip = sys.argv[1]
else:
    zabbix_ip = '172.31.254.6'

zabbix = ZabbixReportAPI(server='http://{0}/zabbix'.format(zabbix_ip))
report_template = './templateSI5.docx'

@get('/')
def redirect_to_top():
    redirect('/top')

@get('/top')
def top():
    title = 'トップ画面'
    return template('top', title=title, request=request)

@get('/history')
def history():
    title = 'アイテムのヒストリから値一覧を取得'
    zabbix.update_hosts_dictionary()
    host_names = list(zabbix.hosts_dictionary.keys())
    host_names.sort()
    item_history_form = ItemHistoryForm()
    return template(
        'history',
        title = title,
        request = request,
        item_history_form = item_history_form,
        host_names = host_names,
        selected_host = None
    )

@post('/history')
def save_history():
    title='save history'
    item_history_form = ItemHistoryForm(request.forms.decode())
    if item_history_form.host_name.data != 'None':
        selected_host = item_history_form.host_name.data
        host_id = zabbix.hosts_dictionary[selected_host]
        host_names = list(zabbix.hosts_dictionary.keys())
        host_names.sort()
        items_dictionary = zabbix.items_dictionary_of_host(host_id)
        items = list(items_dictionary.keys())
        items.sort()
        item_history_form.item.choices = list((items_dictionary[item], item) for item in items)
        now = datetime.now()
        last_month = datetime(now.year, now.month -1, 1).isoformat().replace('T', ' ')
        this_month = datetime(now.year, now.month, 1).isoformat().replace('T', ' ')
        return template(
            'history',
            title = title,
            request = request,
            selected_host = selected_host,
            host_names = host_names,
            item_history_form = item_history_form,
            last_month = last_month,
            this_month = this_month
        )
    elif item_history_form.item.data != 'None':
        print('selected item_id:' + item_history_form.item.data)
        tmp_dir = TemporaryDirectory(prefix = 'history')
        saved_path = zabbix.save_history_as_csv(
            itemid = item_history_form.item.data,
            time_from = item_history_form.time_from.data,
            time_till = item_history_form.time_till.data,
            save_as = tmp_dir.name + '/history.csv'
        )
    return static_file(saved_path, root='/', download = 'history.csv')

@get('/report')
def report():
    title = 'スクリーンを.docxファイルで保存'
    zabbix.update_screens_dictionary()
    screens = list(zabbix.screens_dictionary.keys())
    report_form = ReportForm()
    screens =  list(zabbix.screens_dictionary.keys())
    screens.sort()
    report_form.screen.choices = list((screen, screen) for screen in screens)
    now = datetime.now()
    last_month = datetime(now.year, now.month -1, 1).isoformat().replace('T', ' ')
    this_month = datetime(now.year, now.month, 1).isoformat().replace('T', ' ')
    return template(
        'report.tpl',
        title = title,
        request = request,
        report_form = report_form,
        last_month = last_month,
        this_month = this_month
    )

@post('/report')
def save_report():
    forms = request.forms.decode()
    if forms.screen:
        tmp_dir = TemporaryDirectory(prefix = 'report')
        zip_file = zipfile.ZipFile(tmp_dir.name + '/reports.zip', 'w', zipfile.ZIP_DEFLATED)
        regexp_sinetdc = re.compile(r'[0-9]{2}(.).+DC.*')
        for screen in tqdm(forms.getall('screen')):
            if '共同調達' in screen:
                dc_name = regexp_sinetdc.search(screen).group().replace('DC', '')
                file_name = dc_name + '.docx'
            else:
                file_name = screen.replace(' ', '').replace('　', '') + '.docx'

            screenid = zabbix.screens_dictionary[screen]
            time_from = forms.time_from
            time_till = forms.time_till
            report_file_path = zabbix.save_report_from_screen(screenid = screenid,
                                                              time_from = time_from,
                                                              time_till = time_till,
                                                              template = report_template,
                                                              save_as = tmp_dir.name + '/' + file_name)
            zip_file.write(report_file_path, arcname=file_name)
        zip_file.close()
        return static_file(zip_file.filename, root ='/', download = 'reports.zip')

    else:
        print('no screens selected')
        for screen in forms.getall('screen'):
            print(screen)
        return template('top', request = request, title = 'スクリーンが選択されていませんでした')

if __name__=='__main__':
    run(host='0.0.0.0', port=8080, debug=True, reloader=True)


import json
import random
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import time
import os
import pandas as pd
from collections import defaultdict

def plot_charge(file):
    img_dir = "../../data/img"
    fmt = '%Y-%m-%d %H:%M:%S'
    reference_time = "2020-01-02 00:00:00"
    color = ['black', 'blue', 'green', 'yellow', 'red']
    state = ['battery_charged_off', 'battery_charged_on', 'battery_low', 'battery_okay', 'phone_off', 'phone_on', 'screen_off', 'screen_on', 'screen_unlock']
    with open(file, "r", encoding="utf-8") as f:
        data = json.load(f)
    # key_list = list(data.keys())
    a = list(data.keys())
    random.shuffle(a)
    fig_num = 3
    fig_size = 50
    color = color * (fig_size // len(color) + 1)
    a = a[:fig_num * fig_size]
    for i in range(fig_num):
        plt.figure()
        for j in range(fig_size):
            start, end = None, None
            plot_j = False
            message = data[str(a[i * fig_num + j])]['messages'].split("\n")
            # for s in state:
                # message = message.replace(s, "\t" + s + "\n")
            # message = message.replace('\x00', '').strip().split("\n")
            for mes in message:
                try:
                    t, s = mes.strip().split("\t")
                    t = t.strip()
                    s = s.strip()
                    if s == 'battery_charged_on' and not start:
                        start = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                    elif s == 'battery_charged_off' and start:
                        end = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                        plt.plot([start, end], [j + 1, j + 1], color[j])
                        start, end = None, None
                        plot_j = True
                except:
                    pass
            if not plot_j:
                plt.plot([0], [j + 1], color[j])
        plt.xlabel("relative time")
        plt.ylabel("client id")
        plt.title("charged time")
        plt.savefig(os.path.join(img_dir, "relativeTime_clientID_{}.png".format(i + 1)), format="png")
    # plt.show()

def gen_json():
    data = pd.read_csv("../../data/user_behavior_tiny.csv", encoding="utf-8")
    data = data['extra']
    d = dict()
    for i in range(len(data)):
        d[i] = json.loads(data[i])
    with open("../../data/user_behavior_tiny.json", "w", encoding="utf-8") as f:
        json.dump(d, f, indent=4)

def static_ready(google=True):
    fmt = '%Y-%m-%d %H:%M:%S'
    reference_time = "2018-03-06 00:00:00"
    state = ['battery_charged_off', 'battery_charged_on', 'battery_low', 'battery_okay',
             'phone_off', 'phone_on', 'screen_off', 'screen_on', 'screen_unlock']
    with open("../../data/user_behavior_tiny.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    ready = defaultdict(list)
    for i in data:
        start_charge, end_charge, okay, low = None, None, None, None
        d = data[i]
        message = d['messages']
        uid = d['user_id']
        all = []
        for s in state:
            message = message.replace(s, "\t" + s + "\n")
        message = message.replace('\x00', '').strip().split("\n")
        for mes in message:
            t, s = mes.strip().split("\t")
            t = t.strip()
            s = s.strip()
            try:
                if s == 'battery_charged_on' and not start_charge:
                    start_charge = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                elif s == 'battery_charged_off' and start_charge:
                    end_charge = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                    all.append([start_charge, end_charge])
                    start_charge, end_charge = None, None

                if not google:
                    if s == 'battery_okay' and not okay:
                        okay = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                    elif s == 'battery_low' and okay:
                        low = time.mktime(datetime.strptime(t, fmt).timetuple()) - time.mktime(datetime.strptime(reference_time, fmt).timetuple())
                        all.append([okay, low])
                        okay, low = None, None
            except:
                pass
        try:
            all = sorted(all, key=lambda x: x[0])
            now = all[0]
            for a in all:
                if now[1] >= a[0]:
                    now = [now[0], max(a[1], now[1])]
                else:
                    ready[uid].append(now)
                    now = a
            ready[uid].append(now)
        except:
            if len(ready[uid]) == 0:
                ready[uid] = []

    with open("../../data/ready_{}.json".format("strict" if google else "loose"), "w", encoding="utf-8") as f:
        json.dump(ready, f, indent=4)

def uid2behavior_tiny():
    with open('../../data/user_behavior_tiny.json', 'r', encoding='utf-8') as f:
        d = json.load(f)
    data = dict()
    for value in d.values():
        data[value['user_id']] = value
    with open('../../data/uid2behavior_tiny.json', 'w', encoding='utf-8') as f:
        json.dump(data, f)

def plot_ready(extend=False, google=True):
    img_dir = "../../data/img"
    with open("../../data/ready{}_{}.json".format("_extend" if extend else "", "strict" if google else "loose"), "r", encoding="utf-8") as f:
        ready = json.load(f)
    client_cnt = []
    tmin, tmax = 0, 0
    static_section = 3 * 60
    min_section = 2 * 60
    for uid, ready_list in ready.items():
        for start, end in ready_list:
            if start > 0:
                tmin = min(tmin, start / static_section)
                tmax = max(tmax, end / static_section)
                client_cnt.extend(list(range(int(start / static_section), int((end + (static_section - min_section)) / static_section))))
            elif end > 0:
                tmax = max(tmax, end / static_section)
                client_cnt.extend(list(range(0, int((end + (static_section - min_section)) / static_section))))
    plt.hist(client_cnt, bins=int(tmax - tmin), color="blue")
    if not extend:
        plt.axis([0, int(4500 * 60 / static_section), 0, 1600])
    else:
        plt.axis([0, 7000, 0, 2000])
    plt.xlabel("relative time / {} min".format(int(static_section / 60)))
    plt.ylabel("client num")
    plt.title("relativeTime_clientNumber_{}.png".format("strict" if google else "loose"))
    plt.savefig(os.path.join(img_dir, "relativeTime_clientNumber{}_{}.png".
                             format("_extend" if extend else "", "strict" if google else "loose")), format="png")
    # plt.show()

def static_ready_extend(google=True):
    # if extend trace feature is okay
    fmt = '%Y-%m-%d %H:%M:%S'
    reference_time = "2018-03-06 00:00:00"
    refer_second = time.mktime(datetime.strptime(reference_time, fmt).timetuple())
    state = ['battery_charged_off', 'battery_charged_on', 'battery_low', 'battery_okay',
             'phone_off', 'phone_on', 'screen_off', 'screen_on', 'screen_unlock']
    with open("../../data/user_behavior_tiny.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    with open("../../data/ready_{}.json".format("strict" if google else "loose"), "r", encoding="utf-8") as f:
        ready = json.load(f)
    ready_extend = defaultdict(list)

    for i in data:
        message = data[i]['messages']
        uid = data[i]['user_id']
        for s in state:
            message = message.replace(s, "\t" + s + "\n")
        message = message.replace('\x00', '').strip().split("\n")
        trace_start, trace_end = None, None
        for mes in message:
            t = mes.strip().split("\t")[0].strip()
            try:
                if not trace_start:
                    trace_start = time.mktime(datetime.strptime(t, fmt).timetuple()) - refer_second
                trace_end = time.mktime(datetime.strptime(t, fmt).timetuple()) - refer_second
            except:
                pass

        if not trace_start or not trace_end:
            continue
        T = trace_end - trace_start
        for p in range(10):
            ready_extend[uid].extend(list(map(lambda x: [x[0] + p * T, x[1] + p * T], ready[uid])))

    with open("../../data/ready_extend_{}.json".format("strict" if google else "loose"), "w", encoding="utf-8") as f:
        json.dump(ready_extend, f, indent=4)

if __name__ == '__main__':
    # static_ready_extend(True)
    # static_ready_extend(False)
    # plot_ready(True, True)
    # plot_ready(True, False)

    plot_charge('/home/ubuntu/storage/ycx/trace_sample/zipped_guid2data.json')


from __future__ import division
import random, math, sys, collections, warnings
from PIL import Image
import torch
import torch.nn.functional as F
import torchvision

if sys.version_info < (3, 3):
	Iterable = collections.Iterable
else:
	Iterable = collections.abc.Iterable

_pil_interpolation_to_str = {
	Image.NEAREST: 'PIL.Image.NEAREST',
	Image.BILINEAR: 'PIL.Image.BILINEAR',
	Image.BICUBIC: 'PIL.Image.BICUBIC',
	Image.LANCZOS: 'PIL.Image.LANCZOS',
	Image.HAMMING: 'PIL.Image.HAMMING',
	Image.BOX: 'PIL.Image.BOX',
}

_pil_interpolation_to_mode = {
	Image.NEAREST: 'nearest',
	Image.BILINEAR: 'bilinear',
	Image.BICUBIC: 'bicubic'
}

Compose = torchvision.transforms.Compose

class Normalize(object):
	def __init__(self, mean, std, inplace=False):
		self.mean = mean
		self.std = std
		self.inplace = inplace

	def __call__(self, tensor):
		return normalize(tensor, self.mean, self.std, self.inplace)

	def __repr__(self):
		return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)

def normalize(tensor, mean, std, inplace=False):
	if not torch.is_tensor(tensor):
		raise TypeError('tensor should be Tensor. Got {}'.format(type(tensor)))

	if not inplace:
		tensor = tensor.clone()

	dtype = tensor.dtype
	mean = torch.as_tensor(mean, dtype=dtype, device=tensor.device)
	std = torch.as_tensor(std, dtype=dtype, device=tensor.device)
	tensor.sub_(mean[None, :, None, None]).div_(std[None, :, None, None])
	return tensor

class RandomHorizontalFlip(object):
	def __init__(self, p=0.5):
		self.p = p

	def __call__(self, img):
		if random.random() < self.p:
			return hflip(img)
		return img

	def __repr__(self):
		return self.__class__.__name__ + '(p={})'.format(self.p)

def hflip(img):
	if not torch.is_tensor(img):
		raise TypeError('img should be Tensor. Got {}'.format(type(img)))

	return img.flip([3])

class RandomResizedCrop(object):
	def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR):
		if isinstance(size, tuple):
			self.size = size
		else:
			self.size = (size, size)
		if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
			warnings.warn("range should be of kind (min, max)")

		self.interpolation = interpolation
		self.scale = scale
		self.ratio = ratio

	@staticmethod
	def get_params(img, scale, ratio):
		area = img.size(2) * img.size(3)

		for attempt in range(10):
			target_area = random.uniform(*scale) * area
			log_ratio = (math.log(ratio[0]), math.log(ratio[1]))
			aspect_ratio = math.exp(random.uniform(*log_ratio))

			w = int(round(math.sqrt(target_area * aspect_ratio)))
			h = int(round(math.sqrt(target_area / aspect_ratio)))

			if w <= img.size(3) and h <= img.size(2):
				i = random.randint(0, img.size(2) - h)
				j = random.randint(0, img.size(3) - w)
				return i, j, h, w

		in_ratio = img.size(3) / img.size(2)
		if (in_ratio < min(ratio)):
			w = img.size(3)
			h = int(round(w / min(ratio)))
		elif (in_ratio > max(ratio)):
			h = img.size(2)
			w = int(round(h * max(ratio)))
		else:
			w = img.size(3)
			h = img.size(2)
		i = (img.size(2) - h) // 2
		j = (img.size(3) - w) // 2
		return i, j, h, w

	def __call__(self, img):
		i, j, h, w = self.get_params(img, self.scale, self.ratio)
		return resized_crop(img, i, j, h, w, self.size, self.interpolation)

	def __repr__(self):
		interpolate_str = _pil_interpolation_to_str[self.interpolation]
		format_string = self.__class__.__name__ + '(size={0}'.format(self.size)
		format_string += ', scale={0}'.format(tuple(round(s, 4) for s in self.scale))
		format_string += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio))
		format_string += ', interpolation={0})'.format(interpolate_str)
		return format_string

def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
	if not torch.is_tensor(img):
		raise TypeError('img should be Tensor. Got {}'.format(type(img)))

	img = crop(img, i, j, h, w)
	img = resize(img, size, interpolation)
	return img

def crop(img, i, j, h, w):
	if not torch.is_tensor(img):
		raise TypeError('img should be Tensor. Got {}'.format(type(img)))

	return img[:, :, i:(i+h), j:(j+w)]

class Resize(object):
	def __init__(self, size, interpolation=Image.BILINEAR):
		assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
		self.size = size
		self.interpolation = interpolation

	def __call__(self, img):
		return resize(img, self.size, self.interpolation)

	def __repr__(self):
		interpolate_str = _pil_interpolation_to_str[self.interpolation]
		return self.__class__.__name__ + '(size={0}, interpolation={1})'.format(self.size, interpolate_str)

def resize(img, size, interpolation=Image.BILINEAR):
	if not torch.is_tensor(img):
		raise TypeError('img should be Tensor. Got {}'.format(type(img)))
	if not (isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)):
		raise TypeError('Got inappropriate size arg: {}'.format(size))

	if isinstance(size, int):
		_, _, h, w = img.size()
		if (w <= h and w == size) or (h <= w and h == size):
			return img
		if w < h:
			ow = size
			oh = int(size * h / w)
			return F.interpolate(img, size=(oh, ow), mode=_pil_interpolation_to_mode[interpolation], align_corners=False)
		else:
			oh = size
			ow = int(size * w / h)
			return F.interpolate(img, size=(oh, ow), mode=_pil_interpolation_to_mode[interpolation], align_corners=False)
	else:
		return F.interpolate(img, size=size, mode=_pil_interpolation_to_mode[interpolation], align_corners=False)

class ToTensor(object):
	def __call__(self, pic):
		return to_tensor(pic)

	def __repr__(self):
		return self.__class__.__name__ + '()'

def to_tensor(pic):
	if not isinstance(pic[0], Image.Image):
		raise TypeError('pic should be PIL Image. Got {}'.format(type(pic)))

	if pic[0].mode != 'RGB':
		raise TypeError('pic.mode should be RGB. Got {}'.format(pic.mode))

	img = torch.stack([torch.ByteTensor(torch.ByteStorage.from_buffer(p.tobytes())) for p in pic])
	img = img.view(len(pic), pic[0].size[1], pic[0].size[0], 3)
	img = img.transpose(1, 2).transpose(1, 3).contiguous()

	if isinstance(img, torch.ByteTensor):
		return img.float().div(255)
	else:
		return img


import torchvision
import torch.utils.data
import numpy as np
import os
import sys
import faiss
from latent_spaces import LatentSpace
from typing import Callable, Optional

class ThreeDIdentDataset(torch.utils.data.Dataset):
    """
    Samples latents according to a marginal and conditional distribution and then finds
    the closest latent representation in a previously rendered dataset and returns
    that latent and the according rendering.

    Args:
        root: Path to root folder of the dataset.
        latent_space: Space to sample the negative samples and positive pairs of latents from.
        transform: Transformation to apply to the images.
        approximate_mode: Use a faster approximate mode for the NN matching of the latents.
        use_gpu: Use the GPU for FAISS NN matching.
        loader: How to load the images.
        latent_dimensions_to_use: Which of the latent dimensions should be returned. None for all.
    """

    def __init__(
        self,
        root: str,
        latent_space: LatentSpace,
        transform: Optional[Callable] = None,
        approximate_mode: Optional[bool] = False,
        use_gpu: Optional[bool] = False,
        loader: Optional[Callable] = torchvision.datasets.folder.pil_loader,
        latent_dimensions_to_use=None,
    ):
        super(ThreeDIdentDataset, self).__init__()

        self.root = root
        self.latents = np.load(os.path.join(root, "raw_latents.npy"))
        self.unfiltered_latents = self.latents

        if latent_dimensions_to_use is not None:
            self.latents = np.ascontiguousarray(
                self.latents[:, latent_dimensions_to_use]
            )

        self.latent_space = latent_space
        dummy_sample = latent_space.sample_marginal(size=1, device="cpu")
        assert (
            dummy_sample.shape[1] == self.latents.shape[1]
        ), f"Shapes do not match, i.e. {dummy_sample.shape} vs. {self.latents.shape}"
        if transform is None:
            transform = lambda x: x
        self.transform = transform

        max_length = int(np.ceil(np.log10(len(self.latents))))
        self.image_paths = [
            os.path.join(root, "images", f"{str(i).zfill(max_length)}.png")
            for i in range(self.latents.shape[0])
        ]
        self.loader = loader

        if approximate_mode:
            self._index = faiss.index_factory(
                self.latents.shape[1], "IVF1024_HNSW32,Flat"
            )
            self._index.efSearch = 8
            self._index.nprobe = 10
        else:
            self._index = faiss.IndexFlatL2(self.latents.shape[1])

        if use_gpu:
            # make it an IVF GPU index
            self._index_cpu = self._index
            self._index = faiss.index_cpu_to_gpu(
                faiss.StandardGpuResources(), 0, self._index_cpu
            )

        if approximate_mode:
            self._index.train(self.latents)
        self._index.add(self.latents)

    def __len__(self) -> int:
        return sys.maxsize

    def __repr__(self) -> str:
        head = "Dataset " + self.__class__.__name__
        body = ["Number of datapoints: {}".format(len(self.latents))]
        if self.root is not None:
            body.append("Root location: {}".format(self.root))
        body += self.extra_repr().splitlines()
        if hasattr(self, "transforms") and self.transforms is not None:
            body += [repr(self.transforms)]
        lines = [head] + [" " * self._repr_indent + line for line in body]
        return "\n".join(lines)

    def __getitem__(self, item):
        del item

        # at first sample z, z~
        # then map them to the closes grid point for which we have images
        z = self.latent_space.sample_marginal(size=1, device="cpu")
        z_tilde = self.latent_space.sample_conditional(z, size=1, device="cpu")

        distance_z, index_z = self._index.search(z.numpy(), 1)
        distance_z_tilde, index_z_tilde = self._index.search(z_tilde.numpy(), 2)

        index_z = index_z[0, 0]

        # don't use the same sample for z, z~
        if index_z_tilde[0, 0] != index_z:
            index_z_tilde = index_z_tilde[0, 0]
        else:
            index_z_tilde = index_z_tilde[0, 1]

        z = self.latents[index_z]
        z_tilde = self.latents[index_z_tilde]

        path_z = self.image_paths[index_z]
        path_z_tilde = self.image_paths[index_z_tilde]

        x, x_tilde = self.transform(self.loader(path_z)), self.transform(
            self.loader(path_z_tilde)
        )

        return (z.flatten(), z_tilde.flatten()), (x, x_tilde)

class SequentialThreeDIdentDataset(torch.utils.data.Dataset):
    """
    Sequentially load all samples in the 3DIdent dataset.

    Args:
        root: Path to root folder of the dataset.
        transform: Transformation to apply to the images.
        loader: How to load the images.
        latent_dimensions_to_use: Which of the latent dimensions should be returned. None for all.
    """

    def __init__(
        self,
        root: str,
        transform: Optional[Callable] = None,
        loader: Optional[Callable] = torchvision.datasets.folder.pil_loader,
        latent_dimensions_to_use=None,
    ):
        super(SequentialThreeDIdentDataset, self).__init__()

        self.root = root
        self.latents = np.load(os.path.join(root, "raw_latents.npy"))
        self.unfiltered_latents = self.latents

        if latent_dimensions_to_use is not None:
            self.latents = np.ascontiguousarray(
                self.latents[:, latent_dimensions_to_use]
            )

        if transform is None:
            transform = lambda x: x
        self.transform = transform

        max_length = int(np.ceil(np.log10(len(self.latents))))
        self.image_paths = [
            os.path.join(root, "images", f"{str(i).zfill(max_length)}.png")
            for i in range(self.latents.shape[0])
        ]
        self.loader = loader

    def __len__(self) -> int:
        return len(self.latents)

    def __repr__(self) -> str:
        head = "Dataset " + self.__class__.__name__
        body = ["Number of datapoints: {}".format(len(self.latents))]
        if self.root is not None:
            body.append("Root location: {}".format(self.root))
        body += self.extra_repr().splitlines()
        if hasattr(self, "transforms") and self.transforms is not None:
            body += [repr(self.transforms)]
        lines = [head] + [" " * self._repr_indent + line for line in body]
        return "\n".join(lines)

    def __getitem__(self, item):
        z = self.latents[item]
        path_z = self.image_paths[item]

        x = self.transform(self.loader(path_z))

        return z.flatten(), x


from django.db import models
from django.utils import timezone

class Venda(models.Model):
    data = models.DateTimeField(default=timezone.now)
    descricao = models.CharField(max_length=255)
    valor = models.DecimalField(max_digits=5, decimal_places=2)

    def adicionar(self):
        self.data = timezone.now().date()
        self.save()

    def __str__(self):
        return self.descricao


from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import *
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, ZeroPadding2D
from keras.layers import BatchNormalization
from keras import initializers
from keras import regularizers
from keras.layers.advanced_activations import LeakyReLU

import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.preprocessing import image
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.optimizers import Adam

base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(2,activation='softmax')(x) #final layer with softmax activation

# Sizes of the scaled images
height = 450
width = 600

ROOT_PATH = "../../"

train_directory_path = ROOT_PATH + 'train_new'
test_directory_path = ROOT_PATH + 'test_new'

number_of_epochs = 2
batch_size = 16

train_data = ImageDataGenerator(
     rescale=1 / 255
)#     shear_range=0.2,
#     zoom_range=0.2,
#     horizontal_flip=True)

#train_data = ImageDataGenerator()

test_datagen = ImageDataGenerator()

train_generator = train_data.flow_from_directory(
    train_directory_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical',
    seed=42)

validation_generator = test_datagen.flow_from_directory(
    test_directory_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical',
    seed=42)

model=Model(inputs=base_model.input,outputs=preds)

for layer in model.layers[:-8]:
    layer.trainable=False

model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

step_size_train=train_generator.n//train_generator.batch_size
step_size_validate = validation_generator.n // validation_generator.batch_size

checkpoint = keras.callbacks.ModelCheckpoint('checkpoint.check', monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
callbacks_list = [checkpoint]
model.fit_generator(generator=train_generator,
                    steps_per_epoch=step_size_train,
                    epochs=2,
                    validation_data=validation_generator,
                    validation_steps=step_size_validate,
                    callbacks=callbacks_list)

model.summary()
model.save('new_model.h5')

print(model.metrics_names)
s = model.evaluate_generator(validation_generator, step_size_validate, verbose=1)
print(s)


from msrest.serialization import Model

class SkuDescription(Model):
    """Describes a sku for a scalable resource.

    :param name: Name of the resource sku
    :type name: str
    :param tier: Service Tier of the resource sku
    :type tier: str
    :param size: Size specifier of the resource sku
    :type size: str
    :param family: Family code of the resource sku
    :type family: str
    :param capacity: Current number of instances assigned to the resource
    :type capacity: int
    """

    _attribute_map = {
        'name': {'key': 'name', 'type': 'str'},
        'tier': {'key': 'tier', 'type': 'str'},
        'size': {'key': 'size', 'type': 'str'},
        'family': {'key': 'family', 'type': 'str'},
        'capacity': {'key': 'capacity', 'type': 'int'},
    }

    def __init__(self, name=None, tier=None, size=None, family=None, capacity=None):
        self.name = name
        self.tier = tier
        self.size = size
        self.family = family
        self.capacity = capacity


#import library
import pandas as pd
import seaborn as sns
import re
from twython import Twython
import json
import matplotlib.pyplot as plt
from geopy.geocoders import Nominatim
import numpy as np
import gmplot
import webbrowser

#Load API Keys
with open('twitter_credentials.json','r') as file:
    creds = json.load(file)

python_tweets = Twython(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET'])

#Get the Tweets from the API on different dates
dates =["2022-03-26","2022-03-25","2022-03-24","2022-03-23","2022-03-22","2022-03-21","2022-03-20","2022-03-19"]

dict_test = {'user':[], 'date':[], 'text':[], 'favorite_count': [], 'location': [], 'verified':[],
         'protected':[], 'retweet':[], 'source':[], 'coordinates':[], 'timezone':[],
         'geo':[], 'place':[], 'Followers':[], 'Account Created': []}

for i in dates:
    query = {'q' : 'SpringStatement', 'result_type' : 'mixed', 'count' : 100, 'lang' : 'en', "until":i}
    sample_return = python_tweets.search(**query)
    for status in python_tweets.search(**query)['statuses']:
        dict_test['user'].append(status['user']['screen_name'])
        dict_test['date'].append(status['created_at'])
        dict_test['text'].append(status['text'])
        dict_test['favorite_count'].append(status['favorite_count'])
        dict_test['location'].append(status['user']['location'])
        dict_test['verified'].append(status['user']['verified'])
        dict_test['protected'].append(status['user']['protected'])
        dict_test['retweet'].append(status['retweet_count'])
        dict_test['source'].append(status['source'])
        dict_test['coordinates'].append(status['coordinates'])
        dict_test['timezone'].append(status['user']['time_zone'])
        dict_test['geo'].append(status['geo'])
        dict_test['place'].append(status['place'])
        dict_test['Followers'].append(status['user']['followers_count'])
        dict_test['Account Created'].append(status['user']['created_at'])
    print(len(sample_return['statuses']))

df_test2 = pd.DataFrame(dict_test)

# df_test2 = pd.read_csv("C:/Users/richa/Desktop/22102875_assessment2/tweet.csv")
# df_test2 = df_test2.drop(['Unnamed: 0'],axis =1)

#Data Manipulation
df_test2['date']= pd.to_datetime(df_test2['date'])
df_test2['Tweet Created'] = df_test2['date'].dt.date

df_test2['Account Created']= pd.to_datetime(df_test2['Account Created'])
df_test2['Date Account Created'] = df_test2['Account Created'].dt.date

df_test2['Account Age'] = df_test2['Tweet Created'] - df_test2['Date Account Created']
df_test2['Account Age'] = df_test2['Account Age'].astype(str)
df_test2.sort_values(by='Account Age', inplace = True, ascending = True)

#When does the trend starts?
a = df_test2.groupby(['Tweet Created']).sum()
a['date'] = a.index
a.plot(x="date", y=["retweet", "favorite_count"], kind="bar")
plt.xticks(rotation=45)

a.plot(x="date", y=["retweet", "favorite_count"], kind="line")
plt.xticks(rotation=45)

#What devices are used?
source_count = df_test2['source'].value_counts()

source_plot = pd.DataFrame(source_count)
source_plot = source_plot.rename(columns={"source": "count"})
source_plot['Source'] = source_plot.index

#Cleaning the source using Regex
temp = []
for i in source_plot['Source']:
    a = re.findall(">.*\w.<", i)
    temp.append(a)
temp = pd.DataFrame(temp)

#Removing the first and last character of the source
source_clean = []
for i in temp[0]:
    source_clean.append(i[1:-1])

source_plot = list(zip(source_count, source_clean))
source_plot = pd.DataFrame(source_plot, columns = ['Count', 'Source Clean'])
sns.barplot(x="Count", y="Source Clean", data = source_plot).set(ylabel = 'Devices')

#Which source to trust
verified = df_test2['verified'].value_counts()
colors = sns.color_palette('pastel')

# create pie chart using matplotlib
plt.pie(verified, labels=['Unverified', 'Verified'], colors=colors, autopct='%.0f%%')
plt.title("Percentage of Verified Accounts")
plt.show()

verified_sum = df_test2.groupby(['verified']).sum()
verified_sum['verified'] = verified_sum.index

verified_sum.plot(x="verified", y=["favorite_count","retweet"], kind="bar")
plt.xticks(rotation=0)

sns.barplot(x = "Followers", y = "verified", data = verified_sum)
verified_sum.plot(x = "verified", y="Followers", kind="bar")

#Cleaning Account Age
df_test2['Account Age Clean'] = df_test2['Account Age'].str.extract('(\d+)')
df_test2['Account Age Clean'] = df_test2['Account Age Clean'].astype(int)

sns.lineplot(x = "Account Age Clean", y = "Followers", data = df_test2).set(xlabel = "Account Age")
sns.histplot(data=df_test2, x="Account Age Clean").set(xlabel = "Account Age")

#Location of the trend
Location = df_test2['location']
Location = Location.dropna()

#Clean the Emoji within the location column
def remove_emoji(string):
    emoji_pattern = re.compile("["
                               u"\U0001F600-\U0001F64F"  # emoticons
                               u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                               u"\U0001F680-\U0001F6FF"  # transport & map symbols
                               u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                               u"\U00002500-\U00002BEF"  # chinese char
                               u"\U00002702-\U000027B0"
                               u"\U00002702-\U000027B0"
                               u"\U000024C2-\U0001F251"
                               u"\U0001f926-\U0001f937"
                               u"\U00010000-\U0010ffff"
                               u"\u2640-\u2642"
                               u"\u2600-\u2B55"
                               u"\u200d"
                               u"\u23cf"
                               u"\u23e9"
                               u"\u231a"
                               u"\ufe0f"
                               u"\u3030"
                               "]+", flags=re.UNICODE)
    return emoji_pattern.sub(r'', string)

Location_clean = []
for i in Location:
    Location_clean.append(remove_emoji(i))

#Extract the coordinates using the geocode function
geolocator = Nominatim(user_agent="geo")
def geolocate(country):
    try:
        # Geolocate the center of the country
        loc = geolocator.geocode(country)
        # And return latitude and longitude
        return (loc.latitude, loc.longitude)
    except:
        # Return missing value
        return np.nan

coordinates = []
for i in Location_clean:
    coordinates.append(geolocate(i))

#Clean Coordinates that has NA values and plot into Heatmap using gmplot
coordinates = [x for x in coordinates if str(x) != 'nan']
coordinates = pd.DataFrame(coordinates, columns=["Latitude", 'Logitude'])
coordinates = coordinates.dropna()
Lat = coordinates['Latitude']
Long = coordinates['Logitude']
map_plot = gmplot.GoogleMapPlotter(53.81604806664296, -3.0548307614209813, 3)
map_plot.heatmap(Lat, Long)
map_plot.draw("C:/Users/richa/Desktop/Web Social Media Code/heatmap.html")
webbrowser.open_new_tab("heatmap.html")


# diagram.py
from diagrams import Cluster, Diagram, Edge
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB
from diagrams.onprem.ci import Jenkins, Circleci
from diagrams.onprem.container import Docker
from diagrams.onprem.vcs import Git, Github, Gitlab
from diagrams.azure import devops
from diagrams.generic.storage import Storage
from diagrams.alibabacloud.application import RdCloud

with Diagram("Upskilling Malmö", show=True, direction="TB"):
    # Timeline
    now = Storage("Now")

    ## -------------------------------------------------------
    ## PAST
    ## -------------------------------------------------------
    # Castle: Git
    with Cluster("Git"):
        git = Git("")
    git >> now

    ## -------------------------------------------------------
    ## FUTURE
    ## -------------------------------------------------------
    # Castle: Docker
    with Cluster("Docker"):
        docker = Docker("")
    now >> docker

    # Castle: Build Tools
    with Cluster("Build Tools Familiarity"):
        buildtools = ELB("")
        jenkins = Jenkins("Jenkins")
        buildtools << Edge(color="brown", style="dashed") << [
            jenkins,
            Gitlab("Gitlab runners"),
            Github("Github actions"),
            Circleci("Circle CI"),
            devops.Pipelines("Azure pipelines"),
            ]
    now >> buildtools

    # Castle: JCasC
    with Cluster("Jenkins (w/ JCasc)"):
        jenkinsjcasc = Jenkins("Jenkins (w/ JCasc)")
        trainingmaterial = EC2("Training material")
        jenkinsjcasc << Edge(color="brown", style="dashed") << [
            trainingmaterial,
            ]
    docker >> jenkinsjcasc
    jenkins >> jenkinsjcasc
    jenkinsjcasc

    # Castle: Basic knowledge on modern ops tooling
    with Cluster("Modern ops tooling"):
        opstooling = RdCloud("")
    now >> opstooling

    # Castle: Workshop facilitation
    with Cluster("Workshop facilitation"):
        workshop = ELB("")
    now >> workshop

    # Castle: Value Stream Mapping
    with Cluster("Value Stream Mapping"):
        valuestreammapping = ELB("")
    workshop >> valuestreammapping

    # Castle: Coach TDD Mob programming
    with Cluster("Coach TDD Mob programming"):
        tddmobprogramming = ELB("")
        tdd = EC2("Coach TDD")
        mobprogramming = EC2("Mob programming")
        tddmobprogramming << Edge(color="brown", style="dashed") << [
            tdd,
            mobprogramming,
            ]
    now >> tddmobprogramming


from django.urls import path
from .views import (TourListView, TourDetailView,
                    CategoryDetailView, TypesDetailView,
                    CreateTourViews, EntryTourViews,
                    CreateCommentView, DeleteCommentView, LikeCreateView, LikeDeleteView)

urlpatterns = [
    path('create/', CreateTourViews.as_view(), name='tour-create'),
    path('all/', TourListView.as_view(), name='tour-list'),
    path('detail/<int:pk>/', TourDetailView.as_view(), name='tour-detail'),

    path('category/<int:pk>/', CategoryDetailView.as_view(), name='tour-category'),
    path('types/<int:pk>/', TypesDetailView.as_view(), name='tour-types'),
    path('entry/', EntryTourViews.as_view(), name='tour-entry'),

    path('comment/', CreateCommentView.as_view(), name='tour-comments'),
    path('delete_comment/<int:pk>/', DeleteCommentView.as_view(), name='comments-delete'),
    path('like/', LikeCreateView.as_view(), name='like-create'),
    path('like_delete/<int:pk>/', LikeDeleteView.as_view(), name='like-delete'),

]


import re
import requests
import os

from time import sleep
from bs4 import BeautifulSoup
from urllib.parse import urljoin

HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/52.0.2743.116 Safari/537.36'
    }

def get_novel_title(book_url):
    response = requests.get(book_url, headers=HEADERS)
    response.encoding = 'utf8'
    if response.status_code == 200:
        pattern = re.compile("
.*?'(.*?)' >(.*?).*?
", re.S)
        results = re.findall(pattern, response.text)
        for result in results:
            yield{
                'title': result[1],
                'link': urljoin(book_url, result[0])
            }

def get_novel_text(url):
    response = requests.get(url, headers=HEADERS)
    response.encoding = 'utf8'
    soup = BeautifulSoup(response.text, 'lxml')
    html_text = soup.prettify()
    pattern = re.compile('
(.*?)
|"content">(.*?)
', re.S)
    results = pattern.findall(html_text)
    novel = results[0][1].strip() + '\n'
    for result in results[1:-2]:
        novel += result[0].strip() + '\n'
    return novel

def write_to_local(chapter_title, content, novel_path):
    try:
        chapter_path = os.path.join(novel_path, chapter_title)
        with open(chapter_path + '.txt', 'a', encoding='utf8') as f:
            f.write(content)
    except FileNotFoundError:
        print('目录不存在')
    except FileExistsError:
        print('目录已经存在')

def main(base_url, path):
    for item in get_novel_title(base_url):
        print(f'正在下载----{item["title"]}')
        novel = get_novel_text(item['link'])
        write_to_local(item['title'], novel, path)
        sleep(0.2)

if __name__ == '__main__':
    try:
        title, url, path = input('请依次输入要爬虫的小说的名称、网址和保存地址：').split()
        novel_path = os.path.join(path, title)
        os.mkdir(novel_path)
        main(url, novel_path)
    except FileNotFoundError:
        print('目录不存在')
    except FileExistsError:
        print('目录已经存在了')


import argparse
import os

import h5py
from tqdm import tqdm

from config import config
from feature_extractor import FeatureExtractor2D
import nets

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--video_dpath', type=str,
                        default='/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/YouTubeClips',
                        help="The directory path of videos.")
    parser.add_argument('-m', '--model', type=str,
                        default='resnet152',
                        help="The name of model from which you extract features.")
    parser.add_argument('-b', '--batch_size', type=int, default=128, help="The batch size.") # 32gv100
    parser.add_argument('-s', '--stride', type=int, default=1,
                        help="Extract feature from every <s> frames.")
    parser.add_argument('-o', '--out', type=str,
                        default='feature_resnet152_s1/MSVD_ResNet152_{}.hdf5',
                        help="The file path of extracted feature.")
    return parser.parse_args()

def process_vidio(args, video_lists, subset):
    videoss = os.listdir(args.video_dpath)
    videos = []
    for video in videoss:
        if os.path.basename(video).split('.')[0] in video_lists:
            videos.append(video)
    save_path = args.out.format(subset)
    h5 = h5py.File(save_path, 'w') if not os.path.exists(subset) else h5py.File(subset, 'r+')
    for video in tqdm(videos):
        video_id = os.path.splitext(video)[0]
        if video_id in h5.keys():
            continue

        video_fpath = os.path.join(args.video_dpath, video)
        feats = extractor(video_fpath)
        if feats is not None:
            h5[video_id] = feats
    h5.close()

if __name__ == '__main__':
    args = parse_args()

    config = config[args.model]

    model = getattr(nets, args.model)(pretrained=True)
    model.cuda()
    model.eval()

    extractor = FeatureExtractor2D(
        stride=args.stride,
        mean=config.mean,
        std=config.std,
        resize_to=config.resize_to,
        crop_to=config.crop_to,
        model=model,
        batch_size=args.batch_size)

    if not os.path.exists(os.path.dirname(args.out)):
        os.makedirs(os.path.dirname(args.out))

    subsets = ["train", "val", "test"]

    videoindex = open("/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/txt_labels/youtube_mapping.txt", 'r').readlines()
    name2idx = dict()
    idx2name = dict()
    for v in videoindex:
        name2idx[v.split()[0]] = v.split()[1]
        idx2name[v.split()[1]] = v.split()[0]

    for subset in subsets:
        txtfile = "/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/txt_labels/sents_{}_lc_nopunc.txt".format(subset)
        capinfos = open(txtfile, 'r').readlines()
        visited_imames = set()
        for caption in capinfos:
            vidindex = caption.split('\t')[0]
            if vidindex not in visited_imames:
                visited_imames.add(idx2name[vidindex])
        visited_imames = list(visited_imames)
        print("set {} has {} video".format(subset, len(visited_imames)))

        process_vidio(args, visited_imames, subset)


from collections import deque
import sys

if __name__=='__main__' :
    x=list(map(int,sys.stdin.readline().split()))
    y=list(map(int,sys.stdin.readline().split()))

    result = deque([i for i in range(1,x[0]+1)])  # deque사용

    count=0

    for i in y:
        while(i!=result[0]): #2번, 3번 수행 조건
            count+=1

            c=list(result) #slice위해 list로 변환

            #최솟값 찾기 위해 길이 구함
            a=len(c[c.index(i)::])
            b=len(c[:c.index(i)])

            if a<=b: #a가 더 작으면 오른쪽으로 이동 ak, a1, ..., ak-1
                temp=result.pop()
                result.appendleft(temp)
            else: #b가 더 작으면 왼쪽으로 이동 a2, ..., ak, a1
                temp=result.popleft()
                result.append(temp)
        result.popleft() #1번 수행

    print(count)


import pyautogui
import sys, os, time
from PIL import Image

class crafter:
    def __init__(self):
        print("INITIALIZED")
        self.log_im_region = [0,0,884,625]
        #PUT STUFF HERE LATER

    def set_screen(self, icon_loc):
        #find the icon location
        locs = pyautogui.locateOnScreen(icon_loc)
        if locs is not None:
            x = locs[0]
            y = locs[1]
            #move mouse in positions
            pyautogui.moveTo(x+30,y+10)
            #drag screen to corner
            pyautogui.mouseDown()
            pyautogui.moveTo(31,11,duration=.5)
            pyautogui.mouseUp()
        else:
            print("NOT FOUND")

    def slice_Log(self):
        #get crafting image screenshot
        log_loc = pyautogui.locateOnScreen(".\\ff14_crafting_icon.png")
        self.log_im_region[0]=log_loc[0]
        self.log_im_region[1]=log_loc[1]
        self.log_im = pyautogui.screenshot(region=self.log_im_region)
        #get width and height
        w, h = self.log_im.size
        '''for a in range(0,w,20):
            for b in range(h):
                self.log_im.putpixel((a,b),(255,0,0,))'''
        self.log_im.save(".\\sliced_Log.png")
        #SLICE UP THE CRAFTING LOG INTO USEFUL IMAGES

    def craft_loop(self):
        icon_loc = pyautogui.locateOnScreen("dbmg.png")
        pyautogui.moveTo(icon_loc[0]+15,icon_loc[1]+15)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(.5)
        pyautogui.mouseUp()
        icon_loc = pyautogui.locateOnScreen("pp.png")
        pyautogui.moveTo(icon_loc[0]+40,icon_loc[1]+20,duration=1)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(1)
        pyautogui.moveTo(1,1,duration=.25)
        pyautogui.mouseUp()
        table_loc = pyautogui.locateOnScreen("Capture.png")
        pyautogui.moveTo(table_loc[0]+20,table_loc[1]+20)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(.5)
        pyautogui.mouseUp()
        synth_loc = pyautogui.locateOnScreen("synth.png")
        pyautogui.moveTo(synth_loc[0]+30,synth_loc[1]+10)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(2)
        #craft loop
        log_up = pyautogui.locateOnScreen("ff14_crafting_icon.png")
        while log_up is None:
            pyautogui.press("1")
            time.sleep(3)
            log_up = pyautogui.locateOnScreen("ff14_crafting_icon.png")
        time.sleep(10)

def main():
    c = crafter()
    c.set_screen("FF14_top_left_icon.png")
    c.slice_Log()
    while True:
        c.craft_loop()

main()


import os
import openai
from flask import Flask, render_template, request
app = Flask(__name__)
openai.api_key = 'YOUR-API-KEY'

prompt = 'The following is a conversation with Leonardo. Leonardo is a bot acting like the painter and inventor Leonardo Da Vinci.\nYou: Can you paint?\nLeonardo: Yes I can actually paint very well, I painted the famous Mona Lisa\nYou: When were you born?\nLeonardo: I was born on 15 April 1452\nYou: When did you die?\nLeonardo: I died on the 2nd of May 1519, I was 67 years old\n'

def get_response(q):
    global prompt
    prompt=prompt+'You: '+q+'\n'
    response = openai.Completion.create(
    engine="davinci",
    prompt=prompt,
    temperature=0.9,
    max_tokens=150,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
    stop=["\n", " You:", " Leonardo:"],
    )
    return response['choices'][0]['text']
@app.route("/")
def index():
    return render_template("index.html")

@app.route("/get")
#function for the bot response
def get_bot_response():
    global prompt
    userText = request.args.get('msg')
    response = get_response(userText)
    prompt = prompt + response
    return str(response)

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


from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Random import get_random_bytes
from Crypto.Signature import PKCS1_PSS
from Crypto.Hash import SHA256

def encrypt_data(data, recipient_key):
    session_key = get_random_bytes(16)

    # Encrypt the session key with the public RSA key
    cipher_rsa = PKCS1_OAEP.new(recipient_key)
    enc_session_key = cipher_rsa.encrypt(session_key)

    # Encrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX)
    ciphertext, tag = cipher_aes.encrypt_and_digest(data)

    return enc_session_key + cipher_aes.nonce + tag + ciphertext

def decrypt_data(enc_data, private_key):
    pl = private_key.size_in_bytes()

    # extract encrypted session key nonce tag and ciphertext from enc_data
    enc_session_key = enc_data[0:pl]
    nonce = enc_data[pl:pl + 16]
    tag = enc_data[pl + 16:pl + 32]
    ciphertext = enc_data[pl + 32:]

    # Decrypt the session key with the private RSA key
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)

    # Decrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    ResultData = cipher_aes.decrypt_and_verify(ciphertext, tag)
    return ResultData

def sign(data, private_key):
    hasher = SHA256.new(data)
    signer = PKCS1_PSS.new(private_key)
    signature = signer.sign(hasher)
    return signature

def checksign(data, public_key, signature):
    hasher = SHA256.new(data)
    verifier = PKCS1_PSS.new(public_key)
    return verifier.verify(hasher, signature)


from lxml import html
import requests
import numpy as np

def parse_html(row):
    page = requests.get('http://www.mirbase.org/cgi-bin/mirna_entry.pl?acc={}'.format(row['From']))
    tree = html.fromstring(page.content)

    text1 = tree.xpath('//a[@href="/cgi-bin/get_read.pl?acc={}"]/text()'.format(row['Alias']))

    try:
        return int(text1[0])
    except IndexError:
        return np.nan


import time
from logging import exception

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from MakeMyTrip import Utilites
from MakeMyTrip.AllElementsPath import Test_AllElementsPath
from MakeMyTrip.HomePageObjects import Test_HomePage

class Test_Activites(Test_AllElementsPath):
    global Logs
    global MoreActivitesText
    Logs = Utilites.getlogs()
    MoreActivitesText = []

    def __init__(self, driver):
        self.driver = driver

    def Activites(self):
        try:
            global TestHomePageObj

            TestHomePageObj = Test_HomePage(self.driver)
            MMTLogoLink = TestHomePageObj.MMTLogoClick()
            Utilites.webelementclick(self.driver, MMTLogoLink)
            time.sleep(3)
            Logs.info("::::::::::::::Verifying Activites tab::::::::::::::::::::::::::")
            Activitylink = TestHomePageObj.ActivitiesTab()
            Utilites.webelementclick(self.driver, Activitylink)
            ActivityMoreLink = TestHomePageObj.ActivitiesMore()
            try:
                assert ActivityMoreLink
                Logs.info("Activity More link exist")
                a = ActionChains(self.driver)
                a.move_to_element(ActivityMoreLink).perform()
                MoreActivitesWebElementList = TestHomePageObj.ActivitesList()
                for ActivitesText in MoreActivitesWebElementList:
                    MoreActivitesText.append(ActivitesText.text)
                LISTOFALLACTIVITES = self.ListOfActivites()
                j = 0
                for MAT in MoreActivitesText:
                    try:
                        assert MAT ==  LISTOFALLACTIVITES[j]
                        Logs.info(str(MAT) + " Exist in drop down")
                    except Exception as e:
                        Logs.error(str(MAT) + " Not exist in drop down")
                        raise Exception(e)
                    j = j+1

            except AssertionError:
                Logs.error("Activity link doesn't exist")

        except Exception as e:
            raise Exception(e)

    def ListOfActivites(self):
        listofact = ["Charter Flights", "Activities", "Trip Ideas", "Giftcards", "Nearby Getaways", "Trip Money"]
        return listofact


import cv2
import numpy as np

img_base = cv2.imread('assets/soccer_practice.jpg', 0)
template = cv2.imread('assets/shoe.png', 0)
img_base = cv2.resize(img_base, (0, 0), fx=.8, fy=.8)
template = cv2.resize(template, (0, 0), fx=.8, fy=.8)
h, w = template.shape

methods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR,
           cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF,
           cv2.TM_SQDIFF_NORMED]  # these r all the different methods/algos u can apply to your image

for method in methods:
    img = img_base.copy()

    result = cv2.matchTemplate(img, template, method)  # (W-w+1, H-h+1)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(
        result)  # returns the min and max value in the array and the location of them

    if method in [cv2.TM_SQDIFF_NORMED, cv2.TM_SQDIFF]:
        location = min_loc
    else:
        location = max_loc
    print(location)

    bottom_right = (location[0] + w, location[1] + h)
    cv2.rectangle(img, location, bottom_right, 255, 5)
    cv2.imshow('Match', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()


import os
import sys
from itertools import chain
from logging import Logger, getLogger
from pathlib import Path
from typing import Dict, Iterable, Union

from .config import ConfigParser
from .processor import ProcessManager

class BaseSparkCommand:

    def __init__(self,
                 config: ConfigParser = None,
                 spark_executable: str = None,
                 master: str = None,
                 deploy_mode: str = None,
                 queue: str = None,
                 conf: Iterable[str] = None,
                 packages: Union[Iterable[str], str] = None,
                 exclude_packages: Union[Iterable[str], str] = None,
                 repositories: Union[Iterable[str], str] = None,
                 reqs_paths: Union[Iterable[str], str] = None,
                 env: Dict[str, str] = None,
                 properties_file: str = None,
                 klass: str = None,
                 logger: Logger = None):
        try:
            cmd_config = config['spark']
        except KeyError:
            config.add_section('spark')
            cmd_config = config['spark']
        except TypeError:
            config = ConfigParser(default_sections=('spark', 'spark_env'))
            cmd_config = config['spark']

        try:
            env_config = config['spark-env']
        except KeyError:
            config.add_section('spark-env')
            env_config = config['spark-env']

        self.spark_executable = spark_executable or cmd_config.get('spark-executable', fallback='spark-submit')
        self.master = master or cmd_config.get('master')
        self.deploy_mode = deploy_mode or cmd_config.get('deploy-mode')
        self.queue = queue or cmd_config.get('queue')
        self.conf = cmd_config.getlist('conf', fallback=[])
        self.packages = cmd_config.getlist('packages', fallback=[])
        self.exclude_packages = cmd_config.getlist('exclude-packages', fallback=[])
        self.repositories = cmd_config.getlist('repositories', fallback=[])
        self.reqs_paths = cmd_config.getlist('reqs_paths', fallback=[])

        self.property_file = properties_file or cmd_config.get('property-file')
        self.klass = klass or cmd_config.get('class')

        try:
            self.env = {k: v for k, v in env_config.items()}
        except KeyError:
            self.env = {}

        self.env.update(env or {})

        self.logger = logger or getLogger(__name__)

        if conf:
            self.conf.extend(conf)

        if packages:
            if isinstance(packages, str):
                packages = packages.split(',')

            packages = [p.strip() for p in packages if p.strip()]
            if packages:
                self.packages.extend(packages)

        if exclude_packages:
            if isinstance(exclude_packages, str):
                exclude_packages = exclude_packages.split(',')

            exclude_packages = [p.strip() for p in exclude_packages if p.strip()]
            if packages:
                self.exclude_packages.extend(exclude_packages)

        if repositories:
            if isinstance(repositories, str):
                repositories = repositories.split(',')

            repositories = [p.strip() for p in repositories if p.strip()]
            if repositories:
                self.repositories.extend(repositories)

        if reqs_paths:
            if isinstance(reqs_paths, str):
                reqs_paths = reqs_paths.split(',')
            reqs_paths = [Path(p.strip()) for p in reqs_paths if p.strip()]

            if reqs_paths:
                self.reqs_paths.extend(reqs_paths)

    def build_command(self, *, executable):
        spark_cmd = [executable, ]
        if self.master:
            spark_cmd.extend(['--master', self.master])

        if self.deploy_mode:
            spark_cmd.extend(['--deploy-mode', self.deploy_mode])

        if self.queue:
            spark_cmd.extend(['--queue', self.queue])

        if self.conf:
            spark_cmd.extend(chain(*[['--conf', c] for c in self.conf]))

        if self.packages:
            spark_cmd.extend(['--packages', ','.join(self.packages)])

        if self.exclude_packages:
            spark_cmd.extend(['--exclude-packages', ','.join(self.exclude_packages)])

        if self.repositories:
            spark_cmd.extend(['--repositories', ','.join(self.repositories)])

        if self.property_file:
            spark_cmd.extend(['--properties-file', self.property_file])

        if self.klass:
            spark_cmd.extend(['--class', self.klass])

        if self.reqs_paths:
            ps = [r.strip() for r in chain(*[[str(p.resolve())
                                              for p in chain(Path(rp).rglob('*.egg'),
                                                             Path(rp).rglob('*.whl'),
                                                             Path(rp).rglob('*.zip'))
                                              if p.is_file()] for rp in self.reqs_paths]) if r.strip()]

            if len(ps):
                spark_cmd.extend(['--py-files', ','.join(ps)])

        return spark_cmd

class SparkSubmitCommand(BaseSparkCommand):

    def build_command(self, *, job_args: Iterable[str], **kwargs):
        kwargs.setdefault('executable', self.spark_executable)
        spark_cmd = super(SparkSubmitCommand, self).build_command(**kwargs)

        spark_cmd.extend(job_args)

        return spark_cmd

    def run(self, job_args: Iterable[str]):
        self.logger.info('Executing Spark job...')
        spark_command = self.build_command(job_args=job_args)

        env = os.environ.copy()

        env['PYSPARK_PYTHON'] = sys.executable
        env['PYSPARK_DRIVER_PYTHON'] = sys.executable

        env.update(self.env or {})

        self.logger.info(' '.join(spark_command))

        process = ProcessManager(spark_command, pass_through=True, env=env)
        process.start_process()
        process.wait()

        if process.returncode != 0:
            raise RuntimeError(f'Spark job failed with error: {process.returncode}')

class SparkInteractiveCommand(BaseSparkCommand):

    def __init__(self,
                 cmd_config,
                 *args,
                 pyspark_executable: str = None,
                 python_interactive_driver: str = None,
                 **kwargs):
        super(SparkInteractiveCommand, self).__init__(cmd_config, *args, **kwargs)

        try:
            cmd_config = cmd_config['interactive']
        except (KeyError, TypeError):
            cmd_config = ConfigParser(default_sections=('interactive',))
            cmd_config = cmd_config['interactive']

        self.pyspark_executable = pyspark_executable or cmd_config.get('pyspark-executable', fallback='pyspark')
        self.python_interactive_driver = python_interactive_driver or cmd_config.get('python-interactive-driver',
                                                                                fallback=sys.executable)

        # Force client deploy mode
        self.deploy_mode = 'client'

    def build_command(self, **kwargs):
        kwargs.setdefault('executable', self.pyspark_executable)
        return super(SparkInteractiveCommand, self).build_command(**kwargs)

    def run(self):

        self.logger.info('Executing Spark interactive...')

        spark_command = self.build_command()

        env = os.environ.copy()

        env['PYSPARK_PYTHON'] = sys.executable
        env['PYSPARK_DRIVER_PYTHON'] = self.python_interactive_driver

        env.update(self.env)

        self.logger.info(' '.join(spark_command))

        process = ProcessManager(spark_command, pass_through=True, env=env)
        process.start_process()
        process.wait()

        if process.returncode != 0:
            raise RuntimeError(f'Interactive Spark failed with error: {process.returncode}')


import random
import matplotlib.pyplot as plt

F_n = []
n = 1000
for s in range(n):
    cards = list(range(52))
    F_n.append([52])
    for _ in range(200):
        i = random.randint(0, 51);
        j = random.randint(0, 51);
        temp = cards[i]
        cards[i] = cards[j]
        cards[j] = temp

        f_n = F_n[-1][-1]
        if cards[i] == j:
            f_n-=1
        if cards[j] == i:
            f_n-=1
        if cards[i] == i:
            f_n+=1
        if cards[j] == j:
            f_n+=1
        F_n[-1].append(f_n)

F_n_avg = [sum([f[i] for f in F_n]) / n for i in range(len(F_n[0]))]

print(F_n_avg)

plt.plot(F_n_avg);

plt.show()


from django.conf.urls import url, include

from nutrition import views

log_urls = [
    url(r'^new/$', views.log_foods, name='log_food'),
    url(r'^day/', views.day_log, name='day_log'),
    url(r'^week/', views.week_log, name='week_log'),
    url(r'^month/', views.month_log, name='month_log')
]

goals_urls = [
    url(r'^view/', views.view_goals, name='view_goals'),
    url(r'^set/', views.set_goals, name='set_goals')
]

food_urls = [
    url(r'^search/$', views.search_foods, name='search_foods'),
    url(r'^(?P<pk>[0-9]+)/', views.food_details, name='food_detail'),
    url(r'^create/', views.create_food, name='create_food'),
]

urlpatterns = [
    url(r'^$', views.nutrition_home, name='nutrition_home'),
    url(r'^food/', include(food_urls)),
    url(r'^log/', include(log_urls)),
    url(r'^goals/', include(goals_urls))
]


from setuptools import setup, find_packages
import os

package_root = os.path.abspath(os.path.dirname(__file__))

version = {}
with open(os.path.join(package_root, "LoopProjectFile/Version.py")) as fp:
    exec(fp.read(), version)
version = version["__version__"]

setup(
    name="LoopProjectFile",
    install_requires=["netCDF4>=1.5.4", "numpy", "pandas"],
    description="Open source structural geology data storage for Loop Projects",
    author="Roy Thomson",
    author_email="roy.thomson@monash.edu",
    url="https://github.com/Loop3D/LoopProjectFile",
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "Intended Audience :: Other Audience",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: MIT License",
        "License :: Free for non-commercial use",
        "Natural Language :: English",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Topic :: Multimedia :: Graphics :: 3D Modeling",
        "Topic :: Scientific/Engineering",
        "Topic :: Scientific/Engineering :: GIS",
    ],
    version=version,
    packages=find_packages()
    )


import cv2
import numpy as np
import os
import argparse

def create_circular_mask(h, w, center=None, radius=None):

    if center is None: # use the middle of the image
        center = (int(w/2), int(h/2))
    if radius is None: # use the smallest distance between the center and image walls
        radius = min(center[0], center[1], w-center[0], h-center[1])

    Y, X = np.ogrid[:h, :w]
    dist_from_center = np.sqrt((X - center[0])**2 + (Y-center[1])**2)

    mask = dist_from_center <= radius
    return mask

def distance_matrix(rows,cols):
    crow,ccol = rows//2, cols//2

    x = np.argwhere(np.ones((rows,cols))).reshape((rows,cols,2))
    y = np.array((crow,ccol))

    #Euclidian distance formula
    x -= y
    x = np.square(x)
    z = np.sum(x[:][:], axis=2)
    z = np.sqrt(z)

    return z

def butterworth_lowpass(img, n, D):
    #Take fourier transform
    fshift = np.fft.fftshift(np.fft.fft2(img))

    rows, cols = img.shape

    distance = distance_matrix(rows,cols) + 0.0001

    butterworth_scale = 1 / (1 + (distance / D)**(2*n))

    fshift = np.multiply(fshift, butterworth_scale)

    #Convert back to image pixel format
    img_back = np.real(np.fft.ifft2(np.fft.ifftshift(fshift)))

    return img_back

#Get the location of the folder from the arguments list
parser = argparse.ArgumentParser(prog='Preprocessor')
parser.add_argument('folderpath')
[parser.add_argument('endpath')]
args = parser.parse_args()

#Ensure folder exists and raise error if not
assert os.path.exists(args.folderpath), "Error: Could not locate folder"

#Make new directory to save processed images
processed_path = args.endpath
new_processed_path = processed_path
i = 0
while os.path.exists(new_processed_path):
    new_processed_path = processed_path + str(i)
    i+=1
os.mkdir(new_processed_path)

#Operate on every .jpg in the directory
for filename in os.listdir(args.folderpath):
    if not filename.endswith(".jpg"):
        continue

    #Read custom image
    img = cv2.imread(os.path.join(args.folderpath,filename))
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    #Process the image
    img[img < 78] = 0
    #img = butterworth_lowpass(img,2,50)
    img = butterworth_lowpass(img,2,30)
    img[img > 4] = 255

    #Save the image
    cv2.imwrite(os.path.join(new_processed_path,filename), img)


from compas.geometry.elements.point import Point
from compas.geometry.elements.vector import Vector
from compas.geometry.elements.line import Line

from compas.geometry import area_polygon
from compas.geometry import cross_vectors
from compas.geometry import centroid_points
from compas.geometry import center_of_mass_polygon
from compas.geometry import is_coplanar
from compas.geometry import is_polygon_convex

__author__     = ['Tom Van Mele', ]
__copyright__  = 'Copyright 2014, Block Research Group - ETH Zurich'
__license__    = 'MIT License'
__email__      = 'vanmelet@ethz.ch'

class Polygon(object):
    """An object representing ...

    A ``Polygon`` has a closed boundary that separates its interior from the
    exterior. The boundary does not intersect itself, and is described by an
    ordered set of of points.

    Note:
        All ``Polygon`` objects are considered closed. Therefore the first and
        last element in the list of points are not the same. The existence of the
        closing edge is implied.

    Parameters:
        points (sequence): A sequence of XYZ coordinates.

    Attributes:
        points (list): A list of ``Point`` objects.
        lines (list): A list of ``Line`` objects.
        length (float): The total length of the boundary.
        centroid (Point): A ``Point`` object at the location of the centroid.
        area (float): The size of the area enclosed by the boundary.

    Examples:
        >>> polygon = Polygon([[0,0,0], [1,0,0], [1,1,0], [0,1,0]])
        >>> polygon.centroid
        [0.5, 0.5, 0.0]
        >>> polygon.area
        1.0

    """
    def __init__(self, points):
        self.lines = None
        self.points = points

    @property
    def points(self):
        """The points of the polygon.

        Parameters:
            points (sequence): A sequence of XYZ coordinates.

        Returns:
            list: A list of ``Point`` objects.
        """
        return self.points

    @points.setter
    def points(self, points):
        if points[-1] == points[0]:
            del points[-1]
        self.points = [Point(xyz) for xyz in points]
        self.lines  = [Line(self.points[i], self.points[i + 1]) for i in range(-1, len(points) - 1)]

    @property
    def lines(self):
        """The lines of the polygon."""
        return self.lines

    @property
    def length(self):
        """The length of the boundary."""
        return sum([line.length for line in self.lines])

    @property
    def centroid(self):
        """The centroid of the polygon."""
        return Point(centroid_points(self.points))

    @property
    def normal(self):
        """The (average) normal of the polygon."""
        o = self.center
        points = self.points
        a2 = 0
        normals = []
        for i in range(-1, len(points) - 1):
            p1  = points[i]
            p2  = points[i + 1]
            u   = [p1[_] - o[_] for _ in range(3)]
            v   = [p2[_] - o[_] for _ in range(3)]
            w   = cross_vectors(u, v)
            a2 += sum(w[_] ** 2 for _ in range(3)) ** 0.5
            normals.append(w)
        n = [sum(axis) / a2 for axis in zip(*normals)]
        n = Vector(n)
        return n

    @property
    def tangent(self):
        """The (average) tangent plane."""
        o = self.center
        a, b, c = self.normal
        d = - (a * o.x + b * o.y + c * o.z)
        return a, b, c, d

    @property
    def frame(self):
        """The local coordinate frame."""
        o = self.center
        w = self.normal
        p = self.points[0]
        u = Vector(p, o)
        u.normalize()
        v = w.cross(u)
        return o, u, v, w

    @property
    def center(self):
        """The center (of mass) of the polygon."""
        return Point(center_of_mass_polygon(self.points))

    @property
    def area(self):
        """The area of the polygon.

        The area is computed as the sum of the areas of the triangles formed
        by each of the lines of the boundary and the centroid.
        """
        return area_polygon(self.points)

    def is_convex(self):
        return is_polygon_convex(self.points)

    def is_coplanar(self):
        return is_coplanar(self.points)

# ==============================================================================
# Debugging
# ==============================================================================

if __name__ == '__main__':

    polygon = Polygon([[1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0]])

    print(polygon.centroid)
    print(polygon.center)
    print(polygon.area)
    print(polygon.length)
    print(polygon.normal)
    print(polygon.frame)

    print(polygon.is_convex())
    print(polygon.is_coplanar())


from django.urls import path,include
from  Customer import views

urlpatterns = [
    path('customer-settings', views.customer_settings, name="customer_settings"),
    path('subscribed-businesses',views.subscribed_businesses, name='subscribed_businesses'),
    path('customer-signup',views.customer_signup,name="customer-signup"),
    path('customer-login',views.login_view,name="customer-login"),
    path('customer-login/<return_link>',views.login_view,name="customer-login"),
    path('customer-logout',views.logout_view,name="customer-logout"),
    path('homepage', views.homepage, name="homepage"),
    path('cart/<business_key>', views.cart_view, name="cart"),
    path('confirm-order', views.confirm_order, name="confirm_order"),
    path('orders', views.order_view, name="order"),
    path('orders/<order_id>', views.customer_order_page, name="customer_order_page"),
    path('subscribe/<business_key>/<action>', views.subscribe, name="subscribe"),
    path('customer-signup/<return_link>',views.inside_signup,name="inside_signup"),
    path('customer-verify',views.inside_signup,name="inside_signup"),
    path('payment',views.payment_view,name="payment_view"),
    path('handel-payment',views.handelpayment,name="handelpayment"),
    path('order_placed/<orderid>', views.success, name="success"),
    path('payment-failure/<orderid>',views.failure, name="failure"),
    path('<business_key>', views.catalog, name="catalog"),
    path('<business_key>/<product_key>', views.product, name="product"),
    path('<business_key>/', views.catalog, name="catalog"),
    path('<business_key>/<product_key>/', views.product, name="product"),
]


# -*- coding: UTF-8 -*-
import logging
import argparse
import pandas as pd

from sklearn.preprocessing import StandardScaler
from sklearn_pandas import DataFrameMapper
import torchtuples as tt

from pycox.models import CoxTime
import os

class coxDataLoader:
    def parse_data_args(parser):
        parser.add_argument('--path', type=str, default='../data/',
                            help='Input data dir.')
        parser.add_argument('--dataset', type=str, default='kwai_1115',
                            help='Choose a dataset.')
        parser.add_argument('--prediction_dataset', type=str, default='',
                            help='Choose a dataset.')
        parser.add_argument('--sep', type=str, default='\t',
                            help='sep of csv file.')
        parser.add_argument('--play_rate', type=int, default=1,
                            help='in_features')
        parser.add_argument('--pctr', type=int, default=0,
                            help='in_features')
        parser.add_argument('--start_time', type=int, default=24,
                            help='group hour')
        return parser

    def __init__(self, args):
        dataFolder=args.path+args.dataset
        self.coxData=pd.read_csv("%s/cox.csv"%dataFolder)
        renameDict={'item_id':'photo_id'}
        for i in range(168):
            renameDict['ctr%d'%i]='click_rate%d'%i
            renameDict['exp%d'%i]='exposure%d'%i
        self.coxData.rename(renameDict,inplace=True,axis=1)

        # self.filtered_data()
        print(len(self.coxData),self.coxData.columns)
        self.labtrans=None
        self.play_rate=args.play_rate
        self.pctr=args.pctr
        self.start_time=args.start_time
        self.prediction_dataset=args.prediction_dataset
        return

    def filtered_data(self):
        caredList=['photo_id']
        for i in range(self.start_time):
            caredList.append('click_rate%d'%(i))
            if self.play_rate:
                caredList.append('play_rate%d'%(i))
            if self.pctr:
                caredList.append('new_pctr%d'%(i))
        self.coxData=self.coxData[caredList]

    def load_data(self,args):
        # self.filtered_data()
        self.diedInfo=pd.read_csv(args.label_path)
        df_train=self.coxData
        df_train.fillna(-1,inplace=True)
        caredList=['died','timelevel','photo_id']
        for i in range(self.start_time):
            caredList.append('click_rate%d'%(i))
            if self.play_rate:
                caredList.append('play_rate%d'%(i))
            if self.pctr:
                caredList.append('new_pctr%d'%(i))
        df_train=df_train[caredList[2:]]
        df_train=pd.merge(df_train,self.diedInfo[['photo_id','died','timelevel']],on='photo_id')
        logging.info('[coxDataLoader] before died filter %d items'%len(df_train))
        df_train=df_train[df_train['died']==1]
        logging.info(df_train[['died','timelevel']].describe())
        df_test = df_train.sample(frac=0.2)
        df_train = df_train.drop(df_test.index)
        df_val = df_train.sample(frac=0.2)
        df_train = df_train.drop(df_val.index)

        if self.prediction_dataset!='':
            x=args.label_path
            # prediction_label_path=x.replace(args.dataset,args.prediction_dataset)
            # diedInfo=pd.read_csv(prediction_label_path)
            coxData=pd.read_csv(args.path+args.prediction_dataset+'/cox.csv')
            coxData.fillna(-1,inplace=True)
            caredList=['died','timelevel','photo_id']
            for i in range(self.start_time):
                caredList.append('click_rate%d'%(i))
                if self.play_rate:
                    caredList.append('play_rate%d'%(i))
                if self.pctr:
                    caredList.append('new_pctr%d'%(i))
            coxData=coxData[caredList[2:]]
            df_test=coxData
            print("********",len(df_test))
            df_test['died']=1
            df_test['timelevel']=168
            # df_test=pd.merge(coxData,diedInfo[['photo_id','died','timelevel']],on='photo_id')
            # print("length!!!",len(diedInfo),len(coxData),len(df_test))

        return df_train,df_val,df_test,caredList

    def preprocess(self,args):
        df_train,df_val,df_test,cared=self.load_data(args)
        print(len(df_train),len(df_val),len(df_test))
        ignore_length=3
        cols_standardize = cared[ignore_length:]
        # cols_leave = ['photo_id']
        #scaler.mean_,np.sqrt(scaler.var_)
        standardize = [([col], StandardScaler()) for col in cols_standardize]
        # leave = [(col, None) for col in cols_leave]
        x_mapper = DataFrameMapper(standardize)#+leave

        self.x_train = x_mapper.fit_transform(df_train).astype('float32')
        if len(df_val)>0:
            x_val = x_mapper.transform(df_val).astype('float32')
        self.x_test = x_mapper.transform(df_test).astype('float32')
        self.df_test=df_test

        self.labtrans = CoxTime.label_transform()
        get_target = lambda df: (df['timelevel'].values, df['died'].values)
        self.y_train = self.labtrans.fit_transform(*get_target(df_train))
        y_val = self.labtrans.transform(*get_target(df_val))
        self.val = tt.tuplefy(x_val, y_val)
        self.durations_test, self.events_test = get_target(df_test)
        # self.y_test= self.corpus.labtrans(*get_target(df_test))

if __name__ == '__main__':
    print("[dataLoader]")
    logging.basicConfig(level=logging.INFO)
    parser = argparse.ArgumentParser()
    parser = coxDataLoader.parse_data_args(parser)
    args, extras = parser.parse_known_args()

    # args.path = '../../data/'
    corpus = coxDataLoader(args)
    # label=Label(corpus)
    # corpus.preprocess()


import pandas as pd
from rdflib import Graph,Namespace
from rdflib.namespace import FOAF,RDF,RDFS,OWL
from rdflib import Literal,URIRef,RDF,RDFS
import re
df=pd.read_excel(r'C:\Users\Niharika Sri Parasa\Downloads\list-animals-world-611j.xls',encoding="utf-8")

url="http://dbpedia.org/resource/"
g=Graph()
dict={'subject':[],'predicate':[],'object':[]}
concepts=['Animal','Amphibian','Reptile','Fish','Bird','Mammal','Insect','Crustacean','Eukaryote','Crustacean','Mollusc','Plant','Species']
#for animal in df['Animal'].unique():
for animal in concepts:
    if 'nan' not in str(animal):
        resource=url+animal
        print(resource)
        g.parse(resource)
        for s,p,o in g.triples((URIRef(resource),URIRef('http://dbpedia.org/ontology/abstract'),None)):
          if o.language =="en":
             dict['subject'].append(animal)
             dict['predicate'].append('abstract')
             dict['object'].append(o.value)
        for s,p,o in g.triples((URIRef(resource),RDF.type,None)):
            dict['subject'].append(animal)
            dict['predicate'].append('type')
            #regex = "\/rc\/(.*)'"
            dict['object'].append(o)
print(dict)
df=pd.DataFrame.from_dict(dict)
df.to_excel('animal(200+)_conceptdef.xlsx')
#df.to_csv('Animal_triples(200+).csv')


from django.shortcuts import render, redirect
from .models import Student, Comment

# Create your views here.
def index(request):
    students = Student.objects.all().order_by('-id')
    context = {
        'students': students,
    }
    return render(request, 'students/index.html', context)

def new(request):
    if request.method == 'POST':
        # student를 생성(create)
        name = request.POST.get('name')
        age = request.POST.get('age')
        student = Student(name=name, age=age)
        student.save()
        return redirect('students:detail', student.pk)
    else: #=> 'GET'
        # new page를 보여줌
        return render(request, 'students/new.html')

# def create(request): => RESTful한 주소를 위해 new로 이동
#     name = request.POST.get('name')
#     age = request.POST.get('age')
#     student = Student.objects.create(name=name, age=age)
#     # return redirect(f'/students/{student.pk}/')
#     return redirect('students:detail', student.pk)

def detail(request, pk):
    # 1. pk에 해당하는 student를 db에서 가져오기
    student = Student.objects.get(id=pk)
    # 1-1. student의 comment를 다 가져오기
    comments = student.comment_set.all()
    # 1-2. student의 comment 개수
    # comments.count

    context = {
        'student': student,
        'comments': comments,
    }
    return render(request, 'students/detail.html', context)

def delete(request, pk):
    if request.method == 'POST': # GET요청이 들어올 경우 수행x
        student = Student.objects.get(id=pk)
        student.delete()
        # return redirect('/students/')
        return redirect('students:index')

def edit(request, pk):
    student = Student.objects.get(id=pk)

    if request.method == 'POST':
        student = Student.objects.get(id=pk)
        name = request.POST.get('name')
        age = request.POST.get('age')
        student.name = name
        student.age = age
        student.save()
        return redirect('students:detail', student.pk)

    else:
        context = {
            'student': student,
        }
        return render(request, 'students/edit.html', context)

# def update(request, pk):
#     student = Student.objects.get(id=pk)
#     name = request.POST.get('name')
#     age = request.POST.get('age')

#     student.name = name
#     student.age = age
#     student.save()
#     # return redirect(f'/students/{student.pk}/')
#     return redirect('students:detail', student.pk)

def comments_new(request, student_pk):
    # 1. request에서 데이터 가져오기
    content = request.POST.get('content')
    # 2. Comment 생성
    comment = Comment()
    comment.content = content
    comment.student_id = student_pk
    comment.save()
    # 3. student 상세 페이지로 redirect
    return redirect('students:detail', student_pk)

# POST 요청을 받음
def comments_delete(request, student_pk, pk):
    comment = Comment.objects.get(pk=pk)
    comment.delete()
    return redirect('students:detail', student_pk)

def comments_edit(request, student_pk, pk):
    comment = Comment.objects.get(pk=pk)
    if request.method == 'POST':
        # POST
        # 1. POST로 넘어온 데이터 가져오기
        content = request.POST.get('content')
        # 2. comment에 바꿔 넣기 & 저장
        comment.content = content
        comment.save()
        # 3. 댓글목록 페이지로 redirect
        return redirect('students:detail', student_pk)

    else:
        # GET
        context = {
            'comment': comment,
        }
        return render(request, 'students/comments_edit.html', context)


#!/usr/bin/python3

# A script to interpolate the TRMM data to match ISCCP regular area boxes. The data is coarsened to 2.5 deg resolution first.

import numpy as np
import sys
from calendar import isleap
from trend_funcs import read_netcdf
from scipy.interpolate import griddata
from netCDF4 import Dataset

year = int(sys.argv[1])
datapath = '/home/btan1/Monash/Data/'
trmmpath = '/media/Sentinel/data/TRMM/3B42/netcdf/'

# ninja-load the lat/lon info
with Dataset('%s3B42.199801.nc' % (trmmpath,)) as f:
    lats = f.variables['latitude'][:].copy()
    lons = f.variables['longitude'][:].copy()

lat0 = np.where(lats == -34.875)[0][0]
lat1 = np.where(lats ==  34.875)[0][0] + 1
nlat = lat1 - lat0
nlon = len(lons)

# extract lat/lon coordinates of the TRMM grid
lats_avg = np.array([np.mean(lats[lat0 + ii : lat0 + ii + 10]) for ii in range(0, nlat, 10)])
lons_avg = np.array([np.mean(lons[jj : jj + 10]) for jj in range(0, nlon, 10)])
lons_ext = np.hstack([lons_avg, lons_avg + 360]).flatten()
pts = np.array([[lat, lon] for lat in lats_avg for lon in lons_ext])

# extract lat/lon coordinates of ISCCP grid
box_lat = read_netcdf(datapath, 2007, 1, variable = 'box_lat')
box_lon = read_netcdf(datapath, 2007, 1, variable = 'box_lon')

# load data
P = np.ma.masked_all([(365 + isleap(year)) * 8, len(box_lat)], dtype = np.float32)
mday = 0

for month in range(1, 13):

    f = Dataset('%s3B42.%d%02d.nc' % (trmmpath, year, month), 'r')
    nday = len(f.variables['pcp'])

    for day in range(nday):

        # average the data to 1 deg boxes so values are comparable to GPCP prior to interpolation
        trmm = f.variables['pcp'][day, lat0 : lat1] * 24
        trmm_avg = np.ma.masked_invalid([[np.ma.mean(trmm[ii : ii + 10, jj : jj + 10])
            for jj in range(0, nlon, 10)] for ii in range(0, nlat, 10)])

        # perform the interpolation
        trmm_ext = np.ma.concatenate([trmm_avg, trmm_avg], 1).flatten()
        mask = ~np.ma.getmaskarray(trmm_ext)
        mask_intp = griddata(pts, mask, (box_lat, box_lon), method = 'linear')
        P[mday + day] = np.ma.masked_where(mask_intp != 1,
            griddata(pts[mask], trmm_ext[mask], (box_lat, box_lon), method = 'linear'))

    mday += nday
    f.close()

with open('%strend/precip_data/trmm_%d.npy' % (datapath, year), 'wb') as f:
    np.ma.dump(P, f)


import numpy as np
import pandas as pd
from d02_processing.variables import ExtractVariables
from d02_processing.indices import CalculateIndices
import json

class Tables:
    def __init__(self, data, time_step):
        self.data = data
        self.time_step = time_step
        self.output_path = 'images/tables'
        date = pd.to_datetime(
            np.datetime_as_string(data[list(dict(self.data.dims).keys())[-1]][self.time_step].values, unit='h',
                                  timezone='UTC'))
        self.time_stamp = f'{date.day:02d}-{date.month:02d}-{date.year} {date.hour:02d}UTC'
        self.levels = list(self.data['isobaric'].values / 100)[::-1]

        # Variables
        variables = ExtractVariables(self.data, self.time_step)
        self.tair = variables.temperature
        self.dewp = variables.dew_point
        self.rhum = variables.relative_humidity
        self.hgpt = variables.geopotential_height
        self.mslp = variables.mean_sea_level_pressure
        self.omega = variables.omega
        self.wnd_dir = variables.wind_direction
        self.wnd_spd = variables.wind_speed
        self.u_wnd = variables.u_wind
        self.v_wnd = variables.v_wind
        self.div = variables.divergence
        self.prwt = variables.precipitable_water

        # Indices
        indices = CalculateIndices(self.data, self.time_step)
        self.k_index = indices.k
        self.tt_index = indices.tt
        self.li_index = indices.li
        self.vt_index = indices.vt
        self.sweat_index = indices.sweat

        # Loading airports coordinates
        with open('data/02_external_data/airports.json') as json_file:
            self.airports = json.load(json_file)

    def extract_airport_data(self, index, lat, lon):
        i_lat = np.where(self.data['lat'] == round(lat * 4) / 4)[0][0]
        i_lon = np.where(self.data['lon'] - 360 == round(lon * 4) / 4)[0][0]
        airport_data = np.array(index)[i_lat][i_lon]
        return airport_data

    def create_table(self):
        """
        :param adjusted_data: receives the post processed dataframe
        :param valid:
        :return:
        """
        for area in self.airports:
            for airport in self.airports[area]:
                lon = self.airports[area][airport]['lon']
                lat = self.airports[area][airport]['lat']
                k_index = self.extract_airport_data(self.k_index(), lat, lon)
                print(f'{self.time_stamp} - {airport}: {k_index}')
                # table_output_path = f'{self.output_path}/{area}/'
                # Path(table_output_path).mkdir(parents=True, exist_ok=True)
                # plt.savefig(f'{table_output_path}/sounding_{airport}_{self.time_step}.jpg')

        return k_index

    # def calculate_indices(self, station_data):
    #     p = station_data['pressure'].values * units.hPa
    #     T = station_data['Temperature_isobaric'].values * units.degC
    #     Td = station_data['Dewpoint'].replace(
    #         np.nan, 0.0000001).values * units.degC
    #     prof = mpcalc.parcel_profile(p, T[0], Td[0])
    #     cape = mpcalc.cape_cin(pressure=p,
    #                            temperature=T,
    #                            dewpt=Td,
    #                            parcel_profile=prof)
    #     lcl_pressure, lcl_temperature = mpcalc.lcl(pressure=p[0],
    #                                                temperature=T[0],
    #                                                dewpt=Td[0])
    #     el_pressure, el_temperature = mpcalc.el(pressure=p[0],
    #                                             temperature=T[0],
    #                                             dewpt=Td[0])
    #     lfc_pressure, lfc_temperature = mpcalc.lfc(pressure=p[0],
    #                                                temperature=T[0],
    #                                                dewpt=Td[0])

if __name__ == "__main__":
    pass


import numpy as np
import pymesh
import multiprocessing
import json
import argparse

import objwriter
import lsystem

obj = objwriter.Objwriter()

def create_tree(name: str, file_name: str):
    with open('rules.json') as f:
        data = json.load(f)
    rules = data[name]

    if "leaf" in rules:
        lsys = lsystem.Lsystem(rules=rules["rules"], leaf=rules["leaf"])
    else:
        lsys = lsystem.Lsystem(rules=rules["rules"])

    output = lsys.create_lsystem(iterations=6, axiom=rules["axiom"], leaf=False)
    meshes, leaves = lsys.draw_lsystem(instructions=output, angle=22, distance=1)

    merged_mesh = pymesh.merge_meshes(meshes)
    mesh, info = pymesh.collapse_short_edges(merged_mesh, 0.1)
    mesh, info = pymesh.remove_duplicated_vertices(mesh, 0.1)
    mesh, info = pymesh.remove_duplicated_faces(mesh)

    if len(leaves) > 0:
        obj.generate_obj(file_path=file_name, mesh=mesh, leaves=leaves)
    else:
        obj.generate_obj(file_path=file_name, mesh=mesh)

def divide_and_conquer(plants_to_generate: int, name: str, number: str):
    index = number
    for x in range(plants_to_generate):
        index+=1
        file_name = name+"-"+str(index)
        create_tree(name=name, file_name=file_name)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Generate a tree.')
    parser.add_argument('--cores', type=int, help='Specify how many cores to use')
    parser.add_argument('--tn', type=str, help='Specify the rule to use from rules.json')
    parser.add_argument('--n', type=int, help='Number of trees/plants to generate')
    args = parser.parse_args()

    cpu_cores = multiprocessing.cpu_count()
    plants_to_generate = 1

    if args.cores is None:
        cpu_cores = 1
    elif cpu_cores >= args.cores:
        cpu_cores = args.cores
    if args.n is not None:
        plants_to_generate = args.n
    if args.tn is None:
        print('Please provide a name for the rule you using from rules.json --tn argument.')

    if cpu_cores > plants_to_generate:
        cpu_cores = plants_to_generate

    initial_chunk_size = plants_to_generate // cpu_cores
    remaining = plants_to_generate % cpu_cores
    chunks = [initial_chunk_size] * cpu_cores
    for i in range(remaining):
        chunks[i] += 1

    processes = []

    index = 0
    for chunk in chunks:
        p = multiprocessing.Process(target=divide_and_conquer, args=[chunk,args.tn,index])
        p.start()
        processes.append(p)
        index += chunk

    for p in processes:
        p.join()

    obj.generate_mtl(file_path=args.tn+"-1")