# coding: utf-8

"""
    Bacalhau API

    This page is the reference of the Bacalhau REST API. Project docs are available at https://docs.bacalhau.org/. Find more information about Bacalhau at https://github.com/bacalhau-project/bacalhau.  # noqa: E501

    OpenAPI spec version: ${PYPI_VERSION}
    Contact: team@bacalhau.org
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class ResourceUsageData(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {"cpu": "float", "disk": "int", "gpu": "int", "memory": "int"}

    attribute_map = {"cpu": "CPU", "disk": "Disk", "gpu": "GPU", "memory": "Memory"}

    def __init__(self, cpu=None, disk=None, gpu=None, memory=None):  # noqa: E501
        """ResourceUsageData - a model defined in Swagger"""  # noqa: E501
        self._cpu = None
        self._disk = None
        self._gpu = None
        self._memory = None
        self.discriminator = None
        if cpu is not None:
            self.cpu = cpu
        if disk is not None:
            self.disk = disk
        if gpu is not None:
            self.gpu = gpu
        if memory is not None:
            self.memory = memory

    @property
    def cpu(self):
        """Gets the cpu of this ResourceUsageData.  # noqa: E501

        cpu units  # noqa: E501

        :return: The cpu of this ResourceUsageData.  # noqa: E501
        :rtype: float
        """
        return self._cpu

    @cpu.setter
    def cpu(self, cpu):
        """Sets the cpu of this ResourceUsageData.

        cpu units  # noqa: E501

        :param cpu: The cpu of this ResourceUsageData.  # noqa: E501
        :type: float
        """

        self._cpu = cpu

    @property
    def disk(self):
        """Gets the disk of this ResourceUsageData.  # noqa: E501

        bytes  # noqa: E501

        :return: The disk of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._disk

    @disk.setter
    def disk(self, disk):
        """Sets the disk of this ResourceUsageData.

        bytes  # noqa: E501

        :param disk: The disk of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._disk = disk

    @property
    def gpu(self):
        """Gets the gpu of this ResourceUsageData.  # noqa: E501

        :return: The gpu of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._gpu

    @gpu.setter
    def gpu(self, gpu):
        """Sets the gpu of this ResourceUsageData.

        :param gpu: The gpu of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._gpu = gpu

    @property
    def memory(self):
        """Gets the memory of this ResourceUsageData.  # noqa: E501

        bytes  # noqa: E501

        :return: The memory of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._memory

    @memory.setter
    def memory(self, memory):
        """Sets the memory of this ResourceUsageData.

        bytes  # noqa: E501

        :param memory: The memory of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._memory = memory

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(
                    map(lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value)
                )
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(
                    map(
                        lambda item: (item[0], item[1].to_dict())
                        if hasattr(item[1], "to_dict")
                        else item,
                        value.items(),
                    )
                )
            else:
                result[attr] = value
        if issubclass(ResourceUsageData, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, ResourceUsageData):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


import io
import struct
from typing import Iterable

from antlir.btrfs_diff.parse_send_stream import (
    AttributeKind,
    check_magic,
    check_version,
    CommandKind,
    file_unpack,
    parse_send_stream,
    read_attribute,
    read_command,
)
from antlir.btrfs_diff.send_stream import SendStreamItem
from antlir.btrfs_diff.tests.demo_sendstreams import gold_demo_sendstreams
from antlir.btrfs_diff.tests.demo_sendstreams_expected import (
    get_filtered_and_expected_items,
)

from antlir.tests.common import AntlirTestCase

def _parse_stream_bytes(s: bytes) -> Iterable[SendStreamItem]:
    return parse_send_stream(io.BytesIO(s))

class ParseSendStreamTestCase(AntlirTestCase):
    def test_verify_gold_parse(self) -> None:
        stream_dict = gold_demo_sendstreams()
        filtered_items, expected_items = get_filtered_and_expected_items(
            items=[
                *_parse_stream_bytes(stream_dict["create_ops"]["sendstream"]),
                *_parse_stream_bytes(stream_dict["mutate_ops"]["sendstream"]),
            ],
            build_start_time=stream_dict["create_ops"]["build_start_time"],
            build_end_time=stream_dict["mutate_ops"]["build_end_time"],
            dump_mode=False,
        )
        self.assertEqual(filtered_items, expected_items)

    def test_errors(self) -> None:
        with self.assertRaisesRegex(RuntimeError, "Magic b'xxx', not "):
            check_magic(io.BytesIO(b"xxx"))
        with self.assertRaisesRegex(RuntimeError, "but expected version 1"):
            check_version(io.BytesIO(b"abcd"), expected=1)
        with self.assertRaisesRegex(RuntimeError, "Not enough bytes"):
            file_unpack("<Q", io.BytesIO(b""))

        cmd_header_2_attrs = struct.pack(
            "<IHI",
            2 * (2 + 2 + 3),  # length excluding this header
            CommandKind.MKFILE.value,
            0,  # crc32c
        )

        with self.assertRaisesRegex(RuntimeError, "CommandHead.* got 0 bytes"):
            read_command(io.BytesIO(cmd_header_2_attrs))

        with self.assertRaisesRegex(RuntimeError, "AttributeH.* got 0 bytes"):
            read_attribute(
                io.BytesIO(
                    struct.pack(
                        "<HH",
                        AttributeKind.PATH.value,
                        # length excluding this header -- error: we write no
                        # data!
                        3,
                    )
                )
            )

        with self.assertRaisesRegex(RuntimeError, "\\.PATH occurred twice"):
            read_command(
                io.BytesIO(
                    cmd_header_2_attrs
                    + struct.pack(
                        "<" + "HH3s" * 2,  # 2 attributes
                        AttributeKind.PATH.value,
                        3,  # length excluding this header
                        b"cat",
                        AttributeKind.PATH.value,
                        3,  # length excluding this header
                        b"dog",
                    )
                )
            )


import tensorflow as tf
import tensorflow_probability as tfp

ds = tfp.distributions

class SeqVAE(object):

    def __init__(self, hparams, encoder, decoder_theta, decoder_beta, discriminator):
        self._hparams = hparams

        self.encoder = encoder
        self.decoder_theta = decoder_theta
        self.decoder_beta = decoder_beta
        self.discriminator = discriminator

        # z's prior distribution
        self.z_p = ds.MultivariateNormalDiag(
            loc=[0.] * self._hparams.z_size,
            scale_diag=[1.] * self._hparams.z_size)

        # global step
        self._global_step = tf.train.get_or_create_global_step()

    def compute_loss(self, target_sequence, sequence_len):
        # encoder ==> kl divergence
        z_q = self.encoder.encode(target_sequence, sequence_len)
        z = z_q.sample()
        # compute formula:https://zhuanlan.zhihu.com/p/22464760
        self._kl_div = ds.kl_divergence(z_q, self.z_p)
        # <Improving variational inference with inverse auto regressive flow>, NIPS 2016
        # Bits to exclude from KL loss per dimension.
        free_nats = self._hparams.free_bits * tf.math.log(2.0)
        self.kl_cost = tf.reduce_mean(tf.maximum(self._kl_div - free_nats, 0))
        # kl divergence scalar
        self.kl_bits_scalar = tf.summary.scalar("vae-pg/kl_bits", tf.reduce_mean(self._kl_div / tf.math.log(2.0)))
        self.kl_loss_scalar = tf.summary.scalar("vae-pg/kl_loss", self.kl_cost)

        # decoder & discriminator ==> rewards
        hparams = self._hparams
        kl_loss_beta = (1.0 - tf.pow(hparams.beta_decay_rate, tf.to_float(self._global_step))) * hparams.max_beta
        tf.summary.scalar("vae-pg/kl_loss_beta", kl_loss_beta)
        step = hparams.compute_rewards_step
        max_seq_len = hparams.max_seq_len
        batch_size = hparams.batch_size
        rollout_num = hparams.rollout_num
        self.pg_loss = 0

        _, output_sm = self.decoder_theta.generate_part(z, target_sequence, max_seq_len)

        # accuracy
        truth = tf.argmax(target_sequence, axis=2)
        predictions = tf.argmax(output_sm, axis=2)
        self.train_accuracy_real = tf.reduce_sum(tf.cast(tf.equal(truth, predictions), tf.float32)) \
                                   / (1.0 * batch_size * max_seq_len)
        tf.summary.scalar("vae-pg/train_accuracy_real", self.train_accuracy_real)

        rewards = []
        for k in range(rollout_num):
            count = 0
            for i in range(step, max_seq_len, step):
                gen_part_output, _ = self.decoder_beta.generate_part(z, target_sequence, i)
                scores_sm, _ = self.discriminator.discriminate(gen_part_output)
                reward = tf.squeeze(scores_sm[:, :1], 1)
                if k == 0:
                    rewards.append(reward)
                else:
                    rewards[count] += reward
                count += 1

            # the last token reward
            scores_sm, _ = self.discriminator.discriminate(target_sequence)
            reward = tf.squeeze(scores_sm[:, :1], 1)
            if k == 0:
                rewards.append(reward)
            else:
                rewards[count] += reward

        rewards = [reward / (1.0 * rollout_num) for reward in rewards]
        rewards = tf.transpose(tf.convert_to_tensor(rewards), perm=[1, 0])  # batch_size * (max_seq_len // step)

        # make log(output_sm) not get nan
        output_sm = tf.clip_by_value(output_sm, 1e-20, 1.0)
        log_signal = tf.reduce_sum(target_sequence * tf.log(output_sm), axis=2)  # batch_size * max_seq_len
        log_sum = []
        for i in range(max_seq_len // step):
            log_sum.append(tf.reduce_sum(log_signal[:, i * step:(i + 1) * step], axis=1))
        log_sum = tf.transpose(tf.convert_to_tensor(log_sum), perm=[1, 0])

        pg_loss = tf.reduce_sum(log_sum * rewards, axis=1)  # batch_size
        self.pg_loss = - tf.reduce_mean(pg_loss)
        tf.summary.scalar("vae-pg/pg_loss", self.pg_loss)

        # VAE loss
        # <Generating sentences from a continuous space>, CONLL 2016
        self.vae_loss = kl_loss_beta * self.kl_cost + self.pg_loss
        tf.summary.scalar("vae-pg/vae_loss", self.vae_loss)

        return self.vae_loss

    def discriminator_loss(self, inputs, labels, dropout_keep_prob=1.0):
        _, self.dis_loss = self.discriminator.discriminate(inputs, labels, dropout_keep_prob)
        return self.dis_loss

    def generate(self, z):
        return self.decoder_theta.generate(z)

    def update_decoder_beta(self):
        return self.decoder_beta.update(self.decoder_theta.collect_all_variables())


from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import matplotlib.pyplot as plt

from tensorflow import keras

y = np.load('labels/00e5671e594a6fe621c3605fcc5a0e4466ba6478.npy')
x = np.rot90(np.load('spectro/00e5671e594a6fe621c3605fcc5a0e4466ba6478.npy'))
x = x[0:y.shape[0]]
x = np.expand_dims(x, axis=2)

print('x.shape', x.shape)
print('y.shape', y.shape)

model = keras.Sequential()
model.add(keras.layers.LSTM(128))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history = model.fit(x, y, epochs=250, shuffle=False)
print(model.summary())
# score = model.evaluate(x_test, y_test)
# print(score)

plt.plot(history.history['accuracy'])
plt.plot(history.history['loss'])
plt.title('onset by neural network')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['accuracy', 'loss'], loc='upper left')
plt.show()


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('rtiapp', '0031_auto_20151228_2331'),
    ]

    operations = [
        migrations.CreateModel(
            name='Message',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message_text', models.TextField()),
                ('read_status', models.BooleanField(default=False)),
                ('message_date', models.DateTimeField(auto_now_add=True)),
                ('receiver', models.ForeignKey(related_name='receiver', to=settings.AUTH_USER_MODEL, null=True)),
                ('sender', models.ForeignKey(related_name='sender', to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 23 02:12:21 2017

@author: JonStewart
"""

# import libraries
from __future__ import print_function
from __future__ import division

import numpy as np
import pandas as pd
import os
import re

from tflearn.datasets import mnist
from keras.models import Model, Sequential
from keras.layers import *
from keras.optimizers import Adam
# from tqdm import tqdm
from keras.layers.advanced_activations import LeakyReLU
import numpy as np
import matplotlib.pyplot as plt
import random
from timeit import default_timer as timer

# ---------------------------------------------------------------------------------------
# Constants
#
# INPUT_FOLDER:                 The folder that contains the source data
#
# PREPROCESSED_DATA_FOLDER:     The folder that contains preprocessed .npy files
#
# STAGE1_LABELS:                The CSV file containing the labels by subject
#
# THREAT_ZONE:                  Threat Zone to train on (actual number not 0 based)
#
# BATCH_SIZE:                   Number of Subjects per batch
#
# EXAMPLES_PER_SUBJECT          Number of examples generated per subject
#
# FILE_LIST:                    A list of the preprocessed .npy files to batch
#
# TRAIN_TEST_SPLIT_RATIO:       Ratio to split the FILE_LIST between train and test
#
# TRAIN_SET_FILE_LIST:          The list of .npy files to be used for training
#
# TEST_SET_FILE_LIST:           The list of .npy files to be used for testing
#
# IMAGE_DIM:                    The height and width of the images in pixels
#
# LEARNING_RATE                 Learning rate for the neural network
#
# N_TRAIN_STEPS                 The number of train steps (epochs) to run
#
# TRAIN_PATH                    Place to stor                           e the tensorboard logs
#
# MODEL_PATH                    Path where model files are stored
#
# MODEL_NAME                    Name of the model files
#
# ----------------------------------------------------------------------------------------
INPUT_FOLDER = 'D:\\stage1_aps'
PREPROCESSED_DATA_FOLDER = 'D:\\kaggle_tsa\\stage_1dproc'
STAGE1_LABELS = 'D:\\kaggle_tsa\\stage1_labels.csv'
THREAT_ZONE = 1
BATCH_SIZE = 16
THREAT_PRESENT = 1
EXAMPLES_PER_SUBJECT = 182

FILE_LIST = []
TRAIN_TEST_SPLIT_RATIO = 0.2
TRAIN_SET_FILE_LIST = []
TEST_SET_FILE_LIST = []

IMAGE_DIM = 224
LEARNING_RATE = 1e-3
N_TRAIN_STEPS = 1
TRAIN_PATH = 'D:\\kaggle_tsa\\tsa_logs\\train'
MODEL_PATH = 'D:\\kaggle_tsa\\tsa_logs\\model'
MODEL_NAME = ('tsa-{}-lr-{}-{}-{}-tz-{}'.format('alexnet-v0.1', LEARNING_RATE, IMAGE_DIM,
                                                IMAGE_DIM, THREAT_ZONE))

# ---------------------------------------------------------------------------------------
# preprocess_tsa_data(): preprocesses the tsa datasets
#
# parameters:      none
#
# returns:         none
# ---------------------------------------------------------------------------------------

def preprocess_tsa_data():
    # OPTION 1: get a list of all subjects for which there are labels
    df = pd.read_csv(STAGE1_LABELS)
    df['Subject'], df['Zone'] = df['Id'].str.split('_', 1).str
    SUBJECT_LIST = df['Subject'].unique()

    # OPTION 2: get a list of all subjects for whom there is data
    # SUBJECT_LIST = [os.path.splitext(subject)[0] for subject in os.listdir(INPUT_FOLDER)]

    # OPTION 3: get a list of subjects for small bore test purposes
    # SUBJECT_LIST = ['00360f79fd6e02781457eda48f85da90','0043db5e8c819bffc15261b1f1ac5e42',
    #                '0050492f92e22eed3474ae3a6fc907fa','006ec59fa59dd80a64c85347eef810c7',
    #                '0097503ee9fa0606559c56458b281a08','011516ab0eca7cad7f5257672ddde70e']

    # intialize tracking and saving items
    batch_num = 1
    threat_zone_examples = []
    start_time = timer()

    for subject in SUBJECT_LIST:

        # read in the images
        print('--------------------------------------------------------------')
        print('t+> {:5.3f} |Reading images for subject #: {}'.format(timer() - start_time,
                                                                     subject))
        print('--------------------------------------------------------------')
        images = tsa.read_data(INPUT_FOLDER + '/' + subject + '.aps')

        # transpose so that the slice is the first dimension shape(16, 620, 512)
        images = images.transpose()

        # for each threat zone, loop through each image, mask off the zone and then crop it
        for tz_num, threat_zone_x_crop_dims in enumerate(zip(tsa.zone_slice_list,
                                                             tsa.zone_crop_list)):

            threat_zone = threat_zone_x_crop_dims[0]
            crop_dims = threat_zone_x_crop_dims[1]

            # get label
            label = np.array(tsa.get_subject_zone_label(tz_num,
                                                        tsa.get_subject_labels(STAGE1_LABELS, subject)))
            if label == '1':
                for img_num, img in enumerate(images):

                    print('Threat Zone:Image -> {}:{}'.format(tz_num, img_num))
                    print('Threat Zone Label -> {}'.format(label))

                    if threat_zone[img_num] is not None:

                        # correct the orientation of the image
                        print('-> reorienting base image')
                        base_img = np.flipud(img)
                        print('-> shape {}|mean={}'.format(base_img.shape,
                                                           base_img.mean()))

                        # convert to grayscale
                        print('-> converting to grayscale')
                        rescaled_img = tsa.convert_to_grayscale(base_img)
                        print('-> shape {}|mean={}'.format(rescaled_img.shape,
                                                           rescaled_img.mean()))

                        # spread the spectrum to improve contrast
                        print('-> spreading spectrum')
                        high_contrast_img = tsa.spread_spectrum(rescaled_img)
                        print('-> shape {}|mean={}'.format(high_contrast_img.shape,
                                                           high_contrast_img.mean()))

                        # get the masked image
                        print('-> masking image')
                        masked_img = tsa.roi(high_contrast_img, threat_zone[img_num])
                        print('-> shape {}|mean={}'.format(masked_img.shape,
                                                           masked_img.mean()))

                        # crop the image
                        print('-> cropping image')
                        cropped_img = tsa.crop(masked_img, crop_dims[img_num])
                        print('-> shape {}|mean={}'.format(cropped_img.shape,
                                                           cropped_img.mean()))

                        # normalize the image
                        print('-> normalizing image')
                        normalized_img = tsa.normalize(cropped_img)
                        print('-> shape {}|mean={}'.format(normalized_img.shape,
                                                           normalized_img.mean()))

                        # zero center the image
                        print('-> zero centering')
                        zero_centered_img = tsa.zero_center(normalized_img)
                        print('-> shape {}|mean={}'.format(zero_centered_img.shape,
                                                           zero_centered_img.mean()))

                        # append the features and labels to this threat zone's example array
                        print('-> appending example to threat zone {}'.format(tz_num))
                        threat_zone_examples.append([[tz_num], zero_centered_img, label])
                        print('-> shape {:d}:{:d}:{:d}:{:d}:{:d}:{:d}'.format(
                            len(threat_zone_examples),
                            len(threat_zone_examples[0]),
                            len(threat_zone_examples[0][0]),
                            len(threat_zone_examples[0][1][0]),
                            len(threat_zone_examples[0][1][1]),
                            len(threat_zone_examples[0][2])))
                    else:
                        print('-> No view of tz:{} in img:{}. Skipping to next...'.format(
                            tz_num, img_num))
                    print('------------------------------------------------')

        # each subject gets EXAMPLES_PER_SUBJECT number of examples (182 to be exact,
        # so this section just writes out the the data once there is a full minibatch
        # complete.
        if len(threat_zone_examples) % (BATCH_SIZE * EXAMPLES_PER_SUBJECT) == 0:
            for tz_num, tz in enumerate(tsa.zone_slice_list):
                tz_examples_to_save = []

                # write out the batch and reset
                print(' -> writing: ' + PREPROCESSED_DATA_FOLDER +
                      'preprocessed_TSA_scans-tz{}-{}-{}-b{}.npy'.format(
                          tz_num + 1,
                          len(threat_zone_examples[0][1][0]),
                          len(threat_zone_examples[0][1][1]),
                          batch_num))

                # get this tz's examples
                tz_examples = [example for example in threat_zone_examples if example[0] ==
                               [tz_num]]

                # drop unused columns
                tz_examples_to_save.append([[features_label[1], features_label[2]]
                                            for features_label in tz_examples])

                # save batch.  Note that the trainer looks for tz{} where {} is a
                # tz_num 1 based in the minibatch file to select which batches to
                # use for training a given threat zone
                np.save(PREPROCESSED_DATA_FOLDER +
                        'preprocessed_TSA_scans-tz{}-{}-{}-b{}.npy'.format(tz_num + 1,
                                                                           len(threat_zone_examples[0][1][0]),
                                                                           len(threat_zone_examples[0][1][1]),
                                                                           batch_num),
                        tz_examples_to_save)
                del tz_examples_to_save

            # reset for next batch
            del threat_zone_examples
            threat_zone_examples = []
            batch_num += 1

    # we may run out of subjects before we finish a batch, so we write out
    # the last batch stub
    if len(threat_zone_examples) > 0:
        for tz_num, tz in enumerate(tsa.zone_slice_list):
            tz_examples_to_save = []

            # write out the batch and reset
            print(' -> writing: ' + PREPROCESSED_DATA_FOLDER
                  + 'preprocessed_TSA_scans-tz{}-{}-{}-b{}.npy'.format(tz_num + 1,
                                                                       len(threat_zone_examples[0][1][0]),
                                                                       len(threat_zone_examples[0][1][1]),
                                                                       batch_num))

            # get this tz's examples
            tz_examples = [example for example in threat_zone_examples if example[0] ==
                           [tz_num]]

            # drop unused columns
            tz_examples_to_save.append([[features_label[1], features_label[2]]
                                        for features_label in tz_examples])

            # save batch
            np.save(PREPROCESSED_DATA_FOLDER +
                    'preprocessed_TSA_scans-tz{}-{}-{}-b{}.npy'.format(tz_num + 1,
                                                                       len(threat_zone_examples[0][1][0]),
                                                                       len(threat_zone_examples[0][1][1]),
                                                                       batch_num),
                    tz_examples_to_save)

# unit test ---------------------------------------
# preprocess_tsa_data()

# ---------------------------------------------------------------------------------------
# get_train_test_file_list(): gets the batch file list, splits between train and test
#
# parameters:      none
#
# returns:         none
#
# -------------------------------------------------------------------------------------

def get_train_test_file_list():
    global FILE_LIST
    global TRAIN_SET_FILE_LIST
    global TEST_SET_FILE_LIST

    if os.listdir(PREPROCESSED_DATA_FOLDER) == []:
        print('No preprocessed data available.  Skipping preprocessed data setup..')
    else:
        FILE_LIST = [f for f in os.listdir(PREPROCESSED_DATA_FOLDER)
                     if re.search(re.compile('-tz' + str(THREAT_ZONE) + '-'), f)]
        train_test_split = len(FILE_LIST) - \
                           max(int(len(FILE_LIST) * TRAIN_TEST_SPLIT_RATIO), 1)
        TRAIN_SET_FILE_LIST = FILE_LIST[:train_test_split]
        TEST_SET_FILE_LIST = FILE_LIST[train_test_split:]
        print('Train/Test Split -> {} file(s) of {} used for testing'.format(
            len(FILE_LIST) - train_test_split, len(FILE_LIST)))

# unit test ----------------------------
# get_train_test_file_list()
# print (

# ---------------------------------------------------------------------------------------
# input_pipeline(filename, path): prepares a batch of features and labels for training
#
# parameters:      filename - the file to be batched into the model
#                  path - the folder where filename resides
#
# returns:         feature_batch - a batch of features to train or test on
#                  label_batch - a batch of labels related to the feature_batch
#
# ---------------------------------------------------------------------------------------

def input_pipeline(filename, path):
    preprocessed_tz_scans = []
    feature_batch = []
    label_batch = []
    feature_batch_with_threat = []
    label_batch_with_threat = []
    feature_batch_wo_threat = []
    label_batch_wo_threat = []
    # Load a batch of preprocessed tz scans
    preprocessed_tz_scans = np.load(os.path.join(path, filename))

    # Shuffle to randomize for input into the model
    np.random.shuffle(preprocessed_tz_scans)
    # the below implies that input pipline of preprocessed tz scans already has label type
    # separate features and labels
    for example_list in preprocessed_tz_scans:
        for example in example_list:
            feature_batch.append(example[0])
            label_batch.append(example[1])
            # below is added
            if example[1] == 1:
                feature_batch_with_threat.append(example[0])
                label_batch_with_threat(example[1])
            else:
                feature_batch_wo_threat.append(example[0])
                label_batch_wo_threat.append(example[1])

    feature_batch = np.asarray(feature_batch, dtype=np.float32)
    label_batch = np.asarray(label_batch, dtype=np.float32)
    feature_batch_with_threat = np.asarray(feature_batch_with_threat, dtype=np.float32)
    label_batch_with_threat = np.asarray(label_batch_with_threat, dtype=np.float32)
    feature_batch_wo_threat = np.asarray(feature_batch_wo_threat, dtype=np.float32)
    label_batch_wo_threat = np.asarray(label_batch_wo_threat, dtype=np.float32)

    return feature_batch, label_batch, feature_batch_with_threat, label_batch_with_threat, label_batch_wo_threat, feature_batch_wo_threat

# unit test ------------------------------------------------------------------------
# print ('Train Set -----------------------------')
# for f_in in TRAIN_SET_FILE_LIST:
#    feature_batch, label_batch = input_pipeline(f_in, PREPROCESSED_DATA_FOLDER)
#    print (' -> features shape {}:{}:{}'.format(len(feature_batch),
#                                                len(feature_batch[0]),
#                                                len(feature_batch[0][0])))
#    print (' -> labels shape   {}:{}'.format(len(label_batch), len(label_batch[0])))

# print ('Test Set -----------------------------')
# for f_in in TEST_SET_FILE_LIST:
#    feature_batch, label_batch = input_pipeline(f_in, PREPROCESSED_DATA_FOLDER)
#    print (' -> features shape {}:{}:{}'.format(len(feature_batch),
#                                                len(feature_batch[0]),
#                                                len(feature_batch[0][0])))
#    print (' -> labels shape   {}:{}'.format(len(label_batch), len(label_batch[0])))

# ---------------------------------------------------------------------------------------
# shuffle_train_set(): shuffle the list of batch files so that each train step
#                      receives them in a different order since the TRAIN_SET_FILE_LIST
#                      is a global
#
# parameters:      train_set - the file listing to be shuffled
#
# returns:         none
#
# -------------------------------------------------------------------------------------

def shuffle_train_set(train_set):
    sorted_file_list = random.shuffle(train_set)
    TRAIN_SET_FILE_LIST = sorted_file_list

# Unit test ---------------
# print ('Before Shuffling ->', TRAIN_SET_FILE_LIST)
# shuffle_train_set(TRAIN_SET_FILE_LIST)
# print ('After Shuffling ->', TRAIN_SET_FILE_LIST)

# ---------------------------------------------------------------------------------------
# alexnet(width, height, lr): defines the alexnet
#
# parameters:      width - width of the input image
#                  height - height of the input image
#                  lr - learning rate
#
# returns:         none
#
# -------------------------------------------------------------------------------------

def alexnet(width, height, lr):
    network = input_data(shape=[None, width, height, 1], name='features')
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='momentum', loss='categorical_crossentropy',
                         learning_rate=lr, name='labels')

    model = tflearn.DNN(network, checkpoint_path=MODEL_PATH + MODEL_NAME,
                        tensorboard_dir=TRAIN_PATH, tensorboard_verbose=3, max_checkpoints=1)

    return model

# ---------------------------------------------------------------------------------------
# train_conv_net(): runs the train op
#
# parameters:      none
#
# returns:         none
#
# -------------------------------------------------------------------------------------

def train_conv_net():
    val_features = []
    val_labels = []
    val_threatfeatures = []
    val_threatlabels = []
    val_nothreatlabels = []
    val_nothreatfeatures = []
    # get train and test batches
    get_train_test_file_list()

    # instantiate model
    model = alexnet(IMAGE_DIM, IMAGE_DIM, LEARNING_RATE)

    # read in the validation test set
    for j, test_f_in in enumerate(TEST_SET_FILE_LIST):
        if j == 0:
            val_features, val_labels, val_threatfeatures, val_threatlabels, val_nothreatfeatures, val_nothreatlabels = input_pipeline(
                test_f_in, PREPROCESSED_DATA_FOLDER)
        else:
            tmp_feature_batch, tmp_label_batch, tmp_threatfeaturebatch, tmp_threatlabelbatch, tmp_nothreatlabelbatch, tmp_nothreatfeaturebatch = input_pipeline(
                test_f_in,
                PREPROCESSED_DATA_FOLDER)
            val_features = np.concatenate((tmp_feature_batch, val_features), axis=0)
            val_labels = np.concatenate((tmp_label_batch, val_labels), axis=0)
            val_threatlabels = np.concatenate((tmp_threatlabelbatch, val_threatlabels), axis=0)
            val_threatfeatures = np.concatenate((tmp_threatfeaturebatch, val_featurebatch), axis=0)
            val_nothreatlabels = np.concatenate((tmp_nothreatlabelbatch, val_nothreatlabels), axis=0)
            val_nothreatfeatures = np.concatenate((tmp_nothreatfeaturebatch, val_nothreatfeatures), axis=0)
    val_features = val_features.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)
    val_threatfeatures = val_threatfeatures.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)
    val_nothreatlabels = val_nothreatlabels.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)

    # start training process
    for i in range(N_TRAIN_STEPS):

        # shuffle the train set files before each step
        shuffle_train_set(TRAIN_SET_FILE_LIST)

        # run through every batch in the training set
        for f_in in TRAIN_SET_FILE_LIST:

            # read in a batch of features and labels for training
            feature_batch, label_batch, threatfeaturebatch, threatlabelbatch, nothreatlabelbatch, nothreatfeaturebatch = input_pipeline(
                f_in, PREPROCESSED_DATA_FOLDER)
            feature_batch = feature_batch.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)
            threatfeaturebatch = threatfeaturebatch.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)
            nothreatfeaturebatch = nothreatfeaturebatch.reshape(-1, IMAGE_DIM, IMAGE_DIM, 1)

            # print ('Feature Batch Shape ->', feature_batch.shape)

            # run the fit operation
            # model.fit({'features': feature_batch}, {'labels': label_batch}, n_epoch=1,
            #          validation_set=({'features': val_features}, {'labels': val_labels}),
            #          shuffle=True, snapshot_step=None, show_metric=True,
            #          run_id=MODEL_NAME)

            # unit test -----------------------------------
            # train_conv_net()

            # Data loading and preprocessing
            # this data, which it gets from the pipeline, is only images containing threats
            X_train = threatfeaturebatch.reshape(X_train.shape[0], 224, 224, 1)
            X_test = val_threatfeatures.reshape(X_test.shape[0], 224, 224, 1)

            X_train = X_train.astype('float32')

            # Scaling the range of the image to [-1, 1]
            # Because we are using tanh as the activation function in the last layer of the generator
            # and tanh restricts the weights in the range [-1, 1]
            X_train = (X_train - 127.5) / 127.5

            X_train.shape
            # 1           #the below generator is build for size 225,224,1 sized inputs. That was our resizing issue last time
            generator = Sequential([
                Dense(128 * 7 * 7, input_dim=200, activation=LeakyReLU(0.2)),
                BatchNormalization(),
                Reshape((7, 7, 128)),
                UpSampling2D(),
                Convolution2D(250, 5, 5, border_mode='same', activation=LeakyReLU(0.2)),
                BatchNormalization(),
                UpSampling2D(),
                Convolution2D(1, 5, 5, border_mode='same', activation=LeakyReLU(0.2)),
                BatchNormalization(),
                UpSampling2D(),
                Convolution2D(1, 5, 5, border_mode='same', activation=LeakyReLU(0.2)),
                BatchNormalization(),
                UpSampling2D(),
                Convolution2D(1, 5, 5, border_mode='same', activation=LeakyReLU(0.2)),
                BatchNormalization(),
                UpSampling2D(),
                Convolution2D(1, 5, 5, border_mode='same', activation='tanh')

            ])
            # this also has to accept 224,224,1 sized input, I also added a layer to this model
            discriminator = Sequential([
                Convolution2D(128, 5, 5, subsample=(2, 2), input_shape=(224, 224, 1), border_mode='same',
                              activation=LeakyReLU(0.2)),
                Dropout(0.3),
                Convolution2D(256, 5, 5, subsample=(2, 2), border_mode='same', activation=LeakyReLU(0.2)),
                Dropout(0.3),
                Convolution2D(512, 5, 5, subsample=(2, 2), border_mode='same', activation=LeakyReLU(0.2)),
                Dropout(0.3),
                Flatten(),
                Dense(1, activation='sigmoid')
            ])
            generator.compile(loss='binary_crossentropy', optimizer=Adam())
            discriminator.compile(loss='binary_crossentropy', optimizer=Adam())
            discriminator.trainable = False
            ganInput = Input(shape=(200,))
            # getting the output of the generator
            # and then feeding it to the discriminator
            # new model = D(G(input))
            x = generator(ganInput)
            ganOutput = discriminator(x)
            gan = Model(input=ganInput, output=ganOutput)
            gan.compile(loss='binary_crossentropy', optimizer=Adam())
            gan.summary()

            def train(epoch=10, batch_size=128):
                batch_count = X_train.shape[0] // batch_size

                for i in range(epoch):
                    for j in tqdm(range(batch_count)):
                        # Input for the generator
                        noise_input = np.random.rand(batch_size, 100)

                        # getting random images from X_train of size=batch_size
                        # these are the real images that will be fed to the discriminator
                        image_batch = X_train[np.random.randint(0, X_train.shape[0], size=batch_size)]

                        # these are the predicted images from the generator
                        predictions = generator.predict(noise_input, batch_size=batch_size)

                        # the discriminator takes in the real images and the generated images
                        X = np.concatenate([predictions, image_batch])

                        # labels for the discriminator
                        y_discriminator = [0] * batch_size + [1] * batch_size

                        # Let's train the discriminator
                        discriminator.trainable = True
                        discriminator.train_on_batch(X, y_discriminator)

                        # Let's train the generator
                        noise_input = np.random.rand(batch_size, 100)
                        y_generator = [1] * batch_size
                        discriminator.trainable = False
                        gan.train_on_batch(noise_input, y_generator)
                        generator.save_weights('gen_30_scaled_images.h5')
                        discriminator.save_weights('dis_30_scaled_images.h5')


"""KT1 (K14)."""
from math import ceil

def capitalize_string(s: str) -> str:
    """
    Return capitalized string. The first char is capitalized, the rest remain as they are.

    capitalize_string("abc") => "Abc"
    capitalize_string("ABc") => "ABc"
    capitalize_string("") => ""
    """
    if len(s) < 1:
        return s
    elif len(s) < 2:
        return s.capitalize()
    return s[0].capitalize() + s[1:]

def sum_half_evens(nums: list) -> int:
    """
    Return the sum of first half of even ints in the given array.

    If there are odd number of even numbers, then include the middle number.

    sum_half_evens([2, 1, 2, 3, 4]) => 4
    sum_half_evens([2, 2, 0, 4]) => 4
    sum_half_evens([1, 3, 5, 8]) => 8
    sum_half_evens([2, 3, 5, 7, 8, 9, 10, 11]) => 10
    """
    nl = [i for i in nums if i % 2 == 0]
    nr = 0
    for i in range(ceil(len(nl) / 2)):
        nr += nl[i]
    return nr

def max_block(s: str) -> int:
    """
    Given a string, return the length of the largest "block" in the string.

    A block is a run of adjacent chars that are the same.

    max_block("hoopla") => 2
    max_block("abbCCCddBBBxx") => 3
    max_block("") => 0
    """
    nr = 0
    letter = ''
    for i in range(len(s)):
        if letter == '' or letter[0] != s[i]:
            if len(letter) > nr:
                nr = len(letter)
            letter = s[i]
        elif letter[0] == s[i]:
            letter += s[i]
    if len(letter) > nr:
        nr = len(letter)
    return nr


class Tweet(object):

    def __init__(self, text):
        self.like = 0
        self.text = text
    def aumentar_like(self):
        self.like += 1

LIMIT = 140
list_tweets = [Tweet('@Cosita Me encanta.'), Tweet('@Ayquemequedo Me voy.')]
play = True
error = False
buscar = False
buscar_palabra =''
mensaje_error = ''

while play:
    print('-------------------------')
    print('---------TWITTER---------')
    print('-------------------------')
    for pos, tweet in enumerate(list_tweets):
        if (buscar and tweet.text.find(buscar_palabra) >= 0) or not buscar:
            print(f'\u2605{tweet.like}\n'
                f'{pos}. 'f'{tweet.text}''\n'
                '-------------------------')
    print('1. Nuevo')
    print('2. Like')
    print('3. Buscar')
    print('4. Salir')
    if error:
        print('')
        print(mensaje_error)
        print('')
        error = False
    opcion = input('Introduce opción: ')

    if opcion == '1':
        texto = input('Introduce Tweet: ')
        if len(texto) <= LIMIT:
            list_tweets.append(Tweet(texto))
        else:
            mensaje_error = f'No Puedes añadir mas de {LIMIT} caracteres'
            error = True

    elif opcion == '2':
        pos_like = int(input('Posicion: '))
        list_tweets[pos_like].aumentar_like()

    elif opcion == '3':
        buscar_search = input('Buscar: ')
        buscar = True

    elif opcion == '4':
        play = False


import random
rand=random.randint(1,100)

guess=int(input("Enter your guess  : "))

while guess != rand:
    if guess>rand and guess!=0:
        guess=int(input("Wrong answer. Answer is lower than you think. Try again! (Type 0 to give up) : "))
    elif guess<rand and guess!=0:
        guess=int(input("Wrong answer. Answer is higher than you think. Try again! (Type 0 to give up) : "))
    elif guess == rand:
        print("Correct answer. You win.")
    elif guess == 0:
        print("The correct answer is : ",rand)
        break
    else:
        print("Error! Game crashed!")


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

import urllib
import pandas
import csv
import ast
import datetime
import requests
import sched
import time
import json
import sys
from bs4 import BeautifulSoup

titles = ['庫存', '股票代碼', '股票名稱', '產業類別', \
        '高價(2.5%)', '合理價(5%)', '低價(10%)', \
        '殖利率(%)', '當盤成交價', \
        '現金股利', '股票股利', '盈餘配股', '公積配股', \
        '成立時間', '上市(櫃)時間', \
        '營業毛利率', '營業利益率', '稅前淨利率', '資產報酬率', '股東權益報酬率', '每股淨值', \
        '前4季盈餘', '前3季盈餘', '前2季盈餘', '前1季盈餘', \
        '前4年盈餘', '前3年盈餘', '前2年盈餘', '前1年盈餘', \
        '個股資訊', '持股比例',
        ]

def check_float(var, multiple):
    try:
        return round(float(var) * multiple, 1)
    except ValueError:
        return 0;

def price(id):
    url = 'https://tw.stock.yahoo.com/q/q?s='+id
    try:
        page = urllib.request.urlopen(url).read()
        soup = BeautifulSoup(page.decode('cp950','ignore').encode('utf-8'), features="lxml")
        table = soup.find_all(text='成交')[0].parent.parent.parent
        stock_name = table.select('tr')[1].select('td')[0].text
        price = table.select('tr')[1].select('td')[2].text
        #print(stock_name.strip('加到投資組合'))
        #print('成交價 :',price)
        return price
    except:
        print('股票代碼錯誤或查無此代碼!!')
        return 0

def percentage(stock_price, cash_share):
    return round(float(cash_share) * 100.0/float(stock_price), 2)

def profile(id, had):
    url = 'https://tw.stock.yahoo.com/d/s/company_'+id+'.html'
    resp = urllib.request.urlopen(url)
    html = resp.read()
    soup = BeautifulSoup(html.decode('cp950','ignore').encode('utf-8'), features="lxml")
    try:
        name = soup.title.string.split('-')[0]
        table1 = soup.findAll(text='基 本 資 料')[0].parent.parent.parent
        table2 = soup.findAll(text='營業毛利率')[0].parent.parent.parent
        category = table1.select('tr')[1].select('td')[1].text.strip()
        cashshare = table1.select('tr')[1].select('td')[3].text.strip("元")
        stockshare = table1.select('tr')[2].select('td')[3].text.strip("元")
        earnshare = table1.select('tr')[3].select('td')[3].text.strip("元")
        remainshare = table1.select('tr')[4].select('td')[3].text.strip("元")
        setupDate = table1.select('tr')[2].select('td')[1].text.strip()
        onboardDate = table1.select('tr')[3].select('td')[1].text.strip()
        grossprofit = table2.select('tr')[1].select('td')[1].text.strip()
        netprofit = table2.select('tr')[2].select('td')[1].text.strip()
        taxprofit = table2.select('tr')[3].select('td')[1].text.strip()
        rate = table2.select('tr')[4].select('td')[1].text.strip()
        bq4 = table2.select('tr')[1].select('td')[3].text.strip().strip("元")
        bq3 = table2.select('tr')[2].select('td')[3].text.strip().strip("元")
        bq2 = table2.select('tr')[3].select('td')[3].text.strip().strip("元")
        bq1 = table2.select('tr')[4].select('td')[3].text.strip().strip("元")
        earn = table2.select('tr')[5].select('td')[1].text.strip()
        netvalue = table2.select('tr')[5].select('td')[2].text.strip("每股淨值:").strip().strip("元")
        by4 = table2.select('tr')[1].select('td')[5].text.strip().strip("元")
        by3 = table2.select('tr')[2].select('td')[5].text.strip().strip("元")
        by2 = table2.select('tr')[3].select('td')[5].text.strip().strip("元")
        by1 = table2.select('tr')[4].select('td')[5].text.strip().strip("元")
        current_price = price(id)
        result = list([had, id, name, category , \
                check_float(cashshare, 40), check_float(cashshare, 20), check_float(cashshare, 10), \
                percentage(current_price, cashshare), current_price, \
                cashshare, stockshare, earnshare, remainshare, \
                setupDate, onboardDate, \
                grossprofit, netprofit, taxprofit, rate, earn, netvalue, \
                bq4, bq3, bq2, bq1, \
                by4, by3, by2, by1, \
                'https://tw.stock.yahoo.com/d/s/company_'+id+'.html', \
                'https://fubon-ebrokerdj.fbs.com.tw/z/zc/zck/zck_'+id+'.djhtm', \
                ])
    except:
        result = [id, name, 'access fail']
    #print('result=',result)
    return result

def parse_and_save(candidate, name):
    candidate_list = [titles]
    for row in candidate:
        result = profile(str(row[0]), row[1])
        print(result)
        candidate_list.append(result)

    #save result
    f = open(name+".csv","w+")
    w = csv.writer(f, lineterminator='\n')
    w.writerows(candidate_list)
    f.close()


import os
from bs4 import BeautifulSoup
import re
import csv

# HTML content (replace with your actual HTML content)

def clean_and_extract_dialogues(file_path):
    with open(file_path, "r", encoding="utf-8") as file:

        # Pass the filehandle to Beautiful Soup for parsing
        soup = BeautifulSoup(file, 'html.parser')
        character_dialogue_pairs = []
        formatted_output = []

    # Find all
 elements with class "npc"
    npc_elements = soup.find_all('div', class_='npc')

    # Iterate through the
 elements
    for div_element in npc_elements:
        # Extract the character name from the
 element
        character_name = div_element.get_text().strip()
        character_name = re.sub(r'\s+', ' ', character_name)

        # Find the  element with class "dialog" immediately following the

        next_span = div_element.find_next('span', class_='dialog')

        if next_span:
            # Extract and format the dialogue line
            dialogue_text = next_span.get_text().strip()
            dialogue_text = re.sub(r'
','',dialogue_text)
            dialogue_text = re.sub(r'\s+', ' ', dialogue_text)
            formatted_line = f"{character_name}: {dialogue_text}"
            if len(character_name) < 30:
                formatted_output.append(formatted_line)

    # Join the formatted lines with line breaks
    formatted_text = '\n'.join(formatted_output)
    return formatted_output

# Specify the root folder where your HTML files are located
root_folder = r"C:\Users\cemea\Documents\BG3_-_parsed_dialogue_1.0-20230915T195130Z-001\BG3-parsedDialogue\Dialogs"

# Create an empty list to store all transcripts
all_transcripts = []

# Recursively traverse the directory tree using os.walk
for root, _, files in os.walk(root_folder):
    for filename in files:
        if filename.endswith('.html'):
            file_path = os.path.join(root, filename)
            transcript = clean_and_extract_dialogues(file_path)
            all_transcripts.extend(transcript)

r"""
# Specify the output file path
output_file_path = 'output_transcript.txt'

# Write all transcripts to the output file
with open(r"C:\Users\cemea\Documents\BG3_-_parsed_dialogue_1.0-20230915T195130Z-001\BG3-parsedDialogue\output_text.txt", 'w', encoding='utf-8') as output_file:
    for transcript in all_transcripts:
   #     output_file.write(r"{transcript}\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvomb  ")
"""

# Specify the output CSV file path
output_csv_file = r'C:\Users\cemea\Documents\BG3_-_parsed_dialogue_1.0-20230915T195130Z-001\BG3-parsedDialogue\output_transcript.csv'

# Write all transcripts to the CSV file
with open(output_csv_file, 'w', newline='', encoding='utf-8') as csvfile:
    csvwriter = csv.writer(csvfile)
    for transcript in all_transcripts:
        character, dialogue = transcript.split(': ', 1)
        csvwriter.writerow([character, dialogue])


import pandas as pd
import numpy as np
from sklearn.externals import joblib

from main import main
from Core.classific import cv, my_test
from setting import VEC_TRAIN_PATH, VEC_TEST_PATH, VEC_LARGE_PATH

def vec():
	data = pd.read_csv(VEC_TRAIN_PATH)
	print("训练集规模：", data.shape)
	print("训练集标签分布：\n", pd.value_counts(data.get("62")))
	test = pd.read_csv(VEC_TEST_PATH)
	print("测试集规模：", test.shape)
	print("测试集标签分布：\n", pd.value_counts(test.get("62")))
	target = "62"
	# 去掉ID和属性列
	x_columns = [x for x in data.columns if x not in [target]]
	t_columns = [x for x in test.columns if x not in [target]]
	data_x = np.array(data[x_columns])
	data_y = np.array(data[target])
	test_x = np.array(test[t_columns])
	test_y = np.array(test[target])
	clf = joblib.load("core/models/rf_62_vec.m")
	print("平衡数据集随机森林模型交叉验证评估结果：")
	cv(data_x, data_y, clf)
	print("-----------------------------------")

	print("平衡数据集随机森林模型测试集评估结果：")
	my_test(test_x, test_y, clf)
	print("-----------------------------------")

	large = pd.read_csv(VEC_LARGE_PATH)
	print("完全不平衡数据集规模：", large.shape)
	print("完全不平衡数据集标签分布：\n", pd.value_counts(large.get("62")))
	# 去掉ID和属性列
	l_columns = [x for x in large.columns if x not in [target]]
	large_x = np.array(large[l_columns])
	large_y = np.array(large[target])
	my_test(large_x, large_y, clf)

def predict():
	with open("./input_test.txt", "rb") as f:
		input_data = [data.decode().strip() for data in f.readlines()]
	input_data.append("")
	data = input_data
	rf = joblib.load("./core/models/rf_62_vec.m")
	vec_proba, desc = main(data, "rf")
	print("特征向量概率向量：", vec_proba)
	if vec_proba[0] < 0.6:
		print("\n ----> 预判为假！！")
	else:
		print("\n ----> 预判为真！！")

if __name__ == '__main__':
	print("开始测试特征向量分类模型性能：")
	vec()
	print("开始测试对输入数据识别：")
	predict()


from functools import partial
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    qApp,
    QGraphicsScene,
    QListWidget,
    QHBoxLayout,
    QWidget,
    QMessageBox,
    QInputDialog,
    QAction,
    QFileDialog,
)
from canvas import MyCanvas
from PyQt5.QtGui import QImage,QPainter,QColor

class MainWindow(QMainWindow):
    """
    主窗口类
    """
    def __init__(self):
        super().__init__()
        self.item_cnt = 0

        # 使用QListWidget来记录已有的图元，并用于选择图元。注：这是图元选择的简单实现方法，更好的实现是在画布中直接用鼠标选择图元
        self.list_widget = QListWidget(self)
        self.list_widget.setMinimumWidth(200)

        # 使用QGraphicsView作为画布
        self.scene = QGraphicsScene(self)
        h,w = 600,600
        self.scene.setSceneRect(0, 0, h, w)
        self.canvas:MyCanvas = MyCanvas(self.scene, self)
        self.canvas.setFixedSize(h, w)
        self.canvas.main_window = self
        self.canvas.list_widget = self.list_widget
        self.modified = False
        self.file_name = ''
        # 设置菜单栏
        menubar = self.menuBar()
        file_menu = menubar.addMenu('文件')
        file_menu.addAction('设置画笔').triggered.connect(self.canvas.setColor)
        file_menu.addAction('重置画布').triggered.connect(self.resetCanvas)
        file_menu.addAction('调整画布大小').triggered.connect(partial(self.resetCanvas,True))
        file_menu.addAction('保存画布').triggered.connect(self.save_slot)
        file_menu.addAction('加载画布').triggered.connect(self.load_slot)
        file_menu.addAction('导出画布').triggered.connect(self.export_slot)
        file_menu.addAction('退出').triggered.connect(self.quit_slot)
        draw_menu = menubar.addMenu('DRAW')
        line_menu = draw_menu.addMenu('Line')
        line_menu.addAction('Naive')
        line_menu.addAction('DDA')
        line_menu.addAction('Bresenham')
        polygon_menu = draw_menu.addMenu('Polygon')
        polygon_menu.addAction('DDA')
        polygon_menu.addAction('Bresenham')
        draw_menu.addAction('Ellipse')
        curve_menu = draw_menu.addMenu('Curve')
        curve_menu.addAction('Bezier')
        curve_menu.addAction('B-spline')
        edit_menu = menubar.addMenu('Edit')
        edit_menu.addAction('translate')
        edit_menu.addAction('rotate')
        edit_menu.addAction('scale')
        clip_menu = edit_menu.addMenu('clip')
        clip_menu.addAction('Cohen-Sutherland')
        clip_menu.addAction('Liang-Barsky')
        history_menu = menubar.addMenu('历史记录')
        self.undo_action = history_menu.addAction('撤销')
        self.undo_action.triggered.connect(self.canvas.op_record.undo)
        self.redo_action = history_menu.addAction('重做')
        self.redo_action.triggered.connect(self.canvas.op_record.redo)
        self.delete_action = history_menu.addAction('删除')
        self.delete_action.triggered.connect(self.canvas.deleteItem)
        self.canvas.actionChanged.connect(self.updateMenu)
        self.updateMenu()
        # 连接信号和file_menu.children()槽函数
        for func_menu in [draw_menu,edit_menu]:
            for menu in func_menu.children():
                if isinstance(menu,QAction):
                    type = menu.text()
                    menu.triggered.connect(partial(self.draw_slot,type,''))
                    continue
                type = menu.title()
                for action in menu.children():
                    algr = action.text()
                    action.triggered.connect(partial(self.draw_slot,type,algr))
        self.list_widget.currentTextChanged.connect(self.canvas.selectionChanged)
        # 设置主窗口的布局
        self.hbox_layout = QHBoxLayout()
        self.hbox_layout.addWidget(self.canvas)
        self.hbox_layout.addWidget(self.list_widget, stretch=1)
        self.central_widget = QWidget()
        self.central_widget.setLayout(self.hbox_layout)
        self.setCentralWidget(self.central_widget)
        self.statusBar().showMessage('freenom')
        self.resize(h, w)
        self.setWindowTitle('CG Demo')
        self.canvas.start("freenom","")

    def get_id(self,inc=False):
        if inc:
            self.item_cnt += 1
        return str(self.item_cnt)

    def resetCanvas(self,resize=False):
        h,w = None,None
        if resize:
            h = QInputDialog.getInt(self, '请输入', '长度', 800, 200, 1500)[0]
            w = QInputDialog.getInt(self, '请输入', '宽度', 800, 200, 900)[0]
            self.scene.setSceneRect(0, 0, h, w)
        self.list_widget.clearSelection()
        self.list_widget.clear()
        self.item_cnt = 0
        self.file_name = ''
        self.canvas.reset(h,w)

    def draw_slot(self,type:str,algr:str):
        type = type.lower()
        if self.canvas.start(type,algr):
            self.statusBar().showMessage(type+'-'+algr)

    def quit_slot(self):
        if self.modified:
            ret = QMessageBox.question(self, '退出 - 是否保存', '是否保存当前草稿？', QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Yes)
            if ret == QMessageBox.Cancel:
                return
            if ret == QMessageBox.Yes:
                self.save_slot()
        qApp.quit()

    def export_slot(self):
        file_name = QFileDialog.getSaveFileName(self,caption="导出画布", filter="bmp;;png;;jpg")
        if file_name[0] != '':
            scene = self.canvas.scene()
            scene.clearSelection()
            image = QImage(scene.sceneRect().size().toSize(), QImage.Format.Format_ARGB32)
            image.fill(QColor(255,255,255))
            painter = QPainter(image)
            scene.render(painter)
            image.save(file_name[0]+'.'+file_name[1])
            painter.end()

    def save_slot(self):
        if self.file_name == '':
            file_name = QFileDialog.getSaveFileName(self,caption="保存画布", filter="canvas")
            if file_name[0] == '':
                return
            self.file_name = file_name[0] + '.' + file_name[1]
        self.canvas.saveToFile(self.file_name)

    def load_slot(self):
        file_name = QFileDialog.getOpenFileName(self,caption="加载画布", filter="画布(*.canvas)")
        if file_name[0] != '':
            self.file_name = file_name[0]
        self.canvas.loadFromFile(self.file_name)

    def closeEvent(self, e) -> None:
        self.quit_slot()
        return super().closeEvent(e)

    def updateMenu(self):
        self.undo_action.setEnabled(self.canvas.op_record.canUndo())
        self.redo_action.setEnabled(self.canvas.op_record.canRedo())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import datetime

import isodate

from django.utils import timezone

from crashstats.supersearch.form_fields import split_on_operator

DEFAULT_RANGE_DAYS = 7

def get_date_boundaries(params):
    """Return the date boundaries in a set of parameters.

    Return a tuple with 2 datetime objects, the first one is the lower bound
    date and the second one is the upper bound date.
    """
    default_date_range = datetime.timedelta(days=DEFAULT_RANGE_DAYS)

    start_date = None
    end_date = None

    if not params.get("date"):
        end_date = timezone.now()
        start_date = end_date - default_date_range
    else:
        for param in params["date"]:
            d = isodate.parse_datetime(split_on_operator(param)[1]).replace(
                tzinfo=timezone.utc
            )

            if param.startswith("<") and (not end_date or end_date < d):
                end_date = d
            if param.startswith(">") and (not start_date or start_date > d):
                start_date = d

        if not end_date:
            end_date = timezone.now()

        if not start_date:
            start_date = end_date - default_date_range

    return (start_date, end_date)


import datetime
import time

from scraper import Scraper
from validator import Validator
from courier import Courier
from environment import Environment

class Bot():
    '''This bot contains a scraper, a validator, and a courier. It is epic.'''
    def __init__(self):
        '''Initializes bot class.'''
        self.scraper = Scraper()
        self.validator = Validator()
        self.courier = Courier()
        self.loopInterval = 300 # In seconds
        self.currentMode = Environment.TEST # This is set in self.ModeSelect() method in the run method. I have it set to Environment.TEST default incase someone removes self.modeSelect().

    def run(self):
        '''This is the method that drives the entire program.'''
        # Ask the user what mode they want to run the bot in.
        self.currentMode = self.modeSelect()

        # Last output before loop
        print(f'Starting bot in: {self.currentMode}')

        # While loop.
        while(1):
            print('-------------')

            # Let the scraper scrape for new cases.
            self.scraper.scrape()

            # Check if there is a new case from that scrape.
            if self.validator.checkForNewCase():
                print('Sherlock has found a new case.')

                # Logic to decide who gets emails when there is a new case.
                if self.currentMode == Environment.TEST:
                    Courier.sendEmailsToAdminOnly('Covid Case sent to admin emails.')

                if self.currentMode == Environment.PROD:
                    # Courier.sendEmailsToEveryoneInMailingList('New covid-19 case confirmed on campus.')
                    pass

            self.cleanUp()
            print('-------------')

            # Sleep for as long as indicated in self.loopInterval.
            time.sleep(self.loopInterval)

    def modeSelect(self):
        '''This is the method that helps the user decide what mode to run the bot in.'''
        # List of available modes.
        modes = ['test', 'prod']

        # Loop until the user has inputted a correct mode.
        while(1):
            input('Hi, I\'m Sherlock!\nIf you didn\'t mean to start me, press control + c to stop.\nPress ENTER for mode selection.\n')
            selected_mode = input('Enter "test" to run in test mode.\nEnter "prod" to run in production mode.\n')
            if selected_mode in modes:
                print(f'You entered "{selected_mode}". If this is not correct, press control + c. Otherwise, press ENTER.')
                input()
                if selected_mode == 'test':
                    return Environment.TEST
                if selected_mode == 'prod':
                    return Environment.PROD

            else: print(f'{selected_mode} is not valid mode.')

    def cleanUp(self):
        '''CleanUp crew for scraper, validator, and courier.'''
        self.scraper.cleanUp()
        self.validator.cleanUp()


import pygame

import time

import sys

pygame.init()
pygame.font.init()

window_width = 720
window_height = 480
window = pygame.display.set_mode([window_width, window_height])

startbg = pygame.image.load('start_screenbg.png').convert()

clock = pygame.time.Clock()

def image_load(filename):
    image = pygame.image.load(filename).convert_alpha()
    image = pygame.transform.scale(image, (20, 30))
    return image

def loop(window, cycles, state):
    if state == 0:
        window.blit(startbg, (0,0))
        flame_sprite.update()
        flame_sprite.draw(window)
        if cycles < 1900:
            state = 0
        else:
            state = 1
    if state == 1:
        if cycles < 2600:
            window.blit(startbg, (0, 0))
            state = 1
        else:
            state = 2
    elif state == 2:
        window.blit(startbg, (0, 0))
        if cycles < 3900:
            text_print(window, 'Hey, good job', 80)
            state = 2
        else:
            state = 3
    elif state == 3:
        window.blit(startbg, (0, 0))
        if cycles < 4900:
            text_print(window, "You didn't blow up", 80)
            state = 3
        else:
            state = 4
    elif state == 4:
        window.blit(startbg, (0, 0))
        if cycles < 5900:
            text_print(window, "Try to drive better next time", 50)
            state = 4
        else:
            state = 5
    cycles += 1
    return state, cycles

def text_print(window, text, size):
    font = pygame.font.SysFont('haettenschweiler', size)
    textsurface = font.render(text, False, (255, 255, 255))
    window.blit(textsurface, ((window_width/2) - (textsurface.get_rect().width/2), (window_height/2) - (textsurface.get_rect().height/2)))

class Flames(pygame.sprite.Sprite):
    """
    #Creates moving flames during intro screen
    """
    def __init__(self):
        # Constructor function
        pygame.sprite.Sprite.__init__(self)

        # Flames animation list
        self.animation = []

         # Index shit
        self.index = 0

        # Loading all flame images
        self.animation.append(image_load('flame1.png'))
        self.animation.append(image_load('flame2.png'))
        self.animation.append(image_load('flame3.png'))
        self.animation.append(image_load('flame4.png'))
        self.animation.append(image_load('flame5.png'))
        self.animation.append(image_load('flame6.png'))
        self.animation.append(image_load('flame7.png'))

        # Rect and pos
        self.image = self.animation[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = (407, 275)

    def update(self):

        self.index += 1
        if self.index >= len(self.animation):
            self.index = 0
        self.image = self.animation[self.index]

class Flames2(pygame.sprite.Sprite):
    """
    #Creates moving flames during intro screen
    """
    def __init__(self):
        # Constructor function
        pygame.sprite.Sprite.__init__(self)

        # Flames animation list
        self.animation = []

         # Index shit
        self.index = 0

        # Loading all flame images
        self.animation.append(image_load('flame1.png'))
        self.animation.append(image_load('flame2.png'))
        self.animation.append(image_load('flame3.png'))
        self.animation.append(image_load('flame4.png'))
        self.animation.append(image_load('flame5.png'))
        self.animation.append(image_load('flame6.png'))
        self.animation.append(image_load('flame7.png'))

        # Rect and pos
        self.image = self.animation[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = (347, 201)

    def update(self):

        self.index += 1
        if self.index >= len(self.animation):
            self.index = 0
        self.image = self.animation[self.index]

# Fire shit
flame_sprite = pygame.sprite.Group()
flames = Flames()
flame_sprite.add(flames)
flames2 = Flames2()
flame_sprite.add(flames2)
flame_sprite.update()

def run(window, bg):
    state = 0
    window.blit(bg, (0, 0))

    cycles = 0

    while state < 5:
        state, cycles = loop(window, cycles, state)
        pygame.display.flip()
    if state == 5:
        pygame.quit()


import json
import os
from collections import Counter
from random import choice

import discord
from discord.ext.commands.core import check
from discord.ext import commands
from dotenv import load_dotenv

#Don't steal my token !!!
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = commands.Bot(command_prefix='r/', case_insensitive=True)

#Open helpme.txt and make a string
helpme_text = ""
with open("helpme.txt") as file:
    for line in file:
        helpme_text += line

#Load each commentsn.json file into a list
#Create a list of comments for each subreddit/all
sr_list = []
subreddit_dict = {}
#subreddit_dict["irl"] = []
subreddit_dict["all"] = []
with open("comments.json") as file:
    file_data = json.load(file)

    for comment in file_data:
        subreddit_dict["all"].append(comment)
        srstr = comment["subreddit"]
        if srstr.lower() in subreddit_dict:
            subreddit_dict[srstr.lower()].append(comment)
        else:
            subreddit_dict[srstr.lower()] = [comment]
        sr_list.append(srstr)

sr_chart = dict(sorted(Counter(sr_list).items(), key=lambda a : a[1], reverse=True))

subreddit_dict["top"] = sorted(subreddit_dict["all"], key=lambda a: a["score"], reverse=True)

commands_list = ["r/random", "r/top", "r/helpme", "r/subreddit", "r/find", "r/topsubreddits"]

#Notify when the bot has connected to Discord
@client.event
async def on_ready():
    await client.change_presence(activity=discord.Game("Reddit ● r/helpme"))
    print(f'{client.user} has connected to Discord!')

#Splits message into smaller bits to send through discord
async def send_split(ctx, string):
    size = len(string)
    lower = 0
    while size > 0:
        await ctx.send(string[lower:lower + min(size, 2000)])

        if size > 2000:
            size -= 2000
            lower += 2000
        else:
            break

#General subreddit output command
async def output_subreddit(ctx, subreddit_name, index_choice=0, show_index=True, update_status=True):
    #await print("r/" + subreddit_name + " was executed")
    subreddit_list = subreddit_dict[subreddit_name]
    subreddit_length = len(subreddit_list)

    if index_choice >= 0 and index_choice <= subreddit_length:
        if index_choice == 0:
            index = choice(range(0, subreddit_length))
        else:
            index = index_choice-1

        await send_split(ctx, subreddit_list[index]["body"])
        if show_index:
            await ctx.send(f"**({str(index + 1)}/{str(len(subreddit_list))})**")
        if update_status:
            await client.change_presence(activity=discord.Game(f"Reddit ● r/{subreddit_name}"))
    else:
        await ctx.send("*Invalid index*")

flag_list = ["--showindex", "-si", "--bold", "-b", "--index", "-i", "--updatestatus", "-us"]
#take in args and determine what they are
async def parse_input(args, *, return_index_change=False, return_bold=False, status_only=False):
    index = 0
    show_index = True
    input_string = ""
    modified_index_args = False
    show_bold = True
    update_status = True

    for num, Input in enumerate(args):
        input = Input.lower()
        try:
            integer = int(input)
        except ValueError:
            pass

        last_arg = args[num-1]
        if input == "false":
            if last_arg == "--showindex" or last_arg == "-si":
                show_index = False
                modified_index_args = True
            elif last_arg == "--bold" or last_arg == "-b":
                show_bold = False
            elif last_arg == "--updatestatus" or last_arg == "-us":
                update_status = False
        elif input == "true":
            if last_arg == "--showindex" or last_arg == "-si":
                show_index = True
                modified_index_args = True
            elif last_arg == "--bold" or last_arg == "-b":
                show_bold = False
            elif last_arg == "--updatestatus" or last_arg == "-us":
                update_status = True
        elif "integer" in locals() and (last_arg == "--index" or last_arg == "-i"):
            index = integer
            modified_index_args = True
        elif input != "--showindex" and input not in flag_list:
            input_string += " " + Input

    input_string = input_string[1:] #Take out first space

    if status_only:
        return update_status
    elif return_bold and return_index_change:
        return (index, show_index, input_string, modified_index_args, show_bold, update_status)
    elif return_bold and not return_index_change:
        return (index, show_index, input_string, show_bold, update_status)
    elif return_index_change and not return_bold:
        return (index, show_index, input_string, modified_index_args, update_status)
    else:
        return (index, show_index, input_string, update_status)
#Gives a random message when "r/random" is typed
@client.command()
async def random(ctx, *args):
    status = await parse_input(args, status_only=True)
    await output_subreddit(ctx, "all", 0, show_index=False, update_status=status)

#Gives nth ranked message in terms of upvotes
@client.command()
async def top(ctx, *args):
    index, show_index, string, index_changed, update_status = await parse_input(args, return_index_change=True)
    if not index_changed:
        index = 1
    await output_subreddit(ctx, "top", index, show_index, update_status)

#Finds messages containing the user input and gives a random one of those when "r/find" is typed
@client.command()
async def find(ctx, *args):#usermessage, index_choice=0):
    index_choice, show_index, usermessage, bold, update_status = await parse_input(args, return_bold=True)

    search_list = []
    for message in subreddit_dict["all"]:
        if usermessage in message["body"]:
            search_list.append(message)

    search_list_len = len(search_list)

    if search_list_len > 0 and index_choice >= 0 and index_choice <= search_list_len:
        if index_choice == 0:
            list_index = choice(range(0, search_list_len))
        elif index_choice <= search_list_len and index_choice >= 1:
            list_index = index_choice - 1

        mensaje = search_list[list_index]["body"]

        string_index = mensaje.find(usermessage)
        last = string_index + len(usermessage) + 2
        if bold:
            mensaje = mensaje[:string_index] + "**" + mensaje[string_index:]
            mensaje = mensaje[:last] + "**" + mensaje[last:]

        await send_split(ctx, mensaje)
        if show_index:
            await ctx.send(f"**({str(list_index + 1)}/{str(search_list_len)})**")
    elif search_list_len == 0:
        await ctx.send("*Term not found*")
    else:
        await ctx.send("*Invalid index*")

    if update_status:
        await client.change_presence(activity=discord.Game("Reddit ● r/find"))

#Gives a random message from a particular subreddit
@client.command()
async def subreddit(ctx, *args):
    index, show_index, sr_string, update_status = await parse_input(args)

    #Only the first word should be considered as a potential subreddit
    #Otherwise a user accidentally putting something after the command (i.e. an index number w/o flag)
    #will still get the expected output
    sr_string = sr_string.split()[0].lower()

    if sr_string in subreddit_dict:
        await output_subreddit(ctx, sr_string, index, show_index, update_status)
    else:
        await ctx.send("*Subreddit not found*")
        if update_status:
            await client.change_presence(activity=discord.Game("Reddit ● r/subreddit"))

"""
#Add/look at quotes from user
@client.command()
async def IRL(ctx, *args):
    if not args:
        await output_subreddit(ctx, subreddit_dict["irl"], "IRL", 0)
    elif str(args[0]).lower() == "add":
        msg_str = ""
        for i in range(1, len(args)):
            msg_str += args[i] + " "

        msg_str = msg_str[:-1]

        msg_dict = {
            "body": msg_str,
            "score": 0,
            "subreddit": "IRL"
        }
        subreddit_dict["irl"].append(msg_dict)
        await ctx.send(f"*{msg_str} added*")
    elif int(args[0]):
        await output_subreddit(ctx, subreddit_dict["irl"], "IRL", int(args[0]))
    else:
        await ctx.send("Invalid input!")

    await client.change_presence(activity=discord.Game("Reddit ● r/IRL"))
"""

#Gives the list of subreddits by message counts
@client.command()
async def topsubreddits(ctx, *args):
    dont, care, string, status = await parse_input(args)

    try:
        numshown = int(string)
    except ValueError:
        numshown = 10

    output = ""
    for number, key in enumerate(sr_chart):
        output += f"{number+1}. {str(key)}: {str(sr_chart[key])} \n"

        if number + 1 == numshown:
            break

    await ctx.send(output)
    if status:
        await client.change_presence(activity=discord.Game("Reddit ● r/topsubreddits"))

#Gives help message when user requests it
@client.command()
async def helpme(ctx, *args):
    await ctx.send(helpme_text)
    status = await parse_input(args, status_only=True)
    if status:
        await client.change_presence(activity=discord.Game("Reddit ● r/helpme"))

@client.event
async def on_message(message):
    content = message.content
    first_word = content.split()[0]

    if first_word.lower() not in commands_list and first_word[0:2] == "r/":
        subreddit_name = content[2:]
        message.content = f"r/subreddit {subreddit_name}"

    await client.process_commands(message)

client.run(TOKEN)


#coding=utf-8
'''
Created on Feb 8, 2011

@author: vvitvitskiy
'''
from com.hp.ucmdb.discovery.common import CollectorsConstants
from com.hp.ucmdb.discovery.library.clients.agents import AgentConstants
from connection import PlatformSpecification, Config
from java.lang import Exception as JException
import jmx
import entity
from errormessages import ErrorResolverFactory, ErrorMessageConfig,\
    _PROTOCOL_PARAM, STR_ARG
import errorcodes
import errormessages
import connection
import protocol

class EnhancedFramework:
    r''''Used to monitor actions of Framework object
    For instance, track statistics how many CIs were sent to the uCMDB using
    methods sendObject and sendObjects.
    '''
    def __init__(self, framework):
        self.__framework = framework
        self.__sentObjectStatistic = {}

    def __getattr__(self, name):
        return getattr(self.__framework, name)

    def __count(self, osh):
        cit = osh.getObjectClass()
        count = self.__sentObjectStatistic.get(cit) or 0
        self.__sentObjectStatistic[cit] = count + 1

    def sendObject(self, osh):
        self.__count(osh)
        self.__framework.sendObject(osh)

    def sendObjects(self, vector):
        it = vector.iterator()
        while it.hasNext():
            self.__count(it.next())
        self.__framework.sendObjects(vector)

    def getParameter(self, name, defaultValue=None):
        r'@types: str, any -> str or any'
        return self.__framework.getParameter(name) or defaultValue

    def getSentObjectsCount(self):
        return len(self.__sentObjectStatistic)

    def getSentObjectsCountByCit(self):
        return self.__sentObjectStatistic

def getAvailableProtocols(framework, protocolName, ip):
    '@types: Framework, str, str -> list(ProtocolObject)'
#    return protocol.MANAGER_INSTANCE.getProtocols(protocolName, ip, None)
    ids = framework.getAvailableProtocols(ip, protocolName)
    return map(protocol.MANAGER_INSTANCE.getProtocolById, ids)

def getDestinationListAttribute(framework, attributeName, predicate=None):
    '@types: Framework, str, callable(str -> bool) -> list(str)'
    values = framework.getTriggerCIDataAsList(attributeName) or []
    values = values != 'NA' and values or []
    predicate = predicate or (lambda v: v is not None)
    return filter(predicate, values)

def getParameterAsList(framework, parameterName, separator=','):
    value = framework.getParameter(parameterName)
    items = (value and value.strip() and value.split(separator)) or []
    return map(lambda s: str(s).strip(), items)

def getDestinationPorts(framework):
    '@types: Framework -> list(int)'
    isDigit = lambda p: str(p).isdigit()
    ports = getDestinationListAttribute(framework, 'ports', isDigit)
    return map(int, ports)

def getIpAndDomain(framework):
    '''@types: Framework -> str, str
    @raise ValueError: Ip address is not valid
    '''
    return (framework.getDestinationAttribute('ip_address'),
            framework.getDestinationAttribute('ip_domain') or 'DefaultDomain')

class PlatformSpec(PlatformSpecification):
    def __init__(self, platformType):
        '@types: jee.PlatformType)'
        PlatformSpecification.__init__(self, platformType)

    def getAvailableVersions(self, config):
        '@types: connection.Cofnig -> list(str)'
        return jmx.getAvailableVersions(self.platform.getName())

    def _buildVersion(self, version):
        ''' It's a template method to build correct version representation for
        JEE platform.
        @types: str -> str'''
        return version

class SpecificationByPorts(PlatformSpec):
    def __init__(self, platformType, ports):
        '@types: PlatformSpec, list(int)'
        PlatformSpec.__init__(self, platformType)
        self.__ports = ports or []

    def _sortVersions(self, versions):
        r'@types: list -> list'
        listOfMajorMinorPairs = map(lambda v: v.split('.'), versions)
        listOfMajorMinorPairs.sort(lambda a, b: int(b[0]) - int(a[0]))
        return map('.'.join, listOfMajorMinorPairs)

    def _getConnectionConfigs(self, protocolObj):
        '@types: ProtocolObj -> bool'
        portAttribute = CollectorsConstants.PROTOCOL_ATTRIBUTE_PORT
        protocolPort = entity.WeakNumeric(int)
        protocolPort.set(protocol.MANAGER_INSTANCE.getProtocolProperty(protocolObj, portAttribute))
        protocolPort = protocolPort.value()
        configurations = []

        ports = {}
        # collect unique ports
        # including protocol port
        if protocolPort:
            ports[protocolPort] = 1
        else:
            for port in self.__ports:
                ports[port] = 1
        # build configurations that will cover
        # different combinations for versions and ports
        versions = self._sortVersions(self.getAvailableVersions(None))
        for port in ports.keys():
            for version in versions:
                config = Config(protocolObj, port)
                config.addProperty(AgentConstants.PORT_PROPERTY, port)
                config.addProperty(AgentConstants.VERSION_PROPERTY, version)
                configurations.append(config)
        return configurations

class SucceededPortsConfigManager:
    SUCCESS = 1
    FAILED = 0

    def __init__(self):
        self.__discoveryStatusByConfig = {}

    def __process(self, config, status):
        port = config.getProperty(AgentConstants.PORT_PROPERTY)
        if port:
            self.__discoveryStatusByConfig[port] = status

    def processAsSuccessful(self, config):
        self.__process(config, self.SUCCESS)

    def processAsFailed(self, config):
        self.__process(config, self.FAILED)

    def isSimilarConfigSucceeded(self, config):
        r'@types: connection.Config -> bool'
        configPort = config.getProperty(AgentConstants.PORT_PROPERTY)
        if configPort:
            for port, status in self.__discoveryStatusByConfig.items():
                if (status == self.SUCCESS and port == configPort):
                    return 1
        return 0

class Factory(connection.Factory):
    pass

def reportError(Framework, msg, j2eetype, reportToFramework=1):
    '@types: Framework, str, str, bool -> str'
    if isinstance(msg, JException):
        msg = msg.getMessage()
    resolver = ErrorResolverFactory().getResolver()
    resolver['Could not create SOAP Connector'] = ErrorMessageConfig(STR_ARG(_PROTOCOL_PARAM) + ": Failed to connect to server. If server uses HTTPS please ensure that proper truststore/keystore files provided in credentials", errorcodes.CONNECTION_FAILED)
    resolver['Failed to retrieve stub from server'] = ErrorMessageConfig(STR_ARG(_PROTOCOL_PARAM) + ": Failed to connect to server. Problems to connect to RMI registry.", errorcodes.CONNECTION_FAILED)
    resolver['NO_PERMISSION'] = ErrorMessageConfig(STR_ARG(_PROTOCOL_PARAM) + ": Failed to connect with given user/password.Please check credentials.", errorcodes.CONNECTION_FAILED)
    resolver['server license allows'] = ErrorMessageConfig(STR_ARG(_PROTOCOL_PARAM) + ": Server allows limited number of connection, check your license", errorcodes.CONNECTION_FAILED)

    reporter = errormessages.Reporter(resolver)
    reporter.resolve(msg, j2eetype)
    if reportToFramework:
        reporter.reportToFramework(Framework)
    return reporter.msg


# -*- coding: utf-8 -*-
# @Time    : 2018/11/15 20:29
# @Author  : MengnanChen
# @FileName: feeder_mlp.py
# @Software: PyCharm

import linecache

import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import pandas as pd

from models import hparams

class CachedLineList:
    def __init__(self, fname):
        self._fname = fname

    def __getitem__(self, x):
        if type(x) is slice:
            return [linecache.getline(self._fname, n + 1)
                    for n in range(x.start, x.stop, x.step)]
        else:
            return linecache.getline(self._fname, x + 1)

    def __getslice__(self, beg, end):
        return self[beg:end:1]

class Feeder(object):
    def __init__(self, data_path, sess):
        super(Feeder, self).__init__()
        self._data_path = data_path

        data = pd.read_csv(self._data_path)
        label_name = 'emolabel'
        data[label_name] = LabelEncoder().fit_transform(data[label_name])
        feature_column_names = [x for x in data.columns if x != 'emolabel']
        test_size = hparams.test_batch_size if hparams.test_batch_size is not None \
            else hparams.test_batch_size_percent
        X_train, X_test, y_train, y_test = train_test_split(data[feature_column_names].values,
                                                            data['emolabel'].values,
                                                            test_size=test_size)

        self.num_batch = len(X_train) // hparams.batch_size
        self.eval_num_batch = len(X_test) // hparams.batch_size

        self._placeholder = (
            tf.placeholder(tf.float32, shape=(None, hparams.n_features), name='input_x'),
            tf.placeholder(tf.int32, shape=(None,), name='input_y')
        )

        with tf.device('/cpu:0'):
            dataset = tf.data.Dataset.from_tensor_slices(self._placeholder)
            dataset = dataset.batch(hparams.batch_size).shuffle(buffer_size=1000).repeat()
            iterator = dataset.make_initializable_iterator()
            sess.run(iterator.initializer, feed_dict=dict(zip(self._placeholder, (X_train, y_train))))
            self.next_element = iterator.get_next()

            eval_dataset = tf.data.Dataset.from_tensor_slices(self._placeholder)
            eval_dataset = eval_dataset.batch(hparams.batch_size).repeat()
            eval_iterator = eval_dataset.make_initializable_iterator()
            sess.run(eval_iterator.initializer, feed_dict=dict(zip(self._placeholder, (X_test, y_test))))
            self.eval_next_element = eval_iterator.get_next()


#!/usr/bin/env python
"""
This module contains a component that sets the individual components
of a twist (represented as a geometry_msgs/TwistStamped message) to
a specified maximum, if they exceed their respective limit.

"""
#-*- encoding: utf-8 -*-
__author__ = 'jsanch'

import rospy
import std_msgs.msg
import geometry_msgs.msg
import mcr_twist_limiter.limiter as limiter

class TwistLimiter(object):
    """
    Sets the individual components of a twist to a specified
    maximum, if they exceed their respective limit.

    """
    def __init__(self):
        # params
        self.event = None
        self.twist = None

        # Maximum linear velocities in the axes X, Y and Z (in meters/second)
        self.max_velocity_x = rospy.get_param('~max_velocity_x', 0.1)
        self.max_velocity_y = rospy.get_param('~max_velocity_y', 0.1)
        self.max_velocity_z = rospy.get_param('~max_velocity_z', 0.1)

        # Maximum angular velocities around the axes X, Y and Z (in radians/second)
        self.max_velocity_roll = rospy.get_param('~max_velocity_roll', 0.1)
        self.max_velocity_pitch = rospy.get_param('~max_velocity_pitch', 0.1)
        self.max_velocity_yaw = rospy.get_param('~max_velocity_yaw', 0.1)

        # node cycle rate (in hz)
        self.loop_rate = rospy.Rate(rospy.get_param('~loop_rate', 10))

        # publishers
        self.limited_twist = rospy.Publisher(
            '~limited_twist', geometry_msgs.msg.TwistStamped, queue_size=5
        )
        self.event_out = rospy.Publisher('~event_out', std_msgs.msg.String, queue_size=5)

        # subscribers
        rospy.Subscriber('~event_in', std_msgs.msg.String, self.event_in_cb)
        rospy.Subscriber('~twist', geometry_msgs.msg.TwistStamped, self.twist_cb)

    def start(self):
        """
        Starts the component.

        """
        rospy.loginfo("Ready to start...")
        state = 'INIT'

        while not rospy.is_shutdown():

            if state == 'INIT':
                state = self.init_state()
            elif state == 'IDLE':
                state = self.idle_state()
            elif state == 'RUNNING':
                state = self.running_state()

            rospy.logdebug("State: {0}".format(state))
            self.loop_rate.sleep()

    def event_in_cb(self, msg):
        """
        Obtains an event for the component.

        """
        self.event = msg.data

    def twist_cb(self, msg):
        """
        Obtains the twist.

        """
        self.twist = msg

    def init_state(self):
        """
        Executes the INIT state of the state machine.

        :return: The updated state.
        :rtype: str

        """
        if self.event == 'e_start':
            return 'IDLE'
        else:
            return 'INIT'

    def idle_state(self):
        """
        Executes the IDLE state of the state machine.

        :return: The updated state.
        :rtype: str

        """
        if self.event == 'e_stop':
            self.event = None
            self.twist = None
            return 'INIT'
        elif self.twist:
            return 'RUNNING'
        else:
            return 'IDLE'

    def running_state(self):
        """
        Executes the RUNNING state of the state machine.

        :return: The updated state.
        :rtype: str

        """
        if self.event == 'e_stop':
            self.event = None
            self.twist = None
            self.event_out.publish('e_stopped')
            return 'INIT'
        else:
            limited_twist = self.limit_twist()
            if limited_twist:
                self.limited_twist.publish(limited_twist)
                self.event_out.publish('e_success')
            else:
                self.event_out.publish('e_failure')

            self.event = None
            self.twist = None
            return 'IDLE'

    def limit_twist(self):
        """
        Limits a twist if it exceeds the specified maximum.

        :return: The Cartesian velocity to reduce the position error.
        :rtype: geometry_msgs.msg.TwistStamped

        """
        limited_twist = geometry_msgs.msg.TwistStamped()
        limited_twist.header.frame_id = self.twist.header.frame_id
        limited_twist.header.stamp = self.twist.header.stamp

        limited_twist.twist.linear.x = limiter.limit_value(
            self.twist.twist.linear.x, self.max_velocity_x
        )
        limited_twist.twist.linear.y = limiter.limit_value(
            self.twist.twist.linear.y, self.max_velocity_y
        )
        limited_twist.twist.linear.z = limiter.limit_value(
            self.twist.twist.linear.z, self.max_velocity_z
        )
        limited_twist.twist.angular.x = limiter.limit_value(
            self.twist.twist.angular.x, self.max_velocity_roll
        )
        limited_twist.twist.angular.y = limiter.limit_value(
            self.twist.twist.angular.y, self.max_velocity_pitch
        )
        limited_twist.twist.angular.z = limiter.limit_value(
            self.twist.twist.angular.z, self.max_velocity_yaw
        )

        return limited_twist

def main():
    rospy.init_node('twist_limiter', anonymous=True)
    twist_limiter = TwistLimiter()
    twist_limiter.start()