from data_manager import DataManager
from grapher import Grapher
import matplotlib.pyplot as plt
from algorithms.knn import KNearestNeighbours
from algorithms.neighbourhood import NeighbourhoodClassifier
from algorithms.perceptron import Perceptron

class PerformanceAnalyzer:
    figure_number = 1

    def __init__(self):
        self.metrics = None

    def run(self):
        data_manager = DataManager('hw2_dataProblem.txt')
        data = data_manager.get_data()
        data = data_manager.get_column_wise_rescaled_data(data)

        self.metrics = {'knn': [], 'neighbourhood': [], 'perceptron': []}

        for i in range(9):
            x_train, y_train, x_test, y_test = data_manager.test_train_split(data)

            self.metrics['knn'].append(self.run_knn(x_train, y_train, x_test, y_test))
            self.metrics['neighbourhood'].append(self.run_neighbourhood(x_train, y_train, x_test, y_test))
            self.metrics['perceptron'].append(self.run_perceptron(x_train, y_train, x_test, y_test))

    def run_knn(self, x_train, y_train, x_test, y_test):
        knn = KNearestNeighbours(k=15)
        knn.load_data(x_train, y_train)

        y_pred = knn.classify(x_test)

        tp, tn, fp, fn = self.get_confusion_metrics(y_test, y_pred)

        metrics = {
            'test': {
                'sensitivity': tp / (tp + fn) if (tp + fn) != 0 else 999,
                'specificity': tn / (fp + tn) if (fp + tn) != 0 else 999,
                'PPV': tp / (tp + fp) if (tp + fp) != 0 else 999,
                'NPV': tn / (tn + fn) if (tn + fn) != 0 else 999,
                'hit-rate': (tp + tn) / (fp + fn + tp + tn)
            },
            'model': knn
        }

        return metrics

    def run_neighbourhood(self, x_train, y_train, x_test, y_test):
        model = NeighbourhoodClassifier(k=0.15)
        model.load_data(x_train, y_train)

        y_pred = model.classify(x_test)

        tp, tn, fp, fn = self.get_confusion_metrics(y_test, y_pred)

        metrics = {
            'test': {
                'sensitivity': tp / (tp + fn) if (tp + fn) != 0 else 999,
                'specificity': tn / (fp + tn) if (fp + tn) != 0 else 999,
                'PPV': tp / (tp + fp) if (tp + fp) != 0 else 999,
                'NPV': tn / (tn + fn) if (tn + fn) != 0 else 999,
                'hit-rate': (tp + tn) / (fp + fn + tp + tn)
            },
            'model': model
        }

        return metrics

    def run_perceptron(self, x_train, y_train, x_test, y_test):
        train_graph = Grapher()
        test_graph = Grapher()
        model = Perceptron(x_train, y_train)
        model.learn(50, 0.01, x_test, y_test, train_graph, test_graph)

        y_pred = model.test(x_test)
        tp, tn, fp, fn = self.get_confusion_metrics(y_test, y_pred)

        y_pred_train = model.test(x_train)
        tp_train, tn_train, fp_train, fn_train = self.get_confusion_metrics(y_train, y_pred_train)

        metrics = {
            'test': {
                'sensitivity': tp / (tp + fn) if (tp + fn) != 0 else 999,
                'specificity': tn / (fp + tn) if (fp + tn) != 0 else 999,
                'PPV': tp / (tp + fp) if (tp + fp) != 0 else 999,
                'NPV': tn / (tn + fn) if (tn + fn) != 0 else 999,
                'hit-rate': (tp + tn) / (fp + fn + tp + tn)
            },
            'train': {
                'sensitivity': tp_train / (tp_train + fn_train) if (tp_train + fn_train) != 0 else 999,
                'specificity': tn_train / (fp_train + tn_train) if (fp_train + tn_train) != 0 else 999,
                'PPV': tp_train / (tp_train + fp_train) if (tp_train + fp_train) != 0 else 999,
                'NPV': tn_train / (tn_train + fn_train) if (tn_train + fn_train) != 0 else 999,
                'hit-rate': (tp_train + tn_train) / (fp_train + fn_train + tp_train + tn_train)
            },
            'model': model,
            'train-error': {'epochs': train_graph.x, 'error': train_graph.y}
        }

        return metrics

    @staticmethod
    def get_confusion_metrics(y_test, y_pred):
        tp = 0
        tn = 0
        fn = 0
        fp = 0

        for i in range(len(y_test)):
            if y_test[i] == 1 and y_pred[i] == 1:
                tp += 1
            elif y_test[i] == 0 and y_pred[i] == 0:
                tn += 1
            elif y_test[i] == 1 and y_pred[i] == 0:
                fn += 1
            elif y_test[i] == 0 and y_pred[i] == 1:
                fp += 1

        return tp, tn, fp, fn

    def plot_trails(self):
        """
        Performance on Individual Trials: For the perceptron algorithm, four bar plots with nine groups
        of two bars each. Each plot will be for one of the metrics (sensitivity, specificity, PPV and NPV) ,
        with each pair of bars showing the value of the metric for final training and final testing on one trial.
        The training bar should be on the left, the testing on the right.
        For the other two algorithms, four bar plots with nine bars each. Each plot will be for one of the metrics
        sensitivity, specificity, PPV and NPV, with each bar showing the value of the metric for testing on one
        trial.
        Thus, in all, you will have twelve total plots - four for each classifier. The four graphs for each classifier
        should be grouped into one figure with either four panels in one row or a 2×2 arrangement. Thus, this
        part will have a total of three figures, each with four panels.
        """
        self.plot_trails_single_bars(self.metrics['knn'], 'KNN Classifier')
        self.plot_trails_single_bars(self.metrics['neighbourhood'], 'Neighbourhood Classifier')
        self.plot_trails_double_bars(self.metrics['perceptron'], 'Perceptron Classifier')

    @staticmethod
    def plot_trails_double_bars(metrics, title):
        fig, axs = Grapher.create_figure(1, 4, PerformanceAnalyzer.figure_number, (16, 4))
        PerformanceAnalyzer.figure_number += 1
        graphs = [Grapher() for i in range(4)]
        trails_count = [i + 1 for i in range(len(metrics))]

        for i in range(len(metrics)):
            graphs[0].record(metrics[i]['train']['sensitivity'], metrics[i]['test']['sensitivity'])
            graphs[1].record(metrics[i]['train']['specificity'], metrics[i]['test']['specificity'])
            graphs[2].record(metrics[i]['test']['PPV'], metrics[i]['test']['PPV'])
            graphs[3].record(metrics[i]['train']['NPV'], metrics[i]['test']['NPV'])

        for i in range(4):
            axs[i].bar(trails_count, graphs[i].x, label='Train', width=0.35)
            axs[i].bar([t + 0.35 for t in trails_count], graphs[i].y, label='Test', width=0.35)
            axs[i].set_xticks(trails_count)
            axs[i].set_xlabel('Trails')
            axs[i].legend(['Train', 'Test'])
            axs[i].set_ylim([0.6, 1.01])

        fig.suptitle(title)

        axs[0].set_title('sensitivity')
        axs[1].set_title('specificity')
        axs[2].set_title('ppv')
        axs[3].set_title('npv')

        fig.savefig(f'figures/{title}_metric_bars.png')

    @staticmethod
    def plot_trails_single_bars(metrics, title):
        fig, axs = Grapher.create_figure(1, 4, PerformanceAnalyzer.figure_number, (16, 4))
        PerformanceAnalyzer.figure_number += 1
        graphs = [Grapher() for i in range(4)]
        for i in range(len(metrics)):
            graphs[0].record(i + 1, metrics[i]['test']['sensitivity'])
            graphs[1].record(i + 1, metrics[i]['test']['specificity'])
            graphs[2].record(i + 1, metrics[i]['test']['PPV'])
            graphs[3].record(i + 1, metrics[i]['test']['NPV'])

        for i in range(4):
            axs[i].bar(graphs[i].x, graphs[i].y)
            axs[i].set_xticks(graphs[i].x)
            axs[i].set_xlabel('Trails')
            axs[i].set_ylim([0.6, 1.01])

        fig.suptitle(title)

        axs[0].set_title('sensitivity')
        axs[1].set_title('specificity')
        axs[2].set_title('ppv')
        axs[3].set_title('npv')

        fig.savefig(f'figures/{title}_metric_bars.png')

    def plot_average_performance(self):
        """
        Average Performance: A table giving the mean values of sensitivity, specificity, PPV, NPV, and
        hit-rate for each algorithm on testing data averaged over all 9 trials, along with the standard deviation
        for each case. The standard deviations will be indicated as ± values after the mean, e.g., 0.93 ± 0.03.
        Each table will have five data rows – one for each metric – and three data columns - one for each
        algorithm.
        """
        sensitivities = [[], [], []]
        specificities = [[], [], []]
        ppvs = [[], [], []]
        npvs = [[], [], []]
        hit_rates = [[], [], []]
        for i in range(len(self.metrics['knn'])):
            sensitivities[0].append(self.metrics['knn'][i]['test']['sensitivity'])
            sensitivities[1].append(self.metrics['neighbourhood'][i]['test']['sensitivity'])
            sensitivities[2].append(self.metrics['perceptron'][i]['test']['sensitivity'])

            specificities[0].append(self.metrics['knn'][i]['test']['specificity'])
            specificities[1].append(self.metrics['neighbourhood'][i]['test']['specificity'])
            specificities[2].append(self.metrics['perceptron'][i]['test']['specificity'])

            ppvs[0].append(self.metrics['knn'][i]['test']['PPV'])
            ppvs[1].append(self.metrics['neighbourhood'][i]['test']['PPV'])
            ppvs[2].append(self.metrics['perceptron'][i]['test']['PPV'])

            npvs[0].append(self.metrics['knn'][i]['test']['NPV'])
            npvs[1].append(self.metrics['neighbourhood'][i]['test']['NPV'])
            npvs[2].append(self.metrics['perceptron'][i]['test']['NPV'])

            hit_rates[0].append(self.metrics['knn'][i]['test']['hit-rate'])
            hit_rates[1].append(self.metrics['neighbourhood'][i]['test']['hit-rate'])
            hit_rates[2].append(self.metrics['perceptron'][i]['test']['hit-rate'])

        sensitivities_avg = [self.get_avg_with_std(x) for x in sensitivities]
        specificities_avg = [self.get_avg_with_std(x) for x in specificities]
        ppvs_avg = [self.get_avg_with_std(x) for x in ppvs]
        npvs_avg = [self.get_avg_with_std(x) for x in npvs]
        hit_rates_avg = [self.get_avg_with_std(x) for x in hit_rates]

        data = [sensitivities_avg, specificities_avg, ppvs_avg, npvs_avg, hit_rates_avg]
        row_labels = ['sensitivity', 'specificity', 'PPV', 'NPV', 'hit-rate']
        col_labels = ['knn', 'neighbourhood', 'perceptron']

        fig, ax = plt.subplots(figsize=(7, 3))

        # fig.patch.set_visible(False)
        ax.axis('off')
        ax.axis('tight')
        ax.set_title('Averaged Performance On Trails')

        ax.table(data, colLabels=col_labels, rowLabels=row_labels, loc='center')

        fig.savefig('figures/average_performances.png')

    @staticmethod
    def get_avg_with_std(data):
        avg = round(sum(data) / len(data), 2)
        std = PerformanceAnalyzer.get_standard_deviation(data, avg)
        return f'{avg} ± {std}'

    @staticmethod
    def get_standard_deviation(data, mean):
        return round((sum([(x - mean) ** 2 for x in data]) / len(data)) ** 0.5, 2)

    def plot_training_error(self):
        """
        Trial-Wise Training Error Time-Series for the Perceptrons: For the perceptron case only:
        graphs showing the error (1 - hit-rate) on the training set plotted against epoch over the duration of
        training for the nine trials . Each plot will start with the initial error and plot the error at every tenth
        epoch. You should plot all nine curves on the same graph. You should choose graph properties (line
        thicknesses, colors, etc.) for maximum clarity.
        """
        fig, axs = Grapher.create_figure(1, 1, PerformanceAnalyzer.figure_number, figsize=(6, 4))
        PerformanceAnalyzer.figure_number += 1

        lines = []

        for i in range(len(self.metrics['perceptron'])):
            x = self.metrics['perceptron'][i]['train-error']['epochs']
            y = self.metrics['perceptron'][i]['train-error']['error']
            line, = axs.plot(x, y, label=f'Trail {i + 1}', linewidth=((len(self.metrics['perceptron']) - i) / 2))
            lines.append(line)

        plt.legend(lines, [f'Trail {i + 1}' for i in range(len(lines))])
        axs.set_title('Perceptron training error for each epoch')
        axs.set_xlabel('Epochs')
        axs.set_ylabel('Error')

        fig.savefig('figures/training_error.png')

    def plot_mean_training_error(self):
        """
        Mean Training Error for the Perceptron: For the perceptron case only: a graph showing the
        mean training error averaged over the 9 trials plotted against time. This graph will have only one
        plotted curve with a datapoint every 10 epochs, where each point of the curve is the average value
        of the 9 points at the corresponding time in the graph above. At each plotted point, put error bars
        indicating the ± standard deviation over the 9 trials.
        """
        num_trails = len(self.metrics['perceptron'])
        if num_trails == 0:
            return
        epochs = self.metrics['perceptron'][0]['train-error']['epochs']
        num_epochs = len(epochs)

        avg_errors = []
        error_labels = []
        for epoch_i in range(num_epochs):
            errors = []
            for trail_j in range(num_trails):
                errors.append(self.metrics['perceptron'][trail_j]['train-error']['error'][epoch_i])
            avg = round(sum(errors) / num_trails, 2)
            avg_errors.append(avg)
            error_labels.append(f'{avg} ± {self.get_standard_deviation(errors, avg)}')

        fig, axs = Grapher.create_figure(1, 1, 1, figsize=(6, 4))
        axs.plot(epochs, avg_errors)

        for i in range(len(error_labels)):
            axs.annotate(error_labels[i], (epochs[i], avg_errors[i]))

        axs.set_title('Perceptron average training error')
        axs.set_xlabel('Epochs')
        axs.set_ylabel('Error')

        fig.savefig('figures/mean_training_error.png')

    @staticmethod
    def get_sample_space(num_points):
        x = [i / num_points for i in range(num_points + 1)]

        points = []
        for i in x:
            for j in x:
                points.append((i, j))

        return points

    def plot_knn_decision_boundary(self):
        """
        Best k-NN Decision Boundary: For k-NN only: a plot of the data in the feature space indicating
        the decision boundaries found by the classifier in the best trial. This will involve sampling the feature
        space as we discussed in class, and will only give an approximate boundary.
        """
        best_metric = self.get_best_model(self.metrics['knn'])
        model = best_metric['model']

        sample = self.get_sample_space(20)
        classes = model.classify(sample)

        x0, y0, x1, y1 = self.split_sample_with_classes(sample, classes)

        fig, axs = Grapher.create_figure(1, 1, 1, figsize=(5, 5))
        axs.scatter([i[0] for i in x0], [i[1] for i in x0])
        axs.scatter([i[0] for i in x1], [i[1] for i in x1])

        axs.set_title('KNN Decision Boundary')
        axs.set_xlabel('L')
        axs.set_ylabel('P')
        axs.legend(['0', '1'])

        fig.savefig('figures/knn_decision_boundary.png')

    @staticmethod
    def split_sample_with_classes(sample, classes):
        x1 = []
        x0 = []
        y0 = []
        y1 = []
        for i in range(len(classes)):
            if classes[i] == 0:
                x0.append(sample[i])
                y0.append(classes[i])
            elif classes[i] == 1:
                x1.append(sample[i])
                y1.append(classes[i])
        return x0, y0, x1, y1

    @staticmethod
    def get_best_model(metrics):
        max_metric = max(metrics, key=lambda metric: metric['test']['hit-rate'])
        return max_metric

    def plot_best_neighbourhood_boundary(self):
        """
        Best Neighborhood Classifier Decision Boundary: For neighborhood-based classifier only: a
        plot of the data in the feature space indicating the decision boundaries found by the classifier in the
        best trial. The approach used will be the same as for k-NN, i.e., sample the feature space to get
        classification.
        """
        best_metric = self.get_best_model(self.metrics['neighbourhood'])
        model = best_metric['model']

        sample = self.get_sample_space(20)
        classes = model.classify(sample)

        x0, y0, x1, y1 = self.split_sample_with_classes(sample, classes)

        fig, axs = Grapher.create_figure(1, 1, 1, figsize=(5, 5))
        axs.scatter([i[0] for i in x0], [i[1] for i in x0])
        axs.scatter([i[0] for i in x1], [i[1] for i in x1])

        axs.set_title('Neighbourhood Decision Boundary')
        axs.set_xlabel('L')
        axs.set_ylabel('P')
        axs.legend(['0', '1'])

        fig.savefig('figures/neighbourhood_decision_boundary.png')

    def plot_perceptron_boundary(self):
        """
        Perceptron Decision Boundary: For the perceptron classifier only: Take the perceptron (out of
        the 9) that had the best performance, and plot the decision boundary it found in the feature space.
        Your figure size and L-P coordinate ranges for this figure and the previous two figures should be the
        same so the three figures can be compared.
        """
        best_metric = self.get_best_model(self.metrics['perceptron'])
        model = best_metric['model']

        sample = self.get_sample_space(20)
        classes = model.test(sample)
        x0, y0, x1, y1 = self.split_sample_with_classes(sample, classes)

        c, a, b = model.weights
        c = c - 0.5
        sample_ys = [x[1] for x in sample]
        boundary_line_xs = self.get_straight_line_xs(a, b, c, sample_ys)

        fig, axs = Grapher.create_figure(1, 1, 1, figsize=(5, 5))

        axs.scatter([i[0] for i in x0], [i[1] for i in x0])
        axs.scatter([i[0] for i in x1], [i[1] for i in x1])
        axs.plot(boundary_line_xs, sample_ys, 'g')

        axs.set_title('Perceptron Decision Boundary')
        axs.set_xlabel('L')
        axs.set_ylabel('P')
        axs.legend(['Decision Boundary', '0', '1'])

        fig.savefig('figures/perceptron_decision_boundary.png')

    @staticmethod
    def get_straight_line_xs(a, b, c, ys):
        return [(-b * y - c) / a for y in ys]

    @staticmethod
    def save_metrics(metrics):
        import json
        with open('metrics.json', 'w') as fp:
            json.dump(metrics, fp)

def main():
    performance = PerformanceAnalyzer()
    performance.run()

    # PerformanceAnalyzer.save_metrics(performance.metrics)

    # performance.plot_trails()
    # performance.plot_average_performance()
    # performance.plot_training_error()
    # performance.plot_mean_training_error()
    # performance.plot_knn_decision_boundary()
    # performance.plot_best_neighbourhood_boundary()
    # performance.plot_perceptron_boundary()

if __name__ == "__main__":
    main()


"""remove_pro_show_invoices_on_pro_portal
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "01e4e1b7f714"
down_revision = "0511102bcb21"
branch_labels = None
depends_on = None

def upgrade() -> None:
    op.execute("DELETE FROM feature WHERE name = 'SHOW_INVOICES_ON_PRO_PORTAL'")

def downgrade() -> None:
    op.execute(
        """
        INSERT INTO feature (name, description, "isActive")
        VALUES ('SHOW_INVOICES_ON_PRO_PORTAL', 'Activer l''affichage des remboursements sur le portail pro', True)
        """
    )


"""
Controller for contact lists
"""

import cgi
from kernel.models import *
from src.controllers.request import *
from src.serializer import serialize_one
from src.regex import REGEX_EMAIL, REGEX_NAME, REGEX_SLUG
from django.shortcuts import render, redirect
from django.db.models import F, Q, Count
import csv

import pdb

@login_required()
def index(request, user):
    """
    List Dashboard
    """
    profiles = Profile.objects.filter(managers = user)
    profile = profiles[0]
    lists = List.objects.filter(owner = profile, is_deleted = False)
    listsCount = lists.count()
    for lt in lists:
        list_items = List_item.objects.filter(_list = lt)
        lt.items = list_items.count()
    return render(request, 'list/index.html', locals())

@login_required()
def list(request, lt, user):
    """
    Single List Page
    """
    if not List.objects.filter(id = lt, is_deleted = False).exists():
        return redirect('list:index')
    profiles = Profile.objects.filter(managers = user)
    profile = profiles[0]
    if List.objects.filter(id = lt).exists():
        lt = List.objects.get(id = lt)
        if not Profile_manager.objects.filter(profile = lt.owner, user = user).exists():
            return redirect('index:index')
        list_items = List_item.objects.filter(_list = lt).order_by('-id')
        pastEventList = {}
        profiles = Profile.objects.filter(managers = user)
        pids = []
        for profile in profiles:
            pids.append(profile.id)
        pastEvents = Event.objects.filter(Q(end_time = None,
                                            start_time__lt = timezone.now()) |
                                          Q(end_time__isnull = False,
                                            end_time__lt = timezone.now()),
                                          is_launched = True,
                                          organizers__in = pids) \
                                  .order_by('-start_time')
        eids = []
        for pastEvent in pastEvents:
            eids.append(pastEvent.id)
        purchases = Purchase.objects.filter(Q(checkout = None) |
                                    Q(checkout__is_charged = True,
                                      checkout__is_refunded = False),
                                    event__in = eids,
                                    is_expired = False)
        for purchase in purchases:
            if purchase.event.id in pastEventList:
                pastEventList[purchase.event.id]['quantity'] += 1
            else:
                pastEventList[purchase.event.id] = {
                    'id': purchase.event.id,
                    'name': purchase.event.name,
                    'quantity': 1
                }
    else:
        return redirect('index:index')
    if not Profile_manager.objects.filter(profile = lt.owner, user = user).exists():
        return redirect('index:index')
    return render(request, 'list/list.html', locals())
@login_required()
@validate('POST', ['profile', 'name', 'is_hidden'])
def create(request, params, user):
    """
    Create an empty list
    """
    if not Profile.objects.filter(id = params['profile']).exists():
        response = {
            'status':'FAIL',
            'error':'PROFILE_NOT_FOUND',
            'message':'The profile doesn\'t exist.'
        }
        return json_response(response)
    profile = Profile.objects.get(id = params['profile'])
    # Check if the user is a manager of the profile
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the profile.'
        }
        return json_response(response)
    params['name'] = cgi.escape(params['name'])
    if len(params['name']) > 50:
        response = {
            'status':'FAIL',
            'error':'INVALID_NAME',
            'message':'The name must be within 50 characters.'
        }
        return json_response(response)
    lt = List(owner = profile, name = params['name'],
              is_hidden = params['is_hidden'])
    lt.save()
    response = {
        'status':'OK',
        'list':serialize_one(lt)
    }
    return json_response(response)

@login_required()
@validate('POST', ['id'], ['name'])
def edit(request, params, user):
    """
    Edit a list
    """
    if not List.objects.filter(id = params['id'], is_deleted = False).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_NOT_FOUND',
            'message':'The list doesn\'t exist.'
        }
        return json_response(response)
    lt = List.objects.get(id = params['id'])
    profile = lt.owner
    # Check if the user is a manager of the profile
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    if params['name'] is not None:
        params['name'] = cgi.escape(params['name'])
        if len(params['name']) > 50:
            response = {
                'status':'FAIL',
                'error':'INVALID_NAME',
                'message':'The name must be within 50 characters.'
            }
            return json_response(response)
        else:
            lt.name = params['name']
    lt.save()
    response = {
        'status':'OK',
        'list':serialize_one(lt)
    }
    return json_response(response)

@login_required()
@validate('POST', ['id'])
def remove_item(request, params, user):
    """
    Remove an item from a list
    """
    if not List_item.objects.filter(id = params['id']).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_ITEM_NOT_FOUND',
            'message':'The list item doesn\'t exist.'
        }
        return json_response(response)
    item = List_item.objects.get(id = params['id'])
    lt = item._list
    profile = lt.owner
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    item.delete()
    response = {
            'status':'OK'
    }
    return json_response(response)

@login_required()
@validate('POST', ['id', 'first_name', 'last_name', 'email'], ['note'])
def add_item(request, params, user):
    """
    Add an item to a list
    """
    if not List.objects.filter(id = params['id']).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_NOT_FOUND',
            'message':'The list doesn\'t exist.'
        }
        return json_response(response)
    lt = List.objects.get(id = params['id'])
    profile = lt.owner
    # Check if the user is a manager of the profile
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    # Check email format
    if not REGEX_EMAIL.match(params['email']):
        response = {
            'status':'FAIL',
            'error':'INVALID_EMAIL',
            'message':'Email format is invalid.'
        }
        return json_response(response)
    # Check if the name is valid
    if (not REGEX_NAME.match(params['first_name']) or
        not REGEX_NAME.match(params['last_name'])):
        response = {
            'status':'FAIL',
            'error':'INVALID_NAME',
            'message':'First or last name contain illegal characters.'
        }
        return json_response(response)
    if len(params['first_name']) > 50 or len(params['last_name']) > 50:
        response = {
            'status':'FAIL',
            'error':'NAME_TOO_LONG',
            'message':'First or last name is too long.'
        }
        return json_response(response)
    item, created = List_item.objects.get_or_create(_list = lt,
                                                    email = params['email'])
    item.first_name = params['first_name']
    item.last_name = params['last_name']
    if params['note']:
        item.note = params['note']
    item.save()
    response = {
        'status':'OK',
        'list':serialize_one(lt),
        'item':serialize_one(item)
    }
    return json_response(response)

@login_required()
@validate('POST', ['id', 'event'])
def add_from_event(request, params, user):
    """
    Add items from an event
    """
    if not List.objects.filter(id = params['id'], is_deleted = False).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_NOT_FOUND',
            'message':'The list doesn\'t exist.'
        }
        return json_response(response)
    lt = List.objects.get(id = params['id'])
    profile = lt.owner
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    if not Event.objects.filter(id = params['event'],
                                is_deleted = False).exists():
        response = {
            'status':'FAIL',
            'error':'EVENT_NOT_FOUND',
            'message':'The event doesn\'t exist.'
        }
        return json_response(response)
    event = Event.objects.get(id = params['event'])
    if not Organizer.objects.filter(event = event,
                                    profile__managers = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the event.'
        }
        return json_response(response)
    purchases = Purchase.objects.filter(Q(checkout = None) |
                                        Q(checkout__isnull = False,
                                          checkout__is_charged = True),
                                        event = event,
                                        is_expired = False) \
                                .prefetch_related('owner')
    added = 0
    duplicates = 0
    for purchase in purchases:
        if not List_item.objects.filter(_list = lt, email = purchase.owner.email).exists():
            item = List_item(_list = lt, email = purchase.owner.email, first_name = purchase.owner.first_name, last_name = purchase.owner.last_name)
            item.save()
            added += 1
        else:
            duplicates += 1
    response = {
        'status':'OK',
        'list':serialize_one(lt),
        'added': added,
        'duplicates': duplicates
    }
    return json_response(response)

@login_required()
@validate('POST', ['csv'])
def prepare_csv(request, params, user):
    """
    Prepare CSV For List Upload
    """
    if not Csv.objects.filter(id = params['csv']).exists():
        response = {
            'status':'FAIL',
            'error':'EVENT_NOT_FOUND',
            'message':'The event doesn\'t exist.'
        }
        return json_response(response)
    csv_file = Csv.objects.get(id = params['csv'])
    csvfile = csv_file.source.file
    reader = csv.reader(csvfile)
    results = {}
    for num, row in enumerate(reader):
        results[num] = row
    csvfile.close()
    response = {
        'status':'OK',
        'results': results
    }
    return json_response(response)

@login_required()
@validate('POST', ['id', 'csv', 'format'])
def add_from_csv(request, params, user):
    """
    Add items from a csv file
    """
    if not List.objects.filter(id = params['id'], is_deleted = False).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_NOT_FOUND',
            'message':'The list doesn\'t exist.'
        }
        return json_response(response)
    lt = List.objects.get(id = params['id'])
    profile = lt.owner
    # Check if the user is a manager of the profile
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    # Check if CSV Exists
    if not Csv.objects.filter(id = params['csv']).exists():
        response = {
            'status':'FAIL',
            'error':'CSV_NOT_FOUND',
            'message':'The csv doesn\'t exist.'
        }
        return json_response(response)
    csv_file = Csv.objects.get(id = params['csv'])
    csvfile = csv_file.source.file
    reader = csv.reader(csvfile)
    # Load Format {field_type:col}
    format = json.loads(params['format'])
    added = 0
    duplicates = 0
    for num, row in enumerate(reader):
        if 'email' in format and len(row) >= format['email'] and REGEX_EMAIL.match(row[format['email']]):
            if not List_item.objects.filter(_list = lt, email = row[format['email']]).exists():
                item = List_item(_list = lt, email = row[format['email']])
                item.first_name = row[format['first_name']]
                if 'last_name' in format:
                    item.last_name = row[format['last_name']]
                item.save()
                added += 1
            else:
                duplicates += 1

    response = {
        'status':'OK',
        'list':serialize_one(lt),
        'added': added,
        'duplicates': duplicates
    }
    return json_response(response)

@login_required()
@validate('POST', ['id'])
def delete(request, params, user):
    """
    Delete List
    """
    if not List.objects.filter(id = params['id'], is_deleted = False).exists():
        response = {
            'status':'FAIL',
            'error':'LIST_NOT_FOUND',
            'message':'The list doesn\'t exist.'
        }
        return json_response(response)
    lt = List.objects.get(id = params['id'])
    profile = lt.owner
    # Check if the user is a manager of the profile
    if not Profile_manager.objects.filter(profile = profile,
                                          user = user).exists():
        response = {
            'status':'FAIL',
            'error':'NOT_A_MANAGER',
            'message':'You don\'t have permission for the list.'
        }
        return json_response(response)
    lt.is_deleted = True
    lt.save()
    response = {
        'status': 'OK',
        'list': serialize_one(lt)
    }
    return json_response(response)


from setuptools import setup

package_name = 'ros2_rpi_camera'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        ('share/' + package_name + '/resource', ['resource/image.png'])
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='pablo',
    maintainer_email='pawel.rawicki@gmail.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'ros2_rpi_camera = ros2_rpi_camera.ros2_rpi_camera:main'
        ],
    },
)


import requests
import os

TOKEN = os.environ['TOKEN']

def img(chat_id:str, photo:str):
    URl = f'https://api.telegram.org/bot{TOKEN}/sendPhoto'
    param = {
        'chat_id':chat_id
    }
    files = {
        'photo':photo
    }

    r = requests.post(url=URl, params=param, files=files)
    r.json()

photo = open('logo.jpg', 'rb').read()

chat_id = '996172963'

img(chat_id, photo)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from app.models import Publisher, Author, Book

from django.contrib import admin

# Register your models here.
class PublisherAdmin(admin.ModelAdmin):
    list_display = ('name', 'address', 'city', 'state_province', 'country', 'website')
    list_filter = ('city', 'state_province', 'country')

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('salutation', 'first_name', 'last_name', 'email')
    list_filter = ('salutation',)

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date')
    list_filter = ('publication_date',)
    ordering = ('-publication_date',)
    search_fields = ('title',)

admin.site.register(Publisher, PublisherAdmin)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)


from turtle import Screen
from paddle import Paddle
from score_board import PlayBoard, Score
from ball import Ball
import time

screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)

right_paddle = Paddle((350, 0))
left_paddle = Paddle((-350, 0))

r_paddle_score = Score((20, 250))
l_paddle_score = Score((-20, 250))

play_board = PlayBoard()
ball = Ball()

screen.listen()
screen.onkey(fun=right_paddle.up, key="Up")
screen.onkey(fun=right_paddle.down, key="Down")
screen.onkey(fun=left_paddle.up, key="w")
screen.onkey(fun=left_paddle.down, key="s")

game_is_on = True
while game_is_on:
    time.sleep(ball.move_speed)
    ball.move()
    screen.update()

    # Detect collision with wall
    if ball.ycor() > 280 or ball.ycor() < - 280:
        ball.bounce_y()

    # Detect collision with paddle
    if ball.xcor() > 320 and ball.distance(right_paddle) < 50 or ball.xcor() < -320 and ball.distance(left_paddle) < 50:
        ball.bounce_x()

    # Detect paddle miss and add score
    if ball.xcor() > 370:
        ball.refresh()
        l_paddle_score.add_score()
    if ball.xcor() < -370:
        ball.refresh()
        r_paddle_score.add_score()

screen.exitonclick()


# exercise 1
class Family:
    members = [
    {'name':'Michael','age':35,'gender':'Male','is_child':False},
    {'name':'Sarah','age':32,'gender':'Female','is_child':False}
    ]

    def __init__(self, members, last_name):
        self.members = members
        self.last_name = last_name

    @classmethod
    def born(cls, **kwargs):
        member = {}
        for key,value in kwargs.items():
            member[key] = value
        cls.members.append(member)

    @staticmethod
    def is_18(name):
        for member in Family.members:
            if member["name"] == name:
                if member["is_child"] == True:
                    return True
                else:
                    return False

    @staticmethod
    def show_members():
        for member in Family.members:
            print(f'''name: {member["name"]}
age: {member["age"]}
gender: {member["gender"]}
is child: {member["is_child"]}''')

Family.born(name="maor", age="26", gender="male", is_child=False)
print(Family.members)
print(Family.is_18("maor"))
Family.show_members()
print("------")

# exercise 2
class TheIncredibles(Family):
    members = [
    {'name':'Michael','age':35,'gender':'Male','is_child':False, 'power': "flying", 'incredible_name':'superman'},
    {'name':'Sarah','age':3,'gender':'Female','is_child':True, 'power': "strength", 'incredible_name':'hulk'}
    ]

    def __init__(self, members):
        self.members = members

    @staticmethod
    def use_power(name):
        for member in TheIncredibles.members:
            if member["name"] == name:
                if member["is_child"] == False:
                    print(f'the power of {member["name"]} is {member["power"]}')
                else:
                    raise Exception('under 18 cant show power')

    @staticmethod
    def indredible_presentation():
        for member in TheIncredibles.members:
            print(f'''name: {member["name"]}
age: {member["age"]}
gender: {member["gender"]}
is child: {member["is_child"]}
power: {member["power"]}
incredible name: {member["incredible_name"]}
''')

# TheIncredibles.use_power('Sarah')
TheIncredibles.use_power('Michael')

TheIncredibles.indredible_presentation()
TheIncredibles.born(name="Jack", age="2", gender="male", is_child=True, power="unkown", incredible_name="baby")
TheIncredibles.indredible_presentation()


import os
import shutil
import warnings
from pathlib import Path
from typing import Dict, List, Optional

import yaml

warnings.filterwarnings("ignore")
import torch
import wandb
import hydra
import omegaconf
import pytorch_lightning as pl
from omegaconf import OmegaConf
from pytorch_lightning import Callback
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint, RichProgressBar
from pytorch_lightning.loggers import WandbLogger

from src.common.utils import load_envs, PROJECT_ROOT, gpus, enable_16precision, set_determinism_the_old_way
from src.pl_data_modules import BasePLDataModule
from src.pl_modules import BasePLModule

# Load environment variables
load_envs()

def build_callbacks(
    cfg: omegaconf.DictConfig, checkpoint_path: str = ""
) -> List[Callback]:
    # callbacks declaration

    callbacks_store.append(
        ModelCheckpoint(
            **cfg.train.model_checkpoint,
            dirpath=checkpoint_path,
        )
    )
    return callbacks_store

def get_dataset_info(conf: omegaconf.DictConfig) -> Dict[str, List[Dict[str, int]]]:
    """
    Return dict which contains each split (train, validation, test) with the number of samples belonging to the labels selected
    e.g.
     {'train': [{'COVID': 563}, {'bacteria': 1285}, {'virus': 1035}, {'normal': 950}],
     'validation': [{'COVID': 15}, {'bacteria': 16}, {'virus': 16}, {'normal': 16}],
     'test': [{'COVID': 17}, {'bacteria': 18}, {'virus': 18}, {'normal': 18}]}
    """
    class_to_use: List[str] = conf.labels.class_to_use
    dataset_path: Dict[str, str] = {
        "train": conf.data.dataset.train_path,
        "validation": conf.data.dataset.validation_path,
        "test": conf.data.dataset.test_path,
    }
    dataset_info: Dict[str, List[Dict[str, int]]] = {
        "train": [],
        "validation": [],
        "test": [],
    }
    for key, path in dataset_path.items():
        for label in class_to_use:
            num_samples = len(os.listdir(f"{path}/{label}"))
            label_num_samples: Dict[str, int] = {label: num_samples}
            dataset_info[key].append(label_num_samples)
    return dataset_info

def train(conf: omegaconf.DictConfig) -> None:
    # reproducibility

    pl.seed_everything(conf.train.seed)
    set_determinism_the_old_way(True)
    logger = None

    # data module declaration
    pl_data_module = BasePLDataModule(conf)

    # main module declaration
    pl_module = BasePLModule(conf)

    # callbacks declaration

    if conf.logging.log:
        class_to_use_str: str = (
            str(conf.labels.class_to_use)
            .strip("[]")
            .replace(",", "-")
            .replace("'", "")
            .replace(" ", "")
        )
        hydra.utils.log.info(f"Instantiating <WandbLogger>")
        wandb_config = conf.logging
        logger: WandbLogger = WandbLogger(
            **conf.logging.wandb,
            group=f"{conf.data.dataset.name}-{class_to_use_str}-test",
            name=conf.train.model_type,
            log_model=True,
        )
        hydra.utils.log.info(f"W&B is now watching <{wandb_config.watch.log}>!")
        logger.watch(
            pl_module, log=wandb_config.watch.log, log_freq=wandb_config.watch.log_freq
        )

        dataset_info = get_dataset_info(conf=conf)
        with open(Path(logger.experiment.dir) / "dataset-info.yaml", "w") as outfile:
            yaml.dump(dataset_info, outfile, default_flow_style=False)
        yaml_conf: str = OmegaConf.to_yaml(cfg=conf)
        (Path(logger.experiment.dir) / "hparams.yaml").write_text(yaml_conf)

    callbacks_store: List[Callback] = [RichProgressBar()]

    if not torch.cuda.is_available():
        conf.train.pl_trainer.gpus = 0
        conf.train.pl_trainer.precision = 32

    trainer = pl.Trainer(
        **conf.train.pl_trainer,
        callbacks=callbacks_store,
        logger=logger
    )
    # module test
    model_path: str = (
        f"{PROJECT_ROOT}/ckpt/{conf.data.subset_percentage}/{conf.train.model_type}"
    )
    best_model_ckpt: str = model_path + "/" + str(os.listdir(model_path)[0])
    hydra.utils.log.info(f"path best_model: {best_model_ckpt}")
    # main module declaration
    pl_module = BasePLModule.load_from_checkpoint(best_model_ckpt, strict=False)
    trainer.test(pl_module, datamodule=pl_data_module)

    if conf.logging.log:
        logger.experiment.finish()

@hydra.main(config_path="../conf", config_name="root")
def main(conf: omegaconf.DictConfig):
    import os

    print("Working directory : {}".format(os.getcwd()))
    train(conf)

if __name__ == "__main__":
    main()


import sys
import ast

from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, DoubleType, Row

if len(sys.argv) == 1:
    # hdfs://localhost:9000/full/meta_Electronics.json
    # hdfs://localhost:9000/full/avg_rating.csv
    # hdfs://localhost:9000/full/output
    INPUT_META_FILE = 'hdfs://localhost:9000/part/sample_100_meta.json'
    INPUT_AVG_RATING_FILE = 'hdfs://localhost:9000/part/avg_rating.csv'
    OUTPUT_DIR = 'hdfs://localhost:9000/part/output'
elif len(sys.argv) == 2:
    INPUT_META_FILE = sys.argv[1]
    INPUT_AVG_RATING_FILE = 'hdfs://localhost:9000/part/avg_rating.csv'
    OUTPUT_DIR = 'hdfs://localhost:9000/part/output'
elif len(sys.argv) == 3:
    INPUT_META_FILE = sys.argv[1]
    INPUT_AVG_RATING_FILE = sys.argv[2]
    OUTPUT_DIR = 'hdfs://localhost:9000/part/output'
else:
    INPUT_META_FILE = sys.argv[1]
    INPUT_AVG_RATING_FILE = sys.argv[2]
    OUTPUT_DIR = sys.argv[3]

def getReviewMeta(reviewMeta):
    decodeReviewMeta = ast.literal_eval(reviewMeta)
    try:
        asin = decodeReviewMeta["asin"]
    except KeyError:
        asin = None
    try:
        title = decodeReviewMeta["title"]
    except:
        title = None
    return (asin, title)

conf = SparkConf().setAppName("task2")
sc = SparkContext(conf=conf)
spark = SparkSession.builder.config(conf=conf).getOrCreate()
rddReviewMeta = sc.textFile(INPUT_META_FILE)

rddReviewMetaDf = rddReviewMeta.map(lambda row: getReviewMeta(row)).toDF(['ProdId', 'Name'])

schema = StructType([StructField('ProdId', StringType(), False),
                     StructField('Rating', DoubleType(), False)])

rddAvgRating = spark.read.load(path=INPUT_AVG_RATING_FILE, format="csv", schema=schema, header="false",
                               inferSchema="false", sep=",", nullValue="null", mode="DROPMALFORMED")

joinDf = rddReviewMetaDf.join(rddAvgRating, ['ProdId'], how='full')
joinDf.na.drop().coalesce(1).write.format('csv').option('header', 'false').save(OUTPUT_DIR)


from __future__ import annotations

from romkit.resources.actions.base import BaseAction

import lxml.etree
import re
import tempfile
from pathlib import Path

class ArchiveToDat(BaseAction):
    name = 'archive_to_dat'

    # Converts an archive file listing to a dat file readable by romkit
    def install(self, source: ResourcePath, target: ResourcePath, **kwargs) -> None:
        doc = lxml.etree.iterparse(str(source.path), tag=('file'))
        pattern = re.compile(self.config.get('match', '.*'))

        with tempfile.TemporaryDirectory() as tmpdir:
            # Write initially to a temporary file in case there's a failure part-way through
            tmp_target = Path(tmpdir).joinpath('out.xml')

            with lxml.etree.xmlfile(str(tmp_target), encoding='utf-8') as file:
                file.write_declaration(standalone=True)
                file.write_doctype('<!DOCTYPE datafile PUBLIC "-//Logiqx//DTD ROM Management Datafile//EN" "http://www.logiqx.com/Dats/datafile.dtd">')

                with file.element('datafile'):
                    for event, archive_file in doc:
                        # Get actual name as it'll be downloaded from the source
                        filename = archive_file.get('name')

                        if pattern.search(filename):
                            name = Path(filename).stem

                            # Build element in target file
                            element = lxml.etree.Element('game', name=name)

                            # Add description
                            description = lxml.etree.Element('description')
                            description.text = name
                            element.append(description)

                            # Add rom
                            rom = lxml.etree.Element('rom')
                            rom.attrib['name'] = filename

                            size_tag = archive_file.find('size')
                            if size_tag is not None:
                                rom.attrib['size'] = size_tag.text

                            md5_tag = archive_file.find('md5')
                            if size_tag is not None:
                                rom.attrib['md5'] = md5_tag.text

                            crc32_tag = archive_file.find('crc32')
                            if crc32_tag is not None:
                                rom.attrib['crc'] = crc32_tag.text

                            sha1_tag = archive_file.find('sha1')
                            if sha1_tag is not None:
                                rom.attrib['sha1'] = sha1_tag.text

                            element.append(rom)

                            file.write(element, pretty_print=True)
                            element = None

                        # Release memory
                        archive_file.clear()

            tmp_target.rename(target.path)


import math

import torch
from torch import nn
import torch.nn.functional as F

class RotatedMultiboxLoss(nn.Module):
    def __init__(self, neg_pos_ratio: int):
        super(RotatedMultiboxLoss, self).__init__()
        self._neg_pos_ratio = neg_pos_ratio

    def forward(self, confidence, predicted_locations, labels, gt_locations):
        num_classes = confidence.size(2)
        with torch.no_grad():
            # derived from cross_entropy=sum(log(p))
            loss = -F.log_softmax(confidence, dim=2)[:, :, 0]
            mask = hard_negative_mining(loss, labels, self._neg_pos_ratio)

        confidence = confidence[mask, :]
        classification_loss = F.cross_entropy(confidence.reshape(-1, num_classes), labels[mask], size_average=False)
        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].reshape(-1, 5)
        gt_locations = gt_locations[pos_mask, :].reshape(-1, 5)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations, gt_locations, size_average=False)
        num_pos = gt_locations.size(0)
        return smooth_l1_loss / num_pos, classification_loss / num_pos

def hard_negative_mining(loss, labels, neg_pos_ratio):
    pos_mask = labels > 0
    num_pos = pos_mask.long().sum(dim=1, keepdim=True)
    num_neg = num_pos * neg_pos_ratio

    loss[pos_mask] = -math.inf
    _, indexes = loss.sort(dim=1, descending=True)
    _, orders = indexes.sort(dim=1)
    neg_mask = orders < num_neg
    return pos_mask | neg_mask


import tensorflow as tf
from tensorflow.contrib.layers import xavier_initializer
from typing import List, Union, Optional

def rnn_cell(cell: str,
             hidden: int,
             keep_prob: Union[float, tf.Tensor, tf.Variable],
             output_drop: bool,
             state_drop: bool):
    if cell == 'LSTM':
        cell = tf.nn.rnn_cell.LSTMCell(hidden, state_is_tuple=True)
    elif cell == 'GRU':
        cell = tf.nn.rnn_cell.GRUCell(hidden)
    elif cell == 'RNN':
        cell = tf.nn.rnn_cell.RNNCell(hidden)
    else:
        raise TypeError('cell name error')

    if output_drop and state_drop:
        cell = tf.nn.rnn_cell.DropoutWrapper(cell,
                                             output_keep_prob=keep_prob,
                                             state_keep_prob=keep_prob,
                                             variational_recurrent=True,
                                             dtype=tf.float32)
    elif output_drop:
        cell = tf.nn.rnn_cell.DropoutWrapper(cell,
                                             output_keep_prob=keep_prob,
                                             dtype=tf.float32)
    elif state_drop:
        cell = tf.nn.rnn_cell.DropoutWrapper(cell,
                                             state_keep_prob=keep_prob,
                                             variational_recurrent=True,
                                             dtype=tf.float32)
    else:
        pass
    return cell

def rnn_cells(cell_type: str,
              hidden_list: List[int],
              keep_prob: Union[float, tf.Tensor, tf.Variable],
              output_drop=True,
              state_drop=True):
    multi_cell = []
    for hidden in hidden_list:
        cell = rnn_cell(cell_type, hidden, keep_prob, output_drop, state_drop)
        multi_cell.append(cell)
    return multi_cell

def get_tf_config():
    config = tf.ConfigProto(allow_soft_placement=True)
    # config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    return config

def clip_gradients(grads_and_vars, clip_norm=5.0):
    """
    :param grads_and_vars: A list of (gradients and variables) pairs
    :param clip_norm: clip norm
    :return: A list of (clipped gradients and variables) pairs
    """
    # gvs = [('g1', 'v1'), ('g2', 'v2'), ('g3', 'v3')]
    # zip(*gvs)
    # gradients = ('g1', 'g2', 'g3')
    # variables = ('v1', 'v2', 'v3')
    gradients, variables = zip(*grads_and_vars)
    clipped_gradients, _ = tf.clip_by_global_norm(gradients, clip_norm)
    return list(zip(clipped_gradients, variables))

def merge_gradients(towers_gvs):
    """
    Merge every grads_and_vars of each tower
    :param towers_gvs: [each tower's grads and vars]
    :return: merged grads_and_vars
    """
    average_grads = []
    for grad_and_vars in zip(*towers_gvs):
        # iteration through all grads and vars
        grads = []
        for g, _ in grad_and_vars:
            # iteration through all towers
            ex_g = tf.expand_dims(g, 0)
            grads.append(ex_g)
        grad = tf.concat(grads, axis=0)
        grad = tf.reduce_mean(grad, 0)

        # variables of first tower
        v = grad_and_vars[0][1]
        grad_and_vars = (grad, v)
        average_grads.append(grad_and_vars)
    return average_grads

def entry_stop_gradients(embedding: tf.Variable, mask):
    mask_n = tf.logical_not(mask)

    mask = tf.cast(mask, dtype=embedding.dtype)
    mask_n = tf.cast(mask_n, dtype=embedding.dtype)

    return tf.stop_gradient(mask_n * embedding) + mask + embedding

class OutputLayer(tf.layers.Layer):
    def __init__(self, num_inputs, embedding_matrix):
        super(OutputLayer, self).__init__()
        self.num_inputs = num_inputs
        self.embedding_matrix = embedding_matrix
        self.shape = embedding_matrix.get_shape()       # (vocab_size, embedding_dim)

    def build(self, _):
        embedding_size = self.embedding_matrix.get_shape()[1]
        self.kernel = self.add_variable("kernel", [self.num_inputs, embedding_size])

    def call(self, inputs, **kwargs):
        # inputs
        # if Greedy, [batch, hidden]
        # if Beam, [batch, beam, hidden]

        # layer hidden
        # if Greedy, [batch, kernel second dim]
        # if Beam, [batch, beam, kernel second dim]
        hidden = tf.tensordot(inputs, self.kernel, axes=((-1,), (0,)))  # Remove 'a' last dim, 'b' first dim

        # layer hidden
        # if Greedy, [batch, vocab size]
        # if Beam, [batch, beam, vocab size]
        return tf.tensordot(hidden, tf.transpose(self.embedding_matrix), axes=((-1,), (0,)))

    def compute_output_shape(self, input_shape):
        input_shape = input_shape.with_rank_at_least(2)
        if input_shape[-1].value is None:
            raise ValueError(
              'The innermost dimension of input_shape must be defined, but saw: %s'
              % input_shape)
        return input_shape[:-1].concatenate(self.shape[0])


"""
Given two strings s and t, determine if they are isomorphic.

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters.
No two characters may map to the same character, but a character may map to itself.
"""

def is_isomorphic(s: str, t: str) -> bool:
    map_s = []
    map_t = []
    s_dict = {}
    t_dict = {}
    if len(s) != len(t):
        return False
    for ch_s, ch_t in zip(s, t):
        if ch_s not in s_dict:
            s_dict[ch_s] = len(s_dict) + 1
        if ch_t not in t_dict:
            t_dict[ch_t] = len(t_dict) + 1
        map_s.append(s_dict[ch_s])
        map_t.append(t_dict[ch_t])
    return map_s == map_t

def is_isomorphic_faster(s: str, t: str) -> bool:
    len_zip = len(set(zip(s, t)))
    return len_zip == len(set(s)) == len(set(t))

if __name__ == '__main__':
    test_cases = [
        ("egg", "add", True),
        ("foo", "bar", False),
        ("paper", "title", True),
        ("bbbaaaba", "aaabbbba", False),
        ("bba", "bba", True),
        ("bba", "adc", False)
    ]
    for test_case in test_cases:
        expected = test_case[2]
        actual = is_isomorphic_faster(test_case[0], test_case[1])
        print(expected == actual)


from revChatGPT.V1 import Chatbot
import json
import os

from .text import ArticleText

class TextBackendChatGPT:
    def __init__(self) -> None:
        self.CACHE_DIR = os.path.expanduser("~/.cache/chatgpt-blog")
        self.TOPICS_CACHE_FILE = os.path.join(self.CACHE_DIR, "topics.json")
        self.bot = Chatbot(self.read_json_file(os.path.expanduser("~/.config/revChatGPT/config.json")))
        self.CONVERSATION_ID = self.read_json_file(os.path.expanduser("~/.config/revChatGPT/config.json"))["conversation_id"]
        self.PROMPT_TEMPLATES = {
            "ideas": "What are five unique article ideas for a tech blog? Only give the titles.",
            "image_idea": "What could an image lokk like that visualizes an article with title '{title}'? Give a short, one-paragraph description, which is still very detailed.",
            "image_caption": "Condense the following text down to five words: '{desc}'",
            "article": "Write an article for a tech blog with following title: '{title}'; continuous, but structured text; introduction and conclusion; avoid listings and headings"
        }

    def ask(self, prompt) -> str:
        try:
            for data in self.bot.ask(prompt, conversation_id=self.CONVERSATION_ID):
                resp = data["message"]
        except Exception:
            print("failed to ask ChatGPT. Do you have a valid session (press enter to proceed)?")
            input()
            return self.ask(prompt)
        return resp

    def read_json_file(self, path: str) -> any:
        with open(path) as f:
            return json.load(f)

    def read_json_list_file(self, path: str) -> any:
        return [i.strip() for i in self.read_json_file(path) if i.strip() != ""]

    def write_json_list_file(self, path: str, data: list) -> any:
        json.dump(data, open(path, "w"))

    def get_topic(self) -> str:
        if not os.path.isfile(self.TOPICS_CACHE_FILE) or len(self.read_json_list_file(self.TOPICS_CACHE_FILE)) < 1:
            resp = self.ask(self.PROMPT_TEMPLATES["ideas"].format())
            topics = [i[2:] for i in resp.split("\n") if not i.startswith("Sure, ")]
            self.write_json_list_file(self.TOPICS_CACHE_FILE, topics)
        topics = self.read_json_list_file(self.TOPICS_CACHE_FILE)
        self.write_json_list_file(self.TOPICS_CACHE_FILE, topics[1:])
        return topics[0].strip("\"").strip("'")

    def get_body(self, title: str) -> str:
        text = self.ask(self.PROMPT_TEMPLATES["article"].format(title=title))
        text = "\n".join([l for l in text.split("\n") if l.strip() not in ("Conclusion:", "Introduction:", "Body:")])
        return ArticleText(self.ask(self.PROMPT_TEMPLATES["article"].format(title=title)), title, "ChatGPT")

    def get_image_idea(self, title: str) -> str:
        resp = self.ask(self.PROMPT_TEMPLATES["image_idea"].format(title=title))
        joined = ""
        for line in resp.split("\n"):
            if line.startswith("As an AI language model"):
                continue
            joined += line
        return joined.strip()

    def get_caption(self, desc: str) -> str:
        return self.ask(self.PROMPT_TEMPLATES["image_caption"].format(desc=desc))


from MFramework import (
    Bot,
    ChannelID,
    Context,
    Groups,
    Message_Reaction_Add,
    Message_Reaction_Remove,
    RoleID,
    Snowflake,
    onDispatch,
    register,
)
from MFramework.database.alchemy import models, types
from mlib.utils import replace_multiple

from . import role

@register(group=Groups.MODERATOR, main=role)
async def reaction():
    """Manages Reaction Roles"""
    pass

@register(group=Groups.MODERATOR, main=reaction, aliases=["rra"], private_response=True)
async def create(
    ctx: Context,
    emoji: str,
    role: RoleID,
    group: str = None,
    channel: ChannelID = None,
    message_id: Snowflake = None,
):
    """
    Adds new reaction role
    Params
    ------
    emoji:
        Emoji to use as a reaction
    role:
        Role that should be given for reacting
    group:
        Whether this RR should belong to a group
    channel:
        Channel in which RR should be created. Empty means current channel
    message_id:
        Message ID under which RR should be created. Empty means last message in channel
    """
    reaction = f"{emoji}:0" if ":" not in emoji else replace_multiple(emoji, ["<:", ">"], "")

    if group not in ctx.cache.reaction_roles:
        ctx.cache.reaction_roles[group] = {message_id: {reaction: [role]}}
    else:
        if message_id not in ctx.cache.reaction_roles[group]:
            ctx.cache.reaction_roles[group][message_id] = {reaction: [role]}
        else:
            ctx.cache.reaction_roles[group][message_id][reaction] = [role]

    s = ctx.db.sql.session()

    r = models.Role.fetch_or_add(s, server_id=ctx.guild_id, id=role)
    r.add_setting(types.Setting.ChannelID, channel)
    r.add_setting(types.Setting.MessageID, message_id)
    r.add_setting(types.Setting.Reaction, reaction)
    if group:
        r.add_setting(types.Setting.Group, group)
    s.commit()

    await ctx.bot.create_reaction(channel, message_id, replace_multiple(emoji, ["<:", ">"], ""))
    return f"Successfully created reaction {emoji} for role <@&{role}>"

# @register(group=Groups.MODERATOR, main=reaction, aliases=['rre'], private_response=True)
async def edit(
    ctx: Context,
    emoji: str,
    role: RoleID,
    group: str = None,
    channel: ChannelID = None,
    message_id: Snowflake = None,
):
    """
    Edits existing reaction role
    Params
    ------
    emoji:
        Emoji to use as a reaction
    role:
        Role that should be given for reacting
    group:
        Whether this RR should belong to a group
    channel:
        Channel in which RR should be edited. Empty means current channel
    message_id:
        Message ID under which RR should be edited. Empty means last message in channel
    """
    # FIXME!
    await ctx.bot.delete_own_reaction(channel, message_id, emoji)
    await ctx.bot.create_reaction(channel, message_id, emoji)
    return "Successfully modified reaction role"

@register(group=Groups.MODERATOR, main=reaction, aliases=["rrd"], private_response=True)
async def remove(
    ctx: Context,
    emoji: str = None,
    role: RoleID = None,
    group: str = None,
    channel: ChannelID = None,
    message_id: Snowflake = None,
):
    """
    Removes existing reaction role
    Params
    ------
    emoji:
        Emoji to remove from being reaction emoji
    role:
        Role to remove from being reaction role
    group:
        Whether this RR should be removed from belonging to a group
    channel:
        Channel from which RR should be removed. Empty means current channel
    message_id:
        Message ID under which RR should be removed. Empty means last message in channel
    """
    s = ctx.db.sql.session()

    if role:
        r = models.Role.filter(s, server_id=ctx.guild_id, id=role).first()
    elif emoji:
        r = (
            models.Role.with_setting(types.Setting.Reaction, replace_multiple(emoji, ["<:", ">"], ""))
            .filter(server_id=ctx.guild_id)
            .first()
        )
    else:
        return "Either Emoji or Role has to be specified"

    if group:
        r.remove_setting(types.Setting.Group)
        s.commit()
        return

    emoji = r.remove_setting(types.Setting.Reaction)
    channel = r.remove_setting(types.Setting.ChannelID)
    message_id = r.remove_setting(types.Setting.MessageID)
    group = r.remove_setting(types.Setting.Group)
    s.commit()

    ctx.cache.reaction_roles.get(group, {}).get(message_id, {}).pop(emoji, None)
    if ctx.cache.reaction_roles.get(group, {}).get(message_id, None) == {}:
        ctx.cache.reaction_roles.get(group, {}).pop(message_id, None)
        if ctx.cache.reaction_roles.get(group, None) == {}:
            ctx.cache.reaction_roles.pop(group)

    await ctx.bot.delete_own_reaction(channel, message_id, emoji)
    return f"Successfully deleted <@&{role}> from being a {emoji if ':0' in emoji else '<:'+emoji+'>'} reaction role"

@onDispatch(predicate=lambda x: x.guild_id and not x.member.user.bot)
async def message_reaction_add(self: Bot, data: Message_Reaction_Add):
    roles = self.cache[data.guild_id].reaction_roles
    r = []

    for group in roles:
        for msg in roles[group]:
            if data.message_id == msg:
                r = roles[group][data.message_id][f"{data.emoji.name}:{data.emoji.id or 0}"]
                if group is None:
                    continue
                elif all(i in data.member.roles for i in r):
                    return
                elif any(
                    i in data.member.roles for i in [j for e in roles[group][data.message_id].values() for j in e]
                ):
                    return await self.delete_user_reaction(
                        data.channel_id,
                        data.message_id,
                        f"{data.emoji.name}:{data.emoji.id}" if data.emoji.id else data.emoji.name,
                        data.user_id,
                    )

    for i in r:
        await self.add_guild_member_role(data.guild_id, data.user_id, i, "Reaction Role")

@onDispatch(predicate=lambda x: x.guild_id)
async def message_reaction_remove(self: Bot, data: Message_Reaction_Remove):
    roles = self.cache[data.guild_id].reaction_roles
    r = []

    for group in roles:
        if data.message_id in roles[group]:
            role = roles[group][data.message_id][f"{data.emoji.name}:{data.emoji.id or 0}"]
            if role == None:
                return
            break

    for i in r:
        await self.remove_guild_member_role(data.guild_id, data.user_id, i, "Reaction Role")


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

import itertools
import numbers

import numpy

import dask.array

from dask_ndfourier import _compat

try:
    from itertools import imap
except ImportError:
    imap = map

try:
    irange = xrange
except NameError:
    irange = range

def _get_freq_grid(shape, chunks, dtype=float):
    assert len(shape) == len(chunks)

    shape = tuple(shape)
    dtype = numpy.dtype(dtype).type

    assert (issubclass(dtype, numbers.Real) and
            not issubclass(dtype, numbers.Integral))

    ndim = len(shape)

    freq_grid = []
    for i in irange(ndim):
        sl = ndim * [None]
        sl[i] = slice(None)
        sl = tuple(sl)

        freq_grid_i = _compat._fftfreq(shape[i],
                                       chunks=chunks[i]).astype(dtype)[sl]
        for j in itertools.chain(range(i), range(i + 1, ndim)):
            freq_grid_i = freq_grid_i.repeat(shape[j], axis=j)

        freq_grid.append(freq_grid_i)

    freq_grid = dask.array.stack(freq_grid)

    return freq_grid

def _get_ang_freq_grid(shape, chunks, dtype=float):
    dtype = numpy.dtype(dtype).type

    assert (issubclass(dtype, numbers.Real) and
            not issubclass(dtype, numbers.Integral))

    pi = dtype(numpy.pi)

    freq_grid = _get_freq_grid(shape, chunks, dtype=dtype)
    ang_freq_grid = 2 * pi * freq_grid

    return ang_freq_grid

def _norm_args(a, s, n=-1, axis=-1):
    if issubclass(a.dtype.type, numbers.Integral):
        a = a.astype(float)

    if isinstance(s, numbers.Number):
        s = numpy.array(a.ndim * [s])
    elif not isinstance(s, dask.array.Array):
        s = numpy.array(s)

    if issubclass(s.dtype.type, numbers.Integral):
        s = s.astype(a.real.dtype)
    elif not issubclass(s.dtype.type, numbers.Real):
        raise TypeError("The `s` must contain real value(s).")
    if s.shape != (a.ndim,):
        raise RuntimeError(
            "Shape of `s` must be 1-D and equal to the input's rank."
        )

    if n != -1:
        raise NotImplementedError(
            "Currently `n` other than -1 is unsupported."
        )

    return (a, s, n, axis)


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

#%% 1
amazon = {'date': ['2013-02-08', '2013-02-11', '2013-02-12', '2013-02-13', '2013-02-14'],
          'close': [261.950, 257.210, 258.700, 269.470, 269.240]}

amazon = pd.DataFrame(amazon)

sns.lineplot(
    x='date',
    y='close',
    data=amazon)

plt.xticks(rotation=45)
plt.show()

#%% 2
employee = {'employee_id': ['1ex5', '73fd', 'ei10', 'b45e'],
            'first_name': ['Linda', 'Steve', 'Henry', 'Sara'],
            'gender': ['female', 'male', 'male', 'female'],
            'salary': [3400, 5000, 12400, 7600]}

employee = pd.DataFrame(employee)

private_employee = employee[['employee_id', 'salary']]

print(private_employee)

#%% 3

from scipy import stats
IQR = stats.iqr(pH)
print(IQR)

#%% 4

Q1 = df['age'].quantile(0.25)
Q3 = df['age'].quantile(0.75)

print(Q3 - Q1)

#%% 5

df = {'gh owner': ['pandas-dev', 'tidyverse', 'tidyverse', 'has2k1'],
      'language': ['python', 'R', 'R', 'python'],
      'repo': ['pandas', 'dplyr', 'ggplot2', 'plotnine'],
      'stars': [17800, 2800, 3500, 1450]}

df = pd.DataFrame(df)

print(df.stars.apply(lambda x: x / 1000))
print(df.stars.transform(lambda x: x / 1000))

#%% 6

subset_wine = wine[['country', 'price']]
print(subset_wine.head())

#%% 7

x = np.array([2.2, 0.9, 4.4, 6.7, 2.8, 3.2, 1.1, 3.5])
x_var = np.var(x)
x_stdev = np.std(x)
print('Variance: {:4.2f}'.format(x_var))
print('Std Deviation: {:4.2f}'.format(x_stdev))

#%% 8

chess = chess.set_index('Fide id')

print(chess.head())

#%% 9

df = {'day': [0, 1, 2, 3, 4, 5],
      'order': [10, 11, 14, 7, 5, 16]}

df = pd.DataFrame(df)

sns.lineplot(data=df, x='day', y='order')
plt.xticks(rotation=45)
plt.show()

#%% 10

df = {'alignment': ['Good', 'Good', 'Evil', 'Evil'],
      'character': ['Batman', 'Wonder Woman', 'Lex Luthor', 'The Joker'],
      'height': [1.88, 1.78, 1.88, 1.85],
      'location': ['Gotham', 'Themyscira', 'Metropolis', 'Gotham']}

df = pd.DataFrame(df)

print(df[['alignment', 'character']])

#%% 11

employee = {'first_name': ['Linda', 'Steve', 'Henry', 'Sara'],
            'gender': ['female', 'male', 'male', 'female'],
            'salary_usd': [3000, 5000, 12000, 5000]}

employee = pd.DataFrame(employee)

print(employee[employee['salary_usd'] == 5000])

#%% 12

df = {'index': ['zero', 'one', 'two', 'tree'],
      'Month': ['Jan', 'Apr', 'Mar', 'Feb'],
      'Count': [52, 29, 46, 3]}

df = pd.DataFrame(df).set_index('index')

print(df.loc['two':])

#%% 13

# --iris
# species    measurement   value
# 0     setosa     sepal_length  5.1
# 1     setosa     sepal_length  4.9
# ...
# 598   virginica  petal_width   4.6
# 599   virginica  petal_width   5.0

ax = sns.swarmplot(x="measurement",
                   y="value",
                   hue="species",
                   data=iris)
plt.show()

#%% 14

ax = sns.lineplot(x='Overall rank', y='Score', data=happiness)

plt.show()

#%% 15

# age emissions   mpg  value
# 0    15       low  23.1   3047
# 1     8       low  10.4  15428
# 2     4      high  15.4  24973
# 3    10      high  31.0   4638
# 4     1       low  30.2  19303

# import matplotlib.pyplot as plt
# import seaborn as sns

sns.scatterplot(x = "age", y = "value", hue = "emissions", data = valuation)

plt.show()


def solution(input_list):
    input_dict = {}
    for data in input_list:
        try:
            input_dict[data] += 1
        except:
            input_dict[data] = 1

    result = []
    for item in input_dict:
        if input_dict[item] > 1:
            result.append(item)

    return result

Input_list = [5, 4, 3, 2, 1, 1, 0, 0, 1, 2, 4]
print(f'Input: {Input_list}')

result = solution(Input_list)
print(f'Output: {result}')


from tkinter import *
from currency_converter import CurrencyConverter
a=CurrencyConverter()
window=Tk()
window.title("Currency Convertor")
window.geometry("500x380")
window.config(bg="#FF8DC7")

def clicked():
    amount=int(e1.get())
    cur1=e2.get()
    cur2=e3.get()
    data=a.convert(amount,cur1,cur2)
    lab4=Label(window,text=data,font=("Ink Free",20,"bold"),fg="#FDFF00",bg="#FF8DC7").place(x=90,y=300)

lab=Label(window, text="ðŸ’±Currency ConvertorðŸ’±",font=("Ink Free",30,"bold"),fg="#8D72E1",bg="#FF8DC7").place(x=30,y=30)
lab1=Label(window,text="Enter the amount:",font=("Ink Free",20,"bold"),fg="#3F3B6C",bg="#FF8DC7").place(x=20,y=100)
e1=Entry(window,width=20,border=0)
lab2=Label(window,text="Enter currency:",font=("Ink Free",20,"bold"),fg="#3F3B6C",bg="#FF8DC7").place(x=20,y=140)
e2=Entry(window,width=20,border=0)
lab3=Label(window,text="Enter req Currency:",font=("Ink Free",20,"bold"),fg="#3F3B6C",bg="#FF8DC7").place(x=20,y=180)
e3=Entry(window,width=20,border=0)
btn=Button(window,text="convert",bg="#FF8DC7",command=clicked,font=("Ink Free",20,"bold"),fg="#3F3B6C").place(x=200,y=240)
e1.place(x=300,y=110)
e2.place(x=300,y=150)
e3.place(x=300,y=190)
window.mainloop()
#a=CurrencyConverter()
#print(a.convert(12,"USD","INR"))


import pprint
import hashlib
import hmac
import time
import urllib
import json
from datetime import datetime
from exchange_api import ExchangeApi

#https://coincheck.com/ja/documents/exchange/api

class CoincheckAPIWrapper(ExchangeApi):

    url_base = 'https://coincheck.com'

    #----------------------------------------
    # コンストラクタ
    #----------------------------------------
    def __init__(self, api_key, api_secret, market_code):
        super().__init__(self.url_base, api_key, api_secret)
        self.__market_code = market_code
        return

    #----------------------------------------
    # Hashを計算
    #----------------------------------------
    def __generate_hash(self, timestamp, method, path, params, body):
        if params is not None:
            path = path + '?' + urllib.parse.urlencode(params)
        json_body = ''
        if body is not None:
            json_body = json.dumps(body)
        ba_text = bytearray(timestamp + self.url_base + path + json_body, 'ASCII')
        ba_secret = bytearray(self.api_secret, 'ASCII')
        hash = hmac.new(ba_secret, ba_text, hashlib.sha256).hexdigest()
        return hash

    #----------------------------------------
    # Private API用のヘッダを生成
    #----------------------------------------
    def __generate_header_for_private_api(self, method, path, params, body):
        timestamp = '{0}000'.format(int(time.mktime(datetime.now().timetuple())))
        hash = self.__generate_hash(timestamp, method, path, params, body)
        return { 'ACCESS-KEY': self.api_key,
                 'ACCESS-NONCE': timestamp,
                 'ACCESS-SIGNATURE': hash
               }

    #----------------------------------------
    # Private APIを実行(GET)
    #----------------------------------------
    def __submit_private_api_get(self, path, params):
        headers = self.__generate_header_for_private_api('GET', path, params, None)
        pprint.pprint(headers)
        return self.http.get(path, params, headers)

    #----------------------------------------
    # Private APIを実行(POST)
    #----------------------------------------
    def __submit_private_api_post(self, path, params, body):
        headers = self.__generate_header_for_private_api('POST', path, params, body)
        return self.http.post(path, headers, body)

    #----------------------------------------
    # Tickerを取得
    #----------------------------------------
    def get_ticker(self):
        resp = self.http.get('/api/ticker', None, None)
        last = float(resp['last'])
        resp = self.http.get('/api/order_books', None, None)
        bid = float(resp['bids'][0][0])
        ask = float(resp['asks'][0][0])
        return last, bid, ask

    #----------------------------------------
    # 板情報を取得
    #----------------------------------------
    def get_order_books(self):
        resp = self.http.get('/api/order_books', None, None)
        pprint.pprint(resp['bids'][0][0])
        return

    #----------------------------------------
    # 新規注文を出す
    #----------------------------------------
    def send_childorders(self, order_type, side, price, size):
        if order_type == 'MARKET' and side == 'BUY':
            ortype = 'market_buy'
        elif order_type == 'MARKET' and side == 'SELL':
            ortype = 'market_sell'
        elif order_type == 'LIMIT' and side == 'BUY':
            ortype = 'buy'
        elif order_type == 'LIMIT' and side == 'SELL':
            ortype = 'sell'
        else:
            ortype = ''

        body = {
            'id': 12345,
            'rate': str(price),
            'amount': str(size),
            'order_type': ortype,
            'stop_loss_rate': 'null',
            'pair': self.__market_code,
            'executionTYpe': order_type,

        }
        resp = self.__submit_private_api_post('/api/exchange/orders', None, body)
        pprint.pprint(resp)

    #----------------------------------------
    # 資産残高を取得
    #----------------------------------------
    def get_balance(self):
        resp = self.__submit_private_api_get('/api/accounts/balance', None)
        pprint.pprint(resp)
        return


from collections import defaultdict
from odoo import api, fields, models

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    date_planned = fields.Datetime('Planned Date')
    requested_date = fields.Datetime('Requested Date')

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    # In 13, this field exists, but isn't stored and is computed during
    # computation for available inventory (set to order's warehouse)
    warehouse_id = fields.Many2one('stock.warehouse', string='Warehouse',
                                   compute=None, store=True)
    date_planned = fields.Datetime('Planned Date')

    def _prepare_procurement_values(self, group_id=False):
        vals = super(SaleOrderLine, self)._prepare_procurement_values(group_id=group_id)
        if self.warehouse_id:
            vals.update({'warehouse_id': self.warehouse_id})
        if self.date_planned:
            vals.update({'date_planned': self.date_planned})
        elif self.order_id.date_planned:
            vals.update({'date_planned': self.order_id.date_planned})
        return vals

    # Needs modifications to not actually set a warehouse on the line as it is now stored
    @api.depends('product_id', 'customer_lead', 'product_uom_qty', 'product_uom', 'order_id.warehouse_id', 'order_id.commitment_date')
    def _compute_qty_at_date(self):
        """ Compute the quantity forecasted of product at delivery date. There are
        two cases:
         1. The quotation has a commitment_date, we take it as delivery date
         2. The quotation hasn't commitment_date, we compute the estimated delivery
            date based on lead time"""
        qty_processed_per_product = defaultdict(lambda: 0)
        grouped_lines = defaultdict(lambda: self.env['sale.order.line'])
        # We first loop over the SO lines to group them by warehouse and schedule
        # date in order to batch the read of the quantities computed field.
        for line in self:
            if not (line.product_id and line.display_qty_widget):
                continue
            # use warehouse from line or order
            warehouse = line.warehouse_id or line.order_id.warehouse_id
            # line.warehouse_id = line.order_id.warehouse_id
            if line.order_id.commitment_date:
                date = line.order_id.commitment_date
            else:
                date = line._expected_date()
            grouped_lines[(warehouse.id, date)] |= line

        treated = self.browse()
        for (warehouse, scheduled_date), lines in grouped_lines.items():
            product_qties = lines.mapped('product_id').with_context(to_date=scheduled_date, warehouse=warehouse).read([
                'qty_available',
                'free_qty',
                'virtual_available',
            ])
            qties_per_product = {
                product['id']: (product['qty_available'], product['free_qty'], product['virtual_available'])
                for product in product_qties
            }
            for line in lines:
                line.scheduled_date = scheduled_date
                qty_available_today, free_qty_today, virtual_available_at_date = qties_per_product[line.product_id.id]
                line.qty_available_today = qty_available_today - qty_processed_per_product[line.product_id.id]
                line.free_qty_today = free_qty_today - qty_processed_per_product[line.product_id.id]
                line.virtual_available_at_date = virtual_available_at_date - qty_processed_per_product[line.product_id.id]
                if line.product_uom and line.product_id.uom_id and line.product_uom != line.product_id.uom_id:
                    line.qty_available_today = line.product_id.uom_id._compute_quantity(line.qty_available_today, line.product_uom)
                    line.free_qty_today = line.product_id.uom_id._compute_quantity(line.free_qty_today, line.product_uom)
                    line.virtual_available_at_date = line.product_id.uom_id._compute_quantity(line.virtual_available_at_date, line.product_uom)
                qty_processed_per_product[line.product_id.id] += line.product_uom_qty
            treated |= lines
        remaining = (self - treated)
        remaining.virtual_available_at_date = False
        remaining.scheduled_date = False
        remaining.free_qty_today = False
        remaining.qty_available_today = False
        # don't unset warehouse as it may be set by hand
        # remaining.warehouse_id = False


from framework.evaluator import Evaluator
from classifier_data import ClassifierData
import framework.utils.common as utils
from framework.trainer import Trainer
import framework.subgraph.losses as losses
import logging
import sys
import os
from framework.utils.data.text_indexer import TextIndexer
import tensorflow as tf
import numpy as np
from time import time

e = Evaluator.load2("outputv0.ckpt")
i = TextIndexer.from_file('vocab.pkl')
e.dump_variable_sizes()

params = { 'num_words_before': 5,
					 'num_words_after': 5,
					 'embedding_size': 300,
					 'vocab_size': 100000,
					 'embedding_device': None,
					 'batch_size': 128,
					 'num_classes': 2,
					 'mini_batches_between_checkpoint': 100,
					 'monolingual_dir': '/mnt/work/1-billion-word-language-modeling-benchmark'
					 }
test_data = ClassifierData.get_monolingual_test(params=params)
batch = test_data.next_batch(batch_size=50000)

model_results = []
f = open('heldout.txt', 'w')
for sentence, ground_truth in zip(batch['sentence'], batch['y']):
	_, indexed, _, _ = i.index_wordlist(sentence)
	before_time = time()
	r = e.eval({'sentence': [indexed]}, {'sm_decision'})
	after_time = time()
	model_score = r[0][0][1]
	model_results.append([indexed, model_score, ground_truth])
	f.write('%0.8f\t%0.8f\t%s\t%s\n'%(after_time-before_time,model_score,ground_truth,sentence))
f.close()

for thres in np.linspace(0,1,50):
	tp = 0
	fp = 0
	tn = 0
	fn = 0
	#	print('%s - %s'%(model_score,ground_truth))
	for indexed, model_score, ground_truth in model_results:
		if ground_truth < .5:
			if model_score > thres:
				fp += 1
			else:
				tn += 1
		else:
			if model_score > thres:
				tp += 1
			else:
				fn += 1
#	print([tp,fp,tn,fn])
	sp = tp + fp
	if sp == 0:
		sp = 1
	nr = tp + fn
	if nr == 0:
		nr = 1
	precision = tp / sp
	recall = tp / nr
#	print("precision: %s"%precision)
#	print("recall: %s"%recall)
	print('%s,%s,%s'%(thres,precision,recall))


import unittest
from init_attmpt import load_samples_df
import numpy as np
import os
import cv2

sample_folder = './sample behavioral cloning data/'

def generator(samples, batch_size=32):
    num_samples = len(samples)
    dummy_seed=1
    while dummy_seed==1:  # Loop forever so the generator never terminates
        dummy_seed-=1
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples.iloc[offset:min(offset + batch_size,num_samples), 0]

            images = []
            for batch_sample in batch_samples:
                name = os.path.join(sample_folder, batch_sample)
                center_image = cv2.imread(name)
                images.append(center_image)

            # trim image to only see section with road
            X_train = np.array(images)
            y_train=samples.iloc[offset:min(offset + batch_size,num_samples), 3]

            yield X_train,y_train

class MyTestCase(unittest.TestCase):
    def test_generator(self):
        train_samples, validation_samples = load_samples_df(test_size=0.9)

        accum_sample_num=0
        g=generator(train_samples, batch_size=24)
        while accum_sample_num<train_samples.shape[0]:
            X,y=next(g)
            accum_sample_num+=X.shape[0]

        self.assertEqual(train_samples.shape[0], accum_sample_num)

if __name__ == '__main__':
    unittest.main()


def strStr(haystack: str, needle: str) -> int:
    if needle in haystack:
        return haystack.index(needle)
    else:
        return -1

def main():
    testcases = [("mississippi", "issipi"), ("salbutsad", "sad"), ("leetcode", "leeto"), ("appy", "appy")]
    for case in testcases:
        print(f"{case} -> {strStr(case[0], case[1])}")

main()

Easy/28. Find the Index of the First Occurrence in a String.py


import streamlit as st
from datetime import datetime, timedelta
from src.pages.utils.load_data import load_data
from src.pages.utils.fetch_url import fetch_url
from PIL import Image

def main():
    img = Image.open("assets/covid-19-4960246_640.jpg")
    st.image(img)
    st.title("Data")
    date = datetime.today()
    df = None
    load_state = st.text('Loading data......')
    while True:
        try:
            df = load_data(fetch_url(date))
        except Exception as e:
            date = date - timedelta(days=1)
            continue
        break
    load_state.text("Loading data......done!")

    if st.checkbox("Show raw data"):
        st.subheader('Raw data')
        st.write(df)
    st.subheader("The numbers so far")
    st.markdown("* There are **{}** countries that have been affected by the COVID-19 virus.".format(len(df["Country_Region"].unique())))
    st.markdown("* The virus has affected **{:.2f}M** people and caused the death of **{:.2f}K**.".format(df["Confirmed"].sum()/1000000,
                                                                                df["Deaths"].sum()/1000))
    h_confirmed = df.groupby("Country_Region").agg({"Confirmed": "sum"}).nlargest(1,"Confirmed")
    st.markdown("* **{}** has the largest number of confirmed cases with **{:.2f}M** confirmed cases.".format(h_confirmed.index.values[0],
                                                                                h_confirmed["Confirmed"].values[0]/1000000))
    h_deaths = df.groupby("Country_Region").agg({"Deaths": "sum"}).nlargest(1,"Deaths")
    st.markdown("* **{}** has the largest number of deaths with **{:.2f}K** deaths.".format(h_deaths.index.values[0],
                                                                                h_deaths["Deaths"].values[0]/1000))
    h_recovered = df.groupby("Country_Region").agg({"Recovered": "sum"}).nlargest(1,"Recovered")
    st.markdown("* **{}** has the largest number of recoveries with **{:.2f}M** recovered.".format(h_recovered.index.values[0],
                                                                                h_recovered["Recovered"].values[0]/1000000))


import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score

st.title('Personal Financial Prediction App')

# Load personal financial data
# You can upload a CSV file or manually input your financial data.

st.subheader('Financial Data')
financial_data = st.file_uploader('Upload a CSV file', type=['csv'])

if financial_data is not None:
    df = pd.read_csv(financial_data)
    st.write(df)

    # Predict profit or loss percentages
    st.subheader('Profit or Loss Prediction')
    n_years = st.number_input('Years of Prediction:', 1, 10, 1)
    n_days = n_years * 365
    df['Prediction'] = df['Profit'].shift(-n_days)

    X = np.array(df[['InputFeature1', 'InputFeature2']])  # Adjust features accordingly
    X = X[:-n_days]
    y = np.array(df['Prediction'])
    y = y[:-n_days]

    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

    model = RandomForestRegressor()
    model.fit(x_train, y_train)

    prediction = model.predict(x_test)
    st.subheader('Model Evaluation')
    mse = mean_squared_error(y_test, prediction)
    r2 = r2_score(y_test, prediction)
    st.write(f'Mean Squared Error: {mse}')
    st.write(f'R2 Score: {r2}')

    # Predict profit or loss percentages based on user input
    user_input = st.text_input('Enter Input Feature 1:')
    user_input2 = st.text_input('Enter Input Feature 2:')
    user_prediction = model.predict([[user_input, user_input2]])
    st.subheader('Predicted Profit or Loss Percentage')
    st.write(f'Predicted: {user_prediction[0]}%')


from inference_models.pytorch_inference import PytorchInference
from inference_models.onnx_inference import OnnxInference, OnnxInferenceFp16, OnnxInferenceInt8

class NLP:
    def load_model(self, model_type):
        model_path = model_type.check_model_locally()
        return model_type.start_inference(model_path)

class ModelLoader:
    def load_model(self, serializable, inf_model):
        newmodel = factory.get_model(inf_model)
        serializable.load_model(newmodel)
        return newmodel

class InfModelFactory:
    def get_model(self, inf_model):
        if inf_model == 'pytorch':
            return PytorchInference()
        elif inf_model in 'onnx':
            return OnnxInference()
        elif inf_model == 'onnx_fp16':
            return OnnxInferenceFp16()
        elif inf_model == 'onnx_int8':
            return OnnxInferenceInt8()
        else:
            raise ValueError(inf_model)
factory = InfModelFactory()