# -*- coding: utf-8 -*-
"""
Created on Fri May 13 13:10:10 2022

@author: GRCav
"""

#Faça o gráfico de dispersão do tamanho do pé (FootLen) em função da altura da pessoa (Height). Calcule (e mostre na tela) a correlação e plote a reta de regressão entre esses dados.

#importando a tabela
import pandas as pd
BDSinfo = pd.read_csv('https://raw.githubusercontent.com/rnwatanabe/BasesComputacionais2021/main/dados/BDSinfo.csv', delimiter = '\t')

#criando o gráfico de dispersão
import matplotlib.pyplot as plt
plt.figure()
plt.plot(BDSinfo['FootLen'], BDSinfo['Height'], marker = "o", color = 'red', linestyle = '')
plt.xlabel('Tamanho do pé (FootLen)')
plt.ylabel('Altura da pessoa (Height)')
plt.grid()
plt.show()

#Calcule (e mostre na tela) a correlação
corr_BDSinfo = BDSinfo['FootLen'].corr(BDSinfo['Height'])
print(corr_BDSinfo)

#plote a reta de regressão entre esses dados
#Calculando a Regressão
import numpy as np
x = BDSinfo['FootLen']
y = BDSinfo['Height']
mediaX = np.mean(x)
mediaY = np.mean(y)
DesvioX = x - mediaX
DesvioY = y - mediaY
m = np.sum(DesvioX * DesvioY)/np.sum(DesvioX**2)
b = mediaY - m * mediaX
print(m)
print(b)

#Fazendo o gráfico da reta estimada pela regressão
import matplotlib.pyplot as plt
plt.figure()
plt.plot(BDSinfo['FootLen'], BDSinfo['Height'], marker = "o", color = 'red', linestyle = '')
plt.plot(x, m * x + b, color = 'blue', linestyle = '-')
plt.xlabel('Tamanho do pé (FootLen)')
plt.ylabel('Altura da pessoa (Height)')
plt.grid()
plt.show()


from flask import Flask, render_template, request

import sqlite3 as sql

app = Flask(__name__)

@app.route("/")
def home():
    return render_template('home.html')

@app.route("/newstudent")
def new_student():
    return render_template('student.html')

@app.route("/addstudent", methods=['POST', 'GET'])
def addstudent():
    if request.method == 'POST':
        try:
            name = request.form['name']
            address = request.form['address']
            city = request.form['city']
            pin = request.form['pin']
            with sql.connect("database.db") as connection:
                my_cursor = connection.cursor()
                my_cursor.execute("INSERT INTO students (name, addr, city, pin) VALUES (?,?,?,?)", (name, address, city,
                                  pin))
                msg = "Record Successfully inserted"
        except:
            connection.rollback()
            msg = "Error inserting operations"
        finally:
            return render_template("result.html", msg=msg)
            connection.close()

@app.route("/list")
def list():
    connection = sql.connect("database.db")
    connection.row_factory = sql.Row
    my_cursor = connection.cursor()
    my_cursor.execute("SELECT * FROM students")
    rows = my_cursor.fetchall()
    return render_template('list.html', rows=rows)

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


import importlib

from . import old_eye_material
from . import old_misc_material
from . import old_skin_material

files_list = (
    old_eye_material,
    old_misc_material,
    old_skin_material,
)

#Reload
for file in files_list:
    importlib.reload(file)

def register():
    for file in files_list:
        file.register()

def unregister():
    for file in files_list:
        file.unregister()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('perf', '0010_auto_20160806_1519'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='stafftarget',
            options={'ordering': ['-id'], 'verbose_name': '\u5458\u5de5\u76ee\u6807\u7ba1\u7406', 'verbose_name_plural': '\u5458\u5de5\u76ee\u6807\u7ba1\u7406 '},
        ),
        migrations.RemoveField(
            model_name='stafftarget',
            name='month',
        ),
        migrations.RemoveField(
            model_name='stafftarget',
            name='year',
        ),
    ]


from numpy import zeros, where, full, int64, float64
from numba import njit
from HSP2.utilities import initm, make_numba_dict, hourflag

ERRMSG = []

# english system
# parameters for variables with energy units
EFACTA = 407960.
EFACTB = 0.0

# parameters for variables with temperature units
TFACTA = 1.8
TFACTB = 32.0

# parameters for variables for dissolved gases with mass units
MFACTA = 0.2266
MFACTB = 0.

def pwtgas(io_manager, siminfo, uci, ts):
    ''' Estimate water temperature, dissolved oxygen, and carbon dioxide in the outflows
    from a pervious landsegment. calculate associated fluxes through exit gates'''
    simlen = siminfo['steps']

    ui = make_numba_dict(uci)
    ui['simlen'] = siminfo['steps']
    ui['uunits'] = siminfo['units']
    ui['errlen'] = len(ERRMSG)

    u = uci['PARAMETERS']
    if 'IDVFG' in u:
        ts['IDOXP'] = initm(siminfo, uci, u['IDVFG'], 'MONTHLY_IDOXP', u['IDOXP'])
    else:
        ts['IDOXP'] = full(simlen, u['IDOXP'])
    if 'ICVFG' in u:
        ts['ICO2P'] = initm(siminfo, uci, u['ICVFG'], 'MONTHLY_ICO2P', u['ICO2P'])
    else:
        ts['ICO2P'] = full(simlen, u['ICO2P'])
    if 'GDVFG' in u:
        ts['ADOXP'] = initm(siminfo, uci, u['GDVFG'], 'MONTHLY_ADOXP', u['ADOXP'])
    else:
        ts['ADOXP'] = full(simlen, u['ADOXP'])
    if 'GCVFG' in u:
        ts['ACO2P'] = initm(siminfo, uci, u['GCVFG'], 'MONTHLY_ACO2P', u['ACO2P'])
    else:
        ts['ACO2P'] = full(simlen, u['ACO2P'])

    ts['DAYFG'] = hourflag(siminfo, 0, dofirst=True).astype(float64)

    ############################################################################
    errors = _pwtgas_(ui, ts)  # run PWTGAS simulation code
    ############################################################################

    return errors, ERRMSG

@njit(cache=True)
def _pwtgas_(ui, ts):
    ''' Estimate water temperature, dissolved oxygen, and carbon dioxide in the outflows
    from a pervious landsegment. calculate associated fluxes through exit gates'''

    errorsV = zeros(int(ui['errlen'])).astype(int64)

    uunits = ui['uunits']
    simlen = int(ui['simlen'])

    CSNOFG = int(ui['CSNOFG'])
    sotmp  = ui['SOTMP']
    iotmp  = ui['IOTMP']
    aotmp  = ui['AOTMP']
    sodox  = ui['SODOX']
    soco2  = ui['SOCO2']
    iodox  = ui['IODOX']
    ioco2  = ui['IOCO2']
    aodox  = ui['AODOX']
    aoco2  = ui['AOCO2']
    sdlfac = ui['SDLFAC']
    slifac = ui['SLIFAC']
    ilifac = ui['ILIFAC']
    alifac = ui['ALIFAC']
    elev   = ui['ELEV']
    if uunits == 2:
        sotmp = (sotmp * 9. / 5.) + 32.
        iotmp = (iotmp * 9. / 5.) + 32.
        aotmp = (aotmp * 9. / 5.) + 32.
        elev = elev * 3.281  # m to ft
    elevgc = ((288.0 - 0.00198 * elev) / 288.0) ** 5.256

    if 'GDVFG' in ui:
        gdvfg = ui['GDVFG']
    else:
        gdvfg = 0

    IDOXP = ts['IDOXP']
    ICO2P = ts['ICO2P']
    ADOXP = ts['ADOXP']
    ACO2P = ts['ACO2P']

    for name in ['WYIELD', 'SURO', 'IFWO', 'AGWO', 'SURLI', 'IFWLI', 'AGWLI']:
        if name not in ts:
            ts[name] = zeros(simlen)
    WYIELD = ts['WYIELD']
    SURO   = ts['SURO']
    IFWO   = ts['IFWO']
    AGWO   = ts['AGWO']
    SURLI  = ts['SURLI']
    IFWLI  = ts['IFWLI']
    AGWLI  = ts['AGWLI']

    for name in ['SLTMP', 'ULTMP', 'LGTMP', 'SLITMP', 'SLIDOX', 'SLICO2']:
        if name not in ts:
            ts[name] = full(simlen, -1.0E30)
    SLTMP  = ts['SLTMP']
    ULTMP  = ts['ULTMP']
    LGTMP  = ts['LGTMP']
    SLITMP = ts['SLITMP']
    SLIDOX = ts['SLIDOX']
    SLICO2 = ts['SLICO2']

    for name in ['ILITMP', 'ILIDOX', 'ILICO2']:
        if name not in ts:
            ts[name] = full(simlen, -1.0E30)
    ILITMP = ts['ILITMP']
    ILIDOX = ts['ILIDOX']
    ILICO2 = ts['ILICO2']

    for name in ['ALITMP', 'ALIDOX', 'ALICO2']:
        if name not in ts:
            ts[name] = full(simlen, -1.0E30)
    ALITMP = ts['ALITMP']
    ALIDOX = ts['ALIDOX']
    ALICO2 = ts['ALICO2']

    # preallocate output arrays
    SOTMP  = ts['SOTMP']  = zeros(simlen)
    IOTMP  = ts['IOTMP']  = zeros(simlen)
    AOTMP  = ts['AOTMP']  = zeros(simlen)
    SODOX  = ts['SODOX']  = zeros(simlen)
    SOCO2  = ts['SOCO2']  = zeros(simlen)
    IODOX  = ts['IODOX']  = zeros(simlen)
    IOCO2  = ts['IOCO2']  = zeros(simlen)
    AODOX  = ts['AODOX']  = zeros(simlen)
    AOCO2  = ts['AOCO2']  = zeros(simlen)
    SOHT   = ts['SOHT']   = zeros(simlen)
    IOHT   = ts['IOHT']   = zeros(simlen)
    AOHT   = ts['AOHT']   = zeros(simlen)
    POHT   = ts['POHT']   = zeros(simlen)
    SODOXM = ts['SODOXM'] = zeros(simlen)
    SOCO2M = ts['SOCO2M'] = zeros(simlen)
    IODOXM = ts['IODOXM'] = zeros(simlen)
    IOCO2M = ts['IOCO2M'] = zeros(simlen)
    AODOXM = ts['AODOXM'] = zeros(simlen)
    AOCO2M = ts['AOCO2M'] = zeros(simlen)
    PODOXM = ts['PODOXM'] = zeros(simlen)
    POCO2M = ts['POCO2M'] = zeros(simlen)

    DAYFG = ts['DAYFG'].astype(int64)

    if uunits == 2:
        SLTMP = (SLTMP * 9. / 5.) + 32.
        ULTMP = (ULTMP * 9. / 5.) + 32.
        LGTMP = (LGTMP * 9. / 5.) + 32.
        WYIELD = WYIELD * 0.0394  # / 25.4
        SURO = SURO * 0.0394  # mm to inches
        IFWO = IFWO * 0.0394  # mm to inches
        AGWO = AGWO * 0.0394  # mm to inches

    for loop in range(simlen):
        dayfg   = DAYFG[loop]
        suro   = SURO[loop]
        wyield = WYIELD[loop]
        ifwo   = IFWO[loop]
        agwo   = AGWO[loop]
        surli  = SURLI[loop]
        ifwli  = IFWLI[loop]
        agwli  = AGWLI[loop]
        sltmp  = SLTMP[loop]
        slitmp = SLITMP[loop]
        slidox = SLIDOX[loop]
        slico2 = SLICO2[loop]
        ultmp  = ULTMP[loop]
        idoxp  = IDOXP[loop]
        ico2p  = ICO2P[loop]
        adoxp  = ADOXP[loop]
        aco2p  = ACO2P[loop]

        sotmp = -1.0e30
        sodox = -1.0e30
        soco2 = -1.0e30
        if suro > 0.0:  # there is surface outflow
            # local surface outflow temp equals surface soil temp
            sotmp = (sltmp - 32.0) * 5./9.
            if sotmp < 0.5:
                sotmp = 0.5  # min water temp
            if CSNOFG:      # effects of snow are considered
                # adjust surface outflow temperature if snowmelt is occurring
                if wyield > 0.0:
                    sotmp = 0.5  # snowmelt is occuring - use min temp

            # oxygen calculation
            dummy = sotmp * (0.007991 - 0.77774E-4 * sotmp)
            sodox = (14.652 + sotmp * (-0.41022 + dummy)) * elevgc

            # carbon dioxide calculation
            abstmp = sotmp + 273.16
            dummy = 2385.73 / abstmp - 14.0184 + 0.0152642 * abstmp
            soco2 = 10.0**dummy * 3.16e-04 * elevgc * 12000.0

            if surli > 0.0 and slifac > 0.0:  # check for effects of lateral inflow
                if slitmp >= -1.0e10:   # there is temperature of surface lateral inflow
                    sotmp = slitmp * slifac + sotmp * (1.0 - slifac)
                if slidox >= 0.0:    # there is do conc of surface lateral inflow
                    sodox = slidox * slifac + sodox * (1.0 - slifac)
                if slico2 >= 0.0:  #there is co2 conc of surface lateral inflow
                    soco2 = slico2 * slifac + soco2 * (1.0 - slifac)

        # get interflow lateral inflow temp and concentrations
        ilitmp = -1.0e30
        ilidox = -1.0e30
        ilico2 = -1.0e30
        if ifwli > 0.0:    # there is lateral inflow
            ilitmp = ILITMP[loop]
            ilidox = ILIDOX[loop]
            ilico2 = ILICO2[loop]

        if dayfg:    # it is the first interval of the day
            idoxp = IDOXP[loop]
            ico2p = ICO2P[loop]

        iotmp = -1.0e30
        iodox = -1.0e30
        ioco2 = -1.0e30
        if ifwo > 0.0:   # there is interflow outflow
            # local interflow outflow temp equals upper soil temp
            iotmp = (ultmp - 32.0) * 5./9.
            if iotmp < 0.5:
                iotmp = 0.5    # min water temp

            iodox = idoxp
            ioco2 = ico2p

            if ifwli > 0.0 and ilifac > 0.0:
                # check for effects of lateral inflow
                if ilitmp >= -1.0e10:   # there is temperature of interflow lateral inflow
                    iotmp = ilitmp * ilifac + iotmp * (1.0 - ilifac)
                if ilidox >= 0.0:       # there is do conc of interflow lateral inflow
                    iodox = ilidox * ilifac + iodox * (1.0 - ilifac)
                if ilico2 >= 0.0:       # there is co2 conc of interflow lateral inflow
                    ioco2 = ilico2 * ilifac + ioco2 * (1.0 - ilifac)

        # get baseflow lateral inflow temp and concentrations
        alitmp = -1.0e30
        alidox = -1.0e30
        alico2 = -1.0e30
        if agwli > 0.0:
            alitmp = ALITMP[loop]
            alidox = ALIDOX[loop]
            alico2 = ALICO2[loop]

        if dayfg:    #it is the first interval of the day
            if gdvfg:
                adoxp = ADOXP[loop]
                aco2p = ACO2P[loop]

        aotmp = -1.0e30
        aodox = -1.0e30
        aoco2 = -1.0e30
        if agwo > 0.0:   # there is baseflow
            aotmp = (LGTMP[loop] - 32.0) * 5./9.		# local baseflow temp equals lower/gw soil temp
            if aotmp < 0.5:     # min water temp
                aotmp = 0.5

            aodox = adoxp
            aoco2 = aco2p
            if agwli > 0.0 and alifac > 0.0:   #check for effects of lateral inflow
                if alitmp >= -1.0e10:   # there is temperature of baseflow lateral inflow
                    aotmp = alitmp * alifac + aotmp * (1.0 - alifac)
                if alidox >= 0.0:	# there is do conc of baseflow lateral inflow
                    aodox = alidox * alifac + aodox * (1.0 - alifac)
                if alico2 >= 0.0:    # there is co2 conc of baseflow lateral inflow
                    aoco2 = alico2 * alifac + aoco2 * (1.0 - alifac)

        # compute the outflow of heat energy in water - units are deg. c-in./ivl
        soht = sotmp * suro * EFACTA
        ioht = iotmp * ifwo * EFACTA
        aoht = aotmp * agwo * EFACTA
        POHT[loop] = soht + ioht + aoht

        # calculate outflow mass of dox - units are mg-in./l-ivl
        sodoxm = sodox * suro * MFACTA
        iodoxm = iodox * ifwo * MFACTA
        aodoxm = aodox * agwo * MFACTA
        PODOXM[loop] = sodoxm + iodoxm + aodoxm

        # calculate outflow mass of co2 - units are mg-in./l-ivl
        soco2m = soco2 * suro * MFACTA
        ioco2m = ioco2 * ifwo * MFACTA
        aoco2m = aoco2 * agwo * MFACTA
        POCO2M[loop] = soco2m + ioco2m + aoco2m

        SOTMP[loop] = sotmp
        if sotmp > -1e28 and uunits!= 2:
            SOTMP[loop] = (sotmp * 9.0 / 5.0) + 32.0

        IOTMP[loop] = iotmp
        if iotmp > -1e28 and uunits!= 2:
            IOTMP[loop] = (iotmp * 9.0 / 5.0) + 32.0

        AOTMP[loop] = aotmp
        if aotmp > -1e28 and uunits!= 2:
            AOTMP[loop] = (aotmp * 9.0 / 5.0) + 32.0

        SODOX[loop]  = sodox
        SOCO2[loop]  = soco2
        IODOX[loop]  = iodox
        IOCO2[loop]  = ioco2
        AODOX[loop]  = aodox
        AOCO2[loop]  = aoco2
        SOHT[loop]   = soht
        IOHT[loop]   = ioht
        AOHT[loop]   = aoht

        SODOXM[loop] = sodoxm
        SOCO2M[loop] = soco2m
        IODOXM[loop] = iodoxm
        IOCO2M[loop] = ioco2m
        AODOXM[loop] = aodoxm
        AOCO2M[loop] = aoco2m

        if uunits == 2:
            SOHT[loop] = SOHT[loop] / 3.96567 * 2.471  # btu/ac to kcal/ha
            IOHT[loop] = IOHT[loop] / 3.96567 * 2.471  # btu/ac to kcal/ha
            AOHT[loop] = AOHT[loop] / 3.96567 * 2.471  # btu/ac to kcal/ha
            POHT[loop] = POHT[loop] / 3.96567 * 2.471  # btu/ac to kcal/ha
            SODOXM[loop] = SODOXM[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            SOCO2M[loop] = SOCO2M[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            IODOXM[loop] = IODOXM[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            IOCO2M[loop] = IOCO2M[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            AODOXM[loop] = AODOXM[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            AOCO2M[loop] = AOCO2M[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            PODOXM[loop] = PODOXM[loop] / 2.205 * 2.471  # lbs/ac to kg/ha
            POCO2M[loop] = POCO2M[loop] / 2.205 * 2.471  # lbs/ac to kg/ha

    return errorsV


import torch
from torch.optim.lr_scheduler import LambdaLR
import math

from utils.optimizers.sam import SAM

class WarmupCosineSchedule(LambdaLR):
    """ Linear warmup and then cosine decay.
        Linearly increases learning rate from 0 to 1 over `warmup_steps` training steps.
        Decreases learning rate from 1. to 0. over remaining `t_total - warmup_steps` steps following a cosine curve.
        If `cycles` (default=0.5) is different from default, learning rate follows cosine function after warmup.
    """
    def __init__(self, optimizer, warmup_steps, t_total, cycles=.5, last_epoch=-1):
        self.warmup_steps = warmup_steps
        self.t_total = t_total
        self.cycles = cycles
        super(WarmupCosineSchedule, self).__init__(optimizer, self.lr_lambda, last_epoch=last_epoch)

    def lr_lambda(self, step):
        if step < self.warmup_steps:
            return float(step) / float(max(1.0, self.warmup_steps))
        # progress after warmup
        progress = float(step - self.warmup_steps) / float(max(1, self.t_total - self.warmup_steps))
        return max(0.0, 0.5 * (1. + math.cos(math.pi * float(self.cycles) * 2.0 * progress)))

def sgd(opt, modules):
    params = []
    for module in modules:
        params.append({'params': module.parameters()})
    optimizer = torch.optim.SGD(params,
                                lr=opt.optim.lr_init, momentum=opt.optim.momentum,
                                weight_decay=opt.optim.weight_decay, nesterov=True)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, opt.optim.lr_mul, gamma=opt.optim.lr_gamma)
    return (optimizer, scheduler)

def sgd_sam(opt, modules):
    params = []
    for module in modules:
        params.append({'params': module.parameters()})
    base_optimizer = torch.optim.SGD
    optimizer = SAM(params, base_optimizer,
                    lr=opt.optim.lr_init, momentum=opt.optim.momentum,
                    weight_decay=opt.optim.weight_decay, nesterov=True)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, opt.optim.lr_mul, gamma=opt.optim.lr_gamma)
    return (optimizer, scheduler)


import numpy as np
from scipy.optimize import minimize

def func(x): return (x[0] ** 2 + 3 * x[1] ** 2 - 3 * x[0] * x[1] - 6 * x[1])

def func_deriv(x):
    """ Derivative of objective function """
    dfdx0 = (2 * x[0] - 2 * x[1] + 4)
    dfdx1 = (-2 * x[0] + 3 * x[1] - 8)
    return np.array([dfdx0, dfdx1])

cons = ({'type': 'ineq', 'fun': lambda x: np.array([-x[0] - x[1] + 3]), 'jac': lambda x: np.array([-1.0, -1.0])},
        {'type': 'ineq', 'fun': lambda x: np.array([-x[0] + x[1] + 1]), 'jac': lambda x: np.array([-1.0, 1.0])},
        {'type': 'ineq', 'fun': lambda x: np.array([x[0]]), 'jac': lambda x: np.array([1.0, 0.0])},
        {'type': 'ineq', 'fun': lambda x: np.array([x[1]]), 'jac': lambda x: np.array([0.0, 1.0])})
res = minimize(func, [2.0, 4.0], jac=func_deriv, constraints=cons)
print(res)

print('---------------------------------------------------------------------------------------')

x0 = np.array([4, 3])

eps = 10 ** (-8)

def F(x):
    res = x[0] ** 2 + 3 * x[1] ** 2 - 3 * x[0] * x[1] + x[0] - 6 * x[1]
    return res

def grad_F(x):
    res1 = 2 * x[0] - 3 * x[1] + 1
    res2 = 6 * x[1] - 3 * x[0] - 6
    return np.array([res1, res2])

def a(x):
    return 2 * x[0] - 3 * x[1] + 1

def b(x):
    return 6 * x[1] - 3 * x[0] - 6

def find_lambda(x):
    lamb = (2 * a(x) * x[0] - 3 * a(x) * x[1] + a(x) - 3 * b(x) * x[0] + 6 * b(x) * x[1] - 6 * b(x)) / (2 * a(x) ** 2 - 6 * a(x) * b(x) + 6 * b(x) ** 2)
    return lamb

def start_gradient(_x0):
    x = _x0
    count = 0
    while (True):
        grad = grad_F(x)
        if (abs(grad[0]) < eps and abs(grad[1]) < eps):
            break
        s = (-1) * grad
        lamb = find_lambda(x)
        x_new = x + lamb * s
        x = x_new
        _F_new = F(x_new)
        count += 1
        print(x)
        print(_F_new)
        print(count)
    return

start_gradient(x0)

my_res = minimize(F, [1.0, 1.0], jac=grad_F)

print(my_res)


from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404
import logging
logger = logging.getLogger('pybo')

from ..models import Question

def index(request):
    # logger.info("INFO 레벨로 출력")
    '''
    pybo 목록 출력
    '''
    # 입력 파라미터
    page = request.GET.get('page', '1') # 페이지

    # 조회
    question_list = Question.objects.order_by('-create_date')

    # 페이지 정리
    paginator = Paginator(question_list, 10) # 페이지당 10개씩 보요주기
    page_obj = paginator.get_page(page)

    context = {'question_list': page_obj}
    return  render(request, 'pybo/question_list.html', context)

def detail(request, question_id):
    '''pybo 내용 출력 '''
    question = get_object_or_404(Question, pk=question_id)
    context = {'question': question}
    return render(request, 'pybo/question_detail.html', context)


import os
import re
import utils
import thulac
from tqdm import tqdm
import json

EMMISSION_COUNT = './result/emmission_count.json'
PY2HZ_T = './hmmPT/PY2HZ.json'

if __name__ == '__main__':
	with open(EMMISSION_COUNT) as f:
		emmission = json.load(f)

	py2hz = {}
	for hz in emmission:
		for py in emmission[hz]:
			py2hz.setdefault(py, set())
			py2hz[py].add(hz)

	for py in py2hz:
		py2hz[py] = list(py2hz[py])

	with open(PY2HZ_T,'w') as f:
		f.write(json.dumps(py2hz,indent=4,sort_keys=True))


# SPDX-License-Identifier: GPL-2.0-or-later

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QTabWidget, QWidget, QScrollArea, QApplication, QVBoxLayout
from PyQt5.QtGui import QPalette

from constants import KEYCODE_BTN_RATIO
from widgets.display_keyboard import DisplayKeyboard
from widgets.display_keyboard_defs import ansi_100, ansi_80, ansi_70, iso_100, iso_80, iso_70, mods, mods_narrow
from widgets.flowlayout import FlowLayout
from keycodes.keycodes import KEYCODES_BASIC, KEYCODES_ISO, KEYCODES_MACRO, KEYCODES_LAYERS, KEYCODES_QUANTUM, \
    KEYCODES_BOOT, KEYCODES_MODIFIERS, \
    KEYCODES_BACKLIGHT, KEYCODES_MEDIA, KEYCODES_SPECIAL, KEYCODES_SHIFTED, KEYCODES_USER, Keycode, \
    KEYCODES_TAP_DANCE, KEYCODES_MIDI, KEYCODES_BASIC_NUMPAD, KEYCODES_BASIC_NAV, KEYCODES_ISO_KR
from widgets.square_button import SquareButton
from util import tr, KeycodeDisplay

class AlternativeDisplay(QWidget):

    keycode_changed = pyqtSignal(str)

    def __init__(self, kbdef, keycodes, prefix_buttons):
        super().__init__()

        self.kb_display = None
        self.keycodes = keycodes
        self.buttons = []

        self.key_layout = FlowLayout()

        if prefix_buttons:
            for title, code in prefix_buttons:
                btn = SquareButton()
                btn.setRelSize(KEYCODE_BTN_RATIO)
                btn.setText(title)
                btn.clicked.connect(lambda st, k=code: self.keycode_changed.emit(title))
                self.key_layout.addWidget(btn)

        layout = QVBoxLayout()
        if kbdef:
            self.kb_display = DisplayKeyboard(kbdef)
            self.kb_display.keycode_changed.connect(self.keycode_changed)
            layout.addWidget(self.kb_display)
            layout.setAlignment(self.kb_display, Qt.AlignHCenter)
        layout.addLayout(self.key_layout)
        self.setLayout(layout)

    def recreate_buttons(self, keycode_filter):
        for btn in self.buttons:
            btn.deleteLater()
        self.buttons = []

        for keycode in self.keycodes:
            if not keycode_filter(keycode.qmk_id):
                continue
            btn = SquareButton()
            btn.setRelSize(KEYCODE_BTN_RATIO)
            btn.setToolTip(Keycode.tooltip(keycode.qmk_id))
            btn.clicked.connect(lambda st, k=keycode: self.keycode_changed.emit(k.qmk_id))
            btn.keycode = keycode
            self.key_layout.addWidget(btn)
            self.buttons.append(btn)

        self.relabel_buttons()

    def relabel_buttons(self):
        if self.kb_display:
            self.kb_display.relabel_buttons()

        KeycodeDisplay.relabel_buttons(self.buttons)

    def required_width(self):
        return self.kb_display.sizeHint().width() if self.kb_display else 0

    def has_buttons(self):
        return len(self.buttons) > 0

class Tab(QScrollArea):

    keycode_changed = pyqtSignal(str)

    def __init__(self, parent, label, alts, prefix_buttons=None):
        super().__init__(parent)

        self.label = label
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.alternatives = []
        for kb, keys in alts:
            alt = AlternativeDisplay(kb, keys, prefix_buttons)
            alt.keycode_changed.connect(self.keycode_changed)
            self.layout.addWidget(alt)
            self.alternatives.append(alt)

        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setWidgetResizable(True)

        w = QWidget()
        w.setLayout(self.layout)
        self.setWidget(w)

    def recreate_buttons(self, keycode_filter):
        for alt in self.alternatives:
            alt.recreate_buttons(keycode_filter)
        self.setVisible(self.has_buttons())

    def relabel_buttons(self):
        for alt in self.alternatives:
            alt.relabel_buttons()

    def has_buttons(self):
        for alt in self.alternatives:
            if alt.has_buttons():
                return True
        return False

    def select_alternative(self):
        # hide everything first
        for alt in self.alternatives:
            alt.hide()

        # then display first alternative which fits on screen w/o horizontal scroll
        for alt in self.alternatives:
            if self.width() - self.verticalScrollBar().width() > alt.required_width():
                alt.show()
                break

    def resizeEvent(self, evt):
        super().resizeEvent(evt)
        self.select_alternative()

class SimpleTab(Tab):

    def __init__(self, parent, label, keycodes):
        super().__init__(parent, label, [(None, keycodes)])

def keycode_filter_any(kc):
    return True

def keycode_filter_masked(kc):
    return Keycode.is_basic(kc)

class FilteredTabbedKeycodes(QTabWidget):

    keycode_changed = pyqtSignal(str)
    anykey = pyqtSignal()

    def __init__(self, parent=None, keycode_filter=keycode_filter_any):
        super().__init__(parent)

        self.keycode_filter = keycode_filter

        self.tabs = [
            Tab(self, "Basic", [
                (ansi_100, KEYCODES_SPECIAL + KEYCODES_SHIFTED),
                (ansi_80, KEYCODES_SPECIAL + KEYCODES_BASIC_NUMPAD + KEYCODES_SHIFTED),
                (ansi_70, KEYCODES_SPECIAL + KEYCODES_BASIC_NUMPAD + KEYCODES_BASIC_NAV + KEYCODES_SHIFTED),
                (None, KEYCODES_SPECIAL + KEYCODES_BASIC + KEYCODES_SHIFTED),
            ], prefix_buttons=[("Any", -1)]),
            Tab(self, "ISO/JIS", [
                (iso_100, KEYCODES_SPECIAL + KEYCODES_SHIFTED + KEYCODES_ISO_KR),
                (iso_80, KEYCODES_SPECIAL + KEYCODES_BASIC_NUMPAD + KEYCODES_SHIFTED + KEYCODES_ISO_KR),
                (iso_70, KEYCODES_SPECIAL + KEYCODES_BASIC_NUMPAD + KEYCODES_BASIC_NAV + KEYCODES_SHIFTED +
                 KEYCODES_ISO_KR),
                (None, KEYCODES_ISO),
            ], prefix_buttons=[("Any", -1)]),
            SimpleTab(self, "Layers", KEYCODES_LAYERS),
            Tab(self, "Quantum", [(mods, (KEYCODES_BOOT + KEYCODES_QUANTUM)),
                                  (mods_narrow, (KEYCODES_BOOT + KEYCODES_QUANTUM)),
                                  (None, (KEYCODES_BOOT + KEYCODES_MODIFIERS + KEYCODES_QUANTUM))]),
            SimpleTab(self, "Backlight", KEYCODES_BACKLIGHT),
            SimpleTab(self, "App, Media and Mouse", KEYCODES_MEDIA),
            SimpleTab(self, "MIDI", KEYCODES_MIDI),
            SimpleTab(self, "Tap Dance", KEYCODES_TAP_DANCE),
            SimpleTab(self, "User", KEYCODES_USER),
            SimpleTab(self, "Macro", KEYCODES_MACRO),
        ]

        for tab in self.tabs:
            tab.keycode_changed.connect(self.on_keycode_changed)

        self.recreate_keycode_buttons()
        KeycodeDisplay.notify_keymap_override(self)

    def on_keycode_changed(self, code):
        if code == "Any":
            self.anykey.emit()
        else:
            self.keycode_changed.emit(Keycode.normalize(code))

    def recreate_keycode_buttons(self):
        prev_tab = self.tabText(self.currentIndex()) if self.currentIndex() >= 0 else ""
        while self.count() > 0:
            self.removeTab(0)

        for tab in self.tabs:
            tab.recreate_buttons(self.keycode_filter)
            if tab.has_buttons():
                self.addTab(tab, tr("TabbedKeycodes", tab.label))
                if tab.label == prev_tab:
                    self.setCurrentIndex(self.count() - 1)

    def on_keymap_override(self):
        for tab in self.tabs:
            tab.relabel_buttons()

class TabbedKeycodes(QWidget):

    keycode_changed = pyqtSignal(str)
    anykey = pyqtSignal()

    def __init__(self):
        super().__init__()

        self.target = None
        self.is_tray = False

        self.layout = QVBoxLayout()

        self.all_keycodes = FilteredTabbedKeycodes()
        self.basic_keycodes = FilteredTabbedKeycodes(keycode_filter=keycode_filter_masked)
        for opt in [self.all_keycodes, self.basic_keycodes]:
            opt.keycode_changed.connect(self.keycode_changed)
            opt.anykey.connect(self.anykey)
            self.layout.addWidget(opt)

        self.setLayout(self.layout)
        self.set_keycode_filter(keycode_filter_any)

    @classmethod
    def set_tray(cls, tray):
        cls.tray = tray

    @classmethod
    def open_tray(cls, target, keycode_filter=None):
        cls.tray.set_keycode_filter(keycode_filter)
        cls.tray.show()
        if cls.tray.target is not None and cls.tray.target != target:
            cls.tray.target.deselect()
        cls.tray.target = target

    @classmethod
    def close_tray(cls):
        if cls.tray.target is not None:
            cls.tray.target.deselect()
        cls.tray.target = None
        cls.tray.hide()

    def make_tray(self):
        self.is_tray = True
        TabbedKeycodes.set_tray(self)

        self.keycode_changed.connect(self.on_tray_keycode_changed)
        self.anykey.connect(self.on_tray_anykey)

    def on_tray_keycode_changed(self, kc):
        if self.target is not None:
            self.target.on_keycode_changed(kc)

    def on_tray_anykey(self):
        if self.target is not None:
            self.target.on_anykey()

    def recreate_keycode_buttons(self):
        for opt in [self.all_keycodes, self.basic_keycodes]:
            opt.recreate_keycode_buttons()

    def set_keycode_filter(self, keycode_filter):
        if keycode_filter == keycode_filter_masked:
            self.all_keycodes.hide()
            self.basic_keycodes.show()
        else:
            self.all_keycodes.show()
            self.basic_keycodes.hide()


#!/usr/bin/python3

from pypresence import Presence
import time
import psutil
import os
import platform

if platform.system() != "Linux":
    print(
        "linux-roblox-discord-rpc is made for Linux (duh)! But it seems like you're running on a different OS."
    )
    print(
        "linux-roblox-discord-rpc will (probably) not work at all for you, and you'll have absolutely ZERO support for any problems that come up!"
    )
    print("Do you still want to continue? (y/n)")
    decision = input("")
    if decision == "n":
        os._exit(1)

start_time = time.time()
client_id = "1044302023211884624"
rpc = Presence(client_id)
rpc.connect()
other_process_name = "robloxplayerbet"
main_process_name = "robloxplayerbeta"

def start_presence():
    rpc.update(
        large_image="robloxicon",
        large_text="ROBLOX Icon",
        start=start_time,
    )

def if_roblox_running():
    for proc in psutil.process_iter():
        try:
            if main_process_name.lower() in proc.name().lower():
                return True
            elif other_process_name.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False

start_presence()

print("Started presence!")

while True:
    if if_roblox_running():
        start_presence()
    else:
        rpc.clear()
        start_time = time.time()  # reset the time elapsed field in rpc
    time.sleep(1)
    print("Reloaded presence!")


""" Генерация ФОС """
import os
import sys
import difflib
from typing import Dict, List

import pandas as pd
import numpy as np
from docx import Document
from docx.document import Document as DocumentType
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import CT_P, CT_Tbl
from docx.table import Table, _Cell
from docx.text.paragraph import Paragraph
from docxtpl import DocxTemplate

import core
from enigma import Competence, EducationPlan, Subject, get_plan
from enigma.eduction_plan import CT_EXAM, CT_COURSEWORK, CT_CREDIT, CT_CREDIT_GRADE

def iterate_items(parent):
    """ Обход параграфов и таблиц в документе """
    if isinstance(parent, DocumentType):
        parent_elem = parent.element.body
    elif isinstance(parent, _Cell):
        parent_elem = parent._tc
    else:
        raise ValueError('Oops')

    for child in parent_elem.iterchildren():
        if isinstance(child, CT_P):
            yield Paragraph(child, parent)
        elif isinstance(child, CT_Tbl):
            yield Table(child, parent)

def get_section_paragraphs(input_filename: str, start_kw: List[str], final_kw: List[str]) -> List[str]:
    """ Извлечь список абзацев текста из docx-файла """
    result, source = Document(), Document(input_filename)
    started = False
    paragraphs = []
    for item in iterate_items(source):
        if not started:
            if isinstance(item, Paragraph) and any(kw in item.text for kw in start_kw):
                started = True
        else:
            if isinstance(item, Paragraph) and any(kw in item.text for kw in final_kw):
                break
            if isinstance(item, Paragraph):
                text = item.text.strip()
                if text:
                    paragraphs.append(text + '\n')
            elif isinstance(item, Table):
                pass
    return paragraphs

def get_rpd(name):
    result = None
    for fn in os.listdir('rpds'):
        if fn.endswith('.docx'):
            if name in fn:
                result = os.path.join('rpds', fn)
                break
    return result

def check_args() -> None:
    """ Проверка аргументов командной строки """
    if len(sys.argv) != 3:
        print('Синтаксис:\n\tpython {0} <руп> <фос>'.format(*sys.argv))
        sys.exit()

def fill_table_1(template: DocxTemplate, context: Dict[str, any]) -> None:
    """ Заполнение таблиц с формами контроля """
    control_fancy_name = {
        CT_EXAM: 'Экзамен',
        CT_CREDIT_GRADE: 'Зачет с оценкой',
        CT_CREDIT: 'Зачет',
        CT_COURSEWORK: 'Курсовой проект',
    }

    plan: EducationPlan = context['plan']
    if plan.degree == core.BACHELOR:
        core.remove_table(template, 1)
    elif plan.degree == core.MASTER:
        core.remove_table(template, 2)
    table: Table = template.get_docx().tables[1]

    row_number = 0
    for competence in sorted(plan.competence_codes.values(), key=Competence.repr):
        core.add_table_rows(table, 1)
        row = len(table.rows) - 1
        row_number += 1
        core.set_cell_text(table, row, 0, core.CENTER, str(row_number))
        core.set_cell_text(table, row, 1, core.JUSTIFY, competence.code + ' ' + competence.description)
        table.cell(row, 1).merge(table.cell(row, len(table.columns) - 1))
        subjects = [plan.subject_codes[s] for s in competence.subjects]
        for subject in sorted(subjects, key=Subject.repr):
            core.add_table_rows(table, 1)
            row = len(table.rows) - 1
            row_number += 1
            core.set_cell_text(table, row, 0, core.CENTER, str(row_number))
            core.set_cell_text(table, row, 1, core.JUSTIFY, subject.code + ' ' + subject.name)
            for number, semester in subject.semesters.items():
                controls = [control_fancy_name[c] for c in semester.control]
                core.set_cell_text(table, row, number+1, core.CENTER, ', '.join(controls))

    core.fix_table_borders(table)

def table2list(x):
    """ Читает таблицу из РПД в список """
    ls = []
    try:
        for row in x.rows:
            for cell in row.cells:
                celltext = ''
                for paragraph in cell.paragraphs:
                    celltext += '\n' + paragraph.text
                ls.append(celltext)
        return [ls, len(x.rows), len(x.columns)]
    except IndexError:
        return [[' '], 1, 1]

cases = {
    'отл': 'Отлично',
    'хор': 'Хорошо',
    'неудов': 'неУдов',
    'удовл': 'Удовл',
    'не зач': 'не Зачтено',
    'незач': 'не Зачтено',
    'зач': 'Зачтено'
}

testtypes = {
    'the': 'Перевод, лексико-грамматический анализ, объем словарного запаса и аннотирование научной статьи',
    'экзамен': 'Экзаменационные билеты',
    'зачет': 'Задания к зачету'
}

def normalize(alist):
    thelist = []
    for item in alist:
        item = item.lower()
        for key, value in cases.items():
            if key in item:
                item = value
        if item not in cases.values():
            item = ' '
        thelist.append(item)
    return thelist

def findout_fos_part(documento):
    """ Выдергивает все абзацы между литературой и "Примерные контрольные задания" """
    text_at_start = ['Примерные контрольные задания (вопросы']
    text_at_end = ['учебной литературы', 'iprbook', 'lanbook', 'НБ СВФУ',
                   'информационно-телекоммуникационной сети']
    flag, result_text = False, ''
    for paragraph in documento.paragraphs:
        if any(key_word in paragraph.text for key_word in text_at_end):
            flag = False
        if flag:
            result_text += (paragraph.text+'\n')
        if text_at_start[0] in paragraph.text:
            flag = True
    result_text = result_text.replace('\n\n', '\n').replace('  ', ' ', 1000)
    return result_text

def find_rpd(subjectcode, subjectname, control, controls, sem):
    global fileslist
    filename = difflib.get_close_matches(subjectcode + ' ' + subjectname, fileslist)
    if len(filename) < 1:
        return [' ', ' ', ' ', ' ', ' ', ' ']
    filename = './rpds/' + str(filename[0]) + '.docx'
    rpd_doc = Document(filename)
    marks = []
    crirs = []
    zuv = ' '
    zuv_not_found, zuv_not_found2 = True, True
    for table in rpd_doc.tables:
        tablen = table2list(table)
        row, column = tablen[1], tablen[2]
        tablen = tablen[0]
        if len(tablen) < column:
            continue
        else:
            row = len(tablen) // column
            tablen = tablen[:(row*column)]
        df = pd.DataFrame(np.array(tablen).reshape(row, column))  # reshape to the table shape

        if sum([key_word in df.iloc[0, column-1] for key_word in ['Шкал', 'Оценк']]) > 0:
            marks += list(df[df.columns[-1]])
            crirs += list(df[df.columns[-2]])
        try:
            if zuv_not_found:
                for irow in range(3, 0, -1):
                    for icol in range(1, 3):
                        if '(ЗУВ)' in df.iloc[irow, icol]:
                            zuv += df.iloc[irow + 1, icol]
                            zuv_not_found = False

            if zuv_not_found2 and zuv_not_found:
                for irow in range(3, 0, -1):
                    for icol in range(1, 3):
                        if '.1.2.' in df.iloc[irow, icol]:
                            zuv += df.iloc[irow+1, icol]
                            zuv_not_found2 = False
        except IndexError:
            cndekc = 7

    marks = normalize(marks)
    crirs = [x for ind, x in enumerate(crirs) if marks[ind] != ' ']
    marks = [x for x in marks if x != ' ']
    excellent, good, fair, bad, tests = ' ', ' ', ' ', ' ', ' '
    if control == 'Зачет':
        for ind, criteria in enumerate(crirs):
            if marks[ind] == 'Зачтено': excellent += crirs[ind]
            if marks[ind] == 'не Зачтено': bad += crirs[ind]
    else:
        for ind, criteria in enumerate(crirs):
            if marks[ind] == 'Отлично': excellent += crirs[ind]
            if marks[ind] == 'Хорошо': good += crirs[ind]
            if marks[ind] == 'Удовл': fair += crirs[ind]
            if marks[ind] == 'не Удов': bad += crirs[ind]

    if bad == ' ': bad = excellent
    document = Document(filename)
    bigtext = findout_fos_part(document)
    bigtext = (control + bigtext).lower()
    testtype = ' '
    for key, value in testtypes.items():
        if key in bigtext:
            testtype = value
            continue

    return [zuv.replace('\n\n', '\n'), excellent, good, fair, bad, testtype]

def fill_table_2(template: DocxTemplate, context: Dict[str, any]) -> None:
    """ Заполнение таблиц с формами контроля """
    global fileslist
    control_fancy_name = {
        CT_EXAM: 'Экзамен',
        CT_CREDIT_GRADE: 'Зачет с оценкой',
        CT_CREDIT: 'Зачет',
        CT_COURSEWORK: 'Курсовой проект',
    }

    plan: EducationPlan = context['plan']
    table: Table = template.get_docx().tables[2]
    fileslist = [filename[:-5] for filename in os.listdir('./rpds') if filename.endswith('.docx')]

    row_number = 0
    for competence in sorted(plan.competence_codes.values(), key=Competence.repr):
        core.add_table_rows(table, 1)
        row = len(table.rows) - 1
        row_number += 1
        core.set_cell_text(table, row, 0, core.CENTER, str(row_number))
        core.set_cell_text(table, row, 1, core.JUSTIFY, competence.code + ' ' + competence.description)
        for runover in range(2, 8):
            core.set_cell_text(table, row, runover, core.JUSTIFY, ' ')
        # table.cell(row, 1).merge(table.cell(row, len(table.columns) - 1))
        subjects = [plan.subject_codes[s] for s in competence.subjects]
        for subject in sorted(subjects, key=Subject.repr):
            # core.add_table_rows(table, 1)
            # row = len(table.rows) - 1
            # len(table.rows) - 1
            # row_number += 1
            controls = []
            for number, semester in subject.semesters.items():
                controls += [control_fancy_name[c] for c in semester.control]

            sem = 0
            for control in controls:
                core.add_table_rows(table, 1)
                row = len(table.rows) - 1
                zuv_criteria = find_rpd(subject.code, subject.name, control, controls, sem)
                sem += 1
                if len(zuv_criteria) == 6:
                    core.set_cell_text(table, row, 0, core.CENTER, ' ')
                    core.set_cell_text(table, row, 1, core.JUSTIFY, subject.code + ' ' + subject.name)
                    core.set_cell_text(table, row, 2, core.JUSTIFY, zuv_criteria[0])
                    if control == 'Зачет':
                        core.set_cell_text(table, row, 3, core.JUSTIFY,  zuv_criteria[1])
                        core.set_cell_text(table, row, 4, core.JUSTIFY, ' ')
                        core.set_cell_text(table, row, 5, core.JUSTIFY, ' ')
                        core.set_cell_text(table, row, 6, core.JUSTIFY, zuv_criteria[4])
                        table.cell(row, 3).merge(table.cell(row, 5))
                    else:
                        core.set_cell_text(table, row, 3, core.JUSTIFY, zuv_criteria[1])
                        core.set_cell_text(table, row, 4, core.JUSTIFY, zuv_criteria[2])
                        core.set_cell_text(table, row, 5, core.JUSTIFY, zuv_criteria[3])
                        core.set_cell_text(table, row, 6, core.JUSTIFY, zuv_criteria[4])
                    core.set_cell_text(table, row, 7, core.JUSTIFY, zuv_criteria[5])

    core.fix_table_borders(table)

def fill_table_2_1(template: DocxTemplate, context: Dict[str, any]) -> None:
    """ Заполнение таблицы в разделе 2.1 """
    plan: EducationPlan = context['plan']
    table: Table = template.get_docx().tables[3]
    for subject in sorted(plan.subject_codes.values(), key=Subject.repr):
        core.add_table_rows(table, 1)
        row_index = len(table.rows) - 1
        core.set_cell_text(table, row_index, 0, core.CENTER, subject.code)
        core.set_cell_text(table, row_index, 1, core.JUSTIFY, subject.name)
    core.fix_table_borders(table)

header = [
    'Министерство науки и высшего образования Российской Федерации',
    'Федеральное государственное автономное образовательное учреждение',
    'высшего образования',
    '«СЕВЕРО-ВОСТОЧНЫЙ ФЕДЕРАЛЬНЫЙ УНИВЕРСИТЕТ ИМЕНИ М.К. АММОСОВА»',
    'Институт математики и информатики',
    '\n\n\n\n\n\n\n\n\n\n\n\nФонд оценочных средств дисциплины'
]

middle = [
    '\nкод дисциплины и название дисциплины',  # bold
    '\nдля программы ',
    'по направлению подготовки',
    'УГС Название направления подготовки — ',
    'Направленность программы: ',
    '\n\nФорма контроля:',  # bold
    'экзамен/зачет/зачет со оценкой/курсовая работа',  # bold
    '\n\n'
]

middleBold = [True, False, False, False, False, True, True, False]

footer = ['\n\n\n\n\n\n\n\n\n\n\n\n\nЯкутск 2019']

method_mater = [
    '6.3. Методические материалы, определяющие процедуры оценивания',
    'Текущий контроль успеваемости с БРС состоит из:',
    '– контрольные срезы, устанавливаемые учебными подразделениями;',
    '– рубежный срез, проводимый после окончания зачетной недели, за день до начала',
    'экзаменационной сессии, который определяет общую сумму баллов, набранную '
    'обучающимся в течение всего семестра по данной дисциплине.',
    'Промежуточная аттестация осуществляется в конце семестра после завершения учебных '
    'занятий в форме: зачетов, зачетов с оценкой, оценки отчета по практике, экзаменов.',
    'Оценка знаний, умений и навыков производится согласно положению о балльно-рейтинговой системе в СВФУ.'
]

table61 = [
    'Коды', 'оцениваемых', 'Индикаторы', 'достижения',
    'Показатель', 'оценивания', '(по', 'п.1.2.РПД)', 'Шкалы оценивания',
    'сформированности'
]

table62 = [
    'Оцениваемый', 'показатель', '(ЗУВ)', 'Тема', '(темы)',
    'Образец', 'типового', 'тестового', 'задания', '(вопроса)'
]

tableBooks = [
    'Автор', 'название', 'издания', 'издательство', 'литературы',
    'информационных ресурсов', 'гриф', 'НБ СВФУ', 'библиотека',
    'экземпляров', 'Электронные', 'ЭБС', 'ЭБ СВФУ'
]

def preceding_paragraph(fos_doc, x):
    """ Определяет тип таблицы и добавляет название параграфа """
    global table_flazhok
    if sum([word in x for word in table61]) > 5:
        title6 = '6. Фонд оценочных средств для проведения промежуточной аттестации обучающихся по дисциплине'
        fos_doc.add_paragraph(title6).bold = True
        fos_doc.paragraphs[-1].runs[0].bold = True
        fos_doc.add_paragraph('6.1. Показатели, критерии и шкала оценивания').bold = True
        fos_doc.paragraphs[-1].runs[0].bold = True
        table_flazhok = True
        return True
    if sum([word in x for word in table62]) > 5:
        fos_doc.add_paragraph('6.2. Примерные контрольные задания (вопросы) для промежуточной аттестации').bold = True
        fos_doc.paragraphs[-1].runs[0].bold = True
        table_flazhok = True
        return True
    if sum([word in x for word in tableBooks]) > 5:
        table_flazhok = False
    return table_flazhok

def findout_fos(documento):
    """ Выдергивает все абзацы между литературой и "Примерные контрольные задания" """
    text_at_start = ['Примерные контрольные задания (вопросы']
    text_at_end = ['учебной литературы', 'iprbook', 'lanbook', 'НБ СВФУ', 'информационно-телекоммуникационной сети']
    flag, result_text = False, ''
    for paragraph in documento.paragraphs:
        if any(key_word in paragraph.text for key_word in text_at_end):
            flag = False
        if flag:
            result_text += (paragraph.text + '\n')
        if text_at_start[0] in paragraph.text:
            flag = True
    result_text = result_text.replace('\n\n', '\n').replace('  ', ' ', 1000)
    return result_text

def list2docx(fos_doc, ls, row, column):
    """ Из списка восстанавливает таблицу, объединяя ячейки со совпадающими текстами """
    if len(ls) != row * column:
        return

    df = pd.DataFrame(np.array(ls).reshape(row, column))  # reshape to the table shape
    word_table = fos_doc.add_table(rows=row, cols=column, style='Table Grid')
    for ind_row in range(0, row, 1):
        for ind_col in range(0, column, 1):
            cell = word_table.cell(ind_row, ind_col)
            if ind_row > 0:
                cell2 = word_table.cell(ind_row-1, ind_col)
                if cell2.text == df.iloc[ind_row, ind_col]:
                    cell.text = ''
                    cell.merge(cell2)
                else:
                    cell.text = df.iloc[ind_row, ind_col]
            else:
                if ind_col > 0:
                    cell2 = word_table.cell(ind_row, ind_col-1)
                    if cell2.text == df.iloc[ind_row, ind_col]:
                        cell.text = ''
                        cell.merge(cell2)
                    else:
                        cell.text = df.iloc[ind_row, ind_col]
    fos_doc.add_paragraph(' ')
    return

def fill_section_2_2(template: DocxTemplate, context: Dict[str, any]) -> None:
    global table_flazhok
    control_fancy_name = {
        CT_EXAM: 'Экзамен',
        CT_CREDIT_GRADE: 'Зачет с оценкой',
        CT_CREDIT: 'Зачет',
        CT_COURSEWORK: 'Курсовой проект',
    }

    """ Заполнение раздела 2.2 """
    # marker = None
    # for p1 in template.get_docx().paragraphs:
    #     keywords = ['оценочные средства для', 'государственной итоговой аттестации']
    #     if all(kw in p1.text.lower() for kw in keywords):
    #         marker = p1
    #         break
    global fileslist
    fileslist = [filename[:-5] for filename in os.listdir('./rpds') if filename[-4:] == 'docx']

    plan: EducationPlan = context['plan']
    middle[1] += 'магистратуры' if plan.degree == core.MASTER else 'бакалавриата'
    middle[4] += plan.program
    middle[3] = plan.code + ' ' + plan.name
    subjects = sorted(plan.subject_codes.values(), key=Subject.repr)
    for s in subjects:
        rpd = difflib.get_close_matches(s.code + ' ' + s.name, fileslist)
        if len(rpd) < 1:
            continue
        rpd = './rpds/' + str(rpd[0]) + '.docx'
        rpd_doc = Document(rpd)

        # титульная страница
        document = template.get_docx()
        document.add_page_break()
        document.add_paragraph('\n'.join(header))
        document.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
        controls = []
        for number, semester in s.semesters.items():
            controls += [control_fancy_name[c] for c in semester.control]
        middle[0] = s.code + ' ' + s.name
        middle[6] = ' / '.join(controls)
        for ind, line in enumerate(middle):
            document.add_paragraph(line)
            document.paragraphs[-1].runs[0].bold = middleBold[ind]
            document.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
        document.add_paragraph('\n'.join(footer))
        document.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
        document.add_page_break()

        ''' флажок, чтобы забрать все таблицы между двумя группами ключевых слов '''
        table_flazhok = False
        for table in rpd_doc.tables:
            tablen = table2list(table)
            if preceding_paragraph(document, ' '.join(tablen[0][:6])):
                list2docx(document, *tablen)
        ''' таблицы все включены, теперь забираем все абзацы '''
        text_heap = findout_fos(rpd_doc)
        document.add_paragraph(text_heap)
        if 'Методические материалы, определяющие' not in text_heap:
            document.add_paragraph('\n'.join(method_mater))

        # p2 = marker.insert_paragraph_before('%s %s' % (s.code, s.name))
        # p2.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
        # p2.paragraph_format.first_line_indent = Cm(0)
        # for r in p2.runs:
        #     r.bold = True

        # for item in iterate_items(Document(rpd)):
        #     if isinstance(item, Paragraph):
        #         p3 = marker.insert_paragraph_before(item.text)
        #         p3.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
        #         p3.paragraph_format.first_line_indent = Cm(0)

    # plan: core.EducationPlan = context['plan']
    # table: Table = document.get_docx().tables[3]
    # for subject in sorted(plan.subject_codes.values(), key=core.Subject.repr):
    #     core.add_table_rows(table, 1)
    #     row_index = len(table.rows) - 1
    #     core.set_cell_text(table, row_index, 0, core.CENTER, subject.code)
    #     core.set_cell_text(table, row_index, 1, core.JUSTIFY, subject.name)

def fill_table_4(template: DocxTemplate, context: Dict[str, any]) -> None:
    """ Заполнение бланка "Лист сформированности компетенций" """
    plan: EducationPlan = context['plan']
    table: Table = template.get_docx().tables[-1]
    row_number = 0
    for competence in sorted(plan.competence_codes.values(), key=Competence.repr):
        core.add_table_rows(table, 1)
        row_index = len(table.rows) - 1
        row_number += 1
        core.set_cell_text(table, row_index, 0, core.CENTER, str(row_number))
        core.set_cell_text(table, row_index, 1, core.JUSTIFY, competence.code + ' ' + competence.description)
        subjects = [plan.subject_codes[s] for s in competence.subjects]
        for subject in sorted(subjects, key=Subject.repr):
            core.add_table_rows(table, 1)
            row_index = len(table.rows) - 1
            core.set_cell_text(table, row_index, 1, core.JUSTIFY, subject.code + ' ' + subject.name)

    core.add_table_rows(table, 1)
    row_number += 1
    row_index = len(table.rows) - 1
    core.set_cell_text(table, row_index, 0, core.CENTER, str(row_number))
    core.set_cell_text(table, row_index, 1, core.JUSTIFY, 'Практики')

    core.add_table_rows(table, 1)
    row_number += 1
    row_index = len(table.rows) - 1
    core.set_cell_text(table, row_index, 0, core.CENTER, str(row_number))
    core.set_cell_text(table, row_index, 1, core.JUSTIFY, 'НИР')

    core.fix_table_borders(table)

def main() -> None:
    """ Точка входа """
    check_args()
    global fileslist

    plan = get_plan(sys.argv[1])
    template = core.get_template('fos.docx')
    context = {
        'plan': plan,
    }
    fill_table_1(template, context)
    fileslist = ' '
    fill_table_2(template, context)
    fill_table_2_1(template, context)
    fill_table_4(template, context)
    fill_section_2_2(template, context)
    template.render(context)
    template.save('filled_'+sys.argv[2])
    print('Partially done')

if __name__ == '__main__':
    table_flazhok = False
    fileslist = ' '
    main()


import re
from pymongo import MongoClient
import string

client = MongoClient()
db = client.twitterproj

coll_list = ["tweetsMAGA", "tweetsNMP"]

bad_chars = [u'\u2019', u"\u22EF", "'", u"\u203C", "\"\"", "\"", u"\u2026", "#", u"\u000A", u"\u201C", u"\u201D", u"\u0009"]

#REMOVE RTs
# for tweet in coll.find({}):
# 	searchObj = re.search(r' RT ', tweet["text"])
# 	if searchObj:
# 		coll.remove({"id":tweet["id"]})

for collstr in coll_list:
	tweets_coll = db[collstr]
	count = 0
	for tweet in tweets_coll.find({}):
		text = tweet['text']
		#get rid of unicode
		text = text.encode('ascii','ignore').decode('utf-8')

		#get rid of emojis
		emojis = re.compile(u"[\U0001F600-\U0001F64F] | [\U0001F300-\U0001F5FF] | [\U0001F680-\U0001F6FF] | [\U0001F1E0-\U0001F1FF]", re.VERBOSE)
		text = re.sub(emojis, '', text)

		text = re.sub(r'@\S+', '', text)	#remove usernames
		#get rid of punctuation
		for c in string.punctuation:
			text = text.replace(c,"")
		for un in bad_chars:
			text = text.replace(un,"")

		text = text.lower()		#convert to lowercase
		text = text.replace("notmypresident", "")	#remove NMP and MAGA hashtags
		text = text.replace("maga", "")
		text = re.sub(r'http\S+', '', text)	#remove links
		text = re.sub(r' +',' ',text)	#remove double spaces

		#print(str(tweet["id"]))
		#print(text)
		result = tweets_coll.update_one({"id":tweet["id"]}, {'$set':{"text_clean":text}}, upsert=False)
		count += result.modified_count
	print(str(count))


from logging import getLogger

logger = getLogger(__name__)

"""
用語整理
「カテゴリ１/カテゴリ２/ページ」：ページ名(page_name)。画面に表示されるページ名
「カテゴリ１_._カテゴリ２_._ページ」：ファイル名(file_name)。サーバ上に保存されるファイル名。
[カテゴリ１,カテゴリ２,ページ]：ページディレクトリ(page_dirs)。配列状態でページを特定するももの。
"""

def page_name_to_file_name(page_name):
    """
    「カテゴリ１/カテゴリ２/ページ」のように表示上のページ名の文字列を
    「カテゴリ１_._カテゴリ２_._ページ」というファイル名形式に変換する。
    :param page_name:
    :return:
    """
    # 隠し仕様
    # ページ名に「_._」が使われている場合、自動的にディレクトリ分解点として扱う。
    file_name = page_name.replace('_._', "/")
    # ページ名に「>」が使われている場合、自動的にディレクトリ分解点として扱う。
    file_name = page_name.replace('>', "/")
    page_dirs = file_name.split("/")

    return page_dirs_to_file_name(page_dirs)

def file_name_to_page_name(page_name):
    """
    「カテゴリ１_._カテゴリ２_._ページ」というファイル名形式の文字列を
    「カテゴリ１/カテゴリ２/ページ」の表示上のページ名に変換する。
    :param page_name:
    :return:
    """
    page_name = page_name.replace('_._', "/")

    return page_name

def page_dirs_to_file_name(page_dirs):
    """
    [カテゴリ１,カテゴリ２,ページ]というディレクトリの配列状態になっているページパスを
    「カテゴリ１_._カテゴリ２_._ページ」というファイル名形式に変換する。
    :param page_dirs:
    :return:
    """
    file_name = ""
    for page_dir in page_dirs:
        if page_dir:
            file_name = file_name + page_dir.strip() + '_._'

    file_name = file_name[0:-len('_._')]
    file_name = _replace_windows_ng_word(file_name)

    return file_name

def page_dirs_to_page_name(page_dirs):
    """
    [カテゴリ１,カテゴリ２,ページ]というディレクトリの配列状態になっているページパスを
    「カテゴリ１/カテゴリ２/ページ」というファイル名に変換する。
    :param page_dirs:
    :return:
    """
    page_name = ""
    for page_dir in page_dirs:
        if page_dir:
            page_name = page_name + page_dir + "/"

    page_name = page_name[0:-1]
    page_name = _replace_windows_ng_word(page_name)

    return page_name

def to_page_dirs(page_name):
    """
    「カテゴリ１/カテゴリ２/ページ」のように表示上のページ名の文字列、もしくは
    「カテゴリ１_._カテゴリ２_._ページ」というファイル名形式を、
    [カテゴリ１,カテゴリ２,ページ]というディレクトリの配列状態に変換する。
    :param page_name:
    :return:
    """
    page_name = _replace_windows_ng_word(page_name)
    page_name = page_name.replace('_._', "/")
    page_name = page_name.replace('>', "/")

    page_dirs = page_name.split("/")
    return page_dirs

def _replace_windows_ng_word(str):
    """
    隠し仕様
    Windows環境のファイル名として使用出来ない文字を全角に置換する
    :param str:
    :return:
    """
    str = str.replace('\\', "￥")
    str = str.replace(':', "：")
    str = str.replace('\"', ">")
    str = str.replace('*', "＊")
    str = str.replace('?', "？")
    str = str.replace('\"', "”")
    str = str.replace('|', "｜")
    str = str.replace('<', "＜")

    return str


import datetime

from django.shortcuts import render

def home(request):
    request.session["django_timezone"] = "Asia/Seoul"
    now = datetime.datetime(2023, 1, 1, 0, 0, 0, 0, datetime.timezone.utc)
    number = 1231231231.121
    context = {
        'now': now,
        'number': number,
    }
    return render(request, 'myapp/home.html', context)


from django.urls import path
from . import views

app_name = 'schedules'
urlpatterns = [
    path('', views.intro, name='intro' ),
    path('main', views.index, name='index' ),
    path('main2', views.index2, name='index2' ),
    path('carousel', views.carousel, name='carousel'),
    path('introduce', views.introduce, name='introduce'),
]


# Author: Frank Shang
# Date: 06/24/2021
# Description: This program uses the import statistics to find the mean, median, and mode of a list of student objects.

import statistics

class Student:
  '''student class'''

  def __init__(self, name, grade):
    '''initializes all data members'''
    self._name = name
    self._grade = grade

  def get_grade(self):
    '''method that returns the grade of the student'''
    return self._grade

def basic_stats(students):
    '''function that uses the statistics module to find the mean, median, and mode of the incoming parameter list'''
    list_grades = []
    for student in students:
    #print(student.get_grade())
        list_grades.append(student.get_grade())
    print(list_grades)
    mean = statistics.mean(list_grades)
    median = statistics.median(list_grades)
    mode = statistics.mode(list_grades)
    tuple_stats = (mean, median, mode)
    return tuple_stats

#s1 = Student("Kyoungmin", 73)
#s2 = Student("Mercedes", 74)
#s3 = Student("Avanika", 78)
#s4 = Student("Marta", 74)

#student_list = [s1, s2, s3, s4]
#print(basic_stats(student_list))


import numpy as np
import matplotlib.pyplot as plt
from project1.chap2.adaptiveRK34 import adaptiveRK34

def vanderpol(t, u):
    return np.array([[u[0][1], 100 * (1 - u[0][0] ** 2) * u[0][1] - u[0][0]]])

# testing
# a = np.array([[1, 3]])
# print(a)
# print(a[0])
# print(a ** 2)  # element-wise!!

# set up test
t0 = 0
tf = 200
tol = 10 ** -6
y0 = np.array([[2, 0]])
sol = adaptiveRK34(vanderpol, y0, t0, tf, tol)

time = sol[0]
ys = sol[1]
y1 = []
y2 = []

for i in range(len(time)):
    y1.append(ys[i][0][0])
    y2.append(ys[i][0][1])

plt.subplot(121)
plt.plot(time, y2)
plt.subplot(122)
plt.plot(y1, y2)
plt.show()


"""REST client handling, including JQuantsStream base class."""

from __future__ import annotations

import sys
import typing as t
from pathlib import Path
from typing import Any, Callable, Iterable

import requests
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.streams import RESTStream

from tap_jquants.auth import JQuantsAuthenticator
from tap_jquants.pagination import JQuantsDatePaginator

if sys.version_info >= (3, 8):
    from functools import cached_property
else:
    from cached_property import cached_property

from .helpers import convert_json

if t.TYPE_CHECKING:
    from singer_sdk.pagination import BaseAPIPaginator

_Auth = Callable[[requests.PreparedRequest], requests.PreparedRequest]
SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")

class JQuantsStream(RESTStream):
    """JQuants stream class."""

    @property
    def url_base(self) -> str:
        """Return the API URL root, configurable via tap settings."""
        return "https://api.jquants.com/v1"

    next_page_token_jsonpath = "$.pagination_key"  # noqa: S105

    @cached_property
    def authenticator(self) -> _Auth:
        """Return a new authenticator object.

        Returns:
            An authenticator instance.
        """
        return JQuantsAuthenticator.create_for_stream(self)

    @property
    def http_headers(self) -> dict:
        """Return the http headers needed.

        Returns:
            A dictionary of HTTP headers.
        """
        headers = {}
        if "user_agent" in self.config:
            headers["User-Agent"] = self.config.get("user_agent")
        return headers

    def get_url_params(
        self,
        _context: dict | None,
        next_page_token: Any | None,
    ) -> dict[str, Any]:
        """Return a dictionary of values to be used in URL parameterization.

        Args:
            context: The stream context.
            next_page_token: The next page index or value.

        Returns:
            A dictionary of URL query parameters.
        """
        params: dict = {}
        if next_page_token:
            params["pagination_key"] = next_page_token
        return params

    def parse_response(self, response: requests.Response) -> Iterable[dict]:
        """Parse the response and return an iterator of result records.

        Args:
            response: The HTTP ``requests.Response`` object.

        Yields:
            Each record from the source.
        """
        yield from extract_jsonpath(
            self.records_jsonpath,
            input=convert_json(response.json()),
        )

class JQuantsDateStream(JQuantsStream):
    """JQuants incremental stream class based on date."""

    def get_new_paginator(self) -> BaseAPIPaginator:
        """Return a new paginator object."""
        return JQuantsDatePaginator()

    def get_url_params(
        self,
        context: dict | None,
        next_page_token: Any | None,
    ) -> dict[str, Any]:
        """Return a dictionary of parameters to use in the URL."""
        params: dict = {}
        if next_page_token is not None:
            date_key, pagination_key = next_page_token
            if pagination_key:
                params["pagination_key"] = next_page_token
            params["date"] = date_key
        else:
            starting_date = self.get_starting_timestamp(context)
            if starting_date:
                params["date"] = starting_date.strftime("%Y-%m-%d")
        self.logger.info("URL params: %s", params)
        return params

class JQuantsFromStream(JQuantsStream):
    """JQuants incremental stream class based on from."""

    def get_url_params(
        self,
        context: dict | None,
        next_page_token: Any | None,
    ) -> dict[str, Any]:
        """Return a dictionary of parameters to use in the URL."""
        params: dict = {}
        if next_page_token is not None:
            pagination_key = next_page_token
            if pagination_key:
                params["pagination_key"] = next_page_token
        starting_date = self.get_starting_timestamp(context)
        if starting_date:
            params["from"] = starting_date.strftime("%Y-%m-%d")
        self.logger.info("URL params: %s", params)
        return params


'''
try cobrapy on wc-lang model

algorithm:
x use wc-lang to read model
dFBA:
    save concentrations to history
    x use wc-lang to write FBA submodel to SBML file
    x use cobrapy to read SBML file
    solve FBA submodel
    update concentrations after time interval
plot history
'''
from argparse import Namespace
from os import path
import tempfile
import cobra
import warnings
warnings.filterwarnings('ignore', message='', module='wc_lang.prepare')
from libsbml import writeSBMLToFile

from wc_lang.io import Reader
import wc_lang.sbml.io as sbml_io
from obj_model import utils
from wc_lang.prepare import PrepareModel

args = Namespace(end_time=1,
    time_step=1,
    test_submodel_id='Metabolism',
    wc_lang_model="example-model.xlsx")

MODEL_FILENAME = path.join(path.dirname(__file__), '../../..', 'wc_lang/tests/fixtures',
    args.wc_lang_model)
print('using:', MODEL_FILENAME)
model = Reader().run(MODEL_FILENAME)
PrepareModel(model).run()
submodel = utils.get_component_by_id(model.get_submodels(), args.test_submodel_id)
_, SBMLfile = tempfile.mkstemp()

def try_cobrapy(args):
    time = 0
    while time < args.end_time:
        print('time:', time)
        sbml_document = sbml_io.SBMLExchange.write_submodel(submodel)
        if not writeSBMLToFile(sbml_document, SBMLfile):
            raise ValueError("SBML document for submodel '{}' could not be written to '{}'.".format(
                args.test_submodel_id, SBMLfile))
        # cobra_model = cobra.io.read_sbml_model(SBMLfile)
        cobra_model = cobra.io.sbml3.read_sbml_model(SBMLfile)
        print('cobra_model', cobra_model)
        print('cobra_model', cobra_model._repr_html_())
        solution = cobra_model.optimize()
        print(solution)

        time += args.time_step

try_cobrapy(args)


# !/bin/env python3

import requests, json, csv, pandas
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

rrurl = 'https://reqres.in/api/users'
results_per_page = 5

#to check for pagination
def make_request_url(results_per_page, current_page_number):
    return (
        "https://reqres.in/api/users"
        + "?per_page="
        + str(results_per_page)
        + "&page="
        + str(current_page_number)
    )

def get_result(current_page_number):
    with requests.session() as s:
        url = make_request_url(results_per_page, current_page_number)
        resp = s.get(url, verify=False)

    if resp.status_code != 200:
        raise Exception(resp.status_code)

    raw = resp.json()
    data = raw.get("data")  #list

    if (raw["page"] == raw["total_pages"]):
         return data

    return data + get_result(current_page_number + 1)  # concat lists

# def puttocsv(datablock):
#     with open("output.csv","w",newline="") as f:
#         title = "id,email,first_name,last_name,avatar".split(",") # quick hack
#         cw = csv.DictWriter(f,title,delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
#         cw.writeheader()
#         cw.writerows(datablock)

def pandastohtml(datablock):
    df = pandas.DataFrame(datablock)
    with open('mbchal.html', 'w') as f:
        df.to_html(buf=f,escape=False)

def main():
    datablock = get_result(1) #list of jsons
    for block in datablock:
        block['avatar'] = '<img src="' + block['avatar'] + '" width="60" >'
    pandastohtml(datablock)

    # Then you can print the data or save it to a file

if __name__ == "__main__":
    # Now run the driver function
    main()


import logging
import os
from dataclasses import dataclass
from typing import List, Any

import pandas as pd
from dataclasses_json import dataclass_json
from filelock import FileLock

from task_management.task import Task, TaskPayload
from task_management.tasks_factory import TasksFactory

@dataclass_json
@dataclass
class ComputeMeanPayload(TaskPayload):
    file_paths: List[str]
    result_file: str

@TasksFactory.register(task_type="compute_mean", name="Mean Compute Task")
class ComputeMeanTask(Task):
    def __init__(self, payload: Any):
        if isinstance(payload, dict):
            payload = ComputeMeanPayload.from_dict(payload)
        self.file_paths = payload.file_paths
        self.result_file_path = payload.result_file
        self.lock_file_path = f"{payload.result_file}.{payload.task_id}.lock"
        super().__init__(payload)

    def validate(self):
        if len(self.file_paths) > 5:
            raise ValueError("This function can only process up to 5 files at a time.")

    def compute_mean(self):
        """
        Computing mean is divided in two parts
        - intermediate mean
        - overall mean
        Each worker saves their individual mean, and the final mean is consolidated at the end of a task

        This way a subtask need not be aware how many other subtasks are to be computed.
        """
        dfs = [
            pd.read_csv(file_path, header=None) for file_path in self.file_paths
        ]
        # Concatenate all dataframes along the column axis
        c_df = pd.concat(dfs, axis=1)

        # Compute the sum along the column axis
        sum_values = c_df.sum(axis=1)
        return pd.concat([sum_values, pd.Series(len(dfs))], ignore_index=True)

    def save_results(self, result):
        # Path to the intermediate file
        intermediate_file_path = f"{self.result_file_path}.{self.task_id}.intermediate.csv"

        with FileLock(self.lock_file_path):
            # If the intermediate file already exists, read the existing totals
            if os.path.exists(intermediate_file_path):
                existing_totals_df = pd.read_csv(intermediate_file_path, header=None)
                existing_totals_series = existing_totals_df.squeeze()
                # Add the new sum values to the existing totals
                new_totals = existing_totals_series + result
            else:
                # If the intermediate file does not exist, it will be created
                new_totals = result

            # Write the new totals to the intermediate file
            new_totals.to_csv(intermediate_file_path, index=False, header=False)

            # Compute the final results and save it to results file
            totals_df = pd.read_csv(intermediate_file_path, header=None)
            mean_df = totals_df.iloc[:-1] / totals_df.iloc[-1]
            mean_df.to_csv(self.result_file_path, index=False, header=False)

    def process(self):
        try:
            logging.info(f"Started processing task: {self.task_id} subtask: {self.subtask_id}")
            state="INPROGRESS"
            self.update_state(state=state)
            result = self.compute_mean()
            self.save_results(result)
            state="COMPLETED"
            logging.info(f"Finished processing task: {self.task_id} subtask: {self.subtask_id}")
        except Exception as ex:
            logging.exception(f"Error processing task: {self.task_id} subtask: {self.subtask_id} {ex}")
            state = "FAILURE"
            raise
        finally:
            self.update_state(state=state)


import numpy as np
from scipy import misc
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import sys

def read_data():
	X = np.empty((100, 64*64))
	for i in range(100):
		file_name = chr(65 + int(i/10))
		file_name = file_name +  '{0:0=2d}'.format(i%10) + '.bmp'
		img = misc.imread('faceExpressionDatabase/' + file_name)
		flat = np.ravel(img)
		X[i] = flat
	return X

X = read_data()
X_mean = X.mean(axis=0, keepdims=True)
X_ctr = X - X_mean
u, s, v = np.linalg.svd(X_ctr)

first_ten = v[:9]

fig = plt.figure(figsize=(5,4))

for i in range(9):
	ax = fig.add_subplot(3, 3, i+1)
	ax.imshow(np.reshape(first_ten[i], (64,64)), cmap='gray')
	plt.xticks(np.array([]))
	plt.yticks(np.array([]))
	plt.tight_layout()
fig.savefig('eigenfaces.png')

# top_five = v[:5]
# x_reduced = np.dot((X - X_mean), top_five.T)
# x_recover = X_mean + np.dot(x_reduced ,top_five)
# fig = plt.figure(figsize=(10,8))
# for i in range(100):
# 	ax = fig.add_subplot(10, 10, i+1)
# 	ax.imshow(np.reshape(x_recover[i], (64,64)), cmap='gray')
# 	plt.xticks(np.array([]))
# 	plt.yticks(np.array([]))
# 	plt.tight_layout()
# fig.savefig('recoveredFaces.png')

# fig = plt.figure(figsize=(10,8))
# for i in range(100):
# 	ax = fig.add_subplot(10, 10, i+1)
# 	ax.imshow(np.reshape(X[i], (64,64)), cmap='gray')
# 	plt.xticks(np.array([]))
# 	plt.yticks(np.array([]))
# 	plt.tight_layout()
# fig.savefig('originFaces.png')

for i in range(100):
	top_k = v[:i+1]
	print("top " + str(i+1))
	x_reduced = np.dot((X - X_mean), top_k.T)
	x_recover = X_mean + np.dot(x_reduced, top_k)
	l = (X - x_recover)
	l = l * l
	E = np.sqrt(np.sum(l) / 409600)  / 256
	print(E)
	if E < 0.01 :
		print(i+1)
		break


import os
from typing import List, Optional

from .. import SessionItem, register_session_item
from ...logging import DEBUG, log

__all__: List[str] = []

class WorkDir:
    """Stores the current working directory"""

    def __init__(self, root: Optional[str] = None, subpath: str = os.curdir):
        if root is None:
            root = os.getcwd()
        self.root = root
        self.subpath = subpath

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, WorkDir):
            raise NotImplementedError()
        return other.full_path == self.full_path

    @property
    def root(self) -> str:
        """The root file path"""
        return self.__root

    @root.setter
    def root(self, root: str):
        """Setter for the root file path"""
        root = os.path.expanduser(root)
        root = os.path.expandvars(root)
        root = os.path.abspath(root)
        root = os.path.normcase(root)
        root = os.path.normpath(root)
        self.__root = root

    @property
    def subpath(self) -> str:
        """The currently active subdirectory"""
        return self.__subpath

    @subpath.setter
    def subpath(self, subpath: str):
        """Setter for the currently active subdirectory"""
        subpath = os.path.expandvars(subpath)
        subpath = os.path.normcase(subpath)
        subpath = os.path.normpath(subpath)
        self.__subpath = subpath

    @property
    def full_path(self) -> str:
        """The full path (:func:`root` + :func:`subpath`)"""
        return os.path.join(self.root, self.subpath)

__DEFAULT_WORKDIR = WorkDir()
__CUR_WORKDIR = __DEFAULT_WORKDIR

def _work_dir_setter(work_dir: WorkDir) -> None:
    # pylint: disable=global-statement
    global __CUR_WORKDIR
    if work_dir != __CUR_WORKDIR:
        log(DEBUG, "Changing working directory to: %s", work_dir.full_path)
        if not os.path.exists(work_dir.full_path):
            os.makedirs(work_dir.full_path, exist_ok=True)
        os.chdir(work_dir.full_path)
        __CUR_WORKDIR = work_dir

register_session_item(
    "work_dir",
    SessionItem(setter=_work_dir_setter, default=WorkDir(), persistent=False),
)


#!/usr/bin/env python
"""
Reproduce the artificial example described in the paper [Zellinger2017a]

The central moment discrepancy (CMD) is used for domain adaptation
as first described in the preliminary conference paper [Zellinger2017b].
It is implemented as keras objective function.
This implementation uses keras 1.1.0.

[Zellinger2017a] W. Zellinger, B.A. Moser, T. Grubinger, E. Lughofer,
T. Natschlaeger, and S. Saminger-Platz, "Robust unsupervised domain adaptation
for neural networks via moment alignment," arXiv preprint arXiv:1711.06114, 2017
[Zellinger2017b] W.Zellinger, T. Grubinger, E. Lughofer, T. Ntschlaeger,
and Susanne Saminger-Platz, "Central moment discrepancy (cmd) for
domain-invariant representation learning," International Conference on Learning
Representations (ICLR), 2017

__author__ = "Werner Zellinger"
__copyright__ = "Copyright 2017, Werner Zellinger"
__credits__ = ["Thomas Grubinger, Robert Pollak"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Werner Zellinger"
__email__ = "werner.zellinger@jku.at"
"""

from __future__ import print_function

import matplotlib
matplotlib.use('PS')
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
np.random.seed(0)
from scipy import stats
from keras.layers import Dense, Input, merge
from keras.models import Model
from keras.optimizers import Adadelta
from keras import backend as K

plt.close('all')

N_HIDDEN_NODES = 15
N_MOMENTS = 5
N_CLASSES = 3
DATA_FOLDER = 'data/artificial_dataset/'
TMP_FOLDER = 'temp/artificial_example/'
OUTPUT_FOLDER = 'output/artificial_example/'

def cmd(labels, y_pred):
    """
    central moment discrepancy (cmd)
    objective function for keras models (theano or tensorflow backend)

    - Zellinger, Werner, et al. "Robust unsupervised domain adaptation for
    neural networks via moment alignment.", TODO
    - Zellinger, Werner, et al. "Central moment discrepancy (CMD) for
    domain-invariant representation learning.", ICLR, 2017.
    """
    x1 = y_pred[:,:N_HIDDEN_NODES]
    x2 = y_pred[:,N_HIDDEN_NODES:]
    mx1 = x1.mean(0)
    mx2 = x2.mean(0)
    sx1 = x1 - mx1
    sx2 = x2 - mx2
    dm = l2diff(mx1,mx2)
    scms = dm
    for i in range(N_MOMENTS-1):
        # moment diff of centralized samples
        scms+=moment_diff(sx1,sx2,i+2)
    return scms

def l2diff(x1, x2):
    """
    standard euclidean norm
    """
    return ((x1-x2)**2).sum().sqrt()

def moment_diff(sx1, sx2, k):
    """
    difference between moments
    """
    ss1 = (sx1**K.cast(k,'int32')).mean(0)
    ss2 = (sx2**K.cast(k,'int32')).mean(0)
    return l2diff(ss1,ss2)

def neural_network(domain_adaptation=False):
    """
    moment alignment neural network (MANN)

    - Zellinger, Werner, et al. "Robust unsupervised domain adaptation for
    neural networks via moment alignment.", arXiv preprint arXiv:1711.06114, 2017
    """
    # layer definition
    input_s = Input(shape=(2,), name='souce_input')
    input_t = Input(shape=(2,), name='target_input')
    encoding = Dense(N_HIDDEN_NODES,
                     activation='sigmoid',
                     name='hidden')
    prediction = Dense(N_CLASSES,
                       activation='softmax',
                       name='pred')
    # network architecture
    encoded_s = encoding(input_s)
    encoded_t = encoding(input_t)
    pred_s = prediction(encoded_s)
    pred_t = prediction(encoded_t)
    dense_s_t = merge([encoded_s,encoded_t], mode='concat', concat_axis=1)
    # input/output definition
    nn = Model(input=[input_s,input_t],
               output=[pred_s,pred_t,dense_s_t])
    # seperate model for activation visualization
    visualize_model = Model(input=[input_s,input_t],
                            output=[encoded_s,encoded_t])
    # compile model
    if domain_adaptation==False:
        cmd_weight = 0.
    else:
        # Please note that the loss weight of the cmd is one per default
        # (see paper).
        cmd_weight = 1.
    nn.compile(loss=['categorical_crossentropy',
                     'categorical_crossentropy',cmd],
               loss_weights=[1.,0.,cmd_weight],
               optimizer=Adadelta(),
               metrics=['accuracy'])
    return nn, visualize_model

def plot_classification_boarders(nn,save_name):
    """
    plot dataset and classification boarders
    """
    plt.figure()
    plt.plot(x_s[y_s[:,0]==1,0],x_s[y_s[:,0]==1,1],color='k',marker=r'$+$',
             linestyle='',ms=15)
    plt.plot(x_s[y_s[:,1]==1,0],x_s[y_s[:,1]==1,1],color='k',marker=r'$-$',
             linestyle='',ms=15)
    plt.plot(x_s[y_s[:,2]==1,0],x_s[y_s[:,2]==1,1],color='k',marker='*',
             linestyle='',ms=15)
    plt.plot(x_t[:,0],x_t[:,1],'k.')
    x_min = -1
    y_min = -0.75
    x_max = 1.2
    y_max = 1.3
    xy = np.mgrid[x_min:x_max:0.001, y_min:y_max:0.001].reshape(2,-1).T
    z = nn.predict([xy,xy])[0]
    ind = np.argmax(z,axis=1)
    z[ind!=0,0]=0
    z[ind!=1,1]=0
    x,y = np.mgrid[x_min:x_max:0.001, y_min:y_max:0.001]
    plt.contour(x,y,z[:,0].reshape(x.shape),levels = [0.1],
                colors=('k',),linestyles=('-',),linewidths=(2,))
    plt.contour(x,y,z[:,1].reshape(x.shape),levels = [0],
                colors=('k',),linestyles=('-',),linewidths=(2,))
    plt.axis('off')
    plt.savefig(save_name)

def plot_activations(a_s,a_t,save_name):
    """
    activation visualization via seaborn library
    """
    n_dim=a_s.shape[1]
    n_rows=1
    n_cols=int(n_dim/n_rows)
    fig, axs = plt.subplots(nrows=n_rows,ncols=n_cols, sharey=True,
                            sharex=True)
    for k,ax in enumerate(axs.reshape(-1)):
        if k>=n_dim:
            continue
        sns.kdeplot(a_t[:,k],ax=ax, shade=True, label='target',
                    legend=False, color='0.4',bw=0.03)
        sns.kdeplot(a_s[:,k],ax=ax, shade=True, label='source',
                    legend=False, color='0',bw=0.03)
        plt.setp(ax.xaxis.get_ticklabels(),fontsize=10)
        plt.setp(ax.yaxis.get_ticklabels(),fontsize=10)
    fig.set_figheight(3)
    plt.setp(axs, xticks=[0, 0.5, 1])
    plt.setp(axs, ylim=[0,10])
    plt.savefig(save_name)

# load dataset
x_s = np.load(DATA_FOLDER+'x_s.npy')
y_s = np.load(DATA_FOLDER+'y_s.npy')
x_t = np.load(DATA_FOLDER+'x_t.npy')
y_t = np.load(DATA_FOLDER+'y_t.npy')

# plot dataset
plt.plot(x_s[y_s[:,0]==1,0],x_s[y_s[:,0]==1,1],color='k',marker=r'$+$',
         linestyle='',ms=15)
plt.plot(x_s[y_s[:,1]==1,0],x_s[y_s[:,1]==1,1],color='k',marker=r'$-$',
         linestyle='',ms=15)
plt.plot(x_s[y_s[:,2]==1,0],x_s[y_s[:,2]==1,1],color='k',marker='*',
         linestyle='',ms=15)
plt.plot(x_t[:,0],x_t[:,1],'k.')
plt.axis('off')
plt.savefig(OUTPUT_FOLDER+'dataset.jpg')

# train source model without domain adaptation
nn, nn_vis_model = neural_network(domain_adaptation=False)
nn.fit(x=[x_s,x_t],
       y=[y_s,y_t,np.zeros((x_s.shape[0],1))],
       shuffle=True,
       nb_epoch=10000,
       verbose=0,
       batch_size=x_s.shape[0])
# save the weights
nn.save_weights(TMP_FOLDER+'nn.hdf5')
# train another 5000 epochs for fair comparison
np.random.seed(0)
nn.fit(x=[x_s,x_t],
       y=[y_s,y_t,np.zeros((x_s.shape[0],1))],
       shuffle=True,
       nb_epoch=5000,
       verbose=0,
       batch_size=x_s.shape[0])
# plot the classification boarders (Fig. 3 left in paper)
plot_classification_boarders(nn, OUTPUT_FOLDER+'nn.jpg')
# predict the target accuracy
# The final source accuracy of the NN should be 100% and the final target
# accuracy should be around 89% depending on your system random numbers, theano
# configuration (float32,..), CuDNN version, etc.
print('\nNN acc='+str(nn.evaluate([x_s,x_t],[y_s,y_t,y_t])[-2]))

# adapt the network to to the target domain by means of the central
# moment discrepancy
mann, mann_vis_model = neural_network(domain_adaptation=True)
mann.load_weights(TMP_FOLDER+'nn.hdf5')
np.random.seed(0)
mann.fit(x=[x_s,x_t],
         y=[y_s,y_t,np.zeros((x_s.shape[0],1))],
         shuffle=True,
         nb_epoch=5000,
         verbose=0,
         batch_size=x_s.shape[0])
# plot the classification boarders (Fig. 3 right in paper)
plot_classification_boarders(mann, OUTPUT_FOLDER+'mann.jpg')
# predict the target accuracy
# The predicted acccuracy of the MANN is around 10% more than the
# accuracy of the NN before. This is the result of our approach and does not
# strongly depend on the random numbers.
print('\nMANN acc='+str(mann.evaluate([x_s,x_t],[y_s,y_t,y_t])[-2]))

# Plot activations of NN (Fig. 4 top)
a_s,a_t = nn_vis_model.predict([x_s,x_t])
# Find five most significantly (K-S test) different distributions
p_vals = np.zeros(a_s.shape[1])
for i in range(a_s.shape[1]):
    ksstat, p_vals[i] = stats.ks_2samp(a_s[:,i],a_t[:,i])
ind_worst = p_vals.argsort()[:5]
plot_activations(a_s[:,ind_worst],a_t[:,ind_worst],
                 OUTPUT_FOLDER+'activations_nn.jpg')

# Plot activations of MANN (Fig. 4 bottom)
a_s,a_t = mann_vis_model.predict([x_s,x_t])
# Find five most significantly (K-S test) different distributions
p_vals = np.zeros(a_s.shape[1])
for i in range(a_s.shape[1]):
    ksstat, p_vals[i] = stats.ks_2samp(a_s[:,i],a_t[:,i])
ind_worst = p_vals.argsort()[:5]
plot_activations(a_s[:,ind_worst],a_t[:,ind_worst],
                 OUTPUT_FOLDER+'activations_mann.jpg')


import numpy as np
import pandas as pd
from pytorch_lightning.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from torch.utils.data import Dataset
import torch
from sklearn.preprocessing import StandardScaler

import pytorch_lightning as pl
from torch.utils.data import DataLoader
import random

class SwatDataset(Dataset):
    def __init__(
        self,
        cols: list[str],
        symbols_dct: dict,
        train_data_path: str,
        val_data_path: str,
        seq_len_x: int,
        seq_len_y: int = 200,
        split: str = "train",
        num_train_samples: int = 10000,
        num_val_samples: int = 1000,
    ):
        df_train = pd.read_parquet(train_data_path)
        df_val = pd.read_parquet(val_data_path)
        random.seed(42)

        df_train = df_train[cols]
        df_val = df_val[cols]
        self.scaler = StandardScaler()
        df_train_sc = pd.DataFrame(
            self.scaler.fit_transform(df_train.values),
            columns=df_train.columns,
            index=df_train.index,
        )
        df_train = df_train
        df_train_sc = df_train_sc
        df_val_sc = pd.DataFrame(
            self.scaler.transform(df_val.values),
            columns=df_val.columns,
            index=df_val.index,
        )

        if split == "train":
            self.df = df_train_sc["2015-12-22 16:30:00":"2015-12-27 23:59:59"]
            self.sample_index_list = random.sample(
                list(range(0, len(self.df.index) - seq_len_x - seq_len_y)),
                num_train_samples,
            )
        elif split == "val":
            self.df = df_train_sc["2015-12-28 00:00:00":"2015-12-28 09:59:55"]
            self.sample_index_list = random.sample(
                list(range(0, len(self.df.index) - seq_len_x - seq_len_y)),
                num_val_samples,
            )
        elif split == "test":
            self.df = df_val_sc
            self.sample_index_list = list(
                range(0, len(self.df.index) - seq_len_x - seq_len_y)
            )
        self.x = torch.from_numpy(self.df.values.astype(np.float32))
        self.comp_ls = [
            torch.from_numpy(self.df[symbols_dct[comp]].values.astype(np.float32))
            for comp in symbols_dct.keys()
        ]
        self.length = len(self.sample_index_list)
        self.seq_len_x = seq_len_x
        self.seq_len_y = seq_len_y

    def __len__(self):
        return self.length

    def __getitem__(self, index):
        idx = self.sample_index_list[index]
        return (
            self.x[idx : idx + self.seq_len_x, :].T,
            [comp[idx : idx + self.seq_len_x, :].T for comp in self.comp_ls],
            [
                comp[idx + self.seq_len_x : idx + self.seq_len_x + self.seq_len_y, :].T
                for comp in self.comp_ls
            ],
        )

class SwatDataModule(pl.LightningDataModule):
    def __init__(
        self,
        data_path_train: str,
        data_path_val: str,
        cols: list[str],
        symbols_dct: dict,
        batch_size: int = 32,
        seq_len_x: int = 1000,
        seq_len_y: int = 250,
        dl_workers: int = 8,
        num_train_samples: int = 10000,
        num_val_samples: int = 1000,
    ):
        super().__init__()
        self.batch_size = batch_size
        self.seq_len_x = seq_len_x
        self.cols = cols
        self.num_workers = dl_workers

        self.train_ds, self.val_ds, self.test_ds = [
            SwatDataset(
                train_data_path=data_path_train,
                val_data_path=data_path_val,
                seq_len_x=seq_len_x,
                seq_len_y=seq_len_y,
                cols=cols,
                symbols_dct=symbols_dct,
                split=split,
                num_train_samples=num_train_samples,
                num_val_samples=num_val_samples,
            )
            for split in ["train", "val", "test"]
        ]

    def train_dataloader(self) -> TRAIN_DATALOADERS:
        return DataLoader(
            self.train_ds,
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.num_workers,
        )

    def val_dataloader(self) -> EVAL_DATALOADERS:
        return DataLoader(
            self.val_ds,
            batch_size=self.batch_size,
            shuffle=False,
            num_workers=self.num_workers,
        )

    def test_dataloader(self) -> EVAL_DATALOADERS:
        return DataLoader(
            self.test_ds,
            batch_size=self.batch_size,
            shuffle=False,
            num_workers=self.num_workers,
        )

if __name__ == "__main__":
    import diag_vae.constants as const

    ds = SwatDataset(
        data_path=const.SWAT_VAL_PATH,
        seq_len_x=1000,
        cols=const.SWAT_SENSOR_COLS,
        symbols_dct=const.SWAT_SYMBOLS_MAP,
        scale=True,
    )
    ds.__getitem__(0)

    dm = SwatDataModule(
        data_path_train=const.SWAT_TRAIN_PATH,
        data_path_val=const.SWAT_VAL_PATH,
        seq_len_x=1000,
        cols=const.SWAT_SENSOR_COLS,
        symbols_dct=const.SWAT_SYMBOLS_MAP,
        dl_workers=8,
    )