from birdnetlib.watcher import DirectoryWatcher
from birdnetlib.analyzer_lite import LiteAnalyzer
from birdnetlib.analyzer import Analyzer
import os
from collections import namedtuple
from mock import patch, Mock

def test_watcher_complete():
    analyzer = Analyzer()
    analyzer_lite = LiteAnalyzer()

    directory = "."
    watcher = DirectoryWatcher(directory, analyzers=[analyzer, analyzer_lite])

    input_path = os.path.join(os.path.dirname(__file__), "test_files/soundscape.wav")

    # Add a mocked call for on_analyze_complete
    watcher.on_analyze_complete = Mock()
    watcher.on_analyze_file_complete = Mock()

    # Create a "file-created" event in the watcher.
    # Test calling private method directly (this would be called by watchdog)
    event = namedtuple("Event", "src_path")
    event.src_path = input_path
    watcher._on_closed(event)

    # Check complete call count and results.
    assert watcher.on_analyze_complete.call_count == 2
    analyzer_recording = watcher.on_analyze_complete.call_args_list[0][0][0]
    lite_recording = watcher.on_analyze_complete.call_args_list[1][0][0]

    assert len(analyzer_recording.detections) == 36
    assert analyzer_recording.analyzer.name == "Analyzer"
    assert len(lite_recording.detections) == 2
    assert lite_recording.analyzer.name == "LiteAnalyzer"

    assert watcher.on_analyze_file_complete.call_count == 1
    assert len(watcher.on_analyze_file_complete.call_args.args[0]) == 2

def test_watcher_error():
    analyzer = LiteAnalyzer()
    directory = "."
    watcher = DirectoryWatcher(directory, analyzers=[analyzer])

    # Not an mp3 file, should throw error.
    input_path = os.path.join(os.path.dirname(__file__), "test_files/species_list.txt")

    # Add a mocked call for on_analyze_complete
    watcher.on_error = Mock()

    # Create a "file-created" event in the watcher.
    # Normally a txt would never make it this far,
    # but we're just testing to see if error is thrown.
    event = namedtuple("Event", "src_path")
    event.src_path = input_path
    watcher._on_closed(event)

    # Check complete call count and results.
    assert watcher.on_error.call_count == 1
    assert type(watcher.on_error.call_args.args[0]).__name__ == "Recording"
    assert type(watcher.on_error.call_args.args[1]).__name__ == "AudioFormatError"

def test_default_analyzer():

    # Test that if an analyzer isn't provided, that the LiteAnalyzer is used.

    directory = "."
    watcher = DirectoryWatcher(directory)
    assert type(watcher.analyzers[0]).__name__ == "Analyzer"


import tempfile
from os import path, sep

from django import forms
from django.forms.util import ErrorList
from django.conf import settings
from django.template.loader import render_to_string

from transhette import polib, poutil

PO_PROJECT_BASE = 'po_project_base'

class FormAdminDjango(forms.Form):

    def as_django_admin(self):
        return render_to_string('transhette/form_admin_django.html', {'form': self, })

class UpdateConfirmationPoForm(FormAdminDjango):
    pass

class UpdatePoForm(FormAdminDjango):
    priority = forms.BooleanField(required=False)
    file = forms.FileField()

    def __init__(self, pofile, *args, **kwargs):
        super(UpdatePoForm, self).__init__(*args, **kwargs)
        self.fields['priority'].is_checkbox = True
        self.data_file = None
        self.pofile = pofile
        if not pofile:
            application_choices = self._get_application_choices()
            self.fields['application'] = forms.ChoiceField(choices=application_choices, required=False)

            language_choices = [('', '-----')]
            if hasattr(settings, 'LANGUAGES'):
                language_choices.extend([(key, "%s (%s)" % (value, key)) \
                                        for key, value in dict(settings.LANGUAGES).items()])
            self.fields['language'] = forms.ChoiceField(choices=language_choices, required=False)
            self.fields.keyOrder = ['application', 'language', 'priority', 'file']

    def clean(self):
        cleaned_data = super(UpdatePoForm, self).clean()
        if not self.errors and not self.pofile:
            try:
                tmp_file, po_tmp, po_dest_file = self._get_files_to_merge()
                tmp_file.close()
            except IOError:
                file_error = self._errors.get('file', ErrorList([]))
                file_error_new = ErrorList([u'Information incompatible for find the destination file'])
                file_error.extend(file_error_new)
                self._errors['file'] = ErrorList(file_error)
        return cleaned_data

    def save_temporal_file(self):
        tmp_file, po_tmp, po_dest_file = self._get_files_to_merge()
        tmp_file.flush()
        return po_tmp, po_dest_file, self.cleaned_data['priority']

    def _get_files_to_merge(self):
        # Escribo el archivo que ha mandado el usuario en un archivo temporal
        temporal_filepath = tempfile.NamedTemporaryFile().name
        tmp_file = open(temporal_filepath, "w")

        if self.data_file is None:
            self.data_file = self.cleaned_data['file'].read()
        tmp_file.write(self.data_file)
        tmp_file.flush()
        po_tmp = polib.pofile(temporal_filepath)

        if not self.pofile:
            # Consigo la ruta del archivo con el cual voy a hacer un merge, creo un pofile
            path_file = _get_path_file(po_tmp, self.cleaned_data['file'].name,
                        self.cleaned_data.get('language', None),
                        self.cleaned_data.get('application', None))
            po_dest_file = polib.pofile(path_file)
        else:
            po_dest_file = self.pofile
        return (tmp_file, po_tmp, po_dest_file)

    def _get_application_choices(self):
        l = []
        choices = [('', '-----')]
        for language in settings.LANGUAGES:
            l_extend = poutil.find_pos(language[0], include_djangos=False, include_transhette=False)
            l_extend = [item.split(settings.BASEDIR)[1] for item in l_extend if item.startswith(settings.BASEDIR)]
            l.extend(l_extend)
        for item in l:
            item_split = item.split(sep)
            if item_split[1] == 'locale':
                if not (PO_PROJECT_BASE, 'entire project') in choices:
                    choices.append((PO_PROJECT_BASE, 'entire project'))
            else:
                item_split2 = item.split(sep+"locale"+sep)
                item_tuple = (item_split2[0][1:], item_split2[0].split('/')[-1])
                if not item_tuple in choices:
                    choices.append(item_tuple)
        return choices

def _get_lang(lang, lang_cleaned_data=None):
    if lang_cleaned_data:
        return lang_cleaned_data
    if not lang:
        return ''
    lang = lang.replace('\n', '')
    lang = lang.strip()
    lang_words = lang.split(' ')
    if len(lang_words):
        lang = lang_words[0]
    return lang.lower()

def _get_lang_by_file(file_path):
    cut_start = "locale/"
    cut_end = "/LC_MESSAGES"
    index_start = file_path.index(cut_start) + len(cut_start)
    index_end = file_path.index(cut_end)
    return file_path[index_start:index_end]

def _get_application(application, application_cleaned_data=None):
    if application_cleaned_data:
        app = application_cleaned_data
        if app != PO_PROJECT_BASE:
            return app
        else:
            return ''

    if not application:
        return ''

    application = application.replace('\n', '')
    application = application.strip()
    return application.lower()

def _get_path_file(po_tmp, filename, lang=None, application=None):
    lang = _get_lang(po_tmp.metadata.get('Language-Team'), lang)
    directory = _get_application(po_tmp.metadata.get('Project-Id-Version'), application)
    return path.join(settings.BASEDIR, directory, 'locale', lang, 'LC_MESSAGES', filename)


import sqlite3

conn = sqlite3.connect('D:\pythonPRJ\kgitbankPython\sqliteTest\example.db')
c = conn.cursor()
data = [
    ('2020-03-07','buy','rhat',100,35.14),
    ('2020-02-09','buy','net',80,24.95),
    ('2020-03-05','buy','com',54,220.55),
    ('2020-01-18','buy','rhat',210,35.14)
    ]

sql = """insert into stocks values(?,?,?,?,?)"""
c.executemany(sql, data)

conn.commit()
conn.close()


# Given a string and a non-negative int n,
# we'll say that the front of the string is the first 3 chars,
# or whatever is there if the string is less than length 3.
# Return n copies of the front.

text = str(input())
n = int(input())
prefix = ""
ans = ""
for i in range (0, min(3, len(text))):
  prefix += text[i];
for i in range (0, n):
  ans += prefix
print(ans)


from django.shortcuts import render, get_object_or_404
from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views import generic
from django.contrib.auth.models import User
import nfldb
import json

from .models import Team, Player, UserTeam, UserPlayer, NflDbHelper
from .forms import CreateTeamForm

# Create one instance of the db per process

# Create your views here.
def index(request):
    """ Home page of site """
    return render(request, 'index.html', { 'page': 'home' })

def page_not_found(request):
    """ 404 Error Page """
    return render(request, 'page_not_found.html')

def build_query(year, phase, week):
    db = nfldb.connect()
    query = nfldb.Query(db)

    query.game(season_year=year)

    weeks = []
    if phase != 'All':
        if phase == 'Preseason':
            weeks += ["All", "1", "2", "3", "4"]
        elif phase == 'Regular':
            weeks += ["All", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"]
        elif phase == 'Postseason':
            weeks += ["All", "1", "2", "3"]
        else:
            raise Http404("Page not found!")

        query.game(season_type=phase)

        if week != 'All':
            query.game(week=week)

    return (query, weeks)

def players_paginate(query, page):
    paginator = Paginator(query, 20)

    try:
        stats = paginator.page(page)
    except PageNotAnInteger:
        # if page is not an integer, return the first page
        stats = paginator.page(1)
    except EmptyPage:
        # if page is out of range, return the last page
        stats = paginator.page(paginator.num_pages)

    return stats

def team_create(request):

    if request.method == 'POST':
        form = CreateTeamForm(request.POST)

        # check if the form is valid
        if form.is_valid():
            ut = UserTeam()
            ut.team_name = form.cleaned_data['team_name']
            ut.user = User.objects.get(id=request.POST['user_id'])
            ut.save()

        return HttpResponseRedirect('/')
    else:
        # If this is a GET (or any other) request, create the form
        form = CreateTeamForm()

    return render(request, 'football/team_create.html', {'form': form })

def team_detail(request, pk):

    # Get the team object
    team = get_object_or_404(UserTeam, pk=pk)

    # Get the players
    year = 2016
    (qbs, rbs, wrs, tes, oth) = NflDbHelper.players(year, team.userplayer_set.all())

    return render(request,'football/team_detail.html', {
        'team': team,
        'qbs': qbs,
        'rbs': rbs,
        'wrs': wrs,
        'tes': tes,
        'oth': oth,
    })

def reload_team_detail(request, pk):

    # Get the team object
    team = get_object_or_404(UserTeam, pk=pk)

    # Get the players
    year = 2016
    (qbs, rbs, wrs, tes, oth) = NflDbHelper.players(year, team.userplayer_set.all())

    return render(request,'football/reload_team_detail.html', {
        'team': team,
        'qbs': qbs,
        'rbs': rbs,
        'wrs': wrs,
        'tes': tes,
        'oth': oth,
    })

def add_player_to_team(request, player_id):

    if request.method == 'POST':
        # add players to selected teams

        team_ids = request.POST.getlist('team_ids[]')
        response_data = {}

        if team_ids:
            for team_id in team_ids:
                team = UserTeam.objects.get(id=team_id)
                player = Player.objects.get(player_id=player_id)

                # Check if this player has been added
                if UserPlayer.objects.filter(fantasy_team__exact=team, player__exact=player).count() != 0:
                    response_data[team.id] = '%s already added to %s' % (player, team)
                else:
                    up = UserPlayer()
                    up.fantasy_team = UserTeam.objects.get(id=team_id)
                    up.player = Player.objects.get(player_id=player_id)
                    up.save()

                    response_data[team.id] = '%s added to %s' % (player, team)

        else:
            response_data['result'] = 'No teams selected!'

        return HttpResponse(json.dumps(response_data), content_type="application/json")

    else:
        return HttpResponse(
                json.dumps({"nothing to see": "this isn't happening"}),
                content_type="application/json"
        )

def del_player_from_team(request, player_id):

    if request.method == 'POST':
        # remove player from team

        team_id = request.POST.get('team_id')
        response_data = {}

        if team_id:
            team = UserTeam.objects.get(id=team_id)
            player = Player.objects.get(player_id=player_id)
            up = UserPlayer.objects.get(fantasy_team_id=team_id, player_id=player_id)
            up.delete()

            response_data[team.id] = '%s removed from %s' % (player, team)

        else:
            response_data['result'] = 'No team found'

        return HttpResponse(
                json.dumps(response_data),
                content_type="application/json",
        )

def players_quarterbacks(request, year="2016", phase="Regular", week="All"):
    """ Listing of quarterbacks """
    sort = request.GET.get('sort', 'ppr')
    weeks = NflDbHelper.weeks(phase)
    fantasy = NflDbHelper.query(year, phase, week, 'QB', sort)

    # Build the pager
    page = request.GET.get('page')
    stats = players_paginate(fantasy, page)

    return render(request, 'positions/passing.html',
            { 'position': 'Quarterbacks',
                'urlId': 'pos-qb',
                'page': 'positions',
                'year': year,
                'phase': phase,
                'weeks': weeks,
                'week': week,
                'stats': stats,
            })

def players_runningbacks(request, year="2016", phase="Regular", week="All"):
    """ Listing of running backs """
    sort = request.GET.get('sort', 'ppr')
    weeks = NflDbHelper.weeks(phase)
    fantasy = NflDbHelper.query(year, phase, week, 'RB', sort)

    # Build the pager
    page = request.GET.get('page')
    stats = players_paginate(fantasy, page)

    return render(request, 'positions/receiving.html',
            { 'position': 'Running Backs',
                'urlId': 'pos-rb',
                'page': 'positions',
                'year': year,
                'phase': phase,
                'weeks': weeks,
                'week': week,
                'stats': stats })

def players_widereceivers(request, year="2016", phase="Regular", week="All"):
    """ Listing of wide receivers """
    sort = request.GET.get('sort', 'ppr')
    weeks = NflDbHelper.weeks(phase)
    fantasy = NflDbHelper.query(year, phase, week, 'WR', sort)

    # Build the pager
    page = request.GET.get('page')
    stats = players_paginate(fantasy, page)

    return render(request, 'positions/receiving.html',
            { 'position': 'Wide Receivers',
                'urlId': 'pos-wr',
                'page': 'positions',
                'year': year,
                'phase': phase,
                'weeks': weeks,
                'week': week,
                'stats': stats })

def players_tightends(request, year="2016", phase="Regular", week="All"):
    """ Listing of wide receivers """
    sort = request.GET.get('sort', 'ppr')
    weeks = NflDbHelper.weeks(phase)
    fantasy = NflDbHelper.query(year, phase, week, 'TE', sort)

    # Build the pager
    page = request.GET.get('page')
    stats = players_paginate(fantasy, page)

    return render(request, 'positions/receiving.html',
            { 'position': 'Tight-Ends',
                'urlId': 'pos-te',
                'page': 'positions',
                'year': year,
                'phase': phase,
                'weeks': weeks,
                'week': week,
                'stats': stats })

class NflListView(generic.ListView):
    model = Team
    template_name = 'football/nfl_team_list.html'

class NflDetailView(generic.DetailView):
    model = Team
    template_name = 'football/nfl_team_detail.html'

class PlayerListView(generic.ListView):
    model = Player


# RachelPotter.py
# A program that changes the lowercase names in a .txt file to uppercase and prints them to a new file

def main():
    infile_name = "Before.txt"
    outfile_name = "After.txt"
    infile = open(infile_name, "r")
    outfile = open(outfile_name, "w")
    for row in infile:
        for letter in row:
            cap_name = letter.capitalize()
            print(cap_name, end="", file=outfile)
    infile.close()
    outfile.close()
    print("Capitalized names have been written to:", outfile_name)

main()

# I pledge my honor that I have abided by the Stevens Honor System
# Rachel Potter


from silence_tensorflow import silence_tensorflow
import tensorflow as tf
from tensorflow.keras import layers, Model, models, Input, regularizers, initializers
from tensorflow.keras import backend as K
import numpy as np
import matplotlib.pyplot as plt
import glob
import imageio
import os
import time
import datetime
import pathlib
from PIL import Image
import cv2
import csv
import Template

silence_tensorflow()
tf.config.run_functions_eagerly(True)
# gpu = tf.config.list_physical_devices('GPU')
# tf.config.experimental.set_memory_growth(gpu[0], True)

class MoGLayer(layers.Layer):
    def __init__(self,
                 kernel_regularizer=None,
                 kernel_initializer=None,  # 'glorot_uniform',
                 bias_initializer=None,  # 'zeros',
                 **kwargs):
        if 'input_shape' not in kwargs and 'input_dim' in kwargs:
            kwargs['input_shape'] = (kwargs.pop('input_dim'),)
        super(MoGLayer, self).__init__(**kwargs)
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)

    def build(self, input_shape):
        assert len(input_shape) >= 2
        input_dim = input_shape[-1]

        self.std = self.add_weight(shape=(input_dim,),
                                   name='std',
                                   initializer=self.kernel_initializer,
                                   regularizer=self.kernel_regularizer)

        self.mean = self.add_weight(shape=(input_dim,),
                                    initializer=self.bias_initializer,
                                    name='mean')

        self.built = True

    def call(self, inputs, *args, **kwargs):
        output = inputs * self.std
        output = K.bias_add(output, self.mean)
        return output

    def compute_output_shape(self, input_shape):
        assert input_shape and len(input_shape) >= 2
        assert input_shape[-1]
        output_shape = list(input_shape)
        output_shape[-1] = input_shape[-1]
        return tuple(output_shape)

def time_string(sec_elapsed):
    h = int(sec_elapsed/3600)
    m = int((sec_elapsed % 3600)/60)
    s = sec_elapsed % 60
    return '{}:{:>2}:{:>05.2f}'.format(h, m, s)

# Parameter
image_row = 64
image_column = 64
image_color = 3
batch_size = 16
feature_size = 2048
category_size = 40
noise_size = 2048
Epoch = 1000

# Optimizer
generator_optimizer = tf.keras.optimizers.Adam(1e-4, beta_1=0.5)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4, beta_1=0.5)

current_folder = pathlib.Path(__file__).parent

# Models
extractor_file = 'Results/_Feature_Extractor_Model'  # Trained Classifier
extractor = models.load_model(extractor_file, compile=False)
Feature_Layer_Model = Model(inputs=extractor.input, outputs=extractor.get_layer('features2048').output)

# Model for Classifying Generated Images
resnet40_file = 'Data/ResNet_40Class/ResNet_40class_Model'
resnet40 = models.load_model(resnet40_file, compile=False)
resnet40.trainable = False

def make_generator():
    noise_inputs = Input(shape=(noise_size,))
    eeg_inputs = Input(shape=(feature_size,))

    x = MoGLayer(kernel_initializer=initializers.RandomUniform(minval=-0.2, maxval=0.2),
                 bias_initializer=initializers.RandomUniform(minval=-1.0, maxval=1.0),
                 kernel_regularizer=regularizers.l2(0.01))(noise_inputs)
    x = layers.multiply([x, eeg_inputs])

    x = layers.Reshape((1, 1, -1))(x)
    x = layers.Conv2DTranspose(filters=512, kernel_size=4, strides=1, padding='valid')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.Conv2DTranspose(filters=256, kernel_size=4, strides=2, padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.Conv2DTranspose(filters=128, kernel_size=4, strides=2, padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.Conv2DTranspose(filters=64, kernel_size=4, strides=2, padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.Conv2DTranspose(filters=3, kernel_size=4, strides=2, padding='same')(x)
    output = layers.Activation("tanh")(x)

    model = Model(inputs=[noise_inputs, eeg_inputs], outputs=output)
    return model

def make_discriminator():
    image_inputs = Input(shape=(image_row, image_column, image_color))

    x = layers.Conv2D(64, (3, 3), strides=(1, 1), padding='same')(image_inputs)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.Dropout(0.5)(x)

    x = layers.Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.Dropout(0.5)(x)

    x = layers.Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(256, (1, 1), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.Dropout(0.5)(x)

    x = layers.Conv2D(512, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(512, (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)
    x = layers.Conv2D(512, (1, 1), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(0.2)(x)

    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.Dropout(0.5)(x)

    x = layers.Flatten()(x)

    real_fake = layers.Dense(1024, activation='relu')(x)
    real_fake = layers.Dropout(0.5)(real_fake)
    real_fake = layers.Dense(1, activation='sigmoid')(real_fake)

    category = layers.Dense(1024, activation='relu')(x)
    category = layers.Dropout(0.5)(category)
    category = layers.Dense(1024, activation='relu')(category)
    category = layers.Dropout(0.5)(category)
    category = layers.Dense(category_size, activation='softmax')(category)

    model = Model(inputs=image_inputs, outputs=[real_fake, category])
    return model

generator = make_generator()
discriminator = make_discriminator()

# Create Image Dictionary
data_img_name_label = np.empty((2000, 2), dtype=np.object)
image_dictionary = {}
folder_path = 'Data/Class_Images_Selected'
images_file = 'Data/Class_Images_Selected/Labels.csv'
with open(images_file) as f:
    img_data = csv.reader(f, delimiter='/')
    images_list = [row for row in img_data]
for i in range(len(images_list)):
    img_path = os.path.join(images_list[i][1], images_list[i][2])
    image_name = os.path.join(folder_path, img_path)
    one_image = Image.open(image_name)
    one_image = np.array(one_image.resize((image_row, image_column), Image.LANCZOS))
    if one_image.reshape(-1).shape[0] == image_row*image_column:
        one_image = np.array(Image.open(image_name))
        one_image = cv2.cvtColor(one_image, cv2.COLOR_GRAY2RGB)
        one_image = Image.fromarray(one_image)
        one_image = np.array(one_image.resize((image_row, image_column), Image.LANCZOS))
    one_image = (one_image - 127.5) / 127.5
    one_image = one_image.astype(np.float32)
    image_dictionary[images_list[i][1] + '/' + images_list[i][2]] = one_image
    data_img_name_label[i][0] = images_list[i][1] + '/' + images_list[i][2]
    data_img_name_label[i][1] = int(images_list[i][0])

# Load Data
loader = Template.data_block_loader_train_test_image_separate2(use_all=False, eye_remove=True, do_norm=True, do_zscore=True)

train_eeg = loader[0]
train_labels = loader[1].reshape((-1, 1))
train_images = loader[2].reshape((-1, 1))
test_eeg = loader[3]
test_labels = loader[4]
test_images = loader[5].reshape((-1, 1))
counter = loader[6]

print(train_eeg.shape)
print(test_eeg.shape)
print(counter)

train_features = Feature_Layer_Model.predict(train_eeg)
test_features = Feature_Layer_Model.predict(test_eeg)

base_data = np.concatenate([train_features, train_images, train_labels], axis=1)

# Loss
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=False)
category_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)

def generator_loss(fake_true_false, fake_pred, label):
    generated_loss = cross_entropy(tf.ones_like(fake_true_false), fake_true_false) + category_loss(label, fake_pred)
    return generated_loss

def discriminator_loss_real(real_true_false, real_pred, label):
    real_loss = cross_entropy(tf.ones_like(real_true_false), real_true_false) + category_loss(label, real_pred)
    return real_loss

def discriminator_loss_fake(fake_true_false):
    fake_loss = cross_entropy(tf.zeros_like(fake_true_false), fake_true_false)
    return fake_loss

loss_tracker_gen = tf.keras.metrics.Mean(name='gen_loss')
loss_tracker_disc = tf.keras.metrics.Mean(name='disc_loss')
loss_tracker_gen_total = tf.keras.metrics.Mean(name='gen_total_loss')
loss_tracker_disc_total = tf.keras.metrics.Mean(name='disc_total_loss')
loss_tracker_test = tf.keras.metrics.Mean(name='test_gen_loss')
acc_gen = tf.keras.metrics.SparseCategoricalAccuracy(name='gen_acc')
acc_disc = tf.keras.metrics.SparseCategoricalAccuracy(name='disc_acc')
acc_test = tf.keras.metrics.SparseCategoricalAccuracy(name='test_acc')
rf_d = tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='real')
rf_g = tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='fake')
rf_test = tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='rf')

# GAN Training Class
class GAN(Model):
    def __init__(self, gen_model, disc_model):
        super(GAN, self).__init__()
        self.gen_model = gen_model
        self.disc_model = disc_model

    def compile(self, g_optimizer, d_optimizer):
        super(GAN, self).compile()
        self.g_optimizer = g_optimizer
        self.d_optimizer = d_optimizer

    def train_step(self, data):
        x, label = data
        feature = x[0]
        real_image_name = x[1].numpy()
        data_size = tf.shape(feature)[0]
        noise = tf.random.normal(shape=(data_size, noise_size))

        real_images = np.empty((data_size, image_row, image_column, image_color), dtype=np.float32)
        for k0 in range(data_size):
            real_images[k0] = image_dictionary[real_image_name[k0][0].decode()]

        # Train Discriminator
        generated_images = self.gen_model([noise, feature], training=True)
        with tf.GradientTape() as tape:
            fake_true_false, _ = self.disc_model(generated_images, training=True)
            real_true_false, real_category = self.disc_model(real_images, training=True)
            disc_loss_real = discriminator_loss_real(real_true_false, real_category, label)
            disc_loss_fake = discriminator_loss_fake(fake_true_false)
            disc_total_loss = disc_loss_real + disc_loss_fake
        gradient = tape.gradient(disc_total_loss, self.disc_model.trainable_variables)
        self.d_optimizer.apply_gradients(zip(gradient, self.disc_model.trainable_variables))

        acc_disc.update_state(label, real_category)
        disc_loss = category_loss(label, real_category)
        rf_d.update_state(tf.ones_like(real_true_false), real_true_false)

        # Train Generator
        with tf.GradientTape() as tape:
            generated_images = self.gen_model([noise, feature], training=True)
            fake_true_false, fake_category = self.disc_model(generated_images, training=True)
            gen_total_loss = generator_loss(fake_true_false, fake_category, label)
        gradient = tape.gradient(gen_total_loss, self.gen_model.trainable_variables)
        self.g_optimizer.apply_gradients(zip(gradient, self.gen_model.trainable_variables))

        rf_g.update_state(tf.ones_like(fake_true_false), fake_true_false)

        resnet_image = tf.image.resize(generated_images, (224, 224), method=tf.image.ResizeMethod.LANCZOS3)
        resnet_image = (resnet_image + 1) / 2 * 255
        resnet_image = tf.keras.applications.resnet.preprocess_input(resnet_image)
        resnet_category = resnet40(resnet_image)
        acc_gen.update_state(label, resnet_category)
        gen_loss = category_loss(label, resnet_category)

        loss_tracker_gen.update_state(gen_loss)
        loss_tracker_disc.update_state(disc_loss)
        loss_tracker_gen_total.update_state(gen_total_loss)
        loss_tracker_disc_total.update_state(disc_total_loss)

        return {'gen_acc': acc_gen.result(), 'gen_loss': loss_tracker_gen.result(),
                'gen_total_loss': loss_tracker_gen_total.result(),
                'disc_acc': acc_disc.result(), 'disc_loss': loss_tracker_disc.result(),
                'disc_total_loss': loss_tracker_disc_total.result(),
                'real': rf_d.result(), 'fake': rf_g.result()}

    def test_step(self, data):
        feature, label = data
        data_size = tf.shape(feature)[0]
        noise = tf.random.normal(shape=(data_size, noise_size))

        generated_image = self.gen_model([noise, feature], training=False)
        fake_real_fake, fake_cat = self.disc_model(generated_image, training=False)
        rf_test.update_state(tf.ones_like(fake_real_fake), fake_real_fake)

        resnet_image = tf.image.resize(generated_image, (224, 224), method=tf.image.ResizeMethod.LANCZOS3)
        resnet_image = (resnet_image + 1) / 2 * 255
        resnet_image = tf.keras.applications.resnet.preprocess_input(resnet_image)
        resnet_cat = resnet40(resnet_image)
        acc_test.update_state(label, resnet_cat)
        gen_loss = category_loss(label, resnet_cat)
        loss_tracker_test.update_state(gen_loss)

        return {'test_acc': acc_test.result(), 'test_gen_loss': loss_tracker_test.result(),
                'rf': rf_test.result()}

    @property
    def metrics(self):
        return [acc_gen, acc_disc, acc_test, loss_tracker_gen, loss_tracker_gen_total,
                loss_tracker_disc, loss_tracker_disc_total, loss_tracker_test, rf_d, rf_g, rf_test]

# Create Generated Images After Each Epoch and Save Model After Each 100 Epoch
class MakeImage(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        generate_and_save_images(self.model.gen_model, epoch+1, seed, sample_feature)
        test_and_save_images(self.model.gen_model, epoch+1)
        if (epoch+1) % 100 == 0 and epoch+1 != Epoch:
            now0 = datetime.datetime.now()
            now_time0 = now0.strftime('%y%m%d%_H%M%S')
            generator_save_name0 = now_time0 + '_Generator_Model'
            discriminator_save_name0 = now_time0 + '_Discriminator_Model'
            generator.save(current_folder / 'Results' / generator_save_name0)
            discriminator.save(current_folder / 'Results' / discriminator_save_name0)

def generate_and_save_images(model, epoch, test_seed, feature):
    # 'training' is set to False
    # This is so all layers run in inference mode (batch norm).
    generated_image = model([test_seed, feature])
    generated_image = (generated_image+1)/2  # 0-1

    fig = plt.figure(figsize=(8, int(np.ceil(category_size/8))))

    for i2 in range(category_size):
        plt.subplot(8, int(np.ceil(category_size/8)), i2+1)
        plt.imshow(generated_image[i2])
        plt.axis('off')

    plt.savefig('Results/Generated_Images/image_at_epoch_{:04d}.png'.format(epoch))
    plt.close()
    # plt.show()

def test_and_save_images(model, epoch):
    # 'training' is set to False
    # This is so all layers run in inference mode (batch norm).
    for i3 in range(category_size):
        for ii2 in range(test_labels.shape[0]):
            if test_labels[ii2] == i3:
                # noise = np.random.normal(size=noise_size)
                noise = seed_for_test[i3]
                generated_image = model([np.array([noise]), np.array([test_features[ii2]])])
                generated_image = (generated_image + 1) / 2
                plt.subplot(8, int(np.ceil(category_size/8)), i3+1)
                plt.imshow(generated_image[0])
                plt.axis('off')
                break
    plt.savefig('Results/Tested_Images/image_at_epoch_{:04d}.png'.format(epoch))
    plt.close()
    # plt.show()

# Train
start = time.time()
seed = tf.random.normal([category_size, noise_size])
seed_for_test = np.random.normal(size=(category_size, noise_size))
sample_feature = np.empty((category_size, feature_size))  # For making GIF
sample_image = np.empty((category_size, image_row, image_column, image_color))

# Save Real Images in Train Dataset
for k in range(category_size):
    for m in range(len(base_data)):
        if int(base_data[m][-1]) == k:
            sample_feature[k] = base_data[m][:feature_size].astype(np.float32)
            sample_image_name = base_data[m][feature_size:-1]
            sample_img = Image.open('Data/Class_Images_Selected/' + sample_image_name[0])
            sample_img_array = np.asarray(sample_img)
            sample_img_array = Image.fromarray(sample_img_array)
            sample_image[k] = np.array(sample_img_array.resize((image_row, image_column), Image.LANCZOS))
            break
fig0 = plt.figure(figsize=(8, int(np.ceil(category_size / 8))))
for i0 in range(category_size):
    plt.subplot(8, int(np.ceil(category_size / 8)), i0 + 1)
    plt.imshow(sample_image[i0].astype(np.uint8))
    plt.axis('off')
plt.savefig('Results/GAN_real_image.png')
plt.close()

# Save Real Images in Test Dataset
test_sample_image = np.empty((category_size, image_row, image_column, image_color))
for k in range(category_size):
    for m in range(test_labels.shape[0]):
        if test_labels[m] == k:
            test_sample_img = Image.open('Data/Class_Images_Selected/' + test_images[m][0])
            test_sample_img_array = np.asarray(test_sample_img)
            test_sample_img_array = Image.fromarray(test_sample_img_array)
            test_sample_image[k] = np.array(test_sample_img_array.resize((image_row, image_column), Image.LANCZOS))
            break
fig0 = plt.figure(figsize=(8, int(np.ceil(category_size / 8))))
for i0 in range(category_size):
    plt.subplot(8, int(np.ceil(category_size / 8)), i0 + 1)
    plt.imshow(test_sample_image[i0].astype(np.uint8))
    plt.axis('off')
plt.savefig('Results/GAN_real_test_image.png')
plt.close()

gan = GAN(gen_model=generator, disc_model=discriminator)
gan.compile(g_optimizer=generator_optimizer, d_optimizer=discriminator_optimizer)
history = gan.fit(x=[train_features, train_images], y=train_labels, epochs=Epoch, batch_size=batch_size,
                  validation_data=(test_features, test_labels), verbose=2,
                  callbacks=[MakeImage()])
total_time = time.time() - start
print(f'Total Time: {time_string(total_time)}')

# Save Generator and Discriminator
now = datetime.datetime.now()
now_time = now.strftime('%y%m%d%_H%M%S')
generator_save_name = now_time + '_Generator_Model'
discriminator_save_name = now_time + '_Discriminator_Model'
generator.save(current_folder/'Results'/generator_save_name)
discriminator.save(current_folder/'Results'/discriminator_save_name)

save_name = 'Results/' + now_time + '_GAN_Results'
np.savez(save_name,
         gen_accs=history.history['gen_acc'], gen_losses=history.history['gen_loss'],
         gen_total_loss=history.history['gen_total_loss'], disc_total_loss=history.history['disc_total_loss'],
         test_gen_accs=history.history['val_test_acc'], test_gen_losses=history.history['val_test_gen_loss'],
         fake_rf=history.history['fake'], real_rf=history.history['real'], test_rf=history.history['val_rf'])

# Create a GIF
anim_file = now_time + '_GAN_generated_images.gif'
with imageio.get_writer('Results/Generated_Images/' + anim_file, mode='I') as writer:
    filenames = glob.glob('Results/Generated_Images/image*.png')
    filenames = sorted(filenames)
    last = -1
    for ii, filename in enumerate(filenames):
        frame = 2*(ii**0.5)
        if round(frame) > round(last):
            last = frame
        else:
            continue
        image = imageio.imread(filename)
        writer.append_data(image)
    image = imageio.imread(filename)
    writer.append_data(image)

# Create a GIF
anim_file2 = now_time + '_GAN_tested_images.gif'
with imageio.get_writer('Results/Tested_Images/' + anim_file2, mode='I') as writer:
    filenames2 = glob.glob('Results/Tested_Images/image*.png')
    filenames2 = sorted(filenames2)
    last = -1
    for ii, filename2 in enumerate(filenames2):
        frame = 2*(ii**0.5)
        if round(frame) > round(last):
            last = frame
        else:
            continue
        image2 = imageio.imread(filename2)
        writer.append_data(image2)
    image2 = imageio.imread(filename2)
    writer.append_data(image2)

# Plot
fig1 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['gen_acc'], label='Generator Accuracy')
plt.plot([0, Epoch], [0.025, 0.025], color='gray', linestyle='dashed')
plt.xlabel('Epoch')
plt.ylabel('Train Accuracy')
plt.legend(loc='lower right')
plt.subplot(2, 1, 2)
plt.plot(history.history['gen_loss'], label='Generator Loss')
plt.xlabel('Epoch')
plt.ylabel('Train Loss')
plt.legend(loc='upper right')

fig2 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['disc_acc'], label='Discriminator Accuracy')
plt.plot([0, Epoch], [0.025, 0.025], color='gray', linestyle='dashed')
plt.xlabel('Epoch')
plt.ylabel('Train Accuracy')
plt.legend(loc='lower right')
plt.subplot(2, 1, 2)
plt.plot(history.history['disc_loss'], label='Discriminator Loss')
plt.xlabel('Epoch')
plt.ylabel('Train Loss')
plt.legend(loc='upper right')

fig3 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['gen_total_loss'], label='Generator Total Loss')
plt.ylabel('Total Loss')
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(history.history['disc_total_loss'], label='Discriminator Total Loss')
plt.xlabel('Epoch')
plt.ylabel('Total Loss')
plt.legend()

fig4 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['val_test_acc'], label='Generator Test Accuracy')
plt.plot([0, Epoch], [0.025, 0.025], color='gray', linestyle='dashed')
plt.xlabel('Epoch')
plt.ylabel('Test Accuracy')
plt.legend(loc='lower right')
plt.subplot(2, 1, 2)
plt.plot(history.history['val_test_gen_loss'], label='Generator Loss')
plt.xlabel('Epoch')
plt.ylabel('Test Loss')
plt.legend(loc='upper left')

fig5 = plt.figure()
plt.plot(history.history['fake'], label='Fake True False')
plt.plot(history.history['real'], label='Real True False')
plt.xlabel('Epoch')
plt.legend()

fig6 = plt.figure()
plt.plot(history.history['val_rf'], label='Test True False')
plt.xlabel('Epoch')
plt.legend()

plt.show()


import sys
sys.stdin = open('input.txt')

# 가장 나중에 사용되거나 앞으로 사용하지 않을 숫자 반환
def get_far_num(arr):
    idxs = {}

    for p in plug:
        if p not in arr:
            return p
        else:
            k = arr.index(p)
            idxs[k] = p

    max_key = max(idxs.keys())
    return idxs[max_key]

# 입력 데이터
N, K = map(int, sys.stdin.readline().split())
li = list(map(lambda x: int(x)-1, sys.stdin.readline().split()))

answer = 0

# 플러그 꽂아진 숫자들
plug = set()
for idx in range(K):
    # 다음에 사용할 번호
    next = li[idx]

    # 이미 플러그에 꽂아서 사용중이면 아무 동작 안함
    if next in plug:
        pass
    # 플러그 모두 사용중 아니라면 추가
    elif len(plug) < N:
        plug.add(next)
    # 가장 나중에 사용되거나 앞으로 사용되지 않을 숫자 삭제 및 새로 추가
    else:
        num = get_far_num(li[idx+1:])
        plug.remove(num)
        plug.add(next)
        answer += 1

print(answer)


import os
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from catVdog.models import IMG
from Include.cnn.predict import predict

# Create your views here.
# 添加 index 函数，返回 index.html 页面
def index(request):
    return render(request, 'index.html')

@csrf_exempt
def uploadImg(request):
    for file in os.listdir("E:/PycharmProjects/CatVsDog/media/img/"):
        targetFile = os.path.join("E:/PycharmProjects/CatVsDog/media/img/", file)
        if os.path.isfile(targetFile):
            os.remove(targetFile)
    if request.method == 'POST':
        new_img = IMG(
            img=request.FILES.get('img'),
            name=request.FILES.get('img').name
        )
        new_img.save()
    return render(request, 'uploadimg.html')

def result (request):
    result = predict()
    return render(request, 'result.html', {"data": result})


import streamlit as st
import pickle
import numpy as np
import pandas as pd

model = pickle.load(open("model.pkl", "rb"))
dt = pickle.load(open("dictionary_map.pkl", "rb"))

st.header("Laptop Price Predictor!!!")

val1 = st.selectbox(
    'Company Name',
    (dt["company_name"].keys()))

val2 = st.selectbox(
    'Processor Name',
    (dt["processor_name"].keys()))

val3 = st.selectbox(
    'Processor Generation',
    (dt["processor_generation"].keys()))

val4 = st.selectbox(
    'Operating System',
    ("DOS ","64 bit Windows 11 ", "64 bit Windows 10 ", "Windows 11 ", "Mac OS ", "Windows 10 ", "Chrome "))

val5 = st.selectbox(
    'Storage (SSD)',
    (128, 256, 512))

val6 = st.selectbox(
    'Screen Size',
    (14, 15.6, 16, 17))

val7 = st.selectbox(
    'Ram Capacity (GB)',
    (4, 8, 16, 32))

val8 = st.selectbox(
    'Ram Type',
    (dt["ram_type"].keys()))

value = [dt["company_name"][val1], dt["processor_name"][val2], dt["processor_generation"][val3],
dt["operating_system"][val4], int(val5), int(val6), int(val7), dt["ram_type"][val8]]

price = model.predict([value])
print(price)
st.write('\n\tPrice of the laptop approxmately ₹', np.round(price[0], decimals=2))


"""
---------------------------------------------------------
Change Log
Author    Date:       Change:             Reason:

---------------------------------------------------------
Python Scripting Activity
For Instructor:
Author: Ken Livesey

---------------------------------------------------------

Activity 2: Simple printing with line breaks

Using the lesson shell script and a text editor, create a Python script. Change the documentation to show you wrote the script.

This script should do the following:
Print the last 3 movies that you saw in the theatre. Separate each movie with a blank line.
Print the last 3 software titles that you purchased. Separate each software with 2 blank lines.
Print your 3 favorite meals. Separate each meal with 3 blank lines.

Save the script as Lesson4_2.py

Test the script in Python """

# Set Vars
MOVIES = ["The Hobbit", "South Paw", "Inside Out"]
STITLE = ["NordVPN", "Office 2013", "Ninite Updater"]
MEALS = ["Tom's Ribeyes", "Fresh BLT's", "Christmas Lasagna"]
STR1 = "The last three movies I attended are:"
STR2 = "The last three software programs I purchased are:"
STR3 = "My top three favorite meals are:"

# List last three movies with single spaces in between
print (STR1)
for X in MOVIES:
        print(X + "\n")

# List last three software purchases with double spaces in between
print (STR2)
for X in STITLE:
        print(X + "\n\n")

# List top three meals with double spaces in between
print (STR3)
for X in MEALS:
        print(X +"\n\n\n")

# end of script


# lista = []
# baza = [20, -3, 12, -89, 22, -67]
#
# a = "dodatnie"
# b = "ujemne"
#
# if a >= 1:
#     print("Są to liczby dodatnie")
# elif b <= 1:
#     print("Są to liczby ujemne")
#

baza = [22, 34, 12, -3, -44, -34]

dodatnie = 0
ujemne = 0

for liczba in baza:
    if liczba > 0:
        dodatnie += 1
    elif liczba < 0:
        ujemne += 1

print(f"Liczba dodatnich: {dodatnie}, liczby ujemnie: {ujemne}")

licznik_dodatnich = len([x for x in baza if x > 0])
licznik_ujemnych = len([x for x in baza if x < 0])
print(f"Liczby dodatnie: {licznik_dodatnich}, Liczby ujemne: {licznik_ujemnych}")


# %%
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, BatchNormalization
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import layers
import tensorflow as tf
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
from os import scandir
import pyedflib as pyedf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from PyAstronomy import pyaC
import warnings
import scipy as sp
import scipy.fftpack
warnings.filterwarnings('ignore')
# %%
ruta_archivos = "../../data/sleep-edf-database-expanded-1.0.0/sleep-cassette"
lista_archivos = [arch.name for arch in scandir(ruta_archivos) if arch.is_file()]
# %%
num_canales = 7
excedente_despierto = 0
excedente_ligero = 0
excedente_rem = 0
datos_1 = []
datos_2 = []
etiquetas = []
descriptores_1 = [[] for _ in range(27)]
descriptores_2 = [[] for _ in range(24)]
#%%
lista_nueva = lista_archivos[145:len(lista_archivos)]
#%%
for ruta_actual in lista_nueva:
    print(ruta_actual)
    if ruta_actual[9:len(ruta_actual)] == "Hypnogram.edf":
        identificador = ruta_actual[2:7]
        st_FileHypEdf = pyedf.EdfReader(ruta_archivos + "/" + ruta_actual)
        v_HypTime, v_HypDur, v_Hyp = st_FileHypEdf.readAnnotations()# Lectura de las señales s_SigNum señales con nombres v_Signal_Labels
        st_FileEdf = pyedf.EdfReader(ruta_archivos + "/SC" + identificador + "0-PSG.edf")
        s_SigNum = st_FileEdf.signals_in_file
        v_Signal_Labels = st_FileEdf.getSignalLabels()

        s_FirstInd = 0
        datos_actual = [[] for _ in range(num_canales)]
        etiquetas_actual = []
        for s_SigRef in range(num_canales):
            s_NSamples = st_FileEdf.getNSamples()[0]
            s_FsHz = st_FileEdf.getSampleFrequency(s_SigRef)
            v_Sig = st_FileEdf.readSignal(s_SigRef)
            v_Time = np.arange(0, s_NSamples) / s_FsHz
            s_WinSizeSec = 30
            s_WinSizeSam = np.round(s_FsHz * s_WinSizeSec)
            s_FirstInd = 0
            while 1:
                s_LastInd = s_FirstInd + s_WinSizeSam
                if s_LastInd > s_NSamples:
                    break
                for i in range(len(v_Hyp)-1):
                    if ((v_HypTime[i] <= s_FirstInd/s_FsHz) and (s_FirstInd/s_FsHz < v_HypTime[i+1])):
                        if v_Hyp[i] == "Sleep stage W" or v_Hyp[i] == "Sleep stage 1" or v_Hyp[i] == "Sleep stage 2" or v_Hyp[i] == "Sleep stage 3" or v_Hyp[i] == "Sleep stage 4" or v_Hyp[i] == "Sleep stage R":
                            if s_SigRef == 0:
                                etiquetas_actual.append(v_Hyp[i])
                            datos_actual[s_SigRef].append(np.array(v_Sig[s_FirstInd:s_LastInd]))
                        break
                s_FirstInd = s_LastInd

        despierto = 0
        ligero = 0
        profundo = 0
        rem = 0
        for etiqueta in etiquetas_actual:
            if etiqueta == "Sleep stage W":
                despierto = despierto + 1
            elif etiqueta == "Sleep stage 1" or etiqueta == "Sleep stage 2":
                ligero = ligero + 1
            elif etiqueta == "Sleep stage 3" or etiqueta == "Sleep stage 4":
                profundo = profundo + 1
            elif etiqueta == "Sleep stage R":
                rem = rem + 1

        if despierto < profundo:
            excedente_despierto = excedente_despierto + (profundo - despierto)
        if ligero < profundo:
            excedente_ligero = excedente_ligero + (profundo - ligero)
        if rem < profundo:
            excedente_rem = excedente_rem + (profundo - rem)

        datos_actual_1 = np.array(datos_actual[0:3])
        datos_actual_2 = np.array(datos_actual[3:7])
        datos_actual_canales_1 = []
        datos_actual_canales_2 = []
        for i in range(datos_actual_1.shape[1]):
            datos_actual_canales_1.append(datos_actual_1[:,i,:].transpose())
            datos_actual_canales_2.append(datos_actual_2[:,i,:].transpose())

        X_datos_1, X_datos_2, y_datos_11, y_datos_21 = train_test_split(datos_actual_canales_1, etiquetas_actual, test_size=0.5, random_state=4)
        datos_shuffle_1 = np.concatenate((X_datos_1, X_datos_2))
        X_datos_1, X_datos_2, y_datos_12, y_datos_22 = train_test_split(datos_actual_canales_2, etiquetas_actual, test_size=0.5, random_state=4)
        datos_shuffle_2 = np.concatenate((X_datos_1, X_datos_2))
        etiquetas_shuffle = np.concatenate((y_datos_11, y_datos_22))

        despierto = 0
        ligero = 0
        rem = 0
        for i in range(etiquetas_shuffle.shape[0]):
            agregar_dato = False
            if etiquetas_shuffle[i] == "Sleep stage W":
                if despierto < (profundo+excedente_despierto):
                    etiquetas.append(0)
                    agregar_dato = True
                    if despierto == profundo:
                        excedente_despierto = excedente_despierto - 1
                    else:
                        despierto = despierto + 1
            elif etiquetas_shuffle[i] == "Sleep stage 1" or etiquetas_shuffle[i] == "Sleep stage 2":
                if ligero < (profundo+excedente_ligero):
                    etiquetas.append(1)
                    agregar_dato = True
                    if ligero == profundo:
                        excedente_ligero = excedente_ligero - 1
                    else:
                        ligero = ligero + 1
            elif etiquetas_shuffle[i] == "Sleep stage 3" or etiquetas_shuffle[i] == "Sleep stage 4":
                etiquetas.append(2)
                agregar_dato = True
            elif etiquetas_shuffle[i] == "Sleep stage R":
                if rem < (profundo+excedente_rem):
                    etiquetas.append(3)
                    agregar_dato = True
                    if rem == profundo:
                        excedente_rem = excedente_rem - 1
                    else:
                        rem = rem + 1
            if agregar_dato:
                datos_1.append(datos_shuffle_1[i,:,:])
                datos_2.append(datos_shuffle_2[i,:,:])
                for s_SigRef in range(num_canales):
                    if s_SigRef < 3:
                        arreglo_x = np.array(list(range(3000)))
                        dato_df = pd.DataFrame(datos_shuffle_1[i,:,s_SigRef])
                        descriptores_1[9*s_SigRef].append(dato_df.mean()[0])
                        descriptores_1[9*s_SigRef + 1].append(dato_df.var()[0])
                        descriptores_1[9*s_SigRef + 2].append(dato_df.kurt()[0])
                        descriptores_1[9*s_SigRef + 3].append(dato_df.skew()[0])
                        descriptores_1[9*s_SigRef + 4].append(len(pyaC.zerocross1d(x=arreglo_x, y=dato_df.values[:,0])))
                        # Frecuencia
                        dato_fft = sp.fftpack.fft(dato_df)
                        fftfreq = sp.fftpack.fftfreq(len(dato_fft), 1/100)
                        potencia_02 = 0
                        potencia_28 = 0
                        potencia_814 =  0
                        potencia_14inf = 0
                        for j in range(len(fftfreq)):
                            if abs(fftfreq[j]) <= 2:
                                potencia_02 = potencia_02 + abs(dato_fft[j][0])**2
                            elif abs(fftfreq[j]) < 8:
                                potencia_28 = potencia_28 + abs(dato_fft[j][0])**2
                            elif abs(fftfreq[j]) < 14:
                                potencia_814 = potencia_814 + abs(dato_fft[j][0])**2
                            else:
                                potencia_14inf = potencia_14inf + abs(dato_fft[j][0])**2
                        descriptores_1[9*s_SigRef + 5].append(potencia_02/2)
                        descriptores_1[9*s_SigRef + 6].append(potencia_28/6)
                        descriptores_1[9*s_SigRef + 7].append(potencia_814/6)
                        descriptores_1[9*s_SigRef + 8].append(potencia_14inf/(max(fftfreq)-14))
                    else:
                        arreglo_x = np.array(list(range(30)))
                        dato_df = pd.DataFrame(datos_shuffle_2[i,:,(s_SigRef-3)])
                        descriptores_2[6*(s_SigRef-3)].append(dato_df.mean()[0])
                        descriptores_2[6*(s_SigRef-3) + 1].append(dato_df.var()[0])
                        descriptores_2[6*(s_SigRef-3) + 2].append(dato_df.kurt()[0])
                        descriptores_2[6*(s_SigRef-3) + 3].append(dato_df.skew()[0])
                        descriptores_2[6*(s_SigRef-3) + 4].append(len(pyaC.zerocross1d(x=arreglo_x, y=dato_df.values[:,0])))
                        # Frecuencia
                        dato_fft = sp.fftpack.fft(dato_df)
                        fftfreq = sp.fftpack.fftfreq(len(dato_fft), 1)
                        potencia = 0
                        for j in range(len(fftfreq)):
                            potencia = potencia + abs(dato_fft[j][0])**2
                        descriptores_2[6*(s_SigRef-3) + 5].append(potencia/max(fftfreq))

#%%
despierto = 0
ligero = 0
profundo = 0
rem = 0
no_se_sabe = 0
for etiqueta in etiquetas:
    if etiqueta == 0:
        # Depierto = 0
        despierto = despierto + 1
    elif etiqueta == 1:
        # Ligero = 1
        ligero = ligero + 1
    elif etiqueta == 2:
        # Profundo = 2
        profundo = profundo + 1
    elif etiqueta == 3:
        # REM = 3
        rem = rem + 1
    else:
        no_se_sabe = no_se_sabe + 1
#%%
datos_1_np = np.array(datos_1)
datos_2_np = np.array(datos_2)
descriptores_1_np = np.array(descriptores_1).transpose()
descriptores_2_np = np.array(descriptores_2).transpose()
etiquetas_np = np.array(etiquetas)

#%%
np.save('./datos_1.npy', datos_1_np)
np.save('./datos_2.npy', datos_2_np)
np.save('./descriptores_1.npy', descriptores_1_np)
np.save('./descriptores_2.npy', descriptores_2_np)
np.save('./etiquetas.npy', etiquetas_np)


import os
import sys
import argparse
import logging
from pyspark.sql.functions import col, when
from pyspark.sql import SparkSession
from table_mappings import get_schema_struct, get_column_name_diff, get_primary_key

def _get_config(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--host', dest='host', type=str)
    parser.add_argument('--port', dest='port', type=int)
    parser.add_argument('--username', dest='username', type=str)
    parser.add_argument('--password', dest='password', type=str)
    parser.add_argument('--database', dest='database', type=str)
    parser.add_argument('--table', dest='table', type=str)
    parser.add_argument('--source', dest='source', type=str)
    parser.add_argument('--delimiter', dest='delimiter', type=str, default=',')
    config, _ = parser.parse_known_args(argv)

    return config

def _get_source_path(source):
    """Build absolute source file path

    :param source Source path
    """
    current_path = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(current_path, source)

def _write_to_db(data, config):
    return  data.write \
                .format("jdbc") \
                .option("url", f"jdbc:postgresql://{config.host}:{config.port}/{config.database}") \
                .option("dbtable", config.table) \
                .option("user", config.username) \
                .option("password", config.password) \
                .option("driver", "org.postgresql.Driver") \
                .mode('append') \
                .save()

def _map_columns(data, schema):
    return data.select([col(c).alias(schema.get(c, c)) for c in data.columns])

def _read_csv(spark, source_path, schema, delimiter=','):
    return spark.read \
                .format("csv") \
                .option("header", True) \
                .option("delimiter", delimiter) \
                .schema(schema) \
                .load(source_path) \
                .na.replace('NA', None)

def main(argv, spark):
    try:
        config = _get_config(argv)
        source_path = _get_source_path(config.source)

        raw_data = _read_csv(spark, source_path, get_schema_struct(config.table))
        mapped_data = _map_columns(raw_data, get_column_name_diff(config.table))

        if config.table == "flights":
            mapped_data = mapped_data.drop("index")
            #mapped_data_with_delay = mapped_data.withColumn("is_delayed", when((col("dep_delay") > 15), True).otherwise(False))

        return _write_to_db(mapped_data.dropDuplicates(get_primary_key(config.table)), config)

    except Exception as e:
        return logging.exception(e)

if __name__ == '__main__':
    spark = SparkSession \
        .builder \
        .master("local") \
        .config("spark.jars", "C:\spark\spark-3.4.0-bin-hadoop3\jars\postgresql-42.6.0.jar") \
        .appName("load_dw") \
        .getOrCreate()

    logging.getLogger().setLevel(logging.INFO)
    main(sys.argv, spark)


import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
from tensorflow import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

imdb,info = tfds.load("imdb_reviews",with_info=True, as_supervised=True)

train_data,test_data = imdb['train'],imdb['test']
training_sentences=[]
training_labels=[]
test_sentences=[]
test_labels=[]

for s,l in train_data:
   training_sentences.append(str(s.numpy()))
   training_labels.append(l.numpy())
for s,l in test_data:
   test_sentences.append(str(s.numpy()))
   test_labels.append(l.numpy())
training_labels_final = np.array(training_labels)
test_labels_final = np.array(test_labels)

vocab_size = 10000
embedding_dim = 16
max_length = 120
trunc_type = 'post'
oov_tok = "<OOV>"

tokenizer = Tokenizer(num_words=vocab_size,oov_token=oov_tok)
tokenizer.fit_on_texts(training_sentences)
word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(training_sentences)
padded = pad_sequences(sequences,maxlen=max_length,truncating=trunc_type)

testing_sequences = tokenizer.texts_to_sequences(test_sentences)
testing_padded = pad_sequences(testing_sequences,maxlen=max_length,truncating=trunc_type)

reverse_word_index = dict([(value,key) for (key,value) in word_index.items()])
def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])
# print(decode_review(padded[1]))
# print(training_sentences[1])

model = tf.keras.Sequential([
    keras.layers.Embedding(vocab_size,embedding_dim,input_length=max_length),
    keras.layers.Flatten(),
    keras.layers.Dense(6, activation='relu'),
    keras.layers.Dense(1,activation='sigmoid')
])

model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
model.summary()

num_epochs = 10
model.fit(padded,training_labels_final,epochs=num_epochs,validation_data=(testing_padded,test_labels_final))

e = model.layers[0]
weights = e.get_weights()[0]

import io
out_v = io.open('vecs.tsv' , 'w' , encoding='utf-8')
out_m = io.open('meta.tsv' , 'w' , encoding='utf-8')
for word_num in range(1,vocab_size):
    word=reverse_word_index[word_num]
    embeddings=weights[word_num]
    out_m.write(word+'\n')
    out_v.write('\t'.join([str(x) for x in embeddings]) + '\n')
out_v.close()
out_m.close()


# In[1]:

import os
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import pdb
from PIL import Image
# Root directory of the project
ROOT_DIR = os.path.abspath("../")
# Root directory of the dataset
DATA_DIR = '../images/trash_test/'
DATA_NUM = 40

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log

# get_ipython().run_line_magic('matplotlib', 'inline')

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# ## Configurations

# In[2]:

class TrashConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "objects"

    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
    IMAGES_PER_GPU = 32

    # Number of classes (including background)
    NUM_CLASSES = 1 + 2  # background + 3 shapes

    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 256
    IMAGE_MAX_DIM = 256

    # Use smaller anchors because our image and objects are small
    RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)  # anchor side in pixels

    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE = 32

    # Use a small epoch since the data is simple
    STEPS_PER_EPOCH = 100

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 5

config = TrashConfig()
config.display()

# ## Notebook Preferences

# In[3]:

def get_ax(rows=1, cols=1, size=8):
    """Return a Matplotlib Axes array to be used in
    all visualizations in the notebook. Provide a
    central point to control graph sizes.

    Change the default size attribute to control the size
    of rendered images
    """
    _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
    return ax

# In[4]:

# ## preprocess the ground truth label
'''
file_clothes = open(DATA_DIR+'clothes.txt','r')
data_clothes = []
for i in range(DATA_NUM):
    data_clothes.append([int(j) for j in file_clothes.readline().split()])

file_paperball = open(DATA_DIR+'paperball.txt','r')
data_paperball = []
for i in range(DATA_NUM):
    data_paperball.append([int(j) for j in file_paperball.readline().split()])
'''

data_clothes = []
for i in range(DATA_NUM):
    data_clothes.append([0, 0, 0, 0])

data_paperball = []
for i in range(DATA_NUM):
    data_paperball.append([0, 0, 0, 0])

# ## Dataset
#
# Create a synthetic dataset
#
# Extend the Dataset class and add a method to load the shapes dataset, `load_objects()`, and override the following methods:
#
# * load_image()
# * load_mask()
# * image_reference()

# In[4]:

class TrashDataset(utils.Dataset):
    """Generates the shapes synthetic dataset. The dataset consists of simple
    shapes (triangles, squares, circles) placed randomly on a blank surface.
    The images are generated on the fly. No file access required.
    """

    def load_objects(self, start, end, height, width):
        """Generate the requested number of synthetic images.
        count: number of images to generate.
        height, width: the size of the generated images.
        """
        # Add classes
        self.add_class("objects", 1, "clothes")
        self.add_class("objects", 2, "paperball")

        # Add images
        # Generate random specifications of images (i.e. color and
        # list of shapes sizes and locations). This is more compact than
        # actual images. Images are generated on the fly in load_image().

        for i in range(start,end):
            objects = []
            data_clothes_line = data_clothes[i]
            data_paperball_line = data_paperball[i]
            for k in range(len(data_clothes_line)//4):
                objects.append(('clothes', tuple(data_clothes_line[4*k:4*(k+1)])))
            for k in range(len(data_paperball_line)//4):
                objects.append(('paperball', tuple(data_paperball_line[4*k:4*(k+1)])))

            # bg_color, shapes = self.random_image(height, width)
            self.add_image("objects", image_id=i, path=DATA_DIR + str(i) + '.png',
                           width=width, height=height, objects=objects)

    def load_image(self, image_id):
        assert image_id < len(self.image_info)
        image_path = self.image_info[image_id]['path']
        image = Image.open(image_path)
        return np.array(image)

    def image_reference(self, image_id):
        """Return the shapes data of the image."""
        info = self.image_info[image_id]
        if info["source"] == "shapes":
            return info["shapes"]
        else:
            super(self.__class__).image_reference(self, image_id)

    def load_mask(self, image_id):
        """Generate instance masks for shapes of the given image ID.
        """
        info = self.image_info[image_id]
        objects = info['objects']
        count = len(objects)

        mask = np.zeros([info['height'], info['width'], count], dtype=np.uint8)
        for i, (shape, pos) in enumerate(info['objects']):
            x1,y1,width,height = pos
            mask[y1:y1+height, x1:x1+width, i] = 1
        # Handle occlusions
        # occlusion = np.logical_not(mask[:, :, -1]).astype(np.uint8)
        # for i in range(count-2, -1, -1):
            # mask[:, :, i] = mask[:, :, i] * occlusion
            # occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
        # Map class names to class IDs.
        class_ids = np.array([self.class_names.index(s[0]) for s in objects])
        return mask.astype(np.bool), class_ids.astype(np.int32)

    def draw_shape(self, image, shape, dims, color):
        """Draws a shape from the given specs."""
        # Get the center x, y and the size s
        x, y, s = dims
        if shape == 'square':
            cv2.rectangle(image, (x-s, y-s), (x+s, y+s), color, -1)
        elif shape == "circle":
            cv2.circle(image, (x, y), s, color, -1)
        elif shape == "triangle":
            points = np.array([[(x, y-s),
                                (x-s/math.sin(math.radians(60)), y+s),
                                (x+s/math.sin(math.radians(60)), y+s),
                                ]], dtype=np.int32)
            cv2.fillPoly(image, points, color)
        return image

    def random_shape(self, height, width):
        """Generates specifications of a random shape that lies within
        the given height and width boundaries.
        Returns a tuple of three valus:
        * The shape name (square, circle, ...)
        * Shape color: a tuple of 3 values, RGB.
        * Shape dimensions: A tuple of values that define the shape size
                            and location. Differs per shape type.
        """
        # Shape
        shape = random.choice(["square", "circle", "triangle"])
        # Color
        color = tuple([random.randint(0, 255) for _ in range(3)])
        # Center x, y
        buffer = 20
        y = random.randint(buffer, height - buffer - 1)
        x = random.randint(buffer, width - buffer - 1)
        # Size
        s = random.randint(buffer, height//4)
        return shape, color, (x, y, s)

    def random_image(self, height, width):
        """Creates random specifications of an image with multiple shapes.
        Returns the background color of the image and a list of shape
        specifications that can be used to draw the image.
        """
        # Pick random background color
        bg_color = np.array([random.randint(0, 255) for _ in range(3)])
        # Generate a few random shapes and record their
        # bounding boxes
        shapes = []
        boxes = []
        N = random.randint(1, 4)
        for _ in range(N):
            shape, color, dims = self.random_shape(height, width)
            shapes.append((shape, color, dims))
            x, y, s = dims
            boxes.append([y-s, x-s, y+s, x+s])
        # Apply non-max suppression wit 0.3 threshold to avoid
        # shapes covering each other
        keep_ixs = utils.non_max_suppression(np.array(boxes), np.arange(N), 0.3)
        shapes = [s for i, s in enumerate(shapes) if i in keep_ixs]
        return bg_color, shapes

# In[5]:
# Test dataset
dataset_test = TrashDataset()
dataset_test.load_objects(0, DATA_NUM, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
dataset_test.prepare()

# ## Detection

# In[11]:
class InferenceConfig(TrashConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 32

inference_config = InferenceConfig()

# Recreate the model in inference mode
model = modellib.MaskRCNN(mode="inference",
                          config=inference_config,
                          model_dir=MODEL_DIR)

model_path = model.find_last()

# Load trained weights
print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)

# In[12]:
for pos_ids in range(5):
    # Test on random images
    original_images = []
    image_ids = range(8)
    for image_id in image_ids:
        original_image, image_meta, gt_class_id, gt_bbox, gt_mask =    modellib.load_image_gt(dataset_test, inference_config,
                                   image_id+pos_ids*8, use_mini_mask=False)

        for j in range(4):
            original_images.append(original_image)

        '''
        log("original_image", original_image)
        log("image_meta", image_meta)
        log("gt_class_id", gt_class_id)
        log("gt_bbox", gt_bbox)
        log("gt_mask", gt_mask)

        visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id,
                                    dataset_train.class_names, figsize=(8, 8))
        '''

    results = model.detect(original_images, verbose=1)

    for i in range(8):
        image_id = i * 4
        r = results[image_id]
        img_save_path = DATA_DIR+'pos_'+str(pos_ids)+'_'+str(i)+'_out.png'
        visualize.display_instances(original_images[image_id], r['rois'], r['masks'], r['class_ids'],
                                    dataset_test.class_names, r['scores'], ax=get_ax(), img_save_path=img_save_path)


import xlsxwriter
from tkinter import *

from openpyxl import load_workbook

myFileName = 'decmo.xlsx'
# load the workbook, and put the sheet into a variable
wb = load_workbook(filename=myFileName)

newRowLocation = 1

global index
index = 1

def functionate():
    v = e.get()
    vv = f.get()
    d = date.get()
    m = month.get()
    find = d + '##'
    print(find)
    x = 0
    y = 0
    c = 0
    ws = wb['january']
    if m == '1':
        ws = wb['january']
    if m == '2':
        ws = wb['february']
    if m == '3':
        ws = wb['march']
    if m == '4':
        ws = wb['april']
    if m == '5':
        ws = wb['may']
    if m == '6':
        ws = wb['june']
    if m == '7':
        ws = wb['july']
    if m == '8':
        ws = wb['august']
    if m == '9':
        ws = wb['september']
    if m == '10':
        ws = wb['october']
    if m == '11':
        ws = wb['november']
    if m == '12':
        ws = wb['december']

    global index

    for row in ws.iter_rows(min_row=1, min_col=1, max_row=30, max_col=2):
        for cell in row:
            if cell.value == find:
                print('found')
                x = cell.column
                y = cell.row
                z = cell.coordinate
                income_obj = ws.cell(row=y + 1, column=x + 1)
                cost_obj = ws.cell(row=y + 1, column=x + 2)
                c = income_obj.value
                cost=cost_obj.value

    z = ws.max_column

    ws.cell(column=c + x + 3, row=y + 1, value=v)
    ws.cell(column=c + x + 4, row=y + 1, value=vv)
    total=float(cost)+float(vv)
    ws.cell(column=x + 1, row=y + 1, value=c + 2)
    ws.cell(column=x + 2, row=y+1, value=total)

    print(c)
    wb.save(filename=myFileName)
    wb.close()

root = Tk()
root.geometry('500x500')
e = Entry(root)
b = Button(root, text='Click', command=functionate)
e.pack()
b.pack()
e.place(x=0, y=0)
b.place(x=50, y=50)

f = Entry(root)
f.pack()
date = Entry(root)
date.pack()
date.place(x=100, y=100)
month = Entry(root)
month.pack()
month.place(x=100, y=150)

print(4)

root.mainloop()


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import fingerfood as fd
from os import remove, path

# testing texts
text_01 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis finibus metus quam, eget pulvinar ligula viverra vitae. Duis egestas vitae diam ac sodales. Mauris mattis lacinia sodales. Aliquam vitae finibus tortor. Aliquam erat volutpat. Duis et ex aliquam, ultrices ipsum non, malesuada tortor. Pellentesque bibendum elit dolor, quis malesuada turpis viverra sit amet. Ut pretium aliquam vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean vel convallis arcu. Sed non feugiat mi. Fusce vulputate dui vel metus tincidunt blandit. Sed finibus faucibus mi, quis dictum nulla tempus non. Ut ultricies sagittis laoreet."

text_02 = "Aenean consectetur turpis et gravida pharetra. Mauris vitae faucibus orci. Curabitur lobortis arcu id iaculis scelerisque. Morbi magna nunc, blandit quis congue eu, ullamcorper ac odio. Nullam luctus semper tristique. Integer vel rhoncus odio, nec fermentum libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in nibh ante. In gravida vitae augue sed rhoncus. Curabitur lobortis aliquam faucibus. Vestibulum pharetra orci finibus viverra lacinia. Suspendisse quis varius enim."

def test_hashing():

    # normal hash
    hh0 = fd.HashedString.normal('Lorem ipsum dolor sit amet')
    assert hh0.get_hash_string() == '74.65.6d.61.20.74.69.73.20.72.6f.6c.6f.64.20.6d.75.73.70.69.20.6d.65.72.6f.4c'

    # rolling vs normal hash
    hh1 = fd.HashedString.rolling('orem ipsum dolor sit amet,',hh0.get_hash_list())
    hh2 = fd.HashedString.normal('orem ipsum dolor sit amet,')

    assert hh1.get_hash_string() == hh2.get_hash_string()

def test_comparing():

    fp1 = fd.Fingerprint(text_01,'text_01',kgram_len=12,detection_thr=30)
    fp2 = fd.Fingerprint(text_02,'text_02',kgram_len=12,detection_thr=30)

    # compare Fingerprint objects
    m = fp1.compare_with(fp2)
    assert m
    assert m[0].get('pos') == 16
    assert m[0].get('substr') == 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '

    # compare Fingerprint obj with string
    m = fp1.compare_with(text_02)
    assert m
    assert m[0].get('pos') == 16
    assert m[0].get('substr') == 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '

def test_db_io():

    fp = fd.Fingerprint(text_01,'text_01')

    # remove existing database
    if path.isfile('test.db'):
        remove('test.db')

    # store data to db
    res = fp.to_sql('test.db')
    assert res

    # load data from db
    stored_text= fd.sql.get_text('test.db','text_01')
    stored_fps = fd.sql.get_fingerprints('test.db','text_01')

    # store same data to db
    res = fp.to_sql('test.db',if_exists='replace')
    assert res

    # remove db
    remove('test.db')

    # check content
    assert stored_text == text_01
    assert len(stored_fps[0]) == len(fp.fingerprints[0])

    for el in fp.fingerprints[0]:
        assert el in stored_fps[0]
    for el in fp.fingerprints[1]:
        assert el in stored_fps[1]

def test_db_match():

    fp1 = fd.Fingerprint(text_01,'text_01')
    fp2 = fd.Fingerprint(text_02,'text_02')

    # remove existing database
    if path.isfile('test.db'):
        remove('test.db')

    # store data to db
    fp1.to_sql('test.db','replace')

    m = fp2.search_in_db('test.db')

    # remove db
    remove('test.db')

    assert m
    assert m[0].get('pos') == 279
    assert m[0].get('substr') == 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '

if __name__ == '__main__':

    print('starting tests...')

    # testing fingerprints
    test_hashing()

    # testing text compare
    test_comparing()

    # testing sql storing
    test_db_io()

    # testing db search
    test_db_match()

    print('success!')


""" This script is an example of benchmarking the continuous mlp baseline."""
import datetime
import os
import os.path as osp
import random

from baselines.bench import benchmarks
import dowel
from dowel import logger as dowel_logger
import gym
import pytest
import tensorflow as tf

from metarl.envs import normalize
from metarl.experiment import deterministic
from metarl.tf.algos import PPO
from metarl.tf.baselines import ContinuousMLPBaseline
from metarl.tf.envs import TfEnv
from metarl.tf.experiment import LocalTFRunner
from metarl.tf.policies import GaussianLSTMPolicy
from tests.fixtures import snapshot_config

policy_params = {
    'policy_lr': 1e-3,
    'policy_hidden_sizes': 32,
    'hidden_nonlinearity': tf.nn.tanh
}

baseline_params = {'regressor_args': dict(hidden_sizes=(64, 64))}

algo_params = {
    'n_envs':
    8,
    'n_epochs':
    20,
    'n_rollout_steps':
    2048,
    'discount':
    0.99,
    'max_path_length':
    100,
    'gae_lambda':
    0.95,
    'lr_clip_range':
    0.2,
    'policy_ent_coeff':
    0.02,
    'entropy_method':
    'max',
    'optimizer_args':
    dict(
        batch_size=32,
        max_epochs=10,
        tf_optimizer_args=dict(learning_rate=policy_params['policy_lr']),
    ),
    'center_adv':
    False
}

# number of processing elements to use for tensorflow
num_proc = 4 * 2
# number of trials to run per environment
num_trials = 3

@pytest.mark.huge
def test_benchmark_ppo_continuous_mlp_baseline():
    """ Compare benchmarks between CMB and potentially other baselines."""
    mujoco1m = benchmarks.get_benchmark('Mujoco1M')

    timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
    benchmark_dir = osp.join(os.getcwd(), 'data', 'local', 'benchmarks',
                             'ppo_cmb', timestamp)
    for task in mujoco1m['tasks']:
        env_id = task['env_id']
        env = gym.make(env_id)

        seeds = random.sample(range(100), num_trials)

        task_dir = osp.join(benchmark_dir, env_id)
        cmb_csvs = []
        for trial in range(num_trials):
            seed = seeds[trial]

            trial_dir = task_dir + '/trial_%d_seed_%d' % (trial + 1, seed)
            cmb_dir = trial_dir + '/continuous_mlp_baseline'

            with tf.Graph().as_default():
                env.reset()
                cmb_csv = ppo_cmb(env, seed, cmb_dir)
            cmb_csvs.append(cmb_csv)

        env.close()

def ppo_cmb(env, seed, log_dir):
    """Create test continuous mlp baseline on ppo.

    Args:
        env (gym_env): Environment of the task.
        seed (int): Random seed for the trial.
        log_dir (str): Log dir path.

    Returns:
        str: training results in csv format.

    """
    deterministic.set_seed(seed)
    config = tf.ConfigProto(allow_soft_placement=True,
                            intra_op_parallelism_threads=num_proc,
                            inter_op_parallelism_threads=num_proc)
    sess = tf.Session(config=config)
    with LocalTFRunner(snapshot_config, sess=sess,
                       max_cpus=num_proc) as runner:
        env = TfEnv(normalize(env))

        policy = GaussianLSTMPolicy(
            env_spec=env.spec,
            hidden_dim=policy_params['policy_hidden_sizes'],
            hidden_nonlinearity=policy_params['hidden_nonlinearity'],
        )

        baseline = ContinuousMLPBaseline(
            env_spec=env.spec,
            regressor_args=baseline_params['regressor_args'],
        )

        algo = PPO(env_spec=env.spec,
                   policy=policy,
                   baseline=baseline,
                   max_path_length=algo_params['max_path_length'],
                   discount=algo_params['discount'],
                   gae_lambda=algo_params['gae_lambda'],
                   lr_clip_range=algo_params['lr_clip_range'],
                   entropy_method=algo_params['entropy_method'],
                   policy_ent_coeff=algo_params['policy_ent_coeff'],
                   optimizer_args=algo_params['optimizer_args'],
                   center_adv=algo_params['center_adv'],
                   stop_entropy_gradient=True)

        # Set up logger since we are not using run_experiment
        tabular_log_file = osp.join(log_dir, 'progress.csv')
        dowel_logger.add_output(dowel.StdOutput())
        dowel_logger.add_output(dowel.CsvOutput(tabular_log_file))
        dowel_logger.add_output(dowel.TensorBoardOutput(log_dir))

        runner.setup(algo,
                     env,
                     sampler_args=dict(n_envs=algo_params['n_envs']))
        runner.train(n_epochs=algo_params['n_epochs'],
                     batch_size=algo_params['n_rollout_steps'])

        dowel_logger.remove_all()

        return tabular_log_file


'''
Created on 2018年11月21日

@author: Jacky
'''
from random import randint
from collections import Counter
from pip._vendor.msgpack.fallback import xrange
import re

data = [randint(60,100) for _ in xrange(30)]

dt1 = dict.fromkeys(data, 0)
print(dt1)
for x in data:
    dt1[x]+=1
print(dt1)

lst = sorted(dt1.items(),key = lambda x:x[1],reverse=True)
print(lst)

cnt1 = Counter(data)
print(cnt1)
print (cnt1.most_common(3))

txt = open('new22.txt').read()
lst2 = re.split('\W+', txt)
cnt2 = Counter(lst2)
print(lst2)
print(cnt2.most_common(20))


# Mappings for the FHIR class generator.
#
# This should be useable as-is for Python classes.

# Which class names to map to resources and elements
classmap = {
    'Any': 'Resource',
    'Practitioner.role': 'PractRole',   # to avoid Practinioner.role and PractitionerRole generating the same class

    'boolean': 'bool',
    'integer': 'int',
    'positiveInt': 'int',
    'unsignedInt': 'int',
    'date': 'FHIRDate',
    'dateTime': 'FHIRDate',
    'instant': 'FHIRDate',
    'time': 'FHIRDate',
    'decimal': 'float',

    'string': 'str',
    'markdown': 'str',
    'id': 'str',
    'code': 'str',      # for now we're not generating enums for these
    'uri': 'str',
    'url': 'str',
    'canonical': 'str',
    'oid': 'str',
    'uuid': 'str',
    'xhtml': 'str',
    'base64Binary': 'str',
}

# Classes to be replaced with different ones at resource rendering time
replacemap = {
    'Reference': 'FHIRReference',     # `FHIRReference` adds dereferencing capabilities
}

# Which class names are native to the language (or can be treated this way)
natives = ['bool', 'int', 'float', 'str', 'dict']

# Which classes are to be expected from JSON decoding
jsonmap = {
    'str': 'str',
    'int': 'int',
    'bool': 'bool',
    'float': 'float',

    'FHIRDate': 'str',
}
jsonmap_default = 'dict'

# Properties that need to be renamed because of language keyword conflicts
reservedmap = {
    'for': 'for_fhir',
    'from': 'from_fhir',
    'class': 'class_fhir',
    'import': 'import_fhir',
    'global': 'global_fhir',
    'assert': 'assert_fhir',
    'except': 'except_fhir',
}

# For enum codes where a computer just cannot generate reasonable names
enum_map = {
    '=': 'eq',
    '!=': 'ne',
    '<': 'lt',
    '<=': 'lte',
    '>': 'gt',
    '>=': 'gte',
    '*': 'max',
}

# If you want to give specific names to enums based on their URI
enum_namemap = {
    # 'http://hl7.org/fhir/coverage-exception': 'CoverageExceptionCodes',
}

# If certain CodeSystems don't need to generate an enum
enum_ignore = {
    # 'http://hl7.org/fhir/resource-type-link',
}


def make_counter():
    """Return a counter function.

    >>> c = make_counter()
    >>> c('a')
    1
    >>> c('a')
    2
    >>> c('b')
    1
    >>> c('a')
    3
    >>> c2 = make_counter()
    >>> c2('b')
    1
    >>> c2('b')
    2
    >>> c('b') + c2('b')
    5
    """
    "*** YOUR CODE HERE ***"
    s={}
    def counter(i):
        nonlocal s
        if not i in s:
            s[i]=1
        else:
            s[i]+=1
        return s[i]
    return counter

def make_fib():
    """Returns a function that returns the next Fibonacci number
    every time it is called.

    >>> fib = make_fib()
    >>> fib()
    0
    >>> fib()
    1
    >>> fib()
    1
    >>> fib()
    2
    >>> fib()
    3
    >>> fib2 = make_fib()
    >>> fib() + sum([fib2() for _ in range(5)])
    12
    """
    "*** YOUR CODE HERE ***"
    pre,now,called=0, 0, 0
    def fibo():
        nonlocal pre, now, called
        if called==0:
            called+=1
            return 0
        elif called==1:
            called+=1
            now = 1
            return 1
        else:
            pre, now= now, pre+now
            return now
    return fibo

class Account:
    """An account has a balance and a holder.

    >>> a = Account('John')
    >>> a.deposit(10)
    10
    >>> a.balance
    10
    >>> a.interest
    0.02

    >>> a.time_to_retire(10.25) # 10 -> 10.2 -> 10.404
    2
    >>> a.balance               # balance should not change
    10
    >>> a.time_to_retire(11)    # 10 -> 10.2 -> ... -> 11.040808032
    5
    >>> a.time_to_retire(100)
    117
    """

    interest = 0.02  # A class attribute

    def __init__(self, account_holder):
        self.holder = account_holder
        self.balance = 0

    def deposit(self, amount):
        """Add amount to balance."""
        self.balance = self.balance + amount
        return self.balance

    def withdraw(self, amount):
        """Subtract amount from balance if funds are available."""
        if amount > self.balance:
            return 'Insufficient funds'
        self.balance = self.balance - amount
        return self.balance

    def time_to_retire(self, amount):
        """Return the number of years until balance would grow to amount."""
        assert self.balance > 0 and amount > 0 and self.interest > 0
        "*** YOUR CODE HERE ***"
        ans=0
        my_now=self.balance
        while amount>my_now:
            ans+=1
            my_now=my_now*(1+self.interest)
        return ans

class FreeChecking(Account):
    """A bank account that charges for withdrawals, but the first two are free!

    >>> ch = FreeChecking('Jack')
    >>> ch.balance = 20
    >>> ch.withdraw(3)    # First one's free
    17
    >>> ch.withdraw(100)  # And the second
    'Insufficient funds'
    >>> ch.balance
    17
    >>> ch.withdraw(3)    # Ok, two free withdrawals is enough
    13
    >>> ch.withdraw(3)
    9
    >>> ch2 = FreeChecking('John')
    >>> ch2.balance = 10
    >>> ch2.withdraw(3) # No fee
    7
    >>> ch.withdraw(3)  # ch still charges a fee
    5
    """
    withdraw_fee = 1
    free_withdrawals = 2

    "*** YOUR CODE HERE ***"
    def withdraw(self, amount):
        if self.free_withdrawals>0:
            self.free_withdrawals -= 1
            return Account.withdraw(self,amount)
        else:
            return Account.withdraw(self,amount+self.withdraw_fee)


# coding: utf-8

# # Assignment 2
#
# Before working on this assignment please read these instructions fully. In the submission area, you will notice that
# you can click the link to **Preview the Grading** for each step of the assignment. This is the criteria that will be used
# for peer grading. Please familiarize yourself with the criteria before beginning the assignment.
#
# An NOAA dataset has been stored in the file `data/C2A2_data/BinnedCsvs_d100/2f0bb04162655f0cba429b865292f31482e817e0b3ee9da0f40185d7.csv`.
# The data for this assignment comes from a subset of The National Centers for Environmental Information (NCEI)
# [Daily Global Historical Climatology Network](https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt)
# (GHCN-Daily). The GHCN-Daily is comprised of daily climate records from thousands of land surface stations across the globe.
#
# Each row in the assignment datafile corresponds to a single observation.
#
# The following variables are provided to you:
#
# * **id** : station identification code
# * **date** : date in YYYY-MM-DD format (e.g. 2012-01-24 = January 24, 2012)
# * **element** : indicator of element type
#     * TMAX : Maximum temperature (tenths of degrees C)
#     * TMIN : Minimum temperature (tenths of degrees C)
# * **value** : data value for element (tenths of degrees C)
#
# For this assignment, you must:
#
# 1. Read the documentation and familiarize yourself with the dataset, then write some python code which returns a line
# graph of the record high and record low temperatures by day of the year over the period 2005-2014. The area between
# the record high and record low temperatures for each day should be shaded.
# 2. Overlay a scatter of the 2015 data for any points (highs and lows) for which the ten year record (2005-2014) record
# high or record low was broken in 2015.
# 3. Watch out for leap days (i.e. February 29th), it is reasonable to remove these points from the dataset for
# the purpose of this visualization.
# 4. Make the visual nice! Leverage principles from the first module in this course when developing your solution.
# Consider issues such as legends, labels, and chart junk.
#
# The data you have been given is near **None, None, United Kingdom**, and the stations the data comes from are shown on the map below.

# In[33]:

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

#df_temp - Opens file and make it easy to read
df_temp = pd.read_csv('Dataset.csv') #open the data file
df_temp['Temp']=df_temp['Data_Value']*0.1 #Creates a column with temperatures in °C
df_temp1 = df_temp.loc[df_temp['ID'] == 'UKM00003740'] #Select only one location
df_temp1 = df_temp1.sort_values(['Date','Element']) #Sorts the data by date
#df_temp - Creates columns for Day and Month of the year
df_temp1['Date'] = pd.to_datetime(df_temp1['Date']) #Converts argument to datetime
df_temp1['Month'] = df_temp1['Date'].dt.strftime('%b')#Creates a column "Month" with the month taken by "Date"
df_temp1['Day'] = df_temp1['Date'].dt.day #Creates a column "Day" with the day taken by "Date"
df_temp1['Year'] = df_temp1['Date'].dt.year #Creates a column "Year" with the year taken by "Date"
df_temp1.set_index(['Month','Day']).sum(level=[0,1]).reset_index() #Creates 2 indexes "Month" and "Day"

#df graph_2015 - Creates df filtered for data in 2015
graph_2015max = df_temp1[(df_temp1['Year']==2015) & (df_temp['Element']=='TMAX')] #Creates a df where there are only the temperature with Element = TMAX
graph_2015min = df_temp1[(df_temp1['Year']==2015) & (df_temp['Element']=='TMIN')] #Creates a df where there are only the temperature with Element = TMIN
graph_2015 = pd.merge(graph_2015max, graph_2015min, how='outer', on=['Date']) #Merges the 2 creates dfs "graph_2015max" and "graph:2015min" on the column "Date"
graph_2015 = graph_2015[['Date', 'Temp_x','Temp_y']] #Selects only 3 columns from the df
graph_2015 = graph_2015.sort_values(['Date']) #Sorts the df by Date
graph_2015 = graph_2015.rename(index=str, columns={"Temp_x": "2015_Tmax", "Temp_y": "2015_Tmin"})
graph_2015 = graph_2015.reset_index() #Resets index
graph_2015.drop("index", 1, inplace = True) #Drops the old index

#df graph_2015 - Creates columns for Day and Month of the year
graph_2015['Date'] = pd.to_datetime(graph_2015['Date']) #Converts argument to datetime
graph_2015['Month'] = graph_2015['Date'].dt.strftime('%b')#Creates a column "Month" with the month taken by "Date"
graph_2015['Day'] = graph_2015['Date'].dt.day #Creates a column "Day" with the day taken by "Date"

#df graph - Creates df filtered for data from 2005 until 2014 (Exclude the only other different year "2015")
graph = df_temp1.where(df_temp1['Year']!=2015).groupby(['Month','Day']).Temp.agg(['min','max']) #Groups by Month and by Day
                                                            # (removing the year 2014) and select the Max and Min Value from the column "Temp"
graph = graph.reset_index() #Resets the index
sorter = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec"] #Creates a list of months to be used to sort the months in the "graph" df
sorterIndex = dict(zip(sorter,range(len(sorter)))) #Creates a dictionary with months and number
                                    # {'Aug': 7, 'Apr': 3, 'Oct': 9, 'Jun': 5, 'Dec': 11, 'Mar': 2, 'Nov': 10, 'Jul': 6, 'Feb': 1, 'Sep': 8, 'May': 4, 'Jan': 0}
graph['Month_Rank'] = graph['Month'].map(sorterIndex) #Creates a new column 'Month_Rank' with a number associated with the month
graph.sort_values(['Month_Rank', 'Day'], ascending = [True, True], inplace = True) #Sorts 'graph' df according to 'Month_Rank' and Day'
graph.drop('Month_Rank', 1, inplace = True) #Drops 'Month_Rank' column
graph = graph.reset_index() #Resets index
graph.drop("index", 1, inplace = True) #Drops the old index

#df2 - Creates a new df which included the merged dfs graph and graph_2015
df2 = pd.merge(graph, graph_2015, how='outer', on=['Month','Day']) #Merges the 2 creates dfs "graph_2015max" and "graph:2015min" on the column "Date"
df2 = df2.drop(59) #Drops the 29th of February using the index value of 59
df2 = df2.reset_index() #Resets index
df2.drop("index", 1, inplace = True) #Drops the old index
df2 = df2[['Date','Month','Day','min','max','2015_Tmin','2015_Tmax']] #Reorders the columns
df2['outmax']=df2['2015_Tmax'].where(df2['2015_Tmax']>df2['max']) #Creates a new column that shows only temperature
                                                                # where the Tmax in 2015 was higher than the other years
df2['outmin']=df2['2015_Tmin'].where(df2['2015_Tmin']<df2['min']) #Creates a new column that shows only temperature
                                                                # where the Tmin in 2015 was lower than the other years

#df2 - Plots line Tmin 2005-2014 and Tmax 2005-2014
plt.plot(df2.index.values,df2['min'], label = 'Tmin 2005-2014') #Plots the min values using a line
plt.plot(df2.index.values,df2['max'], label = 'Tmax 2005-2014') #Plots the max values using a line
plt.gca().fill_between(range(len(df2)), df2['min'], df2['max'], facecolor='blue', alpha=0.25) #Fills the area between the linear data and exponential data

#df2 - Scatter of the 2015 data for any points (highs and lows) for which the ten year record (2005-2014) record high or record low was broken in 2015
plt.scatter(df2.index.values,df2['outmin'], c='blue', label = 'Record Tmin 2015') #Plots the min values using a line
plt.scatter(df2.index.values,df2['outmax'], c='red', label = 'Record Tmax 2015') #Plots the max values using a line

#Legend format
plt.legend(loc=2, frameon=False, fontsize =12)

#Tick format
fig = plt.figure(1) # Prepare the figure
plot = fig.add_subplot(111) # Defines a fake subplot that is in fact only the plot
plot.tick_params(axis='both', which='major', labelsize=12) #Changes the fontsize of minor ticks label

#Title format
plt.title('Daily climate records in United Kingdom', fontsize = 16, fontweight='bold')

#y and x-axis label format
plt.ylabel('Temperature /°C', fontsize = 14)
plt.xlabel('Month', fontsize = 14)
plt.gca().xaxis.set_label_coords(0.5,-0.1)

#Figure size
fig = plt.gcf()
fig.set_size_inches(12.5, 7)
pylab.xlim([0,365])

x_axis = df2.groupby(['Month']).size()
x_axis = x_axis.reset_index()
x_axis['Month_Rank'] = x_axis['Month'].map(sorterIndex) #Creates a new column 'Month_Rank' with a number associated with the month
x_axis.sort_values(['Month_Rank'], ascending = True, inplace = True) #Sorts 'graph' df according to 'Month_Rank' and Day'
x_axis.drop('Month_Rank', 1, inplace = True) #Drops 'Month_Rank' column
x_axis = x_axis.reset_index() #Resets index
x_axis.drop("index", 1, inplace = True) #Drops the old index
x_axis.rename(index=str, columns={0: "Days"}, inplace = True)

x_values = []
x=0
for i in x_axis['Days']:
    x = x+i
    x_values.append(x)

plt.xticks(x_values, sorter)
plt.gca().axes.get_xaxis().set_ticklabels([])

x=0
for i in x_values:
    plt.gca().text(i - 20, -13.7, sorter[x],fontsize=12)
    x = x+1

plt.show()


import re

from sql_interpreter.sql import SQL_REGEX_COLUMN, SQL_REGEX_COLUMN_ALIAS

class Column:
    def __init__(self, columns_str: str) -> None:
        if 'as' in columns_str.lower():
            patron = re.compile(SQL_REGEX_COLUMN_ALIAS, re.I)
            matcher = patron.search(columns_str)
            self.column = matcher.group('column')
            self.alias = matcher.group('alias')
        else:
            patron = re.compile(SQL_REGEX_COLUMN, re.I)
            matcher = patron.search(columns_str)
            self.column = matcher.group('column')
            self.alias = None

    def __str_(self):
        if self.alias is None:
            return f'{self.column}'
        else:
            return f'{self.column} as {self.alias}'

    def __repr__(self):
        if self.alias is None:
            return f'{self.column}'
        else:
            return f'{self.column} as {self.alias}'