# Testing NMI for localization

import cv2
import numpy as np
import matplotlib.pyplot as plt
import random
from numpy import linalg as LA
import sys
import pandas as pd
from sklearn.metrics.cluster import normalized_mutual_info_score
from mpl_toolkits.mplot3d import Axes3D
import scipy.ndimage.filters as fltrs
import math

# def norm_diff(mat):
    # if mat.shape[0] != mat.shape[1]:
        # print("Error: Matrix must be square.")
        # return -10
    # diag = np.zeros(mat.shape)
    # for i in range(mat.shape[0]):
        # diag[i, i] = mat[i, i]
    # return LA.norm(np.subtract(mat, diag))

# https://math.stackexchange.com/questions/1392491/measure-of-how-much-diagonal-a-matrix-is
def samp_corr_coeff(array1, array2):
    hist_2d, x_edges, y_edges = np.histogram2d(
        array1.ravel(),
        array2.ravel(),
        bins=256)
    mat = hist_2d.T
    # if mat.shape[0] != mat.shape[1]:
        # print("Error: Matrix must be square.")
        # return -10
    d = mat.shape[0]
    j = np.full(d, 1.0)
    r = np.linspace(1, d, d)
    r_2 = np.square(r)
    n = np.sum(mat)
    sum_x = np.matmul(np.matmul(r, mat), np.transpose(j))
    sum_y = np.matmul(np.matmul(j, mat), np.transpose(r))
    sum_x_2 = np.matmul(np.matmul(r_2, mat), np.transpose(j))
    sum_y_2 = np.matmul(np.matmul(j, mat), np.transpose(r_2))
    sum_xy = np.matmul(np.matmul(r, mat), np.transpose(r))
    return (n * sum_xy - sum_x * sum_y) / (np.sqrt(n * sum_x_2 - sum_x**2) * np.sqrt(n * sum_y_2 - sum_y**2))

def gen_rand_matrix(shape, values):
    mat = np.zeros(shape)
    for row in range(mat.shape[0]):
        for col in range(mat.shape[1]):
            if random.randint(0, 1) == 1:
                mat[row, col] = values[0]
            else:
                mat[row, col] = values[1]
    return mat

def normalized_mut_info(array1, array2):
    hist_2d, x_edges, y_edges = np.histogram2d(
        array1.ravel(),
        array2.ravel(),
        # range=[[0, 255], [0, 255]],
        bins=256)
    # show hist
    # plt.imshow(hist_2d.T, origin='lower')
    # plt.show()
    # calc probs
    prob_xy = hist_2d / float(np.sum(hist_2d))
    prob_x = np.sum(prob_xy, axis=1)
    prob_y = np.sum(prob_xy, axis=0)
    prob_x_prob_y = prob_x[:, None] * prob_y[None, :] # prob(x)*prob(y)
    # we want non-zero values
    nzrs_xy = prob_xy > 0
    nzrs_x = prob_x > 0
    nzrs_y = prob_y > 0
    # print(prob_x)
    # print(prob_y)
    # print(prob_xy)
    entropy_x = -np.sum(prob_x[nzrs_x] * np.log(prob_x[nzrs_x]))
    entropy_y = -np.sum(prob_y[nzrs_y] * np.log(prob_y[nzrs_y]))
    entropy_xy = -np.sum(prob_xy[nzrs_xy] * np.log(prob_xy[nzrs_xy]))
    # mut_info = np.sum(prob_xy[nzrs_xy] * np.log(prob_xy[nzrs_xy] / prob_x_prob_y[nzrs_xy]))
    return (entropy_x + entropy_y) / entropy_xy # Sid's way
    # return 2 * mut_info / (entropy_x + entropy_y) # other way

def NMI(array):
    prob_xy = array / float(np.sum(array))
    prob_x = np.sum(prob_xy, axis=1)
    prob_y = np.sum(prob_xy, axis=0)
    prob_x_prob_y = prob_x[:, None] * prob_y[None, :] # prob(x)*prob(y)
    # we want non-zero values
    nzrs_xy = prob_xy > 0
    nzrs_x = prob_x > 0
    nzrs_y = prob_y > 0
    # print(prob_x)
    # print(prob_y)
    # print(prob_xy)
    entropy_x = -np.sum(prob_x[nzrs_x] * np.log(prob_x[nzrs_x]))
    entropy_y = -np.sum(prob_y[nzrs_y] * np.log(prob_y[nzrs_y]))
    entropy_xy = -np.sum(prob_xy[nzrs_xy] * np.log(prob_xy[nzrs_xy]))
    # mut_info = np.sum(prob_xy[nzrs_xy] * np.log(prob_xy[nzrs_xy] / prob_x_prob_y[nzrs_xy]))
    return (entropy_x + entropy_y) / entropy_xy # Sid's way
    # return 2 * mut_info / (entropy_x + entropy_y) # other way

def add_noise(array, mean, var, norm=False):
    sigma = var**0.5
    gaussian = np.random.normal(mean, sigma, array.shape)
    array_wnoise = array + gaussian
    if norm:
        cv2.normalize(array_wnoise, array_wnoise, 0, 255, cv2.NORM_MINMAX, dtype=-1)
    else:
        # clip at 0 and 255
        array_wnoise = np.clip(array_wnoise, 0, 255)
    return array_wnoise.astype(np.uint8)

def add_unequal_noise(array):
    noisy = np.zeros(array.shape)
    for row in range(array.shape[0]):
        cols = array.shape[1]
        sigma = (array.shape[0] - row - 1) * 0.05
        gaussian = np.random.normal(0, sigma, cols)
        noisy[row, :] = array[row, :] + gaussian
    noisy = np.clip(noisy, 0, 255)
    return noisy.astype(np.uint8)

def add_shift(array, shift):
    shifted = array + float(shift)
    shifted = np.clip(shifted, 0, 255)
    return shifted.astype(np.uint8)

# fig = plt.figure()
# ax = fig.gca(projection='3d')
# x = np.linspace(-5, 5, 101)
# y = np.linspace(-5, 5, 101)
# x, y = np.meshgrid(x, y)
# mean = 0
# var = 4
# z = (2 * np.pi * var)**-1 * np.exp(-((x - mean)**2 + (y - mean)**2) / (2* var))
# print(np.round(z, 2))
# ax.plot_surface(x, y, z)
# plt.show()
# exit()

# conv = fltrs.gaussian_filter(np.float_([0, 0, 1, 0, 0]), 5)
# print(conv)
# print(np.sum(conv))
# exit()

# other NMI stuff ******************************************

# mat = np.full((32, 32), 0)

# for i in range(mat.shape[0]):
    # mat[i, i] = 15

# mat[0, 10] = 3
# mat[0, 11] = 3
# mat[0, 12] = 3
# mat[0, 13] = 3
# mat[0, 14] = 3

# print(NMI(mat))

# plt.imshow(mat, origin='lower')
# plt.show()

# exit()

# **********************************************************

# ************ gaussian filter 2d hist *********************

sample_mat = np.array([[200, 200, 100, 100],
                       [200, 200, 100, 100],
                       [150, 150, 45, 45],
                       [150, 150, 45, 45]])

# noisy_mat = add_unequal_noise(sample_mat)

# hist_2d, x_edges, y_edges = np.histogram2d(
    # sample_mat.ravel(),
    # noisy_mat.ravel(),
    # bins=32)

bins = 32

img = cv2.imread(r'C:\Users\toddf\Desktop\data_4_5_19\01.jpg', cv2.IMREAD_GRAYSCALE)

noisy_img = add_unequal_noise(img)

plt.imshow(np.hstack((img, noisy_img)))
plt.show()

hist_2d, x_edges, y_edges = np.histogram2d(
    img.ravel(),
    noisy_img.ravel(),
    normed=True,
    bins=bins)

plt.imshow(hist_2d.T, origin='lower')
plt.show()

print('NMI:', NMI(hist_2d))
# print('NMI package:', normalized_mutual_info_score(img.ravel(), noisy_img.ravel()))

# y (rows) is img, x (cols) is noisy_img
hist_mat = np.zeros((bins, bins))

# 2 std devs +/- mean is 95% of gaussian

for i in range(img.shape[0]):
    sigma = (img.shape[0] - i - 1) * 0.1
    num_bins = math.ceil(2 * sigma / (256 / bins)) * 2 + 1
    delta_func = np.zeros(num_bins)
    delta_func[int(num_bins / 2)] = 1
    conv = fltrs.gaussian_filter(delta_func.astype(np.float), sigma)
    # print(conv)
    for j in range(img.shape[1]):
        col_location = int(noisy_img[i, j] / (256 / bins))
        row_location = int(img[i, j] / (256 / bins))
        # print(row_location)
        for k in range(conv.size):
            col_shifted = col_location + k - int(num_bins / 2)
            if (col_shifted < 0): # edge cases
                hist_mat[row_location, 0] += conv[k]
            elif (col_shifted >= hist_mat.shape[1]):
                hist_mat[row_location, hist_mat.shape[1] - 1] += conv[k]
            else:
                hist_mat[row_location, col_shifted] += conv[k]

print('Enhanced NMI:', NMI(hist_mat))

plt.imshow(hist_mat.T, origin='lower')
plt.show()

bin_array = np.linspace(1, bins, bins)

fig = plt.figure()
ax = fig.add_subplot(1, 2, 1, projection='3d')
#ax = plt.subplot(projection='3d')#fig.gca(projection='3d')
x, y = np.meshgrid(bin_array, bin_array)
ax.plot_surface(x, y, hist_2d)

ax2 = fig.add_subplot(1, 2, 2, projection='3d')
ax2.plot_surface(x, y, hist_mat)

plt.show()

exit()

# **********************************************************

img = cv2.imread(r'C:\Users\toddf\Desktop\data_4_5_19\01.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(r'C:\Users\toddf\Desktop\data_4_5_19\02.jpg', cv2.IMREAD_GRAYSCALE)

number_of_points = 101
vars = np.linspace(0, 10, number_of_points)

results_NMI = []
results_CC = []

# for variance in vars:
    # for i in range(10):
    # img_noisy = add_noise(img, 0, variance)

    # results_NMI.append(normalized_mut_info(img, img_noisy))
    # results_CC.append(samp_corr_coeff(img, img_noisy))

    # samples = 1000
    # sum_errors_NMI = 0
    # sum_errors_CC = 0

    # for i in range(samples):
        # sample = gen_rand_matrix((5, 5), (-1, 1))
        # sample_noisy = add_noise(sample, 0, variance)
        # alt_sample = gen_rand_matrix((5, 5), (-1, 1))

        # if normalized_mut_info(sample_noisy, alt_sample) > normalized_mut_info(sample_noisy, sample):
            # sum_errors_NMI += 1

        # if samp_corr_coeff(sample_noisy, alt_sample) > samp_corr_coeff(sample_noisy, sample):
            # sum_errors_CC += 1

    # results_NMI.append(sum_errors_NMI / samples)
    # results_CC.append(sum_errors_CC / samples)

# df = pd.DataFrame()

# df['var'] = vars
# df['p_error_NMI'] = results_NMI
# df['p_error_CC'] = results_CC

# df.to_csv('NMI_vs_CC_loc.csv', index = False)

# exit()

img_noisy = add_noise(img, 0, 1)

img_edit = np.zeros(img.shape)
img_edit[30:, :] = img[30:, :]

# plt.imshow(np.hstack((img, img_edit)))
# plt.show()

# plt.scatter(img.ravel(), img_edit.ravel())
# plt.show()

mat = gen_rand_matrix((5, 5), (-1, 1))

test_mat = np.array([[-1, 1, 1, -1, -1],
                    [1, 1, -1, 1, 1],
                    [-1, -1, 1, -1, -1],
                    [-1, 1, -1, -1, 1],
                    [-1, 1, 1, -1, 1]])

test_mat2 = np.array([[0, 0.8, 0.8, -0.8, -0.8],
                    [0.8, 0.8, -0.8, 0.8, 0.8],
                    [-0.8, -0.8, 0.8, -0.8, -0.8],
                    [-0.8, 0.8, -0.8, -0.8, 0.8],
                    [-0.8, 0.8, 0.8, -0.8, 0.8]])

test_mat3 = np.array([[-0.5, 0.8, 0.8, -0.8, -0.8],
                    [0.8, 0.8, -0.8, 0.8, 0.8],
                    [-0.8, -0.8, 0.8, -0.8, -0.8],
                    [-0.8, 0.8, -0.8, -0.8, 0.8],
                    [-0.8, 0.8, 0.8, -0.8, 0.8]])

test_mat4 = np.array([[-1, 1],
                    [-1, 1]])

test_mat5 = np.array([[0, 0.8],
                    [-0.8, 0.8]])

test_mat6 = np.array([[-1, 0.8],
                    [-0.8, 0.8]])

test_mat7 = test_mat + 10
test_mat7[0, 0] = 0

weights = np.full(test_mat.shape, 1.0)
weights[0, :] = 0.5

# print(weights)

# plt.scatter(mat.ravel(), test_mat2.ravel())
# plt.show()

mat_wnoise = np.zeros(mat.shape)
gaussian = np.random.normal(0, 0.4, mat.shape)
mat_wnoise = mat + gaussian

other_mat = gen_rand_matrix((5, 5), (-1, 1))
# other_mat_wnoise = np.zeros(other_mat.shape)
# gaussian = np.random.normal(0, 0.1, other_mat.shape)
# other_mat_wnoise = other_mat + gaussian

# print(mat_wnoise)

# print(test_mat.ravel())

# move img 15 pixels down
img_moved = np.zeros(img.shape)
img_moved[15:, :] = img[:-15, :]

# show images
# plt.imshow(np.hstack((img, img2)))
# plt.show()

hist_2d, x_edges, y_edges = np.histogram2d(
    test_mat.ravel(),
    test_mat7.ravel(),
    # weights=weights.ravel(),
    # normed=True,
    bins=20)

other_hist_2d, other_x_edges, other_y_edges = np.histogram2d(
    other_mat.ravel(),
    mat_wnoise.ravel(),
    # weights=weights.ravel(),
    # normed=True,
    bins=20)

# score1 = normalized_mut_info(img, img_noisy)
# score2 = normalized_mut_info(other_mat, mat_wnoise)
if len(sys.argv) > 1:
    if sys.argv[1] == 'CC':
        # score1 = samp_corr_coeff(hist_2d.T)
        score2 = samp_corr_coeff(hist_2d.T)
# print(score1)
# print(score2)

# print(hist_2d.T)

array = np.linspace(0, 100, 1001)
array2 = array + 100
array2[0] = 250
print(normalized_mut_info(img.ravel(), img2.ravel()))
print(normalized_mutual_info_score(img.ravel(), img2.ravel()))
# clipped_img = np.clip(img)
# print(normalized_mut_info(img, add_shift(img, 40)))

# plt.imshow(hist_2d.T, origin='lower')
# plt.show()

# plt.imshow(other_hist_2d.T, origin='lower')
# plt.show()

# hist_2d, x_edges, y_edges = np.histogram2d(
    # test_mat4.ravel(),
    # test_mat6.ravel(),
    # weights=weights.ravel(),
    # normed=True,
    # bins=20)

# score = normalized_mut_info(test_mat4, test_mat6)
# print(score)

# plt.imshow(hist_2d.T, origin='lower')
# plt.show()

# plt.hist(img.ravel(), bins=64)
# plt.show()

# show the image
# cv2.imshow("Image 08", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


from flask import render_template, request, url_for, redirect
from flaskext.mysql import MySQL
from app import app
from app.frm_entry import EntryForm

# Connecting database
app.config["MYSQL_DATABASE_HOST"]="localhost"
app.config["MYSQL_DATABASE_USER"]="root"
app.config["MYSQL_DATABASE_PASSWORD"]="opansan63"
app.config["MYSQL_DATABASE_DB"]="test"
app.config["MYSQL_PORT"]="3306"

# Preparing variable on mysqlnya
mysqlnya=MySQL(app)
mysqlnya.init_app(app)

@app.route("/")
def home():
    return render_template("home.html", title="Home")

@app.route("/frm_entry", methods=["GET","POST"])
def frm_entry():
    resultnya=0
    rem=""
    xvalue_1=0
    xvalue_2=0
    xoperator=""

    formnya=EntryForm()

    # Save inputted data to variables
    if request.method=="POST":
        details=request.form
        xvalue_1=details["value_1"]
        xvalue_2=details["value_2"]
        xoperator=details["operatornya"]

    # Processing result
    if xoperator=="+":
       resultnya=int(xvalue_1)+int(xvalue_2)
    elif xoperator=="-":
       resultnya=int(xvalue_1)-int(xvalue_2)
    elif xoperator=="/":
       resultnya=int(xvalue_1)/int(xvalue_2)
    elif xoperator=="*":
       resultnya=int(xvalue_1)*int(xvalue_2)

    # Processing remarks, Even, Odd or Zero
    if resultnya==0:
        rem="Zero"
    elif resultnya % 2 ==0:
        rem="Even"
    elif resultnya % 2==1:
         rem="Odd"
    else:
        rem=""

    # Save data
    cur=mysqlnya.connect().cursor()
    cur.execute("insert into penjumlahan (value_1,value_2,operator,result, remark) values (%s,%s,%s,%s,%s)",((xvalue_1,xvalue_2,xoperator,resultnya,rem)))
    cur.connection.commit()
    cur.close()

    return render_template("frm_entry.html", title="Nilai", formnya=formnya, xresultnya=resultnya, remnya=rem)

@app.route("/display_data")
def display_data():
    formnya=EntryForm()

    cur=mysqlnya.connect().cursor()
    cur.execute("select * from penjumlahan")
    hasilnya=cur.fetchall()
    return render_template("tampil_data.html", title="Tampil Data",hasilnya=hasilnya,no=1)

@app.route("/frm_edit_data/<id>",methods=["GET","POST"])
def frm_edit_data(id):
    cur=mysqlnya.connect().cursor()
    form=EntryForm()
    cur.execute("select * from penjumlahan where id='"+id+"'")
    hasilnya=cur.fetchall()
    for rows in hasilnya:
        xvalue_1=rows[1]
        xvalue_2=rows[2]
        xoperator=rows[3]
        xresult=rows[4]
        xremark=rows[5]

    if request.method=="POST":

       details=request.form
       xvalue_1=details["value_1"]
       xvalue_2=details["value_2"]
       xoperator=details["operatornya"]

       # Memproses hasil
       if xoperator=="+":
          xresult=float(xvalue_1)+float(xvalue_2)
       elif xoperator=="-":
          xresult=float(xvalue_1)-float(xvalue_2)
       elif xoperator=="/":
          xresult=float(xvalue_1)/float(xvalue_2)
       elif xoperator=="*":
           xresult=float(xvalue_1)*float(xvalue_2)
       else:
           xresult=0

       # Menentukan Ganjil atau Genap
       if xresult % 2 ==0:
          xremark="Even"
       elif xresult % 2==1:
          xremark="Odd"
       else:
          xremark="Zero"

       cur=mysqlnya.connect().cursor()
       #cur.execute("update penjumlahan set value_1='"+xnilai_1+"'"+
       #            ",value_2='"+xnilai_2+"'"+",operator='"+xoperator+"'"+
       #            ",result='"+hasil+"'"+",remark='"+xremark+"'"+"where id='"+id+"'")

       cur.execute ("update penjumlahan set value_1=%s,value_2=%s,operator=%s,result=%s,remark=%s where id= %s",
                    ((xvalue_1,xvalue_2,xoperator,xresult,xremark,id)))
       cur.connection.commit()
       cur.close()
    else:
       form.value_1.data=xvalue_1
       form.value_2.data=xvalue_2
       form.operatornya.data=xoperator

    return render_template("frm_edit.html", title="Edit Data",znilai_1=xvalue_1,form=form,
                           ketnya=xremark,hasilnya=xresult)

@app.route("/delete_data/<id>", methods=["GET","POST"])
def delete_data(id):
    cur=mysqlnya.connect().cursor()
    cur.execute("delete from penjumlahan where id='"+id+"'")
    cur.connection.commit()
    cur.close()
    return redirect(url_for("display_data"))


"""Module for calculating normal mode frequencies and plotting IR spectrum of molecules.

This module provides classes and methods required for calculating normal mode frequencies and plotting IR spectrum of molecules whose coordinates are given as .xyz files.

example:
  ::

    if __name__ == `__main__`:
      wat=harmonic.MolOptimize('H2O',0)
      wat.freq_show()
      wat.IR_plot()

"""

from pyscf import gto,scf,dft
import numpy as np
import scipy.linalg as linalg
import scipy.constants as const
import math
import pymatgen.core as mg
from pyscf import hessian
from pyscf.geomopt.berny_solver import optimize
import matplotlib.pyplot as plt

Eh_to_J = const.physical_constants['hartree-joule relationship'][0]
"""float: Hartree energy in joules quoted from ``scipy.constants``."""

Bohr = const.physical_constants['Bohr radius'][0]
"""float: Bohr radius in meters quoted from ``scipy.constants``."""

amu = const.physical_constants['atomic mass constant'][0]
"""float: Atomic mass constants in kilograms quoted from ``scipy.constats``."""

m_to_cm = 100
"""float: metets in centimeters."""

class MolOptimize:
  """A class for optimizing molecules and showing normal mode frequencies and IR spectrum calculated using optimized structures.

    Args:
      name(str): Name of the xyz file.
      hess_method(int):if 0, hessian is calculated analytically by using `pyscf.hessian`. if not, hessian is calculated numerically.

    Attributes:
      name(str): Name of the xyz file.
      hess_method(int):if 0, hessian is calculated analytically by using `pyscf.hessian`. if not, hessian is calculated numerically.
      mol(pyscf.gto.mole.Mole):Optimized molecule.
      x_eq(numpy.adarray): Coordinates of atoms in optimized molecule in Bohrs.
      elem(list): Element symbols of atoms in the given molecule.
      mf(Energy): RKS object which holds results of executing ``kernel`` method.
      freq_list(FreqAnalysis): Frequencies of normal modes.
      mwc(FreqAnalysis): normal mode coordinates.
      IR_list(IR_intensity): IR absorption intensities corresponding to each normal mode.

  """

  def __init__(self,name,hess_method=0):
    self.name = name
    self.hess_method = hess_method
    self.mol = self.opt()
    self.x_eq = self.mol.atom_coords(unit='Bohr').flatten()
    self.elem = Molecule(self.name).mol_read()[0]
    self.mf = Energy(self.mol).ene()[1]
    self.freq_list = FreqAnalysis(self.mf,self.elem,self.x_eq,self.hess_method).freq_mode()[0]
    self.mwc = FreqAnalysis(self.mf,self.elem,self.x_eq,self.hess_method).freq_mode()[1]
    self.IR_list = IR_intensity(self.freq_list,self.mwc,self.elem,self.x_eq).intensity()

  def opt(self):
    """Optimize coordinates from .xyz file.

    Geometry optimization with DFT calculation by ``pyscf.geomopt.berny_solver``.

    Returns:
      pyscf.gto.mole.Mole: Optimized molecule.

    """
    mol = gto.Mole()
    mol.atom=self.name+'.xyz'
    mol.verbose = 0
    mol.basis = "ccpvdz"
    mol.unit='Angstrom'
    mol.build()
    mf = dft.RKS(mol)
    mf.xc='b3lyp'
    return optimize(mf,verbose=0)

  def freq_show(self):
    """Show the frequency wave number of normal mode vibration.

    Frequency wave number is calculated by ``FreqAnalysis.freq_mode``.

    """
    self.mwc = np.array(self.mwc).reshape((len(self.freq_list),len(self.elem),3))
    print("----------Normal Mode Frequency-----------")
    for i in range(len(self.freq_list)):
      print("")
      print(f"Frequency:{self.freq_list[i]:.5f} cm^-1")
      print("Normal Mode Coordinate")
      for j in range(int(len(self.x_eq)/3)):
        print(self.elem[j],f": x {self.mwc[i,j,0]:2.5f} y {self.mwc[i,j,1]:2.5f} z {self.mwc[i,j,2]:2.5f}")
      print("-------------------------------------------")
    print("")
    print("frequency wave number[cm^-1]")
    print(self.freq_list)

  def IR_plot(self):
    """Show the IR spectrum.
    """
    IRPlot(self.freq_list,self.IR_list,self.name).plot()

class MolBuild:
  """A class for building molecule, whose coordinates of atoms are displaced.

    Args:
      dx(numpy.ndarray): Displacement of atoms from optimized coordinates.
      elem(list): Element symbols of atoms.
      x_eq(numpy.ndarray): Coordinates of atoms in optimized molecule in Bohrs.

  """
  def __init__(self,dx,elem,x_eq):
    self.dx = dx
    self.elem = elem
    self.x_eq = x_eq

  def mol_build(self):
    """Build Mole object, whose coordinates of atoms are displaced by dx.

    Returns:
      pyscf.gto.mole.Mole: Displaced molecule.

    """
    x = self.x_eq + self.dx
    mol = gto.Mole()
    mol.atom = [[self.elem[i],x[3*i:3*(i+1)]] for i in range(len(self.elem))]
    mol.basis = 'ccpvdz'
    mol.unit = 'Bohr'
    mol.verbose = 0
    mol.build()
    return mol

class Energy:
  """ A class for calculating energy of the molecule.

    Args:
      mol(pyscf.gto.mole.Mole): Molecule object.

  """

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

  def ene(self):
    """DFT calculation.

    Returns:
      tuple: (E,mf)
        - **ene** (`numpy.float`) - Energy calculated with DFT method.
        - **mf** (`pyscf.dft.rks.RKS`) -RKS object which holds results of executing ``kernel`` method.
    """
    mf = dft.RKS(self.mol)
    mf.xc = 'b3lyp'
    E = mf.kernel()
    return E,mf

class DipoleMoment:
  """Dipole moment of the molecule.

    Args:
      mol(pyscf.gto.mole.Mole): Mole object.

    Attributes:
      mf(Energy.mf): RKS object which holds results of executing ``kernel`` method.
  """

  def __init__(self,mol):
    self.mol = mol
    self.mf = Energy(self.mol).ene()[1]

  def dp_moment(self):
    """DFT calculation.

    Returns:
      numpy.ndarray: Dipole moment.
    """
    return self.mf.dip_moment(verbose=0)

class Hess:
  """Hessian matrix of energy.

  Args:
    mf(Energy.mf): RKS object which holds results of executing ``kernel`` method.
    elem(list): Element symbols of atoms in the given molecule.
    x_eq(numpy.adarray): Coordinates of atoms in optimized molecule in Bohrs.
    hess_method(int):if 0, hessian is calculated analytically by using `pyscf.hessian`. if not, hessian is calculated numerically.

  """

  def __init__(self,mf,elem,x_eq,hess_method):
    self.hess_method=hess_method
    self.mf = mf
    self.elem = elem
    self.x_eq = x_eq
    self.hess_method = hess_method

  def analytical_hess(self):
    """Analytically calculate hessian metrix of energy.

    Returns:
      numpy.ndarray: Analytical hessian matrix in cartesian coordinates.
    """
    self.mf.kernel()
    H = self.mf.Hessian().kernel()
    return H

  def numerical_hess(self):
    """Numerically calculate hessia matrix.

    Returns:
      numpy.ndarray: Numerical hessian matrix in cartesian coordinates.
    """
    h = np.eye(len(self.x_eq),dtype=float)*1e-10/Bohr
    H = np.empty((len(self.x_eq),len(self.x_eq)))
    for i in range(len(self.x_eq)):
      for j in range(i+1):
        H[i,j] = (
        Energy(MolBuild((h[i]+h[j]),self.elem,self.x_eq).mol_build()).ene()[0]
        - Energy(MolBuild((-h[i]+h[j]),self.elem,self.x_eq).mol_build()).ene()[0]
        - Energy(MolBuild((h[i]-h[j]),self.elem,self.x_eq).mol_build()).ene()[0]
        + Energy(MolBuild((-h[i]-h[j]),self.elem,self.x_eq).mol_build()).ene()[0]
        )/ (4.0*h[i,i]*h[j,j])
        if i != j:
          H[j,i] = H[i,j]
    return H

  def mw_hessian(self):
    """Caluculate hessian matrix in mass weighted coordinates.

    Convert unit from Hatree /(amu Bohr*Bohr) to J /(kg m*m).

    Returns:
      numpy.ndarray: Hessian matrix in mass weighted coordinates.
    """
    if self.hess_method == 0:
      H = self.analytical_hess()
    else:
      H = self.numerical_hess()
    H = H.reshape(len(self.x_eq),len(self.x_eq))
    mw_H = np.eye(len(self.x_eq),len(self.x_eq))
    for i in range(len(self.x_eq)):
      for j in range(i + 1):
        mw_H[i,j] = H[i,j]*Eh_to_J/(Bohr**2)\
                 /(amu*math.sqrt(mg.Element(self.elem[int(i/3)]).atomic_mass * mg.Element(self.elem[int(j/3)]).atomic_mass))
        mw_H[j,i] = mw_H[i,j]
    return mw_H

class FreqAnalysis:
  """A class for calculating normal mode frequencies and corresponding normal mode coordinates.

  Args:
    mf(Energy.mf): RKS object which holds results of executing ``kernel`` method.
    elem(list): Element symbols of atoms in the given molecule.
    x_eq(numpy.adarray): Coordinates of atoms in optimized molecule in Bohrs.
    hess_method(int):if 0, hessian is calculated analytically by using `pyscf.hessian`. if not, hessian is calculated numerically.

  Attributes:
    hess(Hess.mw_hessian): Hessian matrix with mass weighted coordinates.
  """

  def __init__(self,mf,elem,x_eq,hess_method):
    self.mf = mf
    self.elem = elem
    self.x_eq = x_eq
    self.hess_method = hess_method
    self.hess = Hess(self.mf,self.elem,self.x_eq,self.hess_method).mw_hessian()

  def diagonalization(self):
    """Diagonalize the hessian matrix.

    Returns:
      tuple: (scipy.linalg.eig[0],scipy.linalg.eig[1])
        - **scipy.linalg.eig[0]** (`numpy.ndarray`) - Eigenvalues of the hessian matrix.
        - **scipy.linalg.eig[1]** (`pyscf.dft.rks.RKS`) - Eigenvectors of the hessian matrix.
    """
    return linalg.eig(self.hess)

  def freq_mode(self):
    """Calculate the normal mode frequencies and corresponding normal mode coordinates.

    Returns:
      tuple: (freq_list,mwc_list)
        - **freq_list** (list) - List including all the normal mode frequencies.
        - **mwc_list** (list) - List of normal mode coordinates corresponding to each frequency.

    """
    omega_sq = self.diagonalization()[0]
    mwc = self.diagonalization()[1]
    freq_list = []
    mwc_list = []
    for i in range(len(omega_sq)):
      if omega_sq[i] > 0:
        nu = abs(omega_sq[i])**0.5/(2*math.pi*const.c*m_to_cm)
        freq_list.append(nu)
        mwc_list.append(mwc[:,i])
    return freq_list,mwc_list

class IR_intensity:
  """A class for calculating IR abosorption intensity of molecule.

  Args:
    freq_list(FrequencyAnalysis.freq_mode): List including all the normal mode frequencies
    mwc(FrequencyAnalysis.freq_mode): List of normal mode coordinates corresponding to each frequency.
    elem(list): Element symbols of atoms in the given molecule.
    x_eq(numpy.adarray): Coordinates of atoms in optimized molecule in Bohrs.

  Attributes:
    mwc(numpy.ndarray): normal mode coordinates.
    delta(float): infinitesimal displacement.
    dip_moment_grad(numpy.ndarray): gradient of dipole moment.

  """

  def __init__(self,freq_list,mwc,elem,x_eq):
    self.freq_list = freq_list
    self.mwc = np.array(mwc)
    self.elem = elem
    self.x_eq = x_eq
    self.delta = 1.e-5
    self.dip_moment_grad = self.dm_grad()

  def dm_grad(self):
    """Calculate gradient of dipole moment.

    Returns:
      numpy.darray: gradient of dipole moment.

    """
    dip_moment_grad = np.empty((len(self.freq_list),3))
    dq = np.empty((len(self.freq_list),len(self.x_eq)))
    for i in range(len(self.freq_list)):
      for j in range(len(self.elem)):
        for k in range(3):
          dq[i] = self.mwc[i]*self.delta
      dip_moment_grad[i] = (DipoleMoment(MolBuild(dq[i],self.elem,self.x_eq).mol_build()).dp_moment()-DipoleMoment(MolBuild(-dq[i],self.elem,self.x_eq).mol_build()).dp_moment())/(self.delta*2)
    return dip_moment_grad

  def intensity(self):
    """Calculate IR absorption intensity.

    Returns:
      list: list of IR absorption intensity of each normal mode frequency.
    """
    IR_list = []
    for i in range(len(self.freq_list)):
      intense = pow(linalg.norm(self.dip_moment_grad[i]),2.0)
      IR_list.append(intense)
    return IR_list

class Oscillator:
  """A class for oscillating IR intensity.

  Args:
    freq_list(FreqAnalysis): List including all the normal mode frequencies.
    intensity(IR_intensity): Intensity of IR absorption.

  """

  def __init__(self,freq_list,intensity):
    self.freq_list = freq_list
    self.intensity = intensity

  def oscillator(self):
    """Oscillate IR intensity.

    Returns:
      tuple: (x,y)
        - **x** (numpy.darray): frequency wave number between 200 and 3500(cm^-1). x-axis of IR spectrum.
        - **y** (numpy.darray): IR intensity of each x. y-axis of IR spectrum.
    """
    x = np.arange(200,3500,10)
    y = np.zeros(len(x))
    w = 5
    for i in range(len(self.freq_list)):
      y = y + ((1/math.pi) * self.intensity[i] * 5)/ ((x - self.freq_list[i])**2 + w**2)
    y = 1.2*np.amax(y) -y
    return x,y

class IRPlot:
  """A class for plotting IR spectrum.

  Args:
    freq_list(FrequencyAnalysis.freq_mode): List including all the normal mode frequencies.
    intensity(IR_intensity): Intensity of IR absorption.
    name(str): name of .xyz file.

  Attributes:
    x(list): oscillated frequencies, horizontal axis
    y(list): IR intensity, vertical axis
  """

  def __init__(self,freq_list,intensity,name):
    self.freq_list = freq_list
    self.intensity = intensity
    self.name = name
    self.x,self.y = Oscillator(freq_list,intensity).oscillator()

  def plot(self):
    """Generate IR spectrum in a .png file.
    """
    plt.plot(self.x,self.y)
    plt.xlabel('Frequency(cm^-1)')
    plt.ylabel('IR intensity')
    plt.title('IR Spectrum of ' + self.name)
    plt.savefig(self.name + "-IR.png")

class Molecule:
  """a class for reading .xyz file.

  Args:
    name(str): name of .xyz file.

  Attributes:
    atom_list(list): List of the element symbol of atoms.
    coord_list(list): List of the coordinates of each atom.
  """

  def __init__(self,name):
    self.name=name+'.xyz'
    self.atom_list=self.mol_read()[0]
    self.coord_list=self.mol_read()[1]

  def mol_read(self):
    """Read .xyz file, and extract element symbol and coordinates of atoms.

    Returns:
      tuple: (atom_list,coord_list)
        - **atom_list** (list) - List of the element symbol of atoms.
        - **coord_list** (list) - List of the coordinates of each atom.
    """
    atom_list=[]
    coord_list=[]
    with open(self.name,'r') as file:
      atom_num=int(file.readline())
      file.readline()

      for _ in range(atom_num):
        line = file.readline().split()
        atom = line[0]
        atom_list.append(atom)
        x,y,z = line[1:4]
        coord_list.append((x,y,z))
    return atom_list,coord_list


import os
import sys
import logging
from pathlib import PosixPath

if sys.version_info < (3, 5):
    raise RuntimeError('Only supports Python 3.5 or higher.')

# ------------------------------------------------------------------------------
# GLOBAL VARIABLES
# ------------------------------------------------------------------------------

# Number of paragraphs contained to extract from each document
# (50 in the original research paper)
num_docs = 30

# Number of guesses to display
display_num = int(num_docs/3) # == 10

# Probability of extracting answer given a question and a paragraph
# (1) Max: we assume that only one token in the paragraph indicates the correct answer
# (2) Sum: we regard all tokens matched to the correct answer equally
type_max = True

# ------------------------------------------------------------------------------
# DIRECTORIES
# ------------------------------------------------------------------------------

sys_dir = '/home/nboukraa/Documents/projet-personnel/open-qa'
MODEL_DIR = sys_dir + '/models/'
EMBED_DIR = sys_dir + '/data/embeddings/'
embed_name = 'glove.840B.300d.txt'

DATA_DIR = (
    os.getenv('DRQA_DATA') or
    os.path.join(PosixPath(__file__).absolute().parents[1].as_posix(), 'data')
)

# ------------------------------------------------------------------------------
# LOGGER
# ------------------------------------------------------------------------------

logger = logging.getLogger()
logger.setLevel(logging.INFO)

txtfmt = '%(asctime)s: [ %(message)s ]'
datefmt = '%m/%d/%Y %I:%M:%S %p'
fmt = logging.Formatter(txtfmt, datefmt)

console = logging.StreamHandler()
console.setFormatter(fmt)
logger.addHandler(console)

# ------------------------------------------------------------------------------
# CHILDREN
# ------------------------------------------------------------------------------

from . import tokenizers
from . import reader
from . import retriever
from . import pipeline


import logging

from narraint.backend.database import SessionExtended
from narraint.backend.util import get_db_connection_name

def main():
    logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
                        datefmt='%Y-%m-%d:%H:%M:%S',
                        level=logging.INFO)

    logging.info('Trying to create database schema / add missing tables')

    database_name = get_db_connection_name()
    logging.info(f'Your current database is: {database_name}')

    session = SessionExtended.get()
    logging.info('Finished')

if __name__ == "__main__":
    main()


from bitstream import c_bool, c_float, c_int, c_int64, c_ubyte, c_uint, ReadStream

def from_ldf(ldf):
	ldf_dict = {}
	if isinstance(ldf, ReadStream):
		for _ in range(ldf.read(c_uint)):
			encoded_key = ldf.read(bytes, length=ldf.read(c_ubyte))
			key = encoded_key.decode("utf-16-le")
			data_type_id = ldf.read(c_ubyte)
			if data_type_id == 0:
				value = ldf.read(str, length_type=c_uint)
			elif data_type_id == 1:
				value = ldf.read(c_int)
			elif data_type_id == 3:
				value = ldf.read(c_float)
			elif data_type_id == 5:
				value = ldf.read(c_uint)
			elif data_type_id == 7:
				value = ldf.read(c_bool)
			elif data_type_id in (8, 9):
				value = ldf.read(c_int64)
			elif data_type_id == 13:
				value = ldf.read(bytes, length=ldf.read(c_uint))
			else:
				raise NotImplementedError(key, data_type_id)
			ldf_dict[key] = data_type_id, value
	else:
		raise NotImplementedError

	return ldf_dict


def checkRotation(s1, s2):
    temp = ''
    if len(s1) != len(s2):
        return False

    # concatenating both strings
    temp = s1 + s1
    if s2 in temp:
        return True #returning true if 2nd string is present in concatenated string
    else:
        return False
# Driver
string1 = "HELLO"
string2 = "LOHEL"

if checkRotation(string1, string2):
    print("Given Strings are rotations of each other.")
else:
    print("Given Strings are not rotations of each other.")


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#Data
data = pd.read_csv('VietnamConflict.csv')
data1 = data[data['BRANCH'].isin(['ARMY','MARINE CORPS'])]
pd.set_option('display.width', 400)
pd.set_option('display.max_columns', 202)

# print(data1.head(5))
# print(data.columns)

#infantry troops
infantry_MOS = ['INFANTRY OPERATIONS AND INTELLIGENCE SPECIALIST', 'INDIRECT FIRE INFANTRYMAN', 'INFANTRY UNIT LEADER',
                'PARACHUTIST, INFANTRY UNIT COMMANDER', 'INFANTRYMAN', 'RIFLEMAN', 'MACHINEGUNNER',
                'INFANTRY OFFICER (I)', 'ASSAULTMAN', 'HEAVY ANTI-ARMOR WEAPONS INFANTRYMAN', 'MORTARMAN',
                'INFANTRY UNIT COMMANDER', 'BASIC INFANTRY OFFICER', 'RANGER, OPERATIONS AND TRAINING STAFF OFFICER (G3,A3,S3)',
                'INFANTRY SENIOR SERGEANT', 'BASIC INFANTRYMAN', 'RANGER, UNIT OFFICER, TRAINING CENTER',
                'RANGER, INFANTRY UNIT COMMANDER', 'RANGER', 'INFANTRY UNIT COMMANDER, (MECHANIZED)', 'LAV ASSAULTMAN',
                'SCOUT-SNIPER']

infantry=data1[data1['POSITION'].isin(infantry_MOS)]
infantry_cas = infantry.FATALITY_YEAR.count()
total_cas = data.FATALITY_YEAR.count()
infantry_portion = infantry_cas / total_cas * 100
# print('Infantrymen sustained {}% of total casualties in Vietnam war.'.format(infantry_portion))

#Non- Infantry
non_infantry = data1[-data1['POSITION'].isin(infantry_MOS)]
navy_af = data[data['BRANCH'].isin(['NAVY','AIR FORCE'])]
by_grade = ['E01', 'E02', 'E03', 'E04', 'E05', 'E06', 'E07', 'E08', 'E09', 'W01',
            'W02', 'W03','W04', 'W05', 'O01', 'O02', 'O03', 'O04', 'O05', 'O06', 'O07']
# plt.subplot(3,1,1)
# sns.countplot(x='PAY_GRADE',data=infantry,order=by_grade)
# plt.title('Infantry Casualties(ARMY & MARINE CORPS)')
# plt.xlabel('PAY GRADE')
# plt.ylabel('Number of Casualties')
# plt.subplot(3,1,2)
# sns.countplot(x='PAY_GRADE',data=non_infantry,order=by_grade)
# plt.title('Non-Infantry (ARMY & MARINE CORPS)')
# plt.xlabel('Pay Grade')
# plt.ylabel('Number Of Casualties')
# plt.subplot(3,1,3)
# sns.countplot(x='PAY_GRADE',data=navy_af,order=by_grade)
# plt.title('NAVY & AIR FORCE CASUALTIES')
# plt.xlabel('PAY GRADE')
# plt.ylabel('Number Of Casualties')
# plt.tight_layout()
# plt.show()

pd.options.mode.chained_assignment = None

#Infantry ages

birth = pd.Series(infantry.loc[:,'BIRTH_YEAR'].floordiv(10000),index=infantry.index)
infantry.loc[:,'BIRTH_YEAR'] = birth
for row in infantry:
    age_at_death = []
    birth = infantry.BIRTH_YEAR
    death = infantry.FATALITY_YEAR
    age = death - birth
    age_at_death.extend(age)
age = pd.Series(age_at_death,index=infantry.index)
infantry.loc[:,'AGE'] = age

#then non-infantry
birth = pd.Series(non_infantry.loc[:,'BIRTH_YEAR'].floordiv(10000),index=non_infantry.index)
non_infantry.loc[:,'BIRTH_YEAR'] = birth
for row in non_infantry:
    age_at_death2 = []
    birth = non_infantry.BIRTH_YEAR
    death = non_infantry.FATALITY_YEAR
    age = death - birth
    age_at_death2.extend(age)
age = pd.Series(age_at_death2,index=non_infantry.index)
non_infantry.loc[:,'AGE'] = age

#my sister service

birth = pd.Series(navy_af.loc[:,'BIRTH_YEAR'].floordiv(10000),index=navy_af)
navy_af.loc[:,'BIRTH_YR'] = birth
for row in navy_af :
    age_at_death3 = []
    birth = navy_af.BIRTH_YEAR
    death = navy_af.FATALITY_YEAR
    age = death - birth
    age_at_death3.extend(age)
age = pd.Series(age_at_death3,index=navy_af.index)
navy_af.loc[:,'AGE'] = age

# plt.subplot(3,1,1)
# sns.countplot(x='AGE',data= infantry)
# plt.title('Infantry Age at Death')
# plt.xlabel('Age')
# plt.ylabel('Number of Fatalities')
# plt.subplot(3,1,2)
# sns.countplot(x='AGE',data=non_infantry)
# plt.title('Non Infantry Age at death (ARMY & MARINE CORPS)')
# plt.xlabel('Age')
# plt.ylabel('Number of fatalities')
# plt.subplot(3,1,3)
# sns.countplot(x='AGE',data=navy_af)
# plt.title('Navy & Air Force Age at Death')
# plt.xlabel('AGE')
# plt.ylabel('Number Of Fatalities')
# plt.tight_layout()
# plt.show()

#Mean and Median

inf_mean = infantry.AGE.mean()
inf_median = infantry.AGE.median()
non_inf_mean = non_infantry.AGE.mean()
non_inf_median = non_infantry.AGE.median()
navy_mean = navy_af.AGE.mean()
navy_median = navy_af.AGE.median()
# print('Infantry mean and median age at death are '+ str(inf_mean) + 'and'+str(inf_median)+'respectively')
# print('AF/Navy mean and median age at death are '+ str(navy_mean) + 'and ' + str(navy_median) + 'respectively')

#Hostility

infantry['HOSTILITY_CONDITIONS'] = infantry['HOSTILITY_CONDITIONS'].replace(['H','HN'],['HOSTILE','NON-HOSTILE'])
# sns.countplot(x='HOSTILITY_CONDITIONS',data=infantry)
# plt.title('Casualty Breakdown, Infantry')
# plt.xlabel('HOSTILITY_CONDITIONS')
# plt.ylabel('Number Of Fatalities')
# plt.show()

total_deaths = infantry['HOSTILITY_CONDITIONS'].count()
hostile = infantry[infantry['HOSTILITY_CONDITIONS'] == ' Hostile']
hostile_death = hostile['HOSTILITY_CONDITIONS'].count()
non_hostile = infantry[infantry['HOSTILITY_CONDITIONS'] == 'Non_hostile']
non_hostile_death = non_hostile['HOSTILITY_CONDITIONS'].count()
non_hostile_ratio = non_hostile_death / total_deaths * 100
hostile_ratio = hostile_death / total_deaths * 100
    # print('Infantry Sms sustained {}% hostile casualties and {}% non-hostile casualties'.format(hostile_ratio,non_hostile_ratio))


from flask import Blueprint, session, redirect
from ..models.model import UserInfo
index = Blueprint('index_api', __name__)

@index.route('/', methods=['GET'])
def index_view():
    user_info = session.get('uname')
    user = UserInfo.query.filter(UserInfo.username == 'yuan').all()
    print(user)
    if user_info:
        print(user_info)
        return 'index page'
    else:
        return redirect('/api/login')


import scrapy
from ..items import NadlanscraperItem
import sys
class NadscraperSpider(scrapy.Spider):
    name = 'nadScraper'
    page_number = 2
    start_urls = [
                'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%D7%A0%D7%95%D7%94-%D7%91%D7%A8%D7%91%D7%95%D7%A8'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%a9%d7%a4%d7%99%d7%a8%d7%90'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%94%d7%aa%d7%a7%d7%95%d7%95%d7%94'
                , 'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%99%d7%93-%d7%90%d7%9c%d7%99%d7%94%d7%95'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%a4%d7%9c%d7%95%d7%a8%d7%a0%d7%98%d7%99%d7%9f'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%a0%d7%95%d7%94-%d7%97%d7%9f'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%a0%d7%95%d7%94-%d7%91%d7%a8%d7%91%d7%95%d7%a8'
                ,'https://www.ad.co.il/nadlanprice?city=tel-aviv&hood=%d7%a2%d7%96%d7%a8%d7%90'
            ]

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, callback=self.parse,dont_filter=True)

    def parse(self, response):
        base_url = response.url
        yield scrapy.Request(response.url, callback=self.parse_area, cb_kwargs= dict(base_url=base_url))
        NadscraperSpider.page_number = 2

    def parse_area(self, response,base_url):

        all_date = response.css('td:nth-child(1)').css('::text').getall()
        all_address = response.css('td:nth-child(3)').css('::text').getall()
        all_numOfRooms = response.css('td:nth-child(4)').css('::text').getall()
        all_size = response.css('td:nth-child(5)').css('::text').getall()
        all_floor = response.css('td:nth-child(6)').css('::text').getall()
        all_price = response.css('td:nth-child(7)').css('::text').getall()
        all_pricePerMeter = response.css('td:nth-child(8)').css('::text').getall()
        all_buildYear = response.css('td:nth-child(9)').css('::text').getall()
        area = response.css('.btn-sm+ .btn-sm .me-2').css('::text').get()

        item = NadlanscraperItem()
        for i  in range(len(all_date)):
            try:
                item['area'] = area
                print(area)
            except:
                item['area'] = "None"
            try:
                item['date'] = all_date[i]
            except:
                item['date'] = "None"
            try:
                item['address'] = all_address[i]
            except:
                item['address'] = "None"
            try:
                item['numOfRooms'] = all_numOfRooms[i]
            except:
                item['numOfRooms'] = "None"
            try:
                item['size'] = all_size[i]
            except:
                item['size'] = "None"
            try:
                item['floor'] = all_floor[i]
            except:
                item['floor'] = "None"
            try:
                item['price'] = all_price[i]
            except:
                item['price'] = "None"
            try:
                item['pricePerMeter'] = all_pricePerMeter[i]
            except:
                item['pricePerMeter'] = "None"
            try:
                item['buildYear'] = all_buildYear[i]
            except:
                item['buildYear'] = "None"

            yield item

        next_page = base_url+"&pageindex="+str(NadscraperSpider.page_number)
        if NadscraperSpider.page_number <= 100:
            NadscraperSpider.page_number+=1
            yield response.follow(next_page, callback = self.parse_area,cb_kwargs= dict(base_url=base_url))


# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        zero_node = current = ListNode(0)
        carry = 0

        while carry or l1 or l2:
            if l1:
                carry += l1.val
                l1 = l1.next
            if l2:
                carry += l2.val
                l2 = l2.next
            current.next = ListNode(carry % 10)
            current = current.next
            carry //= 10

        return zero_node.next


from Practice_tasks.practice_task_2.paradox.monty_hall_paradox.analyst import Analyst
from Practice_tasks.practice_task_2.paradox.monty_hall_paradox.game_ai import GameAI

def monty_hall(n: int) -> str:
    """
    Функция запуска симуляции для проверки парадокса.
    Результат n-го кол-ва само-игр компьютера заносится в анализатор.
    В конце симуляции анализатор возвращает статистику в консоль,
    где прописаны вероятности побед при разных стратегиях.
    :param n: Кол-во само-игр компьютера.
    :return: Статистика в виде строки
    """
    analyst = Analyst()
    game = GameAI()

    for _ in range(n):
        game.prepare()
        game.start()
        result = game.result()
        analyst.parse_result(result)

    return str(analyst)

if __name__ == "__main__":
    print(monty_hall(10000))


#!/usr/bin/env python3

try:
    from ev3dev2.sensor.lego import TouchSensor
except:
    pass

import random

from .ports import Ports

class PortableTouchSensor:
    '''
    A wrapper to ev3dev.sensor.lego.TouchSensor
    If ev3dev is not available or the there is no sensor plugged in
    then it pretends to read the sensor but does nothing.
    Useful for running the program without an EV3 or having all the ports plugged in.
    '''
    def __init__(self, port_name: str):
        self.port_name = port_name

        if not Ports.simulated:
            try:
                self.ev3_sensor = TouchSensor(self.port_name)
            except:
                print('Failed to create TouchSensor on {}.'.format(self.port_name))
        else:
            self.ev3_sensor = None

    def is_pressed(self, value_if_dummy: bool = None) -> bool:
        if self.ev3_sensor is not None:
            return bool(self.ev3_sensor.is_pressed)
        else:
            if value_if_dummy is not None:
                return value_if_dummy
            else:
                return bool(random.getrandbits(1))


"""
Contains the display tool.
This tool is often implemented both by server and console.
"""

import azcam
from azcam.header import Header, ObjectHeaderMethods
from azcam.tools import Tools

class Display(Tools, ObjectHeaderMethods):
    """
    The base display tool for server and consoles.
    Usually implemented as the "display" tool.
    """

    def __init__(self, tool_id="display", description=None):
        Tools.__init__(self, tool_id, description)

        # create the display Header object
        self.header = Header("Display")
        self.header.set_header("display", 2)

        # set default display server
        self.default_display = 0

        # allow initialization of display on server in exposure
        try:
            azcam.db.tools_init["display"] = self
        except AttributeError:
            pass

    def initialize(self):
        """
        Initialize display.
        """

        if self.initialized:
            return

        if not self.enabled:
            azcam.AzcamWarning("Display is not enabled")
            return

        self.set_display(self.default_display)

        return

    def set_display(self, display_number):
        """
        Set the current display by number.

        :param int display_number: Number for display to be used (0->N)
        :return None:
        """

        return

    def display(self, image, extension_number=-1):
        """
        Display a file on the image display.
        If specified for an MEF file, only extension_number is displayed.

        :param image: a filename or an image object
        :param int extension_number: FITS extension number of image, -1 for all
        :return None:
        """

        raise NotImplementedError


from KEY.GetKey import GetToken
from Locations.GetMyLocation import GetMyLocation
from Clans.Get import GetTagbyPart
from Clans.Members.Get import GetMembers
from Clans.Members.Export import ExportMembers

NameKey = "Minha Nova Key 2"
Pais = "BR"
clanName = "The resistance"
InitTag = "#9V2Y"
OutputPath = "D:\\Seleção Prime Control\\Atividade Pratica\\Process\\Criação_Chave\\"
NameReport = "Report"
Header = ["nome","level","troféus","papel"]

Token =  GetToken(NameKey)
LocationID = GetMyLocation(Pais,Token)
TagClan = GetTagbyPart(Name=clanName,Token=Token,InitTag=InitTag,IDLocation=LocationID)
Members = GetMembers(TagClan=TagClan,Token=Token)
ExportMembers(ListMembers=Members,OutputPath=OutputPath,NameReport=NameReport,Header=Header)


n, m = map(int, input().split())
edges = []
for i in range(m):
    a, b = map(int, input().split())
    edges.append((a, b))

friends = []
for i in range(1, n+1):
    friend = []
    for a, b in edges:
        if i == a:
            friend.append(b)
        elif i == b:
            friend.append(a)
    friends.append(friend)

for i in range(1, n+1):
    friend = friends[i-1]
    ff = []
    for f in friend:
        ff += friends[f-1]
    print(len(list(set(ff) - set([i]) - set(friend))))


#!/usr/bin/python3
"""
base class
"""

import json
import csv

class Base:
    """
    base
    """

    __nb_objects = 0

    def __init__(self, id=None):
        """
        function that create an object from json file
        """

        if id is None:
            Base.__nb_objects += 1
            self.id = self.__nb_objects
        else:
            self.id = id

        """
        Dictionary to JSON string
        """

    @staticmethod
    def to_json_string(list_dictionaries):
        """
        static method that returns the json string
        """

        if list_dictionaries is None or list_dictionaries == []:
            return "[]"
        return json.dumps(list_dictionaries)

    """
    JSON STRING TO FILE
    """
    @classmethod
    def save_to_file(cls, list_objs):
        """
        method that write the JSON string to file
        """
        filename = cls.__name__ + ".json"
        list_dic = []

        if list_objs is None:
            with open(filename, "w") as f:
                f.write(cls.to_json_string(list_dic))
            return
        for obj in list_objs:
            list_dic.append(obj.to_dictionary())
        with open(filename, "w") as f:
            f.write(cls.to_json_string(list_dic))

    """
    JSON string to dictionary
    """
    @staticmethod
    def from_json_string(json_string):
        """
        hat returns the list of the JSON string
        representation json_string
        """

        if json_string is None or json_string == "":
            return []
        else:
            return json.loads(json_string)

    """
    Dictionary to Instance
    """
    @classmethod
    def create(cls, **dictionary):
        """
        method that returns an instance with
        all attributes already set
        """

        if cls.__name__ == "Rectangle":
            dummy = cls(1, 1)
        else:
            dummy = cls(1)
        dummy.update(**dictionary)
        return(dummy)

    """
    File to instances
    """
    @classmethod
    def load_from_file(cls):
        """
        that returns a list of instances:
        """

        filename = cls.__name__ + ".json"

        try:
            with open(filename, "r") as f:
                lis_obj = cls.from_json_string(f.read())
                return [cls.create(**dic) for dic in lis_obj]
        except FileNotFoundError:
            return []

    """
     JSON ok, but CSV?
    """
    @classmethod
    def save_to_file_csv(cls, list_objs):
        """
        method that serializez a list to file csv
        """

        filename = cls.__name__ + ".csv"
        with open(filename, "w", newline='') as f:
            if list_objs is None or list_objs == []:
                f.write("[]")
            else:
                if cls.__name__ == "Rectangle":
                    fieldnames = ['id', 'width', 'height', 'x', 'y']
                elif cls.__name__ == "Square":
                    fieldnames = ['id', 'size', 'x', 'y']
                writer = csv.DictWriter(f, fieldnames=fieldnames)
                writer.writeheader()
                for obj in list_objs:
                    writer.writerow(obj.to_dictionary())

    @classmethod
    def load_from_file_csv(cls):
        """
        method that deserializes in csv
        """
        filename = cls.__name__ + ".csv"

        try:
            with open(filename, "r") as f:
                instanceList = []
                reader = csv.DictReader(f)
                for row in reader:
                    for key, value in row.items():
                        row[key] = int(value)
                    instanceList.append(cls.create(**row))
                return instanceList
        except FileNotFoundError:
            return []


from django.shortcuts import get_object_or_404
from rest_framework import filters, permissions, viewsets
from rest_framework.mixins import CreateModelMixin, ListModelMixin
from rest_framework.pagination import LimitOffsetPagination

from posts.models import Group, Post
from .permissions import IsAuthorOrReadOnly
from .serializers import (CommentSerializer, FollowSerializer, GroupSerializer,
                          PostSerializer)

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (IsAuthorOrReadOnly,)
    pagination_class = LimitOffsetPagination

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

class GroupViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

class CommentViewSet(viewsets.ModelViewSet):
    serializer_class = CommentSerializer
    permission_classes = (IsAuthorOrReadOnly,)

    def get_queryset(self):
        post = get_object_or_404(Post, id=self.kwargs.get('post_id'))
        comments = post.comments.all()
        return comments

    def perform_create(self, serializer):
        post = get_object_or_404(Post, id=self.kwargs.get('post_id'))
        serializer.save(author=self.request.user, post=post)

class FollowViewSet(CreateModelMixin, ListModelMixin, viewsets.GenericViewSet):
    serializer_class = FollowSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_backends = (filters.SearchFilter,)
    search_fields = ('following__username',)

    def get_queryset(self):
        return self.request.user.follower.all()

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)


heights = []
for _ in range(9):
    heights.append(int(input()))

heights.sort()

value = sum(heights)

pos = []

for i in range(len(heights)):
    for j in range(i+1, len(heights)):
        if value - (heights[i] + heights[j]) == 100:
            pos = [i, j]

for i in range(len(heights)):
    if i != pos[0] and i != pos[1]:
        print(heights[i])


import torch
import torch.nn.functional as fn
from RL.common import get_distribution_from_logits

# The network is very small
FORCE_CPU = True
device = torch.device("cuda" if torch.cuda.is_available() and not FORCE_CPU else "cpu")

if device.type == 'cuda':
    print(torch.cuda.get_device_name(0))
    print('Memory Usage:')
    print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
    print('Cached:   ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')

class GeneralGameTrajectories(torch.utils.data.Dataset):
    def __init__(self, rollouts, normalize_advantages=True):
        super(GeneralGameTrajectories).__init__()

        traj_keys = ["states", "actions", "action_masks", "returns",
                     "advantages", "old_pi_logits"]

        self.data = {}
        for k in traj_keys:
            self.data[k] = torch.cat([d[k] for d in rollouts])

        # Normalize advantages:
        if normalize_advantages:
            self.data['advantages'] = (self.data['advantages'] - self.data['advantages'].mean()) \
                                      / (self.data['advantages'].std() + 1e-5)

        # Get the size of the data
        self.n_samples = self.data['states'].size()[0]

    def __getitem__(self, index):
        return {k: self.data[k][index].to(device) for k in self.data}

    def __len__(self):
        return self.n_samples

class ActCritNetwork(torch.nn.Module):
    def __init__(self, input_size, num_actions, eval_only=True):
        super(ActCritNetwork, self).__init__()
        self.in_layer = torch.nn.Linear(input_size, 15)

        self.l_1 = torch.nn.Linear(15, 15)
        self.l_2 = torch.nn.Linear(15, 10)

        self.pl_1 = torch.nn.Linear(10, 10)
        self.pl_out = torch.nn.Linear(10, num_actions)

        self.v_1 = torch.nn.Linear(10, 5)
        self.v_out = torch.nn.Linear(5, 1)

        # Disable gradient calculation
        if eval_only:
            self.eval()

    def forward(self, input_data):
        # input_data = torch.from_numpy(x).float().detach()
        x = fn.relu(self.in_layer(input_data))
        x = fn.relu(self.l_1(x))
        x = fn.relu(self.l_2(x))

        pl = fn.relu(self.pl_1(x))
        vlr = fn.relu(self.v_1(x))

        return self.v_out(vlr), self.pl_out(pl)

    def sample_action(self, x, action_mask=None):
        """action_mask: boolean tensor of the same shape as x """

        _, policy_logits = self(x)
        distr = get_distribution_from_logits(policy_logits=policy_logits,
                                             action_mask=action_mask)

        return distr.sample()

class PPO(object):
    def __init__(self,
                 input_size,
                 num_actions,
                 lr=1e-3,
                 minibatch_size=128,
                 clip=0.2,
                 w_policy=1,
                 w_vf=0.5,
                 w_entropy=1,
                 last_epoch=0):

        self.network = ActCritNetwork(input_size=input_size,
                                      num_actions=num_actions,
                                      eval_only=False).to(device)
        # Optimizer
        self.optimizer = torch.optim.Adam(self.network.parameters(), lr=lr)

        self.w_policy = w_policy
        self.w_entropy = w_entropy
        self.w_vf = w_vf
        self.clip = clip
        self.minibatch_size = minibatch_size

        self.global_step = 0
        self.last_epoch = last_epoch
        self.writer = None

    def update(self, traj, epochs=5):
        """
        Input: trajectories / outputs new weights + update_done flag
        Expected Trajectory:
        dict of tensors with key/type/shape as follows:

        states float32 (N X D)
        actions int32 (N X 1)
        action_masks boolean (N X # Actions)
        returns float32 (N X 1)
        advantages float32 (N X 1)
        old_pi_logits float32 (N X 1)
        aux_data: dict of tensors {}

        NB: aux_data is a placeholder for meta data

        """
        # Construct the dataset

        # For K epochs iterate over each mini batch

        ds = GeneralGameTrajectories(traj)
        dl = torch.utils.data.DataLoader(ds,
                                         batch_size=self.minibatch_size,
                                         shuffle=True)

        for epoch in range(epochs):

            p_loss, v_loss, e_loss, c_loss = [], [], [], []

            for i, b in enumerate(dl):

                self.optimizer.zero_grad()
                policy_loss, value_loss,  entropy_loss = self.__ppo_loss(**b)
                loss = - self.w_policy * policy_loss + \
                       self.w_vf * value_loss + \
                       - self.w_entropy * entropy_loss

                # add loss to list
                p_loss.append(policy_loss.item())
                v_loss.append(value_loss.item())
                e_loss.append(entropy_loss.item())
                c_loss.append(loss.item())

                # Update
                loss.backward()
                self.optimizer.step()

        if self.writer:
            for l_label, l_value in zip(['loss', 'policy', 'value', 'entropy'],[c_loss, p_loss, v_loss, e_loss]):
                self.writer.add_scalar(f'Loss/{l_label}', torch.tensor(l_value).mean(), self.last_epoch)

            self.last_epoch += 1

        print(f"Loss after update {self.last_epoch} is {torch.tensor(c_loss).mean().item()}")
        return self.network.state_dict()

    def restore_from_checkpoint(netwokr_state, optimizer_state):
        """Restores the state of the network and the optimizer
        params:
            network_state: dict

        """
        self.network.load_state_dict(netwokr_state)
        self.optimizer.load_state_dict(optimizer_state)

    def __ppo_loss(self,
                   states,
                   actions,
                   returns,
                   old_pi_logits,
                   advantages,
                   action_masks=None,
                   **kwargs):
        # The input will be tensors
        # We get logits estimate
        v_hat, p_hat = self.network(states)
        distr = get_distribution_from_logits(p_hat, action_masks)

        # Value loss
        value_loss = torch.mean((v_hat - returns) ** 2)

        # Entropy loss
        entropy_loss = torch.mean(distr.entropy())

        # Policy Loss
        action_log_pi = p_hat.gather(1, actions)
        ratio = action_log_pi - old_pi_logits
        surr_1 = ratio * advantages
        surr_2 = torch.clamp(ratio, min=1.0 - self.clip, max=1 + self.clip) * advantages

        policy_loss = torch.min(surr_1, surr_2).mean()

        return policy_loss, value_loss,  entropy_loss


#1. Count Letters

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

def unique_english_letters(word):
  counter = 0
  for letter in letters:
    if letter in word:
      counter += 1
  return counter

print(unique_english_letters("mississippi"))
# should print 4
print(unique_english_letters("Apple"))
# should print 4

#2. Count X

def count_char_x(word, x):
  character_count = 0
  for letter in word:
    if letter == x:
      character_count += 1
  return character_count

print(count_char_x("mississippi", "s"))

print(count_char_x("mississippi", "m"))

#3. Check Name

def check_for_name(sentence, name):
  return name.lower() in sentence.lower()

#4 Every other letter

def every_other_letter(word):
  other_letter = ""
  for letter in range(0, len(word), 2):
    other_letter += word[letter]
  return other_letter

#5 Word length dict

def word_length_dictionary(words):
  word_length = {}
  for word in words:
    new_key = word
    new_value = len(new_key)
    word_length.update({new_key: new_value})
  return word_length

print(word_length_dictionary(["apple", "dog", "cat"]))
# should print {"apple":5, "dog": 3, "cat":3}
print(word_length_dictionary(["a", ""]))
# should print {"a": 1, "": 0}

#6 frequency counts

def frequency_dictionary(words):
  frequency = {}
  for word in words:
    if word not in frequency:
      frequency[word] = 0
    frequency [word] += 1
  return frequency

print(frequency_dictionary(["apple", "apple", "cat", 1]))
# should print {"apple":2, "cat":1, 1:1}
print(frequency_dictionary([0,0,0,0,0]))
# should print {0:5}

#Unique values

def unique_values(my_dictionary):
  unique_values = []
  for value in my_dictionary.values():
    if value not in unique_values:
      unique_values.append(value)
  return len(unique_values)

print(unique_values({0:3, 1:1, 4:1, 5:3}))
# should print 2
print(unique_values({0:3, 1:3, 4:3, 5:3}))
# should print 1

#count first letter

def count_first_letter(names):
  letters = {}
  for key in names.keys():
    first_letter = key[0]
    if first_letter not in letters:
      letters[first_letter] = 0
    letters[first_letter] += len(names[key])
  return letters

print(count_first_letter({"Stark": ["Ned", "Robb", "Sansa"], "Snow" : ["Jon"], "Lannister": ["Jaime", "Cersei", "Tywin"]}))
# should print {"S": 4, "L": 3}
print(count_first_letter({"Stark": ["Ned", "Robb", "Sansa"], "Snow" : ["Jon"], "Sannister": ["Jaime", "Cersei", "Tywin"]}))
# should print {"S": 7}


import logging

import numpy as np

logger = logging.getLogger(__name__)

"""
Extrapolation functions
	Note: these are called internally
"""

def log_ratio(ds, to_height, from_height, from_name):
    """Logarithmic ratio law
    # Equation (2) in Andresen, G. et al (2015):
    # 	'Validation of Danish wind time series from a new global renewable
    # 	energy atlas for energy system analysis'.
    """

    wnd_spd = ds[from_name] * (
        np.log(to_height / ds["roughness"]) / np.log(ds[from_height] / ds["roughness"])
    )
    wnd_spd.attrs.update(
        {
            "long_name": f"extrapolated {to_height} m wind speed using log ratio",
            "units": "m s**-1",
        }
    )
    return wnd_spd

def log_law(ds, to_height, from_height, from_name):
    """Logarithmic (integration) law
    [3] S. Emeis, Wind Energy Meteorology (Springer, Berlin, 2013).
    """
    vonk = 0.4
    wnd_spd = ds[from_name] + (
        ds["ustar"] / vonk * np.log((to_height - ds["disph"]) / ds[from_height])
    )
    wnd_spd.attrs.update(
        {
            "long_name": f"extrapolated {to_height} m wind speed using log (integration) law",
            "units": "m s**-1",
        }
    )
    return wnd_spd

## Flux stability correction functions
def psi_linear(z, ds):
    """NOTE: This function does not perform well for high z/L
    Linear stability correction [1,2]
            z = to_height
            ds = dataset with variables: L

    Eval = 	0, if z/L <=0
                    linear if z/L > 0
    [1] Businger, J.A., Wyngaard, J.C., Izumi, Y., Bradley, E.F., 1971. Flux profile relationships in the atmospheric surface layer. J. Atmos. Sci. 28, 181–189.
    [2] Dyer, A.J., 1974. A review of flux–profile relationships. Boundary-Layer Meteorol. 7, 363–372.
    """
    beta = 5.2
    ds["a"] = z / ds["L"]
    ds["psim"] = ds["a"] * 0  # create zeroes same length as dataset
    ds["psim"].values[0 < ds["a"]] = -beta * ds["a"].values[0 < ds["a"]]
    ds["psim"].values[ds["a"] <= 0] = 0
    return ds["psim"]

def psi_linearexp(z, ds):
    """Linear-exponential piecewise stability correction [1] (repeated in [2])
            z = to_height
            ds = dataset with variables: L

    [1] Emeis, S. (2013). Wind Energy Meteorology. Retrieved from http://link.springer.com/10.1007/978-3-642-30523-8 (Note: error in eq 3.21)
    [2] Rose, S., & Apt, J. (2016). Quantifying sources of uncertainty in reanalysis derived wind speed. Renewable Energy, 94, 157–165.
            https://doi.org/10.1016/j.renene.2016.03.028
    """
    aconst = 5
    A = 1
    B = 2 / 3
    C = 5
    D = 0.35
    ds["a"] = z / ds["L"]
    ds["psim"] = ds["a"] * 0  # create zeroes same length as dataset
    ds["psim"].values[(0 < ds["a"]) & (ds["a"] <= 0.5)] = (
        -aconst * ds["a"].values[(0 < ds["a"]) & (ds["a"] <= 0.5)]
    )
    ds["psim"].values[0.5 < ds["a"]] = -A * (
        ds["a"].values[0.5 < ds["a"]]
        + B
        * (ds["a"].values[0.5 < ds["a"]] - C / D)
        * np.exp(-D * ds["a"].values[0.5 < ds["a"]])
        + B * C / D
    )
    ds["psim"].values[ds["a"] <= 0] = 0
    return ds["psim"]

def psi_linearexpconst(z, ds, const=7):
    """Linear-exponential piecewise stability correction [1] (repeated in [2]) with constant plateau
            z = to_height
            ds = dataset with variables: L
            const = upper bound of z/L, after which = constant

    [1] Emeis, S. (2013). Wind Energy Meteorology. Retrieved from http://link.springer.com/10.1007/978-3-642-30523-8 (Note: error in eq 3.21)
    [2] Rose, S., & Apt, J. (2016). Quantifying sources of uncertainty in reanalysis derived wind speed. Renewable Energy, 94, 157–165.
            https://doi.org/10.1016/j.renene.2016.03.028
    """
    aconst = 5
    A = 1
    B = 2 / 3
    C = 5
    D = 0.35
    ds["a"] = z / ds["L"]
    ds["psim"] = ds["a"] * 0  # create zeroes same length as dataset
    ds["psim"].values[(0 < ds["a"]) & (ds["a"] <= 0.5)] = (
        -aconst * ds["a"].values[(0 < ds["a"]) & (ds["a"] <= 0.5)]
    )
    ds["psim"].values[0.5 < ds["a"]] = -A * (
        ds["a"].values[0.5 < ds["a"]]
        + B
        * (ds["a"].values[0.5 < ds["a"]] - C / D)
        * np.exp(-D * ds["a"].values[0.5 < ds["a"]])
        + B * C / D
    )
    ds["psim"].values[ds["a"] > const] = -A * (
        const + B * (const - C / D) * np.exp(-D * const) + B * C / D
    )
    ds["psim"].values[ds["a"] <= 0] = 0
    return ds["psim"]

def L_vph(ds):
    """Obuhkov length using virtual potential heat flux term [1] (described in detail in SI [2])

    [1] Emeis, S. (2013). Wind Energy Meteorology. Retrieved from http://link.springer.com/10.1007/978-3-642-30523-8 (Note: error in eq 3.21)
    [2] Rose, S., & Apt, J. (2016). Quantifying sources of uncertainty in reanalysis derived wind speed. Renewable Energy, 94, 157–165.
            https://doi.org/10.1016/j.renene.2016.03.028
    """
    vonk = 0.4  # Von Karman constant
    grav = 9.81  # gravitational acceleration in kg m s-2
    CPD = 1004  # specific heat of dry air at constant pressure J K-1 kg-1
    Le = 2.257e6  # latent heat of evaporation [J/kg]
    kp = 2 / 7  # Poisson constant
    Rd = 287  #  Ideal gas constant [J/kg/K]
    p0 = 1e5  # standard air pressure

    ds["p"] = ds["rhoa"] * Rd * ds["tlml"]
    ds["vphflux"] = (
        ds["hflux"] + 0.61 * CPD / Le * ds["tlml"] * (p0 / ds["p"]) ** kp * ds["eflux"]
    )
    ds["L"] = -(ds["tlml"] * ds["ustar"] ** 3 * CPD * ds["rhoa"]) / (
        vonk * grav * ds["vphflux"]
    )
    return ds["L"]

def winddir(ds):
    """Wind direction using lowest model layer"""

    ds["winddir"] = np.degrees(np.arctan(ds["ulml"] / ds["vlml"]))
    ds["winddir"].values[ds["vlml"] < 0] += 180
    ds["winddir"].values[(ds["vlml"] > 0) & (ds["ulml"] < 0)] += 360
    return ds["winddir"]

def _log_law_flux(
    ds, to_height, from_height, from_name, psifn, Lfn=L_vph
):  # pylint: disable=unused-argument
    """Compute logarithmic (integration) law given stability correction fn in terms of Obukhov length (derived from heat flux) [1]
    Called by: log_law_flux_**

    [1] Sharan, M., & Aditi. (2009).
            Performance of various similarity functions for nondimensional wind and temperature profiles in the surface layer in stable conditions.
            Atmospheric Research, 94(2), 246–253.
            https://doi.org/10.1016/j.atmosres.2009.05.014
    """
    vonk = 0.4  # Von Karman constant
    ds["L"] = L_vph(ds)

    wnd_spd = ds[from_name] + ds["ustar"] / vonk * (
        np.log((to_height - ds["disph"]) / ds[from_height])
        - psifn(to_height, ds[["L", "roughness"]])
    )
    wnd_spd.attrs.update(
        {
            "long_name": f"extrapolated {to_height} m wind speed using log (integration) law and stability correction {psifn}",
            "units": "m s**-1",
        }
    )
    return wnd_spd

def log_law_flux_linear(ds, to_height, from_height, from_name):
    """Logarithmic (integration) law with linear stability correction in terms of Obukhov length"""
    return _log_law_flux(ds, to_height, from_height, from_name, psi_linear)

def log_law_flux_linearexp(ds, to_height, from_height, from_name):
    """Logarithmic (integration) law with piecewise linear-exponential stability correction in terms of Obukhov length"""
    return _log_law_flux(ds, to_height, from_height, from_name, psi_linearexp)

def log_law_flux_linearexpconst(ds, to_height, from_height, from_name):
    """Logarithmic (integration) law with piecewise linear-exponential-constant stability correction in terms of Obukhov length"""
    return _log_law_flux(ds, to_height, from_height, from_name, psi_linearexpconst)

"""
Main call (from convert.convert_wind)
"""

def extrapolate_wind_speed(
    ds, to_height, extrap_fn=log_ratio, from_height=None, var_height=None
):
    """Extrapolate the wind speed from a given height above ground to another.

    If ds already contains a key refering to wind speeds at the desired to_height,
    no conversion is done and the wind speeds are directly returned.

    Otherwise, extrapolates according to (1) extrap_fn and (2) heights

    Parameters
    ----------
    ds : xarray.Dataset
            Dataset containing the wind speed time-series
    to_height : int|float
            Height (m) to which the wind speeds are extrapolated
    extrap_fn : function for wind speed extrapolation
            log_ratio : wind speed follows the logarithmic ratio law as desribed in [1]
            log_law : wind speed follows logarithmic (integration) law described in [3]
            power_law : wind speed follows power law (with fixed alpha), e.g., in [4]
    from_height : int
            (Optional)
            Height (m) from which the wind speeds are interpolated to 'to_height'.
            If not provided, the closest height to 'to_height' is selected.
    var_height : str
            (Optional)
            suffix of variables in ds corresponding to variable height
            e.g., `lml` => height contained in `hlml`, wind speed contained in `wndlml`

    Returns
    -------
    da : xarray.DataArray
            DataArray containing the extrapolated wind speeds. Name of the DataArray
            is 'wnd{to_height:d}'.

    References
    ----------
    [1] Equation (2) in Andresen, G. et al (2015):
            'Validation of Danish wind time series from a new global renewable
            energy atlas for energy system analysis'.
    [2] https://en.wikipedia.org/w/index.php?title=Roughness_length&oldid=862127433,
            Retrieved 2019-02-15.
    [3] S. Emeis, Wind Energy Meteorology (Springer, Berlin, 2013).
    [4] Archer, C.L., Jacobson, M.Z., 2005. Evaluation of global wind power.
            Journal of Geophysical Research 110, D12110.
    """

    to_name = "wnd{h:0d}m".format(h=int(to_height))
    if to_name in ds:
        # already found wind speed at given height in dataset
        return ds[to_name]

    # Sanitize roughness for logarithm: 0.0002 corresponds to open water [2]
    ds["roughness"].values[ds["roughness"].values <= 0.0] = 0.0002

    if not from_height is None:
        # passed a from_height
        if not var_height is None:
            raise AssertionError(
                "Cannot pass both from_height and var_height to extrapolate_wind_speed"
            )
        from_name = "wnd{h:0d}m".format(h=int(from_height))
        ds["from_height"] = from_height

        wnd_spd = extrap_fn(ds, to_height, "from_height", from_name)
        wnd_spd.attrs["long_name"] = (
            wnd_spd.attrs["long_name"] + ", " + f"from fixed height = {from_height}"
        )

    elif not var_height is None:
        # passed a variable height (eg lml)
        # set variable names
        from_height = f"h{var_height}"
        from_name = f"wnd{var_height}"

        wnd_spd = extrap_fn(ds, to_height, from_height, from_name)
        wnd_spd.attrs["long_name"] = (
            wnd_spd.attrs["long_name"] + ", " + f"from variable height = {var_height}"
        )

    else:
        # based on nearest height
        heights = np.asarray([int(s[3:-1]) for s in ds if s.startswith("wnd")])
        if len(heights) == 0:
            raise AssertionError("Wind speed is not in dataset")

        from_height = heights[np.argmin(np.abs(heights - to_height))]
        from_name = "wnd{h:0d}m".format(h=int(from_height))
        ds["from_height"] = from_height

        wnd_spd = extrap_fn(ds, to_height, "from_height", from_name)
        wnd_spd.attrs["long_name"] = (
            wnd_spd.attrs["long_name"] + ", " + f"from nearest height = {from_height}"
        )

    return wnd_spd.rename(to_name)


def fibonacci(n, max_value):
    d = {}
    fib_prev_prev = 0
    fib_prev = 1
    fib = 0
    while True:
        fib = fib_prev + fib_prev_prev
        d[fib] = 1
        fib_prev, fib_prev_prev = fib, fib_prev
        if fib > max_value:
            break
    return d

def solution(A):
    len_A = len(A)
    fib_dict = fibonacci(len_A + 1, len_A)
    jumps = 1
    last_jump_position = set()
    last_jump_position.add(-1)
    while True:
        next_jump_position = set()
        for last_jump in last_jump_position:
            for i in xrange(last_jump+1, len_A+1):
                can_jump = True if i == len_A or A[i] else False
                if can_jump and fib_dict.get(i-last_jump, 0):
                    if i == len_A:
                        return jumps
                    elif i not in last_jump_position:
                        next_jump_position.add(i)
        jumps += 1
        if not next_jump_position:
            return -1
        last_jump_position = next_jump_position

assert solution([0,0,0,1,1,0,1,0,0,0,0]) == 3
assert solution([]) == 1
assert solution([1]) == 1


#!/usr/bin/python3
def add_tuple(tuple_a=(), tuple_b=()):
    count_a = tuple_a.count
    count_b = tuple_b.count
    if count_a not in range(1) or count_b not in range(1):
        tuple_a += (0, 0)
        tuple_b += (0, 0)
    sum_a = tuple_a[0] + tuple_b[0]
    sum_b = tuple_a[1] + tuple_b[1]
    new_tuple = (sum_a, sum_b)
    return new_tuple


# сколько раз символ встречается в строке
string = 'Python Software Foundation'
print(string.count('o'))

# Нарисовать треугольник заданной размерности. Пример для размера 6:
# *
# **
# ***
# ****
# *****
# ******

def create_symb():
    a = "*"
    for i in range (0,7):
        print(a*i)

create_symb()

def create_symb1():
    a = "*"
    for i in range (0,15,3):
        print(a*i)

create_symb1()


import math

epsilon_0 = 8.854187817e-12  # Permitividad del vacío en F/m

#Funcion para capacitor placas paralelas sin dieléctrico
def capacitor_placas_paralelas(base, altura, voltaje, distancia):
    # Constantes
    epsilon_0 = 8.854187817e-12  # Permitividad del vacío en F/m

    # Cálculos
    # Área de las placas
    A = base * altura

    # Capacitancia
    C = (epsilon_0 * A)/distancia

    # Carga del capacitor
    Q = C * voltaje

    # Energía almacenada en el capacitor
    U = 0.5 * C * voltaje**2

    return C, Q, U

#Funcion para capacitor placas paralelas con dieléctrico
def capacitor_placas_paralelas_con_dielectrico(base, altura, voltaje, distancia, half):
    # Constantes
    epsilon_0 = 8.854187817e-12  # Permitividad del vacío en F/m
    K = 3.40  # Constante dieléctrica del Plexiglas

    # Cálculos
    # Área de las placas
    A = base * altura

    # Capacitancia con aire
    C0 = (epsilon_0 * A) / distancia

    # Capacitancia con dieléctrico

    C = (epsilon_0 * K * A) / distancia
    # Carga del capacitor
    Q = C0 * voltaje
    if half:
        C = (C0 + C) / 2
        U = 0.5 * ((C0 * voltaje**2) / 2 + (C * (voltaje/K)**2) / 2)
        sigma_libre = ((Q)/((K+1)*(A)))*2
        sigma_libre_plexiglas = sigma_libre * K
        sigma_ligada = sigma_libre_plexiglas * (1-(1/K))
        return C, Q, U, sigma_libre, sigma_libre_plexiglas, sigma_ligada

    else:
        U = 0.5 * C * (voltaje/K)**2
        sigma_libre_plexiglas = (Q)/(base*altura)
        sigma_ligada = sigma_libre_plexiglas * (1-(1/K))
        return C, Q, U, sigma_libre_plexiglas, sigma_ligada

# Función para capacitor esférico sin dieléctrico
def capacitor_esferico(r1, r2, voltaje):
    C = 4 * math.pi * epsilon_0 * (r1 * r2) / (r2 - r1)
    Q = C * voltaje
    U = 0.5 * C * voltaje**2
    return C, Q, U

# Función para capacitor esférico sin dieléctrico
def capacitor_esferico_diel(r1, r2, voltaje, k, half):
    epsilon_0 = 8.854187817e-12
    C0 = 4 * math.pi * epsilon_0 * (r1 * r2) / (r2 - r1)
    Q = C0 * voltaje

    if half:
        C_dielectrico = 0.5 * C0 * k
        C_vacio = 0.5 * C0
        C = C_dielectrico + C_vacio

        Q_libre_aire_inner = (Q/(1+k))*(1/(2*math.pi*r1**2))
        Q_libre_aire_outer = (Q/(1+k))*(1/(2*math.pi*r2**2))
        Q_libre_dielectrico_inner = Q_libre_aire_inner * k
        Q_libre_dielectrico_outer = Q_libre_aire_outer * k

        Q_ligada_r1 = Q_libre_dielectrico_inner* (1-(1/k))
        Q_ligada_r2 = Q_libre_dielectrico_outer* (1-(1/k))

        U = 0.5 * ((voltaje**2)*C0/2)/2 + ((((voltaje/k)**2)*C/2))/2

        return (C, Q, U,
                (Q_libre_aire_inner, Q_libre_aire_outer),
                (Q_libre_dielectrico_inner, Q_libre_dielectrico_outer),
                (Q_ligada_r1, Q_ligada_r2))

    else:
        C = C0 * k
        Q_libre_inner = Q/(4 * math.pi * r1**2)
        Q_libre_outer = Q/(4 * math.pi * r2**2)
        Q_ligada_inner = Q_libre_inner * (1-(1/k))
        Q_ligada_outer = Q_libre_outer * (1-(1/k))

        U = 0.5 * C * (voltaje/k)**2

        return (C, Q, U, (Q_libre_inner, Q_libre_outer), (Q_ligada_inner, Q_ligada_outer))

# Función para capacitor cilíndrico sin dieléctrico
def capacitor_cilindrico(r1, r2, L, voltaje):
    epsilon_0 = 8.854187817e-12  # Permitividad del vacío en F/m
    C = 2 * math.pi * epsilon_0 * L / math.log(r2/r1)
    Q = C * voltaje
    U = 0.5 * C * voltaje**2
    return C, Q, U

# Función para capacitor cilíndrico con dieléctrico
def capacitor_cilindrico_diel(r1, r2, L, voltaje, half):
    k = math.pi
    epsilon_0 = 8.854187817e-12

    C0 = (2*math.pi*epsilon_0*L)/(math.log(r2/r1))
    Q = C0 * voltaje

    if half:
        C_dielectrico = 0.5 * C0 * k
        C = 0.5 * C0 + C_dielectrico
        U = ((voltaje**2)*C0/2)/2 + ((((voltaje/k)**2)*C/2))/2

        rho_libre_air_inner = (Q/(1+k))*(1/(math.pi*r1*L))
        rho_libre_air_outer = (Q/(1+k))*(1/(math.pi*r2*L))
        rho_libre_dielectrico_inner =  rho_libre_air_inner * k
        rho_libre_dielectrico_outer = rho_libre_air_outer * k

        rho_ligada_inner = rho_libre_dielectrico_inner * (1-(1/k))
        rho_ligada_outer = rho_libre_dielectrico_outer * (1-(1/k))

        return (C, Q, U,
                (rho_libre_air_inner, rho_libre_air_outer),
                ( rho_libre_dielectrico_inner, rho_libre_dielectrico_outer),
                (rho_ligada_inner, rho_ligada_outer))

    else:
        C = C0 * k
        U = 0.5 * C * (voltaje/k)**2

        rho_libre_inner = Q / (2*math.pi*r1*L)
        rho_libre_outer = Q / (2*math.pi*r2*L)
        rho_ligada_inner = rho_libre_inner * (1-(1/k))
        rho_ligada_outer = rho_libre_outer * (1-(1/k))

        return (C, Q, U, (rho_libre_inner, rho_libre_outer), (rho_ligada_inner, rho_ligada_outer))


import os
import json
from . import utils
from .core import BKTree, Node

from ...utils import str_util
from ocr_structuring.utils.logging import logger

# 目录名称
SOURCE_DIR_NAME = 'data'
TREE_DIR_NAME = '.tree'

def load_from_disk(working_dir, tree_name, force=False):
    """
    从磁盘中读取数据入BK-TREE
        1. 先尝试从当前目录下的 .tree 目录下读取之前已经落盘的树文件
        2. 如果之前并没有落盘（或者树源文件MD5变化了)，则从源文件中生成树并落盘
        3. 如果Force为True，则强制重新从源文件中生成树
    :param working_dir: 工作目录
    :param tree_name:
    :param force: 是否强行生成
    :return:
    """
    logger.debug('Load BK tree: {}'.format(tree_name))
    source_dir = os.path.join(working_dir, SOURCE_DIR_NAME)
    tree_dir = os.path.join(working_dir, TREE_DIR_NAME)
    source_file_path = os.path.join(source_dir, tree_name)

    if not os.path.exists(source_file_path):
        logger.error('BK-Tree source file not exist: {}'.format(source_file_path))
        raise AssertionError('BK-Tree source file not exist: {}'.format(source_file_path))

    if not os.path.exists(tree_dir):
        os.makedirs(tree_dir)

    file_md5 = utils.md5(source_file_path)
    tree_file_path = os.path.join(tree_dir, file_md5 + '.json')

    if force:
        logger.debug('Force create a new tree')
        bk_tree = load_from_source_file(source_file_path)
        save_to_tree_file(bk_tree, tree_file_path)
    elif not os.path.exists(tree_file_path):  # 树文件不存在代表树之前没有落过盘
        logger.debug('Create a new tree since there is no cache')
        bk_tree = load_from_source_file(source_file_path)
        save_to_tree_file(bk_tree, tree_file_path)
    else:
        logger.debug('Load tree from tree file: %s' % tree_file_path)
        bk_tree = load_from_tree_file(tree_file_path)

    return bk_tree

class BKTreeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, Node):
            json_str = {
                'word': o.word,
                'children': {}
            }
            for dist, node in o.children.items():
                json_str['children'][str(dist)] = node
            return json_str
        else:
            return json.JSONEncoder.default(self, o)

class BKTreeDecoder(json.JSONDecoder):
    def __init__(self, *args, **kwargs):
        super(BKTreeDecoder, self).__init__(object_hook=self.dict_to_node, *args, **kwargs)

    @staticmethod
    def dict_to_node(d):
        if 'word' in d:
            n = Node(d['word'])
            for k, v in d['children'].items():
                n.children[int(k)] = v
            return n
        else:
            return d

def load_from_source_file(source_filepath):
    bk_tree = BKTree()
    with open(source_filepath, encoding='utf-8', mode='r') as f:
        logger.debug('build bktree...')
        for line in f.readlines():
            word = line.strip()
            word = str_util.str_sbc_2_dbc(word)
            bk_tree.insert_node(word)
        logger.debug('finish build bktree...')
    return bk_tree

def load_from_tree_file(tree_filepath):
    with open(tree_filepath, mode='r', encoding='utf-8') as f:
        root = json.load(f, cls=BKTreeDecoder)
    return BKTree(root)

def save_to_tree_file(bk_tree, bk_tree_filepath):
    with open(bk_tree_filepath, mode='w', encoding='utf-8') as f:
        json.dump(bk_tree.root, f, separators=(',', ':'), cls=BKTreeEncoder, ensure_ascii=False)


#!/usr/bin/python3

def safe_print_division(a, b):
    """
    A function that prints and returns the value of the division expression
    """

    result = None

    try:
        result = a/b
    except ZeroDivisionError:
        print("Can't divide by 0. Please try again.")
    finally:
        print("Inside result: {}".format(result))

    return result


#!/usr/bin/env python3
"""
Day 5 of Advent of Code 2017
http://adventofcode.com/2017/day/5
"""
from typing import List

def jump_list_steps(sequence: List[int]) -> int:
    """Compute number of steps needed to escape list."""
    current_idx = steps = 0

    within_sequence = True

    while within_sequence:
        jump_by = sequence[current_idx]

        # update the previous value before we jump
        sequence[current_idx] += 1

        current_idx += jump_by

        steps += 1

        if current_idx > len(sequence) - 1:
            # we're outside of the sequence
            within_sequence = False
            break

    return steps

def jump_list_steps_part2(sequence: List[int]) -> int:
    """Compute the number of steps needed to escape the list for part two."""
    current_idx = steps = 0
    within_sequence = True

    while within_sequence:
        jump_by = sequence[current_idx]

        if sequence[current_idx] >= 3:
            sequence[current_idx] -= 1
        else:
            sequence[current_idx] += 1

        current_idx += jump_by

        steps += 1

        if current_idx > len(sequence) - 1:
            within_sequence = False
            break

    return steps

if __name__ == '__main__':
    with open('data/day05.txt', 'rt') as f:
        data = [int(x) for x in f]

    assert jump_list_steps([0, 3, 0, 1, -3]) == 5
    assert jump_list_steps_part2([0, 3, 0, 1, -3]) == 10

    # send a copy of data as we mutate within this function
    print(jump_list_steps(data[:]))
    print(jump_list_steps_part2(data))


from django.shortcuts import render
from friend.models import *  # models
from django.shortcuts import render, get_object_or_404 # communication with the data base
from django.db.models import Q # advanced query conditions
from django.http import HttpResponse, HttpResponseRedirect
from friend.forms import *
from django.conf.urls.static import static

#for login
from django.contrib.auth.decorators import login_required

@login_required
def ajax_comments_status(request):
    comments = Comment_Page_Comment.objects.all()

    context = {
        'comments': comments,
        }

    return render(request, 'ajax/comments_status.txt', context)

@login_required
def ajax_energy_status(request):
    friend = request.user.friend
    if friend.energy < 100:
        friend.energy += 1
    friend.save()
    context = {
        'friend': friend,
        }

    return render(request, 'ajax/energy_status.txt', context)

@login_required
def ajax_diamond_status(request):
    friend = request.user.friend
    friend.diamonds += friend.diamond_gen
    friend.save()

    diamonds = friend.diamonds

    context = {
        'diamonds': diamonds,
        }

    return render(request, 'ajax/diamond_status.txt', context)