from functools import reduce
from operator import mul

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable

class MetaOptimizer(nn.Module):

    def __init__(self, hidden_size):
        super(MetaOptimizer, self).__init__()
        self.hidden_size = hidden_size

        self.linear1 = nn.Linear(1, hidden_size)

        self.lstm = nn.LSTMCell(hidden_size, hidden_size)

        self.linear2 = nn.Linear(hidden_size, 1)
        self.linear2.weight.data.mul_(0.1)
        self.linear2.bias.data.fill_(0.0)

        self.reset_lstm()

    def reset_lstm(self):
        self.hx = Variable(torch.zeros(1, self.hidden_size))
        self.cx = Variable(torch.zeros(1, self.hidden_size))

    def forward(self, inputs):
        initial_size = inputs.size()
        x = inputs.view(-1, 1)
        x = F.tanh(self.linear1(x))

        if x.size(0) != self.hx.size(0):
            self.hx = self.hx.expand(x.size(0), self.hx.size(1))
            self.cx = self.hx.expand(x.size(0), self.cx.size(1))

        self.hx, self.cx = self.lstm(x, (self.hx, self.cx))
        x = self.hx

        x = self.linear2(x)
        x = x.view(*initial_size)
        return x

    def meta_update(self, meta_model, model_with_grads):
        # First we need to create a flat version of parameters and gradients
        weight_shapes = []
        bias_shapes = []

        params = []
        grads = []

        for module in meta_model.children():
            weight_shapes.append(list(module._parameters['weight'].size()))
            bias_shapes.append(list(module._parameters['bias'].size()))

            params.append(module._parameters['weight'].view(-1))
            params.append(module._parameters['bias'].view(-1))

        for module in model_with_grads.children():
            grads.append(module._parameters['weight'].grad.view(-1))
            grads.append(module._parameters['bias'].grad.view(-1))

        flat_params = torch.cat(params)
        flat_grads = torch.cat(grads)

        # Meta update itself
        flat_params = flat_params + self(flat_grads)

        # Restore original shapes
        offset = 0
        for i, module in enumerate(meta_model.children()):
            weight_flat_size = reduce(mul, weight_shapes[i], 1)
            bias_flat_size = reduce(mul, bias_shapes[i], 1)

            module._parameters['weight'] = flat_params[
                offset:offset + weight_flat_size].view(*weight_shapes[i])
            module._parameters['bias'] = flat_params[
                offset + weight_flat_size:offset + weight_flat_size + bias_flat_size].view(*bias_shapes[i])

            offset += weight_flat_size + bias_flat_size

        # Finally, copy values from the meta model to the normal one.
        meta_model.copy_params_to(model_with_grads)


from django.shortcuts import render, redirect
from django.http import HttpResponse
from .forms import SearchForm
from  .controllers.Spotify import Spotify
from django.urls import reverse
from radio.models import Entity, EntityQueue, Song
from django.template.loader import render_to_string
from django.template import loader

import json
import requests
# Create your views here.
def index(request):
    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():
            search = form.save(commit=False)
            resultType = search.resultType
            query = search.query
            platform = search.platform
            spotify = Spotify(search)
            jsonFilePath = spotify.search(request)
            return redirect('./results/' + jsonFilePath)

    else:
        form = SearchForm()
        return render(request, 'search/searchForm.html', {'form': form})

def results(request, jsonFilePath):
    completePath = './search/results/' + jsonFilePath
    print(completePath)
    jsonData = open(completePath).read()
    searchData = json.loads(jsonData)
    songs = searchData['tracks']['items']

    searchResults = []

    for jsonSong in songs:
        song = Song()
        song.name = jsonSong['name']
        song.artist = jsonSong['artists'][0]['name']
        song.album = jsonSong['album']['name']
        song.release = jsonSong['album']['release_date']
        song.popularity = jsonSong['popularity']
        song.spotifyID = jsonSong['id']
        song.image_url = jsonSong['album']['images'][0]['url']
        searchResults.append(song)
        del song

    baseContext = {
        'search_results': searchResults,
    }

    return render(request, './song-small.html', baseContext)

def song(request):
    return render(request, './song-small.html')


import datetime

from flask_restplus import Namespace, Resource

from utils import brl_format, percentage_format
from stock_models.models.daily_quote_holder import DailyQuoteHolder
from stock_models.models.fund_daily_values import DailyFund
from stock_models.models.quote_holder import QuoteHolderMovements
from stock_models.models.yahoo_ticker_value import YahooTickerValue
from stock_models.utils.io_utils import session

dashboard_ns = Namespace("dashboard")

@dashboard_ns.route("/")
class Dashboard(Resource):
    def get(self):
        graph = {'labels': [], 'series': [[], []]}
        all_fund = session().query(DailyFund).order_by(DailyFund.ref_date.asc()).all()
        first = True
        for fund in all_fund:
            yahoo = session().query(YahooTickerValue).filter(YahooTickerValue.ticker == '^BVSP').filter(
                YahooTickerValue.period == 'daily').filter(YahooTickerValue.time == fund.ref_date).one_or_none()

            if not yahoo or fund.ref_date.date().weekday():
                continue
            if first:
                first_fund = fund.quote_price
                first_bvsp = yahoo.close
                first = False
            graph['labels'].append(fund.ref_date.strftime('%m-%y') if fund.ref_date.day <= 8 else '')
            graph['series'][0].append(f'{100 * ((fund.quote_price / first_fund) - 1):.2f}')
            graph['series'][1].append(f'{100 * ((yahoo.close / first_bvsp) - 1):.2f}')

        today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        last_friday = (today.date()
                       - datetime.timedelta(days=today.weekday())
                       + datetime.timedelta(days=4, weeks=-1))
        last_month = today.replace(day=1)

        fund = session().query(DailyFund).order_by(DailyFund.ref_date.desc()).all()
        last_week_fund = session().query(DailyFund).filter(DailyFund.ref_date <= last_friday).order_by(
            DailyFund.ref_date.desc()).first()
        last_month_fund = session().query(DailyFund).filter(DailyFund.ref_date < last_month).order_by(
            DailyFund.ref_date.desc()).first()

        quote_holder = session().query(QuoteHolderMovements).all()
        qh_dict = {}
        final_bova, ref_date = session().query(YahooTickerValue.close, YahooTickerValue.time).filter(
            YahooTickerValue.ticker == '^BVSP').filter(
            YahooTickerValue.period == 'daily').order_by(YahooTickerValue.time.desc()).first()
        # final_bova = yahoo.close
        # ref_date = yahoo.time
        for qh in quote_holder:
            try:
                yahoo = session().query(YahooTickerValue).filter(YahooTickerValue.ticker == '^BVSP').filter(
                    YahooTickerValue.period == 'daily').filter(YahooTickerValue.time == qh.time).one()
                if not qh.name in qh_dict.keys():
                    qh_dict[qh.name] = {'bova': 0, 'sum': 0}
                qh_dict[qh.name]['bova'] = qh_dict[qh.name]['bova'] + qh.value * final_bova / yahoo.close
                qh_dict[qh.name]['sum'] = qh_dict[qh.name]['sum'] + qh.value
            except:
                print(qh.time)
        daily_qh = session().query(DailyQuoteHolder).filter(DailyQuoteHolder.ref_date == ref_date).order_by(
            DailyQuoteHolder.pl.desc()).all()
        quote_holder = {
            'header': ['Name', 'Quote Amount', 'Value', 'Return', 'Ibovespa'],
            'data': [],
        }
        for dqh in daily_qh:
            quote_holder['data'].append([dqh.name, f'{dqh.amount:.2f}', brl_format(dqh.pl),
                                         percentage_format(100 * (dqh.pl / qh_dict[dqh.name]['sum'] - 1)),
                                         percentage_format(100 * (qh_dict[dqh.name]['bova'] / qh_dict[dqh.name]['sum'] - 1))])

        table = {
            'patrimonial_equity': brl_format(fund[0].pl),
            'ref_date': fund[0].ref_date.strftime('%Y-%m-%d'),
            'cash': brl_format(fund[0].cash),
            'quote_amount': f'{fund[0].amount:.2f}',
            'quote_price': brl_format(fund[0].quote_price),
            'return': {
                'daily': [percentage_format(fund[0].pl / fund[1].pl), fund[1].ref_date.strftime('%Y-%m-%d')],
                'weekly': [percentage_format(fund[0].pl / last_week_fund.pl),
                           last_week_fund.ref_date.strftime('%Y-%m-%d')],
                'monthly': [percentage_format(fund[0].pl / last_month_fund.pl),
                            last_month_fund.ref_date.strftime('%Y-%m-%d')],
            },
            'graph': graph,
            'legend': {
                'names': ['Fund', 'Ibovespa'],
                'types': ['info', 'danger']
            },
            'quote_holder': quote_holder
        }
        return table


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

import logging
from .. import readers
from .. import writers
logger = logging.getLogger(__name__)

# Module API

def process_publication(conn, table, extractors):
    """Translate publication records from warehouse to database.

    Args:
        conn (dict): connection dict
        table (str): table name
        extractors (dict): extractors dict

    """

    # Extract and write source
    source = extractors['extract_source'](None)
    source_id = writers.write_source(conn, source)

    errors = 0
    success = 0
    for record in readers.read_records(conn, table):

        conn['database'].begin()

        try:

            # Extract and write publication/relationships
            publications = extractors['extract_publications'](record)
            for publication in publications:

                # Write publication
                publication_id = writers.write_publication(conn, publication, source_id)
                if publication_id is None:
                    continue

                # Following code could be moved to separete linking processor:
                # https://github.com/opentrials/opentrials/issues/115

                # Delete existent relationships
                conn['database']['trials_publications'].delete(
                    publication_id=publication_id)

                # Write relationships
                for id in publication['trial_identifiers']:
                    trial_objects = readers.read_objects(conn, 'trials', facts=[id])
                    for trial_object in trial_objects:
                        trial_id = trial_object['id']
                        writers.write_trial_relationship(
                            conn, 'publication', publication, publication_id, trial_id)

        except Exception as exception:
            errors += 1
            conn['database'].rollback()
            logger.exception('Processing error: %s [%s]',
                repr(exception), errors)

        else:
            success += 1
            conn['database'].commit()
            if not success % 100:
                logger.info('Processed %s publications from %s',
                    success, table)


from PyQt5.QtCore import QRegularExpression, pyqtSignal
from PyQt5.QtGui import QIcon, QRegularExpressionValidator, QValidator
from PyQt5.QtWidgets import QLineEdit,QHBoxLayout, QComboBox, QToolButton,QWidget

class SelectionWidget(QWidget):
    completed = pyqtSignal(bool)

    def __init__(self, items, parent=None):
        QWidget.__init__(self, parent)
        #Main Layout is a horizontal box
        self.setLayout(QHBoxLayout())

        #Create the drop down menu and fill it with the table names
        self.combobox = QComboBox(self)
        self.combobox.addItems(items)
        self.layout().addWidget(self.combobox)

        self.le = QLineEdit(self)
        self.le.setValidator(QRegularExpressionValidator(QRegularExpression("^([0-9])+(,[0-9]+)*$"), self))
        self.le.textChanged.connect(self.check_state)
        self.layout().addWidget(self.le)

        btnDelete = QToolButton(self)
        btnDelete.setIcon(QIcon.fromTheme("edit-delete"))
        self.layout().addWidget(btnDelete)
        btnDelete.clicked.connect(self.deleteLater)

    def getRows(self):
        try:
            return self.combobox.currentText(), [int(el) for el in self.le.text().split(",")]
        except Exception as p:
            print("error P: {}".format(p))

    def check_state(self, text):
        sender = self.sender()
        validator = sender.validator()
        state = validator.validate(text, 0)[0]
        if state == QValidator.Acceptable:
            color = '#c4df9b'  # green
            self.completed.emit(True)
        elif state == QValidator.Intermediate:
            color = '#fff79a'  # yellow
            self.completed.emit(False)
        else:
            color = '#f6989d'  # red
            self.completed.emit(True)
        sender.setStyleSheet('QLineEdit { background-color: %s }' % color)


# -*- coding: utf-8 -*-
from django.urls import path
from . import views

urlpatterns = [
        path('',views.home,name='home'),
        path('login',views.login,name='login'),
        path('signup',views.signup,name='signup'),
        path('slogin',views.slogin,name='slogin'),
        path('ssignup',views.ssignup,name='ssignup'),
        path('predict',views.predict,name="predict"),
        path('consult',views.consult,name="consult")
        ]


from bs4 import BeautifulSoup
import requests
import json
import random
url = "http://tj.xiaozhu.com/fangzi/28657302903.html"
wb_data = requests.get(url)
print(wb_data.text)
soup = BeautifulSoup(wb_data.text,"lxml")
titles = soup.select("body > div > div > div > h4 > em")
names = soup.select("body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p")
print(names)
for title,name in zip(titles,names):
    data ={
        "title" : title.get_text(),
        "name" : title.get_text()
    }

resp = requests.get("http://tor1024.com/static/proxy_pool.txt")
ips_txt = resp.text.strip().split("\n")
ips = []
for i in ips_txt:
    try:
        k = json.loads(i)
        ips.append(k)
    except Exception as e:
        print(e)
r = requests.get("http://bj.ganji.com/",proxies=random.choice(ips),timeout=6)
print(r)


import plotly.graph_objects as go
import pandas as pd
import sys, datetime

#where is the data source file. Contains 'name' and 'data'
data_source_file=sys.argv[1]
#Getting the start date like 2020-03-28
if len(sys.argv) > 2:
    start_date = sys.argv[2]

#Read the Lon, Lat of each state
df = pd.read_csv('./us_state_lonlat.csv')
#read the data set to be displayed on the map
df_data = pd.read_csv(data_source_file)
df.head()
state_df=df

# Let's combine the data source into the  geolocation source
# https://github.com/nytimes/covid-19-data/blob/master/us-states.csv
df['text'] = df['name'] # + df['data'].astype(str)
df = df.set_index('name')
df_data =df_data.set_index('state')

#Turn this on if bubbles are displayed based on categories and size.
#limits = [(0,2),(3,10),(11,20),(21,50),(50,3000)]
limits = [(0,3000)]
colors = ["royalblue","crimson","lightseagreen","orange","lightgrey"]
cities = []
scale = 10 #the scaled used to resize the bubble

# make figure
fig_dict = {
    "data": [],
    "layout": {},
    "frames": []
}
for i in range(len(limits)):
    lim = limits[i]
    # Breaking the list which is based in the order by population.
    df_sub = df[lim[0]:lim[1]]
fig_dict = dict(
    layout = dict(
        title_text = '',
        showlegend = True,
        geo = dict(
            scope = 'usa',
            landcolor = 'lightblue',#'rgb(217, 217, 217)',
        ),
        updatemenus=[dict(
            type="buttons",
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
    ),
    data = [
        {'type': 'scattergeo',
        'locationmode' : 'USA-states',
		},
#    'name' : '{0} - {1}'.format(lim[0],lim[1])
    ],

    frames = []
)

state_df = state_df.set_index('name')
data_source = pd.read_csv(data_source_file)

count=0
for i in pd.date_range(start_date, datetime.date.today()-datetime.timedelta(days=1), freq='1D'):
    count=count+1

    df_data2=data_source
    df_data2=df_data2.set_index('state')

    df_data2['date'] = pd.to_datetime(df_data2['date'])
    mask=(df_data2['date'] == i)
    df_data2 = df_data2.loc[mask]
    # Let's combine the data source into the  geolocation source
    # https://github.com/nytimes/covid-19-data/blob/master/us-states.csv
    df['data']=df_data2['cases']
    df=df.fillna(axis=1, value='0')
    convertedDate = i.to_pydatetime()
    appendFrame={'name' : count, 'layout' : {'title_text':convertedDate.strftime('%Y-%m-%d'), 'title_x':0.5},
         'data': [
             {'type': 'scattergeo',
              'locationmode' : 'USA-states',
              'lon' : df['lon']-0.5,
              'lat' : df['lat']-0.5,
              'text' : df['text']+' ' +df['data'].astype(str),
              'marker' : {
                'size' : df['data'].astype(int)/scale, #10,  #df_sub['pop']/scale,
                'color' : 'orange',
                'line_color' : 'rgb(40,40,40)',
                'line_width': 0.5,
                'sizemode' : 'area'
            }
            }
            ]
        }

    fig_dict["frames"].append(appendFrame)

fig = go.Figure(fig_dict)

fig.update_layout(
    title={
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top',
        'font':dict(
            family="Courier New, monospace",
            size=40,
            color="#7f7f7f"
    )      }
        )

#Make the date dynamically as the time serious
fig.add_trace(go.Scattergeo(
        lon = df['lon'],
        lat = df['lat'],
        text = df['code'],
        mode = 'text',
        textfont=dict(
           family="sans serif",
           color="white"
        ),
        showlegend = False,
    ))

# fig=go.Figure()
# animals=['giraffes', 'orangutans', 'monkeys']
# fig.add_trace(go.Bar
#                 (x=animals,
#                  y=[20, 14, 23]
#                  )
#                 )

fig.show()


import os
import torch
import numpy as np
from torch import nn, optim
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
from tensorboardX import SummaryWriter
from networks import torch_utils, basic_networks
from networks.conv_lstm_cell import ConvLSTMCell
from utils.dataset import KddDataset, random_split
from utils.eval import SMAPE

if __name__ == "__main__":
    # set manual seed
    torch.manual_seed(0)

    # define sequence_length, batch_size, channels, height, width
    T, T_PRED, B, C, H, W, NUM_CITY, NUM_AQ = 48, 24, 16, 5, 31, 21, 35, 3
    HIDDEN_SIZE = 32
    NUM_LAYERS = 1
    LEARNING_RATE = 1e-4
    MAX_EPOCH = 1000

    dataset = torch.load('data/dataset_bj.pt')
    train_set, validation_set = random_split(dataset, [3, 1])
    train_loader = DataLoader(train_set, batch_size=B, shuffle=True)
    validation_loader = DataLoader(validation_set, batch_size=B, shuffle=True)
    print('Size of train, validation: {}, {}'.format(
        len(train_set), len(validation_set)))

    print('Instantiate model')
    aq_lstm = nn.LSTM(input_size=NUM_CITY * NUM_AQ, hidden_size=HIDDEN_SIZE,
                      num_layers=NUM_LAYERS, batch_first=True)
    me_lstm = ConvLSTMCell(C, HIDDEN_SIZE)
    fusion = basic_networks.NNList([
        torch_utils.Flatten(),
        basic_networks.FC(
            input_dim=HIDDEN_SIZE + HIDDEN_SIZE * H * W,
            output_dim=T_PRED * NUM_CITY * NUM_AQ,
            hidden_dims=[],
        ),
    ])

    # print('Run on multiple gpus')
    aq_lstm = aq_lstm.cuda()
    me_lstm = me_lstm.cuda()
    fusion = fusion.cuda()
    models = [aq_lstm, me_lstm, fusion]

    print('Create a criterion and optimizer')
    loss_fn = nn.MSELoss()  # reduce=False)
    optimizer = optim.Adam(
        [params for model in models for params in model.parameters()], lr=LEARNING_RATE)

    print('Run for {MAX_EPOCH} epochs')
    writer = SummaryWriter()
    PLOT_EVERY = 1

    def run_batch(sample):
        B = sample[0].shape[0]
        aq, me, me_pred, y = sample[0].cuda(), sample[1].cuda(
        ), sample[2].cuda(), sample[3].cuda()
        me = torch.cat([me, me_pred], dim=1)
        # TODO this is not elegant
        aq[torch.isnan(aq)] = 0
        me[torch.isnan(me)] = 0
        ori_y = y.clone()
        y[torch.isnan(y)] = 0

        hidden = (Variable(torch.zeros(NUM_LAYERS, B, HIDDEN_SIZE)).cuda(),
                  Variable(torch.zeros(NUM_LAYERS, B, HIDDEN_SIZE)).cuda())
        aq_state = aq_lstm(aq, hidden)
        aq_feature = aq_state[0][:, -1]

        me_state = None
        for t in range(T):
            me_state = me_lstm(me[:, t], me_state)
        me_feature = me_state[0].view(B, -1)

        # compute predicted value
        # y_hat = fusion(aq_feature)
        y_hat = fusion(torch.cat([aq_feature, me_feature], dim=1))
        y_hat = y_hat.view(B, T_PRED, NUM_CITY * NUM_AQ)
        loss = loss_fn(y_hat, y)
        # nan_mask = torch.isnan(loss)
        # loss = torch.mean(loss[~nan_mask])

        ori_y = ori_y.view(B, T_PRED, NUM_CITY, NUM_AQ)
        y_hat = y_hat.view(B, T_PRED, NUM_CITY, NUM_AQ)
        smape = SMAPE(dataset.aq_mean + dataset.aq_std * ori_y.data,
                      dataset.aq_mean + dataset.aq_std * y_hat.data)
        return loss, smape

    for epoch in range(MAX_EPOCH):
        for sample_batched in train_loader:
            loss, smape = run_batch(sample_batched)

            print(' > Epoch {:2d} loss: {:.3f}'.format(epoch, loss.data[0]))

            # zero grad parameters
            for model in models:
                model.zero_grad()

            # compute new grad parameters through time!
            loss.backward()
            # for model in models:
            # for param in model.parameters():
            # if param.grad is None:
            # continue
            # print(int(torch.sum(torch.isnan(param.grad))))
            # print(int(torch.sum(torch.isnan(param.grad))) / int(np.prod(param.grad.shape)))
            # param.grad[torch.isnan(param.grad)] = 0

            # learning_rate step against the gradient
            optimizer.step()

        if epoch % PLOT_EVERY == 0:
            writer.add_scalar('train_loss', loss, epoch)
            writer.add_scalar('train_SMAPE', smape, epoch)

            validation_loss, validation_smape = \
                run_batch(iter(validation_loader).next())
            print(' > Epoch {:2d} validation loss: {:.3f}'.format(
                epoch, validation_loss.data[0]))
            writer.add_scalar('validation_loss',
                              validation_loss, epoch)
            writer.add_scalar('validation_SMAPE',
                              validation_smape, epoch)


import torch.nn as nn

from models.GeneralModel import GeneralModel
from utils.constants import *
from utils.model_utils import find_right_model

class CombinedClassifier(GeneralModel):

    def __init__(self,
                 hidden_dim, z_dim,
                 hidden_dim_vae, num_classes,
                 lstm_file, vae_files,
                 generator_loss,
                 generator_class,
                 dataset_options,
                 classifier_name, vaes_names,
                 # below: probably things we wont change
                 classifier_class='LSTMClassifier',
                 test_mode=False,
                 combined_weights_load = None,
                 combination_method='joint',
                 only_eval=True,
                 n_channels_in=(0), device="cpu", **kwargs):
        super(CombinedClassifier, self).__init__(n_channels_in, device, **kwargs)

        self.base_classifier = find_right_model(CLASS_DIR,
                                                classifier_class,
                                                n_channels_in=n_channels_in,
                                                num_classes=num_classes,
                                                device=device,
                                                hidden_dim=hidden_dim
                                                ).to(device)

        self.vae_classifier = find_right_model(CLASS_DIR,
                                               'VAEClassifier',
                                               generator_class=generator_class,
                                               generator_loss=generator_loss,
                                               vae_files=vae_files,
                                               vaes_names=vaes_names,
                                               device=device,
                                               n_channels_in=n_channels_in,
                                               hidden_dim=hidden_dim_vae,
                                               z_dim=z_dim,
                                               only_eval=only_eval,
                                               dataset_options=dataset_options,
                                               test_mode=test_mode
                                               ).to(device)

        if only_eval:
            self.base_classifier.eval()

        lstm_file = os.path.join(GITIGNORED_DIR, RESULTS_DIR, lstm_file)
        datamanager = DataManager(lstm_file)
        loaded = datamanager.load_python_obj(os.path.join('models', classifier_name))

        for state_dict in loaded.values():
            state_dict = state_dict
        self.base_classifier.load_state_dict(state_dict)

        # in case we wanna learn a weighted sum
        self.combination_method = combination_method

        if self.combination_method == 'learn_classifier':

            self.layers_lstm = nn.Sequential(
                nn.Linear(5, 5),
                nn.ReLU()
            )

            self.layers_vae = nn.Sequential(
                nn.Linear(5, 5),
                nn.ReLU()
            )

            self.shared = nn.Sequential(
                nn.Linear(10, 5),
                nn.Sigmoid()
            )

        elif self.combination_method == "learn_sum":

            self.W_classifier = nn.Parameter(torch.ones((num_classes,)) * 0.5)
            self.W_vaes = nn.Parameter(torch.ones((num_classes,)) * 0.5)
            self.W_classifier.requires_grad = True
            self.W_vaes.requires_grad = True

            if combined_weights_load is not None:
                weights_load = os.path.join(GITIGNORED_DIR, RESULTS_DIR, combined_weights_load)
                datamanager = DataManager(weights_load)
                loaded = datamanager.load_python_obj(os.path.join('models', 'finished'))

            for state_dict in loaded.values():
                state_dict = state_dict
            self.load_state_dict(state_dict)

            print(self.W_classifier)
            print(self.W_vaes)

    def forward(self, inp, targets, lengths, inp_sentence, step):
        inp2, targets2, lengths2 = inp_sentence
        out_base = self.base_classifier.forward(inp.detach(), lengths.detach())  # need to put through softmax
        out_base_class_scores = nn.functional.softmax(*out_base, dim=-1).detach()

        _, _, vae_loss = self.vae_classifier.forward(inp2, lengths2, step, targets2)
        vae_loss = torch.stack(vae_loss)
        vae_likelihood = - vae_loss
        vae_score = nn.Softmax(dim=-1)(vae_likelihood)

        if 'joint' in self.combination_method:
            combined_score = self.joint_probability(out_base_class_scores, vae_score)
        elif 'learn' in self.combination_method:
            combined_score = self.weighted_sum(out_base_class_scores, vae_score)
        else:
            raise Exception

        return combined_score, (out_base_class_scores, vae_likelihood)

        # todo | keep in mind that scores should be as if they're probabilities, meaning for
        # todo | classification we take argmax and classify directly

    def joint_probability(self, pxy, px):
        approach_px = torch.exp(px)
        return pxy * approach_px

    def weighted_sum(self, classifier_score, vaes_score):  # TODO: give elbo or recons or (regul??) ??
        if self.combination_method == "learn_classifier":
            return self.shared.forward(torch.cat((self.layers_lstm.forward(classifier_score.detach().squeeze()),
                                                  self.layers_vae.forward(vaes_score.detach())), dim=0))

        elif self.combination_method == "learn_sum":
            return self.W_classifier * classifier_score.detach() + self.W_vaes * vaes_score.detach()


# -*- encode: utf-8 -*-
from copy import copy
from functools import partial, wraps
from typing import Hashable, Callable

class Pipe:
	def __init__(self, func=None, *args, **kwargs):
		# initialization args
		self.func = func
		self.args = args
		self.kwargs = kwargs
		# attributions
		self.cache = kwargs.get('cache', False)
		self.unpack = kwargs.get('unpack', False)
		self.append = kwargs.get('append', False)
		self.done = False
		# cached result
		self.result = {None:None}

	def __repr__(self):
		cached = '(cached)' if self.cache else ''
		unpacked = '(unpacked)' if self.unpack else ''
		appended = '(appended)' if self.append else ''
		hash_ = hash(self)
		return f'<Pipe{cached}{unpacked}{appended} @ {hash_:#x}>'

	def __or__(self, value):
		assert isinstance(value, (Pipe, Callable)), 'Pipe or Callable required.'
		if isinstance(value, Pipe) and self.func is None:
			return value(self())
		def couple(*args, **kwargs):
			return value(self(*args, **kwargs))
		return Pipe(couple)

	def __ror__(self, value):
		return self(value)

	def __getitem__(self, args):
		if isinstance(args, dict):
			args, kwargs = tuple(), args
		elif isinstance(args, tuple):
			args, kwargs = args, dict()
		elif len(args) == 0:
			args, kwargs = tuple(), dict()
		elif len(args) == 1:
			args, kwargs = (tuple(), args[0]) if isinstance(args[0], dict) else \
				(tuple(args[0]), dict())
		elif len(args) == 2:
			args, kwargs = args
		assert isinstance(args, tuple), 'Argument type error.'
		assert isinstance(kwargs, dict), 'Argument type error.'
		temp = self.copy()
		temp.args += args
		temp.kwargs.update(kwargs)
		return temp

	def __call__(self, arg=None, *args, **kwargs):
		if self.unpack:
			args = (args+arg) if self.append else (arg+args)
		else:
			assert args==(), 'One argument required if not unpacked.'
			assert kwargs=={}, 'One argument required if not unpacked.'
		if self.func is None:
			result = tuple(filter(bool, (arg, self.args, self.kwargs)))
			if not result:
				return None
			elif len(result) == 1:
				return result[0]
			return result
		elif isinstance(arg, Pipe):
			return self.func(*arg.args, **arg.kwargs)
		if arg is not None:
			if self.unpack:
				args = (self.args+args) if self.append else (args+self.args)
				kwargs.update(self.kwargs)
			if self.cache:
				if isinstance(arg, Hashable):
					if arg not in self.result:
						self.result[arg] = \
							self.func(*args, **kwargs) if self.unpack else \
							self.func(arg, *self.args, **self.kwargs)
					return self.result[arg]
			return self.func(*args, **kwargs) if self.unpack else \
				(self.func(*self.args, arg, **self.kwargs) if self.append else \
					self.func(arg, *self.args, **self.kwargs))
		else:
			if self.cache:
				if not self.done:
					self.result[None] = \
						self.func(*self.args, **self.kwargs)
					self.done = True
				return self.result[None]
			return self.func(*self.args, **self.kwargs)

	def copy(self):
		return copy(self)

	def enable_cache(self):
		self.cache = True
		return self

	def enable_unpack(self):
		self.unpack = True
		return self

	def enable_append(self):
		self.append = True
		return self

def piped(func=None, cache=False, unpack=False, append=False, *args, **kwargs):
	if func is None:
		return partial(piped, cache=cache, unpack=unpack, append=append)
	result = Pipe(func, *args, **kwargs)
	if cache:  result=result.enable_cache()
	if unpack: result=result.enable_unpack()
	if append: result=result.enable_append()
	return result

if __name__ == '__main__':
	# example 1
	from operator import neg
	from random import randint
	from shutil import get_terminal_size

	r = 3.1415927
	int = Pipe(int)
	neg = Pipe(neg).enable_cache()
	abs = Pipe(abs).enable_cache()
	rnd = Pipe(randint, r|int|neg, r|int|abs)
	add = Pipe(lambda x: x+(r|int|abs))
	echo = Pipe(print, end=', ')

	col, row = get_terminal_size()
	length = min(col, row) // 2
	for _ in range(length):
		for __ in range(length):
			(rnd | add)() | echo
		print()

	# example 2
	import matplotlib.pyplot as plt

	x = (1, 2, 3, 4, 5)
	y = (2, 4, 1, 3, 5)
	z = (4, 2, 5, 3, 1)
	plot = Pipe(plt.plot).enable_unpack()
	args = Pipe(None, x, y)
	args | plot
	(x, z) | plot

	plt.show()

	# example 3
	@piped(unpack=True)
	def echo_(*args):
		print(*args, sep=', ')
		print()
	(1, 2, 3) | echo_

	# example 4
	echo__ = Pipe(print, sep=',' ).enable_unpack()
	(1, 2, 3) | echo__[{'sep': ' - '}]

	# example 5
	where = Pipe(filter).enable_append()
	tuple_ = Pipe(tuple)
	print_ = piped(unpack=True)(print, sep=': ')
	(1, 2, 3, 4) | where[(lambda x: x%2==0, )] | tuple_ | print_


import re
from typing import Tuple, Callable, Iterable, Optional
import sys
fname=sys.argv[1] if len(sys.argv)>1 else "input"
f = open(fname)
ftext=f.read()
f=[line.strip() for line in ftext.split("\n")[:-1]]

from collections import defaultdict

d=defaultdict(int)

#copied from https://github.com/joefarebrother/adventofcode/blob/master/misc_utils.py
def lmap(f: Callable, *xs) -> list:
    """Like map but returns a list"""
    return list(map(f, *xs))

def ints(xs: Iterable) -> list[int]:
    """Casts each element of xs to an int"""
    return lmap(int, xs)

def mint(x, default=None):
    """Maybe int - casts to int and returns default on failure"""
    try:
        return int(x)
    except ValueError:
        return default

def ints_in(x: str) -> list[int]:
    """Finds and parses all integers in the string x"""
    ex = r'(?:(?<!\d)-)?\d+'
    return ints(re.findall(ex, x))

class Pt(tuple):
    def __add__(self,other):
        return Pt([x+y for x,y in zip(self,other)])
    def __sub__(self,other):
        return Pt([x-y for x,y in zip(self,other)])
    def __rsub__(self,other):
        return Pt([y-x for x,y in zip(self,other)])
    def __radd__(self,other):
        return Pt([y+x for x,y in zip(self,other)])
    def __rmul__(self,other):
        return Pt([other*x for x in self])
dirs = {
    "R":Pt((0,1)),
    "U":Pt((1,0)),
    "L":Pt((0,-1)),
    "D":Pt((-1,0))
    }
ods = list(dirs.values())

#odirs = [(0,1),(1,0),(0,-1),(-1,0)]
ddirs = lmap(Pt,[(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)])

try:
    xs = ints(f)
except Exception:
    pass
try:
    xss = lmap(ints_in,f)
except Exception:
    pass

ps = """####

.#.
###
.#.

..#
..#
###

#
#
#
#

##
##"""
ps = [x.split("\n") for x in ps.split("\n\n")]
pss = []
for k in ps:
    p = set()
    for i,row in enumerate(k):
        for j,c in enumerate(row):
            if c=="#":
                p.add(Pt((j,len(k)-1-i)))
    mxx = max(x[0] for x in p)
    pss.append((p,mxx))

h = 0
for i in range(7):
    d[(i,0)] = 1

from itertools import cycle

def canmove(p,dr):
    for c in p:
        if d[c+dr] or not 0<=(c+dr)[0]<7:
            break
    else:
        return True
    return False

ps = cycle(pss)
dirs = cycle(f[0])
ds = defaultdict(list)
for i in range(1585+1705*10):
    hs = tuple([d[x,y+h] for x in range(7) for y in range(0,-10,-1)])
    ds[hs].append((i,h))
    p,mx = next(ps)
    ll = Pt((2,h+4))
    while True:
        c = next(dirs)
        if c=="<":
            if canmove(p,ll+(-1,0)):
                ll+=(-1,0)
        elif c==">":
            if canmove(p,ll+(1,0)):
                ll+=(1,0)
        if canmove(p,ll+(0,-1)):
            ll+=(0,-1)
        else:
            for c in p:
                d[c+ll]=1
                h=max(h,(c+ll)[1])
            break

ks = list(ds.keys())
a = defaultdict(int)
for k in ds:
    a[len(ds)]+=1

s=0
for (line,ints) in zip(f,xss):
    pass
print(h + 586510253*2618)


from __future__ import print_function

from models.resnet3d import resnet34
from datasets import VideoDataset, EqualDistSampler
from deploy import train
from utils import load_checkpoint
from torch.utils.data import DataLoader

import os
import torch
import csv
import argparse
import socket
import torch.optim as optim
import torch.nn as nn
import functools

from transforms.image_transforms import video_transform as transform

parser = argparse.ArgumentParser(description='Action Recognition')
parser.add_argument('--params', default='', type=str)
parser.add_argument('--name', default='default', type=str)
parser.add_argument('--dataset', default=['gestures'], nargs='+')
parser.add_argument('--version', default='random', type=str)
parser.add_argument('--version_train', nargs='+')
parser.add_argument('--version_valid', nargs='+')
parser.add_argument('--learning_rate', default=10 ** -4, type=float)
parser.add_argument('--network', default='resnet34', type=str)
parser.add_argument('--sample_size', default=112, type=int)
parser.add_argument('--resize_to', default=128, type=int)
parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--sample_length', default=24, type=int)
parser.add_argument('--max_epochs', default=50, type=int)
parser.add_argument('--seed', default=1234, type=int)
parser.add_argument('--test', action='store_true')
parser.set_defaults(test=False)
parser.add_argument('--num_workers', default=12, type=int)
parser.add_argument('--location', default='<default>', type=str)
parser.add_argument('--test_location', default='<default>', type=str)
parser.add_argument('--csv_location', default='<default>', type=str)
args = parser.parse_args()

torch.manual_seed(args.seed)

print(args)

# Get accuracte location based on hostname
if args.location == "<default>":
    args.location = '/media/datadrive/baptist/data/autolabel/'
if args.test_location == "<default>":
    args.test_location = '/media/datadrive/baptist/data/autolabel/'
if args.csv_location == "<default>":
    args.csv_location = '/home/baptist/projects/actions/data/csv/'

labels = []
for d in args.dataset:
    with open(os.path.join(args.csv_location, '%s-labels.csv'%d), 'r') as f:
        reader = csv.reader(f, delimiter=';')
        labels += [v for v, in reader]

transform_train = functools.partial(transform,  apply_resize=True,
                                                size=args.sample_size,
                                                resize_to=args.resize_to,
                                                apply_hflip=True,
                                                apply_randomcrop=True,
                                                apply_brightness=True,
                                                apply_saturation=True,
                                                apply_zoom=False,
                                                apply_wb=False)

transform_valid = functools.partial(transform, apply_resize=True,
                                               apply_centercrop=True,
                                               size=args.sample_size,
                                               resize_to=args.sample_size,
                                               apply_wb=False)

transform_test = functools.partial(transform, apply_resize=True,
                                              apply_centercrop=True,
                                              apply_focus=False,
                                              size=args.sample_size,
                                              resize_to=args.sample_size,
                                              apply_wb=False)

# List all files for train, valid, and test set.
train_files = []
valid_files = []
for d in args.dataset:
    if args.version == '':
        train_files += [os.path.join(args.csv_location, '%s-%s.csv' % (d, version)) for version in args.version_train]
        valid_files += [os.path.join(args.csv_location, '%s-%s.csv' % (d, version)) for version in args.version_valid]
    else:
        train_files += [os.path.join(args.csv_location, '%s-%s-train.csv'% (d, args.version))]
        valid_files += [os.path.join(args.csv_location, '%s-%s-validation.csv'%(d, args.version))]

dataset = dict(train=VideoDataset(train_files, labels, select='random', transform=transform_train, sample_length=args.sample_length, sample_size=args.sample_size, location=args.location),
               valid=VideoDataset(valid_files, labels, select='center', transform=transform_valid, sample_length=args.sample_length, sample_size=args.sample_size, location=args.test_location))

sampler = EqualDistSampler(dataset['train']._targets)
train_loader = DataLoader(dataset["train"], batch_size=args.batch_size, num_workers=args.num_workers, sampler=sampler) #shuffle=True) #

valid_loader = DataLoader(dataset["valid"], batch_size=args.batch_size, num_workers=args.num_workers)

if args.version == 'random':
    test_files = []
    for d in args.dataset:
        test_files += [
            os.path.join(args.csv_location, '%s-%s-test.csv' % (d, args.version))] if args.version != '' else \
            [os.path.join(args.csv_location, '%s-%s.csv' % (d, version)) for version in args.version_test]
    dataset['test'] = VideoDataset(test_files, labels, select='center', transform=transform_valid, sample_length=args.sample_length, sample_size=args.sample_size, location=args.location)
    test_loader = DataLoader(dataset["test"], batch_size=args.batch_size, num_workers=args.num_workers, shuffle=True)

net = eval(args.network)( sample_size=(3, args.sample_length, args.sample_size, args.sample_size), num_classes=len(labels))
net.cuda()

optimizer = optim.Adam(net.parameters(), lr=args.learning_rate)

if args.params != "":
    load_checkpoint(net, None, args.params, remove_module=True)

criterion = nn.CrossEntropyLoss().cuda()

if not args.test:
    print("---------")
    print("%d samples and %d batches in train set." % (len(dataset['train']), len(train_loader)))
    print("%d samples and %d batches in validation set." % (len(dataset['valid']), len(valid_loader)))
    if args.version == 'random':
        print("%d samples and %d batches in test set." % (len(dataset['test']), len(test_loader)))
    print("---------")

    train(net,
          dict(train=train_loader, valid=valid_loader, test=None if args.version != 'random' else test_loader),
          args.name,
          optimizer=optimizer,
          criterion=criterion,
          max_epochs=args.max_epochs,
          phases=['train', 'valid'] if args.version != 'random' else ['train', 'valid', 'test'],
          classlabels=labels,
    )


# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
from numpy.testing import assert_almost_equal
from astropy.time import TimeDelta
from ..utils import time_ref_from_dict, time_relative_to_ref, absolute_time

def test_time_ref_from_dict():
    mjd_int = 500
    mjd_frac = 0.5
    time_ref_dict = dict(MJDREFI=mjd_int, MJDREFF=mjd_frac)

    time_ref = time_ref_from_dict(time_ref_dict)

    assert_almost_equal(time_ref.mjd, mjd_int + mjd_frac, decimal=4)

def test_time_relative_to_ref():
    time_ref_dict = dict(MJDREFI=500, MJDREFF=0.5)
    time_ref = time_ref_from_dict(time_ref_dict)
    delta_time_1sec = TimeDelta(1., format='sec')
    time = time_ref + delta_time_1sec

    delta_time = time_relative_to_ref(time, time_ref_dict)

    assert_almost_equal(delta_time.sec, delta_time_1sec.sec, decimal=4)

def test_absolute_time():
    time_ref_dict = dict(MJDREFI=500, MJDREFF=0.5)
    time_ref = time_ref_from_dict(time_ref_dict)
    delta_time_1sec = TimeDelta(1., format='sec')
    time = time_ref + delta_time_1sec

    abs_time = absolute_time(delta_time_1sec, time_ref_dict)

    assert abs_time.value == time.utc.isot


'''
Created on Oct 6, 2017

@author: 502375911
'''
import logging
import sys

def setup_logging():
    logger = logging.getLogger()
    for h in logger.handlers:
        logger.removeHandler(h)

    h = logging.StreamHandler(sys.stdout)

    FORMAT = '%(asctime)s [%(levelname)s] (%(threadName)-10s) %(name)-15s %(message)s'
    h.setFormatter(logging.Formatter(FORMAT))
    logger.addHandler(h)
    logger.setLevel(logging.INFO)

    return logger

logger = setup_logging()

class AppRole:

    def __init__(self, dbUtil):
        self.dbutil = dbUtil

    def is_event_running(self):
        try:
            response = self.dbutil.execute_query("""
                select count(1) event from INFORMATION_SCHEMA.EVENTS WHERE EVENT_NAME='CALL_ROLE_UPDATE'
            """)
            if response[0]['event'] == 1:
                return True
            else:
                return False

        except Exception as e:
            logger.error('Error fetching count [%s]', str(e))
            raise e

    def __delete_uom_records(self):
        try:
            self.dbutil.execute_query('''
                    delete from uom.user_entity_app_role
                    where id not in ( select distinct id from uom.user_entity_app_role_baseline )
                    ''');
        except Exception as e:
            logger.error('Error deleting records ' + str(e))
            raise e

    def update_roles(self):
        try:
            #res = self.dbutil.execute_proc('update_uom_role_mapping')
            is_running = self.is_event_running()
            if is_running is False:
                self.dbutil.execute_query('''
                    CREATE EVENT CALL_ROLE_UPDATE
                        ON SCHEDULE
                            AT CURRENT_TIMESTAMP
                        DO CALL update_uom_role_mapping()
                ''')
                logger.info('Update Event called')
            else:
                logger.info('Update event is already running')
        except Exception as e:
            logger.error('Error updating roles '+ str(e))
            raise e

    def rollback_update(self):
        trunc_er = 'truncate table auth.hc_subject_attribute'
        ins_er = 'insert into auth.hc_subject_attribute select * from auth.hc_subject_attribute_baseline'
        trunc_sa = 'truncate table uom.user_entity_app_role'
        ins_sa = 'insert into uom.user_entity_app_role select * from uom.user_entity_app_role_baseline'
        try:
            logger.debug('Data rollback')
            self.dbutil.execute_query(trunc_er)
            self.dbutil.execute_query(ins_er)
            self.dbutil.execute_query(trunc_sa)
            self.dbutil.execute_query(ins_sa)
            logger.info('Rollback completed')
        except Exception as e:
            logger.error('Error rollback ' + str(e))
            raise e


from urllib import parse,request
from urllib.request import Request,urlopen
import json

class OAuth_Base(object):  # 基类，将相同的方法写入到此类中
    def __init__(self, client_id, client_key, redirect_url):  # 初始化，载入对应的应用id、秘钥和回调地址
        self.client_id = client_id
        self.client_key = client_key
        self.redirect_url = redirect_url

    def _get(self, url, data):  # get方法
        request_url = '%s?%s' % (url, parse.urlencode(data))
        response = request.urlopen(request_url)
        return response.read()

    def _post(self, url, data):  # post方法
        request = Request(url, data= parse.urlencode(data).encode(encoding='UTF8'))  # 1
        response = urlopen(request)
        return response.read()

# 微博类
class OAuth_WEIBO(OAuth_Base):
    def get_auth_url(self):
        params = {
            'client_id': self.client_id,
            'redirect_uri': self.redirect_url,
            # 'forcelogin' : 'true'
        }
        url = 'https://api.weibo.com/oauth2/authorize?%s&state=/' % parse.urlencode(params)
        return url

    def get_access_token(self, code):
        params = {
            'grant_type': 'authorization_code',
            'client_id': self.client_id,
            'client_secret': self.client_key,
            'code': code,
            'redirect_uri': self.redirect_url
        }
        response = self._post('https://api.weibo.com/oauth2/access_token', params)
        result = json.loads(response.decode('utf-8'))
        self.access_token = result["access_token"]
        return self.access_token


import discord
from discord.ext import commands
import os
import config
import images
import pyfiglet
import random

os.system('clear')

result = pyfiglet.figlet_format(config.bannerText, font="slant")
print(result)
print("Developed by @GamingLucas, github.")
print("----------------------------------")

client = commands.Bot(command_prefix=config.commandPrefix)

@client.event
async def on_ready():
    print(f'{client.user}, is now running and connected!')

@client.event
async def on_member_join(member):
    print(f'{member} has joined the server.')

@client.event
async def on_member_remove(member):
    print(f'{member} has left the server.')

@client.command()
async def stockphoto(ctx):
    await ctx.send(random.choice(images.stockImages))

client.run(config.TOKEN)


import datetime
from collections import Counter

import pandas as pd
import weka
import weka.core.converters as converters
import weka.core.jvm as jvm
from numpy import load
from weka.classifiers import Classifier, PredictionOutput, Evaluation
from weka.core.classes import Random

from utils import *

def java_start(path_packages):
    jvm.start(packages=path_packages)

def java_stop():
    jvm.stop()

def create_prediction(col_label, col_prediction, col_different, indicator, images, name, path):
    check_folder_or_create(path)
    df = pd.DataFrame()
    df['indicator'] = indicator
    df['image'] = images
    df['label'] = col_label
    df['prediction'] = col_prediction
    df['different'] = col_different
    df['prediction'] = list(map(lambda x: (x[2:]), df['prediction']))
    df['label'] = list(map(lambda x: (x[2:]), df['label']))
    df.to_csv(path + name + 'pred_data.csv', index=False)

def experiment_sequential_file(path_indices, path_features, path_folder_save_results, options, classifier, name,
                               indicator_col, images):
    ind_f = load(path_indices)
    lst = ind_f.files

    for item in lst:
        ind = ind_f[item] + 1

    cls = Classifier(classname=classifier, options=weka.core.classes.split_options(options))

    data = converters.load_any_file(path_features)

    ind = np.append(ind, len(data))

    data.class_is_last()

    pout = PredictionOutput(classname="weka.classifiers.evaluation.output.prediction.CSV")

    d_results = {'index': [], 'percent_correct': [], 'percent_incorrect': [], 'precision': [], 'recall': [], 'f-score': [], 'confusion_matrix': []}

    for j in range(len(ind) - 1):
        first = ind[j]

        if j == len(ind) - 2:
            last = ind[j + 1]
        else:
            last = ind[j + 1] - 1

        d_test = data.subset(row_range=str(first) + '-' + str(last))

        if j == 0:  # first
            d_train = data.subset(row_range=str(last + 1) + '-' + str(ind[-1]))  # last element
            print(str(last + 1) + '-' + str(ind[-1]))
        elif j == len(ind) - 2:  # last
            d_train = data.subset(row_range='1-' + str(first - 1))  # last element
            print('1-' + str(first - 1))
        else:  # central
            s = '1-' + str(first - 1) + ',' + str(last + 1) + '-' + str(ind[-1])
            print(s)
            d_train = data.subset(row_range=s)

        cls.build_classifier(d_train)

        evl = Evaluation(data)
        evl.test_model(cls, d_test, pout)

        # print(type(d_train))
        # print(type(d_test))

        d_results['index'].append(str(ind[j]))
        d_results['percent_correct'].append(evl.percent_correct)
        d_results['percent_incorrect'].append(evl.percent_incorrect)
        d_results['precision'].append(evl.precision(1))
        d_results['recall'].append(evl.recall(1))
        d_results['f-score'].append(evl.f_measure(1))
        d_results['confusion_matrix'].append(evl.matrix())  # Generates the confusion matrix.

    save = pout.buffer_content()

    check_folder_or_create(path_folder_save_results + '/' + 'prediction')

    with open(path_folder_save_results + '/' + 'prediction/' + name + 'pred_data.csv', 'w') as f:
        f.write(save)

    buffer_save = pd.read_csv(path_folder_save_results + '/' + 'prediction/' + name + 'pred_data.csv', index_col=False, header=None)

    col_label = buffer_save[1]
    col_prediction = buffer_save[2]
    col_different = buffer_save[3]

    create_prediction(col_label, col_prediction, col_different, indicator_col, images, name, path_folder_save_results + '/prediction/')

    d_results = pd.DataFrame(data=d_results)

    d_results.to_csv(path_folder_save_results + '/' + name + 'results.csv', index=False)

def experiment_file_random(path_features, path_folder_save_results, options, classifier, fold, random, name):
    print(name + "  Start: " + str(datetime.datetime.now()))
    time = datetime.datetime.now()
    cls = Classifier(classname=classifier, options=weka.core.classes.split_options(options))
    d_results = {'percent_correct': [], 'percent_incorrect': [], 'precision': [], 'recall': [], 'f-score': [], 'confusion_matrix': []}
    data = converters.load_any_file(path_features)
    data.class_is_last()
    pout = PredictionOutput(classname="weka.classifiers.evaluation.output.prediction.CSV")
    evl = Evaluation(data)
    evl.crossvalidate_model(cls, data, fold, Random(random), pout)
    d_results['percent_correct'].append(evl.percent_correct)
    d_results['percent_incorrect'].append(evl.percent_incorrect)
    d_results['precision'].append(evl.precision(1))
    d_results['recall'].append(evl.recall(1))
    d_results['f-score'].append(evl.f_measure(1))
    d_results['confusion_matrix'].append(evl.matrix())  # Generates the confusion matrix.

    d_results = pd.DataFrame(data=d_results)

    d_results.to_csv(path_folder_save_results + '/' + str(name) + '.csv', index=False)

    save = pout.buffer_content()

    check_folder_or_create(path_folder_save_results + '/' + 'prediction')

    with open(path_folder_save_results + '/' + 'prediction/' + str(name) + '.csv', 'w') as f:
        f.write(save)
    print(name + "  End: " + str(datetime.datetime.now() - time))

def experiment_more_file(path_files, path_folder_save_results, fold, options, classifier, random, name, voting=False):
    cls = Classifier(classname=classifier, options=weka.core.classes.split_options(options))

    file_list = os.listdir(path_files)

    for file in file_list:
        if ".csv" not in file:
            file_list.remove(file)

    d_results = {'name_file': [], 'percent_correct': [], 'percent_incorrect': [], 'precision': [], 'recall': [], 'f-score': [], 'confusion_matrix': []}

    for file in file_list:
        indicator_table = pd.read_csv(path_files + '/indicator/' + file[0] + '_indicator.csv')
        indicator = list(indicator_table['indicator'])
        images = list(indicator_table['image'])

        data = converters.load_any_file(path_files + "/" + file)

        data.class_is_last()

        pout = PredictionOutput(classname="weka.classifiers.evaluation.output.prediction.CSV")

        evl = Evaluation(data)

        evl.crossvalidate_model(cls, data, fold, Random(random), pout)

        d_results['name_file'].append(str(file))
        d_results['percent_correct'].append(evl.percent_correct)
        d_results['percent_incorrect'].append(evl.percent_incorrect)
        d_results['precision'].append(evl.precision(1))
        d_results['recall'].append(evl.recall(1))
        d_results['f-score'].append(evl.f_measure(1))
        d_results['confusion_matrix'].append(evl.matrix())  # Generates the confusion matrix.

        save = pout.buffer_content()

        check_folder_or_create(path_folder_save_results + '/' + name + '/' + 'prediction')

        with open(path_folder_save_results + '/' + name + '/' + 'prediction/pred_data.csv',
                  'w') as f:
            f.write(save)

        buffer_save = pd.read_csv(path_folder_save_results + '/' + name + '/' + 'prediction/pred_data.csv',
                                  index_col=False)

        col_label = buffer_save['actual']
        col_prediction = buffer_save['predicted']
        col_different = buffer_save['error']

        create_prediction(col_label, col_prediction, col_different, indicator, images, file[:-4],
                          path_folder_save_results + '/' + name + '/prediction/')

    d_results = pd.DataFrame(data=d_results)

    d_results.to_csv(path_folder_save_results + '/' + str(name) + ".csv")

def check(indicator, image_labels, details_indicator):
    flag = False
    if len(image_labels) % 2 == 0:
        most_c = Counter(image_labels).most_common()
        if len(most_c) == 2:
            a = Counter(image_labels).most_common()[0]
            b = Counter(image_labels).most_common()[1]
            if a[1] == b[1]:
                flag = True
                details_indicator[indicator]['tot'] = details_indicator[indicator]['tot'] + 1
                if a[0] == 'attentive':
                    details_indicator[indicator]['attentive first'] = details_indicator[indicator][
                                                                          'attentive first'] + 1
                else:
                    details_indicator[indicator]['distracted first'] = details_indicator[indicator][
                                                                           'distracted first'] + 1
    return details_indicator, flag

def voting_version_check_len(path_file_prediction, folder_to_save_results):
    predictions = [pd.read_csv(path_file_prediction + "/" + str(el), index_col=False) for el in
                   sorted(os.listdir(path_file_prediction))]
    name_files = sorted(os.listdir(path_file_prediction))

    for i in range(len(predictions)):
        image_labels = list()
        prediction = list()

        details_indicator = {'over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0, 'label assigned attentive': 0,
                                  'label assigned distracted': 0},
                             'attentive':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0, 'label assigned attentive': 0,
                                  'label assigned distracted': 0},
                             'error-no over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0, 'label assigned attentive': 0,
                                  'label assigned distracted': 0},
                             'error-over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0, 'label assigned attentive': 0,
                                  'label assigned distracted': 0},
                             }

        p = predictions[i]
        #print(p.columns)

        if list(p.columns) == ['indicator', 'image', 'label', 'prediction', 'different']:

            for index in range(p.shape[0]):
                if len(image_labels) > 0:
                    if p['image'][index] != p['image'][index - 1]:

                        details_indicator, flag = check(p['indicator'][index - 1], image_labels, details_indicator)

                        if flag:
                            if len(image_labels) > 4:
                                vote = 'distracted'
                                details_indicator[p['indicator'][index - 1]]['label assigned distracted'] = \
                                    details_indicator[p['indicator'][index - 1]]['label assigned distracted'] + 1
                            else:
                                vote = 'attentive'
                                details_indicator[p['indicator'][index - 1]]['label assigned attentive'] = \
                                    details_indicator[p['indicator'][index - 1]]['label assigned attentive'] + 1
                        else:
                            vote = Counter(image_labels).most_common()[0][0]

                        if vote != p['label'][index - 1]:
                            sign = 'incorrect'
                        else:
                            sign = 'correct'

                        prediction.append(
                            (p['image'][index - 1], p['indicator'][index - 1], p['label'][index - 1], vote, sign))
                        image_labels = list()

                image_labels.append(p['prediction'][index])

            create_results_voting(prediction, folder_to_save_results, name_files[i][:-4], i, details_indicator)

def voting(path_file_prediction, folder_to_save_results):
    predictions = [pd.read_csv(path_file_prediction + "/" + str(el), index_col=False) for el in
                   sorted(os.listdir(path_file_prediction))]
    name_files = sorted(os.listdir(path_file_prediction))

    for i in range(len(predictions)):
        image_labels = list()
        prediction = list()

        details_indicator = {'over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0},
                             'attentive':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0},
                             'error-no over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0},
                             'error-over time':
                                 {"tot": 0, 'attentive first': 0, 'distracted first': 0},
                             }

        p = predictions[i]

        if list(p.columns) == ['indicator', 'image', 'label', 'prediction', 'different'] or list(p.columns) == ['instance','label','prediction','different','perc_error','indicator','image']:

            for index in range(p.shape[0]):
                if len(image_labels) > 0:
                    if p['image'][index] != p['image'][index - 1]:
                        vote = Counter(image_labels).most_common()[0][0]

                        details_indicator, flag = check(p['indicator'][index - 1], image_labels, details_indicator)

                        if vote != p['label'][index - 1]:
                            sign = 'incorrect'
                        else:
                            sign = 'correct'

                        prediction.append(
                            (p['image'][index - 1], p['indicator'][index - 1], p['label'][index - 1], vote, sign))
                        image_labels = list()

                image_labels.append(p['prediction'][index])

            create_results_voting(prediction, folder_to_save_results, name_files[i][:-4], i, details_indicator)

def create_results_voting(prediction, folder_to_save_results, name_file, i, details_indicator):
    df = pd.DataFrame(prediction, columns=['image', 'indicator', 'original label', 'prediction', 'status'])
    check_folder_or_create(folder_to_save_results + '/details')

    sys.stdout = open(folder_to_save_results + '/details/' + name_file + '_' + ".csv", "w")
    print(name_file)
    print('\n')
    print('perc incorrect: ' + str((list(df['status']).count('incorrect') * 100) / df.shape[0]))
    print('perc correct: ' + str((list(df['status']).count('correct') * 100) / df.shape[0]))
    print('\n')
    df.to_csv(folder_to_save_results + '/' + str(i) + 'prediction_voting.csv')
    c = [x for _, x in df.groupby(['status'])]
    print("Numero totale di immagini divise per indicatore:\n" + str(
        (df[['indicator', 'image']].groupby(['indicator']).count())))
    print('\n\n')
    print("Numero di immagini classificate scorrettamente con indicatore:\n" + str(
        (c[1][['indicator', 'status']].groupby(['indicator']).count())))
    print('\n')
    print("Numero di immagini classificate scorrettamente con label:\n" + str(
        (c[1][['original label', 'status']].groupby(['original label']).count())))
    print('\n\n')
    print("Caso feature pari e nessuna classe in numero maggiore: \n")
    print('attentive')
    print(details_indicator['attentive'])
    print("over time")
    print(details_indicator['over time'])
    print("error-no over time")
    print(details_indicator['error-no over time'])
    print("error-over time")
    print(details_indicator['error-over time'])
    sys.stdout.close()


#!/usr/bin/python
#import glob
#import re
import itertools
import numpy as np
#import scipy as sp
#from scipy.optimize import curve_fit
import matplotlib.cm as cm
import matplotlib.pyplot as plt
#from matplotlib.widgets import Slider, Button, CheckButtons
c=[]
for i in np.arange(10):c.append(cm.jet(1-i/10.))
colors =itertools.cycle(c)
def Lorentz(x,f0,fwhm,a0):
	return (a0 * (0.5 * fwhm)/((x * f0)**2.0 + 0.25 * fwhm**2))

def J_debye(nu,tau):
	omega=nu*2.*np.pi
	return (tau/(1+(omega*tau)**2))
def J_cd(nu,tau,beta=1):
	omega=nu*2.*np.pi
	a=omega*tau*beta
	return (1./omega*
		np.sin(beta*np.arctan(a))/((1.+a**2.)**(beta/2.))
		)
def R1_CSA(nu,tau,delta_sigma,beta=1):
	omega=nu*2.*np.pi
	return (0.3*omega**2.*delta_sigma**2.*J_cd(nu,tau,beta))
def R1_DD(nu,tau,K_dd,beta=1):
	return (K_dd*(J_cd(nu,tau,beta)+4.*J_cd(2.*nu,tau,beta)))
def R1_ges(nu,tau,K_dd,delta_sigma,beta=1.):
	return (R1_DD(nu,tau,K_dd,beta)+R1_CSA(nu,tau,delta_sigma,beta))
delta_sigma_ppm=220e-6
nu=np.logspace(1.5,12.2,120)
nu=121.49e6
plt.ion()
plt.figure(1)
rateax=plt.axes([0.05,0.1,0.8,0.8])
plt.xscale('log')
plt.yscale('log')
tau=np.logspace(-11,-7,120)
rateax.plot(tau,1/R1_CSA(nu,tau,delta_sigma_ppm,0.4),label='beta = 0.4',color=c[2])
rateax.plot(tau,1/R1_CSA(nu,tau,delta_sigma_ppm,1),label='beta = 1',color=c[1])
#rateax.plot(tau,1/R1_CSA(nu,tau,delta_sigma_ppm,0.9),color=c[2])
rateax.legend()
plt.show()
i=raw_input('ente')


import numpy as np
import array
import os, sys
import re

import smpy.smpy as S
from scipy.interpolate import CubicSpline

from astropy.table import Table
from astropy import units as u

def f99_extinction(wave):
    """
    Return Fitzpatrick 99 galactic extinction curve as a function of wavelength
    """
    anchors_x = [0., 0.377, 0.820, 1.667, 1.828, 2.141, 2.433, 3.704, 3.846]
    anchors_y = [0., 0.265, 0.829, 2.688, 3.055, 3.806, 4.315, 6.265, 6.591]

    f99 = CubicSpline(anchors_x, anchors_y)

    output_x = (1 / wave.to(u.micron)).value

    return f99(output_x)

def process(catalog_in, translate_file, filter_file,
            cat_format = 'ascii.commented_header',
            id_col = 'id', flux_col = 'flux',
            fluxerr_col = 'fluxerr', exclude_columns=None,
            correct_extinction=True,
            overwrite=True, verbose=False):

    input_data = Table.read(catalog_in, format=cat_format)
    if exclude_columns != None:
        input_data.remove_columns(exclude_columns)
    column_names = input_data.columns.keys()

    ID = input_data[id_col]

    flux_col_end = flux_col
    fluxerr_col_end = fluxerr_col

    try:
        translate_init = Table.read(translate_file, format='ascii.no_header')
        fnames = translate_init['col1']
        fcodes = translate_init['col2']
        flux_cols = np.array([a.startswith('F') for a in fcodes])
        fluxerr_cols = np.array([a.startswith('E') for a in fcodes])

    except:
        raise

        # Parse filter file with smpy - get central wavelengths back
    filt_obj = S.LoadEAZYFilters(filter_file)

    lambda_cs = np.zeros(flux_cols.sum())
    f99_means = np.zeros(flux_cols.sum())

    for il, line in enumerate(translate_init[flux_cols]):
        filtnum = int(line['col2'][1:])-1
        lambda_cs[il] = filt_obj.filters[filtnum].lambda_c.value

        wave = filt_obj.filters[filtnum].wave
        resp = filt_obj.filters[filtnum].response

        f99_ext = f99_extinction(wave)
        f99_means[il] = np.trapz(resp*f99_ext, wave.value) / np.trapz(resp, wave.value)

    wl_order = np.argsort(lambda_cs)

    flux_colnames_ordered = fnames[flux_cols][wl_order]
    fluxerr_colnames_ordered = fnames[fluxerr_cols][wl_order]

    f99_means_ordered = f99_means[wl_order]

    fluxes = np.zeros((len(input_data), len(flux_colnames_ordered)))
    fluxerrs = np.zeros((len(input_data), len(flux_colnames_ordered)))

    for i in range(len(flux_colnames_ordered)):
        if correct_extinction:
            try:
                extinctions = f99_means_ordered[i]*input_data['EBV']
                flux_correction = 10**(extinctions/2.5)

                isgood = (input_data[fluxerr_colnames_ordered[i]] > 0.)

                input_data[flux_colnames_ordered[i]][isgood] *= flux_correction[isgood]
            except:
                raise

        fluxes[:,i] = input_data[flux_colnames_ordered[i]]
        fluxerrs[:,i] = input_data[fluxerr_colnames_ordered[i]]

    fluxes = fluxes[:,1:-1]
    fluxerrs = fluxerrs[:,1:-1]
    fnames_full = flux_colnames_ordered[1:-1]
    efnames_full = fluxerr_colnames_ordered[1:-1]
    fnames = [filt.split('_')[0] for filt in fnames_full]

    color_a = np.zeros(fluxes.shape)
    color_b = np.zeros(fluxes.shape)

    upper_lims = np.where(np.logical_and(fluxes/fluxerrs < 2., fluxerrs > 0.))
    fluxes[upper_lims] = -99.
    #np.maximum(fluxes[upper_lims], 0) + 2.*fluxerrs[upper_lims]

    color_a[:,1:] = -2.5*np.log10(fluxes[:,:-1]/fluxes[:,1:])
    color_b[:,:-1] = -2.5*np.log10(fluxes[:,:-1]/fluxes[:,1:])

    # Identify anomalous datapoints by extreme red/blue colours
    # Or strong jumps in colour

    drop = np.logical_and(color_a < -1., color_b > 1.)
    spike = np.logical_and(color_a > 2.5, color_b < -2.5)

    criteria1 = np.logical_or(drop, spike)
    criteria2 = np.logical_or(color_a < -5, color_b > 5)

    anomalous = np.logical_or(criteria1,criteria2) #* (fluxes / fluxerrs > 2.)
    #anomalous_bands = I[anomalous]

    good = (fluxerrs > 0)
    fraction_bad = (good*anomalous).sum(0).astype('float') / good.sum(0)
    gg = zip(fnames, fraction_bad*100)

    for i, name in enumerate(fnames_full):
        if verbose:
            print(name)
        input_data[name][anomalous[:,i]] = -90.
        input_data[efnames_full[i]][anomalous[:,i]] = -90.

    outname = catalog_in+'.mod'

    if (overwrite == True) and os.path.isfile(outname):
        os.remove(outname)

    input_data.write(outname, format=cat_format)

    return outname, gg

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-p","--params", type=str,
                        help = "Parameter file")
    parser.add_argument("-q", "--quiet", help = "Suppress extra outputs",
                        action = "store_true")
    args = parser.parse_args()
    quiet = args.quiet

    params_root = re.split(".py", args.params)[0]
    if os.path.isfile(params_root+".pyc"):
        os.remove(params_root+".pyc")

    import importlib
    try:
        pipe_params = importlib.import_module(params_root)
        print('Successfully loaded "{0}" as params'.format(args.params))
        reload(pipe_params)
    except:
        print('Failed to load "{0}" as params'.format(args.params))
        raise

    catalog_in = '/data2/ken/photoz/ezpipe-bootes/Bootes_merged_Icorr_2014a_all_ap4_mags.zs.fits'

    outname, bf, f, fe = process(catalog_in, '{0}/{1}'.format(pipe_params.working_folder, pipe_params.translate_file),
                                '{0}/{1}'.format(pipe_params.working_folder, pipe_params.filter_file),
                                cat_format='fits')


# general tools
import sys
import time
import argparse
from glob import glob
from datetime import datetime, timedelta

# data tools
import h5py
import numpy as np

# ANN tools
from tensorflow import keras

# custom tools
sys.path.insert(0, '/glade/u/home/ksha/WORKSPACE/utils/')
sys.path.insert(0, '/glade/u/home/ksha/WORKSPACE/DL_downscaling/')
sys.path.insert(0, '/glade/u/home/ksha/WORKSPACE/DL_downscaling/utils/')
import data_utils as du
import verif_utils as vu
import pipeline_utils as pu
from namelist import *

# parse user inputs
parser = argparse.ArgumentParser()
# positionals
parser.add_argument('y', help='year')
args = vars(parser.parse_args())
year = int(args['y'])

# defining prediction range
# dt list for all
N_all = 365 + 366 + 365 + 365 + 365
all_list = [datetime(2015, 1, 1, 0) + timedelta(days=x) for x in range(N_all)]
# pred range

N_pred = 365
pred_list = [datetime(year, 1, 1, 0) + timedelta(days=x) for x in range(N_pred)]
# indices
ind_pred = du.dt_match(all_list, pred_list)

# import geo. info
with h5py.File(PRISM_dir+'land_mask_NA.hdf', 'r') as hdf_io:
    lon_4km = hdf_io['lon_4km'][...]
    lat_4km = hdf_io['lat_4km'][...]
    lon_025 = hdf_io['lon_025'][...]
    lat_025 = hdf_io['lat_025'][...]
    etopo_4km = hdf_io['etopo_4km'][...]
    etopo_regrid = hdf_io['etopo_regrid'][...]
    land_mask = hdf_io['land_mask'][...]

grid_shape = land_mask.shape

# macros
# cycle-GAN import
model_import_dir = temp_dir
CGAN = keras.models.load_model(model_import_dir+'Cycle-GAN_TMEAN_LR.hdf')

# overlapped tile prediction settings
param = {}
param['gap'] = 22
param['edge'] = 44
param['size'] = 96

# loop over variables and seasons
VARS = ['TMEAN']

for VAR in VARS:
    # import 3d (time, lat, lon) features
    with h5py.File(JRA_dir + 'JRA_{}_features_2015_2020.hdf'.format(VAR), 'r') as hdf_io:
        REGRID_T = hdf_io['{}_REGRID'.format(VAR)][ind_pred, ...]

    shape_3d = REGRID_T.shape
    RESULT_CLEAN = np.zeros(shape_3d)
    RESULT_025 = np.zeros((shape_3d[0],)+lon_025.shape)

    for n in range(shape_3d[0]):
        #start_time = time.time()
        print('\t{}'.format(n))
        X = (REGRID_T[n, ...], etopo_regrid)
        temp_unet = vu.pred_domain(X, land_mask, CGAN, param, method='norm_std')
        #temp_025 = du.nearest_wraper(lon_4km, lat_4km, temp_unet, lon_025, lat_025)
        temp_025 = du.interp2d_wraper(lon_4km, lat_4km, temp_unet, lon_025, lat_025, method=interp_method)
        temp_4km = du.interp2d_wraper(lon_025, lat_025, temp_025, lon_4km, lat_4km, method=interp_method)
        RESULT_025[n, ...] = temp_025
        RESULT_CLEAN[n, ...] = temp_4km
        #print("--- %s seconds ---" % (time.time() - start_time))

    RESULT_CLEAN[:, land_mask] = np.nan
    tuple_save = (lon_4km, lat_4km, RESULT_CLEAN, RESULT_025, etopo_4km, etopo_regrid)
    label_save = ['lon_4km', 'lat_4km', '{}_REGRID'.format(VAR), '{}_025'.format(VAR), 'etopo_4km', 'etopo_regrid']
    du.save_hdf5(tuple_save, label_save, out_dir=JRA_dir, filename='JRA_{}_clean_{}.hdf'.format(VAR, year))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('beta', '0015_dailyautochk_chk_date'),
    ]

    operations = [
        migrations.CreateModel(
            name='AutoChk',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
                ('fs_chk', models.TextField()),
                ('proc_chk', models.TextField()),
                ('port_chk', models.TextField()),
                ('itm_chk', models.TextField()),
                ('uptm_chk', models.TextField()),
                ('chk_date', models.DateField()),
                ('group', models.ForeignKey(to='beta.DifGroup')),
                ('host', models.ForeignKey(to='beta.Host')),
                ('system', models.ForeignKey(to='beta.System')),
            ],
            options={
                'ordering': ('group__groupname', 'system__systemname', 'host__hostname'),
            },
        ),
        migrations.RemoveField(
            model_name='dailyautochk',
            name='group',
        ),
        migrations.RemoveField(
            model_name='dailyautochk',
            name='host',
        ),
        migrations.RemoveField(
            model_name='dailyautochk',
            name='system',
        ),
        migrations.DeleteModel(
            name='DailyAutochk',
        ),
    ]


from django.conf.urls import url
from .views import *

urlpatterns = [
    url(r'^$', index, name='index'),
    url(r'^old_login_page/?$', index, name='old_login_page', kwargs={'landing': False}),
    url(r'^login_page/?$', index, name='login_page'),
    url(r'^guest_login/?$', guest_login, name='guest_login'),
    url(r'^login/?$', process_login, name='process_login'),
    url(r'^logout/?$', trigger_logout, name='trigger_logout'),
    url(r'^logout/login/?$', logout_login, name='logout_login'),
]