import requests
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from django.utils.dateparse import parse_datetime

from dimagi.data.blog import nav_categories
from dimagi.data.case_studies import studies
from dimagi.data.sectors import sub_sectors
from dimagi.data.terms import PREVIOUS_TERMS, LATEST_TERMS
from dimagi.data.toolkits import toolkits
from dimagi.data.quick_start import all_areas
from dimagi.pages.models.terms import VERSION_2
from dimagi.utils.wordpress_api import get_json

class MainViewSitemap(Sitemap):
    protocol = 'https'

    @staticmethod
    def _get_changefreq(slug):
        view_to_changefreq = {
            'home': 'daily',
            'commcare': 'daily',
            'commcare_home': 'daily',
            'careers': 'daily',
            'blog_home': 'daily',
        }
        return view_to_changefreq.get(slug, 'weekly')

    @staticmethod
    def _get_priority(slug):
        view_to_priority = {
            'home': 1.0,
            'commcare': 1.0,
            'commcare_pricing': 1.0,
            'services': 0.9,
            'about': 0.9,
            'contact': 0.8,
            'careers': 0.8,
            'toolkits': 0.6,
            'sectors': 0.6,
            'press': 0.6,
            'blog_home': 0.7,
            'archive': 0.6,
            'team': 0.9,
            'monitoring_evaluation': 0.9,
            'commcare_features': 0.9,
            'ict4d': 0.9,
            'enterprise': 0.9,
            'mobile_data_collection': 0.9,
            'data_collection': 0.9,
            'community_health_worker': 0.9,

        }
        return view_to_priority.get(slug, 0.5)

    def items(self):
        return [
            'home',
            'services',
            'contact',
            'india',
            'resources',
            'research_and_data',
            'partners',
            'focus',
            'awards',
            'covid_19',
            'ict4d',
            'covid19',
            'webinar_home',
            'global_me',
            'data_already',
            'optimize',
            'interoperability',
            'data_democracy',
            'frontline',
            'adapt',
            'new_function',
            'data_clean',
            'remote',
            'power_bi',
            'focus_mdm',
            'focus_mdm_pricing',
            'research',
            'commcare_onboarding',
            'classroom',
            'commcare_messaging',
            'enterprise',
            'vaccine_delivery',
            'monitoring_evaluation',
            'commcare_features',
            'sector_nutrition_programs',
            'sector_disease_treatment',
            'sector_agriculture_extension_programs',
            'financial_inclusion',
            'commcare_integration',
            'mobile_data_collection',
            'data_collection',
            'community_health_worker',
            'us_health',
            'data_driven_programs',
            'partner_program',
            'careers',
            'commcare',
            'commcare_pricing',
            'case_studies',
            'toolkits',
            'sectors',
            'press',
            'archive',
            'blog_home',
            'team',
            'about',
            'proposals',
            'quick_start',
            'case_management',
        ]

    def priority(self, obj):
        return self._get_priority(obj)

    def changefreq(self, obj):
        return self._get_changefreq(obj)

    def location(self, obj):
        return reverse(obj)

class BlogPostSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'never'
    priority = 0.6

    def items(self):
        return get_json('blog/sitemap').get('posts', [])

    def location(self, obj):
        return reverse('blog_post', args=[obj.get('slug')])

    def lastmod(self, obj):
        return parse_datetime(obj.get('date'))

class BlogArchiveCategorySitemap(Sitemap):
    protocol = 'https'
    changefreq = 'daily'
    priority = 0.6

    def items(self):
        return nav_categories

    def location(self, obj):
        return reverse('archive_category', args=[obj.slug])

class SectorSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'monthly'
    priority = 0.5

    def items(self):
        return sub_sectors

    def location(self, obj):
        return reverse('sector', args=[obj.SECTOR.slug])

class CaseStudySitemap(Sitemap):
    protocol = 'https'
    changefreq = 'monthly'
    priority = 0.5

    def items(self):
        return studies

    def location(self, obj):
        return reverse('case_study', args=[obj.STUDY.slug])

class ToolkitSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'monthly'
    priority = 0.5

    def items(self):
        return toolkits

    def location(self, obj):
        return reverse('toolkit', args=[obj.TOOLKIT.slug])

class QuickStartSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'monthly'
    priority = 0.5

    def items(self):
        return all_areas

    def location(self, obj):
        return reverse('quick_start_area', args=[obj.AREA.slug])

class TermsSitemap(Sitemap):
    protocol = 'https'
    priority = 0.4

    def items(self):
        terms = [
            'default',
            'latest',
            'previous'
        ]
        terms.extend(PREVIOUS_TERMS)
        terms.extend(LATEST_TERMS)
        return terms

    def location(self, obj):
        if obj == 'default':
            return reverse('terms_default')

        if obj in ['latest', 'previous']:
            return reverse('terms_version', args=[obj])

        if obj.version == VERSION_2:
            version = 'previous'
        else:
            version = 'latest'

        return reverse('terms', args=[version, obj.slug])

class JobsSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'daily'
    priority = 0.6

    def items(self):
        data = requests.get("https://api.greenhouse.io/v1/boards/dimagi/jobs")
        data = data.json()
        return data.get('jobs', [])

    def location(self, obj):
        return reverse('careers_job', args=[obj['id']])

class TeamMemberSitemap(Sitemap):
    protocol = 'https'
    changefreq = 'monthly'
    priority = 0.5

    def items(self):
        return get_json('team/sitemap')

    def location(self, obj):
        return reverse(
            'team_member', args=[obj['office'], obj['slug']]
        )


from tqdm import tqdm
import torch
import numpy as np
from utils.io import writeFlow
import torch.nn as nn

def epe(input_flow, target_flow, mean=True):
    """
    End-point-Error computation
    Args:
        input_flow: estimated flow [BxHxW,2]
        target_flow: ground-truth flow [BxHxW,2]
    Output:
        Averaged end-point-error (value)
    """
    EPE = torch.norm(target_flow - input_flow, p=2, dim=1)
    if mean:
        EPE = EPE.mean()
    return EPE

def correct_correspondences(input_flow, target_flow, alpha, img_size):
    """
    Computation PCK, i.e number of the pixels within a certain threshold
    Args:
        input_flow: estimated flow [BxHxW,2]
        target_flow: ground-truth flow [BxHxW,2]
        alpha: threshold
        img_size: image size
    Output:
        PCK metric
    """
    # input flow is shape (BxHgtxWgt,2)
    dist = torch.norm(target_flow - input_flow, p=2, dim=1)
    # dist is shape BxHgtxWgt
    pck_threshold = alpha * img_size
    mask = dist.le(pck_threshold) # Computes dist ≤ pck_threshold element-wise (element then equal to 1)
    return mask.sum().item()

def F1_kitti_2015(input_flow, target_flow, tau=[3.0, 0.05]):
    """
    Computation number of outliers
    for which error > 3px(tau[0]) and error/magnitude(ground truth flow) > 0.05(tau[1])
    Args:
        input_flow: estimated flow [BxHxW,2]
        target_flow: ground-truth flow [BxHxW,2]
        alpha: threshold
        img_size: image size
    Output:
        PCK metric
    """
    # input flow is shape (BxHgtxWgt,2)
    dist = torch.norm(target_flow - input_flow, p=2, dim=1)
    gt_magnitude = torch.norm(target_flow, p=2, dim=1)
    # dist is shape BxHgtxWgt
    mask = dist.gt(3.0) & (dist/gt_magnitude).gt(0.05)
    return mask.sum().item()

def calculate_epe_and_pck_per_dataset(test_dataloader, network, device, threshold_range, path_to_save=None,
                                      compute_F1=False, save=False):
    aepe_array = []
    pck_alpha_0_05_over_image = []
    pck_thresh_1_over_image = []
    pck_thresh_5_over_image = []
    F1 = 0.0

    n_registered_pxs = 0.0
    array_n_correct_correspondences = np.zeros(threshold_range.shape, dtype=np.float32)

    pbar = tqdm(enumerate(test_dataloader), total=len(test_dataloader))
    for i_batch, mini_batch in pbar:
        source_img = mini_batch['source_image']
        target_img = mini_batch['target_image']
        mask_gt = mini_batch['correspondence_mask'].to(device)
        flow_gt = mini_batch['flow_map'].to(device)
        if flow_gt.shape[1] != 2:
            # shape is BxHxWx2
            flow_gt = flow_gt.permute(0,3,1,2)
        bs, ch_g, h_g, w_g = flow_gt.shape

        flow_estimated = network.estimate_flow(source_img, target_img, device, mode='channel_first')

        # torch tensor of shape Bx2xH_xW_, will be the same types (cuda or cpu) depending on the device
        # H_ and W_ could be smaller than the ground truth flow (ex DCG Net takes only 240x240 images)
        if flow_estimated.shape[2] != h_g or flow_estimated.shape[3] != w_g:
            '''
            the estimated flow is downscaled (the original images were downscaled before
            passing through the network)
            as it is the case with DCG Net, the estimate flow will have shape 240x240
            it needs to be upscaled to the same size as flow_target_x and rescaled accordingly:
            '''
            ratio_h = float(h_g) / float(flow_estimated.shape[2])
            ratio_w = float(w_g) / float(flow_estimated.shape[3])
            flow_estimated = nn.functional.interpolate(flow_estimated, size=(h_g, w_g), mode='bilinear',
                                                       align_corners=False)
            flow_estimated[:, 0, :, :] *= ratio_w
            flow_estimated[:, 1, :, :] *= ratio_h
        assert flow_estimated.shape == flow_gt.shape

        flow_target_x = flow_gt.permute(0, 2, 3, 1)[:, :, :, 0]
        flow_target_y = flow_gt.permute(0, 2, 3, 1)[:, :, :, 1]
        flow_est_x = flow_estimated.permute(0, 2, 3, 1)[:, :, :, 0]  # B x h_g x w_g
        flow_est_y = flow_estimated.permute(0, 2, 3, 1)[:, :, :, 1]

        flow_target = \
            torch.cat((flow_target_x[mask_gt].unsqueeze(1),
                       flow_target_y[mask_gt].unsqueeze(1)), dim=1)
        flow_est = \
            torch.cat((flow_est_x[mask_gt].unsqueeze(1),
                       flow_est_y[mask_gt].unsqueeze(1)), dim=1)
        # flow_target_x[mask_gt].shape is (number of pixels), then with unsqueze(1) it becomes (number_of_pixels, 1)
        # final shape is (B*H*W , 2), B*H*W is the number of registered pixels (according to ground truth masks)

        # let's calculate EPE per batch
        aepe = epe(flow_est, flow_target)  # you obtain the mean per pixel
        aepe_array.append(aepe.item())

        # let's calculate PCK values
        img_size = max(mini_batch['source_image_size'][0], mini_batch['source_image_size'][1]).float().to(device)
        alpha_0_05 = correct_correspondences(flow_est, flow_target, alpha=0.05, img_size=img_size)
        px_1 = correct_correspondences(flow_est, flow_target, alpha=1.0/float(img_size), img_size=img_size) # threshold of 1 px
        px_5 = correct_correspondences(flow_est, flow_target, alpha=5.0/float(img_size), img_size=img_size) # threshold of 5 px

        # percentage per image is calculated for each
        pck_alpha_0_05_over_image.append(alpha_0_05/flow_target.shape[0])
        pck_thresh_1_over_image.append(px_1/flow_target.shape[0])
        pck_thresh_5_over_image.append(px_5/flow_target.shape[0])

        # PCK curve for different thresholds ! ATTENTION, here it is over the whole dataset and not per image
        n_registered_pxs += flow_target.shape[0]  # also equal to number of correspondences that should be correct
        # according to ground truth mask
        for t_id, threshold in enumerate(threshold_range):
            array_n_correct_correspondences[t_id] += correct_correspondences(flow_est,
                                                                             flow_target,
                                                                             alpha=threshold,
                                                                             img_size=img_size)
            # number of correct pixel correspondence below a certain threshold, added for each batch

        if compute_F1:
            F1 += F1_kitti_2015(flow_est, flow_target)

        if save:
            writeFlow(np.dstack([flow_est_x[0].cpu().numpy(), flow_est_y[0].cpu().numpy()]),
            'batch_{}'.format(i_batch), path_to_save)

    output = {'final_eape': np.mean(aepe_array),
              'pck_alpha_0_05_average_per_image': np.mean(pck_alpha_0_05_over_image),
              'pck_thresh_1_average_per_image': np.mean(pck_thresh_1_over_image),
              'pck_thresh_5_average_per_image': np.mean(pck_thresh_5_over_image),
              'alpha_threshold': threshold_range.tolist(),
              'pixel_threshold': np.round(threshold_range * img_size.cpu().numpy(), 2).tolist(),
              'pck_per_threshold_over_dataset': np.float32(array_n_correct_correspondences /
                                                           (n_registered_pxs + 1e-6)).tolist()}

    if compute_F1:
        output['kitti2015-F1'] = F1 / float(n_registered_pxs)
    return output


from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (
    AtrRsiStrategy,
)
from datetime import datetime

engine = BacktestingEngine()
engine.set_parameters(
    vt_symbol="IF88.CFFEX",
    interval="1m",
    start=datetime(2019, 1, 1),
    end=datetime(2019, 4, 30),
    rate=0.3/10000,
    slippage=0.2,
    size=300,
    pricetick=0.2,
    capital=1_000_000,
)
engine.add_strategy(AtrRsiStrategy, {})

engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
engine.show_chart()

setting = OptimizationSetting()
setting.set_target("sharpe_ratio")
setting.add_parameter("atr_length", 3, 39, 1)
setting.add_parameter("atr_ma_length", 10, 30, 1)

engine.run_ga_optimization(setting)


from django.shortcuts import render, get_object_or_404
from .models import Portfolio

# Create your views here.

def all_portfolio(request):
    """ A view to return the index page """

    portfolios = Portfolio.objects.all()

    context = {
        'portfolios': portfolios,
    }

    return render(request, 'portfolio/portfolio.html', context)

def portfolio_details(request, portfolio_id):
    """ A view single portfolio details"""

    portfolio = get_object_or_404(Portfolio, pk=portfolio_id)

    context = {
        'portfolio': portfolio,
    }

    return render(request, 'portfolio/portfolio_details.html', context)


"""Add product table

Revision ID: 434a7905b168
Revises: ceff866606d9
Create Date: 2020-05-24 20:19:01.064978

"""
import salsa
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '434a7905b168'
down_revision = 'ceff866606d9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('product',
    sa.Column('id', salsa.db.types.GUID(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=True),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.Text(), nullable=False),
    sa.Column('image_urls', sa.ARRAY(sa.String(length=255)), nullable=False),
    sa.Column('user_id', salsa.db.types.GUID(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['user_account.id'], name=op.f('fk_product_user_id_user_account')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_product'))
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('product')
    # ### end Alembic commands ###


even = [2, 4, 6, 8, 10, 12]
print(even[1:4])

fruits = ['apple']

favorite = input("好きなフルーツは？")

if favorite in fruits:
    print("{}ですね、あるよん".format(favorite))
    fruits.remove(favorite)
else:
    print("{}ですね、仕入れました。".format(favorite))
    fruits.append(favorite)

print("今あるフルーツ{}".format(fruits))


from collections import deque
def solution(s):
    if s[0]==")": return False

    s = deque(list(s))
    temp = 0
    for i in range(len(s)):
        s_left = s.popleft()
        if temp < 0: return False
        if s_left=="(": temp += 1
        elif s_left==")": temp -= 1

    if temp != 0: return False
    else: return True

# 개선 코드
def is_pair(s):
    pair = 0
    for x in s:
        if pair < 0: break
        pair = pair + 1 if x == "(" else pair - 1 if x == ")" else pair
        # x=="("면 pair + 1 그 반대면 pair -1
        # 중간에 pair가 음수가 되면 잘못된 괄호 이므로 반복문 종료 이때 반복문 종료시 pair != 0 이므로 return 값은 자연스럽게 False가 됨.
    return pair == 0
        # pair가 0인 경우 = 괄호가 정상인 경우 = True / 그 외에는 전부 False로 출력

프로그래머스/lv2/12909. 올바른 괄호/올바른 괄호.py


# 좋아하시는 프링글스 스윗 마요 프로그램입니다.

hour , minu , seco = input().split()
hour = int(hour)
minu = int(minu)
seco = int(seco)
while True:
    bakeTime = int(input())
    if(0 <= bakeTime <= 500000):
        break
    else:
        print("값이 너무 큽니다, 500000 이하로 입력해주세요.")

allTime = hour*3600 + minu*60 + seco + bakeTime
allTime = allTime % 86400
hour, allTime = int(allTime/3600), int(allTime%3600)
minu, allTime = int(allTime/60), int(allTime%60)
seco = int(allTime)

print(hour, minu, seco)


from __future__ import annotations

import argparse
import json
import re
from typing import Any, Dict, List

import together
from together import Complete
from together.utils import get_logger

logger = get_logger(str(__name__))

def add_parser(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) -> None:
    COMMAND_NAME = "complete"
    subparser = subparsers.add_parser(COMMAND_NAME)

    subparser.add_argument(
        "prompt",
        metavar="PROMPT",
        default=None,
        type=str,
        help="A string providing context for the model to complete.",
    )

    subparser.add_argument(
        "--model",
        "-m",
        default=together.default_text_model,
        type=str,
        help=f"The name of the model to query. Default={together.default_text_model}",
    )

    subparser.add_argument(
        "--no-stream",
        default=False,
        action="store_true",
        help="Indicates wether to disable streaming",
    )

    subparser.add_argument(
        "--max-tokens",
        default=128,
        type=int,
        help="Maximum number of tokens to generate. Default=128",
    )
    subparser.add_argument(
        "--stop",
        default=["<human>"],
        nargs="+",
        type=str,
        metavar="STOP_WORD",
        help="Strings that will truncate (stop) text generation. Default='<human>'",
    )
    subparser.add_argument(
        "--temperature",
        default=0.7,
        type=float,
        help="Determines the degree of randomness in the response. Default=0.7",
    )
    subparser.add_argument(
        "--top-p",
        default=0.7,
        type=float,
        help="Used to dynamically adjust the number of choices for each predicted token based on the cumulative probabilities. Default=0.7",
    )
    subparser.add_argument(
        "--top-k",
        default=50,
        type=int,
        help="Used to limit the number of choices for the next predicted word or token. Default=50",
    )
    subparser.add_argument(
        "--repetition-penalty",
        default=None,
        type=float,
        help="Controls the diversity of generated text by reducing the likelihood of repeated sequences. Higher values decrease repetition.",
    )
    subparser.add_argument(
        "--logprobs",
        default=None,
        type=int,
        help="Specifies how many top token log probabilities are included in the response for each token generation step.",
    )
    subparser.add_argument(
        "--raw",
        default=False,
        action="store_true",
        help="temperature for the LM",
    )
    subparser.set_defaults(func=_run_complete)

def _enforce_stop_tokens(text: str, stop: List[str]) -> str:
    """Cut off the text as soon as any stop words occur."""
    return re.split("|".join(stop), text)[0]

def no_streamer(args: argparse.Namespace, response: Dict[str, Any]) -> None:
    if args.raw:
        print(json.dumps(response, indent=4))

    else:
        if "output" in response.keys():
            if "choices" in dict(response["output"]).keys():
                text = str(response["output"]["choices"][0]["text"])
                print(text.strip())
            elif "error" in dict(response["output"]).keys():
                raise together.ResponseError(response["output"]["error"])
            else:
                raise together.ResponseError(
                    f"Unknown error occured. Received unhandled response: {response}"
                )

        elif "error" in response.keys():
            if response["error"] == "Returned error: no instance":
                message = f"No running instances for {args.model}. You can start an instance by navigating to the Together Playground at api.together.xyz"
                raise together.InstanceError(model=args.model, message=message)
            else:
                raise together.ResponseError(
                    message=f"Error raised: {response['error']}"
                )

        else:
            raise together.ResponseError("Unknown response received. Please try again.")

def _run_complete(args: argparse.Namespace) -> None:
    complete = Complete()

    if args.no_stream:
        try:
            response = complete.create(
                prompt=args.prompt,
                model=args.model,
                max_tokens=args.max_tokens,
                stop=args.stop,
                temperature=args.temperature,
                top_p=args.top_p,
                top_k=args.top_k,
                repetition_penalty=args.repetition_penalty,
                logprobs=args.logprobs,
            )
        except together.AuthenticationError:
            logger.critical(together.MISSING_API_KEY_MESSAGE)
            exit(0)
        no_streamer(args, response)
    else:
        try:
            for text in complete.create_streaming(
                prompt=args.prompt,
                model=args.model,
                max_tokens=args.max_tokens,
                stop=args.stop,
                temperature=args.temperature,
                top_p=args.top_p,
                top_k=args.top_k,
                repetition_penalty=args.repetition_penalty,
                raw=args.raw,
            ):
                if not args.raw:
                    print(text, end="", flush=True)
                else:
                    print(text)
        except together.AuthenticationError:
            logger.critical(together.MISSING_API_KEY_MESSAGE)
            exit(0)
        if not args.raw:
            print("\n")


def fib(n):
    arr = [0, 1]
    for i in range(2, n + 1):
        arr.append(arr[i - 1] + arr[i - 2])
    return arr[n]

def fib_last_digit(n):
    arr = [0, 1]
    for i in range(2, n + 1):
        arr.append((arr[i - 1] + arr[i - 2]) % 10)
    print(arr)
    return arr[n]

def fib_mod(n, m):
    arr = [0, 1]
    for i in range(2, n + 1):
        arr.append((arr[i - 1] + arr[i - 2]) % m)
        if arr[i - 1] == 1 and arr[i] == 0:
            return arr[n % i]
    return n % m

def main():
    n, m = map(int, input().split())
    print(fib_mod(n, m))

if __name__ == "__main__":
    main()


import typing as ty
import reapy_boost as rpr

def get_bounds(midi: ty.Iterable[rpr.MIDIEventDict]) -> ty.Tuple[float, float]:
    start, end = float('inf'), 0
    for event in midi:
        if not event['selected']:
            continue
        if start > event['ppq']:
            start = event['ppq']
        if end < event['ppq']:
            end = event['ppq']
    return start, end

def spread_times(notes: ty.List[rpr.Note], start: float,
                 end: float) -> ty.List[int]:
    step = (end - start) / (len(notes))
    times = []
    for i in range(len(notes) + 1):
        times.append(int(start + i * step))
    return times

@rpr.inside_reaper()
def spread() -> None:
    take = rpr.Project().selected_items[0].active_take
    midi = take.get_midi()
    # filtered = ty.cast(
    #     ty.Tuple[rpr.MIDIEventDict],
    #     tuple(
    #         filter(
    #             lambda event: event['selected'] and event['buf'][0] == 144,
    #             midi
    #         )
    #     )
    # )
    start_ppq, end_ppq = get_bounds(midi)
    notes = list(filter(lambda note: note.selected, take.notes))
    times = spread_times(notes, start_ppq, end_ppq)
    times_on_i, times_off_i = 0, 1
    for note in notes:
        note.start = take.ppq_to_time(times[times_on_i])
        note.end = take.ppq_to_time(times[times_off_i])
        times_on_i += 1
        times_off_i += 1

spread()


import base64
import os

"""
Both path & path_2 will need to be changed based on users local environment.

The deeply nested set of for loops below is sequentially finding each and only each .jpg file
of the student's written stories, converting the .jpg file to base64, and storing both the base64
and the name of the file as a .json object. That .json object is then being stored in path_2

"""

path = "C:/Users/micha/Desktop/Lambda/DS-Unit-5_Labs/Story_Squad_Dataset/Transcribed_Stories/"
path_2 = "C:/Users/micha/Desktop/Lambda/DS-Unit-5_Labs/story-squad-ds/testing/json_files"

for root, dirs, files in os.walk(path):
    for dir in dirs:
        input_path = os.path.join(path, dir)
        for root, dirs, files in os.walk(input_path):
            for dir_2 in dirs:
                input_path_2 = os.path.join(input_path, dir_2)
                for root, dirs, files in os.walk(input_path_2):
                    for name in files:
                        if name.endswith((".jpg")):
                            input_path_3 = os.path.join(input_path_2, name)
                            with open(input_path_3, "rb") as img_file:
                                my_string = base64.b64encode(img_file.read())
                                my_string = my_string.decode()

                                file_name = os.path.splitext(name)[0].replace(" ", "_")
                                json = f"{{\"images\": [\"data:image/jpg;base64,{my_string}\"], \"file_name\": \"{file_name}\"}}"

                                file_name = os.path.join(path_2, file_name+".json")
                                json_file = open(f"{file_name}", "w")
                                json_file.write(json)
                                json_file.close()


"""
    Written by Samuel Solomon

    --------------------------------------------------------------------------
    Data Aquisition:
    Each Channel Consists of 3 Electrodes: Two EMG Electrodes + 1 EMG Reference
    The Standard Setup Consists of Placing the Electrodes along a muscle group.
    The Reference Electrode Should be Placed in the Middle, And the Electrodes
    Should Line Up On the Axis From the hand to the Elbow (If Using Lower Arm).
    Provide Decent Spacing Between the Electrodes (Noticeable Gap)

    HardWare Processing:
    The Code Below Used the Following Electronic Material from Olimex:
        Circuit Board: https://www.olimex.com/Products/Duino/Shields/SHIELD-EKG-EMG/open-source-hardware
        Electrodes: https://www.olimex.com/Products/Duino/Shields/SHIELD-EKG-EMG-PRO/

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

    Note: Not ALL of the Modules are Required for Every Run (Some are ONYL if Plotting)
    Modules to Import Before Running the Program (Some May be Missing):
        $ conda install -c conda-forge tensorflow
        % conda install -c conda-forge ffmpeg
        $ conda install scikit-learn
        $ conda install matplotlib
        $ conda install openpyxl
        $ conda install pyserial
        $ conda install joblib
        $ conda install numpy
        $ conda install keras

    --------------------------------------------------------------------------
"""
# Use '%matplotlib qt' to View Plot

# Basic Modules
import sys
import numpy as np

# Import Data Aquisition and Analysis Files
sys.path.append('./Data Aquisition and Analysis/')  # Folder with Data Aquisition Files
import readDataExcel as excelData         # Functions to Save/Read in Data from Excel
import readDataArduino as streamData      # Functions to Read in Data from Arduino
import emgAnalysis as emgAnalysis         # Functions to Analyze the EMG Data

# Import Files for Machine Learning
sys.path.append('./Machine Learning/')  # Folder with Machine Learning Files
import machineLearningMain  # Class Header for All Machine Learning

if __name__ == "__main__":
    # ---------------------------------------------------------------------- #
    #    User Parameters to Edit (More Complex Edits are Inside the Files)   #
    # ---------------------------------------------------------------------- #

    # General Data Collection Information (You Will Likely Not Edit These)
    emgSerialNum = '85735313333351E040A0'    # Arduino Serial Number (port.serial_number) Collecting EMG Signals
    handSerialNum = None   # Arduino Serial Number for the Robotic Hand Control. Leave None if NOT Controlling the Hand
    numDataPoints = 50000   # The Number of Points to Stream into the Arduino
    numTimePoints = 10000    # The Number of Data Points to Display to the User at a Time; My beta-Test Used 2000 Points
    moveDataFinger = 200    # The Number of NEW Data Points to Analyze at a Time; My Beta-Test Used 200 Points with Plotting (100 Without). This CAN Change How SOME Peaks are Found (be Careful)
    samplingFreq = None     # The Average Number of Points Steamed Into the Arduino Per Second; If NONE Given, Algorithm will Calculate Based on Initial Data
    numChannels = 4         # The Number of Arduino Channels with EMG Signals Read in; My Beta-Test Used 4 Channels
    # Specify the Type of Movements to Learn
    gestureClasses = np.char.lower(["Up", "Down", "Left", "Right", "Grab", "Release"])  # Define Labels as Array
    #gestureClasses = np.char.lower(["Forwards", "Back", "Left", "Right", "Rotate Left", "Rotate Right"])  # Define Labels as Array
    #gestureClasses = np.char.lower(["Front 90", "Back", "Left", "Right 90", "Open Chest", "Shrug"])  # Define Labels as Array
    #gestureClasses = np.char.lower(["Front 45", "Front 90", "Front 135", "Front 180"])  # Define Labels as Array
    #gestureClasses = np.char.lower(["Fingers Curl", "Fingers to Thumb", "Fingers to Palm", "Full Grab"])  # Define Labels as Array
    #gestureClasses = np.char.lower(["Back", "Left", "Open Chest", "Shrug", "Right 45", "Right 90", "Right 135", "Right 180", "Front 45", "Front 90", "Front 135", "Front 180"])  # Define Labels as Array

    # Protocol Switches: Only One Can be True; Only the First True Variable Excecutes
    streamArduinoData = False  # Stream in Data from the Arduino and Analyze; Input 'testModel' = True to Apply Learning
    readDataFromExcel = True   # Analyze Data from Excel File called 'testDataExcelFile' on Sheet Number 'testSheetNum'
    reAnalyzePeaks = False     # Read in ALL Data Under 'trainingDataExcelFolder', and Reanalyze Peaks (THIS EDITS EXCEL DATA IN PLACE!; DONT STOP PROGRAM MIDWAY)
    trainModel = False         # Read in ALL Data Under 'neuralNetworkFolder', and Train the Data

    # User Options During the Run: Any Number Can be True
    plotStreamedData = False    # Graph the Data to Show Incoming Signals + Analysis
    saveModel = False          # Save the Machine Learning Model for Later Use
    testModel = True          # Apply the Learning Algorithm to Decode the Signals
    saveData = False           # Saves the Data in 'readData.data' in an Excel Named 'saveExcelName' or map2D if Training

    # ---------------------------------------------------------------------- #

    # Take Data from the Arduino and Save it as an Excel (For Later Use)
    if saveData:
        saveExcelName = "You Yu 11-12-2021 Finger Angles.xlsx"  # The Name of the Saved File
        saveDataFolder = "../Output Data/EMG Data/Upper Back/2021-11-11/"  # Data Folder to Save the Excel Data; MUST END IN '/'
        # Speficy the Movement You Will Perform
        currentMovement = "Full Grab".lower() # Make Sure it is Lowercase
        if currentMovement and currentMovement not in gestureClasses:
            print("The Gesture", "'" + currentMovement + "'", "is Not in", gestureClasses)
    else:
        saveDataFolder = False

    # Instead of Arduino Data, Use Test Data from Excel File
    if readDataFromExcel:
        testDataExcelFile = "../Output Data/EMG Data/Arm/May11 Test Results/New Analysis/Samuel Solomon 2021-05-11 Round 1 All 5 Features.xlsx" # Path to the Test Data
        testSheetNum = 5   # The Sheet/Tab Order (Zeroth/First/Second/Third) on the Bottom of the Excel Document

    # Use Previously Processed Data that was Saved; Extract Features for Training
    if reAnalyzePeaks or trainModel:
        trainingDataExcelFolder = "../Output Data/EMG Data/Arm/May11 Test Results/New Analysis/" # Path to the Training Data Folder; All .xlsx Data Used

    if trainModel or testModel and not reAnalyzePeaks:
        # Pick the Machine Learning Module to Use
        modelType = "KNN"  # Machine Learning Options: RF, LR, KNN, SVM
        modelPath = "./Machine Learning/Models/predictionModel_Arm_" + modelType + ".pkl" # Path to Model (Creates New if it Doesn't Exist)
        if trainModel:
            saveDataFolder = trainingDataExcelFolder + "Del/" + modelType + "/"
        else:
            saveDataFolder = None
        # Get the Machine Learning Module
        performMachineLearning = machineLearningMain.predictionModelHead(modelType, modelPath, dataDim = numChannels, gestureClasses = gestureClasses, saveDataFolder = saveDataFolder)
        predictionModel = performMachineLearning.predictionModel
        # Feature Labels
        featureLabels = []
        # ["peakAverage", "peakHeight", "peakVariance", "peakSTD", "maxSlope"]
        for feature in ["peakAverage", "peakHeight", "peakEnergy", "peakSTD", "maxSlope"]:
            for channel in range(1,5):
                featureLabels.append(feature + " in Channel " + str(channel))
    else:
        predictionModel = None

    # ---------------------------------------------------------------------- #
    # ---------------------------------------------------------------------- #
    #           Data Collection Program (Should Not Have to Edit)            #
    # ---------------------------------------------------------------------- #
    # ---------------------------------------------------------------------- #

    if not streamArduinoData:
        emgProtocol = emgAnalysis.emgProtocol(numTimePoints, moveDataFinger, numChannels, samplingFreq, gestureClasses, plotStreamedData)
    # Stream in Data from Arduino
    if streamArduinoData:
        arduinoRead = streamData.arduinoRead(emgSerialNum = emgSerialNum, handSerialNum = handSerialNum)
        emgProtocol = streamData.emgArduinoRead(arduinoRead, numTimePoints, moveDataFinger, numChannels, samplingFreq, gestureClasses, plotStreamedData, guiApp = None)
        emgProtocol.streamEMGData(numDataPoints, predictionModel = predictionModel, actionControl = None)
    # Take Data from Excel Sheet
    elif readDataFromExcel:
        readData = excelData.readExcel(emgProtocol)
        readData.streamExcelData(testDataExcelFile, plotStreamedData, testSheetNum, predictionModel = predictionModel, actionControl = None)
    # Redo Peak Analysis
    elif reAnalyzePeaks:
        readData = excelData.readExcel(emgProtocol)
        readData.getTrainingData(trainingDataExcelFolder, numChannels, gestureClasses, mode='reAnalyze')
    # Take Preprocessed (Saved) Features from Excel Sheet
    elif trainModel:
        # Extract the Data
        readData = excelData.readExcel(emgProtocol)
        signalData, signalLabels = readData.getTrainingData(trainingDataExcelFolder, numChannels, gestureClasses, mode='Train')
        print("\nCollected Signal Data")

        # Train the Data on the Gestures
        performMachineLearning.trainModel(signalData, signalLabels, featureLabels = featureLabels)
        # Save Signals and Labels
        if len(performMachineLearning.map2D) != 0:
            saveInputs = excelData.saveExcel(numChannels, numChannels)
            saveExcelNameMap = "MapedData_" + modelType + ".xlsx" #"Signal Features with Predicted and True Labels New.xlsx"
            saveInputs.saveLabeledPoints(performMachineLearning.map2D, signalLabels,  performMachineLearning.predictionModel.predictData(signalData), saveDataFolder, saveExcelNameMap, sheetName = "Signal Data and Labels")
        # Save the Neural Network (The Weights of Each Edge)
        if saveModel:
             performMachineLearning.predictionModel.saveModel(modelPath)

    # Save the Data in Excel: EMG Channels (Cols 1-4); X-Peaks (Cols 5-8); Peak Features (Cols 9-12)
    if saveData and not trainModel and not reAnalyzePeaks:
        # Format Sheet Name
        sheetName = "Trial 1 - "  # If SheetName Already Exists, Increase Trial # by One
        sheetName = sheetName + currentMovement
        # Double Check to See if User Wants to Save the Data
        verifiedSave = input("Are you Sure you Want to Save the Data (Y/N): ")
        if verifiedSave.upper() == "Y":
            # Initialize Class to Save the Data and Save
            saveInputs = excelData.saveExcel(numChannels, numChannels)
            saveInputs.saveData(emgProtocol.data, emgProtocol.featureList, saveDataFolder, saveExcelName, sheetName, currentMovement)
        else:
            print("User Chose Not to Save the Data")


from utils.clear_screen import clear_screen
import random

clear_screen()

deck = []

def draw_card():
    return deck.pop(random.randint(0, len(deck) - 1))

def get_score(cards):
    score = sum(cards)

    while score > 21:
        if 11 in cards:
            cards[cards.index(11)] = 1
            return get_score(cards)
        else:
            break

    return score

user_cards = []
dealer_cards = []

def display_score(is_stand):

    user_score = get_score(user_cards)
    print(f"Your deck: {user_cards} Score: {user_score}")

    if not is_stand:
        print(f"Dealer's deck: [{dealer_cards[1]}]")
    else:
        dealer_score = get_score(dealer_cards)
        print(f"Dealer's deck: {dealer_cards} Score: {dealer_score}")

def setup():
    global deck

    deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
            11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
            11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
            11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

    global user_cards
    global dealer_cards

    user_cards = [draw_card(), draw_card()]
    dealer_cards = [draw_card(), draw_card()]
    display_score(False)

setup()

def hit():
    user_cards.append(draw_card())
    display_score(False)

def stand():

    dealer_score = get_score(dealer_cards)

    while dealer_score < random.randint(15, 21):
        dealer_cards.append(draw_card())
        dealer_score = get_score(dealer_cards)

    display_score(True)

def play():

    playing = True

    user_score = get_score(user_cards)

    if user_score == 21:
        print("* * * Blackjack * * * YOU WON")
        playing = False

    while playing:

        playing_input = input("Do you want to hit or stand. h or s: ").lower()

        clear_screen()

        if playing_input == "h":
            hit()

            user_score = get_score(user_cards)
            if user_score > 21:
                print("BUST - YOU LOST")
                playing = False
            elif user_score == 21:
                print("YOU WON")
                playing = False

        else:
            stand()

            dealer_score = get_score(dealer_cards)
            user_score = get_score(user_cards)
            if dealer_score > 21:
                print("Dealer Bust - YOU WON")
            elif dealer_score == 21:
                print("YOU LOST")
            elif user_score > dealer_score:
                print("YOU WON")
            elif dealer_score > user_score:
                print("YOU LOST")
            else:
                print("DRAW")

            playing = False

    playing_input = input("Do you want to continue playing. y or n: ").lower()
    if playing_input == 'y':
        clear_screen()
        setup()
        play()

play()


import pandas as pd

#loading data
df = pd.read_csv('russell2000_1year_average_rsi.csv', sep = ';', index_col=0)

"""
The strategy works in this way: We open a long position when there’s a close
outside the lower band with the RSI in oversold territory, hence below 30; while
for short position, we short it when the close is above the upper band and the
RSI is overbought, above 70.
"""

# COLUMNS
#['Averages', 'StDevs', 'UpperBand', 'LowerBand', 'RSI', 'Close']

# CREATING THE MODEL
def model(df, endowment, rsi_buy, rsi_sell):

    #INITIALIZING ECONOMIC VALUES
    total_sold = 0 #Cumulative Stake Sold
    n_investments = 0 #Number of Investments Made
    in_market_stake = 0 #Current Stake In The Market

    # CREATE A COLUMN FOR THE DAILY RETURNS
    df['Returns'] = df['Close'].pct_change()
    df = df.dropna()

    # CREATING A COUNTER TO DEAL WITH THE LAST ITERATION
    last_iteration = len(df.index)
    count = 0

    for idx in df.index:

        count += 1 #Refresh the counter

        # BUY STATEMENT
        if df['Close'].loc[idx] < df['LowerBand'].loc[idx] and df['RSI'].loc[idx] <= rsi_buy:
            print('Buying!')
            in_market_stake += endowment
            n_investments += 1 #count the number of investments
            continue

        # SELL STATEMENT
        if df['Close'].loc[idx] > df['UpperBand'].loc[idx] and df['RSI'].loc[idx] >= rsi_sell:
            print('Selling!')
            total_sold += in_market_stake
            in_market_stake = 0
            continue

        # COMPUTE the RETURN of the investment fund DAY-BY-DAY
        in_market_stake = in_market_stake * (1 + df['Returns'].loc[idx])
        print(f'Current Stake in the Market is {in_market_stake}')

        # HANDLING LAST ITERATION -> Transfer everything to total sold
        if count == last_iteration:
            total_sold += in_market_stake
            in_market_stake = 0

    # BRUTE FORCE RETURN FORMULA; NOT 100% CORRECT TO SYMPLIFY
    numerator = (total_sold - n_investments * endowment)
    denominator = n_investments * endowment
    total_return = numerator / denominator
    print('\n')
    #to edit!
    return f'Total Return Earned {100 * round(total_return, 4)} %'

#MODEL OUTPUT
rsi_buy = 30
rsi_sell = 70
mod = model(df, 100, rsi_buy, rsi_sell) #df, endowment
print(mod)

# TO FIX:
# 1. Not convicing me the RSI COMPUTATOR
# 2. Fix the Return Formula


from Modules.Module_Basic import *
from Modules.Module_SQL import *
from Utils.jsonData import roleJsonData
from Utils.sendLog import sendLogging

class certification_btn(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(label="로그인", style=discord.ButtonStyle.green, custom_id="ok_btn")
    async def certification_user(self, interaction: discord.Interaction, button: discord.ui.Button):
        class modal(ui.Modal, title="아래의 항목에 정보를 작성해주세요."):
            user_SchoolType = ui.TextInput(
                label="학과명",
                style=discord.TextStyle.short,
                placeholder="예시) 컴퓨터공학과",
                default="",
                required=True,
                max_length=10,
            )
            user_ClassNum = ui.TextInput(
                label="학번",
                style=discord.TextStyle.short,
                placeholder="예시) 20230101",
                default="",
                required=True,
                max_length=9,
            )
            user_Name = ui.TextInput(
                label="이름",
                style=discord.TextStyle.short,
                placeholder="예시) 박세찬",
                default="",
                required=True,
                max_length=9,
            )

            async def on_submit(self, interaction: Interaction) -> None:
                db, SQL = get_SQL()
                if db is False:
                    return await interaction.response.send_message(
                        f"서버와의 연결이 불안정 합니다.", ephemeral=True
                    )

                try:
                    SQL.execute(
                        f"SELECT * FROM USER_DATA WHERE CLASS_NUM = '{str(self.user_ClassNum)}' and USER_NAME = '{str(self.user_Name)}' and SCHOOL_TYPE = '{str(self.user_SchoolType)}'"
                    )
                    ResultData = SQL.fetchone()
                    if ResultData == None or ResultData == False:
                        await sendLogging(interaction.client, "ERROR - `users_joinMember.py` 서버에서 사용자가 조회 되지 않음")
                        return await interaction.response.send_message(f"서버에서 사용자가 조회되지 않습니다.", ephemeral=True)

                    updateDiscordIdResult = await updateDiscordId(
                        discordId=interaction.user.id,
                        classNum=str(self.user_ClassNum),
                        userName=str(self.user_Name),
                        schoolType=str(self.user_SchoolType),
                    )
                    if updateDiscordIdResult != True:
                        await sendLogging(interaction.client, "ERROR - `users_joinMember.py` 디스코드 아이디 업데이트 되지 않음")
                        return await interaction.response.send_message(f"디스코드 계정에 이상이 있습니다. <@833277196947947531>에게 문의 해주세요!", ephemeral=True)

                    if str(self.user_ClassNum) == "7966":
                        await interaction.user.add_roles(interaction.guild.get_role((1143519495550410872)))
                        await interaction.user.edit(
                            nick=f"{self.user_SchoolType}_{self.user_Name}교수"
                        )
                        await interaction.response.send_message(
                            f"안녕하세요. {interaction.user.mention}!! \n{self.user_SchoolType} {self.user_Name} 교수님!!",
                            ephemeral=True,
                        )
                        await interaction.client.get_channel(1166636388070989874).send(
                            f"Log - {self.user_SchoolType} | {self.user_Name}({self.user_ClassNum}) 인증 완료 되었습니다."
                        )

                    try:
                        await interaction.user.add_roles(interaction.guild.get_role((int(roleJsonData["학생"]))))
                    except Exception as e:
                        print(e)
                        await sendLogging(interaction.client, f"ERROR - {e}")
                        pass

                    await interaction.user.edit(
                        nick=f"{self.user_SchoolType}_{self.user_Name}"
                    )
                    await interaction.response.send_message(
                        f"안녕하세요. {interaction.user.mention}!! \n{self.user_SchoolType} / {self.user_ClassNum} / {self.user_Name}님!!",
                        ephemeral=True,
                    )
                    await interaction.client.get_channel(1166636388070989874).send(
                        f"Log - {self.user_SchoolType} | {self.user_Name}({self.user_ClassNum}) 인증 완료 되었습니다."
                    )

                except Exception as e:
                    print("error", e)
                    await interaction.response.send_message(
                        f"서버에서 사용자가 조회되지 않습니다.", ephemeral=True
                    )

        await interaction.response.send_modal(modal())

class users_joinMember(commands.Cog):
    def __init__(self, client: commands.Bot) -> None:
        self.client = client

    def get_btn(self):
        return certification_btn()

    @app_commands.command(name="로그인버튼_생성", description="로그인 버튼을 생성 합니다.")
    async def JoinMember_System(self, interaction: Interaction):
        if not interaction.user.guild_permissions.administrator:
            return await interaction.response.send_message("운영팀이 아닙니다.", ephemeral=True)
        view = certification_btn()
        _channel = self.client.get_channel(1166632566242152468)
        await interaction.response.send_message(f"정상적으로 생성 되었습니다.", ephemeral=True)
        await _channel.send(
            "# 아래의 버튼을 눌러 참가자 등록을 완료 해주세요! \n### 학교명, 학번, 이름을 입력해 역할을 받으세요!", view=view
        )

async def setup(client: commands.Bot):
    await client.add_cog(users_joinMember(client))


#!/usr/bin/python3

import argparse
from secrets import randbits
import dew

def _encrypt(args):
    if (args.keyfile):
        with open(args.keyfile, 'rb') as f:
            key = int.from_bytes(f.read(), byteorder='little')
    else:
        key = randbits(256)

    nonce0 = randbits(256)
    nonce1 = randbits(255)

    if (args.cmdtext):
        with open(args.outfile, 'wb') as out:
            result = dew.transform(args.infile.encode(), key, nonce0, nonce1)
            out.write(nonce0.to_bytes(32, byteorder='little')
                      + nonce1.to_bytes(32, byteorder='little')
                      + result)
    else:
        with open('{}'.format(args.infile), 'rb') as f, open(args.outfile, 'wb') as out:
            result = dew.transform(f.read(), key, nonce0, nonce1)
            out.write(nonce0.to_bytes(32, byteorder='little')
                      + nonce1.to_bytes(32, byteorder='little')
                      + result)

    if not args.keyfile:
        with open('{}.key'.format(args.outfile), 'wb') as out:
            out.write(key.to_bytes(32, byteorder='little'))

def _decrypt(args):
    with open(args.keyfile, 'rb') as f:
        key = int.from_bytes(f.read(), byteorder='little')

    with open('{}'.format(args.infile), 'rb') as f, open(args.outfile, 'wb') as out:
        nonce0 = int.from_bytes(f.read(32), byteorder='little')
        nonce1 = int.from_bytes(f.read(32), byteorder='little')

        result = dew.transform(f.read(), key, nonce0, nonce1)
        out.write(result)

if __name__ == '__main__':
    #Main argument parser
    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers(dest='operation')
    subparsers.required = True

    #Subparser for encryption
    parser_e = subparsers.add_parser('encrypt', help='Option to select to encrypt input')
    parser_e.add_argument('--cmdtext', action='store_true',
                        help="Specifiy this command to treat the 'infile' argument as the text the encrypt")
    parser_e.add_argument('infile', help="The plaintext wanted to be encrypted/decrypted")
    parser_e.add_argument('--keyfile', help="Optional keyfile to encrypt the file, truncated/padded to 256 bits")
    parser_e.add_argument('outfile', help="Name of output encrypted file")
    parser_e.set_defaults(func=_encrypt)

    #Subparer for decryption
    parser_d = subparsers.add_parser('decrypt', help='Option to select to decrypt input')
    parser_d.add_argument('infile', help="The plaintext wanted to be encrypted/decrypted")
    parser_d.add_argument('keyfile', help="The file generated holding the key during encryption")
    parser_d.add_argument('outfile', help="Name of output unencrypted file")
    parser_d.set_defaults(func=_decrypt)

    args = parser.parse_args()
    args.func(args)


import os
import time
import numpy as np
from autognas.search_algorithm.graphpas import utils
from autognas.parallel import ParallelOperater, \
                              ParallelConfig

from autognas.datasets.planetoid import Planetoid # for unit test
from autognas.search_space.search_space_config import SearchSpace  # for unit test

class GraphPASSearch(object):
    """
    Realizing population random initializing and genetic search process,
    the genetic search process includes:
    1.selecting parents based on wheel strategy and sharing population
    2.mutating parents based on mutation_select_probability to generate children
    3.updating sharing population based on children

    Args:
        sharing_num: int
            confirming initialized sharing population scale for all searchers.
        mutation_num: list
            confirming one gnn architecture genetic list mutation number
            in the mutation process for each searcher.
        initial_num: int
            confirming the scale of initialized population.
        search_space: SearchSpace class
            preparing the search space dict and the stack gcn architecture.

    returns:
        children: list
            the children gnn architecture genetic list
        total_pop: list
            the total history gnn architecture genetic list
    """

    def __init__(self,
                 sharing_num,
                 mutation_num,
                 search_space):

        self.sharing_num = sharing_num
        self.mutation_num = mutation_num

        self.search_space = search_space.space_getter()
        self.stack_gcn_architecture = search_space.stack_gcn_architecture

    def search(self,
               total_pop,
               sharing_population,
               sharing_performance,
               mutation_selection_probability):

        print(35*"=", "graphpas search", 35*"=")
        print("sharing population:\n", sharing_population)
        print("sharing performance:\n", sharing_performance)
        print("[sharing population performance] Mean/Median/Best:\n",
              np.mean(sharing_performance),
              np.median(sharing_performance),
              np.max(sharing_performance))

        # select parents based on wheel strategy.
        parents = self.selection(sharing_population, sharing_performance)
        print("parents:\n", parents)

        # mutation based on mutation_select_probability
        children = self.mutation(parents, mutation_selection_probability, total_pop)
        print("children:\n", children)

        total_pop = total_pop + children
        return children, total_pop

    def selection(self,
                  population,
                  performance):
        print(35*"=", "select parents based on wheel strategy", 35*"=")

        fitness = np.array(performance)
        fitness_probility = fitness / sum(fitness)
        fitness_probility = fitness_probility.tolist()

        index_list = [index for index in range(len(fitness))]
        parents = []
        parent_index = np.random.choice(index_list, self.sharing_num, replace=False, p=fitness_probility)
        for index in parent_index:
            parents.append(population[index].copy())
        return parents

    def mutation(self,
                 parents,
                 mutation_selection_probability,
                 total_pop):

        print(35 * "=", "mutation based on mutation_select_probability", 35 * "=")
        for index in range(len(parents)):

            # stopping until sampling the new gnn architecture which not in the total_pop
            while parents[index] in total_pop:
                # confirming mutation point in the gnn architecture genetic list based on information_entropy_probability
                position_to_mutate_list = np.random.choice([gene for gene in range(len(parents[index]))],
                                                           self.mutation_num,
                                                           replace=False,
                                                           p=mutation_selection_probability)

                for mutation_index in position_to_mutate_list:
                    mutation_space = self.search_space[self.stack_gcn_architecture[mutation_index]]
                    parents[index][mutation_index] = np.random.randint(0, len(mutation_space))

        children = parents

        return children

    def updating(self,
                 sharing_children,
                 sharing_children_val_performance_list,
                 sharing_population,
                 sharing_performance
                 ):

        print(35*"=", "updating", 35*"=")
        print("before sharing_performance:\n", sharing_performance)

        # calculating the average fitness based on top k gnn architecture in sharing population
        _, top_performance = utils.top_population_select(sharing_population,
                                                 sharing_performance,
                                                 top_k=self.sharing_num)
        avg_performance = np.mean(top_performance)

        index = 0
        for performance in sharing_children_val_performance_list:
            if performance > avg_performance:
                sharing_performance.append(performance)
                sharing_population.append(sharing_children[index])
                index += 1
            else:
                index += 1
        print("after sharing_performance:\n", sharing_performance)
        return sharing_population, sharing_performance

class PopulationInitialization(object):

    def __init__(self,
                 initial_num,
                 search_space):

        self.initial_num = initial_num
        self.initial_gnn_architecture_embedding_list = []
        self.initial_gnn_architecture_list = []
        self.search_space = search_space.space_getter()
        self.stack_gcn_architecture = search_space.stack_gcn_architecture

    def initialize_random(self):

        print(35*"=", "population initializing based on random strategy", 35*"=")

        while len(self.initial_gnn_architecture_embedding_list) < self.initial_num:
            gnn_architecture_embedding = utils.random_generate_gnn_architecture_embedding(self.search_space,
                                                                                self.stack_gcn_architecture)
            gnn_architecture = utils.gnn_architecture_embedding_decoder(gnn_architecture_embedding,
                                                                        self.search_space,
                                                                        self.stack_gcn_architecture)
            # gnn architecture genetic embedding based on number
            self.initial_gnn_architecture_embedding_list .append(gnn_architecture_embedding)
            self.initial_gnn_architecture_list.append(gnn_architecture)

        return self.initial_gnn_architecture_embedding_list, self.initial_gnn_architecture_list

class Search(object):
    """
    Graphpas search algorithm search logic class

    Args:
        data: graph data obj
            the target graph data object
        search_parameter: dict
            the search algorithm configuration dict to control the
            automatic search process including required attributes:
            1.search_algorithm_type,2.parallel_num,3.mutation_num
            4.initial_num,5.sharing_num,6.search_epoch
            7.test_gnn_num
        gnn_parameter: dict
            the gnn configuration dict to complete the gnn model train
            validate and test based on the gnn architecture

    Return:
        none
    """

    def __init__(self, data, search_parameter, gnn_parameter, search_space):

        self.data = data
        self.search_parameter = search_parameter

        # parallel estimation operator initialize
        self.parallel_estimation = ParallelOperater(data, gnn_parameter)

        self.search_space = search_space

    def search_operator(self):

        print(35 * "=", "graphpas search start", 35 * "=")

        # searcher initializing
        searcher_list = []
        for index in range(int(self.search_parameter["parallel_num"])):
            searcher = GraphPASSearch(sharing_num=int(self.search_parameter["sharing_num"]),
                                      mutation_num=eval(self.search_parameter["mutation_num"])[index],
                                      search_space=self.search_space)
            searcher_list.append(searcher)

        # population initializing and fitness calculating
        time_initial = time.time()
        population_initialization = PopulationInitialization(int(self.search_parameter["initial_num"]), self.search_space)
        initial_gnn_architecture_embedding_list, initial_gnn_architecture_list = population_initialization.initialize_random()
        result = self.parallel_estimation.estimation(initial_gnn_architecture_list)
        total_pop = initial_gnn_architecture_embedding_list
        total_performance = result
        time_initial = time.time() - time_initial

        # initial gnn architecture time cost record
        path = os.path.split(os.path.realpath(__file__))[0][:-34] + "logger/graphpas_logger/"
        print(path)
        if not os.path.exists(path):
            os.makedirs(path)
        utils.experiment_time_save_initial(path, self.data.data_name + "_initial_time.txt", time_initial)

        # sharing population select based on top strategy
        print(35 * "=", "sharing population select", 35 * "=")
        sharing_population, sharing_performance = utils.top_population_select(total_pop,
                                                                           total_performance,
                                                                           top_k=self.search_parameter["sharing_num"])
        # mutation select probability vector calculate
        sharing_population_temp = sharing_population.copy()
        mutation_selection_probability = utils.mutation_selection_probability(sharing_population_temp,
                                                                              self.search_space.stack_gcn_architecture)
        print(35 * "=", "mutation select probability vector:", 35 * "=")
        print(mutation_selection_probability)

        print(35 * "=", "multiple mutation search_algorithm start", 35 * "=")

        time_search_list = []
        epoch = []

        for i in range(int(self.search_parameter["search_epoch"])):

            time_search = time.time()
            # parent select and mutate based on mutation select probability and mutation intensity
            sharing_children_embedding = []
            for searcher in searcher_list:

                children, total_pop = searcher.search(total_pop,
                                                      sharing_population,
                                                      sharing_performance,
                                                      mutation_selection_probability)
                sharing_children_embedding = sharing_children_embedding + children

            # sharing children decoding
            sharing_children_architecture = []
            sharing_children_val_performance_list = []

            for gnn_architecture_embedding in sharing_children_embedding:

                gnn_architecture = utils.gnn_architecture_embedding_decoder(gnn_architecture_embedding,
                                                                            self.search_space.space_getter(),
                                                                            self.search_space.stack_gcn_architecture)
                sharing_children_architecture.append(gnn_architecture)

            # sharing children parallel estimation
            result = self.parallel_estimation.estimation(sharing_children_architecture)

            for performance in result:

                sharing_children_val_performance_list += [performance]

            # sharing population updating
            sharing_population, sharing_performance = searcher_list[0].updating(sharing_children_embedding,
                                                                             sharing_children_val_performance_list,
                                                                             sharing_population,
                                                                             sharing_performance)

            #  mutation select probability vector recalculate
            sharing_population_temp = sharing_population.copy()
            mutation_selection_probability = utils.mutation_selection_probability(sharing_population_temp, self.search_space.stack_gcn_architecture)
            time_search_list.append(time.time()-time_search)
            epoch.append(i+1)

            # model architecture and val performance record
            path = os.path.split(os.path.realpath(__file__))[0][:-34] + "logger/graphpas_logger/"
            if not os.path.exists(path):
                os.makedirs(path)
            utils.experiment_graphpas_data_save(path,
                                                self.data.data_name + "_search_epoch_" + str(i+1) + ".txt",
                                                sharing_population,
                                                sharing_performance,
                                                self.search_space.space_getter(),
                                                self.search_space.stack_gcn_architecture)

        index = sharing_performance.index(max(sharing_performance))
        best_val_architecture = sharing_population[index]
        best_val_architecture = utils.gnn_architecture_embedding_decoder(best_val_architecture,
                                                                         self.search_space.space_getter(),
                                                                         self.search_space.stack_gcn_architecture)
        best_performance = max(sharing_performance)
        print("Best GNN Architecture:\n", best_val_architecture)
        print("Best VAL Performance:\n", best_performance)

        path = os.path.split(os.path.realpath(__file__))[0][:-34] + "logger/graphpas_logger/"
        if not os.path.exists(path):
            os.makedirs(path)
        utils.experiment_time_save(path,
                                   self.data.data_name + "_search_time.txt",
                                   epoch,
                                   time_search_list)

if __name__=="__main__":

    #ParallelConfig(True)
    ParallelConfig(False)

    graph = Planetoid("cora").data

    search_parameter = {"parallel_num": "2",
                        "mutation_num": "[1, 2]",
                        "initial_num": "5",
                        "sharing_num": "5",
                        "search_epoch": "1"}

    gnn_parameter = {"gnn_type": "stack_gcn",
                     "downstream_task_type": "node_classification",
                     "train_batch_size": "1",
                     "val_batch_size": "1",
                     "test_batch_size": "1",
                     "gnn_drop_out": "0.6",
                     "train_epoch": "10",
                     "early_stop": "False",
                     "early_stop_patience": "10",
                     "opt_type": "adam",
                     "opt_type_dict": "{\"learning_rate\": 0.005, \"l2_regularization_strength\": 0.0005}",
                     "loss_type": "nll_loss",
                     "val_evaluator_type": "accuracy",
                     "test_evaluator_type":  "[\"accuracy\", \"precision\", \"recall\", \"f1_value\"]"}

    search_space = SearchSpace(gnn_layers="2")

    graphpas_instance = Search(graph, search_parameter, gnn_parameter, search_space)
    graphpas_instance.search_operator()


# Method-1

def productExceptSelf(nums):
    res = [1] * len(nums)

    prefix = 1
    for i in range(len(nums)):
        res[i] = prefix
        prefix *= nums[i]
    postfix = 1
    for i in range(len(nums) -1, -1, -1):
        res[i] *= postfix
        postfix *= nums[i]
    return res

# Time Complexity : O(n)
# Space Complexity : O(1)

204.Product of Array Except Self/Method-1.py


from beds import Beds
from bedsrepository import BedsRepositry
from random import randint

class patientrepositry:
    PatientList = []
    messageForAddingPatient = " "
    message = ""
    messagev = ""

    def addPatient(patient):
        bedID = BedsRepositry.checkVacantBed()
        if bedID == -1:
            messageForAddingPatient = " No Vacant Bed "
        else:
            messageForAddingPatient = "Bed number " + bedID + " is now occupied by " + patient.name
            patient.bedid = bedID
            patientrepositry.PatientList.append(patient)
        return messageForAddingPatient

    def checkSPO(spo2):
        spo2value = int(spo2)
        if spo2value > 95:
            return True
        elif spo2value < 95:
            patientrepositry.messagev = patientrepositry.messagev + " Low SPO2 level"
            return False

    def checkheartbeat(heartbeat):
        heartbeatvalue = int(heartbeat)
        if heartbeatvalue > 100:
            patientrepositry.messagev = patientrepositry.messagev + " High heart beat "
            return False
        elif heartbeatvalue < 60:
            patientrepositry.messagev = patientrepositry.messagev + " Low heart beat "
            return False
        else:
            return True

    def checkBP(bloodpressure):
        bloodpressurevalue = int(bloodpressure)
        if bloodpressurevalue < 80:
            patientrepositry.messagev = patientrepositry.messagev + " Low blood pressure "
            return False
        elif bloodpressurevalue > 120:
            patientrepositry.messagev = patientrepositry.messagev + " High blood pressure "
            return False
        else:
            return True

    def patientstatus(patient):
        patient.heartbeat = str(randint(50, 110))
        patient.bp = str(randint(60, 130))
        patient.spo2 = str(randint(80, 100))

        if patientrepositry.checkheartbeat(patient.heartbeat) & patientrepositry.checkBP(patient.bp) & patientrepositry.checkSPO(patient.spo2):
            patientrepositry.message = patientrepositry.message + " Patient is OK on bed number " + patient.bedid
        else:
            patientrepositry.message = patientrepositry.message + " Check patient on bed number " + patient.bedid + " for following issues" + patientrepositry.messagev + '\n'

    def patientCheckVitals():
        patientrepositry.message = ""
        for patient in patientrepositry.PatientList:
            patientrepositry.messagev = ""
            if patient.bedid != "null":
                patientrepositry.patientstatus(patient)
            else:
                patientrepositry.message = patientrepositry.message + "Patient is not present in ICU"
        return patientrepositry.message

    def resetPatientVitals(id):
        messageReset = "BedId not found"
        for patient in patientrepositry.PatientList:
            if patient.bedid == id:
                patient.spo2 = "null"
                patient.bp = "null"
                patient.heartbeat = "null"
                messageReset = "Patient's Vitals Reset"
                break
        return messageReset

    def dischargePatient(id):
        messageDischargePatient = " "
        for patient in patientrepositry.PatientList:
            if patient.bedid == id:
                patient.bedid = "null"
                patient.spo2 = "null"
                patient.bp = "null"
                patient.heartbeat = "null"
                messageDischargePatient = "Patient " + patient.name + " is discharged"
                BedsRepositry.emptyBed(id)
                break
            else:
                messageDischargePatient = "Bed is not occupied by patient or Bed id does not exist"

        return messageDischargePatient


def update_ipn(letter, stack, ipn, priorities):
    while stack and stack[-1] in priorities:
        ipn.append(stack.pop())
    stack.append(letter)

def manage_stack(letter, ipn, stack, new):
    priorities = ['^', '|', '+', '!']
    if letter == '(':
        stack.append(letter)
    elif letter == ')':
        if not new:
            return False
        while stack and stack[-1] != '(':
            ipn.append(stack.pop())
        if not stack:
            return False
        del stack[-1]
    elif letter == '!':
        update_ipn(letter, stack, ipn, priorities[3:])
    elif letter == '+':
        update_ipn(letter, stack, ipn, priorities[2:])
    elif letter == '|':
        update_ipn(letter, stack, ipn, priorities[1:])
    elif letter == '^':
        update_ipn(letter, stack, ipn, priorities)
    return True

def erase_multiple_neg(elements):
    cpy = []
    for word in elements:
        for letter in word:
            if letter == '!' and len(cpy) > 1 and cpy[-1] == '!':
                cpy.pop()
            else:
                cpy.append(letter)
    return cpy

def create_ipn(elements):
    ipn = []
    stack = []
    new = False
    new_elems = erase_multiple_neg(elements)
    for idx_letter, letter in enumerate(new_elems):
            if 'A' <= letter <= 'Z':
                ipn.append(letter)
                new = True
            elif letter in ['(', ')', '+', '|', '^', '!']:
                if not manage_stack(letter, ipn, stack, new):
                    return 0, 0, None
                if letter != ')':
                    new = False
            else:
                while stack:
                    ret = stack.pop()
                    if ret == '(':
                        return 0, 0, None
                    ipn.append(ret)
                return letter, new_elems[idx_letter:], ipn
    while stack:
        ret = stack.pop()
        if ret == '(':
            return 0, 0, None
        ipn.append(ret)
    return None, None, ipn


import json as json_lib

from absl.testing import absltest
from tfx.types import channel_utils, standard_artifacts

from tfx_addons.model_card_generator import artifact
from tfx_addons.model_card_generator.component import ModelCardGenerator

class ComponentTest(absltest.TestCase):
  def test_component_construction(self):
    model_card_gen = ModelCardGenerator(
        statistics=channel_utils.as_channel(
            [standard_artifacts.ExampleStatistics()]),
        evaluation=channel_utils.as_channel(
            [standard_artifacts.ModelEvaluation()]),
        pushed_model=channel_utils.as_channel(
            [standard_artifacts.PushedModel()]),
        json=json_lib.dumps(
            {'model_details': {
                'name': 'my model',
                'version': {
                    'name': 'v1'
                }
            }}),
        template_io=[('path/to/html/template', 'mc.html'),
                     ('path/to/md/template', 'mc.md')],
    )

    with self.subTest('outputs'):
      self.assertEqual(model_card_gen.outputs['model_card'].type_name,
                       artifact.ModelCard.TYPE_NAME)

    with self.subTest('exec_properties'):
      self.assertDictEqual(
          {
              'json':
              json_lib.dumps({
                  'model_details': {
                      'name': 'my model',
                      'version': {
                          'name': 'v1'
                      }
                  }
              }),
              'template_io': [('path/to/html/template', 'mc.html'),
                              ('path/to/md/template', 'mc.md')],
              'features_include':
              None,
              'features_exclude':
              None,
              'metrics_include':
              None,
              'metrics_exclude':
              None,
          }, model_card_gen.exec_properties)

  def test_empty_component_construction(self):
    model_card_gen = ModelCardGenerator()
    with self.subTest('outputs'):
      self.assertEqual(model_card_gen.outputs['model_card'].type_name,
                       artifact.ModelCard.TYPE_NAME)

  def test_component_construction_with_filtered_features(self):
    model_card_gen = ModelCardGenerator(features_include=['feature_name1'])
    self.assertEqual(model_card_gen.exec_properties['features_include'],
                     ['feature_name1'])

    model_card_gen_features_exclude = ModelCardGenerator(
        features_exclude=['feature_name2'], )
    self.assertEqual(
        model_card_gen_features_exclude.exec_properties['features_exclude'],
        ['feature_name2'])

    with self.assertRaises(ValueError):
      ModelCardGenerator(
          features_include=['feature_name1'],
          features_exclude=['feature_name2'],
      )

  def test_component_construction_with_filtered_metrics(self):
    model_card_gen = ModelCardGenerator(metrics_include=['accuracy'])
    self.assertEqual(model_card_gen.exec_properties['metrics_include'],
                     ['accuracy'])

    model_card_gen_metrics_exclude = ModelCardGenerator(
        metrics_exclude=['loss'], )
    self.assertEqual(
        model_card_gen_metrics_exclude.exec_properties['metrics_exclude'],
        ['loss'])

    with self.assertRaises(ValueError):
      ModelCardGenerator(
          metrics_include=['accuracy'],
          metrics_exclude=['loss'],
      )

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


class transport:

    def __init__(self, transToken, inventory, inventoryMax, speed, rLoc, tLoc, status):
        self.transToken = transToken
        self.inventory = inventory
        self.inventoryMax = inventoryMax
        self.speed = speed
        self.rLoc = rLoc
        self.tLoc = tLoc
        self.status = status

    def isInNode(self, node):
        if self.rLoc == node.rLoc and self.tLoc == node.tLoc:
            return True
        else:
            return False


import os
import json
import torch
import numpy as np
from thirdparty.bop_toolkit.bop_toolkit_lib.inout import load_ply

def load_meshes(model_dir, obj_ids):
    meshes = {}
    for obj_id in obj_ids:
        fln = os.path.join(model_dir, f"obj_{obj_id:06d}.ply")
        print("Loading mesh file", fln, end='\r')
        meshes[obj_id] = load_ply(fln)
    print()
    return meshes

def load_mesh_db(model_dir):
    with open(os.path.join(model_dir, 'models_info.json'), 'r') as f:
        model_info = json.load(f)
    obj_ids = [int(obj_id) for obj_id in model_info.keys()]
    meshes = load_meshes(model_dir, obj_ids)
    mesh_db = {}
    for obj_id in obj_ids:
        info = model_info[str(obj_id)]
        is_sym_disc = len(info.get("symmetries_discrete",[])) > 0
        is_sym_cont = len(info.get("symmetries_continuous",[])) > 0
        cont_sym = []
        if is_sym_cont:
            cont_sym = info["symmetries_continuous"]
        points_np = np.array(meshes[obj_id]["pts"], dtype=np.float32)
        points = torch.tensor(points_np).to(torch.float32)
        if torch.cuda.is_available():
            points = points.cuda()
        mesh_db[obj_id] = {
                "is_symmetric": is_sym_disc or is_sym_cont,
                "continuous_sym": cont_sym,
                "diameter": info["diameter"],
                "points": points,
        }
        #print(f"Object {obj_id} is_symmetric: {is_sym_disc or is_sym_cont}")
        #for k, v in meshes[obj_id].items():
        #    mesh_db[k] = v
        #for k, v in model_info[str(obj_id)].items():
        #    mesh_db[k] = v
    return mesh_db

def load_meshes_DEBUG(model_dir, obj_ids, obj_map):
    meshes = {}
    for obj_id in obj_ids:
        fln = os.path.join(model_dir, obj_map[obj_id], "points.xyz")
        print("Loading mesh file", fln, end='\r')
        meshes[obj_id] = 1000 * np.loadtxt(fln, dtype=np.float32)
    print()
    return meshes

# For loading the original YCBV models for DEBUGGING ONLY
def load_mesh_db_DEBUG(model_dir, obj_map):
    obj_ids = [int(obj_id) for obj_id in obj_map.keys()]
    meshes = load_meshes_DEBUG(model_dir, obj_ids, obj_map)
    mesh_db = {}
    for obj_id in obj_ids:
        points_np = meshes[obj_id]
        points = torch.tensor(points_np).to(torch.float32)
        if torch.cuda.is_available():
            points = points.cuda()
        mesh_db[obj_id] = {
                "is_symmetric": False,
                "continuous_sym": False,
                "points": points,
                "points_np": points_np,
        }
        #print(f"Object {obj_id} is_symmetric: {is_sym_disc or is_sym_cont}")
        #for k, v in meshes[obj_id].items():
        #    mesh_db[k] = v
        #for k, v in model_info[str(obj_id)].items():
        #    mesh_db[k] = v
    return mesh_db


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import io
import os
import codecs
import shutil
import logging
from bigbuild.models import BasePage
from django.template import Engine, Context
from django.utils.encoding import python_2_unicode_compatible
from bigbuild.serializers import BigBuildFrontmatterSerializer
logger = logging.getLogger(__name__)

@python_2_unicode_compatible
class Page(BasePage):
    """
    A custom page published via static.latimes.com
    """
    def __str__(self):
        return self.slug

    def delete(self):
        """
        Delete the page directory.
        """
        shutil.rmtree(self.page_directory_path)

    @property
    def frontmatter_path(self):
        """
        Returns the metadata.md path where this page will be configured.
        """
        return os.path.join(self.page_directory_path, 'metadata.md')

    def create_directory(
        self,
        force=False,
        index_template_name='bigbuild/pages/default_index.html',
        index_template_context={},
    ):
        """
        Creates a new directory for the page.

        Returns the path to the directory that has been created, which is
        the same as the self.page_directory_path property.

        Throws an error if the directory already exists. You can force it
        to overwrite a pre-existing directory by submitting the force keyword
        argument as true.
        """
        # Check if the directory exists already and act accordingly
        if os.path.exists(self.page_directory_path):
            if force:
                shutil.rmtree(self.page_directory_path)
            else:
                raise ValueError(
                    "Page directory already exists at %s" % self.page_directory_path
                )

        # Make the directory
        os.mkdir(self.page_directory_path)

        # Add the index template
        self.write_index(
            template_name=index_template_name,
            template_context=index_template_context,
        )

        # Create the metadata configuration file
        self.write_frontmatter()

        # Create the ./static/ directory for extra stuff
        self.write_static()

        # Create the checklist file
        self.write_checklist()

        # Return the directory path
        return self.page_directory_path

    def write_index(self, template_name, template_context={}):
        """
        Creates index.html in the page directory.
        """
        template = self.get_template(template_name)
        html = template.render(Context(template_context))
        index_path = os.path.join(self.page_directory_path, 'index.html')
        with codecs.open(index_path, 'w', 'utf-8') as f:
            f.write(html)

    def write_frontmatter(self, path=None):
        """
        Creates metadata.yaml in the page directory, or a supplied target directory.
        """
        serializer = BigBuildFrontmatterSerializer()
        with io.open(path or self.frontmatter_path, 'w', encoding='utf8') as f:
            f.write(serializer.serialize([self]))

    def write_static(self):
        """
        Creates a ./static/ subdirectory within the page directory.
        """
        static_path = os.path.join(self.page_directory_path, 'static')
        os.mkdir(static_path)

        css_path = os.path.join(static_path, 'style.css')
        css = self.get_template("bigbuild/pages/style.css-tpl").source
        with codecs.open(css_path, 'w', 'utf-8') as f:
            f.write(css)

        js_path = os.path.join(static_path, 'app.js')
        js = self.get_template("bigbuild/pages/app.js-tpl").source
        with codecs.open(js_path, 'w', 'utf-8') as f:
            f.write(js)

    def write_checklist(self):
        """
        Creates checklist.md in the page directory.
        """
        template = self.get_template("bigbuild/pages/checklist.md-tpl")
        md = template.render(Context(dict(object=self)))
        checklist_path = os.path.join(self.page_directory_path, 'checklist.md')
        with codecs.open(checklist_path, 'w', 'utf-8') as f:
            f.write(md)

    def get_template(self, name):
        """
        Returns a Django template of the provided name ready to render.
        """
        engine = Engine.get_default()
        return engine.get_template(name)


import datetime
import hashlib
import hmac
import json
from base64 import b64decode, b64encode

import requests

class SentinelApiConnector:
    def __init__(self, *, workspace_id: str, log_analytics_uri: str, shared_key: str, log_type: str):
        self.workspace_id = workspace_id
        self.shared_key = shared_key
        self.key_encoder = KeyEncoder(shared_key)
        self.log_type = log_type
        self.uri = log_analytics_uri
        self.resource = "/api/logs"

    def post(self, json_body: list) -> None:
        body = json.dumps(json_body)
        headers = self._build_headers(len(body))
        requests.post(
            self.uri + self.resource, params={"api-version": "2016-04-01"}, data=body, headers=headers
        ).raise_for_status()

    def _build_headers(self, content_length: int) -> dict:
        date = _make_rfc1123date()
        signature = self._build_signature(date, content_length)
        return {
            "content-type": "application/json",
            "Authorization": signature,
            "Log-Type": self.log_type,
            "x-ms-date": date,
        }

    def _build_signature(self, date: str, content_length: int) -> str:
        string_to_hash = SIGNATURE_TEMPLATE.format(content_length=content_length, date=date, resource=self.resource)
        encoded_hash = self.key_encoder.encode(string_to_hash)
        return f"SharedKey {self.workspace_id}:{encoded_hash}"

def _make_rfc1123date() -> str:
    return datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")

SIGNATURE_TEMPLATE = """
POST
{content_length}
application/json
x-ms-date:{date}
{resource}
""".strip()

class KeyEncoder:
    def __init__(self, key: str):
        self.key = b64decode(key)

    def encode(self, text: str) -> str:
        return b64encode(hmac.new(self.key, bytes(text, encoding="utf-8"), digestmod=hashlib.sha256).digest()).decode()


from sequence.kernel.entity import Entity
from sequence.kernel.event import Event
from sequence.kernel.process import Process
from sequence.kernel.timeline import Timeline

class Dummy(Entity):
    def __init__(self, name, tl):
        Entity.__init__(self, name, tl)
        self.flag = False
        self.counter = 0
        self.click_time = None

    def init(self):
        self.flag = True

    def op(self):
        self.counter += 1

    def click(self):
        self.click_time = self.timeline.now()

def test_init():
    tl = Timeline()
    dummys = [Dummy(str(i), tl) for i in range(100)]
    for d in dummys:
        assert d.flag is False

    tl.init()
    for d in dummys:
        assert d.flag is True

def test_run():
    from numpy import random
    random.seed(0)
    tl = Timeline()
    dummy = Dummy("dummy", tl)
    times = random.randint(0, 20, 200)
    priorities = random.randint(0, 20, 200)

    for t, p in zip(times, priorities):
        process = Process(dummy, "op", [])
        e = Event(t, process, p)
        tl.schedule(e)

    tl.init()
    tl.run()

    assert dummy.counter == 200

    tl = Timeline(5)
    for t, p in zip(times, priorities):
        process = Process(dummy, "op", [])
        e = Event(t, process, p)
        tl.schedule(e)

    tl.init()
    tl.run()

    assert tl.now() == tl.time < 5 and len(tl.events) > 0

def test_remove_event():
    tl = Timeline()
    dummy = Dummy('1', tl)
    event = Event(1, Process(dummy, 'op', []))
    d2 = Dummy('2', tl)
    event2 = Event(1, Process(d2, 'op', []))
    tl.schedule(event)
    tl.schedule(event2)
    tl.init()
    assert dummy.counter == 0 and d2.counter == 0
    tl.remove_event(event)
    tl.run()
    assert dummy.counter == 0 and d2.counter == 1

def test_update_event_time():
    tl = Timeline()
    d1 = Dummy('1', tl)
    event1 = Event(10, Process(d1, 'click', []))
    d2 = Dummy('2', tl)
    event2 = Event(10, Process(d2, 'click', []))

    tl.schedule(event1)
    tl.schedule(event2)
    tl.init()
    assert d1.click_time is None and d2.click_time is None
    tl.update_event_time(event2, 20)
    tl.run()

    assert d1.click_time == 10 and d2.click_time == 20


from route import in_goal_sector, cPoint, get_shortest_path, distance
from obj_factories import TurnpointFactory, TaskFactory
import math
from pilot.track import GNSSFix
from geo import Geo
import factory_objects

turnpoints = [
    TurnpointFactory(lat=41.3435, lon=21.2568, radius=400, how='entry', shape='circle', type='launch'),
    TurnpointFactory(lat=41.5778, lon=21.333, radius=22000, how='entry', shape='circle', type='speed'),
    TurnpointFactory(lat=41.5778, lon=21.333, radius=4000),
    TurnpointFactory(lat=41.2448, lon=21.5773, radius=3000),
    TurnpointFactory(lat=41.348, lon=21.3042, radius=3000, how='entry', shape='circle', type='endspeed'),
    TurnpointFactory(lat=41.348, lon=21.3042, radius=100, how='entry', shape='line', type='goal')
]

projected_line = [cPoint(x=-3906.90986083417, y=-6423.21345696099),
                  cPoint(x=-3996.3205467033026, y=-6602.114951142762),
                  cPoint(x=-3947.1426667070477, y=-6514.8994708094115),
                  cPoint(x=-4045.390364525586, y=-6465.428373867)]

test_task = factory_objects.test_task()
test_task.turnpoints = turnpoints
test_task.geo = Geo.from_coords(41.40665, 21.351416666666665)
test_task.projected_line = projected_line

short = GNSSFix(rawtime=1, lat=41.348016666666666, lon=21.304666666666666,
                validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)
after_and_out = GNSSFix(rawtime=1, lat=41.34809381046268, lon=21.300493968601263,
                        validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)
inside = GNSSFix(rawtime=1, lat=41.34809381046268, lon=21.303493968601263,
                 validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)
line = GNSSFix(rawtime=1, lat=41.348, lon=21.3042,
               validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)
meter_short_of_tolerance = GNSSFix(rawtime=1, lat=41.348, lon=21.30428,
                                   validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)
short_but_tolerance = GNSSFix(rawtime=1, lat=41.348, lon=21.30425,
                              validity=True, press_alt=1, gnss_alt=1, index=1, extras=None)

goal_tp = TurnpointFactory(lat=41.348, lon=21.3042, radius=100)
previous_tp = TurnpointFactory(lat=41.2448, lon=21.5773)

def test_route_distance(task=test_task):
    task.calculate_task_length()
    assert math.isclose(task.distance, 94624.2, abs_tol=1)

def test_opt_route(task=test_task):
    """
    Before SS optimised:
    opt_dist_to_SS = 4799.913132554999
    SS_distance = 73709.60228966962
    opt_dist_to_ESS = 78509.51542222462
    opt_dist_ESS_goal = 2995.165098084239
    opt_dist = 81504.68052030886
    partial_distances = [0, 4799.913132554999, 23075.3094884808, 58794.22702507772, 78509.51542222462, 81504.68052030886]

    After SS optimised:
    Formula: pwc 2023
    SS_distance = 73131.30677591835
    Formula: gap 2022
    SS_distance = 73708.14384420638
    """
    task.calculate_optimised_task_length()
    assert math.isclose(task.opt_dist, 81504.6, abs_tol=1)
    assert math.isclose(task.opt_dist_to_SS, 4799.9, abs_tol=1)
    assert math.isclose(task.opt_dist_to_ESS, 78509.5, abs_tol=1)

    partial_distances = [0, 4799.91, 23075.3, 58794.2, 78509.5, 81504.7]
    for idx, d in enumerate(task.partial_distance):
        assert math.isclose(d, partial_distances[idx], abs_tol=1)

    # SS Distance Optimization:
    # PWCA
    assert math.isclose(task.SS_distance, 73131.3, abs_tol=1)
    # CIVL
    task.formula.formula_name = "GAP2022"
    task.formula.ss_dist_calc = "launch_to_ess"
    task.calculate_optimised_task_length()
    assert math.isclose(task.SS_distance, 73708.1, abs_tol=1)

def test_check_in_radius():
    # check in radius
    assert goal_tp.in_radius(short, 0, 0) is True
    assert goal_tp.in_radius(inside, 0, 0) is True
    assert goal_tp.in_radius(after_and_out, 0, 0) is False
    assert goal_tp.in_radius(line, 0, 0) is True

def test_in_goal_sector():
    assert in_goal_sector(test_task, short) is False
    assert in_goal_sector(test_task, after_and_out) is False
    assert in_goal_sector(test_task, inside) is True
    assert in_goal_sector(test_task, line) is True
    assert in_goal_sector(test_task, meter_short_of_tolerance) is False
    assert in_goal_sector(test_task, short_but_tolerance) is True