from collections import defaultdict
from itertools import chain

from ieml.commons import OrderedEnum, monitor_decorator
from ieml.dictionary.script import Script
from ieml.usl.constants import FLEXION_SCRIPTS
from ieml.usl.syntagmatic_function import SyntagmaticFunction, SyntagmaticRole
from ieml.usl.usl import USL
from ieml.usl.polymorpheme import PolyMorpheme
from ieml.usl.lexeme import Lexeme
from ieml.usl.variation import PolyMorphemeVariation
from ieml.usl.word import Word

class DeferenceError(KeyError):
	pass

SEPARATOR = '>'

def path(string) -> 'UslPath':
	from ieml.usl.decoration.parser.parser import PathParser

	return PathParser().parse(string)

class UslPath:
	USL_TYPE = USL

	def __init__(self, child=None):
		if child is not None:
			if not isinstance(child, UslPath):
				raise ValueError("Invalid path child, expected a UslPath, got a "+ child.__class__.__type__)

		self.child = child

	def _deference(self, usl):
		return usl

	def has_prefix(self, prefix: 'UslPath'):
		if prefix is None:
			return True

		if self.__class__ != prefix.__class__ or (self.__class__ == prefix.__class__ and not self._do_eq(prefix)):
			return self._has_prefix(prefix)

		if self.child is None:
			return prefix.child is None

		return self.child.has_prefix(prefix.child)

	def _has_prefix(self, other):
		return False

	def as_constant(self, u=None):
		return UslPath(child=None if self.child is None else self.child.as_constant(u))

	def without_morpheme(self):
		return UslPath(child=None if self.child is None else self.child.without_morpheme())

	def concat(self, suffix: 'UslPath', force: bool=False) -> 'UslPath':
		if self.__class__ == UslPath:
			return suffix

		if suffix.__class__ == UslPath:
			return self

		if not isinstance(suffix, UslPath):
			raise ValueError("Invalid suffix to concat, got "+ suffix.__class__.__name__)

		if self.__class__ != suffix.__class__:
			if self.child is None:
				return self.clone(child=suffix, use_child=True)
			else:
				return self.clone(child=self.child.concat(suffix, force=force), use_child=True)
		else:
			if not suffix.has_prefix(self) and not force:
				return self.clone()

			return suffix

	def remove_prefix(self, prefix: 'UslPath'):
		if not self.has_prefix(prefix):
			return None

		if prefix is None:
			return self

		if not isinstance(self, prefix.__class__) or not self._do_eq(prefix):
			return None

		if self.child is None:
			return None

		return self.child.remove_prefix(prefix.child)

	@property
	def tail(self):
		last = self
		while last.child is not None:
			last = last.child
		return last

	def deference(self, usl: USL) -> USL:
		from ieml.usl.decoration.instance import InstancedUSL

		if isinstance(usl, InstancedUSL):
			usl = usl.usl

		if not isinstance(usl, self.USL_TYPE):
			raise DeferenceError("Invalid Usl type for a " + self.__class__.__name__ + \
											", expected a " + self.USL_TYPE.__name__ + \
											", got a " + usl.__class__.__name__)

		node = self._deference(usl)

		if self.child is not None:
			return self.child.deference(node)
		else:
			return node

	def contained(self, usl):
		try:
			self.deference(usl)
			return True
		except DeferenceError:
			return False

	def _to_str(self):
		return ''

	def __str__(self):
		return SEPARATOR + self._to_str() + (str(self.child) if self.child is not None else '')

	@staticmethod
	def _from_string(string, children):
		return UslPath()

	@classmethod
	def from_string(cls, string):
		if string == SEPARATOR:
			return UslPath._from_string(string, None)

		split = string.split(SEPARATOR)
		return cls._from_string(split[1], split[2:])

	def __eq__(self, other):
		return isinstance(other, UslPath) and str(other) == str(self)

	def __lt__(self, other):
		if other.__class__ != self.__class__:
			return self.USL_TYPE.syntactic_level < other.USL_TYPE.syntactic_level
		else:
			return self._do_lt(other)

	def _do_eq(self, other):
		return True

	def _do_lt(self, other):
		return False

	def __hash__(self):
		return hash(str(self))

	def clone(self, use_child=False, child=None):
		return UslPath(child=(child if use_child else self.child))

	def no_child_clone(self):
		return self.clone(use_child=True, child=None)

	@classmethod
	def build_usl_from_path_to_node(cls, path_to_node):
		raise NotImplementedError()

	@property
	def is_constant_path(self):
		if self.child is not None:
			return self._is_constant_path and self.child.is_constant_path
		else:
			return self._is_constant_path

	@property
	def _is_constant_path(self):
		return False

	def split_tail(self):
		if self.child is None:
			return (UslPath(), self)
		elif self.child.child is None:
			return (self.no_child_clone(), self.child)
		else:
			p, tail = self.child.split_tail()
			return (self.clone(use_child=True, child=p), tail)

class GroupIndex(OrderedEnum):
	CONSTANT = -1
	GROUP_0 = 0
	GROUP_1 = 1
	GROUP_2 = 2

class PolymorphemePath(UslPath):
	USL_TYPE = PolyMorpheme

	def __init__(self, group_idx: GroupIndex, morpheme: Script=None, multiplicity=None, child=None):
		super().__init__(child=child)
		self.group_idx = group_idx
		self.morpheme = morpheme
		self.multiplicity = multiplicity

	@property
	def _is_constant_path(self):
		return self.group_idx == GroupIndex.CONSTANT

	def _do_eq(self, other):
		return self.group_idx == other.group_idx and self.morpheme == other.morpheme and \
			   self.multiplicity == other.multiplicity

	def _has_prefix(self, other):
		return self.group_idx == other.group_idx and self.multiplicity == other.multiplicity

	def as_constant(self, u=None):
		return PolymorphemePath(group_idx=GroupIndex.CONSTANT,
								morpheme=self.morpheme if self.morpheme is not None else u,
								multiplicity=None,
								child=(None if self.child is None else self.child.as_constant(u)))

	def _do_lt(self, other):
		return (self.group_idx, self.morpheme) < (other.group_idx, other.morpheme)

	def without_morpheme(self):
		return PolymorphemePath(group_idx=self.group_idx,
								morpheme=None,
								multiplicity=self.multiplicity,
								child=(None if self.child is None else self.child.without_morpheme()))

	def _deference(self: 'PolymorphemePath', usl: PolyMorpheme):
		if self.group_idx != GroupIndex.CONSTANT and self.group_idx.value >= len(usl.groups):
			raise DeferenceError("Group index " + str(self.group_idx.name) + " not in polymorpheme")

		if self.morpheme is not None:
			if self.group_idx == GroupIndex.CONSTANT:
				group = usl.constant
			else:
				group = usl.groups[self.group_idx.value][0]

			if not self.morpheme.empty and self.morpheme not in group:
				raise DeferenceError("Morpheme " + str(self.morpheme) + " not in group at " + str(self.group_idx.name))

			return self.morpheme
		else:
			if self.group_idx == GroupIndex.CONSTANT:
				return PolyMorpheme(constant=usl.constant)
			else:
				return PolyMorpheme(groups=[usl.groups[self.group_idx.value]])

	def _to_str(self):
		if self.group_idx == GroupIndex.CONSTANT:
			if self.morpheme is not None:
				return 'constant' + SEPARATOR + str(self.morpheme)
			else:
				return 'constant'
		else:
			if self.morpheme is not None:
				return 'group_{}'.format(self.group_idx.value) + \
					(' {}'.format(self.multiplicity) if self.multiplicity is not None else '') + \
					SEPARATOR + str(self.morpheme)
			else:
				return 'group_{}'.format(self.group_idx.value) + \
					(' {}'.format(self.multiplicity) if self.multiplicity is not None else '')

	@staticmethod
	def _from_string(elem, children):
		if elem == '':
			return UslPath()

		key = elem
		morph = None
		if len(children) == 1:
			from ieml.usl.usl import usl
			morph = usl(children[0])

		idx = None
		multiplicity = None

		if key.startswith('constant'):
			idx = GroupIndex.CONSTANT
		elif key.startswith('group_'):
			if ' ' in key:
				key_, multi = key.split(' ')
				multiplicity = int(multi)
			else:
				key_ = key

			n = int(''.join(key_[6:]))

			if n == 0:
				idx = GroupIndex.GROUP_0
			elif n == 1:
				idx = GroupIndex.GROUP_1
			elif n == 2:
				idx = GroupIndex.GROUP_2
			else:
				raise ValueError("Invalid argument index for a PolymorphemePath _from_string constructor: " + str(n))
		else:
			raise ValueError("Invalid argument for a PolymorphemePath _from_string constructor: " + key)

		return PolymorphemePath(group_idx=idx, morpheme=morph, multiplicity=multiplicity)

	def clone(self, use_child=False, child=None):
		return PolymorphemePath(group_idx=self.group_idx, morpheme=self.morpheme,
								multiplicity=self.multiplicity, child=(child if use_child else self.child))

	@classmethod
	def build_usl_from_path_to_node(cls, path_to_node):
		"""
		path_to_node: dict PolymorphemePath -> Script
		# TODO handle multiplicity
		:param path_to_node:
		:return:
		"""

		expend_values = lambda e: [e] if isinstance(e, Script) else e.constant

		constant = []
		groups = [[[], None],[[], None],[[], None]]

		for k, values in path_to_node.items():
			if not isinstance(k, PolymorphemePath):
				raise ValueError("invalid path type to instantiate a polymorphem " + k.__class__.__name__)

			if k.group_idx == GroupIndex.CONSTANT:
				constant.extend(chain.from_iterable(map(expend_values, values)))
			else:
				if k.group_idx == GroupIndex.GROUP_0:
					idx = 0
				elif k.group_idx == GroupIndex.GROUP_1:
					idx = 1
				elif k.group_idx == GroupIndex.GROUP_2:
					idx = 2
				else:
					raise ValueError("Invalid polymorpheme path " + str(k))

				groups[idx][0].extend(chain.from_iterable(map(expend_values, values)))
				if k.multiplicity:
					if groups[idx][1] is not None and k.multiplicity != groups[idx][1]:
						raise ValueError("Incoherent multiplicity across the path")

					groups[idx][1] = k.multiplicity
				else:
					groups[idx][1] = 1

		groups = [g for g in groups if g[0] != []]
		return PolyMorpheme(constant=constant, groups=groups)

class FlexionPath(UslPath):
	USL_TYPE = PolyMorpheme

	def __init__(self, morpheme, child=None):
		super().__init__(child=child)
		from ieml.dictionary.script import Script

		assert isinstance(morpheme, Script), morpheme.__class__.__name__
		self.morpheme = morpheme

	@property
	def _is_constant_path(self):
		return True

	def as_constant(self, u=None):
		return FlexionPath(morpheme=self.morpheme,
						   child=(None if self.child is None else self.child.as_constant(u)))

	def without_morpheme(self):
		raise ValueError("Unable to create a flexion path without a morpheme")
		# return FlexionPath(morpheme=None,
		# 				   child=(None if self.child is None else self.child.without_morpheme()))

	def _do_eq(self, other):
		return self.morpheme == other.morpheme

	def _do_lt(self, other):
		return self.morpheme < other.morpheme

	def _deference(self: 'FlexionPath', usl: PolyMorpheme):
		all_group = [usl.constant, *(list(filter(lambda m: not m.empty, g)) for g, _ in usl.groups)]
		all_morphemes = {str(w): w for g in all_group for w in g}

		if self.morpheme not in all_morphemes:
			raise DeferenceError("Morpheme " + str(self.morpheme) + " not in flexion")

		return self.morpheme

	def _to_str(self):
		return str(self.morpheme)

	@staticmethod
	def _from_string(elem, children):
		if elem == '':
			return UslPath()

		from ieml.usl.usl import usl

		morph = usl(elem)
		assert len(children) == 0
		return FlexionPath(morpheme=morph)

	def clone(self, use_child=False, child=None):
		return FlexionPath(morpheme=self.morpheme, child=(child if use_child else self.child))

	@classmethod
	def build_usl_from_path_to_node(cls, path_to_node):
		all_morphemes = []
		for k, values in path_to_node.items():
			if not isinstance(k, FlexionPath):
				raise ValueError("invalid path type to instantiate a polymorpheme " + k.__class__.__name__)

			all_morphemes.extend(values)

		root_flexion_groups = defaultdict(set)

		for m in all_morphemes:
			for r in FLEXION_SCRIPTS:
				if m in r.singular_sequences_set:
					root_flexion_groups[r].add(m)

		constant = []
		groups = []

		for k, v in root_flexion_groups.items():
			if len(v) == 0:
				continue
			elif len(v) == 1:
				constant.append(next(iter(v)))
			else:
				groups.append([list(v), 1])

		return PolyMorpheme(constant=constant, groups=groups)

class LexemeIndex(OrderedEnum):
	CONTENT = 0
	FLEXION = 1

class LexemePath(UslPath):
	USL_TYPE = Lexeme

	def __init__(self, index: LexemeIndex, child=None):
		super().__init__(child=child)
		assert isinstance(index, LexemeIndex)
		self.index = index

		if child is not None and not child.__class__ == UslPath:
			if self.index == LexemeIndex.CONTENT:
				assert isinstance(self.child, PolymorphemePath), \
					"Invalid path structure, a lexeme content child must be a PolymorphemePath, not a " + self.child.__class__.__name__
			else:
				assert isinstance(self.child, FlexionPath), \
					"Invalid path structure, a lexeme flexion child must be a FlexionPath, not a " + self.child.__class__.__name__

	def as_constant(self, u=None):
		return LexemePath(index=self.index,
						  child=(None if self.child is None else self.child.as_constant(u)))

	def without_morpheme(self):
		return LexemePath(index=self.index,
						  child=(None if self.child is None or self.index == LexemeIndex.FLEXION else self.child.without_morpheme()))

	@property
	def _is_constant_path(self):
		return True

	def _do_eq(self, other):
		return self.index == other.index

	def _do_lt(self, other):
		return self.index < other.index

	def _deference(self: 'LexemePath', usl: Lexeme):
		if self.index == LexemeIndex.CONTENT:
			return usl.pm_content
		else:
			return usl.pm_flexion

	def _to_str(self):
		if self.index == LexemeIndex.CONTENT:
			return 'content'
		else:
			return 'flexion'

	@staticmethod
	def _from_string(elem, children):
		if elem == '':
			return UslPath()

		idx = None
		if elem == 'content':
			idx = LexemeIndex.CONTENT
		elif elem == 'flexion':
			idx = LexemeIndex.FLEXION
		else:
			raise ValueError("Invalid argument for a LexemePath _from_string constructor: " + elem)

		child = None
		if len(children) != 0:
			if idx == LexemeIndex.CONTENT:
				child = PolymorphemePath._from_string(children[0], children[1:])
			else:
				child = FlexionPath._from_string(children[0], children[1:])

		return LexemePath(index=idx, child=child)

	def clone(self, use_child=False, child=None):
		if child is not None:
			if self.index == LexemeIndex.CONTENT:
				if not isinstance(child, PolymorphemePath):
					raise ValueError("Can't cast to polymorpheme path")
			else:
				if isinstance(child, PolymorphemePath):
					if child.morpheme is not None:
						child = FlexionPath(morpheme=child.morpheme)
					else:
						child = None
				elif not isinstance(child, FlexionPath):
					raise ValueError("Can't cast to flexion path")

		return LexemePath(index=self.index, child=(child if use_child else self.child))

	@classmethod
	def build_usl_from_path_to_node(cls, path_to_node):
		pm_content = None
		pm_flexion = None

		for k, v in path_to_node.items():
			if not isinstance(k, LexemePath):
				raise ValueError("invalid path type to instantiate a Lexeme " + k.__class__.__name__)

			if k.index == LexemeIndex.CONTENT:
				if pm_content is not None:
					raise ValueError("Multiple candidates for the content of the Lexeme")

				if not isinstance(v, PolyMorpheme):
					pm_content = PolyMorpheme(constant=v)
				else:
					pm_content = v

			elif k.index == LexemeIndex.FLEXION:
				if pm_flexion is not None:
					raise ValueError("Multiple candidates for the flexion of the Lexeme")

				if not isinstance(v, PolyMorpheme):
					pm_flexion = FlexionPath.build_usl_from_path_to_node({FlexionPath(morpheme=ss): [ss] for ss in v})
				else:
					pm_flexion = v

		if pm_content is None:
			pm_content = PolyMorpheme([])

		if pm_flexion is None:
			pm_flexion = PolyMorpheme([])

		return Lexeme(pm_content=pm_content, pm_flexion=pm_flexion)

class RolePath(UslPath):
	USL_TYPE = Word

	def __init__(self, role, has_focus=False, child=None):
		super().__init__(child)
		from ieml.usl.syntagmatic_function import SyntagmaticRole

		if not isinstance(role, SyntagmaticRole):
			raise DeferenceError("Invalid role for a RolePath " + role.__class__.__name__)

		self.role = role

		if child is not None and not child.__class__ == UslPath:
			if not isinstance(child, LexemePath):
				raise DeferenceError("Invalid path structure, a lexeme content child must be a PolymorphemePath, not a " + self.child.__class__.__name__)

		self.has_focus = has_focus

	def as_constant(self, u=None):
		return RolePath(role=self.role,
						has_focus=self.has_focus,
						child=(None if self.child is None else self.child.as_constant(u)))
	def without_morpheme(self):
		return RolePath(role=self.role,
						has_focus=self.has_focus,
						child=(None if self.child is None else self.child.without_morpheme()))

	@property
	def _is_constant_path(self):
		return True

	def _do_eq(self, other):
		return self.role == other.role and self.has_focus == other.has_focus

	def _do_lt(self, other):
		return self.role < other.role

	def _deference(self: 'RolePath', usl: Word):
		try:
			return usl.syntagmatic_fun.get(self.role, ignore_prefix=True, ignore_process_valence=True)
		except KeyError as key:
			raise DeferenceError(key)

	def _to_str(self):
		return 'role' + SEPARATOR + ('! ' if self.has_focus else '') + str(self.role)

	# def __hash__(self):
	# 	"""Ignore focus when used as key"""
	# 	return hash( 'role' + SEPARATOR + str(self.role))

	@staticmethod
	def _from_string(elem, children):
		from ieml.usl.syntagmatic_function import SyntagmaticRole

		if elem == '':
			if len(children) == 0:
				return UslPath()

			raise ValueError("Empty role in RolePath")

		from ieml.usl.usl import usl

		sfun_role = SyntagmaticRole([usl(s) for s in elem.split(' ') if s != '!'])

		child = None
		if len(children) != 0:
			child = LexemePath._from_string(children[0], children[1:])

		return RolePath(role=sfun_role, has_focus='!' in elem,child=child)

	def clone(self, use_child=False, child=None):
		return RolePath(role=self.role,
						has_focus=self.has_focus, # if use_child and child is not None else False,
						child=(child if use_child else self.child))

	@classmethod
	def build_usl_from_path_to_node(cls, path_to_node):
		focus_role = None

		for k, v in path_to_node.items():
			if not isinstance(k, RolePath):
				raise ValueError("invalid path type to instantiate a Word " + k.__class__.__name__)

			if k.has_focus:
				if focus_role is not None and focus_role != k.role:
					raise ValueError("Incoherent focus role, multiple differents values")
				focus_role = k

		if focus_role is None:
			raise ValueError("No focus role defined in this word")

		path_to_node_grouped = defaultdict(list)
		for k, v in path_to_node.items():
			if v is not None:
				if focus_role.role == k.role and not k.has_focus:
					path_to_node_grouped[focus_role].append(v)
				else:
					path_to_node_grouped[k].append(v)

		path_to_node_res = {}
		for k, v in path_to_node_grouped.items():
			if len(v) != 1:
				raise ValueError('Two many lexemes at role: ' + str(k))
			path_to_node_res[k] = v[0]

		lex_list = [(k.role.constant, v) for k, v in path_to_node_res.items()]

		ctx_type, sfun = SyntagmaticFunction.from_list(lex_list)

		return Word(syntagmatic_fun=sfun,
					role=focus_role.role,
					context_type=ctx_type)

# @monitor_decorator('usl_from_path_values')
def usl_from_path_values(paths_values):
	from ieml.usl.decoration.parser.parser import PathParser
	from ieml.usl.parser import IEMLParser

	path_parser = PathParser()
	usl_parser = IEMLParser()

	path_to_value = {path_parser.parse(p): set() for p, _ in paths_values}
	for p, v in paths_values:
		path_to_value[path_parser.parse(p)].add(usl_parser.parse(v))

	Tree = lambda: defaultdict(Tree)
	bins = Tree()

	def recursive_group_by(bin, path, values):
		p_cloned = path.no_child_clone()

		if 'type' in bin:
			if not isinstance(path, bin['type']):
				raise ValueError("Inconsistent path system")
		else:
			bin['type'] = path.__class__

		if path.child is None:
			bin[p_cloned]["node"] = values
		else:
			recursive_group_by(bin[p_cloned], path.child, values)

	def build_nodes(bin):
		if 'node' not in bin:
			path_to_node = {}
			for p, bin_child in bin.items():
				if isinstance(p, UslPath):
					path_to_node[p] = build_nodes(bin_child)

			assert 'type' in bin
			bin['node'] = bin['type'].build_usl_from_path_to_node(path_to_node)

		return bin['node']

	for p, values in path_to_value.items():
		recursive_group_by(bins, p, list(values))

	return build_nodes(bins)


import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

n = int(sys.stdin.readline())
x = []
y = []
color = []

cdict = { '0' : 'y',
          '1' : 'g',
          '2' : 'b',
          '3' : 'r',
          '4' : 'c'
          }

for line in range(0,n):
    line = sys.stdin.readline()
    data = line.split()
    x.append(float(data[0]))
    y.append(float(data[1]))
    color.append(cdict[data[2]])

plt.scatter(x,y,c=color)
plt.show()


import requests

# Define the URL
url = "http://127.0.0.1:8000/scrape_and_store"

# Define the payload (data to send)
data = {"url": "https://en.wikipedia.org/wiki/Emmerson_Nogueira"}

# Make the POST request
response = requests.post(url, json=data)

# Print the response
print(response.json())

# curl -X POST -H "Content-Type: application/json" -d '{"url":"https://en.wikipedia.org/wiki/Emmerson_Nogueira"}' http://127.0.0.1:8000/scrape_and_store


"""Tests for the GQCISD model.
"""

import unittest
import pytest

import numpy as np
from pyscf import cc, gto, lib, scf
from pyscf.cc import qcisd as pyscf_qcisd
from pyscf.cc import ccsd_rdm

from ebcc import GEBCC, NullLogger, Space, util

@pytest.mark.reference
class GQCISD_PySCF_Tests(unittest.TestCase):
    """Test GQCISD against PySCF's RQCISD values.
    """

    @classmethod
    def setUpClass(cls):
        mol = gto.Mole()
        mol.atom = "Li 0 0 0; H 0 0 1.4"
        mol.basis = "cc-pvdz"
        mol.verbose = 0
        mol.build()

        mf = scf.RHF(mol)
        mf.conv_tol = 1e-12
        mf.kernel()

        qcisd_ref = pyscf_qcisd.QCISD(mf)
        qcisd_ref.conv_tol = 1e-10
        qcisd_ref.conv_tol_normt = 1e-14
        qcisd_ref.max_cycle = 200
        qcisd_ref.kernel()

        qcisd = GEBCC(
                mf.to_ghf(),
                ansatz="QCISD",
                log=NullLogger(),
        )
        qcisd.options.e_tol = 1e-10
        eris = qcisd.get_eris()
        qcisd.kernel(eris=eris)

        cls.mf, cls.qcisd_ref, cls.qcisd, cls.eris = mf, qcisd_ref, qcisd, eris

    @classmethod
    def tearDownClass(cls):
        del cls.mf, cls.qcisd_ref, cls.qcisd

    def test_converged(self):
        self.assertTrue(self.qcisd.converged)
        self.assertTrue(self.qcisd_ref.converged)

    def test_energy(self):
        a = self.qcisd_ref.e_tot
        b = self.qcisd.e_tot
        self.assertAlmostEqual(a, b, 7)

@pytest.mark.regression
class GQCISD_Tests(unittest.TestCase):
    """Test GQCISD against regression.
    """

    @classmethod
    def setUpClass(cls):
        mol = gto.Mole()
        mol.atom = "Li 0 0 0; H 0 0 1.4"
        mol.basis = "cc-pvdz"
        mol.verbose = 0
        mol.build()

        mf = scf.RHF(mol)
        mf.conv_tol = 1e-12
        mf.kernel()

        qcisd = GEBCC(
                mf.to_ghf(),
                ansatz="QCISD",
                log=NullLogger(),
        )
        qcisd.options.e_tol = 1e-10
        eris = qcisd.get_eris()
        qcisd.kernel(eris=eris)

        cls.mf, cls.qcisd, cls.eris = mf, qcisd, eris

    @classmethod
    def tearDownClass(cls):
        del cls.mf, cls.qcisd, cls.eris

    def test_converged(self):
        self.assertTrue(self.qcisd.converged)

    def test_t1(self):
        ci = self.qcisd.mo_coeff[:, self.qcisd.mo_occ > 0]
        ca = self.qcisd.mo_coeff[:, self.qcisd.mo_occ == 0]
        t1 = util.einsum("ia,pi,qa->pq", self.qcisd.t1, ci, ca)
        self.assertAlmostEqual(lib.fp(t1), 0.002669284765052053)

    def test_t2(self):
        ci = self.qcisd.mo_coeff[:, self.qcisd.mo_occ > 0]
        ca = self.qcisd.mo_coeff[:, self.qcisd.mo_occ == 0]
        t2 = util.einsum("ijab,pi,qj,ra,sb->pqrs", self.qcisd.t2, ci, ci, ca, ca)
        self.assertAlmostEqual(lib.fp(t2), -0.0008836462499253184)

if __name__ == "__main__":
    print("Tests for GQCISD")
    unittest.main()


import sys
r = sys.stdin.readline
n,m = map(int, r().split())
graph = [[False for _ in range(n+1)] for _ in range(n+1)]

def solution():
    answer = 0

    for k in range(1,n+1):
        for i in range(1,n+1):
            for j in range(1,n+1):
                if graph[i][k] and graph[k][j]:
                    graph[i][j] = True

    for i in range(1,n+1):
        cnt = 0
        for j in range(1,n+1):
            if graph[i][j] or graph[j][i]:
                cnt += 1
        if cnt == n - 1:
            answer+=1

    return answer

for _ in range(m):
    a ,b = map(int, r().split())
    graph[a][b] = True

print(solution())


import codecs
import os
import re
from setuptools import setup
from setuptools import find_packages

def find_version(*file_paths):
    here = os.path.abspath(os.path.dirname(__file__))
    with codecs.open(os.path.join(here, *file_paths), 'r') as f:
        version_file = f.read()
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError("Unable to find version string.")

setup(
    name="jans-pycloudlib",
    version=find_version("jans", "pycloudlib", "version.py"),
    url="",
    license="Apache",
    author="Janssen",
    author_email="isman@gluu.org",
    description="",
    long_description=__doc__,
    packages=find_packages(),
    zip_safe=False,
    install_requires=[
        "requests>=2.22.0",
        "python-consul>=1.0.1",
        "hvac>=0.7.0",
        "kubernetes>=11.0",
        "ldap3>=2.5",
        "backoff>=2.1.2",
        "docker>=3.7.2",
        "requests-toolbelt>=0.9.1",
        "cryptography>=2.8",
        "google-cloud-secret-manager>=2.2.0",
        "pymysql>=1.0.2",
        "sqlalchemy>=1.3,<1.4",
        "psycopg2>=2.8.6",
        "google-cloud-spanner>=3.3.0",
        "Click>=6.7",
        "ldif>=4.1.1",
        # handle CVE-2022-36087
        "oauthlib>=3.2.1",
        "boto3",
    ],
    classifiers=[
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
    ],
    include_package_data=True,
    entry_points={
        "console_scripts": ["jans-pycloudlib=jans.pycloudlib.cli:cli"],
    },
    package_data={
        "jans.pycloudlib": ["py.typed"],
    },
    python_requires=">=3.9",
)


# coding: utf-8

# In[1]:

import numpy as np

# ### ceil
# 端数の切り上げ

# In[4]:

a = np.arange(0,2,0.2)
print(a)

# In[5]:

b = np.ceil(a)
print(b)


from argparse import ArgumentParser
import matplotlib
from matplotlib import pyplot as plt
matplotlib.rcParams['pdf.fonttype'] = 42 # Editable text when exporting pdfs
import numpy as np
import pandas as pd
import scipy as sp
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import confusion_matrix

#%% Normalize count matrix using arcsinh transformation. Assumes each row is a sample/cell and columns correspond to genes
def normalize_arcsinh(count_matrix,cofactor = 1000):
    total_counts = np.squeeze(np.sum(count_matrix,axis = 1))
    normalized_counts = np.arcsinh(count_matrix / total_counts[:,np.newaxis] * cofactor)
    return(normalized_counts)

#%% Train a classifer using training_object generated by create_training_data
def train_LR( training_data, training_labels, num_genes = 1024, pct_var = 0.90, save_results = True ):
    """
    Description
    -----------
    Trains logistic regression classification model.
        1) Select subset of features based on standard deviation
        2) Run PCA using only selected features
        3) Use PCA components as inputs into logistic regression classifier
    Calls sklearn logistic regression, which has cross-validation built-in

    Parameters
    ----------
    training_data : numpy array
        contains count data
    training_labels : numpy array
        cell type labels corresponding to each row of training data
    num_genes : int
        number of genes to select
    pct_var : int
        percentage of variance to explain by PCA. Determines number of components
        used as inputs to logistic regression
    save_results : boolean
        specify whether to save cross validation results
    See individual arguments

    Returns
    -------
    a dictionary containing handles to functions for each step in the classification
    procedure (i.e. normalization, feature selection, pca transformation, and prediction)
    """

    normalized_expression = normalize_arcsinh(training_data)
    # Select subset of genes to use based on standard deviation
    std_dev = np.std( normalized_expression, axis = 0)
    sorted_idx = np.argsort(std_dev)
    # Train on PCA componenets rather than gene expression
    pca = PCA(n_components = pct_var)
    training_pca_coordinates = pca.fit_transform( normalized_expression[:,sorted_idx[-num_genes:]] )
    # Fit logisitc regression model and cross validate
    LR = LogisticRegressionCV( penalty = "l2", Cs = np.logspace(-7,1,9), dual = False, class_weight = "balanced", multi_class = "ovr", solver = "sag")
    LR.fit( training_pca_coordinates, training_labels )

    # Save cross validation results
    if save_results:
        directory = "results/classification/training_results/"
        parameters = str(num_genes) + "_" + str(int(pct_var*100))
        for ct in np.unique( training_labels ):
            pd.DataFrame( LR.scores_[ct], columns=[f'{x:.20f}' for x in LR.Cs_]).to_csv(
                         directory + ct + "_" + parameters + ".csv")
        confusion_mat = pd.DataFrame(confusion_matrix( training_labels, LR.predict( training_pca_coordinates )),
                                     index = np.unique(training_labels), columns = np.unique(training_labels))
        confusion_mat.to_csv(directory + "confusion_matrix" + "_" + parameters + ".csv")

    def feature_selection(X):
        return(X[:,sorted_idx[-num_genes:]])
    # Pack functions for classification into dictionary
    return {'normalization': normalize_arcsinh,
            'feature_selection': feature_selection,
            'pca_function': pca.transform,
            'prediction_function': LR.predict,
            'prediction_probability': LR.predict_proba}
    #    return(LR)
#%%
def _argparse():
    """
    Description
    -----------
    Parse command line arguments
    Parameters
    ----------
    See individual arguments
    Returns
    -------
    """
    parser = ArgumentParser('Train classifier using given parameters')
    parser.add_argument("training_data", help = "sparse matrix (.npz containing training data)",
                        type=str,default="")
    parser.add_argument("training_labels", help = ".csv file containing training labels (cell types)",
                        type=str,default="")
    #parser.add_argument("gene_names", help = "file containing gene names",
    #                    type=str,default="")
    parser.add_argument("-k","--k_fold",
                        help = "Number of partitions to use for k-fold cross validation"
                        ,type=int,default=5)
    parser.add_argument("-n","--num_genes",
                        help = "Only use the n highest genes by standard deviation"
                        ,type=int ,default=1024)
    parser.add_argument("-pca","--pca_variance",
                        help = "% of PCA variance to keep when defining training inputs"
                        ,type=float  ,default=0.90)
    return parser

#%% Compute expression thresholds for each marker using otsu's method
#   Assumes two groups (high expressing and low expressing)
def main(args):
    """
    Description
    -----------
    Predict cell-types of each cell in the single cell expression data using
    pre-defined marker genes

    Parameters
    ----------
    see _argparse

    Returns
    -------
    No return value.
    train_LR function will save cross-validation results if specified.
    For each cell type in the training labels, train_LR will write accuracy
    scores over a range of regularization parameters. Will also save a confusion matrix
    """
    # Parse inputs
    parser = _argparse()
    argp = parser.parse_args(args[1:])
    # Read data
    sparse_training = sp.sparse.load_npz( argp.training_data )
    training_counts = np.array( sparse_training.todense() )
    training_labels = np.squeeze(np.array(pd.read_csv("results/classification/training_labels.csv",index_col = 0 )))

    train_LR( training_counts, training_labels, num_genes = argp.num_genes, pct_var = argp.pca_variance/100 )

if __name__ == '__main__':
    from sys import argv
    exit(main(argv))


#!/usr/bin/python3
'''This module defines a function to print a user name'''

def say_my_name(first_name, last_name=""):
    """
    Prints 'My name is <first name> <last name>'.

    Args:
        first_name (str): First name of the person.
        last_name (str, optional): Last name of the person. Defaults to "".

    Raises:
        TypeError: If first_name or last_name is not a string.

    Returns:
        None.
    """
    if not isinstance(first_name, str):
        raise TypeError("first_name must be a string")
    if not isinstance(last_name, str):
        raise TypeError("last_name must be a string")
    print(f"My name is {first_name} {last_name}")


import argparse
import json
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Mapping, Sequence, Any, Callable, MutableMapping

from external_services import ExternalServices

def action(fun):
    """Method decorator signaling to Deployster Python wrapper that this method is a resource action."""
    # TODO: validate function has single 'args' argument (using 'inspect.signature(fun)')
    fun.action = True
    return fun

class DAction:
    """
    Deployster action descriptor.

    Resources return a list of actions as part of their response from the 'state' action.
    """

    def __init__(self,
                 name: str = None,
                 description: str = None,
                 image: str = None,
                 entrypoint: str = None,
                 args: Sequence[str] = None) -> None:
        super().__init__()
        self._name: str = name
        self._description: str = description
        self._image: str = image
        self._entrypoint: str = entrypoint
        self._args: Sequence[str] = args if args is not None else [self._name.replace('-', '_')]

    @property
    def name(self) -> str:
        """The action name."""
        return self._name

    @property
    def description(self) -> str:
        """Short description of the action, possibly shown the user."""
        return self._description

    @property
    def image(self) -> str:
        """The Docker image to run to execute this action. Does not have to be the same image of the resource that
        provides the action, but it usually is."""
        return self._image

    @property
    def entrypoint(self) -> str:
        """The entrypoint used when executing the action Docker image. If not provided, the Docker image's default
        entrypoint will be executed."""
        return self._entrypoint

    @property
    def args(self) -> Sequence[str]:
        """Optional arguments to pass to the Docker image's entrypoint."""
        return self._args

    def to_dict(self) -> dict:
        """Converts the action into a dictionary that can be serialized (usually as JSON) back to Deployster."""
        data: dict = {}
        if self.name: data['name'] = self.name
        if self.description: data['description'] = self.description
        if self.image: data['image'] = self.image
        if self.entrypoint: data['entrypoint'] = self.entrypoint
        if self.args: data['args'] = self.args
        return data

class DPlug:
    """
    Describes a plug that a resource requests (or demands).

    Each resource can requested or demand a set of plugs that allow it to communicate back to Deployster, other
    resources, or even back to the user.

    Each plug is essentially a mounted volume that Deployster provides to the
    resource Docker image from the host machine. The resource can then read or write information to/from the volume. In
    turn, that plug is usually "plugged" into additional resources, enabling cross-resource communication.

    Common use-cases include providing GCP service account JSON files as plugs, shared directories for storing data
    that's used by multiple resources, etc.
    """

    def __init__(self, container_path: str, optional: bool, writable: bool) -> None:
        super().__init__()
        self._container_path: str = container_path
        self._optional: bool = optional
        self._writable: bool = writable

    @property
    def container_path(self) -> str:
        """The path to mount the plug in the resource Docker image when plugged into the resource."""
        return self._container_path

    @property
    def optional(self) -> bool:
        """Whether the resource can work without the plug, or whether the it's required for the resource to function."""
        return self._optional

    @property
    def writable(self) -> bool:
        """Whether the plug must be writable for the resource to function, or if it can be a readonly plug."""
        return self._writable

    def to_dict(self) -> dict:
        """Converts the plug descriptor to a dictionary for serialization back to Deployster (usually as JSON)."""
        return {'container_path': self.container_path, 'optional': self.optional, 'writable': self.writable}

class DResourceInfo:
    """Provides the raw resource information provided by Deployster. Each resource receives (on stdin) everything that
    Deployster knows about the resource, which includes:

    - the resource name
    - the resource type (its Docker image)
    - the resource configuration (everything under the 'config' property of the resource in the manifest)
    - dependency information: set of named dependencies that the user wired to the resource
    - optional actual state (properties) of the resource, as previously discovered for the resource in its state action
    """

    def __init__(self, data: dict) -> None:
        super().__init__()
        self._data = data

    @property
    def name(self) -> str:
        """The resource name, as depicted by the user in the deployment manifest."""
        return self._data['name']

    @property
    def type(self) -> str:
        """The resource type. This is the resource's Docker image."""
        return self._data['type']

    @property
    def deployster_version(self) -> str:
        return self._data['version']

    @property
    def verbose(self) -> bool:
        return self._data['verbose']

    @property
    def workspace(self) -> Path:
        return Path(self._data['workspace'])

    @property
    def has_config(self)->bool:
        return 'config' in self._data

    @property
    def config(self) -> Mapping[str, Any]:
        """The resource configuration, which in essence is the *desired* state of the resource, as depicted by the user
        in the deployment manifest."""
        return self._data['config']

    @property
    def stale_state(self) -> dict:
        return self._data['staleState']

class DResource(ABC):
    """
    Deployster resource base class.

    This class serves as a base class for Deployster resource implementations. It handles most of the heavy lifting of
    creating a resource by handling the resource lifecycle:

    - execution: parsing command line arguments, determining the correct "action", etc
    - initialization (the "init" action)
    - state calculation (the "state" action)
    - dependency handling
    """

    def __init__(self, data: dict, svc: ExternalServices) -> None:
        super().__init__()
        self._info = DResourceInfo(data)
        self._svc: ExternalServices = svc
        self._plugs: MutableMapping[str, DPlug] = {}
        self._config_schema = {
            "type": "object",
            "additionalProperties": True,
            "properties": {}
        }

    @property
    def svc(self) -> ExternalServices:
        return self._svc

    @property
    def info(self) -> DResourceInfo:
        return self._info

    def get_plug(self, name: str) -> DPlug:
        return self._plugs[name]

    def add_plug(self, name: str, container_path: str, optional: bool, writable: bool):
        """Signals that this resource requests or demands this plug."""
        self._plugs[name] = DPlug(container_path=container_path, optional=optional, writable=writable)

    @property
    def config_schema(self) -> dict:
        """Provides the JSON schema to be used to validate resource configuration in the manifest.

        The default schema only validates that the configuration is an object. You can modify the returned dict."""
        return self._config_schema

    @abstractmethod
    def discover_state(self):
        """Discovers the resource's actual properties, as they currently deployed.

        This method must be implemented by subclasses, and either return a dictionary describing the resource, or, if
        the resource is not found, return None."""
        raise NotImplementedError(f"internal error: 'discover_state' not implemented")  # pragma: no cover

    @abstractmethod
    def get_actions_for_missing_state(self) -> Sequence[DAction]:
        """Provides the list of actions to invoke when the resource is MISSING.

        Must be implemented by subclasses. It will be called when the "discover_actual_properties" method returns
        None."""
        raise NotImplementedError(
            f"internal error: 'get_actions_for_missing_state' not implemented")  # pragma: no cover

    @abstractmethod
    def get_actions_for_discovered_state(self, state: dict) -> Sequence[DAction]:
        """Provides the list of actions to invoke when the resource *was* found.

        Must be implemented by subclasses. It will be called when the "discover_actual_properties" method DOES NOT
        return None.

        This method can return an empty list, signaling in essence that the resource is VALID."""
        raise NotImplementedError(
            f"internal error: 'get_actions_for_discovered_state' not implemented")  # pragma: no cover

    # noinspection PyUnusedLocal
    def configure_action_argument_parser(self, action: str, argparser: argparse.ArgumentParser):
        """Called to configure the argument parser for the given action.

        SHOULD be implemented by subclasses to support actions that require command line arguments. Those arguments will
        be supplied by Deployster if specified for the action in its "args" array."""
        if action == 'init':
            pass
        elif action == 'state':
            pass

    @action
    def init(self, args) -> None:
        """This is the "init" action, usually set as the default entrypoint of the resource."""
        if args: pass

        plugs: dict = self._plugs
        print(json.dumps({
            "plugs": {plug_name: plug.to_dict() for plug_name, plug in plugs.items()},
            "config_schema": self.config_schema,
            "state_action": {
                "args": ["state"]
            }
        }))

    @action
    def state(self, args) -> None:
        if args: pass
        state: dict = self.discover_state()
        if state is not None:
            actions: Sequence[DAction] = self.get_actions_for_discovered_state(state=state)
            if actions:
                print(json.dumps({
                    'status': 'STALE',
                    'staleState': state,
                    'actions': [action.to_dict() for action in actions]
                }, indent=2))
            else:
                print(json.dumps({
                    'status': 'VALID',
                    'state': state
                }, indent=2))
        else:
            print(json.dumps({
                'status': 'STALE',
                'actions': [action.to_dict() for action in self.get_actions_for_missing_state()]
            }, indent=2))

    def execute_action(self, action_name: str,
                       action_method: Callable[['DResource', argparse.Namespace], None],
                       args: argparse.Namespace):
        if action_name: pass
        action_method(self, args)

    def execute(self, args=sys.argv[1:]) -> None:
        argparser: argparse.ArgumentParser = argparse.ArgumentParser(description=f"Resource {self.info.name}")
        subparsers = argparser.add_subparsers()

        inspection_target = type(self)
        for attr_name in dir(inspection_target):
            attr: Any = getattr(inspection_target, attr_name)
            if callable(attr):
                method: function = attr
                if hasattr(method, 'action'):
                    parser = subparsers.add_parser(method.__name__)
                    self.configure_action_argument_parser(method.__name__, parser)
                    parser.set_defaults(action_name=method.__name__, action_method=method)

        args = argparser.parse_args(args=args)
        self.execute_action(args.action_name, args.action_method, args)


#Функция get_the_fastest_func()
#Реализуйте функцию get_the_fastest_func(), которая принимает два аргумента в следующем порядке:

#funcs — список произвольных функций
#arg — произвольный объект
#Функция get_the_fastest_func() должна возвращать функцию из списка funcs,
# которая затратила на вычисление значения при вызове с аргументом arg наименьшее количество времени.

import time

def get_the_fastest_func(funcs,arg):
    list_of_time=[]
    for functions in funcs:
        start_time = time.time()
        result=functions(arg)
        end_time = time.time()
        time_func=end_time-start_time
        list_of_time.append(time_func)
    minimum = min(list_of_time)
    return funcs[list_of_time.index(minimum)]


import pygame
import pygame_menu
from paddle import Paddle
from ball import Ball
from brick import Brick
from mc import Agent
from board import Board
import os
import sys
import time
import random

# Clearing the board
WHITE = (255, 255, 255)

# Define the Brick class
# class Brick(pygame.sprite.Sprite):
#     def __init__(self, x, y, width, height):
#         super().__init__()
#         self.image = pygame.Surface((width, height))
#         self.image.fill((255, 255, 255))  # White color for the brick
#         self.rect = self.image.get_rect()
#         self.rect.x = x
#         self.rect.y = y

# Initialize Pygame
pygame.init()

# Set up the screen
board: Board = Board()
agent = Agent()
#screen = pygame.display.set_mode((800, 600))
#pygame.display.set_caption("Breakout Game")

# Create the menu
menu = pygame_menu.Menu("Select Game Layout!!", 400, 300, theme=pygame_menu.themes.THEME_BLUE)

def create_bricksss(layers, brick_size, bricks_per_layer):
    bricks = []
    for layer in range(layers):
        for brick in range(bricks_per_layer):
            bricks.append(
                Brick(
                    brick_size[0] * brick,
                    brick_size[1] * layer,
                    brick_size[0],
                    brick_size[1],
                )
            )

    return pygame.sprite.Group(bricks)

def run_game(bricks):
    # Variables for time tracking
    start_time = time.time()
    last_time = start_time

    won = False
    while not won:
        # List with all sprite objects
        all_sprites_list = pygame.sprite.Group()

        # Code for putting in the paddle
        paddle = Paddle(5, 1, *board.size)
        ball = Ball(1, 1, *board.size)

        all_sprites_list.add(paddle)
        all_sprites_list.add(ball)
        all_sprites_list.add(bricks)
        # We want to play
        play = True

        # Define a clock
        clock = pygame.time.Clock()
        #fps = 200
        agent_wait_time = 10
        iteration = 0
        paddle_bumps: int = 0
        current_time = time.time()
        while play:
            for event in pygame.event.get():
                # Manual control
                # if event.type == pygame.KEYDOWN:
                #     # keys = pygame.key.get_pressed()
                #     # if keys[pygame.K_LEFT]:
                #     #     paddle.speed-=1
                #     # if keys[pygame.K_RIGHT]:
                #     #     paddle.speed+=1
                #     # paddle.update_speed()
                #     paddle.speed += random.choice([-1, 0, 1])

                if event.type == pygame.QUIT:
                    play = False
                    won = True

            if not iteration % agent_wait_time:
                # Agent:

                state = (
                    paddle.rect.x,  # Paddle position
                    paddle.speed,  # Paddle speed
                    (ball.rect.x, ball.rect.y),  # Ball position
                    tuple(ball.speed),  # Ball speed
                )

                action = agent.policy(state)
                paddle.speed += action
                paddle.update_speed()

            # --- Drawing code should go here
            # First, clear the screen be white
            board.surface.fill(WHITE)

            # Recognizes break successfully
            tobreak = False
            tobreak = ball.check_over(board.size[1], bricks)
            if tobreak:
                elapsed_time = int(current_time - start_time)
                print(f"The game took {elapsed_time} seconds to complete!")
                # pygame.time.delay(3000)
                play = False
                #break
                # Restarts the program after time delay
                # restart_program()

            # Calculate current time
            current_time = time.time()

            # Check if 10 seconds have passed since the last time print
            if current_time - last_time >= 10:
                print(f"The length of the bricks list is {len(bricks)}")
                elapsed_time = int(current_time - start_time)
                print("Elapsed time: ", elapsed_time, " seconds")
                last_time = current_time

            # Move paddle
            # Moving the paddle when the user uses the arrow keys

            ball.reflect()
            ball.collision_bricks(bricks)
            # Return only bricks
            # bricks = all_sprites_list.sprites()[2:]
            if ball.collision_paddle(paddle):
                paddle_bumps += 1
            ball.move(ball.speed)
            paddle.move_x(paddle.speed)
            if paddle.collision_x():
                paddle.speed = 0

            # if ball.check_gameover():
            #    play = False
            #    break

            all_sprites_list.update()

            # Now let's draw all the sprites in one go. (For now we only have 2 sprites!)
            all_sprites_list.draw(board.surface)

            # Draw the grid lines
            # for x in range(0, size[0], pixel_scale):
            #     pygame.draw.line(screen, ORANGE, (x, 0), (x, size[1]))
            # for y in range(0, size[1], pixel_scale):
            #     pygame.draw.line(screen, ORANGE, (0, y), (size[0], y))

            # pygame.draw.line(screen, ORANGE, [0, 38], [800, 38], 2)

            if not iteration % agent_wait_time:
                # Agent rewards
                reward = agent.get_score(bricks, paddle_bumps)
                paddle_bumps = 0
                agent.remember_reward(state, action, reward)
                print(
                    f"""
                    Agent:
                    state:        {state}
                    action:       {action}
                    reward:       {reward}
                    total_reward: {agent.total_reward}
                    bricks:       {len(bricks)}
                    """
                )

            # --- Go ahead and update the screen with what we've drawn.
            board.render()
            board.display.flip()

            # --- Limit to 60 frames per second
            iteration += 1
            clock.tick(30)

# Define layout selection functions
def layout1_function():
    print("Layout 1 selected")
    bricks = create_bricksss(2, (3, 1), 5)
    run_game(bricks)

def layout2_function():
    print("Layout 2 selected")
    bricks = create_bricksss(3, (3, 1), 5)
    run_game(bricks)

def layout3_function():
    print("Layout 3 selected")
    bricks = create_bricksss(5, (3, 1), 5)
    run_game(bricks)

# create_bricks(3, (3, 1), 5)

# Add layout options
menu.add.button("Layout 1", layout1_function)
menu.add.button("Layout 2", layout2_function)
menu.add.button("Layout 3", layout3_function)
menu.add.button("Exit", pygame_menu.events.EXIT)

running = True
while running:
    # Process events
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False

    # Update menu
    menu.mainloop(board.screen, disable_loop=True)

    board.display.flip()
    # Update screen
    #pygame.display.flip()

pygame.quit()


import numpy as np
import pysmile

# pysmile_license is your license key
import smile_licence.pysmile_license
import pandas as pd
from pprint import pprint

class BayesNetwork:
    def __init__(self):
        self.net = pysmile.Network()
        self.net.read_file("kabada_customer_segments.xdsl")
        self.num_added_evidence = 0
        # pprint([_ for _ in dir(self.net) if "get_" in _])
        # # print(self.net.get_first_node())
        # # exit()
        # print(self.net.get_all_node_ids())
        # print(self.net.get_all_nodes())
        # # exit()
        # self.id2name = {k: v for k, v in zip(self.net.get_all_node_ids(), self.net.get_all_nodes())}
        # # pprint(self.id2name)
        # # exit()

    def learn(self, path_newdata):
        ds = pysmile.learning.DataSet()
        ds.read_file(path_newdata)
        matching = ds.match_network(self.net)
        em = pysmile.learning.EM()
        em.learn(ds, self.net, matching)

    def add_evidence(self, varname, val):
        self.num_added_evidence += 1
        self.net.set_evidence(varname, val)
        self.net.update_beliefs()

    def predict_popup(self, varname):
        if self.num_added_evidence == 0:
            arr = self.net.get_node_definition(varname)
            # self.net.get_node_count(self.net.get_node(varname))
            # node = self.net.get_node(varname)
            # print(node)
            n = len(arr)
            n2 = int(n / 2) # number of states are 2
            arr = np.array([arr[:n2], arr[n2:]])
            print("add some evidence")
            return None
        probs = self.net.get_node_value(varname)
        i_most_likely = np.argmax(probs)
        return self.net.get_outcome_id(varname, i_most_likely), probs[i_most_likely]

    def clear_evidence(self, varname=None):
        if varname is None:
            self.num_added_evidence = 0
            self.net.clear_all_evidence()
        else:
            self.num_added_evidence -= 1
            self.net.clear_evidence(varname)

popup_ai = BayesNetwork()

# tab = pd.read_csv("kabada_customer_segments.txt")
# print(tab.columns)
# exit()
# popup_ai.add_evidence("is_added_consumer", "no")
popup_ai.add_evidence("age_under_12", "yes")
popup_ai.add_evidence("age_12_17", "no")
popup_ai.add_evidence("age_18_24", "no")
popup_ai.add_evidence("age_25_34", "no")
popup_ai.add_evidence("age_35_64", "no")
popup_ai.add_evidence("age_65_74", "no")
popup_ai.add_evidence("age_75_over", "no")
print(popup_ai.predict_popup("education_primary"))
print(popup_ai.predict_popup("education_secondary"))
print(popup_ai.predict_popup("education_higher"))


from os import stat
from sqlite3.dbapi2 import connect
from tkinter import *
from PIL import Image,ImageTk
from tkinter import ttk,messagebox
import sqlite3
import pyttsx3
class DepartmentClass:
    def __init__(self,root):
        self.root = root
        root.geometry('1033x500+310+140')
        root.resizable(False,False)
        root.config(bg='#9ec9d9')
        root.focus_force()
        root.title('Departments Details')

        #===========Search============
        self.var_searchby = StringVar()
        srch_lbl_frame = LabelFrame(self.root,text='Search Department',bg='#9ec9d9')
        srch_lbl_frame.place(x=200,y=15,w=600,h=55)
        self.search_combo = ttk.Combobox(srch_lbl_frame,font=('times new roman',13,'bold'),textvariable=self.var_searchby,values=('DepID','Name'),justify=CENTER,state='readonly')
        self.search_combo.place(x=20,y=3,width=200)
        self.search_combo.set('Select')

        self.var_searchtxt = StringVar()
        self.txt_search = Entry(srch_lbl_frame,font=('times new roman',14),bg='lightyellow',textvariable=self.var_searchtxt)
        self.txt_search.place(x=230,y=3)

        self.btn_search = Button(srch_lbl_frame,text='Search',font=('times new roman',14),bg='#51b54a',fg='white')
        self.btn_search.place(x=420,y=2,h=25,w=125)
         #=========heading Label================
        title = Label(self.root,text='Department Details',font=('goudy old style',20,'bold'),bg='#596980',fg='white').place(x=30,y=80,width=970,height=30)
        #============widgets===================
        supp_frame = LabelFrame(self.root,bg='white',text='Category')
        supp_frame.place(x=30,y=120,w=970,h=90)

        self.var_department = StringVar()
        self.var_department_id = StringVar()
        lbl_department = Label(supp_frame,text='Department:',font=('times new roman',15),bg='white').place(x=200,y=5)
        self.txt_department = Entry(supp_frame,font=('times new roman',14),bg='lightyellow',textvariable=self.var_department)
        self.txt_department.place(x=310,y=5)

        #========Buttons================
        self.btn_save = Button(supp_frame,command=self.add,text='Save',font=('times new roman',14),bg='#597b99',fg='white')
        self.btn_save.place(x=230,y=40,h=25,w=80)
        self.btn_update = Button(supp_frame,text='Update',command=self.update,font=('times new roman',14),bg='#256934',fg='white')
        self.btn_update.place(x=320,y=40,h=25,w=80)
        self.btn_delete = Button(supp_frame,text='Delete',command=self.delete,font=('times new roman',14),bg='#ad2a42',fg='white')
        self.btn_delete.place(x=410,y=40,h=25,w=80)
        self.btn_clear = Button(supp_frame,text='Clear',font=('times new roman',14),bg='#285863',fg='white')
        self.btn_clear.place(x=500,y=40,h=25,w=80)

        #==========employee details table============
        emp_frame = Frame(self.root,bd=3,relief=RIDGE)
        emp_frame.place(x=30,y=210,h=265,w=600)

        self.xscroll = Scrollbar(emp_frame,orient=HORIZONTAL)
        self.yscroll = Scrollbar(emp_frame,orient=VERTICAL)
        self.departmentTable = ttk.Treeview(emp_frame,columns=('catid','name'),xscrollcommand=self.xscroll.set,yscrollcommand=self.yscroll.set)
        self.xscroll.pack(side=BOTTOM,fill=X)
        self.yscroll.pack(side=RIGHT,fill=Y)
        self.xscroll.config(command=self.departmentTable.xview)
        self.yscroll.config(command=self.departmentTable.yview)

        self.departmentTable.heading('catid',text='department ID')
        self.departmentTable.heading('name',text='Name')
        self.departmentTable['show'] = 'headings'
        self.departmentTable.pack(fill=BOTH,expand=1)
        self.departmentTable.bind('<ButtonRelease>',self.fetch_data)
        self.show()

    def show(self):
        con = sqlite3.connect(database='database/ims.db')
        cur = con.cursor()
        try:
            cur.execute('select * from department')
            row = cur.fetchall()
            if len(row)!=0:
                self.departmentTable.delete(*self.departmentTable.get_children())
                for i in row:
                    self.departmentTable.insert('',END,values=i)
        except Exception as ex:
            messagebox.showerror('Error',f'error due to:{str(ex)}',parent=self.root)

    def add(self):
        con = sqlite3.connect(database='database/ims.db')
        cur = con.cursor()
        try:
            if self.var_department.get()=='':
                messagebox.showerror('Error','Department name required to be entered!!',parent=self.root)
            else:
                cur.execute('select * from department where depid=?',(self.var_department_id.get(),))
                row = cur.fetchone()
                if row!=None:
                    messagebox.showerror('Error','Department name already exist!!',parent=self.root)
                else:
                    cur.execute('insert into department (name) values(?)',(self.var_department.get(),))
                    con.commit()
                    e = pyttsx3.init()
                    e.say('department added successfully')
                    e.runAndWait()
                    con.close()
                    self.show()
        except Exception as ex:
            messagebox.showerror('Error',f'error due to:{str(ex)}',parent=self.root)

    def update(self):
        con = sqlite3.connect(database='database/ims.db')
        cur = con.cursor()
        try:
            if self.var_department.get()=='':
                messagebox.showerror('Error','Department name required to be entered!!',parent=self.root)
            # else:
            #     cur.execute('select * from category where name=?',(self.var_category.get(),))
            #     row = cur.fetchone()
            #     if row==None:
            #         messagebox.showerror('Error','please select category from list !!',parent=self.root)
            else:
                        cur.execute('update department set name=? where depid=?',(
                        self.var_department.get(),
                        self.var_department_id.get()
                        ))
                        con.commit()
                        e = pyttsx3.init()
                        e.say('department updated successfully')
                        e.runAndWait()
                        con.close()
                        self.show()
        except Exception as ex:
            messagebox.showerror('Error',f'error due to:{str(ex)}',parent=self.root)

    def clear(self):
        self.var_department.set('')
        self.var_searchby.set('Select')
        self.var_searchtxt.set('')

    def fetch_data(self,e):
        r= self.departmentTable.focus()
        content = self.departmentTable.item(r)
        row = content['values']
        # print(row)
        self.var_department_id.set(row[0])
        self.var_department.set(row[1])

    def delete(self):
        con = sqlite3.connect(database='database/ims.db')
        cur = con.cursor()
        try:
            if self.var_department_id.get()=='':
                messagebox.showerror('Error','Department id must be required!!!',parent=self.root)
            else:
                cur.execute('select * from department where depid=?',(self.var_department_id.get(),))
                row = cur.fetchone()
                if row==None:
                    messagebox.showerror('Error','Please select record from list',parent=self.root)
                else:
                    op = messagebox.askyesno('confirm','Do you really want to delete this record!!!',parent=self.root)
                    if op==True:
                        cur.execute('delete from department where depid=?',(self.var_department_id.get(),))
                        con.commit()
                        e = pyttsx3.init()
                        e.say('department deleted successfully')
                        e.runAndWait()
                        self.show()
                        con.close()
        except Exception as ex:
            messagebox.showerror('Error',f'error due to:{str(ex)}',parent=self.root)

if __name__ == '__main__':
    root = Tk()
    obj = DepartmentClass(root)
    root.mainloop()


import os
import tempfile

import numpy as np
import pytest

from ipie.utils.io import read_hamiltonian, read_wavefunction, write_hamiltonian, write_wavefunction
from ipie.utils.testing import get_random_phmsd_opt

@pytest.mark.unit
def test_read_write():
    nmo = 10
    naux = 100
    hcore = np.random.random((nmo, nmo))
    LXmn = np.random.random((naux, nmo, nmo))
    e0 = 18.0
    with tempfile.NamedTemporaryFile() as tmpfile:
        write_hamiltonian(hcore, LXmn, e0, filename=tmpfile.name)
        hcore_read, LXmn_read, e0_read = read_hamiltonian(tmpfile.name)
        assert np.allclose(hcore_read, hcore)
        assert np.allclose(LXmn_read, LXmn)
        assert e0 == pytest.approx(e0_read)

@pytest.mark.unit
def test_read_write_single_det_rhf():
    nmo = 10
    nalpha = 5
    wfn = np.random.random((nmo, nalpha))
    with tempfile.NamedTemporaryFile() as tmpfile:
        write_wavefunction(wfn, filename=tmpfile.name)
        wfn_read = read_wavefunction(tmpfile.name)
        assert np.allclose(wfn, wfn_read)

@pytest.mark.unit
def test_read_write_single_det_uhf():
    nmo = 10
    nalpha = 5
    nbeta = 3
    wfna = np.random.random((nmo, nalpha))
    wfnb = np.random.random((nmo, nbeta))
    wfn = [wfna, wfnb]
    with tempfile.NamedTemporaryFile() as tmpfile:
        write_wavefunction(wfn, filename=tmpfile.name)
        wfn_read, _ = read_wavefunction(tmpfile.name)
        assert np.allclose(wfn[0], wfn_read[0])
        assert np.allclose(wfn[1], wfn_read[1])

@pytest.mark.unit
def test_read_write_noci_wavefunction():
    ndet = 10
    nmo = 10
    nalpha = 5
    nbeta = 7
    wfna = np.random.random((ndet, nmo, nalpha))
    wfnb = np.random.random((ndet, nmo, nbeta))
    ci_coeffs = np.random.random((ndet))
    with tempfile.NamedTemporaryFile() as tmpfile:
        wfn = (ci_coeffs, [wfna, wfnb])
        write_wavefunction(wfn, filename=tmpfile.name)
        wfn_read, _ = read_wavefunction(tmpfile.name)
        assert np.allclose(wfn[0], wfn_read[0])
        assert np.allclose(wfn[1][0], wfn_read[1][0])
        assert np.allclose(wfn[1][1], wfn_read[1][1])

@pytest.mark.unit
def test_read_write_particle_hole_wavefunction():
    ndet = 10
    nmo = 10
    nalpha = 5
    nbeta = 7
    wfn, _ = get_random_phmsd_opt(nalpha, nbeta, nmo, ndet=ndet)
    with tempfile.NamedTemporaryFile() as tmpfile:
        write_wavefunction(wfn, filename=tmpfile.name)
        wfn_read, _ = read_wavefunction(tmpfile.name)
        assert np.allclose(wfn[0], wfn_read[0])
        assert np.allclose(wfn[1], wfn_read[1])
        assert np.allclose(wfn[2], wfn_read[2])


"""
S. Bianco, R. Schettini,
Two New von Kries Based Chromatic Adaptation Transforms Found by Numerical Optimization,
2010,
<https://doi.org/10.1002/col.20573>
"""
import numpy as np
from numpy.typing import ArrayLike

from .von_kries import _von_kries_type

def bianco_schettini(whitepoint_source: ArrayLike, whitepoint_target: ArrayLike):
    M = np.array(
        [
            [0.8752, 0.2787, -0.1539],
            [-0.8904, 1.8709, 0.0195],
            [-0.0061, 0.0162, 0.9899],
        ]
    )
    return _von_kries_type(M, whitepoint_source, whitepoint_target)

def bianco_schettini_pos(whitepoint_source: ArrayLike, whitepoint_target: ArrayLike):
    M = np.array(
        [
            [0.6489, 0.3915, -0.0404],
            [-0.3775, 1.3055, 0.0720],
            [-0.0271, 0.0888, 0.9383],
        ]
    )
    return _von_kries_type(M, whitepoint_source, whitepoint_target)


# -*- coding: utf-8 -*-
import requests
import pandas as pd
url = "https://iftp.chinamoney.com.cn/ags/ms/cm-u-bond-md/BondMarketInfoListEN"
rows = []
for i in range(1,5):
    params = {'bondType': '100001', 'issueYear': '2023', 'pageNo': str(i),'pageSize': '15'}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
    }
    response = requests.post(url, params=params,headers=headers)
    data = response.json()
    # 解析返回的表格数据
    table_data = data["data"]["resultList"]
    #print(len(table_data))
    columns = ["isin","bondCode","entyFullName","bondType","issueStartDate","debtRtng"]

    for record in table_data:
        row = [record[col] for col in columns]
        rows.append(row)
new_colum = ["ISIN", "Bond Code", "Issuer", "Bond Type", "Issue Date", "Latest Rating"]

df = pd.DataFrame(rows, columns=new_colum)
df.to_csv("bond_data.csv", index=False)


import sys, os, re, collections
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'resource-gcg', 'scripts'))
import tree

for line in sys.stdin:
  Q = [ ]
  f = 0
  j = 0
  p = ''
  w = ''

  def calccontext ( tr, s=1, d=0 ):
    global Q, f, j, p, w

    #### at preterminal node...
    if len(tr.ch)==1 and len(tr.ch[0].ch)==0:
      f = 1 - s
      p = tr.c
      w = tr.ch[0].c

    #### at non-preterminal unary node...
    elif len(tr.ch)==1:
      calccontext ( tr.ch[0], s, d )

    #### at binary nonterminal node...
    elif len(tr.ch)==2:
      ## traverse left child...
      calccontext ( tr.ch[0], 0, d if s==0 else d+1 )
      j = s
      #print( d, f, j, Q )
      #Q = ( Q[:d+f-j-2] if d+f-j>=2 else [ ] ) + ( [ ( Q[d+f-j-1].partition('/')[0] if j==1 else tr.c ) + '/' + tr.ch[1].c ] if d+f-j>=1 else [ ] )
      Q = ( Q[:d-1] if d>=1 else [ ] ) + ( [ ( Q[d-1].partition('/')[0] if j==1 else tr.c ) + '/' + tr.ch[1].c ] if d>=1 else [ ] )
      print ( w + ' ' + p + ' ' + str(f) + ' ' + str(j) + ' ' + ';'.join(Q) )
      ## traverse right child...
      t = calccontext ( tr.ch[1], 1, d )

  # print line
  #print ( line )
  tr = tree.Tree('T',[tree.Tree(),tree.Tree('T')])
  tr.ch[0].read ( line )
  calccontext ( tr )
  #print ( str(f) + ' ' + str(j) + ' ' + ';'.join(Q) + ' ' + p )


# Why we use Class???

champ1_name = "Ezreal"
champ1_health = 700
champ1_attack = 90

print(f" Gamer {champ1_name}. Welcome to Summoner's Canyon.")

champ2_name = "Lee Sin"
champ2_health = 900
champ2_attack = 85

print(f" Gamer {champ2_name}. Welcome to Summoner's Canyon.")

def basic_attack(name, attack):
    print(f"{name} basic-attack power {attack}")

basic_attack(champ1_name, champ1_attack)
basic_attack(champ2_name, champ2_attack)

print("============Case in using Class.=========")

class Champion:
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack
        print(f"Gamer {name}. Welcome to EVERLAND PARK")
    def basic_attack(self):
        print(f"{self.name} normal strike {self.attack}")

Jayce = Champion("Jayce", 769, 100)
Teemo = Champion("Teemo", 600, 60)
Jhin = Champion("Jhin", 875, 87)
Jayce.basic_attack()
Teemo.basic_attack()
Jhin.basic_attack()

# It's easy to make output


#!/usr/bin/env python

import numpy;
import sptensor;
import tensor;
import tools;

class ttensor:
    core = None;
    u = None;

    def __init__(self, core, uIn):
        """ Create a tucker tensor object with the core and matrices.
        NOTE: uIn has to be a list of arrays/matrices"""
        if not(core.__class__ == tensor.tensor or core.__class__ == sptensor.sptensor):
            raise ValueError("core is neither tensor nor sptensor!");

        #Handle if the uIn is not a list
        if(uIn.__class__ != list):
            newuIn = [];
            for x in uIn:
                newuIn.extend([x]);
            uIn = newuIn;

        newuIn = [];
        for i in range(0, len(uIn)):
            newuIn.extend([uIn[i].copy()]);
        uIn = newuIn;

        # check that each U is indeed a matrix
        for i in range(0,len(uIn)):
            if (uIn[i].ndim != 2):
                raise ValueError("{0} is not a 2-D matrix!".format(uIn[i]));

        # Size error checking
        k = core.shape;

        if (len(k) != len(uIn)):
            raise ValueError("Number of dims of Core andthe number of matrices are different");

        for i in range(0,len(uIn)):
            if (k[i] != len((uIn[i])[0])):
                raise ValueError(
                    "{0} th dimension of Core is different from the number of columns of uIn[i]"
                    .format(i));

        self.core = core.copy();
        self.u = uIn;

        #save the shape of the ttensor
        shape = [];
        for i in range(0, len(self.u)):
            shape.extend([len(self.u[i])]);
        self.shape = tuple(shape);
        # constructor end #

    def size(self):
        ret = 1;
        for i in range(0, len(self.shape)):
            ret = ret * shape[i];
        return ret;

    def dimsize(self, ind):
        return shape[ind];

    def copy(self):
        return ttensor(self.core, self.u);

    def totensor(self):
        """returns a tensor object that is represented by the tucker tensor"""

        #ensure that there is enough space
        X = tensor.tenzeros(self.shape);
        X = self.core.ttm(self.u);

        if(X.__class__ == sptensor.sptensor):
            X = X.totensor();
        return X;

    def __str__(self):
        ret = "ttensor of size {0}\n".format(self.shape);
        ret += "Core = {0} \n".format(self.core.__str__());
        for i in range(0, len(self.u)):
            ret += "u[{0}] =\n{1}\n".format(i, self.u[i]);

        return ret;


import scenes.lamps as lamps
import scenes.pre_process as pre_process
import time

def lf1():
    """Controll Lamps."""
    HOUSE = pre_process.get_house()

    v1 = HOUSE.lamps[1]
    v2 = HOUSE.lamps[2]
    v3 = HOUSE.lamps[3]
    window = HOUSE.lamps[5]
    ball = HOUSE.lamps[19]

    active_lamps = set()
    active_lamps.add(v1.id_number)
    active_lamps.add(v2.id_number)
    active_lamps.add(v3.id_number)
    active_lamps.add(window.id_number)
    active_lamps.add(ball.id_number)

    for id_num, lamp in HOUSE.lamps.items():
        if id_num not in active_lamps:
            lamp.turn_off()
            lamp.set_transition_time(50)
            lamp.send_command()

    time.sleep(6)

    v1.turn_on()
    v2.turn_on()
    v3.turn_on()
    window.turn_on()
    ball.turn_on()

    v1.set_brigthness(30)
    v2.set_brigthness(30)
    v3.set_brigthness(30)

    window.set_brigthness(200)
    window.set_transition_time(50)
    window.set_ct(500)

    ball.set_brigthness(100)
    ball.set_transition_time(50)
    ball.set_hue(43690)
    ball.set_sat(254)
    ball.set_xy(0.1691, 0.0441)

    v1.send_command()
    v2.send_command()
    v3.send_command()
    window.send_command()
    ball.send_command()

def all_lamps_slow():
    """Turn on all lamps in the house in 50 seconds."""
    HOUSE = pre_process.get_house()

    for id_num, lamp in HOUSE.lamps.items():
        lamp.turn_on()
        lamp.set_transition_time(500)
        lamp.set_brigthness(254)
        lamp.send_command()


# Declare Array
_array = []
# input Array Size
n = int(input("Enter the number of element: "))

# save Array Element

for v in range(0, n):
    _array.append(int(input("Enter a number")))

# print array elements2
print(_array)

# implementation of bubblesort
def bubble_sort(a):
    n = len(a)
    for i in range(0, n):
        for j in range(n - 1, i, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]

# call the bubble sort
bubble_sort(_array)

print('sorted array:')

print(_array)


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

import random
from tqdm import tqdm
import glob
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from dataloader import CQTsDataset
from model import SSMnet
from utils import weighted_bce_loss
from madgrad import madgrad_wd

print('libraries imported')

device = torch.device("cuda:0" if torch.cuda.is_available() else "error")
root_path = "/tsi/clusterhome/atiam-1005/M2-ATIAM-internship/music-structure-estimation/SSMnet/"
data_path_harmonix = "/tsi/clusterhome/atiam-1005/data/Harmonix/cqts/*"
data_path_isoph = "/tsi/clusterhome/atiam-1005/data/Isophonics/cqts/*"

name_exp = "no_freeze_f1_fine_tune_a02_euc_lr5e-3_wd1e-2_harmonix"
writer = SummaryWriter('{0}runs/{1}'.format(root_path, name_exp))

N_EPOCH = 250
batch_size = 1
backward_size = 6

files_train = glob.glob(data_path_harmonix)
files_val = glob.glob(data_path_isoph)

val_dataset = CQTsDataset(files_val)
validation_loader = DataLoader(
    dataset=val_dataset,
    batch_size=batch_size,
)

print(len(files_train), 'training examples')
print(len(files_val), 'validation examples')

model = SSMnet().to(device)

best_loss = float('inf')

losses_cqts = np.zeros((len(validation_loader)))
losses_init = np.zeros((len(validation_loader)))
losses_pretrain = np.zeros((len(validation_loader)))
losses_ssmnet = np.zeros((len(validation_loader)))

"""
print('evaluation untrained model')
with torch.no_grad():
        for n in range(10):
            model = SSMnet().to(device)
            model.eval()
            running_loss = 0.0
            running_loss_cqts = 0.0
            for i, (cqts, ssm) in enumerate(tqdm(validation_loader)):
                embeds = model.apply_cnn(cqts.transpose(0, 1)).unsqueeze(0)
                cqts_flat = cqts.reshape(1, -1, 72 * 64)/(torch.max(cqts) + 1e-7)
                _, T, _ = cqts_flat.shape
                ssm_cqts = torch.cdist(cqts_flat, cqts_flat)
                ssm_cqts = 1 - ssm_cqts/torch.max(ssm_cqts)
                ssm_hat = torch.cdist(embeds, embeds)
                ssm_hat = 1 - ssm_hat/torch.max(ssm_hat)
                val_loss = weighted_bce_loss(ssm_hat, ssm)
                cqts_loss = weighted_bce_loss(ssm_cqts, ssm)
                running_loss += val_loss.item()
                running_loss_cqts += cqts_loss.item()
                losses_cqts[i] +=  cqts_loss/10
                losses_init[i] +=  val_loss/10

        # print statistics
        print('average validation loss (Isophonics): %.6f' %
              (running_loss / len(validation_loader)))
        print('average cqts loss (Isophonics): %.6f' %
              (running_loss_cqts / len(validation_loader)))

np.save(root_path + "losses_cqts", losses_cqts)
np.save(root_path + "losses_init", losses_init)

"""

model.load_state_dict(torch.load('{0}best_ssm_net.pt'.format(root_path)), strict=False)

print('evaluating model with pretrained embeddings')

with torch.no_grad():
        model.eval()
        running_loss = 0.0
        for i, (cqts, ssm) in enumerate(tqdm(validation_loader)):
            embeds = model.apply_cnn(cqts.transpose(0, 1)).unsqueeze(0)
            ssm_hat = torch.cdist(embeds, embeds)
            ssm_hat = 1 - ssm_hat/torch.max(ssm_hat)
            val_loss = weighted_bce_loss(ssm_hat, ssm)
            running_loss += val_loss.item()
            losses_ssmnet[i] =  val_loss
        # print statistics
        print('average validation loss (Isophonics): %.6f' %
              (running_loss / len(validation_loader)))

np.save(root_path + "losses_ssmnet", losses_ssmnet)

optimizer = madgrad_wd(model.parameters(), lr=5e-3, weight_decay=0.01)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min')

for epoch in range(N_EPOCH):
    running_loss = 0.0
    random.shuffle(files_train)
    train_dataset = CQTsDataset(files_train)
    train_loader = DataLoader(
          dataset=train_dataset,
          batch_size=batch_size,
          )
    model.train()
    print("EPOCH " + str(epoch + 1) + "/" + str(N_EPOCH))
    for i, (cqts, ssm) in enumerate(tqdm(train_loader)):
        ssm_hat = model(cqts)
        train_loss = weighted_bce_loss(ssm_hat, ssm)
        train_loss.backward()
        running_loss += train_loss.item()
        if (i + 1) % backward_size == 0:
            optimizer.step()
            optimizer.zero_grad()

    optimizer.step()
    optimizer.zero_grad()

    # print statistics
    print('average train loss: %.6f' %
                (running_loss/len(train_loader)))

    # ...log the running loss
    writer.add_scalar('training loss',
                    running_loss / len(train_loader),
                    epoch)

    with torch.no_grad():
        model.eval()
        running_loss = 0.0
        for i, (cqts, ssm) in enumerate(tqdm(validation_loader)):
            ssm_hat = model(cqts)
            val_loss = weighted_bce_loss(ssm_hat, ssm)
            running_loss += val_loss.item()
        # print statistics
        print('average validation loss (Isophonics): %.6f' %
              (running_loss / len(validation_loader)))
        # ...log the running loss
        writer.add_scalar('validation loss (Isophonics)',
                          running_loss / len(validation_loader),
                          epoch)

    scheduler.step(running_loss)
    # Save best model if validation loss is improved and update regularly last model state
    if running_loss <= best_loss:
        torch.save(model.state_dict(), "{0}weights/{1}_best.pt".format(root_path, name_exp))
        best_loss = running_loss

    torch.save(model.state_dict(), "{0}weights/{1}_last.pt".format(root_path, name_exp))

print('Finished Training')


#to do: ramka - mnozenie str
import random
import os
import userchoice
from userchoice import user_input

def check_choices(user_choice, computer_choice, optionslist, game_output):
    if user_choice + computer_choice in optionslist:
        print ('Wynik rozgrywki: ' + game_output + '\n')
        return True
    return False

def computer_level_choice():
    os.system ('cls')
    user_choice_level = user_input ('Wybierz poziom trudnosci komputera:\nA - Sprytny komputer\nB - Mniej sprytny komputer\n', 'AB')
    if user_choice_level == 'A':
        os.system ('cls')
        print ('Wybrales SPRYTNY komputer.\n')
    if user_choice_level == 'B':
        os.system ('cls')
        print ('Wybrales MNIEJ SPRYTNY komputer.\n')
    return user_choice_level

def game_over_YN():
    game_over_choice = 'NNNT'
    game_over = random.choice (game_over_choice)
    user_choice_continue = user_input ('Czy chcesz grac dalej? T/N: ', 'TN')
    if user_choice_continue == 'N':
        if game_over == 'N':
            return True
        else:
            os.system('cls')
            print ('Nie!? Tak łatwo mi się nie wymkniesz! Gramy dalej!')
    else:
        os.system ('cls')
    return False

def computer_random_choice(user_choice_level, options_available):
    if user_choice_level == 'A':
        computer_choice = random.choice (options_available)
    if user_choice_level == 'B':
        double_element = random.choice(options_available)
        all_elements = double_element + options_available
        computer_choice = random.choice(all_elements)
    return computer_choice

class Game():

    def __init__(self, first_screen, possible_choices,choice_screen, remis, win, loss):
        self.first_screen = first_screen
        self.possible_choices = possible_choices
        self.choice_screen = choice_screen
        self.remis = remis
        self.win = win
        self.loss = loss

    def play(self):
        user_choice_welcome = user_input(self.first_screen, 'SPQ')
        os.system ('cls')
        if user_choice_welcome == 'Q':
            print ('Wybrales BRAK rozgrywki.')

        if user_choice_welcome == 'P':
            user_choice_level = computer_level_choice()

        if user_choice_welcome == 'S':
            os.system ('cls')
            print ('Brak mozliwosci wyboru poziomu trudnosci. Grasz ze SPRYTNYM komputerem.\n')

        while user_choice_welcome == 'S' or user_choice_welcome == 'P':
            user_choice = user_input(self.choice_screen, self.possible_choices)

            if user_choice_welcome == 'P':
                computer_choice = computer_random_choice(user_choice_level, self.possible_choices)

            if user_choice_welcome == 'S':
                computer_choice = random.choice (self.possible_choices)
            os.system ('cls')

            print ('Twoj wybor: ', user_choice)
            print ('Wybor komputera: ', computer_choice)

            if check_choices(user_choice, computer_choice, self.remis, 'REMIS'):
                data.append('remis')

            if check_choices(user_choice, computer_choice, self.loss, ' Twoja PRZEGRANA'):
                data.append('P')

            if check_choices(user_choice, computer_choice, self.win, 'Twoja WYGRANA'):
                data.append('W')

            if game_over_YN():
                break

user_win = 0
user_loss = 0
data = []

os.system('cls')
print('WITAJ W GRZE!\nW jaka gre chcesz zagrac?\n')

user_choice_game = user_input ('Wybierz:\n1 - KAMIEN-PAPIER-NOZYCZKI\n2 - KAMIEN-PAPIER-NOZYCZKI-LIZARD-SPOCK\nQ - wyjscie\n', '12Q')

os.system ('cls')
if user_choice_game == 'Q':
        print ('Wybrales BRAK rozgrywki.')

#KAMIEN-PAPIER-NOZYCZKI
if user_choice_game == '1':
    game = Game(
        first_screen='Wybrales KPN .......',
        possible_choices='KPN',
        choice_screen = 'Podaj swoj wybor: \nK - kamien \nP - papier \nN - nozyczki\n',
        remis = ['KK', 'PP', 'NN'],
        win = ['PK', 'NP', 'KN'],
        loss = ['KP', 'PN', 'NK']

    )
    game.play()

#KAMIEN-PAPIER-NOZYCZKI-LIZARD-SPOCK
elif user_choice_game == '2':
    game = Game(
        first_screen='Wybrales KPNLS .....',
        possible_choices='KPNLS',
        choice_screen = 'Podaj swoj wybor: \nK - kamien \nP - papier \nN - nozyczki\n L - lizard\n S - spock',
        remis = ['KK', 'PP', 'NN', 'LL', 'SS'],
        win = ['PK', 'NP', 'KN', 'SK', 'LP', 'SN', 'NL', 'KL', 'PS', 'LS'],
        loss = ['KP', 'PN', 'NK', 'KS', 'PL', 'NS', 'LN', 'LK', 'SP', 'SL']
    )

    game.play()

if data == []:
    print ('KONIEC GRY\n')
else:
    import CC
    a = CC.win_loss_sequence(data, 'W')
    b = len(data)
    c = CC.win_loss_sequence(data, 'P')
    print ('wyniki rozgrywek: ', data)

    print ('ilosc WYGRANYCH uzytkownika: ', data.count('W'))
    print ('ilosc PRZEGRANYCH uzytkownika: ', data.count('P'))

   # jesli W = 0  i P = 0 => ZeroDivisionError (np. dla data = ['remis', 'remis'] dlatego:
    if (data.count('W') + data.count('P')) > 0:
        print ('ilosc WYGRANYCH uzytkownika w %: ', data.count('W')/(data.count('W') + data.count('P'))*100, '%')
        print ('ilosc PRZEGRANYCH uzytkownika w %: ', data.count('P')/(data.count('W') + data.count('P'))*100, '%')
    else:
        print ('ilosc WYGRANYCH uzytkownika w %: 0\nilosc PRZEGRANYCH uzytkownika w %: 0')

    print ('najdluzsza sekwencja WYGRANYCH uzytkownika:', a)
    print ('najdluzsza sekwencja PRZEGRANYCH uzytkownika:', c)

    print ('\nKONIEC GRY')


"""
Взять файл со смертностью, спрогнозировать его на 5 лет вперёд
"""

import sklearn.linear_model as lm

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

with open('2_death.csv', encoding = 'cp1251') as f:
    data = [row.replace('\n', '').split(';') for row in f.readlines()]
    data[0][0] = 'регион'

#print(data)
#sys.exit('')

new_data = []
for row_index, row in enumerate(data):
    cur_row = data[row_index]
    if row_index == 0:
        cur_row.extend(['2019', '2020', '2021', '2022', '2023', '2024'])
    else:
        cur_region = cur_row[0]
        y = list(map(lambda x: float(x.replace(',', '.')), cur_row[2:]))
        #print(cur_region, len(y))

        # построим график текущих значений
        x = list(range(1, len(y) + 1))
        #plt.plot(x, y)
        #plt.show()

        # спрогнозируем ещё пять лет, сделаем график прогноза

        linear = lm.LinearRegression()
        predict_x = list(range(len(y) + 1, len(y) + 1 + 5 + 1))
        #print(predict_x)
        trainX = np.asarray(x).reshape(-1, 1)
        trainY = np.asarray(y).reshape(-1, 1)

        testX = np.asarray(predict_x).reshape(-1, 1)

        linear.fit(trainX, trainY)

        #print(linear.score(trainX, trainY))

        testY = linear.predict(testX)
        y_predict = [int(x[0]) for x in list(testY)]
        #print(testY, y_predict)
        #print(testY)
        #print(x + predict_x)
        #plt.plot(x + predict_x, y + y_predict)
        #plt.show()
        cur_row.extend(list(map(str, y_predict)))

    new_data.append(cur_row)
    #data[row_index] = cur_row

import csv

with open('2_death_export.csv', mode = 'w', encoding = 'utf-8') as f:
    f_writer = csv.writer(f, delimiter=';', lineterminator="\n")
    for row in new_data:
        #print(row)
        f_writer.writerow(row)


# -*- coding: utf-8 -*-
"""
Created on Sat Jun 23 13:40:24 2018

@author: sande
"""
#once we find best height and best width frim count_min.py , we print the top ten ips as per count_min, and top 10 ips as per normal computation

import numpy as np
import hashlib

def countIP(ip_dict, ip_addr):
    if ip_addr in ip_dict:
        ip_dict[ip_addr] += 1
    else:
        ip_dict[ip_addr] = 1

def compute_top_10_error(real, estimation):
    error = 0
    for i in range(10):
        current_ip = estimation[i][1]
        if real[i][1] == current_ip:
            continue

        for j in range(len(real)):
            if real[j][1] == current_ip:
                #print(i, "Found IP address {} {} places away: adding {} to error.".format(current_ip, np.abs(j - i), np.minimum(10, np.abs(j - i)) * (10 - i)))
                error += np.minimum(10, np.abs(j - i)) * (10 - i)
                break

    return error

if __name__ == "__main__":
    h = [15]
    w = [55]
    for height in h:
        for width in w:
            ip_dict = {}
            infected_host_addr = '147.32.84.165'
            sketch_matrix = np.ones((height,width))
            src = '../Data/capture20110816.pcap.netflow.labeled'
            ah = open(src, 'r')

            ah.readline()#skip first line
            counter = 0
            ip_set = set()

            for line_ah in ah:
                counter += 1
                line_array = line_ah.strip().split()
                #print (line_array)
                source_ip = line_array[4].split(':')[0]
                dest_ip = line_array[6].split(':')[0]

                # We are interested in the infected host: which addresses does it connect to?
                # Skip lines that are not from/to this host
                if source_ip != infected_host_addr and dest_ip != infected_host_addr:
                    continue

                # Let 'ip' store the value of the other host
                if source_ip == infected_host_addr:
                    ip = dest_ip
                else:
                    ip = source_ip

                #Do normal count, to evaluate results
                countIP(ip_dict, ip)
                ip_set.add(ip)
                #use md5 and SHA-1 hashing algorithms and build new hash values as per
                #this paper https://www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf
                hash1 = hashlib.md5()
                hash2 = hashlib.sha1()
                for i in range(1,height):
                    hash1.update(ip.encode('utf-8'))
                    hash2.update(ip.encode('utf-8'))

                    hash_n = float.fromhex(hash1.hexdigest()) + (float(i) * float.fromhex(hash2.hexdigest()))
                    # take modulus of the hashed value so that the entry corresponding to that column can be incremented
                    hex_int = int(hash_n) % width
                    #print (hex_int)
                    sketch_matrix[i][hex_int] += 1
                #if (counter == 10):
                    #print (sketch_matrix)
                    #break

#            print (sketch_matrix)
#            print (sketch_matrix.shape)
            # COUNTING EXACT OCCURANCES
            ip_frequencies_real = sorted([tuple(reversed(x)) for x in ip_dict.items()])[::-1]
            ip_dict_hash = {}
            for ip in ip_set:
                min_count = 999999999999999999999999
                hash3 = hashlib.md5()
                hash4 = hashlib.sha1()
                for i in range(1,height):
                    hash3.update(ip.encode('utf-8'))
                    hash4.update(ip.encode('utf-8'))
                    hash_n = float.fromhex(hash3.hexdigest()) + (float(i) * float.fromhex(hash4.hexdigest()))
                    hex_int = int(hash_n) % width
                    if ((sketch_matrix[i][hex_int] < min_count)):
                        min_count = sketch_matrix[i][hex_int]
                ip_dict_hash[ip] = min_count

            ip_frequencies_hash = sorted([tuple(reversed(x)) for x in ip_dict_hash.items()])[::-1]

            print ("Real values")
            for i in range(10):
                print("{}: {} occurances".format(ip_frequencies_real[i][1], ip_frequencies_real[i][0]))
            print ("Top 10 ips with occurrences as per sketching task")
            for i in range(10):
                print("{}: {} occurances".format(ip_frequencies_hash[i][1], ip_frequencies_hash[i][0]))


import sys
import threading
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 7777
try:
    s.bind((host, port))
except socket.error as msg:
    print("bind failed" + str(msg))
    sys.exit()
s.listen(10)
process_sockets_list = []
process_list = []
neighbor_list = []
msg_token = ""
def recv_message(conn):
    while True:
        try:
            received = conn.recv(1024)
            msg_token = received.decode('utf-8')
            print("received token: " + msg_token)
        except:
            continue
        if "Coordinator: " in msg_token :
            le=msg_token.split()
            leader=le[1]
        process_index = process_sockets_list.index(conn)
        if len(process_sockets_list)==process_index+1 :
            to_process=0
        else :
            to_process=process_index+1
        try :
            process_sockets_list[to_process].send(received)
            print("sending :" + received.decode('utf-8'))
        except :
            if process_list[to_process]!=leader :
                process_sockets_list[to_process+1].send(received)
                print("sending :" + received.decode('utf-8'))
            process_sockets_list[to_process].close()
            process_sockets_list.remove(process_sockets_list[to_process])
            process_list.remove(process_list[to_process])
            continue

while True:
    try:
        connection, addr = s.accept()
        process_sockets_list.append(connection)
        recv_process_id = connection.recv(1024)
        from_to_process = recv_process_id.decode('utf-8')
        process_list.append(from_to_process)
        print("Process: " + from_to_process)
        start_thread = threading.Thread(target=recv_message, args=(connection,))
        start_thread.start()
    except socket.error as msg:
        print("thread failed"+msg)

connection.close()
s.close()


from io import BytesIO

import pytest
from werkzeug.datastructures import FileStorage
from PIL import Image

from flask_uploader.validators import (
    ImageSize,
    ValidationError,
)

def test_unsupported_image():
    storage = FileStorage(BytesIO(b'Text file'), 'input.txt')
    validator = ImageSize(min_width=100)
    with pytest.raises(ValidationError):
        validator(storage)

@pytest.mark.parametrize('params', (
    {},
    {'min_width': -1},
    {'min_height': -2},
    {'max_width': -3},
    {'max_height': -4},
    {'min_width': -1, 'min_height': -2, 'max_width': -3, 'max_height': -4},
))
def test_required_one_size_option(params):
    with pytest.raises(ValueError, match=r'At least one of the size options must be given\.'):
        ImageSize(**params)

@pytest.mark.parametrize('params', (
    {'min_width': 10, 'max_width': 1},
))
def test_invalid_width_range(params):
    with pytest.raises(ValueError, match=r'The minimum width must be less than the maximum\.'):
        ImageSize(**params)

@pytest.mark.parametrize('params', (
    {'min_height': 10, 'max_height': 1},
))
def test_invalid_height_range(params):
    with pytest.raises(ValueError, match=r'The minimum height must be less than the maximum\.'):
        ImageSize(**params)

@pytest.mark.parametrize('params,size', (
    ({'min_width': 200, 'min_height': 200, 'max_width': 200, 'max_height': 200}, (100, 100)),
    ({'min_width': 200, 'max_width': 200}, (100, 200)),
    ({'min_height': 200, 'max_height': 200}, (200, 100)),
    ({'min_width': 200, 'min_height': 200}, (100, 100)),
    ({'max_width': 100, 'max_height': 100}, (200, 200)),
    ({'min_width': 200}, (100, 200)),
    ({'max_width': 100}, (200, 100)),
    ({'min_height': 200}, (200, 100)),
    ({'max_height': 100}, (100, 200)),
))
def test_validate_image(params, size):
    validator = ImageSize(**params)
    img = Image.new('RGB', size)
    with pytest.raises(ValidationError, match=r'Invalid image size\.'):
        validator.validate_image(img)

def test_custom_message(mocker):
    mock_img = mocker.Mock(size=(100, 100))
    validator = ImageSize(
        min_width=200,
        min_height=200,
        message='Image size %(width)dx%(height)dpx less then %(min_width)dx%(min_height)dpx.',
    )
    with pytest.raises(ValidationError, match=r'Image size 100x100px less then 200x200px\.'):
        validator.validate_image(mock_img)


from collections import Counter
from typing import Tuple, List

def process_compartments(line: str) -> Tuple[str, str]:
    half = len(line) // 2
    return line[0:half], line[half:]

def find_common(groups: List[str]) -> str:
    first = True
    common = set()
    for group in groups:
        group_uniques = set(Counter(group).keys())
        if first:
            first = False
            common = group_uniques
        else:
            common = common.intersection(group_uniques)

    assert len(common) == 1
    return list(common)[0]

def get_priority(a: str) -> int:
    ascii_value = ord(a)
    if ascii_value >= ord("a"):
        return ascii_value - ord("a") + 1
    else:
        return ascii_value - ord("A") + 27

with open("day03.txt", "r") as file:
    lines = [x.strip() for x in file.readlines()]

    # part 1
    part1_priority = 0
    for x in lines:
        a, b = process_compartments(x)
        common = find_common([a, b])
        part1_priority += get_priority(common)
    print(part1_priority)

    # part 2
    part2_priority = 0
    for x in range(0, len(lines), 3):
        common = find_common(lines[x : x + 3])
        part2_priority += get_priority(common)
    print(part2_priority)