#!/usr/bin/env python
#
# Author: Qiming Sun <osirpt.sun@gmail.com>
#

import time
import numpy
import pyscf.lib as lib
import pyscf.lib.logger
import pyscf.ao2mo
from pyscf.cc import _ccsd
from pyscf.cc import ccdiis

# t2 as ijba
#TODO: optimize diis extrapolation

# default max_memory = 2000 MB
def kernel(cc, t1, t2, eris, max_cycle=50, tol=1e-8, tolnormt=1e-6,
           max_memory=2000, verbose=None):
    if verbose is None:
        verbose = cc.verbose
    log = lib.logger.Logger(cc.stdout, verbose)
    cput0 = (time.clock(), time.time())
    blksize = block_size(cc.nmo, cc.nocc, max_memory)
    log.debug('block size = %d, nocc = %d is divided into %d blocks',
              blksize, cc.nocc, int((cc.nocc+blksize-1)/blksize))
    eold = 0
    eccsd = 0
    damp = cc.diis()
    conv = False
    for istep in range(max_cycle):
        t1new, t2new = cc.update_amps(cc, t1, t2, eris, blksize)
        normt = numpy.linalg.norm(t1new-t1) + numpy.linalg.norm(t2new-t2)
        t1, t2 = t1new, t2new
        t1new = t2new = None
        t1, t2 = damp(t1, t2, istep, normt, eccsd-eold)
        eold, eccsd = eccsd, energy(cc, t1, t2, eris, blksize)
        log.info('istep = %d, E(CCSD) = %.15g, dE = %.9g, norm(t1,t2) = %.6g',
                 istep, eccsd, eccsd - eold, normt)
        cput0 = log.timer('CCSD iter', *cput0)
        if abs(eccsd-eold) < tol and normt < tolnormt:
            conv = True
            break
    return conv, eccsd, t1, t2

def update_amps(cc, t1, t2, eris, blksize=1):
    nocc = cc.nocc
    nmo = cc.nmo
    nvir = nmo - nocc
    nov = nocc*nvir
    fock = eris.fock
    t2shape = t2.shape
    t1new = numpy.zeros_like(t1)
    t2new = numpy.zeros_like(t2)

#** make_inter_F
    fov = fock[:nocc,nocc:].copy()

    foo = fock[:nocc,:nocc].copy()
    foo[range(nocc),range(nocc)] = 0
    foo += .5 * numpy.einsum('ia,ja->ij', fock[:nocc,nocc:], t1)

    fvv = fock[nocc:,nocc:].copy()
    fvv[range(nvir),range(nvir)] = 0
    fvv -= .5 * numpy.einsum('ia,ib->ab', t1, fock[:nocc,nocc:])

    #: woooo = numpy.einsum('la,ikja->ikjl', t1, eris.ooov)
    woooo = numpy.dot(eris.ooov.reshape(-1,nvir), t1.T).reshape((nocc,)*4)
    woooo = lib.transpose_sum(woooo.reshape(nocc*nocc,-1), inplace=True)
    woooo = woooo.reshape(nocc,nocc,nocc,nocc).transpose(0,2,1,3) + eris.oOoO

    for p0, p1 in prange(0, nocc, blksize):
# ==== read eris.ovvv ====
        eris_ovvv = _ccsd.unpack_tril(numpy.array( \
                eris.ovvv[p0:p1]).reshape((p1-p0)*nvir,-1))
        eris_ovvv = eris_ovvv.reshape(p1-p0,nvir,nvir,nvir)

        #: g2 = 2 * eris_ovvv - eris_ovvv.transpose(0,2,1,3)
        #: fvv += numpy.einsum('kc,kcba->ab', t1[p0:p1], g2)
        g2 = _ccsd.make_g0213(eris_ovvv, eris_ovvv, 2, -1)
        fvv += numpy.einsum('kc,kcba->ab', t1[p0:p1], g2)
        g2 = None

        #: g2 = 2 * eris.ooov[p0:p1] - eris.ooov[p0:p1].transpose(0,2,1,3)
        g2 = _ccsd.make_g0213(eris.ooov[p0:p1], eris.ooov[p0:p1], 2, -1)
        foo[:,p0:p1] += numpy.einsum('kc,jikc->ij', t1, g2)
        g2 = None

    #: tau = t2 + numpy.einsum('ia,jb->ijba', t1, t1)
    #: tmp = numpy.einsum('jicd,kcdb->kijb', tau, eris.ovvv)
    #: t2new += numpy.einsum('ka,kijb->ijba', -t1, tmp)
        #: eris_vovv = eris_ovvv.transpose(3,0,1,2).copy()
        eris_vovv = lib.transpose(eris_ovvv.reshape(-1,nvir))
        eris_vovv = eris_vovv.reshape(nvir,p1-p0,nvir,nvir)
        tmp = numpy.empty((nocc,nocc,nvir,p1-p0))
        for j0, j1 in prange(0, nocc, blksize):
            #: tau = t2[j0:j1] + numpy.einsum('ia,jb->ijba', t1[j0:j1], t1)
            tau = _ccsd.make_tau(t2[j0:j1], t1[j0:j1], t1)
            #: tmp[j0:j1] += numpy.einsum('ijcd,bkcd->ijbk', tau, eris_vovv)
            lib.dot(tau.reshape(-1,nvir*nvir),
                    eris_vovv.reshape(-1,nvir*nvir).T, 1,
                    tmp[j0:j1].reshape((j1-j0)*nocc,-1), 0)
        #: t2new += numpy.einsum('ka,jibk->ijba', -t1[p0:p1], tmp)
        t2new += lib.dot(tmp.transpose(1,0,2,3).reshape(-1,p1-p0),
                         t1[p0:p1], -1).reshape(nocc,nocc,nvir,nvir)
        tmp = eris_vovv = None
        #==== mem usage blksize*(nvir**3*2+nvir*nocc**2*2)

    #: woOVv += numpy.einsum('jabc,ic->jiba', eris.ovvv, t1)
    #: woOVv -= numpy.einsum('ikja,kb->jiba', eris.ooov, t1)
    #: t2new += woOVv
        #: woOVv = -numpy.einsum('kija,kb->ijab', eris.ooov[:,:,p0:p1], t1)
        tmp = eris.ooov[:,:,p0:p1].copy().reshape(nocc,-1)
        woOVv = lib.dot(tmp.T, t1, -1).reshape(nocc,p1-p0,nvir,nvir)
        #: woOVv += numpy.einsum('jabc,ic->ijab', eris_ovvv, t1)
        lib.dot(t1, eris_ovvv.reshape(-1,nvir).T, 1, woOVv.reshape(nocc,-1), 1)
        woOVv = woOVv.transpose(1,0,3,2).copy()
        t2new[p0:p1] += woOVv
        tmp = None

        #: woovv = numpy.einsum('ka,ijkb->ijba', t1, eris.ooov[p0:p1])
        #: woovv -= numpy.einsum('jc,icab->ijab', t1, eris_ovvv)
        woovv = lib.dot(eris.ooov[p0:p1].transpose(0,1,3,2).reshape(-1,nocc),
                        t1).reshape(p1-p0,nocc,nvir,nvir)
        for i in range(eris_ovvv.shape[0]):
            lib.dot(t1, eris_ovvv[i].reshape(nvir,-1), -1,
                    woovv[i].reshape(nocc,-1), 1)

    #: theta = t2 * 2 - t2.transpose(0,1,3,2)
    #: t1new += numpy.einsum('ijcb,jcba->ia', theta, eris.ovvv)
        #: theta = t2[p0:p1] * 2 - t2[p0:p1].transpose(0,1,3,2)
        theta = _ccsd.make_g0132(t2[p0:p1], t2[p0:p1], 2, -1)
        #: t1new += numpy.einsum('jibc,jcba->ia', theta, eris_ovvv)
        lib.dot(theta.transpose(1,0,3,2).reshape(nocc,-1),
                eris_ovvv.reshape(-1,nvir), 1, t1new, 1)
        eris_ovvv = None
        #==== mem usage blksize*(nvir**3+nocc*nvir**2*4)

# ==== read eris.oOVv ====
        eris_oOVv = numpy.array(eris.oOVv[p0:p1])
        #==== mem usage blksize*(nocc*nvir**2*4)

        t2new[p0:p1] += eris_oOVv * .5
    #: g2 = 2 * eris.oOVv - eris.oOVv.transpose(1,0,2,3)
    #: fov = fock[:nocc,nocc:] + numpy.einsum('kc,ikca->ia', t1, g2)
        g2 = _ccsd.make_g0132(eris_oOVv, eris_oOVv, 2, -1)
        fov[p0:p1] += numpy.einsum('kc,ikca->ia', t1, g2)
        g2 = None
        #==== mem usage blksize*(nocc*nvir**2*5)

    #: theta = t2 * 2 - t2.transpose(1,0,2,3)
    #: t1new += numpy.einsum('jb,ijba->ia', fov, theta)
    #: t1new -= numpy.einsum('ikjb,kjba->ia', eris.ooov, theta)
        #theta = t2[p0:p1] * 2 - t2[p0:p1].transpose(0,1,3,2)
        t1new += numpy.einsum('ia,ijba->jb', fov[p0:p1], theta)
        #: t1new -= numpy.einsum('kijb,kjba->ia', eris.ooov[p0:p1], theta)
        lib.dot(eris.ooov[p0:p1].transpose(1,0,2,3).reshape(nocc,-1),
                theta.reshape(-1,nvir), -1, t1new, 1)

    #: woOVv += eris.oOVv
    #: theta = t2 * 2 - t2.transpose(1,0,2,3)
    #: tau = theta - numpy.einsum('ic,kb->ikcb', t1, t1*2)
    #: woOVv += .5 * numpy.einsum('jkca,ikcb->jiba', eris.oOVv, tau)
    #: woOVv -= .5 * numpy.einsum('jkac,ikcb->jiba', eris.oOVv, t2)
    #: t2new += numpy.einsum('ikca,kjbc->ijba', theta, woOVv)
        theta = theta.transpose(1,2,3,0).copy()
        woOVv += eris_oOVv
        woOVv = woOVv.transpose(1,2,3,0).copy()
        eris_OVvo = eris_oOVv.transpose(1,2,3,0).reshape(nov,-1)
        eris_VoOv = eris_oOVv.transpose(2,0,1,3).reshape(-1,nov)
        for j0, j1 in prange(0, nocc, blksize):
            t2iajb = t2[j0:j1].transpose(0,3,1,2).copy()
            #: woOVv[j0:j1] -= .5 * numpy.einsum('ikac,jbkc->jbai', eris_oOVv, t2iajb)
            lib.dot(t2iajb.reshape(-1,nov), eris_VoOv.T,
                    -.5, woOVv[j0:j1].reshape((j1-j0)*nvir,-1), 1)
            #: tau = t2iajb*2 - t2[j0:j1].transpose(0,2,1,3) \
            #:         - numpy.einsum('ic,kb->ibkc', t1[j0:j1]*2, t1)
            tau = _ccsd.make_tau(t2[j0:j1], t1[j0:j1]*2, t1)
            tau = _ccsd.make_g0213(t2iajb, tau, 2, -1, inplace=True)
            #tau = t2iajb*2 - tau.transpose(0,2,1,3)
            #: woOVv[j0:j1] += .5 * numpy.einsum('ikca,jbkc->jbai', eris_oOVv, tau)
            lib.dot(tau.reshape(-1,nov), eris_OVvo,
                    .5, woOVv[j0:j1].reshape((j1-j0)*nvir,-1), 1)

            #theta = t2[p0:p1] * 2 - t2[p0:p1].transpose(0,1,3,2)
            #t2new[j0:j1] += numpy.einsum('kiac,jbck->jiab', theta, woOVv[j0:j1])
            _ccsd.madd_admn_bcmn(woOVv[j0:j1], theta, t2new[j0:j1])
            #==== mem usage blksize*(nocc*nvir**2*8)
        t2iajb = tau = None
        theta = woOVv = eris_VoOv = eris_OVvo = None
        #==== mem usage blksize*(nocc*nvir**2*2)

    #: fvv -= numpy.einsum('ijca,ijcb->ab', theta, eris.oOVv)
    #: foo += numpy.einsum('ikba,jkba->ij', eris.oOVv, theta)
        #: tau = t2[p0:p1] + numpy.einsum('ia,jb->ijba', t1[p0:p1], t1*.5)
        tau = _ccsd.make_tau(t2[p0:p1], t1[p0:p1]*.5, t1)
        #: theta = tau * 2 - tau.transpose(0,1,3,2)
        theta = _ccsd.make_g0132(tau, tau, 2, -1)
        #: foo += numpy.einsum('kiab,kjab->ij', eris_oOVv, theta)
        #: fvv -= numpy.einsum('ijca,ijcb->ab', theta, eris_oOVv)
        for i in range(eris_oOVv.shape[0]):
            lib.dot(eris_oOVv[i].reshape(nocc,-1),
                    theta[i].reshape(nocc,-1).T, 1, foo, 1)
        lib.dot(theta.reshape(-1,nvir).T, eris_oOVv.reshape(-1,nvir),
                -1, fvv, 1)
        tau = theta = None

# ==== read eris.oovv ====
        eris_oovv = numpy.array(eris.oovv[p0:p1])
        #==== mem usage blksize*(nocc*nvir**2*3)

        #: tmp  = numpy.einsum('ic,kjbc->kjib', t1, eris_oovv)
        #: tmp += numpy.einsum('ic,kjbc->kijb', t1, eris_oOVv)
        tmp = lib.dot(eris_oovv.reshape(-1,nvir),
                      t1.T).reshape(p1-p0,nocc,nvir,nocc).transpose(0,3,2,1).copy()
        lib.dot(eris_oOVv.reshape(-1,nvir), t1.T, 1, tmp.reshape(-1,nocc), 1)
        tmp = tmp.transpose(3,1,2,0)
        #: t2new += numpy.einsum('ka,ijbk->ijba', -t1[p0:p1], tmp)
        lib.dot(tmp.reshape(-1,p1-p0), t1[p0:p1], -1, t2new.reshape(-1,nvir), 1)
        tmp = None

    #: g2 = 2 * eris.oOVv - eris.oovv
    #: t1new += numpy.einsum('jb,ijba->ia', t1, g2)
        g2 = 2 * eris_oOVv - eris_oovv
        t1new[p0:p1] += numpy.einsum('jb,ijba->ia', t1, g2)
        g2 = None

    #: woVoV -= eris.oovv
    #: tau = .5*t2 + numpy.einsum('ia,jb->ijba', t1, t1)
    #: woVoV += numpy.einsum('ka,ijkb->ijab', t1, eris.ooov)
    #: woVoV += numpy.einsum('kjca,ikbc->ijab', tau, eris.oOVv)
        woovv -= eris_oovv
        woovv = woovv.transpose(1,3,0,2).copy()
        eris_oVOv = eris_oOVv.transpose(0,2,1,3).reshape(-1,nov)
        #==== mem usage blksize*(nocc*nvir**2*3)

    #: tau = t2 + numpy.einsum('ia,jb->ijba', t1, t1)
    #: woooo += numpy.einsum('ijba,klba->ijkl', eris.oOVv, tau)
        for j0, j1 in prange(0, nocc, blksize):
            #: tau = t2[j0:j1] + numpy.einsum('ia,jb->ijba', t1[j0:j1], t1)
            tau = _ccsd.make_tau(t2[j0:j1], t1[j0:j1], t1)
            #: woooo[p0:p1,:,j0:j1] += numpy.einsum('ijba,klba->ijkl', eris_oOVv, tau)
            lib.numpy_helper._dgemm('N', 'T', (p1-p0)*nocc, (j1-j0)*nocc, nvir*nvir,
                                    eris_oOVv.reshape(-1,nvir*nvir),
                                    tau.reshape(-1,nvir*nvir),
                                    woooo[p0:p1].reshape(-1,nocc*nocc), 1, 1,
                                    0, 0, j0*nocc)

            tau -= .5*t2[j0:j1]
            #: woovv[j0:j1] += numpy.einsum('jkac,ikbc->jaib', tau, eris_oOVv)
            tau = tau.transpose(0,2,1,3).reshape(-1,nov)
            lib.dot(tau, eris_oVOv.T,
                    1, woovv[j0:j1].reshape((j1-j0)*nvir,-1), 1)
            #==== mem usage blksize*(nocc*nvir**2*5)

        #: tau = t2[p0:p1] + numpy.einsum('ia,jb->ijba', t1[p0:p1], t1)
        tau = _ccsd.make_tau(t2[p0:p1], t1[p0:p1], t1)
        #: t2new += .5 * numpy.einsum('klij,klba->ijba', woooo[p0:p1], tau)
        lib.dot(woooo[p0:p1].reshape(-1,nocc*nocc).T,
                tau.reshape(-1,nvir*nvir), .5,
                t2new.reshape(nocc*nocc,-1), 1)
        eris_oovv = eris_oOVv = eris_oVOv = tau = None
        #==== mem usage blksize*(nocc*nvir**2*1)

        t2ibja = t2[p0:p1].transpose(0,2,1,3).copy()
        for j0, j1 in prange(0, nocc, blksize):
            #: t2new[j0:j1] += numpy.einsum('jbkc,kcia->jiba', woovv[j0:j1], t2ibja)
            _ccsd.madd_acmn_mnbd(woovv[j0:j1], t2ibja, t2new[j0:j1])

        t2iajb = t2[p0:p1].transpose(0,3,1,2).copy()
        for j0, j1 in prange(0, nocc, blksize):
            #: t2new[j0:j1] += numpy.einsum('jbkc,kcia->jiab', woovv[j0:j1], t2iajb)
            _ccsd.madd_admn_mnbc(woovv[j0:j1], t2iajb, t2new[j0:j1])
        t2ibja = t2iajb = woovv = None
        #==== mem usage blksize*(nocc*nvir**2*3)
# ==================

    ft_ij = foo + numpy.einsum('ja,ia->ij', .5*t1, fov)
    ft_ab = fvv - numpy.einsum('ia,ib->ab', .5*t1, fov)
    #: t2new += numpy.einsum('ijbc,ac->ijba', t2, ft_ab)
    #: t2new -= numpy.einsum('ki,kjba->ijba', ft_ij, t2)
    lib.dot(t2.reshape(-1,nvir), ft_ab.T, 1, t2new.reshape(-1,nvir), 1)
    lib.dot(ft_ij.T, t2.reshape(nocc,-1),-1, t2new.reshape(nocc,-1), 1)

    #: t2new = t2new + t2new.transpose(1,0,3,2)
    t2new_tril = numpy.empty((nocc*(nocc+1)//2,nvir,nvir))
    ij = 0
    for i in range(nocc):
        for j in range(i+1):
            t2new_tril[ij] = t2new[i,j] + t2new[j,i].T
            ij += 1
    t2new = None
    cc.add_wvvVV_(t1, t2, eris, t2new_tril, blksize)

    mo_e = fock.diagonal()
    eia = (mo_e[:nocc,None] - mo_e[None,nocc:])
    p0 = 0
    for i in range(nocc):
        djba = (eia[:i+1].reshape(-1,1) + eia[i].reshape(1,-1))
        t2new_tril[p0:p0+i+1] /= djba.reshape(i+1,nvir,nvir)
        p0 += i+1

    t2new = numpy.empty((nocc,nocc,nvir,nvir))
    ij = 0
    for i in range(nocc):
        for j in range(i):
            t2new[i,j] = t2new_tril[ij]
            t2new[j,i] = t2new_tril[ij].T
            ij += 1
        t2new[i,i] = t2new_tril[ij]
        ij += 1
    t2new_tril = None

#** update_amp_t1
    t1new += fock[:nocc,nocc:] \
           + numpy.einsum('ib,ab->ia', t1, fvv) \
           - numpy.einsum('ja,ji->ia', t1, foo)

    mo_e = fock.diagonal()
    eia = mo_e[:nocc,None] - mo_e[None,nocc:]
    t1new /= eia
#** end update_amp_t1

    return t1new, t2new

def energy(cc, t1, t2, eris, blksize=1):
    nocc = cc.nocc
    fock = eris.fock
    e = numpy.einsum('ia,ia', fock[:nocc,nocc:], t1)
    for p0, p1 in prange(0, nocc, blksize):
        tau = _ccsd.make_tau(t2[p0:p1], t1[p0:p1], t1)
        #theta = tau*2 - tau.transpose(0,1,3,2)
        theta = _ccsd.make_g0132(tau, tau, 2, -1)
        e += numpy.einsum('ijab,ijab', eris.oOVv[p0:p1], theta)
    return e

class CC(object):
    def __init__(self, mf):
        from pyscf import gto
        if isinstance(mf, gto.Mole):
            raise RuntimeError('''
You see this error message because of the API updates in pyscf v0.10.
In the new API, the first argument of CC class is HF objects.  Please see
http://sunqm.net/pyscf/code-rule.html#api-rules for the details of API conventions''')

        mol = mf.mol
        self.mol = mol
        self._scf = mf
        self.verbose = mol.verbose
        self.stdout = mol.stdout
        self.max_memory = mf.max_memory

        self.max_cycle = 50
        self.conv_tol = 1e-7
        self.conv_tol_normt = 1e-5
        self.diis_space = 6
        self.diis_start_cycle = 1

        self.nocc = mol.nelectron // 2
        self.nmo = mf.mo_energy.size

        self._conv = False
        self.emp2 = None
        self.ecc = None
        self.t1 = None
        self.t2 = None

    def init_amps(self, eris):
        nocc = self.nocc
        nmo = self.nmo
        nvir = nmo - nocc
        mo_e = eris.fock.diagonal()
        eia = mo_e[:nocc,None] - mo_e[None,nocc:]
        t2 = numpy.empty((nocc,nocc,nvir,nvir))
        self.emp2 = 0
        for i in range(nocc):
            djba = (eia.reshape(-1,1) + eia[i].reshape(1,-1)).reshape(-1)
            gi = eris.oOVv[i]
            t2i = (gi.reshape(-1)/djba).reshape(nocc,nvir,nvir)
            t2[i] = t2i
            self.emp2 += 4 * numpy.einsum('jab,jab', t2i[:i], gi[:i])
            self.emp2 += 2 * numpy.einsum('ab,ab'  , t2i[i] , gi[i] )
            self.emp2 -= 2 * numpy.einsum('jab,jba', t2i[:i], gi[:i])
            self.emp2 -=     numpy.einsum('ab,ba'  , t2i[i] , gi[i] )

        lib.logger.info(self, 'Init t2, MP2 energy = %.15g', self.emp2)
        t1 = numpy.zeros((nocc,nvir))
        return self.emp2, t1, t2

    def kernel(self, t1=None, t2=None):
        return self.ccsd(t1, t2)
    def ccsd(self, t1=None, t2=None):
        nocc = self.nocc
        nmo = self.nmo
        nvir = nmo - nocc
        eris = self.ao2mo()
        if t2 is None:
            emp2, _, t2 = self.init_amps(eris)
        if t1 is None:
            t1 = numpy.zeros((nocc,nvir))
        cput0 = (time.clock(), time.time())
        self._conv, self.ecc, self.t1, self.t2 = \
                kernel(self, t1, t2, eris, max_cycle=self.max_cycle,
                       tol=self.conv_tol,
                       tolnormt=self.conv_tol_normt,
                       verbose=self.verbose)
        if self._conv:
            lib.logger.info(self, 'CCSD converged')
            lib.logger.info(self, ' E(CCSD) = %.16g, E_corr = %.16g',
                            self.ecc+self._scf.hf_energy+self.mol.energy_nuc(),
                            self.ecc)
        else:
            lib.logger.info(self, 'CCSD does converge')
            lib.logger.info(self, ' E(CCSD) = %.16g, E_corr = %.16g',
                            self.ecc+self._scf.hf_energy+self.mol.energy_nuc(),
                            self.ecc)
        lib.logger.timer(self, 'CCSD', *cput0)
        return self.ecc, self.t1, self.t2

    def ao2mo(self):
        nocc = self.nocc
        nmo = self.nmo
        nvir = nmo - nocc
        eri1 = pyscf.ao2mo.incore.full(self._scf._eri, self._scf.mo_coeff)
        eri1 = pyscf.ao2mo.restore(1, eri1, nmo)
        eris = lambda:None
        eris.oOoO = eri1[:nocc,:nocc,:nocc,:nocc].transpose(0,2,1,3).copy()
        eris.ooov = eri1[:nocc,:nocc,:nocc,nocc:].copy()
        eris.oovv = eri1[:nocc,:nocc,nocc:,nocc:].copy()
        eris.oOVv = eri1[:nocc,nocc:,:nocc,nocc:].transpose(0,2,3,1).copy()
        ovvv = eri1[:nocc,nocc:,nocc:,nocc:].copy()
        eris.ovvv = numpy.empty((nocc,nvir,nvir*(nvir+1)//2))
        for i in range(nocc):
            for j in range(nvir):
                eris.ovvv[i,j] = lib.pack_tril(ovvv[i,j])
        eris.vvvv = pyscf.ao2mo.restore(4, eri1[nocc:,nocc:,nocc:,nocc:].copy(), nvir)
        eris.fock = numpy.diag(self._scf.mo_energy)
        return eris

    def add_wvvVV_(self, t1, t2, eris, t2new, blksize=1):
        nocc = self.nocc
        nvir = self.nmo - nocc
        #: tau = t2 + numpy.einsum('ia,jb->ijba', t1, t1)
        #: t2new += numpy.einsum('ijdc,bdca->ijba', tau, vvvv)
        tau = numpy.empty((nocc*(nocc+1)//2,nvir,nvir))
        p0 = 0
        for i in range(nocc):
            #: tau[p0:p0+i+1] += numpy.einsum('a,jb->jba', t1[i], t1[:i+1])
            tau[p0:p0+i+1] = _ccsd.make_tau(t2[i,:i+1], t1[i:i+1], t1[:i+1])
            p0 += i + 1

        p0 = 0
        for b in range(nvir):
            buf = _ccsd.unpack_tril(eris.vvvv[p0:p0+b+1])
            #: t2new[i,:i+1, b] += numpy.einsum('xdc,dca->xa', tau[:,:b+1], buf)
            lib.numpy_helper._dgemm('N', 'N', nocc*(nocc+1)//2, nvir, (b+1)*nvir,
                                    tau.reshape(-1,nvir*nvir), buf.reshape(-1,nvir),
                                    t2new.reshape(-1,nvir*nvir), 1, 1,
                                    0, 0, b*nvir)

            #: t2new[i,:i+1,:b] += numpy.einsum('xc,bac->xba', tau[:,b], buf[:b])
            if b > 0:
                lib.numpy_helper._dgemm('N', 'T', nocc*(nocc+1)//2, b*nvir, nvir,
                                        tau.reshape(-1,nvir*nvir), buf.reshape(-1,nvir),
                                        t2new.reshape(-1,nvir*nvir), 1, 1,
                                        b*nvir, 0, 0)
            p0 += b+1
        return t2new
    def add_wvvVV(self, t1, t2, eris, blksize=1):
        nocc = self.nocc
        nvir = self.nmo - nocc
        t2new = numpy.zeros((nocc*(nocc+1)//2,nvir,nvir))
        return self.add_wvvVV_(t1, t2, eris, t2new, blksize=1)

    def update_amps(self, *args, **kwargs):
        return update_amps(*args, **kwargs)

    def diis(self):
        nocc = self.nocc
        nvir = self.nmo-self.nocc
        nov = nocc*nvir
        damp = ccdiis.DIIS(self)
        damp.space = self.diis_space
        damp.min_space = 1
        def fupdate(t1, t2, istep, normt, de):
            if istep > self.diis_start_cycle and \
               (abs(de) < 1e-3 and istep % 3 == 0):
                tbuf = numpy.empty(nov*(nov+1))
                tbuf[:nov] = t1.ravel()
                tbuf[nov:] = t2.ravel()
                tbuf = damp.update(tbuf)
                t1 = tbuf[:nov].reshape(nocc,nvir)
                t2 = tbuf[nov:].reshape(nocc,nocc,nvir,nvir)
                lib.logger.debug(self, 'DIIS for step %d', istep)
            return t1, t2
        return fupdate

# assume nvir > nocc, minimal requirements on memory in loop of update_amps
def _memory_usage_inloop(nmo, nocc):
    nvir = nmo - nocc
    v = max(nvir**3*2+nvir*nocc**2*2,
            nvir**3+nocc*nvir**2*5+nvir*nocc**2,
            nocc*nvir**2*9)
    return v*8/1e6
# assume nvir > nocc, minimal requirements on memory
def _memory_usage(nmo, nocc):
    nvir = nmo - nocc
    v = _memory_usage_inloop(nmo, nocc)*1e6/8
    v = max(v, nocc*(nocc+1)//2*nvir**2) + (nocc*nvir)**2*2
    return v*8/1e6

def block_size(nmo, nocc, max_memory):
    nvir = nmo - nocc
    unit = _memory_usage_inloop(nmo, nocc)*1e6/8
    rest = max_memory*1e6/8 - (nocc*nvir)**2*2
    return min(nocc, max(1, int(rest/unit)))

def prange(start, end, step):
    for i in range(start, end, step):
        yield i, min(i+step, end)

if __name__ == '__main__':
    from pyscf import gto
    from pyscf import scf

    mol = gto.Mole()
    mol.verbose = 5
    #mol.output = 'out_h2o'
    mol.atom = [
        [8 , (0. , 0.     , 0.)],
        [1 , (0. , -0.757 , 0.587)],
        [1 , (0. , 0.757  , 0.587)]]

    mol.basis = {'H': 'cc-pvdz',
                 'O': 'cc-pvdz',}
    mol.build()
    rhf = scf.RHF(mol)
    rhf.scf() # -76.0267656731

    mcc = CC(rhf)
    eris = mcc.ao2mo()
    emp2, t1, t2 = mcc.init_amps(eris)
    print(abs(t2).sum() - 4.9556571218177)
    print(emp2 - -0.2040199672883385)
    t1, t2 = update_amps(mcc, t1, t2, eris)
    print(abs(t1).sum()-0.0475038989126)
    print(abs(t2).sum()-5.401823846018721)
    print(energy(mcc, t1, t2, eris) - -0.208967840546667)
    t1, t2 = update_amps(mcc, t1, t2, eris)
    print(energy(mcc, t1, t2, eris) - -0.212173678670510)
    print(abs(t1).sum() - 0.05470123093500083)
    print(abs(t2).sum() - 5.5605208391876539)

    mcc.ccsd()
    print(mcc.ecc - -0.2133432312951)
    print(abs(mcc.t2).sum() - 5.63970279799556984)
    print(abs(mcc.t2-mcc.t2.transpose(1,0,3,2)).sum())


# 1523. Count Odd Numbers in an Interval Range
# Easy

# 81

# 8

# Add to List

# Share
# Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).

# Example 1:

# Input: low = 3, high = 7
# Output: 3
# Explanation: The odd numbers between 3 and 7 are [3,5,7].
# Example 2:

# Input: low = 8, high = 10
# Output: 1
# Explanation: The odd numbers between 8 and 10 are [9].

class Solution(object):
    def countOdds(self, low, high):
        """
        :type low: int
        :type high: int
        :rtype: int
        """
        if low%2 == 0 and high%2 == 0:
            ans = ((high - low)/2)
        else:
            ans = ((high - low)/2)+1

        return ans

Aug 20/1523. Count Odd Numbers in an Interval Range.py


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' #suppress tensorflow messages
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR) #suppress tensorflow messages

# Import dependencies
import pandas as pd
import numpy as np
from numpy import array
from IPython.display import display
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar, DateEntry
import datetime
import glob
import time
from keras import backend as K

# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
	x, y = list(), list()
	for i in range(len(sequence)):
		# find the end of this pattern
		end_ix = i + n_steps
		# check if we are beyond the sequence
		if end_ix > len(sequence)-1:
			break
		# gather input and output parts of the pattern
		seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
		x.append(seq_x)
		y.append(seq_y)
	return array(x), array(y)

def predict(cal):
	date = cal.get_date()
	print(date)

	minPurchases = 100

	i = 0
	avgError = 0
	count = 0
	countPredictedPurchase = 0
	countPredictedNoPurchase = 0
	countRealizedPurchases = 0
	countNoRealizedPurchases = 0
	countPredictedPurchaseCorrect = 0
	countPredictedNoPurchaseCorrect = 0
	countLackTrainingData = 0
	countLackEvaluationData = 0

	path = 'purchaseData' + str(minPurchases) + 'OrMore2019'
	dir = os.path.join(os.getcwd(), path)
	resultsPath = 'predictionResults'
	resultsDir = os.path.join(os.getcwd(), resultsPath)
	timestamp = time.time()

	resultsFilename = os.path.join(resultsDir, "predictionResultsForDate" + date.strftime("%Y-%m-%d") + "-" + str(timestamp) + " " + str(minPurchases) + " or more in training set" + ".txt")
	resultsFile = open(resultsFilename, 'w')

	for filename in glob.glob(os.path.join(dir, '*.csv')):
		if "PurchasePeriodsCustNo" in filename:

			startIndex = filename.index("CustNo") + 6
			endIndex = filename.index("ItemNo")
			custNo = filename[startIndex:endIndex]

			startIndex = filename.index("ItemNo") + 6
			endIndex = filename.index(".csv")
			itemNo = filename[startIndex:endIndex]

			i = i + 1
			line = "Iteration " + str(i) + " Customer " + custNo + " and Item " + itemNo
			print(line)
			resultsFile.write(line + "\n")

			# Read data from csv file
			df = pd.read_csv(filename)
			# display(df[0:5])

			allX = df['PurchasePeriod'].to_numpy()
			#print(allX)

			# transform string date to date and then compare it to entered date
			df['InvDateCurr'] = df['InvDateCurr'].map(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d").date())
			#display(df[0:5])

			df = df.loc[df['InvDateCurr'] <= date]
			#display(df[:])

			# Remove unnecessary columns
			df.drop(['CustNo', 'ItemNo', 'InvDateCurr', 'InvDatePrior'], axis=1, inplace=True)
			# display(df[0:5])

			# Convert to numpy array
			trainX = df.to_numpy()
			# print(x)

			# Convert to one dimensional array
			trainX = np.reshape(trainX, (len(trainX)))
			#print(trainX)
			#print(len(trainX))

			# number of time steps
			n_steps = 3

			if (len(trainX) > n_steps) & (len(trainX) >= minPurchases):

				# Split into samples
				x, y = split_sequence(trainX, n_steps)
				# print("Split sequence:")
				# print("x = ", x)
				# print("y = ", y)

				# reshape from [samples, timesteps] into [samples, timesteps, features]
				n_features = 1
				x = x.reshape((x.shape[0], x.shape[1], n_features))

				# define model
				model = Sequential()
				model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
				model.add(Dense(1))
				model.compile(optimizer='adam', loss='mse')
				# fit model
				model.fit(x, y, epochs=200, verbose=0)
				# demonstrate prediction
				x_input = trainX[len(trainX) - 3:]
				line = "x = [" + str(x_input[0]) + ", " + str(x_input[1]) + ", " + str(x_input[2]) + "]"
				print(line)
				resultsFile.write(line + "\n")
				x_input = x_input.reshape((1, n_steps, n_features))
				yhat = model.predict(x_input, verbose=0)
				line = "y = " + str("{:.2f}".format(yhat[0][0]))
				print(line)
				resultsFile.write(line + "\n")

				if yhat <= 7:
					line = "Purchase in the next 7 days is expected."
					print(line)
					resultsFile.write(line + "\n")
				else:
					line = "Purchase in the next 7 days is NOT expected."
					print(line)
					resultsFile.write(line + "\n")

				K.clear_session()

				if len(allX) > len(trainX):
					actual = allX[len(trainX)]
					line = "actualY = " + str(actual)
					print(line)
					resultsFile.write(line + "\n")
					'''
					error = abs(yhat- actual)/actual * 100
					avgError = avgError + error
					count = count + 1
					line = "Prediction error is " + str(error) + " percent"
					print(line)
					resultsFile.write(line + "\n")
					'''
					if yhat <= 7 and actual <= 7:
						line = "Prediction is correct."
						print(line)
						resultsFile.write(line + "\n")
						countPredictedPurchaseCorrect = countPredictedPurchaseCorrect + 1
						countRealizedPurchases = countRealizedPurchases + 1
						countPredictedPurchase = countPredictedPurchase + 1
					elif yhat > 7 and actual > 7:
						line = "Prediction is correct."
						print(line)
						resultsFile.write(line + "\n")
						countPredictedNoPurchaseCorrect = countPredictedNoPurchaseCorrect + 1
						countNoRealizedPurchases = countNoRealizedPurchases + 1
						countPredictedNoPurchase = countPredictedNoPurchase + 1
					else:
						line = "Prediction is NOT correct."
						print(line)
						resultsFile.write(line + "\n")
						if (actual <= 7):
							countRealizedPurchases = countRealizedPurchases + 1
							countPredictedNoPurchase = countPredictedNoPurchase + 1
						else:
							countNoRealizedPurchases = countNoRealizedPurchases + 1
							countPredictedPurchase = countPredictedPurchase + 1
				else:
					line = "Prediction evaluation not possible due to lack of data."
					print(line)
					resultsFile.write(line + "\n")
					countLackEvaluationData = countLackEvaluationData + 1
			else:
				line = "Not enough data for training"
				print(line)
				resultsFile.write(line + "\n")
				countLackTrainingData = countLackTrainingData + 1
	'''
	avgError = avgError / count
	line = "Average error is " + str(avgError)
	'''

	if (countPredictedPurchase > 0) :
		line = "Purchases prediction precision: " + str(countPredictedPurchaseCorrect) + " out of " + str(countPredictedPurchase) \
			   + " purchase predictions = " + str("{:.2f}".format(countPredictedPurchaseCorrect/countPredictedPurchase * 100)) + "%"
	else:
		line = "No predicted purchases"
	print(line)
	resultsFile.write(line + "\n")

	if (countRealizedPurchases > 0) :
		line = "Purchases prediction coverage: " + str(countPredictedPurchaseCorrect) + " out of " + str(countRealizedPurchases) \
			   + " = " + str("{:.2f}".format(countPredictedPurchaseCorrect / countRealizedPurchases * 100)) + "%"
	else:
		line = "No realized purchases"
	print(line)
	resultsFile.write(line + "\n")

	line = "Predicted purchases: " + str(countPredictedPurchase) + "\nPredicted no purchase: " + str(countPredictedNoPurchase) \
		   + "\nRealized purchases: " + str(countRealizedPurchases) + "\nNo realized purchases: " + str(countNoRealizedPurchases) + \
			"\nCorrectly predicted purchases: " + str(countPredictedPurchaseCorrect) + "\nCorrectly predicted no purchase: " + str(countPredictedNoPurchaseCorrect) \
		   + "\nIterations with no evaluation data: " + str(countLackEvaluationData) + "\nIterations with no training data: " + str(countLackTrainingData)
	print(line)
	resultsFile.write(line + "\n")
	resultsFile.close()

master = tk.Tk()
ttk.Label(master, text='Choose date').pack(padx=10, pady=10)
cal = DateEntry(master, width=12, background='grey', foreground='white', borderwidth=2, year=2017, showweeknumbers=False, date_pattern='dd.mm.yyyy')
cal.pack(padx=10, pady=10)
ttk.Button(master, text='Predict', command=lambda: predict(cal)).pack(padx=10, pady=10)

master.mainloop()

'''
master = tk.Tk()
tk.Label(master, text="First Name").grid(row=0)
tk.Label(master, text="Last Name").grid(row=1)

e1 = tk.Entry(master)
e2 = tk.Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

master.mainloop()
'''


"""
Test nimare.meta.cbma.ale (ALE/SCALE meta-analytic algorithms).
"""
import pytest

import nimare
from nimare.meta.cbma import ale
from nimare.correct import FWECorrector

def test_ale():
    """
    Smoke test for ALE
    """
    meta = ale.ALE()
    res = meta.fit(pytest.cbma_testdata1)
    assert isinstance(res, nimare.base.MetaResult)
    corr = FWECorrector(method='bonferroni')
    cres = corr.transform(res)
    assert isinstance(cres, nimare.base.MetaResult)
    corr = FWECorrector(method='permutation', voxel_thresh=0.001,
                        n_iters=5, n_cores=1)
    cres = corr.transform(meta.results)
    assert isinstance(cres, nimare.base.MetaResult)

def test_ale_subtraction():
    """
    Smoke test for ALE
    """
    meta1 = ale.ALE()
    res1 = meta1.fit(pytest.cbma_testdata1)

    meta2 = ale.ALE()
    res2 = meta2.fit(pytest.cbma_testdata1)

    corr = FWECorrector(method='permutation', voxel_thresh=0.001,
                        n_iters=5, n_cores=1)
    cres1 = corr.transform(res1)
    cres2 = corr.transform(res2)

    sub_meta = ale.ALESubtraction()
    sub_meta.fit(
        meta1, meta2,
        image1=cres1.get_map('logp_level-cluster_corr-FWE_method-permutation', return_type='image'),
        image2=cres2.get_map('logp_level-cluster_corr-FWE_method-permutation', return_type='image'))
    assert isinstance(sub_meta.results, nimare.base.MetaResult)


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

#importing data from c++ code
data = pd.read_csv('rk6.txt', names=['x', 'y_rk6', 'y_exact', 'diff_rk6'])

#Defining Points
numPoints=np.array([10, 100, 1000, 10000])
x0=0.0; xf=5.
h = (xf-x0)/numPoints

#Defining mean difference with c++ data
diff_all = np.array([np.mean(np.array(data.iloc[0:10,3])), np.mean(np.array(data.iloc[11:110,3])),
	np.mean(np.array(data.iloc[111:1110,3])), np.mean(np.array(data.iloc[1111:11110,3]))])

#ploting data
fig = plt.figure(figsize=(8,4))
ax0 = fig.add_subplot(131); ax1 = fig.add_subplot(132)
ax2 = fig.add_subplot(133)

#first subplot
ax0.plot(data.iloc[111:1110,0], data.iloc[111:1110,1], 'm-', label='rk6 sol')
ax0.plot(data.iloc[111:1110,0], data.iloc[111:1110,2], 'c--', label='exact sol')
ax0.set_xlabel('x'); ax0.set_ylabel('y')
ax0.legend()

#second subplot
ax1.semilogy(data.iloc[111:1110,0], data.iloc[111:1110,3], 'm-', label='rk6 diff')
ax1.set_xlabel('x'); ax1.set_ylabel(r'log|$y_{exact}$ - $y_{num}$|')
ax1.legend()

#third subplot
ax2.plot(h, diff_all, 'm-')
ax2.set_xlabel('h'); ax2.set_ylabel('mean difference')

plt.tight_layout()
plt.show()


"""add comment

Revision ID: d088fb3bb7bd
Revises: dc67a31159b7
Create Date: 2016-08-02 15:25:02.523107

"""

# revision identifiers, used by Alembic.
revision = 'd088fb3bb7bd'
down_revision = 'dc67a31159b7'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('comments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('content', sa.Text(), nullable=True),
    sa.Column('content_html', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('disabled', sa.Boolean(), nullable=True),
    sa.Column('author_id', sa.Integer(), nullable=True),
    sa.Column('blog_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['author_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['blog_id'], ['blogs.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_comments_created_at'), 'comments', ['created_at'], unique=False)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_comments_created_at'), table_name='comments')
    op.drop_table('comments')
    ### end Alembic commands ###


from __future__ import print_function
from __future__ import division
import ee
import ee.mapclient
import geetools

def reference_code():
    ## Initialize connection to server
    ee.Initialize()
    ## Define your image collection
    collection = ee.ImageCollection('COPERNICUS/S2') # LANDSAT/LC8_L1T_TOA
    ## Define time range
    collection_time = collection.filterDate('2017-04-11', '2018-01-01') #YYYY-MM-DD
    ## Select location based on location of tile
    path = collection_time.filter(ee.Filter.eq('WRS_PATH', 198))
    pathrow = path.filter(ee.Filter.eq('WRS_ROW', 24))
    # or via geographical location:
    #point_geom = ee.Geometry.Point(5, 52) #longitude, latitude
    #pathrow = collection_time.filterBounds(point_geom)
    ## Select imagery with less then 5% of image covered by clouds
    clouds = pathrow.filter(ee.Filter.lt('CLOUD_COVER', 5))
    ## Select bands
    bands = clouds.select(['B4', 'B3', 'B2'])
    ## Make 8 bit data
    def convertBit(image):
        return image.multiply(512).uint8()
    ## Convert bands to output video
    outputVideo = bands.map(convertBit)
    print("Starting to create a video")
    ## Export video to Google Drive
    out = ee.batch.Export.video.toDrive(outputVideo,
                                        description='Netherlands_video_region_L8_time',
                                        dimensions=720,
                                        framesPerSecond=2,
                                        region=([5.588144,51.993435],
                                                [5.727906, 51.993435],
                                                [5.727906, 51.944356],
                                                [5.588144, 51.944356]),
                                        maxFrames=10000)
    ## Process the image
    process = ee.batch.Task.start(out)
    print("Process sent to cloud")

def downloader():
    ee.Initialize() ################################ this must be done before anything
    ee.mapclient.centerMap(-110, 40, 5)
    germany = ee.Geometry.Polygon([[
        [-109.05, 37.0], [-102.05, 37.0], [-102.05, 41.0],  # colorado
        [-109.05, 41.0], [-111.05, 41.0], [-111.05, 42.0],  # utah
        [-114.05, 42.0], [-114.05, 37.0], [-109.05, 37.0]]])

    def cropout_aoi(image):
        return image.clip(germany)

    def scale(image):
        return image.divide(4096).multiply(255).uint8()

    count = 1
    month_interval = 5
    month_end = 11 # download one image for 6-month interval
    year_interval = 1
    year_end = 2019
    for year in range(2016, year_end, year_interval):
        for month in range(1, month_end, month_interval):
            next_month = month + month_interval
            print('currently on ({})/({}) -> ({})/({})'.format(month, year, next_month, year))
            # Now define the dates we want to get data on
            date_from = ee.Date(date='{}-{}-01'.format(year, month))
            date_to = ee.Date(date='{}-{}-01'.format(year, next_month))
            # import sentinel images, clip your area of interest, dates and cloud cover as needed
            image_collection = (ee.ImageCollection('COPERNICUS/S2') #('LANDSAT/LE07/C01/T1_RT')
                .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
                .filterDate(date_from, date_to)
                .filterBounds(germany) # locates ones close to this polygon
                .map(cropout_aoi)) # actually does the clipping part
                #.map(maskS2clouds))
            all_bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12']
            total_count = image_collection.size()
            # print('log: total images in collection =', total_count)

            this_image = image_collection.median()
            # ee.mapclient.addtoMap(this_image.select('B4', 'B3', 'B2'), {'gain': [1.4, 1.4, 1.1]})

            image = this_image.select('B3', 'B2', 'B1')
            ee.mapclient.addToMap(image, {'gain': [1.4, 1.4, 1.1]})

            # ee.batch.Export.image.toDrive(image=this_image.select('B2', 'B3', 'B4', 'B5', 'B8'),
            #                               description='German_S2_median_{}'.format(count),
            #                               folder='germany_s2',
            #                               fileNamePrefix='g_median_{}'.format(count),
            #                               region= germany,
            #                               scale=10)
            # image1 = ee.Image('CGIAR/SRTM90_V4')
            path = image.getDownloadUrl({
                'scale': 10,
                # 'crs': 'EPSG:4326',
                'region': '[[-109.05, 40.0],'
                          '[-108.05, 40.0],'
                          '[-108.05, 41.0],'
                          '[-109.05, 41.0]]'
            })
            print(path)
            count += 1

    pass

downloader()


import time
from scipy.spatial.transform import Rotation as R
import scipy
import numpy as np
import matplotlib.pyplot as plt
from utils import NOISE_FILENAME

class FTSensor:
    def __init__(self, robot):
        self.frequency = 1/500
        self.robot = robot
        self.noiseMean = 0
        self.noiseSTD = 1
        self.acceleration = []
        self.filename = NOISE_FILENAME

    def convertForceInBase2TCP(self,ftInBase):
        tcpPose = self.robot.getActualTCPPose()
        rot = R.from_rotvec(tcpPose[3:6])
        rMatrix = scipy.linalg.inv(rot.as_matrix())
        return rMatrix @ ftInBase[0:3]

    def saveNoiseValues(self):
        print("Saving",self.filename)
        with open(self.filename, 'w+') as file:
            string = str(self.noiseMean) + " " + str(self.noiseSTD) + " " + str(self.acceleration)[1:-1]
            file.write(string)

    def noiseFT(self, duration):
        print("Recording noise F/T sensor!")

        durTime = time.time()
        self.robot.zeroFtSensor()
        accelerations = []
        noiseMagnitudeOfForce = []
        while time.time() - durTime < duration:
            startTime = time.time()

            ftInBase = self.robot.getActualTCPForce()
            ftInTCP = self.convertForceInBase2TCP(ftInBase)

            noiseMagnitudeOfForce.append(np.linalg.norm(ftInTCP))

            accelerations.append(self.robot.getActualToolAccelerometer())

            diff = time.time() - startTime
            if(diff < self.frequency):
                time.sleep(self.frequency - diff)

        self.noiseMean = np.asarray(noiseMagnitudeOfForce).mean()
        self.noiseSTD = np.asarray(noiseMagnitudeOfForce).std()
        self.acceleration = np.mean(np.asarray(accelerations),axis=0)
        self.saveNoiseValues()
        print("Noise mean:", self.noiseMean, "Noise std:", self.noiseSTD,"Acceleration:",self.acceleration)


#!/usr/bin/env python

from optparse import OptionParser
from subprocess import check_output
import sys
import time
from textwrap import dedent

def _git_config(config_name, git_exe):
    return check_output([git_exe, "config", "--get", config_name]).strip()

def _git_user_name(git_exe):
    return _git_config("user.name", git_exe)

def _git_user_email(git_exe):
    return _git_config("user.email", git_exe)

def _iso_time():
    return time.strftime("%Y-%m-%dT%H:%M:%S%z")

def _git_log(revision, git_exe):
    """
    Return a list of log lines.
    """
    return check_output([git_exe, "log", "--pretty=format:%B%n", revision]).split('\n')

def _write_diff(output_f, revision, git_exe):
    output_f.write(check_output([git_exe, "diff", "--unified=20", revision]))

def _write_diffscuss_header(output_f, author, email, git_exe):
    if author is None:
        author = _git_user_name(git_exe)
    if email is None:
        email = _git_user_email(git_exe)

    iso_time = _iso_time()

    header_lines = ['',
                    "author: %s" % author,
                    "email: %s" % email,
                    "date: %s" % iso_time,
                    '']
    header_lines = ['%%* %s' % s for s in header_lines]

    output_f.write('\n'.join(header_lines))
    output_f.write('\n')

def _write_diffscuss_body(output_f, revision, git_exe):
    log_lines = ['%%- %s' % s for s in _git_log(revision, git_exe)]
    output_f.write('\n'.join(log_lines))
    output_f.write('\n')

def main(revision, output_f, author=None, email=None, git_exe=None):
    if not git_exe:
        git_exe = 'git'
    _write_diffscuss_header(output_f, author, email, git_exe)
    _write_diffscuss_body(output_f, revision, git_exe)
    _write_diff(output_f, revision, git_exe)

if __name__ == '__main__':
    parser = OptionParser(usage=dedent("""\
                %prog [options] git_revision_range

                git_revision_range will be passed as-is to git diff and git log.
                For example, you could use HEAD~3..HEAD or some_branch..master.
                Note that you probably don't want just a branch name, as git log
                will start the log from that point and run backwards to the initial
                commit in the repo."""))
    parser.add_option("-a", "--author",
                      help="The author for the diffscuss review (defaults to git user.name)",
                      dest="author")
    parser.add_option("-e", "--email",
                      help="The email address for the diffscuss review (defaults to git user.email)",
                      dest="email")
    parser.add_option("-g", "--git-exe",
                      help="The path to the git executable (defaults to 'git')",
                      dest="git_exe")
    parser.add_option("-o", "--output",
                      help="File in which to put the output (defaults to stdout).",
                      dest="output")

    opts, args = parser.parse_args()

    if opts.output is None:
        out_f = sys.stdout
    else:
        out_f = open(opts.output, 'wb')

    main(args[0], out_f, git_exe=opts.git_exe, author=opts.author, email=opts.email)

    if opts.output is not None:
        out_f.close()


import os
import logging
import subprocess

this_code = 'emerald_dwi_analysis.py'

def find_masks(dir_to_search, search_string_list):
    #Return file paths/names in dir_to_search that contain all
    #the strings in search_string_list

    this_function = 'find_masks()'
    files_to_return = []

    #Make sure directory is there
    if not os.path.exists(dir_to_search):
      logging.error('Proposed mask directory not found: {}'.format(dir_to_search))
      logging.error('Error from: {} -- {}'.format(this_code, this_function))
      return 0

    #List files in the directory
    contents = os.listdir(dir_to_search)

    for file_name in contents:
      count = 0
      for element in search_string_list:
        if element in file_name:
          count = count + 1
      if count == len(search_string_list):
        files_to_return.append(os.path.join(dir_to_search, file_name))

    return files_to_return

def transform_roi(in_image, add_suffix, ref, transform):
    #Put an input image through antsApplyTransforms
    this_function = 'transform_roi()'
    #Create output file name
    input_path, input_name = os.path.split(in_image)
    if '.nii' not in input_name:
      logging.error('Input image expected to be a .nii file!')
      logging.error('input file name: {}'.format(input_name))
      raise RuntimeError('Input image not a NIFTI file! -- {}, {}'.format(this_code,this_function))
    if input_name[-7:] == '.nii.gz':
      input_end = '.nii.gz'
    elif input_name[-4:] == '.nii':
      input_end = '.nii'
    else:
      logging.error('Input image name does not end in .nii or .nii.gz???')
      raise RuntimeError('Input image file name should end in .nii or .nii.gz! -- {}, {}'.format(this_code,this_function))

    input_prefix = input_name.split(input_end)[0]
    output_name = '{}_{}{}'.format(input_prefix, add_suffix, input_end)
    out_image = os.path.join(input_path, output_name)

    call_parts = [
                  'antsApplyTransforms',
                  '--float',
                  '--default-value', '0',
                  '--input', in_image,
                  '--interpolation', 'NearestNeighbor',
                  '--output', out_image,
                  '--reference-image', ref,
                  '--transform', transform
                  ]

    logging.info('Applying transform to image...')
    logging.info('Input Image: {}'.format(in_image))
    logging.info('Transform: {}'.format(transform))

    error_flag = subprocess.call(call_parts)
    if error_flag:
        logging.error('Process failed: {}'.format(call_parts))
        return None
    else:
        return out_image

def generate_tracks(in_dwi, in_fod, out_tck, mask, include_list, bval, bvec, track_num):
    #Generate tracktography
    call_beginning = [
                      'tckgen',
                      '-seed_image', in_dwi, in_fod, out_tck,
                      '-mask', mask
                      ]

    for element in include_list:
        call_beginning.append('-include')
        call_beginning.append(element)

    call_ending = [
                   '-select', track_num,
                   '-maxlength', '300',
                   '-fslgrad', bvec, bval,
                   '-seeds', '0',
                   '-force'
                  ]

    call_parts = call_beginning + call_ending

    logging.info('Generating tracks...')
    logging.info('in_dwi: {}'.format(in_dwi))
    logging.info('in_fod: {}'.format(in_fod))
    logging.info('out_tck: {}'.format(out_tck))
    logging.info('mask: {}'.format(mask))
    logging.info('include: {}'.format(include_list))
    logging.info('bval: {}'.format(bval))
    logging.info('bvec: {}'.format(bvec))
    logging.info('track_num: {}'.format(track_num))

    error_flag = subprocess.call(call_parts)
    if error_flag:
        logging.error('Process failed: {}'.format(call_parts))
        return None
    else:
        return out_tck

def generate_wholebrain_tracks(in_dwi, in_fod, out_tck, bval, bvec, track_num):
    #Generate whole-brain tractography
    call_parts = [
                  'tckgen',
                  '-seed_image', in_dwi, in_fod, out_tck,
                  '-select', track_num,
                  '-maxlength', '250',
                  '-fslgrad', bvec, bval,
                  '-force'
                  ]

    logging.info('Generating whole-brain tracks...')
    logging.info('in_dwi: {}'.format(in_dwi))
    logging.info('in_fod: {}'.format(in_fod))
    logging.info('out_tck: {}'.format(out_tck))
    logging.info('bval: {}'.format(bval))
    logging.info('bvec: {}'.format(bvec))
    logging.info('track_num: {}'.format(track_num))

    error_flag = subprocess.call(call_parts)
    if error_flag:
      logging.error('Process failed: {}'.format(call_parts))
      return None
    else:
      return 1

def sift_tracks(in_tck, in_fod, out_sift, sift_num):
    #Sift tracks
    call_parts = [
                  'tcksift',
                  in_tck,
                  in_fod,
                  out_sift,
                  '-term_number', sift_num,
                  '-force'
                  ]

    logging.info('Sifting tracks...')

    error_flag = subprocess.call(call_parts)
    if error_flag:
        logging.error('Process failed: {}'.format(call_parts))
        return None
    else:
        return out_sift

def sample_FA(in_tck, in_fa, out_fa):
    #Sample FA
    call_parts = [
                  'tcksample',
                  in_tck,
                  in_fa,
                  out_fa,
                  '-stat_tck', 'mean',
                  '-force'
                  ]

    logging.info('Sampling FA...')

    error_flag = subprocess.call(call_parts)
    if error_flag:
        logging.error('Process failed: {}'.format(call_parts))
        return None
    else:
        return out_fa

def align_centers(base_image, moving_image):

    current_dir = os.getcwd()

    #Create output image file name
    if moving_image[-7:] == '.nii.gz':
      input_prefix = moving_image[:-7]
      input_extension = '.nii.gz'
    elif moving_image[-4:] == '.nii':
      input_prefix = moving_image[:-4]
      input_extension = '.nii'
    else:
      logging.error('Input moving image should be .nii or .nii.gz!')
      logging.error('moving_image: {}'.format(moving_image))
      logging.error('emerald_dwi_analysis.py -- align_centers()')
      return None

    out_image = '{}_shft{}'.format(input_prefix, input_extension)

    target_dir = os.path.split(base_image)[0]
    os.chdir(target_dir)

    #Align the centers of two images
    call_parts = [
                    '@Align_Centers',
                    '-base', base_image,
                    '-dset', moving_image
                    ]
    logging.info('Aligning centers...')

    error_flag = subprocess.call(call_parts)
    os.chdir(current_dir)
    if error_flag:
        logging.error('Process failed: {}'.format(call_parts))
        return None
    else:
        return out_image


import socket
s=socket.socket()
host="localhost"
port=4001
s.bind((host,port))
print("server is listening on :",port)
s.listen(1)
c,addr=s.accept()
print('connection eshtablished with client from address:',str(addr))
s.send(b'hey client what are you excepting from me')
s.send('i can help you with anything :'.encode)
s.close()


from model.address import Address
from model.group import Group
import random
from fixture.orm import ORMFixture
import allure
import pytest

@allure.story("Добавление группу в адрес")
def test_add_group_in_address(app, db_orm):
    # db_orm = ORMFixture(host="127.0.0.1", name="addressbook", user="root", password="")
    groups = db_orm.get_group_list()
    # а есть вообще группы?
    if len(groups) == 0:
        app.group.create(Group(name="test_grup", header="test", footer="test_comment"))
        # создадим новую группу
    contacts = db_orm.get_contact_list()
    # в контакты есть?
    if len(contacts) == 0:
        app.address.create(Address(first_name="First_name"))
    # возьмем случайную группу и контакт для добавления
    group_to_add = random.choice(groups)
    contact_to_add = random.choice(contacts)
    # переносим контакт в группу
    app.address.move_contact_to_group(contact_to_add.id, group_to_add.id)
    # берем список контактов в нашей группе
    l = db_orm.contacts_in_group(Group(index=group_to_add.id))
    # теперь проверяем, что наш id в этом списке. Ели так, то все норм
    assert contact_to_add.id in app.address.return_id_contacts(l), "Error, check table, contact not add to group"

@allure.story("Удаление группы из адреса")
@pytest.mark.xfail
def test_delete_group_from_address(app, db_orm):
    contacts = db_orm.get_contact_list()
    # в контакты есть?
    if len(contacts) == 0:
        app.address.create(Address(first_name="First_name"))
    groups = db_orm.get_group_list()
    # а есть вообще группы?
    if len(groups) == 0:
        app.group.create(Group(name="test_grup", header="test", footer="test_comment"))
        # создадим новую группу
    groups = db_orm.get_group_list()
    # а есть контакты в группах?
    id_gr = None
    for row in groups:
        if len(db_orm.contacts_in_group(Group(index=row.id))) > 0:
            id_gr = row.id
            break
    # нет? создвем
    if id_gr ==None:
        group_to_add = random.choice(groups)
        contact_to_add = random.choice(contacts)
        app.address.move_contact_to_group(contact_to_add.id, group_to_add.id)
        id_gr = group_to_add.id
    # удаляем первый элемент привязанный к группе
    app.address.return_to_home_page()
    id_adress = app.address.delete_address_from_group(id_gr)
    # список контактов, которые не в этой группе
    l = db_orm.get_contact_not_in_group(Group(index=id_gr))
    # проверяем
    assert id_adress in app.address.return_id_contacts(l), "Error, check table"


import sys, time, telepot, unicodedata, urllib3, wikiquote, random
from telepot.loop import MessageLoop
from urllib.request import urlopen, Request #as uReq
from bs4 import BeautifulSoup as soup
#BeautifulSoup + Wikiquote part
def scrap():
    url = 'URL/'
    req = urlopen(url)
    page = req.read()
    req.close()
    page_soup = soup(page, "html.parser")
    containers = page_soup.find("span", {"id":"ajaxtemp"}).text.strip()
    norm_containers = unicodedata.normalize('NFKD', containers).encode('ascii', 'ignore')+ b"\n"
    the_quote = random.choice(wikiquote.quotes('Helen Keller')).encode('UTF-8')
    output = norm_containers +  the_quote
    return output
#Telepot part
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(content_type, chat_type, chat_id)

    if content_type == 'text':
        bot.sendMessage(chat_id, scrap())

TOKEN = 'WHATEVER BOT FATHER SAYS'

bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')

# Keep the program running.
while 1:
    time.sleep(1)


election = {} #The election dictionary will hold all the votes.
vote = True   #The vote variable will stop the loop when all
			  #votes are in.

#The Vote loop will take in votes.
while vote:
	#We ask the user if they want to vote.
	print("Would you like to vote?")
	choice = input()
	if (choice == 'yes') | (choice == "Yes") | (choice == "y"):
		#This adds votes to the total count and/or
		#adds candidates to the election dictionary.
		print("Who would you like to vote for?")
		answer = input()
		if answer in election:
			election[answer] += 1
		else:
			election[answer] = 1
	else:#This allows the user to stop voting when they are done.
		vote = False

print(election) #This prints out all the votes at the end!
winner = (max(election, key=election.get, default="No one"))
sorter = (sorted(election, key=election.get, reverse=True))
print(winner, "wins!")
print(sorter)


from flask import Blueprint, send_file
import os

NAME = 'images'
page = Blueprint(NAME, __name__)

def initialize_app(app):
    app.register_blueprint(page, url_prefix="/")

@page.route("/")
def index():
    return send_file("static/index.html")

@page.route("/images/<image>")
def images(image):
    try:
        respond = send_file(os.path.join(os.path.abspath('.'), 'app/static/images/' + image))
    except FileNotFoundError:
        respond = send_file(os.path.join(os.path.abspath("."), "app/static/index.html"))
    return respond

@page.after_app_request
def after_request(response):
    if 'Access-Control-Allow-Origin' not in response.headers:
        response.headers.add('Access-Control-Allow-Origin', '*')
    return response


#!/usr/bin/env python

import pyttsx
import rospy
import time
import threading
from std_msgs.msg import String

class wm_tts():
    def __init__(self,node_name):
        # get parameters
        rospy.init_node(node_name)
        self.engine = pyttsx.init()
        self.engine_thread = threading.Thread(target=self.loop)
        self.engine_thread.start()
        rate = rospy.get_param('rate', 150)
        self.engine.setProperty('rate', rate)
        self.engine.setProperty('volume', 0.5)

        self.sub = rospy.Subscriber('SaraVoice', String, self.callback)

    def loop(self):
        self.engine.startLoop(False)
        while not rospy.is_shutdown():
            self.engine.iterate()
            time.sleep(0.1)
        self.engine.endLoop()

    def callback(self, msg):

        rospy.loginfo(msg.data)
        self.engine.say(msg.data)
        while self.engine.isBusy():
            time.sleep(0.1)

if __name__ == '__main__':

    try:

        wm_tts('wm_tts_node')

        rospy.spin()

    except rospy.ROSInterruptException:
        pass


"""
encoding=utf-8
_author = youzipi
date = 17/1/20
"""
import click

@click.command()
@click.option('--name', default='123')
@click.option('--debug', '-d', is_flag=True)
@click.option('--help', '-h')
def func(name, debug):
    click.echo('hello,' + name)
    click.echo(debug)

if __name__ == '__main__':
    func()


import pye57
import sys
sys.path.append('/home/sadams/green/greenUtilities')

import numpy as np

e57 = pye57.E57("/home/sadams/sites/MezSnake/MezSnake.e57")

# the ScanHeader object wraps most of the scan information:
header = e57.get_header()

# all the header information can be printed using:
for line in header.pretty_print():
    print(line)

data = e57.read_scan(0, colors = True, ignore_missing_fields = True)
print(data.keys())
x = data['cartesianX']
y = data['cartesianY']
z = data['cartesianZ']

red = data['colorRed']
green = data['colorGreen']
blue = data['colorBlue']

points = np.hstack((
	np.expand_dims(x, axis = 1),
	np.expand_dims(y, axis = 1),
	np.expand_dims(z, axis = 1), ))

colors = np.hstack((
	np.expand_dims(red, axis = 1),
	np.expand_dims(green, axis = 1),
	np.expand_dims(blue, axis = 1), ))

print(points.shape)


# metacritic.com scraper

import re
from urllib2 import HTTPError

from util import hook, http

@hook.command('mc')
def metacritic(inp):
    '.mc [all|movie|tv|album|x360|ps3|pc|gba|ds|3ds|wii|vita|wiiu|xone|ps4] <title> -- gets rating for'\
    ' <title> from metacritic on the specified medium'

    # if the results suck, it's metacritic's fault

    args = inp.strip()

    game_platforms = ('x360', 'ps3', 'pc', 'gba', 'ds', '3ds', 'wii', 'vita', 'wiiu', 'xone', 'ps4')
    all_platforms = game_platforms + ('all', 'movie', 'tv', 'album')

    try:
        plat, title = args.split(' ', 1)
        if plat not in all_platforms:
            # raise the ValueError so that the except block catches it
            # in this case, or in the case of the .split above raising the
            # ValueError, we want the same thing to happen
            raise ValueError
    except ValueError:
        plat = 'all'
        title = args

    cat = 'game' if plat in game_platforms else plat

    title_safe = http.quote_plus(title)

    url = 'http://www.metacritic.com/search/%s/%s/results' % (cat, title_safe)

    try:
        doc = http.get_html(url)
    except HTTPError:
        return 'error fetching results'

    ''' result format:
    -- game result, with score
    -- subsequent results are the same structure, without first_result class

            Game
            WII

...

                        Metascore:
                        87

...

    -- other platforms are the same basic layout
    -- if it doesn't have a score, there is no div.basic_score
    -- the
 changes content for non-games:

Movie

    '''

    # get the proper result element we want to pull data from

    result = None

    if not doc.find_class('query_results'):
        return 'no results found'

    # if they specified an invalid search term, the input box will be empty
    if doc.get_element_by_id('search_term').value == '':
        return 'invalid search term'

    if plat not in game_platforms:
        # for [all] results, or non-game platforms, get the first result
        result = doc.find_class('result first_result')[0]

        # find the platform, if it exists
        result_type = result.find_class('result_type')
        if result_type:

            # if the result_type div has a platform div, get that one
            platform_div = result_type[0].find_class('platform')
            if platform_div:
                plat = platform_div[0].text_content().strip()
            else:
                # otherwise, use the result_type text_content
                plat = result_type[0].text_content().strip()

    else:
        # for games, we want to pull the first result with the correct
        # platform
        results = doc.find_class('result')
        for res in results:
            result_plat = res.find_class('platform')[0].text_content().strip()
            if result_plat == plat.upper():
                result = res
                break

    if not result:
        return 'no results found'

    # get the name, release date, and score from the result
    product_title = result.find_class('product_title')[0]
    name = product_title.text_content()
    link = 'http://metacritic.com' + product_title.find('a').attrib['href']

    try:
        release = result.find_class('release_date')[0].\
            find_class('data')[0].text_content()

        # strip extra spaces out of the release date
        release = re.sub(r'\s{2,}', ' ', release)
    except IndexError:
        release = None

    try:
        score = result.find_class('metascore_w')[0].text_content()
    except IndexError:
        score = None

    return '[%s] %s - %s, %s -- %s' % (plat.upper(), name,
                                       score or 'no score',
                                       'release: %s' % release if release else 'unreleased',
                                       link)


from splinter import Browser
from bs4 import BeautifulSoup as bs
import time

def init_browser():
    # ---------------------------------------------------------------------
    # @NOTE: Replace the path with your actual path to the chromedriver
    # executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
    #                                            Piruz Alemi Jan 27, 2020
    # ---------------------------------------------------------------------
    executable_path = {"executable_path": "./bin/chromedriver"}
    return Browser("chrome", **executable_path, headless=False)

def scrape_info():
    browser = init_browser()

    # ------------------------------------------------------------------
    #        Use Splinter to initiate a "visit" to the browser!
    #        And freeze it for 2 seconds!
    #        Use html parser for Soup of this visit
    # -----------------------------------------------------------------
    # Visit visitcostarica.herokuapp.com
    # Visit: https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars
    # url = "https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars"
    url = "https://www.jpl.nasa.gov"
    browser.visit(url)

    time.sleep(2)

    # Scrape page into Soup
    html = browser.html
    soup = bs(html, "html.parser")
    print(soup)
    # ------------------------------------------------------------------------
    # Get the the videos with mp4 type
    # avg_temps = soup.find('div', id='weather')
    # <source src='/images/exoplanet/20200124/PIA21472.m4v' type='video/mp4'>
    # The code for video was extracted after "inspecting" the website
    # ------------------------------------------------------------------------
    # video = soup.find('source', type='video/mp4')
    paragraph = soup.find('<p>')
    # Get the min avg temp
    # min_temp = avg_temps.find_all('strong')[0].text
    title = soup.title.string

    title_content=soup.title.contents[0]
    paragraph_content=soup.p.contents[0]

    # ------------------------------------------------------
    #      How to load a list of href  into the dictionary?
    #      Gets all href
    # -----------------------------------------------------
    for link in soup.find_all('a'):
        links=link.get('href')
    # ------------------------------------------------------
    #       Gets all the texts
    # ------------------------------------------------------
    text=soup.get_text()
    # Get the max avg temp
    # max_temp = avg_temps.find_all('strong')[1].text

    # BONUS! : Find the src for nasa images
    relative_image_path = soup.find_all('img')[2]["src"]
    # nasa_img = url + relative_image_path
    nasa_img = relative_image_path

    # Store data in a nasa dictionary
    nasa_data = {
        "nasa_img": nasa_img,
        "Title": title_content,
        "Text": text,
        "Links": links,
        "paragraph": paragraph_content

    }

    # Close the browser after scraping
    browser.quit()

    # Return results
    return nasa_data


#!/usr/bin/python
import sys
from StyleRubric import StyleRubric

def main(files):

    rubric = StyleRubric()
    show_errors = []

    for filename in files:
        rubric.grade_student_file(filename, filename)

    rubric.adjust_errors()
    rubric.print_errors()

    sys.exit(rubric.total_errors)

if __name__ == "__main__":
    main(sys.argv[1:])


# Example:  Computes a median value from many samples
"""
A Sat/XLink setup is associated with this module:
`median_setup.txt <median_setup.txt>`_

The setup requires that one measurement be setup to collect samples from the sensor (e.g. every 5 seconds).  It gets connected to the one_sample script function.  It does not get logged.  Instead, the samples get stored in temporary memory.
A second measurement is setup to compute the median.  It is a manual entry measurement (it does not sample the sensor).  It should happen less frequently than the first measurement (e.g. every hour).  It is logged and may be transmitted.
"""

from sl3 import *
import utime

sample_list = []  # samples are stored here

def compute_median(numbers_list):
    """ computes median on numbers in  list"""
    numbers_list = sorted(numbers_list)
    n = len(numbers_list)
    i = n // 2
    if n % 2 == 1:
        return numbers_list[i]
    else:
        return sum(numbers_list[i-1:i+1])/2

@MEASUREMENT
def one_sample(value):
    """ adds a sample to the list for later computation """
    global sample_list

    """ wait a touch to let the median computation complete first
    otherwise, if a new sample is collected at the same time
    as the median is computed, the new sample would end up in the
    computation for the old median """
    utime.sleep(0.1)

    lock()  # protect data

    sample_list.append(value)  # add value to list

    unlock()  # MUST unlock after locking.  otherwise, no other script could ever run

    if sutron_link:
        print("sample: ", value, ", ", ascii_time(utime.localtime()))
    meas_do_not_log()
    return value

@MEASUREMENT
def median_meas(ignored):
    """ computes the median over the collected samples """

    global sample_list

    lock()  # protect data

    median_value = compute_median(sample_list)  # compute the median

    if sutron_link:
        print("sample count: ", len(sample_list), " median: ", median_value, ", ", ascii_time(utime.localtime()))

    sample_list.clear()  # clear the list

    unlock()  # MUST unlock after locking.  otherwise, no other script could ever run

    return median_value

def test_median():
    """ test that the median is computed correctly"""

    """
    samples 1.0, 2.0, 4.0, 7.0
    median = 3.0
    """
    test_list = [1, 2, 4, 7]
    for i in test_list:
        one_sample(i)
    assert(3.0 == median_meas(0))

    """
    samples 13.0, 13.0, 13.0, 13.0, 14.0, 14.0, 16.0, 18.0, 21.0
    median = 14.0
    """
    test_list = [13.0, 13.0, 13.0, 13.0, 14.0, 14.0, 16.0, 18.0, 21.0]
    for i in test_list:
        one_sample(i)
    assert(14.0 == median_meas(0))


#FUNCIONES DE LA CALCULADORA
respuesta="si"
#FUNCION QUE SUMA DOS VALORES
def sumar(valor1=0,valor2=0):
	return valor1 + valor2
#FUNCION QUE SUMA DOS VALORES

#FUNCION QUE RESTA DOS VALORES
def restar(valor1=0,valor2=0):
	return valor1 - valor2
#FUNCION QUE RESTA DOS VALORES

#FUNCION QUE MULTIPLICACION DOS VALORES
def multiplicar(valor1=0,valor2=0):
	return valor1 * valor2
#FUNCION QUE DIVIDE DOS VALORES

#FUNCION QUE DIVIDE DOS VALORES
def dividir(valor1=0,valor2=0):
	return valor1 / valor2
#FUNCION QUE DIVIDE DOS VALORES

#FUNCIONES DE LA CALCULADORA
def menu():
	#MENU DE OPCIONES
	print("** ::::::::::::::::::::::::: **")
	print(":: Seleccione una operanción ::")
	print("** ::::::::::::::::::::::::: **")
	print("-------------------------------")
	print("| Suma:             ->  1     |")
	print("| Resta:            ->  2     |")
	print("| Multiplicación:   ->  3     |")
	print("| División:         ->  4     |")
	print("| Salir:            ->  s     |")
	print("-------------------------------")
	print("** ::::::::::::::::::::::::: **")
	print("\n")
	#MENU DE OPCIONES
#OPCIÓN DEL MENU

def opciones(opc=0):
	opcion = int(input("Selecione una Opción... "))
	return opcion

def valores(valor1=0,valor2=0):
	valor1 = int(input("Ingrese su primer valor: "))
	valor2 = int(input("Ingrese su segundo valor: "))
	return (valor1,valor2)

def errorOperacion(opcionError=0):
	regresar = input("¿Quiere realizar una nueva operanción [S/N]? ")
	return (regresar)

while respuesta!="no":
	menuPrincipal = menu()
	opc = opciones()
#OPCIÓN DEL MENU

#MODULO DE SUMA
	if(opc == 1):
		print("\n")
		print ("** Entrando al modulo de Suma **")
		valoresTeclado = valores()
		valor1 = valoresTeclado[0]
		valor2 = valoresTeclado[1]
		resultadoSuma = sumar(valor1, valor2)
		print ("El resultado de su suma es: " + str (resultadoSuma))
		#print "Quiere realizar otra operanción?.."
		nuevaOperacion = errorOperacion()
		#regresar = raw_input("¿Quiere realizar una nueva operanción [Si/No]? ")
		if(nuevaOperacion == "S" and "s"):
			print ("Muestro el menu")
			menuPrincipal = menu()
			opc = opciones()
			if(opc == 1):
				valoreTecladoNuevo = valores()
				val1Ope = valoreTecladoNuevo[0]
				val2Ope = valoreTecladoNuevo[1]
				sumarNuevo = sumar(val1Ope,val2Ope)
				print ("El resultado de su suma es: ") + str(sumarNuevo)

			elif(opc == 2):
				restarNuevo = restar(valor1,valor2)

			elif(opc == 3):
				multiplicacionNuevo = multiplicar(valor1,valor2)

		if (nuevaOperacion != "N" and "n" and "S" and "s"):
			print ("adios")
			respuesta="no"
			nuevaOperacion2 = errorOperacion()

			if(nuevaOperacion2 == "S" and "s"):
				print ("Muestro el menu")
				menuNuevo2 = menu()
				opc2 = opciones()

				if(opc2 == 1):
					valoreTecladoNuevo2 = valores()
					val1Ope2 = valoreTecladoNuevo2[0]
					val2Ope2 = valoreTecladoNuevo2[1]
					sumarNuevo2 = sumar(val1Ope2,val2Ope2)
					print ("El resultado de su suma es: ") + str(sumarNuevo2)

				elif(opc == 2):
					restarNuevo2 = restar(valor1,valor2)

				elif(opc == 3):
					multiplicacionNuevo2 = multiplicar(valor1,valor2)

		#PROBLEMA QUE LA LETRA SEA DISTINTA A S Y A N
		# if(nuevaOperacion != "S" and "s" and "N" and "n"):
		# 	print "Seleccione una opción valida"

	#MODULO DE SUMA

	#MODULO DE RESTA
	if(opc == 2):
		print("\n")
		print ("** Entrando al modulo de Resta **")
		valor1 = int(input("Ingrese su primer valor: "))
		valor2 = int(input("Ingrese su segundo valor: "))
		resultadoResta = restar(valor1,valor2)
		print ("El resultado de su resta es: " + str(resultadoResta))

	#MODULO DE MULTIPLICACIÓN
	if(opc == 3):
		print("\n")
		print ("** Entrando al modulo de Multiplicación **")
		valor1 = int(input("Ingrese su primer valor: "))
		valor2 = int(input("Ingrese su segundo valor: "))
		resultadoMultiplicacion = multiplicar(valor1,valor2)
		print ("El resultado de su Multiplicación es: " + str(resultadoMultiplicacion))
	#MODULO DE MULTIPLICACIÓN


#https://codeforces.com/contest/1486/problem/C1

import sys

def solve(l,r):
    if(l!=r):
        center = int((l + r) / 2)
        print('? ',l,' ',r)
        second_max=int(input())
        sys.stdout.flush()
        max1=-1
        dreapta=False

        #caut dreapta
        if(second_max>center):
            print(' ? ', center, ' ', r)
            max1 = int(input())
            sys.stdout.flush()
            if(max1!=second_max):
                dreapta=False
            else:
                dreapta=True
        #caut stanga
        else:
            print('? ', l, ' ', center)
            max1 = int(input())
            sys.stdout.flush()
            if (max1 != second_max):
                dreapta = True
            else:
                dreapta=False
        if (abs(l - r) <= 1 and max1==second_max):
            if(l==max1):
                return (r)
            else:
                return (l)
        else:
            if(dreapta==True):
                return(solve(center,r))
            else:
                return(solve(l,center))
n = int(input())

print('! ',solve(1,n))

CodeForces/Codeforces Round #703 (Div. 2)/C1. Guessing the Greatest (easy version).py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###
###   Goal:
###      Create gene tree file and distance matrix file
###      to root gene tree with profileNJ from RAxML gene trees
###
###   INPUT:
###      1- INPUT DIRECTORY containing files Best ML tree compute by RAxML
###         (DATA/DATA_WaterHouse/GENE_TREES/PROT/bootstrap/RAxML/GF0000001)
###      2- OUTPUT directory path where gene trees files will be stored
###         (DATA/DATA_WaterHouse/GENE_TREES/PROT/bootstrap/profileNJ/UNROOTED_GENE_TREES)
###      3- Bootstrap or SH-like support compute with RAxML boolean
###         (bootstrap | SH-like)
###      4- gene_TAG-species_name association file
###         (WORKING_PROJECT/fixed_params_160407/INPUT_DATA/name_geneID_18anopheles)
###      6- prefix/postfix boolean
###         (prefix or postfix)
###
###   OUTPUT:
###      - Gene trees files and distance matrix files for each gene tree
###        in gene trees file use as INPUT
###
###   Name: 05a-modifGT.py                   Author: Yoann Anselmetti
###   Creation date: 2015/09/21              Last modification: 2020/11/05
###

from sys import argv, path
from re import search, sub, match
from os import close, path, makedirs, rename, listdir
from datetime import datetime

from Bio import Phylo   # Biopython library
from ete3 import Tree

def mkdir_p(dir_path):
   try:
      makedirs(dir_path)
   except OSError as exc: # Python >2.5
      if exc.errno == errno.EEXIST and path.isdir(dir_path):
         pass
      else: raise

def file_len(fname):
   with open(fname) as f:
      for i, l in enumerate(f):
         pass
   return i+1

def replaceStringInFile(filePath,old_pattern,new_pattern):
   "replaces all string by a regex substitution"
   tempName = filePath+'~~~'
   inputFile = open(filePath)
   outputFile = open(tempName,'w')
   fContent = str(inputFile.read(), "utf-8")

   outText = sub(old_pattern, new_pattern, fContent)
   outputFile.write((outText.encode("utf-8")))

   outputFile.close()
   inputFile.close()

   rename(tempName, filePath)

################
###   MAIN
################
if __name__ == '__main__':

   start_time = datetime.now()

   # Recovery of input parameters
   DIR_RAxML=argv[1]
   DIR_gene_trees=argv[2]
   bool_supp_raxml=argv[3]
   speciesName_geneID_file=open(argv[4],"r")
   order_bool=argv[5]

   sep="@"

   # Store association Gene-Species in "dict_gene_species"
   dict_geneID_speciesName={}
   for gene in speciesName_geneID_file:
      r=search("^([^\t ]*)[\t ]*([^\t ]*)\n$",gene)
      if r:
         speciesName=r.group(1)
         geneID=r.group(2)
         dict_geneID_speciesName[geneID]=speciesName
   speciesName_geneID_file.close()

   # To be sure than directory have no "/" to the end of the path
   DIR_RAxML=path.normpath(DIR_RAxML)
   # To be sure than directory have no "/" to the end of the path
   DIR_gene_trees=path.normpath(DIR_gene_trees)

   # Create DIR_gene_trees if not existing
   if not path.exists(DIR_gene_trees):
      mkdir_p(DIR_gene_trees)

   # Get Gene Family ID
   GF_ID=path.basename(DIR_RAxML)

   # Get path of Best ML tree with support (SH-like or bootstrap)
   BestML_tree=""
   if bool_supp_raxml=="bootstrap":
      BestML_tree="RAxML_bipartitionsBranchLabels."+GF_ID+"_treeBS"
   elif bool_supp_raxml=="SH-like":
      BestML_tree="RAxML_fastTreeSH_Support."+GF_ID+"_treeSH"
   else:
      exit("\n!!! ERROR, RAxML support boolean, should be \"bootstrap\" or \"SH-like\" and not "+bool_supp_raxml+" !!!")

   # TEST if a Best ML tree have been computed for the current Gene family ID
   if not BestML_tree in listdir(DIR_RAxML):
      print("NO BestML Tree file for Gene Family: "+GF_ID)
   else:
      print("Processing Gene Family "+GF_ID+":")
      # Path for OUTPUT gene tree & matrix distance files
      # OUTPUT_DIST=DIR_dist_matrix+"/dist_matrix_"+GF_ID+".dist"
      OUTPUT_tree=DIR_gene_trees+"/gene_tree_"+GF_ID+".nwk"

      # Get tree with RAxML support
      INPUT_tree=DIR_RAxML+"/"+BestML_tree

      # Write INPUT_tree (Best ML RAxML tree) without branch length in OUTPUT_tree
      in_tree=open(INPUT_tree,'r')
      out_tree=open(OUTPUT_tree,'w')
      # Remove branch length from INPUT RAxML gene tree
      tree_with_ete3_format = sub('\)(:[0-9.]+)\[([0-9]+)\]', r')\2\1', in_tree.read())
      # Add species name to gene
      out_tree.write(tree_with_ete3_format)
      out_tree.close()
      in_tree.close()

   #########################
   ### with ETE3 module  ### => Function to prune gene trees!!!
   #########################

      list_genes=list()
      tree=Tree(OUTPUT_tree)
      # Get list of extant genes in current gene tree
      list_genes=tree.get_leaf_names()

      # Browse list_genes to create FASTA file of the current gene tree
      for gene in list_genes:
         print("\t"+gene)
         gene_present=False
         species=""
         for geneID in dict_geneID_speciesName:
            if geneID in gene:
               gene_present=True
               species=dict_geneID_speciesName[geneID]
               break
         if not gene_present:
            exit("ERROR, species of gene "+gene+" is not present in "+str(argv[4])+" !!!!")
         else:
            old_pattern=gene
            new_pattern=""
            if order_bool=="prefix":
               new_pattern=species+sep+gene
            elif order_bool=="postfix":
               new_pattern=gene+sep+species
            else:
               exit("ERROR, parameter 5 should be equal to \"prefix\" or \"postfix\" !!!")
            replaceStringInFile(OUTPUT_tree,old_pattern,new_pattern)

   end_time = datetime.now()
   print('\nDuration: {}'.format(end_time - start_time))


import pickle
from scipy import spatial
from sqlalchemy import create_engine
import pandas as pd

def LoadKDTree():
    nsDb = create_engine('mssql+pymssql://netcoreusr:OC9Ey84RF*Vp^a@172.29.8.30:1433/NetCoreProdCache2008R2')
    citiesQuery = "select Cities.Id, Cities.CityCode, Cities.Latitude, Cities.Longitude, Cities.DescriptionES, " \
                  "C.DescriptionES from Cities inner join Countries C on Cities.CountryId = C.Id " \
                  "where (Latitude <> 0 or Latitude is not null) and IsSearchable = 1"
    citiesNs = pd.read_sql_query(citiesQuery, con=nsDb)
    cities = []
    for index, row in citiesNs.iterrows():
        cities.append((row['Latitude'], row['Longitude']))

    tree = spatial.cKDTree(cities)
    pickle.dump(tree, open('cities.tree', 'wb'))
    pickle.dump(citiesNs, open('cities.pandas', 'wb'))

if __name__ == '__main__':
    LoadKDTree()


from modelAdmin import ModelAdmin
from modelProfessor import ModelProfessor

class Login():
    def __init__(self):
        self.admin = ModelAdmin()
        self.professor = ModelProfessor()

    def login(self, data):
        usuario = self.admin.search(data["usuario"])
        if usuario != None:
            if usuario[0][4] == data["senha"]:
                return ["Deu Boa!", {"id": usuario[0][0], "usuario": usuario[0][3], "admin": True }]
            else:
                return ["Senha incorreta!", None]

        else:
            usuario = self.professor.search(data["usuario"])
            if usuario != None:
                if usuario[0][4] == data["senha"]:
                    return ["Deu Boa!", {"id": usuario[0][0], "usuario": usuario[0][3], "admin": False }]
                else:
                    return ["Senha incorreta!", None]

        return ["Usuario não existe!", None]


import win32com.client as client
import pandas as pd
from tools import Sample_Gen, Crude_Feed, Xlsx_Read, Emerge_xlsx
import random
import os, math

# Arguements
CDU1_Feed = 100
CDU2_Feed = 95
Num_of_Samples = 30000
Least_crudes_blend = 2
Save_Ksc_File = False
Auto_shut_up = True
Emerge_forms = True
# File_path
try:
    working_directory = os.path.dirname(os.path.realpath(__file__))
except NameError:
    working_directory =  r'C:\Users\en19\Desktop\Coker'
index_path = working_directory + r'\index.xlsx'
ksc_path = working_directory + r'\No1.ksc'
record_path = working_directory + r'\records' + '\\' + r'Record.xlsx'
records_path = working_directory + r'\records'
final_path = working_directory + '\\' + r'Record.xlsx'
# Load the index
Index = Xlsx_Read(index_path)
# Locate the Crudes
crude_Names_1 = []
crude_Names_2 = []
crude_index = Index['crude']
Num_of_crudes = len(crude_index) - 2
for row in range(2, Num_of_crudes + 2):
    if crude_index[row][1] == 'CDU1':
        crude_Names_1.append(str(crude_index[row][2]) + '@' + str(crude_index[row][0]))
    elif crude_index[row][1] == 'CDU2':
        crude_Names_2.append(str(crude_index[row][2]) + '@' + str(crude_index[row][0]))
# Locate the Operation Units
input_Names = []
opu_Outputs = []
input_Props = []
Bound_List = []
opu_index = Index['opu']
Num_of_opus = len(opu_index) // 4
for row in range(0, Num_of_opus):
    for col in range(opu_index[row * 4 + 3][3]):
        if opu_index[row * 4 + 3][col + 4] == 1:
            input_Names.append(str(opu_index[row * 4 + 1][col + 4]) + ' of ' +
                               str(opu_index[row * 4 + 3][1]) + '@' +
                               str(opu_index[row * 4 + 3][0]))
            input_Props.append(str(opu_index[row * 4 + 1][col + 4]))
            Bound_List.append(list(eval(str(opu_index[row * 4][col + 4]))))
        elif opu_index[row * 4 + 3][col + 4] == 2:
            opu_Outputs.append(str(opu_index[row * 4 + 1][col + 4]) + ' of ' +
                               str(opu_index[row * 4 + 3][1]) + '@' +
                               str(opu_index[row * 4 + 3][0]))
# Locate the Material Streams
mstr_index = Index['mstr_in']
Num_of_mstrs = len(mstr_index) // 4
for row in range(0, Num_of_mstrs):
    for col in range(mstr_index[row * 4 + 3][2]):
        if mstr_index[row * 4 + 3][col + 3] == 1:
            input_Names.append(str(mstr_index[row * 4 + 1][col + 3]) + ' of ' +
                               str(mstr_index[row * 4 + 3][1]) + '@' +
                               str(mstr_index[row * 4 + 3][0]))
            input_Props.append(str(mstr_index[row * 4 + 1][col + 3]))
            Bound_List.append(list(eval(str(mstr_index[row * 4][col + 3]))))
# Locate the Detailed mstrs
detailed_mstr_Names = []
detailed_mstr_index = Index['mstr_out']
Num_of_detailed_mstr = max(0, len(detailed_mstr_index) - 1)
for row in range(0, Num_of_detailed_mstr):
    for col in range(5, len(detailed_mstr_index[row + 1])):
        if detailed_mstr_index[row + 1][col]:
            detailed_mstr_Names.append(str(detailed_mstr_index[0][col]) + ' of ' +
                                       str(detailed_mstr_index[row + 1][3]) + '@' +
                                       str(detailed_mstr_index[row + 1][0]))
# Generate the Crude feeds
Crude_Feeds_1 = []
for num_of_crudes in range(Least_crudes_blend, len(crude_Names_1)):
    seed = num_of_crudes
    Crude_Feeds = Crude_Feed(num_of_crudes, len(crude_Names_1), CDU1_Feed, seed = seed, num_of_balls = 6, fluc = 0.1)
    while len(Crude_Feeds) < math.ceil(Num_of_Samples / (len(crude_Names_1) - Least_crudes_blend)):
        seed += 1
        Crude_Feeds = Crude_Feeds + Crude_Feed(num_of_crudes, len(crude_Names_1), CDU1_Feed, seed = seed, num_of_balls = 6, fluc = 0.1)
    Crude_Feeds_1 = Crude_Feeds_1 + Crude_Feeds[0 : math.ceil(Num_of_Samples / (len(crude_Names_1) - Least_crudes_blend))]
Crude_Feeds_2 = []
for num_of_crudes in range(Least_crudes_blend, len(crude_Names_1)):
    seed = num_of_crudes
    Crude_Feeds = Crude_Feed(num_of_crudes, len(crude_Names_2), CDU2_Feed, seed = seed, num_of_balls = 6, fluc = 0.1)
    while len(Crude_Feeds) < math.ceil(Num_of_Samples / (len(crude_Names_1) - Least_crudes_blend)):
        seed += 1
        Crude_Feeds = Crude_Feeds + Crude_Feed(num_of_crudes, len(crude_Names_1), CDU1_Feed, seed = seed, num_of_balls = 6, fluc = 0.1)
    Crude_Feeds_2 = Crude_Feeds_2 + Crude_Feeds[0 : math.ceil(Num_of_Samples / (len(crude_Names_1) - Least_crudes_blend))]
random.shuffle(Crude_Feeds_1)
random.shuffle(Crude_Feeds_2)
# Generate the Operation settings
for i in range (1, Num_of_Samples):
    if i**len(Bound_List) >= Num_of_Samples:
        Breaks_Points = i
        break
seed = 1
Inputs = Sample_Gen(Bound_List, seed, Breaks_Points)
while len(Inputs) < Num_of_Samples:
    seed += 1
    Inputs = Inputs + Sample_Gen(Bound_List, seed, Breaks_Points)
Inputs = Inputs[0 : Num_of_Samples]
Crude_Datas = {'title' : (crude_Names_1 + crude_Names_2)}
Input_Datas = {'title' : input_Names}
Opu_Datas = {'title' : opu_Outputs}
Stream_Datas = {'title' : detailed_mstr_Names}
# Start Generating
PeSIM = client.Dispatch('Petrosim.Application')
flag = 11224
for i in range(20473, Num_of_Samples):
    # Restart Petrosim
    if i % 20 == 19:
        PeSIM = client.Dispatch('Petrosim.Application')
        print('Reastarting PetroSim')
        Crude_Datas = {'title' : (crude_Names_1 + crude_Names_2)}
        Input_Datas = {'title' : input_Names}
        Opu_Datas = {'title' : opu_Outputs}
        Stream_Datas = {'title' : detailed_mstr_Names}
    # Load the Case
    Case = PeSIM.SimulationCases.Open(ksc_path)
    Case.Visible = 1
    # Locate the Crude Feeds
    crude_List_1 = []
    crude_List_2 = []
    for row in range(2, Num_of_crudes + 2):
        if crude_index[row][1] == 'CDU1':
            crude_List_1.append(Case.flowsheet.Operations.Item(str(crude_index[row][0])).\
                                OwnedFlowsheet.Streams.Item(str(crude_index[row][2])))
        elif crude_index[row][1] == 'CDU2':
            crude_List_2.append(Case.flowsheet.Operations.Item(str(crude_index[row][0])).\
                                OwnedFlowsheet.Streams.Item(str(crude_index[row][2])))
    # Locate the opu_Inputs
    opu_List = []
    input_List = []
    for row in range(0, Num_of_opus):
        opu_List.append(Case.flowsheet.Operations.Item(str(opu_index[row * 4 + 3][0])).\
                        OwnedFlowsheet.Operations.Item(str(opu_index[row * 4 + 3][1])))
        for col in range(opu_index[row * 4 + 3][3]):
            if opu_index[row * 4 + 3][col + 4] == 1:
                input_List.append(Case.flowsheet.Operations.Item(str(opu_index[row * 4 + 3][0])).\
                                  OwnedFlowsheet.Operations.Item(str(opu_index[row * 4 + 3][1])))
    # Locate the mstr_Inputs
    for row in range(0, Num_of_mstrs):
        for col in range(mstr_index[row * 4 + 3][2]):
            if mstr_index[row * 4 + 3][col + 3] == 1:
                input_List.append(Case.flowsheet.Operations.Item(str(mstr_index[row * 4 + 3][0])).\
                                  OwnedFlowsheet.Streams.Item(str(mstr_index[row * 4 + 3][1])))
    # Locate the Outputs
    Properties = Case.flowsheet.Operations.Item(detailed_mstr_index[0][0]).\
                 OwnedFlowsheet.Operations.Item(detailed_mstr_index[0][1])
    # Turn off the solver
    Case.Solver.CanSolve = False
    # Input the Crude Composition
    for j in range(len(crude_List_1)):
        crude_List_1[j].MassFlowValue = Crude_Feeds_1[i][j]
    for j in range(len(crude_List_2)):
        crude_List_2[j].MassFlowValue = Crude_Feeds_2[i][j]
    # Input the stream and opu operating datas
    for j in range(len(input_Props)):
        exec('input_List[' + str(j) + '].' + input_Props[j] + ' = Inputs[' + str(i) + '][' + str(j) + ']')
    # Turn on the solver
    Case.Solver.CanSolve = True
    # Gather the Operation Datas
    Solved = 1
    for opu in opu_List:
        if not ((opu.StatusCondition() == 'OK' or opu.StatusCondition() == 'Warning')
                 and (Properties.Cell(1,1).cellValue != -32767)):
            Solved = 0
    # Gather Datas
    if Solved:
        flag += 1
        # Save the .ksc File
        if Save_Ksc_File:
            a = r'No.' + '%d' %flag
            b = r'%d' %(i + 1)
            Case.SaveAs(working_directory + r'\kscs' + '\\' + a + r' of '+ b + r'.ksc')
        Crude_Datas[str(flag)] = Crude_Feeds_1[i] + Crude_Feeds_2[i]
        Input_Datas[str(flag)] = Inputs[i]
        Opu_Data = []
        for j in range(0, Num_of_opus):
            for k in range(opu_index[j * 4 + 3][3]):
                if opu_index[j * 4 + 3][k + 4] == 2:
                    Opu_Data.append(eval('opu_List[' + str(j) + '].' + opu_index[j * 4 + 1][k + 4]))
        Opu_Datas[str(flag)] = Opu_Data
        Stream_Data = []
        for j in range(0, Num_of_detailed_mstr):
            for k in range(0, len(detailed_mstr_index[row + 1]) - 5):
                if detailed_mstr_index[j + 1][k + 5]:
                    Stream_Data.append(Properties.Cell(j + 1, k + 1).cellValue)
        Stream_Datas[str(flag)] = Stream_Data
    # Close the case and quit the Petrosim every 20 times
    Case.Close([False], [ksc_path])
    if i % 20 == 18:
        PeSIM.Quit()
    # Export the datas to excel and make some time difference between Closing and Restarting in case of error
    DataFrame1 = pd.DataFrame(Crude_Datas).set_index('title').T
    DataFrame2 = pd.DataFrame(Input_Datas).set_index('title').T
    DataFrame3 = pd.DataFrame(Opu_Datas).set_index('title').T
    DataFrame4 = pd.DataFrame(Stream_Datas).set_index('title').T
    with pd.ExcelWriter(record_path) as writer:
        DataFrame1.to_excel(writer, sheet_name = 'Crude_Datas')
        DataFrame2.to_excel(writer, sheet_name = 'Input_Datas')
        DataFrame3.to_excel(writer, sheet_name = 'Opu_Datas')
        DataFrame4.to_excel(writer, sheet_name = 'Stream_Datas')
    print('Samples:{},Total:{}'.format(flag,(i + 1)))
    if i % 20 == 18:
        record_path = working_directory + r'\records' + '\\' + r'Record' + str(i//20) + r'.xlsx'
PeSIM.Quit()
# Emerge the xlsxs
if Emerge_forms:
    Emerge_xlsx(records_path, final_path)
# Shut down the computer
if Auto_shut_up:
    os.system('shutdown -s -t 60')


from django.urls import path
from . import views

app_name = 'pages'
urlpatterns = [
    path('index', views.PublicationIndexView.as_view(), name='publications-index-view'),
    path('publication/<int:pk>', views.PublicationDetailView.as_view(), name='publication-detail-view'),

    path('publication/create-comment', views.CommentCreationAjaxView.as_view(), name='create-comment-view'),
    path('publication/vote', views.VoteAjaxView.as_view(), name='vote'),
]