import io
from typing import List, Set, Tuple

from clvm import KEYWORD_FROM_ATOM, KEYWORD_TO_ATOM, SExp
from clvm import run_program as default_run_program
from clvm.casts import int_from_bytes
from clvm.EvalError import EvalError
from clvm.operators import OP_REWRITE, OPERATOR_LOOKUP
from clvm.serialize import sexp_from_stream, sexp_to_stream
from clvm_rs import STRICT_MODE, deserialize_and_run_program2, serialized_length
from clvm_tools.curry import curry, uncurry

from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.hash import std_hash

from .tree_hash import sha256_treehash

def run_program(
    program,
    args,
    max_cost,
    operator_lookup=OPERATOR_LOOKUP,
    pre_eval_f=None,
):
    return default_run_program(
        program,
        args,
        operator_lookup,
        max_cost,
        pre_eval_f=pre_eval_f,
    )

INFINITE_COST = 0x7FFFFFFFFFFFFFFF

class Program(SExp):
    """
    A thin wrapper around s-expression data intended to be invoked with "eval".
    """

    @classmethod
    def parse(cls, f) -> "Program":
        return sexp_from_stream(f, cls.to)

    def stream(self, f):
        sexp_to_stream(self, f)

    @classmethod
    def from_bytes(cls, blob: bytes) -> "Program":
        f = io.BytesIO(blob)
        result = cls.parse(f)  # type: ignore # noqa
        assert f.read() == b""
        return result

    def to_serialized_program(self) -> "SerializedProgram":
        return SerializedProgram.from_bytes(bytes(self))

    def __bytes__(self) -> bytes:
        f = io.BytesIO()
        self.stream(f)  # type: ignore # noqa
        return f.getvalue()

    def __str__(self) -> str:
        return bytes(self).hex()

    def get_tree_hash(self, *args: List[bytes32]) -> bytes32:
        """
        Any values in `args` that appear in the tree
        are presumed to have been hashed already.
        """
        return sha256_treehash(self, set(args))

    def run_with_cost(self, max_cost: int, args) -> Tuple[int, "Program"]:
        prog_args = Program.to(args)
        cost, r = run_program(self, prog_args, max_cost)
        return cost, Program.to(r)

    def run(self, args) -> "Program":
        cost, r = self.run_with_cost(INFINITE_COST, args)
        return r

    def curry(self, *args) -> "Program":
        cost, r = curry(self, list(args))
        return Program.to(r)

    def uncurry(self) -> Tuple["Program", "Program"]:
        r = uncurry(self)
        if r is None:
            return self, self.to(0)
        return r

    def as_int(self) -> int:
        return int_from_bytes(self.as_atom())

    def as_atom_list(self) -> List[bytes]:
        """
        Pretend `self` is a list of atoms. Return the corresponding
        python list of atoms.

        At each step, we always assume a node to be an atom or a pair.
        If the assumption is wrong, we exit early. This way we never fail
        and always return SOMETHING.
        """
        items = []
        obj = self
        while True:
            pair = obj.pair
            if pair is None:
                break
            atom = pair[0].atom
            if atom is None:
                break
            items.append(atom)
            obj = pair[1]
        return items

    def __deepcopy__(self, memo):
        return type(self).from_bytes(bytes(self))

    EvalError = EvalError

def _tree_hash(node: SExp, precalculated: Set[bytes32]) -> bytes32:
    """
    Hash values in `precalculated` are presumed to have been hashed already.
    """
    if node.listp():
        left = _tree_hash(node.first(), precalculated)
        right = _tree_hash(node.rest(), precalculated)
        s = b"\2" + left + right
    else:
        atom = node.as_atom()
        if atom in precalculated:
            return bytes32(atom)
        s = b"\1" + atom
    return bytes32(std_hash(s))

def _serialize(node) -> bytes:
    if type(node) == SerializedProgram:
        return bytes(node)
    else:
        return SExp.to(node).as_bin()

class SerializedProgram:
    """
    An opaque representation of a clvm program. It has a more limited interface than a full SExp
    """

    _buf: bytes = b""

    @classmethod
    def parse(cls, f) -> "SerializedProgram":
        length = serialized_length(f.getvalue()[f.tell() :])
        return SerializedProgram.from_bytes(f.read(length))

    def stream(self, f):
        f.write(self._buf)

    @classmethod
    def from_bytes(cls, blob: bytes) -> "SerializedProgram":
        ret = SerializedProgram()
        ret._buf = bytes(blob)
        return ret

    @classmethod
    def from_program(cls, p: Program) -> "SerializedProgram":
        ret = SerializedProgram()
        ret._buf = bytes(p)
        return ret

    def to_program(self) -> Program:
        return Program.from_bytes(self._buf)

    def uncurry(self) -> Tuple["Program", "Program"]:
        return self.to_program().uncurry()

    def __bytes__(self) -> bytes:
        return self._buf

    def __str__(self) -> str:
        return bytes(self).hex()

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, str(self))

    def __eq__(self, other) -> bool:
        if not isinstance(other, SerializedProgram):
            return False
        return self._buf == other._buf

    def __ne__(self, other) -> bool:
        if not isinstance(other, SerializedProgram):
            return True
        return self._buf != other._buf

    def get_tree_hash(self, *args: List[bytes32]) -> bytes32:
        """
        Any values in `args` that appear in the tree
        are presumed to have been hashed already.
        """
        tmp = sexp_from_stream(io.BytesIO(self._buf), SExp.to)
        return _tree_hash(tmp, set(args))

    def run_safe_with_cost(self, max_cost: int, *args) -> Tuple[int, Program]:
        return self._run(max_cost, STRICT_MODE, *args)

    def run_with_cost(self, max_cost: int, *args) -> Tuple[int, Program]:
        return self._run(max_cost, 0, *args)

    def _run(self, max_cost: int, flags, *args) -> Tuple[int, Program]:
        # when multiple arguments are passed, concatenate them into a serialized
        # buffer. Some arguments may already be in serialized form (e.g.
        # SerializedProgram) so we don't want to de-serialize those just to
        # serialize them back again. This is handled by _serialize()
        serialized_args = b""
        if len(args) > 1:
            # when we have more than one argument, serialize them into a list
            for a in args:
                serialized_args += b"\xff"
                serialized_args += _serialize(a)
            serialized_args += b"\x80"
        else:
            serialized_args += _serialize(args[0])

        # TODO: move this ugly magic into `clvm` "dialects"
        native_opcode_names_by_opcode = dict(
            ("op_%s" % OP_REWRITE.get(k, k), op) for op, k in KEYWORD_FROM_ATOM.items() if k not in "qa."
        )
        cost, ret = deserialize_and_run_program2(
            self._buf,
            serialized_args,
            KEYWORD_TO_ATOM["q"][0],
            KEYWORD_TO_ATOM["a"][0],
            native_opcode_names_by_opcode,
            max_cost,
            flags,
        )
        return cost, Program.to(ret)

NIL = Program.from_bytes(b"\x80")


"""Module for quad element with 4 nodes - type 3 in gmsh

"""
from diffuspy.element import Element
import numpy as np

class Quad4(Element):
    """Constructor of a 4-node quadrangle (TYPE 3) element

    """
    def __init__(self, eid, model, material):

        super().__init__(eid, model)

        # Nodal coordinates in the natural domain (isoparametric coordinates)
        self.XEZ = np.array([[-1.0, -1.0],
                             [1.0, -1.0],
                             [1.0, 1.0],
                             [-1.0, 1.0]])

        # check if conductivity was assigned
        try:
            self.λ = material.λ[self.surf]
        except AttributeError:
            print('Conductivity (λ) not defined!')
        except KeyError:
            print('Surface ', self.surf,
                  ' with no conductivity (λ) assigned!')

        # check if capacitance material properties were assigned
        # if not, just pass because it maybe not a transient analysis
        try:
            self.ρ = material.ρ[self.surf]
            self.c = material.c[self.surf]
        except:
            pass

        # check if its a boundary element
        if eid in model.bound_ele[:, 0]:

            # index where bound_ele refers to this element
            index = np.where(model.bound_ele[:, 0] == eid)[0]

            # side of the element at the boundary
            self.side_at_boundary = model.bound_ele[index, 1]

            # boundary line where the element side share interface
            self.at_boundary_line = model.bound_ele[index, 2]

        else:
            self.side_at_boundary = []
            self.at_boundary_line = []

    def shape_function(self, xez):
        """Create the basis function and evaluate them at xez coordinates

        Args:
            xez (array): position in the isoparametric coordinate xi, eta, zeta

        Return:
            N (array): shape functions

        """
        # variables in the natural (iso-parametric) domain
        e1 = xez[0]
        e2 = xez[1]

        # Terms of the shape function
        e1_term = 0.5*(1.0 + self.XEZ[:, 0] * e1)
        e2_term = 0.5*(1.0 + self.XEZ[:, 1] * e2)

        # Basis functions
        # N = [ N_1 N_2 N_3 N_4 ]
        N = e1_term*e2_term
        self.N = np.array(N)

        # Derivative of the shape functions
        # dN = [ dN1_e1 dN2_e1 ...
        #         dN1_e2 dN2_e2 ... ]
        self.dN_ei = np.zeros((2, 4))
        self.dN_ei[0, :] = 0.5 * self.XEZ[:, 0] * e2_term
        self.dN_ei[1, :] = 0.5 * self.XEZ[:, 1] * e1_term

        return self.N, self.dN_ei

    @staticmethod
    def mapping(N, xyz):
        """maps from cartesian to isoparametric.

        """
        x1, x2 = N @ xyz
        return x1, x2

    def jacobian(self, xyz, dN_ei):
        """Creates the Jacobian matrix of the mapping between an element

        Args:
            xyz (array of floats): coordinates of element nodes in cartesian
                coordinates
            dN_ei (array of floats): derivative of shape functions

        Return:
            det_jac (float): determinant of the jacobian matrix
            dN_xi (array of floats): derivative of shape function
                with respect to cartesian system
            arch_length (array of floats): arch length for change of variable
                in the line integral
        """
        # Jac = [ x1_e1 x2_e1
        #         x1_e2 x2_e2 ]
        Jac = dN_ei @ xyz

        det_jac = abs((Jac[0, 0]*Jac[1, 1] -
                       Jac[0, 1]*Jac[1, 0]))

        # jac_inv = [ e1_x1 e2_x1
        #            e1_x2 e2_x2 ]
        jac_inv = np.linalg.inv(Jac)

        # Using Chain rule,
        # N_xi = N_eI * eI_xi (2x8 array)
        dN_xi = np.zeros((2, 4))
        dN_xi[0, :] = (dN_ei[0, :]*jac_inv[0, 0] +
                       dN_ei[1, :]*jac_inv[0, 1])

        dN_xi[1, :] = (dN_ei[0, :]*jac_inv[1, 0] +
                       dN_ei[1, :]*jac_inv[1, 1])

        # Length of the transofmation arch
        # Jacobian for line integral-2.
        arch_length = np.array([
            (Jac[0, 0]**2 + Jac[0, 1]**2)**(1/2),
            (Jac[1, 0]**2 + Jac[1, 1]**2)**(1/2),
            (Jac[0, 0]**2 + Jac[0, 1]**2)**(1/2),
            (Jac[1, 0]**2 + Jac[1, 1]**2)**(1/2)
        ])
        return det_jac, dN_xi, arch_length

    def heat_stiffness_matrix(self, t=1):
        """Build the element heat (q) stiffness (k) matrix

        """
        k_q = np.zeros((4, 4))

        gauss_points = self.XEZ / np.sqrt(3.0)

        for gp in gauss_points:
            N, dN_ei = self.shape_function(xez=gp)
            dJ, dN_xi, _ = self.jacobian(self.xyz, dN_ei)

            B = dN_xi

            # Check if condutivity is a function
            if callable(self.λ) is True:
                x1, x2 = self.mapping(N, self.xyz)
                λ = self.λ(x1, x2, t)
            else:
                λ = self.λ

            k_q += λ * (B.T @ B) * dJ

        return k_q

    def heat_capacitance_matrix(self, t=1):
        """Build element matrix (k) due internal thermal energy storage (s)

        """
        k_s = np.zeros((4, 4))

        gauss_points = self.XEZ / np.sqrt(3.0)

        for gp in gauss_points:
            N, dN_ei = self.shape_function(xez=gp)
            dJ, dN_xi, _ = self.jacobian(self.xyz, dN_ei)

            # check if attribute and surface were assigned correctly
            try:
                # Check if specific heat is a function
                if callable(self.c) is True:
                    x1, x2 = self.mapping(N, self.xyz)
                    c = self.c(x1, x2, t)
                else:
                    c = self.c
            except AttributeError:
                print('Specific heat (c) not defined')
            except KeyError:
                print('Surface ', self.surf,
                      ' with no specific heat (c) assigned!')

            try:
                # Check if density is a function
                if callable(self.ρ) is True:
                    x1, x2 = self.mapping(N, self.xyz)
                    ρ = self.ρ(x1, x2, t)
                else:
                    ρ = self.ρ
            except AttributeError:
                print('Density (ρ) not defined')
            except KeyError:
                print('Surface ', self.surf,
                      ' with no density (ρ) assigned!')

            k_s += c*ρ*(np.atleast_2d(N).T @ np.atleast_2d(N))*dJ

        return k_s

    def heat_convection_matrix(self, h, t=1):
        """Build the element matrix (k) due convection boundary (c)

        """
        k_c = np.zeros((4, 4))

        gp = np.array([
            [[-1.0/np.sqrt(3), -1.0],
             [1.0/np.sqrt(3), -1.0]],
            [[1.0, -1.0/np.sqrt(3)],
             [1.0, 1.0/np.sqrt(3)]],
            [[-1.0/np.sqrt(3), 1.0],
             [1.0/np.sqrt(3), 1.0]],
            [[-1.0, -1.0/np.sqrt(3)],
             [-1.0, 1/np.sqrt(3)]]])

        # check if there is convection
        if h is not None:

            # loop for specified boundary conditions
            for key in h(1, 1).keys():
                line = key

                # loop over each boundary line that intersects the element
                # sides
                for ele_boundary_line, ele_side in zip(self.at_boundary_line,
                                                       self.side_at_boundary):
                    # Check if this element is at the line with convection bc
                    if line == ele_boundary_line:

                        # solve the integral with GQ
                        for w in range(2):
                            N, dN_ei = self.shape_function(xez=gp[ele_side, w])
                            _, _, arch_length = self.jacobian(self.xyz, dN_ei)

                            # check if condutance is a function
                            if callable(h) is True:
                                x1, x2 = self.mapping(N, self.xyz)
                                h_v = h(x1, x2, t)[line]
                            else:
                                h_v = h[line]

                            dL = arch_length[ele_side]

                            k_c += h_v * (
                                np.atleast_2d(N).T @ np.atleast_2d(N)) * dL

                    else:
                        # Catch element that is not at boundary
                        continue

        return k_c

    def heat_source_vector(self, σ_q=None, t=1, T_ip=1, dα=1, tol=1e-5):
        """Build the element vector due internal heat (q) source (σ)

        Args:
            T_ip: Nodal temperature form previous iteration i

        """
        gauss_points = self.XEZ / np.sqrt(3.0)

        pq = np.zeros(4)

        # find the average temperature of element
        # check if heat source is nonlinear in T
        if np.size(T_ip) > 1:
            T_avg = np.average(T_ip[self.conn])

        for gp in gauss_points:
            N, dN_ei = self.shape_function(xez=gp)
            dJ, dN_xi, _ = self.jacobian(self.xyz, dN_ei)

            x1, x2 = self.mapping(N, self.xyz)

            if σ_q is not None:
                if 'Reaction Degree' in σ_q.__defaults__:
                    pq[:] += N[:] * σ_q(x1, x2, t=t, dα=dα) * dJ
                elif 'Temperature' in σ_q.__defaults__:
                    pq[:] += N[:] * σ_q(x1, x2, t=t, T=T_avg) * dJ
                else:
                    pq[:] += N[:] * σ_q(x1, x2, t=t) * dJ
        return pq

    def heat_boundary_flux_vector(self, q_bc, t=1):
        """Build element load vector due q_bc boundary condition

        """
        gp = np.array([
            [[-1.0/np.sqrt(3), -1.0],
             [1.0/np.sqrt(3), -1.0]],
            [[1.0, -1.0/np.sqrt(3)],
             [1.0, 1.0/np.sqrt(3)]],
            [[-1.0/np.sqrt(3), 1.0],
             [1.0/np.sqrt(3), 1.0]],
            [[-1.0, -1.0/np.sqrt(3)],
             [-1.0, 1/np.sqrt(3)]]])

        p_t = np.zeros(4)

        if q_bc is not None:

            # loop for specified boundary conditions
            for key in q_bc(1, 1).keys():
                line = key

                for ele_boundary_line, ele_side in zip(self.at_boundary_line,
                                                       self.side_at_boundary):
                    # Check if this element is at the line with traction
                    if line == ele_boundary_line:

                        # solve the integral with GQ
                        for w in range(2):
                            N, dN_ei = self.shape_function(xez=gp[ele_side, w])
                            _, _, arch_length = self.jacobian(self.xyz, dN_ei)

                            dL = arch_length[ele_side]
                            x1, x2 = self.mapping(N, self.xyz)

                            p_t[:] += N[:] * q_bc(x1, x2, t)[line] * dL

                    else:
                        # Catch element that is not at boundary
                        continue

        return p_t

    def heat_boundary_convection_vector(self, T_a, h, t=1):
        """Build the element heat vector due convection bc

        """
        gp = np.array([
            [[-1.0/np.sqrt(3), -1.0],
             [1.0/np.sqrt(3), -1.0]],
            [[1.0, -1.0/np.sqrt(3)],
             [1.0, 1.0/np.sqrt(3)]],
            [[-1.0/np.sqrt(3), 1.0],
             [1.0/np.sqrt(3), 1.0]],
            [[-1.0, -1.0/np.sqrt(3)],
             [-1.0, 1/np.sqrt(3)]]])

        p_c = np.zeros(4)

        # Try compute the vector due convection
        if h is not None:

            # loop for specified boundary condition line
            for key in h(1, 1).keys():
                line = key

                for ele_boundary_line, ele_side in zip(self.at_boundary_line,
                                                       self.side_at_boundary):
                    # Check if this element is at the line with traction
                    if line == ele_boundary_line:

                        # solve the integral with GQ
                        for w in range(2):
                            N, dN_ei = self.shape_function(xez=gp[ele_side, w])
                            _, _, arch_length = self.jacobian(self.xyz, dN_ei)

                            dL = arch_length[ele_side]

                            # check if condutance is a function
                            if callable(h) is True:
                                x1, x2 = self.mapping(N, self.xyz)
                                h_v = h(x1, x2, t)[line]
                            else:
                                h_v = h[line]

                            # check if the surrounded fluid temperature is
                            # a function
                            if callable(T_a) is True:
                                x1, x2 = self.mapping(N, self.xyz)
                                T_a_v = T_a(x1, x2, t)[line]
                            else:
                                T_a_v = T_a[line]

                            p_c[:] += N[:] * h_v * T_a_v * dL

                    else:
                        # Catch element that is not at boundary
                        continue

        return p_c


import subprocess
import numpy as np

# Tamaño de las matrices
n = 8

# Crear matrices aleatorias en Python entre 1 y 5 (con decimales)
A = np.random.randint(1, 6, size=(n, n))
B = np.random.randint(1, 6, size=(n, n))

# girar la matriz 90 grados a la derecha

# Ejecutar el programa en C
# Asegúrate de que este sea el nombre correcto del ejecutable
program_name = "./bin"
num_threads = 8

# Construct the command to run the C program with arguments
command = [program_name, str(num_threads), str(n)]

process = subprocess.Popen(command, stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE, text=True)

# Construct the input data as you were doing
input_data = "\n".join(" ".join(str(val) for val in row) for row in A) + \
    "\n" + "\n".join(" ".join(str(val) for val in row) for row in B)

stdout, stderr = process.communicate(input=input_data)

# Verificar que no haya habido errores en la ejecución
if process.returncode != 0:
    print("Error al ejecutar el programa en C:")
    print(stderr)
else:
    # Obtener la matriz resultante del programa en C
    C = np.array([[float(val) for val in row.split()]
                 for row in stdout.strip().split('\n')])

    # Calcular la multiplicación de matrices en Python
    expected_result = np.dot(A, B)
    print("Matriz A:")
    print(A)
    print("Matriz B:")
    print(B)
    print("Matriz C:")
    print(C)
    print("Matriz esperada:")
    print(expected_result)

    # Verificar si las matrices son iguales dentro de una pequeña tolerancia
    tolerance = 1e-6
    if np.allclose(C, expected_result, rtol=tolerance, atol=tolerance):
        print("La multiplicación de matrices es correcta.")
    else:
        print("La multiplicación de matrices es incorrecta.")


n = int(input())
array = []
for i in range(n):
    array.append(int(input()))

def merge_sort(array):
    def sort(low, high):
        if high - low < 2:
            return
        mid = (low + high) // 2
        sort(low, mid)
        sort(mid, high)
        merge(low, mid, high)

    def merge(low, mid, high):
        arr = []
        l, h = low, mid

        while l < mid and h < high:
            if array[l] < array[h]:
                arr.append(array[l])
                l += 1
            else:
                arr.append(array[h])
                h += 1
        while l < mid:
            arr.append(array[l])
            l += 1
        while h < high:
            arr.append(array[h])
            h += 1
        for i in range(low, high):
            array[i] = arr[i-low]
    return sort(0, len(array))

merge_sort(array)
for i in range(n):
    print(array[i])


from django.test import SimpleTestCase
from website.forms import CreateUserForm, SignUpForm, FeedbackForm, PatientForm, DocumentationP, EventForm, MessageForm, RequestForm
from website.models import Patient, SignUp, Feedback, Documentation, Event, Messages, Requests

class TestForms(SimpleTestCase):

    def test_create_user_form(self):
        form = CreateUserForm(data={
            'model': ['Patient'],
            'fields': ['dina', 'balua']
        })

    def test_sign_up_form(self):
        form = SignUpForm(data={
            'model': ['SignUp'],
            'fields': ['lior', 'inbar', 16, 'man', 'dinab@gmail', +972855555555, 'Canada', 'write']
        })

    def test_feedback_form(self):
        form = FeedbackForm(data={
            'model': ['Feedback'],
            'fields': ['dina', 'balua', 'message']
        })

    def test_patient_form(self):
        form = PatientForm(data={
            'model': ['Patient'],
            'fields': ['dan']
        })

    def test_documentation_form(self):
        form = DocumentationP(data={
            'model': ['Documentation'],
            'fields': ['inbar', 'balua', 'message', 'meeting', 'diagnosis']
        })

    def test_event_form(self):
        form = EventForm(data={
            'model': ['Event'],
            'fields': ['avihai', 27/7/92]
        })

    def test_message_form(self):
        form = MessageForm(data={
            'model': ['Messages'],
            'fields': ['vika', 18/3/98]
        })

    def test_request_form(self):
        form = RequestForm(data={
            'model': ['Requests'],
            'fields': ['lior', 27/10/1994]
        })


from django.contrib.auth.models import User
from django.shortcuts import render
from profile_app.models import UserProfileInfo
from video_app.models import Video
from comment_app.models import Comment
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

# Create your views here.
@login_required
def write_comment(request, video_id):
	if request.method == 'POST':
		user = request.user
		userprofileinfo = UserProfileInfo.objects.get(user=user)
		text = request.POST.get('text')
		video = Video.objects.get(video_id=video_id)
		comment = Comment(text=text, userprofileinfo = userprofileinfo, video=video)
		comment.save()

		response_data = {
			'result': 'success',
			'id': comment.id,
			'text': comment.text,
			'userprofileinfo': comment.userprofileinfo.user.username,
			'date': comment.date,
			'video': comment.video.title,
		}

		return JsonResponse(response_data)

	else:
		error = {'error': 'Non POST method not allowed'}
		return JsonResponse(error)


import numpy as np
import itertools
import cv2

def draw_epipolar_lines(img_left, img_right):
    height = np.shape(img_left)[0]
    divisions = 40.0
    colors = [(255,0,0), (0,0,255), (0,255,0), (255,255,0), (255,255,255), (0,255,255)]
    color_generator = itertools.cycle(colors)
    step = int(np.floor(height/divisions))
    stop = int(divisions*step)
    img = np.hstack([img_left, img_right])

    for col in range(0,stop-1, step):
        img[col, :, :] = next(color_generator)

    return img

def rectify_images(left_img, right_img, left_K, right_K, transl_RL_R, rot_RL, crop_parameter):
    left_img_size = left_img.shape[0:2][::-1]
    right_img_size = right_img.shape[0:2][::-1]
    distCoeffs = None

    R1,R2,P1,P2,Q,_,_ = cv2.stereoRectify(left_K, distCoeffs, right_K, distCoeffs, left_img_size, rot_RL, transl_RL_R, alpha=crop_parameter)
    left_maps = cv2.initUndistortRectifyMap(left_K, distCoeffs, R1, P1, left_img_size, cv2.CV_16SC2)
    right_maps = cv2.initUndistortRectifyMap(right_K, distCoeffs, R2, P2, right_img_size, cv2.CV_16SC2)
    left_img_remap = cv2.remap(left_img, left_maps[0], left_maps[1], cv2.INTER_LANCZOS4)
    right_img_remap = cv2.remap(right_img, right_maps[0], right_maps[1], cv2.INTER_LANCZOS4)
    return left_img_remap, right_img_remap

def filter_images(bright_img, no_light_img, treshold=0):
    mask = (bright_img<(no_light_img+treshold))
    filtered_img = bright_img
    filtered_img[mask] = 0
    return filtered_img

def nothing(x):
    pass

def stereo_SGBM_tuner(img1, img2):
    win_name = 'window'

    cv2.namedWindow(win_name)
    cv2.createTrackbar("disparity_min", win_name, 20, 10, nothing)
    cv2.createTrackbar("disparity_num", win_name, 20,50, nothing)
    win_size = 5
    min_disp = -1
    max_disp = 63
    num_disp = max_disp - min_disp
    uniqueness_ratio = 5
    block_size = 5

    while(1):
        min_disp = cv2.getTrackbarPos("disparity_min", win_name) * 16
        num_disp = cv2.getTrackbarPos("disparity_num", win_name) * 16
        print(num_disp)
        assert(num_disp % 16 is 0)

        stereo_SGBM = cv2.StereoSGBM_create(min_disp, num_disp, block_size)
        disp = stereo_SGBM.compute(img2, img1)

        cv2.imshow(win_name,disp)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            cv2.destroyAllWindows()
            break

    cv2.destroyAllWindows()

if __name__ == '__main__':
    pass


import xlrd   #读取excel
import xlwt   #写入excel
from datetime import date,datetime

def read_excel(name):
    #打开文件
    workbook = xlrd.open_workbook('../data/' + name + '.xlsx')
    #获取所有sheet
    # print(workbook.sheet_names())   #只有一张表
    sheet_name = workbook.sheet_names()[0]

    #根据sheet索引或者名称获取sheet内容
    sheet = workbook.sheet_by_index(0)   #sheet索引从0开始
    # sheets = workbook.sheet_by_name('Sheet1')

    # sheet的名称,行数,列数
    # print(sheet.name,sheet.nrows,sheet.ncols)

    #获取整行, 整列的值(数组)
    # rows = sheet.row_values(1)  #获取第二行的内容
    f = open('../data/' + name + '.csv','w+')
    string = ''
    for k in range(sheet.nrows):
        rows = sheet.row_values(k)
        # print(rows)
        for i in range(sheet.ncols):
            if i == 0:
                if k == 0:
                    string = str(rows[i])
                else:
                    string = str(int(rows[i]))

            else:
                if k == 0:
                    string += ',' + str(rows[i])
                else:
                    string += ',' + str(int(rows[i]))
        print(string, file = f)
        string = ''
    # cols = sheet.col_values(2)  #获取第三列的内容
    # print('rows:',rows)
    # print('cols:',cols)

    #获取单元格内容
    # print(sheet.cell(0,0).value)
    # print(sheet.cell(0,0).value.encode('utf-8'))
    #获取单元格内容的数据类型
    # print(sheet.cell(1,0).ctype)
if __name__ == "__main__":
    roads = ['airport','lihua','zhenning','jianshe4','jianshe3','jianshe2','jianshe1']
    for i in range(len(roads)):
        read_excel(roads[i])


"""
Project Tasks that can be invoked using using the program "invoke" or "inv"
"""

import os
from invoke import task

# disable the check for unused-arguments to ignore unused ctx parameter in tasks
# pylint: disable=unused-argument

IS_WINDOWS = os.name == "nt"
if IS_WINDOWS:
    # setting 'shell' is a work around for issue #345 of invoke
    RUN_ARGS = {"pty": False, "shell": r"C:\Windows\System32\cmd.exe"}
else:
    RUN_ARGS = {"pty": True}

def get_files():
    """
    Get the files to run analysis on
    """
    files = [
        "dploy",
        "tests",
        "tasks.py",
    ]
    files_string = " ".join(files)
    return files_string

@task
def setup(ctx):
    """
    Install python requirements
    """
    ctx.run("python -m pip install -r requirements.txt", **RUN_ARGS)

@task
def clean(ctx):
    """
    Clean repository using git
    """
    ctx.run("git clean --interactive", **RUN_ARGS)

@task
def lint(ctx):
    """
    Run pylint on this module
    """
    cmds = ["pylint --output-format=parseable", "flake8"]
    base_cmd = "python -m {cmd} {files}"

    for cmd in cmds:
        ctx.run(base_cmd.format(cmd=cmd, files=get_files()), **RUN_ARGS)

@task
def reformat_check(ctx):
    """
    Run formatting check
    """
    cmd = "black --check"
    base_cmd = "python -m {cmd} {files}"
    ctx.run(base_cmd.format(cmd=cmd, files=get_files()), **RUN_ARGS)

@task
def reformat(ctx):
    """
    Run formatting
    """
    cmd = "black"
    base_cmd = "python -m {cmd} {files}"
    ctx.run(base_cmd.format(cmd=cmd, files=get_files()), **RUN_ARGS)

@task
def metrics(ctx):
    """
    Run radon code metrics on this module
    """
    cmd = "radon {metric} --min B {files}"
    metrics_to_run = ["cc", "mi"]
    for metric in metrics_to_run:
        ctx.run(cmd.format(metric=metric, files=get_files()), **RUN_ARGS)

@task()
def test(ctx):
    """
    Test Task
    """
    # Use py.test instead of the recommended pytest so it works on Python 3.3
    cmd = "py.test --cov-report term-missing --cov=dploy --color=no"
    ctx.run(cmd, **RUN_ARGS)

# pylint: disable=redefined-builtin
@task(test, lint, reformat_check)
def all(default=True):
    """
    All tasks minus
    """

@task(clean)
def build(ctx):
    """
    Task to build an executable using pyinstaller
    """
    cmd = "pyinstaller -n dploy --onefile " + os.path.join("dploy", "__main__.py")
    ctx.run(cmd, **RUN_ARGS)


from __future__ import print_function
import socket
import sys
import os
import re
import logging
import datetime

"""
FTPClient object requires:
- HOST (IP address or domain)
- PORT (Integer value between 0-99999)
- COMMANDS (List of Strings: LIST|PUT|GET followed by filename)

CTRL+C to exit client
"""

EXAMPLE_INPUT = "\n - Example input: python client.py <domain/ip> <port> <put filename|get filename|list>"

class FTPClient:

    def __init__(self, host, port, command):
        logging.basicConfig(filename='client.log', level=logging.DEBUG)
        self.cli_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = self.check_host(host)
        self.port = self.check_port(port)
        self.command = self.check_command(command)
        self.connected = False

        self.protocol_commands = {
            "put": self.put_file,
            "get": self.get_file,
            "list": self.show_list
        }

        self.protocol_errors = {
            "FileAlreadyExists": "File already exists in current directory",
            "FileNotFound": "File could not be found in current directory",
            "FileTooLarge": "File is too large to transfer (over 5GB in size)",
            "FileZeroSized": "File is a zero-sized file (does not contain data)",
            "FileNameTooLong": "Filename of file is too long (over 255 chars)",
            "FileIsDirectory": "File is actually a directory (folder containing files)"
        }

        self.protocol_messages = {
            "FileOkTransfer": "No existing file present, OK to create new file.",
            "FileSizeReceived": "The filesize of file being transferred has successfully been received."
        }

    def log(self, ctype, message):
        # Logs passed message with date and time to client.log
        date = str(datetime.datetime.now()).split(".")[0]
        line = "[%s] %s" % (ctype, message)
        logging.info("%s | %s" % (date, line))

        if ctype == "ERR":

            try:
                self.disconnect()
            except OSError:
                pass

            raise SystemExit("[ERR] %s" % message)
        print(line)

    @staticmethod
    def get_filesize(size_bytes):
        # Converts bytes to larger suffix
        # Returns converted filesize as a string
        sizes = ['B', 'KB', 'MB', 'GB']
        i = 0
        while size_bytes > 1024 and i < 5:
            size_bytes = size_bytes / 1024.00
            i += 1
        return "%0.2f%s" % (size_bytes, sizes[i])

    # Arguement Checkers

    def check_command(self, command):
        cmd_type = command[0].lower()

        if cmd_type not in ["list", "put", "get"]:
            self.log("ERR", "The parameter %s is not supported by this client. Try: %s" % (cmd_type, EXAMPLE_INPUT))

        if (cmd_type == "put" or cmd_type == "get") and len(command) != 2:
            self.log("ERR",
                     "The \"%s\" command must be followed by the <filename> field. Try: %s" % (cmd_type, EXAMPLE_INPUT))

        return command

    def check_host(self, host):

        if host.lower() != "localhost" and (" " in host or not re.match(r"^[a-zA-Z0-9_.-]*$", host)):
            self.log("ERR", "The domain/IP address provided contains spaces and/or special characters. " +
                     "Allowed characters: letters, numbers, periods, dashes and underscores.")
        return host

    def check_port(self, port):

        if not port.isdigit() or not (1 <= len(port) <= 5):
            self.log("ERR", "The port parameter that has been provided is too short/long or is not a numerical value")
        if int(port) < 0:
            self.log("ERR", "The port parameter that has been provided is not a positive numerical value")

        return int(port)

    def start(self):
        self.log("OK!", "Client startup initialised.")

        # Parse command list and check if valid command. Also, check if command needs the parameter filename
        if self.command[0] == "list":
            self.protocol_commands[self.command[0]]()
        else:
            self.protocol_commands[self.command[0]](filename=self.command[1])
            # After command execution, notify server of disconnect and close socket on client side.
            # self.disconnect()

    def connect(self):
        try:
            # Try connect to server. If connection refused, log and raise SystemExit
            self.cli_socket.connect((self.host, self.port))
            self.log("CON", "Successfully connected to server at: %s:%s" % (self.host, self.port))
            self.connected = True
        except (socket.gaierror, ConnectionRefusedError) as e:
            self.cli_socket.close()
            self.log("ERR", "An error occurred when connecting to host %s:%s\n%s" % (self.host, self.port, str(e)))

    def disconnect(self):
        # Notify server of disconnect, then close client.
        if self.connected:
            self.connected = False
            self.cli_socket.send(b"DISCONNECT")
            self.log("DIS", "Disconnected from server.")

    # Command execution
    def put_file(self, filename):
        # Check file/filename for security/file issues
        if filename not in os.listdir(os.getcwd()):
            self.cli_socket.sendall(b"FileNotFound")
            self.log("ERR", "FileNotFound: " + self.protocol_errors["FileNotFound"] + " (server).")

        elif len(filename) > 255:
            self.cli_socket.sendall(b"FileNameTooLong")
            self.log("ERR", "FileNameTooLong: " + self.protocol_errors["FileNameTooLong"])

        elif os.path.isdir('%s/%s' % (os.getcwd(), filename)):
            self.cli_socket.sendall(b"FileIsDirectory")
            self.log("ERR", "FileIsDirectory: " + self.protocol_errors["FileIsDirectory"])

        elif os.path.getsize(('%s/%s' % (os.getcwd(), filename))) > 5368709120:
            self.cli_socket.sendall(b"FileTooLarge")
            self.log("ERR", "FileTooLarge: " + self.protocol_errors["FileTooLarge"])

        elif os.path.getsize(('%s/%s' % (os.getcwd(), filename))) == 0:
            self.cli_socket.sendall(b"FileZeroSized")
            self.log("ERR", "FileZeroSized: " + self.protocol_errors["FileZeroSized"])

        else:
            self.log("OK!", "File '%s' found in client directory. Sending server total file-size." % filename)
            self.connect()
            self.cli_socket.sendall(("PUT " + filename).encode())

            # send client the filesize of file being sent.
            response = self.cli_socket.recv(24).decode()

            if response in self.protocol_errors:
                self.log("ERR", "Server response: \"%s\" - %s" % (response, self.protocol_errors[response]))

            elif response in self.protocol_messages:
                filesize = str(os.path.getsize(os.getcwd() + '/' + filename))
                self.cli_socket.sendall(filesize.encode())

                max_size = self.get_filesize(int(filesize))
                bytes_sent = 0

                upload = open(os.getcwd() + '/' + filename, 'rb')
                data = upload.read(4096)

                while data:
                    bytes_sent += len(data)
                    current_size = self.get_filesize(bytes_sent)
                    print("[UPL] Uploading '%s' [%s / %s]\t" % (filename, current_size, max_size), end='\r')
                    self.cli_socket.sendall(data)
                    data = upload.read(4096)
                self.log("UPL", "Upload Complete '%s' [%s / %s]" % (filename, current_size, max_size))

    def get_file(self, filename):
        # send GET request to server, w/ filename
        self.log("CMD", "Invoking Server Protocol 'GET' command with filename: %s" % filename)

        # If filename exists in client directory, do not continue
        if filename in os.listdir(os.getcwd()):
            self.log("ERR", "FileAlreadyExists: " + self.protocol_errors["FileAlreadyExists"] + " (client).")

        self.connect()
        self.cli_socket.sendall(("GET " + filename).encode())

        # If server responds with a protocol error, log and raise SystemExit
        response = self.cli_socket.recv(1024).decode()
        if response in self.protocol_errors:
            self.log("ERR", "Server response: \"%s\" - %s" % (response, self.protocol_errors[response]))
        elif response in self.protocol_messages:
            self.log("OK!", "Server response: \"%s\" - %s" % (response, self.protocol_messages[response]))

        # Else server has resonded with filesize. Continue with downloading file.
        file_size = int(response)
        bytes_collected = 0
        max_size = self.get_filesize(file_size)
        download_file = open(filename, 'wb')

        # Write downloded byte data to a file named by filename received form server.
        while bytes_collected < file_size:
            data = self.cli_socket.recv(4096)
            bytes_collected += len(data)
            current_size = self.get_filesize(bytes_collected)
            download_file.write(data)
            print("[DWN] Downloading '%s' [%s / %s]" % (filename, current_size, max_size), end='\r')

        # Once filesize matches the downloaded bytes we have received, close file (download complete).
        download_file.close()
        self.log("DWN", "Download Complete '%s' [%s / %s]" % (filename, current_size, max_size))
        self.log("OK!", "File saved to: %s/%s" % (os.getcwd(), filename))

    def show_list(self):
        # send LIST request to server, w/ no other parameters.
        self.log("CMD", "Invoking Server Protocol 'LIST' command.")
        self.connect()
        self.cli_socket.sendall("LIST".encode())

        # If response is empty, log and raise SystemExit. Else, print response.
        response = self.cli_socket.recv(16384)
        if response:
            self.log("OK!", "Server responded with:\n%s" % response.decode())
        else:
            self.log("ERR", "Server responded without a file list.")

if __name__ == '__main__':
    if len(sys.argv) < 4:
        raise SystemExit("[ERR] The domain/IP and port parameters are required:\n" + EXAMPLE_INPUT)

    client = FTPClient(host=sys.argv[1], port=sys.argv[2], command=sys.argv[3:])
    client.start()


from itertools import chain

from . import builder
from .. import options as opts, safe_str, shell
from .common import Builder, choose_builder, SimpleBuildCommand
from ..file_types import HeaderFile, SourceFile
from ..iterutils import iterate
from ..languages import known_langs
from ..path import Path
from ..versioning import detect_version

# Set the source language to C++, since we want to be able to use the C++
# language definition to infer whether a file passed to `moc` is a source or
# header file based on its extension.
with known_langs.make('qtmoc', src_lang='c++') as x:
    x.vars(compiler='MOC', flags='MOCFLAGS')

with known_langs.make('qrc') as x:
    x.vars(compiler='RCC', flags='RCCFLAGS')
    x.exts(source=['.qrc'])

with known_langs.make('qtui') as x:
    x.vars(compiler='UIC', flags='UICFLAGS')
    x.exts(source=['.ui'])

@builder('qtmoc')
def moc_builder(env):
    return choose_builder(env, known_langs['qtmoc'], (MocBuilder,),
                          default_candidates=['moc'])

class MocBuilder(Builder):
    def __init__(self, env, langinfo, command, found, version_output):
        super().__init__(langinfo.name, *self._parse_brand(version_output))

        name = langinfo.var('compiler').lower()
        mocflags_name = langinfo.var('flags').lower()
        mocflags = shell.split(env.getvar(langinfo.var('flags'), ''))

        self.transpiler = MocCompiler(
            self, env, command=(name, command, found),
            flags=(mocflags_name, mocflags)
        )

    @staticmethod
    def _parse_brand(version_output):
        if 'moc' in version_output:
            return 'qt', detect_version(version_output)
        return 'unknown', None

    @staticmethod
    def check_command(env, command):
        return env.execute(command + ['--version'], stdout=shell.Mode.pipe,
                           stderr=shell.Mode.devnull)

class MocCompiler(SimpleBuildCommand):
    @property
    def deps_flavor(self):
        return None

    def _call(self, cmd, input, output, flags=None):
        return list(chain(
            cmd, iterate(flags), [input, '-o', output]
        ))

    def default_name(self, input, step):
        if isinstance(input, SourceFile):
            return input.path.stripext('.moc').suffix
        base, leaf = input.path.stripext(
            known_langs['c++'].default_ext('source')
        ).splitleaf()
        return base.append('moc_' + leaf).suffix

    def output_file(self, name, step):
        return SourceFile(Path(name), 'c++')

    def flags(self, options, global_options=None, output=None, mode='normal'):
        flags = []
        for i in options:
            if isinstance(i, opts.include_dir):
                flags.append('-I' + i.directory.path)
            elif isinstance(i, opts.define):
                if i.value:
                    flags.append('-D' + i.name + '=' + i.value)
                else:
                    flags.append('-D' + i.name)
            elif isinstance(i, opts.warning):
                for j in i.value:
                    if j == opts.WarningValue.disable:
                        flags.append('--no-warnings')
                    else:
                        raise ValueError('unsupported warning level {!r}'
                                         .format(j))
            elif isinstance(i, safe_str.stringy_types):
                flags.append(i)
            else:
                raise TypeError('unknown option type {!r}'.format(type(i)))
        return flags

@builder('qrc')
def qrc_builder(env):
    return choose_builder(env, known_langs['qrc'], (RccBuilder,),
                          default_candidates=['rcc'])

class RccBuilder(Builder):
    def __init__(self, env, langinfo, command, found, version_output):
        super().__init__(langinfo.name, *self._parse_brand(version_output))

        name = langinfo.var('compiler').lower()
        rccflags_name = langinfo.var('flags').lower()
        rccflags = shell.split(env.getvar(langinfo.var('flags'), ''))

        self.transpiler = RccCompiler(
            self, env, command=(name, command, found),
            flags=(rccflags_name, rccflags)
        )

    @staticmethod
    def _parse_brand(version_output):
        if 'rcc' in version_output:
            return 'qt', detect_version(version_output)
        return 'unknown', None

    @staticmethod
    def check_command(env, command):
        return env.execute(command + ['--version'], stdout=shell.Mode.pipe,
                           stderr=shell.Mode.devnull)

class RccCompiler(SimpleBuildCommand):
    @property
    def deps_flavor(self):
        return 'gcc'

    def _call(self, cmd, input, output, deps=None, flags=None):
        result = list(chain(cmd, iterate(flags), [input, '-o', output]))
        if deps:
            return self.env.tool('rccdep')(result, deps)
        return result

    def default_name(self, input, step):
        return input.path.stripext(
            known_langs['c++'].default_ext('source')
        ).suffix

    def output_file(self, name, step):
        return SourceFile(Path(name), 'c++')

    def flags(self, options, global_options=None, output=None, mode='normal'):
        flags = []
        for i in options:
            if isinstance(i, safe_str.stringy_types):
                flags.append(i)
            else:
                raise TypeError('unknown option type {!r}'.format(type(i)))
        return flags

@builder('qtui')
def qtui_builder(env):
    return choose_builder(env, known_langs['qtui'], (UicBuilder,),
                          default_candidates=['uic'])

class UicBuilder(Builder):
    def __init__(self, env, langinfo, command, found, version_output):
        super().__init__(langinfo.name, *self._parse_brand(version_output))

        name = langinfo.var('compiler').lower()
        uicflags_name = langinfo.var('flags').lower()
        uicflags = shell.split(env.getvar(langinfo.var('flags'), ''))

        self.transpiler = UicCompiler(
            self, env, command=(name, command, found),
            flags=(uicflags_name, uicflags)
        )

    @staticmethod
    def _parse_brand(version_output):
        if 'uic' in version_output:
            return 'qt', detect_version(version_output)
        return 'unknown', None

    @staticmethod
    def check_command(env, command):
        return env.execute(command + ['--version'], stdout=shell.Mode.pipe,
                           stderr=shell.Mode.devnull)

class UicCompiler(SimpleBuildCommand):
    @property
    def deps_flavor(self):
        return None

    def _call(self, cmd, input, output, flags=None):
        return list(chain(
            cmd, iterate(flags), [input, '-o', output]
        ))

    def default_name(self, input, step):
        base, leaf = input.path.stripext('.h').splitleaf()
        return base.append('ui_' + leaf).suffix

    def output_file(self, name, step):
        return HeaderFile(Path(name), 'c++')

    def flags(self, options, global_options=None, output=None, mode='normal'):
        flags = []
        for i in options:
            if isinstance(i, safe_str.stringy_types):
                flags.append(i)
            else:
                raise TypeError('unknown option type {!r}'.format(type(i)))
        return flags


#https://leetcode.com/problems/xor-operation-in-an-array/
class Solution:
    def xorOperation(self, n: int, start: int) -> int:
        ans=[]
        for i in range(1,n+1,1):
            ans.append(start+2*(i-1))
        l=ans[0]
        ans=ans[1:]
        for x in ans:
            l=l^x
        return l


#!/usr/bin/python
import sys

#input should be space-separated lines of
#waterfall-like quantites
#with time on the vertical axis
#types on horizontal axis
#bar heights as values

linecnt = 0

while 1:
	line = sys.stdin.readline()
	if len(line) == 0:
		break
	colcnt  = 0
	for col in line.split():
		print('%d %d %d' % (linecnt, colcnt, float(col)))
		colcnt += 1
	print()
	linecnt += 1


'''
candidate generation: writes a pickle file of candidates

'''

import sys
import nltk
import numpy as np

from ncbi_normalization import load, sample
from ncbi_normalization.parse_MEDIC_dictionary import concept_obj

from normalize import dump_data, load_data, load_mentions

from gensim.models import KeyedVectors

def prepare_embedding_vocab(filename, binary = True, limit = 1000000):
    '''filename: '~/disease-normalization/data/embeddings/wvec_50_haodi-li-et-al.bin'
       1. Use gensim for reading in embedding model
       2. Sort based on the index to make sure that they are in the correct order
       3. Normalize the vectors
       4. Build vocabulary mappings, zero for padding
       5. Create an inverse dictionary
    '''
    vector_model = KeyedVectors.load_word2vec_format(filename, binary = binary, limit = limit)
    #vector_model=KeyedVectors.load_word2vec_format(config['embedding']['emb_file'], binary=True, limit=50000)
    words = [k for k,v in sorted(vector_model.vocab.items(),key = lambda x:x[1].index)]
    vector_model.init_sims(replace = True)
    vocabulary={"<SPECIAL>": 0, "<OOV>": 1}
    for word in words:
        vocabulary.setdefault(word, len(vocabulary))
    inversed_vocabulary={value:key for key, value in vocabulary.items()}
    return vector_model, vocabulary, inversed_vocabulary

def load_pretrained_word_embeddings(vocab,embedding_model):
    """vocab: vocabulary from data vectorizer
       embedding_model: model loaded with gensim"""
    pretrained_embeddings = np.random.uniform(low=-0.05, high=0.05, size=(len(vocab)-1,embedding_model.vectors.shape[1]))
    pretrained_embeddings = np.vstack((np.zeros(shape=(1,embedding_model.vectors.shape[1])), pretrained_embeddings))
    found=0
    for word,idx in vocab.items():
        if word in embedding_model.vocab:
            pretrained_embeddings[idx]=embedding_model.get_vector(word)
            found+=1
    print("Found pretrained vectors for {found} words.".format(found=found))
    return pretrained_embeddings

def load_concepts(dict_file,order):
    '''
    dict_file: directory to the tsv file of MEDIC dictionary
    dictionary.loaded format:
        dictionary of entries, key = canonical id, value = named tuple in the form of
        MEDIC_ENTRY(DiseaseID='MESH:D005671', DiseaseName='Fused Teeth',
        AllDiseaseIDs=('MESH:D005671',), AllNames=('Fused Teeth', 'Teeth, Fused')
    '''
    # MEDIC dictionary
    dictionary = load.Terminology()
    dictionary.loaded = load.load(dict_file,'MEDIC')

    concept = concept_obj(dictionary,order=order)
    concept.names = [name.lower() for name in concept.names]

    return concept, dictionary

def span_to_sum_of_w2v(spans,vocabulary,pretrained):
    '''
    represent all spans by sum of w2v
    '''
    embeddings = []
    for span in spans:
        tokenized = nltk.word_tokenize(span.lower())
        index = [vocabulary.get(token,1) for token in tokenized]
        #emb = np.mean(np.array([pretrained[i] for i in index]), axis=0)
        emb = np.sum(np.array([pretrained[i] for i in index]), axis=0)
        embeddings.append(emb)
    embeddings = np.array(embeddings)
    return embeddings

def cosine_similarity_candidates(mention_spans,concept_spans,emb_path,n_cossim):
    '''
    yields list of list of candidates

    n_cossim = number of candidates for each mention
    '''
    # prepare embeddings
    vector_model, vocabulary, inversed_vocabulary = prepare_embedding_vocab(emb_path, binary = True)
    pretrained = load_pretrained_word_embeddings(vocabulary, vector_model)

    # vector representations
    mention_embeddings = span_to_sum_of_w2v(mention_spans,vocabulary,pretrained)
    concept_embeddings = span_to_sum_of_w2v(concept_spans,vocabulary,pretrained)

    from sklearn.preprocessing import normalize
    concept_embeddings = normalize(concept_embeddings)
    mention_embeddings = normalize(mention_embeddings)

    dot_product_matrix = np.dot(mention_embeddings,np.transpose(concept_embeddings))
    dot_product_matrix = dot_product_matrix.tolist()
    candidate_indices = [np.argpartition(np.array(mention_candidates),-n_cossim)[-n_cossim:].tolist() for mention_candidates in dot_product_matrix]

    return candidate_indices

def jaccard_distance_candidates(mention_spans,concept_spans,n_jaccard):
    candidate_indices = []
    for mention in mention_spans:
        distances = [nltk.jaccard_distance(set(mention),set(concept)) for concept in concept_spans]
        indices = np.argpartition(np.array(distances),-n_jaccard)[-n_jaccard:].tolist()
        candidate_indices.append(indices)
    return candidate_indices

if __name__ == "__main__":
    '''
    1. prepare concept spans & mention spans
    2. get the candidates based on cosine similarity
    3. get the candidates based on Jaccard distance
    4. prepare (start, end, span), gold standard
    '''

    dict_file = 'data/CTD_diseases.tsv'
    dev_file = 'data/NCBIdevelopset_corpus.txt'
    emb_path = 'data/wvec_50_haodi-li-et-al.bin'
    n_cossim = sys.argv[1]
    n_jaccard = sys.argv[2]
    save_to = 'data/selected_max200.pickle'

    # (1)
    # concepts
    [potato0,potato1,concept_order,potato2,potato3,potato4] = load_data('data/sampled_dev_set.pickle')
    del potato0, potato1, potato2, potato3, potato4
    concept, dictionary = load_concepts(dict_file,concept_order)

    # mentions
    corpus_dev = load_mentions(dev_file,'dev corpus')

    # (2)
    cossim_candidate_indices = cosine_similarity_candidates(corpus_dev.names,concept.names,emb_path,n_cossim)

    # (3)
    jaccard_candidate_indices = jaccard_distance_candidates(corpus_dev.names,concept.names,n_jaccard)

    # (4)
    assert len(cossim_candidate_indices)==len(jaccard_candidate_indices)
    candidates = []
    for cossim,jaccard in zip(cossim_candidate_indices,jaccard_candidate_indices):
        mention_candidates = sorted(list(set(cossim+jaccard)))
        candidates.append(mention_candidates)

    positives_training, positives_dev, positives_dev_truncated = load_data('data/gitig_positive_indices.pickle')
    del positives_training, positives_dev_truncated
    positives_dev = sample.prepare_positives(positives_dev,nltk.word_tokenize,vocabulary)

    can_val_data = sample.NewDataSet('dev corpus')
    can_val_data.y = []
    can_val_data.mentions = []
    start = 0
    for cans, poss, span in zip(candidates,positives_dev,corpus_dev.names):
        end = start + len(cans)
        (chosen_idx, idces), e_token_indices = poss
        can_val_data.y.extend([1 if can in idces else 0 for can in cans])
        can_val_data.mentions.append((start,end,span))
        start = end

    assert len(can_val_data.mentions)==len(candidates)

    data = [candidates, can_val_data.mentions, can_val_data.y]
    dump_data(save_to,data)


#!/usr/bin/python3

def safe_print_list(my_list=[], x=0):
    ctr = 0

    for index in range(0, x):
        try:
            print(f"{my_list[index]}", end="")
            ctr += 1
        except IndexError:
            break
    print()
    return (ctr)


import wttest
from wtscenario import make_scenarios
from wiredtiger import WT_NOTFOUND

# test_prepare_hs05.py
# Test that after aborting prepare transaction, correct update from the history store is restored.
class test_prepare_hs05(wttest.WiredTigerTestCase):
    conn_config = 'cache_size=50MB'

    format_values = [
        ('column', dict(key_format='r', key=1, value_format='S')),
        ('column-fix', dict(key_format='r', key=1, value_format='8t')),
        ('string-row', dict(key_format='S', key=str(1), value_format='S')),
    ]

    scenarios = make_scenarios(format_values)

    def test_check_prepare_abort_hs_restore(self):
        uri = 'table:test_prepare_hs05'
        create_params = 'key_format={},value_format={}'.format(self.key_format, self.value_format)
        self.session.create(uri, create_params)

        if self.value_format == '8t':
            value1 = 97
            value2 = 98
            value3 = 99
        else:
            value1 = 'a' * 5
            value2 = 'b' * 5
            value3 = 'c' * 5

        self.conn.set_timestamp('oldest_timestamp=' + self.timestamp_str(1))
        cursor = self.session.open_cursor(uri)

        key = self.key

        self.session.begin_transaction()
        cursor[key] = value1
        cursor.set_key(key)
        self.session.commit_transaction('commit_timestamp=' + self.timestamp_str(2))

        # Commit update and remove operation in the same transaction.
        self.session.begin_transaction()
        cursor[key] = value2
        cursor.set_key(key)
        cursor.remove()
        self.session.commit_transaction('commit_timestamp=' + self.timestamp_str(3))

        # Add a prepared update for the key.
        self.session.begin_transaction()
        cursor[key] = value3
        self.session.prepare_transaction('prepare_timestamp='+ self.timestamp_str(4))

        # Try to evict the page with prepared update. This will ensure that prepared update is
        # written as the on-disk version and the older versions are moved to the history store.
        session2 = self.conn.open_session()
        session2.begin_transaction('ignore_prepare=true')
        cursor2 = session2.open_cursor(uri, None, "debug=(release_evict=true)")
        cursor2.set_key(key)
        if self.value_format == '8t':
            # In FLCS, deleted values read back as 0.
            self.assertEquals(cursor2.search(), 0)
            self.assertEquals(cursor2.get_value(), 0)
        else:
            self.assertEquals(cursor2.search(), WT_NOTFOUND)
        cursor2.reset()

        # This should abort the prepared transaction.
        self.session.rollback_transaction()

        self.session.checkpoint()

        # We should be able to read the older version of the key from the history store.
        self.session.begin_transaction('read_timestamp='+self.timestamp_str(2))
        cursor.set_key(key)
        self.assertEqual(cursor.search(), 0)
        self.assertEqual(cursor.get_value(), value1)
        self.session.rollback_transaction()

        # The latest version should be marked deleted.
        self.session.begin_transaction()
        cursor.set_key(key)
        if self.value_format == '8t':
            # In FLCS, deleted values read back as 0.
            self.assertEquals(cursor.search(), 0)
            self.assertEquals(cursor.get_value(), 0)
        else:
            self.assertEqual(cursor.search(), WT_NOTFOUND)
        self.session.rollback_transaction()