# coding: utf-8

# public items
__all__ = [
    "chunk",
    "xarrayfunc",
]

# standard library
from concurrent.futures import ProcessPoolExecutor as Pool
from functools import wraps
from inspect import Parameter, signature, stack
from multiprocessing import cpu_count
from sys import _getframe as getframe

# dependent packages
import fmflow as fm
import numpy as np
import xarray as xr

# module constants
DEFAULT_N_CHUNKS = 1
try:
    MAX_WORKERS = cpu_count() - 1
except:
    MAX_WORKERS = 1

# decorators
def xarrayfunc(func):
    """Make a function compatible with xarray.DataArray.

    This function is intended to be used as a decorator like::

        >>> @fm.xarrayfunc
        >>> def func(array):
        ...     # do something
        ...     return newarray
        >>>
        >>> result = func(array)

    Args:
        func (function): A function to be wrapped. The first argument
            of the function must be an array to be processed.

    Returns:
        wrapper (function): A wrapped function.

    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        if any(isinstance(arg, xr.DataArray) for arg in args):
            newargs = []
            for arg in args:
                if isinstance(arg, xr.DataArray):
                    newargs.append(arg.values)
                else:
                    newargs.append(arg)

            return fm.full_like(args[0], func(*newargs, **kwargs))
        else:
            return func(*args, **kwargs)

    return wrapper

def chunk(*argnames, concatfunc=None):
    """Make a function compatible with multicore chunk processing.

    This function is intended to be used as a decorator like::

        >>> @fm.chunk('array')
        >>> def func(array):
        ...     # do something
        ...     return newarray
        >>>
        >>> result = func(array, timechunk=10)

    or you can set a global chunk parameter outside the function::

        >>> timechunk = 10
        >>> result = func(array)

    """

    def _chunk(func):
        depth = [s.function for s in stack()].index("<module>")
        f_globals = getframe(depth).f_globals

        # original (unwrapped) function
        orgname = "_original_" + func.__name__
        orgfunc = fm.utils.copy_function(func, orgname)
        f_globals[orgname] = orgfunc

        @wraps(func)
        def wrapper(*args, **kwargs):
            depth = [s.function for s in stack()].index("<module>")
            f_globals = getframe(depth).f_globals

            # parse args and kwargs
            params = signature(func).parameters
            for i, (key, val) in enumerate(params.items()):
                if not val.kind == Parameter.POSITIONAL_OR_KEYWORD:
                    break

                try:
                    kwargs.update({key: args[i]})
                except IndexError:
                    kwargs.setdefault(key, val.default)

            # n_chunks and n_processes
            n_chunks = DEFAULT_N_CHUNKS
            n_processes = MAX_WORKERS
            multiprocess = True

            if argnames:
                length = len(kwargs[argnames[0]])

                if "numchunk" in kwargs:
                    n_chunks = kwargs.pop("numchunk")
                elif "timechunk" in kwargs:
                    n_chunks = round(length / kwargs.pop("timechunk"))
                elif "numchunk" in f_globals:
                    n_chunks = f_globals["numchunk"]
                elif "timechunk" in f_globals:
                    n_chunks = round(length / f_globals["timechunk"])

                if "n_processes" in kwargs:
                    n_processes = kwargs.pop("n_processes")
                elif "n_processes" in f_globals:
                    n_processes = f_globals["n_processes"]

                if "multiprocess" in kwargs:
                    multiprocess = kwargs.pop("multiprocess")
                elif "multiprocess" in f_globals:
                    multiprocess = f_globals["multiprocess"]

            # make chunked args
            chunks = {}
            for name in argnames:
                arg = kwargs.pop(name)
                try:
                    chunks.update({name: np.array_split(arg, n_chunks)})
                except TypeError:
                    chunks.update({name: np.tile(arg, n_chunks)})

            # run the function
            futures = []
            results = []

            if multiprocess:
                with fm.utils.one_thread_per_process(), Pool(n_processes) as p:
                    for i in range(n_chunks):
                        chunk = {key: val[i] for key, val in chunks.items()}
                        futures.append(p.submit(orgfunc, **{**chunk, **kwargs}))

                    for future in futures:
                        results.append(future.result())
            else:
                for i in range(n_chunks):
                    chunk = {key: val[i] for key, val in chunks.items()}
                    results.append(orgfunc(**{**chunk, **kwargs}))

            # make an output
            if concatfunc is not None:
                return concatfunc(results)

            try:
                return xr.concat(results, "t")
            except TypeError:
                return np.concatenate(results, 0)

        return wrapper

    return _chunk


import os
import json
from copy import deepcopy
from mechanics import get_damage
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pickle

VERSION = 3
_TYPE = 0

def main(config, name, ret_dist=False, sim_size=250, dur_dist=None):
    config["sim_size"] = sim_size
    values = get_damage(config, ret_dist=ret_dist, dur_dist=dur_dist)
    return values

def load_config(cfn):
    config_file = os.path.join("../config/", cfn)
    with open(config_file, 'rt') as fid:
        config = json.load(fid)

    return config

def regress(encounter):
    config = load_config(encounter + ".json")

    sim_size = 10000
    values = main(config, encounter, sim_size=sim_size)
    print(encounter, values)

if __name__ == '__main__':
    for rdx in range(4):
       regress(f"regression{rdx + 1:d}")


import imp
import os

PluginFolder = "./plugins"
MainModule = "__init__"

class PluginLoader():
    def __init__(self, logger):
        self.logger = logger

    def __listPlugins(self):
        """
        List available plugins

        :return: list of available plugins
        """
        plugins = []
        possibleplugins = os.listdir(PluginFolder)
        for i in possibleplugins:
            location = os.path.join(PluginFolder, i)
            if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
                continue
            info = imp.find_module(MainModule, [location])
            plugins.append({"name": i, "info": info})
        return plugins

    def __loadPlugin(self, plugin):
        """
        Loads (initializes) specified plugin

        :param plugin: initialized plugin instance
        """
        return getattr(imp.load_module(MainModule, *plugin["info"]), "BotPlugin")(self.logger)

    def loadPlugins(self, callbackfunction):
        """
        Load all available plugins and sets callback for them

        :param callbackfunction: function that plugins can call to relay messages to bot
        :return: dict with initialized plugin instances
        """
        pluginlist = {}
        for i in self.__listPlugins():
            plugin = self.__loadPlugin(i)
            self.logger.info("Loaded plugin " + plugin.getname() + (" with commands: " + ', '.join([e for e in plugin.getcommands().keys()]) if plugin.getcommands() else "" ))
            plugin.setcallback(callbackfunction)
            if plugin.getcommands():
                for command in plugin.getcommands():
                    pluginlist[command] = plugin
        return pluginlist


'''
Plot the results of a Grid Search
'''

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
import localConfig as cfg

def findParam(inA,word,outA):
    for i in range(4,len(inA)):
        if inA[i].find(word) != -1:
            outA.append(float(inA[i+1].replace(',','').replace('}','')))

filepath = cfg.lgbk+"Searches"
os.chdir(filepath)

with open("gS:2017-09-06_00:10.txt") as f:
    data = f.read()

data = data.split("\n")

accuracy = []
std_accuracy = []
layers = []
learn_rate = []
dropout_rate = []
batch_size = []
epochs = []
neurons = []

for line in range(5,len(data)-1):
    l = data[line].split(" ")
    accuracy.append(float(l[0]))
    std_accuracy.append(float(l[2].replace('(','').replace(')','')))

    findParam(l,'layers',layers)
    findParam(l,'learn_rate',learn_rate)
    findParam(l,'dropout_rate',dropout_rate)
    findParam(l,'batch_size',batch_size)
    findParam(l,'epochs',epochs)
    findParam(l,'neurons',neurons)

di = dropout_rate.index(0.5)

fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(121, projection='3d')
x = neurons[:di]
y = layers[:di]
z = accuracy[:di]
ax.plot(x, y, z, c='r', marker='o')
ax.set_title('Dropout-rate: 0')
ax.set_xlabel('Neurons')
ax.set_ylabel('Layers')
ax.set_zlabel('Accuracy')

ax = fig.add_subplot(122, projection='3d')
x = neurons[di:]
y = layers[di:]
z = accuracy[di:]
ax.scatter(x, y, z, c='b', marker='^')
ax.set_title('Dropout-rate: 0.5')
ax.set_xlabel('Neurons')
ax.set_ylabel('Layers')
ax.set_zlabel('Accuracy')

plt.show()

'''
print accuracy
print "\n"
print std_accuracy
print "\n"
print layers
print "\n"
print learn_rate
print "\n"
print dropout_rate
print "\n"
print batch_size
print "\n"
print epochs
print "\n"
print neurons
'''


from twitter_collect.tweets_collector import collect
from pytest import *

def test_collect():
    tweets = collect()
    data =  transform_to_dataframe(tweets)
    assert 'tweet_textual_content' in data.columns

import pandas as pd

def transform_to_dataframe(tweets):
    tmp_list=[]
    if type(tweets)=='dictionary' or type(tweets)=='list':
        for tweet in tweets:
            tweet_as_dict={"text":tweet.text,"user": tweet.user.id ,"date":str(tweet.created_at),"hashtags":tweet.entities.get("hashtags"),"retweeted":tweet.retweeted,"retweet_count":tweet.retweet_count}
            tmp_list.append(tweet_as_dict)#rajouter condition si liste des hashtags vides ou non
    else:
        tmp_list.append("I'm empty")
    Dataframe=pd.DataFrame(tmp_list)
    return Dataframe


from .renderer import Renderer
from flask import Response, render_template
import urllib.request, urllib.parse, urllib.error
import database
from modules.ldapi import LDAPI
from rdflib import Graph, URIRef, Literal, Namespace

class ReportRenderer(Renderer):
    def __init__(self, uri, endpoints):
        Renderer.__init__(self, uri, endpoints)

        self.uri_encoded = urllib.parse.quote_plus(uri)
        self.label = None
        self.rt = None
        self.rt_label = None
        self.nid = None
        self.gat = None
        self.rs = None
        self.rs_encoded = None
        self.rs_label = None
        self.sa = None
        self.sa_label = None
        self.ea = None
        self.ea_label = None
        self.script = None

    def render(self, view, mimetype):
        if view == 'neighbours':
            # no work to be done as we have already loaded the triples
            if mimetype in LDAPI.get_rdf_mimetypes_list():
                return self._neighbours_rdf(mimetype)
            elif mimetype == 'text/html':
                self._get_details()
                return self._neighbours_html()
        elif view == 'prov':
            if mimetype in LDAPI.get_rdf_mimetypes_list():
                return Response(
                    self._prov_rdf().serialize(format=LDAPI.get_rdf_parser_for_mimetype(mimetype)),
                    status=200,
                    mimetype=mimetype
                )
            elif mimetype == 'text/html':
                self._get_details()
                return self._prov_html()

    def _neighbours_rdf(self, mimetype):
        query = '''
                  SELECT * WHERE {
                     <%(uri)s>  ?p ?o .
                  }
          ''' % {'uri': self.uri}
        g = Graph()
        g.bind('prov', Namespace('http://www.w3.org/ns/prov#'))
        for r in database.query(query)['results']['bindings']:
            if r['o']['type'] == 'literal':
                g.add((URIRef(self.uri), URIRef(r['p']['value']), Literal(r['o']['value'])))
            else:
                g.add((URIRef(self.uri), URIRef(r['p']['value']), URIRef(r['o']['value'])))

        query2 = '''
                  SELECT * WHERE {
                     ?s ?p <%(uri)s> .
                  }
          ''' % {'uri': self.uri}
        for r in database.query(query2)['results']['bindings']:
            g.add((URIRef(r['s']['value']), URIRef(r['p']['value']), URIRef(self.uri)))

        return Response(
            g.serialize(format=LDAPI.get_rdf_parser_for_mimetype(mimetype)),
            status=200,
            mimetype=mimetype
        )

    def _neighbours_html(self):
        """Returns a simple dict of Activity properties for use by a Jinja template"""
        self._make_svg_script()

        ret = {
            'rt_label': self.rt_label,
            'uri': self.uri,
            'uri_encoded': self.uri_encoded,
            'label': self.label,
            'nid': self.nid,
            'gat': self.gat,
            'rs_encoded': self.rs_encoded,
            'rs_label': self.rs_label,
            'sa': self.sa,
            'ea': self.ea,
            'script': self.script
        }

        return render_template(
            'class_report.html',
            report=ret
        )

    def _prov_rdf(self):
        query = '''
                 SELECT * WHERE {
                    <%(uri)s>  ?p ?o .
                 }
         ''' % {'uri': self.uri}
        g = Graph()
        g.bind('prov', Namespace('http://www.w3.org/ns/prov#'))
        for r in database.query(query)['results']['bindings']:
            if r['o']['type'] == 'literal':
                g.add((URIRef(self.uri), URIRef(r['p']['value']), Literal(r['o']['value'])))
            else:
                g.add((URIRef(self.uri), URIRef(r['p']['value']), URIRef(r['o']['value'])))

        query2 = '''
                 SELECT * WHERE {
                    ?s ?p <%(uri)s> .
                 }
         ''' % {'uri': self.uri}
        for r in database.query(query2)['results']['bindings']:
            g.add((URIRef(r['s']['value']), URIRef(r['p']['value']), URIRef(self.uri)))

        return g

    def _prov_html(self):
        """Returns a simple dict of Entity properties for use by a Jinja template"""
        ret = {
            'rt_label': self.rt_label,
            'uri': self.uri,
            'uri_encoded': self.uri_encoded,
            'label': self.label,
            'nid': self.nid,
            'gat': self.gat,
            'rs_encoded': self.rs_encoded,
            'rs_label': self.rs_label,
            'sa': self.sa,
            'ea': self.ea
        }

        prov_data = self._prov_rdf().serialize(format='turtle')

        return render_template(
            'class_report_prov.html',
            report=ret,
            prov_data=prov_data
        )

    def _get_details(self):
        """ Get the details for a Report from an RDF triplestore"""
        # formulate the query
        query = '''
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX proms: <http://promsns.org/def/proms#>
            PREFIX prov: <http://www.w3.org/ns/prov#>
            SELECT *
            WHERE {
                GRAPH ?g {
                    <%(uri)s>
                        a ?rt ;
                        rdfs:label ?label ;
                        proms:nativeId ?nid ;
                        prov:generatedAtTime ?gat ;
                        proms:wasReportedBy ?rs .
                    OPTIONAL {
                       ?rs rdfs:label ?rs_label .
                    }
                    OPTIONAL {
                        <%(uri)s>
                            proms:startingActivity ?sa .
                            ?sa rdfs:label ?sa_label .
                    }
                    OPTIONAL {
                        <%(uri)s>
                            proms:endingActivity ?ea .
                            ?ea rdfs:label ?ea_label .
                    } .
                }
            }
        ''' % {'uri': self.uri}

        # run the query
        report_details = database.query(query)

        # extract results into instance vars
        if report_details and 'results' in report_details:
            if len(report_details['results']['bindings']) > 0:
                ret = report_details['results']['bindings'][0]
                self.rt = ret['rt']['value']
                if 'Basic' in self.rt:
                    self.rt_label = 'Basic'
                elif 'Internal' in self.rt:
                    self.rt_label = 'Internal'
                elif 'External' in self.rt:
                    self.rt_label = 'External'
                self.label = ret['label']['value']
                self.nid = ret['nid']['value']
                self.gat = ret['gat']['value']
                self.rs = ret['rs']['value']
                self.rs_encoded = urllib.parse.quote_plus(self.rs)
                self.rs_label = ret['rs_label']['value'] if 'rs_label' in ret else self.rs
                if 'sa' in ret:
                    self.sa = ret['sa']['value']
                    self.sa_label = ret['sa_label']['value']
                if 'ea' in ret:
                    self.ea = ret['ea']['value']
                    self.ea_label = ret['ea_label']['value']

    def _make_svg_script(self):
        """ Construct the SVG code for a Report's Neighbours view"""
        self.script = '''
            var rLabel = "%(label)s";
            var report = addReport(350, 200, rLabel, "");
        ''' % {'label': self.label}

        self.script += '''
            var rsUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s";
            var rsLabel = "%(label)s";
            var repSystem = addReportingSystem(350, 20, rsLabel, rsUri);
            addLink(report, repSystem, "proms:reportingSystem", RIGHT);
        ''' % {
            'instance_endpoint': self.endpoints['instance'],
            'uri_encoded': self.rs_encoded,
            'label': self.rs_label
        }

        if self.sa is not None and self.ea is not None:
            if self.sa == self.ea:
                # External Report -- single Activity
                self.script += '''
                    var uri = "%(instance_endpoint)s?_uri=%(uri_encoded)s";
                    var label = "%(label)s";
                    var activity = addActivity(50, 200, label, uri);
                    addLink(report, activity, "proms:startingActivity", TOP);
                    addLink(report, activity, "proms:endingActivity", BOTTOM);
                ''' % {
                    'instance_endpoint': self.endpoints['instance'],
                    'uri_encoded': urllib.parse.quote(self.sa),
                    'label': self.sa_label
                }
            else:
                # Internal Report -- 2 Activities
                self.script += '''
                    var saUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s";
                    var saLabel = "%(label)s";
                    var sacActivity = addActivity(50, 120, sacLabel, sacUri);
                    addLink(report, sacActivity, "proms:startingActivity", TOP);
                ''' % {
                    'instance_endpoint': self.endpoints['instance'],
                    'uri_encoded': urllib.parse.quote(self.sa),
                    'label': self.sa_label
                }

                self.script += '''
                    var eacUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s";
                    var eacLabel = "%(label)s";
                    var eacActivity = addActivity(50, 280, eacLabel, eacUri);
                    addLink(report, eacActivity, "proms:endingActivity", BOTTOM);
                ''' % {
                    'instance_endpoint': self.endpoints['instance'],
                    'uri_encoded': urllib.parse.quote(self.ea),
                    'label': self.ea_label
                }
        else:
            # Basic Report -- no Activities
            pass


# coding: utf-8
import json
import os
from xml.sax.saxutils import escape
from nhentai.constant import LANGUAGE_ISO

def serialize_json(doujinshi, output_dir):
    metadata = {'title': doujinshi.name,
                'subtitle': doujinshi.info.subtitle}
    if doujinshi.info.date:
        metadata['upload_date'] = doujinshi.info.date
    if doujinshi.info.parodies:
        metadata['parody'] = [i.strip() for i in doujinshi.info.parodies.split(',')]
    if doujinshi.info.characters:
        metadata['character'] = [i.strip() for i in doujinshi.info.characters.split(',')]
    if doujinshi.info.tags:
        metadata['tag'] = [i.strip() for i in doujinshi.info.tags.split(',')]
    if doujinshi.info.artists:
        metadata['artist'] = [i.strip() for i in doujinshi.info.artists.split(',')]
    if doujinshi.info.groups:
        metadata['group'] = [i.strip() for i in doujinshi.info.groups.split(',')]
    if doujinshi.info.languages:
        metadata['language'] = [i.strip() for i in doujinshi.info.languages.split(',')]
    metadata['category'] = doujinshi.info.categories
    metadata['URL'] = doujinshi.url
    metadata['Pages'] = doujinshi.pages

    with open(os.path.join(output_dir, 'metadata.json'), 'w') as f:
        json.dump(metadata, f, separators=(',', ':'))

def serialize_comic_xml(doujinshi, output_dir):
    from iso8601 import parse_date
    with open(os.path.join(output_dir, 'ComicInfo.xml'), 'w', encoding="utf-8") as f:
        f.write('<?xml version="1.0" encoding="utf-8"?>\n')
        f.write('<ComicInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
                'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')

        xml_write_simple_tag(f, 'Manga', 'Yes')

        xml_write_simple_tag(f, 'Title', doujinshi.name)
        xml_write_simple_tag(f, 'Summary', doujinshi.info.subtitle)
        xml_write_simple_tag(f, 'PageCount', doujinshi.pages)
        xml_write_simple_tag(f, 'URL', doujinshi.url)
        xml_write_simple_tag(f, 'NhentaiId', doujinshi.id)
        xml_write_simple_tag(f, 'Genre', doujinshi.info.categories)

        xml_write_simple_tag(f, 'BlackAndWhite', 'No' if doujinshi.info.tags and
                             'full color' in doujinshi.info.tags else 'Yes')

        if doujinshi.info.date:
            dt = parse_date(doujinshi.info.date)
            xml_write_simple_tag(f, 'Year', dt.year)
            xml_write_simple_tag(f, 'Month', dt.month)
            xml_write_simple_tag(f, 'Day', dt.day)
        if doujinshi.info.parodies:
            xml_write_simple_tag(f, 'Series', doujinshi.info.parodies)
        if doujinshi.info.characters:
            xml_write_simple_tag(f, 'Characters', doujinshi.info.characters)
        if doujinshi.info.tags:
            xml_write_simple_tag(f, 'Tags', doujinshi.info.tags)
        if doujinshi.info.artists:
            xml_write_simple_tag(f, 'Writer', ' & '.join([i.strip() for i in
                                                          doujinshi.info.artists.split(',')]))

        if doujinshi.info.languages:
            languages = [i.strip() for i in doujinshi.info.languages.split(',')]
            xml_write_simple_tag(f, 'Translated', 'Yes' if 'translated' in languages else 'No')
            [xml_write_simple_tag(f, 'LanguageISO', LANGUAGE_ISO[i]) for i in languages
             if (i != 'translated' and i in LANGUAGE_ISO)]

        f.write('</ComicInfo>')

def xml_write_simple_tag(f, name, val, indent=1):
    f.write(f'{" "*indent}<{name}>{escape(str(val))}</{name}>\n')

def merge_json():
    lst = []
    output_dir = "./"
    os.chdir(output_dir)
    doujinshi_dirs = next(os.walk('.'))[1]
    for folder in doujinshi_dirs:
        files = os.listdir(folder)
        if 'metadata.json' not in files:
            continue
        data_folder = output_dir + folder + '/' + 'metadata.json'
        json_file = open(data_folder, 'r')
        json_dict = json.load(json_file)
        json_dict['Folder'] = folder
        lst.append(json_dict)
    return lst

def serialize_unique(lst):
    dictionary = {}
    parody = []
    character = []
    tag = []
    artist = []
    group = []
    for dic in lst:
        if 'parody' in dic:
            parody.extend([i for i in dic['parody']])
        if 'character' in dic:
            character.extend([i for i in dic['character']])
        if 'tag' in dic:
            tag.extend([i for i in dic['tag']])
        if 'artist' in dic:
            artist.extend([i for i in dic['artist']])
        if 'group' in dic:
            group.extend([i for i in dic['group']])
    dictionary['parody'] = list(set(parody))
    dictionary['character'] = list(set(character))
    dictionary['tag'] = list(set(tag))
    dictionary['artist'] = list(set(artist))
    dictionary['group'] = list(set(group))
    return dictionary

def set_js_database():
    with open('data.js', 'w') as f:
        indexed_json = merge_json()
        unique_json = json.dumps(serialize_unique(indexed_json), separators=(',', ':'))
        indexed_json = json.dumps(indexed_json, separators=(',', ':'))
        f.write('var data = ' + indexed_json)
        f.write(';\nvar tags = ' + unique_json)


import database_util
import unittest
from gradescope_utils.autograder_utils.decorators import weight, visibility
from HiddenPrints import HiddenPrints
with HiddenPrints():
    import SQLLab
import sqlite3 as sqlite
import os

class q3(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        try:
            os.remove('database.db')
        except:
            pass

    def setUp(self):
        self.con = sqlite.connect('database.db')
        self.cur = self.con.cursor()
        database_util.initialize_tables()

    '''
    @weight(1)
    def test_empty(self):
        """Q5: Function called on an empty table"""
        self.assertEqual(len(SQLLab.max_salary_title()), 0)
    '''

    @weight(4)
    def test_q5(self):
        """Q5: Main function"""
        database_util.testDatabase()
        self.assertListEqual(SQLLab.max_salary_title(),
            [(4, 'John', 'Cena', 'Sales', 100.01),
            (2, 'Rachel', 'Green', 'HR', 50.0),
            (3, 'Ross', 'Geller', 'IT', 49.99),
            (7, 'John', 'Watson', 'Investments', 39.0),
            (0, 'Harry', 'Truman', 'Engineering', 17.1)])

    def tearDown(self):
        self.con.close()
        os.remove('database.db')

if __name__ == '__main__':
    unittest.main()


from peos import pmlParser
import argparse
import os
import pathlib
from mockDinto import MockDinto
import itertools
from peos.peosSimpleTimeParser import PeosSimpleTimeParser

cd = os.path.dirname(os.path.realpath(__file__))

default_filepath = os.path.join(cd, 'test/complex.pml')

default_csv_filepath = os.path.join(cd, 'DINTO/Mock-DINTO-sample.csv')

class DdiFinder:

    def __init__(self, root):
        self.root = root
        self.ddi_list = self.find_possible_ddis(self.root)
        self.sequential_ddis = []
        self.parallel_ddis = []
        for ddi in self.ddi_list:
            self.find_actual_ddis(self.root, ddi['first_drug'], ddi['second_drug'],
                                  self.convert_time_to_secs(ddi['time_value'], ddi['time_unit']), False)

    def get_sequential_ddis(self):
        data = []
        for ddi in self.sequential_ddis:
            for possible_ddi in self.ddi_list:
                if (ddi["first_drug"] == possible_ddi["first_drug"] and ddi["second_drug"] == possible_ddi["second_drug"]) \
                        or (ddi["first_drug"] == possible_ddi["second_drug"] and ddi["second_drug"] == possible_ddi["first_drug"]):

                    ddi_data = {}
                    ddi_data["drug_one"] = ddi["first_drug"]
                    ddi_data["drug_two"] = ddi["second_drug"]
                    ddi_data["construct"] = ddi["construct"]
                    ddi_data["type"] = possible_ddi["DDI_type"]
                    ddi_data["time"] = possible_ddi["time_value"]
                    ddi_data["time_unit"] = possible_ddi["time_unit"]
                    data.append(ddi_data)
        return data

    def get_parallel_ddis(self):
        data = []
        for ddi in self.parallel_ddis:
            for possible_ddi in self.ddi_list:
                if (ddi["first_drug"] == possible_ddi["first_drug"] and ddi["second_drug"] == possible_ddi["second_drug"]) \
                     or (ddi["first_drug"] == possible_ddi["second_drug"] and ddi["second_drug"] == possible_ddi["first_drug"]):

                    ddi_data = {}
                    ddi_data["drug_one"] = ddi["first_drug"]
                    ddi_data["drug_two"] = ddi["second_drug"]
                    ddi_data["construct"] = ddi["construct"]
                    ddi_data["type"] = possible_ddi["DDI_type"]
                    ddi_data["time"] = possible_ddi["time_value"]
                    ddi_data["time_unit"] = possible_ddi["time_unit"]
                    data.append(ddi_data)
        return data

    # Given two drugs, it finds the closes they can occur in a DDI.
    def find_closest_ddis(self, drugs):
        if len(drugs) < 2:
            print("Error: More than two drugs must be specified to query")
            return

        drug_one = drugs[0]
        drug_two = drugs[1]

        self.ddi_list = self.find_possible_ddis(self.root)
        mock_dinto = MockDinto(filepath=default_csv_filepath, supress=True)
        ddi_drugs = mock_dinto.ddi_query([drug_one, drug_two])
        # Check that a DDI can occur between the two specified drugs.
        if len(ddi_drugs) > 0:
            contains_ddi = False
            # For each possible DDI in the parsed pml tree.
            for ddi in self.ddi_list:
                # If this is the DDI specified by the user, find it's closest time.
                if ddi["first_drug"] == drug_one and ddi["second_drug"] == drug_two \
                     or ddi["first_drug"] == drug_two and ddi["second_drug"] == drug_one:

                    self.sequential_ddis = []
                    self.parallel_ddis = []
                    # Find all the DDIs
                    self.find_actual_ddis(self.root, ddi["first_drug"], ddi["second_drug"],
                                          self.convert_time_to_secs(ddi['time_value'], ddi['time_unit']), True)
                    # Order the DDIs then output the closest

                    # Parallel DDIs have a closest time of 0 and therefore take priority over sequnetial.
                    if len(self.parallel_ddis) > 0:
                        contains_ddi = True
                        closest_ddi_data = {}
                        closest_ddi_data["drug_one"] = drug_one
                        closest_ddi_data["drug_two"] = drug_two
                        closest_ddi_data["time"] = 0
                        closest_ddi_data["construct"] = ""
                        return closest_ddi_data

                    elif len(self.sequential_ddis) > 0:
                        contains_ddi = True
                        closest_ddi = self.sequential_ddis[0]
                        for ddi in self.sequential_ddis:
                            if (ddi["time"] < closest_ddi["time"]):
                                closest_ddi = ddi
                        closest_ddi_data = {}
                        closest_ddi_data["drug_one"] = drug_one
                        closest_ddi_data["drug_two"] = drug_two
                        closest_ddi_data["time"] = closest_ddi["time"]
                        closest_ddi_data["construct"] = closest_ddi["construct"]
                        return closest_ddi_data
                    break

            if not contains_ddi:
                closest_ddi_data = {}
                closest_ddi_data["drug_one"] = drug_one
                closest_ddi_data["drug_two"] = drug_two
                closest_ddi_data["time"] = "infinity"
                closest_ddi_data["construct"] = ""
                return closest_ddi_data
        else:
            print("Specified drugs cannot participate in a ddi")

    def get_drugs_list(self, node):
        drugsList = set()
        if node.children:
            for c in node.children:
                drugsList |= self.get_drugs_list(c)
        elif node.value.lower().startswith('take '):
            drugsList.add(node.value[5:])
        return drugsList

    def find_possible_ddis(self, node):
        mockDinto = MockDinto(filepath=default_csv_filepath, supress=True)
        drugsList = self.get_drugs_list(node)
        ddiDrugs = mockDinto.ddi_query(list(drugsList))
        return ddiDrugs

    def find_actual_ddis(self, node, first_drug, second_drug, minTime, duplicates):
        if node.children:
            # if 2 actions occur in parrelell with conflicting drugs a ddi is possible
            if node.construct == 'branch' or node.construct == 'process':
                contains_first_drug = []
                contains_second_drug = []
                for c in node.children:
                    local_drug_list = self.get_drugs_list(c)
                    if first_drug in local_drug_list:
                        contains_first_drug.append(c.name)
                    if second_drug in local_drug_list:
                        contains_second_drug.append(c.name)
                if contains_first_drug and contains_second_drug and \
                    not((len(contains_first_drug) == 1) and
                        (len(contains_second_drug) == 1) and contains_first_drug[0] == contains_second_drug[0]):
                    parallel_ddi = {}
                    parallel_ddi["first_drug"] = first_drug
                    parallel_ddi["second_drug"] = second_drug
                    parallel_ddi["construct"] = node.name
                    self.parallel_ddis.append(parallel_ddi)
            # sequence ddi check
            elif node.construct == 'sequence' or node.construct == 'iteration':
                for perm in self.permutations(node, first_drug, second_drug):
                    totalTime = self.evaluate_ddi(perm, first_drug, second_drug, minTime)
                    if totalTime >= 0 and totalTime < int(minTime):
                        sequential_ddi = {}
                        sequential_ddi["first_drug"] = first_drug
                        sequential_ddi["second_drug"] = second_drug
                        sequential_ddi["construct"] = node.name
                        sequential_ddi["time"] = totalTime
                        self.sequential_ddis.append(sequential_ddi)

                        if not duplicates:
                            break
            # run check on children nodes
            for c in node.children:
                self.find_actual_ddis(c, first_drug, second_drug, minTime, duplicates)

    def evaluate_ddi(self, path, first_drug, second_drug, minTime):
        if first_drug not in path or second_drug not in path:
            return -1
        last_drug = ''
        timesList = []
        localTimesList = []
        for element in path:
            if element.lower().startswith('wait ') and last_drug != '':
                localTimesList.append(element.lower())
            if element == first_drug:
                if last_drug == first_drug:
                    localTimesList = []
                if last_drug == second_drug:
                    timesList.append(localTimesList)
                    localTimesList = []
                last_drug = first_drug
            if element == second_drug:
                if last_drug == first_drug:
                    timesList.append(localTimesList)
                    localTimesList = []
                if last_drug == second_drug:
                    localTimesList = []
                last_drug = second_drug

        time_parser = PeosSimpleTimeParser()
        sorted_parsed_time = sorted(list(map(time_parser.get_total_time_delay_for_list, timesList)))
        if len(sorted_parsed_time) > 0 and sorted_parsed_time[0] < int(minTime):
            return sorted_parsed_time[0]
        else:
            return -1

    # Converts any standard time unit to seconds.
    def convert_time_to_secs(self, time_value, time_unit):
        if (time_unit == "sec"):
            return time_value
        elif (time_unit == "min"):
            return time_value * 60
        elif (time_unit == "hour"):
            return time_value * 60 * 60
        elif (time_unit == "day"):
            return time_value * 60 * 60 * 24
        elif (time_unit == "week"):
            return time_value * 60 * 60 * 24 * 7

        return -1

    def permutations(self, node, first_drug, second_drug):
        permutationsList = []
        if node.children:
            if node.construct == 'branch' or node.construct == 'process':
                for c in node.children:
                    permutationsList = self.combine_parallel(permutationsList, self.permutations(c, first_drug, second_drug))
            elif node.construct == 'selection':
                for c in node.children:
                    permutationsList = permutationsList + self.permutations(c, first_drug, second_drug)
            else:
                for c in node.children:
                    permutationsList = self.combine_seq(permutationsList, self.permutations(c, first_drug, second_drug))
                if node.construct == 'iteration':
                    morePerms = permutationsList
                    for c in node.children:
                        morePerms = self.combine_seq(morePerms, self.permutations(c, first_drug, second_drug))
                    permutationsList = permutationsList + morePerms
        else:
            if (node.value.lower().startswith('wait ')):
                permutationsList.append([node.value])
            elif node.value.lower().startswith('take ') and (node.value[5:] == first_drug or node.value[5:] == second_drug):
                permutationsList.append([node.value[5:]])
        return permutationsList

    # combines two events in sequence.
    # combine_seq([[abc][bac],[[ef][fe]]):
    #    results:
    #    [[abcef][abcfe][bacef][bacfe]]
    def combine_seq(self, list1, list2):
        result = []
        if not list1:
            return list2
        if not list2:
            return list1
        for lst1 in list1:
            for lst2 in list2:
                result.append(lst1+lst2)
        return result

    # combines to list of lists to generate all of the possible ways to combine the lists within them
    def combine_parallel(self, list1, list2):
        result = []
        if not list1:
            return list2
        if not list2:
            return list1
        for lst1 in list1:
            for lst2 in list2:
                result = result + self.in_order_combinations(lst1, lst2)
        return result

    # eg in_order_combinations(['a','b'], ['c','d'])
    # returns [
    # ['c', 'd', 'a', 'b']
    # ['c', 'a', 'd', 'b']
    # ['c', 'a', 'b', 'd']
    # ['a', 'c', 'd', 'b']
    # ['a', 'c', 'b', 'd']
    # ['a', 'b', 'c', 'd'] ]
    def in_order_combinations(self, lst1, lst2):
        permutations = []
        for locations in itertools.combinations(range(len(lst1) + len(lst2)), len(lst2)):
            result = lst1[:]
            for location, element in zip(locations, lst2):
                result.insert(location, element)
            permutations.append(result)
        return permutations

def sharedFlags(subparser):
    subparser.add_argument('-p', '--path', required=False, default=default_filepath, help='Path to PML file.')
    subparser.add_argument('-v', '--verbose', nargs='?', required=False, help='Prints the Tree Representation of the given PML file')
    subparser.add_argument('-m', '--mockdinto', required=False, default=default_csv_filepath, help='Path to CSV file.')

def clargs():
    parser = argparse.ArgumentParser(description='Parses a pml file to search within for DDIs.')
    sub_parser = parser.add_subparsers(dest='cmd')
    sub_parser.required = True

    # Parallel DDIs sub parser
    parallel = sub_parser.add_parser('parallel', help='Performs a query to find the parallel DDIs in the specified PML.')
    sharedFlags(parallel)

    # Sequential DDIs sub parser
    sequential = sub_parser.add_parser('sequential', help='Performs a query to find the sequential DDIs in the specified PML.')
    sharedFlags(sequential)

    # All DDIs sub parser
    all_query = sub_parser.add_parser('all', help='Performs a query to find all the DDIs in the specified PML.')
    sharedFlags(all_query)

    # Closest DDI sub parser
    closest = sub_parser.add_parser('closest', help="Performs a query to find the closest DDI between two drugs")
    closest.add_argument('-d', '--drugs', nargs='+', default=['coke', 'pepsi'], help='Drugs to find the closest interactions between.')
    sharedFlags(closest)

    # Save PML File sub parser
    saving = sub_parser.add_parser('save', help="Extracts content from a given source pml file and saves it to a destnation file.")
    saving.add_argument('--dest', nargs='?', help='Path to Destingation file')
    sharedFlags(saving)

    return parser.parse_args()

if __name__ == '__main__':
    args = clargs()

    if(args.mockdinto):
        default_csv_filepath = args.mockdinto

    pmlp = pmlParser.PmlParser(filename=args.path)
    ddiFinder = DdiFinder(pmlp.tree)

    if(args.verbose):
        print(pmlp)

    if(args.cmd == 'parallel' or args.cmd == 'all'):
        data = ddiFinder.get_parallel_ddis()
        if data == []:
            print("No parallel ddis found")
        else:
            for ddi_data in data:
                print("Parallel DDI found between {drug1} and {drug2} in {construct}. DDI type: {type}, DDI time: {time} {format}.".format(drug1=ddi_data["drug_one"], drug2=ddi_data["drug_two"], construct=ddi_data["construct"], type=ddi_data["type"], time=ddi_data["time"], format=ddi_data["time_unit"]))
    if(args.cmd == 'sequential' or args.cmd == 'all'):
        data = ddiFinder.get_sequential_ddis()
        if data == []:
            print("No sequential ddis found")
        else:
            for ddi_data in data:
                print("Sequential DDI found between {drug1} and {drug2} in {construct}. DDI type: {type}, DDI time: {time} {format}.".format(drug1=ddi_data["drug_one"], drug2=ddi_data["drug_two"], construct=ddi_data["construct"], type=ddi_data["type"], time=ddi_data["time"], format=ddi_data["time_unit"]))
    if(args.cmd == 'closest'):
        closest_ddi_data = ddiFinder.find_closest_ddis(args.drugs)
        if closest_ddi_data['construct'] is not "":
            print("Closest ddi approach for '%s' and '%s' is %s sec in construct '%s'" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"], closest_ddi_data["construct"]))
        elif closest_ddi_data['time'] is not "infinity":
            print("Closest ddi approach for '%s' and '%s' is %s sec" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"]))
        else:
            print("Closest ddi approach for '%s' and '%s' is %s" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"]))
    if(args.cmd == 'save'):
        pmlp.save_pml(args.path, args.dest)


# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

from msrest.serialization import Model

class DataTableResponseObject(Model):
    """Data Table which defines columns and raw row values.

    :param table_name: Name of the table
    :type table_name: str
    :param columns: List of columns with data types
    :type columns: list[~azure.mgmt.web.models.DataTableResponseColumn]
    :param rows: Raw row values
    :type rows: list[list[str]]
    """

    _attribute_map = {
        'table_name': {'key': 'tableName', 'type': 'str'},
        'columns': {'key': 'columns', 'type': '[DataTableResponseColumn]'},
        'rows': {'key': 'rows', 'type': '[[str]]'},
    }

    def __init__(self, *, table_name: str=None, columns=None, rows=None, **kwargs) -> None:
        super(DataTableResponseObject, self).__init__(**kwargs)
        self.table_name = table_name
        self.columns = columns
        self.rows = rows


import pandas as pd
import os, sys
import re
import TemplateTools as TT

def main():

    current_path = os.path.abspath(__file__)
    os.chdir(os.path.dirname(current_path))

    df_SER_raw_whole = pd.read_excel('sources/SER20190809.xlsx', sheet_name='SER export', header=[4])
    df_ESR_raw_whole = pd.read_excel('sources/Data Source 1- Main.xlsx', sheet_name='Data', header=[0])

    df_SER_providerType_selected = pd.read_excel(\
    'sources/Provider Type SER - selected.xlsx', sheet_name='Sheet1', header=[0])
    list_SER_providerType_selected = \
    df_SER_providerType_selected["Selected Provider Type"].tolist()

    df_SER_raw = df_SER_raw_whole[df_SER_raw_whole['Provider type'].isin(list_SER_providerType_selected)]
    df_ESR_raw = df_ESR_raw_whole[df_ESR_raw_whole['Staff Group'].isin(['Medical and Dental', 'Students'])]

    columnNames_SER = ['ID', 'Provider name', 'External Name', 'Sex', \
    'Rpt Grp One', 'Provider type', 'Provider specialty', \
    'Rpt Grp Six', 'MPI ID-Type', 'MPI ID']
    columnNames_ESR = ['SER File Name', 'SER External Name', 'Gender', \
    'Employee Number', 'Assignment No.', 'Position Title', 'Role', 'Area Of Work', \
    'Org L2', 'Org L3', 'Org L4', 'Org L5', \
    'Professional Registration Num']

    df_SER = df_SER_raw[columnNames_SER]
    df_ESR = df_ESR_raw[columnNames_ESR]

    df_SER['Rpt Grp One'] = df_SER['Rpt Grp One'].apply(cleanID)
    df_ESR['Employee Number'] = df_ESR['Employee Number'].apply(cleanID)

    df_SER_providerName_toCheckEmNum = df_SER.loc[pd.isna(df_SER['Rpt Grp One']), 'Provider name']
    list_SER_providerName_toCheckEmNum = df_SER_providerName_toCheckEmNum.drop_duplicates().tolist()
    df_SER_toCheckEmNum = df_SER[df_SER['Provider name'].isin(list_SER_providerName_toCheckEmNum)]
    df_SER_remain = df_SER[~df_SER['Provider name'].isin(list_SER_providerName_toCheckEmNum)]
    df_SER_toCheckEmNum = df_SER_toCheckEmNum.sort_values(by='Provider name')

    dupIndicated = df_SER_remain.duplicated('Rpt Grp One', keep=False)
    df_SER_dup = df_SER_remain[dupIndicated]
    df_SER_remain = df_SER_remain[~dupIndicated]

    dupIndicated = df_ESR.duplicated('Employee Number', keep=False)
    df_ESR_dup = df_ESR[dupIndicated]
    df_ESR_remain = df_ESR[~dupIndicated]

    df_SER_remain['Key'] = df_SER_remain.apply(lambda row: \
    (row['Provider name'], row['External Name'], row['Rpt Grp One']), axis=1)

    df_ESR_remain['Key'] = df_ESR_remain.apply(lambda row: \
    (row['SER File Name'], row['SER External Name'], row['Employee Number']), axis=1)

    res = pd.merge(df_SER_remain, df_ESR_remain, \
    # left_on=['Rpt Grp One'], right_on=['Employee Number'], \
    on='Key',
    how='outer', indicator=True, validate='one_to_one')

    res_both = res[res['_merge'].isin(['both'])]
    res_SER = res[res['_merge'].isin(['left_only'])]
    res_ESR = res[res['_merge'].isin(['right_only'])]

    res_SER = res_SER[columnNames_SER]
    res_ESR = res_ESR[columnNames_ESR]

    res_both['IfHasThisRegNumInSER'] = res_both.apply(lambda row: \
    checkID( \
    str(row['MPI ID']), str(row['Professional Registration Num']) \
    ), axis=1)

    res_both_HasRegNum = res_both[res_both['IfHasThisRegNumInSER'].isin([True])]

    res_both['TemplateGene'] = res_both.apply(lambda row: \
    TT.getTemplateIndirect( \
    str(row['Position Title']) + ' ' + \
    str(row['Role']) + ' ' + str(row['Area Of Work']) \
    # , str(row['Org L5'])
    ), axis=1)

    res_both['IsSameTemplate'] = res_both.apply(lambda row: \
    str(row['TemplateGene']) == str(row['Rpt Grp Six']), axis=1)

    res_both_HasSameTemplate = res_both[res_both['IsSameTemplate'].isin([True])]

    res_both = res_both[[
    'ID', 'Provider name', 'External Name', 'Sex', \
    'Provider type', 'Provider specialty', \
    'Rpt Grp One', 'MPI ID-Type', 'MPI ID', \
    'Rpt Grp Six', 'TemplateGene', 'IsSameTemplate', \
    'Key', \
    'SER File Name', 'SER External Name', 'Gender', \
    'Position Title', 'Role', 'Area Of Work', \
    'Org L2', 'Org L3', 'Org L4', 'Org L5', \
    'Assignment No.', 'Employee Number', 'Professional Registration Num', \
    'IfHasThisRegNumInSER' \
    ]]

    summary = pd.Series([ \
    'Input source files: ' + 'SER ({} rows), ESR ({} rows).'.format(len(df_SER_raw_whole), len(df_ESR_raw_whole)),
    'Choose the subset of SER ({} rows): '.format(len(df_SER)) + 'from the selected provider types.',
    'Choose the subset of ESR ({} rows): '.format(len(df_ESR)) + '"Medical and Dental", "Students" in "Staff Group".',

    'Remove SER rows (those provider names with empty "Rpt Grp One" ' \
    + '(i.e. Employee Number)) as shown in the tab SER_ToCheck ' \
    + '({} rows) ;remaining {} SER rows.'\
    .format(len(df_SER_toCheckEmNum), len(df_SER_remain)),

    'Process "Employee Number" in ESR ("Rpt Grp One" in SER) by deleting digits after "-".',

    'Remove duplicated rows (i.e. multiple rows with same Employee Number) as shown in two tabs: ' \
    + 'SER_DUP ({} rows), ESR_DUP ({} rows).'.format(len(df_SER_dup), len(df_ESR_dup)),

    'Match on the Key: SER ("Provider name", "External Name", "Rpt Grp One") '
    + ', ESR ("SER File Name", "SER External Name", "Employee Number").',

    'Join non-duplicated rows which results in three tabs: ' \
    + 'IN_BOTH ({} rows), SER_ONLY ({} rows), ESR_ONLY ({} rows).'.format(len(res_both), len(res_SER),
     len(res_ESR)),

    'Add "IfHasThisRegNumInSER" Column (to the tab IN_BOTH): ' \
    + 'True ({}/{}={}% rows) if "Professional Registration Num" (ESR) in "MPI ID" (SER).'\
    .format(len(res_both_HasRegNum), len(res_both), round(100*len(res_both_HasRegNum)/len(res_both),2) ),

    'Add "TemplateGene" Column (to the tab IN_BOTH): ' \
    + 'generated template (by the logic version 1.0) from the ESR info ("Position Title", "Role", "Area Of Work").',

    'Add "IsSameTemplate" Column (to the tab IN_BOTH): ' \
    + 'True ({}/{}={}% rows) if "TemplateGene" (ESR) is same with "Rpt Grp Six" (SER).'\
    .format(len(res_both_HasSameTemplate), len(res_both), round(100*len(res_both_HasSameTemplate)/len(res_both),2) ),
    ])

    summary.name = 'Analysis Process'

    with pd.ExcelWriter('results/output.xlsx') as writer:
        res_both.to_excel(writer, sheet_name='IN_BOTH')
        res_SER.to_excel(writer, sheet_name='SER_ONLY')
        res_ESR.to_excel(writer, sheet_name='ESR_ONLY')
        df_SER_dup.to_excel(writer, sheet_name='SER_DUP')
        df_ESR_dup.to_excel(writer, sheet_name='ESR_DUP')
        df_SER_toCheckEmNum.to_excel(writer, sheet_name='SER_ToCheck')
        summary.to_excel(writer, sheet_name='SUMMARY')

def cleanID(thisOneIn):
    thisOne = thisOneIn
    if pd.isna(thisOne):
        return thisOne
    else:
        try:
            thisOne = int(thisOne)
        except:
            pass
        else:
            thisOne = str(thisOne)
        thisIndex = thisOne.find('-')
        if thisIndex != -1:
            return thisOne[:thisIndex]
        else:
            return thisOne

def checkID(stringSER, stringESR):
    if re.search(stringESR, stringSER, re.I):
        return True
    else:
        return False

if __name__ == '__main__':
    print('********** Scripts start. **********')
    main()
    print('********** Scripts end. **********')


import wiringpi
import time

servo_gpio_pin = 23
_max = 200
first = 50

print('setup results: {}'.format(wiringpi.wiringPiSetupGpio()))

def setup_and_test(pin, small, large, first):
    print('{} {} {}'.format(small, large, first))

    print('creating pin')
    cr = wiringpi.softPwmCreate(pin, small, large)
    print('Create results: {}'.format(cr))

    print('first write')
    wres = wiringpi.softPwmWrite(servo_gpio_pin, first)
    print('Write res {}'.format(wres))

    time.sleep(1)
    print('done sleeping')
    wiringpi.softPwmWrite(servo_gpio_pin, small)

# for l in range(0, 1000, 50):
#     setup_and_test(servo_gpio_pin, 0, l, int(l/2))
setup_and_test(servo_gpio_pin, 0, 200, 50)

def big_test_a_bunch_of_ranges():
    def testrange(top):
        for i in range(0, top, int(top/10)):
            print('sending {}'.format(i))
            wiringpi.softPwmWrite(servo_gpio_pin, i)
            time.sleep(1)

    for k in range(10, 1000, 50):
        time.sleep(2)
        print('creating pin', k)
        wiringpi.softPwmCreate(17, 0, k)
        testrange(k)


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 6 13:57:58 2018

@author: Tianshu
"""
import numpy as np
import pickle
from SWCto3D import SWCto3D
import cv2

with open('config.pkl','rb') as f:
	viewpoints,fileNames,testFileNames,part,resolution=pickle.load(f)

for fileName in (fileNames):
	print("Processing file:" + fileName )
	metedata,matrix,images2D = SWCto3D("input/" + fileName + ".CNG.swc",resolution,part,viewpoints)
	np.save("matrix3D/matrix3D_half_" + fileName + '_' +part + '_' + str(resolution),matrix)

for i in range( len(images2D)):
		image = images2D[i]
		viewpoint = viewpoints[i]
		#cv2.imshow( "2Dimages/image" + fileName + "_" + str(viewpoint) + '_' + part + '_' + str(resolution) + '.png', image)
		cv2.imwrite( "2Dimages/image_half_" + fileName + "_" + str(viewpoint) + '_' + part + '_' + str(resolution) + '.png', image)

cv2.waitKey(0)
cv2.destroyAllWindows()


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

import os
import random

origin_path = '../../../data/VOCdevkit/SAR_ship/'
h5_path = '../../../data/VOCdevkit/SAR_high_quality_datasets/h5/'
h6_path = '../../../data/VOCdevkit/SAR_high_quality_datasets/h6/'

origin_img_path = origin_path + 'JPEGImages'
h5_train = h5_path + 'train/JPEGImages'
h5_test = h5_path + 'test/JPEGImages'

origin_list = os.listdir(origin_img_path)
h5_train_list = os.listdir(h5_train)
h5_test_list = os.listdir(h5_test)

# print(origin_list)
print(len(origin_list), len(h5_train_list), len(h5_test_list))

new = []
for i in origin_list:
    if i not in h5_train_list and i not in h5_test_list:
        new.append(i)

random.shuffle(new)
print(len(new))

split = 0.8
new_train = new[:int(len(new)*split)]
new_test = new[int(len(new)*split):]
print(len(new_train), len(new_test))

h5_train_list.extend(new_train)
h5_test_list.extend(new_test)

print(len(h5_train_list), len(h5_test_list))

split = 7.0/8

train_list = h5_train_list[:int(len(h5_train_list)*split)]
val_list = h5_train_list[int(len(h5_train_list)*split):]
test_list = h5_test_list

print(len(train_list), len(val_list), len(test_list))
print('----------')

# 使用全数据集
new = origin_list
split = 0.9

new_train = new[:int(len(new)*split)]
new_test = new[int(len(new)*split):]
print(len(new_train), len(new_test))

split = 7.0/9

train_list = new_train[:int(len(new_train)*split)]
val_list = new_train[int(len(new_train)*split):]
test_list = new_test
print(len(train_list), len(val_list), len(test_list))

f = open(h6_path+'train.txt', 'w')
for i in train_list:
    i = i.replace('.jpg', '')
    f.write(i+'\n')
f.close()

f = open(h6_path+'./valid.txt', 'w')
for i in val_list:
    i = i.replace('.jpg', '')
    f.write(i+'\n')
f.close()

f = open(h6_path+'./test.txt', 'w')
for i in test_list:
    i = i.replace('.jpg', '')
    f.write(i+'\n')
f.close()


from ..udf import concat_string_arrays, union_all
from ...libraries import *

class Profiler(object):
    def __init__(self, spark, customers, date_range, indicator=[], granularity="Period"):
        """
            This is a class that combines multiple customer classes.

            :param spark: spark initialization object
            :param customers: Customer class
            :param indicator: Boolean
            :param granularity: string ("Period", "Week", "Day")
            :return: Profiler class object

            Examples:
            >>> cust_prof = cp.Profiler(spark,
            >>>         [
            >>>           caramel_cloud,
            >>>           cinnamon_cloud
            >>>         ],
            >>>         date_range = ("2018-02-26", "2019-05-26")
            >>> )
        """
        self.pf_spdf = union_all(*[a.pf_spdf for a in customers])
        self.start_dates_pd = [str(a.start_dates_pd) for a in customers]
        self.end_dates_pd = [str(a.end_dates_pd) for a in customers]
        self.products_names = [a.products_names for a in customers]
        self.level = [a.level for a in customers]
        self.products_id = [a.products_id for a in customers]
        self.pch_frq_min = [a.pch_frq_min for a in customers]
        self.pch_frq_max = [a.pch_frq_max for a in customers]
        self.date_range = date_range
        self.spark = spark
        self.indicator = indicator

        if granularity == "Period":
            date_granularity = ["pos.FiscalYearNumber", "pos.FiscalQuarterInYearNumber", "pos.FiscalPeriodInYearNumber"]
        elif granularity == "Week":
            date_granularity = ["pos.FiscalYearNumber", "pos.FiscalQuarterInYearNumber", "pos.FiscalPeriodInYearNumber", "pos.FiscalWeekInYearNumber"]
        else:
            date_granularity = [
                "pos.FiscalYearNumber",
                "pos.FiscalQuarterInYearNumber",
                "pos.FiscalPeriodInYearNumber",
                "pos.FiscalWeekInYearNumber",
                "pos.BusinessDate"
            ]

        self.var = (
            date_granularity +
            [
                "Customer_Type",
                "pos.Id",
                "P30_Trans_Freq",
                "Product_Cohort",
                "ProductTypeDescription",
                "ProductTypeId",
                "ProductCategoryDescription",
                "ProductCategoryId",
                "ProductStyleDescription",
                "ProductStyleId",
                "NotionalProductDescription",
                "NotionalProductlId",
                "MarketedProductDescription",
                "MarketedProductid",
                "SellableItemDescription",
                "SellableItemId",
                "pos.NetDiscountedSalesAmount",
                "pos.TransactionId",
                "TransactionLineNumber",
                "ItemNumber",
                "ItemDescription",
                "ContainerId",
                "ContainerChildSequenceNumber",
                "pos.GrossLineItemQty",
                "pos.NetDiscountedSalesQty",
                "pos.LoyaltyMemberTenureDays"
             ]
        )
        self.grp_var = ["ind.Id", "ind.P30_Trans_Freq"]

        if self.indicator:
            self.var.extend(["ind." + i for i in self.indicator])
            self.grp_var.extend(["ind." + i for i in self.indicator])

        # POS
        self.pos = (
            self.spark
            .table("fkwan.pos_line_item")
            .filter(
                sqlf.col("AccountId").isNotNull() |
                sqlf.col("FirstPaymentToken").isNotNull()
            )
            .filter(sqlf.col("BusinessDate").between(self.date_range[0], self.date_range[1]))
            .withColumn(
                "Customer_Type",
                sqlf.when(
                    sqlf.col("AccountId").isNotNull(), "SR"
                )
                .otherwise("Token")
            )
            .withColumn(
                "Id",
                sqlf.when(
                    sqlf.col("AccountId").isNotNull(), sqlf.col("AccountId")
                )
                .otherwise(sqlf.col("FirstPaymentToken"))
            )
            .withColumn(
                "ProductCategoryDescription",
                sqlf.when(
                    sqlf.col("ProductTypeDescription").isin(["Food", "Beverage"]),
                    sqlf.col("ProductCategoryDescription")
                )
                .otherwise("Other")
            )
            .withColumn(
                "ProductStyleDescription",
                sqlf.when(
                    sqlf.col("ProductTypeDescription").isin(["Food", "Beverage"]),
                    sqlf.col("ProductStyleDescription")
                )
                .otherwise("Other")
            )
        )

        # Join Nutrition and Customer Segmentation
        self.pos = (
            self.pos
            .alias("pos")
            .join(
                spark
                .table("edap_pub_customer.customer360_behavior_restricted")
                .alias("cust"),
                sqlf.col("pos.Id") == sqlf.col("cust.AccountId"),
                how="left"
            )
            .select([
                "pos.*",
                "cust.LoyaltyMemberTenureDays"
            ])
        )

    def details(self):
        """
            This method generates customer profile cohort details.

            :param spark: spark initialization object
            :return: spark dataframe

            Examples:
            >>> cust_prof.details()
        """
        cols = ["Details"] + self.products_names
        cols = [re.sub("\s", "_", i) for i in cols]
        fields = [(StructField(field, StringType(), True)) for field in cols]
        schema = StructType(fields)

        df = pd.DataFrame(
            [
                ["Cohort Start Date"] + self.start_dates_pd,
                ["Cohort End Date"] + self.end_dates_pd,
                ["EPH Level"] + self.level,
                ["Min Units Purchased"] + self.pch_frq_min,
                ["Max Units Purchased"] + self.pch_frq_max,
                ["Id"] + self.products_id
            ],
            columns=cols
        )
        return self.spark.createDataFrame(df, schema)

    def overlap(self):
        """
            This method generates customer profile with overlap product segments.

            :return: spark dataframe

            Examples:
            >>> cust_prof_overlap = cust_prof.overlap()
        """
        # Indicator
        exprs_ind = [
            (sqlf.max(
                sqlf.when(
                    ((sqlf.col("BusinessDate")
                      .between(str(self.start_dates_pd[i]), str(self.end_dates_pd[i]))) &
                     (sqlf.col(self.level[i]).isin(self.products_id[i]))
                     ), self.products_names[i])
                .otherwise(None)))
            .alias(re.sub("\s", "_", self.products_names[i])) for i in range(0, len(self.products_id))]

        ind = (
            self.pf_spdf
            .alias("ind")
            .join(
                self.pos.alias("pos"),
                sqlf.col("pos.Id") == sqlf.col("ind.Id"),
                how="left"
            )
            .groupBy(self.grp_var)
            .agg(*exprs_ind)
        )

        table_overlap = (
            self.pos.alias("pos")
            .join(
                ind.alias("ind"),
                sqlf.col("pos.Id") == sqlf.col("ind.Id"),
                how="inner"
            )
            .withColumn(
                "Product_Cohort",
                concat_string_arrays(*[re.sub("\s", "_", i) for i in self.products_names])
            )
            .select(self.var)
            .filter(sqlf.col("Product_Cohort") != '')
        )

        return table_overlap

    def individual(self):
        """
            This method generates customer profile with product segments.

            :return: spark dataframe

            Examples:
            >>> cust_prof_indv = cust_prof.individual()
        """
        ind = (
            self.pf_spdf
            .alias("ind")
            .select(self.grp_var + ["Product_Cohort"])
            .distinct()
        )

        table_a = (
            self.pos.alias("pos")
            .join(
                ind.alias("ind"),
                sqlf.col("pos.Id") == sqlf.col("ind.Id"),
                how="inner"
            )
            .select(self.var)
            .filter(sqlf.col("Product_Cohort") != '')
        )

        return table_a

    def proportion(self, rng=[(1/3), (2/3)]):
        """
        This method generates customer profile with dominant proportion.

        :param rng: array with cutoffs
        :return: spark dataframe

        Examples:
        >>> cust_prof_proportion = cust_prof.proportion(rng=[(1/3), (2/3)])
        """
        if len(self.products_names) != 2:
            return print("This function can only accept Profiler with 2 customers.")

        self.var.remove("Product_Cohort")

        proportion = (
            self.pf_spdf.alias("ind")
            .join(
                (
                    self.pf_spdf
                    .filter(sqlf.col("Product_Cohort") == self.products_names[0])
                )
                .alias("A"),
                sqlf.col("ind.Id") == sqlf.col("A.Id"),
                how="left"
            )
            .join(
                (
                    self.pf_spdf
                    .filter(sqlf.col("Product_Cohort") == self.products_names[1])
                )
                .alias("B"),
                sqlf.col("ind.Id") == sqlf.col("B.Id"),
                how="left"
            )
            .withColumn(
                "Proportion",
                (sqlf.coalesce(sqlf.col("A.Qty"), sqlf.lit(0.0))/
                (sqlf.coalesce(sqlf.col("B.Qty"), sqlf.lit(0.0))+sqlf.coalesce(sqlf.col("A.Qty"), sqlf.lit(0.0))))
            )
            .select(self.grp_var + ["Proportion"])
            .distinct()
            .where(sqlf.col("Proportion").isNotNull())
        )

        cohort = (
            proportion
            .withColumn(
                "Product_Cohort",
                sqlf.when(
                    sqlf.col("Proportion") <= rng[0],
                    self.products_names[1] + " Dominant"
                )
                .when(
                    sqlf.col("Proportion") >= rng[1],
                    self.products_names[0] + " Dominant"
                )
                .otherwise("Not Dominant")
            )
            .select(self.grp_var + ["Proportion", "Product_Cohort"])
            .distinct()
        )

        result = (
            self.pos.alias("pos")
            .join(
                cohort.alias("ind"),
                sqlf.col("pos.Id") == sqlf.col("ind.Id"),
                how="inner"
            )
            .withColumn(
                "Cohort_2",
                sqlf.when(
                    sqlf.col("ind.Proportion") == 1,
                    self.products_names[0] + " only"
                )
                .when(
                    sqlf.col("ind.Proportion") == 0,
                    self.products_names[1] + " only"
                )
                .otherwise(sqlf.col("ind.Product_Cohort"))
            )
            .select(self.var + ["Proportion", "Product_Cohort", "Cohort_2"])
        )

        return result


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import gym
import numpy as np
import ray

ray.init(num_cpus=10,redirect_output =True)
# ray.init()
class TwoLayerPolicy(object):
    def __init__(self, num_inputs, num_hiddens, num_outputs=1):
        self.num_inputs = num_inputs
        self.num_hidden_units = num_hiddens
        self.num_outputs = num_outputs
        self.weights1 = np.random.normal(size=(num_hiddens, num_inputs))
        self.biases1 = np.random.normal(size=num_hiddens)
        self.weights2 = np.random.normal(size=(num_outputs, num_hiddens))
        self.biases2 = np.random.normal(size=num_outputs)

    def __call__(self, state):
        hiddens = np.maximum(np.dot(self.weights1, state) + self.biases1, 0)
        output = np.dot(self.weights2, hiddens) + self.biases2
        assert output.size == 1
        return 0 if output[0] < 0 else 1

policy = TwoLayerPolicy(4, 5)
# You can get an action by applying the policy to a state.
action = policy(np.random.normal(size=4))
print(action)

# NOTE: You may find the helper function 'rollout_policy' helpful.
# This implementation here is the solution to one of the exercises
# from the previous notebook.
def rollout_policy(env, policy):
    state = env.reset()
    cumulative_reward = 0
    done = False

    # Keep looping as long as the simulation has not finished.
    while not done:
        # Choose an action.
        action = policy(state)
        # Take an action.
        state, reward, done, _ = env.step(action)
        # Update the cumulative reward.
        cumulative_reward += reward
    return cumulative_reward

@ray.remote
def evaluate_random_policy(num_rollouts):
    # Generate a random policy.
    policy = TwoLayerPolicy(4, 5)

    # Create an environment.
    env = gym.make('CartPole-v0')

    # We evaluate the same policy multiple times and then take the average
    # in order to evaluate the policy more accurately (the environment is
    # stochastic).
    starttime = time.time()
    x= np.mean([rollout_policy(env, policy) for _ in range(num_rollouts)])
    p =0
    for i in range(1000000):
        p = p+1
    endtime  = time.time()
    # print ("duration is ",endtime-starttime)
    return x,endtime-starttime

average_reward = ray.get(evaluate_random_policy.remote(10))
print(average_reward)

# p =
# Evaluate 100 randomly generated policies.
x =[None]* 200
y = [None]*200
# raise NotImplementedErr
gstart = time.time()
for i in range(len(x)):
    x[i] = evaluate_random_policy.remote(10)
# f ,_ = ray.wait(x,10)
x = ray.get(x)
gend = time.time()
gduration = gend-gstart
# Print the best score obtained.
# raise NotImplementedError
for i in range(200):
    y[i] = x[i][1]
    x[i] = x[i][0]

print ("global time is",gduration)
print ("actural time is ", np.mean(y))
print ('time difference is',gduration-np.mean(y))


# file: absolute.py
# !/usr/bin/python
import requests
from PyQt6.QtCore import QThread, pyqtSignal
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QWidget, QLabel, QApplication, QTextEdit, QPushButton, QLineEdit, QFileDialog

import ReadExec
import  sys

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.open_th = None
        self.th_get_im = None
        self.cwd = None
        self.n = 1
        self.all_page = 0
        self.rd = ReadExec.ReadExec()
        self.photo = QPixmap()
        self.name = "" #旧标题
        self.new_name = "" #新标题
        self.url = "" #图片链接

        self.th = MyThread(self)  # 重写的多线程对象

        self.init_ui()
        self.resize(1000, 500)  # 设置窗口大小
        self.move(100, 0)

    def change_page(self, sigin):
        self.button_next.setEnabled(False)
        self.button_up.setEnabled(False)
        self.img.setText("图片加载中...")
        #print("self.name1=", self.name)
        #print("self.name2=", self.new_name)
        #print("当标题发生改变时，触发保存函数")
        if self.name != self.new_name:
            #print("执行保存")
            self.save_excl()
        #print("sigin=", sigin)
        if sigin == "next":
            #print("执行get_next")
            self.n = self.n + 1
        if sigin == "up":
            #print("执行get_up")
            self.n = self.n - 1
        if sigin == "rest":
            pass
            #print("执行刷新")
        img_url, name = self.rd.get_image(self.n)
        self.name = name
        self.lb_name.setText(name)  # 设置文本内容
        self.page_name.setText(str(self.n))  # 设置当前页码
        self.th_get_im = GetTh(img_url, self.n, self.all_page)
        self.th_get_im.start()  # 启动线程
        self.th_get_im.signal_photo.connect(self.change_img)  # 接受来自线程的图片数据
        self.th_get_im.signal_next.connect(self.change_next)
        self.th_get_im.signal_up.connect(self.change_up)
        self.th_get_im.exit()

    def change(self, msg):
        self.lb_name.setText(msg)

    def change_img(self, msg):
        #print("图片数据:", msg)
        if msg["msg"]=="no":
            self.img.setText("图片加载失败")
        else:
            self.img.setPixmap(msg["im"])  # 设置图片

    def change_next(self, msg):
        self.button_next.setEnabled(msg)

    def change_up(self, msg):
        self.button_up.setEnabled(msg)

    def save_excl(self):
        tx = self.lb_name.toPlainText()
        self.size_name.setText(str(len(tx)))
        self.rd.sava_f(self.n, tx)

    def init_ui(self):
        self.setMaximumSize(1000, 1200)
        # 显示图片
        self.img = QLabel("", self)
        self.img.move(270, 5)  # 位置
        self.img.setFixedSize(250, 250)  # 大小
        self.img.setScaledContents(True)  # 设置图片自适应窗口大小
        # 商品标题
        lb_name1 = QLabel("标题:", self)
        lb_name1.move(20, 270)
        # 标题
        self.lb_name = QTextEdit(self)
        self.lb_name.setPlaceholderText("标题")
        self.lb_name.move(60, 270)
        self.lb_name.setFixedSize(500, 100)  # 大小
        self.lb_name.textChanged.connect(self.th.run)  # 当内容发生变化时触发此信号，
        # 显示标题长度
        self.size_name_t = QLabel("标题长:", self)
        self.size_name_t.move(561, 270)
        self.size_name = QLabel("0", self)
        self.size_name.move(620, 270)
        self.size_name.setFixedSize(40, 20)
        self.size_name.setText(str(len(self.lb_name.toPlainText())))
        # 显示页码
        self.page_t = QLabel("总页码:", self)
        self.page_t.move(563, 320)

        self.page_all = QLabel("0", self)
        self.page_all.move(620, 320)
        self.page_all.setFixedSize(40, 20)

        self.page_t2 = QLabel("当前:", self)
        self.page_t2.move(563, 350)
        self.page_t2.setFixedSize(40, 20)

        self.page_name = QLabel("0", self)
        self.page_name.move(620, 350)
        self.page_name.setFixedSize(40, 20)
        self.page_name.setText(self.page_name.text())
        # 保存按钮
        button_save = QPushButton("保存", self)
        button_save.setFixedSize(100, 50)
        button_save.move(650, 270)
        button_save.clicked.connect(self.save_excl)
        # 刷新
        button_res = QPushButton("刷新", self)
        button_res.setFixedSize(100, 50)
        button_res.move(750, 270)
        button_res.clicked.connect(lambda: self.change_page("rest"))
        # 下一条按钮
        self.button_next = QPushButton("下一条", self)
        self.button_next.setFixedSize(100, 50)
        self.button_next.move(650, 320)
        self.button_next.clicked.connect(lambda: self.change_page("next"))
        # 上一条按钮
        self.button_up = QPushButton("上一条", self)
        self.button_up.setFixedSize(100, 50)
        self.button_up.move(750, 320)
        self.button_up.clicked.connect(lambda: self.change_page("up"))
        # 文件选择区域
        # 标题
        path_name = QLabel("文件:", self)
        path_name.move(20, 456)
        # 单行文本
        self.path_edit = QLineEdit(self)
        self.path_edit.move(60, 450)
        self.path_edit.setFixedSize(500, 30)
        # 选择文件
        self.opt_path_button = QPushButton("选择文件", self)
        self.opt_path_button.clicked.connect(self.select_f)
        self.opt_path_button.move(570, 450)
        # 确定按钮
        self.ok_path_button = QPushButton("确定", self)
        self.ok_path_button.move(660, 450)
        self.ok_path_button.clicked.connect(self.ok_select_f)

        #测试按钮
        #self.test_button=QPushButton("测试",self)
        #self.test_button.move(720,450)
        #self.le=QLineEdit(self)

        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('亚马逊商品修改')
        self.show()

    def select_f(self):
        # 获取表格文件地址
        self.ok_path_button.setEnabled(True)
        f_name, _ = QFileDialog.getOpenFileName(self, "选取文件", self.cwd,  # 起始路径
                                                "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,用双分号间隔
        self.path_edit.setText(f_name)
        #print(f_name)

    def ok_select_f(self):
        path = self.path_edit.text()
        self.open_th = OpenExcel(path)

        # 启动线程用来加载文件
        self.open_th.start()  # 创建线程
        self.open_th.SignalExcel_data.connect(self.get_exec_data)  # 链接信号到函数 用函数接受来自线程的信号
        self.open_th.exit()

    def get_exec_data(self, msg):
        # 获取子线程读取的表格数据
        #print("getExecData  msg=", msg)
        self.rd = msg
        self.page_all.setText(str(self.rd.max_r))
        self.all_page = self.rd.max_r
        self.page_all.setText(str(self.all_page - 3))
        self.button_up.setEnabled(False)
        self.page_name.setText("1")
        self.n = 1
        self.change_page("rest")

class MyThread(QThread):
    def __init__(self, pa=None):
        super().__init__(pa)
        self.pa = pa

    def run(self):
        md = self.pa.lb_name.toPlainText()  # 获取标签上用户输入的内容。
        self.pa.new_name = md
        self.pa.size_name.setText(str(len(md)))  # 将获取的长度赋值给size_name
        pg = self.pa.page_name.text()  # 获取页码数
        self.pa.page_name.setText(str(pg))  # 把页码数设置到页码上

class GetTh(QThread):
    #print("go GetTh")
    signal = pyqtSignal(str)  # 定义信号
    signal_next = pyqtSignal(bool)
    signal_up = pyqtSignal(bool)
    signal_photo = pyqtSignal(dict)

    def __init__(self, img_url, n, all_page):
        super(QThread, self).__init__()
        self.photo = QPixmap()
        self.rd = ReadExec.ReadExec()  # 实例化
        self.img_url = img_url
        self.n = n
        self.all_page = all_page

    # 下载图片，下载完后显示图片
    def run(self):
        #print("go image")
        photo = QPixmap()
        # 获取图片链接和图片地址
        # 获取图片内容
        pho_msg = {"im": photo, "msg": ""}
        try:
            photo.loadFromData(requests.get(self.img_url,timeout=3).content)
            pho_msg = {"im": photo, "msg": "ok"}
            self.signal_photo.emit(pho_msg)  # 把图片发送到主框架并显示出来
        except Exception as e:
            #print(e)
            pho_msg["msg"] = "no"
            self.signal_photo.emit(pho_msg)
            self.exit()
        # 恢复按钮状态
        #print("self.n=", self.n)
        #print("self.all_page - 3=", self.all_page - 3)
        if 1 < self.n < self.all_page - 3:
            self.signal_next.emit(True)
            self.signal_up.emit(True)
        if self.n == self.all_page - 3:
            self.signal_next.emit(False)
            self.signal_up.emit(True)
        if self.n == 1:
            self.signal_next.emit(True)
            self.signal_up.emit(False)
        #print("ok image\n\n")
        self.exit()

# 用来打开表格
class OpenExcel(QThread):
    SignalExcel_data = pyqtSignal(ReadExec.ReadExec)
    RE = ReadExec.ReadExec()  # 实例化

    def __init__(self, path):
        super(OpenExcel, self).__init__()
        self.path = path

    def __del__(self):
        self.wait()

    def run(self):
        #print("使用多线程==", self.path)
        self.RE.open_excl(self.path)  # 执行函数获得一系列数据
        self.SignalExcel_data.emit(self.RE)  # 把实列传入主函数

def main():
    app = QApplication(sys.argv)
    _ = MyApp()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()

工作工具/可视化修改亚马逊表格/main.py


from numpy import *
from math import log
import operator as op
def clcShEnt(dataset):
	nofd=len(dataset)
	classnum={}
	for i in dataset:
		if i[-1] not in classnum.keys():
			classnum[i[-1]]=0
		classnum[i[-1]] += 1
	ent=0
	for key in classnum:
		prob=float(classnum[key])/nofd
		ent -= prob*log(prob,2)
	return ent

def dataSpl(dataset,feature,value):
	returnset=[]
	m=0
	for i in dataset:
		if i[feature]==value:
			returnset.append(i[:feature])
			returnset[m].extend(i[feature+1:])
			m += 1
	return returnset

def choFea(dataset):
	numfea=len(dataset[0])-1
	selfea=-1
	bestinfogain=0
	baseent=clcShEnt(dataset)
	for i in range(numfea):
		fealist=set([j[i] for j in dataset])
		newent=0
		for m in fealist:
			subdataset=dataSpl(dataset,i,m)
			pro=len(subdataset)/float(len(dataset))
			newent += pro*clcShEnt(subdataset)
		infogain=baseent-newent
		if infogain>bestinfogain:
			bestinfogain=infogain
			selfea=i
	return selfea

def classchoose(classnum):
	classcount={}
	for i in classnum:
		if i not in classcount.keys(): classcount[i]=0
		classcount[i] += 1
	sortedclasscount = sorted(classcount.iteritems(),key=op.itemgetter(1),reverse=1)
	return sortedclasscount[0][0]

def creattree(dataset,labels):
	classlist=[i[-1] for i in dataset]
	if classlist.count(classlist[0])==len(classlist):
		return classlist[0]
	elif len(dataset)==1:
		return classchoose(classlist)
	bestfeature=choFea(dataset)
	print(labels)
	bestfealab=labels[bestfeature]
	tree={bestfealab:{}}
	del(labels[bestfeature])
	feavalue=set([i[bestfeature] for i in dataset])
	for m in feavalue:
		sublabel=labels[:]
		tree[bestfealab][m]=creattree(dataSpl(dataset,bestfeature,m),sublabel)
	return tree

def classify(intree,labels,instance):
	fstr=intree.keys()[0]
	index=labels.index(fstr)
	nextdict=intree[fstr]
	feat=instance[index]
	for key in nextdict.keys():
		if key==feat:
			if isinstance(nextdict[key],dict):
				classresult=classify(nextdict[key],labels,instance)
			else: classresult=nextdict[key]
	return classresult

def stotree(intree,filename):
	import pickle
	fw=open(filename,'w')
	pickle.dump(intree,fw)
	fw.close()
def gettree(filename):
	import pickle
	fr=open(filename,'r')
	return pickle.load(fr)


from blaze.expr import *
from blaze.expr.collections import *

def test_merge():
    e = Symbol('e', '3 * 5 * {name: string, amount: int, x: real}')
    expr = merge(name=e.name, y=e.x)

    assert set(expr.fields) == set(['name', 'y'])
    assert expr.y.isidentical(e.x.label('y'))


#!/usr/bin/python3.6

import os
import sys
from reaction import Reaction
from utilities import SetToString,SeqToString,SetFromList
from reaction_system import ReactionSystem
from interactive_process import InteractiveProcess

# define a reaction by providing the three sets as lists (it will be automatically converted in sets by the function)
# if the creation isn't right the function will return None
def MakeReaction(_name,_r,_i,_p):
    # pass the entities through lists data structure
    if type(_r) is not list or type(_i) is not list or type(_p) is not list:
        print(f'Please provide the sets as lists data type structure!')
        return None
    # NO ONE of the sets can be empty
    if (len(_r)==0 or len(_i)==0 or len(_p)==0):
        print(f'The reaction_{_name} cannot be created: some of the provided sets is empty')
        return None
    # construct the sets, being sure that each element is viewed as a string (if I would have converted directly
    # in this way: e.g. set([a,1]) the element 1 was been an int instead of a string '1')
    name = str(_name).lower()
    reactants = set()
    inhibitors = set()
    products = set()
    for r in _r:
        reactants.add(str(r).lower())
    for i in _i:
        inhibitors.add(str(i).lower())
    for p in _p:
        products.add(str(p).lower())
    # intersection between reactants and inhibitors must be empty
    if (not reactants.isdisjoint(inhibitors)): # true if the intersection IS NOT empty
        print(f'The reaction_{name} cannot be created: intersection between reactants and inhibitors is not empty')
        return None
    # create the reaction
    return Reaction(name,reactants,inhibitors,products)

# check if a reaction belong to a given nonempty set
def ReactionBelongTo(_r,_s):
    if type(_s) is not set:
        print(f"Please provide _s as a set data type! Can't tell you if the reaction_{_r.name} belong to _s")
        return False
    if len(_s)==0:
        print(f"You have given me an empty set, so the reaction_{_r.name} can't belong to _s")
        return False
    # be sure to consider the elements as string
    str_set = set()
    for elem in _s:
        str_set.add(str(elem))
    return _r.BelongTo(str_set)

# check if a reaction is enabled in a certain nonempty set
def ReactionEnabledBy(_r,_t):
    if type(_t) is not set:
        print(f"Please provide _t as a set data type! Can't tell you if the reaction_{_r.name} is enabled by _t")
        return False
    if len(_t)==0:
        print(f"You have given me an empty set, so the reaction_{_r.name} can't be enabled by _t")
        return False
    # be sure to consider the elements as string
    str_set = set()
    for elem in _t:
        str_set.add(str(elem))
    return _r.EnabledBy(str_set)

# Get the set result of reaction _r over the set _t
def GetResOfReaction(_r,_t):
    # be sure to consider the elements as string
    str_set = set()
    for elem in _t:
        str_set.add(str(elem).lower())
    if _r.EnabledBy(str_set):
        return _r.products
    else:
        return set()

# Get the result of a reaction system _rs over the set _t
def GetResOfReactionSystem(_rs,_t):
    # be sure to consider the elements as string
    str_set = set()
    for elem in _t:
        str_set.add(str(elem).lower())
    # firse check if _t is included into the BGSet
    if _rs.BGSetInclude(str_set):
        return SetToString(_rs.GetResOverT(str_set))
    else:
        return f"The set _s has entities not included in the reaction_system_{_rs.name}"

# Import a set of reactions from file
def LoadRSFromFile(_file):
    _file = open(_file)
    s = (_file.readline()[:-1]).split(',')
    reactions = []
    k = 1
    for line in _file:
        r = [line[:-1].split(';')[0]]
        r = r[0].split(',')
        i = [line[:-1].split(';')[1]]
        i = i[0].split(',')
        if line[-1]==f'\n':
            p = [line[:-1].split(';')[2]]
        else:
            p = [line.split(';')[2]]
        p = p[0].split(',')
        #print(r,i,p)
        reactions += [MakeReaction(k,r,i,p)]
        k += 1
    _file.close()
    return (s,reactions)

# return None if the creation of the rs isn't correct, provide the background set and the set of reactions
def MakeReactionSystem(_name,_bgset,_reactions):
    # pass the background set and the set of reactions as lists
    if type(_bgset) is not list or type(_reactions) is not list:
        print(f'Please provide the sets as lists data type structure!')
        return None
    # NO ONE of the sets can be empty
    if len(_bgset)==0 or len(_reactions)==0:
        print(f'The reaction_system_{_name} cannot be created: the provided background set and/or the set of reactions are empty')
        return None
    # construct the background set, being sure that each element is viewed as a string
    name = str(_name).lower()
    bgset = set()
    for e in _bgset:
        bgset.add(str(e).lower())
    # construct the set of reaction and check that it is included into the background set
    reactions = set()
    for reac in _reactions:
        if type(reac) is Reaction and reac != None:
            if not reac.BelongTo(bgset):
                print(f'some of reactions of the reaction system is not included into the backgroung set: rs definition violated!')
                return None
            else:
                reactions.add(reac) # note that Reaction type object has implemented the logic to prevent a duplicate in a set!
    # if there isn't been a return None, we can return the reaction system
    return ReactionSystem(name,bgset,reactions)

# return None if the creation of the interactive process isn't correct, provide the reaction system and the initial state
def MakeInteractiveProcess(_name,_rs,_c,_interactive_sequence=False):
    if type(_c) is not list:
        print(f'Please provide the context sequence as list of sets!')
        return None
    for s in _c:
        if type(s) is not set:
            print(f"Please provide the context sequence as list of sets!")
            return None
        if not _rs.BGSetInclude(s):
            print(f"Sorry a set of the context sequence do not belong to the background set of the reaction system")
            return None
    if len(_c)==0:
        print(f'The interactive_process_{_name} cannot be created: the provided initial state is empty')
        return None
    name = str(_name).lower()
    return InteractiveProcess(name,_rs,_c,_interactive_sequence)

# ---------------------------------------
# -----------------main------------------
# ---------------------------------------

# check to have provided the name of file in order to load the bgset and the reactions
if len(sys.argv)!=2:
    print("Please provide the name of the file to load the reaction system")
    sys.exit(0)

# build-up the reaction system from file specified by sys.argv[1]
s,reactions = LoadRSFromFile(sys.argv[1])
reaction_system_1 = MakeReactionSystem(1,s,reactions)

# do the choice between automatic and human-interactive
os.system("clear")
choice = -1
while choice!=1 and choice!=2 and choice!=0:
    print(reaction_system_1)
    choice = int(input(f"\nDo you want to do an interactive process with a human interaction? (type 1)\nOR\nDo you prefer to provide directly the entire context sequence? (type 2)\n(type 0 to exit)\n"))
if choice==0:
    sys.exit(0)

# automatic processation, give the initial state only or provide a full sequence of contexts
if choice == 2:
    # check for a correct input sequence
    c_in=""
    while c_in=="":
        c_in = input(f"\nInsert the context sequence C.\nPlease separe the entities with comma and the various step sets with semicolon, no spaces between them.\ne.g. if you want to insert a context sequence of two sets with the entities 1, 2 and 3 and the entities 4, 5 and 6 rispectively, type: 1,2,3;4,5,6\nnote: type '_' to insert an empty set\n")
        if c_in=="":
            print("INVALID INPUT")
    # split the input, separe sets and elements
    c_in = c_in.split(';')
    c = []
    for elem in c_in:
        if elem!='_':
            c += [SetFromList(elem.split(','))]
        else:
            c += [set()]
    # now just save the input context sequence into a string to print it correctly each iteration
    c_str = SeqToString(c) # otherwise each iter c is linked to c into the object
    # build the interactive process over the rs with the inserted context sequence
    interactive_process_1 = MakeInteractiveProcess(1,reaction_system_1,c)
    # print the initial situation
    os.system("clear")
    print(reaction_system_1)
    print(f"\nThe interactive process over the reaction_system_{reaction_system_1.name} with the following context sequence: C = {c_str}\nis the following:\n")
    print(interactive_process_1)
    # begin the steps
    while interactive_process_1.terminated==False:
        choice = input("\nPress ENTER to go ahead, q to quit\n")
        if choice=='q':
            sys.exit(0)
        # the inner method of the class to update the status
        interactive_process_1.GoAheadOneStep()
        # clear the screen and update the situation
        os.system("clear")
        print(reaction_system_1)
        print(f"\nThe interactive process over the reaction_system_{reaction_system_1.name} with the following context sequence: C = {c_str}\nis the following:\n")
        print(interactive_process_1)

# human interaction, every step define the context set
if choice == 1:
    # check for a correct input sequence
    c_in=""
    while c_in=="":
        c_in = input(f"\nInsert the initial state C0.\nPlease separe the entities with comma, no spaces between them.\ne.g. if you want to insert the initial state with the entities 1, 2 and 3, type: 1,2,3\n")
        if c_in=="":
            print("INVALID INPUT")
    # split the input, separe the elements (in this case the user must provide only the initial context set c0)
    c = [SetFromList(c_in.split(','))]
    # build the interactive process over the rs with the inserted context sequence
    c_str = SeqToString(c) # otherwise each iter c is linked to c into the object
    # build the interactive process over the rs with the inserted context sequence
    interactive_process_1 = MakeInteractiveProcess(1,reaction_system_1,c,True) # True indicates that the process is human-interactive
    # print the initial situation
    os.system("clear")
    print(reaction_system_1)
    print(f"\nThe interactive process over the reaction_system_{reaction_system_1.name} with the following initial state: C0 = {c_str}\nis the following:\n")
    print(interactive_process_1)
    # begin the steps
    while interactive_process_1.terminated==False:
        # the inner method of the class here handles the logic to require the user to insert the next set to use as context set in the rs
        interactive_process_1.GoAheadOneStep()
        # clear the screen and update the situation
        os.system("clear")
        print(reaction_system_1)
        print(f"\nThe interactive process over the reaction_system_{reaction_system_1.name} with the following initial state: C0 = {c_str}\nis the following:\n")
        print(interactive_process_1)