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

import glob
import itertools
import tempfile
from collections import Counter

import numpy as np
import tensorflow as tf
from pathlib import Path

from tensorflow.lite.python.op_hint import convert_op_hints_to_stubs
from tensorflow.python.framework import test_util
from tensorflow.python.platform import test
from tensorflow.python.tools import optimize_for_inference_lib

# Number of steps to train model.
TRAIN_STEPS = 100
TEST_SAMPLES = 5

CONFIG = tf.ConfigProto(device_count={"GPU": 0})

UNK_ID = 0
EOS_ID = 2
UNK = "<unk>"
SOS = "<s>"
EOS = '</s>'
DIR = "data/"

max_seq_len = 150
EMBED_SIZE = 64
DICT_SIZE = 10000

class BatchInput:
    """"""

    def __init__(self, data: np.array, batch_size: int, shuffle: bool):
        """Constructor for BatchInput"""
        self.data = data
        self.batch_cursor = 0
        self.batch_size = batch_size

        if shuffle:
            np.random.shuffle(data)

    def next_batch(self):
        self.batch_cursor += self.batch_size
        return self.data[self.batch_cursor - self.batch_size:self.batch_cursor]

class UnidirectionalSequenceLstmTest(test_util.TensorFlowTestCase):

    def setUp(self):
        tf.reset_default_graph()

        self.train_data = load_data_matrix(DIR + "data.csv")

        self.vocab_len = np.max(self.train_data)+1

        # Define constants
        # Unrolled through 100 time steps
        self.time_steps = 100
        # Learning rate for Adam optimizer
        self.learning_rate = 0.001
        # MNIST is meant to be classified in 10 classes(0-9).
        self.n_classes = 3
        # Batch size
        self.batch_size = 64
        # Lstm Units.
        self.num_units = 128

    def buildLstmLayer(self):
        return tf.keras.layers.StackedRNNCells([
            tf.lite.experimental.nn.TFLiteLSTMCell(
                self.num_units, use_peepholes=True, forget_bias=1.0, name="rnn1"),
            tf.lite.experimental.nn.TFLiteLSTMCell(
                self.num_units, num_proj=8, forget_bias=1.0, name="rnn2"),
            tf.lite.experimental.nn.TFLiteLSTMCell(
                self.num_units // 2,
                use_peepholes=True,
                num_proj=8,
                forget_bias=0,
                name="rnn3"),
            tf.lite.experimental.nn.TFLiteLSTMCell(
                self.num_units, forget_bias=1.0, name="rnn4")
        ])

    def buildModel(self, lstm_layer, is_dynamic_rnn):
        enc_embedding = tf.get_variable("emb", [self.vocab_len, EMBED_SIZE], tf.float32)

        # Weights and biases for output softmax layer.
        out_weights = tf.Variable(
            tf.random_normal([self.num_units, self.n_classes]))
        out_bias = tf.Variable(tf.random_normal([self.n_classes]))

        # input text placeholder
        sentences = tf.placeholder(
            tf.int32, [None, self.time_steps], name="INPUT_TEXT")

        lengths2 = tf.placeholder(tf.int32, [None], name="INPUT_LENGTH")

        encoder_emb_inp = tf.nn.embedding_lookup(enc_embedding, sentences)

        # x is shaped [batch_size,time_steps,num_inputs]
        if is_dynamic_rnn:
            lstm_input = tf.transpose(encoder_emb_inp, perm=[1, 0, 2])
            # outputs, _ = tf.lite.experimental.nn.dynamic_rnn(
            #     lstm_layer, lstm_input, sequence_length=lengths, dtype="float32")
            outputs, _ = tf.lite.experimental.nn.dynamic_rnn(
                lstm_layer, lstm_input, dtype="float32")

            batch_size = tf.shape(outputs)[1]
            # lengths = tf.minimum(lengths, tf.tile([self.time_steps],[batch_size]))
            # lengths = tf.minimum(lengths, tf.constant([self.time_steps], dtype=tf.int32))
            # lengths2 = tf.maximum(lengths, tf.constant([200], dtype=tf.int32))
            lengths = tf.minimum(lengths2, self.time_steps)
            # lengths = tf.reduce_min(tf.stack([lengths, tf.tile([self.time_steps], [batch_size])]), axis=0)
            outputs = tf.gather_nd(outputs, tf.transpose(tf.stack([lengths-1, tf.range(batch_size)])))
            # outputs = tf.transpose(outputs, perm=[1, 0, 2])

        # outputs = tf.unstack(outputs, axis=0)
        else:
            lstm_input = tf.unstack(encoder_emb_inp, self.time_steps, 1)
            outputs, _ = tf.nn.static_rnn(lstm_layer, lstm_input,  dtype="float32")
            outputs = outputs[-1]
            # outputs, _ = tf.nn.static_rnn(lstm_layer, lstm_input, sequence_length=lengths,  dtype="float32")

        # outputs = tf.transpose(outputs, [1, 0, 2])
        #
        # # outputs is [batch_size, L, N] where L is the maximal sequence length and N
        # # the number of nodes in the last layer.
        # mask = tf.tile(
        #     tf.expand_dims(tf.sequence_mask(lengths, tf.shape(outputs)[1]), 2),
        #     [1, 1, tf.shape(outputs)[2]])
        # zero_outside = tf.where(mask, outputs, tf.zeros_like(outputs))
        #
        # lstm_out = tf.reduce_sum(zero_outside, axis=1)

    # Compute logits by multiplying outputs[-1] of shape [batch_size,num_units]
        # by the softmax layer's out_weight of shape [num_units,n_classes]
        # plus out_bias
        # TODO: change to last_encoder_state !!
        prediction = tf.matmul(outputs, out_weights) + out_bias
        output_class = tf.nn.softmax(prediction, name="OUTPUT_CLASS")

        return sentences, lengths2, prediction, output_class

    def trainModel(self, sentences, lengths, prediction, output_class, sess):
        batch_input = BatchInput(self.train_data, self.batch_size, True)

        # input label placeholder
        labels = tf.placeholder(tf.int32, [None])

        y = tf.one_hot(labels, 3)

        # Loss function
        loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
        # Optimization
        opt = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate).minimize(loss)

        # Initialize variables
        init = tf.global_variables_initializer()
        sess.run(init)
        for _ in range(TRAIN_STEPS):
            batch = batch_input.next_batch()

            batch_lengths = batch[:, 0]
            batch_sentences = batch[:, 1:self.time_steps + 1]
            batch_labels = batch[:, max_seq_len + 1]

            sess.run(opt, feed_dict={sentences: batch_sentences, lengths: batch_lengths, labels: batch_labels})

    def saveAndRestoreModel(self, lstm_layer, sess, saver, is_dynamic_rnn):
        model_dir = tempfile.mkdtemp()
        saver.save(sess, model_dir)

        # Reset the graph.
        tf.reset_default_graph()
        sentences, lengths, prediction, output_class = self.buildModel(
            lstm_layer, is_dynamic_rnn)

        new_sess = tf.Session(config=CONFIG)
        saver = tf.train.Saver()
        saver.restore(new_sess, model_dir)
        return sentences, lengths, prediction, output_class, new_sess

    def getInferenceResult(self, sentences, lengths, output_class, sess):
        # TODO: change to eval (may be)
        batch = BatchInput(self.train_data, self.batch_size, True).next_batch()

        batch_lengths = batch[0:TEST_SAMPLES, 0]
        batch_sentences = batch[0:TEST_SAMPLES, 1:self.time_steps + 1]

        expected_output = sess.run(output_class, feed_dict={sentences: batch_sentences, lengths: batch_lengths})
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            sess, sess.graph_def, [output_class.op.name])
        return [batch_sentences, batch_lengths], expected_output, frozen_graph

    def tfliteInvoke(self, graph, test_inputs, outputs):
        tf.reset_default_graph()

        # input text placeholder
        sentences = tf.placeholder(
            tf.int32, [TEST_SAMPLES, self.time_steps], name="INPUT_TEXT_LITE")

        lengths = tf.placeholder(tf.int32, [TEST_SAMPLES], name="INPUT_LENGTH_LITE")

        tf.import_graph_def(graph, name="", input_map={"INPUT_TEXT": sentences, "INPUT_LENGTH": lengths})
        with tf.Session() as sess:
            curr = sess.graph_def
            curr = convert_op_hints_to_stubs(graph_def=curr)

        curr = optimize_for_inference_lib.optimize_for_inference(
            curr, ["INPUT_TEXT_LITE", "INPUT_LENGTH_LITE"], ["OUTPUT_CLASS"],
            [tf.int32.as_datatype_enum, tf.int32.as_datatype_enum])

        converter = tf.lite.TFLiteConverter(curr, [sentences, lengths], [outputs])
        tflite = converter.convert()
        interpreter = tf.lite.Interpreter(model_content=tflite)

        try:
            interpreter.allocate_tensors()
        except ValueError:
            assert False

        # first input (sentences)
        input_index = (interpreter.get_input_details()[0]["index"])
        interpreter.set_tensor(input_index, test_inputs[0])

        # second input (lengths)
        input_index = (interpreter.get_input_details()[1]["index"])
        interpreter.set_tensor(input_index, test_inputs[1])

        interpreter.invoke()
        output_index = (interpreter.get_output_details()[0]["index"])
        result = interpreter.get_tensor(output_index)
        # Reset all variables so it will not pollute other inferences.
        interpreter.reset_all_variables()
        return result

    def testStaticRnnMultiRnnCell(self):
        sess = tf.Session(config=CONFIG)

        sentences, lengths, prediction, output_class = self.buildModel(
            self.buildLstmLayer(), is_dynamic_rnn=False)
        self.trainModel(sentences, lengths, prediction, output_class, sess)

        saver = tf.train.Saver()
        sentences, lengths, prediction, output_class, new_sess = self.saveAndRestoreModel(
            self.buildLstmLayer(), sess, saver, is_dynamic_rnn=False)

        test_inputs, expected_output, frozen_graph = self.getInferenceResult(
            sentences, lengths, output_class, new_sess)

        result = self.tfliteInvoke(frozen_graph, test_inputs, output_class)
        self.assertTrue(np.allclose(expected_output, result, rtol=1e-6, atol=1e-2))

    @test_util.enable_control_flow_v2
    def testDynamicRnnMultiRnnCell(self):
        sess = tf.Session(config=CONFIG)

        sentences, lengths, prediction, output_class = self.buildModel(
            self.buildLstmLayer(), is_dynamic_rnn=True)
        self.trainModel(sentences, lengths, prediction, output_class, sess)

        saver = tf.train.Saver()

        sentences, lengths, prediction, output_class, new_sess = self.saveAndRestoreModel(
            self.buildLstmLayer(), sess, saver, is_dynamic_rnn=True)

        test_inputs, expected_output, frozen_graph = self.getInferenceResult(
            sentences, lengths, output_class, new_sess)

        result = self.tfliteInvoke(frozen_graph, test_inputs, output_class)
        print(expected_output, result)
        self.assertTrue(np.allclose(expected_output, result, rtol=1e-6, atol=1e-2))

def load_data_matrix(file_name: str) -> np.array:
    return np.loadtxt(file_name, dtype=np.int32, delimiter=",")

if __name__ == "__main__":
    test.main()


import mlflow
import sys

sys.path.append("~/torch_study/paper/06_BERT/source")
from utils import *
from finetune.ner_bert import *
from finetune.dataset import *
import torch
import dill
import pytorch_lightning as pl
import mlflow.pytorch
from mlflow.tracking import MlflowClient
from pytorch_lightning.callbacks import ModelCheckpoint
from torch.utils.data import Dataset, DataLoader

class NER_BERT_trainer(pl.LightningModule):
    def __init__(self, config):
        super(NER_BERT_trainer, self).__init__()
        self.config = config
        data_config = config["data"]

        dataset = NER_Dataset(data_config["src"], data_config["vocab"])
        valid_dataset = NER_Dataset(data_config["src_valid"], data_config["vocab"])

        self.pad_idx = dataset.tokenizer.token_to_id("[PAD]")
        dataloader = DataLoader(
            dataset,
            data_config["batch_size"],
            collate_fn=lambda batch: pad_collate(batch, self.pad_idx),
        )

        valid_dataloader = DataLoader(
            valid_dataset,
            data_config["batch_size"],
            collate_fn=lambda batch: pad_collate(batch, self.pad_idx),
        )

        print("before special token", dataset.tokenizer.get_vocab_size())
        dataset.tokenizer.add_special_tokens(["[SEP]", "[CLS]", "[MASK]", "[EOD]"])
        self.vocab_size = dataset.tokenizer.get_vocab_size()
        print("after special token", self.vocab_size)

        self.output_dim = dataset.output_dim
        self.ner_bert = NER_BERT(
            self.config, self.vocab_size, self.output_dim, self.pad_idx
        )
        device = config["train"]["device"]
        self.ner_bert.to(device)
        self.ner_bert.train()
        self.ner_bert.zero_grad()
        self.ner_bert.fcn.apply(self.initialize_weights)

        if device == "cuda":
            self.gpus = 1
        else:
            self.gpus = 0

        artifact_path = mlflow.mlflow.get_artifact_uri()
        artifact_path = artifact_path.replace("file://", "")
        print("////////////////arftifact_path", artifact_path)
        checkpoint_callback = ModelCheckpoint(
            dirpath=artifact_path, monitor="valid_macro_f1"
        )

        # Add your callback to the callbacks list
        trainer = pl.Trainer(
            callbacks=[checkpoint_callback],
            max_epochs=self.config["train"]["n_epochs"],
            progress_bar_refresh_rate=10,
            gpus=self.gpus,
        )

        # Auto log all MLflow entities
        mlflow.pytorch.autolog()

        # Train the model
        mlflow.end_run()  # 이전에 돌아가고 있던거 끄기
        with mlflow.start_run() as run:
            for key, value in config.items():
                mlflow.log_param(key, value)
            trainer.fit(self.ner_bert, dataloader, valid_dataloader)

    # https://github.com/GyuminJack/torchstudy/blob/main/06Jun/BERT/src/trainer.py
    def initialize_weights(self, m):
        for name, param in m.named_parameters():
            if ("fc" in name) or ("embedding" in name):
                if "bias" in name:
                    torch.nn.init.zeros_(param.data)
                else:
                    torch.nn.init.normal_(param.data, mean=0.0, std=0.02)
            elif "layer_norm" in name:
                if "bias" in name:
                    torch.nn.init.zeros_(param.data)
                else:
                    torch.nn.init.constant_(param.data, 1.0)

if __name__ == "__main__":
    config_file = "~/torch_study/paper/06_BERT/config_finetune.yaml"
    config = read_yaml(config_file)
    print("train started..")
    trainer = NER_BERT_trainer(config)


import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('../input/train.csv')

df.dropna(axis=0)

df.set_index('id', inplace = True)

df.head()
import re

from nltk.corpus import stopwords

stopWords = set(stopwords.words('english'))

#creating a function to encapsulate preprocessing, to mkae it easy to replicate on  submission data

def processing(df):

    #lowering and removing punctuation

    df['processed'] = df['text'].apply(lambda x: re.sub(r'[^\w\s]','', x.lower()))

    #numerical feature engineering

    #total length of sentence

    df['length'] = df['processed'].apply(lambda x: len(x))

    #get number of words

    df['words'] = df['processed'].apply(lambda x: len(x.split(' ')))

    df['words_not_stopword'] = df['processed'].apply(lambda x: len([t for t in x.split(' ') if t not in stopWords]))

    #get the average word length

    df['avg_word_length'] = df['processed'].apply(lambda x: np.mean([len(t) for t in x.split(' ') if t not in stopWords]) if len([len(t) for t in x.split(' ') if t not in stopWords]) > 0 else 0)

    #get the average word length

    df['commas'] = df['text'].apply(lambda x: x.count(','))

    return(df)

df = processing(df)

df.head()
from sklearn.model_selection import train_test_split

features= [c for c in df.columns.values if c  not in ['id','text','author']]

numeric_features= [c for c in df.columns.values if c  not in ['id','text','author','processed']]

target = 'author'

X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.33, random_state=42)

print('X_train size: {}, X_test size: {}'.format(X_train.shape, X_test.shape))

X_train.head()
from sklearn.base import BaseEstimator, TransformerMixin

class DataFrameSelector(BaseEstimator, TransformerMixin):

    """

     Transformer to select a part of the data frame to perform additional transformations on

     """

    def __init__(self, attribute_names):

        self.attribute_names = attribute_names

    def fit(self, X, y = None):

        return self

    def transform(self, X):

        return X[self.attribute_names]

str_feature = 'processed'

num_features_list = ['length', 'words', 'words_not_stopword', 'avg_word_length', 'commas']
from sklearn.pipeline import Pipeline

from sklearn.feature_extraction.text import TfidfVectorizer

text = Pipeline([

                ('selector', DataFrameSelector(str_feature)),

                ('tfidf', TfidfVectorizer( stop_words='english'))

            ])

text.fit_transform(X_train)
from sklearn.preprocessing import StandardScaler

num_feats = Pipeline([

    ('selector', DataFrameSelector(num_features_list)),

    ('standard', StandardScaler())

])

num_feats.fit_transform(X_train)
from sklearn.pipeline import FeatureUnion

feats = FeatureUnion([

    ('text', text),

    ('num_feats', num_feats)

])

feature_processing = Pipeline([('feats', feats)])

feature_processing.fit_transform(X_train)
from sklearn.ensemble import RandomForestClassifier

pipeline = Pipeline([

    ('features',feats),

    ('classifier', RandomForestClassifier(random_state = 42)),

])

pipeline.fit(X_train, y_train)

preds = pipeline.predict(X_test)

np.mean(preds == y_test)
pipeline.get_params().keys()
from sklearn.model_selection import GridSearchCV

hyperparameters = { 'features__text__tfidf__max_df': [0.9, 0.95],

                    'features__text__tfidf__ngram_range': [(1,1), (1,2)],

                   'classifier__max_depth': [50, 70],

                    'classifier__min_samples_leaf': [1,2]

                  }

clf = GridSearchCV(pipeline, hyperparameters, cv=5)

# Fit and tune model

clf.fit(X_train, y_train)
clf.best_params_
#refitting on entire training data using best settings

clf.refit

preds = clf.predict(X_test)

probs = clf.predict_proba(X_test)

np.mean(preds == y_test)
submission = pd.read_csv('../input/test.csv')

#preprocessing

submission = processing(submission)

predictions = clf.predict_proba(submission)

preds = pd.DataFrame(data=predictions, columns = clf.best_estimator_.named_steps['classifier'].classes_)

#generating a submission file

result = pd.concat([submission[['id']], preds], axis=1)

result.set_index('id', inplace = True)

result.head()


#!/usr/bin/python

import RPi.GPIO as GPIO
import time
from threading import Timer

DEFAULT_KEY_DELAY = 300
DEFAULT_REPEAT_DELAY = 1.0
DEFAULT_REPEAT_RATE = 1.0
DEFAULT_DEBOUNCE_TIME = 10

class KeypadFactory():

    def create_keypad(self, keypad=None, row_pins=None, col_pins=None, key_delay=DEFAULT_KEY_DELAY, repeat=False, repeat_delay=None, repeat_rate=None, gpio_mode=GPIO.BCM):

        if keypad is None:
            keypad = [
                [1,2,3],
                [4,5,6],
                [7,8,9],
                ["*",0,"#"]
            ]

        if row_pins is None:
            row_pins = [4,14,15,17]

        if col_pins is None:
            col_pins = [18,27,22]

        return Keypad(keypad, row_pins, col_pins, key_delay, repeat, repeat_delay, repeat_rate, gpio_mode)

    def create_4_by_3_keypad(self):

        KEYPAD = [
            [1,2,3],
            [4,5,6],
            [7,8,9],
            ["*",0,"#"]
        ]

        ROW_PINS = [4,14,15,17]
        COL_PINS = [18,27,22]

        return self.create_keypad(KEYPAD, ROW_PINS, COL_PINS)

    def create_4_by_4_keypad(self):

        KEYPAD = [
            [1,2,3,"A"],
            [4,5,6,"B"],
            [7,8,9,"C"],
            ["*",0,"#","D"]
        ]

        ROW_PINS = [4,14,15,17]
        COL_PINS = [18,27,22,23]

        return self.create_keypad(KEYPAD, ROW_PINS, COL_PINS)

class Keypad():
    def __init__(self, keypad, row_pins, col_pins, key_delay=DEFAULT_KEY_DELAY, repeat=False, repeat_delay=None, repeat_rate=None,gpio_mode=GPIO.BCM):
        self._handlers = []

        self._keypad = keypad
        self._row_pins = row_pins
        self._col_pins = col_pins
        self._key_delay = key_delay
        self._repeat = repeat
        self._repeat_delay = repeat_delay
        self._repeat_rate = repeat_rate
        self._repeat_timer = None
        if repeat:
            self._repeat_delay = repeat_delay if repeat_delay is not None else DEFAULT_REPEAT_DELAY
            self._repeat_rate = repeat_rate if repeat_rate is not None else DEFAULT_REPEAT_RATE
        else:
            if repeat_delay is not None:
                self._repeat = True
                self._repeat_rate = repeat_rate if repeat_rate is not None else DEFAULT_REPEAT_RATE
            elif repeat_rate is not None:
                self._repeat = True
                self._repeat_delay = repeat_delay if repeat_delay is not None else DEFAULT_REPEAT_DELAY

        self._last_key_press_time = 0
        self._first_repeat = True

        GPIO.setmode(gpio_mode)

        self._setRowsAsInput()
        self._setColumnsAsOutput()

    def registerKeyPressHandler(self, handler):
        self._handlers.append(handler)

    def unregisterKeyPressHandler(self, handler):
        self._handlers.remove(handler)

    def clearKeyPressHandlers(self):
        self._handlers = []

    def _repeatTimer(self):
        self._repeat_timer = None
        self._onKeyPress(None)

    def _onKeyPress(self, channel):
        currTime = self.getTimeInMillis()
        if currTime < self._last_key_press_time + self._key_delay:
            return

        keyPressed = self.getKey()
        if keyPressed is not None:
            for handler in self._handlers:
                handler(keyPressed)
            self._last_key_press_time = currTime
            if self._repeat:
                self._repeat_timer = Timer(self._repeat_delay if self._first_repeat else 1.0/self._repeat_rate, self._repeatTimer)
                self._first_repeat = False
                self._repeat_timer.start()
        else:
            if self._repeat_timer is not None:
                self._repeat_timer.cancel()
            self._repeat_timer = None
            self._first_repeat = True

    def _setRowsAsInput(self):
        # Set all rows as input
        for i in range(len(self._row_pins)):
            GPIO.setup(self._row_pins[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
            GPIO.add_event_detect(self._row_pins[i], GPIO.FALLING, callback=self._onKeyPress, bouncetime=DEFAULT_DEBOUNCE_TIME)

    def _setColumnsAsOutput(self):
        # Set all columns as output low
        for j in range(len(self._col_pins)):
            GPIO.setup(self._col_pins[j], GPIO.OUT)
            GPIO.output(self._col_pins[j], GPIO.LOW)

    def getKey(self):

        keyVal = None

        # Scan rows for pressed key
        rowVal = None
        for i in range(len(self._row_pins)):
            tmpRead = GPIO.input(self._row_pins[i])
            if tmpRead == 0:
                rowVal = i
                break

        # Scan columns for pressed key
        colVal = None
        if rowVal is not None:
            for i in range(len(self._col_pins)):
                GPIO.output(self._col_pins[i], GPIO.HIGH)
                if GPIO.input(self._row_pins[rowVal]) == GPIO.HIGH:
                    GPIO.output(self._col_pins[i], GPIO.LOW)
                    colVal = i
                    break
                GPIO.output(self._col_pins[i], GPIO.LOW)

        # Determine pressed key, if any
        if colVal is not None:
            keyVal = self._keypad[rowVal][colVal]

        return keyVal

    def cleanup(self):
        if self._repeat_timer is not None:
            self._repeat_timer.cancel()
        GPIO.cleanup()

    def getTimeInMillis(self):
        return time.time() * 1000

if __name__ == "__main__":
    from pad4pi import rpi_gpio

    KEYPAD = [
        ["F1", "F2", "#", "*"],
        [1, 2, 3, "Up"],
        [4, 5, 6, "Down"],
        [7, 8, 9, "Esc"],
        ["Left", 0, "Right", "Ent"]
        ]
    ROW_PINS = [4, 17, 18, 27, 22]
    COL_PINS = [9, 10, 24, 23]
    kp = rpi_gpio.KeypadFactory().create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS, repeat=True, repeat_rate=5, key_delay=100)
    def printkey(key):
        print(key)
    kp.registerKeyPressHandler(printkey)
    i = raw_input('')
    kp.cleanup()


import wandb
import time
import numpy as np
NO_REWARD_VALUE = 1000
NO_POSITION_VALUE = 10000

def train(agent, env, config):
    print("Starting training...")
    update_timestep = config.update_multiplier * env.max_ep_length
    debug = False
    time_step = 0
    # training loop
    reward_times = np.ones(config.num_episodes) * NO_REWARD_VALUE
    log_running_reward = []
    steps_per_100_episodes = []
    trajectories = np.ones((config.num_episodes, env.max_ep_length + 1, 2)) * NO_POSITION_VALUE
    t0 = time.time()

    for episode in range(config.num_episodes):

        state = env.reset()
        if episode >= 100 and episode % 100 == 0:
            print(f"episode: {episode}")
            print(f"took {time.time() - t0} s")
            t0 = time.time()
            average_reward = np.mean(log_running_reward)
            average_episode_length = np.mean(steps_per_100_episodes)
            wandb.log({f"epoch n={env.n}": episode, f"average_reward n={env.n}": average_reward, f"average_episode_length n={env.n}": average_episode_length}, step=time_step)
            print(f"n: {env.n}")
            print(f"average reward: {average_reward}")
            print(f"average episode length: {average_episode_length}")
            log_running_reward = []
            steps_per_100_episodes = []

        trajectories[episode][0] = np.array(env.coordinates)

        for t in range(1, env.max_ep_length + 1):

            # select action with policy
            if agent.variable_action_space:
                action_mask = env.get_action_mask()
            else:
                action_mask = None
            action = agent.select_action(state, action_mask, debug)
            state, reward, done = env.step(action)
            trajectories[episode][t] = np.array(env.coordinates)

            # saving reward and is_terminals
            agent.buffer.rewards.append(reward)
            agent.buffer.is_terminals.append(done)

            time_step += 1

            # update weights of agent
            if time_step % update_timestep == 0:
                agent.update()

            # break; if the episode is over
            if done:
                # save time till reward
                if reward > 0:
                    reward_times[episode] = env.t
                else:
                    reward_times[episode] = NO_REWARD_VALUE

                log_running_reward.append(reward)
                steps_per_100_episodes.append(env.t)

                break

    return reward_times, trajectories


number = int(input("Enter a number: "))

table_range = 10

print(f"Multiplication table for {number}:")

for i in range(1, table_range + 1):
    product = number * i
    print(f"{number} x {i} = {product}")

7D] Write a program to print the multiplication table of a given number..py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sep  26 10:52:43 2020

Compute residauls and EWS for anoxia data

@author: Thomas M. Bury
"""

import numpy as np
import pandas as pd

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objs as go

import ewstools

# Import transition data
df = pd.read_csv('data/data_transitions.csv')

# EWS computation parameters

# span = 100 # span for Lowess filtering
rw = 0.5 # half the length of the data
ews = ['var','ac'] # EWS to compute
lag_times = [1] # lag times for autocorrelation computation (lag of 10 to show decreasing AC where tau=T/2)
span = 0.2
bandwidth = 0.09 # BW used in paper = 900yr = 0.09% of pre-transiiton data (10,000kyr)
smooth = 'Gaussian'

#-------------
# Compute EWS for transition data
#--------------

# Record names

# Loop through each record
list_df = []
list_tsid = df['tsid'].unique()
for tsid in list_tsid:

    # Get record specific data up to the transition point
    df_temp = df[(df['tsid']==tsid)]
    df_select = df_temp[
        df_temp['Age [ka BP]']>=df_temp['t_transition_start'].iloc[0]
        ].copy()

    # Make time negative so it increaes up to transition
    df_select['Age [ka BP]'] = -df_select['Age [ka BP]']
    # Reverse order of dataframe so transition occurs at the end of the series
    df_select = df_select[::-1]

    #------------
    # Compute EWS for Mo
    #------------
    series=df_select.set_index('Age [ka BP]')['Mo [ppm]']
    ews_dic = ewstools.core.ews_compute(series,
                              roll_window = rw,
                              smooth = smooth,
                              upto='Full',
                              span = span,
                              ews = ews,
                              lag_times = lag_times,
                              )
    df_ews = ews_dic['EWS metrics']
    df_ews['tsid'] = tsid
    df_ews['Variable label'] = 'Mo'

    # Export residuals for ML
    df_ews[['Residuals']].reset_index().round(6).to_csv('data/resids/resids_anoxia_forced_mo_{}.csv'.format(tsid),index=False)

    # Add to list
    list_df.append(df_ews)

    #------------
    # Compute EWS for U
    #------------
    series=df_select.set_index('Age [ka BP]')['U [ppm]']
    ews_dic = ewstools.core.ews_compute(series,
                              roll_window = rw,
                              smooth = smooth,
                              upto='Full',
                              span = span,
                              ews = ews,
                              lag_times = lag_times,
                              )
    df_ews = ews_dic['EWS metrics']
    df_ews['tsid'] = tsid
    df_ews['Variable label'] = 'U'

    # # Export residuals for ML
    # df_ews[['Residuals']].reset_index().to_csv('data/resids_gaussian/resids_anoxia_gaussian_forced_u_{}.csv'.format(tsid),index=False)

    # Add to list
    list_df.append(df_ews)

    print('EWS computed for tsid {}'.format(tsid))

# Concatenate dataframes
df_ews = pd.concat(list_df)

# Export ews dataframe
df_ews.to_csv('data/ews/df_ews_forced.csv')

# #------------------
# # Plot of smoothing
# #–-----------------

# # var_plot = 'Mo'
# var_plot = 'Mo'

# # Do plots for Mo
# df_ews = df_ews[df_ews['Variable label']==var_plot]

# fig = make_subplots(
#     rows=4,
#     cols=4,
#     subplot_titles=['tsid = {}'.format(tsid) for tsid in list_tsid],
#     vertical_spacing=0.15,
#     horizontal_spacing=0.15,
#     )
# i=0
# for row in [1,2,3,4]:
#     for col in [1,2,3,4]:
#         try:
#             tsid=list_tsid[i]
#             # Data for plot
#             df_plot = df_ews[df_ews['tsid']==tsid].reset_index()
#             fig.add_trace(
#                 go.Scatter(
#                     x=df_plot['Time'],
#                     y=df_plot['State variable'],
#                     marker_color='#1f77b4',
#                     ),
#                 row=row,
#                 col=col
#                 )
#             fig.add_trace(
#                 go.Scatter(
#                     x=df_plot['Time'],
#                     y=df_plot['Smoothing'],
#                     marker_color='#ff7f0e',
#                     ),
#                 row=row,
#                 col=col
#                 )
#             # Axes labels
#             fig.update_xaxes(title_text='Age (kyr BP)', row=row, col=col)
#             fig.update_yaxes(title_text=var_plot+' (ppm)', row=row, col=col)

#         except:
#             pass
#         i+=1

# fig.update_layout(height=800, width=800,
#                   showlegend=False,
#                   )

# # fig.write_html('figures/anoxia_smoothing.html')
# fig.write_image('figures/fig_smoothing_{}.png'.format(var_plot),
#                 scale=2,)

# #-----------------
# # Plot of variance
# #–-----------------

# fig = make_subplots(
#     rows=4,
#     cols=4,
#     subplot_titles=['tsid = {}'.format(tsid) for tsid in list_tsid],
#     vertical_spacing=0.15,
#     horizontal_spacing=0.15,
#     )
# i=0
# for row in [1,2,3,4]:
#     for col in [1,2,3,4]:
#         try:
#             tsid=list_tsid[i]
#             # Data for plot
#             df_plot = df_ews[df_ews['tsid']==tsid].reset_index()
#             fig.add_trace(
#                 go.Scatter(
#                     x=df_plot['Time'],
#                     y=df_plot['Variance'],
#                     marker_color='#1f77b4',
#                     ),
#                 row=row,
#                 col=col
#                 )
#             # Axes labels
#             fig.update_xaxes(title_text='Age (kyr BP)', row=row, col=col)
#             fig.update_yaxes(title_text='Variance', row=row, col=col)

#         except:
#             pass
#         i+=1

# fig.update_layout(height=800, width=800,
#                   showlegend=False,
#                   title='Variance for {}'.format(var_plot),
#                   )

# # fig.write_html('figures/dakos_variance.html')
# fig.write_image('figures/fig_var_{}.png'.format(var_plot),
#                 scale=2,)

# #-----------------
# # Plot of lag-1 AC
# #–-----------------

# fig = make_subplots(
#     rows=4,
#     cols=4,
#     subplot_titles=['tsid = {}'.format(tsid) for tsid in list_tsid],
#     vertical_spacing=0.15,
#     horizontal_spacing=0.15,
#     )
# i=0
# for row in [1,2,3,4]:
#     for col in [1,2,3,4]:
#         try:
#             tsid=list_tsid[i]
#             # Data for plot
#             df_plot = df_ews[df_ews['tsid']==tsid].reset_index()
#             fig.add_trace(
#                 go.Scatter(
#                     x=df_plot['Time'],
#                     y=df_plot['Lag-1 AC'],
#                     marker_color='#1f77b4',
#                     ),
#                 row=row,
#                 col=col
#                 )
#             # Axes labels
#             fig.update_xaxes(title_text='Age (kyr BP)', row=row, col=col)
#             fig.update_yaxes(title_text='Lag-1 AC', row=row, col=col)

#         except:
#             pass
#         i+=1

# fig.update_layout(height=800, width=800,
#                   showlegend=False,
#                   title='Lag-1 AC for {}'.format(var_plot)
#                   )

# # fig.write_html('figures/dakos_ac1.html')
# fig.write_image('figures/fig_ac1_{}.png'.format(var_plot),
#                 scale=2,)


#-*-coding:utf-8 -*-
__author__ = 'Jy2881'

import numpy as np
import matplotlib.pyplot as plt
import multivariateGaussian as mG

def visualizeFit(X,  mu, sigma2):
    X1,X2 = np.meshgrid(np.arange(0,35,0.5),np.arange(0,35,0.5))
    m,n = np.shape(X1)
    a = X1.ravel().reshape(m*n,1)

    Z = mG.multivariateGaussian(np.c_[(X1.ravel().reshape(m*n,1),X2.ravel().reshape(m*n,1))],mu,sigma2)
    Z = Z.reshape(np.shape(X1))

    # plot points in buttom layer
    plt.scatter(X[:,0],X[:,1],marker="x",color='b',linewidths=0.01)
    plt.axis([0,30,0,30])
    plt.xlabel('Latency (ms)')
    plt.ylabel('Throughput (mb/s)')

    # plot countours; Do not plot if there are infinities
    if (np.sum(np.isinf(Z)) == 0):
        C = plt.contour(X1, X2, Z, 10, colors = 'black', linewidth = 0.5)
        # C.contour(X1, X2, Z, 10**np.arange(-20,0,3))
    plt.xlabel('Latency (ms)')
    plt.ylabel('Throughput (mb/s)')
    plt.show()


import numpy as np

from tensorflow.python.eager import def_function
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors_impl
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import gradient_checker
from tensorflow.python.ops import gradients_impl
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test

class ConcatOpTest(test.TestCase):

  @test_util.run_deprecated_v1
  def testHStack(self):
    with self.session():
      p1 = array_ops.placeholder(dtypes.float32, shape=[4, 4])
      p2 = array_ops.placeholder(dtypes.float32, shape=[4, 4])
      c = array_ops.concat([p1, p2], 0)
      params = {
          p1: np.random.rand(4, 4).astype("f"),
          p2: np.random.rand(4, 4).astype("f")
      }
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:4, :], params[p1])
    self.assertAllEqual(result[4:, :], params[p2])

  @test_util.run_deprecated_v1
  def testVStack(self):
    with self.session():
      p1 = array_ops.placeholder(dtypes.float32, shape=[4, 4])
      p2 = array_ops.placeholder(dtypes.float32, shape=[4, 4])
      c = array_ops.concat([p1, p2], 1)
      params = {
          p1: np.random.rand(4, 4).astype("f"),
          p2: np.random.rand(4, 4).astype("f")
      }
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:, :4], params[p1])
    self.assertAllEqual(result[:, 4:], params[p2])

  @test_util.run_deprecated_v1
  def test4DStack(self):
    with self.session():
      p1 = array_ops.placeholder(dtypes.float32, shape=[2, 3, 1, 1])
      p2 = array_ops.placeholder(dtypes.float32, shape=[2, 3, 4, 1])
      c = array_ops.concat([p1, p2], 2)
      params = {
          p1: np.random.rand(2, 3, 1, 1).astype("f"),
          p2: np.random.rand(2, 3, 4, 1).astype("f")
      }
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:, :, :1, :], params[p1])
    self.assertAllEqual(result[:, :, 1:, :], params[p2])

  def testInt32GPU(self):
    with test_util.use_gpu():
      p1 = np.random.rand(2, 3).astype("i")
      p2 = np.random.rand(2, 3).astype("i")
      x1 = constant_op.constant(p1)
      x2 = constant_op.constant(p2)
      c = array_ops.concat([x1, x2], 0)
      result = self.evaluate(c)
    self.assertAllEqual(result[:2, :], p1)
    self.assertAllEqual(result[2:, :], p2)

  def testBfloat16GPU(self):
    with test_util.use_gpu():
      p1 = np.random.rand(2, 3).astype(dtypes.bfloat16.as_numpy_dtype)
      p2 = np.random.rand(2, 3).astype(dtypes.bfloat16.as_numpy_dtype)
      x1 = constant_op.constant(p1)
      x2 = constant_op.constant(p2)
      c = array_ops.concat([x1, x2], 0)
      result = self.evaluate(c)
    self.assertAllEqual(result[:2, :], p1)
    self.assertAllEqual(result[2:, :], p2)

  def testFloat8E5M2GPU(self):
    with test_util.use_gpu():
      p1 = np.random.rand(2, 3).astype(dtypes.float8_e5m2.as_numpy_dtype)
      p2 = np.random.rand(2, 3).astype(dtypes.float8_e5m2.as_numpy_dtype)
      x1 = constant_op.constant(p1)
      x2 = constant_op.constant(p2)
      c = array_ops.concat([x1, x2], 0)
      result = self.evaluate(c)
    self.assertAllEqual(result[:2, :], p1)
    self.assertAllEqual(result[2:, :], p2)

  def testFloat8E4M3FNGPU(self):
    with test_util.use_gpu():
      p1 = np.random.rand(2, 3).astype(dtypes.float8_e4m3fn.as_numpy_dtype)
      p2 = np.random.rand(2, 3).astype(dtypes.float8_e4m3fn.as_numpy_dtype)
      x1 = constant_op.constant(p1)
      x2 = constant_op.constant(p2)
      c = array_ops.concat([x1, x2], 0)
      result = self.evaluate(c)
    self.assertAllEqual(result[:2, :], p1)
    self.assertAllEqual(result[2:, :], p2)

  def testRefType(self):
    with test_util.use_gpu():
      p1 = np.random.rand(4, 4).astype("f")
      p2 = np.random.rand(4, 4).astype("f")
      v1 = variables.Variable(p1)
      v2 = variables.Variable(p2)
      c = array_ops.concat([v1, v2], 0)
      self.evaluate(variables.global_variables_initializer())
      result = self.evaluate(c)

    self.assertEqual(result.shape, c.get_shape())
    self.assertAllEqual(result[:4, :], p1)
    self.assertAllEqual(result[4:, :], p2)

  def _testRandom(self, dtype):
    # Random dims of rank 5
    shape = np.random.randint(1, 5, size=5)
    # Random number of tensors, but always > 1.
    num_tensors = np.random.randint(2, 10)
    # Random dim to concat on
    concat_dim = np.random.randint(5)
    params = {}
    if dtype == dtypes.bfloat16:
      dtype_feed = dtypes.float32
    else:
      dtype_feed = dtype
    with self.session():
      p = []
      for i in np.arange(num_tensors):
        input_shape = shape
        input_shape[concat_dim] = np.random.randint(1, 5)
        placeholder = array_ops.placeholder(dtype_feed, shape=input_shape)
        p.append(placeholder)

        t = dtype_feed.as_numpy_dtype
        params[placeholder] = np.random.rand(*input_shape).astype(t)

      if dtype != dtype_feed:
        concat_inputs = [math_ops.cast(p_i, dtype) for p_i in p]
      else:
        concat_inputs = p
      c = array_ops.concat(concat_inputs, concat_dim)
      if dtype != dtype_feed:
        c = math_ops.cast(c, dtype_feed)
      result = c.eval(feed_dict=params)

    self.assertEqual(result.shape, c.get_shape())
    cur_offset = 0

    for i in np.arange(num_tensors):
      # The index into the result is the ':' along all dimensions
      # except the concat_dim. slice(0, size) is used for ':', and
      # a list of slices is used to index into result.
      ind = [slice(0, params[p[i]].shape[j]) for j in np.arange(5)]
      ind[concat_dim] = slice(cur_offset,
                              cur_offset + params[p[i]].shape[concat_dim])
      cur_offset += params[p[i]].shape[concat_dim]
      if dtype == dtype_feed:
        self.assertAllEqual(result[tuple(ind)], params[p[i]])
      else:
        self.assertAllClose(result[tuple(ind)], params[p[i]], 0.01)

  @test_util.run_deprecated_v1
  def testRandom(self):
    self._testRandom(dtypes.bool)
    self._testRandom(dtypes.float32)
    self._testRandom(dtypes.int16)
    self._testRandom(dtypes.int32)
    self._testRandom(dtypes.int64)
    self._testRandom(dtypes.bfloat16)
    self._testRandom(dtypes.float8_e5m2)
    self._testRandom(dtypes.float8_e4m3fn)
    self._testRandom(dtypes.complex64)
    self._testRandom(dtypes.complex128)

  @test_util.run_deprecated_v1
  def testInvalidConcatDimTypeAndShape(self):
    a = variables.Variable(constant_op.constant(1.0, shape=[1]))
    b = variables.Variable(constant_op.constant(2.0, shape=[1]))
    with self.assertRaises(ValueError):
      array_ops.concat(b, a)
    with self.assertRaises(TypeError):
      array_ops.concat(1, 4.2)
    with self.assertRaises(ValueError):
      array_ops.concat(1, a)
    with self.assertRaises(TypeError):
      array_ops.concat([a, b], a)
    with self.assertRaises(ValueError):
      array_ops.concat([a, b], [3])
    with self.assertRaises(ValueError):
      array_ops.concat([], 0)
    # An integer tensor for shape dim should throw no error.
    array_ops.concat(1, constant_op.constant(0, shape=[]))
    # A non-scalar tensor for shape should throw ValueError.
    with self.assertRaises(ValueError):
      array_ops.concat(1, constant_op.constant(0, shape=[1]))

  def testScalars(self):
    arr = ops.convert_to_tensor([0.2, 0.3])
    outs = []
    for i in range(arr.shape[0]):
      outs.append(arr[i]**2)
    with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
      _ = array_ops.concat(outs, axis=0)

  def _testGradientsSimple(self, dtype):
    # Test both positive and negative concat axis.
    # -2 and 1 correspond to the same axis for 3-dimensional tensors.
    for axis in [-2, 1]:
      with test_util.use_gpu():
        inp = []
        inp_tensors = []
        for x in [1, 2, 6]:
          shape = [10, x, 2]
          t = np.random.rand(*shape).astype(dtype.as_numpy_dtype)
          if dtype.is_complex:
            t += -1j * t
          inp.append(t)
          inp_tensors.append(
              constant_op.constant(
                  t.flatten(),
                  shape=shape,
                  dtype=dtype))
        c = array_ops.concat(inp_tensors, axis)
        output_shape = [10, 9, 2]
        grad_inp = np.random.rand(*output_shape).astype(dtype.as_numpy_dtype)
        if dtype.is_complex:
          grad_inp += -1j * grad_inp
        grad_tensor = constant_op.constant(
            grad_inp.flatten(), shape=output_shape)
        grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
        concated_grad = array_ops.concat(grad, axis)
        result = self.evaluate(concated_grad)
    self.assertAllEqual(result, grad_inp)

  @test_util.run_deprecated_v1
  def testGradientsSimple(self):
    self._testGradientsSimple(dtypes.float32)
    self._testGradientsSimple(dtypes.complex64)

  @test_util.run_deprecated_v1
  def testGradientsFirstDim(self):
    with test_util.use_gpu():
      inp = []
      inp_tensors = []
      for x in [1, 2, 6]:
        shape = [x, 10, 2]
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            constant_op.constant(
                t.flatten(),
                shape=shape,
                dtype=dtypes.float32))
      c = array_ops.concat(inp_tensors, 0)
      output_shape = [9, 10, 2]
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = constant_op.constant(
          grad_inp.flatten(), shape=output_shape)
      grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = array_ops.concat(grad, 0)
      result = self.evaluate(concated_grad)

    self.assertAllEqual(result, grad_inp)

  @test_util.run_deprecated_v1
  def testGradientsLastDim(self):
    # Test both positive and negative concat axis.
    # -1 and 2 correspond to the same axis for 3-dimensional tensors.
    for axis in [-1, 2]:
      with test_util.use_gpu():
        inp = []
        inp_tensors = []
        for x in [1, 2, 6]:
          shape = [10, 2, x]
          t = np.random.rand(*shape).astype("f")
          inp.append(t)
          inp_tensors.append(
              constant_op.constant(
                  t.flatten(),
                  shape=shape,
                  dtype=dtypes.float32))
        c = array_ops.concat(inp_tensors, 2)
        output_shape = [10, 2, 9]
        grad_inp = np.random.rand(*output_shape).astype("f")
        grad_tensor = constant_op.constant(
            grad_inp.flatten(), shape=output_shape)
        grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
        concated_grad = array_ops.concat(grad, axis)
        result = self.evaluate(concated_grad)

    self.assertAllEqual(result, grad_inp)

  def _RunAndVerifyGradientsRandom(self):
    # Random dims of rank 5
    input_shape = np.random.randint(1, 5, size=5)
    # Random number of tensors
    num_tensors = np.random.randint(12, 20)
    # Random dim to concat on
    concat_dim = np.random.randint(5)
    concat_dim_sizes = np.random.randint(1, 5, size=num_tensors)
    with test_util.use_gpu():
      inp = []
      inp_tensors = []
      for x in concat_dim_sizes:
        shape = input_shape
        shape[concat_dim] = x
        t = np.random.rand(*shape).astype("f")
        inp.append(t)
        inp_tensors.append(
            constant_op.constant(t.flatten(), shape=shape,
                                 dtype=dtypes.float32))
      c = array_ops.concat(inp_tensors, concat_dim)
      output_shape = input_shape
      output_shape[concat_dim] = concat_dim_sizes.sum()
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = constant_op.constant(grad_inp.flatten(), shape=output_shape)
      grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = array_ops.concat(grad, concat_dim)
      result = self.evaluate(concated_grad)

    self.assertAllEqual(result, grad_inp)

  @test_util.run_deprecated_v1
  def testGradientsRandom(self):
    for _ in range(5):
      self._RunAndVerifyGradientsRandom()

  @test_util.run_deprecated_v1
  def testGradientWithUnknownInputDim(self):
    with self.session():
      x = array_ops.placeholder(dtypes.float32)
      y = array_ops.placeholder(dtypes.float32)
      c = array_ops.concat([x, y], 2)

      output_shape = [10, 2, 9]
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = constant_op.constant(
          grad_inp.flatten(), shape=output_shape)

      grad = gradients_impl.gradients([c], [x, y], [grad_tensor])
      concated_grad = array_ops.concat(grad, 2)
      params = {
          x: np.random.rand(10, 2, 3).astype("f"),
          y: np.random.rand(10, 2, 6).astype("f")
      }
      result = concated_grad.eval(feed_dict=params)

      self.assertAllEqual(result, grad_inp)

  @test_util.run_deprecated_v1
  def testShapeError(self):
    # Rank doesn't match.
    with self.assertRaises(ValueError):
      array_ops.concat(
          [constant_op.constant(10.0, shape=[4, 4, 4, 4]),
           constant_op.constant(20.0, shape=[4, 4, 4])
          ], 1)

    # Dimensions don't match in a non-concat dim.
    with self.assertRaises(ValueError):
      array_ops.concat(
          [constant_op.constant(10.0, shape=[1, 2, 1]),
           constant_op.constant(20.0, shape=[3, 2, 1])
          ], 1)

    # concat_dim out of range.
    with self.assertRaises(ValueError):
      array_ops.concat(
          [constant_op.constant(10.0, shape=[4, 4, 4]),
           constant_op.constant(20.0, shape=[4, 4, 4])
          ], 3)

    # concat_dim out of range
    with self.assertRaises(ValueError):
      array_ops.concat(
          [constant_op.constant(10.0, shape=[4, 4, 4]),
           constant_op.constant(20.0, shape=[4, 4, 4])
          ], -4)

  @test_util.run_deprecated_v1
  def testShapeWithUnknownConcatDim(self):
    p1 = array_ops.placeholder(dtypes.float32)
    c1 = constant_op.constant(10.0, shape=[4, 4, 4, 4])
    p2 = array_ops.placeholder(dtypes.float32)
    c2 = constant_op.constant(20.0, shape=[4, 4, 4, 4])
    dim = array_ops.placeholder(dtypes.int32)
    concat = array_ops.concat([p1, c1, p2, c2], dim)
    self.assertEqual(4, concat.get_shape().ndims)

    # All dimensions unknown.
    concat2 = array_ops.concat([p1, p2], dim)
    self.assertEqual(None, concat2.get_shape())

    # Rank doesn't match.
    c3 = constant_op.constant(30.0, shape=[4, 4, 4])
    with self.assertRaises(ValueError):
      array_ops.concat([p1, c1, p2, c3], dim)

  @test_util.run_deprecated_v1
  def testZeroSize(self):
    # Verify that concat doesn't crash and burn for zero size inputs
    np.random.seed(7)
    with test_util.use_gpu():
      for shape0 in (), (2,):
        axis = len(shape0)
        for shape1 in (), (3,):
          for n0 in 0, 1, 2:
            for n1 in 0, 1, 2:
              x0 = np.random.randn(*(shape0 + (n0,) + shape1))
              x1 = np.random.randn(*(shape0 + (n1,) + shape1))
              correct = np.concatenate([x0, x1], axis=axis)
              # TODO(irving): Make tf.concat handle map, then drop list().
              xs = list(map(constant_op.constant, [x0, x1]))
              c = array_ops.concat(xs, axis)
              self.assertAllEqual(self.evaluate(c), correct)
              # Check gradients
              dc = np.random.randn(*c.get_shape().as_list())
              dxs = self.evaluate(gradients_impl.gradients(c, xs, dc))
              self.assertAllEqual(dc, np.concatenate(dxs, axis=axis))

  @test_util.run_deprecated_v1
  def testTensorConcatDim0Grad(self):
    x_shapes = [[20, 7, 3], [10, 7, 3], [14, 7, 3]]
    output_shape = [44, 7, 3]
    x_vals = [
        np.random.random_sample(x_shape).astype(np.float64)
        for x_shape in x_shapes
    ]
    with self.cached_session():
      xs = [constant_op.constant(x_val) for x_val in x_vals]
      output = array_ops.concat(xs, 0)
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape)
    self.assertLess(err, 1e-11)

  @test_util.run_deprecated_v1
  def testTensorConcatDim1Grad(self):
    x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]]
    output_shape = [20, 11, 3]
    x_vals = [
        np.random.random_sample(x_shape).astype(np.float64)
        for x_shape in x_shapes
    ]
    with self.cached_session():
      xs = [constant_op.constant(x_val) for x_val in x_vals]
      output = array_ops.concat(xs, 1)
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape)
    self.assertLess(err, 1e-11)

  @test_util.run_deprecated_v1
  def testIndexedSlicesConcatDim0Grad(self):
    x_shapes = [[20, 7, 3], [10, 7, 3], [14, 7, 3]]
    output_shape = [4, 7, 3]
    x_vals = [
        np.random.random_sample(x_shape).astype(np.float64)
        for x_shape in x_shapes
    ]
    with self.cached_session():
      xs = [constant_op.constant(x_val) for x_val in x_vals]
      x_concat = array_ops.concat(xs, 0)
      output = array_ops.gather(x_concat, [1, 2, 0, 5])
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape)
    self.assertLess(err, 1e-11)

  @test_util.run_deprecated_v1
  def testIndexedSlicesConcatDim1Grad(self):
    x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]]
    output_shape = [4, 11, 3]
    x_vals = [
        np.random.random_sample(x_shape).astype(np.float64)
        for x_shape in x_shapes
    ]
    with self.cached_session():
      xs = [constant_op.constant(x_val) for x_val in x_vals]
      x_concat = array_ops.concat(xs, 1)
      output = array_ops.gather(x_concat, [1, 2, 0, 5])
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape)
    self.assertLess(err, 1e-11)

  @test_util.run_deprecated_v1
  def testIndexedSlicesConcatDim2Grad(self):
    x_shapes = [[20, 7, 3], [20, 7, 1], [20, 7, 2]]
    output_shape = [4, 7, 6]
    x_vals = [
        np.random.random_sample(x_shape).astype(np.float64)
        for x_shape in x_shapes
    ]
    with self.cached_session():
      xs = [constant_op.constant(x_val) for x_val in x_vals]
      x_concat = array_ops.concat(xs, 2)
      output = array_ops.gather(x_concat, [1, 2, 0, 5])
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape)
    self.assertLess(err, 1e-11)

  @test_util.run_deprecated_v1
  def testIndexedSlicesConcatDim1Grad_UnknownInputDim(self):
    x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]]
    output_shape = [4, 11, 3]
    with self.cached_session():
      x_1 = array_ops.placeholder(dtypes.float64)
      x_2 = array_ops.placeholder(dtypes.float64)
      x_3 = array_ops.placeholder(dtypes.float64)
      xs = [x_1, x_2, x_3]

      x_concat = array_ops.concat(xs, 1)
      output = array_ops.gather(x_concat, [1, 2, 0, 5])
      params = {
          x_1: np.random.random_sample(x_shapes[0]).astype(np.float64),
          x_2: np.random.random_sample(x_shapes[1]).astype(np.float64),
          x_3: np.random.random_sample(x_shapes[2]).astype(np.float64)
      }
      err = gradient_checker.compute_gradient_error(xs, x_shapes, output,
                                                    output_shape,
                                                    extra_feed_dict=params)
    self.assertLess(err, 1e-11)

  def testConcatTuple(self):
    c1 = np.random.rand(4, 4)
    c2 = np.random.rand(4, 4)
    concat_list_t = array_ops.concat([c1, c2], 0)
    concat_tuple_t = array_ops.concat((c1, c2), 0)
    self.assertAllEqual(
        self.evaluate(concat_list_t), self.evaluate(concat_tuple_t))

  @test_util.run_deprecated_v1
  def testConcatNoScalars(self):
    scalar = constant_op.constant(7)
    dim = array_ops.placeholder(dtypes.int32)
    with self.assertRaisesRegex(
        ValueError, r"Can't concatenate scalars \(use tf\.stack instead\)"):
      array_ops.concat([scalar, scalar, scalar], dim)

  # important as gpu implementation could fail if
  # shared memory is not large for all the inputs
  @test_util.run_deprecated_v1
  def testConcatLargeNumberOfTensors(self):
    with self.session():
      for concat_dim in range(2):
        params = {}
        p = []
        shape = np.array([7, 13])
        if test.is_gpu_available():
          num_tensors = 5000
        else:
          num_tensors = 500
        for i in np.arange(num_tensors):
          input_shape = shape
          placeholder = array_ops.placeholder(dtypes.float32, shape=input_shape)
          p.append(placeholder)

          params[placeholder] = np.random.rand(*input_shape).astype(np.float32)

        concat_inputs = p
        c = array_ops.concat(concat_inputs, concat_dim)
        result = c.eval(feed_dict=params)

        self.assertEqual(result.shape, c.get_shape())
        cur_offset = 0

        for i in np.arange(num_tensors):
          # The index into the result is the ':' along all dimensions
          # except the concat_dim. slice(0, size) is used for ':', and
          # a list of slices is used to index into result.
          index = [slice(0, params[p[i]].shape[j]) for j in np.arange(2)]
          index[concat_dim] = slice(cur_offset,
                                    cur_offset + params[p[i]].shape[concat_dim])
          cur_offset += params[p[i]].shape[concat_dim]
          self.assertAllEqual(result[tuple(index)], params[p[i]])

  def testConcatEmpty(self):
    with test_util.use_gpu():
      t1 = []
      t2 = []
      output = gen_array_ops.concat_v2([t1, t2], 0)
      self.assertFalse(self.evaluate(output))  # Checks that output is empty

  @test_util.run_deprecated_v1
  def testConcatInvalidAxis(self):
    with self.assertRaises(ValueError):
      with test_util.use_gpu():
        t1 = [1]
        t2 = [2]
        gen_array_ops.concat_v2([t1, t2], 1).eval()

  def testConcatInvalidAxisInTfFunction(self):

    @def_function.function
    def concat_wrapper():
      y = gen_array_ops.concat_v2(
          values=[[1, 2, 3], [4, 5, 6]], axis=0xb500005b)
      return y

    with self.assertRaises(ValueError):
      concat_wrapper()

  def testConcatNegativeAxis(self):
    with test_util.use_gpu():
      t1 = [[1, 2, 3], [4, 5, 6]]
      t2 = [[7, 8, 9], [10, 11, 12]]

      c = gen_array_ops.concat_v2([t1, t2], -2)
      self.assertEqual([4, 3], c.get_shape().as_list())
      output = self.evaluate(c)
      self.assertAllEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
                          output)

      c = gen_array_ops.concat_v2([t1, t2], -1)
      self.assertEqual([2, 6], c.get_shape().as_list())
      output = self.evaluate(c)
      self.assertAllEqual([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]], output)

  def _testGradientsForAxis(
      self, inp_tensors, axis, output_shape, feed_dict=None):
    with self.cached_session():
      c = array_ops.concat(inp_tensors, axis)
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = constant_op.constant(
          grad_inp.flatten(), shape=output_shape)
      grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = array_ops.concat(grad, axis)
      result = concated_grad.eval(feed_dict=feed_dict)
      self.assertAllEqual(result, grad_inp)

  def _testIndexedSlicesGradientsForAxis(
      self, inp_tensors, axis, output_shape, gather_indexes, feed_dict=None):
    with self.cached_session():
      c = array_ops.gather(
          array_ops.concat(inp_tensors, axis), gather_indexes)
      grad_inp = np.random.rand(*output_shape).astype("f")
      grad_tensor = constant_op.constant(
          grad_inp.flatten(), shape=output_shape)
      grad = gradients_impl.gradients([c], inp_tensors, [grad_tensor])
      concated_grad = array_ops.gather(
          array_ops.concat(grad, axis), gather_indexes)
      result = concated_grad.eval(feed_dict=feed_dict)
      self.assertAllEqual(result, grad_inp)

  @test_util.run_deprecated_v1
  def testGradientsNegativeAxis(self):
    x1 = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
    x2 = [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]
    inp_tensors = [constant_op.constant(x1, shape=(2, 3), dtype=dtypes.float32),
                   constant_op.constant(x2, shape=(2, 3), dtype=dtypes.float32)]

    # Test concat gradient with axis == -2
    self._testGradientsForAxis(inp_tensors, -2, output_shape=[4, 3])

    # Test concat gradient with unknown-shape tensors.
    x1_placeholder = array_ops.placeholder(dtypes.float32)
    x2_placeholder = array_ops.placeholder(dtypes.float32)
    inp_tensors_placeholders = [x1_placeholder, x2_placeholder]
    feed_dict = {x1_placeholder: x1, x2_placeholder: x2}
    self._testGradientsForAxis(
        inp_tensors_placeholders, -1, output_shape=[2, 6], feed_dict=feed_dict)

    # Test IndexedSlices concat gradient.
    self._testIndexedSlicesGradientsForAxis(
        inp_tensors, -2, output_shape=[2, 3], gather_indexes=[2, 0])

    # We don't support calculating IndexedSlices concat gradient for
    # negative indexes when rank is not known.
    with self.assertRaises(ValueError):
      self._testIndexedSlicesGradientsForAxis(
          inp_tensors_placeholders, -2, output_shape=[2, 3],
          gather_indexes=[2, 0], feed_dict=feed_dict)

  def testConcatDtype(self):
    for dtype in [dtypes.int32, dtypes.int64, dtypes.uint32, dtypes.uint64]:
      with test_util.use_gpu():
        t1 = constant_op.constant([[1, 2, 3], [4, 5, 6]], dtype=dtype)
        t2 = constant_op.constant([[7, 8, 9], [10, 11, 12]], dtype=dtype)

        c = gen_array_ops.concat_v2([t1, t2], 1)
        self.assertEqual([2, 6], c.get_shape().as_list())
        output = self.evaluate(c)
        self.assertAllEqual([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]], output)

  def testConcatAxisType(self):
    for dtype in [dtypes.int32, dtypes.int64]:
      with test_util.use_gpu():
        t1 = [[1, 2, 3], [4, 5, 6]]
        t2 = [[7, 8, 9], [10, 11, 12]]

        c = gen_array_ops.concat_v2([t1, t2],
                                    constant_op.constant(1, dtype=dtype))
        self.assertEqual([2, 6], c.get_shape().as_list())
        output = self.evaluate(c)
        self.assertAllEqual([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]], output)

class ConcatOffsetTest(test.TestCase):

  def testBasic(self):
    with test_util.use_gpu():
      cdim = constant_op.constant(1, dtypes.int32)
      s0 = constant_op.constant([2, 3, 5], dtypes.int32)
      s1 = constant_op.constant([2, 7, 5], dtypes.int32)
      s2 = constant_op.constant([2, 20, 5], dtypes.int32)
      off = gen_array_ops.concat_offset(cdim, [s0, s1, s2])
      ans = self.evaluate(off)
      self.assertAllEqual(ans, [[0, 0, 0], [0, 3, 0], [0, 10, 0]])

  @test_util.run_deprecated_v1
  def testNotVector(self):
    cdim = constant_op.constant(1, dtypes.int32)
    s0 = constant_op.constant([[2, 3, 5]], dtypes.int32)
    s1 = constant_op.constant([[2, 7, 5]], dtypes.int32)
    off = gen_array_ops.concat_offset(cdim, [s0, s1])
    with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                r"should be a vector"):
      self.evaluate(off)

  @test_util.run_deprecated_v1
  def testConcatDimOutOfRange(self):
    cdim = constant_op.constant(4, dtypes.int32)
    s0 = constant_op.constant([2, 3, 5], dtypes.int32)
    s1 = constant_op.constant([2, 7, 5], dtypes.int32)
    off = gen_array_ops.concat_offset(cdim, [s0, s1])
    with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                r"Concat dim is out of range: 4 vs. 3"):
      self.evaluate(off)

  @test_util.run_deprecated_v1
  def testDimMismatch(self):
    cdim = constant_op.constant(1, dtypes.int32)
    s0 = constant_op.constant([2, 3, 5], dtypes.int32)
    s1 = constant_op.constant([2, 7, 5, 10], dtypes.int32)
    off = gen_array_ops.concat_offset(cdim, [s0, s1])
    with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                r"should contain 3 elem"):
      self.evaluate(off)

  @test_util.run_deprecated_v1
  def testSizeMismatch(self):
    cdim = constant_op.constant(1, dtypes.int32)
    s0 = constant_op.constant([2, 3, 5], dtypes.int32)
    s1 = constant_op.constant([2, 7, 10], dtypes.int32)
    off = gen_array_ops.concat_offset(cdim, [s0, s1])
    with self.assertRaisesRegex(
        errors_impl.InvalidArgumentError,
        r"All dimensions except 1 must match. Input 1 has shape \[2 7 10\] "
        r"and doesn't match input 0 with shape \[2 3 5\]."):
      self.evaluate(off)

  def testNegativeDim(self):
    with test_util.use_gpu():
      cdim = constant_op.constant(-2, dtypes.int32)
      s0 = constant_op.constant([2, 3, 5], dtypes.int32)
      s1 = constant_op.constant([2, 7, 5], dtypes.int32)
      s2 = constant_op.constant([2, 20, 5], dtypes.int32)
      off = gen_array_ops.concat_offset(cdim, [s0, s1, s2])
      ans = self.evaluate(off)
      self.assertAllEqual(ans, [[0, 0, 0], [0, 3, 0], [0, 10, 0]])

      cdim = constant_op.constant(-3, dtypes.int32)
      s0 = constant_op.constant([2, 3, 5], dtypes.int32)
      s1 = constant_op.constant([1, 3, 5], dtypes.int32)
      s2 = constant_op.constant([3, 3, 5], dtypes.int32)
      off = gen_array_ops.concat_offset(cdim, [s0, s1, s2])
      ans = self.evaluate(off)
      self.assertAllEqual(ans, [[0, 0, 0], [2, 0, 0], [3, 0, 0]])

  def testCreateMemDecBlockedFormat(self):
    """Try to create the mkl concat operation

    when one of the input's memory descriptor is in blocked format
    """
    if test_util.IsMklEnabled():
      s0 = np.ones((1, 8188, 4092, 1), dtype=np.uint8).astype(np.float32)
      s1 = array_ops.strided_slice(
          s0, [0, 1, 1, 0], [0, -1, -1, 0], [1, 1, 1, 1],
          begin_mask=9,
          end_mask=9)
      s2 = array_ops.slice(s1, [0, 0, 0, 0], [-1, -1, -1, 1])
      s3_1 = array_ops.slice(s2, [0, 4, 4, 0], [-1, 8178, 4082, 1])
      s3_2 = array_ops.slice(s2, [0, 4, 4, 0], [-1, 8178, 4082, 1])
      filter4_1 = constant_op.constant([[[[1.18, -0.51]]]])
      s4_1 = nn_ops.conv2d(
          s3_1, filter4_1, strides=[1, 1, 1, 1], padding="VALID")
      filter4_2 = constant_op.constant([[[[1.38, -0.11]]]])
      s4_2 = nn_ops.conv2d(
          s3_2, filter4_2, strides=[1, 1, 1, 1], padding="VALID")
      s5_1 = array_ops.slice(s4_1, [0, 6, 6, 0], [-1, 1, 1, -1])
      s5_2 = array_ops.slice(s4_2, [0, 6, 6, 0], [-1, 1, 1, -1])
      x_concat = array_ops.concat([s5_1, s5_2], 3)
      self.evaluate(
          x_concat
      )  # This test is only meant to check the creation is not crashed

  def testInt64Shape(self):
    with test_util.use_gpu():
      cdim = constant_op.constant(1, dtypes.int32)
      s0 = constant_op.constant([2, 5000000000, 5], dtypes.int64)
      s1 = constant_op.constant([2, 7, 5], dtypes.int64)
      s2 = constant_op.constant([2, 20, 5], dtypes.int64)
      off = gen_array_ops.concat_offset(cdim, [s0, s1, s2])
      ans = self.evaluate(off)
      self.assertAllEqual(
          ans, [[0, 0, 0], [0, 5000000000, 0], [0, 5000000007, 0]])
      self.assertEqual(ans[0].dtype, dtypes.int64)

if __name__ == "__main__":
  test.main()


class Constraint:

    def __init__(self, leftVariable,rightVariable,equality):
        self.leftVariable = leftVariable   # 0 (x0)
        self.rightVariable = rightVariable # 1 (x1)
        self.equality = equality # True (=) False (!=)

def checkConstraints(variables,constraints):

    for i in range (len(constraints)):
        leftVariableIndex = constraints[i].leftVariable
        rightVariableIndex = constraints[i].rightVariable
        if constraints[i].equality and variables[leftVariableIndex] != variables[rightVariableIndex]:
            return False
        elif (not constraints[i].equality) and variables[leftVariableIndex] == variables[rightVariableIndex]:
            return False
    return True

############ Scenario 1 #############
c1 = Constraint(0,2,True) # (x0 = x2)
c2 = Constraint(1,3,False) # (x1 != x3)

A = [5,2,5,7] # Variables
B = [c1,c2] # Constraints

print("Scenario 1:",checkConstraints(A,B))

############ Scenario 2 #############

c1 = Constraint(0,2,True) # (x0 = x2)
c2 = Constraint(0,3,True) # (x0 = x3)

A = [5,2,5,7] # Variables
B = [c1,c2] # Constraints

print("Scenario 2:",checkConstraints(A,B))


def longest(lst):
    maxl=0
    for i in lst:
        if(len(i)>maxl):
            maxl=len(i)
            temp=i
    print("The word with longest length is ",temp," : length ",maxl)

list1=[]
n=int(input("Enter number of strings :"))
print("Enter strings :")
for i in range(n):
    s=input()
    list1.append(s)
longest(list1)


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

# In[ ]:

import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import GradientBoostingClassifier, AdaBoostClassifier
from sklearn.metrics import recall_score

# In[ ]:

df = pd.read_csv('./datasets/data_all.csv')

# In[ ]:

X = df['content']
y = df['subreddit']

# In[ ]:

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, stratify=y)

# In[ ]:

vects = [CountVectorizer(), TfidfVectorizer()]
models = [LogisticRegression(), KNeighborsClassifier(),
          MultinomialNB(), RandomForestClassifier(random_state=42),
          AdaBoostClassifier(random_state=42),
          GradientBoostingClassifier(random_state=42),
          SVC(random_state=42)]

# In[ ]:

pipes = []
for vect in vects:
    for model in models:
        pipe = Pipeline([
        ('vect', vect),
        ('model', model)])
        pipes.append(pipe)

# In[ ]:

params_vect = {'CountVectorizer': {'vect__stop_words': [None, 'english'],
                                   'vect__ngram_range': [(1,1), (1,2)]},
               'TfidfVectorizer': {'vect__stop_words': [None, 'english'],
                                   'vect__ngram_range': [(1,1), (1,2)]}}

# In[ ]:

params_model = {'LogisticRegression': {'model__penalty': ['l1', 'l2'],
                                       'model__C': np.logspace(-3, 3, 10)},
                'KNeighborsClassifier': {'model__n_neighbors': [5, 7, 15],
                                         'model__weights': ['uniform', 'distance']},
                'MultinomialNB': {},
                'RandomForestClassifier': {'model__n_estimators': [100, 150, 200],
                                           'model__max_depth': [None, 2, 4]},
                'AdaBoostClassifier': {'model__n_estimators': [100, 150, 200]},
                'GradientBoostingClassifier': {'model__n_estimators': [100, 150, 200]},
                'SVC': {'model__C': np.logspace(-2, 2, 5),
                        'model__gamma': np.logspace(-4, 0, 5)}
               }

# In[ ]:

df = pd.DataFrame(columns=['Vectorizer', 'Classifier',
                           'best_params', 'train_recall', 'test_recall',
                           'train_accuracy', 'test_accuracy'])

# In[ ]:

for pipe in pipes:
    Vectorizer = str(pipe.steps[0][1]).split('(')[0]
    Classifier = str(pipe.steps[1][1]).split('(')[0]

    print('\nGrid search using {} and {} ...\n'.format(Vectorizer, Classifier))

    params = {**params_vect[Vectorizer], **params_model[Classifier]}

    grid = GridSearchCV(pipe,
                    param_grid=params,
                    cv = 5,
                    scoring = 'accuracy',
                    verbose = 1,
                    n_jobs = -1,
                    return_train_score = True)

    grid.fit(X_train, y_train);

    output = {}
    output['Vectorizer'] = Vectorizer
    output['Classifier'] = Classifier
    output['best_params'] = grid.best_params_
    output['train_accuracy'] = grid.best_score_
    output['test_accuracy'] = grid.score(X_test, y_test)
    output['train_recall'] = recall_score(y_train, grid.predict(X_train))
    output['test_recall'] = recall_score(y_test, grid.predict(X_test))

    df = df.append(output, ignore_index=True)

    df.to_csv('./datasets/results.csv', index=False)


import re
import json

file = 'class.txt'

# Dst-Site-Prefix: 10.225.76.164/32    DSCP: default [0] Traffic class id:59827542
p1 = re.compile(r'Dst-Site-Prefix:\s*(?P<dst_site_prefix>\S+)\s*DSCP:\s*(?P<dscp>\S*)')

#  Clock Time:                 14:16:10 (CDT) 08/05/2019
p2 = re.compile(r'\S*Clock\s*Time:\s*(?P<clock_time>.*)')

#  TC Learned:                 4w2d ago
p3 = re.compile(r'\S*TC\s*Learned:\s*(?P<tc_learned>.*)')

#  Present State:              CONTROLLED
p4 = re.compile(r'\S*Present\s*State:\s*(?P<present_state>.*)')

#  Current Performance Status: in-policy
p5 = re.compile(r'\S*Current\s*Performance\s*Status:\s*(?P<current_performance_status>.*)')

#  Current Service Provider:   ISP2 since 09:39:28
p6 =   re.compile(r'\S*Current\s*Service\s*Provider:\s*(?P<current_service_provider>.*)')

#  Previous Service Provider:  ISP1 pfr-label: 0:0 | 0:0 [0x0] for 180 sec
p7 = re.compile(r'\S*Previous\s*Service\s*Provider:\s*(?P<previous_service_provider>.*)')

#  BW Used:                    0 Kbps
p8 = re.compile(r'\S*BW\s*Used:\s*(?P<bw_used>.*)')

#  Present WAN interface:      Tunnel200 in Border 10.1.0.104
p9 = re.compile(r'\S*Present\s*WAN\s*interface:\s*(?P<present_wan_interface>\S*)\s*in\sBorder\s(?P<border>.*)')

#  Present Channel (primary):  12488652 ISP2 pfr-label:0:0 | 0:0 [0x0]
p10 = re.compile(r'\S*Present\s*Channel\s*\(primary\):\s*(?P<present_channel>\d+)')

#  Backup Channel:             12488653 ISP1 pfr-label:0:0 | 0:0 [0x0]
p11 = re.compile(r'\S*Backup\s*Channel:\s*(?P<backup_channel>\d+)')

#  Destination Site ID bitmap: 0
p12 = re.compile(r'\S*Destination\s*Site\s*ID\s*bitmap:\s*(?P<dst_site_id_bit>.*)')

#  Destination Site ID:        10.50.0.84
p13 = re.compile(r'\S*Destination\s*Site\s*ID:\s*(?P<dst_site_id>.*)')

#  Class-Sequence in use:      20
p14 = re.compile(r'\S*Class-Sequence\s*in\s*use:\s*(?P<class_sequence_in_use>.*)')

#  Class Name:                 BEST_EFFORT using policy User-defined
p15 = re.compile(r'.*\S*Class\s*Name:\s*(?P<class_name>\S*)')
    #priority 2 packet-loss-rate threshold 40.0 percent
    #priority 2 byte-loss-rate threshold 40.0 percent
p16 = re.compile(r'(?P<priority>priority\s[1-9])\s*(?P<detail>\S*)\s*(?P<type>\S*)\s*(?P<value>.*)')

#  BW Updated:                 00:00:26 ago
p17 = re.compile(r'\S*BW\s*Updated:\s*(?P<bw_updated>.*)')

#  Reason for Latest Route Change:    Backup to Primary path preference transition

#  Route Change History:
p18 = re.compile(r'Date and Time')

#  1:  04:36:42 (CDT) 08/05/19   ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       Backup to Primary path preference transition
#  2:  04:33:42 (CDT) 08/05/19   ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       Unreachable
#  3:  02:23:41 (CDT) 08/05/19   ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       Backup to Primary path preference transition
#  4:  02:20:39 (CDT) 08/05/19   ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       Unreachable
#  5:  02:10:21 (CDT) 08/03/19   ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       Backup to Primary path preference transition
p19 = re.compile(r'\s*(\d:)\s*(?P<date>\S*\s*\S*\s*\S*)\s*(?P<previous_exit>\S*\s*\S*)\s*(?P<current_exit>\S*\s*\S*)\s*(?P<reason>.*)')

# 0 or 1 flags
route_change_flag = 0

# Init vars
parsed_dict = {}
class_name = ''

out = open(file, "r")

for line in out:
    if len(line.strip()) == 0: continue

    line = line.strip()

    # Dst-Site-Prefix: 10.225.76.164/32    DSCP: default [0] Traffic class id:59827542
    m = p1.match(line)
    if m:
        dst_site_prefix = m.group('dst_site_prefix')
        dst_site_dscp = m.group('dscp')

        if dst_site_prefix not in parsed_dict:
            parsed_dict[dst_site_prefix] = {}

        if dst_site_dscp not in parsed_dict[dst_site_prefix]:
            parsed_dict[dst_site_prefix][dst_site_dscp] = {}
            traffic_classes_dict = parsed_dict[dst_site_prefix][dst_site_dscp]

        # Init keys
        traffic_classes_dict['clock_time'] = ''
        traffic_classes_dict['tc_learned'] = ''
        traffic_classes_dict['present_state'] = ''
        traffic_classes_dict['current_performance_status'] = ''
        traffic_classes_dict['current_service_provider'] = ''
        traffic_classes_dict['previous_service_provider'] = ''
        traffic_classes_dict['bandwidth'] = {}
        traffic_classes_dict['present_wan_interface'] = {}
        traffic_classes_dict['channel'] = {}
        traffic_classes_dict['dst_site_id_bit'] = ''
        traffic_classes_dict['dst_site_id'] = ''
        traffic_classes_dict['class_sequence_in_use'] = ''
        traffic_classes_dict['class_name'] = {}
        traffic_classes_dict['route_change_history'] = []
        route_change_flag = 0
        class_name = ''
        continue

    #  Clock Time:                 14:16:10 (CDT) 08/05/2019
    m = p2.match(line)
    if m:
        traffic_classes_dict['clock_time'] = m.group('clock_time')
        continue

    #  TC Learned:                 4w2d ago
    m = p3.match(line)
    if m:
        traffic_classes_dict['tc_learned'] = m.group('tc_learned')
        continue
    #  Present State:              CONTROLLED
    m = p4.match(line)

    if m:
        traffic_classes_dict['present_state'] = m.group('present_state')
        continue
    #  Current Performance Status: in-policy
    m = p5.match(line)

    if m:
        traffic_classes_dict['current_performance_status'] = m.group('current_performance_status')
        continue
    #  Current Service Provider:   ISP2 since 09:39:28
    m = p6.match(line)

    if m:
        traffic_classes_dict['current_service_provider'] = m.group('current_service_provider')
        continue
    #  Previous Service Provider:  ISP1 pfr-label: 0:0 | 0:0 [0x0] for 180 sec
    m = p7.match(line)

    if m:
        traffic_classes_dict['previous_service_provider'] = m.group('previous_service_provider')
        continue
    #  BW Used:                    0 Kbps
    m = p8.match(line)

    if m:
        traffic_classes_dict['bandwidth']['used'] = m.group('bw_used')
        continue

    #  BW Updated:                 00:00:26 ago
    m = p17.match(line)
    if m:
        traffic_classes_dict['bandwidth']['updated'] = m.group('bw_updated')
        continue

    #  Present WAN interface:      Tunnel200 in Border 10.1.0.104
    m = p9.match(line)
    if m:
        traffic_classes_dict['present_wan_interface']['tunnel'] = m.group('present_wan_interface')
        traffic_classes_dict['present_wan_interface']['border'] = m.group('border')
        continue

    #  Present Channel (primary):  12488652 ISP2 pfr-label:0:0 | 0:0 [0x0]
    m = p10.match(line)
    if m:
        traffic_classes_dict['channel']['present'] = m.group('present_channel')
        #traffic_classes_dict['channel']['present'] = channel_data[m.group('present_channel')]
        continue

    #  Backup Channel:             12488653 ISP1 pfr-label:0:0 | 0:0 [0x0]
    m = p11.match(line)
    if m:
        traffic_classes_dict['channel']['backup'] = m.group('backup_channel')
        #  traffic_classes_dict['channel']['backup'] = channel_data[m.group('backup_channel')]
        continue

    #  Destination Site ID bitmap: 0
    m = p12.match(line)
    if m:
        traffic_classes_dict['dst_site_id_bit'] = m.group('dst_site_id_bit')
        continue

    #  Destination Site ID:        10.50.0.84
    m = p13.match(line)
    if m:
        traffic_classes_dict['dst_site_id'] = m.group('dst_site_id')
        continue

    #  Class-Sequence in use:      20
    m = p14.match(line)
    if m:
        traffic_classes_dict['class_sequence_in_use'] = m.group('class_sequence_in_use')
        continue

    #  Class Name:                 BEST_EFFORT using policy User-defined
    m = p15.match(line)
    if m:
        class_name = m.group('class_name')
        traffic_classes_dict['class_name'][class_name] = {}
        continue
    #  priority 2 packet-loss-rate threshold 40.0 percent
    m = p16.match(line)
    if m:
        traffic_classes_dict['class_name'][class_name]['priority'] = m.group('priority')
        traffic_classes_dict['class_name'][class_name]['detail'] = m.group('detail')
        traffic_classes_dict['class_name'][class_name]['type'] = m.group('type')
        traffic_classes_dict['class_name'][class_name]['value'] = m.group('value')
        continue

    #  Route Change History:
    m = p18.match(line)
    if m:
        route_change_flag = 1
        continue

    if route_change_flag:
        #  1:  04:36:42 (CDT) 08/05/19   ISP1(0:0|0:0)/10.1.0.101/Tu100 (Ch:12488653)       ISP2(0:0|0:0)/10.1.0.104/Tu200 (Ch:12488652)       Backup to Primary path preference transition
        m = p19.match(line)
        if m:
            route_change_dict = {}
            route_change_dict['date'] = m.group('date')
            route_change_dict['previous_exit'] = m.group('previous_exit')
            route_change_dict['current_exit'] = m.group('current_exit')
            route_change_dict['reason'] = m.group('reason')
            traffic_classes_dict['route_change_history'].append(route_change_dict)
            continue

print(json.dumps(parsed_dict))


import os
import io
import sys
import pickle
import random
import argparse
import subprocess
import numpy as np
import torch
import pandas as pd
from logging import getLogger
from itertools import permutations

from .logger import create_logger

MAIN_DUMP_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'dumped')

logger = getLogger()

# load Faiss if available (dramatically accelerates the nearest neighbor search)
try:
    import faiss
    FAISS_AVAILABLE = True
    if not hasattr(faiss, 'StandardGpuResources'):
        sys.stderr.write("Impossible to import Faiss-GPU. "
                         "Switching to FAISS-CPU, "
                         "this will be slower.\n\n")

except ImportError:
    sys.stderr.write("Impossible to import Faiss library!! "
                     "Switching to standard nearest neighbors search implementation, "
                     "this will be significantly slower.\n\n")
    FAISS_AVAILABLE = False

def initialize_exp(params):
    """
    Initialize experiment.
    """
    # initialization
    if getattr(params, 'seed', -1) == -1:
        params.seed = np.random.randint(1e8)
    random.seed(params.seed)
    np.random.seed(params.seed)
    torch.manual_seed(params.seed)
    if params.cuda:
        torch.cuda.manual_seed(params.seed)

    # dump parameters
    params.exp_path = get_exp_path(params)
    with io.open(os.path.join(params.exp_path, 'params.pkl'), 'wb') as f:
        pickle.dump(params, f)

    # create logger
    logger = create_logger(os.path.join(params.exp_path, 'train.log'), vb=params.verbose)
    logger.info('============ Initialized logger ============')
    logger.info('\n'.join('%s: %s' % (k, str(v)) for k, v in sorted(dict(vars(params)).items())))
    logger.info('The experiment will be stored in %s' % params.exp_path)
    return logger

def get_nn_avg_dist(emb, query, knn):
    """
    Compute the average distance of the `knn` nearest neighbors
    for a given set of embeddings and queries.
    Use Faiss if available.
    """
    if FAISS_AVAILABLE:
        emb = emb.cpu().numpy()
        query = query.cpu().numpy()
        if hasattr(faiss, 'StandardGpuResources'):
            # gpu mode
            res = faiss.StandardGpuResources()
            config = faiss.GpuIndexFlatConfig()
            config.device = 0
            index = faiss.GpuIndexFlatIP(res, emb.shape[1], config)
        else:
            # cpu mode
            index = faiss.IndexFlatIP(emb.shape[1])
        index.add(emb)
        distances, _ = index.search(query, knn)
        return distances.mean(1)
    else:
        bs = 1024
        all_distances = []
        emb = emb.transpose(0, 1).contiguous()
        for i in range(0, query.shape[0], bs):
            distances = query[i:i + bs].mm(emb)
            best_distances, _ = distances.topk(knn, dim=1, largest=True, sorted=True)
            all_distances.append(best_distances.mean(1).cpu())
        all_distances = torch.cat(all_distances)
        return all_distances.numpy()

def bool_flag(s):
    """
    Parse boolean arguments from the command line.
    """
    if s.lower() in ['off', 'false', '0']:
        return False
    if s.lower() in ['on', 'true', '1']:
        return True
    raise argparse.ArgumentTypeError("invalid value for a boolean flag (0 or 1)")

def get_exp_path(params):
    """
    Create a directory to store the experiment.
    """
    # create the main dump path if it does not exist
    exp_folder = MAIN_DUMP_PATH if params.exp_path == '' else params.exp_path
    if not os.path.exists(exp_folder):
        subprocess.Popen("mkdir %s" % exp_folder, shell=True).wait()
    assert params.exp_name != ''
    exp_folder = os.path.join(exp_folder, params.exp_name)
    if not os.path.exists(exp_folder):
        subprocess.Popen("mkdir %s" % exp_folder, shell=True).wait()
    if params.exp_id == '':
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
        while True:
            exp_id = ''.join(random.choice(chars) for _ in range(10))
            exp_path = os.path.join(exp_folder, exp_id)
            if not os.path.isdir(exp_path):
                break
    else:
        exp_path = os.path.join(exp_folder, params.exp_id)
        assert not os.path.isdir(exp_path), exp_path
    # create the dump folder
    if not os.path.isdir(exp_path):
        subprocess.Popen("mkdir %s" % exp_path, shell=True).wait()
    return exp_path

def normalize_embeddings(emb, types, mean=None):
    """
    Normalize embeddings by their norms / recenter them.
    """
    for t in types.split(','):
        if t == '':
            continue
        if t == 'center':
            if mean is None:
                mean = emb.mean(0, keepdim=True)
            emb.sub_(mean.expand_as(emb))
        elif t == 'renorm':
            emb.div_(emb.norm(2, 1, keepdim=True).expand_as(emb))
        else:
            raise Exception('Unknown normalization type: "%s"' % t)
    return mean.cpu() if mean is not None else None

def apply_mapping(map,input,device=None):
    if device is None:
        device = map.weight.device
    map = map.to(device)
    return map(input.to(device))

def save_results(params,results_by_inf_met, methods):
    columns_order = ['{}-{}'.format(src, tgt) for src, tgt in permutations(params.langs, 2)
                     if 'en' in [src, tgt] or
                     len(set(['en','de','fr','es','it','pt']).intersection(set([src, tgt])))==2]
    df = pd.DataFrame()
    # aggregate results
    for (src, tgt), pair_res in results_by_inf_met.items():
        for inf_met, p_at_1 in pair_res.items():
            df = df.append({'task':'{}-{}'.format(src,tgt),'inf-met':inf_met,'p@1':p_at_1}, ignore_index=True)
    # re-arrange results in more convenience way
    df = df.pivot(index='inf-met',columns='task',values='p@1')
    df = df.loc[methods]
    df = df[columns_order]
    df['avg'] = df.mean(1)
    # print and save results
    logger.info(df)
    df.to_csv(os.path.join(params.exp_path, 'results.csv'))


import machine
import utime

led = machine.PWM(machine.Pin(10))
led.freq(1000)
temp_sensor = machine.ADC(4)
potenciometer = machine.ADC(26)
conversion_factor = 3.3 / (65535)

while True:
    # voltage = potenciometer.read_u16() * conversion_factor
    reading = temp_sensor.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    led.duty_u16(int(temperature * 1000))
    print(int(temperature * 1000))
    utime.sleep(1)


import datetime
import database as d
def sumafecha(fecha,dias_repeticion):

	fecha      = fecha
	Date       = datetime.datetime.strptime(fecha, "%Y-%m-%d")
	EndDate    = str(Date + datetime.timedelta(days=dias_repeticion))[0:10]
	a          = datetime.datetime.strptime(EndDate, "%Y-%m-%d").date()
	fechaactual= datetime.datetime.strptime(str(datetime.datetime.today())[0:10], "%Y-%m-%d").date()
	return int((a-fechaactual).days)

def plan_mantto(tipo_medicion,nivel_medicion,fecha,id_usuario):

	where_mantto  = " where medicion="+tipo_medicion+" and evaluacion="+ str(nivel_medicion) + ""
	mantto        = d.Database('plan_mantto_general','','dias_repeticion')
	row_mantto    = mantto.get(where_mantto)
	if(row_mantto==True):
		dias= mantto.rows[0]['dias_repeticion']

		atrasoMedicion='0'
		sumfec=sumafecha(fecha,dias)
		if(sumfec<0):
			atrasoMedicion='1'

		#print str(id_usuario)+'*'+str(atrasoMedicion)
		return  atrasoMedicion
	else:
		return false;