import numpy as np
import matplotlib.pyplot as plt

def RK4(f, y0, x1, xN, N, coeffs):
    step = (xN - x1) / (N - 1)
    t, y = x1, y0
    result = [(t, y)]
    while t < xN:
        k1 = step * f(y, t, *coeffs)
        k2 = step * f(y + k1/2, t + step/2, *coeffs)
        k3 = step * f(y + k2/2, t + step/2, *coeffs)
        k4 = step * f(y + k3/2, t + step, *coeffs)
        y += (0.166666666) * (k1 + 2*k2 + 2*k3 + k4)
        t += step
        result.append((t, y))
    return np.array(result)

def vRK4(f, y0, x1, xN, N, coeffs):
    assert( np.shape(f) == np.shape(y0) )
    assert( np.shape(f)[0] == np.shape(coeffs)[0] )

    # Calls each function in f_arr
    #with it's respective coeffs in c_arr.
    def caller(f_arr, t, p_arr, c_arr):
        return np.array([f(t, [*p, *c]) for f, p, c in zip(f_arr, p_arr, c_arr)])

    step = (xN - x1) / (N - 1)

    times = np.linspace(x1, xN, N)
    result = np.zeros((N, len(y0) + 1))

    # Set initial conditions
    result[0, 1:] = y0
    result[:, 0] = times

    for i in range(1, len(times)):
        t = times[i - 1]

        # [ [t, x, y, rabbit_coeffs],
        #   [t, x, y, fox_coeffs] ]
        # Construct initial coefficients
        params = np.array([ result[i-1, 1:] for j in range(len(f)) ])

        k1 = step * caller(f, t, params, coeffs)
        k2 = step * caller(f, t + step/2, params + k1/2, coeffs)
        k3 = step * caller(f, t + step/2, params + k2/2, coeffs)
        k4 = step * caller(f, t + step, params + k3/3, coeffs)

        y = params + (0.166666666) * (k1 + 2*k2 + 2*k3 + k4)
        result[i, 1:] = np.diag(y)

    return result

def rabbit_pop(t, r):
    x, y, a, b = r
    return [a * x - b * x * y, y]
def fox_pop(t, r):
    x, y, gamma, delta = r
    return [x, gamma * x * y - delta * y]

f = [rabbit_pop, fox_pop]
y0 = [2, 2]
coeffs = [[1, 0.5],
          [0.5, 2]]

model = vRK4(f, y0, 0, 50, 50000, coeffs)

plt.plot(model[:, 0], model[:, 1], c='r', label='Rabbits')
plt.plot(model[:, 0], model[:, 2], c='c', label='Foxes')
plt.axhline(7)
plt.legend()
plt.show()


import os
import pymongo

from pandas import DataFrame

from datab.db_manager import *
from common.utils import Util

def init_check(func):
    def wrap(*args, **kwargs):
        if not args[0]._client:
            raise DbException("Not initialized")
        return func(*args, **kwargs)
    return wrap

class Mongo(BaseDriver):
    """
    Provides some high level logic over MongoDB.
    """

    def __init__(self):
        self._client = None
        self._dbs = []
        self._active_db = None

    def init(self):
        try:
            self._client = pymongo.MongoClient('mongodb://localhost:27017/')
        except Exception as e:
            raise DbException("Failed to connect to mongodb server: %s" % e)

    def __del__(self):
        if self._client:
            try:
                self._client.drop_database(self._active_db)
                self._client.close()
            except:
                pass

    @init_check
    def add_db(self, name):
        """
        Add database and initialize it.
        """
        if (name in self._dbs):
            raise DbException("DB %s already exists")

        db = self._client[name]
        db_init_data = {'user':    os.getlogin(),
                        'created': Util.datetime()}
        db['init'].insert_one(db_init_data)
        self._dbs.append(name)

    @property
    @init_check
    def active_db(self):
        if not self._active_db:
            raise DbException("Set active DB first")
        return self._client[self._active_db]

    @active_db.setter
    def active_db(self, value):
        if (value not in self._dbs):
            raise DbException("DB %s doesn't exist" % value)
        self._active_db = value

    @init_check
    def fill_random(self, table_name, data_template, size):
        """
        Fill it random data based on a simple template.

                {'field_1': (type, metdata), ...}

        Type can be int, float or str. Metadata depends on type and is used as a guideline for generating
        random entries and should adhere to the following rules:

                int     -> (min_value, max_value)
                float   -> (min_value, range)
                str     -> (list of all possible string values)
        """
        keys = list(data_template.keys())
        # randomly generated vectors are zipped together and map function is used to build a dict object
        gen_data = list(map(lambda _: {keys[i]:data[i] for i in range(len(keys))
                                        for data in zip(*(Util.get_random_set(v[0], v[1], size) if v else None for v in data_template.values()))
                                      },
                            range(size))
                        )

        res = self.active_db[table_name].insert_many(gen_data)
        return (len(gen_data) == len(res.inserted_ids)) if res else False

    @init_check
    def read(self, table_name, keys=None, filt=None, limit=0):
        query_res = self.active_db[table_name].find(filt, keys, limit=limit)
        try:
            _ = query_res[0]
        except IndexError:
            return DataFrame()

        if not keys:
            keys = list(query_res[0].keys())

        data = {k:[] for k in keys}
        for doc in query_res:
            [data[k].append(doc[k]) for k in keys]
        query_res.close()
        return DataFrame(data)


# -*- coding: utf-8 -*-

import time
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
from openerp import api, fields, models
import openerp.addons.decimal_precision as dp
from openerp.exceptions import UserError, RedirectWarning

class wizrad_invoice(models.TransientModel):
    _name = "wizard.invoice"

    name = fields.Char(string='Invoice Number')
    date = fields.Date(string='Date', default=fields.Datetime.now, required=False)
    journal_id = fields.Many2one("account.journal", 'Journal', required=True)
    currency_id = fields.Many2one("res.currency", string="Currency", required=True)
    wizard_ids = fields.One2many('wizard.invoice.line', 'wizard_id', 'Wizard Lines')

    @api.model
    def default_get(self, fields):
        res = {}
        vars = []
        active_id = self._context.get('active_id')
        if active_id:

            si = self.env['sale.contract'].browse(active_id)

            nvs =si.name[4:len(si.name)]
            now = datetime.now()
            current_year = str(now.year)
            current_year = current_year[2:4]

            name ='INV-'+ str(current_year) +'-'+ nvs

            journal_ids = self.env['account.journal'].search([('type', '=', 'sale')], limit=1)
            res = {'contract_id': active_id, 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT) or False,
                   'currency_id': si.currency_id.id or False,'name':name, 'journal_id': journal_ids.id}
            for line in si.contract_line:
                vars.append((0, 0, {'product_id':line.product_id.id,'product_qty': line.product_qty or 0.0,
                                'price_unit': line.price_unit or 0.0}))
                res.update({'wizard_ids':vars})
        return res

    @api.multi
    def _get_taxes_invoice(self, move_line, type):
        taxes = move_line.product_id.taxes_id
        if move_line.picking_id and move_line.picking_id.partner_id and move_line.picking_id.partner_id.id:
            position = self.env['account.fiscal.position'].browse(move_line.picking_id.partner_id.property_account_position_id.id)
            return position.map_tax(taxes)
        else:
            return map(lambda x: x.id, taxes)

    @api.multi
    def _get_account_analytic_invoice(self, picking, move_line):
        return False

    @api.multi
    def _prepare_invoice_line(self,  move_line, invoice_id, invoice_vals):
        name = move_line.product_id.name or ''
        origin = move_line.product_id.name or ''

        if self.journal_id.default_debit_account_id:
            account_id = self.journal_id.default_debit_account_id.id or False
        if not account_id:
            account_id = move_line.product_id.property_account_income_id.id or False
        if not account_id:
            account_id = move_line.product_id.categ_id.property_account_income_categ_id.id or False

        if invoice_vals['fiscal_position_id']:
            fp_obj = self.env['account.fiscal.position']
            fiscal_position = fp_obj.browse(invoice_vals['fiscal_position_id'])
            account_id = fiscal_position.map_account(account_id)

        uos_id = move_line.product_id and move_line.product_id.uom_id.id or False
        if not uos_id and invoice_vals['type'] == 'out_invoice':
            uos_id = move_line.product_uom.id

        return { 'name': name, 'origin': origin,
            'invoice_id': invoice_id.id, 'uom_id': 7, 'product_id': move_line.product_id.id,
            'account_id': account_id, 'price_unit': move_line.price_unit or 0.0, 'quantity': move_line.product_qty/1000,
            'invoice_line_tax_ids': [(6, 0, self._get_taxes_invoice(move_line, invoice_vals['type']))]
            }

    @api.multi
    def create_invoice(self):

        active_id = self._context.get('active_id')

        invoice_pool = self.env['account.invoice']
        invoice_line_pool = self.env['account.invoice.line']
        si = self.env['sale.contract'].browse(active_id)

        partner_id = si.partner_id or False

        account_id = partner_id.property_account_receivable_id.id or False
        payment_term = partner_id.property_payment_term_id.id or False

#         nvs =si.name[4:len(si.name)]
#         now = datetime.now()
#         current_year = now.year
#
#         name ='INV-'+ str(current_year) +'.'+ nvs

        invoice_vals = {'name': self.name,
                        'origin': si.name,
                        'supplier_inv_date': False,
                        'partner_id': partner_id.id,
                        'reference': self.name,
            'type': 'out_invoice', 'account_id': account_id, 'date_invoice': self.date or False,
            'currency_id': self.currency_id.id or False,
            'comment': '',
            'company_id': 1,
            'user_id': self.env.uid,
            'journal_id': self.journal_id.id or False,
            'payment_term_id': payment_term,
            'fiscal_position_id': partner_id.property_account_position_id.id or False,
            'sale_contract_id': si.id or False}

        invoice_id = invoice_pool.create(invoice_vals)
        for line in self.wizard_ids:
            vals = self._prepare_invoice_line( line, invoice_id, invoice_vals)
            invoice_line_pool.create(vals)

        if invoice_id:
            imd = self.env['ir.model.data']
            action = imd.xmlid_to_object('account.action_invoice_tree1')
            form_view_id = imd.xmlid_to_res_id('account.invoice_form')
            result = {
                    'name': action.name,
                    'help': action.help,
                    'type': action.type,
                    'views': [[form_view_id, 'form']],
                    'target': action.target,
                    'context': action.context,
                    'res_model': action.res_model,
                    'res_id': invoice_id.ids[0],
                    'views' : [(form_view_id, 'form')],
                }

        return result

class wizard_invoice_line(models.TransientModel):
    _name = "wizard.invoice.line"

    wizard_id = fields.Many2one('wizard.invoice', string='Wizard invoice', ondelete='cascade')
    do_id = fields.Many2one('delivery.order', string='Delivey Order', readonly=True)
    picking_id = fields.Many2one('stock.picking', string='Picking', readonly=True)
    check_picking = fields.Boolean('Check', _defaut=True)

    product_id = fields.Many2one('product.product', string='Product', ondelete='cascade',required=True,)
    product_qty = fields.Float(string='product Qty',digits=(12, 0) )
    price_unit= fields.Float(string='Price Unit',digits=(12, 3) )


#!/usr/bin/env python
import argparse
import time
import datetime
import random
import json
import logging
import urllib
import urllib2
import sys

def exit_gracefully(ret=0):
    """
    Exit the application returning :ret:
    """
    import sys
    sys.exit(ret)

if __name__ == '__main__':
    # You can change logging level to INFO to get some information on the data
    # received and sent
    logging.basicConfig(stream=sys.stdout, level=logging.WARNING,
            format='%(message)s')

    parser = argparse.ArgumentParser(
    description='This is the script for simulating the incoming data.\n\
If a file is specified then the CSV data on that file will be posted.\n\
If no file is passed, random data is generated and sent every second.',
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('url',
            help='URL to post the data.\n\
Default: http://localhost:8080/opened',
            default='http://localhost:8080/opened',
            nargs='?')
    args = parser.parse_args()

    try:
        import signal
        for i in (signal.SIGABRT, signal.SIGALRM, signal.SIGHUP, signal.SIGILL,
                signal.SIGINT, signal.SIGTERM):
            signal.signal(i, lambda a,b: exit_gracefully())
    except:
        pass #Not UNIX/like system

    res = None
    try:
        res = urllib2.urlopen(args.url)
    except urllib2.URLError as ex:
        logging.error('Error accessing URL: {}\n\t{}'.format(args.url, ex.message))
        exit_gracefully(1)

    res = res.read()
    logging.info('Data got from the server: {}'.format(res))
    conveyor_data = json.loads(res)
    current_total_weight = 0
    if conveyor_data['last_data']:
        current_total_weight = conveyor_data['last_data']['current_total_weight']

    values = {
            'timestamp': None,
            'current_total_weight': current_total_weight,
            'status': None
            }
    while True:
        values['timestamp'] = datetime.datetime.utcnow()\
                .strftime('%Y-%m-%d %H:%M:%S.%f')
        values['current_total_weight'] += random.random() * 100 + 50
        values['status'] = 'Working' if random.random() > .1 else 'Stopped'
        data = urllib.urlencode(values)
        try:
            req = urllib2.Request(args.url, data)
            res = urllib2.urlopen(req)
        except urllib2.URLError as ex:
            loggin.error('Error sending to URL: {}\n\t{}'.format(args.url, ex))
            exit_gracefully(1)
        logging.info('Data sent: {}'.format(data))
        time.sleep(random.random() * 5)

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4


from typing import List
from typing import Optional

class TreeNode:

    def __init__(self, val=0, left=None, right=None):
         self.val = val
         self.left = left
         self.right = right

# Print the tree
def pre_order_traversal(node: TreeNode, indent):
    # Root
    if node:
        print(f"{indent}'{node.val}'")
    else:
        return
    indent = indent + "-"
    # Left
    pre_order_traversal(node.left, indent)
    # Right
    pre_order_traversal(node.right, indent)

class Solution:

    def right_views(self, root, path, res):

        if not root:
            return

        if not path or sum(path) == len(path):
            res.append(root.val)

        self.right_views(root.left, path + [0], res)
        self.right_views(root.right, path + [1], res)

    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        self.right_views(root, [], res)
        return res

def test():
    solution = Solution()
    # test method
    root = TreeNode(3, None, None)
    node2 = TreeNode(2, None, None)
    root.left = node2
    node4 = TreeNode(4, None, None)
    root.right = node4

    node1 = TreeNode(1, None, None)
    node2.left = node1

    node3 = TreeNode(3, None, None)
    node4.left = node3
    node6 = TreeNode(6, None, None)
    node4.right = node6

    node5 = TreeNode(5, None, None)
    node6.left = node5

    node9 = TreeNode(9, None, None)
    node6.right = node9

    pre_order_traversal(root, "")

    #print(solution.rightSideView(root))

    root = TreeNode(1, None, None)
    node2 = TreeNode(2, None, None)
    root.left = node2
    print(solution.rightSideView(root))

test()


from flask import Flask, jsonify, request, abort
from inference import process_request

APP = Flask(__name__)

@APP.route('/prediction', methods=['POST'])
def prediction():
    '''
    Handles the call from the client.
    '''
    if request.is_json:
        content = request.get_json()
    else:
        abort(404)
    return jsonify(process_request(content))
    # if request.form is not None:
    #     request.get_data()
    #     # print(type(request))
    #     print(request.json)
    #     result = process_request(request)
    #     print(result)
    #     return jsonify(process_request(request))

if __name__ == '__main__':
    APP.run(debug=True, host='0.0.0.0')


#!/usr/bin/env python3
import argparse
import sys

import screed

def n50(lengths):
    lengths.sort()
    total = sum(lengths)
    cumsum = 0
    for seqlen  in lengths:
        cumsum += seqlen
        if cumsum >= (total/2):
            return seqlen
    return 0

def process_fasta(fastafile):
    lengths = []
    with screed.open(fastafile) as sequences:
        for sequence in sequences:
            lengths.append(len(sequence.sequence))
    return lengths

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("-o", "--outfile", type=argparse.FileType('w'), default=sys.stdout,
                    help="Output TSV file")
    ap.add_argument("assemblies", nargs='+', type=str,
                    help="Input genome assemblies")
    args = ap.parse_args()

    print("AssemblyFasta", "NumContigs", "TotalLength", "N50", sep="\t", file=args.outfile)
    for asm in args.assemblies:
        lengths = process_fasta(asm)
        print(asm, len(lengths), sum(lengths), n50(lengths), sep='\t', file=args.outfile)

if __name__ == "__main__":
    main()


from django.urls import path

from .views import UserCreateApiView, UserTokenView, \
    UserRetrieveUpdateApiView, Postman

app_name = "user"

urlpatterns = [
    path('create/', UserCreateApiView.as_view(), name="create"),
    path('token/', UserTokenView.as_view(), name="token"),
    path('me/', UserRetrieveUpdateApiView.as_view(), name="me"),
    path('postman/', Postman.as_view(), name="postman")
]


from flask import Flask, jsonify, request
import redis

app = Flask(__name__)
red = redis.StrictRedis(host='localhost', port=6379, db=0)

# Helper functions
def saveMessage(msg):
  new_id = red.incr('messageid')
  red.hset('messages', new_id, msg.rstrip()) # chomp whitespace, e.g. '\n'
  return jsonify(message_id=new_id, message_content=msg)

def getMessage(mid):
  if red.hexists('messages', mid):
    return red.hget('messages', mid)

  return 'no such message', 404

# Routing functions
# Input route - data is posted and we save it
@app.route("/in/", methods=['POST'])
def inRoute():
  if request.data:
    message = request.data
  elif request.form.keys():
    message = request.form.keys().pop()
  else:
    message = None

  if message:
    return saveMessage(message)

  return 'No data received', 400

# Output route - output message identified by mid
@app.route('/messages/<int:mid>/')
def outRoute(mid):
  return getMessage(mid)

# Ensure the server only runs from Python interpreter and not if used as an
# imported module
if __name__ == '__main__':
  app.debug = True # using for the livereload functionality
  app.run(
    port=8888
  )


from cutadapt.statistics import ReadLengthStatistics

class TestReadLengthStatistics:
    def test_empty_on_init(self):
        rls = ReadLengthStatistics()
        assert rls.written_reads() == 0
        assert rls.written_bp() == (0, 0)
        lengths = rls.written_lengths()
        assert not lengths[0] and not lengths[1]

    def test_some_reads(self):
        rls = ReadLengthStatistics()
        rls.update("THEREAD")  # length: 7
        rls.update("YETANOTHER")  # length: 10
        rls.update2("FIRST", "SECOND")  # lengths: 5, 6
        rls.update("12345")

        assert rls.written_reads() == 4
        assert rls.written_bp() == (7 + 10 + 5 + 5, 6)
        lengths = rls.written_lengths()
        assert sorted(lengths[0].items()) == [(5, 2), (7, 1), (10, 1)]
        assert sorted(lengths[1].items()) == [(6, 1)]

    def test_iadd(self):
        rls = ReadLengthStatistics()
        rls.update("THEREAD")  # length: 7
        rls.update("YETANOTHER")  # length: 10
        rls.update2("FIRST", "SECOND")  # lengths: 5, 6
        rls.update("12345")

        rls2 = ReadLengthStatistics()
        rls2.update("TESTING")  # length: 7
        rls2.update2("LEFT", "RIGHT")  # lengths: 4, 5
        rls += rls2

        assert rls.written_reads() == 6
        assert rls.written_bp() == (7 + 10 + 5 + 5 + 7 + 4, 6 + 5)
        lengths = rls.written_lengths()
        assert sorted(lengths[0].items()) == [(4, 1), (5, 2), (7, 2), (10, 1)]
        assert sorted(lengths[1].items()) == [(5, 1), (6, 1)]


import cv2
import dlib
import numpy as np
import time

from visualization import Camera  # 카메라 관련 (Color도 여기에서 get함수로 받아올 수 있다)
from tracker import *  # 트래킹
from face_detector import *  # 얼굴 detector
from mark_detector import *  # 랜드마크 detector
from Eye import *
from pose_estimator import *
from myHead import *
from myMouth import *

print(__doc__)
print("OpenCV version: {}".format(cv2.__version__))

# cm = Camera()
cm = Camera(path="dataset/dataset_ver1.1.mp4")  # path를 안하면 카메라 하면 영상
# cm = Camera(path="Z:/DMS/Dataset/DMS_V/capture.avi")
tk = Tracker()
fd = FaceDetector()
md = MarkDetector()
pe = PoseEstimator()
head = myHead()
mouth = myMouth()

while cm.cap.isOpened():
    ret, frame = cm.cap.read()  # 영상 프레임 받기
    key = cv2.waitKey(1)

    if key == 27:
        break
    # md.changeMarkIndex(key)  # 랜드마크 점 종류를 바꾸고 싶다면 활성화 (미완성)

    if ret:
        # 전처리 구간 ==========================================================
        imgNdarray = cm.getFrameResize2ndarray(frame)  # frame to ndarray
        gray = cv2.cvtColor(imgNdarray, cv2.COLOR_BGR2GRAY)  # (H, W)
        # ====================================================================

        rect = tk.getRectangle(gray, fd)  # 트래킹 하는것과 동시에 face detect 반환
        if rect is not None:
            landmarks = md.get_marks(gray, rect)
            md.draw_marks(gray, landmarks, color=cm.getRed())
            landmarks = md.full_object_detection_to_ndarray(landmarks)

            eyes = [eye_crop_none_border(gray, landmarks[36:42]),
                    eye_crop_none_border(gray, landmarks[42:48])]

            # 후처리 구간 ==========================================================
            head.lowerHeadText(landmarks, gray) # 수정했습니다 (class pose_estimator file_name= 부분)
            mouth.openMouthText(landmarks, gray)
            # x, y, z 축 보고 싶다면?
            landmarks_32 = np.array(landmarks, dtype=np.float32)
            pose = pe.solve_pose_by_68_points(landmarks_32)
            axis = pe.get_axis(gray, pose[0], pose[1])
            # axis = [[[RED_x RED_y]],[[GREEN_x GREEN_y]],[[BLUE_x BLUE_y]],[[CENTER_x CENTER_y]]]
            # --> BLUE(정면) GREEN(아래) RED(좌측) CENTER(중심)
            if axis is not None:
                pe.draw_axes(gray, pose[0], pose[1])
                head.directionText(axis, gray)
        cv2.imshow("gray", gray)
    else:
        break

cv2.destroyAllWindows()
cm.cap.release()


# -*- encoding: utf-8 -*-
"""
hio.help.helping module

"""

import types
import functools

def copy_func(f, name=None):
    """
    Copy a function in detail.
    To change name of func provide name parameter

    functools to update_wrapper assigns and updates following attributes
    WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
                       '__annotations__')
    WRAPPER_UPDATES = ('__dict__',)
    Based on
    https://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python
    https://stackoverflow.com/questions/13503079/how-to-create-a-copy-of-a-python-function
    """
    g = types.FunctionType(f.__code__,
                           f.__globals__,
                           name=f.__name__,
                           argdefs=f.__defaults__,
                           closure=f.__closure__
                          )
    g = functools.update_wrapper(g, f)
    g.__kwdefaults__ = f.__kwdefaults__
    if name:
        g.__name__ = name
    return g


import sys
import argparse
from backend.camera_module_options import get_camera_module_name_options
from run_commands.command_options import get_instance_of_run_config_by_name, get_command_names

def main():
    raw_args = sys.argv
    parsed_args = parse_command_line_args()
    run_config = get_instance_of_run_config_by_name(parsed_args.command, parsed_args, raw_args)
    run_config.run()

def parse_command_line_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("command",
                        help="The name of the command you want to use",
                        choices=get_command_names())
    camera_module_name_options = get_camera_module_name_options()
    parser.add_argument("-c", "--camera_module",
                        help="The name of the camera module to use. Defaults to \"dummy\"",
                        choices=camera_module_name_options,
                        default=camera_module_name_options[0])
    parser.add_argument("-f", "--force_album",
                        help="If this is set, the application is limited to the specified album when running")
    return parser.parse_args()

if __name__ == '__main__':
    main()


import pygame
import cv2
import numpy as np

pygame.init()
dis = pygame.display.set_mode((400, 300))
pygame.display.update()
pygame.display.set_caption('Snake game by Suhani and Nidhi')

blue=(0,0,255)
red=(255,0,0)

# Create a VideoCapture object
cap = cv2.VideoCapture(0)  # 0 for the default camera, you can change this number for other cameras

# Check if the camera is opened correctly
if not cap.isOpened():
    print("Error: Could not open camera.")
    exit()

# Loop to continuously capture frames from the camera
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Check if the frame was successfully captured
    if not ret:
        print("Error: Could not read frame.")
        break

    # Display the frame
    cv2.imshow('Camera Feed', frame)

    # Break the loop on pressing 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the VideoCapture object and close the OpenCV window
cap.release()
cv2.destroyAllWindows()

game_over = False
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

    pygame.draw.rect(dis,blue,[200,150,10,10])
    pygame.display.update()

pygame.quit()
quit()


import numpy as np
from sklearn.preprocessing import PolynomialFeatures

class LinearRegression:
    def __init__(self, data, labels, poly=1, include_bias=True, regularization=False) -> None:
        self.poly = poly
        self.include_bias = include_bias
        self.regularization = regularization

        # 更新data
        self.data = PolynomialFeatures(degree=self.poly, include_bias=self.include_bias).fit_transform(data)
        self.labels = labels.reshape(-1, 1)

        # 更新theta
        num_features = self.data.shape[1]
        self.theta = np.zeros((num_features, 1))

    def fit(self, lamda=None):
        if self.regularization:
            self.theta = np.linalg.inv(self.data.T.dot(self.data) + lamda * np.eye(self.data.shape[1])).dot(
                self.data.T).dot(self.labels)
        else:
            self.theta = np.linalg.inv(self.data.T.dot(self.data)).dot(self.data.T).dot(self.labels)
        return self.theta

    def train(self, alpha, num_iterations=500, lamda=None):
        cost_history = self.gradient_descent(alpha, num_iterations, lamda)
        return self.theta, cost_history

    def gradient_descent(self, alpha, num_iterations, lamda=None):
        cost_history = []
        for _ in range(num_iterations):
            cost = self.gradient_step(alpha, lamda)
            cost_history.append(cost)
        return cost_history

    def gradient_step(self, alpha, lamda=None):
        num_examples = self.data.shape[0]
        prediction = LinearRegression.hypothesis(self.data, self.theta)
        delta = prediction - self.labels
        theta = self.theta
        if self.regularization:
            theta[1:] = (theta[1:] - alpha * (1 / num_examples) * (np.dot(delta.T, self.data[:, 1:])).T
                         - alpha * lamda * theta[1:])
            theta[0] = theta[0] - alpha * (1 / num_examples) * np.sum(delta)
        else:
            theta = theta - alpha * (1 / num_examples) * (np.dot(delta.T, self.data)).T

        self.theta = theta

        cost = (1 / 2) * np.dot(delta.T, delta)
        return cost[0][0]

    @staticmethod
    def hypothesis(data, theta):
        predictions = np.dot(data, theta)
        return predictions

    def predict(self, data):
        data_processed = PolynomialFeatures(degree=self.poly, include_bias=self.include_bias).fit_transform(data)
        predictions = LinearRegression.hypothesis(data_processed, self.theta)
        return predictions


import asyncio
import time
import random

global_lock = asyncio.Semaphore(1)

async def run_cmd(cmd, timeout=None, task_id=None):
    async with global_lock:
        print(f"{task_id} subprocess start")
        start = time.time()
        proc = await asyncio.create_subprocess_shell("exec "+cmd)
        try:
            if timeout:
                await asyncio.wait_for(proc.wait(), timeout=timeout)
            else:
                await proc.wait()
            print(f'[{cmd!r} exited with {proc.returncode}]')
        except asyncio.TimeoutError as e:
            print(f'[{cmd!r} get timeout error]')
        except Exception as e:
            print(f'[{cmd!r} get uncaught exception {e}]')
        finally:
            if proc.returncode is None:
                print(f"{task_id} kill")
                proc.kill()
            end = time.time()
            print(f"{task_id} subprocess cost {end-start}")
        print(f"{task_id} subprocess end")

def cpu(task_id):
    print(f"{task_id} cpu start")
    for i in range(10000000):
        i = i + 1
    print(f"{task_id} cpu end")

async def io(task_id):
    print(f"{task_id} io start")
    await asyncio.sleep(0.05)
    print(f"{task_id} io end")

async def single_round(cmd, timeout=None, task_id=None):
    await run_cmd(cmd, timeout=timeout, task_id=task_id)
    await io(task_id)
    cpu(task_id)
    await io(task_id)

async def main(timeout):
    tasks = [single_round(f"sleep {random.random()*timeout}", timeout=timeout, task_id=random.randint(0, 1000)) for i in range(5)]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(8))


from django.urls import include, path, re_path
from rest_framework.routers import DefaultRouter

from .views import (IngredientViewSet, RecipeViewSet, TagViewSet, UserViewSet,
                    add_delete_favorite, add_delete_shoppingcart,
                    download_shopping_cart, subscribe, subscriptions)

router = DefaultRouter()
router.register('tags', TagViewSet, basename='tag')
router.register('users', UserViewSet, basename='user')
router.register('ingredients', IngredientViewSet, basename='ingredient')
router.register('recipes', RecipeViewSet, basename='recipe')

urlpatterns = [
    path('users/subscriptions/', subscriptions, name='subscription_list'),
    path('users/<int:id>/subscribe/', subscribe, name='subscribe'),
    path('recipes/download_shopping_cart/', download_shopping_cart,
         name='download-shoppingcart'),
    path('', include(router.urls)),
    path('', include('djoser.urls')),
    path('recipes/<int:id>/favorite/', add_delete_favorite,
         name='add-delete-favorite'),
    path('recipes/<int:id>/shopping_cart/', add_delete_shoppingcart,
         name='add-delete-shoppingcart'),
    re_path(r'^auth/', include('djoser.urls.authtoken')),
]


from json import dumps

from sostrades_core.tools.tree.data_management_discipline import DataManagementDiscipline
from sostrades_core.execution_engine.proxy_discipline import ProxyDiscipline
from os.path import dirname, isdir, isfile, join
import inspect
import os
import base64
import re
from os import listdir

class TreeNode:
    """
    Class to build tree node from data manager
    """
    STATUS_INPUT_DATA = 'INPUT_DATA'
    MARKDOWN_NAME_KEY = 'name'
    MARKDOWN_DOCUMENTATION_KEY = 'documentation'

    needed_variables = [ProxyDiscipline.TYPE, ProxyDiscipline.USER_LEVEL, ProxyDiscipline.EDITABLE,
                        ProxyDiscipline.COUPLING, ProxyDiscipline.VALUE, ProxyDiscipline.NUMERICAL, ProxyDiscipline.OPTIONAL]

    def __init__(self, name, exec_display=False):
        """ class constructor
        """
        self.name = name

        self.exec_display = exec_display
        # Children treenode: TreeNode[]
        self.children = []

        # Data coming from DataManager.data_dict for the associated namespace
        self.data = {}  # current node data

        # Store the discipline type
        self.node_type = 'data'

        # Treenode status (bijective with associated discipline if exist)
        self.status = TreeNode.STATUS_INPUT_DATA

        # Namespace associated to the disciplines
        self.full_namespace = ''

        # Disciplines indentifier (execution engine side => uuid)
        self.identifier = ''

        # Lisf of discipline identifier associated to this TreeNode
        self.disc_ids = []

        # Dict with addition of maturity for each discipline on this TreeNode
        self.multi_discipline_maturity = {}

        # Disciplines maturity (determined using the discipline maturity)
        self.maturity = ''

        # Data at discipline level (not namespace level => cf. self.data
        self.disc_data = {}  # to be able to show all variable at each discipline level

        # Treenode documentation (markdown format)
        self.markdown_documentation = []

        # List of models present at current treenode
        self.models_full_path_list = []

        # List of disciplines at this node
        self.data_management_disciplines = {}

        # List of discipline_full_path_list for each variables
        self.disciplines_by_variable = {}

        self.model_name = None
        self.model_name_full_path = None
        self.last_treenode = None

    def to_json(self):
        dict_obj = self.to_dict()
        return dumps(dict_obj)

    def to_dict(self):
        dict_obj = {}
        # Serialize name attribute
        dict_obj.update({'name': self.name})

        # Serialize data attribute
        dict_obj.update({'data': self.data})

        # Serialize node_type attribute
        dict_obj.update({'node_type': self.node_type})

        # Serialize status attribute
        dict_obj.update({'status': self.status})

        # Serialize full_namespace attribute
        dict_obj.update({'full_namespace': self.full_namespace})

        # Serialize identifier attribute
        dict_obj.update({'identifier': self.identifier})

        # Serialize maturity attribute
        dict_obj.update({'maturity': self.maturity})

        # Serialize disc_data attribute
        dict_obj.update({'disc_data': self.disc_data})

        # Serialize model_name_full_path attribute
        dict_obj.update({'model_name_full_path': self.model_name_full_path})

        # Serialize models_full_path_list attribute
        dict_obj.update({'models_full_path_list': self.models_full_path_list})

        # Serialize data_management_disciplines attribute
        json_data_management_disciplines = {}
        for key in self.data_management_disciplines.keys():
            json_data_management_disciplines[key] = self.data_management_disciplines[key].to_json()
        dict_obj.update({'data_management_disciplines': json_data_management_disciplines})

        # Serialize markdown_documentation
        dict_obj.update(
            {'markdown_documentation': self.markdown_documentation})

        # Serialize children attribute
        dict_child = []
        for tn in self.children:
            dict_child.append(tn.to_dict())
        dict_obj.update({'children': dict_child})
        return dict_obj

    def update_treenode_attributes(self, discipline, no_data=False, read_only=False):
        """ Inject discipline data into the current treenode

        :params: discipline to set into the treenode
        :type: ProxyDiscipline
        """
        self.full_namespace = discipline.get_disc_display_name(
            self.exec_display)
        self.identifier = discipline.disc_id
        self.disc_ids.append(self.identifier)
        self.node_type = discipline.__class__.__name__

        self.model_name_full_path = discipline.get_module()
        self.models_full_path_list.append(self.model_name_full_path)

        # add a new data_management_discipline
        data_management_discipline = DataManagementDiscipline()
        data_management_discipline.namespace = self.full_namespace
        data_management_discipline.model_name_full_path = self.model_name_full_path
        data_management_discipline.discipline_label = discipline.get_disc_label()

        # Some modification has to be done on variable:
        # identifier : variable namespace + variable name
        # I/O type : 'in' for data_in and 'out' for data_out
        disc_in = discipline.get_data_in()
        if not no_data:
            for key, data_key in disc_in.items():
                # if self.exec_display:

                namespaced_key = discipline.get_var_full_name(
                        key, disc_in)
                # else:
                #     namespaced_key = discipline.get_var_display_name(
                #         key, disc_in)
                new_disc_data = {k: v for k, v in data_key.items()}
                new_disc_data[ProxyDiscipline.IO_TYPE] = ProxyDiscipline.IO_TYPE_IN
                if read_only:
                    new_disc_data[ProxyDiscipline.EDITABLE] = False
                new_disc_data[ProxyDiscipline.VARIABLE_KEY] = self.create_data_key(self.model_name_full_path, ProxyDiscipline.IO_TYPE_IN, key)
                self.update_disc_data(
                    new_disc_data, namespaced_key, discipline)

                if not namespaced_key in self.disciplines_by_variable.keys():
                    self.disciplines_by_variable[namespaced_key] = []
                self.disciplines_by_variable[namespaced_key].append(data_management_discipline.discipline_label)
                if new_disc_data[ProxyDiscipline.NUMERICAL]:
                    self.add_disc_data_in_data_management_discipline(new_disc_data,
                                                                    namespaced_key,
                                                                    data_management_discipline.model_name_full_path,
                                                                    data_management_discipline.numerical_parameters)
                else:
                    self.add_disc_data_in_data_management_discipline(new_disc_data,
                                                                    namespaced_key,
                                                                    data_management_discipline.model_name_full_path,
                                                                    data_management_discipline.disciplinary_inputs)

        disc_out = discipline.get_data_out()
        if not no_data:
            for key, data_key in disc_out.items():
                # if self.exec_display:
                namespaced_key = discipline.get_var_full_name(
                        key, disc_out)
                # else:
                #     namespaced_key = discipline.get_var_display_name(
                #         key, disc_out)

                new_disc_data = {
                    needed_key: data_key[needed_key] for needed_key in self.needed_variables}
                new_disc_data[ProxyDiscipline.IO_TYPE] = ProxyDiscipline.IO_TYPE_OUT
                if read_only:
                    new_disc_data[ProxyDiscipline.EDITABLE] = False
                new_disc_data[ProxyDiscipline.VARIABLE_KEY] = self.create_data_key(self.model_name_full_path, ProxyDiscipline.IO_TYPE_OUT, key)
                self.update_disc_data(
                    new_disc_data, namespaced_key, discipline)

                if not namespaced_key in self.disciplines_by_variable.keys():
                    self.disciplines_by_variable[namespaced_key] = []
                self.disciplines_by_variable[namespaced_key].append(data_management_discipline.discipline_label)
                if new_disc_data[ProxyDiscipline.NUMERICAL]:
                    self.add_disc_data_in_data_management_discipline(new_disc_data,
                                                                    namespaced_key,
                                                                    data_management_discipline.model_name_full_path,
                                                                    data_management_discipline.numerical_parameters)
                else:
                    self.add_disc_data_in_data_management_discipline(new_disc_data,
                                                                    namespaced_key,
                                                                    data_management_discipline.model_name_full_path,
                                                                    data_management_discipline.disciplinary_outputs)

        self.__manage_status(discipline.status)

        # Convert maturity dictionary to string for display purpose
        # Update multi_discipline_maturity with current discipline maturity
        o_mat = discipline.get_maturity()
        if isinstance(o_mat, dict):
            for k, v in o_mat.items():
                if k in self.multi_discipline_maturity:
                    self.multi_discipline_maturity[k] += v
                else:
                    self.multi_discipline_maturity[k] = v
        else:
            if len(o_mat) > 0:
                if o_mat in self.multi_discipline_maturity:
                    self.multi_discipline_maturity[o_mat] += 1
                else:
                    self.multi_discipline_maturity[o_mat] = 1

        if len(self.multi_discipline_maturity.items()) > 0:
            l_mat = [
                f'{k}({v})' for k, v in self.multi_discipline_maturity.items() if v != 0]
            s_mat = ' '.join(l_mat)
        else:
            s_mat = ''
        self.maturity = s_mat

        # save maturity in discipline data
        data_management_discipline.maturity = o_mat
        self.data_management_disciplines[f'{data_management_discipline.discipline_label}'] = data_management_discipline

        # Manage markdown documentation
        filepath = inspect.getfile(discipline.__class__)
        markdown_data = TreeNode.get_markdown_documentation(filepath)
        self.add_markdown_documentation(markdown_data, self.model_name_full_path)

    def create_data_key(self, disc_name, io_type, variable_name):
        io_type = io_type.lower()
        return f'{disc_name}_{io_type}put_{variable_name}'

    def update_disc_data(self, new_disc_data, namespace, discipline):
        """ Set variable from discipline into treenode disc_data
        :params: new_disc_data, variable data
        :type: ProxyDiscipline variable data_dict

        :params: namespace, namespace of the variable
        :type: string

        :params: discipline to set into the treenode
        :type: ProxyDiscipline
        """

        disc_full_path = discipline.get_module()

        if namespace not in self.disc_data:
            self.disc_data[namespace] = new_disc_data
            self.disc_data[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST] = [disc_full_path]
        else:
            for key, value in new_disc_data.items():
                self.disc_data[namespace][key] = value
            if disc_full_path not in self.disc_data[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST]:
                self.disc_data[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST].append(disc_full_path)

    def add_disc_data_in_data_management_discipline(self, new_disc_data, namespace, disc_full_path, discipline_variable_list):
        """ Set variable from discipline into treenode disc_data
        :params: new_disc_data, variable data
        :type: ProxyDiscipline variable data_dict

        :params: namespace, namespace of the variable
        :type: string

        :params: discipline to set into the treenode
        :type: ProxyDiscipline
        """
        # because variable is in discipline, it is not initialy editable. If it is also in data it will be
        new_disc_data[ProxyDiscipline.EDITABLE] = False
        if namespace not in discipline_variable_list:
            discipline_variable_list[namespace] = new_disc_data
            discipline_variable_list[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST] = [disc_full_path]
        else:
            for key, value in new_disc_data.items():
                discipline_variable_list[namespace][key] = value
            if disc_full_path not in discipline_variable_list[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST]:
                discipline_variable_list[namespace][ProxyDiscipline.DISCIPLINES_FULL_PATH_LIST].append(disc_full_path)

    def add_markdown_documentation(self, markdown_data, key):
        """ Add a markdon documentation to the treenode

        :params: markdown_data, markdown documenation to set
        :type: str

        :params: key, associated key (used to manage multiple documentation into the same treenode
        :type: key
        """

        if markdown_data is not None and markdown_data != "":
            self.markdown_documentation.append({
                TreeNode.MARKDOWN_NAME_KEY: key,
                TreeNode.MARKDOWN_DOCUMENTATION_KEY: markdown_data
            })

    @staticmethod
    def get_markdown_documentation(filepath):
        # Manage markdown documentation

        doc_folder_path = join(dirname(filepath), 'documentation')
        filename = os.path.basename(filepath).split('.')[0]
        markdown_data = ""
        if isdir(doc_folder_path):
            # look for markdown file with extension .markdown or .md
            markdown_list = [join(doc_folder_path, md_file) for md_file in listdir(doc_folder_path) if ((
                md_file.endswith(r".markdown") or md_file.endswith(r".md")) and md_file.startswith(filename))]

            if len(markdown_list) > 0:
                # build file path
                markdown_filepath = markdown_list[0]

                if isfile(markdown_filepath):
                    markdown_data = ''

                    with open(markdown_filepath, 'r+t', encoding='utf-8') as f:
                        markdown_data = f.read()

                    # Find file reference in markdown file
                    place_holder = f'!\\[(.*)\\]\\((.*)\\)'
                    matches = re.finditer(place_holder, markdown_data)

                    images_base_64 = {}
                    base64_image_tags = []

                    for matche in matches:
                        # Format:
                        # (0) => full matche line
                        # (1) => first group (place holder name)
                        # (2) => second group (image path/name)

                        image_name = matche.group(2)

                        # Convert markdown image link to link to base64 image
                        image_filepath = join(doc_folder_path, image_name)

                        if isfile(image_filepath):
                            image_data = open(image_filepath, 'r+b').read()
                            encoded = base64.b64encode(
                                image_data).decode('utf-8')

                            images_base_64.update({image_name: encoded})

                            # first replace the matches
                            matche_value = matche.group(1)
                            matches_replace = f'![{matche_value}]({image_name})'
                            matches_replace_by = f'![{matche_value}][{image_name}]'

                            base64_image_tag = f'[{image_name}]:data:image/png;base64,{images_base_64[image_name]}'
                            base64_image_tags.append(base64_image_tag)

                            markdown_data = markdown_data.replace(
                                matches_replace, matches_replace_by)

                    for image_tag in base64_image_tags:
                        markdown_data = f'{markdown_data}\n\n{image_tag}'

        return markdown_data

    def __str__(self):
        children_str = ''.join([str(c) for c in self.children])
        return f'name : {self.name}, data : {self.data}, node_type : {self.node_type}, status : {self.status}, maturity: {self.maturity}\nchildren : {children_str}'

    def __manage_status(self, new_status):
        """ Each treenode can have multiple discipline
            for each new discipline, we compare the new status with the old one to choose
            which status is the most prioritary
        """
        current_status_priority = TreeNode.status_to_integer(self.status)
        new_status_priority = TreeNode.status_to_integer(new_status)

        if new_status_priority > current_status_priority:
            self.status = new_status

    @staticmethod
    def status_to_integer(status):
        """ Convert status to integer value to make some sort with status

            :params: status, disciplines status name
            :type: string

            :return: integer
        """

        if status == TreeNode.STATUS_INPUT_DATA:
            return -1
        elif status == ProxyDiscipline.STATUS_VIRTUAL:
            return 0
        elif status == ProxyDiscipline.STATUS_CONFIGURE:
            return 10
        elif status == ProxyDiscipline.STATUS_DONE:
            return 20
        elif status == ProxyDiscipline.STATUS_PENDING:
            return 30
        elif status == ProxyDiscipline.STATUS_RUNNING:
            return 40
        else:  # status = ProxyDiscipline.STATUS_FAILED
            return 50


import pandas as pd
import os
import glob
import openpyxl
import warnings
import re
import tensorflow_hub as hub
import difflib
import numpy as np
import math
from sklearn.metrics.pairwise import linear_kernel
import pickle

warnings.simplefilter(action='ignore', category=UserWarning)

embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")

class Course:

    course_name_list = set([])
    file_name = ""
    code = ""
    description = ""
    embedding = []
    similarity_matrix = []
    file_ptr = ""
    name = ""

    def pre_process_text(self, new_s):
        new_s = ".".join(new_s.split("\n"))
        #new_s = " ".join(new_s.split("-"))
        new_s = new_s.lower()
        # translate_table = dict((ord(char), " ") for char in string.punctuation)
        # new_s = new_s.translate(translate_table)
        # li = word_tokenize(new_s)
        # stop_words = set(stopwords.words("english"))
        # filter_li = []
        # for words in li:
        #     if(words not in stop_words):
        #         filter_li.append(words)
        # ans = " ".join(filter_li)
        ans = new_s
        return ans

    def __init__(self, file_pointer):
        self.file_name = str(f)
        self.file_ptr = file_pointer
        self.description = self.find_description() + " " + self.find_course_plan()
        self.code = self.find_code()
        self.name = self.find_name()

    def find_description(self):
        workbook = openpyxl.load_workbook(self.file_ptr)
        worksheet = workbook.active
        for coll in range(1,6):
            for roww in range(1,20):
                if("description" in "".join(re.sub(r'[^\w\s]','', re.sub(r'\d+','',str(worksheet.cell(row=roww, column=coll).value).lower())).split())):
                    return str(worksheet.cell(row=roww, column=coll+1).value)
        return "-1"

    def find_code(self):
        f = self.file_ptr
        workbook = openpyxl.load_workbook(f)
        worksheet = workbook.active
        for coll in range(1,4):
            for roww in range(1,5):
                cell_val = "".join("".join(str(worksheet.cell(row=roww, column=coll).value).lower().strip().split()).split("-"))
                if('code' in cell_val):
                    cell_val = " ".join("".join(str(worksheet.cell(row=roww, column=coll+1).value).lower().strip().split()).split("-"))
                    return cell_val
        #If not found in the sheet, find in file_name
        file_name = str(f).split("\\")[-1]
        file_name = file_name.split("/")[-1]
        file_name = file_name.split(".")
        if(len(file_name) >= 2):
            file_name = file_name[-2]
        file_name = "".join(file_name.lower().split())
        return file_name

    def find_name(self):
        f = self.file_ptr
        workbook = openpyxl.load_workbook(f)
        worksheet = workbook.active
        for coll in range(1,4):
            for roww in range(1,5):
                cell_val = "".join("".join(str(worksheet.cell(row=roww, column=coll).value).lower().strip().split()).split("-"))
                if('name' in cell_val):
                    cell_val = " ".join("".join(str(worksheet.cell(row=roww, column=coll+1).value).lower().strip().split()).split("-"))
                    return cell_val
        #If not found in the sheet, find in file_name
        file_name = str(f).split("\\")[-1]
        file_name = file_name.split("/")[-1]
        file_name = file_name.split(".")
        if(len(file_name) >= 2):
            file_name = file_name[-2]
        file_name = "".join(file_name.lower().split())
        return file_name

    def find_course_plan(self):
        f = self.file_ptr
        workbook = openpyxl.load_workbook(f)
        worksheet = workbook.active
        lec_cell = (-1,-1)
        for coll in range(1,20):
            for roww in range(1,41):
                cell_val = "".join("".join(str(worksheet.cell(row=roww, column=coll).value).lower().strip().split()).split("-"))
                if(len(cell_val) >= 2 and len(cell_val) <= 30 and 'lecture' in cell_val and 'topic' in cell_val):
                    lec_cell = (roww,coll)
                    break
            if(lec_cell[0]!=-1):
                break
        if(lec_cell[0] == -1):
            return "-1"
        plan = ""
        for i in range(8):
            new_row = lec_cell[0]+i+1
            new_col = lec_cell[1]
            cell_val = str(worksheet.cell(row=new_row, column=new_col).value).lower().strip()
            plan = plan + ". " + cell_val
        plan = self.pre_process_text(plan)
        plan = ". ".join(plan.split("•"))
        plan = plan + "."
        return plan

    def compute_embedding(self):
        return embed([self.description])


import pickle
import sys

import emcee
import numpy
import pandas as pd
from sklearn.neighbors import KernelDensity
from tqdm import tqdm

from dataloader import *
from linmix import LinMix
from multiprocessing import Pool
from scipy import integrate

np.random.seed(20200918)

def linear_model(x, slope, intercept):
    return slope * x + intercept

def log_lnl_chi2(theta, x, y, yerr):
    intercept, slope, scatter = theta
    ypred = linear_model(x, slope, intercept)
    var = (yerr ** 2) + (scatter ** 2)

    residual = y - ypred

    return -0.5 * np.sum(((residual ** 2) / var) + np.log(2 * np.pi * var))

def log_lnl_with_x_posterior(theta, x, y, yerr, x_kdes):
    """Similar to the chi-2 likelihood but we assume x is a random variable.

    Product of the joint probability of x and y which is an integral where P(y|x') is Gaussian
    with mean at y_hat(x') and variance yerr^2 + scatter^2 and P(x') is the posterior probability of x=x'
    integrated over dx'.
    """
    intercept, slope, scatter = theta

    def y_given_x_pdf(x, y, yerr):
        """Returns a single value, the log probability of y given x"""
        ypred = linear_model(x, slope, intercept)
        var = (yerr ** 2) + (scatter ** 2)
        return stats.norm(loc=ypred, scale=var).pdf(y)

    lnl = 0
    for i in range(x.shape[0]):
        x_pdf = lambda _x: np.exp(x_kdes[i].score(np.array([_x]).reshape(1, -1)))

        integrand = lambda _x: y_given_x_pdf(_x, y[i], yerr[i]) * x_pdf(_x)
        prob, _ = integrate.quad(integrand, 0, 20)
        lnl += np.log(prob)

    return lnl if np.isfinite(lnl) else -np.inf

def log_posterior(theta, x, y, yerr, log_lnl_func=log_lnl_chi2, log_prior_func=None):
    """Return the posterior probability.
    By default uniform prior is assumed on all free parameters
    """
    x_kdes = []
    for i in range(x.shape[0]):
        x_kde = KernelDensity().fit(x[i].reshape(len(x[i]), -1))
        x_kdes.append(x_kde)

    lnl = log_lnl_func(theta, x, y, yerr, x_kdes)
    if log_prior_func is not None:
        raise NotImplementedError

    return lnl

def run_x_posterior_mcmc(x, y, yerr, nwalkers=25, nsteps=5000, sampler_kwargs={}):
    """[summary]

    Args:
        x (array-like; n x m): Sample of the posterior distribution of sample size m
          for each of n rows.
        y (array-like; n x 1): Observed y values of size n.
    """
    if isinstance(x, pd.Series):
        x = x.values
    if isinstance(y, pd.Series):
        y = y.values
    if isinstance(yerr, pd.Series):
        yerr = yerr.values

    # Start with OLS solution as a guess
    xmean = np.mean(x, axis=1)
    slope_guess = stats.pearsonr(xmean, y)[0] * np.std(y) / np.std(xmean)
    intercept_guess = np.mean(y) - (slope_guess * np.mean(xmean))
    guess = np.array([intercept_guess, slope_guess, 1])
    ndim = len(guess)

    # Let the initial point sample from a Gaussian with standard deviation
    # that's 1/2 of the guess value such that about 95% of the initial position
    # starts within the range (-2*guess, 2*guess)
    pos = guess + ((guess / 2) * np.random.randn(nwalkers, ndim))
    sampler = emcee.EnsembleSampler(
        nwalkers,
        ndim,
        log_posterior,
        args=(x, y, yerr),
        kwargs=dict(log_lnl_func=log_lnl_with_x_posterior),
        **sampler_kwargs,
    )
    sampler.run_mcmc(pos, nsteps, progress=VERBOSE)

    theta_estimate = sampler.get_chain().reshape((nsteps * nwalkers, ndim)).mean(axis=0)
    if VERBOSE:
        print(
            f"ESTIMATED PARAMTERS\nSlope: {theta_estimate[1]}\nIntercept: {theta_estimate[0]}\nScatter: {theta_estimate[2]}"
        )
    return sampler

def run_chi2_mcmc(x, y, yerr, nwalkers=25, nsteps=5000, sampler_kwargs={}):
    # Start with OLS solution as a guess
    slope_guess = stats.pearsonr(x, y)[0] * np.std(y) / np.std(x)
    intercept_guess = np.mean(y) - (slope_guess * np.mean(x))
    guess = np.array([intercept_guess, slope_guess, 1])
    ndim = len(guess)

    # Let the initial point sample from a Gaussian with standard deviation
    # that's 1/2 of the guess value such that about 95% of the initial position
    # starts within the range (-2*guess, 2*guess)
    pos = guess + ((guess / 2) * np.random.randn(nwalkers, ndim))
    sampler = emcee.EnsembleSampler(
        nwalkers, ndim, log_posterior, args=(x, y, yerr), **sampler_kwargs
    )
    sampler.run_mcmc(pos, nsteps, progress=VERBOSE)

    theta_estimate = sampler.get_chain().reshape((nsteps * nwalkers, ndim)).mean(axis=0)
    if VERBOSE:
        print(
            f"ESTIMATED PARAMTERS\nSlope: {theta_estimate[1]}\nIntercept: {theta_estimate[0]}\nScatter: {theta_estimate[2]}"
        )
    return sampler

def run_linmix(age, age_err, hr, hr_err, linmix_kwargs={}, run_kwargs={}):
    """Estimated mock of Lee20 result."""

    lm = LinMix(x=age, y=hr, xsig=age_err, ysig=hr_err, **linmix_kwargs)
    lm.run_mcmc(silent=(not VERBOSE), **run_kwargs)

    results = pd.DataFrame({"slope": lm.chain["beta"], "intercept": lm.chain["alpha"]})
    return results

if __name__ == "__main__":
    VERBOSE = "-v" in sys.argv[1:]

    # # With summary statistics
    # data = pd.read_csv("data/HRvsAge_Median+STD+Bounds.csv")
    # age = data["Age_global"]
    # age_err = data["Age_global_err"]
    # hr = data["HR"]
    # hr_err = data["HR_err"]
    # results = run_linmix(age, age_err, hr, hr_err, linmix_kwargs=dict(K=2))
    # results.to_csv("results/linmix_1.csv", index=False)

    # # With real data
    # age_df = load_age_sample_from_mcmc_chains("campbell", mode="read")
    # hr_df = load_hr("campbell")
    # age_df, hr_df = clean_data(age_df, hr_df)

    # age = age_df["age"].groupby("snid").mean()
    # age_err = age_df["age"].groupby("snid").std()
    # hr = hr_df["hr"]
    # hr_err = hr_df["hr_err"]
    # results = run_linmix(age, age_err, hr, hr_err)
    # results.to_csv("results/linmix_2.csv", index=False)

    # # With real data but use the whole Age distribution
    # # Assume each age datapoint is a delta function with zero variance
    # age_df = load_age_sample_from_mcmc_chains("campbell", mode="read")
    # hr_df = load_hr("campbell")
    # age_df, hr_df = clean_data(age_df, hr_df)

    # df = age_df.merge(hr_df, on="snid", how="left")
    # age = df["age"]
    # age_err = np.zeros(len(age))
    # hr = df["hr"]
    # hr_err = df["hr_err"]

    # with Pool() as pool:
    #     sampler = run_chi2_mcmc(
    #         age, hr, hr_err, nsteps=1000, sampler_kwargs=dict(pool=pool)
    #     )
    # with open("results/chi2-mcmc-sampler.pkl", "wb") as f:
    #     pickle.dump(sampler.get_chain(), f)

    # results = run_linmix(age, age_err, hr, hr_err, linmix_kwargs=dict(K=1))
    # results.to_csv("results/linmix_3.csv", index=False)

    # Create all possible slope given each Age from its posterior sample
    # age_df = load_age_sample_from_mcmc_chains("campbell", mode="read")
    # hr_df = load_hr("campbell")
    # age_df, hr_df = clean_data(age_df, hr_df)

    # slopes = []
    # intercepts = []
    # for i in tqdm(range(1000), total=1000):
    #     age = age_df.groupby("snid").sample(1)["age"]
    #     hr = hr_df["hr"]
    #     hr_err = hr_df["hr_err"]

    #     with Pool() as pool:
    #         sampler = run_chi2_mcmc(
    #             age,
    #             hr,
    #             hr_err,
    #             nsteps=3000,
    #             nwalkers=10,
    #             sampler_kwargs=dict(pool=pool),
    #         )

    #     intercept, slope, _ = sampler.get_chain(flat=True, discard=1000).mean(axis=0)
    #     intercepts.append(intercept)
    #     slopes.append(slope)

    # pd.DataFrame({"slope": slopes, "intercept": intercepts}).to_csv(
    #     "results/chi2-mcmc-sampler.csv"
    # )

    # slopes = []
    # intercepts = []
    # for i in tqdm(range(1000)):
    #     age = age_df.groupby("snid").sample(1)["age"]
    #     age_err = np.zeros(len(age))
    #     hr = hr_df["hr"]
    #     hr_err = hr_df["hr_err"]

    #     results = run_linmix(age, age_err, hr, hr_err, linmix_kwargs=dict(K=2))

    #     slopes.append(results["slope"].mean())
    #     intercepts.append(results["intercept"].mean())
    # pd.DataFrame({"slope": slopes, "intercept": intercepts}).to_csv(
    #     "results/linmix-mcmc-sampler.csv"
    # )

    age_df = load_age_sample_from_mcmc_chains("campbell", mode="read")
    hr_df = load_hr("campbell")
    age_df, hr_df = clean_data(age_df, hr_df)

    age_matrix = np.array(list(age_df["age"].groupby("snid").agg(list)))

    with Pool() as pool:
        sampler = run_x_posterior_mcmc(
            age_matrix,
            hr_df["hr"],
            hr_df["hr_err"],
            nsteps=20,
            sampler_kwargs=dict(pool=pool),
        )

    np.savetxt("results/x_posterior_mcmc.csv", sampler.get_chain(flat=True), delimiter=",")


import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
def browser_web():
    options = webdriver.ChromeOptions()
    options.add_experimental_option("detach", True)
    options.add_argument("--user-data-dir=C:/Users/landa.DESKTOP-GJ08DGE/AppData/Local/Google/Chrome/User Data/")
    driver = webdriver.Chrome(options=options)
    yield driver
    driver.quit()

@pytest.fixture
def login_all_pets(browser_web):
    """Страница 'Все питомцы'"""
    browser_web.get('https://petfriends.skillfactory.ru/')
    browser_web.implicitly_wait(3)

    browser_web.find_element(By.CLASS_NAME, 'btn.btn-success').click()
    browser_web.find_element(By.XPATH, '/html/body/div/div/form/div[4]/a').click()
    browser_web.find_element(By.ID, "email").send_keys("log")
    browser_web.find_element(By.ID, "pass").send_keys("pass")
    browser_web.find_element(By.XPATH, "/html/body/div/div/form/div[3]/button").click()
    return browser_web

@pytest.fixture
def login_my_pets(login_all_pets):
    """Страница 'Мои питомцы'"""
    browser_web.implicitly_wait(3)
    login_all_pets.find_element(By.XPATH, '//*[@id="navbarNav"]/ul/li[1]/a').click()
    return login_all_pets


import csv
import io
import os

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

from reviews.models import Category, Comment, Genre, Review, Title

User = get_user_model()

DATAFOLDER = os.getcwd() + '//static//data//'

class Command(BaseCommand):
    help = "Импортирует информацию в БД из csv файлов в папке data"

    def handle(self, *args, **options):
        self.import_genres()
        self.import_categories()
        self.import_users()
        self.import_titles()
        self.import_reviews()

    def import_genres(self):
        with io.open(DATAFOLDER + 'genre.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                Genre.objects.get_or_create(name=row['name'], slug=row['slug'])

    def import_categories(self):
        with io.open(DATAFOLDER + 'category.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                Category.objects.get_or_create(name=row['name'],
                                               slug=row['slug'])

    def import_users(self):
        with io.open(DATAFOLDER + 'users.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                User.objects.get_or_create(id=row['id'],
                                           username=row['username'],
                                           email=row['email'],
                                           role=row['role'],
                                           bio=row['bio'],
                                           first_name=row['first_name'],
                                           last_name=row['last_name'])

    def import_titles(self):
        with io.open(DATAFOLDER + 'titles.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                Title.objects.get_or_create(
                    name=row['name'],
                    year=row['year'],
                    category=Category.objects.get(id=row['category']))

        with io.open(DATAFOLDER + 'genre_title.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                genre = Genre.objects.get(id=row['genre_id'])
                Title.objects.get(id=row['title_id']).genre.add(genre)

    def import_reviews(self):
        with io.open(DATAFOLDER + 'review.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                title = Title.objects.get(id=row['title_id'])
                author = User.objects.get(id=row['author'])
                Review.objects.get_or_create(title=title,
                                             text=row['text'],
                                             author=author,
                                             score=row['score'],
                                             pub_date=row['pub_date'])

    def import_comments(self):
        with io.open(DATAFOLDER + 'comments.csv', encoding='utf-8') as f:
            csv_file = csv.DictReader(f)
            for row in csv_file:
                review = Review.objects.get(id=row['review_id'])
                author = User.objects.get(id=row['author'])
                Comment.objects.get_or_create(review=review,
                                              text=row['text'],
                                              author=author,
                                              pub_date=row['pub_date'])


import os
import sys
current_dir = os.path.dirname(os.path.abspath('__file__'))
root_dir = os.path.abspath(os.path.join(current_dir, '../../'))
sys.path.append(root_dir)

from glob2 import glob
import joblib
from scipy.io import wavfile
import torch
import sudo_rm_rf.utils.progress_display as progress_display
from __config__ import *

def parse_info_from_name(preprocessed_dirname):
    """! Given a name of a preprocessed dataset root dirname infer all the
    encoded information
    Args:
        preprocessed_dirname: A dirname as the one follows:
        wsj0_2mix_8k_4s_min_preprocessed or even a dirpath
    Returns:
        min_or_max: String whether the mixtures are aligned with the maximum
        or the minimum number of samples of the constituting sources
        n_speakers: number of speakers in mixtures
        fs: sampling rate in kHz
        wav_timelength: The timelength in seconds of all the mixtures and
                        clean sources
    """
    try:
        dirname = os.path.basename(preprocessed_dirname)
        elements = dirname.split('_')
        min_or_max = elements[-2]
        assert (min_or_max == 'min' or min_or_max == 'max')
        wav_timelength = float(elements[-3][:-1])
        fs = float(elements[-4][:-1]) * 1000
        n_speakers = int(elements[-5].strip("mix"))

        return min_or_max, n_speakers, fs, wav_timelength

    except:
        raise IOError("The structure of the wsj0-mix preprocessed "
                      "dataset name is not in the proper format. A proper "
                      "format would be: "
                      "wsj0_{number of speakers}mix_{fs}k_{timelength}s_{min "
                      "or max}_preprocessed")

def infer_output_name(input_dirpath, wav_timelength):
    """! Infer the name for the output folder as shown in the example: E.g.
    for input_dirpath: wsj0-mix/2speakers/wav8k/min and for 4s timelength it
    would be wsj0_2mix_8k_4s_min_preprocessed
    Args: input_dirpath: The path of a wsj0mix dataset e.g.
                         wsj0-mix/2speakers/wav8k/min (for mixes with minimum
                         length)
          wav_timelength: The timelength in seconds of all the mixtures and
                          clean sources
    Returns: outputname: as specified in string format
             fs: sampling rate in Hz
             n_speakers: number of speakers in mixtures
    """

    try:
        elements = input_dirpath.lower().split('/')
        min_or_max = elements[-1]
        assert (min_or_max == 'min' or min_or_max == 'max')
        fs = int(elements[-2].strip('wav').strip('k')) * 1000
        n_speakers = int(elements[-3].strip("speakers"))
        output_name = "wsj0_{}mix_{}k_{}s_{}_preprocessed".format(
            n_speakers,
            fs / 1000.,
            float(wav_timelength),
            min_or_max)

        # test that the inferred output name is parsable back
        (inf_min_or_max,
         inf_n_speakers,
         inf_fs,
         inf_wav_timelength) = parse_info_from_name(output_name)
        assert(inf_min_or_max == min_or_max and
               inf_n_speakers == n_speakers and
               inf_fs == fs and
               inf_wav_timelength == wav_timelength)

        return output_name, fs, n_speakers, min_or_max

    except:
        raise IOError("The structure of the wsj0-mix is not in the right "
                "format. A proper format would be: "
                "wsj0-mix/{2 or 3}speakers/wav{fs in Hz}k/{min or max}")

def normalize_wav(wav, eps=10e-7, std=None):
    mean = wav.mean()
    if std is None:
        std = wav.std()
    return (wav - mean) / (std + eps)

def normalize_tensor_wav(wav, eps=10e-7, std=None):
    mean = wav.mean(-1, keepdim=True)
    if std is None:
        std = wav.std(-1, keepdim=True)
    return (wav - mean) / (std + eps)

def write_data_wrapper_func(input_dirpath,
                            clean_folders,
                            max_wav_samples,
                            output_dirpath,
                            min_or_max=None):

    def process_uid(uid):
        mix_path = os.path.join(input_dirpath, 'mix', uid)
        mix_wav = wavfile.read(mix_path)[1] / 29491.

        mix_wav = torch.tensor(mix_wav, dtype=torch.float32)

        sources_paths = [os.path.join(input_dirpath, fo, uid)
                         for fo in clean_folders]
        sources_w_list = [wavfile.read(p)[1] / 29491.
                          for p in sources_paths]
        sources_w_list = [torch.tensor(normalize_wav(np_vec),
                                       dtype=torch.float32).unsqueeze(0)
                          for np_vec in sources_w_list]
        sources_wavs = torch.cat(sources_w_list, dim=0)

        min_len = min(mix_wav.shape[-1], sources_wavs.shape[-1])
        if min_len < max_wav_samples:
            padded_mix_wav = torch.zeros(max_wav_samples)
            padded_sources_wavs = torch.zeros(sources_wavs.shape[0],
                                              max_wav_samples)
            padded_mix_wav[:mix_wav.shape[-1]] = mix_wav
            padded_sources_wavs[
                :, :min(max_wav_samples, sources_wavs.shape[-1])] = \
                sources_wavs[:, :min(max_wav_samples, sources_wavs.shape[-1])]
            mix_wav = padded_mix_wav
            sources_wavs = padded_sources_wavs

        mix_wav = mix_wav[:max_wav_samples]
        sources_wavs = sources_wavs[:, :max_wav_samples]
        mix_std = mix_wav.detach().cpu().numpy().std()
        mix_wav_norm = torch.tensor(normalize_wav(mix_wav),
                                    dtype=torch.float32)
        sources_wavs_norm = normalize_tensor_wav(sources_wavs,
                                                 std=mix_std)

        output_uid_folder = os.path.join(output_dirpath, uid)
        data = {
            'mixture_wav': mix_wav,
            'clean_sources_wavs': sources_wavs,
            'mixture_wav_norm': mix_wav_norm,
            'clean_sources_wavs_norm': sources_wavs_norm
        }

        if not os.path.exists(output_uid_folder):
            os.makedirs(output_uid_folder)

        for k, v in data.items():
            file_path = os.path.join(output_uid_folder, k)
            joblib.dump(v, file_path, compress=0)

    return lambda uid: process_uid(uid)

def convert_subset(input_dirpath,
                   output_dirpath,
                   fs,
                   n_speakers,
                   wav_timelength,
                   subset=None,
                   min_or_max=None):
    """! Convert a subset of files in the appropriate format
    Args:
        input_dirpath: The path of a wsj0mix dataset e.g.
                       wsj0-mix/2speakers/wav8k/min (for mixes with
                       minimum length)
        output_dirpath: The path for storing the new dataset
                        (the directories would be created recursively)
        n_speakers: number of speakers in mixtures
        fs: sampling rate in Hz
        wav_timelength: The timelength in seconds of all the mixtures and
                        clean sources
    Generates: The structure in output_dirpath where each unique id would
    have a corresponding folder with all the files in tensors or .txt for
    configs
            e.g. uid: 423a0105_1.2681_446o030n_-1.2681.wav -->
            output_dirpath/423a0105_1.2681_446o030n_-1.2681
                          mixture_wav (tensor)
                          clean_sources_wavs (tensor)
    """
    mixtures_dir = os.path.join(input_dirpath, 'mix')
    files = glob(mixtures_dir + '/*.wav')
    unique_ids = set([os.path.basename(f) for f in files])
    max_wav_samples = int(wav_timelength * fs)

    clean_folders = ['s'+str(n+1) for n in range(n_speakers)]

    write_data_func = write_data_wrapper_func(input_dirpath,
                                              clean_folders,
                                              max_wav_samples,
                                              output_dirpath,
                                              min_or_max=min_or_max)

    progress_display.progress_bar_wrapper(
        write_data_func,
        list(unique_ids),
        message='{}: Processing {} files...'.format(
            subset, len(unique_ids)))

def convert_wsj0mix_to_universal_dataset(input_dirpath,
                                         output_dirpath,
                                         wav_timelength):
    """! This function converts the wsj0mix dataset in a universal
    dataset where each sample has each own folder and all values are
    stored as Tensors for efficiency and in order to be loaded
    irrespective of the structure of the dataset.
    Args:
        input_dirpath: The path of a wsj0mix dataset e.g.
                       wsj0-mix/2speakers/wav8k/min (for mixes with
                       minimum length)
        output_dirpath: The path for storing the new dataset
                        (the directories would be created recursively)
        wav_timelength: The timelength in seconds of all the mixtures
                        and clean sources
    Intermediate:
        output_name: Default name would be infered as follows:
                     E.g. for wsj0-mix/2speakers/wav8k/min and for 4s
                     timelength:
                     it would be wsj0_2mix_8k_4s_min_preprocessed
    """
    output_name, fs, n_speakers, min_or_max = infer_output_name(
        input_dirpath, wav_timelength)

    root_out_dir = os.path.join(output_dirpath, output_name)

    subsets = os.listdir(input_dirpath)
    print("All the subsets are: {}".format(subsets))
    for subset in subsets:
        subset_input_dirpath = os.path.join(input_dirpath, subset)
        subset_output_dirpath = os.path.join(root_out_dir, subset)
        convert_subset(subset_input_dirpath,
                       subset_output_dirpath,
                       fs,
                       n_speakers,
                       wav_timelength,
                       subset=subset,
                       min_or_max=min_or_max)

    print("Dataset ready at: {}".format(root_out_dir))

def run_all():
    input_dirpath = os.path.join(WSJ0_MIX_2_8K_PATH, 'max')
    output_dirpath = WSJ0_MIX_2_8K_PREPROCESSED_BASE_P
    wav_timelength = 4
    convert_wsj0mix_to_universal_dataset(input_dirpath,
                                         output_dirpath,
                                         wav_timelength)

if __name__ == "__main__":
    run_all()


import numpy as np
from PIL import Image
import torch
import torchvision.transforms.functional as F
import matplotlib.pyplot as plt
from model import DeepLab
from dataset import camvid_colors
import os
from tqdm import tqdm

device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
model = DeepLab(num_classes=32).to(device)
model.load_state_dict(torch.load('output/weight.pth', map_location=device))
model.eval()

if __name__ == '__main__':
    for img_name in tqdm(os.listdir('data/CamVid/test/')):
        # input
        image = Image.open('data/CamVid/test/' + img_name)
        image = F.resize(image, [512, 512])
        image = F.to_tensor(image).unsqueeze(0)

        # ground truth
        gt = Image.open('data/CamVid/test_labels/' + img_name.split('.')[0] + '_L.png')
        gt = F.resize(gt, [512, 512])

        # output
        with torch.no_grad():
            mask = model(image.float().to(device))

        mask = torch.argmax(mask.squeeze(), dim=0)
        im_mask = np.zeros([512, 512, 3]).astype(np.uint8)
        for i in range(512):
            for j in range(512):
                im_mask[i, j] = list(camvid_colors.items())[mask[i, j]][1]

        # save result
        fig = plt.figure()
        fig.add_subplot(1, 3, 1)
        plt.imshow(image.squeeze().permute(1, 2, 0))
        plt.axis('off')
        plt.title('Input')

        fig.add_subplot(1, 3, 2)
        plt.imshow(im_mask)
        plt.axis('off')
        plt.title('Output')

        fig.add_subplot(1, 3, 3)
        plt.imshow(gt)
        plt.axis('off')
        plt.title('Ground truth')

        plt.savefig('results/' + img_name, dpi=200, bbox_inches='tight')
        plt.close()


'''from PIL import Image
img = Image.open('images/letters/set1/blue/75.png', 'r')
img = img.convert("RGBA")
img_w, img_h = img.size
background = Image.open('../test.png', 'r')#Image.new('RGBA', (1440, 900), (255, 255, 225, 225))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(img, offset)
background.save('out3.png')
'''
import textwrap
import os
from PIL import Image

def textlist(text): # converts text into list of strings
	list1 = text.split("<br />")

	textlist = []
	for text in list1:
		textlist= textlist + textwrap.wrap(text,width = 33)
	return(textlist)

scale = 15
xoff = 42
yoff = 40
line_spacing = 23

letter_spacing = 10
max_lines = 18

inputtext = 'test.text'
f = open(inputtext, "r")
text = textlist(f.read())
f.close

letterlist = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.?{}()-_+=*&^%@<>|/'\"\\;:"

#for char in text:
filename = 'images/letters/set1/blue/{}.png'
bg_file = 'images/bg/ruled.jpg'
save_name = 'page{}.png'
bg = Image.open(bg_file, 'r')
bg_w, bg_h = bg.size
text_img = Image.new('RGBA', (bg_w, bg_h), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
image_list = []

i = 0
j = -1
img_num = 0

#str1 = '\n'
#str1 += str1.join(text)
for str1 in text:
	print(str1)
	i = 0
	j +=1
	for letts in str1:
		if(letts in letterlist):
			if i*letter_spacing < bg_w - xoff - 2*scale:
				pass
			else:
				i = 0
				j += 1

			if j > max_lines:
				text_img.save(save_name.format(img_num), format="png")
				text_img = text_img.convert('RGB')#.resize((2480, 3508), Image. ANTIALIAS)
				image_list.append(text_img)
				img_num += 1
				i = 0
				j = 0
				text_img = Image.new('RGBA', (bg_w, bg_h), (0, 0, 0, 0))
				text_img.paste(bg, (0,0))
			if ord(letts) == 32 and i==0:
				pass
			else:
				try:
					ironman = Image.open(filename.format(ord(letts)), 'r').resize((2*scale,3*scale))
				except:
					ironman = Image.open(filename.format(ord(letts)), 'r').resize((2*scale,3*scale))
				text_img.paste(ironman, (i*letter_spacing + xoff,j*line_spacing + yoff), mask=ironman)

			i += 1

text_img.save(save_name.format(img_num), format="png")
text_img = text_img.convert('RGB')#.resize((2480, 3508), Image. ANTIALIAS)
image_list.append(text_img)

image_list[0].save('imagelist.pdf',save_all=True, append_images=image_list[1:])


from django.contrib.auth import get_user_model
from django.db import models

User = get_user_model()

class Tag(models.Model):
    name = models.CharField(verbose_name='Имя тэга',
                            max_length=200, unique=True)
    color = models.CharField(verbose_name='Цвет тэга',
                             max_length=7, unique=True)
    slug = models.SlugField(verbose_name='Слаг', max_length=200,
                            unique=True)

    def __str__(self):
        return f'{self.name} цвет {self.color}'

    class Meta:
        verbose_name = 'Тэг'
        verbose_name_plural = 'Тэги'
        ordering = ('name', )

class Ingredient(models.Model):
    name = models.CharField(verbose_name='Имя ингредиента', max_length=150)
    measurement_unit = models.CharField(
        verbose_name='Единицы измерения', max_length=10)

    def __str__(self):
        return f'{self.name} {self.measurement_unit}'

    class Meta:
        verbose_name = 'Ингредиент'
        verbose_name_plural = 'Ингредиенты'

class Recipe(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                               verbose_name='Автор рецепта',
                               related_name='recipes')
    name = models.CharField(max_length=200, verbose_name='Название рецепта')
    text = models.TextField(verbose_name='Текст рецепта')
    cooking_time = models.PositiveSmallIntegerField()
    image = models.ImageField(
        verbose_name='Фото блюда', upload_to='recipe_images/',
        default=None, null=True)
    tags = models.ManyToManyField(
        Tag, through='TagRecipe')
    ingredients = models.ManyToManyField(
        Ingredient, through='IngredientRecipe', through_fields=('recipe',
                                                                'ingredient'))
    pub_date = models.DateTimeField(
        verbose_name='Дата публикации',
        auto_now_add=True,
        editable=False,
    )

    def __str__(self) -> str:
        return f'{self.name}. Автор: {self.author}'

    class Meta:
        verbose_name = 'Рецепт'
        verbose_name_plural = 'Рецепты'
        ordering = ('-pub_date',)

class IngredientRecipe(models.Model):
    ingredient = models.ForeignKey(
        Ingredient, on_delete=models.CASCADE, verbose_name='Ингредиент')
    recipe = models.ForeignKey(
        Recipe, on_delete=models.CASCADE, verbose_name='Рецепт')
    amount = models.PositiveIntegerField(
        verbose_name='Количество ингридиентов')

    def __str__(self):
        return f'{self.ingredient} {self.recipe}'

    class Meta:
        verbose_name = 'Ингредиент рецепта'
        verbose_name_plural = 'Ингредиенты рецепта'

class TagRecipe(models.Model):
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE,
                            verbose_name='Тэги рецепта')
    recipe = models.ForeignKey(
        Recipe, on_delete=models.CASCADE, verbose_name='Рецепт')

    def __str__(self):
        return f'{self.tag} {self.recipe}'

    class Meta:
        verbose_name = 'Ингредиент рецепта'
        verbose_name_plural = 'Ингредиенты рецепта'

class FavoriteRecipe(models.Model):
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='favorite',
        verbose_name='Пользователь'
    )
    favorite_recipe = models.ForeignKey(
        Recipe,
        on_delete=models.CASCADE,
        related_name='favorite_recipe',
        verbose_name='Избранный рецепт'
    )

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=('user', 'favorite_recipe'),
                name='unique favourite')]
        verbose_name = 'Избранное'
        verbose_name_plural = 'Избранные'
        ordering = ('id',)

    def __str__(self):
        return (f'Пользователь: {self.user.username}'
                f'рецепт: {self.favorite_recipe.name}')

class ShoppingCart(models.Model):
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='shopping_cart',
        verbose_name='Пользователь'
    )
    recipe = models.ForeignKey(
        Recipe,
        on_delete=models.CASCADE,
        related_name='recipe_shopping_cart',
        verbose_name='Рецепт'
    )

    class Meta:
        ordering = ('id',)
        constraints = [
            models.UniqueConstraint(
                fields=('user', 'recipe'),
                name='unique recipe in shopping cart')]
        verbose_name = 'Список покупок'
        verbose_name_plural = 'Список покупок'

    def __str__(self):
        return (f'Пользователь: {self.user.username},'
                f'рецепт в списке: {self.recipe.name}')


import gzip
import io
from urllib import parse

from env_test_env import *

try:
    import weed
except Exception as e:
    print(f'Could not import weed, err: {e}')
    import traceback

    traceback.print_exc()

# try:
#     import python_weed
# except Exception as e:
#     print(f'Could not import python_weed, err: {e}')
#     import traceback
#     traceback.print_exc()

# import weed
# print(weed)
from weed.conf import *
from weed.util import *
from weed.master import *
from weed.volume import *
from weed.operation import *
from weed.filer import WeedFiler

set_global_logger_level(logging.DEBUG)

WEED_MASTER_IP = 'localhost'
WEED_MASTER_PORT = 9333

# noinspection DuplicatedCode
def test_weed_master():
    master = WeedMaster()
    assert master.__repr__()

    # assign
    assign_key_dict = master.acquire_assign_info()
    assert isinstance(assign_key_dict, dict)
    assert 'fid' in assign_key_dict
    assert int(assign_key_dict['fid'].replace(',', ''), 16) > 0x001
    fid = assign_key_dict['fid']
    volume_id = fid.split(',')[0]
    assert len(volume_id) >= 1

    # lookup
    lookup_dict = master.lookup(fid)
    assert isinstance(lookup_dict, dict)
    assert 'locations' in lookup_dict
    locations_list = lookup_dict['locations']
    assert 'url' in locations_list[0]
    assert 'publicUrl' in locations_list[0]

    # vacuum
    vacuum_dict = master.vacuum()
    assert isinstance(vacuum_dict, dict)
    assert 'Topology' in vacuum_dict

    # status
    status_dict = master.get_status()
    assert isinstance(status_dict, dict)
    assert 'Topology' in status_dict

def test_weed_master_2():
    master = WeedMaster()
    assert master.__repr__()

    # assign
    wak = master.acquire_new_assign_key(10)
    assert isinstance(wak, dict)
    assert 'fid' in wak

    fid = wak['fid']

    fids = [fid] + [fid + '_' + str(i + 1) for i in range(10)]
    locations = []
    for i in fids:
        locations_dict = master.lookup(i)
        assert 'locations' in locations_dict
        locations.append(locations_dict['locations'])

    for i, l in enumerate(locations):
        if i < (len(l) - 1):
            assert l[i] == l[i + 1]

def test_weed_volume():
    volume = WeedVolume()
    assert volume.__repr__()

    # status
    status_dict = volume.get_status()
    assert isinstance(status_dict, dict)
    assert 'Version' in status_dict
    assert 'Volumes' in status_dict

def test_weed_assign_key():
    wak = WeedAssignKey()
    assert wak
    assert wak.__repr__()

def test_weed_assign_key_extended():
    wak = WeedAssignKeyExtended()
    assert wak
    assert wak.__repr__()

def test_weed_operation_response():
    wor = WeedOperationResponse()
    assert wor.status == Status.FAILED  # init is FAILED
    assert wor.__repr__()

# noinspection DuplicatedCode
def test_volume_put_get_delete_file():
    master = WeedMaster()
    assert master.__repr__()

    # assign
    assign_key_dict = master.get_assign_key()
    assert isinstance(assign_key_dict, dict)
    assert 'fid' in assign_key_dict
    assert int(assign_key_dict['fid'].replace(',', ''), 16) > 0x001
    fid = assign_key_dict['fid']
    volume_id = fid.split(',')[0]
    assert len(volume_id) >= 1

    # lookup
    lookup_dict = master.lookup(fid)
    assert isinstance(lookup_dict, dict)
    assert 'locations' in lookup_dict
    locations_list = lookup_dict['locations']
    assert 'url' in locations_list[0]
    assert 'publicUrl' in locations_list[0]

    volume_url = 'http://' + locations_list[0]['publicUrl']
    url = parse.urlparse(volume_url)
    assert url is not None

    # volume = WeedVolume(host=url_base.hostname, port=url_base.port)
    volume = master.get_volume(fid)
    status_dict = volume.get_status()
    assert isinstance(status_dict, dict)
    assert 'Version' in status_dict
    assert 'Volumes' in status_dict
    assert isinstance(status_dict['Volumes'], list)

    # file_to_post = './test_file_to_post_to_weed.xml'
    file_to_update = 'test_file_to_post_to_weed.xml'
    file_to_post = os.path.join(TEST_PATH, file_to_update)

    with open(file_to_post, 'wb') as tmp_file:
        # tmp_file.write(b"nonsense " * 1024 * 256)
        tmp_file.write(b"nonsense " * 8)
    put_result = volume.put_file(os.path.abspath(file_to_post), fid)
    assert 'error' not in put_result
    assert 'size' in put_result

    data = volume.get_file(fid)
    assert data
    # print data
    with open(file_to_post, 'rb') as fd:
        file_data = fd.read()
    assert data == file_data

    delete_result = volume.delete_file(fid)
    assert delete_result
    assert b'error' not in delete_result
    assert b'size' in delete_result

def test_accquire_new_fid():
    op = WeedOperation()
    fids = op.acquire_new_fids()
    assert isinstance(fids, (tuple, list))
    assert ',' in fids[0]
    count = 5
    fids = op.acquire_new_fids(count)
    for i in range(count):
        assert ',' in fids[i]

def test_put_a_file_with_fid():
    op = WeedOperation()
    fid = op.acquire_new_fids()[0]
    file_name = 'test_opensource_logo.jpg'
    fpath = os.path.join(TEST_PATH, file_name)
    assert op.put(open(fpath, 'rb'), fid, file_name)

def test_put_file():
    # test put_file in weed.util
    op = WeedOperation()
    # fid = ''
    file_name = 'test_opensource_logo.jpg'
    fpath = os.path.join(TEST_PATH, file_name)
    master = WeedMaster()
    fid_full_url = master.acquire_new_assign_key()['fid_full_url']
    print(('test_weed.py, fid_full_url: %s, fpath: %s' % (fid_full_url, fpath)))
    with open(fpath, 'rb') as f:
        g_logger.info('fp position: %d' % f.tell())
        g_logger.info('fp info: length: %d' % len(f.read()))
        f.seek(0)

        rsp = put_file(f, fid_full_url, file_name)
        assert rsp.ok()
        assert rsp.storage_size > 0
        fid = os.path.split(fid_full_url)[1]

    # read
    wor = op.crud_read(fid, file_name)
    assert wor
    content = wor.content
    assert len(content) > 3
    with open(fpath, 'rb') as _:
        original_content = _.read()
    assert content == original_content

def test_file_operations():
    op = WeedOperation()
    assert op.__repr__()

    # create
    # fid = ''
    file_name = 'test_opensource_logo.jpg'
    fpath = os.path.join(TEST_PATH, file_name)
    with open(fpath, 'rb') as f:
        rsp = op.crud_create(f, file_name)
        assert rsp.ok()
        assert len(rsp.fid) > 3
        fid = rsp.fid
        print(fid)
        assert len(fid) > 1
        assert os.path.split(op.get_fid_full_url(fid))[1] == fid
        assert rsp.fid.replace(',', '') > '01'
        assert rsp.url
        assert rsp.storage_size > 0

    # test exists
    assert op.exists(fid)
    assert op.exists(fid + 'wrong_fid') is False
    assert op.exists('wrong_fid') is False
    assert op.exists(fid.replace(',', '/')) is False

    # read
    content = op.get(fid, file_name).content
    content2 = op.get_content(fid, file_name)
    assert content == content2
    with open(fpath, 'rb') as f:
        original_content = f.read()
    assert content == original_content

    # update
    file_to_update = 'test_file_to_post_to_weed.xml'
    fpath = os.path.join(TEST_PATH, file_to_update)
    with open(fpath, 'rb+') as tmp_file:
        tmp_file.write(b"testdata " * 1024 * 256)
        tmp_file.seek(0)
        rsp = op.crud_update(tmp_file, fid)
        assert rsp
        fid_2 = rsp.fid
        assert fid_2 == fid
        assert rsp.fid.replace(',', '') > '01'
        assert rsp.url
        assert rsp.storage_size > 0

    # delete
    rsp = op.crud_delete(fid)
    assert rsp.storage_size > 0
    rsp = op.crud_delete('3,20323023')  # delete an unexisted file, should return False
    assert rsp.storage_size == 0

def test_weed_filer():
    wf = WeedFiler()
    assert wf.uri == 'localhost:27100'
    assert wf.url_base == 'http://localhost:27100'

    d = '/test/'
    new_d = '/test/new_dir/'

    # put f1
    f1_content = 'hello, how are you'
    f1 = io.StringIO(f1_content)
    f1_path = d + 'test.txt'
    assert wf.put(f1, f1_path) == f1_path

    # mkdir
    assert wf.mkdir(new_d)

    # put f2
    f2_content = 'hello, how are you?'
    f2 = io.StringIO(f2_content)
    f2_path = new_d + 'hello.txt'
    assert wf.put(f2, f2_path) == f2_path

    # list
    j = wf.list(new_d)
    assert isinstance(j, dict)
    # print(j)

    if 'Directory' in j:
        assert j['Directory'] == '/test/new_dir/'
    if 'Path' in j:
        assert j['Path'] == '/test/new_dir'
    assert j['Entries']
    entries = j['Entries']
    file_names = []
    for f in entries:
        if 'FullPath' in f:
            file_names.append(f['FullPath'].split('/')[-1])

    assert 'hello.txt' in file_names

    # get f1
    wf_get = wf.get(f1_path)
    assert int(wf_get['content_length']) > 0
    if 'gzip' in wf_get['content_type']:
        content_gz = wf_get['content']
        content = gzip.open(io.BytesIO(content_gz)).read()
        assert content == f1_content
    elif 'text' in wf_get['content_type']:
        assert wf_get['content'] == str.encode(f1_content)
    else:
        pass

    # delete f1, f2
    assert wf.delete(f1_path)
    assert wf.delete(f2_path)

if __name__ == '__main__':
    test_weed_master()
    test_weed_volume()
    test_file_operations()
    test_volume_put_get_delete_file()


"""The setup script."""
import os

from setuptools import find_packages, setup

REQUIREMENTS = [
    "click>=8.1.1",
    "colorama>=0.4.4",
    "dateparser>=1.1.4",
    "tzlocal>=4.0",
    "backports.zoneinfo>=0.2.1;python_version < '3.9'",
    "click-default-group>=1.2.2",
    "peewee>=3.15.2",
    "platformdirs>=2.6.0",
]

curr_dir = os.path.abspath(os.path.dirname(__file__))

def get_file_text(file_name):
    with open(os.path.join(curr_dir, file_name), encoding="utf-8") as in_file:
        return in_file.read()

_init = {}
_init_file = os.path.join(curr_dir, "workedon", "__init__.py")
with open(_init_file) as fp:
    exec(fp.read(), _init)
name = _init["__name__"]
author = _init["__author__"]
email = _init["__email__"]
version = _init["__version__"]

setup(
    name=name,
    version=version,
    description="Work tracking from your shell.",
    long_description=get_file_text("README.md") + "\n\n" + get_file_text("CHANGELOG.md"),
    long_description_content_type="text/markdown",
    author=author,
    author_email=email,
    maintainer=author,
    maintainer_email=email,
    license="MIT license",
    packages=find_packages(include=["workedon"], exclude=["tests", "tests.*"]),
    include_package_data=True,
    zip_safe=False,
    classifiers=[
        "Development Status :: 4 - Beta",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Natural Language :: English",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    url="https://github.com/viseshrp/workedon",
    project_urls={
        "Documentation": "https://github.com/viseshrp/workedon#readme",
        "Changelog": "https://github.com/viseshrp/workedon/blob/develop/CHANGELOG.md",
        "Bug Tracker": "https://github.com/viseshrp/workedon/issues",
        "Source Code": "https://github.com/viseshrp/workedon",
        "CI": "https://github.com/viseshrp/workedon/actions",
    },
    python_requires=">=3.7",
    keywords="workedon work worklog log journal",
    install_requires=REQUIREMENTS,
    entry_points={
        "console_scripts": [
            "workedon=workedon.__main__:main",
        ],
    },
)