import numpy as np
from scipy import stats

import lib_detect as lib

def calculate_fdr_threshold_vector(chrom, cfg, **kwargs):
    '''
    Compute vector of FDR-controlling detection thresholds by region.

    Parameters
    ----------
        - chrom : int
            Index of chromosome to analyze
        - cfg : dictionary
            Dictionary of configuration information, formatted as documented
            elsewhere.
        - **kwargs
            Optional arguments to avoid reloading data

    Returns
    -------
        Dictionary containing:
        - thresh_vec : ndarray
            Vector of detection thresholds by region
        - region_ids : integer ndarray
            Vector of region identifiers
        - alpha : float
            FDR
    '''
    # Extract commonly-used data from cfg
    alpha       = cfg['detection_params']['alpha']
    n_proc      = cfg['detection_params']['n_proc']
    maxima      = cfg['detection_params']['compute_maxima_only']
    method      = cfg['detection_params']['method_fdr']
    verbose     = cfg['detection_params']['verbose']

    # Load null coefficients
    if 'null' in kwargs.keys():
        null = kwargs['null']
    else:
        null_path = cfg['estimation_output']['null_coef_pattern']
        null_path = null_path.format(**cfg).strip()
        null_path = null_path % chrom
        null = np.loadtxt(null_path)

    # Load nonnull coefficients
    if 'nonnull' in kwargs.keys():
        nonnull = kwargs['nonnull']
    else:
        nonnull_path = cfg['estimation_output']['coef_pattern']
        nonnull_path = nonnull_path.format(**cfg).strip()
        nonnull_path = nonnull_path % chrom
        nonnull = np.loadtxt(nonnull_path)

    # Load region type information
    if 'region_types' in kwargs.keys() and 'region_ids' in kwargs.keys():
        region_types = kwargs['region_types']
        region_ids = kwargs['region_ids']
    else:
        with open(cfg['data']['regions_path'], 'rb') as f:
            lines_read = 0
            for line in f:
                lines_read += 1
                if lines_read == chrom:
                    region_types = np.fromstring(line.strip(), sep=' ',
                                                 dtype=int)
                    break

        region_types = region_types[:null.size]
        region_types -= region_types.min()
        region_ids = np.unique(region_types)

    if 'region_list' in kwargs.keys() and 'region_lengths' in kwargs.keys():
        region_list = kwargs['region_list']
        region_lengths = kwargs['region_lengths']
    else:
        region_list = []
        region_lengths = []
        for id in region_ids:
            region_list.append( np.where(region_types==id)[0] )
            region_lengths.append( np.sum(region_types==id) )

        region_lengths = np.array(region_lengths)

    # Calculate threshold for given FDR
    if method.lower() == 'bh':
        thresh_vec = lib.get_fdr_threshold_bh(null=null, nonnull=nonnull,
                                              region_list=region_list,
                                              alpha=alpha, maxima=maxima,
                                              n_proc=n_proc, verbose=verbose)
    elif method.lower() == 'direct':
        thresh_vec = lib.get_fdr_threshold_estimate(null, nonnull, region_list,
                                                    alpha, maxima=maxima,
                                                    n_proc=n_proc,
                                                    verbose=verbose)
    else:
        thresh_vec = lib.get_fdr_threshold(null, nonnull, region_list, alpha,
                                           maxima=maxima)

    result = {'thresh_vec' : thresh_vec,
              'region_ids' : region_ids,
              'alpha' : alpha}

    return result

def write_fdr_thresholds(result, cfg, chrom=1):
    '''
    Output FDR threshold vector to appropriate file.

    Parameters
    ----------
        - result : ndarray
            Dictionary as returned by calculate_fdr_threshold_vector.
        - cfg : dictionary
            Dictionary containing at least detection_output section wth
            appropriate parameters.

    Returns
    -------
        None
    '''
    # Output detection threshold by region to appropriate path
    out_path = cfg['detection_output']['fdr_pattern'].format(**cfg).strip()
    out_path = out_path % chrom

    n_regions = result['region_ids'].size

    np.savetxt(out_path, np.vstack((result['alpha']*np.ones(n_regions),
                                    result['region_ids'],
                                    result['thresh_vec'])).T,
               fmt="%.15g")

def detect(cfg, chrom=1):
    '''
    Coordinate detection procedure.

    Parameters
    ----------
        - cfg : dictionary
            Dictionary of parameters containing at least those relevant for
            detection.
        - chrom : int
            Index of chromosome to analyze
    '''

    # Load nonnull coefficients
    coef_path = cfg['estimation_output']['coef_pattern']
    coef_path = coef_path.format(**cfg).strip()
    coef_path = coef_path % chrom
    coef = np.loadtxt(coef_path)

    # Load region_types
    with open(cfg['data']['regions_path'], 'rb') as f:
        lines_read = 0
        for line in f:
            lines_read += 1
            if lines_read == chrom:
                region_types = np.fromstring(line.strip(), sep=' ',
                                             dtype=int)
                break

    region_types = region_types[:coef.size]
    region_types -= region_types.min()
    region_ids = np.unique(region_types)

    # Obtain FDR-based detection thresholds
    results_fdr = calculate_fdr_threshold_vector(chrom=chrom, cfg=cfg,
                                                 nonnull=coef,
                                                 region_types=region_types,
                                                 region_ids=region_ids)

    # Output FDR thresholds
    write_fdr_thresholds(results_fdr, cfg, chrom=chrom)

    if cfg['detection_params']['use_bayes_se']:
        # Load standard errors
        se_path = cfg['estimation_output']['se_pattern']
        se_path = se_path.format(**cfg).strip()
        se_path = se_path % chrom
        se = np.loadtxt(se_path)

        # Load parameters
        param_path = cfg['estimation_output']['param_pattern']
        param_path = param_path.format(**cfg).strip()
        param_path = param_path % chrom
        mu, sigmasq = np.loadtxt(param_path, unpack=True, skiprows=1)[1:]

        mu = np.ravel(mu)
        sigmasq = np.ravel(sigmasq)

        # Compute n_se for detection
        n_se = -stats.norm.ppf(cfg['detection_params']['alpha'])

        # Detect positions based upon both FDR and Bayes criteria
        detected = np.where((coef > results_fdr['thresh_vec'][region_types]) &
                            (np.log(coef) - n_se*se > mu[region_types]))[0]
    else:
        detected = np.arange(coef.size, dtype=np.integer)

    # Restrict to local maxima if request
    if cfg['detection_params']['detect_maxima_only']:
        detected = np.intersect1d(lib.find_maxima(coef), detected)

    # Output detected positions
    detected_path = cfg['detection_output']['detected_pattern'].format(**cfg)
    detected_path = detected_path.strip() % chrom
    np.savetxt(detected_path, detected, fmt='%d')


#!/usr/bin/env python3
#
#   This file is part of MiniFluxDiv.

#   MiniFluxDiv is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   any later version.

#   MiniFluxDiv is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with MiniFluxDiv. If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
import math
import subprocess as sub
import traceback as tb
import argparse as ap

DEBUG = False #True

def ncpus():
    import multiprocessing
    return multiprocessing.cpu_count()

def run(args, vars={}, stream=None):
    output = ''

    for key in vars:
        os.environ[key] = vars[key]

    try:
        data = sub.check_output(args, env=os.environ, stderr=sub.STDOUT)
        output = data.decode()
    except Exception as e:
        eprint(str(e))

    if stream is not None:
        stream.write(output)

    return output

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

def parseArgs():
    parser = ap.ArgumentParser('runMiniFluxDiv', description='Execute miniFluxDiv test cases.')
    parser.add_argument('-e', '--exec', default='', help='Executable to run.')
    parser.add_argument('-l', '--legend', default='', help='Chart legend for output table.')
    parser.add_argument('-b', '--boxes', default='32', help='List of box sizes.')
    parser.add_argument('-c', '--cells', default='128', help='List of cell sizes.')
    parser.add_argument('-p', '--threads', default='1', help='List of thread counts.')
    parser.add_argument('-n', '--runs', default='3', help='Number of runs.')
    parser.add_argument('-d', '--delim', default=',', help='List delimiter.')
    parser.add_argument('-v', '--verify', default='0', help='Perform verification.')

    return parser.parse_known_args()

def main():
    if len(sys.argv) < 2:
        sys.argv.append('-h')

    (args, unknowns) = parseArgs()

    executable = args.exec
    delim = args.delim

    boxStr = args.boxes.split(delim)
    boxList = [int(val) for val in boxStr]

    cellStr = args.cells.split(delim)
    cellList = [int(val) for val in cellStr]

    maxThreads = ncpus()
    thrStr = args.threads.split(delim)

    thrList = []
    for val in thrStr:
        val = int(val)
        if val > 0 and val <= maxThreads:
            thrList.append(val)

    nRuns = int(args.runs)
    doVerify = int(args.verify) > 0

    program = executable.split('/')[-1]
    vars = {'OMP_SCHEDULE': 'static'}

    legend = args.legend
    if len(legend) < 1:
        legend = program.replace('miniFluxdiv-', '')

    header = 'Program,Legend,nBoxes,nCells,nThreads,'
    for i in range(nRuns):
        header += 'Time' + str(i) + ','
    header += 'MeanTime,MedianTime,StdevTime'

    print(header)

    nPairs = len(boxList)
    for threads in thrList:
        for i in range(nPairs):
            nBoxes = boxList[i]
            nCells = cellList[i]

            args = [executable, '-B', '%d' % nBoxes, '-C', '%d' % nCells, '-p', '%d' % threads]
            if doVerify and i == 0:     # Only verify first run...
                args.append('-v')

            cmd = ' '.join(args)

            times = []
            tsum = 0.0
            tsumsq = 0.0

            for i in range(nRuns):
                output = run(args, vars)
                isValid = len(output) > 0

                if isValid:
                    lines = output.rstrip().split("\n")
                    line = lines[-1].rstrip(',')
                    items = line.split(',')

                    dct = {}
                    for item in items:
                        (key, val) = item.split(':')
                        dct[key] = val

                    if doVerify and 'verification' in dct and 'FAIL' in dct['verification']:
                        print("VERIFICATION FAILED: on '%s' with '%s'" % (cmd, lines[0]))
                        isValid = False
                        break
                    else:
                        rtime = float(dct['RunTime'])
                        times.append(rtime)
                        tsum += rtime
                        tsumsq += (rtime * rtime)

            if isValid:
                tList = ['%lf' % time for time in times]

                # Calc mean & stdev
                fRuns = float(nRuns)
                mean = tsum / fRuns
                stdev = math.sqrt((tsumsq - (tsum * tsum) / fRuns) / float(nRuns - 1))

                # Calc median
                times = sorted(times)
                mid = int(nRuns / 2)
                if nRuns % 2 == 1:
                    median = times[mid]
                else:
                    median = (times[mid] + times[mid + 1]) / 2.0

                line = '%s,%s,%d,%d,%d,%s,%lf,%lf,%lf' % (program, legend, nBoxes, nCells, threads, ','.join(tList), mean, median, stdev)
                print(line)
                sys.stdout.flush()

if __name__ == '__main__':
    try:
        main()

    except KeyboardInterrupt as e: # Ctrl-C
        print("Closing gracefully on keyboard interrupt...")

    except Exception as e:
        print('Unexpected Exception: ' + str(e))
        tb.print_exc()
        os._exit(1)


import pandas as pd

import openai
import json
import pathlib
import numpy as np
import Levenshtein as lev
from Levenshtein import ratio
from functools import partial

def get_edit_ratio(s1, s2):
    return lev.ratio(s1, s2)

def get_edit_distance(s1, s2):
    return lev.distance(s1, s2)

class OpenAI:

    def __init__(self, model="text-davinci-003", api_key=None, organization_id='org-7PVKrNOMnq1PvOkuHr1XJX88'):

        if api_key is None:
            path = pathlib.Path("openai_api_key.pkl")
            if path.exists():
                api_key = pd.read_pickle("openai_api_key.pkl")
            else:
                api_key = input("Please enter your OpenAI API key:")
                pd.to_pickle(api_key, "openai_api_key.pkl")

        self.model = model
        self.organization_id = organization_id
        self.api_key = api_key
        self.usage = {"prompt_tokens": 0,
                      "completion_tokens": 0,
                      "total_tokens": 0}

        self.chat_history = []

        openai.api_key = api_key
        openai.organization = organization_id
        self._models = None

    def update_usage(self, response):

        response = response['usage']

        self.usage["prompt_tokens"] += response["prompt_tokens"]
        self.usage["completion_tokens"] += response["completion_tokens"]
        self.usage["total_tokens"] += response["prompt_tokens"] + response["completion_tokens"]

    def chat(self, message, name=None, system=None, system_name=None, reset_chat=False, model=None, temperature=1,
             top_p=1, n=1, stream=False, stop=None, max_tokens=None, presence_penalty=0, frequency_penalty=0.0,
             logit_bias=None):

        '''

        :param name:
        :param system:
        :param system_name:
        :param reset_chat:
        :param temperature:
        :param top_p:
        :param n:
        :param stream:
        :param stop:
        :param max_tokens:
        :param presence_penalty:
        :param frequency_penalty:
        :param logit_bias:
        :return:
        '''

        if reset_chat:
            self.chat_history = []

        messages = []
        if system is not None:
            system = {'system': system}
            if system_name is not None:
                system['system_name'] = system_name
            messages.append(system)

        messages.extend(self.chat_history)

        message = {'role': 'user', 'content': message}
        if name is not None:
            message['name'] = name

        messages.append(message)

        kwargs = {}
        if logit_bias is not None:
            kwargs['logit_bias'] = logit_bias
        if max_tokens is not None:
            kwargs['max_tokens'] = max_tokens
        if stop is not None:
            kwargs['stop'] = stop

        if model is None:
            model = self.model

        response = openai.ChatCompletion.create(
            model=model,
            messages=messages,
            temperature=temperature,
            top_p=top_p,
            n=n,
            stream=stream,
            stop=stop,
            presence_penalty=presence_penalty,
            frequency_penalty=frequency_penalty,
            **kwargs
        )

        self.update_usage(response)
        response_message = response['choices'][0]['message']
        self.chat_history.append(response_message)

        return response

    @property
    def is_chat(self):
        chat_models = ['gpt-4', 'gpt-3.5-turbo']
        if any([m in self.model for m in chat_models]):
            return True
        return False

    def docstring(self, text, element_type, name=None, docstring_format=None, parent=None, parent_name=None,
                  parent_type=None, children=None, children_type=None, children_name=None, **kwargs):

        if docstring_format is None:
            docstring_format = f"in \"{docstring_format}\" format, "
        else:
            docstring_format = ""

        prompt = f"Task: write a full python docstring {docstring_format}for the following {element_type}\n\n" \
                 f"========================================================================\n\n" \
                 f"{text}\n\n" \
                 f"========================================================================\n\n"

        if parent is not None:
            prompt = f"{prompt}" \
                     f"where is parent {parent_type}: {parent_name}, has the following docstring\n\n" \
                     f"========================================================================\n\n" \
                     f"{parent}\n\n" \
                     f"========================================================================\n\n"

        if children is not None:
            for i, (c, cn, ct) in enumerate(zip(children, children_name, children_type)):
                prompt = f"{prompt}" \
                         f"and its #{i} child: {ct} named {cn}, has the following docstring\n\n" \
                         f"========================================================================\n\n" \
                         f"{c}\n\n" \
                         f"========================================================================\n\n"

        prompt = f"{prompt}" \
                 f"Response: \"\"\"\n{{docstring text here (do not add anything else)}}\n\"\"\""

        if self.is_chat:
            try:
                res = self.chat(prompt, **kwargs)
            except Exception as e:
                print(f"Error in response: {e}")
                try:
                    print(f"{name}: switching to gpt-4 model")
                    res = self.chat(prompt, model='gpt-4', **kwargs)
                except:
                    print(f"{name}: error in response")
                    res = None
        else:
            res = self.ask(prompt, **kwargs)

        # res = res.choices[0].text

        return res

    def file_list(self):
        return openai.File.list()

    def retrieve(self, model=None):
        if model is None:
            model = self.model
        return openai.Engine.retrieve(id=model)

    @property
    def models(self):
        if self._models is None:
            models = openai.Model.list()
            self._models = {m.id: m for m in models.data}
        return self._models

    def embedding(self, text, model=None):
        if model is None:
            model = self.model
        response = openai.Engine(model).embedding(input=text, model=model)
        embedding = np.array(response.data[1]['embedding'])
        return embedding

    def ask(self, question, max_tokens=1024, temperature=1, top_p=1, frequency_penalty=0.0,
            presence_penalty=0.0, stop=None, n=1, stream=False, logprobs=None, echo=False):
        """
        Ask a question to the model
        :param n:
        :param logprobs:
        :param stream:
        :param echo:
        :param question:
        :param max_tokens:
        :param temperature: 0.0 - 1.0
        :param top_p:
        :param frequency_penalty:
        :param presence_penalty:
        :param stop:
        :return:
        """
        response = openai.Completion.create(
          engine=self.model,
          prompt=question,
          temperature=temperature,
          max_tokens=max_tokens,
          top_p=top_p,
          frequency_penalty=frequency_penalty,
          presence_penalty=presence_penalty,
          stop=stop,
          n=n,
          stream=stream,
          logprobs=logprobs,
          echo=echo
        )

        self.update_usage(response)

        return response

    def summary(self, text, n_words=100, n_paragraphs=None, **kwargs):
        """
        Summarize a text
        :param text:  text to summarize
        :param n_words: number of words to summarize the text into
        :param n_paragraphs:   number of paragraphs to summarize the text into
        :param kwargs: additional arguments for the ask function
        :return: summary
        """
        if n_paragraphs is None:
            prompt = f"Task: summarize the following text into {n_words} words\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""
        else:
            prompt = f"Task: summarize the following text into {n_paragraphs} paragraphs\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        return res.choices[0].text

    def question(self, text, question, **kwargs):
        """
        Answer a yes-no question
        :param text: text to answer the question from
        :param question: question to answer
        :param kwargs: additional arguments for the ask function
        :return: answer
        """
        prompt = f"Task: answer the following question\nText: {text}\nQuestion: {question}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text

        return res

    def yes_or_no(self, question, text=None, **kwargs):
        """
        Answer a yes or no question
        :param text: text to answer the question from
        :param question:  question to answer
        :param kwargs: additional arguments for the ask function
        :return: answer
        """

        if text is None:
            preface = ''
        else:
            preface = f"Text: {text}\n"

        prompt = f"{preface}Task: answer the following question with yes or no\nQuestion: {question}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text.lower().strip()
        res = res.split(" ")[0]

        i = pd.Series(['no', 'yes']).apply(partial(get_edit_ratio, s2=res)).idxmax()
        # print(res)
        return bool(i)

    def names_of_people(self, text, **kwargs):
        """
        Extract names of people from a text
        :param text: text to extract names from
        :param kwargs: additional arguments for the ask function
        :return: list of names
        """
        prompt = f"Task: extract names of people from the following text, return in a list of comma separated values\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text

        res = res.strip().split(",")

        return res

    def answer_email(self, input_email_thread, responder_from, receiver_to, **kwargs):
        """
        Answer a given email thread as an chosen entity
        :param input_email_thread_test: given email thread to answer to
        :param responder_from: chosen entity name which will answer the last mail from the thread
        :param receiver_to: chosen entity name which will receive the generated mail
        :param kwargs: additional arguments for the prompt
        :return: response mail
        """

        prompt = f"{input_email_thread}\n---generate message---\nFrom: {responder_from}To: {receiver_to}\n\n###\n\n"
        # prompt = f"Text: {text}\nTask: answer the following question with yes or no\nQuestion: {question}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text  # response email from model
        return res

    def classify(self, text, classes, **kwargs):
        """
        Classify a text
        :param text: text to classify
        :param classes: list of classes
        :param kwargs: additional arguments for the ask function
        :return: class
        """
        prompt = f"Task: classify the following text into one of the following classes\nText: {text}\nClasses: {classes}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text
        res = res.lower().strip()

        i = pd.Series(classes).str.lower().str.strip().apply(partial(get_edit_ratio, s2=res)).idxmax()

        return classes[i]

    def entities(self, text, humans=True, **kwargs):
        """
        Extract entities from a text
        :param humans:  if True, extract people, else extract all entities
        :param text: text to extract entities from
        :param kwargs: additional arguments for the ask function
        :return: entities
        """
        if humans:
            prompt = f"Task: extract people from the following text in a comma separated list\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""
        else:
            prompt = f"Task: extract entities from the following text in a comma separated list\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)

        entities = res.choices[0].text.split(',')
        entities = [e.lower().strip() for e in entities]

        return entities

    def title(self, text, **kwargs):
        """
        Extract title from a text
        :param text: text to extract title from
        :param kwargs: additional arguments for the ask function
        :return: title
        """
        prompt = f"Task: extract title from the following text\nText: {text}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text

        return res

    def similar_keywords(self, text, keywords, **kwargs):
        """
        Find similar keywords to a list of keywords
        :param text: text to find similar keywords from
        :param keywords: list of keywords
        :param kwargs: additional arguments for the ask function
        :return: list of similar keywords
        """

        keywords = [e.lower().strip() for e in keywords]
        prompt = f"Keywords: {keywords}\nTask: find similar keywords in the following text\nText: {text}\n\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text.split(',')
        res = [e.lower().strip() for e in res]

        res = list(set(res) - set(keywords))

        return res

    def is_keyword_found(self, text, keywords, **kwargs):
        """
        chek if one or more key words found in given text
        :param text: text to looks for
        :param keywords:  key words list
        :param kwargs: additional arguments for the ask function
        :return: yes if one of the keywords found else no
        """
        prompt = f"Text: {text}\nTask: answer with yes or no if Text contains one of the keywords \nKeywords: {keywords}\nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text.lower().strip().replace('"', "")

        i = pd.Series(['no', 'yes']).apply(partial(get_edit_ratio, s2=res)).idxmax()
        return bool(i)

    def get_similar_terms(self, keywords, **kwargs):
        """
        chek if one or more key words found in given text
        :param keywords:  key words list
        :param kwargs: additional arguments for the ask function
        :return: similar terms
        """
        prompt = f"keywords: {keywords}\nTask: return all semantic terms for given Keywords \nResponse: \"\"\"\n{{text input here}}\n\"\"\""

        res = self.ask(prompt, **kwargs)
        res = res.choices[0].text.lower().strip()
        return res

    #
    def build_dataset(self, data=None, question=None, answer=None, path=None) -> object:
        """
        Build a dataset for training a model
        :param data: dataframe with prompt and completion columns
        :param question: list of questions
        :param answer: list of answers
        :param path: path to save the dataset
        :return: path to the dataset
        """
        if data is None:
            data = pd.DataFrame(data={'prompt': question, 'completion': answer})

        records = data.to_dict(orient='records')

        if path is None:
            print('No path provided, using default path: dataset.jsonl')
            path = 'dataset.jsonl'

        # Open a file for writing
        with open(path, 'w') as outfile:
            # Write each data item to the file as a separate line
            for item in records:
                json.dump(item, outfile)
                outfile.write('\n')

        return path


#!/usr/bin/env python2
__author__ = 'Will Rowe'
__mail__ = "will.rowe@liverpool.ac.uk"

# Install a project in editable mode
# pip install -e . --user

from setuptools import setup

setup(
    name = "rnaseq",
    version = '0.1.0',
    packages = ["rnaseq"],
    author = 'Will Rowe',
    author_email = 'will.rowe@liverpool.ac.uk',
    url = 'http://will-rowe.github.io/',
    description = 'RNA-seq analysis pipeline for the Hinton Lab, Liverpool, UK',
    long_description = open('README.md').read(),
    package_dir = {'rnaseq': 'scripts'},
    scripts = ['scripts/dupRadar.sh'
    ],
    entry_points = {
        "console_scripts": ['rnaseq = rnaseq.rnaseq:main']
        },
    license = 'license.txt',
    install_requires = [
        "numpy == 1.7.0",
        "matplotlib == 1.5.3",
        "Jinja2 == 2.7.3",
        "MarkupSafe == 0.23",
        # the commandline executable multiqc needs to be installed and in path
        #"multiqc == 0.8",
    ],
)


# -*- coding: utf-8 -*-
import scrapy
from doubleScrapy.items import DoublescrapyItem

class DoubanSpiderSpider(scrapy.Spider):
    name = 'douban_spider'  #爬虫名字
    allowed_domains = ['movie.douban.com']  # 允许的域名
    start_urls = ['https://movie.douban.com/top250'] # 入口url，丢到调度里去

    def parse(self, response):
        """对请求回来的信息进行解析"""
        movieList = response.xpath("//div[@class='article']//ol[@class='grid_view']/li")
        for item in movieList:
            douban_item = DoublescrapyItem()
            douban_item['serial_number'] = item.xpath(".//div[@class='item']//em/text()").extract_first()
            douban_item['movie_name'] = item.xpath(".//div[@class='info']/div[@class='hd']/a/span[1]/text()").extract_first()
            douban_item['introduce'] = item.xpath(".//div[@class='item']//em/text()").extract_first()
            douban_item['star'] = item.xpath(".//div[@class='item']//em/text()").extract_first()
            douban_item['evaluate'] = item.xpath(".//div[@class='item']//em/text()").extract_first()
            douban_item['describe'] = item.xpath(".//div[@class='item']//em/text()").extract_first()

            print(douban_item)


import pytest

from ldndctools.misc.calculations import calc_hydraulic_properties
from ldndctools.misc.errors import ParameterMissingError
from ldndctools.misc.types import LayerData

def test_basic_computation():
    ld = LayerData(sand=0.1, clay=0.3, corg=0.05, bd=1.2)
    ld = calc_hydraulic_properties(ld)
    assert ld.wcmin == pytest.approx(295.35934)
    assert ld.wcmax == pytest.approx(472.21525)

def test_raise_for_missing_parameter():
    ld = LayerData(sand=0.1, clay=0.3, corg=0.05)
    with pytest.raises(ParameterMissingError):
        calc_hydraulic_properties(ld)

@pytest.mark.skip(
    reason="no way of currently testing this, wcmin corrected atm (check with David)"
)
def test_raise_for_bad_wcmin_wcmax():
    ld = LayerData(sand=0.1, clay=0.3, corg=1.0, bd=1.2)
    with pytest.raises(ValueError):
        calc_hydraulic_properties(ld)


from datetime import datetime

from database import get_db
from database.models import Card

from card import RegisterCardModel

# добавить карту
def add_new_card_db(card_data: RegisterCardModel):
    db = next(get_db())

    card_info = card_data.model_dump()

    new_card = Card(reg_date=datetime.now(), **card_info)

    db.add(new_card)
    db.commit()

    return True

# удалить карты
def delete_exact_card_db(user_id: int, card_id: int):
    db = next(get_db())

    exact_card = db.query(Card).filter_by(user_id=user_id, id=card_id).first()

    if exact_card:
        db.delete(exact_card)
        db.commit()

        return 'Карта успешно удалена'

    return 'Карта не найдена'

# вывести все карты пользователя
def get_all_user_cards_db(user_id: int):
    db = next(get_db())

    user_cards = db.query(Card).filter_by(user_id=user_id).all()

    return user_cards

# вывести определенную карту пользователя
def get_exact_user_card_db(user_id: int, card_id: int):
    db = next(get_db())

    exact_card = db.query(Card).filter_by(user_id=user_id, id=card_id).first()

    return exact_card


from PIL import Image
from tqdm import tqdm
# import tensorflow as tf
import binascii
import glob
import os

import numpy as np

dataCnt = 0
dataNum = 9000
width = 100
height = 100
channels = 3
dataset = np.zeros(shape=(dataNum, height, width, channels), dtype=np.float32)

def main():
    global dataCnt
    malware_path = './bytes'
    malware_categories = os.listdir(malware_path + '/')
    print(malware_categories)

    dataCnt = 0
    cnt = 0

    for malware_family in tqdm(malware_categories):
        print(malware_family)
        malware_files = glob.glob(malware_path + '/' + malware_family + '/*.txt')
        for malware_file in tqdm(malware_files):
            if not malware_file:
                continue
            try:
                f = open(malware_file, 'r')
                #                 print("FILE OPEN")
                code = f.readlines()
                one_line = ''
                for line in code:
                    line = line.replace("+", "")
                    line = line.replace("\n", "")

                    if len(line) != 2:
                        print(str(len(line)) + "### " + line)
                    one_line = one_line + line

                BinarySrcCode = binascii.unhexlify(one_line)
                code_len = len(BinarySrcCode)
                padLen = width * height * 3 - code_len

                if code_len == 0:
                    continue
                if padLen < 0:
                    print("file is over")
                    cnt += 1
                    continue

                zeroPad = bytes(padLen)
                image = bytearray(BinarySrcCode) + bytes(zeroPad)
                img = Image.frombytes("RGB", (height, width), bytes(list(image)))
                #                 print("READY TO SAVE")
                img.save("./malware_images/" + malware_family + "/" + malware_file[17:-4] + ".PNG", 'PNG')
                dataCnt += 1

            except Exception as ex:
                #                 print("FILE NOT OPENED")
                print(malware_file)
                print(ex)
                return

        print("num of exceeded sol :", cnt)
        print("num of total vul :", dataCnt)

    return

main()

from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf

data_dir = './malware_images/'
datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2)

train_it = datagen.flow_from_directory(
    data_dir,
    class_mode='categorical',
    target_size=(100, 100),
    batch_size=32)

val_it = validation_generator = datagen.flow_from_directory(
    data_dir,
    target_size=(100, 100),
    batch_size=32,
    subset='validation')

cnn_model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 1)),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(9, activation='softmax')
])

cnn_model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

cnn_model.fit(train_it, epochs=10)
test_eval_result = cnn_model.evaluate(val_it)
print(test_eval_result)

converter = tf.lite.TFLiteConverter.from_keras_model(cnn_model)
tflite_model = converter.convert()

with open('./keras_model_cnn_malware.tflite', 'wb') as f:
    f.write(tflite_model)


#!/usr/bin/env python
import itertools
import networkx as nx
import matplotlib.pyplot as plt

from networkx.algorithms.community.centrality import girvan_newman as nx_girvan_newman
from girvan_newman import girvan_newman as our_girvan_newman

from networkx.algorithms.centrality import edge_betweenness_centrality as nx_edge_betweenness_centrality
from girvan_newman import edge_betweenness_centrality as our_edge_betweenness_centrality

from tools import Timer

def benchmark_girvan_newman(G):
    print("Starting benchmark_girvan_newman...")
    max_iteration_level = 10

    Y_our = []
    Y_nx = []

    our_it = our_girvan_newman(G)
    nx_it = nx_girvan_newman(G)

    our_it = itertools.islice(our_it, max_iteration_level)
    nx_it = itertools.islice(nx_it, max_iteration_level)

    i = 0
    while True:
        try:
            with Timer(f"Starting iteration {i} on our girvan newman") as t:
                next(our_it)
                Y_our.append(t.get_time())
            with Timer(f"Starting iteration {i} on nx girvan newman") as t:
                next(nx_it)
                Y_nx.append(t.get_time())
        except StopIteration:
            break
        i += 1

    X = list(range(len(Y_our)))
    plt.figure()
    plt.title("Time over iteration for executing girvan newman")
    plt.xlabel("Iteration level []")
    plt.ylabel("Time [s]")
    plt.plot(X, Y_our, label="Our implementation")
    plt.plot(X, Y_nx, label="Networkx implementation")
    plt.legend()
    plt.savefig("benchmark_girvan_newman.png")

    Y_our_cumulative = get_cumulative_array(Y_our)
    Y_nx_cumulative = get_cumulative_array(Y_nx)

    plt.figure()
    plt.title("Cumulative time over iteration for executing girvan newman")
    plt.xlabel("Iteration level []")
    plt.ylabel("Time [s]")
    plt.plot(X, Y_our_cumulative, label="Our implementation")
    plt.plot(X, Y_nx_cumulative, label="Networkx implementation")
    plt.legend()
    plt.savefig("benchmark_girvan_newman_cumulative.png")

def benchmark_edge_betweenness_centrality(G):
    print("Starting benchmark_edge_betweenness_centrality...")

    X = list(range(0, len(G.nodes), 5))
    Y_our = []
    Y_nx = []

    for i in X:
        sub_G = G.subgraph(list(G.nodes)[0:i])
        with Timer("Starting our edge_betweenness_centrality") as t:
            our_edge_betweenness_centrality(sub_G)
            Y_our.append(t.get_time())
        with Timer("Starting nx edge_betweenness_centrality") as t:
            nx_edge_betweenness_centrality(sub_G)
            Y_nx.append(t.get_time())

    plt.figure()
    plt.title("Time over graph size for executing edge_betweenness_centrality")
    plt.xlabel("Iteration level []")
    plt.ylabel("Time [s]")
    plt.plot(X, Y_our, label="Our implementation")
    plt.plot(X, Y_nx, label="Networkx implementation")
    plt.legend()
    plt.savefig("benchmark_edge_betweenness_centrality.png")

def get_cumulative_array(a):
    sum = 0
    b = []
    for i in a:
        b.append(i + sum)
        sum += i
    return b

def main():
    G = nx.read_edgelist("subgraph.gz")
    # from tools import lecture_graph
    # G = lecture_graph()
    benchmark_girvan_newman(G)
    benchmark_edge_betweenness_centrality(G)

    plt.show()

if __name__ == "__main__":
    main()


import hail as hl
from hail.typecheck import typecheck
from hail.expr.expressions import expr_call, expr_numeric, expr_array, \
    raise_unless_entry_indexed, raise_unless_row_indexed

@typecheck(call_expr=expr_call,
           loadings_expr=expr_array(expr_numeric),
           af_expr=expr_numeric)
def pc_project(call_expr, loadings_expr, af_expr):
    """Projects genotypes onto pre-computed PCs. Requires loadings and
    allele-frequency from a reference dataset (see example). Note that
    `loadings_expr` must have no missing data and reflect the rows
    from the original PCA run for this method to be accurate.

    Example
    -------
    >>> # Compute loadings and allele frequency for reference dataset
    >>> _, _, loadings_ht = hl.hwe_normalized_pca(mt.GT, k=10, compute_loadings=True)   # doctest: +SKIP
    >>> mt = mt.annotate_rows(af=hl.agg.mean(mt.GT.n_alt_alleles()) / 2)                # doctest: +SKIP
    >>> loadings_ht = loadings_ht.annotate(af=mt.rows()[loadings_ht.key].af)            # doctest: +SKIP
    >>> # Project new genotypes onto loadings
    >>> ht = pc_project(mt_to_project.GT, loadings_ht.loadings, loadings_ht.af)         # doctest: +SKIP

    Parameters
    ----------
    call_expr : :class:`.CallExpression`
        Entry-indexed call expression for genotypes
        to project onto loadings.
    loadings_expr : :class:`.ArrayNumericExpression`
        Location of expression for loadings
    af_expr : :class:`.Float64Expression`
        Location of expression for allele frequency

    Returns
    -------
    :class:`.Table`
        Table with scores calculated from loadings in column `scores`
    """
    raise_unless_entry_indexed('pc_project', call_expr)
    raise_unless_row_indexed('pc_project', loadings_expr)
    raise_unless_row_indexed('pc_project', af_expr)

    gt_source = call_expr._indices.source
    loadings_source = loadings_expr._indices.source
    af_source = af_expr._indices.source

    loadings_expr = _get_expr_or_join(loadings_expr, loadings_source, gt_source, '_loadings')
    af_expr = _get_expr_or_join(af_expr, af_source, gt_source, '_af')

    mt = gt_source._annotate_all(row_exprs={'_loadings': loadings_expr, '_af': af_expr},
                                 entry_exprs={'_call': call_expr})

    if isinstance(loadings_source, hl.MatrixTable):
        n_variants = loadings_source.count_rows()
    else:
        n_variants = loadings_source.count()

    mt = mt.filter_rows(hl.is_defined(mt._loadings) & hl.is_defined(mt._af) & (mt._af > 0) & (mt._af < 1))

    gt_norm = (mt._call.n_alt_alleles() - 2 * mt._af) / hl.sqrt(n_variants * 2 * mt._af * (1 - mt._af))

    return mt.select_cols(scores=hl.agg.array_sum(mt._loadings * gt_norm)).cols()

def _get_expr_or_join(expr, source, other_source, loc):
    if source != other_source:
        if isinstance(source, hl.MatrixTable):
            source = source.annotate_rows(**{loc: expr})
        else:
            source = source.annotate(**{loc: expr})
        expr = source[other_source.row_key][loc]
    return expr


import h5py
import numpy as np

from mvpa2.base.collections import SampleAttributesCollection, \
     DatasetAttributesCollection, FeatureAttributesCollection

from pyitab.utils.bids import get_dictionary
from scipy.io import loadmat

def load_hcp_motor(filename, **kwargs):

    targets = {
        1: 'LH',
        2: 'LF',
        4: 'RH',
        5: 'RF',
        6: 'FIX'
    }

    mat = h5py.File(filename)
    data = mat['powerbox'][:]
    data /= np.nanmean(data)
    # Trials x Sources x Times
    data = np.float32(data.swapaxes(1, 2))

    labels = [targets[t] for t in mat['trialvec'][:][0]]
    limb = [t[1] for t in labels]
    side = [t[0] for t in labels]

    times = mat['timevec'][:].squeeze()
    rt = mat['trailinfo'][:][5]
    subject = kwargs['subject']

    sa_dict = get_dictionary(filename)
    sa_dict.pop('extension')
    sa_dict.pop('filename')
    sa = {k: [v for _ in range(rt.shape[0])] for k, v in sa_dict.items()}

    sa.update({'targets': labels,
               'chunks': np.arange(rt.shape[0]),
               'limb': limb,
               'side': side,
               'rt': rt,
               'subject': [subject for _ in range(rt.shape[0])],
               'file':   [filename for _ in range(rt.shape[0])]
               })

    sa = SampleAttributesCollection(sa)

    a = DatasetAttributesCollection({'times': times})
    fa = FeatureAttributesCollection({'matrix_values': np.ones(data.shape[1])})

    mat.close()

    return data, sa, a, fa

def load_hcp_blp(filename, **kwargs):
    mat = loadmat(filename)
    data = mat['data']

    parcels = data.shape[0]

    data = data[np.triu_indices(data.shape[0], k=1)]
    data = np.expand_dims(data, axis=0)

    subject = kwargs['subject']

    sa_dict = get_dictionary(filename)
    sa_dict.pop('extension')
    sa_dict.pop('filename')
    sa = {k: [v for _ in range(data.shape[0])] for k, v in sa_dict.items()}

    mainbands = ['alpha', 'beta', 'gamma']
    sa['mainband'] = np.zeros_like(sa['band'])
    for band in mainbands:
        mask = [band in dsband for dsband in sa['band']]
        sa['mainband'][mask] = band

    maintask = {'task1':  'LH',
                'task2':  'LF',
                'task4':  'RH',
                'task5':  'RF',
                'task14': 'AH',
                'task25': 'AF',
                'rest':   'NN'
                }

    for i, k in enumerate(['side', 'maintask']):
        sa[k] = [maintask[t][i] for t in sa['task']]

    sa.update({
               'subject': [subject for _ in range(data.shape[0])],
               'file':   [filename for _ in range(data.shape[0])]
               })

    idx_from, idx_to = np.triu_indices(parcels, k=1)

    labels = np.recfromcsv("/media/robbis/DATA/meg/viviana-hcp/atlas.csv")
    args = np.argsort(labels['node'])
    labels = labels[args]

    nodes_from = [labels[i]['abbr'].decode().strip() for i in idx_from]
    nodes_to = [labels[i]['abbr'].decode().strip() for i in idx_to]
    networks = ["%s+%s" %(nodes_from[i], nodes_to[i]) for i in range(len(nodes_to))]

    fa = dict(
        nodes_1=nodes_from,
        nodes_2=nodes_to,
        networks=networks,
        matrix_values=np.ones(data.shape[1])
    )

    fa = FeatureAttributesCollection(fa)
    a = DatasetAttributesCollection({})

    return data, sa, a, fa


import pytest

from lispy.context import ExecutionContext
from lispy.interpreter import IterativeInterpreter
from lispy.utils import eval_expr, parse_expr
from lispy.globals import GLOBALS
from lispy.tokenizer import Token

def test_balanced_parentheses():
    parse_expr('(()()((())())())')

    with pytest.raises(SyntaxError):
        parse_expr('(((())')

    with pytest.raises(SyntaxError):
        parse_expr('(())))')

def test_operations():
    inpr = IterativeInterpreter()
    assert eval_expr('(+ 1 1)', inpr) == 2
    assert eval_expr('(- 1 1)', inpr) == 0
    assert eval_expr('(/ 2 1)', inpr) == 2
    assert eval_expr('(* 2 2)', inpr) == 4

def test_nested_call():
    inpr = IterativeInterpreter()
    assert eval_expr('(+ (+ 1 1) 1)', inpr) == 3

def test_conditional():
    inpr = IterativeInterpreter()
    assert eval_expr('(if (= 0 (- 1 1)) 1 -1)', inpr) == 1

def test_let():
    inpr = IterativeInterpreter()
    assert eval_expr('(let (x 1 y 3) (+ x y))', inpr) == 4

def test_defn():
    inpr = IterativeInterpreter()
    assert eval_expr('(defn double (x) (+ x x)) (double 2)', inpr) == 4
    assert 'double' in inpr.ctx

def test_do():
    inpr = IterativeInterpreter()
    assert eval_expr('(do (+ 1 1) (- 1 1))', inpr) == 0

    assert eval_expr('(do (defn inc (x) (+ x 1)) (inc 1))', inpr) == 2
    assert 'inc' in inpr.ctx

    assert eval_expr('(do 3 & (list (+ 1 1) (- 1 1)))', inpr) == 0

def test_recursion():
    inpr = IterativeInterpreter()
    assert eval_expr(
        '(defn fact (x) (if (< x 2) 1 (* x (fact (- x 1))))) (fact 4)',
        inpr
    ) == 24

def test_varargs():
    inpr = IterativeInterpreter()
    assert eval_expr('(defn sum (& args) (+ &args)) (sum 1 2 3 4)', inpr) == 10
    assert eval_expr('(defn f (x y z) (+ x y z)) (f 1 & (list 2 3))', inpr) == 6

def test_import():
    inpr = IterativeInterpreter()
    eval_expr('(pyimport json)', inpr)

    import json
    assert inpr.ctx.get('json') == json
    assert eval_expr('(json.loads "[1, 2, 3]")', inpr) == [1, 2, 3]

def test_import_from():
    inpr = IterativeInterpreter()
    eval_expr('(pyimport_from json.decoder JSONArray)', inpr)

    from json.decoder import JSONArray
    assert inpr.ctx.get('JSONArray') == JSONArray

def test_member():
    ctx = ExecutionContext(None)
    ctx['s'] = '  abc  '
    inpr = IterativeInterpreter(ctx)
    assert eval_expr(r'(. strip s)', inpr) == ctx['s'].strip

def test_def():
    inpr = IterativeInterpreter()
    assert eval_expr('(def x 1 y (+ x 1))', inpr) == 2
    assert 'x' in inpr.ctx and 'y' in inpr.ctx
    assert eval_expr('(+ x y)', inpr) == 3

def test_str_names():
    inpr = IterativeInterpreter()
    with pytest.raises(NameError):
        eval_expr('(+ x y)', inpr)

def test_call():
    inpr = IterativeInterpreter()
    assert eval_expr('((. strip " abc "))', inpr) == 'abc'

def test_and():
    inpr = IterativeInterpreter()
    eval_expr('(def calls (list)) (defn canary (x) (do ((. append calls) x) x))', inpr)
    assert not eval_expr('(and (canary 0) (canary 1) (canary 2))', inpr)
    assert inpr.ctx['calls'] == [0]

    assert not eval_expr('(and 1 & (list 2 0 3))', inpr)

def test_or():
    inpr = IterativeInterpreter()
    eval_expr('(def calls (list)) (defn canary (x) (do ((. append calls) x) x))', inpr)
    assert eval_expr('(or (canary 0) (canary 1) (canary 2))', inpr)
    assert inpr.ctx['calls'] == [0, 1]

    assert eval_expr('(or 0 & (list 0 2 3))', inpr)

def test_anonymous_functions():
    inpr = IterativeInterpreter()
    assert eval_expr('((# * %0 2) 2)', inpr) == 4

def test_quote():
    inpr = IterativeInterpreter()

    def t(val):
        return Token(val, Token.guess_token_type(str(val)[0]))

    assert eval_expr('(quote + 1 2)', inpr) == [t('+'), t(1), t(2)]
    assert eval_expr('(quote 1 2 (3 (4 5) 6) 7)', inpr) == [
        t(1), t(2), [t(3), [t(4), t(5)], t(6)], t(7)
    ]
    assert eval_expr('(quote + 1 ~(+ 1 1))', inpr) == [t('+'), t(1), 2]

    with pytest.raises(RuntimeError):
        eval_expr('(+ ~(= 1 0) 2)', inpr)

def test_macro():
    inpr = IterativeInterpreter()
    eval_expr('(defmacro infix (args) (list (nth args 1) (nth args 0) (nth args 2)))', inpr)
    assert eval_expr('(let (i 1) (infix (i + 1)))', inpr) == 2

    assert eval_expr('(macroexpand infix (1 + 1))', inpr) == [
        Token('+', Token.TOKEN_OTHER), Token(1, Token.TOKEN_LITERAL), Token(1, Token.TOKEN_LITERAL)
    ]

    assert eval_expr('(infix (1 + 1))', inpr) == 2

def test_dynamic_bindings():
    inpr = IterativeInterpreter()
    assert eval_expr('(defn f (x) (+ x y)) (let (y 1) (f 2))', inpr) == 3

def test_dollar():
    inpr = IterativeInterpreter()
    assert eval_expr('(let (x 3) ($ "x"))', inpr) == 3

def test_function_arg_unpack():
    inpr = IterativeInterpreter()
    eval_expr('(defn f (a (b c) (d (e (f g)))) (+ a b c d e f g))', inpr)
    assert eval_expr(
        '(let (a 1 b 1 c 1 d 1 x (list 1 (list 1 1))) (f a (list b c) (list d x)))',
        inpr
    ) == 7

    with pytest.raises(SyntaxError):
        eval_expr('(defn g (a & (b c)) (+ a b c))', inpr)

def test_let_unpack():
    inpr = IterativeInterpreter()
    assert eval_expr('(let ((a b c) (list 1 2 3)) (+ a b c))', inpr) == 6

    with pytest.raises(RuntimeError):
        eval_expr('(let ((a b c) (list 1)) 3)', inpr)

    with pytest.raises(RuntimeError):
        eval_expr('(let ((a) (list 1 2 3)) 3)', inpr)

def test_in():
    inpr = IterativeInterpreter()
    assert eval_expr('(in 4 (list 1 2 3))', inpr) == False
    assert eval_expr('(in 2 (set (list 1 2 3)))', inpr) == True

def test_filter():
    inpr = IterativeInterpreter()
    assert eval_expr('(filter (defn even (x) (= 0 (% x 2))) (range 10))', inpr) == [0, 2, 4, 6, 8]
    assert eval_expr('(filter (defn even (x) (= 0 (% x 2))) (list))', inpr) == []

def test_map():
    inpr = IterativeInterpreter()
    res = eval_expr('(map (defn double (x) (* 2 x)) (range 5))', inpr)
    assert res == [0, 2, 4, 6, 8]

def test_match():
    inpr = IterativeInterpreter()

    match = '(match %s ((a) 1) ((a b) 2) ((a b c) 3) (a -1))'

    assert eval_expr(match % '(list 1)', inpr) == 1
    assert eval_expr(match % '(list 1 2)', inpr) == 2
    assert eval_expr(match % '(list 1 2 3)', inpr) == 3
    assert eval_expr(match % '4', inpr) == -1

    with pytest.raises(RuntimeError):
        eval_expr('(match (list 1 2) ((a) 1))', inpr)


"""
from tkinter import *

def run_animation():
    while True:
        try:
            global photo
            global frame
            global label
            photo = PhotoImage(
                file = photo_path,
                format = "gif - {}".format(frame)
                )

            label.configure(image = nextframe)

            frame = frame + 1

        except Exception:
            frame = 1
            break

root = Tk()
photo_path = "C:/Users/user/Downloads/glitch_webcam_me.gif"

photo = PhotoImage(
    file = photo_path,
    )
label = Label(
    image = photo
    )
animate = Button(
    root,
    text = "animate",
    command = run_animation
    )

label.pack()
animate.pack()

root.mainloop()
"""
import datetime
import json
import os
import random
import re
import smtplib
import subprocess
import sys
import time
import webbrowser

import pyautogui
import pyttsx3  # pip install pyttsx3
import requests
import speech_recognition as sr  # pip install speechRecognition
import wikipedia  # pip install wikipedia
import wolframalpha
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

from tkinter import *
import time
import os

root = Tk()

frameCnt = 12
frames = [PhotoImage(file='C:/Users/user/Downloads/q.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[1].id)

def update(ind):

    frame = frames[ind]
    ind += 1
    if ind == frameCnt:
        ind = 0
    label.configure(image=frame)
    root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)

root.mainloop()

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def start():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        speak("who are you")
        r.pause_threshold = 6
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)

    try:
        speak("comnfirmed access")
        quer = r.recognize_google(audio, language='en-in')
        print(f"sir: {quer}\n")

    except Exception as e:
        # print(e)
        print("sorry sir stranger not allowed")
        return "None"
    return quer

def wishMe():
    #print("                     I only have access to these only               ")
    #print("************************************************************************************************")
    #print("")
    #print("")
    #print("           WIKI  YOUTUBE FACEBOOK GOOGLE TAB SWITCH ACTIVE CHROME CAMERA CAPTURE FOLDER XDA STACKOVERFLOW   ")
    #print("************************************************************************************************")
    #print("          MUSIC TIME VS EMAIL EXIT NOTEPAD SHUTDOWN NEWS TIMER PC STATUS LOCATION CALCULATE")
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("  Good Morning sir ")

    elif hour>=12 and hour<18:
        speak(" Good Afternoon sir")

    else:
        speak(" Good Evening sir ")

    speak("I am online ")

    stMsgs = ['how are you today? ', 'Hope you are having a nice time ', 'at your command ', 'how can I help you sir ' , 'working on improvement' , 'having completed all previous task ' , 'glad to see you working boss' , 'pleased with your work sir' , 'installed all required data files ']
    speak(random.choice(stMsgs))

    speak("  ")
    speak(" ")

    stMsgs = ['calibrated data file  ', 'connected to data servers  ', 'restablizied utilities',  ]
    speak(random.choice(stMsgs))

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("hmmmmm")
        r.pause_threshold = 6
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)

    try:
        speak("searching for  resources")
        query = r.recognize_google(audio, language='en-in')
        print(f"sir: {query}\n")

    except Exception as e:
        # print(e)
        print("sorry sir again please ")
        return "None"
    return query

def search_web():
    driver =  webdriver.Chrome(r'C:\Users\B&O\Downloads\chromedriver11111.exe')

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('youremail@gmail.com', 'your-password')
    server.sendmail('youremail@gmail.com', to, content)
    server.close()

if __name__ == "__main__":
    update()
    wishMe()
    while True:
    # if 1:
        query = takeCommand().lower()

        # Logic for executing tasks based on query
        if 'wikipedia' in query or  'who ' in query or  'how ' in query or  'why ' in query or  'when ' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=8)
            speak("According to Wikipedia")
            page = wikipedia.page(query)
            print(page.images[0])
            print(results)
            speak(results)

        elif  'youtube' in query :
            speak('Opening ')
            indx = query.split().index('youtube')
            quer = query.split()[indx+1:]
            driver =  webdriver.Chrome(r'C:\Users\B&O\Downloads\chromedriver11111.exe')
            driver.maximize_window()
            driver.get("http://www.youtube.com/results?search_query=" + '+'.join(quer))
            pyautogui.click(370, 328)

        elif 'open facebook' in query:
            speak('Opening ')
            webbrowser.open("facebook.com")

        elif 'google' in query or  'show information regarding ' in query  :
           speak('Opening ')
           indx1 = query.split().index('google')
           quer1 = query.split()[indx1+1:]
           driver =  webdriver.Chrome(r'C:\Users\B&O\Downloads\chromedriver11111.exe')
           driver.maximize_window()
           driver.get("https://www.google.com/search?q=" + '+'.join(quer1))
           pyautogui.click(1271, 51)

        elif 'close the web' in query:
            pyautogui.click(1354, 17)

        elif 'first tab ' in query:
            speak(' yes')
            pyautogui.click(170, 13)

        elif 'close first tab' in query:
            speak(' yes')
            pyautogui.click(175, 13)

        elif 'close second tab' in query:
            speak(' yes')
            pyautogui.click(360, 13)

        elif 'open second  tab' in query:
            speak(' yes')
            pyautogui.click(365, 13)

        elif 'activate chrome ' in query:
            speak(' yes')
            pyautogui.click(624, 751)

        elif 'camera' in query:
            speak(' yes')
            pyautogui.click(552, 752)

        elif 'capture' in query:
            speak(' yes')
            pyautogui.click(1314, 376)

        elif 'open my folder ' in query:
            speak(' yes')
            pyautogui.click(0, 29)
            pyautogui.click(1041, 351)
            pyautogui.click(1201, 218)

        elif 'open xda' in query:
            speak(' right away')
            webbrowser.open("forum.xda-developers.com")

        elif 'open stackoverflow' in query:
            speak(' straight away ')
            webbrowser.open("stackoverflow.com")

        elif 'play music' in query:
            speak(' on my way sir')
            music_dir = 'C:\\Users\\rames\\Desktop\\ani\\aa'
            songs = os.listdir(music_dir)
            print(songs)
            os.startfile(os.path.join(music_dir, songs[0]))

        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"Sir, the time is {strTime}")

        elif 'open code' in query:
            codePath = "G:\\old\\Microsoft VS Code\\Code.exe"
            os.startfile(codePath)

        elif 'pubg' in query:
            codePath = "C:\\Program Files\\txgameassistant\\ui\\AndroidEmulator.exe"
            os.startfile(codePath)

        elif 'email to a' in query:
            try:
                speak("What should I say?")
                content = takeCommand()
                to = "harryyourEmail@gmail.com"
                sendEmail(to, content)
                speak("Email has been sent!")
            except Exception as e:
                print(e)
                speak("Sorry . I am not able to send this email")

        elif ' play endgame' in query:
            codePath = "C:\\Users\\rames\\Desktop\\Avengers Endgame (2019) Dual Audio Hindi 720p HDCAM NEW MovieMAD.mkv"
            os.startfile(codePath)

        elif 'ok done ' in query:
          speak("bye sir")
          sys.exit()

        elif 'open notepad' in query:
            speak(' yes ')
            codePath = "G:\\old\\danger\\notepad.bat"
            os.startfile(codePath)

        elif 'shut down the system' in query:
            speak('bye sir ')
            codePath = "G:\\old\\danger\\shutdown.bat"
            os.startfile(codePath)

        elif 'news' in query:
            speak('fetching latest news sir ')
            codePath = "G:\\old\\AI\\news.py"
            os.startfile(codePath)

        elif 'weather' in query or ' todays weather' in query:
            codePath = "G:\\old\\AI\\weather.py"
            os.startfile(codePath)

        elif 'start timer' in query:
            codePath = "G:\\old\\danger\\timer.bat"
            os.startfile(codePath)

        elif 'pc status' in query:
            codePath = "G:\\old\\danger\\sign.bat"
            os.startfile(codePath)

        elif 'where is ' in query:
           query = query.split(" ")
           location = query[2]
           speak("looking for  " + location + " sir " )
           os.system("chromium-browser http://www.google.nl/maps/place/" + location + "/&")

        elif "calculate" in query:
            app_id= "K62LAE-GKW95R2TJR"
            client = wolframalpha.Client(app_id)

            indx = query.split().index('calculate')
            quer = query.split()[indx + 1:]
            res = client.query(' '.join(quer))
            answer = next(res.results).text
            print("The answer is " + answer)
            speak("The answer is " + answer)


bl_info = {
    "name": "Render Settings Text",
    "author": "Harlepengren",
    "version": (1, 0),
    "blender": (3, 4, 0),
    "location": "3D View Sidebar",
    "description": "Creates renders with different settings to compare settings.",
    "warning": "",
    "doc_url": "",
    "tracker_url": "",
    "category": "Scene",
}

#import the modules
import bpy

# Property Group for the settings
class RenderSettingsPropertyGroup(bpy.types.PropertyGroup):
    numRenders: bpy.props.IntProperty(name="numRenders",default=1)
    targetName: bpy.props.StringProperty(name="targetName",description="Name of the setting that will change.")
    target: bpy.props.StringProperty(name="target", description="datapath of setting we want to change")
    start: bpy.props.FloatProperty(name="start", default=0.0, description="starting value")
    end: bpy.props.FloatProperty(name="end", default=1.0, description="ending value")

class RenderSettingsOperator(bpy.types.Operator):
    """Allows you to render multiple versions with different settings for comparison."""
    bl_idname = "scene.render_setting_operator"
    bl_label = "Render Instance Settings"

    def CreateMaterial(self,text_object):
        mat = bpy.data.materials.get("TextMaterial")
        if mat is None:
            mat = bpy.data.materials.new(name="TextMaterial")

        if not mat.use_nodes:
            mat.use_nodes = True
            nodes = mat.node_tree.nodes

        # Add emission
        bsdf = mat.node_tree.nodes["Principled BSDF"]
        bsdf.inputs['Emission'].default_value = (1.0,1.0,1.0,1.0)
        bsdf.inputs['Emission Strength'].default_value = 0.3

        if text_object.data.materials:
            text_object.materials[0] = mat
        else:
            text_object.data.materials.append(mat)

    def execute(self, context):
        print("Rendering the scene")

        renderSlots = bpy.data.images["Render Result"].render_slots

        # Determine the type of the target

        isInt = eval('type(' + context.scene.render_settings_prop.target + ')== int')

        # Set the render seetings
        startingValue = context.scene.render_settings_prop.start
        iterations = int(min(context.scene.render_settings_prop.numRenders,len(renderSlots)))
        incrementalValue = (context.scene.render_settings_prop.end - startingValue)/max(1,iterations-1)

        if isInt:
            startingValue = int(startingValue)
            incrementalValue = int(incrementalValue)

        # Create the text
        bpy.ops.object.add(type="FONT")
        currText = bpy.context.object
        currText.parent = bpy.data.objects["Camera"]
        currText.location = (-1.4,-0.696,-4.0)
        currText.scale = (0.3,0.3,0.3)
        self.CreateMaterial(currText)

        wm = bpy.context.window_manager
        totalProgress = iterations
        wm.progress_begin(0,totalProgress)
        for index in range(0,iterations):
            renderSlots.active_index = index

            # Change the setting - using exec is not safe
            currentValue = startingValue + incrementalValue*index

            exec(context.scene.render_settings_prop.target + "= " + str(currentValue))
            currText.data.body = bpy.context.scene.render_settings_prop.targetName + ": {:.2f}".format(currentValue)
            bpy.ops.render.render()
            wm.progress_update(index)

        # Delete the text
        bpy.ops.object.select_all(action="DESELECT")
        currText.select_set(True)
        bpy.context.view_layer.objects.active = currText
        bpy.ops.object.delete()
        wm.progress_end()

        return {'FINISHED'}

class RenderSettingsPanel(bpy.types.Panel):
    bl_idname = 'VIEW3D_PT_ender_settings_panel'
    bl_label = 'Render Settings'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    def draw(self, context):
        self.layout.prop(bpy.context.scene.render_settings_prop,"numRenders")
        self.layout.prop(bpy.context.scene.render_settings_prop,"targetName")
        self.layout.prop(bpy.context.scene.render_settings_prop,"target")
        self.layout.prop(bpy.context.scene.render_settings_prop,"start")
        self.layout.prop(bpy.context.scene.render_settings_prop,"end")
        self.layout.separator()
        self.layout.operator(RenderSettingsOperator.bl_idname, text=RenderSettingsOperator.bl_label)

def register():
    bpy.utils.register_class(RenderSettingsPropertyGroup)
    bpy.utils.register_class(RenderSettingsOperator)
    bpy.utils.register_class(RenderSettingsPanel)

    # Create pointer reference for the property group
    bpy.types.Scene.render_settings_prop = bpy.props.PointerProperty(type=RenderSettingsPropertyGroup)

def unregister():
    bpy.utils.unregister_class(RenderSettingsPropertyGroup)
    bpy.utils.unregister_class(RenderSettingsOperator)
    bpy.utils.unregister_class(RenderSettingsPanel)

if __name__ == "__main__":
    register()


from utils.commonUtils import randomSelect
from utils.commonUtils import opt1Rules,opt2Rules
from utils.commonUtils import isThereAnswer,isTopAnswer,step1,step2
import numpy as np

def judgeLeft(reverseCarLines,carLines,leftMachine,carList,count,yangRunOuOrder):

    # 返回车道10（index 9）有车
    if (reverseCarLines[9] != -1):
        car = carList[reverseCarLines[9]]
        if(np.all(carLines[:,9]!=-1)):
            return -1

        else:
            leftMachine.dispatch(carLines, reverseCarLines, car, randomSelect(carLines), count.time)

    # 处理涂装 - PBS出车口上的车
    elif(count.carListptr<len(carList)):
        car = carList[count.carListptr]
        if (carLines[yangRunOuOrder[count.carListptr], 9] != -1):
            # leftMachine.dispatch(carLines, reverseCarLines, car, randomSelect(carLines), count.time)
            print("序列被占用，左机器等待")
            return -1
        else:
            leftMachine.dispatch(carLines, reverseCarLines, car, yangRunOuOrder[count.carListptr], count.time)

        count.carListptr += 1

    else:
        return -1

def judgeRight(reverseCarLines,carLines,rightMachine,carList,count,opt1,opt2,ansList):
    if(carLines[:,0].sum()==-6):
        print("右机器没有可以进行的操作 跳过")
        print(carLines)
        return -1
    flag1=0
    flag2=0

    for i in range(0, 6):
        if (carLines[i][0] != -1 and opt1Rules(opt1, carList[carLines[i][0]].energyType)):
            flag1=1
            rightMachine.dispatch(carLines, reverseCarLines, carList[carLines[i][0]], 101, count,ansList,opt1,opt2)
            break
    if(flag1==0):#没有满足条件1的
        for i in range(0, 6):
            if (carLines[i][0] != -1 and opt2Rules(opt2, carList[carLines[i][0]].energyNum)):
                flag2=1
                rightMachine.dispatch(carLines, reverseCarLines, carList[carLines[i][0]], 101, count,ansList,opt1,opt2)
                break
    if(flag1==0 and flag2==0):#没满足1也没满足2
        if (isTopAnswer(carLines, carList, opt1, opt2) and reverseCarLines[0] == -1):

            print("第一位有解，跳过")
            return -1
    if(flag1==0 and flag2==0):#没满足1也没满足2也没3
        arr = []
        for i in range(0, 6):
            if (carLines[i][0] != -1):
                arr.append([carList[carLines[i][0]],carList[carLines[i][0]].arriveTime])
        #arr为1号位置为-1的车
        if len(arr)!=0:
            min = arr[0][1]
            obj = None
            for ar in arr:
                if ar[1]<=min:
                    obj = ar[0]#最早到达的 carLines[i][0]=-1会触发语法糖问题
            if(isThereAnswer(carLines,carList,opt1,opt2) and reverseCarLines[0]==-1):
                rightMachine.dispatch(carLines, reverseCarLines, obj, 6, count, ansList,opt1,opt2)
            else:
                rightMachine.dispatch(carLines, reverseCarLines, obj, 101, count,ansList,opt1,opt2)


import dash_bootstrap_components as dbc
import plotly.express as px
from dash import html, Output, Input, dcc

from report.Base import BaseBlock

def get_history(player_df):
    ret = [dbc.Row([dbc.Col('Year', width=3), dbc.Col('Team', width=4), dbc.Col('Salary', width=4)],
                   className='text-center')]
    for idx, row in player_df.iterrows():
        ret.append(dbc.Row([
            dbc.Col(row['year'], className='text-center', width=3),
            dbc.Col(row['Team'], className='text-center', width=4),
            dbc.Col('£' + format(int(row['Base Salary']), ',d'), className='text-end', width=4)
        ]))

    return ret

class PlayerInfo:
    def __init__(self, df, app):
        self.sample_data = df
        self._profile = Profile(self.sample_data, app)
        self._salary = PlayerSalary(self.sample_data, app)

    def recommend_players(self):
        threshold = 50
        s_eda_df = self.sample_data.sort_values(by='Base Salary', ascending=False)  # 연봉으로 정렬
        s_eda_df = s_eda_df[s_eda_df['Position'] != 'Goalkeeper']  # Goalkeeper 제외
        return list(s_eda_df[~s_eda_df.duplicated(subset='Name', keep='first')].iloc[:threshold][['Name', 'Position']].itertuples(index=False))

    def render(self):
        return html.Div([
            dbc.Row([
                dbc.Select(
                    id="player-select",
                    options=[{'label': f'{name}({position})', 'value': name} for (name, position) in self.recommend_players()],
                    value="Son Heung-Min"
                ),
            ]),

            dbc.Row(html.Div(id='player_profile')),
            dbc.Card(dbc.Row(
                dcc.Graph(
                    figure={},
                    id='player_salary',
                    config={
                        'displayModeBar': False
                    }
                )
            ))

        ])

class Profile(BaseBlock):
    def __init__(self, sample_data, app):
        super().__init__(sample_data, app, 'Profile')

    @property
    def player_df(self):
        return self._player_df

    def callbacks(self, app):
        @app.callback(
            Output("player_profile", "children"), [Input(component_id="player-select", component_property='value')]
        )
        def select_name(name):
            if name:
                self.change_player(name)
            return self.render()

    def render(self):
        player_df = self._player_df.sort_values(by='year')
        return dbc.Card([
            dbc.Row([
                # image div
                dbc.Col(dbc.CardImg(
                    src=f"https://d2zywfiolv4f83.cloudfront.net/img/players/{player_df['Player Id'].values[-1]}.jpg",
                    top=True), width=4),

                # info div
                ## position, birth year, team
                dbc.Col(html.Div([
                    html.H5(f'이름: {self._player_name}', className="card-title",
                            style={'margin-bottom': '10px', 'margin-top': '10px'}),
                    html.Hr(),
                    html.P(f"출생: {player_df['Birth Year'].values[-1]}({2023 - player_df['Birth Year'].values[-1]})",
                           className="card-text", ),
                    html.P(f"포지션: {player_df['Position'].values[-1]}", className="card-text", ),
                    html.P(f"소속: {player_df['Team'].values[-1]}", className="card-text", )
                ]), width=8, align='center')
            ]),
            dbc.Row([
                # history
                ## 연도 - 팀 - 연봉
                dbc.Col(html.Div([*get_history(player_df)]), width=12, className='text-end')
            ], style={'margin-top': '10px'})
        ], style={'height': '50vh'})

class PlayerSalary(BaseBlock):
    def __init__(self, sample_data, app):
        super().__init__(sample_data, app, 'PlayerStats')

    def callbacks(self, app):
        @app.callback(
            Output(component_id="player_salary", component_property='figure'),
            [Input(component_id="player-select", component_property='value')]
        )
        def select_name(name):
            if name:
                self.change_player(name)
            return self.render()

    def render(self):
        player_df = self._player_df.sort_values(by='year')
        position = player_df['Position'].values[-1]
        year_mean_df = self._sample_data[self._sample_data['Position'] == position].groupby('year')[
            'Base Salary'].mean().reset_index()
        fig = px.bar(
            year_mean_df,
            x='year',
            y='Base Salary',
            title='Salary',
            labels={'year': 'year', 'Base Salary': 'Salary'}
        )
        fig.add_scatter(x=player_df['year'], y=player_df['Base Salary'], mode='lines+markers', showlegend=False)
        fig.update_layout(title_text=f"Salary '{self._player_name}' vs Avg", title={'x': 0.5, 'y': 0.94}, margin_t=60,
                          margin_b=40, height=450)
        return fig


from .lib.Qt import QtCore, QtGui, QtWidgets
from . import vrayUtils
from . import vray_toolboxUI as customUI
from maya import cmds
import pymel.core as pm

try:
    from shiboken import wrapInstance
except:
    from shiboken2 import wrapInstance

import maya.OpenMayaUI as omui

reload(vrayUtils)
reload(customUI)

__all__ = [
    'launch',
    ]

def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)

class ControlMainWindow(QtWidgets.QDialog):
    version = "1.1"

    def __init__(self, parent=None):
        super(ControlMainWindow, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.Tool)
        self.ui = customUI.Ui_vray_toolbox()
        self.ui.setupUi(self)

        self.ui.applyTo_btngrp = QtWidgets.QButtonGroup(self.ui.applyTo_hlyt)
        self.ui.applyTo_btngrp.addButton(self.ui.transforms_rbtn)
        self.ui.applyTo_btngrp.addButton(self.ui.shapes_rbtn)
        self.ui.selection_rbtn.setChecked(True)
        self.ui.shapes_rbtn.setChecked(True)

        self.ui.add_btn.clicked.connect(self.add_btn_clicked)
        self.ui.rem_bt.clicked.connect(self.remove_btn_clicked)

        self.ui.transforms_rbtn.clicked.connect(self.transforms_clicked)
        self.ui.shapes_rbtn.clicked.connect(self.transforms_clicked)

    def check_ui(self):
        selected = self.ui.selection_rbtn.isChecked()
        shapes = self.ui.shapes_rbtn.isChecked()

        commands = []
        if self.ui.subd_chk.isChecked():
            commands.append("vray_subdivision")
        if self.ui.subdDisp_chk.isChecked():
            commands.append("vray_subquality")
        if self.ui.disp_chk.isChecked():
            commands.append("vray_displacement")
        if self.ui.objID_chk.isChecked():
            commands.append("vray_objectID")
        if self.ui.openSubdiv_chk.isChecked():
            commands.append("vray_opensubdiv")

        return selected, shapes, commands

    def add_btn_clicked(self):
        selected, shapes, commands = self.check_ui()

        if not commands:
            return

        for c in commands:
            vrayUtils.vray_attributes(selected=selected, shapes=shapes, add=True, command=c)

    def remove_btn_clicked(self):
        selected, shapes, commands = self.check_ui()

        if not commands:
            return

        for c in commands:
            vrayUtils.vray_attributes(selected=selected, shapes=shapes, add=False, command=c)

    def transforms_clicked(self):
        is_checked = self.ui.transforms_rbtn.isChecked()
        if is_checked:
            self.ui.subd_chk.setDisabled(True)
            self.ui.subdDisp_chk.setDisabled(True)
            self.ui.disp_chk.setDisabled(True)
            self.ui.openSubdiv_chk.setDisabled(True)
            self.ui.subd_chk.setChecked(False)
            self.ui.subdDisp_chk.setChecked(False)
            self.ui.disp_chk.setChecked(False)
            self.ui.openSubdiv_chk.setChecked(False)
        else:
            self.ui.subd_chk.setEnabled(True)
            self.ui.subdDisp_chk.setEnabled(True)
            self.ui.disp_chk.setEnabled(True)
            self.ui.openSubdiv_chk.setEnabled(True)

##########################################

def launch():
    global dialog
    #if dialog is None:
    #    dialog = ControlMainWindow(parent=maya_main_window())

    #dialog.show()
    try:
        if dialog:
            delete()
    except:
        pass
    dialog = ControlMainWindow(parent=maya_main_window())
    dialog.show()

def delete():
    global dialog
    if dialog is None:
        return
    dialog.deleteLater()
    dialog = None


import numpy as np
from PIL import Image
import cv2

import tensorflow as tf
tf.compat.v1.enable_eager_execution()

from . import logging as logutil

logger = logutil.Logger(loggee="util/img")

class GaussianBlur():
    """Mainly to support Gaussian blurring before computing L1, a feature that
    might alleviate the color shift problem when L1 is used together with a
    perceptual loss.

    Intuition: "Consider the limiting case: if you blurred all the way (take
    global average of the image) and set the weight on the blurred-L1 to
    infinity, it would match the mean RGB value of the image."
    """
    def __init__(self, sigma, kernel_size=None, n_ch=3):
        if kernel_size is None:
            kernel_size = 6 * sigma # +/- 3 sigmas covers 99.7%
        x = tf.range(-kernel_size // 2 + 1.0, kernel_size // 2 + 1.0)
        xx, yy = tf.meshgrid(x, x)
        kernel = tf.exp(-(xx ** 2 + yy ** 2) / (2.0 * sigma ** 2))
        kernel = kernel / tf.reduce_sum(kernel)
        kernel = tf.tile(kernel[:, :, tf.newaxis], (1, 1, n_ch)) # KxKxC
        kernel = kernel[:, :, :, tf.newaxis] # KxKxCx1
        self.kernel = kernel

    def filter(self, x):
        """Input shape should be NxHxWxC.
        """
        y = tf.nn.depthwise_conv2d(
            x, self.kernel, (1, 1, 1, 1), 'SAME', data_format='NHWC')
        return y

def _clip_0to1_warn(tensor_0to1):
    """Enforces [0, 1] on a tensor/array that should be already [0, 1].
    """
    msg = "Some values outside [0, 1], so clipping happened"
    if isinstance(tensor_0to1, tf.Tensor):
        if tf.reduce_min(tensor_0to1) < 0 or tf.reduce_max(tensor_0to1) > 1:
            logger.debug(msg)
            tensor_0to1 = tf.clip_by_value(
                tensor_0to1, clip_value_min=0, clip_value_max=1)
    else:
        if tensor_0to1.min() < 0 or tensor_0to1.max() > 1:
            logger.debug(msg)
            tensor_0to1 = np.clip(tensor_0to1, 0, 1)
    return tensor_0to1

def alpha_blend(tensor1, alpha, tensor2=None):
    """Alpha-blend two tensors. If the second tensor is `None`, the first
    tensor will be blended with a all-zero tensor, equivalent to masking if
    alpha is binary.

    Alpha should range from 0 to 1.
    """
    if isinstance(tensor1, tf.Tensor):
        zeros = tf.zeros
        multiply = tf.multiply
    else:
        zeros = np.zeros
        multiply = np.multiply
    if tensor2 is None:
        tensor2 = zeros(tensor1.shape, dtype=tensor1.dtype)
    return multiply(tensor1, alpha) + multiply(tensor2, 1 - alpha)

def resize(tensor, new_h=None, new_w=None):
    """Assumes NxHxW(xC) tensor or HxW(xC) array.
    """
    if isinstance(tensor, tf.Tensor):
        h, w = tensor.shape[1:3]
    else:
        h, w = tensor.shape[:2]

    if new_h is not None and new_w is not None:
        if int(h / w * new_w) != new_h:
            logger.warn(("Aspect ratio changed in resizing: "
                         "original size is %s; new size is %s"),
                        (h, w), (new_h, new_w))
    elif new_h is None and new_w is not None:
        new_h = int(h / w * new_w)
    elif new_h is not None and new_w is None:
        new_w = int(w / h * new_h)
    else:
        raise ValueError("At least one of new height or width must be given")

    # TF tensor
    if isinstance(tensor, tf.Tensor):
        new_shape = (new_h, new_w)
        resized = tf.image.resize(tensor, new_shape)
        return tf.cast(resized, tensor.dtype)

    # NumPy array
    new_shape = (new_w, new_h)
    return cv2.resize(tensor, new_shape)

def linear2srgb(tensor_0to1):
    """Takes in a (Nx)HxW(x3) or (Nx)HxW(x1) tensor/array.
    """
    if isinstance(tensor_0to1, tf.Tensor):
        pow_func = tf.math.pow
        where_func = tf.where
    else:
        pow_func = np.power
        where_func = np.where

    srgb_linear_thres = 0.0031308
    srgb_linear_coeff = 12.92
    srgb_exponential_coeff = 1.055
    srgb_exponent = 2.4

    tensor_0to1 = _clip_0to1_warn(tensor_0to1)

    tensor_linear = tensor_0to1 * srgb_linear_coeff
    tensor_nonlinear = srgb_exponential_coeff * (
        pow_func(tensor_0to1, 1 / srgb_exponent)
    ) - (srgb_exponential_coeff - 1)

    is_linear = tensor_0to1 <= srgb_linear_thres
    tensor_srgb = where_func(is_linear, tensor_linear, tensor_nonlinear)

    return tensor_srgb

def to_uint(tensor_0to1, target_type='uint8'):
    """Converts a float tensor/array with values in [0, 1] to an unsigned
    integer type for visualization.
    """
    if isinstance(tensor_0to1, tf.Tensor):
        target_type = tf.as_dtype(target_type)
        tensor_0to1 = _clip_0to1_warn(tensor_0to1)
        tensor_uint = tf.cast(tensor_0to1 * target_type.max, target_type)
    else:
        tensor_0to1 = _clip_0to1_warn(tensor_0to1)
        tensor_uint = (np.iinfo(target_type).max * tensor_0to1).astype(
            target_type)
    return tensor_uint

# NOTE: untested
def rot90(img, counterclockwise=False):
    if isinstance(img, np.ndarray):
        from_to = (0, 1) if counterclockwise else (1, 0)
        img_ = np.rot90(img, axes=from_to)
    elif isinstance(img, tf.Tensor):
        k = 1 if counterclockwise else 3
        img_ = tf.image.rot90(img, k=k)
    else:
        raise TypeError(img)
    return img_

def set_left_top_corner(tensor, val):
    """Thanks to "eager tensor doesn't support assignment."
    """
    mask = np.ones(tensor.shape)
    mask[:, 0, 0, :] = val
    mask = tf.convert_to_tensor(mask, dtype=tf.float32)
    return tf.multiply(mask, tensor)

def hconcat(img_list, out_w=None):
    total = []
    for img in img_list:
        if total:
            prev = total[-1]
            img = resize(img, new_h=prev.shape[0])
        total.append(img)
    total = np.hstack(total)
    if out_w is not None:
        total = resize(total, new_w=out_w)
    return total

def put_text(
        img, text, font_scale=1.5, thickness=2, bottom_left_corner=(100, 100),
        text_bgr=(1, 1, 1)):
    text_bgr = [x * np.iinfo(img.dtype).max for x in text_bgr]
    img_ = img.copy()
    cv2.putText(
        img_, text,
        bottom_left_corner,
        cv2.FONT_HERSHEY_SIMPLEX, font_scale,
        text_bgr,
        thickness)
    return img_

def frame_image(img, rgb=None, width=4):
    if rgb is None:
        rgb = (0, 0, 1)
    rgb = np.array(rgb, dtype=img.dtype) * np.iinfo(img.dtype).max

    img[:width, :, :] = rgb
    img[-width:, :, :] = rgb
    img[:, :width, :] = rgb
    img[:, -width:, :] = rgb

def embed_into(inset, img, inset_scale=0.2):
    inset_h = int(inset_scale * img.shape[0])
    inset_w = int(inset_h / inset.size[1] * inset.size[0])
    inset = inset.resize((inset_w, inset_h))
    bg = Image.fromarray(img)
    bg.paste(inset, (bg.size[0] - inset.size[0], 0), inset) # inset's A
    # channel will be used as mask
    composite = np.array(bg)
    return composite


import numpy as np
import matplotlib.pyplot as plt

class bandit:
    def __init__(self,p):
        self.p = p
        self.p_estimate = 0
        self.N = 0

    def pull(self):
        return np.random.rand() < self.p

    def update(self, x):
        self.N +=1
        self.p_estimate = ((self.N-1)*self.p_estimate + x)/self.N

def ucb1(mean, N, nj):
    return mean + np.sqrt(2*np.log(N)/nj)

def experiment(num_trials,bandits_prob):
    reward = np.zeros(num_trials)
    bandits = [bandit(p) for p in bandits_prob]

    for i in range(len(bandits_prob)):
        x = bandits[i].pull()
        reward[i] = x
        bandits[i].update(x)

    for i in range(1,num_trials):
        #select bandit
        j = np.argmax([ucb1(b.p_estimate,i, b.N) for b in bandits])
        #pull arm
        x = bandits[j].pull()
        reward[i] = x
        #update
        bandits[j].update(x)
    #print details
    print("Total reward",reward.sum())
    print("_________________________\n")
    print("Estimation",[b.p_estimate for b in bandits])
    print("Ground Truth",bandits_prob)

    #plot reward
    plt.plot(np.cumsum(reward)/(np.arange(num_trials)+1))
    plt.plot(np.ones(num_trials)*np.max(bandits_prob))
    plt.xscale('log')
    plt.show()

if __name__ == "__main__":
    experiment(20000,[0.2,0.5,0.8])


from django.core.exceptions import ValidationError
from .models import TinyUrls
import re
from django import forms

class UrlForm(forms.ModelForm):
    class Meta:
        model = TinyUrls
        fields = ['full_url','tiny_url']
        widgets = {
            'full_url':forms.widgets.URLInput(attrs={'placeholder':'paste any long urls.. here'}),
            'tiny_url':forms.widgets.TextInput(attrs={'minlength':5}),
            }

    def clean_full_url(self):
        url = self.cleaned_data['full_url']
        # if not str(url).startswith(('http','https',)):
        #     raise ValidationError('url must begin with http , https')
        if len(url) > 2000:
            raise ValidationError('Url exceeded the size limit of 2000 charecters.!')
        if len(url) == 0 or str(url).isspace() :
            raise ValidationError('provide a valid url')
        else:
            return url

    def clean_tiny_url(self):
        tiny_url = self.cleaned_data['tiny_url']
        length = len(tiny_url)
        if length < 5 and length != 0:
            raise ValidationError(f'Atleast 5 charecters required.')
        elif length > 10:
            raise ValidationError(f'Atmost 10 charecters allowed. But got {length}.')
        elif length != 0:
            s2=re.findall('^[a-zA-Z0-9,-]{5,10}$',tiny_url)   # regex to ensure the key given is valid. (some string and digits of 5 to 10 charecters).
            try:
                s=str(s2[0])
                if s.startswith('-') or s.endswith('-'):
                    raise ValueError()
            except IndexError:
                raise ValidationError(f'Use letters and digits and -.')
            except ValueError:
                raise ValidationError(f"Hyphen ' - ' cannot begin or end a tiny url")
        if TinyUrls.objects.filter(tiny_url = tiny_url).exists():
            raise ValidationError(f'{tiny_url} exists.Choose another tiny url.')
        return tiny_url


import natnet
import asyncio

class OptitrackManager:
    callback = None
    client = None

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

    async def start(self):
        print('Optitrack startup')
        await asyncio.sleep(0.5)
        print('Optitrack running')
        self.client = natnet.Client.connect()
        print('Connected')
        self.client.set_callback(self._callback)
        await self._spin()

    async def _spin(self):
        #while True:
        #    print('Spinning')
        #    await asyncio.get_event_loop().run_in_executor(None, self.client.run_once)
        await asyncio.get_event_loop().run_in_executor(None, self._full_spin)

    def _full_spin(self):
        self.client.spin()

    def _callback(self, rigid_bodies, markers, timing):
        if self.callback is not None:
            self.callback(rigid_bodies, markers, timing)