import csv
import codecs
import pprint
import re
import xml.etree.cElementTree as ET

import cerberus
import audit
import schema

OSM_PATH = "data/beijing_china.osm"

NODES_PATH = "data/nodes.csv"
NODE_TAGS_PATH = "data/nodes_tags.csv"
WAYS_PATH = "data/ways.csv"
WAY_NODES_PATH = "data/ways_nodes.csv"
WAY_TAGS_PATH = "data/ways_tags.csv"

LOWER_COLON = re.compile(r'^([a-z]|_)+:([a-z]|_)+')
PROBLEMCHARS = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')

SCHEMA = schema.schema
NODE_FIELDS = schema.NODE_FIELDS
NODE_TAGS_FIELDS = schema.NODE_TAGS_FIELDS
WAY_FIELDS = schema.WAY_FIELDS
WAY_TAGS_FIELDS = schema.WAY_TAGS_FIELDS
WAY_NODES_FIELDS = schema.WAY_NODES_FIELDS

def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
                  problem_chars=PROBLEMCHARS, default_tag_type='regular'):
    """Clean and shape node or way XML element to Python dict"""

    node_attribs = {}
    way_attribs = {}
    way_nodes = []
    tags = []  # Handle secondary tags the same way for both node and way elements

    for tag_elem in element.iter('tag'):
        tag = {}
        tag['id'] = element.attrib['id']
        if PROBLEMCHARS.match(tag_elem.attrib['k']):
            continue
        elif LOWER_COLON.match(tag_elem.attrib['k']):
            ind = tag_elem.attrib['k'].find(':')
            tag['key'] = tag_elem.attrib['k'][ind+1:]
            tag['type'] = tag_elem.attrib['k'][0:ind]
            if tag_elem.attrib['k'] == "name:en":
                tag['value'] = audit.update_way_names(tag_elem.attrib['v'], audit.mapping)
            elif tag_elem.attrib['k'] == "addr:postcode":
                if audit.check_postcode(tag_elem.attrib['v']):
                    tag['value'] = tag_elem.attrib['v']
                else:
                    continue
            else:
                tag['value'] = tag_elem.attrib['v']
        else:
            tag['key'] = tag_elem.attrib['k']
            tag['value'] = tag_elem.attrib['v']
            tag['type'] = default_tag_type
        tags.append(tag)

    if element.tag == 'node':
        for attr in node_attr_fields:
            node_attribs[attr] = element.attrib[attr]

        return {'node': node_attribs, 'node_tags': tags}

    elif element.tag == 'way':
        for attr in way_attr_fields:
            way_attribs[attr] = element.attrib[attr]

        i = 0
        for nd_elem in element.iter('nd'):
            way_node = {}
            way_node['id'] = element.attrib['id']
            way_node['node_id'] = nd_elem.attrib['ref']
            way_node['position'] = i
            i = i + 1
            way_nodes.append(way_node)

        return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}

# ================================================== #
#               Helper Functions                     #
# ================================================== #
def get_element(osm_file, tags=('node', 'way', 'relation')):
    """Yield element if it is the right type of tag"""

    context = ET.iterparse(osm_file, events=('start', 'end'))
    _, root = next(context)
    for event, elem in context:
        if event == 'end' and elem.tag in tags:
            yield elem
            root.clear()

def validate_element(element, validator, schema=SCHEMA):
    """Raise ValidationError if element does not match schema"""
    if validator.validate(element, schema) is not True:
        field, errors = next(validator.errors.iteritems())
        message_string = "\nElement of type '{0}' has the following errors:\n{1}"
        error_string = pprint.pformat(errors)

        raise Exception(message_string.format(field, error_string))

class UnicodeDictWriter(csv.DictWriter, object):
    """Extend csv.DictWriter to handle Unicode input"""

    def writerow(self, row):
        super(UnicodeDictWriter, self).writerow({
            k: (v.encode('utf-8') if isinstance(v, unicode) else v) for k, v in row.iteritems()
        })

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

# ================================================== #
#               Main Function                        #
# ================================================== #
def process_map(file_in, validate):
    """Iteratively process each XML element and write to csv(s)"""

    with codecs.open(NODES_PATH, 'w') as nodes_file, \
         codecs.open(NODE_TAGS_PATH, 'w') as nodes_tags_file, \
         codecs.open(WAYS_PATH, 'w') as ways_file, \
         codecs.open(WAY_NODES_PATH, 'w') as way_nodes_file, \
         codecs.open(WAY_TAGS_PATH, 'w') as way_tags_file:

        nodes_writer = UnicodeDictWriter(nodes_file, NODE_FIELDS)
        node_tags_writer = UnicodeDictWriter(nodes_tags_file, NODE_TAGS_FIELDS)
        ways_writer = UnicodeDictWriter(ways_file, WAY_FIELDS)
        way_nodes_writer = UnicodeDictWriter(way_nodes_file, WAY_NODES_FIELDS)
        way_tags_writer = UnicodeDictWriter(way_tags_file, WAY_TAGS_FIELDS)

        nodes_writer.writeheader()
        node_tags_writer.writeheader()
        ways_writer.writeheader()
        way_nodes_writer.writeheader()
        way_tags_writer.writeheader()

        validator = cerberus.Validator()

        for element in get_element(file_in, tags=('node', 'way')):
            el = shape_element(element)
            if el:
                if validate is True:
                    validate_element(el, validator)

                if element.tag == 'node':
                    nodes_writer.writerow(el['node'])
                    node_tags_writer.writerows(el['node_tags'])
                elif element.tag == 'way':
                    ways_writer.writerow(el['way'])
                    way_nodes_writer.writerows(el['way_nodes'])
                    way_tags_writer.writerows(el['way_tags'])

if __name__ == '__main__':
    # Note: Validation is ~ 10X slower. For the project consider using a small
    # sample of the map when validating.
    process_map(OSM_PATH, validate=False)


# -*- coding: utf-8 -*-
'''
    :codeauthor: Rahul Handay <rahulha@saltstack.com>
'''

import os

from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
    MagicMock,
    patch,
    NO_MOCK,
    NO_MOCK_REASON
)

import hubblestack.modules.systemd_service as systemd
import hubblestack.utils.systemd
from hubblestack.exceptions import CommandExecutionError

_SYSTEMCTL_STATUS = {
    'sshd.service': {
        'stdout': '''\
* sshd.service - OpenSSH Daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)''',
        'stderr': '',
        'retcode': 3,
        'pid': 12345,
    },

    'foo.service': {
        'stdout': '''\
* foo.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)''',
        'stderr': '',
        'retcode': 3,
        'pid': 12345,
    },
}

# This reflects systemd >= 231 behavior
_SYSTEMCTL_STATUS_GTE_231 = {
    'bar.service': {
        'stdout': 'Unit bar.service could not be found.',
        'stderr': '',
        'retcode': 4,
        'pid': 12345,
    },
}

_LIST_UNIT_FILES = '''\
service1.service                           enabled
service2.service                           disabled
service3.service                           static
timer1.timer                               enabled
timer2.timer                               disabled
timer3.timer                               static'''

@skipIf(NO_MOCK, NO_MOCK_REASON)
class SystemdTestCase(TestCase, LoaderModuleMockMixin):
    '''
    Test case for hubblestack.modules.systemd
    '''
    def setup_loader_modules(self):
        return {systemd: {}}

    def test_systemctl_reload(self):
        '''
            Test to Reloads systemctl
        '''
        mock = MagicMock(side_effect=[
            {'stdout': 'Who knows why?',
             'stderr': '',
             'retcode': 1,
             'pid': 12345},
            {'stdout': '',
             'stderr': '',
             'retcode': 0,
             'pid': 54321},
        ])
        with patch.dict(systemd.__mods__, {'cmd.run_all': mock}):
            self.assertRaisesRegex(
                CommandExecutionError,
                'Problem performing systemctl daemon-reload: Who knows why?',
                systemd.systemctl_reload
            )
            self.assertTrue(systemd.systemctl_reload())

    def test_get_all(self):
        '''
        Test to return a list of all available services
        '''
        listdir_mock = MagicMock(side_effect=[
            ['foo.service', 'multi-user.target.wants', 'mytimer.timer'],
            [],
            ['foo.service', 'multi-user.target.wants', 'bar.service'],
            ['mysql', 'nginx', 'README']
        ])
        access_mock = MagicMock(
            side_effect=lambda x, y: x != os.path.join(
                systemd.INITSCRIPT_PATH,
                'README'
            )
        )
        with patch.object(os, 'listdir', listdir_mock):
            with patch.object(os, 'access', side_effect=access_mock):
                self.assertListEqual(
                    systemd.get_all(),
                    ['bar', 'foo', 'mysql', 'mytimer.timer', 'nginx']
                )

    def test_available(self):
        '''
        Test to check that the given service is available
        '''
        mock = MagicMock(side_effect=lambda x: _SYSTEMCTL_STATUS[x])

        # systemd < 231
        with patch.dict(systemd.__context__, {'hubblestack.utils.systemd.version': 230}):
            with patch.object(systemd, '_systemctl_status', mock):
                self.assertTrue(systemd.available('sshd.service'))
                self.assertFalse(systemd.available('foo.service'))

        # systemd >= 231
        with patch.dict(systemd.__context__, {'hubblestack.utils.systemd.version': 231}):
            with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231):
                with patch.object(systemd, '_systemctl_status', mock):
                    self.assertTrue(systemd.available('sshd.service'))
                    self.assertFalse(systemd.available('bar.service'))

        # systemd < 231 with retcode/output changes backported (e.g. RHEL 7.3)
        with patch.dict(systemd.__context__, {'hubblestack.utils.systemd.version': 219}):
            with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231):
                with patch.object(systemd, '_systemctl_status', mock):
                    self.assertTrue(systemd.available('sshd.service'))
                    self.assertFalse(systemd.available('bar.service'))


import socket
from rdt_prova import RDT
from rdt import Server

# cria o socket UDP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', 5000)
server_socket.bind(server_address)

sv = Server(server_socket)

while True:
    mensagem, cliente = sv.receber()
    print(f' O cliente {cliente} disse {mensagem}')
    sv.enviar(f'Você disse: {mensagem}', cliente)


import re

def palindrome_checker(str_eval):
    removals = '[\W|_]'
    store = re.sub(removals, '', str_eval)
    store2 = store[::-1]
    print(store2)
    print(store)
    if store2 == store:
        print('The string entered is a palindrome!')
        return True
    else:
        print('The string entered is not a palindrome')
        return False

while True:
    value1 = input('Welcome! Do you want to check for a possible Palindrome? Y/N: ')
    value1= value1.lower()
    if value1 == 'n':
        print('Thanks for using this program! See you next time')
        break
    elif value1 == 'y':
        value2 = input('Please, enter the characters to check: ')
        palindrome_checker(value2)
    else:
        print('Please answer Y for "Yes", or N for "No"')
        continue


import numpy as np

global scale
scale = 1

""" objective_function_grad.py is used to obtain the objective function gradients values. The values are hard-coded """

def rosenbrock(state):
    x = state[0]
    y = state[1]*scale
    # Return the rosenbrock function and its gradient for the given ranges of x and y
    grad = np.array([-2 * (1 - x) - 400 * x * (y - x**2), 200 * (y - x**2)])    # gradient of the function
    return grad

def ackley(state):
    x = state[0]
    y = state[1]*scale
    # Return the ackley function and its gradient for the given ranges of x and y
    grad = np.array([-0.4 * x * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) + 0.5 * np.pi * np.sin(2 * np.pi * x) * np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))), -0.4 * y * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) + 0.5 * np.pi * np.sin(2 * np.pi * y) * np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))])    # gradient of the function
    return grad

def booth(state):
    x = state[0]
    y = state[1]*scale
    grad = np.array([2 * (x + 2 * y - 7) + 4 * (2 * x + y - 5), 4 * (x + 2 * y - 7) + 2 * (2 * x + y - 5)])    # gradient of the function
    return grad

def styblinski_tang_2d(state):
    x = state[0]
    y = state[1]
    grad = np.array([x**3 - 16 * x + 5, y**3 - 16 * y + 5])    # gradient of the function
    return grad


#!/usr/bin/env python
import argparse

import numpy as np
import os
import sys

from taiyaki import alphabet, bio, fast5utils, helpers, prepare_mapping_funcs
from taiyaki.cmdargs import FileExists, Maybe
from taiyaki.common_cmdargs import add_common_command_args
from taiyaki.iterators import imap_mp

program_description = "Prepare data for model training and save to hdf5 file by remapping with flip-flop model"
parser = argparse.ArgumentParser(description=program_description,
                                 formatter_class=argparse.ArgumentDefaultsHelpFormatter)

add_common_command_args(parser, 'alphabet input_folder input_strand_list jobs limit overwrite recursive version'.split())

parser.add_argument('--localpen', metavar='penalty', default=0.0, type=float,
                    help='Penalty for local mapping')
parser.add_argument('--max_read_length', metavar='bases', default=None, type=Maybe(int),
                    help='Don\'t attempt remapping for reads longer than this')
parser.add_argument('--mod', nargs=3, metavar=('base', 'canonical', 'name'),
                    default=[], action='append',
                    help='Modified base description')
parser.add_argument('input_per_read_params', action=FileExists,
                    help='Input per read parameter .tsv file')
parser.add_argument('output', help='Output HDF5 file')
parser.add_argument('model', action=FileExists, help='Taiyaki model file')
parser.add_argument('references', action=FileExists,
                    help='Single fasta file containing references for each read')

def main():
    """Main function to process mapping for each read using functions in prepare_mapping_funcs"""
    args = parser.parse_args()
    print("Running prepare_mapping using flip-flop remapping")

    if not args.overwrite:
        if os.path.exists(args.output):
            print("Cowardly refusing to overwrite {}".format(args.output))
            sys.exit(1)

    # Create alphabet and check for consistency
    modified_bases = [elt[0] for elt in args.mod]
    canonical_bases = [elt[1] for elt in args.mod]
    for b in modified_bases:
        assert len(b) == 1, "Modified bases must be a single character, got {}".format(b)
        assert b not in args.alphabet, "Modified base must not be a canonical base, got {}".format(b)
    for b in canonical_bases:
        assert len(b) == 1, "Canonical coding for modified bases must be a single character, got {}".format(b)
        assert b in args.alphabet, "Canonical coding for modified base must be a canonical base, got {}".format(b)
    full_alphabet = args.alphabet + ''.join(modified_bases)
    flat_alphabet = args.alphabet + ''.join(canonical_bases)
    modification_names = [elt[2] for elt in args.mod]

    alphabet_info = alphabet.AlphabetInfo(full_alphabet, flat_alphabet,
                                          modification_names, do_reorder=True)

    print("Converting references to labels using {}".format(str(alphabet_info)))

    # Make an iterator that yields all the reads we're interested in.
    fast5_reads = fast5utils.iterate_fast5_reads(
        args.input_folder, limit=args.limit, strand_list=args.input_strand_list,
        recursive=args.recursive)

    # Set up arguments (kwargs) for the worker function for each read
    kwargs = {}
    kwargs['per_read_params_dict'] = prepare_mapping_funcs.get_per_read_params_dict_from_tsv(
        args.input_per_read_params)
    kwargs['model'] = helpers.load_model(args.model)
    kwargs['alphabet_info'] = alphabet_info
    kwargs['max_read_length'] = args.max_read_length
    kwargs['localpen'] = args.localpen

    # remaps a single read using flip-flip network
    workerFunction = prepare_mapping_funcs.oneread_remap

    def iter_jobs():
        references = bio.fasta_file_to_dict(args.references, alphabet=full_alphabet)
        for fn, read_id in fast5_reads:
            yield fn, read_id, references.get(read_id, None)

    if args.limit is not None:
        chunksize = args.limit // (2 * args.jobs)
        chunksize = int(np.clip(chunksize, 1, 50))
    else:
        chunksize = 50

    results = imap_mp(workerFunction, iter_jobs(), threads=args.jobs,
                      fix_kwargs=kwargs, unordered=True, chunksize=chunksize)

    # results is an iterable of dicts
    # each dict is a set of return values from a single read
    prepare_mapping_funcs.generate_output_from_results(
        results, args.output, alphabet_info)

if __name__ == '__main__':
    main()


from __future__ import annotations

from typing import Any

import pandas as pd
import pytest

from tests.utils import convert_series_to_pandas_numpy
from tests.utils import integer_dataframe_1
from tests.utils import integer_dataframe_7
from tests.utils import interchange_to_pandas

@pytest.mark.parametrize(
    ("comparison", "expected_data"),
    [
        ("__eq__", [True, True, False]),
        ("__ne__", [False, False, True]),
        ("__ge__", [True, True, False]),
        ("__gt__", [False, False, False]),
        ("__le__", [True, True, True]),
        ("__lt__", [False, False, True]),
        ("__add__", [2, 4, 7]),
        ("__sub__", [0, 0, -1]),
        ("__mul__", [1, 4, 12]),
        ("__truediv__", [1, 1, 0.75]),
        ("__floordiv__", [1, 1, 0]),
        ("__pow__", [1, 4, 81]),
        ("__mod__", [0, 0, 3]),
    ],
)
def test_column_comparisons(
    library: str,
    comparison: str,
    expected_data: list[object],
) -> None:
    ser: Any
    df = integer_dataframe_7(library)
    ser = df.get_column_by_name("a")
    other = df.get_column_by_name("b")
    result = df.insert(0, "result", (getattr(ser, comparison)(other)))
    result_pd = interchange_to_pandas(result, library)["result"]
    expected = pd.Series(expected_data, name="result")
    result_pd = convert_series_to_pandas_numpy(result_pd)
    pd.testing.assert_series_equal(result_pd, expected)

@pytest.mark.parametrize(
    ("comparison", "expected_data"),
    [
        ("__eq__", [False, False, True]),
        ("__ne__", [True, True, False]),
        ("__ge__", [False, False, True]),
        ("__gt__", [False, False, False]),
        ("__le__", [True, True, True]),
        ("__lt__", [True, True, False]),
        ("__add__", [4, 5, 6]),
        ("__sub__", [-2, -1, 0]),
        ("__mul__", [3, 6, 9]),
        ("__truediv__", [1 / 3, 2 / 3, 1]),
        ("__floordiv__", [0, 0, 1]),
        ("__pow__", [1, 8, 27]),
        ("__mod__", [1, 2, 0]),
    ],
)
def test_column_comparisons_scalar(
    library: str,
    comparison: str,
    expected_data: list[object],
) -> None:
    ser: Any
    df = integer_dataframe_1(library)
    ser = df.get_column_by_name("a")
    other = 3
    result = df.insert(0, "result", (getattr(ser, comparison)(other)))
    result_pd = interchange_to_pandas(result, library)["result"]
    expected = pd.Series(expected_data, name="result")
    result_pd = convert_series_to_pandas_numpy(result_pd)
    pd.testing.assert_series_equal(result_pd, expected)

def test_invalid_comparison() -> None:
    df1 = integer_dataframe_1("polars-lazy")
    df2 = integer_dataframe_1("polars-lazy")
    with pytest.raises(ValueError):
        _ = df1.get_column_by_name("a") > df2.get_column_by_name("a")


#ask user what items to place on their to-do list
#append items to list
#dispay items
#if done, mark them as done and display remaining items on list

import tkinter
from tkinter import Label, StringVar, filedialog
from tkinter.font import Font
import pickle
from datetime import datetime

""" def time_now():
    string = time.strftime(f'%H:%M:%S %p')
    display_time.config(text=string)
    display_time.after(1000, time_now) """

root = tkinter.Tk()
root.title("To-Do List")
root.geometry("500x500")

my_menu = tkinter.Menu(root)
root.config(menu=my_menu)

def save_list():
    items = task_list.get(0, tkinter.END)
    file_name = filedialog.asksaveasfilename(
        initialdir="C:/Udacity/python/data",
        title="Save File",
        filetypes=(("Dat Files", "*.dat"),
                    ("All Files", "*.*"))
    )
    if file_name:
        if file_name.endswith(".dat"):
            pass
        else:
            file_name = f'{file_name}.dat'

    output_file = open(file_name, 'wb')
    pickle.dump(items, output_file)

def open_list():
    file_name = filedialog.askopenfilename(
            initialdir="C:/Udacity/python/data",
        title="Open File",
        filetypes=(("Dat Files", "*.dat"),
                    ("All Files", "*.*"))
    )

    if file_name:
        task_list.delete(0, tkinter.END)
        input_file = open(file_name, 'rb')
        tasks = pickle.load(input_file)
        for item in tasks:
            task_list.insert(tkinter.END, item)

def delete_crossed():
    count = 0
    while count < task_list.size():
        if task_list.itemcget(count, "fg") == "#dedede":
            task_list.delete(task_list.index(count))
        else:
            count += 1

def clear_list():
    task_list.delete(0, tkinter.END)

def change_color():
    task_list.config(bg=radio.get())

file_menu = tkinter.Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Save List", command=save_list)
file_menu.add_command(label="Open List", command=open_list)
file_menu.add_separator()
file_menu.add_command(label="Delete Crossed Items", command=delete_crossed)
file_menu.add_command(label="Clear List", command=clear_list)
file_menu.add_command(label="Exit", command=root.quit)

radio = StringVar()
radio.set("#FEFFCC")

color_menu = tkinter.Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Color", menu=color_menu)
color_menu.add_radiobutton(label="white", background="white", variable=radio, value="white", command=change_color)
color_menu.add_radiobutton(label="yellow", state="active", background="#FEFFCC", variable=radio, value="#FEFFCC", command=change_color)
color_menu.add_radiobutton(label="pink", background="#FFE5F0",variable=radio, value="#FFE5F0", command=change_color)
color_menu.add_radiobutton(label="mint", background="#EBFFF6", variable=radio, value="#EBFFF6", command=change_color)
color_menu.add_radiobutton(label="purple", background="#E9D3FF", variable=radio, value="#E9D3FF", command=change_color)

my_font = Font(
    family="Lucida Calligraphy italic",
    size=28,
    weight="bold"
)

date_frame = tkinter.Frame(root)
date_frame.pack()

today = datetime.now()
date = Label(date_frame, text=f'{today:%A, %B %d, %Y}', font="Arial, 11", fg="#A9A9A9")
date.pack()

my_frame = tkinter.Frame(root)
my_frame.pack()

task_list = tkinter.Listbox(my_frame,
    font=my_font,
    width=25,
    height=5,
    bg="#FEFFCC",
    bd=0,
    fg="#464646",
    highlightthickness=0,
    selectbackground="#C2E5D3",
    activestyle="none"
)
task_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)

my_scrollbar = tkinter.Scrollbar(my_frame)
my_scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.BOTH)

task_list.config(yscrollcommand=my_scrollbar.set)
my_scrollbar.config(command=task_list.yview)

def add_task():
    task = my_entry.get()
    if len(task) > 0:
        task_list.insert(tkinter.END, task)
    my_entry.delete(0, tkinter.END)

def task_done():
    task_list.itemconfig(task_list.curselection(), fg="#dedede")
    task_list.selection_clear(0, tkinter.END)

def delete_task():
    task_list.delete(tkinter.ANCHOR)

def uncross():
    task_list.itemconfig(task_list.curselection(), fg="#464646")
    task_list.selection_clear(0, tkinter.END)

my_entry = tkinter.Entry(root, font="Helvetica, 20", width=26)
my_entry.pack(pady=20)

button_frame = tkinter.Frame(root)
button_frame.pack(pady=20)

add = tkinter.Button(button_frame, text="Add", padx=20, pady=5, command=add_task).grid(row=0, column=0)
cross_off = tkinter.Button(button_frame, text="Cross Off Item", padx=20, pady=5, command=task_done).grid(row=0, column=2)
uncross_off = tkinter.Button(button_frame, text="Uncross Item", padx=20, pady=5, command=uncross).grid(row=0, column=3, pady=10)
delete = tkinter.Button(button_frame, text="Delete", padx=20, pady=5, command=delete_task).grid(row=0, column=1, pady=10)

#today = tkinter.Label(root, text=f"{date:%A, %B %d, %Y}", font="Calibri, 15", justify='center')
#today.grid(row=0, pady=10, columnspan=2)

root.mainloop()


from abc import ABC
from logging import getLogger

import numpy as np
import torch
import torch.nn as nn

from tips.utils.basic_utils import set_color, ModelType

class BaseGNNRecommender(nn.Module):
    def __init__(self):
        self.logger = getLogger()
        super(BaseGNNRecommender, self).__init__()

    def calculate_loss(self, interaction):
        raise NotImplementedError

    def predict(self, interaction):
        raise NotImplementedError

    def full_sort_predict(self, interaction):
        raise NotImplementedError

    def other_parameter(self):
        if hasattr(self, "other_parameter_name"):
            return {key: getattr(self, key) for key in self.other_parameter_name}
        return dict()

    def load_other_parameter(self, para):
        if para is None:
            return
        for key, value in para.items():
            setattr(self, key, value)

    def __str__(self):
        model_parameters = filter(lambda p: p.requires_grad, self.parameters())
        params = sum([np.prod(p.size()) for p in model_parameters])
        return (
                super().__str__()
                + set_color("\nTrainable parameters", "blue")
                + f": {params}"
        )

class GenericGNNRecommender(BaseGNNRecommender, ABC):
    type = ModelType.GENERAL

    def __init__(self, config, dataset):
        super(GenericGNNRecommender, self).__init__()

        # load dataset info
        self.USER_ID = config["USER_ID_FIELD"]
        self.ITEM_ID = config["ITEM_ID_FIELD"]
        self.NEG_ITEM_ID = config["NEG_PREFIX"] + self.ITEM_ID
        self.n_users = dataset.num(self.USER_ID)
        self.n_items = dataset.num(self.ITEM_ID)

        # load parameters info
        self.device = config["device"]

class GeneralGraphRecommender(GenericGNNRecommender, ABC):
    type = ModelType.GENERAL

    def __init__(self, config, dataset):
        super(GeneralGraphRecommender, self).__init__(config, dataset)
        self.edge_index, self.edge_weight = dataset.get_norm_adj_mat()
        self.edge_index, self.edge_weight = self.edge_index.to(self.device), self.edge_weight.to(self.device)


try:
    num=int(input('ingrese un nuero para dividir entre 100  :'))
    if num!=0:
        print(100/num)
    else:
        print('no puedes dividir por el 0')
except Exception as e:
    print(e)

#################################################################
    #<-- sdsds -->


'''Exercise : how many'''
# write a procedure, called how_many, which returns the
#sum of the number of values associated with a dictionary.

def how_many(animals):
    '''
    aDict: A dictionary, where all the values are lists.

    returns: int, how many values are in the dictionary.
    '''
    # Your Code Here
    sum1 = 0
    values = animals.values()
    for words in values:
        sum1 = sum1 + len(words)
    return sum1

def main():
    '''dist'''
    number = input()
    animals = {}
    for i in range(int(number)):
        entries = input()
        list1 = entries.split()
        if list1[0][0] not in animals:
            animals[list1[0][0]] = [list1[1]]
        else:
            animals[list1[0][0]].append(list1[1])
    #animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}

    #animals['d'] = ['donkey']
    #animals['d'].append('dog')
    #animals['d'].append('dingo')
    print(animals)
    print(how_many(animals))

if __name__ == "__main__":
    main()


import os
import glob
import torch.distributed as dist
import torch
from monai.data import Dataset, DataLoader, DistributedSampler
# from numba import cuda
from GPUtil import showUtilization as gpu_usage
from collections import OrderedDict

#
# def prepare(dataset,
#             rank,
#             world_size,
#             batch_size,
#             pin_memory=False,
#             num_workers=0):
#     sampler = DistributedSampler(
#         dataset,
#         num_replicas=world_size,
#         rank=rank,
#         shuffle=False,
#         drop_last=False)
#     dataloader=DataLoader(dataset,
#                           batch_size=batch_size,
#                           pin_memory=pin_memory,
#                           num_workers=num_workers,
#                           drop_last=False,
#                           shuffle=False,
#                           sampler=sampler
#                           )
#     return dataloader
#
#
# def free_gpu_cache():
#     print("Initial GPU Usage")
#     gpu_usage()
#
#     torch.cuda.empty_cache()
#
#     cuda.select_device(0)
#     cuda.close()
#     cuda.select_device(0)
#
#     print("GPU Usage after emptying the cache")
#     gpu_usage()

#
# def setup(rank, world_size):
#     os.environ['MASTER_ADDR'] = 'localhost'
#     os.environ['MASTER_PORT'] = '12355'
#     os.environ['WORLD_SIZE'] = '2'
#     os.environ['LOCAL RANK'] = str(rank)
#     dist.init_process_group("nccl", rank=rank, world_size=world_size)
#
#
# def cleanup():
#     dist.destroy_process_group()
#
#
# def ddp_state_dict(model_path):
#     # original saved file with DataParallel
#     state_dict = torch.load(model_path)
#     # create new OrderedDict that does not contain `module.`
#     new_state_dict = OrderedDict()
#     for k, v in state_dict.items():
#         name = k[7:]  # remove `module.`
#         new_state_dict[name] = v
#     # load params
#     return(new_state_dict)
#
#
# class DDPSetUp():
#     def __init__(self,
#                 rank,
#                 world_size,
#                 ):
#         self.cleanup = dist.destroy_process_group()
#         os.environ['MASTER_ADDR'] = 'localhost'
#         os.environ['MASTER_PORT'] = '12355'
#         os.environ['WORLD_SIZE'] = '2'
#         os.environ['LOCAL RANK'] = str(rank)
#         self.setup = dist.init_process_group("nccl", rank=rank, world_size=world_size)
#

class BuildDataset():
    def __init__(self, directory, string):
        images = sorted(
            glob.glob(os.path.join(directory, string, 'images', '*.nii.gz'))
        )
        labels = sorted(
            glob.glob(os.path.join(directory, string, 'masks', '*.nii.gz'))
        )
        nccts = sorted(
            glob.glob(os.path.join(directory, string, 'ncct', '*.nii.gz'))
        )
        self.images_dict = [
            {"image": image_name, "label": label_name}
            for image_name, label_name in zip(images, labels)
        ]
        self.no_seg_dict = [
            {"image": image_name}
            for image_name in images
                            ]

        self.no_seg_ncct_dict = [
            {"image": image_name, "ncct": ncct_name}
            for image_name, ncct_name in zip(images, nccts)
                            ]
        self.ncct_dict = [
            {"image": image_name, "ncct":ncct_name, "label": label_name}
            for image_name, ncct_name, label_name in zip(images, nccts, labels)
        ]


info = [
    {'dni': 70522066, 'nombre': 'raul', 'apellido': 'quijua', 'sexo': 'Masculino', 'ciudad': 'lima'},
    {'dni': 87654321, 'nombre': 'jideki', 'apellido': 'torres', 'sexo': 'Masculino', 'ciudad': 'tumbes'},
    {'dni': 12345678, 'nombre': 'marco', 'apellido': 'torner', 'sexo': 'Masculino', 'ciudad': 'callao'},
    {'dni': 11111111, 'nombre': 'rubi', 'apellido': 'butstrap', 'sexo': 'Femenino', 'ciudad': 'arequipa'}
]

dni = int(input('Ingrese DNI: '))

dni_encontrado = 0
for persona in info:
    if persona['dni'] == dni:
        dni_encontrado = persona
        break

if dni_encontrado is not None:
    print(f'El dato DNI {dni} fue encontrado en el sistema. Datos de la persona:')
    print(f'Nombre: {dni_encontrado["nombre"]}')
    print(f'Apellido: {dni_encontrado["apellido"]}')
    print(f'Sexo: {dni_encontrado["sexo"]}')
    print(f'Ciudad: {dni_encontrado["ciudad"]}')
else:
    print(f'El dato DNI {dni} NO fue encontrado en el sistema.')


'''
my approach is to recursive traverse the tree if it is not root return none.
and declare a local variable to record the length
'''

'''
The actual apparoach is to use recursion to get the maximum length each time and at the end when return it, we just return the maximum
'''
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        left_length = self.maxDepth(root.left)
        right_length = self.maxDepth(root.right)
        return max(left_length,right_length)+1


from flask import Flask,render_template,url_for,redirect,request,jsonify
from flask_mail import Mail,Message
import Text_summarization
from werkzeug.utils import secure_filename
# from allennlp.predictors.predictor import Predictor
from summarizer import Summarizer
import os
import re
import docx
import PyPDF2
from striprtf.striprtf import rtf_to_text
from pptx import Presentation
import json

# Make a regular expression
# for validating an Email
regex = "^[a-z0-9]+[\\._]?[a-z0-9]+[@]\\w+[.]\\w{2,3}$"

#app configurations
application=app=Flask(__name__)
app.config['UPLOAD_FOLDER']= 'uploads/'
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT']=587
app.config['MAIL_USE_TLS']=True
app.config['MAIL_USE_SSL']=False
app.config['MAIL_USERNAME']='aashima0809@gmail.com'
app.config['MAIL_PASSWORD']=os.environ.get('password')
app.config['MAIL_DEFAULT_SENDER']='aashima0809@gmail.com'

mail=Mail(app)

text=''
text_summary=''
pred_summary=''
name=''

# predictor = Predictor.from_path("static/bidaf-model-2020.03.19.tar.gz")

def readfile(flag):
	global text
	global text_summary
	global name
	File=''

	#loads file from
	if(flag==0):
		File=request.files['filename']
	else:
		File=request.files['file_summary']

	print(File)

	#contains filename
	fn=os.path.basename(File.filename)
	fn=fn.replace(" ", "_")
	print(fn)

	#saving file in specified directory
	File.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(File.filename)))

	#contains path where file is saved
	File=str(os.path.join(app.config['UPLOAD_FOLDER'])+fn)

	#dividing the file location into its filename and extension
	name, ext =os.path.splitext(File)

	#checking whether the file is videofile or not
	allowed_extensions=[".doc",".docx",".pdf",".rtf",".pptx",".txt"]
	if (ext.lower() in allowed_extensions)==False:
		return "extension_error"

	if(ext.lower()=='.txt'):
		try:
			f = open(File, "r")
			data = f.read()
		except Exception:
			return "Something_went_wrong"

	if(ext.lower()=='.docx' or ext.lower()=='.doc'):
		try:
			doc = docx.Document(File)  # Creating word reader object.
			data = ""
			fullText = []
			for para in doc.paragraphs:
				fullText.append(para.text)
				data = '\n'.join(fullText)
		except Exception:
			return "Something_went_wrong"

	if(ext.lower()=='.pptx'):
		try:
			prs = Presentation(File)
			data = ""
			fullText = []
			for slide in prs.slides:
				for shape in slide.shapes:
					if hasattr(shape, "text"):
						fullText.append(shape.text)
				data = '\n'.join(fullText)
		except Exception:
			return "Something_went_wrong"

	if(ext.lower()=='.pdf'):
		try:
			pdfFileObj = open(File, mode='rb')    # creating a pdf file object
			pdfReader = PyPDF2.PdfFileReader(pdfFileObj)    # creating a pdf reader object
			number_of_pages=pdfReader.numPages    #counting number of pages
			data=""
			fullText=[]
			for i in range(0,number_of_pages):
				pageObj = pdfReader.getPage(i)    # a page object
				fullText.append(pageObj.extractText())   # extracting text from pdf
			data=' '.join(fullText)
		except Exception:
			return "Something_went_wrong"

	if(ext.lower()=='.rtf'):
		try:
			f = open(File, 'r')
			rtf_text=f.read()
			data = rtf_to_text(rtf_text)
		except Exception:
			return "Something_went_wrong"

	# flag=0 -> Original_File
	# flag=1 -> Summary_File
	if(flag==0):
		text=data
	else:
		text_summary=data
	return "successfully_read"

@app.route('/',methods=["GET","POST"])
def dashboard():
    return render_template('index.html')

@app.route('/email',methods=["GET","POST"])
def email():
	#retrieves email ID from html
	global pred_summary
	global name

	recipient_id=str(request.form.get("email"))

	#if invalid email is provided
	if not(re.search(regex,recipient_id)):
		return render_template("summarize.html",text="Invalid Email Id")

	name=name[8:]
	print(name)
	#Summary file name
	Summary_file="summary_"+name+".txt"
	file1 = open(Summary_file, "w+")
	file1.write(pred_summary)
	file1.close()

	try:
		#Message Content that is to be emailed
		msg=Message(
			subject='Summary file',
			recipients=[recipient_id],
			body='Below is your required summary'
			)

		#attaching summary file
		with app.open_resource(Summary_file) as output_file:
			msg.attach(Summary_file,'text/plain',output_file.read())

		#sending mail
		mail.send(msg)

		return render_template("summarize.html",text="Successfully mailed summary to your provided email id")

	except Exception:
		return render_template("summarize.html",text="Sorry!!! Unable to send the email")

@app.route('/summarize',methods=["GET","POST"])
def summarize():
	global pred_summary

	if request.method=="POST":
		radio_butt=request.form.get("optradio")
		print(radio_butt)
		output=readfile(0)
		if(output=="extension_error"):
			return render_template("summarize.html",summary_output="Not a text file")

		if(output=="Something_went_wrong"):
			return render_template("summarize.html",summary_output="Sorry!!! Something_went_wrong")

		if(radio_butt=="bert"):
			model = Summarizer()
			pred_summary = model(text)
			return render_template("summarize.html",summary_output=pred_summary)
		else:
			pred_summary=Text_summarization.get_data(text,'',0)
			return render_template("summarize.html",summary_output=pred_summary)

	return render_template('summarize.html')

@app.route('/analyse',methods=["GET","POST"])
def analyse():
	global text
	global text_summary
	if request.method=="POST":

		output_text=readfile(0)
		output_summary=readfile(1)

		#if original file is not a text file
		if(output_text=="extension_error"):
			return render_template("analyse.html",analyse_output="Original file is not a text file")

		if(output_summary=="extension_error"):
			return render_template("analyse.html",analyse_output="summary file is not a text file")

		if(output_text=="Something_went_wrong" or output_summary=="Something_went_wrong"):
			return render_template("analyse.html",analyse_output="Sorry!!! Something_went_wrong")

		accuracy=Text_summarization.get_data(text,text_summary,1)
		return render_template("analyse.html",analyse_output=accuracy)

	return render_template('analyse.html')

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

@app.route('/file',methods=["POST"])
def file():
	global text

	output=readfile(0)

	if(output=="extension_error"):
		return render_template("quesans.html",qa_output="Not a text file")

	if(output=="successfully_read"):
		return render_template("quesans.html",qa_output="File uploaded successfully")

	if(output=="Something_went_wrong"):
		return render_template("quesans.html",qa_output="Sorry!!! Something_went_wrong")

# @app.route('/question',methods=["GET","POST"])
# def question():
# 	global text
# 	question=str(request.form.get('comment'))
# 	try:
# 		result=predictor.predict(
# 		  	passage=text,
# 	        question=question
# 		)
# 		answer=result['best_span_str']
# 		return render_template("quesans.html",question=question,answer=answer)
# 	except Exception:
# 		return render_template("quesans.html",answer="Sorry!!! Currently application is out of service")

@app.route('/MailMe', methods=['POST', 'GET'])
def MailMe():
	data= request.get_json(force = True)
	name=data.get("name")
	email=data.get("email")
	phone=data.get("phone")
	message=data.get("message")
	try:
		msg=Message(
			subject='Text Summarization Website Contact Form: %s'%(name),
			recipients=['aadarshgupta875@gmail.com'],
			body="You have received a new message from your website contact form.\n\n. Here are the details:\n\nName: {}\n\nEmail: {}\n\nPhone: {}\n\nMessage:\n{}".format(name,email,phone,message)
		)
		mail.send(msg)
		return json.dumps({'success':True})
	except Exception:
		return json.dumps({'error':True})

if __name__=="__main__":
	app.run()


import re
from collections import defaultdict
import numpy as np
import scipy.sparse as sp

from patterns import TOKEN_PATTERN, ID_PATTERN
from csr_featuriser import CSRFeaturiser

class TextAnalyser():

    def __init__(self):
        self.splitter = re.compile(TOKEN_PATTERN).findall

    def __call__(self, text):
        return self.splitter(text.lower())

class TagAnalyser():
    def __init__(self):
        self.splitter = re.compile(ID_PATTERN).findall
    def __call__(self, text):
        return self.splitter(text)

class Featuriser(CSRFeaturiser):
    """
    Featuriser for Recommender.
    Optionally performs tf-idf feature weighting.
    Transformed vectors are returned L1 normalised.
    """

    def __init__(self, analyzer, min_document_frequency, min_interaction_count,
                 feature_idf, dtype=np.float32):
        """
        Words that appear in less than `min_document_frequency` documents
        are discarded as well as those with document frequency times
        document `seen_count` smaller than `min_interaction_count`.
        """
        super().__init__(analyzer, vocab=None, dtype=dtype, binary=False)
        self.min_interaction_count = min_interaction_count
        self.min_document_frequency = min_document_frequency
        self.feature_idf = feature_idf

    def fit(self, id_document_iter, interaction_counts):
        """
        Build a vocabulary of unigrams by iterating over the (id, text) pairs
        of `id_document_iter`, keeping those unigrams that appear in at least
        `self.min_document_frequency` documents, the sum of whose interactions
        (as specified by the dict `interaction_counts`) is at least
        `self.min_interaction_count`.
        Fit also the IDF feature weights, if `self.feature_idf`.
        """
        vocab_df = defaultdict(lambda: 0)   # map word -> document frequency
        vocab_ic = defaultdict(lambda: 0)   # map word -> interaction count
        num_docs = 0

        for doc_id, doc in id_document_iter:
            doc_vocab = set()
            for word in self.analyzer(doc):
                doc_vocab.add(word)
            for word in doc_vocab:
                vocab_df[word] += 1
                vocab_ic[word] += interaction_counts[doc_id]
            num_docs += 1

        vocab_ic_filtered = [(word, ic) for word, ic in vocab_ic.items()
                             if vocab_df[word] >= self.min_document_frequency
                             and vocab_ic[word] >= self.min_interaction_count]

        self.vocab = [word for word, _ in sorted(vocab_ic_filtered,
                                                 key=lambda p: p[1],
                                                 reverse=True)]
        self.vocab_map = dict(zip(self.vocab, range(len(self.vocab))))

        if self.feature_idf:
            self.feature_weights = np.log([num_docs / vocab_df[word]
                                           for word in self.vocab]).astype(self.dtype)
        else:
            self.feature_weights = np.ones(len(self.vocab), dtype=self.dtype)
        return self

    def transform(self, id_document_iter):
        """
        Returns an IndexedSparseMatrix (wrapping a CSR matrix) with the
        documents ids as row labels giving the L1 normalisation of the (IDF
        weighted, if applicable) term frequency counts.
        """
        ism = super().transform(id_document_iter)
        if not len(self.feature_weights):
            # then ism has no columns, nothing more to do
            # (return, since sp.diags will fail)
            return ism
        # apply the feature weighting
        ism.M = ism.M * sp.diags(self.feature_weights, 0)
        # L1 normalise the rows (entries are non-negative)
        row_sums = np.array(ism.M.sum(axis=1))[:, 0]
        row_indices, _ = ism.M.nonzero()
        ism.M.data /= row_sums[row_indices]
        return ism


from flask import Flask, render_template, request
import requests
import os

app = Flask(__name__)

url = "https://spotify23.p.rapidapi.com/search/"

headers = {
	'X-RapidAPI-Key': os.environ.get('API_KEY'),
	'X-RapidAPI-Host': os.environ.get('API_HOST')
}

@app.route('/music/<artist_name>')
def music_home(artist_name):
    print(artist_name)
    querystring = {"q":artist_name,"type":"songs","offset":"0","limit":"6","numberOfTopResults":"1"}
    api_data = requests.request("GET", url, headers=headers, params=querystring).json()

    songs = []
    for entry in api_data['albums']['items']:
        song = entry['data']
        songs.append({
            'title' : song['name'],
            'artists': [artist['profile']['name'] for artist in song['artists']['items']],
            'url': 'https://open.spotify.com/album/' + song['uri'].split(':')[-1],
            'image': song['coverArt']['sources'][-1]['url'],
        })
        # Height & width 640px

    return songs

if __name__ == '__main__':
    app.run(debug=True)


import pandas as pd
import numpy as np
from io import StringIO
import math
import ast
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import statistics
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from scipy import stats
import matplotlib.pyplot as plt
import ruptures as rpt

import plotly.express as px

# Famous Players
nba_1 = pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_kingjames.csv", index_col=[0])
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_damianlillard.csv", index_col=[0]), ignore_index = True)
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_giannis_an34_ygtrece.csv", index_col=[0]), ignore_index = True)
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_kyrieirving_EasyMoneySniper_jharden13_cp3_zo.csv", index_col=[0]), ignore_index = True)
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_stephencurry30_russwest44.csv", index_col=[0]), ignore_index = True)
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_new_players_jordan_poole_jokicnikolaofficial_melo.csv", index_col=[0]), ignore_index = True)
nba_1 = nba_1.append(pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_new_players_jamorant_anthony.csv", index_col=[0]), ignore_index = True)
nba_1['posts'] = [ast.literal_eval(x) for x in nba_1['posts']]
nba_final_df = nba_1

import plotly.express as px

def df_maker_1(df, ma, momentum, entity):
  df_entity_posts = df.loc[df.insta_handle == entity]['posts'].item()
  df_entity_posts.reverse()

  df_list = []
  df_nan_list = []
  df_nan_2_list = []

  value_list = []
  value_list_nan = []
  value_list_nan_2 = []

  momentum_value = 0
  momentum_value_nan = ma

  count = 0
  index_count = 1
  for post in df_entity_posts:
    _, values = zip(*post.items())
    if (values[0]['type'] == 'photo') and (values[0]['post_date'] > '2020-05-10'):  # 2020-04-20
      df_dict = {}
      try:
        value_list.append(values[0]['likes'])
        if count >= momentum:
          df_dict['momentum'] = values[0]['likes'] - value_list[momentum_value]
          momentum_value += 1
        else:
          df_dict['momentum'] = np.nan
        count += 1
        df_dict['index'] = index_count
        df_dict['insta_handle'] = entity
        df_dict['date'] = values[0]['post_date']
        df_dict['date_month'] = values[0]['post_date']
        df_dict['likes'] = values[0]['likes']
        df_list.append(df_dict)
      except:
        likes_got = statistics.mean(value_list)
        value_list.append(likes_got)
        if count >= momentum:
          df_dict['momentum'] = likes_got - value_list[momentum_value]
          momentum_value += 1
        else:
          df_dict['momentum'] = np.nan
        count += 1
        df_dict['insta_handle'] = entity
        df_dict['date'] = values[0]['post_date']
        df_dict['date_month'] = values[0]['post_date']
        df_dict['likes'] = likes_got
        df_list.append(df_dict)

  df_new = pd.DataFrame(df_list).sort_values(by=['date'])
  df_new['date'] = pd.to_datetime(df_new['date'])
  df_new['date_month'] = pd.to_datetime(df_new['date_month'])
  df_new = df_new.set_index('date')

  df_ma = df_new.copy()
  df_ma['likes'] = df_ma['likes'].rolling(window=ma, min_periods = 10).mean() #

  df_resample = df_new.copy()
  df_resample = df_resample.groupby('insta_handle').resample('D').mean().fillna(0)
  df_resample['likes'] = df_resample['likes'].rolling(window=ma).mean()
  df_resample = df_resample.reset_index()

  df_resample_nan = df_new.copy()
  df_resample_nan = df_resample_nan.groupby('insta_handle').resample('D').mean().fillna(0)
  # print(df_resample_nan.head(20))
  # df_resample_nan['likes'] = df_resample_nan['likes'].rolling(window=ma).mean()
  df_resample_nan = df_resample_nan.reset_index()

  count_nan = 0
  for index, row in df_resample_nan.iterrows():
    df_nan_dict = {}
    value_list_nan.append(row['likes'])

    df_nan_dict['date'] = row['date']
    df_nan_dict['insta_handle'] = entity
    if count_nan >= ma:
      df_nan_dict['likes'] = (np.nan if (sum(value_list_nan[-(ma+1):])) == 0 else (sum(value_list_nan[-(ma+1):]))) / ma
      df_nan_dict['likes_nan'] = (np.nan if sum(value_list_nan[-(ma+1):]) == 0 else sum(value_list_nan[-(ma+1):])) / (1 if (ma - value_list_nan[-(ma+1):].count(0)) == 0 else (ma - value_list_nan[-(ma+1):].count(0)))  # ma+1 or just ma
    else:
      df_nan_dict['likes'] = np.nan
      df_nan_dict['likes_nan'] = np.nan

    count_nan += 1
    df_nan_list.append(df_nan_dict)
  df_resample_nan_2 = pd.DataFrame(df_nan_list).sort_values(by=['date'])
  df_resample_nan_2['date'] = pd.to_datetime(df_resample_nan_2['date'])

  count_nan_2 = 0
  for index, row_2 in df_resample_nan_2.iterrows():
    df_nan_2_dict = {}
    value_list_nan_2.append(row_2['likes_nan'])

    df_nan_2_dict['date'] = row_2['date']
    df_nan_2_dict['insta_handle'] = entity
    df_nan_2_dict['likes'] = row_2['likes']
    df_nan_2_dict['likes_nan'] = row_2['likes_nan']
    if (count_nan_2 >= (momentum+ma)):
      if not math.isnan(value_list_nan_2[-(momentum+momentum+1)]):
        df_nan_2_dict['momentum_absolute'] = row_2['likes_nan'] - value_list_nan_2[-(momentum+momentum+1)]
        # print(entity, row_2['date']  ,row_2['likes_nan'], value_list_nan_2[-(momentum+momentum+1)])
        df_nan_2_dict['momentum_relative'] = (row_2['likes_nan'] - ((0 if ((math.isnan(value_list_nan_2[-(momentum+momentum+1)])) or (value_list_nan_2[-(momentum+momentum+1)] == 0))  else value_list_nan_2[-(momentum+momentum+1)]))) / ((1 if ((math.isnan(value_list_nan_2[-(momentum+momentum+1)])) or (value_list_nan_2[-(momentum+momentum+1)] == 0))  else value_list_nan_2[-(momentum+momentum+1)]))
        momentum_value_nan += 1
    else:
      df_nan_2_dict['momentum_absolute'] = np.nan
      df_nan_2_dict['momentum_relative'] = np.nan

    count_nan_2 += 1
    value_list_nan_2.append(row_2['likes_nan'])
    df_nan_2_list.append(df_nan_2_dict)
  df_resample_nan_3 = pd.DataFrame(df_nan_2_list).sort_values(by=['date'])
  df_resample_nan_3['date'] = pd.to_datetime(df_resample_nan_3['date'])

  df_grouped = df_new.groupby([(df_new.date_month.dt.year), (df_new.date_month.dt.month)]).agg('count')

  return df_new, df_ma, df_resample, df_resample_nan_2, df_resample_nan_3, df_grouped

def plot_maker_1(df, ma, momentum, entity = False):
  if entity:
    df_new, df_ma, df_resample, df_resample_nan_2, df_resample_nan_3, df_grouped = df_maker_1(df, ma, momentum, entity)

    fig_1 = px.line(df_new, x=df_new.index, y="likes", title='Instagram Post Likes VS Date')
    fig_1.show()

    fig_2 = px.line(df_ma, x=df_ma.index, y="likes", title='Instagram Post Likes VS Date (RMA - post focused)')
    fig_2.show()

    fig_3 = px.line(df_resample, x='date', y="likes", title='Instagram Post Likes VS Date (RMA - Time focused)')
    fig_3.show()

    fig_4 = px.line(df_ma, x='date_month', y="momentum", title='Instagram Post Likes VS Date (momentum - post focused)')
    fig_4.show()

    fig_4_b = px.line(df_resample, x='date', y="momentum", title='Instagram Post Likes VS Date (momentum - Time focused)')
    fig_4_b.show()

    fig_5 = px.line(df_resample_nan_2, x='date', y="likes_nan", title='Instagram Post Likes VS Date (RMA - considers nan, Time period focused)')
    fig_5.show()

    fig_6 = px.histogram(df_ma, x="date_month", y="likes",
             color='insta_handle', barmode='group', title='Month-Wise Total Like received',
             height=400)
    fig_6.show()

    fig_7 = px.line(df_resample_nan_3, x='date', y="momentum_absolute", title='Instagram Post Likes VS Date (Absolute Momentum)')
    fig_7.show()

    fig_8 = px.line(df_resample_nan_3, x='date', y="momentum_relative", title='Instagram Post Likes VS Date (Relative Momentum)')
    fig_8.show()

  else:
    df_new, df_ma, df_resample, df_resample_nan_2, df_resample_nan_3, df_grouped = df_maker_1(df, ma, momentum, 'kingjames') # change entity? jordan_poole. kingjames
    entities = df.insta_handle.unique()
    for entity in entities[1:]:
        df_new_e, df_ma_e, df_resample_e, df_resample_nan_2_e, df_resample_nan_3_e, df_grouped_e = df_maker_1(df, ma, momentum, entity)
        df_new = df_new.append(df_new_e)
        df_ma = df_ma.append(df_ma_e)
        df_resample = df_resample.append(df_resample_e)
        df_resample_nan_2 = df_resample_nan_2.append(df_resample_nan_2_e)
        df_resample_nan_3 = df_resample_nan_3.append(df_resample_nan_3_e)
        df_grouped = df_grouped.append(df_resample_e)
    fig_1 = px.line(df_new, x='date_month', y='likes', color = 'insta_handle',
                  hover_data={"date_month": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date')
    fig_1.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_1.show()

    fig_2 = px.line(df_ma, x='date_month', y='likes', color = 'insta_handle',
                  hover_data={"date_month": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date (RMA - post focused)')
    fig_2.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_2.show()

    fig_3 = px.line(df_resample, x='date', y='likes', color = 'insta_handle',
                  hover_data={"date": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date (RMA - Time period focused)')
    fig_3.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_3.show()

    fig_4 = px.line(df_ma, x='date_month', y='momentum', color = 'insta_handle',
                  hover_data={"date_month": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date (momentum - post focused)')
    fig_4.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_4.show()

    fig_4_b = px.line(df_resample, x='date', y='momentum', color = 'insta_handle',
                  hover_data={"date": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date (momentum - Time focused)')
    fig_4_b.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_4_b.show()

    fig_5 = px.line(df_resample_nan_2, x='date', y='likes_nan', color = 'insta_handle',
                  hover_data={"date": "|%B %d, %Y"},
                  title='Instagram Post Likes VS Date (RMA - considers nan, Time focused)')
    fig_5.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_5.show()

    fig_6 = px.histogram(df_ma, x="date_month", y="likes",
             color='insta_handle', barmode='group',
             title='Month-Wise Total Like received',
             height=400)
    fig_6.show()

    fig_7 = px.line(df_resample_nan_3, x='date', y='momentum_absolute', color = 'insta_handle',
              hover_data={"date": "|%B %d, %Y"},
              title='Instagram Post Likes VS Date ((Absolute Momentum))')
    fig_7.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_7.show()

    fig_8 = px.line(df_resample_nan_3, x='date', y='momentum_relative', color = 'insta_handle',
              hover_data={"date": "|%B %d, %Y"},
              title='Instagram Post Likes VS Date ((Relative Momentum))')
    fig_8.update_xaxes(
        dtick="M1",
        tickformat="%b\n%Y")
    fig_8.show()

    import pandas as pd
    import plotly.graph_objects as go
    from plotly.offline import iplot
    from plotly.subplots import make_subplots

    for entity_x in df_resample_nan_2.insta_handle.unique().tolist():
      df_resample_nan_2_entity =  df_resample_nan_2.loc[df_resample_nan_2.insta_handle == entity_x]
      df_resample_nan_3_entity =  df_resample_nan_3.loc[df_resample_nan_3.insta_handle == entity_x]
      # dict for the dataframes and their names
      dfs = {"Moving Average" : df_resample_nan_2_entity, "Relative Momentum": df_resample_nan_3_entity}

      # plot the data
      fig = go.Figure()
      fig = make_subplots(specs=[[{"secondary_y": True}]])

      for i in dfs:
        if i == 'Moving Average':
          fig.add_trace(go.Scatter(x = dfs[i]["date"],
                                        y = dfs[i]["likes_nan"],
                                        name = i), secondary_y=False)
        elif i == 'Relative Momentum':
          fig.add_trace(go.Scatter(x = dfs[i]["date"],
                                        y = dfs[i]["momentum_relative"],
                                        name = i), secondary_y=True)
      # Add figure title
      fig.update_layout(
          title_text=f"{entity_x}"
      )

      # Set x-axis title
      fig.update_xaxes(title_text="xaxis title")

      # Set y-axes titles
      fig.update_yaxes(title_text= f"Rolling Moving Average ({ma} Days)", secondary_y=False)
      fig.update_yaxes(title_text= f"Relative Momentum ({momentum} frame)", secondary_y=True)

      fig.show()
      try:
        points=np.array(dfs[i]["likes_nan"])
        #Changepoint detection with window-based search method
        model = "l2"
        algo = rpt.Window(width=40, model=model).fit(points)
        my_bkps = algo.predict(n_bkps=10)
        rpt.show.display(points, my_bkps, figsize=(40, 7))
        plt.title('Change Point Detection: Window-Based Search Method')
        plt.show()

        points=np.array(df_new.loc[df_new.insta_handle==entity_x]["likes"])
        #Changepoint detection with window-based search method
        model = "l2"
        algo = rpt.Window(width=40, model=model).fit(points)
        my_bkps = algo.predict(n_bkps=10)
        rpt.show.display(points, my_bkps, figsize=(40, 7))
        plt.title('Change Point Detection: Window-Based Search Method')
        plt.show()
      except:
        pass
    print(entities)

    return df_resample_nan_3

momentum_frame = 30
rolling_movinge_average_frame = 90
# nba_final_df = pd.read_csv("/Users/dannysimon/Documents/QARIK/OPUS/src/nba_final_df.csv")
#Give a instahandle id in entity to see player focused plots, else keep it as false
entity = False
# entity = 'kingjames'

# Run this function
# Give Entity a insta handle value to plot individually
momentum_df_1 = plot_maker_1(nba_final_df, rolling_movinge_average_frame, momentum_frame,entity)
sdbadsb


import os
import csv
import sys
import re

from surprise import Dataset
from surprise import Reader

from collections import defaultdict
import numpy as np

class BookCrossing:

    bookID_to_name = {}
    name_to_bookID = {}
    ratingsPath = './datasets/processed/ratings.csv'
    booksPath = './datasets/processed/books.csv'
    usersPath = './datasets/processed/users.csv'

    def loadBookCrossingLatestSmall(self):

        # Look for files relative to the directory we are running from

        ratingsDataset = 0
        self.bookID_to_name = {}
        self.name_to_bookID = {}

        reader = Reader(line_format='user item rating ', sep=',', rating_scale=(1,10) ,skip_lines=1)

        ratingsDataset = Dataset.load_from_file(self.ratingsPath, reader=reader)

        with open(self.booksPath, newline='', encoding='ISO-8859-1') as csvfile:
                bookReader = csv.reader(csvfile)
                next(bookReader)  #Skip header line
                for i, row in enumerate(bookReader):
                    bookID = row[0]
                    bookName = row[1]
                    self.bookID_to_name[bookID] = bookName
                    self.name_to_bookID[bookName] = bookID

        return ratingsDataset

    def getUserRatings(self, user):
        userRatings = []
        hitUser = False
        with open(self.ratingsPath, newline='') as csvfile:
            ratingReader = csv.reader(csvfile)
            next(ratingReader)
            for row in ratingReader:
                userID = int(row[0])
                if (user == userID):
                    bookID = row[1]
                    rating = float(row[2])
                    userRatings.append((bookID, rating))
                    hitUser = True
                if (hitUser and (user != userID)):
                    break

        return userRatings

    def getPopularityRanks(self):
        ratings = defaultdict(int)
        rankings = defaultdict(int)
        with open(self.ratingsPath, newline='') as csvfile:
            ratingReader = csv.reader(csvfile)
            next(ratingReader)
            for row in ratingReader:
                bookID = row[1]
                ratings[bookID] += 1
        rank = 1
        for bookID, ratingCount in sorted(ratings.items(), key=lambda x: x[1], reverse=True):
            rankings[bookID] = rank
            rank += 1
        return rankings

    def getYears(self):
        years = defaultdict(int)
        with open(self.booksPath, newline='', encoding='ISO-8859-1') as csvfile:
            bookReader = csv.reader(csvfile)
            next(bookReader)
            for row in bookReader:
                bookID = row[0]
                year = int(row[3])
                years[bookID] = year
        return years

    def getBookName(self, bookID):
        if bookID in self.bookID_to_name:
            return self.bookID_to_name[bookID]
        else:
            return ""

    def getBookID(self, bookName):
        if bookName in self.name_to_bookID:
            return self.name_to_bookID[bookName]
        else:
            return 0


"""CSV files reader implementation."""
import csv
import os
from typing import Iterator, List

import settings

class CSVReader:
    """CSV files reader implementation."""

    text_data_folder_path = settings.INPUT_DATA_FOLDER_PATH
    csv_type_ending = ".csv"
    first_column = 0

    def read_by_one(self) -> Iterator[str]:
        """Read text from all CSV files found in data-directory one by one.

        Reads only first column. File shouldn't have header.
        """
        csv_files: List[str] = self._get_files_from_data_dir()

        for file_path in csv_files:
            with open(file_path, "r") as file:
                csv_reader = csv.reader(file)

                for row in csv_reader:
                    yield row[self.first_column]

    def _get_files_from_data_dir(self) -> List[str]:
        csv_files: List[str] = []

        for file in os.listdir(self.text_data_folder_path):
            if file.endswith(self.csv_type_ending):
                csv_file_path = self.text_data_folder_path / file

                csv_files.append(csv_file_path)

        if not csv_files:
            raise ValueError(f"No CSV file found in {self.text_data_folder_path}")

        return csv_files


class Solution:
    def check(self, root, prev):
        if not root:
            return

        if root.val > prev:
            self.cnt += 1

        l = self.check(root.left, max(prev, root.val))
        r = self.check(root.right, max(prev, root.val))
        return l and r

    def solve(self, A):
        self.cnt = 0
        self.check(A, float('-inf'))
        return self.cnt

Trees/Trees III (BST)/11. Counting the Nodes.py


import contextlib
import pylint.testutils

class BaseCheckerTestCase(pylint.testutils.CheckerTestCase):
    """Extends the basic pylint test case with easier helpers."""

    @contextlib.contextmanager
    def assertAddedMessage(self, message):
        """Assert that a msg_id occurred."""
        yield
        got = [msg.msg_id for msg in self.linter.release_messages()]
        got_str = "\n".join(repr(m) for m in got)
        msg = (
            "Expected messages did not match actual.\n"
            f"Got:\n{got_str}\n"
        )
        assert message in got, msg


from turtle import Turtle
import random

start_x = -460

colours = ["yellow", "gold", "orange", "red", "maroon", "violet", "magenta", "purple", "navy", "blue", "skyblue",
           "cyan", "turquoise", "lightgreen", "green", "darkgreen", "chocolate", "brown", "gray", "white"]

class Wall(Turtle):
    def __init__(self):
        super().__init__()
        self.bricks = []
        self.start_x = start_x

    def create_bricks(self, y):
        for i in range(0, 12):
            self.brick = Turtle("square")
            self.brick.color("white", random.choice(colours))
            self.brick.shapesize(stretch_len=4, stretch_wid=2)
            self.brick.penup()
            # if i == 0:
            #     brick.goto(self.start_x + (i * 80), y)
            # else:
            self.brick.goto(self.start_x + (i * 80), y)
            self.bricks.append(self.brick)

    # def create_middle(self):
    #     for i in range(0, 24):
    #         brick = Turtle("square")
    #         brick.color(random.choice(colours))
    #         brick.shapesize(stretch_len=4, stretch_wid=1)
    #         brick.penup()
    #         brick.goto(self.start_x + (i * 80), self.start_y + 20)
    #         self.bricks.append(brick)
    #
    # def create_bottom(self):
    #     for i in range(0, 24):
    #         brick = Turtle("square")
    #         brick.color(random.choice(colours))
    #         brick.shapesize(stretch_len=4, stretch_wid=1)
    #         brick.penup()
    #         brick.goto(self.start_x + (i * 80), self.start_y + 40)
    #         self.bricks.append(brick)


'''
def reverse(input):
    n = len(input)-1
    final = []
    for k in range(len(input)-1):
        final[k] = input[n-k]

    return "".join(final)
'''
'''
def reverse(x):
    output = ""
    for c in x:
        output = c + output
        print(output)
def main():
    print(reverse("plip"))

main()

'''

# Find missing items in a list
def missing_number(list1,list2):
    list_1Map = {}
    list_2Map = {}

    for num in list1:
        list_1Map[num] = list_1Map.get(num,0) + 1

    for num in list2:
        list_2Map[num] = list_2Map.get(num,0) + 1

    return (list_1Map.items() != list_2Map.items())

def main():
    full_list = [4,9,3,2]
    cmp_list = [9,4,3,2]
    print(missing_number(full_list,cmp_list))

main()


import numpy as np
from py_stringmatching.utils import *

def sim_ident(char1, char2):
    return 0.0 if char1 == char2 else -1.0

def nw_norm(string1, string2):
    string1 = convert_to_unicode(string1)
    string2 = convert_to_unicode(string2)
    n, m = len(string1), len(string2)

    if n==0 and m==0:
        return 1.0

    dist = nw_dist(string1, string2, n, m)
    return (-1 * dist + 2 * max(n,m)) / (2 * max(n,m))

def nw_dist(string1, string2, n, m):
    if string1 == string2:
        return 0

    gap_cost = -2.0
    if n == 0:
        return -1 * gap_cost * m
    if m == 0:
        return -1 * gap_cost * n

    dist_mat = np.zeros((len(string1) + 1, len(string2) + 1),
                        dtype=np.float)

    v0 = np.zeros(m+1, dtype=np.float)
    v1 = np.zeros(m+1, dtype=np.float)

    for j in xrange(m+1):
        v0[j]=j

    for i in xrange(1, n+1):
        v1[0] = i
        for j in xrange(1, m+1):
            v1[j] = min(v0[j] - gap_cost,
                        v1[j-1] - gap_cost,
                        v0[j-1] - sim_ident(string1[i-1], string2[j-1]))

        v0, v1 = v1, v0

    return v0[m]


from __future__ import print_function
import torch
import scipy.io as spio
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

from scipy.io import loadmat
import os
from tqdm import tqdm
import copy

import pandas as pd
import matplotlib.pyplot as plt
import os

get_ipython().run_line_magic('tensorflow_version', '1.x')
from torchvision.transforms import Compose, Grayscale
import torchvision.transforms as transforms
from torchvision.transforms.functional import to_grayscale

from PIL import Image, ImageDraw
from sklearn.model_selection import train_test_split, cross_val_score

import keras
from keras.utils import to_categorical
from keras import models
from keras import layers
from keras.datasets import imdb
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

from sklearn.model_selection import KFold

from numpy.random import seed
from numpy import asarray
from tensorflow import set_random_seed

import tensorflow as tf
import datetime
import glob

keras.applications.resnet_v2.ResNet50V2()
keras.applications.resnet_v2.ResNet101V2()
keras.applications.resnet_v2.ResNet152V2()

keras.applications.resnet.ResNet50()
keras.applications.resnet.ResNet101()
keras.applications.resnet.ResNet152()

# Preprocessings
# List classes  consists of 17 types of actions, including 9 ADLs and 8 Falls:

classes = [
    'StandingUpFS',
    'StandingUpFL',
    'Walking',
    'Running',
    'GoingUpS',
    'Jumping',
    'GoingDownS',
    'LyingDownFS',
    'SittingDown',
    'FallingForw',
    'FallingRight',
    'FallingBack',
    'HittingObstacle',
    'FallingWithPS',
    'FallingBackSC',
    'Syncope',
    'FallingLeft'
]

# Method transforms.Compose combines methods transforms.ToTensor
# (converts image with pixel diapason [0, 255] into tensor with [0, 1] diapason)
# and transforms.Normalize (normalizes images) into a method transform:

transform = transforms.Compose([transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,))])

# Dictionary dict_classes assigns a unique index to every class:

dict_classes = {}
i = 0
for activity in classes:
    dict_classes[activity] = i
    i += 1

# Function preprocess_function processes иoriginal images and forms a benchmarked dataset.
# Arguments:
#
# * input_path - input directory pathway,
# * sample_width - width of cropped images,
# * step - step of frame shift,
# * list_of_folders - list of directories with original images,
#
# Returns:
#
# * array - tensor of cropped and preprocessed images,
# * targets - vetor of labels

def preprocess_function(input_path,
                        multiframe=True,
                        sample_width=90, step=15,
                        list_of_folders=[]):

    array = []                             # List of final images
    targets_words = []                     # Vector of labels (consists of action names, for example Running)
    targets = []                           # Encoded vector of labels

    """Here we test if there is at least one directory in the list.
       If not, the function stops running and returns empty arrays"""

    if len(list_of_folders)==0:
        print("No folder is chosen. Chose at least one folder, please")
        return [], []

    path = os.path.join(input_path, list_of_folders[0])

    """Here we test if all directories have the same number of images.
       If not, the function stops running and returns empty arrays"""

    def listdir_nohidden(path):
        return glob.glob(os.path.join(path, '*'))      # ignore hidden files (including '.ipynb_checkpoints')

    if len(list_of_folders) > 1:
        for i in range(len(list_of_folders)-1):
            path_1 = os.path.join(input_path, list_of_folders[i])
            path_2 = os.path.join(input_path, list_of_folders[i+1])
            if len(listdir_nohidden(path_1)) != len(listdir_nohidden(path_2)):
                print("Error!  There are different number of files in folders!")
                return [], []

    """Search for the minimal image size min_width."""

    all_widths = []

    for filename in tqdm(os.listdir(path)):
        if filename==".ipynb_checkpoints":
            continue
        current_image = Image.open(os.path.join(input_path, list_of_folders[0], filename))
        data = asarray(current_image)
        height, width, dimensions = data.shape
        all_widths.append(width)

    min_width = min(all_widths)

    """Here we iterate on images from the chosen directory.
       If any images is not in every directory,
       the function stops running and returns empty arrays.
       It is assumed that images in different directories
       related to the same object are identical in size"""

    for filename in tqdm(os.listdir(path)):

        if filename==".ipynb_checkpoints":
            continue

        try:
            data_array = []
            for folder in list_of_folders:
                current_image = Image.open(os.path.join(input_path, folder, filename))
                data_array.append(current_image)
            data = asarray(current_image)
            height, width, dimensions = data.shape

        except:
            print("Image " + str(filename) + " has not all the chosen axes")
            return [], []

        if multiframe:
            for j in range(int(round((width - sample_width)/step + 1))):

                # Image size
                left = step*j
                right = left + sample_width
                top = 0
                bottom = copy.copy(height)

                # Images along all chosen axes
                images = []

                for data in data_array:
                    im = data.crop((left, top, right, bottom))
                    im = transforms.functional.to_grayscale(im, 1)
                    im = np.array(im)
                    images.append(im)

                images = np.array(transform(np.array(images)))
                array.append(images)
                targets_words.append(filename)

        else:

            # Image size
            left = 0
            right = copy.copy(min_width)
            top = 0
            bottom = copy.copy(height)

            # Images along all chosen axes
            images = []

            for data in data_array:
                im = data.crop((left, top, right, bottom))
                im = transforms.functional.to_grayscale(im, 1)
                im = np.array(im)
                images.append(im)

            images = np.array(transform(np.array(images)))
            array.append(images)
            targets_words.append(filename)

    """Now we have 2 lists: arrays (contains tensors of cropped
       and preprocessed images) and targets_words (vector of labels).
       Let's encode categorical targets_words to targets"""

    for i in range(len(targets_words)):
        targets_words[i] = targets_words[i][:-10]
        if targets_words[i][-1] == '_':
            targets_words[i] = targets_words[i][:-1]

    for word in targets_words:
        targets.append(dict_classes[word])
    targets = np.array(targets)

    targets = to_categorical(targets, num_classes=len(classes))
    targets = np.array(targets)

    return array, targets

# Dataset split

# Function split_function splits dataset into train and test samples.
# Arguments:
#
# * array - original array of features,
# * targets - vector of labels,
# * test_size - test sample fraction (0.2 by default),
# * random_state - random state value (26 by defaut).
#
# Function split_function returns train and test samples and corresponing vector of labels:
#
# * X_train,
# * X_test,
# * y_train,
# * y_test.

def split_function(array, targets, test_size=0.20, random_state=26):

    X_train, X_test, y_train, y_test = train_test_split(array, targets,
                                                        test_size=test_size,
                                                        shuffle=True,
                                                        random_state=random_state)

    X_train = np.array(X_train)
    y_train = np.array(y_train)
    X_test = np.array(X_test)
    y_test = np.array(y_test)

    X_train = np.transpose(X_train, (0, 3, 1, 2))
    X_test = np.transpose(X_test, (0, 3, 1, 2))

    return X_train, X_test, y_train, y_test

# Now we build neural network

# my_callback is a daughter class of the class callbacks:

class my_callback(tf.keras.callbacks.Callback):

    def __init__(self):
        super().__init__()
        self.acc = []
        self.loss = []
        self.rec = []
        self.prec = []

    """After every epoch metrics are counted via cross-validation"""

    def on_epoch_end(self, epoch, logs=None):
        l, a, r, p = self.model.evaluate(self.validation_data[0],
                                         self.validation_data[1])
        self.loss.append(l)
        self.acc.append(a)
        self.rec.append(r)
        self.prec.append(p)

    """After training is complete metrics are saved into metrics.csv"""

    def on_train_end(self, logs=None):

            df = {'loss': self.loss,
                  'accuracy': self.acc,
                  'recall': self.rec,
                  'precision': self.prec}
            df = pd.DataFrame(data=df)
            df.to_csv("metrics.csv", index=False)

# Function create_and_run_model builds and trains a neural network and creates a file metrics.csv containing metrics.
# Arguments:
#
# * X_train, X_test, y_train, y_test - train and test samples and corresponding vectors of labels,
# * res_model - ResNet model from Keras (keras.applications.resnet_v2.ResNet50V2,
#                                        keras.applications.resnet_v2.ResNet101V2,
#                                        keras.applications.resnet_v2.ResNet152V2,
#                                        keras.applications.resnet.ResNet50,
#                                        keras.applications.resnet.ResNet101,
#                                        keras.applications.resnet.ResNet152),
# * number_of_classes - number of different types of activity,
# * height - input image height,
# * width - input image width,
# * dimensions - number of channels of input images,
# * batch_size - batch size,
# * epochs - number of epochs.

def create_and_run_model(X_train, X_test, y_train, y_test,
                         resnet = True,
                         base_model=keras.applications.resnet_v2.ResNet50V2,
                         number_of_classes=len(classes),
                         height=36,
                         width=90,
                         dimensions=3,
                         batch_size=200,
                         epochs=300):

    if resnet:
        model = base_model(weights=None, input_shape=(height, width, dimensions),
                         classes=number_of_classes)
    else:
        model = base_model

    model.compile(optimizer = "adam",
                  loss = "binary_crossentropy",
                  metrics = ["accuracy", tf.keras.metrics.Recall(), tf.keras.metrics.Precision()])

    log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

    model.fit(X_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=(X_test, y_test),
              callbacks=[my_callback()])


import time
import pytest
from pages.profile_page import ProfilePage
from tests.test_login_page import test_go_to_login

@pytest.mark.smoke
@pytest.mark.regression
def test_go_to_quit_button(browser):
    test_go_to_login(browser)
    link = 'http://34.141.58.52:8080/#/profile'
    page = ProfilePage(browser, link)
    page.open()
    page.go_to_quit_button()
    browser.save_screenshot('quit_btn_screen.png')

@pytest.mark.regression
def test_go_to_edit_pet_profile(browser):
    test_go_to_login(browser)
    link = 'http://34.141.58.52:8080/#/profile'
    page = ProfilePage(browser, link)
    page.open()
    page.go_to_edit_pet_profile()
    browser.save_screenshot('edit_profile_screen.png')

@pytest.mark.smoke
@pytest.mark.regression
def test_go_to_add_pet(browser):
    test_go_to_login(browser)
    link = 'http://34.141.58.52:8080/#/profile'
    page = ProfilePage(browser, link)
    page.open()
    page.go_to_add_pet_btn()
    page.go_to_add_new_name()
    page.go_to_add_new_age()
    page.choose_new_pet_type()
    page.choose_new_pet_type_dog()
    page.choose_new_pet_gender()
    page.choose_new_pet_gender_male()
    page.go_to_submit_add_pet_btn()
    page.open()
    time.sleep(2)
    browser.save_screenshot('add_pet_screen.png')