# coding:utf8

"""
读取xml文件
"""

import os
import xml.etree.cElementTree as cET

def get_value(filename, key):
    xmlFilePath = os.path.abspath(filename)
    if not os.path.exists(xmlFilePath):
        raise "Has no such file: %s" % xmlFilePath
    try:
        tree = cET.parse(xmlFilePath)
        root = tree.getroot()
    except Exception as e:
        raise "Errors:  %s"  % e
    dict1 = {}
    for child in root:
        tag = child.tag
        value = child.text
        value = value.replace("\\", "/")
        dict1[tag] = value
    try:
        value = dict1[key]
        return value
    except:
        raise "Has no key: %s" % key

if __name__ == "__main__":
    filename = input("文件名:")
    key = input("属性:")
    value = get_value(filename, key)
    print('The value of %s is %s' % (key, value))


from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.fields.core import SelectField
from wtforms.validators import DataRequired, URL
import pandas as pd

app = Flask(__name__)
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
Bootstrap(app)

class CafeForm(FlaskForm):
    cafe = StringField('Cafe name', validators=[DataRequired()])
    location = StringField('Location (Google Maps URL)',
                           validators=[DataRequired(), URL()])
    open = StringField('Opening Time (e.g. 8am)', validators=[DataRequired()])
    close = StringField('Closing Time (e.g. 10pm)',
                        validators=[DataRequired()])
    coffee = SelectField('Coffee Rating', choices=[
                         "☕", "☕☕", "☕☕☕", "☕☕☕☕", "☕☕☕☕☕"], validators=[DataRequired()])
    wifi = SelectField('Wifi Strength Rating', choices=[
                       "✘", "💪", "💪💪", "💪💪💪", "💪💪💪💪", "💪💪💪💪💪"], validators=[DataRequired()])
    power = SelectField('Power Socket Availability', choices=["✘", "🔌", "🔌🔌", "🔌🔌🔌", "🔌🔌🔌🔌", "🔌🔌🔌🔌🔌"],
                        validators=[DataRequired()])
    submit = SubmitField('Submit')

# all Flask routes below
@ app.route("/")
def home():
    return render_template("index.html")

@ app.route('/add', methods=["GET", "POST"])
def add_cafe():
    form = CafeForm()
    if form.validate_on_submit():
        with open('/Users/smith/python/100DaysofPython/Day62CoffeeandWifi/cafe-data.csv') as csv_file:
            csv_file.write(f"\n{form.cafe.data},"
                           f"{form.location.data},"
                           f"{form.open.data},"
                           f"{form.close.data},"
                           f"{form.coffee_rating.data},"
                           f"{form.wifi_rating.data},"
                           f"{form.power_rating.data}")
        return redirect(url_for('cafes'))
    return render_template('add.html', form=form)

@ app.route('/cafes')
def cafes():
    with open('/Users/smith/python/100DaysofPython/Day62CoffeeandWifi/cafe-data.csv', newline='') as csv_file:
        csv_data = pd.read_csv(csv_file)
    return render_template('cafes.html', rows=list(csv_data.values.tolist()), titles=csv_data.columns.values)

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


import ctypes
import errno
import inspect
import json
import logging
import os
import sys

from pyroute2 import NetlinkError, IPRoute

from interface import Interface as CniInterface
from interface import CniNamespace as CniNamespace
from cni import Error as Error

# Error codes
CNI_ERROR_VETH_ADD = 301

# logger for the file
logger = None

class CniVEthPair(CniInterface, object):
    '''
    Class to manage veth-pair interfaces created for containers.
    Creates a veth-pair and one end of the pair is in host-os and another
    end inside the container.
    '''
    # Number of characters to pick from UUID for ifname
    CNI_UUID_IFNAME_LEN = 11

    def __init__(self, cni, mac):
        self.host_ifname = CniVEthPair._build_tapname(cni.container_uuid)
        self.container_mac = mac
        self.pid = os.getpid()
        CniInterface.__init__(self, cni)
        return

    @staticmethod
    def _build_tapname(uuid):
        '''
        Make tap-interface name in host namespace
        '''
        return 'tap' + str(uuid)[:CniVEthPair.CNI_UUID_IFNAME_LEN]

    def delete_interface(self):
        '''
        Delete the veth-pair.
        Deleting container end of interface will delete both end of veth-pair
        '''
        self.delete_link()
        return

    def create_interface(self):
        '''
        Create veth-pair
        Creates veth-pair in the container-namespace first and then moves
        one end of the pair to host-os namespace
        '''
        # If the host end of veth is already present in host-os, it means
        # interface create was already done. There is nothing to do
        iproute = IPRoute()
        iface = iproute.link_lookup(ifname=self.host_ifname)
        if len(iface) != 0:
            return

        host_ifindex = None
        with CniNamespace(self.cni.container_netns):
            # Create veth pairs if not already created inside namespace
            # One end of pair is named host_ifname and the other end of pair
            # is set a container_ifname
            ns_iproute = IPRoute()
            ns_iface = ns_iproute.link_lookup(ifname=self.cni.container_ifname)
            if len(ns_iface) == 0:
                try:
                    ns_iproute.link_create(ifname=self.cni.container_ifname,
                                        peer=self.host_ifname, kind='veth',
                                        address=self.container_mac)
                except NetlinkError as e:
                    if e.code != errno.EEXIST:
                        raise Error(CNI_ERROR_VETH_ADD,
                                    'Error creating veth device ' +
                                    self.host_ifname + ' code ' +
                                    str(e.code) + ' message ' + e.message)

            # We must move the host part of veth pair to host-namespace
            # Get the interface-index. We will move to host namespace once
            # we exit container-name space and go to host-namespace
            host_ifindex = ns_iproute.link_lookup(ifname=self.host_ifname)[0]

        if host_ifindex is not None:
            ns_iproute.link('set', index=host_ifindex, net_ns_pid=self.pid)

        return

    def configure_interface(self, ip4, plen, gw):
        # Set link-up for interface on host-os
        iproute = IPRoute()
        idx = iproute.link_lookup(ifname=self.host_ifname)[0]
        iproute.link('set', index=idx, state='up')
        super(CniVEthPair, self).configure_interface(ip4, plen, gw)


from django import template
from django.db.models.aggregates import Count

from ..models import Post, Category, Tag
from ..forms import CommentForm

register = template.Library()

@register.inclusion_tag('petblog/inclusions/_recent_posts.html', takes_context=True)
def show_recent_posts(context, num=3):
    return {
        'recent_post_list': Post.objects.all().order_by('-created_time')[:num]
    }

@register.inclusion_tag('petblog/inclusions/_categories.html', takes_context=True)
def show_categories(context):
    category_list = Category.objects.annotate(num_posts=Count('post')).filter(num_posts__gt=0)
    return {
        'category_list': category_list,
    }

@register.inclusion_tag('petblog/inclusions/_tags.html', takes_context=True)
def show_tags(context):
    tag_list = Tag.objects.annotate(num_posts=Count('post')).filter(num_posts__gt=0)
    return {
        'tag_list': tag_list,
    }

@register.inclusion_tag('petblog/inclusions/_archives.html', takes_context=True)
def show_archives(context):
    return {
        'date_list': Post.objects.dates('created_time', 'month', order='DESC'),
    }

@register.inclusion_tag('petblog/inclusions/_form.html', takes_context=True)
def show_comment_form(context, post, form=None):
    if form is None:
        form = CommentForm()
    return {
        'form': form,
        'post': post,
    }

@register.inclusion_tag('petblog/inclusions/_comments.html', takes_context=True)
def show_comments(context, post):
    comment_list = post.comment_set.all().order_by('created_time')
    comment_count = comment_list.count()
    return {
        'comment_count': comment_count,
        'comment_list': comment_list,
    }


def WordsInDict(s, word_list):
    n = 0
    for i in s:
        temp = i
        if i in word_list:
            n += 1
        else:
            for j in word_list:
                if j in i:
                    i = i.replace(j, '')
            if not i:
                n += 1
                word_list.append(temp)
    return n

n = int(input())
for i in range(n):
    num_word = int(input())
    w = input().strip().split(' ')
    w = sorted(w, key=lambda x: len(x))[::-1]
    s = input().strip().split(' ')
    print(WordsInDict(s, w))


# SPDX-License-Identifier: Apache-2.0

import omni.kit.context_menu

def show_menu(objects):
    print("show it?")
    return True

def hello_world(objects):
    print(f"Hello Objects: {objects}")

menu_item_config = {
    "name": "Hello World!",
    "glyph": "menu_search.svg",
    "show_fn": [show_menu],
    "onclick_fn": hello_world,
}

# You must keep a reference to the menu item. Set this variable to None to remove the item from the menu
hello_world_menu_item = omni.kit.context_menu.add_menu(menu_item_config, "MENU", "omni.kit.window.viewport")
hello_world_menu_item = None


import frappe
from frappe.model.document import Document
from field_force.field_force.doctype.utils import set_cheat_status, set_location_to_map, set_sales_person, set_employee

class MerchandisingPicture(Document):
    def validate(self):
        # if frappe.session.user != 'administrator':
        # 	self.user = frappe.session.user

        if not self.email or not self.user_fullname:
            email, user_fullname = frappe.db.get_value('User', self.user, ['email', 'full_name'])

            if not self.email and email:
                self.email = email
            if not self.user_fullname and user_fullname:
                self.user_fullname = user_fullname

        if not self.contact_number or not self.customer_address:
            contact_number, customer_address = frappe.db.get_value('Customer', self.customer, ['contact_number', 'address'])

            if not self.contact_number and contact_number:
                self.contact_number = contact_number
            if not self.customer_address and customer_address:
                self.customer_address = customer_address

        set_sales_person(self)
        set_employee(self)
        set_cheat_status(self)

    def before_save(self):
        self.add_feedback_from()
        set_location_to_map(self)

    def add_feedback_from(self):
        if self.feedback:
            previous_doc = self.get_doc_before_save()

            if previous_doc:
                previous_feedback = previous_doc.get('feedback')

                if previous_feedback != self.feedback \
                    and frappe.db.exists("Sales Person", {"user": frappe.session.user}):
                    self.feedback_from = frappe.db.get_value("Sales Person", {"user": frappe.session.user}, 'name')
                else:
                    self.feedback_from = None
        else:
            self.feedback_from = None


import string
import nltk
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob
from nltk import PorterStemmer,word_tokenize
from nltk.corpus import stopwords
import spacy

df = pd.read_csv('D:\Desktop\SMA\Practicals\SMA Pycharm Pracs\sma_dataset.csv')
print(df)

nlp = spacy.load('en_core_web_sm')
# preprocessing
def preprocess(text):

    # text = text.lower()
    #
    # # Removing punctutions
    # text = text.translate(str.maketrans('','',string.punctuation))
    #
    # # Tokenizing words and Removing stopwords
    # words = nltk.word_tokenize(text)
    # stop_words = set(stopwords.words(('english')))
    # words = [word for word in words if word not in stop_words]
    #
    # # Stem the words
    # stemmer = PorterStemmer()
    # words = [stemmer.stem(word) for word in words]
    doc = nlp(str(text).lower())

    tokens = [token for token in doc]
    tokens = [token for token in doc if not token.is_stop and not token.is_punct and not token.is_digit]
    print(tokens)
    print()

    # finding lemma
    tokens = [token.lemma_ for token in tokens]

    # Join the words back into a string
    text = ' '.join(tokens)

    return text

# Cleaning/ Preprocessing the tweets
df['Clean Tweet'] = df['Tweet'].apply(preprocess)

# Getting the polarity from the tweets
def get_sentiment(text):
    return TextBlob(text).sentiment.polarity

df['Sentiment_polarity'] = df['Clean Tweet'].apply(get_sentiment)

sentiment = df[['Clean Tweet','Sentiment_polarity']]
print()
print(sentiment)

# identifying the tweet as positive,negative or neutral
pos = 0
neg = 0
neu = 0

sentiment_list = []

# Create a loop to classify the tweets as Positive, Negative, or Neutral.
# Count the number of each

for items in df['Sentiment_polarity']:

    if items > 0:
        s = 'Positive'
        pos = pos + 1
        sentiment_list.append(s)

    elif items < 0:
        s = 'Negative'
        neg = neg + 1
        sentiment_list.append(s)

    else:
        s = 'Neutral'
        neu = neu + 1
        sentiment_list.append(s)

df['Sentiment'] = sentiment_list
print("\n\n",df[['Clean Tweet','Sentiment']].head(5))

sentiment_counts = df['Sentiment'].value_counts()

# plotting a bar graph
plt.bar(sentiment_counts.index,sentiment_counts.values)
plt.title('Sentiment Distribution')
plt.xlabel('Sentiment')
plt.ylabel('Count')
plt.show()

# Plotting a pie graph
plt.pie(sentiment_counts,autopct='%1.1f%%',startangle=90,labels=sentiment_counts.index)
plt.title('Sentiment Distribution')
plt.show()


import requests
from concurrent.futures import ThreadPoolExecutor
import difflib
from bs4 import BeautifulSoup, SoupStrainer
from typing import Dict
import logging
from constants import (
    imdb_search_movie_url,
    imdb_poster_url,
    imdb_enrich_movie_url,
    imdb_api_url,
    imdb_enrich_tv_url,
    imdb_search_tv_url,
    imdb_external_id_url,
)
import os

movieDB_key = os.getenv("MOVIEDB_KEY")

def find_image_from_source(url):
    headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
        }
    request_object = requests.get(url, headers=headers, timeout=10).text

    parse_only = SoupStrainer("meta")
    head_content = BeautifulSoup(
        request_object, "lxml", parse_only=parse_only
    ).find_all("meta")

    for meta in head_content:
        if "og:image" in str(meta) and "property" in meta.attrs.keys():
            return meta['content']

        if "twitter:image" in str(meta) and "property" in meta.attrs.keys():
            return meta['content']

    return None

def film_single_result(idd):
    url = f"{imdb_enrich_movie_url}/{idd}?api_key={movieDB_key}&language=en-US"
    movie_result = requests.get(url).json()

    try:
        movie_data = {}
        movie_data["title"] = movie_result["title"]

        movie_data["medium"] = "film"
        movie_data["url"] = "https://www.imdb.com/title/" + movie_result["imdb_id"]
        movie_data["creators"] = [
            production["name"] for production in movie_result["production_companies"]
        ]
        movie_data['published_at'] = movie_result['release_date']

        if movie_result["poster_path"]:
            movie_data["image"] = imdb_poster_url + movie_result["poster_path"]

        else:
            movie_data["image"] = find_image_from_source(movie_data['url'])

        return movie_data

    except Exception as e:
        logging.error(e)
        return None

def movie_search(query):
    result = []
    imdb_url = f"{imdb_search_movie_url}api_key={movieDB_key}&query={query}&page=1"

    search_data = requests.get(imdb_url).json()
    result_ids = []

    if 'results' in list(search_data.keys()):
        for movie_result in search_data["results"]:
            result_ids.append(movie_result["id"])

        if result_ids:
            workers = len(result_ids)
        else:
            workers = 1

        with ThreadPoolExecutor(max_workers=workers) as executor:

            for each in executor.map(film_single_result, result_ids):
                if each is not None:
                    result.append(each)

    return result

def enrich_movie(internal_id):
    url = f"{imdb_enrich_movie_url}/{internal_id}?api_key={movieDB_key}&language=en-US"
    movie_result = requests.get(url).json()

    movie_data = {}
    movie_data["title"] = movie_result["title"]
    movie_data["tagline"] = movie_result["tagline"]
    movie_data["status"] = movie_result["status"]
    movie_data["release_date"] = movie_result["release_date"]
    movie_data["vote_average"] = movie_result["vote_average"]
    movie_data["productions"] = [
        c["name"] for c in movie_result["production_companies"]
    ]
    movie_data["runtime"] = movie_result["runtime"]
    movie_data["genres"] = [genre["name"] for genre in movie_result["genres"]]
    movie_data["poster_path"] = imdb_poster_url + movie_result["poster_path"]
    movie_data["official_website"] = movie_result["homepage"]
    movie_data["overview"] = movie_result["overview"]
    movie_data["imdb_link"] = "https://www.imdb.com/title/" + movie_result["imdb_id"]

    url_movie = "%smovie/%s/videos" % (imdb_api_url, internal_id)
    Trailer = requests.get(url_movie, params={"api_key": movieDB_key,}).json()[
        "results"
    ][0]

    movie_data["trailer"] = "https://www.youtube.com/watch?v=" + Trailer["key"]

    return movie_data

def tv_single_result(idd):
    url = f"{imdb_enrich_tv_url}/{idd}?api_key={movieDB_key}&language=en-US"
    get_id = (
        f"{imdb_external_id_url}{idd}/external_ids?api_key={movieDB_key}&language=en-US"
    )
    tv_result = requests.get(url).json()
    imdb_id = requests.get(get_id).json()["imdb_id"]
    try:
        tv_data = {}
        tv_data["title"] = tv_result["name"]
        tv_data["medium"] = "tv"
        tv_data["url"] = "https://www.imdb.com/title/" + imdb_id
        tv_data["creators"] = [creator["name"] for creator in tv_result["created_by"]]
        tv_data['published_at'] = tv_result['first_air_date']

        if tv_result["poster_path"]:
            tv_data["image"] = imdb_poster_url + tv_result["poster_path"]

        else:
            tv_data["image"] = find_image_from_source(tv_data['url'])

        return tv_data

    except Exception as e:
        logging.error(e)
        return None

def tv_search(query):
    result = []
    imdb_url = f"{imdb_search_tv_url}api_key={movieDB_key}&query={query}&page=1"

    search_data = requests.get(imdb_url).json()
    result_ids = []

    if "results" in list(search_data.keys()):
        for tv_result in search_data["results"]:
            result_ids.append(tv_result["id"])

        if result_ids:
            workers = len(result_ids)
        else:
            workers = 1

        with ThreadPoolExecutor(max_workers=workers) as executor:

            for each in executor.map(tv_single_result, result_ids):
                if each is not None:
                    result.append(each)

    return result

def search_film(query):

    pool = ThreadPoolExecutor(max_workers=2)
    movie_result = pool.submit(movie_search, query)
    tv_result = pool.submit(tv_search, query)

    movie_result = movie_result.result()
    tv_result = tv_result.result()

    main_result = tv_result+movie_result

    if main_result:
        result_size = len(main_result)
    else:
        result_size = 1

    relevance_sort = difflib.get_close_matches(
        query, [x["title"] for x in main_result], n=result_size, cutoff=0.4
    )
    final = []
    added = []

    for rel_sorted in relevance_sort:
        for result in main_result:
            if (
                result["title"] == rel_sorted
                and len(final) < 10
                and rel_sorted not in added
            ):
                final.append(result)
                added.append(rel_sorted)
                break

    del relevance_sort
    del added

    return final


from time import sleep

from selenium import webdriver

driver = webdriver.Chrome()  # 需要 下载 对应浏览器 驱动到 python 安装目录
driver.get("http://wxmz.gzthfy.com/?v=2021060101159#/ReportList")  # 刷新网址

for i in range(10000):  # 刷新次数
    driver.refresh()  # 刷新网页
    sleep(10)  # 五秒一次


import requests
import json
import os
from checkin_api.mattermost.auth import get_session_token
from checkin_api.config import mattermost_token

def post_message_to_channel(message):
    token = mattermost_token
    res = requests.post(url='https://chat.vietnamlab.vn/api/v4/posts',
                        headers={
                            'Authorization': f'Bearer {token}',
                            'Content-Type': 'application/json',
                        },
                        data=json.dumps({
                            "channel_id": "n3oyx6upyi86jjcixc65hdbkqc",
                            "message": message,
                            "file_ids": [
                            ],
                            "props": {}
                        }))
    print(res.status_code, res.json())

if __name__ == '__main__':
    post_message_to_channel()


from __future__ import annotations

from promptware.constants import (
    ApplicationCategory,
    ApplicationSubcategory,
    DesignPatternType,
    LanguageType,
    LicenseType,
    PlatformType,
    TaskType,
)
from promptware.info import SoftwareInfo
from promptware.kernels.plm import PLMKernelConfig
from promptware.promptware import PromptConfig, Promptware

class NaturalLanguageToStripeAPIPromptware(Promptware):
    def _info(self) -> SoftwareInfo:
        return SoftwareInfo(
            description="This promptware is used to "
            "convert natural lanugage to Stripe API.",
            creator="OpenAI",
            homepage="https://beta.openai.com/examples/",
            reference="",
            codebase_url="https://beta.openai.com/examples/",
            license=LicenseType.no_license,
            research_tasks=[TaskType.conditional_generation],
            application_categories=[ApplicationCategory.transformation],
            application_subcategories=[ApplicationSubcategory.translation],
            original_platform=PlatformType.gpt3,
            design_pattern=DesignPatternType.standalone,
            source_language=LanguageType.en,
            target_language=LanguageType.javascript,
        )

    def _kernel_configs(self):
        return {
            "openai": PLMKernelConfig(
                platform="openai",
                model_name="code-davinci-002",  # "text-curie-001",
                max_tokens=100,
                top_p=1.0,
                temperature=0,
                frequency_penalty=0.0,
                presence_penalty=0.0,
                stop=['"""'],
            )
        }

    def _software_configs(self):
        return {
            "natural_language_to_stripe_api": PromptConfig(
                name="natural_lanugage_to_stripe_api",
                description="This promptware is used to "
                "convert natural lanugage to Stripe API.",
                instruction='"""\nUtil exposes the following:\n\n'
                "util.stripe() -> authenticates "
                "& returns the stripe module; "
                'usable as stripe.Charge.create etc\n"""\n',
                demonstration=[],
                prompt_template=lambda input: f"{input['text']}",
                task=TaskType.conditional_generation,
            )
        }

    def _example(self):
        return {
            "input": {
                "text": 'import util\n"""\n'
                "Create a Stripe token using the users credit card:"
                " 5555-4444-3333-2222, expiration date 12 / 28,"
                ' cvc 521\n"""'
            },
            "output": 'token = stripe.Token.create(\n    card={\n        "number": '
            '"5555-4444-3333-2222",\n        "exp_month": 12,\n        '
            '"exp_year": 28,\n        "cvc": "521"\n    },\n)',
        }


#!/usr/bin/python
#
## @file
#
# Handles the display of the current parameters for a given camera.
#
# Methods:
#
# newParameters(parameters)
#    Update the display with new parametes.
#
# newTemperature(temperature_data)
#    Update the temperature display data.
#
#
# Signals:
#
# gainChange(int gain)
#
#
# Hazen 10/13
#

from PyQt4 import QtCore, QtGui

# Debugging
import sc_library.hdebug as hdebug

## CameraParams
#
# This class handles displaying (some of) the current camera parameters
# in the UI. It also handles the EMCCD gain slider.
#
class CameraParams(QtGui.QGroupBox):
    gainChange = QtCore.pyqtSignal(int)

    ## __init__
    #
    # Create a camera parameters object.
    #
    # @param camera_params_ui The UI object like what would be created from a .ui file.
    # @param parent (Optional) The PyQt parent of this object.
    #
    @hdebug.debug
    def __init__(self, camera_params_ui, parent = None):
        QtGui.QGroupBox.__init__(self, parent)
        self.temperature = False

        # UI setup
        self.ui = camera_params_ui
        self.ui.setupUi(self)

        # connect signals
        self.connect(self.ui.EMCCDSlider, QtCore.SIGNAL("valueChanged(int)"), self.cameraGainChange)

    ## cameraGainChange
    #
    # Handles camera gain changes. Updates the display and emits a gainChange signal
    # that is received by the camera control object.
    #
    # @param new_gain The new EMCCD gain value.
    #
    @hdebug.debug
    def cameraGainChange(self, new_gain):
        self.ui.EMCCDLabel.setText("EMCCD Gain: %d" % new_gain)
        self.gainChange.emit(new_gain)

    ## newParameters
    #
    # Update the displayed camera parameters based on the new parameters object.
    #
    # @param parameters A parameters object.
    #
    @hdebug.debug
    def newParameters(self, parameters):
        p = parameters
        if (hasattr(p, "temperature")):
            self.temperature = p.temperature

        if (hasattr(p, "emgainmode")):
            if p.emgainmode == 0:
                self.ui.EMCCDSlider.setMaximum(255)
            else:
                self.ui.EMCCDSlider.setMaximum(100)
            self.ui.EMCCDSlider.setValue(p.emccd_gain)

        if (hasattr(p, "preampgain")):
            self.ui.preampGainText.setText("%.1f" % p.preampgain)

        self.ui.pictureSizeText.setText(str(p.x_pixels) + " x " + str(p.y_pixels) +
                                        " (" + str(p.x_bin) + "," + str(p.y_bin) + ")")

        if p.external_trigger:
            self.ui.exposureTimeText.setText("External")
            self.ui.FPSText.setText("External")
        else:
            self.ui.exposureTimeText.setText("%.4f" % p.exposure_value)
            self.ui.FPSText.setText("%.4f" % (1.0/p.kinetic_value))

    ## newTemperature
    #
    # Updates the temperature display element of the UI.
    #
    # @param temp_data A two element array, [camera temperature, "stable" / "unstable"].
    #
    @hdebug.debug
    def newTemperature(self, temp_data):
        if temp_data[1] == "stable":
            self.ui.temperatureText.setStyleSheet("QLabel { color: green }")
        else:
            self.ui.temperatureText.setStyleSheet("QLabel { color: red }")
        self.ui.temperatureText.setText(str(temp_data[0]) + " (" + str(self.temperature) + ")")

    ## showEMCCD
    #
    # Show or hide the UI fields associated with EM gain.
    #
    # @param visible True/False.
    #
    @hdebug.debug
    def showEMCCD(self, visible):
        if visible:
            self.ui.EMCCDLabel.show()
            self.ui.EMCCDSlider.show()
        else:
            self.ui.EMCCDLabel.hide()
            self.ui.EMCCDSlider.hide()

    ## showPreamp
    #
    # Show or hide the UI fields associated with pre-amplifier gain.
    #
    # @param visible True/False.
    #
    @hdebug.debug
    def showPreamp(self, visible):
        if visible:
            self.ui.preampGainLabel.show()
            self.ui.preampGainText.show()
        else:
            self.ui.preampGainLabel.hide()
            self.ui.preampGainText.hide()

    ## showTemperature
    #
    # Show or hide the UI fields associated with camera sensor temperature.
    #
    @hdebug.debug
    def showTemperature(self, visible):
        if visible:
            self.ui.temperatureLabel.show()
            self.ui.temperatureText.show()
        else:
            self.ui.temperatureLabel.hide()
            self.ui.temperatureText.hide()

#
# The MIT License
#
# Copyright (c) 2013 Zhuang Lab, Harvard University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#


import time
from selenium.webdriver.common.keys import Keys
from config import (
    get_web_driver_options,
    get_chrome_web_driver,
    set_ignore_certificate_error,
    set_browser_as_incognito,
    set_automation_as_head_less,
    NAME,
    CURRENCY,
    FILTERS,
    BASE_URL,
    DIRECTORY
)
from selenium.common.exceptions import NoSuchElementException
import json
from datetime import datetime

#url  == https://www.amazon.in/s?k={product name}&ref=filter conditions

class AmazonApi:
    def __init__(self,searchTerm,filter,baseURL,currency):
        self.baseURL=baseURL
        self.searchTerm=searchTerm
        options = get_web_driver_options()
        set_ignore_certificate_error(options)
        set_browser_as_incognito(options)
        self.driver=get_chrome_web_driver(options)
        self.currency =currency
        self.price_filter = f"&rh=p_36%3A{filter['min']}00-{filter['max']}00"

    def Run(self):
        print("starting Script.....")
        print(f"Looking for {self.searchTerm} product")
        links = self.GetProductLinks()
        if not links:
            print("Stopped script.")
            return
        print(f"Got {len(links)} links to products...")
        print("Getting info about products...")
        products = self.GetProductInfo(links)
        print( f" Got info of {len(products)} products ")
        self.driver.quit()
        return products

    def GetProductLinks(self):
        self.driver.get(self.baseURL)
        element = self.driver.find_element_by_id("twotabsearchtextbox")
        element.send_keys(self.searchTerm)
        element.send_keys(Keys.ENTER)
        time.sleep(1)
        self.driver.get(f"{self.driver.current_url}{self.price_filter}")
        time.sleep(1)
        resultList = self.driver.find_elements_by_class_name('s-result-list')
        links = []
        try:
            results = resultList[0].find_elements_by_xpath(
                "//div/span/div/div/div[2]/div[2]/div/div[1]/div/div/div[1]/h2/a")
            links = [link.get_attribute('href') for link in results]
            return links
        except Exception as e:
            print("Didn't get any products...")
            print(e)
            return links

    def GetProductInfo(self ,links):
        Asins=self.GetAsins(links)
        products=[]
        for asin in Asins:
            product = self.GetSingleProductInfo(asin)
            if product:
                products.append(product)
        return products

    def GetSingleProductInfo(self,asin):
        shortenURL = self.ShortURL(asin)
        self.driver.get(shortenURL)
        time.sleep(1)
        ProductName =self.GetName()
        Price = self.GetPrice()
        Seller =self.GetSeller()
        if Seller and ProductName and Price:
            ProductInfo={
                "Title":ProductName,
                "Price": Price,
                "Seller" : Seller
            }
            return ProductInfo
        return None

    def GetName(self):
        try:
            return self.driver.find_element_by_id("productTitle").text
        except Exception as e:
            print (e)
            print(f"Couldn'd find Title for, product :{self.driver.current_url}")
            return None

    def GetSeller(self):
        try:
            return self.driver.find_element_by_id("sellerProfileTriggerId").text
        except Exception as e:
            print (e)
            print(f"Couldn'd find Seller for, product :{self.driver.current_url}")
            return None

    def GetPrice(self):
        try:
            return self.driver.find_element_by_id("priceblock_ourprice").text
        except:
            try:
                return self.driver.find_element_by_id("priceblock_dealprice").text
            except Exception as e:
                print(e)
                print(f"Couldn'd find Price for, product :{self.driver.current_url}")
                return None

    def ShortURL(self,asin):
        return (self.baseURL+"/dp/"+asin)

    def GetAsins(self,links):
        return [self.GetAsin(link) for link in links]

    def GetAsin(self,link):
        return (link[link.find("/dp/")+4: link.find("/ref")])

if __name__ == '__main__' :
    hi = AmazonApi(NAME,FILTERS,BASE_URL,CURRENCY)
    data = hi.Run()
    print (data)


from ufl.algorithms.map_integrands import map_integrand_dags
from ufl.classes import ReferenceValue
from ufl.corealg.multifunction import MultiFunction, memoized_handler

class FunctionPullbackApplier(MultiFunction):
    """A pull back applier."""

    def __init__(self):
        """Initalise."""
        MultiFunction.__init__(self)

    expr = MultiFunction.reuse_if_untouched

    def terminal(self, t):
        """Apply to a terminal."""
        return t

    @memoized_handler
    def form_argument(self, o):
        """Apply to a form_argument."""
        # Represent 0-derivatives of form arguments on reference
        # element
        r = ReferenceValue(o)
        element = o.ufl_element()

        if r.ufl_shape != element.reference_value_shape:
            raise ValueError(
                f"Expecting reference space expression with shape '{element.reference_value_shape}', "
                f"got '{r.ufl_shape}'")
        f = element.pullback.apply(r)
        if f.ufl_shape != element.value_shape:
            raise ValueError(f"Expecting pulled back expression with shape '{element.value_shape}', "
                             f"got '{f.ufl_shape}'")

        assert f.ufl_shape == o.ufl_shape
        return f

def apply_function_pullbacks(expr):
    """Change representation of coefficients and arguments in an expression.

    Applies Piola mappings where applicable and represents all
    form arguments in reference value.

    Args:
        expr: An Expression
    """
    return map_integrand_dags(FunctionPullbackApplier(), expr)


__all__ = ['AnchorIcon', 'Icon', 'ButtonIcon', 'ToggleButtonIcon']

from .behaviors.button import ButtonBehavior, ToggleButtonBehavior
from .behaviors.touch_effecs import EffectBehavior
from .behaviors.hover import HoverBehavior

from kivy.uix.image import Image
from kivy.uix.anchorlayout import AnchorLayout

from kivy.properties import ListProperty, ObjectProperty

from kivy.lang import Builder
from kivy.clock import Clock

Builder.load_string("""

<AnchorIcon>:
    size_hint_x: None
    width: '70dp'
    radius: [0, 0, 0, 0]
    background_color: [0, 0, 0, 0]
    anchor_x: 'center'
    anchor_y: 'center'
    canvas.before:
        Color:
            rgba: self.background_color
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: self.radius or [0]

<Icon>:
    anchor_y: 'center'
    anchor_x: 'center'
    source: ''
    icon_size: ['30dp', '30dp']
    color: [1, 1, 1, 1]
    allow_stretch: True
    keep_ratio: False
    mipmap: True
    BoxLayout:
        padding: ['5dp', '5dp', '5dp', '5dp']
        size_hint: [None, None]
        size: root.icon_size
        Image:
            source: root.source
            allow_stretch: root.allow_stretch
            keep_ratio: root.keep_ratio
            mipmap: root.mipmap
            color: root.color

<ButtonIcon>:
    size_hint: [None, None]
    size: ['40dp', '40dp']
    mipmap: True
    allow_strech: True
    keep_ratio: False
    canvas:
        Clear
    canvas.after:
        Color:
            rgba: self.color
        Rectangle:
            texture: self.texture
            pos: self.pos
            size: self.size

<ToggleButtonIcon>:
    size: ['30dp', '30dp']

""")

class AnchorIcon(AnchorLayout):
    background_color = ListProperty([0, 0, 0, 0])
    radius = ListProperty([0, 0, 0, 0])

class Icon(AnchorIcon):
    pass

class ButtonIcon(ButtonBehavior, Image, EffectBehavior, HoverBehavior):
    # Internal use
    _color_icon = ListProperty([1, 1, 1, 1])

    # Properties
    icon_color = ObjectProperty([1, 1, 1, 1])
    icon_source = ObjectProperty('')
    icon_state_source = ListProperty(['', ''])

    def __init__(self, **kwargs):
        self.type_button = 'rounded'
        super().__init__(**kwargs)
        Clock.schedule_once(self.set_color)

    def set_color(self, *args):
        if self.icon_source != '' and len(self.icon_source) != 2:
            self.source = self.icon_source
        elif len(self.icon_source) == 2 and '' not in self.icon_source:
            self.source = self.icon_source[0]
        elif len(self.icon_state_source) == 2 and '' not in self.icon_state_source:
            self.source = self.icon_state_source[0]

        if isinstance(self.icon_color[0], (int, float)):
            self.color = self.icon_color
        else:
            self.color = self.icon_color[0]

    def on_state(self, widget, state):
        if len(self.icon_state_source) == 2 and '' not in self.icon_state_source:
            if state == 'normal':
                self.source = self.icon_state_source[0]
            elif state == 'down':
                self.source = self.icon_state_source[1]

    def on_cursor_enter(self, *args):
        if len(self.icon_source) == 2 and '' not in self.icon_source:
            self.source = self.icon_source[1]

        if not isinstance(self.icon_color[0], (int, float)):
            self.color = self.icon_color[1]

        return super().on_cursor_enter(*args)

    def on_cursor_leave(self, *args):
        if len(self.icon_source) == 2 and '' not in self.icon_source:
            self.source = self.icon_source[0]

        if not isinstance(self.icon_color[0], (int, float)):
            self.color = self.icon_color[0]

        return super().on_cursor_leave(*args)

    def on_touch_down(self, touch):
        if not self.collide_point(*touch.pos):
            return False

        # touch.grab(self)
        self.ripple_show(touch)
        return super().on_touch_down(touch)

    def on_touch_up(self, touch):
        if not self.collide_point(*touch.pos):
            return False

        self.ripple_fade()
        return super().on_touch_up(touch)

class ToggleButtonIcon(ToggleButtonBehavior, ButtonIcon):
    pass


from flask import Flask, render_template, request, Markup
import learn

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    # return render_template("index.html")
    countries, country_name, date, time = learn.get_country()
    da, ti, world_total_confirmed_cases, world_total_confirmed_cases_new, world_total_death, world_total_death_new, world_total_recovered, world_recovered_new, world_total_active, world_total_serious, country_cases, country_total_cases, country_total_death, country_total_recovered, country_recovered_new, country_total_active, country_cases_new, country_death_new, country_total_serious, world_total_predicted_cases, country_total_predicted_cases, fig = learn.covid_processing()
    # x = pd.DataFrame(np.random.randn(20, 5))

    detected_country = country_name
    if request.args.get("select_country") in countries:
        country_name = request.args.get("select_country")
        da, ti, world_total_confirmed_cases, world_total_confirmed_cases_new, world_total_death, world_total_death_new, world_total_recovered, world_recovered_new, world_total_active, world_total_serious, country_cases, country_total_cases, country_total_death, country_total_recovered, country_recovered_new, country_total_active, country_cases_new, country_death_new, country_total_serious, world_total_predicted_cases, country_total_predicted_cases, fig = learn.covid_processing(
            str(country_name))

    countries.remove(country_name)

    iframe = Markup(fig)

    return render_template("index.html", detected_country=detected_country, country=countries,
                           country_name=country_name, date=date, time=time, da=da, ti=ti,
                           world_total_confirmed_cases=world_total_confirmed_cases,
                           world_total_confirmed_cases_new=world_total_confirmed_cases_new,
                           world_total_death=world_total_death, world_total_death_new=world_total_death_new,
                           world_total_recovered=world_total_recovered, world_recovered_new=world_recovered_new,
                           world_total_active=world_total_active,
                           world_total_serious=world_total_serious,
                           country_cases=country_cases, country_total_cases=country_total_cases,
                           country_total_death=country_total_death,
                           country_total_recovered=country_total_recovered, country_recovered_new=country_recovered_new,
                           country_total_active=country_total_active,
                           country_cases_new=country_cases_new, country_death_new=country_death_new,
                           country_total_serious=country_total_serious,
                           world_total_predicted_cases=world_total_predicted_cases,
                           country_total_predicted_cases=country_total_predicted_cases, fig=iframe)
    '''return f"{simplejson.dumps(country_cases)},Name = {country_name}, Total Case = {country_total_cases}, New Case = {country_cases_new}, Total Death = {country_total_death}, New Death = {country_death_new}, Total Active = {country_total_active}"'''

if __name__ == "__main__":
    from waitress import serve

    serve(app)
    app.run(debug=True)


from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from airflow.contrib.hooks.aws_hook import AwsHook

class StageToRedshiftOperator(BaseOperator):
    ui_color = '#358140'

    copy_template = """
             COPY {}
             FROM '{}'
             ACCESS_KEY_ID '{}'
             SECRET_ACCESS_KEY '{}'
             REGION '{}'
             {};
            """
    csv_format = "CSV IGNOREHEADER 1"
    json_format = "FORMAT AS JSON 'auto'"

    @apply_defaults
    def __init__(self,
                 aws_credentials_id='',
                 redshift_conn_id='',
                 s3_bucket='',
                 s3_key='',
                 region='',
                 table='',
                 file_format='',
                 *args, **kwargs):

        super(StageToRedshiftOperator, self).__init__(*args, **kwargs)
        self.aws_credentials_id = aws_credentials_id
        self.redshift_conn_id = redshift_conn_id
        self.s3_bucket = s3_bucket
        self.s3_key = s3_key
        self.region = region
        self.file_format = file_format
        self.table = table

    def execute(self, context):
        aws_hook = AwsHook(self.aws_credentials_id)
        credentials = aws_hook.get_credentials()
        s3_path = "s3://{}/{}".format(self.s3_bucket, self.s3_key)
        self.log.info(f"Staging file location {s3_path}")
        redshift = PostgresHook(postgres_conn_id = self.redshift_conn_id)
        if self.file_format == 'CSV':
            s3_copy_query = self.copy_template.format(self.table, s3_path, credentials.access_key, credentials.secret_key, self.region, self.csv_format)
        if self.file_format == 'JSON':
            s3_copy_query = self.copy_template.format(self.table, s3_path, credentials.access_key, credentials.secret_key, self.region, self.json_format)
        redshift.run(s3_copy_query)
        self.log.info(f"Table {self.table} staged!")


from allauth.account.adapter import DefaultAccountAdapter
from ..models import ResearchInterestUser, AgeRangeUser, DeliveryModeUser
from rest_framework.response import Response
from rest_framework import status
import os
import requests

# Used for overriding built in user model
class CustomAccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=False):

        user = super().save_user(request, user, form, commit)
        data = form.cleaned_data

        user.first_name = data.get('first_name')
        user.last_name = data.get('last_name')
        user.role = data.get('role')
        user.displayRole = data.get('displayRole')
        user.affiliation = data.get('affiliation')
        user.location = data.get('location')
        user.phone = data.get('phone')
        user.website = data.get('website')
        user.researchDescription = data.get('researchDescription')
        user.roles = data.get('roles')
        user.researchNeeds = data.get('researchNeeds')
        user.evaluationNeeds = data.get('evaluationNeeds')
        user.locatedAtCornell = data.get('locatedAtCornell')
        user.locatedAtCCE = data.get('locatedAtCCE')
        user.profile_picture = data.get('profile_picture')

        user.save()

        for interest in data.get('researchInterests'):
            ResearchInterestUser.objects.create(user=user, researchInterest=interest)

        for age in data.get('ageRanges'):
            AgeRangeUser.objects.create(user=user, ageRange=age)

        for mode in data.get('deliveryModes'):
            DeliveryModeUser.objects.create(user=user, deliveryMode=mode)

        return user


import unittest

import numpy as np
import tensorflow as tf
import nnutil2 as nnu

from tensorflow.python.keras import testing_utils

class LayersSegment(tf.test.TestCase):
    # def test_layers_segment_1(self):
    #     dense = tf.layers.Dense(units=4)
    #     testing_utils.layer_test(
    #         nnu.layers.Segment,
    #         kwargs={"layers": [dense]},
    #         input_shape=(2, 3))

    def test_layers_segment_2(self):
        dense = tf.keras.layers.Dense(units=4)
        segment = nnu.layers.Segment(layers=[dense])
        x = tf.random.normal(shape=(2, 3), dtype=tf.float32)
        y = segment(x)

        self.assertEqual(len(segment.layers), 1)
        self.assertEqual(len(segment.trainable_weights), 2)
        self.assertEqual(len(segment.states), 3)

        # self.assertEqual(segment.inputs[0].shape, (2, 3))

    def test_layers_segment_nested(self):
        identity0 = nnu.layers.Identity()
        identity1 = nnu.layers.Identity()

        segment = nnu.layers.Segment(layers=[identity0, identity1])
        x = {
            'a': tf.random.normal(shape=(2, 3), dtype=tf.float32),
            'b': tf.random.normal(shape=(4,), dtype=tf.float32),
        }

        y = segment(x)

        self.assertEqual(len(segment.layers), 2)
        self.assertEqual(len(segment.trainable_weights), 0)

        self.assertEqual(x['a'].shape, y['a'].shape)
        self.assertEqual(x['b'].shape, y['b'].shape)

if __name__ == '__main__':
    tf.test.main()


# -*- coding: utf-8 -*-
"""
Created on Thu Apr  7 17:52:09 2016

@author: zhengzhang
"""
import util

def knn(p, data, k):
    # Calculating the distances b/w p & every pt. in data
    distances = {}
    for d in data:
        if d.distance(p) not in distances.keys():
            distances[ d.distance(p) ] = [ d ]
        else:
            distances[ d.distance(p) ].append(d)

    # Sorting the k nearest neighbours
    result = []
    for key in sorted(distances.keys()):
        result.extend( distances[key] )

    k_nearest_neighbours = result[:k]

    # Assigning a label to the new point based on the k neighbours
    label_votes = { l:0 for l in util.LABELS }
    for x in k_nearest_neighbours:
        label_votes[ x.getLabel() ] += 1
    max_label = sorted(label_votes, key = label_votes.get, reverse = True)[0]

    p.setLabel(max_label)


from turtle import Turtle

class Ball(Turtle):
    def __init__(self):
        super(Ball, self).__init__()
        self.shape("circle")
        self.penup()
        self.color("white")
        self.x_cor=0
        self.y_cor=0
        self.mov_xunit = 10
        self.mov_yunit = 10
        self.move_speed = 0.1

    def move(self):
        x_cor = self.xcor() + self.mov_xunit
        y_cor = self.ycor() + self.mov_yunit
        self.goto(x_cor, y_cor)

    def bounce_y(self):
        self.mov_yunit *= -1
        self.move_speed *= 0.9
        self.move()

    def bounce_x(self):
        self.mov_xunit *= -1
        self.move_speed *= 0.9
        self.move()

    def reset(self):
        self.goto(0, 0)
        self.bounce_x()
        self.move_speed = 0.1


import re,operator
import sys
from collections import defaultdict
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *

reverse_sorted_freq=[]

#filename=sys.argv[1]
def trigram(filename):
	f = open(filename)
	#unigram=re.sub('http[s]? : //(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(:%[0-9a-fA-F][0-9a-fA-F]))+',' ', f.read())
	unigram=re.sub('(&gt)+|(&amp)+|(&lt)+|(&nbsp)+',' ',f.read())
	unigram=re.sub('(\.)+','.', unigram)
	#unigram=re.findall('[A-Z]?[a-z]+|[A-Z]+|[0-9]+|[0-9]+th|[0-9]+st|[0-9]+rd|[0-9]+nd|[a-z]+-[a-z]+|Dr\.|Mr\.|Mrs\.|\'s|\'d|,|&|\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.-]+@[\w\.\w]|http[s]?://(?:[a-zA-Z]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+|&',unigram)
	unigram=re.findall('[A-Z]?[a-z]+|[A-Z]+|[0-9]+|[0-9]+th|[0-9]+st|[0-9]+rd|[0-9]+nd|[a-z]+-[a-z]+|Dr\.|Mr\.|Mrs\.|\'s|\'d|\.|,|\?|&|\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.-]+@[\w\.\w]|http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+|&',unigram)
	unigram_dict={}
	for k in unigram:
		if k in unigram_dict:
			unigram_dict[k]+=1
		else:
			unigram_dict[k]=1
	trigram=[]
	i=0
	j=1
	length=len(unigram)-1
	k=2
	while k<=length:
		s=unigram[i]+' '+unigram[j]+' '+unigram[k]
		trigram.append(s)
		i=i+1
		j=j+1
		k=k+1

	trigram_dict={}
	for k in trigram:
		if k in trigram_dict:
			trigram_dict[k]+=1
		else:
			trigram_dict[k]=1

	tg=[]
	V=sum(trigram_dict.values())
	reverse_sorted_trigrams=sorted(trigram_dict.items(),reverse=True,key=operator.itemgetter(1))
	return V,reverse_sorted_trigrams,trigram_dict

def unigram(filename):
	f = open(filename)
	unigram_dict=defaultdict(int)
	#unigram=re.sub('http[s]? : //(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(:%[0-9a-fA-F][0-9a-fA-F]))+',' ', f.read())
	unigram=re.sub('(&gt)+|(&amp)+|(&lt)+|(&nbsp)+',' ',f.read())
	unigram=re.sub('(\.)+','.', unigram)
	tokens=re.findall('[A-Z]?[a-z]+|[A-Z]+|[0-9]+|[0-9]+th|[0-9]+st|[0-9]+rd|[0-9]+nd|[a-z]+-[a-z]+|Dr\.|Mr\.|Mrs\.|\'s|\'d|\?|\.|,|&|\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.-]+@[\w\.\w]|http[s]?://(?:[a-zA-Z]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+|&',unigram)
	#tokens=re.findall('[A-Z]?[a-z]+|[A-Z]+|[0-9]+|[0-9]+th|[0-9]+st|[0-9]+rd|[0-9]+nd|[a-z]+-[a-z]+|Dr\.|Mr\.|Mrs\.|\'s|\'d|,|&|2}|[\w\.-]+@[\w\.\w]|http[s]?://(?:[a-zA-Z]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+|&',f.read())
	unigram_dict={}
	for k in tokens:
		try:
			unigram_dict[k]+=1
		except:
			unigram_dict[k]=1
	V=len(tokens)
	reverse_sorted_unigrams=sorted(unigram_dict.items(),reverse=True,key=operator.itemgetter(1))
	return V,reverse_sorted_unigrams,unigram_dict

def bigram(filename):
	f = open(filename)
	unigram_dict=defaultdict(int)
	unigram_dict={}
	#unigram=re.sub('http[s]? : //(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(:%[0-9a-fA-F][0-9a-fA-F]))+',' ', f.read())
	unigram=re.sub('(&gt)+|(&amp)+|(&lt)+|(&nbsp)+',' ',f.read())
	unigram=re.sub('(\.)+','.', unigram)
	unigram=re.findall('[A-Z]?[a-z]+|[A-Z]+|[0-9]+|[0-9]+th|[0-9]+st|[0-9]+rd|[0-9]+nd|[a-z]+-[a-z]+|Dr\.|Mr\.|Mrs\.|\'s|\'d|\.|\?|,|&|\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.-]+@[\w\.\w]|http[s]?://(?:[a-zA-Z]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+|&',unigram)

	for k in unigram:
		try:
			unigram_dict[k]+=1
		except:
			unigram_dict[k]=1
	i=0
	j=1
	#V=len(tokens)
	length=len(unigram)-1
	bigram=[]
	while j<=length:
		s=unigram[i]+' '+unigram[j]
		bigram.append(s)
		i=i+1
		j=j+1

	bigram_dict={}
	for k in bigram:
		if k in bigram_dict:
			bigram_dict[k]+=1
		else:
			bigram_dict[k]=1

	bg=[]
	V=sum(bigram_dict.values())
	reverse_sorted_bigrams=sorted(bigram_dict.items(),reverse=True,key=operator.itemgetter(1))
	return V,reverse_sorted_bigrams,bigram_dict

def KN_bigram(bigramdict,unigramdict):
	prob_bidict={}
	d=.05
	type_first_dict=defaultdict(int)
	type_second_dict=defaultdict(int)

	count_first_bidict=defaultdict(int)
	count_second_bidict=defaultdict(int)

	for k,v in bigramdict.items():
		first=k.strip().split()[0]
		second=k.strip().split()[1]

		temp=bigramdict[k]
		count_first_bidict[first]+=temp
		count_second_bidict[second]+=temp

		type_first_dict[first]+=1
		type_second_dict[second]+=1

	for k,v in bigramdict.items():
		first=k.strip().split()[0]
		second=k.strip().split()[1]
		try:
			prob_bidict[k]=((max(bigramdict[k]-d,0)/float(unigramdict[first]))+(d/float(unigramdict[first])*type_first_dict[first]*(type_second_dict[second]/float(len(bigramdict)))))
		except :
			prob_bidict[k]=(d/float(unigramdict[first]))*type_first_dict[first]*(type_second_dict[second]/float(len(bigramdict)))
		prob_bidict[k]*=unigramdict[first]
	return prob_bidict

def KN_trigram(trigramdict,bigramdict):
	prob_bidict={}
	d=.5
	type_first_dict=defaultdict(int)
	type_second_dict=defaultdict(int)
	type_third_dict=defaultdict(int)

	count_firstsecond_dict=defaultdict(int)
	count_second_dict=defaultdict(int)
	count_third_dict=defaultdict(int)

	for k,v in trigramdict.items():
		first=k.strip().split()[0]
		second=k.strip().split()[1]
		third=k.strip().split()[2]
		first=first+" "+second

		#print k,v
		temp=trigramdict[k]
		count_firstsecond_dict[first]+=temp
		count_second_dict[second]+=temp
		count_third_dict[third]+=temp

		type_first_dict[first]+=1
		type_second_dict[second]+=1
		type_second_dict[third]+=1

	for k,v in trigramdict.items():
		first=k.strip().split()[0]
		second=k.strip().split()[1]
		third=k.strip().split()[2]
		first=first+" "+second

		try:
			prob_bidict[k]=((max(trigramdict[k]-d,0)/float(bigramdict[first]))+(d/float(bigramdict[first])*type_first_dict[first]*(type_third_dict[third]/float(len(trigramdict)))))
		except :
			prob_bidict[k]=(d/float(bigramdict[first]))*type_first_dict[first]*(type_third_dict[third]/float(len(trigramdict)))
		prob_bidict[k]*=bigramdict[first]
	return prob_bidict

#print unigram_dict
def bigram_KN(filename):
	V,reverse_sorted_bigrams,bigram_dict=bigram(filename)
	Vuni,reverse_sorted_unigrams,unigram_dict=unigram(filename)
	prob_bidict=KN_bigram(bigram_dict,unigram_dict)
	reverse_sorted__discounted_bigrams=sorted(prob_bidict.items(),reverse=True,key=operator.itemgetter(1))
	p1 = Bar(x = list(zip(*reverse_sorted__discounted_bigrams))[0], y= list(zip(*reverse_sorted__discounted_bigrams))[1], name="KN_KNB")
	p2 = Bar(x = list(zip(*reverse_sorted_bigrams))[0], y= list(zip(*reverse_sorted_bigrams))[1], name="Unsmoothed_KNB")
	return p1,p2
	#plot([p1, p2])#, p3, p4,p5])

def trigram_KN(filename):
	V,reverse_sorted_trigrams,trigram_dict=trigram(filename)
	V,reverse_sorted_bigrams,bigram_dict=bigram(filename)

	prob_bidict=KN_trigram(trigram_dict,bigram_dict)
	reverse_sorted__discounted_trigrams=sorted(prob_bidict.items(),reverse=True,key=operator.itemgetter(1))
	p1 = Bar(x = list(zip(*reverse_sorted__discounted_trigrams))[0], y= list(zip(*reverse_sorted__discounted_trigrams))[1], name="KN_KNT")
	p2 = Bar(x = list(zip(*reverse_sorted_trigrams))[0], y= list(zip(*reverse_sorted_trigrams))[1], name="Unsmoothed_KNT")
	return p1,p2
	#plot([p1, p2],filename="test")#, p3, p4,p5])

# bigram_KN()
# trigram_KN()


# いまにゅ   Udemy streamlit超入門
#    2022.10.15   14:13

import streamlit as st
import numpy  as np
import pandas as pd
from   PIL import Image

st.title('Streamlit 超入門 ')

"""
# Udemy Streamlit

##
## st.image(img)

```python
import streamlit as st
import numpy  as np
import pandas as pd
from   PIL import Image
```
"""

st.write('Interactive Widgets')

if st.checkbox('Show Image'):
    img = Image.open('img/obaba.jpg')
    st.image(img , caption='オババ',use_column_width=True)

option = st.selectbox(
    'あなたが好きな数字を教えて下さい',
    list(range(1,11))
)
'あなたの好きな数字は',option,'です'

### --------------------------------
### sidebar
text = st.sidebar.text_input('あなたの趣味を教えて下さい')
condition = st.sidebar.slider('あなたの今の気分は?',0,10,5)
### --------------------------------/////

'あなたの趣味は',text,'です'
'コンディション:',condition
#---------------------------------- with sidebar


import numpy as np

import laygo2.object.template
import laygo2.object.physical
import laygo2.object.database

# Should be `import laygo2_tech as tech` for actual use.
# import laygo2_tech as tech
import laygo2_tech_quick_start as tech

# tech_params = tech.tech_params
# templates = tech_params["templates"]
# grids = tech_params["grids"]

# Template functions for primitive devices
def _mos_update_params(params):
    """Make a complete parameter table for mos"""
    us = np.array([30, 100])  # unit transistor size
    params["nf"]              = params.get("nf",              1)     # number of fingers
    params["nfdmyl"]          = params.get("nfdmyl",          0)     # number of left-dummy fingers
    params["nfdmyr"]          = params.get("nfdmyr",          0)     # number of right-dummy fingers
    params["trackswap"]       = params.get("trackswap",       False) # s/d track swap
    params["tie"]             = params.get("tie",             None)  # tie to power rail
    params["bndl"]            = params.get("bndl",            True)  # create local left boundary
    params["bndr"]            = params.get("bndr",            True)  # create local right boundary
    params["gbndl"]           = params.get("gbndl",           True)  # create global left boundary
    params["gbndr"]           = params.get("gbndr",           True)  # create global right boundary
    params["unit_size_core"]  = params.get("unit_size_core",  us)    # core unit size
    params["unit_size_dmy"]   = params.get("unit_size_dmy",   us)    # dummy unit size
    params["unit_size_bndl"]  = params.get("unit_size_bndl",  us)    # left local boundary size
    params["unit_size_bndr"]  = params.get("unit_size_bndr",  us)    # right local boundary size
    params["unit_size_gbndl"] = params.get("unit_size_gbndl", us)    # left global boundary size
    params["unit_size_gbndr"] = params.get("unit_size_gbndr", us)    # right global boundary size
    return params

def mos_bbox_func(params):
    """Computes x and y coordinate values from params."""
    params = _mos_update_params(params)
    unit_size = params["unit_size_core"]
    xy0 = [0, 0]
    xy1 = [unit_size[0] * params["nf"], unit_size[1]]
    xy = np.array([xy0, xy1])
    # Increment bbox based on boundary / dummy parameters.
    if params["gbndl"]:
        xy[1, 0] += params["unit_size_gbndl"][0]
    if params["bndl"]:
        xy[1, 0] += params["unit_size_bndl"][0]
    if params["nfdmyl"] > 0:
        xy[1, 0] += params["unit_size_dmy"][0] * round(params["nfdmyl"])
    if params["nfdmyr"] > 0:
        xy[1, 0] += params["unit_size_dmy"][0] * round(params["nfdmyr"])
    if params["bndr"]:
        xy[1, 0] += params["unit_size_bndr"][0]
    if params["gbndr"]:
        xy[1, 0] += params["unit_size_gbndr"][0]
    return xy

def mos_pins_func(devtype, params):
    """Generate a pin dictionary from params."""
    # Compute parameters
    params = _mos_update_params(params)
    nf = params["nf"]
    nfdl = params["nfdmyl"]
    nfdr = params["nfdmyr"]
    blx = 30 if params["bndl"] == True else 0
    brx = 30 if params["bndr"] == True else 0
    gblx = 30 if params["gbndl"] == True else 0
    gbrx = 30 if params["gbndr"] == True else 0
    us = np.array([30, 100])  # unit transistor size

    # Create a pin dictionary
    pins = dict()
    # metal2 - gate
    pxy = np.array([[gblx + blx + us[0] * nfdl + 12, 75], [gblx + blx + us[0] * (nfdl + nf) - 12, us[1] - 15]])
    pins["G"] = laygo2.object.Pin(name="G", xy=pxy, layer=["metal2", "drawing"], netname="G")
    # metal2 - sourcedrain
    srx = -1 * (nf % 2) * us[0]
    if params["trackswap"] == False:
        sy = 25
        dy = 45
    else:
        sy = 45
        dy = 25
    sdx = gblx + blx + nfdl * us[0]
    pxy = np.array([[sdx - 7, sy], [sdx + us[0] * nf + srx + 7, sy + 10]])
    pins["S"] = laygo2.object.Pin(name="S", xy=pxy, layer=["metal2", "drawing"], netname="S")
    drx = -1 * ((nf + 1) % 2) * us[0]
    pxy = np.array([[sdx + us[0] - 7, dy], [sdx + us[0] * nf + drx + 7, dy + 10]])
    pins["D"] = laygo2.object.Pin(name="D", xy=pxy, layer=["metal2", "drawing"], netname="D")
    # metal2 - rail
    pxy = np.array([[0, -5], [gblx + blx + us[0] * (nfdl + nf + nfdr) + brx + gbrx, 5]])
    pins["RAIL"] = laygo2.object.Pin(name="RAIL", xy=pxy, layer=["metal2", "drawing"], netname="RAIL")
    return pins

def pmos_pins_func(params):
    return mos_pins_func(devtype="pmos", params=params)

def nmos_pins_func(params):
    return mos_pins_func(devtype="nmos", params=params)

def mos_generate_func(devtype, name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    """Generates an instance from the input parameters."""
    # Compute parameters
    params = _mos_update_params(params)
    nf = params["nf"] if "nf" in params.keys() else 1
    nfdl = params["nfdmyl"]
    nfdr = params["nfdmyr"]
    blx = 30 if params["bndl"] == True else 0
    brx = 30 if params["bndr"] == True else 0
    gblx = 30 if params["gbndl"] == True else 0
    gbrx = 30 if params["gbndr"] == True else 0
    us = np.array([30, 100])  # unit transistor size

    # Create the base mosfet structure.
    nelements = dict()
    # prboundary
    rxy = np.array([[0, 0], [gblx + blx + us[0] * (nfdl + nf + nfdr) + brx + gbrx, us[1]]])
    nelements["PRBND0"] = laygo2.object.Rect(xy=rxy, layer=["prBoundary", "drawing"], name="PRBND0")
    # well
    if devtype == "pmos":
        rxy = np.array([[-20, 0], [gblx + blx + us[0] * (nfdl + nf + nfdr) + brx + gbrx + 20, us[1]]])
        nelements["NW0"] = laygo2.object.Rect(xy=rxy, layer=["nwell", "drawing"], name="NW0")
    # implant
    rxy = np.array([[0, 5], [gblx + blx + us[0] * (nfdl + nf + nfdr) + brx + gbrx, us[1] - 5]])
    if devtype == "nmos":
        nelements["NIMPL0"] = laygo2.object.Rect(xy=rxy, layer=["nimplant", "drawing"], name="NIMPL0")
    else:
        nelements["PIMPL0"] = laygo2.object.Rect(xy=rxy, layer=["pimplant", "drawing"], name="PIMPL0")
    # diffusion
    rxy = np.array([[gblx + blx - 5, 15], [gblx + blx + us[0] * (nfdl + nf + nfdr) + 5, us[1] - 35]])
    nelements["DIFF0"] = laygo2.object.Rect(xy=rxy, layer=["diffusion", "drawing"], name="DIFF0")
    # poly
    for i in range(nf + nfdl + nfdr):
        rxy = np.array([[gblx + blx + us[0] * i + 12, 10], [gblx + blx + us[0] * (i + 1) - 12, us[1] - 10]])
        nelements["POLY" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["poly", "drawing"], name="POLY" + str(i))
    # metal1 - gate
    rxy = np.array([[gblx + blx + us[0] * nfdl + 12, 75], [gblx + blx + us[0] * (nfdl + nf) - 12, us[1] - 15]])
    nelements["M1G0"] = laygo2.object.Rect(xy=rxy, layer=["metal1", "drawing"], name="M1G0")
    # metal1 - sourcedrain
    for i in range(nf + nfdl + nfdr + 1):
        rxy = np.array([[gblx + blx + us[0] * i - 7, 13], [gblx + blx + us[0] * i + 7, us[1] - 33]])
        nelements["M1SD" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["metal1", "drawing"], name="M1SD" + str(i))
    # via1 - gate
    gx = gblx + blx + nfdl * us[0]
    for i in range(nf):
        rxy = np.array([[gx + us[0] * i - 5 + 15, 75], [gx + us[0] * i + 5 + 15, us[1] - 15]])
        nelements["VG0" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="VG0" + str(i))
    # via1 - sourcedrain
    if params["trackswap"] == False:
        sy = 25
        dy = 45
    else:
        sy = 45
        dy = 25
    sdx = gblx + blx + nfdl * us[0]
    for i in range(nf + 1):
        if i % 2 == 0:
            rxy = np.array([[sdx + us[0] * i - 5, sy], [sdx + us[0] * i + 5, sy + 10]])
            nelements["VS0" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="VS0" + str(i))
        else:
            rxy = np.array([[sdx + us[0] * i - 5, dy], [sdx + us[0] * i + 5, dy + 10]])
            nelements["VD0" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="VD0" + str(i))
    # metal1/via1 - tie
    sdx = gblx + blx + nfdl * us[0]
    for i in range(nf + 1):
        tie = False
        if (i % 2 == 0) and (params["tie"] is not None):
            if params["tie"] == "S":
                tie = True
        if (i % 2 == 1) and (params["tie"] is not None):
            if params["tie"] == "D":
                tie = True
        if tie:
            rxy = np.array([[sdx + us[0] * i - 7, -5], [sdx + us[0] * i + 7, 23]])
            nelements["M1TIE" + str(i)] = laygo2.object.Rect(
                xy=rxy, layer=["metal1", "drawing"], name="M1TIE" + str(i)
            )
            rxy = np.array([[sdx + us[0] * i - 5, -5], [sdx + us[0] * i + 5, 5]])
            nelements["V1TIE" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="V1TIE" + str(i))
    # metal1 - dummy (gate)
    if nfdl > 0:
        rxy = np.array([[gblx + blx + 12, 75], [gblx + blx + us[0] * nfdl - 12, us[1] - 15]])
        nelements["M1DMYGL"] = laygo2.object.Rect(xy=rxy, layer=["metal1", "drawing"], name="M1DMYGL")
        #gx = gblx + blx
        #for i in range(nfdl):
        #    rxy = np.array([[gx + us[0] * i - 5 + 15, 75], [gx + us[0] * i + 5 + 15, us[1] - 15]])
        #    nelements["VDMYGL" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="VDMYGL" + str(i))
    if nfdr > 0:
        rxy = np.array([[gblx + blx + us[0] * (nfdl + nf) + 12, 75], [gblx + blx + us[0] * (nfdl + nf + nfdr) - 12, us[1] - 15]])
        nelements["M1DMYGR"] = laygo2.object.Rect(xy=rxy, layer=["metal1", "drawing"], name="M1DMYGR")
        #gx = gblx + blx + (nfdl + nf) * us[0]
        #for i in range(nfdr):
        #    rxy = np.array([[gx + us[0] * i - 5 + 15, 75], [gx + us[0] * i + 5 + 15, us[1] - 15]])
        #    nelements["VDMYGR" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="VDMYGR" + str(i))
    # metal1/via1 - dummy (source/drain)
    sdx = gblx + blx
    for i in range(nfdl + 1 - 1):
        if i == 0:
            rxy = np.array([[sdx + us[0] * i - 7, -5], [sdx + us[0] * i + 7, 23]])
        else:
            rxy = np.array([[sdx + us[0] * i - 7, -5], [sdx + us[0] * i + 7, us[1] - 15]])
        nelements["M1DML" + str(i)] = laygo2.object.Rect(
            xy=rxy, layer=["metal1", "drawing"], name="M1DML" + str(i)
        )
        rxy = np.array([[sdx + us[0] * i - 5, -5], [sdx + us[0] * i + 5, 5]])
        nelements["V1DML" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="V1DML" + str(i))
    sdx = gblx + blx + (nfdl + nf) * us[0]
    for i in range(1, nfdr + 1):
        if i == nfdr:
            rxy = np.array([[sdx + us[0] * i - 7, -5], [sdx + us[0] * i + 7, 23]])
        else:
            rxy = np.array([[sdx + us[0] * i - 7, -5], [sdx + us[0] * i + 7, us[1] - 15]])
        nelements["M1DMR" + str(i)] = laygo2.object.Rect(
            xy=rxy, layer=["metal1", "drawing"], name="M1DMR" + str(i)
        )
        rxy = np.array([[sdx + us[0] * i - 5, -5], [sdx + us[0] * i + 5, 5]])
        nelements["V1DMR" + str(i)] = laygo2.object.Rect(xy=rxy, layer=["via1", "drawing"], name="V1DMR" + str(i))

    # metal2 - gate
    sdx = gblx + blx + nfdl * us[0]
    rxy = np.array([[gblx + blx + us[0] * nfdl + 12, 75], [gblx + blx + us[0] * (nfdl + nf) - 12, us[1] - 15]])
    nelements["M2G0"] = laygo2.object.Rect(xy=rxy, layer=["metal2", "drawing"], name="M2G0")
    # metal2 - sourcedrain
    srx = -1 * (nf % 2) * us[0]
    rxy = np.array([[sdx - 7, sy], [sdx + us[0] * nf + srx + 7, sy + 10]])
    nelements["M2S0"] = laygo2.object.Rect(xy=rxy, layer=["metal2", "drawing"], name="M2S0")
    drx = -1 * ((nf + 1) % 2) * us[0]
    rxy = np.array([[sdx + us[0] - 7, dy], [sdx + us[0] * nf + drx + 7, dy + 10]])
    nelements["M2D0"] = laygo2.object.Rect(xy=rxy, layer=["metal2", "drawing"], name="M2D0")
    # metal2 - rail
    rxy = np.array([[0, -5], [gblx + blx + us[0] * (nfdl + nf + nfdr) + brx + gbrx, 5]])
    nelements["M2RAIL0"] = laygo2.object.Rect(xy=rxy, layer=["metal2", "drawing"], name="M2RAIL0")

    # Create pins
    pins = mos_pins_func(devtype=devtype, params=params)
    nelements.update(pins)  # Add physical pin structures to the virtual object.

    # inst_unit_size = unit_size * np.array([nf, 1])
    # Unit size
    inst_xy = mos_bbox_func(params=params)
    inst_unit_size = [inst_xy[1, 0] - inst_xy[0, 0], inst_xy[1, 1] - inst_xy[0, 1]]

    # Generate and return the final instance
    inst = laygo2.object.VirtualInstance(
        name=name,
        xy=np.array([0, 0]),
        libname="mylib",
        cellname="myvcell_" + devtype,
        native_elements=nelements,
        shape=shape,
        pitch=pitch,
        transform=transform,
        unit_size=inst_unit_size,
        pins=pins,
    )
    return inst

def pmos_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return mos_generate_func(devtype="pmos", name=name, shape=shape, pitch=pitch, transform=transform, params=params)

def nmos_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return mos_generate_func(devtype="nmos", name=name, shape=shape, pitch=pitch, transform=transform, params=params)

# Spacers
def space_bbox_func(params):
    """Computes x and y coordinate values from params."""
    us = np.array([30, 100])  # unit space
    xy0 = [0, 0]
    xy1 = [us[0] * params["nf"], us[1]]
    xy = np.array([xy0, xy1])
    return xy

def space_pins_func(params):
    return None

def space_generate_func(devtype="pmos", name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    # Compute parameters
    nf = params["nf"] if "nf" in params.keys() else 1
    us = np.array([30, 100])  # unit space

    # Create the base mosfet structure.
    nelements = dict()
    # prboundary
    rxy = np.array([[0, 0], [us[0] * nf, us[1]]])
    nelements["PRBND0"] = laygo2.object.Rect(xy=rxy, layer=["prBoundary", "drawing"], name="PRBND0")
    # well
    rxy = np.array([[-20, 0], [us[0] * nf + 20, us[1]]])
    nelements["NW0"] = laygo2.object.Rect(xy=rxy, layer=["nwell", "drawing"], name="NW0")
    # implant
    rxy = np.array([[0, 5], [us[0] * nf, us[1] - 5]])
    if devtype == "nmos":
        nelements["NIMPL0"] = laygo2.object.Rect(xy=rxy, layer=["nimplant", "drawing"], name="NIMPL0")
    else:
        nelements["PIMPL0"] = laygo2.object.Rect(xy=rxy, layer=["pimplant", "drawing"], name="PIMPL0")
    # metal2 - rail
    rxy = np.array([[0, -5], [us[0] * nf, 5]])
    nelements["M2RAIL0"] = laygo2.object.Rect(xy=rxy, layer=["metal2", "drawing"], name="M2RAIL0")

    # Unit size
    inst_xy = space_bbox_func(params=params)
    inst_unit_size = [inst_xy[1, 0] - inst_xy[0, 0], inst_xy[1, 1] - inst_xy[0, 1]]

    # Generate and return the final instance
    inst = laygo2.object.VirtualInstance(
        name=name,
        xy=np.array([0, 0]),
        libname="mylib",
        cellname="myvcell_space_" + devtype,
        native_elements=nelements,
        shape=shape,
        pitch=pitch,
        transform=transform,
        unit_size=inst_unit_size,
        pins=None,
    )
    return inst

def pspace_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return space_generate_func(devtype="pmos", name=name, shape=shape, pitch=pitch, transform=transform, params=params)

def nspace_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return space_generate_func(devtype="nmos", name=name, shape=shape, pitch=pitch, transform=transform, params=params)

# Vias
def via_generate_func(devtype, name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    """Generates an instance from the input parameters."""
    # Create the via structure.
    nelements = dict()
    if devtype.startswith("via_r12"):
        layer = ["via1", "drawing"]
        xy = [[-5, -5], [5, 5]]
    if devtype.startswith("via_r23"):
        layer = ["via2", "drawing"]
        xy = [[-5, -5], [5, 5]]
    if devtype.startswith("via_r34"):
        layer = ["via3", "drawing"]
        xy = [[-5, -5], [5, 5]]
    nelements["VIA0"] = laygo2.object.Rect(xy=xy, layer=layer, name="VIA0")

    # Generate and return the final instance
    inst = laygo2.object.VirtualInstance(
        name=name,
        xy=np.array([0, 0]),
        libname="mylib",
        cellname="myvcell_devtype",
        native_elements=nelements,
        shape=shape,
        pitch=pitch,
        transform=transform,
        unit_size=np.array([0, 0]),
        pins=None,
    )
    return inst

def via_r12_default_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return via_generate_func(
        devtype="via_r12_default", name=name, shape=shape, pitch=pitch, transform=transform, params=params
    )

def via_r23_default_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return via_generate_func(
        devtype="via_r23_default", name=name, shape=shape, pitch=pitch, transform=transform, params=params
    )

def via_r34_default_generate_func(name=None, shape=None, pitch=np.array([0, 0]), transform="R0", params=None):
    return via_generate_func(
        devtype="via_r34_default", name=name, shape=shape, pitch=pitch, transform=transform, params=params
    )

# Template library
def load_templates():
    """Load template to a template library object"""
    tlib = laygo2.object.database.TemplateLibrary(name="advtech_templates")
    # Transistors
    # Transistor layouts are created in laygo and stored as a virtual instance.
    tnmos = laygo2.object.template.UserDefinedTemplate(
        name="nmos", bbox_func=mos_bbox_func, pins_func=nmos_pins_func, generate_func=nmos_generate_func
    )
    tpmos = laygo2.object.template.UserDefinedTemplate(
        name="pmos", bbox_func=mos_bbox_func, pins_func=pmos_pins_func, generate_func=pmos_generate_func
    )
    tlib.append([tpmos, tnmos])
    # Spacers
    tnspace = laygo2.object.template.UserDefinedTemplate(
        name="nspace", bbox_func=space_bbox_func, pins_func=space_pins_func, generate_func=nspace_generate_func
    )
    tpspace = laygo2.object.template.UserDefinedTemplate(
        name="pspace", bbox_func=space_bbox_func, pins_func=space_pins_func, generate_func=pspace_generate_func
    )
    tlib.append([tpspace, tnspace])
    # Vias
    # Via layouts are created in laygo and stored as a virtual instance.
    tvia_r12_default = laygo2.object.template.UserDefinedTemplate(
        name="via_r12_default",
        bbox_func=lambda params: np.array([0, 0]),
        pins_func=lambda params: None,
        generate_func=via_r12_default_generate_func,
    )
    tlib.append([tvia_r12_default])
    tvia_r23_default = laygo2.object.template.UserDefinedTemplate(
        name="via_r23_default",
        bbox_func=lambda params: np.array([0, 0]),
        pins_func=lambda params: None,
        generate_func=via_r23_default_generate_func,
    )
    tlib.append([tvia_r23_default])
    tvia_r34_default = laygo2.object.template.UserDefinedTemplate(
        name="via_r34_default",
        bbox_func=lambda params: np.array([0, 0]),
        pins_func=lambda params: None,
        generate_func=via_r34_default_generate_func,
    )
    tlib.append([tvia_r34_default])
    return tlib

# Tests
if __name__ == "__main__":
    # Create templates.
    print("Create templates")
    templates = load_templates()
    print(templates["nmos"])
    print(templates["pmos"])


from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import numpy as np
import sys

filename = sys.argv[1]
file = open(filename)

# Extract data and labels from dataset
data, labels = [], []
for l in file:
    cols = l.strip().split('\t')
    vals, label = list(map(float, cols[:-1])), cols[-1]
    data.append(vals)
    labels.append(label)
data = np.array(data)
result = TSNE(n_components=2, n_iter=1000).fit_transform(data)

# Visualize the top two dimensions
diseases = list(set(labels))
colors = [plt.cm.jet(float(i) / len(diseases)) for i, x in enumerate(diseases)]
for i, u in enumerate(diseases):
    xi = [result[j][0] for j in range(len(result)) if labels[j] == u]
    yi = [result[j][1] for j in range(len(result)) if labels[j] == u]
    plt.scatter(xi, yi, c=colors[i], label=str(u))
plt.title("tSNE scatter plot for " + filename)
plt.legend()
plt.show()


# 将元素进行重复
#
# 将元素进行重复，可以采用“乘法”的形式，直接乘以原来的元素
# 也可以采用“加法”的形式，更方便理解

def multi_repeat(str_rep, n):
    """
    乘法表述
    str_rep: str to repeat
    n: number of repetition
    """
    return str_rep * n

def plus_repeat(str_rep, n):
    """
    加法表述
    str_rep: str to repeat
    n: number of repetition
    """
    if type(str_rep) is str:
        res_str = ''
        for i in range(n):
            res_str += str_rep
        return res_str
    elif type(str_rep) is list:
        res_list = []
        for i in range(n):
            res_list.extend(str_rep)
        return res_list
    else:
        return 'Error: str_rep should be str or list'

str_rep = '1 2 3 '
list_rep = [1, 's', (5,'3')]
n = 2
print(multi_repeat(str_rep, n))
print(multi_repeat(list_rep, n))
print(plus_repeat(str_rep, n))
print(plus_repeat(list_rep, n))


import pandas as pd

from energy_models.core.stream_type.carbon_models.carbon_storage import CarbonStorage
from energy_models.core.techno_type.techno_disc import TechnoDiscipline
from sostrades_core.tools.post_processing.charts.chart_filter import ChartFilter
from sostrades_core.tools.post_processing.charts.two_axes_instanciated_chart import InstanciatedSeries, \
    TwoAxesInstanciatedChart

class CSTechnoDiscipline(TechnoDiscipline):

    # ontology information
    _ontology_data = {
        'label': 'Carbon Storage Technology Model',
        'type': 'Research',
        'source': 'SoSTrades Project',
        'validated': '',
        'validated_by': 'SoSTrades Project',
        'last_modification_date': '',
        'category': '',
        'definition': '',
        'icon': 'fas fa-truck-loading fa-fw',
        'version': '',
    }
    DESC_IN = {'transport_cost': {'type': 'dataframe', 'unit': '$/t', 'visibility': TechnoDiscipline.SHARED_VISIBILITY, 'namespace': 'ns_carbon_storage',
                                  'dataframe_descriptor': {'years': ('int',  [1900, 2100], False),
                                                           'transport': ('float',  None, True)},
                                  'dataframe_edition_locked': False},
               'transport_margin': {'type': 'dataframe', 'unit': '%', 'visibility': TechnoDiscipline.SHARED_VISIBILITY, 'namespace': 'ns_carbon_storage',
                                    'dataframe_descriptor': {'years': ('int',  [1900, 2100], False),
                                                             'margin': ('float',  None, True)},
                                    'dataframe_edition_locked': False},
               'data_fuel_dict': {'type': 'dict', 'visibility': TechnoDiscipline.SHARED_VISIBILITY,
                                  'namespace': 'ns_carbon_storage', 'default': CarbonStorage.data_energy_dict,
                                  'unit': 'defined in dict'},
               }
    DESC_IN.update(TechnoDiscipline.DESC_IN)

    _maturity = 'Research'

    energy_name = CarbonStorage.name

    def compute_sos_jacobian(self):
        # Grad of price vs energyprice

        TechnoDiscipline.compute_sos_jacobian(self)

        grad_dict = self.techno_model.grad_price_vs_energy_price()

        self.set_partial_derivatives_techno(
            grad_dict, None)

    def get_chart_filter_list(self):

        chart_filters = []
        chart_list = ['Detailed prices',
                      'Consumption and production',
                      'Initial Production', 'Factory Mean Age', 'CO2 emissions']
        if self.get_sosdisc_inputs('is_apply_ratio'):
            chart_list.extend(['Applied Ratio'])
        chart_filters.append(ChartFilter(
            'Charts', chart_list, chart_list, 'charts'))

        price_unit_list = ['$/tCO2']
        chart_filters.append(ChartFilter(
            'Price unit', price_unit_list, price_unit_list, 'price_unit'))
        return chart_filters

    def get_post_processing_list(self, filters=None):

        # For the outputs, making a graph for block fuel vs range and blocktime vs
        # range

        instanciated_charts = []
        charts = []
        price_unit_list = ['$/tCO2']
        # Overload default value with chart filter
        if filters is not None:
            for chart_filter in filters:
                if chart_filter.filter_key == 'charts':
                    charts = chart_filter.selected_values
                if chart_filter.filter_key == 'price_unit':
                    price_unit_list = chart_filter.selected_values

        if 'Detailed prices' in charts and '$/tCO2' in price_unit_list:
            new_chart = self.get_chart_detailed_price_in_dollar_ton()
            if new_chart is not None:
                instanciated_charts.append(new_chart)

        if 'Consumption and production' in charts:
            new_chart = self.get_chart_investments()
            if new_chart is not None:
                instanciated_charts.append(new_chart)

            new_charts = self.get_charts_consumption_and_production()
            for new_chart in new_charts:
                if new_chart is not None:
                    instanciated_charts.append(new_chart)

        if 'Applied Ratio' in charts:
            new_chart = self.get_chart_applied_ratio()
            if new_chart is not None:
                instanciated_charts.append(new_chart)

        if 'Initial Production' in charts:
            if 'initial_production' in self.get_data_in():
                new_chart = self.get_chart_initial_production()
                if new_chart is not None:
                    instanciated_charts.append(new_chart)

        return instanciated_charts

    def get_chart_detailed_price_in_dollar_ton(self):

        techno_detailed_prices = self.get_sosdisc_outputs(
            'techno_detailed_prices')
        chart_name = f'Detailed prices of {self.techno_name} technology over the years'
        year_start = min(techno_detailed_prices['years'].values.tolist())
        year_end = max(techno_detailed_prices['years'].values.tolist())
        minimum = 0
        maximum = max(
            (techno_detailed_prices[self.techno_name].values).tolist()) * 1.2

        new_chart = TwoAxesInstanciatedChart('years', 'Prices [$/tCO2]', [year_start, year_end], [minimum, maximum],
                                             chart_name=chart_name)

        if 'percentage_resource' in self.get_data_in():
            percentage_resource = self.get_sosdisc_inputs(
                'percentage_resource')
            new_chart.annotation_upper_left = {
                'Percentage of total price at starting year': f'{percentage_resource[self.energy_name][0]} %'}
            tot_price = (techno_detailed_prices[self.techno_name].values) / \
                (percentage_resource[self.energy_name] / 100.)
            serie = InstanciatedSeries(
                techno_detailed_prices['years'].values.tolist(),
                tot_price.tolist(), 'Total price without percentage', 'lines')
            new_chart.series.append(serie)
        # Add total price
        serie = InstanciatedSeries(
            techno_detailed_prices['years'].values.tolist(),
            (techno_detailed_prices[self.techno_name].values).tolist(), 'Total price with margin', 'lines')

        new_chart.series.append(serie)

        # Factory price
        serie = InstanciatedSeries(
            techno_detailed_prices['years'].values.tolist(),
            (techno_detailed_prices[f'{self.techno_name}_factory'].values).tolist(), 'Factory', 'lines')

        new_chart.series.append(serie)

        if 'energy_costs' in techno_detailed_prices:
            # energy_costs
            serie = InstanciatedSeries(
                techno_detailed_prices['years'].values.tolist(),
                (techno_detailed_prices['energy_costs'].values).tolist(), 'Energy costs', 'lines')

            new_chart.series.append(serie)

        # Transport price
        serie = InstanciatedSeries(
            techno_detailed_prices['years'].values.tolist(),
            (techno_detailed_prices['transport'].values).tolist(), 'Transport', 'lines')

        new_chart.series.append(serie)
        # CO2 taxes
        serie = InstanciatedSeries(
            techno_detailed_prices['years'].values.tolist(),
            (techno_detailed_prices['CO2_taxes_factory'].values).tolist(), 'CO2 taxes due to production', 'lines')
        new_chart.series.append(serie)

        return new_chart

    def get_chart_investments(self):
        # Chart for input investments
        input_investments = self.get_sosdisc_inputs('invest_level')

        chart_name = f'Input investments over the years'

        new_chart = TwoAxesInstanciatedChart('years', 'Investments [G$]',
                                             chart_name=chart_name, stacked_bar=True)
        invest = input_investments['invest'].values
        serie = InstanciatedSeries(
            input_investments['years'].values.tolist(),
            invest.tolist(), '', 'bar')

        new_chart.series.append(serie)

        return new_chart

    def get_charts_consumption_and_production(self):
        instanciated_charts = []
        # Charts for consumption and prod
        techno_consumption = self.get_sosdisc_outputs(
            'techno_detailed_consumption')
        techno_production = self.get_sosdisc_outputs(
            'techno_detailed_production')
        chart_name = f'{self.techno_name} technology energy Capture & consumption
with input investments'

        new_chart = TwoAxesInstanciatedChart('years', 'Mass [Mt]',
                                             chart_name=chart_name, stacked_bar=True)

        for reactant in techno_consumption.columns:
            if reactant != 'years' and reactant.endswith('(Mt)'):
                energy_twh = -techno_consumption[reactant].values
                legend_title = f'{reactant} consumption'.replace(
                    "(Mt)", "")
                serie = InstanciatedSeries(
                    techno_consumption['years'].values.tolist(),
                    energy_twh.tolist(), legend_title, 'bar')

                new_chart.series.append(serie)

        for products in techno_production.columns:
            if products != 'years' and products.endswith('(Mt)'):
                energy_twh = techno_production[products].values
                legend_title = f'{products}'.replace(
                    "(Mt)", "")
                serie = InstanciatedSeries(
                    techno_production['years'].values.tolist(),
                    energy_twh.tolist(), legend_title, 'bar')

                new_chart.series.append(serie)
        instanciated_charts.append(new_chart)

        return instanciated_charts

    def get_chart_initial_production(self):

        year_start = self.get_sosdisc_inputs(
            'year_start')
        initial_production = self.get_sosdisc_inputs(
            'initial_production')
        initial_age_distrib = self.get_sosdisc_inputs(
            'initial_age_distrib')
        initial_prod = pd.DataFrame({'age': initial_age_distrib['age'].values,
                                     'distrib': initial_age_distrib['distrib'].values, })
        initial_prod['CO2 (Mt)'] = initial_prod['distrib'] / \
            100.0 * initial_production
        initial_prod['years'] = year_start - initial_prod['age']
        initial_prod.sort_values('years', inplace=True)
        initial_prod['cum CO2 (Mt)'] = initial_prod['CO2 (Mt)'].cumsum()

        study_production = self.get_sosdisc_outputs(
            'techno_detailed_production')
        chart_name = f'World {self.energy_name} capture via {self.techno_name}
with 2020 factories distribution'

        new_chart = TwoAxesInstanciatedChart('years', f'{self.energy_name} capture (Mt)',
                                             chart_name=chart_name)

        serie = InstanciatedSeries(
            initial_prod['years'].values.tolist(),
            initial_prod[f'cum CO2 (Mt)'].values.tolist(), 'Initial carbon capture by 2020 factories', 'lines')
        study_prod = study_production[f'{self.energy_name} (Mt)'].values
        new_chart.series.append(serie)
        years_study = study_production['years'].values.tolist()
        years_study.insert(0, year_start - 1)
        study_prod_l = study_prod.tolist()
        study_prod_l.insert(
            0, initial_prod[f'cum CO2 (Mt)'].values.tolist()[-1])
        serie = InstanciatedSeries(
            years_study,
            study_prod_l, 'Predicted carbon capture', 'lines')
        new_chart.series.append(serie)

        return new_chart


import numpy as np

radius = 1
total_dots = 100000
inner_dots = 0

# Using loop
# for dot in range(total_dots):
#     x = np.random.uniform(0, radius)
#     y = np.random.uniform(0, radius)
#
#     distance = np.sqrt(x**2 + y**2)
#     if distance <= radius:
#         inner_dots += 1
#
# print(4 * inner_dots / total_dots)

x = np.random.rand(1, total_dots)
y = np.random.rand(1, total_dots)

compare = (np.sqrt(x**2 + y**2) <= np.ones(total_dots))[0]
count_true = np.bincount(compare)[1]

print(4 * count_true / len(compare))


# Import Spark SQL and Spark ML libraries
from pyspark.sql.types import *
from pyspark.sql.functions import *
from pyspark.ml import Pipeline
from pyspark.ml.regression import LinearRegression
from pyspark.ml.feature import VectorAssembler
import xlrd
import csv
from pyspark.context import SparkContext
from pyspark.sql.session import SparkSession
from pyspark.ml.tuning import ParamGridBuilder, CrossValidator
from pyspark.ml.evaluation import RegressionEvaluator

sc = SparkContext('local')
spark = SparkSession(sc)
import pandas

# convert xls file to csv format
wb = xlrd.open_workbook('kauai.xls')
sh = wb.sheet_by_index(0)
kauai_csv = open('kauai_jobs_data.csv', 'w')
wr = csv.writer(kauai_csv, quoting=csv.QUOTE_ALL)
lHeaders = ['year','month','Arts_Entertainment_Recreation','Accommodation','FoodServices_DrinkingPlaces']
wr.writerow(lHeaders)
for rownum in range(6, sh.nrows-7):
    lDataRow = [sh.row_values(rownum)[0],sh.row_values(rownum)[1],sh.row_values(rownum)[20],sh.row_values(rownum)[21],sh.row_values(rownum)[22]]
    wr.writerow(lDataRow)
kauai_csv.close(),

# Load visitor data into pandas dataframe for unipoviting later.  Just keep the
# header row, total visitors, domestic, and international rows.
csv_visitors_pandadf = pandas.read_csv('Visitor Arrival_State of Hawaii-Monthly.csv', nrows=3)

# load mapping table to get from FEB to 02, etc...
month_schema = StructType([
    StructField("month_text", StringType())
    , StructField("month_num", StringType())
])
dfMapMonths = spark.read.format("csv")\
    .option("header", True)\
    .schema(month_schema)\
    .load('map_months.csv')
# Load job data
dfJobs = spark.read.format("csv")\
    .option("header", True)\
    .option("infertype", True)\
    .load("kauai_jobs_data.csv")

# Unpivot visitor data so years and moths are in nrows
csv_visitors_pandadf = csv_visitors_pandadf.unstack(level=1).reset_index(name='visitor_count')
# in column level_1 after pivot, 0 = Total Visitor Arrivals, 1 = Domestic
# Visitor Arrivals, 2 = International Visitor Arrivals
# pandas uses NaN for null.  Use .dropna() to drop rows with no visitor count
csv_visitors_clean = csv_visitors_pandadf.loc[ (csv_visitors_pandadf['level_0'] != 'Series') ].dropna()
csv_visitors_clean = csv_visitors_clean.rename(columns={'level_0': 'year_month', 'level_1': 'visitor_type_id'})
# convert pandas dataframe to spark
dfVisitors = spark.createDataFrame(csv_visitors_clean)
# join kauai jobs dataframe with map months so we can join to visitor data on year-months
jobs = dfJobs.alias('jobs')
m = dfMapMonths.alias('m')
dfJ = jobs.join(m, jobs.month == m.month_text, 'inner')
# add column for year as str
dfJ1 = dfJ.withColumn("year_str", col("year").cast(StringType()).substr(1, 4))
#  cast(StringType()).split('.',1)[0])
# add column for year_month
dfJ2 = dfJ1.withColumn("year_month", concat(col("year_str"), lit("-"), col("month_num")))
dfJ3 = dfJ2.withColumn("year_int", col("year_str").cast(IntegerType()))
dfJ4 = dfJ3.withColumn("month_int", col("month_num").cast(IntegerType()))
# make the join to visitor dataframe
vis = dfVisitors.alias('vis')
df_joined = vis.join(dfJ4, vis.year_month == dfJ4.year_month).select(
    "vis.year_month"
    , "year_int"
    , "month_int"
    , "visitor_type_id"
    , "visitor_count"
    , col("Arts_Entertainment_Recreation").cast(DoubleType())
    , col("Accommodation").cast(DoubleType())
    , col("FoodServices_DrinkingPlaces").cast(DoubleType())
)
# start machine learning training
# Select features and label
data = df_joined.select(
    "year_int"
    , "month_int"
    , "visitor_type_id"
    , "visitor_count"
    , col("Arts_Entertainment_Recreation").alias("label")
)
# Split the data
splits = data.randomSplit([0.7, 0.3])
train = splits[0]
test = splits[1].withColumnRenamed("label", "trueLabel")
# Define the pipeline
assembler = VectorAssembler(inputCols=[
    "year_int"
    , "month_int"
    , "visitor_type_id"
    , "visitor_count"
], outputCol="features")
lr = LinearRegression(labelCol="label",featuresCol="features")
pipeline = Pipeline(stages=[assembler, lr])
# Tune Parameters using the CrossValidator class to evaluate each combination
# of parameters defined in a ParameterGrid against multiple folds of the data
paramGrid = ParamGridBuilder().addGrid(lr.regParam, [0.3, 0.01]).addGrid(lr.maxIter, [10, 5]).build()
cv = CrossValidator(estimator=pipeline, evaluator=RegressionEvaluator(), estimatorParamMaps=paramGrid, numFolds=2)

model = cv.fit(train)
# Test the Model
prediction = model.transform(test)
predicted = prediction.select("features", "prediction", "trueLabel")
predicted.show()

# Retrieve the Root Mean Square Error (RMSE)
# There are a number of metrics used to measure the variance between predicted
# and actual values. Of these, the root mean square error (RMSE) is a commonly
# used value that is measured in the same units as the prediced and actual
# values. You can use the RegressionEvaluator class to retrieve the RMSE.
# my notes-> in this case, I think RMSE shows the average number of jobs
# between predicted and actual job values
evaluator = RegressionEvaluator(labelCol="trueLabel", predictionCol="prediction", metricName="rmse")
rmse = evaluator.evaluate(prediction)
print ("Root Mean Square Error (RMSE):", rmse)


import pickle
import os
import torch
import torch.nn as nn
import gc

def save_model(model: nn.Module, path=f"models/model.pth"):
    torch.save(model.state_dict(), path)

def load_model(model: nn.Module, path=f"models/model.pth"):
    model.load_state_dict(torch.load(path))

def save_data(model_id: str, data_id, data, delete_after_save=True):
    if not os.path.exists(f"models/{model_id}"):
        os.makedirs(f"models/{model_id}")
    with open(f"models/{model_id}/{data_id}.pkl", "wb") as f:
        pickle.dump(data, f)
    if delete_after_save:
        del data
        data = None

def save_model_results(model_id: str, train_loss_history, test_loss_history, test_accuracy_history, delete_after_save=True):
    save_data(model_id, "train_loss_history",
              train_loss_history, delete_after_save)
    save_data(model_id, "test_loss_history",
              test_loss_history, delete_after_save)
    save_data(model_id, "test_accuracy_history",
              test_accuracy_history, delete_after_save)
    if delete_after_save:
        gc.collect()

def load_data(model_id: str, data_id):
    with open(f"models/{model_id}/{data_id}.pkl", "rb") as f:
        data = pickle.load(f)
    return data

def load_model_results(model_id: str, flags=(1, 1, 1)):
    train_loss_history, test_loss_history, test_accuracy_history = None, None, None
    if flags[0]:
        train_loss_history = load_data(model_id, "train_loss_history")
    if flags[1]:
        test_loss_history = load_data(model_id, "test_loss_history")
    if flags[2]:
        test_accuracy_history = load_data(model_id, "test_accuracy_history")
    return train_loss_history, test_loss_history, test_accuracy_history