import os
import jinja2
import jinja2.environment
import jinja2.meta
import jinja2.nodes
from pydgeot.processors import register, Processor

@register(name='jinja')
class JinjaProcessor(Processor):
    """
    Compile a Jinja (http://jinja.pocoo.org) template source file in to the build directory.

    Context variables can be set using the 'setcontext' tag. This also works as Jinja's built in 'set' tag.
    File paths that have set context variables can be retrieved with 'getcontexts("name", "value")'.
    To mark a file as only being used as a template (no file will be generated for it,) use Jinja's built in 'set'
    to set the 'template_only' variable to True.
    """

    def __init__(self, app):
        from .extensions import SetContextExtension, get_contexts

        super().__init__(app)

        self._env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(self.app.source_root),
            extensions=[SetContextExtension])

        # Prepare template extensions
        for extension in self._env.extensions.values():
            extension.processor = self

        # Add template functions
        self._env.globals['getcontexts'] = get_contexts(self.app)

        # Set during prepare(), so extensions can get the processing file path
        self.current_path = None
        """:type: str | None"""
        self._generate_paths = {}
        """:type: dict[str, jinja2.nodes.Template]"""

    def can_process(self, path):
        """
        :type path: str
        :rtype: bool
        """
        from .dirconfig import DirConfig
        config = DirConfig.get(self.app, path)
        return path.endswith(config.source_ext)

    def prepare(self, path):
        """
        :type path: str
        """
        self.current_path = path
        self.app.contexts.clear_dependencies(path)
        self.app.contexts.remove_context(path)

        with open(path) as fh:
            ast = self._env.parse(fh.read())

        self.app.sources.set_targets(path, [self.target_path(path)])
        self.app.sources.set_dependencies(path, [self.app.source_path(t)
                                                 for t in jinja2.meta.find_referenced_templates(ast)])

        # Add new context dependencies (populated from get_contexts)
        for name, value in self._get_context_requests(ast):
            self.app.contexts.add_dependency(path, name, value)

        consts = self._get_const_vars(ast)
        template_only = consts.get('template_only', False)

        # Add this to the list of paths to be generated if it's not template only
        if not template_only:
            self._generate_paths[path] = ast

    def generate(self, path):
        """
        :type path: str
        """
        if path not in self._generate_paths:
            return

        target_path = self.target_path(path)
        template = jinja2.environment.Template.from_code(self._env, self._env.compile(self._generate_paths[path]),
                                                         self._env.make_globals(None))

        context_dict = {}
        source = self.app.sources.get_source(path)
        if source is not None:
            targets = self.app.sources.get_targets(path)
            if len(targets) == 1:
                context_dict['url'] = '/' + self.app.relative_path(list(targets)[0].path)
            context_dict['urls'] = ['/' + self.app.relative_path(target.path) for target in targets]
            context_dict['size'] = source.size
            context_dict['modified'] = source.modified

        os.makedirs(os.path.dirname(target_path), exist_ok=True)
        with open(target_path, 'w', encoding='utf-8') as fh:
            fh.write(template.render(**context_dict))
        del self._generate_paths[path]

    def target_path(self, path):
        """
        :type path: str
        :rtype: str
        """
        from .dirconfig import DirConfig
        config = DirConfig.get(self.app, path)
        path = path[:-len(config.source_ext)] + config.build_ext
        return self.app.target_path(path)

    @staticmethod
    def _get_const_vars(template):
        """
        :type template: jinja2.nodes.Template
        :rtype: dict[str, Any]
        """
        const_vars = {}
        for node in template.find_all((jinja2.nodes.Assign,)):
            if isinstance(node.target, jinja2.nodes.Name) and isinstance(node.node, jinja2.nodes.Const):
                # noinspection PyUnresolvedReferences
                const_vars[node.target.name] = node.node.value
        return const_vars

    @staticmethod
    def _get_context_requests(template):
        """
        :type template: jinja2.nodes.Template
        :rtype: list[tuple[str, str]]
        """
        context_requests = []
        for node in template.find_all((jinja2.nodes.Call,)):
            if isinstance(node.node, jinja2.nodes.Name) and len(node.args) == 2 and \
                    isinstance(node.args[0], jinja2.nodes.Const) and isinstance(node.args[1], jinja2.nodes.Const):
                context_requests.append((node.args[0].value, node.args[1].value))
        return context_requests


from requests import get
from pandas import DataFrame as pdf, read_csv, concat, Series, set_option, options, option_context
from numpy import arange, array
import cvxpy

options.display.width = 0
set_option('display.max_columns', None)
#set_option('display.max_rows', None)

def get_player(x): return xpts.loc[x[0], 'Name']
def decay(row, rate): return row * rate**arange(0, len(row))
def x90(mins): return 0.000215171*(mins**2) - 0.04515889*mins + 2.342686553 + 1

base_path = '/Users/desmundhui/Downloads/'
xpts = read_csv(base_path + 'fplreview.csv')
xpts['Pos'] = xpts['Pos'].astype("category").cat.set_categories(['F', 'M', 'D', 'G'])

#approximate points assuming all xMin=90
xpts_90 = xpts.copy()
for i in range(5, xpts_90.shape[1], 2):
    xpts_90.iloc[:, i+1] *= xpts_90.iloc[:, i].map(x90)

#decay([1,1,1,1,1,1,1,1], 0.84)
#adjustment for form of player, using a factor of 0.8
#form = read_csv(base_path + 'fplreview_formFactor.csv', encoding= 'unicode_escape')
#form['FF_0.8'] = (((form['Form Factor'] - 1) * 0.8) + 1).fillna(1)

#check size of dataframe first
#if xpts.shape[0] != form.shape[0]:
#    print('New player(s) in fplreview.csv, please update form.csv first.')
#    quit()

#cvxpy
def optim(itb=100, incl=[], excl=[], preset=[], weeks_from=1, weeks_to=8, ft=1, use_form=False, use_90=False,
    bench_weight=[0, 0.2, 0.05, 0], BB=0, TC=0, WC=0, FH=0, decay_rate=0.84):
    global xpts
    #use form to estimate
    #if use_form: xpts.iloc[:, 5:] = xpts.iloc[:, 5:].mul(form['FF_0.8'], axis=0)
    #use 90xMin
    if use_90: xpts = xpts_90
    #Isolate the targeted GWs, and eliminate poor value options
    pts_df = xpts.filter(like='_Pts', axis=1).iloc[:,weeks_from-1:weeks_to]
    xpts['total'] = pts_df.sum(axis=1)
    #include v cheap players that may offer no value (in terms of ppm)
    cheap = xpts[xpts['SV']<=4.5].sort_values(['SV', 'total'], ascending=[True, False])
    cheap_idx = (
        cheap[cheap['SV']<=4].groupby(['Pos', 'SV']).head(5).index.union(  #take first 5 G & D of each price point <= 4.0
            cheap[(cheap['Pos']=='M') | (cheap['Pos']=='F')].groupby(['Pos', 'SV']).head(5).index #M & D, <= 4.5
        )
    )

    xpts['value'] = xpts['total'] / xpts['SV'] / pts_df.shape[1]
    xpts2 = concat([xpts[xpts['value'] > (0.35 if not use_90 else 0.5)],xpts.loc[cheap_idx, :],xpts.iloc[preset, :]]).drop_duplicates()
    pts_df2 = xpts2.filter(like='_Pts', axis=1).iloc[:,weeks_from-1:weeks_to]
    #time decay
    if decay_rate != 1:
        pts_df2 = pts_df2.apply(decay, axis=1, args=(decay_rate,))
        undecay = (1/decay_rate)**arange(0, 8)
        undecay_total = 0

    GW = int(pts_df2.columns[0][:-4]) #initial GW
    weeks_to = min(weeks_to, 38+weeks_from-GW) #in case weeks_to exceed GW38

    pts_dict = pts_df2.to_dict('list')
    sell_price = xpts2['SV'].tolist()
    buy_price = xpts2['BV'].tolist()

    var_dict = {}
    price_dict = {}
    total_points = 0
    constraints = []

    var_dict[f'{GW}_FT'] = ft
    var_dict[f'{GW}_itb'] = itb

    for gw in range(GW, GW - weeks_from + weeks_to + 1):#gw=34
        #variable construction
        var_dict[f'{gw}_selction'] = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_bench_0']  = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_bench_1']  = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_bench_2']  = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_bench_3']  = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_squad']    = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_captain']  = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_auxiliary']= cvxpy.Variable(boolean=True) #refer to Optimization for FPL (with Excel) - Part 3
        var_dict[f'{gw}_tra_used'] = cvxpy.Variable(integer=True)
        var_dict[f'{gw}_incoming'] = cvxpy.Variable(xpts2.shape[0], boolean=True)
        var_dict[f'{gw}_outgoing'] = cvxpy.Variable(xpts2.shape[0], boolean=True)

        #price dict
        price_dict[f'{gw}_sold_amt'] = cvxpy.sum(sell_price @ var_dict[f'{gw}_outgoing'])
        price_dict[f'{gw}_bought_amt']  = cvxpy.sum(buy_price @ var_dict[f'{gw}_incoming'])

        #construct total_points as target to maximise
        total_points += (
            pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_selction'] +
            pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_captain'] +
            (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_0']) * bench_weight[0] +
            (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_1']) * bench_weight[1] +
            (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_2']) * bench_weight[2] +
            (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_3']) * bench_weight[3]
        )
        #Add the remaining weight of bench if BB
        if gw==GW+BB-weeks_from:
            total_points += (
                (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_0']) * (1-bench_weight[0]) +
                (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_1']) * (1-bench_weight[1]) +
                (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_2']) * (1-bench_weight[2]) +
                (pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_bench_3']) * (1-bench_weight[3])
            )
        #TC
        elif gw==GW+TC-weeks_from: total_points += pts_dict[f'{gw}_Pts'] @ var_dict[f'{gw}_captain']

        #total_points deduction for too many transfers
        #WC or FH or no preset (i.e. a WC)
        if gw==GW+WC-weeks_from or gw==GW+FH-weeks_from or (not preset and gw==GW):
            #no transfer cost when WC/FH, so no deduction from total_points. Next GW always 1 FT
            var_dict[f'{gw+1}_FT'] = 1
            var_dict[f'{gw+1}_itb'] = var_dict[f'{gw}_itb'] + price_dict[f'{gw}_sold_amt'] - price_dict[f'{gw}_bought_amt']
        #in week FH+1, compare itb with week FH-1
        elif (FH!=0 and gw==GW+FH+1-weeks_from):
            total_points -= cvxpy.maximum((var_dict[f'{gw}_tra_used'] - var_dict[f'{gw}_FT']) * 4, 0)
            var_dict[f'{gw+1}_FT'] = var_dict[f'{gw}_auxiliary'] + 1
            var_dict[f'{gw+1}_itb'] = var_dict[f'{gw-1}_itb'] + price_dict[f'{gw}_sold_amt'] - price_dict[f'{gw}_bought_amt']
        else:
            total_points -= cvxpy.maximum((var_dict[f'{gw}_tra_used'] - var_dict[f'{gw}_FT']) * 4, 0)
            var_dict[f'{gw+1}_FT'] = var_dict[f'{gw}_auxiliary'] + 1
            var_dict[f'{gw+1}_itb'] = var_dict[f'{gw}_itb'] + price_dict[f'{gw}_sold_amt'] - price_dict[f'{gw}_bought_amt']

        #give more weight to rolling FT
        total_points += (var_dict[f'{gw}_FT']-1) * 0.5 #so 0.5 pts if you have 2FTs, else 0
        #doesnt matter if it deducts points when ft==0, since further weeks' results wont change

        #constraints
        #each team cannot have more than 3 players
        for team in set(xpts2['Team']):
            constraints += [array(xpts2['Team'] == team) @ var_dict[f'{gw}_squad'] <= 3]

        constraints += [
            var_dict[f'{gw}_squad'] == ( #so player cant be selected AND benched
                var_dict[f'{gw}_selction'] + var_dict[f'{gw}_bench_0'] + var_dict[f'{gw}_bench_1'] +
                var_dict[f'{gw}_bench_2'] + var_dict[f'{gw}_bench_3']
            ),
            sum(var_dict[f'{gw}_bench_0']) == 1,
            array(xpts2['Pos'] == 'G') @ var_dict[f'{gw}_bench_0'] == 1, #bench_0 = GK
            sum(var_dict[f'{gw}_bench_1']) == 1,
            sum(var_dict[f'{gw}_bench_2']) == 1,
            sum(var_dict[f'{gw}_bench_3']) == 1,
            sum(var_dict[f'{gw}_captain']) == 1,
            sum(cvxpy.pos(var_dict[f'{gw}_selction'] - var_dict[f'{gw}_captain'])) <= 10, #always 10 noncaptains from XI, fit DCP rule
            var_dict[f'{gw}_tra_used'] >= 0, #no negative transfers
            #squad limits
            array(xpts2['Pos'] == 'G') @ var_dict[f'{gw}_squad'] == 2,
            array(xpts2['Pos'] == 'D') @ var_dict[f'{gw}_squad'] == 5,
            array(xpts2['Pos'] == 'M') @ var_dict[f'{gw}_squad'] == 5,
            array(xpts2['Pos'] == 'F') @ var_dict[f'{gw}_squad'] == 3,
            #XI limits
            array(xpts2['Pos'] == 'G') @ var_dict[f'{gw}_selction'] == 1,
            array(xpts2['Pos'] == 'D') @ var_dict[f'{gw}_selction'] >= 3,
            array(xpts2['Pos'] == 'M') @ var_dict[f'{gw}_selction'] >= 2,
            array(xpts2['Pos'] == 'F') @ var_dict[f'{gw}_selction'] >= 1,
            #only 10 outfielders
            array(xpts2['Pos'] != 'G') @ var_dict[f'{gw}_selction'] == 10,

            #auxiliary for FT logic (cr. @Sertalpbial)
            var_dict[f'{gw}_FT'] - var_dict[f'{gw}_tra_used'] <= 2 * var_dict[f'{gw}_auxiliary'],
            var_dict[f'{gw}_FT'] - var_dict[f'{gw}_tra_used'] >= var_dict[f'{gw}_auxiliary'] + (-14) * (1-var_dict[f'{gw}_auxiliary'])
        ]

    #compare last week squad to this week squad, ensuring no. of transfers made and itb are correct
        constraints += [
            cvxpy.sum(var_dict[f'{gw}_incoming']) == var_dict[f'{gw}_tra_used'],
            var_dict[f'{gw+1}_itb'] >= 0
        ]
        if (FH!=0 and gw==GW+FH+1-weeks_from): #make sure in the week after FH, you get back original squad + transfer
            constraints += [
                var_dict[f'{gw}_squad'] == (
                    var_dict[f'{gw-2}_squad'] + var_dict[f'{gw}_incoming'] - var_dict[f'{gw}_outgoing']
            )]
        else:
            constraints += [
                var_dict[f'{gw}_squad'] == (
                    ([idx in preset for idx in xpts2.index.values] if gw==GW else var_dict[f'{gw-1}_squad']) +
                        var_dict[f'{gw}_incoming'] - var_dict[f'{gw}_outgoing']
            )]

    #force incl/excl player
    for id, gw_from_start in excl:
        constraints += [(xpts2.index==id) @ var_dict[f'{GW - weeks_from + gw_from_start}_squad'] == 0]
    for id, gw_from_start in incl:
        constraints += [(xpts2.index==id) @ var_dict[f'{GW - weeks_from + gw_from_start}_squad'] == 1]

    problem = cvxpy.Problem(cvxpy.Maximize(total_points), constraints)
    problem.solve(solver=cvxpy.CBC, allowablePercentageGap=0.1, verbose=False)

    if problem.status in ["infeasible", "unbounded"]:
        print(problem.status)
        quit()

    print(f'\nObjective value between GW{GW}-GW{GW - weeks_from + weeks_to} is {round(problem.value, 3)}. (Decay at {decay_rate})')
    if preset:  print(f'Preset used.')
    #if use_form: print(f'Form considered.')
    if incl: print(f'Force include: {", ".join(map(get_player, incl))}')
    if excl: print(f'Force exclude: {", ".join(map(get_player, excl))}')

    for gw in range(GW, GW - weeks_from + weeks_to + 1):
        res = concat([xpts2.iloc[:, :5], pts_df2[f'{gw}_Pts']], axis=1).reset_index(drop=True)
        #transfers in/out
        if gw==GW or (gw==GW + FH and FH==1): #no gw-1 for first week OR week 1 FH (so no gw-2)
            tra = concat([res, Series((var_dict[f'{gw}_squad'].value -
                [idx in preset for idx in xpts2.index.values]).round(), name='tra')],axis=1)
        elif gw==GW+FH-weeks_from+1: #for the week after FH is used, compare squad with 2 weeks ago
            tra = concat([res, Series((var_dict[f'{gw}_squad'].value - var_dict[f'{gw-2}_squad'].value).round(), name='tra')], axis=1)
        else:
            tra = concat([res, Series((var_dict[f'{gw}_squad'].value - var_dict[f'{gw-1}_squad'].value).round(), name='tra')], axis=1)
        tra = tra.sort_values(by='Pos', ascending=False)

        res = concat([res,
            Series(var_dict[f'{gw}_selction'].value, name=f'{gw}_Select'),
            Series(var_dict[f'{gw}_captain'].value, name=f'{gw}_Cap'),
            Series(var_dict[f'{gw}_squad'].value, name=f'{gw}_Squad'),
            Series(var_dict[f'{gw}_bench_0'].value, name=f'{gw}_Bench_0'),
            Series(var_dict[f'{gw}_bench_1'].value, name=f'{gw}_Bench_1'),
            Series(var_dict[f'{gw}_bench_2'].value, name=f'{gw}_Bench_2'),
            Series(var_dict[f'{gw}_bench_3'].value, name=f'{gw}_Bench_3'),
        ], axis=1)
        #select elements that are not equal to 0, then sort by selected, Pos
        res = res[res.iloc[:, 6:].ne(0).any(1)].sort_values(by=[res.columns[6], *res.columns[9:12], 'Pos'], ascending=False).iloc[:,:-4]
        prediction = res.iloc[:11, -4] @ (res.iloc[:11,-2]+1) #dot prod of pts and captain + 1 (so captain becomes 2)
        pred_bench = res.iloc[-4:,-4].sum()
        if decay_rate != 1:
            undecay_pred  = round(prediction * undecay[gw-GW], 3)
            undecay_bench = round(pred_bench * undecay[gw-GW], 3)

        if gw==GW and not preset: #for first week WC
            print(
                f'\nxPts: {round(prediction, 3)} ' + '(0) '+ f'Bench Points: {round(pred_bench, 3)} ' +
                f'TV used: {round(res["SV"].sum(), 1)} ' + 'WC'
            )
            if decay_rate != 1:
                print(f'un-decay xPts: {undecay_pred} Bench: {undecay_bench}')
                undecay_total += undecay_pred
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}') #no transfer out as no preset
            print(res.reset_index(drop=True))
        elif gw==GW+WC-weeks_from:
            print(
                f'\nxPts: {round(prediction, 3)} ' + '(0) '+ f'Bench Points: {round(pred_bench, 3)} ' +
                f'TV used: {round(res["SV"].sum(), 1)} ' +
                f'Transfer: {var_dict[f"{gw}_tra_used"].value.round()} ' + 'WC'
            )
            if decay_rate != 1:
                print(f'un-decay xPts: {undecay_pred} Bench: {undecay_bench}')
                undecay_total += undecay_pred
            print(f'Transfer(s) out: {", ".join(tra[tra["tra"].eq(-1)]["Name"])}')
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}')
            print(res.reset_index(drop=True))
        elif gw==GW+FH-weeks_from:
            print(
                f'\nxPts: {round(prediction, 3)} ' + '(0) '+
                f'Bench Points: {round(pred_bench, 3)} ' +
                f'TV used: {round(res["SV"].sum(), 1)} ' + 'FH'
            )
            if decay_rate != 1:
                print(f'un-decay xPts: {undecay_pred} Bench: {undecay_bench}')
                undecay_total += undecay_pred
            print(f'Transfer(s) out: {", ".join(tra[tra["tra"].eq(-1)]["Name"])}')
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}')
            print(res.reset_index(drop=True))
        elif gw==GW+BB-weeks_from:
            prediction = res.iloc[:, -4] @ (res.iloc[:,-2] + 1) #included bench
            try: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - var_dict[f'{gw}_FT'].value, 0) * -4) #pts hit
            except AttributeError: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - ft, 0) * -4)
            print(
                f'\nxPts: {round(prediction, 3)} ' + #included bench
                f'({pts_hit}) ' + f'Bench Boost TV used: {round(res["SV"].sum(), 1)} ' +
                f'Transfer: {var_dict[f"{gw}_tra_used"].value.round()}'
            )
            if decay_rate != 1:
                print(f'un-decay xPts: {round(prediction * undecay[gw-GW], 3)}')
                undecay_total += round(prediction * undecay[gw-GW], 3) + pts_hit
            print(f'Transfer(s) out: {", ".join(tra[tra["tra"].eq(-1)]["Name"])}')
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}')
            print(res.sort_values(by=[res.columns[5]], ascending=False).reset_index(drop=True))
        elif gw==GW+TC-weeks_from:
            prediction = res.iloc[:, -4] @ (2*(res.iloc[:,-2])+1) #2*captaincy + all 1
            try: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - var_dict[f'{gw}_FT'].value, 0) * -4) #pts hit
            except AttributeError: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - ft, 0) * -4)
            print(
                f'\nxPts: {round(prediction, 3)} ' + f'({pts_hit}) ' +
                f'Bench Points: {round(pred_bench, 3)} ' +
                f'TV used: {round(res["SV"].sum(), 1)} ' +
                f'Transfer: {var_dict[f"{gw}_tra_used"].value.round()} ' + 'Triple Cap'
            )
            if decay_rate != 1:
                print(
                    f'un-decay xPts: {round(prediction * undecay[gw-GW], 3)} ' +
                    f'Bench: {undecay_bench}'
                )
                undecay_total += round(prediction * undecay[gw-GW], 3) + pts_hit
            print(f'Transfer(s) out: {", ".join(tra[tra["tra"].eq(-1)]["Name"])}')
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}')
            print(res.reset_index(drop=True))
        else:
            try: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - var_dict[f'{gw}_FT'].value, 0) * -4) #pts hit
            except AttributeError: pts_hit = round(max(var_dict[f"{gw}_tra_used"].value - ft, 0) * -4)
            print(
                f'\nxPts: {round(prediction, 3)} ' + f'({pts_hit}) ' +
                f'Bench Points: {round(pred_bench, 3)} ' +
                f'TV used: {round(res["SV"].sum(), 1)} ' +
                f'Transfer: {var_dict[f"{gw}_tra_used"].value.round()}'
            )
            if decay_rate != 1:
                print(f'un-decay xPts: {undecay_pred} Bench: {undecay_bench}')
                undecay_total += round(prediction * undecay[gw-GW], 3) + pts_hit
            print(f'Transfer(s) out: {", ".join(tra[tra["tra"].eq(-1)]["Name"])}')
            print(f'Transfer(s) in: {", ".join(tra[tra["tra"].eq(1)]["Name"])}')
            print(res.reset_index(drop=True))

    print("Total xPts:", undecay_total)

#with option_context('display.max_rows', None):
#    print(xpts_90.iloc[:,:7].sort_values(by=xpts_90.columns[6], ascending=False))
#optim(101.1, preset=[11, 258, 103, 541, 389, 253, 300, 467, 387, 201, 232, 522, 23, 66, 60], FH=3)
#optim(itb=0.6, preset=[11, 258, 103, 541, 389, 253, 300, 467, 387, 201, 232, 522, 23, 66, 60])
#optim(102.8, FH=2, weeks_to=2, weeks_from=2, bench_weight=[0.01, 0.1, 0.01, 0], use_90=True)


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from django.views.decorators.cache import cache_page

from .models import Category, Post

def posts(request, filter_kwargs={}, search=None):
    objects = Post.visible_posts.filter(**filter_kwargs)
    paginator = Paginator(objects, 10)
    try:
        objects = paginator.page(request.GET.get('page'))
    except PageNotAnInteger:
        objects = paginator.page(1)
    except EmptyPage:
        objects = paginator.page(paginator.num_pages)
    context = {'posts': objects, 'search': search}
    return render(request, 'blog/blog.html', context)

@cache_page(60*15)
def blog(request):
    return posts(request)

@cache_page(60*15)
def archive(request, year):
    return posts(request, {'created__year': year})

@cache_page(60*15)
def category(request, slug):
    categories = get_object_or_404(Category, slug=slug).get_children()
    return posts(request, {'category__in': categories})

def search(request):
    query = request.GET.get('q')
    if query:
        return posts(request, {'content__icontains': query}, search=query)
    return posts(request, search=query)

@cache_page(60*15)
def blog_post(request, year, month, slug):
    post = get_object_or_404(Post, slug=slug)
    return render(request, 'blog/post.html', {'post': post})


#!/usr/bin/env python3
# coding: utf-8
"""
title: processContactForm.py
date: 02-18-2020
author: jskrable
description: lambda function to process contact form submissions
"""

import json
import uuid
import boto3
import datetime

def lambda_handler(event, context):
    """
    handles request to the lambda function from frontend. parses json request
    and sends to update_table function.
    """
    submission = event['submission']
    # ACTUALLY USE RESPONSE HERE TO RETURN AN ERROR IF OCCURS
    update_table(submission)
    response = "Successful submission"
    return {
        'statusCode': 200,
        'body': json.dumps(response)}

def update_table(submission):
    """
    adds new entry to dynamoDB table. creates a unique ID and adds timestamp
    of submission. SubmissionBody is a JSON object containing the form submission.
    """
    dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
    history = dynamodb.Table('jackskrable.com-contact')
    item = {
        'SubmissionID': str(uuid.uuid4()),
        'SubmissionTimestamp': str(datetime.datetime.now()),
        'SubmissionBody': submission
    }
    history.put_item(
        Item = item
    )


import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    return 1.0/(1+np.exp(-x))

"""
    Predicts the outcome for the input x and the weights w
    x_0 is 1 and w_0 is the bias
"""
def predict(x,w):
    return sigmoid(np.sum([w[p]*x[p] for p in range(len(x))]))

"""
    Determine the cost of the prediction (pred)
"""
def cost(real,pred):
    return np.sqrt(1.0/2*np.mean(np.power(real-pred,2)))

"""
    get the gradient for the inputs x,weights w and the outputs y
"""
def gradient(x,w,y):
    grads = np.empty((len(w)))
    for j in range(len(w)):
        grads[j] = np.mean(np.sum([x[i,j]*(predict(x[i],w)-y[i]) for i in range(len(x))]))
    return grads

"""
    get the new weights based on the old ones w, learning rate a and the gradients
"""
def getWeights(w,a,grads):
    return w-a*grads

"""
    initialize the weights w, inputs x and outputs y
    here the outputs are the outputs for x[0] or x[1]
"""
w = np.array([0,0,0])
# the first value in each subarray is the weight of the bias input, which is always set to 1.
x = np.array([[1,0,0],[1,0,1],[1,1,0],[1,1,1]])
y = np.array([0,0,0,1])

"""
    Use several steps to minimize the costs
"""
steps = 100
arr = np.empty((steps,2))
# the learning rate
a = 1
for c in range(steps):
    # Create a new array (prediction) which afterwards holds the prediction for the inputs x.
    prediction = np.empty((len(x)))
    for i in range(len(x)):
        # calculate the prediction for each input (keep the weights unchanged)
        prediction[i] = predict(x[i],w)
        print("prediction for: ",x[i]," is ",prediction[i])

    # get the costs of the prediction
    print("Cost: ")
    vCost = cost(y,prediction)
    print(vCost)
    # calculate the gradients
    print("Grads: ")
    grads = gradient(x,w,y)
    print(grads)
    # get the new weights using a learning rate of 0.3
    print("New Weights: ")
    w = getWeights(w,a,grads)
    print(w)
    # only for plotting the costs
    arr[c] = [c,vCost]

plt.scatter(arr[:,0],arr[:,1])
plt.xlabel('steps')
plt.ylabel('cost')
plt.title('Cost graph')
plt.show()


#!/usr/bin/python3

"""
ZetCode PyQt5 tutorial
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction, qApp
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(qApp.quit)

        #self.statusBar()

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Menubar')
        self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


###################################################################################################
# Measure the detection performance: reference code is https://github.com/ShiyuLiang/odin-pytorch #
###################################################################################################
# Writer: Kimin Lee
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import data_loader
import numpy as np
import torchvision.utils as vutils
import calculate_log as callog
import models
import math

#from torch.utils.serialization import load_lua
from torchvision import datasets, transforms
from torch.nn.parameter import Parameter
from torch.autograd import Variable
from numpy.linalg import inv

# Training settings
parser = argparse.ArgumentParser(description='Test code - measure the detection peformance')
parser.add_argument('--batch-size', type=int, default=128, help='batch size')
parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA')
parser.add_argument('--seed', type=int, default=1,help='random seed')
parser.add_argument('--dataset', required=True, help='target dataset: cifar10 | svhn')
parser.add_argument('--dataroot', required=True, help='path to dataset')
parser.add_argument('--imageSize', type=int, default=32, help='the height / width of the input image to network')
parser.add_argument('--outf', default='/home/rack/KM/2017_Codes/overconfidence/test/log_entropy', help='folder to output images and model checkpoints')
parser.add_argument('--out_dataset', required=True, help='out-of-dist dataset: cifar10 | svhn | imagenet | lsun')
parser.add_argument('--num_classes', type=int, default=10, help='number of classes (default: 10)')
parser.add_argument('--pre_trained_net', default='', help="path to pre trained_net")

args = parser.parse_args()
print(args)
args.cuda = not args.no_cuda and torch.cuda.is_available()
print("Random Seed: ", args.seed)
torch.manual_seed(args.seed)

if args.cuda:
    torch.cuda.manual_seed(args.seed)

kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}

print('Load model')

model = models.vgg13()
model.load_state_dict(torch.load(args.pre_trained_net))
print(model)

cifar10_train_loader, cifar10_test_loader = data_loader.getTargetDataSet("cifar10", args.batch_size, args.imageSize, args.dataroot)

#cifar10_train_loader = data_loader.getTargetDataSet("cifar10", args.batch_size, args.imageSize, args.dataroot)

#cifar10_test_loader = data_loader.getNonTargetDataSet("cifar10", args.batch_size, args.imageSize, args.dataroot)

svhn_test_loader = data_loader.getNonTargetDataSet("svhn", args.batch_size, args.imageSize, args.dataroot)

stl10_test_loader = data_loader.getNonTargetDataSet("stl10", args.batch_size, args.imageSize, args.dataroot)

mnist_mean = [0.1307, 0.1307, 0.1307]
mnist_std = [0.3081, 0.3081, 0.3081]

mnist_transform = transforms.Compose([
    transforms.Scale(32),
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x.repeat(3, 1, 1)),
    transforms.Normalize(mean = mnist_mean, std = mnist_std)
])

mnist_test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=False, download=True, transform=mnist_transform),
    batch_size=128, shuffle=True)

if args.cuda:
    model.cuda()

#norms = open('%s/norms.txt' % args.outf, 'w')

#features = list(model.features)
# print(len(features))
# https://discuss.pytorch.org/t/backward-starting-from-intermediate-layer/10189/3
#features = nn.ModuleList(features).eval()

def generate_svhn():
    totalout = 0.0
    count = 0.0
    for data, target in svhn_test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        outloss = my_loss(model, data)
        totalout += outloss
        count += 1.0
        #print("svhn out \n", outloss)
    print("svhn out \n", totalout / count)

def my_loss(D, x):
    out, features = D(x)
    total = 0.0
    loss = None
    if loss == None:
        loss = torch.sum(features**2, dim = (1,2,3))
    else:
        loss += torch.sum(features**2, dim = (1,2,3))
    return loss

def my_losses(D, x):

    out, features = D(x,False)

    a = torch.mean(
            features**2,
            dim = (1,2,3)
        ).tolist()
    assert len(a) == x.shape[0]
    return a

def plot(input, title, median):
    # Import the libraries
    import matplotlib.pyplot as plt
    #import seaborn as sns
    #input =  [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
    num_bins = 40
    # matplotlib histogram
    #n,bins, patches = plt.hist(input, num_bins, facecolor='blue', alpha = 0.5)

    fig, (ax) = plt.subplots(1, 1, figsize=(8, 6))
    ax.hist(input, num_bins, facecolor='blue', alpha=0.5)
    ax.axvline(x=median, color='r', linestyle='dashed', linewidth=2)
    plt.title( title + "\n\nmedian: " + str(median) )
    plt.show()

def evaluateWeights(D = None):
    all_linear1_params = torch.cat([x.view(-1) for x in D.parameters()])
    norm = torch.mean(all_linear1_params**2)
    print("l2 norm of weights", norm)
    plot(all_linear1_params.tolist(),"weights hist", torch.median(all_linear1_params).data.item())

def getCifar10():
    l = []
    for data, target in cifar10_test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        inloss = my_losses(model, data)
        l += inloss

    return l, torch.median(torch.FloatTensor(l)).data.item()

def getSVHNValues():
    l = []

    for data, target in svhn_test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        outloss = my_losses(model,data)
        l += outloss

    return l, torch.median(torch.FloatTensor(l)).data.item()

def getStl10Values():
    l = []

    for data, target in stl10_test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        outloss = my_losses(model,data)
        l += outloss

    return l, torch.median(torch.FloatTensor(l)).data.item()

def getMnistValues():
    l = []

    for data, target in mnist_test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        outloss = my_losses(model,data)
        l += outloss

    return l, torch.median(torch.FloatTensor(l)).data.item()

evaluateWeights(model)

l, median = getCifar10()
plot(l, 'cifar10 norms per input', median)

l, median = getSVHNValues()
plot(l, 'svhn norms per input', median)

l, median = getStl10Values()
plot(l, 'stl10 norms per input', median)

l, median = getMnistValues()
plot(l, 'mnist norms per input', median)
#https://discuss.pytorch.org/t/output-from-hidden-layers/6325/2


# Implementar la funcion agregar_peso, que inserte un registro en la tabla PersonaPeso.
# Debe validar:
# - que el ID de la persona ingresada existe (reutilizando las funciones ya implementadas).
# - que no existe de esa persona un registro de fecha posterior al que queremos ingresar.

# Debe devolver:
# - ID del peso registrado.
# - False en caso de no cumplir con alguna validacion.

import datetime

from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import select

from practico_03.ejercicio_02 import agregar_persona
from practico_03.ejercicio_06 import reset_tabla, PersonaPeso
from practico_03.ejercicio_04 import buscar_persona

engine = create_engine('sqlite:///sqlalchemy_ejemplo.db', echo=False) #cambiar a True para ver el log

def agregar_peso(id_persona, fecha, peso):

    rta = False
    Session = sessionmaker(bind=engine)

    # create a Session
    session = Session()

    if buscar_persona(id_persona) != False:
        try:
            personasPesos = session.query(PersonaPeso).filter(PersonaPeso.IdPersona == id_persona, PersonaPeso.Fecha > fecha).all()
            if personasPesos == []:
                # work with sess
                personaPeso =  PersonaPeso(IdPersona=id_persona, Fecha=fecha,Peso=peso)
                session.add(personaPeso)
                session.commit()
                session.refresh(personaPeso)
                return personaPeso.Id
            else:
                return False

        except:
            session.rollback()
            rta = False
        finally:
            session.close()

    else:
        return False

@reset_tabla
def pruebas():
    id_juan = agregar_persona('juan perez', datetime.datetime(1988, 5, 15), 32165498, 180)
    assert agregar_peso(id_juan, datetime.datetime(2018, 5, 26), 80) > 0
    # id incorrecto
    assert agregar_peso(200, datetime.datetime(1988, 5, 15), 80) == False
    # registro previo al 2018-05-26
    assert agregar_peso(id_juan, datetime.datetime(2018, 5, 16), 80) == False

if __name__ == '__main__':
    pruebas()


from abc import abstractmethod
import os
import numpy as np
import xlrd
import csv

# Interface
class AParser:
	"""
	Interface class for the strategy pattern.
	Contains methods for the specific parsers.
	"""
	@abstractmethod
	def parse(self):
		pass

	@abstractmethod
	def channels_num(self):
		pass

	@abstractmethod
	def points_num(self):
		pass

# Strategy 1
class ExcelParser(AParser):

	"""
	Class for parsing Excel files. Based on xlrd library.

	Opens and reads excel file (if available), checks if there are any data.
	Limits the number of rows to equal or less than 300 and the number of columns equal or less than 3.

	:parameter: a path to the user's uploaded file (normally an attribute of the Prepack instance).

	attributes:
		self.inputWorkbook: xlrd instance which opens spreadsheet file for data extraction
		self.inputWorksheet: xlrd instance which contains the data for the first sheet in the workbook
		self.rows: total number of the rows in the user's file
		self.columns: total number of the columns in the user's file.
		self.values: an empty (filled with none) numpy array with limited size for storing user's value read.

	:raises:
		TypeError if the file couldn't be open
		ValueError if there are no data in the file.
	"""

	def __init__(self, xlsfile: str):
		try:
			self.inputWorkbook = xlrd.open_workbook(xlsfile)
		except Exception:
			raise TypeError("Unable to locate the file or invalid file format.")

		self.inputWorksheet = self.inputWorkbook.sheet_by_index(0)

		if self.inputWorksheet.nrows == 0:
			raise ValueError("Unable to detect any data in the file.")

		if self.inputWorksheet.nrows > 300:
			self.rows = 300
		else:
			self.rows = self.inputWorksheet.nrows

		self.values = np.full((self.rows, 3), None)

		if self.inputWorksheet.ncols > 2:
			self.cols = 3
		else:
			self.cols = self.inputWorksheet.ncols

		self.values = np.full((self.rows, 3), None)

	def parse(self):

		"""
		Parsing method.

		A cycle runs through the xlrd instance, checks the value and writes it to the self.values attribute.
		:raises ValueError if there is invalid data in the source array.
		:return: self.values array with user's data.
		"""

		for x in range(self.cols):
			for y in range(self.rows):
				if not self.inputWorksheet.cell_value(y, x):
					self.values[y][x] = None
				elif isinstance(self.inputWorksheet.cell_value(y, x), (float, int)):
					self.values[y][x] = self.inputWorksheet.cell_value(y, x)
				else:
					raise ValueError("Invalid data format in the file.")
		return self.values

	def channels_num(self):
		"""
		Method for getting the number of channels for writing to the particular attribute of the prepack instance.
		:return: number of the actual columns (channels) in the output array.
		"""
		return self.cols

	def points_num(self):
		"""
		Method for getting the number of points for writing to the particular attribute of the prepack instance.
		:return: number of the actual rows (points) in the output array.
		"""
		return self.rows

# Strategy 2
class CSVParser(AParser):

	"""
	Class for parsing csv files.

	Opens and reads csv file (if available), parses the delimiter in the file.
	Checks if there are any data, converts its format to the float (if possible), writes it to the output array.
	Limits the number of rows to equal or less than 300 and the number of columns equal or less than 3.

	:parameter: a path to the user's uploaded file (normally an attribute of the Prepack instance).

	attributes:
		self.csvpath: path to the csv file
		self.inputfile: an aemty list for storing the lines from the user's file
		self.lines: total number of the lines in the user's file
		self.ch: total number of the columns in the user's file.
		self.output: an empty variable for storing numpy array with user's values after the parsing.
	"""

	def __init__(self, csvfile: str):
		self.csvpath = csvfile
		self.inputfile = []
		self.output = None
		self.lines = 0

		self.ch = 0

	def __get_delimiter(self):
		"""
		Protected method for getting the delimiter from the user's file.
		"""
		with open(self.csvpath, 'r', newline='') as file:
			sniffer = csv.Sniffer()
			dialect = sniffer.sniff(file.read())
			dlmtr = dialect.delimiter
			return dlmtr

	@staticmethod
	def __float_checker(float_string):
		"""
		Protected method for converting a float string ("1,23" or "1,234.567.890")
		to floating point number (1.23 or 1.234567890).
		:parameter: float string
		:raises: ValueError in case of the incorrect user's data.
		:returns: float number
		"""
		float_string = str(float_string)

		try:
			if float_string.count(".") == 1 and float_string.count(",") == 0:
				return float(float_string)
			else:
				semi_string = list(float_string)
				while semi_string.count(".") != 0:
					semi_string.remove(".")
				out_string = str.replace("".join(semi_string), ",", ".")
			return float(out_string)
		except ValueError:
			raise ValueError("Invalid data format in the file.")

	def parse(self):
		"""
		Parsing method.
		Gets delimiter from the user's file (returns TypeError if unable to open the file).

		Reads the lines from the file, counts them and stores first three values of each line
		to the specific attribute with the limit of 300 lines.
		Checks if there is any lines stored.
		Checks the number of the actual columns (channels).
		Creates an output array, writes the user's data to it, converting to the float numbers simultaneously.
		:raises:
			TypeError if unable to open and read user's file.
			ValueError if there is no data in the source array.

		:return: self.output array with user's data.
		"""
		try:
			dlmtr = self.__get_delimiter()

		except Exception:
			raise TypeError("Unable to locate the file or invalid file format.")

		with open(self.csvpath, 'r', newline='') as file:
			reader = csv.reader(file, delimiter=dlmtr)

			for line in reader:
				self.inputfile.append(line[:3])
				self.lines += 1
				if self.lines >= 300:
					break

		if self.inputfile == [[]]:
			raise ValueError("Unable to detect any data in the file.")
		if dlmtr in {"\r", "\r\n", "\n"}:
			self.ch = 1
		else:
			self.ch = len(self.inputfile[0])

		self.output = np.full((len(self.inputfile), 3), None)
		for y in range(len(self.inputfile)):
			for x in range(len(self.inputfile[0])):
				if self.inputfile[y][x] == "":
					self.output[y][x] = None
				else:
					self.output[y][x] = self.__float_checker(self.inputfile[y][x])

		return self.output

	def channels_num(self):
		"""
		Method for getting the number of channels for writing to the particular attribute of the prepack instance.
		:return: number of the actual columns (channels) in the output array.
		"""
		return self.ch

	def points_num(self):
		"""
		Method for getting the number of points for writing to the particular attribute of the prepack instance.
		:return: number of the actual lines (points) in the output array.
		"""
		return self.lines

# Context
class GeneralParser:
	"""
	Context class for the strategy pattern.
	Reads the extension of the file (if any) and chooses the strategy (i.e. specific parser).
	Calls needed methods of the specific parsers.
	:parameter: path to the user's file.
	:raises: TypeError if there is no any file or file has invalid extension.
	:returns: the results of the parser's methods.
	"""
	def __init__(self, path: str):

		self.ext = os.path.splitext(path)[1]

		if self.ext.lower() == '.xls' or self.ext.lower() == '.xlsx':
			self.__parser = ExcelParser(path)
		elif self.ext.lower() == '.csv':
			self.__parser = CSVParser(path)
		else:
			raise TypeError('Unable to locate the file or invalid file format.')

	def parse(self):
		return self.__parser.parse()

	def channels_num(self):
		return self.__parser.channels_num()

	def points_num(self):
		return self.__parser.points_num()


# Script to plot longitude/latitude points on a shapefile
import pandas as pd
import matplotlib.pyplot as plt
import descartes
import geopandas as gpd
from shapely.geometry import Point, Polygon

map_file = '/home/jon/PycharmProjects/jon-insight-project/data/external/PA/tl_2017_42_place.shp'

file_name = '/home/jon/PycharmProjects/jon-insight-project/data/processed/pa_course_database_processed.plk'
df = pd.read_pickle(file_name)

crs = {'init': 'eosg:4326'}

street_map = gpd.read_file(map_file)

fig, ax = plt.subplots(figsize = (15, 15))
street_map.plot(ax = ax)

plt.show()

geometry = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]

geo_df = gpd.GeoDataFrame(df, crs = crs, geometry= geometry)

geo_df.head()

fig, ax = plt.subplots(figsize = (15, 15))
street_map.plot(ax = ax, alpha = 0.4, color = 'grey')
geo_df.plot(ax=ax, markersize = 20, color = 'blue', marker = 'o')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_title('Disc golf courses in Pennsylvania')
plt.show()


import os
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
from torchvision import models
from torch.autograd import Variable

# from net import vgg16, vgg16_bn
#from resnet_yolo import resnet50, resnet18
from yololoss import yoloLoss
from dataset import yoloDataset
from load_dataset import VotTrainDataset
#from visualize import Visualizer
import numpy as np
from models import Yolov1_vgg16bn
from resnet_model import resnet18
from resnet_model import resnet50
from resnet_model import resnet101
use_gpu = torch.cuda.is_available()

file_root = '/home/xiec/DLCV/hw2-xiechen0692/hw2_train_val/'
#learning_rate = 0.002
learning_rate = 0.002
num_epochs = 50
batch_size = 8

net = resnet50()
resnet = models.resnet50(pretrained=True)
new_state_dict = resnet.state_dict()
dd = net.state_dict()
for k in new_state_dict.keys():
    print(k)
    if k in dd.keys() and not k.startswith('fc'):
        print('yes')
        dd[k] = new_state_dict[k]
net.load_state_dict(dd)
criterion = yoloLoss(7, 2, 5, 0.5)
if use_gpu:
    net.cuda()

net.train()
# different learning rate
params = []
params_dict = dict(net.named_parameters())
for key, value in params_dict.items():
    if key.startswith('features'):
        params += [{'params': [value], 'lr': learning_rate * 1}]
    else:
        params += [{'params': [value], 'lr': learning_rate}]
optimizer = torch.optim.SGD(params, lr=learning_rate, momentum=0.9, weight_decay=5e-4)

train_dataset = yoloDataset(root=file_root,img_size=448,  transforms=[transforms.ToTensor()],train= True)#S=7, B=2, C=16,
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = yoloDataset(root=file_root, img_size=448, transforms=[transforms.ToTensor()],train=False)#S=7, B=2, C=16,
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
##
print('the dataset has %d images' % (len(train_dataset)))
print('the batch_size is %d' % (batch_size))
logfile = open('log.txt', 'w')

num_iter = 0
# vis = Visualizer(env='xiong')
best_test_loss = np.inf

for epoch in range(num_epochs):
    net.train()
    if epoch == 10:
        learning_rate = 0.0015
    if epoch == 20:
        learning_rate = 0.001
    if epoch == 50:
        learning_rate = 0.0005
    if epoch == 60:
        learning_rate = 0.0001
    if epoch == 80:
        learning_rate = 0.00001

    for param_group in optimizer.param_groups:
        param_group['lr'] = learning_rate

    print('\n\nStarting epoch %d / %d' % (epoch + 1, num_epochs))
    print('Learning Rate for this epoch: {}'.format(learning_rate))

    total_loss = 0.

    for i, (images, target) in enumerate(train_loader):
        images = Variable(images)
        target = Variable(target)
        if use_gpu:
            images, target = images.cuda(), target.cuda()
        # print("****",images.size(),target.size())
        pred = net(images)
        # print("####",pred.size())
        loss = criterion(pred, target)
        total_loss += loss.data#[0]
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if (i + 1) % 5 == 0:
            print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f, average_loss: %.4f'
                  % (epoch + 1, num_epochs, i + 1, len(train_loader), loss.data, total_loss / (i + 1)))#[0]
            num_iter += 1
            loss_train = total_loss / (i + 1)
            print(loss_train)
            #vis.plot_train_val(loss_train=total_loss / (i + 1))

    # validation
    validation_loss = 0.0
    net.eval()
    for i, (images, target) in enumerate(test_loader):
        images = Variable(images, requires_grad=False)
        target = Variable(target, requires_grad=False)
        if use_gpu:
            images, target = images.cuda(), target.cuda()

        pred = net(images)
        loss = criterion(pred, target)
        validation_loss += loss.data
    validation_loss /= len(test_loader)
    print("validation_loss", validation_loss)
    #vis.plot_train_val(loss_val=validation_loss)

    if best_test_loss > validation_loss:
        best_test_loss = validation_loss
        print('get best test loss %.5f' % best_test_loss)
        torch.save(net.state_dict(), 'best.pth')
    logfile.writelines(str(epoch) + '\t' + str(validation_loss) + '\n')
    logfile.flush()
    torch.save(net.state_dict(), 'yolo.pth')
    if epoch % 10 == 0:
        torch.save(net.state_dict(), 'yolo_{}epoch.pth'.format(epoch))


# coding=utf-8
import random
import time
import unittest
import HTMLTestRunner
from appium import webdriver
from app.student.login.object_page.login_page import LoginPage
from app.student.homework.object_page.home_page import HomePage, Tow_homepage
from app.student.homework.object_page.games_page import Homework, HomePage,VocabularySelection,Vobcabulary
from app.student.homework.object_page.end_page import Words_end
from utils.screen_swipe import Swipe
from conf.decorator import setup, teardown, testcase, teststeps
from utils.vob_data import vob_operate

class Games(unittest.TestCase):
    @classmethod
    @setup
    def setUp(cls):
        """启动应用"""

        cls.login_page = LoginPage()
        cls.home_page = HomePage()
        cls.homework = Homework()
        cls.vob_page = Vobcabulary()

    @classmethod
    @teardown
    def tearDown(self):
        pass

    @testcase
    def test_homework(self):
        """对不同小游戏类型，选择不同函数进行相应的操作"""
        # self.login_page.login()
        time.sleep(5)

        self.login_page.app_status1()

        time.sleep(2)

        if self.home_page.wait_check_page() == "试卷":
            print("登录成功")
            # 进入首页后点击作业x条目
        while True:
            var = self.homework.judge_homework_YB()

            if '测试重构1' in var[1] and '测试重构1' in var[3][0]:

                # self.homework_exist(var[1].index + 1, var[2], var[3])
                self.home_page.test_reconstruction_one()
                self.vob_selection_explantion()
                break

            else:
                Swipe().swipe_up(1000)
                self.home_page.test_reconstruction_one()
                self.vob_selection_explantion()

                break

    def vob_selection_explantion(self):
        # 进入词汇选择的条件
        count = self.homework.vob_count()
        title_type = self.vob_page.vob_explanation_reconstruction()
        print(title_type)
        print(len(count))
        time.sleep(3)
        for i in range(0, len(count)):
            var = self.homework.vob_type(i).text
            print(var)  # 获取小游戏类型
            if var == ' 词汇选择 ' and title_type == '选解释':
                print(var)
                self.vob_page.vob_reconstrction()
                time.sleep(2)
                self.method()

            break
        self.vob_page.end_page()
        self.vob_page.wrong_problem()
        self.mothed()
        self.vob_page.end_page()
        print("第二次的做题正确率，星星数目，积分和做题时间大于等于第一次做题的正确率星星数目和积分和做题时间")
        self.vob_page.chick_answer()
        self.vob_page.clcik_voice()
        self.vob_page.show_answer()
        self.vob_page.return_btn()
        self.vob_page.return_btn()
        self.vob_page.return_btn()

    def method(self):
        max_num = int(self.vob_page.vob_explantion_num_reconstruction())
        j = 0
        time.sleep(1)
        for i in range(int(max_num)):
            self.vob_page.grey_btn()
            self.vob_page.time_view_begin()
            j = j + 1
            max_num = int(self.vob_page.vob_explantion_num_reconstruction())
            print("进入第%d题,我们需要完成的小题的数目%d" % (j, max_num))
            options = self.vob_page.vob_option_button()
            time.sleep(1)
            options[random.randint(1, len(options) - 1)].click()  # 随机点击选项
            self.vob_page.time_view_end()
            self.vob_page.light_btn()
            print("----------------------")
            time.sleep(2)

    def mothed(self):
        time.sleep(2)
        max_num = self.vob_page.vob_explantion_num_reconstruction()
        j = 0
        time.sleep(1)

        for i in range(int(max_num)):
            self.vob_page.grey_btn()
            self.vob_page.time_view_begin()
            j = j + 1
            max_num = int(self.vob_page.vob_explantion_num_reconstruction())
            print("进入第%d题,我们需要完成的小题的数目%d" % (j, max_num))
            time.sleep(2)
            self.vob_page.options_word(self.vob_option()).click()
            ex = self.vob_page.options_word_ex(self.vob_option())
            print("我们选择正确的单词解释是：", ex)
            self.vob_page.time_view_end()
            self.vob_page.light_btn()
            time.sleep(3)
            print("输入正确后，点击下一步按钮进入下一题")
            print("------------------------------------------------")

    def vob_option(self):
        # 得到我们正确答案的列表的下表盘
        time.sleep(2)
        value = vob_operate(self.vob_page.word())
        time.sleep(2)
        print(value)
        index = [i for i, x in enumerate(self.vob_page.option()) if x == value][0]
        print("数字是多少", index)
        return index

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(Games('test_homework'))

    report_title = u'Example用例执行报告'
    desc = '用于展示修改样式后的HTMLTestRunner'
    timestr = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    filename = r'/Users/work/test_IOS/storges/test_report/screen_shot/Result_' + timestr + '.html'
    print(filename)
    fp = open(filename, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=report_title,
        description=desc)
    runner.run(suite)
    fp.close()


import os
import shutil
import json
import yaml

from fabric.api import local, task, lcd, env
from fabric.state import output

from docker import from_env as dockerclient
from docker import APIClient

__version__ = "1.3"

CONFIG_FILE = os.path.join("..", ".project.yaml")

config = yaml.load(open(CONFIG_FILE, 'r'))
output.update(config.get("output", dict()))
v = env['vars'] = config.get("vars", dict())
l = v['local'] = dict(
    vcs=f"git@{v['vcs_server']}:{v['remote_repository']}.git",
    registry=f"{v['registry_server']}/{v['remote_repository']}",
    branch=v['default']['branch'],
    tag=v['default']['tag']
)

@task(name="env")
def setup_env(activate=None):
    """ Setup current shell enviorments """

    if activate == "yes":
        activate = True
    elif activate == "no":
        activate = False
    else:
        activate = None

    if activate is None:
        print("Usage:", "------", sep="\n")
        if os.environ.get("OLD_PS1"):
            return print("eval $(fab env:no) - Deactivate enviorment")
        return print("eval $(fab env:yes) - Аctivate enviorment")

    if activate:
        return print(f'export',
                     'OLD_PS1="$(echo $PS1) "',
                     'PS1="(build) $(echo $PS1) "')

    return print('export',
                 'PS1="$(echo $OLD_PS1) "'
                 ';',
                 'unset',
                 'OLD_PS1')

@task
def stable():
    """ Setup enviorment for production """
    l.update(v.get('stable', dict()))

############
# Commands #
############

@task(default=True)
def info():
    """ Print versions and bbase variables """
    print(
        f"Fabric version: {env['version']}",
        f"Script revision: {__version__}",
        f"Places:",
        f"|-- Source code: {l['vcs']}",
        f"|-- Docker Registry: {l['registry']}",
        f"Staging:",
        f"|-- Branch: {l['branch']}",
        f"|-- Tag: {l['tag']}",
        sep="\n"
    )

@task
def create():
    """ Default command for create docker image """
    repository_pull()
    docker_build(replace="yes")
    repository_clean()

@task(name="pull")
def repository_pull():
    """ Get current version of application sources for docker image """

    title_print(f"Pull: {l['vcs']}")

    if os.path.isdir("app/.git"):
        with lcd("app"):
            local("git pull")
    else:
        local(f"git clone -b {l['branch']} {l['vcs']} app")

    if os.path.isfile("app/.gitmodules"):
        with lcd("app"):
            local("git submodule deinit .")
            local("git submodule init")
            local("git submodule update")

@task(name="clean")
def repository_clean():
    """ Clean cache and temp files """

    title_print(f"Clean temp files")

    shutil.rmtree("app") if os.path.exists("app") else None
    os.remove("Dockerfile") if os.path.exists("Dockerfile") else None

@task(name="build")
def docker_build(replace=None):
    """ Build docker image """
    replace = True if replace == "yes" else False
    docker_tag = f"{l['registry']}:{l['tag']}"
    client = APIClient()

    docker_remove() if replace else None

    title_print(f"Build: {docker_tag}")

    if os.path.isfile("app/Dockerfile"):
        shutil.move("app/Dockerfile", "Dockerfile")

    if os.path.exists("Dockerfile"):
        for line in client.build(path=".", tag=docker_tag, nocache=True, rm=True):
            print(json.loads(line).get("stream").rstrip())
    else:
        print("Can't find Dockerfile")

@task(name="remove")
def docker_remove():
    """ Remove docker image from local docker host"""
    docker_tag = f"{l['registry']}:{l['tag']}"
    client = dockerclient()

    title_print(f"Remove: {docker_tag}")

    images = [i for i in dockerclient().images.list() if docker_tag in i.tags]
    for image in images:
        client.images.remove(image.id)
        print(f"Delete: {image.short_id}")

@task(name="push")
def docker_push():
    """ Publish docker image in registry """
    docker_tag = f"{l['registry']}:{l['tag']}"

    title_print(f"Publish: {docker_tag}")

    client = dockerclient()
    client.images.push(docker_tag)

###########
# Helpers #
###########

def title_print(message):
    return print("", message, "".ljust(len(message), "-"), sep="\n")


import pyglet
from pyglet.gl import *
from pyglet.window import key
from vector import *
import settings
import renderer

class Player( renderer.Renderable3D ):
    def __init__( self, window, level, renderer, cubes, platform ):
        pyglet.clock.schedule( self.update )
        self.window = window
        self.level = level
        self.renderer = renderer
        self.cubes = cubes
        self.platform = platform
        self.camera = self.renderer.camera
        self.position = self.level.start_position
        self.window.set_exclusive_mouse( True )
        self.window.set_mouse_visible( False )
        self.camera.player_camera = True
        self.keys = key.KeyStateHandler()
        self.window.push_handlers( self.keys )
        self.window.on_key_press = self.key_pressed
        self.window.on_mouse_release = self.on_mouse_release
        self.window.on_mouse_press = self.on_mouse_press
        self.window.on_mouse_scroll = self.on_mouse_scroll
        self.camera.position = Vector3( 0, -0.5, 0 ) - self.position
        self.camera.target = Vector3( -1, 0, 0 ) - self.position
        self.velocity = Vector3(0,0,0)
        self.selected_cube = None
        self.placement_cube = None
        self.template_cube = None
        self.deathTimer = 0
        self.placing = False

        # Some defaults
        self.score = settings.PLAYER_DEFAULT_SCORE
        self.radius = settings.PLAYER_COLLISION_RADIUS

    def key_pressed( self, symbol, modifiers ):
        # Space is jump, its awesome
        if self.deathTimer <= 0:
            if symbol == key.SPACE:
                bottomCube = self.platform.completed_cube( self.position.x + 1, self.position.y - 0.5, self.position.z )
                if bottomCube == None:
                    bottomCube = self.level.cube( self.position.x+0.5, self.position.y - 0.5, self.position.z+0.5 )

                if (bottomCube and bottomCube.is_visible()) or \
                 ((self.position.x - 0.5 + self.radius) < 0 and self.position.y >= self.platform.POSITION.y):
                    self.velocity.y = max( self.velocity.y, settings.PLAYER_JUMP_SPEED )

            if symbol == key.Q:
                self.dig()
            if symbol == key.E:
                self.place()
            if symbol in [ key.ENTER, key.RETURN, key.C ]:
                self.build()
            if symbol == key.ESCAPE:
                pyglet.app.exit()
            if symbol == key.UP:
                self.cubes.selection = max( self.cubes.selection - 1, 0 )
            if symbol == key.DOWN:
                self.cubes.selection = min( self.cubes.selection + 1, 5 )

    def on_mouse_scroll(self, x, y, dx, dy):
        if dy > 0:
            self.cubes.selection = max( self.cubes.selection - 1, 0 )
        else:
            self.cubes.selection = min( self.cubes.selection + 1, 5 )

    def on_mouse_press (self, x, y, buttons, modifiers):
        if buttons == pyglet.window.mouse.LEFT:
            self.dig()
        if buttons == pyglet.window.mouse.RIGHT:
            self.placing = True

        #self.window.on_mouse_motion()

    def on_mouse_release (self, x, y, buttons, modifiers):
        if self.placing and buttons == pyglet.window.mouse.RIGHT:
            self.place()

        self.placing = False

    def dig( self ):
        if self.selected_cube and not self.selected_cube.locked:
            self.cubes.incrementCubeCount( self.selected_cube.type() )
            self.selected_cube.set_visible( False )
            self.selected_cube = None

    def place( self ):
        """ Places a new block at the cursor with the current type selection. """
        if self.placement_cube and self.cubes.getCubeCount( self.cubes.selection ) and not self.placement_cube.locked:
            self.cubes.incrementCubeCount( self.cubes.selection, -1 )
            self.placement_cube.set_type( self.cubes.selection )
            self.placement_cube.set_visible( True )
            self.placement_cube = None

    def build( self ):
        """ Creates a new block by combining some combination of others. Builds the currently selected block. """
        # Get the blueprint for the currently selected cube
        blueprint = self.cubes.BLUEPRINTS[ self.cubes.selection ]

        # No blueprint means we cannot build it
        if blueprint is None:
            return

        # Check if we have all the required components
        for key, value in blueprint.items():
            if self.cubes.getCubeCount( key ) < value:
                return

        # Now decrement the required cubes
        for key, value in blueprint.items():
            self.cubes.incrementCubeCount( key, -value )

        # And increment for the new constructed value
        self.cubes.incrementCubeCount( self.cubes.selection )

    def draw(self):
        if self.placing:
            if self.placement_cube and self.cubes.getCubeCount( self.cubes.selection ):
                self.placement_cube.draw(self.cubes.selection)

    def update( self, delta ):
        """ Handles player movement and highligh interaction with the level. """
        delta = delta * settings.PLAYER_MOVEMENT_SPEED
        if self.deathTimer <= 0:
            if self.keys[ key.W ]:
                self.camera.forward( -delta, True )
            if self.keys[ key.S ]:
                self.camera.forward( delta, True )
            if self.keys[ key.A ]:
                self.camera.strafe( delta )
            if self.keys[ key.D ]:
                self.camera.strafe( -delta )
        self.camera.update_direction()

        # Grab our current position according to the camera
        self.position = -self.camera.position

        # For some reason we are offset from the graphics by 0.5f units, so just adjust
        # the "real" position from the one we got from the camera, and then change it back afterwards
        self.position += Vector3( 0.5, 0, 0.5 )
        self.collision_check( delta )
        self.select_cube( delta )
        self.position -= Vector3( 0.5, 0, 0.5 )

        if self.deathTimer > 0:
            self.deathTimer -= delta

        if (self.position.y < self.level.lower_bound):
            self.position = self.level.start_position
            self.velocity = Vector3(0,0,0)
            self.deathTimer = 2.0
            self.score -= 50
            self.camera.reset()

            i = 0
            for count in self.cubes.cubeCounts:
                self.cubes.incrementCubeCount( i, -count/2 )
                i += 1

        # Reset the camera position based on our player adjustments
        self.camera.position = -self.position

    def collision_check( self, delta ):
        """ Makes sure the player isn't going through the blocks. """

        # Apply gravity to our velocity
        self.velocity.y -= settings.PLAYER_GRAVITY * delta

        # Update the player position due to velocity
        self.position += self.velocity * delta / 100

        # Make sure they stay within the bounds of the level
        self.position.z = max( self.radius, min(self.level.bounds.z + 1 - self.radius, self.position.z) )

        # Platorm - Special condition here for the "platform" area
        if (self.position.x - 0.5 + self.radius) < 0 and self.position.y > self.platform.POSITION.y:
            bottom_position = (self.platform.POSITION.y + self.radius * 2  + 0.5 )
            if bottom_position > self.position.y:
                self.velocity.y = 0
                self.position.y = bottom_position
            self.position.x = max( -self.platform.SIZE.x + self.radius, min(self.level.bounds.x + 1 - self.radius, self.position.x) )

            # Perform collision against the platform cubes
            self.collide_with_cubes( self.platform.completed_cube, Vector3(-0.5,0,0), Vector3(-0.5,0,0) )
            return

        # Make sure they stay within the bounds of the level
        self.position.x = max( self.radius, min(self.level.bounds.x + 1 - self.radius, self.position.x) )

        # Perform collision against the level cubes
        self.collide_with_cubes( self.level.cube )

    def collide_with_cubes( self, cube_method, offset = Vector3(0, 0, 0), bump = Vector3(0,0,0) ):
        # Make sure they don't go "sideways" through blocks
        left = cube_method( self.position.x - 1 + bump.x, self.position.y, self.position.z )
        right = cube_method( self.position.x + 1 + bump.x, self.position.y, self.position.z )
        front = cube_method( self.position.x + offset.x, self.position.y, self.position.z - 1  )
        back = cube_method( self.position.x + offset.x, self.position.y, self.position.z + 1 )
        bottom = cube_method( self.position.x + offset.x, self.position.y - 1, self.position.z )
        top = cube_method( self.position.x + offset.x, self.position.y + 1, self.position.z  )

        # Bound check the left, right, front and back cubes surrounding the player
        if left and left.is_visible():
            self.position.x = max( left.x + 1 + self.radius * 3 + bump.x, self.position.x )
        if right and right.is_visible():
            self.position.x = min( right.x - self.radius * 3 + bump.x, self.position.x )
        if front and front.is_visible():
            self.position.z = max( front.z + 1 + self.radius * 3, self.position.z )
        if back and back.is_visible():
            self.position.z = min( back.z - self.radius * 3, self.position.z )

        # For the top and bottom cases, we need to reset velocity if we hit the ground
        # TODO: This is a little difficult to land, you instinctvely fall short because you can't see
        # your feet when falling. It would be good if this had radius as well, especially
        # weighted more towards the camera facing direction.
        if bottom and bottom.is_visible():
            bottom_position = (bottom.position[1] + self.radius * 2 + 1)
            if bottom_position > self.position.y:
                self.velocity.y = 0
                self.position.y = bottom_position
        if top and top.is_visible():
            top_position = (top.position[1] - self.radius * 2)
            if top_position < self.position.y:
                self.velocity.y = 0
                self.position.y = top_position

    def select_cube( self, delta ):
        """ Performs a ray cast or something and displays a placeholder cube when
        hovering over existing cubes, or when about to place a new cube. """
        # Starting position for the selected cube search
        current = self.level.cube( self.position.x, self.position.y, self.position.z )
        position = self.position + Vector3(0,0.5,0)
        cube = None
        self.placement_cube = None

        # Iterative step from the camera 3 times attempting to find a cube to select
        for i in range(3):
            if cube and not cube.is_visible() and cube != current and self.placement_cube == None:
                self.placement_cube = cube

            if cube is None or not cube.is_visible():
                position = position + self.camera.direction * 0.85
                cube = self.level.cube( position.x, position.y, position.z )
            else:
                break

        # Platform for placing cubes
        current = self.platform.completed_cube( self.position.x, self.position.y, self.position.z )
        position = self.position + Vector3(-0.5,0.5,0)
        for i in range(4):
            if i > 1 and cube and not cube.is_visible() and cube != current and self.placement_cube == None:
                self.placement_cube = cube

                if self.template_cube is not None:
                    self.template_cube.revert_type()
                self.template_cube = self.platform.template_cube( position.x, position.y, position.z )
                self.template_cube.set_type( self.template_cube.type() + 8, False )
            if cube is None or not cube.is_visible():
                position = position + self.camera.direction * 0.85
                cube = self.platform.completed_cube( position.x, position.y, position.z )
            else:
                break

        if cube:
            if self.selected_cube == cube:
                return
            elif self.selected_cube is not None:
                self.selected_cube.revert_type()
            if cube.is_visible():
                if not cube.has_alternative_type():
                    cube.set_type( cube.type() + 8, False )
                self.selected_cube = cube
            else:
                self.selected_cube = None
        elif self.selected_cube is not None:
            self.selected_cube.revert_type()
            self.selected_cube = None

        if self.placement_cube is None and self.template_cube is not None:
            self.template_cube.revert_type()
            self.template_cube = None


from . import views
from django.urls import path

app_name = 'userscreen'

urlpatterns = [
    path('',views.mainpage_view, name="mainpage"),
    path('userview/', views.user_view, name="userview"),
    path('create/', views.userPage_create, name="create"),
    path('showpvmforms/', views.showPvmForms, name="showpvmforms"),
    path('createpvmform/', views.userPage_create, name="createpvmform"),
    path('createform/<id>/', views.createForm, name="createform"),
    path('addmembers/', views.addMembers, name="addmembers"),
    path('addvertybe/',views.addVertybe, name="addvertybe"),
    path('pdf/',views.get, name="get")
]


#!/usr/bin/env python

# Edited by xbacov04

import sys
import signal
import rospy
from PyQt5 import QtWidgets, QtCore
from xbacov04.gui import SceneViewer

def sigint_handler(*args):
    """Handler for the SIGINT signal."""
    sys.stderr.write('\r')
    QtWidgets.QApplication.quit()

def main(args):

    rospy.init_node('rescuers_gui_projector', anonymous=True)
    rospy.sleep(1)

    signal.signal(signal.SIGINT, sigint_handler)

    app = QtWidgets.QApplication(sys.argv)

    ps = SceneViewer()

    timer = QtCore.QTimer()
    timer.start(500)
    timer.timeout.connect(lambda: None)  # Let the interpreter run each 500 ms.

    sys.exit(app.exec_())

if __name__ == '__main__':
    try:
        main(sys.argv)
    except KeyboardInterrupt:
        print("Shutting down")


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseNotFound
from models import Table
from django.views.decorators.csrf import csrf_exempt

# Create your views here.

def login(request):
    if request.user.is_authenticated():
        return ("
You're: " + request.user.username +
                "
Logout")
    else:
        return ("
You aren't registred\nLogin")

def all(request):
    list = Table.objects.all()
    out = "
\n"
    for i in list:
        out += "
" + i.name + "
\n"
    out += "
\n"
    out += login(request)
    return HttpResponse(out)

@csrf_exempt
def number(request, recurso):
    if request.method == "GET":
        list = Table.objects.filter(name=recurso)
        if not list:
            return notfound(request, recurso)
        out = " "
        for i in list:
            out += i.name + ": " + str(i.number)
    if request.method == "PUT":
        if request.user.is_authenticated():
            new = Table(name=recurso, number=request.body)
            new.save()
            out = ("Saved Page, check it with GET")
        else:
            out = ("You must be registred")
    out += login(request)
    return HttpResponse(out)

def notfound(request, recurso):
    out = ("Not found: " + recurso)
    out += login(request)
    return HttpResponseNotFound(out)


def image_extract(pmcid):

    url = f"https://europepmc.org/article/PMC/{pmcid}"

    import chromedriver_autoinstaller
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as ec
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException
    from selenium.webdriver.common.alert import Alert
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.chrome.options import Options
    import urllib
    from selenium import webdriver

    chromedriver_autoinstaller.install()
    options = webdriver.ChromeOptions()
    webdriver = webdriver.Chrome(
        options=options
    )
    print("Started Searching")
    webdriver.get(url)

    images = []
    a = 0
    keep_seaching = True
    while(keep_seaching):
        try:

            results = WebDriverWait(webdriver, 10).until(ec.visibility_of_element_located(
                (By.XPATH, "//a[@class='figures--figure-box']")))
            results = webdriver.find_elements_by_xpath(
                "//a[@class='figures--figure-box']")
        except:
            break
        try:
            button_scroll = WebDriverWait(webdriver, 2).until(ec.visibility_of_element_located(
                (By.XPATH, "//i[@class='fas fa-angle-right fa-stack-1x']")))
        except:
            break

        for result in results:
            img_data = result.get_attribute("style")
            img_data = img_data.strip("background-image: ")
            img_data = img_data.strip('l("')
            img_data = img_data.lstrip('");')
            if img_data not in images:
                images.append(img_data)

        if button_scroll:
            webdriver.execute_script("arguments[0].click();", button_scroll)

        else:
            break

    for image in images:
        response = urllib.request.urlopen(str(image))
        with open(f'{pmcid}_{a}.jpg', 'wb') as f:
            f.write(response.file.read())
        a += 1

image_extract("PMC8120281")


# coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import seaborn

from src.fourier_series import piecewise_analytic

def animate_series(series, max_degree):
    series = series
    x = series.x
    x0 = series.x0
    gen = series()
    index = 0

    step_function = piecewise_analytic.step_function(x, x0, 1, 0)
    v_function = piecewise_analytic.v_function(x, x0, 1, -1, x0)

    seaborn.set()
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(2 * 6, 2 * 8))

    # Series
    axes[0, 0].set(xlim=(np.min(x), np.max(x)),
                   ylim=(np.min(step_function)-0.5, np.max(step_function)+0.5))
    axes[0, 1].set(xlim=(np.min(x), np.max(x)),
                   ylim=(np.min(v_function)-0.01, np.max(v_function)+0.01))
    # Error
    axes[1, 0].set(xlim=(np.min(x), np.max(x)), ylim=(10 ** -7, 1))
    axes[1, 1].set(xlim=(np.min(x), np.max(x)), ylim=(10 ** -7, 1))
    # Functions
    axes[0, 0].plot(x, step_function)
    axes[0, 1].plot(x, v_function)

    l1, = axes[0, 0].plot([], [], marker='o', markersize=2.0, linestyle='-',
                          color='black', alpha=0.5)
    l2, = axes[0, 1].plot([], [], marker='o', markersize=2.0, linestyle='-',
                          color='black', alpha=0.5)
    l3, = axes[1, 0].semilogy([], [], marker='o', markersize=2.0, linestyle='-',
                              color='black', alpha=0.5)
    l4, = axes[1, 1].semilogy([], [], marker='o', markersize=2.0, linestyle='-',
                              color='black', alpha=0.5)

    def update(_):
        # series expansion
        deg, functions = next(gen)

        step_function3 = functions[0]
        step_function_series = step_function3[index]
        step_function_error = np.abs(step_function - step_function_series)

        l1.set_xdata(x)
        l1.set_ydata(step_function_series)

        l3.set_xdata(x)
        l3.set_ydata(step_function_error)

        if len(functions) == 2:
            v_function3 = functions[1]

            v_function_series = v_function3[index]
            v_function_error = np.abs(v_function - v_function_series)

            l2.set_xdata(x)
            l2.set_ydata(v_function_series)

            l4.set_xdata(x)
            l4.set_ydata(v_function_error)

        print(deg)
        return l1, l2, l3, l4

    anim = animation.FuncAnimation(fig, update,
                                   frames=max_degree,
                                   repeat=False,
                                   interval=10,
                                   blit=True)

    plt.show()


# -*- coding: utf-8 -*-
# @Time:2018.11.15 15:57
# @Author:Zhang
# @Desc  :
from greenlet import greenlet
import time

def test1():
    while True:
        print("test1")
        gr2.switch()
        time.sleep(0.5)
        print('test1--end')

def test2():
    while True:
        print("test2")
        gr1.switch()
        time.sleep(0.5)
        print('test2--end')

gr1 = greenlet(test1)
gr2 = greenlet(test2)

# 切换到gr1中运行
gr1.switch()


from csv import DictReader
import json
from schema import userRecord
from address_parser import address_parser, nameSplit
import re
import uuid
import userGroup
def space_snip(string):
    # collapse consecutive spaces
    snipped = re.sub("  +", "", string)
    return snipped

def date_format(date):
    year = date[0:4]
    month = date[4:6]
    day = date[6:]
    return (f"{year}-{month}-{day}")

def userMaker(file):
    outputFile = []
    with open("amh_users.txt", 'r', encoding='utf8') as user_file:
        userparse =  DictReader(user_file, delimiter = "|")
        for row in userparse:
            x = uuid.uuid4()
            address = address_parser(row['ADDRESS1'])
            address = address.parse()
            name = nameSplit(row["Z303_NAME"])
            name = name.namesplit()
            name["email"] = row["EMAIL1"]
            name["phone"] = row["PHONE2"]
            if row['Z305_BOR_TYPE'] in ["EM", "RE", "SP"]:
                group = row["Z305_BOR_STATUS"] + "AC"
                #print (group)
            else:
                group =  row["Z305_BOR_STATUS"]+ row['Z305_BOR_TYPE']
            groupUUID = userGroup.groups("userGroups.csv", group)

            if address:

                if  len (space_snip(row["ZIP1"])) > 3:
                    address['postalCode'] = space_snip(row["ZIP1"])
                name["addresses"] = [address]

            newUser = userRecord(
                id = str(x),
                externalSystemId = space_snip(row["EXTERNAL_ID"]),
                barcode =  space_snip(row['BARCODE']),
                type = row['Z305_BOR_TYPE'],
                personal = name,
                expirationDate = date_format(row['Z305_EXPIRY_DATE']),
                patronGroup = groupUUID.getgroup(),
                customFields = {"bor_type": row['Z305_BOR_TYPE'],
                            "alephKey": space_snip(row["Z303_REC_KEY"]) })
            outputFile.append(newUser)

        return outputFile

if __name__ == "__main__":
    x = userMaker("mhc_users.txt")
    with open("outputFile.json", 'w') as output:
        json.dump(x, output, indent = 4)


"""pokus

Revision ID: 9e7a47c85b58
Revises: 688fd85f3129
Create Date: 2019-12-08 18:59:30.176186

"""
from alembic import op
import sqlalchemy as sa
from datetime import datetime

# revision identifiers, used by Alembic.
revision = '9e7a47c85b58'
down_revision = '688fd85f3129'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column("Kalkulacka", sa.Column("date", sa.DateTime, default=datetime.utcnow()))

def downgrade():
    op.drop_column("Kalkulacka", "date")


import numpy as np
import pandas as pd
import json
import matplotlib.pyplot as plt
from Bio import SeqIO
from os import getcwd, listdir, system
from itertools import permutations
from scipy.fft import fft, ifft
from scipy import stats
import pywt

#Trying to get in JSON final_permutations
output_path = getcwd() + "/data/JSON_Files"
#with open(f"{output_path}/final_permutations.json", "r") as my_file:
    #json.dump(folder_dict, my_file)

f = open(f"{output_path}/{listdir(output_path)[1]}", )

my_dict = json.load(f)
#JSON not loading
#_______________________________________________________________________________

#Using dictionary instead
folder_path = getcwd() + "/data"

folders = sorted(listdir(folder_path))[1:8]
folders
folder_dict = {}

# Going to Specific Virus Folders inside the Test folders
for folder in folders:
    subfolder_dict = {}
    for sub_folder in listdir(f"{folder_path}/{folder}"):
        subfolder_dict[sub_folder] = listdir(f"{folder_path}/{folder}/{sub_folder}")
    folder_dict[folder] = subfolder_dict

output_path = getcwd() + "/data/JSON_Files"

with open(f"{output_path}/fasta_files.json", "w") as my_file:
    json.dump(folder_dict, my_file)

f = open(f"{output_path}/{listdir(output_path)[0]}", )

my_dict = json.load(f)
my_dict

# Getting all possible combinations of 2 for the fasta files
file_tuple_list = []

for folder in folders:
    for sub_folder in listdir(f"{folder_path}/{folder}"):
        for file in listdir(f"{folder_path}/{folder}/{sub_folder}"):
            file_tuple_list.append((folder,sub_folder,file))

file_combos = list(permutations(file_tuple_list, 2))

new_dict = {}
#each of the keys shows tuples, first element is the virus, second is file

for i in my_dict.keys():
    file_list = []
    for j in my_dict[i].keys():
        for file in my_dict[i][j]:
            file_list.append((j,file))
        new_dict[i] = file_list

new_dict_2 = {}
for key in new_dict.keys():
    seq_perm = list(permutations(new_dict[key], 2))
    new_dict_2[key] =  seq_perm

new_dict_3 = {}
for key in new_dict_2.keys():
    file_list_2 = []
    for i,j in new_dict_2[key]:
        if(i[0] != j[0]):
            file_list_2.append((i,j))
    new_dict_3[key] = file_list_2

#___________________________________________________________________________
#Using the dictionary to apply to our code

def make_sequence(path_of_file):
    start_seq = list(SeqIO.parse(f"{path_of_file}", "fasta"))
    count = len(start_seq[0].seq)
    final_seq = "".join([char for char in start_seq[0].seq])
    return final_seq

dict_of_bases = {"T":1,"t":1,"C":1,"c":1, "A":-1,"a":-1 ,"G":-1, "g":-1}

def numerical_pp(dna_strand):
    numeric = []
    for base in dna_strand:
        numeric.append(dict_of_bases[base])
    return np.array(numeric)

def normalization(numerical1, numerical2):
    if len(numerical1)>len(numerical2):
        dna_seq = np.array(numerical2)
        numer = len(numerical1)- len(numerical2)
        if(numer%2 != 0):
            numer = numer - 0.5
            pad_width = numer/2
            numerical2 = pywt.pad(dna_seq, pad_width, "antisymmetric")
            numerical1 = numerical1[0:len(numerical1)-1]
        else:
            pad_width = numer/2
            numerical2 = pywt.pad(dna_seq,pad_width, "antisymmetric")
    else:
        dna_seq = np.array(numerical1)
        numer = len(numerical2)- len(numerical1)
        if(numer%2 != 0):
            numer = numer - 0.5
            pad_width = numer/2
            numerical1 = pywt.pad(dna_seq, pad_width, "antisymmetric")
            numerical2 = numerical2[0:len(numerical2)-1]
        else:
            pad_width = numer/2
            numerical1 = pywt.pad(dna_seq,pad_width, "antisymmetric")

    return numerical1,numerical2

def normalization_min(numerical1, numerical2):
    if len(numerical1)>len(numerical2):
        numerical1 = numerical1[0:len(numerical2)]
    else:
        numerical2 = numerical2[0:len(numerical1)]

    return numerical1,numerical2

pearsons_dict = {}
for test in new_dict_3:
    list_sequences= []
    for file1,file2 in new_dict_3[test]:
        file_path = getcwd() + f"/data/{test}/{file1[0]}/{file1[1]}"
        file_path2 = getcwd() + f"/data/{test}/{file2[0]}/{file2[1]}"
        seq1  = make_sequence(file_path)
        seq2 = make_sequence(file_path2)
        pp1 = numerical_pp(seq1)
        pp2 = numerical_pp(seq2)
        pp1_norm = normalization(pp1,pp2)[0]
        pp2_norm = normalization(pp1,pp2)[1]
        #print(len(pp1),len(pp1_norm), file1[1],len(pp2),len(pp2_norm),file2[1])
        fft_1 = fft(pp1_norm)
        fft_2 = fft(pp2_norm)
        mag_1 = abs(fft_1)
        print(mag1)
        mag_2 = abs(fft_2)
        print(mag2)
        pcc = stats.pearsonr(mag_1, mag_2)
        list_sequences.append((file1[1],file2[1],pcc))
    pearsons_dict[test] = list_sequences

#___________________________________________________________________
# Printing PCC data by Test
pearsons_dict = {}
list_sequences = []
for file1,file2 in new_dict_3['Test3b']:
    file_path = getcwd() + f"/data/Test3b/{file1[0]}/{file1[1]}"
    file_path2 = getcwd() + f"/data/Test3b/{file2[0]}/{file2[1]}"
    seq1  = make_sequence(file_path)
    seq2 = make_sequence(file_path2)
    pp1 = numerical_pp(seq1)
    pp2 = numerical_pp(seq2)
    pp1_norm = normalization_min(pp1,pp2)[0]
    pp2_norm = normalization_min(pp1,pp2)[1]
    print(type(pp1_norm),type(pp2_norm))
    fft_1 = fft(pp1_norm)
    fft_2 = fft(pp2_norm)
    mag_1 = abs(fft_1)
    mag_2 = abs(fft_2)
    pcc = stats.pearsonr(mag_1, mag_2)
    list_sequences.append((file1[1],file2[1],pcc, mag_1 , mag_2))
    pearsons_dict["Test3b"] = list_sequences

pearsons_dict["Test3b"][200]
one = pearsons_dict["Test3b"][200][3]
two = pearsons_dict["Test3b"][200][4]

len(one)
len(two)

pcc = stats.pearsonr(one, two)
print(pcc)

plt.scatter(one, two)


import numpy as np
from matplotlib import pyplot as plt
from skimage.transform import resize
from os.path import join

from utils.quantisation.mmcq import mmcq

VERBOSE = 1

imsource = "../../images"
image_name = "kth_campus.jpg"
image_path = join(imsource, image_name)
target_scale = 1/1
n_colours = 8

#
# load image and prepare pixel data
#
if VERBOSE >= 1: print("loading and preparing image...")
image = plt.imread(image_path)
image = resize(image, (int(image.shape[0]*target_scale), int(image.shape[1]*target_scale), image.shape[2]))
if VERBOSE >= 2:
    print(image.shape)
    print(image[0,0,:])

#
# Use MMCQ to produce colour scheme.
# Note that due to how MMCQ works, the number of components should be 2^x for some integer x.
#
if VERBOSE >= 1: print("running MMCQ algorithm...")
colours = mmcq(image, n_colours)

#
# display resulting colours
#
if VERBOSE >= 1: print("displaying colours...")
colours = np.asarray(colours)
colours = np.reshape(colours, (colours.shape[0], 1, colours.shape[1]))
if VERBOSE >= 2: print(colours[0, 0, :])

plt.imshow(colours)
plt.show()


#打猴子补丁  打补丁： 在不修改程序源码的情况下，为程序添加原来不支持的功能
from gevent import monkey
monkey.patch_all() #破解所有

import time,gevent
#创建生成器1
def work1():
    while True:
        print("正在执行work1",gevent.getcurrent()) #没有产生新的线程 在原线程自动划分不同编号
        time.sleep(0.5) #默认情况 不被gevent 识别为耗时操作 需要打补丁
        # gevent.sleep(0.5)
        #打补丁： 在不修改程序源码的情况下，为程序添加原来不支持的功能

#创建生成器1
def work2():
    while True:
        print("正在执行work2",gevent.getcurrent())
        # time.sleep(0.5)
        gevent.sleep(0.5)

if __name__ == '__main__':
    #创建greenlet的对象
    g1=gevent.spawn(work1)
    g2=gevent.spawn(work2)
    g1.join() #让主线程等待协程执行完毕后再退出
    # gevent.joinall() 批量让携程join


"""
A panel widget showing all worlds in the game.
May be filtered as All, Friend or Foe.
"""
from ui.ui_abstract.widget import Widget
from ui.ui_abstract.text import Text
import pygame
from color import COLORS
from model import game
from ui import texture_cache
from assets.png import PNG
import assets
from ui.ui_abstract.image import Image

class LedgerAllShips(Widget):
    IDLE = object()
    FRIEND = object()
    FOE = object()

    def __init__(self,rect):
        super(LedgerAllShips,self).__init__(rect)

        self.tile_height = 1
        self.list_start = 0
        self.scroll = 0

        self.idle_ship_button = None
        self.friend_ship_button = None
        self.foe_ship_button = None
        self.scroll_up_button = None
        self.scroll_down_button = None

        self.displayed = []

        self.show = self.FRIEND

        self.elements = [] # List of (Widget, Method, [args]) 3-tuples.

        x = y = 0
        font = pygame.font.Font(pygame.font.get_default_font(), 14)
        # Title
        def show_title(x,y):
            string = "All ships"
            widget = Text(pygame.Rect(x,y,0,0), font, COLORS["white"],
                          string)
            def nop():
                pass
            self.elements.append((widget,nop,[]))
            return (widget.width, widget.height)
        (dx,dy) = show_title(x,y)
        y += dy *1.5

        # Show idle ships
        def all_worlds_button(x,y):
            widget = Text(pygame.Rect(x,y,0,0), font, COLORS["white"],
                          "IDLE")
            def clicked():
                self.show = self.IDLE
                self.scroll = 0
                self.update()
            widget.rect.w = widget.surface.get_width()
            widget.rect.h = widget.surface.get_height()
            self.elements.append((widget,clicked,[]))
            return (widget.width, widget.height)
        (dx,dy) = all_worlds_button(x,y)
        x += dx *1.2

        # Filter friendly ships
        def friend_worlds_button(x,y):
            widget = Text(pygame.Rect(x,y,0,0), font, COLORS["green"],
                          "FRIEND")
            def clicked():
                self.show = self.FRIEND
                self.scroll = 0
                self.update()
            widget.rect.w = widget.surface.get_width()
            widget.rect.h = widget.surface.get_height()
            self.elements.append((widget,clicked,[]))
            return (widget.width, widget.height)
        (dx,dy) = friend_worlds_button(x,y)
        x += dx *1.2

        # Filter enemy ships
        def foe_worlds_button(x,y):
            widget = Text(pygame.Rect(x,y,0,0), font, COLORS["red"],
                          "FOE")
            def clicked():
                self.show = self.FOE
                self.update()
            widget.rect.w = widget.surface.get_width()
            widget.rect.h = widget.surface.get_height()
            self.elements.append((widget, clicked, []))
            return (widget.width, widget.height)
        (dx,dy) = foe_worlds_button(x, y)
        x = 0
        dy += dy *1.5

        self.list_start = dy # Start list from here.

    def on_mouse(self,event):
        if event.type != pygame.MOUSEBUTTONDOWN:
            return False
        ((mouse_x, mouse_y), button) = (event.pos, event.button)
        (x,y) = (mouse_x - self.x0, mouse_y - self.y0)
        for (widget, method, args) in self.elements:
            if widget.rect.collidepoint(x, y) and button == 1:
                method(*args)
                return True
        try:
            if self.scroll_up_button.rect.collidepoint(x, y) and button == 1:
                if self.scroll > 0:
                    self.scroll -= 1
                    self.update()
                    return True
        except:
            pass
        try:
            if self.scroll_down_button.rect.collidepoint(x,y) and button ==1:
                self.scroll += 1
                self.update()
                return True
        except:
            pass

        try:
            y -= self.list_start
            y = int(y/self.tile_height)
            ship = self.displayed[y]
            event = pygame.event.Event(pygame.USEREVENT,action="go to",goto=ship.position)
            pygame.event.post(event)
        except:
            pass

    def on_draw(self):
        self.surface.fill((0x0,0x0,0x0))

        self.scroll_up_button = None
        self.scroll_down_button = None

        self.displayed = []

        for (widget,_,_) in self.elements:
            self.surface.blit(widget.surface, widget.rect)

        # Draw list.
        y = self.list_start
        if self.scroll > 0: # Scroll up button?
            asset = assets.get(PNG,"up_16")
            widget = Image(pygame.Rect(self.width-16,y,0,0),asset)
            self.scroll_up_button = widget
            self.surface.blit(widget.surface,widget.rect)

        i = self.scroll
        for ship in sum(game.ships.values(),[]):
            if y +self.tile_height > self.height:
                # Draw button to scroll list down.
                asset = assets.get(PNG,"down_16")
                widget = Image(pygame.Rect(self.width-16,self.height-16,0,0), asset)
                self.scroll_down_button = widget
                self.surface.blit(widget.surface,widget.rect)
                break

            if self.show == self.FOE and ship.faction == game.factions[0]:
                # Hide friends when filtering for foes.
                continue
            if (self.show == self.FRIEND or self.show == self.IDLE) and ship.faction != game.factions[0]:
                # Hide foes when filtering for friends.
                continue
            if self.show == self.IDLE and ship.orders != []:
                continue

            if ship.invisible(game.factions[0]):
                continue # Skip ships that cannot be seen.

            if i > 0:
                i -= 1
                continue # Skip the first few elements if there's a scroll.

            dy = 0
            # Ship name
            if ship.faction == game.factions[0]:
                color = COLORS["green"]
            else:
                color = COLORS["red"]
            string = ship.name+" "+str(ship.position)
            texture = texture_cache.text(None, 14, color, string)
            self.surface.blit(texture,(0,y+dy))
            dy += texture.get_height()

            # Type
            string = "    "+ship.type_
            texture = texture_cache.text(None, 14, color, string)
            self.surface.blit(texture,(0,y+dy))
            dy += texture.get_height()

            # Show orders
            if ship.faction == game.factions[0]:
                string = "    "+str(len(ship.orders))+" orders"
                color = COLORS["green"]
                texture = texture_cache.text(None,14, color,
                                         string)
                self.surface.blit(texture,(0,dy+y))
                if ship.orders != [] and ship.faction == game.factions[0]:
                    try:
                        (order,coordinate) = ship.orders[0]
                    except ValueError:
                        (order) = ship.orders[0]
                        coordinate = ""
                    dx = texture.get_width()
                    texture = texture_cache.text(None, 14, COLORS["green"],
                                             "    "+order+" "+str(coordinate))
                    self.surface.blit(texture,(dx,dy+y))
                dy += texture.get_height()

            # Add entry to self.displayed for clicking on.
            self.displayed.append(ship)

            # Next entry
            y += dy
            self.tile_height = dy