import argparse
import torch
import torch.nn as nn
import numpy as np
import time
import tril_cuda

class trilFunction(torch.autograd.Function):
    # Note that both forward and backward are @staticmethods
    @staticmethod
    def forward(ctx, input,row,column,diag):
        ctx.save_for_backward(input,row,column,diag)
        output = tril_cuda.forward(input,row,column,diag)

        return output[0]

class tril(nn.Module):
    def __init__(self):
        super(tril, self).__init__()

    def forward(self, input,row,column,diag):
        return trilFunction.apply(input,row,column,diag)

def verify(device):
    batch_size = 77
    row = 100
    column = 88
    diag = 3
    shape = [batch_size, row, column]

    my_tril = tril().to(device)

    # correctness
    in_t = torch.rand(shape).to(device)
    out_torch =  torch.tril(in_t, diag).detach()

    in_t = torch.flatten(in_t)
    out_my = my_tril(in_t, row, column, diag).detach()
    out_my = torch.reshape(out_my, shape)

    np.testing.assert_allclose(out_my.cpu().numpy(), out_torch.cpu().numpy(), 1e-4)

def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    # device = torch.device("cpu")

    verify(device)

if __name__ == '__main__':
    main()


from __future__ import print_function
import os
from os.path import dirname, join, abspath, isdir
import sys
import json
import requests
try:
    from urlparse import urljoin
except:
    from urllib.parse import urljoin        # python 3.x

# http://python-redmine.readthedocs.org/
from redmine import Redmine

if __name__=='__main__':
    SRC_ROOT = dirname(dirname(abspath(__file__)))
    sys.path.append(SRC_ROOT)

from datetime import datetime
from utils.msg_util import *

class RedmineIssueDownloader:
    """
    For a given Redmine project, download the issues in JSON format
    """

    #TIME_FORMAT_STRING = '%Y-%m%d-%H%M'
    TIME_FORMAT_STRING = '%Y-%m%d'

    # Redmine tickets are written to JSON files with the naming convention  "(issue id).json"
    # For file sorting, preceding zeros are tacked on.
    #   Example: If  ZERO_PADDING_LEVEL=5:
    #               issue #375 is written to file "00375.json"
    #               issue #1789 is written to file "01789.json"
    #               issue #2 is written to file "00002.json"
    #
    # If your issue numbers go beyond 99,999 then increase the ZERO_PADDING_LEVEL
    #
    ZERO_PADDING_LEVEL = 5

    def __init__(self, redmine_server, redmine_api_key, project_name_or_identifier, issues_base_directory, **kwargs):
        """
        Constructor

        :param redmine_server: str giving the url of the redmine server.  e.g. https://redmine.myorg.edu/
        :param redmine_api_key: str with a redmine api key
        :param project_name_or_identifier: str or int with either the redmine project id or project identifier
        :param issues_base_directory: str, directory to download the redmine issues in JSON format.  Directory will be crated
        :param specific_tickets_to_download: optional, list of specific ticket numbers to download. e.g. [2215, 2216, etc]
        """
        self.redmine_server = redmine_server
        self.redmine_api_key = redmine_api_key
        self.project_name_or_identifier = project_name_or_identifier
        self.issues_base_directory = issues_base_directory
        self.issue_status = kwargs.get('issue_status', '*') # values 'open', 'closed', '*'

        self.specific_tickets_to_download = kwargs.get('specific_tickets_to_download', None)

        self.redmine_conn = None
        self.redmine_project = None

        self.issue_dirname = join(self.issues_base_directory\
                                ,  datetime.today().strftime(RedmineIssueDownloader.TIME_FORMAT_STRING)\
                                )

        self.setup()

    def setup(self):
        self.connect_to_redmine()
        if not isdir(self.issue_dirname):
            os.makedirs(self.issue_dirname)
            msgt('Directory created: %s' % self.issue_dirname)

    def connect_to_redmine(self):
        self.redmine_conn = Redmine(self.redmine_server, key=self.redmine_api_key)
        self.redmine_project = self.redmine_conn.project.get(self.project_name_or_identifier)
        msg('Connected to server [%s] project [%s]' % (self.redmine_server, self.project_name_or_identifier))

    def get_issue_count(self):
        msgt('get_issue_count')

        issue_query_str = 'issues.json?project_id=%s&limit=1&status_id=%s' \
                            % (self.project_name_or_identifier, self.issue_status)

        url = urljoin(self.redmine_server, issue_query_str)

        msg('Issue count url: %s' % url)

        # Note: Auth purposely uses the API KEY "as a username with a random password via HTTP Basic authentication"
        #   from: http://www.redmine.org/projects/redmine/wiki/Rest_api
        #
        auth = (self.redmine_api_key, 'random-pw')
        r = requests.get(url, auth=auth)
        if not r.status_code == 200:
            msgt('Error!')
            msg(r.text)
            raise Exception("Request for issue count failed! Status code: %s\nUrl: %s\nAuth:%s" % (r.status_code, url, auth))

        msg('Convert result to JSON')
        try:
            data = r.json()     # Let it blow up
        except:
            msgt('Error!')
            msg('Data from request (as text): %s' % r.text)
            raise Exception('Failed to convert issue count data to JSON.\nUrl: %s\nAuth:%s" % (url, auth)')

        if not data.has_key('total_count'):
            msgx('Total count not found in data: \n[%s]' % data)

        return data['total_count']

        """
from __future__ import print_function
import requests

project_id = 'dvn'
redmine_api_key = 'some-key'
url = 'https://redmine.hmdc.harvard.edu/issues.json?project_id=%s&limit=1' % project_id

#---------------------
# Alternative 1
#---------------------
auth = (redmine_api_key, 'random-pw')
r = requests.get(url, auth=auth)
print (r.text)
print (r.status_code)
data = r.json()
print (data['total_count'])

#---------------------
# Alternative 2
#---------------------
url2 = '%s&key=%s' % (url, redmine_api_key)
r = requests.get(url2)
print (r.text)
print (r.status_code)
data = r.json()
print (data['total_count'])

"""

    def write_issue_list(self, issue_fname, issue_dict):
        if issue_fname is None or not type(issue_dict) == dict:
            msgx('ERROR: write_issue_list, issue_fname is None or issue_dict not dict')
            return
        fh = open(issue_fname, 'w')
        fh.write(json.dumps(issue_dict))
        fh.close()
        msg('file updated: %s' % issue_fname)

    def show_project_info(self):
        msg(self.redmine_project._attributes)

    def download_tickets2(self):
        """
        fyi: Retrieving total count via regular api, not python redmine package
        """
        issue_dict = {}
        issue_fname = join(self.issue_dirname, 'issue_list.json')
        msg('Gathering issue information.... (may take a minute)')

        ticket_cnt = self.get_issue_count()

        RECORD_RETRIEVAL_SIZE = 100

        num_loops = ticket_cnt / RECORD_RETRIEVAL_SIZE
        extra_recs = ticket_cnt % RECORD_RETRIEVAL_SIZE
        if extra_recs > 0:
            num_loops+=1
        #num_loops=3
        msg('num_loops: %d' % num_loops)
        msg('extra_recs: %d' % extra_recs)

        cnt = 0
        for loop_num in range(0, num_loops):
            start_record = loop_num * RECORD_RETRIEVAL_SIZE
            end_record = (loop_num+1) * RECORD_RETRIEVAL_SIZE

            msgt('Retrieve records via idx (skip last): %s - %s' % (start_record, end_record))

            # limit of 100 is returning 125
            rec_cnt = 0
            for item in self.redmine_conn.issue.filter(project_id=self.project_name_or_identifier, status_id=self.issue_status, sort='id', offset=start_record)[:RECORD_RETRIEVAL_SIZE]: #, limit=RECORD_RETRIEVAL_SIZE):   #[start_record:end_record]
                rec_cnt +=1
                cnt +=1
                msg('(%s) %s - %s' % (rec_cnt, item.id, item.subject))

                if self.specific_tickets_to_download is not None:
                    # only download specific tickets
                    #
                    if item.id in self.specific_tickets_to_download:
                        self.save_single_issue(item)
                        issue_dict[self.pad_issue_id(item.id)] = item.subject
                    continue    # go to next item
                else:
                    # Get all tickets
                    #
                    self.save_single_issue(item)
                    issue_dict[self.pad_issue_id(item.id)] = item.subject
                if rec_cnt == RECORD_RETRIEVAL_SIZE:
                    break
                #continue
                #self.save_single_issue(item)
            self.write_issue_list(issue_fname, issue_dict)

    def pad_issue_id(self, issue_id):
        if issue_id is None:
            msgx('ERROR. pad_issue_id. The "issue_id" is None')

        return ('%s' % issue_id).zfill(self.ZERO_PADDING_LEVEL)

    def save_single_issue(self, single_issue):
        """
        Write a single issue object to a file using JSON format

        :param single_issue: Issue object
        """
        if single_issue is None:
            msgx('ERROR. download_single_issue. The "single_issue" is None')

        ## FIX: Expensive adjustment -- to pull out full relation and journal info
        json_str = self.get_single_issue(single_issue.id)       # another call to redmine

        #json_str = json.dumps(single_issue._attributes, indent=4)

        fullpath = join(self.issue_dirname, self.pad_issue_id(single_issue.id) + '.json')
        open(fullpath, 'w').write(json_str)
        msg('Ticket retrieved: %s' % fullpath)

    def process_files(self, issues_dirname=None):
        if issues_dirname is None:
            issues_dirname = self.issue_dirname

        tracker_info = []
        status_info = []
        priority_info = []

        fnames = [x for x in os.listdir(issues_dirname) if x.endswith('.json')]
        for fname in fnames:
            content = open(join(issues_dirname, fname), 'rU').read()
            d = json.loads(content)

            # Tracker Info
            tracker = d.get('tracker', None)
            if tracker:
                tracker_str = '%s|%s' % (tracker['id'], tracker['name'])
                if not tracker_str in tracker_info:
                    tracker_info.append(tracker_str)
            # Status Info
            status = d.get('status', None)
            if status:
                status_str = '%s|%s' % (status['id'], status['name'])
                if not status_str in status_info:
                    status_info.append(status_str)

            # Priority Info
            priority = d.get('priority', None)
            if priority:
                priority_str = '%s|%s' % (priority['id'], priority['name'])
                if not priority_str in priority_info:
                    priority_info.append(priority_str)
        #print d.keys()
        msg(tracker_info)
        msg(status_info)
        msg(priority_info)

    def get_single_issue(self, issue_id):
        """
        Download a single issue

        :param ticket_id: int of issue id in redmine
        :returns: json string with issue information
        """
        # test using .issue.get
        issue = self.redmine_conn.issue.get(issue_id, include='children,journals,watchers,relations')
        json_str = json.dumps(issue._attributes, indent=4)
        msg('Issue retrieved: %s' % issue_id)
        return json_str

if __name__=='__main__':
    from settings.base import REDMINE_SERVER, REDMINE_PROJECT_ID, REDMINE_API_KEY, REDMINE_ISSUES_DIRECTORY
    #rn = RedmineIssueDownloader(REDMINE_SERVER, REDMINE_API_KEY, 'dvn', REDMINE_ISSUES_DIRECTORY)
    #Only import some specific tickets
    #kwargs = dict(specific_tickets_to_download=[1371, 1399, 1843, 2214, 2215, 2216, 3362, 3387, 3397, 3400, 3232, 3271, 3305, 3426, 3425, 3313, 3208])
    rn = RedmineIssueDownloader(REDMINE_SERVER, REDMINE_API_KEY, REDMINE_PROJECT_ID, REDMINE_ISSUES_DIRECTORY)
    rn.download_tickets2()

    msg(rn.get_issue_count())
    #rn.show_project_info()
    #rn.process_files()
    #msg(rn.get_single_issue(3232))

"""
import json
c = open('issue_list2.txt', 'rU').read()
d = json.loads(c)
print(len(d))
"""


# helper functions to serialize peak times
import sqlite3

import pyabf

import patch_clamp.database as db

PEAK_INS_QUERY = """INSERT INTO peak_times
                    VALUES (:fname, :fpath, :mouse_id, :cell_side, :cell_n,
                            :membrane_potential, :include, :protocol,
                            :treatment, :sweep, :current, :peak_time)"""

SWEEP_TO_CURRENT_MAP = {
    0: -0.05,
    1: -0.025,
    2: 0.0,
    3: 0.025,
    4: 0.05,
    5: 0.075,
    6: 0.1,
    7: 0.125,
    8: 0.15,
    9: 0.175,
    10: 0.2,
    11: 0.225,
    12: 0.25,
    13: 0.275,
    14: 0.3,
    15: 0.325,
    16: 0.35,
    17: 0.375,
    18: 0.4,
    19: 0.425,
    20: 0.45,
    21: 0.475,
    22: 0.5,
}

def get_groups_per_sweep(sweep, include, query, db_path):
    """pulls out all the data in a sweep and structures it as a dict.
    String of integers (`peaks`) is transformed into a list of ints or empty list."""
    con = db.persistent_connection_to_db(db_path)
    con.row_factory = sqlite3.Row
    data = con.execute(query, (sweep, include))
    extracted = [
        {
            "fname": p["fname"],
            "fpath": p["fpath"],
            "mouse_id": p["mouse_id"],
            "cell_side": p["cell_side"],
            "cell_n": p["cell_n"],
            "memb_potential": p["membrane_potential_uncorrected"],
            "include": p["include"],
            "protocol": p["protocol"],
            "peaks": db.str_list_to_list_ints(p["peak_index"]),
            "treatment": p["treatment_group"],
            "sweep": sweep,
        }
        for p in data
    ]
    con.close()
    return extracted

def get_x(item):
    """adds the x (time) array for the sweep"""
    abf = pyabf.ABF(item["fpath"])
    abf.setSweep(item["sweep"])
    time = abf.sweepX
    item["x"] = time
    return item

def serialize(item):
    out = []
    for spike_ind in item["peaks"]:
        spike = item["x"][spike_ind]
        out.append(
            {
                "fname": item["fname"],
                "fpath": item["fpath"],
                "mouse_id": item["mouse_id"],
                "cell_side": item["cell_side"],
                "cell_n": item["cell_n"],
                "membrane_potential": item["memb_potential"],
                "include": item["include"],
                "protocol": item["protocol"],
                "treatment": item["treatment"],
                "sweep": item["sweep"],
                "peak_time": float(spike),
            }
        )
    if not out:
        return [
            {
                "fname": item["fname"],
                "fpath": item["fpath"],
                "mouse_id": item["mouse_id"],
                "cell_side": item["cell_side"],
                "cell_n": item["cell_n"],
                "membrane_potential": item["memb_potential"],
                "include": item["include"],
                "protocol": item["protocol"],
                "treatment": item["treatment"],
                "sweep": item["sweep"],
                "peak_time": None,
            }
        ]
    return out

def add_current(item):
    try:
        item["current"] = SWEEP_TO_CURRENT_MAP[item["sweep"]]
    except TypeError:
        print(f"type sweep: {type(item['sweep'])}")
        print(f"val sweep: {item['sweep']}")
    return item

def to_db(dict_item, db, query):
    """add dict_item to database (db) using query"""
    try:
        con = sqlite3.connect(db)
        con.execute(query, dict_item)
        con.commit()
    except Exception as e:
        print(f"Exception adding dict to database. Exception is {e}\n.")
        print(f"Dict is {dict_item}")
        print(f"Query is {query}")
    finally:
        con.close()


import os
import json

from github import Github, UnknownObjectException

class ReleaseResolver:
    def __init__(self, client, repo_name, data):
        self.client = client
        self.repo_name = repo_name
        self.prefer_default_branch = data.get("prefer_default", False)
        self.sha = data["sha"]

    def fetch_latest_release(self, repo):
        latest_release = repo.get_latest_release()
        self.ref = latest_release.tag_name
        # NOTE: latest_commit_sha will differ from latest_release.target_committish, because
        # the release was created and then the tag was advanced if a HookSet was used
        self.latest_commit_sha = repo.get_commit(self.ref).sha
        self.tarball_url = latest_release.tarball_url

    def fetch_latest_commit(self, repo):
        default_branch = repo.get_branch(repo.default_branch)
        self.ref = default_branch.name
        self.latest_commit_sha = default_branch.commit.sha
        self.tarball_url = (
            f"https://api.github.com/repos/{self.repo_name}/tarball/{self.latest_commit_sha}"
        )

    def resolve_release(self):
        diff_url = ""
        self.repo = self.client.get_repo(self.repo_name)

        if self.prefer_default_branch:
            self.fetch_latest_commit(self.repo)
        else:
            try:
                self.fetch_latest_release(self.repo)
            except UnknownObjectException:
                print(
                    f'{self.repo_name} has no release data.  retreiving latest SHA from "{self.repo.default_branch}"'
                )
                self.fetch_latest_commit(self.repo)

        should_update = self.latest_commit_sha != self.sha
        if should_update:
            compared = self.repo.compare(self.sha, self.latest_commit_sha)
            diff_url = compared.html_url
        return should_update, diff_url

    def emit_status(self, should_update, diff_url):
        return [
            self.repo.full_name,
            should_update,
            self.sha,
            self.latest_commit_sha,
            diff_url,
        ]

    def update_corpus(self, corpus_dict):
        should_update, diff_url = self.resolve_release()

        corpus_dict[self.repo_name] = dict(
            ref=self.ref,
            sha=self.latest_commit_sha,
            tarball_url=self.tarball_url,
            prefer_default_branch=self.prefer_default_branch,
        )
        return self.emit_status(should_update, diff_url)

def main():
    """
    Small helper script used to update to latest releases
    of corpus repos.

    If releases are not found, defaults to the last commit
    on `master`.
    """
    ACCESS_TOKEN = os.environ.get("GITHUB_ACCESS_TOKEN", "")
    if ACCESS_TOKEN:
        client = Github(ACCESS_TOKEN)
    else:
        client = Github()

    statuses = []
    corpus = json.load(open("corpus.json"))
    new_corpus = dict()
    for repo_name, data in corpus.items():
        resolver = ReleaseResolver(client, repo_name, data)

        status_result = resolver.update_corpus(new_corpus)
        statuses.append(status_result)

    json.dump(new_corpus, open("corpus.json", "w"), indent=2)

if __name__ == "__main__":
    main()


import math
my_range= range(0,10*2)
for i in my_range:
    print (i/2)

#accumulator
sum = 0
for i in range(101):
    sum = sum + i
    print(sum)

#Factorial of an input
fact= eval(input("Enter value to factorial:"))
acc= 1
for i in range(fact,0,-1):
    acc = i * acc
print(acc)

list(range(6,0,-1))

# Quadratic, Find root
a,b,c = eval(input("Enter a, b, and c:"))
root_1 = (-b + math.sqrt(b*b-4 * a *c ))/ (2*a)
root_2 = (-b - math.sqrt(b*b-4 * a *c ))/ (2*a)
print("Root 1:", root_1, "Root 2:", root_2)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        '''
        Use 2 pointers, one goes first, another follows. The second pointer only starts after
        the first one has gone n steps. Note that second pointer needs to start from a dummy node
        in order to handle list with only one node, aka, when the result is None.
        '''
        first = head
        dummy = second = ListNode(0)
        dummy.next = head
        while first:
            first = first.next
            if n > 0:
                n -= 1
            else:
                second = second.next
        second.next = second.next.next
        return dummy.next

medium/19. Remove Nth Node From End of List.py


x = int(input("Enter 1st number: "))
y = int(input("Entrer 2nd number: "))

a = x
b = y

if(a*b == 0):
    pgcd = a+b

else:
    while(a != b):
        if(a>b):
            a = a-b
        elif (b>a):
            b = b-a
    pgcd = a

print("PGCD(",x,",",y,")= ", pgcd);


"""
This module aims to be the refactored code of old function "reviewapp/signals.py:check_trial_fields"

The method trial_validator.register() was created in replacement to STEP_FORM_MATRIX
"""

import re

from django.template.defaultfilters import slugify
from django.utils import simplejson

from reviewapp.consts import STEP_STATES, REMARK, MISSING, PARTIAL, COMPLETE, TRIAL_FORMS
from polyglot.multilingual_forms import BaseMultilingualWidget

FIELDS = {
    TRIAL_FORMS[0]: {
        'scientific_title': {'required': True, 'type': 'text', 'poly': True},
        'scientific_acronym': {'required': False, 'type': 'text', 'poly': True},
        'scientific_acronym_expansion': {'required': False, 'type': 'text', 'poly': True},
        'public_title': {'required': True, 'type': 'text', 'poly': True},
        'acronym': {'required': False, 'type': 'text', 'poly': True},
        'acronym_expansion': {'required': False, 'type': 'text', 'poly': True},
        'utrn_number': {'required': True, 'type': 'text', 'poly': False},
    },
    TRIAL_FORMS[1]: {
        'primary_sponsor': {'required': True, 'type': 'text', 'poly': False}
    },
    TRIAL_FORMS[2]: {
        'hc_freetext': {'required': True, 'type': 'text', 'poly': True}
    },
    TRIAL_FORMS[3]: {
        'i_freetext': {'required': True, 'type': 'text', 'poly': True},
        'i_code': {'required': True, 'type': 'mult', 'poly': False, 'queryset': None}
    },
    TRIAL_FORMS[4]: {
        'recruitment_status': {'required': True, 'type': 'text', 'poly': False},
        'recruitment_country': {'required': True, 'type': 'mult', 'poly': False, 'queryset': None},
        'enrollment_start_date': {'required': True, 'type': 'datetime', 'poly': False},
        #'enrollment_end_planned': {'required': True, 'type': 'text', 'poly': False},
        'target_sample_size': {'required': True, 'type': 'text', 'poly': False},
        'inclusion_criteria': {'required': True, 'type': 'text', 'poly': True},
        'exclusion_criteria': {'required': True, 'type': 'text', 'poly': True},
        'gender': {'required': True, 'type': 'text', 'poly': False},
        'agemin_value': {'required': True, 'type': 'text', 'poly': False},
        'agemin_unit': {'required': True, 'type': 'text', 'poly': False},
        'agemax_value': {'required': True, 'type': 'text', 'poly': False},
        'agemax_unit': {'required': True, 'type': 'text', 'poly': False},
    },
    TRIAL_FORMS[5]: {
        #'study_type': {'required': True, 'type': 'text', 'poly': False},
        'study_design': {'required': True, 'type': 'text', 'poly': True},
        'phase': {'required': True, 'type': 'text', 'poly': False},
        #'expanded_access_program': {'required': True, 'type': 'text', 'poly': False},
        'intervention_assignment': {'required': True, 'type': 'text', 'poly': False},
        'number_of_arms': {'required': True, 'type': 'text', 'poly': False},
        'masking': {'required': True, 'type': 'text', 'poly': False},
        'allocation': {'required': True, 'type': 'text', 'poly': False},
        'purpose': {'required': True, 'type': 'text', 'poly': False}
    },
    TRIAL_FORMS[6]: {},
    TRIAL_FORMS[7]: {}
}

class TrialValidator(object):
    steps_forms = None # Replaced STEP_FORM_MATRIX
    model = None

    def __init__(self):
        self.steps_forms = {}

    def register(self, step, forms):
        self.steps_forms[step] = forms

    def validate(self, instance):
        """
        FIXME: Some thoughts about this function:

        - Should have comments/docs/tests
        - Should be refactored (in progress)
        - Seems to be too complex for what it does
        """

        if not hasattr(instance, 'submission'):
            return

        fields_status = {}
        mandatory_languages = [lang.lower() for lang in instance.submission.get_mandatory_languages()]

        # Setting instance queryset on validation fields
        FIELDS[TRIAL_FORMS[3]]['i_code']['queryset'] = instance.intervention_code()
        FIELDS[TRIAL_FORMS[4]]['recruitment_country']['queryset'] = instance.recruitment_country.all()

        # attachment
        remarks = instance.submission.remark_set.filter(status='open').filter(context=slugify(TRIAL_FORMS[8])).count()
        count = instance.submission.attachment_set.all().count()
        for lang in mandatory_languages:
            if remarks > 0:
                fields_status.update({lang: {TRIAL_FORMS[8]: REMARK}})
            elif count == 0:
                fields_status.update({lang: {TRIAL_FORMS[8]: PARTIAL}})
            else:
                fields_status.update({lang: {TRIAL_FORMS[8]: COMPLETE}})

        for step, forms in self.steps_forms.items():

            step_status = {}

            remarks = instance.submission.remark_set.filter(status='open').filter(context=slugify(step)).count()
            if remarks > 0:
                for lang in mandatory_languages:
                    step_status.update({lang: REMARK})
            else:
                for form in forms:
                    if hasattr(form.Meta,'queryset'):
                        count = form.Meta.queryset.filter(trial=instance).count()

                        for lang in mandatory_languages:
                            if count < form.Meta.min_required:
                                step_status.update({lang: MISSING})
                            elif count == 0:
                                if step_status.get(lang, '') != MISSING:
                                    step_status.update({lang: PARTIAL})
                            else:
                                if not form.Meta.polyglot:
                                    if step_status.get(lang, '') == '':
                                        step_status.update({lang: COMPLETE})
                                else:
                                    for obj in form.Meta.queryset.filter(trial=instance):
                                        for field in form.Meta.polyglot_fields:
                                            if lang == 'en':
                                                value = [getattr(obj, field)]
                                            else:
                                                list_value = obj.translations.filter(language=lang)
                                                if len(list_value) > 0:
                                                    value = getattr(list_value[0], field)
                                                else:
                                                    value = None

                                            if value is None:
                                                if form.Meta.min_required != 0:
                                                    step_status.update({lang: MISSING})
                                                else:
                                                    if step_status.get(lang, '') != MISSING:
                                                        step_status.update({lang: PARTIAL})
                                            elif type(value) is str or type(value) is unicode:
                                                if re.match('^\s*$', value):
                                                    if step_status.get(lang, '') != MISSING:
                                                        step_status.update({lang: MISSING})
                                                    else:
                                                        if step_status.get(lang, '') != MISSING:
                                                            step_status.update({lang: PARTIAL})
                                                else:
                                                    if step_status.get(lang, '') == '':
                                                        step_status.update({lang: COMPLETE})
                                            else:
                                                if len(str(value)) == 0:
                                                    if form.Meta.min_required != 0:
                                                        step_status.update({lang: MISSING})
                                                    else:
                                                        if step_status.get(lang, '') != MISSING:
                                                            step_status.update({lang: PARTIAL})
                                                else:
                                                    if step_status.get(lang, '') == '':
                                                        step_status.update({lang: COMPLETE})

                    else:
                        if hasattr(form.Meta,'model'):
                            if form.Meta.model == self.model:
                                check_fields = FIELDS[step]
                                for field in form.base_fields.keys():
                                    values = {}
                                    if field in check_fields.keys():
                                        if check_fields[field]['type'] == 'text':
                                            values.update({'en': getattr(instance, field)})

                                            for trans in instance.translations.all():
                                                if trans.language.lower() in mandatory_languages:
                                                    if check_fields[field]['poly']:
                                                        values.update({trans.language.lower(): getattr(trans, field)})
                                                    else:
                                                        values.update({trans.language.lower(): values['en']})

                                            for lang, value in values.items():
                                                if value is None:
                                                    if check_fields[field]['required']:
                                                        step_status.update({lang: MISSING})
                                                    else:
                                                        if step_status.get(lang, '') != MISSING:
                                                            step_status.update({lang: PARTIAL})
                                                elif type(value) is str or type(value) is unicode:
                                                    if re.match('^\s*$', value):
                                                        if check_fields[field]['required']:
                                                            step_status.update({lang: MISSING})
                                                        else:
                                                            if step_status.get(lang, '') != MISSING:
                                                                step_status.update({lang: PARTIAL})
                                                    else:
                                                        if step_status.get(lang, '') == '':
                                                            step_status.update({lang: COMPLETE})
                                                else:
                                                    if len(str(value)) == 0:
                                                        if check_fields[field]['required']:
                                                            step_status.update({lang: MISSING})
                                                        else:
                                                            if step_status.get(lang, '') != MISSING:
                                                                step_status.update({lang: PARTIAL})
                                                    else:
                                                        if step_status.get(lang, '') == '':
                                                            step_status.update({lang: COMPLETE})

                                        else:
                                            if check_fields[field]['type'] == 'mult':
                                                count = check_fields[field]['queryset'].count()

                                                for lang in mandatory_languages:
                                                    if count < 1:
                                                        step_status.update({lang: MISSING})
                                                    else:
                                                        if step_status.get(lang, '') == '':
                                                            step_status.update({lang: COMPLETE})

            for language, status in step_status.items():
                if fields_status.get(language) is None:
                    fields_status[language] = {step: status}
                else:
                    fields_status[language].update({step: status})

        # Stores the serialized dictionary with fields statuses in the submission, to be
        # used in further later, when showing the form (also to block before the trial go
        # to the next state)
        instance.submission.fields_status = simplejson.dumps(fields_status)
        instance.submission.save()

    def field_is_required(self, form, field_name):
        """Returns a boolean value about this is a required field according to validation rules"""

        # Returns this is or is not a required field
        try:
            rules = self.get_rules_by_field(form, field_name)

            if rules:
                return rules['required']
        except KeyError:
            pass

    def get_rules_by_field(self, form, field_name):
        """Returns a dictionary with rules from constant FIELDS for a given field."""

        # Gets step where the given form is
        step_in = None
        for step, forms in self.steps_forms.items():
            if form.__class__ in forms:
                step_in = step
                break

        # Returns the rules from a dictionary
        try:
            return FIELDS[step_in][field_name]
        except KeyError:
            pass

    def get_field_status(self, form, field_name, instance):
        """Returns the status of a given field, for a given instance.submission"""

        rules = self.get_rules_by_field(form, field_name)

        if rules and rules['required']:
            if field_name in form.fields:
                field = form.fields[field_name]
                field_value = form.initial.get(field_name, None)

                # For multilingual fields
                if isinstance(field.widget, BaseMultilingualWidget):
                    # Default language value (english)
                    values = [field_value]

                    # Values for available languages (except to english)
                    for lang in field.available_languages:
                        if lang != 'en':
                            values.append(field.widget.get_value_by_language(field_name, field_value, lang))

                    # Returns "required" if any of translation values is not valid
                    if not all(values):
                        return 'required'

                # For common fields
                elif not field_value:
                    return 'required'

        return ''

trial_validator = TrialValidator()


# -*- coding: utf-8 -*-
#! /usr/bin/env python
"""
Created on  05 10 10:39:56  2023
@Author : Lulu Shi
@Mails : crazzy_rabbit@163.com
"""
import click
import pandas as pd

def load_pop(list):
    pop = pd.read_csv(list, sep='\t', header=None)
    return pop

def load_data(infile):
    df = pd.read_csv(infile, sep='\t')
    return df

def target(poplist, infile):
    target_df = infile.filter(items=['chr', 'start', 'end'] + poplist[0].tolist())
    return target_df

def cal_sd2(pop):
    sd2 = pop.iloc[:, 3:].var(numeric_only=True, axis=1)
    return sd2

@click.command()
@click.option('--file', help='GetCleanCNV.py的结果文件')
@click.option('--p1', help='属于群体1的个体ID，一行一个ID')
@click.option('--p2', help='属于群体2的个体ID，一行一个ID')
@click.option('--out', help='输出文件前缀')
def main(file, p1, p2, out):
    data = load_data(file)

    # 计算1群体方差
    pop1list = load_pop(p1)
    pop1 = target(pop1list, data)
    pop1_sd_weight = cal_sd2(pop1) * len(pop1)

    # 计算2群体方差
    pop2list = load_pop(p2)
    pop2 = target(pop2list, data)
    pop2_sd_weight = cal_sd2(pop2) * len(pop2)

    # 两群体方差加权群体大小
    Nt = len(pop1) + len(pop2)
    Vs = (pop1_sd_weight + pop2_sd_weight) / Nt

    # 两比较群体样本量之和的方差
    total = pd.concat([pop1.iloc[:, 3:], pop2.iloc[:, 3:]], axis=1)
    Vt = total.var(numeric_only=True, axis=1)

    # Vst
    Vst = (Vt - Vs) / Vt

    chat = pop1.iloc[:, :3]
    chat['Vst'] = Vst
    chat.to_csv(f'{out}.Vst.txt', sep='\t', index=False)

if __name__ == '__main__':
    main()


import wx

from .defaults import DEFAULT_COLUMN_WIDTH
from .worker import WorkerThread
from ..wallet import Wallet

CONFIRMED = 'Confirmed'
PENDING = 'Pending'
RETRYING = 'Retrying'

class HistoryTab(wx.Panel):  # type: ignore
    def __init__(self,
                 parent: wx.Window,
                 wallet: Wallet) -> None:
        super().__init__(parent)
        self.wallet = wallet
        self.worker = WorkerThread()
        self.init_popup_menu()
        self.create_history_tab()

    def create_history_tab(self) -> None:
        self.transactions = wx.ListCtrl(self, style=wx.LC_REPORT)
        self.transactions.InsertColumn(0, 'Transactions')
        self.transactions.InsertColumn(1, 'Status')

        for bundle in self.wallet.bundles['unconfirmed']:
            self.transactions.Append([str(bundle.hash), PENDING])
        for bundle in self.wallet.bundles['confirmed']:
            self.transactions.Append([str(bundle.hash), CONFIRMED])

        if self.transactions.GetItemCount() > 0:
            column_width = wx.LIST_AUTOSIZE
        else:
            column_width = DEFAULT_COLUMN_WIDTH

        self.transactions.SetColumnWidth(0, column_width)
        self.transactions.SetColumnWidth(1, column_width)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.transactions, proportion=1, flag=wx.EXPAND)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(vbox, proportion=1, flag=(wx.EXPAND | wx.ALL), border=10)

        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.transactions_right_click, self.transactions)
        self.Bind(wx.EVT_MENU, self.popup_menu_clicked)

        self.SetSizer(hbox)

    def init_popup_menu(self) -> None:
        self.popup_menu = wx.Menu()
        self.popup_menu.Append(0, 'Retry Bundle')

    def popup_menu_clicked(self,
                           event: wx.ContextMenuEvent) -> None:
        index = event.GetId()
        if index == 0:
            # Retry Bundle
            bundle_hashes = []
            list_index = self.transactions.GetFirstSelected()
            while list_index != -1:
                if self.transactions.GetItemText(list_index, col=1) == PENDING:
                    bundle_hashes.append(self.transactions.GetItemText(list_index))
                    list_index = self.transactions.GetNextSelected(list_index)

            print(bundle_hashes)
            bundles = []
            for bundle in self.wallet.bundles['unconfirmed']:
                if bundle.hash in bundle_hashes:
                    bundles.append(bundle)

            self.worker.send(self.wallet.retry_unconfirmed_bundles, args=tuple(bundles))

    def transactions_right_click(self,
                                 event: wx.ListEvent) -> None:
        if event.GetText():
            self.PopupMenu(self.popup_menu)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Python Programming Exercises 1 - 10

https://github.com/rednikon/Python-programming-exercises

@author: veemac
"""

# COMPLETE 1 - 5 BY FRIDAY JULY 12

# Question 1: Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included).
# The numbers obtained should be printed in a comma-separated sequence on a single line.
# My answer: use the range function to find the applicable numbers, and present them as a list
l = []
for i in range(2000, 3201):
    if (i%7==0) and (i%5!=0):
        l.append(str(i))
print(',',str(l))

# Question 2:
# Write a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line.
# My answer: create a recursive function to find the factorial.
def factorial(x):
    """A recursive function to find the factorial of a user-generated integer"""
    if x == 0:
        return 1
    return x * factorial(x - 1)

x = int(input("Enter a number for the factorial equation: "))
print(factorial(x))

# Question 3: With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number
# between 1 and n (both included) and then the program should print the dictionary.
# My answer: create a dictionary using the dict() function, and fill it based on user input
squares = dict()
base = int(input("Enter a number to create a dictionary of squares: "))

for i in range(1, base +1):
    squares[i] = i*i

print(squares)

# Question 4: Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
# My answer: accept comma-separated numbers from the user, convert the string to integers, and build a list and tuple out of it

q5_numbers = input("Enter a comma-separated list of numbers: ")
q5_list = q5_numbers.split(',')
q5_tuple = tuple(q5_list)
print(q5_list)
print(q5_tuple)

# Question 5:


import requests, zipfile, StringIO
import numpy as np
import pandas as pd

# -------------------------------------------------------------------------- ##
# load and clean geographic distance data, scale between 0 and 1, invert

dist = pd.read_csv('http://privatewww.essex.ac.uk/~ksg/data/capdist.csv')
dist = dist[['numa', 'numb', 'kmdist']]
dist['kmdist_norm'] = dist['kmdist'] / dist['kmdist'].max()
dist['weight'] = dist['kmdist_norm'] * -1 + np.max(dist['kmdist_norm'])
dist.drop(['kmdist', 'kmdist_norm'], axis=1, inplace=True)
dist.rename(columns = {'numa' : 'cowcode1', 'numb' : 'cowcode2'}, inplace=True)
dist.to_csv('clean_data/geographic_distance.txt', sep=',', index=False)

# -------------------------------------------------------------------------- ##
# load and clean trade distance data, scale between 0 and 1

trade = pd.read_table('clean_data/dyadic_trade_imputed.txt', sep=',')
trade['trade'] = trade['flow1'] + trade['flow2']
trade['trade'] = np.log(trade['trade'] + 1)
trade['weight'] = trade['trade'] / trade['trade'].max()
trade.rename(columns={'ccode1' : 'cowcode1', 'ccode2' : 'cowcode2'}, inplace=True)
trade.dropna(inplace=True)
trade['cowcode1'] = trade['cowcode1'].astype(int)
trade['cowcode2'] = trade['cowcode2'].astype(int)
trade[['cowcode1','cowcode2','weight','year']].to_csv('clean_data/trade_distance.txt', sep=',', index=False)

# -------------------------------------------------------------------------- ##
# load and clean linguistic distance data, fix weight column name

ling = pd.read_table('clean_data/linguistic_distance.txt', sep=',')
if 'connection' in ling.columns.tolist():
	ling.rename(columns={'connection' : 'weight'}, inplace=True)
ling.to_csv('clean_data/linguistic_distance.txt', sep=',', index=False)

# -------------------------------------------------------------------------- ##
# gwf data

# access the data
gwf_data_url = 'http://sites.psu.edu/dictators/wp-content/uploads/sites/12570/2015/04/GWF-Autocratic-Regimes-1.2.zip'
r = requests.get(gwf_data_url)
z = zipfile.ZipFile(StringIO.StringIO(r.content))
z.extract('GWF Autocratic Regimes 1.2/GWFtscs.txt');
gwf = pd.read_table('GWF Autocratic Regimes 1.2/GWFtscs.txt')

# subset to relevant columns
gwf = gwf[['cowcode', 'year', 'gwf_fail', 'gwf_enddate', 'gwf_fail_subsregime', 'gwf_fail_type',
           'gwf_party', 'gwf_personal', 'gwf_military', 'gwf_monarch', 'gwf_duration']]

# apply specific date to observations
gwf['end_date'] = pd.to_datetime(gwf['gwf_enddate'], format='%d%b%Y')
gwf['end_year'] = pd.DatetimeIndex(gwf['end_date']).year
gwf['date_year'] = gwf['end_year'] != gwf['year']
gwf['date'] = np.where(gwf['date_year'] == True, pd.to_datetime('12/31/' + gwf['year'].astype(str)), gwf['end_date'])
gwf['dateInt'] = gwf['date'].apply(lambda x: x.to_datetime().date().toordinal())

# create coercion, democratic transition, and autocratic transition indicators
gwf['coerce'] = (gwf['gwf_fail_type'].isin([4, 5, 6])).astype(int)
gwf['demtrans'] = (gwf['gwf_fail_subsregime'] == 1).astype(int)
gwf['auttrans'] = (gwf['gwf_fail_subsregime'] == 2).astype(int)

# clean up
gwf.rename(columns = {'gwf_fail' : 'failure',
                      'gwf_party' : 'party',
                      'gwf_personal' : 'personal',
                      'gwf_monarch' : 'monarch',
                      'gwf_military' : 'military',
                      'gwf_duration' : 'duration'}, inplace=True)
gwf = gwf[['cowcode', 'year', 'date', 'dateInt', 'failure', 'coerce', 'demtrans', 'auttrans',
           'party', 'personal', 'military', 'monarch', 'duration']]
gwf.to_csv('clean_data/gwf.txt', sep=',', index=False)

# -------------------------------------------------------------------------- ##
# powell & thyne coup data

def to_date(x):
    try:
        return datetime.strptime(x, '%d%b%Y')
    except:
        return pd.NaT

coups = pd.read_table('http://www.uky.edu/~clthyn2/coup_data/powell_thyne_ccode_year.txt', header=0, sep='\t')
coups.rename(columns={'ccode' : 'cowcode'}, inplace=True)
coups.drop('version', axis=1, inplace=True)

c = coups[['cowcode', 'year'] + [col for col in coups.columns if 'coup' in col]]
c = pd.melt(c, id_vars=['cowcode', 'year']).set_index(['cowcode', 'year'])
c.columns = ['cvar', 'coup']
d = coups[['cowcode', 'year'] + [col for col in coups.columns if 'date' in col]]
d = pd.melt(d, id_vars=['cowcode', 'year']).set_index(['cowcode', 'year'])
d.columns = ['dvar', 'date']

coups = pd.concat([c, d], axis=1)
coups['date_coup'] = coups['date'].apply(lambda x: to_date(x))
coups['success'] = (coups['coup'] > 1).astype(int)
coups['attempt'] = (coups['coup'] > 0).astype(int)
coups.drop(['coup', 'cvar', 'dvar', 'date'], axis=1, inplace=True)
coups.reset_index(inplace=True)

coups = coups.groupby(['cowcode', 'year']).first().reset_index()
coups['date_coup'] = np.where(pd.isnull(coups['date_coup']), pd.to_datetime('12/31/' + coups['year'].astype(str)), coups['date_coup'])
coups['dateInt_coup'] = coups['date_coup'].apply(lambda x: x.to_datetime().date().toordinal())
coups.to_csv('clean_data/coups.txt', sep=',', index=False)


import csv
import copy
import json
import datetime
import logging

from RequestForQuotes import *
from ExtMaterials import ExtMaterials

from pymongo import MongoClient

class RFQCreator:

    def __init__(self):
        """clase que crea RFQs en el formator necesario para guardar en la DB"""
        self.rfq = RequestForQuotes()
        self.client = MongoClient('localhost', 27017)
        self.db = self.client.conpancol
        self.collection = self.db.mnmaterials
        self.rfqcollection = self.db.rfquotes
        logging.basicConfig(filename='logs/rfqcreator.log', level=logging.DEBUG)

    def lookForMaterial(self, itemCode):

        if self.collection.find({'itemcode': itemCode}).count() > 0 :
            material = self.collection.find_one({'itemcode': itemCode})
            return material
        else:
            raise AttributeError('Material not found')

    def alreadyExists(self, newID):
        if self.rfqcollection.find({'internalCode': newID}).count() > 0:
            logging.info('RFQ already exists  \t' + str(newID))
            return True
        else:
            logging.info('RFQ does not exists  \t' + str(newID))
            return False

    def setRFQInformation(self, internalCode, externalCode, sender, company, receivedDate):
        self.rfq.setIntenalCode(internalCode)
        self.rfq.setExternalCode(externalCode)
        self.rfq.setSender(sender)
        self.rfq.setCompany(company)
        self.rfq.setReceivedDate(receivedDate)
        dt = datetime.datetime.now()
        self.rfq.setProcessedDate(dt.strftime('%d/%m/%Y'))

    def addRFQNote(self, note):
        self.rfq.setNote(note)

    def createRFQfromCSV(self, csvfile):
        try:
            with open(csvfile, 'r', encoding='utf-8') as f:
                reader = csv.reader(f, dialect="excel-tab")
                extmaterials = []
                for row in reader:
                    try:
                        orderNum = row[0]
                        itemCode = row[1]
                        quantity = float(row[2])
                        unit = row[3]

                        material = self.lookForMaterial(itemCode)
                        extendedMaterial = ExtMaterials(material)
                        extendedMaterial.setOrderNumber(orderNum)
                        extendedMaterial.setUnit(unit)
                        extendedMaterial.setQuantity(quantity)
                        extmaterials.append(extendedMaterial)
                        logging.info('Material found in DB \t' + itemCode + '\t' + "OK")

                    except AttributeError as ex:
                        logging.info('Material code not found in DB \t' + itemCode + '\t' + "NA")
                        continue

                    except ValueError as ex:
                        logging.info('There is a wrong data format entry. Please check')
                        continue

                self.rfq.setMaterialList(extmaterials)

            rfq_json = self.rfq.__dict__
            rfq_json['materialList'] = self.rfq.to_json()
            output = copy.deepcopy(rfq_json)
            print(json.dumps(output))
            totalMaterials = len(rfq_json['materialList'])
            logging.info('End of RFQ creation  \t' + str(totalMaterials))
            self.rfqcollection.insert_one(output)

        except IOError:
            logging.info('Error opening input file')

    def exportRFQtoCSV(self, code, csvfile):

        labels = []
        labels.append('Id')
        labels.append('OrderId')
        labels.append('ItemId')
        labels.append('Description')
        labels.append('Type')
        labels.append('Quantity')
        labels.append('Unit')

        try:
            path = "./data/export/" + csvfile
            with open(path, 'w', encoding='utf-8', newline='') as f:
                writer = csv.writer(f, dialect="excel", delimiter=';')

                if self.alreadyExists(code):

                    cursor = self.rfqcollection.find({'internalCode': code})

                    rfq = cursor.next()

                    header = 'RFQ internal code: ' + str(rfq["internalCode"])
                    writer.writerow([header])
                    info = 'Originator: ' + str(rfq["sender"]) + ' ' + str(rfq["company"])
                    writer.writerow([info])
                    details = 'Received date: ' + str(rfq["receivedDate"])
                    writer.writerow([details])
                    note = 'Note: ' + str(rfq["note"])

                    writer.writerow([note])
                    writer.writerow([''])
                    labels_rwo = '\t'.join(labels)
                    print(labels_rwo)
                    writer.writerow(labels)

                    items = rfq["materialList"]

                    id = 1

                    for item in items:
                        rwo = []
                        rwo.append(str(id))
                        rwo.append(item["orderNumber"])
                        rwo.append(item["itemcode"])
                        description = item["description"]
                        rwo.append(description)
                        rwo.append(item["type"])
                        rwo.append(str(item["quantity"]))
                        rwo.append(str(item["unit"]))

                        if item["category"] == "PLATE":
                            nplates = self.getNumberPlates(item["dimensions"], item["quantity"])
                            rwo.append(str(nplates))
                            rwo.append('PC')

                        writer.writerow(rwo)
                        id += 1

                    for i in range(1,3):
                        writer.writerow([' '])
                    bottom_txt_file = open('./data/Bottom_conditions_EN_FOB.txt', 'r')
                    bottom_txt_rows = bottom_txt_file.readlines()
                    print(bottom_txt_rows)
                    for line in bottom_txt_rows:
                        row = line.replace('\n', ' ')
                        writer.writerow([row])
                    bottom_txt_file.close()

                else:
                    logging.info('RFQ does not exists in DB')

            f.close()

        except IOError:
            logging.info('Problem with file creation')

    def getNumberPlates(self, dimensions, total_area):

        if dimensions.find("MM"):
            dim_values = []
            dims = dimensions.split(',')[1].split('X')
            for dd in dims:

                try:
                    dim_values.append(float( dd.replace(' ','').replace('MM','')))
                except ValueError:
                    dim_values.append(0.0)

            area = dim_values[0] * dim_values[1] * 0.000001
            nplates = format( total_area / (area), '.2f')
            print(dim_values, total_area, nplates)

        return nplates


import numpy as np
import math

from .graph import Graph

class GraphGenerator:

    # Graph generation is per frame so take a bbstore which is one frame
    def __init__(self, bbstore):
        self.generate_graph(bbstore)

    def generate_graph(self, bbstore):
        """
        Generates a graph of the relevant bounding boxes in the frame
        Args:
            bbstore - An object of the BoundingBoxStore class
        Returns:
            An object of the graph class based on the bounding boxes provided
        """
        bbstore = bbstore.get_store()   # We don't require the extra functionality of the class from here

        # Get the bounding boxes related to people, filter out others
        filtered_bbs = []
        has_ball = False
        for bb in bbstore:
            if bb.get_class_and_score()[0] == "person":
                filtered_bbs.append(bb)
            elif bb.get_class_and_score()[0] == "ball":
                has_ball = True
                ball = bb
        if has_ball:
            filtered_bbs.append(ball)

        # The upper bound for the number of nodes in the graph
        # The neural network takes a fixed size so our graph must be standardised in size
        max_bbs = 50

        nodes = np.empty(max_bbs, dtype=object)
        edges = np.zeros((max_bbs, max_bbs))    # The graph is fully connected
        for i in range(0, max_bbs):
            for j in range(0, max_bbs):
                edges[i][j] = -1

        for i in range(0, min(max_bbs, len(filtered_bbs))):
            bb1 = filtered_bbs[i]
            midpoints = bb1.get_mid_point()
            node_name = str(midpoints[0]) + "," + str(midpoints[1])
            i_val = i
            if i == len(filtered_bbs) - 1 and has_ball:
                nodes[max_bbs - 1] = node_name
                i_val = max_bbs - 1
            else:
                nodes[i] = node_name

            for j in range(0, min(max_bbs, len(filtered_bbs))):
                bb2 = filtered_bbs[j]
                dist = self.calculate_distance(bb1, bb2)
                j_val = j
                if j == len(filtered_bbs) - 1 and has_ball:
                    j_val = max_bbs - 1
                edges[i_val][j_val] = dist

        # Need to provide node names for any nodes not reached in the loop
        start = len(filtered_bbs) - 1 if has_ball else len(filtered_bbs)
        stop = max_bbs - 1 if has_ball else max_bbs
        for i in range(start, stop):
            nodes[i] = str(i * -1)   # No other nodes will be prefixed by a '-' so there are no conflicts

        self.graph = Graph(nodes, edges, True)
        return self.graph

    def calculate_distance(self, bb1, bb2):
        """
        Calcualtes the distance of the two bounding boxes for the edge weight in the graph
        Args:
            bb1 - The first bounding box
            bb2 - The second bounding box
        Returns:
            A float representing the physical distance between bb1 and bb2
        """
        w1, h1 = bb1.get_width_height()
        _, h2 = bb2.get_width_height()

        # Background people will appear smaller than foreground people through parralax
        # Therefore their distance must be greater
        # This means that a form of 3D data must be extracted from the 2D image
        # This 'z-distance' will be calculated using the relative heights of the players. This is less likely to be obscured than their width

        ratio = h1 / h2 if h1 > h2 else h2 / h1 # Ratio is always >= 1 to be multiplicative of the distance. Smaller distance is closer

        mp1 = bb1.get_mid_point()
        mp2 = bb2.get_mid_point()

        # Only in the case where the boxes are the same will the second ratio be returned
        # This allows us to take account of how horizontal a player is
        val = math.sqrt((mp1[0] - mp2[0]) ** 2.0 + (mp1[1] - mp2[1]) ** 2.0)
        dist = max(val * ratio, w1 / h1)
        return dist

    def get_graph(self):
        """
        Getter method for the graph generator
        Returns:
            The graph object stored in this generator
        """
        return self.graph


#encoding=utf-8

from esAPI.ESsetting import *
import requests
import json

LOCALURL=LOCALHOST

header={'Content-Type':'application/json'}

def createDOC(index_type,index_name,data_json,unique_id="-1"):
    tmp_url=LOCALURL+"/"+index_type+"/"+index_name
    if(unique_id!="-1"):
        tmp_url=tmp_url+"/"+str(unique_id)
    response=requests.post(tmp_url,data=json.dumps(data_json),headers=header)
    return response.status_code

def createIndex(index_type,data_json,number_of_shards=3,number_of_replicas=1):
    tmp_url=LOCALURL+"/"+index_type+"/"
    data={
        "settings": {
            "index": {
                "number_of_shards": number_of_shards,
                "number_of_replicas": number_of_replicas
            }
        },
        "mappings": {
            "properties":data_json
        }
    }
    response=requests.put(tmp_url,data=json.dumps(data),headers=header)
    return response.status_code

def insertDoc(index_type,index_name,data_json):
    tmp_url = LOCALURL + "/" + index_type + "/"+ index_name
    response = requests.post(tmp_url,data=json.dumps(data_json),headers=header)
    return response


import io
import picamera
import time

my_file = open('/tmp/image.jpg', 'wb')

with picamera.PiCamera() as camera:
	camera.sharpness = 0
	camera.contrast = 0
	camera.brightness = 50
	camera.saturation = 0
	camera.ISO = 0
	camera.video_stabilization = False
	camera.exposure_mode = 'auto'
	camera.meter_mode = 'average'
	camera.awb_mode = 'auto'
	camera.image_effect = 'none'
	camera.color_effects = None
	camera.rotation = 0
	camera.hflip = False
	camera.vflip = False
	camera.start_preview()
	time.sleep(5)
	camera.capture(my_file)

my_file.close()


from PIL import Image, ImageDraw
import numpy
import random
import matplotlib.pyplot as plt
import string

import numpy as np

from skimage.transform import resize
from skimage.morphology import label
from sklearn.model_selection import train_test_split

import tensorflow as tf

from keras.models import Model, load_model
from keras.layers import Input, BatchNormalization, Activation, Dense, Dropout
from keras.layers.core import Lambda, RepeatVector, Reshape
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import MaxPooling2D, GlobalMaxPool2D
from keras.layers.merge import concatenate, add
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

def generate_images(amount_of_pictures = 1000, image_shape = (512, 512), rect_bottom = 450, betwin = 20, x_first = 40):

    number_weigt_dict = {7: 42, 6: 53, 5:70, 4:90, 8:35}

    for i in range(amount_of_pictures):
        img = Image.new("L", image_shape, color="white")
        img_label = Image.new("L", image_shape, color="black")
        columns_amount = random.randint(4, 8)
        rect_up = random.randint(40, 400)

        img1 = ImageDraw.Draw(img)
        img1_label = ImageDraw.Draw(img_label)

        img1.rectangle([(x_first, rect_up), (x_first+number_weigt_dict[columns_amount], rect_bottom)], fill ="gray")
        img1_label.rectangle([(x_first, rect_up), (x_first+number_weigt_dict[columns_amount], rect_bottom)], fill ="white")

        cur_x = x_first+number_weigt_dict[columns_amount]+betwin

        for j in range(1, columns_amount):
            rect_up = random.randint(40, 400)

            shape = [(cur_x, rect_up), (cur_x+number_weigt_dict[columns_amount], rect_bottom)]

            img1.rectangle(shape, fill ="gray")
            img1_label.rectangle(shape, fill ="white")

            cur_x = cur_x+number_weigt_dict[columns_amount]+betwin

	#adding random words from letters and numbers for better diagram imitation. It increase quality

        img1.text((470, 100), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
        img1.text((470, 150), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
        img1.text((470, 200), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
        img1.text((470, 250), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
        img1.text((470, 300), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
        img1.text((470, 350), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))

        img1.text((50, 470), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)))
        img1.text((150, 470), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)))
        img1.text((250, 470), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)))
        img1.text((350, 470), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)))
        img1.text((450, 470), ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3)))

        img.save("images/{}.png".format(i))
        img_label.save("labels/{}.png".format(i))

        img.close()
        img_label.close()

def load_model(model_name='model_exp.h5'):
    def conv2d_block(input_tensor, n_filters, kernel_size = 3, batchnorm = True):
        # first layer
        x = Conv2D(filters = n_filters, kernel_size = (kernel_size, kernel_size),\
              kernel_initializer = 'he_normal', padding = 'same')(input_tensor)
        if batchnorm:
            x = BatchNormalization()(x)
        x = Activation('relu')(x)

        # second layer
        x = Conv2D(filters = n_filters, kernel_size = (kernel_size, kernel_size),\
                  kernel_initializer = 'he_normal', padding = 'same')(input_tensor)
        if batchnorm:
            x = BatchNormalization()(x)
        x = Activation('relu')(x)

        return x

    def get_unet(input_img, n_filters = 16, dropout = 0.1, batchnorm = True):
        # Contracting Path
        c1 = conv2d_block(input_img, n_filters * 1, kernel_size = 3, batchnorm = batchnorm)
        p1 = MaxPooling2D((2, 2))(c1)
        p1 = Dropout(dropout)(p1)

        c2 = conv2d_block(p1, n_filters * 2, kernel_size = 3, batchnorm = batchnorm)
        p2 = MaxPooling2D((2, 2))(c2)
        p2 = Dropout(dropout)(p2)

        c3 = conv2d_block(p2, n_filters * 4, kernel_size = 3, batchnorm = batchnorm)
        p3 = MaxPooling2D((2, 2))(c3)
        p3 = Dropout(dropout)(p3)

        c5 = conv2d_block(p3, n_filters = n_filters * 8, kernel_size = 3, batchnorm = batchnorm)

        # Expansive Path

        u7 = Conv2DTranspose(n_filters * 4, (3, 3), strides = (2, 2), padding = 'same')(c5)
        u7 = concatenate([u7, c3])
        u7 = Dropout(dropout)(u7)
        c7 = conv2d_block(u7, n_filters * 4, kernel_size = 3, batchnorm = batchnorm)

        u8 = Conv2DTranspose(n_filters * 2, (3, 3), strides = (2, 2), padding = 'same')(c7)
        u8 = concatenate([u8, c2])
        u8 = Dropout(dropout)(u8)
        c8 = conv2d_block(u8, n_filters * 2, kernel_size = 3, batchnorm = batchnorm)

        u9 = Conv2DTranspose(n_filters * 1, (3, 3), strides = (2, 2), padding = 'same')(c8)
        u9 = concatenate([u9, c1])
        u9 = Dropout(dropout)(u9)
        c9 = conv2d_block(u9, n_filters * 1, kernel_size = 3, batchnorm = batchnorm)

        outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)
        model = Model(inputs=[input_img], outputs=[outputs])
        return model

    input_img = Input((512, 512, 1), name='img')
    model = get_unet(input_img, n_filters=16, dropout=0.05, batchnorm=True)
    model.compile(optimizer=Adam(), loss="binary_crossentropy", metrics=["accuracy"])

    model.load_weights(model_name)
    return model

def get_mask(model, image_name, image_size=(512, 512, 1)):
    X_graph = []
    img_graph = img_to_array(load_img(image_name, grayscale=True))
    img_graph = resize(img_graph, image_size, mode = 'constant', preserve_range= True)
    img_graph = img_graph/255.0
    X_graph.append(img_graph)
    X_graph = np.array(X_graph)

    preds_graph = model.predict(X_graph, verbose=1)
    preds_graph_t = (preds_graph > 0.5).astype(np.uint8)

    return preds_graph_t

def plot_result(X, binary_preds, ix=0):
    fig, ax = plt.subplots(1, 2, figsize=(20, 10))
    ax[0].imshow(X[ix, ..., 0], cmap='seismic')
    ax[1].imshow(binary_preds[ix].squeeze(), vmin=0, vmax=1)


# %% [markdown]
#  ## Node Deletion
#
#  There is one option for removing nodes, and that is by name. It is not recommended to use low level
#  APIs such as `removeChild()` as they can remove non-nodes.
#
#  In this example `getNodes()` is used to get the nodes and delete them one at a time.# Deleting Nodes
#

# %% [markdown]
# ## Setup

# %%
import MaterialX as mx

libraryPath = mx.FilePath('libraries')
stdlib = mx.createDocument()
searchPath = mx.FileSearchPath()
libFiles = mx.loadLibraries([ libraryPath ], searchPath, stdlib)

doc = mx.createDocument()
doc.importLibrary(stdlib)

# Write predicate
def skipLibraryElement(elem):
    return not elem.hasSourceUri()

# %% [markdown]
#

# %%
# There is no way to remove all nodes at the same time, so we remove them
# one at a timme
print('Clear library:')
for elem in doc.getChildren():
    if elem.hasSourceUri():
        doc.removeChild(elem.getName())

print('- Result:')
print('  ', mx.prettyPrint(doc))


import abc
from typing import Any, ClassVar, Dict, Optional, Set, Tuple

import colt
import flax
import jax

Array = Any

class Model(abc.ABC, flax.linen.Module, colt.Registrable):
    rngkeys: ClassVar[Set[str]] = set()
    mutables: ClassVar[Set[str]] = set()
    default_training_module: ClassVar[str] = "default"

    @abc.abstractmethod
    def __call__(
        self,
        *args: Any,
        train: bool = False,
        **kwargs: Any,
    ) -> Dict[str, Any]:
        raise NotImplementedError

    def split_rngs(
        self,
        rngs: Any,
        train: bool = False,
        additional_keys: Optional[Set[str]] = None,
    ) -> Tuple[Any, Dict[str, Any]]:
        additional_keys = additional_keys or set()
        required_rngkeys = list(self.rngkeys | additional_keys)

        if not train:
            if "dropout" in required_rngkeys:
                required_rngkeys.remove("dropout")

        rngs, *new_rngs = jax.random.split(rngs, len(required_rngkeys) + 1)
        rngs_for_call = {name: rng for name, rng in zip(required_rngkeys, new_rngs)}
        return rngs, rngs_for_call


import os
import pipes
import subprocess
import logging

def run(command):

    logger = logging.getLogger(__name__)

    logger.debug(command)

    os.environ['PATH'] = '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/share/npm/bin:'

    process = subprocess.Popen([command],
        shell=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    stdout = process.stdout.readline()
    stderr = process.stderr.readline()

    if stdout: logger.info(stdout)
    if stderr: logger.warning(stderr)

def preBuild(site):
    run('coffee -c %s/js/*.coffee' % pipes.quote(site.static_path))


from ast import alias
from turtle import title
import discord
from discord.ext import commands

from youtube_dl import YoutubeDL

class musicCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

        self.isPlaying = False
        self.isPaused = False

        self.musicQueue = []
        self.ydlOptions = {'format': 'bestaudio', 'noplaylist':'True'}
        self.ffmpegOptions = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

        self.vc = None

    def searchYT(self, item):
        with YoutubeDL(self.ydlOptions) as ydl:
            try:
                info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'][0]
            except Exception:
                return False

        return {'source': info['formats'][0]['url'], 'title': info['title']}

    def playNext(self):
        if len(self.musicQueue) > 0:
            self.isPlaying = True

            m_url = self.musicQueue[0][0]['source']

            self.musicQueue.pop(0)

            self.vc.play(discord.FFmpegPCMAudio(m_url, **self.ffmpegOptions), after=lambda e: self.playNext())
        else:
            self.isPlaying = False

    async def playSong(self, ctx):
        if len(self.musicQueue) > 0:
            self.isPlaying = True

            m_url = self.musicQueue[0][0]['source']

            if self.vc == None or not self.vc.is_connected():
                self.vc = await self.musicQueue[0][1].connect()

                if self.vc == None:
                    await ctx.send("I CAN'T JOIN, ASSHOLE.")
                    return
            else:
                await self.vc.move_to(self.musicQueue[0][1])

            self.musicQueue.pop(0)

            self.vc.play(discord.FFmpegPCMAudio(m_url, **self.ffmpegOptions), after=lambda e: self.playNext())
        else:
            self.isPlaying = False

    @commands.command(name="play", aliases=["p","playing"])
    async def play(self, ctx, *args):
        query = " ".join(args)

        voice_channel = ctx.author.voice.channel
        if voice_channel is False:
            await ctx.send("COULD YOU USE YOUR THINKING FLESH MOUND AND JOIN A CHANNEL FIRST?")
        elif self.isPaused:
            self.vc.resume()
        else:
            song = self.searchYT(query)
            if type(song) == type(True):
                await ctx.send("COULDN'T GET WHATEVER THE FUCK THAT WAS. REMEMBER: NO PLAYLISTS AND YOUTUBE ONLY, ASSCLOWN.")
            else:
                await ctx.send("ADDED THAT SHIT TO THE QUEUE.")
                self.musicQueue.append([song, voice_channel])

                if self.isPlaying == False:
                    await self.playSong(ctx)

    @commands.command(name="pause")
    async def pause(self, ctx, *args):
        if self.isPlaying:
            self.isPlaying = False
            self.isPaused = True
            await ctx.send("PAUSING, OR WHATEVER THE FUCK.")
            self.vc.pause()
        elif self.isPaused:
            self.isPaused = False
            self.isPlaying = True
            self.vc.resume()

    @commands.command(name = "resume", aliases=["r"])
    async def resume(self, ctx, *args):
        if self.isPaused:
            self.isPaused = False
            self.isPlaying = True
            await ctx.send("RESUMING, OR SOMETHING. I DON'T CARE.")
            self.vc.resume()

    @commands.command(name="skip", aliases=["s"])
    async def skip(self, ctx):
        if self.vc != None and self.vc:
            await ctx.send("DON'T CARE, NEXT.")
            self.vc.stop()

            await self.playSong(ctx)

    @commands.command(name="queue", aliases=["q"])
    async def queue(self, ctx):
        queue = ""
        await ctx.send("UP NEXT ON THE PRIMITIVE, SHAMEFUL RAZZMATAZZ OF GO FUCK YOURSELF IS: \n")
        for i in range(0, len(self.musicQueue)):
            queue += f"{i+1}. {self.musicQueue[i][0]['title']} \n"

        if queue != "":
            await ctx.send(queue)
        else:
            await ctx.send("NOTHING. NO UPCOMING SONGS.")

    @commands.command(name="clear", aliases=["c", "bin"])
    async def clear(self, ctx):
        self.musicQueue = []
        await ctx.send("HOLY SHIT. THANK FUCK.")

    @commands.command(name="leave", aliases=["disconnect", "l", "d"])
    async def dc(self, ctx):
        self.isPlaying = False
        self.isPaused = False
        await ctx.send("WHATEVER.")
        await self.vc.disconnect()
        self.vc = None
        self.musicQueue = []


import configparser
import enum
import logging
import os
import sys
import tempfile
from typing import List, Tuple

from gi.repository import Gio, GLib, Gtk

import meld.misc
from meld.conf import _

log = logging.getLogger(__name__)

class RecentType(enum.Enum):
    File = "File"
    Folder = "Folder"
    VersionControl = "Version control"
    Merge = "Merge"

class RecentFiles:

    mime_type = "application/x-meld-comparison"
    recent_path = os.path.join(GLib.get_user_data_dir(), "meld")
    recent_suffix = ".meldcmp"

    # Recent data
    app_name = "Meld"

    def __init__(self):
        self.recent_manager = Gtk.RecentManager.get_default()
        self.recent_filter = Gtk.RecentFilter()
        self.recent_filter.add_mime_type(self.mime_type)
        self._stored_comparisons = {}
        self.app_exec = os.path.abspath(sys.argv[0])

        if not os.path.exists(self.recent_path):
            os.makedirs(self.recent_path)

        self._clean_recent_files()
        self._update_recent_files()
        self.recent_manager.connect("changed", self._update_recent_files)

    def add(self, tab, flags=None):
        """Add a tab to our recently-used comparison list

        The passed flags are currently ignored. In the future these are to be
        used for extra initialisation not captured by the tab itself.
        """
        try:
            recent_type, gfiles = tab.get_comparison()
        except Exception:
            log.warning(f'Failed to get recent comparison data for {tab}')
            return

        # While Meld handles comparisons including None, recording these as
        # recently-used comparisons just isn't that sane.
        if None in gfiles:
            return

        uris = [f.get_uri() for f in gfiles]
        names = [f.get_parse_name() for f in gfiles]

        # If a (type, uris) comparison is already registered, then re-add
        # the corresponding comparison file
        comparison_key = (recent_type, tuple(uris))
        if comparison_key in self._stored_comparisons:
            gfile = Gio.File.new_for_uri(
                self._stored_comparisons[comparison_key])
        else:
            recent_path = self._write_recent_file(recent_type, uris)
            gfile = Gio.File.new_for_path(recent_path)

        if len(uris) > 1:
            display_name = " : ".join(meld.misc.shorten_names(*names))
        else:
            display_path = names[0]
            userhome = os.path.expanduser("~")
            if display_path.startswith(userhome):
                # FIXME: What should we show on Windows?
                display_path = "~" + display_path[len(userhome):]
            display_name = _("Version control:") + " " + display_path
        # FIXME: Should this be translatable? It's not actually used anywhere.
        description = "{} comparison\n{}".format(
            recent_type.value, ", ".join(uris))

        recent_metadata = Gtk.RecentData()
        recent_metadata.mime_type = self.mime_type
        recent_metadata.app_name = self.app_name
        recent_metadata.app_exec = "%s --comparison-file %%u" % self.app_exec
        recent_metadata.display_name = display_name
        recent_metadata.description = description
        recent_metadata.is_private = True
        self.recent_manager.add_full(gfile.get_uri(), recent_metadata)

    def read(self, uri: str) -> Tuple[RecentType, List[Gio.File]]:
        """Read stored comparison from URI"""
        comp_gfile = Gio.File.new_for_uri(uri)
        comp_path = comp_gfile.get_path()
        if not comp_gfile.query_exists(None) or not comp_path:
            raise IOError("Recent comparison file does not exist")

        try:
            config = configparser.RawConfigParser()
            config.read(comp_path)
            assert (config.has_section("Comparison") and
                    config.has_option("Comparison", "type") and
                    config.has_option("Comparison", "uris"))
        except (configparser.Error, AssertionError):
            raise ValueError("Invalid recent comparison file")

        try:
            recent_type = RecentType(config.get("Comparison", "type"))
        except ValueError:
            raise ValueError("Invalid recent comparison file")

        uris = config.get("Comparison", "uris").split(";")
        gfiles = [Gio.File.new_for_uri(u) for u in uris]

        return recent_type, gfiles

    def _write_recent_file(self, recent_type: RecentType, uris):
        # TODO: Use GKeyFile instead, and return a Gio.File. This is why we're
        # using ';' to join comparison paths.
        with tempfile.NamedTemporaryFile(
                mode='w+t', prefix='recent-', suffix=self.recent_suffix,
                dir=self.recent_path, delete=False) as f:
            config = configparser.RawConfigParser()
            config.add_section("Comparison")
            config.set("Comparison", "type", recent_type.value)
            config.set("Comparison", "uris", ";".join(uris))
            config.write(f)
            name = f.name
        return name

    def _clean_recent_files(self):
        # Remove from RecentManager any comparisons with no existing file
        meld_items = self._filter_items(self.recent_filter,
                                        self.recent_manager.get_items())
        for item in meld_items:
            if not item.exists():
                self.recent_manager.remove_item(item.get_uri())

        meld_items = [item for item in meld_items if item.exists()]

        # Remove any comparison files that are not listed by RecentManager
        item_uris = [item.get_uri() for item in meld_items]
        item_paths = [
            Gio.File.new_for_uri(uri).get_path() for uri in item_uris]
        stored = [p for p in os.listdir(self.recent_path)
                  if p.endswith(self.recent_suffix)]
        for path in stored:
            file_path = os.path.abspath(os.path.join(self.recent_path, path))
            if file_path not in item_paths:
                try:
                    os.remove(file_path)
                except OSError:
                    pass

    def _update_recent_files(self, *args):
        meld_items = self._filter_items(self.recent_filter,
                                        self.recent_manager.get_items())
        item_uris = [item.get_uri() for item in meld_items if item.exists()]
        self._stored_comparisons = {}
        for item_uri in item_uris:
            try:
                recent_type, gfiles = self.read(item_uri)
            except (IOError, ValueError):
                continue
            # Store and look up comparisons by type and paths
            gfile_uris = tuple(gfile.get_uri() for gfile in gfiles)
            self._stored_comparisons[recent_type, gfile_uris] = item_uri

    def _filter_items(self, recent_filter, items):
        getters = {Gtk.RecentFilterFlags.URI: "uri",
                   Gtk.RecentFilterFlags.DISPLAY_NAME: "display_name",
                   Gtk.RecentFilterFlags.MIME_TYPE: "mime_type",
                   Gtk.RecentFilterFlags.APPLICATION: "applications",
                   Gtk.RecentFilterFlags.GROUP: "groups",
                   Gtk.RecentFilterFlags.AGE: "age"}
        needed = recent_filter.get_needed()
        attrs = [v for k, v in getters.items() if needed & k]

        filtered_items = []
        for i in items:
            filter_data = {}
            for attr in attrs:
                filter_data[attr] = getattr(i, "get_" + attr)()
            filter_info = Gtk.RecentFilterInfo()
            filter_info.contains = needed
            for f, v in filter_data.items():
                # https://bugzilla.gnome.org/show_bug.cgi?id=695970
                if isinstance(v, list):
                    continue
                setattr(filter_info, f, v)
            if recent_filter.filter(filter_info):
                filtered_items.append(i)
        return filtered_items

    def __str__(self):
        items = self.recent_manager.get_items()
        descriptions = []
        for i in self._filter_items(self.recent_filter, items):
            descriptions.append("%s\n%s\n" % (i.get_display_name(),
                                              i.get_uri_display()))
        return "\n".join(descriptions)

recent_comparisons = RecentFiles()


# coding:utf-8

import json
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
from collections import Counter
from pandas import DataFrame, Series

# 创建用户字段名称列表
unames = ['user_id','gender','age','occupation','zip']
# 读取dat类型文件 分隔符为::
users = pd.read_table('data/users.dat', sep="::", header=None, names=unames)
# 创建评分字段名称列表
rnames = ['user_id','movie_id', 'rating', 'timestamp']
ratings = pd.read_table('data/ratings.dat', sep="::", header=None, names=rnames)
# 创建电影字段名称列表
mnames = ['movie_id', 'title', 'genres']
movies = pd.read_table('data/movies.dat', sep="::", header=None, names=mnames)
# 合并DF
data = pd.merge(users, pd.merge(ratings, movies))
# 对data按照年龄 电影名称进行分组 计算评分均值
mean_ratings = data.pivot_table(values='rating', index='title', columns='gender', aggfunc='mean')
print(mean_ratings[:2])
# 过滤掉评分数据不够250条的数据 返回Series对象 标签为分组值 对应元素为size条数
ratings_by_title = data.groupby('title').size()
print(ratings_by_title)
# 开始过滤
active_titles = ratings_by_title.index[ratings_by_title >= 250]
# 获取对应行从DF中 传入一个Series 过滤掉数量小于250的data行记录
mean_ratings = mean_ratings.ix[active_titles]  # 对应index中第一个索引
# 获取女性最喜欢的电影
top_female_ratings = mean_ratings.sort_values(by='F', ascending=False)  # 降序排列
print(top_female_ratings[:10])
# 计算评分分歧 添加新的列标记diff
top_female_ratings['diff'] = top_female_ratings['F'] - top_female_ratings['M']
# 对diff列值进行排序 并倒序 [i:j:s] 当步长s为-1 i和j 默认为-1 到-len(list)-1 即倒序复制列表
print(top_female_ratings[:2])
sorted_by_diff = top_female_ratings.sort_values('diff')
print(sorted_by_diff[::-1][:10])


# Numbers

from math import * # For floor and ceil functions

## Arithmetic Operations ##

# 01. Parentheses

num1 = (5)          # = 6

# 02. Exponents

num2 = 5 ** 3       # = 125                 (Returns 5 ^ 3)
num2_1 = 25 ** 0.5  # = 5.0                 (Similar to sqrt(25))

# 03. Multiplication / Division / Modulus

num3 = 5 * 7        # = 35
num3_1 = 5 / 7      # = 0.7142857142857143  (True Division)
num3_2 = 5 // 7     # = 0                   (Returns Quotient)
num3_3 = 5 % 7      # = 5                   (Returns Remainder)

# 04. Addition / Subtraction

num4 = 5 + 7        # = 12
num4_1 = 5 - 7      # = -2

# Order of Operation (01 -> 04)

arithmetic_operation = 7 * 4 ** 5 % 6 - 3 / 2 + 1 // 8 + 9  # Grouping looks like: (((((7 * (4 ** 5)) % 6) - (3 / 2)) + (1 // 8)) + 9)

print(arithmetic_operation) # = 11.5

## Number Functions ##

# Force Convert into Datatype

num5 = -3.0
int_number = int(num5)      # = -3      (Ensures that num5 fits to be an int, can convert from str as well)
float_number = float(num5)  # = -3.0    (Ensures that num5 becomes a float, can convert from str as well)
positive_number = abs(num5) # = 3.0     (Ensures that num5 becomes positive value)

# Comparison

num6 = 5
num7 = 19

larger_number = max(num1, num2, num3, num4, num5, num6, num7)
smaller_number = min(num1, num2, num3, num4, num5, num6, num7)

print(larger_number)        # 125
print(smaller_number)       # -3.0

# Rounding Numbers

num8 = 5.3
num9 = 5.8

normal_round_down = round(num8) # 5
normal_round_up = round(num9)   # 6

force_round_down1 = floor(num8) # 5
force_round_down2 = floor(num9) # 5 (5.8 still round down to 5)

force_round_up1 = ceil(num8)    # 6 (5.3 still round up to 6)
force_round_up2 = ceil(num9)    # 6

# Printing Numbers with Texts

message = "The lucky number is " + str(num6) + "."    # String cannot concatenate with non-strings.
print(message)              # The lucky number is 5.


import sys
import os
import pandas as pd
import numpy as np

def month_sess(sess_path,out_path):
    '''
    sess_path: csv file for sessions
    out_path: folder to save sessions by month
    '''
    ses_df = pd.read_csv(sess_path)
    #dealing datetime format
    ses_df.date = ses_df.date.apply(lambda x:x.split(".")[0] if "." in x else x)
    ses_df["datetime"] = pd.to_datetime(ses_df.date,format="%Y-%m-%d %H:%M:%S")
    #print(ses_df.datetime.dt.year.unique())
    #print(ses_df.datetime.dt.month.unique())
    # loop thru year save per month
    for year in ses_df.datetime.dt.year.unique():
        print("dealing with year: {} data".format(year))
        year_ses = ses_df[ses_df.datetime.dt.year==year]
        for month in year_ses.datetime.dt.month.unique():
            print("dealing with month: {} data".format(month))
            month_ses = year_ses[year_ses.datetime.dt.month==month]
            month_ses.to_csv(out_path+str(year)+"_"+str(month)+"session.csv",index=False)
    #print(ses_df.datetime.unique())
    return

if __name__=="__main__":
    month_sess("../../recsys2022/train_purchases.csv","../datasets/monthly_cand/")


import os

from .defaults import *  # noqa

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

DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'travis_ci',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

SECRET_KEY = 'zj6ay3l7s^ga3!*!zi(s81+jj01eblt=wxzzd3=__n0wh0a5uw'

LANGUAGE_CODE = "fr-FR"

TIME_ZONE = 'Europe/Paris'

STATIC_ROOT = os.path.join(BASE_DIR, 'static/static/')

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media/')

MEDIA_URL = "/media/"

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


def is_decimal(n):
	a=int(n**0.5)
	for i in range(2,a+1):
		if n%i==0:
			return False
		else:
			continue
	return True

def solution(brown,yellow):
	if is_decimal(yellow):
		width=(yellow+2)
		height=3
		return [width,height]
	else:
		# 2로 나눴을 때 가능한 숫자만 높이로 지정한다.
		max_height=(int(yellow//2))
		for h in range(1,max_height+1):
			if yellow%h==0:
				garo=int((yellow/h)+2)
				sero=h+2
				if garo*sero-yellow==brown:
					return [garo,sero]


num = int(input("请输入整数："))
if num <= 1:
    print("不是素数")
else:
    for item in range(2, num):
        if num % item == 0:
            print("不是素数")
            break  # 若发现满足条件的数字，就不再判断后面的了，不执行else语句。
    else:
        print("是素数")


print("Welcome to love calculator!")
name1=input("What is your name? ")
name2=input("What is their name? ")
name=name1.lower()+name2.lower()
t=name.count("t")
r=name.count("r")
u=name.count("u")
e=name.count("e")
count_true=t+r+u+e
l=name.count("l")
o=name.count("o")
v=name.count("v")
e=name.count("e")
count_love=l+o+v+e
love_percent=count_true*10+count_love
if love_percent<10 or love_percent>90:
    print("Your score is",love_percent,", you go together like coke and mentos.")
elif 40<love_percent<50:
    print("Your score is",love_percent,", you are alright together.")
else:
    print("Your score is",love_percent)


from django.contrib import admin
from .models import *

# Register your models here.

class ExerciseAdmin(admin.ModelAdmin):
    list_display = ("for_lecture", )
    list_display_links = ("for_lecture", )
    search_fields = ("for_lecture", "task")

class AnswerAdmin(admin.ModelAdmin):
    list_display = ("for_exercise", )
    list_display_links = ("for_exercise", )
    search_fields = ("for_exercise", "option", "correct")

admin.site.register(Exercise, ExerciseAdmin)
admin.site.register(Answer, AnswerAdmin)


# Import the dependencies.
from statistics import mean
import pandas as pd
import numpy as np
import datetime as dt
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

import flask
from flask import Flask , jsonify

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()

# reflect the tables
Base.prepare(engine, reflect = True)

# Save references to each table
Measurement = Base.classes.measurement
Station = Base.classes.station

# Create our session (link) from Python to the DB
session = Session(bind=engine)

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

#################################################
# Flask Routes
#################################################

# welcome page
@app.route("/")
def welcome():
    """List all available routes"""
    return (
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/'start_date'/'end_date'
"
    )

# precipitation
@app.route("/api/v1.0/precipitation")
def precipitation():
    # precipitaion analysis
    prcp_results = session.query(Measurement.prcp, Measurement.date).\
    filter(Measurement.date > '2016-08-23').\
    order_by(Measurement.date).all()
    # close session
    session.close()

    # Create a dictionary for prcp results and make a list of the values
    prcp_list = []
    for prcp, date in prcp_results:
        prcp_dict = {}
        prcp_dict["Precipitation"] = prcp
        prcp_dict["Date"] = date
        prcp_list.append(prcp_dict)

    # return jsonify list
    return jsonify(prcp_list)

# stations
@app.route("/api/v1.0/stations")
def station():
    # get stations
    station_results = session.query(Station.station, Station.name).all()
    # close session
    session.close()

    # list
    station_list = []
    # loop like precipitation
    for station, name in station_results:
        station_dict = {}
        station_dict["Station"] = station
        station_dict["Name"] = name
        station_list.append(station_dict)

    # return
    return jsonify(station_list)

# tobs
@app.route("/api/v1.0/tobs")
def tobs():
    # Query the dates and temperature observations of the most-active station for the previous year of data.
    station_data = session.query(Measurement.date, Measurement.tobs).\
    filter(Measurement.station == 'USC00519281').\
    filter(Measurement.date >= '2016-8-18').all()

    # close session
    session.close()

    # tobs list
    tobs_list = []
    # loop like above
    for date, tobs in station_data:
        station_dict = {}
        station_dict["Date"] = date
        station_dict["tobs"] = tobs
        tobs_list.append(station_dict)

    # return
    return jsonify(tobs_list)

# start and end date
@app.route("/api/v1.0/<start>/<end>")
def temperature(start, end):
    # Return a JSON list of the minimum temperature, the average temperature, \
    # and the maximum temperature for a specified start or start-end range.
    temp_results = session.query(Measurement).\
    filter(Measurement.date >= start).\
    filter(Measurement.date <= end)

    # close session
    session.close()

    temp_list = []
    for temp in temp_results:
        temp_list.append(temp.tobs)

    temp_dict = {
        "Min": min(temp_list),
        "Average": mean(temp_list),
        "Max": max(temp_list)
    }
    return jsonify(temp_dict)

# boilerplate
if __name__ == "__main__":
    app.run(debug=True)


import sys

# compute the utility matrix M
M = {}

f = open(sys.argv[1], 'r')
while True:
    line = f.readline()
    if not line:  break

    line = line.split(',')

    if int(line[0]) not in M.keys():
        M[int(line[0])] = {}
    M[int(line[0])][int(line[1])] = float(line[2])

f.close()

# function for measuring cosine distance
def cosDistance(m1, m2):
    len1 = sum([i**2 for i in m1.values()]) ** 0.5
    len2 = sum([i**2 for i in m2.values()]) ** 0.5
    dot = 0
    for j in m1.keys():
        if j in m2.keys():
            dot += m1[j]*m2[j]

    if len1==0 or len2 ==0:
        return 0
    else:
        return dot/(len1*len2)

# 1. user-based
# normalize
Mu_norm = {}
for u in M.keys():
    Mu_norm[u] = {}
    user_avg = sum([i for i in M[u].values()]) / len(M[u].keys())
    for i in M[u].keys():
        Mu_norm[u][i] = M[u][i] - user_avg

# measure distance
distance = {}
for u in M.keys():
    if u != 600:
        distance[u] = cosDistance(Mu_norm[u], Mu_norm[600])

# find similar users
sim_user = []
for w in sorted(distance, key=distance.get, reverse=True)[:10]:
    sim_user.append(w)

# calculate rating and print
rating = {}
for i in range(1, 1001):
    count = 0
    s = 0
    for u in sim_user:
        if i in M[u].keys():
            count += 1
            s += M[u][i]
    if count == 0:
        rating[i] = 0
    else:
        rating[i] = s/count

top5 = sorted(rating.items(), key=lambda x: (-x[1], x[0]))[:5]
for t in top5:
    print("%d\t%.1f" %(t[0], t[1]))

# 2. item-based
# rearrange norm dictionary for distance measure
Mi_norm = {}
for u in Mu_norm.keys():
    for i in Mu_norm[u].keys():
        if i not in Mi_norm.keys():
            Mi_norm[i] = {}
        Mi_norm[i][u] = Mu_norm[u][i]

# measure distance
distance2 = {}
for i1 in range(1, 1001):
    if i1 in Mi_norm.keys():
        distance2[i1] = {}
        for i2 in Mi_norm.keys():
            if i2 != i1:
                distance2[i1][i2] = cosDistance(Mi_norm[i1], Mi_norm[i2])

# find similar items for each items
sim_item = {}
for i in range(1, 1001):
    if i in Mi_norm.keys():
        sim_item[i] = [w for w in sorted(distance2[i], key=distance2[i].get, reverse=True)[:10]]

# calculate rating and print
rating = {}
for i in range(1, 1001):
    if i in Mi_norm.keys():
        count = 0
        s = 0
        for j in sim_item[i]:
            if j in M[600].keys():
                count += 1
                s += M[600][j]
            if count == 0:
                #print(0)
                rating[i] = 0
            else:
                #print(count, sum/count)
                rating[i] = s/count

top5 = sorted(rating.items(), key=lambda x: (-x[1], x[0]))[:5]
for t in top5:
    print("%d\t%.1f" %(t[0], t[1]))