import random
import string

import numpy as np
import torch
from torch import autograd

class Logger:
  """A class for printing logging information to screen."""

  def __init__(self, verbosity):
    self._verbosity = verbosity

  def print(self, level, message):
    """Print a message if level is not higher than verbosity.

    Args:
      level: the level of this message, smaller value means more important
      message: the message to be printed
    """
    if level <= self._verbosity:
      print(message)

def generate_random_string(length=6):
  """Generate a random string of upper case letters and digits.

  Args:
    length: length of the generated string

  Returns:
    the generated string
  """
  return ''.join([
      random.choice(string.ascii_uppercase + string.digits)
      for _ in range(length)])

def enforce_cluster_id_uniqueness(cluster_ids):
  """Enforce uniqueness of cluster id across sequences.

  Args:
    cluster_ids: a list of 1-dim list/numpy.ndarray of strings

  Returns:
    a new list with same length of cluster_ids

  Raises:
    TypeError: if cluster_ids or its element has wrong type
  """
  if not isinstance(cluster_ids, list):
    raise TypeError('cluster_ids must be a list')
  new_cluster_ids = []
  for cluster_id in cluster_ids:
    sequence_id = generate_random_string()
    if isinstance(cluster_id, np.ndarray):
      cluster_id = cluster_id.tolist()
    if not isinstance(cluster_id, list):
      raise TypeError('Elements of cluster_ids must be list or numpy.ndarray')
    new_cluster_id = ['_'.join([sequence_id, s]) for s in cluster_id]
    new_cluster_ids.append(new_cluster_id)
  return new_cluster_ids

def concatenate_training_data(train_sequences, train_cluster_ids,
                              enforce_uniqueness=True, shuffle=True):
  """Concatenate training data.

  Args:
    train_sequences: a list of 2-dim numpy arrays to be concatenated
    train_cluster_ids: a list of 1-dim list/numpy.ndarray of strings
    enforce_uniqueness: a boolean indicated whether we should enfore uniqueness
      to train_cluster_ids
    shuffle: whether to randomly shuffle input order

  Returns:
    concatenated_train_sequence: a 2-dim numpy array
    concatenated_train_cluster_id: a list of strings

  Raises:
    TypeError: if input has wrong type
    ValueError: if sizes/dimensions of input or their elements are incorrect
  """
  # check input
  if not isinstance(train_sequences, list) or not isinstance(
      train_cluster_ids, list):
    raise TypeError('train_sequences and train_cluster_ids must be lists')
  if len(train_sequences) != len(train_cluster_ids):
    raise ValueError(
        'train_sequences and train_cluster_ids must have same size')
  train_cluster_ids = [
      x.tolist() if isinstance(x, np.ndarray) else x
      for x in train_cluster_ids]
  global_observation_dim = None
  for i, (train_sequence, train_cluster_id) in enumerate(
      zip(train_sequences, train_cluster_ids)):
    train_length, observation_dim = train_sequence.shape
    if i == 0:
      global_observation_dim = observation_dim
    elif global_observation_dim != observation_dim:
      raise ValueError(
          'train_sequences must have consistent observation dimension')
    if not isinstance(train_cluster_id, list):
      raise TypeError(
          'Elements of train_cluster_ids must be list or numpy.ndarray')
    if len(train_cluster_id) != train_length:
      raise ValueError(
          'Each train_sequence and its train_cluster_id must have same length')

  # enforce uniqueness
  if enforce_uniqueness:
    train_cluster_ids = enforce_cluster_id_uniqueness(train_cluster_ids)

  # random shuffle
  if shuffle:
    zipped_input = list(zip(train_sequences, train_cluster_ids))
    random.shuffle(zipped_input)
    train_sequences, train_cluster_ids = zip(*zipped_input)

  # concatenate
  concatenated_train_sequence = np.concatenate(train_sequences, axis=0)
  concatenated_train_cluster_id = [x for train_cluster_id in train_cluster_ids
                                   for x in train_cluster_id]
  return concatenated_train_sequence, concatenated_train_cluster_id

def sample_permuted_segments(index_sequence, number_samples):
  """Sample sequences with permuted blocks.

  Args:
    index_sequence: (integer array, size: L)
      - subsequence index
      For example, index_sequence = [1,2,6,10,11,12].
    number_samples: (integer)
      - number of subsampled block-preserving permuted sequences.
      For example, number_samples = 5

  Returns:
    sampled_index_sequences: (a list of numpy arrays) - a list of subsampled
      block-preserving permuted sequences. For example,
    ```
    sampled_index_sequences =
    [[10,11,12,1,2,6],
     [6,1,2,10,11,12],
     [1,2,10,11,12,6],
     [6,1,2,10,11,12],
     [1,2,6,10,11,12]]
    ```
      The length of "sampled_index_sequences" is "number_samples".
  """
  segments = []
  if len(index_sequence) == 1:
    segments.append(index_sequence)
  else:
    prev = 0
    for i in range(len(index_sequence) - 1):
      if index_sequence[i + 1] != index_sequence[i] + 1:
        segments.append(index_sequence[prev:(i + 1)])
        prev = i + 1
      if i + 1 == len(index_sequence) - 1:
        segments.append(index_sequence[prev:])
  # sample permutations
  sampled_index_sequences = []
  for _ in range(number_samples):
    segments_array = []
    permutation = np.random.permutation(len(segments))
    for permutation_item in permutation:
      segments_array.append(segments[permutation_item])
    sampled_index_sequences.append(np.concatenate(segments_array))
  return sampled_index_sequences

def resize_sequence(sequence, cluster_id, num_permutations=None):
  """Resize sequences for packing and batching.

  Args:
    sequence: (real numpy matrix, size: seq_len*obs_size) - observed sequence
    cluster_id: (numpy vector, size: seq_len) - cluster indicator sequence
    num_permutations: int - Number of permutations per utterance sampled.

  Returns:
    sub_sequences: A list of numpy array, with obsevation vector from the same
      cluster in the same list.
    seq_lengths: The length of each cluster (+1).
  """
  # merge sub-sequences that belong to a single cluster to a single sequence
  unique_id = np.unique(cluster_id)
  sub_sequences = []
  seq_lengths = []
  if num_permutations and num_permutations > 1:
    for i in unique_id:
      idx_set = np.where(cluster_id == i)[0]
      sampled_idx_sets = sample_permuted_segments(idx_set, num_permutations)
      for j in range(num_permutations):
        sub_sequences.append(sequence[sampled_idx_sets[j], :])
        seq_lengths.append(len(idx_set) + 1)
  else:
    for i in unique_id:
      idx_set = np.where(cluster_id == i)
      sub_sequences.append(sequence[idx_set, :][0])
      seq_lengths.append(len(idx_set[0]) + 1)
  return sub_sequences, seq_lengths

def pack_sequence(
    sub_sequences, seq_lengths, batch_size, observation_dim, device):
  """Pack sequences for training.

  Args:
    sub_sequences: A list of numpy array, with obsevation vector from the same
      cluster in the same list.
    seq_lengths: The length of each cluster (+1).
    batch_size: int or None - Run batch learning if batch_size is None. Else,
      run online learning with specified batch size.
    observation_dim: int - dimension for observation vectors
    device: str - Your device. E.g., `cuda:0` or `cpu`.

  Returns:
    packed_rnn_input: (PackedSequence object) packed rnn input
    rnn_truth: ground truth
  """
  num_clusters = len(seq_lengths)
  sorted_seq_lengths = np.sort(seq_lengths)[::-1]
  permute_index = np.argsort(seq_lengths)[::-1]

  if batch_size is None:
    rnn_input = np.zeros((sorted_seq_lengths[0],
                          num_clusters,
                          observation_dim))
    for i in range(num_clusters):
      rnn_input[1:sorted_seq_lengths[i], i,
                :] = sub_sequences[permute_index[i]]
    rnn_input = autograd.Variable(
        torch.from_numpy(rnn_input).float()).to(device)
    packed_rnn_input = torch.nn.utils.rnn.pack_padded_sequence(
        rnn_input, sorted_seq_lengths, batch_first=False)
  else:
    mini_batch = np.sort(np.random.choice(num_clusters, batch_size))
    rnn_input = np.zeros((sorted_seq_lengths[mini_batch[0]],
                          batch_size,
                          observation_dim))
    for i in range(batch_size):
      rnn_input[1:sorted_seq_lengths[mini_batch[i]],
                i, :] = sub_sequences[permute_index[mini_batch[i]]]
    rnn_input = autograd.Variable(
        torch.from_numpy(rnn_input).float()).to(device)
    packed_rnn_input = torch.nn.utils.rnn.pack_padded_sequence(
        rnn_input, sorted_seq_lengths[mini_batch], batch_first=False)
  # ground truth is the shifted input
  rnn_truth = rnn_input[1:, :, :]
  return packed_rnn_input, rnn_truth

def output_result(model_args, training_args, test_record):
  """Produce a string to summarize the experiment."""
  accuracy_array, _ = zip(*test_record)
  total_accuracy = np.mean(accuracy_array)
  output_string = """
Config:
  sigma_alpha: {}
  sigma_beta: {}
  crp_alpha: {}
  learning rate: {}
  regularization: {}
  batch size: {}

Performance:
  averaged accuracy: {:.6f}
  accuracy numbers for all testing sequences:
  """.strip().format(
      training_args.sigma_alpha,
      training_args.sigma_beta,
      model_args.crp_alpha,
      training_args.learning_rate,
      training_args.regularization_weight,
      training_args.batch_size,
      total_accuracy)
  for accuracy in accuracy_array:
    output_string += '\n    {:.6f}'.format(accuracy)
  output_string += '\n' + '=' * 80 + '\n'
  filename = 'layer_{}_{}_{:.1f}_result.txt'.format(
      model_args.rnn_hidden_size,
      model_args.rnn_depth, model_args.rnn_dropout)
  with open(filename, 'a') as file_object:
    file_object.write(output_string)
  return output_string

def estimate_transition_bias(cluster_ids, smooth=1):
  """Estimate the transition bias.

  Args:
    cluster_id: Either a list of cluster indicator sequences, or a single
      concatenated sequence. The former is strongly preferred, since the
      transition_bias estimated from the latter will be inaccurate.
    smooth: int or float - Smoothing coefficient, avoids -inf value in np.log
      in the case of a sequence with a single speaker and division by 0 in the
      case of empty sequences. Using a small value for smooth decreases the
      bias in the calculation of transition_bias but can also lead to underflow
      in some remote cases, larger values are safer but less accurate.

  Returns:
    bias: Flipping coin head probability.
    bias_denominator: The denominator of the bias, used for multiple calls to
      fit().
  """
  transit_num = smooth
  bias_denominator = 2 * smooth
  for cluster_id_seq in cluster_ids:
    for entry in range(len(cluster_id_seq) - 1):
      transit_num += (cluster_id_seq[entry] != cluster_id_seq[entry + 1])
      bias_denominator += 1
  bias = transit_num / bias_denominator
  return bias, bias_denominator


# coding: utf-8

import time
import logging
from socket import *

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("tcp-client")

def benchmark(name, addr, rqty):
    sock = socket(AF_INET, SOCK_STREAM)
    try:
        sock.connect(addr)
    except ConnectionRefusedError:
        logger.warning("Unable to connect to %s / %s", name, addr)
        return

    ts = time.time()
    for n in range(rqty):
        sock.send(b'ping\n')
        resp = sock.recv(5)
        if resp != b'pong\n':
            break
    sock.send(b'exit\n')
    sock.close()

    sec = time.time() - ts
    logger.info("{:>9s} - {:.2f}s [{:.2f} RPS]".format(name, sec, rqty / sec))
    time.sleep(1.)

for i, name in enumerate(["tokio", "libuv", "asyncio", "trio"]):
    time.sleep(1.)
    benchmark(name, ("localhost", 50000 + i), 500000)


from pulp import LpProblem, LpVariable, lpSum, LpMinimize, LpMaximize
import math
from decimal import Decimal
from secondObjective import solve_linear_problem2

def solve_linear_problem(N, M, K, pij):
    # Create the LP problem
    problem = LpProblem("Linear_Problem", LpMinimize)

    # Define the decision variables
    x = LpVariable.dicts("x", N, lowBound=0, upBound=1, cat="Binary")
    lambda1 = LpVariable("lambda1")

    # Add the objective function
    problem += lambda1

    # Add the constraints
    for j in M:
        problem += lpSum(math.log(1 - pij[i][j]) * x[i] for i in N) <= lambda1

    problem += lpSum(x[i] for i in N) == K

    # Initialize a variable to store the optimal j value
    optimal_j = None

    # Solve the problem
    problem.solve()

    # Iterate over the constraints to find the corresponding j value
    for j in M:
        # print("Value for this j = ", j ,sum(math.log(1 - pij[i][j]) * x[i].varValue for i in N))
        # print("lambda1 value ", lambda1.varValue)
        # Convert lambda1.varValue to a Decimal object
        lambda1_decimal = Decimal(str(lambda1.varValue))
        # Convert the decimal number to a tuple representation
        decimal_tuple = lambda1_decimal.as_tuple()

        # Count the number of digits after the decimal point
        num_digits_after_decimal = abs(decimal_tuple.exponent)
        trimmed_number = round(sum(math.log(1 - pij[i][j]) * x[i].varValue for i in N), num_digits_after_decimal)
        # if optimal_j != None:
        #     continue
        if trimmed_number == lambda1.varValue:
            optimal_j = j
            # break

    lambda_optimal = sum(math.log(1 - pij[i][optimal_j]) * x[i].varValue for i in N)
    # Return the optimal solution and the corresponding j value
    optimal_solution = {
        "lambda1": lambda_optimal,
        "optimal_j": optimal_j,
        "x": [x[i].varValue for i in N]
    }
    return optimal_solution

pij = [ [0.9,0.33333333,0.9,0.33333333,0,0.33333333,0,0.33333333],
        [0.33333333,0.9,0.33333333,0,0,0,0,0],
        [0.9,0.33333333, 0.9,0.33333333,0,0.33333333, 0, 0.33333333],
        [0.33333333, 0, 0.33333333, 0.9,        0, 0.33333333,0,0.33333333],
        [0,     0, 0,    0,     0.9,        0,0.33333333, 0.33333333],
        [0.33333333, 0,         0.33333333, 0.33333333,0, 0.9,0,0.33333333],
        [0,0, 0, 0,    0.33333333, 0, 0.9, 0.66666667],
        [0.33333333, 0, 0.33333333, 0.33333333, 0.33333333, 0.33333333,0.66666667 ,0.9]]  # Example pij matrix

length = len(pij[0])
N = list(range(length)) # Set of i values
M = list(range(length)) # Set of j values
K = 2  # Value of K

indexj =[]
lambda1 = []

solution = solve_linear_problem(N, M, K,pij)
lambda1.append(solution["lambda1"])
indexj.append(solution["optimal_j"])

length = len(N)
avarage_connections =[]
for i in range(length-1):
    infoi = solve_linear_problem2(N,M,K,pij,lambda1, indexj,i+2)
    lambda1.append(infoi["lambda1"])
    indexj.append(infoi["optimal_j"])
controllers = [i+1 for i, val in enumerate(infoi["x"]) if val == 1]
print("this are controllersssssss:",controllers)
# Print the solution
print("Optimal Solution:")
print("lambda1 =", infoi["lambda1"])
print("Controllers =", [i+1 for i, val in enumerate(solution["x"]) if val == 1])
print("Optimal j =", solution["optimal_j"])
print("lambda1 =", lambda1)


# unit tests for rates
import pytest

import pynucastro as pyna

class TestDuplicates:

    @pytest.fixture(scope="class")
    def ecsn_lib(self, reaclib_library, tabular_library):
        all_nuclei = ["p", "he4",
                      "ne20", "o20", "f20",
                      "mg24", "al27", "o16",
                      "si28", "s32", "p31"]
        ecsn_rl_lib = reaclib_library.linking_nuclei(all_nuclei, with_reverse=True)

        ecsn_tabular_lib = tabular_library.linking_nuclei(["f20", "o20", "ne20"])

        return ecsn_rl_lib + ecsn_tabular_lib

    def test_rate_collection(self, ecsn_lib):

        with pytest.raises(pyna.networks.RateDuplicationError):
            pyna.RateCollection(libraries=[ecsn_lib])

    def test_library_find_duplicate_links(self, ecsn_lib):

        dupes = ecsn_lib.find_duplicate_links()

        assert len(dupes) == 2

        d1 = dupes[0][0]
        assert d1.reactants == [pyna.Nucleus("o20")]
        assert d1.products == [pyna.Nucleus("f20")]

        d2 = dupes[1][0]
        assert d2.reactants == [pyna.Nucleus("f20")]
        assert d2.products == [pyna.Nucleus("ne20")]


from setuptools import setup
import os
from glob import glob
from shutil import rmtree

packageName = "pyPman"
try:
    from setuptools.command.clean import clean as clean_command
except ImportError as err:
    from distutils.command.clean import clean as clean_command

class Clean(clean_command):
    def run(self):
        clean_command.run(self)
        delete_in_root = ['build', 'dist', '*.egg-info', '.tox', '.eggs', '.cache']
        delete_everywhere = ['__pycache__', '*.pyc']

        for candidate in delete_in_root:
            rmtree_glob(candidate)

        for visible_dir in glob("[A-Za-z0-9]*"):
            for candidate in delete_everywhere:
                rmtree_glob(os.path.join(visible_dir, candidate))
                rmtree_glob(os.path.join(visible_dir, "*", candidate))

def rmtree_glob(candidate):
    for fobj in glob(candidate):
        try:
            rmtree(fobj)
            print("%s/ removed from project" % fobj)
        except OSError:
            try:
                os.remove(fobj)
                print("%s/ removed from project" % fobj)
            except OSError:
                print("Testing")

def read_file(path=None, file_name=None):
    path = path or "."
    file_path = os.path.join(path, file_name)
    print("Reading file %s" % file_path)
    with open(file_path) as fd:
        return fd.read()

exec(read_file(path=os.path.join('.', packageName), file_name="__init__.py"))
version = __version__
projectName = __name__
readmeData = read_file(file_name="README.md")
licenseData = read_file(file_name="LICENSE")

setup(
    name=packageName,
    description="Python project manager",
    version=version,
    packages=[
        packageName,
        "%s.initialize" % packageName,
        "%s.install" % packageName,
        "%s.utilities" % packageName,
    ],
    install_requires=[
        "Click",
        "traVer == 1.1.0",
        "setuptools",
        "importlib_metadata"
    ],
    license="GNU GENERAL PUBLIC LICENSE",
    long_description_content_type='text/markdown',
    long_description=readmeData,
    author=', '.join(__author__),
    author_email=', '.join(__author_email__),
    url="https://github.com/sunilk-n/TraVersion",
    classifiers=[
        # See https://pypi.org/pypi?%3Aaction=list_classifiers
        "Environment :: Web Environment",
        "Intended Audience :: Developers",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
    data_files=[
        "README.md",
        "LICENSE"
    ],
    entry_points={
        'console_scripts': [
            'pyPman=pyPman.__main__:cli'
        ],
    },
    cmdclass={
        "clean": Clean,
    },
)


import os

from flask import Flask, send_from_directory, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'static', 'uploads')
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 * 1024
app.config['TEMPLATES_AUTO_RELOAD'] = True

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico',
                               mimetype='image/vnd.microsoft.icon')

@app.route('/')
def share():
    if not os.path.exists(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])):
        os.mkdir(os.path.join(app.root_path, app.config['UPLOAD_FOLDER']))
    files = os.listdir(os.path.join(app.root_path, app.config['UPLOAD_FOLDER']))
    return render_template('share.html', files=files)

@app.route('/upload', methods=['POST'])
def upload():
    files = request.files.getlist('files')
    for f in files:
        f.save(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
    return redirect(url_for('share'))

if __name__ == '__main__':
    app.run('0.0.0.0', 8080)  # You can change the IP and port yourself


import sys
import py21cmfast as p21c
import time
import pickle

sys.path.insert(1, '/gpfs0/elyk/users/hovavl/21CMPSemu')
from tau import calc_tau
from power_spectrum import powerspectra

user_params = {"DIM": 512, "HII_DIM": 128, "BOX_LEN": 256, "N_THREADS": 1, 'USE_RELATIVE_VELOCITIES': True,
               'DO_VCB_FIT': True,
               "POWER_SPECTRUM": 6}
flag_options = {"USE_MASS_DEPENDENT_ZETA": True, "USE_CMB_HEATING": False, "USE_LYA_HEATING": False,
                'USE_TS_FLUCT': True,
                'USE_MINI_HALOS':False, "INHOMO_RECO": True}

cosmo_params = {"hlittle": 0.6736, "OMb": 0.0493, "OMm": 0.3153,
                "A_s": 2.1e-9, "POWER_INDEX": 0.9649}

# print(cosmo_params['hlittle']**2 * cosmo_params['OMb'])
# print(cosmo_params['hlittle']**2 * cosmo_params['OMm'])

def run(theta):
    F_STAR10, F_STAR7_MINI, ALPHA_STAR, ALPHA_STAR_MINI, F_ESC10, F_ESC7_MINI, ALPHA_ESC, M_TURN, L_X, NU_X_THRESH, = theta
    astro_params = {'F_STAR10': F_STAR10, 'F_ESC10': F_ESC10, 'L_X': L_X, 'M_TURN': M_TURN,
                    'NU_X_THRESH': NU_X_THRESH * 1000, 'ALPHA_STAR': ALPHA_STAR, 'ALPHA_ESC': ALPHA_ESC,
                    'F_STAR7_MINI': F_STAR7_MINI, 'ALPHA_STAR_MINI': ALPHA_STAR_MINI, 'F_ESC7_MINI': F_ESC7_MINI,
                    't_STAR': 0.5
                    }
    start = time.time()
    lightcone = p21c.run_lightcone(redshift=5.0,
                                   random_seed=1,
                                   write=False,
                                   user_params=user_params,
                                   lightcone_quantities={'brightness_temp', 'density', 'xH_box', 'Ts_box', 'dNrec_box',
                                                         'Gamma12_box', 'J_21_LW_box'},
                                   global_quantities={'brightness_temp', 'density', 'xH_box', 'Ts_box', 'dNrec_box',
                                                      'Gamma12_box', 'J_21_LW_box'},
                                   flag_options=flag_options,
                                   astro_params=astro_params,
                                   cosmo_params=cosmo_params,
                                   direc='_cache')
    end = time.time()
    elapsed_time = time.strftime('%H:%M:%S', time.gmtime(end - start))
    print(f'Total running time: {elapsed_time}')

    z_global = lightcone.node_redshifts
    lightcone_redshifts = lightcone.lightcone_redshifts
    T_b = lightcone.global_quantities['brightness_temp']
    xH_box = lightcone.xH_box
    global_xH = lightcone.global_xH
    density_box = lightcone.density
    tau = calc_tau(lightcone_redshifts, 1 + density_box, xH_box, cosmo_params)
    output = {'astro_params': astro_params,
              'global_xH': global_xH,
              'tau': tau,
              'T_b':T_b,
              'z_global': z_global
              }

    nchunks = 20

    data_PS, z_PS = powerspectra(lightcone_box=lightcone.brightness_temp,
                                 BOX_LEN=lightcone.user_params.BOX_LEN,
                                 HII_DIM=lightcone.user_params.HII_DIM,
                                 lightcone_redshifts=lightcone.lightcone_redshifts,
                                 nchunks=nchunks)

    output['data_PS'] = data_PS
    output['z_PS'] = z_PS
    x = 1
    with open('/gpfs0/elyk/users/hovavl/21CMPSemu/simulations/testFDM.pk',
              'wb') as f:
        pickle.dump(output, f)

run([-1.22,-2.72, 0.51, -0.02, -1.03, -2.32, 0.11,  8.59, 39, 0.74])


import lab1
import lab2
import sys
import time

# TODO: update help message before submitting
helpMessage = """
COMP 412, Scheduler (lab 3)
Syntax: ./schedule [options] filename

        filename is the pathname (absolute or relative) to the input file

        Options can precede or follow the filename.
        -h prints this message (requires no filename)

        -c : dump value table after constant propagation.
        -e : turns off early release
        -f : toggles fast output
        -g : creates a '.dot' file for graphviz
        -n : disables constant propagation.
        -s : disables dependence trimming for LOAD, STORE, & OUTPUT
        -t : turns on internal timing reports
        -V : verifies dependence graph structure before use.
        -v : prints version number.
        -w : suppresses some warning messages
"""

EDGE_TYPES = ["Data", "Serialization", "Conflict"]
DATA, SERIALIZATION, CONFLICT = 0, 1, 2

LOAD_LATENCY = 5
LOADI_LATENCY = 1
STORE_LATENCY = 5
ADD_LATENCY = 1
SUB_LATENCY = 1
MULT_LATENCY = 3
LSHIFT_LATENCY = 1
RSHIFT_LATENCY = 1
OUTPUT_LATENCY = 1
NOP_LATENCY = 1

LEX_TO_LATENCY = (LOAD_LATENCY, STORE_LATENCY, LOADI_LATENCY, ADD_LATENCY, SUB_LATENCY, MULT_LATENCY, LSHIFT_LATENCY,
            RSHIFT_LATENCY, OUTPUT_LATENCY, NOP_LATENCY)

FUNC0_ALLOWED = {lab1.LOAD_LEX, lab1.STORE_LEX, lab1.OUTPUT_LEX, lab1.LOADI_LEX, lab1.ADD_LEX, lab1.SUB_LEX, lab1.LSHIFT_LEX, lab1.RSHIFT_LEX, lab1.NOP_LEX}
FUNC1_ALLOWED = {lab1.MULT_LEX, lab1.OUTPUT_LEX, lab1.LOADI_LEX, lab1.ADD_LEX, lab1.SUB_LEX, lab1.LSHIFT_LEX, lab1.RSHIFT_LEX, lab1.NOP_LEX}

NOT_READY, READY, ACTIVE, RETIRED = 1, 2, 3, 4

class GraphNode:
    def __init__(self, ir_node: lab1.IR_Node):
        self.ir_node =  ir_node   # represents IR_Node
        self.out_edges = [] # note: edges will be tuples: (<destination GraphNode>, <edge type (int)> ) NOTE: there will be an optional 3rd element in tuple to store vr for DATA edges
        self.in_edges = []
        self.prio = 0
        self.status = NOT_READY # status values are 1: not ready, 2: ready, 3: active, 4: retired
        self.cycleToRetire = 0
        # potentially have a field for self.in (in-edges)
    def add_edge(self, dest, edge_type: int, data_vr=-1):
        if data_vr == -1:
            self.out_edges.append((dest, edge_type))
            dest.in_edges.append((self, edge_type))
        else:
            self.out_edges.append((dest, edge_type, data_vr))
            dest.in_edges.append((self, edge_type, data_vr))

def create_dependence_graph(dummy: lab1.IR_Node):
    def_location = {}
    nodes_arr = []
    # prev_stores = []
    # prev_outputs = []
    # prev_loads = []
    prev_store = None
    prev_output = None
    prev_load = None

    curr = dummy.next
    while curr != dummy:
        # create a node for current operation
        node = GraphNode(curr)
        nodes_arr.append(node)

        # if o defines a virtual register, update the def_location map
        if curr.op3.sr != -1: # check if curr.op3 exists
            def_location[curr.op3.vr] = node

        # temp code
        # if curr.opcode == lab1.STORE_LEX:
        #     print(f"{lab1.LEXEMES[curr.opcode]}\t{curr.op1.printSR()}, {curr.op2.printSR()}, {curr.op3.printSR()}")

        # last_store_idx = len(prev_stores) - 1
        # last_output_idx = len(prev_outputs) - 1
        # last_load_idx = len(prev_loads) - 1

        data_edge_nodes = []
        # set edges for each of this node's uses to their definition locations
        for i in range(1, 3):
            if i == 1:
                o = curr.op1
            elif i == 2:
                o = curr.op2
            if o.sr == -1:  # o.sr == -1 indicates empty operand
                continue
            if o.isConstant:
                o.vr = o.sr # o.vr is set to o.sr. This may be inefficient, not sure.
                continue
            # add DATA edge
            node.add_edge(def_location[o.vr], DATA, o.vr)
            data_edge_nodes.append(def_location[o.vr])

        # sort data_edge_nodes to help with updating last_store, last_output, and last_load
        # print(f"data_edge_nodes before sort: {[node.lineno for node in data_edge_nodes]}")
        # data_edge_nodes.sort(key=node.ir_node.lineno, reverse=True)
        # print(f"data_edge_nodes after sort: {[node.lineno for node in data_edge_nodes]}")

        # update last_store, last_output, and load_load to be the most recent NON-DATA node
        # if len(prev_stores) > last_store_idx and data_edge_nodes[-1]
        # while len(prev_stores) > last_store_idx and data_edge_node[] == prev_stores[last_store_idx]:
        #     last_store_idx -= 1

        # nondata_prev_stores = [node for node in prev_stores if node not in data_edge_nodes]
        # # if len(nondata_prev_stores) != len(prev_stores):
        #     # print("//data store removed!")
        # nondata_prev_loads = [node for node in prev_loads if node not in data_edge_nodes]
        # # if len(nondata_prev_loads) != len(prev_loads):
        #     # print("//data load removed!")
        # nondata_prev_outputs = [node for node in prev_outputs if node not in data_edge_nodes]
        # # if len(nondata_prev_outputs) != len(prev_outputs):
        #     # print("//data output removed!")

        # set up a list comprehension containing just the GraphNodes at the end of edges to use "in" operation
        # edge_nodes = [edge[0] for edge in node.edges]

        # if o is a load, store, or output, add serial and conflict edges to other memory ops
        if curr.opcode == lab1.LOAD_LEX:                    # load needs conflict edge to most recent store
            if prev_store != None: # add extra check to confirm there isn't already a DATA edge to that node
                node.add_edge(prev_store, CONFLICT)
            prev_load = node
            # if len(prev_loads) > 3:
            #     prev_loads.pop(0)
        elif curr.opcode == lab1.OUTPUT_LEX:                # output needs conflict edge to recent store + serialization edge to recent output
            if prev_store != None:
                node.add_edge(prev_store, CONFLICT)
            if prev_output != None:
                node.add_edge(prev_output, SERIALIZATION)
            prev_output = node
            # if len(prev_outputs) > 3:
            #     prev_outputs.pop(0)
        elif curr.opcode == lab1.STORE_LEX:                 # store needs serialization edge to most recent store, load, & output
            # print(f"entered store for curr={curr.lineno}: last_load={prev_loads.ir_node.lineno}")
            if prev_load != None:
                # temp code
                # print(f"adding serialization edge from {node.ir_node.lineno} to {prev_loads.ir_node.lineno}")
                # print(f"node's edges:")
                # for edge in node.edges:
                #     print(edge[0].ir_node.lineno)
                # print(f"last_load({prev_loads.ir_node.lineno}) in node.edges = {prev_loads in node.edges}")
                # temp code
                node.add_edge(prev_load, SERIALIZATION)
            if prev_output != None:
                node.add_edge(prev_output, SERIALIZATION)
            if prev_store != None:
                node.add_edge(prev_store, SERIALIZATION)
            prev_store = node
            # if len(prev_stores) > 3:
            #     prev_stores.pop(0)

        curr = curr.next

    return nodes_arr

def get_roots_and_leaves(nodes_arr):
    root_set = set()
    leaf_set = set()
    for node in nodes_arr:
        if len(node.in_edges) == 0:
            root_set.add(node)
        elif len(node.out_edges) == 0:
            leaf_set.add(node)
    return root_set, leaf_set

def assign_priorities(nodes_arr, root_set):
    for root in root_set:
        assign_priorities_helper(root)

def assign_priorities_helper(curr):
    for edge in curr.out_edges:
        child = edge[0]
        child_prio = LEX_TO_LATENCY[child.ir_node.opcode]

        child_prio *= 10    # Multiply latency weight by 10
        child_prio += len(child.out_edges)  # Add number of descendants
        child_prio += curr.prio # Add parent weight

        if child_prio > child.prio:
            child.prio = child_prio
            assign_priorities_helper(child)
            return True
        else:
            False

# helper function for inserting node into a list of nodes that's currently sorted in descending order of priorities
def insertNode(ready, node):
    for i in range(len(ready)):
        if node.prio > ready[i].prio:
            ready.insert(i, node)
            return
    ready.append(node)

def printInstruction(node1, node2):
    print(f"[{node1.ir_node.printWithVRClean()}; {node2.ir_node.printWithVRClean()}]")

def schedule(leaf_set):
    nop_graph_node = GraphNode(lab1.IR_Node.createNOP())  # singleton NOP graph node
    cycle = 1
    for leaf in leaf_set: # Insert each root into ready set
        leaf.status = READY
    ready = list(leaf_set)    # Potential optimizatino: may want to replace with maxheap
    ready.sort(key=(lambda root: root.prio), reverse=True)
    active = set()

    while len(ready) > 0 or len(active) > 0:
        # pick an operation for each functional unit
        node1 = None
        node2 = None
        if len(ready) >= 1:
            node1_idx = 0
            node1 = ready[node1_idx]
            while node1.ir_node.opcode not in FUNC0_ALLOWED:
                # print(f"node1_idx: {node1_idx}")
                node1_idx += 1
                if node1_idx >= len(ready):
                    # print("entered base case about to break")
                    node1 = None
                    break
                node1 = ready[node1_idx]
            if node1 != None:
                # print(f"node1: {node1}")
                ready.pop(node1_idx)
        if node1 == None:
            node1 = nop_graph_node

        extra_restrictions = set()  # use extra_restrictions to guarantee that if node1 is output, node2 isn't
        if node1.ir_node.opcode == lab1.OUTPUT_LEX:
            extra_restrictions.add(lab1.OUTPUT_LEX)

        if len(ready) >= 1:
            node2_idx = 0
            node2 = ready[node2_idx]

            while node2.ir_node.opcode not in FUNC1_ALLOWED and node2.ir_node.opcode not in extra_restrictions:
                # print(f"node2_idx: {node2_idx}")
                node2_idx += 1
                if node2_idx >= len(ready):
                    node2 = None
                    break
                node2 = ready[node2_idx]
            if node2 != None:
                ready.pop(node2_idx)
        if node2 == None:
            node2 = nop_graph_node

        # move them from ready to active
        node1.cycleToRetire = cycle + LEX_TO_LATENCY[node1.ir_node.opcode]
        node1.status = ACTIVE
        node2.cycleToRetire = cycle + LEX_TO_LATENCY[node2.ir_node.opcode]
        node2.status = ACTIVE
        active.add(node1)
        active.add(node2)

        # Print Instruction
        printInstruction(node1, node2)

        cycle += 1

        # find each op o in Active that retires
        retire_set = set()
        for node in active:
            if node.cycleToRetire == cycle:
                retire_set.add(node)
                node.status = RETIRED
                # add parents of retired node to Ready
                for edge in node.in_edges:
                    parent = edge[0]
                    # TODO: look into this some more
                    # print(f"child.out_edges: {child.out_edges}")

                    # print("parent.out_edges:", parent.out_edges)
                    # print(f"node: {node}, edge[1]: {edge[1]}")
                    # if (edge[1] != SERIALIZATION):  # serialization edges have been removed by early release
                    if len(edge) == 2:
                        parent.out_edges.remove((node, edge[1]))
                    if len(edge) > 2:
                        parent.out_edges.remove((node, edge[1], edge[2]))

                    # # ensure that node is still in out_edges before removing to avoid exception
                    # for out_edge_idx in range(len(parent.out_edges)):
                    #     if node == parent.out_edges[out_edge_idx][0]:
                    #         # print(f"node found in out_edges")
                    #         parent.out_edges.pop(out_edge_idx)
                    #         break

                    # insert the node into ready list (while maintaining ordering)
                    if len(parent.out_edges) == 0:
                        parent.status = READY
                        insertNode(ready, parent)
        # print("len(retire_set):", len(retire_set))
        # remove retired nodes from Active
        for node in retire_set:
            active.remove(node)

        # # for each multi-cycle operation in Active, check ops that depend on o for early releases
        # for node in active:
        #     for edge in node.in_edges:
        #         parent = edge[0]
        #         if edge[1] == SERIALIZATION:
        #             # print(f"parent.out_edges: {parent.out_edges}")

        #             # ensure that node is still in out_edges before removing to avoid exception
        #             for out_edge_idx in range(len(parent.out_edges)):
        #                 if node == parent.out_edges[out_edge_idx][0]:
        #                     # print(f"node found in out_edges")
        #                     parent.out_edges.pop(out_edge_idx)
        #                     break

        #             if len(parent.out_edges) == 0:
        #                 parent.status = READY
        #                 insertNode(ready, parent)
        #         # don't think we have to account for CONFLICT because you still have to wait until parent retires
        # print("len(ready):", len(ready))

def write_graphviz(nodes_arr):
    filename = "out.dot"
    try:
        with open("out.dot", 'w') as file:
            file.write("digraph testcase1\n{\n")

            for node in nodes_arr:
                file.write(f"{node.ir_node.lineno} [label=\"{node.ir_node.lineno}: {node.ir_node.printWithVRClean()} \n prio: {node.prio}\"];\n")

            for node in nodes_arr:
                tail = node.ir_node.lineno
                for edge in node.out_edges:
                    head = edge[0].ir_node.lineno  # recall that edge is formatted as (dest GraphNode, edge_type int)
                    edge_type = EDGE_TYPES[edge[1]]
                    edge_vr_string = "" # for data edges, there will be a 3rd element in edge which represents the vr
                    if len(edge) == 3:
                        edge_vr_string = f", vr{edge[2]}"
                    # edge_vr = edge[0].ir_node.op
                    file.write(f"{tail} -> {head} [label = \" {edge_type}{edge_vr_string}\"];\n")
            file.write("}")

    except IOError:
        print(f"ERROR: could not open file {filename} as the input file.", file=sys.stderr)
        exit(0)

def main():
    # HANDLE COMMAND LINE
    argc = len(sys.argv)
    filename = ""

    if argc < 2 or argc > 4:
        print("ERROR: Invalid number of arguments passed in. Syntax should be ./412alloc k filename [flag]", file=sys.stderr)
        print(helpMessage)
        exit(0)
    if argc == 2:
        if sys.argv[1] == "-h":
            print(helpMessage)
            return
        filename = sys.argv[1]
    if argc == 3:
        # TODO: implement helper flags
        filename = sys.argv[2]

    # PARSE
    # if filename can't be opened, lab1 will print error message and exit cleanly
    # tic = time.perf_counter()
    dummy, maxSR = lab1.parse(["lab1.py", filename]) # dummy is the head of the linked list
    # toc = time.perf_counter()
    # print(f"Parse took {toc - tic:0.4f} seconds")

    # RENAME
    # tic = time.perf_counter()
    maxLive, maxVR = lab2.rename(dummy, maxSR)
    # toc = time.perf_counter()
    # print(f"Rename took {toc - tic:0.4f} seconds")

    # CREATE DEPENDENCE GRAPH
    # tic = time.perf_counter()
    nodes_arr = create_dependence_graph(dummy)
    # toc = time.perf_counter()
    # print(f"Creating dependence graph took {toc - tic:0.4f} seconds")

    # Get roots and leaves
    # tic = time.perf_counter()
    root_set, leaf_set = get_roots_and_leaves(nodes_arr)
    # toc = time.perf_counter()
    # print(f"Getting roots and leaves took {toc - tic:0.4f} seconds")

    # ASSIGN PRIORITIES
    # tic = time.perf_counter()
    assign_priorities(nodes_arr, root_set)
    # toc = time.perf_counter()
    # print(f"Assigning priorities took {toc - tic:0.4f} seconds")

    # Construct .dot file for graphviz
    # write_graphviz(nodes_arr)

    # SCHEDULE
    # tic = time.perf_counter()
    schedule(leaf_set)
    # toc = time.perf_counter()
    # print(f"Scheduling took {toc - tic:0.4f} seconds")

if __name__ == "__main__": # if called by the command line, execute parse()
    main()


import rclpy
from example_interfaces.msg import Bool
from example_interfaces.srv import AddTwoInts
from rclpy.node import Node

SUBSCRIBER1_TOPIC = "/input_1"
SUBSCRIBER2_TOPIC = "/input_2"
PUBLISHER_TOPIC = "/output"
SERVICE_TOPIC = "/add"

class ExampleAdderNode(Node):
    def __init__(self):
        super().__init__("example_adder_node")  # type: ignore
        self.get_logger().info("Node started")

        self.subscriber1_ = self.create_subscription(
            Bool, SUBSCRIBER1_TOPIC, self.on_sub_1, 1
        )
        self.subscriber2_ = self.create_subscription(
            Bool, SUBSCRIBER2_TOPIC, self.on_sub_2, 1
        )
        self.publisher_ = self.create_publisher(Bool, PUBLISHER_TOPIC, 1)
        self.service = self.create_service(
            AddTwoInts, SERVICE_TOPIC, self.on_add_numbers
        )

    sub_1_value = False
    sub_2_value = False

    def on_sub_1(self, msg: Bool):
        self.sub_1_value = msg.data
        self.get_logger().info(f"Sub 1: {msg.data}")
        self.publish_result()

    def on_sub_2(self, msg: Bool):
        self.sub_2_value = msg.data
        self.get_logger().info(f"Sub 2: {msg.data}")
        self.publish_result()

    def on_add_numbers(
        self, request: AddTwoInts.Request, response: AddTwoInts.Response
    ):
        response.sum = request.a + request.b
        return response

    def publish_result(self):
        msg = Bool()
        msg.data = self.sub_1_value and self.sub_2_value
        self.get_logger().info(f"Publishing result: {msg.data}")
        self.publisher_.publish(msg)

def main(args=None):

    rclpy.init(args=args)
    example_adder_node = ExampleAdderNode()
    rclpy.spin(example_adder_node)
    example_adder_node.destroy_node()
    rclpy.shutdown()

if __name__ == "__main__":
    main()


import numpy as np
from keras.layers.core import Dense
from keras.models import Sequential

class ANN:
    def __init__(
        self, name, input_dim, output_dim, layers, activation, loss, optimizer
    ):
        model = Sequential()

        first = True
        for num_nodes in layers:
            if first:
                model.add(Dense(num_nodes, input_dim=input_dim, activation=activation))
                first = False
            else:
                model.add(Dense(num_nodes))
        model.add(Dense(output_dim, activation='linear'))
        model.compile(loss=loss, optimizer=optimizer)

        self.input_dim  = input_dim
        self.output_dim = output_dim
        self.name       = name
        self.model      = model

    # Supervised learning
    def train(self, X, y, batch_size):
        return self.model.fit(
            X, y,
            batch_size=batch_size,
            epochs=1,
            verbose=0
        )

    def predict(self, state):
        return self.model.predict(state)

    # Weights management
    def set(self, weights):
        self.model.set_weights(weights)

    def get(self):
        return self.model.get_weights()

    def dump(self):
        self.model.save_weights('.weights/{}.h5'.format(self.name), overwrite=True)

    def load(self):
        try:
            self.model.load_weights('.weights/{}.h5'.format(self.name))
        except IOError:
            self.model.load_weights('.weights/{}.h5'.format(self.name.replace('ann', 'target_ann')))


import sys
from collections import deque
N, L, R = map(int, sys.stdin.readline().split())
pan = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
# 연합이 될 수 있는지?를 확인하고 만약 된다면 하나로 묶어 인구수/칸의 개수를 구하고 각 칸에 값을 넣는다.
# 이를 연합이 될 수 없을 때까지 반복한다.
# 그 때 소요된 날짜를 출력한다.
q = deque()
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def bfs(x,y):
    q.append((x,y))
    union = []
    union.append((x,y))
    while q:
        a,b = q.popleft()
        for i in range(4):
            na = a + dx[i]
            nb = b + dy[i]
            if na>=N or nb>=N or nb<0 or na<0 or visited[na][nb]==1:
                continue
            if R>=abs(pan[a][b]-pan[na][nb])>=L:
                visited[na][nb] = 1
                q.append((na,nb))
                union.append((na,nb))
    if len(union)<=1:
        return 0
    result=sum(pan[a][b] for a,b in union)//len(union)
    for a,b in union:
        pan[a][b] = result
    return 1
day = 0
while 1:
    stop = 0
    visited = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if visited[i][j] == 0:
                visited[i][j] = 1
                stop += bfs(i,j)
    if stop==0:
        break
    day += 1
print(day)

백준/삼성SW역량테스트기출/16234인구이동.py


import sys
import os
dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append("%s/../../lib/" % dir)
import entity
import webutils
import serrcode

def GetCreateTableSQL():
    sql = """
        create table if not exists `Tbl_VUser` (
            `id` char(36) not null,
            `nickname` char(32) not null,
            `register_date` datetime not null,
            `last_login` datetime not null,
            `birthday` date not null,
            `description` varchar(128) default null,
            `icon` char(16) default null,
            primary key (`id`)
        );
    """
    return sql

class VUser(entity.Entity):
    def __init__(self, accessor, uid):
        entity.Entity.__init__(self, accessor)
        self.__conditions__["id"] = uid
        self.__keys__= set(("id", "nickname", "register_date", "last_login",
                "birthday", "description", "icon"))

    def Key(self):
        return "user_%s" % self.__conditions__["id"]

    def GetSource(self):
        if self.id:
            parts = self.id.split("_")
            if len(parts) > 0:
                return parts[0]
        return "unknown"

    def Validate(self):
        if self.IsEmpty(self.id) or self.IsEmpty(self.nickname):
            raise serrcode.VCityException(
                    "usr.id or usr.nickname can't be null")

if __name__ == "__main__":
    import unittest
    class MockAccessor:
        def Get(self, entity):
            entity.Set({
                "id" : "qq_test",
                "nickname" : "test_user"
                })

        def Set(self, entity):
            return

    class VUserTest(unittest.TestCase):
        def setUp(self):
            self.user = VUser(MockAccessor(), "qq_test")

        def test_Load(self):
            self.user.Load()
            self.assertEqual("qq_test", self.user.id)
            self.assertEqual("test_user", self.user.nickname)
            self.assertEqual("qq", self.user.GetSource())

        def test_Validate(self):
            self.user.nickname = ""
            self.user.id = "qq_test"
            try:
                self.user.Validate()
                self.assertTrue(False)
            except serrcode.VCityException:
                self.assertTrue(True)
            self.user.nickname = "name"
            self.user.Validate()

    unittest.main()


word1,word2=map(str,input().split())
count=0
for iter in range(0,len(word1)):
	if word1[iter]!=word2[iter]:
		count=count+1
	else:
		continue
if count==1:
	print("yes")
else:
	print("no")


'''
Created on 15 Aug 2011

@author: samgeen
'''

' TODO: CREATE POLYMORPHIC INTERFACE!!! '

import pyglet
from pyglet.gl import *
from pyglet.gl.glu import *

import numpy as np

#import EventHandler as Events
#from MouseHandler import MouseHandler
import math

from .Maths.Vect3 import Vect3
from .Maths.Quaternion import Rotation

from . import Events

class CameraController(object):
    def __init__(self, camera, window):
        self._lmb = False
        self._camera = camera
        self._window = window
        self._userInput = Events.UserInputHandler(self)
        self._window.push_handlers(self._userInput)

    def OnMouseMove(self, state):
        '''
        Parse mouse movement
        '''
        if self._lmb:
            self._camera.OnMouseMove(state)
        else:
            self._camera.ResetPos(state)

    def OnKeyboard(self, state):
        '''
        Register the pressing or releasing of a keyboard key
        This can be overridden by the concrete class if desired, or left inactive
        state - a dictionary of the mouse state:
                   {"button": button, "mod": modifier keys used, "pressed":True or False}
        '''
        # Escape
        pressed = state["pressed"]
        button = state["button"]

        # Q
        if button == pyglet.window.key.Q:
            self._camera.ZoomIn(pressed)
        # E
        if button == pyglet.window.key.E:
            self._camera.ZoomOut(pressed)

    def OnMouseButton(self, state):
        if state["button"] % 2 == pyglet.window.mouse.LEFT:
            self._lmb = state["pressed"] # True if pressed, False if not

class Camera(object):
    '''
    classdocs
    '''

    def __init__(self, window):
        '''
        Constructor
        '''
        zoomInit = 2.0
        self.__zoom = zoomInit
        self.__lookAt = Vect3(0.0,0.0,0.0)
        self.__position = Vect3(0.0,0.0,zoomInit)
        self.__up = Vect3(0.0,1.0,0.0)
        self.__right = Vect3(1.0,0.0,0.0) # This is reset during the view calculations
        self.__xangle = 0.0
        self.__yangle = 0.0
        self.__zoomSpeed = 1.30
        # Keep track of time for smooth moving of camera
        self._clock = pyglet.clock.Clock()
        self._time = self._clock.time()
        self._prevtime = self._time
        self._zoomIn = False
        self._zoomOut = False
        # Keyboard and mouse handler
        self._controller = CameraController(self, window)
        # PREVIOUS MOUSE POSITIONS
        self.__oldscrx = 0
        self.__oldscry = 0

    def ResetPos(self, state):
        '''
        Reset position when the user begins scrolling again
        '''
        x,y = state["pos"]
        self.__oldscrx = x
        self.__oldscry = y

    def OnMouseMove(self, state):
        '''
        Parse mouse movement
        '''
        x, y = state["pos"]
        # Good speed for a mouse
        rotateSpeed = 5e-2
        # Good speed for a wiimote
        rotateSpeed = 2e-2
        xangle = -(x - self.__oldscrx) * rotateSpeed
        yangle = (y - self.__oldscry) * rotateSpeed
        self.__oldscrx = x
        self.__oldscry = y
        self.Rotate((xangle, yangle))

    def Rotate(self, angles):
        self.__xangle += angles[0]
        self.__yangle += angles[1]
        # Find total angle
        angle = math.sqrt(angles[0]*angles[0] + angles[1]*angles[1])
        # Make sure the right-pointing vector is set properly (as cross product of up and lookAt)
        self.__right = -self.__up ^ self.__position.Normal()
        # Find direction the angle displacement is pointing
        displacement = self.__right*angles[0] + self.__up*angles[1]
        # Now find the vector perpendicular to that and the camera direction
        vector = -self.__position ^ displacement
        # Now ROTATE
        if vector.LengthSqrd() > 0:
            rotation = Rotation(angle, vector)
            self.__position = rotation.RotateVector(self.__position)
            self.__up = rotation.RotateVector(self.__up)
        # Done!
        Events.Redraw()

    def Zoom(self, zoom=None):
        '''
        Set zoom or return its current value (or both)
        '''
        if zoom:
            self.__position *= zoom / self.__zoom
            self.__zoom = zoom
        return self.__zoom

    def ZoomActive(self):
        '''
        HACK - Is the zoom currently active?
        '''
        return self._zoomIn or self._zoomOut

    def ZoomIn(self, on=True):
        '''
        Zoom in camera
        '''
        self._zoomIn = on

    def ZoomOut(self, on=True):
        '''
        Zoom in camera
        '''
        self._zoomOut = on

    def Draw(self):
        '''
        Update the display with the current camera view
        '''
        self._prevtime = self._time
        self._time = self._clock.time()
        dt = self._time - self._prevtime
        self._CalcZoom(dt)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45,1.0,0.01,10.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(self.__position[0], self.__position[1], self.__position[2],
                  self.__lookAt[0], self.__lookAt[1], self.__lookAt[2],
                  self.__up[0], self.__up[1], self.__up[2])

    def LookAt(self, point):
        '''
        Set point to look at
        '''
        self.__lookAt = point

    def Position(self, point=0):
        '''
        Set/get position of camera
        '''
        if type(point) == type(0):
            return self.__position
        else:
            self.__position = point

    def Up(self, direction):
        '''
        Give direction of up
        '''
        self.__up = direction

    def _CalcZoom(self, dt):
        # The zoom is a logarithmic zoom, i.e. zooms by factors of 10 in constant time
        zoomfact = self.__zoomSpeed ** dt
        if self._zoomIn:
            self.__zoom /= zoomfact
            self.__position /= zoomfact
        if self._zoomOut:
            self.__zoom *= zoomfact
            self.__position *= zoomfact


from PyQt5 import QtCore, QtWidgets, QtGui

from Item import Item
import Constant as c
from Tikzifyables.Labelable import Labelable
from Tikzifyables.DashPatternable import DashPatternable
from Tikzifyables.Colourable.LineColourable import LineColourable
from Tikzifyables.Colourable.FillColourable import FillColourable

class Point(Item, Labelable, DashPatternable, LineColourable, FillColourable):
    def __init__(self, item):
        """Construct Point."""
        Item.__init__(self, item)
        if item is None:
            self.dictionary_builder(None, "", "")
        Labelable.__init__(self, self.item)
        DashPatternable.__init__(self, self.item)
        LineColourable.__init__(self, self.item)
        FillColourable.__init__(self, self.item)
        self.__canvas_x = 0
        self.__canvas_y = 0

    def get_canvas_coordinates(self):
        return self.__canvas_x, self.__canvas_y

    def set_canvas_coordinates(self, x, y):
        self.__canvas_x = x
        self.__canvas_y = y

    def __tikzify_marker(self):
        extra = ''
        if self.item["marker"]["shape"] == 'regular polygon':
            extra = 'regular polygon sides=' + str(self.item["marker"]["shape_number"])
        elif self.item["marker"]["shape"] == 'star':
            extra = f'star points={self.item["marker"]["shape_number"]}, star point ratio={self.item["marker"]["ratio"]}'

        options = [
            self.item["marker"]["shape"],
            'minimum size=' + str(self.item["marker"]["size"]),
            'inner sep=' + str(self.item["marker"]["inner_sep"]),
            '' if self.item["marker"]["shape"] == 'circle' or self.item["marker"]["rounded_corners"] == c.Point.Default.Marker.ROUNDED_CORNERS else f'rounded corners={self.item["marker"]["rounded_corners"]}pt',
            '' if self.item["marker"]["text_width"] == c.Point.Default.Marker.TEXT_WIDTH else 'text width=' + str(self.item["marker"]["text_width"]),
        ]
        if extra:
            options.append(extra)
        options = filter(bool, options)

        return ', '.join(options)

    def tikzify_node(self):
        options = [
            f'draw={c.Point.Default.Line_Colour.NAME}' if not bool(self.tikzify_line_colour()) else f'draw={self.tikzify_line_colour()}',
            f'fill={c.Point.Default.FillColor.NAME}' if not bool(self.tikzify_fill_colour()) else f'fill={self.tikzify_fill_colour()}',
            '' if self.item["line"]["line_width"] == c.Point.Default.LINE_WIDTH else f'line width={self.item["line"]["line_width"]}',
            self.__tikzify_marker(),
            self.tikzify_dash()
        ]
        options = filter(bool, options)
        return '\\node[%s] (%s) at (%s) {%s};' % (', '.join(options), self.get_id(), self.get_id(), self.item["marker"]["text"])

    def tikzify(self, items=None):
        raise NotImplementedError

    def draw_on_canvas(self, items, scene, colour=QtCore.Qt.darkMagenta):
        radius = 6
        graphics_point = QtWidgets.QGraphicsEllipseItem(self.__canvas_x-radius,self.__canvas_y-radius, 2*radius, 2*radius)
        graphics_point.setBrush(QtGui.QBrush(colour))
        scene.addItem(graphics_point)
        if scene.show_canvas_labels:
            text = QtWidgets.QGraphicsTextItem(self.item["id"])
            text.setPos(self.__canvas_x,self.__canvas_y)
            text.setDefaultTextColor (colour)
            font = text.font()
            font.setPointSize(12)
            font.setWeight(600)
            text.setFont(font)
            # text.setTextWidth(400)
            scene.addItem(text)

    def distance_sqr(self, x, y, items):
        return (self.__canvas_x-x) ** 2 + (self.__canvas_y-y) ** 2

    def next_id_func(self, definition, iter_counter):
        return chr(ord('A') + iter_counter % 26) + (iter_counter // 26) * '\''

    def dictionary_builder(self, definition, id_, sub_type=None):
        dictionary = {}
        dictionary["id"] = id_
        dictionary["type"] = 'point'
        dictionary["sub_type"] = sub_type
        dictionary["show"] = True
        dictionary["definition"] = definition
        dictionary["marker"] = {}
        dictionary["marker"]["size"] = c.Point.Default.Marker.SIZE
        dictionary["marker"]["shape"] = c.Point.Default.Marker.SHAPE
        dictionary["marker"]["shape_number"] = c.Point.Default.Marker.SHAPE_NUMBER
        dictionary["marker"]["ratio"] = c.Point.Default.Marker.RATIO
        dictionary["marker"]["inner_sep"] = c.Point.Default.Marker.INNER_SEP
        dictionary["marker"]["rounded_corners"] = c.Point.Default.Marker.ROUNDED_CORNERS
        dictionary["marker"]["text"] = c.Point.Default.Marker.TEXT
        dictionary["marker"]["text_width"] = c.Point.Default.Marker.TEXT_WIDTH
        dictionary["line"] = {}
        dictionary["line"]["line_width"] = c.Point.Default.LINE_WIDTH
        dictionary["line"]["colour"] = {}
        dictionary["line"]["colour"]["name"] = c.Point.Default.Line_Colour.NAME
        dictionary["line"]["colour"]["mix_with"] = c.Point.Default.Line_Colour.MIX_WITH
        dictionary["line"]["colour"]["mix_percent"] = c.Point.Default.Line_Colour.MIX_RATIO
        dictionary["line"]["colour"]["strength"] = c.Point.Default.Line_Colour.STRENGTH
        dictionary["line"]["dash"] = {}
        dictionary["line"]["dash"]["stroke"] = c.Point.Default.LINE_DASH_STROKE
        dictionary["line"]["dash"]["custom_pattern"] = c.Point.Default.LINE_DASH_CUSTOM
        dictionary["fill"] = {}
        dictionary["fill"]["colour"] = {}
        dictionary["fill"]["colour"]["name"] = c.Point.Default.Fill_Colour.NAME
        dictionary["fill"]["colour"]["mix_with"] = c.Point.Default.Fill_Colour.MIX_WITH
        dictionary["fill"]["colour"]["mix_percent"] = c.Point.Default.Fill_Colour.MIX_RATIO
        dictionary["fill"]["colour"]["strength"] = c.Point.Default.Fill_Colour.STRENGTH
        dictionary["label"] = {}
        dictionary["label"]["show"] = c.Point.Default.Label.SHOW
        dictionary["label"]["text"] = f'${id_}$'
        dictionary["label"]["text"] = c.Point.Default.Label.TEXT
        dictionary["label"]["anchor"] = c.Point.Default.Label.ANCHOR
        dictionary["label"]["offset"] = c.Point.Default.Label.OFFSET

        self.item = dictionary

#


from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
from scipy.ndimage import affine_transform
import haaris
import hog
import numpy as np
import cv2 as cv
import time
def key_des(img,key,desc_func,patch_size=16):
    keypoint_desc = []
    for i,point in enumerate(key):
        x,y = point
        patch = img[x-patch_size//2:x+int(np.ceil(patch_size/2)),y-patch_size//2:y+int(np.ceil(patch_size/2))]
        # print(patch.shape)
        description = desc_func(patch)
        # if description=='0':
        #     print(x,y)
        #     print(patch.shape)
        keypoint_desc.append(description)
    return np.array(keypoint_desc)

def description_matches(desc1,desc2,threshold=0.5):
    distance_array = cdist(desc1,desc2)
    matches = []
    i=0
    for each_distance_list in distance_array:
        arg_list = np.argsort(each_distance_list)
        index1 = arg_list[0]
        index2 = arg_list[1]
        if each_distance_list[index1] / each_distance_list[index2] <= threshold:
            matches.append([i,index1])
        i+=1
    return np.array(matches)
def plot_matches(ax,image1,image2,keypoint1,keypoint2,matches):
    H1,W1 = image1.shape
    H2,W2 = image2.shape
    if H1>H2:
        new_image2 = np.zeros((H1,W2))
        new_image2[:H2,:] = image2
        image2 = new_image2
    if H1<H2:
        new_image1 = np.zeros((H2,W1))
        new_image1[:H1,:]=image1
        image1 = new_image1
    image = np.concatenate((image1,image2),axis=1)
    ax.scatter(keypoint1[:,1],keypoint1[:,0],facecolors='none',edgecolors='k')
    ax.scatter(keypoint2[:,1]+image1.shape[1],keypoint2[:,0],facecolors='none',edgecolors='k')
    ax.imshow(image,interpolation='nearest',cmap='gray')
    for one_match in matches:
        index1 = one_match[0]
        index2 = one_match[1]
        color = np.random.rand(3)
        ax.plot((keypoint1[index1,1],keypoint2[index2,1] + image1.shape[1]),
                (keypoint1[index1,0],keypoint2[index2,0]),'-',color=color)
def ransac(keypoint1,keypoint2,matches,n_iters=200,threshold=20):
    N=matches.shape[0]
    # 横向合并，类似于ax+b=0变换为ax=0给系数矩阵加一列1变为增广矩阵
    match_keypoints1 = np.hstack((keypoint1[matches[:,0]],np.ones((N,1))))
    match_keypoints2 = np.hstack((keypoint2[matches[:,1]],np.ones((N,1))))
    n_samples=int(N*0.2)
    n_max = 0
    H=np.zeros((3,3))
    robust_matches=matches
    for i in range(n_iters):
        # 生成0-100的20个下标，不重复
        random_index = np.random.choice(N,n_samples,replace=False)
        p1_choice = match_keypoints1[random_index]
        p2_choice = match_keypoints2[random_index]
        # print('shapeff')
        # print(p1_choice.shape)
        # print(p2_choice.shape)
        # w.Tp2=p1
        '''
          [[a,b,c],[d,e,f],[0,0,1]].T
        '''
        H_choice = np.linalg.pinv(p2_choice) @ p1_choice
        # print(H_choice)
        H_choice[:,2] = np.array([0,0,1])
        # print(H_choice)
        # print('shape:')
        # print('H_choice:',H_choice.shape)
        p1_test = match_keypoints2 @ H_choice
        diff = np.sum((match_keypoints1[:,:2]-p1_test[:,:2])**2,axis=1)
        # print('diff:')
        # print(diff.shape)
        index=np.where(diff<=threshold)[0]
        # print('where:')
        # print(np.where(diff<=threshold))
        n_index = index.shape[0]
        if n_index>n_max:
            H=H_choice
            robust_matches=matches[index]
            n_max=n_index
    return H,robust_matches
# 大体上根据参考矩阵和放射变换，将其他图像转换到参考图像
def get_output_space(image_ref,images,transforms):
    H_ref , W_ref = image_ref.shape
    corner_ref = np.array([[0,0,1],[H_ref,0,1],[0,W_ref,1],[H_ref,W_ref,1]])

    # print('shape')
    # print(corner_ref)
    all_corners=[corner_ref]
    if len(images) != len(transforms):
        print('The size of images and transforms does\'t match')
    for i in range(len(images)):
        H,W = images[i].shape
        corner = np.array([[0,0,1],[H,0,1],[0,W,1],[H,W,1]]) @ transforms[i]
        all_corners.append(corner)
    all_corners = np.vstack(all_corners)
    # print(all_corners.shape)
    max_corner = np.max(all_corners,axis=0)
    # print(max_corner)
    min_corner = np.min(all_corners,axis=0)
    # print(min_corner)
    out_space = np.ceil((max_corner - min_corner)[:2]).astype(int)
    # print(out_space)
    offset = min_corner[:2]
    # print(offset)
    return out_space,offset

def warp_image(image, H, output_shape, offset):
     H_invT = np.linalg.inv(H.T)
     matrix = H_invT[:2,:2]
     o = offset+H_invT[:2,2]
     image_warped = affine_transform(image,matrix,o,output_shape,cval=-1)
     return image_warped
def linear_blend(image1_warped,image2_warped):
    merged = image1_warped + image2_warped
    H , W = image1_warped.shape
    image1_mask = (image1_warped!=0)
    image2_mask = (image2_warped!=0)
    left_margin = np.argmax(image2_mask,axis=1)
    right_margin = W - np.argmax(np.fliplr(image1_mask),axis=1)
    for i in range(H):
        k = right_margin[i] - left_margin[i]
        for j in range(k):
            alpha = j / (k - 1)
            # alpha=0.5
            merged[i,left_margin[i]+j] = (1-alpha) * image1_warped[i,left_margin[i]+j]+\
            alpha * image2_warped[i,left_margin[i]+j]
    return merged
img1=cv.imread('0102.jpg',0)
img2=cv.imread('0304.jpg',0)
s=time.perf_counter()
plt.subplot(121)
plt.imshow(img1,cmap='gray')
# plt.scatter(img1key1[:,1],img1key1[:,0],marker='o')
plt.subplot(122)
plt.imshow(img2,cmap='gray')
# plt.scatter(img2key2[:,1],img2key2[:,0],marker='o')
plt.show()
img1key1=haaris.harris(img1)
img2key2=haaris.harris(img2)
# print(img1key1.shape)
# print(img2key2.shape)
plt.subplot(121)
plt.imshow(img1,cmap='gray')
plt.scatter(img1key1[:,1],img1key1[:,0],marker='o')
plt.subplot(122)
plt.imshow(img2,cmap='gray')
plt.scatter(img2key2[:,1],img2key2[:,0],marker='o')
plt.show()
des2=key_des(img2,img2key2,hog.hog_des)
des1=key_des(img1,img1key1,hog.hog_des)

# print('df')
# print(des1.shape)
# print(des2.shape)
hog_m=description_matches(des1,des2,threshold=0.7)
H,hog_m=ransac(img1key1,img2key2,hog_m)

e=time.perf_counter()
print(e-s)
fig,ax = plt.subplots(1,1)
ax.axis('off')
plot_matches(ax,img1,img2,img1key1,img2key2,hog_m)

plt.show()
output_shape, offset = get_output_space(img1, [img2], [H])

image1_warped = warp_image(img1,np.eye(3),output_shape,offset)
image1_mask = (image1_warped != -1)
image1_warped[~image1_mask]=0

image2_warped = warp_image(img2,H,output_shape,offset)
image2_mask = (image2_warped != -1)
image2_warped[~image2_mask]=0

plt.figure()
plt.subplot(121)
plt.imshow(image1_warped,cmap='gray')
plt.subplot(122)
plt.imshow(image2_warped,cmap='gray')
plt.show()

merged = image1_warped + image2_warped

overlap = np.maximum(image1_mask*1+image2_mask,1)
merged = merged / overlap

plt.figure()
plt.imshow(merged,cmap='gray')
plt.show()

merged = linear_blend(image1_warped,image2_warped)
plt.figure()
plt.imshow(merged,cmap='gray')
# plt.savefig('0304.jpg')
cv.imwrite('1234.jpg',merged)
plt.show()


# https://www.acmicpc.net/problem/1388
def dfs(x, y):
    if floor[x][y] == '-':
        floor[x][y] = 1
        for _y in (1, -1):
            ty = y + _y
            if (0 < ty < m) and floor[x][ty] == '-':
                dfs(x, ty)
    elif floor[x][y] == '|':
        floor[x][y] = 1
        for _x in (1, -1):
            tx = x + _x
            if (0 < tx < n) and floor[tx][y] == '|':
                dfs(tx, y)

n, m = map(int, input().split())
floor = list()
for _ in range(n):
    floor.append(list(input()))

result = 0
for i in range(n):
    for j in range(m):
        if floor[i][j] in ('-', '|'):
            dfs(i, j)
            result += 1
print(result)


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

def load():
    from keras.applications import MobileNet
    model = MobileNet(weights = 'imagenet',include_top = False,input_shape=(224,224,3))
    for layers in model.layers:
        print(layers.__class__.__name__)
    for layers in model.layers:
        layers.trainable = False
    for (i,layer) in enumerate(model.layers):
        print(str(i)+" "+layer.__class__.__name__,layer.trainable)
    return model

def newmodel(model):
    from keras.layers import Dense,Activation, Flatten, GlobalAveragePooling2D
    top_model = model.output
    top_model = GlobalAveragePooling2D()(top_model)
    top_model = Dense(1024,activation='relu')(top_model)
    top_model = Dense(1024,activation='relu')(top_model)
    top_model = Dense(512,activation='relu')(top_model)
    top_model = Dense(2,activation='softmax')(top_model)
    return top_model

def final_model(model,modify_mod):
    from keras.models import Model
    fin_model = Model(inputs=model.input,outputs=modify_mod)
    return(fin_model)

def data_gen(train_data,validation_data):
    from keras.preprocessing.image import ImageDataGenerator

    train_datagen = ImageDataGenerator(rescale=1./255,
                                       rotation_range=45,
                                       width_shift_range=0.3,
                                       height_shift_range=0.3,
                                       horizontal_flip=True,
                                       fill_mode='nearest')
    validation_datagen = ImageDataGenerator(rescale=1./255)
    batch_size=32
    train_generator = train_datagen.flow_from_directory(train_data,
                                                        target_size=(224,224),
                                                        batch_size=batch_size,
                                                        class_mode='categorical')
    validation_generator = validation_datagen.flow_from_directory(validation_data,
                                                                 target_size=(224,224),
                                                                 batch_size=batch_size,
                                                                 class_mode='categorical')
    return(train_generator,validation_generator)

def calc_epoch(train_generator,validation_generator,fin_model):
    from keras.optimizers import RMSprop
    fin_model.compile(optimizer=RMSprop(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
    fin_model.fit_generator(train_generator,
                        steps_per_epoch= 200,
                        epochs=1,
                        validation_data=validation_generator,
                        validation_steps= 50)

def result():
    model = load()
    modify_mod = newmodel(model)
    fin_model = final_model(model,modify_mod)
    train_data = '/root/train'
    validation_data = '/root/validation'
    train_generator,validation_generator=data_gen(train_data,validation_data)
    calc_epoch(train_generator,validation_generator,fin_model)

result()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('twitter', '0010_auto_20141201_1848'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='tweet',
            name='approved',
        ),
        migrations.AddField(
            model_name='tweet',
            name='status',
            field=models.CharField(default='PE', choices=[('PE', 'pending'), ('PO', 'posted'), ('RE', 'rejected')], max_length=2),
            preserve_default=True,
        ),
    ]


import argparse
import glob
import numpy as np
import os
import PIL
import random
import shutil
import time
import types
import warnings

import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.multiprocessing as mp
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision.models as models
from efficientnet_pytorch import EfficientNet

from robusta.batchnorm import bn
from robusta.batchnorm import stages
from robusta.models.fixup import fixup_resnet50
from robusta.models.resnet_gn import resnet50 as resnet_50_gn
from robusta.models.resnet_gn import resnet152 as resnet_152_gn
from robusta.models.resnet_gn import resnet101 as resnet_101_gn

import config
from meters import AverageMeter, ProgressMeter, get_accuracy

try:
    import tqdm.tqdm as tqdm
except ImportError:
    tqdm = None

def print_version():
    for name, val in globals().items():
        if isinstance(val, types.ModuleType):
            try:
                print(f"| {val.__name__} version: {imp.__version__}")
            except Exception:
                continue

def main(argv):
    print_version()

    args = config.parse_args(argv)
    print("| Parsed arguments:", args)

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
        warnings.warn(
            "You have chosen to seed training. "
            "This will turn on the CUDNN deterministic setting, "
            "which can slow down your training considerably! "
            "You may see unexpected behavior when restarting "
            "from checkpoints."
        )

    if args.gpu is not None:
        warnings.warn(
            "You have chosen a specific GPU. This will completely "
            "disable data parallelism."
        )

    ngpus_per_node = torch.cuda.device_count()
    print(f"| Using {ngpus_per_node} gpus.")
    return main_worker(args.gpu, ngpus_per_node, args)

def main_worker(gpu, ngpus_per_node, args):
    args.gpu = gpu

    if args.gpu is not None:
        print("| Use GPU: {} for training".format(args.gpu))

    # create model
    if args.arch.startswith("resnext") and args.arch.endswith("wsl"):
        valid_options = [
            "resnext101_32x8d_wsl",
            "resnext101_32x16d_wsl",
            "resnext101_32x32d_wsl",
            "resnext101_32x48d_wsl",
        ]
        assert args.arch in valid_options
        model = torch.hub.load("facebookresearch/WSL-Images", args.arch)
    else:
        if args.pretrained:
            print("| => using pre-trained model '{}'".format(args.arch))
            if "efficientnet" in args.arch:
                model = EfficientNet.from_pretrained(args.arch)
            elif "fixup_resnet50" in args.arch:
                model = fixup_resnet50()
            elif "resnet50_gn" in args.arch:
                model = resnet_50_gn()
            elif "resnet101_gn" in args.arch:
                model = resnet_101_gn()
            elif "resnet152_gn" in args.arch:
                model = resnet_152_gn()
            else:
                model = models.__dict__[args.arch](pretrained=True)
        else:
            if "efficientnet" in args.arch:
                raise NotImplementedError(
                    "Using a not pretrained Efficient Net is not supported."
                )
            elif "resnet50_gn" in args.arch:
                model = resnet_50_gn()
            elif "resnet101_gn" in args.arch:
                model = resnet_101_gn()
            elif "resnet152_gn" in args.arch:
                model = resnet_152_gn()
            else:
                print("| => creating model '{}'".format(args.arch))
                model = models.__dict__[args.arch]()

    if args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = torch.nn.DataParallel(model).cuda()

        # DataParallel will divide and allocate batch_size to all available GPU
        if args.arch.startswith("alexnet") or args.arch.startswith("vgg"):
            model.features = torch.nn.DataParallel(model.features)
            model.cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss()

    if args.gpu is not None:
        criterion = criterion.cuda(args.gpu)

    # optionally evaluate previous model
    if args.resume != "":
        print('Loading model checkpoint')
        checkpoint = torch.load(args.resume)
        model.load_state_dict(checkpoint["state_dict"])

    # cudnn.benchmark = True

    # Data loading code
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
    )
    T = []
    if args.resize_and_crop and "efficientnet" not in args.arch:
        T.extend(
            [
                transforms.Resize(args.resizepar),
                transforms.CenterCrop(args.croppar),
            ]
        )
    if "efficientnet" in args.arch:
        image_size = EfficientNet.get_image_size(args.arch)
        T.extend(
            [
                transforms.Resize(image_size, interpolation=PIL.Image.BICUBIC),
                transforms.CenterCrop(image_size),
            ]
        )
    T.extend([transforms.ToTensor(), normalize])
    print(T)

    fnames = glob.glob(os.path.join(args.imagenet_path, "*"))
    print(f"Dataset at {args.imagenet_path}, {len(fnames)}")
    if len(fnames) == 1000:
        print(f"| Use imagenet folder from {args.imagenet_path}")
        dataset = datasets.ImageFolder(
            args.imagenet_path, transforms.Compose(T))
    else:
        print(f"| Use subfolders in {args.imagenet_path}")
        print("|" + "\n|".join(fnames))
        dataset = torch.utils.data.ConcatDataset(
            [datasets.ImageFolder(path, transforms.Compose(T))
                for path in fnames]
        )

    val_loader = torch.utils.data.DataLoader(
        dataset,
        batch_size=args.test_batch_size,
        shuffle=not args.no_shuffle,
        num_workers=args.workers,
        pin_memory=True,
    )
    print("| Shuffling: ", not args.no_shuffle)

    Evaluation.gpu = args.gpu
    evaluate = Evaluation(model, criterion, args)
    emissions = evaluate(val_loader)

    top1, top5, loss = \
        emissions.top1.avg, emissions.top5.avg, emissions.losses.avg
    print(f"| Finished eval with: top-1 {top1}; top-5 {top5}; loss {loss}")

    return emissions
    # emissions.finalize().save(args.emission_path, args=args)

class Emissions:
    """ Experiment Outputs """

    def __init__(self, num_batches):
        self.num_batches = num_batches
        self.init_metrics()

    def init_metrics(self):
        self.targets = []
        self.predictions = []
        self.batch_time = AverageMeter("Time", ":6.3f")
        self.losses = AverageMeter("Loss", ":.4e")
        self.top1 = AverageMeter("Acc@1", ":6.2f")
        self.top5 = AverageMeter("Acc@5", ":6.2f")
        self.progress = ProgressMeter(
            self.num_batches,
            [self.batch_time, self.losses, self.top1, self.top5],
            prefix="| Test: ",
        )
        self.finalized = False

    def update_metrics(self, output, target, loss):
        assert not self.finalized
        acc1, acc5 = get_accuracy(output, target, topk=(1, 5))
        self.losses.update(loss.item(), output.size(0))
        self.top1.update(acc1[0].cpu(), output.size(0))
        self.top5.update(acc5[0].cpu(), output.size(0))

    def append(self, output, targets, loss, time=0):
        assert len(output) == len(targets)
        assert not self.finalized

        self.batch_time.update(time)
        self.update_metrics(output, targets, loss)

        self.predictions.append(output.detach().cpu().numpy())
        self.targets.append(targets.detach().cpu().numpy())

    def finalize(self):
        assert not self.finalized
        # assert self.num_batches == len(self.targets)
        # assert self.num_batches == len(self.predictions)

        self.predictions = np.concatenate(self.predictions, axis=0)
        self.targets = np.concatenate(self.targets, axis=0)
        self.finalized = True
        return self

    def save(self, fname, **kwargs):
        assert self.finalized
        assert not os.path.exists(fname)
        np.savez(
            fname,
            top1=self.top1.avg,
            top5=self.top5.avg,
            loss=self.losses.avg,
            predictions=self.predictions,
            targets=self.targets,
            **kwargs,
        )
        return self

class Evaluation:
    """ Evaluation loop """

    def __init__(self, model, criterion, args):
        super(Evaluation, self).__init__()

        self.args = args
        self.model = model
        self.criterion = Evaluation.to_device(criterion)

    gpu = None
    def to_device(arg):
        if Evaluation.gpu is not None:
            return arg.cuda(Evaluation.gpu, non_blocking=True)
        else:
            return arg

    def iterate(self, loader):
        if self.args.tqdm and tqdm is not None:
            return tqdm(enumerate(loader))
        return enumerate(loader)

    def use_train_statistics(self, module):
        if isinstance(module, nn.BatchNorm2d):
            module.train()

    @property
    def elapsed_time(self):
        """ return elapsed time since last call """
        if not hasattr(self, "_end"):
            self._end = time.time()
        span = time.time() - self._end
        self._end = time.time()
        return span

    def select_ablations(self):
        """ Check args for ablation settings """
        if self.args.ema_batchnorm:
            print(
                "| Collecting statistics during test time with exponential \
                    moving averaging. Experimental version from 30-04"
            )
            bn.adapt(self.model)
            self.warmup_batches = (
                self.args.ema_warmup_samples // self.args.test_batch_size
            )
            assert self.warmup_batches > 0
            return

        if self.args.adapt_mean or self.args.adapt_var:
            print(
                f"| Adapting mean[{self.args.adapt_mean}] and \
                    var[{self.args.adapt_var}]"
            )
            bn.adapt_parts(self.model,
                           self.args.adapt_mean, self.args.adapt_var)
            return

        if self.args.adapt_stage is not None:
            print(f"| Adapting only model stage {self.args.adapt_stage}")
            stages.choose_one_adaptation(self.model, self.args.adapt_stage)
            return

        if self.args.leave_stage is not None:
            print(f"| Adapting all but model stage {self.args.adapt_stage}")
            stages.leave_one_out_adaptation(self.model, self.args.leave_stage)
            return

        if self.args.adapt_prior is not None or \
                self.args.adapt_prior_bsz is not None:

            assert self.args.adapt_prior is None or \
                self.args.adapt_prior_bsz is None

            if self.args.adapt_prior_bsz is not None:
                n = self.args.test_batch_size
                N = self.args.adapt_prior_bsz
                setattr(self.args, "adapt_prior", float(N) / float(N + n))
            print(
                f"| Using a prior on the statistics with \
                    lambda = {self.args.adapt_prior}"
            )
            bn.adapt_bayesian(self.model, self.args.adapt_prior,
                              Evaluation.to_device)
            return

    def __call__(self, val_loader):
        print("| Start evaluation")

        os.system("git log -n1 --oneline")
        self.warmup_batches = 0
        self.model.eval()
        if self.args.train_mode_during_eval:
            print("| Using model in train() mode")
            self.model.apply(self.use_train_statistics)

            assert self.args.ema_batchnorm is False, \
                "--ema-batchnorm mode does not make sense with \
                    --train-mode-during-eval"
        else:
            print("| Using model in eval() mode")
            self.model.eval()
            self.select_ablations()

        emissions = Emissions(len(val_loader))
        with torch.no_grad():
            _ = self.elapsed_time

            if self.warmup_batches > 0:
                print(f"| Starting warmup for a total of \
                    {self.warmup_batches} batches")
                for i, (images, target) in self.iterate(val_loader):
                    if i > self.warmup_batches:
                        break
                    images = Evaluation.to_device(images)
                    output = self.model(images)

            print("| Starting evaluation")
            for i, (images, target) in self.iterate(val_loader):
                images = Evaluation.to_device(images)
                target = Evaluation.to_device(target)
                output = self.model(images)
                loss = self.criterion(output, target)

                emissions.append(output, target, loss, self.elapsed_time)
                if i % self.args.print_freq == 0:
                    emissions.progress.display(i)
                    if self.args.dry_run:
                        break

        return emissions

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])


import unittest
from ath import tokenizer

class TokenizerTest(unittest.TestCase):
    def check_tokenize(self, code, itype, value, position):
        t = tokenizer.Tokenizer(code)
        result = t.tokenize()

        for i, tok in enumerate(result):
            print(str(tok) + '\n')
            self.assertEqual(tok.itype, itype[i])
            self.assertEqual(tok.value, value[i])
            self.assertEqual(tok.position, position[i])

    def test_declaration_nums(self):
        code = "var = 5;"
        itype = ["identifier", "equals", "integer", "scolon"]
        value = ["var", "=", "5", ";"]
        position = [(0,0), (0,4), (0,6), (0,7)]
        self.check_tokenize(code, itype, value, position)

        code = "num1 = 123;"
        itype = ["identifier", "equals", "integer", "scolon"]
        value = ["num1", "=", "123", ";"]
        position = [(0,0), (0,5), (0,7), (0,10)]
        self.check_tokenize(code, itype, value, position)

        code = "var=5;"
        itype = ["identifier", "equals", "integer", "scolon"]
        value = ["var", "=", "5", ";"]
        position = [(0,0), (0,3), (0,4), (0,5)]
        self.check_tokenize(code, itype, value, position)

    def test_declaration_strings(self):
        code = "str = >hello=;"
        itype = ["identifier", "equals", "string", "scolon" ]
        value = ["str", "=", ">hello=", ";"]
        position = [(0,0), (0,4), (0,6), (0,13)]
        self.check_tokenize(code, itype, value, position)

        code = "> hello, world!\\n ==> ;"
        itype = ["string", "print", "scolon"]
        value = ["> hello, world!\\n =", "=>", ";"]
        position = [(0,0), (0,19), (0,22)]
        self.check_tokenize(code, itype, value, position)

    def test_function_declaration(self):
        code = """
ACT func() {
    APPEARIFY 0;
}
"""
        itype = ["act", "identifier", "oparen", "cparen",
                "obrac", "return", "integer", "scolon",
                "cbrac"]
        value = ["ACT", "func", "(", ")", "{", "APPEARIFY",
                "0", ";", "}"]
        position = [(1,0), (1,4), (1,8), (1,9), (1,11), (2,4),
                    (2,14), (2,15), (3,0)]
        self.check_tokenize(code, itype, value, position)

    def test_relational_opperators(self):
        code = """
a == b;
a != b;
a >= b;
a <= b;
"""
        itype = ["identifier", "rop", "identifier", "scolon",
                "identifier", "rop", "identifier", "scolon",
                "identifier", "rop", "identifier", "scolon",
                "identifier", "rop", "identifier", "scolon",]
        value = ["a", "==", "b", ";", "a", "!=", "b", ";",
                "a", ">=", "b", ";", "a", "<=", "b",";"]
        position = [(1,0), (1,2), (1,5), (1,6),
                    (2,0), (2,2), (2,5), (2,6),
                    (3,0), (3,2), (3,5), (3,6),
                    (4,0), (4,2), (4,5), (4,6)]
        self.check_tokenize(code, itype, value, position)

    def test_logical_opperators(self):
        code = """
a && b;
a || b;
a = !a;
"""
        itype = ["identifier", "lop", "identifier", "scolon",
                "identifier", "lop", "identifier", "scolon",
                "identifier", "equals", "lop", "identifier", "scolon"]
        value = ["a", "&&", "b", ";", "a", "||", "b", ";",
                "a", "=", "!", "a", ";" ]
        position = [(1,0), (1,2), (1,5), (1,6),
                    (2,0), (2,2), (2,5), (2,6),
                    (3,0), (3,2), (3,4), (3,5), (3,6)]
        self.check_tokenize(code, itype, value, position)

    def test_loops(self):
        code = """
u1 = ALIVE;
u1.lifespan = HE_IS_ALREADY_HERE;

~ATH(u1) {
    > This loop will run forever. ==> ;
}EXECUTE(NULL);
THIS.DIE();
"""
        itype = ["identifier", "equals", "identifier", "scolon",
                "identifier", "dot", "identifier", "equals", "identifier",
                "scolon", "loop", "oparen", "identifier", "cparen", "obrac",
                "string", "print", "scolon", "cbrac", "execute", "oparen",
                "identifier", "cparen", "scolon", "end", "scolon"]
        value = ["u1", "=", "ALIVE", ";", "u1", ".", "lifespan", "=",
                "HE_IS_ALREADY_HERE", ";", "~ATH", "(", "u1", ")", "{",
                "> This loop will run forever. =", "=>", ";", "}",
                "EXECUTE", "(", "NULL", ")", ";", "THIS.DIE()", ";"]
        position = [(1,0), (1,3), (1,5), (1,10), (2,0), (2,2), (2,3),
                    (2,12), (2,14), (2,32), (4,0), (4,4), (4,5), (4,7),
                    (4,9), (5,4), (5,35), (5,38), (6,0), (6,1), (6,8),
                    (6,9), (6,13), (6,14), (7,0), (7,10)]
        self.check_tokenize(code, itype, value, position)

    def test_function_factorial(self):
        code = """
ACT fac(x) {
    u1 = ALIVE;
    u1.DIE_UNLESS( x >= 1 );
    ~ATH(u1) {
        APPEARIFY REPLICATE(x, frac(SUBIFY(x, 1)));
        u1.DIE();
    }EXECUTE(u1.REVIVE());
    ~ATH(!u1) {
        APPEARIFY 1;
        u1.REVIVE();
    }EXECUTE(u1.DIE());
}
result = fac(6);
> The result is: $(result) ==> ;
THIS.DIE();
"""
        itype = ["act", "identifier", "oparen", "identifier", "cparen",
                "obrac", "identifier", "equals", "identifier", "scolon",
                "identifier", "dot", "identifier", "oparen", "identifier",
                "rop", "integer", "cparen", "scolon", "loop", "oparen",
                "identifier", "cparen", "obrac", "return", "identifier",
                "oparen", "identifier", "cama", "identifier", "oparen",
                "identifier", "oparen", "identifier", "cama", "integer",
                "cparen", "cparen", "cparen", "scolon", "identifier",
                "dot", "identifier", "oparen", "cparen", "scolon", "cbrac",
                "execute", "oparen", "identifier", "dot", "identifier",
                "oparen", "cparen", "cparen", "scolon", "loop", "oparen",
                "lop", "identifier", "cparen", "obrac", "return", "integer",
                "scolon", "identifier", "dot", "identifier", "oparen",
                "cparen", "scolon", "cbrac", "execute", "oparen", "identifier",
                "dot", "identifier", "oparen", "cparen", "cparen", "scolon",
                "cbrac", "identifier", "equals", "identifier", "oparen",
                "integer", "cparen", "scolon", "string", "print", "scolon",
                "end", "scolon"]
        value = ["ACT", "fac", "(", "x", ")", "{", "u1", "=", "ALIVE",
                 ";", "u1", ".", "DIE_UNLESS", "(", "x", ">=", "1", ")",
                 ";", "~ATH", "(", "u1", ")", "{", "APPEARIFY", "REPLICATE",
                 "(", "x", ",", "frac", "(", "SUBIFY", "(", "x", ",", "1",
                 ")", ")", ")", ";", "u1", ".", "DIE", "(", ")", ";", "}",
                 "EXECUTE", "(", "u1", ".", "REVIVE", "(", ")", ")", ";",
                 "~ATH", "(", "!", "u1", ")", "{", "APPEARIFY", "1", ";",
                 "u1", ".", "REVIVE", "(", ")", ";", "}", "EXECUTE", "(",
                 "u1", ".", "DIE", "(", ")", ")", ";", "}", "result", "=",
                 "fac", "(", "6", ")", ";", "> The result is: $(result) =",
                 "=>", ";", "THIS.DIE()", ";"]
        position = [(1,0), (1,4), (1,7), (1,8), (1,9), (1,11), (2,4),
                    (2,7), (2,9), (2,14), (3,4), (3,6), (3,7), (3,17),
                    (3,19), (3,21), (3,24), (3,26), (3,27), (4,4), (4,8),
                    (4,9), (4,11), (4,13), (5,8), (5,18), (5,27), (5,28),
                    (5,29),(5,31), (5,35), (5,36), (5,42), (5,43), (5,44),
                    (5,46), (5,47), (5,48), (5,49), (5,50), (6,8), (6,10),
                    (6,11), (6,14), (6,15), (6,16), (7,4), (7,5), (7,12),
                    (7,13), (7,15), (7,16), (7,22), (7,23), (7,24), (7,25),
                    (8,4), (8,8), (8,9), (8,10), (8,12), (8,14), (9,8),
                    (9,18), (9,19), (10,8), (10,10), (10,11), (10,17),
                    (10,18), (10,19), (11,4), (11,5), (11,12), (11,13),
                    (11,15), (11,16), (11,19), (11,20), (11,21), (11,22),
                    (12,0), (13,0), (13,7),(13,9), (13,12), (13,13),
                    (13,14), (13,15), (14,0), (14,28), (14,31), (15,0),
                    (15,10)]
        self.check_tokenize(code, itype, value, position)

    def test_invalid_token(self):
        code = "$ = 8"

        with self.assertRaises(RuntimeError):
            t = tokenizer.Tokenizer(code)
            result = t.tokenize()


import logging

# Third-party modules
import orjson
from typing import Optional, Dict

# NOC modules
from noc.core.http.client import fetch_sync
from noc.config import config
from noc.core.perf import metrics
from noc.core.comp import smart_text
from noc.core.liftbridge.base import LiftBridgeClient
from noc.core.ioloop.util import run_sync

logger = logging.getLogger(__name__)

def pub(topic, data, raw=False):
    logger.debug("Publish to topic %s", topic)
    url = "http://%s:%s/pub" % (
        config.nsqd.http_addresses[0].host,
        config.nsqd.http_addresses[0].port,
    )
    code, headers, body = fetch_sync(
        "%s?topic=%s" % (url, topic),
        method="POST",
        body=data if raw else smart_text(orjson.dumps(data)),
        connect_timeout=config.nsqd.connect_timeout,
        request_timeout=config.nsqd.request_timeout,
    )
    if code != 200:
        metrics["error", ("type", "nsq_pub_error_code %s" % code)] += 1
        raise Exception("Cannot publish: %s %s" % (code, body))

def publish(
    value: bytes,
    stream: str,
    partition: Optional[int] = None,
    key: Optional[bytes] = None,
    headers: Optional[Dict[str, bytes]] = None,
):
    async def wrap():
        async with LiftBridgeClient() as client:
            await client.publish(
                value=value, stream=stream, partition=partition, key=key, headers=headers
            )

    run_sync(wrap)


from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QScrollArea, QGridLayout, QLineEdit, QPushButton, QButtonGroup, QMessageBox
from PyQt5.QtWidgets import QHBoxLayout, QDialog, QDialogButtonBox

class TableListView:
    def __init__(self, window, root_window):
        self.main_widget = window
        self.root_window = root_window

        self.drawToolBar()

    def __del__(self):
        self.main_widget_draw_area.deleteLater()
        self.scrollable.deleteLater()
        self.scrollable_v_lay.deleteLater()
        self.main_widget.deleteLater()
        self.button_group_toolbar.deleteLater()

    def initDraw(self, width, height):
        self.main_widget.setMinimumWidth(width)
        self.main_widget.setMinimumHeight(height)

        self.scrollable = QScrollArea(self.main_widget)
        self.scrollable.move(0, 70)
        self.scrollable_v_lay = QVBoxLayout()
        self.scrollable.setLayout(self.scrollable_v_lay)
        self.main_widget_draw_area = QWidget(self.main_widget)
        self.scrollable_v_lay.addWidget(self.main_widget_draw_area)

        self.main_widget.show()
        pass

    def drawToolBar(self):
        self.button_group_toolbar = QButtonGroup(self.main_widget)

        self.search_b_g_tb = QPushButton(self.main_widget)
        self.search_b_g_tb.setText("Konsola SQL")

        self.button_group_toolbar.addButton(self.search_b_g_tb)


from django.conf import settings
from .file_system_helpers import *
import os, glob

def getAllSeqSetsWithCategory():
    dir_with_seq_sets = os.path.join(settings.PIPELINE_DIR, 'data/ref/')
    # All folders with seq_sets i.e.:
    # data/ref/virus
    seq_sets_dirs = get_folders_in_path(dir_with_seq_sets)

    # Because it contains no seqs. Why is it even there?
    seq_sets_dirs.remove('index')

    # Now lets construct a dict {ref_type: [seq1, se2, ...], ...}
    categories_with_seq_sets = {}
    for seq_sets_dir in seq_sets_dirs:
        seq_sets_for_type = get_files_from_path_with_ext(dir_with_seq_sets+seq_sets_dir+'/', '.fa')
        categories_with_seq_sets[seq_sets_dir] = seq_sets_for_type

    print(categories_with_seq_sets)

def get_types_of_seq_sets():
    dir_with_seq_sets = os.path.join(settings.PIPELINE_DIR, 'data/ref/')
    # All folders with seq_sets i.e.:
    # data/ref/virus
    seq_sets_dirs = get_folders_in_path(dir_with_seq_sets)

    # Because it contains no seqs. Why is it even there?
    seq_sets_dirs.remove('index')

    return seq_sets_dirs

def get_seqs_for_seq_type(seq_type):
    dir_with_seq_sets = os.path.join(settings.PIPELINE_DIR, 'data/ref/')
    return get_files_from_path_with_ext(dir_with_seq_sets+seq_type+'/', '.fa')


'''
file_handle = open('helloW.txt', 'w')
file_handle.write('Hello world\n')
file_handle.close()

file_handle = open('HelloW.txt','r')
contents = file_handle.read()
file_handle.close()
print(contents)

#1
userinp = input("what is the name of the file you want to open?")
file_name = open(userinp, 'r')
contents1 = file_name.read()
file_name.close()
print(contents1)
 #2
filename = input("What is the filename?")
file_contents = input("What are the files contents")
with open(filename, 'w') as file_name1:
    file_name1.write(file_contents)
'''
#3
filename1 = input("What is the file name ")

letterhis= {}
wordhis= {}

file_variable = open(filename1, "r")

readin = file_variable.read()

newreadin = readin.split()

for i in range(0, len(readin)):
    if readin[i] in letterhis:
        letterhis[readin[i]] +=  1
    else:
        letterhis[readin[i]] = 1

for i in range(0, len(newreadin)):
    if newreadin[i] in wordhis:
        wordhis[newreadin[i]] = wordhis[newreadin[i]] + 1
    else:
        wordhis[newreadin[i]] = 1

print(letterhis)

print(wordhis)