import pickle
import os
import sys
import time
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
from sklearn import preprocessing, metrics

import utilities
import videoPlayer as vp

class DataHandler:

    def __init__(self, basePath=None, csvPath=None, classifierPath=None, showViz=False, store=False, axisLabels=False, baseOutputPath=None):

        if store and baseOutputPath is None:
            raise ValueError("Cannot store files without output Path!")

        if classifierPath is not None:
            self.classifier = pickle.load(open(classifierPath, 'rb'))
        else:
            self.classifier = None

        self.micArray = utilities.loadMicarray()

        self.store = store
        self.showViz = showViz
        self.axisLabels = axisLabels
        self.basePath = basePath
        self.baseOutputPath = baseOutputPath
        if baseOutputPath is not None:
            utilities.makeDirectory(baseOutputPath)

        if basePath is not None:
            self.logTable = pd.read_csv(os.path.join(basePath, 'DataLog.csv'))
            if not all(elem in self.logTable.columns.to_list() for elem in ['ID', 'Environment', 'Class', 'T0']):
                raise ValueError('Provided DataLog.csv doesnt contain all required columns')

            if csvPath is not None:
                filterCsv = pd.read_csv(csvPath)
                if not 'ID' in filterCsv.columns.to_list():
                    raise ValueError('Provided filtering csv doesnt contain the required column ID')
                self.logTable = self.logTable[self.logTable['ID'].isin(filterCsv['ID'])]
                self.logTable = self.logTable.reset_index(drop=True)

        if store:
            self.videoPath = os.path.join(baseOutputPath, 'VideoOverlays')
            self.plotPath = os.path.join(baseOutputPath, 'Plots')
            utilities.makeDirectory(self.videoPath)
            utilities.makeDirectory(self.plotPath)
        else:
            self.videoPath = None
            self.plotPath = None

    def loadResultTable(self, path):
        self.logTable = pickle.load(open(path, 'rb'))

    def setLogTable(self, newTable):
        # join results table with the data table
        self.logTable = self.logTable.join(newTable)
        self.logTable['T0'] -= 10

    def padData(self):

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        Dall = Lall - Tall

        # iterate over all rows, we want to pad first three elements, features confidences and the classifcation results
        for index, rows in self.logTable.iterrows():
            Tcurr = rows['T0']
            Lcurr = rows['VideoLength']
            Dcurr = Lcurr - Tcurr

            padBefore = Tall - Tcurr
            padAfter = Dall - Dcurr

            # check if padding was already carried out and then pad the data
            if rows['ClassificationResults'].shape[0] == Lcurr:
                self.logTable.at[index, 'ClassificationResults'] = np.pad(rows['ClassificationResults'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'ConfidenceResults'] = np.pad(rows['ConfidenceResults'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'Features'] = np.pad(rows['Features'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'VisualDetections'] = np.pad(rows['VisualDetections'], ((padBefore, padAfter)))

    def postProcessing(self):
        self.padData()
        self.plotConfidences()
        self.plotAbsolutes()
        self.plotVisualBaseline()
        self.plotMeanFeatures()
        plt.show()

    def loopOverRecordings(self):

        if self.classifier is not None and self.basePath is not None and self.baseOutputPath is not None:
            newColumns = ['Features', 'ConfidenceResults', 'ClassificationResults', 'VisualDetections', 'InferenceTime', 'VideoLength']
            newTable = pd.DataFrame(columns=newColumns)

            tic = time.perf_counter()

            for index, rows in self.logTable.iterrows():
                identifier = str(rows['ID'])
                location = rows['Environment']
                label = rows['Class']
                tZero = rows['T0']

                print('Starting Session {} with class {} at location {} and T0 of {}, number {} out of {}'.format(identifier, label, location, tZero, index+1, self.logTable.shape[0]))
                samplePath = os.path.join(self.basePath, location, label, identifier)

                session = vp.VideoPlayer(samplePath, self.classifier, self.micArray, self.showViz, self.store, self.videoPath)
                featData, confidenceResults, classificationResults, visualDetections, inferenceTime, videoLength = session.loopOverFrames()

                print('Inference ran for {:.2f} seconds for the {:.1f} second long Clip\n\n'.format(inferenceTime, videoLength/10))

                newTable = newTable.append({'Features': featData, 'ConfidenceResults': confidenceResults, 'ClassificationResults': classificationResults, 'VisualDetections': visualDetections, 'InferenceTime': inferenceTime, 'VideoLength': videoLength}, ignore_index=True)

            toc = time.perf_counter()
            print('Inference ran successfully for all recordings in {} [HH:MM:SS]'.format(time.strftime("%H:%M:%S", time.gmtime(int(round(toc-tic))))))

            # append the results of inference to the log table, do post processing and then dump the data
            self.setLogTable(newTable)
            pickle.dump((self.logTable), open(os.path.join(self.baseOutputPath, 'ResultTable.obj'), 'wb'))
            self.postProcessing()
            pickle.dump((self.logTable), open(os.path.join(self.baseOutputPath, 'ResultTable.obj'), 'wb'))

        else:
            print('Classifier, Datapath or output path is missing, cannot loop over Recordings')

    def plotConfidences(self):

        print('Plotting the mean and std of the confidences of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()
        unClasses = ['front', 'left', 'none', 'right']
        c = ['blue', 'green', 'black', 'red']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]
                stackedDataList = []
                for k in range(len(unClasses)):

                    # stack the data over all samples of one environment
                    stackedData = []
                    for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                        stackedData.append(rows['ConfidenceResults'])
                    stackedData = np.stack(stackedData, axis=2)

                    # get the mean over all present samples, but do not count values in that were empty (0)
                    nonZeroOverClasses = np.count_nonzero(np.sum(stackedData, axis=1), axis=1)
                    trueMean = np.divide(np.sum(stackedData[:,k], axis=1), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData[:,k], axis=1)), where=nonZeroOverClasses!=0)
                    stackedDataList.append([trueMean, np.std(stackedData[:,k], axis=1)])
                locationList.append(stackedDataList)

            # repeat for all environment types
            stackedDataList = []
            for k in range(len(unClasses)):
                stackedData = []
                for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                    stackedData.append(rows['ConfidenceResults'])
                stackedData = np.stack(stackedData, axis=2)

                nonZeroOverClasses = np.count_nonzero(np.sum(stackedData, axis=1), axis=1)
                trueMean=np.divide(np.sum(stackedData[:,k], axis=1), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData[:,k], axis=1)), where=nonZeroOverClasses!=0)
                stackedDataList.append([trueMean, np.std(stackedData[:,k], axis=1)])
            locationList.append(stackedDataList)
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'ConfidencePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    for k in range(len(unClasses)):
                        axes.plot(time, dataList[i][j][k][0], color=c[k], label=unClasses[k])
                        axes.fill_between(time, dataList[i][j][k][0] - dataList[i][j][k][1], dataList[i][j][k][0] + dataList[i][j][k][1], color=c[k], alpha=0.15)
                        axes.grid('on')
                        axes.set_xlim([np.min(time), np.max(time)])
                        axes.set_ylim([0, 1])
                        if self.axisLabels:
                            axes.set_ylabel('Confidence in [0, 1]')
                            axes.set_xlabel('time t in [s]')
                            axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'none':
                            axes.legend(loc='upper center', ncol=4, prop={'size': 8.5})
                    fig.set_size_inches((4.449, 2.40157))

                    plt.savefig(os.path.join(confidencePath, 'Confidence_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    for k in range(len(unClasses)):
                        axes[i][j].plot(time, dataList[i][j][k][0], color=c[k], label=unClasses[k])
                        axes[i][j].fill_between(time, dataList[i][j][k][0] - dataList[i][j][k][1], dataList[i][j][k][0] + dataList[i][j][k][1], color=c[k], alpha=0.15)
                        axes[i][j].grid('on')
                        axes[i][j].set_xlim([np.min(time), np.max(time)])
                        axes[i][j].set_ylim([0, 1])
                        if self.axisLabels:
                            axes[i][j].set_ylabel('Confidence in [0, 1]')
                            axes[i][j].set_xlabel('time t in [s]')
                            axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'none':
                            axes[i][j].legend(loc='upper center', ncol=2, prop={'size': 8.5})
            plt.tight_layout()

    def plotAbsolutes(self):

        print('Plotting the normalized absolute classification results of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()
        unClasses = ['front', 'left', 'none', 'right']
        c = ['blue', 'green', 'black', 'red']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]
                summedDataList = []
                for k in range(len(unClasses)):

                    # stack the data over all samples of one environment
                    summedData = np.zeros((len(time), len(unClasses)))
                    for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                        summedData += rows['ClassificationResults']

                    # get the mean over all present samples, but do not count values in that were empty (0)
                    trueMean = np.divide(summedData[:,k], np.sum(summedData, axis=1), out=np.zeros_like(summedData[:,k]), where=np.sum(summedData, axis=1)!=0)
                    summedDataList.append(trueMean)
                locationList.append(summedDataList)

            # repeat for all environment types
            summedDataList = []
            for k in range(len(unClasses)):
                summedData = np.zeros((len(time), len(unClasses)))
                for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                    summedData += rows['ClassificationResults']

                trueMean=np.divide(summedData[:,k], np.sum(summedData, axis=1), out=np.zeros_like(summedData[:,k]), where=np.sum(summedData, axis=1)!=0)
                summedDataList.append(trueMean)
            locationList.append(summedDataList)
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'AbsolutePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    for k in range(len(unClasses)):
                        axes.plot(time, dataList[i][j][k], color=c[k], label=unClasses[k])
                        axes.grid('on')
                        axes.set_xlim([np.min(time), np.max(time)])
                        axes.set_ylim([0, 1])
                        if self.axisLabels:
                            axes.set_ylabel('Normalized classifications in [0, 1]')
                            axes.set_xlabel('time t in [s]')
                            axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'negative':
                            axes.legend(loc='upper center', ncol=4, prop={'size': 8.5})

                    plt.savefig(os.path.join(confidencePath, 'Absolute_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    for k in range(len(unClasses)):
                        axes[i][j].plot(time, dataList[i][j][k], color=c[k], label=unClasses[k])
                        axes[i][j].grid('on')
                        axes[i][j].set_xlim([np.min(time), np.max(time)])
                        axes[i][j].set_ylim([0, 1])
                        if self.axisLabels:
                            axes[i][j].set_ylabel('Normalized classifications in [0, 1]')
                            axes[i][j].set_xlabel('time t in [s]')
                            axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'negative':
                            axes[i][j].legend(loc='upper center', ncol=2, prop={'size': 8.5})
            plt.tight_layout()

    def plotVisualBaseline(self):

        print('Plotting the auditory classification results against the visual baseline')

        le = preprocessing.LabelBinarizer()
        le.fit([0,1,2,3])

        # prepare gt labels
        unClasses = ['front', 'left', 'negative', 'right']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)
        offset = 15

        gtLeft = np.zeros((Lall, len(unClasses)), dtype=int)
        gtLeft[:Tall+offset,1] = 1
        gtLeft[Tall+1:,0] = 1

        gtRight = np.zeros((Lall, len(unClasses)), dtype=int)
        gtRight[:Tall+offset,3] = 1
        gtRight[Tall+1:,0] = 1

        gtNone = np.zeros((Lall, len(unClasses)), dtype=int)
        gtNone[:,2] = 1

        gtVisualNeg = np.zeros(Lall, dtype=int)
        gtVisualPos = np.ones(Lall, dtype=int)

        # stack labels and results
        stackedLabels = []
        stackedResults = []
        stackedVisualLabel = []
        stackedVisualResult = []

        for index, rows in self.logTable.iterrows():
            if rows['Class'] == 'left':
                stackedLabels.append(gtLeft)
                stackedVisualLabel.append(gtVisualPos)
            elif rows['Class'] == 'right':
                stackedLabels.append(gtRight)
                stackedVisualLabel.append(gtVisualPos)
            else:
                stackedLabels.append(gtNone)
                stackedVisualLabel.append(gtVisualNeg)

            stackedResults.append(rows['ClassificationResults'])
            stackedVisualResult.append(rows['VisualDetections'])

        stackedLabels = np.stack(stackedLabels)
        stackedResults = np.stack(stackedResults)
        stackedVisualLabel = np.stack(stackedVisualLabel)
        stackedVisualResult = np.stack(stackedVisualResult)

        # print(stackedVisualResult)
        #
        visualAccuracy = []
        auditoryAccuracy = []

        for i in range(stackedLabels.shape[1]):
            # Make sure only elements are considered that contributed and filter those, i.e. results do not show zero just from padding
            idx = np.where(np.sum(stackedResults[:,i,:], axis=1) != 0)[0]
            stackedLabelsStep = stackedLabels[idx,i,:]
            stackedResultsStep = stackedResults[idx,i,:]
            stackedVisualLabelStep = stackedVisualLabel[idx,i]
            stackedVisualResultStep = stackedVisualResult[idx,i]

            # get indexes from label matrix to extract from Result matrix
            idxLeft  = np.where(stackedLabelsStep[:,1] == 1)[0]
            idxRight = np.where(stackedLabelsStep[:,3] == 1)[0]
            idxNone  = np.where(stackedLabelsStep[:,2] == 1)[0]
            idxFront = np.where(stackedLabelsStep[:,0] == 1)[0]

            # first deal with visual detections
            tn, _, _, tp = metrics.confusion_matrix(stackedVisualLabelStep, stackedVisualResultStep, labels=[0,1]).ravel()

            visualAccuracy.append((tn + tp) / len(stackedVisualLabelStep))

            # in offset area manually calculate TPs
            if i > Tall and i <= Tall+offset:
                totalSamples = len(idxLeft) + len(idxRight) + len(idxNone)

                # define tp as correct whenever front or left/right is correct
                tpa = np.array([np.sum(stackedResultsStep[idxLeft,0]) + np.sum(stackedResultsStep[idxLeft,1]),
                        np.sum(stackedResultsStep[idxRight,0]) + np.sum(stackedResultsStep[idxRight,3]),
                        np.sum(stackedResultsStep[idxNone, 2])])

            else:
                totalSamples = len(idxLeft) + len(idxRight) + len(idxNone) + len(idxFront)
                mcm = metrics.multilabel_confusion_matrix(stackedLabelsStep, stackedResultsStep, labels=[0,1,2,3])
                tpa = mcm[:, 1, 1]
                idx_two = np.where(np.sum(stackedLabelsStep, axis=1) == 2)

            auditoryAccuracy.append(np.sum(tpa) / totalSamples)

        visualAccuracy = np.array(visualAccuracy)
        auditoryAccuracy = np.array(auditoryAccuracy)

        # now plot
        fig, axes = plt.subplots()
        transitionArea = np.zeros((Lall))
        transitionArea[Tall+1:Tall+offset] = 1

        axes.fill_between(time, transitionArea, 0, color='k', step='mid', alpha=0.2)

        axes.plot(time, visualAccuracy, label='Faster R-CNN (visual)', linewidth=2.5)
        axes.plot(time, auditoryAccuracy, 'peru', label='ours (acoustic)', linewidth=2.5)
        axes.grid('on')
        axes.set_xlim([np.min(time), np.max(time)])
        axes.set_ylim([0.4, 1])
        if self.axisLabels:
            axes.set_xlabel('time t in [s]')
            axes.set_ylabel('Accuracy in [0, 1]')
            axes.set_title('Visual Baseline Comparison')
        axes.legend(loc='upper left', ncol = 1, prop={'size': 10})
        if self.store:
            baselinePath = os.path.join(self.plotPath, 'VisualBaselineComparison')
            utilities.makeDirectory(baselinePath)
            fig.set_size_inches((1.8*4.449, 1.8*1.40157))
            plt.savefig(os.path.join(baselinePath, 'VisualBaselineComparison.pdf'), dpi=300, bbox_inches='tight')
        if self.showViz:
            plt.tight_layout()
        else:
            plt.close(fig)

    def plotMeanFeatures(self):

        print('Plotting the mean feature vector of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)
        offset = 15

        maxValue = 0
        minValue = 100

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]

                # stack the data over all samples of one environment
                stackedData = []
                for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                    stackedData.append(rows['Features'])
                stackedData = np.stack(stackedData, axis=2)

                # get the mean over all present samples, but do not count values in that were empty (0)
                nonZeroOverClasses = np.count_nonzero(stackedData, axis=2)
                trueMean = np.divide(np.sum(stackedData, axis=2), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData, axis=2)), where=nonZeroOverClasses!=0)
                if trueMean.max() > maxValue:
                    maxValue = trueMean.max()
                if trueMean.min() < minValue:
                    minValue = trueMean.min()
                locationList.append([trueMean, np.std(stackedData)])

            # repeat for all environment types
            stackedData = []
            for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                stackedData.append(rows['Features'])
            stackedData = np.stack(stackedData, axis=2)

            nonZeroOverClasses = np.count_nonzero(stackedData, axis=2)
            trueMean=np.divide(np.sum(stackedData, axis=2), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData, axis=2)), where=nonZeroOverClasses!=0)
            if trueMean.max() > maxValue:
                maxValue = trueMean.max()
            if trueMean.min() < minValue:
                minValue = trueMean.min()

            locationList.append([trueMean, np.std(stackedData)])
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'FeaturePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    axes.imshow(np.transpose(dataList[i][j][0])[0:29,:], cmap='gray', vmin=minValue, vmax=maxValue)
                    axes.vlines([Tall, Tall+offset], 0, 29, colors=['blue', 'red'], linestyles='dashed')

                    axes.set_xticks(np.arange(len(time)))
                    axes.set_xticklabels(time)
                    axes.set_yticks(np.arange(30))
                    axes.set_yticklabels(np.linspace(-90, 90, 30).astype(int))

                    everyNth = 10
                    for n, label in enumerate(axes.xaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    everyNth = 3
                    for n, label in enumerate(axes.yaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    axes.set_ylim([0, 29])

                    if self.axisLabels:
                        axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        axes.set_xlabel('Angle in [deg]')
                        axes.set_ylabel('time t in [s]')

                    fig.set_size_inches((4.449, 2.40157))

                    plt.savefig(os.path.join(confidencePath, 'Feature_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):

                    axes[i][j].imshow(np.transpose(dataList[i][j][0])[0:29,:], cmap='gray', vmin=minValue, vmax=maxValue)
                    axes[i][j].vlines([Tall, Tall+offset], 0, 29, colors=['blue', 'red'], linestyles='dashed')

                    axes[i][j].set_xticks(np.arange(len(time)))
                    axes[i][j].set_xticklabels(time)
                    axes[i][j].set_yticks(np.arange(30))
                    axes[i][j].set_yticklabels(np.linspace(-90, 90, 30).astype(int))

                    everyNth = 10
                    for n, label in enumerate(axes[i][j].xaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    everyNth = 3
                    for n, label in enumerate(axes[i][j].yaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    axes[i][j].set_ylim([0, 29])

                    if self.axisLabels:
                        axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        axes[i][j].set_xlabel('Angle in [deg]')
                        axes[i][j].set_ylabel('time t in [s]')

            plt.tight_layout()


#!/usr/bin/env python2.5
# -*- coding: utf-8 -*-
# File: xmppio.py
# Date: 2010-04-20
# Author: jyf<jyf1987@gmail.com>

from google.appengine.api import xmpp
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import bot

class XMPPHandler(webapp.RequestHandler):
    def get(self):
        return """<script>alert("hello,world");</script>"""

    def post(self):
        jid = self.request.get("from")
        body = self.request.get("body")

        req = {}
        req["from"] = {"full": jid, "node": jid.split("/")[0].split("@")[0], "domain": jid.split("/")[0].split("@")[1], "resource": jid.split("/")[1]}
        req["raw"] = body
        response = bot.main(req)
        xmpp.send_message(jid, response)

urls = [
    ('/_ah/xmpp/message/chat/', XMPPHandler)
]

app = webapp.WSGIApplication(urls , debug=True)
if __name__ == "__main__":
  run_wsgi_app(app)


from Tkinter import *
import Tkinter as tk
import ttk
import MySQLdb;
con=MySQLdb.connect("localhost","root","1234","dbms");
cur=con.cursor()

def full():
	win=Tk()
	win.geometry("1200x1000")

	label1=Label(win,text=' ')
	label1.pack()
	label1=Label(win,text=' ')
	label1.pack()

	label1=Label(win,text='SEARCH BY ROLL NO : ')
	label1.pack()

	impo=tk.StringVar(win)
	name1=ttk.Entry(win,width=25,textvariable=impo)
	name1.pack()
	name1.focus()

	label1=Label(win,text='  ')
	label1.pack()

	label1=Label(win,text='OR ')
	label1.pack()

	label1=Label(win,text='  ')
	label1.pack()

	label1=Label(win,text='SEARCH BY NAME (CAPITAL) : ')
	label1.pack()

	impo2=tk.StringVar(win)
	name1=ttk.Entry(win,width=25,textvariable=impo2)
	name1.pack()
	name1.focus()

	label1=Label(win,text='')
	label1.pack()
	def action():

		label1=Label(win,text='')
		label1.pack()
		label1=Label(win,text='')
		label1.pack()

		q=impo.get()
		l=impo2.get()

		if len(q)!= 0:
			cur.execute("select * from Table1 where roll_no = '%s'"% q)
		else :
			cur.execute("select * from Table1 where name = '%s'"% l)
		row=cur.fetchall()
		tree=ttk.Treeview(win,height=20,columns=('name','gmail','age','gender','roll_no','branch','field','degree'))
		tree.pack()
		tree.heading("#0",text="Name")
		tree.column("#0",width=120)
		tree.heading("#1",text="Gmail")
		tree.heading("#2",text="Age")
		tree.column("#2",width=80)
		tree.heading("#3",text="Gender")
		tree.column("#3",width=100)
		tree.heading("#4",text="Roll No.")
		tree.column("#4",width=100)
		tree.heading("#5",text="Branch")
		tree.column("#5",width=100)
		tree.heading("#6",text="Occupation")
		tree.column("#6",width=100)
		tree.heading("#7",text="Degree")
		tree.column("#7",width=100)
		tree.heading("#8",text="Address")
		tree.tag_configure('T',font='Arial 20')
		if(row):
			for i in row:
				tree.insert('','end',text=i[0], values=(i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8]))
		else:
			label1=Label(win,text='DATA DOES NOT FOUND')
			label1.pack()

	submit=ttk.Button(win,text='Search',command=action)
	submit.pack()
	mainloop()


'''
Question 2 Skeleton Code

Here you should implement and evaluate the Conditional Gaussian classifier.
'''

import data
import numpy as np
# Import pyplot - plt.imshow is useful!
import zipfile
import os
from scipy.special import logsumexp
import matplotlib.pyplot as plt

def compute_mean_mles(train_data, train_labels):
    '''
    Compute the mean estimate for each digit class

    Should return a numpy array of size (10,64)
    The ith row will correspond to the mean estimate for digit class i
    '''
    means = []
    for i in range(0, 10):
        values_i = data.get_digits_by_label(train_data, train_labels, i) #this is all training examples for digit i
        avg_list = np.mean(values_i, axis=0) #averages every row in each column, reminder that rows are examples and each column is a pixel
        means.append(avg_list)

    # Compute means
    return np.array(means)

def compute_sigma_mles(train_data, train_labels):
    '''
    Compute the covariance estimate for each digit class

    Should return a three dimensional numpy array of shape (10, 64, 64)
    consisting of a covariance matrix for each digit class
    '''
    #remember from the assignment instructions we should add 0.01*identity matrix to our answer to avoid numerical errors
    covariances = []
    averages = compute_mean_mles(train_data, train_labels)
    for i in range(0, 10):
        values_i = data.get_digits_by_label(train_data, train_labels, i)
        avg_i = averages[i]
        difference_from_mean = values_i - avg_i
        #the covariance matrix - calculated using the formula: 1/m sum(i=1..m) (xi-Mew)(xi-mew).T
        covariance = np.dot(difference_from_mean.transpose(), difference_from_mean) / values_i.shape[0]

        #add the stabalizing constant
        covariance = covariance + np.eye(values_i.shape[1]) * 0.01

        covariances.append(covariance)
    # Compute covariances
    return covariances

def generative_likelihood(digits, means, covariances):
    '''
    Compute the generative log-likelihood:
        log p(x|y,mu,Sigma)

    Should return an n x 10 numpy array
    '''
    #we need to compute this formula: log(p(x|y=k, mew, sigmak)) = log((2*pi)^(-d/2)|sigmak|^(-1/2) exp {-1/2(x-mewk).T sigmak^-1(x-mewk)})
    #will return the log-likelihood of each example being from each category

    likelihoods = []

    #multiplied terms can now be added once you take the log
    #log(2*pi^-d/2)) becomes (-d/2)*log(2*pi)

    first = (-means.shape[0]/2)*np.log(2*np.pi)
    for digit in digits:
        likelihoods_digit = []
        for i in range(0, 10):

            #next term is the determinent of sigma to the power of -1/2 which becomes: -1/2 log(det(sigma k))
            second = (-1/2)*np.log(np.linalg.det(covariances[i]))

            #last term becomes -1/2*(x-mewk).T * sigmak^-1(x-mewk)
            inv = np.linalg.inv(covariances[i])
            x_minus_mewk = digit - means[i]
            #print(inv.shape)
            #print(x_minus_mewk.shape)

            last = (-1/2) * np.matmul(np.transpose(x_minus_mewk),inv)
            last = np.matmul(last, x_minus_mewk)
            likelihoods_digit.append((first+second+last))
        likelihoods.append(likelihoods_digit)
    #print(np.array(likelihoods).shape)
    return np.array(likelihoods)

def conditional_likelihood(digits, means, covariances):
    '''
    Compute the conditional likelihood:

        log p(y|x, mu, Sigma)

    This should be a numpy array of shape (n, 10)
    Where n is the number of datapoints and 10 corresponds to each digit class
    '''
    # using bayes rule: p(y|x) = p(x|y)p(y) / P(x) and taking the log of this gives us: log(p(y|x) = log(p(x|y)) + log(p(y)) - log(p(x))
    #in generative_likelihoods we calculated log(p(x|y))

    log_prob_x_y = generative_likelihood(digits, means, covariances)

    log_prob_y = np.log(1/10)

    #p(x) = sum over all y p(x, y) = sum over all y (p(x|y)p(y))

    log_prob_x = logsumexp(log_prob_x_y + log_prob_y, axis=1).reshape(-1, 1)

    log_prob_y_x = log_prob_x_y + log_prob_y - log_prob_x

    return log_prob_y_x

def avg_conditional_likelihood(digits, labels, means, covariances):
    '''
    Compute the average conditional likelihood over the true class labels

        AVG( log p(y_i|x_i, mu, Sigma) )

    i.e. the average log likelihood that the model assigns to the correct class label
    '''
    cond_likelihood = conditional_likelihood(digits, means, covariances)

    #need to get the conditional likelihood for only the correct class labels
    #slice data where the label is equal to i
    sums = 0
    for i, likelihoods in enumerate(cond_likelihood):
        sums += likelihoods[int(labels[i])]
    avg = sums/cond_likelihood.shape[0]
    # Compute as described above and return
    return avg

def classify_data(digits, means, covariances):
    '''
    Classify new points by taking the most likely posterior class
    '''
    #look at the conditional likelihood and choose the column (out of 10) that has the highest value for each example (row)
    prob_y_given_x = conditional_likelihood(digits, means, covariances)
    # Compute and return the most likely class
    #pick the column with the highest likelihood

    most_likely = np.argmax(prob_y_given_x, axis=1)

    return most_likely

def questiona (train_data, train_labels, test_data, test_labels):

    #compute average conditional log likelihood on both the train and test set
    means = compute_mean_mles(train_data, train_labels)
    covariances = compute_sigma_mles(train_data, train_labels)

    train_avg_conditional_log_likelihood = avg_conditional_likelihood(train_data, train_labels, means, covariances)

    print("training avg log likelihood: " + str(train_avg_conditional_log_likelihood))

    test_avg_conditional_log_likelihood = avg_conditional_likelihood(test_data, test_labels, means, covariances)

    print("test avg log likelihood: " + str(test_avg_conditional_log_likelihood))

def questionb(train_data, train_labels, test_data, test_labels):
    # select the most likely posterior class for each training and test point as your prediction and report your accuracy on the training and test set
    # need to call classify data function and keep track of accuracy
    means = compute_mean_mles(train_data, train_labels)
    covariances = compute_sigma_mles(train_data, train_labels)

    #predict
    train_predictions = classify_data(train_data, means, covariances)
    #create a list of True or False if they were correct
    train_correct = [train_predictions == train_labels]
    #averaging 0s and 1s gives you the percentage of correctly classified data
    train_accuracy = np.mean(train_correct)
    print("train accuracy is: " + str(train_accuracy))

    test_predictions = classify_data(test_data, means, covariances)
    test_correct = [test_predictions == test_labels]
    test_accuracy = np.mean(test_correct)
    print("test accuracy is: " + str(test_accuracy))

def questionc(covariances):
    # Compute the leading eigenvectors (largest eigenvalue) for each class covariance matrix (can use np.linalg.eig) and plot them side by side as 8 by 8 images.

    each_digit = []
    #for each class, compute the eigenvalues and eigenvectors and then choose the eigenvector with the highest associated eigenvalue
    for i in range(0, 10):
        cov = covariances[i]
        vals, vecs = np.linalg.eig(cov)
        #get largest eigenvalue - this expression is from the np.linalg.eig documentation:
        leading_eigenvector = vecs[:, np.argmax(vals)]
        each_digit.append(leading_eigenvector.reshape(8, 8))

    #this only works when run on an interactive python terminal and it seems to be a common issue with plt.imshow
    plt.title("leading eignvectors")
    plt.imshow(np.concatenate(np.array(each_digit), axis = 1), cmap = 'gray')
    plt.show()

def main():
    train_data, train_labels, test_data, test_labels = data.load_all_data('hw4digits/')

    # Evaluation
    # Fit the model
    means = compute_mean_mles(train_data, train_labels)
    covariances = compute_sigma_mles(train_data, train_labels)

    #part a
    print("question 2)a)")
    questiona(train_data, train_labels,test_data,test_labels)

    #part b
    print("question 2)b)")
    questionb(train_data, train_labels, test_data, test_labels)

    #part c
    print("question 2)c)")
    questionc(covariances)

if __name__ == '__main__':
    main()


from rest_framework import generics, status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from ..serializers import *

class InterestsAPI(generics.ListCreateAPIView):
    queryset = Interests.objects.all()
    serializer_class = InterestsSerializer

    @api_view(['POST'])
    def get_interests_by_categories(request):
        if request.method == 'POST':
            categories = InterestCategories.objects.all()
            interests = Interests.objects.all()
            interests2 = []
            exists = 0

            for category in categories:
                if category.name == request.data['category']:
                    exists = 1
                    category_id = category.id

            if exists:
                for interest in interests:
                    if interest.category_id == category_id:
                        interests2.append(interest)

                interests_serializer = InterestsSerializer(interests2, many=True)
                return Response(interests_serializer.data, status=status.HTTP_201_CREATED)
            else:
                return Response('Category does not exist')


import asyncio
import datetime
import functools
import logging
import time
from typing import Callable, Union

from smarthouse.storage import Storage
from smarthouse.utils import HOUR, MIN, get_timedelta_now

logger = logging.getLogger("root")

def calc_sleep(
    interval: tuple[
        Union[datetime.timedelta, Callable[[], datetime.timedelta]],
        Union[datetime.timedelta, Callable[[], datetime.timedelta]],
    ]
    | None = None
):
    if interval is None:
        return 0

    timedelta_now = get_timedelta_now()
    interval_0: datetime.timedelta = (
        interval[0]()  # type: ignore[operator,assignment]
        if isinstance(interval[0], Callable)  # type: ignore[arg-type]
        else interval[0]
    )
    interval_1: datetime.timedelta = (
        interval[1]()  # type: ignore[operator,assignment]
        if isinstance(interval[1], Callable)  # type: ignore[arg-type]
        else interval[1]
    )

    if interval_0 <= interval_1:
        if not interval_0 <= timedelta_now < interval_1:
            return (
                interval_0 - timedelta_now
                if timedelta_now < interval_0
                else interval_0 + datetime.timedelta(hours=24) - timedelta_now
            ).total_seconds()
    elif interval_1 <= timedelta_now < interval_0:
        return (timedelta_now - interval_0).total_seconds()

    return 0

def looper(
    timeout: int | float,
    interval: tuple[
        Union[datetime.timedelta, Callable[[], datetime.timedelta]],
        Union[datetime.timedelta, Callable[[], datetime.timedelta]],
    ]
    | None = None,
):
    def decorator(func):
        @functools.wraps(func)
        async def wrapper(*args, **kwargs):
            last_written = time.time()
            calls_time = 0.0
            calls_count = 0
            exceptions_count = 0
            while True:
                try:
                    sleep = calc_sleep(interval)
                    if last_written + 24 * HOUR < time.time() + sleep:
                        last_written = time.time()
                        if calls_time > 10:
                            logger.debug(f"{func.__name__}: {calls_count} times, {int(calls_time)} secs")
                        calls_time = 0.0
                        calls_count = 0

                    await asyncio.sleep(sleep)

                    start = time.time()
                    sleep = await func(*args, **kwargs)
                    calls_time += time.time() - start
                    calls_count += 1

                    exceptions_count = 0
                except Exception as exc:
                    logger.exception(exc)

                    exceptions_count += 1
                    if exceptions_count > 10:
                        raise exc

                    sleep = 1

                    storage = Storage()
                    await storage.messages_queue.put({"message": f"{func.__name__} {exc}"})

                    await asyncio.sleep(10)

                await asyncio.sleep(sleep or timeout)

        wrapper._original = func
        return wrapper

    return decorator

def scheduler(schedules: tuple[Union[datetime.timedelta, Callable[[], datetime.timedelta]], ...]):
    def decorator(func):
        @looper(MIN)
        @functools.wraps(func)
        async def wrapper(*args, **kwargs):
            key_name = f"__{func.__name__}"

            timedelta_now = get_timedelta_now()
            for schedule in schedules:
                if isinstance(schedule, Callable):
                    schedule_time = schedule()
                else:
                    schedule_time = schedule

                storage = Storage()
                if (
                    timedelta_now > schedule_time
                    and (timedelta_now - schedule_time).total_seconds() < 2 * MIN
                    and time.time() - storage.get(key_name) > 3 * MIN
                ):
                    result = await func(*args, **kwargs)
                    storage.put(key_name, time.time())
                    return result

        wrapper._original = func
        return wrapper

    return decorator


from collections import deque
f,s,g,u,d=map(int,input().split())
q=deque()
q.append((s,0))
min_value=int(1e9)
visited=set([s])

while q:
  now, cnt=q.popleft()
  if now==g:
    min_value=min(min_value, cnt)
  else:
    if now + u <=f and now+u not in visited:
        q.append((now+u,cnt+1))
        visited.add(now+u)
    if now-d>=1 and now -d not in visited:
      q.append((now-d,cnt+1))
      visited.add(now-d)

if g not in visited:
  print("use the stairs")
else:
  print(min_value)


import pytest

from fparser.two import Fortran2003

from psyclone.domain.lfric.kernel import ScalarArgMetadata

def test_create():
    '''Test that an instance of ScalarArgMetadata can be created
    successfully.

    '''
    field_arg = ScalarArgMetadata("GH_REAL", "GH_READ")
    assert isinstance(field_arg, ScalarArgMetadata)
    assert field_arg.form == "gh_scalar"
    assert field_arg._datatype == "gh_real"
    assert field_arg._access == "gh_read"

def test_get_metadata():
    '''Test that the _get_metadata class method works as expected.'''
    fparser2_tree = ScalarArgMetadata.create_fparser2(
        "arg_type(GH_SCALAR, GH_REAL, GH_READ)", Fortran2003.Part_Ref)
    datatype, access = ScalarArgMetadata._get_metadata(fparser2_tree)
    assert datatype == "GH_REAL"
    assert access == "GH_READ"

def test_get_datatype_access_metadata():
    '''Test that the get_datatype_access_metadata class method works as
    expected. Test that all relevant check and get methods are called
    by raising exceptions within them, as well as checking for valid
    input.

    '''
    # check_fparser2_arg
    with pytest.raises(TypeError) as info:
        _ = ScalarArgMetadata._get_datatype_access_metadata("hello")
    assert ("Expected kernel metadata to be encoded as an fparser2 "
            "Part_Ref object but found type 'str' with value 'hello'."
            in str(info.value))

    # check_nargs
    fparser2_tree = ScalarArgMetadata.create_fparser2(
        "arg_type(x)", Fortran2003.Part_Ref)
    with pytest.raises(ValueError) as info:
        _ = ScalarArgMetadata._get_datatype_access_metadata(fparser2_tree)
    assert ("Expected kernel metadata to have 3 arguments, but "
            "found 1 in 'arg_type(x)'." in str(info.value))

    # check_first_arg
    fparser2_tree = ScalarArgMetadata.create_fparser2(
        "arg_type(GH_FIELD, GH_REAL, GH_READ)", Fortran2003.Part_Ref)
    with pytest.raises(ValueError) as info:
        _ = ScalarArgMetadata._get_datatype_access_metadata(fparser2_tree)
    assert ("Metadata for 'scalar' kernel arguments should have 'gh_scalar' "
            "as the first metadata property, but found 'GH_FIELD'."
            in str(info.value))

    # OK
    fparser2_tree = ScalarArgMetadata.create_fparser2(
        "arg_type(GH_SCALAR, GH_REAL, GH_READ)", Fortran2003.Part_Ref)
    datatype, access = ScalarArgMetadata._get_datatype_access_metadata(
        fparser2_tree)
    assert datatype == "GH_REAL"
    assert access == "GH_READ"

def test_fortran_string():
    '''Test that the fortran_string method works as expected.'''

    fortran_string = "arg_type(gh_scalar, gh_real, gh_read)"
    field_arg = ScalarArgMetadata.create_from_fortran_string(fortran_string)
    result = field_arg.fortran_string()
    assert result == fortran_string

def test_check_datatype():
    '''Test the check_datatype method works as expected.'''
    ScalarArgMetadata.check_datatype("GH_REAL")
    with pytest.raises(ValueError) as info:
        ScalarArgMetadata.check_datatype("invalid")
    assert ("The 'datatype descriptor' metadata should be a recognised value "
            "(one of ['gh_real', 'gh_integer', 'gh_logical']) but found "
            "'invalid'." in str(info.value))

def test_check_access():
    '''Test the check_access method works as expected.'''
    ScalarArgMetadata.check_access("GH_READ")
    ScalarArgMetadata.check_access("gh_sum")
    with pytest.raises(ValueError) as info:
        ScalarArgMetadata.check_access("invalid")
    assert ("The 'access descriptor' metadata should be a recognised value "
            "(one of ['gh_read', 'gh_sum']) but found 'invalid'."
            in str(info.value))


import pathlib

def injector_dual_wag(well, group, operate, monitor, geometry, perf
        , completion, open_time, on_time, wag_start, layerclump, output_folder):

    from well.scripts.frames.inje_dual_wag import Inje_Dual_Wag

    w = Inje_Dual_Wag(well, group)

    w.get_incomp('W','*WATER')
    w.get_incomp('G','*GAS')

    for ope in operate:
        w.get_operate(*ope)

    for mon in monitor:
        w.get_monitor(*mon)

    w.get_geometry(*geometry)
    w.get_perf(perf)

    for com in (completion):
        w.get_completion(com)

    if on_time:
        w.get_on_time(on_time)

    if open_time:
        w.get_open(*open_time)

    for lay in layerclump:
        w.get_layerclump(lay)

    if wag_start:
        w.get_wag(*wag_start)

    w.build()
    w.write(pathlib.Path(output_folder) / '{}.inc'.format(well))


import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
from torch.utils.data import Dataset, DataLoader

torch.manual_seed(0)

class Greater1L(nn.Module):
    def __init__(self):
        super().__init__()

        self.WI = nn.Linear(2, 2, bias=True)
        self.WO = nn.Linear(2, 3, bias=True)
        self.criterion = nn.NLLLoss()

    def forward(self, x):
        x = self.WI(x)
        x = torch.relu(x)
        x = self.WO(x)
        x = torch.sigmoid(x)
        x = torch.softmax(x, dim=1)
        return x

    def loss(self, y_, y):
        return self.criterion(y_, y)

N = 10000
X = torch.rand(N, 2)
y = torch.empty(N, 1)
for i, x in enumerate(X):
    if x[0] > x[1]:
        y[i] = torch.as_tensor([0]).view(1, 1)
    elif x[0] < x[1]:
        y[i] = torch.as_tensor([1]).view(1, 1)
    else:
        y[i] = torch.as_tensor([2]).view(1, 1) #very rare due to random process in generating the pairs of numbers!

X_y = torch.cat((X, y), 1)

class PrepareData(Dataset):
    def __init__(self, X, y):
        self.X = X
        self.y = y

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        return self.X[idx], self.y[idx]

ds = PrepareData(X=X_y[:, :-1], y=X_y[:, -1])
ds = DataLoader(ds, batch_size=10, shuffle=True)

greater_nn = Greater1L()
optmizer = optim.SGD(greater_nn.parameters(), lr=0.01)

epoch =100
for e in range(epoch):
    sum_l = 0
    for i, (X, y) in enumerate(ds):
        if i == 0:
            X_test, y_test = (X, y)
            continue
        optmizer.zero_grad()
        y_ = greater_nn.forward(X)
        y = y.type(torch.LongTensor)
        l = greater_nn.loss(y_, y)
        l.backward()
        optmizer.step()
        sum_l += l.item()
    else:
        with torch.no_grad():
            sum_tl = 0
            acc = 0
            y_ = greater_nn.forward(X_test)
            tl = greater_nn.loss(y_, y_test.type(torch.LongTensor))
            probs, predictions = y_.topk(1, dim=1)
            sum_tl += tl.item()
            equal = predictions == y_test.view(*predictions.shape)  # (b * 1) vs. (b)
            acc = torch.mean(equal.type(torch.FloatTensor))  # equals returns boolean values which mean function does not understand!
        print(f'Epoch: {e}/{epoch}\n'
              f'Batch Loss:{sum_l / i - 1}\n'
              f'Test Loss: {sum_tl}\n'
              f'Accuracy: {acc.item() * 100}%')

print(greater_nn.state_dict())

#try out of range numbers
y_ = greater_nn.forward(torch.as_tensor([[100., 20.]]))
print(y_.topk(1, dim=1))


import torch
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
import shutil
import logging
from moviepy.editor import VideoFileClip, AudioFileClip
from pydub import AudioSegment
import os
import soundfile as sf
import tempfile
MODEL = None

def load_model(version):
    print("Loading model", version)
    return MusicGen.get_pretrained(version)

def predict(model, text, melody, duration, topk, topp, temperature, cfg_coef):
    global MODEL
    topk = int(topk)
    if MODEL is None or MODEL.name != model:
        MODEL = load_model(model)

    if duration > MODEL.lm.cfg.dataset.segment_duration:
        # raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")
        print("MusicGen currently supports durations of up to 30 seconds!")
        # return
    MODEL.set_generation_params(
        use_sampling=True,
        top_k=topk,
        top_p=topp,
        temperature=temperature,
        cfg_coef=cfg_coef,
        duration=duration,
    )

    if melody:
        sr, melody = melody[0], torch.from_numpy(melody[1]).to(MODEL.device).float().t().unsqueeze(0)
        # print(melody.shape)
        if melody.dim() == 2:
            melody = melody[None]
        melody = melody[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]
        output = MODEL.generate_with_chroma(
            descriptions=[text],
            melody_wavs=melody,
            melody_sample_rate=sr,
            progress=False
        )
    else:
        output = MODEL.generate(descriptions=[text], progress=False)

    output = output.detach().cpu().numpy()
    return MODEL.sample_rate, output

def predict_and_save(model, text, melody, duration, topk, topp, temperature, cfg_coef, filename):
    global MODEL
    topk = int(topk)
    if MODEL is None or MODEL.name != model:
        MODEL = load_model(model)

    if duration > MODEL.lm.cfg.dataset.segment_duration:
        # raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")
        print("MusicGen currently supports durations of up to 30 seconds!")
        # return
    MODEL.set_generation_params(
        use_sampling=True,
        top_k=topk,
        top_p=topp,
        temperature=temperature,
        cfg_coef=cfg_coef,
        duration=duration,
    )

    if melody:
        sr, melody = melody[0], torch.from_numpy(melody[1]).to(MODEL.device).float().t().unsqueeze(0)
        # print(melody.shape)
        if melody.dim() == 2:
            melody = melody[None]
        melody = melody[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]
        output = MODEL.generate_with_chroma(
            descriptions=[text],
            melody_wavs=melody,
            melody_sample_rate=sr,
            progress=False
        )
    else:
        output = MODEL.generate(descriptions=[text], progress=True)

    output = output.detach().cpu()
    output = output.squeeze()

    # Save the audio file
    audio_write(filename, output, MODEL.sample_rate, strategy="loudness", loudness_compressor=True)

    return MODEL.sample_rate, output

def save_audio(model, text, melody, duration, topk, topp, temperature, cfg_coef, filename):
    sample_rate, output = predict(model, text, melody, duration, topk, topp, temperature, cfg_coef)

    # Check the dimensions of the output and reduce if necessary
    if output.ndim > 2:
        output = output.squeeze()

    # Save the audio file
    audio_write(filename, torch.from_numpy(output), sample_rate, strategy="loudness", loudness_compressor=True)

def add_audio_to_video(model, text, melody, duration, topk, topp, temperature, cfg_coef, video_filepath, keep_audio=False, audio_format="mp3"):
    dir_path = os.path.dirname(video_filepath)
    logging.basicConfig(filename=os.path.join(dir_path, 'log.log'), level=logging.INFO)
    logging.info(f'Called add_audio_to_video with log: {locals()}')

    if duration > 30:
        duration = 30
        print("MusicGen currently supports durations of up to 30 seconds, only the first 30 seconds will be used")

    sample_rate, output = predict(model, text, melody, duration, topk, topp, temperature, cfg_coef)

    if output.ndim > 2:
        output = output.squeeze()

    temp_audio_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
    temp_audio_filepath = temp_audio_file.name

    sf.write(temp_audio_file.name, output, sample_rate)

    audio = AudioFileClip(temp_audio_file.name)

    video = VideoFileClip(video_filepath)

    video_with_audio = video.set_audio(audio)

    output_video_filepath = os.path.splitext(video_filepath)[0] + "_with_audio.mp4"

    video_with_audio.write_videofile(output_video_filepath)

    audio.close()
    video.close()
    video_with_audio.close()
    temp_audio_file.close()

    if keep_audio:
        if audio_format == "wav":
            output_audio_filepath = os.path.splitext(video_filepath)[0] + "_audio.wav"
            shutil.move(temp_audio_filepath, output_audio_filepath)
        elif audio_format == "mp3":
            output_audio_filepath = os.path.splitext(video_filepath)[0] + "_audio.mp3"
            audio = AudioSegment.from_wav(temp_audio_filepath)
            audio.export(output_audio_filepath, format="mp3")
            os.unlink(temp_audio_filepath)  # delete the temporary .wav file
        else:
            raise ValueError("Unsupported audio_format. Please choose either 'wav' or 'mp3'.")

    return output_video_filepath


import os
import socket
import datetime
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from dataclasses import dataclass

# from configs.config import SLACK_TOKEN, SLACK_CHANNEL
from tabulate import tabulate
from typing import List, Dict

@dataclass
class SlackContent:
    message: str
    error: str = ""

hex = {
    "green": "#00ff00",
    "red": "ff0000",
    "yellow": "#ffba01",
    "white": "ffffff",
}

token = os.getenv("SLACK_TOKEN")
client = WebClient(token=token)
channel = os.getenv("SLACK_CHANNEL")
# host = socket.gethostname()

# token = SLACK_TOKEN
# client = WebClient(token=token)
# channel = SLACK_CHANNEL

def post_apt_message(complex_name: str, available_apt_list: List[Dict]):
    attachments = generate_apt_info(complex_name, available_apt_list)
    try:
        response = client.chat_postMessage(channel=channel, **attachments)
    except SlackApiError as e:
        print(f"메시지 전송 실패: {e.response['error']}")

def generate_apt_info(complex_name: str, available_apt_list: List[Dict]):
    if available_apt_list == []:
        return {"title": complex_name, "text": f"{complex_name}: 확인된 매물이 없습니다."}

    row_text = ""
    for apt in available_apt_list:
        row_text += "{} <https://m.land.naver.com/article/info/{}|아파트이름: {}>\n".format(
            apt.get("noticed_label"), apt.get("item_id"), apt.get("title")
        )
        row_text += "동: {}\n".format(apt.get("building"))
        row_text += "가격: {}\n".format(apt.get("price"))
        row_text += "평형: {}\n".format(apt.get("size"))
        row_text += "층: {}\n".format(apt.get("floor"))
        row_text += "거실창 방향: {}\n".format(apt.get("direction"))
        row_text += "기타: {}\n".format(apt.get("describe"))
        row_text += "\n"
    if len(row_text) > 3000:
        row_text = row_text[:3000]
    text = "아파트 매일 알람"
    blocks = [
        {
            "type": "section",
            "text": {"type": "mrkdwn", "text": f"{row_text}"},
        }
    ]
    return {
        "title": f"{complex_name}",
        "text": text,
        "blocks": blocks,
    }

def post_log_message(color: str, slack_content: SlackContent):
    attachments = generate_attachments(color, slack_content)
    try:
        response = client.chat_postMessage(channel=channel, attachments=attachments)
    except SlackApiError as e:
        print(f"메시지 전송 실패: {e.response['error']}")

def generate_attachments(color: str, slack_content: SlackContent) -> str:
    ts = datetime.datetime.now()
    ts = ts.isoformat("T") + "Z"
    msg = f"`TS`: {ts}\n{slack_content.message}\n"

    if slack_content.error:
        msg += "*ERROR*\n```{}```".format(slack_content.error)

    return [{"color": hex[color], "title": "아파트 알리미 로그", "text": msg}]

def info(message: str):
    post_log_message("green", SlackContent(message=message))

def error(message: str, error_message: str = ""):
    post_log_message(
        "red",
        SlackContent(message=message, error=error_message),
    )

def warning(message: str):
    post_log_message("yellow", SlackContent(message=message))


import numpy as np
import matplotlib.pyplot as plt
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.callbacks import EarlyStopping
from astropy.io import fits
from astropy.io import ascii
from numpy import savetxt
import keras_tuner as kt

#important params
folder_path =  "/c4/wfh842"
write_path = "/c4/wfh842/CNNmodelV2.4"

#Get data
img_size=200
t=ascii.read(folder_path+'/catalog95.csv')
file_name_array = os.listdir(folder_path+'/simimg')
length=len(file_name_array)
train_data=[]
train_labels=[]
val_data=[]
val_labels=[]
test_data=[]
test_labels=[]
test_ID=[]
for i in range(length):
  file_path=folder_path +'/simimg' +"/"+ file_name_array[i]
  hdu = fits.open(file_path,memmap=False)
  data = hdu[0].data
  data=data/np.max(data)
  ID=int(file_name_array[i][:-5])
  #print(str(i)+'/'+str(length-1))
  for j in range(len(t['ID'])):
            if round(t['ID'][j])==ID:
                label=round(t['Label'][j])
                break
  if i%6==0:
    val_data.append(data)
    val_labels.append(label)
  elif i%7==0:
    test_ID.append(ID)
    test_data.append(data)
    test_labels.append(label)
  else:
    train_data.append(data)
    train_labels.append(label)
  hdu.close()
  del data
train_ds = tf.data.Dataset.from_tensor_slices((np.array(train_data), np.array(train_labels)))
test_ds = tf.data.Dataset.from_tensor_slices((np.array(test_data), np.array(test_labels)))
val_ds = tf.data.Dataset.from_tensor_slices((np.array(val_data), np.array(val_labels)))
class_names = ('Below','Above')
print(len(test_ID))

#Tune model
AUTOTUNE = tf.data.AUTOTUNE
BATCH_SIZE = 64
SHUFFLE_BUFFER_SIZE = 100

train_ds = train_ds.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
val_ds = val_ds.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
test_ds = test_ds.batch(BATCH_SIZE)
num_classes = 2
#maxvals=[np.max(val_data),np.max(train_data),np.max(test_data)]
#maxval=np.max(maxvals)

def build_model(hp):
    a=units = hp.Int("units_a", min_value=2, max_value=16, step=2)
    b=units = hp.Int("units_b", min_value=2, max_value=4, step=1)
    d=units = hp.Int("units_d", min_value=64, max_value=256, step=64)
    model = Sequential([
      layers.Rescaling(1, input_shape=(img_size, img_size,1)),
      layers.Conv2D(a, b, padding='same', activation='relu'),
      layers.MaxPooling2D(),
      layers.Conv2D(2*a, b, padding='same', activation='relu'),
      layers.MaxPooling2D(),
      layers.Conv2D(4*a, b, padding='same', activation='relu'),
      layers.MaxPooling2D(),
      layers.Flatten(),
      layers.Dense(d, activation='relu'),
      layers.Dense(num_classes)
    ])

    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    return model
tuner = kt.Hyperband(build_model,
                     objective='val_accuracy',
                     max_epochs=10,
                     factor=3,
                     directory=write_path,
                     project_name='tuner')
tuner.search_space_summary()
es = EarlyStopping(monitor='val_loss', min_delta=0, patience=5)
epochs=100
tuner.search(np.array(train_data), np.array(train_labels), epochs=epochs, validation_data=(np.array(val_data), np.array(val_labels)), callbacks=[es])
#tuner.search(train_ds, epochs=epochs, validation_data=val_ds, callbacks=[es])

model = tuner.get_best_models()[0]
epochs_range = range(epochs)

#Fit Model
model.summary()
history = model.fit(
  x=np.array(train_data),
  y=np.array(train_labels),
  validation_data=(np.array(val_data), np.array(val_labels)),
  epochs=epochs,
  callbacks=[es])
model.save(write_path+'/model')
np.save(write_path+'/traininghistory.npy',history.history)

#Plot results
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(len(history.history['loss']))

plt.figure(figsize=(10, 10))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy fraction')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss fraction')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig(write_path+'/AccuracyLossPlot.png')

#Run test data
correct=0
redx=[]
redy=[]
greenx=[]
greeny=[]
for i in range(len(test_data)):
  img_array = np.array(test_data[i])
  img_array = tf.expand_dims(img_array, 0) # Create a batch

  predictions = model.predict(img_array)
  score = tf.nn.softmax(predictions[0])
  ID=test_ID[i]
  for j in range(len(t['ID'])):
            if round(t['ID'][j])==ID:
                label=round(t['Label'][j])
                sSFR=t['SSFR'][j]
                if sSFR<10**(-14):
                    sSFR=10**(-14)
                M=t['M_s'][j]
                break
  if test_labels[i]==1:
    imgclass='Above'
  else:
    imgclass='Below'
  print(
      "Prediction: {} with a {:.2f} percent confidence, Actual= {}"
      .format(class_names[np.argmax(score)], 100 * np.max(score),imgclass)
  )
  if class_names[np.argmax(score)]==imgclass:
    correct=correct+1
    greenx.append(M)
    greeny.append(sSFR)
  else:
    redx.append(M)
    redy.append(sSFR)
print("Score="+str(correct)+'/'+str(len(test_ID)))
#Save important test data
#savetxt(write_path+'/test_data.csv', np.array(test_data), delimiter=',')
savetxt(write_path+'/test_ID.csv', np.array(test_ID), delimiter=',')
f = open(write_path+"/score.txt", "w")
#txt="a="+str(a)+" b="+str(b)+" c="+str(c)+" d="+str(d)
#f.write(txt)
#f.write('\n')
f.write("Score="+str(correct)+'/'+str(len(test_ID)))
f.write('\n')
f.write("Training Accuracy="+str(acc[len(history.history['loss'])-6]))
f.write('\n')
f.write("Training Loss="+str(loss[len(history.history['loss'])-6]))
f.write('\n')
f.write("Epochs="+str(len(history.history['loss'])-5))
f.close()
#Plot test results
fig=plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(greenx,greeny,'g.',label='Correct')
ax.plot(redx,redy,'r+',label='Incorrect')
ax.axhline(y=10**-11, color='k', linestyle='-')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Stellar Mass M_0')
ax.set_ylabel('SSFR yr^-1')
ax.legend()
fig.savefig(write_path+'/TestResults.png')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 16 20:49:21 2022

@author: hanjiayue
"""

from textblob import TextBlob
import pandas as pd
import plotly.graph_objects as go
import requests
import numpy as np
import re
from textblob import Word
from collections import Counter, defaultdict
import matplotlib.pyplot as plt

class nlp:

    def __init__(self, files,stopfile = None):
        ''' Constructor '''
        self.M = {}  # stores text specific analysis variables
        self.text = ''
        self.contained_files = {}

        if stopfile is None:
            self.stop_words = requests.get(
                "https://gist.githubusercontent.com/rg089/35e00abf8941d72d"
                "419224cfd5b5925d/raw/12d899b70156fd0041fa9778d657330b024b959c"
                "/stopwords.txt").content.decode().splitlines()
        else:
            with open(stopfile) as f:
               for line in f:
                   self.stop_words = [line.split(',') for line in f]

        for file in files:
            brand = re.search('(?<=_)([a-zA-Z]*)(?=\.csv)', file)[0]
            self.contained_files[brand] = self.run_all(file)

    def load_stop_words(self, text):
        ''' A list of common or stop words.
        These get filtered from each file automatically'''
        return ' '.join([word for word in text.split()
                         if word not in self.stop_words])

    def clean_text(self, t):
        ''' remove everything after the https in the text'''
        split_str = t.split('https', 1)
        substr = split_str[0]
        return substr

    def read_text(self, filename):
        ''' Reads given text from file, converts to string and stores '''
        raw_csv = pd.read_csv(filename, names=['brand', 'date',
                                               'code', 'text',
                                               'unlike', 'like'], sep=',')
        raw_csv['text'] = raw_csv['text'].apply(lambda x: self.clean_text(x))

        for row in raw_csv['text']:
            row = self.load_stop_words(row.lower())
            row = row + '.'
            self.text += row

        self.M['text'] = self.text
        return self.text

    def sent_tokenize(self):
        ''' Returns list of stripped, tokenized sentences in text '''
        sents = re.split('[.!?]+', self.text.lower())
        self.M['Sents'] = sents
        return sents

    def word_tokenize(self):
        ''' Returns list of stripped, tokenized words in text '''
        words = [re.sub('[^\w\s]', '', word) for word in
                 re.split('\s+', self.text.lower())]
        while("" in words):
            words.remove("")
        return words

    def sentence_count(self):
        ''' Calculates total sentences in text '''
        sent_count = len(self.sent_tokenize())
        self.M['sentence_count'] = sent_count
        return sent_count

    def total_word_count(self):
        ''' Returns total words in text '''
        total_word_count = len(self.word_tokenize())
        self.M['total_word_count'] = total_word_count
        return total_word_count

    def word_count(self):
        ''' Returns word counts for each text and stores it'''

        word_count = Counter(self.word_tokenize())
        self.M['word_counts'] = word_count
        return word_count

    def avg_sent_len(self):
        ''' Returns and stores average sentence length for given text '''
        avg_slen = round(self.total_word_count() / self.sentence_count(), 3)
        self.M['avg_sentence_length'] = avg_slen
        return avg_slen

    def avg_word_len(self):
        ''' Returns and stores average word length for given
        text excluding punctuation '''
        total_wchar = sum([len(word) for word in self.word_tokenize()])
        avg_wlen = round(total_wchar / self.total_word_count(), 3)
        self.M['avg_word_length'] = avg_wlen
        return avg_wlen

    def sentiment(self, minsub=0.0, maxsub=1.0, minpol=-1.0, maxpol=1.0):
        ''' Gets polarity and subjectivity of text per given lines (int),
        stores, and returns tuple (pol,sub).'''

        # Finds Polarity and subjectivity for a string
        pol, sub = TextBlob(self.text).sentiment
        pol, sub = (round(pol,3), round(sub,3))

        # Sets the Polarity and Subjectivity values to an attribute
        if minpol <= pol <= maxpol and minsub <= sub <= maxsub:
            self.M['polarity'] = pol
            self.M['subjectivity'] = sub
        return pol,sub

    def count_syllable(self, word):
        ''' Counts the number of syllables in a word
        https://stackoverflow.com/questions/46759492/syllable-count-in-python'''

        # Sets the list of vowels
        count = 0
        vowels = 'aeiouy'

        # Checks if the first letter is a vowel
        if word[0] in vowels:
            count += 1

        # Iterates through string to see check for consonant followed by vowel
        for i in range(1, len(word)):
            if word[i] in vowels and word[i-1] not in vowels:
                count += 1

        # Checks if word ends with an e
        if word.endswith('e'):
            count -= 1

        # Sets the minimum count to 1 and then returns it
        return 1 if count < 1 else count

    def readability(self):
        ''' Assesses Readability using the Gunning Fog Formula '''

        # Finds the variables necessary for Gunning Fog Calculation
        sen_length = self.avg_sent_len()
        words = self.word_tokenize()
        syllables = [self.count_syllable(word) for word in words]

        # Finds percentage of hard words and then performs gunning fog equation
        hard_word_count = [count for count in syllables if count >= 2]
        hard_word_percent = (len(hard_word_count)/ len(words)) * 100
        score = 0.4 * (sen_length + hard_word_percent)
        self.M['readability'] = score

        return score

    def run_all(self, filename):
        ''' Runs all general analysis methods and
        returns dictionary with results '''
        self.M = {}
        # Runs all functions selected as below
        gen_methods = [self.read_text(filename), self.avg_sent_len(),
                       self.word_count(),self.avg_word_len(),self.sentiment(),
                       self.readability(),self.sentence_count(),
                       self.sent_tokenize()]

        for nlp_method in gen_methods:
            nlp_method

        return self.M

def main():
   pass

if __name__ == '__main__':
    main()


from transformers import pipeline

def img2txt(url: str) -> str :
    model = "Salesforce/blip-image-captioning-base"
    task = "image-to-text"
    image_to_text = pipeline(task, model=model)
    response = image_to_text(url)
    text = response[0]['generated_text']
    return text


"""

This sample performs a PAdES signature in three steps using PKI Express and
Web PKI.

"""
import os
import uuid

from flask import render_template
from flask import request
from flask import current_app
from flask import Blueprint
from pkiexpress import standard_signature_policies
from pkiexpress import PadesSignatureStarter
from pkiexpress import SignatureFinisher

from sample.pades_visual_elements_express import PadesVisualElementsExpress
from sample.storage_mock import create_app_data
from sample.storage_mock import get_pdf_stamp_path
from sample.utils import set_pki_defaults

# 26-08-2022
# By further inspecting in the latest Blueprint documentation (https://flask.palletsprojects.com/en/2.2.x/api/#blueprint-objects),
# when creating a Blueprint object, the first parameter (name) is prepend to the URL endpoint. Therefore, Blueprint no longer
# allows dots in the name since it would break the URL entirely.
__name__ = __name__.replace(".","/")
blueprint = Blueprint(os.path.basename(__name__), __name__,
                      url_prefix='/pades-signature-express')

@blueprint.route('/<file_id>')
def index(file_id):
    """

    This method only renders the signature page.

    """
    # Verify if the provided userfile exists.
    file_path = os.path.join(current_app.config['APPDATA_FOLDER'], file_id)
    if not os.path.exists(file_path):
        return render_template('error.html', msg='File not found')

    return render_template('pades_signature_express/index.html')

@blueprint.route('/start/<file_id>', methods=['POST'])
def start(file_id):
    """

    This method starts the signature. In this sample, it will be called
    programatically after the user press the "Sign File" button (see method
    readCertificate() on static/js/signature-start-form.js).

    """
    try:
        # Recover variables from the POST arguments to be used on this step.
        cert_thumb = request.form['certThumbField']
        cert_content = request.form['certContentField']

        # Get an instance of the PadesSignatureStarter class, responsible for
        # receiving the signature elements and start the signature process.
        signature_starter = PadesSignatureStarter()

        # Set PKI default options (see utils.py).
        set_pki_defaults(signature_starter)

        # Set signature policy.
        signature_starter.signature_policy = \
            standard_signature_policies.PADES_BASIC

        # Set PDF to be signed
        signature_starter.set_pdf_to_sign_from_path(
            os.path.join(current_app.config['APPDATA_FOLDER'], file_id))

        # Set Base64-encoded certificate's content to signature starter.
        signature_starter.set_certificate_from_base64(cert_content)

        # Set a file reference for the stamp file. Note that this file can be
        # referenced later by "fref://{alias}" at the "url" field on the visual
        # representation (see static/vr.json or get_visual_representation()
        # method).
        signature_starter.add_file_reference('stamp', get_pdf_stamp_path())

        # Set the visual representation. We provided a dictionary that
        # represents the visual representation JSON model.
        signature_starter.set_visual_representation(
            PadesVisualElementsExpress.get_visual_representation())

        # Start the signature process. Receive as response the following fields:
        # - to_sign_hash:     The hash to be signed.
        # - digest_algorithm: The digest algorithm that will inform the Web PKI
        #                     component to compute the signature.
        # - transfer_file:    A temporary file to be passed to "complete" step.
        response = signature_starter.start()

        # Render the field from start() method as hidden field to be used on the
        # javascript or on the "complete" step.
        return render_template('pades_signature_express/start.html',
                               cert_thumb=cert_thumb,
                               transfer_file_id=response['transferFile'],
                               to_sign_hash=response['toSignHash'],
                               digest_algorithm=response['digestAlgorithm'])

    except Exception as e:
        return render_template('error.html', msg=e)

@blueprint.route('/complete/<file_id>', methods=['POST'])
def complete(file_id):
    """

    This function completes the signature, it will be called programatically
    after the Web PKI component perform the signature and submit the form (see
    method sign() on static/js/signature-complete-form.js).

    """
    try:

        # Recover variables form the POST arguments to be used on this step.
        transfer_file = request.form['transferFileIdField']
        signature = request.form['signatureField']

        # Get an instance of the SignatureFinisher class, responsible for
        # completing the signature process.
        signature_finisher = SignatureFinisher()

        # Set PKI default options (see utils.py).
        set_pki_defaults(signature_finisher)

        # Set PDF to be signed. It's the same file we used on "start" method.
        signature_finisher.set_file_to_sign_from_path(
            os.path.join(current_app.config['APPDATA_FOLDER'], file_id))

        # Set the transfer file.
        signature_finisher.set_transfer_file_from_path(transfer_file)

        # Set the signature file.
        signature_finisher.signature = signature

        # Generate path for output file and add to the signature finisher.
        create_app_data()  # Guarantees that "app data" folder exists.
        filename = '%s.pdf' % (str(uuid.uuid4()))
        signature_finisher.output_file = \
            os.path.join(current_app.config['APPDATA_FOLDER'], filename)

        # Complete the signature process.
        signer_cert = signature_finisher.complete(get_cert=True)

        return render_template('pades_signature_express/signature-info.html',
                               signer_cert=signer_cert,
                               signed_pdf=filename)

    except Exception as e:
        return render_template('error.html', msg=e)


import logging
from rich.logging import RichHandler

FORMAT = "%(message)s"

def configure_logger(level: int) -> None:
    logging.basicConfig(
        level=level, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
    )
    log.setLevel(level)

log = logging.getLogger("nettowel")
log.info("Hello, World!")


#!/usr/bin/env python
"""Provides scikit interface."""

import numpy as np
import scipy as sp
import toolz as tz
import networkx as nx
from itertools import combinations
from collections import defaultdict
from scipy.sparse import csr_matrix
from ego.encode import node_encode_graph
from ego.encode import _make_encoder
from ego.encode import make_fragmenter_func
from ego.encode import fast_hash
from ego.component import convert
from ego.component import get_subgraphs_from_graph_component
from ego.vectorize import set_feature_size

def _to_sparse_matrix(encoding, node_ids, feature_size):
    mtx = defaultdict(int)
    for c, i in zip(encoding, node_ids):
        mtx[c, i] += 1
    data, row, col = [], [], []
    for c, i in mtx:
        row.append(i)
        col.append(c)
        data.append(mtx[(c, i)])
    shape = (max(row) + 1, feature_size)
    data_matrix = csr_matrix((data, (row, col)), shape=shape, dtype=np.float64)
    return data_matrix

def _node_vectorize(graph, decomposition_funcs, preprocessors, bitmask, feature_size):
    fragmenter_func = make_fragmenter_func(decomposition_funcs, preprocessors)
    encoding, node_ids = node_encode_graph(graph, fragmenter_func, bitmask)
    data_matrix = _to_sparse_matrix(encoding, node_ids, feature_size)
    return data_matrix

def node_vectorize(graph, decomposition_funcs, preprocessors=None, nbits=16):
    feature_size, bitmask = set_feature_size(nbits=nbits)
    return _node_vectorize(graph, decomposition_funcs, preprocessors, bitmask, feature_size)

def get_distance(graph, weight=None, type_of='shortest'):
    if type_of == 'shortest':
        dist_func = nx.shortest_path_length
    elif type_of == 'resistance':
        dist_func = nx.resistance_distance
    else:
        raise Exception('Unknown type of distance:%s'%type_of)
    n = nx.number_of_nodes(graph)
    dist_mtx = np.zeros((n,n))
    for i,u in enumerate(graph.nodes()):
        for j,v in enumerate(graph.nodes()):
            if u != v :
                dist_mtx[i,j] = dist_func(graph, u, v, weight=weight)
    return dist_mtx

def get_proximity(graph, effective_radius=1, weight=None, type_of='shortest', cutoff_effective_radius_factor=2):
    if effective_radius == 0:
        n = nx.number_of_nodes(graph)
        return np.eye(n,n)
    if effective_radius is None:
        effective_radius = nx.diameter(graph) / 4
    dist_mtx = get_distance(graph, weight=weight, type_of=type_of)
    prox_mtx = np.exp( - dist_mtx / effective_radius)
    prox_mtx[dist_mtx >= cutoff_effective_radius_factor * effective_radius] = 0
    return prox_mtx

def build_node_proximity_data_matrix(nodes_mtx, data_matrix, nbits, max_num_node_features=1):
    feature_size, bitmask = set_feature_size(nbits=nbits)
    data, row, col = [], [], []
    for node_id, (node_vec, row_vec) in enumerate(zip(nodes_mtx, data_matrix)):
        all_combinations = [sorted(comb) for combinations_order in range(1, max_num_node_features + 1) for comb in combinations(node_vec.indices, combinations_order)]
        for node_feature_id_combinations in all_combinations:
            node_feature_id_combinations = list(node_feature_id_combinations)
            for remote_feature_id, remote_feature_val in zip(row_vec.indices, row_vec.data):
                new_feature_id = fast_hash(node_feature_id_combinations+[remote_feature_id], bitmask=bitmask)
                row.append(node_id)
                col.append(new_feature_id)
                data.append(remote_feature_val)
    shape = (max(row) + 1, feature_size)
    new_data_matrix = csr_matrix((data, (row, col)), shape=shape, dtype=np.float64)
    return new_data_matrix

def node_attributes_vectorize(graph, attribute_label='attributes'):
    vec_list = [graph.nodes[u].get(attribute_label, [0]) for u in graph.nodes()]
    vecs = csr_matrix(np.array(vec_list))
    return vecs

def proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, weight=None, type_of='shortest', attribute_label=None, return_node_mtx=False):
    graph = nx.convert_node_labels_to_integers(graph_orig)
    prox_mtx = csr_matrix(get_proximity(graph, effective_radius=effective_radius, weight=weight, type_of=type_of, cutoff_effective_radius_factor=cutoff_effective_radius_factor))
    nodes_mtx = node_vectorize(graph, decomposition_funcs, preprocessors, nbits)

    if attribute_label is not None:
        attribute_nodes_mtx = node_attributes_vectorize(graph, attribute_label=attribute_label)
        nodes_mtx = sp.sparse.hstack([attribute_nodes_mtx, nodes_mtx])
        nodes_mtx = csr_matrix(nodes_mtx)

    data_matrix = prox_mtx.dot(nodes_mtx)

    if return_node_mtx:
        return nodes_mtx, data_matrix
    else:
        return data_matrix

def node_proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    nodes_mtx, data_matrix = proximity_node_vectorize(graph_orig, decomposition_funcs, preprocessors=preprocessors, nbits=nbits, effective_radius=effective_radius, cutoff_effective_radius_factor=cutoff_effective_radius_factor, weight=weight, type_of=type_of, attribute_label=attribute_label, return_node_mtx=True)
    node_proximity_data_matrix = build_node_proximity_data_matrix(nodes_mtx, data_matrix, nbits, max_num_node_features)
    return node_proximity_data_matrix

def _graph_node_vectorize(graph, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    data_matrix = node_proximity_node_vectorize(graph, decomposition_funcs, preprocessors, nbits, effective_radius, cutoff_effective_radius_factor, max_num_node_features, weight, type_of, attribute_label)
    vec = csr_matrix(csr_matrix.sum(data_matrix, axis=0))
    return vec

def graph_node_vectorize(graphs, decomposition_funcs, preprocessors=None, nbits=16, effective_radius=1, cutoff_effective_radius_factor=2, max_num_node_features=1, weight=None, type_of='shortest', attribute_label=None):
    data_list = [_graph_node_vectorize(graph, decomposition_funcs, preprocessors, nbits, effective_radius, cutoff_effective_radius_factor, max_num_node_features, weight, type_of, attribute_label) for graph in graphs]
    data_matrix = sp.sparse.vstack(data_list)
    return data_matrix


import itertools

from django.contrib.auth.models import Group, Permission
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver

from guardian.shortcuts import assign_perm, get_groups_with_perms

from ..models import Agency, Tour

@receiver(post_save, sender=Agency)
def set_permissions(sender, instance, created, **kwargs):
    """
    When an Agency is created, this signal will create a managers group for it and
    assign the managers permission to edit the Agency and create Tours.
    """

    if created:
        group = Group.objects.create(name=f"{instance.title} Managers")

        perms = Permission.objects.filter(
            Q(codename__contains="agency") | Q(codename__contains="tour")
        )

        manager_perms = [
            i
            for i in perms
            if i.codename
            in [
                "view_agency",
                "change_agency",
                "add_tour",
                "view_tour",
                "change_tour",
                "delete_tour",
            ]
        ]

        group.permissions.add(*manager_perms)

        for perm in ["view_agency", "change_agency"]:
            assign_perm(perm, group, instance)

@receiver(post_save, sender=Tour)
def set_permissions(sender, instance, created, **kwargs):
    """
    When a new Tour is created, this signal will retrieve the associated manager group for it and
    assign the appropriate object level permissions for the Tour.
    """

    if created:
        groups = get_groups_with_perms(instance.agency)
        perms = ["view_tour", "change_tour", "delete_tour"]

        groups_and_perms = itertools.product(groups, perms)

        for group, perm in groups_and_perms:
            assign_perm(perm, group, instance)


import socket
import sys
import time
import datetime

sys.path.append(".\definitions")
from customer_def import customer
from retailer_def import retailer
from transaction_def import transaction
from product_def import product
from blockchain_def import blk_chain
from blockchain_def import block

customer1 = customer('customer1',10000)
chain_c1 = blk_chain()
rec = None
tranc = 0

while(1):
	s = socket.socket()
	print('press 1 to buy products, 0 to view previous transactions')
	t = input()
	if(t=='1'):
		port = 12345
		print('connecting to market...')
		time.sleep(2)
		s.connect(('127.0.0.1', port))
		print('connected to peer : (\'127.0.0.1\',{})'.format(port))
		print('Enter the product id')
		pid = input()
		print('Enter the quanitiy required')
		qty = input()
		senddet = 'reqt'+str(pid)+str(qty)
		s.send(bytes(senddet, 'utf-8'))
		x = str(s.recv(1024))
		print(x[2:len(x)-1])
		while(1):
			r = str(s.recv(1024))
			if(r[:5]=="b'yes"):
				print('Transaction successful')
				customer1.balance -= int(r[5:len(r)-1])
				ctime = datetime.datetime.now()
				transaction0 = transaction('customer1','retailer1',pid,qty,int(r[5:len(r)-1]),ctime)
				block0 = block(transaction0)
				if(tranc!=0):
					block0.prev = rec
				chain_c1.add_block(block0)
				rec = block0
				tranc += 1
				print('Disconnected')
				break
			else:
				print('Validation failed, try again later')
				break
		s.close()
	else:
		chain_c1.print_transaction()


import re
import streamlit as st
from send_email import send_email

st.header('Contact me')

with st.form(key='contact_form'):
    st.info('All fields are mandatory.')
    email = st.text_input('Your email address')
    subject = st.text_input('Enter subject').strip()
    details = st.text_area('Describe some details').strip()
    submit = st.form_submit_button()

    if submit:
        if re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email) is not None\
                and len(details) > 0 and len(subject) > 0:
            send_email(receiver=email, subject=subject, details=details)
            st.success("Form submitted successfully!")
        else:
            st.error('An error occured.')


# -*- coding: utf-8 -*-
from telnetlib import EC

import pandas as pd
import openpyxl
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from openpyxl import Workbook

driver = webdriver.Chrome(executable_path='chromedriver')
driver.maximize_window()

write_wb = Workbook();
write_ws = write_wb.active

count = 0

url = 'https://aihub.or.kr/aihub-data/vision/about'
driver.get(url)
for k in range(8):
    for j in range(8):
        if j==0 :
            clicknum = 20
        elif j==1 :
            clicknum = 20
        elif j==2 :
            clicknum = 1
        elif j==3 :
            clicknum = 12
        elif j==4 :
            clicknum = 15
        elif j==5 :
            clicknum = 20
        elif j==6 :
            clicknum = 20
        elif j==7 :
            clicknum = 20
        for i in range(clicknum):
            count += 1
            # //*[@id="block-views-block-8dae-bunlyu-block-5"]/div/div/div/div/ul/li[1]
            # //*[@id="block-views-block-8dae-bunlyu-block-5"]/div/div/div/div/ul/li[48]
            xpathhead = '//*[@id="block-views-block-8dae-bunlyu-block-3"]/div/div/div/div/ul/li['
            xpathmiddle = str(i + 1)
            xpathtail = ']'
            xpath = xpathhead + xpathmiddle + xpathtail
            driver.find_element(By.XPATH,xpath).click()
            cururl = driver.current_url
            write_ws.cell(count, 1, cururl)
            print(count)
            driver.back()
        if  j== 0 :
            driver.find_element(By.XPATH,'//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[2]/a').click()
        elif j==1 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[3]/a').click()
        elif j==2 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[4]/a').click()
        elif j==3 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[5]/a').click()
        elif j==4 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[6]/a').click()
        elif j==5 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[7]/a').click()
        elif j==6 :
            driver.find_element(By.XPATH, '//*[@id="block-stig-sub-system-main"]/div/section[2]/div/div[1]/nav/ul/li[8]/a').click()

write_wb.save('AI_Url.xlsx')

# 다음 페이지 버튼 클릭