from .basehandler import BaseHandler

from ppmessage.api.error import API_ERR
from ppmessage.db.models import AppInfo
from ppmessage.db.models import AppUserData
from ppmessage.core.redis import redis_hash_to_dict

from ppmessage.core.constant import API_LEVEL

import json

class PPRemoveAppHandler(BaseHandler):

    def _remove(self):
        _redis = self.application.redis

        _app = redis_hash_to_dict(_redis, AppInfo, _app_uuid)
        if _app == None:
            self.setErrorCode(API_ERR.NO_APP)
            return

        if _app.get("user_uuid") != _user_uuid:
            self.setErrorCode(API_ERR.NOT_OWNER)
            return

        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + ".is_service_user.True"
        _services = _redis.smembers()

        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + ".is_service_user.False"
        _users = _redis.smembers()

        _pi = _redis.pipeline()
        _data_key_pre = AppUserData.__tablename__ + ".app_uuid." + _app_uuid
        for _user_uuid in _sevices:
            _data_key = _data_key_pre + ".user_uuid." + _user_uuid + ".is_service_user.True"
            _pi.get(_data_key)
        _data_uuid_list = _pi.execute()

        for _i in _data_uuid_list:
            _row = AppUserData(uuid=_i)
            _row.async_delete()
            _row.delete_redis_keys(_redis)

        _row = AppInfo(uuid=_app_uuid)
        _row.async_delete()
        _row.delete_redis_keys(_redis)
        return

    def initialize(self):
        self.addPermission(app_uuid=True)
        self.addPermission(api_level=API_LEVEL.PPCONSOLE)
        self.addPermission(api_level=API_LEVEL.THIRD_PARTY_CONSOLE)
        return

    def _Task(self):
        super(PPRemoveAppHandler, self)._Task()
        _request = json.loads(self.request.body)
        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        if _user_uuid == None or _app_uuid == None:
            self.setErrorCode(API_ERR.NO_PARA)
            return
        self._remove(_app_uuid, _user_uuid)
        return


import streamlit as st
import pandas as pd
import requests
import cv2

captions_df = pd.read_csv(".\data\captions.csv")
captions_df = captions_df.drop_duplicates(subset=['image'], keep='first').reset_index(drop=True)
filenames = captions_df['image'].tolist()
PATH_IMAGES = ".\data\Images\\"

def search_images():
    matches = requests.get(
        url="http://localhost:3020/query_to_image/",
        params={
            "query": query
        }
    ).json()
    matched_indices = matches['matches']
    scores = matches['scores']
    for score, inx in zip(scores, matched_indices):
        image = cv2.imread(PATH_IMAGES+filenames[inx])
        st.image(image, channels="BGR")
        st.caption(str(score))

st.markdown("# Search Images based on the query")
st.sidebar.markdown("# Search Images")
query = st.text_input("Describe an image you are looking for!", value="", max_chars=256)
if st.button("Search images"):
    matches = requests.get(
        url="http://localhost:3020/query_to_image/",
        params={
            "query": query
        }
    ).json()
    matched_indices = matches['matches']
    scores = matches['scores']
    for score, inx in zip(scores, matched_indices):
        image = cv2.imread(PATH_IMAGES+filenames[inx])
        st.image(image, channels="BGR")
        st.caption(str(score))


# Provides terminal-based chat interface for RWKV model.
# Usage: python chat_with_bot.py C:\rwkv.cpp-169M.bin
# Prompts and code adapted from https://github.com/BlinkDL/ChatRWKV/blob/9ca4cdba90efaee25cfec21a0bae72cbd48d8acd/chat.py

import os
import argparse
import pathlib
import copy
import torch
import sampling
import rwkv_cpp_model
import rwkv_cpp_shared_library
from rwkv_tokenizer import get_tokenizer
import json
from typing import List, Dict, Optional
import time

# ======================================== Script settings ========================================

# English, Chinese, Japanese
LANGUAGE: str = 'English'
# QA: Question and Answer prompt to talk to an AI assistant.
# Chat: chat prompt (need a large model for adequate quality, 7B+).
PROMPT_TYPE: str = 'QA'

MAX_GENERATION_LENGTH: int = 250

# Sampling temperature. It could be a good idea to increase temperature when top_p is low.
TEMPERATURE: float = 0.8
# For better Q&A accuracy and less diversity, reduce top_p (to 0.5, 0.2, 0.1 etc.)
TOP_P: float = 0.5
# Penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
PRESENCE_PENALTY: float = 0.2
# Penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
FREQUENCY_PENALTY: float = 0.2

END_OF_LINE_TOKEN: int = 187
DOUBLE_END_OF_LINE_TOKEN: int = 535
END_OF_TEXT_TOKEN: int = 0

# =================================================================================================

parser = argparse.ArgumentParser(description='Provide terminal-based chat interface for RWKV model')
parser.add_argument('model_path', help='Path to RWKV model in ggml format')
parser.add_argument('tokenizer', help='Tokenizer to use; supported tokenizers: 20B, world', nargs='?', type=str, default='20B')
args = parser.parse_args()

script_dir: pathlib.Path = pathlib.Path(os.path.abspath(__file__)).parent

with open(script_dir / 'prompt' / f'{LANGUAGE}-{PROMPT_TYPE}.json', 'r', encoding='utf8') as json_file:
    prompt_data = json.load(json_file)

    user, bot, separator, init_prompt = prompt_data['user'], prompt_data['bot'], prompt_data['separator'], prompt_data['prompt']

assert init_prompt != '', 'Prompt must not be empty'

tokenizer_decode, tokenizer_encode = get_tokenizer(args.tokenizer)

library = rwkv_cpp_shared_library.load_rwkv_shared_library()
print(f'System info: {library.rwkv_get_system_info_string()}')

print('Loading RWKV model')
model = rwkv_cpp_model.RWKVModel(library, args.model_path)

# =================================================================================================

processed_tokens: List[int] = []
logits: Optional[torch.Tensor] = None
state: Optional[torch.Tensor] = None

def process_tokens(_tokens: List[int], new_line_logit_bias: float = 0.0) -> None:
    global processed_tokens, logits, state

    processed_tokens += _tokens

    for _token in _tokens:
        logits, state = model.eval(_token, state, state, logits)

    logits[END_OF_LINE_TOKEN] += new_line_logit_bias

state_by_thread: Dict[str, Dict] = {}

def save_thread_state(_thread: str) -> None:
    state_by_thread[_thread] = {
        'tokens': copy.deepcopy(processed_tokens),
        'logits': copy.deepcopy(logits),
        'state': copy.deepcopy(state)
    }

def load_thread_state(_thread: str) -> None:
    global processed_tokens, logits, state

    thread_state = state_by_thread[_thread]

    processed_tokens = copy.deepcopy(thread_state['tokens'])
    logits = copy.deepcopy(thread_state['logits'])
    state = copy.deepcopy(thread_state['state'])

# Model only saw '\n\n' as [187, 187] before, but the tokenizer outputs [535] for it at the end.
# See https://github.com/BlinkDL/ChatRWKV/pull/110/files
def split_last_end_of_line(tokens):
    if len(tokens) > 0 and tokens[-1] == DOUBLE_END_OF_LINE_TOKEN:
        tokens = tokens[:-1] + [END_OF_LINE_TOKEN, END_OF_LINE_TOKEN]

    return tokens

# =================================================================================================

processing_start = time.time()

prompt_tokens = tokenizer_encode(init_prompt)
prompt_token_count = len(prompt_tokens)
print(f'Processing {prompt_token_count} prompt tokens, may take a while')

process_tokens(split_last_end_of_line(prompt_tokens))

processing_duration = time.time() - processing_start

print(f'Processed in {int(processing_duration)} s, {int(processing_duration / prompt_token_count * 1000)} ms per token')

save_thread_state('chat_init')
save_thread_state('chat')

print(f'\nChat initialized! Your name is {user}. Write something and press Enter. Use \\n to add line breaks to your message.')

while True:
    # Read user input
    user_input = input(f'> {user}{separator} ')
    msg = user_input.replace('\\n', '\n').strip()

    temperature = TEMPERATURE
    top_p = TOP_P

    if '-temp=' in msg:
        temperature = float(msg.split('-temp=')[1].split(' ')[0])

        msg = msg.replace('-temp='+f'{temperature:g}', '')

        if temperature <= 0.2:
            temperature = 0.2

        if temperature >= 5:
            temperature = 5

    if '-top_p=' in msg:
        top_p = float(msg.split('-top_p=')[1].split(' ')[0])

        msg = msg.replace('-top_p='+f'{top_p:g}', '')

        if top_p <= 0:
            top_p = 0

    msg = msg.strip()

    # + reset --> reset chat
    if msg == '+reset':
        load_thread_state('chat_init')
        save_thread_state('chat')
        print(f'{bot}{separator} Chat reset.\n')
        continue
    elif msg[:5].lower() == '+gen ' or msg[:3].lower() == '+i ' or msg[:4].lower() == '+qa ' or msg[:4].lower() == '+qq ' or msg.lower() == '+++' or msg.lower() == '++':

        # +gen YOUR PROMPT --> free single-round generation with any prompt. Requires Novel model.
        if msg[:5].lower() == '+gen ':
            new = '\n' + msg[5:].strip()
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +i YOUR INSTRUCT --> free single-round generation with any instruct. Requires Raven model.
        elif msg[:3].lower() == '+i ':
            new = f'''
Below is an instruction that describes a task. Write a response that appropriately completes the request.

# Instruction:
{msg[3:].strip()}

# Response:
'''
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +qq YOUR QUESTION --> answer an independent question with more creativity (regardless of context).
        elif msg[:4].lower() == '+qq ':
            new = '\nQ: ' + msg[4:].strip() + '\nA:'
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +qa YOUR QUESTION --> answer an independent question (regardless of context).
        elif msg[:4].lower() == '+qa ':
            load_thread_state('chat_init')

            real_msg = msg[4:].strip()
            new = f'{user}{separator} {real_msg}\n\n{bot}{separator}'

            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +++ --> continue last free generation (only for +gen / +i)
        elif msg.lower() == '+++':
            try:
                load_thread_state('gen_1')
                save_thread_state('gen_0')
            except Exception as e:
                print(e)
                continue

        # ++ --> retry last free generation (only for +gen / +i)
        elif msg.lower() == '++':
            try:
                load_thread_state('gen_0')
            except Exception as e:
                print(e)
                continue
        thread = 'gen_1'

    else:
        # + --> alternate chat reply
        if msg.lower() == '+':
            try:
                load_thread_state('chat_pre')
            except Exception as e:
                print(e)
                continue
        # chat with bot
        else:
            load_thread_state('chat')
            new = f'{user}{separator} {msg}\n\n{bot}{separator}'
            process_tokens(tokenizer_encode(new), new_line_logit_bias=-999999999)
            save_thread_state('chat_pre')

        thread = 'chat'

        # Print bot response
        print(f'> {bot}{separator}', end='')

    start_index: int = len(processed_tokens)
    accumulated_tokens: List[int] = []
    token_counts: Dict[int, int] = {}

    for i in range(MAX_GENERATION_LENGTH):
        for n in token_counts:
            logits[n] -= PRESENCE_PENALTY + token_counts[n] * FREQUENCY_PENALTY

        token: int = sampling.sample_logits(logits, temperature, top_p)

        if token == END_OF_TEXT_TOKEN:
            print()
            break

        if token not in token_counts:
            token_counts[token] = 1
        else:
            token_counts[token] += 1

        process_tokens([token])

        # Avoid UTF-8 display issues
        accumulated_tokens += [token]

        decoded: str = tokenizer_decode(accumulated_tokens)

        if '\uFFFD' not in decoded:
            print(decoded, end='', flush=True)

            accumulated_tokens = []

        if thread == 'chat':
            if '\n\n' in tokenizer_decode(processed_tokens[start_index:]):
                break

        if i == MAX_GENERATION_LENGTH - 1:
            print()

    save_thread_state(thread)


#!/usr/bin/env python

''' Script that creates input files (AssayInfo & LocusInfo)
    required by GTSeq pipeline

    input:  sequence file (fastq)
            vcf file (vcf)
            primer file (csv):
              Contains the contig #, SNP position, and
              primer seq (FWD primer only for paired-end
              seqs), with the format:
                    Contig#-POS,FWD-primer
              where Contig#-POS is the SNP name

    output: AssayInfo.txt - first input file
            LocusInfo.txt - second input file
            missing_snps.txt - file with snps that were
                               not added to prior 2 output files

    NOTE: make sure snp/contig names match between files

    usage: ./gtseq-make_infiles.py contigs.fa snp-info.vcf primers.csv

    CYP 09/26/2017
'''
import re
import sys
from Bio import SeqIO
from Bio.Seq import Seq

if len(sys.argv) != 4:
  print("ERROR: Please specify a contigs (fasta), snp (vcf), and " + \
          "primer file (csv).")
  sys.exit()

# global variables
fasta_file = open(sys.argv[1], 'r')
vcf_file = sys.argv[2]
primer_file = sys.argv[3]
vcf_dict = {}
missing_list = []
ref_allele, alt_allele = '', ''
ref_probe, alt_probe = '', ''

# change these variables as needed
FLANK = 3 # num of bps that should flank snp in probe
DEBUG = False # specify True to display DEBUG msgs
REMOVE_ADAPTER = False # specify True to remove adapter sequence
adapter = 'CGACAGGTTCAGAGTTCTACAGTCCGACGATC' # adapter to remove

# Process FASTA file
# create dictionary of contigs from fasta file
fasta_dict = SeqIO.to_dict(SeqIO.parse(fasta_file, "fasta"))
fasta_file.close()

# Process VCF file
# create dictionary of all snps and their ref and
# alt alleles found in VCF file
with open(vcf_file, 'r') as vcf:
  for line in vcf:
    if not line.startswith('#'):
      items = line.split('\t')
      # the full snp name (as it appears in
      # primer file) is the dict key
      name = items[0]+'-'+items[1]
      # items[3] = ref allele
      # items[4] = alt allele
      vcf_dict[name] = [items[3],items[4]]

# file with contents of vcf_dict
if DEBUG:
  with open('vcf_dict.txt', 'w') as out:
    for key in vcf_dict:
      out.write(key+': ')
      for value in vcf_dict[key]:
        out.write(value+' ')
      out.write('\n')

assayinfo = open("AssayInfo.txt", 'w')
locusinfo = open("LocusInfo.csv", 'w')
header = True
with open(primer_file) as prim:
  for line in prim:
    # skip header
    if header:
      header = False
      continue
    # collect the contig#, SNP position, and primer seq
    # from the primer file (format: Contig#-POS,primer)
    snp_name = line.split(',')[0]
    name_pos = snp_name.split('-')
    contig_name = name_pos[0]
    snp_pos = int(name_pos[1])
    if DEBUG: print(snp_pos)
    primer = line.split(',')[1]
    if ADD_REV_PRIMER: rev_primer = line.split(',')[2] #for including rev in assayinfo

    # if enabled, shave off adapter from each primer
    if REMOVE_ADAPTER:
      primer = primer.replace(adapter,'')
      if ADD_REV_PRIMER: rev_primer = rev_primer.replace(adapter,'')
    if ADD_REV_PRIMER: rev_primer_rc = Seq(rev_primer).reverse_complement()

    # set start and end positions for probe sequence
    probe_start = snp_pos-FLANK-1
    probe_end = snp_pos+FLANK-1

    if contig_name in fasta_dict:
        # collect probe sequence from fasta contigs dict
        fasta = fasta_dict[contig_name].seq
        probeseq = fasta[probe_start:probe_end+1]

    if not snp_name in vcf_dict:
      # if snp name cannot be found in vcf snp dictionary,
      # notify user, add to list of missing snps
      print('%s could not be found in vcf file' % snp_name)
      missing_list.append(snp_name)
    else:
      # otherwise find snp name in vcf dictionary to
      # collect reference and alternate alleles
      ref_allele = vcf_dict[snp_name][0]
      alt_allele = vcf_dict[snp_name][1]

      # create ref,alt probe sequences using the ref,alt alleles
      # and the probeseq fragment collect from the fasta file
      ref_probe = str(probeseq[:FLANK]+ref_allele+probeseq[FLANK+1:])
      alt_probe = str(probeseq[:FLANK]+alt_allele+probeseq[FLANK+1:])

      # snp_dict[snp_name] = [primer,ref_allele,alt_allele,ref_probe,alt_probe]
      # write all relevant values in specific format to each outfile
      if ADD_REV_PRIMER:
        assayinfo.write(snp_name+'\t'+primer+'\t'+ref_probe+'\t'+alt_probe+'\t'+str(rev_primer_rc)+'\n')
      else:
        assayinfo.write(snp_name+'\t'+primer+'\t'+ref_probe+'\t'+alt_probe+'\n')
      locusinfo.write(snp_name+','+ref_allele+','+alt_allele+','+ref_probe+','+alt_probe+','+primer+'\n')

# write all snps that weren't added to out files
# to a file called missing_snps.txt
with open('missing_snps.txt', 'w') as out:
  for snp in missing_list:
    out.write(snp+'\n')

assayinfo.close()
locusinfo.close()


#!/usr/bin/python
import argparse
import numpy as np
import sebastians_matplotlib_addons as splt
import os
import plenopy

parser = argparse.ArgumentParser(
    prog="plenopy/trigger/scripts/plot.py",
    description=("Plot the trigger's geometry of the plenoscope"),
)

parser.add_argument(
    "trigger_geometry_path",
    metavar="PATH",
    type=str,
    help="directory containing the trigger's geometry.",
)

args = parser.parse_args()

trg_geom_dir = os.path.abspath(args.trigger_geometry_path)
plot_dir = os.path.join(trg_geom_dir, "plot")

os.makedirs(plot_dir, exist_ok=True)

MATPLOTLIB_RCPARAMS = {
    "mathtext.fontset": "cm",
    "font.family": "STIXGeneral",
}
FIGURE_STYLE = {"rows": 720, "cols": 1280, "fontsize": 1.0}
AX_SPAN = [0.2, 0.2, 0.75, 0.75]
splt.matplotlib.rcParams.update(MATPLOTLIB_RCPARAMS)

trigger_geometry = plenopy.trigger.geometry.read(trg_geom_dir)
trigger_summation_statistics = plenopy.trigger.geometry.init_summation_statistics(
    trigger_geometry=trigger_geometry
)

stats = trigger_summation_statistics

cxy_lim = [-3.5, 3.5]

for focus in range(trigger_geometry["number_foci"]):

    fig = splt.figure(FIGURE_STYLE)
    ax = splt.add_axes(fig=fig, span=AX_SPAN)
    plenopy.plot.image.add2ax(
        ax=ax,
        I=np.array(stats["foci"][focus]["number_lixel_in_pixel"]),
        px=np.rad2deg(trigger_geometry["image"]["pixel_cx_rad"]),
        py=np.rad2deg(trigger_geometry["image"]["pixel_cy_rad"]),
        colormap="viridis",
        hexrotation=30,
        vmin=None,
        vmax=None,
        colorbar=True,
    )
    ax.set_xlim(cxy_lim)
    ax.set_ylim(cxy_lim)
    ax.set_aspect("equal")
    fig.suptitle("number lixel in pixel")
    ax.set_xlabel(r"$c_x\,/\,1^{\circ}$")
    ax.set_ylabel(r"$c_y\,/\,1^{\circ}$")
    fig.savefig(
        os.path.join(
            plot_dir, "focus_{:06d}_lixel_in_pixel_overview.jpg".format(focus),
        )
    )
    splt.close(fig)

    fig = splt.figure(FIGURE_STYLE)
    ax = splt.add_axes(fig=fig, span=AX_SPAN)
    num_lip = stats["foci"][focus]["number_lixel_in_pixel"]
    bin_edges = np.arange(np.min(num_lip), np.max(num_lip) + 1)
    splt.ax_add_histogram(
        ax=ax,
        bincounts=np.histogram(num_lip, bins=bin_edges)[0],
        bin_edges=bin_edges,
        linestyle="-",
        linecolor="k",
    )
    ax.set_xlabel("number lixel in pixel")
    ax.set_ylabel("number pixel")
    ax.semilogy()
    ax.grid(color="k", linestyle="-", linewidth=0.66, alpha=0.1)
    fig.savefig(
        os.path.join(
            plot_dir,
            "focus_{:06d}_lixel_in_pixel_histogram.jpg".format(focus),
        )
    )
    splt.close(fig)

    fig = splt.figure(FIGURE_STYLE)
    ax = splt.add_axes(fig=fig, span=AX_SPAN)
    num_pil = stats["foci"][focus]["number_pixel_in_lixel"]
    bin_edges = np.arange(np.min(num_pil), np.max(num_pil) + 1)
    splt.ax_add_histogram(
        ax=ax,
        bincounts=np.histogram(num_pil, bins=bin_edges)[0],
        bin_edges=bin_edges,
        linestyle="-",
        linecolor="k",
    )
    ax.set_xlabel("number pixel in lixel")
    ax.set_ylabel("number lixel")
    ax.semilogy()
    ax.grid(color="k", linestyle="-", linewidth=0.66, alpha=0.1)
    fig.savefig(
        os.path.join(
            plot_dir,
            "focus_{:06d}_pixel_in_lixel_histogram.jpg".format(focus),
        )
    )
    splt.close(fig)


from __future__ import print_function

import logging

import click
import tqdm

from .. import core
from .. import fs_to_csv
from .. import tools

from . import shared

log = logging.getLogger(__name__)

@shared.git_log_args
@click.option('--output', '-o', type=click.File(mode='w'), default='-',
              help="Output destination (default is stdout)")
@click.option('--force', '-f', is_flag=True, help="Force refresh of cache")
@click.option('--filter', '-F', 'filters', multiple=True,
              help=('Report only for changes to files matching this artifact. '
                    'Default is to show just stats changes for entire tree'))
@click.pass_context
def command(ctx, rev_range=None, path=None, **options):
    stats_fs = ctx.obj['fs_class'](ctx.obj['cache_dir'], init=options['force'])

    total, source_commits = tools.tee_count(
        core.generate_shas_for_source_range(ctx.obj['repo'], rev_range))

    fs_commits = tqdm.tqdm(
        core.generate_stats_fs_commits_for_source_commits(
            stats_fs, ctx.obj['repo'], source_commits,
            paths=[path] if path else None, **ctx.obj['cache_kwargs']),
        unit='commit', total=total, unit_scale=True)

    filters = options['filters'] or ['^SUBTREE_TOTAL']
    log.info("Generating csv for range '{}'.".format(rev_range) or 'all')

    fs_to_csv.fs_commits_to_csv(
        stats_fs, fs_commits, options['output'],
        test_patterns=ctx.obj['test_patterns'], filters=filters)


from PIL import Image
import sys
import os

pic_name = sys.argv[1]
json_name = pic_name.split('.')[0]

img_path = os.getcwd() + '/pic/' + pic_name

output_file = open(os.getcwd() + '/pic-json/' + json_name + '.json',"w")

img = Image.open(img_path)
img_rgb = img.convert('RGB')

tuple_value = (0,0,0)
hex_color = ""
r_hex = ""
g_hex = ""
b_hex = ""

output_file.write("[")
for i in range(img_rgb.size[0]):
    for j in range(img_rgb.size[1]):
        tuple_value = img_rgb.getpixel((i,j))
        r_hex = hex(tuple_value[0])[2:]
        if len(r_hex) == 1 :
            r_hex = "0" + r_hex
        g_hex = hex(tuple_value[1])[2:]
        if len(g_hex) == 1 :
            g_hex = "0" + g_hex
        b_hex = hex(tuple_value[2])[2:]
        if len(b_hex) == 1 :
            b_hex = "0" + b_hex
        hex_color = r_hex + g_hex + b_hex
        if((i == img_rgb.size[0] - 1) and (j == img_rgb.size[1] - 1)):
            output_file.write('{"x":' + str(i) + ',"y":' + str(j) + ',"hex":"' + hex_color + '"}')
        else:
            output_file.write('{"x":' + str(i) + ',"y":' + str(j) + ',"hex":"' + hex_color + '"},')
output_file.write("]")

output_file.close()


from confluent_kafka import Producer
from confluent_kafka.serialization import StringSerializer, SerializationContext, MessageField
from confluent_kafka.schema_registry import SchemaRegistryClient
from confluent_kafka.schema_registry.avro import AvroSerializer
from Config import config, srConfig, rootDirectory
from Module.ClassStock import Stock, stockToDict
from Module.ClassCompany import Company, companyToDict
from datetime import datetime
import os, json, random, time

def deliveryReport(err, event):
    if err is not None:
        print(f"Error ID: {event.key().decode('utf8')}: {err}")
    else:
        print(f"Stored ID: {event.key().decode('utf8')} to Topic: {event.topic()}")

def sendProducer(topic, object, producer, avroSerializer):
    producer.produce(topic=topic,
                     key=StringSerializer('utf_8')(str(object.id)),
                     value=avroSerializer(object, SerializationContext(topic, MessageField.VALUE)),
                     on_delivery=deliveryReport)

if __name__ == '__main__':
    topic1 = 'streaming.goapi.idx.stock.json'
    topic2 = 'streaming.goapi.idx.companies.json'
    schema1 = 'IDX-Stock'
    schema2 = 'IDX-Company'
    jsonFile = 'kafka.ksql-join-stock-company 2023-07-28.json'

    string_serializer = StringSerializer('utf_8')
    schema_registry_client = SchemaRegistryClient(srConfig)

    stockSchema = schema_registry_client.get_latest_version('IDX-Stock')
    stockAvroSerializer = AvroSerializer(schema_registry_client,
                                         stockSchema.schema.schema_str,
                                         stockToDict)
    companySchema = schema_registry_client.get_latest_version('IDX-Company')
    companyAvroSerializer = AvroSerializer(schema_registry_client,
                                           companySchema.schema.schema_str,
                                           companyToDict)
    # Read Dummy Input
    jsonPath = os.path.join(rootDirectory, 'Resource', jsonFile)

    # Opening JSON file
    file = open(jsonPath)

    # returns JSON object as a dictionary
    jsonData = json.load(file)

    producer = Producer(config)
    while True:
        try:
            for row in jsonData:
                close = random.uniform(row['low'], row['high'])  # For testing purpose
                stock = Stock(row['id'], row['ticker'], row['date'], row['open'], row['high'], row['low'], close,
                              row['volume'])
                company = Company(row['ticker'], row['ticker'], row['name'], row['logo'],
                                  str(datetime.today().strftime('%Y-%m-%d %H:%M:%S')))
                print(f"id:{row['id']} high:{row['high']} open:{row['open']} low:{row['low']} close:{close} "
                      f"name:{row['name']} logo:{row['logo']}")
                sendProducer(topic1, stock, producer, stockAvroSerializer)
                sendProducer(topic2, company, producer, companyAvroSerializer)
            time.sleep(5)
            # Save and flush
            producer.flush()
        except KeyboardInterrupt:
            print("Shutdown Starting...")
            break


import os
from typing import List
from matplotlib.axes import Axes
from matplotlib.ticker import FormatStrFormatter

figure_style = {
    # tight layout used for better visualisation
    'tight_layout': [0.05, 0.05, 0.95, 0.95],
    # xlimits used for the visualisation of non-expectation values
    'xlimits': (None, None),
    # ylimits used for the visualisation of non-expecation values
    'ylimits': (None, None),
    # format string for xticks
    'xtick_format': "%.0f",
    # format string for yticks
    'ytick_format': "%.1f",
    # scaling factor used in QuantDisplace2
    'scaling': 1
}

def scaling():
    if "FIGURE_SCALING" in os.environ:
        return float(os.environ["FIGURE_SCALING"])
    else:
        return 1

def set_scaling(scale: float):
    os.environ["FIGURE_SCALING"] = str(scale)

def set_axis_limits(axes: List[Axes], style: dict, direction: str):
    """
    Set new axes limits based on the 'xlimits' or 'ylimits' key given in
    the style dictionary for all axis elements passed in the
    collection of axis elements.

    Parameters
    ------------
        axes: List[matplotlib.Axes]
            Collection of matplotlib.Axes instances
        style: dict
            Style dictionary containing 'xlim' keyword
        direction: str
            Can either be 'x' or 'y'
    """
    assert(direction in ['x', 'y'], "Direction must be 'x' or 'y'")
    ax_min, ax_max = style['xlimits'] if direction == 'x' else style['ylimits']

    for ax in axes:
        if direction == 'x':
            ax.set_xlim(ax_min, ax_max)
        else:
            ax.set_ylim(ax_min, ax_max)

def axis_tick_formatting(axes: List[Axes], style: dict, direction: str):
    """
    Set the string formatting of the axis ticks.
    """
    assert(direction in ['x', 'y'], "Direction must be 'x' or 'y'")

    format, attr = (style['xtick_format'], 'xaxis') if direction == 'x' else (style['ytick_format'], 'yaxis')

    for ax in axes: getattr(ax, attr).set_major_formatter(FormatStrFormatter(format))

def apply_styles(axes: List[Axes], style: dict):
    style_keys = style.keys()

    if "xlimits" in style_keys:
        if not (None, None) ==  style["xlimits"]:
            set_axis_limits(axes, style, 'x')

    if "ylimits" in style_keys:
        if not (None, None) == style["ylimits"]:
            set_axis_limits(axes, style, 'y')

    if "xtick_format" in style_keys:
        if style["xtick_format"] is not None:
            axis_tick_formatting(axes, style, 'x')

    if "ytick_format" in style_keys:
        if style["ytick_format"] is not None:
            axis_tick_formatting(axes, style, 'y')

    if "scaling" in style_keys:
        if style["scaling"] is not None:
            set_scaling(style['scaling'])


import pygame
from projectil import projectile
import animation
# CREATION CLASSE JOUER
class player (animation.AnimateSprite):

    def __init__(self,game):
        super().__init__('player')
        self.game=game
        self.vie = 150
        self.max_vie = 150
        self.attack = 20
        self.vitesse = 5
        self.son = pygame.mixer.Sound("assets/sounds/tir.ogg")
        self.all_projectiles=pygame.sprite.Group()

        self.rect = self.image.get_rect()
        self.rect.x= 500
        self.rect.y = 600
    def launch_projectile(self):
        self.start_animation()
        self.son.play()

        self.all_projectiles.add(projectile(self))

    def deplacement_droite(self):

        if not self.game.check_collision(self,self.game.all_monstres):
            self.rect.x += self.vitesse
    def deplacement_gauche(self):
        self.rect.x -= self.vitesse

    def char_bar_vie(self, surface):

        pygame.draw.rect(surface, (63, 60, 60), [self.rect.x + 20, self.rect.y + 10, self.max_vie, 7])
        pygame.draw.rect(surface, (111, 210, 45),[self.rect.x + 20, self.rect.y +10, self.vie, 7])
    def domage(self,amount):

        if self.vie - amount > amount:
            self.vie -= amount
        else:
            self.game.game_over()
    def update_animation(self):
        self.animat()


from datetime import datetime
from typing import Any, Annotated, Optional

import structlog
from fastapi import Depends

from src.commons import set_context_value, get_ctx
from .internal.odoo_manager import OdooManager, get_odoo_provider
from .internal.odoo_repo import OdooRepo, get_odoo_repo, RedisKeys
from .internal.ordercast_manager import OrdercastManager, get_ordercast_manager
from .internal.utils import (
    has_objects,
    set_ordercast_id,
    set_user_ordercast_id,
    validate_partners,
)
from .internal.utils.builders import (
    get_partner_data,
    get_attribute_data,
    get_product_data,
    get_product_variant_data,
    get_delivery_option_data,
    get_pickup_location_data,
)
from .internal.utils.helpers import set_attribute_value_ordercast_id
from .internal.validators import (
    validate_products,
    validate_categories,
    validate_product_variants,
    validate_attributes,
    validate_delivery_options,
    validate_pickup_locations,
)
from .internal.webhook_handler import WebhookHandler

logger = structlog.getLogger(__name__)

class OdooSyncManager:
    def __init__(
        self,
        repo: OdooRepo,
        odoo_manager: OdooManager,
        ordercast_manager: OrdercastManager,
        webhook_handler: WebhookHandler,
    ) -> None:
        self.repo = repo
        self.odoo_manager = odoo_manager
        self.ordercast_manager = ordercast_manager
        self.webhook_handler = webhook_handler

    def handle_webhook(self, topic: str, **kwargs: dict[str, Any]) -> None:
        self.webhook_handler.handle(topic=topic, **kwargs)

    def sync(self) -> None:
        logger.info("Load common data")
        self.load_commons()

        logger.info("Start full syncing with Odoo.")
        self.sync_users()

        logger.info("Start receiving products from Odoo")
        self.sync_products(full_sync=True)

        logger.info(
            "Start receiving order data from Odoo => "
            "delivery methods & pickup locations"
        )
        self.sync_delivery_methods_and_pickup_locations()

        logger.info("Start sync orders with Odoo")
        self.sync_orders_with_odoo(
            from_date=self.repo.get_key(RedisKeys.LAST_SUCCESSFUL_ORDERCAST_SYNC_DATE)
        )
        logger.info("Start syncing orders with Ordercast")
        self.sync_orders_with_ordercast(
            from_date=self.repo.get_key(RedisKeys.LAST_SUCCESSFUL_ODOO_SYNC_DATE)
        )
        # self.check_deletion()?

    def sync_users(self) -> None:
        logger.info("Started syncing user's data with Ordercast")
        self.sync_users_from_odoo_to_ordercast()
        self.sync_users_from_ordercast_to_odoo()

    def sync_users_from_odoo_to_ordercast(self) -> None:
        existing_odoo_users = self.repo.get_list(key=RedisKeys.USERS)
        partners = self.odoo_manager.receive_partner_users(
            exclude_user_ids=[p.odoo_id for p in existing_odoo_users]  # type: ignore
        )
        validate_partners(
            partners=partners, ordercast_users=self.ordercast_manager.get_users()
        )

        logger.info(f"Received partners => {len(partners)}, started saving them.")

        users_to_sync = [
            get_partner_data(partner) for partner in partners if partner["email"]
        ]
        self.ordercast_manager.upsert_users(users_to_sync=users_to_sync)

        users_with_ordercast_id = set_user_ordercast_id(
            users_to_sync, source=self.ordercast_manager.get_users
        )
        self.odoo_manager.save_users(users_to_sync=users_with_ordercast_id)
        self.sync_billing(users_to_sync=users_with_ordercast_id)

    def sync_users_from_ordercast_to_odoo(self) -> None:
        users_to_sync = self.ordercast_manager.get_users_with_related_entities()
        logger.info(
            f"Received merchants => {len(users_to_sync)}, started syncing with Odoo."
        )
        self.odoo_manager.sync_users(users_to_sync)

    def sync_billing(self, users_to_sync: list[dict[str, Any]]) -> None:
        for partner in users_to_sync:
            self.ordercast_manager.create_billing_address(partner)
            self.ordercast_manager.create_shipping_address(partner)

    def sync_products(self, full_sync: bool = False) -> None:
        self.sync_categories_to_ordercast()
        self.sync_attributes_to_ordercast()
        self.sync_products_to_ordercast(full_sync=full_sync)
        self.sync_product_variants_to_ordercast(
            full_sync=full_sync,
        )

    def sync_categories_to_ordercast(self) -> None:
        categories = self.odoo_manager.get_categories()
        logger.info(
            f"""
            Received {len(categories['objects']) if categories['objects'] else 0}
            categories, start saving them.
            """
        )
        if categories:
            validate_categories(categories)
            self.ordercast_manager.save_categories(
                categories_to_sync=categories["objects"]
            )
            self.odoo_manager.save_categories(
                categories_to_sync=set_ordercast_id(
                    items=categories["objects"],
                    source=self.ordercast_manager.get_categories,
                )
            )

    def sync_attributes_to_ordercast(self) -> None:
        attributes = self.odoo_manager.get_product_attributes()

        logger.info(
            f"""
            Received {len(attributes['objects']) if attributes['objects'] else 0}
            attributes with total of
            {sum([len(a['values']) for a in attributes['objects'] if 'values' in a])}
            values, start saving them.
            """
        )

        if attributes:
            validate_attributes(attributes)
            attributes_to_sync = [
                get_attribute_data(attribute) for attribute in attributes["objects"]
            ]
            attribute_values_to_sync = {
                a["id"]: a.get("values", []) for a in attributes["objects"]
            }

            logger.info("Syncing attributes")
            self.ordercast_manager.save_attributes(
                attributes_to_sync=attributes_to_sync
            )
            attributes_with_ordercast_id = set_ordercast_id(
                items=attributes_to_sync,
                source=self.ordercast_manager.get_attributes,
            )
            self.odoo_manager.save_attributes(
                attributes_to_sync=attributes_with_ordercast_id
            )

            self.sync_attribute_values(
                attribute_values_to_sync=attribute_values_to_sync,
                attributes_with_ordercast_id=attributes_with_ordercast_id,
            )

    def sync_attribute_values(
        self,
        attribute_values_to_sync: dict[int, Any],
        attributes_with_ordercast_id: list[dict[str, Any]],
    ) -> None:
        logger.info("Syncing attribute values")

        attributes_odoo_to_ordercast_mapper = {
            a["id"]: a["ordercast_id"] for a in attributes_with_ordercast_id
        }

        self.ordercast_manager.save_attribute_values(
            attributes_odoo_to_ordercast_mapper=attributes_odoo_to_ordercast_mapper,
            attribute_values_to_sync=attribute_values_to_sync,
        )
        self.odoo_manager.save_attribute_values(
            attribute_values_to_sync=set_attribute_value_ordercast_id(
                attributes_odoo_to_ordercast_mapper=attributes_odoo_to_ordercast_mapper,
                attribute_values=[
                    value
                    for attribute_value in attribute_values_to_sync.values()
                    for value in attribute_value
                ],
                source=self.ordercast_manager.get_attribute_values,
            )
        )

    def sync_products_to_ordercast(self, full_sync: bool = False) -> None:
        last_sync_date = (
            None if full_sync else self.repo.get_key(RedisKeys.LAST_PRODUCT_SYNC)
        )
        products = self.odoo_manager.get_products(last_sync_date)

        logger.info(
            f"""Received {len(products['objects']) if has_objects(products) else 0}
            products, start saving them.
            """
        )

        if has_objects(products):
            validate_products(products)

            products_to_sync = [
                get_product_data(product, odoo_repo=self.repo)
                for product in products["objects"]
            ]

            self.ordercast_manager.save_products(products_to_sync=products_to_sync)
            self.odoo_manager.save_products(
                products_to_sync=set_ordercast_id(
                    items=products_to_sync,
                    source=self.ordercast_manager.get_products,
                    key="sku",
                )
            )

    def sync_product_variants_to_ordercast(
        self,
        full_sync: bool = False,
    ) -> None:
        last_sync_date = (
            None
            if full_sync
            else self.repo.get_key(RedisKeys.LAST_PRODUCT_VARIANT_SYNC)
        )
        product_variants = self.odoo_manager.get_product_variants(last_sync_date)
        has_product_variants = has_objects(product_variants)

        logger.info(
            f"""
            Received {len(product_variants['objects']) if has_product_variants else 0}
            products variants.
            """
        )

        if has_product_variants:
            validate_product_variants(product_variants["objects"])
            logger.info(
                """
                Starting saving product variants after
                saving categories and attributes.
                """
            )

            product_variants_to_sync = [
                get_product_variant_data(
                    product_variant=product_variant, odoo_repo=self.repo
                )
                for product_variant in product_variants["objects"]
            ]

            units = self.odoo_manager.get_units()
            self.ordercast_manager.save_product_variants(
                product_variants=product_variants_to_sync,
                units=units,
            )
            self.odoo_manager.save_product_variants(
                product_variants_to_sync=set_ordercast_id(
                    items=product_variants_to_sync,
                    source=self.ordercast_manager.get_product_variants,
                    key="slug_name",
                )
            )

    def sync_delivery_methods_and_pickup_locations(self) -> None:
        delivery_options = self.odoo_manager.receive_delivery_options()
        logger.info(
            f"""
Received {len(delivery_options['objects']) if has_objects(delivery_options) else 0}
delivery options, start saving them."""
        )
        if delivery_options:
            validate_delivery_options(delivery_options)

            delivery_options_to_sync = [
                get_delivery_option_data(delivery_option)
                for delivery_option in delivery_options["objects"]
            ]

            self.ordercast_manager.save_delivery_options(delivery_options_to_sync)
            self.odoo_manager.save_delivery_options(delivery_options_to_sync)

        pickup_locations = self.odoo_manager.receive_pickup_locations(
            odoo_repo=self.repo
        )
        logger.info(
            f"""
Received {len(pickup_locations['objects']) if has_objects(pickup_locations) else 0}
warehouses, start saving them."""
        )
        if pickup_locations:
            validate_pickup_locations(pickup_locations)

            pickup_locations_to_sync = [
                get_pickup_location_data(pickup_location)
                for pickup_location in pickup_locations["objects"]
            ]

            self.ordercast_manager.save_pickup_locations(
                pickup_locations_to_sync=pickup_locations_to_sync
            )
            self.odoo_manager.save_pickup_locations(
                pickup_locations_to_sync=pickup_locations_to_sync
            )

    def sync_orders_with_odoo(
        self,
        order_ids: Optional[list[int]] = None,
        from_date: Optional[datetime] = None,
    ) -> None:
        orders = self.ordercast_manager.get_orders_for_sync(
            odoo_repo=self.repo, order_ids=order_ids, from_date=from_date
        )
        logger.info(f"Loaded {len(orders)} orders, start sending them to Odoo.")
        _, unique_orders = self.repo.get_diff(
            compare_to=RedisKeys.ORDERS,
            comparable=RedisKeys.SYNC_ORDERCAST_ORDERS,
            entities=[o["_remote_id"] for o in orders],
        )
        logger.info(f"Syncing only unique orders => {unique_orders}")
        self.odoo_manager.sync_orders(
            [o for o in orders if o["_remote_id"] in unique_orders]
        )

    def sync_orders_with_ordercast(self, from_date: Optional[datetime] = None) -> None:
        ctx = get_ctx()
        orders = self.odoo_manager.receive_orders(
            from_date=from_date,
            orders_invoice_attach_pending=self.odoo_manager.get_orders_invoice_attach_pending(),
        )
        logger.info(
            f"Received {len(orders) if orders else 0} orders, start saving them."
        )
        if orders:
            self.ordercast_manager.sync_orders(
                orders=orders,
                default_price_rate_id=ctx["commons"]["default_price_rate_id"],
                odoo_repo=self.repo,
            )
            self.odoo_manager.save_orders(orders)

    def load_commons(self) -> None:
        default_sector_id = self.ordercast_manager.get_sector()
        default_price_rate_id = self.repo.get_key(RedisKeys.DEFAULT_PRICE_RATE_ID)
        if not default_price_rate_id:
            default_price_rate = self.ordercast_manager.get_default_price_rate()
            default_price_rate_id = default_price_rate["id"]
            self.repo.set(
                key=RedisKeys.DEFAULT_PRICE_RATE_ID, value=default_price_rate_id
            )

        set_context_value(
            value={
                "commons": {
                    "default_sector_id": default_sector_id,
                    "default_price_rate_id": default_price_rate_id,
                }
            }
        )

def get_odoo_sync_manager(
    odoo_repo: Annotated[OdooRepo, Depends(get_odoo_repo)],
    odoo_provider: Annotated[OdooManager, Depends(get_odoo_provider)],
    ordercast_manager: Annotated[OrdercastManager, Depends(get_ordercast_manager)],
) -> OdooSyncManager:
    return OdooSyncManager(
        odoo_repo, odoo_provider, ordercast_manager, WebhookHandler(odoo_provider)
    )


import pytorch_lightning as pl
import torch
from xmuda.models.modules import Net2DFeat, Net3DFeat, FuseNet
from xmuda.models.LMSCNet import LMSCNet
from xmuda.common.utils.metrics import Metrics
import pickle
import numpy as np
import time
import os.path as osp

class RecNetLMSC(pl.LightningModule):
    def __init__(self, preprocess_dir):
        super().__init__()
        self.class_frequencies = np.array([5.41773033e+09, 1.57835390e+07, 1.25136000e+05, 1.18809000e+05,
                                           6.46799000e+05, 8.21951000e+05, 2.62978000e+05, 2.83696000e+05,
                                           2.04750000e+05, 6.16887030e+07, 4.50296100e+06, 4.48836500e+07,
                                           2.26992300e+06, 5.68402180e+07, 1.57196520e+07, 1.58442623e+08,
                                           2.06162300e+06, 3.69705220e+07, 1.15198800e+06, 3.34146000e+05])
        self.class_num = 20
        self.lmscnet = LMSCNet(
            class_num=self.class_num,
            class_frequencies=self.class_frequencies)

        with open(osp.join(preprocess_dir, "visible_voxels.pkl"), 'rb') as f:
            self.invisible_voxels = pickle.load(f)

        self.train_metrics = Metrics(self.class_num)
        self.val_metrics = Metrics(self.class_num)
        self.train_metrics_visible = Metrics(self.class_num)
        self.val_metrics_visible = Metrics(self.class_num)
        # tensorboard = self.logger.experiment

    def forward(self, batch):
        occupancy = batch['voxel_occupancy'].cuda()
        # n_points_3d = batch['n_points_3d']
#        img = batch['img']
#        bs = img.shape[0]
#        coords_3d = batch['coords_3d']
#        occupancy = torch.zeros(bs, 256, 256, 32, device=self.device)
#        # print(coords_3d.shape)
#        # prev = 0
#        for i in range(bs):
#            idx = coords_3d[:, 3] == i
#            b_coords = coords_3d[idx]
#            occupancy[i, b_coords[:, 0], b_coords[:, 1], b_coords[:, 2]] = 1.0
#            # prev = n_point
#        occupancy = occupancy.transpose(2, 3)

        out = self.lmscnet(occupancy)
        return out

    def training_step(self, batch, batch_idx):

        pred = self(batch)

        target = batch['ssc_label_1_4'].cuda()
        loss = self.lmscnet.compute_loss(pred, target)

        self.train_metrics.add_batch(prediction=pred, target=target)
        self.train_metrics_visible.add_batch(prediction=pred,
                                             target=target,
                                             scenes=batch['scene'],
                                             invisible_data_dict=self.invisible_voxels)

        self.log('train/loss', loss.item())
        self.log('train/loss_ssc', loss.item())
        for metrics, suffix in [(self.train_metrics, ""), (self.train_metrics_visible, "_visible")]:

            self.log("train/mIoU" + suffix,
                     metrics.get_semantics_mIoU().item())
            self.log("train/IoU" + suffix, metrics.get_occupancy_IoU().item())
            self.log("train/Precision" + suffix,
                     metrics.get_occupancy_Precision().item())
            self.log("train/Recall" + suffix,
                     metrics.get_occupancy_Recall().item())
            self.log("train/F1" + suffix, metrics.get_occupancy_F1().item())
            metrics.reset_evaluator()

        return loss

    def validation_step(self, batch, batch_idx):
        pred = self(batch)

        target = batch['ssc_label_1_4'].cuda()
        loss = self.lmscnet.compute_loss(pred, target)

        # loss = self.bce_logits_loss(logits, occ_labels)
        self.log('val/loss', loss.item())
        self.log('val/loss_ssc', loss.item())

        self.val_metrics.add_batch(prediction=pred, target=target)
        self.val_metrics_visible.add_batch(prediction=pred,
                                           target=target,
                                           scenes=batch['scene'],
                                           invisible_data_dict=self.invisible_voxels)

        # pred_occ_labels = (torch.sigmoid(logits) > 0.5).float()
        # acc = (pred_occ_labels == occ_labels).float().mean()
        # self.log('train/acc', acc.item())

    def validation_epoch_end(self, outputs):
        for metrics, suffix in [(self.val_metrics, ""), (self.val_metrics_visible, "_visible")]:
            self.log("val/mIoU" + suffix,
                     metrics.get_semantics_mIoU().item())
            self.log("val/IoU" + suffix, metrics.get_occupancy_IoU().item())
            self.log("val/Precision" + suffix,
                     metrics.get_occupancy_Precision().item())
            self.log("val/Recall" + suffix,
                     metrics.get_occupancy_Recall().item())
            self.log("val/F1" + suffix, metrics.get_occupancy_F1().item())
            metrics.reset_evaluator()

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=1e-4)
        return optimizer


import factory

from .models import User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    tel = factory.Sequence(lambda n: f"{n:08d}")
    name = factory.Faker("name")
    is_active = True


import requests
from bs4 import BeautifulSoup
import random
# columbus salami: 073007107034

def lookup(barcode):

    # list of user agents to not raise alert
    user_agent_list = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)',
        'Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134'
    ]

    # pick a random user agent
    user_agent = random.choice(user_agent_list)

    # initialize header with user agent
    headers = {
        "User-Agent": user_agent
    }

    # create URL with entered barcode
    URL = "https://us.openfoodfacts.org/product/"+str(barcode)

    # grab html content and apply beautifulsoup
    page = requests.get(URL, headers=headers)
    soup = BeautifulSoup(page.content, "html.parser")

    # search for name of product
    raw_name = soup.find('title')
    name = raw_name.text.strip()
    print(f"Product: {name}")

    # list of ids for nutritional types lookup
    type_list = [
        ['nutriment_fat_tr', 'fat'],
        ['nutriment_cholesterol_tr', 'cholesterol'],
        ['nutriment_salt_tr', 'salt'],
        ['nutriment_sodium_tr', 'sodium'],
        ['nutriment_carbohydrates_tr', 'carbohydrates'],
        ['nutriment_fiber_tr', 'fiber'],
        ['nutriment_sugars_tr', 'sugars'],
        ['nutriment_proteins_tr', 'proteins']
    ]

    # iterate over all given contents
    for item in type_list:
        # search for given content
        result = soup.find(id=item[0])

        # search for values within content
        inner_result = result.find_all(class_="nutriment_value")

        # iterate over all values and insert into list initalized with type in 0 place.
        # format [type, 100g value, serving value, extra info...]
        nutrition_list = []
        nutrition_list.append(item[1])
        for nutriment_value in inner_result:
            nutrition_list.append(nutriment_value.text.strip())

        # see everything returned
        #print(nutrition_list)

        # grab necessary values
        nutri_type = nutrition_list[0]
        nutri_100g = nutrition_list[1]
        nutri_ss = nutrition_list[2]

        print(f"Type: {nutri_type}")
        print(f"\tServing Size: {nutri_ss}")
        print(f"\t100g: {nutri_100g}")


import os
import shutil
import subprocess
import sys
import time

import cv2
import numpy as np
import torch
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms

from utils.database.Pixelmap import PixelMap_fold_STmap

raw_video = "./VideoCapture/raw_video.MOV"
time_stamps_txt = "./VideoCapture/time_stamps.txt"
camera_dir = "./Camera/"
landmark_exe = "points81.exe"
face_landmarks_dir = "./VideoCapture/rt_face_landmarks/"
st_maps_dir = "./VideoCapture/STmaps/"
matlab_dir = "./Matlab for STmap/"
matlab_exe = "map_generation.exe"
out_video_dir = "./Test Record/"

# Clear Cache
face_landmarks_cache = os.listdir(face_landmarks_dir)
for f in face_landmarks_cache:
    os.remove(face_landmarks_dir + f)

st_maps_cache = os.listdir(st_maps_dir)
for f in st_maps_cache:
    shutil.rmtree(st_maps_dir + f)

if os.path.exists(raw_video):
    os.remove(raw_video)

if os.path.exists(time_stamps_txt):
    os.remove(time_stamps_txt)

# Show Welcome Prompt
welcome_window = "Welcome to Heart Rate Estimation Based on Face Perception"
frame_welcome_raw = np.zeros((540, 960, 3), np.uint8)
frame_welcome_raw[0:, 0:, 0], frame_welcome_raw[0:, 0:, 1], frame_welcome_raw[0:, 0:, 2] \
    = 239, 173, 0  # BGR
cv2.namedWindow(welcome_window, cv2.WINDOW_NORMAL)
cv2.resizeWindow(welcome_window, 960, 540)
cv2.setWindowProperty(welcome_window, cv2.WND_PROP_TOPMOST, 1)
welcome_end_time = time.time() + 5
while time.time() < welcome_end_time:
    frame_welcome = frame_welcome_raw.copy()
    cv2.putText(
        frame_welcome, "Welcome!", (50, 80),
        cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 5
    )
    cv2.putText(
        frame_welcome, "Heart Rate Estimation Based on Face Perception", (50, 140),
        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3
    )
    cv2.putText(
        frame_welcome,
        "The camera will be opened in " + str(round(welcome_end_time - time.time())) + "s.",
        (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
    )
    cv2.putText(
        frame_welcome,
        "After being opened, it may take another few seconds for the camera to be ready.",
        (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2
    )
    cv2.putText(
        frame_welcome, "Press Esc before the camera is opened to exit the program.", (50, 300),
        cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
    )
    cv2.imshow(welcome_window, frame_welcome)
    if cv2.waitKey(10) == 27:
        print("Keyboard Interrupt: Program ended by Esc")
        cv2.destroyAllWindows()
        os.system("pause")
        sys.exit(0)

# Record Raw Video
# Open Camera
camera_id = 0
capture = cv2.VideoCapture(camera_id)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Close Welcome Window
cv2.destroyWindow(welcome_window)

# Video Writer
vw = cv2.VideoWriter()
codec = vw.fourcc('a', 'v', 'c', '1')
vw.open(raw_video, codec, 30.0, (1280, 720))
if not vw.isOpened():
    print("Error: Failed to instantiate video writer")
    os.system("pause")
    sys.exit(1)

# Process Each Frame
frame_count = 0
while True:
    cap_suc, frame = capture.read()
    if not cap_suc:
        print("Error: Failed to open camera")
        os.system("pause")
        sys.exit(1)
    frame_count += 1

    # Write Time
    wf = open(time_stamps_txt, mode='a')
    wf.write(str(round(time.time() * 1000)) + "\n")
    wf.close()

    # Write Video
    vw.write(frame)

    cv2.putText(
        frame, time.ctime() + "    Frame: " + str(frame_count), (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX, 1, (215, 120, 0), 2
    )
    cv2.putText(
        frame, "You can record for as long as you want, but at least 360 frames.", (10, 80),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (64, 64, 255), 2
    )
    cv2.putText(
        frame, "Press Esc to Stop Recording After 360 Frames", (10, 120),
        cv2.FONT_HERSHEY_SIMPLEX, 1, (64, 64, 255), 2
    )

    camera_window = "Heart Rate Estimation Based on Face Perception"
    cv2.namedWindow(camera_window, cv2.WINDOW_NORMAL)
    cv2.resizeWindow(camera_window, 960, 540)
    cv2.setWindowProperty(camera_window, cv2.WND_PROP_TOPMOST, 1)
    cv2.imshow(camera_window, frame)

    if cv2.waitKey(1) == 27 and frame_count > 360:
        break

capture.release()
vw.release()
cv2.destroyWindow(camera_window)

# Face Landmarking
os.chdir(camera_dir)
try:
    subprocess.check_output(landmark_exe, creationflags=subprocess.CREATE_NEW_CONSOLE)
except subprocess.CalledProcessError:
    print("Error: No face detected in the current frame. Please keep steady and try again.")
    os.system("pause")
    sys.exit(1)

# Show prompt
stmap_window = "Generating STmaps..."
frame_stmap = np.zeros((540, 960, 3), np.uint8)
frame_stmap[0:, 0:, 0], frame_stmap[0:, 0:, 1], frame_stmap[0:, 0:, 2] \
    = 239, 173, 0  # BGR
cv2.namedWindow(stmap_window, cv2.WINDOW_NORMAL)
cv2.resizeWindow(stmap_window, 960, 540)
cv2.setWindowProperty(stmap_window, cv2.WND_PROP_TOPMOST, 1)
cv2.putText(
    frame_stmap, "Heart Rate Estimation Based on Face Perception",
    (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3
)
cv2.putText(
    frame_stmap, "Generating Spatial-Temporal Maps (STmaps) of the video...",
    (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
)
cv2.putText(
    frame_stmap, "A console application implemented by MATLAB should be running.",
    (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
)
cv2.putText(
    frame_stmap, "This may take up to several minutes. Please wait...",
    (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
)
cv2.putText(
    frame_stmap, "This window will close automatically after completion.",
    (50, 300), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2
)
cv2.imshow(stmap_window, frame_stmap)
cv2.waitKey(10)

# STmap Generation
os.chdir("../")
os.chdir(matlab_dir)
try:
    subprocess.check_output(matlab_exe, creationflags=subprocess.CREATE_NEW_CONSOLE)
except subprocess.CalledProcessError:
    print("Error: An unexpected error occurred while generating STmaps.")
    cv2.destroyAllWindows()
    os.system("pause")
    sys.exit(1)

# Close Prompt
cv2.destroyWindow(stmap_window)

# Get Heart Rate from Network
os.chdir("../")

video_length = 300

toTensor = transforms.ToTensor()
resize = transforms.Resize(size=(320, 320))

test_dataset = PixelMap_fold_STmap(root_dir='./VideoCapture/STmaps/',
                                   Training=False,
                                   transform=transforms.Compose([resize, toTensor]),
                                   VerticalFlip=False,
                                   video_length=video_length)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=0)

################################################################################
net_id = "1662170218"

net_file = "./model.pt"
if os.path.exists("./HR_models/"):
    net_file = './HR_models/hr_model_cuda_' + net_id + ".pt"

net = torch.load(net_file, map_location=torch.device('cpu'))

net.to(torch.device("cpu"))
net.eval()

################################################################################
output_list = []
for (data, fps, idx) in test_loader:
    data = Variable(data)

    data = data.to(torch.device("cpu"))

    feat_hr, feat_n, output, img_out, \
    feat_hrf1, feat_nf1, hrf1, idx1, \
    feat_hrf2, feat_nf2, hrf2, idx2 = net(data)

    output_list.append(output.item() / (video_length / fps.item()) * 60)

################################################################################
# Show Output
print("Estimation completed: ", end="")
print(output_list)

capture_o = cv2.VideoCapture(raw_video)

vw_o = cv2.VideoWriter()
out_codec = vw_o.fourcc('a', 'v', 'c', '1')
out_video_file = out_video_dir + "Test_" + str(round(time.time())) + ".MOV"
vw_o.open(out_video_file, out_codec, 30.0, (1280, 720))

# Read All Timestamps
rf = open(time_stamps_txt, 'r')
time_list = []
line = rf.readline()
while line:
    time_list.append(eval(line[:-1]))
    line = rf.readline()
rf.close()

# Calculate Average FPS
frame_num = len(time_list)
avr_fps = frame_num / (time_list[-1] - time_list[0]) * 1000.0
frame_interval = round(avr_fps / 2)
clip_num = int((frame_num - 300) / avr_fps * 2)

# Update Output Value Every 0.5s
frm_o_count = 0
clip_idx = 0
while frm_o_count < frame_num:
    out_cap_suc, frame_o = capture_o.read()
    if not out_cap_suc:
        print("Error: Failed to read video file " + raw_video)
        cv2.destroyAllWindows()
        os.system("pause")
        sys.exit(1)
    frm_o_count += 1

    # Update Output
    if frm_o_count % frame_interval == 0 and clip_idx < clip_num - 1:
        clip_idx += 1

    # Out of Bound
    if clip_idx >= len(output_list):
        print("Warning: No more output values @ Frame " + str(frm_o_count) + " of " + str(frame_num))
        clip_idx = len(output_list) - 1

    # Put Text
    cv2.putText(
        frame_o,
        time.ctime(time_list[frm_o_count - 1] / 1000.0) + "    Frame: " + str(frm_o_count),
        (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (215, 120, 0), 2
    )
    cv2.putText(
        frame_o, "Estimated Heart Rate: ",
        (10, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (215, 120, 0), 2
    )
    cv2.putText(
        frame_o, str(round(output_list[clip_idx], 1)),
        (50, 160), cv2.FONT_HERSHEY_SIMPLEX, 2, (215, 120, 0), 5
    )

    # Write Output Video
    vw_o.write(frame_o)

    # Last Frame
    if frm_o_count == frame_num - 1:
        cv2.putText(
            frame_o, "Successfully finished! You can close this window and view the video at ",
            (10, 210), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (64, 64, 255), 2
        )
        cv2.putText(
            frame_o, out_video_file,
            (10, 250), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (64, 64, 255), 2
        )

    output_window = "Estimated Heart Rates"
    cv2.namedWindow(output_window, cv2.WINDOW_NORMAL)
    cv2.resizeWindow(output_window, 960, 540)
    cv2.setWindowProperty(output_window, cv2.WND_PROP_TOPMOST, 1)
    cv2.imshow(output_window, frame_o)

    if frm_o_count == frame_num - 1:
        cv2.waitKey(0)
    else:
        cv2.waitKey(round(1000 / avr_fps))

capture_o.release()
vw_o.release()


import sqlite3
import uuid
from configparser import ConfigParser

cp = ConfigParser()
cp.read("bms.ini")

class Database_test():
    def __init__(self):
        self.nic_count = int(cp.get('nic', 'count'))
        self.conn = sqlite3.connect("test.db", timeout=10)
        self.cursor = self.conn.cursor()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

    # task execute
    def create(self):
        sql = "create table create_bms " \
              "(create_id varchar(50) primary key, ipmi_ip varchar(30), dhcp_ip varchar(30), " \
              "poweroff_s varchar(20), poweron_s varchar(20), clone_s varchar(20)," \
              "init_s varchar(20), powerreset_s varchar(20))"
        self.cursor.execute(sql)

    def insert(self, uuid, ipmi_ip):
        sql = "insert into create_bms " \
              "(create_id, ipmi_ip, dhcp_ip, poweroff_s, poweron_s, clone_s, init_s, powerreset_s) " \
              "values ( '{}', '{}', 0, 0, 0, 0, 0, 0)".format(uuid, ipmi_ip)
        self.cursor.execute(sql)

    # server hardinfo table
    def create_host(self):
        macs = ''
        for i in range(self.nic_count):
            mac = "mac%s varchar(20), " % (i + 1)
            macs += mac
        sql = "create table host (ipmi_ip varchar(30) primary key, " \
              + macs + "result varchar(10))"

        self.cursor.execute(sql)

    def insert_host(self, attr):
        macs = ''
        for i in range(self.nic_count + 1):
            mac = "?, "
            macs += mac
        sql = "insert into host values (" + macs + "?)"
        attr.append("0")
        self.cursor.execute(sql, tuple(attr))

    def select_host(self, attr):
        sql = "select count(*) from host where ipmi_ip='%s'" % attr
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    # dhcpIP table
    def create_dhcpinfo(self):
        sql = "create table dhcpinfo " \
              "(create_id varchar(50) primary key, dhcp_ip varchar(30), mac varchar(30))"
        self.cursor.execute(sql)

    def insert_dhcpinfo(self, dhcp_ip, mac):
        sql = "insert into dhcpinfo " \
              "(create_id, dhcp_ip, mac) " \
              "values ( '{}', '{}', '{}')".format(str(uuid.uuid4()), dhcp_ip, mac)
        self.cursor.execute(sql)

    # server table
    def create_host_conf(self):
        ips = ''
        for i in range(self.nic_count):
            ip = "ip%s varchar(20), " % (i + 1)
            ips += ip
        sql = "create table host_conf " \
              "(ipmi_ip varchar(20) primary key, " + ips + "hostname varchar(30))"
        self.cursor.execute(sql)

    def insert_host_conf(self, attr):
        ips = ''
        for i in range(self.nic_count + 1):
            ip = "?, "
            ips += ip
        sql = "insert into host_conf values (" + ips + "?)"
        attr.append("bms")
        self.cursor.execute(sql, tuple(attr))

    # update create_bms table
    def update_create_bms(self, attr, value, pk):
        sql = "update create_bms " \
              "set %s='%s' " \
              "where create_id='%s'" % \
              (attr, value, pk)
        self.cursor.execute(sql)

    def update_host(self, value, ip):
        sql = "update host " \
              "set result='%s' " \
              "where ipmi_ip='%s'" % \
              (value, ip)
        self.cursor.execute(sql)

    def select(self):
        sql = "select * from host where result!='success'"
        self.cursor.execute(sql)
        value = self.cursor.fetchall()
        return value

    def select_host_ip(self):
        sql = "select ipmi_ip from host"
        self.cursor.execute(sql)
        value = self.cursor.fetchall()
        return value

    def select_dhcpip_count(self):
        sql = "select count(*) from dhcpinfo"
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    def select_dhcp_ip(self):
        sql = "select dhcp_ip from dhcpinfo"
        self.cursor.execute(sql)
        value = self.cursor.fetchall()
        return value

    def select_attr(self, attr, id):
        sql = "select {} from create_bms where create_id='{}'".format(attr, id)
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value[0]

    def select_mac(self, mac):
        sql = "select ipmi_ip, mac1 from host where mac1='{}'".format(mac)
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    def select_create_id(self, ip):
        sql = "select create_id from create_bms where ipmi_ip='{}'".format(ip)
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    def select_host_conf(self, ip):
        sql = "select * from host_conf " \
              "where ipmi_ip='{}'".format(ip)
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    def select_host_conf_count(self):
        sql = "select count(*) from host_conf "
        self.cursor.execute(sql)
        value = self.cursor.fetchone()
        return value

    def select_ipmi_ips(self):
        sql = "select ipmi_ip from host_conf"
        self.cursor.execute(sql)
        value = self.cursor.fetchall()
        return value

    def delete_create_bms(self, ip):
        sql = "delete from create_bms " \
              "where ipmi_ip='{}'".format(ip)
        self.cursor.execute(sql)

    def delete_dhcpinfo(self):
        sql = "delete from dhcpinfo"
        self.cursor.execute(sql)


from xml.dom.minidom import Document
from validate_docbr import CPF, CNPJ

class documento:
    @staticmethod
    def cria_documento(documento):
        if len(documento) == 11:
            return Cpf(documento)
        elif len(documento) == 14:
            return Cnpj(documento)
        else:
            raise ValueError ("Numero de caracteres incorreto!")

class Cpf:
    def __init__(self, documento):
        documento = str(documento)
        if self.validar_cpf(documento):
            self.cpf = documento
        else:
            raise ValueError("CPF inválido")

    def validar_cpf(self, cpf):
            validador = CPF()
            return validador.validate(cpf)

    def __str__(self):
        cpf = CPF()
        mascara = cpf.mask(self.cpf)
        return ("O CPF é {}".format(mascara))

class Cnpj:
    def __init__(self, documento):
        documento = str(documento)
        if self.valida_cnpj(documento):
            self.cnpj = documento
        else:
            raise ValueError("CNPJ inválido!")

    def valida_cnpj(self, cnpj):
            validador = CNPJ()
            return validador.validate(cnpj)

    def __str__(self):
        cnpj = CNPJ()
        mascara= cnpj.mask(self.cnpj)
        return ("O CNPJ é {}".format(mascara))

Eduardo = documento.cria_documento("04082987070")
print(Eduardo)

StarBit_enterprise = documento.cria_documento("02828446000134")
print(StarBit_enterprise)


from nltk.tokenize import sent_tokenize, word_tokenize
import Stopwords
import string
import re

class tweet:

    def __init__(self, dataline, wordMap, uniWordMap):
        self.tweetwords = []
        self.dataline = dataline
        self.wordMap = wordMap
        self.uniWordMap = uniWordMap
        self.time = 0

    def tweet(self):
        number = len(self.wordMap)
        inline = self.dataline
        words = []
        tokens = self.tokenize(inline)

        for token in tokens:
            tmpToken = token.lower()
            if ((not Stopwords.isStopword(tmpToken)) and (not self.isNoisy(tmpToken))):
                if (not(tmpToken in self.wordMap)):
                    words.append(number)
                    self.wordMap[tmpToken] = number
                    number += 1
                    self.uniWordMap.append(tmpToken)
                else:
                    words.append(self.wordMap.get(tmpToken))

        for w in words:
            self.tweetwords.append(w)

        words.clear()
        tokens.clear()

    def isNoisy(self, token):
        if(("#pb#" in token.lower()) or ("http:" in token.lower())):
            return True

        if("@" in token.lower()):
            return True

        if(token in string.punctuation):
            return True

        return False

    def tokenize(self, line):
        tokens = word_tokenize(line)
        return tokens


import time
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

Notify.init('Nag')

def show(msg):
	print(msg)
	notification = Notify.Notification.new(msg)
	notification.show()
	time.sleep(10)
	notification.close()

def show_all(msgs):
	show('\r\n'.join(msgs))


from typing import List

input = """office, country, telephone
chicago, USA, +1 312 373 1000
Beijing, China, +86 4008 900 505
Bangalore, India, +91 80 4064 9570
"""

def acquireData(input):
    lines: List[str] = input.split("\n")
    result = []
    loopItems = lines[1:].filter
    for line in loopItems:
        record = line.split(',')
        if record[1].strip() == "India":
            result.append({
                'city': record[0].strip(),
                'phone': record[2].strip()
            })
        return result


from django.conf.urls import url

from . import views

urlpatterns = [
        url(r'^questions/(?P<pk>[0-9]+)/$', views.QuestionList.as_view(), name='questions-list'),
        url(r'^question_answer_submit/$', views.QuestionAnswerSubmit.as_view(), name='question-answer-submit'),
        url(r'^tips_tricks/$', views.TipsAndTricks.as_view(), name='tips-tickets'),
        url(r'^articles/$', views.ArticleList.as_view(), name='articles')
]


import torch.nn as nn
import torch

class TD_LSTM(nn.Module):
	def __init__(self):
		super().__init__()
		self.rnn = nn.LSTM(10, 20, 3) # params: embedding, hidden_size, num_layers
	def forward(self):
		input = torch.randn(5, 3, 10)
		h0 = torch.randn(2, 3, 20)
		c0 = torch.randn(2, 3, 20)
		output, (hn, cn) = self.rnn(input, (h0, c0))
		print('output==hn', output[-1,:,:]==hn[-1])

model = TD_LSTM()
for i in model.parameters():
	print(i.size())

def _reset_params(model):
	for child in model.children():
		print('child-->', child, type(child))
		i = 0
		for p in child.parameters():
			i=i+1
			print('i-->',i)
			print('p-->', p)
	# 		if p.requires_grad:
	# 			if len(p.shape) > 1:
	# 				opt.initializer(p)
	# 			else:
	# 				stdv = 1. / math.sqrt(p.shape[0])
	# 				torch.nn.init.uniform_(p, a=-stdv, b=stdv)

# _reset_params(model)


from pathlib import Path
import rasterio
import numpy as np

def get_project_path():
    return Path(__file__).parent.parent.absolute()

project_path = get_project_path()
fb_path = project_path / 'data/humdata/population_nga_2018-10-01.tif'
grid_path = project_path / 'data/grid3/NGA - population - v1.2 - mastergrid.tif'

fb_raster = rasterio.open(fb_path)
grid_raster = rasterio.open(grid_path)

data_eye = np.eye(90, dtype=np.uint8)
data_zeros = np.zeros(90*90, dtype=np.uint8).reshape(90,90)
data_ones = np.ones(90*90, dtype=np.uint8).reshape(90,90)
data_left_edge = np.copy(data_zeros)
data_left_edge[:,0] = 1
data_top_edge = np.copy(data_zeros)
data_top_edge[0,:] = 1
data_right_edge = np.copy(data_zeros)
data_right_edge[:,-1] = 1
data_bottom_edge = np.copy(data_zeros)
data_bottom_edge[-1,:] = 1

files = {
    "eye": data_eye,
    "zeros": data_zeros,
    "ones": data_ones,
    "left_edge": data_left_edge,
    "top_edge": data_top_edge,
    "right_edge": data_right_edge,
    "bottom_edge": data_bottom_edge
}

for name,data in files.items():

    with rasterio.open( \
            project_path / f'test/data/pipeline/{name}.tif', \
            'w', \
            driver='GTiff', \
            width=data.shape[1], \
            height=data.shape[0], \
            count=1, \
            dtype=data.dtype, \
            crs=fb_raster.crs, \
            transform=fb_raster.transform \
            ) as dst:
        dst.write(data, indexes=1)

# with this window, not all pixels in the test raster will have corresponding pixel here
grid_data = grid_raster.read(1, window=((0,30),(0,30)))
with rasterio.open( \
        project_path / 'test/data/pipeline/grid3-30.tif', \
        'w', \
        driver='GTiff', \
        width=grid_data.shape[1], \
        height=grid_data.shape[0], \
        count=1, \
        dtype=grid_data.dtype, \
        crs=grid_raster.crs, \
        transform=grid_raster.transform \
        ) as dst:
    dst.write(grid_data, indexes=1)

# with this window, all pixels in the test raster will have corresponding pixel here
grid_data = grid_raster.read(1, window=((0,60),(0,60)))
with rasterio.open( \
        project_path / 'test/data/pipeline/grid3-60.tif', \
        'w', \
        driver='GTiff', \
        width=grid_data.shape[1], \
        height=grid_data.shape[0], \
        count=1, \
        dtype=grid_data.dtype, \
        crs=grid_raster.crs, \
        transform=grid_raster.transform \
        ) as dst:
    dst.write(grid_data, indexes=1)