from django.views.generic import TemplateView
from web_project import TemplateLayout
from django.shortcuts import render
from django.http import HttpResponse
from scrapy.crawler import CrawlerProcess
from .scraper import MySpider

"""
This file is a view controller for multiple pages as a module.
Here you can override the page view layout.
Refer to sample/urls.py file for more pages.
"""

class SampleView(TemplateView):
    # Predefined function
    def get_context_data(self, **kwargs):
        # A function to init the global layout. It is defined in web_project/__init__.py file
        context = TemplateLayout.init(self, super().get_context_data(**kwargs))

        return context

# views.py

def category_articles(request):
    """
    Scrape articles based on the selected category and render them in a template.

    Args:
        request (HttpRequest): The HTTP request object.

    Returns:
        HttpResponse: The HTTP response object containing the rendered template.
    """
    # Call a function to scrape articles based on the selected category
    process = CrawlerProcess({
        'DOWNLOAD_DELAY': 5,  # Add a delay of 2 seconds between requests
        'CONCURRENT_REQUESTS': 1,  # Limit concurrent requests to 1
        'COOKIES_ENABLED': True,  # Enable cookies for session management
        'RETRY_TIMES': 3,  # Retry failed requests up to 3 times
        'DOWNLOADER_MIDDLEWARES': {
            'scrapy.downloadermiddlewares.retry.RetryMiddleware': 90,
            'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 110,
        },
    })

    process.crawl(MySpider)  # Instantiate MySpider class and crawl the data
    process.start()

    # Process the scraped data to match the format needed for the template
    results = MySpider.results
    articles = []
    for item in results:
        article = {
            'Title': item['Title'],
            'Authors': ', '.join(item['Authors']),  # Convert authors list to a string

        }
        print(article['Title'],article['Authors'])
        articles.append(article)

    # Render the template with the processed data
    return render(request, 'index.html', {'articles': articles})


import sys

def input():
    return sys.stdin.readline().rstrip()

def print(*args, sep=' ', end='\n'):
    sys.stdout.write(sep.join(map(str, args)))
    sys.stdout.write(end)

def solution():
    m, n = [int(num) for num in input().split()]
    a = []
    for i in range(n):
        a.append([int(num) for num in input().split()])
    dp1 = [0 for j in range(m)]
    for j in range(1, m):
        dp1[j] = dp1[j - 1] + abs(a[0][j] - a[0][j - 1])
    dp2 = [0 for j in range(m)]
    for i in range(1, n):
        dp2[0] = dp1[0] + abs(a[i][0] - a[i - 1][0])
        for j in range(1, m):
            dp2[j] = min(dp2[j - 1] + abs(a[i][j] - a[i][j - 1]), dp1[j] + abs(a[i][j] - a[i - 1][j]))
        dp1, dp2 = dp2, dp1
    print(dp1[m - 1])

def main():
    t = 1
    while t:
        solution()
        t -= 1

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*-coding:utf-8 -*-
# =============================================================================
"""
@Author        :   Yujie He
@File          :   eval_crowd.py
@Date created  :   2021/11/02
@Maintainer    :   Yujie He
@Email         :   yujie.he@epfl.ch
"""
# =============================================================================
"""
The module provides the evaluation pipeline includeing the computation of
crowd-related metrics (min_dist, crowd density within 5/10m, nomalized
proximity and corresponding visualization.
The emulation results is exported with suffix as "_crowd_eval.npy".
"""
# =============================================================================
"""
TODO:
1. compare with detected pedestrain from the rosbag!
2. use try/except when loading files
"""
# =============================================================================

import os
import argparse
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from qolo.core.crowdbot_data import (
    CrowdBotDatabase,
    CrowdbotExpParam,
    CROWDBOT_EVAL_TOOLKIT_DIR,
)
from qolo.utils.notebook_util import values2color_list
from qolo.utils.res_plot_util import save_cd_img, save_md_img, save_cd_img_single
from qolo.metrics.metric_crowd import compute_crowd_metrics, compute_norm_prox

# cross-zero checking: https://stackoverflow.com/a/29674950/7961693
def zero_crossing_check(data):
    crossing_idx = np.where(np.diff(np.signbit(data)))[0]
    less_than_zero = math.ceil(float(crossing_idx.shape[0]) / 2)
    return less_than_zero

#%% main function
if __name__ == "__main__":

    data_params_path = os.path.join(
        CROWDBOT_EVAL_TOOLKIT_DIR, "data", "data_params.yaml"
    )

    parser = argparse.ArgumentParser(description="Evaluate crowd characteristics")

    parser.add_argument(
        "-f",
        "--folder",
        default="0424_shared_control",
        type=str,
        help="different subfolder in rosbag/ dir",
    )
    parser.add_argument(
        "--params_path",
        default=data_params_path,
        type=str,
        help="path to dataset parameters",
    )
    parser.add_argument(
        "--save_img",
        dest="save_img",
        action="store_true",
        help="plot and save crowd density image",
    )
    parser.set_defaults(save_img=True)
    parser.add_argument(
        "--overwrite",
        dest="overwrite",
        action="store_true",
        help="Whether to overwrite existing rosbags (default: false)",
    )
    parser.set_defaults(overwrite=False)
    parser.add_argument(
        "--replot",
        dest="replot",
        action="store_true",
        help="Whether to re-plot existing images (default: false)",
    )
    parser.set_defaults(replot=False)
    parser.add_argument(
        "--goal_dist", default=30.0, type=float, help="The length to travel in the test"
    )
    args = parser.parse_args()

    cb_data = CrowdBotDatabase(args.folder)

    print("Starting evaluating crowd from {} sequences!".format(cb_data.nr_seqs()))

    all_data_params = CrowdbotExpParam(args.params_path)
    date = args.folder[:4]
    control_type = args.folder[5:]
    data_params = all_data_params.get_params(date, control_type)
    # {'goal_dist': float, 'vel_user_max': float, 'omega_user_max': float}
    print("# Experiment data:", date)
    print("# Experiment control type:", control_type)
    print("# Experiment settings:", data_params)

    eval_res_dir = os.path.join(cb_data.metrics_dir)

    if not os.path.exists(eval_res_dir):
        print("Result images and npy will be saved in {}".format(eval_res_dir))
        os.makedirs(eval_res_dir, exist_ok=True)

    for seq_idx in range(cb_data.nr_seqs()):

        seq = cb_data.seqs[seq_idx]
        print(
            "({}/{}): {} with {} frames".format(
                seq_idx + 1, cb_data.nr_seqs(), seq, cb_data.nr_frames(seq_idx)
            )
        )

        # load twist, pose2d
        twist_dir = os.path.join(cb_data.source_data_dir, "twist")
        qolo_twist_path = os.path.join(twist_dir, seq + "_twist_raw.npy")  # _twist
        if not os.path.exists(qolo_twist_path):
            print("ERROR: Please extract twist_stamped by using twist2npy.py")
        qolo_twist = np.load(qolo_twist_path, allow_pickle=True).item()

        pose2d_dir = os.path.join(cb_data.source_data_dir, "pose2d")
        qolo_pose2d_path = os.path.join(pose2d_dir, seq + "_pose2d.npy")
        if not os.path.exists(qolo_pose2d_path):
            print("ERROR: Please extract pose2d by using pose2d2npy.py")
        qolo_pose2d = np.load(qolo_pose2d_path, allow_pickle=True).item()

        # load _path_eval.npy
        path_eval_filepath = os.path.join(eval_res_dir, seq, seq + "_path_eval.npy")
        if not os.path.exists(path_eval_filepath):
            print("ERROR: Please extract twist_stamped by using eval_qolo_path.py")
        path_eval_dict = np.load(path_eval_filepath, allow_pickle=True).item()

        # dest: seq+'_crowd_eval.npy' file in eval_res_dir
        crowd_eval_npy = os.path.join(eval_res_dir, seq, seq + "_crowd_eval.npy")

        # only for plotting function update!
        if args.replot:
            crowd_eval_dict = np.load(crowd_eval_npy, allow_pickle=True).item()

            # recalculate virtual_collision
            min_dist_list = np.array(crowd_eval_dict["min_dist"])
            start_idx_ = path_eval_dict['start_idx']
            end_idx_ = path_eval_dict['end_idx']
            min_dist_list = min_dist_list[start_idx_ : end_idx_ + 1]
            # only consider the point that more than zero
            min_dist_list_ = min_dist_list[min_dist_list > 0]
            avg_min_dist = np.sum(min_dist_list_) / min_dist_list_.shape[0]

            # consider the number of crossing zero from positive to negative
            virtual_collision = zero_crossing_check(min_dist_list)

            crowd_eval_dict['avg_min_dist'] = avg_min_dist
            crowd_eval_dict['virtual_collision'] = virtual_collision
            print("virtual_collision:", crowd_eval_dict['virtual_collision'])
            np.save(crowd_eval_npy, crowd_eval_dict)

            # figure1: crowd density
            _, color_unique = values2color_list(
                [0, 1, 2],
                cmap_name='hot',
                given_values=[0.2, 0.55, 0.66],
                reverse=False,
            )

            save_cd_img(
                crowd_eval_dict,
                path_eval_dict,
                eval_res_dir,
                seq,
                fmt='pdf',
                add_title=False,
                use_serif=True,
                color_list=color_unique,
                color_vertical='navy',
            )
            # with single plots
            # save_cd_img_single(
            #     crowd_eval_dict,
            #     path_eval_dict,
            #     eval_res_dir,
            #     seq,
            #     dist=2.5,
            #     fmt='pdf',
            #     linecolor="navy",
            #     use_serif=True,
            # )

            # figure2: min. dist.
            save_md_img(crowd_eval_dict, path_eval_dict, eval_res_dir, seq)

            print("Replot images!")
        else:
            if (not os.path.exists(crowd_eval_npy)) or (args.overwrite):

                # timestamp can be read from lidars/ folder
                lidar_stamp_dir = os.path.join(cb_data.source_data_dir, "timestamp")
                stamp_file_path = os.path.join(lidar_stamp_dir, seq + "_stamped.npy")
                lidar_stamped = np.load(
                    stamp_file_path,
                    allow_pickle=True,
                ).item()
                ts = lidar_stamped.get("timestamp")

                # targeted metrics and correspoding dtype
                attrs = (
                    "all_det",
                    "within_det2_5",
                    "within_det3",
                    "within_det5",
                    "within_det10",
                    "crowd_density2_5",
                    "crowd_density3",
                    "crowd_density5",
                    "crowd_density10",
                    "min_dist",
                )
                dtypes = (
                    np.uint8,
                    np.uint8,
                    np.uint8,
                    np.uint8,
                    np.uint8,
                    np.float32,
                    np.float32,
                    np.float32,
                    np.float32,
                    np.float32,
                )

                crowd_eval_list_dict = {k: [] for k in attrs}

                num_msgs_between_logs = 100
                nr_frames = cb_data.nr_frames(seq_idx)

                for fr_idx in range(nr_frames):

                    _, _, _, trks = cb_data[seq_idx, fr_idx]

                    metrics = compute_crowd_metrics(
                        trks, virtual_radius=data_params["virtual_radius"]
                    )

                    if fr_idx % num_msgs_between_logs == 0 or fr_idx >= nr_frames - 1:
                        print(
                            "Seq {}/{} - Frame {}/{}: Crowd density within 2.5/3/5/10m: {:.2f} / {:.2f} / {:.2f} / {:.2f}".format(
                                seq_idx + 1,
                                cb_data.nr_seqs(),
                                fr_idx + 1,
                                nr_frames,
                                metrics[5],
                                metrics[6],
                                metrics[7],
                                metrics[8],
                            )
                        )

                    # update value for each attr
                    for idx, val in enumerate(metrics):
                        crowd_eval_list_dict[attrs[idx]].append(val)

                # evaluate the metrics through the sequence
                crowd_eval_dict = {
                    name: np.asarray(crowd_eval_list_dict[attrs[idx]], dtype=dtype)
                    for idx, (name, dtype) in enumerate(zip(attrs, dtypes))
                }

                # normalized proximity
                crowd_eval_dict.update(
                    {"normalized_proximity": compute_norm_prox(metrics[5])}
                )

                # calculate the avg for each matrix except avg_min_dist
                for attr in attrs:
                    if attr == "min_dist":
                        continue
                    avg_attr = "avg_" + attr
                    crowd_eval_dict.update(
                        {avg_attr: np.average(crowd_eval_dict[attr])}
                    )

                # calculate avg_min_dist
                min_dist_list = np.array(crowd_eval_dict["min_dist"])
                start_idx_ = path_eval_dict['start_idx']
                end_idx_ = path_eval_dict['end_idx']
                min_dist_list = min_dist_list[start_idx_ : end_idx_ + 1]
                # only consider the point that more than zero
                min_dist_list_ = min_dist_list[min_dist_list > 0]
                avg_min_dist = np.sum(min_dist_list_) / min_dist_list_.shape[0]

                # consider the number of crossing zero from positive to negative
                virtual_collision = zero_crossing_check(min_dist_list)

                crowd_eval_dict.update({'avg_min_dist': avg_min_dist})
                crowd_eval_dict.update({'virtual_collision': virtual_collision})
                print("virtual_collision:", crowd_eval_dict['virtual_collision'])

                # max and std of crowd density within 2.5m/5m
                crowd_eval_dict.update(
                    {
                        'max_crowd_density2_5': np.max(
                            crowd_eval_dict['crowd_density2_5']
                        )
                    }
                )
                crowd_eval_dict.update(
                    {
                        'std_crowd_density2_5': np.std(
                            crowd_eval_dict['crowd_density2_5']
                        )
                    }
                )

                crowd_eval_dict.update(
                    {'max_crowd_density5': np.max(crowd_eval_dict['crowd_density5'])}
                )
                crowd_eval_dict.update(
                    {'std_crowd_density5': np.std(crowd_eval_dict['crowd_density5'])}
                )

                # other attributes
                crowd_eval_dict.update({"timestamp": ts})

                np.save(crowd_eval_npy, crowd_eval_dict)

                if args.save_img:
                    # figure1: crowd density
                    save_cd_img(crowd_eval_dict, path_eval_dict, eval_res_dir, seq)

                    # figure2: min. dist.
                    save_md_img(crowd_eval_dict, path_eval_dict, eval_res_dir, seq)

            else:
                print(
                    "Detecting the generated {} already existed!".format(crowd_eval_npy)
                )
                print(
                    "Will not overwrite. If you want to overwrite, use flag --overwrite"
                )
                continue

    print("Finish crowd evaluation!")


# -*- coding: utf8 -*-
# python play.py --theme=sample --jpg=workspace/sample/toys/toy-ans0-0.jpg
# python play.py --theme=sample --toyjpg=toy-ans0-0

import os
from models.image.cifar10 import cifar10
import tensorflow as tf
from common import *

tf.app.flags.DEFINE_string('theme', 'sample', 'theme')
tf.app.flags.DEFINE_string('jpg', '', '.jpg file')
tf.app.flags.DEFINE_string('toyjpg', '', 'workspace/theme/toys/*.jpg file')
FLAGS = tf.app.flags.FLAGS
FLAGS.batch_size = 1

def detect_input_file ():
    input_image_file = ''
    # 入力画像ファイルパスを決定する
    if FLAGS.toyjpg != '':
        input_image_file = 'workspace/{}/toys/{}.jpg'.format(FLAGS.theme, FLAGS.toyjpg)
    if FLAGS.jpg != '':
        input_image_file = FLAGS.jpg
    if input_image_file == '':
        return None
    return input_image_file

def play_main (img):
    size = get_size()
    # 学習結果を利用するための準備
    cifar10.IMAGE_SIZE = size['width']
    cifar10.NUM_CLASSES = get_num_classes(FLAGS.theme)
    checkpoint_path = 'workspace/{}/train'.format(FLAGS.theme)
    # 入力は画像1枚分なので，FRAGS.batch_size=1としておく
    images = tf.placeholder(tf.float32, shape=(1, cifar10.IMAGE_SIZE, cifar10.IMAGE_SIZE, size['depth']))
    logits = tf.nn.softmax(cifar10.inference(images))

    sess = tf.Session()
    saver = tf.train.Saver(tf.all_variables())
    ckpt = tf.train.get_checkpoint_state(checkpoint_path)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
    else:
        print('No ckpt file')
    # 識別器での判定処理
    decoded = tf.image.decode_jpeg(img, channels=3)
    inputs = tf.reshape(decoded, decoded.eval(session=tf.Session()).shape)
    inputs = tf.image.per_image_whitening(inputs)
    inputs = tf.image.resize_images(tf.expand_dims(inputs, 0), cifar10.IMAGE_SIZE, cifar10.IMAGE_SIZE)
    output = sess.run(logits, feed_dict={images: inputs.eval(session=tf.Session())}).flatten().tolist()
    return output

if __name__ == '__main__':
    input_img_file = detect_input_file()
    # 画像ファイルを読み込む
    with open(input_img_file) as f:
        output = play_main(img=f.read())
        print_answer(FLAGS.theme, output)


# Estruturas de decisão ou condicionais

a = int(input("Digite um numero: "))

# definido o numero de acao
numero_acao = 7

# construindo a estrutura de decisão
if a > numero_acao:
    print("Desculpe o seu numero é maior que o sorteado")
if a < numero_acao:
    print("Desculpe o seu numero é menor que o sorteado")
if a == numero_acao:
    print("Você acertou!")


class Solution:
    def maxSumAfterPartitioning(self, A: List[int], K: int) -> int:
        # 0) Initialization
        #    best_a is used to keep past computation result
        #    best_a[i] is to keep local optimal result of A[0:i-1]
        best_a = [0] * len(A)
        best_a[0] = A[0]

        # 1) Iteratively fill the best_a with local optimal result
        #    best_a[0] = A[0]
        #    best_a[1] = max(best_a[0] + A[1], max(A[0:2]) * 2)
        #    best_a[2] = max(best_a[1] + A[2], best_a[0] + max(A[1:3]) * 2, max(A[0:3]) * 3)
        #    ...
        #    best_a[n] = max(best_a[n-1] + A[n], best_a[n-2] + max(A[n-1:n+1]) * 2, ..., max(A[n-K+1:n+1]) * K)
        for i in range(1, len(A)):
            max_v = 0
            for j in range(i, max(-1, i-K), -1):
                sa = A[j:i+1]
                v = best_a[j-1] + max(sa) * len(sa)
                if v > max_v:
                    max_v = v

            best_a[i] = max_v

        # 3) The global optimal result is kept in last element
        return best_a[-1]

"""
    def maxSumAfterPartitioning(self, A: List[int], K: int) -> int:
        n=len(A)
        dp=[0]*(n+1)
        for i in range(1,n+1):
            num=float('-inf')
            print("i,K",i,K)
            for j in range(1,min(i,K)+1):
                print(i,j,A[i-j],num,dp[i],dp[i-j]+num*j)
                num=max(num,A[i-j])
                dp[i]=max(dp[i],dp[i-j]+num*j)
        return dp[n]
"""

"""
1043. Partition Array for Maximum Sum
Medium

440

61

Add to List

Share
Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

Return the largest sum of the given array after partitioning.

Example 1:

Input: A = [1,15,7,9,2,5,10], K = 3
Output: 84
Explanation: A becomes [15,15,15,9,10,10,10]

Note:

1 <= K <= A.length <= 500
0 <= A[i] <= 10^6

"""


import sys
import json
import datetime
import logging
import os
import uuid
# pylint: disable=import-error
from helpers.response import ResponseHelper
from helpers.utils import convert_datetime_to_epoch
from helpers.dynamodb_helper import DynamoDbHelper

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info(event)
    status_code = 200
    message = "Record added successfully"
    record_id = None

    try:
        user_context = event["requestContext"]["authorizer"]
        question_id = event["pathParameters"]["question_id"]
        community_id = event["pathParameters"]["community_id"]
        body = json.loads(event['body'])

        if DynamoDbHelper.is_exists(os.environ["QUESTION_TABLE_NAME"], question_id):
            question_record = \
                DynamoDbHelper.get(os.environ['QUESTION_TABLE_NAME'],
                                   {"id": question_id, "community_id": community_id})[
                    "number_of_comments"]
            comments_number = question_record.get("number_of_comments", default=0)

            record_id = add_record(body, question_id, user_context, comments_number, community_id)
        else:
            status_code = 404
            message = "Given question doesn't exists"
    except Exception as exception:
        logger.error("Exception: {}".format(exception), exc_info=sys.exc_info())
        status_code = 500
        message = "Process has encountered an internal server exception."

    response = {
        "message": message
    }

    if not record_id is None:
        response["id"] = record_id

    return ResponseHelper.format_response(status_code, response)

def add_record(body, question_id, user_context, comments_number: int, community_id: str):
    current_datetime = datetime.datetime.now()
    body["id"] = str(uuid.uuid4())
    body["created_time"] = str(current_datetime)
    body["created_time_epoch"] = int(convert_datetime_to_epoch(current_datetime))
    body["question_id"] = question_id
    body["user_id"] = user_context["id"]
    body["user_email"] = user_context["email"]

    DynamoDbHelper.insert(os.environ["ANSWER_TABLE_NAME"], body)
    DynamoDbHelper.update_field(table_name=os.environ["QUESTION_TABLE_NAME"], key_name='id',
                                key_value=question_id,
                                field_name='number_of_comments', field_value=comments_number + 1,
                                community_id=community_id)

    return body["id"]


from applications.commons.log_lib import trace_func
import requests

class RequestFetch(object):
    POST = "post"
    GET = "get"
    PUT = "put"
    DELETE = "delete"

    def __init__(self, protocol, host_name, service_name, header=None, gis=None, cert=False):
        self.service_name = service_name
        self.host_name = host_name
        self.gis = gis
        self.protocol = protocol
        self.cert = cert
        self.header = {} if not header else header
        self.url = "{}://{}".format(self.protocol, self.host_name)

    def get_header(self, header=None):
        default_header = {}
        if self.gis:
            default_header.update({
                "gis": self.gis
            })
        if header:
            default_header.update(header)
        return default_header

    @trace_func()
    def fetch(self, uri='', body=None, files=None, logger=None, header=None, method="post", timeout=10, params=None,
              log_resp=True, **kwargs):
        if uri.startswith("/"):
            full_url = "{}{}".format(self.url, uri) if uri else self.url
        else:
            full_url = "{}/{}".format(self.url, uri) if uri else self.url
        func_call = getattr(requests, method)
        header = self.get_header(header)
        func_name = uri.replace("/", "_").replace("-", "_")
        logger.debug("call {} with url {} body {}".format(func_name, full_url, body))
        data = {
            "result": False
        }
        body = body if body else {}
        try:
            response = func_call(full_url, json=body, headers=header, files=files, verify=self.cert, timeout=timeout,
                                 params=params if params else {})
            data.update(http_status=response.status_code)
        except Exception as e:
            logger.error(f"call {func_name} error with {e}")
        else:
            try:
                data.update({"data": response.json()})
                data["result"] = True
            except Exception as e:
                logger.error(f"parse data error with {response.content} and exception {e}")
            else:
                if log_resp:
                    logger.debug('Call API {} SUCCESS: {}'.format(func_name, data))

        return data


from datetime import datetime
from collections import defaultdict
from pycocotools.coco import COCO
from typing import List
import json
import argparse
import numpy as np
import os
import torch

class FoodMetadata(COCO):
    """stores meta data on food images in COCO format"""

    def __init__(self, annotation_file=None, pred=False):
        super().__init__(annotation_file)
        self.file_name = annotation_file

        info = {
            "description": "Segmented Food Images",
            "version": "1.0",
            "year": datetime.now().strftime("%Y"),
            "contributors": "marcasty, pranav270-create",
            "date_created": datetime.now().strftime("%Y-%m-%d"),
        }

        # initialize new COCO JSON
        if annotation_file is None:
            self.dataset = {
                "info": info,
                "categories": [],
                "images": [],
                "annotations": [],
            }
        # if making predictions on given data, remove given annotations
        else:
            if "detection_issues" not in self.dataset["info"]:
                self.dataset["info"]["detection_issues"] = {
                    "failures": [],
                    "detect_nonclass": [],
                }
            if pred is True:
                self.anns = {}
                self.imgToAnns = {}
                self.dataset["annotations"] = []
                for img_id, _ in self.imgs.items():
                    self.imgToAnns[img_id] = []
                self.dataset["info"]["description"] = "Segmented Food Predictions"

    def get_num_categories(self):
        """how many categories (foods) are in this dataset?"""
        return len(self.cats)

    def add_categories(self, new_foods: List[str]):
        """
        -add new foods to categories
        -receives list of new foods; each element is words separated by spaces
        self.dataset["categories"] = [{
            'id': integer,
            'name': 'item-qualifier',
            'name_readable': 'Item, qualifier'
            'supercategory': 'food' }]
        """

        assert isinstance(new_foods, list), "please wrap new foods in list"

        existing_food_names = set(food["name"] for food in self.dataset["categories"])

        for food in new_foods:
            ingredients = food.split(" ")

            # prepare data to add to dicitonary
            if self.get_num_categories() > 0:
                id = self.dataset["categories"][-1]["id"] + 1
            else:
                id = 1
            name = "-".join(ingredients)
            name_readable = ", ".join(
                ingredient.capitalize() for ingredient in ingredients
            )

            if name not in existing_food_names:
                new_category = {
                    "id": id,
                    "name": name,
                    "name_readable": name_readable,
                    "supercategory": "food",
                }
                self.dataset["categories"].append(new_category)
                self.cats[id] = new_category
                self.catToImgs[id] = []

    def get_num_images(self):
        """return number of images in the dataset"""
        return len(self.imgs)

    def add_image_data(self, filename: str, width, height, cat_id, query: str = None):
        """add an image to the coco json file"""

        if self.get_num_images() > 0:
            id = max(self.imgs.keys()) + 1
        else:
            id = 1
        new_image = {
            "id": id,
            "category_id": cat_id,
            "filename": filename,
            "width": width,
            "height": height,
            "date captured": datetime.now().strftime("%Y-%m-%d"),
        }

        # append query if it was google searched
        if query is not None:
            new_image["Google Search Query"]: query

        self.imgs[id] = new_image
        self.catToImgs[cat_id].append(id)
        self.imgToAnns[id] = []

    def imgToCat(self, img_id):
        for cat, img_list in self.catToImgs.items():
            if img_id in img_list:
                return cat

    def get_num_annotations(self):
        """return number of annotations"""
        return len(self.anns)

    def next_ann_id(self):
        if self.get_num_annotations() == 0:
            return 1
        else:
            return max(self.anns.keys()) + 1

    def drop_ann(self, ann_ids: list):
        for ann_id in ann_ids:
            image_id = self.anns[ann_id]["image_id"]
            cat_id = self.anns[ann_id]["category_id"]
            self.anns.pop(ann_id)
            anns = []
            for ann in self.imgToAnns[image_id]:
                if ann["id"] != ann_id or ann["category_id"] != cat_id:
                    anns.append(ann)
            self.imgToAnns[image_id] = anns

    def update_imgToAnns(self, ann_id, image_id, key, value):
        for ann in self.imgToAnns[image_id]:
            if ann["id"] == ann_id:
                ann[key] = value

    def create_annot(self, image_id, cat_id):
        """initializes new id"""
        ann_id = self.next_ann_id()

        new_annotation = {
            "id": ann_id,
            "image_id": image_id,
            "category_id": cat_id,
        }
        self.anns[ann_id] = new_annotation
        self.imgToAnns[image_id].append(new_annotation)
        return ann_id

    def add_annot(self, ann_id, image_id, model, text):
        """add text annotation"""
        self.anns[ann_id][model] = text
        self.update_imgToAnns(ann_id, image_id, model, text)

    def add_dino_annot(self, ann_id, image_id, detections):
        """add DINO annotations"""

        self.add_annot(ann_id, image_id, "num_objects", len(detections["bbox"]))
        self.add_annot(ann_id, image_id, "classes", detections["classes"])

        for i in range(len(detections["bbox"])):
            if "bbox" in self.anns[ann_id]:
                new_annotation = self.anns[ann_id].copy()
                ann_id = self.next_ann_id()
                new_annotation["id"] = ann_id
                self.anns[ann_id] = new_annotation
                self.imgToAnns[image_id].append(new_annotation)
            self.add_annot(ann_id, image_id, "class_id", detections["class_id"][i])
            self.add_annot(ann_id, image_id, "bbox", detections["bbox"][i])
            self.add_annot(
                ann_id,
                image_id,
                "box_confidence",
                round(float(detections["box_confidence"][i]), 4),
            )

    def add_sam_annot(self, ann_id, image_id, mask, mask_confidence, mask_dir):
        if mask_dir:
            mask_id = f"{image_id}_{ann_id}.pt"
            mask_filepath = os.path.join(mask_dir, mask_id)
            torch.save(torch.Tensor(mask), mask_filepath)
            self.add_annot(ann_id, image_id, "mask", mask_id)
        else:
            self.add_annot(ann_id, image_id, "mask", mask)
        self.add_annot(
            ann_id, image_id, "mask_confidence", round(float(mask_confidence), 4)
        )

    # save the dict as a json file
    def export_coco(self, new_file_name=None):
        if new_file_name is None:
            current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
            file_name = f"metadata_{current_datetime}.json"
        else:
            file_name = new_file_name

        self.dataset["categories"] = list(self.cats.values())
        self.dataset["images"] = list(self.imgs.values())
        self.dataset["annotations"] = list(self.anns.values())

        print(f"saving data as {file_name}")
        with open(file_name, "w") as json_file:
            json.dump(self.dataset, json_file, indent=4)

def parse_arguments():
    parser = argparse.ArgumentParser(
        description="Defines a metadata object in COCO format and scrapes Google for images of food."
    )
    parser.add_argument(
        "--metadata_json", type=str, help="JSON file containing metadata in COCO format"
    )
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_arguments()

    # either opens supplied json or creates new coco file
    coco = FoodMetadata(args.metadata_json)

    # if you want to keep the annotations of the json file:
    val_data = FoodMetadata("public_validation_set_2.1_blip_spacy.json")

    # if you want to make new predictions on the data, set pred=True
    prediction = FoodMetadata("public_validation_set_release_2.1.json", pred=True)
    print(prediction.dataset["images"][0])
    print(prediction.dataset["annotations"])
    # export metadata to json file
    # val_data.export_coco(new_file_name='data.json')

    """
    there are multiple annotations per image
    there are multiple images per category
    some images have multiple categories
    .catToImgs and imgToAnns are dicts in FoodMetadata class to keep track of this
    """


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime

from django.db.backends.mysql.schema import DatabaseSchemaEditor as BaseDatabaseSchemaEditor

class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
    sql_alter_column_no_default = ""
    sql_alter_column_no_default_bak = "ALTER COLUMN %(column)s DROP DEFAULT"
    sql_alter_column_default_null = "ALTER COLUMN %(column)s SET DEFAULT NULL"
    sql_rename_column = "ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s %(default)s"

    def column_sql(self, model, field, include_default=False):
        '''
        修改 设置字段允许为NULL时，增加DEFAULT NULL
        修改 include_default为FALSE时，有设置DEFAULT的值列设置DEFAULT，方法的不设置
        '''
        # Get the column's type and use that as the basis of the SQL
        db_params = field.db_parameters(connection=self.connection)
        sql = db_params['type']
        params = []
        # Check for fields that aren't actually columns (e.g. M2M)
        if sql is None:
            return None, None
        # Work out nullability
        null = field.null

        ############# modify 20170420 ###################
        # If we were told to include a default value, do so
        if not self.skip_default(field):
            include_default = self._has_default(field) \
                if not include_default else include_default

            if include_default:
                default_value = self.effective_default(field)
                if default_value is not None:
                    if self.connection.features.requires_literal_defaults:
                        # Some databases can't take defaults as a parameter (oracle)
                        # If this is the case, the individual schema backend should
                        # implement prepare_default
                        sql += " DEFAULT %s" % self.prepare_default(default_value)
                    else:
                        sql += " DEFAULT %s"
                        params += [default_value]
                else:
                    if field.null:
                        # 没有DEFAULT，可允许为空，需要设置DEFAULT NULL
                        sql += ' DEFAULT NULL'
        ############# modify end ###################

        # Oracle treats the empty string ('') as null, so coerce the null
        # option whenever '' is a possible value.
        if (field.empty_strings_allowed and not field.primary_key and
                self.connection.features.interprets_empty_strings_as_nulls):
            null = True
        if null and not self.connection.features.implied_column_null:
            sql += " NULL"
        elif not null:
            sql += " NOT NULL"
        # Primary key/unique outputs
        if field.primary_key:
            sql += " PRIMARY KEY"
        elif field.unique:
            sql += " UNIQUE"
        # Optionally add the tablespace if it's an implicitly indexed column
        tablespace = field.db_tablespace or model._meta.db_tablespace
        if tablespace and self.connection.features.supports_tablespaces and field.unique:
            sql += " %s" % self.connection.ops.tablespace_sql(tablespace, inline=True)
        # Return the sql
        return sql, params

    def add_field(self, model, field):
        '''
        修改 当DEFAULT为方法生成的时，要删除DEFAULT，其它情况保留DEFAULT
        '''
        super(DatabaseSchemaEditor, self).add_field(model, field)

        # 删除不需要保留的default
        result = self._del_auto_default(model, field)
        if field.null and result:
            # 删除默认值，又允许空，那要设置default null
            self._set_default_null(model, field)

    def _alter_field(self, model, old_field, new_field, old_type, new_type,
                     old_db_params, new_db_params, strict=False):
        '''
        调用父类方法前，当新字段没有default而旧的有时，先删除
        调完后，删除不需要保留的default
        '''
        old_has_default = self._has_default(old_field)
        new_has_default = self._has_default(new_field)

        if old_has_default and not new_has_default:
            # 新的字段没有default,删除
            self._del_default(model, old_field)

        super(DatabaseSchemaEditor, self)._alter_field(model, old_field,
            new_field, old_type, new_type, old_db_params, new_db_params, strict)

        # 删除不需要保留的default
        result = self._del_auto_default(model, new_field)
        if new_field.null and (result or self.effective_default(new_field) is None):
            # 没设置default或有设置但被删了，但可为空,设置default null
            self._set_default_null(model, new_field)

    def _rename_field_sql(self, table, old_field, new_field, new_type):
        ''' 列重命令操作语句'''

        default_value = ''
        if not self.skip_default(new_field):
            default = self.effective_default(new_field)
            if default is not None:
                default_value = 'DEFAULT {0}'.format(default)
            else:
                if new_field.null:
                    # 没有DEFAULT，可允许为空，需要设置DEFAULT NULL
                    default_value = ' DEFAULT NULL'

        return self.sql_rename_column % {
            "table": self.quote_name(table),
            "old_column": self.quote_name(old_field.column),
            "new_column": self.quote_name(new_field.column),
            "type": new_type,
            "default": default_value
        }

    def _has_default(self, field):
        '''
        字段是否有设置default
        '''
        if self.skip_default(field):
            return False

        if field.null:
            # 允许为空，不管有没有设置DEFAULT都会有
            return True

        if field.has_default():
            default = field.default
            if default is not None and not callable(default):
                return True
        if field.blank and field.empty_strings_allowed:
            return True

        return False

    def _del_auto_default(self, model, field):
        '''
        删除自动生成的DEFAULT，如方法、auto_now、auto_now_add
        '''
        if self.skip_default(field):
            return False

        default = None
        if field.has_default():
            default = field.default

        if getattr(field, 'auto_now', False) or \
            getattr(field, 'auto_now_add', False) or \
            (default is not None and callable(default)):
            # 删除DEFAULT
            self._del_default(model, field)
            return True

        return False

    def _del_default(self, model, field):
        '''
        删除DEFAULT
        '''
        sql = self.sql_alter_column % {
            "table": self.quote_name(model._meta.db_table),
            "changes": self.sql_alter_column_no_default_bak % {
                "column": self.quote_name(field.column)
                }
            }

        self.execute(sql)

    def _set_default_null(self, model, field):
        '''
        设置DEFAULT NULL
        '''
        sql = self.sql_alter_column % {
            "table": self.quote_name(model._meta.db_table),
            "changes": self.sql_alter_column_default_null % {
                "column": self.quote_name(field.column)
                }
            }

        self.execute(sql)


import cv2
import numpy as np

#Йщох╣■¤Б╦сие
def aHash(img):
    img = cv2.resize(img, (8,8), interpolation=cv2.INTER_CUBIC)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    s = 0
    hash_str = ''
    for i in range(8):
        for j in range(8):
            s = s + gray[i,j]
    avg = s/64
    for i in range(8):
        for j in range(8):
            if gray[i,j] > avg:
                hash_str = hash_str + '1'
            else:
                hash_str = hash_str + '0'
    return hash_str

#▓Ьох╣■¤Б╦сие
def dHash(img):
    img = cv2.resize(img, (9,8), interpolation=cv2.INTER_CUBIC)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    hash_str = ''
    for i in range(8):
        for j in range(8):
            if gray[i,j] > gray[i,j+1]:
                hash_str = hash_str+'1'
            else:
                hash_str = hash_str+'0'
    return hash_str

def cmpHash(hash1, hash2):
    n = 0
    if len(hash1) != len(hash2):
        return -1
    for i in range(len(hash1)):
        if hash1[i] != hash2[i]:
            n = n+1
    return n

img1 = cv2.imread('lenna.png')
img2 = cv2.imread('lenna_PepperandSalt.png')
hash1 = aHash(img1)
hash2 = aHash(img2)
print(hash1)
print(hash2)
n = cmpHash(hash1,hash2)
print("Йщох╣■¤Б╦сие¤Я╦кХ╚Б║", n)

hash2 = dHash(img1)
hash2 = dHash(img2)
print(hash1)
print(hash2)
n = cmpHash(hash1,hash2)
print("▓Ьох╣■¤Б╦сие¤Я╦кХ╚Б║", n)


from plane_sprites import *
import pygame

class PlaneGame(object):
    def __init__(self):
        pygame.init()
        # 1.创建游戏窗口
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        # 2.创建时钟
        self.clock = pygame.time.Clock()
        # 3.调用私有方法，精灵和精灵族的创建
        self.__create_sprites()
        print("init")
        # 4.设置定时器事件 创建敌机 1s
        #pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def __create_sprites(self):
        bq1 = Background()
        bq2 = Background(is_alt=True)
        self.back_group = pygame.sprite.Group(bq1, bq2)
        # 创建敌机精灵组
        self.enemy_group = pygame.sprite.Group()
        # 创建英雄的精灵族
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

    def start_game(self):
        print("start...")
        while True:
            # 1.设置刷新帧
            self.clock.tick(FRAME_PER_SEC)
            # 2.事件监听
            self.__event_handler()
            # 3.碰撞检测
            self.__check_collide()
            # 4.更新/绘制精灵族
            self.__update_sprites()
            # 5.更新显示
            pygame.display.update()

    def __event_handler(self):
        for event in pygame.event.get():
            # 判断退出
            if event.type == pygame.QUIT:
                PlaneGame.__game_over()
            elif event.type == CREATE_ENEMY_EVENT:
                # print("敌机出场...")
                # 创建敌机精灵
                enemy = Enemy()
                # enemy.speedx = random.randint(0, 3)
                # 将敌机精灵加入到精灵族
                self.enemy_group.add(enemy)
            elif event.type == HERO_FIRE_EVENT:
                self.hero.fire()
        # 使用键盘提供的方法提取键盘按键
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            self.hero.speedx = 5
        elif keys_pressed[pygame.K_LEFT]:
            self.hero.speedx = -5
        else:
            self.hero.speedx = 0

        if keys_pressed[pygame.K_DOWN]:
            self.hero.speedy = 5
        elif keys_pressed[pygame.K_UP]:
            self.hero.speedy = -5
        else:
            self.hero.speedy = 0

    def __check_collide(self):
        # 1.子弹摧毁敌机
        pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
        # 2.敌机摧毁英雄
        enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
        # 判断列表是否有内容
        if len(enemies) > 0:
            self.hero.kill()
            PlaneGame.__game_over()

    def __update_sprites(self):
        self.back_group.update()
        self.back_group.draw(self.screen)
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)
        self.hero_group.draw(self.screen)
        self.hero_group.update()
        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    @staticmethod
    def __game_over():
        print("游戏结束")
        pygame.quit()
        exit()

if __name__ == '__main__':
    game = PlaneGame()
    game.start_game()


import gym
import itertools as it
import typing

from vel.api import LinearBackboneModel, ModelFactory, BackboneModel
from vel.modules.input.identity import IdentityFactory
from vel.rl.api import Rollout, RlModel, Evaluator
from vel.rl.modules.action_head import ActionHead
from vel.rl.modules.value_head import ValueHead
from vel.rl.models.stochastic_policy_model import StochasticPolicyEvaluator

class StochasticPolicyModelSeparate(RlModel):
    """
    Policy gradient model class with an actor and critic heads that don't share a backbone
    """

    def __init__(self, input_block: BackboneModel,
                 policy_backbone: LinearBackboneModel, value_backbone: LinearBackboneModel,
                 action_space: gym.Space):
        super().__init__()

        self.input_block = input_block
        self.policy_backbone = policy_backbone
        self.value_backbone = value_backbone

        self.action_head = ActionHead(
            action_space=action_space,
            input_dim=self.policy_backbone.output_dim
        )

        self.value_head = ValueHead(input_dim=self.value_backbone.output_dim)

    def reset_weights(self):
        """ Initialize properly model weights """
        self.input_block.reset_weights()

        self.policy_backbone.reset_weights()
        self.value_backbone.reset_weights()

        self.action_head.reset_weights()
        self.value_head.reset_weights()

    def forward(self, observations):
        """ Calculate model outputs """
        input_data = self.input_block(observations)

        policy_base_output = self.policy_backbone(input_data)
        value_base_output = self.value_backbone(input_data)

        action_output = self.action_head(policy_base_output)
        value_output = self.value_head(value_base_output)

        return action_output, value_output

    def step(self, observation, argmax_sampling=False):
        """ Select actions based on model's output """
        policy_params, values = self(observation)
        actions = self.action_head.sample(policy_params, argmax_sampling=argmax_sampling)

        # log likelihood of selected action
        logprobs = self.action_head.logprob(actions, policy_params)

        return {
            'actions': actions,
            'values': values,
            'action:logprobs': logprobs
        }

    def policy_parameters(self):
        """ Parameters of policy """
        return it.chain(self.policy_backbone.parameters(), self.action_head.parameters())

    def logprob(self, action_sample, policy_params):
        """ Calculate - log(prob) of selected actions """
        return self.action_head.logprob(action_sample, policy_params)

    def value(self, observations):
        """ Calculate only value head for given state """
        input_data = self.input_block(observations)
        base_output = self.value_backbone(input_data)
        value_output = self.value_head(base_output)
        return value_output

    def policy(self, observations):
        """ Calculate only action head for given state """
        input_data = self.input_block(observations)
        policy_base_output = self.policy_backbone(input_data)
        policy_params = self.action_head(policy_base_output)
        return policy_params

    def evaluate(self, rollout: Rollout) -> Evaluator:
        """ Evaluate model on a rollout """
        return StochasticPolicyEvaluator(self, rollout)

    def entropy(self, policy_params):
        """ Entropy of a probability distribution """
        return self.action_head.entropy(policy_params)

    def kl_divergence(self, pd_q, pd_p):
        """ Calculate KL-divergence between two probability distributions """
        return self.action_head.kl_divergence(pd_q, pd_p)

class StochasticPolicyModelSeparateFactory(ModelFactory):
    """ Factory class for policy gradient models """
    def __init__(self, input_block: ModelFactory, policy_backbone: ModelFactory, value_backbone: ModelFactory):
        self.input_block = input_block
        self.policy_backbone = policy_backbone
        self.value_backbone = value_backbone

    def instantiate(self, **extra_args):
        """ Instantiate the model """
        input_block = self.input_block.instantiate()
        policy_backbone = self.policy_backbone.instantiate(**extra_args)
        value_backbone = self.value_backbone.instantiate(**extra_args)

        return StochasticPolicyModelSeparate(input_block, policy_backbone, value_backbone, extra_args['action_space'])

def create(policy_backbone: ModelFactory, value_backbone: ModelFactory,
           input_block: typing.Optional[ModelFactory] = None):
    """ Vel factory function """
    if input_block is None:
        input_block = IdentityFactory()

    return StochasticPolicyModelSeparateFactory(
        input_block=input_block,
        policy_backbone=policy_backbone,
        value_backbone=value_backbone
    )


import heapq
import random
import time

def solution(A):
    heapq.heapify(A)
    minimum = heapq.heappop(A)
    found = False
    while minimum <= 0:
        try:
            minimum = heapq.heappop(A)
        except IndexError:
            return 1
    if minimum > 1:
        return 1
    next = heapq.heappop(A)
    while not found:
        if next <= minimum + 1:
            minimum = next
            try:
                next = heapq.heappop(A)
            except IndexError:
                return minimum + 1

        else:
            return minimum + 1

def check(A):
    maximum = max(A)
    if maximum <= 0:
        return 1
    for i in range(1, maximum):
        if i in A:
            pass
        else:
            return i
    return maximum + 1

B = [random.randint(-100000, 100000) for i in range(-10000, 10001)]
print(check(B))

now = time.time()
print(solution(B))
print(time.time() - now)
B.sort()
print(B)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from .. import users
from ..models import GitHubCore

class UserSearchResult(GitHubCore):
    def _update_attributes(self, data):
        result = data.copy()

        #: Score of the result
        self.score = self._get_attribute(result, 'score')
        if 'score' in result:
            del result['score']

        #: Text matches
        self.text_matches = self._get_attribute(result, 'text_matches', [])
        if 'text_matches' in result:
            del result['text_matches']

        #: User object matching the search
        self.user = users.ShortUser(result, self)

    def _repr(self):
        return '<UserSearchResult [{0}]>'.format(self.user)


'''
Created on 16 mars 2022

@author: Fab
'''

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import itertools
import numpy
from gevent.libev.corecext import NONE
from experiments.sugarsocial.analysis.LogAnalyser import LogAnalyser
import random
from matplotlib.ticker import FuncFormatter, MultipleLocator
import datetime
import json
import numpy as np
import matplotlib
from matplotlib import ticker

worksheet = None
wy= 0

def jitter( value , l ):
    ll =[]
    for i in range( len(l) ):
        ll.append( value+ (random.random()-0.5)*0.4 )
    return ll

def noData(ax ,text="no data"):
    ax.text(0.5,0.5,text,horizontalalignment='center', verticalalignment='center', transform = ax.transAxes)

def format_func(x, pos):
    hours = int(x//3600)
    minutes = int((x%3600)//60)
    #seconds = int(x%60)

    return "{:d}h{:02d}".format(hours, minutes)
    #return "{:d}h".format(hours)

def invert( a,b ):
        return b,a

def convert_to_d_h_m_s( frames ):
    """Return the tuple of days, hours, minutes and seconds."""
    #seconds = frames / 30
    seconds, f = divmod( frames, 30)
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)

    return days, hours, minutes, seconds, f

def frameToTimeTicker(x, pos):

    #    vals= convert_to_d_h_m_s( x*30 )
    #return "D{0} - {1:02d}:{2:02d}".format( int(vals[0])+1, int(vals[1]), int(vals[2]) )
    return str ( int( x/(60*60) ) ).zfill(2)

def compute():

    # batch mode
    from os import listdir
    from os.path import isfile, join
    mypath = "batch social sucrose"
    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    print( onlyfiles )

    numberOfAnimals = {}

    data = {}

    for file in onlyfiles:
        if "jamReport" in file:
            continue
        if ".pdf" in file:
            continue
        if ".xls" in file:
            continue
        if ".zip" in file:
            continue
        #processFile( mypath+"/"+file )

        analyser = LogAnalyser( mypath+"/"+file )
        phases = [ "PHASE1" , "PHASE2" , "PHASE3" , "PHASE4","PHASE5","PHASE6"]

        print("---")
        print( "file" , analyser.logFile )
        print( "name", analyser.experimentName )
        print( "geno", analyser.getGenotype() )
        print( "sex", analyser.getSex() )
        print( "age", analyser.getAge() )
        phaseDurationList = []
        for phase in phases:
            analyser.setAnalysisPhase( phase )
            try:
                phaseDurationList.append( analyser.getPhaseDurationS() )
            except:
                phaseDurationList.append( None ) # this phase has not been reached

        data[ analyser.logFile ] = {}
        d = data[ analyser.logFile ]
        d[ "name" ] = analyser.experimentName
        d[ "genotype" ] = analyser.getGenotype()
        d[ "sex" ] = analyser.getSex()
        d[ "age" ] = analyser.getAge()
        d[ "phaseDurations"] = phaseDurationList
        d[ "endTimePhase6S"] = getEndTimePhase6( analyser )

        json.dump(data, open("SugarSocialData.json", 'w'))

        k = f"{analyser.getGenotype()} {analyser.getSex()} {analyser.getAge()}w"
        if k not in numberOfAnimals:
            numberOfAnimals[k] = 0
        numberOfAnimals[k]+=1

    print("Animal list:")
    for k,v in numberOfAnimals.items():
        print(k,"\t",v)

def plot( data, genotype , phase , y2 ):

    x = []
    y = []
    for d in data.values():
        print( d )
        if d["genotype"] == genotype:
            x.append( d["phaseDurations"][phase] )
            y.append( y2 )
    plt.scatter( x , y )

def toHM( seconds ):

    mm, ss = divmod(seconds, 60)
    hh, mm= divmod(mm, 60)
    return f"{int(hh)}:{str(int(mm)).zfill(2)}"

def getEndTimePhase6( analyser ):

    nbValue = 40
    times, ratios, nbValues , notused = analyser.getRatioProgress( "PHASE6", nbValue )
    threshold = 2.*(nbValue/3.)
    for i in range( len( ratios )):
        ratio = ratios[i]
        time = times[i]
        nbVal = nbValues[i] # to check if enough values have been checked ( 20 or 40 ? )
        if ratio > threshold and nbVal > nbValue :
            return time
            break

def analyse():

    data = json.load(open("SugarSocialData.json"))

    '''
    plot( data, "WT" , 0 , 1 )
    plot( data, "KO" , 0 , 2 )
    '''

    animals = []

    plt.figure(figsize=(20,8))
    colors = plt.get_cmap('RdYlGn')( np.linspace(0.15, 0.95, 6 ))
    ax = plt.gca()

    '''
    # filter data
    for k in data.copy():
        d = data[k]
        if not "KO" in d["genotype"]:
            data.pop(k)
    for k in data.copy():
        d = data[k]
        if not "female" in d["sex"]:
            data.pop(k)

    for k in data.copy():
        d = data[k]
        if d["age"] != 12:
            data.pop(k)

    for k in data.copy():
        d = data[k]
        if d["age"] !=12 :
            data.pop(k)

    for d in data:
        #print( d )
        dd = data[d]
        for maxPhase in range( 6 ):
            if dd["phaseDurations"][maxPhase] == None:
                break
        endTime = ""
        if dd["endTimePhase6S"] != None:
            endTime =dd["endTimePhase6S"]

        print( f"{d}{dd['genotype']}\t{dd['sex']}\t{dd['age']}\t{dd['genotype']}\t{maxPhase+1}\t{endTime}" )
    quit()
    '''
    '''

    '''
    files = [
    "batch social sucrose/gateLog - 2022-10-17 malewtda114(3818)8mois.log.txt",

    "batch social sucrose/gateLog - 2022-08-31 femWTda91(5662)8months.log.txt",
    "batch social sucrose/gateLog - 2022-09-19 femWTda92(5605)8months.log.txt",
    "batch social sucrose/gateLog - 2022-10-26 femwtda115(3799)8mois.log.txt",

    "batch social sucrose/gateLog - 2022-10-19 malekoda113(3777)8mois.log.txt",
    "batch social sucrose/gateLog - 2022-11-02 malekoda117(3809)8mois.log.txt",

    "batch social sucrose/gateLog - 2022-09-05 femKOda103(5580)8months.log.txt",
    "batch social sucrose/gateLog - 2022-09-07 femKOda104(5652 blessee)8months.log.txt",
    "batch social sucrose/gateLog - 2022-11-07 femkoda130(5679)8mois phase1.log.txt",

    "batch social sucrose/gateLog - 2022-11-14 femhetda131(5701)8mois.log.txt",
    "batch social sucrose/gateLog - 2022-11-16 femhetda132(5716)8mois.log.txt",
    "batch social sucrose/gateLog - 2022-11-21 femhet146(5720)phase2 8mois.log.txt",
    "batch social sucrose/gateLog - 2022-11-23 femhet147(5691)8mois.log.txt",
    "batch social sucrose/gateLog - 2022-11-28 femhet138(5728)8moisphase2.log.txt",
    "batch social sucrose/gateLog - 2022-11-30 femhet139(5735)8mois phase3.log.txt",
    ]
    '''

    files = [
    "batch social sucrose/gateLog - 2022-09-21 maleWTda226(5669)12w.log.txt",
    "batch social sucrose/gateLog - 2022-12-09 malewtda334(3795)12w.log.txt",
    "batch social sucrose/gateLog - 2023-02-13 malewtda375(2og)12w.log.txt",
    "batch social sucrose/gateLog - 2023-01-09 malewtda323(3806)12wok.log.txt",
    "batch social sucrose/gateLog - 2023-01-11 malewtda324(3780)12wok.log.txt",

    "batch social sucrose/gateLog - 2022-12-14 femwtda313(3802)12w.log.txt",
    "batch social sucrose/gateLog - 2023-01-18 femwtda355(3876)12w.log.txt",

    "batch social sucrose/gateLog - 2022-12-05 femkoda315(3821)12w.log.txt",
    "batch social sucrose/gateLog - 2022-12-16 femkoda337(3820)12wphase3.log.txt",
    "batch social sucrose/gateLog - 2022-12-19 femkoda338(3786)12wphase2.log.txt",
    "batch social sucrose/gateLog - 2023-01-04 femkoda339(3807)12wphase3.log.txt",
    "batch social sucrose/gateLog - 2023-01-06 femkoda340(3801)12wcompleted.log.txt",

    "batch social sucrose/gateLog - 2023-02-10 malekoda367(2od)12w.log.txt",
    "batch social sucrose/gateLog - 2022-09-26 maleKOda227(5584)12w.log.txt",
    "batch social sucrose/gateLog - 2023-02-07 malekoda365(od)12w.log.txt",
    "batch social sucrose/gateLog - 2023-01-16 maleko318(od)12w.log.txt",
    "batch social sucrose/gateLog - 2023-01-16 maleko319(og)12ws2.log.txt",

    "batch social sucrose/gateLog - 2023-01-30 femhetda379(2od)12w.log.txt",
    "batch social sucrose/gateLog - 2023-02-01 femhetda369(og)12w.log.txt",
    "batch social sucrose/gateLog - 2023-02-03 femhetda371(2og)12w.log.txt",
    "batch social sucrose/gateLog - 2023-01-25 femhetda378(og)12w.log.txt",
    ]
    '''

    files=[ "batch social sucrose/gateLog - 2023-02-20 malewtda372(od)12w-setup1.log.txt",
           "batch social sucrose/gateLog - 2023-03-01 malewtda372(od)12w-setup2.log.txt"
    ]

    for d in data:
        print ( d )

    files.reverse()

    i = 0
    #for d in data.values():
    for f in files:

        d = data[f]
        animals.append( f"{i}: {d['genotype']} {d['sex']} {d['age']}w\n{f.split(' ')[-1].split('.')[0]}" )
        x = 0
        endedTooSoon = False
        for p in range( 6 ):
            val = d["phaseDurations"][p]
            if val == None:
                val = 0
                if not endedTooSoon:
                    #ax.barh( y=i, width = 60*10, left=x, height=0.8, color='black' )
                    endedTooSoon = True

            #ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color)
            color = colors[p]
            rects = ax.barh( y=i, width = val, left=x, height=0.8, color=color, label="test")

            r, g, b, _ = color
            text_color = 'white' if r * g * b < 0.5 else 'darkgrey'
            if p!=5:
                if val!=0:
                    ax.text( x+val/2,i , toHM( val ) ,  ha="center", va="center" , color=text_color )

            #if p == 3: # phase 4
            if p ==5 :

                endPhase6 = d[ "endTimePhase6S"]
                if endPhase6 != None:
                    ax.barh( y=i, width = 600, left=x+endPhase6, height=0.9, color="red", label="end")
                    print( "duration : " , x, endPhase6, endPhase6-x )
                    ax.text( x+endPhase6/2, i , "goal reached in " + toHM( endPhase6 ) ,  ha="center", va="center" , color=text_color )

            x+=val

        i+=1

    animalLabels = []
    for animal in animals:
        animalLabels.append( animal.split(":")[-1] )
    plt.yticks( range( len( animals ) ), animalLabels )
    plt.xlim( 0 , 60*60*48 )

    ''' set x axis '''
    formatter = matplotlib.ticker.FuncFormatter( frameToTimeTicker )
    ax.xaxis.set_major_formatter(formatter)
    ax.tick_params(labelsize=8 )
    ax.xaxis.set_major_locator(ticker.MultipleLocator( 60 * 60 ))
    #ax.xaxis.set_minor_locator(ticker.MultipleLocator( 30 * 60 * 60 ))

    #colors = plt.get_cmap('hsv')( np.linspace(0.15, 0.85, 6 ))
    #print( colors )

    #ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color)

    '''
    b1 = plt.barh(animals, phases[0], color = colors[0] , stacked=True )
    b2 = plt.barh(animals, phases[1], color = colors[1] , stacked=True  )
    '''
    '''
    b3 = plt.barh(animals, phases[2], color = colors[2] )
    b4 = plt.barh(animals, phases[3], color = colors[3] )
    b5 = plt.barh(animals, phases[4], color = colors[4] )
    b6 = plt.barh(animals, phases[5], color = colors[5] )
    '''

    #plt.gca().axvline( 60 *60 * 47 )
    #plt.text( 1000 + 60 *60 * 48, 0, "47h experiment", rotation=90 )

    #b2 = plt.barh(year, issues_pending, left=issues_addressed, color="yellow")
    #plt.legend([b1, b2,b3,b4,b5,b6], ["Phase 1", "Phase 2","Phase 3","Phase 4", "Phase 5", "Phase 6"], title="Time in phases", loc="upper right")

    plt.tight_layout()
    plt.show()

if __name__ == '__main__':

    #compute()

    analyse()

    print("Done.")
    #plt.show()


import re
from .cmudict import CMUDict

_letter_to_arpabet = {
    'A': 'EY1',
    'B': 'B IY1',
    'C': 'S IY1',
    'D': 'D IY1',
    'E': 'IY1',
    'F': 'EH1 F',
    'G': 'JH IY1',
    'H': 'EY1 CH',
    'I': 'AY1',
    'J': 'JH EY1',
    'K': 'K EY1',
    'L': 'EH1 L',
    'M': 'EH1 M',
    'N': 'EH1 N',
    'O': 'OW1',
    'P': 'P IY1',
    'Q': 'K Y UW1',
    'R': 'AA1 R',
    'S': 'EH1 S',
    'T': 'T IY1',
    'U': 'Y UW1',
    'V': 'V IY1',
    'X': 'EH1 K S',
    'Y': 'W AY1',
    'W': 'D AH1 B AH0 L Y UW0',
    'Z': 'Z IY1',
    's': 'Z'
}

# must ignore roman numerals
_acronym_re = re.compile(r'([A-Z][A-Z]+)s?|([A-Z]\.([A-Z]\.)+s?)')
cmudict = CMUDict('data/cmudict_dictionary', keep_ambiguous=False)

def _expand_acronyms(m, add_spaces=True):
    acronym = m.group(0)

    # remove dots if they exist
    acronym = re.sub('\.', '', acronym)

    acronym = "".join(acronym.split())
    arpabet = cmudict.lookup(acronym)

    if arpabet is None:
        acronym = list(acronym)
        arpabet = ["{" + _letter_to_arpabet[letter] + "}" for letter in acronym]
        # temporary fix
        if arpabet[-1] == '{Z}' and len(arpabet) > 1:
            arpabet[-2] = arpabet[-2][:-1] + ' ' + arpabet[-1][1:]
            del arpabet[-1]

        arpabet = ' '.join(arpabet)
    else:
        arpabet = "{" + arpabet[0] + "}"

    return arpabet

def normalize_acronyms(text):
    text = re.sub(_acronym_re, _expand_acronyms, text)
    return text


from art import logo
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]

print(logo)
restart=""
def ceasar(direction,text,shift):
  if direction[0]=='e':
    if shift>26:
      shift=shift%26    #If the person entors a shift number greater than 26, we will modify it to be such thst to which we can calculate
    cipher_text=""
    for letter in text:
      if letter in alphabet:
        if (shift+alphabet.index(letter))>=26:  #If the letter need to go back to the beginning because it exceeds its limit
            cipher_text+=alphabet[shift-(26-alphabet.index(letter))]
        else:
            cipher_text+=alphabet[alphabet.index(letter)+shift]
      else:
        cipher_text+=letter
    print(f"The encoded text is {cipher_text}")

  elif direction[0]=='d':
     if shift>26:
       shift=shift%26  #If the person entors a shift number greater than 26, we will modify it to be such thst to which we can calculate
     plain_text=""
     for letter in text:
       if letter in alphabet:
         if (alphabet.index(letter)-shift)<0: #If the letter goes below "a" then we execute as a circular loop and start from the end
           plain_text+=alphabet[26-(shift-alphabet.index(letter))]
         else:
           plain_text+=alphabet[alphabet.index(letter)-shift]
       else:
         plain_text+=letter
     print(f"The decoded text is {plain_text}")
  else:
    print("Wrong choice my friend, \nWrong Choice.")
end=False
while not end:
  direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
  text = input("Type your message:\n").lower()
  shift = int(input("Type the shift number:\n"))
  ceasar(direction,text,shift)
  restart=input("Do you want to run again? :").lower()
  if restart[0]!='y':
    end=True
    print("GoodBye.")


import struct
import sys
import os

def readUShort(file):
    return struct.unpack("H",file.read(2))[0]

def readUInt(file):
    return struct.unpack("I",file.read(4))[0]

class TXPFile:
    def __init__(self,name,offset):
        self.name = name
        self.offset = offset
        self.length = 0

def ExportTXPs(inFile):
    fileCount = readUShort(inFile)
    fileInfo = []
    for _ in range(fileCount):
        nameSize = int(inFile.read(1)[0])
        name = inFile.read(nameSize)
        offset = readUInt(inFile)
        fileInfo.append(TXPFile(name.decode('utf-8'),offset))
    #generate lengths
    for i in range(len(fileInfo)):
        if i == len(fileInfo)-1:
            fileInfo[i].length = os.stat(inFile.name).st_size - fileInfo[i].offset
        else:
            fileInfo[i].length = fileInfo[i+1].offset - fileInfo[i].offset
    for file in fileInfo:
        #now we have all of the info needed
        basePath = inFile.name.split('.')[0]+'/'
        os.makedirs(basePath,exist_ok=True)
        nFile = open(basePath+file.name+".txp",'wb')
        inFile.seek(file.offset)
        nFile.write(inFile.read(file.length))
        nFile.close()

if __name__ == "__main__":
    for i, arg in enumerate(sys.argv):
        if i > 0:
            ExportTXPs(open(arg,'rb'))


from time import time
from texttable import Texttable

def test(matrix):
    times = [len(matrix.get_matrix()[0])]
    start = time()
    result = matrix.naive_search()
    stop = time()
    times.append(stop - start)
    times.append(float(len(result)))
    start = time()
    result = matrix.rabin_karp()
    stop = time()
    times.append(stop - start)
    times.append(float(len(result)))
    return times

class Table:
    def __init__(self, path):
        file = open(path)
        self.matrix = [k.strip() for k in file]
        file.close()

    def get_matrix(self):
        return self.matrix

    def print_table(self):
        print(self.matrix)

    def naive_search(self, pattern='ABC'):
        result = []
        for x in range(len(self.matrix) - len(pattern)):
            for y in range(len(self.matrix[x])):
                if self.matrix[x][y] == pattern[0]:
                    if self.matrix[x][y:y + len(pattern)] == pattern:
                        if pattern == "".join(self.matrix[k][y] for k in range(x, x + len(pattern))):
                            result.append((x, y))
        return result

    def rabin_karp(self, pattern='ABC', d=16, q=13):
        matrixlen = len(self.matrix)
        patternlen = len(pattern)
        h = (d ** (patternlen - 1)) % q
        result = set()
        for x in range(matrixlen - 2):
            p = 0
            t = 0
            for i in range(patternlen):
                p = (d * p + ord(pattern[i])) % q
                t = (d * t + ord(self.matrix[x][i])) % q
            for y in range(matrixlen - patternlen + 1):
                if p == t:
                    if self.matrix[x][y:y + patternlen] == pattern:
                        if pattern == "".join(self.matrix[k][y] for k in range(x, x + patternlen)):
                            result.add((x, y))
                if y < matrixlen - patternlen:
                    t = ((t - h * ord(self.matrix[x][y])) * d + ord(self.matrix[x][y + patternlen])) % q
        return result

p1, p2, p3, p4, p5, p8 = "patterns/1000_pattern.txt", "patterns/2000_pattern.txt", \
                         "patterns/3000_pattern.txt", "patterns/4000_pattern.txt", \
                         "patterns/5000_pattern.txt", "patterns/8000_pattern.txt"
matrix1, matrix2, matrix3, matrix4, matrix5, matrix8 = Table(p1), Table(p2), Table(p3), Table(p4), Table(p5), Table(p8)
times = []

times.append(test(matrix1))
times.append(test(matrix2))
times.append(test(matrix3))
times.append(test(matrix4))
times.append(test(matrix5))
times.append(test(matrix8))

table = Texttable()
table.add_rows([["size", "naive_time", "naive_result", "rabin_karp_time", "rabin_karp_result"],
                [times[0][0], times[0][1], times[0][2], times[0][3], times[0][4]],
                [times[1][0], times[1][1], times[1][2], times[1][3], times[1][4]],
                [times[2][0], times[2][1], times[2][2], times[2][3], times[2][4]],
                [times[3][0], times[3][1], times[3][2], times[3][3], times[3][4]],
                [times[4][0], times[4][1], times[4][2], times[4][3], times[4][4]],
                [times[5][0], times[5][1], times[5][2], times[5][3], times[5][4]]])

f = open('times.txt', 'w')
f.write(table.draw())
f.close()


import os
import cv2
import json
import bezier
import numpy as np

def four00Points(points):
    new_points = []
    for i in range(len(points) - 1):
        pi, pj = i, i + 1
        new_points.append(points[pi])
        center = ((points[pi][0] + points[pj][0]) / 2, (points[pi][1] + points[pj][1]) / 2)
        new_points.append(center)
    new_points.append(points[-1])
    return new_points

def approximate_curved_polygon(_contour, point_num=200):
    """
    使用贝塞尔曲线进行拟合,得到平滑的闭合多边形轮廓
    :param _contour: 构成多边形轮廓的点集. Array:(N, 2)
    :param point_num: 每次拟合的点的数量,越大则越平滑. Int
    :return: 返回平滑后的轮廓点
    """
    to_return_contour = []
    _contour = np.reshape(_contour, (-1, 2))
    # 复制起始点到最后,保证生成闭合的曲线
    # _contour = np.vstack((_contour, _contour[0, :].reshape((-1, 2))))
    for start_index in range(0, _contour.shape[0], point_num):
        # 多取一个点,防止曲线中间出现断点
        end_index = start_index + point_num + 1
        end_index = end_index if end_index < _contour.shape[0] else _contour.shape[0]
        nodes = np.transpose(_contour[start_index:end_index, :])
        # 拟合贝塞尔曲线
        curve = bezier.Curve(nodes, degree=nodes.shape[1] - 1)
        curve = curve.evaluate_multi(np.linspace(0.0, 1.0, point_num * 5))
        to_return_contour.append(np.transpose(curve))
    to_return_contour = np.array(to_return_contour).reshape((-1, 2))
    return to_return_contour

def select_seven_points(ori_points):
    points = ori_points
    while len(points) < 40:
        points = four00Points(points)
    points = np.array(points)
    res_point = approximate_curved_polygon(points)
    num = (len(res_point)) // 6
    mod = len(res_point) % 6
    new_points = []
    i = 0
    count = 0
    while i < len(res_point):
        new_points.append(res_point[i])
        if count == mod:
            i += num
        else:
            i += num + 1
            count += 1
    new_points.append(ori_points[-1])
    new_points = np.array(new_points)
    return new_points

def feed_data(image_dict, data):
    image_name, number, content, new_points, top_line, end_line = data
    if number in image_dict[image_name]:
        if top_line:
            image_dict[image_name][number].update({"content": content, "top_line": new_points})
        else:
            image_dict[image_name][number].update({"end_line": new_points})
    else:
        if top_line:
            image_dict[image_name].update({number:{"content": content, "top_line": new_points}})
        else:
            image_dict[image_name].update({number:{"end_line": new_points}})
    return image_dict

def write_label_with_PgNet(fp, label_dict):
    write_line = ""
    for image_name in label_dict.keys():
        try:
            write_line = image_name + "\t"
            label_str = ""
            for num in label_dict[image_name].keys():
                content = label_dict[image_name][num]["content"]
                top_line = label_dict[image_name][num]["top_line"].tolist()
                end_line = label_dict[image_name][num]["end_line"].tolist()
                points = top_line + end_line
                write_str = "{" + '"{}": "{}", "{}": {}'.format("transcription", content, "points", points) + "}"
                if len(label_str) < 1:
                    label_str = write_str
                else:
                    label_str = label_str + ',' + write_str
            write_line = write_line + "[" + label_str + "]" + "\n"
        except Exception as e:
            print(e)
    fp.write(write_line)

def data_preproces(root_dir):
    '''
    root_dir: 目录下存放标注的文件
    '''
    fw = open("{}_label.txt".format(root_dir), "w")
    for sub_dir_root in os.listdir(root_dir):
        print(sub_dir_root)
        image_dir = os.path.join(root_dir, sub_dir_root)
        if not os.path.isdir(image_dir):
            continue
        json_file = '{}.json'.format(image_dir)
        with open(json_file) as fr:
            label_dict = json.load(fr)
        img_metadata = label_dict['_via_img_metadata']
        for key in img_metadata.keys():
            image_dict = {}
            sub_dir = img_metadata[key]
            image_name = sub_dir['filename']
            image_dict[image_name] = {}
            image = cv2.imread(os.path.join(image_dir, image_name))
            if image is None:
                continue
            regions = sub_dir["regions"]
            if len(regions) < 1:
                continue
            for sub_regions in regions:
                try:
                    shape_attributes = sub_regions["shape_attributes"]
                    all_points_x = shape_attributes["all_points_x"]
                    all_points_y = shape_attributes["all_points_y"]
                    points = np.vstack((all_points_x, all_points_y))
                    points = np.transpose(points)
                    cv2.polylines(image, [points], False, (255, 0, 0), 5, 1)
                    new_points = select_seven_points(points)        # 平均选7个点
                    region_attributes = sub_regions["region_attributes"]
                    number = region_attributes["number"]
                    top_line, end_line = False, False
                    if "top_line" in region_attributes["line"].keys():
                        content = region_attributes["content"]      # 选取文本信息
                        top_line = True
                    if "end_line" in region_attributes["line"].keys():
                        end_line = True
                        content = ""
                    if "\n" in content:
                        print(image_name)
                        print("###################")
                        exit()
                    image_dict  = feed_data(image_dict, [image_name, number, content, new_points, top_line, end_line])
                except Exception as e:
                    print(image_name)
                    print(e, "@@@@@@@@@@@@@@@@@@@@@@@@")
            write_label_with_PgNet(fw, image_dict)
    fw.close()

def main():
    data_preproces()

if __name__ == '__main__':
    main()


# coding: utf-8
import re
from datetime import datetime
from sqlalchemy.exc import IntegrityError
from builder.exceptions import InvalidRoleName, RoleAlreadyExist, RoleNotFound
from builder.models import Model, db

class Role(Model):
    id = db.Column(db.Integer, primary_key=True)
    __tablename__ = 'role'

    name = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.String(255))
    active = db.Column(db.Boolean(), default=True)
    created_at = db.Column(db.DateTime, index=True, default=datetime.now())

    @property
    def is_active(self):
        """Return current status of role"""
        return self.active

    @classmethod
    def create(cls, name, description=None):
        """Create new role"""
        if not cls.verify_role_name(name):
            raise InvalidRoleName

        try:
            role = Role()
            role.name = name
            role.description = description
            role.save()
            db.session.commit()
            return role
        except IntegrityError:
            raise RoleAlreadyExist

    def edit(self, name=None, description=None):
        """Edit existent role"""
        try:
            if name:
                if not self.verify_role_name(name):
                    raise InvalidRoleName
                self.name = name
            if description:
                self.description = description
            self.save()
            db.session.commit()
        except IntegrityError:
            raise RoleAlreadyExist
        return self

    @classmethod
    def search_role(cls, name, exactly=False):
        """Search role by name, exactly or not"""
        if exactly:
            role = cls.query.filter(Role.name == name).first()
        else:
            role = cls.query.filter(Role.name.contains(name)).all()
        if not role:
            raise RoleNotFound
        return role

    @staticmethod
    def verify_role_name(role_name):
        """Check length and if the username does not contains invalid chars"""
        if len(role_name) >= 3 and re.search(r'^[a-zA-Z0-9_.-]+$', role_name):
            return True


import json
import sys
import numpy as np
from scipy.signal import find_peaks
from scipy.ndimage import grey_erosion, grey_dilation

def detect_eeg_peaks(signal):
    # Define a function for opening operation
    def opening_operation(signal, distance):
        erosion_result = grey_erosion(signal, size=distance)
        dilation_result = grey_dilation(erosion_result, size=distance)
        return dilation_result

    # Define a function for closing operation
    def closing_operation(signal, distance):
        dilation_result = grey_dilation(signal, size=distance)
        erosion_result = grey_erosion(dilation_result, size=distance)
        return erosion_result

    # Define a function for average OC/CO operation
    def average_occo(signal, distance):
        opening_result = opening_operation(signal, distance)
        closing_result = closing_operation(signal, distance)
        occo_result = (opening_result + closing_result) / 2
        return occo_result

    # Define a function to apply the filter
    def apply_filter(signal, distance):
        occo_result = average_occo(signal, distance)
        filtered_signal = signal - occo_result
        return filtered_signal

    # Define a function to calculate the threshold
    def calculate_threshold(filtered_signal):
        extrema_amplitudes = np.abs(filtered_signal[find_peaks(filtered_signal)[0]])
        threshold = 8 * np.median(extrema_amplitudes)
        return threshold

    # Apply the filter to the original EEG signal
    filtered_signal = apply_filter(signal, distance=len(signal)//10)  # Adjust the distance as needed

    # Calculate the threshold
    threshold = calculate_threshold(filtered_signal)

    # Count the peaks above the threshold with width and prominence criteria
    detected_peaks_indices, _ = find_peaks(filtered_signal, height=threshold, width=40, prominence=237)

    detected_peaks_count = len(detected_peaks_indices)

    result = {
        "peaks_count": detected_peaks_count,
        # "peaks_array": detected_peaks_indices.tolist(),
    }

    return result

if __name__ == "__main__":
    try:
        input_data = json.loads(sys.stdin.read())

        # Check for required fields
        if "signal" not in input_data:
            raise ValueError("Missing required fields in input data")

        signal = input_data["signal"]

        result = detect_eeg_peaks(signal)

        print(json.dumps(result))
    except Exception as e:
        print(json.dumps({"error": str(e)}))


#OS - Data Preprocessing
#Author: Nic Gardin

import pandas as pd
import numpy as np
import time

def getCrimeKeysVan(df):

	startTime = time.perf_counter()
	vanc = pd.read_csv("/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crime_datasets/crimeVan.csv", encoding="UTF-8")
	crimeKeys = pd.DataFrame(-1, index=np.arange(len(df)),columns=['crimeKey', 'isNighttime'])

	#1. for Van
	for i in range(len(df)):
		hour = vanc.iloc[i]["HOUR"]
		minute = vanc.iloc[i]["MINUTE"]

		if pd.isnull(hour) or pd.isnull(minute):
			continue

		repTime =  str(int(hour)) + ":" + str(int(minute))
		details = vanc.iloc[i]["TYPE"]

		#get specific crime from crimeDim.csv
		sCrime = df.loc[(df['reportedTime'] == repTime) & (df['crimeDetails'] == details)]

		key = sCrime['crimeKey'].iloc[0]
		crimeKeys.loc[i] = key

		isNight = sCrime['timeOfDay'].iloc[0]

		if (isNight==0) or (isNight == 3):
			crimeKeys['isNighttime'].loc[i] = 1
		else:
			crimeKeys['isNighttime'].loc[i] = 0

		if(i%10000 == 0):
			# endTime = time.perf_counter()
			# print(endTime-startTime)
			# print(i)
			print(repTime + "    " + details + "    " + str(key))

	return crimeKeys

def getCrimeKeysDen(df):

	startTimeCounter = time.perf_counter()
	denc = pd.read_csv("/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crime_datasets/crimeDen.csv", encoding="UTF-8")
	denc.dropna(subset=['FIRST_OCCURRENCE_DATE'])

	denc['REPORTED_DATE'] = pd.to_datetime(denc['REPORTED_DATE'], format='%m/%d/%Y %I:%M:%S %p')
	denc['REPORTED_DATE'] = denc['REPORTED_DATE'].astype(str)

	crimeKeys = pd.DataFrame(-1, index=np.arange(len(df)),columns=['crimeKey', 'isNighttime'])

	df.dropna(subset=['crimeStartTime'])

	for i in range(len(df)):

		try:
			reportedTime = denc.iloc[i]["REPORTED_DATE"]
			if pd.isnull(reportedTime):
				continue

			startTime = denc.iloc[i]['FIRST_OCCURRENCE_DATE']

			startTime = startTime.split(" ")[0]

			startTimeSpl = startTime.split("/")
			#append leading 0s
			if (len(startTimeSpl[0]) == 1):
				startTime = "0" + startTime

			if (len(startTimeSpl[1]) == 1):
				startTime = startTime[0:3] + "0" + startTime[3:]

			repTime = reportedTime.split(" ")[1]
			repTime = repTime[:-3]
			details = denc.iloc[i]["OFFENSE_TYPE_ID"]

			# print(repTime + "    " + details + "    " + startTime)

			#get specific crime from denCrimeDim.csv
			# if (pd.isnull((df.loc[df['crimeStartTime']]))):
			# 	continue
			sCrime = df.loc[(df['reportedTime'] == repTime) & (df['crimeStartTime'] == startTime) & (df['crimeDetails'] == details)]

			key = sCrime['crimeKey'].iloc[0]
			crimeKeys.loc[i] = key

			isNight = sCrime['timeOfDay'].iloc[0]
			if pd.isnull(isNight):
				continue

			if (isNight==0) or (isNight == 3):
				crimeKeys['isNighttime'].loc[i] = 1
			else:
				crimeKeys['isNighttime'].loc[i] = 0

			# print("Row " + str(i) + " info:  " + repTime + "    "  + details + "    " + startTime + "\nKey found: "  + str(key) + "\n") # + "\nKey found: "  + str(key) + "\n"

			if(i%10000 == 0):
				endTimeCounter = time.perf_counter()
				print(endTimeCounter-startTimeCounter)
				print("Row " + str(i) + " info:  " + repTime + "    "  + details + "    " + startTime + "\nKey found: "  + str(key) + "\n") # + "\nKey found: "  + str(key) + "\n"

		except:
			continue

	return crimeKeys

if __name__ == '__main__':

	#crimeDim
	crime_df = pd.read_csv("/Users/nicgardin/Documents/2020_Classes_Master/DataScience/CSI4142_Group16_CrimeDataProject/crimeDim.csv", encoding="UTF-8")

	#split crimeDim.csv
	denCrimeDim = crime_df.head(461699)
	vanCrimeDim = crime_df.tail(17352)

	# vanCrimeDim.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/vanSet.csv', index=False)
	# denCrimeDim.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/denSet.csv', index=False)

	# create and write vanCrimeKeys.csv
	vanCrimeKeys = getCrimeKeysVan(vanCrimeDim)
	vanCrimeKeys.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/vanCrimeKeys.csv', index=False)

	# create and write denCrimeKeys.csv (SLOW)
	denCrimeKeys = getCrimeKeysDen(denCrimeDim)
	denCrimeKeys.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/denCrimeKeys.csv', index=False)

	# create and write denCrimeKeys.csv (FAST)
	# split into groups of 100,000
	# denCrimeDim1 = denCrimeDim.head(100000)
	# denCrimeDim2 = denCrimeDim1.head(100000)
	# denCrimeDim3 = denCrimeDim2.head(100000)
	# denCrimeDim4 = denCrimeDim3.head(100000)
	# denCrimeDim5 = denCrimeDim4.head(61685)

	# # create
	# denCrimeKeys1 = getCrimeKeysDen(denCrimeDim1)
	# denCrimeKeys2 = getCrimeKeysDen(denCrimeDim2)
	# denCrimeKeys3 = getCrimeKeysDen(denCrimeDim3)
	# denCrimeKeys4 = getCrimeKeysDen(denCrimeDim4)
	# denCrimeKeys5 = getCrimeKeysDen(denCrimeDim5)

	# # append
	# denCrimeKeysArr = [denCrimeKeys1,denCrimeKeys2,denCrimeKeys3,denCrimeKeys4,denCrimeKeys5]
	# denCrimeKeys = pd.concat(denCrimeKeysArr)

	# write final denCrimeKeys.csv
	# denCrimeKeys.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/denCrimeKeys.csv', index=False)\

	# merge Denver and Vancouver datasets
	# vanCrimeKeys = pd.read_csv("/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/vanCrimeKeys.csv")
	# denCrimeKeys = pd.read_csv("/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/denCrimeKeys.csv")
	# denCrimeKeys.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/vanCrimeKeys.csv', mode='a', header=False)
	# denCrimeKeys.to_csv('/Users/nicgardin/Documents/2020_Classes_Master/DataScience/crimeKeys/crimeKeys.csv', index=False)


from tkinter import *
from tkinter import ttk
import tkinter as tk

##TODO: Add dropdowns to select control and guidance system

class EditCarUI(): #{
    ##-----------------------------------------------------------------------------
    ## Constructor
    ##-----------------------------------------------------------------------------
    def __init__(self, parent): #{
        self.parent = parent
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the Add New Car Window
    ##-----------------------------------------------------------------------------
    def createWindow(self, car): #{
        self.car = car
        self.destroyWindow() #Called in case the user tries to open multiple windows
        self.window = Tk()

        #Sets the geometry and title on the window
        self.window.geometry('500x300+200+50')
        self.window.wm_iconbitmap('../ccs.ico')
        self.window.title('Edit Car')
        self.window.resizable(0, 0)

        self.createf1()
        self.createf2(car.name)
        self.createf3()
        self.createf4(car.IP)
        self.createf5()
        self.createf6(car.port)
        self.createf7()
        self.createf8(car.control)
        self.createf9()
        self.createf10(car.guidance)
        self.createf11()
        self.createf12()

        self.carNameEntry.focus_force()
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f1
    ##-----------------------------------------------------------------------------
    def createf1(self): #{
        self.f1 = Frame(self.window)
        self.f1.pack(fill=X)

        blankLabel = Label(self.f1, text=' ')
        blankLabel.config(font=("Tahoma", 11))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f2
    ##-----------------------------------------------------------------------------
    def createf2(self, carName): #{
        self.f2 = Frame(self.window)
        self.f2.pack(fill=X)

        carNameLabel = Label(self.f2, text='   Enter Car Name:  ')
        carNameLabel.config(font=("Tahoma", 11))
        carNameLabel.pack(side='left')

        self.carNameEntry = Entry(self.f2, width=55)
        self.carNameEntry.insert(0, str(carName))
        self.carNameEntry.pack(side='left')
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f3
    ##-----------------------------------------------------------------------------
    def createf3(self): #{
        self.f3 = Frame(self.window)
        self.f3.pack(fill=X)

        blankLabel = Label(self.f3, text=' ')
        blankLabel.config(font=("Tahoma", 4))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f4
    ##-----------------------------------------------------------------------------
    def createf4(self, IP): #{
        self.f4 = Frame(self.window)
        self.f4.pack(fill=X)

        IPLabel = Label(self.f4, text='   Enter Car IP:       ')
        IPLabel.config(font=("Tahoma", 11))
        IPLabel.pack(side='left')

        self.IPEntry = Entry(self.f4, width=25)
        self.IPEntry.insert(0, str(IP))
        self.IPEntry.pack(side='left')
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f5
    ##-----------------------------------------------------------------------------
    def createf5(self): #{
        self.f5 = Frame(self.window)
        self.f5.pack(fill=X)

        blankLabel = Label(self.f5, text=' ')
        blankLabel.config(font=("Tahoma", 4))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f6
    ##-----------------------------------------------------------------------------
    def createf6(self, port): #{
        self.f6 = Frame(self.window)
        self.f6.pack(fill=X)

        portLabel = Label(self.f6, text='   Enter Car Port:    ')
        portLabel.config(font=("Tahoma", 11))
        portLabel.pack(side='left')

        self.portEntry = Entry(self.f6, width=25)
        self.portEntry.insert(0, str(port))
        self.portEntry.pack(side='left', padx=3)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f7
    ##-----------------------------------------------------------------------------
    def createf7(self): #{
        self.f7 = Frame(self.window)
        self.f7.pack(fill=X)

        blankLabel = Label(self.f7, text=' ')
        blankLabel.config(font=("Tahoma", 11))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f8
    ##-----------------------------------------------------------------------------
    def createf8(self, controlSystem): #{
        self.f8 = Frame(self.window)
        self.f8.pack(fill=X)

        controlSystemLabel = Label(self.f8, text='   Select Control System:     ')
        controlSystemLabel.config(font=("Tahoma", 11))
        controlSystemLabel.grid(row=0,column=0)

        #self.controlOptions = StringVar()
        #self.controlOptions['values'] = self.parent.getControlSystems()

        vals = self.parent.getControlSystems()
        index = 0
        for i in range(0, len(vals)):
            if vals[i] == controlSystem:
                index = i

        self.controlOptionList = ttk.Combobox(self.f8, values=vals)
        self.controlOptionList.grid(row=0,column=1)
        self.controlOptionList.current(index)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f9
    ##-----------------------------------------------------------------------------
    def createf9(self): #{
        self.f9 = Frame(self.window)
        self.f9.pack(fill=X)

        blankLabel = Label(self.f9, text=' ')
        blankLabel.config(font=("Tahoma", 11))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f10
    ##-----------------------------------------------------------------------------
    def createf10(self, guidanceSystem): #{
        self.f10 = Frame(self.window)
        self.f10.pack(fill=X)

        guidanceSystemLabel = Label(self.f10, text='   Select Guidance System:  ')
        guidanceSystemLabel.config(font=("Tahoma", 11))
        guidanceSystemLabel.grid(row=0,column=0)

        vals = self.parent.getGuidanceSystems()
        index = 0
        for i in range(0, len(vals)):
            if vals[i] == guidanceSystem:
                index = i

        self.guidanceOptionList = ttk.Combobox(self.f10, values=vals)
        self.guidanceOptionList.grid(row=0,column=1)
        self.guidanceOptionList.current(index)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f11
    ##-----------------------------------------------------------------------------
    def createf11(self): #{
        self.f11 = Frame(self.window)
        self.f11.pack(fill=X)

        blankLabel = Label(self.f11, text=' ')
        blankLabel.config(font=("Tahoma", 11))
        blankLabel.grid(row=0,column=0)
    #}

    ##-----------------------------------------------------------------------------
    ## Creates the frame f12
    ##-----------------------------------------------------------------------------
    def createf12(self): #{
        self.f12 = Frame(self.window)
        self.f12.pack(fill=X)

        blankLabel = Label(self.f12, text='\t\t  ')
        blankLabel.config(font=("Tahoma", 8))
        blankLabel.grid(row=0,column=0)

        self.saveChangesButton = Button(self.f12, text='Save Changes', command=self.saveCarSettings, width=15)
        self.saveChangesButton.config(font=("Tahoma", 10))
        self.saveChangesButton.grid(row=0,column=1)

        blankLabel1 = Label(self.f12, text='\t')
        blankLabel1.config(font=("Tahoma", 8))
        blankLabel1.grid(row=0,column=2)

        self.deleteCarButton = Button(self.f12, text='Delete Car', command=self.deleteCarFrame, width=15)
        self.deleteCarButton.config(font=("Tahoma", 10))
        self.deleteCarButton.grid(row=0,column=3)
    #}

    ##-----------------------------------------------------------------------------
    ## Collects info from entries and sends back to config UI
    ##-----------------------------------------------------------------------------
    def saveCarSettings(self): #{
        self.car.name = self.carNameEntry.get()
        self.car.IP = self.IPEntry.get()
        self.car.port = self.portEntry.get()

        self.parent.updateCarFrame(self.car)

        self.destroyWindow()
    #}

    ##-----------------------------------------------------------------------------
    ## Sends back the car frame to delete
    ##-----------------------------------------------------------------------------
    def deleteCarFrame(self): #{
        self.parent.deleteCarFrame(self.car)
        self.destroyWindow()
    #}

    ##-----------------------------------------------------------------------------
    ## Deletes the editCar Window
    ##-----------------------------------------------------------------------------
    def destroyWindow(self): #{
        try:
            self.window.destroy()
        except:
            print('Error destroying editCar UI')
    #}
#}


import datetime
import requests
# import plotly.plotly as py
import numpy as np
import matplotlib.pyplot as plt

# /repos/:owner/:repo/stats/punch_card
url = 'https://api.github.com/repos/mbostock/d3/stats/commit_activity'
#url = 'https://api.github.com/repos/mbostock/d3/stats/punch_card'

r = requests.get(url)
data = r.json()

week_days = [0,0,0,0,0,0,0]
week_days_text = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
week_total = []
week = []

for w in data:
    week_total.append(w ['total'])
    week.append(w ['week'])
    week_days[0] += w['days'][0]
    week_days[1] += w['days'][1]
    week_days[2] += w['days'][2]
    week_days[3] += w['days'][3]
    week_days[4] += w['days'][4]
    week_days[5] += w['days'][5]
    week_days[6] += w['days'][6]

# print(max(week_total))
index = week_total.index(max(week_total))
# print(week_total.index(max(week_total)))
# print(week[index])
# print(datetime.date(week[index]).isoformat())

print(datetime.datetime.fromtimestamp(week[index]).isocalendar()[0])
print(datetime.datetime.fromtimestamp(week[index]).isocalendar()[1])

week_year = str(datetime.datetime.fromtimestamp(week[index]).isocalendar()[0])\
            + '-' + str(datetime.datetime.fromtimestamp(week[index]).isocalendar()[1])

print("-----------------------------")
print(" The maximum number of commits")
print(" Week: {} - Commits: {}".format(week_year, max(week_total) ))
print("-----------------------------")
print(week_days)
print(max(week_days))
print(week_days_text[week_days.index(max(week_days))])

# date = plt.figure()
#
# x =  week_days_text
# y = week_days
#
# ax = plt.subplot(111)
# ax.bar(x, y, width=10)
#
# plot_url = py.plot_mpl(date, filename='mpl-date-example')