# Python Packages
import json
from pprint import pprint

# Django Packages
from django.views import View
from django.http import JsonResponse
from django.core.paginator import Paginator
from django.forms.models import model_to_dict
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

# Custom App
from .models import (
    Job,
    Category,
    JobTracking,
)
from .forms import CategoryForm, JobForm, JobTrackingForm, JobEditForm

# 0. Static APIs (GET): http://127.0.0.1:8000/api/choices/
@method_decorator(csrf_exempt, name='dispatch')
class StaticChoiceView(View):
    def get(self, request):
        data = [
            {
                "gender": dict(Job.Gender.choices),
                "employment_status": dict(Job.EmploymentStatus.choices),
                "job_level": dict(Job.JobLevel.choices),
                "resume_receiving_option": dict(Job.ResumeReceivingOption.choices)
            }
        ]
        return JsonResponse({"data": data}, status=200)

# 1. Create Job (POST): http://127.0.0.1:8000/api/job/
# 2. Job List (GET)(QS): http://127.0.0.1:8000/api/job/?no_of_vacancies=14&category=1&employer_id=14&age=25&gender=M
# 3. Job Details (GET): http://127.0.0.1:8000/api/job/{id}/
# 4. Edit Job (PUT): http://127.0.0.1:8000/api/job/{id}/
# 5. Delete Job (DELETE): http://127.0.0.1:8000/api/job/{id}/
# 6. Apply Job (POST): http://127.0.0.1:8000/api/job/{id}/apply/
@method_decorator(csrf_exempt, name='dispatch')
class JobView(View):
    job_fields_with_id = [
        'id', 'job_title', 'job_level', 'job_responsibilities', 'job_location', 'no_of_vacancies', 'category',
        'employer_id', 'employer_name', 'employer_information', 'employment_status', 'employer_location',
        'age', 'gender', 'skill', 'experience', 'training', 'salary', 'compensation_and_other_benefits',
        'application_deadline', 'resume_receiving_option'
    ]
    job_tracking_fields = ['seeker_id', 'seeker_name', 'job']
    job_list_querystring = ['no_of_vacancies', 'category', 'employer_id', 'age', 'gender']

    # 1
    # 6
    def post(self, request, id=None):
        # 6. Apply Job
        if id:
            body_unicode = request.body.decode('utf-8')
            body = json.loads(body_unicode)
            body.update({"job": id})

            form = JobTrackingForm(body)

            if form.is_valid():
                instance = form.save()
                return JsonResponse(model_to_dict(instance, fields=self.job_tracking_fields), status=201)
            else:
                return JsonResponse({"errors": form.errors.as_json()}, status=422)

        # 1. Create Job
        else:
            body_unicode = request.body.decode('utf-8')
            body = json.loads(body_unicode)

            form = JobForm(body)

            if form.is_valid():
                instance = form.save()
                return JsonResponse(model_to_dict(instance, fields=self.job_fields_with_id), status=201)
            else:
                return JsonResponse({"errors": form.errors.as_json()}, status=422)

    # 2
    # 3
    def get(self, request, id=None, *args, **kwargs):
        jobs = Job.objects

        # 3. Job Details
        if id:
            query = jobs.get(id=id)
            return JsonResponse(model_to_dict(query, fields=self.job_fields_with_id), status=200)

        # 2. Job List (QueryString)
        else:
            data = {}
            fields = jobs.values(*self.job_fields_with_id)
            for field in self.job_list_querystring:
                if request.GET.get(field):
                    data.update({field: request.GET.get(field)})

            jobs = list(fields.filter(**data))

            # pagination
            paginator = Paginator(jobs, 4)
            page_number = request.GET.get('page')
            page_obj = paginator.get_page(page_number)
            context = {
                "data": list(page_obj),  # or, list(page_obj.object_list)
                "pagination": {
                    "total_pages": page_obj.paginator.num_pages,
                    "current_page_number": page_obj.number,
                    "previous": page_obj.has_previous() > 0 and page_obj.previous_page_number() or None,
                    "next": page_obj.has_next() > 0 and page_obj.next_page_number() or None,
                }
            }
            return JsonResponse(context)

    # 4
    def put(self, request, id=None):
        body_unicode = request.body.decode('utf-8')
        body = json.loads(body_unicode)

        job = Job.objects.get(id=id)

        form = JobEditForm(body, instance=job)

        if form.is_valid():
            instance = form.save()
            return JsonResponse(model_to_dict(instance, fields=[field.name for field in instance._meta.fields]))
        else:
            return JsonResponse({"errors": form.errors.as_json()}, status=422)

    # 5
    def delete(self, request, id=None):
        try:
            job = Job.objects.get(id=id)
            job.delete()
            return JsonResponse({"data": "deleted"}, status=200)
        except Job.DoesNotExist as e:
            return JsonResponse({"errors": f"{e}"}, status=204)

# 7. Create Category (POST): http://127.0.0.1:8000/api/category/
# 8. Category List (GET): http://127.0.0.1:8000/api/category/
# 9. Edit Category (PUT): http://127.0.0.1:8000/api/category/{id}/
# 10. Delete Category (DELETE): http://127.0.0.1:8000/api/category/{id}/
@method_decorator(csrf_exempt, name='dispatch')
class CategoryView(View):
    # 7
    def post(self, request):
        body_unicode = request.body.decode('utf-8')
        body = json.loads(body_unicode)

        form = CategoryForm(body)

        if form.is_valid():
            instance = form.save()
            return JsonResponse(model_to_dict(instance, fields=["name"]), status=201)
        else:
            return JsonResponse({"errors": form.errors.as_json()}, status=422)

    # 8
    def get(self, request):
        queryset = Category.objects.all()

        data = []

        for element in queryset:
            job_count = Job.objects.filter(category__id=element.id).count()

            data.append(
                {"id": element.id, "name": element.name, "job_count": job_count}
            )

        if data:
            return JsonResponse({"data": data}, status=200)
        else:
            return JsonResponse({"data": "not found"}, status=404)

    # 9
    def put(self, request, id=None):
        body_unicode = request.body.decode('utf-8')
        body = json.loads(body_unicode)

        category = Category.objects.get(id=id)

        form = CategoryForm(body, instance=category)

        if form.is_valid():
            instance = form.save()
            return JsonResponse(model_to_dict(instance, fields=["name"]), status=200)
        else:
            return JsonResponse({"errors": form.errors.as_json()}, status=422)

    # 10
    def delete(self, request, id=None):
        try:
            category = Category.objects.get(id=id)
            category.delete()
            return JsonResponse({"data": "deleted"}, status=200)
        except Category.DoesNotExist as e:
            return JsonResponse({"errors": f"{e}"}, status=204)

# 11. Applied Jobs by Seeker ID (GET): http://127.0.0.1:8000/api/job/seeker/{id}/
@method_decorator(csrf_exempt, name='dispatch')
class AppliedJobsBySeekerView(View):
    # 11
    def get(self, request, id=None):
        tracking_query = JobTracking.objects.filter(seeker_id=id)

        data = []

        for element in tracking_query:
            data.append(
                {"job_title": element.job.job_title}
            )

        if data:
            return JsonResponse({"data": data}, status=200)
        else:
            return JsonResponse({"data": "not found"}, status=404)

# 12. Posted Job List by Employer ID (GET): http://127.0.0.1:8000/api/job/employer/{id}/
@method_decorator(csrf_exempt, name='dispatch')
class PostedJobListByEmployerView(View):
    # 12
    def get(self, request, id=None):
        tracking_query = Job.objects.filter(employer_id=id)

        data = []

        for element in tracking_query:
            data.append(
                {"job_title": element.job_title}
            )

        if data:
            return JsonResponse({"data": data}, status=200)
        else:
            return JsonResponse({"data": "not found"}, status=404)

# 13. Job-wise Seeker List (GET): http://127.0.0.1:8000/api/job/{id}/seeker/
@method_decorator(csrf_exempt, name='dispatch')
class JobWiseSeekerListView(View):
    # 13
    def get(self, request, id=None):
        tracking_query = JobTracking.objects.filter(job=id)

        data = []

        for element in tracking_query:
            data.append(
                {"seeker_name": element.seeker_name}
            )

        if data:
            return JsonResponse({"data": data}, status=200)
        else:
            return JsonResponse({"data": "not found"}, status=404)


"""empty message

Revision ID: b5658f866fa4
Revises: a5a5e318846c
Create Date: 2021-06-23 01:56:29.504114

"""
from alembic import op
import sqlalchemy as sa
from forms import genres

# revision identifiers, used by Alembic.
revision = 'b5658f866fa4'
down_revision = 'a5a5e318846c'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('venues_genres')
    op.drop_table('artists_genres')
    op.add_column('artists', sa.Column('genres', sa.ARRAY(sa.String())))
    op.add_column('venues', sa.Column('genres', sa.ARRAY(sa.String())))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('venues', 'genres')
    op.drop_column('artists', 'genres')
    op.create_table(
        'artists_genres',
        sa.Column(
            'artist_id', sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.Column(
            'genre_id', sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.ForeignKeyConstraint(
            ['artist_id'],
            ['artists.id'],
            name='artists_genres_artist_id_fkey'
        ),
        sa.ForeignKeyConstraint(
            ['genre_id'], ['genres.id'], name='artists_genres_genre_id_fkey'
        ),
        sa.PrimaryKeyConstraint(
            'artist_id', 'genre_id', name='Artist_Genres_pkey'
        )
    )
    op.create_table(
        'venues_genres',
        sa.Column(
            'venue_id', sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.Column(
            'genre_id', sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.ForeignKeyConstraint(
            ['genre_id'], ['genres.id'], name='venues_genres_genre_id_fkey'
        ),
        sa.ForeignKeyConstraint(
            ['venue_id'], ['venues.id'], name='venues_genres_venue_id_fkey'
        ),
        sa.PrimaryKeyConstraint(
            'venue_id', 'genre_id', name='Venue_Genres_pkey'
        )
    )
    # ### end Alembic commands ###


import typer
from rich.style import Style
from rich.color import Color
from rich.console import Console
from rich.table import Table
from rich.text import Text
import boto3
from mypy_boto3_rekognition.type_defs import DominantColorTypeDef

import json

app = typer.Typer()

s3 = boto3.resource("s3")
rekognition = boto3.client("rekognition")

def stylize(color: DominantColorTypeDef) -> Style:
    color = Color.from_rgb(color["Red"], color["Green"], color["Blue"])
    return Style(color="black", bgcolor=color, bold=True)

@app.command(help="Get properties of images in a bucket.")
def main(
    bucket_name: str = typer.Argument(..., help="Bucket name"),
    prefix: str = typer.Argument("", help="Prefix to filter by"),
    json_out: bool = typer.Option(False, "--json", help="Output JSON"),
):
    bucket = s3.Bucket(bucket_name)
    console = Console()

    json_output = not console.is_terminal or json_out

    if json_output:
        output = []
    else:
        table = (
            Table(
                title=f"Properties of images in {bucket_name} {f'with prefix {prefix}' if prefix else ''}",
                show_header=True,
                header_style="bold magenta",
            )
            if console.is_terminal
            else Table.grid(show_header=True)
        )
        table.add_column("Name", style="dim", no_wrap=True)
        table.add_column("Size")
        table.add_column("Dominant colors")
        table.add_column("Brightness")
        table.add_column("Contrast")
        table.add_column("Sharpness")
        table.add_column("Labels")

    for obj in bucket.objects.filter(Prefix=prefix):
        if obj.key.endswith(("jpg", "png")):
            response = rekognition.detect_labels(
                Image={"S3Object": {"Bucket": bucket_name, "Name": obj.key}},
                Features=["GENERAL_LABELS", "IMAGE_PROPERTIES"],
                MaxLabels=8,
                MinConfidence=80,
                Settings={"ImageProperties": {"MaxDominantColors": 4}},
            )
            if "ImageProperties" in response:
                props = response["ImageProperties"]

                if json_output:
                    colors = [color["HexCode"] for color in props["DominantColors"]]
                    info = {
                        "Name": obj.key,
                        "Size": obj.size,
                        "DominantColors": colors,
                        "Brightness": props["Quality"]["Brightness"],
                        "Contrast": props["Quality"]["Contrast"],
                        "Sharpness": props["Quality"]["Sharpness"],
                    }
                    if "Labels" in response:
                        info["Labels"] = [label["Name"] for label in response["Labels"]]

                    output.append(info)
                else:
                    colors = Text.assemble(
                        *[
                            Text.assemble((color["HexCode"], stylize(color)), " ")
                            for color in props["DominantColors"]
                        ]
                    )
                    info = [
                        obj.key,
                        f"{obj.size/1024:.2f} kB",
                        colors,
                        f"{props['Quality']['Brightness']:.2f} %",
                        f"{props['Quality']['Contrast']:.2f} %",
                        f"{props['Quality']['Sharpness']:.2f} %",
                    ]
                    if "Labels" in response:
                        info.append(
                            ", ".join([label["Name"] for label in response["Labels"]])
                        )

                    table.add_row(*info)

    if json_output:
        typer.echo(json.dumps(output, indent=2))
    else:
        console.print(table)

if __name__ == "__main__":
    app()


import glob
import pandas as pd

class Setup:
    def __init__(self, name, path):
        self.name = name
        self.path = path

        self.search_for_probes()
        self.load_data(0)

    def search_for_probes(self):

        path = self.path

        files = glob.glob(f"{path}**/*probe*.csv", recursive=True)
        files.sort()
        files.append("None")

        self.probefiles = files
        self.selected_probe = 0

    def load_data(self, index):
        filename = self.probefiles[self.selected_probe]
        if filename != "None":
            df = pd.read_csv(filename)
            self.data = df
        else:
            self.data = None

    def print_setup(self):
        # maybe better in json.dumps?!
        print(f"name: {self.name}")
        print(f"path: {self.path}")
        print(f"probefiles: {self.probefiles}")
        print(f"selected_probe: {self.selected_probe}")
        # print(f"data: {self.data}")
        # setup["Data"] = df.to_dict()

if __name__ == '__main__':

    setups = []
    setups.append(Setup("Setup_A", "./data_1/"))
    setups.append(Setup("Setup_B", "./data_2/"))
    setups.append(Setup("Setup_C", "./data_2/"))

    for s in setups:
        s.print_setup()


# Pequeño progama que utiliza el Módulo de MatPlotLib para generar una gráfica de los ejericicos que he hecho esta semana
# TRACKING DE EJERCICIOS v1
import matplotlib.pyplot as plt

"""   Inicializar los parametros   """
semanas_Mes = [1,2,3,4] # Eje X
# Dias de la Semana
weekdays = ["Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"]
# Semanas
semanas = []
semanal = []

"""" Tomar Inputs"""

# Semanas
cant = int(input("Semanas que quiere graficar: "))
for i in range(cant):
    semanas.append([])

# Periodo
#ini = int(input("Fecha de inicio de la semana: "))
#fin = int(input("Fecha de fin de la semana: "))

# Repeticiones
for i in range(cant):
    for j in range(7):
        ejercicios = int(input("Por favor introduzca sus repeticiones del dia " + str(weekdays[j]) + " de la semana " + str(i + 1) + " :"))
        semanas[i].append(ejercicios)

# Realizar la grafica con los datos obtenidos
for i in range(cant):
    semanal.append(sum(semanas[i]))

plt.plot(semanas_Mes, semanal, semanas_Mes, semanal, "bo")
plt.xlabel('Semana del Mes')
plt.ylabel('Repeticiones totales')
plt.suptitle("Aumento Mensual de Ejercicios")
# plt.suptitle("Ejercicios Semana " + str(ini) + " - " + str(fin))
plt.show()


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

from nwae.utils.Log import Log
from inspect import currentframe, getframeinfo
from nwae.lang.detect.LangDetect import LangDetect
from nwae.lang.LangFeatures import LangFeatures
try:
    from nltk import pos_tag, word_tokenize, download
    download('punkt')
    download('averaged_perceptron_tagger')
except Exception as ex:
    raise Exception(
        str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
        + ': Unable to load nltk pos_tag: ' + str(ex)
    )

"""
Вместо этих "уродливых стоп-слов", у нас такой подход
  - через частеречную разметку, присвоить вес каждому слову в зависимости от типа слова
  - вес всех разметок предопределен. например, маленкое значение частице речи но большое
    значение имя-существительному
"""
class Stopwordtags:

    # https://stackoverflow.com/questions/15388831/what-are-all-possible-pos-tags-of-nltk
    DEFAULT_KEEP_TAGS_ENG = ('JJ', 'JJR', 'JJS', 'NN', 'NNP', 'NNS', 'VB', 'VBG', 'VBN', 'VBP', 'VBZ')
    DEFAULT_DISCARD_TAGS_ENG = ('CC', 'CD', 'DT', 'EX', 'IN', 'MD', 'PDT', 'TO')

    # TODO Expand
    # 名詞=noun, 動詞=verb
    DEFAULT_KEEP_TAGS_JAP = ('名詞', '動詞')
    # 助詞=particle, 接頭辞=prefix, 補助記号=auxiliary symbol
    DEFAULT_DISCARD_TAGS_JAP = ('助詞', '接頭辞', '補助記号')

    def __init__(
            self,
    ):
        return

    def filter_sentence_by_pos_tag_english(
            self,
            word_list,
            keep_tags = DEFAULT_KEEP_TAGS_ENG,
    ):
        if type(word_list) is str:
            word_list = word_tokenize(text=word_list, language='english')
        words_postags = pos_tag(word_list)
        sent_filtered = [w for w,t in words_postags if (t in keep_tags)]
        Log.debugdebug(
            str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
            + ': POS TAGs: ' + str(words_postags)
        )
        Log.debugdebug(
            str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
            + ': Filtered sentence: ' + str(sent_filtered)
        )
        return sent_filtered

    def filter_sentence_by_pos_tag_japanese(
            self,
            # string or word list
            word_list,
            keep_tags = DEFAULT_KEEP_TAGS_JAP,
    ):
        try:
            import nagisa
        except Exception as ex:
            raise Exception(
                str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
                + ': Unable to load nagisa: ' + str(ex)
            )
        if type(word_list) in [list, tuple]:
            text = ' '.join(word_list)
        else:
            text = word_list
        words_postags_obj = nagisa.tagging(text)
        txt_sym_tok = words_postags_obj.words
        txt_sym_postags = words_postags_obj.postags
        Log.debug(
            str(self.__class__) + ' ' + str(getframeinfo(currentframe()).lineno)
            + ': Japanese segmentation ' + str(txt_sym_tok) + ', word & POS tags: ' + str(txt_sym_postags)
        )

        words_postags = list(zip(txt_sym_tok, txt_sym_postags))
        sent_filtered = [w for w,t in words_postags if (t in keep_tags)]
        Log.debugdebug(
            str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
            + ': POS TAGs: ' + str(words_postags)
        )
        Log.debugdebug(
            str(__name__) + ' ' + str(getframeinfo(currentframe()).lineno)
            + ': Filtered sentence: ' + str(sent_filtered)
        )
        return sent_filtered

if __name__ == '__main__':
    Log.LOGLEVEL = Log.LOG_LEVEL_DEBUG_2
    Log.DEBUG_PRINT_ALL_TO_SCREEN = True
    sents = [
        'Freezing temperatures have gripped the nation, making Wednesday the coldest day yet this winter.',
        'Morning lows plunged to minus 16-point-three degrees Celsius in Seoul , the lowest to be posted during this year’s cold season.',
        'As of 7 a.m. Wednesday , morning lows stood at minus 15-point-four degrees in Daejeon , nearly minus 22 degrees in the Daegwallyeong mountain pass in Pyeongchang and minus 14 degrees in Gangneung.',
        'Due to the wind chill factor, temperatures stood at nearly minus 23 degrees in Seoul , minus 25 in Incheon and roughly minus 36 degrees in Daegwallyeong .',
        'An official of the Korea Meteorological Administration said the nation will continue to see subzero temperatures for the time being with the central regions and some southern inland areas projected to see morning lows plunge below minus 15 degrees',
        'Currently , a cold wave warning is in place for Seoul , Incheon , Daejeon and Sejong as well as the provinces of Gangwon , Chungcheong , North Jeolla and North Gyeongsang.',
        '本日はチャットサービスをご利用いただき、ありがとうございます。オペレーターと接続中です。',
        'The code run is successful',
        'I run the code',
        'Run',
    ]
    ld = LangDetect()
    swt = Stopwordtags()
    text_tag = []
    for sent in sents:
        lang = ld.detect(text = sent)
        if lang:
            lang = lang[0]
        else:
            continue

        if lang == LangFeatures.LANG_EN:
            sent_new = swt.filter_sentence_by_pos_tag_english(word_list=sent)
        elif lang == LangFeatures.LANG_JA:
            sent_new = swt.filter_sentence_by_pos_tag_japanese(word_list=sent)
        else:
            raise Exception(lang)
        text_tag.append(sent_new)
        print()
    exit(0)

"""
From https://stackoverflow.com/questions/15388831/what-are-all-possible-pos-tags-of-nltk

CC: conjunction, coordinating
& 'n and both but either et for less minus neither nor or plus so
therefore times v. versus vs. whether yet

CD: numeral, cardinal
mid-1890 nine-thirty forty-two one-tenth ten million 0.5 one forty-
seven 1987 twenty '79 zero two 78-degrees eighty-four IX '60s .025
fifteen 271,124 dozen quintillion DM2,000 ...

DT: determiner
all an another any both del each either every half la many much nary
neither no some such that the them these this those

EX: existential there
there

IN: preposition or conjunction, subordinating
astride among upon whether out inside pro despite on by throughout
below within for towards near behind atop around if like until below
next into if beside ...

JJ: adjective or numeral, ordinal
third ill-mannered pre-war regrettable oiled calamitous first separable
ectoplasmic battery-powered participatory fourth still-to-be-named
multilingual multi-disciplinary ...

JJR: adjective, comparative
bleaker braver breezier briefer brighter brisker broader bumper busier
calmer cheaper choosier cleaner clearer closer colder commoner costlier
cozier creamier crunchier cuter ...

JJS: adjective, superlative
calmest cheapest choicest classiest cleanest clearest closest commonest
corniest costliest crassest creepiest crudest cutest darkest deadliest
dearest deepest densest dinkiest ...

LS: list item marker
A A. B B. C C. D E F First G H I J K One SP-44001 SP-44002 SP-44005
SP-44007 Second Third Three Two * a b c d first five four one six three
two

MD: modal auxiliary
can cannot could couldn't dare may might must need ought shall should
shouldn't will would

NN: noun, common, singular or mass
common-carrier cabbage knuckle-duster Casino afghan shed thermostat
investment slide humour falloff slick wind hyena override subhumanity
machinist ...

NNP: noun, proper, singular
Motown Venneboerger Czestochwa Ranzer Conchita Trumplane Christos
Oceanside Escobar Kreisler Sawyer Cougar Yvette Ervin ODI Darryl CTCA
Shannon A.K.C. Meltex Liverpool ...

NNS: noun, common, plural
undergraduates scotches bric-a-brac products bodyguards facets coasts
divestitures storehouses designs clubs fragrances averages
subjectivists apprehensions muses factory-jobs ...

PDT: pre-determiner
all both half many quite such sure this

POS: genitive marker
' 's

PRP: pronoun, personal
hers herself him himself hisself it itself me myself one oneself ours
ourselves ownself self she thee theirs them themselves they thou thy us

PRP$: pronoun, possessive
her his mine my our ours their thy your

RB: adverb
occasionally unabatingly maddeningly adventurously professedly
stirringly prominently technologically magisterially predominately
swiftly fiscally pitilessly ...

RBR: adverb, comparative
further gloomier grander graver greater grimmer harder harsher
healthier heavier higher however larger later leaner lengthier less-
perfectly lesser lonelier longer louder lower more ...

RBS: adverb, superlative
best biggest bluntest earliest farthest first furthest hardest
heartiest highest largest least less most nearest second tightest worst

RP: particle
aboard about across along apart around aside at away back before behind
by crop down ever fast for forth from go high i.e. in into just later
low more off on open out over per pie raising start teeth that through
under unto up up-pp upon whole with you

TO: "to" as preposition or infinitive marker
to

UH: interjection
Goodbye Goody Gosh Wow Jeepers Jee-sus Hubba Hey Kee-reist Oops amen
huh howdy uh dammit whammo shucks heck anyways whodunnit honey golly
man baby diddle hush sonuvabitch ...

VB: verb, base form
ask assemble assess assign assume atone attention avoid bake balkanize
bank begin behold believe bend benefit bevel beware bless boil bomb
boost brace break bring broil brush build ...

VBD: verb, past tense
dipped pleaded swiped regummed soaked tidied convened halted registered
cushioned exacted snubbed strode aimed adopted belied figgered
speculated wore appreciated contemplated ...

VBG: verb, present participle or gerund
telegraphing stirring focusing angering judging stalling lactating
hankerin' alleging veering capping approaching traveling besieging
encrypting interrupting erasing wincing ...

VBN: verb, past participle
multihulled dilapidated aerosolized chaired languished panelized used
experimented flourished imitated reunifed factored condensed sheared
unsettled primed dubbed desired ...

VBP: verb, present tense, not 3rd person singular
predominate wrap resort sue twist spill cure lengthen brush terminate
appear tend stray glisten obtain comprise detest tease attract
emphasize mold postpone sever return wag ...

VBZ: verb, present tense, 3rd person singular
bases reconstructs marks mixes displeases seals carps weaves snatches
slumps stretches authorizes smolders pictures emerges stockpiles
seduces fizzes uses bolsters slaps speaks pleads ...

WDT: WH-determiner
that what whatever which whichever

WP: WH-pronoun
that what whatever whatsoever which who whom whosoever

WRB: Wh-adverb
how however whence whenever where whereby whereever wherein whereof why
"""


import  pygame
pygame.init()

#colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

#PI
pi=3.141592653

#CLOCK
clock = pygame.time.Clock()

#COORDS_AND_OTHER
x = 2
y = 468
width = 30
height = 27
speed = 10
isJump = False
JumpCount = 10
left = False
right = False
animCount = 0
JC = 0

#WALK_ANIM
walk_RIGHT = [pygame.image.load("figure-R1.gif"), pygame.image.load("figure-R2.gif"), pygame.image.load("figure-R3.gif")]
walk_LEFT = [pygame.image.load("figure-L1.gif"), pygame.image.load("figure-L2.gif"), pygame.image.load("figure-L3.gif")]
stand = pygame.image.load("figure-S.gif")

#PLATFORMS
platform_b = pygame.image.load("platform1.gif")
platform_m = pygame.image.load("platform2.gif")
platform_s = pygame.image.load("platform3.gif")

#DOORS
door_c = pygame.image.load("door1.gif")
door_o = pygame.image.load("door2.gif")

#SCREEN
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("MY OWN GAME(NO)")

#SPRITES

#DRAWING_FUNCTION
def draw():
    global animCount
    screen.fill(WHITE)

    if animCount + 1 >= 15:
        animCount = 0

    if left:
        screen.blit(walk_LEFT[animCount // 5], (x, y))
        animCount += 1
    elif right:
        screen.blit(walk_RIGHT[animCount // 5], (x, y))
        animCount += 1
    else:
        screen.blit(stand, (x, y))

    #pygame.draw.rect(screen, (0, 0, 255), (x, y, width, height))
    #pygame.display.update()

def draw_Platforms():
    screen.blit(platform_s, (200, 200))

    screen.blit(platform_b, (300, 400))
    #pygame.display.update()

def draw_Door():
    screen.blit(door_c, (20, 20))
    #pygame.display.update()

def draw_Text():
    # Выбрать шрифт для использования.
    # Стандартный шрифт, размером в 25.
    font = pygame.font.Font(None, 20)

    # Воспроизвести текст. "True" значит,
    # что текст будет сглаженным (anti-aliased).
    # Чёрный - цвет. Переменную BLACK мы задали ранее,
    # списком [0,0,0]
    # Заметьте: эта строка создаёт картинку с буквами,
    # но пока не выводит её на экран.
    text = font.render(str(JC), True, BLACK)

    # Вывести сделанную картинку на экран в точке (250, 250)
    screen.blit(text, (478, 1))
    #pygame.display.update()

def draw_mouse():
    pygame.mouse.set_visible(0)
    pos = pygame.mouse.get_pos()
    x1 = pos[0]
    y1 = pos[1]

    # Рисование
    screen.blit(stand, (x1, y1))
    #pygame.display.update()

looper = True
while looper:
    clock.tick(30)

    #EVENTS
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            looper = False

    #KEYS
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and x > 2:
        x -= speed
        left = True
        right = False
    if keys[pygame.K_RIGHT] and x < 467:
        x += speed
        left = False
        right = True
    else:
        left = False
        right = False
        animCount = 0
    if not isJump:
        #if keys[pygame.K_UP] and y > 2:
        #    y -= speed
        #if keys[pygame.K_DOWN] and y < 468:
        #    y += speed
        if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
            isJump = True
            JC += 1
    else:
        if JumpCount >= -10:
            if JumpCount < 0:
                y += (JumpCount ** 2) // 2
            else:
                y -= (JumpCount** 2) // 2
            JumpCount -= 1
        else:
            isJump = False
            JumpCount = 10
    if keys[pygame.K_ESCAPE]:
        looper = False

    draw()
    draw_Door()
    draw_Text()
    draw_Platforms()
    draw_mouse()
    pygame.display.update()
pygame.quit()


class Solution:
    def reorganizeString(self, s: str) -> str:
        ct = Counter(s)
        ans = ""
        maxH = [(-ct[i],i) for i in ct]
        heapify(maxH)
        prev = None
        while maxH:
            cnt,cur = heappop(maxH)
            cnt *= -1
            cnt-=1
            if prev!=None and prev[0]!=0:
                heappush(maxH,prev)
            prev = (-cnt,cur)
            ans += cur

        return "" if len(ans)<len(s) else ans


import redis
import time
import datetime
import numpy as np
from bokeh.plotting import figure, output_file, show, HBox, VBox
from bokeh.document import Document

""" 1. Create a redis database object - redis_object
    2. Pull 60 most recent temp values from redis db redis_object.zrange()
    3. Convert the redis data strings into integers with convert_tempstring_to_integers
    6. Pull out the temp data by tank number with  get_temps_by_tank()
    7. Assign the x an y axis values to bokeh lists
    8. Create  bokeh plots and index.html file then copy to /var/www for nginx access
"""

def convert_date_to_epochscore(date_string):

    """conver_date_to_epochscore takes a human readable data and
     converts it to seconds since epoch.  We use that second since
     epoch as a score in the redis database"""

    date_pattern = '%Y/%m/%d %H:%M:%S'
    time_stamp = time.strptime(date_string, date_pattern)
    epoch_seconds = float(time.mktime(time_stamp))
   # epoch_seconds = float(time.mktime(time.strptime(data_string, date_pattern)))
    return epoch_seconds

def convert_epoch_to_date(epoch_int):

    """convert_epoch_to_date takes an seconds to epoch integer and converts
    it to a human readable format"""

    human_readable_date = float(
        datetime.datetime.fromtimestamp(epoch_int).strftime(
        '%Y/%m/%d %H:%M:%S'))
    return human_readable_date

def convert_tempstring_to_floats(temp_string):

    """Clean up the data string from redis and convert the list elements
    to floats"""

    temp_list = temp_string.split(", ")

    first_element = temp_list[0].replace("[", "")
    temp_list[0] = first_element

    last_element = temp_list[-1].replace("]", "")
    temp_list[-1] = last_element

    temp_floats = [float(temp) for temp in temp_list]
    return temp_floats

def convert_redis_data_to_floats(range_data2):

    """convert_redis_data_to_integers using temp_floats to convert the
    entire data from redis into row and reconized by timestamp"""

    raw_temp_list = []

    for data in range_data2:
        temp_floats = convert_tempstring_to_floats(data)
        raw_temp_list.append(temp_floats)
    return raw_temp_list

def get_temps_by_tank(tank_temp_list, num_vals=9):

    """get_temps_by_tank takes a list of redis database range temperatures and
    pulls out column data."""

    # now initialize the return variable
    temps_by_tank = []

    for index in range(num_vals):  #8 temps and 1 epoch second values = 9 range items
        temp_temps = [tmp[index] for tmp in tank_temp_list]  #pull column data
        temps_by_tank.append(temp_temps)
    return temps_by_tank

if __name__ == '__main__':

    # create redis connection
    redis_object = redis.StrictRedis(host='localhost', port=6379, db=0)
    #  query the redis database with a range of dates
    most_recent_data = redis_object.zrange('Temps', -40, -1)
    # convert each row from redis call into floats
    tank_temps = convert_redis_data_to_floats(most_recent_data)
    # pull out the tank temps and group into y axis lists
    temps_by_tank = get_temps_by_tank(tank_temps)
    # pull out the last elment from the temps by tank which is epoch score
    x_axis_time_stamps = temps_by_tank[-1]
    # adjust the timestamps to central time -18000 seconds or 5 hours
    x_axis_time_stamps = [epoch - 18000 for epoch in x_axis_time_stamps]
    #create the x axis for bokeh
    x_axis_time_stamps = [datetime.datetime.fromtimestamp(ts) for ts in x_axis_time_stamps]
    #create date time object to a bokeh time line
    x_axis_time_stamps = [np.datetime64(dt).astype(long)/1000 for dt in x_axis_time_stamps]

    # assign each tank its list of temperatures
    y0_temps = temps_by_tank[0]
    y1_temps = temps_by_tank[1]
    y2_temps = temps_by_tank[2]
    y3_temps = temps_by_tank[3]
    y4_temps = temps_by_tank[4]
    y5_temps = temps_by_tank[5]
    y6_temps = temps_by_tank[6]
    y7_temps = temps_by_tank[7]
    y8_temps = temps_by_tank[8]

    #create the bokeh document to populate with plots and index.html file
    Document = Document()
    output_file("/var/www/index.html")
    # create some attributes to save typing when formating plots
    attrib_dict = dict(width=300, height=300, x_axis_type="datetime")
    line_dict = dict(size=12, alpha=0.5)

    # create the figures that hold each tanks plot
    plot0 = figure(title="Tank 1", **attrib_dict)
    plot1 = figure(title="Tank 2", **attrib_dict)
    plot2 = figure(title="Tank 3", **attrib_dict)
    plot3 = figure(title="Tank 4", **attrib_dict)
    plot4 = figure(title="Tank 5", **attrib_dict)
    plot5 = figure(title="Tank 6", **attrib_dict)
    plot6 = figure(title="Tank 7", **attrib_dict)
    plot7 = figure(title="Tank 8", **attrib_dict)

    # create the lines that go on the figure with attributes
    plot0.line(x_axis_time_stamps, y0_temps, color="black", **line_dict)
    plot1.line(x_axis_time_stamps, y1_temps, color="red", **line_dict)
    plot2.line(x_axis_time_stamps, y2_temps, color="green", **line_dict)
    plot3.line(x_axis_time_stamps, y3_temps, color="blue", **line_dict)
    plot4.line(x_axis_time_stamps, y4_temps, color="violet", **line_dict)
    plot5.line(x_axis_time_stamps, y5_temps, color="blue", **line_dict)
    plot6.line(x_axis_time_stamps, y6_temps, color="red", **line_dict)
    plot7.line(x_axis_time_stamps, y7_temps, color="purple", **line_dict)

    # tell bokeh to create the plots and display them
    show(VBox(HBox(plot0,plot1,plot2,plot3), HBox(plot4,plot5,plot6,plot7)))


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

import sys
import collections

def main():
    prefix = sys.argv[1]
    max_n = int(sys.argv[2])
    res = collections.defaultdict(list)
    for f_i in range(max_n+1):
        f = open(f'{prefix}.{f_i}', 'r')
        lines = [l.strip() for l in f]
        for line_no, l in enumerate(lines):
            for p in l.split('|||'):
                v, i = p.strip().split()
                v = float(v)
                i = int(i)
                res[line_no].append((i, v))

    for line_no in sorted(res):
        s = ' ||| '.join([f'{i} {v}' for i,v in sorted(res[line_no], key=lambda x:x[1], reverse=True)])
        print(s)

if __name__ == '__main__':
    main()


#! /usr/bin/env python3
from typing import List, Tuple, Dict
from collections import Counter

# from random import randint
from math import log
from collections import defaultdict
from bisect import bisect_left, bisect_right

"""08/20/2019

Solution1:
I predict, without even writing Solution1, it will time out.
And my prediction is correct. Solution1 is the most naive. Due to a ton of
repetitive computation, it is not surprising that it timed out.

Solution2:
I used segment tree for this solution, with each node being a dict recording
the frequency of each number and a tuple recording the max frequency number and
its corresponding frequency. This segment tree implementation does speed up
the solution, but it still timed out.

Solution3:
I gave up after the unsuccessful attempt with Solution2, even though I did
believe segment tree was a viable solution. After reading the discussion, I
saw that segment tree was indeed one of the standard solution to this problem.
The difference between a successful implementation of segment tree and my
previous segment tree is the merge step both during segment tree initialization
and querying. The key factors are:

1. Only the majority number in a child node has the possibility of becoming
majority in the parent node. So when merging two child nodes, we only need to
examine the majority number in each child. If majority does not exist in a
child node, we don't even have to examine it.

2. Examination can be done in O(log(n)) time. The way I examined majority in
the previous method was via dictionary, and it was O(n) time with probably quite
some overhead with dictionary processes. A very good way of examination is to
use binary search on a list of positions of the target number against a querying
range's start and end. Once proper positions on the list for querying range's
start and end, we can easily read how many of such target number exist within
this range. This takes O(log(n)) time with little overhead.

Once the merge function is optimized via the two factors above, the solution
shall pass OJ. I cannot do it today since I don't have internet at home. I will
run it tomorrow.

Update 08/21/2019
Solution3 clocked in at 1076 ms, 55%

P.S. The random pick method is definitely brilliant. It is similar to the method
in one of the CodeJam problem. Here, since we are looking for the majority
number, it is natural that if such number exists, we shall be able to randomly
pick it out more than 50% of the time. Error happens if none of the number we
pick is the majority while the majority actually exists. The probably of doing
this is less than 1/2 for each pick. Thus, the more picks we do, the less likely
that all of them are non-majority number. The concensus on the discussion was
to pick 20 times, in which the probability all of them are wrong picks is as
small as (1/2)**20, which furthr amounts to 0.01 times of error in 10000 queries.
For each random pick, we do the same binary search method to check whether it
is the real majority or not.
"""

class MajorityChecker1:
    def __init__(self, arr: List[int]):
        self.arr = arr

    def query(self, left: int, right: int, threshold: int) -> int:
        count = Counter(self.arr[left : right + 1])
        for k, v in count.items():
            if v >= threshold:
                return k
        return -1

class MajorityChecker2:
    def __init__(self, arr: List[int]) -> None:
        self.arr = arr
        # https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/
        size = 2 * (2 ** int(log(len(self.arr), 2) + 1)) - 1
        self.seg_tree: List[Tuple[Dict[int, int], Tuple[int, int]]] = [
            (defaultdict(int), (0, 0)) for _ in range(size)
        ]
        self.init_seg_tree(0, 0, len(self.arr) - 1)

    def query(self, left: int, right: int, threshold: int) -> int:
        count_dict, majority_tuple = self.query_seg_tree(
            left, right, 0, len(self.arr) - 1, 0
        )
        return -1 if majority_tuple[1] < threshold else majority_tuple[0]

    def merge(
        self,
        item1: Tuple[Dict[int, int], Tuple[int, int]],
        item2: Tuple[Dict[int, int], Tuple[int, int]],
    ) -> Tuple[Dict[int, int], Tuple[int, int]]:
        res_dict: Dict[int, int] = defaultdict(int)
        res_majority: Tuple[int, int] = item1[1]
        res_dict.update(item1[0])
        for k, v in item2[0].items():
            res_dict[k] += v
            if res_dict[k] > res_majority[1]:
                res_majority = (k, res_dict[k])

        return res_dict, res_majority

    def init_seg_tree(self, root: int, ss: int, se: int) -> None:
        if ss == se:
            self.seg_tree[root] = ({self.arr[ss]: 1}, (self.arr[ss], 1))
        else:
            mid = ss + (se - ss) // 2
            self.init_seg_tree(2 * root + 1, ss, mid)
            self.init_seg_tree(2 * root + 2, mid + 1, se)
            self.seg_tree[root] = self.merge(
                self.seg_tree[2 * root + 1], self.seg_tree[2 * root + 2]
            )

    def query_seg_tree(
        self, qs: int, qe: int, ss: int, se: int, root: int
    ) -> Tuple[Dict[int, int], Tuple[int, int]]:
        if qs == ss and qe == se:
            return self.seg_tree[root]
        mid = ss + (se - ss) // 2
        if qs > mid:
            return self.query_seg_tree(qs, qe, mid + 1, se, root * 2 + 2)
        elif qe <= mid:
            return self.query_seg_tree(qs, qe, ss, mid, root * 2 + 1)
        else:
            return self.merge(
                self.query_seg_tree(qs, mid, ss, mid, root * 2 + 1),
                self.query_seg_tree(mid + 1, qe, mid + 1, se, root * 2 + 2),
            )

class MajorityChecker3:
    def __init__(self, arr: List[int]) -> None:
        self.arr = arr

        # provision record for all positions of each number
        self.pos_dict: Dict[int, List[int]] = defaultdict(list)
        for i, a in enumerate(self.arr):
            self.pos_dict[a].append(i)

        # https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/
        size = 2 * (2 ** int(log(len(self.arr), 2) + 1)) - 1
        # provision segment tree
        self.seg_tree: List[int] = [-1] * size
        self.init_seg_tree(0, 0, len(self.arr) - 1)

    def query(self, left: int, right: int, threshold: int) -> int:
        maj = self.query_seg_tree(left, right, 0, len(self.arr) - 1, 0)
        return maj if self.frequency(maj, left, right) >= threshold else -1

    def merge(self, maj1: int, maj2: int, start: int, end: int) -> int:
        # NOTE: if a child node has no majority, none of its members can be
        # majority in the parent node. When a child has a majority number, it
        # may or may not be the majority in the parent node. Further examination
        # is needed.
        if (
            maj1 != -1
            and self.frequency(maj1, start, end) * 2 > end - start + 1
        ):
            return maj1
        if (
            maj2 != -1
            and self.frequency(maj2, start, end) * 2 > end - start + 1
        ):
            return maj2
        return (
            -1
        )  # when neither child node has majority, parent node has no majority

    def frequency(self, target: int, start: int, end: int) -> int:
        # use binary search to find how many target number resides within the
        # given range [start, end]
        return bisect_right(self.pos_dict[target], end) - bisect_left(
            self.pos_dict[target], start
        )

    def init_seg_tree(self, root: int, ss: int, se: int) -> None:
        if ss == se:
            self.seg_tree[root] = self.arr[ss]
        else:
            mid = ss + (se - ss) // 2
            self.init_seg_tree(2 * root + 1, ss, mid)
            self.init_seg_tree(2 * root + 2, mid + 1, se)
            self.seg_tree[root] = self.merge(
                self.seg_tree[2 * root + 1],
                self.seg_tree[2 * root + 2],
                ss,
                se,
            )

    def query_seg_tree(
        self, qs: int, qe: int, ss: int, se: int, root: int
    ) -> int:
        if qs == ss and qe == se:
            return self.seg_tree[root]
        mid = ss + (se - ss) // 2
        if qs > mid:
            return self.query_seg_tree(qs, qe, mid + 1, se, root * 2 + 2)
        elif qe <= mid:
            return self.query_seg_tree(qs, qe, ss, mid, root * 2 + 1)
        else:
            return self.merge(
                self.query_seg_tree(qs, mid, ss, mid, root * 2 + 1),
                self.query_seg_tree(mid + 1, qe, mid + 1, se, root * 2 + 2),
                qs,
                qe,
            )

test_case_first: List[int] = [2, 2, 1, 2, 1, 2, 2, 1, 1, 2]
test_case: List[List[int]] = [
    [2, 5, 4],
    [0, 5, 6],
    [0, 1, 2],
    [2, 3, 2],
    [6, 6, 1],
    [0, 3, 3],
    [4, 9, 6],
    [4, 8, 4],
    [5, 9, 5],
    [0, 1, 2],
]
mc = MajorityChecker1(test_case_first)
for t in test_case:
    print(mc.query(t[0], t[1], t[2]))


import src  # My utility functions
import itertools
import math

WEIGHTS = src.read(ints=True)

def configure(weights: list, groups=3):
    assert sum(weights) % groups == 0, f"Total weight is not a multiple of {groups}."
    target = sum(weights) // groups
    for size in itertools.count(1):
        qe_list = [math.prod(c) for c in itertools.combinations(weights, size) if sum(c) == target]
        if qe_list:
            return min(qe_list)

def main(weights=None):
    weights = weights or WEIGHTS

    print("Part One:")
    ans1 = configure(weights)
    print(f"The best configuration has a QE of {ans1}.")

    print("\nPart Two:")
    ans2 = configure(weights, groups=4)
    print(f"The best QE is now {ans2}.")
    src.clip(ans2)

if __name__ == '__main__':
    main()


'''
摂動によって得られた近傍解で暫定解（初期解）を一回だけ更新する
'''

import sys
import numpy as np

# 初期解の数(引数)
P = int(sys.argv[1])
# 順列の数
N = 24

# 割当て問題(横方向に各A,B,C,Dそれぞれの能力、縦方向は処理する問題に対応)
A = [6, 1, 9, 3]
B = [2, 5, 7, 8]
C = [6, 3, 5, 4]
D = [3, 5, 2, 1]

# 組み込み関数で重複無しの乱数値を得るには事前に数列のリストを作成する必要がある
list = range(N)

num = np.arange(4)
index = np.zeros(4, dtype = np.int_)
ans = np.zeros((P, 4, 4), dtype = np.int_)
ans_old = np.zeros((N, 4, 4), dtype = np.int_)
sum = np.zeros(P*4, dtype = np.int_)
l = 0
h = 0
same = False

# 重複無しでリストからランダムに値を取得
n = np.random.choice(list, P, replace = False)

# 数字から順列をつくるアルゴリズム
# http://itakanaya9.hatenablog.com/entry/2014/02/17/121428
for i in range(P):
    b = n[i] / 2
    index[2] = n[i] % 2
    c = b / 3
    index[1] = b % 3
    index[0] = c % 4
    num_tmp = num

    for j in range(4):
        ans[i,0,j] = num_tmp[index[j]]
        if j < 4:
            num_tmp = np.delete(num_tmp, index[j])

    # 目的関数の計算
    sum[l] = A[ans[i,0,0]] + B[ans[i,0,1]] + C[ans[i,0,2]] + D[ans[i,0,3]]
    print("P", i, "=", '(', ans[i,0,0],ans[i,0,1],ans[i,0,2],ans[i,0,3], ') Ans =', sum[l])
    l += 1

    # 隣同士を先頭から順番に入れ替える摂動
    for k in range(3):
        ans[i,k+1] = ans[i,0]
        tmp = ans[i,k+1,0+k]
        ans[i,k+1,0+k] = ans[i,k+1,1+k]
        ans[i,k+1,1+k] = tmp

        # 目的関数の計算
        sum[l] = A[ans[i,k+1,0]] + B[ans[i,k+1,1]] + C[ans[i,k+1,2]] + D[ans[i,k+1,3]]
        print("P", i, k+1, "=", '(', ans[i,k+1,0],ans[i,k+1,1],ans[i,k+1,2],ans[i,k+1,3], ') Ans =', sum[l])
        l += 1

    print()

# 最小の値の取得
min = np.min(sum)
print("min =", min)

# 最小の値のインデックスを取得
# ！np.where の出力は2次元配列なので注意！ min[0]を使う！
min = np.where(sum == min)

for m in min[0]:
    # 割り算の結果が int じゃないので変換する
    trns = int(m/4)
    # 一致しているものがあればパス
    for old in ans_old:
        if (ans[trns, m%4] == old).all():
            same = True
    if same == True:
        same = False
        continue

    print("(", ans[trns, m%4, 0], ans[trns, m%4, 1], ans[trns, m%4, 2], ans[trns, m%4, 3], ")")
    ans_old[h] = ans[trns, m%4]
    h += 1


from unittest import TestCase

from app.entities.contact import Contact
from app.entities.exceptions import ValidationError

class TestContact(TestCase):
    def test_init_should_initialize_the_instance(self):
        c = Contact('Homer Simpson', '+1 555-7334', 'homer@fox.com')

        self.assertEqual('Homer Simpson', c.name)
        self.assertEqual('+1 555-7334', c.phone)
        self.assertEqual('homer@fox.com', c.email)

    def test_validate_should_raise_an_exception_if_name_is_none_or_empty(self):
        c = Contact()

        self.assertRaises(ValidationError, lambda: c.validate())

        c.name = ''

        self.assertRaises(ValidationError, lambda: c.validate())


from .trainer import Trainer
import latentsafesets.utils.spb_utils as spbu
import latentsafesets.utils.plot_utils as pu

import logging
from tqdm import trange
import os

log = logging.getLogger("dyn train")

class PETSDynamicsTrainer(Trainer):
    def __init__(self, params, dynamics, loss_plotter):
        self.params = params
        self.dynamics = dynamics
        self.loss_plotter = loss_plotter

        self.ensemble = params['dyn_n_models']

        self.env_name = params['env']

    def initial_train(self, replay_buffer, update_dir):
        if self.dynamics.trained:
            self.visualize(os.path.join(update_dir, "dyn_start.gif"), replay_buffer)
            return

        log.info('Beginning dynamics initial optimization')

        for i in range(self.params['dyn_init_iters']):
            out_dict = replay_buffer.sample(self.params['dyn_batch_size'],
                                            ensemble=self.ensemble)
            obs, next_obs, act = out_dict['obs'], out_dict['next_obs'], out_dict['action']

            loss, info = self.dynamics.update(obs, next_obs, act, already_embedded=True)

            self.loss_plotter.add_data(info)

            if i % self.params['log_freq'] == 0:
                self.loss_plotter.print(i)
            if i % self.params['plot_freq'] == 0:
                log.info('Creating dynamics visualization')
                self.loss_plotter.plot()

                self.visualize(os.path.join(update_dir, "dyn%d.gif" % i), replay_buffer)

            if i % self.params['checkpoint_freq'] == 0 and i > 0:
                self.dynamics.save(os.path.join(update_dir, 'dynamics_%d.pth' % i))

        self.dynamics.save(os.path.join(update_dir, 'dyn.pth'))

    def update(self, replay_buffer, update_dir):
        log.info('Beginning dynamics optimization')

        for _ in trange(self.params['dyn_update_iters']):
            out_dict = replay_buffer.sample(self.params['dyn_batch_size'],
                                            ensemble=self.ensemble)
            obs, next_obs, act = out_dict['obs'], out_dict['next_obs'], out_dict['action']

            loss, info = self.dynamics.update(obs, next_obs, act, already_embedded=True)
            self.loss_plotter.add_data(info)

        log.info('Creating dynamics heatmap')
        self.loss_plotter.plot()
        self.visualize(os.path.join(update_dir, "dyn.gif"), replay_buffer)
        self.dynamics.save(os.path.join(update_dir, 'dyn.pth'))

    def visualize(self, file, replay_buffer):
        out_dict = replay_buffer.sample_chunk(8, 10)

        obs = out_dict['obs']
        act = out_dict['action']
        pu.visualize_dynamics(obs, act, self.dynamics, self.dynamics.encoder, file)


import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from sympy import *
from mpl_toolkits.mplot3d import Axes3D
import pickle
from matplotlib import cm
import math as mth
from mayavi import mlab
import pandas as pd
import h5py
#Initial data from density field and classification
sim_sz=250#Mpc
in_val,fnl_val=-140,140
tot_parts=8
s=3.92
grid_nodes=2000
box_sz=100
#Calculate the std deviation in physical units
grid_phys=1.*sim_sz/grid_nodes#Size of each voxel in physical units
val_phys=1.*(2*fnl_val)/grid_nodes#Value in each grid voxel
std_dev_phys=1.*s/val_phys*grid_phys

f=h5py.File("/import/oth3/ajib0457/bolchoi_z0/investigation/dtfe_box/box_sz%s_grid%d_smth%sMpc.h5" %(box_sz,grid_nodes,std_dev_phys), 'r')
recon_vecs_x=f['/slc/x'][:]
recon_vecs_y=f['/slc/y'][:]
recon_vecs_z=f['/slc/z'][:]
f.close()
fil_vecs=np.column_stack((recon_vecs_x,recon_vecs_y,recon_vecs_z))
#these AM are filtered for >=500 particles and also with eigvecs,already normalized
f2=h5py.File("/import/oth3/ajib0457/bolchoi_z0/investigation/dtfe_box/box_cutout_sz%s_AM_situatedingrid.h5"%box_sz, 'r')
halo_vecs_x=f2['/AM/x'][:]
halo_vecs_y=f2['/AM/y'][:]
halo_vecs_z=f2['/AM/z'][:]
f2.close()
halo_vecs=np.column_stack((halo_vecs_x,halo_vecs_y,halo_vecs_z))
#fil_vecs=skl.normalize(fil_vecs)
#halo_vecs=skl.normalize(halo_vecs)
mask_x=np.where(halo_vecs_x!=0)[0]
mask_y=np.where(halo_vecs_y!=0)[0]
mask_z=np.where(halo_vecs_z!=0)[0]

dotprod=np.zeros(len(mask_x))

for i in range(len(mask_x)):
    dotprod[i]=np.inner(halo_vecs[mask_x[i]],fil_vecs[mask_x[i]])
dotprod=abs(dotprod)

spin_res=open('/import/oth3/ajib0457/bolchoi_z0/investigation/dtfe_box/resid.pkl','rb')
resid=pickle.load(spin_res)
resid=np.asarray(resid)
mask=np.zeros(len(resid))
a=np.where(resid[:,0]<=100)
b=np.where(resid[:,1]<=100)
c=np.where(resid[:,2]<=100)
mask[a]=1
mask[b]+=1
mask[c]+=1

resid_cube100=resid[np.where(mask==3)]
dotprod_last=np.inner(resid_cube100[:,3:6],fil_vecs[mask_x[52]])
dotprod=np.append(dotprod,dotprod_last)
print(dotprod)
print(len(dotprod))


import random
from turtle  import Screen, Turtle

colors = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
choice = ['heads', 'tails']
directions = [0, 90, 180, 270]

tim = Turtle()

tim.shape('arrow')
tim.color(colors[1])
tim.pensize(10)
tim.speed(10)

for _ in range(200):
    tim.color(random.choice(colors))
    tim.forward(30)
    tim.setheading(random.choice(directions))

screen = Screen()
screen.exitonclick()


from rest_framework import serializers
from .models import Message

class MessageSerializer(serializers.ModelSerializer):
    owner_email = serializers.ReadOnlyField(source='owner.email')

    class Meta:
        fields = (
                'text',
                'owner_email',
                'created',
                'updated'
                )
        model = Message


#!/usr/bin/env python

"""
Usage:
  travis2tox <dot_travis_dot_yml_file>
  travis2tox --help

Reads as input <dot_travis_dot_yml_file>, a path to a YAML file for Travis
(typically called ".travis.yml") and writes the contents of an equivalent
tox.ini file to stdout.

"""

import argparse

from travis2tox import travis2tox

def main():
    """The ``main`` for the ``travis2tox`` program."""

    parser = argparse.ArgumentParser(
        description='Convert a .travis.yml file to a tox.ini file.')
    parser.add_argument(
        'dot_travis_dot_yml_file',
        help='path to a .travis.yml file')
    args = parser.parse_args()

    tox_config = travis2tox(args.dot_travis_dot_yml_file)
    print(tox_config.getvalue())

if __name__ == '__main__':
    main()


from rest_framework.response import Response
from rest_framework.views import APIView

from app.models import Book
from app.serializers import BookModelSerializer

class BookAPIView(APIView):
    def get(self, requset, *args, **kwargs):
        book_id = kwargs.get('id')
        if book_id:
            book = Book.objects.get(pk=book_id, is_delete=False)
            data = BookModelSerializer(book).data
            return Response({
                'status': 200,
                'message': '查询单个图书成功',
                'result': data,
            })
        else:
            book_object_all = Book.objects.filter(is_delete=False)
            book_ser = BookModelSerializer(book_object_all, many=True).data
            return Response({
                'status': 200,
                'message': '查询所有图书成功',
                'result': book_ser,
            })

    def post(self, request, *args, **kwargs):
        request_data = request.data
        if isinstance(request_data, dict):
            flag = False
        elif isinstance(request_data, list):
            flag = True
        else:
            return Response({
                "status": 400,
                "message": "参数格式有误",
            })
        serializer = BookModelSerializer(data=request_data, many=flag)
        serializer.is_valid(raise_exception=True)
        book_obj = serializer.save()
        return Response({
            "status": 200,
            "message": "添加图书成功",
            "results": BookModelSerializer(book_obj, many=flag).data,
        })

    def delete(self, request, *args, **kwargs):
        book_id = kwargs.get('id')
        if book_id:
            ids = [book_id]
        else:
            ids = request.data.get('ids')
        response = Book.objects.filter(pk__in=ids, is_delete=False).update(is_delete=True)
        if response:
            return Response({
                "status": 200,
                "message": '删除成功'
            })
        return Response({
            "status": 400,
            "message": '删除失败或者图书存在'
        })

    def put(self, request, *args, **kwargs):
        request_data = request.data
        book_id = kwargs.get('id')
        try:
            book_obj = Book.objects.get(pk=book_id)
        except Book.DoesNotExist:
            return Response({
                "status": 400,
                "message": '图书不存在'
            })
        serializer = BookModelSerializer(data=request_data, instance=book_obj)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({
            "status": 200,
            "message": '修改成功',
            "results": BookModelSerializer(book_obj).data
        })

    def patch(self, request, *args, **kwargs):
        request_data = request.data
        book_id = kwargs.get('id')
        try:
            book_obj = Book.objects.get(pk=book_id)
        except Book.DoesNotExist:
            return Response({
                "status": 400,
                "message": '图书不存在'
            })
        serializer = BookModelSerializer(data=request_data, instance=book_obj, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({
            "status": 200,
            "message": '修改成功',
            "results": BookModelSerializer(book_obj).data
        })


# first of all import the socket library
import socket
import random

def Edit(word): # splitting all the words in a sentence.
    return word.translate({ord(i): None for i in ",."}).split(" ")

def main():
    # creating a socket object
    s = socket.socket()
    print("Socket successfully created")

    # reserve a port on your computer in our
    # case it is 12345 but it can be anything
    port = 12345

    # Next bind to the port
    # we have not typed any ip in the ip field
    # instead we have inputted an empty string
    # this makes the server listen to requests
    # coming from other computers on the network
    s.bind(('', port))
    print("socket binded to %s" % (port))

    # put the socket into listening mode
    s.listen(5)
    print("socket is listening")
    # random sentence that each word will be seperated and server will choose one randomly
    dictionary = Edit(
        "It seems likely that this character will never get Devil Fruit. "
        "Aside from it being unnecessary for his character, it would be very trite and boring to see any more o"
        "f the crew suddenly gain Devil Fruit abilities."
        " It just furthers the strength of crew seeing them get as far as they "
        "are without the need of superhuman powers.")

    index_of_word = random.randint(0, len(dictionary) - 1)  # the (index_of_word+1)th word of the dictionary is selected

    hiding_word = dictionary[index_of_word][0]  # the first letter is known
    hiding_word += "_" * (len(dictionary[index_of_word]) - 1)  # all the others are hiding.
    hiding_word = list(hiding_word)

    UnFoundLetters = [True for i in range(
        len(hiding_word))]  # every index of letter that client has not found has UnfoundLetters[index]=True
    UnFoundLetters[0] = False  # because the first letter is already known

    # if the chosen word has more than 4 letters the last letter will be provided
    if (len(hiding_word) > 4):
        hiding_word[-1] = dictionary[index_of_word][-1]
        UnFoundLetters[-1] = False  # now and the last letter is known
    lifes = len(hiding_word) # the chances for the client to find the word correct

    # Establish connection with client.
    c, _ = s.accept()
    print("The client has started to play")
    c.send(bytes([lifes]))  # sending his lifes
    c.send("".join(hiding_word).encode())  # the starting info for the hiding word

    while (not ("".join(hiding_word) == dictionary[index_of_word]) and lifes > 0):
        letter = c.recv(1024).decode()
        false_guess = True  # this variable represents if the client did a wrong guess

        for i in range(len(dictionary[index_of_word])):
            if dictionary[index_of_word][i] == letter and UnFoundLetters[i]:
                UnFoundLetters[i] = False  # now an extra letter is found from client
                hiding_word[i] = letter
                false_guess = False  # client picked it up correct this time

        if (false_guess): lifes -= 1  # this means client did a wrong guess so he has to lose 1 life.
        # sending the updated infos to client
        c.send(bytes([lifes]))
        c.send("".join(hiding_word).encode())

    c.send(dictionary[index_of_word].encode())  # sendint to client the hidden word

    if lifes > 0:
        print("Client won")
    else:
        print("Client lost")

    # Close the connection with the client
    c.close()

if __name__ == "__main__":
    main()


import matplotlib
matplotlib.use('TkAgg')
matplotlib.rcParams['figure.figsize'] = (18.55, 9.86)
matplotlib.rcParams['axes.titlesize'] = 'medium'
from matplotlib.lines import Line2D

import numpy as np
import matplotlib.cm as cm

import matplotlib.pyplot as plt
import os
from analytics.utils import get_experiment_files
import re
from typing import List

# -- Load experiment Data

# experiment_path = "results/2019Apr10-202258_multiple_envs_icm/"
# data, cfgs, df = get_experiment_files(experiment_path, files={"log.csv": "read_csv"})

def extract_name(s):
    s = re.sub("_env[^/]*/", "", s)
    s = re.sub("^\d*[^_]*_", "", s)
    return s

def plot_experiment(experiment_paths: List[str], main_groupby = "main.env", x_axis="frames",
                    groupby_clm="run_name", kind="scatter",
                    show_legend=False, simple=True):

    data, cfgs, df = get_experiment_files(experiment_path, files={"log.csv": "read_csv"})

    # df.boxplot(by="run_index", column="FPS", grid=0)

    # experiments_group = df.groupby("experiment_id", sort=False)
    experiments_group = df.groupby(main_groupby, sort=False)

    save_path = os.path.join(experiment_path, "analysis")
    if not os.path.isdir(save_path):
        os.mkdir(save_path)

    not_plot = ['frames', 'run_id', 'run_index', 'update', 'cfg_id', 'comment', 'commit', 'experiment',
                'extra_logs', 'out_dir', 'resume', 'title', '_experiment_parameters.env',  'run_name', 'cfg_dir']

    plot_data = set(df.columns) - set(not_plot)
    plot_data = [x for x in plot_data if "." not in x]

    experiment_name, exp_gdf = next(iter(experiments_group))
    no_subgroups = exp_gdf.groupby(groupby_clm).ngroups
    subgroup_names = list(exp_gdf.groupby(groupby_clm).groups.keys())
    if not simple:
        subgroup_titles = [extract_name(x) for x in subgroup_names]
    else:
        subgroup_titles = [str(x) for x in subgroup_names]

    colors = cm.rainbow(np.linspace(0, 1, no_subgroups))
    colors_map = {x: y for x, y in zip(subgroup_titles, colors)}
    legend_elements = {color_name: Line2D([0], [0], marker='o', color=colors_map[color_name], label='Scatter',
                                          markerfacecolor=colors_map[color_name], markersize=7)
                       for color_name in subgroup_titles}

    size = int(pow(experiments_group.ngroups, 1/2))+1

    for plot_p in plot_data:
        plt.figure()
        # Iterate through continents
        share_ax = None
        for i, (experiment_name, exp_gdf) in enumerate(experiments_group):
            # create subplot axes in a 3x3 grid
            ax = plt.subplot(size, size, i + 1)  # n rows, n cols, axes position
            if share_ax is None:
                share_ax = ax
            # plot the continent on these axes
            if simple:
                exp_gdf.groupby(groupby_clm).plot(x_axis, plot_p, ax=ax, legend=False, kind=kind)
            else:
                for sub_group_name, sub_group_df in exp_gdf.groupby(groupby_clm):
                    color_name = extract_name(sub_group_name)
                    sub_group_df.plot(x_axis, plot_p, ax=ax, legend=False, kind=kind, c=colors_map[color_name], s=1.)

            # set the title
            if simple:
                ax.set_title(exp_gdf.iloc[0].title[13:-3])
            else:
                ax.set_title(experiment_name)

            # set the aspect
            # adjustable datalim ensure that the plots have the same axes size
            # ax.set_aspect('equal', adjustable='data lim')
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 3))

        # handles, labels = ax.get_legend_handles_labels()
        if show_legend:
            plt.gcf().legend([legend_elements[x] for x in subgroup_titles], subgroup_titles, loc="lower right")

        plt.title(plot_p, fontsize=12, y=-0.50)

        plt.tight_layout()
        plt.subplots_adjust(wspace=0.15, hspace=0.35)
        plt.savefig(f"{save_path}/{plot_p}.png")
        plt.close()

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Plot multiple groups of experiments.')
    parser.add_argument('experiment_paths', nargs='+')
    parser.add_argument('--complex', action='store_true', default=False)
    args = parser.parse_args()

    if not args.complex:
        plot_experiment(args.path, main_groupby="experiment_id", groupby_clm="run_id", kind="line", simple=True,
                        show_legend=False)
    else:
        plot_experiment(args.path, show_legend=True, kind="scatter", simple=False)


import numpy as np
import pandas as pd

from sklearn.metrics import mean_squared_error, mean_tweedie_deviance, mean_absolute_error

def evaluation(model, model_name, eval_path):
    """
    Given a model and a dataset, perform an evaluation using 4 different metrics.

    Parameters
    ----------
    model: Model to evaluate (loaded pickle)
    model_name: Name of the model being evaluated (str)
    eval_path: Path to test dataset (str)

    Returns
    -------
    Dictionary with model name and hyperaparameters, evaluation metrics and current date. (dict)
    - model: Model name that is evaluated. (str)
    - hyperparameters: Each hyperparameter is a key.
    - mae, wmape, rmse, tweedie: Evaluation metrics results. (float)
    - val_date: Current datetime. (datetime)
    """

    # Read the test set and prepare it
    df = pd.read_csv(eval_path)
    target_col = 'sales'
    test_x = df.drop([target_col, 'date'], axis='columns')
    test_y = df[target_col]

    # Predict and compute the different metrics
    predictions = model.predict(test_x)
    mae = round(mae_loss(predictions, test_y), 3)
    wmape = round(wmape_loss(predictions, test_y), 3)
    rmse = round(rmse_loss(predictions, test_y), 3)
    tweedie = round(tweedie_loss(predictions, test_y), 3)

    # Get the correspondent parameters of the model_name to save them in the variable 'results'
    parameters = get_parameters(model_name)

    # Save the results
    results = {'model': model_name,
               'fit_intercept': parameters['fit_intercept'],
               'n_jobs': parameters['n_jobs'],
               'd_max_depth': parameters['d_max_depth'],
               'max_leaf_nodes': parameters['max_leaf_nodes'],
               'd_max_features': parameters['d_max_features'],
               'learning_rate': parameters['learning_rate'],
               'n_estimators': parameters['n_estimators'],
               'g_max_depth': parameters['g_max_depth'],
               'g_max_features': parameters['g_max_features'],
               'mae': mae,
               'wmape': wmape,
               'rmse': rmse,
               'tweedie': tweedie,
               'eval_date': pd.to_datetime('now', utc=True)}

    return results

def get_parameters(model_name):
    """
    Function that receives a model name and returns the parameters it was trained with.
    Parameters
    ----------
    model_name: Name of the model. (str)

    Returns
    -------
    Dictionary containing the hyperparameters of the model.
    """

    # Initialize all possible hyperparameters with np.nan
    all_hyper = []
    for i in range(0, 9):
        all_hyper.append(np.nan)

    # Get the model type selected by the user
    model_id = model_name.split('_')

    # Save hyperparameters from linear_regression
    if model_id[0] == 'linear':

        # Get possible fit_intercept values (all_hyper[0])
        if model_id[1] == 'T':
            all_hyper[0] = True
        else:
            all_hyper[0] = False

        # Get possible n_jobs values (all_hyper[1])
        all_hyper[1] = model_id[2]

    # Save hyperparameters from decision_tree
    elif model_id[0] == 'decision':
        # Get possible max_depth values (all_hyper[2])
        all_hyper[2] = model_id[1]

        # Get possible max_leaf_node values (all_hyper[3])
        if model_id[2] == 'N':
            all_hyper[3] = 'None'
        else:
            all_hyper[3] = model_id[2]

        # Get possible max_features values (all_hyper[4])
        if model_id[3] == 'N':
            all_hyper[4] = 'None'
        elif model_id[3] == 'a':
            all_hyper[4] = 'auto'
        elif model_id[3] == 's':
            all_hyper[4] = 'sqrt'
        else:
            all_hyper[4] = 'log2'

    # Save hyperparameters from gradient boosting
    elif model_id[0] == 'gradient':

        # Get possible learning_rate values (all_hyper[5])
        if all_hyper != '1':
            for i in range(len(model_id[1])):
                all_hyper[5] = model_id[1]
        else:
            all_hyper[5] = model_id[1]

        # Get possible n_estimators values (all_hyper[6])
        all_hyper[6] = model_id[2]

        # Get possible max_depth values (all_hyper[7])
        all_hyper[7] = model_id[3]

        # Get possible max_features values (all_hyper[8])
        if model_id[4] == 'N':
            all_hyper[8] = 'None'
        elif model_id[3] == 'a':
            all_hyper[8] = 'auto'
        elif model_id[3] == 's':
            all_hyper[8] = 'sqrt'
        else:
            all_hyper[8] = 'log2'

    # There are no more type models implemented
    else:
        raise Exception('Model name not implemented.')

    # Save all parameters in a dictionary
    parameters = {'fit_intercept': all_hyper[0], 'n_jobs': all_hyper[1], 'd_max_depth': all_hyper[2],
                  'max_leaf_nodes': all_hyper[3], 'd_max_features': all_hyper[4], 'learning_rate': all_hyper[5],
                  'n_estimators': all_hyper[6], 'g_max_depth': all_hyper[7], 'g_max_features': all_hyper[8]}

    return parameters

def mae_loss(predictions, true):
    return mean_absolute_error(predictions, true)

def wmape_loss(predictions, true):
    return np.mean(np.abs(true - predictions)) / np.mean(true)

def rmse_loss(predictions, true):
    return np.sqrt(mean_squared_error(predictions, true))

def tweedie_loss(predictions, true):
    return mean_tweedie_deviance(true, predictions)


from typing import Dict, Optional
from uuid import UUID

from autoinsight.ident.IdentObjectBase import IdentObjectBase
from .ServiceBase import ServiceBase

class ContextManagementService(ServiceBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._registeredContexts: Dict[UUID, IdentObjectBase] = {}
        self._currentContext: Optional[IdentObjectBase] = None
        self._contextQueue: list[IdentObjectBase] = []
        self._os: IdentObjectBase = None
        self._script: IdentObjectBase = None

    @property
    def currentContext(self) -> Optional[IdentObjectBase]:
        if not self._currentContext and self._contextQueue:
            self._currentContext = self._contextQueue[-1]
        return self._currentContext

    @currentContext.setter
    def currentContext(self, context: IdentObjectBase):
        if not context:
            self._currentContext = None

        if self._currentContext != context and context.id in self._registeredContexts:
            self._currentContext = context
            self._updateContextQueue(context)

    @property
    def os(self) -> Optional[IdentObjectBase]:
        return self._os

    @property
    def script(self) -> Optional[IdentObjectBase]:
        return self._script

    def registerContext(self, context: IdentObjectBase):
        if context.id not in self._registeredContexts:
            self._registeredContexts[context.id] = context
            self._contextQueue.append(context)

            if not self.currentContext:
                self.currentContext = context

    def _updateContextQueue(self, context: IdentObjectBase):
        if context.id in self._registeredContexts and self._contextQueue and self._contextQueue[-1] != context:
            self._contextQueue.remove(context)
            self._contextQueue.append(context)

    def unregisterContext(self, context: IdentObjectBase):
        if not context:
            return

        if context.id in self._registeredContexts:
            del self._registeredContexts[context.id]
            self._contextQueue.remove(context)

            if self.currentContext == context:
                self.currentContext = None

    def reset(self):
        """
        Clean all the registered contexts
        """
        self._registeredContexts.clear()
        self._contextQueue.clear()
        self._currentContext = None


#check whether it is prime or not.Print 'yes' if it is prime else print 'no'.
#take input from user
n=int(input())
#check whether number greater than1
if(n>>1):
    for i in range(2,n):
        if(n%1==0):
            print("no")
            break
    else:
        print("yes")
else:
    print("yes")


class Node:
    def __init__(self,data):
        self.left = None
        self.right = None
        self.data = data

def height(root):
    if root==None:
        return 0
    else:
        hl=height(root.left)
        hr=height(root.right)
        h=max(hl,hr)+1
        return h

def printLevelOrder(root):
    h = height(root)
    for i in range(1,h+1):
        printGivenLevel(root, i)

def printGivenLevel(root,level):
    if root is None:
        return
    if level == 1:
        print(root.data,end=" ")
    elif level > 1:
        printGivenLevel(root.left, level-1)
        printGivenLevel(root.right, level-1)

N1=Node(1)
N2=Node(2)
N3=Node(3)
N4=Node(4)
N5=Node(5)
N6=Node(6)
N7=Node(7)

N1.left=N2
N2.left=N4
N2.right=N5
N1.right=N3
N5.left=N6
N6.right=N7

print(height(N1))
printLevelOrder(N1)


from src.Shared.Helpers.Transformer import Transformer
from src.Role.Domain.Entities.Role import Role

class RoleTransformer(Transformer):

    def __init__(self):
        super()

    def transform(self, role: Role):
        return {
            "id": str(role.id),
            "name": role.name,
            "slug": role.slug,
            "enable": role.enable,
            "permissions": role.permissions
        }


"""
All experiments for extension to undersampling paper.

Notes:
Might need to adjust Batch size, learning rate, and stopping
criteria for neural net.
"""
import time

start_time = time.time()
def print_verbose(txt, verbose=False):
    global start_time
    if verbose:
        print("Time for last phase: {} s".format(time.time() - start_time))
        print(txt)
    start_time = time.time()

print_verbose("Importing libraries.")

import os
import numpy as np
from sklearn.linear_model import LogisticRegression
import data.load_data as load_data
print_verbose("Importing neural net library")
import models.uplift_neural_net as uplift_neural_net
print_verbose("Importing undersampling experiment_aux")
import models.uplift_calibration as uplift_calibration
print_verbose("Trying to import R")
try:
    import rpy2.robjects as robjects
except:
    print("R not available in this environment.")
    print("Uplift forests cannot be used.")
print_verbose("Imports done.")

class DoubleClassifier():
    """
    Basic class for double classifier with logistic regression.
    """
    def __init__(self):
        """
        Initialize model
        """
        self.model_t = LogisticRegression()
        self.model_c = LogisticRegression()
        self.analytic_correction = False

    def fit(self, X_t, y_t, X_c, y_c,
            k_t=None, k_c=None, p_t=None, p_c=None):
        """
        Fit method
        If k_t, k_c, p_t, p_c are available, the model will make
         an analytic correction in the prediction phase for the
         bias caused by undersampling with these factors.

        Args:
        k_t (float): k_t used in split undersampling. Needed here
         for analytic correction
        k_c (float): k_c used is split undersampling. Needed
         for analytic correction.
        p_t (float): Average conversion rate in treated observations.
        p_c (float): Average conversion rate in untreated observations.

        """
        if k_t is not None and k_c is not None:
            self.analytic_correction = True
            self.k_t = k_t
            self.k_c = k_c
            self.p_t = p_t
            self.p_c = p_c

        self.model_t.fit(X_t, y_t)
        self.model_c.fit(X_c, y_c)
        # LogisticRegression predicts values for every class.
        # Store the index for the positive classes.
        if self.model_t.classes_[0] == True:
            self.model_t_pos_idx = 0
        else:
            self.model_t_pos_idx = 1
        if self.model_c.classes_[0] == True:
            self.model_c_pos_idx = 0
        else:
            self.model_c_pos_idx = 1

    def predict_uplift(self, X):
        """
        Basic predict function. Handles sklearn.

        Args:
        X (numpy.array): Features to predict from.
        """
        pred_t = self.model_t.predict_proba(X)
        # Keep only the predictions for the positive class
        pred_t = pred_t[:, self.model_t_pos_idx]
        pred_c = self.model_c.predict_proba(X)
        # Keep positive predictions
        pred_c = pred_c[:, self.model_c_pos_idx]
        if self.analytic_correction:
            pred_t = uplift_calibration.analytic_correction(
                pred_t, k=self.k_t, p_bar=self.p_t
            )
            pred_c = uplift_calibration.analytic_correction(
                pred_c, k=self.k_c, p_bar=self.p_t
            )
        tau = pred_t - pred_c
        return tau

class ClassVariableTransformation():
    """
    Basic class for class-variable transformation.
    """
    def __init__(self):
        """
        """
        self.model = LogisticRegression()

    def fit(self, X, z):
        """
        Args:
        X (numpy.array): Features of the data
        z (numpy.array): The reverted label.

        Notes:
        The class-variable transformation is solely dependent on
        the y and the t variables leaving it unaffected by undersampling.
        Hence we can use the z-variable provided by the
        load_data.DatasetCollection-class.
        """
        self.model.fit(X, z)
        # Index for positive class
        if self.model.classes_[0] is True:
            self.pos_idx = 0
        else:
            self.pos_idx = 1

    def predict_uplift(self, X):
        """
        This function does the "normalization" of the predictions.
        While this calibrates the predictions, this does not
        affect the rank between samples and hence does not affect
        AUUC.

        Args:
        X (np.array): Features to predict from.
        """
        predictions = self.model.predict_proba(X)
        predictions = predictions[:, self.pos_idx]
        predictions = 2 * predictions - 1
        return predictions

class UpliftNeuralNet(uplift_neural_net.DirectGradientUpliftNN):
    """
    Class to package data conversions etc. to standardize signature (?).
    Although the neural net uses a somewhat more exotic format for the data.

    Need a version that takes in just training and validation sets, maybe as
    dict with appropriate keys? Or just plain X and y separately.
    The undersampling functions return dicts with keys X, y, z, r, and t.
    We need a wrapper for that that can then be fed into the dataloader class.
    Howerver, r needs to be re-evaluated after undersampling.
    """
    def __init__(self, n_features=12):
        """
        Args:
        n_features (int): Number of features
        """
        print("Maybe set batch size, number of epochs etc in uplift_neural_net.py")
        print_verbose("Initializing neural net.")
        super().__init__(n_features=n_features)
        print_verbose("Done initializing super")

    def fit(self, training_set, validation_set):
        """
        The neural net needs the input data as load_data.DatasetWrapper
        objects.

        Args:
        training_set (dict): Dict with keys 'X', 'y', and 'r'
        validation_set (dict): Dict with keys 'X', 'y', and 'r'
        """
        # DatasetWrapper actually takes a dict with keys X, y, and then possibly
        # t, z, and r. So undersampling can be done, then
        print_verbose("Wrapping datasets")
        training_set = load_data.DatasetWrapper(training_set)
        validation_set = load_data.DatasetWrapper(validation_set)
        # The super() uses the r-variable for training automatically.
        print_verbose("Fitting model")
        super().fit(training_set, validation_set)
        print_verbose("Fitting done")

    def predict_uplift(self, dataset):
        """
        Method for predicting uplift given dict or DatasetWrapper.
        Using "overloading" (checking type and adjusting input as needed) to
        make this method compatible also with the function calls in teh fit-loop.

        Args:
        dataset (np.array, dict or load_data.DatasetWrapper): One set from
         DatasetCollection (e.g. testing set). Needs to contain
         key 'X'.
        """
        if isinstance(dataset, np.ndarray):
            dataset = {'X': dataset}
        if isinstance(dataset, dict):
            dataset = load_data.DatasetWrapper(dataset)
        return super().predict_uplift(dataset)

class UpliftRandomForest():
    """
    Wrapper class for uplift random forest.
    Include analytic correction with a flag (?).
    """
    def __init__(self):
        # Generating name for instance to avoid conflicts  between
        # multiple processes (e.g. tmp-files).
        self.model_id = str(os.getpid())
        self.analytic_correction = False
        self.k_t = None
        self.k_c = None
        self.p_t = None
        self.p_c = None

    # Add some auxiliary functions to communicate with R
    def numpy_to_csv_(self, data, filename, path='./tmp/'):
        """
        Function for converting numpy array to csv
        -skip headers

        Args:
        data (?): Data in some format. Should it be a dict
        with X, y, and t or just one of these?
        """
        # Check if tmp exists:
        if not os.path.isdir(path):
            if os.path.isfile(path):
                # There is a file with this name. Crash.
                raise Exception  # Just generic exception
            else:
                # Create needed directory
                os.mkdir(path)
        np.savetxt(path + filename+'.train_X.csv', data['X'], delimiter=',')
        np.savetxt(path + filename+'.train_y.csv', data['y'], delimiter=',')
        np.savetxt(path + filename+'.train_t.csv', data['t'], delimiter=',')
        return

    def numpy_X_to_csv_(self, data, filename, path='./tmp/'):
        """
        Function for storing testing or validation set data (X) for
        further processing in R.
        Args:
        data (np.array): duh.
        filename (str): name of _original_ datafile used in experiments.
        used in naming to keep track of which experiment this belongs to.
        path (str):
        set ({'test', 'val'}): One of two values depending on whether we are
        dealing with validation or testing set.
        """
        np.savetxt(path + filename + '.test.csv', data, delimiter=',')
        return

    def csv_to_numpy_(self, filename, path='./tmp/', k=1, delimiter=','):
        """
        Used to read results (predictions) from R.
        """
        tmp = np.genfromtxt(path + filename + '.' + str(k)  # Use self.model_id here instead.
                            + '.predictions.csv', delimiter=delimiter)
        return tmp

    def fit(self, training_set, k_t=None, k_c=None,
            p_t=None, p_c=None):
        """
        Model fitting with signature conforming to other models
        in this file.

        Args:
        training_set (?): Containing 'X', 'y', and 't' for training set.
        """
        if p_t is not None and p_c is not None:
            self.analytic_correction = True
            self.k_t = k_t
            self.k_c = k_c
            self.p_t = p_t
            self.p_c = p_c
        # Where is the numpy_to_csv-function located? COPY or IMPORT!!
        self.numpy_to_csv_(training_set, self.model_id)  # Add some ID here.
        # Load code for uplift random forest in R
        robjects.r("source('./models/uplift_random_forest.R')")
        # Train model. Will automatically look for the file created by
        # numpy_to_csv() with the given model id.
        robjects.r("model = train_uplift_rf('{}')".format(self.model_id))

    def predict_uplift(self, data):
        # Should k_t and k_c be set earlier?
        # We also need p_t and p_c for the correction!!!
        """
        Method for predicting uplift. Corrects for any biases
        introduced by undersampling assuming that k_t and k_c are
        set. k_t=1 and k_c=1 is equivalent to no correction.

        Args:
        data (np.array): Features of data.
        k_t (float): k_t used in split-undersampling. Used here to correct
         for the bias introduced by undersampling.
        k_c (float): k_c used in split-undersampling. Used here to correct
         for the bias introduced by undersampling.
        """
        # Now store validation or testing set
        # Loop this to not cause R to crash.
        bins = 100
        pred = np.empty(shape=[0, 2])
        # Model already in memory after training uplift rf.
        #r_command = "model = load_model('{}')".format(filename + str(1))
        #robjects.r(r_command)
        n_testing_samples = data.shape[0]
        for i in range(bins):
            tmp_data = data[int(i * n_testing_samples/bins):int((i + 1) * n_testing_samples/bins), :]
            self.numpy_X_to_csv_(tmp_data, self.model_id)
            # Model already loaded.
            robjects.r("predict_uplift_rf('{}', load_model=FALSE)".format(self.model_id))
            # Add results to list
            tmp = self.csv_to_numpy_(self.model_id, delimiter=' ')
            pred = np.concatenate((pred, tmp), axis=0)
            print("{} out of {} done.".format(i + 1, bins))

        assert len(pred) == n_testing_samples, "Error in predictions."
        # Uplift without any corrections:
        # (treated conversion probability is in col 0)
        if self.k_t is not None and self.k_c is not None:  # etc
            prob_t = uplift_calibration.analytic_correction(pred[:, 0], self.k_t, self.p_t)
            prob_c = uplift_calibration.analytic_correction(pred[:, 1], self.k_c, self.p_c)
        else:
            prob_t = pred[:, 0]  # undersampling_experiment_aux.analytic_correction(pred[:, 0])
            prob_c = pred[:, 1]  # undersampling_experiment_aux.analytic_correction(pred[:, 1])
        tau = np.array([item1 - item2 for item1, item2 in zip(prob_t, prob_c)])
        return tau


# The arithmetic expression can be converted to a binary tree structure, where all leaves are numbers and all inner-node labels are operators (just considering 7 operations: +, -, *, /, //, %, **), such as the following binary tree for the expression: 3 + ((5+9)*2). Define a function/method eval(t) to calculate the expression value denoted by t

class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def evaluate_expression(root):
    if root is None:
        return 0
    if root.left is None and root.right is None:
        return int(root.val)
    left_val = evaluate_expression(root.left)
    right_val = evaluate_expression(root.right)
    if root.val == "+":
        return left_val + right_val
    elif root.val == "-":
        return left_val - right_val
    elif root.val == "*":
        return left_val * right_val
    elif root.val == "/":
        return left_val / right_val
    elif root.val == "//":
        return left_val // right_val
    elif root.val == "%":
        return left_val % right_val
    elif root.val == "**":
        return left_val ** right_val
    else:
        raise ValueError("Invalid operator: " + root.val)

root = TreeNode("+")
root.left = TreeNode("3")
root.right = TreeNode("*")
root.right.left = TreeNode("+")
root.right.right = TreeNode("2")
root.right.left.left = TreeNode("5")
root.right.left.right = TreeNode("9")

result = evaluate_expression(root)
print(result)


import numpy as np
import matplotlib.pyplot as plt

"""
Calculates the distance from the origin to the center of each
cell
"""

f=np.loadtxt("massDensityTest.txt")
print("Data has {} Rows".format(np.size(f,0)))
rad = np.ones((np.size(f,0),))

for n in np.arange(np.size(f,0)):
	rad[n]=np.sqrt(f[n,0]**2+f[n,1]**2)
	print (rad[n])

with open('ShellMassDensity Test.txt', 'w') as f1:
	for i in np.arange(np.size(f,0)):
		f1.write('{}\t{}\t{}\n'.format(str(rad[i]),str(f[i,2]),str(rad[i]-rad[i-1])))


#!/usr/bin/env python3
import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

def explained_variance():
    df = pd.read_csv("src/data.tsv", sep="\t")
    pca = PCA()
    pca.fit(df)
    ev = pca.explained_variance_
    v = df.var()

    return v, ev

def stringify(arr):
    return " ".join(map(lambda x: f"{x:.3f}", arr))

def main():
    v, ev = explained_variance()
    print("The variances are: " + stringify(v))
    print("The explained variances after PCA are: " + stringify(ev))

    ev_cumsum = np.cumsum(ev)
    plt.plot(np.arange(len(ev_cumsum)) + 1, ev_cumsum)

    plt.show()

if __name__ == "__main__":
    main()


import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
import numpy as np
import joblib

# Function to train and save both the model and TF-IDF vectorizer
def train_and_save_model(file_path, model_save_path, vectorizer_save_path):
    # Create a DataFrame from the sample data
    df = pd.read_excel(file_path)

    # Vectorize the text data using TF-IDF (Term Frequency-Inverse Document Frequency)
    tfidf_vectorizer = TfidfVectorizer(max_features=5000)
    X_tfidf = tfidf_vectorizer.fit_transform(df['Ingredients'])
    y_recipe = df['Recipe']

    # Train a Multinomial Naive Bayes classifier for category prediction
    model = MultinomialNB()
    model.fit(X_tfidf, y_recipe)

    # Save the trained model and vectorizer
    joblib.dump(model, model_save_path)
    joblib.dump(tfidf_vectorizer, vectorizer_save_path)

# Function to make N category predictions based on user input
def predict_category(input_text, model_path, vectorizer_path, N=3):
    # Load the trained model and TF-IDF vectorizer
    model = joblib.load(model_path)
    tfidf_vectorizer = joblib.load(vectorizer_path)

    # Vectorize the input text using the loaded TF-IDF vectorizer
    input_tfidf = tfidf_vectorizer.transform([input_text])

    # Get probabilities for each category
    probabilities = model.predict_proba(input_tfidf)[0]

    # Get the top N categories with the highest probabilities
    top_indices = np.argsort(-probabilities)[:N]  # Sort in descending order and get top N indices
    top_categories = [model.classes_[i] for i in top_indices]  # Get category labels
    top_probabilities = probabilities[top_indices]  # Get corresponding probabilities

    return list(zip(top_categories, top_probabilities)), 1

    return predictions, 1