import json
import logging
import os
import uuid

logger = logging.getLogger(__name__)

ALLOWED_EXTENSIONS = ['.wav', '.mp3', '.m4a']

class KnownRequestParseError(Exception):
    """
    Occurs when encountering an HTTP request that does not conform to the API interface.
    """
    pass

class UploadedFile(object):
    """
    Represents a file extracted from a multipart/form-data HTTP request and saved to disk.
    """
    def __init__(self, original_filename, stored_filename, mime_type, metadata=None):
        self.original_filename = original_filename
        self.stored_filename = stored_filename
        self.mime_type = mime_type
        self.metadata = metadata or {}

def extract_file_from_http_request(headers, body, upload_dir, unique_id=None):
    """
    Parse the raw HTTP POST request and extract the audio file from it.

    Parameters
    ----------
    headers : dict
        Request headers.
    body : bytes
        Raw request body.
    upload_dir: str
        Path to the folder for storing extracted files.
    unique_id : str (optional)
        A string uniquely identifying this HTTP request.

    Returns
    -------
    UploadedFile
    """
    headers = {
        header.lower(): value
        for header, value in headers.items()
    }
    if 'content-type' not in headers or 'boundary=' not in headers['content-type']:
        msg = 'Expected a multipart/form-data request with content type defined'
        raise KnownRequestParseError(msg)

    unique_id = unique_id or str(uuid.uuid4())
    content_type = headers['content-type']

    # Separate the file part from multipart form data.
    newline = b'\r\n'
    boundary = content_type.split('boundary=')[-1].replace('"', '').encode('utf-8')
    initial_delimiter = b'--' + boundary + newline
    final_delimiter = newline + b'--' + boundary + b'--'
    try:
        file_part = body.split(initial_delimiter)[1].split(final_delimiter)[0]
    except Exception:
        logger.warning('Failed to parse the raw multipart body', exc_info=True)
        raise KnownRequestParseError('Expected a non-empty multipart/form-data body')

    # Separate Content-Disposition, Content-Type (optional), and file content.
    file_part_lines = [line for line in file_part.split(newline) if line]
    cdisp_line = None
    ctype_line = None
    file_content = None

    for i, line in enumerate(file_part_lines):
        if line.startswith(b'Content-Disposition'):
            cdisp_line = line.decode('utf-8')
        elif line.startswith(b'Content-Type'):
            ctype_line = line.decode('utf-8')
        else:
            file_content = newline.join([fpl for fpl in file_part_lines[i:]])
            break

    # Parse Content-Disposition to extract parameter name and original filename.
    cdisp_line = cdisp_line.replace('; name=', '"name": ')
    cdisp_line = cdisp_line.replace('; filename=', ', "filename": ')
    cdisp_line = '{' + cdisp_line.replace('Content-Disposition: form-data', '') + '}'
    try:
        content_disposition = json.loads(cdisp_line)
    except Exception:
        logger.warning(f'Failed to parse Content-Disposition line: {cdisp_line}', exc_info=True)
        raise KnownRequestParseError('Malformed request body')

    # Check if the request form is valid.
    if content_disposition['name'] != 'audio-file':
        raise KnownRequestParseError('Expected a file with key "audio-file" in the request')

    original_filename = content_disposition['filename']
    original_extension = os.path.splitext(original_filename)[1].lower()
    mime_type = ctype_line.replace('Content-Type: ', '') if ctype_line else 'text/plain'
    logger.info(f'Original filename: "{original_filename}". Inferred MIME type: {mime_type}')

    if original_extension not in ALLOWED_EXTENSIONS:
        msg = 'Only the following file extensions are supported: ' + ', '.join(ALLOWED_EXTENSIONS)
        raise KnownRequestParseError(msg)

    # Save the file to disk.
    storage_path = os.path.join(upload_dir, f'{unique_id}{original_extension}')
    logger.info('Saving uploaded file ({} bytes) to: "{}"'.format(len(file_content), storage_path))
    with open(storage_path, 'wb') as f:
        f.write(file_content)

    return UploadedFile(
        original_filename=original_filename,
        stored_filename=storage_path,
        mime_type=mime_type,
    )


import sys
from array import array

from lvtools import chunk, util, graphics, palette, level_info
from lvtools.compression import decompress_data
import png
import dump_tileset as tileset

def parse_metatile_info(data):
    metatiles = []
    for i in xrange(0, len(data), 2 * 4):
        # Read 4 values from data
        tile = (util.read_word(data, i+offs) for offs in xrange(0, 8, 2))
        # Extract attributes
        tile = [(x >> 6, x & 0x10, x & 0x20) for x in tile]
        metatiles.append(tile)
    return metatiles

def main(level_header, output_file, tiles_per_row):
    output_pal = palette.assemble_palette(level_header.load_list.palettes)
    tiles = tileset.extract_tiles(level_header)

    metatile_data = decompress_data(chunk.read_chunk(level_header.metatile_chunk))
    meta_info = parse_metatile_info(metatile_data)
    del metatile_data

    def assemble_metatile(info):
        flipped_tiles = [graphics.flip_image(tiles[i], 8, hf, vf) for i, hf, vf in info]
        return graphics.layout_tiles(flipped_tiles, 8, 8, [[0, 1], [2, 3]])[0]

    meta_imgs = [assemble_metatile(info) for info in meta_info]

    output_data, output_w, output_h = graphics.layout_tiles(meta_imgs, 16, 16, tileset.rows_of_tiles(len(meta_imgs), tiles_per_row))

    with open(output_file, 'wb') as f:
        w = png.Writer(output_w, output_h, palette=output_pal)
        w.write_array(f, output_data)

if __name__ == '__main__':
    level_id = int(sys.argv[1], 0)
    level_header = level_info.load_level_header(level_id=level_id)
    main(level_header, sys.argv[2], int(sys.argv[3]))


from PyQt5 import QtGui
from PyQt5.QtWidgets import *
import requests
from bs4 import BeautifulSoup

URL = "https://kitsunekko.net/dirlist.php?dir=subtitles%2Fjapanese%2F"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html.parser')

animeLinks = []
animeNames = []
table = soup.find('table', attrs = {'id':'flisttable'})
for row in table.findAll('td', attrs = {'class':''}):
    animeURL = row.a['href']
    anime = row.find("strong").text
    animeNames.append(anime)
    animeLinks.append('https://kitsunekko.net/' + animeURL)

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("Kitsunekko Crawler")
        self.setWindowIcon(QtGui.QIcon('logo.png'))

        self.animeIndex = 0

        # 0 = all, 1 = SRT, 2 = ASS, 3 = ZIP
        self.downloadIndicator = 0
        self.fileName = "subs.srt"
        self.subtitleLinks = []

        layout = QVBoxLayout()
        animeNameLabel = QLabel("Select Anime: ")
        downloadAllbutton = QPushButton("Download All Files")
        downloadAllbutton.clicked.connect(self.downloadAll)
        downloadSRTbutton = QPushButton("Download SRT Files")
        downloadSRTbutton.clicked.connect(self.setSRT)
        downloadSRTbutton.clicked.connect(self.downloadAll)
        downloadASSbutton = QPushButton("Download ASS Files")
        downloadASSbutton.clicked.connect(self.setASS)
        downloadASSbutton.clicked.connect(self.downloadAll)
        downloadZIPbutton = QPushButton("Download ZIP and RAR Files")
        downloadZIPbutton.clicked.connect(self.setZIP)
        downloadZIPbutton.clicked.connect(self.downloadAll)

        comboBoxAnimeNames = QComboBox()
        comboBoxAnimeNames.addItems(animeNames)
        comboBoxAnimeNames.currentIndexChanged.connect(self.index_changed)
        layout.addWidget(animeNameLabel)
        layout.addWidget(comboBoxAnimeNames)
        layout.addWidget(downloadAllbutton)
        layout.addWidget(downloadSRTbutton)
        layout.addWidget(downloadASSbutton)
        layout.addWidget(downloadZIPbutton)

        comboBoxAnimeNames = QWidget()
        comboBoxAnimeNames.setLayout(layout)

        self.setCentralWidget(comboBoxAnimeNames)

    def index_changed(self, i):  # i is an int
        self.animeIndex = i

    def downloadAll(self):
        URL = animeLinks[self.animeIndex]
        r = requests.get(URL)
        soup = BeautifulSoup(r.content, 'html.parser')

        self.subtitleLinks = []
        table = soup.find('table', attrs={'id': 'flisttable'})
        for row in table.findAll('td', attrs={'class': ''}):
            subtitle = row.a['href']
            self.subtitleLinks.append('https://kitsunekko.net/' + subtitle)

        for i in range(len(self.subtitleLinks)):
            if self.downloadIndicator == 1 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'srt':
                    self.downloadSRT(i)
            if self.downloadIndicator == 2 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'ass':
                    self.downloadASS(i)
            if self.downloadIndicator == 3 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'zip' or self.subtitleLinks[i].rsplit('.', 1)[-1] == 'rar' or self.subtitleLinks[i].rsplit('.', 1)[-1] == '7z':
                    self.downloadZIP(i)

    def downloadSRT(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as srt:
            srt.write(r.content)

    def downloadASS(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as ass:
            ass.write(r.content)

    def downloadZIP(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as code:
            code.write(r.content)

    def setSRT(self):
        self.downloadIndicator = 1

    def setASS(self):
        self.downloadIndicator = 2

    def setZIP(self):
        self.downloadIndicator = 3

app = QApplication([])

window = MainWindow()
window.show()

app.exec_()


import glob
import os
import random
import h5py
from os.path import join

import numpy as np
import torch

#from fastmri_utils.data import transforms
from tqdm import tqdm

from operators import rotate_real, to_complex
from extra_utils import cut_to_01

from PIL import Image
# ----- Dataset creation, saving, and loading -----

def create_iterable_dataset(
    n, set_params, iterator, iter_params,
):
    """ Creates training, validation, and test data sets.

    Samples data signals from a data generator and stores them.

    Parameters
    ----------
    n : int
        Dimension of signals x.
    set_params : dictionary
        Must contain values for the following keys:
        path : str
            Directory path for storing the data sets.
    generator : callable
        Generator function to create signal samples x. Will be called with
        the signature generator(n, **gen_params).
    gen_params : dictionary
        Additional keyword arguments passed on to the signal generator.
    """

    dataset_train = iterator(mode='train', **iter_params)
    dataset_val = iterator(mode='val', **iter_params)
    dataset_test = iterator(mode='test', **iter_params)
    iter_train = iter(dataset_train)
    iter_val = iter(dataset_val)
    iter_test = iter(dataset_test)
    os.makedirs(os.path.join(set_params["path"], "train"), exist_ok=True)
    os.makedirs(os.path.join(set_params["path"], "val"), exist_ok=True)
    os.makedirs(os.path.join(set_params["path"], "test"), exist_ok=True)

    for idx in tqdm(range(len(iter_train)), desc="generating training signals"):
        torch.save(
            next(iter_train),
            os.path.join(
                set_params["path"], "train", "sample_{:05d}.pt".format(idx)
            ),
        )

    for idx in tqdm(range(len(iter_val)), desc="generating validation signals"):
        torch.save(
            next(iter_val),
            os.path.join(
                set_params["path"], "val", "sample_{:05d}.pt".format(idx)
            ),
        )

    for idx in tqdm(range(len(iter_test)), desc="generating test signals"):
        torch.save(
            next(iter_test),
            os.path.join(
                set_params["path"], "test", "sample_{:05d}.pt".format(idx)
            ),
        )

class Load_fluorescence_microscopy_dataset:
    """ Loads a dataset of fluorescence microscopy images.

    Parameters
    ----------
    mode : str
        One of 'train', 'test', 'val'. Decides which dataset to create.
    path_train : str
        Path to training images.
    path_val : str
        Path to validation images.
    path_test : str
        Path to testing images.
    """
    def __init__(self, mode, path_train = None, path_val = None, path_test = None):

        self.mode = mode

        self.path_train = path_train
        self.path_val = path_val
        self.path_test = path_test

        if self.mode.lower() == 'train':
            self.path = path_train
        elif self.mode.lower() == 'val':
            self.path = path_val
        elif self.mode.lower() == 'test':
            self.path = path_val
        else:
            self.path = None
        if self.path is not None:
            self.filenames = glob.glob(join(self.path, '*.tif'))
        else:
            self.filenames = [];

        self.data_size = len(self.filenames)

    def __len__(self):
        return self.data_size

    def __iter__(self):
        self.count = 0
        return self

    def __next__(self):
        if self.path is None:
            raise StopIteration
        if self.count < self.data_size:
            fname = self.filenames[self.count]
            self.count += 1
            im = np.array(Image.open(fname)).astype(np.float64)
            im /= (256*256-1)
            return torch.tensor(im, dtype=torch.float)
        else:
            raise StopIteration

class Load_fastMRI_dataset_complex(Load_fastMRI_dataset):

    def __init__(self, mode, path_train = None, path_val = None, path_test = None):
        super().__init__(mode, path_train, path_val, path_test)

    def __next__(self):
        if self.path is None:
            raise StopIteration
        if self.count < self.data_size:
            fname = self.filenames[self.count]
            self.count += 1
            with h5py.File(fname, 'r') as hf:
                rec = np.array(hf['reconstruction_rss'])
                rec = rec[int(rec.shape[0]/2), ...]
                amax = np.amax(rec)
                rec *= (255/amax)
                im = Image.fromarray(np.uint8(rec))
                im = im.resize((256,256))
                np_im = np.array(im).astype(np.float32)/255;
                X = np.zeros([2, 256,256]);
                X[0,:,:] = np_im;
                return torch.tensor(X, dtype=torch.float)
        else:
            raise StopIteration

class IPDataset(torch.utils.data.Dataset):
    """ Datasets for imaging inverse problems.

    Loads image signals created by `create_iterable_dataset` from a directory.

    Implements the map-style dataset in `torch`.

    Attributed
    ----------
    subset : str
        One of "train", "val", "test".
    path : str
        The directory path. Should contain the subdirectories "train", "val",
        "test" containing the training, validation, and test data respectively.
    """

    def __init__(self, subset, path, transform=None, device=None):
        self.path = path
        self.files = glob.glob(os.path.join(path, subset, "*.pt"))
        self.transform = transform
        self.device = device

    def __len__(self):
        return len(self.files)

    def __getitem__(self, idx):
        # load image and add channel dimension
        out = torch.load(self.files[idx])
        if out.dim() == 2:
            out = out.unsqueeze(0)
        if self.device is not None:
            out.to(self.device)
        out = (out,)
        return self.transform(out) if self.transform is not None else out

# ----- data transforms -----

class SimulateMeasurements(object):
    """ Forward operator on target samples.

    Computes measurements and returns (measurement, target) pair.

    Parameters
    ----------
    operator : callable
        The measurement operation to use.

    """

    def __init__(self, operator):
        self.operator = operator

    def __call__(self, target):
        (target,) = target
        meas = self.operator(target)
        return meas, target

class Jitter(object):
    """ Adds random pertubations to the input of (input, target) pairs.
    """

    def __init__(self, eta, scale_lo, scale_hi, n_seed=None, t_seed=None):
        self.eta = eta
        self.scale_lo = scale_lo
        self.scale_hi = scale_hi
        self.rng = np.random.RandomState(n_seed)
        self.trng = torch.Generator()
        if t_seed is not None:
            self.trng.manual_seed(t_seed)

    def __call__(self, inputs):
        meas, target = inputs
        m = meas.shape[-1]  # number of sampled measurements
        scale = (
            self.scale_lo + (self.scale_hi - self.scale_lo) * self.rng.rand()
        )
        noise = torch.randn(meas.shape, generator=self.trng).to(meas.device)
        meas_noisy = meas + self.eta / np.sqrt(m) * noise * scale
        return meas_noisy, target

# ---- run data generation -----
if __name__ == "__main__":
    import config

    np.random.seed(config.numpy_seed)
    torch.manual_seed(config.torch_seed)
    create_iterable_dataset(
        config.n, config.set_params, config.data_gen, config.data_params,
    )


"""Provides the run command and related functions.
"""

import argparse
import logging
from typing import Dict, List

from .db import init as init_db
from .scan import scan
from .copy import copy
from .encode import encode
from .prune import prune

logger = logging.getLogger(__name__)

def run_command(parent_parser: argparse.ArgumentParser, args: List[str], config: Dict):
    """Entrypoint for the CLI `run` command.

    Args:
        parent_parser (argparse.ArgumentParser): The parser from the main entrypoint.
            Used to display a full --help output by inheriting its arguments.
        args (List[str]): List of arguments not parsed by the main parser.
        config (Dict): Dictionary containing the MusicBird configuration
    """
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                     description=__doc__, prog="musicbird", parents=[parent_parser])
    parser.add_argument("--rescan", action="store_true",
                        help="Force a rescan of the entire library. This will force all files to be reprocessed")
    parser.add_argument("--pretend", action="store_true",
                        help="Show what changes would be made but don't modify any files")
    args = parser.parse_args(args)
    return run(config, args.rescan, args.pretend)

def run(config: Dict, rescan: bool = False, pretend: bool = False):
    """Scan, then process the entire music library.

    Equivalent to calling scan(), copy(), encode(), prune() in that order.

    Args:
        config (Dict): Dictionary containing the musicbird configuration.
        db (LibraryDB): Database object to read/write the library status from/to.
        pretend (bool, optional): Pretend to process/scan, but don't perform any actual operations. Defaults to False.

    Returns:
        bool: True if all files were processed successfully, false if not.
    """
    db = init_db(config, delete=rescan, pretend=pretend)

    results = []
    results.append(scan(config, db))
    results.append(copy(config, db, pretend=pretend))
    results.append(encode(config, db, pretend=pretend))
    results.append(prune(config, db, pretend=pretend))
    if False in results:
        return False
    else:
        return True


import torchvision
from torchvision.models.detection import FasterRCNN
from torchvision.models.detection.rpn import AnchorGenerator

def fasterRCNN(backbone, class_num):
    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),
                                    aspect_ratios=((0.5, 1.0, 2.0),))

    roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=['0'],
                                                    output_size=7,
                                                    sampling_ratio=2)

    # put the pieces together inside a FasterRCNN model
    model = FasterRCNN(backbone,
                    num_classes=class_num,
                    rpn_anchor_generator=anchor_generator,
                    box_roi_pool=roi_pooler)
    return model


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/7/23 19:57
# @Author  : Ly
# @File    : BasicBlock.py
# @Software: PyCharm
# @Github  : https://github.com/ly8073

import torch.nn as nn

from Cats.model.ResNet.blocks.conv_function import conv3x3, conv1x1

class BasicBlock(nn.Module):
    def __init__(self, input_channel, output_channel):
        super(BasicBlock, self).__init__()
        self.output_channel = output_channel
        self.down_sample = None
        stride = 1
        if input_channel != self.output_channel:
            self.down_sample = conv1x1(input_channel, self.output_channel, 2)
            stride = 2

        self.conv1 = conv3x3(input_channel, self.output_channel, stride)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(self.output_channel, self.output_channel)

    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.relu(out)
        out = self.conv2(out)
        if self.down_sample is not None:
            residual = self.down_sample(x)

        out += residual
        out = self.relu(out)
        return out


import time
from sound_keyboard.queue import get_queue
from sound_keyboard.face_gesture_detector.face_detection import inference
from sound_keyboard.face_gesture_detector.eye_blink_detection import get_eye_state
from sound_keyboard.face_gesture_detector.enums import (
    EyeDirection,
    EyeState,
    MouthState,
    Gestures
)
import copy
import cv2
import dlib
import numpy as np
from math import hypot
from sound_keyboard.face_gesture_detector.gaze_tracking import GazeTracking

class FaceGestureDetector:
    def __init__(self, queue):
        self.cap = cv2.VideoCapture(0)
        self.queue = queue
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor("./sound_keyboard/face_gesture_detector/shape_predictor_68_face_landmarks.dat")
        # self.debug = len(sys.argv) >= 2 and sys.argv[1] == 'DEBUG'
        self.debug = True

        self.previous = None

        self.gaze = GazeTracking()

    def get_gaze_state(self, x):
        if x <= 0.54:
            return EyeDirection.LEFT
        if x >= 0.57:
            return EyeDirection.RIGHT
        return EyeDirection.CENTER

    def get_mouth_state(self, mouth_ratio):
        if mouth_ratio > 6.8:
            return MouthState.CLOSE
        else:
            return MouthState.OPEN

    #ランドマークの重心
    def midpoint(self, p1, p2):
        return int((p1.x + p2.x) / 2), int((p1.y + p2.y) / 2)

    #口開閉検知
    def get_mouth_ratio(self, eye_points, facial_landmarks):
        mouth_region = np.array([
            (
                facial_landmarks.part(eye_points[0]).x,
                facial_landmarks.part(eye_points[0]).y
            ),
            (
                facial_landmarks.part(eye_points[1]).x,
                facial_landmarks.part(eye_points[1]).y
            ),
            (
                facial_landmarks.part(eye_points[2]).x,
                facial_landmarks.part(eye_points[2]).y
            ),
            (
                facial_landmarks.part(eye_points[3]).x,
                facial_landmarks.part(eye_points[3]).y
            ),
            (
                facial_landmarks.part(eye_points[4]).x,
                facial_landmarks.part(eye_points[4]).y
            ),
            (
                facial_landmarks.part(eye_points[5]).x,
                facial_landmarks.part(eye_points[5]).y
            )], np.int32)
        left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
        right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
        center_top = self.midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
        center_bottom = self.midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))

        # hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
        # ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)

        hor_line_length = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
        ver_line_length = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))

        if ver_line_length == 0:
            ver_line_length += 0.001

        ratio = hor_line_length / ver_line_length
        return ratio, mouth_region

    # 視線検知
    def get_gaze_right_level(self, eye_points, facial_landmarks, frame, gray):
        left_eye_region = np.array([
            (
                facial_landmarks.part(eye_points[0]).x,
                facial_landmarks.part(eye_points[0]).y
            ),
            (
                facial_landmarks.part(eye_points[1]).x,
                facial_landmarks.part(eye_points[1]).y
            ),
            (
                facial_landmarks.part(eye_points[2]).x,
                facial_landmarks.part(eye_points[2]).y
            ),
            (
                facial_landmarks.part(eye_points[3]).x,
                facial_landmarks.part(eye_points[3]).y
            ),
            (
                facial_landmarks.part(eye_points[4]).x,
                facial_landmarks.part(eye_points[4]).y
            ),
            (
                facial_landmarks.part(eye_points[5]).x,
                facial_landmarks.part(eye_points[5]).y
            )], np.int32)

        height, width, _ = frame.shape
        mask = np.zeros((height, width), np.uint8)
        cv2.polylines(mask, [left_eye_region], True, 255, 2)
        cv2.fillPoly(mask, [left_eye_region], 255)
        eye = cv2.bitwise_and(gray, gray, mask=mask)

        min_x = np.min(left_eye_region[:, 0])
        max_x = np.max(left_eye_region[:, 0])
        min_y = np.min(left_eye_region[:, 1])
        max_y = np.max(left_eye_region[:, 1])

        gray_eye = eye[min_y: max_y, min_x: max_x]
        _, threshold_eye = cv2.threshold(gray_eye, 70, 255, cv2.THRESH_BINARY)
        height, width = threshold_eye.shape
        left_side_threshold = threshold_eye[0: height, int(width / 2): width]
        left_side_white = cv2.countNonZero(left_side_threshold)

        right_side_threshold = threshold_eye[0: height, 0: int(width / 2)]
        right_side_white = cv2.countNonZero(right_side_threshold)

        under_side_threshold = threshold_eye[int(height/2):height, 0:width]
        under_side_white = cv2.countNonZero(under_side_threshold)

        if left_side_white == 0:
            gaze_right_level = 0
        elif right_side_white == 0:
            gaze_right_level = 1
        else:
            gaze_right_level = left_side_white / (right_side_white+left_side_white)

        return gaze_right_level, under_side_white, left_eye_region

    def gaze_preprocess(self, frame, face):

        #ランドマーク
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        landmarks = self.predictor(gray, face)
        return landmarks, gray

    def get_eye_blink_state(self, frame, landmarks, facial_landmarks):
        x = [0 for i in range(len(facial_landmarks))]
        y = [0 for i in range(len(facial_landmarks))]

        for i, facial_landmark in enumerate(facial_landmarks):
            x[i] = landmarks.part(facial_landmark).x
            y[i] = landmarks.part(facial_landmark).y

        trim_val = 2
        frame_trim = frame[y[1]-trim_val:y[3]+trim_val,x[0]:x[2]]
        height, width = frame_trim.shape[0],frame_trim.shape[1]
        frame_trim_resize = cv2.resize(frame_trim , (int(width*7.0), int(height*7.0)))
        # gray scale
        frame_gray = cv2.cvtColor(frame_trim_resize, cv2.COLOR_BGR2GRAY)
        # 平滑化
        frame_gray = cv2.GaussianBlur(frame_gray,(7,7),0)
        # 二値化
        thresh = 80
        maxval = 255
        e_th, frame_black_white = cv2.threshold(frame_gray,thresh,maxval,cv2.THRESH_BINARY_INV)
        eye_contours, _ = cv2.findContours(frame_black_white, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        if len(eye_contours) == 0:
            return EyeState.CLOSE
        # (rx, ry, rw, rh) = cv2.boundingRect(eye_contours[0])
        # cv2.circle(frame_trim_resize, (int(rx+rw/2), int(ry+rh/2)), int((rw+rh)/4), (255, 0, 0), 2) #円で表示
        # cv2.circle(frame, (int(x[0]+(rx+rw)/10), int(y[1]-3+(ry+rh)/10)), int((rw+rh)/20), (0, 255, 0), 1)    #元画像に表示
        return EyeState.OPEN

    def run(self):
        # この関数のwhile True:下でcapから画像を取得して、ジェスチャーを判別、
        # queueに(Gestureオブジェクト、time.time())の形でジェスチャーを入れていってください。
        while True:
            _, frame = self.cap.read()
            self.gaze.refresh(frame)
            frame = self.gaze.annotated_frame()
            face = inference(frame)
            start, end = face
            if start == -1 and end == -1:
                cv2.imshow('frame', frame)
                cv2.waitKey(1)
                continue

            (left, top), (right, bottom) = start, end
            if self.debug:
                cv2.rectangle(frame, start, end, (255, 0, 0), 2)
            dlib_face = dlib.rectangle(left=left, top=top, right=right, bottom=bottom)
            landmarks, gray = self.gaze_preprocess(frame, dlib_face)
            if landmarks == -1 and gray == -1:
                continue

            # まばたきの計測
            left_eye_state, right_eye_state, left_eye_position, right_eye_position = get_eye_state(frame, landmarks)

            #口の開閉度測定
            mouth_landmarks = [60, 61, 63, 64, 65, 67]
            mouth_ratio, mouth_region = self.get_mouth_ratio(mouth_landmarks, landmarks)

            eye_direction = EyeDirection.CENTER
            if self.gaze.is_right():
                eye_direction = EyeDirection.RIGHT
            elif self.gaze.is_left():
                eye_direction = EyeDirection.LEFT
            else:
                eye_direction = EyeDirection.CENTER

            if self.debug:
                def draw_eye(position, state):
                    x, y, x1, y1 = position
                    cv2.rectangle(frame, (x, y), (x1, y1), (255, 0, 0), 2)

                    text = ""
                    if state == EyeState.OPEN:
                        text = "open"
                    else:
                        text = "close"

                    cv2.putText(frame, text, (x, y1), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 5)

                draw_eye(left_eye_position, left_eye_state)
                draw_eye(right_eye_position, right_eye_state)
                cv2.polylines(frame, pts=[mouth_region], isClosed=True, color=(0, 255, 0), thickness=2)
                cv2.imshow("frame", frame)
                key = cv2.waitKey(1)
                if key ==27:
                    break

            if self.queue.full():
                self.queue.queue.clear()

            gestures = Gestures(
                eye_direction=eye_direction,
                left_eye_state=left_eye_state,
                right_eye_state=right_eye_state,
                mouth_state=self.get_mouth_state(mouth_ratio),
            )

            if (
                    gestures.eye_direction != EyeDirection.CENTER or
                    gestures.left_eye_state != gestures.right_eye_state or
                    ((self.previous == None or self.previous.mouth_state == MouthState.CLOSE) and gestures.mouth_state == MouthState.OPEN)
                ):
                self.queue.put((gestures, time.time()))

            self.previous = copy.deepcopy(gestures)

if __name__ == '__main__':
    queue = get_queue()
    FaceGestureDetector(queue).run()


import boto3
import simplejson as json
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
from boto3.dynamodb.types import TypeDeserializer

dynamodb = boto3.client('dynamodb')
table_name = 'Project'

def lambda_handler(event, context):
    try:
        event = json.loads(event['body'])

        project_name = event.get('project_name')
        status = event.get('status')
        description = event.get('description')
        email = event.get('email')
        deadline = event.get('deadline')
        risk_eval = event.get('risk_eval')
        project_id = event.get('project_id')
        team = event.get('team')
        start_date = event.get('start_date')

        # byte_id = (f"{project_name}{email}").encode('utf-8')
        # project_id = (bcrypt.hashpw(byte_id, salt)).decode('utf-8')

        dynamodb.put_item(
            TableName=table_name,
            Item={
                'project_id': {'S': project_id},
                'status': {'N': str(status)},
                'deadline': {'S': deadline},
                'project_name': {'S': project_name},
                'risk_eval': {'N': str(risk_eval)},
                'email': {'S': email},
                'description': {'S': description},
                'team': {'L': [{'S': x} for x in team] },
                'start_date': {'S': start_date}
            }
        )

        return {"code": 200, "message": "Project added successfully"}
    except dynamodb.exceptions.ConditionalCheckFailedException:
        return {"code": 400, "message": f"Project with same name already exists"}
    except Exception as e:
        return {"code": 500, "message": f"An unexpected error occurred while adding project: {e}"}


from datetime import datetime
import re
import urllib.request
import json
import time
import csv
import paramiko
import pods
import getfiles
import sys
import threading
from multiprocessing import Process
from multiprocessing.pool import ThreadPool
import subprocess
import os
import signal
import usbmeter

url_pods = "http://192.168.101.243:30000/api/v1/query?query=kubelet_running_pods{kubernetes_io_hostname='server06'}"
values_pods_t=json.loads(urllib.request.urlopen(url_pods).read())["data"]["result"][0]['value'][1]
DEFAULT_PODS=int(values_pods_t)

PI_IP = "192.168.101.70"
TEST_MODE = False

finished = False
timestamps={}
jobs_status = {
    "warm_done" : False,
    "cold_done" : False,
    "delete_done": False}

def start_master(command:str):

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('192.168.101.243', username='fil', password='1')
    print(command)
    stdin, stdout, stderr = client.exec_command(command)

    for line in stdout:
        print (line.strip('\n'))

    client.close()

def get_prom_pod_time(values:dict, target_pods:int, repetition: int):
    success = False
    url_time = "http://192.168.101.243:30000/api/v1/query?query=kube_pod_status_phase{namespace='capture',job='Kubernetes'}"
    raw = urllib.request.urlopen(url_time).read()
    output = json.loads(raw)

    for pod in output["data"]["result"]:
        try:
            pod_name = pod['metric']['pod']
            if pod_name in values.keys():
                phase = pod['metric']['phase']
                status = pod['value'][1]
                if(str(values[pod_name][0]) == "0" and phase == "Pending" and status == "1"):
                    values[pod_name][0] = pod['value'][0]
                    print("Pending!")
                elif(str(values[pod_name][1]) == "0" and phase == "Running" and status == "1"):
                    values[pod_name][1] = pod['value'][0]
                    values[pod_name][2] = datetime.utcfromtimestamp(float( values[pod_name][1]) - float(values[pod_name][0])).strftime('%H:%M:%S:%f')
                    print("Running!")
                else:
                    dummy = 0
            else:
                # initialize the values
                values[pod_name] = ["0", "0", "0"]
        except Exception as e:
            print(e)
            continue

    if len(values) == int(target_pods) and (not any ("0" in x for x in list(values.values()))):
        writer = csv.writer(open("/home/luongtrann/hanoi/code/data/pod_start_time_{}_{}.csv".format(str(target_pods), str(repetition)), 'w'))
        for key, value in values.items():
            writer.writerow([key, value])
        success = True
    # else:
    #     #print("Condition is not satisfied.")
    #     print(len(values))
    #     print("require: " + str(target_pods))
    #     print(values)
    return success, values
def get_data_from_api(query:str, instance:str):
    url_data = "http://192.168.101.243:30000/api/v1/query?query=" + query
    try:
        contents = urllib.request.urlopen(url_data).read().decode('utf-8')
        values=json.loads(contents)["data"]["result"][0]['value']
    except:
        values = -1
    return values

def get_prometheus_values(target_pods:int, job:str, repetition: int):

    #CPU Usage
    url_cpu = "http://192.168.101.243:30000/api/v1/query?query=100-(avg%20by%20(instance,%20job)%20(irate(node_cpu_seconds_total{mode=%22idle%22,%20job=%22node_exporter_metrics%22,%20instance=%22192.168.101.72:9100%22}[30s])*100))"
    try:
        contents_cpu = urllib.request.urlopen(url_cpu).read().decode('utf-8')
        values_cpu=json.loads(contents_cpu)["data"]["result"][0]['value']
    except:
        values_cpu = "-1"
    #number of pods.
    url_pods = "http://192.168.101.243:30000/api/v1/query?query=kubelet_running_pods{kubernetes_io_hostname='server06'}"
    try:
        contents_pods = urllib.request.urlopen(url_pods).read()
        values_pods=json.loads(contents_pods)["data"]["result"][0]['value']
    except:
        values_pods="-1"
    #memory status
    url_memory = "http://192.168.101.243:30000/api/v1/query?query=(node_memory_MemTotal_bytes{job='node_exporter_metrics',instance='192.168.101.72:9100'}-node_memory_MemAvailable_bytes{job='node_exporter_metrics',instance='192.168.101.72:9100'})/(1024*1024)"
    try:
        contents_memory = urllib.request.urlopen(url_memory).read()
        values_memory=json.loads(contents_memory)["data"]["result"][0]['value']
    except:
        values_memory = "-1"

    #write values to file
    try:
        writer = csv.writer(open("/home/luongtrann/hanoi/code/data/server/data-prometheus_{}_{}_server.csv".format(str(target_pods), str(repetition)), 'a'))
        writer.writerow([datetime.utcfromtimestamp(values_pods[0]).strftime('%Y-%m-%d %H:%M:%S'), values_pods[1], values_cpu[1], values_memory[1], job])
    except:
        print("Error")
    #+2 on target pods for the default pods
    curr_pods = int(values_pods[1])
    if TEST_MODE: print("Current pods: %s, target: %d" % (curr_pods, (int(target_pods)+DEFAULT_PODS)))
    if(curr_pods>=(int(target_pods)+DEFAULT_PODS)):
       jobs_status["cold_done"] = True
       jobs_status["delete_done"] = False

    elif(curr_pods==DEFAULT_PODS):
        jobs_status["delete_done"] = True

def start_measurement_prometheus_time(target_pods: int, repetition: int):
    pod_start_status = False # variable for Kien function
    values = {} # variable for Kien function
    if(int(target_pods) < 5):
        return
    while pod_start_status != True:
        pod_start_status, values = get_prom_pod_time(values, target_pods, repetition)
        time.sleep(0.2)
    return

def start_measurement_prometheus(minutes: int, target_pods: int, repetition: int):
    '''
    minutes: defines the time to measure the warm scenario
    '''
    print("Scenario: COLD - Started...")
    timestamps["cold_start"]= time.time()
    while not jobs_status["cold_done"]:
        get_prometheus_values(target_pods, "cold", repetition)
        time.sleep(1)
    timestamps["cold_end"]= time.time()

    time.sleep(15)

    print("Scenario: WARM - Started...")
    timestamps["warm_start"]= time.time()
    warm_endtime = time.time()+(60*int(minutes)) #5min x 60
    while warm_endtime-time.time() > 0:
        get_prometheus_values(target_pods, "warm", repetition)
        time.sleep(1)
    jobs_status["warm_done"] = True
    timestamps["warm_end"]= time.time()

    #time.sleep(30) # there is no point to sleep here

    print("Scenario: DELETE - Started...")
    timestamps["delete_start"]= time.time()
    pods.delete_pods()
    while not jobs_status["delete_done"]:
        get_prometheus_values(target_pods, "delete", repetition)
        time.sleep(1)
        if jobs_status["delete_done"]:
            timestamps["pods_deleted"]= time.time()
            for i in range(0, 5, 1):
                get_prometheus_values(target_pods, "delete_after", repetition)
                time.sleep(1)
            timestamps["pods_deleted_after"]= time.time()

    #time.sleep(30) # there is no point to sleep here

    print("Measurement finished.")
    print("Saving timestamps..")
    timestamps_to_file(target_pods, repetition)
    print("Done")
    global finished
    finished = True
    return

def start_ummeter(target_pods: int, rep: int, time:int):
    cmd = '/usr/bin/python3 /home/luongtrann/usbmeter/usbmeter --addr 00:16:A5:00:0F:65 --out /home/luongtrann/hanoi/code/data_ummeter/data_ummeter_{}_{} --time {}'.format(target_pods, rep, time)
    s = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

def timestamps_to_file(target_pods:int, repetition:int):
    with open('/home/luongtrann/hanoi/code/data/server/timestamps_{}_{}_server.csv'.format(str(target_pods), str(repetition)), 'w') as f:
        for key in timestamps.keys():
            f.write("%s,%s\n"%(key,timestamps[key]))

if __name__ == "__main__":

    """
    call: python3 main.py [COMMAND] [TARGET_PODS] [MINUTES_WARM]
    """
    target_pods = sys.argv[2]
    minutes = sys.argv[3]
    rep = sys.argv[4]
    if sys.argv[1] == "master":
        #Update replicas
        cmd = '/usr/bin/python3 /home/fil/filip/hanoi/code/main.py changevalue {} {} {}'.format(str(target_pods), str(minutes), str(rep))
        start_master(cmd)
        #p3=Process(target=start_measurement_prometheus_time, args=(target_pods, rep, ), daemon = True)
        p2=Process(target=start_measurement_prometheus, args=(minutes, target_pods, rep, ), daemon = True)
        p1=Process(target=usbmeter.main, args=(target_pods, rep, minutes, ), daemon = True)
        #p3.start()
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        #p3.join()
        print("Every process is done.")
    elif sys.argv[1] == "changevalue":
        usbmeter.main(target_pods, rep, minutes, )

    else: print("Not recognized command")


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

# Import externe
import yaml
import sqlite3 as sql

DATA_DATABASE_LOCATION = "res/game_data.db"
SAVE_DATABASE_LOCATION = "sav/save.db"

CONFIGURATION_FILES = {
    "player" : "sav/player.yaml",
    "window" : "stg/window.yaml",
    "controls" : "stg/controls.yaml"
    }

DEFAULT_CONFIG = {
    "player" : {"map_id" : 1,
                "position" : [1600,1200],
                "speed" : 1.5,
                "cash" : 10.},
    "window" : {"size" : (1000, 600),
                "fullscreen" : False},
    "controls" : {
        # Contrôle du mouvement
        "PLAYER_MOVE_UP" : "UP",
        "PLAYER_MOVE_DOWN" : "DOWN",
        "PLAYER_MOVE_LEFT" : "LEFT",
        "PLAYER_MOVE_RIGHT" : "RIGHT",
        "PLAYER_SPRINT" : "LEFT CTRL",
        "PLAYER_SPRINT_SEC" : "RIGHT CTRL",

        # Contrôle des mécanismes du jeu
        "ACTION_INTERACT" : "SPACE",

        # Contrôle des menus
        "MENU_SHOW_SIDEBAR" : "LEFT SHIFT",
        "MENU_MOVE_UP" : "UP",
        "MENU_MOVE_DOWN" : "DOWN",
        "MENU_MOVE_LEFT" : "LEFT",
        "MENU_MOVE_RIGHT" : "RIGHT",
        "MENU_INTERACT" : "SPACE",
        "MENU_CANCEL" : "x",

        # Contrôle de la fenêtre
        "GAME_FULLSCREEN" : "F11",
        "GAME_TERMINATE" : "ESCAPE",

        # Debug du jeu
        "DEBUG" : "F3",
        "RESET_CONFIG" : "F5",
        "RESET_SAVE" : "F6"
        }
    }

def load_config(object):
    """Charge une configuration YAML"""
    try:
        with open(CONFIGURATION_FILES[object], 'r') as file:
            config = yaml.load(file, Loader = yaml.FullLoader)
        return config
    except FileNotFoundError: # Cas où le fichier n'existe pas
        create_default_config(object)
        return load_config(object)

def create_default_config(object):
    """Créé le fichier avec une configuration par défaut"""
    config = DEFAULT_CONFIG[object]
    with open(CONFIGURATION_FILES[object], 'w') as file:
        yaml.dump(config, file) #Ecriture du fichier de config

def save_config(object, **args):
    """Sauvegarde en modifiant les données passées en paramètres"""
    try:
        config = load_config(object)
    except FileNotFoundError:
        print("Impossible d'accéder au fichier de {} lors de la sauvegarde. Cela peut être dû à une réinitialisation des données...".format(object))
        return
    for arg in args.items():
        config[arg[0]] = arg[1]
    with open(CONFIGURATION_FILES[object], 'w') as file:
        yaml.dump(config, file) #Ecriture du fichier de config

def init_save_database():
    """Création du fichier de sauvegarde"""
    data_db = sql.connect(DATA_DATABASE_LOCATION)
    map_list = data_db.cursor().execute('select id from maps;').fetchall()
    npc_list = data_db.cursor().execute('select * from npc;').fetchall()
    mapobj_list = data_db.cursor().execute('select id from objects;').fetchall()
    save_db = sql.connect(SAVE_DATABASE_LOCATION)
    save_db.cursor().execute('CREATE TABLE IF NOT EXISTS "bag" ("id_item" INTEGER NOT NULL PRIMARY KEY, "quantity" INTEGER NOT NULL)')
    save_db.cursor().execute('create table if not exists "maps" ("map_id" integer not null primary key,\
                                                                 "mapscript_triggered" integer not null,\
                                                                 "flags" text);')
    save_db.cursor().execute('create table if not exists "npc" ("npc_id" integer not null primary key,\
                                                                "flags" text);')
    save_db.cursor().execute('create table if not exists "mapobjects" ("mapobj_id" integer not null primary key,\
                                                                       "obtained" integer);')
    save_db.cursor().execute('create table if not exists "events" ("tag" text not null primary key,\
                                                                   "state" integer);')

    # Création de la sauvegarde des maps
    if save_db.cursor().execute('select * from maps;').fetchall() == []:
        for map in map_list:
            save_db.cursor().execute('insert into maps values (?, 0, "[0,0,0,0]");', (map[0],)) # Temporaire : 4 flags par map
                                                                                # Proposition : passer en binaire (ordre des flags inversé, l'état "flags 3 et 1 levés" est alors symbolisé par l'entier 10)
    # Création de la sauvegarde des PNJ
    if save_db.cursor().execute('select * from npc;').fetchall() == []:
        for npc in npc_list:
            save_db.cursor().execute('insert into npc values (?, "[0,0,0,0]");', (npc[0],))     # Même proposition
    # Création de la sauvegarde des objets sur la carte
    if save_db.cursor().execute('select * from mapobjects;').fetchall() == []:
        for mapobj in mapobj_list:
            save_db.cursor().execute('insert into mapobjects values (?, 0);', (mapobj[0],))
    save_db.commit()
    data_db.close()
    return save_db


"""Tests `phydmslib.models.GammaDistributedBetaModel` class.

Written by Jesse Bloom and Sarah Hilton.
"""

import random
import unittest
import numpy
import scipy.optimize
from phydmslib.constants import N_AA, N_NT, AA_TO_INDEX
import phydmslib.models

class test_GammaBeta_ExpCM(unittest.TestCase):
    """Test gamma distributed beta for `ExpCM`."""

    # use approach here to run multiple tests:
    # http://stackoverflow.com/questions/17260469/instantiate-python-unittest-testcase-with-arguments
    BASEMODEL = phydmslib.models.ExpCM

    def test_GammaDistributedBeta(self):
        """Initialize, test values, update, test again."""
        random.seed(1)
        numpy.random.seed(1)
        nsites = 10

        # create preference set
        prefs = []
        minpref = 0.01
        for _r in range(nsites):
            rprefs = numpy.random.dirichlet([0.5] * N_AA)
            rprefs[rprefs < minpref] = minpref
            rprefs /= rprefs.sum()
            prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        if self.BASEMODEL == phydmslib.models.ExpCM:
            paramvalues = {"eta": numpy.random.dirichlet([5] * (N_NT - 1)),
                           "omega": 0.7,
                           "kappa": 2.5,
                           "beta": 1.2,
                           "mu": 0.5}
            basemodel = self.BASEMODEL(prefs)
            assert set(paramvalues.keys()) == set(
                basemodel.freeparams
            ), "{0} vs {1}".format(set(paramvalues.keys()),
                                   set(basemodel.freeparams))
            basemodel.updateParams(paramvalues)
        elif self.BASEMODEL == phydmslib.models.ExpCM_empirical_phi:
            g = numpy.random.dirichlet([3] * N_NT)
            paramvalues = {"omega": 0.7,
                           "kappa": 2.5,
                           "beta": 1.2,
                           "mu": 0.5}
            basemodel = self.BASEMODEL(prefs, g)
            assert set(paramvalues.keys()) == set(
                basemodel.freeparams
            ), "{0} vs {1}".format(set(paramvalues.keys()),
                                   set(basemodel.freeparams))
            basemodel.updateParams(paramvalues)
        else:
            raise ValueError("Invalid BASEMODEL: {0}".format(self.BASEMODEL))
            rprefs /= rprefs.sum()
            prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        ncats = 4
        gammamodel = phydmslib.models.GammaDistributedBetaModel(basemodel,
                                                                ncats)
        self.assertTrue(
            numpy.allclose(
                numpy.array([m.beta for m in gammamodel._models]),
                phydmslib.models.DiscreteGamma(
                    gammamodel.alpha_lambda,
                    gammamodel.beta_lambda,
                    gammamodel.ncats)))
        for (param, pvalue) in paramvalues.items():
            if param != gammamodel.distributedparam:
                self.assertTrue(numpy.allclose(getattr(gammamodel, param),
                                               pvalue))

        # try some updates and make sure everything remains OK
        for i in range(3):
            newvalues = {}
            for param in gammamodel.freeparams:
                (low, high) = gammamodel.PARAMLIMITS[param]
                if gammamodel.PARAMTYPES[param] == float:
                    newvalues[param] = random.uniform(low, high)
                else:
                    paramlength = gammamodel.PARAMTYPES[param][1]
                    newvalues[param] = numpy.random.uniform(low, high,
                                                            paramlength)
            gammamodel.updateParams(newvalues)
            self.assertTrue(
                numpy.allclose(
                    numpy.array([m.beta for m in gammamodel._models]),
                    phydmslib.models.DiscreteGamma(
                        gammamodel.alpha_lambda,
                        gammamodel.beta_lambda,
                        gammamodel.ncats)))
            for (param, pvalue) in newvalues.items():
                if param != gammamodel.distributedparam:
                    self.assertTrue(numpy.allclose(pvalue,
                                                   getattr(gammamodel, param)))
                    if param not in gammamodel.distributionparams:
                        self.assertTrue(
                            all((numpy.allclose(pvalue, getattr(m, param))
                                 for m in gammamodel._models)))

            # This is the opposite test of gammaomega
            self.assertTrue(
                gammamodel._models[0].branchScale
                > gammamodel.branchScale
                > gammamodel._models[-1].branchScale)

            t = 0.15
            for k in range(gammamodel.ncats):
                M = gammamodel.M(k, t)
                self.assertTrue(numpy.allclose(gammamodel._models[k].M(t), M))
                for param in gammamodel.freeparams:
                    if param not in gammamodel.distributionparams:
                        dM = gammamodel.dM(k, t, param, M)
                        self.assertTrue(
                            numpy.allclose(
                                dM,
                                gammamodel._models[k].dM(t, param, Mt=None)))

            # Check derivatives with respect to distribution params
            d_distparams = gammamodel.d_distributionparams
            self.assertTrue((d_distparams["alpha_lambda"] > 0).all())
            self.assertTrue((d_distparams["beta_lambda"] < 0).all())
            for param in gammamodel.distributionparams:
                diffs = []
                for k in range(gammamodel.ncats):
                    pvalue = getattr(gammamodel, param)

                    def func(x):
                        gammamodel.updateParams({param: x[0]})
                        return getattr(gammamodel._models[k],
                                       gammamodel.distributedparam)

                    def dfunc(x):
                        gammamodel.updateParams({param: x[0]})
                        return gammamodel.d_distributionparams[param][k]

                    diff = scipy.optimize.check_grad(func, dfunc,
                                                     numpy.array([pvalue]))
                    gammamodel.updateParams({param: pvalue})
                    diffs.append(diff)
                diffs = numpy.array(diffs)
                self.assertTrue(
                    (diffs < 1e-5).all(),
                    ("Excessive diff for d_distributionparams[{0}] when "
                     "distributionparams = {1}:\n{2}".format(
                            param, gammamodel.distributionparams, diffs)))

            # Check the stationary state & deriviative of the stationary state
            # Stationary states should be different for each `k`
            self.assertFalse(
                all((numpy.allclose(gammamodel.stationarystate(i),
                                    gammamodel.stationarystate(j))
                    for i in range(gammamodel.ncats)
                    for j in range(i + 1, gammamodel.ncats))))
            # The derviative of the stationary states should be different with
            # respect to beta for each `k`
            self.assertFalse(
                all(
                    (numpy.allclose(gammamodel.dstationarystate(i, "beta"),
                                    gammamodel.dstationarystate(j, "beta"))
                     for i in range(gammamodel.ncats)
                     for j in range(i + 1, gammamodel.ncats))))

class test_GammaBeta_ExpCM_empirical_phi(test_GammaBeta_ExpCM):
    """Test gamma distributed beta for `ExpCM_empirical_phi`."""

    # use approach here to run multiple tests:
    # http://stackoverflow.com/questions/17260469/instantiate-python-unittest-testcase-with-arguments
    BASEMODEL = phydmslib.models.ExpCM_empirical_phi

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    unittest.main(testRunner=runner)


import asyncio

import pandas as pd
from stockpredict.fetcher.api_client import AbstractApi

from stockpredict.settings import BALANCE_CASH_INCOME_DROP, CASH_ADDITIONAL_DROP, BALANCE_CASH_INCOME_JOIN_KEYS, \
    INFERENCE_DROP, DTYPE
from stockpredict.util.helpers import async_resolver

class DataFetcher:
    def __init__(self, client: AbstractApi):
        self.client = client

    def get_data(self, marker: str, period: str = "annual") -> pd.DataFrame:
        task = (asyncio.gather(self.client.get_balance_sheet(marker=marker, period=period, limit=1),
                               self.client.get_cash_flow(marker=marker, period=period, limit=1),
                               self.client.get_income_statement(marker=marker, period=period, limit=1)
                               ))
        balance, cash, income = async_resolver(task)
        balance_df = pd.DataFrame.from_dict(balance)
        cash_flow_df = pd.DataFrame.from_dict(cash)
        income_df = pd.DataFrame.from_dict(income)

        return self.preprocess(balance_df, cash_flow_df, income_df)

    def preprocess(self, balance_sheet: pd.DataFrame, cash_flow: pd.DataFrame, income: pd.DataFrame) -> pd.DataFrame:
        balance_sheet.drop(columns=BALANCE_CASH_INCOME_DROP, inplace=True)
        cash_flow.drop(columns=[*BALANCE_CASH_INCOME_DROP, *CASH_ADDITIONAL_DROP], inplace=True)
        income.drop(columns=BALANCE_CASH_INCOME_DROP, inplace=True)

        balance_sheet.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)
        cash_flow.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)
        income.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)

        df = balance_sheet.join(cash_flow).join(income)
        df.reset_index(inplace=True)
        df.drop(columns=INFERENCE_DROP, inplace=True)
        df.fillna(value=0, inplace=True)
        df = df.astype(DTYPE)

        return df


from datetime import datetime
from database_connections.database_manager import users, remembers

class user_utilities:
    @staticmethod
    def verify_if_user_exist(user_id):
        user = users.find({ "user_id":user_id})

        user = [x for x in user]

        # If it doesn't find any user, the array will be len -> 0
        if len(user) == 0:
            return False
        else:
            return True

    @staticmethod
    def create_user(user_id, language, timezone):
        user = {'user_id': user_id, 'language': language, 'timezone':timezone}
        users.insert_one(user)
        return

    @staticmethod
    def user_language(user_id):
        user = users.find({"user_id":user_id})
        return user[0]['language']

    @staticmethod
    def user_timezone(user_id):
        user = users.find({ "user_id":user_id})
        return user[0]['timezone']

    @staticmethod
    def get_remember_list(user_id):
        remembers_list = [remember for remember in remembers.find({"user_id":user_id})]

        return remembers_list

    @staticmethod
    def get_remember_list_only_pending(user_id):
        remembers_list = [remember for remember in remembers.find({"user_id":user_id, "expire_date": {"$gte":datetime.now()}})]

        return remembers_list

    @staticmethod
    def change_user_language(user_id, language):

        user_query = { "user_id":user_id}
        new_user = users.find({ "user_id":user_id})[0]
        new_user['language'] = language

        users.replace_one(user_query, new_user )
        return


import time
import random

from tqdm import tqdm

from evaluate_mcts_strategy import sample_a_defect, sample_initial_state
from base import get_cost, check_useful_action
from simulations import simulate_one_step_for_defect

# noinspection DuplicatedCode
def pick_best_available_action(statistics, state):
    avail_actions = statistics["all_actions"]
    useful_actions = []
    for action in avail_actions:
        if check_useful_action(statistics, state, action):
            useful_actions.append(action)

    if len(useful_actions) == 0:
        return 0

    i = random.randrange(len(useful_actions))
    return useful_actions[i]

def no_possible_successors(statistics, state):
    for action in statistics["all_actions"]:
        if check_useful_action(statistics, state, action):
            return False
    return True

def simulate_a_path(statistics, defect):
    state = sample_initial_state(statistics, defect)
    acc_cost = 0
    while not no_possible_successors(statistics, state):
        action = pick_best_available_action(statistics, state)
        state = simulate_one_step_for_defect(statistics, state, action, defect)
        acc_cost += get_cost(statistics, action)
    return acc_cost

# noinspection DuplicatedCode
def evaluate_naive(statistics):
    max_num_simulations = 10000
    total_cost = 0
    defects = []
    print("\nStarting evaluation of naive strategy...")
    time.sleep(0.1)
    for _ in tqdm(range(max_num_simulations)):
        defect = sample_a_defect(statistics)
        defects.append(defect)
        cost = simulate_a_path(statistics, defect)
        total_cost += cost
    print("done")
    print("Average cost for", max_num_simulations, "faults:", total_cost / max_num_simulations)


# coding: utf-8

import numpy as np
import keras

class DySpeechGen(keras.utils.Sequence):
    def __init__(self, file_IDs, labels, batch_size=64, dim=16000, shuffle=False):
        self.Files = file_IDs
        self.labels = labels
        self.dim = dim
        self.batchSize = batch_size
        self.shuffle =shuffle
        self.on_epoch_end()

    def __len__(self):
        return int(np.floor(len(self.Files)/self.batchSize))

    def load_all_data(self):
        indexes = self.indexes[:]
        # Find list of IDs
        list_IDs_temp = [self.Files[k] for k in indexes]
        #print("list_IDs_temp : ", list_IDs_temp)
        # Generate data
        X, y = self.__gen_part_training_data(list_IDs_temp)

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index*self.batchSize:(index+1)*self.batchSize]

        # Find list of IDs
        list_IDs_temp = [self.Files[k] for k in indexes]
        print("list_IDs_temp : ", list_IDs_temp)
        # Generate data
        X, y = self.__data_generation(list_IDs_temp)

        return X, y

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.allFilesLen = len(self.Files)
        self.indexes = np.arange(self.allFilesLen)
        #print("self.indexes is {}.",len(self.Files))

    def __data_generation(self,list_of_files):

        X = np.empty((self.batchSize,self.dim)) #64 files, each file is 16000 long
        y = np.empty((self.batchSize),dtype=int)

        #Start to generate the training data
        for i, _f in enumerate(list_of_files):
            print("current _f is : ",_f)
            #load npy file
            curX = np.load(_f)

            #check equal,smaller, or bigger
            #and truncate or padding
            #curX could be bigger or smaller than self.dim
            if curX.shape[0] == self.dim:
                X[i] = curX
                #print('Same dim')
            elif curX.shape[0] > self.dim: #bigger
                #we can choose any position in curX-self.dim
                randPos = np.random.randint(curX.shape[0]-self.dim)
                X[i] = curX[randPos:randPos+self.dim]
                #print('File dim bigger')
            else: #smaller
                randPos = np.random.randint(self.dim-curX.shape[0])
                X[i,randPos:randPos+curX.shape[0]] = curX
                #print('File dim smaller')
        # Store class
            y[i] = self.labels[_f]

        return X, y


import csv
import argparse

def filter_csv(input_csv, output_csv, lat_min, lat_max, lon_min, lon_max):
    with open(input_csv, 'r') as infile, open(output_csv, 'w', newline='') as outfile:
        csvreader = csv.reader(infile)
        csvwriter = csv.writer(outfile)

        header = next(csvreader)
        csvwriter.writerow(header)

        for row in csvreader:
            node_id, lat, lon = row
            lat, lon = float(lat), float(lon)

            if lat_min <= lat <= lat_max and lon_min <= lon <= lon_max:
                csvwriter.writerow(row)

def main(args):
    filter_csv(
        args.input_csv,
        args.output_csv,
        args.lat_min,
        args.lat_max,
        args.lon_min,
        args.lon_max
    )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Filter a CSV file of geographic nodes by bounding box.")

    parser.add_argument('--input_csv', default='nodes.csv', type=str, help="Path to the input CSV file.")
    parser.add_argument('--output_csv', default='filtered_nodes.csv', type=str, help="Path to the output CSV file.")

    parser.add_argument('--lat_min', type=float, default=37.6, help="Minimum latitude.")
    parser.add_argument('--lat_max', type=float, default=37.9, help="Maximum latitude.")
    parser.add_argument('--lon_min', type=float, default=-123.1, help="Minimum longitude.")
    parser.add_argument('--lon_max', type=float, default=-122.3, help="Maximum longitude.")

    args = parser.parse_args()
    main(args)


import numpy as np
import cv2
import cvui
import random

WINDOW1_NAME = 'Window 1'
WINDOW2_NAME = 'Windows 2'

def main():
	# We have one mat for each window.
	frame1 = np.zeros((600, 800, 3), np.uint8)
	frame2 = np.zeros((600, 800, 3), np.uint8)

	# Create variables used by some components
	window1_values = []
	window2_values = []
	window1_checked = [False]
	window1_checked2 = [False]
	window2_checked = [False]
	window2_checked2 = [False]
	window1_value = [1.0]
	window1_value2 = [1.0]
	window1_value3 = [1.0]
	window2_value = [1.0]
	window2_value2 = [1.0]
	window2_value3 = [1.0]

	img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR)
	imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR)
	imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR)

	padding = 10

	# Fill the vector with a few random values
	for i in range(0, 20):
		window1_values.append(random.uniform(0., 300.0))
		window2_values.append(random.uniform(0., 300.0))

	# Start two OpenCV windows
	cv2.namedWindow(WINDOW1_NAME)
	cv2.namedWindow(WINDOW2_NAME)

	# Init cvui and inform it to use the first window as the default one.
	# cvui.init() will automatically watch the informed window.
	cvui.init(WINDOW1_NAME)

	# Tell cvui to keep track of mouse events in window2 as well.
	cvui.watch(WINDOW2_NAME)

	while (True):
		# Inform cvui that all subsequent component calls and events are related to window 1.
		cvui.context(WINDOW1_NAME)

		# Fill the frame with a nice color
		frame1[:] = (49, 52, 49)

		cvui.beginRow(frame1, 10, 20, 100, 50)
		cvui.text('This is ')
		cvui.printf('a row')
		cvui.checkbox('checkbox', window1_checked)
		cvui.window(80, 80, 'window')
		cvui.rect(50, 50, 0x00ff00, 0xff0000)
		cvui.sparkline(window1_values, 50, 50)
		cvui.counter(window1_value)
		cvui.button(100, 30, 'Fixed')
		cvui.image(img)
		cvui.button(img, imgGray, imgRed)
		cvui.endRow()

		padding = 50
		cvui.beginRow(frame1, 10, 150, 100, 50, padding)
		cvui.text('This is ')
		cvui.printf('another row')
		cvui.checkbox('checkbox', window1_checked2)
		cvui.window(80, 80, 'window')
		cvui.button(100, 30, 'Fixed')
		cvui.printf('with 50px paddin7hg.')
		cvui.endRow()

		cvui.beginRow(frame1, 10, 250, 100, 50)
		cvui.text('This is ')
		cvui.printf('another row with a trackbar ')
		cvui.trackbar(150, window1_value2, 0., 5.)
		cvui.printf(' and a button ')
		cvui.button(100, 30, 'button')
		cvui.endRow()

		cvui.beginColumn(frame1, 50, 330, 100, 200)
		cvui.text('Column 1 (no padding)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.text('End of column 1')
		cvui.endColumn()

		padding = 10
		cvui.beginColumn(frame1, 300, 330, 100, 200, padding)
		cvui.text('Column 2 (padding = 10)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.trackbar(150, window1_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25)
		cvui.text('End of column 2')
		cvui.endColumn()

		cvui.beginColumn(frame1, 550, 330, 100, 200)
		cvui.text('Column 3 (use space)')
		cvui.space(5)
		cvui.button('button1 5px below')
		cvui.space(50)
		cvui.text('Text 50px below')
		cvui.space(20)
		cvui.button('Button 20px below')
		cvui.space(40)
		cvui.text('End of column 2 (40px below)')
		cvui.endColumn()

		# Update all components of window1, e.g. mouse clicks, and show it.
		cvui.update(WINDOW1_NAME)
		cv2.imshow(WINDOW1_NAME, frame1)

		# From this point on, we are going to render the second window. We need to inform cvui
		# that all updates and components from now on are connected to window 2.
		# We do that by calling cvui.context().
		cvui.context(WINDOW2_NAME)
		frame2[:] = (49, 52, 49)

		cvui.beginRow(frame2, 10, 20, 100, 50)
		cvui.text('This is ')
		cvui.printf('a row')
		cvui.checkbox('checkbox', window2_checked)
		cvui.window(80, 80, 'window')
		cvui.rect(50, 50, 0x00ff00, 0xff0000)
		cvui.sparkline(window2_values, 50, 50)
		cvui.counter(window2_value)
		cvui.button(100, 30, 'Fixed')
		cvui.image(img)
		cvui.button(img, imgGray, imgRed)
		cvui.endRow()

		padding = 50
		cvui.beginRow(frame2, 10, 150, 100, 50, padding)
		cvui.text('This is ')
		cvui.printf('another row')
		cvui.checkbox('checkbox', window2_checked2)
		cvui.window(80, 80, 'window')
		cvui.button(100, 30, 'Fixed')
		cvui.printf('with 50px paddin7hg.')
		cvui.endRow()

		# Another row mixing several components
		cvui.beginRow(frame2, 10, 250, 100, 50)
		cvui.text('This is ')
		cvui.printf('another row with a trackbar ')
		cvui.trackbar(150, window2_value2, 0., 5.)
		cvui.printf(' and a button ')
		cvui.button(100, 30, 'button')
		cvui.endRow()

		cvui.beginColumn(frame2, 50, 330, 100, 200)
		cvui.text('Column 1 (no padding)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.text('End of column 1')
		cvui.endColumn()

		padding = 10
		cvui.beginColumn(frame2, 300, 330, 100, 200, padding)
		cvui.text('Column 2 (padding = 10)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.trackbar(150, window2_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25)
		cvui.text('End of column 2')
		cvui.endColumn()

		cvui.beginColumn(frame2, 550, 330, 100, 200)
		cvui.text('Column 3 (use space)')
		cvui.space(5)
		cvui.button('button1 5px below')
		cvui.space(50)
		cvui.text('Text 50px below')
		cvui.space(20)
		cvui.button('Button 20px below')
		cvui.space(40)
		cvui.text('End of column 2 (40px below)')
		cvui.endColumn()

		# Update all components of window2, e.g. mouse clicks, and show it.
		cvui.update(WINDOW2_NAME)
		cv2.imshow(WINDOW2_NAME, frame2)

		# Check if ESC key was pressed
		if cv2.waitKey(20) == 27:
			break

if __name__ == '__main__':
	main()


import itertools
import sys
import time
from collections import defaultdict
from collections.abc import Sequence
from typing import Any, TypedDict

import gymnasium as gym
import numpy as np
import numpy.typing as npt
from gymnasium import spaces
from solvers.base import Solver
from utils import Seed

from .graph import SamplingMethod, SATGraph

class G2SATObservation(TypedDict):
    graph: spaces.GraphInstance
    valid_actions: list[tuple[int, int]]
    total_valid_actions: int

class G2SATEnv(gym.Env[dict, npt.NDArray[np.integer]]):
    def __init__(
        self,
        num_vars: int,
        num_clauses: int,
        solver: Solver,
        reward_metric: str,
        num_sampled_pairs: int = 2_000,
        compress_observations: bool = True,
        intermediate_rewards: bool = False,
        intermediate_rewards_coeff: float = 0.1,
        sampling_method: SamplingMethod = "g2sat",
        allow_overlaps: bool = False,
        fixed_templates: Sequence[npt.NDArray[np.int64] | list[list[int]]]
        | None = None,
        solve_repetitions: int = 1,
        solve_agg: str = "mean",
        reference_instances: list[list[list[int]]] | None = None,
        normalize_by_reference: bool = False,
    ) -> None:
        self.num_vars = num_vars
        self.num_clauses = num_clauses
        self.solver = solver
        self.reward_metric = reward_metric
        self.num_sampled_pairs = num_sampled_pairs
        self.compress_observations = compress_observations
        self.intermediate_rewards = intermediate_rewards
        self.intermediate_rewards_coeff = intermediate_rewards_coeff
        self.sampling_method: SamplingMethod = sampling_method
        self.allow_overlaps = allow_overlaps
        self.solve_repetitions = solve_repetitions
        self.solve_agg = solve_agg
        self.reference_instances = reference_instances
        self.normalize_by_reference = normalize_by_reference

        self.fixed_templates = fixed_templates
        if fixed_templates is None:
            self.fixed_templates_iter = None
        else:
            self.fixed_templates_iter = iter(itertools.cycle(fixed_templates))

        max_num_nodes = self._compute_max_node_count()
        self.observation_space = spaces.Dict(
            {
                "graph": spaces.Graph(
                    node_space=spaces.Discrete(2),
                    edge_space=None,
                ),
                "valid_pairs": spaces.Sequence(
                    spaces.MultiDiscrete([max_num_nodes, max_num_nodes]),
                ),
            }
        )
        self.action_space = spaces.MultiDiscrete([max_num_nodes, max_num_nodes])

    def _compute_max_node_count(self) -> int:
        max_num_nodes = self.num_vars * 2 + self.num_clauses * 3
        if self.fixed_templates is None:
            return max_num_nodes

        for template in self.fixed_templates:
            if isinstance(template, list):
                num_vars = max(
                    abs(literal) for clause in template for literal in clause
                )
                num_literals = 2 * num_vars
                num_clauses = sum(len(clause) for clause in template)
            else:
                num_literals = len(template)
                num_clauses = template.sum()
            num_nodes = num_literals + num_clauses
            max_num_nodes = max(max_num_nodes, num_nodes)
        return max_num_nodes

    def step(
        self,
        action: npt.NDArray[np.integer] | tuple[int, int],
    ) -> tuple[G2SATObservation, float, bool, bool, dict[str, Any]]:
        t0 = time.monotonic()
        clause1, clause2 = action

        if tuple(action) not in self.valid_actions:
            msg = f"Invalid action: {action}"
            raise ValueError(msg)

        self.graph.merge(clause1, clause2)

        obs, sample_time = self.get_obs()
        self.valid_actions = obs["valid_actions"]

        is_3sat = self.graph.is_3sat()
        terminated = is_3sat or len(obs["valid_actions"]) == 0
        truncated = False

        if terminated or self.intermediate_rewards:
            metrics = self._get_metrics()
            reward = metrics[self.reward_metric]
            if not terminated:
                reward *= self.intermediate_rewards_coeff
        else:
            metrics = {}
            reward = 0

        t1 = time.monotonic()
        info = {
            "is_3sat": is_3sat,
            "metrics": metrics,
            "timing": {
                "step": t1 - t0,
                "sample_pairs": sample_time,
            },
        }

        return obs, reward, terminated, truncated, info

    def _solve_instance(self, instance: list[list[int]]) -> dict[str, Any]:
        return self.solver.solve_instance_agg(
            instance,
            repetitions=self.solve_repetitions,
            agg_fn=self.solve_agg,
        )

    def _get_metrics(self) -> dict[str, Any]:
        metrics = self._solve_instance(self.graph.to_clauses())
        if self.reference_instances:
            ref_lists = defaultdict(list)
            for instance in self.reference_instances:
                ref = self._solve_instance(instance)
                for k, v_ref in ref.items():
                    ref_lists[k].append(v_ref)
            fn = getattr(np, self.solve_agg)
            for k, v_refs in ref_lists.items():
                metrics[f"ref/{k}"] = fn(v_refs)
                if self.normalize_by_reference:
                    # Add epsilon to avoid divide-by-zero warnings
                    metrics[k] /= metrics[f"ref/{k}"] + sys.float_info.epsilon
        return metrics

    def reset(
        self,
        template: npt.NDArray[np.int64] | list[list[int]] | None = None,
        seed: Seed = None,
    ) -> tuple[G2SATObservation, dict[str, Any]]:
        rng = np.random.default_rng(seed)
        if template is not None:
            # ok, already have template
            pass
        elif self.fixed_templates_iter is not None:
            # get next fixed template
            template = next(self.fixed_templates_iter)
        else:
            # sample a new template
            template = SATGraph.sample_template(
                self.num_vars, self.num_clauses * 3, seed=rng
            )

        if isinstance(template, list):
            self.graph = SATGraph.from_clauses(
                template,
                sampling_method=self.sampling_method,
                allow_overlaps=self.allow_overlaps,
                seed=rng,
            )
        else:
            self.graph = SATGraph.from_template(
                template,
                sampling_method=self.sampling_method,
                allow_overlaps=self.allow_overlaps,
                seed=rng,
            )
        obs, sample_time = self.get_obs()
        self.valid_actions = obs["valid_actions"]

        info = {
            "template": template,
            "timing": {
                "sample_pairs": sample_time,
            },
        }
        return obs, info

    def render(self) -> None:
        self.graph.plot_nx()

    def get_obs(self) -> tuple[G2SATObservation, float]:
        t0 = time.monotonic()
        valid, count = self.graph.sample_valid_merges_with_count(self.num_sampled_pairs)
        t1 = time.monotonic()
        return {
            "graph": self.graph.to_graph_instance(self.compress_observations),
            "valid_actions": valid,
            "total_valid_actions": count,
        }, t1 - t0


import cv2
from ImageSketcher import ImageSketcher
import numpy as np

def main():
    print("\nWelcome to the Take-On-Me Multi-Input-Converter\nYou can choose as your input to be either a:")
    print("i (for Image)\nv (for Live Webcam)\n")

    response = input("First, Select an Input type:")

    if response.lower() in ["i","image","pic","picture"]:
        print("\nNext, you'll need to put the image file in same the folder with everything else")

        #makes sure that the image file exists
        while True:
            name = input("Enter the file name of your image (include file extension like .png,.jpg):")
            img = cv2.imread(name)
            if img is not None:
                break

        response2 = input("\nYour image is now ready to process\nDo you want to resize your ouput? y/n :")

        if response2.lower() in ["y","yes"]:
            OUT_Height = int(input("\nFirst, Enter the height of your new image:"))
            OUT_Width = int(input("\nNext, Enter the width:"))

            img_resized = cv2.resize(img,(OUT_Width,OUT_Height))
            IS = ImageSketcher(img_resized)
            IS.sketch()
            IS.exportImage(name)

            print("\nImage Processed! Go to the folder with these files to view it!")
        else:
            IS = ImageSketcher(img)
            IS.sketch()
            IS.exportImage(name)
            print("\nImage Processed! Go to the folder with these files to view it!")

    else:
        webcam = cv2.VideoCapture(0)
        while True:
            ret, frame = webcam.read()
            IS = ImageSketcher(frame)
            IS.sketch()
            imgf = np.array(IS.getImg())
            cv2.imshow('Take On Me Live Capture',imgf)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        webcam.release()
        cv2.destroyAllWindows()

while True:
    main()
    ask = input("Would you like to process another image/video/webcam capture? y/n: ")
    if ask.lower() in["n","no"]:
        break


import collections
import copy
from itertools import chain
import string

HEER_LABELS = string.ascii_uppercase
MAX_PAIRS = 10

class PlugboardError(Exception):
    pass

class Plugboard:
    def __init__(self, settings=None):
        self.wiring_map = list(range(26))
        self._backup_map = list(range(26))

        if not settings:
            return

        wiring_pairs = []

        # detect which syntax is being used
        if settings.find('/') != -1:
            # Kriegsmarine syntax
            pairs = settings.split()
            for p in pairs:
                try:
                    m, n = p.split('/')
                    m, n = int(m), int(n)
                except ValueError:
                    raise PlugboardError('invalid pair: %s' % p)

                wiring_pairs.append((m - 1, n - 1))
        else:
            # Heer/Luftwaffe syntax
            pairs = settings.upper().split()

            for p in pairs:
                if len(p) != 2:
                    raise PlugboardError('invalid pair: %s' % p)

                m = p[0]
                n = p[1]
                if m not in HEER_LABELS or n not in HEER_LABELS:
                    raise PlugboardError('invalid pair: %s' % p)

                wiring_pairs.append((ord(m) - ord('A'), ord(n) - ord('A')))

        if len(wiring_pairs) > MAX_PAIRS:
            raise PlugboardError('Please specify %d or less pairs' % MAX_PAIRS)

        counter = collections.Counter(chain.from_iterable(wiring_pairs))
        path, count = counter.most_common(1)[0]
        if count != 1:
            raise PlugboardError('duplicate connection: %d' % path)

        for pair in wiring_pairs:
            m = pair[0]
            n = pair[1]
            if not (0 <= m < 26) or not (0 <= n < 26):
                raise PlugboardError('invalid connection: %s' % str(pair))

            self.wiring_map[m] = n
            self.wiring_map[n] = m

    def signal(self, n):
        return self.wiring_map[n]


import time
import sys
import board

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import adafruit_dht

import RPi.GPIO as GPIO

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

# import subprocess

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used

# initialise 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# initialise dht sensor
dhtDevice = adafruit_dht.DHT11(board.D21) # Change board and pin number here

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
# text_font_name = 'Roboto-Bold.ttf'
text_font_name = 'Poppins-Light.ttf'
text_font_bold = 'Poppins-Bold.ttf'
icon_font_name = 'fontawesome-webfont.ttf'
font_large = ImageFont.truetype(text_font_name, 16)
font_large_bold = ImageFont.truetype(text_font_bold, 16)
font_small = ImageFont.truetype(text_font_name, 10)
font_icon = ImageFont.truetype(icon_font_name, 20)

while True:
  try:
      # Print the values to the serial port
      temperature_c = dhtDevice.temperature
      temperature_f = temperature_c * (9 / 5) + 32
      humidity = dhtDevice.humidity

      # display temprature

      # print icon
      draw.text((5, 2), chr(62152), font=font_icon, fill=255)
    #   # print temrature string
      draw.text((20, 2), str("Temprature") , font=font_small, fill=255)
      draw.text((20, 14), str(temperature_c), font=font_large_bold, fill=255)
      draw.text((45, 14), str("°c"), font=font_large, fill=255)

      # display temprature

      # print icon
      draw.text((0, 36), chr(62172), font=font_icon, fill=255)
      # print humidity string
      draw.text((20, 36), str("Humidity"), font=font_small, fill=255)
      draw.text((20, 46), str(humidity), font=font_large_bold, fill=255)
      draw.text((40, 46), str("%"), font=font_large, fill=255)

      # Display image.
      disp.image(image)
      disp.display()

      # time.sleep(1)
      draw.rectangle((0,0,width,height), outline=0, fill=0)

  except RuntimeError as error:
      # Errors happen fairly often, DHT's are hard to read, just keep going
      # print(error.args[0])
      time.sleep(2.0)
      continue
  except Exception as error:
      dhtDevice.exit()
      raise error

  time.sleep(2)


import unittest
from unittest.mock import patch, Mock
from django.test import TestCase

from lists.forms import (
    DUPLICATE_ITEM_ERROR, EMPTY_LIST_ERROR,
    ExistingListItemForm, ItemForm, NewListForm
)
from lists.models import Item, List

class ItemFormTest(TestCase):

    def test_form_item_input_has_placeholder_and_css_classes(self):
        form = ItemForm()
        self.assertIn('placeholder="Enter a to-do item"', form.as_p())
        self.assertIn('class="form-control input-lg"', form.as_p())

    def test_form_validation_for_blank_items(self):
        form = ItemForm(data={'text': ''})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['text'], [EMPTY_LIST_ERROR])

@patch('lists.forms.List')
@patch('lists.forms.Item')
class NewListFormTest(unittest.TestCase):

    def test_is_an_ItemForm(self, mockItem, mockList):
        self.assertIsInstance(NewListForm(), ItemForm)

    def test_save_creates_new_list_and_item_from_cleaned_data(
        self, mockItem, mockList
    ):
        mock_item = mockItem.return_value
        mock_list = mockList.return_value
        user = Mock()
        form = NewListForm()
        form.cleaned_data = {'text': 'new item text'}

        def check_item_text_and_list():
            self.assertEqual(mock_item.text, 'new item text')
            self.assertEqual(mock_item.list, mock_list)
            self.assertTrue(mock_list.save.called)
        mock_item.save.side_effect = check_item_text_and_list

        form.save(owner=user)

        self.assertTrue(mock_item.save.called)

    def test_save_saves_owner_if_authenticated(self, mockItem, mockList):
        mock_list = mockList.return_value
        mock_list.owner = None
        user = Mock(is_authenticated=lambda: True)
        form = NewListForm()
        form.cleaned_data = {'text': 'new item text'}

        form.save(owner=user)

        self.assertEqual(mock_list.owner, user)

    def test_does_not_save_owner_if_not_authenticated(self, mockItem, mockList):
        mock_list = mockList.return_value
        mock_list.owner = None
        user = Mock(is_authenticated=lambda: False)
        form = NewListForm()
        form.cleaned_data = {'text': 'new item text'}

        form.save(owner=user)

        self.assertEqual(mock_list.owner, None)

    def test_save_returns_new_list_object(self, mockItem, mockList):
        mock_list = mockList.return_value
        user = Mock(is_authenticated=lambda: True)
        form = NewListForm()
        form.cleaned_data = {'text': 'new item text'}

        response = form.save(owner=user)

        self.assertEqual(response, mock_list)

class ExistingListItemFormTest(TestCase):

    def test_is_an_ItemForm(self):
        self.assertIsInstance(ExistingListItemForm(for_list=List()), ItemForm)

    def test_form_validation_for_duplicate_items(self):
        list_ = List.objects.create()
        Item.objects.create(list=list_, text='no twins!')
        form = ExistingListItemForm(for_list=list_, data={'text': 'no twins!'})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['text'], [DUPLICATE_ITEM_ERROR])

    def test_form_save(self):
        list_ = List.objects.create()
        form = ExistingListItemForm(for_list=list_, data={'text': 'hi'})
        new_item = form.save()
        self.assertEqual(new_item, Item.objects.all()[0])


from sqlalchemy.orm import backref, relationship
from sqlalchemy.sql.expression import or_
from sqlalchemy.ext.hybrid import hybrid_method

from credoscript import Base, BaseQuery, schema
from credoscript.mixins import PathMixin
from credoscript.support.vector import Vector

class Atom(Base, PathMixin):
    """
    Represents an Atom entity from CREDO.

    Attributes
    ----------
    atom_id : int
        Primary key.
    residue_id : int
        Foreign key of the parent `Residue`.
    biomolecule_id : int
        Foreign key of the parent `Biomolecule`.
    group_pdb : string
        'ATOM' or 'HETATM'.
    atom_serial : int
        The PDB serial number.
    atom_name : string
        PDB atom name.
    alt_loc : string
        The alternate location code.
    coords : vector
    occupancy : float
    b_factor : float
    element : string
    hyb : int
        OpenEye atom hybridisation namespace.
    tripos_atom_type : string
        Tripos atom type.
    is_donor : bool

    is_acceptor : bool

    is_aromatic : bool

    is_weak_acceptor : bool

    is_weak_donor : bool

    is_hydrophobe : bool

    is_metal : bool

    is_pos_ionisable : bool

    is_neg_ionisable : bool

    is_xbond_donor : bool

    is_xbond_acceptor : bool

    is_carbonyl_oxygen : bool

    is_carbonyl_carbon : bool

    Mapped attributes
    -----------------
    Coords : Vector
    Contacts : Query
        Contacts that this Atom forms with other Atoms.

    Notes
    -----

    """
    __tablename__ = '%s.atoms' % schema['credo']

    Contacts  = relationship("Contact", query_class=BaseQuery,
                             primaryjoin="and_(or_(Contact.atom_bgn_id==Atom.atom_id, Contact.atom_end_id==Atom.atom_id), Contact.biomolecule_id==Atom.biomolecule_id)",
                             foreign_keys="[Contact.atom_bgn_id, Contact.atom_end_id, Contact.biomolecule_id]",
                             uselist=True, innerjoin=True, lazy='dynamic',
                             backref=backref('Atoms', uselist=True, innerjoin=True))

    def __repr__(self):
        """
        """
        return "<Atom({self.path})>".format(self=self)

    @property
    def res_num(self):
        return self.Residue.res_num

    @property
    def res_name(self):
        return self.Residue.res_name

    @property
    def ins_code(self):
        return self.Residue.ins_code

    @property
    def chain_id(self):
        return self.Residue.Chain.pdb_chain_id

    @property
    def _atom_name_alt_loc_tuple(self):
        """
        Full name of a PDB atom consisting of its name and alternate location identifier.
        Only used for the Residue|Peptide.Atom mappers.
        """
        return self.atom_name, self.alt_loc

    @property
    def pdb_line(self):
        name_fix = " %s" % self.atom_name if (len(self.atom_name) < 4 and not self.atom_name.startswith('H')) else self.atom_name
        line = "{:6s}{:5d} {:4s}{:1s}{:3s} {:1s}{:4d}{:1s}   {:8.3f}{:8.3f}{:8.3f}{:6.2f}{:6.2f}          {:>2s}  \n".format(
               self.group_pdb, self.atom_serial, name_fix, self.alt_loc, self.res_name,
               self.chain_id, self.res_num, self.ins_code, self.coords[0], self.coords[1], self.coords[2],
               self.occupancy, self.b_factor, self.element)
        return line

    @property
    def Vector(self):
        """
        Returns the coordinates of this atom as Vector object that supports linear
        algebra routines.
        """
        return Vector(self.coords)

    @property
    def ProximalWater(self):
        """
        Returns all water atoms that are in contact with this atom.

        Parameters
        ----------
        *expressions : BinaryExpressions, optional
            SQLAlchemy BinaryExpressions that will be used to filter the query.

        Joins
        -----
        Subquery (not exposed)

        Returns
        -------
        atoms : list
            List of `Atom` objects.

         Examples
        --------
        >>>
        """
        adaptor = AtomAdaptor(dynamic=True)
        return adaptor.fetch_all_water_in_contact_with_atom_id(self.atom_id,
                                                               self.biomolecule_id)

    @hybrid_method
    @property
    def is_polar(self):
        """
        Meta atom type indicating whether the atom is polar or not. The traditional
        medinical chemistry definition for polar atoms is used, i.e. either oxygen
        or nitrogen.
        """
        return any((self.element=='O', self.element=='N'))

    @is_polar.expression
    @property
    def is_polar(self):
        """
        Returns an SQLAlchemy boolean clause list that can enables usage of this
        meta atom type to filter query constructs.
        """
        return or_(Atom.element=='O', Atom.element=='N')

    @hybrid_method
    @property
    def is_mc(self):
        """
        Meta atom type indicating if the atom belongs to a protein backbone or
        not.
        """
        return any((self.atom_name=='C', self.atom_name=='N',
                    self.atom_name=='CA', self.atom_name=='O'))

    @is_mc.expression
    @property
    def is_mc(self):
        """
        """
        return or_(Atom.atom_name=='C', Atom.atom_name=='N',
                   Atom.atom_name=='CA', Atom.atom_name=='O')

    @property
    def type_bm(self):
        all_prop = ["is_acceptor","is_donor","is_weak_acceptor","is_weak_donor","is_xbond_acceptor","is_xbond_donor",
                    "is_pos_ionisable" ,"is_neg_ionisable","is_aromatic","is_hydrophobe",
                    "is_carbonyl_carbon","is_carbonyl_oxygen","is_metal"]

        return sum([int(getattr(self, prop, 0)) << i for (i, prop) in enumerate(all_prop)])

from ..adaptors.atomadaptor import AtomAdaptor


#!/python
import serial
import sys

import glob
dev=glob.glob('/dev/cu.usb*')[0]
print("Device:",dev)
#1stline: echo back of command
#2~nline: message
#last line: ">" only
class Cmd:
    def __init__(self,dev):
        self.ser = serial.Serial(dev, timeout=0.01)
        #self.ser.reset_input_buffer()
        #self.ser.reset_output_buffer()
        self.nowCmdNo=0
        self.results=[]
        self.lines=[]
        self.readBuf=""
        self.lastCmd=""

    def initResults(self):
        self.nowCmdNo=0
        self.results=[]
        self.lines=[]
        self.readBuf=""
    def retCmd(self):
        return [ r[0] for r in self.results]
    def getResults(self):
        self.readBuf+=self.ser.read(10000).decode().replace('>','>\n')

        newLines=self.readBuf.split('\n')
        self.readBuf=newLines[-1]
        newLines=newLines[:-1]
        self.lines.extend( [l.rstrip() for l in newLines])
        if(">" in self.lines):
            l=self.lines.index(">")
            if(l>0 and len(self.lines[0])>2 and (self.lines[0][0]=="G" or self.lines[0][0]=="M")):
                self.results.append(self.lines[:l])
            self.lines=self.lines[l+1:]

    def command(self,cmd):
        if(len(cmd)<2): return None
        if(cmd[0]!='M' and cmd[0]!='G'): return None
        print("send:",cmd)
        cmd+="\n"
        self.ser.write(cmd.encode("utf-8"))
        self.ser.flush()
        cmd=cmd.rstrip("\n")
        self.nowCmdNo+=1
        lastCmd=cmd

cmd=Cmd(dev)
cmd.command("M114\n\n\n\n\n\n\n\n\n\n\n\n\n")
cmd.command("M39")
while not "M39" in  cmd.retCmd():
    cmd.getResults()
print("inited")
cmd.initResults()

printedNo=0
for line in sys.stdin:
    resLen=len(cmd.results)
    while(cmd.nowCmdNo-resLen>4):
        cmd.getResults()
        resLen=len(cmd.results)
        if(printedNo!=resLen):
            res='\n'.join([ ''.join(lines) for lines in cmd.results[printedNo:]])
            print(res)
            printedNo=resLen

    cmd.command(line)

while(cmd.nowCmdNo!=resLen):
    cmd.getResults()
    resLen=len(cmd.results)
    if(printedNo!=resLen):
        res='\n'.join([ ''.join(lines) for lines in cmd.results[printedNo:]])
        print(res)
        printedNo=resLen

print("total # of command",cmd.nowCmdNo)