#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 20 21:28:06 2022

@author: Maurice
"""
import os
import sys
import csv
import subprocess
from Bio import SeqIO

def check_dependancy(programs=[], rScript='', logfile=None):
    log = open(logfile, 'a')
    # the argument programs is a list of dependancies
    check = 'Checking dependancies:\n'
    log.write(check +'\n')
    print(check)
    missingPrograms = []

    # Check Biopython
    if 'Biopython' in programs:
        try:
            from Bio import SeqIO
            check = f' Biopython: OK ---'
            log.write(check +'\n')
            print(check)
        except ModuleNotFoundError:
            missingPrograms.append('Biopython')
            check =' Biopython: Module not found'
            log.write(check +'\n')
            print(check)

    # Check for system programs
    for program in programs:
        if not program == 'Biopython':
            track = subprocess.run(['which', program], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            #path = track.stdout.strip().decode('utf-8')
            if track.returncode != 0:
                check = f' {program}: Executable not found'
                log.write(check +'\n')
                print(check)
                missingPrograms.append(program)
            else:
                check = f' {program}: OK ---'
                log.write(check +'\n')
                print(check)

    # check for R libraries
    if not rScript == '':
        results = subprocess.run(['Rscript', rScript], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in results.stdout.decode('utf-8').strip().split('\n'):
            log.write(line + '\n')
            print(line)
            if 'Library not found' in line:
                lib = line.split(':')[0]
                missingPrograms.append(lib)
    log.close()
    return missingPrograms

class genViz:
    def __init__(self, outDir='None', outFileBase='None', fastq1='None',
                 fastq2='None', unpairedFastq='None', ref='None', gff='None', cpus=2, zs='None', ze='None', logfile='None'):
        self.outDir = outDir
        self.outFileBase = outFileBase
        self.fastq1 = fastq1
        self.fastq2 = fastq2
        self.unpairedFastq = unpairedFastq
        self.ref = ref
        self.gff = gff
        self.cpus = cpus
        self.zs = zs
        self.ze = ze
        seqRecord = SeqIO.read(self.ref, 'fasta')
        self.refLen = len(seqRecord.seq)
        self.log = logfile

    def __str__(self):

        # params = [
        # f'\nParameters to be used:\n',
        # f' Basename: {self.outFileBase}',
        # f' OutDir: {os.path.abspath(self.outDir)}',
        # f' First read file: {os.path.abspath(self.fastq1)}',
        # f' Second read file: {os.path.abspath(self.fastq2)}',
        # f' Unpared reads file: {os.path.abspath(self.unpairedFastq)}',
        # f' Reference genome: {os.path.abspath(self.ref)}',
        # f' Interval file: {os.path.abspath(self.gff)}',
        # f' Zoom will start at: {self.zs}',
        # f' Zoom will end at: {self.ze}',
        # f' Threads to be used: {self.cpus}'
        # ]

        params = [
        f'\nParameters to be used:\n',
        f' Basename: {self.outFileBase}',
        f' OutDir: {os.path.abspath(self.outDir)}',
        f' First read file: {self.fastq1}',
        f' Second read file: {self.fastq2}',
        f' Unpared reads file: {self.unpairedFastq}',
        f' Reference genome: {os.path.abspath(self.ref)}',
        f' Interval file: {os.path.abspath(self.gff)}',
        f' Zoom will start at: {self.zs}',
        f' Zoom will end at: {self.ze}',
        f' Threads to be used: {self.cpus}'
        ]

        with open (self.log, 'a') as log:
            for param in params:
                log.write(param + '\n')
                print(param)

        return ''

    def trimmer(self):
        with open (self.log, 'a') as log:
            message = '\n====== Trimming raw reads for quality control ======\n'
            log.write(message +'\n')
            print(message)
            newReadsFolder = os.path.join(self.outDir, 'trimmed_reads')
            if not os.path.exists(newReadsFolder):
                os.makedirs(newReadsFolder)

            if self.unpairedFastq != 'None':
                base = os.path.basename(self.unpairedFastq)
                newRead = os.path.join(newReadsFolder, f'trimmed_{base}')
                cmd = f'fastp -i {self.unpairedFastq} -o {newRead} -w {self.cpus} -j /dev/null -h /dev/null -q 20 -u 40 -l 15 -z 4'
                newRead1 = newRead
                newRead2 = 'None'
            else:
                base1 = os.path.basename(self.fastq1)
                base2 = os.path.basename(self.fastq2)
                newRead1 = os.path.join(newReadsFolder, f'trimmed_{base1}')
                newRead2 = os.path.join(newReadsFolder, f'trimmed_{base2}')
                cmd = f'fastp -i {self.fastq1} -I {self.fastq2} -o {newRead1} -O {newRead2} -w {self.cpus} -j /dev/null -h /dev/null -q 20 -u 40 -l 15 -z 4'
            if not os.path.exists(newRead1) and not os.path.exists(newRead2):
                execute = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                for line in execute.stdout.decode('utf-8').strip().split('\n'):
                    log.write(line +'\n')

                if execute.returncode != 0:
                    print("\nFailed to trim reads for quality with fastp. Check the log file for more details.\n")
                    sys.exit()

            else:
                message = '\n=============== TRIMMED READS EXIST ALREADY ========== SKIPPING!!!'
                log.write(message +'\n')
                print(message)
            outmessage = [
            '\nDone Trimming:\n',
            f'\t---> Trimmed read1: {newRead1}',
            f'\t---> Trimmed read2: {newRead2}\n',
            ]
            for message in outmessage:
                log.write(message + '\n')
                print(message)
            return [newRead1, newRead2]

    def mapper(self, read1=None, read2=None, mapper='bwa', rmdup=True):
        with open (self.log, 'a') as log:
            message1 = '\n====== Aligning reads to the reference genome ======\n'
            message2 = f'\t---> Reference length: {self.refLen:,}'
            log.write(message1 + '\n')
            print(message1)
            log.write(message2 + '\n')
            print(message2)
            rawBam = os.path.join(self.outDir, self.outFileBase + '_raw.bam')
            outBam = os.path.join(self.outDir, self.outFileBase + '_dedupped.bam')  # PCR duplicates removed

            if mapper == 'bwa':
                cmd1 = f'bwa index {self.ref}'
                if self.unpairedFastq != 'None':
                    cmd2 = f'bwa mem -t {self.cpus} {self.ref} {read1} | samtools view -b | samtools sort -@{self.cpus} > {rawBam}'
                else:
                    cmd2 = f'bwa mem -t {self.cpus} {self.ref} {read1} {read2} | samtools view -b | samtools sort -@{self.cpus} > {rawBam}'

                if not os.path.exists(self.ref + '.pac'):
                    message = '\t---> Indexing the reference genome'
                    log.write(message + '\n')
                    print(message)

                    execute = subprocess.run(cmd1.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                    for line in execute.stdout.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute.returncode != 0:
                        print("\nFailed to index the reference. Check the log file for more details.\n")
                        sys.exit()

            elif mapper == 'bowtie2':
                cmd1 = f'bowtie2-build {self.ref} {self.ref}'
                if self.unpairedFastq != 'None':
                    cmd2 = f'bowtie2 -p {self.cpus} -x {self.ref} -U {read1} | samtools view -b | samtools sort -@{self.cpus} > {rawBam}'
                else:
                    cmd2 = f'bowtie2 -p {self.cpus} -x {self.ref} -1 {read1} -2 {read2} | samtools view -b | samtools sort -@{self.cpus} > {rawBam}'

                if not os.path.exists(self.ref + '.1.bt2'):
                    message = '\t---> Indexing the reference genome'
                    log.write(message + '\n')
                    print(message)

                    execute = subprocess.run(cmd1.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                    for line in execute.stdout.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute.returncode != 0:
                        print("\nFailed to index the reference. Check the log file for more details.\n")
                        sys.exit()

            elif mapper == 'bbmap.sh':
                rawSam = os.path.join(self.outDir, self.outFileBase + '_raw.sam')
                if self.unpairedFastq != None:
                    cmd2a = f'bbmap.sh threads={self.cpus} ref={self.ref} in={read1} out={rawSam} nodisk'
                else:
                    cmd2a = f'bbmap.sh threads={self.cpus} ref={self.ref} in={read1} in2={read2} out={rawSam} nodisk'
                cmd2b = f'samtools view -b {rawSam} | samtools sort -@{self.cpus} > {rawBam}'

            cmd3 = f'samtools rmdup {rawBam} {outBam}'

            if not os.path.exists(outBam):
                message1 = f'\t---> Aligning reads with {mapper}'
                log.write(message1 + '\n')
                print(message1)

                if mapper == 'bbmap.sh':

                    execute2a = subprocess.run(cmd2a, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                    for line in execute2a.stdout.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute2a.returncode != 0:
                        print("\nbbmap failed. Check the log file for more details.\n")
                        sys.exit()

                    execute2b = subprocess.run(cmd2b, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                    for line in execute2b.stdout.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute2b.returncode != 0:
                        print("\nSamtools failed. Check the log file for more details.\n")
                        sys.exit()
                    os.remove(rawSam)
                else:

                    execute2 = subprocess.run(cmd2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                    for line in execute2.stdout.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute2.returncode != 0:
                        print(f"\n{mapper} failed. Check the log file for more details.\n")
                        sys.exit()

                message2 = '\t---> Removing PCR and optical duplicates'
                log.write(message2 + '\n')
                print(message2)

                execute3 = subprocess.run(cmd3.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                for line in execute3.stdout.decode('utf-8').strip().split('\n'):
                    log.write(line + '\n')

                if execute3.returncode != 0:
                    print("\nSamtools failed. Check the log file for more details.\n")
                    sys.exit()

            else:
                message1 = '\n====== ALIGNMENT FILE EXISTS ALREADY ======== SKIPPING!!!'
                log.write(message1 + '\n')
                print(message1)

            if rmdup == False:
                dupInfo = '\nThe option to remove duplicates was turned off. The raw alignment file will be used. \n'
                log.write(dupInfo + '\n')
                print(dupInfo)

                outBam = rawBam

            outinfo = ['\nDone with mapping:\n', f'\t---> Alignment file: {outBam}']

            for info in outinfo:
                log.write(info + '\n')
                print(info)

        return outBam

    def makeBed(self, inBam):
        with open (self.log, 'a') as log:
            inmessage = '\n====== Making a coverage bedGraph file from the alignment file ======\n'
            log.write(inmessage + '\n')
            print(inmessage)

            bedGraph = os.path.join(self.outDir, f'{self.outFileBase}.bedGraph')
            if not os.path.exists(bedGraph):
                cmd = f'bedtools genomecov -ibam {inBam} -bga'
                with open(bedGraph, 'w') as bed:
                    execute = subprocess.run(cmd.split(), stdout=bed, stderr=subprocess.PIPE)
                    for line in execute.stderr.decode('utf-8').strip().split('\n'):
                        log.write(line + '\n')

                    if execute.returncode != 0:
                        print("\nFailed to make the coverage file from thre bam file. Check the log file for more details.\n")
                        sys.exit()
            else:
                message = '\n====== BEDGRAPH FILE EXISTS ALREADY ======== SKIPPING!!!\n'
                log.write(message + '\n')
                print(message)

            outmessage = f'\t---> bedGraph file: {bedGraph}'
            log.write(outmessage + '\n')
            print(outmessage)

            with open(bedGraph, 'r') as bed:
                chrom = bed.readline().split('\t')[0]
            return [chrom, os.path.abspath(bedGraph)]

    def collectVarNames(self, values):  # values is a list of values
        outFile = os.path.join(self.outDir, self.outFileBase + '_values.csv')
        with open(outFile, 'w') as vals:
            writer = csv.writer(vals, dialect=csv.excel_tab)
            writer.writerow(['sample', 'out_dir', 'virus', 're_fasta', 'gff', 'zoomStart', 'zoomEnd', 'seqEnd', 'bam', 'bedfile', 'chromosome'])
            writer.writerow(values)
        return(outFile)

    def generateGraphs(self, rScript, valueFile):
        with open (self.log, 'a') as log:
            inmessage = '\n====== Generating coverage graphs ======\n'
            log.write(inmessage + '\n')
            print(inmessage)

            execute = subprocess.run(['Rscript', rScript, valueFile], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

            for line in execute.stdout.decode('utf-8').strip().split('\n'):
                log.write(line + '\n')

            if execute.returncode != 0:
                print("\nFailed to produce the coverage images. Check the log file for more details.\n")
                sys.exit()
            else:
                message = 'The following alignment graphs were generated:\n'
                log.write(message + '\n')
                print(message)

                file1 = os.path.join(self.outDir, f'graphs/{self.outFileBase}_full_coverage.png')
                # file2 = os.path.join(self.outDir, f'graphs/{self.outFileBase}_zoom_{self.zs}-{self.ze}_coverage.png')
                # file3 = os.path.join(self.outDir, f'graphs/{self.outFileBase}_full_alignment.png')
                # file4 = os.path.join(self.outDir, f'graphs/{self.outFileBase}_zoom_{self.zs}-{self.ze}_alignment.png')
                for file in [file1]:#, file2]:#, file4]:
                    if os.path.exists(file):
                        outmessage = f'\t---> {file}'
                        log.write(outmessage + '\n')
                        print(outmessage)
                    else:
                        log.write(f'Graph {os.path.basename(file)} was not generated! Something is wrong with input files\n')
                        print(f'Graph {os.path.basename(file)} was not generated! Check log file for more details')
                print('\n')


#!/usr/local/bin//python3

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf

matplotlib.use("MacOSX", warn=False, force=True)

def plot_dataset(X, y, axes):
    plt.plot(X[:, 0][y == 0], X[:, 1][y == 0], "bs")
    plt.plot(X[:, 0][y == 1], X[:, 1][y == 1], "g^")
    plt.axis(axes)
    plt.grid(True, which='both')
    plt.xlabel(r"$x_1$", fontsize=20)
    plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
    plt.legend()

def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

def fetch_batch(X, y, epoch, batch_index, batch_size=100):
    n = X.shape[0]
    # print("n = ".format(n))
    n_batches = int(np.ceil(n / batch_size))
    np.random.seed(epoch * n_batches + batch_index)
    indices = np.random.randint(n, size=batch_size)
    indices
    X_batch = X[indices]
    y_batch = y.reshape(-1, 1)[indices]
    return X_batch, y_batch

def main():
    #############
    # Load data
    #############
    from sklearn.datasets import make_moons
    X_moon, y_moon = make_moons(n_samples=100, noise=0.15, random_state=42)
    X_dtype = X_moon.dtype  # to coerce y to same dtype (float64)
    # X_moon_bias = np.c_[np.ones((n, 1)), X_moon]  # done by poly
    print("Loaded {} points".format(X_moon.shape[0]))

    # plot data
    print("Plotting dataset")
    plot_dataset(X_moon, y_moon, [-1.5, 2.5, -1, 1.5])
    plt.show()

    y_moon = y_moon.reshape(-1, 1).astype('float64')

    # Add polynomial features
    from sklearn.preprocessing import PolynomialFeatures
    pf = PolynomialFeatures(degree=4,
                            interaction_only=False,
                            include_bias=True)
    X_moon_bias = pf.fit_transform(X_moon, y=None)
    n, p = X_moon_bias.shape

    ##########
    # training
    ##########

    # logging
    from datetime import datetime
    now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
    root_logdir = "tf_logs"
    logdir = "{}/run-{}/".format(root_logdir, now)

    # train/ test split
    ratio = 0.8
    train_indices = int(ratio * n)
    X_train = X_moon_bias[:train_indices]
    y_train = y_moon[:train_indices]
    X_test = X_moon_bias[train_indices:]
    y_test = y_moon[train_indices:]

    # parameters
    reset_graph()
    n_epochs = 5000
    learning_rate = .01
    batch_size = 10
    n_batches = int(x=np.ceil(n / batch_size))

    X = tf.placeholder(dtype=X_dtype, shape=(None, p), name="X")
    y = tf.placeholder(dtype=X_dtype, shape=(None, 1), name="y")
    y.dtype
    theta = tf.Variable(tf.random_uniform([p, 1], -1, 1,
                                          dtype=X_dtype, seed=42),
                        name="theta", dtype=X_dtype)
    logits = tf.matmul(X, theta, name="logits")
    y_proba = tf.sigmoid(logits)
    loss = tf.losses.log_loss(y, y_proba)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    training_op = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    loss_summary = tf.summary.scalar('Loss', loss)
    file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(init, feed_dict=None, run_metadata=None)
        for epoch in range(n_epochs):
            for batch_index in range(n_batches):
                X_batch, y_batch = fetch_batch(X_train, y_train, epoch,
                                               batch_index, batch_size)
                feed_dict = {X: X_batch, y: y_batch}
                if batch_index % 10 == 0:
                    summary_str = loss_summary.eval(feed_dict=feed_dict)
                    step = epoch * n_batches + batch_index
                    file_writer.add_summary(summary_str, global_step=step)
                sess.run(training_op,
                         feed_dict=feed_dict,
                         run_metadata=tf.RunMetadata())
            if epoch % 100 == 0:
                print("Epoch: {}, Logloss: {}".format(
                    epoch,
                    loss.eval(feed_dict={X: X_moon_bias, y: y_moon})))

        y_proba_val = y_proba.eval(feed_dict={X: X_test, y: y_test})
        best_theta = theta.eval()
        save_path = saver.save(sess, "saved_models/moon_regression.ckpt")

    print("Best theta = {}".format(best_theta), end='\n')

    #########
    # Results
    #########

    y_pred = (y_proba_val >= 0.5)
    from sklearn.metrics import precision_score, recall_score  # noqa
    precision = precision_score(y_test, y_pred)
    print("precision: {}".format(precision))
    recall = recall_score(y_test, y_pred)
    print("recall: {}".format(recall))

    # plot results
    y_pred_idx = y_pred.reshape(-1)  # a 1D array rather than a column vector
    plt.plot(X_test[y_pred_idx, 1],
             X_test[y_pred_idx, 2], 'go', label="Positive")
    plt.plot(X_test[~y_pred_idx, 1],
             X_test[~y_pred_idx, 2], 'r^', label="Negative")
    plt.legend()
    plt.show()

    #########
    # restore
    #########
    # with tf.Session() as sess:
    #     saver.restore(sess, save_path)
    #     best_theta_restored = theta.eval()
    #     print("best_theta_restored: {}".format(best_theta_restored))

if __name__ == "__main__":
    main()


import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

padding = 1.5
leftMargin = 0.1
rightMargin = 0.1
upMargin = 0.5
bottomMargin = 0.5
def adjustLegendPadding(plt,ax, scaling = 1.5):
    plt.tight_layout(pad = padding * scaling)
    chartBox = ax.get_position()
    ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.7, chartBox.height])
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), shadow=True, ncol=1, prop = {"size":6})
    params = {'legend.fontsize': 5,
            'legend.handlelength': 2}
    plt.rcParams.update(params)

def angularPowerAxisLabel(plt,ax, scaled = False):
    """
        used to scale the axis

    Parameters:
    -----------------

    scaled: boolean
        default False, if True, will extra times 4 Pi / N^2
    """
    plt.xlabel("$\ell$")
    if (scaled == False):
        plt.ylabel("$(2\ell+1)C_{\ell}$")
    else:
        # plt.ylabel(r'$\frac{4\pi(2\ell+1)C_{\ell}}{N^2}')
        # plt.tight_layout(pad = padding * 2)
        plt.ylabel(r"$\frac{4 \pi(2\ell+1)C_{\ell}}{N^2}$", labelpad = -1)

    plt.grid()

def correlationLabel(plt,ax):
    print("currently not supported")
    pass


import img_edit
import img_search
import rhymer

import random
import sys
import os
import time
import tweepy

def file2list(file_name):
    with open(file_name, 'r') as f:
        return f.read().split("\n")

"""
Main
"""
if __name__ == "__main__":

    creds = file2list("creds.txt")

    CONSUMER_KEY = creds[0]
    CONSUMER_SECRET = creds[1]
    ACCESS_KEY = creds[2]
    ACCESS_SECRET = creds[3]
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)

    # bot loop
    while True:
        # do the meme
        query_list = file2list("query_list.txt")
        query = query_list[random.randint(0, len(query_list)-1)]
        nouns = rhymer.make_sentence(query)

        if len(nouns) <= 0:
            continue

        chosen_noun = nouns[random.randint(0, len(nouns)-1)]

        # load images
        img_list = img_search.get_img_from_search(chosen_noun)

        # keep trying until valid img
        #got_img = False
        #while not got_img:
        #    try:
        img_edit.construct_img(query, img_list[random.randint(0, len(img_list)-1)], 500)
        #got_img = True
        #    except:
        #        pass

        # post
        fn = os.path.abspath("./images/out.png")
        msg = "#blairsilverstrea\nYou've heard of Elf on the Shelf. Now get ready for..."

        print(query + " on a " + chosen_noun)

        #api.update_with_media(fn, msg)
        break

        time.sleep(1200)

    else:
        print("No query provided")


# QApplication e QPushButton de PySide6.QtWidgets
# QApplication -> O Widget principal da aplicação
# QPushButton -> Um botão
# PySide6.QtWidgets -> Onde estão os widgets do PySide6

import sys
from PySide6.QtWidgets import QApplication, QPushButton

app = QApplication(sys.argv)

button = QPushButton('button text')
button.setStyleSheet('font-size: 40px; color: red; margin: 5px; padding:10px')
button.show()   # Adicione o widget na hierarquia e exibe a janela

app.exec()


from pybal import monitor

from twisted.internet import reactor, protocol
from twisted.web import client
from twisted.python.runtime import seconds

class ProxyFetchMonitoringProtocol(monitor.MonitoringProtocol):
    """
    Monitor that checks server uptime by repeatedly fetching a certain URL
    """

    INTV_CHECK = 10

    TIMEOUT_GET = 5

    __name__ = 'ProxyFetch'

    from twisted.internet import defer, error
    from twisted.web import error as weberror
    catchList = ( defer.TimeoutError, weberror.Error, error.ConnectError )

    def __init__(self, coordinator, server, configuration={}):
        """Constructor"""

        # Call ancestor constructor
        super(ProxyFetchMonitoringProtocol, self).__init__(coordinator, server, configuration)

        self.intvCheck = self._getConfigInt('interval', self.INTV_CHECK)
        self.toGET = self._getConfigInt('timeout', self.TIMEOUT_GET)

        self.checkCall = None

        self.checkStartTime = None

        self.URL = self._getConfigStringList('url')

        # Install cleanup handler
        reactor.addSystemEventTrigger('before', 'shutdown', self.stop)

    def run(self):
        """Start the monitoring"""

        super(ProxyFetchMonitoringProtocol, self).run()

        if not self.checkCall or not self.checkCall.active():
            self.checkCall = reactor.callLater(self.intvCheck, self.check)

    def stop(self):
        """Stop all running and/or upcoming checks"""

        super(ProxyFetchMonitoringProtocol, self).stop()

        if self.checkCall and self.checkCall.active():
            self.checkCall.cancel()

        # TODO: cancel a getPage as well

    def check(self):
        """Periodically called method that does a single uptime check."""

        # FIXME: Check if this monitoring instance is even still active

        # FIXME: Use GET as a workaround for a Twisted bug with HEAD/Content-length
        # where it expects a body and throws a PartialDownload failure

        import random
        url = random.choice(self.URL)

        self.checkStartTime = seconds()
        self.getProxyPage(url, method='GET', host=self.server.host, port=self.server.port,
                            timeout=self.toGET, followRedirect=False
            ).addCallbacks(self._fetchSuccessful, self._fetchFailed
            ).addBoth(self._checkFinished)

    def _fetchSuccessful(self, result):
        """Called when getProxyPage is finished successfully."""

        self.report('Fetch successful, %.3f s' % (seconds() - self.checkStartTime))
        self._resultUp()

        return result

    def _fetchFailed(self, failure):
        """Called when getProxyPage finished with a failure."""

        self.report('Fetch failed, %.3f s' % (seconds() - self.checkStartTime))

        self._resultDown(failure.getErrorMessage())

        failure.trap(*self.catchList)

    def _checkFinished(self, result):
        """
        Called when getProxyPage finished with either success or failure,
        to do after-check cleanups.
        """

        self.checkStartTime = None

        # Schedule the next check
        if self.active:
            self.checkCall = reactor.callLater(self.intvCheck, self.check)

        return result

    def getProxyPage(url, contextFactory=None, host=None, port=None, *args, **kwargs):
        """Download a web page as a string. (modified from twisted.web.client.getPage)

        Download a page. Return a deferred, which will callback with a
        page (as a string) or errback with a description of the error.

        See HTTPClientFactory to see what extra args can be passed.
        """

        factory = client.HTTPClientFactory(url, *args, **kwargs)

        host = host or factory.host
        port = port or factory.port

        if factory.scheme == 'https':
            from twisted.internet import ssl
            if contextFactory is None:
                contextFactory = ssl.ClientContextFactory()
            reactor.connectSSL(host, port, factory, contextFactory)
        else:
            reactor.connectTCP(host, port, factory)
        return factory.deferred
    getProxyPage = staticmethod(getProxyPage)


import os
import socket
import pickle

import pytorch3d
from torch.utils.data import DataLoader
from torch.utils.data.dataloader import default_collate

from .pointcloud_dataset import PointcloudDataset
from .video_dataset import VideoDataset

# Define some important paths
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

# get dataset root
RGBD_3D_ROOT = ""
GEO_3DMATCH_ROOT = ""
SCANNET_ROOT = ""

def build_dataset(cfg, split, overfit=None):
    """
    Builds a dataset from the provided dataset configs.
    Configs can be seen is configs/config.py
    """
    if cfg.name == "ScanNet":
        root_path = SCANNET_ROOT
        dict_path = os.path.join(PROJECT_ROOT, f"data/scannet_{split}.pkl")
        data_dict = load_pickle(dict_path)
        dataset = VideoDataset(cfg, root_path, data_dict, split)

        # Reduce ScanNet validation size to allow for more frequent validation
        if split == "valid":
            dataset.instances = dataset.instances[::10]
    elif cfg.name == "3DMatch":
        root_path = RGBD_3D_ROOT
        dict_path = os.path.join(PROJECT_ROOT, f"data/3dmatch_{split}.pkl")
        data_dict = load_pickle(dict_path)
        dataset = VideoDataset(cfg, root_path, data_dict, split)

        # Reduce ScanNet validation size to allow for more frequent validation
        if split == "valid":
            dataset.instances = dataset.instances[::10]
    elif cfg.name == "3DMatch_PCReg":
        assert split == "test"
        root_path = GEO_3DMATCH_ROOT
        dict_path = os.path.join(PROJECT_ROOT, "data/3dmatch_reg_test.pkl")
        data_dict = load_pickle(dict_path)
        dataset = PointcloudDataset(cfg, root_path, data_dict, split, rotated=False)
    else:
        raise ValueError("Dataset name {} not recognized.".format(cfg.name))

    # Overfit only loads a single batch for easy debugging/sanity checks
    if overfit is not None:
        assert type(overfit) is int
        dataset.instances = dataset.instances[: cfg.batch_size] * overfit

    return dataset

def build_loader(cfg, split, overfit=None):
    """
    Builds the dataset loader (including getting the dataset).
    """
    dataset = build_dataset(cfg, split, overfit)
    shuffle = (split == "train") and (not overfit)
    batch_size = cfg.batch_size

    loader = DataLoader(
        dataset=dataset,
        batch_size=int(batch_size),
        shuffle=shuffle,
        pin_memory=False,
        collate_fn=custom_collate,
        num_workers=cfg.num_workers,
    )

    return loader

def custom_collate(data):
    out = {}
    keys = data[0].keys()

    for key in keys:
        if "points_rgb_" in key:
            view_i = key.split("_")[-1]
            xyz = [ins[f"points_{view_i}"] for ins in data]
            rgb = [ins[f"points_rgb_{view_i}"] for ins in data]
            out[key] = pytorch3d.structures.Pointclouds(xyz, None, rgb)
        elif "points_" in key:
            out[key] = pytorch3d.structures.Pointclouds([ins[key] for ins in data])
        else:
            out[key] = default_collate([ins[key] for ins in data])

    return out

# load pickle
def load_pickle(path):
    with open(path, "rb") as f:
        return pickle.load(f)


# 传递任意数量的实参--类比JDK1.5引入的不定参数
# 空元组--toppings

def make_pizza(*toppings):
    """打印Pizza"""
    print(toppings)
    for topping in toppings:
        print('--' + topping)

make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')

# 结合使用位置实参和任意数量实参
# 如果要让函数接受不同类型的实参， 必须在函数定义中将接纳任意数量实参的形参放在最后。

def make_pizza(size, *toppings):
    """概述要制作的比萨"""
    print("\nMaking a " + str(size) +
          "-inch pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)

make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

# 使用任意数量的关键字实参
# **info--空字典
def build_profile(first, last, **info):
    """创建一个字典来保存客户信息"""
    profile = {'first_name': first, 'last_name': last}
    for key, value in info.items():
        profile[key] = value

    return profile

user_profile = build_profile('albert', 'einstein',
                             location='princeton', field='physics')

print(user_profile)


import web
from bson.objectid import ObjectId
import logging
from inginious.frontend.webapp.pages.utils import INGIniousAuthPage

class AggregationPage(INGIniousAuthPage):
    """ Aggregation page """

    _logger = logging.getLogger("inginious.webapp.aggregations")

    def GET_AUTH(self, courseid):  # pylint: disable=arguments-differ
        """ GET request """

        course = self.course_factory.get_course(courseid)
        username = self.user_manager.session_username()

        error = False
        change = False
        msg = ""
        data = web.input()
        if self.user_manager.has_staff_rights_on_course(course):
            raise web.notfound()
        elif not self.user_manager.course_is_open_to_user(course, lti=False):
            return self.template_helper.get_renderer().course_unavailable()
        elif "register_group" in data:
            change = True
            if course.can_students_choose_group() and course.use_classrooms():
                aggregation = self.database.aggregations.find_one({"courseid": course.get_id(), "students": username})

                if int(data["register_group"]) >= 0 and (len(aggregation["groups"]) > int(data["register_group"])):
                    group = aggregation["groups"][int(data["register_group"])]
                    if group["size"] > len(group["students"]):
                        for index, group in enumerate(aggregation["groups"]):
                            if username in group["students"]:
                                aggregation["groups"][index]["students"].remove(username)
                        aggregation["groups"][int(data["register_group"])]["students"].append(username)
                    self.database.aggregations.replace_one({"courseid": course.get_id(), "students": username}, aggregation)
                    self._logger.info("User %s registered to group %s/%s/%s", username, courseid, aggregation["description"], data["register_group"])
                else:
                    error = True
                    msg = "Couldn't register to the specified group."
            elif course.can_students_choose_group():

                aggregation = self.database.aggregations.find_one(
                    {"courseid": course.get_id(), "students": username})

                if aggregation is not None:
                    aggregation["students"].remove(username)
                    for index, group in enumerate(aggregation["groups"]):
                        if username in group["students"]:
                            aggregation["groups"][index]["students"].remove(username)
                    self.database.aggregations.replace_one({"courseid": course.get_id(), "students": username}, aggregation)

                # Add student in the classroom and unique group
                self.database.aggregations.find_one_and_update({"_id": ObjectId(data["register_group"])},
                                                             {"$push": {"students": username}})
                new_aggregation = self.database.aggregations.find_one_and_update({"_id": ObjectId(data["register_group"])},
                                                                             {"$push": {"groups.0.students": username}})

                if new_aggregation is None:
                    error = True
                    msg = "Couldn't register to the specified group."
                else:
                    self._logger.info("User %s registered to team %s/%s", username, courseid, aggregation["description"])
            else:
                error = True
                msg = "You are not allowed to change group."
        elif "unregister_group" in data:
            change = True
            if course.can_students_choose_group():
                aggregation = self.database.aggregations.find_one({"courseid": course.get_id(), "students": username, "groups.students": username})
                if aggregation is not None:
                    for index, group in enumerate(aggregation["groups"]):
                        if username in group["students"]:
                            aggregation["groups"][index]["students"].remove(username)
                    self.database.aggregations.replace_one({"courseid": course.get_id(), "students": username}, aggregation)
                    self._logger.info("User %s unregistered from group/team %s/%s", username, courseid, aggregation["description"])
                else:
                    error = True
                    msg = "You're not registered in a group."
            else:
                error = True
                msg = "You are not allowed to change group."

        tasks = course.get_tasks()
        last_submissions = self.submission_manager.get_user_last_submissions(5, {"courseid": courseid, "taskid": {"$in": list(tasks.keys())}})
        for submission in last_submissions:
            submission["taskname"] = tasks[submission['taskid']].get_name()

        aggregation = self.user_manager.get_course_user_aggregation(course)
        aggregations = self.user_manager.get_course_aggregations(course)
        users = self.user_manager.get_users_info(self.user_manager.get_course_registered_users(course))

        if course.use_classrooms():
            mygroup = None
            for index, group in enumerate(aggregation["groups"]):
                if self.user_manager.session_username() in group["students"]:
                    mygroup = group
                    mygroup["index"] = index + 1

            return self.template_helper.get_renderer().classroom(course, last_submissions, aggregation, users,
                                                                 mygroup, msg, error, change)
        else:
            return self.template_helper.get_renderer().team(course, last_submissions, aggregations, users,
                                                            aggregation, msg, error)


import sys
import pyautogui as py

"""
Spotlight example that accepts 1 argument "String" and runs it through Spotlight
to search. When running in Terminal, provide the argument after the file.
Ex: "python3 spotlight.py Settings"
"""

def spotlight(text):
    py.keyDown('command')
    py.press('space')
    py.keyUp('command')
    py.write(text)
    py.press('return')
    return

spotlight(text=sys.argv[1])


n=int(input())
src=[int(x) for x in input().split()]
ans=[]
def reverse_in_array(start,end):
    tmp=[]
    for i in range(start,end):
        tmp.append(src[i])
    tmp=list(reversed(tmp))
    curr=0
    for i in range(start,end):
        src[i]=tmp[curr]
        curr+=1
for i in range(n):
    minindex=i
    minvalue=src[i]
    for j in range(i+1,n):
        if src[j]<minvalue:
            minvalue=src[j]
            minindex=j
    if minindex!=i:
        reverse_in_array(i,minindex+1)
    ans.append(minindex+1)
print(*ans,end=' ')


#!/usr/bin/python
# -*- coding: UTF-8 -*-
s=input().split(" ")
n=int(s[0])
k=int(s[1])
a = input()
l = [int(x) for x in a.split(" ")]
li = []
re = 0
for i in range(0, k):
    ss = [int(x) for x in input().split(" ")]
    if (ss[0] == 1):
        le = ss[1] - 1
        r = ss[2]
        li = l[le:r]
        li.sort()
        print(li.index(ss[3]) + 1)
    elif (ss[0] == 2):
        le = ss[1] - 1
        r = ss[2]
        li = l[le:r]
        li.sort()
        print(li[ss[3] - 1])
    elif (ss[0] == 3):
        l[ss[1] - 1] = ss[2]
    elif (ss[0] == 4):
        le = ss[1] - 1
        r = ss[2]
        re = 0
        for j in range(le, r):
            if (l[j] < ss[3] and re < l[j]):
                re = l[j]
        print(re)
    else:
        le = ss[1] - 1
        r = ss[2]
        re = 10000000
        for j in range(le, r):
            if (l[j] > ss[3] and re > l[j]):
                re = l[j]
        print(re)


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 21:26:57 2020

@author: yakup
"""

import numpy as np
import pandas as pd
import re
import nltk

from nltk.stem.porter import PorterStemmer
ps=PorterStemmer()

nltk.download("stopwords")
from nltk.corpus import stopwords

#yorumlar=pd.read_table("Restaurant_Reviews.tsv")
yorumlar=pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t', quoting = 3)

satir=yorumlar.index

derlem=[]

for i in range(len(satir)):
    yorum=re.sub("[^a-zA-Z]"," ",yorumlar["Review"][i])
    yorum=yorum.lower().split()
    yorum=[ps.stem(kelime) for kelime in yorum if not kelime in set(stopwords.words("english"))]
    yorum=" ".join(yorum)
    derlem.append(yorum)

#bag of words

from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer(max_features=2000)
x=cv.fit_transform(derlem).toarray()
y=yorumlar.iloc[:,1]

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

from sklearn.naive_bayes import GaussianNB
gaus=GaussianNB()
gaus.fit(x_train,y_train)

y_tahmin=gaus.predict(x_test)

from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_tahmin)

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(5,5))
colors=["green",'pink']
pos=yorumlar[yorumlar['Liked']==1]
neg=yorumlar[yorumlar['Liked']==0]
ck=[pos['Liked'].count(),neg['Liked'].count()]
legpie=plt.pie(ck,labels=["Positive","Negative"],
                 autopct ='%1.1f%%',
                 shadow = True,
                 colors = colors,
                 startangle = 45,
                 explode=(0, 0.1))

stop=stopwords.words('english')

from wordcloud import WordCloud
positivedata = yorumlar[ yorumlar['Liked'] == 1]
positivedata =positivedata['Review']
negdata = yorumlar[yorumlar['Liked'] == 0]
negdata= negdata['Review']

def wordcloud_draw(yorumlar, color = 'white'):
    words = ' '.join(yorumlar)
    cleaned_word = " ".join([word for word in words.split()
                              if(word!='food' and word!='place')
                            ])
    wordcloud = WordCloud(stopwords=stop,
                      background_color=color,
                      width=2500,
                      height=2000
                     ).generate(cleaned_word)
    plt.figure(1,figsize=(10, 7))
    plt.imshow(wordcloud)
    plt.axis('off')
    plt.show()

wordcloud_draw(positivedata,'white')

wordcloud_draw(negdata,"white")


def tic_tac_toe():
    board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    end = False
    #All posible winning combinations
    win_commbinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))

    #Prints Tic Tac Toe Board
    def draw():
        print(board[0], board[1], board[2])
        print(board[3], board[4], board[5])
        print(board[6], board[7], board[8])
        print()

    def p1():
        n = choose_number()
        #Checks if a player has already played in that spot
        if board[n] == "X" or board[n] == "O":
            print("\nYou can't go there. Try again")

        else:
            board[n] = "X"

    def p2():
        n = choose_number()
        if board[n] == "X" or board[n] == "O":
            print("\nYou can't go there. Try again")
            p2()
        else:
            board[n] = "O"

    def choose_number():
        while True:
                a = input()
                try:
                    a = int(a)
                    a -= 1
                    if a in range(0, 9):
                        return a
                    else:
                        print("\nThat's not on the board. Try again")
                        continue
                #Runs when the input entered is not a number value
                except ValueError:
                    print("\nThat's not a number. Try again")
                    continue

    def check_board():
        count = 0
        for a in win_commbinations:
            if board[a[0]] == board[a[1]] == board[a[2]] == "X":
                print("Player 1 Wins!\n")
                print("Congrats!\n")
                return True

            if board[a[0]] == board[a[1]] == board[a[2]] == "O":
                print("Player 2 Wins!\n")
                print("Congratulations!\n")
                return True
        for a in range(9):
            if board[a] == "X" or board[a] == "O":
                count += 1
            #Checks if 9 plays have been made and if so calls a tie
            if count == 9:
                print("The game ends in a Tie\n")
                return True

    #While the game is still playing
    while not end:
        draw()
        end = check_board()
        if end == True:
            break
        print("Player 1 choose where to place a cross")
        p1()
        print()
        draw()
        end = check_board()
        if end == True:
            break
        print("Player 2 choose where to place a nought")
        p2()
        print()

    #Runs when the game endss
    if input("Play again (y/n)\n") == "y":
        print()
        tic_tac_toe()

#Runs the game
tic_tac_toe()


from turtle import Turtle, Screen

class Ball(Turtle):
    def __init__(self): # no need for paddle = Turtle() => inheritance => The code knows Paddle is a Turtle
        super().__init__() # important so the CPU understands where all the attributes come from (ie : xcor)
        self.shape("circle")
        # self.shapesize(stretch_wid=2, stretch_len=1)
        self.color("white")
        self.penup()
        self.goto(0,0)
        self.speed("slow")
        self.y_move = 10
        self.x_move = 10

    def move(self):
        new_x = self.xcor() + self.x_move
        new_y = self.ycor() + self.y_move
        self.goto(new_x, new_y)

    # def wall_bounce(self):
    #     # always check first!!! The code stays in move function
    #     self.y_move *= -1  # and does not use the __init__ anymore
    #
    # def paddle_bounce(self):
    #     self.x_move *= -1
    # def reset_position(self):
    #     ball.goto(0, 0)
    #     ball.x_move = 10


import json
import time
from datetime import *
import math
from webview.DataControl import match_time

cur_time = ""
# cur_time =
def get_json_dic(directory):
	return json.loads(open(directory,'r').read())

def coh_date_time(date, time):
	return date + ' ' + time

# 2018-06-14 18:00:00
def date_intp(str):
	return datetime.strptime(str,'%Y-%m-%d %H:%M:%S')

def dic_rev(dic):
	ret = {}
	for key in dic.keys():
		ret.update({dic[key]:key})
	return ret

def get_cur_time():
	global cur_time
	return date_intp(cur_time)

def get_time_str():
	global cur_time
	return cur_time
# rec_arr is the record array
# left is the left opeation index
# while right is the right operation index
def qs(rec_arr):
	quick_sort(rec_arr, 0, len(rec_arr) - 1)

def quick_sort(rec_arr, left, right):
	if(right <= left):
		return
	pivot = math.floor((left + right) / 2)
	rec_arr[pivot], rec_arr[right] = rec_arr[right], rec_arr[pivot]
	pivot = Partition(rec_arr, left, right)
	quick_sort(rec_arr, left, pivot - 1)
	quick_sort(rec_arr, pivot + 1, right)

def Partition(rec_arr, left, right):
	l = left
	r = right
	temp_rec = rec_arr[r]
	while l < r:
		while (rec_arr[l] <= temp_rec) and (r > l):
			l += 1
		if l < r:
			rec_arr[r] = rec_arr[l]
			r -= 1
		while (rec_arr[r] >= temp_rec) and (r > l):
			r -= 1
		if l < r:
			rec_arr[l] = rec_arr[r]
			l += 1
	rec_arr[l] = temp_rec
	return l

# "2018-07-29 19:40:00"

def set_cur_time(str):
	global cur_time
	# cur_time = datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
	cur_time = str


from django.shortcuts import render

from shop.models import Product

def filter(request):

    if 'min_price' in request.GET:
        filter_price1 = request.GET.get('min_price')
        filter_price2 = request.GET.get('max_price')
        if filter_price1 == '':
            filter_price1 = 0

        products= Product.objects.filter(price__range=(filter_price1, filter_price2))
    else:
        products=Product.objects.all()

    return render(request,'products.html',{'products':products})


import requests
from bs4 import BeautifulSoup

url = "https://www.n11.com/bilgisayar/dizustu-bilgisayar"
headers = {"User-Agent":"###################"} # Insert your User-Agent here

html = requests.get(url, headers=headers).text

soupObject = BeautifulSoup(html, "html.parser")

laptops = soupObject.find_all("li", class_= "column")
for laptop in laptops:
    name = laptop.find("h3").string
    link = laptop.div.a["href"]
    print("Product Link: "+link)
    priceCont = laptop.find("div", class_= "priceContainer").div.span
    if priceCont.next_sibling.next_sibling.next_sibling.next_sibling != None: # If there is a discount on the cart n11 has 3 spans for it in the html. Therefore, I preferred to check discount number of spans
        price = priceCont.find("del").text.strip("TL").strip() # We are deleting unnecessary things like currency and spaces. In case of converting string to float or int.
        cartPrice = priceCont.next_sibling.next_sibling.next_sibling.next_sibling.find('ins').text.strip('TL').strip() # We are deleting unnecessary things like currency and spaces. In case of converting string to float or int.
        floatPrice = float(price[:-3])
        floatCartPrice = float(cartPrice[:-3])
        print(f"Product: {(name+',').ljust(110)} Price: {(price+ 'TL').ljust(12)} Price in cart: {cartPrice} You are getting:%{(100*(floatPrice-floatCartPrice)/floatPrice)} discount")

    else:
        price = priceCont.next_sibling.next_sibling.find("ins").text.strip("TL").strip() # We are deleting unnecessary things like currency and spaces. In case of converting string to float or int.
        floatPrice = price[:-3]
        print(f"Product: {(name+',').ljust(110)} Price: {(price+ 'TL').ljust(12)} (***This product does not have any discount***) Price in cart: {priceCont.next_sibling.next_sibling.find('ins').text}")
    print("".center(50, "#"))


from statistics import median

def print_sum_mean():
    print("\n-------------------Exercício 08--------------------\n")

    lista = [35,6,75,3,500]
    for valor in lista:
        maior =sum(lista)
        media =sum(lista)/len(lista)
    print(f"A soma dos valores foi: {maior}, e a média foi : {media}")

Exercicios Python Brasil Estrutura de Repetição/ex08.py


people = int(input())
weight_all = []
height_all = []

for i in range(people):
    weight, height = map(int, input().split())

    weight_all.append(weight)
    height_all.append(height)

for j in range(people):
    rank = 1

    for h in range(people):
        if (weight_all[j] < weight_all[h]) and (height_all[j] < height_all[h]):
            rank += 1

    print(rank, end = " ")


import os
from pathlib import Path

AUDIO_EXT = ['.mp3']
VIDEO_EXT = ['.mp4', '.mkv', '.avi']
DOCUMENT_EXT = ['.pdf', '.docx', '.pptx', '.xls', '.doc', '.ppt', '.txt']
IMAGE_EXT = ['.jpg', '.jpeg', '.png']

def get_file_extension(file: str) -> str:
    """Returns the extension of a given file."""
    return Path(file).suffix.lower()

def organize_files(path: str) -> None:
    """Organizes files in a given directory into corresponding subdirectories based on file extension."""
    document_path = os.path.join(path, 'documents')
    images_path = os.path.join(path, 'images')
    audios_path = os.path.join(path, 'audios')
    videos_path = os.path.join(path, 'videos')
    others_path = os.path.join(path, 'others')

    # Check if subdirectories exist and create them if they don't
    for dir_path in [document_path, images_path, audios_path, videos_path, others_path]:
        if not os.path.isdir(dir_path):
            os.mkdir(dir_path)

    files = os.listdir(path)

    for file_name in files:
        extension = get_file_extension(file_name)

        if not extension:
            continue  # Skip directories and files without extensions

        if extension in IMAGE_EXT:
            new_path = os.path.join(images_path, file_name)
        elif extension in DOCUMENT_EXT:
            new_path = os.path.join(document_path, file_name)
        elif extension in VIDEO_EXT:
            new_path = os.path.join(videos_path, file_name)
        elif extension in AUDIO_EXT:
            new_path = os.path.join(audios_path, file_name)
        else:
            new_path = os.path.join(others_path, file_name)

        # Move the file to its corresponding folder
        os.rename(os.path.join(path, file_name), new_path)

if __name__ == '__main__':
    path = input(r'PATH: ')
    organize_files(path)


import colored
import os
import requests
import threading
from bs4 import BeautifulSoup as bs

play_list_url = "https://www.youtube.com/user/TheViperAOC/playlists"

def get_list():
    list_page = requests.get(play_list_url).text
    list_page_soup = bs(list_page, "lxml")
    play_list = list_page_soup.find_all("h3", {"class": "yt-lockup-title"})
    number = 0
    info_list = []
    print("All play list is : ")
    for item in play_list:
        list_title = item.a.get_text()
        list_url = "https://www.youtube.com" + item.a["href"]
        print("+------------------------------------------------------------------------+")
        print("{}Number : {}".format(colored.fg(5), colored.attr(0)) + str(number))
        print("{}List Title : {}".format(colored.fg(11), colored.attr(0)) + list_title)
        print("{}List URL : {}".format(colored.fg(1), colored.attr(0)) + list_url)
        list_info = (number, list_title, list_url)
        number += 1
        info_list.append(list_info)
    return info_list

def get_video(selected_info):
    list_page_url = selected_info[2]
    list_page = requests.get(list_page_url).text
    list_soup = bs(list_page, "lxml")
    video_list = list_soup.find_all("tr")
    number = 0
    info_list = []
    for item in video_list:
        info = item.find("a", {"class": "pl-video-title-link yt-uix-tile-link yt-uix-sessionlink spf-link"})
        video_url = "https://www.youtube.com" + info["href"]
        video_name = info.get_text()
        video_name = video_name.replace("\n", "")
        video_name = video_name.replace(" ", "")
        list_info = (number, video_name, video_url)
        number += 1
        info_list.append(list_info)
    return info_list

def download_subtitle(url):
    os.system("youtube-dl.exe --no-playlist --proxy \"http://127.0.0.1:1099\" --write-auto-sub --skip-download \"%s\"" % url)

def download_video(url):
    os.system("youtube-dl.exe --no-playlist --proxy \"http://127.0.0.1:1099\" -f \"(mp4)[width>=1920][height>=1080][fps>=60]\" \"%s\"" % url)

def download_audio(url):
    os.system("youtube-dl.exe --no-playlist --proxy \"http://127.0.0.1:1099\" -f \"(m4a)[asr=44100][abr<=320]\" \"%s\"" % url)

def threading_download(info):
    while True:
        if_only_sub_select = input("Would you like to Download Video+Audio+Subtitle or just Subtitle ? VAS/S").upper()
        if if_only_sub_select == "VAS":
            print("Downloading Video+Audio+Subtitle")
            for video in info:
                url = str(video[2])
                threading.Thread(target=download_audio, args=(url,)).start()
                threading.Thread(target=download_video, args=(url,)).start()
                threading.Thread(target=download_subtitle, args=(url,)).start()
            break
        elif if_only_sub_select == "S":
            print("Downloading only the Subtitle")
            for video in info:
                url = str(video[2])
                threading.Thread(target=download_subtitle, args=(url,)).start()
            break
        else:
            print("Please enter 'VAS'(for Video+Audio+Subtitle) or 'S'(only Subtitle)")
            continue

if __name__ == '__main__':
    print("Launching, please wait...")
    play_list_info_list = get_list()
    selected_list = None
    while True:
        try:
            list_selection = int(input("Which one would you like to use ? "))
        except TypeError:
            print("Wrong Input, Try again!")
            continue
        if list_selection > len(play_list_info_list):
            print("Too large number, Try again!")
            continue
        else:
            print("\nYour selection is :")
            print("+------------------------------------------------------------------------+")
            print("{}Number : {}".format(colored.fg(5), colored.attr(0)) + str(play_list_info_list[list_selection][0]))
            print("{}List Title : {}".format(colored.fg(11), colored.attr(0)) + play_list_info_list[list_selection][1])
            print("{}List URL : {}".format(colored.fg(1), colored.attr(0)) + play_list_info_list[list_selection][2])
            while True:
                try:
                    list_confirm = input("Is that correct ? Y/N").upper()
                except TypeError:
                    print("Please input 'Y' or 'N', Try again!")
                    continue
                if list_confirm == "Y":
                    print("Processing...")
                    selected_list = play_list_info_list[list_selection]
                    break
                elif list_confirm == "N":
                    print("Please try again...")
                    continue
                else:
                    print("Wrong input, please try again!")
                    continue
            break
    print("Entering List...")
    video_list_info = get_video(selected_list)
    threading_download(video_list_info)


import time
def wait_for_echo(timeout):
        count = timeout
        while count>0:
            count = count-1

start = time.time()
wait_for_echo(10000)
finish = time.time()

pulse_len = finish-start
distance_cm = pulse_len/0.000058

print(pulse_len)
print(distance_cm)


import keras

def LSTM( units, gpu=False, **kwargs ):
  '''
  Uses optimized CuDNNLSTM if gpu is activated
  '''
  if gpu:
    layer = keras.layers.CuDNNLSTM( units=units, **kwargs )
  else:
    # Recurrent activation should default to sigmoid so that it
    # behaves similar to CuDNNLSTM. The normal default is hard
    # sigmoid
    if not 'recurrent_activation' in kwargs:
      kwargs['recurrent_activation'] = 'sigmoid'
    layer = keras.layers.LSTM( units=units, **kwargs )

  return layer


"""Open Api Specs request decorator."""
import copy
from functools import partial
from typing import Any

# locations supported by openapi
VALID_SCHEMA_LOCATIONS = (
    "cookies",
    "files",
    "form",
    "headers",
    "json",
    "match_info",
    "path",
    "query",
    "querystring",
)

def request_schema(
    schema: Any,
    location: str = "json",
    put_into: Any = None,
    example: Any = None,
    add_to_refs: bool = False,
    **kwargs: Any,
) -> Any:
    """Request info for Open API docs.

    Add request info into the swagger spec.
    put_into (Any): name of the key in Request object where validated
    data will be placed. If None (by default) default key will be used

    add_to_refs (bool): Working only if example not None, if True,
    add example for ref schema, otherwise add example to endpoint.

    Parameters:
        schema (Any): `Schema <marshmallow.Schema>` class or instance
        location (str): Default request locations to parse
        put_into (Any): name of the key in Request object
        example (Any): Adding example for current schema
        add_to_refs (bool): Default False
        **kwargs (Any): extra arguments

    Raises:
        ValueError: if invalid location found or if multiple location found

    Returns:
        function of annotation
    """  # noqa: RST210
    if location not in VALID_SCHEMA_LOCATIONS:
        raise ValueError(f"Invalid location argument: {location}")

    if callable(schema):
        schema = schema()

    options = {"required": kwargs.pop("required", False)}

    def wrapper(func: Any) -> Any:
        if not hasattr(func, "__apispec__"):
            func.__apispec__ = {
                "schemas": [],
                "responses": {},
                "parameters": [],
            }
            func.__schemas__ = []

        _example = copy.copy(example) or {}
        if _example:
            _example["add_to_refs"] = add_to_refs
        func.__apispec__["schemas"].append(
            {
                "schema": schema,
                "location": location,
                "options": options,
                "example": _example,
            }
        )

        # "body" location was replaced by "json" location
        if location == "json" and any(
            func_schema["location"] == "json"
            for func_schema in func.__schemas__
        ):
            raise RuntimeError("Multiple json locations are not allowed")

        func.__schemas__.append(
            {"schema": schema, "location": location, "put_into": put_into}
        )

        return func

    return wrapper

# For backward compatibility
use_kwargs = request_schema

# Decorators for specific request data validations (shortenings)
match_info_schema = partial(
    request_schema, location="match_info", put_into="match_info"
)
querystring_schema = partial(
    request_schema, location="querystring", put_into="querystring"
)
form_schema = partial(request_schema, location="form", put_into="form")
json_schema = partial(request_schema, location="json", put_into="json")
headers_schema = partial(
    request_schema, location="headers", put_into="headers"
)
cookies_schema = partial(
    request_schema, location="cookies", put_into="cookies"
)


import pickle
from rdkit import Chem
import torch
from rdkit.Chem import AllChem
from rdkit.Chem import Descriptors
class smi_inform(object):
    def __init__(self,radius=2, nBits=1024):
        self.radius = radius
        self.nBits = nBits
    def get_mol(self,smi):
        mol = Chem.MolFromSmiles(smi)
        return mol
    def get_prop(self,mol):
        fp1 = AllChem.GetMorganFingerprintAsBitVect(mol,self.radius,self.nBits,useChirality=True)
        self.fp = torch.Tensor(fp1)
        self.TPSA = Descriptors.TPSA(mol)
        self.logp = Descriptors.MolLogP(mol)
        self.rings = Descriptors.RingCount(mol)
        return self


# -*- coding: utf-8 -*-
from lxml import etree
import sys
import os

def seed_target_from_sbml(draft,biomass_name,repository) :

        if not os.path.isdir(repository) :
            os.makedirs(repository)

        seeds = set()
        bio_reac = set()
        BDD_species = {}

        draft_parse = etree.parse(draft)
        sbml = draft_parse.find('sbml')
        root = "}".join(draft_parse.getroot().tag.split("}")[:-1])+"}"

        listOfSpecies = draft_parse.find(root+'model/'+root+'listOfSpecies')
        for specie in listOfSpecies :
                BDD_species.update({specie.attrib["id"] : specie})
                if specie.attrib["boundaryCondition"] == "true" :
                        seeds.add(specie)

        listOfReactions = draft_parse.find(root+'model/'+root+'listOfReactions')
        for reaction in listOfReactions :
                try :
                        id_reac = reaction.attrib["id"]
                except KeyError :
                        id_reac = ""
                try :
                        id_name_reac = "_".join(id_reac.split("_")[1:])
                except KeyError :
                        id_name_reac = ""
                try :
                        name_reac = reaction.attrib["name"]
                except KeyError :
                        name_reac = ""
                if (biomass_name in id_reac) or (biomass_name in id_name_reac) or (biomass_name in name_reac) :
                        Reactants = reaction.find(root+'listOfReactants')
                        for specie in Reactants :
                                bio_reac.add(BDD_species[specie.attrib["species"]])

        with open(draft,"r") as file :
                line0 = file.readlines()[0]
        ################################################################ seed

        seed_doc = draft_parse
        root = "}".join(seed_doc.getroot().tag.split("}")[:-1])+"}"
        model = seed_doc.find(root+'model')
        listR = model.find(root+"listOfReactions")
        model.remove(listR)

        listS = model.find(root+"listOfSpecies")
        listS.clear()
        for element in seeds :
                listS.append(element)
        outFile=open(repository+'/seeds_from_sbml.sbml','wb')
        outFile.write(line0.encode('utf-8'))
        seed_doc.write(outFile, pretty_print=True)

        ################################################################ target

        target_doc = draft_parse
        root = "}".join(target_doc.getroot().tag.split("}")[:-1])+"}"
        model = target_doc.find(root+'model')
        try :
                listR = model.find(root+"listOfReactions")
                model.remove(listR)
        except TypeError :
                pass
        listS = model.find(root+"listOfSpecies")
        listS.clear()
        for element in bio_reac :
                listS.append(element)
        outFile=open(repository+'/targets_from_sbml.sbml','wb')
        outFile.write(line0.encode('utf-8'))
        target_doc.write(outFile, pretty_print=True)

if __name__ == '__main__':

        draft = sys.argv[1]
        biomass_name = sys.argv[2]
        try :
                repository = sys.argv[3]
        except IndexError :
                repository = "/".join(file_read.split("/")[:-1])

        seed_target_from_sbml(draft,biomass_name,repository)


import json
import os
from urllib.parse import urljoin

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Sec-Fetch-Site': 'same-origin',
    'Accept-Language': 'zh-TW,zh-Hant;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Sec-Fetch-Mode': 'navigate',
    'Host': 'exam.naer.edu.tw',
    'Origin': 'https://exam.naer.edu.tw',
    'Referer': 'https://exam.naer.edu.tw/searchResult.php',
    'Connection': 'keep-alive',
    'Sec-Fetch-Dest': 'document',
}

total_pages = 4767
data_file = "./data.json"

if os.path.exists(data_file):
    with open(data_file, 'r', encoding='utf-8') as f:
        all_data = json.load(f)
    if all_data:
        last_record = all_data[-1]
        last_page = int(last_record['page'])
        start_page = last_page + 1
    else:
        start_page = 1
else:
    all_data = []
    start_page = 1

for page in tqdm(range(start_page, total_pages + 1)):
    data = {
        'page': page,
        'orderBy': 'lastest',
        'selCategory': '0',
        'selTech': '0',
        'chkClass[]': ['15', '16', '17'],
        'keyword': '',
        'selCountry': '',
        'selYear': '',
        'selTerm': '',
        'selType': '',
        'selPublisher': '',
    }

    response = requests.post('https://exam.naer.edu.tw/searchResult.php', headers=headers, data=data)
    soup = BeautifulSoup(response.text, 'html.parser')
    rows = soup.find_all('tr')
    for row in rows[1:]:
        try:
            cols = row.find_all('td')
            if len(cols) < 11:
                continue
            # Check if the "縣市" column contains invalid data
            city = cols[0].text.strip()
            if "回首頁" in city or "版權所有" in city:
                continue
            test_paper_link = cols[9].find('a')['href'] if cols[9].find('a') else None
            answer_paper_link = cols[10].find('a')['href'] if cols[10].find('a') else None
            if test_paper_link is None and answer_paper_link is None:
                continue
            record = {
                "縣市": cols[0].text.strip(),
                "學校名稱": cols[1].text.strip(),
                "年級": cols[2].text.strip(),
                "學年度": cols[3].text.strip(),
                "領域/群科": cols[4].text.strip(),
                "科目": cols[5].text.strip(),
                "種類": cols[6].text.strip(),
                "版本": cols[7].text.strip(),
                "點閱率": cols[8].text.strip(),
                "下載試卷": urljoin('https://exam.naer.edu.tw', test_paper_link),
                "下載答案": urljoin('https://exam.naer.edu.tw', answer_paper_link),
                "page": page,
            }
            all_data.append(record)
        except:
            pass

    with open(data_file, 'w', encoding='utf-8') as f:
        json.dump(all_data, f, ensure_ascii=False, indent=2)


import cv2
import PIL
import numpy as np
import colorgram
from PIL import Image
from matplotlib import pyplot as plt

hijau = 0
hitam = 0

def treshold(img1, hijau, hitam):
		row, col = img1.shape
		treshold = np.zeros((row,col,1), np.uint8)
		for i in range(0, row):
			for j in range(0, col):
				val = img1[i,j]

				if(val > 15 and val < 65):
					val = 0
					hijau = hijau + 1
				elif (val > 65): val = 255
				else: hitam = hitam + 1

				treshold.itemset((i, j, 0), val)
		return treshold, hijau, hitam

img = cv2.imread("DSC_0322.jpg", 0)
plt.hist(img.ravel(),256,[0,256])
#plt.show()

row, col = img.shape
byka= row*col

img2, hijau, hitam = treshold(img, hijau, hitam)
byk = float(hijau+hitam)/1.0
prop = float(hijau)/byk
hit = float(hitam)/byk
print("hitam: ",float(hit),"prop: ",float(prop),"hijau: ",hijau,"byk: ",float(byk/byka))

# cv2.imshow("img 1", img)
# cv2.imshow("img", img2)

# img = cv2.medianBlur(img, 5)
# th3 = cv2.adaptiveThreshold(img,70,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
# cv2.imshow(th3)
# color = ('b', 'g', 'r')
# for i, col in enumerate(color):
#     histr = cv2.calcHist([img],[i],None,[256],[0,256])
#     plt.plot(histr, color = col)
#     plt.xlim([0,256])
# plt.show()
#
# print(histr)

# # img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# colors = colorgram.extract('mangga.jpg', 6)
#
# def hitung(image):
#     hijau, hitam = 0
#     row, col, ch= image.shape
#     canvas = np.zeros((row, col, 3), np.uint8)
#     for m in range(0, row):
#         for n in range(0, col):
#             h, s, v = image[m, n]
#            # gray = (red * 0.299 + green * 0.587 + blue * 0.114)
#            # canvas.itemset((m,n,0),gray)
#     return canvas

# print(colors)

cv2.waitKey(0)
cv2.destroyAllWindows()


class Movie:

    def __init__(self,imdbId, original_title, title,genres, duration, release_date, rating, imdbScore):
        self.imdbId = imdbId
        self.original_title = original_title
        self.title = title
        self.duration = duration
        self.release_date = release_date
        self.genres = genres
        self.imdbScore = imdbScore

        self.rating = None
        self.id = None
        self.actors = []
        self.productors = []
        self.is_3d = None
        self.production_budget = None
        self.marketing_budget = None
        self.synopsis = None

        self.boxOffice = None

    def total_budget(self):
        if (self.production_budget == None or self.marketing_budget == None):
            return None

        return self.production_budget + self.marketing_budget


from flask import Flask, render_template, jsonify, request, send_from_directory
from random import *
from threading import Thread
from flask_cors import CORS
from wattpad2epub import get_book
import time
import os
import platform

def creation_date(path_to_file):
    """
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See http://stackoverflow.com/a/39501288/1709587 for explanation.
    """
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime

def cleanupFiles():
  while (1 == 1):
    current_time = time.time()
    for f in os.listdir("./books"):
      creation_time = os.path.getctime(os.path.join("./books", f))
      if (current_time - 7 * 24 * 60 * 60 > creation_time):
        os.unlink(os.path.join("./books", f))
        print('{} removed'.format(f))
    time.sleep(10)

app = Flask(__name__,
            static_folder = "./dist/",
            template_folder= "./dist/")

cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

@app.route('/cdn/<path:filename>')
def custom_static(filename):
    return send_from_directory('./', filename)

@app.route('/<path:filename>')
def stat(filename):
    return send_from_directory('./dist', filename)

@app.route('/api', methods=["POST"])
def random_number():
    return get_book(request.get_json()["url"])

@app.route('/')
def catch_all():
    return render_template("index.html")

port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)

cleanup = Thread(target=cleanupFiles)
cleanup.start()


import string
import random
from assignment_1.count_min_sketch import CountMinSketch

letters = string.ascii_uppercase
list = []
for i in range(1000):
    list.append(random.choice(letters))

alg = CountMinSketch(26, letters)
alg.create_hash_table()

# the following two loops is created for testing the algorithm's accuracy
dict  = {}
for i in letters:
    dict[i] = 0
for i in list:
    dict[i] = dict[i] + 1
hash_table = alg.create_hash_table()

for i in list:
    alg.increment(i)
for i in letters:
    print(i + " - Estimated: " + str(alg.estimate(i)) + " | Actual: " + str(dict[i]))


import numpy as np
import bpy  # noqa
import gpu  # noqa
import bgl  # noqa
from OpenGL.GL import glGetTexImage

from .camera import Camera
from .utils import find_first_view3d

class OffScreenRenderer:
    """Provides offscreen scene rendering using Eevee.

    Rendering reusing the first found 3D Space View in Blender. The way this view is configured also defines how the resulting image looks like. Use the helper method `set_render_style` to adjust the appearance from within Python.

    This class' `render` method is expected to be called from a `POST_PIXEL` callback, which `AnimationController` takes care of. That is, invoking `render()` from  withing `post_frame` is considered save.

    Note, color images are in linear color spaces and may appear darker than
    expected when visualized. Use `btt.colors.gamma` to gamma encode before
    visualizing.

    Params
    ------
    camera: btb.Camera, None
        Camera view to be rendered. When None, default camera is used.
    origin: str
        When 'upper-left' flip the rendered data to match OpenCV image coordinate
        system. When 'lower-left' image is created using OpenGL coordinate system. Defaults to 'upper-left'.
    mode: str
        Defines the number of color channels. Either 'RGBA' or 'RGB'
    """

    def __init__(self, camera=None, mode="rgba", origin="upper-left"):
        assert mode in ["rgba", "rgb"]
        assert origin in ["upper-left", "lower-left"]
        self.camera = camera or Camera()
        self.offscreen = gpu.types.GPUOffScreen(self.shape[1], self.shape[0])
        self.area, self.space, self.region = find_first_view3d()
        self.handle = None
        self.origin = origin
        channels = 4 if mode == "rgba" else 3
        self.buffer = np.zeros((self.shape[0], self.shape[1], channels), dtype=np.uint8)
        self.mode = bgl.GL_RGBA if mode == "rgba" else bgl.GL_RGB

    @property
    def shape(self):
        return self.camera.shape

    def render(self):
        """Render the scene and return image as buffer.

        Returns
        -------
        image: HxWxD array
            where D is 4 when `mode=='RGBA'` else 3.
        """
        with self.offscreen.bind():
            self.offscreen.draw_view3d(
                bpy.context.scene,
                bpy.context.view_layer,
                self.space,  # bpy.context.space_data
                self.region,  # bpy.context.region
                self.camera.view_matrix,
                self.camera.proj_matrix,
            )

            bgl.glActiveTexture(bgl.GL_TEXTURE0)
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.offscreen.color_texture)

            # np.asarray seems slow, because bgl.buffer does not support the python buffer protocol
            # bgl.glGetTexImage(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGB, bgl.GL_UNSIGNED_BYTE, self.buffer)
            # https://docs.blender.org/api/blender2.8/gpu.html
            # That's why we use PyOpenGL at this point instead.
            glGetTexImage(
                bgl.GL_TEXTURE_2D, 0, self.mode, bgl.GL_UNSIGNED_BYTE, self.buffer
            )

        buffer = self.buffer
        if self.origin == "upper-left":
            buffer = np.flipud(buffer)
        return buffer

    def set_render_style(self, shading="RENDERED", overlays=False):
        self.space.shading.type = shading
        self.space.overlay.show_overlays = overlays


"""Site Views

(Views that do not belong in any app but are needed by site.)

I'm breaking the rules here for simplicity: this should not
be in config, as this is not configuration for the project.
However, introducing an app specifically for a single view
would overcomplicate the work we're doing, so it's going
here instead.
"""

from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.status import HTTP_200_OK
from rest_framework.views import APIView

from oauth2_provider.contrib.rest_framework.permissions import (
    IsAuthenticatedOrTokenHasScope,
)

from .celery import debug_task

def test_celery(request):
    """Use Celery debug task"""
    debug_task.delay("Hello from Celery")
    return HttpResponse("Hello from Django")

class RootApiView(APIView):
    """Direct users to other API endpoints"""

    permission_classes = [IsAuthenticatedOrTokenHasScope]

    def get(self, request, *args, **kwargs):
        """Build & display links to other endpoints"""
        api_endpoints = [
            # (name, url_name),
            ("tag", "api-tag-list"),
            ("startup", "api-startup-list"),
            ("newslink", "api-newslink-list"),
            ("blog", "api-post-list"),
        ]
        data = {
            name: reverse(
                url_name,
                request=request,
                format=kwargs.get("format", None),
            )
            for (name, url_name) in api_endpoints
        }
        return Response(data=data, status=HTTP_200_OK)


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

import numpy as np
import subprocess as sp
import os, sys
from numpy import savez_compressed
import pathlib
import shutil
import glob
from platform import python_version

dimCells = (117,86,38)
totalCells = np.prod(dimCells)

def getEnabledCellsId():
    with open('building', 'r') as file:
        for i, line in enumerate(file):
            if i == 18:
                meshCells = int(line.strip())
                break

    cellBoolArray = np.zeros((totalCells), dtype=bool)
    with open('building', 'r') as file:
        headerSize = 20
        line = file.readline()
        cnt = 1
        while line != '':  # The EOF char is an empty string
            #print(line, end='')
            line = file.readline()
            cnt += 1
            if (cnt > headerSize):
                value = int(line.strip())
                cellBoolArray[value] = True
                if (cnt == (meshCells + headerSize)):
                    break

    #buildingCells = cellBoolArray
    #buildingCells = cellBoolArray.reshape(dimCells,  order='F')
    #buildingCells = buildingCells.reshape(-1,  order='F')

    return cellBoolArray

def getEnabledMatrix():
    with open('building', 'r') as file:
        for i, line in enumerate(file):
            if i == 18:
                meshCells = int(line.strip())
                break

    cellBoolArray = np.zeros((totalCells), dtype=bool)
    with open('building', 'r') as file:
        headerSize = 20
        line = file.readline()
        cnt = 1
        while line != '':  # The EOF char is an empty string
            #print(line, end='')
            line = file.readline()
            cnt += 1
            if (cnt > headerSize):
                value = int(line.strip())
                cellBoolArray[value] = True
                if (cnt == (meshCells + headerSize)):
                    break

    matrix = cellBoolArray.reshape(dimCells,  order='C')

    return matrix

if __name__ == "__main__":
    n_args = len(sys.argv)
    caseVar = 'U'
    path = sys.argv[1]
    #caseId = "{:.2f}".format(float(sys.argv[2]) / 100)
    caseId = sys.argv[2]
    last_of = sys.argv[3]

    predictedTs = "ts%s%s.npz" % (caseId, caseVar)

    #print("ERROR - USAGE: python %s <path> <ts>" % (sys.argv[0]))
    #sys.exit(-1)

    print("Regenerating timestep %s in %s (%s)" % (caseId, path, predictedTs))

    ds = np.load(predictedTs)
    ds = np.array(ds.f.data, order='C')
    ds = ds.reshape(dimCells + (3,),  order='C')
    ds = ds.reshape((-1, 3),  order='F')

    print("Predicted DS shape", ds.shape)

    enabledCells = getEnabledCellsId()
    print("Building DS shape", enabledCells.shape)
    #enabledMatrix = getEnabledMatrix()
    #print("Building DS shape", enabledMatrix.shape)

    cnt=0
    values = "(\n"
    for idx, cell in enumerate(enabledCells):
        if not cell:
            for c in range(3):
                ds[idx][c] = -1000
            cnt += 1
        else:
            #print(ds[idx])
            cell = "(%f %f %f)\n" % (ds[idx][0], ds[idx][1], ds[idx][2])
            values = values + cell
    print(cnt)

    filename = "%s/%s/U" % (path, str(last_of))
    header = ""
    footer = ""
    headerlines = 21
    with open(filename, 'r') as reader:
        line = reader.readline()
        cntline = 0
        cntarray = 0
        while line:
            if cntline <= headerlines:
                header = header + line
                if cntline == headerlines:
                    ncells = int(line.strip())
                    reader.readline()
                    cntline += 1
            elif cntline > (headerlines + ncells +1):
                footer = footer + line
            line = reader.readline()
            cntline += 1

    dir1 = "%s/%s" % (path, str(last_of))
    dir2 = "%s/%s" % (path, caseId)
    print("Copying %s in %s ..." % (dir1, dir2))
    shutil.copytree(dir1, dir2, dirs_exist_ok = True)

    filename = "%s/%s/U" % (path, caseId)
    print("Writing file %s ..." % filename)
    with open(filename, 'w') as writer:
        writer.write(header)
        writer.write(values)
        writer.write(footer)

    command = "touch %s/%s/prediction" % (path, caseId)
    #print(command)
    child = sp.Popen(command.split(), stdout=sp.PIPE)
    streamdata = child.communicate()[0]
    rc = child.returncode

    #COMMENT ONLY FOR TESTING PURPOSES
    os.remove(predictedTs)


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

#WORLD GEN ISSUE: Takes up wayyyy too much ram for a 5k x 5k world.  Need to modularize the generation
import random

from WorldGen.Tile import Tile

class World:

    def __init__(self, size = 100, hives = 1):
        #Setup Frame
        self._size = size
        self.board = []

        self.numHives = hives
        self.hiveLocations = []

        self.foodLocations = []

        #Initialize Data
        self.initializeBoard()
        self.initializeFragrance()
        self.initializeHives()

    '''
    initializeFragrance: Cycles through all the tiles in the world and applies fragrance to them based on food locations
    args: none
    return:
        none
    Notes:
    1.  Cycles through all tiles on board
    2.  If tile contains food, generates a circle of radius where the minimum scent added is 1
    3.  Trims the circle's coordinates to fit the board.
    4.  Takes new list and adds fragrance at a rate of f(x) = fragrance / r^2
    '''
    def initializeFragrance(self):
        for tile in self.board:
            if tile.isFoodSource():
                self.foodLocations.append(self.indexToCoordinate(self.board.index(tile))) #adds to food source list
                fragrance = tile.getFragrance()
                radius = round((10 * fragrance) ** 0.5)
                origin = self.indexToCoordinate(self.board.index(tile))
                coordList = self.coordinateFilter(self.circleArea(origin, radius))
                for entry in coordList:
                    if entry == origin:
                        appliedFragrance = fragrance
                        continue
                    dist = self.distance(entry, origin)
                    appliedFragrance = fragrance / (dist ** 2)
                    index = self.coordinateToIndex(entry)
                    tileSelection = self.board[index]
                    tileSelection.increaseFragrance(appliedFragrance)

    '''
    '''
    def initializeHives(self):
        for i in range(self.numHives):
            searching = True
            while searching:
                index = random.randint(0, self._size ** 2)
                if (not self.board[index].isFoodSource()) and (not self.board[index].hasHive()):
                    self.board[index].addHive()
                    self.hiveLocations.append(self.indexToCoordinate(index))
                    searching = False

    '''
    '''
    def getHiveLocations(self):
        return self.hiveLocations

    '''
    updateFragrance: Cycles through all the tiles in the world and updates their fragrance levels.
        This will be done whenever a food source is fully gathered so the bees don't get tricked.
        Note: I may make it whenever a food source drops by a certain percentage and apply it to only that source.
            I think that's actually what I'll do.
    args: none
    return:
        none
    Notes:
    1.  Cycles through all tiles on board
    2.  If tile contains food, generates a circle of radius where the minimum scent added is 1
    3.  Trims the circle's coordinates to fit the board.
    4.  Takes new list and adds fragrance at a rate of f(x) = fragrance / r^2
    '''
    def updateFragrance(self):
        for tile in self.board:
            if tile.isFoodSource():
                fragrance = tile.getFragrance()
                radius = round((10 * fragrance) ** 0.5)
                origin = self.indexToCoordinate(self.board.index(tile))
                coordList = self.coordinateFilter(self.circleArea(origin, radius))
                for entry in coordList:
                    if entry == origin:
                        appliedFragrance = fragrance
                        continue
                    dist = self.distance(entry, origin)
                    appliedFragrance = fragrance / (dist ** 2)
                    entry.increaseFragrance(appliedFragrance)

    '''
    indexToCoordinate: converts an index into the associated coordinate pair
    args: index
        index is the index of a specific tile
    return:
        returns the (x, y)
    '''
    def indexToCoordinate(self, index):
        x = (index % self._size) + 1
        y = int(((index - (index % self._size)) / self._size) + 1)
        return (x, y)

    '''
    coordinateToIndex: converts a coordinate tuple into the associated index
    args: coordinates
        coordinates is a tuple of (x, y)
    return:
        returns the associated index for that coordinate pair
    '''
    def coordinateToIndex(self, coordinates):
        x, y = coordinates
        x -= 1
        y -= 1
        index = (y * self._size) + x
        return index

    '''
    initializeBoard: generates tiles for every slot on the board
    args: none
    return: none
    '''
    def initializeBoard(self):
        for i in range(self._size ** 2):
            self.board.append(Tile(self.indexToCoordinate(i)))

    '''
    coordinateFilter: takes a list of coordinates, and filters out invalid ones based on board
    args: coordList
        coordList is a list of tuple coordinates in (x, y) format.
    return:
        an updated list of coordinates with all out of bounds coordinates deleted.
    '''
    def coordinateFilter(self, coordList):
        maxX = self._size
        maxY = self._size
        trimmedList = []
        for entry in coordList:
            if entry[0] >= 0 and entry[0] < maxX and entry[1] >= 0 and entry[1] < maxY:
                trimmedList.append(entry)
        return trimmedList

    '''
    getSize: returns the size of one side of the board
    args: none
    return:
        returns the size of the board
    '''
    def getSize(self):
        return self._size

    '''
    '''
    def getFoodLocations(self):
        return self.foodLocations

    '''
    getTile: returns the tile at a location
    args: coordinates
        coordinates is a tuple in (x, y) format indicating the location of the tile
    return:
        returns the tile at the given location
    '''
    def getTile(self, coordinates):
        return self.board[self.coordinateToIndex(coordinates)]

    '''
    getBoardData: Returns a desired set of data from the board
    args: dataType
        dataType is the type of data that is desired from the board.
    return:
        Returns a list of coordinate pairs with the desired data.
    '''
    def getBoardData(self, dataType = "standard"):
        boardData = []
        for tile in self.board:
            index = self.board.index(tile)
            if dataType == "fragrance":
                data = tile.getFragrance()
            elif dataType == "standard":
                data = tile.getFood()
            else:
                print("Invalid dataType")
            entry = [tile.getCoordinates(), data]
            boardData.append(entry)
        return boardData

    '''
    circleArea: generates a list of all the coordinates that fall within a circle of given radius/origin
    args: origin, radius
        origin is a coordinate pair in (x, y) format that marks the center of a circle
        radius is the radius of the circle
    return:
        a list of coordinates that fall within the area of a circle.  Does not account for world border.
    Notes:
        Circle Equation: (x-h)^2 + (y-k)^2 = r^2
        Knowns: y, h, k, r.  solve for x
        x^2 - 2xh + h^2 = r^2 - (y-k)^2 for (y = k - r to y = k + r)
        x^2 -2xh = r^2 - h^2 - (y-k)^2
        x^2 - 2xh = constant - (y-k)^2
        x^2 - 2xh - constant + (y-k)^2 = 0
    '''
    def circleArea(self, origin, radius):
        coordList = []
        h, k = origin
        constant = (radius ** 2) - (h ** 2)
        for y in range(k - radius, k + radius):
            for x in range(h - radius, h + radius):
                if (x ** 2) - (2 * x * h) - constant + ((y - k) ** 2) <= 0.1:
                    coordList.append((x, y))
        return coordList

    '''
    distance: returns the distance between two coordinates
    args: coord1, coord2
        coord1 is the first pair of [y,x] coordinates
        coord2 is the second pair of [y,x] coordinates
    return:
        returns a double value that's the distance between the two points
    '''
    def distance(self, coord1, coord2):
        x1 = coord1[1]
        x2 = coord2[1]
        y1 = coord1[0]
        y2 = coord2[0]
        distance = (((x2 - x1) ** 2) + ((y2 - y1) ** 2)) ** 0.5
        return distance


import torch
import gym
import numpy as np
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from ..memory import Memory
from tqdm.auto import tqdm
import pickle

class BaseAgent(nn.Module):
    def __init__(self, save_path='evals/auto'):
        super().__init__()
        self.memory = Memory()
        self.eval_memory = Memory()
        self.env_steps = nn.Parameter(torch.zeros(1)[0], requires_grad=False)
        self.env_episodes = nn.Parameter(torch.zeros(1)[0], requires_grad=False)
        self.eval_env_steps = nn.Parameter(torch.zeros(1)[0], requires_grad=False)
        self.eval_env_episodes = nn.Parameter(torch.zeros(1)[0], requires_grad=False)
        self.writer = None
        self.save_path = save_path
        self.write_tensorboard_files = False     # is set to True when train is called, so tensorboard file is not written when only evaluation is done

        # if save_path is not None:
        #     self.load(save_path)

    def load(self):
        self.writer = SummaryWriter(self.save_path, flush_secs=10)
        pass

    def write_scalar(self, tag, value, step=None):
        if self.write_tensorboard_files:
            if self.writer is None:
                self.load()
            step = self.env_steps if step is None else step
            self.writer.add_scalar(tag, value, step)

    def write_scalars(self, main_tag, tag_scalar_dict, step=None):
        if self.write_tensorboard_files:
            if self.writer is None:
                self.load()
            step = self.env_steps if step is None else step
            self.writer.add_scalars(main_tag, tag_scalar_dict, step)

    def store_agent(self, store_additional=False):
        if self.save_path is not None:
            save_file = self.save_path + '/agent'
            with open(save_file, 'wb') as f:
                pickle.dump(self.state_dict(), f)
            if store_additional:
                save_file2 = self.save_path + '/agent{}'.format(self.env_steps)
                with open(save_file2, 'wb') as f:
                    pickle.dump(self.state_dict(), f)

    def load_agent_params(self):
        if self.save_path is not None:
            save_file = self.save_path + '/agent'
            with open(save_file, 'rb') as f:
                state_dict = pickle.load(f)
            self.load_state_dict(state_dict)

    def train(self, env, episodes=1000, eval_freq=None, eval_env=None, store_agent_every=100000, **kwargs):
        self.write_tensorboard_files = True
        store_additional = False
        for episode in tqdm(range(episodes)):
            done = False
            state = env.reset()
            while not done:
                action, action_info = self.get_action(state, False, kwargs)
                next_state, reward, done, _ = env.step(action)
                self.memory.append(state=state, action=action, next_state=next_state, reward=reward, done=done, **action_info)
                state = next_state
                done = done.max() == 1
                self.env_steps += 1
                if self.env_steps % store_agent_every == 0:
                    store_additional = True
                self.after_step(done, False, kwargs)

            self.env_episodes += 1

            if eval_freq is not None and self.env_episodes % eval_freq == 0 and ("warmup_steps" not in kwargs or len(self.memory) > kwargs["warmup_steps"]):
                self.store_agent(store_additional=store_additional)
                store_additional = False
                self.eval(env if eval_env is None else eval_env, episodes=1, **kwargs)

        self.store_agent()   # store at the end, so it is stored also when eval is off

    def eval(self, env, eval_episodes=10, render=False, **kwargs):
        for episode in range(eval_episodes):
            done = False
            state = env.reset()
            while not done:
                if render:
                    env.render()
                action, action_info = self.get_action(state, True, kwargs)
                next_state, reward, done, _ = env.step(action)
                self.eval_memory.append(state=state, action=action, next_state=next_state, reward=reward, done=done, **action_info)
                state = next_state
                done = done.max() == 1
                self.eval_env_steps += 1
                self.after_step(done, True, kwargs)

            self.eval_env_episodes += 1

    def get_action(self, state, eval, args):
        raise NotImplementedError()

    def after_step(self, done, eval, args):
        raise NotImplementedError()


# -*- coding: utf-8 -*-
"""
Created on Tue Aug  1 19:11:11 2023

@author: hp
"""

import os
import pandas as pd
import numpy as np
from plotnine import *
from siuba import *

#%%
ruta = "https://raw.githubusercontent.com/scidatmath2020/ML_Py_23/main/data/datos_regresion.csv"
mi_tabla = pd.read_csv(ruta)

mi_tabla

(ggplot(data = mi_tabla) +
 geom_point(mapping=aes(x="caracteristica_1",y="valor_real"),color="red")
)

#%%

variables_independientes = mi_tabla >> select(_.caracteristica_1)
objetivo = mi_tabla >> select(_.valor_real)

#%%

from sklearn.linear_model import LinearRegression
modelo = LinearRegression()

#%%

modelo.fit(X=variables_independientes,y=objetivo) # Aquí se calcula la regresión
modelo.intercept_ #esta es la alpha de la regresión
modelo.coef_ # estas son las beta's de la regresión

#%%

mi_tabla["predicciones"] = modelo.predict(variables_independientes)

#%%

(ggplot(data = mi_tabla) +
 geom_point(mapping=aes(x="caracteristica_1",y="valor_real"),color="blue") +
 geom_point(mapping=aes(x="caracteristica_1",y="predicciones"),color="red") +
 geom_abline(slope=1.85,intercept=5.711) +
 geom_smooth(mapping=aes(x="caracteristica_1",y="valor_real"),color="green")
)

modelo.coef_
modelo.intercept_

#%%

from sklearn import metrics

metrics.mean_absolute_error(mi_tabla["valor_real"],mi_tabla["predicciones"])
metrics.mean_squared_error(mi_tabla["valor_real"],mi_tabla["predicciones"])
np.sqrt(metrics.mean_squared_error(mi_tabla["valor_real"],mi_tabla["predicciones"]))

mi_tabla >> mutate(error = _.valor_real-_.predicciones)

R2 = metrics.r2_score(mi_tabla["valor_real"],mi_tabla["predicciones"])

1-(1-R2)*(50-1)/(50-1-1)

#%%

n = mi_tabla.shape[0]
k = variables_independientes.shape[1]

1-(1-R2)*(n-1)/(n-k-1)