import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from utils import save_variable, train_model, clf_metrics
from random import random
from kmodes.kmodes import KModes
from kmodes.kprototypes import KPrototypes
from sklearn.cluster import SpectralClustering, KMeans
from sklearn.metrics import silhouette_score
from sklearn.metrics import calinski_harabasz_score
from sklearn.metrics import davies_bouldin_score
from sklearn.metrics.pairwise import cosine_similarity

class Cohort:
    def __init__(
        self,
        coef=[0.3, 0.3, 0.3],
        cluster_algo="spectral",
        seed=13,
        model="rdf",
        scorer="f1_micro",
        use_val=True,
        cluster_assign="questions",
        cluster_predict_criteria="cold",
        test_user_data=1,
        folder_str="",
        ohe_qns="",
        worst_cluster=False,  # True if want to purposely assign to wrong cluster
    ):
        """Load parameters"""
        self.coef = coef
        self.cluster_algo = cluster_algo
        self.seed = seed
        self.model = model
        self.scorer = scorer
        self.use_val = use_val
        self.cluster_assign = cluster_assign
        self.cluster_predict_criteria = cluster_predict_criteria
        self.cluster_model_algo = 0  # placeholder, will be updated later
        self.test_user_data = test_user_data
        self.folder_str = folder_str  # e.g., '20201008/iter0_'
        self.ohe = ohe_qns
        self.worst_cluster = worst_cluster

        if self.cluster_algo == "kprototype":
            self.cluster_plot_name = self.cluster_algo
        else:
            self.cluster_plot_name = (
                str(self.coef[0]) + "-" + str(self.coef[1])
            )

        self.dict_user_cluster = {}
        self.dict_cluster_model = {}
        self.dict_cluster_df = {}
        self.dict_cluster_qns = {}

        self.cluster_forced = "-1"  # -1 means automatic clustering
        self.cluster_forced_cutoff = 0  # only used with forced clustering != sex

    def add_sim_matrices(self, dataframe_feedback, dataframe_cross):
        """
        Add similarities/distances matrices with their respective coefficients.
        If the matrix is a distance matrix, an RBF kernel is applied since
        affinity matrices are needed for the subsequent clustering. In an
        affinity matrix, a value of 1 means two items are identical.

        Args:
            dataframe_feedback: Feedback distance matrix
            dataframe_cross: Cross-model performance similarity matrix
        Returns:
            df_total_sim: sum of all similarity matrices with their coefs
                multiplied
        """
        # feedback is a distance, not affinity, matrix, so it's pass through an RBF kernel
        df_feedback_sim = self.RBF_kernel(dataframe_feedback)
        # cross-model performance
        df_cross_model_sim_avg = (dataframe_cross + dataframe_cross.transpose()) / 2

        # alpha * feedback + beta * cross_model
        df_total_sim = (
            self.coef[0] * df_feedback_sim + self.coef[1] * df_cross_model_sim_avg
        )

        return df_total_sim

    def RBF_kernel(self, dataframe, c=0):
        """Non-linear transformation for distance matrices to similarity matrices"""
        std_users = dataframe.std(axis=0)  # standard deviation of distance vector

        i = 0
        for _, row in dataframe.iterrows():
            normalised_dataframe = np.exp(
                -((dataframe - c) ** 2) / (2.0 * std_users[i] ** 2)
            )
            i += 1
        normalised_df = pd.DataFrame(
            normalised_dataframe, index=dataframe.index, columns=dataframe.columns
        )

        return normalised_df

    def find_clusters(
        self, dataframe, qns_idx=[], cat_features=[], k_range=range(2, 11)
    ):
        """
        Run three different cluster metrics.
        Silhouette Score Index (SSI):higher the better
        Calinski Harabasz Score (CHI): higher the better
        Davies Bouldin Index (DBI): lower the better

        Args:
            dataframe: Affinity dataframe (squared matrix)
                (self.cluster_algo == spectral)
                or qns dataframe (self.cluster_algo == kprototype)
            k_range: List of number of clusters to use.
            fig_name: Matplotlib figure name.

        Returns:
            metrics: Dictionary with the metric name as `keys` and
                the cluster metric as `values`.
        """
        X = dataframe.copy()

        ssi_list, chi_list, dbi_list = [], [], []

        for k in k_range:
            if self.cluster_algo == "spectral":
                cluster_labels = self.spectral_clustering(k, X)
            elif self.cluster_algo == "kmeans":
                cluster_labels = self.kmeans_clustering(k, X)
            elif self.cluster_algo == "kprototype":
                cluster_labels = self.kprototype_clustering(k, X, qns_idx, cat_features)

            ssi_list.append(silhouette_score(X, cluster_labels))
            chi_list.append(calinski_harabasz_score(X, cluster_labels))
            dbi_list.append(davies_bouldin_score(X, cluster_labels))

        metrics = {"SSI": ssi_list, "CHI": chi_list, "DBI": dbi_list}
        metrics_x_label = {
            "SSI": "higher better",
            "CHI": "higher better",
            "DBI": "lower better",
        }

        figure, ax = plt.subplots(1, 3, figsize=(20, 6))

        for axis, key_values in zip(ax.flatten(), metrics.items()):
            axis.plot(k_range, key_values[1])
            axis.set_xlabel(metrics_x_label[key_values[0]], fontsize=20)
            axis.set_title(key_values[0], size=20)
            axis.tick_params(length=10, direction="inout", labelsize=20)

        figure.tight_layout()
        figure.savefig(
            f"{self.folder_str}{self.cluster_plot_name}.pdf", bbox_inches="tight"
        )
        plt.close()

        scores = {}
        scores["SSI"] = ssi_list
        scores["CHI"] = chi_list
        scores["DBI"] = dbi_list

        save_variable(f"{self.folder_str}cluster_metrics", scores)

        return scores

    def spectral_clustering(self, n_clusters, dataframe, assign_labels="discretize"):
        """Run Spectral Clustering"""
        df = dataframe.copy()
        spectral = SpectralClustering(
            n_clusters=n_clusters,
            assign_labels=assign_labels,
            affinity="precomputed",
            random_state=self.seed,
        )
        labels = spectral.fit_predict(df)

        # save cluster label for each user
        for label, user in zip(labels, df.columns):  # df's columns are all user_id's
            self.dict_user_cluster[user] = label

        return labels

    def kmeans_clustering(self, n_clusters, dataframe):
        """Run KMeans Clustering"""
        print("Using KMeans")
        df = dataframe.copy()

        # perform clustering
        self.cluster_model_algo = KMeans(n_clusters=n_clusters, random_state=0).fit(df)
        labels = self.cluster_model_algo.predict(df)

        # save cluster label for each user
        for label, user in zip(
            labels, df.index.tolist()
        ):  # df's indices are all user_id's
            self.dict_user_cluster[user] = label

        return labels

    def kprototype_clustering(self, n_clusters, dataframe, qns_idx, cat_features=[]):
        """Run K-Prototype Clustering"""
        df = dataframe.copy()

        # get index of cat_features columns in the dataframe, only for 'sex'
        if "sex" in cat_features and "sex" in df.columns.values:
            cat_idx = [df.columns.get_loc("sex")]
            #             df = df.drop('sex', axis=1)
            # append the index to the other categorical (qns) variables
            total_idx = qns_idx + cat_idx
        else:
            total_idx = qns_idx  # if sex is not used, don't add cat_idx

        # initialise clustering
        if sorted(total_idx) == list(range(0, len(df.columns.values))) or len(
            total_idx
        ) == len(
            df.columns.values
        ):  # sex was dropped
            print("Using KModes")
            kprototype = KModes(
                n_clusters=n_clusters, init="Huang", random_state=self.seed
            )
        else:
            print("Using KPrototype")
            kprototype = KPrototypes(
                n_clusters=n_clusters, init="Huang", random_state=self.seed
            )

        print(
            f"Clustering using {len(df.columns.values)} features: \n{df.columns.values}"
        )
        print(
            f"The categorical features {len(df.columns.values[total_idx])}: \n{df.columns.values[total_idx]}"
        )

        # perform clustering
        self.cluster_model_algo = kprototype.fit(df, categorical=total_idx)
        labels = self.cluster_model_algo.predict(df, categorical=total_idx)

        # save cluster label for each user
        for label, user in zip(
            labels, df.index.tolist()
        ):  # df's indices are all user_id's
            self.dict_user_cluster[user] = label

        return labels

    def find_forced_cluster(self, cluster_forced, dataframe_qns):
        """
        Performs clustering based on `cluster_forced` criteria

        Args:
            cluster_forced: Column name (string) that will determine the clusters
            dataframe-qns: Training qns dataframe with original column names (not encoded)

        Returns: (class variables)
            dict.user_cluster: Dictionary where the keys are `user_id` and the value is its
                respective cluster label
        """
        self.cluster_forced = cluster_forced  # update class variable
        df = dataframe_qns.copy()

        if cluster_forced != "sex":
            # find the cut_off (median) of this feature and insert value in new column
            self.cluster_forced_cutoff = df[cluster_forced].median()
            print(f"{cluster_forced} cluster cutoff {self.cluster_forced_cutoff}")
            df["cluster_col"] = df[cluster_forced].apply(
                lambda x: 0 if x < self.cluster_forced_cutoff else 1
            )
        else:  # == sex
            df["cluster_col"] = df[cluster_forced]

        # update class variable of dictionary {user: cluster_label}
        for index, row in df.iterrows():
            # each row in a qns dataframe consist of a single user
            print(
                f"Training data: {index} has value of {row[cluster_forced]}, so is assigned to {row['cluster_col']}"
            )
            self.dict_user_cluster[index] = row["cluster_col"]

        print(
            f"Available cohors in the train dataset {np.unique(list(self.dict_user_cluster.values()))}"
        )

    def cluster_models(self, dataframe, dataframe_qns):
        """
        Partitions the data according to cluster memebership and trains a model
        with CV
        Args:
            dataframe: Training dataframe already encoded
            dataframe_qns: Training qns dataframe already encoded

        Returns (class variables):
            dict_cluster_model: Dictionary with keys as the cluster label and
                the CV-tuned model as values
            dict_cluster_df: Dictionary with keys as the cluster label and the
                full data + qns + label dataframe of its members
            dict_cluster_qns: Dictionary with keys as the cluster label and the
                qns dataframe of its memebers
        """

        clusters_labels = np.unique(list(self.dict_user_cluster.values()))

        # filter data for each cluster and train model
        for curr_cluster in clusters_labels:
            # get all users in cluster `label`
            user_list = []
            for user, label in self.dict_user_cluster.items():
                if curr_cluster == label:
                    user_list.append(user)

            # update dictionary where the key is the cluster_label and the
            # value is the dataframe
            self.dict_cluster_df[curr_cluster] = dataframe[
                dataframe["user_id"].isin(user_list)
            ]
            self.dict_cluster_qns[curr_cluster] = dataframe_qns[
                dataframe_qns.index.isin(user_list)
            ]

            # train cluster model
            df = self.dict_cluster_df[curr_cluster].drop(
                ["user_id"], axis=1
            )  # holistic model

            # remove the column (from training dataframe) that was used for
            # cluster_forced, if any
            if self.cluster_forced == "sex":
                df = df.drop([self.cluster_forced], axis=1)

            plot_name = (
                self.folder_str + "cluster_" + str(curr_cluster) + "_" + self.model
            )
            tuned_model, _, _ = train_model(
                df,
                False,  # stratified
                self.model,
                self.scorer,
                self.use_val,
                plot_name,
            )
            self.dict_cluster_model[curr_cluster] = tuned_model

    def assign_cluster(self, dataframe, qns_idx=[], cat_features=[]):
        """
        Assign users from `dataframe` to a cluster following the `assign`
        criteria or by using the `cluster_forced` column from the qns dataframe

        Args:
            dataframe: Dataframe of test users
            cat_idx: Indices of categorical columns in dataframe, used with
                K-prototype
        Returns:
            dict_cluster_label: Dictionary with `user_id` as `keys` and the
                assigned label as `values`
        """
        df = dataframe.copy()
        if (
            self.cluster_assign == "questions"
            or self.cluster_forced != "-1"
            or self.cluster_algo in ("kprototype", "kmeans")
        ):
            user_id_list = df.index.tolist()
        else:  # cluster_assign == 'performance'
            user_id_list = df["user_id"].unique()

        clusters_labels = np.unique(list(self.dict_user_cluster.values()))
        dict_cluster_label = {}

        # assign the label to each user in `dataframe`
        if self.cluster_forced != "-1":
            # dataframe consists of qns for test users with indices as user_id
            for user, qns in df.iterrows():
                if self.cluster_forced == "sex":
                    all_clusters = list(clusters_labels).copy()
                    print(f"Available clusters: {all_clusters}")
                    # remove correct cluster from list
                    all_clusters.remove(qns[self.cluster_forced])
                    wrong_cluster = all_clusters[0]  # only works for 2 clusters
                    # assign cluster to user
                    dict_cluster_label[user] = (
                        wrong_cluster
                        if self.worst_cluster
                        else qns[self.cluster_forced]
                    )
                    print(f"User forced cluster is: {qns[self.cluster_forced]}")
                    print(f"Assigned cluster is: {dict_cluster_label[user]}")
                else:
                    all_clusters = list(clusters_labels).copy()
                    print(f"Available clusters: {all_clusters}")
                    # assign based on median value saved in `cluster_forced_cutoff`
                    if self.worst_cluster:
                        # purposely assign to wrong cluster
                        dict_cluster_label[user] = (
                            1
                            if qns[self.cluster_forced] < self.cluster_forced_cutoff
                            else 0
                        )
                        print(f"Wrong label: {dict_cluster_label[user]}")
                    else:
                        # correct cluster, matches the definition in `find_forced_cluster`
                        dict_cluster_label[user] = (
                            0
                            if qns[self.cluster_forced] < self.cluster_forced_cutoff
                            else 1
                        )
                        print(f"Correct label: {dict_cluster_label[user]}")
                    print(f"{self.cluster_forced} value is {qns[self.cluster_forced]}")
                    print(f"Assigned label: {dict_cluster_label[user]}")

        # no need to use the questions, cluster_model can predict directly
        elif self.cluster_algo in ("kprototype", "kmeans"):
            # get index of cat_features columns in the dataframe, only for 'sex'
            if "sex" in cat_features and "sex" in df.columns.values:
                cat_idx = [df.columns.get_loc("sex")]
                # append the index to the other categorical (qns) variables
                total_idx = qns_idx + cat_idx
            else:
                total_idx = qns_idx  # if sex is not used, don't add cat_idx

            # predict label for all the df, where each row it's a user
            if self.cluster_algo == "kprototype":
                predicted_labels = self.cluster_model_algo.predict(
                    df, categorical=total_idx
                )
            elif self.cluster_algo == "kmeans":
                predicted_labels = self.cluster_model_algo.predict(df)

            i = 0
            for user, _ in df.iterrows():
                all_clusters = list(clusters_labels).copy()
                print(f"Available clusters: {all_clusters}")

                right_cluster = predicted_labels[i]

                # remove correct cluster from list
                all_clusters.remove(right_cluster)
                wrong_cluster = all_clusters[0]  # only works for 2 clusters

                # assign cluster to user
                dict_cluster_label[user] = (
                    wrong_cluster if self.worst_cluster else right_cluster
                )
                print(f"User right cluster is: {right_cluster}")
                print(f"Assigned cluster is: {dict_cluster_label[user]}")
                i += 1
            # end user for loop

        elif (
            self.cluster_assign == "performance"
            or self.cluster_assign == "performance-noretrain"
        ):  # warm start
            # use users' data to predict their comfort in each cluster (dist-cross and cross)
            for user in user_id_list:
                df_user = df[df["user_id"] == user]

                if self.cluster_assign == "performance":
                    # only use a fraction of samples per user
                    df_user = df_user.sample(
                        frac=self.test_user_data, axis=0, random_state=self.seed
                    )
                elif self.cluster_assign == "performance-noretrain":
                    # only use some samples per user
                    df_user = df_user.sample(
                        n=self.test_user_data, axis=0, random_state=self.seed
                    )

                df_user = df_user.drop(["user_id"], axis=1)
                acc_list = []

                # test user performance in each cluster' model
                for _, model in self.dict_cluster_model.items():
                    # create feature matrix X and target vector y
                    X = np.array(
                        df_user.iloc[:, 0 : df_user.shape[1] - 1]
                    )  # minus 1 for the target column
                    y = np.array(df_user.iloc[:, -1])
                    # evaluate model
                    y_pred = model.predict(X)
                    acc, _ = clf_metrics(
                        y, y_pred, conf_matrix_print=False, scorer=self.scorer
                    )  # acc
                    acc_list.append(acc)

                # get idx of designated cluster: max performance (best cluster), min performance (worst cluster)
                cluster_idx = (
                    acc_list.index(min(acc_list))
                    if self.worst_cluster
                    else acc_list.index(max(acc_list))
                )
                dict_cluster_label[user] = list(self.dict_cluster_model.keys())[
                    cluster_idx
                ]
                print(f"Best cluster: {acc_list.index(max(acc_list))}")
                print(f"Worst cluster: {acc_list.index(min(acc_list))}")
                print(f"Assigned cluster: {cluster_idx}")

        elif self.cluster_assign == "random":
            # randomly choose one cluster label
            for user in user_id_list:
                dict_cluster_label[user] = random.sample(list(clusters_labels), 1)[0]

        return dict_cluster_label

    def cluster_predict(self, dataframe, dict_test_label):
        """Use cluster model to predict the performance of each user in
        `dataframe`

        Returns:
        dict_acc: Dictionary with `keys` as the `user_id` and cluster model
            performance as `values`
        """
        df = dataframe.copy()
        user_id_list = df["user_id"].unique()
        dict_acc = {}
        dict_acc["f1_micro"] = {}
        dict_acc["cohen_kappa"] = {}

        # remove the column that was used for cluster_forced, if any
        if self.cluster_forced == "sex":
            df = df.drop([self.cluster_forced], axis=1)

        for user in user_id_list:
            # prepare test user data
            df_user = df[df["user_id"] == user]
            df_user = df_user.drop(["user_id"], axis=1)

            # prepare cluster model
            print(f"Predicted cluster for {user} is {dict_test_label[user]}")
            cluster_label = dict_test_label[user]  # test user cluster label
            cluster_model = self.dict_cluster_model[cluster_label]

            # prepare user data
            X = np.array(
                df_user.iloc[:, 0 : df_user.shape[1] - 1]
            )  # minus 1 for the target column
            y = np.array(df_user.iloc[:, -1])

            if self.cluster_predict_criteria == "cold":  # use model to predict
                # evaluate model and save accuracy
                y_pred = cluster_model.predict(X)

            elif self.cluster_predict_criteria == "warm":  # retweak the model
                # get current cluster data and remove the column that was used
                # for cluster_forced, if any
                df_cluster = self.dict_cluster_df[cluster_label]
                df_cluster = df_cluster.drop(["user_id"], axis=1)
                if self.cluster_forced == "sex":
                    df_cluster = df_cluster.drop([self.cluster_forced], axis=1)

                # transform it to matrix X and target vector y
                # minus 1 for the target column
                X_cluster = np.array(df_cluster.iloc[:, 0 : df_cluster.shape[1] - 1])
                y_cluster = np.array(df_cluster.iloc[:, -1])

                # use only a specific ratio of the test user data
                df_user_warm = df_user.sample(
                    frac=self.test_user_data, random_state=self.seed
                )
                # minus 1 for the target column
                X_warm = np.array(df_user_warm.iloc[:, 0 : df_user_warm.shape[1] - 1])
                y_warm = np.array(df_user_warm.iloc[:, -1])

                # concatenate cluster data and test user
                X_cluster_user = np.vstack((X_cluster, X_warm))
                y_cluster_user = np.hstack((y_cluster, y_warm))

                # train new model with concatenated data
                if self.model == "rdf":
                    cluster_model.fit(X_cluster_user, y_cluster_user)
                    # predict on all test user's test data
                    y_pred = cluster_model.predict(X)

            # save performance metrics
            acc_f1_micro, _ = clf_metrics(
                y, y_pred, conf_matrix_print=False, scorer="f1_micro"
            )
            acc_cohen_kappa, _ = clf_metrics(
                y, y_pred, conf_matrix_print=False, scorer="cohen_kappa"
            )
            dict_acc["f1_micro"][user] = acc_f1_micro
            dict_acc["cohen_kappa"][user] = acc_cohen_kappa

        return dict_acc


Movies = [
{"Name":"KGF2" ,"Actor":"Yash", "Release":2022}
]

print("Welcome to Movie Collection!!")
choice = input("Please enter your choice a for add l for list & f for find: ")

while choice != 'q':
    if choice == 'a':
        movie_name = input("Enter movie name: ")
        movie_actor = input("Enter movie actor: ")
        movie_date = input("Enter movie release date: ")

        Movie_new = {
            "Name":movie_name ,"Actor":movie_actor, "Release":movie_date
        }
        Movies.append(Movie_new)
    elif choice == 'l':
        print(Movies)
    elif choice == 'f':
        fname = input("Enter movie name to be found: ")
        for movie in Movies:
            if fname.lower() == movie["Name"].lower():
                mname = movie["Name"]
                mactor = movie["Actor"]
                mdate = movie["Release"]
                print(f"Movie name is {mname} and actor is {mactor} & release date is {mdate}")
    elif choice == 'q':
        print("Quitting application")
        quit()
    else:
        print("Improper input please try again!!")
    choice = input("Please enter your choice a for add l for list & f for find: ")


from flask import Flask, request, jsonify, render_template
from mutagen.mp3 import MP3
from io import BytesIO
from pydub import AudioSegment
from flask import Flask, send_file
from concurrent.futures import ThreadPoolExecutor
from flask import redirect
import pika
import os
import io
import wave

app = Flask(__name__)

executor = ThreadPoolExecutor()
music_data = []
music_files = {}
jobs = []
jobs_inicialize = []
job_id_counter = 1
music_id_counter = 1

class Music:
    def __init__(self, music_id, name, band, instrument_tracks,n_pieces):
        self.music_id = music_id
        self.name = name
        self.band = band
        self.n_pieces = n_pieces
        self.instrument_tracks = instrument_tracks
        self.processed = False
        self.processing_progress = 0

class Job:
    def __init__(self, job_id, music_id, size, time, track, link):
        self.job_id = job_id
        self.music_id = music_id
        self.size = size
        self.time = time
        self.track = track
        self.link = link

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

@app.route('/start', methods=['GET'])
def start():
    return render_template('start.html')

@app.route('/music', methods=['POST','GET'])
def submit_music():
    if request.method == 'POST':
        global music_id_counter
        global music_data
        global music_files
        global jobs_inicialize
        global jobs

        music_file = request.files['musicFile']
        octet_stream = BytesIO(music_file.read())

        audio = MP3(octet_stream)

        if 'TIT2' in audio:
            title = audio['TIT2'].text[0]
        else:
            title = 'Unknown Title'

        if 'TPE1' in audio:
            artist = audio['TPE1'].text[0]
        else:
            artist = 'Unknown Artist'

        music_id = music_id_counter
        selected_instruments = request.form.getlist('instrumentTrack')

        instrument_tracks = []
        for i, instrument in enumerate(selected_instruments):
            track = {
                'name': instrument,
                'track_id': i + 1
            }
            instrument_tracks.append(track)

        music_id_counter += 1

        file_name = f'{music_id}.mp3'
        save_path = os.path.join('Input', file_name)

        music_files[music_id] = octet_stream.getvalue()

        audio_data = BytesIO(music_files[music_id])
        audio = AudioSegment.from_file(audio_data, format="mp3")
        audio.export(save_path, format="mp3")

        #added ----------------------------------
        duration = len(audio)/1000
        n_pieces = int(duration/15)+1
        #----------------------------------------

        music = Music(music_id, title, artist, instrument_tracks,n_pieces)

        music_data.append(music)

        response = {
            'music_id': music_id,
            'name': title,
            'band': artist,
            'instrument_tracks': instrument_tracks
        }
        return redirect('/music')
    else:
        response = []
        for music in music_data:
            response.append({
                'music_id': music.music_id,
                'name': music.name,
                'band': music.band,
                'instrument_tracks': music.instrument_tracks
            })
        return render_template('allmusic.html', music_data=response)

@app.route('/music/<int:music_id>', methods=['POST','GET'])
def process_music(music_id):
    if request.method == 'POST':
        global music_data
        global job_id_counter
        for music in music_data:
            if music.music_id == music_id:
                if music.processed:
                    merged_audio = None
                    for track in music.instrument_tracks:
                        if track["name"] == 'Vocals':
                            audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Vocals_{music.music_id}.mp3')
                            print(" [x]  Got Vocals")
                        if track["name"] == 'Other':
                            audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Other_{music.music_id}.mp3')
                            print(" [x]  Got Other")
                        if track["name"] == 'Bass':
                            audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Bass_{music.music_id}.mp3')
                            print(" [x]  Got Bass")
                        if track["name"] == 'Drums':
                            audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Drums_{music.music_id}.mp3')
                            print(" [x]  Got Drums")
                        if merged_audio is None:
                            merged_audio = audio_segment
                        else:
                            merged_audio = merged_audio.overlay(audio_segment, position=0)
                    merged_audio.export(f'CompleteAudio/merged_{music.music_id}.mp3', format='mp3')
                    print(" [x] Done Merging")
                    return redirect('/music/' + str(music_id))
                if music.processing_progress == 0:
                    print(" [x]  Starting Processing")
                    if music_id in music_files:
                        binary_audio = music_files[music_id]
                        audio_data = BytesIO(binary_audio)
                        #pieces = split_music(audio_data, 8)
                        pieces = split_music(audio_data, music.n_pieces)
                        for piece in pieces:
                            message_header = {'music_id': music_id,
                                            'piece_id': piece['id'],
                                            'selected_instruments': music.instrument_tracks,
                                            'job_id': job_id_counter}
                            jobs_inicialize.append(job_id_counter)
                            job_id_counter += 1
                            send_message(piece['data'],message_header)
                        music.processed = False
                        music.processing_progress += 0.00000001
                        return redirect('/music/' + str(music_id))
                else:
                    return redirect('/music/' + str(music_id))
        return jsonify({'error': 'Music not found'}), 404
    else:
        for music in music_data:
            if music.music_id == music_id:
                instrument_urls = []
                if music.processed:
                    response = {
                        'status': 'processed',
                        'mix_file_url': f'/audio/merged_{music_id}.mp3',
                        'mix_file_name': f'merged_{music_id}.mp3'
                    }
                    instrument_tracks = music.instrument_tracks
                    for track in instrument_tracks:
                        instrument_urls.append({
                            'instrument': track['name'],
                            'url': f'/audio/{track["name"]}_{music_id}.mp3',
                            'name': f'{track["name"]}_{music_id}.mp3'
                        })
                else:
                    if music.processing_progress  < 1:
                        response = {
                        'status': 'processing',
                        'progress': 0
                    }
                    else:
                        response = {
                            'status': 'processing',
                            'progress': music.processing_progress
                        }
                return render_template('music.html', music=response, instrument_urls=instrument_urls)
        return render_template('music.html', music=None, instrument_urls=[])

@app.route('/music/edit/<int:music_id>', methods=['POST'])
def edit_music(music_id):
    selected_instruments = request.form.getlist(f'instrumentTrack_{music_id}[]')

    for music in music_data:
        if music.music_id == music_id:
            music.instrument_tracks = []
            for i, instrument in enumerate(selected_instruments):
                track = {
                    'name': instrument,
                    'track_id': i + 1
                }
                music.instrument_tracks.append(track)
            break
    return redirect('/music')

@app.route('/audio/<path:filename>')
def serve_audio(filename):
    audio_path = 'CompleteAudio/' + filename
    return send_file(audio_path, mimetype='audio/mp3', as_attachment=True)

@app.route('/audioj/<path:filename>')
def serve_audioj(filename):
    audio_path = 'FinalMFIles/' + filename
    return send_file(audio_path, mimetype='audio/mp3', as_attachment=True)

@app.route('/job', methods=['GET'])
def list_jobs():
    return render_template('jobs.html', jobs=jobs)

@app.route('/job/<int:job_id>', methods=['GET'])
def get_job(job_id):
    #sem uso ja se encontra implementado em cima
    """ for job in jobs:
        if job.job_id == job_id:
            return jsonify(job)
    return jsonify({'message': 'Job não encontrado'}) """

@app.route('/reset', methods=['POST'])
def reset_system():
    # reset
    global music_data
    global music_files
    global music_id_counter
    global job_id_counter
    global jobs
    global jobs_inicialize
    music_data = []
    music_files = {}
    jobs = []
    jobs_inicialize = []
    music_id_counter = 1
    job_id_counter = 1

    # Delete all files in FinalMFIles
    folder = '../cd2023-proj-107854_107476_/FinalMFIles/'
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) and not filename.endswith('.txt'):
                os.unlink(file_path)

        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

    # Delete all files in CompleteAudio
    folder = '../cd2023-proj-107854_107476_/CompleteAudio/'
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) and not filename.endswith('.txt'):
                os.unlink(file_path)

        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

    # Delete all files in Input
    folder = '../cd2023-proj-107854_107476_/Input/'
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) and not filename.endswith('.txt'):
                os.unlink(file_path)

        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

    # Delete all files in tracks
    folder = '../cd2023-proj-107854_107476_/tracks/'
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) and not filename.endswith('.txt'):
                os.unlink(file_path)

        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

    #Purge all the queues
    credentials = pika.PlainCredentials('guest', 'guest')
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', virtual_host='broker', credentials=credentials))
    channel = connection.channel()
    channel.queue_purge(queue='task_queue')
    channel.queue_purge(queue='return_queue')

    return redirect('/start')

def send_message(data, header):
    executor.submit(send_msg, data, header)

def send_msg(data, header):
    credentials = pika.PlainCredentials('guest', 'guest')
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', virtual_host='broker', credentials=credentials, heartbeat=30, socket_timeout=300))
    channel = connection.channel()
    channel.queue_declare(queue='task_queue', durable=True)
    channel.basic_publish(exchange='', routing_key='task_queue', body=data,
                          properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE,headers=header))
    connection.close()

def split_music(music_file, num_pieces):
    audio = AudioSegment.from_file(music_file, format="mp3")
    total_duration = len(audio)

    piece_duration = total_duration // num_pieces

    pieces = []
    for i in range(num_pieces):
        # Calculate start and end timestamps for each piece
        start_time = i * piece_duration
        end_time = start_time + piece_duration

        # Extract the audio segment for the current piece
        piece = audio[start_time:end_time]

        # Export the piece to binary data
        binary_data = io.BytesIO()
        piece.export(binary_data, format='mp3')

        piece_dict = {
            'id': i + 1,
            'data': binary_data.getvalue()
        }

        # Add the dictionary to the list
        pieces.append(piece_dict)

    return pieces

def combine_music_pieces(track, music_id, num_pieces):

    print("Combining music pieces... ")
    combined_audio = AudioSegment.empty()
    output_dir = '../cd2023-proj-107854_107476_/FinalMFIles/'

    for piece_id in range(1, num_pieces + 1):
        current_file_path = output_dir + f"{track}_{music_id}_{piece_id}.mp3"
        # Load the audio segment from the file
        audio_segment = AudioSegment.from_mp3(current_file_path)

        # Append the audio segment to the combined audio
        combined_audio += audio_segment

    # Export the combined audio to a filez
    location = '../cd2023-proj-107854_107476_/CompleteAudio/' + f'{track}_{music_id}.mp3'
    combined_audio.export(location, format="mp3")

def return_message(ch, method, properties, body):
    global music_data
    music_id = properties.headers['music_id']
    piece_id = properties.headers['piece_id']
    track = properties.headers['track']
    print("music_id: ", music_id)
    print("piece_id: ", piece_id)
    print("track: ", track)

    job_id = properties.headers['job_id']
    print("job_id: ", job_id)
    size = properties.headers['size_file']
    print("size: ", size)
    time = properties.headers['processed_time']
    print("time: ", time)

    link = f'{track}_{music_id}_{piece_id}.mp3'
    mp3_file_path = f'../cd2023-proj-107854_107476_/FinalMFIles/{track}_{music_id}_{piece_id}.mp3'

    job = Job(job_id, music_id, size, time, track, link)
    jobs.append(job)

    # Convert the WAV audio data to AudioSegment
    audio = AudioSegment.from_wav(BytesIO(body))

    # Export the AudioSegment to MP3 format
    audio.export(mp3_file_path, format='mp3')

    selected_instruments = ['Vocals', 'Other', 'Bass', 'Drums']

    for music in music_data:
        if music.music_id == music_id:
            #music.processing_progress += (1/8 * 100)/(len(selected_instruments))#+1)
            music.processing_progress += (1/(music.n_pieces) * 100)/(len(selected_instruments))
            if music.processing_progress > 100-1/(music.n_pieces):
                music.processed = True
                #combine_music_pieces('merged', music_id, 8) changed
                instrument_tracks = []
                for i, instrument in enumerate(selected_instruments):
                    track = {
                        'name': instrument,
                    }
                    instrument_tracks.append(track)
                for track in instrument_tracks:
                    #combine_music_pieces(track['name'], music_id, 8)
                    combine_music_pieces(track['name'], music_id, music.n_pieces)
                #added ----------------------------------------
                merged_audio = None
                for track in music.instrument_tracks:
                    if track["name"] == 'Vocals':
                        audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Vocals_{music.music_id}.mp3')
                        print(" [x]  Got Vocals")
                    if track["name"] == 'Other':
                        audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Other_{music.music_id}.mp3')
                        print(" [x]  Got Other")
                    if track["name"] == 'Bass':
                        audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Bass_{music.music_id}.mp3')
                        print(" [x]  Got Bass")
                    if track["name"] == 'Drums':
                        audio_segment = AudioSegment.from_mp3(f'CompleteAudio/Drums_{music.music_id}.mp3')
                        print(" [x]  Got Drums")
                    if merged_audio is None:
                        merged_audio = audio_segment
                    else:
                        merged_audio = merged_audio.overlay(audio_segment, position=0)
                merged_audio.export(f'CompleteAudio/merged_{music.music_id}.mp3', format='mp3')
                print(" [x] Done Merging")
                #------------------------------------------------------
            break

    # Acknowledge the message
    ch.basic_ack(delivery_tag=method.delivery_tag)

def start_server():
    credentials = pika.PlainCredentials('guest', 'guest')
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='localhost', virtual_host='broker', credentials=credentials, heartbeat=30,socket_timeout=300))
    channel = connection.channel()
    channel.queue_declare(queue='return_queue', durable=True)
    channel.basic_qos(prefetch_count=1)

    channel.basic_consume(queue='return_queue', on_message_callback=return_message)
    channel.start_consuming()

if __name__ == '__main__':
    executor.submit(app.run)
    executor.submit(start_server())


"""
File to make a figure of how the beam parameters where extracted from the beam profiles.

Author: Birk Emil Karlsen-Bæck
"""

# Imports -------------------------------------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

import utility_files.data_utilities as dut

#plt.rcParams.update({
#        'text.usetex': True,
#        'text.latex.preamble': r'\usepackage{fourier}',
#        'font.family': 'serif',
#        'font.size': 16
#    })

# Options -------------------------------------------------------------------------------------------------------------
PLT_BUNCH = True
PLT_FWHM = True
HOME = False
n_points = 60
turn_points = 99999
n_turns = 100
sample_rate = 10e9
sample_period = 1 / sample_rate

# Functions -----------------------------------------------------------------------------------------------------------
if HOME:
    pass
else:
    meas_dir = f'/Users/bkarlsen/cernbox/SPSOTFB_benchmark_data/data/2021-11-05/profiles_SPS_OTFB_flattop/'

profile_meas = f'MD_104'

profiles_data, profiles_data_corr = dut.import_profiles(meas_dir, [profile_meas])
profile_data_corr = profiles_data_corr[0, :]
profile_data_corr = profile_data_corr.reshape((n_turns, turn_points))

profile = profile_data_corr[0,:]
time_array = np.linspace(0, (turn_points - 1) * sample_period, turn_points)

if PLT_BUNCH:
    plt.figure()
    #plt.title(r'Measured Bunch')

    plt.plot(time_array, profile)

plt.show()


import cv2
import numpy as np

def gettruth(videopath):
    cap = cv2.VideoCapture(videopath)

    _, frame1 = cap.read()

    i = 1

    while (i):

        ret, frame2 = cap.read()

        if ret != True:
            break

        cv2.imshow("",frame2)

        kk = cv2.waitKey(0) & 0xff  # 实时可视化光流图片，（人自己写好了flow2img函数）
        # Press 'e' to exit the video
        if kk == ord('s'):
            cv2.imwrite(videopath[:-4]+"_"+str(i)+".png",frame2)

        cv2.waitKey(0)

        i += 1
gettruth("D:\Study\Datasets\AEXTENSION\Cho80_extension\static_cam\pulseNstatic\\14.avi")


from __future__ import print_function

import pandas as pd

import tensorflow as tf

import numpy as np

from PIL import Image

import os

MAX_DISTANCE=16

if not "FLAGS" in globals():

  tf.flags.DEFINE_string(
      'input_dir', '.../kaggle/nips-2017/cleverhans/examples/nips17_adversarial_competition/dataset/images/', 'Input directory with images.')

  tf.flags.DEFINE_string(
      'attacks_output_dir', '.../kaggle/nips-2017/tmp.ZZZZ/intermediate_results/attacks_output/', '...')

  tf.flags.DEFINE_string(
      'attack_name', 'fgsm', '...')

  FLAGS = tf.flags.FLAGS

  attack_output_dir=FLAGS.attacks_output_dir + FLAGS.attack_name

results=[]
for filepath in tf.gfile.Glob(os.path.join(attack_output_dir, '*.png')):
  with tf.gfile.Open(filepath) as f:

    png_filename=os.path.basename(f.name)

    adv_image = np.array(Image.open(f).convert('RGB')).astype(np.int16)
    original_image_filename = FLAGS.input_dir + png_filename
#    print(f.name)
#    print(original_image_filename)
    original_image=np.array(Image.open(original_image_filename).convert('RGB')).astype(np.int16)

    image_diff=original_image-adv_image

    result={
        "png_filename":png_filename,
        "DISTANCE": np.max(np.abs(image_diff)),
        "STD":np.std(image_diff),
        "MODIFIED PIXELS": np.count_nonzero(image_diff),
        "original_image values between": str( (np.min(original_image), np.max(original_image))),
        "attack output img values between": str( (np.min(adv_image), np.max(adv_image)))
    }

    results.append(result)

results__pd=pd.DataFrame(results)
results__pd.sort_values(by='DISTANCE', inplace=True, ascending=False)

num_errors=sum((results__pd.DISTANCE>MAX_DISTANCE).values)

if num_errors>0:

  print("ERROR!!!. Total errors: ",  num_errors)
  print("See results__pd dataframe")
else:
  print('OK!')


"""
This file converts a Comma separated value spreadsheet of CUES into a text
file that can then be imported to a lighting desk. The original code was created by David Orlando.
Tlaloc Lopez-Watermann added file name choice, export file name choice and cue list select.
"""
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo

filename = ""
fileout= ""
q_list= ""
root = tk.Tk()
root.title('Tkinter Open File Dialog')
root.geometry('300x300')
root.resizable(True,True)
#q_list = Entry(root, width=50)
#q_list.pack()
#q_list.insert(0, "enter the cuelist")
#label = tk.Label(0,text= fileout)
#label.pack(side="bottom")

#select the file to convert.

def select_file():
    global filename
    filetypes = (
    ('text files', '*.csv'),
    ('All files', '*.*'))

    filename = fd.askopenfilename(
    title='Open a file',
    initialdir='/',
    filetypes=filetypes)

#Set the file and location to save to.

def save_file():
        global fileout
        filetypes = (
        ('text files', '*.txt'),
        ('All files', '*.*'))

        fileout = fd.asksaveasfilename(
        title='name to save your text file to for the EOS',
        initialdir='/',
        filetypes=filetypes)
       # Creates label and shows it

#def cue_list_e():
#        listLabel = "Cue List #:" + q_list()
#        myLabel = Label(root, text=listLabel())
 #       myLabel.pack()

# open button
open_button = ttk.Button(
    root,
    text='Open a File',
    command=select_file)

open_button.pack(expand=True)

# open button
save_button = ttk.Button(
    root,
    text='save name and location',
    command=save_file)
save_button.pack(expand=True)

#q_button = ttk.Button(
#   root,
#    text="enter",
#    command=cue_list_e)
#q_button.pack(expand=True)

#("Which cue list would you like to add this to? ")

CUELIST = input("Which cue list would you like to add this to? ")
CSV=open(str(filename)).readlines()
PATCH = open(fileout,'w')

CUE=['Ident 3:0',"\n",'Clear Cues', "\n",'Manufacturer ETC',"\n","Console Eos","\n",'$CueList'+' '+CUELIST,"\n"]
#print(CSV[0])

TITLES=CSV[0]
TITLES=TITLES.split(',')
CUE_INDEX=TITLES.index("CUE")
UT_INDEX=TITLES.index("UT")
DT_INDEX=TITLES.index("DT")
TEXT_INDEX=TITLES.index("Label")
F_INDEX=TITLES.index("F/H")
B_INDEX=TITLES.index("B")
PT_INDEX=TITLES.index("PT")
NOTE_INDEX=TITLES.index("Note")
SCENE_INDEX=TITLES.index("Scene")

CSV.remove(CSV[0])

for x in CSV:
    line=x.split(',',)
    #print(line)
    #Q number
    if ">" in line[CUE_INDEX]:
        line[CUE_INDEX]=line[CUE_INDEX][2:]
    q_num="Cue "
    q_num+=line[CUE_INDEX]
    q_num+=" 1"
    CUE.append(q_num)
    CUE.append("\n")

    #PT
    if line[PT_INDEX] != '':
        if line[PT_INDEX] != '1':
            print("i lie:",line[PT_INDEX])
            del CUE[CUE.index(q_num,len(CUE)-2)]
        q_pt="Part "
        q_pt+=line[PT_INDEX]
        CUE.append(q_pt)
        CUE.append("\n")

    #UT
    if line[UT_INDEX] != '':
        q_ut="Up "
        q_ut+=line[UT_INDEX]
        CUE.append(q_ut)
        CUE.append("\n")
        q_ut="$$TimeUp "
        q_ut+=line[UT_INDEX]
        q_ut+=" 0 0 1"
        CUE.append(q_ut)
        CUE.append("\n")

    #DT
    if line[DT_INDEX]!= '':
        q_dt="Down "
        q_dt+=line[DT_INDEX]
        CUE.append(q_dt)
        CUE.append("\n")
        q_dt="$$TimeDown "
        q_dt+=line[DT_INDEX]
        q_dt+=" 0 0 1"
        CUE.append(q_dt)
        CUE.append("\n")

    #chans
#    CUE.append("Chan 999 0")
#    CUE.append("\n")

    #f
    if line[F_INDEX] != '':
        f_h=line[F_INDEX]
        if f_h[0] == 'F':
            q_f="Followon "
            q_f+=f_h[1:]
            CUE.append(q_f)
            CUE.append("\n")
        if f_h[0] == 'H':
            q_f="$$Hang "
            q_f+=f_h[1:]
            CUE.append(q_f)
            CUE.append("\n")

    #text
    if line[TEXT_INDEX] != '':
        text_dt="Text "
        text_dt+=line[TEXT_INDEX]
        CUE.append(text_dt)
        CUE.append("\n")

    #Block
    if line[B_INDEX] == 'B':
        q_b='$$Block'
        CUE.append(q_b)
        CUE.append("\n")
    if line[B_INDEX] == 'I':
        q_b='$$IntBlock'
        CUE.append(q_b)
        CUE.append("\n")

    #Note
    if line[NOTE_INDEX] != '':
        text_text='$$CueNotes '
        text_text+=line[NOTE_INDEX]
        CUE.append(text_text)
        CUE.append("\n")

    #scene
    if line[SCENE_INDEX] != '':
        scene_text='$$scenetext '
        scene_text+=line[SCENE_INDEX]
        CUE.append(scene_text)
        CUE.append("\n")

#end append
    CUE.append("\n")
#
CUE.append("EndData")
#
for x in CUE:
    PATCH.write(x)       #writes group to file
    print(x)
PATCH.close()

#README FILE
README=open('README.txt','w')
README.write("""
This file converts a Comma separated value spreadsheet of CUES into a text
file that can then be imported to a lighting desk. The original code was created by David Orlando.
Tlaloc Lopez-Watermann added file name choice, export file name choice and cue list select.

For a tutorial on how to best utilize this code please see this video on YouTube. https://www.youtube.com/watch?v=ApFyayLdHcA

Please use the following label headers for you CSV file:
CUE for the cue number
UT for the Up Time
DT for the Down Time
Label for the cue label
F/H for follow hang
B for block cues. Must be a capital B to work
PT for part
Note for the notes
Scene for scenes
'please know that scenes are associated with cues and should not have their own row'.

!!!!!!!!!!ONLY USE ASCII IMPORT WITH NEW SHOWFILES!!!!!!!!!!!!!!!!
ASCII import empties all the data of the current file and repopulates it ONLY with the data in the .txt file.
A good practice is to use this is to import the ASCII to new (merging) show file in an offline editor, then MERGE the cues to a show on the
console. When merging make sure to MERGE ONLY THE CUES. The reason for doing it this way is so the patch at the theatre is not lost.
!!!!!!!!!!ONLY USE ASCII IMPORT WITH NEW SHOWFILES!!!!!!!!!!!!!!!!

in EOS to import:
-File>Import>USITT ASCII
""")
README.close()

#!!!!EOS ASCII COMMANDS FOR REFERENCE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#! Cues
#! $CueList n      All subsequent cues are in cuelist n
#!                 Cue lists can have a TEXT secondary and the following flags:
#! $$IntensityMaster
#! $$Independent
#! $$HTP           HTP
#! $$Assert        Assert
#! $$SoloMode      Solo Mode
#! $$Master        Slider mode, see comment, default=Master
#! $$BackFrom1st   see comment
#! $$GoFromLast    see comment
#! $$Stomp         see comment
#! $$Exclude       see comment
#! $$Control       number, fader mode, min, max, buttons 1-3 id/arg
#! $Cue:           All cues not in the first cue list and those
#!                 with more than one digit after the decimal point
#! $$Text          Cue/Part label
#! $$Assert        Assert
#! $$Block         Block
#! $$IntBlock      IntBlocK
#! The following cue times use this format:  Fade time, Delay time,
#!                 Use default fadetime (1=true, 0=false, M=manual),
#!                 Use default delay time (1=true, 0=false)
#! $$TimeUp        Up times
#! $$TimeDown      Down times
#! $$TimePosition  Focus times
#! $$TimeColor     Color times
#! $$TimeGraphic   Beam times
#! $$Rate          Rate (100 = normal)
#! $$Follow        Auto-follow time counting from previous cue start
#! $$Hang          Auto-follow time counting from previous cue completion
#! $$Action        Stop/Execute, Target
#! $$Action        UDP/MIDI, String
#! $$Curve         Curve (0.01 .. 9999.99)
#! $$Link list/cue Link to or from a cue not in the first list or to
#!                 one with more than one digit after the decimal point
#! $$CueNotes      Cue Notes
#! $$SceneText     Scene Text
#! $$SceneEnd      Scene End flag
#!
#! $$ChanMove      Intensity moves
#! Chan            Tracked levels


# https://www.acmicpc.net/problem/2512

import sys
input =  sys.stdin.readline
n = int(input())

data = list(map(int,input().split()))

total = int(input())

result = 0

if sum(data) <= total:
  print(max(data))
else:
  start = 0
  end = max(data)
  while start <= end:
    mid = (start + end)//2
    sum_data = 0
    for i in data:
      if i <= mid :
        sum_data += i
      else:
        sum_data += mid
    if sum_data <= total :
      start = mid + 1
      result = mid
    else:
      end = mid - 1

  print(result)

Baekjoon_Algorithm/5. Binary Search/2512 예산.py


# Rest Framewrok Django
from rest_framework.decorators import api_view
from rest_framework.response import Response

# Models
from Security.models.resources import Resources

#Serializers
from Security.serializers import ResourceSerializer

@api_view(['GET'])
def find_resources(request):
    resources = Resources.objects.all()
    resources_serializer = ResourceSerializer(resources, many=True)
    return Response(resources_serializer.data)

@api_view(['POST'])
def save_resource(request):
    resources_serializer = ResourceSerializer(data=request.data)
    resources_serializer.is_valid(raise_exception=True)
    resources_data = resources_serializer.data
    resources_saved = Resources.objects.create(**resources_data)
    resources_saved_serializer =  ResourceSerializer(resources_saved)
    return Response(resources_saved_serializer.data)


from django.urls import path

from . import views

urlpatterns = [
    path('', views.suprimentos, name='suprimentos'),
    path('edit_material/<str:id>', views.update_suprimento, name='up_suprimento'),
    path('delete_material/<str:id>', views.delete_suprimento, name='del_suprimento'),

    path('processos/', views.processos, name='processos_licitatorios'),
    path('edit_processo/<str:id>', views.update_processo, name='up_processo'),
    path('delete_processo/<str:id>', views.delete_processo, name='del_processo'),

    path('cpucompleto/', views.cpucompleto, name='cpucompleto'),
]


import sys
from math import radians, sin, cos, sqrt, atan2

def distance_lat_long(lat1, lon1, lat2, lon2):
    try:
        # convert decimal degrees to radians
        lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])

        # Haversine formula
        dlat = lat2 - lat1
        dlon = lon2 - lon1
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * atan2(sqrt(a), sqrt(1-a))
        km = 6371 * c

        return km
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        print("distance_lat_long Failed: %s at %s",
                     str(e), str(exc_tb.tb_lineno))

        return -1

def create_new_shop(data, ShopSerializer):
    try:
        serializer = ShopSerializer(data=data)
        if serializer.is_valid():
            serializer = serializer.save()

        return serializer.id
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        print("create_new_shop Failed: %s at %s",
                     str(e), str(exc_tb.tb_lineno))

        return 0

def update_shop_data(data, shop_obj, ShopSerializer):
    try:
        serializer = ShopSerializer(shop_obj, data=data)

        if serializer.is_valid():
            serializer.save()
        else:
            print("invalid serializer")
            print(serializer.errors)
            return False

        return True

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        print("create_new_shop Failed: %s at %s",
                     str(e), str(exc_tb.tb_lineno))

        return False


"""add_column_active_to_motd
Revision ID: 908382be548a
Revises: cd1903e5f2b0
Create Date: 2022-06-13 12:34:34.981373
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "908382be548a"
down_revision = "cd1903e5f2b0"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("motd", sa.Column("active", sa.Boolean(), nullable=False))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("motd", "active")
    # ### end Alembic commands ###


from django.urls import path
from .views import *

urlpatterns = [
    path('', index, name='home'),
    path('playlist/<int:playlist_id>', show_playlist, name='playlist'),
    path('tracks', tracks, name="tracks"),
    path('new_playlist/', create_playlist, name='new'),
    path('tracks/<int:track_id>', play_track, name="play"),
    path('<int:playlist_id>/track/<int:track_order>', new_track, name="next"),
    path('playlist/<int:playlist_id>/track/<int:order>', play_all,
         name="play_all"),
    path("playlist/<int:playlist_id>/-track/<int:track_id>", delete_track,
         name="delete"),
    path('-playlists/<int:deleted_id>', delete_playlist, name="delete_pl"),
    path('add_track/<int:track_id>', choose_playlist, name="choose"),
    path('add_track/<int:track_id>/playlist/<int:playlist_id>', add_track,
         name="add"),
    path("sort/<int:playlist_id>", sort, name="sort"),
    path("favorite/<int:track_id>", remove_from_favorite, name="remove")
]


import os
import re

class UnknownModelException(Exception):
    '''Exception when wrong model/prefix-name is send to SnapJob'''
    pass

class SnapJob:
    """tasks to work with the model SNAP, SNAPGLOBAL and TRAJ with EC-data"""

    def __init__(self, task, hpc):
        """ construct a snap-job with a task (see snapRemoteRunner)  and a hpc"""
        self.task = task
        self.hpc = hpc

    def get_return_filename(self):
        """return the filename expected by argos when return a file"""
        if self.task.model == "TRAJ":
            return self.task.id + "_TRAJ2ARGOS.zip"
        elif re.match(r"^SNAP.*", self.task.model):
            return self.task.id + "_" + self.task.model + "2ARGOS.zip"
        else:
            raise UnknownModelException("unknown model:" + self.task.model)

    def get_input_files(self):
        """return a list of input-files for each model"""
        if self.task.model == "TRAJ":
            return [self.task.id + "_TRAJ_input"]
        elif re.match(r"^SNAP.*", self.task.model):
            return [self.task.id + "_Rimsterm.xml", self.task.id + "_SNAP_request.xml"]
        else:
            raise UnknownModelException("unknown model:" + self.task.model)

    def job_script(self):
        """return a sge job-script for the different models
        allow for SNAP, SNAPGLOBAL, SNAPNORDIC, SNAPICONGLOBAL
        and       SNAPBOMB, SNAPBOMBGLOBAL, SNAPBOMBNORDIC, SNAPBOMBICONGLOBAL
        """
        if self.task.model.startswith('SNAPBOMB'):
            task_model = self.task.model[8:]
        elif self.task.model.startswith('SNAP'):
            task_model = self.task.model[4:]
        else:
            raise UnknownModelException("unknown model:" + self.task.model)
        if task_model == "":
            metmodel = "nrpa_ec_0p1"
        elif task_model == "GLOBAL":
            metmodel = "nrpa_ec_0p1_global"
        elif task_model == "NORDIC":
            metmodel = "meps_2_5km"
        elif task_model == "ICONGLOBAL":
            metmodel = "icon_0p25_global"
        else:
            raise UnknownModelException("unknown model:" + self.task.model)

        (xmlfile, requestfile) = self.get_input_files()
        argosrequest = ""
        if os.path.exists(os.path.join(self.task.rundir, requestfile)):
            argosrequest = "--argosrequest " + requestfile

        module_to_load = os.getenv("SNAP_MODULE", default="SnapPy/2.1.7")
        mem_options = "h_rss=8G,mem_free=8G,h_data=8G"
        queue = self.task.queue

        # Create qsub script
        script = """#!/bin/bash
#$ -N dsa_bsnap
#$ -S /bin/bash
#$ -v STORE
#$ -j n
#$ -r y
#$ -l h_rt=2:00:00
#$ -l {mem_options}
#$ -M beredskap-fou-kl@met.no
#$ -m a
#$ -P dsa
#$ -pe shmem-1 1
#$ -q {queue}
#$ -sync no
#$ -o {rundir}/$JOB_NAME.$JOB_ID.logout
#$ -e {rundir}/$JOB_NAME.$JOB_ID.logerr

chmod g+rw {rundir}/$JOB_NAME.$JOB_ID.logout
chmod g+rw {rundir}/$JOB_NAME.$JOB_ID.logerr

function send_status()
{{
    rsync --archive --rsh 'ssh {sshoptions}' {statusfile} {scpdestination}
}}

function send_msg()
{{
    code=$1
    msg="$2"
    TS=`date +%Y%m%d%H%M`
    echo $code":"$TS":"$msg > {statusfile} && send_status
}}

module load {module_to_load}

ulimit -c 0
export OMP_NUM_THREADS=1

cd {rundir}
send_msg 102 "Starting run for {model} (timeout: 2h)"
snap4rimsterm --rimsterm {xmlfile} {argosrequest} --dir . --ident {ident}_SNAP --metmodel {metmodel} --bitmapCompress
if [ $? -ne 0 ]; then
    send_msg 409 "{model} output data does not exist, snap4rimsterm failed"
    exit 1;
fi
ncatted -a title,global,o,c,"{ident}" snap.nc
if [ $? -ne 0 ]; then
    send_msg 410 "{model} internal error, ncatted failed"
    exit 1;
fi

# create and deliver the file
zip {zipreturnfile} {ident}_SNAP_conc {ident}_SNAP_dose {ident}_SNAP_depo {ident}_SNAP_prec {ident}_SNAP_wetd {ident}_SNAP_tofa {ident}_SNAP_all.nc {ident}_SNAP_IncidentFile.zip
if [ $? -ne 0 ]; then
    send_msg 410 "{model} internal error, zip failed"
    exit 1;
fi
rsync --archive --rsh 'ssh {sshoptions}' {zipreturnfile} {scpdestination}
if [ $? -ne 0 ]; then
    send_msg 410 "{model} internal error copying data to destination"
    exit 1;
fi
send_msg 202 "Finished extracting {model} data for ARGOS"
exit 0;
""".format(
            rundir=self.task.rundir,
            ident=self.task.id,
            xmlfile=xmlfile,
            argosrequest=argosrequest,
            metmodel=metmodel,
            zipreturnfile=self.get_return_filename(),
            model=self.task.model,
            statusfile=self.task.status_filename(),
            sshoptions=self.task.sshoptions,
            scpdestination=self.task.scpdestination,
            module_to_load=module_to_load,
            queue=queue,
            mem_options=mem_options,
        )

        return script


from django.shortcuts import render,redirect
from .models import Product,description
from django.contrib.auth.decorators import login_required
from cart.cart import Cart
from django.http import HttpResponse
import logging

# Create your views here.
def index(request):

    # d1=description()
    # d1.id=1
    # d1.mtitle="pizza"
    # d1.iimg="p2.png"
    # d1.price=4
    # d1.desc="a dish of Italian origin consisting of a usually round, flat base of leavened wheat-based dough topped with tomatoes, cheese, and often various other ingredients"

    # d2=description()
    # d2.id=2
    # d2.mtitle="Non pizza"
    # d2.iimg="p1.png"
    # d2.price=4
    # d2.desc="a dish of Italian origin consisting of a usually round, flat base of leavened wheat-based dough topped with tomatoes, cheese, and often various other ingredients"

    # d3=description()
    # d3.id=3
    # d3.mtitle="Chicken burger"
    # d3.iimg="f2.png"
    # d3.price=4
    # d3.desc="Juicy, big, loaded with toppings of my choice." "High quality beef medium to well with cheese and bacon on a multigrain bun." "A huge single or triple burger with"

    # d4=description()
    # d4.id=4
    # d4.mtitle="Mexicon burger"
    # d4.iimg="f2.png"
    # d4.price=4
    # d4.desc="Juicy, big, loaded with toppings of my choice.""High quality beef medium to well with cheese and bacon on a multigrain bun." "A huge single or triple burger with"

    # d5=description()
    # d5.id=5
    # d5.mtitle="frinch frys"
    # d5.iimg="f1.png"
    # d5.price=4
    # d5.desc="french fries, also called chips, finger chips, fries, or French pommes frites, side dish or snack made from deep-fried potatoes that have been cut into pices."

    # d6=description()
    # d6.id=6
    # d6.mtitle="pizza"
    # d6.iimg="pizza.jpg"
    # d6.price=4
    # d6.desc="french fries, also called chips, finger chips, fries, or French pommes frites, side dish or snack made from deep-fried potatoes that have been cut into pices."

    # dlists=[d1,d2,d3,d4,d5,d6]

    dlist=description.objects.all()
    plist=Product.objects.all()
    return render (request,"index.html",{'dlists':dlist,'plists':plist})

#cart views

@login_required(login_url="/users/login")
def cart_add(request, id):

    #id=request.POST["id"]
    # logging.debug(id)
    cart = Cart(request)
    product = Product.objects.get(id=id)
    cart.add(product=product)
    return HttpResponse("sucessfully added",content_type="text/plain")

@login_required(login_url="/users/login")
def item_clear(request, id):
    cart = Cart(request)
    product = Product.objects.get(id=id)
    cart.remove(product)
    return redirect("cart_detail")

@login_required(login_url="/users/login")
def item_increment(request, id):
    cart = Cart(request)
    product = Product.objects.get(id=id)
    cart.add(product=product)
    return redirect("cart_detail")

@login_required(login_url="/users/login")
def item_decrement(request, id):
    cart = Cart(request)
    product = Product.objects.get(id=id)
    cart.decrement(product=product)
    return redirect("cart_detail")

@login_required(login_url="/users/login")
def cart_clear(request):
    cart = Cart(request)
    cart.clear()
    return redirect("cart_detail")

@login_required(login_url="/users/login")
def cart_detail(request):
    return render(request, 'cart_detail.html')


from sklearn.decomposition import NMF
from sklearn.feature_extraction.text import TfidfVectorizer

def NMF_topic_extraction(vectorizer = None,documents = None,n_top_words = 10,n_topics = 20):
    """Source:
    https://ahmedbesbes.com/how-to-mine-newsfeed-data-and-extract-interactive-insights-in-python.html
    """

    if vectorizer is None:
        vectorizer = TfidfVectorizer(min_df=5, analyzer='word', ngram_range=(1, 2), stop_words='english')

    tfidf = vectorizer.fit_transform(documents)

    nmf = NMF(n_components=40, random_state=1, alpha=.1, l1_ratio=.5, init='nndsvd').fit(tfidf)

    feature_names = vectorizer.get_feature_names()

    for topic_idx, topic in enumerate(nmf.components_[:n_topics]):
        print("Topic %d:"% (topic_idx))
        print(" | ".join([feature_names[i]
                        for i in topic.argsort()[:-n_top_words - 1:-1]]))


#####CANYY İLE KENAR TESPİTİ##############
'''
import  cv2
from matplotlib import pyplot as plt
import numpy as np

resim=cv2.imread('messi.jpg',0)##gri resim

kenarlar=cv2.Canny(resim,300,300)

plt.subplot(121),plt.imshow(resim,cmap='gray')
plt.title('Resim'),plt.xticks([]),plt.yticks([])

plt.subplot(122),plt.imshow(kenarlar,cmap='gray')
plt.title('Kenarlar'),plt.xticks([]),plt.yticks([])

plt.show()
'''
#####CANYY İLE KENAR TESPİTİ##############

import  cv2
import  numpy as np
from  matplotlib import pyplot as plt

resim=cv2.imread('messi.jpg')
ilk,ilk1=cv2.threshold(resim,127,255,cv2.THRESH_BINARY)
ret,thresh1=cv2.threshold(resim,127,255,cv2.THRESH_BINARY_INV)
ret2,thresh2=cv2.threshold(resim,127,255,cv2.THRESH_TRUNC)
ret3,thresh3=cv2.threshold(resim,127,255,cv2.THRESH_TOZERO)
ret4,thresh4=cv2.threshold(resim,127,255,cv2.THRESH_TOZERO_INV)

basliklar=['Orjinal','BINARY','BINARY INV','TRUNC','TOZERO','TOZERO INV']
resimler=[resim,ilk1,thresh1,thresh2,thresh3,thresh4]

for i in range(0,6):
    plt.subplot(2,3,i+1),plt.imshow(resimler[i],'gray')
    plt.title(basliklar[i])
    plt.xticks([]),plt.yticks([])

plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()


import pybullet as p
import numpy as np
import pybullet_data

p.connect(p.GUI)
p.setGravity(0, 0, -9.8)

p.loadURDF("plane.urdf")
p.loadURDF("obstacle.urdf", [0, 5, 0.1], useFixedBase=1)

p.resetDebugVisualizerCamera(cameraDistance=26, cameraYaw=-180, cameraPitch=-120,
                             cameraTargetPosition=[0, 0, 0])

for i in np.arange(-3.0, 4.0,0.1):
    for j in np.arange(2.0, 9.0,0.1):
        for x in np.arange(-3.0, 4.0,0.1):
            for y in np.arange(2.0, 9.0,0.1):
                if i != x and j != y:
                    res_1 = p.rayTest([i, j, 0.2], [x, y, 0.2])
                    res_2 = p.rayTest([i, j, 0.2], [x, y, 0.2])
                    if res_1[0][0] == -1 and res_2[0][0] == -1:
                        p.addUserDebugLine([i, j, 0.2], [x, y, 0.2], [0, 0, 0])
                        #print("No collision")
                    if (res_1[0][0] != -1 and res_2[0][0] == -1) or (res_1[0][0] == -1 and res_2[0][0] != -1):
                        print("Collision found once only")
                    #if res_1[0][0] != -1 and res_2[0][0] != -1:
                        #print("Collision detected")

while True:
    p.stepSimulation()


#!/usr/bin/python3
"""Module that saves Object to a file"""
import json

def save_to_json_file(my_obj, filename):
    """Funtion that save the object to a file"""
    with open(filename, mode="w", encoding="utf-8") as myfile:
        teststr = json.dumps(my_obj)
        myfile.write(teststr)


#reminders.py

import pytz
import commands

class Reminders(commands.Cog):
    def __init__(self, bot, user_data_manager, scheduler):
        self.bot = bot
        self.user_data_manager = user_data_manager
        self.scheduler = scheduler

    async def register(self, guild):
        # Register the reminder commands
        await guild.add_command(self.reminder)
        await guild.add_command(self.removereminder)

    async def send_reminder(self, user_id, methods):
        user_data = self.user_data_manager.get_user_data(user_id)
        if 'reminder' in user_data:
            for method in methods:
                if method == 'dm':
                    user = self.bot.get_user(user_id)
                    if user:
                        await user.send('This is your scheduled reminder!')
                elif method == 'email':
                    send_email(user_data['email'], 'This is your scheduled reminder!')
                elif method == 'sms':
                    send_sms(user_data['phone'], 'This is your scheduled reminder!')

    @commands.command()
    async def reminder(self, ctx, days: str, time: str, method: str, tz: str = "UTC"):
        # Check if provided timezone is valid
        if tz not in pytz.all_timezones:
            await ctx.send("Invalid timezone provided.")
            return
        # Check if provided days are valid
        valid_days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
        days_list = days.lower().split(',')
        if not all(day in valid_days for day in days_list):
            await ctx.send("Invalid day provided. Please provide days in this format: mon,tue,wed,thu,fri,sat,sun.")
            return
        # Check if provided methods are valid
        valid_methods = ['dm', 'email', 'sms']
        methods_list = method.lower().split(',')
        if not all(m in valid_methods for m in methods_list):
            await ctx.send("Invalid method provided. Available methods: dm, email, sms.")
            return

        # Add user to reminder list
        user_data = self.user_data_manager.get_user_data(ctx.author.id)
        if not user_data:
            user_data = {}
        user_data['reminder'] = {
            'days': days_list,
            'time': time,
            'method': methods_list,
            'timezone': tz
        }
        self.user_data_manager.set_user_data(ctx.author.id, user_data)

        # If user already has a reminder, remove it
        if 'reminder_job' in user_data:
            self.scheduler.remove_job(user_data['reminder_job'])

        # Register a reminder job
        job = self.scheduler.add_job(self.send_reminder, 'cron', day_of_week=days, hour=int(time.split(':')[0]), minute=int(time.split(':')[1]), args=[ctx.author.id, methods_list], timezone=pytz.timezone(tz))
        user_data['reminder_job'] = job.id
        self.user_data_manager.set_user_data(ctx.author.id, user_data)
        await ctx.send("Your reminder has been set!")

    @commands.command()
    async def removereminder(self, ctx):
        user_data = self.user_data_manager.get_user_data(ctx.author.id)
        if user_data and 'reminder_job' in user_data:
            self.scheduler.remove_job(user_data['reminder_job'])
            del user_data['reminder_job']
            self.user_data_manager.set_user_data(ctx.author.id, user_data)
            await ctx.send("Your reminder has been removed.")
        else:
            await ctx.send("You don't have any active reminder.")


allGuests = {'Alice': {'apples': 5, 'pretzels': 12}, 'Bob': {'ham sanwiches': 3, 'apples': 3},
             'Carol': {'cups': 3, 'apple pies': 1}}
for j in allGuests.values():
	print (j)
for j in allGuests.keys():
	print (j)
for j in allGuests.items():
	print (j)
def totalBrought(guests, item):
	numBrought = 0
	for k, v in guests.items():
		numBrought = numBrought + v.get(item, 0)
	return numBrought

print ('Number of things being brought:')
print ('- Apples ' + str(totalBrought(allGuests, 'apples')))


from random import randrange

class RandomizedSet:

    def __init__(self):
        self.map = {}
        self.list = []

    def insert(self, val):
        if val in self.map:
            return False
        self.list.append(val)
        self.map[val] = len(self.list) - 1

    def remove(self, val):
        if val not in self.map:
            return False
        if val == self.list[-1]:
            self.list.pop()
        else:
            idx = self.map[val]
            last_idx = len(self.list) - 1
            # swap the elements
            self.list[idx] = self.list[last_idx]
            self.list.pop()
            self.map[self.list[idx]] = idx
        # if the val is the last element
        del self.map[val]
        return True

    """
    @return: Get a random element from the set
    """
    def getRandom(self):
        i = randrange(len(self.list))
        return self.list[i]

# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param = obj.insert(val)
# param = obj.remove(val)
# param = obj.getRandom()


from multiprocessing import Pool, cpu_count
from joblib import Parallel, delayed

def parfor(task, processes, args):
    """Parallel For.

    Applies a function *task* to each argument in *args*,
    using a pool of concurrent processes.

    Parameters
    ----------
    task : function
        Function object that should process each element in args.
    processes : int
        Maximum number of concurrent processes to be used in the pool.
        If higher than multiprocessing.cpu_count(),
        all processor's cores will be used.
    args : any
        Iterable object, where each element is an argument for task.

    Returns
    -------
    out : iterable
        Iterable object containing the output of
        task(arg) for each arg in args.

    """
    # Don't try to spawn more processes than available CPUs
    num_cores = min(cpu_count(), processes)

    pool = Pool(processes=num_cores)
    return pool.map(task, args)

def parfor2(task, n_reps, processes, *args):
    """Parallel For.

    Run function `task` using each argument in `args` as input,
    using a pool of concurrent processes.
    The `task` should take as first input the index of parfor iteration.

    Parameters
    ----------
    task : function
        Function object that should process each element in `args`.
    n_reps : int
        Number of times the `task` should be run.
    processes : int
        Maximum number of concurrent processes to be used in the pool.
        If higher than `multiprocessing.cpu_count()`,
        all processor's cores will be used.
    args : any, optional
        Tuple with input arguments for `task`.

    Returns
    -------
    out : list
        List with iteration output, sorted (rep1, rep2, ..., repN).

    """
    # Don't try to spawn more processes than available CPUs
    num_cores = min(cpu_count(), processes)

    return Parallel(n_jobs=num_cores, backend='multiprocessing')(
        delayed(task)(i, *args) for i in range(n_reps))

if __name__ == "__main__":

    from math import factorial
    arguments = range(10)
    res = [factorial(z) for z in arguments]
    parres = parfor(factorial, 2, arguments)
    print(parres)

    def element_wise_power(idx, list_of_scalars):
        print("Repetition {:} started...".format(idx))
        list_of_scalars_pow = []
        for obj_idx, obj in enumerate(list_of_scalars):
            list_of_scalars_pow.append(list_of_scalars[obj_idx]**idx)
        print("Repetition {:} ended...".format(idx))
        return list_of_scalars_pow

    parout = parfor2(element_wise_power, 4, 2, ([j for j in range(10)]))
    print(parout)


# train.py

import time
import torch
import torch.optim as optim
import plugins

class Trainer:
    def __init__(self, args, model, criterion, evaluation):

        self.args = args
        self.total_classes = args.total_classes
        self.nclasses_A = args.nclasses_a
        self.nclasses_T = args.nclasses_t

        self.model = model
        self.criterion = criterion
        self.evaluation = evaluation

        self.device = args.device
        self.nepochs = args.nepochs
        self.batch_size = args.batch_size_train

        self.lr_e = args.learning_rate_e
        self.optim_method_e = args.optim_method_e
        self.optim_options_e = args.optim_options_e
        self.scheduler_method_e = args.scheduler_method_e
        self.scheduler_options_e = args.scheduler_options_e

        self.w=args.w

        self.optimizer = {}

        self.optimizer['Encoder'] = getattr(optim, self.optim_method_e)(
            filter(lambda p: p.requires_grad, list(self.model['Encoder'].parameters())+list(self.model['Target'].parameters())),
            lr=self.lr_e, **self.optim_options_e)

        self.scheduler = {}
        if self.scheduler_method_e is not None:
            self.scheduler['Encoder'] = getattr(optim.lr_scheduler, self.scheduler_method_e)(
                self.optimizer['Encoder'], **self.scheduler_options_e
            )

        # for classification
        self.X = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )
        self.Y = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )
        self.sensitives_org = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )
        self.labels_org = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )

        self.sensitives = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )
        self.labels = torch.zeros(
            self.batch_size,
            dtype=torch.float,
            device=self.device
        )

        self.params_loss = ['Loss', 'P_M*A','Loss_Target','Loss_r','Accuracy_Target']

        # monitor training
        self.monitor = plugins.Monitor()
        self.params_monitor = {
            'Loss': {'dtype': 'running_mean'},
            'P_M*A': {'dtype': 'running_mean'},
            'Loss_Target': {'dtype': 'running_mean'},
            'Loss_r': {'dtype': 'running_mean'},
            'Accuracy_Target': {'dtype': 'running_mean'},
           }

        self.monitor.register(self.params_monitor)

        # display training progress
        self.print_formatter = 'Train [%d/%d][%d/%d] '
        for item in self.params_loss:
            self.print_formatter += item + " %.4f "

        self.evalmodules = []
        self.losses = {}

    def model_train(self):

        self.model['Encoder'].train()
        self.model['Target'].train()

    def train(self, epoch, dataloader, reg,writer):
        dataloader = dataloader['train']
        self.monitor.reset()
        self.model_train()

        for i, (X, Y, labels, sensitives) in enumerate(dataloader):

            ############################
            # Update network
            ############################
            batch_size=X.size(0)

            self.labels_org.resize_(labels.size()).copy_(labels)
            self.sensitives_org.resize_(sensitives.size()).copy_(sensitives)

            #### ARL-used label####
            labels = torch.zeros(batch_size, self.total_classes).scatter_(1, labels.unsqueeze(1).long(), 1)
            sensitives = torch.zeros(batch_size, self.total_classes).scatter_(1, sensitives.unsqueeze(1).long(), 1)
            self.labels = labels.to(torch.float).to(self.device)
            self.sensitives = sensitives.to(torch.float).to(self.device)

            self.X = X.to(self.device)
            self.Y = Y.to(self.device)

            in_c,self.outputs_E,rx,ry= self.model['Encoder'](self.X,self.Y)

            self.outputs_E= self.outputs_E /torch.norm(self.outputs_E,dim=1).unsqueeze(1).repeat(1,self.outputs_E.shape[1])

            #### adversarial loss ####
            loss_a = self.criterion['Encoder'](self.outputs_E, self.sensitives, self.labels, reg,
                                                                 self.device,
                                                                 self.args.sigma)

            #### regression loss ####
            loss_r=self.criterion['regression'](rx/batch_size,ry/batch_size)

            #### classification loss ####
            outputs_c = self.model['Target'](self.outputs_E,in_c)
            loss_c = self.criterion['Target'](outputs_c, self.labels_org.long())

            loss=loss_a*self.w+loss_r+loss_c

            self.optimizer['Encoder'].zero_grad()
            loss.backward()
            self.optimizer['Encoder'].step()

            acc = self.evaluation['Target'](outputs_c, self.labels_org.long())
            acc = acc.item()

            loss_a = loss_a.item()
            loss_r=loss_r.item()
            loss_c=loss_c.item()
            loss = loss.item()

            ############################
            #   Evaluating
            ############################

            self.losses['Loss'] = loss
            self.losses['P_M*A'] = loss_a
            self.losses['Loss_Target'] = loss_c
            self.losses['Loss_r'] = loss_r
            self.losses['Accuracy_Target'] = acc

            self.monitor.update(self.losses, batch_size)

            # print batch progress
            print(self.print_formatter % tuple(
            [epoch + 1, self.nepochs, i, len(dataloader)] +
            [self.losses[key] for key in self.params_monitor]))

        loss = self.monitor.getvalues()

        #### visualization ####

        if epoch % 10 == 0:
            writer.add_scalar('training loss',
                              loss['Loss'],
                              epoch),
            writer.add_scalar('P_M*A train',
                              loss['P_M*A'],
                              epoch),
            writer.add_scalar('Loss_Target train',
                              loss['Loss_Target'],
                              epoch),
            writer.add_scalar('Loss_r train',
                              loss['Loss_r'],
                              epoch),
            writer.add_scalar('training acc',
                              loss['Accuracy_Target'],
                              epoch)

        if self.scheduler_method_e is not None:
            self.scheduler['Encoder'].step()

        return loss


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 15 09:46:58 2023

@author: tijmen
"""

import imageio
import matplotlib as mpl
import pandas as pd
from pyproj import CRS
import os
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.colors as colors
import geopandas as gpd
import cartopy.feature as cfeature

os.chdir('/Users/tijmen/Documents/Tijmen/Climate_Physics/Thesis_local/Python_scripts')

import Thesis_Functions.calculations as Calculations
import Thesis_Functions.data as Data
import Thesis_Functions.plotting as Plotting

crs_racmo = CRS.from_proj4("-m 57.295779506 +proj=ob_tran +o_proj=latlon +o_lat_p=6.6 +lon_0=180.0")
crs_stations = CRS.from_string("EPSG:4326")

years = [2002, 2003, 2004]
months=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
month_names = ['January', 'February', 'March', ' April', 'May', 'June',
               'July', 'August', 'September', 'October', 'November', 'December']

"""Directories"""

datadir = "/Volumes/Tijmen/Master-Thesis/Data/"

in_situ_data_directory = datadir+'In_situ_data/'
racmo_arctic_data_directory = datadir+'RACMO_2.4/PXARC11/2001/'
snow_cover_extend_measure_dir = datadir+'Snow_cover_Measure/'
mask_directory = datadir+'Mask/'
fig_save_directory = '/Users/tijmen/Desktop/Figures_Thesis/'

images = []

tilefrac6 = xr.open_dataset(racmo_arctic_data_directory+'NC_DEFAULT/tilefrac6.KNMI-2001.PXARC11.RACMO24_1_complete6_UAR_q_noice_khalo6_era5q.DD.nc')['tilefrac6']
tilefrac7 = xr.open_dataset(racmo_arctic_data_directory+'NC_DEFAULT/tilefrac7.KNMI-2001.PXARC11.RACMO24_1_complete6_UAR_q_noice_khalo6_era5q.DD.nc')['tilefrac7']

rlon= tilefrac6.rlon.values
rlat= tilefrac6.rlat.values

forest = tilefrac6.isel(time=0)+tilefrac7.isel(time=0)

forest = forest.where(forest>0.4).squeeze()

for year in years:

    year = str(year)
    print('Starting calculations for year: ' + year)
    """Year specific directories"""

    in_situ_data_directory_year = in_situ_data_directory + year + '/'
    in_situ_data_directory_year_calculated = in_situ_data_directory + year + '/Calculated/'

    ds = xr.open_dataset(racmo_arctic_data_directory + 'NC_DEFAULT/sndp.KNMI-2001.PXARC11.RACMO24_1_complete6_UAR_q_noice_khalo6_era5q.DD.nc')
    racmo_sndp = ds['sndp'].sel(time=slice(year + '/01/01', year + '/12/31')).mean(dim='time').squeeze()
    rlon = ds.rlon.values
    rlat = ds.rlat.values

    station_locs = pd.read_csv(in_situ_data_directory_year_calculated + 'station_in_arctic_domain_' + year + '.csv',
                               index_col=0)

    for month in months:
        print('Month ' + str(month))

        monthdir_in_situ = in_situ_data_directory_year_calculated+'snow_depth/'+ 'month_' + str(month)
        monthdir_racmo = racmo_arctic_data_directory + year + 'month_' + str(month)

        """Import statistics"""

        vmin= -500
        vmax = -400

        station_statistics = pd.read_csv(monthdir_in_situ+'/Calculated_statistics.csv', index_col=0)
        bias = station_statistics.loc['bias']
        racmo_mean = np.round(np.mean(station_statistics.loc['racmo_mean'].values))
        in_situ_mean = np.round(np.mean(np.isnan(station_statistics.loc['in_situ_mean'].values))*100)

        bounds = np.linspace(vmin, vmax, 10)

        """Plot basemap"""

        norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256, extend='both')
        cmap = 'seismic'
        fig = plt.figure(figsize=(12, 10))
        ax = fig.add_subplot(1, 1, 1, projection=ccrs.Stereographic(central_longitude=0., central_latitude=90.) )

        station_lat = np.array(station_locs.loc['latitude'].values).astype(float)
        station_lon = np.array(station_locs.loc['longitude'].values).astype(float)
        gdf = gpd.GeoDataFrame(bias, geometry=gpd.points_from_xy(station_lon, station_lat,
                                                                 crs=ccrs.Stereographic(central_latitude=90.,
                                                                                central_longitude=0.)))

        ax.add_feature(cfeature.OCEAN)
        ax.coastlines(resolution='50m')
        ax.add_feature(cfeature.LAND)

        ax.contourf(rlon, rlat, forest, vmin = 0, vmax = 2, cmap='Greens', transform = ccrs.RotatedPole(pole_longitude=ds.rotated_pole.grid_north_pole_longitude,
                            pole_latitude=ds.rotated_pole.grid_north_pole_latitude))

        stat_dots = gdf.plot(column='bias', ax=ax, markersize=20, cmap=cmap, norm=norm, transform=ccrs.PlateCarree())

        ax.set_xlim(-3939996.3398490055, 4199452.516011998)
        ax.set_ylim(-4171531.5955094383, 4399068.623679058)

        #axpos = ax.get_position()
        #pos_x = axpos.x0 + axpos.width + 0.01
        #pos_y = axpos.y0
        #cax_width = 0.01
        #cax_height = axpos.height
        #pos_cax = fig.add_axes([pos_x, pos_y, cax_width, cax_height])
        #plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap), cax=pos_cax, label='Bias [cm]')

        #ax.set_title(month_names[month-1] + ' ' + year, size='xx-large')

        plt.savefig(fig_save_directory+year+'/month_'+str(month)+'_bias_plot.png')

        plt.close(fig)

        images.append(imageio.v3.imread(fig_save_directory+year+'/month_'+str(month)+'_bias_plot.png'))
        images.append(imageio.v3.imread(fig_save_directory + year + '/month_' + str(month) + '_bias_plot.png'))
        images.append(imageio.v3.imread(fig_save_directory + year + '/month_' + str(month) + '_bias_plot.png'))

        del gdf

imageio.mimsave(fig_save_directory+'movie.gif', images)


import sys

from pprint import pprint

sys.stdin = open("daily/230227/1240.txt")

# 비밀번호 형식이 맞는지 검사하는 함수
def vali(st):
    even = 0
    odd = 0
    ans = 0
    for i in range(len(st)):
        if i % 2 == 0:
            even += int(st[i])
        else:
            odd += int(st[i])
    # 짝수 홀수를 나눠서 10으로 나누어 떨어지면 True 리턴
    if (even + odd * 3) % 10 == 0:
        return True
    else:
        return False

# 문자열을 각 숫자로 변환하는 함수
def trans(st):
    print(st)
    if st[::-1] == "0001101":
        return "0"
    elif st[::-1] == "0011001":
        return "1"
    elif st[::-1] == "0010011":
        return "2"
    elif st[::-1] == "0111101":
        return "3"
    elif st[::-1] == "0100011":
        return "4"
    elif st[::-1] == "0110001":
        return "5"
    elif st[::-1] == "0101111":
        return "6"
    elif st[::-1] == "0111011":
        return "7"
    elif st[::-1] == "0110111":
        return "8"
    elif st[::-1] == "0001011":
        return "9"

# 문자열을 56번째부터 0번째 까지 구함
def deco(i, j):
    st = ""
    ans = ""
    for x in range(56):
        st += arr[i][j - x]
        # 56개를 모두 찾았으면 숫자로 변환한 것을 리턴
        if x == 55:
            ans += trans(st)
            return ans
        # 길이가 7이 될 때마다 숫자로 변환하고 다시 초기화
        if len(st) == 7:
            ans += trans(st)
            st = ""

T = int(input())
for tc in range(1, T + 1):
    arr = []
    answer = 0
    st = ""
    A, B = map(int, input().split())
    for i in range(A):
        lst = list(input())
        arr.append(lst)
    # 리스트를 거꾸로 돌면서 1을 찾고 찾은 후 함수 호출
    for i in range(A):
        for j in range(B - 1, -1, -1):
            if arr[i][j] == "1":
                st = deco(i, j)
                if len(st) != 0:
                    break
        # 문자열이 0 이아니면 반복문을 탈출
        if len(st) != 0:
            break
    k = vali(st)
    # 비밀번호 형식이 맞다면 값을 출력
    if k == True:
        for i in range(len(st)):
            answer += int(st[i])
        print(f"#{tc} {answer}")
    else:
        print(f"#{tc} {answer}")


# enconding: utf-8
from django.db.models import Model, DateTimeField, CharField
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.utils.safestring import mark_safe

VALIDATION_PROCESS_STATUS = (
    ("VALIDATION_IN_PROCCESS", _("Validation in proccess")),
    ("VALIDATED", _("Validated")),
    ("SUSPENDED", _("Suspended")),
)

class ModelDateTimeField(Model):
    """
    Helper to manage in one place the "timing" of creating a new object.

    With the pass of time we can add <deleted_at> to check soft deletes or updated_at,
    and we dont have to add this new fields on every model, we only have to add these
    new fields in this abstract model to spread to all all models that inherit from this
    """

    created = DateTimeField(verbose_name=_('Created'), default=timezone.now)

    class Meta:
        abstract = True

class StatusRequirementDisplay(Model):
    """
    Abstract model to display a friendly tag validation status proccess
    """

    status = CharField(_("Status"), choices=VALIDATION_PROCESS_STATUS, blank=True,
                       max_length=32, default="VALIDATION_IN_PROCCESS")

    def get_status(self):
        background = '#9e9e9e'
        status = self.get_status_display()

        if self.status == 'VALIDATION_IN_PROCCESS':
            background = '#e89657'

        if self.status == 'SUSPENDED':
            background = '#9e9e9e'

        if self.status == 'VALIDATED':
            background = '#11ad36'

        return mark_safe(F"
{status}
")

    get_status.allow_tags = True
    get_status.short_description = 'Status'

    class Meta:
        abstract = True


import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

class OutputVisualizer:
    def __init__(self, model_input, model_output):
        self.input = model_input
        self.output = model_output

    def preprocess_output(self, model_input, model_output):
        return pd.DataFrame(
            [
                pd.Series(
                    {
                        "seat": assignment[0],
                        "person": assignment[1],
                        "team": model_input.person_team_dict[assignment[1]],
                        "x_coor": model_input.seat_to_coordinates[assignment[0]][0],
                        "y_coor": model_input.seat_to_coordinates[assignment[0]][1],
                    }
                )
                for assignment in model_output.assignments
            ]
        )

    def plot_solution(self, plot_df):
        """Plot workplace solution."""
        plt.figure(figsize=(12, 8))
        sns.scatterplot(
            x=plot_df["x_coor"],
            y=plot_df["y_coor"],
            hue=plot_df["team"],
            palette="deep",
            s=150,
        )
        plt.xlim((min(plot_df["x_coor"]) - 1.5, max(plot_df["x_coor"]) + 1.5))
        plt.ylim((min(plot_df["y_coor"]) - 1.5, max(plot_df["y_coor"]) + 1.5))
        plt.xlabel("")
        plt.ylabel("")
        plt.title(
            "Workplace Seating Optimal Solution\n",
            fontsize=25,
            fontweight="bold",
            fontname="Trebuchet MS",
            color="#001F5B",
        )
        for i in range(plot_df.shape[0]):
            plt.text(
                x=plot_df["x_coor"][i] + 0.3,
                y=plot_df["y_coor"][i] + 0.3,
                s=plot_df["person"][i],
                fontdict=dict(color="white", size=10),
                bbox=dict(facecolor="#001F5B", alpha=0.5),
            )

        ax = plt.gca()
        ax.axes.xaxis.set_visible(False)
        ax.axes.yaxis.set_visible(False)

    def visualize_output(self):
        """Visualize model solution in a plot."""
        plot_df = self.preprocess_output(self.input, self.output)
        self.plot_solution(plot_df)


import json
from dateutil import parser
from datetime import timedelta

# 原数据，格式有误
test_str_source = "{'姓名': '秦柯', '年龄': 39, '出生日期': '1979-05-05', '状态': true}"
# 修改后格式正确（手动）
test_str = '{"姓名": "秦柯", "年龄": 39, "出生日期": "1979-05-05", "状态": true}'

test_dict = json.loads(test_str)
print(test_dict)

test_dict['出生日期'] = (parser.parse(test_dict['出生日期']) + timedelta(days=365 * 10)).strftime('%Y-%m-%d')
print(test_dict)

with open('json_test.json', 'w', encoding='utf-8') as f:
    json.dump(test_dict, f, ensure_ascii=False)

with open('json_test.json', 'r', encoding='utf-8') as f:
    test_dict_read = json.load(f)

print(test_dict_read)


#!/usr/bin/python
import Image
import ImageDraw
import subprocess
import random
import wx

class Targeter(wx.Frame):
    def __init__(self):
        super(Targeter, self).__init__(None, -1, 'Winner targeter', size=(250, 100))
        self.base_image_path = 'picture.jpg'
        self.image_path = 'picture_modified.jpg'

        button = wx.Button(self, -1, 'Target!')

        button.Bind(wx.EVT_LEFT_UP, self.on_target)

    def on_target(self, event):
        image = Image.open(self.base_image_path)
        draw = ImageDraw.Draw(image)

        width, height = image.size

        x = random.randint(0, width)
        y = random.randint(0, height)
        size = width / 30
        line_width = size / 20
        line_color = '#00FF00'

        draw.line((x, y - size, x, y + size), fill=line_color, width=line_width)
        draw.line((x - size, y, x + size, y), fill=line_color, width=line_width)
        image.save(self.image_path)

        subprocess.call(['eog', self.image_path])

def run():
    app = wx.App()

    targeter = Targeter()
    targeter.Show()

    app.MainLoop()

if __name__ == '__main__':
    run()