from tensorflow.compiler.tests import xla_test
from tensorflow.python.eager.polymorphic_function import compiler_ir
from tensorflow.python.eager.polymorphic_function import polymorphic_function
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.util import nest

class CompilerIrTest(xla_test.XLATestCase):

  def _compareTwoMethodsCompilerIROutput(self, f, args, kwargs):
    flat_args = list(args) + list(kwargs.values())
    if not all([isinstance(x, tensor.Tensor) for x in flat_args]):
      self.skipTest('It only support args and kwargs are all tf.Tensor types.')

    args_spec = nest.map_structure(tensor.TensorSpec.from_tensor, args)
    kwargs_spec = nest.map_structure(tensor.TensorSpec.from_tensor, kwargs)

    hlo_1 = f.experimental_get_compiler_ir(*args, **kwargs)()
    hlo_2 = f.experimental_get_compiler_ir(*args_spec, **kwargs_spec)()

    if hlo_1 != hlo_2:
      self.fail(
          'The tensor_spec way experimental_get_compiler_ir give diff result'
          ' to normal experimental_get_compiler_ir.'
          f' \nhlo(concrete_input):\n{hlo_1}\nhlo(tensor_spec):\n{hlo_2}\n'
      )

  def test_zero_input(self):
    with ops.device('device:{}:0'.format(self.device)):

      @polymorphic_function.function(jit_compile=True, autograph=False)
      def fun_tf():
        return array_ops.zeros((10), dtype=dtypes.int32)

      self._compareTwoMethodsCompilerIROutput(fun_tf, [], {})

  def test_constant_slice(self):
    with ops.device('device:{}:0'.format(self.device)):
      # Constant slice. This is the common case.
      x = array_ops.zeros((10,), dtype=dtypes.int32)

      @polymorphic_function.function(jit_compile=True, autograph=False)
      def fun_tf(x):
        begin = 0
        return x[begin:5]

      self._compareTwoMethodsCompilerIROutput(fun_tf, [x], {})

  def test_compile_time_constant(self):
    with ops.device('device:{}:0'.format(self.device)):
      # Non-constant slice, but compile-time constant depending only on shapes.
      x = array_ops.zeros((10,), dtype=dtypes.int32)

      @polymorphic_function.function(jit_compile=True, autograph=False)
      def fun_tf(x):
        # begin is a compile-time constant, even if x is not
        begin = array_ops.shape_v2(x)[0] - 2
        return x[begin:]

      self._compareTwoMethodsCompilerIROutput(fun_tf, [x], {})

  def test_capture_constant(self):
    with ops.device('device:{}:0'.format(self.device)):
      # Capture a constant
      outer_ct = [3.0]
      x = ops.convert_to_tensor([2.0, 3.0, 4.0], dtype=dtypes.float32)

      @polymorphic_function.function(jit_compile=True, autograph=False)
      def fun_tf(x):
        return x * gen_array_ops.broadcast_to(outer_ct, x.shape) + 1.0

      self._compareTwoMethodsCompilerIROutput(fun_tf, [x], {})

  def test_unsupported_dynamic_input(self):
    with ops.device('device:{}:0'.format(self.device)):

      @polymorphic_function.function(jit_compile=True)
      def f(x):
        return x

      with self.assertRaisesRegex(
          ValueError, 'Only support static input shape but got'
      ):
        args_spec = [tensor.TensorSpec((None), dtype=dtypes.float32)]
        concrete_fn = f.get_concrete_function(*args_spec)
        _ = compiler_ir.from_concrete_function(concrete_fn)(stage='hlo')

  def test_unsupported_shape_depend_input(self):
    with ops.device('device:{}:0'.format(self.device)):
      # Those cases output shapes are dynamic.
      @polymorphic_function.function(jit_compile=True)
      def f2(x):
        return x[x[0] : 0]

      args = [ops.convert_to_tensor([1, 2, 3, 4])]
      args_spec = nest.map_structure(tensor.TensorSpec.from_tensor, args)
      concrete_fn = f2.get_concrete_function(*args_spec)
      _ = compiler_ir.from_concrete_function(concrete_fn)(stage='hlo')

  def test_make_handledata_tensor_specs(self):
    with ops.device('device:{}:0'.format(self.device)):
      v1 = variables.Variable([0.1, 0.1])
      v3 = variables.Variable([1], dtype=dtypes.int32)

      @polymorphic_function.function(jit_compile=True)
      def f4(a, b):
        return (a + b) * v1 - math_ops.cast(v3, dtypes.float32)

      a = constant_op.constant([1.1, 1.1])
      b = constant_op.constant([2.2, 2.2])

      kwargs = {'b': a, 'a': b}

      kwargs_spec = nest.map_structure(
          tensor.TensorSpec.from_tensor, kwargs
      )
      concrete_fn = f4.get_concrete_function(**kwargs_spec)
      captured_inputs = concrete_fn.captured_inputs
      captured_spec = compiler_ir.make_handledata_tensor_specs(captured_inputs)
      self.assertEqual(len(captured_spec), 2)
      self.assertEqual(
          captured_spec[0], tensor.TensorSpec((2), dtype=dtypes.float32)
      )
      self.assertEqual(
          captured_spec[1], tensor.TensorSpec((1), dtype=dtypes.int32)
      )

  def test_capture_variable_1(self):
    if 'gpu' in self.device.lower():
      self.skipTest('Skip test on GPU')

    with ops.device('device:{}:0'.format(self.device)):
      v1 = variables.Variable([0.1, 0.1])
      v3 = variables.Variable([1], dtype=dtypes.int32)

      @polymorphic_function.function(jit_compile=True)
      def f4(a, b):
        return (a + b) * v1 - math_ops.cast(v3, dtypes.float32)

      a = constant_op.constant([1.1, 1.1])
      b = constant_op.constant([2.2, 2.2])

      kwargs = {'b': a, 'a': b}
      self._compareTwoMethodsCompilerIROutput(f4, [], kwargs)

  def test_capture_variable_2(self):
    if 'gpu' in self.device.lower():
      self.skipTest('Skip test on GPU')

    with ops.device('device:{}:0'.format(self.device)):
      v2 = variables.Variable(2.0, dtype=dtypes.float32)
      v3 = variables.Variable(3.0, dtype=dtypes.float32)

      @polymorphic_function.function(jit_compile=True)
      def fun_tf(x):
        # Defining tf.constants inside func_body is okay.
        t4 = constant_op.constant(4.0, dtype=dtypes.float32)
        t5 = constant_op.constant(5.0, dtype=dtypes.float32)
        return (x * v3 + t4 + v2) * v3 + t5

      x = constant_op.constant(2.0, dtype=dtypes.float32)
      self._compareTwoMethodsCompilerIROutput(fun_tf, [x], {})

  def test_capture_constants(self):
    if 'gpu' in self.device.lower():
      self.skipTest('Skip test on GPU')

    with ops.device('device:{}:0'.format(self.device)):
      v2 = variables.Variable(2.0, dtype=dtypes.float32)
      v3 = variables.Variable(3.0, dtype=dtypes.float32)
      t4 = constant_op.constant([4.0, 5.0], dtype=dtypes.float32)
      t5 = constant_op.constant([5.0, 6.0], dtype=dtypes.float32)

      @polymorphic_function.function(jit_compile=True)
      def fun_tf(x):
        return (x * v3 + t4 + v2) * v3 + t5

      x = constant_op.constant([2.0, 3.0], dtype=dtypes.float32)
      self._compareTwoMethodsCompilerIROutput(fun_tf, [x], {})

  def test_from_concrete_function_with_args(self):
    with ops.device('device:{}:0'.format(self.device)):
      v2 = variables.Variable(2.0, dtype=dtypes.float32)
      v3 = variables.Variable(3.0, dtype=dtypes.float32)
      # Capturing tf.constants outside func_body is not okay.
      t4 = constant_op.constant(4.0, dtype=dtypes.float32)
      t5 = constant_op.constant(5.0, dtype=dtypes.float32)

      @polymorphic_function.function(jit_compile=True)
      def fun_tf(x):
        return (x * v3 + t4 + v2) * v3 + t5

      concrete_fn = fun_tf.get_concrete_function(
          tensor.TensorSpec((None,), dtype=dtypes.float32)
      )

      x = tensor.TensorSpec((10,), dtype=dtypes.float32)
      hlo_1 = compiler_ir.from_concrete_function(concrete_fn, [x])(stage='hlo')
      self.assertIn('f32[10]', hlo_1)
      x = tensor.TensorSpec((20,), dtype=dtypes.float32)
      hlo_2 = compiler_ir.from_concrete_function(concrete_fn, [x])(stage='hlo')
      self.assertIn('f32[20]', hlo_2)

if __name__ == '__main__':
  ops.enable_eager_execution()
  test.main()


eps = 0.00001
x = 169

xn = x / 2
n = 0
while True:
    n += 1
    xn = 0.5 * (xn + x / xn)
    if abs(xn ** 2 - x) < eps:
        break

print(xn, n)
print(x ** 0.5)


import argparse
import os
import shutil
import tempfile
import unittest
from contextlib import contextmanager
from pathlib import Path
from typing import Generator
from unittest.mock import MagicMock, patch

from torchx.cli.cmd_run import CmdBuiltins, CmdRun, _parse_run_config

@contextmanager
def cwd(path: str) -> Generator[None, None, None]:
    orig_cwd = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(orig_cwd)

class CmdRunTest(unittest.TestCase):
    def setUp(self) -> None:
        self.tmpdir = Path(tempfile.mkdtemp())
        self.parser = argparse.ArgumentParser()
        self.cmd_run = CmdRun()
        self.cmd_run.add_arguments(self.parser)

    def tearDown(self) -> None:
        shutil.rmtree(self.tmpdir)

    def test_run_with_user_conf_abs_path(self) -> None:
        args = self.parser.parse_args(
            [
                "--scheduler",
                "local",
                str(Path(__file__).parent / "components.py:touch"),
                "--file",
                str(self.tmpdir / "foobar.txt"),
            ]
        )
        self.cmd_run.run(args)
        self.assertTrue(os.path.isfile(str(self.tmpdir / "foobar.txt.test")))

    def test_run_with_relpath(self) -> None:
        # should pick up test/examples/touch.torchx (not the builtin)
        with cwd(str(Path(__file__).parent)):
            args = self.parser.parse_args(
                [
                    "--scheduler",
                    "local",
                    str(Path(__file__).parent / "components.py:touch_v2"),
                    "--file",
                    str(self.tmpdir / "foobar.txt"),
                ]
            )

            self.cmd_run.run(args)
            self.assertTrue(os.path.isfile(str(self.tmpdir / "foobar.txt.testv2")))

    def test_run_missing(self) -> None:
        with self.assertRaises(ValueError):
            args = self.parser.parse_args(
                [
                    "--scheduler",
                    "local",
                    "1234_does_not_exist.torchx",
                ]
            )
            self.cmd_run.run(args)

    @patch("torchx.runner.Runner.run")
    def test_run_dryrun(self, mock_runner_run: MagicMock) -> None:
        args = self.parser.parse_args(
            [
                "--dryrun",
                "--scheduler",
                "local",
                "utils.echo",
            ]
        )
        self.cmd_run.run(args)
        mock_runner_run.assert_not_called()

    def test_parse_run_config(self) -> None:
        args = "key=value,foo=bar"
        cfg = _parse_run_config(args)
        self.assertEqual("value", cfg.get("key"))
        self.assertEqual("bar", cfg.get("foo"))

class CmdBuiltinTest(unittest.TestCase):
    def test_run(self) -> None:
        # nothing to assert, just make sure the supported schedulers print runopts

        parser = argparse.ArgumentParser()
        cmd_builtins = CmdBuiltins()
        cmd_builtins.add_arguments(parser)
        args = parser.parse_args([])
        cmd_builtins.run(args)

    def test_builtins(self) -> None:
        cmd_builtins = CmdBuiltins()
        builtins = cmd_builtins._builtins()
        # make sure there's at least one
        # there will always be one (example.torchx)
        self.assertTrue(len(builtins) > 0)


#!/usr/bin/python

# Hector Castillo

# Adapted from:
# Andrew Morgan
# Yale University

# import continuum_node.syncRW_XMHandler as handler
import continuum.registerDict as registerDict
import continuum.continuum as continuum
from continuum.srv import *
from geometry_msgs.msg import PointStamped
from std_msgs.msg import Float32
import rospy
import numpy as np
from IPython import embed
import thread

#Communication class for continuum
class ContinuumNode():
    robot = None

    #Initialize to have a hand object
    def __init__(self, robot):
        self.robot = robot
        self.actual_cartesian = np.zeros((3,1))
        self.rho_actual = 0.0
        self.theta_actual = 0.0
        self.dq = np.zeros((3,1))

        self.rho_setpoint = 0.0
        self.theta_setpoint = 0.0
        self.K = 0.25
        self.err_acpt_r = 0.01
        self.err_acpt_t = 0.063
        self.openloop = False

        #initialize service handlers:
        rospy.Service('/continuum_node/reset',Reset,self.ResetCallback)
        rospy.Service('/continuum_node/goto',GoTo,self.GoToCallback)
        rospy.Service('/continuum_node/motor_states',MotorStates,self.MotorStatesCallback)

        rospy.Subscriber('/continuum_end_point',PointStamped,self.ContinuumEndCallback)

        rate = rospy.Rate(10) # 10hz
        setpoint_cartesian_pub = rospy.Publisher('/continuum_node/setpoint_cartesian', PointStamped, queue_size=10)
        setpoint_config_rho_pub = rospy.Publisher('/continuum_node/setpoint_config/rho', Float32, queue_size=10)
        setpoint_config_theta_pub = rospy.Publisher('/continuum_node/setpoint_config/theta', Float32, queue_size=10)
        rho_actual_pub = rospy.Publisher('/continuum_node/rho_actual', Float32, queue_size=10)
        theta_actual_pub = rospy.Publisher('/continuum_node/theta_actual', Float32, queue_size=10)

        position_stamped = PointStamped()
        position_stamped.header.frame_id = 'continuum_base_frame'

        rho_setpoint_msg = Float32()
        theta_setpoint_msg = Float32()
        rho_actual_msg = Float32()
        theta_actual_msg = Float32()

        # start motor control thread
        thread.start_new_thread(self.MotorControl,())

        while not rospy.is_shutdown():
            position_stamped.header.stamp = rospy.Time.now()
            x,y,z = self.robot.getSetpointCartesian()
            position_stamped.point.x = x
            position_stamped.point.y = y
            position_stamped.point.z = z
            setpoint_cartesian_pub.publish(position_stamped)

            rho_setpoint_msg = self.rho_setpoint
            theta_setpoint_msg = self.theta_setpoint
            setpoint_config_rho_pub.publish(rho_setpoint_msg)
            setpoint_config_theta_pub.publish(theta_setpoint_msg)

            rho_actual_msg = self.rho_actual
            theta_actual_msg = self.theta_actual
            rho_actual_pub.publish(rho_actual_msg)
            theta_actual_pub.publish(theta_actual_msg)

            rate.sleep()

        rospy.spin()		#blocking

    def MotorControl(self):
        while not rospy.is_shutdown():
            if not self.openloop:
                err_rho   = self.rho_setpoint   - self.rho_actual
                err_theta = self.theta_setpoint - self.theta_actual
                if err_theta > np.pi:
                    err_theta -= 2*np.pi
                elif err_theta < -np.pi:
                    err_theta += 2*np.pi

                J = self.robot.getJacobian(self.rho_actual, self.theta_actual)
                self.dq = np.dot(J,np.resize(np.array([err_rho,err_theta]),(2,1)))
                self.robot.moveMotors([0,1,2],np.clip(self.robot.getMotorStates()+self.K*self.dq,0.0,1.0),printout=False)

    def ContinuumEndCallback(self, data):

        self.actual_cartesian[0,0] = data.point.x
        self.actual_cartesian[1,0] = data.point.y
        self.actual_cartesian[2,0] = data.point.z

        self.rho_actual, self.theta_actual = self.robot.XYZ2RT(self.actual_cartesian)
        self.theta_actual = np.mod(self.theta_actual,2*np.pi)

    #This should work in theory, but dynamixels have a mind of their own.
    def ResetCallback(self,req):
        self.robot.reset()
        resp = ResetResponse()
        resp.err = 0
        return resp

    def GoToCallback(self,req):
        self.rho_setpoint = req.rho
        self.theta_setpoint = req.theta
        self.speed = req.speed
        self.K = req.K
        self.openloop = req.open

        if self.openloop:
            # run once
            self.robot.goto(self.rho_setpoint, self.theta_setpoint, self.speed, self.K, (self.err_acpt_r, self.err_acpt_t))

        resp = GoToResponse()
        resp.err = 0
        return resp

    def MotorStatesCallback(self, req):

        resp = MotorStatesResponse()
        resp.states = self.robot.getMotorStates()
        resp.err = 0

        return resp

if __name__=="__main__":
    Robot = None
    #initialize ros node elements: (can't read parameters until node is initialized)
    rospy.init_node("continuumNode")

    port = rospy.get_param('~servo_port')
    motor_assignments = rospy.get_param('~motor_assignments')
    dyn_model = rospy.get_param('~dyn_model')
    natural_length = rospy.get_param('~natural_length')
    run_tuning = rospy.get_param('~run_tuning')

    Robot = continuum.Continuum(port=port, motor_assignments=motor_assignments, dyn_model=dyn_model, naturalLength=natural_length, runTuning = run_tuning)

    #start node if everything is initialized correctly
    if Robot is None:
        rospy.logerr("ERR: Cannot Initialize the continuum node (continuumNode.py). Please check to ensure your parameters are correct!")
    else:
        ContinuumNode(Robot)


"""Module to create Effective zoning configuration summary statistics"""

import numpy as np
import pandas as pd
# from common_operations_dataframe import count_frequency, find_mean_max_min
from .zoning_statistics_aux_fn import active_vs_configured_ports, merge_df

import utilities.dataframe_operations as dfop
# import utilities.database_operations as dbop
# import utilities.data_structure_operations as dsop
# import utilities.module_execution as meop
# import utilities.servicefile_operations as sfop
# import utilities.filesystem_operations as fsop

def cfg_dashborad(zonemember_statistics_df, portshow_zoned_aggregated_df, zoning_aggregated_df, alias_aggregated_df):
    """Main function to count effective zoning configuration statistics."""

    def split_statistics(zonemember_statistics_df):
        """Function to split up zonemember_statistics_df into three DataFrames:
        zonelevel statistics for effective zones only,
        zonelevel statitics for effective + defined zones,
        effective config summary statistics."""

        mask_zone_effective = zonemember_statistics_df['cfg_type'] == 'effective'
        mask_cfg_summary = zonemember_statistics_df['zone'].isna()
        # DataFrame with information for each zone (zonelevel) effective and effective+defined
        zonelevel_statistics_effective_df = zonemember_statistics_df.loc[mask_zone_effective & ~mask_cfg_summary].copy()
        zonelevel_statistics_df = zonemember_statistics_df.loc[~mask_cfg_summary].copy()
        for column in ['zone_duplicated', 'zone_paired', 'zone_absorber']:
            if not column in zonelevel_statistics_df.columns:
                zonelevel_statistics_effective_df[column] = np.nan
        # add zone_duplicated_tag
        mask_not_duplicated_zone = zonelevel_statistics_effective_df['zone_duplicated'].isna()
        zonelevel_statistics_effective_df['zone_duplicated'] = \
            zonelevel_statistics_effective_df['zone_duplicated'].where(mask_not_duplicated_zone, 'zone_duplicated_tag')
        # add zone_paired tag
        mask_not_paired_zone = zonelevel_statistics_effective_df['zone_paired'].isna()
        zonelevel_statistics_effective_df['zone_paired'] = \
            zonelevel_statistics_effective_df['zone_paired'].where(mask_not_paired_zone, 'zone_paired_tag')
        # add zone_absorbed tag
        mask_not_absorbed_zone = zonelevel_statistics_effective_df['zone_absorber'].isna()
        zonelevel_statistics_effective_df['zone_absorber'] = \
            zonelevel_statistics_effective_df['zone_absorber'].where(mask_not_absorbed_zone, 'zone_absorbed_tag')

        # DataFrame with summary for all zones in Fabric (cfg level)
        cfglevel_statistics_effective_df = zonemember_statistics_df.loc[mask_zone_effective & mask_cfg_summary].copy()
        return zonelevel_statistics_effective_df, zonelevel_statistics_df, cfglevel_statistics_effective_df

    def count_zone_type(cfglevel_statistics_effective_df):
        """Count quantity of each zone type (all zones, lsan and peer zones)"""

        zone_type_columns = ['Fabric_name', 'Fabric_label', 'zone_tag', 'qos_tag', 'lsan_tag', 'property', 'tdz_tag']
        zone_type_columns = [column for column in zone_type_columns if column in cfglevel_statistics_effective_df.columns]
        zone_type_summary_df = cfglevel_statistics_effective_df.loc[:, zone_type_columns].copy()
        zone_type_summary_df.set_index(['Fabric_name', 'Fabric_label'], inplace=True)
        zone_type_summary_df.fillna(0, inplace=True)
        # summarize all zone types
        zone_type_summary_df.loc[('All', ''), :] = zone_type_summary_df.sum(numeric_only=True, axis=0)
        zone_type_summary_df.reset_index(inplace=True)
        return zone_type_summary_df

    def count_zone_note(zonelevel_statistics_effective_df):
        """Function to count quantity of zones with each type of note:
        no_target, no_initiator, wwnn_zones, wwnp_duplicated_zones, etc"""

        for column in ['Wwnn', 'Wwnp_duplicated']:
            if not column in zonelevel_statistics_effective_df.columns:
                zonelevel_statistics_effective_df[column] = 0
        zonelevel_statistics_effective_df['zone_Wwnn_tag'] = \
            np.where(zonelevel_statistics_effective_df['Wwnn'] > 0, 'zone_Wwnn_tag', pd.NA)
        zonelevel_statistics_effective_df['zone_Wwnp_duplicated_tag'] = \
            np.where(zonelevel_statistics_effective_df['Wwnp_duplicated'] > 0, 'zone_Wwnp_duplicated_tag', pd.NA)

        # add column with tags (correct_zone, commented_zone, non_working_zone)
        for column in ['Target_Initiator_note', 'Target_model_note']:
            if not column in zonelevel_statistics_effective_df.columns:
                zonelevel_statistics_effective_df[column] = np.nan
        for column in ['absent', 'remote_na']:
            if not column in zonelevel_statistics_effective_df.columns:
                zonelevel_statistics_effective_df[column] = 0

        # add no_alias_zone tag
        for column in ['tdz_tag', 'zonemember_wwn', 'zonemember_domain_portindex']:
            if not column in  zonelevel_statistics_effective_df:
                zonelevel_statistics_effective_df[column] = 0
        mask_tdz = zonelevel_statistics_effective_df['tdz_tag'] != 0
        mask_zonemember_wwn = zonelevel_statistics_effective_df['zonemember_wwn'] != 0
        mask_zonemember_di = zonelevel_statistics_effective_df['zonemember_domain_portindex'] != 0
        zonelevel_statistics_effective_df['zone_no_alias_tag'] = \
            np.select([~mask_tdz & (mask_zonemember_wwn | mask_zonemember_di),
                        mask_tdz & mask_zonemember_di], ['zone_no_alias_tag', 'zone_no_alias_tag'],
                        default=pd.NA)

        # add zone_absent_zonemember_tag
        absent_columns = ['remote_na', 'remote_initializing', 'absent', 'remote_configured']
        absent_columns = [column for column in absent_columns if column in zonelevel_statistics_effective_df.columns]
        if absent_columns:
            mask_zone_absent_zonemember = (zonelevel_statistics_effective_df[absent_columns] > 0).any(axis=1)
            zonelevel_statistics_effective_df.loc[mask_zone_absent_zonemember, 'zone_absent_zonemember_tag'] = 'zone_absent_zonemember_tag'
        else:
            zonelevel_statistics_effective_df['zone_absent_zonemember_tag'] = np.nan

        # non_working zones
        non_working_zone_tags = ['no_initiator', 'no_target', 'no_target, no_initiator', 'no_target, several_initiators']
        mask_non_working_zone = zonelevel_statistics_effective_df['Target_Initiator_note'].isin(non_working_zone_tags)
        # commented zones
        mask_several_initiators = zonelevel_statistics_effective_df['Target_Initiator_note'] == 'several_initiators'
        storage_models_tags = ['different_storages', 'storage_library']
        mask_storage_models = zonelevel_statistics_effective_df['Target_model_note'].isin(storage_models_tags)
        mask_wwnn = zonelevel_statistics_effective_df['zone_Wwnn_tag'] == 'zone_Wwnn_tag'
        mask_wwnp_duplicated = zonelevel_statistics_effective_df['zone_Wwnp_duplicated_tag'] == 'zone_Wwnp_duplicated_tag'
        # mask_absent_remote = (zonelevel_statistics_effective_df[['absent', 'remote_na']] > 0).any(axis=1)
        mask_missing_zonemember = zonelevel_statistics_effective_df['zone_absent_zonemember_tag'].notna()

        mask_no_alias = zonelevel_statistics_effective_df['zone_no_alias_tag'].notna()
        mask_mixed_zone = zonelevel_statistics_effective_df['Mixed_zone_note'].notna()
        mask_commented_zone = (mask_several_initiators | mask_storage_models | mask_wwnn | mask_wwnp_duplicated | \
                                mask_missing_zonemember | mask_no_alias | mask_mixed_zone) & ~mask_non_working_zone
        # zones verification
        zonelevel_statistics_effective_df['zone_note_summary'] = np.select(
            [mask_non_working_zone, mask_commented_zone],
            ['non-working_zone', 'commented_zone'], default='correct_zone')

        zone_notes_summary_df = \
            dfop.count_frequency(zonelevel_statistics_effective_df, \
                count_columns=['zone_duplicated', 'zone_absorber', 'zone_paired',
                                'Pair_zone_note', 'Target_Initiator_note', 'Target_model_note', 'Mixed_zone_note',
                                    'zone_Wwnn_tag', 'zone_Wwnp_duplicated_tag', 'zone_no_alias_tag', 'zone_absent_zonemember_tag','zone_note_summary'])
        return zone_notes_summary_df

    def count_unused_zone(zonelevel_statistics_df):
        """Function to add unused zones counter
        (zones wich are not part of active (effective) configuaration)"""

        # remove duplicated zones
        zonelevel_statistics_duplicates_free_df = \
            zonelevel_statistics_df.drop_duplicates(subset=['Fabric_name', 'Fabric_label', 'zone']).copy()
        zone_unused_summary = dfop.count_frequency(zonelevel_statistics_duplicates_free_df, count_columns=['Effective_cfg_usage_note'])
        return zone_unused_summary

    def count_device_port_status(zoning_aggregated_df):
        """"Function to count device ports in zoning configuration for each ports state:
        local, remote_imported, remote_na, remote_initializing, absent, remote_configured"""

        port_status_columns = ['Fabric_name', 'Fabric_label', 'alias_member', 'PortName', 'Fabric_device_status']
        mask_effective = zoning_aggregated_df['cfg_type'] =='effective'
        zoned_ports_status_df = zoning_aggregated_df.loc[mask_effective, port_status_columns].copy()
        # drop duplicated alias memebers
        zoned_ports_status_df.drop_duplicates(subset=port_status_columns, inplace=True)
        # each PortWwn (device port) is counted only once. Nan values are not considered as duplicated
        zoned_ports_status_df = \
            zoned_ports_status_df[(~zoned_ports_status_df[['Fabric_name', 'Fabric_label', 'PortName']].duplicated()) | zoned_ports_status_df['PortName'].isna()]

        zoned_ports_status_summary_df = dfop.count_frequency(zoned_ports_status_df, count_columns=['Fabric_device_status'])
        return zoned_ports_status_summary_df

    def count_device_type(zoning_aggregated_df):
        """"Function to count device types in zoning configuration (SRV, STORAGE, LIB etc)"""

        device_type_columns = ['Fabric_name', 'Fabric_label', 'PortName', 'deviceType']
        mask_effective = zoning_aggregated_df['cfg_type'] =='effective'
        zoned_device_type_df = zoning_aggregated_df.loc[mask_effective, device_type_columns].copy()
        # drop duplicated device_type
        zoned_device_type_df.drop_duplicates(subset=device_type_columns, inplace=True)

        zoned_device_type_summary_df = dfop.count_frequency(zoned_device_type_df, count_columns=['deviceType'])
        return zoned_device_type_summary_df

    def count_total_zoned_ports(zoning_aggregated_df):
        """Function to count total original ports in effective zone configuration including
        wwn or di which are absent"""

        mask_effective = zoning_aggregated_df['cfg_type'] =='effective'
        zoned_ports_total_df = zoning_aggregated_df.loc[mask_effective].copy()
        zoned_ports_total_df['alias_member_PortName'] = zoned_ports_total_df['PortName']
        zoned_ports_total_df['alias_member_PortName'].fillna(zoned_ports_total_df['alias_member'], inplace=True)

        zoned_ports_total_df.drop_duplicates(subset=['Fabric_name', 'Fabric_label', 'alias_member_PortName'], inplace=True)
        zoned_ports_total_df['Total_zoned_ports'] = 'Total_zoned_ports'

        zoned_ports_total_summary_df = dfop.count_frequency(zoned_ports_total_df, count_columns=['Total_zoned_ports'])
        return zoned_ports_total_summary_df

    def count_zone_alias_port_statistics(zonelevel_statistics_effective_df):
        """Function to count mean, max, min values for alias qunatity and active ports quantity in zones for each Fabric"""

        # zonemember might be presented as alias, Wwnp or Domain Index
        alias_count_columns = ['zonemember_alias', 'zonemember_wwn', 'zonemember_domain_portindex']
        alias_count_columns = [column for column in alias_count_columns if column in zonelevel_statistics_effective_df.columns]
        # sum all "aliases" for each zone
        zonelevel_statistics_effective_df['Total_alias'] = \
            zonelevel_statistics_effective_df[alias_count_columns].sum(axis=1)
        # count statistics for each Fabric
        count_columns_dct = {'Total_alias': 'alias_per_zone', 'Total_zonemembers_active': 'active_ports_per_zone'}
        alias_vs_active_ports_per_zone_df = \
            dfop.find_mean_max_min(zonelevel_statistics_effective_df, count_columns = count_columns_dct)
        return alias_vs_active_ports_per_zone_df

    def count_alias_port_zone_statistics(alias_aggregated_df):
        """Function to count mean, max, min values for defined ports, active ports number and
        number zones alias applied in aliases for each Fabric in effective cfg"""

        mask_alias_effective = alias_aggregated_df['cfg_type'] == 'effective'
        alias_effective_df = alias_aggregated_df.loc[mask_alias_effective].copy()
        # defined and active ports number for each zonemember (alias) were calculated in advance
        # thus each alias should be counted only once
        alias_effective_df.drop_duplicates(subset = ['Fabric_name', 'Fabric_label', 'zone_member'], inplace=True)
        count_columns_lst = ['ports_per_alias', 'active_ports_per_alias', 'zone_number_alias_used_in']
        count_columns_dct = {k:k for k in count_columns_lst}
        alias_ports_vs_zone_usage_df = dfop.find_mean_max_min(alias_effective_df, count_columns=count_columns_dct)
        return alias_ports_vs_zone_usage_df

    # split zonemember_statistics_df DataFrame
    zonelevel_statistics_effective_df, zonelevel_statistics_df, cfglevel_statistics_effective_df = \
            split_statistics(zonemember_statistics_df)
    # count values frequencies in splitted statistics DataFrames
    zone_type_summary_df = count_zone_type(cfglevel_statistics_effective_df)
    zone_notes_summary_df = count_zone_note(zonelevel_statistics_effective_df)
    zone_unused_summary = count_unused_zone(zonelevel_statistics_df)
    zoned_ports_total_summary_df = count_total_zoned_ports(zoning_aggregated_df)
    zoned_ports_status_summary_df = count_device_port_status(zoning_aggregated_df)
    zoned_device_type_summary_df = count_device_type(zoning_aggregated_df)
    # count active and unzoned device ports in Fabric
    zoned_vs_total_ports_summary_df = \
        active_vs_configured_ports(portshow_zoned_aggregated_df, configuration_type='cfg_effective')
    alias_vs_active_ports_per_zone_df = count_zone_alias_port_statistics(zonelevel_statistics_effective_df)
    alias_ports_vs_zone_usage_df = count_alias_port_zone_statistics(alias_aggregated_df)

    # merge all summary DataFrames into one
    df_lst = [zone_type_summary_df, zone_notes_summary_df, zone_unused_summary,
                zoned_ports_total_summary_df, zoned_ports_status_summary_df, zoned_device_type_summary_df,
                    zoned_vs_total_ports_summary_df, alias_vs_active_ports_per_zone_df, alias_ports_vs_zone_usage_df]
    active_cfg_statistics_df = merge_df(df_lst, fillna_index=4)

    return active_cfg_statistics_df


import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from argparse import ArgumentParser

def parse_args_for_settings_file():
    """Parse the single argument (the settings JSON file) for scripts that
    require it.
    """

    parser = ArgumentParser(description="Script with JSON Settings File")
    parser.add_argument("file_name", help="The JSON settings file.")
    args = parser.parse_args()

    return args.file_name

# Note: Lower AFD is better
def compute_feature_distance(image_ref: np.ndarray, image_curr: np.ndarray) -> float:
    # Code adapted from OpenCV documentation example
    # https://docs.opencv.org/master/da/de9/tutorial_py_epipolar_geometry.html
    sift = cv.SIFT_create()
    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(image_ref, None)
    kp2, des2 = sift.detectAndCompute(image_curr, None)
    # FLANN parameters for nearest neighbor search
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)
    good = []
    pts1 = []
    pts2 = []

    # Ratio test as per Lowe's paper
    for i, (m, n) in enumerate(matches):
        if m.distance < 0.70 * n.distance:
            good.append(m)
            pts2.append(kp2[m.trainIdx].pt)
            pts1.append(kp1[m.queryIdx].pt)

    pts1 = np.int32(pts1)
    pts2 = np.int32(pts2)

    # The findHomography function implicitly uses RANSAC to correct matches based on
    # the backprojection error. Thus, use the mask returned from this function
    # to filter down the matches and make the method 'robust'
    _, mask = cv.findHomography(pts1, pts2, cv.RANSAC)
    mask = mask.astype("bool")
    mask = mask.squeeze(1)
    pts1 = pts1[mask]
    pts2 = pts2[mask]

    # Compute AFD based on the formula using these filtered match points
    running_sum = 0.0
    for i in range(len(pts1)):
        running_sum += np.linalg.norm(pts1[i] - pts2[i])

    afd = running_sum / len(pts1)

    return afd, pts1, pts2

def is_rotation_matrix(R: np.ndarray) -> bool:
    # Check for the correct type and shape.
    if not (isinstance(R, np.ndarray) and R.shape == (3, 3) and R.dtype == np.float64):
        return False

    # Check that it's a rotation matrix.
    return np.allclose(np.eye(3), R @ R.T, atol=1e-2) and np.allclose(
        np.linalg.det(R), 1, atol=1e-2
    )

def is_translation_vector(t: np.ndarray) -> bool:
    return isinstance(t, np.ndarray) and t.shape == (3,) and t.dtype == np.float64

def is_image(image: np.ndarray) -> bool:
    return (
        isinstance(image, np.ndarray)
        and image.dtype == np.uint8
        and image.shape[2] == 3
    )

def convert_angles_to_matrix(
    x_angle: float, y_angle: float, z_angle: float
) -> np.ndarray:
    # Takes angles in degrees of a rotation about x, y and z axes respectively
    # and returns a matrix!
    x_angle = np.deg2rad(x_angle)
    y_angle = np.deg2rad(y_angle)
    z_angle = np.deg2rad(z_angle)

    x_matrix = np.array(
        [
            [1.0, 0.0, 0.0],
            [0.0, np.cos(x_angle), -np.sin(x_angle)],
            [0.0, np.sin(x_angle), np.cos(x_angle)],
        ]
    )

    y_matrix = np.array(
        [
            [np.cos(y_angle), 0.0, np.sin(y_angle)],
            [0.0, 1.0, 0.0],
            [-np.sin(y_angle), 0.0, np.cos(y_angle)],
        ]
    )

    z_matrix = np.array(
        [
            [np.cos(z_angle), -np.sin(z_angle), 0.0],
            [np.sin(z_angle), np.cos(z_angle), 0.0],
            [0.0, 0.0, 1.0],
        ]
    )

    return np.matmul(z_matrix, np.matmul(y_matrix, x_matrix))

def make_rotation_matrix(location, target):
    """Make a rotation matrix for a camera at location that's pointing towards
    target. This assumes that up is (0, 0, -1) like it is in OpenCV.
    """
    y_up = np.array([0, 0, -1])
    camera_to_target = target - location
    z = camera_to_target
    z /= np.linalg.norm(z)
    x = np.cross(y_up, z)
    x /= np.linalg.norm(x)
    y = np.cross(z, x)
    y /= np.linalg.norm(y)
    return np.stack([x, y, z], axis=1)


from PyQt5.QtWidgets import*
from main_python import Ui_MainWindow
from liste import ListelePage

import sqlite3

#connection = sqlite3.connect("Ariza.db")

class ArizaPage(QMainWindow):
    baglanti = sqlite3.connect("Ariza.db")
    islem = baglanti.cursor()
    table = islem.execute("create table if not exists urun (musteriAd text, urunAdi text, tel int, adres text, arizaAciklama text, marka text, kategori text)")
    baglanti.commit()
    def __init__(self)-> None:
        super().__init__()
        self.arizaform =Ui_MainWindow()
        self.arizaform.setupUi(self)
        self.listeac = ListelePage()
        self.arizaform.btnekle.clicked.connect(self.KayitYap)
    def KayitYap(self):
        urun =self.arizaform.cmbUrun.currentText()
        marka =self.arizaform.cmbMarka.currentText()
        arizaAciklama =self.arizaform.lineEditArizaAciklama.text()
        print(urun," ",marka," ",arizaAciklama)
        self.hide ()
        self.listeac.show()


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

N = int(input())
edges = [0]*N

for i in range(N-1):
    a, b = map(int, input().split())
    edges[a-1] += 1
    edges[b-1] += 1

if max(edges) == N-1:
    print("Yes")
else:
    print("No")


from PyQt5 import QtWidgets, QtCore, QtGui

class SearchParameterWidget(QtWidgets.QWidget):
    removeSelfRequest = QtCore.pyqtSignal(QtCore.QVariant)
    searchParamFieldUpdate = QtCore.pyqtSignal((str, str, QtCore.QVariant))

    def __init__(self, parent=None):
        super(SearchParameterWidget, self).__init__(parent=parent)
        self.paramType = None # Will use to track which field to use when building SQL queries from these widgets
        self.parent = parent
        self.currentfield = ""
        self.setupWidget()

    def setupWidget(self):
        #HLayout to contain our widgets
        self.searchTemplateHLayout = QtWidgets.QHBoxLayout(self)
        self.searchTemplateHLayout.setObjectName("searchTemplateHLayout")

        #First widget is the dropdown to select which param to search with
        self.paramSelectionDropdown = QtWidgets.QComboBox(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.paramSelectionDropdown.sizePolicy().hasHeightForWidth())
        self.paramSelectionDropdown.setSizePolicy(sizePolicy)
        self.paramSelectionDropdown.setMinimumSize(QtCore.QSize(90, 0))
        self.paramSelectionDropdown.setObjectName("paramSelectionDropdown")
        self.paramSelectionDropdown.setEditable(False)
        self.paramSelectionDropdown.activated["QString"].connect(self.changedFieldType)
        self.searchTemplateHLayout.addWidget(self.paramSelectionDropdown)

        #AND/OR checkbox. Multiple values can be searched for in a single field by separating them with a semicolon
        #This checkbox controls whether we use AND or OR in our SQL query. OR'ing has been the default behavior.
        #With dialog searches we change the behavior to switch between exact and fuzzy searching
        self.andorCheckbox = QtWidgets.QCheckBox(self)
        if self.currentfield == "dialog":
            self.andorCheckbox.setToolTip("Checked = Fuzzy, inexact searching (slow)\nUnchecked = Exact match searching only")
        else:
            self.andorCheckbox.setToolTip("Checked = multiple entries for this field are AND'd together, unchecked they are OR'd together. ")
        self.searchTemplateHLayout.addWidget(self.andorCheckbox)

        #spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
        #self.searchTemplateHLayout.addItem(spacerItem2)

        #Text box entry for selected parameter. In the future this could be dynamically updated for the selected param
        # e.g. dropdown for genre, dual input for number range w/ runtime and year
        self.searchTemplateEntry = QtWidgets.QLineEdit(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.searchTemplateEntry.sizePolicy().hasHeightForWidth())
        self.searchTemplateEntry.setSizePolicy(sizePolicy)
        self.searchTemplateEntry.setMinimumSize(QtCore.QSize(0, 25))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.searchTemplateEntry.setFont(font)
        self.searchTemplateEntry.setClearButtonEnabled(True)
        self.searchTemplateEntry.setObjectName("searchTemplateEntry")
        self.searchTemplateHLayout.addWidget(self.searchTemplateEntry)

        #Delete button to remove param
        self.searchTemplateRemoveButton = QtWidgets.QPushButton(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.searchTemplateRemoveButton.sizePolicy().hasHeightForWidth())
        self.searchTemplateRemoveButton.setSizePolicy(sizePolicy)
        self.searchTemplateRemoveButton.setMinimumSize(QtCore.QSize(25, 25))
        self.searchTemplateRemoveButton.setMaximumSize(QtCore.QSize(25, 25))
        self.searchTemplateRemoveButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../icons/delete-icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.searchTemplateRemoveButton.setIcon(icon)
        self.searchTemplateRemoveButton.setIconSize(QtCore.QSize(16, 16))
        self.searchTemplateRemoveButton.setFlat(True)
        self.searchTemplateRemoveButton.setObjectName("searchTemplateRemoveButton")
        self.searchTemplateRemoveButton.clicked.connect(lambda: self.removeSelfRequest.emit(self))
        self.searchTemplateHLayout.addWidget(self.searchTemplateRemoveButton)

    def returnData(self):
        #Just simply access to our field value
        return self.searchTemplateEntry.text()

    def andorState(self):
        #return the AND/OR checkbox state
        return "AND" if self.andorCheckbox.isChecked() else "OR"

    def updateFieldList(self, updatedstringlist):
        #Preserve whatever selection we had before because our model will change the indices
        self.paramSelectionDropdown.clear()
        updatedstringlist[self.currentfield] = True
        for field, enabled in updatedstringlist.items():
            if enabled:
                self.paramSelectionDropdown.addItem(field)
        if self.currentfield == "":
            self.changedFieldType(self.paramSelectionDropdown.itemText(0))
        QtWidgets.QApplication.processEvents()
        self.paramSelectionDropdown.setCurrentText(self.currentfield)  # Will only change index if QComboBox.editable==False

    def changedFieldType(self, fieldname):
        self.searchParamFieldUpdate.emit(self.currentfield, fieldname, self)
        self.currentfield = fieldname


import json
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
from sklearn.metrics import roc_auc_score

from arguments import get_args
from utils import StatsTracker
from utils import load_model

from train_base import train_model

from graph import MolGraph
from models import GCN
import pdb

def main(args=None, quiet=False, splits=None, abs_output_dir=False):
    if args is None:
        args = get_args()

    train_stats = train_model(
        model_class=GCN, run_func=run_func, args=args, quiet=quiet,
        splits=splits, abs_output_dir=abs_output_dir)
    return train_stats

def run_func(model, optim, data_loader, data_type, args, write_path=None,
             quiet=False):
    is_train = data_type == 'train'
    if is_train:
        model.train()
    else:
        model.eval()

    # Keeps track of epoch stats by aggregating batch stats
    stats_tracker = StatsTracker()
    write_output = []
    all_preds, all_labels = [], []
    if args.n_labels > 1:
        for _ in range(args.n_labels):
            all_preds.append([])
            all_labels.append([])

    for _, batch_data in enumerate(tqdm(data_loader, disable=quiet)):
        if is_train:
            optim.zero_grad()
        if args.n_labels == 1:
            smiles_list, labels_list = batch_data
        else:
            smiles_list, labels_list, mask = batch_data
        n_data = len(smiles_list)

        labels = torch.tensor(labels_list, device=args.device).float()
        if args.n_labels == 1:
            labels = labels.unsqueeze(1)
        if args.n_labels > 1:
            mask = torch.tensor(mask, device=args.device).float()

        mol_graph = MolGraph(smiles_list)
        _, preds = model(mol_graph)

        if args.val_stat == 'rmse':
            loss = torch.mean((labels - preds) ** 2)
            stats_tracker.add_stat('mse', loss.item() * n_data, n_data)
        elif args.val_stat in ['auc', 'acc']:
            pred_probs = nn.Sigmoid()(preds)
            loss = nn.BCELoss()(input=pred_probs, target=labels)
            stats_tracker.add_stat('ce', loss.item() * n_data, n_data)

            # Aggregate predictions to compute Acc/AUC
            all_preds.append(pred_probs.detach().cpu().numpy())
            all_labels.append(np.array(labels_list))
        elif args.val_stat == 'mae':
            loss = torch.mean(torch.abs(labels - preds))
            stats_tracker.add_stat('mae', loss.item() * n_data, n_data)
        elif args.val_stat == 'multi_obj':
            total_loss = 0
            for i in range(args.n_labels):
                cur_preds = preds[:, i].unsqueeze(1)
                cur_labels = labels[:, i].unsqueeze(1)
                cur_mask = mask[:, i].unsqueeze(1)

                n_sum = torch.sum(cur_mask).item()
                if n_sum == 0:
                    continue

                all_preds[i].append(cur_preds[cur_mask.byte()].detach().cpu().numpy())
                all_labels[i].append(cur_labels[cur_mask.byte()].detach().cpu().numpy())
                task_stat = args.label_task_list[i]
                if task_stat == 'rmse':
                    loss = (cur_preds - cur_labels) ** 2
                    loss = torch.sum(loss * cur_mask)
                    total_loss += loss
                if task_stat in ['auc', 'acc']:
                    loss = nn.BCEWithLogitsLoss(reduction='none')(input=cur_preds, target=cur_labels)
                    loss = torch.sum(loss * cur_mask)
                    total_loss += loss
                else:
                    assert False
            loss = total_loss / n_data
            stats_tracker.add_stat('loss', loss.item() * n_data, n_data)
        else:
            assert False

        if write_path is not None and args.n_labels == 1:
            for smiles_idx, smiles in enumerate(smiles_list):
                label = labels[smiles_idx].item()
                pred = preds[smiles_idx].item()

                write_output.append({
                    'smiles': smiles,
                    'label': label,
                    'pred': pred})

        if is_train:
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
            optim.step()

    if args.n_labels == 1:
        if args.val_stat == 'rmse':
            mse = stats_tracker.get_stats()['mse']
            stats_tracker.add_stat('rmse', mse ** 0.5, 1)
        elif args.val_stat in ['auc', 'acc']:
            all_preds = np.squeeze(np.concatenate(all_preds), axis=1)
            all_labels = np.concatenate(all_labels)

            acc = np.mean((all_preds > 0.5) == all_labels)
            stats_tracker.add_stat('acc', -1 * acc, 1)

            auc = roc_auc_score(y_true=all_labels, y_score=all_preds)
            stats_tracker.add_stat('auc', -1 * auc, 1)
    else:
        all_stats = []
        for label_idx in range(args.n_labels):
            cur_preds = np.concatenate(all_preds[label_idx])
            cur_labels = np.concatenate(all_labels[label_idx])

            data_name = args.data.split('_')[label_idx]
            if args.label_task_list[i] == 'rmse':
                mse = np.mean((cur_preds - cur_labels) ** 2)
                stats_tracker.add_stat('%s_mse' % data_name, mse, 1)
                stats_tracker.add_stat('%s_rmse' % data_name, mse ** 0.5, 1)
            elif args.label_task_list[i] in ['auc', 'acc']:
                cur_pred_probs = 1 / (1 + np.exp(-cur_preds))
                acc = np.mean((cur_pred_probs > 0.5) == cur_labels)

                auc = roc_auc_score(y_true=cur_labels, y_score=cur_pred_probs)
                stats_tracker.add_stat('%s_acc' % data_name, acc, 1)
                stats_tracker.add_stat('%s_auc' % data_name, auc, 1)

                all_stats.append(-1 * auc)
            else:
                assert False
        stats_tracker.add_stat('multi_obj', np.mean(np.array(all_stats)), 1)

    if write_path is not None and args.n_labels == 1:
        with open(write_path, 'w+') as write_file:
            for write_ele in write_output:
                json.dump(write_ele, write_file)
                write_file.write('\n')

    return stats_tracker

if __name__ == '__main__':
    main()


from django.urls import reverse_lazy
from django.views.generic import ListView, CreateView, UpdateView

from score.forms import CreateRestaurantForm, CreateMenuForm, CreateEmployeeForm, VoteMenuForm
from score.models import Restaurant, Employee, Menu, Scores

class RestaurantListView(ListView):
    model = Restaurant
    template_name = 'restaurant/list.html'

class EmployeeListView(ListView):
    model = Employee
    template_name = 'employee/list.html'

class MenuListView(ListView):
    model = Menu
    template_name = 'menu/list.html'

class CreateRestaurantView(CreateView):
    model = Restaurant
    form_class = CreateRestaurantForm
    success_url = reverse_lazy('restaurant:list')
    template_name = 'restaurant/create.html'

class CreateMenuView(CreateView):
    model = Menu
    form_class = CreateMenuForm
    template_name = 'menu/create.html'
    success_url = reverse_lazy('menu:list')

    def get_initial(self):
        initial = super().get_initial()
        try:
            initial['restaurant_field'] = self.object.restaurant.pk
        except AttributeError:
            initial['restaurant_field'] = 0

        return initial

    def form_valid(self, form):
        response = super().form_valid(form)
        pk = int(form.cleaned_data['restaurant_field'])
        if pk:
            form.instance.restaurant = Restaurant.objects.get(pk=pk)
        else:
            form.instance.restaurant = None
        form.save()

        return response

class VoteMenuView(CreateView):
    model = Scores
    form_class = VoteMenuForm
    success_url = reverse_lazy('menu:list')
    template_name = 'menu/vote.html'

    # def get_initial(self):
    #     initial = super().get_initial()
    #     try:
    #         initial['vote_field'] = self.object.score.pk
    #     except AttributeError:
    #         initial['vote_field'] = 0
    #
    #     return initial
    #
    # def form_valid(self, form):
    #     response = super().form_valid(form)
    #     pk = int(form.cleaned_data['vote_field'])
    #     if pk:
    #         form.instance.score = Menu.objects.get(pk=pk)
    #     else:
    #         form.instance.score = None
    #     form.save()
    #
    #     return response

class CreateEmployeeView(CreateView):
    model = Employee
    form_class = CreateEmployeeForm
    template_name = 'employee/create.html'
    success_url = reverse_lazy('employee:list')


# SPDX-License-Identifier: MIT
# See LICENSE.md and CONTRIBUTORS.md at https://github.com/SSAGESLabs/PySAGES

"""
Umbrella Integration.

Umbrella integration uses multiple replicas placed along a pathway in the free energy
landscape by Harmonic Bias simulations.
From the statistics of the simulations, the thermodynamic forces along the path are
determined and integrated to obtain an approximation of the free energy landscape.
This class implements the replica simulations and approximates the free energy.
However, the method is not very accurate and it is preferred that more advanced methods
(e.g. the Weighted Histogram Analysis Method) are used for the analysis of the simulations.
"""

from copy import deepcopy

from pysages.methods.core import (
    ReplicaResult,
    Result,
    SamplingMethod,
    _run_replica,
    get_method,
)
from pysages.methods.harmonic_bias import HarmonicBias
from pysages.methods.utils import (
    HistogramLogger,
    SerialExecutor,
    listify,
    methods_dispatch,
    numpyfy_vals,
)
from pysages.typing import Callable, Optional, Union
from pysages.utils import dispatch

class UmbrellaIntegration(SamplingMethod):
    """
    This class combines harmonic biasing with multiple replicas.
    It also collects histograms of the collective variables throughout the simulations
    for subsequent analysis.
    By default the class also estimates an approximation of the free energy landscape
    along the given path via umbrella integration.
    Note that this is not very accurate and usually requires more sophisticated analysis on top.
    """

    submethods = []
    histograms = []

    @methods_dispatch
    def __init__(
        self,
        cvs,
        ksprings,
        centers,
        hist_periods: Union[list, int],
        hist_offsets: Union[list, int] = 0,
        **kwargs
    ):
        """
        Initialization, sets up the HarmonicBias subsamplers.

        Arguments
        ---------
        centers: list[numbers.Real]
            CV centers along the path of integration. Its length defines the number replicas.

        ksprings: Union[float, list[float]]
            Spring constants of the harmonic biasing potential for each replica.

        hist_periods: Union[int, list[int]]
            Indicates the period for logging the CV into the histogram of each replica.

        hist_offsets: Union[int, list[int]] = 0
            Offset before starting logging into each replica's histogram.
        """

        super().__init__(cvs, **kwargs)

        replicas = len(centers)
        ksprings = listify(ksprings, replicas, "ksprings", float)
        periods = listify(hist_periods, replicas, "hist_periods", int)
        offsets = listify(hist_offsets, replicas, "hist_offsets", int)

        self.submethods = [HarmonicBias(cvs, k, c) for (k, c) in zip(ksprings, centers)]
        self.histograms = [HistogramLogger(p, o) for (p, o) in zip(periods, offsets)]

    @methods_dispatch
    def __init__(  # noqa: F811 # pylint: disable=C0116,E0102
        self,
        biasers: list,
        hist_periods: Union[list, int],
        hist_offsets: Union[list, int] = 0,
        **kwargs
    ):
        cvs = None
        for bias in biasers:
            if cvs is None:
                cvs = bias.cvs
            else:
                if bias.cvs != cvs:
                    raise RuntimeError(
                        "Attempted run of UmbrellaSampling with different CVs"
                        " for the individual biaser."
                    )
        super().__init__(cvs, **kwargs)
        replicas = len(biasers)
        periods = listify(hist_periods, replicas, "hist_periods", int)
        offsets = listify(hist_offsets, replicas, "hist_offsets", int)

        self.submethods = biasers
        self.histograms = [HistogramLogger(p, o) for (p, o) in zip(periods, offsets)]

    def __getstate__(self):
        return (self.submethods, self.histograms)

    def __setstate__(self, state):
        biasers, histograms = state
        self.__init__(biasers, 1)
        self.histograms = histograms

    # We delegate the sampling work to HarmonicBias
    # (or possibly other methods in the future)
    def build(self):  # pylint: disable=arguments-differ
        pass

@dispatch(precedence=1)
def run(  # pylint: disable=arguments-differ
    method_or_result: Union[UmbrellaIntegration, Result[UmbrellaIntegration]],
    context_generator: Callable,
    timesteps: Union[int, float],
    context_args: dict = {},
    post_run_action: Optional[Callable] = None,
    executor=SerialExecutor(),
    executor_shutdown: bool = True,
    **kwargs
):
    # """
    # Implementation of the execution of umbrella integration with up to linear
    # order (ignoring second order terms with covariance matrix) as described in
    # J. Chem. Phys. 131, 034109 (2009); https://doi.org/10.1063/1.3175798 (equation 13).
    # Higher order approximations can be implemented by the user using the provided
    # covariance matrix.

    # Arguments
    # ---------
    # context_generator: Callable
    #     User defined function that sets up a simulation context with the backend.
    #     Must return an instance of `hoomd.conext.SimulationContext` for HOOMD-blue and
    #     `openmm.Context` for OpenMM.
    #     The function gets `context_args` unpacked for additional user args.
    #     For each replica along the path, the argument `replica_num` in [0, ..., N-1]
    #     is set in the `context_generator` to load the appropriate initial condition.

    # timesteps: int
    #     Number of timesteps the simulation is running.

    # context_args: dict = {}
    #     Arguments to pass down to `context_generator` to setup the simulation context.

    # kwargs:
    #     Passed to the backend run function as additional user arguments.

    # post_run_action: Optional[Callable] = None
    #     Callable function that enables actions after the run execution of PySAGES.
    #     Actions are executed inside the generated context.
    #     Example uses for this include writing a final configuration file.
    #     This function gets `context_args` unpacked just like `context_generator`.

    # **Note**: This method does not accept a user defined callback.
    # """
    method = get_method(method_or_result)
    timesteps = int(timesteps)

    def submit_work(executor, method_or_result, context_args, callback):
        return executor.submit(
            _run_replica,
            method_or_result,
            context_generator,
            timesteps,
            context_args,
            *callback,
            post_run_action,
            **kwargs
        )

    futures = []
    for n in range(len(method.submethods)):
        replica_context_args = deepcopy(context_args)
        replica_context_args["replica_num"] = n
        submethod_or_result, callback = _pack_args(method_or_result, n)
        futures.append(submit_work(executor, submethod_or_result, replica_context_args, callback))

    results = [future.result() for future in futures]
    states = [r.states for r in results]
    callbacks = [r.callbacks for r in results]
    snapshots = [r.snapshots for r in results]

    if executor_shutdown:
        executor.shutdown()

    return Result(method, states, callbacks, snapshots)

@dispatch
def _pack_args(method: UmbrellaIntegration, n: int):
    return method.submethods[n], (method.histograms[n],)

@dispatch
def _pack_args(  # noqa: F811 # pylint: disable=C0116,E0102
    result: Result[UmbrellaIntegration], n: int
):
    method = result.method
    submethod, callback = method.submethods[n], method.histograms[n]
    return ReplicaResult(submethod, result.states[n], callback, result.snapshots[n]), ()

@dispatch
def analyze(result: Result[UmbrellaIntegration]):
    """
    Computes the free energy from the result of an `UmbrellaIntegration` run.
    """

    def free_energy_gradient(k_spring, xi_mean, xi_ref):
        """
        Equation 13 from https://doi.org/10.1063/1.3175798
        """
        return -(k_spring @ (xi_mean - xi_ref))

    def integrate(A, nabla_A, centers, i):
        return A[i - 1] + nabla_A[i - 1].T @ (centers[i] - centers[i - 1])

    submethods = result.method.submethods

    ksprings = [s.kspring for s in submethods]
    centers = [s.center for s in submethods]
    hist_means = [cb.get_means() for cb in result.callbacks]

    submethods_iterator = zip(ksprings, centers, hist_means)
    mean_forces = []
    free_energy = [0.0]

    for i, (kspring, center, mean) in enumerate(submethods_iterator):
        mean_forces.append(free_energy_gradient(kspring, mean, center))
        if i > 0:
            free_energy.append(integrate(free_energy, mean_forces, centers, i))

    for callback in result.callbacks:
        callback.numpyfy()

    ana_result = dict(
        ksprings=ksprings,
        centers=centers,
        histograms=result.callbacks,
        histogram_means=hist_means,
        mean_forces=mean_forces,
        free_energy=free_energy,
    )
    return numpyfy_vals(ana_result)


from linkedListClassAndTests import *

def reverseList(head):
    if not head:
        return None
    ret = ListNode(head.val)
    while head.next:
        newHead = ListNode(head.next.val)
        newHead.next = ret
        ret = newHead
        head = head.next
    return ret

def main():
    test = initTest()
    if test:
        print(reverseList(test))
    tests = []
    if tests:
        for test in tests:
            print(test,'->',function(test))

if __name__ == "__main__":
    main()


from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import math
from Timba.TDL import *
from Timba.Meq import meq
from .PointSource import *
from . import Context

STOKES = ("I","Q","U","V");

class DiskSource(PointSource):
  def __init__(self,ns,name,direction,
               I=0.0,Q=None,U=None,V=None,
               spi=None,freq0=None,
               RM=None,
               size=None,order=0,symmetric=False):
    PointSource.__init__(self,ns,name,direction,I,Q,U,V,
                        spi,freq0,RM);
    self._symmetric = symmetric;
    #size=radius in radians
    # order=0, ring, order=1, disk
    self._order=order
    self._add_parm('sigma',size,tags='shape');

  def is_symmetric (self):
    return self._symmetric;

  def sigma (self):
    """Returns the size for this source,
    """
    return self._parm('sigma');

  def phi (self):
    """Returns the orientation node for this source""";
    return self._parm("phi");

  def transformation_matrix (self):
    pass

  def coherency (self,array=None,observation=None,nodes=None,**kw):
    coherency = nodes or self.ns.coh;
    array = Context.get_array(array);
    if not coherency(*array.ifrs()[0]).initialized():
      observation = Context.get_observation(observation);
      dir0 = observation.phase_centre;
      radec0 = dir0.radec();
      # 1/wl = freq/c
      iwl = self.ns.inv_wavelength << ((self.ns.freq<<Meq.Freq) / 2.99792458e+8);
      # 1/(wl^2): scaling factor applied to exp() argument below
      m_iwlsq = self.ns.m_inv_wavelength_sq << Meq.Sqr(iwl);
      # scaling factor of gaussian for unit flux
      gscale = self.ns.diskcomponent_scale << Meq.Constant(1.0);
      # baseline UVs
      uv_ifr = array.uv_ifr(dir0);
      # flux scale -- coherency multiplied by scale constant above
      fluxscale = self.ns.fluxscale << self.brightness(observation) * gscale;
      # transformed uv's (rotated and scaled)
      uv1sq = self.ns.uv1sq;
      u1sq = self.ns.u1sq;
      v1sq = self.ns.v1sq;
      sumuvsq = self.ns.sumuvsq;
      sigma = self._parm("sigma");
      # now generate nodes
      for ifr in array.ifrs():
        # rotate uvs and scale to wavelength
        uv1s = uv1sq(*ifr) << Meq.Sqr(uv_ifr(*ifr));
        u1s = u1sq(*ifr) << Meq.Selector(uv1s,index=0);
        v1s = v1sq(*ifr) << Meq.Selector(uv1s,index=1);
        rr  = sumuvsq(*ifr) <<Meq.Sqrt((u1s+v1s)*m_iwlsq);
        coherency(*ifr) << fluxscale * Meq.Bessel(rr*2*math.pi*sigma,order=self._order)/rr;
    return coherency;


import requests
from bs4 import BeautifulSoup
import smtplib
import text_to_speech as speech
import time
URL = "https://www.rokomari.com/book/202772/hate-kolome-javascript"

headers ={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" }

def  find_price():

    page =  requests.get(URL,headers=headers)

    soup = BeautifulSoup(page.content,'html.parser')

    price = soup.find("span", {"class": "sell-price"}).get_text()
    # print(soup.prettify())

    convertedprice = float(price[4:])

    if convertedprice > 300:
        speech.speak("Hey Alif you can buy this", "en")

    print(convertedprice)

while True:

    find_price()
    time.sleep(60*60)


import os
import numpy as np
from PIL import Image

path = './final/flower/normal picture/'

for item1 in os.listdir(path):
    # if item1 != 'apple':
    #     continue
    path1 = path + item1 + '/'
    t = 0
    print(t)
    for item2 in os.listdir(path1):
        path2 = path1 + item2
        # if os.path.getsize(path2) == 0:
        #     os.remove(path2)
        #     print(path2)
        # else:
        #     # img = Image.open(path2)
        #     # img_array = np.array(img)
        #     # if np.shape(img_array.shape)[0] != 3:
        #     #     os.remove(path2)
        #     #     print('p', path2)
        #     # else:
        os.rename(path2, path1+str(t)+'.jpg')
        t += 1


from flask_restful import Resource, reqparse
from flask_jwt import jwt_required, current_identity
from models.pages import PageModel
from models.site import SiteModel
from models.rank import RankModel
from datetime import datetime

class Pages(Resource):
    @jwt_required()
    def get(self, id=None, name=None):
        if id:
            stat = PageModel.find_by_id(id)
        else:
            stat = PageModel.find_by_name(name)
        if stat:
            return stat.json(current_identity.admin), 200
        return {'message': 'not found pages in this site'}, 404

class StatList(Resource):
    @jwt_required()
    def get(self):
        result = SiteModel.query.all()
        if result:
            return {
                'base statistic': [
                    PageModel.find_by_id(el.id).json(
                        current_identity.admin
                        ) for el in result if PageModel.find_by_id(el.id)]
                }, 200
        return {'message': 'not found base statistic'}, 404

class Rank(Resource):
    @jwt_required()
    def get(self, id=None, name=None):
        if id:
            stat = RankModel.find_by_id(id)
        else:
            stat = RankModel.find_by_name(name)
        if stat:
            return stat.json(), 200
        return {'message': 'not found rank in this site'}, 404

class RankList(Resource):
    @jwt_required()
    def get(self):
        result = SiteModel.query.all()
        if result:
            return {
                'base_rank statistic': [
                    RankModel.find_by_id(
                        el.id).json() for el in result if RankModel.find_by_id(
                            el.id)]
                }, 200
        return {'message': 'not found base_rank statistic'}, 404

class RankDay(Resource):
    #@jwt_required()
    def _pars_date(self, date):
        #pattern = r'\d{2}-\d{2}-\d{4}'
        #if re.match(pattern, date) is not None:
         #   return True
        #return False
        try:
            #date_str = "30-10-2016 16:18"
            #format_str = "%d-%m-%Y %H:%M"
            format_str = "%Y-%m-%d"
            return datetime.strptime(date, format_str)
        except ValueError:
            return False
    @jwt_required()
    def get(self, id=None, name=None, date=None):
        date = self._pars_date(date)
        if not date:
            return {'message': 'format of date is false ' + str(id)}, 404
        stat = 'None'
        if id and date:
            stat = RankModel.find_by_id_day(id, date)
        elif name and date:
            stat = RankModel.find_by_name_day(name, date)
        if stat:
            return stat.json_day(date), 200
        return {'message': 'not found pages in this site, day'}, 404

class RankDayList(Resource):
    @jwt_required()
    def get(self, date=None):
        result = PageModel.query.filter(PageModel.scan == date).first()
        if result:
            return {
                'base_rank_day statistic - ' + date: [
                    RankModel.find_by_id_day(
                        el.id, date).json_day(
                            date) for el in SiteModel.query.all(
                                ) if RankModel.find_by_id_day(
                                    el.id, date)]
                }, 200
        return {'message': 'not found base_rank_day statistic for this day'}, 404

class RankTime(Resource):
    def _pars_date(self, date):
        try:
            format_str = "%Y-%m-%d"
            return datetime.strptime(date, format_str)
        except ValueError:
            return False

    @jwt_required()
    def get(self, id=None, name=None, date1=None, date2=None):
        date1 = self._pars_date(date1)
        date2 = self._pars_date(date2)
        if not date1 or not date2:
            return {'message': 'format of date is false'}, 404
        stat = 'None'
        if id and date1 and date2:
            stat = RankModel.find_by_id_time(id, date1, date2)
        elif name and date1 and date2:
            stat = RankModel.find_by_name_time(name, date1, date2)
        if stat:
            return stat.json_time(date1, date2), 200
        return {'message': 'not found pages in this site, time'}, 404

class RankTimeList(Resource):
    @jwt_required()
    def get(self, date1=None, date2=None):
        result = PageModel.query.filter(
            PageModel.scan >= date1,
            PageModel.scan <= date2
            ).first()
        if result:
            return {
                'base_rank_time statistic - ' + date1 + '/' + date2: [
                    RankModel.find_by_id_time(
                        el.id, date1, date2
                        ).json_time(
                            date1, date2
                            ) for el in SiteModel.query.all() if RankModel.find_by_id_time(
                                el.id, date1, date2)]
                }, 200
        return {'message': 'not found base_rank_time statistic for this time'}, 404


import networkx as nx
import random
import functools
import operator
import numpy as np
import ast
from scipy import stats
from sklearn.neighbors import KernelDensity as KDE
from scipy import interpolate
import itertools
import pickle

def add_marriage_edges(all_fam, all_unions, D, marriage_probs, p, ncp, n, infdis):
    """Add marriage edges to a given generation
    Paramters:
                all_fam (list): list of all nodes in current generation--all eligible nodes for marrige.
                                (Flattened list of families)
                D (array): nxn matrix of distances between nodes
                marriage_probs (dict): dictionary of marriage distance probabilities.
                                        (e.g., value at key 5 is probability of distance of length 5)
                p (float): probability of marrying
                ncp (float): probability of a nonconnected marriage
                n (int): current number of nodes in network
                inf_dis (int): number of marriage pairs in data that have an infinite distance
    Returns:
                new_unions (list): list of edges that represent new unions between nodes
                no_unions (list): list of nodes with no unions
                all_unions (list): list of all unions since generation 0
                n (int): current number of nodes in network
                m (int): number of nonconnected nodes added to network

    """
    unions = []
    # number of non-connected people to add
    m = round(((ncp*len(all_fam))/(1-ncp))/2)
    infdis -= m
    # list of people not connected to family network
    nc_ppl = []
    for i in range(1, m+1):
        nc_ppl.append(n+i)
    # update number of people in network
    n = n + m
    # marry off non-connected people
    for nc in nc_ppl:
        spouse = random.choice(all_fam)
        unions.append((nc, spouse))
        all_fam.remove(spouse)

    # select how many of the n+m people get married based on probability of marriage
    k = round(((n+1)*p)/2)

    # get all possible pairs of rest of nodes
    all_pairs = list(itertools.combinations(all_fam, 2))

    # get all possible pairs of nodes that can marry
    poss_dis = {}
    for pair in all_pairs.copy():
        # get distance of pair
        dis = D[pair[0]][pair[1]]

        # check that pair can marry (i.e., siblings can't marry)
        if dis < min(marriage_probs):
            all_pairs.remove(pair)

        # keep track of distances of all possible nodes to marry
        else:
            if dis >= 100:
                poss_dis[pair] = 100  # all infinite distances are '100'
            else:
                poss_dis[pair] = dis

    while (len(all_pairs) > 0) and (len(unions) <= k):
        # find probabilities of all possible distances--must update after creating each union
        dis_probs = []
        for d in poss_dis.values():
            dis_probs.append(marriage_probs[d])

        # normalize probabilities--must update after creating each union
        rel_probs = list(np.array(dis_probs)/sum(dis_probs))  # relative probability of distances

        # choose pair to marry based on relative probability of distances
        marry = random.choices(population=all_pairs, weights=rel_probs)[0]
        unions.append(marry)

        # remove all pairs that include one of the nodes of 'marry'
        for pair in all_pairs.copy():
            if (pair[0] == marry[0]) or (pair[0] == marry[1]) or (pair[1] == marry[0]) or (pair[1] == marry[1]):
                all_pairs.remove(pair)
                poss_dis.pop(pair)

    # keep track of nodes that didn't marry
    no_unions = list(set(all_fam) - set(functools.reduce(operator.iconcat, unions, ())))
    all_unions += unions

    return unions, no_unions, all_unions, n, m, infdis

def add_children_edges(unions, n, child_probs, all_children):
    """Add children edges to a given generation of unions
    Parameters: unions (list): unions formed in current generation
                n (int): current number of nodes in graph
                child_probs (dict): dictionary of number of children probabilities
                                    (e.g., value at key 5 is probability of having 5 children)
    Returns: child_edges (list): list of new parent-child edges to add to graph
             families (list): list of lists where each inner list represents siblings
             n (int): new number of nodes in graph after adding children
             all_children (list): list of number of children per union since generation 0

    """

    families = []

    child_edges = []

    for union in unions:
        # how many children the union will have--based on children data distribution
        num = random.choices(population=list(child_probs.keys()), weights=list(child_probs.values()))[0]

        # add children
        if num != 0:
            # initialize list of children
            children = []
            # add children nodes to graph
            for c in range(num):
                # specify node number (label) to add
                n = n+1
                children.append(n)
                # add edges from child to parents
                child_edges.append((union[0], n))
                child_edges.append((union[1], n))
            # keep track of families
            families.append(children)

        # no children so add empty list
        else:
            families.append([])

        all_children.append(num)

    return child_edges, families, n, all_children

def update_distances(D, n, unions, no_unions, families):
    """Build a distance matrix that keeps track of how far away each node is from each other.
        Need to update distances after new nodes added to graph (i.e., after adding children)
    Parameters: D (array): "old" matrix of distances
                n (int): number of nodes currently in graph
                unions (list):
                no_unions (list):
                families (list):
    Returns: D1 (array): "new" (updated) matrix of distances
    """

    # initialize new matrix
    D1 = np.zeros((n+1,n+1))

    # number of old nodes (grandparent+)
    oldies = np.shape(D)[0] - ((len(unions)*2) + len(no_unions))

    # fill in new matrix with old information
    oldn = np.shape(D)[0]
    for i in range(oldn):
        for j in range(oldn):
            D1[i][j] = D[i][j]
            D1[j][i] = D[j][i]

    # compute new distances
    for u, union in enumerate(unions):
        u_children = families[u]

        for other in unions:
            if (union != other):
                o_children = families[unions.index(other)]

                # find all possible distances from union to other
                d1 = D[union[0]][other[0]]
                d2 = D[union[1]][other[0]]
                d3 = D[union[0]][other[1]]
                d4 = D[union[1]][other[1]]

                # compute distance between children of union and other
                p0_d = min(d1, d2) + 1     # distance from child to other[0]
                p1_d = min(d3, d4) + 1     # distance from child to other[1]

                # compute distance between children of union and children of other
                d = min(d1, d2, d3, d4) + 2

                # add distances to matrix
                for uc in u_children:
                    D1[uc][other[0]] = p0_d
                    D1[other[0]][uc] = p0_d

                    D1[uc][other[1]] = p1_d
                    D1[other[1]][uc] = p1_d

                    for oc in o_children:
                        D1[uc][oc] = d
                        D1[oc][uc] = d

        # add immediate family distances
        for ch in u_children:
            # add sibling distances
            for c in u_children:
                if ch != c:
                    D1[ch][c] = 2
                    D1[c][ch] = 2
            # add parent-child distances
            D1[ch][union[0]] = 1
            D1[union[0]][ch] = 1
            D1[ch][union[1]] = 1
            D1[union[1]][ch] = 1

        # get distances of nonmarried nodes to union
        for nm in no_unions:
            d5 = D[nm][union[0]]
            d6 = D[nm][union[1]]
            # add distance of nonmarried nodes to children
            for child in u_children:
                D1[nm][child] = min(d5, d6) + 1
                D1[child][nm] = min(d5, d6) + 1

        # need to find shortest path from parents to all "old" nodes
        for old in range(oldies):
            d7 = D[old][union[0]]
            d8 = D[old][union[1]]
            # children inherit parent distance
            for child in u_children:
                D1[old][child] = min(d7, d8) + 1
                D1[child][old] = min(d7, d8) + 1

    return D1

def toKDE(data, bandwidth, kernel='gaussian'):
    #data is a list of numbers that occur, and you want to find a KDE of their frequency.
    return KDE(kernel=kernel,bandwidth=bandwidth).fit(np.array(data).reshape(-1,1))

def get_probabilities(data, gen=0):
    """Create dictionary of probabilities based on real data distribution
    Parameters:
                data (list): data from a real family network
                gen (int): number of generations the network will grow (only need for marriage distribution)
    Returns:
                probs (dictionary): probabilities of all possible datapoints
                                    (e.g., probability at index 5 is probability of distance/children of length 5)

    """

    # changing bandwidth changes KDE
    bandwidth = 1

    # get kernel density of data
    kde = toKDE(data, bandwidth)
    x = np.arange(min(data)-1, max(data)+2, 1) # start and stop might need to change
    domain = x[:,np.newaxis]
    logs = kde.score_samples(domain)  # evaluate log density model on data
    y = np.exp(logs)  # get density model

    # fit spline (i.e., fit equation to density curve to then be able to integrate)
    spl = interpolate.InterpolatedUnivariateSpline(x,y)

    # create dictionary of probabilities by integrating under density curve
    probs = {}
    keys = set(data)
    for i in range(min(keys), max(keys)+gen+2):
        probs[i] = spl.integral(i-.5, i+5)

    return probs

def human_family_network(n, gen, marriage_dist, p, ncp, infdis, children_dist, name):
    """Create an artifical human family network
    Parameters:
                n (int): number of nodes for initial graph minus 1
                          (e.g., to initialize a graph of 100 nodes, set n=99)
                gen (int): number of generations for network to grow for gen+1 total generations in network
                          (e.g., when gen=3, the network grows from generation 0 to generation 4)
                marriage_dist (list): data distribution from real family network of how "far" people married
                p (float): probability of marriage
                ncp (float): probability of marriage of nonconnected nodes
                infdis (int): number of infinite distance marriages
                children_dist (list): data distribution from a real family network of number of children per union
                name (str): name for prefix of saved files

    Returns: G (graph): contrived human family network
             D (array): matrix of distances between nodes in network
             all_unions (list): list of all marriage pairs in network
             all_children (list): list of number of children per union since generation 0

    """

    # generate empty graph
    G = nx.Graph()

    # add nodes to graph
    G.add_nodes_from([i for i in range(n+1)])

    # create lists needed for function
    families = []
    for node in G.nodes():
        families.append([node])
    all_fam = functools.reduce(operator.iconcat, families, [])
    all_unions = []
    all_children = []

    # initialize distance matrix of specified size
    D = np.zeros((n+1,n+1))

    # create list of all possible distances (including infinite distances)
    all_distances = marriage_dist.copy() + [100 for i in range(infdis)]   # best number of infdis to add?

    # fill in distances with data
    for i in range(n+1):
        for j in range(n+1):
            if i == j:
                # distance from self to self
                D[i][j] = 0
            else:
                d = random.choice(marriage_dist)  # using marriage_dist instead of all_distances gives D all finite distances
                D[i][j] = d
                D[j][i] = d

    # get probabilities of possible distances to use in marriage function
    marriage_probs = get_probabilities(marriage_dist, gen=gen) # dictionary of probabilities of all finite distances
    marriage_probs[100] = (infdis/len(all_distances))/2  # include probability of infinite distance
    factor = 1.0/sum(marriage_probs.values())   # normalizing factor
    # normalize values for finite and infinite distances
    for k in marriage_probs:
        marriage_probs[k] = marriage_probs[k]*factor

    # get probabilities of possible number of children to use in children function
    child_probs = get_probabilities(children_dist)  # dictionary of probabilities of all possible number of children

    # add specified number of generations to network
    for i in range(gen+1):
        print('generation: ', i)

        # save output at each generation
        Gname = "{}_G{}.gpickle".format(name, i)   # save graph
        nx.write_gpickle(G, Gname)
        Dname = "{}_D{}.npy".format(name, i)   # save D
        np.save(Dname, D)
        Uname = "{}_U{}".format(name, i)   # save unions
        with open(Uname, 'wb') as fup:
            pickle.dump(all_unions, fup)
        Cname = "{}_C{}".format(name, i)   # save children
        with open(Cname, 'wb') as fcp:
            pickle.dump(all_children, fcp)

        # create unions between nodes to create next generation
        unions, no_unions, all_unions, n, m, infdis = add_marriage_edges(all_fam, all_unions, D, marriage_probs, p, ncp, n, infdis)
        G.add_edges_from(unions)

        oldn = n-m

        for j in range(m):
            # add non-connected ppl to distance matrix--infinte distances with everyone else
            r = np.ones((1,oldn+2+j))*100
            r[0,-1] = 0  # distance to self is 0
            c = np.ones((oldn+1+j,1))*100
            D = np.hstack((D, c))
            D = np.vstack((D, r))

        # add children to each union
        children, families, n, all_children = add_children_edges(unions, n, child_probs, all_children)
        G.add_edges_from(children)
        all_fam = functools.reduce(operator.iconcat, families, [])

        # update distances between nodes
        D = update_distances(D, n, unions, no_unions, families)

        # save output of last generation
        if i == gen:
            print("Last generation: ", i+1)
            Gname = "{}_G{}.gpickle".format(name, i+1)   # save graph
            nx.write_gpickle(G, Gname)
            Dname = "{}_D{}.npy".format(name, i+1)   # save D
            np.save(Dname, D)
            Uname = "{}_U{}".format(name, i+1)   # save unions
            with open(Uname, 'wb') as fup:
                pickle.dump(all_unions, fup)
            Cname = "{}_C{}".format(name, i+1)   # save children
            with open(Cname, 'wb') as fcp:
                pickle.dump(all_children, fcp)

    print(G.number_of_nodes())

    return G, D, all_unions, all_children, infdis


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

import logging

import libmc
from MySQLdb import connect, IntegrityError  # noqa
from ORZ import setup, OrzField, OrzBase  # noqa
from douban.mc import create_decorators
from douban.sqlstore import store_from_config
from redis2beansdb import Beansdb
from vilya.config import (
    MEMCACHED_HOSTS,
    MEMCACHED_CONFIG,
    MYSQL_STORE,
)

logging.getLogger().setLevel(logging.DEBUG)

# mc
ONE_MINUTE = 60
HALF_HOUR = 1800
ONE_HOUR = 3600
HALF_DAY = ONE_HOUR * 12
ONE_DAY = ONE_HOUR * 24
ONE_WEEK = ONE_DAY * 7
ONE_MONTH = ONE_DAY * 30
ONE_YEAR = ONE_DAY * 365

_clients = {}

def hashdict(d):
    """
    make dictionary becomes a immutable tuple
    """
    if isinstance(d, (tuple, list)):
        return tuple(hashdict(v) for v in d)
    elif isinstance(d, dict):
        return tuple(sorted((k, hashdict(v)) for k, v in d.iteritems()))
    else:
        return d

def Client(servers=None, *args, **kwargs):
    key = hashdict([servers, kwargs])
    mc = _clients.get(key)
    if not mc:
        mc = libmc.Client(servers, *args, **kwargs)
        _clients[key] = mc
    return mc

def get_mc():
    return Client(MEMCACHED_HOSTS, **MEMCACHED_CONFIG)

def get_db():
    return Beansdb()

def stub_cache(*args, **kws):
    pass

mc = get_mc()
bdb = get_db()

pcache = pcache2 = listcache = cache_in_obj = delete_cache = cache = stub_cache
globals().update(create_decorators(mc))

def mc_gets(mc_key, getter, ids):
    '''helpler for gets function'''
    results = mc.get_multi([mc_key % i for i in ids])
    return [results.get(mc_key % i) or getter(i) for i in ids]

# mysql
def connect_mysql():
    return connect(use_unicode=True, user='root', passwd='', db='valentine')

def make_dict(cursor, row):
    return dict(zip((str(d[0]) for d in cursor.description), row))

def reset_mc(deep=False):
    for mc in _clients.itervalues():
        getattr(mc, 'clear', lambda: True)()
        if deep:
            getattr(mc.mc, 'clear', lambda: True)()

# TODO(xutao) delete reset_beansdb?
def reset_beansdb():
    for db in _clients.itervalues():
        db.clear_cache()

# FIXME(xutao) rename to clear_mc_for_test?
def clear_beansdb_for_test():
    for db in _clients.itervalues():
        db.clear_cache()
        for s in db.db.servers:
            clear = getattr(s.mc, 'clear', None)
            if clear:
                clear()

# FIXME(xutao) I can't find any code about put beansdb client to `_clients`, and beansdb shouldn't be clear after request
def clear_local_cache():
    reset_mc()
    reset_beansdb()

store = store_from_config(MYSQL_STORE, use_cache=False)
setup(store, mc)


class Solution:
    def minSetSize(self, arr: List[int]) -> int:
        count = {}
        for num in arr:
            count[num] = 1 + count.get(num,0)
        count = dict(sorted(count.items(), key= lambda item: item[1],reverse = True))
        target = len(arr)/2
        k = 0
        tot = 0
        for num, cnt in count.items():
            tot+=cnt
            k+=1
            if tot >= target:
                return k


import logging
import os
import re
import yaml

from PIL import Image
from pelican import signals
from jinja2 import Template

logger = logging.getLogger(__name__)

pswipe_regex = re.compile(r'(\[pswipe:([^,]+),([^.,]+)(\.[^,]+),([^\]]+)\])')
pswipe_template = """<img src='/images/{{album}}/{{image_root}}_thumbnail_tall{{image_ext}}' title='{{caption}}' onclick='pswipe("{{album}}",{{image_index}});'/>"""

def initialized(pelican):
    pelican.settings['GALLERIES'] = {}

def article_generator_finalized(generator):
    galleries = generator.context.get('GALLERIES')
    template = Template(pswipe_template)
    for article in reversed(generator.articles):
        for match in pswipe_regex.findall(article._content):
            album = match[1]
            image_root = match[2]
            image_ext = match[3]
            caption = match[4]
            if album not in galleries: galleries[album] = []
            gallery = galleries[album]
            image_id = len(gallery)
            image_uri = '/images/' + album + '/' + image_root + image_ext
            image_path = os.path.join('content/images', album, image_root + image_ext)
            image = Image.open(image_path)
            image_index = next((i for (i,e) in enumerate(gallery) if e[0] == image_uri), len(gallery))
            if image_index == len(gallery): gallery.append([])
            gallery[image_index] = [image_uri, image.size, caption]
            context = generator.context.copy()
            context.update({
                'album': album,
                'image_root': image_root,
                'image_ext': image_ext,
                'caption': caption,
                'image_index': image_index
            })
            replacement = template.render(context)
            article._content = article._content.replace(match[0], replacement)

galleries_template = """\
var galleries = {
    {% for galname, items in GALLERIES.items() %}\
        {{galname}}: [
            {% for item in items %}\
                {src:"{{item[0]}}", w:{{item[1][0]}}, h:{{item[1][1]}}, title:"{{item[2]}}"},
            {% endfor %}\
        ],
    {% endfor %}\
};

"""

def article_writer_finalized(generator, writer):
    writer.write_file("galleries.js", Template(galleries_template), generator.context)

def register():
    signals.initialized.connect(initialized)
    signals.article_generator_finalized.connect(article_generator_finalized)
    signals.article_writer_finalized.connect(article_writer_finalized)