from __future__ import absolute_import
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator, img_to_array,array_to_img
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from keras.callbacks import ModelCheckpoint
import keras
from keras.datasets import mnist
from six.moves import range
from os import listdir
from os.path import isfile, join
from pydub import AudioSegment

from dataset_utils import *
from ec2s3 import store_to_s3, get_from_s3, get_bucket_items, shutdown_spot_request, check_for_early_shutdown, self_instance_id
import tempfile
import time
import StringIO
import requests
import theano
import os
from spectrogram import plotstft
from multi import parmap
'''
    Train a (fairly simple) deep CNN on the CIFAR10 small images dataset.
    GPU run command:
        THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cifar10_cnn.py
    It gets down to 0.65 test logloss in 25 epochs, and down to 0.55 after 50 epochs.
    (it's still underfitting at that point, though).
    Note: the data was pickled with Python 2, and some encoding issues might prevent you
    from loading it in Python 3. You might have to load it in Python 2,
    save it in a different format, load it in Python 3 and repickle it.
'''
config_file = os.environ.get('CONFIG')
config = init_config()
#config = read_json_file(config_file)

debug_mode = os.environ.get('LOCALDEBUG')
api_key = os.environ['SENDGRID']
sendgrid_url = "https://api.sendgrid.com/api/mail.send.json"

startup_data={"to" : "quinnjarr@gmail.com",
              "from" : "quinnjarr@gmail.com",
              "subject" : "Startup",
              "html" : "Starting ec2 cats vs dogs",
              }
authent_header = {"Authorization" : "Bearer %s" % api_key}
if debug_mode is None:
    requests.post(sendgrid_url, data=startup_data, headers=authent_header)
max_num_data = 300000
temp_file_name = "/tmp/genres.hdf5"
batch_size = 32
nb_epoch = 50
data_augmentation = True
# shape of the image (SHAPE x SHAPE)
shapex, shapey = 32, 32
# number of convolutional filters to use at each layer
nb_filters = [32, 32]
# level of pooling to perform at each layer (POOL x POOL)
nb_pool = (2, 2)
# level of convolution to perform at each layer (CONV x CONV)
nb_conv = (3, 3)
# the CIFAR10 images are RGB
image_dimensions = 3

def load_data():

    config['data'] = load_files(config)
    """for song, category in config['data'].items():
        export_path = add_paths(config['data_path'], 'segmented')
        segments = segment_song(song, export_path)
        for idx, segment in enumerate(segments):
            path = export_path + song
            create_path(path)
            segment.export(append_file_name(path, str(idx)),format="wav")"""

    #song = AudioSegment.from_mp3("test1.mp3")

    #prep_spectrograms()

    x_train_imgs, x_test_imgs, y_train, y_test = gen_test_train(config)
    print("loading mp3s")

    X_train = np.zeros((len(x_train_imgs), 3, shapex, shapey), dtype="uint8")
    y_train = np.asarray(y_train, dtype="uint8")

    X_test = np.zeros((len(x_test_imgs), 3, shapex, shapey), dtype="uint8")
    y_test = np.asarray(y_test, dtype="uint8")

    for i in range(len(x_train_imgs)):
        X_train[i] = load_image(x_train_imgs[i], xsize=shapex, ysize=shapey)
    for i in range(len(x_test_imgs)):
        X_test[i] = load_image(x_test_imgs[i], xsize=shapex, ysize=shapey)

    y_train = np.reshape(y_train, (len(y_train), 1))

    y_test = np.reshape(y_test, (len(y_test), 1))

    X_train = X_train.astype("float32")
    X_test = X_test.astype("float32")

    X_train /= 255
    X_test /= 255

    return (X_train, y_train), (X_test, y_test)

# the data, shuffled and split between tran and test sets
print("Loading training data")
(X_train, y_train), (X_test, y_test)= load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, len(config['cat_to_id'].items()))
Y_test = np_utils.to_categorical(y_test, len(config['cat_to_id'].items()))

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(image_dimensions, shapey, shapex)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(len(config['cat_to_id'].items())))
model.add(Activation('softmax'))

print("compiling")
mode = 'FAST_COMPILE' if debug_mode else 'FAST_RUN'
#mode = 'FAST_RUN'
print("Using %s mode" % mode)
model.compile(loss='categorical_crossentropy', optimizer='adadelta', theano_mode=mode)

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []
        self.best_lost = 9239129
        self.i = 0

    def on_epoch_end(self, epoch, logs={}):
        if check_for_early_shutdown():
            save_data()
            shutdown_spot_request()
        loss = logs.get('val_loss')
        print("%s loss is %s. Current best lost is %s" % (self.i, loss, self.best_lost))

        if loss < self.best_lost:
            print("%s new BEST loss is %s" % (self.i, loss))
            self.i += 1
            print(self.i)
            self.best_lost = loss
            if True:
                print("Saving data")
                save_data()
                save_email_data = startup_data
                save_email_data['html'] = "new loss is %s" % loss
                save_email_data['subject'] = "New saved!"
                if debug_mode is None:
                    requests.post(sendgrid_url, data=save_email_data, headers=authent_header)

    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        if check_for_early_shutdown():
            save_data()
            shutdown_spot_request()

def create_model():
    # the data, shuffled and split between tran and test sets
    (X_train, y_train), (X_test, y_test) = load_data()
    print('X_train shape:', X_train.shape)
    print(X_train.shape[0], 'train samples')
    print(X_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, len(config['cat_to_id'].items()))
    Y_test = np_utils.to_categorical(y_test, len(config['cat_to_id'].items()))

    print("starting model")

    model.save_weights(temp_file_name,overwrite=True)
    with open(temp_file_name) as f:
        store_to_s3(str(int(time.time())),config['s3_bucket'], f.read())

def load_model():
    print("Getting %s bucket items" % config['s3_bucket'])
    items = get_bucket_items(config['s3_bucket'])
    newest_item = max(map(int,items))
    print("Newest item %s, retrieving now" % newest_item)
    model_weights = get_from_s3(str(newest_item), config['s3_bucket'])
    print("loading model")
    with open('./current.weights', 'w') as f:
        f.write(model_weights)

    model.load_weights("./current.weights")

def save_data():
    model.save_weights(temp_file_name, overwrite=True)
    if debug_mode is None:
        with open(temp_file_name, 'r') as f:
            store_to_s3(str(int(time.time())), config['s3_bucket'], f.read())
    else:
        print("Not saving to S3, since we're in debug mode.")

def train():

    # compute quantities required for featurewise normalization
    # (std, mean, and principal components if ZCA whitening is applied)
    #checkpointer = ModelCheckpoint(filepath="/Users/quinnjarrell/Desktop/Experiments/keras/saved/", verbose=1, save_best_only=True)
    history = LossHistory()
    model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, Y_test),callbacks=[history])
    score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
    if debug_mode is None:
        save_data()
        shutdown_spot_request()
def make_mosaic(imgs, nrows, ncols, border=1):
    """
    Given a set of images with all the same shape, makes a
    mosaic with nrows and ncols
    """
    nimgs = imgs.shape[0]
    imshape = imgs.shape[1:]

    mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
                            ncols * imshape[1] + (ncols - 1) * border),
                            dtype=np.float32)

    paddedh = imshape[0] + border
    paddedw = imshape[1] + border
    for i in xrange(nimgs):
        row = int(np.floor(i / ncols))
        col = i % ncols

        mosaic[row * paddedh:row * paddedh + imshape[0],
               col * paddedw:col * paddedw + imshape[1]] = imgs[i]
    return mosaic
def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
    import matplotlib.pyplot as plt

    """Wrapper around pl.imshow"""
    if cmap is None:
        cmap = cm.jet
    if vmin is None:
        vmin = data.min()
    if vmax is None:
        vmax = data.max()

    im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', cmap=cmap)
    plt.colorbar(im)
    plt.show()

def predict():
    import pylab as pl
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    np.set_printoptions(precision=5, suppress=True)
    #convout1_f = theano.function([model.get_input(train=False)], convout1.get_output(train=False))
    predictions = model.predict(X_test[:10]).tolist()
    print("Tests ", zip(predictions, X_test[:10].tolist()))
    import pdb; pdb.set_trace()  # breakpoint 8d9fb711 //

    Y_pred = model.predict(X_test)
    # Convert one-hot to index
    y_pred = np.argmax(Y_pred, axis=1)
    from sklearn.metrics import classification_report
    print(classification_report(y_test , y_pred))
    # Visualize convolution result (after activation)
    i = 100
    # Visualize the first layer of convolutions on an input image
    X = X_test[i:i+1]
    print("I predict a ", model.predict(X))

    plt.figure()
    plt.title('input')

    nice_imshow(plt.gca(), array_to_img(np.squeeze(X)), vmin=0, vmax=1, cmap=cm.binary)
    #C1 = convout1_f(X)
    #C1 = np.squeeze(C1)
    #print("C1 shape : ", C1.shape)

    plt.figure(figsize=(shapex, shapey))
    plt.suptitle('convout1')
    #nice_imshow(plt.gca(), make_mosaic(C1, 6, 6), cmap=cm.binary)

def predict_image(song_path, song_type):
    num_pred_segments = 5
    data = segment_song()
    #print(hash(tuple(data.tolist()[0][0])))
    type_id = config.catgory_to_id[song_type]
    X_pred = np.zeros((num_pred_segments, 3, 32, 32), dtype="uint8")
    for i in range(num_pred_segments):
        X_pred[i] = data[i]
    prep_spectrograms()

    X_pred = X_pred.astype("float32")
    X_pred /= 255
    prediction = model.predict(X_pred)
    pred_max = mode(map(prediction, np.argmax))
    pred_name = config['categories'][pred_max]
    pred_conf = prediction[0][pred_max]

    print(prediction)
    print("Guessing %s is a %s with confidence %s. Guess is %s" % (song_path, pred_name, pred_conf, pred_name == song_type))

def save_graph():
    from keras.utils.visualize_util import plot
    plot(model, to_file=str(time.time()) + "-" + self_instance_id + '.png')

#create_model()
load_model()
train()
#predict()
#predict_image("/Users/quinnjarrell/Downloads/catbread.jpg", 'cat')
#save_graph()


import os
import json
import argparse
import pdb
from tqdm import tqdm

import numpy as np
import h5py
from sklearn.cluster import KMeans

from read_data import *
from utils import exists

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Getting features')
    parser.add_argument('--ref_file', default="/examples/ref_file.csv", type=str, required=True, help='Path with reference csv file')
    parser.add_argument('--patch_data_path', default="/examples/Patches_hdf5", type=str, required=True, help='Directory where the patch is saved')
    parser.add_argument('--feature_path', type=str, default="/examples/features", help='Output directory to save features')
    parser.add_argument('--num_clusters', type=int, default=100,
                        help='Number of clusters for the kmeans')
    parser.add_argument("--tcga_projects", help="the tcga_projects we want to use",
                        default=None, type=str, nargs='*')
    parser.add_argument('--start', type=int, default=0,
                        help='Start slide index for parallelization')
    parser.add_argument('--end', type=int, default=None,
                        help='End slide index for parallelization')
    parser.add_argument("--gtex", help="using gtex data",
                    action="store_true")
    parser.add_argument('--gtex_tissue', type=str, default=None,
                        help='GTex tissue being used')
    parser.add_argument('--seed', type=int, default=99,
        help='Seed for random generation')

    args = parser.parse_args()

    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    print(10*'-')
    print('Args for this experiment \n')
    print(args)
    print(10*'-')

    path_csv = args.ref_file

    df = pd.read_csv(path_csv)
    df = df.drop_duplicates(['wsi_file_name'])

    # Filter tcga projects
    if args.tcga_projects:
        df = df[df['tcga_project'].isin(args.tcga_projects)]

    print(f'Total number of slides = {df.shape[0]}')

    # indexing based on values for parallelization
    if exists(args.start) and exists(args.end):
        df = df.iloc[args.start:args.end]
    elif exists(args.start):
        df = df.iloc[args.start:]
    elif args.end:
        df = df.iloc[:args.end]

    print(f'New number of slides = {df.shape[0]}')

    for i, row in tqdm(df.iterrows()):
        WSI = row['wsi_file_name']
        if args.gtex:
            project = args.gtex_tissue
        else:
            project = df.iloc[0]['tcga_project']
            WSI = WSI.replace('.svs', '')

        path = os.path.join(args.feature_path, project, WSI)
        try:
            f = h5py.File(os.path.join(path,WSI+'.h5'), "r+")
        except:
            print(f'Cannot open file {path}')
            continue
        try:
            features = f['resnet_features']
        except:
            print(f'No resnet features for {path}')
            f.close()
            continue

        if features.shape[0] < args.num_clusters:
            print(f'{WSI} less number of patches than clusters')
            f.close()
            continue

        if 'cluster_features' in f.keys():
            print(f'{WSI}: Cluster feature already available')
            f.close()
            continue

        kmeans = KMeans(n_clusters=args.num_clusters, random_state=0).fit(features)
        clusters = kmeans.labels_

        mean_features = []
        for pos in tqdm(range(args.num_clusters)):
            indexes = np.where(clusters == pos)
            features_aux = features[indexes]
            mean_features.append(np.mean(features_aux, axis=0))

        mean_features = np.asarray(mean_features)

        try:
            dset = f.create_dataset("cluster_features", data=mean_features)
            f.close()
        except Exception as e:
            print(f"{WSI}: Error in creating cluster_feauture")
            print(e)
            f.close()
    print('Done!')


import time
import tkinter as tk
import customtkinter as ctk
import speech_synthesis as ss
import settings as st
from PIL import Image
from autocorrect import Speller
from wrapt_timeout_decorator import *

@timeout(10)
def text_correction(text, polish):
    if polish:
        spell = Speller("pl")
    else:
        spell = Speller()
    corrected = spell(text)
    return corrected

class Keyboard(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.bind('<Double-Button-1>', self.handler)
        self.configure(padx=25, pady=10)
        self.width = self.winfo_screenwidth()  # used for alert box
        self.height = self.winfo_screenheight() - 70
        self.wm_attributes("-topmost", 1)
        self.attributes('-fullscreen', True)

        ctk.set_appearance_mode("dark")

        self.buttons = {}
        self.text_box = ctk.CTkTextbox(self, padx=25, pady=25, height=5, font=('Segoe UI Historic', 30, "bold"),
                                       text_color="black", wrap=tk.WORD)
        self.text_box.grid(row=0, column=0, columnspan=13, rowspan=3, sticky="nsew")
        self.text_box.configure(state="disabled", corner_radius=50, fg_color="#c9c9c9")

        self.alt_state = False
        self.alert = None
        self.settings = None
        for i in range(13):
            self.grid_columnconfigure(i, weight=1)
            if i <= 8:
                self.grid_rowconfigure(i, weight=1)

        self.keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '#', 'Backspace',
                     'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
                     '*', '=', 'Enter', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '@', '$', 'CORRECT',
                     'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '?', '!', '%', '£',
                     'ALT', 'CLEAR', 'SPACE', 'READ', 'settings']

        row = 4
        column = 0
        for key in self.keys:

            if row != 8:
                self.buttons[key] = ctk.CTkButton(self, text=key, height=6, text_color='white', fg_color='black',
                                                  hover_color="#ff8c00", font=('Segoe UI Historic', 19, 'bold'),
                                                  command=lambda x=key: self.select(x))
                self.buttons[key].grid(row=row, column=column, sticky="nsew")

            else:
                if column == 0:
                    self.buttons[key] = ctk.CTkButton(self, text=key, text_color='white', fg_color='black',
                                                      corner_radius=20,
                                                      hover_color="#ff8c00", font=('Segoe UI Historic', 19, 'bold'),
                                                      command=lambda x=key: self.select(x))

                    self.buttons[key].grid(columnspan=1, row=row, column=column, sticky="nsew")

                elif column == 1:
                    self.buttons[key] = ctk.CTkButton(self, text=key, text_color='white', fg_color='black',
                                                      corner_radius=20,
                                                      hover_color="#ff8c00", font=('Segoe UI Historic', 19, 'bold'),
                                                      command=lambda x=key: self.select(x))
                    self.buttons[key].grid(columnspan=2, row=row, column=column, sticky="nsew")

                elif column == 2:
                    self.buttons[key] = ctk.CTkButton(self, text=key, text_color='white', fg_color='black',
                                                      corner_radius=20,
                                                      hover_color="#ff8c00", font=('Segoe UI Historic', 19, 'bold'),
                                                      command=lambda x=key: self.select(x))
                    self.buttons[key].grid(columnspan=7, row=row, column=column + 1, sticky="nsew")

                elif column == 3:
                    self.buttons[key] = ctk.CTkButton(self, text=key, text_color='white', fg_color='black',
                                                      corner_radius=20,
                                                      hover_color="#ff8c00", font=('Segoe UI Historic', 19, 'bold'),
                                                      command=lambda x=key: self.select(x))
                    self.buttons[key].grid(columnspan=2, row=row, column=column + 7, sticky="nsew")

                else:
                    img = ctk.CTkImage(light_image=Image.open("img/gear.png"), size=(35, 35))
                    self.buttons[key] = ctk.CTkButton(self, image=img, corner_radius=20, fg_color='black', text="",
                                                      hover_color="#ff8c00",
                                                      command=lambda x=key: self.select(x))
                    self.buttons[key].grid(columnspan=1, row=row, column=column + 8, sticky="nsew")

            # image: Flaticon.com

            column += 1
            if column > 12:
                column = 0
                row += 1

        self.tts = ss.TTS()
        if self.tts.language != "pl_PL":
            self.buttons['ALT'].configure(state='disabled')
        else:
            self.buttons['READ'].configure(text='CZYTAJ')
            self.buttons['CLEAR'].configure(text='WYCZYŚĆ')
            self.buttons['CORRECT'].configure(text='POPRAW')
            self.buttons['SPACE'].configure(text='SPACJA')

    def correct(self):
        txt = self.text_box.get(1.0, tk.END)
        txt = txt.lower()
        try:
            if self.tts.language != "pl_PL":
                result = text_correction(txt, 0)
            else:
                result = text_correction(txt, 1)
            print(result)
            self.text_box.delete(1.0, tk.END)
            self.text_box.insert(tk.INSERT, result.upper())
        except TimeoutError:
            self.show_alert()

    def show_alert(self):
        alert_box = ctk.CTkToplevel(self.master)
        x = (self.width - 400) / 2
        y = (self.height - 250) / 2
        alert_box.geometry(f"400x250+{int(x)}+{int(y)}")

        if self.tts.language == "pl_PL":
            message_label = ctk.CTkLabel(alert_box, text="Błąd korekty tekstu", padx=10, pady=10,
                                         text_color="#fdd890", font=('Segoe UI Historic', 22, 'bold'))
        else:
            message_label = ctk.CTkLabel(alert_box, text="Error in text correction", padx=10, pady=10,
                                         text_color="#fdd890", font=('Segoe UI Historic', 22, 'bold'))

        message_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
        alert_box.overrideredirect(True)
        self.after(2500, alert_box.destroy)

    def select(self, key):
        self.text_box.configure(state="normal")
        if key == "SPACE":
            self.text_box.insert(tk.INSERT, ' ')
        elif key == "Backspace":
            temp_text = self.text_box.get(1.0, tk.END)
            self.text_box.delete(1.0, tk.END)
            self.text_box.insert(tk.INSERT, temp_text[:-2])
        elif key == 'Enter':
            self.text_box.insert(tk.INSERT, '\n')
        elif key == 'CLEAR':
            self.text_box.delete(1.0, tk.END)
        elif key == 'READ':
            txt = self.text_box.get(1.0, tk.END)
            self.tts.speak(txt)
        elif key == 'ALT':
            if not self.alt_state:
                self.alt_state = True
            else:
                self.alt_state = False
            self.alt()
        elif key == 'settings':
            self.wm_attributes("-topmost", 0)
            if self.settings is None or not self.settings.winfo_exists():
                self.settings = st.Settings(self)  # create window if its None or destroyed
        elif key == 'CORRECT':
            self.correct()
        else:
            self.text_box.insert(tk.INSERT, self.buttons[key].cget("text"))

    def alt(self):
        before = ['A', 'Z', 'X', 'C', 'N', 'L', 'S', 'E', 'O', 'U']
        after = ['Ą', 'Ż', 'Ź', 'Ć', 'Ń', 'Ł', 'Ś', 'Ę', 'Ó', '€']
        exceptions = ['ALT', 'CLEAR', 'SPACE', 'READ', 'settings', 'Backspace', 'Enter', 'CORRECT']
        i = 0
        if self.alt_state:
            for char in before:
                self.buttons[char].configure(text=after[i])
                i += 1
            for key in self.keys:
                if key not in before and key not in exceptions:
                    self.buttons[key].configure(text="")
        else:
            for key in self.keys:
                if key not in exceptions:
                    self.buttons[key].configure(text=key)

    def handler(self, e):
        self.destroy()


import os
import sys
import json

_VERSION_CACHE = None

BUILD_DIR = '_build'

CURSEFORGE_URL = 'https://api.curseforge.com/v1/'
CURSEFORGE_MINECRAFT_GAME_ID = '432'
CURSEFORGE_MINECRAFT_MODS_CLASS_ID = '6'

def get_version():
    """ Loads the version name from version.txt """
    global _VERSION_CACHE
    if _VERSION_CACHE is not None:
        return _VERSION_CACHE
    version_file = open('version.txt', 'r')
    _VERSION_CACHE = version_file.read().strip()
    version_file.close()
    return get_version()

def get_game_version():
    return load_manifest()['minecraft']['version']

def load_manifest():
    """ Returns manifest json content """
    manifest_file = open('manifest.json', 'r')
    content = json.load(manifest_file)
    manifest_file.close()
    return content

def get_curseforge_api_key():
    api_key_file = open('.curseforge-api-key.txt', 'r')
    api_key = api_key_file.read().strip()
    api_key_file.close()
    return api_key

def call_curseforge_api(path, method="GET", data={}):
    import requests
    api_key = get_curseforge_api_key()
    headers = {
        'x-api-key': api_key,
        'Accept': 'application/json',
    }
    method = method.upper()
    action = requests.get
    url = CURSEFORGE_URL + path
    if method == 'POST':
        action = requests.post
    return json.loads(action(url, headers=headers, data=data).text)


# coding=utf-8

import socket
from lib.common import save_script_result

ports_to_check = 2181  # 默认服务端口

def do_check(self, url):
    if url != '/':
        return
    port = 2181
    if self.scheme == '	zookeeper' and self.port != 2181:  # 非标准端口
        port = self.port
    elif 2181 not in self.ports_open:
        return

    try:
        socket.setdefaulttimeout(5)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((self.host, port))
        s.send('envi')
        data = s.recv(1024)
        if 'Environment' in data:
            save_script_result(self, '', 'zookeeper://%s:%s' % (self.host, port), '', 'Zookeeper Unauthorized Access')
    except Exception as e:
        pass
    finally:
        s.close()


__author__ = 'rchibana'

from flask import jsonify, request
from flask_restful import Resource
from models import User
from app import db

class UserResource(Resource):

    def post(self):

        data = request.json

        email = data['user'].get('email', '')
        phone = data['user'].get('phone', '')

        user = User.query.filter_by(email=email, phone=phone).first()

        if user:
            return jsonify(user.serialize())

        user = User(email, phone)

        db.session.add(user)
        db.session.commit()

        return jsonify(user.serialize())


from src.data.core.tweet_streamer import TweetStreamer
from unittest.mock import patch

import json

@patch('src.data.data_lake.DataLake')
def test_should_return_exact_data_from_stream(data_lake_mock):
    raw_data = {
        'text': 'this is a mock tweet',
        'in_reply_to_status_id': 1223
    }
    raw_data_json = json.dumps(raw_data)

    tweet_streamer = TweetStreamer(data_lake_mock)

    tweet_streamer.on_data(raw_data_json)

    data_lake_mock.insert.assert_called_with(raw_data)


# from __future__ import print_function, division
import os
import time
import numpy as np
import torch
import pandas as pd
from skimage import io, transform

import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import random

# Ignore warnings
import warnings
warnings.filterwarnings("ignore")
import cv2

from tqdm import tqdm_notebook, tqdm

DIR_TRAIN_IMAGES = "/data/Clouds_Classify/train_images/"

df_train = pd.read_csv(open("/data/Clouds_Classify/train.csv"))
df_train["Label"] = [i.split("_")[1] for i in df_train.Image_Label.values.tolist()]

df_train.head(20)

# print(df_train)

df_train_grp = df_train.dropna().groupby("Label")

idx_fish = df_train_grp.get_group("Fish").index.values.tolist()
idx_flower = df_train_grp.get_group("Flower").index.values.tolist()
idx_gravel = df_train_grp.get_group("Gravel").index.values.tolist()
idx_sugar = df_train_grp.get_group("Sugar").index.values.tolist()
# print(idx_fish, len(idx_flower), len(idx_gravel), len(idx_sugar))

def get_mask(idx:int):
    n = idx
    name_tuple = df_train.iloc[n, 0].split("_")
    img_name, img_label = name_tuple[0], name_tuple[1]
    encoded_pixels = df_train.iloc[n,1]

    enc_pix =  list(map(int,encoded_pixels.split(" ")))
    print(enc_pix,'111111')
    pix, pix_count = [], []
    for i in tqdm(range(0, len(enc_pix))):
        if i%2==0:
            pix.append(enc_pix[i])
        else:
            pix_count.append(enc_pix[i])

    t1 = time.time()
    rle_pixels = [list(range(pix[i],pix[i]+pix_count[i])) for i in range(0, len(pix))]
    rle_mask_pixels = sum(rle_pixels,[])
    print(time.time()-t1)

    img_loc = DIR_TRAIN_IMAGES+img_name
    img = cv2.imread(img_loc)
    mask_img = np.zeros((img.shape[0]*img.shape[1],1), dtype=int)
    mask_img[rle_mask_pixels] = 255
    height,width=img.shape[0], img.shape[1]
    mask = np.reshape(mask_img, (width, height)).T

    return mask, img_label, img

# #plt.imshow(mask);

def disp_image(idx:int):
    mask, img_label, img = get_mask(idx)
    # img_loc = DIR_TRAIN_IMAGES+img_name
    # img = cv2.imread(img_loc)
    # img_to_disp = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    img[mask==0,1] = 255
    return img, img_label

def disp_sample(category:str, ls_idx:list):
    for i in range(len(ls_idx)):
        fig=plt.figure(figsize=(20, 10))
        # fig.subplots_adjust(hspace=0.9, wspace=0.4)

        # ax = fig.add_subplot(1, 1, 1)
        img2disp, _ = disp_image(ls_idx[i])
        # ax.set_title(category)
        plt.imshow(img2disp)

        plt.tight_layout()
        plt.savefig("/data/Clouds_Classify/vis_images/image_{}_{}.png".format(category, ls_idx[i]))
        # plt.show()

disp_sample("fish", idx_fish)
disp_sample("flower", idx_flower)
disp_sample("sugar", idx_sugar)
disp_sample("gravel", idx_gravel)

# def disp_sample_assorted():
#     fig=plt.figure(figsize=(18, 12))
#     #fig.subplots_adjust(hspace=0.9, wspace=0.4)
#     columns = 3
#     rows = 3
#     for i in range(1,10):
#         ax = fig.add_subplot(rows, columns, i)
#         img2disp, img_label = disp_image(random.choice(idx_fish+idx_flower+idx_sugar+idx_gravel))
#         ax.set_title(img_label)
#         plt.imshow(img2disp)

#     plt.tight_layout()
#     plt.show()

# disp_sample_assorted()


from bs4 import BeautifulSoup
import re
import os
from fbSearch import searchInfo

path = os.path.abspath('igreja – Resultados de pesquisa _ Facebook.html')

def nameChurch():
    with open(path,"r") as fp:
        soup = BeautifulSoup(fp, "html.parser")
        cont = 1
        nome = []
        faceb = []
        email = []
        for i in soup.find_all(href=re.compile(r"^https://www.facebook.com/")):
            if i.text != "" and "Paróquia" not in i.text and "Escola" not in i.text and "Senhora" not in i.text:
                if cont > 11:
                    nome.append(i.text)
                    faceb.append(i['href'])
                    email.append(searchInfo(i['href'][24:]))
                print(cont)
                cont+=1
    return nome,faceb,email


import PySimpleGUI as sg

# Criar as janelas e estilos(layouts)
# Criar as janelas iniciais
# Criar loop de leitura de eventos
# Lógica do que deve acontecer ao clicar nos botões

def janela_login():
    sg.theme('Reddit')
    layout = [
        [sg.Text('Nome')],
        [sg.Input()],
        [sg.Button('Continuar')]
    ]
    return sg.Window('Login', layout, finalize=True)

def janela_pedido():
    sg.theme('Reddit')
    layout = [
        [sg.Text('Fazer Pedido')],
        [sg.Checkbox('Pizza de Peperoni', key='pizza1'), sg.Checkbox(
            'Pizza de Frango com Catupiry', key='pizza2')],
        [sg.Button('Voltar'), sg.Button('Fazer Pedido')]
    ]
    return sg.Window('Montar Pedido', layout, finalize=True)

janela1, janela2 = janela_login(), None

while True:
    window, event, values = sg.read_all_windows()
    # Quando Janela for fechada
    if window == janela1 and event == sg.WIN_CLOSED:
        break
    # Quando queremos ir para outra janela
    if window == janela1 and event == 'Continuar':
        janela2 = janela_pedido()
        janela1.hide()
    if window == janela2 and event == 'Voltar':
        janela2.hide()
        janela1.un_hide()
    if window == janela2 and event == 'Fazer Pedido':
        if values['pizza1'] == True and values['pizza2'] == True:
            sg.popup(
                'Foram solicitados uma Pizza Peperoni e uma Pizza Frango com Catupiry')
        elif values['pizza1'] == True:
            sg.popup('Foi solicitado apenas uma Pizza de Peperoni')
        elif values['pizza2'] == True:
            sg.popup('Foi solicitado apenas uma Pizza de Frango com Catupiry')


import pandas as pd
from datetime import datetime

def transform_nyt_json(data:dict) -> pd.DataFrame:
        fields_of_interest = ["id",
                              "published_date",
                              "section", "adx_keywords",
                              "byline",
                              "type",
                              "title",
                              "abstract"
        ]
        filtered_results = [
            {key: result[key] for key in fields_of_interest} for result in data["results"]
        ]
        df = pd.DataFrame(filtered_results)
        df['timestamp_api_call'] = int(datetime.now().strftime('%Y%m%d%H%M%S'))
        return df


import json
from typing import List
from dataclasses import dataclass, field
from pathlib import Path

from rlbottraining.history.exercise_result import ExerciseResultJson
from rlbottraining.history.website.json_utils import slim_copy
from rlbottraining.history.website.view import Aggregator, Renderer
from rlbottraining.paths import HistoryPaths

@dataclass
class SlimResultsRenderer(Renderer):
    """
    Returns contents for static files which can be served.
    Usually for data from an Aggregator.
    """
    slim_results: List[ExerciseResultJson]

    def render(self) -> str:
        return json.dumps(self.slim_results)

slim_keys = [path.split('.') for path in [
    '__class__',
    'run_id',
    'create_time',
    'exercise.name',
    'exercise.__class__',
    'exercise.grader.__class__',
    'grade.__class__',
    'grade.__isinstance_Pass__',
    'grade.__isinstance_Fail__',
]]

@dataclass
class ResultListAggregator(Aggregator):
    path: Path = field(default_factory=lambda: HistoryPaths.Website.data_dir/'slim_results.json')

    def add_exercise_result(self, result_json: ExerciseResultJson):
        if self.path in self.shared_url_map:
            renderer = self.shared_url_map[self.path]
            assert isinstance(renderer, SlimResultsRenderer)
        else:
            renderer = SlimResultsRenderer(slim_results=[])
        slim_result = slim_copy(result_json, slim_keys)
        renderer.slim_results.append(slim_result)
        self.shared_url_map[self.path] = renderer


# Python program to handle directory and files operations in Python

# import modules
import os
import time
from os import scandir
import shutil
import random
import string
import string
import datetime
from datetime import datetime
import sys
import zipfile
from pathlib import Path
import csv
import random
from csv import writer
from faker import Faker

# Path of file
N_PATH = 'C:\\Users\\favou\\Documents\\cs400\\computer-science-400-fall-2022-long-term-systems-project-favourojo\\project\\data\\names.csv'

# Creating a directory
def create_dir():
    """Creates a directory that will include the file"""
    outdir = Path.cwd() / "data"
    if Path.exists(outdir) == True:
        print("Directory already exists!")
    else:
        outdir.mkdir(parents=True, exist_ok=True)
        print("Directory created!")

    return outdir

# Adding files to a directory
def create_file():
    """Generates fake names to CSV file"""
    # creating Faker module
    fake = Faker()
    fake.name()

    # writing into files
    with open(N_PATH, 'a') as csvfile:
        filewriter = csv.writer(csvfile)
        for i in range(10):
            filewriter.writerow([fake.name()])

        myFile = open(N_PATH, "r")
        print(myFile.read())
        myFile.close()

# Getting file attributes
def attributes():
    """Retrieving the file attributes of the specific file"""
    with os.scandir('data') as dir_contents:
        for entry in dir_contents:
            info = entry.stat()
            print(info.st_mtime)

# Time stamp for file
def convert_date(timestamp):
    """Retrieving the time stamp for the specific file"""
    d = datetime.utcfromtimestamp(timestamp)
    formated_date = d.strftime('%d %b %Y')
    return formated_date

# Displaying attributes of file
def get_files():
    dir_entries = scandir('data/')
    for entry in dir_entries:
        if entry.is_file():
            info = entry.stat()
            stat = os.stat(N_PATH)
            print(f'{entry.name}\t')
            print("\nSize: %s bytes" % stat.st_size)
            print("\nLast accessed: %s" % time.ctime(stat.st_atime))
            print(f'Last Modified: {convert_date(info.st_mtime)}')

# Get the file's permissions
def permission():
    """Displaying the read, write, and execute permissions of the file"""
    file_path = N_PATH
    permissions = os.stat(file_path).st_mode

    # Print the read and write permissions
    print('Read permission:', bool(permissions & 0o400))
    print('Write permission:', bool(permissions & 0o200))
    print('Execute permission:', bool(permissions & 0o100))

# Main function
if __name__ == '__main__':
    try:
        print("\nWelcome to the File System Program!")
        print("-------------------------------------")
        print("\nContent of the File System Program will be in the", sys.argv[0], "program")
        print("\nFirst, lets create the directory: ")

        create_dir()

        print("\nYou have successfully created the directory")

        print("\nNow, lets add a file into the directory: ")

        #file_size = os.stat(N_PATH).st_size
        #if file_size > 0:
         #   print('There is content inside this file')
        #else:
         #   print('There is no content inside this file')

        print("\nHere are the file contents: ")
        create_file()

        if os.path.exists("data/names.csv"):
            print("You have successfully added the file!")
        else:
            print("Does not exist")

        print("\nNow, we are going to display the attributes of the file")
        get_files()

        print("\nYou can display the read, write, and execute permissions of the file!")
        permission()

    except KeyboardInterrupt:
        sys.exit(0)


#
import random
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import gym
import gym.spaces
#
from apps.drl.c03.e02.app_config import AppConfig
from apps.drl.c03.e02.input_wrapper import InputWrapper
from apps.drl.c03.e02.discriminator import Discriminator
from apps.drl.c03.e02.generator import Generator

class C03E02(object):
    LEARNING_RATE = 0.0001
    REPORT_EVERY_ITER = 100
    SAVE_IMAGE_EVERY_ITER = 1000

    def __init__(self):
        self.name = 'apps.drl.c03.c03_e02.C03E02'
        self.logger = gym.logger
        self.logger.set_level(gym.logger.INFO)

    def startup(self, args={}):
        '''
        parser = argparse.ArgumentParser()
        parser.add_argument(
            "--cuda", default=False, action='store_true',
            help="Enable cuda computation")
        args = parser.parse_args()
        '''
        cudas = [torch.cuda.device(i) for i in range(torch.cuda.device_count())]
        for ci in range(len(cudas)):
            print(torch.cuda.get_device_name(ci))

        device = torch.device("cuda:0")
        envs = [
            InputWrapper(gym.make(name))
            for name in ('Breakout-v0', 'AirRaid-v0', 'Pong-v0')
        ]
        input_shape = envs[0].observation_space.shape

        net_discr = Discriminator(input_shape=input_shape).to(device)
        net_gener = Generator(output_shape=input_shape).to(device)

        objective = nn.BCELoss()
        gen_optimizer = optim.Adam(
            params=net_gener.parameters(), lr=C03E02.LEARNING_RATE,
            betas=(0.5, 0.999))
        dis_optimizer = optim.Adam(
            params=net_discr.parameters(), lr=C03E02.LEARNING_RATE,
            betas=(0.5, 0.999))

        gen_losses = []
        dis_losses = []
        iter_no = 0

        true_labels_v = torch.ones(AppConfig.BATCH_SIZE, device=device)
        fake_labels_v = torch.zeros(AppConfig.BATCH_SIZE, device=device)

        for batch_v in C03E02.iterate_batches(envs):
            # fake samples, input is 4D: batch, filters, x, y
            gen_input_v = torch.FloatTensor(
                AppConfig.BATCH_SIZE, Generator.LATENT_VECTOR_SIZE, 1, 1).to(device)
            gen_input_v.normal_(0, 1).to(device)
            batch_v = batch_v.to(device)
            gen_output_v = net_gener(gen_input_v)

            # train discriminator
            dis_optimizer.zero_grad()
            dis_output_true_v = net_discr(batch_v)
            dis_output_fake_v = net_discr(gen_output_v.detach())
            dis_loss = objective(dis_output_true_v, true_labels_v) + \
                    objective(dis_output_fake_v, fake_labels_v)
            dis_loss.backward()
            dis_optimizer.step()
            dis_losses.append(dis_loss.item())

            # train generator
            gen_optimizer.zero_grad()
            dis_output_v = net_discr(gen_output_v)
            gen_loss_v = objective(dis_output_v, true_labels_v)
            gen_loss_v.backward()
            gen_optimizer.step()
            gen_losses.append(gen_loss_v.item())

            iter_no += 1
            self.logger.info("Iter %d: gen_loss=%.3e, dis_loss=%.3e",
                        iter_no, gen_loss_v.item(),
                        dis_loss.item())
            if iter_no % C03E02.REPORT_EVERY_ITER == 0:
                self.logger.info("Iter %d: gen_loss=%.3e, dis_loss=%.3e",
                        iter_no, np.mean(gen_losses),
                        np.mean(dis_losses))
                gen_losses = []
                dis_losses = []
            if iter_no % C03E02.SAVE_IMAGE_EVERY_ITER == 0:
                self.logger.info('save...')

    @staticmethod
    def iterate_batches(envs, batch_size=AppConfig.BATCH_SIZE):
        batch = [e.reset() for e in envs]
        env_gen = iter(lambda: random.choice(envs), None)

        while True:
            e = next(env_gen)
            obs, reward, is_done, _ = e.step(e.action_space.sample())
            if np.mean(obs) > 0.01:
                batch.append(obs)
            if len(batch) == batch_size:
                # Normalising input between -1 to 1
                batch_np = np.array(batch, dtype=np.float32)
                batch_np *= 2.0 / 255.0 - 1.0
                yield torch.tensor(batch_np)
                batch.clear()
            if is_done:
                e.reset()


import os
import random
import cv2
import numpy as np
import sys
from create_model import U_NET_fullresolution

import matplotlib.pyplot as plt
# os.chdir("../../")
current_dir = os.path.dirname(os.path.abspath(__file__))

preprocessing_dir = os.path.join(current_dir, "../data_processing")

sys.path.append(preprocessing_dir)

from constansts import *

# os.chdir("../..") # need to be in root directory test_task if you then don't need this

def read_img(filename: str) -> np.ndarray:
    image = cv2.imread(filename)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    return np.float32(image / 255)

def select_random_files(n:int):
    file_list = []
    curr_dir = os.path.join(current_dir, IMAGES_PATH_TEST)
    entries = os.listdir(curr_dir)
    random_entries = random.sample(entries, n)

    for entry in random_entries:
        file_list.append(read_img(os.path.join(curr_dir, entry)))

    return np.stack(file_list)

def plot_helper(ax:any, indx:int, title_name:str):
    ax[indx].set_title(title_name, fontsize=14)
    ax[indx].axis('off')
    ax[indx].grid(False)

def check_results(number_of_examples:int):

    model = U_NET_fullresolution(1,True)
    model.load_weights("models/weights_unet.h5")

    test_imgs = select_random_files(number_of_examples)
    masks = model(test_imgs, training=False)

    for i, img in enumerate(test_imgs):
        fig, ax = plt.subplots(1, 2, figsize=(12, 8))
        fig.suptitle('Prediction results', fontsize=18)

        # plot orig image
        ax[0].imshow(img)
        plot_helper(ax, 0, "Image")

        # plot predicted mask
        mask = masks[i].numpy()
        ax[1].imshow(mask, 'binary_r')
        plot_helper(ax, 1, 'UNET mask')
        fig.tight_layout()

        plt.savefig(os.path.join(current_dir, f"results/res{i}.jpg"), bbox_inches='tight')

if __name__ == "__main__":
    check_results(10)


#!/usr/bin/env python3

import argparse
from requests import get
from tabulate import tabulate
from typing import Optional
from typing import Sequence
# Import json for reading the token file
from json import load

def get_profile_data(profile_url: str, token: str = ""):
    """
        A function to get user data\n
        args:\n
            \tprofile_url - URL for the users repo list\n
            \ttoken - personal access token
    """

    get_user_info = get(
        profile_url,
        headers={
            "Accept": "application/vnd.github.v3+json"
        },
        auth=(token, "x-oauth-basic")
    ).json()

    try:
        # Put the user information in a dict
        parsed_user_info = {
            "username": get_user_info["login"],
            "name": get_user_info["name"],
            "location": get_user_info["location"],
            "repos_url": get_user_info["repos_url"]
        }
    # if there is no data, there is either no user with that username
    # or the token is wrong/invalid - Then we exit with a message
    except KeyError:
        exit("Invalid username or token\n")

    return parsed_user_info

def get_repos_data(repo_url: str, token: str = "", no_forks: bool = False,
                   repo_url_query: str = ""):
    """
        A function to get the repo data\n
        args:\n
            \trepo_url - URL for the users repo list\n
            \ttoken - personal access token\n
            \tno_forks - boolean that determines whether or not we keep forks\n
            \trepo_url_query - URL query string that determines if we get pub/priv/both repos
    """

    get_repos_info = get(
        repo_url + "?{parameters}".format(parameters=repo_url_query),
        headers={
            "Accept": "application/vnd.github.v3+json"
        },
        auth=(token, "x-oauth-basic")
    ).json()

    parsed_repos = list()

    # loop through the repos
    for repo in get_repos_info:
        # check if the no-forks option is passed and if the repo is a fork
        if no_forks is True and repo["fork"] is True:
            continue
        else:
            # add the repo data we need to a list (for outputting as a table)
            parsed_repos.append({
                "name": repo["name"],
                "url": repo["html_url"],
                "stars": repo["stargazers_count"]
            })

    return parsed_repos

def my_profile_function(args: dict):
    """
        A function that handles the my-profile command\n
        args:\n
            \targs - the commandline arguments
    """

    token = args["token"]
    private = args["private"]
    public = args["public"]

    # Check if the private or public option is passed
    # and then set up the visibility query accordingly
    if private is True:
        visibility_param = "visibility=private"
    elif public is True:
        visibility_param = "visibility=public"
    else:
        visibility_param = "visibility=all"

    my_profile_data = get_profile_data(
        profile_url="https://api.github.com/user",
        token=token
    )
    my_profile_repo_data = get_repos_data(
        repo_url="https://api.github.com/user/repos",
        repo_url_query=visibility_param,
        no_forks=args["no_forks"],
        token=token
    )

    format_output(my_profile_data, my_profile_repo_data)

def profile_function(args: dict):
    """
        A function that handles the profile command\n
        args:\n
            \targs - the commandline arguments
    """

    username = args["username"]

    profile_data = get_profile_data(
        profile_url="https://api.github.com/users/{username}".format(
            username=username)
    )
    profile_repo_data = get_repos_data(
        repo_url=profile_data["repos_url"],
        no_forks=args["no_forks"]
    )

    format_output(profile_data, profile_repo_data)

def format_output(user_data: dict, repo_data: list):
    """
        A function that formats the data for the the output:\n
            \tusername: full name - location\n
            \t{table of repos}\n
        \n
        args:\n
            \tuser_data - parsed user information\n
            \trepo_data - parsed repo information\n
    """

    print(
        user_data["username"] + ":",
        user_data["name"], "-", user_data["location"]
    )

    # Check if there is any repo data
    try:
        table_header = repo_data[0].keys()
        table_rows = [row.values() for row in repo_data]
        print(tabulate(table_rows, table_header), "\n")
    # If there is no data exit with a message
    except IndexError:
        exit("No repo data found!\n")

def main(argv: Optional[Sequence[str]] = None) -> int:
    # Initiate the cl arguments parser and subparsers
    arg_parser = argparse.ArgumentParser(
        description="A CLI to get GitHub user & repo information.")
    subparsers = arg_parser.add_subparsers(
        dest="command", title="Commands", required=True)

    # Arguments for the my-profile command
    my_profile_parser = subparsers.add_parser(
        "my-profile", help="display information about the current user's profile")
    my_profile_parser.add_argument(
        "--token", help="GitHub personal access token", action="store")
    my_profile_parser.add_argument(
        "--no-forks", help="do not show forks", action="store_true")
    my_profile_parser.add_argument(
        "--private", help="show private repositories only", action="store_true")
    my_profile_parser.add_argument(
        "--public", help="show public repositories only", action="store_true")

    # Arguments for the profile command
    profile_parser = subparsers.add_parser(
        "profile", help="display information about a github user's profile")
    profile_parser.add_argument(
        "username", help="the username of the profile", action="store")
    profile_parser.add_argument(
        "--no-forks", help="do not show forks", action="store_true")

    args = arg_parser.parse_args(argv)

    # Check which command was passed
    if vars(args)["command"] == "my-profile":
        # If no token was passed check the config file
        if vars(args)["token"] is None:
            with open("github_config.json") as config_file:
                token = load(config_file)["public_access_token"]
                if len(token) <= 0:
                    # If no token is given in the config file raise an error
                    raise argparse.ArgumentTypeError(
                        "No token given, use --token or add a token to the config file.")
                else:
                    vars(args)["token"] = token

        my_profile_function(vars(args))
    else:
        profile_function(vars(args))

    return 0

if __name__ == "__main__":
    exit(main())


from tqdm import tqdm

n = 1000
ans = 1

flag = False

for a in tqdm(range(n)):
    for b in range(a + 1, n):
        for c in range(b + 1, n):
            if a + b + c == n:
                if a**2 + b**2 == c**2:
                    ans = a*b*c
                    print(f"The product of the special pythagorean triplet: {ans}")
                    print(a, b, c)
                    flag = True
                    break
        if flag:
            break
    if flag:
        break

# Find a better solution than brute force


#!/usr/bin/env python

from __future__ import print_function
import json
import os
from os.path import join, dirname, abspath
import subprocess
import sys

from get_text_from_url import process_page

def main(argv=None):
    if argv is None:
        argv = sys.argv
    arg = argv[1:]

    # Enter two  URLs with a space between them
    if len(arg) > 0:
        # Developers can supply URL as an argument...
        urls = arg[0]
    else:
        # ... but normally the URL comes from the allSettings.json file
        with open(os.path.expanduser("~/allSettings.json")) as settings_json:
            settings = json.load(settings_json)
            url1 = settings['source-url']
            url2 = settings['source-url2']
    assert url1 and url2, 'Two URLs not entered.'
    diff_urls(url1, url2)

def diff_urls(url1, url2):
    text1 = process_page('text_from_url1', url1)
    text2 = process_page('text_from_url2', url2)
    subprocess.check_output("./diff_text.sh", cwd=dirname(abspath(__file__)))

if __name__ == '__main__':
    main()


### REGRESSION - SIMPLE LINEAR

# =============================================================================
### Data Preprocessing ###

# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import dataset
dataset = pd.read_csv("Salary_Data.csv")
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, -1].values

# =!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
#
#  NOTE:
#
#   This passage should normally be deleted,
#   because it is not useful, we have no
#   missing data in this dataset, no
#   categorical data and no need for
#   do feature scalling, because we are
#   in an simple linear regression.
#
# =============================================================================
#
# Manage missing data
# from sklearn.impute import SimpleImputer
# imputer = SimpleImputer(missing_values = np.nan, strategy = "mean")
#
# imputer.fit(X[:, 1:3])
# X[:, 1:3] = imputer.transform(X[:, 1:3])
#
# Manage categoric variables
# from sklearn.preprocessing import LabelEncoder
# from sklearn.preprocessing import OneHotEncoder
#
# labelencoder_X = LabelEncoder()
# X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
# onehotencoder = OneHotEncoder(categorical_features = [0])
# X = onehotencoder.fit_transform(X).toarray()
#
# Feature scalling
# from sklearn.preprocessing import StandardScaler
# sc = StandardScaler()
# X_train = sc.fit_transform(X_train)
# X_test = sc.transform(X_test)
#
# =!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=

# Divid dataset between Training set & Test set
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 1.0/3, random_state = 0)
# =============================================================================

# =============================================================================
### Simple Linear Regression model ###

# Build Model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)

# Do new predictions
y_pred = regressor.predict(X_test)

# How to predict a new variable outside the scope of our dataset
regressor.predict([[15]])

# Visualize results
plt.scatter(X_test, Y_test, color = 'red') # Training set observation points.
plt.plot(X_train, regressor.predict(X_train), color = 'blue') # Prediction courbe of our simple linear regression.
plt.title('Salary vs Experience')
plt.xlabel('Experience')
plt.ylabel('Salary')
plt.show()
# =============================================================================


import torch
import os
from skimage.io import imread,imsave
import numpy as np
import matplotlib.pyplot as plt
from utils.visboundaries import visboundaries
from utils.colorize_notouchingsamecolor import colorize_notouchingsamecolor
import pickle
from utils.get_jacards_cell import get_jacards_cell,get_jacards_cell_with_fp

from utils.overlap_for_ap import overlap_for_ap

from scipy.ndimage  import distance_transform_edt
# from scipy.ndimage.measurements import label
from skimage.measure import label
from scipy.ndimage.morphology import binary_dilation,binary_closing,binary_erosion
from skimage.morphology import disk

data_path='../data_train_valid_test/test'

segmenter_names = ['boundary_line_0.6696171116230956.p' ,'loewke_1','loewke_2','ndt_0.6538656675812204.p','dt_0.6640225926514111.p',
                   'cell_border_0.562846843054472.p']

cell_types = ['PNT1A','PC3']

all_jacards=[]
all_binary_jacards=[]
all_aps = []

for segmenter_name in segmenter_names:

    for cell_type  in cell_types:

        save_folder = '../'  + segmenter_name.split('.')[0] + '/' + cell_type

        if not os.path.exists(save_folder):
            os.makedirs(save_folder)

        if not (segmenter_name=='loewke_1' or segmenter_name=='loewke_2'):
            segmenter = pickle.load( open( 'best_models/' +segmenter_name, "rb" ) )

        names=[]
        for root, dirs, files in os.walk(data_path):
            for name in files:
                if name.endswith("_img.tif") and name.find(cell_type)>0:
                    name=name.replace('_img.tif','')
                    names.append(root + os.sep +name)

        jacards=[]

        binary_gts=[]
        binary_res=[]

        for_aps_jac = []
        for_aps_fp = []
        for_aps_fn = []

        for name_num,name in enumerate(names):

            img=imread(name + "_img.tif")
            img0=img.astype(np.float32)
            img=img0.copy()

            mask=imread(name + "_mask.png")
            mask0=mask.copy()

            qqq=segmenter_name.split('.')[0]

            if qqq =='cell_border_0':
                labeled_array=mask

                mask_new=np.zeros(mask.shape)
                strel=disk(6).astype(np.bool)
                u=np.unique(labeled_array)
                u=u[u>0]
                for k in u:
                    cell=labeled_array==k
                    nucs=binary_erosion(cell,strel)
                    mask_new=mask_new+nucs
                mask=mask_new

            if qqq =='boundary_line_0':
                labeled_array = mask

                tmp=np.zeros(mask.shape,dtype=np.int)
                strel = disk(5) #np.ones((5,5),dtype=bool)
                N=int(np.max(labeled_array))
                for k in range(N):
                    tmp = tmp +  binary_dilation(labeled_array==(k+1),strel).astype(np.int)

                lines = tmp>1

                strel=disk(2).astype(np.bool)
                lines = binary_dilation(lines,strel)

                mask=lines

            if qqq =='dt_0':
                labeled_array=mask
                u=np.unique(labeled_array)
                u=u[u>0]
                mask_new=np.zeros(mask.shape)
                for k in u:
                    cell=labeled_array==k
                    dt=distance_transform_edt(cell)
                    mask_new=mask_new+dt/np.max(dt)
                mask=mask_new

            if qqq =='ndt_0':
                labeled_array=mask
                u=np.unique(labeled_array)
                u=u[u>0]

                mask_new=np.zeros(mask.shape)

                strel = disk(5) #np.ones((5,5),dtype=bool)
                tmp_mask=mask>0
                tmp_mask=binary_closing(tmp_mask,strel)

                for k in u:
                    cell=binary_dilation(labeled_array==k,strel)
                    other_cells=(mask>0)
                    other_cells[cell]=0
                    max_val=20
                    dt=distance_transform_edt(other_cells==0)
                    dt=max_val-dt
                    dt=dt*cell
                    dt[dt<0]=0
                    dt=dt/max_val
                    mask_new=mask_new+dt
                mask_new[tmp_mask==0]=0
                mask_new[mask_new>1]=1

                mask=mask_new

            if not (segmenter_name=='loewke_1' or segmenter_name=='loewke_2'):
                gt = mask.copy()
                mask = mask0.copy()

                shape=np.shape(img)
                img=torch.from_numpy(img.reshape((1,1,shape[0],shape[1])).astype(np.float32))

                imgs=segmenter.predict_imgs(img)
                tmp=segmenter.get_segmentation(imgs)

                img=img.detach().cpu().numpy()[0,0,:,:]

            elif segmenter_name=='loewke_1':
                mask = mask0.copy()
                gt = mask.copy()
                img = img.copy()
                imgs = [img,img]
                tmp = imread('../loewke_res/' + name.split('\\')[-1] + '_img.png')

                tmp=label(tmp>0,connectivity=1)

            elif segmenter_name=='loewke_2' :
                mask = mask0.copy()
                gt = mask.copy()
                img = img.copy()
                imgs = [img,img]
                tmp = imread('../loewke_res/' + name.split('\\')[-1] + '_img.png')

                tmp=label(tmp>0,connectivity=2)

            imsave(save_folder+'/img_'+str(name_num).zfill(5) + '.tif', img)
            imsave(save_folder+'/gt_'+str(name_num).zfill(5) + '.tif', mask)
            imsave(save_folder+'/whatpredict_'+str(name_num).zfill(5) + '.tif', gt.astype(np.int))
            imsave(save_folder+'/whatpredicted_'+str(name_num).zfill(5) + '.tif', imgs[0])
            imsave(save_folder+'/whatpredictedfg_'+str(name_num).zfill(5) + '.tif', imgs[1].astype(np.float32))
            imsave(save_folder+'/result_'+str(name_num).zfill(5) + '.tif', tmp)

            jacards=jacards+get_jacards_cell_with_fp(mask,tmp)

            binary_gts.append(mask)
            binary_res.append(tmp)

            aps_jac,aps_fp,aps_fn = overlap_for_ap(mask,tmp)

            for_aps_jac.append(aps_jac)
            for_aps_fp.append(aps_fp)
            for_aps_fn.append(aps_fn)

            colormap_cells=[[1,0,0],[0,1,0],[0,0,1],[0.8314,0.8314,0.0588],[1,0,1],[1,0.5,0],[0.00,1.00,1.00],[0.45,0.00,0.08]]

            L=colorize_notouchingsamecolor(tmp>0)

            plt.figure(figsize=(10,20))
            plt.subplot(121)
            plt.imshow(img)
            for k,c in enumerate(colormap_cells):
                visboundaries(L==(k+1),color=c)

            plt.subplot(122)
            plt.imshow((mask0>0).astype(np.int),'gray',vmin=-1,vmax=2)
            for k,c in enumerate(colormap_cells):
                visboundaries(L==(k+1),color=c)
            plt.savefig(save_folder+'/example_'+str(name_num).zfill(5) + '.png')
            plt.show()

        mean_jacard=np.mean(jacards)
        all_jacards.append(mean_jacard)
        print(mean_jacard)

        tmp_gt = np.stack(binary_gts,axis=2)>0
        tmp_res = np.stack(binary_res,axis=2)>0
        mean_binary_jacard = np.sum(tmp_gt & tmp_res)/np.sum(tmp_gt | tmp_res)
        all_binary_jacards.append(mean_binary_jacard)
        print(mean_binary_jacard)

        jacards_ap = np.concatenate(for_aps_jac,axis=0)
        fp = np.sum(for_aps_fp)
        fn = np.sum(for_aps_fn)

        aps=[]
        for t in np.linspace(0.5,1,100):

            tp_tmp = np.sum(jacards_ap>=t)

            fp_tmp = fp + np.sum(jacards_ap<t)

            fn_tmp = fn + np.sum(jacards_ap<t)

            ap = tp_tmp / (tp_tmp+fp_tmp+fn_tmp)

            aps.append(ap)

        all_aps.append(aps)

np.save('all_jacards.npy', all_jacards)
np.save('all_binary_jacards.npy', all_binary_jacards)
np.save('all_aps.npy', all_aps)

metrics = ['DAP','BIoU','OIoU']

index = -1
all_types_res = [np.zeros((len(segmenter_names),len(metrics))),np.zeros((len(segmenter_names),len(metrics)))]
for segmenter_num,segmenter_name in enumerate(segmenter_names):
    for cell_type_num,cell_type in enumerate(cell_types):

        index = index+1
        for metric_num,metric in enumerate(metrics):
            if metric == 'DAP':
                number = all_aps[index][0]
            if metric == 'BIoU':
                number = all_binary_jacards[index]
            if metric == 'OIoU':
                number = all_jacards[index]

            all_types_res[cell_type_num][segmenter_num,metric_num] = number

all_types_res = np.concatenate(all_types_res,axis=1)

all_types_res = all_types_res[[1,4,3,0,5],:]

# segmenter_names = ['boundary_line_0.6696171116230956.p' ,'loewke_1','loewke_2','ndt_0.6538656675812204.p','dt_0.6640225926514111.p',
#                    'cell_border_0.562846843054472.p']


import io
import logging
import time
import traceback
from datetime import datetime

import aiohttp
import discord
from redbot.core import checks
from redbot.core import commands
from redbot.core.utils.chat_formatting import inline

from rpadutils import CogSettings, box

log = logging.getLogger("red.admin")

# Three hour cooldown
ATTRIBUTION_TIME_SECONDS = 60 * 60 * 3

class ChannelMod(commands.Cog):
    """Channel moderation tools."""

    def __init__(self, bot, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.bot = bot
        self.settings = ChannelModSettings("channelmod")
        self.channel_last_spoke = {}

    @commands.group()
    @checks.is_owner()
    async def channelmod(self, ctx):
        """Manage channel moderation settings"""

    @channelmod.command()
    async def addmirror(self, ctx, source_channel_id: int, dest_channel_id: int, docheck: bool = True):
        """Set mirroring between two channels."""
        if docheck and (not self.bot.get_channel(source_channel_id) or not self.bot.get_channel(dest_channel_id)):
            await ctx.send(inline('Check your channel IDs, or maybe the bot is not in those servers'))
            return
        self.settings.add_mirrored_channel(source_channel_id, dest_channel_id)
        await ctx.send(inline('Done'))

    @channelmod.command()
    @checks.is_owner()
    async def rmmirror(self, ctx, source_channel_id: int, dest_channel_id: int, docheck: bool = True):
        """Remove mirroring between two channels."""
        if docheck and (not self.bot.get_channel(source_channel_id) or not self.bot.get_channel(dest_channel_id)):
            await ctx.send(inline('Check your channel IDs, or maybe the bot is not in those servers'))
            return
        self.settings.rm_mirrored_channel(source_channel_id, dest_channel_id)
        await ctx.send(inline('Done'))

    @channelmod.command()
    @checks.is_owner()
    async def mirrorconfig(self, ctx):
        """List mirror config."""
        mirrored_channels = self.settings.mirrored_channels()
        msg = 'Mirrored channels\n'
        for mc_id, config in mirrored_channels.items():
            channel = self.bot.get_channel(mc_id)
            channel_name = channel.name if channel else 'unknown'
            msg += '\n{} ({})'.format(mc_id, channel_name)
            for channel_id in config['channels']:
                channel = self.bot.get_channel(channel_id)
                channel_name = channel.name if channel else 'unknown'
                msg += '\n\t{} ({})'.format(channel_id, channel_name)
        await ctx.send(box(msg))

    @commands.Cog.listener('on_message')
    async def mirror_msg_new(self, message):
        if message.author.id == self.bot.user.id or isinstance(message.channel, discord.abc.PrivateChannel):
            return

        channel = message.channel
        mirrored_channels = self.settings.get_mirrored_channels(channel.id)

        if not mirrored_channels:
            return

        last_spoke, last_spoke_timestamp = self.settings.get_last_spoke(channel.id)
        now_time = datetime.utcnow()
        last_spoke_time = datetime.utcfromtimestamp(
            last_spoke_timestamp) if last_spoke_timestamp else now_time
        attribution_required = last_spoke != message.author.id
        attribution_required |= (now_time - last_spoke_time).total_seconds() > ATTRIBUTION_TIME_SECONDS
        self.settings.set_last_spoke(channel.id, message.author.id)

        attachment_bytes = None
        filename = None
        if message.attachments:
            # If we know we're copying a message and that message has an attachment,
            # pre download it and reuse it for every upload.
            attachment = message.attachments[0]
            if hasattr(attachment, 'url') and hasattr(attachment, 'filename'):
                url = attachment.url
                filename = attachment.filename
                async with aiohttp.ClientSession() as session:
                    async with session.get(url) as response:
                        attachment_bytes = io.BytesIO(await response.read())

        for dest_channel_id in mirrored_channels:
            try:
                dest_channel = self.bot.get_channel(dest_channel_id)
                if not dest_channel:
                    continue

                if attribution_required:
                    msg = 'Posted by **{}** in *{} - #{}*:'.format(message.author.name,
                                                                   message.guild.name,
                                                                   message.channel.name)
                    await dest_channel.send(msg)

                if attachment_bytes and filename:
                    dest_message = await dest_channel.send(file=discord.File(attachment_bytes, filename),
                                                           content=message.content)
                    attachment_bytes.seek(0)
                elif message.content:
                    dest_message = await dest_channel.send(message.content)
                else:
                    print('Failed to mirror message from ', channel.id, 'no action to take')
                    continue

                self.settings.add_mirrored_message(
                    channel.id, message.id, dest_channel.id, dest_message.id)
            except Exception as ex:
                print('Failed to mirror message from ', channel.id, 'to', dest_channel_id, ':', ex)
                traceback.print_exc()

        if attachment_bytes:
            attachment_bytes.close()

    @commands.Cog.listener('on_message_edit')
    async def mirror_msg_edit(self, message, new_message):
        await self.mirror_msg_mod(message, new_message_content=new_message.content)

    @commands.Cog.listener('on_message_delete')
    async def mirror_msg_delete(self, message):
        await self.mirror_msg_mod(message, delete_message_content=True)

    @commands.Cog.listener('on_reaction_add')
    async def mirror_reaction_add(self, reaction, user):
        message = reaction.message
        if message.author.id != user.id:
            return
        await self.mirror_msg_mod(message, new_message_reaction=reaction.emoji)

    @commands.Cog.listener('on_reaction_remove')
    async def mirror_reaction_remove(self, reaction, user):
        message = reaction.message
        if message.author.id != user.id:
            return
        await self.mirror_msg_mod(message, delete_message_reaction=reaction.emoji)

    async def mirror_msg_mod(self, message,
                             new_message_content: str = None,
                             delete_message_content: bool = False,
                             new_message_reaction=None,
                             delete_message_reaction=None):
        if message.author.id == self.bot.user.id or isinstance(message.channel, discord.abc.PrivateChannel):
            return

        channel = message.channel
        mirrored_messages = self.settings.get_mirrored_messages(channel.id, message.id)
        for (dest_channel_id, dest_message_id) in mirrored_messages:
            try:
                dest_channel = self.bot.get_channel(dest_channel_id)
                if not dest_channel:
                    print('could not locate channel to mod')
                    continue
                dest_message = await dest_channel.fetch_message(dest_message_id)
                if not dest_message:
                    print('could not locate message to mod')
                    continue

                if new_message_content:
                    await dest_message.edit(content=new_message_content)
                elif new_message_reaction:
                    await dest_message.add_reaction(new_message_reaction)
                elif delete_message_content:
                    await dest_message.delete()
                elif delete_message_reaction:
                    await dest_message.remove_reaction(delete_message_reaction, dest_message.guild.me)
            except Exception as ex:
                print('Failed to mirror message edit from ',
                      channel.id, 'to', dest_channel_id, ':', ex)

class ChannelModSettings(CogSettings):
    def make_default_settings(self):
        config = {
            'servers': {},
            'mirrored_channels': {},
            'max_mirrored_messages': 20,
        }
        return config

    def servers(self):
        return self.bot_settings['servers']

    def get_server(self, server_id: int):
        servers = self.servers()
        if server_id not in servers:
            servers[server_id] = {}
        return servers[server_id]

    def max_mirrored_messages(self):
        return self.bot_settings['max_mirrored_messages']

    def mirrored_channels(self):
        # Mirrored channels looks like:
        #  <source_channel_id>: {
        #    'last_spoke: '<user_id>',
        #    'channels': [dest_channel_id_1, dest_channel_id2],
        #    'messages': {
        #      <source_msg_id>: [
        #         (dest_channel_id, dest_channel_msg],
        #      ]
        #    }
        #  }
        return self.bot_settings['mirrored_channels']

    def get_mirrored_channels(self, source_channel: str):
        return self.mirrored_channels().get(source_channel, {}).get('channels', [])

    def get_last_spoke(self, source_channel: str):
        spoke_values = self.mirrored_channels().get(source_channel, {})
        return spoke_values.get('last_spoke', None), spoke_values.get('last_spoke_timestamp', None)

    def set_last_spoke(self, source_channel: str, last_spoke: str):
        spoke_values = self.mirrored_channels().get(source_channel)
        spoke_values['last_spoke'] = last_spoke
        spoke_values['last_spoke_timestamp'] = time.time()
        self.save_settings()

    def add_mirrored_channel(self, source_channel: int, dest_channel: int):
        channels = self.mirrored_channels()
        if source_channel == dest_channel:
            raise commands.UserFeedbackCheckFailure('Cannot mirror a channel to itself')
        if dest_channel in channels:
            raise commands.UserFeedbackCheckFailure('Destination channel is already a source channel')
        if source_channel not in channels:
            channels[source_channel] = {
                'channels': [],
                'messages': {},
            }
        channels[source_channel]['channels'].append(dest_channel)
        self.save_settings()

    def rm_mirrored_channel(self, source_channel: int, dest_channel: int):
        channels = self.mirrored_channels()
        config = channels.get(source_channel)
        if config and dest_channel in config['channels']:
            dest_channel_config = config['channels']
            dest_channel_config.remove(dest_channel)
            if not dest_channel_config:
                channels.pop(source_channel)
            self.save_settings()

    def add_mirrored_message(self, source_channel: int, source_message: str, dest_channel: int, dest_message: str):
        channel_config = self.mirrored_channels()[source_channel]
        messages = channel_config['messages']
        if source_message not in messages:
            messages[source_message] = []

        targets = messages[source_message]
        new_entry = [dest_channel, dest_message]
        if new_entry not in targets:
            targets.append(new_entry)
            self.save_settings()

        if len(messages) > self.max_mirrored_messages():
            oldest_msg = min(messages.keys())
            messages.pop(oldest_msg)
            self.save_settings()

    def get_mirrored_messages(self, source_channel: int, source_message: str):
        """Returns either None or [(channel_id, message_id), ...]"""
        channel_config = self.mirrored_channels().get(source_channel, None)
        if channel_config:
            return channel_config['messages'].get(source_message, [])
        else:
            return []