import logging
import sys
import textwrap
from shutil import get_terminal_size

try:
    from setproctitle import setproctitle
except ImportError:
    def setproctitle(x):
        pass

import click
import pytz

from . import aux, controllers, khalendar, __version__
from .log import logger
from .settings import get_config, InvalidSettingsError
from .settings.exceptions import NoConfigFile
from .exceptions import FatalError
from .terminal import colored

days_option = click.option('--days', default=None, type=int,
                           help='How many days to include.')
week_option = click.option('--week', '-w',
                           help=('Include all events in one week.'), is_flag=True)
events_option = click.option('--events', default=None, type=int,
                             help='How many events to include.')
dates_arg = click.argument('dates', nargs=-1)

def time_args(f):
    return dates_arg(events_option(week_option(days_option(f))))

def _multi_calendar_select_callback(ctx, option, calendars):
    if not calendars:
        return
    if 'calendar_selection' in ctx.obj:
        raise click.UsageError('Can\'t use both -a and -d.')
    if not isinstance(calendars, tuple):
        calendars = (calendars,)

    mode = option.name
    selection = ctx.obj['calendar_selection'] = set()

    if mode == 'include_calendar':
        for cal_name in calendars:
            if cal_name not in ctx.obj['conf']['calendars']:
                raise click.BadParameter(
                    'Unknown calendar {}, run `khal printcalendars` to get a '
                    'list of all configured calendars.'.format(cal_name)
                )

        selection.update(calendars)
    elif mode == 'exclude_calendar':
        selection.update(ctx.obj['conf']['calendars'].keys())
        for value in calendars:
            selection.remove(value)
    else:
        raise ValueError(mode)

def multi_calendar_option(f):
    a = click.option('--include-calendar', '-a', multiple=True, metavar='CAL',
                     expose_value=False,
                     callback=_multi_calendar_select_callback,
                     help=('Include the given calendar. Can be specified '
                           'multiple times.'))
    d = click.option('--exclude-calendar', '-d', multiple=True, metavar='CAL',
                     expose_value=False,
                     callback=_multi_calendar_select_callback,
                     help=('Exclude the given calendar. Can be specified '
                           'multiple times.'))

    return d(a(f))

def _calendar_select_callback(ctx, option, calendar):
    calendar = calendar or ctx.obj['conf']['default']['default_calendar']
    if not calendar:
        raise click.BadParameter(
            'No default calendar is configured, '
            'please provide one explicitly.'
        )
    if calendar not in ctx.obj['conf']['calendars']:
        raise click.BadParameter(
            'Unknown calendar {}, run `khal printcalendars` to get a '
            'list of all configured calendars.'.format(calendar)
        )

    return calendar

def calendar_option(f):
    return click.option('--calendar', '-a', metavar='CAL',
                        callback=_calendar_select_callback)(f)

def global_options(f):
    def config_callback(ctx, option, config):
        prepare_context(ctx, config)

    def verbosity_callback(ctx, option, verbose):
        if verbose:
            logger.setLevel(logging.DEBUG)
        else:
            logger.setLevel(logging.INFO)

    def color_callback(ctx, option, value):
        ctx.color = value

    config = click.option(
        '--config', '-c',
        is_eager=True,  # make sure other options can access config
        help='The config file to use.',
        default=None, metavar='PATH', expose_value=False,
        callback=config_callback
    )
    verbose = click.option(
        '--verbose', '-v',
        is_eager=True,  # make sure to log config when debugging
        help='Output debugging information.',
        is_flag=True, expose_value=False, callback=verbosity_callback
    )
    color = click.option(
        '--color/--no-color',
        help=('Use colored/uncolored output. Default is to only enable colors '
              'when not part of a pipe.'),
        expose_value=False, default=None,
        callback=color_callback
    )

    version = click.version_option(version=__version__)

    return config(verbose(color(version(f))))

def build_collection(ctx):
    try:
        conf = ctx.obj['conf']
        selection = ctx.obj.get('calendar_selection', None)

        props = dict()
        for name, cal in conf['calendars'].items():
            if selection is None or name in ctx.obj['calendar_selection']:
                props[name] = {
                    'name': name,
                    'path': cal['path'],
                    'readonly': cal['readonly'],
                    'color': cal['color'],
                    'ctype': cal['type'],
                }
        collection = khalendar.CalendarCollection(
            calendars=props,
            color=ctx.obj['conf']['highlight_days']['color'],
            locale=ctx.obj['conf']['locale'],
            dbpath=conf['sqlite']['path'],
            hmethod=ctx.obj['conf']['highlight_days']['method'],
            default_color=ctx.obj['conf']['highlight_days']['default_color'],
            multiple=ctx.obj['conf']['highlight_days']['multiple'],
            highlight_event_days=ctx.obj['conf']['default']['highlight_event_days'],
        )
    except FatalError as error:
        logger.fatal(error)
        sys.exit(1)

    collection._default_calendar_name = conf['default']['default_calendar']
    return collection

class _NoConfig(object):
    def __getitem__(self, key):
        logger.fatal(
            'Cannot find a config file. If you have no configuration file '
            'yet, you might want to run `khal configure`.')
        sys.exit(1)

def prepare_context(ctx, config):
    assert ctx.obj is None

    logger.debug('khal %s' % __version__)
    try:
        conf = get_config(config)
    except NoConfigFile:
        conf = _NoConfig()
    except InvalidSettingsError:
        sys.exit(1)
    else:
        logger.debug('Using config:')
        logger.debug(stringify_conf(conf))

    ctx.obj = {'conf_path': config, 'conf': conf}

def stringify_conf(conf):
    # since we have only two levels of recursion, a recursive function isn't
    # really worth it
    out = list()
    for key, value in conf.items():
        out.append('[{}]'.format(key))
        for subkey, subvalue in value.items():
            if isinstance(subvalue, dict):
                out.append('  [[{}]]'.format(subkey))
                for subsubkey, subsubvalue in subvalue.items():
                    out.append('    {}: {}'.format(subsubkey, subsubvalue))
            else:
                out.append('  {}: {}'.format(subkey, subvalue))
    return '\n'.join(out)

def _get_cli():
    @click.group(invoke_without_command=True)
    @global_options
    @click.pass_context
    def cli(ctx):
        # setting the process title so it looks nicer in ps
        # shows up as 'khal' under linux and as 'python: khal (python2.7)'
        # under FreeBSD, which is still nicer than the default
        setproctitle('khal')

        if not ctx.invoked_subcommand:
            command = ctx.obj['conf']['default']['default_command']
            if command:
                ctx.invoke(cli.commands[command])
            else:
                click.echo(ctx.get_help())
                ctx.exit(1)

    @cli.command()
    @time_args
    @multi_calendar_option
    @click.pass_context
    @click.option('--full', '-f', help=('Print description and location with event'),
                  is_flag=True)
    def calendar(ctx, days, events, dates, week, full=False):
        '''Print calendar with agenda.'''
        if week and days:
            raise click.UsageError('Cannot use --days and -week at the same time.')
        controllers.calendar(
            build_collection(ctx),
            dates=dates,
            firstweekday=ctx.obj['conf']['locale']['firstweekday'],
            encoding=ctx.obj['conf']['locale']['encoding'],
            locale=ctx.obj['conf']['locale'],
            weeknumber=ctx.obj['conf']['locale']['weeknumbers'],
            show_all_days=ctx.obj['conf']['default']['show_all_days'],
            days=days or ctx.obj['conf']['default']['days'],
            events=events,
            hmethod=ctx.obj['conf']['highlight_days']['method'],
            default_color=ctx.obj['conf']['highlight_days']['default_color'],
            multiple=ctx.obj['conf']['highlight_days']['multiple'],
            color=ctx.obj['conf']['highlight_days']['color'],
            highlight_event_days=ctx.obj['conf']['default']['highlight_event_days'],
            bold_for_light_color=ctx.obj['conf']['view']['bold_for_light_color'],
            full=full,
            week=week,
        )

    @cli.command()
    @time_args
    @multi_calendar_option
    @click.pass_context
    @click.option('--full', '-f', help=('Print description and location with event'),
                  is_flag=True)
    def agenda(ctx, days, events, dates, week, full=False):
        '''Print agenda.'''
        if week and days:
            raise click.UsageError('Cannot use --days and -week at the same time.')
        controllers.agenda(
            build_collection(ctx),
            dates=dates,
            firstweekday=ctx.obj['conf']['locale']['firstweekday'],
            encoding=ctx.obj['conf']['locale']['encoding'],
            show_all_days=ctx.obj['conf']['default']['show_all_days'],
            locale=ctx.obj['conf']['locale'],
            days=days or ctx.obj['conf']['default']['days'],
            events=events,
            week=week,
            full=full,
            bold_for_light_color=ctx.obj['conf']['view']['bold_for_light_color']
        )

    @cli.command()
    @calendar_option
    @click.option('--location', '-l',
                  help=('The location of the new event.'))
    @click.option('--repeat', '-r',
                  help=('Repeat event: daily, weekly, monthly or yearly.'))
    @click.option('--until', '-u',
                  help=('Stop an event repeating on this date.'))
    @click.option('--alarm', '-m',
                  help=('Alarm time for the new event.'))
    @click.argument('START', nargs=1, required=True)
    @click.argument('END', nargs=1, required=False)
    @click.argument('TIMEZONE', nargs=1, required=False)
    @click.argument('SUMMARY', metavar='SUMMARY', nargs=1, required=False)
    @click.argument('DESCRIPTION', metavar='[:: DESCRIPTION]', nargs=-1, required=False)
    @click.pass_context
    def new(ctx, calendar, start, end, timezone, summary, description, location, repeat,
            until, alarm):
        '''Create a new event from arguments.

        START and END can be either dates, times or datetimes, please have a
        look at the man page for details.
        Everthing than can not be interpreted as a (date)times or a timezone is
        assumed to be the event's summary, if two colons (::) are present,
        everything behind them is taken as the event's description.
        '''
        # ugly hack to change how click presents the help string
        eventlist = [start, end, timezone, summary] + list(description)
        eventlist = [element for element in eventlist if element is not None]

        controllers.new_from_string(
            build_collection(ctx),
            calendar,
            ctx.obj['conf'],
            eventlist,
            location=location,
            repeat=repeat,
            until=until.split(' ') if until is not None else None,
            alarm=alarm,
        )

    @cli.command('import')
    @click.option('--include-calendar', '-a', help=('The calendar to use.'),
                  expose_value=False, callback=_calendar_select_callback,
                  metavar='CAL')
    @click.option('--batch', help=('do not ask for any confirmation.'),
                  is_flag=True)
    @click.option('--random_uid', '-r', help=('Select a random uid.'),
                  is_flag=True)
    @click.argument('ics', type=click.File('rb'))
    @click.pass_context
    def import_ics(ctx, ics, batch, random_uid):
        '''Import events from an .ics file.

        If an event with the same UID is already present in the (implicitly)
        selected calendar import will ask before updating (i.e. overwriting)
        that old event with the imported one, unless --batch is given, than it
        will always update. If this behaviour is not desired, use the
        `--random-uid` flag to generate a new, random UID.
        If no calendar is specified (and not `--batch`), you will be asked
        to choose a calendar. You can either enter the number printed behind
        each calendar's name or any unique prefix of a calendar's name.

        '''
        ics_str = ics.read()
        controllers.import_ics(
            build_collection(ctx),
            ctx.obj['conf'],
            ics=ics_str,
            batch=batch,
            random_uid=random_uid
        )

    @cli.command()
    @multi_calendar_option
    @click.pass_context
    def interactive(ctx):
        '''Interactive UI. Also launchable via `ikhal`.'''
        controllers.interactive(build_collection(ctx), ctx.obj['conf'])

    @click.command()
    @global_options
    @multi_calendar_option
    @click.pass_context
    def interactive_cli(ctx):
        '''Interactive UI. Also launchable via `khal interactive`.'''
        controllers.interactive(build_collection(ctx), ctx.obj['conf'])

    @cli.command()
    @multi_calendar_option
    @click.pass_context
    def printcalendars(ctx):
        '''List all calendars.'''
        click.echo('\n'.join(build_collection(ctx).names))

    @cli.command()
    @click.pass_context
    def printformats(ctx):
        '''Print a date in all formats.

        Print the date 2013-12-21 10:09 in all configured date(time)
        formats to check if these locale settings are configured to ones
        liking.'''
        from datetime import datetime
        time = datetime(2013, 12, 21, 10, 9)

        for strftime_format in [
                'longdatetimeformat', 'datetimeformat', 'longdateformat',
                'dateformat', 'timeformat']:
            dt_str = time.strftime(ctx.obj['conf']['locale'][strftime_format])
            click.echo('{}: {}'.format(strftime_format, dt_str))

    @cli.command()
    @multi_calendar_option
    @click.argument('search_string')
    @click.pass_context
    def search(ctx, search_string):
        '''Search for events matching SEARCH_STRING.

        For repetitive events only one event is currently shown.
        '''
        # TODO support for time ranges, location, description etc
        collection = build_collection(ctx)
        events = collection.search(search_string)
        event_column = list()
        term_width, _ = get_terminal_size()
        for event in events:
            desc = textwrap.wrap(event.event_description, term_width)
            event_column.extend(
                [colored(d, event.color,
                         bold_for_light_color=ctx.obj['conf']['view']['bold_for_light_color'])
                 for d in desc]
            )
        click.echo(
            '\n'.join(event_column).encode(ctx.obj['conf']['locale']['encoding'])
        )

    @cli.command()
    @multi_calendar_option
    @click.argument('datetime', required=False, nargs=-1)
    @click.pass_context
    def at(ctx, datetime=None):
        '''Show all events scheduled for DATETIME.

        if DATETIME is given (or the string `now`) all events scheduled
        for this moment are shown, if only a time is given, the date is assumed
        to be today
        '''
        collection = build_collection(ctx)
        locale = ctx.obj['conf']['locale']
        dtime_list = list(datetime)
        if dtime_list == [] or dtime_list == ['now']:
            import datetime
            dtime = datetime.datetime.now()
        else:
            try:
                dtime, _ = aux.guessdatetimefstr(dtime_list, locale)
            except ValueError:
                logger.fatal(
                    '{} is not a valid datetime (matches neither {} nor {} nor'
                    ' {})'.format(
                        ' '.join(dtime_list),
                        locale['timeformat'],
                        locale['datetimeformat'],
                        locale['longdatetimeformat']))
                sys.exit(1)
        dtime = locale['local_timezone'].localize(dtime)
        dtime = dtime.astimezone(pytz.UTC)
        events = collection.get_events_at(dtime)
        event_column = list()
        term_width, _ = get_terminal_size()
        for event in events:
            lines = list()
            items = event.event_description.splitlines()
            for item in items:
                lines += textwrap.wrap(item, term_width)
            event_column.extend(
                [colored(line, event.color,
                         bold_for_light_color=ctx.obj['conf']['view']['bold_for_light_color'])
                 for line in lines]
            )
        click.echo(
            '\n'.join(event_column).encode(ctx.obj['conf']['locale']['encoding'])
        )

    @cli.command()
    @click.pass_context
    def configure(ctx):
        """Helper for initial configuration of khal."""
        from . import configwizard
        configwizard.configwizard()

    return cli, interactive_cli

main_khal, main_ikhal = _get_cli()


# The zip built-in takes a series of iterables
# and aggregates the elements from each of them

keys = ['x', 'y', 'z']
values = [5, 6, 7]
print (zip(keys, values))
#<zip object at 0x7faaad4dd848>

print (list(zip(keys, values)))
#[('x', 5), ('y', 6), ('z', 7)]

keys = ['x', 'y', 'z']
values = [5, 6, 7]
my_dict = dict(zip(keys, values))
print (my_dict)
#{'x': 5, 'y': 6, 'z': 7}


'''You are given with a number ‘n’. You have to count the pair of two numbers a and b such that sum of two numbers are equal to n.

Note:Both numbers lie in range 1<=a,b<n

Input Description:
You are given a number ‘n’

Output Description:
Print the number of pairs satisfying above condition

Sample Input :
5
Sample Output :
4

1 + 4
2 + 3
3 + 2
4 + 1
'''
i = 1
while(i):
    try:
        n = int(input())
        if n <= 0:
            print("Only  num value greater than 1")
            i = 0
            break
        count = 0
        for a in range(1,n):
            for b in range(1,n):
                c = a + b
                if  c== n:
                    count = count+1
                    #print("{0} + {1}".format(a,b))
        print(count)
        i = 0
    except ValueError:
        i =0
        print("ENter integer value alone")


#Name Kaisen
#Date Novemeber 8, 2021
#Title String9
#Description asks user to enter an integer, if integer is greater than 20 program ends

while (True):
    number = int(input('Please enter a integer: '))
    if number > 20:
        break


from django.contrib import admin
from .models import *

# Register your models here.
@admin.register(Alerta)
class AlertaAdmin(admin.ModelAdmin):
    list_display = ("id",
                    "automovel",
                    "placa",
                    "velocidade",
                    "horario",
                    "enviado",
                    )
    list_filter = ("placa",
                   "enviado",
                   )
    search_fields = ["automovel", "placa", "horario",]


import pandas as pd
#import pandas_usaddress

class dataTransform:
  def __init__(self,df):
    self.df = df

  def myfunc(self):
    # 1st Fill the numerical missing values with the mean of each row
    self.df = self.df.fillna(self.df.mean())
    # Impute the categorical with the most frequent value
    df2 = self.df.apply(lambda x: x.fillna(x.value_counts().index[0]))
    #Standardize date-time field with a given format
    df2['Clean_Date'] = df2.Date.apply(lambda x: pd.to_datetime(x).strftime('%d/%m/%Y %H:%M'))
    #df2.drop(columns=['Name'])
    #Rounding values as configured
    df2.round(3)
    return df2

#Test

df = pd.read_csv("Pokemon.csv")
print(df)
p1 = dataTransform(df)
df = p1.myfunc()
print(df)
df.to_csv('output.csv', header=False, index=False)


"""empty message

Revision ID: bfe0c4b4e44f
Revises: 1d3684d54138
Create Date: 2021-04-21 01:29:46.450606

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'bfe0c4b4e44f'
down_revision = '1d3684d54138'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('contact_way',
    sa.Column('Id', sa.Integer(), nullable=False),
    sa.Column('text', sa.String(), nullable=True),
    sa.Column('url_image', sa.String(), nullable=True),
    sa.Column('link', sa.Text(), nullable=True),
    sa.PrimaryKeyConstraint('Id'),
    sa.UniqueConstraint('text')
    )
    op.create_table('documents',
    sa.Column('Id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=64), nullable=True),
    sa.Column('url_image', sa.String(length=256), nullable=True),
    sa.Column('link', sa.Text(), nullable=True),
    sa.PrimaryKeyConstraint('Id'),
    sa.UniqueConstraint('title')
    )
    op.create_table('settings',
    sa.Column('Id', sa.Integer(), nullable=False),
    sa.Column('FullName', sa.String(length=64), nullable=True),
    sa.Column('AboutME', sa.Text(), nullable=True),
    sa.Column('url_background', sa.String(length=256), nullable=True),
    sa.Column('url_profile', sa.String(length=256), nullable=True),
    sa.PrimaryKeyConstraint('Id')
    )
    op.create_table('skills',
    sa.Column('Id', sa.Integer(), nullable=False),
    sa.Column('Name', sa.String(length=32), nullable=True),
    sa.Column('Value_skill', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('Id'),
    sa.UniqueConstraint('Name')
    )
    op.create_table('work samples',
    sa.Column('Id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=64), nullable=True),
    sa.Column('url_image', sa.String(length=256), nullable=True),
    sa.Column('link', sa.Text(), nullable=True),
    sa.PrimaryKeyConstraint('Id'),
    sa.UniqueConstraint('title')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('work samples')
    op.drop_table('skills')
    op.drop_table('settings')
    op.drop_table('documents')
    op.drop_table('contact_way')
    # ### end Alembic commands ###


import io
import itertools
import logging
import os
import re
import shutil
import glob
import fnmatch
from typing import List, Tuple
from tqdm import tqdm
import cv2
import google.auth
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from google.cloud import vision
from konlpy.tag import Komoran

from analyzer.constants import IMAGE_SIZE
from analyzer.data_utils import read_image
from analyzer.utils import to_best_device
from analyzer.v2.datasets import EvalSegmentationSubsDataset
from analyzer.v2.model import AttentionSubsBoxerModel

logger = logging.getLogger(__name__)
GOOGLE_MAX_HEIGHT = 2050
GOOGLE_MAX_WIDTH = 1536
NB_ROWS = 10
NB_COLUMNS = 3
COLUMN_HEIGHT = int(GOOGLE_MAX_HEIGHT / NB_ROWS)
COLUMN_WIDTH = int(GOOGLE_MAX_WIDTH / NB_COLUMNS)

BREAKS = vision.TextAnnotation.DetectedBreak.BreakType

class obj:
    # constructor
    def __init__(self, dict1):
        self.__dict__.update(dict1)

class VideoAnalyzer:

    def __init__(self, work_directory: str, model_name: str, target_directory: str,storage, skip_frames: int = 30,
                 bb_margin: int = 10):
        self.work_directory = work_directory
        self.target_directory = target_directory
        self.skip_frames = int(skip_frames)
        self.ensure_dir(work_directory)
        self.ensure_dir(target_directory)
        self.threshold = 0.5
        self.bb_margin = bb_margin
        self.vision_client = vision.ImageAnnotatorClient()
        self.storage = storage
        self.model = self._get_model(model_name)
        self.treated = set([])
        logger.info("Loading Komoran...")
        self.analyzer = Komoran()
        logger.info("...Komoran loaded")
        logger.info(f"Working with directory {work_directory}")
        logger.info(f"Target       directory {target_directory}")
        logger.info(f"Loaded model           {model_name}")

    def ensure_dir(self, file_path):
        os.makedirs(file_path, exist_ok=True)

    def treat_incoming_file(self, file_path: str) -> List[str]:
        logger.info(f"Treating file {file_path}")
        work_file_path = self.prepare_file(file_path)
        if work_file_path:
            prefix_splitted: str = self.split_file(work_file_path)
            annotation_file, byproducts = self.create_annotations(prefix_splitted)
            annotation_file_with_subs = self.add_subs(annotation_file, prefix_splitted)
            annotation_file_polished = self.polish(annotation_file_with_subs, prefix_splitted, work_file_path)
            annotation_file_final = self.parse_korean(annotation_file_polished, prefix_splitted)
            products = [annotation_file_final, work_file_path, file_path]
            products = self.move_products(prefix_splitted, products)
            # Get useful by products
            if len(byproducts) > 0:
                products.extend(byproducts)
            logger.info(f"Done ! {file_path} treated")
            return products
        return []

    def move_products(self, prefix: str, products_path: [str]) -> List[str]:
        logger.info(f"Moving products of {prefix} to {self.target_directory}")
        dest_dir = f"{self.target_directory}/{prefix}"
        self.ensure_dir(dest_dir)
        final_paths: List[str] = []
        for path in products_path:
            file_name = os.path.basename(path)
            final_path = f"{dest_dir}/{file_name}"
            shutil.move(path, final_path)
            logger.info(f"------ {path} moved to {final_path}")
            final_paths.append(final_path)
        return final_paths

    def parse_korean(self, annotation_file_path: str, prefix: str) -> str:
        logger.info("Parsing korean subs")
        final_annotation_file_path = f"{self.work_directory}/{prefix}.csv"
        df_in = pd.read_csv(annotation_file_path)
        extends = []
        nb_lines = len(df_in)
        for i, row in df_in.iterrows():
            parsed = self.analyzer.pos(row['subs'])
            extension = []
            for p in parsed:
                extension.append(p[0])
                extension.append(p[1])
            extends.append(extension)
            if (i + 1) % 100 == 0:
                logger.debug(f"{i + 1} / {nb_lines} subs analyzed")

        extends = np.array(list(zip(*itertools.zip_longest(*extends, fillvalue=''))))
        nb_extra_columns = len(extends[0])
        for i in range(nb_extra_columns):
            df_in[f"parsed_{i}"] = extends[:, i]
        df_in.to_csv(final_annotation_file_path, encoding='utf-8', index=False)
        return final_annotation_file_path

    def polish(self, annotation_file_path: str, prefix: str, video_file_path: str):
        logger.info("Polishing of the file")
        final_annotation_file_path = f"{self.work_directory}/{prefix}-polished.csv"
        if os.path.exists(final_annotation_file_path):
            logger.info("--- Polishing already done")
        else:
            df_in = pd.read_csv(annotation_file_path)
            video = cv2.VideoCapture(video_file_path)
            fps = video.get(cv2.CAP_PROP_FPS)
            spf = 1 / fps
            spf_for_sampling_rate = self.skip_frames * spf

            # Merge lines with same subs
            data = []
            curr_subs = None
            curr_frame_start, curr_frame_end = None, None
            for i, annotation in df_in.iterrows():
                start = annotation['start']
                end = annotation['end']
                subs = annotation['subs']
                if curr_subs == subs:
                    curr_frame_end = end
                else:
                    if not curr_subs is None:
                        data.append([curr_subs, curr_frame_start, curr_frame_end])
                    if pd.isna(subs):
                        curr_frame_start = None
                        curr_frame_end = None
                        curr_subs = None
                    else:
                        curr_frame_start = start
                        curr_frame_end = end
                        curr_subs = subs
            if not curr_subs is None:
                data.append([curr_subs, curr_frame_start, curr_frame_end])
            df = pd.DataFrame(columns=['subs', 'start', 'end'], data=data)
            df['start'] = df['start'] * spf_for_sampling_rate
            df['end'] = df['end'] * spf_for_sampling_rate
            df.to_csv(final_annotation_file_path, index=False)
        return final_annotation_file_path

    def prepare_file(self, file_path: str) -> str:
        filename_without_extension = os.path.splitext(os.path.basename(file_path))[0]
        out_file = f"{self.work_directory}/{filename_without_extension}.mp4"
        if os.path.exists(out_file):
            print("File already converted")
            return out_file
        else:
            print(f"Converting {file_path} into {out_file}")
            os.system(f"ffmpeg -i {file_path} {out_file}")
            if os.path.isfile(out_file):
                print(f"Conversion successful")
                return out_file
            else:
                print(f"Could not convert file")
                return None

    def split_file(self, file_path) -> str:
        """
        Extract frames of the video at constant rate and output them to the work directory
        :param file_path: Path of the video
        :return: Prefix of the video file
        """
        print(f"Splitting file {file_path}")
        filename_without_extension = os.path.splitext(os.path.basename(file_path))[0]
        if os.path.exists(f"{self.work_directory}/{filename_without_extension}-0.jpg"):
            print(f"--- File already splitted")
        else:
            cap = cv2.VideoCapture(file_path)
            i = 0
            stop_all = False
            while cap.isOpened() and not stop_all:
                for j in range(0, self.skip_frames):
                    ret, frame = cap.read()
                    if ret == False:
                        stop_all = True
                if not stop_all:
                    cv2.imwrite(f"{self.work_directory}/{filename_without_extension}-{str(i)}.jpg", frame)
                    i += 1
                    if i % 10 == 0:
                        logger.info(f"--- Exported {i} frames")
            cap.release()
            cv2.destroyAllWindows()
            logger.info(f"End splitting file {file_path} into {self.work_directory} with prefix {filename_without_extension}")
        return filename_without_extension

    def create_annotations(self, prefix_splitted: str) -> Tuple[str, List[str]]:
        """
        Analyze splitted files to detect bounding boxes
        :param prefix_splitted: Prefix of the files containing the frames of the video
        :return: Path of the file containing the following columns
        'filename', 'start', 'end'
        filename: string, name of the file with the subs
        start: int, index of the continuous series of frames containing the same subs
        end: int, index of the continuous series of frames containing the same subs
        """
        logger.info("Predicting subs from video")
        annotations_file_path = f"{self.work_directory}/annotations_{prefix_splitted}.csv"
        byproducts: List[str] = []
        if os.path.exists(annotations_file_path):
            logger.info("--- subs prediction already done")
        else:
            size = (IMAGE_SIZE, IMAGE_SIZE)
            data = []
            should_analyze_file = True
            start = -1
            i = 0
            x0 = -1
            x1 = -1
            y0 = -1
            y1 = -1
            current_file_path: str = None
            current_zone: np.array = None
            num_files = len(fnmatch.filter(os.listdir(self.work_directory), f"{prefix_splitted}-*.jpg"))
            for i in tqdm(range(num_files)):
                splitted_file = f"{self.work_directory}/{prefix_splitted}-{i}.jpg"
                if os.path.exists(splitted_file):
                    im = read_image(splitted_file)
                    im = cv2.resize(im, size)
                    if not should_analyze_file:
                        # Check if the bounding boxes contain the same information
                        image_zone = np.array(im[y0: y1, x0: x1])
                        if not self.same_subs(current_zone, image_zone):
                            # This is the end of the optimization for this sub
                            should_analyze_file = True
                            data.extend([[current_file_path, start, i - 1]])
                            current_zone = None
                            current_file_path = None
                            start = None
                        else:
                            logger.debug(f"Optimize frame {i} with frame {start}")
                    if should_analyze_file:
                        resized_file_path = f"{self.work_directory}/{prefix_splitted}-resized-{i}.jpg"
                        cv2.imwrite(resized_file_path, cv2.cvtColor(im, cv2.COLOR_RGB2BGR))
                        test_ds = EvalSegmentationSubsDataset(pd.DataFrame([{'filename': resized_file_path}]))
                        x = test_ds[0]
                        xx = to_best_device(torch.FloatTensor(x[None,]))
                        predicted_bbs = self.model(xx)
                        x0, y0, x1, y1, present = predicted_bbs.detach().cpu()[0]
                        has_bb: bool = present >= self.threshold
                        if has_bb:
                            y0 = int(np.floor(max(y0 - self.bb_margin, 0)))
                            y1 = int(np.ceil(min(y1 + self.bb_margin, IMAGE_SIZE - 1)))
                            x0 = int(np.floor(max(x0 - self.bb_margin, 0)))
                            x1 = int(np.ceil(min(x1 + self.bb_margin, IMAGE_SIZE - 1)))
                            has_bb = self._is_trustable_bounding_box(x0, y0, x1, y1)
                            if not has_bb:
                                postmortem_file = f"{self.work_directory}/{prefix_splitted}-postmortem-{i}.jpg"
                                cv2.imwrite(postmortem_file, cv2.cvtColor(im, cv2.COLOR_RGB2BGR))
                                byproducts.append(postmortem_file)
                        if has_bb:
                            start = i
                            should_analyze_file = False
                            current_zone = np.array(im[y0: y1, x0: x1])
                            current_file_path = f"{self.work_directory}/{prefix_splitted}-bounded-{i}.jpg"
                            cv2.imwrite(current_file_path, current_zone)
                        else:
                            current_zone = None
                            current_file_path = None
                            start = None
                        os.remove(resized_file_path)
                    os.remove(splitted_file)
                else:
                    if current_zone is not None:
                        data.extend([[current_file_path, start, i - 1]])
                    logger.info(f"--- inference done on {i} files")
                    break
                i += 1
                if i % 20 == 0:
                    logger.debug(f"------ {i} files parsed")
            annotations = pd.DataFrame(columns=['filename', 'start', 'end'], data=data)
            annotations.to_csv(annotations_file_path, encoding='utf-8')
        return annotations_file_path, byproducts

    @staticmethod
    def same_subs(expected_zone: np.ndarray, image_zone: np.ndarray) -> bool:
        threshold = 220
        expected_zone_thresholded = np.where(expected_zone > threshold, 1, 0)
        image_zone_thresholded = np.where(image_zone > threshold, 1, 0)
        ratio = (expected_zone_thresholded * image_zone_thresholded).sum() / (expected_zone_thresholded.sum() + 1e-6)
        return ratio > 0.75

    def add_subs(self, annotation_file_extraction: str, prefix: str) -> str:
        logger.info("Adding subs")
        file_with_subs_path = f"{self.work_directory}/{prefix}-with-subs.csv"
        if os.path.exists(file_with_subs_path):
            logger.info("--- Subs already added")
        else:
            df_annotations_in = pd.read_csv(annotation_file_extraction)
            background_images = []
            background_image = np.full((GOOGLE_MAX_HEIGHT, GOOGLE_MAX_WIDTH, 3), 255)
            row = 0
            column = 0
            has_data = False
            subtitles_per_frame = []
            nb_subs_for_page = 0
            for i, annotation in tqdm(df_annotations_in.iterrows()):
                y0 = row * COLUMN_HEIGHT
                x0 = column * COLUMN_WIDTH
                image_file_path = annotation['filename']
                coloured_image = read_image(image_file_path)
                height, width, _ = coloured_image.shape
                ydelta = min(height, COLUMN_HEIGHT)
                xdelta = min(width, COLUMN_WIDTH)
                y1 = y0 + ydelta
                x1 = x0 + xdelta
                # On part de en bas à droite et non en haut à gauche pour éviter les problèmes avec les images mal
                # découpées (en bas à droite on a plus de chances de trouver des sous-titres que en haut à gauche dans
                # une image mal découpée)
                background_image[y0:y1, x0: x1, :] = coloured_image[-ydelta:, -xdelta:, :]
                has_data = True
                column += 1
                nb_subs_for_page += 1
                if column >= NB_COLUMNS:
                    column = 0
                    row += 1
                    if row >= NB_ROWS:
                        background_images.append({'image': background_image, 'nb': nb_subs_for_page})
                        background_image = np.full((GOOGLE_MAX_HEIGHT, GOOGLE_MAX_WIDTH, 3), 255)
                        has_data = False
                        row = 0
                        nb_subs_for_page = 0
            if has_data:
                background_images.append({'image': background_image, 'nb': nb_subs_for_page})

            for i, bg in tqdm(enumerate(background_images)):
                bg_image_path = f"{self.work_directory}/{i}.jpg"
                cv2.imwrite(bg_image_path, bg['image'])
                full_text_annotation = self.send_image_to_google(bg_image_path)
                subs = self.get_texts(full_text_annotation)
                for sub in subs[: bg['nb']]:
                    subtitles_per_frame.append(self.__clean_sub___(sub))
            logger.info(f"{len(background_images)} calls made to Google")

            df_annotations_in['subs'] = subtitles_per_frame
            df_annotations_in = df_annotations_in[df_annotations_in.subs != '']
            df_annotations_in.to_csv(file_with_subs_path, encoding='utf-8')
        return file_with_subs_path

    def __clean_sub___(self, subs: str) -> str:
        subs = re.sub("\[.+?]", "", subs)
        subs = re.sub("\[.+?", "", subs)
        subs = re.sub("\(.+?\)", "", subs)
        subs = re.sub("\(.+?", "", subs)
        return subs.strip()

    def send_image_to_google(self, bg_image_path):
        logger.debug(f"........... Appel à Google")
        with io.open(bg_image_path, 'rb') as image_file:
            content = image_file.read()
        image = vision.Image(content=content)
        response = self.vision_client.document_text_detection(image=image)
        return response.full_text_annotation

    def get_block_coord(self, block) -> int:
        bounding_box = block.bounding_box
        first_vertex = bounding_box.vertices[0]
        x0, y0 = first_vertex.x, first_vertex.y
        row = int(round(y0 / COLUMN_HEIGHT))
        column = int(round(x0 / COLUMN_WIDTH))
        index = min(row * NB_COLUMNS + column, NB_COLUMNS * NB_ROWS - 1)
        return index

    def get_texts(self, document) -> str:
        my_texts = [''] * (NB_ROWS * NB_COLUMNS)
        for page in document.pages:
            for block in page.blocks:
                text = []
                for paragraph in block.paragraphs:
                    for word in paragraph.words:
                        for symbol in word.symbols:
                            if symbol.text:
                                if hasattr(symbol, 'property') and \
                                        (hasattr(symbol.property, 'detectedBreak') or
                                         hasattr(symbol.property, 'detected_break')):
                                    if hasattr(symbol.property, 'detectedBreak'):
                                        detected_break = symbol.property.detectedBreak.type
                                    else:
                                        detected_break = symbol.property.detected_break.type_
                                    if detected_break == BREAKS.UNKNOWN:
                                        break_symbol = ''
                                    elif detected_break == 'LINE_BREAK':
                                        break_symbol = '\n'
                                    else:
                                        break_symbol = ' '
                                else:
                                    break_symbol = ''
                                if symbol.text:
                                    text.append(f"{symbol.text}{break_symbol}")
                if len(text) > 0:
                    block_index = self.get_block_coord(block)
                    my_texts[block_index] = ''.join(text)
        return my_texts

    def _get_model(self, model_name: str) -> nn.Module:
        weights_path: str = f"/tmp/{model_name}"
        model = to_best_device(AttentionSubsBoxerModel(num_encoder_layers=8))
        self.storage.download(from_path=f"models/{model_name}", to_path=weights_path)
        loaded_weights = torch.load(weights_path, map_location=torch.device('cpu'))
        model.load_state_dict(loaded_weights)
        model.eval()
        return model

    def _is_trustable_bounding_box(self, x0: int, y0: int, x1: int, y1: int) -> bool:
        return x1 - x0 > self.bb_margin and y1 - y0 > self.bb_margin

    def clean(self, video_name):
        logger.info(f"Cleaning work files of {video_name}")
        filename_without_extension = os.path.splitext(os.path.basename(video_name))[0]
        file_list = glob.glob(f"{self.work_directory}/*{filename_without_extension}*")
        for file_path in tqdm(file_list):
            if os.path.isfile(file_path):
                try:
                    logger.debug(f"Removing file {file_path}")
                    os.remove(file_path)
                except BaseException as err:
                    logger.warning(f"Error while deleting file {file_path}", err)


# PROJECT TO REFLECT AN IMAGE BASED ON A VERTICAL AND HORIZONTAL AXES
# Santiago Garcia Arango, August 2020

import numpy as np
import cv2 as cv
import os

# This way we can access upper package to save images in output folder
os.sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from SaveImg import save_img

# Get the path for the folder that contains the input and output images
upper_dir = os.path.dirname(__file__)  # Upper dir
img_folder_path = os.path.abspath(os.path.join(upper_dir, "imgs"))

# Get specific path to the images we will use inside the "imgs" folder
path_delfin = os.path.join(img_folder_path, "delfin.jpg")

# Loading our image with its path and parameter to change format
imgColor = cv.imread(path_delfin, 1)

h, w = imgColor.shape[:2]  # Get the shape of the image (height, width, c)

# Now we play to generate 3 new images with Blue, Green, Red backgrounds
x_reflection = np.zeros((h, w, 3), np.uint8)
y_reflection = np.zeros((h, w, 3), np.uint8)

for i in range(0, h):
    for j in range(0, w):
        x_reflection[i, j] = imgColor[h-1-i, j]  # The "-1" is because index 0
        y_reflection[i, j] = imgColor[i, w-1-j]  # The "-1" is because index 0

cv.imshow("Original", imgColor)
cv.imshow("X_reflected", x_reflection)
cv.imshow("Y_reflected", y_reflection)

# Save obtained images
try:
    save_img.SaveResult(x_reflection, "X_reflection.jpg", __file__)
    save_img.SaveResult(y_reflection, "Y_reflection.jpg", __file__)
except:
    pass

cv.waitKey(0)  # Show image until a key is pressed
cv.destroyAllWindows()  # (always at the end)


from math import *
import random
import copy

iternum = 10000 # iternum >= 10000 is recommanded.
sidesize = 17 # sidesize >= 5

mapsize = sidesize * sidesize

def cal_winlist(n):
    win_state_list = []

    for i in range(n):
        for j in range(n):
            # case -
            if j <= n - 5:
                tar0 = i * n + (j + 0)
                tar1 = i * n + (j + 1)
                tar2 = i * n + (j + 2)
                tar3 = i * n + (j + 3)
                tar4 = i * n + (j + 4)

                win_state_list.append((tar0, tar1, tar2, tar3, tar4))

            # case |
            if i <= n - 5:
                tar0 = (i + 0) * n + j
                tar1 = (i + 1) * n + j
                tar2 = (i + 2) * n + j
                tar3 = (i + 3) * n + j
                tar4 = (i + 4) * n + j

                win_state_list.append((tar0, tar1, tar2, tar3, tar4))

            # case \
            if i <= n - 5 and j <= n - 5:
                tar0 = (i + 0) * n + (j + 0)
                tar1 = (i + 1) * n + (j + 1)
                tar2 = (i + 2) * n + (j + 2)
                tar3 = (i + 3) * n + (j + 3)
                tar4 = (i + 4) * n + (j + 4)

                win_state_list.append((tar0, tar1, tar2, tar3, tar4))

            # case /
            if i >= 4 and j <= n - 5:
                tar0 = (i - 0) * n + (j + 0)
                tar1 = (i - 1) * n + (j + 1)
                tar2 = (i - 2) * n + (j + 2)
                tar3 = (i - 3) * n + (j + 3)
                tar4 = (i - 4) * n + (j + 4)

                win_state_list.append((tar0, tar1, tar2, tar3, tar4))

    return win_state_list

def cal_maplist(n):
    map_dot_list = []
    for i in range(n):
        map_dot_list.append(0)

    return map_dot_list

maplist = cal_maplist(mapsize)
winlist = cal_winlist(sidesize)

class GoMoKu:
    def __init__(self, state = maplist):
        self.playerJustMoved = 2
        self.state = state

    def Clone(self):
        state = GoMoKu()
        state.state = self.state[:]
        state.playerJustMoved = self.playerJustMoved
        return state

    def DoMove(self, move):
        assert move >= 0 and move <= mapsize - 1 and self.state[move] == 0
        self.playerJustMoved = 3 - self.playerJustMoved
        self.state[move] = self.playerJustMoved

    def GetMoves(self):
        if self.checkState() != 0:
            return []

        else:
            moves = []
            for i in range(mapsize):
                if self.state[i] == 0:
                    moves.append(i)

            return moves

    def GetResult(self, playerjm):
        result = self.checkState()
        assert result != 0
        if result == -1:
            return 0.5

        elif result == playerjm:
            return 1.0
        else:
            return 0.0

    def checkState(self):
        for (v,w,x,y,z) in winlist:
            if self.state[v] == self.state[w] ==  self.state[x] == self.state[y] == self.state[z]:
                if self.state[v] == 1:
                    return 1
                elif self.state[v] == 2:
                    return 2

        if [i for i in range(mapsize) if self.state[i] == 0] == []:
            return -1
        return 0

    def __repr__(self):
        s = ""
        for i in range(mapsize):
            s += ".0X"[self.state[i]]
            if i % sidesize == sidesize - 1:
                s += "\n"
        return s

class Node:
    def __init__(self, move = None, parent = None, state = None):
        self.move = move
        self.parentNode = parent
        self.childNodes = []
        self.wins = 0
        self.visits = 0
        self.untriedMoves = state.GetMoves()
        self.playerJustMoved = state.playerJustMoved

    def UCTSelectChild(self):
        s = sorted(self.childNodes, key = lambda c: c.wins/c.visits + sqrt(2 * log(self.visits) / c.visits))
        return s[-1]

    def AddChild(self, m ,s):
        n = Node(move = m, parent = self, state = copy.deepcopy(s))
        self.untriedMoves.remove(m)
        self.childNodes.append(n)
        return n

    def Update(self, result):
        self.visits += 1
        self.wins += result

    def __repr__(self):
        return "[M" + str(self.move) + " W/V " + str(self.wins) + "/" + str(self.visits) + " U" + str(self.untriedMoves) + "]"

    def ChildrenToString(self):
        s =""
        for c in self.childNodes:
            s += str(c) + "\n"
        return s

def UCT(rootstate, itermax):
    rootnode = Node(state=rootstate)

    for i in range(itermax):
        node = rootnode
        state = copy.deepcopy(rootstate)

        # selection
        while node.untriedMoves == [] and node.childNodes != []:
            node = node.UCTSelectChild()
            state.DoMove(node.move)

        # expansion
        if node.untriedMoves != []:
            m = random.choice(node.untriedMoves)
            state.DoMove(m)
            node = node.AddChild(m, state)

        # simulation
        while state.GetMoves() != []:
            state.DoMove(random.choice(state.GetMoves()))

        # BackPropagation
        while node != None:
            node.Update(state.GetResult(node.playerJustMoved))
            node = node.parentNode

    print(rootnode.ChildrenToString())

    s = sorted(rootnode.childNodes, key = lambda c: c.wins/c.visits)
    return sorted(s, key = lambda c: c.visits)[-1].move

def UCTPlayGame():
    state = GoMoKu()
    while state.GetMoves() != []:
        if state.playerJustMoved == 2:
            rootstate = copy.deepcopy(state)
            m = UCT(rootstate, itermax = iternum)
        else:
            while True:
                # m n
                try:
                    m, n = input("which Do you want? : ").split()
                    m = int(m);
                    m -= 1
                    n = int(n);
                    n -= 1
                    m = m * sidesize + n
                except:
                    m = -1

                if m < 0 or m >= mapsize: print("WRONG VALUE")
                elif state.state[m] != 0: print("WRONG VALUE")
                else: break

        # print("Best Move : " + str(m) + "\n")
        state.DoMove(m)
        print(str(state))

    if state.GetResult(state.playerJustMoved) == 1.0:
        print("Player " + str(state.playerJustMoved) + " Wins!!")

    elif state.GetResult(state.playerJustMoved) == 0.0:
        print("Payer " + str(3 - state.playerJustMoved) + " Wins!!")

    else: print("Draw!!")

if __name__ == "__main__":
    UCTPlayGame()


import csv
import Pdbc
import io

db = Pdbc.DBConnector()

tableFlocks = "flocksbfe"
tableData = "oldenburg"

flocks = db.getTable(tableFlocks)

stdin = []

for flock in flocks:
    line = ""
    members = flock[3].replace('[','').replace(']','').split(',')
    a, b = int(flock[1]),int(flock[2])
    while(a<=b):
        x = y = 0
        for member in members:
            pos = db.getData("SELECT * from {0} WHERE oid = '{1}' and otime = '{2}'".format(tableData, member, a))
            x = x + pos[2]
            y = y + pos[3]
        centroidX = x / len(members)
        centroidY = y / len(members)
        if a != b:
            centroid = str(centroidX) +" "+ str(centroidY) + ","
        else:
            centroid = str(centroidX) +" "+ str(centroidY)
        line +=  centroid
        a+=1
    line = "LINESTRING ("+ line + ")"
    stdin.append('{0}\t{1}\t{2}\t{3}\t{4}'.format(flock[0],flock[1], flock[2], flock[3], line))

table = ('flocklines')
stdin = '\n'.join(stdin)

db.createTableFlockLines(table)
db.resetTable(table)
db.copyToTable(table,io.StringIO(stdin))


# -*- coding: utf-8 -*-
"""
This class handles the importance sampling.
"""

import os
import sys

root_dir = 'H:\\GDP\\GDP_prediction_minimal_implementation\\' #Set to working directory.
os.chdir(root_dir)
sys.path.append(root_dir)

from utils.geosampler import GeoSampler
from utils.CountryIterator import CountryIterator
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import pandas as pd

np.random.seed(7775)

clipped_images_directory = r"C:/Data/Nightlights/2020/CountryClips/"
filename_pattern = "Joined_VIIRS_{}_{}.tif"
county_list_file = os.path.join(clipped_images_directory,"counties.csv")

save_folder = os.path.join(root_dir,"subnational_samples\\") #Where the samples should be saved.
geobounds_pattern = "{}_{}_geobounds.tif" #Filepath pattern for a file that gives the country borders / bounds.
pps_pattern = "{}_{}_pps.tif" #Filepath pattern for the file that gives the pps sample's first stage.
coordinate_write_file = "{}_{}_sample.txt" #Gives the coordinate list.

n = 250 #Number of points to sample per country.

county_codes = CountryIterator.get_county_codes(county_list_file)

count = 0
for _, r in county_codes.iterrows():
    county = r.county
    state = r.state
    county_path = clipped_images_directory + filename_pattern.format(state,county)
    if os.path.isfile(save_folder + coordinate_write_file.format(state,county)):
        print("Skipping " + county)
        continue

    print("Starting " + county)

    sampler = GeoSampler(county_path) #The GeoSampler class handles all the sampling logic.
    sampler.initialise_selection_probabilities(0.01, True) #Epsilon=0.01. See paper for details about this parameter.
    bounds_image = sampler.visualise_geobounds()
    bounds_image.save(save_folder + geobounds_pattern.format(state,county),"TIFF") #I can't remember what the point of this file is. Might just be for debugging?

    sampler.PPS_sample(n, progress_bar=True)
    sample_image = sampler.visualise_sample()
    sample_image.save(save_folder + pps_pattern.format(state,county),"TIFF") #I think this file is also for debugging.

    sampler.get_sample_points()
    np.savetxt(save_folder + coordinate_write_file.format(state,county), sampler.sample_points, delimiter=',')

    print(county + " complete")


# -*- coding: utf-8 -*-
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.restapi.testing import PloneRestApiDXLayer
from plone.testing import z2

import collective.volto.secondarymenu
import plone.restapi

class VoltoSecondaryMenuLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        # Load any other ZCML that is required for your tests.
        # The z3c.autoinclude feature is disabled in the Plone fixture base
        # layer.
        self.loadZCML(package=plone.restapi)
        self.loadZCML(package=collective.volto.secondarymenu)

    def setUpPloneSite(self, portal):
        applyProfile(portal, "collective.volto.secondarymenu:default")

VOLTO_SECONDARYMENU_FIXTURE = VoltoSecondaryMenuLayer()

VOLTO_SECONDARYMENU_INTEGRATION_TESTING = IntegrationTesting(
    bases=(VOLTO_SECONDARYMENU_FIXTURE,),
    name="VoltoSecondaryMenuLayer:IntegrationTesting",
)

VOLTO_SECONDARYMENU_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(VOLTO_SECONDARYMENU_FIXTURE,),
    name="VoltoSecondaryMenuLayer:FunctionalTesting",
)

class VoltoSecondaryMenuRestApiLayer(PloneRestApiDXLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        super(VoltoSecondaryMenuRestApiLayer, self).setUpZope(
            app, configurationContext
        )

        self.loadZCML(package=plone.restapi)
        self.loadZCML(package=collective.volto.secondarymenu)

    def setUpPloneSite(self, portal):
        applyProfile(portal, "collective.volto.secondarymenu:default")

VOLTO_SECONDARYMENU_API_FIXTURE = VoltoSecondaryMenuRestApiLayer()
VOLTO_SECONDARYMENU_API_INTEGRATION_TESTING = IntegrationTesting(
    bases=(VOLTO_SECONDARYMENU_API_FIXTURE,),
    name="VoltoSecondaryMenuRestApiLayer:Integration",
)

VOLTO_SECONDARYMENU_API_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(VOLTO_SECONDARYMENU_API_FIXTURE, z2.ZSERVER_FIXTURE),
    name="VoltoSecondaryMenuRestApiLayer:Functional",
)


# -*- coding: utf-8 -*-
import sys
import json
import argparse
import os
from collections import defaultdict

"""
"""

def main(fi, args):
    token_freqs = defaultdict(int)
    for line in fi:
        tokens = json.loads(line)
        for token in tokens:
            token_freqs[token["surface"]] += 1

    singleton_txt = os.path.join(args.dest, 'singleton.txt')
    vocab_txt = os.path.join(args.dest, 'vocab.txt')

    singleton_ids = defaultdict(lambda: len(singleton_ids))
    vocab_ids = defaultdict(lambda: len(vocab_ids))
    vocab_ids["<UNK>"]
    for token, freq in token_freqs.items():
        if token.strip() == "":
            continue
        if freq == 1:
            singleton_ids[token] # keep the record of singletons
        vocab_ids[token] # all vocabulary goes here

    with open(singleton_txt, 'w') as singleton_fo:
        for token, _id in singleton_ids.items():
            singleton_fo.write("{}\t{}\n".format(token, _id))

    with open(vocab_txt, 'w') as vocab_fo:
        for token, _id in vocab_ids.items():
            vocab_fo.write("{}\t{}\n".format(token, _id))

    sys.stderr.write("Original Vocab Size:{}\nNumber of Singletons:{}\nNumber of Normal Vocabs:{}\n".format(
        len(token_freqs),
        len(singleton_ids),
        len(vocab_ids)
        ))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="")
    parser.add_argument('--dest', required=True, type=str, help='destination dir')
    args = parser.parse_args()
    main(sys.stdin, args)


'''
Editor de video

'''

import shutil
from pathlib import Path
import os
import ffmpeg

def trim(in_file ,out_file, start, end):
    """ Funcion encargada de recortar video"""
    if os.path.exists(out_file):
        os.remove(out_file)

    input_stream = ffmpeg.input(in_file)

    pts = "PTS-STARTPTS"
    video = input_stream.trim(start = start,end = end).setpts(pts)
    audio = (input_stream
            .filter_("atrim", start = start, end = end)
            .filter_("asetpts",pts))
    video_and_audio = ffmpeg.concat(video, audio, v=1, a=1)
    output = ffmpeg.output(video_and_audio, out_file, format='mp4')
    output.run()
    shutil.move(out_file,get_path())    #Mueve el video a la carpeta con el archivo base

def convert(in_file):
    """Convertidor de video"""
    ffmpeg.input(in_file).output('hola.mp4').run()

def get_path():
    """ Funcion encargada de devolver path de nuestros archivos"""
    path = Path("D:\\Ingenieria en Computacion\\Proyectos personales\\Convertidor de Video\\Recortar video\\Videos")
    return path

def get_input(path):
    """Funcion que se encarga de devolver path del archivo de entrada y nombre del de salida"""
    ext = '.mkv'
    for file in os.listdir(path):
        if file.endswith(ext):
            file_name = Path(file).stem      #tomamos el nombre del archivo sin extension
            return path.joinpath(file), file_name + '_out' + ext
    return

if __name__ == '__main__':

    in_name, out_name= get_input(get_path())
    start = int(input("Ingrese en que segundo empezara el video: "))
    end_trim = int(input("Ingrese en que segundo terminara el video: "))
    trim(in_name,out_name,start,end_trim)


"""
TODO: explain
"""

import argparse
import datetime
import csv
from typing import List, Tuple

import pandas as pd

_NON_MEASUREMENT_COLUMNS = {
    'STATION', 'STATION_NAME', 'ELEVATION', 'LATITUDE', 'LONGITUDE', 'DATE'
    #'STATION', 'DATE', 'SOURCE', 'LATITUDE', 'LONGITUDE', 'ELEVATION', 'NAME',
    #'REPORT_TYPE', 'CALL_SIGN', 'QUALITY_CONTROL'
}

def _main():
    args = _parse_args()
    step_size = {
        '1sec': 1,
        '5sec': 5,
        '10sec': 10,
        '30sec': 30,
        '1min': 60,
        '5min': 60 * 5,
        '10min': 60 * 10,
        '30min': 60 * 30
    }[args.granularity]

    df = pd.read_csv(args.input)
    df.sort_values(['STATION', 'DATE'], inplace=True)
    float_cols = list(df.select_dtypes(include=['float']).columns)
    rows = df.to_dict(orient='record')

    with open(args.output, 'wt') as f:
        writer = csv.DictWriter(f, fieldnames=rows[0].keys())
        writer.writeheader()
        for a, b in zip(rows[:-1], rows[1:]):
            writer.writerow(a)
            if _primary_key(a) == _primary_key(b):
                a_dt = _to_dt(a['DATE'])
                b_dt = _to_dt(b['DATE'])

                # Based on the input granularity/step size, determine how many
                # samples to add between point a and b,
                n_steps = int((b_dt - a_dt).total_seconds() / step_size)
                interpolated_values = {
                    col: _interpolate(float(a[col]), float(b[col]), n_steps)
                    for col in a.keys() if col in float_cols
                }
                for step in range(1, n_steps):
                    row = {
                        col: val for col, val in a.items()
                        if col not in float_cols
                    }
                    row['DATE'] = (
                        (a_dt + datetime.timedelta(seconds=(step * step_size)))
                        .strftime(_DT_FORMAT))
                    row.update({
                        col: f'{interpolated_values[col][step]:.4f}'
                        for col in a.keys()
                        if col in float_cols
                    })
                    writer.writerow(row)

        # Need to write the final row of the input CSV after the interpolation
        # loop.
        writer.writerow(b)

def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-i', '--input',
        required=True,
        help='path to input file to oversample')
    parser.add_argument(
        '-o', '--output',
        required=True,
        help='path of oversampled file to generate')
    parser.add_argument(
        '-g', '--granularity',
        required=True,
        choices=('1min', '10min'),
        help='granularity of oversampling. This determines how many extra '
             'samples will be generated between two time points.')
    return parser.parse_args()

def _primary_key(row: dict) -> Tuple[str, str]:
    return row['STATION']

def _to_dt(val: str) -> datetime.datetime:
    return datetime.datetime.strptime(val, _DT_FORMAT)

_DT_FORMAT = '%Y-%m-%dT%H:%M%S'

def _interpolate(start: float, end: float, n_steps: int) -> List[float]:
    step_size = (end - start) / n_steps
    return [start + step_size * step for step in range(n_steps)]

if __name__ == '__main__':
    _main()


from setuptools import find_packages,setup

from typing import List

HYPEN_E_DOT='-e .'
def get_requirements(file_path:str)->List[str]:

    #this function will return the list of get_requirements

    requirements=[]
    with open(file_path) as file_obj:
        requirements=file_obj.readlines()#this will read requirement from requirement text file line by line
        requirements=[req.replace("\n"," ") for req in requirements]#this for lopp willl replace \n with blank space while reading from requirement txt file

        if HYPEN_E_DOT in requirements:
            requirements.remove(HYPEN_E_DOT)

        return requirements

setup(
#documentation
name='mlproject',
version='0.0.1',
author='vaibhav',
author_email='vaibhavsmart44@gmail.com',
packages=find_packages(),
#instead of giving requirements we can inclue file path from where it can get installation reqirement
install_requires=get_requirements('requirements.txt')

)


import unittest
from bioport.mail_validation import check_email

class TestEmailValidation(unittest.TestCase):

    def test_email_validation_correct(self):
        self.failUnless(check_email('me@example.com'))
        self.failUnless(check_email('me+can_have-some:special_chars@example.com'))
        self.failUnless(check_email('E.J.vanderVeldt1@uu.nl'))

    def test_email_validation_incorrect(self):
        self.failIf(check_email('meexample.com'))
        self.failIf(check_email('@ll.com'))

def test_suite():
    test_suite = unittest.TestSuite()
    tests = [TestEmailValidation]
    for test in tests:
        test_suite.addTest(unittest.makeSuite(test))
    return test_suite

if __name__ == "__main__":
    unittest.main(defaultTest='test_suite')


from random import randint

replacements = {
    ' ': [' '],
    '{': ['{'],
    '}': ['}'],
    'a': ['🥔', '🍠'],
    'b': ['🍌'],
    'c': ['🥐'],
    'd': ['🍑', '🍈'],
    'e': ['🍒', '🫐', '🍇'],
    'f': ['🌽'],
    'g': ['🍐'],
    'h': ['🍉'],
    'i': ['🍍', '🥭'],
    'j': ['🥨'],
    'k': ['🥒'],
    'l': ['🌶', '🫑'],
    'm': ['🍅'],
    'n': ['🍓'],
    'o': ['🥝'],
    'p': ['🥕','🥦'],
    'q': ['🥯'],
    'r': ['🥥'],
    's': ['🍊', '🍋'],
    't': ['🍏', '🍎'],
    'u': ['🥖'],
    'v': ['🥬'],
    'w': ['🥑'],
    'x': ['🍆'],
    'y': ['🧅', '🧄'],
    'z': ['🫒']
}

def encrypt(plain):
    cipher = ''
    for i in plain:
        chars = replacements[i]
        l = len(chars)
        cipher += replacements[i][randint(0,l-1)]
    return cipher

def decrypt(cipher):
    plain = ''
    for i in cipher:
        for j in replacements:
            if i in replacements[j]:
                plain += j
    return plain

# encrypt plain text
plain = 'possibly you have heard about ciphers which map single plaintext letters to more than one ciphertext letters these are called homophonic ciphers the solution is hypervitaminosis'
cipher = encrypt(plain)
print(cipher)

# check
plain2 = decrypt(cipher)
assert (plain == plain2)


classTimes = [(30, 75), (0, 50), (60, 150), (2, 10), (40, 70), (9, 200)]
maxLectures = 0
overlapRange = [0, 0]
for lecture1 in classTimes:
    currMax = 0
    overlapRange = [lecture1[0], lecture1[1]]
    for lecture2 in classTimes:
        start2 = lecture2[0]
        finish2 = lecture2[1]
        if start2 <= overlapRange[1] and finish2 >= overlapRange[0]:
            currMax += 1
            overlapRange[0] = max(overlapRange[0], start2)
            overlapRange[1] = min(overlapRange[1], finish2)
    if currMax > maxLectures:
        maxLectures = currMax

print("Minimum required classrooms is " + str(maxLectures))


import numpy as np
import torch
from lib.utils.visualization import Visualizer, vis_results
from lib.utils.eval_utils import compute_acc_F1, compute_AP, compute_auc_ap
from tqdm import tqdm
import pickle as pkl

def inference(cfg, epoch, model, dataloader, device, logger=None, iteration_based=False):
    model.eval()
    max_iters = len(dataloader)

    viz = Visualizer(cfg, mode='image')
    # loss_act, loss_intent = 0, 0
    gt_bboxes, gt_intents, det_intents, all_image_pathes = [],[],[],[]
    dataloader.dataset.__getitem__(0)
    all_relation_features = {}
    with torch.set_grad_enabled(False):
        for iters, batch in enumerate(tqdm(dataloader), start=1):
            x_ped = batch['obs_bboxes'].to(device)
            ego_motion = batch['obs_ego_motion'].to(device) if cfg.MODEL.WITH_EGO else None
            x_neighbor = batch['neighbor_bboxes']
            cls_neighbor = batch['neighbor_classes']
            x_light = batch['traffic_light']
            x_sign = batch['traffic_sign']
            x_crosswalk = batch['crosswalk']
            x_station = batch['station']

            cur_img_path = batch['cur_image_file']
            image_files = batch['image_files']
            pids = batch['pids']
            target_intent = batch['obs_intent']

            int_det_scores, relation_feature = model(x_ped,
                                                     x_neighbor,
                                                     cls_neighbor,
                                                     x_ego=ego_motion,
                                                     x_light=x_light,
                                                     x_sign=x_sign,
                                                     x_crosswalk=x_crosswalk,
                                                     x_station=x_station)
            relation_feature = relation_feature.cpu().numpy()
            for i in range(len(image_files)):
                for t in range(len(image_files[i])):
                    img_id = image_files[i][t].split('/')[-1].split('.')[0]
                    key = pids[i] + '_' + img_id
                    if key not in all_relation_features:
                        all_relation_features[key] = relation_feature[i, t:t+1]
            bboxes = x_ped
            gt_intents.append(target_intent.view(-1).numpy())
            gt_bboxes.append(bboxes.detach().cpu().numpy())

            if int_det_scores is not None:
                if int_det_scores.shape[-1] == 1:
                    int_det_scores = int_det_scores.sigmoid().detach().cpu()
                else:
                    int_det_scores = int_det_scores.softmax(dim=-1).detach().cpu()
                det_intents.append(int_det_scores.view(-1, int_det_scores.shape[-1]).numpy())
            if cfg.VISUALIZE and iters % max(int(len(dataloader)/15), 1) == 0:
                bboxes = bboxes.detach().cpu().numpy()
                if cfg.DATASET.BBOX_NORMALIZE:
                    # NOTE: denormalize bboxes
                    _min = np.array(cfg.DATASET.MIN_BBOX)[None, None, :]
                    _max = np.array(cfg.DATASET.MAX_BBOX)[None, None, :]
                    bboxes = bboxes * (_max - _min) + _min

                id_to_show = np.random.randint(bboxes.shape[0])
                gt_behaviors, pred_behaviors = {}, {}

                if 'intent' in cfg.MODEL.TASK:
                    target_intent = target_intent.detach().cpu().numpy()
                    int_det_scores = int_det_scores.softmax(dim=-1).detach().cpu().numpy()
                    gt_behaviors['intent'] = target_intent[id_to_show, -1]
                    pred_behaviors['intent'] = int_det_scores[id_to_show, -1]

                vis_results(viz,
                            cur_img_path[id_to_show],
                            bboxes[id_to_show][-1],
                            gt_behaviors=gt_behaviors,
                            pred_behaviors=pred_behaviors,
                            name='intent_test',
                            logger=logger)
    predictions = {'gt_bboxes': gt_bboxes,
                    'gt_intents': gt_intents,
                    'det_intents': det_intents,
                    'frame_id': all_image_pathes,
                    }
    pkl.dump(all_relation_features, open('relation_features_test.pkl', 'wb'))

    # compute accuracy and F1 scores
    # NOTE: PIE paper uses simple acc and f1 computation: score > 0.5 is positive, score < 0.5 is negative
    result_dict = {}
    if iteration_based:
        info = 'Iters: {}; \n'.format(epoch)
    else:
        info = 'Epoch: {}; \n'.format(epoch)

    if 'intent' in cfg.MODEL.TASK:
        gt_intents = np.concatenate(gt_intents, axis=0)
        det_intents = np.concatenate(det_intents, axis=0)
        info += 'Intent:\n'
        if cfg.DATASET.NUM_INTENT == 2:
            res, info = compute_auc_ap(det_intents, gt_intents, info, _type='intent')
            res_acc_F1, info = compute_acc_F1(det_intents, gt_intents, info, _type='intent')
            res.update(res_acc_F1)
            res['score_difference'] = np.mean(det_intents[gt_intents==1]) - np.mean(det_intents[gt_intents==0])
            info += 'score_difference:{:3}; '.format(res['score_difference'])
        else:
            res, info = compute_AP(det_intents, det_intents, info, _type='intent')
        result_dict.update(res)

    if hasattr(logger, 'log_values'):
        logger.info(info)
        logger.log_values(result_dict)
    else:
        print(info)

    return result_dict


from tests.envs import EnvFactory
from tests.envs import OsimWrapper
import os
import Utils
from pynput.mouse import Button, Controller
import opensim
from time import sleep
import numpy as np
import shutil
import subprocess

def main(model_name):

    # Get env
    env = EnvFactory.get(model_name)

    # Get OsimWrapper
    wrapper = OsimWrapper(env, visualize=True)

    # Wait a sec for visualizer window to initialize
    sleep(2)

    # Initialise mouse for stupid automatic image record workaround
    mouse = Controller()

    # Set frame rate and camera parameters
    visualizer = wrapper.osim_model.model.getVisualizer().getSimbodyVisualizer()
    visualizer.setCameraFieldOfView(0.785398)
    pos = env.camera_pos[:3]
    visualizer.setCameraTransform(opensim.Transform(opensim.Rotation(opensim.Mat33(0, 0, 1, 0, 1, 0, -1, 0, 0)),
                                                    opensim.Vec3(pos[0], pos[2], -pos[1])))
    visualizer.setBackgroundType(opensim.SimTKVisualizer.SolidColor)
    visualizer.setBackgroundColor(opensim.Vec3(0, 0, 0))

    # Can't figure out how to change frame rate, let's use default (which is this hopefully)
    frame_rate = visualizer.getDesiredFrameRate()

    # Run simulation for a couple of frames to make sure background color has really changed
    wrapper.reset()
    for _ in range(5):
        wrapper.step([0]*len(env.initial_states["actuators"]))

    # Go through all runs and record videos of them
    runs = os.listdir(env.output_folder)

    # Some runs fail
    failing_runs = ["run_1586166189310"]

    for run in runs:

        # If outputs are already produces (states file and simulation video) don't process this subject
        run_folder = os.path.join(env.forward_dynamics_folder, run)
        states_output_file = os.path.join(run_folder, 'osim_states.sto')
        output_video_file = os.path.join(env.output_folder, run, 'simulation.mp4')
        if os.path.isfile(states_output_file) and os.path.isfile(output_video_file):
            continue

        # Some runs fail, don't process them
        if run in failing_runs:
            continue

        # Make sure 'python3_1' folder doesn't exist
        osim_video_folder = '/home/aleksi/Workspace/O2MConverter/tests/python3_1'
        if os.path.exists(osim_video_folder):
            shutil.rmtree(osim_video_folder)

        # Reset osim simulation
        wrapper.reset()

        # Get controls
        values, hdr = Utils.parse_sto_file(os.path.join(run_folder, 'controls.sto'))
        controls = values.values
        muscle_names = list(values)

        # Make sure muscles are in correct order
        if muscle_names != wrapper.muscle_names:
            raise IndexError('Muscles are in incorrect order')

        # A stupid workaround to record images of the simulation
        mouse.position = (3030, 700)
        mouse.click(Button.left, 1)
        sleep(1)
        mouse.position = (3100, 815)
        mouse.click(Button.left, 1)
        sleep(1)

        # Run simulation
        states = np.zeros((controls.shape[0], len(wrapper.joint_names)+1))
        for i in range(controls.shape[0]):
            obs, _, _, _ = wrapper.step(controls[i, :])
            states[i, 0] = wrapper.osim_model.get_state().getTime()
            states[i, 1:] = obs

        # Stop video capture
        mouse.position = (3030, 700)
        mouse.click(Button.left, 1)
        sleep(1)
        mouse.position = (3100, 815)
        mouse.click(Button.left, 1)

        # Save states for later analysis
        states_output_file = os.path.join(run_folder, 'osim_states.sto')

        # Delete the file if it exists
        if os.path.isfile(states_output_file):
            os.remove(states_output_file)

        # Create the file
        with open(states_output_file, 'w') as file:

            # Write 'endheader' so we can use Utils.parse_sto_file to read the file
            file.write('endheader\n')

            # Write header
            file.write('time\t' + '\t'.join(wrapper.joint_names) + '\n')

            # Write the array
            np.savetxt(file, states, delimiter='\t')

        # Create the video from images; images should be in 'python3_1' folder
        if not os.path.exists(osim_video_folder):
            print("There's no output folder for run {}!".format(run_folder))
        else:

            # Remove the video if it exists
            if os.path.isfile(output_video_file):
                os.remove(output_video_file)

            # Create the video
            subprocess.call(["ffmpeg",
                             "-r", str(frame_rate),
                             "-i", os.path.join(osim_video_folder, "Frame%04d.png"),
                             output_video_file])

if __name__ == "__main__":
    #main(sys.argv[1])
    main("mobl_arms")


from __future__ import division
from builtins import range
import numpy as np
from functools import wraps
from nose.plugins.attrib import attr
import os
import matplotlib.pyplot as plt

from pyhsmm import models, distributions
from pybasicbayes.util import testing

##########
#  util  #
##########

def runmultiple(n):
    def dec(fn):
        @wraps(fn)
        def wrapper():
            fig = plt.figure()
            for i in range(n):
                yield fn, fig
            plt.close('all')
        return wrapper
    return dec

def mkdir(path):
    # from
    # http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python
    import errno
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

figure_dir_path = os.path.join(os.path.dirname(__file__),'figures')
mkdir(figure_dir_path)

###########
#  tests  #
###########

@attr('slow')
@runmultiple(2)
def discrete_geweke_test(fig):
    Nstates = 2
    Nemissions = 2
    alpha = 3.
    init_state_concentration=3.
    T = 10
    num_iter = 10000
    num_checks = 10

    obs_distns = [distributions.Categorical(K=Nemissions,alpha_0=1.)
            for _ in range(Nstates)]

    hmm = models.HMM(
            alpha=alpha,init_state_concentration=init_state_concentration,
            obs_distns=obs_distns)

    # generate state sequences and parameters from the prior
    prior_stateseqs = []
    prior_weights = []
    for itr in range(num_iter):
        hmm.resample_model() # sample parameters from the prior
        _, stateseq = hmm.generate(T,keep=False)
        prior_stateseqs.append(stateseq)
        prior_weights.append(hmm.obs_distns[0].weights)
    prior_stateseqs = np.array(prior_stateseqs)
    prior_weights = np.array(prior_weights)

    # generate state sequences and parameters using Gibbs
    hmm.generate(T,keep=True)
    s = hmm.states_list[0]

    gibbs_stateseqs = []
    gibbs_weights = []
    for itr in range(num_iter):
        s.data = None
        hmm._generate_obs(s)  # resamples data given state sequence, obs params
        hmm.resample_model()  # resamples everything else as usual
        gibbs_stateseqs.append(s.stateseq)
        gibbs_weights.append(hmm.obs_distns[0].weights)
    gibbs_stateseqs = np.array(gibbs_stateseqs)
    gibbs_weights = np.array(gibbs_weights)

    # test that they look similar by checking probability of co-assignment
    time_indices = np.arange(T)
    for itr in range(num_checks):
        i,j = np.random.choice(time_indices,replace=False,size=2)
        prior_prob_of_coassignment = (prior_stateseqs[:,i] == prior_stateseqs[:,j]).std()
        gibbs_prob_of_coassignment = (gibbs_stateseqs[:,i] == gibbs_stateseqs[:,j]).std()

        assert np.isclose(
                prior_prob_of_coassignment,gibbs_prob_of_coassignment,
                rtol=0.025,atol=0.025,
                )

    # test that they look similar by checking parameters
    testing.populations_eq_quantile_plot(prior_weights,gibbs_weights,fig=fig)
    figpath = os.path.join(figure_dir_path,'discrete_geweke_test_weights.pdf')
    plt.savefig(figpath)

@attr('slow')
@runmultiple(2)
def discrete_geweke_multiple_seqs_test(fig):
    Nstates = 2
    Nemissions = 2
    alpha = 3.
    init_state_concentration=3.
    T = 10
    num_seqs = 3
    num_iter = 10000
    num_checks = 10

    obs_distns = [distributions.Categorical(K=Nemissions,alpha_0=1.)
            for _ in range(Nstates)]

    hmm = models.HMM(
            alpha=alpha,init_state_concentration=init_state_concentration,
            obs_distns=obs_distns)

    # generate state sequences and parameters from the prior
    prior_stateseqss = [[] for _ in range(num_seqs)]
    prior_weights = []
    for itr in range(num_iter):
        hmm.resample_model() # sample parameters from the prior

        for itr2 in range(num_seqs):
            _, stateseq = hmm.generate(T,keep=False)
            prior_stateseqss[itr2].append(stateseq)

        prior_weights.append(hmm.obs_distns[0].weights)

    prior_stateseqss = np.array(prior_stateseqss)
    assert prior_stateseqss.shape == (num_seqs,num_iter,T)
    prior_weights = np.array(prior_weights)

    # generate state sequences and parameters using Gibbs
    for itr in range(num_seqs):
        hmm.generate(T,keep=True)
    assert len(hmm.states_list) == num_seqs

    gibbs_stateseqss = [[] for _ in range(num_seqs)]
    gibbs_weights = []
    for itr in range(num_iter):
        for s in hmm.states_list:
            s.data = None
            hmm._generate_obs(s)  # resamples data given state sequence, obs params
        hmm.resample_model()  # resamples everything else as usual

        for itr2, s in enumerate(hmm.states_list):
            gibbs_stateseqss[itr2].append(s.stateseq)

        gibbs_weights.append(hmm.obs_distns[0].weights)

    gibbs_stateseqss = np.array(gibbs_stateseqss)
    assert gibbs_stateseqss.shape == (num_seqs,num_iter,T)
    gibbs_weights = np.array(gibbs_weights)

    # test that they look similar by checking probability of co-assignment
    time_indices = np.arange(T)
    seq_indices = np.arange(num_seqs)
    for itr in range(num_checks):
        i,j = np.random.choice(time_indices,replace=False,size=2)
        si,sj = np.random.choice(seq_indices,replace=True,size=2)
        prior_prob_of_coassignment = \
                (prior_stateseqss[si,:,i] == prior_stateseqss[sj,:,j]).std()
        gibbs_prob_of_coassignment = \
                (gibbs_stateseqss[si,:,i] == gibbs_stateseqss[sj,:,j]).std()

        assert np.isclose(
                prior_prob_of_coassignment,gibbs_prob_of_coassignment,
                rtol=0.025,atol=0.025,
                )

    # test that they look similar by checking parameters
    testing.populations_eq_quantile_plot(prior_weights,gibbs_weights,fig=fig)
    figpath = os.path.join(figure_dir_path,
            'discrete_geweke_multiple_seqs_test_weights.pdf')
    plt.savefig(figpath)


def lengthOfLastWord(s):
    s = s.split()
    c = []
    for i in s:
        c.append(len(i))
    return ((c[::-1][0]))

t1 = "   fly me   to   the moon  "
t2 = "Today is a nice day"
print(lengthOfLastWord(t2))


from transformers import AutoTokenizer, AutoModelForSequenceClassification
import nltk
from torch import argmax
from nltk.corpus import stopwords
from config import Config

class TextClassifier():

    def __init__(self) -> None:
        print("model is loading...")
        self.check_point = Config.TEXT_CLASSIFIER_MODEL_NAME
        self.tokenizer = AutoTokenizer.from_pretrained(self.check_point)
        self.model = AutoModelForSequenceClassification.from_pretrained(self.check_point)

    def inference(self, text: str) -> None:
        text = self.preprocessing(text)
        tokens = self.tokenizer(text, padding=True,
                                truncation=True,
                                return_tensors="pt",
                                max_length=Config.TOKENIZER_MAX_LENGTH)

        output = self.model(**tokens)
        label_num = argmax(output.logits)
        label_emotion = Config.labels_dict[label_num.item()]
        print(f"Emotion: {label_emotion}")
        return label_emotion

    def preprocessing(self, text:str) -> str:
        print("preprocessor is loading...")
        # nltk.download('stopwords')
        stopword = list(stopwords.words('english'))
        text = ' '.join([word for word in text.split() if word not in (stopword)])
        return text


'''
Problem:
We have login data with the following information:
* used_id
* date: seven consecutive days: 1,2...7
* login: if a user logs into our software on that date: 1 mean yes, 0 means no
We can assume that the data is complete, ordered by used_id and date and there are no duplicates

Compute the maximum number of consecutive days (max_no_login) that each user doesn't log in, return results in a dictionary.

Example:
login_data = {'user_id':[1,1,1,1,1,1,1,2,2,2,2,2,2,2],
'date':[1,2,3,4,5,6,7,1,2,3,4,5,6,7],
'login': [1,1,0,0,0,0,1,0,0,0,1,0,1,0]}
Expected results:
{'user_id':[1,2],
'max_no_login':[4,3]}
'''

def count_no_login(login_data):
    users = login_data['user_id']
    us = 0
    max_nonlog = []
    user_nolog = []
    for i in range(len(users)):
        if users[i] != us or i==len(users)-1:
            us  = login_data['user_id'][i]
            if i > 0:
                max_nonlog.append(max_count)
                user_nolog.append(users[i-1])
            max_count = 0
            counter = 0
        if int(login_data['login'][i]) == 0:
            counter += 1
        else:
            if max_count < counter:
                max_count = counter
            counter = 0
    list_nolog = {'user_id':user_nolog,'max_no_login':max_nonlog}
    return list_nolog

if __name__== '__main__':
    login_data = {'user_id':[1,1,1,1,1,1,1,2,2,2,2,2,2,2],
    'date':[1,2,3,4,5,6,7,1,2,3,4,5,6,7],
    'login':[1,1,0,0,0,0,1,0,0,0,1,0,1,0]}
    print(count_no_login(login_data))


def main():
    N = int(input())
    S = input()
    A = [0]
    for i, s in enumerate(S):
        if s == 'L':
            A.insert(max(0, i-1), i+1)
        else:
            A.insert(i, i+1)
        print(A)

    print(A)
    print("{}".format(" ".join([a for a in A])))

if __name__ == "__main__":
    main()


import ctypes
import ipdb

class PyFF(object):
    """
    Python interface to the FF planner.
    """
    def __init__(self,ff_lib='/usr/local/lib/libff.so',max_plan_str_length=8192):
        self.cff = ctypes.cdll.LoadLibrary(ff_lib)
        self.max_plan_str_length=max_plan_str_length

    def plan(self,domain_file,problem_file):
        self.cff = ctypes.cdll.LoadLibrary('/usr/local/lib/libff.so')
        cmd_tokens = ("ff -o %s -f %s"%(domain_file,problem_file)).split()
        arr = (ctypes.c_char_p * len(cmd_tokens))()
        arr[:]=cmd_tokens
        plan_str = ctypes.create_string_buffer(self.max_plan_str_length)
        #ipdb.set_trace()
        if self.cff.ff_plan(ctypes.pointer(plan_str), len(cmd_tokens), arr):
            print('Found plan')
            return plan_str.value.split('\n')
        else:
            print('Failed to find plan.')
            return []


from functools import reduce

file_name = "input-part2.txt"

input_file = open(file_name)
bus_times = [None if time == 'x' else int(time) for time in input_file.readline().strip().split(',')]
print(bus_times)

def chinese_remainder(n, a):
    sum = 0
    prod = reduce(lambda a, b: a*b, n)
    for n_i, a_i in zip(n, a):
        p = prod // n_i
        sum += a_i * mul_inv(p, n_i) * p
    return sum % prod

def mul_inv(a, b):
    b0 = b
    x0, x1 = 0, 1
    if b == 1: return 1
    while a > 1:
        q = a // b
        a, b = b, a%b
        x0, x1 = x1 - q * x0, x0
    if x1 < 0: x1 += b0
    return x1

dividers = []
remainders = []

index = 0
for time in bus_times:
    if time:
        dividers.append(time)
        remainders.append(time-index)
    index += 1

print(dividers)
print(remainders)
print(chinese_remainder(dividers, remainders))


import numpy as np
import matplotlib.pyplot as plt

def transform_map(map_image, rot_mat,
                  in_projection='platecarre',
                  out_projection='platecarre',
                  display=True,
                  debug=False):

    invalid_projection_msg = (
        "Please choose a projection from "
        "'platecarre' or 'gallpeters'."
    )

    ny, nx, _ = map_image.shape
    mapdims = (nx, ny)

    if debug:
        print(f'ny, nx are {ny} and {nx}')
    xs, ys = np.meshgrid(np.arange(0, nx), np.arange(0, ny))
    xs = xs.flatten().astype(float)
    ys = ys.flatten().astype(float)
    if debug:
        print(f'shape of xs and ys are {xs.shape} and {ys.shape}')
        print(f'xs min and max are {min(xs)} and {max(xs)}')
        print(f'ys min and max are {min(ys)} and {max(ys)}')

    if in_projection == 'platecarre':
        longs, lats = longitudeAndLatitudeFromPCPixel(xs, ys, mapdims)
    elif in_projection == 'gallpeters':
        longs, lats = longitudeAndLatitudeFromGPPixel(xs, ys, mapdims)
    else:
        raise ValueError(invalid_projection_msg)

    if debug:
        print(f'shape of longs and lats are {longs.shape} and {lats.shape}')
        print(f'longitudes histogram')
        plt.hist(longs)
        print(f'latitudes histogram')
        plt.hist(lats)

    xxs, yys, zzs = spatialCoordinatesFromLongitudeAndLatitude(longs, lats)
    if debug:
        print('pre rotation histograms')
        plt.hist(xxs)
        plt.hist(yys)
        plt.hist(zzs)
    new_xxyyzz = np.dot(rot_mat, np.array([xxs, yys, zzs]))

    new_xxs = np.squeeze(np.array(new_xxyyzz[0, :]))
    new_yys = np.squeeze(np.array(new_xxyyzz[1, :]))
    new_zzs = np.squeeze(np.array(new_xxyyzz[2, :]))
    if debug:
        print('post rotation hists')
        plt.hist(new_xxs)
        plt.hist(new_yys)
        plt.hist(new_zzs)

    new_longs, new_lats = longitudeAndLatitudeFromSpatialCoordinates(
        new_xxs, new_yys, new_zzs)
    if debug:
        print('new longs')
        plt.hist(new_longs)
        print('new lats')
        plt.hist(new_lats)

    if out_projection == 'platecarre':
        new_xs, new_ys = pixel_for_plate_carre(new_longs, new_lats, mapdims)
    elif out_projection == 'gallpeters':
        new_xs, new_ys = gall_peters_pixels(new_longs, new_lats, mapdims)
    else:
        raise ValueError(invalid_projection_msg)

    if debug:
        print('new x pixels and new y pixels')
        plt.hist(new_xs)
        plt.hist(new_ys)

        print(f'shape of new xs and new ys is {new_xs.shape} {new_ys.shape}')
        print(f'ny, nx are {ny}, and {nx}')

    new_xs.shape = (ny, nx)
    new_ys.shape = (ny, nx)

    new_map = np.zeros(dtype=map_image.dtype, shape=map_image.shape)

    new_map[:, :, 0] = map_image[new_ys, new_xs, 0]
    new_map[:, :, 1] = map_image[new_ys, new_xs, 1]
    new_map[:, :, 2] = map_image[new_ys, new_xs, 2]

    if display:
        plt.imshow(new_map)
    return(new_map)

def longitudeAndLatitudeFromGPPixel(x, y, mapdims):
    """
    GP Projection:

    x = R lambda / sqrt(2)
    y = R sqrt(2) sin(phi)
        lambda is longitude (left right)
        phi is latitude (up down)
        R is radius of globe, we'll use 1.0
    lambda = (x/R) * sqrt(2)
    phi = arcsin((y/R) / sqrt(2))
    """
    nx, ny = mapdims
    longitude = x * (2.0 * np.pi / float(nx)) - np.pi
    latitude = np.arcsin(2.0 * ((y / ny) - (0.5)))
    return(longitude, latitude)

def longitudeAndLatitudeFromPCPixel(x, y, mapdims):
    nx, ny = mapdims
    longitude = 2 * np.pi * ((x / float(nx)) - 0.5)
    latitude = - (np.pi * ((y / float(ny)) - 0.5))
    return (longitude, latitude)

def spatialCoordinatesFromLongitudeAndLatitude(longitude, latitude):
    x = np.cos(longitude) * np.cos(latitude)
    y = np.sin(longitude) * np.cos(latitude)
    z = np.sin(latitude)
    return (x, y, z)

def flip_longitudes(longitude):
    #  which need to be flipped?
    inds = np.greater(longitude, np.pi)
    longitude = longitude - 2 * np.pi * inds
    return(longitude)

def longitudeAndLatitudeFromSpatialCoordinates(xxs, yys, zzs):
    latitude = np.arcsin(zzs)
    longitude = np.arctan2(yys, xxs)
    longitude = flip_longitudes(longitude)
    return((longitude, latitude))

def gall_peters_pixels(longitude, latitude, mapdims):
    nx, ny = mapdims

    u = nx * (0.5 + (longitude / (2 * np.pi)))
    v = (ny / 2) * (np.sin(latitude) + 1.0)

    u = u.astype(int)
    v = v.astype(int)

    #  Happens w/ rounding.
    u[u == nx] = nx - 1
    v[v == ny] = 0

    return((u, v))

def pixel_for_plate_carre(longitude, latitude, mapdims):
    nx, ny = mapdims

    u = nx * (0.5 + (longitude / (2 * np.pi)))
    v = - (ny * (0.5 + (latitude / np.pi)))

    u = u.astype(int)
    v = v.astype(int)

    u[u == nx] = nx - 1
    v[v == ny] = ny - 1

    return(u, v)

def rotationMatrix(axis_vector, angle):
    axis_vector = axis_vector / np.sqrt(sum(axis_vector**2))
    (x, y, z) = axis_vector

    tensor_product_matrix = np.matrix([
        [x**2, x * y, x * z],
        [x * y, y**2, y * z],
        [x * z, y * z, z**2]
    ])
    cross_product_matrix = np.matrix([
        [0.0, -z, y],
        [z, 0.0, -x],
        [-y, x, 0.0]
    ])
    R = np.eye(3) * np.cos(angle) + \
        cross_product_matrix * np.sin(angle) + \
        tensor_product_matrix * (1 - np.cos(angle))

    return R

def rotationFromXYZ(angle_x, angle_y, angle_z):
    r_x = rotationMatrix(np.array([1.0, 0.0, 0.0]), angle_x)
    r_y = rotationMatrix(np.array([0.0, 1.0, 0.0]), angle_y)
    r_z = rotationMatrix(np.array([0.0, 0.0, 1.0]), angle_z)

    return (np.dot(np.dot(r_x, r_y), r_z))

def rotationFromTwoLocations(
        longlatA,
        longlatB):
    """
    Given two tuples (longitude and latitude),
    finds a rotation that places the first center left,
    and the second center right.
    """
    vec1 = spatialCoordinatesFromLongitudeAndLatitude(*longlatA)
    vec2 = spatialCoordinatesFromLongitudeAndLatitude(*longlatB)

    vec1 = np.array(vec1)
    vec2 = np.array(vec2)
    vec1 = vec1 / np.linalg.norm(vec1)
    vec2 = vec2 / np.linalg.norm(vec2)

    middle = vec1 + vec2
    middlevec = middle / np.linalg.norm(middle)

    crossprod = np.cross(vec1, vec2)
    crossvec = crossprod / np.linalg.norm(crossprod)

    #  Determines handedness / puts them in the center
    # thirdvec = np.cross(crossvec, middlevec)
    # thirdvec = thirdvec / np.linalg.norm(thirdvec)

    #  diffvec
    diffvec = vec2 - vec1
    diffvec = diffvec / np.linalg.norm(diffvec)

    rotation = np.matrix([middlevec, diffvec, crossvec])

    return(rotation.T)

def eulerAxisAngleFromRotationMatrix(R):
    """
    Returns a tuple, (theta, v)
    """
    theta = np.arccos(0.5 * (R[0,0] + R[1,1,] + R[2,2] - 1))

    v1 = R[2, 1] - R[1, 2]
    v2 = R[0, 2] - R[2, 0]
    v3 = R[1, 0] - R[0, 1]

    vec = np.array([v1, v2, v3]) / (2.0 * np.sin(theta))

    return(theta, vec)

def quaternionFromMatrix(R):
    """
    Returns a list [qi, qj, qk, qr]
    """
    qr = 0.5 * np.sqrt(1 + np.trace(R))

    v1 = R[2, 1] - R[1, 2]
    v2 = R[0, 2] - R[2, 0]
    v3 = R[1, 0] - R[0, 1]

    qi, qj, qk = [v / 4.0 * qr for v in [v1, v2, v3]]

    return [qi, qj, qk, qr]