from torch.utils.data import Dataset
from dbm.util import make_grid_np, rand_rot_mtx, voxelize_gauss
from dbm.stats import *

class DS(Dataset):
    """
    This class defines a PyTorch Dataset for the DBM algorithm.
    It generates a dataset using a Recurrent_Generator instances

    Attributes:
    - data: instance of a Data object that contains the molecular structures and topology
    - elems: a list of dictionaries representing the elements of the molecular structures
    - resolution: the resolution of the grid used for the local environments centered at a bead position
    - delta_s: the grid spacing used for the local environments
    - sigma: the standard deviation of the Gaussian used to voxelize the molecular
             structure in the local environment
    - rand_rot: a boolean indicating whether random rotations should be applied
                during training
    - align: an integer indicating whether the molecules should be aligned during
             generation
    - grid: the grid used for the molecular structures

    Methods:
    - __init__(self, data, cfg, train=True): initializes the dataset with the given
      molecular data and configuration file
    - __len__(self): returns the length of the dataset
    - __getitem__(self, ndx): retrieves an item from the dataset at the given index
    """

    def __init__(self, data, cfg, train=True, verbose=False):
        """
        Initializes the DS_seq instance with the given molecular data and configuration
        file. Uses a series of Recurrent_Generator instances to generate molecular structures
        for various configurations.

        Arguments:
        - data: an instance of a Data object
        - cfg: a configuration file
        - train: a boolean indicating whether the dataset is for training or testing

        Returns:
        None
        """

        self.data = data

        # set properties
        self.resolution = cfg.getint('grid', 'resolution')
        self.delta_s = cfg.getfloat('grid', 'length') / cfg.getint('grid', 'resolution')
        self.sigma = cfg.getfloat('grid', 'sigma')
        self.rand_rot = cfg.getboolean('training', 'rand_rot')
        self.align = int(cfg.getboolean('universe', 'align'))

        # print properties
        if verbose:
            print("Using an ({} x {} x {}) grid with spacing {} nm".format(self.resolution, self.resolution, self.resolution, self.delta_s))
            print("The sigma value for the Gauss-blobb representation is {} nm".format(self.sigma))
            print("Alignment of the local environments is set to {}".format(self.align))
            print("Random rotations during training is set to {}".format(self.rand_rot))

        # create the grid
        self.grid = make_grid_np(self.delta_s, self.resolution)

        # create generator objects for the different molecular representations
        generators = []
        generators.append(Recurrent_Generator(data, hydrogens=False, gibbs=False, train=train, rand_rot=False))
        generators.append(Recurrent_Generator(data, hydrogens=False, gibbs=True, train=train, rand_rot=False))
        # if hydrogens are included in the representation, add corresponding generator objects
        if cfg.getboolean('training', 'hydrogens'):
            generators.append(Recurrent_Generator(data, hydrogens=True, gibbs=False, train=train, rand_rot=False))
            generators.append(Recurrent_Generator(data, hydrogens=True, gibbs=True, train=train, rand_rot=False))

        # combine all elements from the different generator objects
        self.elems = []
        for g in generators:
            self.elems += g.all_elems()

    def __len__(self):
        """
        Returns the length of the dataset.

        Args:
            None

        Returns:
            int: The length of the dataset.
        """
        return len(self.elems)

    def __getitem__(self, ndx):
        """
        Returns the dataset element at the given index.

        Args:
            ndx (int): The index of the element to be returned.

        Returns:
            tuple: A tuple containing the following elements:
                - ndarray: Voxelized representation of the target atom.
                - int: Type of the target atom.
                - ndarray: feature vectors describing the atoms in the loc environment.
                - ndarray: replacement vector (one-hot), indicates at which index the generated atom will be inserted in the local environment
                - ndarray: a mask for elements for empty tokens in the sequence (zero padding)
                - ndarray: Voxelized representation of the atoms.
                - ndarray: Voxelized representation of the beads.
                - ndarray: Coordinates of the atoms in the local environment (used to compute energies).
                - tuple: A tuple containing the following elements:
                    - ndarray: Indices of the bonded energy terms.
                    - ndarray: Indices of the angle energy terms.
                    - ndarray: Indices of the dihedral energy terms.
                    - ndarray: Indices of the Lennard-Jones energy terms.
        """

        # generate random rotation matrix for training
        if self.rand_rot:
            R = rand_rot_mtx(self.align)
        else:
            R = np.eye(3)

        # get the element at the given index
        d = self.elems[ndx]

        # rotate and create voxelized representation of the target atom, atoms in the environment, and beads
        target_atom = voxelize_gauss(np.dot(d['target_pos'], R.T), self.sigma, self.grid)
        aa_coords = np.dot(d['aa_pos'], R.T).astype('float32')
        atom_grid = voxelize_gauss(aa_coords, self.sigma, self.grid)
        bead_grid = voxelize_gauss(np.dot(d['cg_pos'], R.T), self.sigma, self.grid)

        # create feature representation for beads
        # atom features need to be generated on the fly, as they depend on the previously generated atom
        cg_features = d['cg_feat'][:, :, None, None, None] * bead_grid[:, None, :, :, :]
        # (N_beads, N_chn, 1, 1, 1) * (N_beads, 1, N_x, N_y, N_z)
        cg_features = np.sum(cg_features, 0)
        # (N_chn, N_x, N_y, N_z)

        # define elems (used for iterating over the sequence)
        elems = (target_atom, d['target_type'], d['aa_feat'], d['repl'], d['mask'])
        # define initial (start values for the sequential reconstruction)
        initial = (atom_grid, cg_features, aa_coords)
        # indices for the energy functions, such that we can generate the energy contribution of generated atoms
        energy_ndx = (d['bonds_ndx'], d['angles_ndx'], d['dihs_ndx'], d['ljs_ndx'])

        return elems, initial, energy_ndx


from pyModbusTCP.client import ModbusClient
import time
import paramiko

def exec_ssh_cmds(cmds, out=True):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect("192.168.1.2", port=22, username="admin",
                   password="")
    # stdin, stdout, stderr = client.exec_command("")
    # print(stdout.read().decode("utf-8"))
    cmd = "\n".join(cmds)
    stdin, stdout, stderr = client.exec_command(cmd)
    stdout.read()  # must read output of command or it doesn't run *facepalm*
    client.close()

SERVER_HOST = "192.168.1.20"
SERVER_PORT = 502
SERVER_U_ID = 1

c = ModbusClient()
c2 = ModbusClient()

# uncomment this line to see debug message
# c.debug(True)

# define modbus server host, port and unit_id
c.host(SERVER_HOST)
c.port(SERVER_PORT)
c.unit_id(SERVER_U_ID)

c2.host("192.168.1.30")
c2.port(502)
c2.unit_id(1)
c2.open()
c2.close()

cmds = [
    "config switch physical-port",
    "edit port4",
    "set status down",
    "end"
]
exec_ssh_cmds(cmds)

# open or reconnect TCP to server
if not c.is_open():
    if not c.open():
        print("unable to connect to "+SERVER_HOST+":"+str(SERVER_PORT))

if c.is_open():
    while True:
        # Turn pump on
        c.write_single_coil(1, True)
        # Set pump to 100%
        c.write_single_register(3, 0x64)
        val = c.read_coils(5)[0]
        print(val)

        c2.write_single_coil(1, True)

        if val:
            c.write_single_coil(1, False)

        time.sleep(0.2)


# 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)


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


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")]


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 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


# Project Sentinel: A version of the parser that has no dependencies on Raspberry Pi libraries
# Harris M
# February 27, 2020

# LIBRARIES - PYTHON ONLY
import sys
import socket
import time
from time import sleep
from datetime import datetime
import struct

# CONNECTION CONSTANTS
PORT = 2112
IP_ADDRESS = '169.254.100.100'

# SCAN CONSTANTS
REQUEST_SINGLE_SCAN = b'\x02sRN LMDscandata\x03'
REQUEST_CONT_SCAN = b'\x02sEN LMDscandata 1\x03'
STOP_CONT_SCAN = b'\x02sEN LMDscandata 0\x03'

# SENSOR CONSTANTS
microsecond = 10**(-6)
angle_step = 10**(4)
scale_factor = {'3F800000': '1x',
                '40000000': '2x' }

# Function: type converter
# Description: Converts a number to specified base
def type_conv(num, base):
    initial = int(num, 16)
    if base == 's8':
        conv = (initial + 2**7) % 2**8 - 2**7
    elif base == 'u8':
        conv = initial % 2**8
    elif base == 's16':
        conv = (initial + 2**15) % 2**16 - 2**15
    elif base == 'u16':
        conv = initial % 2**16
    elif base == 's32':
        conv = (initial + 2**31) % 2**32 - 2**31
    elif base == 'u32':
        conv = initial % 2**32
    elif base == 's64':
        conv = (initial + 2**63) % 2**64 - 2**63
    elif base == 'u64':
        conv = initial % 2**64
    else:
        return initial

    return conv

# Function: telegram_parse
# Description: Parses a telegram message
def telegram_parse(scan):
    telegram = {'Version Number': '',
                'Device Number': '',
                'Serial Number': '',
                'Device Status': '',
                'Telegram Counter': '',
                'Scan Counter': '',
                'Time since start-up': '',
                'Time of transmission': '',
                'Scan Frequency': '',
                'Measurement Frequency': '',
                'Amount of Encoder': '',
                '16-bit Channels': '',
                'Scale Factor': '',
                'Scale Factor Offset': '',
                'Start Angle': '',
                'Angular Increment': '',
                'Quantity': '',
                'Motor encoder': '',
                'Timestamp': '',
                'Ax': '',
                'Ay': '',
                'Az': '',
                'Gx': '',
                'Gy': '',
                'Gz': '',
                'Measurement': [] }

    if (scan[1] != 'LMDscandata'):
        print("There is something wrong with the scan data")
    else:
        telegram['Version Number'] = type_conv(scan[2], 'u16')
        telegram['Device Number'] = type_conv(scan[3], 'u16')
        telegram['Serial Number'] = type_conv(scan[4], 'u32')
        telegram['Device Status'] = type_conv(scan[6], 'u8')
        telegram['Telegram Counter'] = type_conv(scan[7], 'u16')
        telegram['Scan Counter'] = type_conv(scan[8], 'u16')
        telegram['Time since start-up'] = type_conv(scan[9], 'u32') * microsecond
        telegram['Time of transmission'] = type_conv(scan[10], 'u32') * microsecond
        telegram['Scan Frequency'] = type_conv(scan[16], 'u32')
        telegram['Measurement Frequency'] = type_conv(scan[17], 'u32')
        telegram['Amount of Encoder'] = type_conv(scan[18], 'u32')
        telegram['16-bit Channels'] = type_conv(scan[19], 'u32')
        telegram['Scale Factor'] = scale_factor[scan[21]]
        telegram['Scale Factor Offset'] = type_conv(scan[22], 'u32')
        telegram['Start Angle'] = type_conv(scan[23], 's32') / angle_step
        telegram['Angular Increment'] = type_conv(scan[24], 'u16') / angle_step
        telegram['Quantity'] = type_conv(scan[25], 'u16')

        for message in range(26, 26 + telegram['Quantity']):
            telegram['Measurement'].append(type_conv(scan[message], 'u16'))

        telegram['Timestamp'] = scan[-1]

        Ax, Ay, Az, Gx, Gy, Gz = accel_read()
        telegram['Ax'] = Ax
        telegram['Ay'] = Ay
        telegram['Az'] = Az
        telegram['Gx'] = Gx
        telegram['Gy'] = Gy
        telegram['Gz'] = Gz

    return telegram

# Function: live_parse
# Description: Starts the socket and begins parsing appropriately
def live_parse(count):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((IP_ADDRESS, PORT))
    sock.send(REQUEST_CONT_SCAN)

    scans = []
    while 1:
        scan = []
        curr = ''
        while 1:
            msg_orig = sock.recv(1)
            msg = msg_orig.decode('utf-8')

            if msg_orig == b'\x03':
                scan.append(curr)
                break
            elif msg == ' ':
                scan.append(curr)
                curr = ''
            else:
                curr += msg

        if (len(scan) < 4):
            continue

        curr = datetime.now()
        nice_timestamp = str(curr.year) + "-" + str(curr.month) + "-" + str(curr.day) + "_" + str(curr.hour) + "-" + str(curr.minute) + "-" + str(curr.second)

        scan.append(nice_timestamp)

        initial_parse = telegram_parse(scan)
        scans.append(initial_parse)

        if len(scans) == count:
            sock.send(STOP_CONT_SCAN)
            break

# Function: telegram_parse_
# Description: Parses a telegram message
def telegram_parse(scan):
    telegram = {'Version Number': '',
                'Device Number': '',
                'Serial Number': '',
                'Device Status': '',
                'Telegram Counter': '',
                'Scan Counter': '',
                'Time since start-up': '',
                'Time of transmission': '',
                'Scan Frequency': '',
                'Measurement Frequency': '',
                'Amount of Encoder': '',
                '16-bit Channels': '',
                'Scale Factor': '',
                'Scale Factor Offset': '',
                'Start Angle': '',
                'Angular Increment': '',
                'Quantity': '',
                'Motor encoder': '',
                'Timestamp': '',
                'Ax': '',
                'Ay': '',
                'Az': '',
                'Gx': '',
                'Gy': '',
                'Gz': '',
                'Measurement': [],
                'Remission': [] }

    if (scan[1] != 'LMDscandata'):
        print("There is something wrong with the scan data.")
    if (scan[20] != 'DIST1'):
        print("DIST1 value not in the correct location.")
    else:
        telegram['Version Number'] = type_conv(scan[2], 'u16')
        telegram['Device Number'] = type_conv(scan[3], 'u16')
        telegram['Serial Number'] = type_conv(scan[4], 'u32')
        telegram['Device Status'] = type_conv(scan[6], 'u8')
        telegram['Telegram Counter'] = type_conv(scan[7], 'u16')
        telegram['Scan Counter'] = type_conv(scan[8], 'u16')
        telegram['Time since start-up'] = type_conv(scan[9], 'u32') * microsecond
        telegram['Time of transmission'] = type_conv(scan[10], 'u32') * microsecond
        telegram['Scan Frequency'] = type_conv(scan[16], 'u32')
        telegram['Measurement Frequency'] = type_conv(scan[17], 'u32')
        telegram['Amount of Encoder'] = type_conv(scan[18], 'u32')
        telegram['16-bit Channels'] = type_conv(scan[19], 'u32')
        telegram['Scale Factor'] = scale_factor[scan[21]]
        telegram['Scale Factor Offset'] = type_conv(scan[22], 'u32')
        telegram['Start Angle'] = type_conv(scan[23], 's32') / angle_step
        telegram['Angular Increment'] = type_conv(scan[24], 'u16') / angle_step
        telegram['Quantity'] = type_conv(scan[25], 'u16')

        for message in range(26, 26 + telegram['Quantity']):
            telegram['Measurement'].append(type_conv(scan[message], 'u16'))

        # Error checking for RSSI1
        if (scan[26 + telegram['Quantity'] + 1] != 'RSSI1'):
            print("RSSI1 value not in the correct location.")

        if (scale_factor[scan[26 + telegram['Quantity'] + 2]] != telegram['Scale Factor']):
            print("RSSI1 scale factor does not match DIST1's scale factor.")

        if (type_conv(scan[26 + telegram['Quantity'] + 3], 'u32') != telegram['Scale Factor Offset']):
            print("RSSI1 scale factor offset does not match DIST1's scale factor offset.")

        if (type_conv(scan[26 + telegram['Quantity'] + 4], 's32') / angle_step != telegram['Start Angle']):
            print("RSSI1 start angle does not match DIST1's start angle.")

        if (type_conv(scan[26 + telegram['Quantity'] + 5], 'u16') / angle_step != telegram['Angular Increment']):
            print("RSSI1 angular increment does not match DIST1's angular increment.")

        if (type_conv(scan[26 + telegram['Quantity'] + 6], 'u16') != telegram['Quantity']):
            print("RSSI1 quantity does not match DIST1's quantity.")

        # RSSI1 conversion loop
        for message in range(26 + telegram['Quantity'] + 7, 26 + telegram['Quantity'] + 7 + telegram['Quantity']):
            telegram['Remission'].append(type_conv(scan[message], 'u16'))

        telegram['Timestamp'] = scan[-1]

    return telegram

# Function: single_parse
# Description: Starts the socket and begins parsing appropriately
def single_parse():
    insideTelegram = False
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((IP_ADDRESS, PORT))
    sock.send(REQUEST_SINGLE_SCAN)

    scan = []
    curr = ''
    while 1:
        msg_orig = sock.recv(1)
        msg = msg_orig.decode('utf-8')
        if msg_orig == b'\x02':
            insideTelegram = True
        elif msg_orig == b'\x03':
            insideTelegram = False
            break
        elif msg == ' ':
            scan.append(curr)
            curr = ''
        else:
            curr += msg

    curr = datetime.now()
    nice_timestamp = str(curr.year) + "-" + str(curr.month) + "-" + str(curr.day) + "_" + str(curr.hour) + "-" + str(curr.minute) + "-" + str(curr.second)

    scan.append(nice_timestamp)

    print(scan)

    initial_parse = telegram_parse(scan)
    print(initial_parse)
    return None

def singleRun():
    """Takes a single scan of the sensor, names it, and then uploads it to AWS

        Args:
            None
        Return:
            None
    """

    accel_init()
    telegram = single_parse()
    createItem(telegram)

# RUN
# Code to run
# accel_init()
# test = single_parse()
# createItem(test)
test = single_parse()


import numpy as np
import dlib
import cv2
from keras.models import load_model
from tensorflow.keras.utils import img_to_array
from scipy.spatial import distance as dist
import imutils
import matplotlib.pyplot as plt
from imutils import face_utils

global points, points_lip, emotion_classifier, detector, predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("C:/Users/arady/aradya/Stress-Detector/shape_predictor_68_face_landmarks.dat")
emotion_classifier = load_model("C:/Users/arady/aradya/emotion_detection_model1.h5")

points=[]; points_lip=[]

def ebdist(leye,reye):
    eyedist = dist.euclidean(leye,reye)
    points.append(int(eyedist))
    return eyedist

def lpdist(l_lower,l_upper):
    lipdist = dist.euclidean(l_lower, l_upper)
    points_lip.append(int(lipdist))
    return lipdist

def emotion_finder(faces,frame):
    EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised","neutral"]
    x,y,w,h = face_utils.rect_to_bb(faces)
    frame = frame[y:y+h,x:x+w]
    roi = cv2.resize(frame,(48,48))
    roi = roi.astype("float") / 255.0
    roi = img_to_array(roi)
    roi = np.expand_dims(roi,axis=0)
    preds = emotion_classifier.predict(roi)[0]
    emotion_probability = np.max(preds)
    label = EMOTIONS[preds.argmax()]
    emtn = label
    if label in ['scared','sad','angry','disgust']:
        label = 'Stressed'
    else:
        label = 'Not Stressed'
    return label,emtn

def normalize_values(points,disp,points_lip,dis_lip):
    normalize_value_lip = abs(dis_lip - np.min(points_lip))/abs(np.max(points_lip) - np.min(points_lip))
    normalized_value_eye =abs(disp - np.min(points))/abs(np.max(points) - np.min(points))
    normalized_value =( normalized_value_eye + normalize_value_lip)/2
    stress_value = (np.exp(-(normalized_value)))
    if stress_value>=0.75:
        stress_label="High Stress"
    else:
        stress_label="Low Stress"
    return stress_value,stress_label

class VideoCamera(object):
    def _init_(cap):
        cap.video = cv2.VideoCapture(0)
    def _del_(cap):
        cv2.VideoCapture.release()

    def get_frame(cap):
        ret,frame = cv2.VideoCapture(0).read()
        frame = cv2.flip(frame,1)
        frame = imutils.resize(frame, width=500,height=500)
        (lBegin, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eyebrow"]
        (rBegin, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eyebrow"]
        (l_lower, l_upper) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        detections = detector(gray,0)
        for detection in detections:
            emotion,emtn= emotion_finder(detection,gray)
            shape = predictor(frame,detection)
            shape = face_utils.shape_to_np(shape)

            leyebrow = shape[lBegin:lEnd]
            reyebrow = shape[rBegin:rEnd]
            openmouth = shape[l_lower:l_upper]

            reyebrowhull = cv2.convexHull(reyebrow)
            leyebrowhull = cv2.convexHull(leyebrow)
            openmouthhull = cv2.convexHull(openmouth)

            cv2.drawContours(frame, [reyebrowhull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [leyebrowhull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [openmouthhull], -1, (0, 255, 0), 1)

            lipdist = lpdist(openmouthhull[-1][0],openmouthhull[0][0])
            eyedist = ebdist(leyebrow[-1],reyebrow[0])

            stress_value,stress_label = normalize_values(points,eyedist, points_lip, lipdist)

            cv2.putText(frame, emotion, (10,10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (235, 52, 52), 2)
            cv2.putText(frame,emtn,(10,25),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,255,255),2)
            cv2.putText(frame,"stress value:{}".format(str(int(stress_value*100))),(10,40),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (51, 66, 232), 2)
            cv2.putText(frame,"Stress level:{}".format((stress_label)),(10,60),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (35, 189, 25), 2)
            if(emotion == "Stressed"):
                if(emtn == "sad"):
                    cv2.putText(frame,"Medidate for a while",(10,80),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,53), 1)
                elif(emtn=="scared"):
                    cv2.putText(frame,"Relax",(10,80),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,53), 1)
                else:
                    cv2.putText(frame,"Calm down",(10,80),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,53), 1)
            else:
                cv2.putText(frame,"Doing Great",(10,80),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,53), 1)
        frame = cv2.resize(frame,(600,500))
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()


# -*- 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 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


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,
    },
)


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')


import requests
import json
from shared_service.dataTransformatorQueryParams import BodyTransformatorQueryParam
import logging
import os
from time import sleep

FORMAT = "%(asctime)-15s    %(service)-8s     %(message)s"
logging.basicConfig(format=FORMAT,level=logging.INFO)
log_info = {'clientip': '192.168.0.1', 'service': 'sharedService'}

class SharedServerService:

    token = ''

    def __init__(self,transformator = BodyTransformatorQueryParam()):
        self.shared_server_addr = os.environ["SHARED_SERVER_ADDR"]
        logging.info("La dirección del shared server es: "+self.shared_server_addr, extra=log_info)
        self.transformator = transformator

    def getToken(self):
        logging.info("Obteniendo el token de autenticacion",extra=log_info)
        code = 0
        if SharedServerService.token == '':
            while(code != 200):
                logging.info('Realizando request a ['+self.shared_server_addr+'/login'+']',extra=log_info)
                try:
                    res = requests.get(self.shared_server_addr+"/login")
                    code = res.status_code
                    SharedServerService.token = res.headers['Authorization']
                except requests.exceptions.ConnectionError:
                    logging.info('Problemas de conexión con el Shared Server. Intentando de nuevo en 10 segundos...',extra=log_info)
                    sleep(10)
        logging.info('Conexión exitosa. El token de este app-server es: ' + SharedServerService.token,extra=log_info)

    def addUser(self,user):
        logging.info('Agregando un usuario',extra=log_info)
        #Setup
        stringQuery = self.transformator.transformate(user)
        url = self.shared_server_addr+'/users'+'?'+stringQuery;
        logging.info('Realizando un POST al shared server con url ['+url+']',extra=log_info)
        #Realizando request
        logging.info('El token es:'+SharedServerService.token,extra=log_info)
        res = requests.post(url,headers = {'Authorization':SharedServerService.token})
        logging.info('Se recibio un '+ str(res.status_code)+" del shared server",extra=log_info)
        return res.status_code == 200 or res.status_code == 201

    def getUserFromEmail(self,email):
        logging.info('Obteniendo información del usuario ['+email+']',extra=log_info)
        #Setup
        url = self.shared_server_addr+'/users/'+email
        logging.info('Realizando GET al shared server con url +['+url+']',extra=log_info)
        #Realizando request
        res = requests.get(url,headers = {'Authorization':SharedServerService.token})
        code = res.status_code
        if code != 200 and code !=201 :
            logging.info('Hubo un problema al tratar de obtener información del user',extra=log_info)
            return None;
        logging.info('Se obtuvo la información de manera correcta',extra=log_info)
        return json.loads(res.text)

    def deleteUser(self,id):
        return 'deleteUser'

    def getCostFromDistanceInKM(self,userEmail,distanceInKM):
        logging.info('Calculando la el costo de viaje para '+userEmail+' y distancia '+str(distanceInKM),extra=log_info)
        url = self.shared_server_addr+'/costos/'+userEmail+'/'+str(distanceInKM)
        logging.info('Realizo request al shared server con el siguiente request'+url,extra=log_info)
        #Realizando request
        res = requests.get(url,headers = {'Authorization':SharedServerService.token})
        return json.loads(res.text)

    def getPayMethods(self):
        logging.info('Llegó una solicitud para poder obtener los medios de pagos',extra=log_info)
        url = self.shared_server_addr+'/paymethods'
        logging.info('Se esta enviando un GET a la siguiente url'+url,extra=log_info)
        res = requests.get(url,headers = {'Authorization':SharedServerService.token})
        return json.loads(res.text)

    def getCarInfo(self,email):
        logging.info('Llegó una solicitud para poder obtener información de un auto de:'+email,extra=log_info)
        url = self.shared_server_addr+'/cars/'+email
        logging.info('Realizo request al shared server con el siguiente request'+url,extra=log_info)
        #Realizando request
        res = requests.get(url,headers = {'Authorization':SharedServerService.token})
        return json.loads(res.text)

    # POST al sharedServer/trips.
    def addTrip(self, userEmail, driverEmail, distance, paymethod):
        logging.info("Enviándole al shared-server un viaje para agregar.", extra=log_info)
        url = self.shared_server_addr+'/trips/'+ userEmail + "/" + driverEmail \
            + "/" + distance.split(" ")[0] + "?" + "metodo=" + paymethod
        logging.info("POST a la siguiente url: "+ url, extra=log_info)
        res = requests.post(url,headers = {'Authorization': SharedServerService.token})
        return res.status_code == 200 or res.status_code == 201


import argparse
import asyncio
import math
import pickle
import random
import sys
import time
from multiprocessing import Process

from numpy.random import default_rng
import numpy as np

class DataGenerator:
    def __init__(self, vector_length: int = 50, noisy_mode: bool = False):
        self.noisy_mode = noisy_mode
        self.vector_length = vector_length

    async def run(self, socket_host, socket_port):
        server = await asyncio.start_server(self._generate_data, socket_host, socket_port)
        async with server:
            await server.serve_forever()

    async def _generate_data(self, reader, writer):
        # for i in range(10):
        timestamp_ms = (int(round(time.time() * 1000)) // 1000) * 1000
        while True:
            # for i in range(5000):
            if self.noisy_mode and random.randrange(0, 2500) == 1234:
                await asyncio.sleep(0.001)
                timestamp_ms += 1
                continue

            vector = self._generate_random_vector(length=self.vector_length)
            timestamp_ms += 1
            data = pickle.dumps((timestamp_ms, vector))
            writer.write(data)

            await asyncio.sleep(0.001)

    def _generate_random_vector(self, length: int = 50):
        vector = default_rng().standard_normal(length)
        return vector

class DataAggregator:
    def __init__(self, vector_length: int = 50):
        self.vector_length = vector_length
        self.num_vectors_received = 0
        self.start_window_timestamp = None
        self.rates = np.array([])
        self.matrix = None

    async def run(self, socket_host, socket_port):
        reader, writer = await asyncio.open_connection(socket_host, socket_port)
        # for i in range(20):
        while True:
            data = await reader.readexactly(self.vector_length * 8 + 160)
            timestamp, vector = pickle.loads(data)
            # vector = np.frombuffer(data, count=self.vector_length, dtype="int32")
            self._handle_vector(vector, timestamp)

    def _handle_vector(self, vector, timestamp_ms):
        if not self.start_window_timestamp or self.num_vectors_received % 1000 == 0:
            self.start_window_timestamp = timestamp_ms

        self.num_vectors_received += 1

        if timestamp_ms % 1000 == 0:
            rate = self.num_vectors_received
            self.rates = np.append(self.rates[-100:], [rate])
            rate_mean = self.rates.mean()
            rate_std = self.rates.std()
            print(f"Rate: {rate} Hz | Mean: {rate_mean} | STD: {rate_std}")
            if rate < 1000:
                print("WARNING! Rate fell below expected frequency")
            self._append_line(f"Rate: {rate} | Mean: {rate_mean} | STD: {rate_std}")
            self.num_vectors_received = 0

        if self.matrix is None:
            self.matrix = np.array([vector])
        else:
            self.matrix = np.append(self.matrix, [vector], axis=0)

        if self.num_vectors_received % 100 == 0:
            mat_mean = self.matrix.mean(axis=0)
            mat_std = self.matrix.std(axis=0)

            # print(f"Matrix Mean: {mat_mean} | Matrix SD: {mat_std}")
            self._append_line(f"Matrix Mean: {mat_mean} | Matrix SD: {mat_std}")

            self.matrix = None

    def _append_line(self, line):
        with open("results.txt", "a") as f:
            f.write(line + "\n")

SOCKET_HOST = '127.0.0.1'
SOCKER_PORT = 8888

def run_aggregator(socket_host, socket_port):
    asyncio.run(DataAggregator().run(socket_host, socket_port))

def run_generator(socket_host, socket_port, noisy_mode):
    asyncio.run(DataGenerator(noisy_mode=noisy_mode).run(socket_host, socket_port))

if __name__ == "__main__":
    # noisy_mode = bool(sys.argv[1]) if len(sys.argv) > 1 else False
    parser = argparse.ArgumentParser()
    parser.add_argument('--noisy', help='Run in Noisy Mode', default=False, action="store_true")
    args = parser.parse_args()

    p_gen = Process(target=run_generator, args=(SOCKET_HOST, SOCKER_PORT, args.noisy))
    p_gen.start()

    p_agg = Process(target=run_aggregator, args=(SOCKET_HOST, SOCKER_PORT))
    p_agg.start()

    p_gen.join()
    p_agg.join()


#!/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()


# 导入通用模块
import tensorflow as tf
from dataLoader.DataLoader import DataGenerator
from utils.parse import process_config
from utils.makeDirs import makeDirs
from utils.logger import Logger
from utils.getArgs import getArgs

# 导入需重写的模块
from models.exampleModel import ExampleModel
from trainers.exampleTrainer import ExampleTrainer
from testers.exampleTester import ExampleTester

def main():
    # capture the config path from the run arguments
    # then process the json configuration file
    try:
        args = getArgs()
        print("args got")
        config = process_config(args.config)

    except:
        print("missing or invalid arguments")
        exit(0)

    if config.mode == "train":

        # create the experiments dirs
        makeDirs([config.summary_dir, config.checkpoint_dir])
        # create tensorflow session
        sess = tf.Session()
        # create your data generator
        data = DataGenerator(config)
        # create an instance of the model
        model = ExampleModel(config)
        # create tensorboard logger
        logger = Logger(sess, config)
        # create trainer and pass all the previous components to it
        trainer = ExampleTrainer(sess, model, data, config, logger)
        # load model if exists
        model.load(sess)
        # train model
        trainer.train()

    if config.mode == "test":
        # create the experiments dirs
        makeDirs([config.summary_dir, config.checkpoint_dir])
        # create tensorflow session
        sess = tf.Session()
        # create your data generator
        data = DataGenerator(config)
        # create an instance of the model
        model = ExampleModel(config)
        # create trainer and pass all the previous components to it
        tester = ExampleTester(sess, model, data, config)
        # train model
        tester.test()

    else:
        print("invalid mode")
        exit(0)

if __name__ == '__main__':
    main()


# importing necessary libraries
import pandas as pd
import numpy as np
import re
import string
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import classification_report, confusion_matrix, f1_score, roc_auc_score, accuracy_score

# loading the date into data frame from the classified email data set which was generated using main,py module
emails = pd.read_csv("email_classify.csv")

# seggregate the dataset
def parse_into_emails(messages):
    emails = [parse_raw_message(message) for message in messages]
    return {
        'body': map_to_list(emails, 'body'),
        'to': map_to_list(emails, 'to'),
        'from_': map_to_list(emails, 'from')
    }

# cleaning
def parse_raw_message(raw_message):
    lines = raw_message.split('\n')
    email = {}
    message = ''
    keys_to_extract = ['from', 'to']
    for line in lines:
        if ':' not in line:
            message += line.strip()
            email['body'] = message
        else:
            pairs = line.split(':')
            key = pairs[0].lower()
            val = pairs[1].strip()
            if key in keys_to_extract:
                email[key] = val
    return email

def map_to_list(emails, key):
    results = []
    for email in emails:
        if key not in email:
            results.append('')
        else:
            results.append(email[key])
    return results

email_df = pd.DataFrame(parse_into_emails(emails.message))
email_data=pd.concat([email_df['body'],emails['State']],axis=1)

#data cleaning
stopwords = nltk.corpus.stopwords.words('english')
ps = nltk.PorterStemmer()
def clean_text(text):
    text = "".join([word.lower() for word in text if word not in string.punctuation])
    tokens = re.split('\W+', text)
    text = [ps.stem(word) for word in tokens if word not in stopwords]
    return text

email_data['clean_body']=email_data['body'].apply(lambda x : clean_text(x))
email_data

# Intializing tfidf vector
tfidf_vect = TfidfVectorizer(analyzer=clean_text)
tfidf_vect_fit = tfidf_vect.fit_transform(email_data['clean_body'])
tfidf_vect_fit

#split the data into train and test data set by 4:1 ratio
X_train, X_test, y_train, y_test = train_test_split(tfidf_vect_fit.toarray(), email_data['State'], test_size=0.2)

#applying the Gaussian model
gnb=GaussianNB()
y_pred2 = gnb.fit(X_train, y_train).predict(X_test)

#Metrics calculation
print("Accuracy score :", accuracy_score(y_test,y_pred2))
print("Confussion matrix :\n",confusion_matrix(y_test,y_pred2))
print("Classification report :\n", classification_report(y_test,y_pred2))


"""Utility functions for testing reward nets."""

import gym

from imitation.rewards import reward_nets

def make_ensemble(
    obs_space: gym.Env,
    action_space: gym.Env,
    num_members: int = 2,
    **kwargs,
):
    """Create a simple reward ensemble."""
    return reward_nets.RewardEnsemble(
        obs_space,
        action_space,
        members=[
            reward_nets.BasicRewardNet(obs_space, action_space, **kwargs)
            for _ in range(num_members)
        ],
    )


import PySimpleGUI as sg

sg.theme('Black')

def gerador_de_tabuada(numero, operacao):
    resultado = ""
    if operacao == 1:
        for i in range(1, 11):
            resultado += f"{numero} + {i} = {numero + i}\n"
    elif operacao == 2:
        for i in range(1, 11):
            resultado += f"{numero} - {i} = {numero - i}\n"
    elif operacao == 3:
        for i in range(1, 11):
            resultado += f"{numero} / {i} = {numero / i}\n"
    elif operacao == 4:
        for i in range(1, 11):
            resultado += f"{numero} * {i} = {numero * i}\n"

    return resultado

layout = [
    [sg.Text("Digite o número para a tabuada: EX 1-10"), sg.InputText(key="numero")],
    [sg.Radio("SOMA", "operacao", default=True, key="soma"), sg.Radio("SUBTRAÇÃO", "operacao", key="subtracao"),
     sg.Radio("DIVISÃO", "operacao", key="divisao"), sg.Radio("MULTIPLICAÇÃO", "operacao", key="multiplicacao")],
    [sg.Button("Gerar Tabuada"), sg.Button("Sair")],
    [sg.Multiline("", size=(40, 10), key="resultado")]
]

window = sg.Window("Gerador de Tabuada", layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED or event == "Sair":
        break
    if event == "Gerar Tabuada":
        numero = int(values["numero"])
        operacao = 0
        if values["soma"]:
            operacao = 1
        elif values["subtracao"]:
            operacao = 2
        elif values["divisao"]:
            operacao = 3
        elif values["multiplicacao"]:
            operacao = 4
        resultado = gerador_de_tabuada(numero, operacao)
        window["resultado"].update(resultado)

window.close()


#! /usr/bin/env python
from structureDamagePrediction.datahandling import StructuralDamageDataAndMetadataReader, StructuralDamageDataset
from datetime import datetime
from structureDamagePrediction.utils import StartEndLogger
import numpy as np
import structureDamagePrediction.models as models
from structureDamagePrediction.training import NeuralNetTrainer
from torch.utils.data import DataLoader
import torch, math, random
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
import scipy.stats as stats
import sys
import argparse

def get_accuracy_w_stderr(real_list, predicted_list):
    # Accuracy w/ statistical significance
    accuracy = 1.0 * sum([real_list[iCnt] == predicted_list[iCnt] for iCnt in range(len(real_list))]) / len(real_list)

    def acc(realAndPredTupleList):
        if realAndPredTupleList[0] == realAndPredTupleList[1]:
            return 1.0
        else:
            return 0.0

    # Mean and std error
    perFoldAcc = list(map(acc, zip(real_list,predicted_list)))
    avgAcc = np.average(perFoldAcc)
    stdErrAcc = np.std(perFoldAcc)/ np.sqrt(len(real_list))

    return avgAcc, stdErrAcc

def output_confusion_matrix(real_list, predicted_list, l):
    classes = list(map(str, set(real_list)))

    # Output confusion matrix
    l.log("Confusion matrix:")
    sHeader = "\tPredicted\n"
    sHeader += "\t%s"%("\t".join(classes))
    l.log("%s"%(sHeader), no_date=True)
    conf_matrix = metrics.confusion_matrix(real_list, predicted_list)

    sStr = ""
    for curClassTrue in classes:
        sStr += curClassTrue
        for curClassPred in classes:
            sStr += "\t" + str(conf_matrix[int(curClassTrue),int(curClassPred)])
        sStr += "\n"
    l.log(sStr, no_date=True)

    sFooter = "^True"
    l.log("%s"%(sFooter), no_date=True)

def main():
    # Setup reproducibility
    torch.manual_seed = 100
    random.seed(100)
    np.random.seed(100)

    # Set up reproducibility for data splitting
    def seed_worker(worker_id):
        worker_seed = torch.initial_seed() % 2**32
        np.random.seed(worker_seed)
        random.seed(worker_seed)
    torch_gen = torch.Generator()
    torch_gen.manual_seed(0)

    # Init utils
    l = StartEndLogger()
    # Init argument parser
    parser = argparse.ArgumentParser(description='Structural data analysis and prediction.')

    parser.add_argument("-b", "--baseDir",  help="The base directory of the dataset. (Default: data/)", default="data/")

    LEAVE_ONE_OUT = "leave-one-out"
    STRATIFY = "stratify"
    RANDOM = "random"
    parser.add_argument("-s", "--splittingMethod", help="The splitting method: leave-one-out or stratify or random. (Default: leave-one-out)",
                        choices=[LEAVE_ONE_OUT, STRATIFY, RANDOM],
                        default=LEAVE_ONE_OUT)
    parser.add_argument("-bsz", "--batchSize", type=int, help="The batch size of the training batches (where applicable). (Default: 5)", default=5)

    CLASSIFICATION = "classification"
    REGRESSION = "regression"
    parser.add_argument("-p", "--predictionType", help="Type of prediction: classification or regression. (Default: classification)",
                        choices=[CLASSIFICATION, REGRESSION], default=CLASSIFICATION)

    parser.add_argument("-e", "--epochs", help="Epochs for neural net training. (Default: 500)",
                        type=int, default=1000)

    SEQUENCE = 'sequence'
    FOURIER = 'fourier'
    parser.add_argument("-r", "--representation", help="Type of representation: sequence or fourier. (Default: sequence)",
                    choices=[SEQUENCE, FOURIER], default=FOURIER)

    parser.add_argument("-fn", "--fourierDimensions", type=int, help="Fourier dimensions to keep. (Default: 100)", default=100)

    def validate_int_none(value):
        if value.lower() == 'none':
            return None
        else:
            try:
                return(int(value))
            except ValueError:
                raise argparse.ArgumentTypeError("Invalid value. It must be either an integer or None.")

    LSTM = 'lstm'
    LINEAR = 'linear'
    MLP = 'mlp'
    KNN = 'knn'
    DUMMY = 'dummy'
    DUMMY_MAJORITY = 'dummy_majority'
    DECISION_TREE = 'decision_tree'
    BASELINE_MEAN = 'baseline_mean'
    parser.add_argument("-cl", "--classifier", choices=[LSTM,MLP,KNN,DUMMY, DUMMY_MAJORITY, DECISION_TREE],
                        help="Selected classifier. (Default: KNN)", default=KNN)
    # TODO: Add arguments for metadata field access as label
    parser.add_argument("-t", "--tupleIndexMetadata", help="Type the index of the tuple in the metadata. Either 1 for the damage percentage or 2 for the layers. (Default=1)",
                        type=int, choices=[1, 2], default=1)
    parser.add_argument("-ro", "--rowInMetadata", help="Type the row in the metadata. Type None if you want the damage percentage",
                         type=validate_int_none, choices=[None, 0, 1, 2, 3, 4], default=None)
    parser.add_argument("-co", "--colInMetadata", help="Type the column in the metadata. Type None if you want the damage percentage.",
                        type=validate_int_none, choices=[None, 0, 1, 2, 3], default=None)
    # TODO: Add arguments for patience

    parser.add_argument("-re", "--regressor", choices=[LSTM,MLP,LINEAR,BASELINE_MEAN],
                        help="Selected regressor. (Default: %s)"%(LINEAR), default=LINEAR)
    # Sequence breaking
    parser.add_argument("-bseq", "--breakSequence", action='store_true',
                        help="Break sequence into subsequences")
    parser.add_argument("-ss", "--subseqSize", type=int,
                        help="Size of subseqeuences that the sequence will be broken down into. ", default=10)

    # LSTM params
    parser.add_argument("-ll", "--lstmLayers", type=int,
                        help="Number of LSTM layers stacked. (Default: 1)", default=1)

    # Read arguments
    args = parser.parse_args(sys.argv[1:])
    base_dir = args.baseDir
    splitting = args.splittingMethod
    classification = args.predictionType == CLASSIFICATION
    # Assign classifier only if we chose classification
    # and rergressor only if we chose regression
    if classification:
        classifier = args.classifier
        args.regressor = None
        regressor = None
    else:
        classifier = None
        args.classifier = None
        regressor = args.regressor

    n_epochs = args.epochs
    fourier_dims = args.fourierDimensions
    representation = args.representation
    tup_in_met=args.tupleIndexMetadata
    row_in_met=args.rowInMetadata
    col_in_met=args.colInMetadata
    batch_size = args.batchSize

    # Select feature vector (sequence) transform function
    if representation == FOURIER:
        # Init transformation function
        def multidim_fft_transform(seq, dimensions=3, fourier_dimensions=fourier_dims):
            # Init concat
            ffts = []
            # For each dimension of the sequence
            for iDim in range(dimensions):
                # Get the data
                cur_dim_seq = seq[:, iDim]
                # Extract fourier
                spectrum = torch.fft.fft(cur_dim_seq, n=fourier_dimensions)
                # Store for concatenation
                ffts.append(spectrum.real)

            # Concatenate and return
            res = torch.cat(ffts)
            return res
        sequence_transform = multidim_fft_transform
        break_sequence = False
        subseq_size = None
    else:
        sequence_transform = None
        # In sequence representation examine if breaking down has been asked (for sequential models)
        break_sequence = args.breakSequence
        subseq_size = args.subseqSize

    l.log("Programme arguments:\n%s"%(str(args)))

    # Init reader
    reader = StructuralDamageDataAndMetadataReader(base_dir=base_dir)
    # Read data and metadata
    data, meta_data = reader.read_data_and_metadata()

    # Transformation function for classification
    transform_idx_list = [] # Init values list
    def transform_func(x):
        if tup_in_met not in [1,2]:
            raise argparse.ArgumentTypeError("Invalid value for tup_in_met. Type 1 or 2 for the tuple index.")

        # Search in existing list
        try:
            idx = transform_idx_list.index(x)
        except ValueError: # If not in list
            idx = len(transform_idx_list) # Make new entry and assign number
            l.log("Adding new class.. %d."%(idx))
            transform_idx_list.append(x)
        return idx

    # Regression (no change)
    if not classification:
        transform_func = None

    # Meta-data format
    # case_id, dmg_perc, dmg_tensor, dmg_loc_x, dmg_loc_y
    dataset = StructuralDamageDataset(data, meta_data,
                                      tgt_tuple_index_in_metadata=tup_in_met,  tgt_row_in_metadata=row_in_met, tgt_col_in_metadata=col_in_met, # What to use: dmg percentage
                                      label_transform_func=transform_func, feature_vector_transform_func=sequence_transform)

    # Update booleans
    leave_one_out = splitting == LEAVE_ONE_OUT
    stratify = splitting == STRATIFY

    if leave_one_out:
        number_of_runs = len(dataset)
    else:
        number_of_runs = 3

    predicted_list = []
    real_list = []

    for iRun in range(number_of_runs):
        l.log("+++++ Starting run #%d"%(iRun))

        if leave_one_out:
            test_perc = 1.0 / len(dataset)
        else:
            test_perc = 0.20

        if stratify:
            _, test_instance_idx = train_test_split(np.arange(len(dataset)),
                                                        test_size=test_perc,
                                                        random_state=5, # Reproducibility
                                                        shuffle=True,
                                                        stratify=list(dataset.labels())
                                                        )
        else:
            # Choose as test instance index the current run number
            if leave_one_out:
                test_instance_idx = [iRun]
            else:
                # Choose test instance indexes
                test_instance_idx=np.random.choice(list(range(0, len(dataset))),  size = math.ceil(test_perc * len(dataset)), replace=False)
                l.log("Selected instances: %s"%(str(test_instance_idx)))

        # Create train and test data
        train_data = []
        test_data = []
        for idx,entry in enumerate(dataset):
            if idx in test_instance_idx:
                test_data.append(entry)
            else:
                train_data.append(entry)

        l.log("Train / test sizes: %4d /%4d"%(len(train_data), len(test_data)))

        # Use batch size and create batches. (Note: batch size is meaningful only on the training part)
        train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, worker_init_fn=seed_worker, generator=torch_gen)
        test_dataloader = DataLoader(test_data, batch_size=1, shuffle=False)

        # Train model
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        l.log("Device for learning: %s"%(device.type))

        if classification:
            modelsLossesAndSupportedRepr={
                # NN
                LSTM: (models.LSTMClassificationModel(device=device, num_classes=3),torch.nn.CrossEntropyLoss(), [SEQUENCE]),
                MLP: (models.MLPClassifier(device=device,num_classes=3), torch.nn.CrossEntropyLoss(),[FOURIER]),
                # Non-NN
                KNN: (models.KNNModel(3, 3 * fourier_dims), None, [FOURIER]),
                DECISION_TREE: (models.DecisionTreeModel(3 * fourier_dims), None, [FOURIER]),
                DUMMY: (models.DummyModel(), None, [FOURIER]),
                DUMMY_MAJORITY: (models.DummyModel(strategy='most_frequent'), None, [FOURIER]),
            }

            # Classification
            #################
            # Neural netowrk models
            model, loss_fn, compat_repr = modelsLossesAndSupportedRepr[classifier]
            if representation not in compat_repr:
                raise RuntimeError("Model %s only supports representations: %s."%(classifier,str(compat_repr)))
        else:
            # Regression
            #############
            modelsLossesAndSupportedRepr={
                # NN
                LSTM: (models.LSTMRegressionModel(device=device, input_size=3, break_seq=break_sequence, subseq_max_size=subseq_size,
                                                  num_layers=args.lstmLayers),
                       torch.nn.L1Loss(), [SEQUENCE]),
                LINEAR: (models.LinearRegressor(input_size = 3 * fourier_dims), torch.nn.L1Loss(), [FOURIER]),
                MLP: (models.MLPRegressor(device=device,input_size = 3 * fourier_dims,num_classes=1), torch.nn.L1Loss(), [FOURIER]),
                BASELINE_MEAN: (models.BaselineMeanRegressor(), None, [FOURIER, SEQUENCE])
            }

            model, loss_fn, compat_repr = modelsLossesAndSupportedRepr[regressor]
            if representation not in compat_repr:
                raise RuntimeError("Model %s only supports representations: %s."%(classifier,str(compat_repr)))

        if not isinstance(model, models.SKLearnModel):
            trainer = NeuralNetTrainer(model,
                            optimizer=torch.optim.Adam(params=model.parameters(),
                                                                betas=(0.9, 0.999), eps=10e-7, lr=1e-4) ,
                            # optimizer=torch.optim.SGD(model.parameters(),lr=0.1, momentum=0.1),
                            n_epochs=n_epochs, device=device, loss_fn=loss_fn)
            trainer.train(train_dataloader,min_abs_loss_change=0.0001, patience_epochs=200, sufficient_loss=0.001, output_every=100)
            final_model = trainer.get_model()
        else:
            all_X = []
            all_y = []
            # Gather batches
            for X_batch, y_batch in train_dataloader:
                all_X.append(X_batch)
                all_y.append(y_batch)

            all_X = torch.cat(all_X)
            all_y = torch.cat(all_y)
            model.fit(all_X, all_y)
            final_model = model

        l.start("Validation...")
        if not isinstance(model, models.SKLearnModel):
            final_model.eval()
        with torch.no_grad():
            for X_test, y_test in test_dataloader:
                X_test = X_test.to(device)
                y_test = y_test.to(device)
                real_list.append(y_test.item())

                y_pred = final_model(X_test)
                if not isinstance(model, models.SKLearnModel):
                    test_loss = trainer.loss_fn(y_pred.cpu(), y_test.cpu()).cpu()
                else:
                    test_loss = torch.mean(torch.abs(y_pred.cpu() - y_test.cpu()), dtype=torch.float)

                if classification:
                    if not isinstance(model, models.SKLearnModel):
                        ypred_final = y_pred.max(1).indices
                    else:
                        ypred_final = y_pred.cpu()

                    predicted_list.append(ypred_final.cpu().item())
                    prc_loss = 0.0
                else:
                    ypred_final = y_pred.cpu().item()
                    predicted_list.append(ypred_final)
                    try:
                        prc_loss = 100 * test_loss.item() / (y_test.cpu().item() + 10e-10) # Ascertain no division by zero
                    except ZeroDivisionError:
                        prc_loss = float('nan')

                l.log("True: %8.6f -- Predicted: %8.6f (Loss: %8.6f; Percantile: %5.2f%%)"%(y_test.cpu().item(), ypred_final, test_loss ,prc_loss))

        l.end()

    l.log("Outputting overall results list:\nReal,Predicted")
    if classification:
        l.log("\n".join(map(lambda x: "%d,%d"%(x[0],x[1]),list(zip(real_list, predicted_list)))), no_date=True)
    else:
        l.log("\n".join(map(lambda x: "%6.4f,%6.4f"%(x[0],x[1]),list(zip(real_list, predicted_list)))), no_date=True)

    if classification:
        # Output measures
        f1_macro = metrics.f1_score(real_list, predicted_list, average="macro")
        avgAcc, stdErrAcc = get_accuracy_w_stderr(real_list, predicted_list)
        l.log("Avg accuracy  %6.4f+/- stderr %6.4f"%(avgAcc, stdErrAcc))
        l.log("F1 macro  %6.4f"%(f1_macro))

        # Confusion matrix
        output_confusion_matrix(real_list, predicted_list, l)

    else:
        mae = metrics.mean_absolute_error(real_list, predicted_list)
        corr, p = stats.spearmanr(real_list, predicted_list)
        l.log("MAE: %6.4f; Correlation: %6.4f (p-val: %6.4f)"%(mae, corr, p))

    l.log("Programme arguments:\n%s"%(str(args)))

if __name__ == "__main__":
    main()


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()


from setuptools import setup
import json

with open("metadata.json", encoding="utf-8") as fp:
    metadata = json.load(fp)

setup(
    name="lexibank_wangbai",
    description=metadata["title"],
    license=metadata.get("license", ""),
    url=metadata.get("url", ""),
    py_modules=["lexibank_wangbai"],
    include_package_data=True,
    zip_safe=False,
    entry_points={
        "lexibank.dataset": ["wangbai=lexibank_wangbai:Dataset"],
        "cldfbench.commands": ["wangbai=wangbaicommands"],
    },
    install_requires=["pylexibank>=3.0"],
    extras_require={"wangbai": ["sinopy", "lingrex", "python-igraph"]},
)


"""Author: Samuel DeLaughter
12/6/14

This program graphs the probability of landing on each integer in a given interval for a 1D random-walk simulation
If called with a -i flag, it will prompt for user input on the following variables:
	Boundary numbers, starting position, number of steps per simulation, number of simulations

"""

import sys
import random
import numpy as np
import matplotlib.pyplot as plt

#Set default values for the variables
MIN_POSITION = -10
MAX_POSITION = 10
START_POSITION = 0
NSTEPS = 12
NSIMS = 1000

if('-i' in sys.argv):
#Get user input for variables if called with the -i argument
	MIN_POSITION = int(raw_input("Enter lower boundary: ") or MIN_POSITION)
	MAX_POSITION = int(raw_input("Enter upper boundary: ") or MAX_POSITION)

	#If the upper boundary is smaller than the lower boundary, swap them
	sorted_bounds = sorted((MIN_POSITION, MAX_POSITION))
	MIN_POSITION = sorted_bounds[0]
	MAX_POSITION = sorted_bounds[1]

	#If the two boundaries are equivalent, increment the upper bound
	if(MIN_POSITION == MAX_POSITION):
		MAX_POSITION+=1

	START_POSITION = int(raw_input("Enter starting position: ") or START_POSITION)
	#Make sure the starting position is within the min/max bounds

	START_POSITION=sorted((MIN_POSITION, START_POSITION, MAX_POSITION))[1]

	NSTEPS = int(raw_input("Enter number of steps for each simulation: ") or NSTEPS)
	NSIMS = int(raw_input("Enter number of simulations to run: ") or NSIMS)

def move(position):
#Change the current position
	if(position == MAX_POSITION):
		#If you're at the upper boundary, either stay put or decrease
		step=random.randint(-1, 0)
	elif(position == MIN_POSITION):
		#If you're at the lower boundary, either stay put or increase
		step=random.randint(0, 1)
	else:
		#If you're within the boundaries, either stay put, decrease, or increase
		step=random.randint(-1, 1)
	position += step
	return position

def sim():
#Run a simulation
	position=START_POSITION
	for i in range(NSTEPS):
		position=move(position)
	return position

def main():
	results=[]
	for i in range(NSIMS):
		results.append(sim())
	counts=[]
	for i in range(MIN_POSITION, (MAX_POSITION+1)):
		counts.append(results.count(i))

	plt.plot(range(MIN_POSITION, (MAX_POSITION+1)), counts, 'ro')
	#Set axis intervals
	plt.xticks(np.arange(MIN_POSITION, (MAX_POSITION + 1), 1.0))
	#plt.yticks(np.arange(0, max(counts), 100))
	#Display gridlines
	plt.grid(b=True, which=u'major', axis=u'both')
	plt.show()

	#Make the plot window visible
	wm = plt.get_current_fig_manager()
	wm.window.attributes('-topmost', 0)

if __name__ == '__main__':
    main()


import csv
import io
import pandas as pd
from tqdm import tqdm

filename = 'data/merge1.csv'
df = pd.read_csv(filename)
data = []
for index, row in tqdm(df.iterrows(), total=df.shape[0]):
    count = row[5]
    if count != 0:
        data.append([row[0], row[1], row[2], row[3], row[4], row[5]])

with io.open("data/filter_properties_count.csv", mode='w', encoding='UTF8', newline='') as toWrite:
    writer = csv.writer(toWrite)
    writer.writerow(["Name", "Label", "Domain", "Range", "URI", "Count"])
    writer.writerows(data)

2018 - Neural QA model/filter_properties_count.py