# -*- coding: utf-8 -*-

import csv
import sys
import time
import random
import copy
import math
import os
sys.path.append(os.getcwd())
import os.path as pt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import skimage.io as skimage
from skimage import transform as tr
import skimage.morphology as mor
from argparse import ArgumentParser
from src.lib.utils import createOpbase
plt.style.use('ggplot')

class GraphDraw():

    def __init__(self, opbase, roi):
        self.opbase = opbase
        self.roi = roi
        #self.scale = 0.8 * 0.8 * 1.75
        self.scale = 0.8 * 0.8 * 2.0
        self.psep = '/'
        self.x = 160
        self.y = 160
        self.z = 111
        self.density = 0
        if roi != 0:
            with open('GT/10minGroundTruth/CSVfile/test{}.csv'.format(roi), 'r') as f:
                dataReader = csv.reader(f)
                l = [i for i in dataReader]
                self.GTCount = []
                tp = 1
                for i in range(len(l)):
                    if l[i][3] == str(tp):
                        tp += 1
                        self.GTCount.append(0)
                    self.GTCount[tp-2] += 1
        else:
            self.GTCount = None

    def graph_draw_number(self, Time, Count):
        # Count
        plt.figure()
        plt.plot(Time, Count)
        if self.GTCount is not None:
            plt.plot(Time, self.GTCount)
            plt.legend(["QCANet", "Ground Truth"],loc=2)
            ytick = [i for i in range(0, ((np.max(self.GTCount) / 5) + 1) * 5, 5)]
        else:
            plt.legend(["QCANet"],loc=2)
            ytick = [i for i in range(0, int(round(((np.max(Count) / 5) + 1))) * 5, 5)]
        plt.xlabel('Time [day]', size=12)
        plt.ylabel('Number of Nuclei', size=12)
        if Time[-1] != 0:
            plt.xlim([0.0, round(Time[-1], 1)])
        plt.yticks(ytick)
        filename = self.opbase + self.psep + 'Count.pdf'
        plt.savefig(filename)

    def graph_draw_volume(self, Time, SumVol, MeanVol, StdVol):
        SumVol = np.array(SumVol) * self.scale
        MeanVol = np.array(MeanVol) * self.scale
        StdVol = np.array(StdVol) * self.scale

        # Volume Mean & SD
        plt.figure()
        plt.plot(Time, MeanVol, color='blue')
        plt.fill_between(Time, np.array(MeanVol)-np.array(StdVol), np.array(MeanVol)+np.array(StdVol), color='blue', alpha=0.4)
        plt.legend(["Mean", "Std. Dev."],loc=1)
        plt.xlabel('Time [day]', size=12)
        plt.ylabel('Volume [$\mu m^{3}$]', size=12)
        if Time[-1] != 0:
            plt.xlim([0.0, round(Time[-1], 1)])
        plt.ylim([0.0, np.max(np.array(MeanVol)+np.array(StdVol)) + 1000])
        filename = self.opbase + self.psep + 'MeanStdVolume.pdf'
        plt.savefig(filename)

    def graph_draw_surface(self, Time, SumArea, MeanArea, StdArea):
        SumArea = np.array(SumArea) * self.scale
        MeanArea = np.array(MeanArea) * self.scale
        StdArea = np.array(StdArea) * self.scale

        # Surface Mean & SD
        plt.figure()
        plt.plot(Time, MeanArea, color='blue')
        plt.fill_between(Time, np.array(MeanArea)-np.array(StdArea), np.array(MeanArea)+np.array(StdArea), color='blue', alpha=0.4)
        plt.legend(["Mean", "Std. Dev."],loc=1)
        plt.xlabel('Time [day]', size=12)
        plt.ylabel('Surface Area [$\mu m^{2}$]', size=12)
        if Time[-1] != 0:
            plt.xlim([0.0, round(Time[-1], 1)])
        plt.ylim([0.0, np.max(np.array(MeanArea)+np.array(StdArea)) + 1000])
        filename = self.opbase + self.psep + 'MeanStdSurface.pdf'
        plt.savefig(filename)

    def graph_draw_centroid(self, cent_x, cent_y, cent_z):
        fig = plt.figure(figsize=(10, 8))
        ax = fig.add_subplot(111, projection='3d')
        #ax = Axes3D(fig)
        ax.set_xlabel("X")
        ax.set_ylabel("Y")
        ax.set_zlabel("Z")
        ax.set_xlim(0, self.x)
        ax.set_ylim(0, self.y)
        ax.set_zlim(0, 51)
        cmap =  plt.get_cmap('jet')
        zero_dim = np.zeros(len(cent_x))
        for i in range(len(cent_x)):
            colors = cmap(i / float(len(cent_x)))
            ax.plot(np.array(cent_x[i]), np.array(cent_y[i]), np.array(cent_z[i]), "o", color=colors, alpha=0.5, ms=2, mew=0.5)
            # ax.plot(np.array(cent_x[i]), np.array(cent_y[i]), np.zeros(len(cent_z[i])), "o", color=colors, alpha=0.5, ms=2, mew=0.5)
            # ax.plot(np.array(cent_x[i]), np.ones(len(cent_y[i])) * self.y, np.array(cent_z[i]), "o", color=colors, alpha=0.5, ms=2, mew=0.5)
            # ax.plot(np.zeros(len(cent_x[i])), np.array(cent_y[i]), np.array(cent_z[i]), "o", color=colors, alpha=0.5, ms=2, mew=0.5)
        filename = self.opbase + self.psep + 'Centroid.pdf'
        plt.savefig(filename)

    def graph_draw_lfunction(self, cent_x, cent_y, cent_z):
        roi = {}
        center = (self.x/2, self.y/2, self.z/2)
        radius_list = [i for i in range(int(self.z/2))]
        for r in radius_list:
            roi[r] = []
        for x in range(self.x):
            for y in range(self.y):
                for z in range(self.z):
                    for r in radius_list:
                        if (x - center[0]) ** 2 + (y - center[1]) ** 2 + (z - center[2]) ** 2 < r ** 2 and \
                            (x - center[0]) ** 2 + (y - center[1]) ** 2 + (z - center[2]) ** 2 >= (r - 1) ** 2:
                            roi[r].append([x, y, z])
        print('roi complete.')
        cmap =  plt.get_cmap('Paired')
        plt.figure(figsize=(10, 8))
        plt.plot(radius_list, [self.volume_density(roi, r, cent_x, cent_y, cent_z) for r in radius_list], alpha=0.8, linewidth=1.0)
        filename = self.opbase + self.psep + 'L-function.pdf'
        plt.savefig(filename)

    def volume_density(self, roi, radius, cent_x, cent_y, cent_z):
        density = 0
        for t in zip(cent_x, cent_y, cent_z):
            for cent in zip(t[0], t[1], t[2]):
                if [int(cent[0]), int(cent[1]), int(cent[2])] in roi[radius]:
                    density += 1
        self.density += density
        print('radius 1 count.')
        return self.density

    def graph_draw_centroid_2axis(self, cent_x, cent_y, axis):
        plt.figure()
        if axis is 'XY':
            plt.xlabel('X')
            plt.ylabel('Y')
            plt.xlim([0, self.x])
            plt.ylim([0, self.y])
        elif axis is 'YZ':
            plt.xlabel('Z')
            plt.ylabel('Y')
            plt.xlim([0, 51])
            plt.ylim([0, self.y])
        elif axis is 'ZX':
            plt.xlabel('X')
            plt.ylabel('Z')
            plt.xlim([0, self.x])
            plt.ylim([0, 51])
        cmap =  plt.get_cmap('jet')
        for i in range(len(cent_x)):
            colors = cmap(i / float(len(cent_x)))
            plt.plot(np.array(cent_x[i]), np.array(cent_y[i]), "o", color=colors, alpha=0.6, ms=3, mew=0.5)
        if axis is 'XY':
            filename = self.opbase + self.psep + 'Centroid-XY.pdf'
        elif axis is 'YZ':
            filename = self.opbase + self.psep + 'Centroid-YZ.pdf'
        elif axis is 'ZX':
            filename = self.opbase + self.psep + 'Centroid-ZX.pdf'
        plt.savefig(filename)

if __name__ == '__main__':
    ap = ArgumentParser(description='python graph_draw.py')
    ap.add_argument('--input', '-i', nargs='?', default='criteria.csv', help='Specify input files (format : csv)')
    ap.add_argument('--outdir', '-o', nargs='?', default='extract_figs', help='Specify output files directory for create figures')
    ap.add_argument('--roi', '-r', type=int, default=0, help='Specify ROI GT')
    args = ap.parse_args()
    argvs = sys.argv
    psep = '/'
    opbase = createOpbase(args.outdir)

    # each criterion
    cnt = []
    SumVol, SumArea, Count = [], [], []
    MeanVol, MeanArea, VarVol, VarArea = [], [], [], []
    Cent_X, Cent_Y, Cent_Z = [], [], []

    # import csv
    f = open(args.input, 'r')
    data = csv.reader(f)
    l = []
    for i in data:
        l.append(i)
    f.close()
    l.pop(0)

    for c in range(len(l)):
        if int(l[c][1]) > 0:
            Count.append(int(l[c][1]))
            SumVol.append(float(l[c][2]))
            MeanVol.append(float(l[c][3]))
            VarVol.append(float(l[c][4]))
            SumArea.append(float(l[c][5]))
            MeanArea.append(float(l[c][6]))
            VarArea.append(float(l[c][7]))
            x, y, z = [], [], []
            for i in range(len(l[c][8][1:-1].split(','))):
                x.append(float(l[c][8][1:-1].split(',')[i]))
                y.append(float(l[c][9][1:-1].split(',')[i]))
                z.append(float(l[c][10][1:-1].split(',')[i]))
            Cent_X.append(x)
            Cent_Y.append(y)
            Cent_Z.append(z)
        else:
            Count.append(0)
            SumVol.append(0)
            MeanVol.append(0)
            VarVol.append(0)
            SumArea.append(0)
            MeanArea.append(0)
            VarArea.append(0)
            Cent_X.append([0])
            Cent_Y.append([0])
            Cent_Z.append([0])

    # Time Scale
    dt = 10 / float(60 * 24)
    Time = [dt*x for x in range(len(Count))]

    gd = GraphDraw(opbase, args.roi)
    gd.graph_draw_number(Time, Count)
    gd.graph_draw_volume(Time, SumVol, MeanVol, VarVol)
    gd.graph_draw_surface(Time, SumArea, MeanArea, VarArea)
    #gd.graph_draw_centroid(Cent_X, Cent_Y, Cent_Z)
    #gd.graph_draw_lfunction(Cent_X, Cent_Y, Cent_Z)
    gd.graph_draw_centroid_2axis(Cent_X, Cent_Y, 'XY')
    gd.graph_draw_centroid_2axis(Cent_Z, Cent_Y, 'YZ')
    gd.graph_draw_centroid_2axis(Cent_X, Cent_Z, 'ZX')
    #plt.show()


import numpy as np
import pandas as pd
df=pd.read_csv('Books.csv',error_bad_lines=False,encoding='latin-1')
df1=pd.read_csv('Ratings.csv',error_bad_lines=False,encoding='latin-1')
df2=pd.read_csv('Users.csv',error_bad_lines=False,encoding='latin-1')
df=df[['ISBN','Book-Title','Book-Author','Year-Of-Publication','Publisher']]
x=df1['User-ID'].value_counts()>200
y=x[x].index
y
df1=df1[df1['User-ID'].isin(y)]
ratings_books=df1.merge(df,on='ISBN')
ratings_books.head()
num_ratings=ratings_books.groupby('Book-Title')['Book-Rating'].count().reset_index()
num_ratings.rename(columns={'Book-Rating':'num_of_ratings'},inplace=True)
final=ratings_books.merge(num_ratings,on='Book-Title')
final=final[final['num_of_ratings']>=50]
final.drop_duplicates(['User-ID','Book-Title'],inplace=True)
book_pivot=final.pivot_table(columns='User-ID',index='Book-Title',values='num_of_ratings')
book_pivot.fillna(0,inplace=True)
from scipy.sparse import csr_matrix
book_sparse=csr_matrix(book_pivot)
from sklearn.neighbors import NearestNeighbors
model=NearestNeighbors(algorithm='brute')
model.fit(book_sparse)
def recommend_book(book_name):
    book_id=np.where(book_pivot.index==book_name)[0][0]
    distances,suggestions=model.kneighbors(book_pivot.iloc[book_id,:].values.reshape(1,-1),n_neighbors=6)
    for i in range(len(suggestions)):
            print(book_pivot.index[suggestions[i]])

recommend_book('A Case of Need')


#!/usr/bin/env python3
import functools

def main():
    with open("day9/input.txt", "r") as input:
        lines = input.read().splitlines()
        low_points =[]
        for r in range(0, len(lines)):
            for c in range(0, len(lines[r])):
                cur_depth = int(lines[r][c])
                if c-1 >= 0 and int(lines[r][c-1]) <= cur_depth:
                    continue
                if c+1 < len(lines[r]) and int(lines[r][c+1]) <= cur_depth:
                    continue
                if r-1 >= 0 and int(lines[r-1][c]) <= cur_depth:
                    continue
                if r+1 < len(lines) and int(lines[r+1][c]) <= cur_depth:
                    continue
                low_points.append(cur_depth)
        print(f"Result: {functools.reduce(lambda x, y: x+y, list(map(lambda x: x+1, low_points)))}")
main()


from datetime import datetime, date, timedelta
import yaml

def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

def get_input_datetime(prompt):
    while True:
        try:
            print(prompt)
            input_date = input().split("/")
            return datetime(int(input_date[2]), int(input_date[0]), int(input_date[1]))
        except IndexError as e:
            print(f"Error occurred in processing date input: {e}")
            pass

start_date = get_input_datetime("Enter semester start date (mm/dd/yyyy):")
end_date = get_input_datetime("Enter semester end date (mm/dd/yyyy):")

dict_file = []
for single_date in daterange(start_date, end_date):
    if (single_date.weekday() > 4):
        continue

    str_date = single_date.strftime("%a %b %d")
    dict_file.append({
        "date": str_date,
        "lecture": {
            "name": "",
            "link": ""
        },
        "recitation": {
            "name": "",
            "slides": "",
            "handout": "",
            "quiz": ""
        },
        "reading": {
            "name": "",
            "link": ""
        },
        "homework": {
            "name": "",
            "deadline": "",
            "link": "",
            "numDays": 0
        }
    })

class LineBreakDumper(yaml.SafeDumper):
    # Reference: https://github.com/yaml/pyyaml/issues/127
    def write_line_break(self, data=None):
        super().write_line_break(data)

        if len(self.indents) == 1:
            super().write_line_break()

with open(r'schedule.yaml', 'w') as file:
    yaml.dump(dict_file, file, Dumper=LineBreakDumper, default_flow_style=False)


"""
Parametric DC inversion with Dipole Dipole array
================================================

This is an example for a parametric inversion with a DC survey.
Resistivity structure of the subsurface is parameterized as following
parameters:

    - sigma_background: background conductivity
    - sigma_block: block conductivity
    - block_x0: horizotontal location of the block (center)
    - block_dx: width of the block
    - block_y0: depth of the block (center)
    - block_dy: thickness of the block

User is promoted to try different initial values of the parameterized model.
"""

from SimPEG.electromagnetics.static import resistivity as DC, utils as DCutils
from discretize import TensorMesh
from discretize.utils import active_from_xyz
from SimPEG import (
    maps,
    utils,
    data_misfit,
    regularization,
    optimization,
    inversion,
    inverse_problem,
    directives,
)
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from pylab import hist

try:
    from pymatsolver import PardisoSolver as Solver
except ImportError:
    from SimPEG import SolverLU as Solver

def run(
    plotIt=True,
    survey_type="dipole-dipole",
    rho_background=1e3,
    rho_block=1e2,
    block_x0=100,
    block_dx=10,
    block_y0=-10,
    block_dy=5,
):
    np.random.seed(1)
    # Initiate I/O class for DC
    IO = DC.IO()
    # Obtain ABMN locations

    xmin, xmax = 0.0, 200.0
    ymin, ymax = 0.0, 0.0
    zmin, zmax = 0, 0
    endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
    # Generate DC survey object
    survey = DCutils.generate_dcip_survey(
        endl, survey_type=survey_type, dim=2, a=10, b=10, n=10
    )
    survey = IO.from_abmn_locations_to_survey(
        survey.locations_a,
        survey.locations_b,
        survey.locations_m,
        survey.locations_n,
        survey_type,
        data_dc_type="volt",
    )

    # Obtain 2D TensorMesh
    mesh, actind = IO.set_mesh()
    # Flat topography
    actind = active_from_xyz(
        mesh, np.c_[mesh.cell_centers_x, mesh.cell_centers_x * 0.0]
    )
    survey.drape_electrodes_on_topography(mesh, actind, option="top")
    # Use Exponential Map: m = log(rho)
    parametric_block = maps.ParametricBlock(mesh, slopeFact=1e2)
    mapping = maps.ExpMap(mesh) * parametric_block
    # Set true model
    # val_background,val_block, block_x0, block_dx, block_y0, block_dy
    mtrue = np.r_[np.log(1e3), np.log(10), 100, 10, -20, 10]

    # Set initial model
    m0 = np.r_[
        np.log(rho_background),
        np.log(rho_block),
        block_x0,
        block_dx,
        block_y0,
        block_dy,
    ]
    rho = mapping * mtrue
    rho0 = mapping * m0
    # Show the true conductivity model
    fig = plt.figure(figsize=(12, 3))
    ax = plt.subplot(111)
    temp = rho.copy()
    temp[~actind] = np.nan
    out = mesh.plot_image(
        temp,
        grid=False,
        ax=ax,
        grid_opts={"alpha": 0.2},
        pcolor_opts={"cmap": "viridis", "norm": colors.LogNorm(10, 1000)},
    )
    ax.plot(
        survey.unique_electrode_locations[:, 0],
        survey.unique_electrode_locations[:, 1],
        "k.",
    )
    ax.set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max())
    ax.set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min())
    cb = plt.colorbar(out[0])
    cb.set_label("Resistivity (ohm-m)")
    ax.set_aspect("equal")
    ax.set_title("True resistivity model")
    plt.show()
    # Show the true conductivity model
    fig = plt.figure(figsize=(12, 3))
    ax = plt.subplot(111)
    temp = rho0.copy()
    temp[~actind] = np.nan
    out = mesh.plot_image(
        temp,
        grid=False,
        ax=ax,
        grid_opts={"alpha": 0.2},
        pcolor_opts={"cmap": "viridis", "norm": colors.LogNorm(10, 1000)},
    )
    ax.plot(
        survey.unique_electrode_locations[:, 0],
        survey.unique_electrode_locations[:, 1],
        "k.",
    )
    ax.set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max())
    ax.set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min())
    cb = plt.colorbar(out[0])
    cb.set_label("Resistivity (ohm-m)")
    ax.set_aspect("equal")
    ax.set_title("Initial resistivity model")
    plt.show()

    # Generate 2.5D DC problem
    # "N" means potential is defined at nodes
    prb = DC.Simulation2DNodal(
        mesh, survey=survey, rhoMap=mapping, storeJ=True, solver=Solver
    )

    # Make synthetic DC data with 5% Gaussian noise
    data = prb.make_synthetic_data(mtrue, relative_error=0.05, add_noise=True)

    # Show apparent resisitivty pseudo-section
    IO.plotPseudoSection(data=data.dobs / IO.G, data_type="apparent_resistivity")

    # Show apparent resisitivty histogram
    fig = plt.figure()
    out = hist(data.dobs / IO.G, bins=20)
    plt.show()
    # Set standard_deviation
    # floor
    eps = 10 ** (-3.2)
    # percentage
    relative = 0.05
    dmisfit = data_misfit.L2DataMisfit(simulation=prb, data=data)
    uncert = abs(data.dobs) * relative + eps
    dmisfit.standard_deviation = uncert

    # Map for a regularization
    mesh_1d = TensorMesh([parametric_block.nP])
    # Related to inversion
    reg = regularization.WeightedLeastSquares(mesh_1d, alpha_x=0.0)
    opt = optimization.InexactGaussNewton(maxIter=10)
    invProb = inverse_problem.BaseInvProblem(dmisfit, reg, opt)
    target = directives.TargetMisfit()
    invProb.beta = 0.0
    inv = inversion.BaseInversion(invProb, directiveList=[target])
    prb.counter = opt.counter = utils.Counter()
    opt.LSshorten = 0.5
    opt.remember("xc")

    # Run inversion
    mopt = inv.run(m0)

    # Convert obtained inversion model to resistivity
    # rho = M(m), where M(.) is a mapping

    rho_est = mapping * mopt
    rho_true = rho.copy()
    # show recovered conductivity
    fig, ax = plt.subplots(2, 1, figsize=(20, 6))
    out1 = mesh.plot_image(
        rho_true,
        pcolor_opts={"cmap": "viridis", "norm": colors.LogNorm(10, 1000)},
        ax=ax[0],
    )
    out2 = mesh.plot_image(
        rho_est,
        pcolor_opts={"cmap": "viridis", "norm": colors.LogNorm(10, 1000)},
        ax=ax[1],
    )
    out = [out1, out2]
    for i in range(2):
        ax[i].plot(
            survey.unique_electrode_locations[:, 0],
            survey.unique_electrode_locations[:, 1],
            "kv",
        )
        ax[i].set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max())
        ax[i].set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min())
        cb = plt.colorbar(out[i][0], ax=ax[i])
        cb.set_label(r"Resistivity ($\Omega$m)")
        ax[i].set_xlabel("Northing (m)")
        ax[i].set_ylabel("Elevation (m)")
        ax[i].set_aspect("equal")
    ax[0].set_title("True resistivity model")
    ax[1].set_title("Recovered resistivity model")
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    run()
    plt.show()


import requests

# Following URL is used to retrieve metadata of gce instance.
# For more information check following link:
# https://cloud.google.com/compute/docs/storing-retrieving-metadata
METADATA_ATTRIBUTES_URL = (
    'http://metadata.google.internal/computeMetadata/v1/instance/attributes/')

METADATA_HEADERS = {
    'Metadata-Flavor': 'Google'
}

def get_metadata_param(param_name):
    """Fetch value of metadata parameter.

    Args:
        param_name: str. Name of the parameter to be fetched.
    """

    # Send a request to get param details.
    resp = requests.get(
        '%s/%s' % (METADATA_ATTRIBUTES_URL, param_name),
        headers=METADATA_HEADERS)

    return resp.text


#!/usr/bin/env python3
import sys
import click

"""
    Endpoint to ExCthulhu
"""

from cthulhu_src.utils import logger

# import commands
from cthulhu_src.routes.config import config
from cthulhu_src.routes.find_txn import find
from cthulhu_src.routes.exchanges import exchanges
from cthulhu_src.routes.available_io import available_io

@click.group()
@click.pass_context
def cli(ctx):
    ctx.ensure_object(dict)

# include cli commands
cli.add_command(find)
cli.add_command(config)
cli.add_command(exchanges)
cli.add_command(available_io)

def main():
    """
        Before command routing initialize global objects.
        :return: int - exit status
    """
    debug = False
    if '--debug' in sys.argv:
        sys.argv.remove('--debug')
        log = logger.init(debug=True)
        debug = True
    else:
        log = logger.init()

    sys.exit(cli(obj={"debug": debug}))

if __name__ == '__main__':
    main()


import os,psutil,time,datetime as dttime,sys
from optparse import OptionParser
from paircarstools.access_ms import *
from paircarstools.basic_func import *
from paircarstools.flagger import *
from casatasks import *
from multiprocessing import current_process
os.system('rm -rf casa*log')

def split_single_ms(paramlist,timeinterval=-1):
	process = current_process()
	process.daemon = False
	vis,datacolumn,chan,timestamp,chanwidth,timewidth,savedir,verbose,max_flag_frac=paramlist
	max_flag_frac=float(max_flag_frac)
	outputvis=savedir+'/time_'+str(timestamp_to_mjdsec(timestamp.split('+')[0],format=0))+'_chan_'+str(chan)+'.tempms'
	outputvis_touch_file=savedir+'/.time_'+str(timestamp_to_mjdsec(timestamp.split('+')[0],format=0))+'_chan_'+str(chan)+'.done'
	if os.path.exists(outputvis_touch_file):
		if verbose:
			print ('Measurment set for time : '+str(timestamp)+' and channel : '+str(chan)+' is already done.\n')
		os.system('touch '+outputvis_touch_file)
		return outputvis+'.junk'
	if os.path.exists(outputvis):
		if verbose:
			print ('Output ms exists. Removing it.\n')
		os.system('rm -rf '+outputvis+' '+outputvis+'.flagversions')
	try:
		if verbose:
			print ('Spliting '+vis+'....')
			print ('split(vis=\''+vis+'\',outputvis=\''+outputvis+'\',spw=\''+chan+'\',timerange=\''+timestamp+'\',datacolumn=\''+datacolumn+'\',width='+str(chanwidth)+\
				',timebin=\''+str(timewidth)+'\')\n')
		split(vis=vis,outputvis=outputvis,spw=chan,timerange=timestamp,datacolumn=datacolumn,width=int(chanwidth),timebin=str(timewidth))
		if verbose:
			print ('Spliting done.\n')
	except Exception as e:
		print('Split error : '+str(e)+'\n')
	time.sleep(5)
	AM=AccessMS(outputvis)
	flagfrac=float(AM.calc_flagfrac())
	if flagfrac<max_flag_frac:
		if verbose:
			print ('Renaming the output ms.\n')
		outputvis=splited_ms_rename(outputvis,ref_time_chan=False,change_msname=True)
		os.system('rm -rf casa*log')
		os.system('touch '+outputvis_touch_file)
		return outputvis
	else:
		if verbose:
			print ('More than '+str(max_flag_frac*100)+'% data are flagged.\n')
		os.system('touch '+outputvis.split('.ms')[0]+'_junk.ms')
		os.system('rm -rf '+outputvis)
		os.system('rm -rf casa*log')
		os.system('touch '+outputvis_touch_file)
		return outputvis+'.junk'

def main():
	usage= 'Split a single measurement set \n'
	parser = OptionParser(usage=usage)
	parser.add_option('--msname',dest="vis",default=None,help="Name of measurement set to split from",metavar="Measurement Set")
	parser.add_option('--datacolumn',dest='datacolumn',default='data',help='Datacolumn to split',metavar="String")
	parser.add_option('--spw',dest='chan',default='',help='CASA format channel range to split',metavar="String")
	parser.add_option('--timestamp',dest='timestamp',default='',help='CASA fomat timerange to split',metavar="String")
	parser.add_option('--chanwidth',dest='chanwidth',default=1,help='Number of channels to average',metavar="Integer")
	parser.add_option('--timewidth',dest='timewidth',default=0,help='Time to average with unit',metavar="String")
	parser.add_option('--savedir',dest='savedir',default=None,help='Save directory',metavar="Directory path")
	parser.add_option('--verbose',dest='verbose',default=False,help='Verbose output',metavar="Boolean")
	parser.add_option('--max_flag_frac',dest='max_flag_frac',default=0.95,help='Maximum allowed flag fraction',metavar="Float")
	(options, args) = parser.parse_args()

	if options.vis==None:
		print ('Measurement set does not exit.\n')
		return 1
	elif os.path.isdir(options.vis)==False:
		print ('Measurement set does not exit.\n')
		return 1
	if options.savedir==None:
		print ('Please provide correct save directory path.\n')
		return 1
	elif os.path.isdir(options.savedir)==False:
		try:
			os.makedirs(options.savedir)
		except:
			print ('Please provide correct save directory path.\n')
			return 1
	result=split_single_ms(str(options.vis),str(options.datacolumn),str(options.chan),str(options.timestamp),int(options.chanwidth),str(options.timewidth),str(options.savedir),\
			eval(str(options.verbose)),max_flag_frac=float(options.max_flag_frac))
	if result!=None and os.path.isdir(result):
		print (options.vis+' is splited to '+result+'\n')
		return 0
	else:
		print (options.vis+' could not be splited.\n')
		return 1

if __name__ == "__main__":
	main()


from __future__ import annotations

import logging
import traceback
from typing import Any

from nni.mutable import MutableExpression, Categorical, Numerical

_logger = logging.getLogger(__name__)

def conclude_assumptions(values: list[int | float]) -> dict[str, bool]:
    """Conclude some sympy assumptions based on the examples in values.

    Support assumptions are: positive, negative, nonpositive, nonnegative,
    zero, nonzero, odd, even, real.
    """
    if not values:
        return {}

    assumptions = {}
    assumptions['real'] = all(isinstance(v, (float, int)) for v in values)
    if not assumptions['real']:
        return assumptions

    assumptions['integer'] = all(isinstance(v, int) for v in values)
    if all(v > 0 for v in values):
        assumptions['positive'] = True
    if all(v < 0 for v in values):
        assumptions['negative'] = True
    if all(v >= 0 for v in values):
        assumptions['nonnegative'] = True
    if all(v <= 0 for v in values):
        assumptions['nonpositive'] = True
    if all(v == 0 for v in values):
        assumptions['zero'] = True
    if all(v != 0 for v in values):
        assumptions['nonzero'] = True

    if not assumptions['integer']:
        return assumptions

    if all(v % 2 == 0 for v in values):
        assumptions['even'] = True
    if all(v % 2 == 1 for v in values):
        assumptions['odd'] = True

    return assumptions

_seen_errors = set()

def expression_simplification(expression: MutableExpression):
    try:
        from sympy import Symbol, Expr, lambdify, simplify
    except ImportError:
        _logger.warning('sympy is not installed, give up expression simplification.')
        return expression

    # Get all symbols.
    mutables = expression.simplify()

    # Mutables will be substituted with sympy symbol / expressions.
    mutable_substitutes: dict[str, Symbol | Expr] = {}
    # (inverse substitution, in lambdify) Each sympy symbol corresponds to a mutable expression.
    inverse_substitutes: dict[Symbol | Expr, MutableExpression] = {}

    for name, mutable in mutables.items():
        if isinstance(mutable, Categorical):
            assumptions = conclude_assumptions(mutable.values)
            if not assumptions.get('real', False):
                _logger.warning('Expression simplification only supports categorical mutables with numerical choices, '
                                'but got %r. Give up.', mutable)

            # Variable substitution as sympy appears not very clever when handling odd/even.
            # Workaround: https://stackoverflow.com/q/75236716/6837658
            # It will leave some *2, //2 in the expression, but it's fine.
            odd = assumptions.pop('odd', False)

            if odd:
                if not assumptions.get('positive', False):
                    # Not all positive. This should be a rare case.
                    # That means we have -1 -> -3, -2 -> -5, ..., and we've lost 0 -> -1.

                    # There is no rule saying whether -1 is allowed or not, but there are rules about 0.
                    # We would simply say that "zero can exist" regardless of whether -1 appears in the sample.
                    if assumptions.get('nonnegative', False):
                        assumptions['nonpositive'] = True
                    assumptions.pop('nonzero', None)

                symbol = Symbol(name, **assumptions)
                mutable_substitutes[name] = symbol * 2 - 1  # 1 -> 1, 2 -> 3, 3 -> 5, ...
                inverse_substitutes[symbol] = (mutable + 1) // 2

                # I can't say whether we should do the same to even.

            else:
                symbol = Symbol(name, **assumptions)
                mutable_substitutes[name] = symbol
                inverse_substitutes[symbol] = mutable

        elif isinstance(mutable, Numerical):
            symbol = Symbol(name, real=True)  # we only know it's real
            mutable_substitutes[name] = symbol
            inverse_substitutes[symbol] = mutable

        else:
            _logger.warning('Expression simplification only supports categorical and numerical mutables, '
                            'but got %s in expression. Give up.',
                            type(mutable))
            return expression

    try:
        # Create sympy expression and simplify it.
        sym_expression = expression.evaluate(mutable_substitutes)
        simplified_sym_expression = simplify(sym_expression)

        # Convert sympy expression back to MutableExpression.
        # Can't use .sub directly here because `__index__` seems unhappy with sympy.
        simplified_fn = lambdify(list(inverse_substitutes.keys()), simplified_sym_expression)
        simplified_expr = simplified_fn(*inverse_substitutes.values())

        # Convert the expression to expected type.
        expected_type = type(expression.default())
        actual_type = type(simplified_expr.default() if isinstance(simplified_expr, MutableExpression) else simplified_expr)
        if actual_type != expected_type:
            if expected_type == int:
                simplified_expr = round(simplified_expr)
            elif expected_type == float:
                simplified_expr = MutableExpression.to_float(simplified_expr)
            else:
                _logger.warning('Simplified expression is of type %s, but expected type is %s. Cannot convert.',
                                actual_type, expected_type)
                return expression

    except Exception as e:
        error_repr = repr(e)
        if error_repr not in _seen_errors:
            _seen_errors.add(error_repr)
            _logger.warning('Expression simplification failed: %s. Give up.\nExpression: %s\n%s',
                            error_repr, expression, traceback.format_exc())
        else:
            # Similar errors have shown already.
            pass
        return expression

    return simplified_expr

def recursive_simplification(obj: Any) -> Any:
    """Simplify all expressions in obj recursively."""
    from .shape import MutableShape

    if isinstance(obj, MutableExpression):
        return expression_simplification(obj)
    elif isinstance(obj, MutableShape):
        return MutableShape(*[recursive_simplification(v) for v in obj])
    elif isinstance(obj, dict):
        return {k: recursive_simplification(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [recursive_simplification(v) for v in obj]
    elif isinstance(obj, tuple):
        return tuple(recursive_simplification(v) for v in obj)
    return obj


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 10 13:35:19 2020

@author: ilail
"""

import pandas as pd
import re
import matplotlib.pyplot as plt
from os import path
import os

# %% Reads MPT or DTA to common data structure

class CyclicVoltammetry:
    def __init__(self, path):
        self.filepath = path  # path of input file
        self.settings = {}  # info from input header
        self.data = None  # contains Pandas dataframe of CV

        self._load_cv()  # read input data

    def _load_cv(self):
        # choose input parser based on file extension
        filename = os.path.basename(self.filepath)
        root = path.splitext(filename)[0]
        extension = path.splitext(filename)[1]

        if extension.lower() == ".dta":
            self.settings["format"] = "Gamry"
            # self.settings['folder'] = ''
            self.settings["filename"] = filename
            self.settings["file_rootname"] = root
            self.settings["extension"] = "dta"
            self._read_DTA()
        elif extension.lower() == ".mpt":
            self.settings["format"] = "Biologic"
            self.settings["filename"] = filename
            self.settings["extension"] = "mpt"
            self.settings["file_rootname"] = root
            self._read_MPT()
        elif extension.lower() == "":
            print("No file selected")
            raise ValueError
        else:
            # if extension is not recognized, raise error
            raise ValueError

    def _read_MPT(self):
        with open(self.filepath, "r", encoding="utf8", errors="ignore") as f:
            iterator = iter(f)  # iterator avoids checking settings after header

            for line in iterator:
                if "Nb header lines" in line:
                    skiprows = int(line.split()[4])
                elif "Ei (V)" in line:
                    v_init = line.replace(",", ".").split()[2]
                    self.settings["initial voltage"] = float(v_init)
                elif "E1 (V)" in line:
                    v_final = line.replace(",", ".").split()[2]
                    self.settings["final voltage"] = float(v_final)
                elif "mode	ox/red	error" in line:
                    break

            self.data = pd.read_csv(
                self.filepath, sep="\t", skiprows=skiprows - 1, decimal=",",
            )

            uniques = self.data["cycle number"].value_counts()  #
            self.settings["n_cycles"] = len(uniques)
            self.data.rename(
                columns={
                    "time/s": "T",
                    "Ewe/V": "Vf",
                    "<I>/mA": "Im",
                    "cycle number": "Cycle n",
                },
                inplace=True,
            )

            self.data = self.data[["Cycle n", "T", "Vf", "Im"]]
            self.data["Cycle n"] -= 1  # switch to 0 based cycle indexing
            self.data.set_index("Cycle n", inplace=True)

    def _read_DTA(self):
        # only consider data after label CURVE\d and ignore CURVEOCV
        is_it_curve = re.compile("CURVE\d")
        with open(self.filepath, "r", encoding="utf8", errors="ignore") as f:
            iterator = iter(f)  # iterator avoids checking settings after header

            # headers for self.settings dict
            row_idx = 0
            for line in iterator:
                row_idx += 1
                if "SCANRATE" in line:
                    scanrate = line.replace(",", ".").split("\t")[2]
                    self.settings["scan rate"] = float(scanrate)
                elif "VINIT" in line:
                    v_init = line.replace(",", ".").split("\t")[2]
                    self.settings["initial voltage"] = float(v_init)
                elif "VLIMIT1" in line:
                    vlimit_1 = line.replace(",", ".").split("\t")[2]
                    self.settings["vlimit 1"] = float(vlimit_1)
                elif "VLIMIT2" in line:
                    vlimit_2 = line.replace(",", ".").split("\t")[2]
                    self.settings["vlimit 2"] = float(vlimit_2)
                elif "INSTRUMENTVERSION" in line:
                    break

            # Find peak (either maximum or minimum) in voltage scan...
            # This is gamry nonsense :@
            if float(v_init) == float(vlimit_1):
                self.settings["final voltage"] = float(vlimit_2)
            else:
                self.settings["final voltage"] = float(vlimit_1)
            file_format = None
            header = None

            for line in iterator:
                row_idx += 1
                if "OCVCURVE" in line:
                    continue
                elif "CURVE" in line:
                    if "CURVE	TABLE" in line:
                        file_format = "Single table"
                        header = next(f)
                        header_units = next(f)
                        break
                    elif is_it_curve.match(line):
                        file_format = "Multiple tables"
                        next(f)
                        next(f)
                        break

            # DTA can come in two formats...
            useful_keys = ["Cycle n", "T", "Vf", "Im"]

            if file_format == "Single table":
                header = header.replace("Cycle", "Cycle n")
                header = header.split("\t")
                self.data = pd.read_csv(
                    self.filepath,
                    sep="\t",
                    skiprows=row_idx + 2,
                    names=header,
                    decimal=",",
                )
                self.data = self.data.drop(self.data.columns[0], axis=1)
                uniques = self.data["Cycle n"].value_counts()  #
                self.settings["n_cycles"] = len(uniques)

                self.data = self.data[useful_keys]
                self.data.set_index("Cycle n", inplace=True)
                return
            else:
                curves = []
                curveN = 0
                # read CV data, multiple tables
                for line in iterator:
                    if is_it_curve.match(line):
                        curveN += 1
                        next(f)
                        next(f)
                        continue
                    if curveN >= 0:
                        line_float = [
                            float(ele)
                            for ele in line.strip().replace(",", ".").split("\t")[0:7]
                        ]
                        curves.append([curveN] + line_float)

                self.settings["n_cycles"] = curveN + 1
                header = ["Cycle n", "Pt", "T", "Vf", "Im", "Vu", "Sig0", "Ach"]
                # just keep the relevant ones and use cycle num as vertical key
                self.data = pd.DataFrame(curves, columns=header)[useful_keys]
                self.data.set_index("Cycle n", inplace=True)

    def _plot(self, cycle):
        curr = self[cycle]["Im"]
        volt = self[cycle]["Vf"]
        plt.rcParams.update({"font.size": 12})
        plt.rc("legend", fontsize=9)
        plt.xlabel("V vs ref [V]")
        plt.ylabel("i [mA]")

        plt.plot(volt, curr)

    def __getitem__(self, key):
        """
        Return table filtered by value key.

        Parameters
        ----------
        key : int or str
            If int, key is interpreted as cycle number, otherwise as pd column

        Returns
        -------
        Pandas dataframe
            Returns pandas filtered by cycle number if key is int
            Returns pandas filtered by column otherwise

        """
        if key in self.data.columns:
            return self.data[key]
        elif isinstance(key, int):
            return self.data.loc[key]
        # else:
        #    return self.data.filter(like = key, axis = 0)

    def __repr__(self):
        """
        Return __repr__ of the data Pandas dataframe.

        Returns
        -------
        str
            Representation of data Pandas dataframe.

        """
        return self.data.__repr__()
        # return self.filepath

    def __str__(self):
        return self.filepath

    def __iter__(self):
        # return pd table filtered by cycle number as in __getitem__
        for cycle in range(self.settings["n_cycles"]):
            yield self.data.loc[cycle]

    def _current_dens(self, area):
        self.data["Idens"] = self.data["Im"] * 1000 / area


def sys_bgcolor(pyplot):
    """Reads system preference and sets plt background accordingly"""
    from winreg import ConnectRegistry, HKEY_CURRENT_USER, OpenKeyEx, QueryValueEx
    root = ConnectRegistry(None, HKEY_CURRENT_USER)
    policy_key = OpenKeyEx(root, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize")
    light, _ = QueryValueEx(policy_key, "AppsUseLightTheme")
    if light:
        pyplot.style.use('default')
    else:
        pyplot.style.use('dark_background')

def winplay(data, rate, *, width=3, offset=0, seconds=15):
    """Plays a numpy array as a sound on Windows (and is unstoppable)"""
    import winsound
    data = data[rate*offset:rate*(offset + seconds)]
    r = bytearray()
    r += b'\x52\x49\x46\x46' # RIFF
    r += (width*len(data) + 36).to_bytes(4, 'little') # bytes until EOF
    r += b'\x57\x41\x56\x45' # WAVE
    r += b'\x66\x6D\x74\x20' # fmt_
    r += b'\x10\x00\x00\x00' # 16 bytes for the chunk
    r += b'\x01\x00' # PCM = 1
    r += b'\x01\x00' # number of channels
    r += (rate).to_bytes(4, 'little') # number of channels
    r += (rate*width).to_bytes(4, 'little') # bytes per sec
    r += (width).to_bytes(2, 'little') # bytes per block
    r += (width*8).to_bytes(2, 'little') # bits per sample
    r += b'\x64\x61\x74\x61' # data
    r += (width*len(data)).to_bytes(4, 'little') # bytes until EOF
    max_value = max(abs(data))
    max_number = 2**(width*8 - 1) - 1
    for e in data:
        r += int(e*max_number/max_value).to_bytes(width, 'little', signed=True)
    winsound.PlaySound(r, winsound.SND_MEMORY)

def fin():
    """To play a sound after execution"""
    from IPython.display import Audio
    return Audio('https://www.soundboard.com/mediafiles/23/230637-88d7c1eb-fd29-4c12-9775-f8dff855374b.mp3',
                 autoplay=True)

def toggle_color_mode():
    """Toggles system color scheme preference."""
    from winreg import ConnectRegistry, HKEY_CURRENT_USER, REG_SZ, KEY_ALL_ACCESS, OpenKeyEx, QueryValueEx, SetValueEx, CloseKey
    root = ConnectRegistry(None, HKEY_CURRENT_USER)
    policy_key = OpenKeyEx(root, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", access=KEY_ALL_ACCESS)
    light, _ = QueryValueEx(policy_key, "AppsUseLightTheme")
    SetValueEx(policy_key, "AppsUseLightTheme", 0, REG_SZ, str(1-int(light)))
    SetValueEx(policy_key, "SystemUsesLightTheme", 0, REG_SZ, str(1-int(light)))
    CloseKey(policy_key)


from flask import Flask, render_template,request
from flask_socketio import SocketIO, emit
from new import myinput_network
# https://flask-socketio.readthedocs.io/en/latest/
# https://github.com/socketio/socket.io-client

app = Flask(__name__)

app.config[ 'SECRET_KEY' ] = 'jsbcfsbfjefebw237u3gdbdc'
socketio = SocketIO( app )

@app.route( '/' )
def hello():
  return render_template( './ChatApp.html' )

def messageRecived():
  print( 'message was received!!!' )

@socketio.on( 'my event' )
def handle_my_custom_event( json ):

  print( 'recived my event: ' + str( json ) )
  mesg=json['message']
  result,x=myinput_network(mesg)
  result=[r*100 for r in result]
  if result[0]>30 or result[1]>30 or result[2]>30 or result[3]>30 or result[4]>30 or result[5]>30:
  	json['message'] = "*Warning: Negative Message Detected*"
  socketio.emit( 'my response', json, callback=messageRecived )

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


"""added columns in payment

Revision ID: dcd654518f68
Revises: e49f52e03497
Create Date: 2022-06-26 22:26:07.373514

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'dcd654518f68'
down_revision = 'e49f52e03497'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('payment', sa.Column('receipt_url', sa.String(length=500), nullable=True))
    op.add_column('payment', sa.Column('receipt_created_date', sa.DateTime(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('payment', 'receipt_created_date')
    op.drop_column('payment', 'receipt_url')
    # ### end Alembic commands ###


from transactions.models import Transaction
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from tests.factories import create_user_with_token

User: AbstractUser = get_user_model()

def create_transaction(user: AbstractUser) -> Transaction:
    transaction_data = {
        "user": str(user.id),
        "type": "cashout",
        "name": "aluguel",
        "value": 1500,
        "category": "house_bills",
    }
    transaction = Transaction.objects.create(**transaction_data)

    return transaction


# gaussian process regression
# Theory: [y, y*] ~ N([mu, mu*], [K, K*][K*T, K**])
#  - y(x) is the data given
#  - y* is to be inferred
# Use theorem on Gaussians (convolution) to get
# conditional for y*: P(y* | y, x, mu, mu*, K*, K**)
# This is a Gaussian again. K* and K** are taken
# from a so-called kernel to be designed; a typical
# choice is exponential squared (see in code)
import numpy as np
import matplotlib.pyplot as pl

def sqdist(a, b):
    a = a[np.newaxis].T
    return a**2 + b**2 - 2*a*b

def kernel(a, b):
    return np.exp(-0.5 * sqdist(a, b))

n = 50
Xtest = np.linspace(-5, 5, n)
Xdata = Xtest[np.abs(Xtest) < 1]
#Ydata = 2*np.sin(2*Xdata)  # function to approximate
Ydata = Xdata ** 2 - 0.5

# kernel for the given data
K_ = kernel(Xdata, Xdata)
# Add 1e-6, to account for the noise (variance) of the input
# data
Ky = K_ + 1e-6*np.eye(Xdata.size)

# Using L is supposedly more stable than inverting K straight
# away; on this example, it does not seem to change anything
L = np.linalg.cholesky(Ky)
#alpha = np.linalg.solve(L.T, np.linalg.solve(L, Ydata))

alpha = np.linalg.solve(Ky, Ydata)

kstars = kernel(Xtest, Xdata)
mean = np.dot(kstars, alpha)
print("kstars: {}  alpha: {}  mean: {}".format(kstars.shape, alpha.shape, mean.shape))

pl.plot(Xtest, mean)
pl.plot(Xdata, Ydata, 'ro')
pl.show()


from __future__ import print_function, division, absolute_import

import inspect
from six import string_types, add_metaclass
from collections import defaultdict

import numpy as np

_non_override_list = ('__setattr__', '__getattr__', '__getattribute__',
                      '__subclasshook__', '__getnewargs__', '__reduce__',
                      '__reduce_ex__', '__new__', '__init__')

_support_primitive = (tuple, list, str, int, float, dict)

def _create_theproxy_(attrname):
    def __theproxy__(self, *args, **kwargs):
        t = self.ref_value
        if t is None:
            t = self.default_value
        return getattr(t, attrname)(*args, **kwargs)
    return __theproxy__

class _MetaPrimitive(type):

    """ This meta class if helpfull for creating reference class
    while keeping its primitive type. """

    def __new__(meta, name, bases, attrdict):
        # ====== check bases classes ====== #
        # check if is primitive ref
        is_primitive_ref = any(i is PrimitiveRef for i in bases)
        if not is_primitive_ref:
            raise ValueError("the bases class of %s must contains PrimitiveRef."
                             % name)
        # check if contain primitive support type
        primitive_type = [i for i in bases if i in _support_primitive]
        if len(primitive_type) == 0:
            raise ValueError("Only support primitive types for the first base, "
                             "but given: %s" % str(bases))
        primitive_type = primitive_type[0]
        # ====== get the primitive type ====== #
        dummy_instance = primitive_type()
        for s in dir(dummy_instance):
            dummy_attr = getattr(dummy_instance, s)
            if type(dummy_attr).__name__ in ('builtin_function_or_method',
                                             'method-wrapper') and \
            s not in attrdict and s not in _non_override_list:
                attrdict[s] = _create_theproxy_(s)
        return super(_MetaPrimitive, meta).__new__(meta, name, bases, attrdict)

    def __init__(cls, name, bases, attrdict):
        super(_MetaPrimitive, cls).__init__(name, bases, attrdict)

class PrimitiveRef(object):
    """ PrimitiveRef """

    def __new__(cls, obj, default=None):
        main_type = [i for i in inspect.getmro(cls)
                     if i in _support_primitive][0]
        if default is None:
            default = main_type()
        if not isinstance(default, main_type):
            raise ValueError("default value must be instance of type: '%s', but "
                             "was given type: '%s'" %
                             (main_type.__name__, type(default).__name__))
        # ====== create new instance ====== #
        instance = super(PrimitiveRef, cls).__new__(cls, default)
        instance._default = default
        return instance

    def __init__(self, obj, default=None):
        super(PrimitiveRef, self).__init__(default)
        self._obj = obj

    # ==================== others ==================== #
    @property
    def default_value(self):
        return self._default

    @property
    def ref_value(self):
        return self._obj

@add_metaclass(_MetaPrimitive)
class DtypeRef(PrimitiveRef, str):

    @property
    def ref_value(self):
        v = self._obj
        if hasattr(v, 'dtype'):
            return str(v.dtype)
        elif isinstance(v, (string_types, np.ndarray, np.dtype)):
            return str(v)
        return None

@add_metaclass(_MetaPrimitive)
class ShapeRef(PrimitiveRef, tuple):

    @property
    def ref_value(self):
        v = self._obj
        if hasattr(v, 'shape'):
            return tuple(v.shape)
        elif isinstance(v, (tuple, list, np.ndarray)):
            return tuple(v)
        return None

# ===========================================================================
# Async file IO
# ===========================================================================
class keydefaultdict(defaultdict):
    def __missing__(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        else:
            ret = self[key] = self.default_factory(key)
            return ret


import requests
from bs4 import BeautifulSoup
import pprint
import lxml
def get_movie_EnglishName():
    movie_list=[]
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \
                (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
               'Host': 'movie.douban.com'}
    for i in range(10):
        link = "https://movie.douban.com/top250?start=" + str(i * 25)
        r = requests.get(link, headers=headers)
        print(str(i+1),"页响应状态码： ",r.status_code)
        soup=BeautifulSoup(r.text,"lxml")
        for tag in soup.find_all(attrs={"class":"item"}):
            title=tag.find_all(attrs={"class":"title"})
            i=0
            for each in title :
                text=each.get_text()
                text=text.replace('/','')
                text=text.lstrip()
                if i==1:
                    movie_list.append(text)
                i+=1
    return movie_list

pprint.pprint(get_movie_EnglishName())


from rest_framework import serializers
from rest_framework.response import Response
from django.http import HttpResponse

from .models import Crop, Seed, Machinery, Others, PesticidesAndFertilizers, BuyRequest, CropSell, CropActivity
from v1apps.user.serializers import UserSerializer

class CropActivitySerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'crops', 'activity', 'images', 'date')
        model = CropActivity

class CropListSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'crop_name', 'harvest_days', 'acres', 'crop_image')
        model = Crop

class CropSerializer(serializers.ModelSerializer):
    user_name = serializers.ReadOnlyField(source='user.name')
    farm_name = serializers.ReadOnlyField(source='farm.area')
    activities = serializers.SerializerMethodField()

    class Meta:
        fields = ('id', 'crop_name', 'exp_price', 'exp_yield', 'exp_harvest_date',
                  'total_expense', 'fertilizer_expense', 'machine_expense', 'seed_expense',
                  'user', 'farm', 'user_name', 'farm_name', 'crop_image', 'activities')
        model = Crop

    def get_activities(self, obj):
        activities_queryset = CropActivity.objects.filter(crops=obj)
        return CropActivitySerializer(activities_queryset, many=True).data

class CropSellSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'crops', 'user')
        model = CropSell

class SeedSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name', 'seed_image', 'price')

        model = Seed

class MachineSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name', 'machine_image', 'price')
        model = Machinery

class OtherSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name', 'other_image', 'price')
        model = Others

class PesticideAndFertilizerSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name', 'pest_image', 'price')
        model = PesticidesAndFertilizers

class BuyListSerializer(serializers.ModelSerializer):
    # pest_fer = PesticideAndFertilizerSerializer(read_only=True, many=True)
    # seed = SeedSerializer(read_only=True, many=True)
    # machine = MachineSerializer(read_only=True, many=True)
    # others = OtherSerializer(read_only=True, many=True)
    # user = UserSerializer(read_only=True)

    class Meta:
        fields = ('id', 'title', 'status', 'date', 'total_amount')
        model = BuyRequest

class BuySerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'title', 'user', 'seed', 'machine', 'pest_fer', 'others')
        model = BuyRequest


import numpy as np
import GPy

DEBUG = True  # Uncomment this line for debug purposes

if DEBUG:
    import sys
    import os

    sys.path.append(os.path.abspath('Debug'))
from sequential_learning import ASGP

def c_consistence():
    n = 200
    n_var = 2

    design = np.random.uniform(size=n * n_var).reshape((-1, n_var))
    response = np.array([np.sin(np.sum(row)) for row in design])

    real_c = np.full((n_var, n_var), 1 / 8 * (3 + 2 * np.cos(2) - np.cos(4)))

    kernel = GPy.kern.RBF(n_var, lengthscale=np.array([1.0 for _ in range(n_var)]), ARD=True,
                          name="rbf") + GPy.kern.White(n_var, variance=1)
    model = GPy.models.GPRegression(design, response.reshape(-1, 1), kernel)
    model.optimize_restarts(3)
    model.optimize(messages=True)

    sigma_f = model.kern.rbf.variance.values[0]

    K = model.kern.K(design) / sigma_f
    k_inv = np.linalg.pinv(K)
    theta = model.kern.rbf.lengthscale.values
    print(theta)

    c_ = ASGP(design, response, theta, k_inv, "RBF")
    c_.compute()
    mat = c_.mat
    print(mat)
    norm = np.linalg.norm(mat - real_c)
    print("NORM:", norm)
    assert norm < 1e-3

def c_consistence_matern32():
    n = 200
    n_var = 2

    design = np.random.uniform(size=n * n_var).reshape((-1, n_var))
    response = np.array([np.sin(np.sum(row)) for row in design])

    real_c = np.full((n_var, n_var), 1 / 8 * (3 + 2 * np.cos(2) - np.cos(4)))

    lengthscale = np.array([1.0 for _ in range(n_var)])

    kernel = GPy.kern.Matern32(n_var, lengthscale=lengthscale, ARD=True,
                               name="rbf") + GPy.kern.White(n_var, variance=1)
    model = GPy.models.GPRegression(design, response.reshape(-1, 1), kernel)
    model.optimize_restarts(3)
    model.optimize(messages=True)

    sigma_f = model.kern.rbf.variance.values[0]

    print(model.kern)
    K = model.kern.K(design) / sigma_f
    k_inv = np.linalg.pinv(K)
    theta = model.kern.rbf.lengthscale.values
    print(theta)

    c_ = ASGP(design, response, theta, k_inv, "Matern32")
    c_.compute()
    mat = c_.mat
    print(mat)
    norm = np.linalg.norm(mat - real_c)
    assert norm < 1e-3

def subspace_dist(A, B):
    return np.linalg.norm(np.dot(A, A.T) - np.dot(B, B.T), ord=2)

def test_precomputed():
    from data import design, response, kinv, theta
    response, theta = map(lambda item: np.array(item), (response, theta))
    kinv = np.array(kinv).reshape(400, 400, order="F")
    design = np.array(design).reshape(-1, 2, order="F")
    true_sub = np.array((1, 1)) / np.sqrt(2)
    c_ = ASGP(design, response, theta, kinv, "Gaussian")
    c_.compute()
    sub_est = np.linalg.eig(c_.mat)[1][:, 0]
    assert subspace_dist(sub_est.reshape(1, 2), true_sub.reshape(1, 2)) < 1e-5

if __name__ == "__main__":
    c_consistence()
    # c_consistence_matern32()
    test_precomputed()


import matplotlib.pyplot as plt

# This should be the inputs of the function
AK = 10000
SR = 1000
i = 0.07
lz = 10
# Empty lists should be defined inside the function
entw_GK = []
entw_zinsen = []
entw_zinseszins = []

def spar_funktion (AK, SR, i, lz):
    for k in range (1, lz + 1):
        #ZInsen auf das bestehende Kapital
        Zinsertrag = AK * i
        #AK neu
        AK = AK + Zinsertrag + SR
        #liste hinzufügen
        entw_GK.append(round(AK, 2))
        entw_zinsen.append(round(Zinsertrag, 2))
        if k > 1:
            zinseszins = Zinsertrag * i
            entw_zinseszins.append(round(zinseszins, 2))
        else:
            zinseszins = 0
            entw_zinseszins.append(zinseszins)
    # We need a return statement that returns the lists
    # entw_GK, entw_zinsen, entw_zinseszins
    print(entw_GK)
    print(entw_zinsen)
    print(entw_zinseszins)

# spar_funktion(AK = 10000, SR = 1000, i = 0.07, lz = 10)
spar_funktion(AK, SR, i, lz)

x_werte = list(range(1, lz + 1))

gesamt_entw_plot = plt.bar(x_werte, entw_GK)
plt.xlabel("Jahre")
plt.ylabel("Kapital")
plt.show()

gesamt_zins_plot = plt.bar(x_werte, entw_zinsen)
plt.xlabel("Jahre")
plt.ylabel("Zinsen")
plt.show()


import numpy as np
#from builtins import range

boxtype = np.dtype([('x', np.float32), ('y', np.float32), ('w', np.float32), ('h', np.float32)])

def max_index(a, n): # float* and int
    if n <= 0:
        return -1
    i = 0
    max_i = 0
    max_v = a[0]
    for i in range(1, n):
        if a[i] > max_v:
            max_v = a[i]
            max_i = i
    return max_i

def resize_image(data, size):
    """
    resizes given image (as an array in which channels are given in last dimension) to given size
    """
    import numpy as np
    import scipy
    (w, h, channels) = data.shape
    (target_w, target_h) = size
    scale_w = 1.0*(w - 1)/(target_w - 1)
    scale_h = 1.0*(h - 1)/(target_h - 1)
    part = np.zeros((target_w, h, 3))
    for c in range(channels):
        arr = np.asarray([[ min(i*scale_w, w - 1) for i in range(target_w)]])
        for j in range(h):
            part[:, j, c] = scipy.ndimage.map_coordinates(data[:, j, c], arr, order=1)
    resized = np.zeros((target_w, target_h, channels))
    for c in range(channels):
        arr = np.asarray([[min(j*scale_h, h - 1) for j in range(target_h)]])
        for i in range(target_w):
            resized[i, :, c] = scipy.ndimage.map_coordinates(part[i, :, c], arr, order=1)
    return resized

def letterbox_image(img_data, size):
    """
    exactly the same as letterbox_image from image.c from darknet sources:
    resizes image (given as a RGB 0-1 float values) and fills empty space with 0.5
    """
    import numpy as np
    import scipy
    (target_w, target_h) = size
    new_w = target_w
    new_h = target_h
    (w, h, c) = img_data.shape
    if 1.0*target_w/w < 1.0*target_h/h:
        new_w = target_w
        new_h = (h*target_w)//w
    else:
        new_h = target_h
        new_w = (w*target_h)//h
    resized = resize_image(img_data, (new_w, new_h))
    assert resized.shape == (new_w, new_h, c)
    x0, y0 = (target_w - new_w)//2, (target_h - new_h)//2 # upper left corner of resized image
    resized = np.pad(
        resized,
        [
            (max(x0 - 1, 0), max(target_w - new_w - max(x0 - 1, 0), 0)),
            (max(y0 - 1, 0), max(target_h - new_h - max(y0 - 1, 0), 0)),
            (0, 0)
        ], 'constant', constant_values=0.5)
    return resized

def load_img(img_path, target_size):
    """
    loads the image the same way darknet does, processes it and returns it (as array).
    uses PIL, like keras.preprocessing.image module.
    This loads image in RGB format.
    """
    from PIL import Image as pil_image
    import keras.backend as K
    import numpy as np
    img = pil_image.open(img_path)
    # TODO: check format and convert to RGB
    #resize
    x = np.asarray(img, dtype=K.floatx())/255.0
    #print(x[0,0,0], x[1,0,0], x[0,1,0], x[1,1,0], img.mode)
    x = letterbox_image(x, target_size)
    return x

def draw_box_width(img, left, top, right, bottom, r, g, b):
    from PIL import ImageDraw
    draw = ImageDraw.Draw(img)
    draw.rectangle([left, top, right, bottom], outline=(r,g,b))
    #draw.text((10, 25-10), "world")

def draw_label(img, left, top, right, bottom, r, g, b, label):
    from PIL import ImageDraw
    draw = ImageDraw.Draw(img)
    print(repr(label))
    (w, h) = draw.textsize(label.encode())
    draw.rectangle([left, top-h, left+w, top], fill=(r,g,b))
    draw.text((left, max(top-h, 0)), label.encode(), fill=(255,255,255))

def draw_detections(img, thresh, boxes, probs, names, alphabet, classes, w=13, h=13, n=5):
    for k in range(n):
        for j in range(h):
            for i in range(w):
                class0 = max_index(probs[i, j, k, :], classes)
                prob = probs[i, j, k, class0]
                if prob <= thresh:
                    continue
                # prob > thresh
                #width = h*0.012
                #printf("%s: %.0f%%\n", names[class0], prob*100);
                print("%s: %5i%%" % (names[class0], int(prob*100)))
                offset = class0*123457 % classes
                color = ((256*(class0+1)/(classes+1))+ (class0+1)/(classes+1))*256+(class0+1)/(classes+1)+offset
                red = int(color/256/256)
                blue = int(color %256)
                green = int((color/256)%256)
                #red = get_color(2,offset,classes);
                #green = get_color(1,offset,classes);
                #blue = get_color(0,offset,classes);
                b = boxes[i, j, k]
                left = int((b["x"] - b["w"]/2.0)*img.width)
                right = int((b["x"] + b["w"]/2.0)*img.width)
                top = int((b["y"] - b["h"]/2)*img.height)
                bottom = int((b["y"] + b["h"]/2.0)*img.height)
                left = max(left, 0)
                right = min(right, img.width - 1)
                top = max(top, 0)
                bottom = min(bottom, img.height - 1)
                draw_box_width(img, left, top, right, bottom, red, green, blue)
                draw_label(img, left, top, right, bottom, red, green, blue, names[class0])
    pass


import csv

import os
from os import DirEntry
from typing import List, Dict

import exceptions as ex, logging as log
import objects.TableData as td
import values as v
from configurations.ConfigurationXml import ConfigurationXml
from exceptions import Logger

class DataReader:
	tables: Dict[str, td.TableData]

	def __init__(self):
		self.tables = {}

	def add_table(self, table: td.TableData):
		if table is None:
			return Logger.log_none_type_error('table')
		if table.filename is None:
			return Logger.log_none_type_error('filename of table')
		self.tables[table.filename] = table

	# reads files from the given directory
	def read_files(self, directory):
		counter: int = 0
		# iterate over found files in directory
		files = os.scandir(directory)

		Logger.log_info_headline1(f'READ FROM DIRECTORY: {directory}')
		for filename in files:
			# exclude sub directories from reading
			if not (os.path.isfile(os.path.join(directory, filename))):
				continue
			if not (str(filename.name).endswith('.csv')):
				ex.Logger.log_info_wrong_file_type(filename.name, '.csv')
				continue

			ex.log.info(f"Reading File:\t{filename.name}")

			with open(filename, 'r') as file:
				csv_reader = csv.reader(file, delimiter=v.Delimiters.Csv.PRIMARY, quotechar=v.Delimiters.Csv.QUOTECHAR)
				tabledata = td.TableData(filename.name)

				try:
					rows = extract_lines(csv_reader)

					# set the column names into tabledata object
					tabledata.set_columnnames(rows[0])

					# set the rest of the data
					if tabledata.add_data(rows[1:]) != 1:
						Logger.log_added_with_errors()

					# add tables to the input data
					counter += 1
					self.add_table(tabledata)

				except ex.ColumnNameError as cne:
					log.warning(cne.message)
					continue

	# reads the directory from the given configuration and executes readFiles(..) with the found directory
	def read_by_xml_config(self, configuration: ConfigurationXml):
		directory = configuration.input_directory
		self.read_files(directory)

	########################################################################
	# GETTER ###############################################################
	def get_tables(self):
		if self.tables is None:
			Logger.log_none_type_error('tables')
			return {}
		return self.tables

	def get_table_by_name(self, table_name: str) -> td.TableData:
		# check none
		if table_name is None:
			Logger.log_none_type_error('table')
			return None

		if table_name not in self.tables:
			Logger.log_key_not_found_error(table_name)
			return None

		return self.tables[table_name]

# tries to cut the given string into parts by the given primary delimiter
def get_column_names(firstrow):
	try:
		columns: List[str] = str(firstrow).split(v.Delimiters.Csv.PRIMARY)
		for x in range(len(columns)):
			columns[x] = str(columns[x]).strip()
		return columns
	except:
		return None

# extracts the rows from a given csv reader and returns them as a list
def extract_lines(reader):
	rows = []
	for r in reader:
		rows.append(r)
	return rows


"""
3. Написать программу, которая обходит не взвешенный ориентированный граф без петель, в котором все вершины связаны,
по алгоритму поиска в глубину (Depth-First Search).
Примечания:
a. граф должен храниться в виде списка смежности;
b. генерация графа выполняется в отдельной функции, которая принимает на вход число вершин.
"""
import random
from collections import deque

def create_graph(num):
    g_lst = {}  # здесь будет наш список смежности
    v_lst = [None] * num  # здесь будем накапливать список вершин, с которыми есть связь
    d = deque([el for el in range(num)])  # создадим очередь, по ней мы будем генерить связи

    for i in range(num):  # организуем цикл создания связей для каждой вершины
        n = random.randint(1, num - 1)  # узнаем сколько связей имеет данная вершина
        v_lst[i] = set()  # создаем пустое множество для заполнения списком вершин

        k = 0
        while k < n or len(v_lst[i]) == 0:  # узнаем, с какими вершинами связана данная
            vertex = random.choice(list(d)[1:])  # случайно выберем вершину для связи из очереди, исключая саму себя
            one_way = random.choice([True, False])  # зададим тип связи - односторонняя или двусторонняя
            if vertex > i:
                v_lst[i].add(vertex)  # если вершина (vertex) больше текущей, добавляем в список связи без вопросов
            elif i not in g_lst[vertex]:
                v_lst[i].add(vertex)  # для вершин, меньше текущей, при отсутствии связи - добавляем в список
            elif not one_way:
                v_lst[i].add(vertex)  # если связь уже есть, проверяем тип связи, если односторонняя - пропускаем
            elif one_way:
                    print(f'One way: вершины {i} и {vertex}. one way = {one_way}')
            k += 1

        g_lst[i] = v_lst[i]  # заполняем список смежности
        d.rotate(-1)
    return g_lst

"""
Алгоритм "Поиск в глубину"
1. Начинаем обход с точки старта.
2. Помещаем точку старта в список посещенных
3. Обходим в цикле все смежные вершины, исключая посещенные
4. Для каждого эелемента в цикле рекурсивно повторяем шаги 1 - 3, помещая в точку старта данные элемент
"""

def dfs(graph, start, visited=None):  # функция вернет список доступных вершин из точки старта
    if visited is None:
        visited = set()
    visited.add(start)  # добавляем вершину в посещенные
    for next in graph[start] - visited:  # рассматриваем только не посещенные элементы
        dfs(graph, next, visited)  # помещаем в точку старта рассматриваемый элемент, и рекурсивно вызываем функцию
        # поиска
    return visited

n = int(input('Задайте количество вершин: '))
g = create_graph(n)
print('*' * 50)
print(f'Наш ориентированный граф с количеством вершин {n}: ')
for k, v in g.items():
    print(f'{k}: {v}')

print('*' * 50)
# visited = dfs(g, 1)
print()
for i in range(n):
    print(f'Список доступных вершин из вершины {i}: {dfs(g, i)}')


"""
This module includes functions for generating continuous graphs.

Examples can be found here:
https://github.com/alexcapstick/alexs-visualisation-tools/blob/main/examples/lineplot.ipynb

"""
import numpy as np
import pandas as pd
import typing
import matplotlib.colors as mcs
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from matplotlib.lines import Line2D
from ._utils import interpolate_nans
import typing

def stackplot(
    data: pd.DataFrame,
    x: typing.Union[str, None] = None,
    y: typing.Union[str, None] = None,
    hue: typing.Union[str, None] = None,
    ax: typing.Union[plt.axes, None] = None,
    hue_order: typing.Union[typing.List[str], None] = None,
    cmap: typing.Union[mcs.Colormap, str, None] = None,
    legend: bool = True,
    cumulative: bool = False,
    **kwargs,
):
    """
    This function plots a stacked continuous graph. The
    missing values are interpolated for all x values.

    Examples
    ---------

    .. code-block::

        >>> import avt
        >>> import seaborn as sns
        >>> import pandas as pd
        >>> flights = sns.load_dataset('flights')
        >>> ax = avt.stackplot(flights, x='year', y='passengers', hue='month', cmap='Blues')

    This will return the plot:

    .. image:: figures/stackplot.png
        :width: 600
        :align: center
        :alt: Stack Plot Example

    Arguments
    ---------

    - data: pd.DataFrame:
        The data.

    - x: typing.Union[str, None], optional:
        The column name containing the x values.
        Defaults to :code:`None`.

    - y: typing.Union[str, None], optional:
        The column containing the heights.
        Defaults to :code:`None`.

    - hue: typing.Union[str, None], optional:
        Semantic variable that is mapped to determine
        the color of plot elements. This will determine
        the stacked bars.
        Defaults to :code:`None`.

    - ax: typing.Union[plt.axes, None], optional:
        A matplotlib axes that the plot can be drawn on.
        Defaults to :code:`None`.

    - hue_order: typing.Union[typing.List[str], None], optional:
        The order of the hue and stacked bars.
        Defaults to :code:`None`.

    - cmap: typing.Union[mcs.Colormap, str, None], optional:
        The colours of the plot. If a string is passed,
        this will be used to colour all of the stacked bars.
        If a cmap is passed, then this is used.
        If :code:`None`, then matplotlib handles
        the colours.
        Defaults to :code:`None`.

    - legend: bool, optional:
        Whether to plot a legend.
        Defaults to :code:`True`.

    - cumulative: bool, optional:
        If :code:`True`, then the cumulative
        values will be plotted, rather than
        the raw values.
        Defaults to :code:`False`.

    - kwargs:
        Any other keyword arguments are
        passed to :code:`plt.fill_between`. From here,
        you can change a variety of the bar
        attributes.

    Returns
    --------

    - out: plt.axes:
        The axes containing the plot.

    """

    data = data.sort_values(x)

    if not hue is None:
        if hue_order is None:
            hue_order = data[hue].unique()

        if type(cmap) == str:
            cmap = plt.get_cmap(cmap)
            cmap = [cmap(i) for i in np.linspace(0, 1, len(hue_order))]
        elif isinstance(cmap, mcs.Colormap):
            cmap = [cmap(i) for i in np.linspace(0, 1, len(hue_order))]

        data = (
            data[[x, y, hue]]
            .pivot_table(index=x, columns=hue, values=y)
            .apply(interpolate_nans, axis=0)
            .reset_index()
            .melt(id_vars=x, var_name=hue, value_name=y)
            .reset_index()
        )

        if cumulative:
            data = (
                data.groupby(by=[hue, x]).sum().groupby(level=0).cumsum().reset_index()
            )

        y_plot = np.vstack(
            [
                data[y].values[data[hue].values == hue_val].reshape(1, -1)
                for hue_val in hue_order
            ]
        )
        y_plot = np.cumsum(y_plot, axis=0)
        x_plot = np.vstack(
            [
                data[x].values[data[hue].values == hue_val].reshape(1, -1)
                for hue_val in hue_order
            ]
        )

    else:
        if type(cmap) == str:
            cmap = plt.get_cmap(cmap)
            cmap = [cmap(1)]
        elif isinstance(cmap, mcs.Colormap):
            cmap = [cmap(1)]

        hue_order = [x]
        y_plot = data[y].values.reshape(1, -1)
        x_plot = data[x].values.reshape(1, -1)
        if cumulative:
            y_plot = np.cumsum(y_plot, axis=1)

    if ax is None:
        ax = plt.gca()

    n_plots = 1 if hue_order is None else len(hue_order)

    for ng in range(n_plots):

        if not cmap is None:
            kwargs["color"] = [cmap[::-1][ng]]
        if not "edgecolor" in kwargs:
            kwargs["edgecolor"] = "white"

        ax.fill_between(
            x=x_plot[n_plots - 1 - ng, :],
            y1=y_plot[n_plots - 1 - ng, :],
            y2=0,
            label=hue_order[::-1][ng],
            **kwargs,
        )

    if legend:
        ax.legend()

    return ax

import matplotlib.ticker

def parallelplot(
    data: pd.DataFrame = None,
    x: str = None,
    y: str = None,
    hue: str = None,
    units: str = None,
    estimator: typing.Callable = np.mean,
    hue_order: typing.List[str] = None,
    order: typing.List[str] = None,
    bezier: bool = True,
    cmap: typing.Union[str, mcs.Colormap] = None,
    cbar: bool = False,
    cbar_x: float = 1.1,
    legend: bool = True,
    tick_top: bool = True,
    cbar_kwargs: typing.Dict[str, typing.Any] = {},
    legend_kwargs: typing.Dict[str, typing.Any] = {},
    ax=None,
    **kwargs,
) -> plt.Axes:
    """
    Plot a parallel plot.

    This has been edited from
    https://stackoverflow.com/a/60401570/19451559

    Examples
    ---------

    You can plot:

    .. image:: figures/parallelplot.png
        :width: 600
        :align: center
        :alt: Alternative text

    Arguments
    ---------

    - data: pd.DataFrame:
        The data to plot. Must be in long format.
        Defaults to :code:`None`.

    - x: str:
        The name of the column in :code:`data`
        that contains the categories to be
        plot along the x axis.
        Defaults to :code:`None`.

    - y: str:
        The name of the column in :code:`data`
        that contains the values to be plotted
        for each category.
        Defaults to :code:`None`.

    - hue: str:
        The name of the column in :code:`data`
        that contains the categories to be
        used to color the lines.
        Defaults to :code:`None`.

    - units: str:
        The name of the column in :code:`data`
        that contains the categories to be
        used to distinguish between lines
        that have the same :code:`hue` value.
        If not :code:`None`, then no
        estimation will be performed.
        Defaults to :code:`None`.

    - estimator: typing.Callable:
        The function to use to estimate the
        value of :code:`y` for each category.
        This is used if :code:`units` is not
        :code:`None`.
        Defaults to :code:`np.mean`.

    - hue_order: typing.List:
        The order in which to plot the
        :code:`hue` categories.
        Defaults to :code:`None`.

    - bezier: bool:
        Whether to use bezier curves to
        connect the points.
        Defaults to :code:`True`.

    - cmap: typing.Union[str, mcs.Colormap]:
        The colormap to use to color the
        lines.
        Defaults to :code:`None`.

    - cbar: bool:
        Whether to add a colorbar.
        Defaults to :code:`False`.

    - cbar_x: float:
        The x position of the colorbar.
        Defaults to :code:`1.1`.

    - legend: bool:
        Whether to add a legend.
        Defaults to :code:`True`.

    - tick_top: bool:
        Whether to put the ticks on the top
        of the plot. If :code:`False`, then
        the ticks will be on the bottom.
        Defaults to :code:`True`.

    - cbar_kwargs: typing.Dict[str, typing.Any]:
        Additional keyword arguments to pass
        to :code:`plt.colorbar`.
        Defaults to :code:`{}`.

    - legend_kwargs: typing.Dict[str, typing.Any]:
        Additional keyword arguments to pass
        to :code:`plt.legend`.
        Defaults to :code:`{}`.

    - ax: plt.Axes:
        The axes on which to plot.
        Defaults to :code:`None`.

    - **kwargs:
        Additional keyword arguments to pass
        to :code:`plt.plot` or patches.PathPatch.

    Returns
    ---------

    - axes: plt.Axes:
        The axes on which the plot was made.

    """

    if ax is None:
        fig, host = plt.subplots()
    else:
        host = ax

    variable_names = list(data[x].unique()) if order is None else order

    index_cols = []
    if hue is not None:
        hues = data[hue].unique() if hue_order is None else hue_order
        index_cols.append(hue)
        if type(cmap) == str:
            cmap = plt.get_cmap(cmap)
            cmap = [cmap(i) for i in np.linspace(0, 1, len(hues))]
        elif isinstance(cmap, mcs.Colormap):
            cmap = [cmap(i) for i in np.linspace(0, 1, len(hues))]
        else:
            cmap = [
                next(host._get_lines.prop_cycler)["color"] for _ in range(len(hues))
            ]
    else:
        if type(cmap) == str:
            cmap = plt.get_cmap(cmap)
            cmap = [cmap(1)]
        elif isinstance(cmap, mcs.Colormap):
            cmap = [cmap(1)]
        else:
            cmap = [next(host._get_lines.prop_cycler)["color"]]

    if units is not None:
        index_cols.append(units)

    data = data.pivot_table(
        index=index_cols,
        columns=x,
        values=y,
        aggfunc=estimator,
        fill_value=np.nan,
    )
    data = data.reindex(variable_names, axis=1)

    # organize the data
    ys = data.values

    if ys.shape[0] == 1:
        raise ValueError(
            "Cannot plot parallel plot with only one line. " "Please pass hue or units"
        )

    if hue is not None:
        hue_values = data.index.get_level_values(hue).values
    else:
        hue_values = np.zeros(ys.shape[0]).astype(int)

    hue_values_to_cmap = dict(zip(np.unique(hue_values), cmap))

    ymins = ys.min(axis=0)
    ymaxs = ys.max(axis=0)
    dys = ymaxs - ymins
    ymins = ymins - dys * 0.05  # add 5% padding below and above
    ymaxs = ymaxs + dys * 0.05
    dys = ymaxs - ymins

    # transform all data to be compatible with the main axis
    zs = np.zeros_like(ys)
    zs[:, 0] = ys[:, 0]
    zs[:, 1:] = (ys[:, 1:] - ymins[1:]) / dys[1:] * dys[0] + ymins[0]

    nticks = len(host.get_yticklabels())

    axes = [host] + [host.twinx() for i in range(ys.shape[1] - 1)]
    for i, ax in enumerate(axes):
        ax.grid(False)
        ax.set_ylim(ymins[i], ymaxs[i])
        ax.spines["top"].set_visible(False)
        ax.spines["bottom"].set_visible(False)
        ax.spines["right"].set_visible(True)
        # ax.yaxis.set_major_locator(matplotlib.ticker.LinearLocator(nticks))
        # ax.set_yticks(np.linspace(ymins[i], ymaxs[i], 5, endpoint=True))
        if ax != host:
            ax.spines["left"].set_visible(False)
            ax.yaxis.set_ticks_position("right")
            ax.spines["right"].set_position(("axes", i / (ys.shape[1] - 1)))

    host.set_xlim(0, ys.shape[1] - 1)
    host.set_xticks(range(ys.shape[1]))
    host.set_xticklabels(variable_names)
    host.tick_params(axis="x", which="major", pad=7)
    host.spines["right"].set_visible(False)
    host.xaxis.set_ticks_position("top" if tick_top else "bottom")

    if hue is not None:
        legend_handles = {h: None for h in hues}

    for j in range(zs.shape[0]):
        if bezier:
            # create bezier curves
            # for each axis, there will a control vertex at the point itself,
            # one at 1/3rd towards the previous and one
            # at one third towards the next axis; the first and last axis have one less control vertex
            # x-coordinate of the control vertices: at each integer (for the axes) and two inbetween
            # y-coordinate: repeat every point three times, except the first and last only twice
            verts = list(
                zip(
                    [
                        x
                        for x in np.linspace(
                            0, ys.shape[1] - 1, ys.shape[1] * 3 - 2, endpoint=True
                        )
                    ],
                    np.repeat(zs[j, :], 3)[1:-1],
                )
            )
            # for x, y in verts:
            #    host.plot(x, y, "go")  # to show the control points of the beziers
            codes = [Path.MOVETO] + [Path.CURVE4 for _ in range(len(verts) - 1)]
            path = Path(verts, codes)
            patch = patches.PathPatch(
                path,
                facecolor="none",
                edgecolor=hue_values_to_cmap[hue_values[j]],
                **kwargs,
            )
            host.add_patch(patch)
        else:
            # to just draw straight lines between the axes:
            host.plot(
                range(ys.shape[1]),
                zs[j, :],
                c=hue_values_to_cmap[hue_values[j]],
                **kwargs,
            )

        if hue is not None:
            legend_handles[hue_values[j]] = Line2D(
                [0],
                [0],
                label=hue_values[j],
                color=hue_values_to_cmap[hue_values[j]],
            )

    if cbar:
        cax = host.inset_axes(
            bounds=[cbar_x, 0, 0.05, 1.0],
        )

        norm = plt.Normalize(hue_values.min(), hue_values.max())
        sm = plt.cm.ScalarMappable(
            cmap=mcs.LinearSegmentedColormap.from_list("new_map", cmap), norm=norm
        )
        sm.set_array([])

        # Remove the legend and add a colorbar
        # ax.get_legend().remove()
        cbar = ax.figure.colorbar(sm, cax=cax, **cbar_kwargs)
        cbar.outline.set_linewidth(1)

    if legend:
        if hue is not None:
            host.legend(
                [legend_handles[h] for h in hues], hues, title=hue, **legend_kwargs
            )

    host.grid(False, axis="y")

    return host


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import code
# `site` initializes the interactive mode (defines `exit`/`quit`, sets up
# copyright notice, etc.).
import site  # pylint: disable=unused-import

from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.api_lib.util import apis_internal
from googlecloudsdk.generated_clients.apis import apis_map

################################################################################
# Consoles: Infrastructure for `gcloud meta debug`
################################################################################
_BANNER = r"""
                   _     _     _     _     _     _     _
                  / \   / \   / \   / \   / \   / \   / \
                 ( W ) ( E ) ( L ) ( C ) ( O ) ( M ) ( E )
                  \_/   \_/   \_/   \_/   \_/   \_/   \_/

                _____               ________)
               |_   _|___          (, /         /)      /)
                 | | | . |           /___, _   (/_     // ___    _
                 |_| |___|          /     (_(_/_) (_(_(/_(_)(_(_/_)_
                                 (_/
             _                 _       _      _
            | |               | |     | |    | |
   __ _  ___| | ___  _   _  __| |   __| | ___| |__  _   _  __ _  __ _  ___ _ __
  / _` |/ __| |/ _ \| | | |/ _` |  / _` |/ _ \ '_ \| | | |/ _` |/ _` |/ _ \ '__|
 | (_| | (__| | (_) | |_| | (_| | | (_| |  __/ |_) | |_| | (_| | (_| |  __/ |
  \__, |\___|_|\___/ \__,_|\__,_|  \__,_|\___|_.__/ \__,_|\__, |\__, |\___|_|
   __/ |                                                   __/ | __/ |
  |___/                                                   |___/ |___/
"""

def _PythonConsole():
  """Run a console based on the built-in code.InteractiveConsole."""
  try:
    # pylint: disable=g-import-not-at-top
    import readline
    import rlcompleter
    # pylint: disable=g-import-not-at-top
  except ImportError:
    pass
  else:
    readline.set_completer(rlcompleter.Completer(globals()).complete)
    readline.parse_and_bind('tab: complete')
  console = code.InteractiveConsole(globals())
  console.interact(_BANNER)

def _PdbConsole():
  """Run a console based on the built-in pdb."""
  import pdb  # pylint: disable=g-import-not-at-top
  pdb.set_trace()

def _IpdbConsole():
  """Run a console based on IPython's ipdb."""
  try:
    import ipdb  # pylint: disable=g-import-not-at-top
    ipdb.set_trace()
  except ImportError:
    log.error('Could not start the ipdb debugger. Please ensure that it is '
              'installed, or try the default debugger with `--mode=python`.')

def _PudbConsole():
  """Run a console based on PuDB."""
  try:
    import pudb  # pylint: disable=g-import-not-at-top
    pudb.set_trace()
  except ImportError:
    log.error('Could not start the PuDB debugger. Please ensure that it is '
              'installed, or try the default debugger with `--mode=python`.')

CONSOLES = {
    'python': _PythonConsole,
    'pdb': _PdbConsole,
    'ipdb': _IpdbConsole,
    'pudb': _PudbConsole,
}

################################################################################
# Common Cloud SDK imports
################################################################################
# pylint: disable=g-import-not-at-top
# pylint: disable=g-bad-import-order
from googlecloudsdk.core import log  # pylint: disable=unused-import
from googlecloudsdk.core import properties  # pylint: disable=unused-import
from googlecloudsdk.core.console import console_io  # pylint: disable=unused-import
from googlecloudsdk.core.util import files  # pylint: disable=unused-import
# pylint: enable=g-import-not-at-top
# pylint: enable=g-bad-import-order

################################################################################
# Pre-initialized API clients
################################################################################
def LoadApis():
  """Populate the global module namespace with API clients."""
  for api_name in apis_map.MAP:
    # pylint:disable=protected-access
    globals()[api_name] = apis.GetClientInstance(
        api_name, apis_internal._GetDefaultVersion(api_name))

def _PopulateApiNamesWithLoadMessage():
  """Make API names print instructions for loading the APIs when __repr__'ed.

  For example:

  >>> appengine
  Run `LoadApis()` to load all APIs, including this one.

  Load APIs it lazily because it takes about a second to load all APIs.
  """
  load_apis_message = (
      'Run `{0}()` to load all APIs, including this one.').format(
          LoadApis.__name__)

  class _LoadApisMessage(object):

    def __repr__(self):
      return load_apis_message

  for api_name in apis_map.MAP:
    globals()[api_name] = _LoadApisMessage()

_PopulateApiNamesWithLoadMessage()


from datetime import datetime, timedelta
from enum import Enum
from typing import List, Type

from foundation.value_objects import UserId
from pomodoros import GetTaskListByOwnerId, ProjectId, QueryTaskDto
from pomodoros.application.queries.tasks import GetRecentTasksByProjectId
from pomodoros.domain.entities import Task
from pomodoros.domain.value_objects import TaskStatus
from pomodoros_infrastructure.models import TaskModel
from pomodoros_infrastructure.repositories.tasks import SQLTaskRepository
from pony.orm import max as maximum
from pony.orm.core import Query  # noqa
from web_app.mixins import FilteredQueryMixin, PaginatedQueryMixin, SortedQueryMixin

class SQLGetRecentTasksByProjectId(GetRecentTasksByProjectId):
    @staticmethod
    def _to_query_dto(task_model: Type[TaskModel]) -> QueryTaskDto:
        return QueryTaskDto(
            id=task_model.id,
            name=task_model.name,
            status=TaskStatus(task_model.status),
            project_id=task_model.project.id,
        )

    @staticmethod
    def _is_from_most_recent_due_date(due_date: datetime, most_recent_due_date: datetime) -> bool:
        return due_date.date() == most_recent_due_date.date()

    def query(self, project_id: ProjectId, return_full_entity: bool = False) -> List[QueryTaskDto]:
        most_recent_due_date = maximum(task.due_date for task in TaskModel if task.project.id == project_id)
        if not most_recent_due_date:
            return []

        recent_tasks = TaskModel.select(
            lambda task: task.project.id == project_id
            and task.is_active
            and self._is_from_most_recent_due_date(task.due_date, most_recent_due_date)
        )

        return list(map(lambda task: self._to_query_dto(task), recent_tasks))

class DueDateFilter(Enum):
    RECENT = 0
    TODAY = 1
    TOMORROW = 2
    UPCOMING = 3

class SQLGetTaskListByOwnerId(FilteredQueryMixin, PaginatedQueryMixin, SortedQueryMixin, GetTaskListByOwnerId):
    @staticmethod
    def _to_dto(orm_task: Type[TaskModel]) -> QueryTaskDto:
        return QueryTaskDto(
            id=orm_task.id,
            name=orm_task.name,
            status=TaskStatus(orm_task.status),
            project_id=orm_task.project.id,
        )

    @staticmethod
    def _to_full_entity(orm_task: Type[TaskModel]) -> Task:
        return SQLTaskRepository.to_domain_entity(orm_task)

    @staticmethod
    def _is_owner(task_owner_id: UserId, current_user_id: UserId) -> bool:
        return task_owner_id == current_user_id

    def _get_basic_task_list(self, owner_id: UserId) -> Query:
        return TaskModel.select(lambda task: self._is_owner(task.project.owner.id, owner_id))

    def _get_recent_task_list(self, owner_id: UserId) -> Query:
        today = datetime.utcnow()
        most_recent_due_date = maximum(
            task.due_date
            for task in TaskModel
            if self._is_owner(task.project.owner.id, owner_id) and task.due_date.date() <= today.date()
        )

        if not most_recent_due_date:
            return []

        return TaskModel.select(
            lambda task: self._is_owner(task.project.owner.id, owner_id)
            and task.due_date.date() == most_recent_due_date.date()
        )

    def _get_task_list_for_today(self, owner_id: UserId) -> Query:
        today = datetime.utcnow()
        return TaskModel.select(
            lambda task: self._is_owner(task.project.owner.id, owner_id) and task.due_date.date() == today.date()
        )

    def _get_task_list_for_tomorrow(self, owner_id: UserId) -> Query:
        tomorrow = datetime.utcnow() + timedelta(days=1)
        return TaskModel.select(
            lambda task: self._is_owner(task.project.owner.id, owner_id) and task.due_date.date() == tomorrow.date()
        )

    def _get_upcoming_task_list(self, owner_id: UserId) -> Query:
        tomorrow = datetime.utcnow() + timedelta(days=1)
        return TaskModel.select(
            lambda task: self._is_owner(task.project.owner.id, owner_id) and task.due_date.date() > tomorrow.date()
        )

    def _get_task_list_for_due_date(self, owner_id: UserId, rule: DueDateFilter) -> Query:
        task_query = None

        if rule is DueDateFilter(DueDateFilter.RECENT):
            task_query = self._get_recent_task_list(owner_id)
        elif rule is DueDateFilter(DueDateFilter.TODAY):
            task_query = self._get_task_list_for_today(owner_id)
        elif rule is DueDateFilter(DueDateFilter.TOMORROW):
            task_query = self._get_task_list_for_tomorrow(owner_id)
        elif rule is DueDateFilter(DueDateFilter.UPCOMING):
            task_query = self._get_upcoming_task_list(owner_id)

        return task_query

    def query(self, owner_id: UserId, return_full_entity: bool = False, **kwargs) -> List[QueryTaskDto]:
        filter_fields: dict = kwargs.get("filter_fields", {}) or {}
        due_date_rule = filter_fields.pop("due_date_rule", None)

        if due_date_rule is None:
            task_query = self._get_basic_task_list(owner_id=owner_id)
        else:
            task_query = self._get_task_list_for_due_date(owner_id=owner_id, rule=due_date_rule)

        task_query = self.get_paginated_query(self.get_sorted_query(self.get_filtered_query(task_query, filter_fields)))

        if return_full_entity:
            return list(map(lambda task: self._to_full_entity(task), task_query))
        return list(map(lambda task: self._to_dto(task), task_query))