from flask import Flask, render_template, redirect, request
from petssqlconnect import connectToMySQL

app = Flask(__name__)

@app.route("/")
def pet_index():

    mysql = connectToMySQL('pets')

    pets = mysql.query_db('SELECT * from pets;')
    print(pets)

    return render_template("petsindex.html", all_pets = pets)

@app.route("/add", methods=['POST'])
def add_pet_to_db():

    mysql = connectToMySQL("pets")

    query = "INSERT INTO pets (name, type, created_at, updated_at) VALUES (%(name)s, %(type)s, NOW(), NOW() );"

    data = {
        "name" : request.form['pet_name'],
        "type" : request.form['pet_type']
    }

    new_pet = mysql.query_db(query, data)

    return redirect("/")

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


#!/usr/bin/env python
import sys
py_v = sys.version_info[:2]
if not (py_v == (2, 7) or py_v >= (3, 3)):
    sys.exit('Only Python 2.7 or 3.3 and up are supported. Current version: ' + '.'.join(py_v))

package_name = 'clearup'

from ngs_utils import setup_utils
version = setup_utils.init(package_name, package_name, __file__)

from setuptools import setup, find_packages
setup(
    name=package_name,
    version=version,
    author='Vlad Saveliev, Tristan Lubinsky, Alla Miheenko',
    author_email='vladislav.sav@gmail.com',
    description='Mix up check tool',
    long_description=(open('README.md').read()),
    keywords='bioinformatics',
    url='https://github.com/AstraZeneca-NGS/ClearUp',
    license='GPLv3',
    packages=find_packages(),
    package_data={
        package_name:
            ['snps/*.bed.gz*'],
        '':
            setup_utils.find_package_files('static', '') +
            setup_utils.find_package_files('templates', ''),
    },
    include_package_data=True,
    zip_safe=False,
    scripts=['start.py', 'manage.py', 'genotyper.py'],
    install_requires=setup_utils.get_reqs(),
    classifiers=[
        'Environment :: Console',
        'Environment :: Web Environment',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Natural Language :: English',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: POSIX',
        'Operating System :: Unix',
        'Programming Language :: Python',
        'Programming Language :: JavaScript',
        'Topic :: Scientific/Engineering',
        'Topic :: Scientific/Engineering :: Bio-Informatics',
    ],
    test_suite='nose.collector',
    tests_require=['nose'],
)

print("""
-------------------------
 Installation complete!
-------------------------
Usage:
$ ./manage.py init_db
$ ./manage.py load_bcbio_project <bcbio_final_path>
$ ./manage.py load_data <dir_with_bams_and_beds> name=<project_name> genome=<genome_build_name>
$ ./start.py
""")


from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as EC
from uiautomator import device as d
from time import sleep
import re
class Action():
    def __init__(self):
    # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "Google_Nexus_6___5_1_0___API_22___1440x2560",
            "appPackage": "com.ss.android.ugc.aweme",
            "appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity",
            "noReset": "True",
            "unicodeKeyboard": "True",
            "resetKeyboard": "True"
        }
# 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        self.start_x = 720
        self.start_y = 1532
        self.distance = 1130
        self.uu = 0
# 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.wait = WebDriverWait(self.driver, 30)

    def comments(self):
        sleep(5)
        # app开启之后点击一次屏幕，确保页面的展示
        self.driver.tap([(500, 1200)], 500)

    def search(self):
        sleep(2)
        search2=self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/a9k')))
        # search2 = self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/a9k')
        search2.click()
        print("-----------------------------------去搜索界面----------------------------------------")
        txt = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/a1x')))
        # # txt.clear()
        txt.send_keys("别克GL6")
        # txt.send_keys(u"qiche")
        print("-----------------------------------输入完成----------------------------------------")
        self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/a1z'))).click()
        print("-----------------------------------搜索完成----------------------------------------")
        # 'android.widget.TextView' 'com.ss.android.ugc.aweme'  checked  checkable
        # d(text=u'视频', className='android.widget.TextView').click()
        # self.wait.until(self.driver.tap([(35, 98), (154, 217)], 500))
        # self.wait.until(EC.presence_of_elements_located((By.CLASS_NAME, 'android.widget.TextView')))
        sleep(5)
        # self.driver.tap([(288, 242), (576, 382)], 500)
        # a = self.driver.find_element_by_xpath(
        #     '//android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView[2]')
        a = self.driver.find_element_by_xpath(
            '//*[@text="视频"]')
        a.click()
        # print(a)

        # self.locator = ("className", "android.widget.TextView")
        # self.text = u'视频'
        # self.wait.until(EC.text_to_be_present_in_element(self.locator, self.text)).click()

        print("-----------------------------------跳转到视频选项----------------------------------------")
        sleep(5)
        # 'com.ss.android.ugc.aweme:id/jn'  --videolist

    def scroll(self):
        self.driver.swipe(self.start_x, self.start_y, self.start_x, self.start_y - self.distance)

        # self.driver.getPageSource()

    def enterComments(self):
        sleep(2)
        print("-----------------------------------已经进入第一个视频----------------------------------------")
        sleep(5)
        # author = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/bc')))

        # print(author.text+'这是作者')
        # title = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/a23')))
        # print(title.text + '这是标题')
        # dianzan = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/al9')))
        # print(dianzan.text + '这是点赞数')
        # self.driver.tap([(288, 242), (576, 382)], 500)  #进入评论
        # self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/wp'))).click()
        # self.driver.tap([(1263, 1460), (1403, 1600)], 500)
        self.driver.tap([(1320, 1510)], 500)
        print("-----------------------------------评论窗口打开----------------------------------------")
        sleep(2)
        print("-----------------------------------好了----------------------------------------")
        sleep(2)
        pingluncount = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/bc')))
        pingping=pingluncount.text
        matchObj = re.match(r'(.*)条评论', pingping, re.M | re.I)

        if int(matchObj.group(1)) < 5:
            self.uu = 2
        elif int(matchObj.group(1)) < 10:
            self.uu = 4
        elif int(matchObj.group(1)) < 20:
            self.uu = 8
        else:
            self.uu = 20

    def catchComments(self):

        pinglunshu = self.wait.until(EC.presence_of_element_located((By.ID, 'com.ss.android.ugc.aweme:id/bc')))
        print(pinglunshu.text + '这是评论数')

        commtauthors = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/bc')))
        commdetails = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/tt')))
        commdates = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/ag3')))
        commdianzans = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/ai_')))
        for commtauthor in commtauthors:
            sleep(2)
            print(commtauthor.text + '这是评论作者')
        for commdetail in commdetails:
            sleep(2)
            print(commdetail.text + '这是评论详情')
        for commdate in commdates:
            sleep(2)
            print(commdate.text + '这是评论时间')
        for commdianzan in commdianzans:
            sleep(2)
            print(commdianzan.text + '这是该评论的点赞数')

    def fechAllResults(self):
        # '#在抖音，记录美好生活#别克GL6对开雨刷，刮的面积很大，副驾驶前面也有好视线。
        # http://v.douyin.com/d6rdRC/ 复制此链接，打开【抖音短视频】，直接观看视频！'
        sleep(2)

        # 'com.ss.android.ugc.aweme:id/a23' 'com.ss.android.ugc.aweme:id/asp' 'com.ss.android.ugc.aweme:id/w5' 'com.ss.android.ugc.aweme:id/a3i'
        # titles  authimages  authors  dianzans
        # titles = self.driver.find_element_by_xpath(
        #     "/hierarchy/android.widget.FrameLayout[*]/android.widget.LinearLayout/*")

        titles = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/a23')))
        # titles = self.driver.find_elements_by_id('com.ss.android.ugc.aweme:id/a23')
        authors = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/w5')))
        dianzans = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.ss.android.ugc.aweme:id/a3i')))
        # self.driver.find_element_by_xpath(
        #         '//*[@text="没有更多了"]')

        for title in titles:
            sleep(2)
            print(title.text)
        for author in authors:
            sleep(2)
            print(author.text)
        for dianzan in dianzans:
            sleep(2)
            print(dianzan.text)

    def ppp(self):
        sleep(5)
        self.driver.tap([(350, 800)], 500)
        print("-----------------------------------点击进入第一个视频----------------------------------------")
        self.enterComments()
        i = 1
        while i <= int(self.uu):
            self.catchComments()
            # self.scroll()
            self.driver.swipe(620, 2030, 620, 1110)
            i = i + 1
            print(i)
        sleep(5)
        # self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/is').click()
        self.driver.tap([(1340, 739)], 500)
        print("-----------------------------------评论关闭----------------------------------------")
        sleep(5)
        # self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/a1y').click()
        self.driver.tap([(80, 196)], 500)
        print("-----------------------------------返回视频选项卡----------------------------------------")
        sleep(2)
        #     ----------------------------------------------------------------
        self.driver.tap([(1110, 800)], 500)
        print("-----------------------------------点击进入第二个视频----------------------------------------")
        self.enterComments()
        i = 1
        while i <= int(self.uu):
            self.catchComments()
            # self.scroll()
            self.driver.swipe(620, 2030, 620, 1110)
            i = i + 1
            print(i)
        sleep(5)
        # self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/is').click()
        self.driver.tap([(1340, 739)], 500)
        print("-----------------------------------评论关闭----------------------------------------")
        sleep(3)
        # self.driver.find_element_by_id('com.ss.android.ugc.aweme:id/a1y').click()
        self.driver.tap([(80, 196)], 500)
        print("-----------------------------------返回视频选项卡----------------------------------------")
        sleep(2)

    def main(self):
        self.comments()
        self.search()
        j = 1
        while j <= 20:
            self.ppp()
            self.scroll()
            j = j + 1

if __name__ == '__main__':
    action = Action()
    action.main()


# coding: utf-8

# **Chapter 9 – Up and running with TensorFlow**

# _This notebook contains all the sample code and solutions to the exercises in chapter 9._

# # Setup

# First, let's make sure this notebook works well in both python 2 and 3, import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures:

# In[1]:

# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

# To plot pretty figures
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "tensorflow"

def save_fig(fig_id, tight_layout=True):
    path = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id + ".png")
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format='png', dpi=300)

# # Creating and running a graph

# In[2]:

import tensorflow as tf

x = tf.Variable(3, name="x")
y = tf.Variable(4, name="y")
f = x*x*y + y + 2

f

sess = tf.Session()
sess.run(x.initializer)
sess.run(y.initializer)
result = sess.run(f)
print(result)

sess.close()

with tf.Session() as sess:
    x.initializer.run()
    y.initializer.run()
    result = f.eval()

result

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    result = f.eval()

result

init = tf.global_variables_initializer()

sess = tf.InteractiveSession()
init.run()
result = f.eval()
print(result)

sess.close()

result

# # Managing graphs

x1 = tf.Variable(1)
x1.graph is tf.get_default_graph()
graph = tf.Graph()
with graph.as_default():
    x2 = tf.Variable(2)

x2.graph is graph

# In[16]:

x2.graph is tf.get_default_graph()

# In[17]:

w = tf.constant(3)
x = w + 2
y = x + 5
z = x * 3

with tf.Session() as sess:
    print(y.eval())  # 10
    print(z.eval())  # 15

# In[18]:

with tf.Session() as sess:
    y_val, z_val = sess.run([y, z])
    print(y_val)  # 10
    print(z_val)  # 15

# # Linear Regression

# ## Using the Normal Equation

# In[19]:

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

X = tf.constant(housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
XT = tf.transpose(X)
theta = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(XT, X)), XT), y)

with tf.Session() as sess:
    theta_value = theta.eval()

theta_value

# Compare with pure NumPy

# In[21]:

X = housing_data_plus_bias
y = housing.target.reshape(-1, 1)
theta_numpy = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)

print(theta_numpy)

# Compare with Scikit-Learn

# In[22]:

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(housing.data, housing.target.reshape(-1, 1))

print(np.r_[lin_reg.intercept_.reshape(-1, 1), lin_reg.coef_.T])

# ## Using Batch Gradient Descent

# Gradient Descent requires scaling the feature vectors first. We could do this using TF, but let's just use Scikit-Learn for now.

# In[23]:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_housing_data = scaler.fit_transform(housing.data)
scaled_housing_data_plus_bias = np.c_[np.ones((m, 1)), scaled_housing_data]

print(scaled_housing_data_plus_bias.mean(axis=0))
print(scaled_housing_data_plus_bias.mean(axis=1))
print(scaled_housing_data_plus_bias.mean())
print(scaled_housing_data_plus_bias.shape)

# ### Manually computing the gradients
n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
gradients = 2/m * tf.matmul(tf.transpose(X), error)
training_op = tf.assign(theta, theta - learning_rate * gradients)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print("Epoch", epoch, "MSE =", mse.eval())
        sess.run(training_op)

    best_theta = theta.eval()

best_theta

# ### Using autodiff

# Same as above except for the `gradients = ...` line:

n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")

gradients = tf.gradients(mse, [theta])[0]

training_op = tf.assign(theta, theta - learning_rate * gradients)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print("Epoch", epoch, "MSE =", mse.eval())
        sess.run(training_op)

    best_theta = theta.eval()

print("Best theta:")
print(best_theta)

# How could you find the partial derivatives of the following function with regards to `a` and `b`?

def my_func(a, b):
    z = 0
    for i in range(100):
        z = a * np.cos(z + i) + z * np.sin(b - i)
    return z

my_func(0.2, 0.3)

a = tf.Variable(0.2, name="a")
b = tf.Variable(0.3, name="b")
z = tf.constant(0.0, name="z0")
for i in range(100):
    z = a * tf.cos(z + i) + z * tf.sin(b - i)

grads = tf.gradients(z, [a, b])
init = tf.global_variables_initializer()

# Let's compute the function at $a=0.2$ and $b=0.3$, and the partial derivatives at that point with regards to $a$ and with regards to $b$:

# In[33]:

with tf.Session() as sess:
    init.run()
    print(z.eval())
    print(sess.run(grads))

# ### Using a `GradientDescentOptimizer`

n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")

optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print("Epoch", epoch, "MSE =", mse.eval())
        sess.run(training_op)

    best_theta = theta.eval()

print("Best theta:")
print(best_theta)

# ### Using a momentum optimizer
n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")

optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
training_op = optimizer.minimize(mse)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        sess.run(training_op)

    best_theta = theta.eval()

print("Best theta:")
print(best_theta)

# # Feeding data to the training algorithm
# ## Placeholder nodes
A = tf.placeholder(tf.float32, shape=(None, 3))
B = A + 5
with tf.Session() as sess:
    B_val_1 = B.eval(feed_dict={A: [[1, 2, 3]]})
    B_val_2 = B.eval(feed_dict={A: [[4, 5, 6], [7, 8, 9]]})

print(B_val_1)
print(B_val_2)

# ## Mini-batch Gradient Descent
n_epochs = 1000
learning_rate = 0.01

X = tf.placeholder(tf.float32, shape=(None, n + 1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")

theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)

init = tf.global_variables_initializer()

n_epochs = 10
batch_size = 100
n_batches = int(np.ceil(m / batch_size))

def fetch_batch(epoch, batch_index, batch_size):
    np.random.seed(epoch * n_batches + batch_index)  # not shown in the book
    indices = np.random.randint(m, size=batch_size)  # not shown
    X_batch = scaled_housing_data_plus_bias[indices] # not shown
    y_batch = housing.target.reshape(-1, 1)[indices] # not shown
    return X_batch, y_batch

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        for batch_index in range(n_batches):
            X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

    best_theta = theta.eval()

best_theta

# # Saving and restoring a model
n_epochs = 1000                                                                       # not shown in the book
learning_rate = 0.01                                                                  # not shown

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")            # not shown
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")            # not shown
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")                                      # not shown
error = y_pred - y                                                                    # not shown
mse = tf.reduce_mean(tf.square(error), name="mse")                                    # not shown
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)            # not shown
training_op = optimizer.minimize(mse)                                                 # not shown

init = tf.global_variables_initializer()
saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print("Epoch", epoch, "MSE =", mse.eval())  # not shown
            save_path = saver.save(sess, "c:/my_model.ckpt")
        sess.run(training_op)

    best_theta = theta.eval()
    save_path = saver.save(sess, "c:/my_model_final.ckpt")

best_theta

with tf.Session() as sess:
    saver.restore(sess, "c:/my_model_final.ckpt")
    best_theta_restored = theta.eval() # not shown in the book

np.allclose(best_theta, best_theta_restored)

# If you want to have a saver that loads and restores `theta` with a different name, such as `"weights"`:
saver = tf.train.Saver({"weights": theta})

# By default the saver also saves the graph structure itself in a second file with the extension `.meta`.
# You can use the function `tf.train.import_meta_graph()` to restore the graph structure.
# This function loads the graph into the default graph and returns a `Saver`
# that can then be used to restore the graph state (i.e., the variable values):

saver = tf.train.import_meta_graph("c:/my_model_final.ckpt.meta")  # this loads the graph structure
theta = tf.get_default_graph().get_tensor_by_name("theta:0") # not shown in the book

with tf.Session() as sess:
    saver.restore(sess, "c:/my_model_final.ckpt")  # this restores the graph's state
    best_theta_restored = theta.eval() # not shown in the book

np.allclose(best_theta, best_theta_restored)

# This means that you can import a pretrained model without having to have the corresponding Python code to build the graph. This is very handy when you keep tweaking and saving your model: you can load a previously saved model without having to search for the version of the code that built it.

# # Visualizing the graph
# ## inside Jupyter

# To visualize the graph within Jupyter, we will use a TensorBoard server available online at https://tensorboard.appspot.com/ (so this will not work if you do not have Internet access).  As far as I can tell, this code was originally written by Alex Mordvintsev in his [DeepDream tutorial](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb). Alternatively, you could use a tool like [tfgraphviz](https://github.com/akimach/tfgraphviz).
from tensorflow_graph_in_jupyter import show_graph
show_graph(tf.get_default_graph())

# ## Using TensorBoard

from datetime import datetime

now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = "tf_logs"
logdir = "{}/run-{}/".format(root_logdir, now)

n_epochs = 1000
learning_rate = 0.01

X = tf.placeholder(tf.float32, shape=(None, n + 1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)

init = tf.global_variables_initializer()

mse_summary = tf.summary.scalar('MSE', mse)
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())

n_epochs = 10
batch_size = 100
n_batches = int(np.ceil(m / batch_size))

with tf.Session() as sess:                                                        # not shown in the book
    sess.run(init)                                                                # not shown

    for epoch in range(n_epochs):                                                 # not shown
        for batch_index in range(n_batches):
            X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            if batch_index % 10 == 0:
                summary_str = mse_summary.eval(feed_dict={X: X_batch, y: y_batch})
                step = epoch * n_batches + batch_index
                file_writer.add_summary(summary_str, step)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

    best_theta = theta.eval()                                                     # not shown

file_writer.close()

best_theta

# # Name scopes

# In[66]:

now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = "tf_logs"
logdir = "{}/run-{}/".format(root_logdir, now)

n_epochs = 1000
learning_rate = 0.01

X = tf.placeholder(tf.float32, shape=(None, n + 1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")

with tf.name_scope("loss") as scope:
    error = y_pred - y
    mse = tf.reduce_mean(tf.square(error), name="mse")

optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)

init = tf.global_variables_initializer()

mse_summary = tf.summary.scalar('MSE', mse)
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())

n_epochs = 10
batch_size = 100
n_batches = int(np.ceil(m / batch_size))

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        for batch_index in range(n_batches):
            X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            if batch_index % 10 == 0:
                summary_str = mse_summary.eval(feed_dict={X: X_batch, y: y_batch})
                step = epoch * n_batches + batch_index
                file_writer.add_summary(summary_str, step)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

    best_theta = theta.eval()

file_writer.flush()
file_writer.close()
print("Best theta:")
print(best_theta)

print(error.op.name)

print(mse.op.name)

a1 = tf.Variable(0, name="a")      # name == "a"
a2 = tf.Variable(0, name="a")      # name == "a_1"

with tf.name_scope("param"):       # name == "param"
    a3 = tf.Variable(0, name="a")  # name == "param/a"

with tf.name_scope("param"):       # name == "param_1"
    a4 = tf.Variable(0, name="a")  # name == "param_1/a"

for node in (a1, a2, a3, a4):
    print(node.op.name)

# # Modularity

# An ugly flat code:

n_features = 3
X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")

w1 = tf.Variable(tf.random_normal((n_features, 1)), name="weights1")
w2 = tf.Variable(tf.random_normal((n_features, 1)), name="weights2")
b1 = tf.Variable(0.0, name="bias1")
b2 = tf.Variable(0.0, name="bias2")

z1 = tf.add(tf.matmul(X, w1), b1, name="z1")
z2 = tf.add(tf.matmul(X, w2), b2, name="z2")

relu1 = tf.maximum(z1, 0., name="relu1")
relu2 = tf.maximum(z1, 0., name="relu2")  # Oops, cut&paste error! Did you spot it?

output = tf.add(relu1, relu2, name="output")

# Much better, using a function to build the ReLUs:
def relu(X):
    w_shape = (int(X.get_shape()[1]), 1)
    w = tf.Variable(tf.random_normal(w_shape), name="weights")
    b = tf.Variable(0.0, name="bias")
    z = tf.add(tf.matmul(X, w), b, name="z")
    return tf.maximum(z, 0., name="relu")

n_features = 3
X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
relus = [relu(X) for i in range(5)]
output = tf.add_n(relus, name="output")

file_writer = tf.summary.FileWriter("logs/relu1", tf.get_default_graph())

# Even better using name scopes:

# In[76]:

reset_graph()

def relu(X):
    with tf.name_scope("relu"):
        w_shape = (int(X.get_shape()[1]), 1)                          # not shown in the book
        w = tf.Variable(tf.random_normal(w_shape), name="weights")    # not shown
        b = tf.Variable(0.0, name="bias")                             # not shown
        z = tf.add(tf.matmul(X, w), b, name="z")                      # not shown
        return tf.maximum(z, 0., name="max")                          # not shown

# In[77]:

n_features = 3
X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
relus = [relu(X) for i in range(5)]
output = tf.add_n(relus, name="output")

file_writer = tf.summary.FileWriter("logs/relu2", tf.get_default_graph())
file_writer.close()

# ## Sharing Variables

# Sharing a `threshold` variable the classic way, by defining it outside of the `relu()` function then passing it as a parameter:

# In[78]:

reset_graph()

def relu(X, threshold):
    with tf.name_scope("relu"):
        w_shape = (int(X.get_shape()[1]), 1)                        # not shown in the book
        w = tf.Variable(tf.random_normal(w_shape), name="weights")  # not shown
        b = tf.Variable(0.0, name="bias")                           # not shown
        z = tf.add(tf.matmul(X, w), b, name="z")                    # not shown
        return tf.maximum(z, threshold, name="max")

threshold = tf.Variable(0.0, name="threshold")
X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
relus = [relu(X, threshold) for i in range(5)]
output = tf.add_n(relus, name="output")

# In[79]:

reset_graph()

def relu(X):
    with tf.name_scope("relu"):
        if not hasattr(relu, "threshold"):
            relu.threshold = tf.Variable(0.0, name="threshold")
        w_shape = int(X.get_shape()[1]), 1                          # not shown in the book
        w = tf.Variable(tf.random_normal(w_shape), name="weights")  # not shown
        b = tf.Variable(0.0, name="bias")                           # not shown
        z = tf.add(tf.matmul(X, w), b, name="z")                    # not shown
        return tf.maximum(z, relu.threshold, name="max")

# In[80]:

X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
relus = [relu(X) for i in range(5)]
output = tf.add_n(relus, name="output")

# In[81]:

reset_graph()

with tf.variable_scope("relu"):
    threshold = tf.get_variable("threshold", shape=(),
                                initializer=tf.constant_initializer(0.0))

# In[82]:

with tf.variable_scope("relu", reuse=True):
    threshold = tf.get_variable("threshold")

# In[83]:

with tf.variable_scope("relu") as scope:
    scope.reuse_variables()
    threshold = tf.get_variable("threshold")

# In[84]:

reset_graph()

def relu(X):
    with tf.variable_scope("relu", reuse=True):
        threshold = tf.get_variable("threshold")
        w_shape = int(X.get_shape()[1]), 1                          # not shown
        w = tf.Variable(tf.random_normal(w_shape), name="weights")  # not shown
        b = tf.Variable(0.0, name="bias")                           # not shown
        z = tf.add(tf.matmul(X, w), b, name="z")                    # not shown
        return tf.maximum(z, threshold, name="max")

X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
with tf.variable_scope("relu"):
    threshold = tf.get_variable("threshold", shape=(),
                                initializer=tf.constant_initializer(0.0))
relus = [relu(X) for relu_index in range(5)]
output = tf.add_n(relus, name="output")

# In[85]:

file_writer = tf.summary.FileWriter("logs/relu6", tf.get_default_graph())
file_writer.close()

# In[86]:

reset_graph()

def relu(X):
    with tf.variable_scope("relu"):
        threshold = tf.get_variable("threshold", shape=(), initializer=tf.constant_initializer(0.0))
        w_shape = (int(X.get_shape()[1]), 1)
        w = tf.Variable(tf.random_normal(w_shape), name="weights")
        b = tf.Variable(0.0, name="bias")
        z = tf.add(tf.matmul(X, w), b, name="z")
        return tf.maximum(z, threshold, name="max")

X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
with tf.variable_scope("", default_name="") as scope:
    first_relu = relu(X)     # create the shared variable
    scope.reuse_variables()  # then reuse it
    relus = [first_relu] + [relu(X) for i in range(4)]
output = tf.add_n(relus, name="output")

file_writer = tf.summary.FileWriter("logs/relu8", tf.get_default_graph())
file_writer.close()

# In[87]:

reset_graph()

def relu(X):
    threshold = tf.get_variable("threshold", shape=(),
                                initializer=tf.constant_initializer(0.0))
    w_shape = (int(X.get_shape()[1]), 1)                        # not shown in the book
    w = tf.Variable(tf.random_normal(w_shape), name="weights")  # not shown
    b = tf.Variable(0.0, name="bias")                           # not shown
    z = tf.add(tf.matmul(X, w), b, name="z")                    # not shown
    return tf.maximum(z, threshold, name="max")

X = tf.placeholder(tf.float32, shape=(None, n_features), name="X")
relus = []
for relu_index in range(5):
    with tf.variable_scope("relu", reuse=(relu_index >= 1)) as scope:
        relus.append(relu(X))
output = tf.add_n(relus, name="output")

# In[88]:

file_writer = tf.summary.FileWriter("logs/relu9", tf.get_default_graph())
file_writer.close()

# # Extra material

# In[89]:

reset_graph()

with tf.variable_scope("my_scope"):
    x0 = tf.get_variable("x", shape=(), initializer=tf.constant_initializer(0.))
    x1 = tf.Variable(0., name="x")
    x2 = tf.Variable(0., name="x")

with tf.variable_scope("my_scope", reuse=True):
    x3 = tf.get_variable("x")
    x4 = tf.Variable(0., name="x")

with tf.variable_scope("", default_name="", reuse=True):
    x5 = tf.get_variable("my_scope/x")

print("x0:", x0.op.name)
print("x1:", x1.op.name)
print("x2:", x2.op.name)
print("x3:", x3.op.name)
print("x4:", x4.op.name)
print("x5:", x5.op.name)
print(x0 is x3 and x3 is x5)

# The first `variable_scope()` block first creates the shared variable `x0`, named `my_scope/x`. For all operations other than shared variables (including non-shared variables), the variable scope acts like a regular name scope, which is why the two variables `x1` and `x2` have a name with a prefix `my_scope/`. Note however that TensorFlow makes their names unique by adding an index: `my_scope/x_1` and `my_scope/x_2`.
#
# The second `variable_scope()` block reuses the shared variables in scope `my_scope`, which is why `x0 is x3`. Once again, for all operations other than shared variables it acts as a named scope, and since it's a separate block from the first one, the name of the scope is made unique by TensorFlow (`my_scope_1`) and thus the variable `x4` is named `my_scope_1/x`.
#
# The third block shows another way to get a handle on the shared variable `my_scope/x` by creating a `variable_scope()` at the root scope (whose name is an empty string), then calling `get_variable()` with the full name of the shared variable (i.e. `"my_scope/x"`).

# ## Strings

# In[90]:

reset_graph()

text = np.array("Do you want some café?".split())
text_tensor = tf.constant(text)

with tf.Session() as sess:
    print(text_tensor.eval())

# ## Autodiff

# Note: the autodiff content was moved to the [extra_autodiff.ipynb](extra_autodiff.ipynb) notebook.

# # Exercise solutions

# ## 1. to 11.

# See appendix A.

# ## 12. Logistic Regression with Mini-Batch Gradient Descent using TensorFlow

# First, let's create the moons dataset using Scikit-Learn's `make_moons()` function:

# In[91]:

from sklearn.datasets import make_moons

m = 1000
X_moons, y_moons = make_moons(m, noise=0.1, random_state=42)

# Let's take a peek at the dataset:

# In[92]:

plt.plot(X_moons[y_moons == 1, 0], X_moons[y_moons == 1, 1], 'go', label="Positive")
plt.plot(X_moons[y_moons == 0, 0], X_moons[y_moons == 0, 1], 'r^', label="Negative")
plt.legend()
plt.show()

# We must not forget to add an extra bias feature ($x_0 = 1$) to every instance. For this, we just need to add a column full of 1s on the left of the input matrix $\mathbf{X}$:

# In[93]:

X_moons_with_bias = np.c_[np.ones((m, 1)), X_moons]

# Let's check:

# In[94]:

X_moons_with_bias[:5]

# Looks good. Now let's reshape `y_train` to make it a column vector (i.e. a 2D array with a single column):

# In[95]:

y_moons_column_vector = y_moons.reshape(-1, 1)

# Now let's split the data into a training set and a test set:

# In[96]:

test_ratio = 0.2
test_size = int(m * test_ratio)
X_train = X_moons_with_bias[:-test_size]
X_test = X_moons_with_bias[-test_size:]
y_train = y_moons_column_vector[:-test_size]
y_test = y_moons_column_vector[-test_size:]

# Ok, now let's create a small function to generate training batches. In this implementation we will just pick random instances from the training set for each batch. This means that a single batch may contain the same instance multiple times, and also a single epoch may not cover all the training instances (in fact it will generally cover only about two thirds of the instances). However, in practice this is not an issue and it simplifies the code:

# In[97]:

def random_batch(X_train, y_train, batch_size):
    rnd_indices = np.random.randint(0, len(X_train), batch_size)
    X_batch = X_train[rnd_indices]
    y_batch = y_train[rnd_indices]
    return X_batch, y_batch

# Let's look at a small batch:

# In[98]:

X_batch, y_batch = random_batch(X_train, y_train, 5)
X_batch

# In[99]:

y_batch

# Great! Now that the data is ready to be fed to the model, we need to build that model. Let's start with a simple implementation, then we will add all the bells and whistles.

# First let's reset the default graph.

# In[100]:

reset_graph()

# The _moons_ dataset has two input features, since each instance is a point on a plane (i.e., 2-Dimensional):

# In[101]:

n_inputs = 2

# Now let's build the Logistic Regression model. As we saw in chapter 4, this model first computes a weighted sum of the inputs (just like the Linear Regression model), and then it applies the sigmoid function to the result, which gives us the estimated probability for the positive class:
#
# $\hat{p} = h_\boldsymbol{\theta}(\mathbf{x}) = \sigma(\boldsymbol{\theta}^T \mathbf{x})$
#

# Recall that $\boldsymbol{\theta}$ is the parameter vector, containing the bias term $\theta_0$ and the weights $\theta_1, \theta_2, \dots, \theta_n$. The input vector $\mathbf{x}$ contains a constant term $x_0 = 1$, as well as all the input features $x_1, x_2, \dots, x_n$.
#
# Since we want to be able to make predictions for multiple instances at a time, we will use an input matrix $\mathbf{X}$ rather than a single input vector. The $i^{th}$ row will contain the transpose of the $i^{th}$ input vector $(\mathbf{x}^{(i)})^T$. It is then possible to estimate the probability that each instance belongs to the positive class using the following equation:
#
# $ \hat{\mathbf{p}} = \sigma(\mathbf{X} \boldsymbol{\theta})$
#
# That's all we need to build the model:

# In[102]:

X = tf.placeholder(tf.float32, shape=(None, n_inputs + 1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
theta = tf.Variable(tf.random_uniform([n_inputs + 1, 1], -1.0, 1.0, seed=42), name="theta")
logits = tf.matmul(X, theta, name="logits")
y_proba = 1 / (1 + tf.exp(-logits))

# In fact, TensorFlow has a nice function `tf.sigmoid()` that we can use to simplify the last line of the previous code:

# In[103]:

y_proba = tf.sigmoid(logits)

# As we saw in chapter 4, the log loss is a good cost function to use for Logistic Regression:
#
# $J(\boldsymbol{\theta}) = -\dfrac{1}{m} \sum\limits_{i=1}^{m}{\left[ y^{(i)} \log\left(\hat{p}^{(i)}\right) + (1 - y^{(i)}) \log\left(1 - \hat{p}^{(i)}\right)\right]}$
#
# One option is to implement it ourselves:

# In[104]:

epsilon = 1e-7  # to avoid an overflow when computing the log
loss = -tf.reduce_mean(y * tf.log(y_proba + epsilon) + (1 - y) * tf.log(1 - y_proba + epsilon))

# But we might as well use TensorFlow's `tf.losses.log_loss()` function:

# In[105]:

loss = tf.losses.log_loss(y, y_proba)  # uses epsilon = 1e-7 by default

# The rest is pretty standard: let's create the optimizer and tell it to minimize the cost function:

# In[106]:

learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)

# All we need now (in this minimal version) is the variable initializer:

# In[107]:

init = tf.global_variables_initializer()

# And we are ready to train the model and use it for predictions!

# There's really nothing special about this code, it's virtually the same as the one we used earlier for Linear Regression:

# In[108]:

n_epochs = 1000
batch_size = 50
n_batches = int(np.ceil(m / batch_size))

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        for batch_index in range(n_batches):
            X_batch, y_batch = random_batch(X_train, y_train, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        loss_val = loss.eval({X: X_test, y: y_test})
        if epoch % 100 == 0:
            print("Epoch:", epoch, "\tLoss:", loss_val)

    y_proba_val = y_proba.eval(feed_dict={X: X_test, y: y_test})

# Note: we don't use the epoch number when generating batches, so we could just have a single `for` loop rather than 2 nested `for` loops, but it's convenient to think of training time in terms of number of epochs (i.e., roughly the number of times the algorithm went through the training set).

# For each instance in the test set, `y_proba_val` contains the estimated probability that it belongs to the positive class, according to the model. For example, here are the first 5 estimated probabilities:

# In[109]:

y_proba_val[:5]

# To classify each instance, we can go for maximum likelihood: classify as positive any instance whose estimated probability is greater or equal to 0.5:

# In[110]:

y_pred = (y_proba_val >= 0.5)
y_pred[:5]

# Depending on the use case, you may want to choose a different threshold than 0.5: make it higher if you want high precision (but lower recall), and make it lower if you want high recall (but lower precision). See chapter 3 for more details.

# Let's compute the model's precision and recall:

# In[111]:

from sklearn.metrics import precision_score, recall_score

precision_score(y_test, y_pred)

# In[112]:

recall_score(y_test, y_pred)

# Let's plot these predictions to see what they look like:

# In[113]:

y_pred_idx = y_pred.reshape(-1) # a 1D array rather than a column vector
plt.plot(X_test[y_pred_idx, 1], X_test[y_pred_idx, 2], 'go', label="Positive")
plt.plot(X_test[~y_pred_idx, 1], X_test[~y_pred_idx, 2], 'r^', label="Negative")
plt.legend()
plt.show()

# Well, that looks pretty bad, doesn't it? But let's not forget that the Logistic Regression model has a linear decision boundary, so this is actually close to the best we can do with this model (unless we add more features, as we will show in a second).

# Now let's start over, but this time we will add all the bells and whistles, as listed in the exercise:
# * Define the graph within a `logistic_regression()` function that can be reused easily.
# * Save checkpoints using a `Saver` at regular intervals during training, and save the final model at the end of training.
# * Restore the last checkpoint upon startup if training was interrupted.
# * Define the graph using nice scopes so the graph looks good in TensorBoard.
# * Add summaries to visualize the learning curves in TensorBoard.
# * Try tweaking some hyperparameters such as the learning rate or the mini-batch size and look at the shape of the learning curve.

# Before we start, we will add 4 more features to the inputs: ${x_1}^2$, ${x_2}^2$, ${x_1}^3$ and ${x_2}^3$. This was not part of the exercise, but it will demonstrate how adding features can improve the model. We will do this manually, but you could also add them using `sklearn.preprocessing.PolynomialFeatures`.

# In[114]:

X_train_enhanced = np.c_[X_train,
                         np.square(X_train[:, 1]),
                         np.square(X_train[:, 2]),
                         X_train[:, 1] ** 3,
                         X_train[:, 2] ** 3]
X_test_enhanced = np.c_[X_test,
                        np.square(X_test[:, 1]),
                        np.square(X_test[:, 2]),
                        X_test[:, 1] ** 3,
                        X_test[:, 2] ** 3]

# This is what the "enhanced" training set looks like:

# In[115]:

X_train_enhanced[:5]

# Ok, next let's reset the default graph:

# In[116]:

reset_graph()

# Now let's define the `logistic_regression()` function to create the graph. We will leave out the definition of the inputs `X` and the targets `y`. We could include them here, but leaving them out will make it easier to use this function in a wide range of use cases (e.g. perhaps we will want to add some preprocessing steps for the inputs before we feed them to the Logistic Regression model).

# In[117]:

def logistic_regression(X, y, initializer=None, seed=42, learning_rate=0.01):
    n_inputs_including_bias = int(X.get_shape()[1])
    with tf.name_scope("logistic_regression"):
        with tf.name_scope("model"):
            if initializer is None:
                initializer = tf.random_uniform([n_inputs_including_bias, 1], -1.0, 1.0, seed=seed)
            theta = tf.Variable(initializer, name="theta")
            logits = tf.matmul(X, theta, name="logits")
            y_proba = tf.sigmoid(logits)
        with tf.name_scope("train"):
            loss = tf.losses.log_loss(y, y_proba, scope="loss")
            optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
            training_op = optimizer.minimize(loss)
            loss_summary = tf.summary.scalar('log_loss', loss)
        with tf.name_scope("init"):
            init = tf.global_variables_initializer()
        with tf.name_scope("save"):
            saver = tf.train.Saver()
    return y_proba, loss, training_op, loss_summary, init, saver

# Let's create a little function to get the name of the log directory to save the summaries for Tensorboard:

# In[118]:

from datetime import datetime

def log_dir(prefix=""):
    now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
    root_logdir = "tf_logs"
    if prefix:
        prefix += "-"
    name = prefix + "run-" + now
    return "{}/{}/".format(root_logdir, name)

# Next, let's create the graph, using the `logistic_regression()` function. We will also create the `FileWriter` to save the summaries to the log directory for Tensorboard:

# In[119]:

n_inputs = 2 + 4
logdir = log_dir("logreg")

X = tf.placeholder(tf.float32, shape=(None, n_inputs + 1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")

y_proba, loss, training_op, loss_summary, init, saver = logistic_regression(X, y)

file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())

# At last we can train the model! We will start by checking whether a previous training session was interrupted, and if so we will load the checkpoint and continue training from the epoch number we saved. In this example we just save the epoch number to a separate file, but in chapter 11 we will see how to store the training step directly as part of the model, using a non-trainable variable called `global_step` that we pass to the optimizer's `minimize()` method.
#
# You can try interrupting training to verify that it does indeed restore the last checkpoint when you start it again.

# In[120]:

n_epochs = 10001
batch_size = 50
n_batches = int(np.ceil(m / batch_size))

checkpoint_path = "/tmp/my_logreg_model.ckpt"
checkpoint_epoch_path = checkpoint_path + ".epoch"
final_model_path = "./my_logreg_model"

with tf.Session() as sess:
    if os.path.isfile(checkpoint_epoch_path):
        # if the checkpoint file exists, restore the model and load the epoch number
        with open(checkpoint_epoch_path, "rb") as f:
            start_epoch = int(f.read())
        print("Training was interrupted. Continuing at epoch", start_epoch)
        saver.restore(sess, checkpoint_path)
    else:
        start_epoch = 0
        sess.run(init)

    for epoch in range(start_epoch, n_epochs):
        for batch_index in range(n_batches):
            X_batch, y_batch = random_batch(X_train_enhanced, y_train, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        loss_val, summary_str = sess.run([loss, loss_summary], feed_dict={X: X_test_enhanced, y: y_test})
        file_writer.add_summary(summary_str, epoch)
        if epoch % 500 == 0:
            print("Epoch:", epoch, "\tLoss:", loss_val)
            saver.save(sess, checkpoint_path)
            with open(checkpoint_epoch_path, "wb") as f:
                f.write(b"%d" % (epoch + 1))

    saver.save(sess, final_model_path)
    y_proba_val = y_proba.eval(feed_dict={X: X_test_enhanced, y: y_test})
    os.remove(checkpoint_epoch_path)

# Once again, we can make predictions by just classifying as positive all the instances whose estimated probability is greater or equal to 0.5:

# In[121]:

y_pred = (y_proba_val >= 0.5)

# In[122]:

precision_score(y_test, y_pred)

# In[123]:

recall_score(y_test, y_pred)

# In[124]:

y_pred_idx = y_pred.reshape(-1) # a 1D array rather than a column vector
plt.plot(X_test[y_pred_idx, 1], X_test[y_pred_idx, 2], 'go', label="Positive")
plt.plot(X_test[~y_pred_idx, 1], X_test[~y_pred_idx, 2], 'r^', label="Negative")
plt.legend()
plt.show()

# Now that's much, much better! Apparently the new features really helped a lot.

# Try starting the tensorboard server, find the latest run and look at the learning curve (i.e., how the loss evaluated on the test set evolves as a function of the epoch number):
#
# ```
# $ tensorboard --logdir=tf_logs
# ```

# Now you can play around with the hyperparameters (e.g. the `batch_size` or the `learning_rate`) and run training again and again, comparing the learning curves. You can even automate this process by implementing grid search or randomized search. Below is a simple implementation of a randomized search on both the batch size and the learning rate. For the sake of simplicity, the checkpoint mechanism was removed.

# In[125]:

from scipy.stats import reciprocal

n_search_iterations = 10

for search_iteration in range(n_search_iterations):
    batch_size = np.random.randint(1, 100)
    learning_rate = reciprocal(0.0001, 0.1).rvs(random_state=search_iteration)

    n_inputs = 2 + 4
    logdir = log_dir("logreg")

    print("Iteration", search_iteration)
    print("  logdir:", logdir)
    print("  batch size:", batch_size)
    print("  learning_rate:", learning_rate)
    print("  training: ", end="")

    reset_graph()

    X = tf.placeholder(tf.float32, shape=(None, n_inputs + 1), name="X")
    y = tf.placeholder(tf.float32, shape=(None, 1), name="y")

    y_proba, loss, training_op, loss_summary, init, saver = logistic_regression(
        X, y, learning_rate=learning_rate)

    file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())

    n_epochs = 10001
    n_batches = int(np.ceil(m / batch_size))

    final_model_path = "./my_logreg_model_%d" % search_iteration

    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(n_epochs):
            for batch_index in range(n_batches):
                X_batch, y_batch = random_batch(X_train_enhanced, y_train, batch_size)
                sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
            loss_val, summary_str = sess.run([loss, loss_summary], feed_dict={X: X_test_enhanced, y: y_test})
            file_writer.add_summary(summary_str, epoch)
            if epoch % 500 == 0:
                print(".", end="")

        saver.save(sess, final_model_path)

        print()
        y_proba_val = y_proba.eval(feed_dict={X: X_test_enhanced, y: y_test})
        y_pred = (y_proba_val >= 0.5)

        print("  precision:", precision_score(y_test, y_pred))
        print("  recall:", recall_score(y_test, y_pred))

# The `reciprocal()` function from SciPy's `stats` module returns a random distribution that is commonly used when you have no idea of the optimal scale of a hyperparameter. See the exercise solutions for chapter 2 for more details.


import torch
from VQ_DR.deezer.models import YoutubeDNN
from VQ_DR.deezer.models.layers import MLP_Layer

class YoutubeDNN_DNN(YoutubeDNN):
    def __init__(self, n_items, item_embedding, user_input_dim, embedding_size, l2_reg, dropout):
        super(YoutubeDNN_DNN, self).__init__(n_items, item_embedding, user_input_dim, embedding_size, l2_reg, dropout)
        self.logits_dnn = MLP_Layer(input_dim=embedding_size*2,
                             output_dim=1,
                             hidden_units=[400, 200, 100],
                             hidden_activations="ReLU",
                             output_activation=None,
                             dropout_rates=dropout,
                             batch_norm=False,
                             use_bias=True)

    def get_logits(self, user_embedding, item_embedding):
        interaction_input = torch.concat([user_embedding, item_embedding], dim=-1)  # (B, Num_Neg or 1, D+D)
        logits = self.logits_dnn(interaction_input).squeeze(-1) # (B, Num_Neg or 1)
        return logits

    def forward(self, input_dict):
        user_features, pos_items, neg_items = input_dict['user_features'], input_dict['pos_items'], input_dict['neg_items']
        Num_Neg = neg_items.shape[1]
        user_embedding = self.user_encoder(user_features)
        pos_item_embedding = self.item_encoder(pos_items)
        neg_item_embedding = self.item_encoder(neg_items)  # B, Num_Neg, D
        pos_logits = self.get_logits(user_embedding.unsqueeze(1), pos_item_embedding.unsqueeze(1)).squeeze(1)
        neg_logits = self.get_logits(user_embedding.unsqueeze(1).repeat(1, Num_Neg, 1), neg_item_embedding)
        softmax_loss = self.softmax_loss(pos_logits, neg_logits)
        # reg_loss = self.l2_loss(hist_items)
        loss = softmax_loss # + reg_loss
        return loss


import grpc
import service_pb2 as pb2
import service_pb2_grpc as pb2_grpc
import ast
from concurrent.futures import ThreadPoolExecutor  # Import ThreadPoolExecutor from concurrent.futures

# Server Implementation
class FunctionServiceServicer(pb2_grpc.FunctionServiceServicer):
    def ExecuteFunction(self, request, context):
        # Extract the code and arguments from the request
        code = request.code
        args = request.args
        output_file = open("/server_output.log", "w")

        # Execute the code dynamically
        try:
            # Parse the code
            code_module = ast.parse(code)
            # Create a namespace for the code to execute in
            code_namespace = {"__builtins__": {}}
            # Redirect stdout to the custom output file
            code_namespace["__builtins__"]["print"] = lambda *args, **kwargs: print(*args, file=output_file, **kwargs)
            # Execute the code in the created namespace
            exec(compile(code_module, '<string>', 'exec'), code_namespace)
            # Call the function
            result = code_namespace['client_function'](*args)
        except Exception as e:
            result = str(e)

        output_file.close()  # Close the custom output file
        return pb2.FunctionResponse(result=str(result))

def serve():
    executor = ThreadPoolExecutor(max_workers=10)  # Use ThreadPoolExecutor from concurrent.futures
    server = grpc.server(executor)
    pb2_grpc.add_FunctionServiceServicer_to_server(FunctionServiceServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    serve()


from multiprocessing.connection import wait
from emails import emails
from jokes import joke_list
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import random
import time

#getting random joke
num = random.randint(0,59)
joke = joke_list[num]

#getting a random email
rand_email = random.randint(0,9)
email = emails[rand_email]

#setting driver to firefox and going to nate's site
PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://apexsoccer.org")

#enter eric jones into nate's "Name" field of his user form
elem = driver.find_element(By.ID, "form-field-name")
elem.clear()
elem.send_keys("Eric Jones")

#enter a ridiculous email into the "Email field"
second_elem = driver.find_element(By.ID, "form-field-email")
second_elem.clear()
second_elem.send_keys(email)

#enter the joke into the message field and submit
third_elem = driver.find_element(By.ID, "form-field-6ed25c0")
third_elem.clear()
third_elem.send_keys(joke)

click_elem = driver.find_element(By.TAG_NAME, "button")
click_elem.click()

time.sleep(3)
#close my connection
driver.close()


from fpdf import FPDF
from PIL import Image
from random import randint
import numpy, glob, os, requests

URL_initial = 'http://www.printable-sudoku-puzzles.com/samurai/unsolved.php?d='
URL_final = '&w='

def main():
	def downloadSudoku(level, index):
		sudokuUrl = URL_initial + str(level) + URL_final + str(index)
		r = requests.get(sudokuUrl)
		with open('sudoku'+str(index)+'.png', 'wb') as f:
			for chunk in r:
				f.write(chunk)

	def makePdf(pdfFileName, listImages, dir = ' '):
		nonlocal number_input
		if (dir):
			dir += "/"

		pdf = FPDF()
		counter = 0
		for image in listImages:
			while counter < number_input:
				pdf.add_page()
				pdf.image(dir + 'sudoku' + str(number[counter]) + '.png', 13, 30)
				os.remove(dir + 'sudoku' + str(number[counter]) + '.png')
				counter += 1
		pdf.output(dir + pdfFileName + '.pdf', "F")
		print("Thank You for your patience. Your PDF has been saved.")

	print("This program is meant to download Samurai Sudoku puzzles off the internet and combine it into a single PDF.")
	number_input = int(input("How many sudoku puzzles would you like to download? Please enter a number between 1 and 2000: "))
	number = []
	difficulty = input("What level of difficulty would you like your puzzles to be? Please choose a level between 1 to 7 with 1 being the easiest and 7 being the hardest: ")
	name = input("What would you like to name the PDF file? ")
	print("Please wait while your sudokus get downloaded and saved into one PDF.")
	for i in range(number_input):
		number.append(randint(0, 2001))
		downloadSudoku(difficulty, number[i])

	imageFolder = '/Users/raaghavgoel/Desktop/GitHub/Sudoku'
	imagePath = glob.glob(imageFolder + '/*.png')
	Image_List = numpy.array( [numpy.array(Image.open(img).convert('L'), 'f') for img in imagePath] )
	makePdf(str(name), Image_List, dir = imageFolder)

if __name__ == "__main__":
	main()


import argparse
import os

VALID_RESOURCES = frozenset(
    ["deployments", "statefulsets", "stacks", "cronjobs", "horizontalpodautoscalers"]
)

def check_include_resources(value):
    resources = frozenset(value.split(","))
    if not resources <= VALID_RESOURCES:
        raise argparse.ArgumentTypeError(
            f"--include-resources argument should contain a subset of [{', '.join(sorted(VALID_RESOURCES))}]"
        )
    return value

def get_parser():
    parser = argparse.ArgumentParser()
    upscale_group = parser.add_mutually_exclusive_group(required=False)
    downscalescale_group = parser.add_mutually_exclusive_group(required=False)
    parser.add_argument(
        "--dry-run",
        help="Dry run mode: do not change anything, just print what would be done",
        action="store_true",
    )
    parser.add_argument(
        "--debug", "-d", help="Debug mode: print more information", action="store_true"
    )
    parser.add_argument(
        "--once", help="Run loop only once and exit", action="store_true"
    )
    parser.add_argument(
        "--interval", type=int, help="Loop interval (default: 30s)", default=30
    )
    parser.add_argument("--namespace", help="Namespace")
    parser.add_argument(
        "--include-resources",
        type=check_include_resources,
        default="deployments",
        help=f"Downscale resources of this kind as comma separated list. [{', '.join(sorted(VALID_RESOURCES))}] (default: deployments)",
    )
    parser.add_argument(
        "--grace-period",
        type=int,
        help="Grace period in seconds for deployments before scaling down (default: 15min)",
        default=900,
    )
    upscale_group.add_argument(
        "--upscale-period",
        help="Default time period to scale up once (default: never)",
        default=os.getenv("UPSCALE_PERIOD", "never"),
    )
    upscale_group.add_argument(
        "--default-uptime",
        help="Default time range to scale up for (default: always)",
        default=os.getenv("DEFAULT_UPTIME", "always"),
    )
    downscalescale_group.add_argument(
        "--downscale-period",
        help="Default time period to scale down once (default: never)",
        default=os.getenv("DOWNSCALE_PERIOD", "never"),
    )
    downscalescale_group.add_argument(
        "--default-downtime",
        help="Default time range to scale down for (default: never)",
        default=os.getenv("DEFAULT_DOWNTIME", "never"),
    )
    parser.add_argument(
        "--exclude-namespaces",
        help="Exclude namespaces from downscaling, comma-separated list of regex patterns (default: kube-system)",
        default=os.getenv("EXCLUDE_NAMESPACES", "kube-system"),
    )
    parser.add_argument(
        "--exclude-deployments",
        help="Exclude specific deployments from downscaling (default: kube-downscaler,downscaler)",
        default=os.getenv("EXCLUDE_DEPLOYMENTS", "kube-downscaler,downscaler"),
    )
    parser.add_argument(
        "--downtime-replicas",
        type=int,
        help="Default amount of replicas when downscaling (default: 0)",
        default=int(os.getenv("DOWNTIME_REPLICAS", 0)),
    )
    parser.add_argument(
        "--deployment-time-annotation",
        help="Annotation that contains a resource's last deployment time, overrides creationTime. Use in combination with --grace-period.",
    )
    parser.add_argument(
        "--enable-events",
        help="Emit Kubernetes events for scale up/down",
        action="store_true",
    )
    return parser


from django.test import TestCase
from api.models import Food
from api.models import Meal
from rest_framework.test import APIClient
from rest_framework import status
from django.urls import reverse
from rest_framework.test import APIRequestFactory
from api.views import FoodViews
class MealViewsTest(TestCase):
    """Test suite for the meal api views."""

    def setUp(self):
        """Define the test client and other test variables."""
        self.client = APIClient()
        self.apple = Food.objects.create(name="apple", calories=50)
        self.oatmeal = Food.objects.create(name="oatmeal", calories=400)
        self.breakfast = Meal.objects.create(name="breakfast")
        self.snack = Meal.objects.create(name="snack")
        self.lunch = Meal.objects.create(name="lunch")
        self.dinner = Meal.objects.create(name="dinner")
        self.breakfast.foods.add(self.apple)

    def test_gets_all_meals(self):
        response = self.client.get('/api/v1/meals/')
        js = response.json()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(js), 4)
        self.assertEqual(js[0]["name"], self.breakfast.name)
        self.assertEqual(js[1]["name"], self.snack.name)
        self.assertEqual(js[2]["name"], self.lunch.name)
        self.assertEqual(js[3]["name"], self.dinner.name)

    def test_gets_single_meal(self):
        response = self.client.get(f'/api/v1/meals/{self.breakfast.id}/foods')

        meal_response = response.json()

        self.assertEqual(meal_response['name'], 'breakfast')
        self.assertEqual(meal_response['foods'][0]['name'], self.apple.name)
        self.assertEqual(meal_response['foods'][0]['calories'], self.apple.calories)

    def test_api_can_add_food_to_a_meal(self):
        """Test the api can add food to a meal capability."""
        response = self.client.post(f'/api/v1/meals/{self.breakfast.id}/foods/{self.oatmeal.id}')
        # import code; code.interact(local=dict(globals(), **locals()))

        self.assertEqual(response.data['message'], "Successfully added oatmeal to breakfast")

    def test_api_can_delete_food_from_a_meal(self):
        """Test the api can delete a food from a meal"""
        response = self.client.delete(f'/api/v1/meals/{self.breakfast.id}/foods/{self.apple.id}')
        # import code; code.interact(local=dict(globals(), **locals()))

        self.assertEqual(response.data['message'], "Successfully removed apple from breakfast")


import os
from stat import ST_CTIME, ST_MODE, S_ISREG

import numpy as np
import cv2
from cv_bridge import CvBridge, CvBridgeError
import rospy
import time
from edgecar_msgs.msg import WheelsCmdStamped
from std_msgs.msg import Bool
from sensor_msgs.msg import CompressedImage

from scanner import Scanner

import config as cfg
import masks as masks

class DrivingModule:
    def __init__(self):
        self.vehicle_name = rospy.get_param("~veh", "edgecar")
        self.start_driving_subscription_name = "/{}/ai/start_driving".format(self.vehicle_name)
        self.image_subscription_name = '/{}/camera_node/image/compressed'.format(self.vehicle_name)
        self.pub_car_cmd_name = '/{}/wheels_driver_node/wheels_cmd'.format(self.vehicle_name)

        self.sub_start_using_ai = rospy.Subscriber(self.start_driving_subscription_name, Bool, self.start_using_ai)

        self.pub_car_cmd = rospy.Publisher(self.pub_car_cmd_name, WheelsCmdStamped, queue_size=1)
        self.wheels_cmd = WheelsCmdStamped()

        self.scanner = Scanner()

        self.image_subscriber = None  # will be used later on when we start using AI
        self.running = False
        self.kl = None

        self.counter = 0

        self.bridge = CvBridge()

def start_using_ai(self, data):
        if data.data:
            rospy.loginfo("[{}] Start Driving making use of CV AI : {}".format(rospy.get_name(), data.data))
            self.running = True
            self.image_subscriber = rospy.Subscriber(self.image_subscription_name, CompressedImage, self.process_img)
            rospy.loginfo("[{}] Subscribed to image topic".format(rospy.get_name()))
        elif not data.data:
            self.stop_running()

def process_img(self, data):
    if data:
        try:
            angle = cfg.MAX_ANGLE
            throttle = cfg.MAX_THROTTLE

            img = self.bridge.compressed_imgmsg_to_cv2(data)
            hsv_data = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

            mask = None
            image_result = None

            mask_bounds = masks.limits[cfg.LOCATION]
            mask = cv2.inRange(hsv_data, np.array(mask_bounds[0][0]), np.array(mask_bounds[0][1]))
            mask2 = cv2.inRange(hsv_data, np.array(mask_bounds[1][0]), np.array(mask_bounds[1][1]))
            total_mask = mask | mask2
            total_mask = cv2.erode(total_mask, (3, 3), 2)
            total_mask = cv2.dilate(total_mask, (3, 3), 2)

            image_result = cv2.bitwise_and(img, img, mask=total_mask)

            self.scanner.set_image(total_mask)
            #d = self.scanner.find_direction()
            #p = Plotter.plot_direction(image_result, d)
            (target, absdir, left, right) = self.scanner.direction_exp()
            #p = Plotter.plot_direction_exp(image_result, d)

            angle = angle * absdir
            throttle = throttle

            self.wheels_cmd.header.stamp = data.header.stamp
            self.wheels_cmd.velocity = throttle
            self.wheels_cmd.rotation = angle
            self.pub_car_cmd.publish(self.wheels_cmd)

            if self.counter % 20 == 0:
                rospy.loginfo("[{}] outputs : {}".format(rospy.get_name(), outputs))
                self.counter = 0
            self.counter += 1

        except CvBridgeError as e:
            rospy.logerr("error cvbridge stuff : ", e)

    def stop_running(self):
        rospy.loginfo("[{} Must stop running AI container".format(rospy.get_name()))
        if not self.image_subscriber is None:
            self.image_subscriber.unregister()
            self.image_subscriber = None
            rospy.logdebug("[{}] Finished unsubscribing from image stream".format(rospy.get_name()))
        self.running = False

    def on_shutdown(self):
        rospy.loginfo('[DrivingModule] onShutdown ...')

if __name__ == '__main__':
    rospy.init_node('driving_node', anonymous=False)
    driving_module = DrivingModule()
    rospy.on_shutdown(driving_module.on_shutdown)
    rospy.spin()


import csv
import os

from datetime import datetime

from collections import Counter
import itertools
import matplotlib.pyplot as plt

files = os.listdir('csv_commits')

cumulative = Counter()
for file in files:
    timeseries = []
    with open('csv_commits/' + file, 'r') as fd:
        reader = csv.reader(fd)
        next(reader)
        # row = [datetime, value]
        for row in reader:
            timeseries.append((datetime.strptime(row[0], '%Y/%m').year, int(row[1])))

    for year, values in itertools.groupby(timeseries, lambda record: record[0]):
        year_sum = sum([value for _, value in values])
        cumulative[year] += year_sum

sorted_series = sorted(cumulative.items())

with open('commits_by_year.csv', 'w+') as fd:
    writer = csv.writer(fd)
    writer.writerow(['year', 'commits'])

    for row in sorted_series:
        writer.writerow(row)

plt.scatter(*zip(*sorted_series))
plt.grid()
plt.show()


from flask import Flask
import requests
import mongo_prod

app = Flask(__name__)

def ga_maker():
    res = requests.request(method='GET', url='http://0.0.0.0:9020/ga_maker_value?delt_name=bbt2_kr_s1_ios_tz_a00&size=1000')
    print(res.text)

def get_url():
    out = mongo_prod.test_ga_maker()
    lst = list()
    for ot in set(out):
        print('http://0.0.0.0:9020/ga_maker_value?delt_name=%s&size=100' % ot)

get_url()


# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
# Filename: EmRecoverUtilClusterLink.py
'''
Utility for restoration(Inter-cluster link creation)
'''

import json
import EmRecoverUtilBase
from EmCommonLog import decorater_log

class EmRecoverUtilClusterLink(EmRecoverUtilBase.EmRecoverUtilBase):
    '''
    Utility for restoration
    '''

    @decorater_log
    def __init__(self):
        '''
        Constructor
        '''

        super(EmRecoverUtilClusterLink, self).__init__()

    @decorater_log
    def _create_recover_ec_message(self, ec_message, db_info):
        '''
        Create inter-cluster link generation EC message for restoration
        Parameter:
            ec_message : EC message for "recover service"
            db_info : DB information
        Return Value :
            Result : Success：True  Failure：False
            EC message for restoration(JSON)
        '''
        self.common_util_log.logging(
            " ", self.log_level_debug, "db_info = %s" % db_info, __name__)
        self.common_util_log.logging(
            " ", self.log_level_debug, "ec_message = %s" %
            ec_message, __name__)

        return_ec_message = \
            {
                "device":
                {
                    "name": None,
                    "cluster-link-physical-interface_value": 0,
                    "cluster-link-physical-interface": [],
                    "cluster-link-lag-interface_value": 0,
                    "cluster-link-lag-interface": []
                }
            }

        self._gen_json_name(return_ec_message, db_info, ec_message)

        self._gen_json_cluster_link_phy(return_ec_message, db_info, ec_message)

        self._gen_json_cluster_link_lag(return_ec_message, db_info, ec_message)

        if return_ec_message["device"][
                "cluster-link-physical-interface_value"] == 0 and \
            return_ec_message["device"][
                "cluster-link-lag-interface_value"] == 0:
            self.common_util_log.logging(
                " ", self.log_level_debug,
                "Need not recover CLUSTER-LINK.", __name__)
            return True, None

        self.common_util_log.logging(
            " ", self.log_level_debug,
            "return_ec_message = %s" % return_ec_message, __name__)

        return True, json.dumps(return_ec_message)

    @decorater_log
    def _create_recover_db_info(self, ec_message, db_info):
        '''
        Create inter-cluster link creation DB information for restoration
        Parameter:
            ec_message : EC message for "recover service"
            db_info : DB information
        Return Value :
            Result : Success：True  Failure：False
            DB information for restoration(JSON)
        '''
        return_db_info = db_info.get(self.name_cluster_link)
        for cl_if in return_db_info.get("cluster-link_info"):
            if cl_if.get("type") == self._if_type_phy:
                if_type = self._if_type_phy
            else:
                if_type = self._if_type_lag
            cl_if["name"] = self._convert_ifname(
                cl_if.get("name"), ec_message, if_type)
        for phys_if in return_db_info.get("physical-interface"):
            phys_if["name"] = self._convert_ifname(
                phys_if.get("name"), ec_message, self._if_type_phy)
        for lag_if in return_db_info.get("lag"):
            lag_if["if_name"] = self._convert_ifname(
                lag_if.get("if_name"), ec_message, self._if_type_lag)
        for lag_mem_if in return_db_info.get("lag_member"):
            lag_mem_if["lag_if_name"] = self._convert_ifname(
                lag_mem_if.get("lag_if_name"), ec_message, self._if_type_lag)
            lag_mem_if["if_name"] = self._convert_ifname(
                lag_mem_if.get("if_name"), ec_message, self._if_type_phy)

        self.common_util_log.logging(
            " ", self.log_level_debug,
            "return_db_info = %s" % return_db_info, __name__)

        return True, json.dumps(return_db_info)

    @decorater_log
    def _gen_json_cluster_link_phy(self, json, db_info, ec_message):
        '''
            Set the inter-cluster link information (physical) to the EC message storage dictionary object
            to be used for restoration based on restoration expansion/EC message for "recover service"
            and DB information
            Explanation about parameter：
                json：EC message storage dictionary object
                db_info : DB information
                ec_message : EC message for restoration expansion/"recover service"
        '''
        cluster_links_db = db_info.get(self.name_cluster_link, {})

        for cluster_link_db in cluster_links_db.get("cluster-link_info", []):
            if cluster_link_db.get("type") == self._if_type_phy:
                old_name = cluster_link_db.get("name")
                name = self._convert_ifname(
                    old_name, ec_message, self._if_type_phy)
                address = cluster_link_db.get("address")
                prefix = cluster_link_db.get("prefix")
                condition = None
                for phys in cluster_links_db.get("physical-interface", []):
                    if phys.get("name") == old_name:
                        condition = phys.get("condition")
                        break
                metric = cluster_link_db.get("ospf").get("metric")
                json["device"]["cluster-link-physical-interface"].append(
                    {"name": name,
                     "address": address,
                     "prefix": prefix,
                     "condition": condition,
                     "ospf": {"metric": metric}})

        json["device"]["cluster-link-physical-interface_value"] = len(
            json["device"]["cluster-link-physical-interface"])

    @decorater_log
    def _gen_json_cluster_link_lag(self, json, db_info, ec_message):
        '''
            Set the inter-cluster link information (LAG) to the EC message storage dictionary object
            to be used for restoration based on restoration expansion/EC message for "recover service"
            and DB information
            Explanation about parameter：
                json：EC message storage dictionary object
                db_info : DB information
                ec_message : EC message for restoration expansion/"recover service"
        '''
        cluster_links_db = db_info.get(self.name_cluster_link, {})

        for cluster_link_db in cluster_links_db.get("cluster-link_info", []):
            if cluster_link_db.get("type") == self._if_type_lag:
                cl_link_list = \
                    {
                        "name": None,
                        "address": None,
                        "prefix": None,
                        "leaf-interface_value": 0,
                        "leaf-interface": [],
                        "ospf": None
                    }
                old_lag_name = cluster_link_db.get("name")
                name = self._convert_ifname(
                    old_lag_name, ec_message, self._if_type_lag)
                cl_link_list["name"] = name
                cl_link_list["address"] = cluster_link_db.get("address")
                cl_link_list["prefix"] = cluster_link_db.get("prefix")

                for lag_mem_db in cluster_links_db.get("lag_member", []):
                    if lag_mem_db.get("lag_if_name") == old_lag_name:
                        old_lag_mem = lag_mem_db.get("if_name")
                        member_if_name = self._convert_ifname(
                            old_lag_mem, ec_message, self._if_type_phy)
                        condition = None
                        for phys in cluster_links_db.get(
                                "physical-interface", []):
                            if phys.get("name") == old_lag_mem:
                                condition = phys.get("condition")
                                break
                        cl_link_list["leaf-interface"].append({
                            "name": member_if_name,
                            "condition": condition})
                cl_link_list["leaf-interface_value"] = \
                    len(cl_link_list["leaf-interface"])

                metric = cluster_link_db.get("ospf").get("metric")
                cl_link_list["ospf"] = {"metric": metric}

                json["device"][
                    "cluster-link-lag-interface"].append(cl_link_list)

        json["device"]["cluster-link-lag-interface_value"] = \
            len(json["device"]["cluster-link-lag-interface"])


from pandas.core.frame import DataFrame
from sklearn.neural_network import MLPRegressor
from typing import List

def train(data: DataFrame, response_vars: List[str]) -> MLPRegressor:
    # split response and predictor vars
    y = data[response_vars]
    predictor_vars = data.columns.difference(response_vars)
    X = data[predictor_vars]
    # create and fit the model to the data
    model = MLPRegressor()
    model.fit(X, y)

    return model


import os
import sys
from io import StringIO
import discord

def main():
  client = discord.Client()

  @client.event
  async def on_ready():
    print("C++ bot is alive")

  @client.event
  async def on_message(message):

    if message.author == client.user:
      return

    if message.content.startswith("!hello"):
      await message.channel.send(f"hello")

    if message.content.startswith("!runPython"):
      # remove the command and
      message.content = message.content[len("!runPython")+1:]
      code = '\n'.join(message.content.split('\n'))
      save_stdout = sys.stdout
      output = StringIO()
      sys.stdout = output
      exec(code)
      await message.channel.send(output.getvalue())
      sys.stdout = save_stdout
      print(output.getvalue())

    if message.content.startswith("!runC++"):
      message.content = message.content[len("!runC++")+1:]
      code = '\n'.join(message.content.split('\n'))
      # write the code in the code.cpp
      with open('code.cpp', 'w') as f:
        f.write(code)
      # use terminal commands to compile and run it
      os.system('clang++-7 -pthread -std=c++17 -o out code.cpp')
      os.system('./out > out.txt')
      #return the output
      with open('out.txt', 'r') as f:
        output = f.read()
      await message.channel.send(output)

  client.run(TOKEN)

if __name__ == "__main__":
  main()


import argparse
import logging
import multiprocessing
import signal
import sys

from gsuite_grant_analyzer.bigquery_helpers import (bq_create_client,
                                                    bq_create_dataset,
                                                    bq_create_table,
                                                    bq_insert_rows)

from gsuite_grant_analyzer.google_api_helpers import (
    create_delegated_credentials, create_directory_client,
    get_denormalized_scopes_for_user, get_users)

# Hard limit on the number of processes from the command line. This limit can
# be raised, however, expect more throttling from the APIs as you go up.
MAX_PROCESSES = 8

# These scopes must be granted in G Suite Admin Console to the service account
# client. See README.md file for more information on setup.
SCOPES = [
    "https://www.googleapis.com/auth/admin.directory.user.readonly",
    "https://www.googleapis.com/auth/admin.directory.user.security",
    "https://www.googleapis.com/auth/bigquery",
    "https://www.googleapis.com/auth/cloud-platform"
]

logging.basicConfig(filename="gsuite_grant_analyzer.log",
                    level=logging.DEBUG,
                    format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s")

logger = logging.getLogger(__name__)

terminate_processes = multiprocessing.Value("i", 0)

def handle_signal(s, f):
    """Handles termination by CTRL-C.

  Args:
    s: signal
    f: frame
  """
    del s
    del f
    global terminate_processes
    terminate_processes = multiprocessing.Value("i", 1)

def parse_args(args):
    """Parses script arguments.

  Args:
    args: script arguments

  Returns:
    Parsed arguments
  """
    parser = argparse.ArgumentParser(
        description="Finds applications and scopes for all users in a given "
        "OU and loads them into BigQuery")
    parser.add_argument("--domain", help="Domain", type=str, required=True)
    parser.add_argument("--ou",
                        help="Organizational Unit path, default is /",
                        type=str,
                        required=False,
                        default="/")
    parser.add_argument("--sa",
                        help="Path to Service Account credentials",
                        type=str,
                        required=True)
    parser.add_argument("--admin",
                        help="Administrator email",
                        type=str,
                        required=True)
    parser.add_argument("--project",
                        help="Project where BigQuery dataset will be created",
                        type=str,
                        required=True)
    parser.add_argument("--dry-run",
                        help="Do not load data into BigQuery",
                        required=False,
                        dest="dry_run",
                        action="store_true",
                        default=False)
    parser.add_argument("--processes",
                        help="Number of concurrent processes (default: 4)",
                        type=int,
                        required=False,
                        dest="num_processes",
                        default=4)

    return parser.parse_args(args)

def build_rows_process(queue, users, directory_client, process_number,
                       num_processes):
    """A process functions to process all users in parallel.

  Gets all users, but processes and builds the rows only for the subset of
  users assigned to the process.

  Args:
    queue: shared synchronized queue where results are stored
    users: list of all users
    directory_client: client for the Directory SDK API
    process_number: number of this process, from 0 to num_processes
    num_processes: total number of processes
  """
    rows = []
    total_users = len(users)
    for i, user in enumerate(users, 1):
        if terminate_processes.value:
            break
        if i % num_processes == process_number:
            logger.debug("Process #%s - User %d/%d: %s", process_number, i,
                         total_users, user)
            rows.extend(get_denormalized_scopes_for_user(
                directory_client, user))

    # Post results in the queue, if not terminating
    if not terminate_processes.value:
        queue.put({
            "process_number": process_number,
            "message_type": "DATA",
            "data": rows
        })

    # Post process complete message
    queue.put({"process_number": process_number, "message_type": "STOP"})

def analyze():
    """Main function for G Suite Grant Analyzer."""
    signal.signal(signal.SIGINT, handle_signal)
    signal.signal(signal.SIGTERM, handle_signal)

    args = parse_args(sys.argv[1:])

    logger.info("### G Suite Grant Analyzer ###")
    logger.debug("Domain=%s", args.domain)
    logger.debug("OU=%s", args.ou)
    logger.debug("Admin=%s", args.admin)
    logger.debug("SA=%s", args.sa)
    logger.debug("Project=%s", args.project)
    logger.debug("Dry run=%s", args.dry_run)
    logger.debug("Processes=%d", args.num_processes)

    if args.num_processes > MAX_PROCESSES:
        logger.error(
            "The maximum number of processes is %d. Please, edit your command "
            "line.", MAX_PROCESSES)
        exit(1)

    # Get credentials for service account and create services and clients
    logger.info("Creating credentials...")
    delegated_credentials = create_delegated_credentials(
        service_account_file=args.sa, scopes=SCOPES, user=args.admin)
    logger.info("Creating Directory API client...")
    directory = create_directory_client(delegated_credentials)

    if not args.dry_run:
        logger.info("Setting up BigQuery...")
        bq_client = bq_create_client(args.project, delegated_credentials)
        dataset = bq_create_dataset(bq_client)
        table = bq_create_table(bq_client, dataset)

    logger.info("Setup complete.")

    logger.info("Getting user list...")
    users = get_users(directory, args.domain, args.ou)
    total_users = len(users)
    logger.info("Got %d users", total_users)

    # Shared queue for process messages
    queue = multiprocessing.Queue()

    # Stores running processes by process number
    processes = {}

    # Create and start processes
    for process_number in range(0, args.num_processes):
        logger.info("Creating process %d", process_number)
        process = multiprocessing.Process(target=build_rows_process,
                                          args=(queue, users, directory,
                                                process_number,
                                                args.num_processes))
        processes[process_number] = process
        process.start()

    total_rows = 0

    # Process results until all processes have terminated
    while processes:
        message = queue.get()
        process_number = message["process_number"]
        message_type = message["message_type"]
        if message_type == "DATA":
            # Insert data in BQ
            if not args.dry_run and not terminate_processes.value:
                logger.info("Inserting data from process #%d", process_number)
                data = message["data"]
                total_rows += len(data)
                bq_insert_rows(bq_client, table, data)
        elif message_type == "STOP":
            # Remove completed process from running processes
            processes[process_number].join()
            logger.info("Process #%d completed", process_number)
            del processes[process_number]
        else:
            logger.error("Unknown message %s from process",
                         message["message_type"])
            exit(1)

    logger.info("All processes terminated - %d rows processed", total_rows)

if __name__ == "__main__":
    analyze()


import math
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

def calc(x):
	return str( math.log (abs ( 12 * math.sin( x ) ) ) )

opt = webdriver.ChromeOptions()
opt.add_experimental_option('w3c', False)

browser = webdriver.Chrome(options=opt)
link = "http://suninjuly.github.io/explicit_wait2.html"
browser.get(link)

button = browser.find_element_by_id("book")
price = WebDriverWait(browser, 12).until(
	EC.text_to_be_present_in_element((By.ID, "price"), "$100")
)
button.click()

x_string = browser.find_element_by_id("input_value")
x_number = int( x_string.text )

answer = calc(x_number)

input_answer = browser.find_element_by_id("answer")
input_answer.send_keys(answer)

send_button = browser.find_element_by_id("solve")
send_button.click()
time.sleep(10)
browser.quit()


import cv2

camera=cv2.VideoCapture(0)

width=int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))#bu satırda kameramızın çerçevesinin genişliğini int biçimde aldık
height=int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))#bu satırda kameramın çerçevesinin yüksekliğini int biçimde aldık

print(width,height)

fourcc=cv2.VideoWriter_fourcc(*"MPV4")#bu satırda cv2.VideoWriter_fourcckomutuyla videomuzun hangi formatta kayıt
#edileceğini belirleyip fourcc değişkenine atadık

writer=cv2.VideoWriter("kayit.mp4",fourcc,17,(width,height))#1. değişkende kayıt dosyamızın ismini 2. değişkende
#kayıt formatımızı (üstte kayıt formatını cv2.VideoWriter_fourcc komtuyla fourcc değişkenine atadık) 3. değişkende
#bir saniyede kaç frame kaydedeceğini (FPS) 4. değişkende kayıt dosyamızın genişlik ve yüksekliğini belirleyip bir değişkene
#atıyoruz. Videomuz şu an kaydedilmiyor sadece kayıt işleminin nasıl yapılacağı bilgilerini bir değişkene atadık

while True:
    ret,image=camera.read()
    image=cv2.flip(image,1)#kameradan alınan görüntünün yatay veya dikey çevirilmesini sağlar
    #0-> x ekseninde yani dikey 1-> y ekseninde yani yatay -1-> ise hem dikey hem de yatay eksene görünntüyü çevirir
    writer.write(image)#kayıt işlei bu satırda .write komutuyla yapılıyor kayıt özelliklerini writer değişkeninden
    #alıyor
    cv2.imshow("camera",image)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

camera.release()
writer.release()#kamerarla işimiz bittiğinde onu nasıl serbest bırakıyorsak kayıt işlemini de aynı şekilde serbest
#bırakmamız gerk
cv2.destroyAllWindows()

24-)Python ile Görüntü İşleme- UYGULAMA - Kameradan Alınan Görüntünün Kayıt Edilmesi/27-)Kamera kayıt.py


from pathlib import Path
from torch.utils.data import Dataset
import pickle
import numpy as np
import pandas as pd

class Cholec80Helper(Dataset):
    def __init__(self, hparams, data_p, dataset_split=None):
        assert dataset_split != None
        self.data_p = data_p
        assert hparams.data_root != ""
        self.data_root = Path(hparams.data_root)
        self.number_vids = len(self.data_p)
        self.dataset_split = dataset_split
        self.factor_sampling = hparams.factor_sampling

    def __len__(self):
        return self.number_vids

    def __getitem__(self, index):
        vid_id = index
        p = self.data_root / self.data_p[vid_id]
        unpickled_x = pd.read_pickle(p)
        stem = np.asarray(unpickled_x[0],
                          dtype=np.float32)[::self.factor_sampling]
        y_hat = np.asarray(unpickled_x[1],
                           dtype=np.float32)[::self.factor_sampling]
        y = np.asarray(unpickled_x[2])[::self.factor_sampling]
        return stem, y_hat, y

class Cholec80():
    def __init__(self, hparams):
        self.name = "Cholec80Pickle"
        self.hparams = hparams
        self.class_labels = [
            "Preparation",
            "CalotTriangleDissection",
            "ClippingCutting",
            "GallbladderDissection",
            "GallbladderPackaging",
            "CleaningCoagulation",
            "GallbladderRetraction",
        ]
        self.out_features = self.hparams.out_features
        self.features_per_seconds = hparams.features_per_seconds
        hparams.factor_sampling = (int(25 / hparams.features_subsampling))
        print(
            f"Subsampling features: 25features_ps --> {hparams.features_subsampling}features_ps (factor: {hparams.factor_sampling})"
        )

        self.data_p = {}
        self.data_p["train"] = [(
            f"{self.features_per_seconds:.1f}fps/video_{i:02d}_{self.features_per_seconds:.1f}fps.pkl"
        ) for i in range(1, 41)]
        self.data_p["val"] = [(
            f"{self.features_per_seconds:.1f}fps/video_{i:02d}_{self.features_per_seconds:.1f}fps.pkl"
        ) for i in range(41, 49)]
        self.data_p["test"] = [(
            f"{self.features_per_seconds:.1f}fps/video_{i:02d}_{self.features_per_seconds:.1f}fps.pkl"
        ) for i in range(49, 81)]

        # Datasplit is equal to Endonet and Multi-Task Recurrent ConvNet with correlation loss for surgical vid analysis
        self.weights = {}
        self.weights["train"] = [
            1.6411019141231247,
            0.19090963801041133,
            1.0,
            0.2502662616859295,
            1.9176363911137977,
            0.9840248158200853,
            2.174635818337618,
        ]
        self.weights["train_log"] = [1.25, 0.5, 1.0, 0.25, 1.25, 1., 1.5]

        self.data = {}
        for split in ["train", "val", "test"]:
            self.data[split] = Cholec80Helper(hparams,
                                              self.data_p[split],
                                              dataset_split=split)

        print(
            f"train size: {len(self.data['train'])} - val size: {len(self.data['val'])} - test size:"
            f" {len(self.data['test'])}")

    @staticmethod
    def add_dataset_specific_args(parser):  # pragma: no cover
        cholec80_specific_args = parser.add_argument_group(
            title='cholec80 dataset specific args options')
        cholec80_specific_args.add_argument("--features_per_seconds",
                                                  default=25,
                                                  type=float)
        cholec80_specific_args.add_argument("--features_subsampling",
                                                  default=5,
                                                  type=float)

        return parser


from functools import lru_cache
from typing import Dict, Union

from django.conf import settings
from django.dispatch import receiver
from django.test.signals import setting_changed

DEFAULT_CONFIG = {
    "DYNAMODB_TABLENAME": "sessions",
    "PARTITION_KEY_NAME": "PK",
    "SORT_KEY_NAME": "SK",
    "TTL_ATTRIBUTE_NAME": "ttl",
    "CACHE_PERIOD": 3600,
    "DYNAMODB_REGION": "ap-northeast-1",
    "LOGGING": {
        "TYPE": "CONSOLE",
    },
}

@lru_cache
def get_config() -> Dict[str, Union[str, int]]:
    """Return cached django-dysession config in dictionary type

    Contain Items:
        * DYNAMODB_TABLENAME
        * PARTITION_KEY_NAME
        * SORT_KEY_NAME
        * TTL_ATTRIBUTE_NAME
        * CACHE_PERIOD
        * DYNAMODB_REGION
        * LOGGING
            * TYPE
            * FILE_PATH

    Returns:
        Dict[str, Union[str, int]]
    """
    config = DEFAULT_CONFIG.copy()
    custom_config = getattr(settings, "DYSESSION", {})
    config.update(custom_config)
    return config

@receiver(setting_changed)
def update_dysession_config(*, setting, **kwargs):
    if setting == "DYSESSION":  # pragma: no cover
        get_config.cache_clear()  # pragma: no cover

__all__ = ["get_config"]