from datetime import datetime
import os

# import common methods for use in recipe from common.py
# (so other recipe can import from spack.pkg.k4.key4hep_stack)
# (which is the most convenient way to make that code available
#  without creation of a new module
import sys

sys.path.append(
    os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "key4hep-stack"
    )
)
from common import *

class Key4hepStackTest(BundlePackage, Key4hepPackage):
    """Bundle package to install the Key4hep software stack (test version)."""

    homepage = "https://cern.ch/key4hep"

    ##################### versions ########################
    #######################################################
    ###  nightly builds
    version("master")
    # this version can be extended with additional version
    # fields to differentiate it, like 'master-2020-10-10'
    #
    ### stable builds
    # builds the latest release of each package
    # the preferred usage is to use the date as version, like:
    version(datetime.today().strftime("%Y-%m-%d"))
    # version('2020-10-06') # example, no need to add here.
    # more complex version configurations should be added in an
    # environment

    # this bundle package installs a custom setup script, so
    # need to add the install phase (which normally does not
    # exist for a bundle package)
    phases = ["install"]

    variant(
        "build_type",
        default="Release",
        description="CMake build type",
        values=("Debug", "Release", "RelWithDebInfo", "MinSizeRel"),
    )

    # depends_on("")

    def setup_run_environment(self, env):
        # set locale to avoid certain issues with xerces-c
        # (see https://github.com/key4hep/key4hep-spack/issues/170)
        env.set("LC_ALL", "C")
        env.set("KEY4HEP_STACK", os.path.join(self.spec.prefix, "setup.sh"))

        # this fixes loading the local emacs, see https://github.com/key4hep/key4hep-spack/issues/486
        env.unset("XDG_DATA_DIRS")

        # set vdt, needed for root, see https://github.com/spack/spack/pull/37278
        if "vdt" in self.spec:
            env.prepend_path("CPATH", self.spec["vdt"].prefix.include)

        # remove when https://github.com/spack/spack/pull/37881 is merged
        if "podio" in self.spec:
            env.prepend_path("LD_LIBRARY_PATH", self.spec["podio"].libs.directories[0])
        if "edm4hep" in self.spec:
            env.prepend_path(
                "LD_LIBRARY_PATH", self.spec["edm4hep"].libs.directories[0]
            )
        if "lcio" in self.spec:
            env.prepend_path("LD_LIBRARY_PATH", self.spec["lcio"].libs.directories[0])

        # remove when https://github.com/spack/spack/pull/38015 is merged
        if "dd4hep" in self.spec:
            env.prepend_path("LD_LIBRARY_PATH", self.spec["dd4hep"].prefix.lib)
            env.prepend_path("LD_LIBRARY_PATH", self.spec["dd4hep"].prefix.lib64)

        # remove when https://github.com/spack/spack/pull/38407 is merged
        if "edm4hep" in self.spec:
            env.prepend_path("PYTHONPATH", self.spec["edm4hep"].prefix.python)

        # Issue on ubuntu, whizard fails to load libomega.so.0
        if self.compiler.operating_system == "ubuntu22.04":
            if "whizard" in self.spec:
                env.prepend_path(
                    "LD_LIBRARY_PATH", self.spec["whizard"].libs.directories[0]
                )

    def install(self, spec, prefix):
        return install_setup_script(self, spec, prefix, "K4_LATEST_SETUP_PATH")


from manim import *

# 1. 2-dimensional vector space and two vectors inside of it
# 2. show how vector addition looks like
# 3. Properties of Linear Algebra in a nice text visual
# 4. Inner Product
# 5. Norms
# 6. Outer product
# 7. Vector to Matrix characteristics and special matrices
## such as Transpose, Identity, Diagonal, Row Echelon Form
# 8. Determinant
# 9. Matrix inversion
# 10. Characteristic equation
# DONE
class LinAlgebra(VectorScene):
    def construct(self):
        # Scene 1

        plane = NumberPlane()

        vec_1 = Vector([1, 2])
        vec_2 = Vector([-3, -2], color=YELLOW)
        vec_3 = Arrow(start=[-3, -2, 0], end=[1,2,0], color=RED)
        label_1 = vec_1.coordinate_label()
        label_2 = vec_2.coordinate_label(color=YELLOW)

        self.play(Create(plane))
        self.play(Create(vec_1), FadeIn(label_1))
        self.play(Create(vec_2), FadeIn(label_2))

        v1 = Tex(r"\[a = \begin{bmatrix} 1 \\ 2 \end{bmatrix}\]")
        v2 = Tex(r" \[+\begin{bmatrix} -3 \\ -2 \end{bmatrix}\]").next_to(v1, RIGHT)
        res = Tex(r" \[= \begin{bmatrix} -2 \\ 0 \end{bmatrix}\]", color=RED).next_to(v2, RIGHT)

        self.wait(5)

        self.next_section("Now vector addition")

        titulo = Title("Vector Addition")

        labels = VGroup(v1, v2, res)

        self.play(Write(titulo), Create(labels.to_corner(UL*2).scale(0.65)))
        self.play(Create(vec_3))

        vectores = VGroup(vec_1, vec_2, vec_3)

        self.wait(5)

        self.remove(titulo)

        self.next_section("Direction")
        # recrea el ejemplo del articulo
        titulo2 = Title("Direction of vector after addition")

        vec_4 = Arrow(start=[2, 1, 0], end=[0,0,0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_5 = Arrow(start=[0,0,0], end=[2, -1, 0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_6 = Arrow(start=[2, 1, 0], end=[2, -1, 0], color=GREEN)
        label_a = Tex("a").next_to(vec_4, UP*0.3).scale(0.8)
        label_b = Tex("b").next_to(vec_5, DOWN*0.3).scale(0.8)
        label6 = Tex("a + b").next_to(vec_6, RIGHT)

        vectores2a = VGroup(vec_4, vec_5, vec_6, label6, label_a, label_b)

        # titulo, eliminar cosas viejas
        self.play(ReplacementTransform(labels, titulo2), Uncreate(VGroup(label_1, label_2, plane)))
        # reemplazar grupo vectores por los tres nuevos ejemplos
        self.play(Uncreate(vectores), Create(vectores2a))
        self.play(vectores2a.animate.move_to(LEFT*2))

        vec_7 = Arrow(start=[0, 0, 0], end=[2,1,0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_8 = Arrow(start=[0,0,0], end=[2, -1, 0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_9 = Arrow(start=[2, 1, 0], end=[2, -1, 0], color=GREEN)
        label_a2 = Tex("a").next_to(vec_7, UP*0.3).scale(0.8)
        label_b2 = Tex("b").next_to(vec_8, DOWN*0.3).scale(0.8)
        label9 = Tex("b - a").next_to(vec_9, RIGHT)

        vectores2b = VGroup(vec_7, vec_8, vec_9, label9, label_a2, label_b2)

        self.play(Create(vectores2b))
        self.play(vectores2b.animate.move_to(RIGHT*2))

        vec_10 = Arrow(start=[0, 0, 0], end=[2,1,0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_11 = Arrow(start=[0,0,0], end=[2, -1, 0], color = BLUE, max_tip_length_to_length_ratio=0.4)
        vec_12 = Arrow(start=[2, -1, 0], end=[2, 1, 0], color=GREEN)
        label_a3 = Tex("a").next_to(vec_11, DOWN*0.3).scale(0.8)
        label_b3 = Tex("b").next_to(vec_10, DOWN*0.3).scale(0.8)
        label12 = Tex("a - b").next_to(vec_12, RIGHT)

        vectores2c = VGroup(vec_10, vec_11, vec_12, label12, label_a3, label_b3)

        self.play(Create(vectores2c.shift(DOWN*2)))
        self.play(vectores2c.animate.move_to(LEFT + DOWN*2))

        self.wait(5)

        self.clear()

        self.next_section("List of properties")

        titulo3 = Title("Properties of Vector Operations")

        # commutativity
        c = Tex("Commutativity: ", color = GREEN)
        cm = MathTex(r"a + b = b + a; a \cdot b = b \cdot a").next_to(c, RIGHT)
        vc = VGroup(c, cm).scale(0.75).next_to(titulo3, DOWN).shift(2*LEFT + DOWN)

        # Associativity
        a = Tex("Associativity: ", color = GREEN)
        am = MathTex(r"a + (b+c) = (a+b) + c; a \cdot (b \cdot c) = (a \cdot b) \cdot c").next_to(a, RIGHT)
        va = VGroup(a, am).scale(0.75).next_to(vc, DOWN)

        # Zero element
        z = Tex("Zero element: ", color = GREEN).next_to(a, DOWN)
        zm = MathTex(r"a + 0 = a").next_to(z, RIGHT)
        zt = Tex("Where 0 is called a zero vector.").next_to(zm, RIGHT)
        vz = VGroup(z, zm, zt).scale(0.75).next_to(va, DOWN)

        # Distributibity
        d = Tex("Distributibity: ", color = GREEN).next_to(z, DOWN)
        dm = MathTex(r"a \cdot (b + c) = (a \cdot b) + (a \cdot c)").next_to(d, RIGHT)
        vd = VGroup(d, dm).scale(0.75).next_to(vz, DOWN)

        # Additive Identity
        aid = Tex("Additive Identity: ", color = GREEN).next_to(d, DOWN)
        aidm = MathTex(r"1 \cdot a = a").next_to(aid, RIGHT)
        aidt = Tex("Where 1 is the identity element.").next_to(aidm, RIGHT)
        vaid = VGroup(aid, aidm, aidt).scale(0.75).next_to(vd, DOWN)

        # Additive Inverse
        ai = Tex("Additive Inverse: ", color = GREEN).next_to(aid, DOWN)
        aim = MathTex(r" a + (-a) = 0").next_to(ai, RIGHT)
        vai = VGroup(ai, aim).scale(0.75).next_to(vaid, DOWN)

        self.play(Write(titulo3))

        properties = VGroup(vc, va, vz, vd, vaid, vai).arrange(DOWN)
        self.play(Create(properties))

        self.play(vc.animate.shift(LEFT), vd.animate.shift(LEFT), vai.animate.shift(LEFT*2))

        self.play(properties.animate.scale(1.2))

        self.wait(5)

        self.clear()

        self.next_section("Inner Product")

        titulo4 = Title("Inner (Dot) Product")

        self.play(Write(titulo4))

        vect1 = Matrix([[1, 3, 5]]).shift(LEFT*2)
        dot = Dot().next_to(vect1, RIGHT)
        vect2 = Matrix([[2,], [4,], [6,]]).next_to(dot, RIGHT)

        vectGroup = VGroup(vect1, dot, vect2)

        self.play(FadeIn(vectGroup), run_time=4)

        self.wait()

        equal = Text(" = ").next_to(vectGroup, RIGHT)
        prod = MathTex(r"1 \cdot 2 + 3 \cdot 4 + 5 \cdot 6").next_to(equal, RIGHT)

        self.play(Create(equal), Write(prod))

        self.wait(2)

        text = Text("Scalar", font_size=22, color=GREEN).next_to(prod, DOWN)

        self.play(Create(text))

        self.wait(3)

        lenVec = MathTex(r"| a | = \sqrt{a_i \cdot a_i} = \sqrt{a^2_1 + a^2_2 + ... + a^2_i").scale(0.85)

        self.play(Uncreate(equal), Uncreate(prod), Uncreate(text))
        self.play(ReplacementTransform(vectGroup, lenVec))

        self.wait(5)

        cos = MathTex(r"cos(\Theta) = \frac{a \cdot b}{|a| \cdot |b|}").scale(0.85)

        self.play(ReplacementTransform(lenVec, cos))

        self.wait(5)

        self.clear()

        self.next_section("Outer product")

        titulo5 = Title("Outer (Matrix) Product")

        vect3 = Matrix([["a1",], ["a2",], ["a3",]]).shift(LEFT*2)
        tensorProd = MathTex(r"\otimes").next_to(vect3, RIGHT)
        vect4 = Matrix([["b1","b2","b3"]]).next_to(tensorProd, RIGHT)

        label3 = Tex(r"n X 1").next_to(vect3, DOWN)
        label4 = Tex(r"1 X n").next_to(vect4, DOWN)

        equal2 = Tex(" = ").next_to(vect4, RIGHT)

        resProd = Matrix([["a1b1", "a1b2", "a1b3"],
                          ["a2b1", "a2b2", "a2b3"],
                          ["a3b1", "a3b2", "a3b3"]]).next_to(equal2, RIGHT)

        labelRes = Tex(r"n X n").next_to(resProd, DOWN)

        labelGroup = VGroup(label3, label4, labelRes).scale(0.7).shift(LEFT*2)
        vectGroup2 = VGroup(vect3, tensorProd, vect4, equal2, resProd).shift(LEFT*2).scale(0.8)

        self.play(Create(titulo5))

        self.play(Create(vectGroup2), Write(labelGroup))
        self.play(labelGroup.animate.set_color(GREEN))

        self.wait(3)

        self.clear()

        self.next_section("Characteristics and Special Matrices")

        titulo6 = Title("Characteristics and Special Matrices")
        rank = Text("Rank = maximum number of linearly independent rows/columns in that matrix",
                    t2s={'Rank':ITALIC}, t2w={'Rank':BOLD},
                    font_size=24)
        LD = Text("Linearly Independent = cannot be represented as a linear combination of\n any other vector in the matrix",
                    t2s={'Linearly Independent':ITALIC}, t2w={'Linearly Independent':BOLD},
                    font_size=24)

        initGroup = VGroup(titulo6, LD.to_edge(DOWN), rank.next_to(LD, UP))

        scalar = Text("Scalar = Tensor of Rank 0", t2c={'Scalar':GREEN, '0':GREEN}, font_size=34)
        vector = Text("Vector = Tensor of Rank 1", t2c={'Vector':YELLOW, '1':YELLOW}, font_size=34)
        matrix = Text("Matrix = Tensor of Rank 2", t2c={'Matrix':BLUE, '2':BLUE}, font_size=34)
        tensor = Text("Tensors = Tensor of Rank n>2", t2c={'Tensors':RED, 'n>2':RED}, font_size=34)

        self.play(Create(initGroup))

        self.play(Write(scalar))

        self.wait(2)

        self.play(scalar.animate.next_to(titulo6, DOWN*2), Write(vector))

        self.wait(2)

        self.play(vector.animate.next_to(scalar, DOWN), Write(matrix))

        self.wait(2)

        self.play(matrix.animate.next_to(vector, DOWN), Write(tensor))

        self.wait(2)

        ranksGroup = VGroup(scalar, vector, matrix)
        self.play(ranksGroup.animate.next_to(tensor, UP))

        fullGroup = VGroup(LD, rank, ranksGroup, tensor)

        self.wait(5)

        titulo7 = Title("Special Matrices")
        self.play(ReplacementTransform(fullGroup, titulo7))

        colors = [YELLOW, BLUE, PURPLE_A]

        tr_text = Text("Transpose of A", t2c={"A":GREEN}, font_size=36, slant=ITALIC).next_to(titulo7, DOWN).to_edge(LEFT)

        a_symbol = MathTex(r"A = ", tex_to_color_map={"A":GREEN}).next_to(tr_text, DOWN).shift(DOWN)
        a_matrix = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], left_bracket="[", right_bracket="]").next_to(a_symbol, RIGHT)
        for i in range(3):
            columns = a_matrix.get_columns()
            rect = SurroundingRectangle(columns[i], color=colors[i], fill_color=colors[i], fill_opacity=0.5).shift(DOWN)
            self.add(rect)

        tr_matrix = Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]], left_bracket="[", right_bracket="]").next_to(a_matrix, RIGHT)
        tr_symbol = MathTex(r"A^T = ", tex_to_color_map={"A":GREEN, "T":RED}).next_to(tr_matrix, LEFT)
        for i in range(3):
            rows = tr_matrix.get_rows()
            rect = SurroundingRectangle(rows[i], color=colors[i], fill_color=colors[i], fill_opacity=0.5).shift(DR+RIGHT)
            self.add(rect)

        orig = VGroup(a_symbol, a_matrix).shift(DOWN)
        transpose = VGroup(tr_text, tr_symbol, tr_matrix).shift(DR+RIGHT)
        self.add(orig, transpose)

        self.wait(4)

        idText = Text("Identity Matrix I", t2c={"I":GREEN}, font_size=42, slant=ITALIC).next_to(titulo7, DOWN).to_edge(LEFT)

        idSymbol = MathTex(r"I = ", tex_to_color_map={"I":GREEN}).next_to(idText, DOWN).shift(DOWN)
        idMatrix = IntegerMatrix([[1, 0], [0, 1]], left_bracket="[", right_bracket="]").next_to(idSymbol, RIGHT)

        identity = VGroup(idText, idSymbol, idMatrix).shift(DOWN)

        self.play(ReplacementTransform(transpose, identity))

        self.wait(4)

        diText = Text("Diagonal Matrix Dg", t2c={"Dg":GREEN}, font_size=42, slant=ITALIC).next_to(titulo7, DOWN).to_edge(LEFT)

        diSymbol = MathTex(r"Dg = ", tex_to_color_map={"Dg":GREEN}).next_to(diText, DOWN).shift(DOWN)
        diMatrix = Matrix([["n", 0], [0, "m"]]).next_to(diSymbol, RIGHT)

        diagonal = VGroup(diText, diSymbol, diMatrix).shift(DOWN)

        self.play(ReplacementTransform(identity, diagonal))

        self.wait(4)

        title = Text("Reducing a Matrix to Echelon Form", font_size=36, slant=ITALIC).to_edge(UP)
        self.add(title)

        original_matrix_text = Text("Original Matrix", t2c={"Original Matrix":GREEN}, font_size=36, slant=ITALIC).next_to(title, DOWN).to_edge(LEFT)
        original_matrix = Matrix([[3, 7, 4], [2, 8, 9], [6, 5, 7]], left_bracket="[", right_bracket="]").next_to(original_matrix_text, DOWN).to_edge(LEFT)

        original_group = VGroup(original_matrix_text, original_matrix).shift(DOWN)

        self.play(Write(original_group.shift(UP*0.7)))

        self.wait(2)

        reduced_matrix_text = Text("Echelon Matrix", t2c={"Echelon Matrix":GREEN}, font_size=36, slant=ITALIC).next_to(title, DOWN).to_edge(RIGHT)
        reduced_matrix = Matrix([[3, 7, 4], [0, 2, 5], [0, 0, 1]], left_bracket="[", right_bracket="]").next_to(reduced_matrix_text, DOWN).to_edge(RIGHT)

        reduced_group = VGroup(reduced_matrix_text, reduced_matrix).next_to(original_group, RIGHT)

        self.play(FadeIn(reduced_group.shift(DOWN*0.5)))

        self.wait(2)

        echelon_matrix_text = Text("Reduced\n Echelon Matrix", t2c={"Reduced\n Echelon Matrix":GREEN}, font_size=36, slant=ITALIC).next_to(title, DOWN).to_edge(RIGHT)
        echelon_matrix = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], left_bracket="[", right_bracket="]").next_to(echelon_matrix_text, DOWN).to_edge(RIGHT)

        echelon_group = VGroup(echelon_matrix_text, echelon_matrix).next_to(reduced_group, RIGHT)

        self.play(Write(echelon_group.shift(DOWN*0.2)))

        self.wait(2)

        rre_group = VGroup(original_group, reduced_group, echelon_group)

        self.play(rre_group.animate.move_to(ORIGIN))

        self.wait(5)

        self.clear()

        self.next_section("Determinant")

        titulo8 = Title("Determinant")

        self.play(Write(titulo8))

        A = Text("A = ").next_to(titulo8, DOWN).to_edge(LEFT).shift(DOWN)

        matrixA = Matrix([
            [2, 0],
            [-1, 1]
        ]).next_to(A, RIGHT)

        detText = Text("det A = ").next_to(A, DOWN).shift(DOWN)
        calcDet = MathTex(r"((2 \cdot 1) - (0 \cdot (-1)) = 3").next_to(detText, RIGHT)

        det = get_det_text(matrixA,
                    determinant=3,
                    initial_scale_factor=2).scale(0.8)

        detGroup = VGroup(A, matrixA, calcDet, detText)

        self.play(Create(detGroup))

        self.play(detGroup.animate.move_to(ORIGIN))
        self.play(detGroup.animate.scale(0.8))

        self.wait(3)
        self.remove(matrixA, A)

        self.play(VGroup(calcDet, detText).animate.shift(UP))

        self.play(FadeIn(matrixA.next_to(calcDet, DOWN)))

        self.play(FadeIn(det.next_to(calcDet, DOWN)))

        self.play(matrixA.animate.shift(RIGHT * 0.24))

        self.wait(5)

        self.clear()

        self.next_section("Matrix Inversion")

        titulo9 = Title("The Inverse of a Matrix")

        A = Tex(r"A = ", tex_to_color_map ={"A":GREEN}).move_to(ORIGIN)
        mat = Matrix([["a", "b"], ["c", "d"]]).next_to(A, RIGHT)

        invA = MathTex(r"A^{-1} = \frac{1}{(a \cdot d) - (b \cdot c)}", tex_to_color_map ={r"A^{-1}":RED})
        invMat = Matrix([["a", "-b"], ["-c", "a"]]).next_to(invA, RIGHT)

        condition = MathTex(r"A \cdot A^{-1} = ", tex_to_color_map ={"A ":GREEN, r"A^{-1}":RED}).move_to(ORIGIN).shift(DOWN)
        idMatrix = Matrix([[1, 0], [0, 1]]).next_to(condition, RIGHT)
        idSymbol = Tex(r" = I").next_to(idMatrix, RIGHT)

        self.play(Write(titulo9))

        group1 = VGroup(A, mat)

        self.play(Create(group1))
        self.wait(3)
        self.play(group1.animate.next_to(titulo9, DOWN).to_edge(LEFT))

        group2 = VGroup(invA, invMat)

        self.play(Create(group2))
        self.wait(4)
        self.play(group2.animate.next_to(mat, RIGHT).to_edge(RIGHT))

        group3 = VGroup(condition, idMatrix, idSymbol)

        self.play(Create(group3))
        self.play(group3.animate.shift(LEFT))

        self.wait(3)

        bigGroup = VGroup(group1, group2, group3)
        self.play(bigGroup.animate.shift(DOWN))

        self.wait(5)

        self.clear()

        self.next_section("Eigen stuff")

        titulo10 = Title("Some Eigen Stuff")

        ax = Axes()
        plane = NumberPlane()

        self.add(titulo10, ax)

        vec1 = Vector([3, 0], color=GREEN)
        vec2 = Vector([1, 2], color=RED)

        eigenvector = Vector([1, -1], color=YELLOW)

        initGroup = VGroup(plane, vec1, vec2, eigenvector).scale(0.8)

        self.play(Create(initGroup))

        line1 = Line(start=[-5,5,0], end=[5, -5, 0], color=YELLOW)

        line2 = Line(start=[-6,0,0], end=[6, 0, 0], color=GREEN)

        line3 = Line(start=[-4,-8,0], end=[4, 8, 0], color=RED)

        lines = VGroup(line1, line2, line3)

        self.play(Create(lines))

        matrix = [[3, 1],
                  [0, 2]]
        self.play(ApplyMatrix(matrix, initGroup))

        self.wait(5)


#!/usr/bin/env python

"""
Creates a gpx file from a FAAM core netcdf data file.

GPX_ files are a standard xml-format, which is understood by many programs.
The gpx file from a flight can for example be used to georeference photographs
taken during a flight (e.g. gpscorrelate_).

The geotagged images can then in a next step be overlaid on maps using the
GPS information from the image metadata.

.. _GPX: https://en.wikipedia.org/wiki/GPS_Exchange_Format
.. _gpscorrelate: https://github.com/freefoote/gpscorrelate

"""

import datetime
import netCDF4
import os
import sys

from matplotlib.dates import num2date

from faampy.core.utils import get_mpl_time, get_base_time, get_fid

_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%SZ'

_GPX_HEADER = """<?xml version="1.0"?>
<gpx
 version="1.0"
 creator="ExpertGPS 1.1 - http://www.topografix.com"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.topografix.com/GPX/1/0"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
"""

_GPX_FOOTER = "</gpx>\n"

_GPX_RTE_TEMPLATE = "<rte>%s</rte>\n"

_GPX_RTEPT_TEMPLATE = """<rtept lat="%f" lon="%f">
 <ele>%f</ele>
 <time>%s</time>
</rtept>
"""

_GPX_TRK_TEMPLATE_START = "<trk><trkseg>\n"
_GPX_TRK_TEMPLATE_END = "</trkseg></trk>\n"

_GPX_TRKPT_TEMPLATE = """<trkpt lat="%f" lon="%f">
 <ele>%f</ele>
 <time>%s</time>
</trkpt>
"""

def lonlatalt_to_gpx(lon, lat, alt, *args):
    if args:
        timestamp = [t.strftime(_TIMESTAMP_FORMAT) for t in args[0]]
    else:
        now = datetime.datetime.utcnow()
        timestamp = [(now+datetime.timedelta(seconds=i)).strftime(_TIMESTAMP_FORMAT) for i in range(len(lon))]
    result = ""
    result += _GPX_HEADER
    result += _GPX_TRK_TEMPLATE_START
    trkp = [_GPX_TRKPT_TEMPLATE % (lat[i], lon[i], alt[i], timestamp[i]) for i in range(len(lat))]
    result += ''.join(trkp)
    result += _GPX_TRK_TEMPLATE_END
    result += _GPX_FOOTER
    return result

def nc_to_gpx(ncfile, outpath):
    ds = netCDF4.Dataset(ncfile, 'r')
    lon = ds.variables['LON_GIN'][:, 0]
    lat = ds.variables['LAT_GIN'][:, 0]
    alt = ds.variables['ALT_GIN'][:, 0]
    t = num2date(get_mpl_time(ds))

    outfilename = '%s_%s.gpx' % (get_fid(ds), get_base_time(ds).strftime('%Y%m%d'))
    outfile = os.path.join(outpath, outfilename)
    outtxt = lonlatalt_to_gpx(lon, lat, alt, t)
    f = open(outfile, 'w')
    f.write(outtxt)
    ds.close()
    f.close()

def _argparser():
    import argparse
    from argparse import RawTextHelpFormatter
    sys.argv.insert(0, 'faampy nc_to_gpx')
    parser = argparse.ArgumentParser(prog = 'faampy nc_to_gpx', description=__doc__,
                                     formatter_class=RawTextHelpFormatter)
    parser.add_argument('ncfile',
                        action="store",
                        type=str,
                        help='input netCDF-file')
    parser.add_argument('outpath',
                        action="store",
                        nargs='?',
                        type=str,
                        default=os.path.expanduser('~'),
                        help='file name of output file')
    return parser

def main():
    parser = _argparser()
    args = parser.parse_args()
    nc_to_gpx(args.ncfile, args.outpath)

if __name__ == '__main__':
    main()


def submitAppPayLoad():
    body = {
    "meta": {
        "bm_request_id": "01c00add-2f6b-4ff5-a6b3-0f5240e7e9b9",
        "bm_session_id": "01c00add-2f6b-4ff5-a6b3-0f5240e7e9b9",
        "app_version": "123",
        "client_source": "akshat_local_host",
        "bright_uid": "f16800c5-7539-4617-900c-cff63e44e2f6"
    },
    "data": {
        "product": "CREDIT_CARD_SECURED_V1",
        "auth_signals": {
            "fingerprint": [],
            "authStatement": "I have reviewed the Bright line of credit agreement and, if approved, agree to the terms and conditions and the Bright credit line agreement. I authorize Bright to obtain my consumer report and understand my credit bureau file will show this as a hard inquiry."
        },
        "loan_version": "v1",
        "application_data": {
            "income": 65000,
            "questions": {
                "occupation": "Retired"
            }
        },
        "refi_funnel_treatment": "legacy"
        }
    }
    return body

def createAccPayLoad():
    body = {

    }
    return body

def activateCardPayLoad():
    body = {

    }
    return body

def blockCardPayLoad():
    body = {

    }
    return body


from django.shortcuts import redirect, render,get_object_or_404
from django.urls import reverse
from django.db.models import Q

from .models import Category, Product, SubCategory
from .utils import min_max_filter,get_paginated

def home(request):
    products = Product.objects.filter().order_by("-rating").all()[:12]

    context = {
        "products": products,
    }
    return render(request, "index.html",context)

def broad_search(items, word):
    return items.filter(Q(title__containes=word)| Q(description__contains=word))

def store(request):
    products = Product.objects.all()

    word = request.GET.get('q',None)
    if word:
        products = broad_search(request, word)

    paginated = get_paginated(request, products, 3)

    context = {
        "products":paginated["items"],
        "pages":paginated["pages"]
    }
    return render(request,"store.html",context)

def category_products(request, category_slug):
    category = get_object_or_404(Category,slug=category_slug)
    products = Product.objects.filter(sub_category__category=category)
    products = min_max_filter(request, products)
    paginated = get_paginated(request, products, 3)
    context = {
        "products":paginated["items"],
        "pages":paginated["pages"]
    }
    return render(request,"store.html",context)

def sub_category_products(request, category_slug , sub_category_slug):
    category = get_object_or_404(Category, slug=category_slug)
    subcategory = get_object_or_404(SubCategory,slug=sub_category_slug,category=category)
    products = Product.objects.filter(sub_category=subcategory)
    paginated = get_paginated(request, products, 3)
    context = {
        "products":paginated["items"],
        "pages":paginated["pages"]
    }
    return render(request,"store.html",context)

def product_detail(request,slug):
    products = Product.objects.filter(slug=slug)
    if not products.exists():
        return redirect(reverse("home-page"))
    else:
        product = products.first()

    context = {
        "product": product
    }
    return render(request,"product_detail.html",context)


import requests
import os

SHEETY_URL = os.environ.get("SHEETY_URL")
headers = {
    "Authorization": os.environ.get("headers")
}

class FetchItems:

    def __init__(self):
        self.items = []

    def get_items(self):
        response = requests.get(SHEETY_URL, headers=headers)
        response = response.json()['menuItems']
        items = [index for index in response]
        self.items = items


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from Product.models import Product, Category

posts = [
    {
        'author': 'holey',
        'title': 'shit',
        'description': 'is',
        'text': 'that shit!?'
    },
    {
        'author': 'that',
        'title': 'is',
        'description': 'unbelievable',
        'text': 'shit!'
    }
]

@csrf_exempt
def index(request):
    if request.method == 'POST':
        return JsonResponse({'profile': posts})
    return HttpResponse("Hello, world. You're at the /api index page.")

def products(request):
    if request.method == 'GET':
        products = Product.objects.all()
        return JsonResponse({
            'products': list(products.values())
        })

@csrf_exempt
def categories(request):
    if request.method == 'GET':
        categories = Category.objects.all()
        return JsonResponse({
            'categories': list(categories.values())
        })
    if request.method == 'POST':
        name = request.GET.get("category_name", None)
        if not name:
            return JsonResponse({"status": False, "msg": "Bad name param"})

        new_category = Category()
        new_category.category_text = name

        existed_category = Category.objects.filter(category_text=name)

        if len(existed_category) > 0:
            return JsonResponse({"status": False, "msg": "Item with that name already exist"})
        else:
            new_category.save()
            return JsonResponse({
                "status": True,
                "msg": "Item was added",
                'Id': new_category.id
            })

@csrf_exempt
def find_categories(request):
    if request.method == 'POST':
        name = request.GET.get("category_name", None)
        queryset = Category.objects.filter(category_text__icontains=name).values()
        return JsonResponse({
            'category': list(queryset)
        })

@csrf_exempt
def category(request, category_id):
    if request.method == 'GET':
        queryset = Category.objects.filter(id=category_id).values()
        return JsonResponse({
            'category': list(queryset)
        })

    elif request.method == 'POST':
        name = request.GET.get("category_name", None)
        if not name:
            return JsonResponse({"status": False, "msg": "Bad name param"})

        categories = Category.objects.filter(id=category_id)

        if len(categories) <= 0:
            return JsonResponse({"status": False, "msg": "Item with that id not exist"})

        for cat in categories:
            cat.category_text = name
            cat.save()

        return JsonResponse({"status": True, "msg": "Item was updated"})

    elif request.method == 'DELETE':
        categories = Category.objects.filter(id=category_id)

        if len(categories) <= 0:
            return JsonResponse({"status": False, "msg": "Item with that id not exist"})

        for cat in categories:
            cat.delete()

        return JsonResponse({"status": True, "msg": "Item was deleted"})


#!/usr/bin/python
# -*- coding: utf-8 -*-

# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE or copy at http://www.boost.org/LICENSE

from lxml import etree, html
from object.constant import reduce_constants, print_constants
from object.function import reduce_functions, print_functions
from parser.dotspec import dotspec_constant_parser, dotspec_function_parser
from parser.dottm import dottm_parser
from printer.cpp import cpp_printer
from resolver import resolver
from utils import log, option_parser
import re
from printer.basic import basic_printer
from parser.header import header_parser

parser = option_parser()
(o, a) = parser.parse_args()

# Registering the Resolver as the default DTD Resolver
xml_parser = etree.XMLParser(load_dtd=True)
xml_parser.resolvers.add(resolver())
etree.set_default_parser(xml_parser)

class source(object):
  def __init__(self, library_name, parser, **kwargs):
    self.library_name = library_name
    self.parser = parser(self.library_name)
    self.files = []

    if 'index' in kwargs:
      local_file = resolver.cache(kwargs['index'])
      root = html.parse(local_file)
      uris = root.xpath(kwargs['xpath'])

      self.files.extend([ kwargs['base'] + f for f in filter(lambda x : re.match(kwargs['regexp'], x), uris) ])
      self.files.sort()
    else:
      self.files = kwargs['files']

    for f in self.files:
      log.debug(self.library_name + ': ' + f)

  def parse(self):
    for url in self.files:
      local_file = resolver.cache(url)
      self.parser.parse(local_file)

sources = []

# 'base' and 'index' shoud be the same in 99% of cases,
# but it's faster to download .xml on http rather than webdav over https
#sources.append(source('gl2', docbook_parser(),
#    regexp=r'^gl[A-Z][a-zA-Z0-9]*\.xml$',
#    xpath='//file/@name',
#    index='https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man/',
#    base='http://www.opengl.org/sdk/docs/man/'))
#
#sources.append(source('gl3', docbook_parser(),
#    regexp=r'^gl[A-Z][a-zA-Z0-9]*\.xml$',
#    xpath='//file/@name',
#    index='https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man3/',
#    base='http://www.opengl.org/sdk/docs/man3/'))
#
#sources.append(source('gl4', docbook_parser(),
#    regexp=r'^gl[A-Z][a-zA-Z0-9]*\.xml$',
#    xpath='//file/@name',
#    index='https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man4/',
#    base='http://www.opengl.org/sdk/docs/man4/'))
#
#sources.append(source('glu', docbook_parser(),
#    regexp=r'^glu[A-Z][a-zA-Z0-9]*\.xml$',
#    xpath='//file/@name',
#    index='https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man/',
#    base='http://www.opengl.org/sdk/docs/man/'))
#
#sources.append(source('glX', docbook_parser(),
#    regexp=r'^glX[A-Z][a-zA-Z0-9]*\.xml$',
#    xpath='//file/@name',
#    index='https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man/',
#    base='http://www.opengl.org/sdk/docs/man/'))
#
#sources.append(source('glext', extension_parser(),
#    regexp=r'^specs/[A-Z]+/[a-zA-Z0-9_]+\.txt$',
#    xpath='//a/@href',
#    index='http://www.opengl.org/registry/',
#    base='http://www.opengl.org/registry/'))

sources.append(source('gl', dotspec_constant_parser, regexp=r'^api/enumext\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('gl', dotspec_function_parser, regexp=r'^api/gl(?:ext)?\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('gl', dottm_parser, regexp=r'^api/gl\.tm$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('gl', header_parser, regexp=r'^api/gl(?:ext)?\.h$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('gl', header_parser, regexp=r'^api/gl3(?:ext)?\.h$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('gl', header_parser, files=['file:///usr/include/GL/gl.h']))

sources.append(source('glX', dotspec_constant_parser, regexp=r'^api/glxenumext\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('glX', dotspec_function_parser, regexp=r'^api/glx(?:ext)?\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('glX', dottm_parser, regexp=r'^api/glx\.tm$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('glX', header_parser, regexp=r'^api/glx(?:ext)?\.h$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
#sources.append(source('glX', header_parser, files=['file:///usr/include/GL/glx.h']))

sources.append(source('wgl', dotspec_constant_parser, regexp=r'^api/wglenumext\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('wgl', dotspec_function_parser, regexp=r'^api/wgl(?:ext)?\.spec$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('wgl', dottm_parser, regexp=r'^api/wgl\.tm$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))
sources.append(source('wgl', header_parser, regexp=r'^api/wgl(?:ext)?\.h$', xpath='//a/@href', index='http://www.opengl.org/registry/', base='http://www.opengl.org/registry/'))

#sources.append(source('glu', header_parser, files=['file:///usr/include/GL/glu.h']))

sources.append(source('egl', dotspec_constant_parser, regexp=r'^api/eglenum\.spec$', xpath='//a/@href', index='http://www.khronos.org/registry/egl/', base='http://www.khronos.org/registry/egl/'))

for s in sources:
  s.parse()

reduce_constants()
reduce_functions()

print_constants(basic_printer())
print_functions(basic_printer())

#print '#include <cstdint>'
#for n in sorted(set([ e.name for e in enums ])):
#  print 'enum class', n, ': std::uint32_t {'
#  for v, c in sorted(set([ (c.value, c.name) for c in constants if c.enum_name == n])):
#    print '  ', c, '=', v, ','
#  print '};'


# Kenneth Otis
# Youtube Video Downloader
from pytube import YouTube
from pytube import exceptions
import time

# getting the link from the user
link=input("Please enter the link of the YouTube Video you would like to download: ")
yt=YouTube(link)

# showing the video's details
print("Title: ", yt.title)
time.sleep(1)
print ("Views: ", yt.views)
time.sleep(1)
print("Length of video: ", yt.length)
time.sleep(1)
print ("Rating of the video: ", yt.rating)
time.sleep(1)

# Get the highest resolution
stream= yt.streams.first()

#Starting download
print ("\nDownloading to Desktop...")
stream.download('Desktop')
time.sleep(1)
print("\nDownload complete!\n")


# 25206번 너의 평점은
import sys
input = sys.stdin.readline

total_score = 0
total_grade = 0
score_dic = {"A+": 4.5, "A0":4.0, "B+":3.5, "B0":3.0, "C+":2.5, "C0":2.0, "D+":1.5, "D0":1.0, "F":0.0}

for _ in range(20):
    subject, grade, score = input().rstrip().split()
    if score == "P":
        continue
    else:
        total_score += (float(grade) * score_dic[score])
        total_grade += float(grade)

ans = total_score/total_grade
print(f"{ans:.6f}")


import torch
from torchtext import datasets
from torchtext import data
import spacy
import torch.nn.functional as F
import torch.nn as nn
import random
import torch.optim as optim
SEED = 1234
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
TEXT = data.Field(tokenize = 'spacy')
LABEL = data.LabelField(tensor_type= torch.FloatTensor)
train, test = datasets.IMDB.splits(TEXT, LABEL)
train, valid = train.split(random_state = random.seed(SEED))
TEXT.build_vocab(train, max_size = 25000, vectors = "glove.6B.100d")
LABEL.build_vocab(train)
BATCH_SIZE = 64
train_itr, valid_itr, test_itr = data.BucketIterator.splits((train,test,valid), batch_size = BATCH_SIZE, sort_key = lambda x:len(x.text), repeat = False)
class RNN(nn.Module):
  def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, bidirectional, dropout):
    super().__init__()
    self.embedding = nn.Embedding(vocab_size, embedding_dim)
    self.rnn = nn.LSTM(embedding_dim, hidden_dim, num_layers = n_layers, bidirectional = bidirectional, dropout = dropout)
    self.fc = nn.Linear(hidden_dim *2, output_dim)
    self.dropout = nn.Dropout(dropout)
  def forward(self, x):
    embedded = self.dropout(self.embedding(x))
    output, (hidden, cell) = self.rnn(embedded)
    hidden = self.dropout(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim=1))
    return self.fc(hidden.squeeze(0))
INPUT_DIM = len(TEXT.vocab)
EMBEDDING_DIM = 100
HIDDEN_DIM = 256
OUTPUT_DIM = 1
N_LAYERS = 2
BIDIRECTIONAL = True
DROPOUT = 0.5
model = RNN(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM, N_LAYERS, BIDIRECTIONAL, DROPOUT)
pretrained_embedding = TEXT.vocab.vectors
optimizer = optim.Adam(model.parameters())
criterion = nn.BCEWithLogitsLoss()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
criterion = criterion.to(device)
def binary_accuracy(preds, y):
  rounds_preds = torch.round(F.sigmoid(preds))
  correct = (rounds_preds == y).float()
  acc = correct.sum()/len(correct)
  return acc
def train(model, iterator, optimizer, criterion):
  epoch_loss = 0
  epoch_acc = 0
  model.train()
  for batch in iterator:
    optimizer.zero_grad()
    predictions = model(batch.text).squeeze(1)
    loss = criterion(predictions, batch.label)
    acc = binary_accuracy(predictions, batch.label)
    loss.backward()
    optimizer.step()
    epoch_loss += loss.item()
    epoch_acc += acc.item()
  return epoch_loss/len(iterator), epoch_acc/len(iterator)

def evaluate(model, iterator, criterion):
    epoch_loss = 0
    epoch_acc = 0
    model.eval()
    with torch.no_grad():
       for batch in iterator:
            predictions = model(batch.text).squeeze(1)
            loss = criterion(predictions, batch.label)
            acc = binary_accuracy(predictions, batch.label)
            epoch_loss += loss.item()
            epoch_acc += acc.item()
    return epoch_loss / len(iterator), epoch_acc / len(iterator)
NLP = spacy.load('en')
def predict_sentiment(sentence):
  tokenized = [tok.text for tok in NLP.tokenizer(sentence)]
  indexed = [TEXT.vocab.stoi[t] for t in tokenized]
  tensor = torch.LongTensor(indexed).to(device)
  tensor = tensor.unsqueeze(1)
  predection = F.sigmoid(model(tensor))
  return predection.item()


def solution(today, terms, privacies):
    def to_date(date):
        y, m, d = map(int, date.split('.'))
        return y * 28 * 12 + m * 28 + d

    answer = []

    terms = dict(map(lambda x: x.split(), terms))
    for i in range(len(privacies)):
        d, t = privacies[i].split()
        if to_date(d) + int(terms[t]) * 28 - 1 < to_date(today):
            answer.append(i + 1)

    return answer

프로그래머스/unrated/150370. 개인정보 수집 유효기간/개인정보 수집 유효기간.py

개인정보 수집 유효기간.py


import os

import cv2
import dlib
import numpy as np

def teste():
    detector = dlib.get_frontal_face_detector()
    detectorPontos = dlib.shape_predictor("res/shape_predictor_68_face_landmarks.dat")
    reconhecimentoFacial = dlib.face_recognition_model_v1("res/dlib_face_recognition_resnet_model_v1.dat")

    indices = np.load("res/indices.pickle", allow_pickle=True)
    descritoresFaciais = np.load("res/descritores.npy", allow_pickle=True)

    limiar = 0.45
    cap = cv2.VideoCapture(0)
    nome = ''
    while True:

        ret_val, frame = cap.read()
        rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        dets = detector(rgb_image)
        for det in dets:
            e, t, d, b = (int(det.left()), int(det.top()), int(det.right()), int(det.bottom()))
            cv2.rectangle(frame,(det.left(), det.top()), (det.right(), det.bottom()), (0, 255, 0), 2)
            pontos = detectorPontos(frame, det)
            descritorFacial = reconhecimentoFacial.compute_face_descriptor(frame, pontos)
            listaDescritorFacial = [fd for fd in descritorFacial]
            npArrayDescritorFacial = np.asarray(listaDescritorFacial, dtype=np.float64)
            npArrayDescritorFacial = npArrayDescritorFacial[np.newaxis, :]

            distancias = np.linalg.norm(npArrayDescritorFacial - descritoresFaciais, axis=1) # Calculo das distancias euclidianas

            minimo = np.argmin(distancias)
            distanciaMinima = distancias[minimo-1]
            if distanciaMinima <= limiar:
                nome = os.path.split(indices[minimo-1])[1].split(".")[0]
                # e, t, d, b = (int(det.left()), int(det.top()), int(det.right()), int(det.bottom()))
                cv2.rectangle(frame,(det.left(), det.top()), (det.right(), det.bottom()), (0, 255, 0), 2)
                texto = f"{nome} {distanciaMinima:.4f}"
                cv2.putText(frame, texto, (d, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 2)
                cv2.putText(frame, texto, (d, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)
                print(f'RECONHECIDO')
                print(nome)

            else:
                nome = "Nao registrado"

                print(f'NAO RECONHECIDO' )
                print(nome)
                # e, t, d, b = (int(det.left()), int(det.top()), int(det.right()), int(det.bottom()))
                cv2.rectangle(frame,(det.left(), det.top()), (det.right(), det.bottom()), (0, 0, 255), 2)

                texto = f"{nome} {distanciaMinima:.4f}"
                cv2.putText(frame, texto, (d, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 2)
                cv2.putText(frame, texto, (d, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
        cv2.imshow('Cam', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


###########################################################################################
# ATTENTION CE MODÈLE EST CHRONOPHAGE, IL EST CONSERVÉ PAR BUT PÉDAGOGIQUE UNIQUEMENT     #
###########################################################################################

"""

Ce modèle réalisé sous tensorflow version 1 n'est pas du tout optimisé
il prend des heures (littéralement!) possibilité de jouer sur la batch size
le problèmes vient probablement d'un manipulation trop conséquentes des entrées
vecteurs de dimension vocab*1 ==> alors que ce sont des vecteurs creux 1 seul coordonnées non nulles
conséquences on se trainent pleins de coéfficients inutiles dans la matrice

Solution
utiliser l'implémentation keras fournit
exploiter l'implémentation dans gensim
"""
from nltk.corpus import stopwords
import stanza
from sklearn.feature_extraction.text import CountVectorizer,TfidfTransformer
import numpy as np
import tensorflow as tf
import pandas as pd
import class_xml
from scipy.sparse import csr_matrix
import random

""" import le module class_texte """
""" Recuperer le liste_docment_traites"""
""" Récupérer le vocabulaire"""

"""Plongement de mots """
for doc in liste_docments_traités:
    for item in doc:
        #print(item)
        object_texte=item[0]
        #print(object_texte)
        object_texte.__one_hot__()
        #print(object_texte.oh)
liste_oh=[txt.oh for doc in liste_docments_traités for (txt,nature,ID) in doc]

# en clair le one hot c'est utilsier le dico test_2 pour remplacer les listes de string par une liste de nombres

#On pourrait stocker ces informations dans le type de texte mais le vocabulaire pose problème
# En effet le vocabulaire est une variable de la classe de Texte
# Cette variable est issue de l'analyse de l'ensemble des texte Cf la fonction vocabulary()

## après il va falloir faire des regroupements
## aka générer des couples de nombre entrées sorties
## possibilité de faire du slicing

"""générer des clouples de données """
Taille_fenêtre=5
liste_de_couple=[]
for each_doc in liste_oh:
    for indice,wordx in enumerate(each_doc):
        for voisin in each_doc[max(indice-Taille_fenêtre,0): min(indice+Taille_fenêtre,len(each_doc)+1)]:
            if voisin!=wordx:
                liste_de_couple.append([wordx,voisin])

import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
Dim_voc=len(set(test_2))
X,Y=[],[]
# X entrée
# Y sortie
def from_numbers_to_array(indice):
    ans=np.zeros(Dim_voc)
    ans[indice]=1
    return ans
    pass
for item  in  liste_de_couple:
    X.append(from_numbers_to_array(item[0]))
    Y.append(from_numbers_to_array(item[1]))
X_train=np.asarray(X)
Y_train=np.asarray(Y)
# window_size=5
# dim_plongement=2
# long_oh=[len(i) for i in liste_oh]
# vocab_size=len(test_2)
# max_longueur=max(long_oh)
# padded_docs=tf.keras.preprocessing.sequence.pad_sequences(liste_oh, maxlen=max_longueur,padding="post")
# sampling_table = tf.keras.preprocessing.sequence.make_sampling_table(vocab_size)
# plt_mot=tf.keras.layers.Embedding(vocab_size,dim_plongement,name='plt_mot')
# # inputs
# w_inputs = tf.keras.Input(shape=(1,),dtype='int32')
# w=plt_mot(w_inputs)
# # context

# c_inputs= tf.keras.Input(shape=(1,),dtype='int32')
# c=plt_mot(c_inputs)
# o=tf.keras.layers.Dot(axes=2)([w,c])
# o=tf.keras.layers.Reshape((1,),input_shape=(1,1))(o)
# o=tf.keras.layers.Activation("sigmoid")(o)
# SkipGram=tf.keras.Model(inputs=[w_inputs,c_inputs],outputs=o)
# SkipGram.summary()
# SkipGram.compile(loss='binary_crossentropy',optimizer='adam')
# from datetime import datetime
# now=datetime.utcnow().strftime("%Y%m%d%H%M%S")
# root_logdir="tf_logs"
# logdir="{}/run-{}/".format(root_logdir,now)
# tensorboard_callback=tf.compat.v1.keras.callbacks.TensorBoard(log_dir=logdir,write_graph=True)
# #writer=tf.summary.FileWriter(logdir,graph=)
# X=[]
# Y=[]
# for _ in range(5):
#     for item in liste_oh:
#         couples,labels=tf.keras.preprocessing.sequence.skipgrams(item, vocab_size, window_size,sampling_table=sampling_table)
#         for couple in couples:
#             X.append(couple)
#         for label in labels:
#             Y.append(label)
# X_new=[np.array(x) for x in zip(*X)]
# SkipGram.fit(X_new,Y,batch_size=10,epochs=5,verbose=1,
#              callbacks=[tensorboard_callback]
# )
# matrice_plgt=SkipGram.get_layer("plt_mot").get_weights()[0]
# tfidf=TfidfTransformer()
# matrice_tfidf=tfidf.fit_transform(Matrice_term_doc)
# matrice_BM25F=BM_25F(Matrice_term_doc)
# liste_modèle=[matrice_tfidf,matrice_BM25F,matrice_plgt]
# with open("modele_mat.npy",'wb') as f:
#     for mat in liste_modèle:
#         np.save(f,mat)
# import pickle

# pickle.dump(test_2,open("dico_mot_index.p", 'wb'))
# pickle.dump(test_dico, open("dico_index_mot.p",'wb'))
# pickle.dump(liste_id,open("liste_id_doc.p", 'wb'))
# pickle.dump(liste_docments_traités,open("docment_traites.p",'wb'))
# pickle.dump(liste_doc_xml,open("liste_doc_xml.p","wb"))
# pickle.dump(stop_words,open("stop_words.p","wb"))
# pickle.dump(p,open("reg_exp.p","wb"))
x=tf.compat.v1.placeholder(tf.float32,shape=(None,Dim_voc),name="entree")

y_label=tf.compat.v1.placeholder(tf.float32,shape=(None,Dim_voc), name="sortie")

""" -* IMPORTANT CHOIX DE LA DIMENSION DU PLONGEMENT *- """
""" finalement c'est la même que du clustering il va falloir la reduction de dimensionalité """

DIM_plongement=2
""" La litterature recommande un nombre dimension entre 100 et 1000"""
# FIRST HIDDEN LAYER
with tf.compat.v1.variable_scope("premiere_couche"):
    W1=tf.Variable(tf.random.normal([Dim_voc,DIM_plongement]),name="poids")
    b1=tf.Variable(tf.random.normal([1]),name="biais") # Bias
    Couche_cachee=tf.add(tf.matmul(x,W1),b1,name="neurone_lineaire")

""" SORTIE """

with tf.compat.v1.variable_scope("Softmax"):
    W2 = tf.Variable(tf.random.normal([DIM_plongement,Dim_voc]),name="poids")
    b2 = tf.Variable(tf.random.normal([1]),name="biais")
    logit=tf.add(tf.matmul(Couche_cachee,W2),b2, name="neurone_lineaire")
    pred=tf.compat.v1.nn.softmax(logit,name="softmax")

fonction_objectif=tf.math.reduce_mean(
#    tf.compat.v1.losses.softmax_cross_entropy(labels=y_label,logits=logit))
    -tf.reduce_sum(y_label*tf.math.log(pred),axis=[1]))
#

optimizer=tf.compat.v1.train.MomentumOptimizer(0.05,0.9, use_nesterov=True)
train_op=optimizer.minimize(fonction_objectif)
from datetime import datetime
now=datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir="tf_logs"
logdir="{}/run-{}/".format(root_logdir,now)

summary_loss=tf.compat.v1.summary.scalar("Xentropy",fonction_objectif)
summary_writer=tf.compat.v1.summary.FileWriter(logdir,tf.compat.v1.get_default_graph())

# TRAINING -- ENTRAÎNEMENT
sess=tf.compat.v1.Session()
saver=tf.compat.v1.train.Saver()
init=tf.compat.v1.global_variables_initializer()
sess.run(init)
iteration = 1000 # 50 000
for i in range(iteration):
    sess.run(train_op, feed_dict={x: X_train, y_label:Y_train})
    if i% 100 == 0:
        print(str(i/iteration*100)+"%: iteration n°"+str(i)+" fonction objectif", sess.run(fonction_objectif, feed_dict={x:X_train,y_label:Y_train}))
        summary_str=summary_loss.eval(session=sess,feed_dict={x:X_train,y_label:Y_train})
        summary_writer.add_summary(summary_str,i)
    if i% 1000 ==0:
        save_path=saver.save(sess, "./modelV3.ckpt")
save_path=saver.save(sess, "./final_modelV3.ckpt")
summary_writer.close()
repz_2D=sess.run(W1+b1)
#print(type(repz_2D))
# import pickle
# pickle.dump(repz_2D,open('vecteur_mots.p',"wb"))


# 1 : Print numbers from 0 to 10 using while

x= 0
while x<11:
    print(x)

    x +=1

# 2 : Print numbers from 0 to 10 using for

for x in range(0,11):
    print(x)

# 3 : Stop the loop if number ==5

for x in range(0,11):
    if x ==5:
        break
    print(x)

# 4 : Skip the 5 iteration from print

for x in range(0,11):
    if x ==5:
        continue
    print(x)

# 5 : Print muliplaction table from 1 to 5

for x in range (1,6):
    print ('--------------------')
    for y in range(1,11):
        print(f'{x}X{y}={x*y}')

# 6 : How to get numbers from 10 to 20 using range

for x in range (10,21):
    print(x)

# 7 : How to get numbers from 10 to 100 with 3 at each step using range

for x in range(10,103,3):
    print (x)

# 8 : Get a list of even numbers from 1 to 100 using for

# 9 : Get a list of even numbers from 1 to 100 using while

x=0

while x<102:
    print(x)

    x +=2

# 10 : Get a list of even numbers from 1 to 100 using range

for x in range (0,102,2):
    print (x)


with open('data/NODERELNODE.csv', 'r', encoding='utf-8') as f:
    with open('data/zstp11_sjdqx_NODERELNODE_YES.csv','a+', encoding='utf-8') as ff:
        # count=0
        for row in f:
            # count+=1
            line=row.strip('\n').replace('"','').replace("'",'').split(',')
            # string="merge (a:Node{NodeId:'"+line[0]+"'}) merge (b:Node{NodeId:'"+line[2]+"'}) merge (a)-[c:"+line[1]+"]->(b);"
            if line[0]!='' and line[1]!='' and line[2]!='':
                # print((line[0]+','+line[1]+','+line[2]+'\n'))
                ff.write(line[0]+','+line[1]+','+line[2]+'\n')

知识图谱11 数据大清洗1.3.py

知识图谱11 数据大清洗1.3.py


from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.core.exceptions import ValidationError
import json

def validate_ticks(value):
    """Validates that ticks list has 15 integers between -10 and -100"""
    # Verify format of ticks object/validation conditions
    # ticks_list = value.ticks
    if len(value) != 15:
        raise ValidationError(
            ('15 Audio ticks required'),
            params={'value': value},
            )
    for x in value:
        if not -100 > x > -10 :
            raise ValidationError(
                ('Ticks must all be between -10 and -100'),
                params={'value': value},
                )

class Step(models.Model):
    session = models.ForeignKey("Session", on_delete=models.CASCADE, related_name="session_steps")
    step_count = models.IntegerField(
        default=0,
        validators=[MaxValueValidator(9), MinValueValidator(0)]
    )
    ticks = models.JSONField(max_length=200, validators=[validate_ticks])
    selected_tick = models.IntegerField(
        default=0,
        validators=[MaxValueValidator(14), MinValueValidator(0)]
    )


from setuptools import setup, find_packages

with open("requirements.txt") as f:
    content = f.readlines()
requirements = [x.strip() for x in content]

setup(name="api-tagger",
      version="1.0",
      description="Automatic question tagging system using multi classification model",
      packages=find_packages(),
      include_package_data=True,  # includes in package files from MANIFEST.in
      install_requires=requirements)


from pyecharts import options as opts
from pyecharts.charts import Map, Geo
from pyecharts.faker import Faker
from pyecharts.globals import ChartType

data = [['杭州市', 200], ['宁波市', 200], ['温州市', 30],
        ['嘉兴市', 40], ['湖州市', 50], ['绍兴市', 60],
        ['金华市', 70], ['衢州市', 8], ['舟山市', 90],
        ['台州市', 10], ['丽水市', 110]]

# map = (Map().add("CO2 emission", data, "浙江", zoom = 1).set_global_opts(
#     title_opts = opts.TitleOpts(title = "Map-浙江地图"),
#     visualmap_opts = opts.VisualMapOpts(max_ = 300, is_piecewise = True)
# ).render("map_Zhejiang.html"))
# def map_yunnan() -> Map:
#     c = (
#         Map()
#             .add("CO2 emission", data, "浙江", zoom = 1,maptype="zhejiang" )
#             .set_series_opts(label_opts = opts.LabelOpts(is_show = False))
#             .set_global_opts(
#             title_opts = opts.TitleOpts(title = "浙江碳排放"),
#             visualmap_opts = opts.VisualMapOpts(max_ = 300, is_pziecewise = True
#                                                 # pieces = [{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
#                                                 #           {"max": 9, "min": 1, "label": "0-9", "color": "#FFE4E1"},
#                                                 #           {"max": 99, "min": 10, "label": "10-99", "color": "#FF7F50"},
#                                                 #           {"max": 499, "min": 100, "label": "100-499",
#                                                 #            "color": "#F08080"},
#                                                 #           {"max": 999, "min": 500, "label": "500-999",
#                                                 #            "color": "#CD5C5C"},
#                                                 #           {"max": 9999, "min": 1000, "label": ">=1000",
#                                                 #            "color": "#8B0000"}]
#                                                 ),
#         )
#     )
#
#     return c
#
#
# d_map = map_yunnan()
# d_map.render("map_zhejiang.html")

citys = ['杭州市', '宁波市', '温州市',
         '嘉兴市', '湖州市', '绍兴市',
         '金华市', '衢州市', '舟山市',
         '台州市', '丽水市']
values = [365, 656, 524, 455, 411, 174, 125, 121, 666, 123, 444]
c = (
    Geo().add_schema(maptype = "浙江")
     .add("CO2 emission", [list(z) for z in zip(citys, values)], type_ = ChartType.SCATTER)
     .set_global_opts(visualmap_opts = opts.VisualMapOpts(), title_opts = opts.TitleOpts(title = "Geo-浙江地图"))
     .set_series_opts(label_opts = opts.LabelOpts(is_show = True)).render("zj.html")
     )
# c = (
#     Geo()
#         .add_schema(maptype = "china")
#         .add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
#         .set_series_opts(label_opts = opts.LabelOpts(is_show = False))
#         .set_global_opts(
#         visualmap_opts = opts.VisualMapOpts(), title_opts = opts.TitleOpts(title = "Geo-基本示例")
#     )
#         .render("geo_base.html")
# )


import random
tp=()
tam=random.randrange(5,15)
for i in range(tam):
    ele=random.randrange(1,100)
    tp+=(ele,)
print(tp)

ope=len(tp)//2
tp2=tp[:ope]
print(tp2)
tp3=tp[ope:]
print(tp3)


'''
3.2 执行精确的浮点数运算
问题
你需要对浮点数执行精确的计算操作，并且不希望有任何小误差的出现。
解决方案
浮点数的一个普遍问题是它们并不能精确的表示十进制数。并且，即使是最简单的
数学运算也会产生小的误差
如果你想更加精确(并能容忍一定的性能损耗)，你可以使用decimal 模块
Decimal对象会像普通浮点数一样的工作(支持所有的常用数学运算)。如果你打印它们或者在
字符串格式化函数中使用它们，看起来跟普通数字没什么两样。
decimal 模块的一个主要特征是允许你控制计算的每一方面，包括数字位数和四
舍五入运算。为了这样做，你先得创建一个本地上下文并更改它的设置
'''
from decimal import Decimal
from decimal import localcontext
a = Decimal('1.3')
b = Decimal('1.7')
print(a / b)
with localcontext() as ctx:
    ctx.prec = 3
    print(a / b)

'''
总的来说，decimal 模块主要用在涉及到金融的领域
当Python 和数据库打交道的时候也通常会遇到Decimal 对象，并且，通常也
是在处理金融数据的时候。
'''


import time
import struct
from threading import RLock

from mltk.utils.jlink import JLink
from mltk.utils.python import prepend_exception_msg

# See <mltk root>/cpp/shared/jlink_stream/jlink_stream/jlink_stream_interface.hpp

CONTEXT_MAGIC_NUMBER            = 0xF3f37BA2
BUFFER_MAGIC_NUMBER             = 0xE3D37B32

COMMAND_TIMEOUT                 = 15.0
MAX_BUFFER_SIZE                 = 8*1024

STATUS_INIT_REQUIRED            = 0
STATUS_INIT_FAILED              = 1
STATUS_INVOKE_INIT              = ~CONTEXT_MAGIC_NUMBER
STATUS_IDLE                     = 2
STATUS_READY                    = 3
STATUS_EXECUTING                = 4
STATUS_COMPLETE                 = 5

COMMAND_OPEN_FOR_READ           = 1
COMMAND_OPEN_FOR_WRITE          = 2
COMMAND_CLOSE                   = 3
COMMAND_READ_BUFFER_STATUS_MASK = 4
COMMAND_READ_BUFFER             = 5
COMMAND_WRITE_BUFFER            = 6

RESULT_SUCCESS                  = 0
RESULT_ERROR                    = 1
RESULT_UNKNOWN_CMD              = 2
RESULT_NOT_FOUND                = 3
RESULT_READ_ONLY                = 4
RESULT_WRITE_ONLY               = 5
RESULT_BAD_ARGS                 = 6
RESULT_MALLOC_FAILED            = 7
RESULT_ALREADY_OPENED           = 8
RESULT_NOT_OPENED               = 9

RESULT_STR = {}
RESULT_STR[RESULT_SUCCESS]          = 'SUCCESS'
RESULT_STR[RESULT_ERROR]            = 'ERROR'
RESULT_STR[RESULT_UNKNOWN_CMD]      = 'UNKNOWN_CMD'
RESULT_STR[RESULT_NOT_FOUND]        = 'NOT_FOUND'
RESULT_STR[RESULT_READ_ONLY]        = 'READ_ONLY'
RESULT_STR[RESULT_WRITE_ONLY]       = 'WRITE_ONLY'
RESULT_STR[RESULT_BAD_ARGS]         = 'BAD_ARGS'
RESULT_STR[RESULT_MALLOC_FAILED]    = 'MALLOC_FAILED'
RESULT_STR[RESULT_ALREADY_OPENED]   = 'ALREADY_OPENED'
RESULT_STR[RESULT_NOT_OPENED]       = 'NOT_OPENED'

CONTEXT_OFFSET_MAGIC_NUMBER     = 0
CONTEXT_OFFSET_STATUS           = 1
CONTEXT_OFFSET_TRIGGER_ADDR     = 2
CONTEXT_OFFSET_TRIGGER_VAL      = 3
CONTEXT_OFFSET_CMD_CODE         = 4
CONTEXT_OFFSET_CMD_RESULT       = 5
CONTEXT_OFFSET_CMD_LENGTH       = 6
CONTEXT_OFFSET_CMD_BUFFER_ADDR  = 7
CONTEXT_OFFSET_COUNT            = 8

BUFFER_OFFSET_MAGIC_NUMBER      = 0
BUFFER_OFFSET_ID                = 1
BUFFER_OFFSET_START             = 2
BUFFER_OFFSET_END               = 3
BUFFER_OFFSET_HEAD              = 4
BUFFER_OFFSET_TAIL              = 5
BUFFER_OFFSET_LENGTH            = 6
BUFFER_OFFSET_COUNT             = 7

class DeviceInterface(object):

    def __init__(self, options):
        self._jlink = JLink(library_paths=options.lib_path)
        self._jlink.set_interface(options.interface)
        self._jlink.set_speed(options.clock)
        self._jlink.set_core(options.core)
        self._sram_base_address = options.sram_address
        self._sram_size         = options.sram_size

        self._lock = RLock()
        self._cmd_lock = RLock()
        self._context_address = 0
        self._trigger_address = 0
        self._trigger_value = 0
        self._command_buffer_address = 0

    @property
    def buffer_status_mask(self) -> int:
        data = self._issue_command(COMMAND_READ_BUFFER_STATUS_MASK)
        return struct.unpack('<L', data)[0]

    @property
    def is_connected(self) -> bool:
        return self._jlink.is_connected()

    def open(self, name:str, mode:str):
        # pylint: disable=import-outside-toplevel
        from .data_stream import JLinkDataStream

        if not self.is_connected:
            raise Exception('Not connected')

        if mode == 'r':
            cmd = COMMAND_OPEN_FOR_READ
        elif mode == 'w':
            cmd = COMMAND_OPEN_FOR_WRITE
        else:
            raise Exception(f'Unsupported mode: {mode}')

        # Ensure the connection is closed
        self._issue_command(COMMAND_CLOSE, name, ignore_error=True)

        res = self._issue_command(cmd, name)

        stream_context = {}
        stream_context['id'], stream_context['base_address'] = struct.unpack('<LL', res)

        stream = JLinkDataStream(
            name=name,
            mode=mode,
            ifc=self,
            stream_context=stream_context
        )

        return stream

    def close(self, name:str):
        if not self.is_connected:
            raise Exception('Not connected')

        self._issue_command(COMMAND_CLOSE, name)

    def read(self, context:dict, length:int) -> bytes:
        if length == 0:
            return None

        if not self.is_connected:
            raise Exception('Not connected')

        buffer_context = self._read_buffer_context(context['base_address'])

        read_length = min(length, buffer_context['length'])
        if read_length == 0:
            return None

        length_to_end = buffer_context['end'] - buffer_context['head']
        data = self._jlink_read8(buffer_context['head'], min(length_to_end, read_length))

        if read_length > length_to_end:
            data2 = self._jlink_read8(buffer_context['start'], read_length - length_to_end)
            data.extend(data2)

        cmd_data = struct.pack('<LL', buffer_context['id'], read_length)
        self._issue_command(COMMAND_READ_BUFFER, cmd_data)

        return bytes(data)

    def write(self, context:dict, data:bytes) -> int:
        if data == None or len(data) == 0:
            return

        if not self.is_connected:
            raise Exception('Not connected')

        buffer_context = self._read_buffer_context(context['base_address'])

        device_buffer_available = MAX_BUFFER_SIZE - buffer_context['length']
        write_length = min(len(data), device_buffer_available)
        if not write_length:
            return

        length_to_end = buffer_context['end'] - buffer_context['tail']

        chunk_length = min(length_to_end, write_length)
        self._jlink_write8(buffer_context['tail'], data[:chunk_length])

        if write_length > length_to_end:
            self._jlink_write8(buffer_context['start'], data[chunk_length:chunk_length + (write_length-length_to_end)])

        cmd_data = struct.pack('<LL', buffer_context['id'], write_length)
        self._issue_command(COMMAND_WRITE_BUFFER, cmd_data)

        return write_length

    def connect(self, reset_device=False):
        # Open JLink connection
        try:
            self._jlink.connect()
        except Exception as e:
            raise type(e)(f"Failed to open JLINK connection: {e}")

        # Reset the device if necessary
        if reset_device:
            try:
                self._jlink.reset()
                self._jlink.resume()
                time.sleep(0.250) # Wait a moment for the device to restart
            except Exception as e:
                self.disconnect()
                raise type(e)(f'Failed to reset target: {e}')

        try:
            # Find the context's address in the device's SRAM
            self._context_address = self._find_context_base_address()

            # Read the contents of the context
            context = self._read_context()

            # Save the context constant values
            self._trigger_address           = context['trigger_address']
            self._trigger_value             = context['trigger_value']
            self._command_buffer_address   = context['command_buffer_address']

            # If the context needs to be initialized
            if context['status'] == STATUS_INIT_REQUIRED:
                context = self._initialize_context()
        except Exception as e:
            self.disconnect()
            prepend_exception_msg(e, 'Failed read context')
            raise

    def disconnect(self):
        with self._lock:
            try:
                self._jlink.close()
            except:
                pass

    def _find_context_base_address(self) -> int:
        start_time = time.time()

        def _query_base_address(base_address):
            value = self._jlink_read32(base_address)

            # If a value within the SRAM address range is found,
            # Then query this address value, if it's a pointer
            # To the context, then the context should have its MAGIC_NUMBER set
            if value > self._sram_base_address and value < base_address:
                # Query the value at this address
                value2 =  self._jlink_read32(value)

                # If the queried value is the magic number
                # then the context address has been found
                if value2 == CONTEXT_MAGIC_NUMBER:
                    return value

            return None

        while (time.time() - start_time) < 30.0:
            # The the SRAM size has been specified
            if self._sram_size != -1:
                base_address = self._sram_base_address + self._sram_size - 4

                context_address = _query_base_address(base_address)
                if context_address:
                    return context_address
                else:
                    time.sleep(0.50)

            else:
                # The SRAM size isn't known, so iterate through the various RAM sizes
                for ram_size in range(96, 1025, 32):
                    base_address = self._sram_base_address + ram_size*1024 - 4
                    context_address = _query_base_address(base_address)
                    if context_address:
                        return context_address

                time.sleep(0.50)

        raise Exception("Timed out searching for context's base address. Ensure the device is executing properly")

    def _initialize_context(self) -> dict:
        context = self._read_context()

        # Write the initialization status to the context
        self._write_context(status=STATUS_INVOKE_INIT)

        # Wait until the status becomes idle
        start_time = time.time()
        while True:
            if (time.time() - start_time) > 30.0:
                raise Exception("Timed out waiting to initialize context")
            context = self._read_context()
            if context['status'] == STATUS_IDLE:
                break
            elif context['status'] == STATUS_INIT_FAILED:
                raise Exception("Failed to initialize context")

        return context

    def _read_context(self) -> dict:
        data = self._jlink_read8(self._context_address, CONTEXT_OFFSET_COUNT*4)

        context = {}
        context['magic_number']             = self._unpack_data(data, CONTEXT_OFFSET_MAGIC_NUMBER)
        context['status']                   = self._unpack_data(data, CONTEXT_OFFSET_STATUS)
        context['trigger_address']          = self._unpack_data(data, CONTEXT_OFFSET_TRIGGER_ADDR)
        context['trigger_value']            = self._unpack_data(data, CONTEXT_OFFSET_TRIGGER_VAL)
        context['command_code']             = self._unpack_data(data, CONTEXT_OFFSET_CMD_CODE)
        context['command_result']           = self._unpack_data(data, CONTEXT_OFFSET_CMD_RESULT)
        context['command_length']           = self._unpack_data(data, CONTEXT_OFFSET_CMD_LENGTH)
        context['command_buffer_address']   = self._unpack_data(data, CONTEXT_OFFSET_CMD_BUFFER_ADDR)

        if context['magic_number'] != CONTEXT_MAGIC_NUMBER:
            raise Exception("Context does not contain a valid magic number")

        return context

    def _write_context(self, status:int, cmd:int=None, data=None):
        self._lock.acquire()

        try:
            self._jlink.halt()

            try:
                if cmd:
                    # Write the 'command_code' field
                    self._jlink.write_mem32(self._context_address + CONTEXT_OFFSET_CMD_CODE*4, cmd)

                if data:
                    # Write the 'command_length' field
                    self._jlink.write_mem32(self._context_address + CONTEXT_OFFSET_CMD_LENGTH*4, len(data))

                    # Populate the 'command_buffer'
                    if isinstance(data, str):
                        data = data.encode()
                    cmd_buffer = bytes(data)
                    self._jlink.write_mem8(self._command_buffer_address, cmd_buffer)

                # Write the 'status' field
                self._jlink.write_mem32(self._context_address + CONTEXT_OFFSET_STATUS*4, status)

                # Write the 'trigger' value to invoke the Kernel to process the context
                self._jlink.write_mem32(self._trigger_address, self._trigger_value)

            finally:
                self._jlink.resume()

        finally:
            self._lock.release()

    def _issue_command(self, cmd, data=None, ignore_error=False):
        self._cmd_lock.acquire()

        try:
            context = self._read_context()

            if context['status'] == STATUS_EXECUTING:
                raise Exception('Device is currently executing another command')

            # Write the command info to the context
            self._write_context(STATUS_READY, cmd, data)

            # Wait for the command to complete
            start_time = time.time()
            while (time.time() - start_time) < COMMAND_TIMEOUT:
                context = self._read_context()

                if context['status'] == STATUS_COMPLETE:
                    break

                time.sleep(0.005)

            if context['status'] != STATUS_COMPLETE:
                raise Exception("Timed-out waiting  for device to execute command")

            response_data = None
            if context['command_length'] > 0:
                try:
                    response_data = self._jlink.read_mem8(self._command_buffer_address, context['command_length'])
                except Exception as e:
                    raise type(e)('Failed to read response data, err:%s' % e)

            if context['command_result'] != RESULT_SUCCESS and not ignore_error:
                if response_data:
                    response_data = response_data.decode('utf-8')
                else:
                    response_data = ''

                if context['command_result'] < len(RESULT_STR):
                    desc = ' - %s' % RESULT_STR[context['command_result']]
                else:
                    desc = ''

                msg = 'Command failed (%d%s)' % (context['command_result'], desc)

                if len(response_data) > 0:
                    msg += ', err: %s' % response_data

                raise Exception(msg)

            return response_data

        finally:
            self._cmd_lock.release()

    def _read_buffer_context(self, base_address) -> dict:
        data = self._jlink_read8(base_address, BUFFER_OFFSET_COUNT*4)

        context = {}
        context['magic_number'] = self._unpack_data(data, BUFFER_OFFSET_MAGIC_NUMBER)
        context['id']           = self._unpack_data(data, BUFFER_OFFSET_ID)
        context['start']        = self._unpack_data(data, BUFFER_OFFSET_START)
        context['end']          = self._unpack_data(data, BUFFER_OFFSET_END)
        context['head']         = self._unpack_data(data, BUFFER_OFFSET_HEAD)
        context['tail']         = self._unpack_data(data, BUFFER_OFFSET_TAIL)
        context['length']       = self._unpack_data(data, BUFFER_OFFSET_LENGTH)

        if context['magic_number'] != BUFFER_MAGIC_NUMBER:
            raise Exception('Stream buffer context is not valid on device')

        return context

    def _unpack_data(self, data, offset) -> int:
        start_index = offset*4
        end_index = start_index + 4
        return struct.unpack('<L', data[start_index:end_index])[0]

    def _jlink_read8(self, addr, length) -> bytearray:
        with self._lock:
            return bytearray(self._jlink.read_mem8(addr, length))

    def _jlink_read32(self, addr) -> int:
        with self._lock:
            return self._jlink.read_mem32(addr)

    def _jlink_write8(self, addr, data):
        with self._lock:
            return self._jlink.write_mem8(addr, data)

    def _jlink_write32(self, addr, value):
        with self._lock:
            return self._jlink.write_mem32(addr, value)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import os
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from scipy.spatial.transform import Rotation

# Part of the code is referred from: https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py

def quat2mat(quat):
    x, y, z, w = quat[:, 0], quat[:, 1], quat[:, 2], quat[:, 3]

    B = quat.size(0)

    w2, x2, y2, z2 = w.pow(2), x.pow(2), y.pow(2), z.pow(2)
    wx, wy, wz = w*x, w*y, w*z
    xy, xz, yz = x*y, x*z, y*z

    rotMat = torch.stack([w2 + x2 - y2 - z2, 2*xy - 2*wz, 2*wy + 2*xz,
                          2*wz + 2*xy, w2 - x2 + y2 - z2, 2*yz - 2*wx,
                          2*xz - 2*wy, 2*wx + 2*yz, w2 - x2 - y2 + z2], dim=1).reshape(B, 3, 3)
    return rotMat

def npmat2euler(mats, seq='zyx'):
    eulers = []
    for i in range(mats.shape[0]):
        r = Rotation.from_dcm(mats[i])
        eulers.append(r.as_euler(seq, degrees=True))
    return np.asarray(eulers, dtype='float32')

def inverse(rot: np.ndarray, trans: np.ndarray):
    """Returns the inverse of the SE3 transform

    Args:
        g: ([B,] 3/4, 4) transform

    Returns:
        ([B,] 3/4, 4) matrix containing the inverse

    """
    # rot = g[..., :3, :3]  # (3, 3)
    # trans = g[..., :3, 3]  # (3)

    inv_rot = np.swapaxes(rot, -1, -2)
    inverse_transform = np.concatenate([inv_rot, inv_rot @ -trans[..., None]], axis=-1)
    # if g.shape[-2] == 4:
    #     inverse_transform = np.concatenate([inverse_transform, [[0.0, 0.0, 0.0, 1.0]]], axis=-2)

    return inverse_transform

def concatenate(a: np.ndarray, b: np.ndarray):
    """ Concatenate two SE3 transforms

    Args:
        a: First transform ([B,] 3/4, 4)
        b: Second transform ([B,] 3/4, 4)

    Returns:
        a*b ([B, ] 3/4, 4)

    """

    r_a, t_a = a[..., :3, :3], a[..., :3, 3]
    r_b, t_b = b[..., :3, :3], b[..., :3, 3]

    r_ab = r_a @ r_b
    t_ab = r_a @ t_b[..., None] + t_a[..., None]

    concatenated = np.concatenate([r_ab, t_ab], axis=-1)

    # if a.shape[-2] == 4:
    #     concatenated = np.concatenate([concatenated, [[0.0, 0.0, 0.0, 1.0]]], axis=-2)

    return torch.tensor(concatenated)


# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class BSTIterator:
    # @param root, a binary search tree's root node
    def __init__(self, root):
        self.stack = []
        self.cur = root

    # @return a boolean, whether we have a next smallest number
    def hasNext(self):
        return self.stack or self.cur

    # @return an integer, the next smallest number
    def next(self):
        while self.hasNext():
            if self.cur:
                self.stack.append(self.cur)
                self.cur = self.cur.left
            else:
                node = self.stack.pop()
                self.cur = node.right
                return node.val
        return None


from django.urls import path
from . import views

app_name = 'student'
urlpatterns = [
    path('', views.main, name='main'),
    path('currenttasks', views.currenttasks, name='currenttasks'),
    path('generatetask', views.generatetask, name='generatetask'),
    path('generatetask/find_templates', views.find_templates, name='find_templates'),
    path('generatetask/gen_templates', views.generateManyJobs, name='gen_templates'),
    path('history', views.history, name='history'),
    path('statistics', views.statistics, name='statistics'),
    path('statistics/change_topic', views.change_topic, name='change_topic'),
    path('profile', views.profile, name='profile'),
    path('support', views.support, name='support'),
    path('toLogin', views.toLogin, name='toLogin'),
    path('changepassword', views.ChangePassword, name='changepassword'),
    path('sendmail', views.sendmail, name='sendmail'),
    path('chp', views.chp, name='chp'),
    path('task/<int:task_id>', views.task, name='task'),
    path('task/<int:task_id>/answer_check', views.checkAnswer, name='answer_check'),
]


from datetime import datetime, date, time, timedelta

class Learn_how_to_use_time():

    '''
        调用构造函数,创建三个时间对象

    '''
    Ian_time = time(19, 12, 10)
    Ian_date = date(2018, 5, 7)
    Ian_datetime = datetime(2018, 5, 7, 19, 12, 10)
    date_str = '2018-5-7'
    time_str = '19:12:10'
    '''
        时间对象=> str
    '''

    def class_to_str(self):
        self.Ian_time = self.Ian_time.strftime('%H:%M:%S')
        self.Ian_date = self.Ian_date.strftime('%Y-%m-%d')
        self.Ian_datetime = self.Ian_datetime.strftime('%Y-%m-%d %H:%M:%S')

    '''
        str => 对象
    '''

    def str_to_class(self):
        self.date_str = datetime.strptime(self.date_str, '%Y-%m-%d')
        self.time_str = datetime.strptime(self.time_str, '%H:%M:%S')

    '''
        日期 运算
    '''

    def add_date(self):
        res = date(2013,10,2) + timedelta(days=10)
        return date(2013,10,2).strftime('%Y-%m-%d'), res.strftime('%Y-%m-%d')

user = Learn_how_to_use_time()
print(type(user.Ian_date), user.Ian_time, user.Ian_datetime)
user.class_to_str()
print(type(user.Ian_date), user.Ian_time, user.Ian_datetime)
user.str_to_class()
print(type(user.date_str), user.time_str)
print(user.add_date())


"""
core
"""
from copy import copy
import json
import os
import warnings

import yaml
from .converter import Converter
from .utils import open_file, Dict

class ConfigMeta(type):
    def __new__(cls, name, bases, attrs):
        converter_mapping = {}
        to_be_removed = []
        for key, value in attrs.items():
            if isinstance(value, (type, Converter)):
                converter_mapping[key.upper()] = value
                to_be_removed.append(key)
            elif key.isupper() and callable(value):
                converter_mapping[key.upper()] = value
                to_be_removed.append(key)
        for key in to_be_removed:
            attrs.pop(key)
        attrs['_converter'] = converter_mapping
        return super().__new__(cls, name, bases, attrs)

class Config(metaclass=ConfigMeta):
    def __init__(self, config_file=None, **defaults):
        self._config = Dict()
        self._converter = copy(self._converter)
        self._update(**defaults)
        if config_file:
            self.load_file(config_file)

    def load(self, file_obj):
        """Same as load_file.
        """
        return self.load_file(file_obj)

    def load_file(self, value, format='yaml'):
        """Load config from a json or yaml file.
        """
        if hasattr(value, 'endswith') and value.endswith('.json') \
                or format == 'json':
            self.load_json_file(value)
        else:
            self.load_yaml_file(value)

    def load_yaml_file(self, value):
        """Load config from yaml file.
        """
        with open_file(value) as f:
            data = yaml.safe_load(f)
        self._update(**data)

    def load_json_file(self, value):
        """Load config from json file.
        """
        with open_file(value) as f:
            data = json.load(f)
        self._update(**data)

    def load_env(self):
        self.load_dict(os.environ)

    def load_dict(self, dct):
        self._update(**dct)

    def _update(self, _prefix='', **dct):
        for key, value in dct.items():
            key = key.upper()
            if _prefix:
                key = _prefix + '.' + key
            if isinstance(value, dict):
                self._update(**value, _prefix=key)
            else:
                self._set(key, value)

    def _set(self, key, value):
        key = key.upper()
        nested_key_lst = key.strip('.').split('.')
        config_dct = self._config
        for part_key in nested_key_lst[:-1]:
            config_dct.setdefault(part_key, Dict())
            if not isinstance(config_dct[part_key], Dict):
                config_dct[part_key] = Dict()
            config_dct = config_dct[part_key]
        part_key = nested_key_lst[-1]
        config_dct[part_key] = self._convert(key, value)

    def _convert(self, key, value):
        if key in self._converter:
            return self._converter[key](value)
        return value

    def set_converter(self, key, convert_func):
        self._converter[key.upper()] = convert_func

    def set_cast_func(self, key, cast_func):
        warnings.warn('use set_converter instead of set_cast_func')
        return self.set_converter(key, cast_func)

    def __getitem__(self, key):
        config = self._config
        for part_key in key.upper().split('.'):
            config = config[part_key]
        return config

    def __setitem__(self, key, value):
        return self._set(key, value)

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __getattr__(self, key):
        if not key.isupper():
            raise KeyError('{} does not exist'.format(key))
        return self.__getitem__(key)


import logging
import math
from typing import List

import torch
import torch.nn as nn
import torch.nn.init as init
from reagent.models.base import ModelBase

logger = logging.getLogger(__name__)

def gaussian_fill_w_gain(tensor, activation, dim_in, min_std=0.0) -> None:
    """ Gaussian initialization with gain."""
    gain = math.sqrt(2) if (activation == "relu" or activation == "leaky_relu") else 1
    init.normal_(tensor, mean=0, std=max(gain * math.sqrt(1 / dim_in), min_std))

ACTIVATION_MAP = {
    "tanh": nn.Tanh,
    "relu": nn.ReLU,
    "leaky_relu": nn.LeakyReLU,
    "linear": nn.Identity,
}

class FullyConnectedNetwork(ModelBase):
    def __init__(
        self,
        layers,
        activations,
        *,
        use_batch_norm=False,
        min_std=0.0,
        dropout_ratio=0.0,
        use_layer_norm=False,
        normalize_output=False,
    ) -> None:
        super().__init__()

        self.input_dim = layers[0]

        modules: List[nn.Module] = []

        assert len(layers) == len(activations) + 1

        for i, ((in_dim, out_dim), activation) in enumerate(
            zip(zip(layers, layers[1:]), activations)
        ):
            # Add BatchNorm1d
            if use_batch_norm:
                modules.append(nn.BatchNorm1d(in_dim))
            # Add Linear
            linear = nn.Linear(in_dim, out_dim)
            gaussian_fill_w_gain(linear.weight, activation, in_dim, min_std=min_std)
            init.constant_(linear.bias, 0)  # type: ignore
            modules.append(linear)
            # Add LayerNorm
            if use_layer_norm and (normalize_output or i < len(activations) - 1):
                modules.append(
                    nn.LayerNorm(out_dim)  # type: ignore
                )
            # Add activation
            if activation in ACTIVATION_MAP:
                modules.append(ACTIVATION_MAP[activation]())
            else:
                # See if it matches any of the nn modules
                modules.append(getattr(nn, activation)())
            # Add Dropout
            if dropout_ratio > 0.0 and (normalize_output or i < len(activations) - 1):
                modules.append(nn.Dropout(p=dropout_ratio))

        self.dnn = nn.Sequential(*modules)  # type: ignore

    def input_prototype(self):
        return torch.randn(1, self.input_dim)

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        """ Forward pass for generic feed-forward DNNs. Assumes activation names
        are valid pytorch activation names.
        :param input tensor
        """
        return self.dnn(input)


from lintcode import (
    ListNode,
)

"""
Definition of ListNode:
class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    """
    @param head: The first node of linked list.
    @return: The head of linked list.
    """
    def insertion_sort_list(self, head: ListNode) -> ListNode:
        # write your code here
        if head is None:
            return head

        # 要對 Linked list 做更動就需要 dummy
        dummy = ListNode(0, head)
        # 一開始假設第一個節點是排序好的，由第二個節點開始往前面的節點比較
        curr = head.next
        latest_sorted = head # 保存已經排序好的 linked list 的最後一個節點

        while curr is not None:
            if latest_sorted.val <= curr.val:
                latest_sorted = latest_sorted.next # 往下移動
            else:
                prev = dummy # 因為要從頭比較，這樣才能修改第一個節點
                # prev.next 是在 curr 前面，要和 curr 比大小的節點
                # 如果 node 比 curr 小，就往下看下一個
                while prev.next.val <= curr.val:
                    prev = prev.next # 往下移動

                # 跑到這裡的時候 curr 一定比 prev.next 小
                # 所以要把 prev -> prev.next 變成 prev -> curr -> prev.next
                # 還有把 latest_sorted -> curr -> curr.next 變成 latest_sorted -> curr.next
                latest_sorted.next = curr.next
                curr.next = prev.next
                prev.next = curr

            curr = latest_sorted.next

        return dummy.next


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
"""
    Утилитки_для_помощи_в_обработке_данных
"""
import os
import sys
import json
import pandas as pd
import matplotlib as mplt
import seaborn as sns

import luigi

mplt.use('Agg')

strip_meta_path = lambda path: path.split('/')[-1:][0].strip('.json')

def get_meta_data(target):
    """
    Получаем данные из meta file
    target: luigi target instace\или путь к мета файлу
    """
    if isinstance(target, str):
        with open(target, 'r') as meta_file:
            meta_data = meta_file.readline()
    else:
        with target.open('r') as meta_file:
            meta_data = meta_file.readline()
    return json.loads(meta_data)

def rewrite_file(target, content):
    """
    Функция перезаписи файла
    target: путь к мета файлу
    content: содержимое файла
    """
    os.remove(target)
    with open(target, 'w') as meta_file:
        meta_file.write(json.dumps(content))

#===============================================================================
def get_data(fpath):
    """
    получаем данные из файла
    """
    return pd.read_csv(fpath)

def get_unique(dataframe):
    """
    Оцениваем количество уникальных  данных
    """
    print('=' * 50)
    print('Data count by columns:\n')
    for i in dataframe.columns:
        print(i, ' ----> ', dataframe[i].nunique())

    print('-' * 50)
    print('Shape: ', dataframe.shape)

def get_nan_by_columns_count(dataframe):
    """
    Оцениваем относительное число пропусков по каждому столбцу
    """
    print('=' * 50)
    print('Nan counts:\n')

    for i in dataframe.columns:
        print(
            i,
            ' ----> ',
            (dataframe[dataframe[i].isnull() == True][i].size * 1.0)\
            /dataframe.index.size

        )

def get_correlation_column_by_target(dataframe, target):
    """
    Сыитаем корреляцию между столбцами и целевым столбцом признаков
    """
    if target not in dataframe.columns:
        raise ValueError

    print('=' * 50)
    print('Correlateion {0} with columns:\n'.format(target))

    for i in dataframe.columns:
        print(
            i,
            ' ----> ',
            dataframe[target].corr(dataframe[i])
        )

#def do_plot(dataframe, target):
#    """
#    СТроит распределение случайных величин в столбцах в  зависимости от наличия
#    признака в  строке, сохраняет картинки в текущую папку
#    """
#    for i in dataframe.columns:
#        tmp_fig = sns.

def main(path_to_data):
    """
    Основная функция оценки данных
    """
    dataframe = get_data(path_to_data)

    get_unique(dataframe)
    get_nan_by_columns_count(dataframe)
    get_correlation_column_by_target(dataframe, 'Delinquent90')

#===============================================================================
#TODO надо орагнизовать слеюдущий алгоритмы:
# - для быстрого результата - заполнить данные и посчитать каким нибудь простым
# алгоритмом
# - но основной подход должен быть следующий:
#    --- оценка нормальности распрделения  фич;
#    --- заолпнение пропусков в данных, дроп результатов;
#    --- построение модели, подгонка параметров
#===============================================================================

if __name__ == "__main__":

    print(sys.argv)
    if len(sys.argv) != 2:
        print('Input path to target csv_data')

    else:
        if os.path.exists(sys.argv[1]):
        #оценим данные
            main(sys.argv[1])

        else:
            print('Path is not exists')