import csv
import os
import typing
from collections import OrderedDict
from datetime import datetime
from pathlib import PurePosixPath

from sqlalchemy import or_
from sqlalchemy.orm import Session

from urllib.parse import urlparse

from backend.common.entities.dataset_asset import DatasetAsset
from backend.common.entities.entity import Entity
from backend.common.entities.geneset import Geneset
from backend.common.corpora_orm import (
    DbDataset,
    DbDatasetArtifact,
    DbDatasetProcessingStatus,
    UploadStatus,
    ProcessingStatus,
    DbGenesetDatasetLink,
    generate_id,
    DatasetArtifactFileType,
    ConversionStatus,
)
from backend.common.utils.corpora_constants import CorporaConstants
from backend.common.utils.db_helpers import clone
from backend.common.utils.ontology_mappings.ontology_map_loader import ontology_mappings
from backend.common.utils.s3_buckets import buckets

class Dataset(Entity):
    table = DbDataset

    def __init__(self, db_object: DbDataset):
        super().__init__(db_object)

    @classmethod
    def create(
        cls,
        session: Session,
        revision: int = 0,
        name: str = "",
        artifacts: list = None,
        processing_status: dict = None,
        **kwargs,
    ) -> "Dataset":
        """
        Creates a new dataset and related objects and store in the database. IDs are generated for all new table
        entries.
        """
        dataset = DbDataset(
            revision=revision,
            name=name,
            **kwargs,
        )
        if artifacts:
            dataset.artifacts = [DbDatasetArtifact(dataset_id=dataset.id, **art) for art in artifacts]
        processing_status = processing_status if processing_status else {}
        dataset.processing_status = DbDatasetProcessingStatus(dataset_id=dataset.id, **processing_status)
        session.add(dataset)
        session.commit()

        return cls(dataset)

    def update(self, artifacts: list = None, processing_status: dict = None, commit=True, **kwargs) -> None:
        """
        Update an existing dataset to match provided the parameters. The specified column are replaced.
        :param artifacts: Artifacts to create and connect to the dataset. If present, the existing attached entries will
         be removed and replaced with new entries. If an empty list is provided, all dataset artifacts will be deleted.
        :param processing_status: A Processing status entity to create and connect to the dataset. If present, the
         existing attached entries will be removed and replaced with new entries. If an empty dictionary is provided the
         processing_status will be deleted.
        :param kwargs: Any other fields in the dataset that will be replaced.
        """
        if any([i is not None for i in [artifacts, processing_status]]):
            if artifacts is not None:
                for af in self.artifacts:
                    self.session.delete(af)
                new_objs = [DbDatasetArtifact(dataset_id=self.id, **art) for art in artifacts]
                self.session.add_all(new_objs)
            if processing_status is not None:
                if self.processing_status:
                    self.session.delete(self.processing_status)
                if processing_status:
                    new_obj = DbDatasetProcessingStatus(dataset_id=self.id, **processing_status)
                    self.session.add(new_obj)
        super().update(commit=commit, **kwargs)

    @classmethod
    def get(
        cls, session: Session, dataset_id=None, include_tombstones=False, collection_id=None
    ) -> typing.Optional["Dataset"]:
        filters = []
        if not include_tombstones:
            filters.append(cls.table.tombstone != True)  # noqa
        if collection_id:
            filters.append(cls.table.collection_id == collection_id)
        if dataset_id:
            filters.append(cls.table.id == dataset_id)
        result = session.query(cls.table).filter(*filters).one_or_none()
        dataset = cls(result) if result else None
        return dataset

    @classmethod
    def get_by_explorer_url(cls, session: Session, explorer_url):
        """
        Return the most recently created dataset with the given explorer_url or None
        """

        def _get_by_explorer_url_query(url):
            filters = [cls.table.explorer_url == url]
            dataset = session.query(cls.table).filter(*filters).order_by(cls.table.created_at.desc()).limit(1).all()
            return cls(dataset[0]) if dataset else None

        # If dataset cannot be found, look for the url with (or without) the final slash
        dataset = _get_by_explorer_url_query(explorer_url)
        if not dataset:
            if explorer_url[-1] == "/":
                dataset = _get_by_explorer_url_query(explorer_url[:-1])
            else:
                dataset = _get_by_explorer_url_query(f"{explorer_url}/")

        return dataset

    @classmethod
    def list(
        cls, session: Session, collection_id: str = None, include_tombstones: bool = False
    ) -> typing.List["Dataset"]:
        """
        Retrieves a list of Datasets from the database

        :param session: an open database session
        :param include_tombstones: True of to include tombstoned datasets
        :return: A list of Datasets
        """
        filters = []

        if not include_tombstones:
            filters.append(or_(cls.table.tombstone == False, cls.table.tombstone == None))  # noqa
        if collection_id:
            filters.append(cls.table.collection_id == collection_id)

        if filters:
            return [cls(obj) for obj in session.query(cls.table).filter(*filters).all()]
        else:
            return [cls(obj) for obj in session.query(cls.table)]

    def get_asset(self, asset_id) -> typing.Union[DatasetAsset, None]:
        """
        Retrieve the asset if it exists in the dataset.
        :param asset_id: uuid of the asset to find
        :return: If the asset is found it is returned, else None is returned.
        """
        asset = [asset for asset in self.artifacts if asset.id == asset_id]
        return None if not asset else DatasetAsset(asset[0])

    def get_assets(self):
        """
        Retrieve all the assets for the dataset
        """
        assets = [
            asset for asset in self.artifacts if asset.filename != CorporaConstants.ORIGINAL_H5AD_ARTIFACT_FILENAME
        ]
        return assets

    @staticmethod
    def transform_sex_for_schema_2_0_0(dataset):
        schema_version = dataset.get("schema_version")
        if "sex" in dataset and (schema_version is None or schema_version < "2.0.0"):
            dataset["sex"] = [{"label": s, "sex_ontology_term_id": "unknown"} for s in dataset["sex"]]

    @staticmethod
    def transform_organism_for_schema_2_0_0(dataset):
        schema_version = dataset.get("schema_version")
        # If organism is an object (version 1.1.0), wrap it into an array to be 2.0.0 compliant
        if "organism" in dataset and (schema_version is None or schema_version < "2.0.0"):
            dataset["organism"] = [dataset["organism"]]

    @staticmethod
    def enrich_development_stage_with_ancestors(dataset):
        Dataset._enrich_with_ancestors(
            dataset, "development_stage", ontology_mappings.development_stage_ontology_mapping
        )

    @staticmethod
    def enrich_tissue_with_ancestors(dataset):
        """
        Tag dataset with ancestors for all tissues in the given dataset, if any.
        """
        Dataset._enrich_with_ancestors(dataset, "tissue", ontology_mappings.tissue_ontology_mapping)

    @staticmethod
    def enrich_cell_type_with_ancestors(dataset):
        """
        Tag dataset with ancestors for all cell types in the given dataset, if any.
        """
        Dataset._enrich_with_ancestors(dataset, "cell_type", ontology_mappings.cell_type_ontology_mapping)

    def _enrich_with_ancestors(dataset, key, ontology_mapping):
        """
        Tag dataset with ancestors for all values of the given key, if any.
        """
        if key not in dataset:
            return

        terms = [e["ontology_term_id"] for e in dataset[key]]

        if not terms:
            return

        ancestors = [ontology_mapping.get(term) for term in terms]
        flattened_ancestors = [item for sublist in ancestors if sublist for item in sublist]
        unique_ancestors = list(OrderedDict.fromkeys(flattened_ancestors))
        if unique_ancestors:
            dataset[f"{key}_ancestors"] = unique_ancestors

    def _create_new_explorer_url(self, new_id: str) -> str:
        if self.explorer_url is None:
            return None
        original_url = urlparse(self.explorer_url)
        original_path = PurePosixPath(original_url.path)
        new_path = str(original_path.parent / f"{new_id}.cxg/")
        new_url = original_url._replace(path=new_path).geturl()
        # Note: the final slash is mandatory, otherwise the explorer won't load this link
        return f"{new_url}/"

    def create_revision(self, revision_collection_id: str) -> "Dataset":
        """
        Generate a dataset revision from a dataset in a public collection
        :param revision_collection_id: specify the collection revision to which this dataset revision belongs
        :return: dataset revision.

        """
        revision_dataset_id = generate_id()
        revision_explorer_url = self._create_new_explorer_url(revision_dataset_id)
        revision_dataset = clone(
            self.db_object,
            id=revision_dataset_id,
            collection_id=revision_collection_id,
            original_id=self.id,
            explorer_url=revision_explorer_url,
        )
        self.session.add(revision_dataset)
        for artifact in self.artifacts:
            self.session.add(clone(artifact, dataset_id=revision_dataset.id))
        if self.processing_status:
            self.session.add(clone(self.processing_status, dataset_id=revision_dataset.id))
        self.session.commit()
        return Dataset(revision_dataset)

    def tombstone_dataset_and_delete_child_objects(self):
        self.update(tombstone=True, artifacts=[], processing_status={})
        self.session.query(DbGenesetDatasetLink).filter(DbGenesetDatasetLink.dataset_id == self.id).delete(
            synchronize_session="evaluate"
        )

    def asset_deletion(self):
        for artifact in self.artifacts:
            asset = DatasetAsset.get(self.session, artifact.id)
            asset.queue_s3_asset_for_deletion()
            asset.delete(commit=False)

    @staticmethod
    def new_processing_status() -> dict:
        return {
            "upload_status": UploadStatus.WAITING,
            "upload_progress": 0,
            "processing_status": ProcessingStatus.PENDING,
        }

    def copy_csv_to_s3(self, csv_file: str) -> str:
        object_name = get_cxg_bucket_path(self.explorer_url)
        s3_file = f"{object_name}-genesets.csv"
        buckets.explorer_bucket.upload_file(csv_file, s3_file)
        return s3_file

    def generate_tidy_csv_for_all_linked_genesets(self, csv_file_path: str) -> str:
        csv_file = os.path.join(csv_file_path, "geneset.csv")
        fieldnames = ["GENE_SET_NAME", "GENE_SET_DESCRIPTION", "GENE_SYMBOL", "GENE_DESCRIPTION"]
        genesets = []
        max_additional_params = 0
        for geneset in self.genesets:
            geneset_entity = Geneset(geneset)
            gene_rows, gene_max = geneset_entity.convert_geneset_to_gene_dicts()
            if gene_max > max_additional_params:
                max_additional_params = gene_max
            genesets += gene_rows

        for i in range(1, max_additional_params + 1):
            fieldnames.append(f"PROVENANCE{i}")
            fieldnames.append(f"PROVENANCE{i}_DESCRIPTION")

        with open(csv_file, "w") as f:
            writer = csv.DictWriter(f, fieldnames=fieldnames)
            writer.writeheader()
            for gene in genesets:
                writer.writerow(gene)
        return csv_file

    def reprocess(self):
        if not self.published:
            self.asset_deletion()
        self.update(
            name="",
            organism=None,
            tissue=None,
            assay=None,
            disease=None,
            sex=None,
            self_reported_ethnicity=None,
            development_stage=None,
            cell_type=None,
            published=False,
            revision=self.revision + 1,
            explorer_url=None,
            artifacts=[],
        )

    def get_most_recent_artifact(self, filetype=DatasetArtifactFileType.CXG):
        filters = [DbDatasetArtifact.dataset_id == self.id, DbDatasetArtifact.filetype == filetype]
        artifact = (
            self.session.query(DbDatasetArtifact)
            .filter(*filters)
            .order_by(DbDatasetArtifact.created_at.desc())
            .limit(1)
            .all()
        )
        return DatasetAsset(artifact[0]) if artifact else None

    def publish_new(self, now: datetime):
        """
        Publish a new dataset with the published_at datetime populated
        with the provided datetime.
        :param now: Datetime to populate dataset's published_at.
        """
        collection_id = self.collection.revision_of if self.collection.revision_of else self.collection.id
        self.update(collection_id=collection_id, published=True, published_at=now, commit=False)

    def publish_revision(self, revision: "Dataset", now: datetime) -> bool:
        """
        Publish a revision of a dataset if the dataset under revision is
        different from the existing dataset.
        :return: True if revision differs from existing dataset, else False.
        """
        if revision.tombstone or revision.revision > self.revision:
            # If the revision is different from the original
            self.asset_deletion()
            if revision.revision > self.revision:
                # connect revised artifacts with published dataset
                for artifact in revision.artifacts:
                    if (
                        artifact.filetype == DatasetArtifactFileType.RDS
                        and revision.processing_status.rds_status == ConversionStatus.SKIPPED
                    ):
                        # Delete old .rds (Seurat) dataset artifact clones if rds_status for dataset revision is SKIPPED
                        DatasetAsset(artifact).delete(commit=False)
                    else:
                        artifact.dataset_id = self.id

            elif revision.tombstone:
                # tombstone
                revision.tombstone_dataset_and_delete_child_objects()

            updates = revision.to_dict(
                remove_attr=[
                    "updated_at",
                    "created_at",
                    "id",
                    "collection_id",
                    "original_id",
                    "published",
                    "revised_at",
                    "explorer_url",
                ],
                remove_relationships=True,
            )

            if revision.tombstone is not False:
                self.update(commit=False, **updates)
            else:
                # There was an update to a dataset, so update revised_at
                self.update(commit=False, **updates, revised_at=now)

            return True
        return False

def get_cxg_bucket_path(explorer_url: str) -> str:
    """Parses the S3 cellxgene bucket object prefix for all resources related to this dataset from the explorer_url"""
    object_name = urlparse(explorer_url).path.split("/", 2)[2]
    if object_name.endswith("/"):
        object_name = object_name[:-1]
    base, _ = os.path.splitext(object_name)
    return base


#!/usr/bin/env python3

import collections
import functools

class memoized:

    def __init__(self, func):
        self.func = func
        self.cache = {}

    def __call__(self, *args):

        # if args is not hashable, perform the computation
        if not isinstance(args, collections.Hashable):
            return self.func(*args)

        if args not in self.cache:
            self.cache[args] = self.func(*args)

        return self.cache[args]

    def __repr__(self):
        return self.func.__doc__

    def __get__(self, obj, objtype):
        return functools.partial(self.__call__, obj)

def xadd(a, b, mod):
    if a > mod:
        a = a % mod

    if b > mod:
        b = b % mod

    return (a + b) % mod

@memoized
def xmul(a, b, mod):
    if a > mod:
        a = a % mod

    if b > mod:
        b = b % mod

    return (a * b) % mod

@memoized
def xgcf(a, b):
    s1, s2 = 1, 0
    t1, t2 = 0, 1

    while b:
        q, r = divmod(a, b)

        a, b = b, r

        s2, s1 = s1 - q * s2, s2
        t2, t1 = t1 - q * t2, t2

    return a, s1, t1

def xdiv(a, b, mod):
    return xmul(a, xgcf(b, mod)[1], mod)

@memoized
def factorial(n, mod):
    if n == 0 or n == 1:
        return 1

    result = 1
    for i in range(n, 1, -1):
        result = xmul(result, i, mod)

    return result

@memoized
def choose_n_k(n, k, mod):
    if k > n:
        return 0

    if k == 0 or k == n:
        return 1

    result = 1
    for i in  range(0, min(k, n - k)):
        result = xdiv(xmul(result, (n - i), mod), (i + 1), mod)

    return result

# Read: compute for two, meaning compute for two islands
def compute_42(a, b, mod):
    total = 0;
    for k in range(min(a, b) + 1):
        ca = choose_n_k(a, k, mod)
        cb = choose_n_k(b, k, mod)

        total = xadd(total, xmul(xmul(ca, cb, mod), factorial(k, mod), mod), mod)

    return total

if __name__ == '__main__':
    a, b, c = list(map(int, input().split()))

    modulus = 998244353;

    total0 = compute_42(a, b, modulus);
    total1 = compute_42(a, c, modulus);
    total2 = compute_42(b, c, modulus);

    print(xmul(xmul(total0, total1, modulus), total2, modulus))


#!/usr/bin/env python
# coding: utf-8

# In[1]:

#import all libraries that will be used

from collections import Counter
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from scipy import stats
from sklearn.datasets import make_classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from imblearn.over_sampling import RandomOverSampler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.model_selection import cross_val_score
import pickle
import sys
import argparse

# In[2]:

# Data Import and Cleaning

# In[48]:

#import data
def main(input_file):
    missing_values = ["n/a","na","--", "NaN"] #list of possibles missing values
    data_raw = pd.read_csv(input_file, sep =',', na_values = missing_values)
    data_fs = data_raw.copy()

    #removing not used features
    data_fs = data_fs.drop(['index'], axis =1).copy() #droping index
    data_fs = data_fs.drop(['cp'], axis =1).copy() #droping cp

    #storing median values
    median_s = data_fs['slope'].median()
    median_c = data_fs['chol'].median()
    median_a = data_fs['age'].median()
    median_tr = data_fs['trestbps'].median()
    median_th= data_fs['thalach'].median()
    median_o = data_fs['oldpeak'].median()
    median_trf = data_fs['trf'].median()

    #filling NA values with the median
    data_fs['slope'].fillna(median_s, inplace = True)
    data_fs['chol'].fillna(median_c, inplace = True)
    data_fs['age'].fillna(median_a, inplace = True) #filling NA with the median
    data_fs['trestbps'].fillna(median_tr, inplace = True) #filling NA with the median
    data_fs['thalach'].fillna(median_th, inplace = True) #filling NA with the median
    data_fs['oldpeak'].fillna(median_o, inplace = True) #filling NA with the median
    data_fs['oldpeak'].fillna(median_trf, inplace = True) #filling NA with the median

    #Removing NA values for discrete features
    data_fs = data_fs[(data_fs['fbs'].notna())    |
                     (data_fs['restecg'].notna()) |
                     (data_fs['exang'].notna())   |
                     (data_fs['ca'].notna())      |
                     (data_fs['thal'].notna())    |
                     (data_fs['nar'].notna())     |
                     (data_fs['hc'].notna())      |
                     (data_fs['sk'].notna())       ]

    #Encoding
    enco_data = data_fs.copy()
    unique = enco_data['sex'].unique()
    enco_data['sex'] = enco_data.sex.str.upper()
    enco_data['sex'] = enco_data['sex'].apply(lambda x: unique.tolist().index(x))

    #Feature selection
    eda = enco_data.copy()
    eda = eda[['age','sex','trestbps','chol','exang','oldpeak','slope','thalach', 'trf']]

    #Outlier Removal
    df = eda.copy()
    df = df[(np.abs(stats.zscore(df[['trestbps'] +['chol'] + ['oldpeak'] + ['thalach']]) < 2.2).all(axis=1))] #outlier removal

    #Separation of Target and Features
    X = df.iloc[:, df.columns != 'sex']  #independent columns
    y = df.iloc[:,1]    #target column i.e sex

    #Prediction CSV Output
    loaded_model = pickle.load(open("rf_model.pkl", 'rb'))
    y_pred = loaded_model.predict(X)
    result = df.copy()
    result[['sex']] = y_pred
    result = result[['sex']]
    result[['sex']] = result['sex'].replace({0: 'F', 1: 'M'})
    result.to_csv('newsample_PREDICTIONS_{candidate_fullname}.csv', index = False)

if __name__ == "__main__":
   input_file = sys.argv[2]
   parent_subparser = argparse.ArgumentParser(add_help=False)
   parent_subparser.add_argument("--input_file", required=True, help="csv file")
   main(input_file)


import drivers.rpc as rpc, serial, serial.tools.list_ports, struct, sys, time, json
from operator import itemgetter
from datetime import datetime
from math import sqrt, pow

def connect_usb():
    print("\nAvailable Ports:\n")
    for port, desc, hwid in serial.tools.list_ports.comports():
        print("{} : {} [{}]".format(port, desc, hwid))
    interface = rpc.rpc_usb_vcp_master(port='COM5')
    print("")
    sys.stdout.flush()
    return interface

def sort_by_pos(params):
    params.sort(key=lambda coord: (sqrt(pow(coord[0],2) + pow(coord[1],2)), coord[1]))
    return params

def calculate_ratio(params_dict):
    c1x, c1y = params_dict['circle1_pos']
    c2x, c2y = params_dict['circle2_pos']
    c3x, c3y = params_dict['circle3_pos']
    c4x, c4y = params_dict['circle4_pos']
    print(c1x, c1y, c2x, c2y, c3x, c3y, c4x, c4y)
    pix_per_mm_ratio = ((c3x - c1x) + (c2y - c1y) + (c4x - c2x) +
                        (c4y - c3y)) / (4 * 120)
    return pix_per_mm_ratio

def calculate_platform_center(params_dict):
    c1x, c1y = params_dict['circle1_pos']
    c2x, c2y = params_dict['circle2_pos']
    c3x, c3y = params_dict['circle3_pos']
    c4x, c4y = params_dict['circle4_pos']
    half_len_x = ((c3x - c1x) + (c4x - c2x)) / 4
    half_len_y = ((c2y - c1y) + (c4y - c3y)) / 4
    center_x = ((c1x + c2x) / 2) + half_len_x
    center_y = ((c1y + c3y) / 2) + half_len_y
    return ([center_x, center_y])

def calculate_relative_rect_pos(params_dict):
    rect_cx, rect_cy = params_dict['rect_pos']
    center_x, center_y = params_dict['platform_center']
    pix_per_mm = params_dict['pixel_per_mm_ratio']
    rel_x = (rect_cx - center_x)/pix_per_mm
    rel_y = (rect_cy - center_y)/pix_per_mm
    return ([rel_x, rel_y])

def dict_dumper(params):
    buff = {
        'timestamp': params[0],
        'rect_pos': [params[1], params[2]]}
    pos_list = [[params[3], params[4]], [params[5], params[6]], [params[7], params[8]], [params[9], params[10]]]
    sorted_list = sort_by_pos(pos_list)
    buff['circle1_pos'] = sorted_list[0]
    buff['circle2_pos'] = sorted_list[1]
    buff['circle3_pos'] = sorted_list[2]
    buff['circle4_pos'] = sorted_list[3]
    buff['pixel_per_mm_ratio'] = calculate_ratio(buff)
    buff['platform_center'] = calculate_platform_center(buff)
    buff['relative_rect_pos'] = calculate_relative_rect_pos(buff)
    print(buff)
    return(buff)

def exe_watcher(interface):
    time_stamp_s = int(datetime.now().strftime('%S'))
    time_stamp_m = int(datetime.now().strftime('%M'))
    print('Sent %d %d' % (time_stamp_s, time_stamp_m))
    result = interface.call("watcher", struct.pack('<II', time_stamp_s, time_stamp_m))
    if result is not None and len(result):
        print("Checksum [A=%d, B=%d]" % struct.unpack("<II", result))

def exe_find_rectangle(interface):
    time_stamp = int(datetime.now().strftime('%H%M%S'))
    time_stamp_packed = struct.pack('<II', time_stamp, 1)
    result = interface.call("find_rectangle", time_stamp_packed)
    if result is None:
        print('None')
    if result is not None:
        print(len(result))
        params = struct.unpack("<IIIIIIIIIII", result)
        if params[0] != 0:
            return(dict_dumper(params))
        else:
            return False

def make_shot():
    interface = connect_usb()
    time_stamp = int(datetime.now().strftime('%H%M%S'))
    result = interface.call("make_shot", struct.pack('<II', time_stamp, 1))
    time.sleep(1)
    try:
        exe_restart(interface)
    except Exception:
        print('Camera has been rebooted')
        time.sleep(5)

def exe_restart(interface):
    interface.call("restart")
    interface = connect_usb()

def shot_series(interface, n_shots=3):
    data = {}
    for i in range(n_shots):
        exe_watcher(interface)
        while True:
            result = exe_find_rectangle(interface)
            if result:
                break
        data[f'Shot {i+1}'] = result
        print(f'Stage {i+1} has been completed\n')
        time.sleep(1)
    print(data)
    return(data)

def connect_to_camera(shots):
    time.sleep(2)
    result = shot_series(interface, shots)
    time.sleep(1)
    try:
        exe_restart(interface)
    except Exception:
        print('Camera has been rebooted')
        time.sleep(5)
    return result

def connect_to_camera_wo_reboot(shots):
    time.sleep(2)
    result = shot_series(interface, shots)
    time.sleep(2)
    return result

interface = connect_usb()


import nltk
from nltk.corpus import stopwords
from QueryProcessor import QueryProcessor
import spacy
import calendar
import pandas as pd
from ErrorCodes import ErrorCodes as EC
nlp = spacy.load('en_core_web_sm')

class InputParser :

    def __init__ (self):
        self.qp = QueryProcessor()
        self.teams = set(self.qp.getAllTeams())

        self.monthMap = ({month: index for index, month in enumerate(calendar.month_name) if month})
        self.monthMap.update({month: index for index, month in enumerate(calendar.month_abbr) if month})
        for x in self.monthMap:
            if len(str(self.monthMap[x])) == 1 :
                self.monthMap[x] = '0' + str(self.monthMap[x])
            else :
                self.monthMap[x] = str(self.monthMap[x])

        df2 = pd.read_csv('database/names.csv')
        df1 = pd.read_csv('database/people.csv')
        df = pd.merge(df1, df2, on = ['identifier', 'identifier'])
        df.drop(['name_x', 'identifier'], axis=1, inplace=True)
        self.playerMap = (dict (zip(df['name_y'], df['unique_name'])))

    def parseDate(self, dt):
        dt = dt[0].split()
        dt.sort()
        dt[1] = self.monthMap[dt[1]]
        return '-'.join(dt)

    def parseRequirement(self, nouns):

        required = set(['runs', 'run', 'wickets', 'wicket'])

        for nn in nouns:
            if nn in required:
                return nn

        return EC.NO_REQUIREMENT_SPECIFIED

    def properNounExtractor(self, text):
        InputProperNouns = []
        sentences = nltk.sent_tokenize(text)
        for sentence in sentences:
            words = nltk.word_tokenize(sentence)
            words = [word for word in words if word not in set(stopwords.words('english'))]
            tagged = nltk.pos_tag(words)
            for (word, tag) in tagged:
                if tag == 'NNS' or tag == 'VBZ':
                    InputProperNouns.append(word)
        return InputProperNouns

    def tag_entities(self, text):

        doc = nlp(text)
        entities = [(e.text, e.start_char, e.end_char, e.label_) for e in doc.ents]
        print(entities)
        tag = {
            'teams'         : [],
            'player'        : [],
            'event'         : [],
            'date'          : [],
            'requirement'   : []
        }

        for entity in entities:

            if entity[3] == 'PERSON':
                try:
                    tag['player'].append(self.playerMap[(entity[0])])
                except:
                    print("No such player found")
                    return EC.PLAYER_DOSENT_EXISTS
            if entity[3] == 'ORG':
                if entity[0] in self.playerMap:
                    tag['player'].append(self.playerMap[(entity[0])])
            if entity[3] == 'EVENT':
                tag['event'].append(entity[0])
            if entity[3] == 'DATE':
                tag['date'].append(entity[0])
            if entity[0] in self.teams:
                tag['teams'].append(entity[0])

        tag['requirement'] = self.parseRequirement(self.properNounExtractor(text))
        tag['date'] = self.parseDate(tag['date'])
        return tag

    def getContext(self, player, action) :
        if action == 'runs' or action == 'run':
            return 'runs_scored'
        elif action == 'wickets' or action == 'wicket':
            return 'wickets'

    def queryTypeA(self, tagEty):

        matchID = self.qp.matchByDateN2Teams(tagEty['date'], tagEty['teams'][0], tagEty['teams'][1])
        if matchID.empty:
            return EC.NO_MATCH_FOUND

        matchID = (dict (zip(matchID['id'], matchID['date'])))
        req = self.getContext(tagEty['player'][0], tagEty['requirement'])

        result = dict()
        for match in matchID:
            res = (self.qp.runWicketByPlayer(match, tagEty['player'][0], req))

            if res != EC.PLAYER_DIDNT_PLAYED:
                result[matchID[match]] = res

        return result

    def queryTypeB(self, tagEty):
        oppTeam = self.qp.teamByPlayerNoppTeam(tagEty['player'][0], tagEty['teams'][0])

        if oppTeam == EC.NO_TEAM_FOUND:
            return EC.NO_TEAM_FOUND

        tagEty['teams'].append(oppTeam)
        print(tagEty)
        return self.queryTypeA(tagEty)

    def parseQuery(self, text):

        qryRes = str()
        tagEty = self.tag_entities(text)
        # print(tagEty)

        if len(tagEty['player']) == 0:
            return EC.NO_PLAYER_FOUND

        if len(tagEty['teams']) == 0:
            return EC.NO_TEAM_FOUND

        if len(tagEty['teams']) == 2 and len(tagEty['player']) == 1:
            qryRes = self.queryTypeA(tagEty)

        if len(tagEty['teams']) == 1 and len(tagEty['player']) == 1:
            qryRes = self.queryTypeB(tagEty)

        return qryRes

if __name__ ==  "__main__":

    ip = InputParser()

    while input("Continue? ") != "no":

        query = input("Query: ")
        print(ip.parseQuery(query))


import random
import os
import time
import multiprocessing
import tqdm
import json
import sys
import cmd
import subprocess
import re
import logging

class GPU:
    def __init__(self, identifier, model, region, servers, vram, num_compute_units):
        self.identifier = identifier
        self.model = model
        self.vram = vram
        self.num_compute_units = num_compute_units
        self.region = region
        self.servers = servers
        self.is_available = True

    def allocate(self):
        if self.is_available:
            self.is_available = False
            return True
        return False

    def release(self):
        self.is_available = True

class GPUPool:
    def __init__(self, gpus):
        self.gpus = gpus

    def allocate_gpu(self, task_id):
        for gpu in self.gpus:
            if gpu.allocate():
                print(
                    f"Task {task_id} allocated to GPU {gpu.model} with {gpu.vram}MB VRAM and {gpu.num_compute_units} compute units.")
                return
        print(f"No available GPU for task {task_id}")

    def release_gpu(self, task_id):
        for gpu in self.gpus:
            if not gpu.is_available:
                gpu.release()
                print(f"Task {task_id} released GPU {gpu.model}")
                return
        print(f"Task {task_id} not found in GPU allocation")

found_server1 = []
found_server2 = []
found_server3 = []
found_server4 = []
server_instances = []

    # p = multiprocessing.Pool(3)
    # for i in range(1, 3):
    #     p.apply_async(job, args=())
    # p.close()
    # p.join()
queue = multiprocessing.Queue()

progress_queue= multiprocessing.Queue()
# aaa

class SubServers:
    def __init__(self,name,ID,GPU,weight,availiable_ram,availiable):
        self.name = name
        self.subID = ID
        self.GPU = GPU
        self.weight = weight
        self.availiable_ram = availiable_ram
        self.availiable = availiable
def fetch_server(desired_model):
    found_server = []

    for server in server_instances:
        if server.GPU == desired_model:
            found_server.append(server)

    if desired_model == '3090':
        found_server1 = found_server

    elif desired_model == 'A800':
        found_server2 = found_server

    elif desired_model == 'A100':
        found_server3 = found_server

    else:
        found_server4 = found_server

    return found_server

def job_and_update(JonLen,JobCOM,queue,process_info):

    # for i in tqdm.tqdm(range(JonLen),position=0,leave = True):
    #         time.sleep(JobCOM)

    queue.put(process_info)
    shared_pid = os.getpid()
    shared_values.append([shared_pid,shared_progress,process_info])
    for i in range(100):
        q.put(i)
        shared_progress = i
        time.sleep(3)
        #actual delay equals to JobCom

def task(selected_server,path):
    for server in server_instances:
        if selected_server == server.id:
            current_server = server
            break
    with open(path, 'r') as json_file:
        data = json.load(json_file)
    JobCOM = data[0]['complexity_level']
    JonLen = data[0]['length']
    ram = JobCOM*JonLen
    process_info = data[0]

    # 若ram不足则需重新分配
    if current_server.availiable_ram < ram:
        return

    # 定义临界值 若 可用内存小于此值则认为server不可用
    if current_server.availiable_ram < 0:
        current_server.availiable = False

    current_server.availiable_ram = current_server.availiable_ram - ram
    process = multiprocessing.Process(target=job_and_update,args=(JonLen,JobCOM,queue,process_info))
    process.start()
    process.join()

def instances():
    with open('servers.json', 'r') as json_file:
        server_data = json.load(json_file)

    for server_info in server_data:
        server = SubServers(server_info["name"], server_info["ID"], server_info["GPU"], server_info["weight"],24,True)
        server_instances.append(server)

gpus1 = [
    GPU("1", "NV RTX 3008", "1", "2", 10240, 68),
    GPU("2", "NV GTX 1660", "1", "2", 6144, 22),
    GPU("3", "AMD RX 5700 XT", "1", "2", 8192, 40),
]
gpus2 = [
    GPU("1", "NV RTX 3008", "1", "2", 10240, 68),
    GPU("2", "NV GTX 1660", "1", "2", 6144, 22),
    GPU("3", "AMD RX 5700 XT", "1", "2", 8192, 40),
]
# 创建一个拥有多个GPU的资源池
gpu_pool1 = GPUPool(gpus1)

gpu_pool2 = GPUPool(gpus2)

q = multiprocessing.Queue()

# process = multiprocessing.Process(target=worker, args=(q,))
# process.start()

# pbar = tqdm.tqdm(total=100)

# while True:

#         item = q.get(timeout=5)  # 从子进程获取进度信息
#         pbar.update(1)
#         if not process.is_alive():
#             break  # 子进程已经完成，退出循环

# pbar.close()
# process.join()

shared_values = []

def worker(pid):
    shared_pid = os.getpid()
    shared_progress

shared_progress = multiprocessing.Value('i', 0 ,lock=False)
shared_pid = multiprocessing.Value('i', 0 ,lock=False)
process = multiprocessing.Process(target=worker, args=(q,))
process.start()

pbar = tqdm.tqdm(total=100)

while True:
    if pbar.n == shared_progress:
        time.sleep(2)
        #avoid busy waiting
    pbar.n = shared_progress  # 从子进程获取进度信息
    pbar.last_print_n = shared_progress
    pbar.update(0)
    if not process.is_alive():
        break  # 子进程已经完成，退出循环

pbar.close()
process.join()

def daemon_listener(q):
    logging.basicConfig(filename="jasmine_shell_log.log",
                        level=logging.INFO, format="[%(levelname)s]: %(message)s")

    while True:
        try:
            msg = q.get()
            logging.info(msg)
            if "exit" in msg:
                break
        except Exception as e:
            logging.error(f"Error in listener: {e}")


import os
import torch
import numpy
import argparse
import random
import models

def parse_args():
    """Command-line argument parser"""
    parser = argparse.ArgumentParser(description='Fashion Recommendation System')
    parser.add_argument('--mode', type=str, choices=['train', 'test', 'run'], help='select mode (train, test, or run)')
    parser.add_argument('--dataset-path', default='./dataset', help='dataset root path (default: ./dataset)')
    parser.add_argument('--data-path', help='data path (mode : run) (default: ./dataset)')
    parser.add_argument('--inC', type=int, default=1, help='Number of input channels after reshape (default: 1)')
    parser.add_argument('--midC', type=int, default=96, help='output channel of hidden conv layer (default: 96)')
    parser.add_argument('--img-size', type=int, default=200, help='Image size for input for batch processing (default: 200)')
    parser.add_argument('--sigma', type=float, default=25, help='variance of noise (default: 0.1)')
    parser.add_argument('--batch-size', type=int, default=64, help='input batch size (default: 64)')
    parser.add_argument('--epochs', type=int, default=100, help='number of epochs to train (default: 100)')
    parser.add_argument('--learning-rate', type=float, default=0.01, help='learning rate (default: 0.01)')
    parser.add_argument('--model-path', default='./N2CModel.pht', help='save model path (default: ./N2CModel.pht)')
    parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training')
    parser.add_argument('--no-rf', action='store_true', help='Do not RotateFlip transform')
    parser.add_argument('--lpfir', action='store_true', help='Linear Phase FIR Filter or NORMAL Filter')

    return parser.parse_args()

def main():
    args = parse_args()

    seed = 1
    random.seed(seed)
    numpy.random.seed(seed)
    torch.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device('cuda' if use_cuda else 'cpu')

    if args.mode == 'train':
        param = {'device':device, 'mode':args.mode, 'model_path':args.model_path, 'dataset_path':args.dataset_path, 'inC':args.inC, 'midC':args.midC,
            'img_size':args.img_size, 'sigma':args.sigma, 'batch_size':args.batch_size, 'lr':args.learning_rate, 'lpfir':args.lpfir, 'no_rf':args.no_rf}
        model = models.N2CModel(param)
        model.train(args.epochs, args.lpfir)

    elif args.mode == 'test':
        param = {'device':device, 'mode':args.mode, 'model_path':args.model_path, 'dataset_path':args.dataset_path,
            'inC':args.inC, 'midC':args.midC, 'img_size':args.img_size, 'sigma':args.sigma, 'batch_size':args.batch_size}
        model = models.N2CModel(param)
        model.test()

    elif args.mode == 'run':
        param = {'device':device, 'mode':args.mode, 'model_path':args.model_path, 'dataset_path':args.dataset_path,
            'inC':args.inC, 'midC':args.midC, 'sigma':args.sigma, 'data_path':args.data_path}
        model = models.N2CModel(param)
        model.run()

if __name__ == '__main__':
    main()


from django.shortcuts import render,redirect
from customer.models import *
from adminpanel.models import *

def customerauthentication(data_fun):
    def check(request):
        if request.session.has_key('customer-id'):
            return data_fun(request)
        else:
            return redirect('/')
    return check

def sessioncreation(data_fun):
    def check(request):
        if request.session.has_key('customer-id'):
            customerid = 'custo-'+str(request.session['customer-id'])
            current_sessionid = request.session['guest-session']
            value = CartList.objects.filter(session=current_sessionid).values_list('productid__id')
            idlist = [i for j in value for i in j]
            if idlist:
                for item in idlist:
                    product_exist = CartList.objects.filter(session=customerid,productid__id=item)
                    if not product_exist:
                        product_details = ProductSize.objects.get(id=item)
                        value2 = CartList.objects.filter(productid__id=item).values_list('quantity','price')
                        prqty = [i for j in value2 for i in j]
                        CartList.objects.create(session=customerid,productid=product_details,quantity=prqty[0],price=prqty[1])

            request.session['customer-session'] = customerid
            return data_fun(request)
        else:
            if request.session.has_key('guest-session'):
                return data_fun(request)
            else:
                lastguest = CartList.objects.all().last()
                if lastguest:
                    lastguestid = lastguest.session
                    questid = f'guest-{int(lastguestid[6:])+1}'
                else:
                    questid = 'guest-1'
                request.session['guest-session'] = questid
                return data_fun(request)
    return check

def checkoutauthentication(function):
    def check(request):
        if request.session.has_key('customer-id'):
            sessionid = request.session['customer-session']
            cart_item = CartList.objects.filter(session=sessionid)
            if cart_item:
                value = CartList.objects.filter(session=sessionid).values_list('id')
                cartids = [i for j in value for i in j]
                price = 0
                for item in cartids:
                    pro_details = CartList.objects.get(id=item)
                    price += int(pro_details.productid.price)*(pro_details.quantity)
                value = CartList.objects.filter(session=sessionid).values_list('quantity')
                qty = [i+i for j in value for i in j]
                count = 0
                for item in qty:
                    count += item           #here creating token for unautherized access.
                tokenno = count+price       #token will delete after order placed

                check = CheckoutToken.objects.filter(customerid=sessionid,tokenno=tokenno)
                if check:
                    return function(request)
                else:
                    return redirect('/shoppingcart/')
            else:
                return redirect('/')

        else:
            return redirect('/shoppingcart/')
    return check


from __future__ import print_function, absolute_import
import os
import glob
import re
import sys
import urllib
import tarfile
import zipfile
import os.path as osp
from scipy.io import loadmat
import numpy as np
import h5py
from scipy.misc import imsave
import scipy.io as sio

from manage_data.get_density_map import create_density_map
from manage_data.utils import mkdir_if_missing, copy_to_directory
from manage_data.data_augmentation import augment

"""class that has a train and test unit"""
class train_test_unit(object):
    train_dir_img = ""
    train_dir_den = ""
    test_dir_img = ""
    test_dir_den = ""
    metadata = dict()
    def __init__(self, _train_dir_img, _train_dir_den, _test_dir_img, _test_dir_den, kwargs):
        self.train_dir_img = _train_dir_img
        self.train_dir_den = _train_dir_den
        self.test_dir_img = _test_dir_img
        self.test_dir_den = _test_dir_den
        self._check_before_run()
        self.metadata = kwargs

    def _check_before_run(self):
        """Check if all files are available before going deeper"""
        if not osp.exists(self.train_dir_img):
            raise RuntimeError("'{}' is not available".format(self.train_dir_img))
        if not osp.exists(self.train_dir_den):
            raise RuntimeError("'{}' is not available".format(self.train_dir_den))
        if not osp.exists(self.test_dir_img):
            raise RuntimeError("'{}' is not available".format(self.test_dir_img))
        if not osp.exists(self.test_dir_den):
            raise RuntimeError("'{}' is not available".format(self.test_dir_den))

    def to_string(self):
        return "_".join([ str(key) + "_" + str(value) for key, value in sorted(self.metadata.items()) if key != 'name'])

"""Dataset classes"""

"""Crowd counting dataset"""

class UCF_CC_50(object):
    root = 'data/ucf_cc_50/'
    ori_dir = osp.join(root, 'UCF_CC_50')
    ori_dir_lab = osp.join(ori_dir, 'labels')
    ori_dir_img = osp.join(ori_dir, 'images')

    ori_dir_den = osp.join(ori_dir, 'density_maps') #.npy files of density maps matrices
    augmented_dir = ""
    train_test_set = []
    signature_args = ['people_thr', 'gt_mode']
    metadata = dict()
    train_test_size = 5

    def __init__(self, force_create_den_maps = False, force_augmentation = False, **kwargs):
        self._check_before_run()
        self.metadata = kwargs
        self._create_original_density_maps(force_create_den_maps)
        self._create_train_test(force_augmentation, kwargs)

    def _create_original_density_maps(self, force_create_den_maps):
        if not osp.exists(self.ori_dir_den):
            os.makedirs(self.ori_dir_den)
        elif not force_create_den_maps:
            return
        create_density_map(self.ori_dir_img, self.ori_dir_lab, self.ori_dir_den, mode = self.metadata['gt_mode'])

    def _check_before_run(self):
        """Check if all files are available before going deeper"""
        if not osp.exists(self.root):
            raise RuntimeError("'{}' is not available".format(self.root))
        if not osp.exists(self.ori_dir):
            raise RuntimeError("'{}' is not available".format(self.ori_dir))
        if not osp.exists(self.ori_dir_img):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_img))
        if not osp.exists(self.ori_dir_lab):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_lab))

    def signature(self):
        return "_".join(["{}_{}".format(sign_elem, self.metadata[sign_elem]) for sign_elem in self.signature_args])

    def _create_train_test(self, force_augmentation, kwargs):
        slide_window_params = {'displace' : kwargs['displace'], 'size_x' : kwargs['size_x'], 'size_y' : kwargs['size_y'], 'people_thr' : kwargs['people_thr']}
        noise_params = {'augment_noise' : kwargs['augment_noise']}
        light_params = {'augment_light' : kwargs['augment_light'], 'bright' : kwargs['bright'], 'contrast' : kwargs['contrast']}

        file_names = os.listdir(self.ori_dir_img)
        file_names.sort()
        img_names = []
        img_ids = []
        for file_name in file_names:
            file_extention = file_name.split('.')[-1]
            file_id = file_name[:len(file_name) - len(file_extention)]
            if file_extention != 'png' and file_extention != 'jpg':
                continue
            img_names.append(file_name)
            img_ids.append(file_id)
        if len(img_names) != 50:
            raise RuntimeError("UCF_CC_50 dataset expects 50 images, {} found".format(len(img_names)))

        self.augmented_dir = osp.join(self.root, self.signature())
        augment_data = False
        if osp.exists(self.augmented_dir):
            print("'{}' already exists".format(self.augmented_dir))
            if force_augmentation:
                augment_data = True
                print("augmenting data anyway")
            else:
                augment_data = False
                print("will not augmenting data")
        else:
            augment_data = True
            os.makedirs(self.augmented_dir)

        #using 5 fold cross validation protocol
        for fold in range(5):
            fold_dir = osp.join(self.augmented_dir, 'fold{}'.format(fold + 1))
            aug_train_dir_img = osp.join(fold_dir, 'train_img')
            aug_train_dir_den = osp.join(fold_dir, 'train_den')
            aug_train_dir_lab = osp.join(fold_dir, 'train_lab')
            fold_test_dir_img = osp.join(fold_dir, 'test_img')
            fold_test_dir_den = osp.join(fold_dir, 'test_den')
            fold_test_dir_lab = osp.join(fold_dir, 'test_lab')

            mkdir_if_missing(aug_train_dir_img)
            mkdir_if_missing(aug_train_dir_den)
            mkdir_if_missing(aug_train_dir_lab)
            mkdir_if_missing(fold_test_dir_img)
            mkdir_if_missing(fold_test_dir_den)
            mkdir_if_missing(fold_test_dir_lab)

            kwargs['name'] = 'ucf-fold{}'.format(fold + 1)
            train_test = train_test_unit(aug_train_dir_img, aug_train_dir_den, fold_test_dir_img, fold_test_dir_den, kwargs.copy())
            self.train_test_set.append(train_test)

            if augment_data:
                test_img = img_names[fold * 10: (fold + 1) * 10]
                test_ids = img_ids[fold * 10: (fold + 1) * 10]
                test_den_paths = [osp.join(self.ori_dir_den, img_id + 'npy') for img_id in test_ids]
                test_lab_paths = [osp.join(self.ori_dir_lab, img_id + 'json') for img_id in test_ids]
                test_img_paths = [osp.join(self.ori_dir_img, img) for img in test_img]

                train_img = sorted(list(set(img_names) - set(test_img)))
                train_ids = sorted(list(set(img_ids) - set(test_ids)))
                train_den_paths = [osp.join(self.ori_dir_den, img_id + 'npy') for img_id in train_ids]
                train_lab_paths = [osp.join(self.ori_dir_lab, img_id + 'json') for img_id in train_ids]
                train_img_paths = [osp.join(self.ori_dir_img, img) for img in train_img]

                #augment train data
                print("Augmenting {}".format(kwargs['name']))
                augment(train_img_paths, train_lab_paths, train_den_paths, aug_train_dir_img, aug_train_dir_lab, aug_train_dir_den, slide_window_params, noise_params, light_params)
                copy_to_directory(test_den_paths, fold_test_dir_den)
                copy_to_directory(test_lab_paths, fold_test_dir_lab)
                copy_to_directory(test_img_paths, fold_test_dir_img)

class ShanghaiTech(object):
    root = 'data/ShanghaiTech/'
    ori_dir_partA = osp.join(root, 'part_A')
    ori_dir_partA_train = osp.join(ori_dir_partA, 'train_data')
    ori_dir_partA_train_mat = osp.join(ori_dir_partA_train, 'ground-truth')
    ori_dir_partA_train_img = osp.join(ori_dir_partA_train, 'images')
    ori_dir_partA_test = osp.join(ori_dir_partA, 'test_data')
    ori_dir_partA_test_mat = osp.join(ori_dir_partA_test, 'ground-truth')
    ori_dir_partA_test_img = osp.join(ori_dir_partA_test, 'images')

    ori_dir_partB = osp.join(root, 'part_B')
    ori_dir_partB_train = osp.join(ori_dir_partB, 'train_data')
    ori_dir_partB_train_mat = osp.join(ori_dir_partB_train, 'ground-truth')
    ori_dir_partB_train_img = osp.join(ori_dir_partB_train, 'images')
    ori_dir_partB_test = osp.join(ori_dir_partB, 'test_data')
    ori_dir_partB_test_mat = osp.join(ori_dir_partB_test, 'ground-truth')
    ori_dir_partB_test_img = osp.join(ori_dir_partB_test, 'images')

    #to be computed
    ori_dir_partA_train_lab = osp.join(ori_dir_partA_train, 'labels')
    ori_dir_partA_train_den = osp.join(ori_dir_partA_train, 'density_maps')
    ori_dir_partA_test_lab = osp.join(ori_dir_partA_test, 'labels')
    ori_dir_partA_test_den = osp.join(ori_dir_partA_test, 'density_maps')

    ori_dir_partB_train_lab = osp.join(ori_dir_partB_train, 'labels')
    ori_dir_partB_train_den = osp.join(ori_dir_partB_train, 'density_maps')
    ori_dir_partB_test_lab = osp.join(ori_dir_partB_test, 'labels')
    ori_dir_partB_test_den = osp.join(ori_dir_partB_test, 'density_maps')

    augmented_dir_partA = ""
    augmented_dir_partB = ""
    train_test_set = []
    signature_args = ['people_thr', 'gt_mode']
    metadata = dict()
    train_test_size = 2

    def __init__(self, force_create_den_maps = False, force_augmentation = False, **kwargs):
        self._check_before_run()
        self.metadata = kwargs
        self._create_labels()
        self._create_original_density_maps(force_create_den_maps)
        self._create_train_test(force_augmentation, kwargs)

    def _create_original_density_maps(self, force_create_den_maps):
        all_density_dirs_exist = osp.exists(self.ori_dir_partA_train_den)
        all_density_dirs_exist = all_density_dirs_exist and osp.exists(self.ori_dir_partA_test_den)
        all_density_dirs_exist = all_density_dirs_exist and osp.exists(self.ori_dir_partB_train_den)
        all_density_dirs_exist = all_density_dirs_exist and osp.exists(self.ori_dir_partB_test_den)
        if not all_density_dirs_exist:
            mkdir_if_missing(self.ori_dir_partA_train_den)
            mkdir_if_missing(self.ori_dir_partA_test_den)
            mkdir_if_missing(self.ori_dir_partB_train_den)
            mkdir_if_missing(self.ori_dir_partB_test_den)
        elif not force_create_den_maps:
            return
        create_density_map(self.ori_dir_partA_train_img, self.ori_dir_partA_train_lab, self.ori_dir_partA_train_den, mode = self.metadata['gt_mode'])
        create_density_map(self.ori_dir_partA_test_img, self.ori_dir_partA_test_lab, self.ori_dir_partA_test_den, mode = self.metadata['gt_mode'])
        create_density_map(self.ori_dir_partB_train_img, self.ori_dir_partB_train_lab, self.ori_dir_partB_train_den, mode = self.metadata['gt_mode'])
        create_density_map(self.ori_dir_partB_test_img, self.ori_dir_partB_test_lab, self.ori_dir_partB_test_den, mode = self.metadata['gt_mode'])

    def _check_before_run(self):
        """Check if all files are available before going deeper"""
        if not osp.exists(self.root):
            raise RuntimeError("'{}' is not available".format(self.root))
        if not osp.exists(self.ori_dir_partA):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA))
        if not osp.exists(self.ori_dir_partB):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB))
        if not osp.exists(self.ori_dir_partA_train):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_train))
        if not osp.exists(self.ori_dir_partB_train):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_train))
        if not osp.exists(self.ori_dir_partA_test):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_test))
        if not osp.exists(self.ori_dir_partB_test):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_test))
        if not osp.exists(self.ori_dir_partA_train_img):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_train_img))
        if not osp.exists(self.ori_dir_partA_train_mat):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_train_mat))
        if not osp.exists(self.ori_dir_partB_train_img):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_train_img))
        if not osp.exists(self.ori_dir_partB_train_mat):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_train_mat))
        if not osp.exists(self.ori_dir_partA_test_img):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_test_img))
        if not osp.exists(self.ori_dir_partA_test_mat):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partA_test_mat))
        if not osp.exists(self.ori_dir_partB_test_img):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_test_img))
        if not osp.exists(self.ori_dir_partB_test_mat):
            raise RuntimeError("'{}' is not available".format(self.ori_dir_partB_test_mat))

    def _json_to_string(self, array):
        """
        converts json to string specifically for shanghai tech dataset
        """
        if len(array)==0:
            return '[]'
        line = '['
        for i in range(len(array)):
            line += '{\"x\":'+str(array[i][0])+',\"y\":'+str(array[i][1])+'},'
        return line[0:len(line)-1]+']'

    def _convert_mat_to_json(self, in_dir, out_dir):
        """
        converts every .mat file in in_dir to a .json equivalent in out_dir
        """
        print("converting mat to json from {}".format(in_dir))
        file_names = os.listdir(in_dir)
        for mat_file in file_names:
            mat_file_path = osp.join(in_dir, mat_file)
            file_extention = mat_file.split('.')[-1]
            file_id = mat_file[3:len(mat_file) - len(file_extention)]
            json_file_path = osp.join(out_dir, file_id + 'json')
            labels = sio.loadmat(mat_file_path)
            labels = labels['image_info'][0][0][0][0][0]
            labels = str(self._json_to_string(labels))
            with open(json_file_path, 'w') as outfile:
                outfile.write(labels)

    def _create_labels(self):
        mkdir_if_missing(self.ori_dir_partA_train_lab)
        mkdir_if_missing(self.ori_dir_partA_test_lab)
        mkdir_if_missing(self.ori_dir_partB_train_lab)
        mkdir_if_missing(self.ori_dir_partB_test_lab)
        #check if number os files is equal
        if len(os.listdir(self.ori_dir_partA_train_mat)) != len(os.listdir(self.ori_dir_partA_train_lab)):
            self._convert_mat_to_json(self.ori_dir_partA_train_mat, self.ori_dir_partA_train_lab)
        if len(os.listdir(self.ori_dir_partA_test_mat)) != len(os.listdir(self.ori_dir_partA_test_lab)):
            self._convert_mat_to_json(self.ori_dir_partA_test_mat, self.ori_dir_partA_test_lab)
        if len(os.listdir(self.ori_dir_partB_train_mat)) != len(os.listdir(self.ori_dir_partB_train_lab)):
            self._convert_mat_to_json(self.ori_dir_partB_train_mat, self.ori_dir_partB_train_lab)
        if len(os.listdir(self.ori_dir_partB_test_mat)) != len(os.listdir(self.ori_dir_partB_test_lab)):
           self._convert_mat_to_json(self.ori_dir_partB_test_mat, self.ori_dir_partB_test_lab)

    def signature(self):
        return "_".join(["{}_{}".format(sign_elem, self.metadata[sign_elem]) for sign_elem in self.signature_args])

    def _create_train_test(self, force_augmentation, kwargs):
        slide_window_params = {'displace' : kwargs['displace'], 'size_x' : kwargs['size_x'], 'size_y' : kwargs['size_y'], 'people_thr' : kwargs['people_thr']}
        noise_params = {'augment_noise' : kwargs['augment_noise']}
        light_params = {'augment_light' : kwargs['augment_light'], 'bright' : kwargs['bright'], 'contrast' : kwargs['contrast']}

        #shanghaiTech part A
        self.augmented_dir_partA = osp.join(self.ori_dir_partA, self.signature())
        augment_data_A = False
        if osp.exists(self.augmented_dir_partA):
            print("'{}' already exists".format(self.augmented_dir_partA))
            if force_augmentation:
                augment_data_A = True
                print("augmenting data anyway")
            else:
                augment_data_A = False
                print("will not augmenting data")
        else:
            augment_data_A = True
            os.makedirs(self.augmented_dir_partA)

        aug_dir_partA_img = osp.join(self.augmented_dir_partA, "train_img")
        aug_dir_partA_den = osp.join(self.augmented_dir_partA, "train_den")
        aug_dir_partA_lab = osp.join(self.augmented_dir_partA, "train_lab")
        mkdir_if_missing(aug_dir_partA_img)
        mkdir_if_missing(aug_dir_partA_den)
        mkdir_if_missing(aug_dir_partA_lab)

        kwargs['name'] = 'shanghai-partA'
        part_A_train_test = train_test_unit(aug_dir_partA_img, aug_dir_partA_den, self.ori_dir_partA_test_img, self.ori_dir_partA_test_den, kwargs.copy())
        self.train_test_set.append(part_A_train_test)

        if augment_data_A:
            ori_img_paths = [osp.join(self.ori_dir_partA_train_img, file_name) for file_name in sorted(os.listdir(self.ori_dir_partA_train_img))]
            ori_lab_paths = [osp.join(self.ori_dir_partA_train_lab, file_name) for file_name in sorted(os.listdir(self.ori_dir_partA_train_lab))]
            ori_den_paths = [osp.join(self.ori_dir_partA_train_den, file_name) for file_name in sorted(os.listdir(self.ori_dir_partA_train_den))]
            augment(ori_img_paths, ori_lab_paths, ori_den_paths, aug_dir_partA_img, aug_dir_partA_lab, aug_dir_partA_den, slide_window_params, noise_params, light_params)

        #shanghaiTech part B
        self.augmented_dir_partB = osp.join(self.ori_dir_partB, self.signature())
        augment_data_B = False
        if osp.exists(self.augmented_dir_partB):
            print("'{}' already exists".format(self.augmented_dir_partB))
            if force_augmentation:
                augment_data_B = True
                print("augmenting data anyway")
            else:
                augment_data_B = False
                print("will not augmenting data")
        else:
            augment_data_B = True
            os.makedirs(self.augmented_dir_partB)

        aug_dir_partB_img = osp.join(self.augmented_dir_partB, "train_img")
        aug_dir_partB_den = osp.join(self.augmented_dir_partB, "train_den")
        aug_dir_partB_lab = osp.join(self.augmented_dir_partB, "train_lab")
        mkdir_if_missing(aug_dir_partB_img)
        mkdir_if_missing(aug_dir_partB_den)
        mkdir_if_missing(aug_dir_partB_lab)

        kwargs['name'] = 'shanghai-partB'
        part_B_train_test = train_test_unit(aug_dir_partB_img, aug_dir_partB_den, self.ori_dir_partB_test_img, self.ori_dir_partB_test_den, kwargs.copy())
        self.train_test_set.append(part_B_train_test)

        if augment_data_B:
            ori_img_paths = [osp.join(self.ori_dir_partB_train_img, file_name) for file_name in sorted(os.listdir(self.ori_dir_partB_train_img))]
            ori_lab_paths = [osp.join(self.ori_dir_partB_train_lab, file_name) for file_name in sorted(os.listdir(self.ori_dir_partB_train_lab))]
            ori_den_paths = [osp.join(self.ori_dir_partB_train_den, file_name) for file_name in sorted(os.listdir(self.ori_dir_partB_train_den))]
            augment(ori_img_paths, ori_lab_paths, ori_den_paths, aug_dir_partB_img, aug_dir_partB_lab, aug_dir_partB_den, slide_window_params, noise_params, light_params)

"""Create dataset"""

__factory = {
    'ucf-cc-50': UCF_CC_50,
    'shanghai-tech': ShanghaiTech
}

def get_names():
    return __factory.keys()

def init_dataset(name, force_create_den_maps = False, force_augmentation = False, **kwargs):
    if name not in __factory.keys():
        raise KeyError("Unknown dataset: {}".format(name))
    return __factory[name](force_create_den_maps, force_augmentation, **kwargs)

if __name__ == '__main__':
    pass


import detectron2
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog

import cv2
import numpy as np

import pickle

BBOX_SCORE_THRESH = .5

class VehicleDetectorDetectron:
  wantsRGB = False
  def __init__(self):
    cfg = get_cfg()

    #cfg.MODEL.DEVICE = 'cpu'

    # See https://github.com/facebookresearch/detectron2/blob/master/MODEL_ZOO.md
    cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"))
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml")
    self.predictor = DefaultPredictor(cfg)

  # Returns torch.tensor of bounding boxes for frame
  def getFeatures(self, frame):
    vehicleFeatures = self.predictor(frame)
    # See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
    instances = vehicleFeatures['instances']

    keepScores = instances.scores > BBOX_SCORE_THRESH

    boxes = instances.pred_boxes[keepScores]
    scores = instances.scores[keepScores]
    classes = instances.pred_classes[keepScores]
    masks = instances.pred_masks[keepScores]

    # envelopes = self._getEnvelopes(masks)
    envelopes = []

    boxes = boxes.tensor.cpu().numpy()
    boxes = [boxes[i] for i in range(len(boxes))]
    classes = classes.cpu().numpy()
    classes = [classes[i] for i in range(len(classes))]
    return boxes, envelopes, scores

  def _getEnvelopes(self, masks):
    envelopes = []
    for mask in masks:
      contour, hierarchy = cv2.findContours(np.uint8(mask.cpu().numpy()), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      envelopes.append(contour)
    return envelopes


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="import-everything",
    version="0.0.1",
    author="Nikola Bebić",
    author_email="nikola.bebic99@gmail.com",
    description="A module containing everything",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/profMagija/everything",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: BSD License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


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

from matplotlib import pyplot as plt
import numpy as np
import cv2
from skimage.morphology import medial_axis
from skimage.morphology import skeletonize
from scipy import ndimage
import pymorph as m
from shapely.geometry import Point
from util import disegna as dsg
from scipy import ndimage

def flip_vertici(vertici_porte, altezza):
	for v in vertici_porte:
		v[1] = altezza - v[1]
	return vertici_porte

def isolato(immagine,riga,col):
	for r in xrange(max(0,riga-1),min(immagine.shape[0],riga+2)):
		for c in xrange(max(0,col-1),min(immagine.shape[1],col+2)):
			if (immagine[r][c]==True) and not (r==riga and c==col):
				return False
	return True

def get_distance_transforme(im_bw):
	'''
	restituisce la distance transforme
	'''
	distanceMap = ndimage.distance_transform_edt(im_bw)
	return distanceMap

def medial_points(thresh):
	#leggo l'immagine e la trasformo in 0 o 255

	(th, im_bw) = cv2.threshold(thresh, 240, 255, cv2.THRESH_BINARY_INV)
	#plt.imshow(cv2.cvtColor(im_bw,cv2.COLOR_GRAY2RGB))
	#plt.show()

	#points = punti dei muri
	points = []
	for y in xrange(0,im_bw.shape[1]):
		for x in xrange(im_bw.shape[0]):
			if im_bw[x, y] == (0):
				points.append([y,x])

	points = np.asarray(points)

	#distance transform
# 	distanceMap = ndimage.distance_transform_edt(im_bw)
# 	plt.title('8.distance map')
# 	plt.imshow(distanceMap)
# 	plt.show()
	#disegno distance transforme

	#distanceMap = dsg.disegna_distance_transform(im_bw, filepath = filepath)#plot
	distanceMap = get_distance_transforme(im_bw)

	#medial axis sulla distance transform
	skel, distance = medial_axis(distanceMap, return_distance=True)

	#elimino parte delle imprecisioni della skeletonization
	#b3 = m.thin(skel, m.endpoints('homotopic'), 15)
	b3 = m.thin(skel, m.endpoints('homotopic'), 500)

	'''
	#------
	import copy
	b4 = copy.copy(b3)
	#ottengo solo critical points
	#critical_points = []
	for riga in xrange(b4.shape[0]):
		for col in xrange(b4.shape[1]):
			if b4[riga][col]==True:
				#controllo se in un intorno di quel punto c'è una frontiera
				#creo intorno del pixel
				start_x = riga-17 #15
				end_x = riga+17
				start_y = col-17
				end_y = col+17
				count = 0
				for x in xrange(start_x,end_x):
					for y in xrange(start_y, end_y):
						if im_bw[x, y] <= (200): #se almeno una componente rgb di un pixel nell'introno del pixel cha fa parte del medial axis e' un pixel della frontiera
							count+=1
				if count <= 5:
					b4[riga][col]=False
				# else:
# 					#è un critical_points
# 					critical_points.append([riga, col])

	critical_points = []
	#salvo in una lista i critical points che fanno parte del medial axis
	for riga in xrange(b4.shape[0]):
		for col in xrange(b4.shape[1]):
			if (b4[riga][col]==True):
				critical_points.append((col,riga))
	critical_points = np.asarray(critical_points)
	flip_vertici(critical_points,b4.shape[0]-1)
	#------
	'''

	#elimino i punti isolati del medial axis, sono rumore
	for riga in xrange(b3.shape[0]):
		for col in xrange(b3.shape[1]):
			if b3[riga][col]==True and isolato(b3,riga,col):
				b3[riga][col]=False

	#disegno medial axis
# 	plt.title('9.medial axis')
# 	plt.plot(points[:,0],points[:,1],'.')
# 	plt.imshow(b3,cmap='Greys')
# 	plt.show()
	#disegno medial axis
	#dsg.disegna_medial_axis(points, b3, filepath = filepath)#quiiiiiiiii

	punti_medial = []
	#salvo in una lista i punti che fanno parte del medial axis
	for riga in xrange(b3.shape[0]):
		for col in xrange(b3.shape[1]):
			if (b3[riga][col]==True):
				punti_medial.append((col,riga))
	punti_medial = np.asarray(punti_medial)
	flip_vertici(punti_medial,b3.shape[0]-1)

	return punti_medial, distanceMap, points, b3, distance

def trovaCollegamenti(stanze,stanze_collegate,punti_medial):
	'''
Date le stanze e i punti del medial axis, trova i punti che confinano con più di una stanza, e restituisce la lista di coppie di stanze collegate e la lista di coordinate dei punti/porte.
	'''
	porte_colleganti = []
	for p in punti_medial:
		a = 0
		temp = []
		for index,s in enumerate(stanze):
			if (s.boundary).distance(Point(p[0],p[1])) < 1: #se il bordo della stanza dista meno di 1 dal punto.
				a+=1
				if not (s in temp): #se la stanza non è già stata aggiunta la aggiungo.
					temp.append(index)
		temp.sort()
		#se la coppia non è già in stanze_porte, la aggiungo.
		if not (temp in stanze_collegate) and (len(temp)==2):
			stanze_collegate.append(temp)
			porte_colleganti.append(Point(p[0],p[1]))
		if (len(temp)>2):
			for index,a in enumerate(temp):
				for b in temp[index+1:]:
					tmp = []
					tmp.extend((a,b))
					if not (tmp in stanze_collegate):
						stanze_collegate.append(tmp)
						porte_colleganti.append(Point(p[0],p[1]))

	#print(stanze_collegate)
	return stanze_collegate, porte_colleganti

def critical_points(metricMap):
	'''
	ottengo i critical points dal medial axis
	'''

	if cv2.__version__[0] == '3' :
		flag = cv2.IMREAD_GRAYSCALE
	elif cv2.__version__[0] == '2' :
		flag = cv2.CV_LOAD_IMAGE_GRAYSCALE
	else :
		raise EnvironmentError('Opencv Version Error. You should have OpenCv 2.* or 3.*')

	im_gray = cv2.imread(metricMap, flag)
	#leggo l'immagine e la trasformo in 0 o 255
	(thresh, im_bw) = cv2.threshold(im_gray, 240, 255, cv2.THRESH_BINARY_INV)

	(th, im_bw) = cv2.threshold(im_bw, 240, 255, cv2.THRESH_BINARY_INV)

	#points = punti dei muri
	points = []
	for y in xrange(0,im_bw.shape[1]):
		for x in xrange(im_bw.shape[0]):
			if im_bw[x, y] == (0):
				points.append([y,x])

	points = np.asarray(points)

	#distance map
	distanceMap = get_distance_transforme(im_bw)

	#medial axis sulla distance transform
	skel, distance = medial_axis(distanceMap, return_distance=True)

	#elimino parte delle imprecisioni della skeletonization
	#b3 = m.thin(skel, m.endpoints('homotopic'), 15)
	b3 = m.thin(skel, m.endpoints('homotopic'), 500)

	#---
	import copy
	b4 = copy.copy(b3)
	#ottengo solo critical points
	#critical_points = []
	for riga in xrange(b4.shape[0]):
		for col in xrange(b4.shape[1]):
			if b4[riga][col]==True:
				#controllo se in un intorno di quel punto c'è una frontiera
				#creo intorno del pixel
				start_x = riga-17 #15
				end_x = riga+17
				start_y = col-17
				end_y = col+17
				count = 0
				for x in xrange(start_x,end_x):
					for y in xrange(start_y, end_y):
						if im_bw[x, y] <= (200): #se almeno una componente rgb di un pixel nell'introno del pixel cha fa parte del medial axis e' un pixel della frontiera
							count+=1
				if count <= 5:
					b4[riga][col]=False
				# else:
# 					#è un critical_points
# 					critical_points.append([riga, col])

	critical_points = []
	#salvo in una lista i critical points che fanno parte del medial axis
	for riga in xrange(b4.shape[0]):
		for col in xrange(b4.shape[1]):
			if (b4[riga][col]==True):
				critical_points.append((col,riga))
	critical_points = np.asarray(critical_points)
	flip_vertici(critical_points,b4.shape[0]-1)
	#------

	return distanceMap,points, b3, b4, critical_points


import networkx as nx
from matplotlib import pyplot as plt
def players_to_graph(evaluations):
    g = nx.Graph()
    agents = []
    for roomIndex, room in enumerate(evaluations):
        g.add_node((roomIndex, 'r'), bipartite=1)
        agents.append((roomIndex, 'r'))

    for playerIndex, player in enumerate(evaluations[0]):
        g.add_node((playerIndex, 'p'),  bipartite=0)

    for roomIndex, room in enumerate(evaluations):
        for playerIndex, playerValue in enumerate(room):
            g.add_edge((playerIndex, 'p'), (roomIndex, 'r'), weight=playerValue)

    return g, agents

def draw_graph(g, agents):
    pos1 = nx.bipartite_layout(g, agents)
    edge_labels = nx.get_edge_attributes(g, "weight")
    nx.draw_networkx_edge_labels(g, pos=pos1,edge_labels=edge_labels, label_pos=0.2)
    nx.draw_networkx(g, pos=pos1)
    plt.show()

def powerset(iterable):
    from itertools import chain, combinations
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range((len(s)+1)))

def brute_force_max_matching(g: nx.Graph(), agents):
    # find a max weight matching and sum the weight of this max weight matching
    res = nx.max_weight_matching(g)
    sumTarget = 0
    for edge in res:
        data = g.get_edge_data(edge[0], edge[1])
        sumTarget += data['weight']
    results = []

    # brute force over all the matchings, if the sum of weight equalts to sumTarget, append the result to a list
    for i in powerset(g.edges()):
        if len(i) != len(agents) or not nx.is_matching(g, i):
            continue
        sum = 0
        for edge in i:
            data = g.get_edge_data(edge[0], edge[1])
            sum += data['weight']
        if sum == sumTarget:
            results.append(i)
    return results

v = [[35,40,25],
[35,60,40],
[25,40,20]]

g, agents = players_to_graph(v)
#draw_graph(g, agents)

results = brute_force_max_matching(g, agents)
print(results)
z = 0
maxMin = -1
for i in results:
    currMin = 999999
    for edge in i:
        currWeight = g.get_edge_data(edge[0], edge[1])['weight']
        if currWeight < currMin:
            currMin = currWeight
    if currMin > maxMin:
        maxMin = currMin
        z = i
print(z)


#!/usr/bin/env python3

import sys, os, urllib

from bs4 import BeautifulSoup

url             = sys.argv[1]
directory       = sys.argv[2]

content         = urllib.request.urlopen(url).read()
soup            = BeautifulSoup(content, 'html.parser')

table = soup.find('table', attrs={'class':'contenttable'})

for table_entry in table.tbody.find_all('tr'):
    for a in table_entry.td.find_all('a'):

        link = a['href']
        filename = directory + '/' + os.path.basename(link)

        urllib.request.urlretrieve(link, filename)


import pygame
import time
import random
from pygame.locals import *

# start Py_Game
pygame.init()

# load a music
crash_sound = pygame.mixer.Sound("Crash1.wav")
pygame.mixer.music.load("Action_Sport_Rock_Trailer .mp3")

# init game display size
display_width = 800
display_height = 600

# init colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
persian_green = (0, 166, 147)

car_image = pygame.image.load('redCar.png')
car_width = 45
game_display = pygame.display.set_mode((display_width, display_height))
# make a Caption 2
pygame.display.set_caption('Race Car')
# make a frame per second
clock = pygame.time.Clock()

def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()
        # game_display.fill(white)
        game_display.blit(pygame.image.load("img.png"), (0, 0))
        large_text = pygame.font.Font('freesansbold.ttf', 90)
        text_surf, text_rect = text_objects("Let's Play Game", large_text, red)
        text_rect.center = ((display_width / 2), (display_height / 5))
        game_display.blit(text_surf, text_rect)

        button("Play", 150, 450, 100, 50, white, persian_green, "play")         # make start button
        button("Quit", 550, 450, 100, 50, red, persian_green, "quit")           # make quit button

        pygame.display.update()

def button(message, x, y, width, height, inactive_color, active_color, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + width > mouse[0] > x and y + height > mouse[1] > y:
        pygame.draw.rect(game_display, active_color, (x, y, width, height))
        if click[0] == 1:
            if action == "play":
                game_loop()
            elif action == "quit":
                quit_game()
    else:
        pygame.draw.rect(game_display, inactive_color, (x, y, width, height))

    small_text = pygame.font.Font("freesansbold.ttf", 20)
    text_surf, text_rect = text_objects(message, small_text, black)
    text_rect.center = ((x + (width / 2)), (y + (height / 2)))
    game_display.blit(text_surf, text_rect)

def quit_game():
    pygame.quit()
    quit()

# display score
def score(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Score: " + str(count), True, red)
    game_display.blit(text, (0, 0))

# init stuff
def stuff_rect(x, y, width, height, my_color):
    pygame.draw.rect(game_display, my_color, [x, y, width, height])

# put a car in the middle
def car(x, y):
    game_display.blit(car_image, (x, y))

def text_objects(text, font, txt_color):
    text_surface = font.render(text, True, txt_color)
    text_rect = text_surface.get_rect()
    return text_surface, text_rect

def message_display(text):
    large_text = pygame.font.Font('freesansbold.ttf', 80)
    text_surf, text_rect = text_objects(text, large_text, black)
    text_rect.center = ((display_width/2), (display_height/2))
    game_display.blit(text_surf, text_rect)
    pygame.display.update()

    time.sleep(2)
    game_loop()

def crash():

    # stop music
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(crash_sound)
    large_text = pygame.font.Font('freesansbold.ttf', 80)
    text_surf, text_rect = text_objects("YOU CRASHED", large_text, white)
    text_rect.center = ((display_width / 2), (display_height / 2))
    game_display.blit(pygame.image.load("crash.jpg"), (0, 20))
    game_display.blit(text_surf, text_rect)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()
        button("Try Again", 150, 450, 100, 50, white, persian_green, "play")  # make start button
        button("Quit", 550, 450, 100, 50, red, persian_green, "quit")  # make quit button
        pygame.display.update()

def game_loop():
    # music load
    pygame.mixer.music.play(-1)
    # init car location
    y = (display_height * 0.8)
    x = (display_width * 0.45)
    # get a car location change
    x_change = 0

    stuff_start_x = random.randrange(0, display_width)
    stuff_start_y = -600
    circle_start_x = random.randrange(0, display_width)
    circle_start_y = random.randrange(500, 1000) * -1
    circle_radius = 25
    stuff_speed = 7
    stuff_width = 100
    stuff_height = 100
    stuff_color = red
    dodged = 0

    game_exit = False
    # get events until quit
    while not game_exit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_exit = True
                quit_game()
            # get a key to move
            keys = pygame.key.get_pressed()
            if keys[K_a] or keys[K_LEFT] and not keys[K_d]:
                x_change -= 5
            if keys[K_d] or keys[K_RIGHT] and not keys[K_a]:
                x_change += 5
            if event.type == pygame.KEYUP and (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):
                x_change = 0
            if event.type == pygame.KEYUP and (event.key == pygame.K_a or event.key == pygame.K_d):
                x_change = 0
            if keys[K_UP]:
                x_change = 0

        x += x_change

        game_display.fill(white)
        # game_display.blit(pygame.image.load("disp.jpg"), (0, 0))

        # x, y, width, height, color
        stuff_rect(stuff_start_x, stuff_start_y, stuff_width, stuff_height, stuff_color)
        if dodged <= 5:
            circle_start_x = display_width
        if dodged > 5:
            pygame.draw.circle(game_display, black, (circle_start_x, circle_start_y), circle_radius)
            if circle_start_y > display_height:
                circle_start_y = 0 - circle_radius
                circle_start_x = random.randrange(0, display_width - 100)
                dodged += 1
                if dodged % 5 == 0:
                    circle_radius += 5
        stuff_start_y += stuff_speed
        circle_start_y += stuff_speed

        car(x, y)

        # Repeat stuff
        if stuff_start_y > display_height:
            stuff_start_y = 0 - stuff_height
            stuff_start_x = random.randrange(0, display_width - 100)
            dodged += 1
            if dodged % 5 == 0:
                stuff_speed += 1
                stuff_width += 5

        # Display player score
        score(dodged)
        stuff_location = stuff_start_x + stuff_width
        if y < stuff_start_y + stuff_height:
            if stuff_start_x < x < stuff_location or stuff_start_x < x + car_width < stuff_location:
                crash()
        circle_location = circle_start_x + circle_radius
        if y < circle_start_y + 25:
            if circle_start_x < x < circle_location or circle_start_x < x + car_width < circle_location:
                crash()

        # crash if impact with wall
        if x > display_width - car_width or x < 0:
            crash()

        pygame.display.update()     # display what happens in underground
        clock.tick(60)

game_intro()
game_loop()
quit_game()


import numpy as np
import torch
from typing import Optional, Dict, Iterable

def torch_one_hot(target: torch.Tensor, num_classes: Optional[int] = None) -> torch.Tensor:
    """source: https://github.com/PhoenixDL/rising. Computes one-hot encoding of input tensor.

    Args:
        target (torch.Tensor): tensor to be converted
        num_classes (Optional[int], optional): number of classes. If None, the maximum value of target is used.

    Returns:
        torch.Tensor: one-hot encoded tensor of the target
    """
    if num_classes is None:
        num_classes = int(target.max().detach().item() + 1)
    dtype, device = target.dtype, target.device
    target_onehot = torch.zeros(*target.shape, num_classes, dtype=dtype, device=device)
    return target_onehot.scatter_(1, target.unsqueeze_(1), 1.0)

def one_hot_batch(target: torch.Tensor,
                  num_classes: Optional[int] = None,
                  dtype: Optional[torch.dtype] = None) -> torch.Tensor:
    """Source: https://github.com/PhoenixDL/rising
    Computes one hot for the input tensor (assumed to a be batch and thus saved
    into first dimension -> input should only have one channel).

    Args:
        target (torch.Tensor): long tensor to be converted.
        num_classes (Optional[int], optional): number of classes. If None, the maximum of target is used.
        dtype (Optional[torch.dtype], optional): optionally changes the dtype of the onehot encoding.

    Raises:
        TypeError: when the input tensor is not of long type.
    Returns:
        torch.Tensor: one hot encoded tensor
    """
    if target.dtype != torch.long:
        raise TypeError(
            f"Target tensor needs to be of type torch.long, found {target.dtype}")

    if target.ndim in [0, 1]:
        return torch_one_hot(target, num_classes)
    else:
        if num_classes is None:
            num_classes = int(target.max().detach().item() + 1)
        _dtype, device, shape = target.dtype, target.device, target.shape
        if dtype is None:
            dtype = _dtype
        target_onehot = torch.zeros(shape[0], num_classes, *shape[2:],
                                    dtype=dtype, device=device)
        return target_onehot.scatter_(1, target, 1.0)

def reduce(tensor: torch.Tensor, reduction: str) -> torch.Tensor:
    """Reduces the given tensor using a specific criterion.

    Args:
        tensor (torch.Tensor): input tensor
        reduction (str): string with fixed values [elementwise_mean, none, sum]

    Raises:
        ValueError: when the reduction is not supported

    Returns:
        torch.Tensor: reduced tensor, or the tensor itself
    """
    if reduction in ("elementwise_mean", "mean"):
        return torch.mean(tensor)
    if reduction == 'sum':
        return torch.sum(tensor)
    if reduction is None or reduction == 'none':
        return tensor
    raise ValueError('Reduction parameter unknown.')

def mask_to_rgb(mask: np.ndarray, palette: Dict[int, Iterable]) -> np.ndarray:
    """Given an input batch, or single picture with dimensions [B, H, W] or [H, W], the utility generates
    an equivalent [B, H, W, 3] or [H, W, 3] array corresponding to an RGB version.
    The conversion uses the given palette, which should be provided as simple dictionary of indices and tuples, lists
    or arrays indicating a single RGB color. (e.g. {0: (255, 255, 255)})

    Args:
        mask (np.ndarray): input mask of indices. Each index should be present in the palette
        palette (Dict[int, Iterable]): dictionary of pairs <index - color>, where colors can be provided in RGB tuple format

    Returns:
        np.ndarray: tensor containing the RGB version of the input index tensor
    """
    lut = np.zeros((256, 3), dtype=np.uint8)
    for index, color in palette.items():
        lut[index] = np.array(color, dtype=np.uint8)
    return lut[mask]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render, HttpResponse
from django.shortcuts import get_object_or_404

from django.views.generic import CreateView, ListView, UpdateView
from django.views.generic.edit import ModelFormMixin, FormMixin, DeleteView, UpdateView
from django.core.urlresolvers import reverse, reverse_lazy
from vsa.genericviews import *
from .models import *
from .forms import *

from .models import User_Host
from .tasks import *
from django.contrib.auth.decorators import login_required
from .forms import *
from vsa.genericviews import GenericUpdateView

# Create your views here.

def user_add_perm(request):
    form = PermForm()
    if request.method == "POST":
        form = PermForm(request)
        return render(request, 'user_add_perm.html', locals())
    return render(request, 'user_add_perm.html', locals())

@login_required()
def operate_user_perm(request, pk):
    perm = get_object_or_404(User_Host, pk=pk)
    username = perm.user.username
    for host in list_user_hosts(perm_id=perm.id):
        add_perm.delay(username=username, ip=host)
    return HttpResponse("<script> alert('" + u'主机授权中!' + "');window.location.href='/permission/perm'</script>")

@login_required()
def user_perm_info(request, pk):
    user_perm = get_object_or_404(User_Host, pk=pk)
    return render(request, 'users_perm.html', locals())

@login_required()
def refresh_user_perm(request, pk):
    perm = get_object_or_404(User_Host, pk=pk)
    x = refresh_key.delay(perm.id)
    return HttpResponse("<script> alert('" + u'授权刷新中!' + "');window.location.href='/permission/perm'</script>")

@login_required()
def refresh_host_key(request, pk):
    perm = get_object_or_404(User_Host, pk=pk)
    username = perm.user.username
    for host in list_user_hosts(perm_id=perm.id):
        SyncKey.delay(username=username, ip=host)
    return HttpResponse("<script> alert('" + u'授权刷新中!' + "');window.location.href='/permission/perm'</script>")

class userpermupdateview(GenericUpdateView):
    template_name = 'userpermupdate.html'

    def post(self, request, *args, **kwargs):
        perm = request.POST.get()

@login_required()
def multadduserperm(request):
    '''
    多用户，多机器同时授权
    :param request:
    :return:
    '''
    form = MuitiAddPerm()
    if request.method == 'POST':
        form = MuitiAddPerm(request.POST)
        if form.is_valid():
            hostlist = Host.objects.filter(pk__in=request.POST.getlist('hosts'))
            for userid in request.POST.getlist('userlist'):
                perm, status = User_Host.objects.get_or_create(user_id=userid)
                for h in hostlist:
                    perm.host.add(h)
                    add_perm.delay(username=perm.user.username, ip=h.ip)
            return HttpResponse("<script> alert('" + u'批量授权中!' + "');window.location.href='/permission/perm'</script>")
    return render(request, 'multadduserperm.html', locals())

class UserPermIndexView(ListView):
    model = User
    template_name = 'userpermindex.html'

    # def get_context_data(self, **kwargs):
    #     context = super(UserPermIndexView,self).get_context_data(**kwargs)
    #     return  context

class UserPermListView(ListView):
    model = UserPerm
    template_name = 'userpermlist.html'

    def get_context_data(self, **kwargs):
        context = super(UserPermListView, self).get_context_data(**kwargs)
        userid = self.kwargs.get('userid', None)
        if userid:
            self.user = User.objects.get(id=userid)
            context['user'] = self.user
        return context

    def get_queryset(self):
        user_id = self.kwargs.get('userid', None)
        if user_id:
            queryset = self.model.objects.filter(user_id=self.kwargs['userid'])
        else:
            queryset = self.model.objects.all()
        return queryset

def remote_add_perm(request, pk):
    perm = get_object_or_404(UserPerm, pk=pk)
    add_perm.delay(perm.user.username, perm.host.ip)
    return HttpResponse("<script> alert('" + u'授权中!' + "');window.location.href=''</script>")

class PermCreateView(SuperuserRequiredMixin, CreateView):
    # model = Records
    form_class = UserPermForm
    template_name = 'userpermform.html'
    success_url = reverse_lazy('userpermlist')

    def get_initial(self):
        userid = self.kwargs.get('userid', None)
        if userid:
            self.user = get_object_or_404(UserPerm, pk=userid)
            return {'user': self.user}

    def get_context_data(self, **kwargs):
        context = super(PermCreateView, self).get_context_data(**kwargs)
        context['zone'] = self.user
        context['success_url'] = reverse('userpermlist', kwargs={'pk': self.user.id})

        return context

    def get_success_url(self):
        return reverse_lazy('userpermlist', kwargs={'pk': self.user.id})

@login_required()
def add_permission(request):
    '''
    多用户，多机器同时授权
    :param request:
    :return:
    '''
    form = UserPermform1()
    if request.method == 'POST':
        form = UserPermform1(request.POST)
        if form.is_valid():
            hostlist = virtualhost.objects.filter(pk__in=request.POST.getlist('hosts'))
            for userid in request.POST.getlist('userlist'):
                for h in hostlist:
                    perm, status = UserPerm.objects.get_or_create(user_id=userid, host=h)
                    add_perm.delay(username=perm.user.username, ip=h.ip)

            return HttpResponse("<script> alert('" + '授权中!' + "');window.location.href='/permission/index'</script>")
    return render(request, 'multadduserperm.html', locals())

def multi_add_permission(request):
    pass